2006-03-15 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / config / arm / arm.c
blob2e5c9c43bb8fbdba72fab7375cf6f507851d8bf6
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_CXX_GUARD_TYPE
316 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
318 #undef TARGET_CXX_GUARD_MASK_BIT
319 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
321 #undef TARGET_CXX_GET_COOKIE_SIZE
322 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
324 #undef TARGET_CXX_COOKIE_HAS_SIZE
325 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
327 #undef TARGET_CXX_CDTOR_RETURNS_THIS
328 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
330 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
331 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
333 #undef TARGET_CXX_USE_AEABI_ATEXIT
334 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
336 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
337 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
338 arm_cxx_determine_class_data_visibility
340 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
341 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
343 #undef TARGET_RETURN_IN_MSB
344 #define TARGET_RETURN_IN_MSB arm_return_in_msb
346 #undef TARGET_MUST_PASS_IN_STACK
347 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
349 #ifdef TARGET_UNWIND_INFO
350 #undef TARGET_UNWIND_EMIT
351 #define TARGET_UNWIND_EMIT arm_unwind_emit
353 /* EABI unwinding tables use a different format for the typeinfo tables. */
354 #undef TARGET_ASM_TTYPE
355 #define TARGET_ASM_TTYPE arm_output_ttype
357 #undef TARGET_ARM_EABI_UNWINDER
358 #define TARGET_ARM_EABI_UNWINDER true
359 #endif /* TARGET_UNWIND_INFO */
361 #undef TARGET_CANNOT_COPY_INSN_P
362 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
364 #ifdef HAVE_AS_TLS
365 #undef TARGET_HAVE_TLS
366 #define TARGET_HAVE_TLS true
367 #endif
369 #undef TARGET_CANNOT_FORCE_CONST_MEM
370 #define TARGET_CANNOT_FORCE_CONST_MEM arm_tls_referenced_p
372 struct gcc_target targetm = TARGET_INITIALIZER;
374 /* Obstack for minipool constant handling. */
375 static struct obstack minipool_obstack;
376 static char * minipool_startobj;
378 /* The maximum number of insns skipped which
379 will be conditionalised if possible. */
380 static int max_insns_skipped = 5;
382 extern FILE * asm_out_file;
384 /* True if we are currently building a constant table. */
385 int making_const_table;
387 /* Define the information needed to generate branch insns. This is
388 stored from the compare operation. */
389 rtx arm_compare_op0, arm_compare_op1;
391 /* The processor for which instructions should be scheduled. */
392 enum processor_type arm_tune = arm_none;
394 /* Which floating point model to use. */
395 enum arm_fp_model arm_fp_model;
397 /* Which floating point hardware is available. */
398 enum fputype arm_fpu_arch;
400 /* Which floating point hardware to schedule for. */
401 enum fputype arm_fpu_tune;
403 /* Whether to use floating point hardware. */
404 enum float_abi_type arm_float_abi;
406 /* Which ABI to use. */
407 enum arm_abi_type arm_abi;
409 /* Which thread pointer model to use. */
410 enum arm_tp_type target_thread_pointer = TP_AUTO;
412 /* Used to parse -mstructure_size_boundary command line option. */
413 int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
415 /* Used for Thumb call_via trampolines. */
416 rtx thumb_call_via_label[14];
417 static int thumb_call_reg_needed;
419 /* Bit values used to identify processor capabilities. */
420 #define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
421 #define FL_ARCH3M (1 << 1) /* Extended multiply */
422 #define FL_MODE26 (1 << 2) /* 26-bit mode support */
423 #define FL_MODE32 (1 << 3) /* 32-bit mode support */
424 #define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
425 #define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
426 #define FL_THUMB (1 << 6) /* Thumb aware */
427 #define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
428 #define FL_STRONG (1 << 8) /* StrongARM */
429 #define FL_ARCH5E (1 << 9) /* DSP extensions to v5 */
430 #define FL_XSCALE (1 << 10) /* XScale */
431 #define FL_CIRRUS (1 << 11) /* Cirrus/DSP. */
432 #define FL_ARCH6 (1 << 12) /* Architecture rel 6. Adds
433 media instructions. */
434 #define FL_VFPV2 (1 << 13) /* Vector Floating Point V2. */
435 #define FL_WBUF (1 << 14) /* Schedule for write buffer ops.
436 Note: ARM6 & 7 derivatives only. */
437 #define FL_ARCH6K (1 << 15) /* Architecture rel 6 K extensions. */
439 #define FL_IWMMXT (1 << 29) /* XScale v2 or "Intel Wireless MMX technology". */
441 #define FL_FOR_ARCH2 0
442 #define FL_FOR_ARCH3 FL_MODE32
443 #define FL_FOR_ARCH3M (FL_FOR_ARCH3 | FL_ARCH3M)
444 #define FL_FOR_ARCH4 (FL_FOR_ARCH3M | FL_ARCH4)
445 #define FL_FOR_ARCH4T (FL_FOR_ARCH4 | FL_THUMB)
446 #define FL_FOR_ARCH5 (FL_FOR_ARCH4 | FL_ARCH5)
447 #define FL_FOR_ARCH5T (FL_FOR_ARCH5 | FL_THUMB)
448 #define FL_FOR_ARCH5E (FL_FOR_ARCH5 | FL_ARCH5E)
449 #define FL_FOR_ARCH5TE (FL_FOR_ARCH5E | FL_THUMB)
450 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
451 #define FL_FOR_ARCH6 (FL_FOR_ARCH5TE | FL_ARCH6)
452 #define FL_FOR_ARCH6J FL_FOR_ARCH6
453 #define FL_FOR_ARCH6K (FL_FOR_ARCH6 | FL_ARCH6K)
454 #define FL_FOR_ARCH6Z FL_FOR_ARCH6
455 #define FL_FOR_ARCH6ZK FL_FOR_ARCH6K
457 /* The bits in this mask specify which
458 instructions we are allowed to generate. */
459 static unsigned long insn_flags = 0;
461 /* The bits in this mask specify which instruction scheduling options should
462 be used. */
463 static unsigned long tune_flags = 0;
465 /* The following are used in the arm.md file as equivalents to bits
466 in the above two flag variables. */
468 /* Nonzero if this chip supports the ARM Architecture 3M extensions. */
469 int arm_arch3m = 0;
471 /* Nonzero if this chip supports the ARM Architecture 4 extensions. */
472 int arm_arch4 = 0;
474 /* Nonzero if this chip supports the ARM Architecture 4t extensions. */
475 int arm_arch4t = 0;
477 /* Nonzero if this chip supports the ARM Architecture 5 extensions. */
478 int arm_arch5 = 0;
480 /* Nonzero if this chip supports the ARM Architecture 5E extensions. */
481 int arm_arch5e = 0;
483 /* Nonzero if this chip supports the ARM Architecture 6 extensions. */
484 int arm_arch6 = 0;
486 /* Nonzero if this chip supports the ARM 6K extensions. */
487 int arm_arch6k = 0;
489 /* Nonzero if this chip can benefit from load scheduling. */
490 int arm_ld_sched = 0;
492 /* Nonzero if this chip is a StrongARM. */
493 int arm_tune_strongarm = 0;
495 /* Nonzero if this chip is a Cirrus variant. */
496 int arm_arch_cirrus = 0;
498 /* Nonzero if this chip supports Intel Wireless MMX technology. */
499 int arm_arch_iwmmxt = 0;
501 /* Nonzero if this chip is an XScale. */
502 int arm_arch_xscale = 0;
504 /* Nonzero if tuning for XScale */
505 int arm_tune_xscale = 0;
507 /* Nonzero if we want to tune for stores that access the write-buffer.
508 This typically means an ARM6 or ARM7 with MMU or MPU. */
509 int arm_tune_wbuf = 0;
511 /* Nonzero if generating Thumb instructions. */
512 int thumb_code = 0;
514 /* Nonzero if we should define __THUMB_INTERWORK__ in the
515 preprocessor.
516 XXX This is a bit of a hack, it's intended to help work around
517 problems in GLD which doesn't understand that armv5t code is
518 interworking clean. */
519 int arm_cpp_interwork = 0;
521 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
522 must report the mode of the memory reference from PRINT_OPERAND to
523 PRINT_OPERAND_ADDRESS. */
524 enum machine_mode output_memory_reference_mode;
526 /* The register number to be used for the PIC offset register. */
527 unsigned arm_pic_register = INVALID_REGNUM;
529 /* Set to 1 when a return insn is output, this means that the epilogue
530 is not needed. */
531 int return_used_this_function;
533 /* Set to 1 after arm_reorg has started. Reset to start at the start of
534 the next function. */
535 static int after_arm_reorg = 0;
537 /* The maximum number of insns to be used when loading a constant. */
538 static int arm_constant_limit = 3;
540 /* For an explanation of these variables, see final_prescan_insn below. */
541 int arm_ccfsm_state;
542 enum arm_cond_code arm_current_cc;
543 rtx arm_target_insn;
544 int arm_target_label;
546 /* The condition codes of the ARM, and the inverse function. */
547 static const char * const arm_condition_codes[] =
549 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
550 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
553 #define streq(string1, string2) (strcmp (string1, string2) == 0)
555 /* Initialization code. */
557 struct processors
559 const char *const name;
560 enum processor_type core;
561 const char *arch;
562 const unsigned long flags;
563 bool (* rtx_costs) (rtx, int, int, int *);
566 /* Not all of these give usefully different compilation alternatives,
567 but there is no simple way of generalizing them. */
568 static const struct processors all_cores[] =
570 /* ARM Cores */
571 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
572 {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
573 #include "arm-cores.def"
574 #undef ARM_CORE
575 {NULL, arm_none, NULL, 0, NULL}
578 static const struct processors all_architectures[] =
580 /* ARM Architectures */
581 /* We don't specify rtx_costs here as it will be figured out
582 from the core. */
584 {"armv2", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
585 {"armv2a", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
586 {"armv3", arm6, "3", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
587 {"armv3m", arm7m, "3M", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
588 {"armv4", arm7tdmi, "4", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
589 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
590 implementations that support it, so we will leave it out for now. */
591 {"armv4t", arm7tdmi, "4T", FL_CO_PROC | FL_FOR_ARCH4T, NULL},
592 {"armv5", arm10tdmi, "5", FL_CO_PROC | FL_FOR_ARCH5, NULL},
593 {"armv5t", arm10tdmi, "5T", FL_CO_PROC | FL_FOR_ARCH5T, NULL},
594 {"armv5e", arm1026ejs, "5E", FL_CO_PROC | FL_FOR_ARCH5E, NULL},
595 {"armv5te", arm1026ejs, "5TE", FL_CO_PROC | FL_FOR_ARCH5TE, NULL},
596 {"armv6", arm1136js, "6", FL_CO_PROC | FL_FOR_ARCH6, NULL},
597 {"armv6j", arm1136js, "6J", FL_CO_PROC | FL_FOR_ARCH6J, NULL},
598 {"armv6k", mpcore, "6K", FL_CO_PROC | FL_FOR_ARCH6K, NULL},
599 {"armv6z", arm1176jzs, "6Z", FL_CO_PROC | FL_FOR_ARCH6Z, NULL},
600 {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC | FL_FOR_ARCH6ZK, NULL},
601 {"ep9312", ep9312, "4T", FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
602 {"iwmmxt", iwmmxt, "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
603 {NULL, arm_none, NULL, 0 , NULL}
606 struct arm_cpu_select
608 const char * string;
609 const char * name;
610 const struct processors * processors;
613 /* This is a magic structure. The 'string' field is magically filled in
614 with a pointer to the value specified by the user on the command line
615 assuming that the user has specified such a value. */
617 static struct arm_cpu_select arm_select[] =
619 /* string name processors */
620 { NULL, "-mcpu=", all_cores },
621 { NULL, "-march=", all_architectures },
622 { NULL, "-mtune=", all_cores }
625 /* Defines representing the indexes into the above table. */
626 #define ARM_OPT_SET_CPU 0
627 #define ARM_OPT_SET_ARCH 1
628 #define ARM_OPT_SET_TUNE 2
630 /* The name of the proprocessor macro to define for this architecture. */
632 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
634 struct fpu_desc
636 const char * name;
637 enum fputype fpu;
641 /* Available values for -mfpu=. */
643 static const struct fpu_desc all_fpus[] =
645 {"fpa", FPUTYPE_FPA},
646 {"fpe2", FPUTYPE_FPA_EMU2},
647 {"fpe3", FPUTYPE_FPA_EMU2},
648 {"maverick", FPUTYPE_MAVERICK},
649 {"vfp", FPUTYPE_VFP}
653 /* Floating point models used by the different hardware.
654 See fputype in arm.h. */
656 static const enum fputype fp_model_for_fpu[] =
658 /* No FP hardware. */
659 ARM_FP_MODEL_UNKNOWN, /* FPUTYPE_NONE */
660 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA */
661 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU2 */
662 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU3 */
663 ARM_FP_MODEL_MAVERICK, /* FPUTYPE_MAVERICK */
664 ARM_FP_MODEL_VFP /* FPUTYPE_VFP */
668 struct float_abi
670 const char * name;
671 enum float_abi_type abi_type;
675 /* Available values for -mfloat-abi=. */
677 static const struct float_abi all_float_abis[] =
679 {"soft", ARM_FLOAT_ABI_SOFT},
680 {"softfp", ARM_FLOAT_ABI_SOFTFP},
681 {"hard", ARM_FLOAT_ABI_HARD}
685 struct abi_name
687 const char *name;
688 enum arm_abi_type abi_type;
692 /* Available values for -mabi=. */
694 static const struct abi_name arm_all_abis[] =
696 {"apcs-gnu", ARM_ABI_APCS},
697 {"atpcs", ARM_ABI_ATPCS},
698 {"aapcs", ARM_ABI_AAPCS},
699 {"iwmmxt", ARM_ABI_IWMMXT},
700 {"aapcs-linux", ARM_ABI_AAPCS_LINUX}
703 /* Supported TLS relocations. */
705 enum tls_reloc {
706 TLS_GD32,
707 TLS_LDM32,
708 TLS_LDO32,
709 TLS_IE32,
710 TLS_LE32
713 /* Emit an insn that's a simple single-set. Both the operands must be known
714 to be valid. */
715 inline static rtx
716 emit_set_insn (rtx x, rtx y)
718 return emit_insn (gen_rtx_SET (VOIDmode, x, y));
721 /* Return the number of bits set in VALUE. */
722 static unsigned
723 bit_count (unsigned long value)
725 unsigned long count = 0;
727 while (value)
729 count++;
730 value &= value - 1; /* Clear the least-significant set bit. */
733 return count;
736 /* Set up library functions unique to ARM. */
738 static void
739 arm_init_libfuncs (void)
741 /* There are no special library functions unless we are using the
742 ARM BPABI. */
743 if (!TARGET_BPABI)
744 return;
746 /* The functions below are described in Section 4 of the "Run-Time
747 ABI for the ARM architecture", Version 1.0. */
749 /* Double-precision floating-point arithmetic. Table 2. */
750 set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
751 set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
752 set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
753 set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
754 set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
756 /* Double-precision comparisons. Table 3. */
757 set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
758 set_optab_libfunc (ne_optab, DFmode, NULL);
759 set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
760 set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
761 set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
762 set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
763 set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
765 /* Single-precision floating-point arithmetic. Table 4. */
766 set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
767 set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
768 set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
769 set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
770 set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
772 /* Single-precision comparisons. Table 5. */
773 set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
774 set_optab_libfunc (ne_optab, SFmode, NULL);
775 set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
776 set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
777 set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
778 set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
779 set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
781 /* Floating-point to integer conversions. Table 6. */
782 set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
783 set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
784 set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
785 set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
786 set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
787 set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
788 set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
789 set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
791 /* Conversions between floating types. Table 7. */
792 set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
793 set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
795 /* Integer to floating-point conversions. Table 8. */
796 set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
797 set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
798 set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
799 set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
800 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
801 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
802 set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
803 set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
805 /* Long long. Table 9. */
806 set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
807 set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
808 set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
809 set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
810 set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
811 set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
812 set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
813 set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
815 /* Integer (32/32->32) division. \S 4.3.1. */
816 set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
817 set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
819 /* The divmod functions are designed so that they can be used for
820 plain division, even though they return both the quotient and the
821 remainder. The quotient is returned in the usual location (i.e.,
822 r0 for SImode, {r0, r1} for DImode), just as would be expected
823 for an ordinary division routine. Because the AAPCS calling
824 conventions specify that all of { r0, r1, r2, r3 } are
825 callee-saved registers, there is no need to tell the compiler
826 explicitly that those registers are clobbered by these
827 routines. */
828 set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
829 set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
831 /* For SImode division the ABI provides div-without-mod routines,
832 which are faster. */
833 set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
834 set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
836 /* We don't have mod libcalls. Fortunately gcc knows how to use the
837 divmod libcalls instead. */
838 set_optab_libfunc (smod_optab, DImode, NULL);
839 set_optab_libfunc (umod_optab, DImode, NULL);
840 set_optab_libfunc (smod_optab, SImode, NULL);
841 set_optab_libfunc (umod_optab, SImode, NULL);
844 /* Implement TARGET_HANDLE_OPTION. */
846 static bool
847 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
849 switch (code)
851 case OPT_march_:
852 arm_select[1].string = arg;
853 return true;
855 case OPT_mcpu_:
856 arm_select[0].string = arg;
857 return true;
859 case OPT_mhard_float:
860 target_float_abi_name = "hard";
861 return true;
863 case OPT_msoft_float:
864 target_float_abi_name = "soft";
865 return true;
867 case OPT_mtune_:
868 arm_select[2].string = arg;
869 return true;
871 default:
872 return true;
876 /* Fix up any incompatible options that the user has specified.
877 This has now turned into a maze. */
878 void
879 arm_override_options (void)
881 unsigned i;
882 enum processor_type target_arch_cpu = arm_none;
884 /* Set up the flags based on the cpu/architecture selected by the user. */
885 for (i = ARRAY_SIZE (arm_select); i--;)
887 struct arm_cpu_select * ptr = arm_select + i;
889 if (ptr->string != NULL && ptr->string[0] != '\0')
891 const struct processors * sel;
893 for (sel = ptr->processors; sel->name != NULL; sel++)
894 if (streq (ptr->string, sel->name))
896 /* Set the architecture define. */
897 if (i != ARM_OPT_SET_TUNE)
898 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
900 /* Determine the processor core for which we should
901 tune code-generation. */
902 if (/* -mcpu= is a sensible default. */
903 i == ARM_OPT_SET_CPU
904 /* -mtune= overrides -mcpu= and -march=. */
905 || i == ARM_OPT_SET_TUNE)
906 arm_tune = (enum processor_type) (sel - ptr->processors);
908 /* Remember the CPU associated with this architecture.
909 If no other option is used to set the CPU type,
910 we'll use this to guess the most suitable tuning
911 options. */
912 if (i == ARM_OPT_SET_ARCH)
913 target_arch_cpu = sel->core;
915 if (i != ARM_OPT_SET_TUNE)
917 /* If we have been given an architecture and a processor
918 make sure that they are compatible. We only generate
919 a warning though, and we prefer the CPU over the
920 architecture. */
921 if (insn_flags != 0 && (insn_flags ^ sel->flags))
922 warning (0, "switch -mcpu=%s conflicts with -march= switch",
923 ptr->string);
925 insn_flags = sel->flags;
928 break;
931 if (sel->name == NULL)
932 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
936 /* Guess the tuning options from the architecture if necessary. */
937 if (arm_tune == arm_none)
938 arm_tune = target_arch_cpu;
940 /* If the user did not specify a processor, choose one for them. */
941 if (insn_flags == 0)
943 const struct processors * sel;
944 unsigned int sought;
945 enum processor_type cpu;
947 cpu = TARGET_CPU_DEFAULT;
948 if (cpu == arm_none)
950 #ifdef SUBTARGET_CPU_DEFAULT
951 /* Use the subtarget default CPU if none was specified by
952 configure. */
953 cpu = SUBTARGET_CPU_DEFAULT;
954 #endif
955 /* Default to ARM6. */
956 if (cpu == arm_none)
957 cpu = arm6;
959 sel = &all_cores[cpu];
961 insn_flags = sel->flags;
963 /* Now check to see if the user has specified some command line
964 switch that require certain abilities from the cpu. */
965 sought = 0;
967 if (TARGET_INTERWORK || TARGET_THUMB)
969 sought |= (FL_THUMB | FL_MODE32);
971 /* There are no ARM processors that support both APCS-26 and
972 interworking. Therefore we force FL_MODE26 to be removed
973 from insn_flags here (if it was set), so that the search
974 below will always be able to find a compatible processor. */
975 insn_flags &= ~FL_MODE26;
978 if (sought != 0 && ((sought & insn_flags) != sought))
980 /* Try to locate a CPU type that supports all of the abilities
981 of the default CPU, plus the extra abilities requested by
982 the user. */
983 for (sel = all_cores; sel->name != NULL; sel++)
984 if ((sel->flags & sought) == (sought | insn_flags))
985 break;
987 if (sel->name == NULL)
989 unsigned current_bit_count = 0;
990 const struct processors * best_fit = NULL;
992 /* Ideally we would like to issue an error message here
993 saying that it was not possible to find a CPU compatible
994 with the default CPU, but which also supports the command
995 line options specified by the programmer, and so they
996 ought to use the -mcpu=<name> command line option to
997 override the default CPU type.
999 If we cannot find a cpu that has both the
1000 characteristics of the default cpu and the given
1001 command line options we scan the array again looking
1002 for a best match. */
1003 for (sel = all_cores; sel->name != NULL; sel++)
1004 if ((sel->flags & sought) == sought)
1006 unsigned count;
1008 count = bit_count (sel->flags & insn_flags);
1010 if (count >= current_bit_count)
1012 best_fit = sel;
1013 current_bit_count = count;
1017 gcc_assert (best_fit);
1018 sel = best_fit;
1021 insn_flags = sel->flags;
1023 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1024 if (arm_tune == arm_none)
1025 arm_tune = (enum processor_type) (sel - all_cores);
1028 /* The processor for which we should tune should now have been
1029 chosen. */
1030 gcc_assert (arm_tune != arm_none);
1032 tune_flags = all_cores[(int)arm_tune].flags;
1033 if (optimize_size)
1034 targetm.rtx_costs = arm_size_rtx_costs;
1035 else
1036 targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
1038 /* Make sure that the processor choice does not conflict with any of the
1039 other command line choices. */
1040 if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
1042 warning (0, "target CPU does not support interworking" );
1043 target_flags &= ~MASK_INTERWORK;
1046 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1048 warning (0, "target CPU does not support THUMB instructions");
1049 target_flags &= ~MASK_THUMB;
1052 if (TARGET_APCS_FRAME && TARGET_THUMB)
1054 /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1055 target_flags &= ~MASK_APCS_FRAME;
1058 /* Callee super interworking implies thumb interworking. Adding
1059 this to the flags here simplifies the logic elsewhere. */
1060 if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1061 target_flags |= MASK_INTERWORK;
1063 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1064 from here where no function is being compiled currently. */
1065 if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1066 warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1068 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1069 warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1071 if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1072 warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1074 if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1076 warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1077 target_flags |= MASK_APCS_FRAME;
1080 if (TARGET_POKE_FUNCTION_NAME)
1081 target_flags |= MASK_APCS_FRAME;
1083 if (TARGET_APCS_REENT && flag_pic)
1084 error ("-fpic and -mapcs-reent are incompatible");
1086 if (TARGET_APCS_REENT)
1087 warning (0, "APCS reentrant code not supported. Ignored");
1089 /* If this target is normally configured to use APCS frames, warn if they
1090 are turned off and debugging is turned on. */
1091 if (TARGET_ARM
1092 && write_symbols != NO_DEBUG
1093 && !TARGET_APCS_FRAME
1094 && (TARGET_DEFAULT & MASK_APCS_FRAME))
1095 warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1097 /* If stack checking is disabled, we can use r10 as the PIC register,
1098 which keeps r9 available. */
1099 if (flag_pic && TARGET_SINGLE_PIC_BASE)
1100 arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
1102 if (TARGET_APCS_FLOAT)
1103 warning (0, "passing floating point arguments in fp regs not yet supported");
1105 /* Initialize boolean versions of the flags, for use in the arm.md file. */
1106 arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1107 arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1108 arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1109 arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1110 arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1111 arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1112 arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1113 arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1114 arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1116 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1117 arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1118 thumb_code = (TARGET_ARM == 0);
1119 arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1120 arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1121 arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1123 /* V5 code we generate is completely interworking capable, so we turn off
1124 TARGET_INTERWORK here to avoid many tests later on. */
1126 /* XXX However, we must pass the right pre-processor defines to CPP
1127 or GLD can get confused. This is a hack. */
1128 if (TARGET_INTERWORK)
1129 arm_cpp_interwork = 1;
1131 if (arm_arch5)
1132 target_flags &= ~MASK_INTERWORK;
1134 if (target_abi_name)
1136 for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1138 if (streq (arm_all_abis[i].name, target_abi_name))
1140 arm_abi = arm_all_abis[i].abi_type;
1141 break;
1144 if (i == ARRAY_SIZE (arm_all_abis))
1145 error ("invalid ABI option: -mabi=%s", target_abi_name);
1147 else
1148 arm_abi = ARM_DEFAULT_ABI;
1150 if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1151 error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1153 if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1154 error ("iwmmxt abi requires an iwmmxt capable cpu");
1156 arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1157 if (target_fpu_name == NULL && target_fpe_name != NULL)
1159 if (streq (target_fpe_name, "2"))
1160 target_fpu_name = "fpe2";
1161 else if (streq (target_fpe_name, "3"))
1162 target_fpu_name = "fpe3";
1163 else
1164 error ("invalid floating point emulation option: -mfpe=%s",
1165 target_fpe_name);
1167 if (target_fpu_name != NULL)
1169 /* The user specified a FPU. */
1170 for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1172 if (streq (all_fpus[i].name, target_fpu_name))
1174 arm_fpu_arch = all_fpus[i].fpu;
1175 arm_fpu_tune = arm_fpu_arch;
1176 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1177 break;
1180 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1181 error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1183 else
1185 #ifdef FPUTYPE_DEFAULT
1186 /* Use the default if it is specified for this platform. */
1187 arm_fpu_arch = FPUTYPE_DEFAULT;
1188 arm_fpu_tune = FPUTYPE_DEFAULT;
1189 #else
1190 /* Pick one based on CPU type. */
1191 /* ??? Some targets assume FPA is the default.
1192 if ((insn_flags & FL_VFP) != 0)
1193 arm_fpu_arch = FPUTYPE_VFP;
1194 else
1196 if (arm_arch_cirrus)
1197 arm_fpu_arch = FPUTYPE_MAVERICK;
1198 else
1199 arm_fpu_arch = FPUTYPE_FPA_EMU2;
1200 #endif
1201 if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1202 arm_fpu_tune = FPUTYPE_FPA;
1203 else
1204 arm_fpu_tune = arm_fpu_arch;
1205 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1206 gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1209 if (target_float_abi_name != NULL)
1211 /* The user specified a FP ABI. */
1212 for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1214 if (streq (all_float_abis[i].name, target_float_abi_name))
1216 arm_float_abi = all_float_abis[i].abi_type;
1217 break;
1220 if (i == ARRAY_SIZE (all_float_abis))
1221 error ("invalid floating point abi: -mfloat-abi=%s",
1222 target_float_abi_name);
1224 else
1225 arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1227 if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1228 sorry ("-mfloat-abi=hard and VFP");
1230 /* If soft-float is specified then don't use FPU. */
1231 if (TARGET_SOFT_FLOAT)
1232 arm_fpu_arch = FPUTYPE_NONE;
1234 /* For arm2/3 there is no need to do any scheduling if there is only
1235 a floating point emulator, or we are doing software floating-point. */
1236 if ((TARGET_SOFT_FLOAT
1237 || arm_fpu_tune == FPUTYPE_FPA_EMU2
1238 || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1239 && (tune_flags & FL_MODE32) == 0)
1240 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1242 if (target_thread_switch)
1244 if (strcmp (target_thread_switch, "soft") == 0)
1245 target_thread_pointer = TP_SOFT;
1246 else if (strcmp (target_thread_switch, "auto") == 0)
1247 target_thread_pointer = TP_AUTO;
1248 else if (strcmp (target_thread_switch, "cp15") == 0)
1249 target_thread_pointer = TP_CP15;
1250 else
1251 error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1254 /* Use the cp15 method if it is available. */
1255 if (target_thread_pointer == TP_AUTO)
1257 if (arm_arch6k && !TARGET_THUMB)
1258 target_thread_pointer = TP_CP15;
1259 else
1260 target_thread_pointer = TP_SOFT;
1263 if (TARGET_HARD_TP && TARGET_THUMB)
1264 error ("can not use -mtp=cp15 with -mthumb");
1266 /* Override the default structure alignment for AAPCS ABI. */
1267 if (TARGET_AAPCS_BASED)
1268 arm_structure_size_boundary = 8;
1270 if (structure_size_string != NULL)
1272 int size = strtol (structure_size_string, NULL, 0);
1274 if (size == 8 || size == 32
1275 || (ARM_DOUBLEWORD_ALIGN && size == 64))
1276 arm_structure_size_boundary = size;
1277 else
1278 warning (0, "structure size boundary can only be set to %s",
1279 ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1282 if (arm_pic_register_string != NULL)
1284 int pic_register = decode_reg_name (arm_pic_register_string);
1286 if (!flag_pic)
1287 warning (0, "-mpic-register= is useless without -fpic");
1289 /* Prevent the user from choosing an obviously stupid PIC register. */
1290 else if (pic_register < 0 || call_used_regs[pic_register]
1291 || pic_register == HARD_FRAME_POINTER_REGNUM
1292 || pic_register == STACK_POINTER_REGNUM
1293 || pic_register >= PC_REGNUM)
1294 error ("unable to use '%s' for PIC register", arm_pic_register_string);
1295 else
1296 arm_pic_register = pic_register;
1299 if (TARGET_THUMB && flag_schedule_insns)
1301 /* Don't warn since it's on by default in -O2. */
1302 flag_schedule_insns = 0;
1305 if (optimize_size)
1307 arm_constant_limit = 1;
1309 /* If optimizing for size, bump the number of instructions that we
1310 are prepared to conditionally execute (even on a StrongARM). */
1311 max_insns_skipped = 6;
1313 else
1315 /* For processors with load scheduling, it never costs more than
1316 2 cycles to load a constant, and the load scheduler may well
1317 reduce that to 1. */
1318 if (arm_ld_sched)
1319 arm_constant_limit = 1;
1321 /* On XScale the longer latency of a load makes it more difficult
1322 to achieve a good schedule, so it's faster to synthesize
1323 constants that can be done in two insns. */
1324 if (arm_tune_xscale)
1325 arm_constant_limit = 2;
1327 /* StrongARM has early execution of branches, so a sequence
1328 that is worth skipping is shorter. */
1329 if (arm_tune_strongarm)
1330 max_insns_skipped = 3;
1333 /* Register global variables with the garbage collector. */
1334 arm_add_gc_roots ();
1337 static void
1338 arm_add_gc_roots (void)
1340 gcc_obstack_init(&minipool_obstack);
1341 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1344 /* A table of known ARM exception types.
1345 For use with the interrupt function attribute. */
1347 typedef struct
1349 const char *const arg;
1350 const unsigned long return_value;
1352 isr_attribute_arg;
1354 static const isr_attribute_arg isr_attribute_args [] =
1356 { "IRQ", ARM_FT_ISR },
1357 { "irq", ARM_FT_ISR },
1358 { "FIQ", ARM_FT_FIQ },
1359 { "fiq", ARM_FT_FIQ },
1360 { "ABORT", ARM_FT_ISR },
1361 { "abort", ARM_FT_ISR },
1362 { "ABORT", ARM_FT_ISR },
1363 { "abort", ARM_FT_ISR },
1364 { "UNDEF", ARM_FT_EXCEPTION },
1365 { "undef", ARM_FT_EXCEPTION },
1366 { "SWI", ARM_FT_EXCEPTION },
1367 { "swi", ARM_FT_EXCEPTION },
1368 { NULL, ARM_FT_NORMAL }
1371 /* Returns the (interrupt) function type of the current
1372 function, or ARM_FT_UNKNOWN if the type cannot be determined. */
1374 static unsigned long
1375 arm_isr_value (tree argument)
1377 const isr_attribute_arg * ptr;
1378 const char * arg;
1380 /* No argument - default to IRQ. */
1381 if (argument == NULL_TREE)
1382 return ARM_FT_ISR;
1384 /* Get the value of the argument. */
1385 if (TREE_VALUE (argument) == NULL_TREE
1386 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1387 return ARM_FT_UNKNOWN;
1389 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1391 /* Check it against the list of known arguments. */
1392 for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1393 if (streq (arg, ptr->arg))
1394 return ptr->return_value;
1396 /* An unrecognized interrupt type. */
1397 return ARM_FT_UNKNOWN;
1400 /* Computes the type of the current function. */
1402 static unsigned long
1403 arm_compute_func_type (void)
1405 unsigned long type = ARM_FT_UNKNOWN;
1406 tree a;
1407 tree attr;
1409 gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1411 /* Decide if the current function is volatile. Such functions
1412 never return, and many memory cycles can be saved by not storing
1413 register values that will never be needed again. This optimization
1414 was added to speed up context switching in a kernel application. */
1415 if (optimize > 0
1416 && (TREE_NOTHROW (current_function_decl)
1417 || !(flag_unwind_tables
1418 || (flag_exceptions && !USING_SJLJ_EXCEPTIONS)))
1419 && TREE_THIS_VOLATILE (current_function_decl))
1420 type |= ARM_FT_VOLATILE;
1422 if (cfun->static_chain_decl != NULL)
1423 type |= ARM_FT_NESTED;
1425 attr = DECL_ATTRIBUTES (current_function_decl);
1427 a = lookup_attribute ("naked", attr);
1428 if (a != NULL_TREE)
1429 type |= ARM_FT_NAKED;
1431 a = lookup_attribute ("isr", attr);
1432 if (a == NULL_TREE)
1433 a = lookup_attribute ("interrupt", attr);
1435 if (a == NULL_TREE)
1436 type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1437 else
1438 type |= arm_isr_value (TREE_VALUE (a));
1440 return type;
1443 /* Returns the type of the current function. */
1445 unsigned long
1446 arm_current_func_type (void)
1448 if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1449 cfun->machine->func_type = arm_compute_func_type ();
1451 return cfun->machine->func_type;
1454 /* Return 1 if it is possible to return using a single instruction.
1455 If SIBLING is non-null, this is a test for a return before a sibling
1456 call. SIBLING is the call insn, so we can examine its register usage. */
1459 use_return_insn (int iscond, rtx sibling)
1461 int regno;
1462 unsigned int func_type;
1463 unsigned long saved_int_regs;
1464 unsigned HOST_WIDE_INT stack_adjust;
1465 arm_stack_offsets *offsets;
1467 /* Never use a return instruction before reload has run. */
1468 if (!reload_completed)
1469 return 0;
1471 func_type = arm_current_func_type ();
1473 /* Naked functions and volatile functions need special
1474 consideration. */
1475 if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1476 return 0;
1478 /* So do interrupt functions that use the frame pointer. */
1479 if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1480 return 0;
1482 offsets = arm_get_frame_offsets ();
1483 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1485 /* As do variadic functions. */
1486 if (current_function_pretend_args_size
1487 || cfun->machine->uses_anonymous_args
1488 /* Or if the function calls __builtin_eh_return () */
1489 || current_function_calls_eh_return
1490 /* Or if the function calls alloca */
1491 || current_function_calls_alloca
1492 /* Or if there is a stack adjustment. However, if the stack pointer
1493 is saved on the stack, we can use a pre-incrementing stack load. */
1494 || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1495 return 0;
1497 saved_int_regs = arm_compute_save_reg_mask ();
1499 /* Unfortunately, the insn
1501 ldmib sp, {..., sp, ...}
1503 triggers a bug on most SA-110 based devices, such that the stack
1504 pointer won't be correctly restored if the instruction takes a
1505 page fault. We work around this problem by popping r3 along with
1506 the other registers, since that is never slower than executing
1507 another instruction.
1509 We test for !arm_arch5 here, because code for any architecture
1510 less than this could potentially be run on one of the buggy
1511 chips. */
1512 if (stack_adjust == 4 && !arm_arch5)
1514 /* Validate that r3 is a call-clobbered register (always true in
1515 the default abi) ... */
1516 if (!call_used_regs[3])
1517 return 0;
1519 /* ... that it isn't being used for a return value ... */
1520 if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
1521 return 0;
1523 /* ... or for a tail-call argument ... */
1524 if (sibling)
1526 gcc_assert (GET_CODE (sibling) == CALL_INSN);
1528 if (find_regno_fusage (sibling, USE, 3))
1529 return 0;
1532 /* ... and that there are no call-saved registers in r0-r2
1533 (always true in the default ABI). */
1534 if (saved_int_regs & 0x7)
1535 return 0;
1538 /* Can't be done if interworking with Thumb, and any registers have been
1539 stacked. */
1540 if (TARGET_INTERWORK && saved_int_regs != 0)
1541 return 0;
1543 /* On StrongARM, conditional returns are expensive if they aren't
1544 taken and multiple registers have been stacked. */
1545 if (iscond && arm_tune_strongarm)
1547 /* Conditional return when just the LR is stored is a simple
1548 conditional-load instruction, that's not expensive. */
1549 if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1550 return 0;
1552 if (flag_pic
1553 && arm_pic_register != INVALID_REGNUM
1554 && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1555 return 0;
1558 /* If there are saved registers but the LR isn't saved, then we need
1559 two instructions for the return. */
1560 if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1561 return 0;
1563 /* Can't be done if any of the FPA regs are pushed,
1564 since this also requires an insn. */
1565 if (TARGET_HARD_FLOAT && TARGET_FPA)
1566 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1567 if (regs_ever_live[regno] && !call_used_regs[regno])
1568 return 0;
1570 /* Likewise VFP regs. */
1571 if (TARGET_HARD_FLOAT && TARGET_VFP)
1572 for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1573 if (regs_ever_live[regno] && !call_used_regs[regno])
1574 return 0;
1576 if (TARGET_REALLY_IWMMXT)
1577 for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1578 if (regs_ever_live[regno] && ! call_used_regs [regno])
1579 return 0;
1581 return 1;
1584 /* Return TRUE if int I is a valid immediate ARM constant. */
1587 const_ok_for_arm (HOST_WIDE_INT i)
1589 int lowbit;
1591 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1592 be all zero, or all one. */
1593 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1594 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1595 != ((~(unsigned HOST_WIDE_INT) 0)
1596 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1597 return FALSE;
1599 i &= (unsigned HOST_WIDE_INT) 0xffffffff;
1601 /* Fast return for 0 and small values. We must do this for zero, since
1602 the code below can't handle that one case. */
1603 if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
1604 return TRUE;
1606 /* Get the number of trailing zeros, rounded down to the nearest even
1607 number. */
1608 lowbit = (ffs ((int) i) - 1) & ~1;
1610 if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
1611 return TRUE;
1612 else if (lowbit <= 4
1613 && ((i & ~0xc000003f) == 0
1614 || (i & ~0xf000000f) == 0
1615 || (i & ~0xfc000003) == 0))
1616 return TRUE;
1618 return FALSE;
1621 /* Return true if I is a valid constant for the operation CODE. */
1622 static int
1623 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1625 if (const_ok_for_arm (i))
1626 return 1;
1628 switch (code)
1630 case PLUS:
1631 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1633 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
1634 case XOR:
1635 case IOR:
1636 return 0;
1638 case AND:
1639 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1641 default:
1642 gcc_unreachable ();
1646 /* Emit a sequence of insns to handle a large constant.
1647 CODE is the code of the operation required, it can be any of SET, PLUS,
1648 IOR, AND, XOR, MINUS;
1649 MODE is the mode in which the operation is being performed;
1650 VAL is the integer to operate on;
1651 SOURCE is the other operand (a register, or a null-pointer for SET);
1652 SUBTARGETS means it is safe to create scratch registers if that will
1653 either produce a simpler sequence, or we will want to cse the values.
1654 Return value is the number of insns emitted. */
1657 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1658 HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1660 rtx cond;
1662 if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1663 cond = COND_EXEC_TEST (PATTERN (insn));
1664 else
1665 cond = NULL_RTX;
1667 if (subtargets || code == SET
1668 || (GET_CODE (target) == REG && GET_CODE (source) == REG
1669 && REGNO (target) != REGNO (source)))
1671 /* After arm_reorg has been called, we can't fix up expensive
1672 constants by pushing them into memory so we must synthesize
1673 them in-line, regardless of the cost. This is only likely to
1674 be more costly on chips that have load delay slots and we are
1675 compiling without running the scheduler (so no splitting
1676 occurred before the final instruction emission).
1678 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1680 if (!after_arm_reorg
1681 && !cond
1682 && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1683 1, 0)
1684 > arm_constant_limit + (code != SET)))
1686 if (code == SET)
1688 /* Currently SET is the only monadic value for CODE, all
1689 the rest are diadic. */
1690 emit_set_insn (target, GEN_INT (val));
1691 return 1;
1693 else
1695 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1697 emit_set_insn (temp, GEN_INT (val));
1698 /* For MINUS, the value is subtracted from, since we never
1699 have subtraction of a constant. */
1700 if (code == MINUS)
1701 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
1702 else
1703 emit_set_insn (target,
1704 gen_rtx_fmt_ee (code, mode, source, temp));
1705 return 2;
1710 return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1714 static int
1715 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1717 HOST_WIDE_INT temp1;
1718 int num_insns = 0;
1721 int end;
1723 if (i <= 0)
1724 i += 32;
1725 if (remainder & (3 << (i - 2)))
1727 end = i - 8;
1728 if (end < 0)
1729 end += 32;
1730 temp1 = remainder & ((0x0ff << end)
1731 | ((i < end) ? (0xff >> (32 - end)) : 0));
1732 remainder &= ~temp1;
1733 num_insns++;
1734 i -= 6;
1736 i -= 2;
1737 } while (remainder);
1738 return num_insns;
1741 /* Emit an instruction with the indicated PATTERN. If COND is
1742 non-NULL, conditionalize the execution of the instruction on COND
1743 being true. */
1745 static void
1746 emit_constant_insn (rtx cond, rtx pattern)
1748 if (cond)
1749 pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1750 emit_insn (pattern);
1753 /* As above, but extra parameter GENERATE which, if clear, suppresses
1754 RTL generation. */
1756 static int
1757 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1758 HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1759 int generate)
1761 int can_invert = 0;
1762 int can_negate = 0;
1763 int can_negate_initial = 0;
1764 int can_shift = 0;
1765 int i;
1766 int num_bits_set = 0;
1767 int set_sign_bit_copies = 0;
1768 int clear_sign_bit_copies = 0;
1769 int clear_zero_bit_copies = 0;
1770 int set_zero_bit_copies = 0;
1771 int insns = 0;
1772 unsigned HOST_WIDE_INT temp1, temp2;
1773 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1775 /* Find out which operations are safe for a given CODE. Also do a quick
1776 check for degenerate cases; these can occur when DImode operations
1777 are split. */
1778 switch (code)
1780 case SET:
1781 can_invert = 1;
1782 can_shift = 1;
1783 can_negate = 1;
1784 break;
1786 case PLUS:
1787 can_negate = 1;
1788 can_negate_initial = 1;
1789 break;
1791 case IOR:
1792 if (remainder == 0xffffffff)
1794 if (generate)
1795 emit_constant_insn (cond,
1796 gen_rtx_SET (VOIDmode, target,
1797 GEN_INT (ARM_SIGN_EXTEND (val))));
1798 return 1;
1800 if (remainder == 0)
1802 if (reload_completed && rtx_equal_p (target, source))
1803 return 0;
1804 if (generate)
1805 emit_constant_insn (cond,
1806 gen_rtx_SET (VOIDmode, target, source));
1807 return 1;
1809 break;
1811 case AND:
1812 if (remainder == 0)
1814 if (generate)
1815 emit_constant_insn (cond,
1816 gen_rtx_SET (VOIDmode, target, const0_rtx));
1817 return 1;
1819 if (remainder == 0xffffffff)
1821 if (reload_completed && rtx_equal_p (target, source))
1822 return 0;
1823 if (generate)
1824 emit_constant_insn (cond,
1825 gen_rtx_SET (VOIDmode, target, source));
1826 return 1;
1828 can_invert = 1;
1829 break;
1831 case XOR:
1832 if (remainder == 0)
1834 if (reload_completed && rtx_equal_p (target, source))
1835 return 0;
1836 if (generate)
1837 emit_constant_insn (cond,
1838 gen_rtx_SET (VOIDmode, target, source));
1839 return 1;
1842 /* We don't know how to handle other cases yet. */
1843 gcc_assert (remainder == 0xffffffff);
1845 if (generate)
1846 emit_constant_insn (cond,
1847 gen_rtx_SET (VOIDmode, target,
1848 gen_rtx_NOT (mode, source)));
1849 return 1;
1851 case MINUS:
1852 /* We treat MINUS as (val - source), since (source - val) is always
1853 passed as (source + (-val)). */
1854 if (remainder == 0)
1856 if (generate)
1857 emit_constant_insn (cond,
1858 gen_rtx_SET (VOIDmode, target,
1859 gen_rtx_NEG (mode, source)));
1860 return 1;
1862 if (const_ok_for_arm (val))
1864 if (generate)
1865 emit_constant_insn (cond,
1866 gen_rtx_SET (VOIDmode, target,
1867 gen_rtx_MINUS (mode, GEN_INT (val),
1868 source)));
1869 return 1;
1871 can_negate = 1;
1873 break;
1875 default:
1876 gcc_unreachable ();
1879 /* If we can do it in one insn get out quickly. */
1880 if (const_ok_for_arm (val)
1881 || (can_negate_initial && const_ok_for_arm (-val))
1882 || (can_invert && const_ok_for_arm (~val)))
1884 if (generate)
1885 emit_constant_insn (cond,
1886 gen_rtx_SET (VOIDmode, target,
1887 (source
1888 ? gen_rtx_fmt_ee (code, mode, source,
1889 GEN_INT (val))
1890 : GEN_INT (val))));
1891 return 1;
1894 /* Calculate a few attributes that may be useful for specific
1895 optimizations. */
1896 for (i = 31; i >= 0; i--)
1898 if ((remainder & (1 << i)) == 0)
1899 clear_sign_bit_copies++;
1900 else
1901 break;
1904 for (i = 31; i >= 0; i--)
1906 if ((remainder & (1 << i)) != 0)
1907 set_sign_bit_copies++;
1908 else
1909 break;
1912 for (i = 0; i <= 31; i++)
1914 if ((remainder & (1 << i)) == 0)
1915 clear_zero_bit_copies++;
1916 else
1917 break;
1920 for (i = 0; i <= 31; i++)
1922 if ((remainder & (1 << i)) != 0)
1923 set_zero_bit_copies++;
1924 else
1925 break;
1928 switch (code)
1930 case SET:
1931 /* See if we can do this by sign_extending a constant that is known
1932 to be negative. This is a good, way of doing it, since the shift
1933 may well merge into a subsequent insn. */
1934 if (set_sign_bit_copies > 1)
1936 if (const_ok_for_arm
1937 (temp1 = ARM_SIGN_EXTEND (remainder
1938 << (set_sign_bit_copies - 1))))
1940 if (generate)
1942 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1943 emit_constant_insn (cond,
1944 gen_rtx_SET (VOIDmode, new_src,
1945 GEN_INT (temp1)));
1946 emit_constant_insn (cond,
1947 gen_ashrsi3 (target, new_src,
1948 GEN_INT (set_sign_bit_copies - 1)));
1950 return 2;
1952 /* For an inverted constant, we will need to set the low bits,
1953 these will be shifted out of harm's way. */
1954 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1955 if (const_ok_for_arm (~temp1))
1957 if (generate)
1959 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1960 emit_constant_insn (cond,
1961 gen_rtx_SET (VOIDmode, new_src,
1962 GEN_INT (temp1)));
1963 emit_constant_insn (cond,
1964 gen_ashrsi3 (target, new_src,
1965 GEN_INT (set_sign_bit_copies - 1)));
1967 return 2;
1971 /* See if we can calculate the value as the difference between two
1972 valid immediates. */
1973 if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
1975 int topshift = clear_sign_bit_copies & ~1;
1977 temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
1978 & (0xff000000 >> topshift));
1980 /* If temp1 is zero, then that means the 9 most significant
1981 bits of remainder were 1 and we've caused it to overflow.
1982 When topshift is 0 we don't need to do anything since we
1983 can borrow from 'bit 32'. */
1984 if (temp1 == 0 && topshift != 0)
1985 temp1 = 0x80000000 >> (topshift - 1);
1987 temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
1989 if (const_ok_for_arm (temp2))
1991 if (generate)
1993 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1994 emit_constant_insn (cond,
1995 gen_rtx_SET (VOIDmode, new_src,
1996 GEN_INT (temp1)));
1997 emit_constant_insn (cond,
1998 gen_addsi3 (target, new_src,
1999 GEN_INT (-temp2)));
2002 return 2;
2006 /* See if we can generate this by setting the bottom (or the top)
2007 16 bits, and then shifting these into the other half of the
2008 word. We only look for the simplest cases, to do more would cost
2009 too much. Be careful, however, not to generate this when the
2010 alternative would take fewer insns. */
2011 if (val & 0xffff0000)
2013 temp1 = remainder & 0xffff0000;
2014 temp2 = remainder & 0x0000ffff;
2016 /* Overlaps outside this range are best done using other methods. */
2017 for (i = 9; i < 24; i++)
2019 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2020 && !const_ok_for_arm (temp2))
2022 rtx new_src = (subtargets
2023 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2024 : target);
2025 insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2026 source, subtargets, generate);
2027 source = new_src;
2028 if (generate)
2029 emit_constant_insn
2030 (cond,
2031 gen_rtx_SET
2032 (VOIDmode, target,
2033 gen_rtx_IOR (mode,
2034 gen_rtx_ASHIFT (mode, source,
2035 GEN_INT (i)),
2036 source)));
2037 return insns + 1;
2041 /* Don't duplicate cases already considered. */
2042 for (i = 17; i < 24; i++)
2044 if (((temp1 | (temp1 >> i)) == remainder)
2045 && !const_ok_for_arm (temp1))
2047 rtx new_src = (subtargets
2048 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2049 : target);
2050 insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2051 source, subtargets, generate);
2052 source = new_src;
2053 if (generate)
2054 emit_constant_insn
2055 (cond,
2056 gen_rtx_SET (VOIDmode, target,
2057 gen_rtx_IOR
2058 (mode,
2059 gen_rtx_LSHIFTRT (mode, source,
2060 GEN_INT (i)),
2061 source)));
2062 return insns + 1;
2066 break;
2068 case IOR:
2069 case XOR:
2070 /* If we have IOR or XOR, and the constant can be loaded in a
2071 single instruction, and we can find a temporary to put it in,
2072 then this can be done in two instructions instead of 3-4. */
2073 if (subtargets
2074 /* TARGET can't be NULL if SUBTARGETS is 0 */
2075 || (reload_completed && !reg_mentioned_p (target, source)))
2077 if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2079 if (generate)
2081 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2083 emit_constant_insn (cond,
2084 gen_rtx_SET (VOIDmode, sub,
2085 GEN_INT (val)));
2086 emit_constant_insn (cond,
2087 gen_rtx_SET (VOIDmode, target,
2088 gen_rtx_fmt_ee (code, mode,
2089 source, sub)));
2091 return 2;
2095 if (code == XOR)
2096 break;
2098 if (set_sign_bit_copies > 8
2099 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2101 if (generate)
2103 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2104 rtx shift = GEN_INT (set_sign_bit_copies);
2106 emit_constant_insn
2107 (cond,
2108 gen_rtx_SET (VOIDmode, sub,
2109 gen_rtx_NOT (mode,
2110 gen_rtx_ASHIFT (mode,
2111 source,
2112 shift))));
2113 emit_constant_insn
2114 (cond,
2115 gen_rtx_SET (VOIDmode, target,
2116 gen_rtx_NOT (mode,
2117 gen_rtx_LSHIFTRT (mode, sub,
2118 shift))));
2120 return 2;
2123 if (set_zero_bit_copies > 8
2124 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2126 if (generate)
2128 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2129 rtx shift = GEN_INT (set_zero_bit_copies);
2131 emit_constant_insn
2132 (cond,
2133 gen_rtx_SET (VOIDmode, sub,
2134 gen_rtx_NOT (mode,
2135 gen_rtx_LSHIFTRT (mode,
2136 source,
2137 shift))));
2138 emit_constant_insn
2139 (cond,
2140 gen_rtx_SET (VOIDmode, target,
2141 gen_rtx_NOT (mode,
2142 gen_rtx_ASHIFT (mode, sub,
2143 shift))));
2145 return 2;
2148 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2150 if (generate)
2152 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2153 emit_constant_insn (cond,
2154 gen_rtx_SET (VOIDmode, sub,
2155 gen_rtx_NOT (mode, source)));
2156 source = sub;
2157 if (subtargets)
2158 sub = gen_reg_rtx (mode);
2159 emit_constant_insn (cond,
2160 gen_rtx_SET (VOIDmode, sub,
2161 gen_rtx_AND (mode, source,
2162 GEN_INT (temp1))));
2163 emit_constant_insn (cond,
2164 gen_rtx_SET (VOIDmode, target,
2165 gen_rtx_NOT (mode, sub)));
2167 return 3;
2169 break;
2171 case AND:
2172 /* See if two shifts will do 2 or more insn's worth of work. */
2173 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2175 HOST_WIDE_INT shift_mask = ((0xffffffff
2176 << (32 - clear_sign_bit_copies))
2177 & 0xffffffff);
2179 if ((remainder | shift_mask) != 0xffffffff)
2181 if (generate)
2183 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2184 insns = arm_gen_constant (AND, mode, cond,
2185 remainder | shift_mask,
2186 new_src, source, subtargets, 1);
2187 source = new_src;
2189 else
2191 rtx targ = subtargets ? NULL_RTX : target;
2192 insns = arm_gen_constant (AND, mode, cond,
2193 remainder | shift_mask,
2194 targ, source, subtargets, 0);
2198 if (generate)
2200 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2201 rtx shift = GEN_INT (clear_sign_bit_copies);
2203 emit_insn (gen_ashlsi3 (new_src, source, shift));
2204 emit_insn (gen_lshrsi3 (target, new_src, shift));
2207 return insns + 2;
2210 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2212 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2214 if ((remainder | shift_mask) != 0xffffffff)
2216 if (generate)
2218 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2220 insns = arm_gen_constant (AND, mode, cond,
2221 remainder | shift_mask,
2222 new_src, source, subtargets, 1);
2223 source = new_src;
2225 else
2227 rtx targ = subtargets ? NULL_RTX : target;
2229 insns = arm_gen_constant (AND, mode, cond,
2230 remainder | shift_mask,
2231 targ, source, subtargets, 0);
2235 if (generate)
2237 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2238 rtx shift = GEN_INT (clear_zero_bit_copies);
2240 emit_insn (gen_lshrsi3 (new_src, source, shift));
2241 emit_insn (gen_ashlsi3 (target, new_src, shift));
2244 return insns + 2;
2247 break;
2249 default:
2250 break;
2253 for (i = 0; i < 32; i++)
2254 if (remainder & (1 << i))
2255 num_bits_set++;
2257 if (code == AND || (can_invert && num_bits_set > 16))
2258 remainder = (~remainder) & 0xffffffff;
2259 else if (code == PLUS && num_bits_set > 16)
2260 remainder = (-remainder) & 0xffffffff;
2261 else
2263 can_invert = 0;
2264 can_negate = 0;
2267 /* Now try and find a way of doing the job in either two or three
2268 instructions.
2269 We start by looking for the largest block of zeros that are aligned on
2270 a 2-bit boundary, we then fill up the temps, wrapping around to the
2271 top of the word when we drop off the bottom.
2272 In the worst case this code should produce no more than four insns. */
2274 int best_start = 0;
2275 int best_consecutive_zeros = 0;
2277 for (i = 0; i < 32; i += 2)
2279 int consecutive_zeros = 0;
2281 if (!(remainder & (3 << i)))
2283 while ((i < 32) && !(remainder & (3 << i)))
2285 consecutive_zeros += 2;
2286 i += 2;
2288 if (consecutive_zeros > best_consecutive_zeros)
2290 best_consecutive_zeros = consecutive_zeros;
2291 best_start = i - consecutive_zeros;
2293 i -= 2;
2297 /* So long as it won't require any more insns to do so, it's
2298 desirable to emit a small constant (in bits 0...9) in the last
2299 insn. This way there is more chance that it can be combined with
2300 a later addressing insn to form a pre-indexed load or store
2301 operation. Consider:
2303 *((volatile int *)0xe0000100) = 1;
2304 *((volatile int *)0xe0000110) = 2;
2306 We want this to wind up as:
2308 mov rA, #0xe0000000
2309 mov rB, #1
2310 str rB, [rA, #0x100]
2311 mov rB, #2
2312 str rB, [rA, #0x110]
2314 rather than having to synthesize both large constants from scratch.
2316 Therefore, we calculate how many insns would be required to emit
2317 the constant starting from `best_start', and also starting from
2318 zero (i.e. with bit 31 first to be output). If `best_start' doesn't
2319 yield a shorter sequence, we may as well use zero. */
2320 if (best_start != 0
2321 && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2322 && (count_insns_for_constant (remainder, 0) <=
2323 count_insns_for_constant (remainder, best_start)))
2324 best_start = 0;
2326 /* Now start emitting the insns. */
2327 i = best_start;
2330 int end;
2332 if (i <= 0)
2333 i += 32;
2334 if (remainder & (3 << (i - 2)))
2336 end = i - 8;
2337 if (end < 0)
2338 end += 32;
2339 temp1 = remainder & ((0x0ff << end)
2340 | ((i < end) ? (0xff >> (32 - end)) : 0));
2341 remainder &= ~temp1;
2343 if (generate)
2345 rtx new_src, temp1_rtx;
2347 if (code == SET || code == MINUS)
2349 new_src = (subtargets ? gen_reg_rtx (mode) : target);
2350 if (can_invert && code != MINUS)
2351 temp1 = ~temp1;
2353 else
2355 if (remainder && subtargets)
2356 new_src = gen_reg_rtx (mode);
2357 else
2358 new_src = target;
2359 if (can_invert)
2360 temp1 = ~temp1;
2361 else if (can_negate)
2362 temp1 = -temp1;
2365 temp1 = trunc_int_for_mode (temp1, mode);
2366 temp1_rtx = GEN_INT (temp1);
2368 if (code == SET)
2370 else if (code == MINUS)
2371 temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2372 else
2373 temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2375 emit_constant_insn (cond,
2376 gen_rtx_SET (VOIDmode, new_src,
2377 temp1_rtx));
2378 source = new_src;
2381 if (code == SET)
2383 can_invert = 0;
2384 code = PLUS;
2386 else if (code == MINUS)
2387 code = PLUS;
2389 insns++;
2390 i -= 6;
2392 i -= 2;
2394 while (remainder);
2397 return insns;
2400 /* Canonicalize a comparison so that we are more likely to recognize it.
2401 This can be done for a few constant compares, where we can make the
2402 immediate value easier to load. */
2404 enum rtx_code
2405 arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
2406 rtx * op1)
2408 unsigned HOST_WIDE_INT i = INTVAL (*op1);
2409 unsigned HOST_WIDE_INT maxval;
2410 maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
2412 switch (code)
2414 case EQ:
2415 case NE:
2416 return code;
2418 case GT:
2419 case LE:
2420 if (i != maxval
2421 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2423 *op1 = GEN_INT (i + 1);
2424 return code == GT ? GE : LT;
2426 break;
2428 case GE:
2429 case LT:
2430 if (i != ~maxval
2431 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2433 *op1 = GEN_INT (i - 1);
2434 return code == GE ? GT : LE;
2436 break;
2438 case GTU:
2439 case LEU:
2440 if (i != ~((unsigned HOST_WIDE_INT) 0)
2441 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2443 *op1 = GEN_INT (i + 1);
2444 return code == GTU ? GEU : LTU;
2446 break;
2448 case GEU:
2449 case LTU:
2450 if (i != 0
2451 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2453 *op1 = GEN_INT (i - 1);
2454 return code == GEU ? GTU : LEU;
2456 break;
2458 default:
2459 gcc_unreachable ();
2462 return code;
2466 /* Define how to find the value returned by a function. */
2469 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2471 enum machine_mode mode;
2472 int unsignedp ATTRIBUTE_UNUSED;
2473 rtx r ATTRIBUTE_UNUSED;
2475 mode = TYPE_MODE (type);
2476 /* Promote integer types. */
2477 if (INTEGRAL_TYPE_P (type))
2478 PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2480 /* Promotes small structs returned in a register to full-word size
2481 for big-endian AAPCS. */
2482 if (arm_return_in_msb (type))
2484 HOST_WIDE_INT size = int_size_in_bytes (type);
2485 if (size % UNITS_PER_WORD != 0)
2487 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2488 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2492 return LIBCALL_VALUE(mode);
2495 /* Determine the amount of memory needed to store the possible return
2496 registers of an untyped call. */
2498 arm_apply_result_size (void)
2500 int size = 16;
2502 if (TARGET_ARM)
2504 if (TARGET_HARD_FLOAT_ABI)
2506 if (TARGET_FPA)
2507 size += 12;
2508 if (TARGET_MAVERICK)
2509 size += 8;
2511 if (TARGET_IWMMXT_ABI)
2512 size += 8;
2515 return size;
2518 /* Decide whether a type should be returned in memory (true)
2519 or in a register (false). This is called by the macro
2520 RETURN_IN_MEMORY. */
2522 arm_return_in_memory (tree type)
2524 HOST_WIDE_INT size;
2526 if (!AGGREGATE_TYPE_P (type) &&
2527 (TREE_CODE (type) != VECTOR_TYPE) &&
2528 !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2529 /* All simple types are returned in registers.
2530 For AAPCS, complex types are treated the same as aggregates. */
2531 return 0;
2533 size = int_size_in_bytes (type);
2535 if (arm_abi != ARM_ABI_APCS)
2537 /* ATPCS and later return aggregate types in memory only if they are
2538 larger than a word (or are variable size). */
2539 return (size < 0 || size > UNITS_PER_WORD);
2542 /* To maximize backwards compatibility with previous versions of gcc,
2543 return vectors up to 4 words in registers. */
2544 if (TREE_CODE (type) == VECTOR_TYPE)
2545 return (size < 0 || size > (4 * UNITS_PER_WORD));
2547 /* For the arm-wince targets we choose to be compatible with Microsoft's
2548 ARM and Thumb compilers, which always return aggregates in memory. */
2549 #ifndef ARM_WINCE
2550 /* All structures/unions bigger than one word are returned in memory.
2551 Also catch the case where int_size_in_bytes returns -1. In this case
2552 the aggregate is either huge or of variable size, and in either case
2553 we will want to return it via memory and not in a register. */
2554 if (size < 0 || size > UNITS_PER_WORD)
2555 return 1;
2557 if (TREE_CODE (type) == RECORD_TYPE)
2559 tree field;
2561 /* For a struct the APCS says that we only return in a register
2562 if the type is 'integer like' and every addressable element
2563 has an offset of zero. For practical purposes this means
2564 that the structure can have at most one non bit-field element
2565 and that this element must be the first one in the structure. */
2567 /* Find the first field, ignoring non FIELD_DECL things which will
2568 have been created by C++. */
2569 for (field = TYPE_FIELDS (type);
2570 field && TREE_CODE (field) != FIELD_DECL;
2571 field = TREE_CHAIN (field))
2572 continue;
2574 if (field == NULL)
2575 return 0; /* An empty structure. Allowed by an extension to ANSI C. */
2577 /* Check that the first field is valid for returning in a register. */
2579 /* ... Floats are not allowed */
2580 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2581 return 1;
2583 /* ... Aggregates that are not themselves valid for returning in
2584 a register are not allowed. */
2585 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2586 return 1;
2588 /* Now check the remaining fields, if any. Only bitfields are allowed,
2589 since they are not addressable. */
2590 for (field = TREE_CHAIN (field);
2591 field;
2592 field = TREE_CHAIN (field))
2594 if (TREE_CODE (field) != FIELD_DECL)
2595 continue;
2597 if (!DECL_BIT_FIELD_TYPE (field))
2598 return 1;
2601 return 0;
2604 if (TREE_CODE (type) == UNION_TYPE)
2606 tree field;
2608 /* Unions can be returned in registers if every element is
2609 integral, or can be returned in an integer register. */
2610 for (field = TYPE_FIELDS (type);
2611 field;
2612 field = TREE_CHAIN (field))
2614 if (TREE_CODE (field) != FIELD_DECL)
2615 continue;
2617 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2618 return 1;
2620 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2621 return 1;
2624 return 0;
2626 #endif /* not ARM_WINCE */
2628 /* Return all other types in memory. */
2629 return 1;
2632 /* Indicate whether or not words of a double are in big-endian order. */
2635 arm_float_words_big_endian (void)
2637 if (TARGET_MAVERICK)
2638 return 0;
2640 /* For FPA, float words are always big-endian. For VFP, floats words
2641 follow the memory system mode. */
2643 if (TARGET_FPA)
2645 return 1;
2648 if (TARGET_VFP)
2649 return (TARGET_BIG_END ? 1 : 0);
2651 return 1;
2654 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2655 for a call to a function whose data type is FNTYPE.
2656 For a library call, FNTYPE is NULL. */
2657 void
2658 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2659 rtx libname ATTRIBUTE_UNUSED,
2660 tree fndecl ATTRIBUTE_UNUSED)
2662 /* On the ARM, the offset starts at 0. */
2663 pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2664 pcum->iwmmxt_nregs = 0;
2665 pcum->can_split = true;
2667 pcum->call_cookie = CALL_NORMAL;
2669 if (TARGET_LONG_CALLS)
2670 pcum->call_cookie = CALL_LONG;
2672 /* Check for long call/short call attributes. The attributes
2673 override any command line option. */
2674 if (fntype)
2676 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2677 pcum->call_cookie = CALL_SHORT;
2678 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2679 pcum->call_cookie = CALL_LONG;
2682 /* Varargs vectors are treated the same as long long.
2683 named_count avoids having to change the way arm handles 'named' */
2684 pcum->named_count = 0;
2685 pcum->nargs = 0;
2687 if (TARGET_REALLY_IWMMXT && fntype)
2689 tree fn_arg;
2691 for (fn_arg = TYPE_ARG_TYPES (fntype);
2692 fn_arg;
2693 fn_arg = TREE_CHAIN (fn_arg))
2694 pcum->named_count += 1;
2696 if (! pcum->named_count)
2697 pcum->named_count = INT_MAX;
2702 /* Return true if mode/type need doubleword alignment. */
2703 bool
2704 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2706 return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2707 || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2711 /* Determine where to put an argument to a function.
2712 Value is zero to push the argument on the stack,
2713 or a hard register in which to store the argument.
2715 MODE is the argument's machine mode.
2716 TYPE is the data type of the argument (as a tree).
2717 This is null for libcalls where that information may
2718 not be available.
2719 CUM is a variable of type CUMULATIVE_ARGS which gives info about
2720 the preceding args and about the function being called.
2721 NAMED is nonzero if this argument is a named parameter
2722 (otherwise it is an extra parameter matching an ellipsis). */
2725 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2726 tree type, int named)
2728 int nregs;
2730 /* Varargs vectors are treated the same as long long.
2731 named_count avoids having to change the way arm handles 'named' */
2732 if (TARGET_IWMMXT_ABI
2733 && arm_vector_mode_supported_p (mode)
2734 && pcum->named_count > pcum->nargs + 1)
2736 if (pcum->iwmmxt_nregs <= 9)
2737 return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2738 else
2740 pcum->can_split = false;
2741 return NULL_RTX;
2745 /* Put doubleword aligned quantities in even register pairs. */
2746 if (pcum->nregs & 1
2747 && ARM_DOUBLEWORD_ALIGN
2748 && arm_needs_doubleword_align (mode, type))
2749 pcum->nregs++;
2751 if (mode == VOIDmode)
2752 /* Compute operand 2 of the call insn. */
2753 return GEN_INT (pcum->call_cookie);
2755 /* Only allow splitting an arg between regs and memory if all preceding
2756 args were allocated to regs. For args passed by reference we only count
2757 the reference pointer. */
2758 if (pcum->can_split)
2759 nregs = 1;
2760 else
2761 nregs = ARM_NUM_REGS2 (mode, type);
2763 if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2764 return NULL_RTX;
2766 return gen_rtx_REG (mode, pcum->nregs);
2769 static int
2770 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2771 tree type, bool named ATTRIBUTE_UNUSED)
2773 int nregs = pcum->nregs;
2775 if (arm_vector_mode_supported_p (mode))
2776 return 0;
2778 if (NUM_ARG_REGS > nregs
2779 && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2780 && pcum->can_split)
2781 return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2783 return 0;
2786 /* Variable sized types are passed by reference. This is a GCC
2787 extension to the ARM ABI. */
2789 static bool
2790 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2791 enum machine_mode mode ATTRIBUTE_UNUSED,
2792 tree type, bool named ATTRIBUTE_UNUSED)
2794 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2797 /* Encode the current state of the #pragma [no_]long_calls. */
2798 typedef enum
2800 OFF, /* No #pramgma [no_]long_calls is in effect. */
2801 LONG, /* #pragma long_calls is in effect. */
2802 SHORT /* #pragma no_long_calls is in effect. */
2803 } arm_pragma_enum;
2805 static arm_pragma_enum arm_pragma_long_calls = OFF;
2807 void
2808 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2810 arm_pragma_long_calls = LONG;
2813 void
2814 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2816 arm_pragma_long_calls = SHORT;
2819 void
2820 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2822 arm_pragma_long_calls = OFF;
2825 /* Table of machine attributes. */
2826 const struct attribute_spec arm_attribute_table[] =
2828 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2829 /* Function calls made to this symbol must be done indirectly, because
2830 it may lie outside of the 26 bit addressing range of a normal function
2831 call. */
2832 { "long_call", 0, 0, false, true, true, NULL },
2833 /* Whereas these functions are always known to reside within the 26 bit
2834 addressing range. */
2835 { "short_call", 0, 0, false, true, true, NULL },
2836 /* Interrupt Service Routines have special prologue and epilogue requirements. */
2837 { "isr", 0, 1, false, false, false, arm_handle_isr_attribute },
2838 { "interrupt", 0, 1, false, false, false, arm_handle_isr_attribute },
2839 { "naked", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2840 #ifdef ARM_PE
2841 /* ARM/PE has three new attributes:
2842 interfacearm - ?
2843 dllexport - for exporting a function/variable that will live in a dll
2844 dllimport - for importing a function/variable from a dll
2846 Microsoft allows multiple declspecs in one __declspec, separating
2847 them with spaces. We do NOT support this. Instead, use __declspec
2848 multiple times.
2850 { "dllimport", 0, 0, true, false, false, NULL },
2851 { "dllexport", 0, 0, true, false, false, NULL },
2852 { "interfacearm", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2853 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2854 { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
2855 { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
2856 { "notshared", 0, 0, false, true, false, arm_handle_notshared_attribute },
2857 #endif
2858 { NULL, 0, 0, false, false, false, NULL }
2861 /* Handle an attribute requiring a FUNCTION_DECL;
2862 arguments as in struct attribute_spec.handler. */
2863 static tree
2864 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2865 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2867 if (TREE_CODE (*node) != FUNCTION_DECL)
2869 warning (OPT_Wattributes, "%qs attribute only applies to functions",
2870 IDENTIFIER_POINTER (name));
2871 *no_add_attrs = true;
2874 return NULL_TREE;
2877 /* Handle an "interrupt" or "isr" attribute;
2878 arguments as in struct attribute_spec.handler. */
2879 static tree
2880 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2881 bool *no_add_attrs)
2883 if (DECL_P (*node))
2885 if (TREE_CODE (*node) != FUNCTION_DECL)
2887 warning (OPT_Wattributes, "%qs attribute only applies to functions",
2888 IDENTIFIER_POINTER (name));
2889 *no_add_attrs = true;
2891 /* FIXME: the argument if any is checked for type attributes;
2892 should it be checked for decl ones? */
2894 else
2896 if (TREE_CODE (*node) == FUNCTION_TYPE
2897 || TREE_CODE (*node) == METHOD_TYPE)
2899 if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2901 warning (OPT_Wattributes, "%qs attribute ignored",
2902 IDENTIFIER_POINTER (name));
2903 *no_add_attrs = true;
2906 else if (TREE_CODE (*node) == POINTER_TYPE
2907 && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2908 || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2909 && arm_isr_value (args) != ARM_FT_UNKNOWN)
2911 *node = build_variant_type_copy (*node);
2912 TREE_TYPE (*node) = build_type_attribute_variant
2913 (TREE_TYPE (*node),
2914 tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2915 *no_add_attrs = true;
2917 else
2919 /* Possibly pass this attribute on from the type to a decl. */
2920 if (flags & ((int) ATTR_FLAG_DECL_NEXT
2921 | (int) ATTR_FLAG_FUNCTION_NEXT
2922 | (int) ATTR_FLAG_ARRAY_NEXT))
2924 *no_add_attrs = true;
2925 return tree_cons (name, args, NULL_TREE);
2927 else
2929 warning (OPT_Wattributes, "%qs attribute ignored",
2930 IDENTIFIER_POINTER (name));
2935 return NULL_TREE;
2938 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2939 /* Handle the "notshared" attribute. This attribute is another way of
2940 requesting hidden visibility. ARM's compiler supports
2941 "__declspec(notshared)"; we support the same thing via an
2942 attribute. */
2944 static tree
2945 arm_handle_notshared_attribute (tree *node,
2946 tree name ATTRIBUTE_UNUSED,
2947 tree args ATTRIBUTE_UNUSED,
2948 int flags ATTRIBUTE_UNUSED,
2949 bool *no_add_attrs)
2951 tree decl = TYPE_NAME (*node);
2953 if (decl)
2955 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2956 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2957 *no_add_attrs = false;
2959 return NULL_TREE;
2961 #endif
2963 /* Return 0 if the attributes for two types are incompatible, 1 if they
2964 are compatible, and 2 if they are nearly compatible (which causes a
2965 warning to be generated). */
2966 static int
2967 arm_comp_type_attributes (tree type1, tree type2)
2969 int l1, l2, s1, s2;
2971 /* Check for mismatch of non-default calling convention. */
2972 if (TREE_CODE (type1) != FUNCTION_TYPE)
2973 return 1;
2975 /* Check for mismatched call attributes. */
2976 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2977 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2978 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2979 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2981 /* Only bother to check if an attribute is defined. */
2982 if (l1 | l2 | s1 | s2)
2984 /* If one type has an attribute, the other must have the same attribute. */
2985 if ((l1 != l2) || (s1 != s2))
2986 return 0;
2988 /* Disallow mixed attributes. */
2989 if ((l1 & s2) || (l2 & s1))
2990 return 0;
2993 /* Check for mismatched ISR attribute. */
2994 l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2995 if (! l1)
2996 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2997 l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2998 if (! l2)
2999 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
3000 if (l1 != l2)
3001 return 0;
3003 return 1;
3006 /* Encode long_call or short_call attribute by prefixing
3007 symbol name in DECL with a special character FLAG. */
3008 void
3009 arm_encode_call_attribute (tree decl, int flag)
3011 const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
3012 int len = strlen (str);
3013 char * newstr;
3015 /* Do not allow weak functions to be treated as short call. */
3016 if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
3017 return;
3019 newstr = alloca (len + 2);
3020 newstr[0] = flag;
3021 strcpy (newstr + 1, str);
3023 newstr = (char *) ggc_alloc_string (newstr, len + 1);
3024 XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
3027 /* Assigns default attributes to newly defined type. This is used to
3028 set short_call/long_call attributes for function types of
3029 functions defined inside corresponding #pragma scopes. */
3030 static void
3031 arm_set_default_type_attributes (tree type)
3033 /* Add __attribute__ ((long_call)) to all functions, when
3034 inside #pragma long_calls or __attribute__ ((short_call)),
3035 when inside #pragma no_long_calls. */
3036 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3038 tree type_attr_list, attr_name;
3039 type_attr_list = TYPE_ATTRIBUTES (type);
3041 if (arm_pragma_long_calls == LONG)
3042 attr_name = get_identifier ("long_call");
3043 else if (arm_pragma_long_calls == SHORT)
3044 attr_name = get_identifier ("short_call");
3045 else
3046 return;
3048 type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
3049 TYPE_ATTRIBUTES (type) = type_attr_list;
3053 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
3054 defined within the current compilation unit. If this cannot be
3055 determined, then 0 is returned. */
3056 static int
3057 current_file_function_operand (rtx sym_ref)
3059 /* This is a bit of a fib. A function will have a short call flag
3060 applied to its name if it has the short call attribute, or it has
3061 already been defined within the current compilation unit. */
3062 if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
3063 return 1;
3065 /* The current function is always defined within the current compilation
3066 unit. If it s a weak definition however, then this may not be the real
3067 definition of the function, and so we have to say no. */
3068 if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
3069 && !DECL_WEAK (current_function_decl))
3070 return 1;
3072 /* We cannot make the determination - default to returning 0. */
3073 return 0;
3076 /* Return nonzero if a 32 bit "long_call" should be generated for
3077 this call. We generate a long_call if the function:
3079 a. has an __attribute__((long call))
3080 or b. is within the scope of a #pragma long_calls
3081 or c. the -mlong-calls command line switch has been specified
3082 . and either:
3083 1. -ffunction-sections is in effect
3084 or 2. the current function has __attribute__ ((section))
3085 or 3. the target function has __attribute__ ((section))
3087 However we do not generate a long call if the function:
3089 d. has an __attribute__ ((short_call))
3090 or e. is inside the scope of a #pragma no_long_calls
3091 or f. is defined within the current compilation unit.
3093 This function will be called by C fragments contained in the machine
3094 description file. SYM_REF and CALL_COOKIE correspond to the matched
3095 rtl operands. CALL_SYMBOL is used to distinguish between
3096 two different callers of the function. It is set to 1 in the
3097 "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
3098 and "call_value" patterns. This is because of the difference in the
3099 SYM_REFs passed by these patterns. */
3101 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
3103 if (!call_symbol)
3105 if (GET_CODE (sym_ref) != MEM)
3106 return 0;
3108 sym_ref = XEXP (sym_ref, 0);
3111 if (GET_CODE (sym_ref) != SYMBOL_REF)
3112 return 0;
3114 if (call_cookie & CALL_SHORT)
3115 return 0;
3117 if (TARGET_LONG_CALLS)
3119 if (flag_function_sections
3120 || DECL_SECTION_NAME (current_function_decl))
3121 /* c.3 is handled by the definition of the
3122 ARM_DECLARE_FUNCTION_SIZE macro. */
3123 return 1;
3126 if (current_file_function_operand (sym_ref))
3127 return 0;
3129 return (call_cookie & CALL_LONG)
3130 || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
3131 || TARGET_LONG_CALLS;
3134 /* Return nonzero if it is ok to make a tail-call to DECL. */
3135 static bool
3136 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3138 int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
3140 if (cfun->machine->sibcall_blocked)
3141 return false;
3143 /* Never tailcall something for which we have no decl, or if we
3144 are in Thumb mode. */
3145 if (decl == NULL || TARGET_THUMB)
3146 return false;
3148 /* Get the calling method. */
3149 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3150 call_type = CALL_SHORT;
3151 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3152 call_type = CALL_LONG;
3154 /* Cannot tail-call to long calls, since these are out of range of
3155 a branch instruction. However, if not compiling PIC, we know
3156 we can reach the symbol if it is in this compilation unit. */
3157 if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
3158 return false;
3160 /* If we are interworking and the function is not declared static
3161 then we can't tail-call it unless we know that it exists in this
3162 compilation unit (since it might be a Thumb routine). */
3163 if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3164 return false;
3166 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
3167 if (IS_INTERRUPT (arm_current_func_type ()))
3168 return false;
3170 /* Everything else is ok. */
3171 return true;
3175 /* Addressing mode support functions. */
3177 /* Return nonzero if X is a legitimate immediate operand when compiling
3178 for PIC. We know that X satisfies CONSTANT_P and flag_pic is true. */
3180 legitimate_pic_operand_p (rtx x)
3182 if (GET_CODE (x) == SYMBOL_REF
3183 || (GET_CODE (x) == CONST
3184 && GET_CODE (XEXP (x, 0)) == PLUS
3185 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
3186 return 0;
3188 return 1;
3192 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3194 if (GET_CODE (orig) == SYMBOL_REF
3195 || GET_CODE (orig) == LABEL_REF)
3197 #ifndef AOF_ASSEMBLER
3198 rtx pic_ref, address;
3199 #endif
3200 rtx insn;
3201 int subregs = 0;
3203 /* If this function doesn't have a pic register, create one now.
3204 A lot of the logic here is made obscure by the fact that this
3205 routine gets called as part of the rtx cost estimation
3206 process. We don't want those calls to affect any assumptions
3207 about the real function; and further, we can't call
3208 entry_of_function() until we start the real expansion
3209 process. */
3210 if (!current_function_uses_pic_offset_table)
3212 gcc_assert (!no_new_pseudos);
3213 if (arm_pic_register != INVALID_REGNUM)
3215 cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
3217 /* Play games to avoid marking the function as needing pic
3218 if we are being called as part of the cost-estimation
3219 process. */
3220 if (!ir_type())
3221 current_function_uses_pic_offset_table = 1;
3223 else
3225 rtx seq;
3227 cfun->machine->pic_reg = gen_reg_rtx (Pmode);
3229 /* Play games to avoid marking the function as needing pic
3230 if we are being called as part of the cost-estimation
3231 process. */
3232 if (!ir_type())
3234 current_function_uses_pic_offset_table = 1;
3235 start_sequence ();
3237 arm_load_pic_register (0UL);
3239 seq = get_insns ();
3240 end_sequence ();
3241 emit_insn_after (seq, entry_of_function ());
3246 if (reg == 0)
3248 gcc_assert (!no_new_pseudos);
3249 reg = gen_reg_rtx (Pmode);
3251 subregs = 1;
3254 #ifdef AOF_ASSEMBLER
3255 /* The AOF assembler can generate relocations for these directly, and
3256 understands that the PIC register has to be added into the offset. */
3257 insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3258 #else
3259 if (subregs)
3260 address = gen_reg_rtx (Pmode);
3261 else
3262 address = reg;
3264 if (TARGET_ARM)
3265 emit_insn (gen_pic_load_addr_arm (address, orig));
3266 else
3267 emit_insn (gen_pic_load_addr_thumb (address, orig));
3269 if ((GET_CODE (orig) == LABEL_REF
3270 || (GET_CODE (orig) == SYMBOL_REF &&
3271 SYMBOL_REF_LOCAL_P (orig)))
3272 && NEED_GOT_RELOC)
3273 pic_ref = gen_rtx_PLUS (Pmode, cfun->machine->pic_reg, address);
3274 else
3276 pic_ref = gen_const_mem (Pmode,
3277 gen_rtx_PLUS (Pmode, cfun->machine->pic_reg,
3278 address));
3281 insn = emit_move_insn (reg, pic_ref);
3282 #endif
3283 /* Put a REG_EQUAL note on this insn, so that it can be optimized
3284 by loop. */
3285 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3286 REG_NOTES (insn));
3287 return reg;
3289 else if (GET_CODE (orig) == CONST)
3291 rtx base, offset;
3293 if (GET_CODE (XEXP (orig, 0)) == PLUS
3294 && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
3295 return orig;
3297 if (GET_CODE (XEXP (orig, 0)) == UNSPEC
3298 && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
3299 return orig;
3301 if (reg == 0)
3303 gcc_assert (!no_new_pseudos);
3304 reg = gen_reg_rtx (Pmode);
3307 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3309 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3310 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3311 base == reg ? 0 : reg);
3313 if (GET_CODE (offset) == CONST_INT)
3315 /* The base register doesn't really matter, we only want to
3316 test the index for the appropriate mode. */
3317 if (!arm_legitimate_index_p (mode, offset, SET, 0))
3319 gcc_assert (!no_new_pseudos);
3320 offset = force_reg (Pmode, offset);
3323 if (GET_CODE (offset) == CONST_INT)
3324 return plus_constant (base, INTVAL (offset));
3327 if (GET_MODE_SIZE (mode) > 4
3328 && (GET_MODE_CLASS (mode) == MODE_INT
3329 || TARGET_SOFT_FLOAT))
3331 emit_insn (gen_addsi3 (reg, base, offset));
3332 return reg;
3335 return gen_rtx_PLUS (Pmode, base, offset);
3338 return orig;
3342 /* Find a spare low register to use during the prolog of a function. */
3344 static int
3345 thumb_find_work_register (unsigned long pushed_regs_mask)
3347 int reg;
3349 /* Check the argument registers first as these are call-used. The
3350 register allocation order means that sometimes r3 might be used
3351 but earlier argument registers might not, so check them all. */
3352 for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3353 if (!regs_ever_live[reg])
3354 return reg;
3356 /* Before going on to check the call-saved registers we can try a couple
3357 more ways of deducing that r3 is available. The first is when we are
3358 pushing anonymous arguments onto the stack and we have less than 4
3359 registers worth of fixed arguments(*). In this case r3 will be part of
3360 the variable argument list and so we can be sure that it will be
3361 pushed right at the start of the function. Hence it will be available
3362 for the rest of the prologue.
3363 (*): ie current_function_pretend_args_size is greater than 0. */
3364 if (cfun->machine->uses_anonymous_args
3365 && current_function_pretend_args_size > 0)
3366 return LAST_ARG_REGNUM;
3368 /* The other case is when we have fixed arguments but less than 4 registers
3369 worth. In this case r3 might be used in the body of the function, but
3370 it is not being used to convey an argument into the function. In theory
3371 we could just check current_function_args_size to see how many bytes are
3372 being passed in argument registers, but it seems that it is unreliable.
3373 Sometimes it will have the value 0 when in fact arguments are being
3374 passed. (See testcase execute/20021111-1.c for an example). So we also
3375 check the args_info.nregs field as well. The problem with this field is
3376 that it makes no allowances for arguments that are passed to the
3377 function but which are not used. Hence we could miss an opportunity
3378 when a function has an unused argument in r3. But it is better to be
3379 safe than to be sorry. */
3380 if (! cfun->machine->uses_anonymous_args
3381 && current_function_args_size >= 0
3382 && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3383 && cfun->args_info.nregs < 4)
3384 return LAST_ARG_REGNUM;
3386 /* Otherwise look for a call-saved register that is going to be pushed. */
3387 for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3388 if (pushed_regs_mask & (1 << reg))
3389 return reg;
3391 /* Something went wrong - thumb_compute_save_reg_mask()
3392 should have arranged for a suitable register to be pushed. */
3393 gcc_unreachable ();
3396 static GTY(()) int pic_labelno;
3398 /* Generate code to load the PIC register. In thumb mode SCRATCH is a
3399 low register. */
3401 void
3402 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
3404 #ifndef AOF_ASSEMBLER
3405 rtx l1, labelno, pic_tmp, pic_tmp2, pic_rtx;
3406 rtx global_offset_table;
3408 if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3409 return;
3411 gcc_assert (flag_pic);
3413 /* We use an UNSPEC rather than a LABEL_REF because this label never appears
3414 in the code stream. */
3416 labelno = GEN_INT (pic_labelno++);
3417 l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3418 l1 = gen_rtx_CONST (VOIDmode, l1);
3420 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3421 /* On the ARM the PC register contains 'dot + 8' at the time of the
3422 addition, on the Thumb it is 'dot + 4'. */
3423 pic_tmp = plus_constant (l1, TARGET_ARM ? 8 : 4);
3424 if (GOT_PCREL)
3425 pic_tmp2 = gen_rtx_CONST (VOIDmode,
3426 gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3427 else
3428 pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3430 pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3432 if (TARGET_ARM)
3434 emit_insn (gen_pic_load_addr_arm (cfun->machine->pic_reg, pic_rtx));
3435 emit_insn (gen_pic_add_dot_plus_eight (cfun->machine->pic_reg,
3436 cfun->machine->pic_reg, labelno));
3438 else
3440 if (arm_pic_register != INVALID_REGNUM
3441 && REGNO (cfun->machine->pic_reg) > LAST_LO_REGNUM)
3443 /* We will have pushed the pic register, so we should always be
3444 able to find a work register. */
3445 pic_tmp = gen_rtx_REG (SImode,
3446 thumb_find_work_register (saved_regs));
3447 emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3448 emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3450 else
3451 emit_insn (gen_pic_load_addr_thumb (cfun->machine->pic_reg, pic_rtx));
3452 emit_insn (gen_pic_add_dot_plus_four (cfun->machine->pic_reg,
3453 cfun->machine->pic_reg, labelno));
3456 /* Need to emit this whether or not we obey regdecls,
3457 since setjmp/longjmp can cause life info to screw up. */
3458 emit_insn (gen_rtx_USE (VOIDmode, cfun->machine->pic_reg));
3459 #endif /* AOF_ASSEMBLER */
3463 /* Return nonzero if X is valid as an ARM state addressing register. */
3464 static int
3465 arm_address_register_rtx_p (rtx x, int strict_p)
3467 int regno;
3469 if (GET_CODE (x) != REG)
3470 return 0;
3472 regno = REGNO (x);
3474 if (strict_p)
3475 return ARM_REGNO_OK_FOR_BASE_P (regno);
3477 return (regno <= LAST_ARM_REGNUM
3478 || regno >= FIRST_PSEUDO_REGISTER
3479 || regno == FRAME_POINTER_REGNUM
3480 || regno == ARG_POINTER_REGNUM);
3483 /* Return TRUE if this rtx is the difference of a symbol and a label,
3484 and will reduce to a PC-relative relocation in the object file.
3485 Expressions like this can be left alone when generating PIC, rather
3486 than forced through the GOT. */
3487 static int
3488 pcrel_constant_p (rtx x)
3490 if (GET_CODE (x) == MINUS)
3491 return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
3493 return FALSE;
3496 /* Return nonzero if X is a valid ARM state address operand. */
3498 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3499 int strict_p)
3501 bool use_ldrd;
3502 enum rtx_code code = GET_CODE (x);
3504 if (arm_address_register_rtx_p (x, strict_p))
3505 return 1;
3507 use_ldrd = (TARGET_LDRD
3508 && (mode == DImode
3509 || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3511 if (code == POST_INC || code == PRE_DEC
3512 || ((code == PRE_INC || code == POST_DEC)
3513 && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3514 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3516 else if ((code == POST_MODIFY || code == PRE_MODIFY)
3517 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3518 && GET_CODE (XEXP (x, 1)) == PLUS
3519 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3521 rtx addend = XEXP (XEXP (x, 1), 1);
3523 /* Don't allow ldrd post increment by register because it's hard
3524 to fixup invalid register choices. */
3525 if (use_ldrd
3526 && GET_CODE (x) == POST_MODIFY
3527 && GET_CODE (addend) == REG)
3528 return 0;
3530 return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3531 && arm_legitimate_index_p (mode, addend, outer, strict_p));
3534 /* After reload constants split into minipools will have addresses
3535 from a LABEL_REF. */
3536 else if (reload_completed
3537 && (code == LABEL_REF
3538 || (code == CONST
3539 && GET_CODE (XEXP (x, 0)) == PLUS
3540 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3541 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3542 return 1;
3544 else if (mode == TImode)
3545 return 0;
3547 else if (code == PLUS)
3549 rtx xop0 = XEXP (x, 0);
3550 rtx xop1 = XEXP (x, 1);
3552 return ((arm_address_register_rtx_p (xop0, strict_p)
3553 && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3554 || (arm_address_register_rtx_p (xop1, strict_p)
3555 && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3558 #if 0
3559 /* Reload currently can't handle MINUS, so disable this for now */
3560 else if (GET_CODE (x) == MINUS)
3562 rtx xop0 = XEXP (x, 0);
3563 rtx xop1 = XEXP (x, 1);
3565 return (arm_address_register_rtx_p (xop0, strict_p)
3566 && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3568 #endif
3570 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3571 && code == SYMBOL_REF
3572 && CONSTANT_POOL_ADDRESS_P (x)
3573 && ! (flag_pic
3574 && symbol_mentioned_p (get_pool_constant (x))
3575 && ! pcrel_constant_p (get_pool_constant (x))))
3576 return 1;
3578 return 0;
3581 /* Return nonzero if INDEX is valid for an address index operand in
3582 ARM state. */
3583 static int
3584 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3585 int strict_p)
3587 HOST_WIDE_INT range;
3588 enum rtx_code code = GET_CODE (index);
3590 /* Standard coprocessor addressing modes. */
3591 if (TARGET_HARD_FLOAT
3592 && (TARGET_FPA || TARGET_MAVERICK)
3593 && (GET_MODE_CLASS (mode) == MODE_FLOAT
3594 || (TARGET_MAVERICK && mode == DImode)))
3595 return (code == CONST_INT && INTVAL (index) < 1024
3596 && INTVAL (index) > -1024
3597 && (INTVAL (index) & 3) == 0);
3599 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3600 return (code == CONST_INT
3601 && INTVAL (index) < 1024
3602 && INTVAL (index) > -1024
3603 && (INTVAL (index) & 3) == 0);
3605 if (arm_address_register_rtx_p (index, strict_p)
3606 && (GET_MODE_SIZE (mode) <= 4))
3607 return 1;
3609 if (mode == DImode || mode == DFmode)
3611 if (code == CONST_INT)
3613 HOST_WIDE_INT val = INTVAL (index);
3615 if (TARGET_LDRD)
3616 return val > -256 && val < 256;
3617 else
3618 return val > -4096 && val < 4092;
3621 return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3624 if (GET_MODE_SIZE (mode) <= 4
3625 && ! (arm_arch4
3626 && (mode == HImode
3627 || (mode == QImode && outer == SIGN_EXTEND))))
3629 if (code == MULT)
3631 rtx xiop0 = XEXP (index, 0);
3632 rtx xiop1 = XEXP (index, 1);
3634 return ((arm_address_register_rtx_p (xiop0, strict_p)
3635 && power_of_two_operand (xiop1, SImode))
3636 || (arm_address_register_rtx_p (xiop1, strict_p)
3637 && power_of_two_operand (xiop0, SImode)));
3639 else if (code == LSHIFTRT || code == ASHIFTRT
3640 || code == ASHIFT || code == ROTATERT)
3642 rtx op = XEXP (index, 1);
3644 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3645 && GET_CODE (op) == CONST_INT
3646 && INTVAL (op) > 0
3647 && INTVAL (op) <= 31);
3651 /* For ARM v4 we may be doing a sign-extend operation during the
3652 load. */
3653 if (arm_arch4)
3655 if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3656 range = 256;
3657 else
3658 range = 4096;
3660 else
3661 range = (mode == HImode) ? 4095 : 4096;
3663 return (code == CONST_INT
3664 && INTVAL (index) < range
3665 && INTVAL (index) > -range);
3668 /* Return nonzero if X is valid as a Thumb state base register. */
3669 static int
3670 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3672 int regno;
3674 if (GET_CODE (x) != REG)
3675 return 0;
3677 regno = REGNO (x);
3679 if (strict_p)
3680 return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3682 return (regno <= LAST_LO_REGNUM
3683 || regno > LAST_VIRTUAL_REGISTER
3684 || regno == FRAME_POINTER_REGNUM
3685 || (GET_MODE_SIZE (mode) >= 4
3686 && (regno == STACK_POINTER_REGNUM
3687 || regno >= FIRST_PSEUDO_REGISTER
3688 || x == hard_frame_pointer_rtx
3689 || x == arg_pointer_rtx)));
3692 /* Return nonzero if x is a legitimate index register. This is the case
3693 for any base register that can access a QImode object. */
3694 inline static int
3695 thumb_index_register_rtx_p (rtx x, int strict_p)
3697 return thumb_base_register_rtx_p (x, QImode, strict_p);
3700 /* Return nonzero if x is a legitimate Thumb-state address.
3702 The AP may be eliminated to either the SP or the FP, so we use the
3703 least common denominator, e.g. SImode, and offsets from 0 to 64.
3705 ??? Verify whether the above is the right approach.
3707 ??? Also, the FP may be eliminated to the SP, so perhaps that
3708 needs special handling also.
3710 ??? Look at how the mips16 port solves this problem. It probably uses
3711 better ways to solve some of these problems.
3713 Although it is not incorrect, we don't accept QImode and HImode
3714 addresses based on the frame pointer or arg pointer until the
3715 reload pass starts. This is so that eliminating such addresses
3716 into stack based ones won't produce impossible code. */
3718 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3720 /* ??? Not clear if this is right. Experiment. */
3721 if (GET_MODE_SIZE (mode) < 4
3722 && !(reload_in_progress || reload_completed)
3723 && (reg_mentioned_p (frame_pointer_rtx, x)
3724 || reg_mentioned_p (arg_pointer_rtx, x)
3725 || reg_mentioned_p (virtual_incoming_args_rtx, x)
3726 || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3727 || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3728 || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3729 return 0;
3731 /* Accept any base register. SP only in SImode or larger. */
3732 else if (thumb_base_register_rtx_p (x, mode, strict_p))
3733 return 1;
3735 /* This is PC relative data before arm_reorg runs. */
3736 else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3737 && GET_CODE (x) == SYMBOL_REF
3738 && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
3739 return 1;
3741 /* This is PC relative data after arm_reorg runs. */
3742 else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3743 && (GET_CODE (x) == LABEL_REF
3744 || (GET_CODE (x) == CONST
3745 && GET_CODE (XEXP (x, 0)) == PLUS
3746 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3747 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3748 return 1;
3750 /* Post-inc indexing only supported for SImode and larger. */
3751 else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3752 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3753 return 1;
3755 else if (GET_CODE (x) == PLUS)
3757 /* REG+REG address can be any two index registers. */
3758 /* We disallow FRAME+REG addressing since we know that FRAME
3759 will be replaced with STACK, and SP relative addressing only
3760 permits SP+OFFSET. */
3761 if (GET_MODE_SIZE (mode) <= 4
3762 && XEXP (x, 0) != frame_pointer_rtx
3763 && XEXP (x, 1) != frame_pointer_rtx
3764 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3765 && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3766 return 1;
3768 /* REG+const has 5-7 bit offset for non-SP registers. */
3769 else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3770 || XEXP (x, 0) == arg_pointer_rtx)
3771 && GET_CODE (XEXP (x, 1)) == CONST_INT
3772 && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3773 return 1;
3775 /* REG+const has 10 bit offset for SP, but only SImode and
3776 larger is supported. */
3777 /* ??? Should probably check for DI/DFmode overflow here
3778 just like GO_IF_LEGITIMATE_OFFSET does. */
3779 else if (GET_CODE (XEXP (x, 0)) == REG
3780 && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3781 && GET_MODE_SIZE (mode) >= 4
3782 && GET_CODE (XEXP (x, 1)) == CONST_INT
3783 && INTVAL (XEXP (x, 1)) >= 0
3784 && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3785 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3786 return 1;
3788 else if (GET_CODE (XEXP (x, 0)) == REG
3789 && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3790 && GET_MODE_SIZE (mode) >= 4
3791 && GET_CODE (XEXP (x, 1)) == CONST_INT
3792 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3793 return 1;
3796 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3797 && GET_MODE_SIZE (mode) == 4
3798 && GET_CODE (x) == SYMBOL_REF
3799 && CONSTANT_POOL_ADDRESS_P (x)
3800 && ! (flag_pic
3801 && symbol_mentioned_p (get_pool_constant (x))
3802 && ! pcrel_constant_p (get_pool_constant (x))))
3803 return 1;
3805 return 0;
3808 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3809 instruction of mode MODE. */
3811 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3813 switch (GET_MODE_SIZE (mode))
3815 case 1:
3816 return val >= 0 && val < 32;
3818 case 2:
3819 return val >= 0 && val < 64 && (val & 1) == 0;
3821 default:
3822 return (val >= 0
3823 && (val + GET_MODE_SIZE (mode)) <= 128
3824 && (val & 3) == 0);
3828 /* Build the SYMBOL_REF for __tls_get_addr. */
3830 static GTY(()) rtx tls_get_addr_libfunc;
3832 static rtx
3833 get_tls_get_addr (void)
3835 if (!tls_get_addr_libfunc)
3836 tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
3837 return tls_get_addr_libfunc;
3840 static rtx
3841 arm_load_tp (rtx target)
3843 if (!target)
3844 target = gen_reg_rtx (SImode);
3846 if (TARGET_HARD_TP)
3848 /* Can return in any reg. */
3849 emit_insn (gen_load_tp_hard (target));
3851 else
3853 /* Always returned in r0. Immediately copy the result into a pseudo,
3854 otherwise other uses of r0 (e.g. setting up function arguments) may
3855 clobber the value. */
3857 rtx tmp;
3859 emit_insn (gen_load_tp_soft ());
3861 tmp = gen_rtx_REG (SImode, 0);
3862 emit_move_insn (target, tmp);
3864 return target;
3867 static rtx
3868 load_tls_operand (rtx x, rtx reg)
3870 rtx tmp;
3872 if (reg == NULL_RTX)
3873 reg = gen_reg_rtx (SImode);
3875 tmp = gen_rtx_CONST (SImode, x);
3877 emit_move_insn (reg, tmp);
3879 return reg;
3882 static rtx
3883 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
3885 rtx insns, label, labelno, sum;
3887 start_sequence ();
3889 labelno = GEN_INT (pic_labelno++);
3890 label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3891 label = gen_rtx_CONST (VOIDmode, label);
3893 sum = gen_rtx_UNSPEC (Pmode,
3894 gen_rtvec (4, x, GEN_INT (reloc), label,
3895 GEN_INT (TARGET_ARM ? 8 : 4)),
3896 UNSPEC_TLS);
3897 reg = load_tls_operand (sum, reg);
3899 if (TARGET_ARM)
3900 emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
3901 else
3902 emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
3904 *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST? */
3905 Pmode, 1, reg, Pmode);
3907 insns = get_insns ();
3908 end_sequence ();
3910 return insns;
3914 legitimize_tls_address (rtx x, rtx reg)
3916 rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
3917 unsigned int model = SYMBOL_REF_TLS_MODEL (x);
3919 switch (model)
3921 case TLS_MODEL_GLOBAL_DYNAMIC:
3922 insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
3923 dest = gen_reg_rtx (Pmode);
3924 emit_libcall_block (insns, dest, ret, x);
3925 return dest;
3927 case TLS_MODEL_LOCAL_DYNAMIC:
3928 insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
3930 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
3931 share the LDM result with other LD model accesses. */
3932 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
3933 UNSPEC_TLS);
3934 dest = gen_reg_rtx (Pmode);
3935 emit_libcall_block (insns, dest, ret, eqv);
3937 /* Load the addend. */
3938 addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
3939 UNSPEC_TLS);
3940 addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
3941 return gen_rtx_PLUS (Pmode, dest, addend);
3943 case TLS_MODEL_INITIAL_EXEC:
3944 labelno = GEN_INT (pic_labelno++);
3945 label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3946 label = gen_rtx_CONST (VOIDmode, label);
3947 sum = gen_rtx_UNSPEC (Pmode,
3948 gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
3949 GEN_INT (TARGET_ARM ? 8 : 4)),
3950 UNSPEC_TLS);
3951 reg = load_tls_operand (sum, reg);
3953 if (TARGET_ARM)
3954 emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
3955 else
3957 emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
3958 emit_move_insn (reg, gen_const_mem (SImode, reg));
3961 tp = arm_load_tp (NULL_RTX);
3963 return gen_rtx_PLUS (Pmode, tp, reg);
3965 case TLS_MODEL_LOCAL_EXEC:
3966 tp = arm_load_tp (NULL_RTX);
3968 reg = gen_rtx_UNSPEC (Pmode,
3969 gen_rtvec (2, x, GEN_INT (TLS_LE32)),
3970 UNSPEC_TLS);
3971 reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
3973 return gen_rtx_PLUS (Pmode, tp, reg);
3975 default:
3976 abort ();
3980 /* Try machine-dependent ways of modifying an illegitimate address
3981 to be legitimate. If we find one, return the new, valid address. */
3983 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3985 if (arm_tls_symbol_p (x))
3986 return legitimize_tls_address (x, NULL_RTX);
3988 if (GET_CODE (x) == PLUS)
3990 rtx xop0 = XEXP (x, 0);
3991 rtx xop1 = XEXP (x, 1);
3993 if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3994 xop0 = force_reg (SImode, xop0);
3996 if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3997 xop1 = force_reg (SImode, xop1);
3999 if (ARM_BASE_REGISTER_RTX_P (xop0)
4000 && GET_CODE (xop1) == CONST_INT)
4002 HOST_WIDE_INT n, low_n;
4003 rtx base_reg, val;
4004 n = INTVAL (xop1);
4006 /* VFP addressing modes actually allow greater offsets, but for
4007 now we just stick with the lowest common denominator. */
4008 if (mode == DImode
4009 || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
4011 low_n = n & 0x0f;
4012 n &= ~0x0f;
4013 if (low_n > 4)
4015 n += 16;
4016 low_n -= 16;
4019 else
4021 low_n = ((mode) == TImode ? 0
4022 : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
4023 n -= low_n;
4026 base_reg = gen_reg_rtx (SImode);
4027 val = force_operand (plus_constant (xop0, n), NULL_RTX);
4028 emit_move_insn (base_reg, val);
4029 x = plus_constant (base_reg, low_n);
4031 else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4032 x = gen_rtx_PLUS (SImode, xop0, xop1);
4035 /* XXX We don't allow MINUS any more -- see comment in
4036 arm_legitimate_address_p (). */
4037 else if (GET_CODE (x) == MINUS)
4039 rtx xop0 = XEXP (x, 0);
4040 rtx xop1 = XEXP (x, 1);
4042 if (CONSTANT_P (xop0))
4043 xop0 = force_reg (SImode, xop0);
4045 if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
4046 xop1 = force_reg (SImode, xop1);
4048 if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4049 x = gen_rtx_MINUS (SImode, xop0, xop1);
4052 /* Make sure to take full advantage of the pre-indexed addressing mode
4053 with absolute addresses which often allows for the base register to
4054 be factorized for multiple adjacent memory references, and it might
4055 even allows for the mini pool to be avoided entirely. */
4056 else if (GET_CODE (x) == CONST_INT && optimize > 0)
4058 unsigned int bits;
4059 HOST_WIDE_INT mask, base, index;
4060 rtx base_reg;
4062 /* ldr and ldrb can use a 12 bit index, ldrsb and the rest can only
4063 use a 8 bit index. So let's use a 12 bit index for SImode only and
4064 hope that arm_gen_constant will enable ldrb to use more bits. */
4065 bits = (mode == SImode) ? 12 : 8;
4066 mask = (1 << bits) - 1;
4067 base = INTVAL (x) & ~mask;
4068 index = INTVAL (x) & mask;
4069 if (bit_count (base & 0xffffffff) > (32 - bits)/2)
4071 /* It'll most probably be more efficient to generate the base
4072 with more bits set and use a negative index instead. */
4073 base |= mask;
4074 index -= mask;
4076 base_reg = force_reg (SImode, GEN_INT (base));
4077 x = plus_constant (base_reg, index);
4080 if (flag_pic)
4082 /* We need to find and carefully transform any SYMBOL and LABEL
4083 references; so go back to the original address expression. */
4084 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4086 if (new_x != orig_x)
4087 x = new_x;
4090 return x;
4094 /* Try machine-dependent ways of modifying an illegitimate Thumb address
4095 to be legitimate. If we find one, return the new, valid address. */
4097 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4099 if (arm_tls_symbol_p (x))
4100 return legitimize_tls_address (x, NULL_RTX);
4102 if (GET_CODE (x) == PLUS
4103 && GET_CODE (XEXP (x, 1)) == CONST_INT
4104 && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
4105 || INTVAL (XEXP (x, 1)) < 0))
4107 rtx xop0 = XEXP (x, 0);
4108 rtx xop1 = XEXP (x, 1);
4109 HOST_WIDE_INT offset = INTVAL (xop1);
4111 /* Try and fold the offset into a biasing of the base register and
4112 then offsetting that. Don't do this when optimizing for space
4113 since it can cause too many CSEs. */
4114 if (optimize_size && offset >= 0
4115 && offset < 256 + 31 * GET_MODE_SIZE (mode))
4117 HOST_WIDE_INT delta;
4119 if (offset >= 256)
4120 delta = offset - (256 - GET_MODE_SIZE (mode));
4121 else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
4122 delta = 31 * GET_MODE_SIZE (mode);
4123 else
4124 delta = offset & (~31 * GET_MODE_SIZE (mode));
4126 xop0 = force_operand (plus_constant (xop0, offset - delta),
4127 NULL_RTX);
4128 x = plus_constant (xop0, delta);
4130 else if (offset < 0 && offset > -256)
4131 /* Small negative offsets are best done with a subtract before the
4132 dereference, forcing these into a register normally takes two
4133 instructions. */
4134 x = force_operand (x, NULL_RTX);
4135 else
4137 /* For the remaining cases, force the constant into a register. */
4138 xop1 = force_reg (SImode, xop1);
4139 x = gen_rtx_PLUS (SImode, xop0, xop1);
4142 else if (GET_CODE (x) == PLUS
4143 && s_register_operand (XEXP (x, 1), SImode)
4144 && !s_register_operand (XEXP (x, 0), SImode))
4146 rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
4148 x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
4151 if (flag_pic)
4153 /* We need to find and carefully transform any SYMBOL and LABEL
4154 references; so go back to the original address expression. */
4155 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4157 if (new_x != orig_x)
4158 x = new_x;
4161 return x;
4165 thumb_legitimize_reload_address (rtx *x_p,
4166 enum machine_mode mode,
4167 int opnum, int type,
4168 int ind_levels ATTRIBUTE_UNUSED)
4170 rtx x = *x_p;
4172 if (GET_CODE (x) == PLUS
4173 && GET_MODE_SIZE (mode) < 4
4174 && REG_P (XEXP (x, 0))
4175 && XEXP (x, 0) == stack_pointer_rtx
4176 && GET_CODE (XEXP (x, 1)) == CONST_INT
4177 && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4179 rtx orig_x = x;
4181 x = copy_rtx (x);
4182 push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4183 Pmode, VOIDmode, 0, 0, opnum, type);
4184 return x;
4187 /* If both registers are hi-regs, then it's better to reload the
4188 entire expression rather than each register individually. That
4189 only requires one reload register rather than two. */
4190 if (GET_CODE (x) == PLUS
4191 && REG_P (XEXP (x, 0))
4192 && REG_P (XEXP (x, 1))
4193 && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
4194 && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
4196 rtx orig_x = x;
4198 x = copy_rtx (x);
4199 push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4200 Pmode, VOIDmode, 0, 0, opnum, type);
4201 return x;
4204 return NULL;
4207 /* Test for various thread-local symbols. */
4209 /* Return TRUE if X is a thread-local symbol. */
4211 static bool
4212 arm_tls_symbol_p (rtx x)
4214 if (! TARGET_HAVE_TLS)
4215 return false;
4217 if (GET_CODE (x) != SYMBOL_REF)
4218 return false;
4220 return SYMBOL_REF_TLS_MODEL (x) != 0;
4223 /* Helper for arm_tls_referenced_p. */
4225 static int
4226 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
4228 if (GET_CODE (*x) == SYMBOL_REF)
4229 return SYMBOL_REF_TLS_MODEL (*x) != 0;
4231 /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
4232 TLS offsets, not real symbol references. */
4233 if (GET_CODE (*x) == UNSPEC
4234 && XINT (*x, 1) == UNSPEC_TLS)
4235 return -1;
4237 return 0;
4240 /* Return TRUE if X contains any TLS symbol references. */
4242 bool
4243 arm_tls_referenced_p (rtx x)
4245 if (! TARGET_HAVE_TLS)
4246 return false;
4248 return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
4251 #define REG_OR_SUBREG_REG(X) \
4252 (GET_CODE (X) == REG \
4253 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
4255 #define REG_OR_SUBREG_RTX(X) \
4256 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
4258 #ifndef COSTS_N_INSNS
4259 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
4260 #endif
4261 static inline int
4262 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
4264 enum machine_mode mode = GET_MODE (x);
4266 switch (code)
4268 case ASHIFT:
4269 case ASHIFTRT:
4270 case LSHIFTRT:
4271 case ROTATERT:
4272 case PLUS:
4273 case MINUS:
4274 case COMPARE:
4275 case NEG:
4276 case NOT:
4277 return COSTS_N_INSNS (1);
4279 case MULT:
4280 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4282 int cycles = 0;
4283 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
4285 while (i)
4287 i >>= 2;
4288 cycles++;
4290 return COSTS_N_INSNS (2) + cycles;
4292 return COSTS_N_INSNS (1) + 16;
4294 case SET:
4295 return (COSTS_N_INSNS (1)
4296 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
4297 + GET_CODE (SET_DEST (x)) == MEM));
4299 case CONST_INT:
4300 if (outer == SET)
4302 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
4303 return 0;
4304 if (thumb_shiftable_const (INTVAL (x)))
4305 return COSTS_N_INSNS (2);
4306 return COSTS_N_INSNS (3);
4308 else if ((outer == PLUS || outer == COMPARE)
4309 && INTVAL (x) < 256 && INTVAL (x) > -256)
4310 return 0;
4311 else if (outer == AND
4312 && INTVAL (x) < 256 && INTVAL (x) >= -256)
4313 return COSTS_N_INSNS (1);
4314 else if (outer == ASHIFT || outer == ASHIFTRT
4315 || outer == LSHIFTRT)
4316 return 0;
4317 return COSTS_N_INSNS (2);
4319 case CONST:
4320 case CONST_DOUBLE:
4321 case LABEL_REF:
4322 case SYMBOL_REF:
4323 return COSTS_N_INSNS (3);
4325 case UDIV:
4326 case UMOD:
4327 case DIV:
4328 case MOD:
4329 return 100;
4331 case TRUNCATE:
4332 return 99;
4334 case AND:
4335 case XOR:
4336 case IOR:
4337 /* XXX guess. */
4338 return 8;
4340 case MEM:
4341 /* XXX another guess. */
4342 /* Memory costs quite a lot for the first word, but subsequent words
4343 load at the equivalent of a single insn each. */
4344 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4345 + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
4346 ? 4 : 0));
4348 case IF_THEN_ELSE:
4349 /* XXX a guess. */
4350 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4351 return 14;
4352 return 2;
4354 case ZERO_EXTEND:
4355 /* XXX still guessing. */
4356 switch (GET_MODE (XEXP (x, 0)))
4358 case QImode:
4359 return (1 + (mode == DImode ? 4 : 0)
4360 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4362 case HImode:
4363 return (4 + (mode == DImode ? 4 : 0)
4364 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4366 case SImode:
4367 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4369 default:
4370 return 99;
4373 default:
4374 return 99;
4379 /* Worker routine for arm_rtx_costs. */
4380 static inline int
4381 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
4383 enum machine_mode mode = GET_MODE (x);
4384 enum rtx_code subcode;
4385 int extra_cost;
4387 switch (code)
4389 case MEM:
4390 /* Memory costs quite a lot for the first word, but subsequent words
4391 load at the equivalent of a single insn each. */
4392 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4393 + (GET_CODE (x) == SYMBOL_REF
4394 && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
4396 case DIV:
4397 case MOD:
4398 case UDIV:
4399 case UMOD:
4400 return optimize_size ? COSTS_N_INSNS (2) : 100;
4402 case ROTATE:
4403 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4404 return 4;
4405 /* Fall through */
4406 case ROTATERT:
4407 if (mode != SImode)
4408 return 8;
4409 /* Fall through */
4410 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4411 if (mode == DImode)
4412 return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
4413 + ((GET_CODE (XEXP (x, 0)) == REG
4414 || (GET_CODE (XEXP (x, 0)) == SUBREG
4415 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4416 ? 0 : 8));
4417 return (1 + ((GET_CODE (XEXP (x, 0)) == REG
4418 || (GET_CODE (XEXP (x, 0)) == SUBREG
4419 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4420 ? 0 : 4)
4421 + ((GET_CODE (XEXP (x, 1)) == REG
4422 || (GET_CODE (XEXP (x, 1)) == SUBREG
4423 && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
4424 || (GET_CODE (XEXP (x, 1)) == CONST_INT))
4425 ? 0 : 4));
4427 case MINUS:
4428 if (mode == DImode)
4429 return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
4430 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4431 || (GET_CODE (XEXP (x, 0)) == CONST_INT
4432 && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
4433 ? 0 : 8));
4435 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4436 return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4437 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4438 && arm_const_double_rtx (XEXP (x, 1))))
4439 ? 0 : 8)
4440 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4441 || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
4442 && arm_const_double_rtx (XEXP (x, 0))))
4443 ? 0 : 8));
4445 if (((GET_CODE (XEXP (x, 0)) == CONST_INT
4446 && const_ok_for_arm (INTVAL (XEXP (x, 0)))
4447 && REG_OR_SUBREG_REG (XEXP (x, 1))))
4448 || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
4449 || subcode == ASHIFTRT || subcode == LSHIFTRT
4450 || subcode == ROTATE || subcode == ROTATERT
4451 || (subcode == MULT
4452 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4453 && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
4454 (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
4455 && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
4456 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
4457 || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
4458 && REG_OR_SUBREG_REG (XEXP (x, 0))))
4459 return 1;
4460 /* Fall through */
4462 case PLUS:
4463 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4464 return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4465 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4466 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4467 && arm_const_double_rtx (XEXP (x, 1))))
4468 ? 0 : 8));
4470 /* Fall through */
4471 case AND: case XOR: case IOR:
4472 extra_cost = 0;
4474 /* Normally the frame registers will be spilt into reg+const during
4475 reload, so it is a bad idea to combine them with other instructions,
4476 since then they might not be moved outside of loops. As a compromise
4477 we allow integration with ops that have a constant as their second
4478 operand. */
4479 if ((REG_OR_SUBREG_REG (XEXP (x, 0))
4480 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
4481 && GET_CODE (XEXP (x, 1)) != CONST_INT)
4482 || (REG_OR_SUBREG_REG (XEXP (x, 0))
4483 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
4484 extra_cost = 4;
4486 if (mode == DImode)
4487 return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4488 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4489 || (GET_CODE (XEXP (x, 1)) == CONST_INT
4490 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4491 ? 0 : 8));
4493 if (REG_OR_SUBREG_REG (XEXP (x, 0)))
4494 return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
4495 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4496 || (GET_CODE (XEXP (x, 1)) == CONST_INT
4497 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4498 ? 0 : 4));
4500 else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
4501 return (1 + extra_cost
4502 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
4503 || subcode == LSHIFTRT || subcode == ASHIFTRT
4504 || subcode == ROTATE || subcode == ROTATERT
4505 || (subcode == MULT
4506 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4507 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
4508 (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
4509 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
4510 && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
4511 || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4512 ? 0 : 4));
4514 return 8;
4516 case MULT:
4517 /* This should have been handled by the CPU specific routines. */
4518 gcc_unreachable ();
4520 case TRUNCATE:
4521 if (arm_arch3m && mode == SImode
4522 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
4523 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4524 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
4525 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
4526 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4527 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
4528 return 8;
4529 return 99;
4531 case NEG:
4532 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4533 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
4534 /* Fall through */
4535 case NOT:
4536 if (mode == DImode)
4537 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4539 return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4541 case IF_THEN_ELSE:
4542 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4543 return 14;
4544 return 2;
4546 case COMPARE:
4547 return 1;
4549 case ABS:
4550 return 4 + (mode == DImode ? 4 : 0);
4552 case SIGN_EXTEND:
4553 if (GET_MODE (XEXP (x, 0)) == QImode)
4554 return (4 + (mode == DImode ? 4 : 0)
4555 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4556 /* Fall through */
4557 case ZERO_EXTEND:
4558 switch (GET_MODE (XEXP (x, 0)))
4560 case QImode:
4561 return (1 + (mode == DImode ? 4 : 0)
4562 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4564 case HImode:
4565 return (4 + (mode == DImode ? 4 : 0)
4566 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4568 case SImode:
4569 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4571 case V8QImode:
4572 case V4HImode:
4573 case V2SImode:
4574 case V4QImode:
4575 case V2HImode:
4576 return 1;
4578 default:
4579 gcc_unreachable ();
4581 gcc_unreachable ();
4583 case CONST_INT:
4584 if (const_ok_for_arm (INTVAL (x)))
4585 return outer == SET ? 2 : -1;
4586 else if (outer == AND
4587 && const_ok_for_arm (~INTVAL (x)))
4588 return -1;
4589 else if ((outer == COMPARE
4590 || outer == PLUS || outer == MINUS)
4591 && const_ok_for_arm (-INTVAL (x)))
4592 return -1;
4593 else
4594 return 5;
4596 case CONST:
4597 case LABEL_REF:
4598 case SYMBOL_REF:
4599 return 6;
4601 case CONST_DOUBLE:
4602 if (arm_const_double_rtx (x))
4603 return outer == SET ? 2 : -1;
4604 else if ((outer == COMPARE || outer == PLUS)
4605 && neg_const_double_rtx_ok_for_fpa (x))
4606 return -1;
4607 return 7;
4609 default:
4610 return 99;
4614 /* RTX costs when optimizing for size. */
4615 static bool
4616 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4618 enum machine_mode mode = GET_MODE (x);
4620 if (TARGET_THUMB)
4622 /* XXX TBD. For now, use the standard costs. */
4623 *total = thumb_rtx_costs (x, code, outer_code);
4624 return true;
4627 switch (code)
4629 case MEM:
4630 /* A memory access costs 1 insn if the mode is small, or the address is
4631 a single register, otherwise it costs one insn per word. */
4632 if (REG_P (XEXP (x, 0)))
4633 *total = COSTS_N_INSNS (1);
4634 else
4635 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4636 return true;
4638 case DIV:
4639 case MOD:
4640 case UDIV:
4641 case UMOD:
4642 /* Needs a libcall, so it costs about this. */
4643 *total = COSTS_N_INSNS (2);
4644 return false;
4646 case ROTATE:
4647 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4649 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4650 return true;
4652 /* Fall through */
4653 case ROTATERT:
4654 case ASHIFT:
4655 case LSHIFTRT:
4656 case ASHIFTRT:
4657 if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4659 *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4660 return true;
4662 else if (mode == SImode)
4664 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4665 /* Slightly disparage register shifts, but not by much. */
4666 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4667 *total += 1 + rtx_cost (XEXP (x, 1), code);
4668 return true;
4671 /* Needs a libcall. */
4672 *total = COSTS_N_INSNS (2);
4673 return false;
4675 case MINUS:
4676 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4678 *total = COSTS_N_INSNS (1);
4679 return false;
4682 if (mode == SImode)
4684 enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4685 enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4687 if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4688 || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4689 || subcode1 == ROTATE || subcode1 == ROTATERT
4690 || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4691 || subcode1 == ASHIFTRT)
4693 /* It's just the cost of the two operands. */
4694 *total = 0;
4695 return false;
4698 *total = COSTS_N_INSNS (1);
4699 return false;
4702 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4703 return false;
4705 case PLUS:
4706 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4708 *total = COSTS_N_INSNS (1);
4709 return false;
4712 /* Fall through */
4713 case AND: case XOR: case IOR:
4714 if (mode == SImode)
4716 enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4718 if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4719 || subcode == LSHIFTRT || subcode == ASHIFTRT
4720 || (code == AND && subcode == NOT))
4722 /* It's just the cost of the two operands. */
4723 *total = 0;
4724 return false;
4728 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4729 return false;
4731 case MULT:
4732 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4733 return false;
4735 case NEG:
4736 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4737 *total = COSTS_N_INSNS (1);
4738 /* Fall through */
4739 case NOT:
4740 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4742 return false;
4744 case IF_THEN_ELSE:
4745 *total = 0;
4746 return false;
4748 case COMPARE:
4749 if (cc_register (XEXP (x, 0), VOIDmode))
4750 * total = 0;
4751 else
4752 *total = COSTS_N_INSNS (1);
4753 return false;
4755 case ABS:
4756 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4757 *total = COSTS_N_INSNS (1);
4758 else
4759 *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4760 return false;
4762 case SIGN_EXTEND:
4763 *total = 0;
4764 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4766 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4767 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4769 if (mode == DImode)
4770 *total += COSTS_N_INSNS (1);
4771 return false;
4773 case ZERO_EXTEND:
4774 *total = 0;
4775 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4777 switch (GET_MODE (XEXP (x, 0)))
4779 case QImode:
4780 *total += COSTS_N_INSNS (1);
4781 break;
4783 case HImode:
4784 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4786 case SImode:
4787 break;
4789 default:
4790 *total += COSTS_N_INSNS (2);
4794 if (mode == DImode)
4795 *total += COSTS_N_INSNS (1);
4797 return false;
4799 case CONST_INT:
4800 if (const_ok_for_arm (INTVAL (x)))
4801 *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4802 else if (const_ok_for_arm (~INTVAL (x)))
4803 *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4804 else if (const_ok_for_arm (-INTVAL (x)))
4806 if (outer_code == COMPARE || outer_code == PLUS
4807 || outer_code == MINUS)
4808 *total = 0;
4809 else
4810 *total = COSTS_N_INSNS (1);
4812 else
4813 *total = COSTS_N_INSNS (2);
4814 return true;
4816 case CONST:
4817 case LABEL_REF:
4818 case SYMBOL_REF:
4819 *total = COSTS_N_INSNS (2);
4820 return true;
4822 case CONST_DOUBLE:
4823 *total = COSTS_N_INSNS (4);
4824 return true;
4826 default:
4827 if (mode != VOIDmode)
4828 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4829 else
4830 *total = COSTS_N_INSNS (4); /* How knows? */
4831 return false;
4835 /* RTX costs for cores with a slow MUL implementation. */
4837 static bool
4838 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4840 enum machine_mode mode = GET_MODE (x);
4842 if (TARGET_THUMB)
4844 *total = thumb_rtx_costs (x, code, outer_code);
4845 return true;
4848 switch (code)
4850 case MULT:
4851 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4852 || mode == DImode)
4854 *total = 30;
4855 return true;
4858 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4860 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4861 & (unsigned HOST_WIDE_INT) 0xffffffff);
4862 int cost, const_ok = const_ok_for_arm (i);
4863 int j, booth_unit_size;
4865 /* Tune as appropriate. */
4866 cost = const_ok ? 4 : 8;
4867 booth_unit_size = 2;
4868 for (j = 0; i && j < 32; j += booth_unit_size)
4870 i >>= booth_unit_size;
4871 cost += 2;
4874 *total = cost;
4875 return true;
4878 *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4879 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4880 return true;
4882 default:
4883 *total = arm_rtx_costs_1 (x, code, outer_code);
4884 return true;
4889 /* RTX cost for cores with a fast multiply unit (M variants). */
4891 static bool
4892 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4894 enum machine_mode mode = GET_MODE (x);
4896 if (TARGET_THUMB)
4898 *total = thumb_rtx_costs (x, code, outer_code);
4899 return true;
4902 switch (code)
4904 case MULT:
4905 /* There is no point basing this on the tuning, since it is always the
4906 fast variant if it exists at all. */
4907 if (mode == DImode
4908 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4909 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4910 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4912 *total = 8;
4913 return true;
4917 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4918 || mode == DImode)
4920 *total = 30;
4921 return true;
4924 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4926 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4927 & (unsigned HOST_WIDE_INT) 0xffffffff);
4928 int cost, const_ok = const_ok_for_arm (i);
4929 int j, booth_unit_size;
4931 /* Tune as appropriate. */
4932 cost = const_ok ? 4 : 8;
4933 booth_unit_size = 8;
4934 for (j = 0; i && j < 32; j += booth_unit_size)
4936 i >>= booth_unit_size;
4937 cost += 2;
4940 *total = cost;
4941 return true;
4944 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4945 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4946 return true;
4948 default:
4949 *total = arm_rtx_costs_1 (x, code, outer_code);
4950 return true;
4955 /* RTX cost for XScale CPUs. */
4957 static bool
4958 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4960 enum machine_mode mode = GET_MODE (x);
4962 if (TARGET_THUMB)
4964 *total = thumb_rtx_costs (x, code, outer_code);
4965 return true;
4968 switch (code)
4970 case MULT:
4971 /* There is no point basing this on the tuning, since it is always the
4972 fast variant if it exists at all. */
4973 if (mode == DImode
4974 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4975 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4976 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4978 *total = 8;
4979 return true;
4983 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4984 || mode == DImode)
4986 *total = 30;
4987 return true;
4990 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4992 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4993 & (unsigned HOST_WIDE_INT) 0xffffffff);
4994 int cost, const_ok = const_ok_for_arm (i);
4995 unsigned HOST_WIDE_INT masked_const;
4997 /* The cost will be related to two insns.
4998 First a load of the constant (MOV or LDR), then a multiply. */
4999 cost = 2;
5000 if (! const_ok)
5001 cost += 1; /* LDR is probably more expensive because
5002 of longer result latency. */
5003 masked_const = i & 0xffff8000;
5004 if (masked_const != 0 && masked_const != 0xffff8000)
5006 masked_const = i & 0xf8000000;
5007 if (masked_const == 0 || masked_const == 0xf8000000)
5008 cost += 1;
5009 else
5010 cost += 2;
5012 *total = cost;
5013 return true;
5016 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5017 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5018 return true;
5020 case COMPARE:
5021 /* A COMPARE of a MULT is slow on XScale; the muls instruction
5022 will stall until the multiplication is complete. */
5023 if (GET_CODE (XEXP (x, 0)) == MULT)
5024 *total = 4 + rtx_cost (XEXP (x, 0), code);
5025 else
5026 *total = arm_rtx_costs_1 (x, code, outer_code);
5027 return true;
5029 default:
5030 *total = arm_rtx_costs_1 (x, code, outer_code);
5031 return true;
5036 /* RTX costs for 9e (and later) cores. */
5038 static bool
5039 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
5041 enum machine_mode mode = GET_MODE (x);
5042 int nonreg_cost;
5043 int cost;
5045 if (TARGET_THUMB)
5047 switch (code)
5049 case MULT:
5050 *total = COSTS_N_INSNS (3);
5051 return true;
5053 default:
5054 *total = thumb_rtx_costs (x, code, outer_code);
5055 return true;
5059 switch (code)
5061 case MULT:
5062 /* There is no point basing this on the tuning, since it is always the
5063 fast variant if it exists at all. */
5064 if (mode == DImode
5065 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5066 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5067 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5069 *total = 3;
5070 return true;
5074 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5076 *total = 30;
5077 return true;
5079 if (mode == DImode)
5081 cost = 7;
5082 nonreg_cost = 8;
5084 else
5086 cost = 2;
5087 nonreg_cost = 4;
5091 *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
5092 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
5093 return true;
5095 default:
5096 *total = arm_rtx_costs_1 (x, code, outer_code);
5097 return true;
5100 /* All address computations that can be done are free, but rtx cost returns
5101 the same for practically all of them. So we weight the different types
5102 of address here in the order (most pref first):
5103 PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL. */
5104 static inline int
5105 arm_arm_address_cost (rtx x)
5107 enum rtx_code c = GET_CODE (x);
5109 if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
5110 return 0;
5111 if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
5112 return 10;
5114 if (c == PLUS || c == MINUS)
5116 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5117 return 2;
5119 if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
5120 return 3;
5122 return 4;
5125 return 6;
5128 static inline int
5129 arm_thumb_address_cost (rtx x)
5131 enum rtx_code c = GET_CODE (x);
5133 if (c == REG)
5134 return 1;
5135 if (c == PLUS
5136 && GET_CODE (XEXP (x, 0)) == REG
5137 && GET_CODE (XEXP (x, 1)) == CONST_INT)
5138 return 1;
5140 return 2;
5143 static int
5144 arm_address_cost (rtx x)
5146 return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
5149 static int
5150 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
5152 rtx i_pat, d_pat;
5154 /* Some true dependencies can have a higher cost depending
5155 on precisely how certain input operands are used. */
5156 if (arm_tune_xscale
5157 && REG_NOTE_KIND (link) == 0
5158 && recog_memoized (insn) >= 0
5159 && recog_memoized (dep) >= 0)
5161 int shift_opnum = get_attr_shift (insn);
5162 enum attr_type attr_type = get_attr_type (dep);
5164 /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
5165 operand for INSN. If we have a shifted input operand and the
5166 instruction we depend on is another ALU instruction, then we may
5167 have to account for an additional stall. */
5168 if (shift_opnum != 0
5169 && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
5171 rtx shifted_operand;
5172 int opno;
5174 /* Get the shifted operand. */
5175 extract_insn (insn);
5176 shifted_operand = recog_data.operand[shift_opnum];
5178 /* Iterate over all the operands in DEP. If we write an operand
5179 that overlaps with SHIFTED_OPERAND, then we have increase the
5180 cost of this dependency. */
5181 extract_insn (dep);
5182 preprocess_constraints ();
5183 for (opno = 0; opno < recog_data.n_operands; opno++)
5185 /* We can ignore strict inputs. */
5186 if (recog_data.operand_type[opno] == OP_IN)
5187 continue;
5189 if (reg_overlap_mentioned_p (recog_data.operand[opno],
5190 shifted_operand))
5191 return 2;
5196 /* XXX This is not strictly true for the FPA. */
5197 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
5198 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
5199 return 0;
5201 /* Call insns don't incur a stall, even if they follow a load. */
5202 if (REG_NOTE_KIND (link) == 0
5203 && GET_CODE (insn) == CALL_INSN)
5204 return 1;
5206 if ((i_pat = single_set (insn)) != NULL
5207 && GET_CODE (SET_SRC (i_pat)) == MEM
5208 && (d_pat = single_set (dep)) != NULL
5209 && GET_CODE (SET_DEST (d_pat)) == MEM)
5211 rtx src_mem = XEXP (SET_SRC (i_pat), 0);
5212 /* This is a load after a store, there is no conflict if the load reads
5213 from a cached area. Assume that loads from the stack, and from the
5214 constant pool are cached, and that others will miss. This is a
5215 hack. */
5217 if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
5218 || reg_mentioned_p (stack_pointer_rtx, src_mem)
5219 || reg_mentioned_p (frame_pointer_rtx, src_mem)
5220 || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
5221 return 1;
5224 return cost;
5227 static int fp_consts_inited = 0;
5229 /* Only zero is valid for VFP. Other values are also valid for FPA. */
5230 static const char * const strings_fp[8] =
5232 "0", "1", "2", "3",
5233 "4", "5", "0.5", "10"
5236 static REAL_VALUE_TYPE values_fp[8];
5238 static void
5239 init_fp_table (void)
5241 int i;
5242 REAL_VALUE_TYPE r;
5244 if (TARGET_VFP)
5245 fp_consts_inited = 1;
5246 else
5247 fp_consts_inited = 8;
5249 for (i = 0; i < fp_consts_inited; i++)
5251 r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
5252 values_fp[i] = r;
5256 /* Return TRUE if rtx X is a valid immediate FP constant. */
5258 arm_const_double_rtx (rtx x)
5260 REAL_VALUE_TYPE r;
5261 int i;
5263 if (!fp_consts_inited)
5264 init_fp_table ();
5266 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5267 if (REAL_VALUE_MINUS_ZERO (r))
5268 return 0;
5270 for (i = 0; i < fp_consts_inited; i++)
5271 if (REAL_VALUES_EQUAL (r, values_fp[i]))
5272 return 1;
5274 return 0;
5277 /* Return TRUE if rtx X is a valid immediate FPA constant. */
5279 neg_const_double_rtx_ok_for_fpa (rtx x)
5281 REAL_VALUE_TYPE r;
5282 int i;
5284 if (!fp_consts_inited)
5285 init_fp_table ();
5287 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5288 r = REAL_VALUE_NEGATE (r);
5289 if (REAL_VALUE_MINUS_ZERO (r))
5290 return 0;
5292 for (i = 0; i < 8; i++)
5293 if (REAL_VALUES_EQUAL (r, values_fp[i]))
5294 return 1;
5296 return 0;
5299 /* Predicates for `match_operand' and `match_operator'. */
5301 /* Return nonzero if OP is a valid Cirrus memory address pattern. */
5303 cirrus_memory_offset (rtx op)
5305 /* Reject eliminable registers. */
5306 if (! (reload_in_progress || reload_completed)
5307 && ( reg_mentioned_p (frame_pointer_rtx, op)
5308 || reg_mentioned_p (arg_pointer_rtx, op)
5309 || reg_mentioned_p (virtual_incoming_args_rtx, op)
5310 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5311 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5312 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5313 return 0;
5315 if (GET_CODE (op) == MEM)
5317 rtx ind;
5319 ind = XEXP (op, 0);
5321 /* Match: (mem (reg)). */
5322 if (GET_CODE (ind) == REG)
5323 return 1;
5325 /* Match:
5326 (mem (plus (reg)
5327 (const))). */
5328 if (GET_CODE (ind) == PLUS
5329 && GET_CODE (XEXP (ind, 0)) == REG
5330 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5331 && GET_CODE (XEXP (ind, 1)) == CONST_INT)
5332 return 1;
5335 return 0;
5338 /* Return TRUE if OP is a valid coprocessor memory address pattern.
5339 WB if true if writeback address modes are allowed. */
5342 arm_coproc_mem_operand (rtx op, bool wb)
5344 rtx ind;
5346 /* Reject eliminable registers. */
5347 if (! (reload_in_progress || reload_completed)
5348 && ( reg_mentioned_p (frame_pointer_rtx, op)
5349 || reg_mentioned_p (arg_pointer_rtx, op)
5350 || reg_mentioned_p (virtual_incoming_args_rtx, op)
5351 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5352 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5353 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5354 return FALSE;
5356 /* Constants are converted into offsets from labels. */
5357 if (GET_CODE (op) != MEM)
5358 return FALSE;
5360 ind = XEXP (op, 0);
5362 if (reload_completed
5363 && (GET_CODE (ind) == LABEL_REF
5364 || (GET_CODE (ind) == CONST
5365 && GET_CODE (XEXP (ind, 0)) == PLUS
5366 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
5367 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
5368 return TRUE;
5370 /* Match: (mem (reg)). */
5371 if (GET_CODE (ind) == REG)
5372 return arm_address_register_rtx_p (ind, 0);
5374 /* Autoincremment addressing modes. */
5375 if (wb
5376 && (GET_CODE (ind) == PRE_INC
5377 || GET_CODE (ind) == POST_INC
5378 || GET_CODE (ind) == PRE_DEC
5379 || GET_CODE (ind) == POST_DEC))
5380 return arm_address_register_rtx_p (XEXP (ind, 0), 0);
5382 if (wb
5383 && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
5384 && arm_address_register_rtx_p (XEXP (ind, 0), 0)
5385 && GET_CODE (XEXP (ind, 1)) == PLUS
5386 && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
5387 ind = XEXP (ind, 1);
5389 /* Match:
5390 (plus (reg)
5391 (const)). */
5392 if (GET_CODE (ind) == PLUS
5393 && GET_CODE (XEXP (ind, 0)) == REG
5394 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5395 && GET_CODE (XEXP (ind, 1)) == CONST_INT
5396 && INTVAL (XEXP (ind, 1)) > -1024
5397 && INTVAL (XEXP (ind, 1)) < 1024
5398 && (INTVAL (XEXP (ind, 1)) & 3) == 0)
5399 return TRUE;
5401 return FALSE;
5404 /* Return true if X is a register that will be eliminated later on. */
5406 arm_eliminable_register (rtx x)
5408 return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
5409 || REGNO (x) == ARG_POINTER_REGNUM
5410 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
5411 && REGNO (x) <= LAST_VIRTUAL_REGISTER));
5414 /* Return GENERAL_REGS if a scratch register required to reload x to/from
5415 VFP registers. Otherwise return NO_REGS. */
5417 enum reg_class
5418 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
5420 if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
5421 return NO_REGS;
5423 return GENERAL_REGS;
5426 /* Values which must be returned in the most-significant end of the return
5427 register. */
5429 static bool
5430 arm_return_in_msb (tree valtype)
5432 return (TARGET_AAPCS_BASED
5433 && BYTES_BIG_ENDIAN
5434 && (AGGREGATE_TYPE_P (valtype)
5435 || TREE_CODE (valtype) == COMPLEX_TYPE));
5438 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
5439 Use by the Cirrus Maverick code which has to workaround
5440 a hardware bug triggered by such instructions. */
5441 static bool
5442 arm_memory_load_p (rtx insn)
5444 rtx body, lhs, rhs;;
5446 if (insn == NULL_RTX || GET_CODE (insn) != INSN)
5447 return false;
5449 body = PATTERN (insn);
5451 if (GET_CODE (body) != SET)
5452 return false;
5454 lhs = XEXP (body, 0);
5455 rhs = XEXP (body, 1);
5457 lhs = REG_OR_SUBREG_RTX (lhs);
5459 /* If the destination is not a general purpose
5460 register we do not have to worry. */
5461 if (GET_CODE (lhs) != REG
5462 || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
5463 return false;
5465 /* As well as loads from memory we also have to react
5466 to loads of invalid constants which will be turned
5467 into loads from the minipool. */
5468 return (GET_CODE (rhs) == MEM
5469 || GET_CODE (rhs) == SYMBOL_REF
5470 || note_invalid_constants (insn, -1, false));
5473 /* Return TRUE if INSN is a Cirrus instruction. */
5474 static bool
5475 arm_cirrus_insn_p (rtx insn)
5477 enum attr_cirrus attr;
5479 /* get_attr cannot accept USE or CLOBBER. */
5480 if (!insn
5481 || GET_CODE (insn) != INSN
5482 || GET_CODE (PATTERN (insn)) == USE
5483 || GET_CODE (PATTERN (insn)) == CLOBBER)
5484 return 0;
5486 attr = get_attr_cirrus (insn);
5488 return attr != CIRRUS_NOT;
5491 /* Cirrus reorg for invalid instruction combinations. */
5492 static void
5493 cirrus_reorg (rtx first)
5495 enum attr_cirrus attr;
5496 rtx body = PATTERN (first);
5497 rtx t;
5498 int nops;
5500 /* Any branch must be followed by 2 non Cirrus instructions. */
5501 if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
5503 nops = 0;
5504 t = next_nonnote_insn (first);
5506 if (arm_cirrus_insn_p (t))
5507 ++ nops;
5509 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5510 ++ nops;
5512 while (nops --)
5513 emit_insn_after (gen_nop (), first);
5515 return;
5518 /* (float (blah)) is in parallel with a clobber. */
5519 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
5520 body = XVECEXP (body, 0, 0);
5522 if (GET_CODE (body) == SET)
5524 rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
5526 /* cfldrd, cfldr64, cfstrd, cfstr64 must
5527 be followed by a non Cirrus insn. */
5528 if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
5530 if (arm_cirrus_insn_p (next_nonnote_insn (first)))
5531 emit_insn_after (gen_nop (), first);
5533 return;
5535 else if (arm_memory_load_p (first))
5537 unsigned int arm_regno;
5539 /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
5540 ldr/cfmv64hr combination where the Rd field is the same
5541 in both instructions must be split with a non Cirrus
5542 insn. Example:
5544 ldr r0, blah
5546 cfmvsr mvf0, r0. */
5548 /* Get Arm register number for ldr insn. */
5549 if (GET_CODE (lhs) == REG)
5550 arm_regno = REGNO (lhs);
5551 else
5553 gcc_assert (GET_CODE (rhs) == REG);
5554 arm_regno = REGNO (rhs);
5557 /* Next insn. */
5558 first = next_nonnote_insn (first);
5560 if (! arm_cirrus_insn_p (first))
5561 return;
5563 body = PATTERN (first);
5565 /* (float (blah)) is in parallel with a clobber. */
5566 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
5567 body = XVECEXP (body, 0, 0);
5569 if (GET_CODE (body) == FLOAT)
5570 body = XEXP (body, 0);
5572 if (get_attr_cirrus (first) == CIRRUS_MOVE
5573 && GET_CODE (XEXP (body, 1)) == REG
5574 && arm_regno == REGNO (XEXP (body, 1)))
5575 emit_insn_after (gen_nop (), first);
5577 return;
5581 /* get_attr cannot accept USE or CLOBBER. */
5582 if (!first
5583 || GET_CODE (first) != INSN
5584 || GET_CODE (PATTERN (first)) == USE
5585 || GET_CODE (PATTERN (first)) == CLOBBER)
5586 return;
5588 attr = get_attr_cirrus (first);
5590 /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
5591 must be followed by a non-coprocessor instruction. */
5592 if (attr == CIRRUS_COMPARE)
5594 nops = 0;
5596 t = next_nonnote_insn (first);
5598 if (arm_cirrus_insn_p (t))
5599 ++ nops;
5601 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5602 ++ nops;
5604 while (nops --)
5605 emit_insn_after (gen_nop (), first);
5607 return;
5611 /* Return TRUE if X references a SYMBOL_REF. */
5613 symbol_mentioned_p (rtx x)
5615 const char * fmt;
5616 int i;
5618 if (GET_CODE (x) == SYMBOL_REF)
5619 return 1;
5621 /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
5622 are constant offsets, not symbols. */
5623 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
5624 return 0;
5626 fmt = GET_RTX_FORMAT (GET_CODE (x));
5628 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5630 if (fmt[i] == 'E')
5632 int j;
5634 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5635 if (symbol_mentioned_p (XVECEXP (x, i, j)))
5636 return 1;
5638 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5639 return 1;
5642 return 0;
5645 /* Return TRUE if X references a LABEL_REF. */
5647 label_mentioned_p (rtx x)
5649 const char * fmt;
5650 int i;
5652 if (GET_CODE (x) == LABEL_REF)
5653 return 1;
5655 /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
5656 instruction, but they are constant offsets, not symbols. */
5657 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
5658 return 0;
5660 fmt = GET_RTX_FORMAT (GET_CODE (x));
5661 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5663 if (fmt[i] == 'E')
5665 int j;
5667 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5668 if (label_mentioned_p (XVECEXP (x, i, j)))
5669 return 1;
5671 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5672 return 1;
5675 return 0;
5679 tls_mentioned_p (rtx x)
5681 switch (GET_CODE (x))
5683 case CONST:
5684 return tls_mentioned_p (XEXP (x, 0));
5686 case UNSPEC:
5687 if (XINT (x, 1) == UNSPEC_TLS)
5688 return 1;
5690 default:
5691 return 0;
5695 /* Must not copy a SET whose source operand is PC-relative. */
5697 static bool
5698 arm_cannot_copy_insn_p (rtx insn)
5700 rtx pat = PATTERN (insn);
5702 if (GET_CODE (pat) == PARALLEL
5703 && GET_CODE (XVECEXP (pat, 0, 0)) == SET)
5705 rtx rhs = SET_SRC (XVECEXP (pat, 0, 0));
5707 if (GET_CODE (rhs) == UNSPEC
5708 && XINT (rhs, 1) == UNSPEC_PIC_BASE)
5709 return TRUE;
5711 if (GET_CODE (rhs) == MEM
5712 && GET_CODE (XEXP (rhs, 0)) == UNSPEC
5713 && XINT (XEXP (rhs, 0), 1) == UNSPEC_PIC_BASE)
5714 return TRUE;
5717 return FALSE;
5720 enum rtx_code
5721 minmax_code (rtx x)
5723 enum rtx_code code = GET_CODE (x);
5725 switch (code)
5727 case SMAX:
5728 return GE;
5729 case SMIN:
5730 return LE;
5731 case UMIN:
5732 return LEU;
5733 case UMAX:
5734 return GEU;
5735 default:
5736 gcc_unreachable ();
5740 /* Return 1 if memory locations are adjacent. */
5742 adjacent_mem_locations (rtx a, rtx b)
5744 /* We don't guarantee to preserve the order of these memory refs. */
5745 if (volatile_refs_p (a) || volatile_refs_p (b))
5746 return 0;
5748 if ((GET_CODE (XEXP (a, 0)) == REG
5749 || (GET_CODE (XEXP (a, 0)) == PLUS
5750 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5751 && (GET_CODE (XEXP (b, 0)) == REG
5752 || (GET_CODE (XEXP (b, 0)) == PLUS
5753 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5755 HOST_WIDE_INT val0 = 0, val1 = 0;
5756 rtx reg0, reg1;
5757 int val_diff;
5759 if (GET_CODE (XEXP (a, 0)) == PLUS)
5761 reg0 = XEXP (XEXP (a, 0), 0);
5762 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5764 else
5765 reg0 = XEXP (a, 0);
5767 if (GET_CODE (XEXP (b, 0)) == PLUS)
5769 reg1 = XEXP (XEXP (b, 0), 0);
5770 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5772 else
5773 reg1 = XEXP (b, 0);
5775 /* Don't accept any offset that will require multiple
5776 instructions to handle, since this would cause the
5777 arith_adjacentmem pattern to output an overlong sequence. */
5778 if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5779 return 0;
5781 /* Don't allow an eliminable register: register elimination can make
5782 the offset too large. */
5783 if (arm_eliminable_register (reg0))
5784 return 0;
5786 val_diff = val1 - val0;
5788 if (arm_ld_sched)
5790 /* If the target has load delay slots, then there's no benefit
5791 to using an ldm instruction unless the offset is zero and
5792 we are optimizing for size. */
5793 return (optimize_size && (REGNO (reg0) == REGNO (reg1))
5794 && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
5795 && (val_diff == 4 || val_diff == -4));
5798 return ((REGNO (reg0) == REGNO (reg1))
5799 && (val_diff == 4 || val_diff == -4));
5802 return 0;
5806 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5807 HOST_WIDE_INT *load_offset)
5809 int unsorted_regs[4];
5810 HOST_WIDE_INT unsorted_offsets[4];
5811 int order[4];
5812 int base_reg = -1;
5813 int i;
5815 /* Can only handle 2, 3, or 4 insns at present,
5816 though could be easily extended if required. */
5817 gcc_assert (nops >= 2 && nops <= 4);
5819 /* Loop over the operands and check that the memory references are
5820 suitable (i.e. immediate offsets from the same base register). At
5821 the same time, extract the target register, and the memory
5822 offsets. */
5823 for (i = 0; i < nops; i++)
5825 rtx reg;
5826 rtx offset;
5828 /* Convert a subreg of a mem into the mem itself. */
5829 if (GET_CODE (operands[nops + i]) == SUBREG)
5830 operands[nops + i] = alter_subreg (operands + (nops + i));
5832 gcc_assert (GET_CODE (operands[nops + i]) == MEM);
5834 /* Don't reorder volatile memory references; it doesn't seem worth
5835 looking for the case where the order is ok anyway. */
5836 if (MEM_VOLATILE_P (operands[nops + i]))
5837 return 0;
5839 offset = const0_rtx;
5841 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5842 || (GET_CODE (reg) == SUBREG
5843 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5844 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5845 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5846 == REG)
5847 || (GET_CODE (reg) == SUBREG
5848 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5849 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5850 == CONST_INT)))
5852 if (i == 0)
5854 base_reg = REGNO (reg);
5855 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5856 ? REGNO (operands[i])
5857 : REGNO (SUBREG_REG (operands[i])));
5858 order[0] = 0;
5860 else
5862 if (base_reg != (int) REGNO (reg))
5863 /* Not addressed from the same base register. */
5864 return 0;
5866 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5867 ? REGNO (operands[i])
5868 : REGNO (SUBREG_REG (operands[i])));
5869 if (unsorted_regs[i] < unsorted_regs[order[0]])
5870 order[0] = i;
5873 /* If it isn't an integer register, or if it overwrites the
5874 base register but isn't the last insn in the list, then
5875 we can't do this. */
5876 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5877 || (i != nops - 1 && unsorted_regs[i] == base_reg))
5878 return 0;
5880 unsorted_offsets[i] = INTVAL (offset);
5882 else
5883 /* Not a suitable memory address. */
5884 return 0;
5887 /* All the useful information has now been extracted from the
5888 operands into unsorted_regs and unsorted_offsets; additionally,
5889 order[0] has been set to the lowest numbered register in the
5890 list. Sort the registers into order, and check that the memory
5891 offsets are ascending and adjacent. */
5893 for (i = 1; i < nops; i++)
5895 int j;
5897 order[i] = order[i - 1];
5898 for (j = 0; j < nops; j++)
5899 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5900 && (order[i] == order[i - 1]
5901 || unsorted_regs[j] < unsorted_regs[order[i]]))
5902 order[i] = j;
5904 /* Have we found a suitable register? if not, one must be used more
5905 than once. */
5906 if (order[i] == order[i - 1])
5907 return 0;
5909 /* Is the memory address adjacent and ascending? */
5910 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5911 return 0;
5914 if (base)
5916 *base = base_reg;
5918 for (i = 0; i < nops; i++)
5919 regs[i] = unsorted_regs[order[i]];
5921 *load_offset = unsorted_offsets[order[0]];
5924 if (unsorted_offsets[order[0]] == 0)
5925 return 1; /* ldmia */
5927 if (unsorted_offsets[order[0]] == 4)
5928 return 2; /* ldmib */
5930 if (unsorted_offsets[order[nops - 1]] == 0)
5931 return 3; /* ldmda */
5933 if (unsorted_offsets[order[nops - 1]] == -4)
5934 return 4; /* ldmdb */
5936 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5937 if the offset isn't small enough. The reason 2 ldrs are faster
5938 is because these ARMs are able to do more than one cache access
5939 in a single cycle. The ARM9 and StrongARM have Harvard caches,
5940 whilst the ARM8 has a double bandwidth cache. This means that
5941 these cores can do both an instruction fetch and a data fetch in
5942 a single cycle, so the trick of calculating the address into a
5943 scratch register (one of the result regs) and then doing a load
5944 multiple actually becomes slower (and no smaller in code size).
5945 That is the transformation
5947 ldr rd1, [rbase + offset]
5948 ldr rd2, [rbase + offset + 4]
5952 add rd1, rbase, offset
5953 ldmia rd1, {rd1, rd2}
5955 produces worse code -- '3 cycles + any stalls on rd2' instead of
5956 '2 cycles + any stalls on rd2'. On ARMs with only one cache
5957 access per cycle, the first sequence could never complete in less
5958 than 6 cycles, whereas the ldm sequence would only take 5 and
5959 would make better use of sequential accesses if not hitting the
5960 cache.
5962 We cheat here and test 'arm_ld_sched' which we currently know to
5963 only be true for the ARM8, ARM9 and StrongARM. If this ever
5964 changes, then the test below needs to be reworked. */
5965 if (nops == 2 && arm_ld_sched)
5966 return 0;
5968 /* Can't do it without setting up the offset, only do this if it takes
5969 no more than one insn. */
5970 return (const_ok_for_arm (unsorted_offsets[order[0]])
5971 || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5974 const char *
5975 emit_ldm_seq (rtx *operands, int nops)
5977 int regs[4];
5978 int base_reg;
5979 HOST_WIDE_INT offset;
5980 char buf[100];
5981 int i;
5983 switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5985 case 1:
5986 strcpy (buf, "ldm%?ia\t");
5987 break;
5989 case 2:
5990 strcpy (buf, "ldm%?ib\t");
5991 break;
5993 case 3:
5994 strcpy (buf, "ldm%?da\t");
5995 break;
5997 case 4:
5998 strcpy (buf, "ldm%?db\t");
5999 break;
6001 case 5:
6002 if (offset >= 0)
6003 sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
6004 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
6005 (long) offset);
6006 else
6007 sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
6008 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
6009 (long) -offset);
6010 output_asm_insn (buf, operands);
6011 base_reg = regs[0];
6012 strcpy (buf, "ldm%?ia\t");
6013 break;
6015 default:
6016 gcc_unreachable ();
6019 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
6020 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
6022 for (i = 1; i < nops; i++)
6023 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
6024 reg_names[regs[i]]);
6026 strcat (buf, "}\t%@ phole ldm");
6028 output_asm_insn (buf, operands);
6029 return "";
6033 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
6034 HOST_WIDE_INT * load_offset)
6036 int unsorted_regs[4];
6037 HOST_WIDE_INT unsorted_offsets[4];
6038 int order[4];
6039 int base_reg = -1;
6040 int i;
6042 /* Can only handle 2, 3, or 4 insns at present, though could be easily
6043 extended if required. */
6044 gcc_assert (nops >= 2 && nops <= 4);
6046 /* Loop over the operands and check that the memory references are
6047 suitable (i.e. immediate offsets from the same base register). At
6048 the same time, extract the target register, and the memory
6049 offsets. */
6050 for (i = 0; i < nops; i++)
6052 rtx reg;
6053 rtx offset;
6055 /* Convert a subreg of a mem into the mem itself. */
6056 if (GET_CODE (operands[nops + i]) == SUBREG)
6057 operands[nops + i] = alter_subreg (operands + (nops + i));
6059 gcc_assert (GET_CODE (operands[nops + i]) == MEM);
6061 /* Don't reorder volatile memory references; it doesn't seem worth
6062 looking for the case where the order is ok anyway. */
6063 if (MEM_VOLATILE_P (operands[nops + i]))
6064 return 0;
6066 offset = const0_rtx;
6068 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
6069 || (GET_CODE (reg) == SUBREG
6070 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6071 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
6072 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
6073 == REG)
6074 || (GET_CODE (reg) == SUBREG
6075 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6076 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
6077 == CONST_INT)))
6079 if (i == 0)
6081 base_reg = REGNO (reg);
6082 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
6083 ? REGNO (operands[i])
6084 : REGNO (SUBREG_REG (operands[i])));
6085 order[0] = 0;
6087 else
6089 if (base_reg != (int) REGNO (reg))
6090 /* Not addressed from the same base register. */
6091 return 0;
6093 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
6094 ? REGNO (operands[i])
6095 : REGNO (SUBREG_REG (operands[i])));
6096 if (unsorted_regs[i] < unsorted_regs[order[0]])
6097 order[0] = i;
6100 /* If it isn't an integer register, then we can't do this. */
6101 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
6102 return 0;
6104 unsorted_offsets[i] = INTVAL (offset);
6106 else
6107 /* Not a suitable memory address. */
6108 return 0;
6111 /* All the useful information has now been extracted from the
6112 operands into unsorted_regs and unsorted_offsets; additionally,
6113 order[0] has been set to the lowest numbered register in the
6114 list. Sort the registers into order, and check that the memory
6115 offsets are ascending and adjacent. */
6117 for (i = 1; i < nops; i++)
6119 int j;
6121 order[i] = order[i - 1];
6122 for (j = 0; j < nops; j++)
6123 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
6124 && (order[i] == order[i - 1]
6125 || unsorted_regs[j] < unsorted_regs[order[i]]))
6126 order[i] = j;
6128 /* Have we found a suitable register? if not, one must be used more
6129 than once. */
6130 if (order[i] == order[i - 1])
6131 return 0;
6133 /* Is the memory address adjacent and ascending? */
6134 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
6135 return 0;
6138 if (base)
6140 *base = base_reg;
6142 for (i = 0; i < nops; i++)
6143 regs[i] = unsorted_regs[order[i]];
6145 *load_offset = unsorted_offsets[order[0]];
6148 if (unsorted_offsets[order[0]] == 0)
6149 return 1; /* stmia */
6151 if (unsorted_offsets[order[0]] == 4)
6152 return 2; /* stmib */
6154 if (unsorted_offsets[order[nops - 1]] == 0)
6155 return 3; /* stmda */
6157 if (unsorted_offsets[order[nops - 1]] == -4)
6158 return 4; /* stmdb */
6160 return 0;
6163 const char *
6164 emit_stm_seq (rtx *operands, int nops)
6166 int regs[4];
6167 int base_reg;
6168 HOST_WIDE_INT offset;
6169 char buf[100];
6170 int i;
6172 switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
6174 case 1:
6175 strcpy (buf, "stm%?ia\t");
6176 break;
6178 case 2:
6179 strcpy (buf, "stm%?ib\t");
6180 break;
6182 case 3:
6183 strcpy (buf, "stm%?da\t");
6184 break;
6186 case 4:
6187 strcpy (buf, "stm%?db\t");
6188 break;
6190 default:
6191 gcc_unreachable ();
6194 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
6195 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
6197 for (i = 1; i < nops; i++)
6198 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
6199 reg_names[regs[i]]);
6201 strcat (buf, "}\t%@ phole stm");
6203 output_asm_insn (buf, operands);
6204 return "";
6207 /* Routines for use in generating RTL. */
6210 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
6211 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6213 HOST_WIDE_INT offset = *offsetp;
6214 int i = 0, j;
6215 rtx result;
6216 int sign = up ? 1 : -1;
6217 rtx mem, addr;
6219 /* XScale has load-store double instructions, but they have stricter
6220 alignment requirements than load-store multiple, so we cannot
6221 use them.
6223 For XScale ldm requires 2 + NREGS cycles to complete and blocks
6224 the pipeline until completion.
6226 NREGS CYCLES
6232 An ldr instruction takes 1-3 cycles, but does not block the
6233 pipeline.
6235 NREGS CYCLES
6236 1 1-3
6237 2 2-6
6238 3 3-9
6239 4 4-12
6241 Best case ldr will always win. However, the more ldr instructions
6242 we issue, the less likely we are to be able to schedule them well.
6243 Using ldr instructions also increases code size.
6245 As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
6246 for counts of 3 or 4 regs. */
6247 if (arm_tune_xscale && count <= 2 && ! optimize_size)
6249 rtx seq;
6251 start_sequence ();
6253 for (i = 0; i < count; i++)
6255 addr = plus_constant (from, i * 4 * sign);
6256 mem = adjust_automodify_address (basemem, SImode, addr, offset);
6257 emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
6258 offset += 4 * sign;
6261 if (write_back)
6263 emit_move_insn (from, plus_constant (from, count * 4 * sign));
6264 *offsetp = offset;
6267 seq = get_insns ();
6268 end_sequence ();
6270 return seq;
6273 result = gen_rtx_PARALLEL (VOIDmode,
6274 rtvec_alloc (count + (write_back ? 1 : 0)));
6275 if (write_back)
6277 XVECEXP (result, 0, 0)
6278 = gen_rtx_SET (VOIDmode, from, plus_constant (from, count * 4 * sign));
6279 i = 1;
6280 count++;
6283 for (j = 0; i < count; i++, j++)
6285 addr = plus_constant (from, j * 4 * sign);
6286 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6287 XVECEXP (result, 0, i)
6288 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
6289 offset += 4 * sign;
6292 if (write_back)
6293 *offsetp = offset;
6295 return result;
6299 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
6300 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6302 HOST_WIDE_INT offset = *offsetp;
6303 int i = 0, j;
6304 rtx result;
6305 int sign = up ? 1 : -1;
6306 rtx mem, addr;
6308 /* See arm_gen_load_multiple for discussion of
6309 the pros/cons of ldm/stm usage for XScale. */
6310 if (arm_tune_xscale && count <= 2 && ! optimize_size)
6312 rtx seq;
6314 start_sequence ();
6316 for (i = 0; i < count; i++)
6318 addr = plus_constant (to, i * 4 * sign);
6319 mem = adjust_automodify_address (basemem, SImode, addr, offset);
6320 emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
6321 offset += 4 * sign;
6324 if (write_back)
6326 emit_move_insn (to, plus_constant (to, count * 4 * sign));
6327 *offsetp = offset;
6330 seq = get_insns ();
6331 end_sequence ();
6333 return seq;
6336 result = gen_rtx_PARALLEL (VOIDmode,
6337 rtvec_alloc (count + (write_back ? 1 : 0)));
6338 if (write_back)
6340 XVECEXP (result, 0, 0)
6341 = gen_rtx_SET (VOIDmode, to,
6342 plus_constant (to, count * 4 * sign));
6343 i = 1;
6344 count++;
6347 for (j = 0; i < count; i++, j++)
6349 addr = plus_constant (to, j * 4 * sign);
6350 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6351 XVECEXP (result, 0, i)
6352 = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
6353 offset += 4 * sign;
6356 if (write_back)
6357 *offsetp = offset;
6359 return result;
6363 arm_gen_movmemqi (rtx *operands)
6365 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
6366 HOST_WIDE_INT srcoffset, dstoffset;
6367 int i;
6368 rtx src, dst, srcbase, dstbase;
6369 rtx part_bytes_reg = NULL;
6370 rtx mem;
6372 if (GET_CODE (operands[2]) != CONST_INT
6373 || GET_CODE (operands[3]) != CONST_INT
6374 || INTVAL (operands[2]) > 64
6375 || INTVAL (operands[3]) & 3)
6376 return 0;
6378 dstbase = operands[0];
6379 srcbase = operands[1];
6381 dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
6382 src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
6384 in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
6385 out_words_to_go = INTVAL (operands[2]) / 4;
6386 last_bytes = INTVAL (operands[2]) & 3;
6387 dstoffset = srcoffset = 0;
6389 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
6390 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
6392 for (i = 0; in_words_to_go >= 2; i+=4)
6394 if (in_words_to_go > 4)
6395 emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
6396 srcbase, &srcoffset));
6397 else
6398 emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
6399 FALSE, srcbase, &srcoffset));
6401 if (out_words_to_go)
6403 if (out_words_to_go > 4)
6404 emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
6405 dstbase, &dstoffset));
6406 else if (out_words_to_go != 1)
6407 emit_insn (arm_gen_store_multiple (0, out_words_to_go,
6408 dst, TRUE,
6409 (last_bytes == 0
6410 ? FALSE : TRUE),
6411 dstbase, &dstoffset));
6412 else
6414 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6415 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
6416 if (last_bytes != 0)
6418 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
6419 dstoffset += 4;
6424 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
6425 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
6428 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
6429 if (out_words_to_go)
6431 rtx sreg;
6433 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6434 sreg = copy_to_reg (mem);
6436 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6437 emit_move_insn (mem, sreg);
6438 in_words_to_go--;
6440 gcc_assert (!in_words_to_go); /* Sanity check */
6443 if (in_words_to_go)
6445 gcc_assert (in_words_to_go > 0);
6447 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6448 part_bytes_reg = copy_to_mode_reg (SImode, mem);
6451 gcc_assert (!last_bytes || part_bytes_reg);
6453 if (BYTES_BIG_ENDIAN && last_bytes)
6455 rtx tmp = gen_reg_rtx (SImode);
6457 /* The bytes we want are in the top end of the word. */
6458 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
6459 GEN_INT (8 * (4 - last_bytes))));
6460 part_bytes_reg = tmp;
6462 while (last_bytes)
6464 mem = adjust_automodify_address (dstbase, QImode,
6465 plus_constant (dst, last_bytes - 1),
6466 dstoffset + last_bytes - 1);
6467 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6469 if (--last_bytes)
6471 tmp = gen_reg_rtx (SImode);
6472 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
6473 part_bytes_reg = tmp;
6478 else
6480 if (last_bytes > 1)
6482 mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
6483 emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
6484 last_bytes -= 2;
6485 if (last_bytes)
6487 rtx tmp = gen_reg_rtx (SImode);
6488 emit_insn (gen_addsi3 (dst, dst, const2_rtx));
6489 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
6490 part_bytes_reg = tmp;
6491 dstoffset += 2;
6495 if (last_bytes)
6497 mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
6498 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6502 return 1;
6505 /* Select a dominance comparison mode if possible for a test of the general
6506 form (OP (COND_OR (X) (Y)) (const_int 0)). We support three forms.
6507 COND_OR == DOM_CC_X_AND_Y => (X && Y)
6508 COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6509 COND_OR == DOM_CC_X_OR_Y => (X || Y)
6510 In all cases OP will be either EQ or NE, but we don't need to know which
6511 here. If we are unable to support a dominance comparison we return
6512 CC mode. This will then fail to match for the RTL expressions that
6513 generate this call. */
6514 enum machine_mode
6515 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
6517 enum rtx_code cond1, cond2;
6518 int swapped = 0;
6520 /* Currently we will probably get the wrong result if the individual
6521 comparisons are not simple. This also ensures that it is safe to
6522 reverse a comparison if necessary. */
6523 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6524 != CCmode)
6525 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6526 != CCmode))
6527 return CCmode;
6529 /* The if_then_else variant of this tests the second condition if the
6530 first passes, but is true if the first fails. Reverse the first
6531 condition to get a true "inclusive-or" expression. */
6532 if (cond_or == DOM_CC_NX_OR_Y)
6533 cond1 = reverse_condition (cond1);
6535 /* If the comparisons are not equal, and one doesn't dominate the other,
6536 then we can't do this. */
6537 if (cond1 != cond2
6538 && !comparison_dominates_p (cond1, cond2)
6539 && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6540 return CCmode;
6542 if (swapped)
6544 enum rtx_code temp = cond1;
6545 cond1 = cond2;
6546 cond2 = temp;
6549 switch (cond1)
6551 case EQ:
6552 if (cond_or == DOM_CC_X_AND_Y)
6553 return CC_DEQmode;
6555 switch (cond2)
6557 case EQ: return CC_DEQmode;
6558 case LE: return CC_DLEmode;
6559 case LEU: return CC_DLEUmode;
6560 case GE: return CC_DGEmode;
6561 case GEU: return CC_DGEUmode;
6562 default: gcc_unreachable ();
6565 case LT:
6566 if (cond_or == DOM_CC_X_AND_Y)
6567 return CC_DLTmode;
6569 switch (cond2)
6571 case LT:
6572 return CC_DLTmode;
6573 case LE:
6574 return CC_DLEmode;
6575 case NE:
6576 return CC_DNEmode;
6577 default:
6578 gcc_unreachable ();
6581 case GT:
6582 if (cond_or == DOM_CC_X_AND_Y)
6583 return CC_DGTmode;
6585 switch (cond2)
6587 case GT:
6588 return CC_DGTmode;
6589 case GE:
6590 return CC_DGEmode;
6591 case NE:
6592 return CC_DNEmode;
6593 default:
6594 gcc_unreachable ();
6597 case LTU:
6598 if (cond_or == DOM_CC_X_AND_Y)
6599 return CC_DLTUmode;
6601 switch (cond2)
6603 case LTU:
6604 return CC_DLTUmode;
6605 case LEU:
6606 return CC_DLEUmode;
6607 case NE:
6608 return CC_DNEmode;
6609 default:
6610 gcc_unreachable ();
6613 case GTU:
6614 if (cond_or == DOM_CC_X_AND_Y)
6615 return CC_DGTUmode;
6617 switch (cond2)
6619 case GTU:
6620 return CC_DGTUmode;
6621 case GEU:
6622 return CC_DGEUmode;
6623 case NE:
6624 return CC_DNEmode;
6625 default:
6626 gcc_unreachable ();
6629 /* The remaining cases only occur when both comparisons are the
6630 same. */
6631 case NE:
6632 gcc_assert (cond1 == cond2);
6633 return CC_DNEmode;
6635 case LE:
6636 gcc_assert (cond1 == cond2);
6637 return CC_DLEmode;
6639 case GE:
6640 gcc_assert (cond1 == cond2);
6641 return CC_DGEmode;
6643 case LEU:
6644 gcc_assert (cond1 == cond2);
6645 return CC_DLEUmode;
6647 case GEU:
6648 gcc_assert (cond1 == cond2);
6649 return CC_DGEUmode;
6651 default:
6652 gcc_unreachable ();
6656 enum machine_mode
6657 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6659 /* All floating point compares return CCFP if it is an equality
6660 comparison, and CCFPE otherwise. */
6661 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6663 switch (op)
6665 case EQ:
6666 case NE:
6667 case UNORDERED:
6668 case ORDERED:
6669 case UNLT:
6670 case UNLE:
6671 case UNGT:
6672 case UNGE:
6673 case UNEQ:
6674 case LTGT:
6675 return CCFPmode;
6677 case LT:
6678 case LE:
6679 case GT:
6680 case GE:
6681 if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6682 return CCFPmode;
6683 return CCFPEmode;
6685 default:
6686 gcc_unreachable ();
6690 /* A compare with a shifted operand. Because of canonicalization, the
6691 comparison will have to be swapped when we emit the assembler. */
6692 if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6693 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6694 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6695 || GET_CODE (x) == ROTATERT))
6696 return CC_SWPmode;
6698 /* This operation is performed swapped, but since we only rely on the Z
6699 flag we don't need an additional mode. */
6700 if (GET_MODE (y) == SImode && REG_P (y)
6701 && GET_CODE (x) == NEG
6702 && (op == EQ || op == NE))
6703 return CC_Zmode;
6705 /* This is a special case that is used by combine to allow a
6706 comparison of a shifted byte load to be split into a zero-extend
6707 followed by a comparison of the shifted integer (only valid for
6708 equalities and unsigned inequalities). */
6709 if (GET_MODE (x) == SImode
6710 && GET_CODE (x) == ASHIFT
6711 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6712 && GET_CODE (XEXP (x, 0)) == SUBREG
6713 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6714 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6715 && (op == EQ || op == NE
6716 || op == GEU || op == GTU || op == LTU || op == LEU)
6717 && GET_CODE (y) == CONST_INT)
6718 return CC_Zmode;
6720 /* A construct for a conditional compare, if the false arm contains
6721 0, then both conditions must be true, otherwise either condition
6722 must be true. Not all conditions are possible, so CCmode is
6723 returned if it can't be done. */
6724 if (GET_CODE (x) == IF_THEN_ELSE
6725 && (XEXP (x, 2) == const0_rtx
6726 || XEXP (x, 2) == const1_rtx)
6727 && COMPARISON_P (XEXP (x, 0))
6728 && COMPARISON_P (XEXP (x, 1)))
6729 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6730 INTVAL (XEXP (x, 2)));
6732 /* Alternate canonicalizations of the above. These are somewhat cleaner. */
6733 if (GET_CODE (x) == AND
6734 && COMPARISON_P (XEXP (x, 0))
6735 && COMPARISON_P (XEXP (x, 1)))
6736 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6737 DOM_CC_X_AND_Y);
6739 if (GET_CODE (x) == IOR
6740 && COMPARISON_P (XEXP (x, 0))
6741 && COMPARISON_P (XEXP (x, 1)))
6742 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6743 DOM_CC_X_OR_Y);
6745 /* An operation (on Thumb) where we want to test for a single bit.
6746 This is done by shifting that bit up into the top bit of a
6747 scratch register; we can then branch on the sign bit. */
6748 if (TARGET_THUMB
6749 && GET_MODE (x) == SImode
6750 && (op == EQ || op == NE)
6751 && GET_CODE (x) == ZERO_EXTRACT
6752 && XEXP (x, 1) == const1_rtx)
6753 return CC_Nmode;
6755 /* An operation that sets the condition codes as a side-effect, the
6756 V flag is not set correctly, so we can only use comparisons where
6757 this doesn't matter. (For LT and GE we can use "mi" and "pl"
6758 instead.) */
6759 if (GET_MODE (x) == SImode
6760 && y == const0_rtx
6761 && (op == EQ || op == NE || op == LT || op == GE)
6762 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6763 || GET_CODE (x) == AND || GET_CODE (x) == IOR
6764 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6765 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6766 || GET_CODE (x) == LSHIFTRT
6767 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6768 || GET_CODE (x) == ROTATERT
6769 || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6770 return CC_NOOVmode;
6772 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6773 return CC_Zmode;
6775 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6776 && GET_CODE (x) == PLUS
6777 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6778 return CC_Cmode;
6780 return CCmode;
6783 /* X and Y are two things to compare using CODE. Emit the compare insn and
6784 return the rtx for register 0 in the proper mode. FP means this is a
6785 floating point compare: I don't think that it is needed on the arm. */
6787 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6789 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6790 rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6792 emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
6794 return cc_reg;
6797 /* Generate a sequence of insns that will generate the correct return
6798 address mask depending on the physical architecture that the program
6799 is running on. */
6801 arm_gen_return_addr_mask (void)
6803 rtx reg = gen_reg_rtx (Pmode);
6805 emit_insn (gen_return_addr_mask (reg));
6806 return reg;
6809 void
6810 arm_reload_in_hi (rtx *operands)
6812 rtx ref = operands[1];
6813 rtx base, scratch;
6814 HOST_WIDE_INT offset = 0;
6816 if (GET_CODE (ref) == SUBREG)
6818 offset = SUBREG_BYTE (ref);
6819 ref = SUBREG_REG (ref);
6822 if (GET_CODE (ref) == REG)
6824 /* We have a pseudo which has been spilt onto the stack; there
6825 are two cases here: the first where there is a simple
6826 stack-slot replacement and a second where the stack-slot is
6827 out of range, or is used as a subreg. */
6828 if (reg_equiv_mem[REGNO (ref)])
6830 ref = reg_equiv_mem[REGNO (ref)];
6831 base = find_replacement (&XEXP (ref, 0));
6833 else
6834 /* The slot is out of range, or was dressed up in a SUBREG. */
6835 base = reg_equiv_address[REGNO (ref)];
6837 else
6838 base = find_replacement (&XEXP (ref, 0));
6840 /* Handle the case where the address is too complex to be offset by 1. */
6841 if (GET_CODE (base) == MINUS
6842 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6844 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6846 emit_set_insn (base_plus, base);
6847 base = base_plus;
6849 else if (GET_CODE (base) == PLUS)
6851 /* The addend must be CONST_INT, or we would have dealt with it above. */
6852 HOST_WIDE_INT hi, lo;
6854 offset += INTVAL (XEXP (base, 1));
6855 base = XEXP (base, 0);
6857 /* Rework the address into a legal sequence of insns. */
6858 /* Valid range for lo is -4095 -> 4095 */
6859 lo = (offset >= 0
6860 ? (offset & 0xfff)
6861 : -((-offset) & 0xfff));
6863 /* Corner case, if lo is the max offset then we would be out of range
6864 once we have added the additional 1 below, so bump the msb into the
6865 pre-loading insn(s). */
6866 if (lo == 4095)
6867 lo &= 0x7ff;
6869 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6870 ^ (HOST_WIDE_INT) 0x80000000)
6871 - (HOST_WIDE_INT) 0x80000000);
6873 gcc_assert (hi + lo == offset);
6875 if (hi != 0)
6877 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6879 /* Get the base address; addsi3 knows how to handle constants
6880 that require more than one insn. */
6881 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6882 base = base_plus;
6883 offset = lo;
6887 /* Operands[2] may overlap operands[0] (though it won't overlap
6888 operands[1]), that's why we asked for a DImode reg -- so we can
6889 use the bit that does not overlap. */
6890 if (REGNO (operands[2]) == REGNO (operands[0]))
6891 scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6892 else
6893 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6895 emit_insn (gen_zero_extendqisi2 (scratch,
6896 gen_rtx_MEM (QImode,
6897 plus_constant (base,
6898 offset))));
6899 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6900 gen_rtx_MEM (QImode,
6901 plus_constant (base,
6902 offset + 1))));
6903 if (!BYTES_BIG_ENDIAN)
6904 emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
6905 gen_rtx_IOR (SImode,
6906 gen_rtx_ASHIFT
6907 (SImode,
6908 gen_rtx_SUBREG (SImode, operands[0], 0),
6909 GEN_INT (8)),
6910 scratch));
6911 else
6912 emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
6913 gen_rtx_IOR (SImode,
6914 gen_rtx_ASHIFT (SImode, scratch,
6915 GEN_INT (8)),
6916 gen_rtx_SUBREG (SImode, operands[0], 0)));
6919 /* Handle storing a half-word to memory during reload by synthesizing as two
6920 byte stores. Take care not to clobber the input values until after we
6921 have moved them somewhere safe. This code assumes that if the DImode
6922 scratch in operands[2] overlaps either the input value or output address
6923 in some way, then that value must die in this insn (we absolutely need
6924 two scratch registers for some corner cases). */
6925 void
6926 arm_reload_out_hi (rtx *operands)
6928 rtx ref = operands[0];
6929 rtx outval = operands[1];
6930 rtx base, scratch;
6931 HOST_WIDE_INT offset = 0;
6933 if (GET_CODE (ref) == SUBREG)
6935 offset = SUBREG_BYTE (ref);
6936 ref = SUBREG_REG (ref);
6939 if (GET_CODE (ref) == REG)
6941 /* We have a pseudo which has been spilt onto the stack; there
6942 are two cases here: the first where there is a simple
6943 stack-slot replacement and a second where the stack-slot is
6944 out of range, or is used as a subreg. */
6945 if (reg_equiv_mem[REGNO (ref)])
6947 ref = reg_equiv_mem[REGNO (ref)];
6948 base = find_replacement (&XEXP (ref, 0));
6950 else
6951 /* The slot is out of range, or was dressed up in a SUBREG. */
6952 base = reg_equiv_address[REGNO (ref)];
6954 else
6955 base = find_replacement (&XEXP (ref, 0));
6957 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6959 /* Handle the case where the address is too complex to be offset by 1. */
6960 if (GET_CODE (base) == MINUS
6961 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6963 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6965 /* Be careful not to destroy OUTVAL. */
6966 if (reg_overlap_mentioned_p (base_plus, outval))
6968 /* Updating base_plus might destroy outval, see if we can
6969 swap the scratch and base_plus. */
6970 if (!reg_overlap_mentioned_p (scratch, outval))
6972 rtx tmp = scratch;
6973 scratch = base_plus;
6974 base_plus = tmp;
6976 else
6978 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6980 /* Be conservative and copy OUTVAL into the scratch now,
6981 this should only be necessary if outval is a subreg
6982 of something larger than a word. */
6983 /* XXX Might this clobber base? I can't see how it can,
6984 since scratch is known to overlap with OUTVAL, and
6985 must be wider than a word. */
6986 emit_insn (gen_movhi (scratch_hi, outval));
6987 outval = scratch_hi;
6991 emit_set_insn (base_plus, base);
6992 base = base_plus;
6994 else if (GET_CODE (base) == PLUS)
6996 /* The addend must be CONST_INT, or we would have dealt with it above. */
6997 HOST_WIDE_INT hi, lo;
6999 offset += INTVAL (XEXP (base, 1));
7000 base = XEXP (base, 0);
7002 /* Rework the address into a legal sequence of insns. */
7003 /* Valid range for lo is -4095 -> 4095 */
7004 lo = (offset >= 0
7005 ? (offset & 0xfff)
7006 : -((-offset) & 0xfff));
7008 /* Corner case, if lo is the max offset then we would be out of range
7009 once we have added the additional 1 below, so bump the msb into the
7010 pre-loading insn(s). */
7011 if (lo == 4095)
7012 lo &= 0x7ff;
7014 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
7015 ^ (HOST_WIDE_INT) 0x80000000)
7016 - (HOST_WIDE_INT) 0x80000000);
7018 gcc_assert (hi + lo == offset);
7020 if (hi != 0)
7022 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7024 /* Be careful not to destroy OUTVAL. */
7025 if (reg_overlap_mentioned_p (base_plus, outval))
7027 /* Updating base_plus might destroy outval, see if we
7028 can swap the scratch and base_plus. */
7029 if (!reg_overlap_mentioned_p (scratch, outval))
7031 rtx tmp = scratch;
7032 scratch = base_plus;
7033 base_plus = tmp;
7035 else
7037 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
7039 /* Be conservative and copy outval into scratch now,
7040 this should only be necessary if outval is a
7041 subreg of something larger than a word. */
7042 /* XXX Might this clobber base? I can't see how it
7043 can, since scratch is known to overlap with
7044 outval. */
7045 emit_insn (gen_movhi (scratch_hi, outval));
7046 outval = scratch_hi;
7050 /* Get the base address; addsi3 knows how to handle constants
7051 that require more than one insn. */
7052 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
7053 base = base_plus;
7054 offset = lo;
7058 if (BYTES_BIG_ENDIAN)
7060 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7061 plus_constant (base, offset + 1)),
7062 gen_lowpart (QImode, outval)));
7063 emit_insn (gen_lshrsi3 (scratch,
7064 gen_rtx_SUBREG (SImode, outval, 0),
7065 GEN_INT (8)));
7066 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7067 gen_lowpart (QImode, scratch)));
7069 else
7071 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7072 gen_lowpart (QImode, outval)));
7073 emit_insn (gen_lshrsi3 (scratch,
7074 gen_rtx_SUBREG (SImode, outval, 0),
7075 GEN_INT (8)));
7076 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7077 plus_constant (base, offset + 1)),
7078 gen_lowpart (QImode, scratch)));
7082 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
7083 (padded to the size of a word) should be passed in a register. */
7085 static bool
7086 arm_must_pass_in_stack (enum machine_mode mode, tree type)
7088 if (TARGET_AAPCS_BASED)
7089 return must_pass_in_stack_var_size (mode, type);
7090 else
7091 return must_pass_in_stack_var_size_or_pad (mode, type);
7095 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
7096 Return true if an argument passed on the stack should be padded upwards,
7097 i.e. if the least-significant byte has useful data.
7098 For legacy APCS ABIs we use the default. For AAPCS based ABIs small
7099 aggregate types are placed in the lowest memory address. */
7101 bool
7102 arm_pad_arg_upward (enum machine_mode mode, tree type)
7104 if (!TARGET_AAPCS_BASED)
7105 return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
7107 if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
7108 return false;
7110 return true;
7114 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
7115 For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
7116 byte of the register has useful data, and return the opposite if the
7117 most significant byte does.
7118 For AAPCS, small aggregates and small complex types are always padded
7119 upwards. */
7121 bool
7122 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
7123 tree type, int first ATTRIBUTE_UNUSED)
7125 if (TARGET_AAPCS_BASED
7126 && BYTES_BIG_ENDIAN
7127 && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
7128 && int_size_in_bytes (type) <= 4)
7129 return true;
7131 /* Otherwise, use default padding. */
7132 return !BYTES_BIG_ENDIAN;
7136 /* Print a symbolic form of X to the debug file, F. */
7137 static void
7138 arm_print_value (FILE *f, rtx x)
7140 switch (GET_CODE (x))
7142 case CONST_INT:
7143 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
7144 return;
7146 case CONST_DOUBLE:
7147 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
7148 return;
7150 case CONST_VECTOR:
7152 int i;
7154 fprintf (f, "<");
7155 for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
7157 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
7158 if (i < (CONST_VECTOR_NUNITS (x) - 1))
7159 fputc (',', f);
7161 fprintf (f, ">");
7163 return;
7165 case CONST_STRING:
7166 fprintf (f, "\"%s\"", XSTR (x, 0));
7167 return;
7169 case SYMBOL_REF:
7170 fprintf (f, "`%s'", XSTR (x, 0));
7171 return;
7173 case LABEL_REF:
7174 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
7175 return;
7177 case CONST:
7178 arm_print_value (f, XEXP (x, 0));
7179 return;
7181 case PLUS:
7182 arm_print_value (f, XEXP (x, 0));
7183 fprintf (f, "+");
7184 arm_print_value (f, XEXP (x, 1));
7185 return;
7187 case PC:
7188 fprintf (f, "pc");
7189 return;
7191 default:
7192 fprintf (f, "????");
7193 return;
7197 /* Routines for manipulation of the constant pool. */
7199 /* Arm instructions cannot load a large constant directly into a
7200 register; they have to come from a pc relative load. The constant
7201 must therefore be placed in the addressable range of the pc
7202 relative load. Depending on the precise pc relative load
7203 instruction the range is somewhere between 256 bytes and 4k. This
7204 means that we often have to dump a constant inside a function, and
7205 generate code to branch around it.
7207 It is important to minimize this, since the branches will slow
7208 things down and make the code larger.
7210 Normally we can hide the table after an existing unconditional
7211 branch so that there is no interruption of the flow, but in the
7212 worst case the code looks like this:
7214 ldr rn, L1
7216 b L2
7217 align
7218 L1: .long value
7222 ldr rn, L3
7224 b L4
7225 align
7226 L3: .long value
7230 We fix this by performing a scan after scheduling, which notices
7231 which instructions need to have their operands fetched from the
7232 constant table and builds the table.
7234 The algorithm starts by building a table of all the constants that
7235 need fixing up and all the natural barriers in the function (places
7236 where a constant table can be dropped without breaking the flow).
7237 For each fixup we note how far the pc-relative replacement will be
7238 able to reach and the offset of the instruction into the function.
7240 Having built the table we then group the fixes together to form
7241 tables that are as large as possible (subject to addressing
7242 constraints) and emit each table of constants after the last
7243 barrier that is within range of all the instructions in the group.
7244 If a group does not contain a barrier, then we forcibly create one
7245 by inserting a jump instruction into the flow. Once the table has
7246 been inserted, the insns are then modified to reference the
7247 relevant entry in the pool.
7249 Possible enhancements to the algorithm (not implemented) are:
7251 1) For some processors and object formats, there may be benefit in
7252 aligning the pools to the start of cache lines; this alignment
7253 would need to be taken into account when calculating addressability
7254 of a pool. */
7256 /* These typedefs are located at the start of this file, so that
7257 they can be used in the prototypes there. This comment is to
7258 remind readers of that fact so that the following structures
7259 can be understood more easily.
7261 typedef struct minipool_node Mnode;
7262 typedef struct minipool_fixup Mfix; */
7264 struct minipool_node
7266 /* Doubly linked chain of entries. */
7267 Mnode * next;
7268 Mnode * prev;
7269 /* The maximum offset into the code that this entry can be placed. While
7270 pushing fixes for forward references, all entries are sorted in order
7271 of increasing max_address. */
7272 HOST_WIDE_INT max_address;
7273 /* Similarly for an entry inserted for a backwards ref. */
7274 HOST_WIDE_INT min_address;
7275 /* The number of fixes referencing this entry. This can become zero
7276 if we "unpush" an entry. In this case we ignore the entry when we
7277 come to emit the code. */
7278 int refcount;
7279 /* The offset from the start of the minipool. */
7280 HOST_WIDE_INT offset;
7281 /* The value in table. */
7282 rtx value;
7283 /* The mode of value. */
7284 enum machine_mode mode;
7285 /* The size of the value. With iWMMXt enabled
7286 sizes > 4 also imply an alignment of 8-bytes. */
7287 int fix_size;
7290 struct minipool_fixup
7292 Mfix * next;
7293 rtx insn;
7294 HOST_WIDE_INT address;
7295 rtx * loc;
7296 enum machine_mode mode;
7297 int fix_size;
7298 rtx value;
7299 Mnode * minipool;
7300 HOST_WIDE_INT forwards;
7301 HOST_WIDE_INT backwards;
7304 /* Fixes less than a word need padding out to a word boundary. */
7305 #define MINIPOOL_FIX_SIZE(mode) \
7306 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
7308 static Mnode * minipool_vector_head;
7309 static Mnode * minipool_vector_tail;
7310 static rtx minipool_vector_label;
7311 static int minipool_pad;
7313 /* The linked list of all minipool fixes required for this function. */
7314 Mfix * minipool_fix_head;
7315 Mfix * minipool_fix_tail;
7316 /* The fix entry for the current minipool, once it has been placed. */
7317 Mfix * minipool_barrier;
7319 /* Determines if INSN is the start of a jump table. Returns the end
7320 of the TABLE or NULL_RTX. */
7321 static rtx
7322 is_jump_table (rtx insn)
7324 rtx table;
7326 if (GET_CODE (insn) == JUMP_INSN
7327 && JUMP_LABEL (insn) != NULL
7328 && ((table = next_real_insn (JUMP_LABEL (insn)))
7329 == next_real_insn (insn))
7330 && table != NULL
7331 && GET_CODE (table) == JUMP_INSN
7332 && (GET_CODE (PATTERN (table)) == ADDR_VEC
7333 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
7334 return table;
7336 return NULL_RTX;
7339 #ifndef JUMP_TABLES_IN_TEXT_SECTION
7340 #define JUMP_TABLES_IN_TEXT_SECTION 0
7341 #endif
7343 static HOST_WIDE_INT
7344 get_jump_table_size (rtx insn)
7346 /* ADDR_VECs only take room if read-only data does into the text
7347 section. */
7348 if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
7350 rtx body = PATTERN (insn);
7351 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
7353 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
7356 return 0;
7359 /* Move a minipool fix MP from its current location to before MAX_MP.
7360 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
7361 constraints may need updating. */
7362 static Mnode *
7363 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
7364 HOST_WIDE_INT max_address)
7366 /* The code below assumes these are different. */
7367 gcc_assert (mp != max_mp);
7369 if (max_mp == NULL)
7371 if (max_address < mp->max_address)
7372 mp->max_address = max_address;
7374 else
7376 if (max_address > max_mp->max_address - mp->fix_size)
7377 mp->max_address = max_mp->max_address - mp->fix_size;
7378 else
7379 mp->max_address = max_address;
7381 /* Unlink MP from its current position. Since max_mp is non-null,
7382 mp->prev must be non-null. */
7383 mp->prev->next = mp->next;
7384 if (mp->next != NULL)
7385 mp->next->prev = mp->prev;
7386 else
7387 minipool_vector_tail = mp->prev;
7389 /* Re-insert it before MAX_MP. */
7390 mp->next = max_mp;
7391 mp->prev = max_mp->prev;
7392 max_mp->prev = mp;
7394 if (mp->prev != NULL)
7395 mp->prev->next = mp;
7396 else
7397 minipool_vector_head = mp;
7400 /* Save the new entry. */
7401 max_mp = mp;
7403 /* Scan over the preceding entries and adjust their addresses as
7404 required. */
7405 while (mp->prev != NULL
7406 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7408 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7409 mp = mp->prev;
7412 return max_mp;
7415 /* Add a constant to the minipool for a forward reference. Returns the
7416 node added or NULL if the constant will not fit in this pool. */
7417 static Mnode *
7418 add_minipool_forward_ref (Mfix *fix)
7420 /* If set, max_mp is the first pool_entry that has a lower
7421 constraint than the one we are trying to add. */
7422 Mnode * max_mp = NULL;
7423 HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
7424 Mnode * mp;
7426 /* If this fix's address is greater than the address of the first
7427 entry, then we can't put the fix in this pool. We subtract the
7428 size of the current fix to ensure that if the table is fully
7429 packed we still have enough room to insert this value by shuffling
7430 the other fixes forwards. */
7431 if (minipool_vector_head &&
7432 fix->address >= minipool_vector_head->max_address - fix->fix_size)
7433 return NULL;
7435 /* Scan the pool to see if a constant with the same value has
7436 already been added. While we are doing this, also note the
7437 location where we must insert the constant if it doesn't already
7438 exist. */
7439 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7441 if (GET_CODE (fix->value) == GET_CODE (mp->value)
7442 && fix->mode == mp->mode
7443 && (GET_CODE (fix->value) != CODE_LABEL
7444 || (CODE_LABEL_NUMBER (fix->value)
7445 == CODE_LABEL_NUMBER (mp->value)))
7446 && rtx_equal_p (fix->value, mp->value))
7448 /* More than one fix references this entry. */
7449 mp->refcount++;
7450 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
7453 /* Note the insertion point if necessary. */
7454 if (max_mp == NULL
7455 && mp->max_address > max_address)
7456 max_mp = mp;
7458 /* If we are inserting an 8-bytes aligned quantity and
7459 we have not already found an insertion point, then
7460 make sure that all such 8-byte aligned quantities are
7461 placed at the start of the pool. */
7462 if (ARM_DOUBLEWORD_ALIGN
7463 && max_mp == NULL
7464 && fix->fix_size == 8
7465 && mp->fix_size != 8)
7467 max_mp = mp;
7468 max_address = mp->max_address;
7472 /* The value is not currently in the minipool, so we need to create
7473 a new entry for it. If MAX_MP is NULL, the entry will be put on
7474 the end of the list since the placement is less constrained than
7475 any existing entry. Otherwise, we insert the new fix before
7476 MAX_MP and, if necessary, adjust the constraints on the other
7477 entries. */
7478 mp = XNEW (Mnode);
7479 mp->fix_size = fix->fix_size;
7480 mp->mode = fix->mode;
7481 mp->value = fix->value;
7482 mp->refcount = 1;
7483 /* Not yet required for a backwards ref. */
7484 mp->min_address = -65536;
7486 if (max_mp == NULL)
7488 mp->max_address = max_address;
7489 mp->next = NULL;
7490 mp->prev = minipool_vector_tail;
7492 if (mp->prev == NULL)
7494 minipool_vector_head = mp;
7495 minipool_vector_label = gen_label_rtx ();
7497 else
7498 mp->prev->next = mp;
7500 minipool_vector_tail = mp;
7502 else
7504 if (max_address > max_mp->max_address - mp->fix_size)
7505 mp->max_address = max_mp->max_address - mp->fix_size;
7506 else
7507 mp->max_address = max_address;
7509 mp->next = max_mp;
7510 mp->prev = max_mp->prev;
7511 max_mp->prev = mp;
7512 if (mp->prev != NULL)
7513 mp->prev->next = mp;
7514 else
7515 minipool_vector_head = mp;
7518 /* Save the new entry. */
7519 max_mp = mp;
7521 /* Scan over the preceding entries and adjust their addresses as
7522 required. */
7523 while (mp->prev != NULL
7524 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7526 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7527 mp = mp->prev;
7530 return max_mp;
7533 static Mnode *
7534 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7535 HOST_WIDE_INT min_address)
7537 HOST_WIDE_INT offset;
7539 /* The code below assumes these are different. */
7540 gcc_assert (mp != min_mp);
7542 if (min_mp == NULL)
7544 if (min_address > mp->min_address)
7545 mp->min_address = min_address;
7547 else
7549 /* We will adjust this below if it is too loose. */
7550 mp->min_address = min_address;
7552 /* Unlink MP from its current position. Since min_mp is non-null,
7553 mp->next must be non-null. */
7554 mp->next->prev = mp->prev;
7555 if (mp->prev != NULL)
7556 mp->prev->next = mp->next;
7557 else
7558 minipool_vector_head = mp->next;
7560 /* Reinsert it after MIN_MP. */
7561 mp->prev = min_mp;
7562 mp->next = min_mp->next;
7563 min_mp->next = mp;
7564 if (mp->next != NULL)
7565 mp->next->prev = mp;
7566 else
7567 minipool_vector_tail = mp;
7570 min_mp = mp;
7572 offset = 0;
7573 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7575 mp->offset = offset;
7576 if (mp->refcount > 0)
7577 offset += mp->fix_size;
7579 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7580 mp->next->min_address = mp->min_address + mp->fix_size;
7583 return min_mp;
7586 /* Add a constant to the minipool for a backward reference. Returns the
7587 node added or NULL if the constant will not fit in this pool.
7589 Note that the code for insertion for a backwards reference can be
7590 somewhat confusing because the calculated offsets for each fix do
7591 not take into account the size of the pool (which is still under
7592 construction. */
7593 static Mnode *
7594 add_minipool_backward_ref (Mfix *fix)
7596 /* If set, min_mp is the last pool_entry that has a lower constraint
7597 than the one we are trying to add. */
7598 Mnode *min_mp = NULL;
7599 /* This can be negative, since it is only a constraint. */
7600 HOST_WIDE_INT min_address = fix->address - fix->backwards;
7601 Mnode *mp;
7603 /* If we can't reach the current pool from this insn, or if we can't
7604 insert this entry at the end of the pool without pushing other
7605 fixes out of range, then we don't try. This ensures that we
7606 can't fail later on. */
7607 if (min_address >= minipool_barrier->address
7608 || (minipool_vector_tail->min_address + fix->fix_size
7609 >= minipool_barrier->address))
7610 return NULL;
7612 /* Scan the pool to see if a constant with the same value has
7613 already been added. While we are doing this, also note the
7614 location where we must insert the constant if it doesn't already
7615 exist. */
7616 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7618 if (GET_CODE (fix->value) == GET_CODE (mp->value)
7619 && fix->mode == mp->mode
7620 && (GET_CODE (fix->value) != CODE_LABEL
7621 || (CODE_LABEL_NUMBER (fix->value)
7622 == CODE_LABEL_NUMBER (mp->value)))
7623 && rtx_equal_p (fix->value, mp->value)
7624 /* Check that there is enough slack to move this entry to the
7625 end of the table (this is conservative). */
7626 && (mp->max_address
7627 > (minipool_barrier->address
7628 + minipool_vector_tail->offset
7629 + minipool_vector_tail->fix_size)))
7631 mp->refcount++;
7632 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7635 if (min_mp != NULL)
7636 mp->min_address += fix->fix_size;
7637 else
7639 /* Note the insertion point if necessary. */
7640 if (mp->min_address < min_address)
7642 /* For now, we do not allow the insertion of 8-byte alignment
7643 requiring nodes anywhere but at the start of the pool. */
7644 if (ARM_DOUBLEWORD_ALIGN
7645 && fix->fix_size == 8 && mp->fix_size != 8)
7646 return NULL;
7647 else
7648 min_mp = mp;
7650 else if (mp->max_address
7651 < minipool_barrier->address + mp->offset + fix->fix_size)
7653 /* Inserting before this entry would push the fix beyond
7654 its maximum address (which can happen if we have
7655 re-located a forwards fix); force the new fix to come
7656 after it. */
7657 min_mp = mp;
7658 min_address = mp->min_address + fix->fix_size;
7660 /* If we are inserting an 8-bytes aligned quantity and
7661 we have not already found an insertion point, then
7662 make sure that all such 8-byte aligned quantities are
7663 placed at the start of the pool. */
7664 else if (ARM_DOUBLEWORD_ALIGN
7665 && min_mp == NULL
7666 && fix->fix_size == 8
7667 && mp->fix_size < 8)
7669 min_mp = mp;
7670 min_address = mp->min_address + fix->fix_size;
7675 /* We need to create a new entry. */
7676 mp = XNEW (Mnode);
7677 mp->fix_size = fix->fix_size;
7678 mp->mode = fix->mode;
7679 mp->value = fix->value;
7680 mp->refcount = 1;
7681 mp->max_address = minipool_barrier->address + 65536;
7683 mp->min_address = min_address;
7685 if (min_mp == NULL)
7687 mp->prev = NULL;
7688 mp->next = minipool_vector_head;
7690 if (mp->next == NULL)
7692 minipool_vector_tail = mp;
7693 minipool_vector_label = gen_label_rtx ();
7695 else
7696 mp->next->prev = mp;
7698 minipool_vector_head = mp;
7700 else
7702 mp->next = min_mp->next;
7703 mp->prev = min_mp;
7704 min_mp->next = mp;
7706 if (mp->next != NULL)
7707 mp->next->prev = mp;
7708 else
7709 minipool_vector_tail = mp;
7712 /* Save the new entry. */
7713 min_mp = mp;
7715 if (mp->prev)
7716 mp = mp->prev;
7717 else
7718 mp->offset = 0;
7720 /* Scan over the following entries and adjust their offsets. */
7721 while (mp->next != NULL)
7723 if (mp->next->min_address < mp->min_address + mp->fix_size)
7724 mp->next->min_address = mp->min_address + mp->fix_size;
7726 if (mp->refcount)
7727 mp->next->offset = mp->offset + mp->fix_size;
7728 else
7729 mp->next->offset = mp->offset;
7731 mp = mp->next;
7734 return min_mp;
7737 static void
7738 assign_minipool_offsets (Mfix *barrier)
7740 HOST_WIDE_INT offset = 0;
7741 Mnode *mp;
7743 minipool_barrier = barrier;
7745 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7747 mp->offset = offset;
7749 if (mp->refcount > 0)
7750 offset += mp->fix_size;
7754 /* Output the literal table */
7755 static void
7756 dump_minipool (rtx scan)
7758 Mnode * mp;
7759 Mnode * nmp;
7760 int align64 = 0;
7762 if (ARM_DOUBLEWORD_ALIGN)
7763 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7764 if (mp->refcount > 0 && mp->fix_size == 8)
7766 align64 = 1;
7767 break;
7770 if (dump_file)
7771 fprintf (dump_file,
7772 ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7773 INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7775 scan = emit_label_after (gen_label_rtx (), scan);
7776 scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7777 scan = emit_label_after (minipool_vector_label, scan);
7779 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7781 if (mp->refcount > 0)
7783 if (dump_file)
7785 fprintf (dump_file,
7786 ";; Offset %u, min %ld, max %ld ",
7787 (unsigned) mp->offset, (unsigned long) mp->min_address,
7788 (unsigned long) mp->max_address);
7789 arm_print_value (dump_file, mp->value);
7790 fputc ('\n', dump_file);
7793 switch (mp->fix_size)
7795 #ifdef HAVE_consttable_1
7796 case 1:
7797 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7798 break;
7800 #endif
7801 #ifdef HAVE_consttable_2
7802 case 2:
7803 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7804 break;
7806 #endif
7807 #ifdef HAVE_consttable_4
7808 case 4:
7809 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7810 break;
7812 #endif
7813 #ifdef HAVE_consttable_8
7814 case 8:
7815 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7816 break;
7818 #endif
7819 default:
7820 gcc_unreachable ();
7824 nmp = mp->next;
7825 free (mp);
7828 minipool_vector_head = minipool_vector_tail = NULL;
7829 scan = emit_insn_after (gen_consttable_end (), scan);
7830 scan = emit_barrier_after (scan);
7833 /* Return the cost of forcibly inserting a barrier after INSN. */
7834 static int
7835 arm_barrier_cost (rtx insn)
7837 /* Basing the location of the pool on the loop depth is preferable,
7838 but at the moment, the basic block information seems to be
7839 corrupt by this stage of the compilation. */
7840 int base_cost = 50;
7841 rtx next = next_nonnote_insn (insn);
7843 if (next != NULL && GET_CODE (next) == CODE_LABEL)
7844 base_cost -= 20;
7846 switch (GET_CODE (insn))
7848 case CODE_LABEL:
7849 /* It will always be better to place the table before the label, rather
7850 than after it. */
7851 return 50;
7853 case INSN:
7854 case CALL_INSN:
7855 return base_cost;
7857 case JUMP_INSN:
7858 return base_cost - 10;
7860 default:
7861 return base_cost + 10;
7865 /* Find the best place in the insn stream in the range
7866 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7867 Create the barrier by inserting a jump and add a new fix entry for
7868 it. */
7869 static Mfix *
7870 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7872 HOST_WIDE_INT count = 0;
7873 rtx barrier;
7874 rtx from = fix->insn;
7875 rtx selected = from;
7876 int selected_cost;
7877 HOST_WIDE_INT selected_address;
7878 Mfix * new_fix;
7879 HOST_WIDE_INT max_count = max_address - fix->address;
7880 rtx label = gen_label_rtx ();
7882 selected_cost = arm_barrier_cost (from);
7883 selected_address = fix->address;
7885 while (from && count < max_count)
7887 rtx tmp;
7888 int new_cost;
7890 /* This code shouldn't have been called if there was a natural barrier
7891 within range. */
7892 gcc_assert (GET_CODE (from) != BARRIER);
7894 /* Count the length of this insn. */
7895 count += get_attr_length (from);
7897 /* If there is a jump table, add its length. */
7898 tmp = is_jump_table (from);
7899 if (tmp != NULL)
7901 count += get_jump_table_size (tmp);
7903 /* Jump tables aren't in a basic block, so base the cost on
7904 the dispatch insn. If we select this location, we will
7905 still put the pool after the table. */
7906 new_cost = arm_barrier_cost (from);
7908 if (count < max_count && new_cost <= selected_cost)
7910 selected = tmp;
7911 selected_cost = new_cost;
7912 selected_address = fix->address + count;
7915 /* Continue after the dispatch table. */
7916 from = NEXT_INSN (tmp);
7917 continue;
7920 new_cost = arm_barrier_cost (from);
7922 if (count < max_count && new_cost <= selected_cost)
7924 selected = from;
7925 selected_cost = new_cost;
7926 selected_address = fix->address + count;
7929 from = NEXT_INSN (from);
7932 /* Create a new JUMP_INSN that branches around a barrier. */
7933 from = emit_jump_insn_after (gen_jump (label), selected);
7934 JUMP_LABEL (from) = label;
7935 barrier = emit_barrier_after (from);
7936 emit_label_after (label, barrier);
7938 /* Create a minipool barrier entry for the new barrier. */
7939 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7940 new_fix->insn = barrier;
7941 new_fix->address = selected_address;
7942 new_fix->next = fix->next;
7943 fix->next = new_fix;
7945 return new_fix;
7948 /* Record that there is a natural barrier in the insn stream at
7949 ADDRESS. */
7950 static void
7951 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7953 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7955 fix->insn = insn;
7956 fix->address = address;
7958 fix->next = NULL;
7959 if (minipool_fix_head != NULL)
7960 minipool_fix_tail->next = fix;
7961 else
7962 minipool_fix_head = fix;
7964 minipool_fix_tail = fix;
7967 /* Record INSN, which will need fixing up to load a value from the
7968 minipool. ADDRESS is the offset of the insn since the start of the
7969 function; LOC is a pointer to the part of the insn which requires
7970 fixing; VALUE is the constant that must be loaded, which is of type
7971 MODE. */
7972 static void
7973 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7974 enum machine_mode mode, rtx value)
7976 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7978 #ifdef AOF_ASSEMBLER
7979 /* PIC symbol references need to be converted into offsets into the
7980 based area. */
7981 /* XXX This shouldn't be done here. */
7982 if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7983 value = aof_pic_entry (value);
7984 #endif /* AOF_ASSEMBLER */
7986 fix->insn = insn;
7987 fix->address = address;
7988 fix->loc = loc;
7989 fix->mode = mode;
7990 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7991 fix->value = value;
7992 fix->forwards = get_attr_pool_range (insn);
7993 fix->backwards = get_attr_neg_pool_range (insn);
7994 fix->minipool = NULL;
7996 /* If an insn doesn't have a range defined for it, then it isn't
7997 expecting to be reworked by this code. Better to stop now than
7998 to generate duff assembly code. */
7999 gcc_assert (fix->forwards || fix->backwards);
8001 /* If an entry requires 8-byte alignment then assume all constant pools
8002 require 4 bytes of padding. Trying to do this later on a per-pool
8003 basis is awkward becuse existing pool entries have to be modified. */
8004 if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
8005 minipool_pad = 4;
8007 if (dump_file)
8009 fprintf (dump_file,
8010 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
8011 GET_MODE_NAME (mode),
8012 INSN_UID (insn), (unsigned long) address,
8013 -1 * (long)fix->backwards, (long)fix->forwards);
8014 arm_print_value (dump_file, fix->value);
8015 fprintf (dump_file, "\n");
8018 /* Add it to the chain of fixes. */
8019 fix->next = NULL;
8021 if (minipool_fix_head != NULL)
8022 minipool_fix_tail->next = fix;
8023 else
8024 minipool_fix_head = fix;
8026 minipool_fix_tail = fix;
8029 /* Return the cost of synthesizing a 64-bit constant VAL inline.
8030 Returns the number of insns needed, or 99 if we don't know how to
8031 do it. */
8033 arm_const_double_inline_cost (rtx val)
8035 rtx lowpart, highpart;
8036 enum machine_mode mode;
8038 mode = GET_MODE (val);
8040 if (mode == VOIDmode)
8041 mode = DImode;
8043 gcc_assert (GET_MODE_SIZE (mode) == 8);
8045 lowpart = gen_lowpart (SImode, val);
8046 highpart = gen_highpart_mode (SImode, mode, val);
8048 gcc_assert (GET_CODE (lowpart) == CONST_INT);
8049 gcc_assert (GET_CODE (highpart) == CONST_INT);
8051 return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
8052 NULL_RTX, NULL_RTX, 0, 0)
8053 + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
8054 NULL_RTX, NULL_RTX, 0, 0));
8057 /* Return true if it is worthwhile to split a 64-bit constant into two
8058 32-bit operations. This is the case if optimizing for size, or
8059 if we have load delay slots, or if one 32-bit part can be done with
8060 a single data operation. */
8061 bool
8062 arm_const_double_by_parts (rtx val)
8064 enum machine_mode mode = GET_MODE (val);
8065 rtx part;
8067 if (optimize_size || arm_ld_sched)
8068 return true;
8070 if (mode == VOIDmode)
8071 mode = DImode;
8073 part = gen_highpart_mode (SImode, mode, val);
8075 gcc_assert (GET_CODE (part) == CONST_INT);
8077 if (const_ok_for_arm (INTVAL (part))
8078 || const_ok_for_arm (~INTVAL (part)))
8079 return true;
8081 part = gen_lowpart (SImode, val);
8083 gcc_assert (GET_CODE (part) == CONST_INT);
8085 if (const_ok_for_arm (INTVAL (part))
8086 || const_ok_for_arm (~INTVAL (part)))
8087 return true;
8089 return false;
8092 /* Scan INSN and note any of its operands that need fixing.
8093 If DO_PUSHES is false we do not actually push any of the fixups
8094 needed. The function returns TRUE if any fixups were needed/pushed.
8095 This is used by arm_memory_load_p() which needs to know about loads
8096 of constants that will be converted into minipool loads. */
8097 static bool
8098 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
8100 bool result = false;
8101 int opno;
8103 extract_insn (insn);
8105 if (!constrain_operands (1))
8106 fatal_insn_not_found (insn);
8108 if (recog_data.n_alternatives == 0)
8109 return false;
8111 /* Fill in recog_op_alt with information about the constraints of
8112 this insn. */
8113 preprocess_constraints ();
8115 for (opno = 0; opno < recog_data.n_operands; opno++)
8117 /* Things we need to fix can only occur in inputs. */
8118 if (recog_data.operand_type[opno] != OP_IN)
8119 continue;
8121 /* If this alternative is a memory reference, then any mention
8122 of constants in this alternative is really to fool reload
8123 into allowing us to accept one there. We need to fix them up
8124 now so that we output the right code. */
8125 if (recog_op_alt[opno][which_alternative].memory_ok)
8127 rtx op = recog_data.operand[opno];
8129 if (CONSTANT_P (op))
8131 if (do_pushes)
8132 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
8133 recog_data.operand_mode[opno], op);
8134 result = true;
8136 else if (GET_CODE (op) == MEM
8137 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
8138 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
8140 if (do_pushes)
8142 rtx cop = avoid_constant_pool_reference (op);
8144 /* Casting the address of something to a mode narrower
8145 than a word can cause avoid_constant_pool_reference()
8146 to return the pool reference itself. That's no good to
8147 us here. Lets just hope that we can use the
8148 constant pool value directly. */
8149 if (op == cop)
8150 cop = get_pool_constant (XEXP (op, 0));
8152 push_minipool_fix (insn, address,
8153 recog_data.operand_loc[opno],
8154 recog_data.operand_mode[opno], cop);
8157 result = true;
8162 return result;
8165 /* Gcc puts the pool in the wrong place for ARM, since we can only
8166 load addresses a limited distance around the pc. We do some
8167 special munging to move the constant pool values to the correct
8168 point in the code. */
8169 static void
8170 arm_reorg (void)
8172 rtx insn;
8173 HOST_WIDE_INT address = 0;
8174 Mfix * fix;
8176 minipool_fix_head = minipool_fix_tail = NULL;
8178 /* The first insn must always be a note, or the code below won't
8179 scan it properly. */
8180 insn = get_insns ();
8181 gcc_assert (GET_CODE (insn) == NOTE);
8182 minipool_pad = 0;
8184 /* Scan all the insns and record the operands that will need fixing. */
8185 for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
8187 if (TARGET_CIRRUS_FIX_INVALID_INSNS
8188 && (arm_cirrus_insn_p (insn)
8189 || GET_CODE (insn) == JUMP_INSN
8190 || arm_memory_load_p (insn)))
8191 cirrus_reorg (insn);
8193 if (GET_CODE (insn) == BARRIER)
8194 push_minipool_barrier (insn, address);
8195 else if (INSN_P (insn))
8197 rtx table;
8199 note_invalid_constants (insn, address, true);
8200 address += get_attr_length (insn);
8202 /* If the insn is a vector jump, add the size of the table
8203 and skip the table. */
8204 if ((table = is_jump_table (insn)) != NULL)
8206 address += get_jump_table_size (table);
8207 insn = table;
8212 fix = minipool_fix_head;
8214 /* Now scan the fixups and perform the required changes. */
8215 while (fix)
8217 Mfix * ftmp;
8218 Mfix * fdel;
8219 Mfix * last_added_fix;
8220 Mfix * last_barrier = NULL;
8221 Mfix * this_fix;
8223 /* Skip any further barriers before the next fix. */
8224 while (fix && GET_CODE (fix->insn) == BARRIER)
8225 fix = fix->next;
8227 /* No more fixes. */
8228 if (fix == NULL)
8229 break;
8231 last_added_fix = NULL;
8233 for (ftmp = fix; ftmp; ftmp = ftmp->next)
8235 if (GET_CODE (ftmp->insn) == BARRIER)
8237 if (ftmp->address >= minipool_vector_head->max_address)
8238 break;
8240 last_barrier = ftmp;
8242 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
8243 break;
8245 last_added_fix = ftmp; /* Keep track of the last fix added. */
8248 /* If we found a barrier, drop back to that; any fixes that we
8249 could have reached but come after the barrier will now go in
8250 the next mini-pool. */
8251 if (last_barrier != NULL)
8253 /* Reduce the refcount for those fixes that won't go into this
8254 pool after all. */
8255 for (fdel = last_barrier->next;
8256 fdel && fdel != ftmp;
8257 fdel = fdel->next)
8259 fdel->minipool->refcount--;
8260 fdel->minipool = NULL;
8263 ftmp = last_barrier;
8265 else
8267 /* ftmp is first fix that we can't fit into this pool and
8268 there no natural barriers that we could use. Insert a
8269 new barrier in the code somewhere between the previous
8270 fix and this one, and arrange to jump around it. */
8271 HOST_WIDE_INT max_address;
8273 /* The last item on the list of fixes must be a barrier, so
8274 we can never run off the end of the list of fixes without
8275 last_barrier being set. */
8276 gcc_assert (ftmp);
8278 max_address = minipool_vector_head->max_address;
8279 /* Check that there isn't another fix that is in range that
8280 we couldn't fit into this pool because the pool was
8281 already too large: we need to put the pool before such an
8282 instruction. */
8283 if (ftmp->address < max_address)
8284 max_address = ftmp->address;
8286 last_barrier = create_fix_barrier (last_added_fix, max_address);
8289 assign_minipool_offsets (last_barrier);
8291 while (ftmp)
8293 if (GET_CODE (ftmp->insn) != BARRIER
8294 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
8295 == NULL))
8296 break;
8298 ftmp = ftmp->next;
8301 /* Scan over the fixes we have identified for this pool, fixing them
8302 up and adding the constants to the pool itself. */
8303 for (this_fix = fix; this_fix && ftmp != this_fix;
8304 this_fix = this_fix->next)
8305 if (GET_CODE (this_fix->insn) != BARRIER)
8307 rtx addr
8308 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
8309 minipool_vector_label),
8310 this_fix->minipool->offset);
8311 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
8314 dump_minipool (last_barrier->insn);
8315 fix = ftmp;
8318 /* From now on we must synthesize any constants that we can't handle
8319 directly. This can happen if the RTL gets split during final
8320 instruction generation. */
8321 after_arm_reorg = 1;
8323 /* Free the minipool memory. */
8324 obstack_free (&minipool_obstack, minipool_startobj);
8327 /* Routines to output assembly language. */
8329 /* If the rtx is the correct value then return the string of the number.
8330 In this way we can ensure that valid double constants are generated even
8331 when cross compiling. */
8332 const char *
8333 fp_immediate_constant (rtx x)
8335 REAL_VALUE_TYPE r;
8336 int i;
8338 if (!fp_consts_inited)
8339 init_fp_table ();
8341 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8342 for (i = 0; i < 8; i++)
8343 if (REAL_VALUES_EQUAL (r, values_fp[i]))
8344 return strings_fp[i];
8346 gcc_unreachable ();
8349 /* As for fp_immediate_constant, but value is passed directly, not in rtx. */
8350 static const char *
8351 fp_const_from_val (REAL_VALUE_TYPE *r)
8353 int i;
8355 if (!fp_consts_inited)
8356 init_fp_table ();
8358 for (i = 0; i < 8; i++)
8359 if (REAL_VALUES_EQUAL (*r, values_fp[i]))
8360 return strings_fp[i];
8362 gcc_unreachable ();
8365 /* Output the operands of a LDM/STM instruction to STREAM.
8366 MASK is the ARM register set mask of which only bits 0-15 are important.
8367 REG is the base register, either the frame pointer or the stack pointer,
8368 INSTR is the possibly suffixed load or store instruction. */
8370 static void
8371 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
8372 unsigned long mask)
8374 unsigned i;
8375 bool not_first = FALSE;
8377 fputc ('\t', stream);
8378 asm_fprintf (stream, instr, reg);
8379 fputs (", {", stream);
8381 for (i = 0; i <= LAST_ARM_REGNUM; i++)
8382 if (mask & (1 << i))
8384 if (not_first)
8385 fprintf (stream, ", ");
8387 asm_fprintf (stream, "%r", i);
8388 not_first = TRUE;
8391 fprintf (stream, "}\n");
8395 /* Output a FLDMX instruction to STREAM.
8396 BASE if the register containing the address.
8397 REG and COUNT specify the register range.
8398 Extra registers may be added to avoid hardware bugs. */
8400 static void
8401 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
8403 int i;
8405 /* Workaround ARM10 VFPr1 bug. */
8406 if (count == 2 && !arm_arch6)
8408 if (reg == 15)
8409 reg--;
8410 count++;
8413 fputc ('\t', stream);
8414 asm_fprintf (stream, "fldmfdx\t%r!, {", base);
8416 for (i = reg; i < reg + count; i++)
8418 if (i > reg)
8419 fputs (", ", stream);
8420 asm_fprintf (stream, "d%d", i);
8422 fputs ("}\n", stream);
8427 /* Output the assembly for a store multiple. */
8429 const char *
8430 vfp_output_fstmx (rtx * operands)
8432 char pattern[100];
8433 int p;
8434 int base;
8435 int i;
8437 strcpy (pattern, "fstmfdx\t%m0!, {%P1");
8438 p = strlen (pattern);
8440 gcc_assert (GET_CODE (operands[1]) == REG);
8442 base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
8443 for (i = 1; i < XVECLEN (operands[2], 0); i++)
8445 p += sprintf (&pattern[p], ", d%d", base + i);
8447 strcpy (&pattern[p], "}");
8449 output_asm_insn (pattern, operands);
8450 return "";
8454 /* Emit RTL to save block of VFP register pairs to the stack. Returns the
8455 number of bytes pushed. */
8457 static int
8458 vfp_emit_fstmx (int base_reg, int count)
8460 rtx par;
8461 rtx dwarf;
8462 rtx tmp, reg;
8463 int i;
8465 /* Workaround ARM10 VFPr1 bug. Data corruption can occur when exactly two
8466 register pairs are stored by a store multiple insn. We avoid this
8467 by pushing an extra pair. */
8468 if (count == 2 && !arm_arch6)
8470 if (base_reg == LAST_VFP_REGNUM - 3)
8471 base_reg -= 2;
8472 count++;
8475 /* ??? The frame layout is implementation defined. We describe
8476 standard format 1 (equivalent to a FSTMD insn and unused pad word).
8477 We really need some way of representing the whole block so that the
8478 unwinder can figure it out at runtime. */
8479 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
8480 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
8482 reg = gen_rtx_REG (DFmode, base_reg);
8483 base_reg += 2;
8485 XVECEXP (par, 0, 0)
8486 = gen_rtx_SET (VOIDmode,
8487 gen_frame_mem (BLKmode,
8488 gen_rtx_PRE_DEC (BLKmode,
8489 stack_pointer_rtx)),
8490 gen_rtx_UNSPEC (BLKmode,
8491 gen_rtvec (1, reg),
8492 UNSPEC_PUSH_MULT));
8494 tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8495 plus_constant (stack_pointer_rtx, -(count * 8 + 4)));
8496 RTX_FRAME_RELATED_P (tmp) = 1;
8497 XVECEXP (dwarf, 0, 0) = tmp;
8499 tmp = gen_rtx_SET (VOIDmode,
8500 gen_frame_mem (DFmode, stack_pointer_rtx),
8501 reg);
8502 RTX_FRAME_RELATED_P (tmp) = 1;
8503 XVECEXP (dwarf, 0, 1) = tmp;
8505 for (i = 1; i < count; i++)
8507 reg = gen_rtx_REG (DFmode, base_reg);
8508 base_reg += 2;
8509 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
8511 tmp = gen_rtx_SET (VOIDmode,
8512 gen_frame_mem (DFmode,
8513 plus_constant (stack_pointer_rtx,
8514 i * 8)),
8515 reg);
8516 RTX_FRAME_RELATED_P (tmp) = 1;
8517 XVECEXP (dwarf, 0, i + 1) = tmp;
8520 par = emit_insn (par);
8521 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8522 REG_NOTES (par));
8523 RTX_FRAME_RELATED_P (par) = 1;
8525 return count * 8 + 4;
8529 /* Output a 'call' insn. */
8530 const char *
8531 output_call (rtx *operands)
8533 gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly. */
8535 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
8536 if (REGNO (operands[0]) == LR_REGNUM)
8538 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8539 output_asm_insn ("mov%?\t%0, %|lr", operands);
8542 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8544 if (TARGET_INTERWORK || arm_arch4t)
8545 output_asm_insn ("bx%?\t%0", operands);
8546 else
8547 output_asm_insn ("mov%?\t%|pc, %0", operands);
8549 return "";
8552 /* Output a 'call' insn that is a reference in memory. */
8553 const char *
8554 output_call_mem (rtx *operands)
8556 if (TARGET_INTERWORK && !arm_arch5)
8558 output_asm_insn ("ldr%?\t%|ip, %0", operands);
8559 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8560 output_asm_insn ("bx%?\t%|ip", operands);
8562 else if (regno_use_in (LR_REGNUM, operands[0]))
8564 /* LR is used in the memory address. We load the address in the
8565 first instruction. It's safe to use IP as the target of the
8566 load since the call will kill it anyway. */
8567 output_asm_insn ("ldr%?\t%|ip, %0", operands);
8568 if (arm_arch5)
8569 output_asm_insn ("blx%?\t%|ip", operands);
8570 else
8572 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8573 if (arm_arch4t)
8574 output_asm_insn ("bx%?\t%|ip", operands);
8575 else
8576 output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8579 else
8581 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8582 output_asm_insn ("ldr%?\t%|pc, %0", operands);
8585 return "";
8589 /* Output a move from arm registers to an fpa registers.
8590 OPERANDS[0] is an fpa register.
8591 OPERANDS[1] is the first registers of an arm register pair. */
8592 const char *
8593 output_mov_long_double_fpa_from_arm (rtx *operands)
8595 int arm_reg0 = REGNO (operands[1]);
8596 rtx ops[3];
8598 gcc_assert (arm_reg0 != IP_REGNUM);
8600 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8601 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8602 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8604 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
8605 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8607 return "";
8610 /* Output a move from an fpa register to arm registers.
8611 OPERANDS[0] is the first registers of an arm register pair.
8612 OPERANDS[1] is an fpa register. */
8613 const char *
8614 output_mov_long_double_arm_from_fpa (rtx *operands)
8616 int arm_reg0 = REGNO (operands[0]);
8617 rtx ops[3];
8619 gcc_assert (arm_reg0 != IP_REGNUM);
8621 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8622 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8623 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8625 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8626 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8627 return "";
8630 /* Output a move from arm registers to arm registers of a long double
8631 OPERANDS[0] is the destination.
8632 OPERANDS[1] is the source. */
8633 const char *
8634 output_mov_long_double_arm_from_arm (rtx *operands)
8636 /* We have to be careful here because the two might overlap. */
8637 int dest_start = REGNO (operands[0]);
8638 int src_start = REGNO (operands[1]);
8639 rtx ops[2];
8640 int i;
8642 if (dest_start < src_start)
8644 for (i = 0; i < 3; i++)
8646 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8647 ops[1] = gen_rtx_REG (SImode, src_start + i);
8648 output_asm_insn ("mov%?\t%0, %1", ops);
8651 else
8653 for (i = 2; i >= 0; i--)
8655 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8656 ops[1] = gen_rtx_REG (SImode, src_start + i);
8657 output_asm_insn ("mov%?\t%0, %1", ops);
8661 return "";
8665 /* Output a move from arm registers to an fpa registers.
8666 OPERANDS[0] is an fpa register.
8667 OPERANDS[1] is the first registers of an arm register pair. */
8668 const char *
8669 output_mov_double_fpa_from_arm (rtx *operands)
8671 int arm_reg0 = REGNO (operands[1]);
8672 rtx ops[2];
8674 gcc_assert (arm_reg0 != IP_REGNUM);
8676 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8677 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8678 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8679 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8680 return "";
8683 /* Output a move from an fpa register to arm registers.
8684 OPERANDS[0] is the first registers of an arm register pair.
8685 OPERANDS[1] is an fpa register. */
8686 const char *
8687 output_mov_double_arm_from_fpa (rtx *operands)
8689 int arm_reg0 = REGNO (operands[0]);
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 ("stf%?d\t%1, [%|sp, #-8]!", operands);
8697 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8698 return "";
8701 /* Output a move between double words.
8702 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8703 or MEM<-REG and all MEMs must be offsettable addresses. */
8704 const char *
8705 output_move_double (rtx *operands)
8707 enum rtx_code code0 = GET_CODE (operands[0]);
8708 enum rtx_code code1 = GET_CODE (operands[1]);
8709 rtx otherops[3];
8711 if (code0 == REG)
8713 int reg0 = REGNO (operands[0]);
8715 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8717 gcc_assert (code1 == MEM); /* Constraints should ensure this. */
8719 switch (GET_CODE (XEXP (operands[1], 0)))
8721 case REG:
8722 output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8723 break;
8725 case PRE_INC:
8726 gcc_assert (TARGET_LDRD);
8727 output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8728 break;
8730 case PRE_DEC:
8731 output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8732 break;
8734 case POST_INC:
8735 output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8736 break;
8738 case POST_DEC:
8739 gcc_assert (TARGET_LDRD);
8740 output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8741 break;
8743 case PRE_MODIFY:
8744 case POST_MODIFY:
8745 otherops[0] = operands[0];
8746 otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8747 otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8749 if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8751 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8753 /* Registers overlap so split out the increment. */
8754 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8755 output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8757 else
8758 output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8760 else
8762 /* We only allow constant increments, so this is safe. */
8763 output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8765 break;
8767 case LABEL_REF:
8768 case CONST:
8769 output_asm_insn ("adr%?\t%0, %1", operands);
8770 output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8771 break;
8773 default:
8774 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8775 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8777 otherops[0] = operands[0];
8778 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8779 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8781 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8783 if (GET_CODE (otherops[2]) == CONST_INT)
8785 switch ((int) INTVAL (otherops[2]))
8787 case -8:
8788 output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8789 return "";
8790 case -4:
8791 output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8792 return "";
8793 case 4:
8794 output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8795 return "";
8798 if (TARGET_LDRD
8799 && (GET_CODE (otherops[2]) == REG
8800 || (GET_CODE (otherops[2]) == CONST_INT
8801 && INTVAL (otherops[2]) > -256
8802 && INTVAL (otherops[2]) < 256)))
8804 if (reg_overlap_mentioned_p (otherops[0],
8805 otherops[2]))
8807 /* Swap base and index registers over to
8808 avoid a conflict. */
8809 otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8810 otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8812 /* If both registers conflict, it will usually
8813 have been fixed by a splitter. */
8814 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8816 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8817 output_asm_insn ("ldr%?d\t%0, [%1]",
8818 otherops);
8820 else
8821 output_asm_insn ("ldr%?d\t%0, [%1, %2]", otherops);
8822 return "";
8825 if (GET_CODE (otherops[2]) == CONST_INT)
8827 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8828 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8829 else
8830 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8832 else
8833 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8835 else
8836 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8838 return "ldm%?ia\t%0, %M0";
8840 else
8842 otherops[1] = adjust_address (operands[1], SImode, 4);
8843 /* Take care of overlapping base/data reg. */
8844 if (reg_mentioned_p (operands[0], operands[1]))
8846 output_asm_insn ("ldr%?\t%0, %1", otherops);
8847 output_asm_insn ("ldr%?\t%0, %1", operands);
8849 else
8851 output_asm_insn ("ldr%?\t%0, %1", operands);
8852 output_asm_insn ("ldr%?\t%0, %1", otherops);
8857 else
8859 /* Constraints should ensure this. */
8860 gcc_assert (code0 == MEM && code1 == REG);
8861 gcc_assert (REGNO (operands[1]) != IP_REGNUM);
8863 switch (GET_CODE (XEXP (operands[0], 0)))
8865 case REG:
8866 output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8867 break;
8869 case PRE_INC:
8870 gcc_assert (TARGET_LDRD);
8871 output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8872 break;
8874 case PRE_DEC:
8875 output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8876 break;
8878 case POST_INC:
8879 output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8880 break;
8882 case POST_DEC:
8883 gcc_assert (TARGET_LDRD);
8884 output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8885 break;
8887 case PRE_MODIFY:
8888 case POST_MODIFY:
8889 otherops[0] = operands[1];
8890 otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8891 otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8893 if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8894 output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8895 else
8896 output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8897 break;
8899 case PLUS:
8900 otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8901 if (GET_CODE (otherops[2]) == CONST_INT)
8903 switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8905 case -8:
8906 output_asm_insn ("stm%?db\t%m0, %M1", operands);
8907 return "";
8909 case -4:
8910 output_asm_insn ("stm%?da\t%m0, %M1", operands);
8911 return "";
8913 case 4:
8914 output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8915 return "";
8918 if (TARGET_LDRD
8919 && (GET_CODE (otherops[2]) == REG
8920 || (GET_CODE (otherops[2]) == CONST_INT
8921 && INTVAL (otherops[2]) > -256
8922 && INTVAL (otherops[2]) < 256)))
8924 otherops[0] = operands[1];
8925 otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8926 output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8927 return "";
8929 /* Fall through */
8931 default:
8932 otherops[0] = adjust_address (operands[0], SImode, 4);
8933 otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8934 output_asm_insn ("str%?\t%1, %0", operands);
8935 output_asm_insn ("str%?\t%1, %0", otherops);
8939 return "";
8942 /* Output an ADD r, s, #n where n may be too big for one instruction.
8943 If adding zero to one register, output nothing. */
8944 const char *
8945 output_add_immediate (rtx *operands)
8947 HOST_WIDE_INT n = INTVAL (operands[2]);
8949 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8951 if (n < 0)
8952 output_multi_immediate (operands,
8953 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8954 -n);
8955 else
8956 output_multi_immediate (operands,
8957 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8961 return "";
8964 /* Output a multiple immediate operation.
8965 OPERANDS is the vector of operands referred to in the output patterns.
8966 INSTR1 is the output pattern to use for the first constant.
8967 INSTR2 is the output pattern to use for subsequent constants.
8968 IMMED_OP is the index of the constant slot in OPERANDS.
8969 N is the constant value. */
8970 static const char *
8971 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8972 int immed_op, HOST_WIDE_INT n)
8974 #if HOST_BITS_PER_WIDE_INT > 32
8975 n &= 0xffffffff;
8976 #endif
8978 if (n == 0)
8980 /* Quick and easy output. */
8981 operands[immed_op] = const0_rtx;
8982 output_asm_insn (instr1, operands);
8984 else
8986 int i;
8987 const char * instr = instr1;
8989 /* Note that n is never zero here (which would give no output). */
8990 for (i = 0; i < 32; i += 2)
8992 if (n & (3 << i))
8994 operands[immed_op] = GEN_INT (n & (255 << i));
8995 output_asm_insn (instr, operands);
8996 instr = instr2;
8997 i += 6;
9002 return "";
9005 /* Return the appropriate ARM instruction for the operation code.
9006 The returned result should not be overwritten. OP is the rtx of the
9007 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
9008 was shifted. */
9009 const char *
9010 arithmetic_instr (rtx op, int shift_first_arg)
9012 switch (GET_CODE (op))
9014 case PLUS:
9015 return "add";
9017 case MINUS:
9018 return shift_first_arg ? "rsb" : "sub";
9020 case IOR:
9021 return "orr";
9023 case XOR:
9024 return "eor";
9026 case AND:
9027 return "and";
9029 default:
9030 gcc_unreachable ();
9034 /* Ensure valid constant shifts and return the appropriate shift mnemonic
9035 for the operation code. The returned result should not be overwritten.
9036 OP is the rtx code of the shift.
9037 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
9038 shift. */
9039 static const char *
9040 shift_op (rtx op, HOST_WIDE_INT *amountp)
9042 const char * mnem;
9043 enum rtx_code code = GET_CODE (op);
9045 switch (GET_CODE (XEXP (op, 1)))
9047 case REG:
9048 case SUBREG:
9049 *amountp = -1;
9050 break;
9052 case CONST_INT:
9053 *amountp = INTVAL (XEXP (op, 1));
9054 break;
9056 default:
9057 gcc_unreachable ();
9060 switch (code)
9062 case ASHIFT:
9063 mnem = "asl";
9064 break;
9066 case ASHIFTRT:
9067 mnem = "asr";
9068 break;
9070 case LSHIFTRT:
9071 mnem = "lsr";
9072 break;
9074 case ROTATE:
9075 gcc_assert (*amountp != -1);
9076 *amountp = 32 - *amountp;
9078 /* Fall through. */
9080 case ROTATERT:
9081 mnem = "ror";
9082 break;
9084 case MULT:
9085 /* We never have to worry about the amount being other than a
9086 power of 2, since this case can never be reloaded from a reg. */
9087 gcc_assert (*amountp != -1);
9088 *amountp = int_log2 (*amountp);
9089 return "asl";
9091 default:
9092 gcc_unreachable ();
9095 if (*amountp != -1)
9097 /* This is not 100% correct, but follows from the desire to merge
9098 multiplication by a power of 2 with the recognizer for a
9099 shift. >=32 is not a valid shift for "asl", so we must try and
9100 output a shift that produces the correct arithmetical result.
9101 Using lsr #32 is identical except for the fact that the carry bit
9102 is not set correctly if we set the flags; but we never use the
9103 carry bit from such an operation, so we can ignore that. */
9104 if (code == ROTATERT)
9105 /* Rotate is just modulo 32. */
9106 *amountp &= 31;
9107 else if (*amountp != (*amountp & 31))
9109 if (code == ASHIFT)
9110 mnem = "lsr";
9111 *amountp = 32;
9114 /* Shifts of 0 are no-ops. */
9115 if (*amountp == 0)
9116 return NULL;
9119 return mnem;
9122 /* Obtain the shift from the POWER of two. */
9124 static HOST_WIDE_INT
9125 int_log2 (HOST_WIDE_INT power)
9127 HOST_WIDE_INT shift = 0;
9129 while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
9131 gcc_assert (shift <= 31);
9132 shift++;
9135 return shift;
9138 /* Output a .ascii pseudo-op, keeping track of lengths. This is
9139 because /bin/as is horribly restrictive. The judgement about
9140 whether or not each character is 'printable' (and can be output as
9141 is) or not (and must be printed with an octal escape) must be made
9142 with reference to the *host* character set -- the situation is
9143 similar to that discussed in the comments above pp_c_char in
9144 c-pretty-print.c. */
9146 #define MAX_ASCII_LEN 51
9148 void
9149 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
9151 int i;
9152 int len_so_far = 0;
9154 fputs ("\t.ascii\t\"", stream);
9156 for (i = 0; i < len; i++)
9158 int c = p[i];
9160 if (len_so_far >= MAX_ASCII_LEN)
9162 fputs ("\"\n\t.ascii\t\"", stream);
9163 len_so_far = 0;
9166 if (ISPRINT (c))
9168 if (c == '\\' || c == '\"')
9170 putc ('\\', stream);
9171 len_so_far++;
9173 putc (c, stream);
9174 len_so_far++;
9176 else
9178 fprintf (stream, "\\%03o", c);
9179 len_so_far += 4;
9183 fputs ("\"\n", stream);
9186 /* Compute the register save mask for registers 0 through 12
9187 inclusive. This code is used by arm_compute_save_reg_mask. */
9189 static unsigned long
9190 arm_compute_save_reg0_reg12_mask (void)
9192 unsigned long func_type = arm_current_func_type ();
9193 unsigned long save_reg_mask = 0;
9194 unsigned int reg;
9196 if (IS_INTERRUPT (func_type))
9198 unsigned int max_reg;
9199 /* Interrupt functions must not corrupt any registers,
9200 even call clobbered ones. If this is a leaf function
9201 we can just examine the registers used by the RTL, but
9202 otherwise we have to assume that whatever function is
9203 called might clobber anything, and so we have to save
9204 all the call-clobbered registers as well. */
9205 if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
9206 /* FIQ handlers have registers r8 - r12 banked, so
9207 we only need to check r0 - r7, Normal ISRs only
9208 bank r14 and r15, so we must check up to r12.
9209 r13 is the stack pointer which is always preserved,
9210 so we do not need to consider it here. */
9211 max_reg = 7;
9212 else
9213 max_reg = 12;
9215 for (reg = 0; reg <= max_reg; reg++)
9216 if (regs_ever_live[reg]
9217 || (! current_function_is_leaf && call_used_regs [reg]))
9218 save_reg_mask |= (1 << reg);
9220 /* Also save the pic base register if necessary. */
9221 if (flag_pic
9222 && !TARGET_SINGLE_PIC_BASE
9223 && arm_pic_register != INVALID_REGNUM
9224 && current_function_uses_pic_offset_table)
9225 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9227 else
9229 /* In the normal case we only need to save those registers
9230 which are call saved and which are used by this function. */
9231 for (reg = 0; reg <= 10; reg++)
9232 if (regs_ever_live[reg] && ! call_used_regs [reg])
9233 save_reg_mask |= (1 << reg);
9235 /* Handle the frame pointer as a special case. */
9236 if (! TARGET_APCS_FRAME
9237 && ! frame_pointer_needed
9238 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
9239 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
9240 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
9242 /* If we aren't loading the PIC register,
9243 don't stack it even though it may be live. */
9244 if (flag_pic
9245 && !TARGET_SINGLE_PIC_BASE
9246 && arm_pic_register != INVALID_REGNUM
9247 && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
9248 || current_function_uses_pic_offset_table))
9249 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9252 /* Save registers so the exception handler can modify them. */
9253 if (current_function_calls_eh_return)
9255 unsigned int i;
9257 for (i = 0; ; i++)
9259 reg = EH_RETURN_DATA_REGNO (i);
9260 if (reg == INVALID_REGNUM)
9261 break;
9262 save_reg_mask |= 1 << reg;
9266 return save_reg_mask;
9269 /* Compute a bit mask of which registers need to be
9270 saved on the stack for the current function. */
9272 static unsigned long
9273 arm_compute_save_reg_mask (void)
9275 unsigned int save_reg_mask = 0;
9276 unsigned long func_type = arm_current_func_type ();
9278 if (IS_NAKED (func_type))
9279 /* This should never really happen. */
9280 return 0;
9282 /* If we are creating a stack frame, then we must save the frame pointer,
9283 IP (which will hold the old stack pointer), LR and the PC. */
9284 if (frame_pointer_needed)
9285 save_reg_mask |=
9286 (1 << ARM_HARD_FRAME_POINTER_REGNUM)
9287 | (1 << IP_REGNUM)
9288 | (1 << LR_REGNUM)
9289 | (1 << PC_REGNUM);
9291 /* Volatile functions do not return, so there
9292 is no need to save any other registers. */
9293 if (IS_VOLATILE (func_type))
9294 return save_reg_mask;
9296 save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
9298 /* Decide if we need to save the link register.
9299 Interrupt routines have their own banked link register,
9300 so they never need to save it.
9301 Otherwise if we do not use the link register we do not need to save
9302 it. If we are pushing other registers onto the stack however, we
9303 can save an instruction in the epilogue by pushing the link register
9304 now and then popping it back into the PC. This incurs extra memory
9305 accesses though, so we only do it when optimizing for size, and only
9306 if we know that we will not need a fancy return sequence. */
9307 if (regs_ever_live [LR_REGNUM]
9308 || (save_reg_mask
9309 && optimize_size
9310 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9311 && !current_function_calls_eh_return))
9312 save_reg_mask |= 1 << LR_REGNUM;
9314 if (cfun->machine->lr_save_eliminated)
9315 save_reg_mask &= ~ (1 << LR_REGNUM);
9317 if (TARGET_REALLY_IWMMXT
9318 && ((bit_count (save_reg_mask)
9319 + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
9321 unsigned int reg;
9323 /* The total number of registers that are going to be pushed
9324 onto the stack is odd. We need to ensure that the stack
9325 is 64-bit aligned before we start to save iWMMXt registers,
9326 and also before we start to create locals. (A local variable
9327 might be a double or long long which we will load/store using
9328 an iWMMXt instruction). Therefore we need to push another
9329 ARM register, so that the stack will be 64-bit aligned. We
9330 try to avoid using the arg registers (r0 -r3) as they might be
9331 used to pass values in a tail call. */
9332 for (reg = 4; reg <= 12; reg++)
9333 if ((save_reg_mask & (1 << reg)) == 0)
9334 break;
9336 if (reg <= 12)
9337 save_reg_mask |= (1 << reg);
9338 else
9340 cfun->machine->sibcall_blocked = 1;
9341 save_reg_mask |= (1 << 3);
9345 return save_reg_mask;
9349 /* Compute a bit mask of which registers need to be
9350 saved on the stack for the current function. */
9351 static unsigned long
9352 thumb_compute_save_reg_mask (void)
9354 unsigned long mask;
9355 unsigned reg;
9357 mask = 0;
9358 for (reg = 0; reg < 12; reg ++)
9359 if (regs_ever_live[reg] && !call_used_regs[reg])
9360 mask |= 1 << reg;
9362 if (flag_pic
9363 && !TARGET_SINGLE_PIC_BASE
9364 && arm_pic_register != INVALID_REGNUM
9365 && current_function_uses_pic_offset_table)
9366 mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9368 /* See if we might need r11 for calls to _interwork_r11_call_via_rN(). */
9369 if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
9370 mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
9372 /* LR will also be pushed if any lo regs are pushed. */
9373 if (mask & 0xff || thumb_force_lr_save ())
9374 mask |= (1 << LR_REGNUM);
9376 /* Make sure we have a low work register if we need one.
9377 We will need one if we are going to push a high register,
9378 but we are not currently intending to push a low register. */
9379 if ((mask & 0xff) == 0
9380 && ((mask & 0x0f00) || TARGET_BACKTRACE))
9382 /* Use thumb_find_work_register to choose which register
9383 we will use. If the register is live then we will
9384 have to push it. Use LAST_LO_REGNUM as our fallback
9385 choice for the register to select. */
9386 reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
9388 if (! call_used_regs[reg])
9389 mask |= 1 << reg;
9392 return mask;
9396 /* Return the number of bytes required to save VFP registers. */
9397 static int
9398 arm_get_vfp_saved_size (void)
9400 unsigned int regno;
9401 int count;
9402 int saved;
9404 saved = 0;
9405 /* Space for saved VFP registers. */
9406 if (TARGET_HARD_FLOAT && TARGET_VFP)
9408 count = 0;
9409 for (regno = FIRST_VFP_REGNUM;
9410 regno < LAST_VFP_REGNUM;
9411 regno += 2)
9413 if ((!regs_ever_live[regno] || call_used_regs[regno])
9414 && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
9416 if (count > 0)
9418 /* Workaround ARM10 VFPr1 bug. */
9419 if (count == 2 && !arm_arch6)
9420 count++;
9421 saved += count * 8 + 4;
9423 count = 0;
9425 else
9426 count++;
9428 if (count > 0)
9430 if (count == 2 && !arm_arch6)
9431 count++;
9432 saved += count * 8 + 4;
9435 return saved;
9439 /* Generate a function exit sequence. If REALLY_RETURN is false, then do
9440 everything bar the final return instruction. */
9441 const char *
9442 output_return_instruction (rtx operand, int really_return, int reverse)
9444 char conditional[10];
9445 char instr[100];
9446 unsigned reg;
9447 unsigned long live_regs_mask;
9448 unsigned long func_type;
9449 arm_stack_offsets *offsets;
9451 func_type = arm_current_func_type ();
9453 if (IS_NAKED (func_type))
9454 return "";
9456 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9458 /* If this function was declared non-returning, and we have
9459 found a tail call, then we have to trust that the called
9460 function won't return. */
9461 if (really_return)
9463 rtx ops[2];
9465 /* Otherwise, trap an attempted return by aborting. */
9466 ops[0] = operand;
9467 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
9468 : "abort");
9469 assemble_external_libcall (ops[1]);
9470 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9473 return "";
9476 gcc_assert (!current_function_calls_alloca || really_return);
9478 sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9480 return_used_this_function = 1;
9482 live_regs_mask = arm_compute_save_reg_mask ();
9484 if (live_regs_mask)
9486 const char * return_reg;
9488 /* If we do not have any special requirements for function exit
9489 (e.g. interworking, or ISR) then we can load the return address
9490 directly into the PC. Otherwise we must load it into LR. */
9491 if (really_return
9492 && ! TARGET_INTERWORK)
9493 return_reg = reg_names[PC_REGNUM];
9494 else
9495 return_reg = reg_names[LR_REGNUM];
9497 if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9499 /* There are three possible reasons for the IP register
9500 being saved. 1) a stack frame was created, in which case
9501 IP contains the old stack pointer, or 2) an ISR routine
9502 corrupted it, or 3) it was saved to align the stack on
9503 iWMMXt. In case 1, restore IP into SP, otherwise just
9504 restore IP. */
9505 if (frame_pointer_needed)
9507 live_regs_mask &= ~ (1 << IP_REGNUM);
9508 live_regs_mask |= (1 << SP_REGNUM);
9510 else
9511 gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
9514 /* On some ARM architectures it is faster to use LDR rather than
9515 LDM to load a single register. On other architectures, the
9516 cost is the same. In 26 bit mode, or for exception handlers,
9517 we have to use LDM to load the PC so that the CPSR is also
9518 restored. */
9519 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9520 if (live_regs_mask == (1U << reg))
9521 break;
9523 if (reg <= LAST_ARM_REGNUM
9524 && (reg != LR_REGNUM
9525 || ! really_return
9526 || ! IS_INTERRUPT (func_type)))
9528 sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
9529 (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
9531 else
9533 char *p;
9534 int first = 1;
9536 /* Generate the load multiple instruction to restore the
9537 registers. Note we can get here, even if
9538 frame_pointer_needed is true, but only if sp already
9539 points to the base of the saved core registers. */
9540 if (live_regs_mask & (1 << SP_REGNUM))
9542 unsigned HOST_WIDE_INT stack_adjust;
9544 offsets = arm_get_frame_offsets ();
9545 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9546 gcc_assert (stack_adjust == 0 || stack_adjust == 4);
9548 if (stack_adjust && arm_arch5)
9549 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9550 else
9552 /* If we can't use ldmib (SA110 bug),
9553 then try to pop r3 instead. */
9554 if (stack_adjust)
9555 live_regs_mask |= 1 << 3;
9556 sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9559 else
9560 sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9562 p = instr + strlen (instr);
9564 for (reg = 0; reg <= SP_REGNUM; reg++)
9565 if (live_regs_mask & (1 << reg))
9567 int l = strlen (reg_names[reg]);
9569 if (first)
9570 first = 0;
9571 else
9573 memcpy (p, ", ", 2);
9574 p += 2;
9577 memcpy (p, "%|", 2);
9578 memcpy (p + 2, reg_names[reg], l);
9579 p += l + 2;
9582 if (live_regs_mask & (1 << LR_REGNUM))
9584 sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9585 /* If returning from an interrupt, restore the CPSR. */
9586 if (IS_INTERRUPT (func_type))
9587 strcat (p, "^");
9589 else
9590 strcpy (p, "}");
9593 output_asm_insn (instr, & operand);
9595 /* See if we need to generate an extra instruction to
9596 perform the actual function return. */
9597 if (really_return
9598 && func_type != ARM_FT_INTERWORKED
9599 && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9601 /* The return has already been handled
9602 by loading the LR into the PC. */
9603 really_return = 0;
9607 if (really_return)
9609 switch ((int) ARM_FUNC_TYPE (func_type))
9611 case ARM_FT_ISR:
9612 case ARM_FT_FIQ:
9613 sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9614 break;
9616 case ARM_FT_INTERWORKED:
9617 sprintf (instr, "bx%s\t%%|lr", conditional);
9618 break;
9620 case ARM_FT_EXCEPTION:
9621 sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9622 break;
9624 default:
9625 /* Use bx if it's available. */
9626 if (arm_arch5 || arm_arch4t)
9627 sprintf (instr, "bx%s\t%%|lr", conditional);
9628 else
9629 sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
9630 break;
9633 output_asm_insn (instr, & operand);
9636 return "";
9639 /* Write the function name into the code section, directly preceding
9640 the function prologue.
9642 Code will be output similar to this:
9644 .ascii "arm_poke_function_name", 0
9645 .align
9647 .word 0xff000000 + (t1 - t0)
9648 arm_poke_function_name
9649 mov ip, sp
9650 stmfd sp!, {fp, ip, lr, pc}
9651 sub fp, ip, #4
9653 When performing a stack backtrace, code can inspect the value
9654 of 'pc' stored at 'fp' + 0. If the trace function then looks
9655 at location pc - 12 and the top 8 bits are set, then we know
9656 that there is a function name embedded immediately preceding this
9657 location and has length ((pc[-3]) & 0xff000000).
9659 We assume that pc is declared as a pointer to an unsigned long.
9661 It is of no benefit to output the function name if we are assembling
9662 a leaf function. These function types will not contain a stack
9663 backtrace structure, therefore it is not possible to determine the
9664 function name. */
9665 void
9666 arm_poke_function_name (FILE *stream, const char *name)
9668 unsigned long alignlength;
9669 unsigned long length;
9670 rtx x;
9672 length = strlen (name) + 1;
9673 alignlength = ROUND_UP_WORD (length);
9675 ASM_OUTPUT_ASCII (stream, name, length);
9676 ASM_OUTPUT_ALIGN (stream, 2);
9677 x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9678 assemble_aligned_integer (UNITS_PER_WORD, x);
9681 /* Place some comments into the assembler stream
9682 describing the current function. */
9683 static void
9684 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9686 unsigned long func_type;
9688 if (!TARGET_ARM)
9690 thumb_output_function_prologue (f, frame_size);
9691 return;
9694 /* Sanity check. */
9695 gcc_assert (!arm_ccfsm_state && !arm_target_insn);
9697 func_type = arm_current_func_type ();
9699 switch ((int) ARM_FUNC_TYPE (func_type))
9701 default:
9702 case ARM_FT_NORMAL:
9703 break;
9704 case ARM_FT_INTERWORKED:
9705 asm_fprintf (f, "\t%@ Function supports interworking.\n");
9706 break;
9707 case ARM_FT_ISR:
9708 asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9709 break;
9710 case ARM_FT_FIQ:
9711 asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9712 break;
9713 case ARM_FT_EXCEPTION:
9714 asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9715 break;
9718 if (IS_NAKED (func_type))
9719 asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9721 if (IS_VOLATILE (func_type))
9722 asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9724 if (IS_NESTED (func_type))
9725 asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9727 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9728 current_function_args_size,
9729 current_function_pretend_args_size, frame_size);
9731 asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9732 frame_pointer_needed,
9733 cfun->machine->uses_anonymous_args);
9735 if (cfun->machine->lr_save_eliminated)
9736 asm_fprintf (f, "\t%@ link register save eliminated.\n");
9738 if (current_function_calls_eh_return)
9739 asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9741 #ifdef AOF_ASSEMBLER
9742 if (flag_pic)
9743 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9744 #endif
9746 return_used_this_function = 0;
9749 const char *
9750 arm_output_epilogue (rtx sibling)
9752 int reg;
9753 unsigned long saved_regs_mask;
9754 unsigned long func_type;
9755 /* Floats_offset is the offset from the "virtual" frame. In an APCS
9756 frame that is $fp + 4 for a non-variadic function. */
9757 int floats_offset = 0;
9758 rtx operands[3];
9759 FILE * f = asm_out_file;
9760 unsigned int lrm_count = 0;
9761 int really_return = (sibling == NULL);
9762 int start_reg;
9763 arm_stack_offsets *offsets;
9765 /* If we have already generated the return instruction
9766 then it is futile to generate anything else. */
9767 if (use_return_insn (FALSE, sibling) && return_used_this_function)
9768 return "";
9770 func_type = arm_current_func_type ();
9772 if (IS_NAKED (func_type))
9773 /* Naked functions don't have epilogues. */
9774 return "";
9776 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9778 rtx op;
9780 /* A volatile function should never return. Call abort. */
9781 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9782 assemble_external_libcall (op);
9783 output_asm_insn ("bl\t%a0", &op);
9785 return "";
9788 /* If we are throwing an exception, then we really must be doing a
9789 return, so we can't tail-call. */
9790 gcc_assert (!current_function_calls_eh_return || really_return);
9792 offsets = arm_get_frame_offsets ();
9793 saved_regs_mask = arm_compute_save_reg_mask ();
9795 if (TARGET_IWMMXT)
9796 lrm_count = bit_count (saved_regs_mask);
9798 floats_offset = offsets->saved_args;
9799 /* Compute how far away the floats will be. */
9800 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9801 if (saved_regs_mask & (1 << reg))
9802 floats_offset += 4;
9804 if (frame_pointer_needed)
9806 /* This variable is for the Virtual Frame Pointer, not VFP regs. */
9807 int vfp_offset = offsets->frame;
9809 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9811 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9812 if (regs_ever_live[reg] && !call_used_regs[reg])
9814 floats_offset += 12;
9815 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9816 reg, FP_REGNUM, floats_offset - vfp_offset);
9819 else
9821 start_reg = LAST_FPA_REGNUM;
9823 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9825 if (regs_ever_live[reg] && !call_used_regs[reg])
9827 floats_offset += 12;
9829 /* We can't unstack more than four registers at once. */
9830 if (start_reg - reg == 3)
9832 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9833 reg, FP_REGNUM, floats_offset - vfp_offset);
9834 start_reg = reg - 1;
9837 else
9839 if (reg != start_reg)
9840 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9841 reg + 1, start_reg - reg,
9842 FP_REGNUM, floats_offset - vfp_offset);
9843 start_reg = reg - 1;
9847 /* Just in case the last register checked also needs unstacking. */
9848 if (reg != start_reg)
9849 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9850 reg + 1, start_reg - reg,
9851 FP_REGNUM, floats_offset - vfp_offset);
9854 if (TARGET_HARD_FLOAT && TARGET_VFP)
9856 int saved_size;
9858 /* The fldmx insn does not have base+offset addressing modes,
9859 so we use IP to hold the address. */
9860 saved_size = arm_get_vfp_saved_size ();
9862 if (saved_size > 0)
9864 floats_offset += saved_size;
9865 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9866 FP_REGNUM, floats_offset - vfp_offset);
9868 start_reg = FIRST_VFP_REGNUM;
9869 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9871 if ((!regs_ever_live[reg] || call_used_regs[reg])
9872 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9874 if (start_reg != reg)
9875 arm_output_fldmx (f, IP_REGNUM,
9876 (start_reg - FIRST_VFP_REGNUM) / 2,
9877 (reg - start_reg) / 2);
9878 start_reg = reg + 2;
9881 if (start_reg != reg)
9882 arm_output_fldmx (f, IP_REGNUM,
9883 (start_reg - FIRST_VFP_REGNUM) / 2,
9884 (reg - start_reg) / 2);
9887 if (TARGET_IWMMXT)
9889 /* The frame pointer is guaranteed to be non-double-word aligned.
9890 This is because it is set to (old_stack_pointer - 4) and the
9891 old_stack_pointer was double word aligned. Thus the offset to
9892 the iWMMXt registers to be loaded must also be non-double-word
9893 sized, so that the resultant address *is* double-word aligned.
9894 We can ignore floats_offset since that was already included in
9895 the live_regs_mask. */
9896 lrm_count += (lrm_count % 2 ? 2 : 1);
9898 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9899 if (regs_ever_live[reg] && !call_used_regs[reg])
9901 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9902 reg, FP_REGNUM, lrm_count * 4);
9903 lrm_count += 2;
9907 /* saved_regs_mask should contain the IP, which at the time of stack
9908 frame generation actually contains the old stack pointer. So a
9909 quick way to unwind the stack is just pop the IP register directly
9910 into the stack pointer. */
9911 gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
9912 saved_regs_mask &= ~ (1 << IP_REGNUM);
9913 saved_regs_mask |= (1 << SP_REGNUM);
9915 /* There are two registers left in saved_regs_mask - LR and PC. We
9916 only need to restore the LR register (the return address), but to
9917 save time we can load it directly into the PC, unless we need a
9918 special function exit sequence, or we are not really returning. */
9919 if (really_return
9920 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9921 && !current_function_calls_eh_return)
9922 /* Delete the LR from the register mask, so that the LR on
9923 the stack is loaded into the PC in the register mask. */
9924 saved_regs_mask &= ~ (1 << LR_REGNUM);
9925 else
9926 saved_regs_mask &= ~ (1 << PC_REGNUM);
9928 /* We must use SP as the base register, because SP is one of the
9929 registers being restored. If an interrupt or page fault
9930 happens in the ldm instruction, the SP might or might not
9931 have been restored. That would be bad, as then SP will no
9932 longer indicate the safe area of stack, and we can get stack
9933 corruption. Using SP as the base register means that it will
9934 be reset correctly to the original value, should an interrupt
9935 occur. If the stack pointer already points at the right
9936 place, then omit the subtraction. */
9937 if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9938 || current_function_calls_alloca)
9939 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9940 4 * bit_count (saved_regs_mask));
9941 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9943 if (IS_INTERRUPT (func_type))
9944 /* Interrupt handlers will have pushed the
9945 IP onto the stack, so restore it now. */
9946 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9948 else
9950 /* Restore stack pointer if necessary. */
9951 if (offsets->outgoing_args != offsets->saved_regs)
9953 operands[0] = operands[1] = stack_pointer_rtx;
9954 operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9955 output_add_immediate (operands);
9958 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9960 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9961 if (regs_ever_live[reg] && !call_used_regs[reg])
9962 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9963 reg, SP_REGNUM);
9965 else
9967 start_reg = FIRST_FPA_REGNUM;
9969 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9971 if (regs_ever_live[reg] && !call_used_regs[reg])
9973 if (reg - start_reg == 3)
9975 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9976 start_reg, SP_REGNUM);
9977 start_reg = reg + 1;
9980 else
9982 if (reg != start_reg)
9983 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9984 start_reg, reg - start_reg,
9985 SP_REGNUM);
9987 start_reg = reg + 1;
9991 /* Just in case the last register checked also needs unstacking. */
9992 if (reg != start_reg)
9993 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9994 start_reg, reg - start_reg, SP_REGNUM);
9997 if (TARGET_HARD_FLOAT && TARGET_VFP)
9999 start_reg = FIRST_VFP_REGNUM;
10000 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10002 if ((!regs_ever_live[reg] || call_used_regs[reg])
10003 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10005 if (start_reg != reg)
10006 arm_output_fldmx (f, SP_REGNUM,
10007 (start_reg - FIRST_VFP_REGNUM) / 2,
10008 (reg - start_reg) / 2);
10009 start_reg = reg + 2;
10012 if (start_reg != reg)
10013 arm_output_fldmx (f, SP_REGNUM,
10014 (start_reg - FIRST_VFP_REGNUM) / 2,
10015 (reg - start_reg) / 2);
10017 if (TARGET_IWMMXT)
10018 for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
10019 if (regs_ever_live[reg] && !call_used_regs[reg])
10020 asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
10022 /* If we can, restore the LR into the PC. */
10023 if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
10024 && really_return
10025 && current_function_pretend_args_size == 0
10026 && saved_regs_mask & (1 << LR_REGNUM)
10027 && !current_function_calls_eh_return)
10029 saved_regs_mask &= ~ (1 << LR_REGNUM);
10030 saved_regs_mask |= (1 << PC_REGNUM);
10033 /* Load the registers off the stack. If we only have one register
10034 to load use the LDR instruction - it is faster. */
10035 if (saved_regs_mask == (1 << LR_REGNUM))
10037 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
10039 else if (saved_regs_mask)
10041 if (saved_regs_mask & (1 << SP_REGNUM))
10042 /* Note - write back to the stack register is not enabled
10043 (i.e. "ldmfd sp!..."). We know that the stack pointer is
10044 in the list of registers and if we add writeback the
10045 instruction becomes UNPREDICTABLE. */
10046 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
10047 else
10048 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
10051 if (current_function_pretend_args_size)
10053 /* Unwind the pre-pushed regs. */
10054 operands[0] = operands[1] = stack_pointer_rtx;
10055 operands[2] = GEN_INT (current_function_pretend_args_size);
10056 output_add_immediate (operands);
10060 /* We may have already restored PC directly from the stack. */
10061 if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
10062 return "";
10064 /* Stack adjustment for exception handler. */
10065 if (current_function_calls_eh_return)
10066 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
10067 ARM_EH_STACKADJ_REGNUM);
10069 /* Generate the return instruction. */
10070 switch ((int) ARM_FUNC_TYPE (func_type))
10072 case ARM_FT_ISR:
10073 case ARM_FT_FIQ:
10074 asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
10075 break;
10077 case ARM_FT_EXCEPTION:
10078 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10079 break;
10081 case ARM_FT_INTERWORKED:
10082 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10083 break;
10085 default:
10086 if (arm_arch5 || arm_arch4t)
10087 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10088 else
10089 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10090 break;
10093 return "";
10096 static void
10097 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10098 HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
10100 arm_stack_offsets *offsets;
10102 if (TARGET_THUMB)
10104 int regno;
10106 /* Emit any call-via-reg trampolines that are needed for v4t support
10107 of call_reg and call_value_reg type insns. */
10108 for (regno = 0; regno < LR_REGNUM; regno++)
10110 rtx label = cfun->machine->call_via[regno];
10112 if (label != NULL)
10114 switch_to_section (function_section (current_function_decl));
10115 targetm.asm_out.internal_label (asm_out_file, "L",
10116 CODE_LABEL_NUMBER (label));
10117 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
10121 /* ??? Probably not safe to set this here, since it assumes that a
10122 function will be emitted as assembly immediately after we generate
10123 RTL for it. This does not happen for inline functions. */
10124 return_used_this_function = 0;
10126 else
10128 /* We need to take into account any stack-frame rounding. */
10129 offsets = arm_get_frame_offsets ();
10131 gcc_assert (!use_return_insn (FALSE, NULL)
10132 || !return_used_this_function
10133 || offsets->saved_regs == offsets->outgoing_args
10134 || frame_pointer_needed);
10136 /* Reset the ARM-specific per-function variables. */
10137 after_arm_reorg = 0;
10141 /* Generate and emit an insn that we will recognize as a push_multi.
10142 Unfortunately, since this insn does not reflect very well the actual
10143 semantics of the operation, we need to annotate the insn for the benefit
10144 of DWARF2 frame unwind information. */
10145 static rtx
10146 emit_multi_reg_push (unsigned long mask)
10148 int num_regs = 0;
10149 int num_dwarf_regs;
10150 int i, j;
10151 rtx par;
10152 rtx dwarf;
10153 int dwarf_par_index;
10154 rtx tmp, reg;
10156 for (i = 0; i <= LAST_ARM_REGNUM; i++)
10157 if (mask & (1 << i))
10158 num_regs++;
10160 gcc_assert (num_regs && num_regs <= 16);
10162 /* We don't record the PC in the dwarf frame information. */
10163 num_dwarf_regs = num_regs;
10164 if (mask & (1 << PC_REGNUM))
10165 num_dwarf_regs--;
10167 /* For the body of the insn we are going to generate an UNSPEC in
10168 parallel with several USEs. This allows the insn to be recognized
10169 by the push_multi pattern in the arm.md file. The insn looks
10170 something like this:
10172 (parallel [
10173 (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
10174 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
10175 (use (reg:SI 11 fp))
10176 (use (reg:SI 12 ip))
10177 (use (reg:SI 14 lr))
10178 (use (reg:SI 15 pc))
10181 For the frame note however, we try to be more explicit and actually
10182 show each register being stored into the stack frame, plus a (single)
10183 decrement of the stack pointer. We do it this way in order to be
10184 friendly to the stack unwinding code, which only wants to see a single
10185 stack decrement per instruction. The RTL we generate for the note looks
10186 something like this:
10188 (sequence [
10189 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
10190 (set (mem:SI (reg:SI sp)) (reg:SI r4))
10191 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
10192 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
10193 (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
10196 This sequence is used both by the code to support stack unwinding for
10197 exceptions handlers and the code to generate dwarf2 frame debugging. */
10199 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
10200 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
10201 dwarf_par_index = 1;
10203 for (i = 0; i <= LAST_ARM_REGNUM; i++)
10205 if (mask & (1 << i))
10207 reg = gen_rtx_REG (SImode, i);
10209 XVECEXP (par, 0, 0)
10210 = gen_rtx_SET (VOIDmode,
10211 gen_frame_mem (BLKmode,
10212 gen_rtx_PRE_DEC (BLKmode,
10213 stack_pointer_rtx)),
10214 gen_rtx_UNSPEC (BLKmode,
10215 gen_rtvec (1, reg),
10216 UNSPEC_PUSH_MULT));
10218 if (i != PC_REGNUM)
10220 tmp = gen_rtx_SET (VOIDmode,
10221 gen_frame_mem (SImode, stack_pointer_rtx),
10222 reg);
10223 RTX_FRAME_RELATED_P (tmp) = 1;
10224 XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
10225 dwarf_par_index++;
10228 break;
10232 for (j = 1, i++; j < num_regs; i++)
10234 if (mask & (1 << i))
10236 reg = gen_rtx_REG (SImode, i);
10238 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
10240 if (i != PC_REGNUM)
10243 = gen_rtx_SET (VOIDmode,
10244 gen_frame_mem (SImode,
10245 plus_constant (stack_pointer_rtx,
10246 4 * j)),
10247 reg);
10248 RTX_FRAME_RELATED_P (tmp) = 1;
10249 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
10252 j++;
10256 par = emit_insn (par);
10258 tmp = gen_rtx_SET (VOIDmode,
10259 stack_pointer_rtx,
10260 plus_constant (stack_pointer_rtx, -4 * num_regs));
10261 RTX_FRAME_RELATED_P (tmp) = 1;
10262 XVECEXP (dwarf, 0, 0) = tmp;
10264 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
10265 REG_NOTES (par));
10266 return par;
10269 /* Calculate the size of the return value that is passed in registers. */
10270 static int
10271 arm_size_return_regs (void)
10273 enum machine_mode mode;
10275 if (current_function_return_rtx != 0)
10276 mode = GET_MODE (current_function_return_rtx);
10277 else
10278 mode = DECL_MODE (DECL_RESULT (current_function_decl));
10280 return GET_MODE_SIZE (mode);
10283 static rtx
10284 emit_sfm (int base_reg, int count)
10286 rtx par;
10287 rtx dwarf;
10288 rtx tmp, reg;
10289 int i;
10291 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
10292 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
10294 reg = gen_rtx_REG (XFmode, base_reg++);
10296 XVECEXP (par, 0, 0)
10297 = gen_rtx_SET (VOIDmode,
10298 gen_frame_mem (BLKmode,
10299 gen_rtx_PRE_DEC (BLKmode,
10300 stack_pointer_rtx)),
10301 gen_rtx_UNSPEC (BLKmode,
10302 gen_rtvec (1, reg),
10303 UNSPEC_PUSH_MULT));
10304 tmp = gen_rtx_SET (VOIDmode,
10305 gen_frame_mem (XFmode, stack_pointer_rtx), reg);
10306 RTX_FRAME_RELATED_P (tmp) = 1;
10307 XVECEXP (dwarf, 0, 1) = tmp;
10309 for (i = 1; i < count; i++)
10311 reg = gen_rtx_REG (XFmode, base_reg++);
10312 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
10314 tmp = gen_rtx_SET (VOIDmode,
10315 gen_frame_mem (XFmode,
10316 plus_constant (stack_pointer_rtx,
10317 i * 12)),
10318 reg);
10319 RTX_FRAME_RELATED_P (tmp) = 1;
10320 XVECEXP (dwarf, 0, i + 1) = tmp;
10323 tmp = gen_rtx_SET (VOIDmode,
10324 stack_pointer_rtx,
10325 plus_constant (stack_pointer_rtx, -12 * count));
10327 RTX_FRAME_RELATED_P (tmp) = 1;
10328 XVECEXP (dwarf, 0, 0) = tmp;
10330 par = emit_insn (par);
10331 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
10332 REG_NOTES (par));
10333 return par;
10337 /* Return true if the current function needs to save/restore LR. */
10339 static bool
10340 thumb_force_lr_save (void)
10342 return !cfun->machine->lr_save_eliminated
10343 && (!leaf_function_p ()
10344 || thumb_far_jump_used_p ()
10345 || regs_ever_live [LR_REGNUM]);
10349 /* Compute the distance from register FROM to register TO.
10350 These can be the arg pointer (26), the soft frame pointer (25),
10351 the stack pointer (13) or the hard frame pointer (11).
10352 In thumb mode r7 is used as the soft frame pointer, if needed.
10353 Typical stack layout looks like this:
10355 old stack pointer -> | |
10356 ----
10357 | | \
10358 | | saved arguments for
10359 | | vararg functions
10360 | | /
10362 hard FP & arg pointer -> | | \
10363 | | stack
10364 | | frame
10365 | | /
10367 | | \
10368 | | call saved
10369 | | registers
10370 soft frame pointer -> | | /
10372 | | \
10373 | | local
10374 | | variables
10375 locals base pointer -> | | /
10377 | | \
10378 | | outgoing
10379 | | arguments
10380 current stack pointer -> | | /
10383 For a given function some or all of these stack components
10384 may not be needed, giving rise to the possibility of
10385 eliminating some of the registers.
10387 The values returned by this function must reflect the behavior
10388 of arm_expand_prologue() and arm_compute_save_reg_mask().
10390 The sign of the number returned reflects the direction of stack
10391 growth, so the values are positive for all eliminations except
10392 from the soft frame pointer to the hard frame pointer.
10394 SFP may point just inside the local variables block to ensure correct
10395 alignment. */
10398 /* Calculate stack offsets. These are used to calculate register elimination
10399 offsets and in prologue/epilogue code. */
10401 static arm_stack_offsets *
10402 arm_get_frame_offsets (void)
10404 struct arm_stack_offsets *offsets;
10405 unsigned long func_type;
10406 int leaf;
10407 int saved;
10408 HOST_WIDE_INT frame_size;
10410 offsets = &cfun->machine->stack_offsets;
10412 /* We need to know if we are a leaf function. Unfortunately, it
10413 is possible to be called after start_sequence has been called,
10414 which causes get_insns to return the insns for the sequence,
10415 not the function, which will cause leaf_function_p to return
10416 the incorrect result.
10418 to know about leaf functions once reload has completed, and the
10419 frame size cannot be changed after that time, so we can safely
10420 use the cached value. */
10422 if (reload_completed)
10423 return offsets;
10425 /* Initially this is the size of the local variables. It will translated
10426 into an offset once we have determined the size of preceding data. */
10427 frame_size = ROUND_UP_WORD (get_frame_size ());
10429 leaf = leaf_function_p ();
10431 /* Space for variadic functions. */
10432 offsets->saved_args = current_function_pretend_args_size;
10434 offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
10436 if (TARGET_ARM)
10438 unsigned int regno;
10440 saved = bit_count (arm_compute_save_reg_mask ()) * 4;
10442 /* We know that SP will be doubleword aligned on entry, and we must
10443 preserve that condition at any subroutine call. We also require the
10444 soft frame pointer to be doubleword aligned. */
10446 if (TARGET_REALLY_IWMMXT)
10448 /* Check for the call-saved iWMMXt registers. */
10449 for (regno = FIRST_IWMMXT_REGNUM;
10450 regno <= LAST_IWMMXT_REGNUM;
10451 regno++)
10452 if (regs_ever_live [regno] && ! call_used_regs [regno])
10453 saved += 8;
10456 func_type = arm_current_func_type ();
10457 if (! IS_VOLATILE (func_type))
10459 /* Space for saved FPA registers. */
10460 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
10461 if (regs_ever_live[regno] && ! call_used_regs[regno])
10462 saved += 12;
10464 /* Space for saved VFP registers. */
10465 if (TARGET_HARD_FLOAT && TARGET_VFP)
10466 saved += arm_get_vfp_saved_size ();
10469 else /* TARGET_THUMB */
10471 saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
10472 if (TARGET_BACKTRACE)
10473 saved += 16;
10476 /* Saved registers include the stack frame. */
10477 offsets->saved_regs = offsets->saved_args + saved;
10478 offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
10479 /* A leaf function does not need any stack alignment if it has nothing
10480 on the stack. */
10481 if (leaf && frame_size == 0)
10483 offsets->outgoing_args = offsets->soft_frame;
10484 return offsets;
10487 /* Ensure SFP has the correct alignment. */
10488 if (ARM_DOUBLEWORD_ALIGN
10489 && (offsets->soft_frame & 7))
10490 offsets->soft_frame += 4;
10492 offsets->locals_base = offsets->soft_frame + frame_size;
10493 offsets->outgoing_args = (offsets->locals_base
10494 + current_function_outgoing_args_size);
10496 if (ARM_DOUBLEWORD_ALIGN)
10498 /* Ensure SP remains doubleword aligned. */
10499 if (offsets->outgoing_args & 7)
10500 offsets->outgoing_args += 4;
10501 gcc_assert (!(offsets->outgoing_args & 7));
10504 return offsets;
10508 /* Calculate the relative offsets for the different stack pointers. Positive
10509 offsets are in the direction of stack growth. */
10511 HOST_WIDE_INT
10512 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10514 arm_stack_offsets *offsets;
10516 offsets = arm_get_frame_offsets ();
10518 /* OK, now we have enough information to compute the distances.
10519 There must be an entry in these switch tables for each pair
10520 of registers in ELIMINABLE_REGS, even if some of the entries
10521 seem to be redundant or useless. */
10522 switch (from)
10524 case ARG_POINTER_REGNUM:
10525 switch (to)
10527 case THUMB_HARD_FRAME_POINTER_REGNUM:
10528 return 0;
10530 case FRAME_POINTER_REGNUM:
10531 /* This is the reverse of the soft frame pointer
10532 to hard frame pointer elimination below. */
10533 return offsets->soft_frame - offsets->saved_args;
10535 case ARM_HARD_FRAME_POINTER_REGNUM:
10536 /* If there is no stack frame then the hard
10537 frame pointer and the arg pointer coincide. */
10538 if (offsets->frame == offsets->saved_regs)
10539 return 0;
10540 /* FIXME: Not sure about this. Maybe we should always return 0 ? */
10541 return (frame_pointer_needed
10542 && cfun->static_chain_decl != NULL
10543 && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
10545 case STACK_POINTER_REGNUM:
10546 /* If nothing has been pushed on the stack at all
10547 then this will return -4. This *is* correct! */
10548 return offsets->outgoing_args - (offsets->saved_args + 4);
10550 default:
10551 gcc_unreachable ();
10553 gcc_unreachable ();
10555 case FRAME_POINTER_REGNUM:
10556 switch (to)
10558 case THUMB_HARD_FRAME_POINTER_REGNUM:
10559 return 0;
10561 case ARM_HARD_FRAME_POINTER_REGNUM:
10562 /* The hard frame pointer points to the top entry in the
10563 stack frame. The soft frame pointer to the bottom entry
10564 in the stack frame. If there is no stack frame at all,
10565 then they are identical. */
10567 return offsets->frame - offsets->soft_frame;
10569 case STACK_POINTER_REGNUM:
10570 return offsets->outgoing_args - offsets->soft_frame;
10572 default:
10573 gcc_unreachable ();
10575 gcc_unreachable ();
10577 default:
10578 /* You cannot eliminate from the stack pointer.
10579 In theory you could eliminate from the hard frame
10580 pointer to the stack pointer, but this will never
10581 happen, since if a stack frame is not needed the
10582 hard frame pointer will never be used. */
10583 gcc_unreachable ();
10588 /* Generate the prologue instructions for entry into an ARM function. */
10589 void
10590 arm_expand_prologue (void)
10592 int reg;
10593 rtx amount;
10594 rtx insn;
10595 rtx ip_rtx;
10596 unsigned long live_regs_mask;
10597 unsigned long func_type;
10598 int fp_offset = 0;
10599 int saved_pretend_args = 0;
10600 int saved_regs = 0;
10601 unsigned HOST_WIDE_INT args_to_push;
10602 arm_stack_offsets *offsets;
10604 func_type = arm_current_func_type ();
10606 /* Naked functions don't have prologues. */
10607 if (IS_NAKED (func_type))
10608 return;
10610 /* Make a copy of c_f_p_a_s as we may need to modify it locally. */
10611 args_to_push = current_function_pretend_args_size;
10613 /* Compute which register we will have to save onto the stack. */
10614 live_regs_mask = arm_compute_save_reg_mask ();
10616 ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10618 if (frame_pointer_needed)
10620 if (IS_INTERRUPT (func_type))
10622 /* Interrupt functions must not corrupt any registers.
10623 Creating a frame pointer however, corrupts the IP
10624 register, so we must push it first. */
10625 insn = emit_multi_reg_push (1 << IP_REGNUM);
10627 /* Do not set RTX_FRAME_RELATED_P on this insn.
10628 The dwarf stack unwinding code only wants to see one
10629 stack decrement per function, and this is not it. If
10630 this instruction is labeled as being part of the frame
10631 creation sequence then dwarf2out_frame_debug_expr will
10632 die when it encounters the assignment of IP to FP
10633 later on, since the use of SP here establishes SP as
10634 the CFA register and not IP.
10636 Anyway this instruction is not really part of the stack
10637 frame creation although it is part of the prologue. */
10639 else if (IS_NESTED (func_type))
10641 /* The Static chain register is the same as the IP register
10642 used as a scratch register during stack frame creation.
10643 To get around this need to find somewhere to store IP
10644 whilst the frame is being created. We try the following
10645 places in order:
10647 1. The last argument register.
10648 2. A slot on the stack above the frame. (This only
10649 works if the function is not a varargs function).
10650 3. Register r3, after pushing the argument registers
10651 onto the stack.
10653 Note - we only need to tell the dwarf2 backend about the SP
10654 adjustment in the second variant; the static chain register
10655 doesn't need to be unwound, as it doesn't contain a value
10656 inherited from the caller. */
10658 if (regs_ever_live[3] == 0)
10659 insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
10660 else if (args_to_push == 0)
10662 rtx dwarf;
10664 insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10665 insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
10666 fp_offset = 4;
10668 /* Just tell the dwarf backend that we adjusted SP. */
10669 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10670 plus_constant (stack_pointer_rtx,
10671 -fp_offset));
10672 RTX_FRAME_RELATED_P (insn) = 1;
10673 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10674 dwarf, REG_NOTES (insn));
10676 else
10678 /* Store the args on the stack. */
10679 if (cfun->machine->uses_anonymous_args)
10680 insn = emit_multi_reg_push
10681 ((0xf0 >> (args_to_push / 4)) & 0xf);
10682 else
10683 insn = emit_insn
10684 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10685 GEN_INT (- args_to_push)));
10687 RTX_FRAME_RELATED_P (insn) = 1;
10689 saved_pretend_args = 1;
10690 fp_offset = args_to_push;
10691 args_to_push = 0;
10693 /* Now reuse r3 to preserve IP. */
10694 emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
10698 insn = emit_set_insn (ip_rtx,
10699 plus_constant (stack_pointer_rtx, fp_offset));
10700 RTX_FRAME_RELATED_P (insn) = 1;
10703 if (args_to_push)
10705 /* Push the argument registers, or reserve space for them. */
10706 if (cfun->machine->uses_anonymous_args)
10707 insn = emit_multi_reg_push
10708 ((0xf0 >> (args_to_push / 4)) & 0xf);
10709 else
10710 insn = emit_insn
10711 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10712 GEN_INT (- args_to_push)));
10713 RTX_FRAME_RELATED_P (insn) = 1;
10716 /* If this is an interrupt service routine, and the link register
10717 is going to be pushed, and we are not creating a stack frame,
10718 (which would involve an extra push of IP and a pop in the epilogue)
10719 subtracting four from LR now will mean that the function return
10720 can be done with a single instruction. */
10721 if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10722 && (live_regs_mask & (1 << LR_REGNUM)) != 0
10723 && ! frame_pointer_needed)
10725 rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
10727 emit_set_insn (lr, plus_constant (lr, -4));
10730 if (live_regs_mask)
10732 insn = emit_multi_reg_push (live_regs_mask);
10733 saved_regs += bit_count (live_regs_mask) * 4;
10734 RTX_FRAME_RELATED_P (insn) = 1;
10737 if (TARGET_IWMMXT)
10738 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10739 if (regs_ever_live[reg] && ! call_used_regs [reg])
10741 insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10742 insn = gen_frame_mem (V2SImode, insn);
10743 insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
10744 RTX_FRAME_RELATED_P (insn) = 1;
10745 saved_regs += 8;
10748 if (! IS_VOLATILE (func_type))
10750 int start_reg;
10752 /* Save any floating point call-saved registers used by this
10753 function. */
10754 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10756 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10757 if (regs_ever_live[reg] && !call_used_regs[reg])
10759 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10760 insn = gen_frame_mem (XFmode, insn);
10761 insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
10762 RTX_FRAME_RELATED_P (insn) = 1;
10763 saved_regs += 12;
10766 else
10768 start_reg = LAST_FPA_REGNUM;
10770 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10772 if (regs_ever_live[reg] && !call_used_regs[reg])
10774 if (start_reg - reg == 3)
10776 insn = emit_sfm (reg, 4);
10777 RTX_FRAME_RELATED_P (insn) = 1;
10778 saved_regs += 48;
10779 start_reg = reg - 1;
10782 else
10784 if (start_reg != reg)
10786 insn = emit_sfm (reg + 1, start_reg - reg);
10787 RTX_FRAME_RELATED_P (insn) = 1;
10788 saved_regs += (start_reg - reg) * 12;
10790 start_reg = reg - 1;
10794 if (start_reg != reg)
10796 insn = emit_sfm (reg + 1, start_reg - reg);
10797 saved_regs += (start_reg - reg) * 12;
10798 RTX_FRAME_RELATED_P (insn) = 1;
10801 if (TARGET_HARD_FLOAT && TARGET_VFP)
10803 start_reg = FIRST_VFP_REGNUM;
10805 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10807 if ((!regs_ever_live[reg] || call_used_regs[reg])
10808 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10810 if (start_reg != reg)
10811 saved_regs += vfp_emit_fstmx (start_reg,
10812 (reg - start_reg) / 2);
10813 start_reg = reg + 2;
10816 if (start_reg != reg)
10817 saved_regs += vfp_emit_fstmx (start_reg,
10818 (reg - start_reg) / 2);
10822 if (frame_pointer_needed)
10824 /* Create the new frame pointer. */
10825 insn = GEN_INT (-(4 + args_to_push + fp_offset));
10826 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10827 RTX_FRAME_RELATED_P (insn) = 1;
10829 if (IS_NESTED (func_type))
10831 /* Recover the static chain register. */
10832 if (regs_ever_live [3] == 0
10833 || saved_pretend_args)
10834 insn = gen_rtx_REG (SImode, 3);
10835 else /* if (current_function_pretend_args_size == 0) */
10837 insn = plus_constant (hard_frame_pointer_rtx, 4);
10838 insn = gen_frame_mem (SImode, insn);
10841 emit_set_insn (ip_rtx, insn);
10842 /* Add a USE to stop propagate_one_insn() from barfing. */
10843 emit_insn (gen_prologue_use (ip_rtx));
10847 offsets = arm_get_frame_offsets ();
10848 if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10850 /* This add can produce multiple insns for a large constant, so we
10851 need to get tricky. */
10852 rtx last = get_last_insn ();
10854 amount = GEN_INT (offsets->saved_args + saved_regs
10855 - offsets->outgoing_args);
10857 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10858 amount));
10861 last = last ? NEXT_INSN (last) : get_insns ();
10862 RTX_FRAME_RELATED_P (last) = 1;
10864 while (last != insn);
10866 /* If the frame pointer is needed, emit a special barrier that
10867 will prevent the scheduler from moving stores to the frame
10868 before the stack adjustment. */
10869 if (frame_pointer_needed)
10870 insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10871 hard_frame_pointer_rtx));
10875 if (flag_pic && arm_pic_register != INVALID_REGNUM)
10876 arm_load_pic_register (0UL);
10878 /* If we are profiling, make sure no instructions are scheduled before
10879 the call to mcount. Similarly if the user has requested no
10880 scheduling in the prolog. Similarly if we want non-call exceptions
10881 using the EABI unwinder, to prevent faulting instructions from being
10882 swapped with a stack adjustment. */
10883 if (current_function_profile || !TARGET_SCHED_PROLOG
10884 || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
10885 emit_insn (gen_blockage ());
10887 /* If the link register is being kept alive, with the return address in it,
10888 then make sure that it does not get reused by the ce2 pass. */
10889 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10891 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10892 cfun->machine->lr_save_eliminated = 1;
10896 /* If CODE is 'd', then the X is a condition operand and the instruction
10897 should only be executed if the condition is true.
10898 if CODE is 'D', then the X is a condition operand and the instruction
10899 should only be executed if the condition is false: however, if the mode
10900 of the comparison is CCFPEmode, then always execute the instruction -- we
10901 do this because in these circumstances !GE does not necessarily imply LT;
10902 in these cases the instruction pattern will take care to make sure that
10903 an instruction containing %d will follow, thereby undoing the effects of
10904 doing this instruction unconditionally.
10905 If CODE is 'N' then X is a floating point operand that must be negated
10906 before output.
10907 If CODE is 'B' then output a bitwise inverted value of X (a const int).
10908 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
10909 void
10910 arm_print_operand (FILE *stream, rtx x, int code)
10912 switch (code)
10914 case '@':
10915 fputs (ASM_COMMENT_START, stream);
10916 return;
10918 case '_':
10919 fputs (user_label_prefix, stream);
10920 return;
10922 case '|':
10923 fputs (REGISTER_PREFIX, stream);
10924 return;
10926 case '?':
10927 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10929 if (TARGET_THUMB)
10931 output_operand_lossage ("predicated Thumb instruction");
10932 break;
10934 if (current_insn_predicate != NULL)
10936 output_operand_lossage
10937 ("predicated instruction in conditional sequence");
10938 break;
10941 fputs (arm_condition_codes[arm_current_cc], stream);
10943 else if (current_insn_predicate)
10945 enum arm_cond_code code;
10947 if (TARGET_THUMB)
10949 output_operand_lossage ("predicated Thumb instruction");
10950 break;
10953 code = get_arm_condition_code (current_insn_predicate);
10954 fputs (arm_condition_codes[code], stream);
10956 return;
10958 case 'N':
10960 REAL_VALUE_TYPE r;
10961 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10962 r = REAL_VALUE_NEGATE (r);
10963 fprintf (stream, "%s", fp_const_from_val (&r));
10965 return;
10967 case 'B':
10968 if (GET_CODE (x) == CONST_INT)
10970 HOST_WIDE_INT val;
10971 val = ARM_SIGN_EXTEND (~INTVAL (x));
10972 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10974 else
10976 putc ('~', stream);
10977 output_addr_const (stream, x);
10979 return;
10981 case 'i':
10982 fprintf (stream, "%s", arithmetic_instr (x, 1));
10983 return;
10985 /* Truncate Cirrus shift counts. */
10986 case 's':
10987 if (GET_CODE (x) == CONST_INT)
10989 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10990 return;
10992 arm_print_operand (stream, x, 0);
10993 return;
10995 case 'I':
10996 fprintf (stream, "%s", arithmetic_instr (x, 0));
10997 return;
10999 case 'S':
11001 HOST_WIDE_INT val;
11002 const char * shift = shift_op (x, &val);
11004 if (shift)
11006 fprintf (stream, ", %s ", shift_op (x, &val));
11007 if (val == -1)
11008 arm_print_operand (stream, XEXP (x, 1), 0);
11009 else
11010 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
11013 return;
11015 /* An explanation of the 'Q', 'R' and 'H' register operands:
11017 In a pair of registers containing a DI or DF value the 'Q'
11018 operand returns the register number of the register containing
11019 the least significant part of the value. The 'R' operand returns
11020 the register number of the register containing the most
11021 significant part of the value.
11023 The 'H' operand returns the higher of the two register numbers.
11024 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
11025 same as the 'Q' operand, since the most significant part of the
11026 value is held in the lower number register. The reverse is true
11027 on systems where WORDS_BIG_ENDIAN is false.
11029 The purpose of these operands is to distinguish between cases
11030 where the endian-ness of the values is important (for example
11031 when they are added together), and cases where the endian-ness
11032 is irrelevant, but the order of register operations is important.
11033 For example when loading a value from memory into a register
11034 pair, the endian-ness does not matter. Provided that the value
11035 from the lower memory address is put into the lower numbered
11036 register, and the value from the higher address is put into the
11037 higher numbered register, the load will work regardless of whether
11038 the value being loaded is big-wordian or little-wordian. The
11039 order of the two register loads can matter however, if the address
11040 of the memory location is actually held in one of the registers
11041 being overwritten by the load. */
11042 case 'Q':
11043 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11045 output_operand_lossage ("invalid operand for code '%c'", code);
11046 return;
11049 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
11050 return;
11052 case 'R':
11053 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11055 output_operand_lossage ("invalid operand for code '%c'", code);
11056 return;
11059 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
11060 return;
11062 case 'H':
11063 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11065 output_operand_lossage ("invalid operand for code '%c'", code);
11066 return;
11069 asm_fprintf (stream, "%r", REGNO (x) + 1);
11070 return;
11072 case 'm':
11073 asm_fprintf (stream, "%r",
11074 GET_CODE (XEXP (x, 0)) == REG
11075 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
11076 return;
11078 case 'M':
11079 asm_fprintf (stream, "{%r-%r}",
11080 REGNO (x),
11081 REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
11082 return;
11084 case 'd':
11085 /* CONST_TRUE_RTX means always -- that's the default. */
11086 if (x == const_true_rtx)
11087 return;
11089 if (!COMPARISON_P (x))
11091 output_operand_lossage ("invalid operand for code '%c'", code);
11092 return;
11095 fputs (arm_condition_codes[get_arm_condition_code (x)],
11096 stream);
11097 return;
11099 case 'D':
11100 /* CONST_TRUE_RTX means not always -- i.e. never. We shouldn't ever
11101 want to do that. */
11102 if (x == const_true_rtx)
11104 output_operand_lossage ("instruction never exectued");
11105 return;
11107 if (!COMPARISON_P (x))
11109 output_operand_lossage ("invalid operand for code '%c'", code);
11110 return;
11113 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
11114 (get_arm_condition_code (x))],
11115 stream);
11116 return;
11118 /* Cirrus registers can be accessed in a variety of ways:
11119 single floating point (f)
11120 double floating point (d)
11121 32bit integer (fx)
11122 64bit integer (dx). */
11123 case 'W': /* Cirrus register in F mode. */
11124 case 'X': /* Cirrus register in D mode. */
11125 case 'Y': /* Cirrus register in FX mode. */
11126 case 'Z': /* Cirrus register in DX mode. */
11127 gcc_assert (GET_CODE (x) == REG
11128 && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
11130 fprintf (stream, "mv%s%s",
11131 code == 'W' ? "f"
11132 : code == 'X' ? "d"
11133 : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
11135 return;
11137 /* Print cirrus register in the mode specified by the register's mode. */
11138 case 'V':
11140 int mode = GET_MODE (x);
11142 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
11144 output_operand_lossage ("invalid operand for code '%c'", code);
11145 return;
11148 fprintf (stream, "mv%s%s",
11149 mode == DFmode ? "d"
11150 : mode == SImode ? "fx"
11151 : mode == DImode ? "dx"
11152 : "f", reg_names[REGNO (x)] + 2);
11154 return;
11157 case 'U':
11158 if (GET_CODE (x) != REG
11159 || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
11160 || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
11161 /* Bad value for wCG register number. */
11163 output_operand_lossage ("invalid operand for code '%c'", code);
11164 return;
11167 else
11168 fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
11169 return;
11171 /* Print an iWMMXt control register name. */
11172 case 'w':
11173 if (GET_CODE (x) != CONST_INT
11174 || INTVAL (x) < 0
11175 || INTVAL (x) >= 16)
11176 /* Bad value for wC register number. */
11178 output_operand_lossage ("invalid operand for code '%c'", code);
11179 return;
11182 else
11184 static const char * wc_reg_names [16] =
11186 "wCID", "wCon", "wCSSF", "wCASF",
11187 "wC4", "wC5", "wC6", "wC7",
11188 "wCGR0", "wCGR1", "wCGR2", "wCGR3",
11189 "wC12", "wC13", "wC14", "wC15"
11192 fprintf (stream, wc_reg_names [INTVAL (x)]);
11194 return;
11196 /* Print a VFP double precision register name. */
11197 case 'P':
11199 int mode = GET_MODE (x);
11200 int num;
11202 if (mode != DImode && mode != DFmode)
11204 output_operand_lossage ("invalid operand for code '%c'", code);
11205 return;
11208 if (GET_CODE (x) != REG
11209 || !IS_VFP_REGNUM (REGNO (x)))
11211 output_operand_lossage ("invalid operand for code '%c'", code);
11212 return;
11215 num = REGNO(x) - FIRST_VFP_REGNUM;
11216 if (num & 1)
11218 output_operand_lossage ("invalid operand for code '%c'", code);
11219 return;
11222 fprintf (stream, "d%d", num >> 1);
11224 return;
11226 default:
11227 if (x == 0)
11229 output_operand_lossage ("missing operand");
11230 return;
11233 switch (GET_CODE (x))
11235 case REG:
11236 asm_fprintf (stream, "%r", REGNO (x));
11237 break;
11239 case MEM:
11240 output_memory_reference_mode = GET_MODE (x);
11241 output_address (XEXP (x, 0));
11242 break;
11244 case CONST_DOUBLE:
11245 fprintf (stream, "#%s", fp_immediate_constant (x));
11246 break;
11248 default:
11249 gcc_assert (GET_CODE (x) != NEG);
11250 fputc ('#', stream);
11251 output_addr_const (stream, x);
11252 break;
11257 #ifndef AOF_ASSEMBLER
11258 /* Target hook for assembling integer objects. The ARM version needs to
11259 handle word-sized values specially. */
11260 static bool
11261 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
11263 if (size == UNITS_PER_WORD && aligned_p)
11265 fputs ("\t.word\t", asm_out_file);
11266 output_addr_const (asm_out_file, x);
11268 /* Mark symbols as position independent. We only do this in the
11269 .text segment, not in the .data segment. */
11270 if (NEED_GOT_RELOC && flag_pic && making_const_table &&
11271 (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
11273 if (GET_CODE (x) == SYMBOL_REF
11274 && (CONSTANT_POOL_ADDRESS_P (x)
11275 || SYMBOL_REF_LOCAL_P (x)))
11276 fputs ("(GOTOFF)", asm_out_file);
11277 else if (GET_CODE (x) == LABEL_REF)
11278 fputs ("(GOTOFF)", asm_out_file);
11279 else
11280 fputs ("(GOT)", asm_out_file);
11282 fputc ('\n', asm_out_file);
11283 return true;
11286 if (arm_vector_mode_supported_p (GET_MODE (x)))
11288 int i, units;
11290 gcc_assert (GET_CODE (x) == CONST_VECTOR);
11292 units = CONST_VECTOR_NUNITS (x);
11294 switch (GET_MODE (x))
11296 case V2SImode: size = 4; break;
11297 case V4HImode: size = 2; break;
11298 case V8QImode: size = 1; break;
11299 default:
11300 gcc_unreachable ();
11303 for (i = 0; i < units; i++)
11305 rtx elt;
11307 elt = CONST_VECTOR_ELT (x, i);
11308 assemble_integer
11309 (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
11312 return true;
11315 return default_assemble_integer (x, size, aligned_p);
11319 /* Add a function to the list of static constructors. */
11321 static void
11322 arm_elf_asm_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
11324 if (!TARGET_AAPCS_BASED)
11326 default_named_section_asm_out_constructor (symbol, priority);
11327 return;
11330 /* Put these in the .init_array section, using a special relocation. */
11331 switch_to_section (ctors_section);
11332 assemble_align (POINTER_SIZE);
11333 fputs ("\t.word\t", asm_out_file);
11334 output_addr_const (asm_out_file, symbol);
11335 fputs ("(target1)\n", asm_out_file);
11337 #endif
11339 /* A finite state machine takes care of noticing whether or not instructions
11340 can be conditionally executed, and thus decrease execution time and code
11341 size by deleting branch instructions. The fsm is controlled by
11342 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
11344 /* The state of the fsm controlling condition codes are:
11345 0: normal, do nothing special
11346 1: make ASM_OUTPUT_OPCODE not output this instruction
11347 2: make ASM_OUTPUT_OPCODE not output this instruction
11348 3: make instructions conditional
11349 4: make instructions conditional
11351 State transitions (state->state by whom under condition):
11352 0 -> 1 final_prescan_insn if the `target' is a label
11353 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
11354 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
11355 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
11356 3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
11357 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
11358 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
11359 (the target insn is arm_target_insn).
11361 If the jump clobbers the conditions then we use states 2 and 4.
11363 A similar thing can be done with conditional return insns.
11365 XXX In case the `target' is an unconditional branch, this conditionalising
11366 of the instructions always reduces code size, but not always execution
11367 time. But then, I want to reduce the code size to somewhere near what
11368 /bin/cc produces. */
11370 /* Returns the index of the ARM condition code string in
11371 `arm_condition_codes'. COMPARISON should be an rtx like
11372 `(eq (...) (...))'. */
11373 static enum arm_cond_code
11374 get_arm_condition_code (rtx comparison)
11376 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
11377 int code;
11378 enum rtx_code comp_code = GET_CODE (comparison);
11380 if (GET_MODE_CLASS (mode) != MODE_CC)
11381 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
11382 XEXP (comparison, 1));
11384 switch (mode)
11386 case CC_DNEmode: code = ARM_NE; goto dominance;
11387 case CC_DEQmode: code = ARM_EQ; goto dominance;
11388 case CC_DGEmode: code = ARM_GE; goto dominance;
11389 case CC_DGTmode: code = ARM_GT; goto dominance;
11390 case CC_DLEmode: code = ARM_LE; goto dominance;
11391 case CC_DLTmode: code = ARM_LT; goto dominance;
11392 case CC_DGEUmode: code = ARM_CS; goto dominance;
11393 case CC_DGTUmode: code = ARM_HI; goto dominance;
11394 case CC_DLEUmode: code = ARM_LS; goto dominance;
11395 case CC_DLTUmode: code = ARM_CC;
11397 dominance:
11398 gcc_assert (comp_code == EQ || comp_code == NE);
11400 if (comp_code == EQ)
11401 return ARM_INVERSE_CONDITION_CODE (code);
11402 return code;
11404 case CC_NOOVmode:
11405 switch (comp_code)
11407 case NE: return ARM_NE;
11408 case EQ: return ARM_EQ;
11409 case GE: return ARM_PL;
11410 case LT: return ARM_MI;
11411 default: gcc_unreachable ();
11414 case CC_Zmode:
11415 switch (comp_code)
11417 case NE: return ARM_NE;
11418 case EQ: return ARM_EQ;
11419 default: gcc_unreachable ();
11422 case CC_Nmode:
11423 switch (comp_code)
11425 case NE: return ARM_MI;
11426 case EQ: return ARM_PL;
11427 default: gcc_unreachable ();
11430 case CCFPEmode:
11431 case CCFPmode:
11432 /* These encodings assume that AC=1 in the FPA system control
11433 byte. This allows us to handle all cases except UNEQ and
11434 LTGT. */
11435 switch (comp_code)
11437 case GE: return ARM_GE;
11438 case GT: return ARM_GT;
11439 case LE: return ARM_LS;
11440 case LT: return ARM_MI;
11441 case NE: return ARM_NE;
11442 case EQ: return ARM_EQ;
11443 case ORDERED: return ARM_VC;
11444 case UNORDERED: return ARM_VS;
11445 case UNLT: return ARM_LT;
11446 case UNLE: return ARM_LE;
11447 case UNGT: return ARM_HI;
11448 case UNGE: return ARM_PL;
11449 /* UNEQ and LTGT do not have a representation. */
11450 case UNEQ: /* Fall through. */
11451 case LTGT: /* Fall through. */
11452 default: gcc_unreachable ();
11455 case CC_SWPmode:
11456 switch (comp_code)
11458 case NE: return ARM_NE;
11459 case EQ: return ARM_EQ;
11460 case GE: return ARM_LE;
11461 case GT: return ARM_LT;
11462 case LE: return ARM_GE;
11463 case LT: return ARM_GT;
11464 case GEU: return ARM_LS;
11465 case GTU: return ARM_CC;
11466 case LEU: return ARM_CS;
11467 case LTU: return ARM_HI;
11468 default: gcc_unreachable ();
11471 case CC_Cmode:
11472 switch (comp_code)
11474 case LTU: return ARM_CS;
11475 case GEU: return ARM_CC;
11476 default: gcc_unreachable ();
11479 case CCmode:
11480 switch (comp_code)
11482 case NE: return ARM_NE;
11483 case EQ: return ARM_EQ;
11484 case GE: return ARM_GE;
11485 case GT: return ARM_GT;
11486 case LE: return ARM_LE;
11487 case LT: return ARM_LT;
11488 case GEU: return ARM_CS;
11489 case GTU: return ARM_HI;
11490 case LEU: return ARM_LS;
11491 case LTU: return ARM_CC;
11492 default: gcc_unreachable ();
11495 default: gcc_unreachable ();
11499 void
11500 arm_final_prescan_insn (rtx insn)
11502 /* BODY will hold the body of INSN. */
11503 rtx body = PATTERN (insn);
11505 /* This will be 1 if trying to repeat the trick, and things need to be
11506 reversed if it appears to fail. */
11507 int reverse = 0;
11509 /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11510 taken are clobbered, even if the rtl suggests otherwise. It also
11511 means that we have to grub around within the jump expression to find
11512 out what the conditions are when the jump isn't taken. */
11513 int jump_clobbers = 0;
11515 /* If we start with a return insn, we only succeed if we find another one. */
11516 int seeking_return = 0;
11518 /* START_INSN will hold the insn from where we start looking. This is the
11519 first insn after the following code_label if REVERSE is true. */
11520 rtx start_insn = insn;
11522 /* If in state 4, check if the target branch is reached, in order to
11523 change back to state 0. */
11524 if (arm_ccfsm_state == 4)
11526 if (insn == arm_target_insn)
11528 arm_target_insn = NULL;
11529 arm_ccfsm_state = 0;
11531 return;
11534 /* If in state 3, it is possible to repeat the trick, if this insn is an
11535 unconditional branch to a label, and immediately following this branch
11536 is the previous target label which is only used once, and the label this
11537 branch jumps to is not too far off. */
11538 if (arm_ccfsm_state == 3)
11540 if (simplejump_p (insn))
11542 start_insn = next_nonnote_insn (start_insn);
11543 if (GET_CODE (start_insn) == BARRIER)
11545 /* XXX Isn't this always a barrier? */
11546 start_insn = next_nonnote_insn (start_insn);
11548 if (GET_CODE (start_insn) == CODE_LABEL
11549 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11550 && LABEL_NUSES (start_insn) == 1)
11551 reverse = TRUE;
11552 else
11553 return;
11555 else if (GET_CODE (body) == RETURN)
11557 start_insn = next_nonnote_insn (start_insn);
11558 if (GET_CODE (start_insn) == BARRIER)
11559 start_insn = next_nonnote_insn (start_insn);
11560 if (GET_CODE (start_insn) == CODE_LABEL
11561 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11562 && LABEL_NUSES (start_insn) == 1)
11564 reverse = TRUE;
11565 seeking_return = 1;
11567 else
11568 return;
11570 else
11571 return;
11574 gcc_assert (!arm_ccfsm_state || reverse);
11575 if (GET_CODE (insn) != JUMP_INSN)
11576 return;
11578 /* This jump might be paralleled with a clobber of the condition codes
11579 the jump should always come first */
11580 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11581 body = XVECEXP (body, 0, 0);
11583 if (reverse
11584 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11585 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11587 int insns_skipped;
11588 int fail = FALSE, succeed = FALSE;
11589 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
11590 int then_not_else = TRUE;
11591 rtx this_insn = start_insn, label = 0;
11593 /* If the jump cannot be done with one instruction, we cannot
11594 conditionally execute the instruction in the inverse case. */
11595 if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11597 jump_clobbers = 1;
11598 return;
11601 /* Register the insn jumped to. */
11602 if (reverse)
11604 if (!seeking_return)
11605 label = XEXP (SET_SRC (body), 0);
11607 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11608 label = XEXP (XEXP (SET_SRC (body), 1), 0);
11609 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11611 label = XEXP (XEXP (SET_SRC (body), 2), 0);
11612 then_not_else = FALSE;
11614 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11615 seeking_return = 1;
11616 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11618 seeking_return = 1;
11619 then_not_else = FALSE;
11621 else
11622 gcc_unreachable ();
11624 /* See how many insns this branch skips, and what kind of insns. If all
11625 insns are okay, and the label or unconditional branch to the same
11626 label is not too far away, succeed. */
11627 for (insns_skipped = 0;
11628 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11630 rtx scanbody;
11632 this_insn = next_nonnote_insn (this_insn);
11633 if (!this_insn)
11634 break;
11636 switch (GET_CODE (this_insn))
11638 case CODE_LABEL:
11639 /* Succeed if it is the target label, otherwise fail since
11640 control falls in from somewhere else. */
11641 if (this_insn == label)
11643 if (jump_clobbers)
11645 arm_ccfsm_state = 2;
11646 this_insn = next_nonnote_insn (this_insn);
11648 else
11649 arm_ccfsm_state = 1;
11650 succeed = TRUE;
11652 else
11653 fail = TRUE;
11654 break;
11656 case BARRIER:
11657 /* Succeed if the following insn is the target label.
11658 Otherwise fail.
11659 If return insns are used then the last insn in a function
11660 will be a barrier. */
11661 this_insn = next_nonnote_insn (this_insn);
11662 if (this_insn && this_insn == label)
11664 if (jump_clobbers)
11666 arm_ccfsm_state = 2;
11667 this_insn = next_nonnote_insn (this_insn);
11669 else
11670 arm_ccfsm_state = 1;
11671 succeed = TRUE;
11673 else
11674 fail = TRUE;
11675 break;
11677 case CALL_INSN:
11678 /* The AAPCS says that conditional calls should not be
11679 used since they make interworking inefficient (the
11680 linker can't transform BL<cond> into BLX). That's
11681 only a problem if the machine has BLX. */
11682 if (arm_arch5)
11684 fail = TRUE;
11685 break;
11688 /* Succeed if the following insn is the target label, or
11689 if the following two insns are a barrier and the
11690 target label. */
11691 this_insn = next_nonnote_insn (this_insn);
11692 if (this_insn && GET_CODE (this_insn) == BARRIER)
11693 this_insn = next_nonnote_insn (this_insn);
11695 if (this_insn && this_insn == label
11696 && insns_skipped < max_insns_skipped)
11698 if (jump_clobbers)
11700 arm_ccfsm_state = 2;
11701 this_insn = next_nonnote_insn (this_insn);
11703 else
11704 arm_ccfsm_state = 1;
11705 succeed = TRUE;
11707 else
11708 fail = TRUE;
11709 break;
11711 case JUMP_INSN:
11712 /* If this is an unconditional branch to the same label, succeed.
11713 If it is to another label, do nothing. If it is conditional,
11714 fail. */
11715 /* XXX Probably, the tests for SET and the PC are
11716 unnecessary. */
11718 scanbody = PATTERN (this_insn);
11719 if (GET_CODE (scanbody) == SET
11720 && GET_CODE (SET_DEST (scanbody)) == PC)
11722 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11723 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11725 arm_ccfsm_state = 2;
11726 succeed = TRUE;
11728 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11729 fail = TRUE;
11731 /* Fail if a conditional return is undesirable (e.g. on a
11732 StrongARM), but still allow this if optimizing for size. */
11733 else if (GET_CODE (scanbody) == RETURN
11734 && !use_return_insn (TRUE, NULL)
11735 && !optimize_size)
11736 fail = TRUE;
11737 else if (GET_CODE (scanbody) == RETURN
11738 && seeking_return)
11740 arm_ccfsm_state = 2;
11741 succeed = TRUE;
11743 else if (GET_CODE (scanbody) == PARALLEL)
11745 switch (get_attr_conds (this_insn))
11747 case CONDS_NOCOND:
11748 break;
11749 default:
11750 fail = TRUE;
11751 break;
11754 else
11755 fail = TRUE; /* Unrecognized jump (e.g. epilogue). */
11757 break;
11759 case INSN:
11760 /* Instructions using or affecting the condition codes make it
11761 fail. */
11762 scanbody = PATTERN (this_insn);
11763 if (!(GET_CODE (scanbody) == SET
11764 || GET_CODE (scanbody) == PARALLEL)
11765 || get_attr_conds (this_insn) != CONDS_NOCOND)
11766 fail = TRUE;
11768 /* A conditional cirrus instruction must be followed by
11769 a non Cirrus instruction. However, since we
11770 conditionalize instructions in this function and by
11771 the time we get here we can't add instructions
11772 (nops), because shorten_branches() has already been
11773 called, we will disable conditionalizing Cirrus
11774 instructions to be safe. */
11775 if (GET_CODE (scanbody) != USE
11776 && GET_CODE (scanbody) != CLOBBER
11777 && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11778 fail = TRUE;
11779 break;
11781 default:
11782 break;
11785 if (succeed)
11787 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11788 arm_target_label = CODE_LABEL_NUMBER (label);
11789 else
11791 gcc_assert (seeking_return || arm_ccfsm_state == 2);
11793 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11795 this_insn = next_nonnote_insn (this_insn);
11796 gcc_assert (!this_insn
11797 || (GET_CODE (this_insn) != BARRIER
11798 && GET_CODE (this_insn) != CODE_LABEL));
11800 if (!this_insn)
11802 /* Oh, dear! we ran off the end.. give up. */
11803 recog (PATTERN (insn), insn, NULL);
11804 arm_ccfsm_state = 0;
11805 arm_target_insn = NULL;
11806 return;
11808 arm_target_insn = this_insn;
11810 if (jump_clobbers)
11812 gcc_assert (!reverse);
11813 arm_current_cc =
11814 get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11815 0), 0), 1));
11816 if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11817 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11818 if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11819 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11821 else
11823 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11824 what it was. */
11825 if (!reverse)
11826 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11827 0));
11830 if (reverse || then_not_else)
11831 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11834 /* Restore recog_data (getting the attributes of other insns can
11835 destroy this array, but final.c assumes that it remains intact
11836 across this call; since the insn has been recognized already we
11837 call recog direct). */
11838 recog (PATTERN (insn), insn, NULL);
11842 /* Returns true if REGNO is a valid register
11843 for holding a quantity of type MODE. */
11845 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11847 if (GET_MODE_CLASS (mode) == MODE_CC)
11848 return (regno == CC_REGNUM
11849 || (TARGET_HARD_FLOAT && TARGET_VFP
11850 && regno == VFPCC_REGNUM));
11852 if (TARGET_THUMB)
11853 /* For the Thumb we only allow values bigger than SImode in
11854 registers 0 - 6, so that there is always a second low
11855 register available to hold the upper part of the value.
11856 We probably we ought to ensure that the register is the
11857 start of an even numbered register pair. */
11858 return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11860 if (TARGET_HARD_FLOAT && TARGET_MAVERICK
11861 && IS_CIRRUS_REGNUM (regno))
11862 /* We have outlawed SI values in Cirrus registers because they
11863 reside in the lower 32 bits, but SF values reside in the
11864 upper 32 bits. This causes gcc all sorts of grief. We can't
11865 even split the registers into pairs because Cirrus SI values
11866 get sign extended to 64bits-- aldyh. */
11867 return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11869 if (TARGET_HARD_FLOAT && TARGET_VFP
11870 && IS_VFP_REGNUM (regno))
11872 if (mode == SFmode || mode == SImode)
11873 return TRUE;
11875 /* DFmode values are only valid in even register pairs. */
11876 if (mode == DFmode)
11877 return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11878 return FALSE;
11881 if (TARGET_REALLY_IWMMXT)
11883 if (IS_IWMMXT_GR_REGNUM (regno))
11884 return mode == SImode;
11886 if (IS_IWMMXT_REGNUM (regno))
11887 return VALID_IWMMXT_REG_MODE (mode);
11890 /* We allow any value to be stored in the general registers.
11891 Restrict doubleword quantities to even register pairs so that we can
11892 use ldrd. */
11893 if (regno <= LAST_ARM_REGNUM)
11894 return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11896 if (regno == FRAME_POINTER_REGNUM
11897 || regno == ARG_POINTER_REGNUM)
11898 /* We only allow integers in the fake hard registers. */
11899 return GET_MODE_CLASS (mode) == MODE_INT;
11901 /* The only registers left are the FPA registers
11902 which we only allow to hold FP values. */
11903 return (TARGET_HARD_FLOAT && TARGET_FPA
11904 && GET_MODE_CLASS (mode) == MODE_FLOAT
11905 && regno >= FIRST_FPA_REGNUM
11906 && regno <= LAST_FPA_REGNUM);
11910 arm_regno_class (int regno)
11912 if (TARGET_THUMB)
11914 if (regno == STACK_POINTER_REGNUM)
11915 return STACK_REG;
11916 if (regno == CC_REGNUM)
11917 return CC_REG;
11918 if (regno < 8)
11919 return LO_REGS;
11920 return HI_REGS;
11923 if ( regno <= LAST_ARM_REGNUM
11924 || regno == FRAME_POINTER_REGNUM
11925 || regno == ARG_POINTER_REGNUM)
11926 return GENERAL_REGS;
11928 if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11929 return NO_REGS;
11931 if (IS_CIRRUS_REGNUM (regno))
11932 return CIRRUS_REGS;
11934 if (IS_VFP_REGNUM (regno))
11935 return VFP_REGS;
11937 if (IS_IWMMXT_REGNUM (regno))
11938 return IWMMXT_REGS;
11940 if (IS_IWMMXT_GR_REGNUM (regno))
11941 return IWMMXT_GR_REGS;
11943 return FPA_REGS;
11946 /* Handle a special case when computing the offset
11947 of an argument from the frame pointer. */
11949 arm_debugger_arg_offset (int value, rtx addr)
11951 rtx insn;
11953 /* We are only interested if dbxout_parms() failed to compute the offset. */
11954 if (value != 0)
11955 return 0;
11957 /* We can only cope with the case where the address is held in a register. */
11958 if (GET_CODE (addr) != REG)
11959 return 0;
11961 /* If we are using the frame pointer to point at the argument, then
11962 an offset of 0 is correct. */
11963 if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11964 return 0;
11966 /* If we are using the stack pointer to point at the
11967 argument, then an offset of 0 is correct. */
11968 if ((TARGET_THUMB || !frame_pointer_needed)
11969 && REGNO (addr) == SP_REGNUM)
11970 return 0;
11972 /* Oh dear. The argument is pointed to by a register rather
11973 than being held in a register, or being stored at a known
11974 offset from the frame pointer. Since GDB only understands
11975 those two kinds of argument we must translate the address
11976 held in the register into an offset from the frame pointer.
11977 We do this by searching through the insns for the function
11978 looking to see where this register gets its value. If the
11979 register is initialized from the frame pointer plus an offset
11980 then we are in luck and we can continue, otherwise we give up.
11982 This code is exercised by producing debugging information
11983 for a function with arguments like this:
11985 double func (double a, double b, int c, double d) {return d;}
11987 Without this code the stab for parameter 'd' will be set to
11988 an offset of 0 from the frame pointer, rather than 8. */
11990 /* The if() statement says:
11992 If the insn is a normal instruction
11993 and if the insn is setting the value in a register
11994 and if the register being set is the register holding the address of the argument
11995 and if the address is computing by an addition
11996 that involves adding to a register
11997 which is the frame pointer
11998 a constant integer
12000 then... */
12002 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12004 if ( GET_CODE (insn) == INSN
12005 && GET_CODE (PATTERN (insn)) == SET
12006 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
12007 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
12008 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
12009 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
12010 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
12013 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
12015 break;
12019 if (value == 0)
12021 debug_rtx (addr);
12022 warning (0, "unable to compute real location of stacked parameter");
12023 value = 8; /* XXX magic hack */
12026 return value;
12029 #define def_mbuiltin(MASK, NAME, TYPE, CODE) \
12030 do \
12032 if ((MASK) & insn_flags) \
12033 lang_hooks.builtin_function ((NAME), (TYPE), (CODE), \
12034 BUILT_IN_MD, NULL, NULL_TREE); \
12036 while (0)
12038 struct builtin_description
12040 const unsigned int mask;
12041 const enum insn_code icode;
12042 const char * const name;
12043 const enum arm_builtins code;
12044 const enum rtx_code comparison;
12045 const unsigned int flag;
12048 static const struct builtin_description bdesc_2arg[] =
12050 #define IWMMXT_BUILTIN(code, string, builtin) \
12051 { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
12052 ARM_BUILTIN_##builtin, 0, 0 },
12054 IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
12055 IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
12056 IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
12057 IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
12058 IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
12059 IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
12060 IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
12061 IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
12062 IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
12063 IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
12064 IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
12065 IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
12066 IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
12067 IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
12068 IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
12069 IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
12070 IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
12071 IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
12072 IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
12073 IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
12074 IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
12075 IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
12076 IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
12077 IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
12078 IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
12079 IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
12080 IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
12081 IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
12082 IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
12083 IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
12084 IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
12085 IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
12086 IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
12087 IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
12088 IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
12089 IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
12090 IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
12091 IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
12092 IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
12093 IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
12094 IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
12095 IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
12096 IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
12097 IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
12098 IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
12099 IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
12100 IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
12101 IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
12102 IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
12103 IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
12104 IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
12105 IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
12106 IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
12107 IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
12108 IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
12109 IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
12110 IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
12111 IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
12113 #define IWMMXT_BUILTIN2(code, builtin) \
12114 { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
12116 IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
12117 IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
12118 IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
12119 IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
12120 IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
12121 IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
12122 IWMMXT_BUILTIN2 (ashlv4hi3_di, WSLLH)
12123 IWMMXT_BUILTIN2 (ashlv4hi3, WSLLHI)
12124 IWMMXT_BUILTIN2 (ashlv2si3_di, WSLLW)
12125 IWMMXT_BUILTIN2 (ashlv2si3, WSLLWI)
12126 IWMMXT_BUILTIN2 (ashldi3_di, WSLLD)
12127 IWMMXT_BUILTIN2 (ashldi3_iwmmxt, WSLLDI)
12128 IWMMXT_BUILTIN2 (lshrv4hi3_di, WSRLH)
12129 IWMMXT_BUILTIN2 (lshrv4hi3, WSRLHI)
12130 IWMMXT_BUILTIN2 (lshrv2si3_di, WSRLW)
12131 IWMMXT_BUILTIN2 (lshrv2si3, WSRLWI)
12132 IWMMXT_BUILTIN2 (lshrdi3_di, WSRLD)
12133 IWMMXT_BUILTIN2 (lshrdi3_iwmmxt, WSRLDI)
12134 IWMMXT_BUILTIN2 (ashrv4hi3_di, WSRAH)
12135 IWMMXT_BUILTIN2 (ashrv4hi3, WSRAHI)
12136 IWMMXT_BUILTIN2 (ashrv2si3_di, WSRAW)
12137 IWMMXT_BUILTIN2 (ashrv2si3, WSRAWI)
12138 IWMMXT_BUILTIN2 (ashrdi3_di, WSRAD)
12139 IWMMXT_BUILTIN2 (ashrdi3_iwmmxt, WSRADI)
12140 IWMMXT_BUILTIN2 (rorv4hi3_di, WRORH)
12141 IWMMXT_BUILTIN2 (rorv4hi3, WRORHI)
12142 IWMMXT_BUILTIN2 (rorv2si3_di, WRORW)
12143 IWMMXT_BUILTIN2 (rorv2si3, WRORWI)
12144 IWMMXT_BUILTIN2 (rordi3_di, WRORD)
12145 IWMMXT_BUILTIN2 (rordi3, WRORDI)
12146 IWMMXT_BUILTIN2 (iwmmxt_wmacuz, WMACUZ)
12147 IWMMXT_BUILTIN2 (iwmmxt_wmacsz, WMACSZ)
12150 static const struct builtin_description bdesc_1arg[] =
12152 IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
12153 IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
12154 IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
12155 IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
12156 IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
12157 IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
12158 IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
12159 IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
12160 IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
12161 IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
12162 IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
12163 IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
12164 IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
12165 IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
12166 IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
12167 IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
12168 IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
12169 IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
12172 /* Set up all the iWMMXt builtins. This is
12173 not called if TARGET_IWMMXT is zero. */
12175 static void
12176 arm_init_iwmmxt_builtins (void)
12178 const struct builtin_description * d;
12179 size_t i;
12180 tree endlink = void_list_node;
12182 tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
12183 tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
12184 tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
12186 tree int_ftype_int
12187 = build_function_type (integer_type_node,
12188 tree_cons (NULL_TREE, integer_type_node, endlink));
12189 tree v8qi_ftype_v8qi_v8qi_int
12190 = build_function_type (V8QI_type_node,
12191 tree_cons (NULL_TREE, V8QI_type_node,
12192 tree_cons (NULL_TREE, V8QI_type_node,
12193 tree_cons (NULL_TREE,
12194 integer_type_node,
12195 endlink))));
12196 tree v4hi_ftype_v4hi_int
12197 = build_function_type (V4HI_type_node,
12198 tree_cons (NULL_TREE, V4HI_type_node,
12199 tree_cons (NULL_TREE, integer_type_node,
12200 endlink)));
12201 tree v2si_ftype_v2si_int
12202 = build_function_type (V2SI_type_node,
12203 tree_cons (NULL_TREE, V2SI_type_node,
12204 tree_cons (NULL_TREE, integer_type_node,
12205 endlink)));
12206 tree v2si_ftype_di_di
12207 = build_function_type (V2SI_type_node,
12208 tree_cons (NULL_TREE, long_long_integer_type_node,
12209 tree_cons (NULL_TREE, long_long_integer_type_node,
12210 endlink)));
12211 tree di_ftype_di_int
12212 = build_function_type (long_long_integer_type_node,
12213 tree_cons (NULL_TREE, long_long_integer_type_node,
12214 tree_cons (NULL_TREE, integer_type_node,
12215 endlink)));
12216 tree di_ftype_di_int_int
12217 = build_function_type (long_long_integer_type_node,
12218 tree_cons (NULL_TREE, long_long_integer_type_node,
12219 tree_cons (NULL_TREE, integer_type_node,
12220 tree_cons (NULL_TREE,
12221 integer_type_node,
12222 endlink))));
12223 tree int_ftype_v8qi
12224 = build_function_type (integer_type_node,
12225 tree_cons (NULL_TREE, V8QI_type_node,
12226 endlink));
12227 tree int_ftype_v4hi
12228 = build_function_type (integer_type_node,
12229 tree_cons (NULL_TREE, V4HI_type_node,
12230 endlink));
12231 tree int_ftype_v2si
12232 = build_function_type (integer_type_node,
12233 tree_cons (NULL_TREE, V2SI_type_node,
12234 endlink));
12235 tree int_ftype_v8qi_int
12236 = build_function_type (integer_type_node,
12237 tree_cons (NULL_TREE, V8QI_type_node,
12238 tree_cons (NULL_TREE, integer_type_node,
12239 endlink)));
12240 tree int_ftype_v4hi_int
12241 = build_function_type (integer_type_node,
12242 tree_cons (NULL_TREE, V4HI_type_node,
12243 tree_cons (NULL_TREE, integer_type_node,
12244 endlink)));
12245 tree int_ftype_v2si_int
12246 = build_function_type (integer_type_node,
12247 tree_cons (NULL_TREE, V2SI_type_node,
12248 tree_cons (NULL_TREE, integer_type_node,
12249 endlink)));
12250 tree v8qi_ftype_v8qi_int_int
12251 = build_function_type (V8QI_type_node,
12252 tree_cons (NULL_TREE, V8QI_type_node,
12253 tree_cons (NULL_TREE, integer_type_node,
12254 tree_cons (NULL_TREE,
12255 integer_type_node,
12256 endlink))));
12257 tree v4hi_ftype_v4hi_int_int
12258 = build_function_type (V4HI_type_node,
12259 tree_cons (NULL_TREE, V4HI_type_node,
12260 tree_cons (NULL_TREE, integer_type_node,
12261 tree_cons (NULL_TREE,
12262 integer_type_node,
12263 endlink))));
12264 tree v2si_ftype_v2si_int_int
12265 = build_function_type (V2SI_type_node,
12266 tree_cons (NULL_TREE, V2SI_type_node,
12267 tree_cons (NULL_TREE, integer_type_node,
12268 tree_cons (NULL_TREE,
12269 integer_type_node,
12270 endlink))));
12271 /* Miscellaneous. */
12272 tree v8qi_ftype_v4hi_v4hi
12273 = build_function_type (V8QI_type_node,
12274 tree_cons (NULL_TREE, V4HI_type_node,
12275 tree_cons (NULL_TREE, V4HI_type_node,
12276 endlink)));
12277 tree v4hi_ftype_v2si_v2si
12278 = build_function_type (V4HI_type_node,
12279 tree_cons (NULL_TREE, V2SI_type_node,
12280 tree_cons (NULL_TREE, V2SI_type_node,
12281 endlink)));
12282 tree v2si_ftype_v4hi_v4hi
12283 = build_function_type (V2SI_type_node,
12284 tree_cons (NULL_TREE, V4HI_type_node,
12285 tree_cons (NULL_TREE, V4HI_type_node,
12286 endlink)));
12287 tree v2si_ftype_v8qi_v8qi
12288 = build_function_type (V2SI_type_node,
12289 tree_cons (NULL_TREE, V8QI_type_node,
12290 tree_cons (NULL_TREE, V8QI_type_node,
12291 endlink)));
12292 tree v4hi_ftype_v4hi_di
12293 = build_function_type (V4HI_type_node,
12294 tree_cons (NULL_TREE, V4HI_type_node,
12295 tree_cons (NULL_TREE,
12296 long_long_integer_type_node,
12297 endlink)));
12298 tree v2si_ftype_v2si_di
12299 = build_function_type (V2SI_type_node,
12300 tree_cons (NULL_TREE, V2SI_type_node,
12301 tree_cons (NULL_TREE,
12302 long_long_integer_type_node,
12303 endlink)));
12304 tree void_ftype_int_int
12305 = build_function_type (void_type_node,
12306 tree_cons (NULL_TREE, integer_type_node,
12307 tree_cons (NULL_TREE, integer_type_node,
12308 endlink)));
12309 tree di_ftype_void
12310 = build_function_type (long_long_unsigned_type_node, endlink);
12311 tree di_ftype_v8qi
12312 = build_function_type (long_long_integer_type_node,
12313 tree_cons (NULL_TREE, V8QI_type_node,
12314 endlink));
12315 tree di_ftype_v4hi
12316 = build_function_type (long_long_integer_type_node,
12317 tree_cons (NULL_TREE, V4HI_type_node,
12318 endlink));
12319 tree di_ftype_v2si
12320 = build_function_type (long_long_integer_type_node,
12321 tree_cons (NULL_TREE, V2SI_type_node,
12322 endlink));
12323 tree v2si_ftype_v4hi
12324 = build_function_type (V2SI_type_node,
12325 tree_cons (NULL_TREE, V4HI_type_node,
12326 endlink));
12327 tree v4hi_ftype_v8qi
12328 = build_function_type (V4HI_type_node,
12329 tree_cons (NULL_TREE, V8QI_type_node,
12330 endlink));
12332 tree di_ftype_di_v4hi_v4hi
12333 = build_function_type (long_long_unsigned_type_node,
12334 tree_cons (NULL_TREE,
12335 long_long_unsigned_type_node,
12336 tree_cons (NULL_TREE, V4HI_type_node,
12337 tree_cons (NULL_TREE,
12338 V4HI_type_node,
12339 endlink))));
12341 tree di_ftype_v4hi_v4hi
12342 = build_function_type (long_long_unsigned_type_node,
12343 tree_cons (NULL_TREE, V4HI_type_node,
12344 tree_cons (NULL_TREE, V4HI_type_node,
12345 endlink)));
12347 /* Normal vector binops. */
12348 tree v8qi_ftype_v8qi_v8qi
12349 = build_function_type (V8QI_type_node,
12350 tree_cons (NULL_TREE, V8QI_type_node,
12351 tree_cons (NULL_TREE, V8QI_type_node,
12352 endlink)));
12353 tree v4hi_ftype_v4hi_v4hi
12354 = build_function_type (V4HI_type_node,
12355 tree_cons (NULL_TREE, V4HI_type_node,
12356 tree_cons (NULL_TREE, V4HI_type_node,
12357 endlink)));
12358 tree v2si_ftype_v2si_v2si
12359 = build_function_type (V2SI_type_node,
12360 tree_cons (NULL_TREE, V2SI_type_node,
12361 tree_cons (NULL_TREE, V2SI_type_node,
12362 endlink)));
12363 tree di_ftype_di_di
12364 = build_function_type (long_long_unsigned_type_node,
12365 tree_cons (NULL_TREE, long_long_unsigned_type_node,
12366 tree_cons (NULL_TREE,
12367 long_long_unsigned_type_node,
12368 endlink)));
12370 /* Add all builtins that are more or less simple operations on two
12371 operands. */
12372 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12374 /* Use one of the operands; the target can have a different mode for
12375 mask-generating compares. */
12376 enum machine_mode mode;
12377 tree type;
12379 if (d->name == 0)
12380 continue;
12382 mode = insn_data[d->icode].operand[1].mode;
12384 switch (mode)
12386 case V8QImode:
12387 type = v8qi_ftype_v8qi_v8qi;
12388 break;
12389 case V4HImode:
12390 type = v4hi_ftype_v4hi_v4hi;
12391 break;
12392 case V2SImode:
12393 type = v2si_ftype_v2si_v2si;
12394 break;
12395 case DImode:
12396 type = di_ftype_di_di;
12397 break;
12399 default:
12400 gcc_unreachable ();
12403 def_mbuiltin (d->mask, d->name, type, d->code);
12406 /* Add the remaining MMX insns with somewhat more complicated types. */
12407 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
12408 def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
12409 def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
12411 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
12412 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
12413 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
12414 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
12415 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
12416 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
12418 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
12419 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
12420 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
12421 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
12422 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
12423 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
12425 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
12426 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
12427 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
12428 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
12429 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
12430 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
12432 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
12433 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
12434 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
12435 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
12436 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
12437 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
12439 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
12441 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
12442 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
12443 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
12444 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
12446 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
12447 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
12448 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
12449 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
12450 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
12451 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
12452 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
12453 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
12454 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
12456 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
12457 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
12458 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
12460 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
12461 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
12462 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
12464 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
12465 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
12466 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
12467 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
12468 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
12469 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
12471 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
12472 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
12473 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
12474 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
12475 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
12476 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
12477 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
12478 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
12479 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
12480 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
12481 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
12482 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
12484 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
12485 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
12486 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
12487 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
12489 def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
12490 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
12491 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
12492 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
12493 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
12494 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
12495 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
12498 static void
12499 arm_init_tls_builtins (void)
12501 tree ftype;
12502 tree nothrow = tree_cons (get_identifier ("nothrow"), NULL, NULL);
12503 tree const_nothrow = tree_cons (get_identifier ("const"), NULL, nothrow);
12505 ftype = build_function_type (ptr_type_node, void_list_node);
12506 lang_hooks.builtin_function ("__builtin_thread_pointer", ftype,
12507 ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
12508 NULL, const_nothrow);
12511 static void
12512 arm_init_builtins (void)
12514 arm_init_tls_builtins ();
12516 if (TARGET_REALLY_IWMMXT)
12517 arm_init_iwmmxt_builtins ();
12520 /* Errors in the source file can cause expand_expr to return const0_rtx
12521 where we expect a vector. To avoid crashing, use one of the vector
12522 clear instructions. */
12524 static rtx
12525 safe_vector_operand (rtx x, enum machine_mode mode)
12527 if (x != const0_rtx)
12528 return x;
12529 x = gen_reg_rtx (mode);
12531 emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12532 : gen_rtx_SUBREG (DImode, x, 0)));
12533 return x;
12536 /* Subroutine of arm_expand_builtin to take care of binop insns. */
12538 static rtx
12539 arm_expand_binop_builtin (enum insn_code icode,
12540 tree arglist, rtx target)
12542 rtx pat;
12543 tree arg0 = TREE_VALUE (arglist);
12544 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12545 rtx op0 = expand_normal (arg0);
12546 rtx op1 = expand_normal (arg1);
12547 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12548 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12549 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12551 if (VECTOR_MODE_P (mode0))
12552 op0 = safe_vector_operand (op0, mode0);
12553 if (VECTOR_MODE_P (mode1))
12554 op1 = safe_vector_operand (op1, mode1);
12556 if (! target
12557 || GET_MODE (target) != tmode
12558 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12559 target = gen_reg_rtx (tmode);
12561 gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
12563 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12564 op0 = copy_to_mode_reg (mode0, op0);
12565 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12566 op1 = copy_to_mode_reg (mode1, op1);
12568 pat = GEN_FCN (icode) (target, op0, op1);
12569 if (! pat)
12570 return 0;
12571 emit_insn (pat);
12572 return target;
12575 /* Subroutine of arm_expand_builtin to take care of unop insns. */
12577 static rtx
12578 arm_expand_unop_builtin (enum insn_code icode,
12579 tree arglist, rtx target, int do_load)
12581 rtx pat;
12582 tree arg0 = TREE_VALUE (arglist);
12583 rtx op0 = expand_normal (arg0);
12584 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12585 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12587 if (! target
12588 || GET_MODE (target) != tmode
12589 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12590 target = gen_reg_rtx (tmode);
12591 if (do_load)
12592 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12593 else
12595 if (VECTOR_MODE_P (mode0))
12596 op0 = safe_vector_operand (op0, mode0);
12598 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12599 op0 = copy_to_mode_reg (mode0, op0);
12602 pat = GEN_FCN (icode) (target, op0);
12603 if (! pat)
12604 return 0;
12605 emit_insn (pat);
12606 return target;
12609 /* Expand an expression EXP that calls a built-in function,
12610 with result going to TARGET if that's convenient
12611 (and in mode MODE if that's convenient).
12612 SUBTARGET may be used as the target for computing one of EXP's operands.
12613 IGNORE is nonzero if the value is to be ignored. */
12615 static rtx
12616 arm_expand_builtin (tree exp,
12617 rtx target,
12618 rtx subtarget ATTRIBUTE_UNUSED,
12619 enum machine_mode mode ATTRIBUTE_UNUSED,
12620 int ignore ATTRIBUTE_UNUSED)
12622 const struct builtin_description * d;
12623 enum insn_code icode;
12624 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12625 tree arglist = TREE_OPERAND (exp, 1);
12626 tree arg0;
12627 tree arg1;
12628 tree arg2;
12629 rtx op0;
12630 rtx op1;
12631 rtx op2;
12632 rtx pat;
12633 int fcode = DECL_FUNCTION_CODE (fndecl);
12634 size_t i;
12635 enum machine_mode tmode;
12636 enum machine_mode mode0;
12637 enum machine_mode mode1;
12638 enum machine_mode mode2;
12640 switch (fcode)
12642 case ARM_BUILTIN_TEXTRMSB:
12643 case ARM_BUILTIN_TEXTRMUB:
12644 case ARM_BUILTIN_TEXTRMSH:
12645 case ARM_BUILTIN_TEXTRMUH:
12646 case ARM_BUILTIN_TEXTRMSW:
12647 case ARM_BUILTIN_TEXTRMUW:
12648 icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12649 : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12650 : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12651 : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12652 : CODE_FOR_iwmmxt_textrmw);
12654 arg0 = TREE_VALUE (arglist);
12655 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12656 op0 = expand_normal (arg0);
12657 op1 = expand_normal (arg1);
12658 tmode = insn_data[icode].operand[0].mode;
12659 mode0 = insn_data[icode].operand[1].mode;
12660 mode1 = insn_data[icode].operand[2].mode;
12662 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12663 op0 = copy_to_mode_reg (mode0, op0);
12664 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12666 /* @@@ better error message */
12667 error ("selector must be an immediate");
12668 return gen_reg_rtx (tmode);
12670 if (target == 0
12671 || GET_MODE (target) != tmode
12672 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12673 target = gen_reg_rtx (tmode);
12674 pat = GEN_FCN (icode) (target, op0, op1);
12675 if (! pat)
12676 return 0;
12677 emit_insn (pat);
12678 return target;
12680 case ARM_BUILTIN_TINSRB:
12681 case ARM_BUILTIN_TINSRH:
12682 case ARM_BUILTIN_TINSRW:
12683 icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12684 : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12685 : CODE_FOR_iwmmxt_tinsrw);
12686 arg0 = TREE_VALUE (arglist);
12687 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12688 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12689 op0 = expand_normal (arg0);
12690 op1 = expand_normal (arg1);
12691 op2 = expand_normal (arg2);
12692 tmode = insn_data[icode].operand[0].mode;
12693 mode0 = insn_data[icode].operand[1].mode;
12694 mode1 = insn_data[icode].operand[2].mode;
12695 mode2 = insn_data[icode].operand[3].mode;
12697 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12698 op0 = copy_to_mode_reg (mode0, op0);
12699 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12700 op1 = copy_to_mode_reg (mode1, op1);
12701 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12703 /* @@@ better error message */
12704 error ("selector must be an immediate");
12705 return const0_rtx;
12707 if (target == 0
12708 || GET_MODE (target) != tmode
12709 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12710 target = gen_reg_rtx (tmode);
12711 pat = GEN_FCN (icode) (target, op0, op1, op2);
12712 if (! pat)
12713 return 0;
12714 emit_insn (pat);
12715 return target;
12717 case ARM_BUILTIN_SETWCX:
12718 arg0 = TREE_VALUE (arglist);
12719 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12720 op0 = force_reg (SImode, expand_normal (arg0));
12721 op1 = expand_normal (arg1);
12722 emit_insn (gen_iwmmxt_tmcr (op1, op0));
12723 return 0;
12725 case ARM_BUILTIN_GETWCX:
12726 arg0 = TREE_VALUE (arglist);
12727 op0 = expand_normal (arg0);
12728 target = gen_reg_rtx (SImode);
12729 emit_insn (gen_iwmmxt_tmrc (target, op0));
12730 return target;
12732 case ARM_BUILTIN_WSHUFH:
12733 icode = CODE_FOR_iwmmxt_wshufh;
12734 arg0 = TREE_VALUE (arglist);
12735 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12736 op0 = expand_normal (arg0);
12737 op1 = expand_normal (arg1);
12738 tmode = insn_data[icode].operand[0].mode;
12739 mode1 = insn_data[icode].operand[1].mode;
12740 mode2 = insn_data[icode].operand[2].mode;
12742 if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12743 op0 = copy_to_mode_reg (mode1, op0);
12744 if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12746 /* @@@ better error message */
12747 error ("mask must be an immediate");
12748 return const0_rtx;
12750 if (target == 0
12751 || GET_MODE (target) != tmode
12752 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12753 target = gen_reg_rtx (tmode);
12754 pat = GEN_FCN (icode) (target, op0, op1);
12755 if (! pat)
12756 return 0;
12757 emit_insn (pat);
12758 return target;
12760 case ARM_BUILTIN_WSADB:
12761 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12762 case ARM_BUILTIN_WSADH:
12763 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12764 case ARM_BUILTIN_WSADBZ:
12765 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12766 case ARM_BUILTIN_WSADHZ:
12767 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12769 /* Several three-argument builtins. */
12770 case ARM_BUILTIN_WMACS:
12771 case ARM_BUILTIN_WMACU:
12772 case ARM_BUILTIN_WALIGN:
12773 case ARM_BUILTIN_TMIA:
12774 case ARM_BUILTIN_TMIAPH:
12775 case ARM_BUILTIN_TMIATT:
12776 case ARM_BUILTIN_TMIATB:
12777 case ARM_BUILTIN_TMIABT:
12778 case ARM_BUILTIN_TMIABB:
12779 icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12780 : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12781 : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12782 : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12783 : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12784 : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12785 : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12786 : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12787 : CODE_FOR_iwmmxt_walign);
12788 arg0 = TREE_VALUE (arglist);
12789 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12790 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12791 op0 = expand_normal (arg0);
12792 op1 = expand_normal (arg1);
12793 op2 = expand_normal (arg2);
12794 tmode = insn_data[icode].operand[0].mode;
12795 mode0 = insn_data[icode].operand[1].mode;
12796 mode1 = insn_data[icode].operand[2].mode;
12797 mode2 = insn_data[icode].operand[3].mode;
12799 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12800 op0 = copy_to_mode_reg (mode0, op0);
12801 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12802 op1 = copy_to_mode_reg (mode1, op1);
12803 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12804 op2 = copy_to_mode_reg (mode2, op2);
12805 if (target == 0
12806 || GET_MODE (target) != tmode
12807 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12808 target = gen_reg_rtx (tmode);
12809 pat = GEN_FCN (icode) (target, op0, op1, op2);
12810 if (! pat)
12811 return 0;
12812 emit_insn (pat);
12813 return target;
12815 case ARM_BUILTIN_WZERO:
12816 target = gen_reg_rtx (DImode);
12817 emit_insn (gen_iwmmxt_clrdi (target));
12818 return target;
12820 case ARM_BUILTIN_THREAD_POINTER:
12821 return arm_load_tp (target);
12823 default:
12824 break;
12827 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12828 if (d->code == (const enum arm_builtins) fcode)
12829 return arm_expand_binop_builtin (d->icode, arglist, target);
12831 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12832 if (d->code == (const enum arm_builtins) fcode)
12833 return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12835 /* @@@ Should really do something sensible here. */
12836 return NULL_RTX;
12839 /* Return the number (counting from 0) of
12840 the least significant set bit in MASK. */
12842 inline static int
12843 number_of_first_bit_set (unsigned mask)
12845 int bit;
12847 for (bit = 0;
12848 (mask & (1 << bit)) == 0;
12849 ++bit)
12850 continue;
12852 return bit;
12855 /* Emit code to push or pop registers to or from the stack. F is the
12856 assembly file. MASK is the registers to push or pop. PUSH is
12857 nonzero if we should push, and zero if we should pop. For debugging
12858 output, if pushing, adjust CFA_OFFSET by the amount of space added
12859 to the stack. REAL_REGS should have the same number of bits set as
12860 MASK, and will be used instead (in the same order) to describe which
12861 registers were saved - this is used to mark the save slots when we
12862 push high registers after moving them to low registers. */
12863 static void
12864 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
12865 unsigned long real_regs)
12867 int regno;
12868 int lo_mask = mask & 0xFF;
12869 int pushed_words = 0;
12871 gcc_assert (mask);
12873 if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12875 /* Special case. Do not generate a POP PC statement here, do it in
12876 thumb_exit() */
12877 thumb_exit (f, -1);
12878 return;
12881 if (ARM_EABI_UNWIND_TABLES && push)
12883 fprintf (f, "\t.save\t{");
12884 for (regno = 0; regno < 15; regno++)
12886 if (real_regs & (1 << regno))
12888 if (real_regs & ((1 << regno) -1))
12889 fprintf (f, ", ");
12890 asm_fprintf (f, "%r", regno);
12893 fprintf (f, "}\n");
12896 fprintf (f, "\t%s\t{", push ? "push" : "pop");
12898 /* Look at the low registers first. */
12899 for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12901 if (lo_mask & 1)
12903 asm_fprintf (f, "%r", regno);
12905 if ((lo_mask & ~1) != 0)
12906 fprintf (f, ", ");
12908 pushed_words++;
12912 if (push && (mask & (1 << LR_REGNUM)))
12914 /* Catch pushing the LR. */
12915 if (mask & 0xFF)
12916 fprintf (f, ", ");
12918 asm_fprintf (f, "%r", LR_REGNUM);
12920 pushed_words++;
12922 else if (!push && (mask & (1 << PC_REGNUM)))
12924 /* Catch popping the PC. */
12925 if (TARGET_INTERWORK || TARGET_BACKTRACE
12926 || current_function_calls_eh_return)
12928 /* The PC is never poped directly, instead
12929 it is popped into r3 and then BX is used. */
12930 fprintf (f, "}\n");
12932 thumb_exit (f, -1);
12934 return;
12936 else
12938 if (mask & 0xFF)
12939 fprintf (f, ", ");
12941 asm_fprintf (f, "%r", PC_REGNUM);
12945 fprintf (f, "}\n");
12947 if (push && pushed_words && dwarf2out_do_frame ())
12949 char *l = dwarf2out_cfi_label ();
12950 int pushed_mask = real_regs;
12952 *cfa_offset += pushed_words * 4;
12953 dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12955 pushed_words = 0;
12956 pushed_mask = real_regs;
12957 for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12959 if (pushed_mask & 1)
12960 dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12965 /* Generate code to return from a thumb function.
12966 If 'reg_containing_return_addr' is -1, then the return address is
12967 actually on the stack, at the stack pointer. */
12968 static void
12969 thumb_exit (FILE *f, int reg_containing_return_addr)
12971 unsigned regs_available_for_popping;
12972 unsigned regs_to_pop;
12973 int pops_needed;
12974 unsigned available;
12975 unsigned required;
12976 int mode;
12977 int size;
12978 int restore_a4 = FALSE;
12980 /* Compute the registers we need to pop. */
12981 regs_to_pop = 0;
12982 pops_needed = 0;
12984 if (reg_containing_return_addr == -1)
12986 regs_to_pop |= 1 << LR_REGNUM;
12987 ++pops_needed;
12990 if (TARGET_BACKTRACE)
12992 /* Restore the (ARM) frame pointer and stack pointer. */
12993 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12994 pops_needed += 2;
12997 /* If there is nothing to pop then just emit the BX instruction and
12998 return. */
12999 if (pops_needed == 0)
13001 if (current_function_calls_eh_return)
13002 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
13004 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
13005 return;
13007 /* Otherwise if we are not supporting interworking and we have not created
13008 a backtrace structure and the function was not entered in ARM mode then
13009 just pop the return address straight into the PC. */
13010 else if (!TARGET_INTERWORK
13011 && !TARGET_BACKTRACE
13012 && !is_called_in_ARM_mode (current_function_decl)
13013 && !current_function_calls_eh_return)
13015 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
13016 return;
13019 /* Find out how many of the (return) argument registers we can corrupt. */
13020 regs_available_for_popping = 0;
13022 /* If returning via __builtin_eh_return, the bottom three registers
13023 all contain information needed for the return. */
13024 if (current_function_calls_eh_return)
13025 size = 12;
13026 else
13028 /* If we can deduce the registers used from the function's
13029 return value. This is more reliable that examining
13030 regs_ever_live[] because that will be set if the register is
13031 ever used in the function, not just if the register is used
13032 to hold a return value. */
13034 if (current_function_return_rtx != 0)
13035 mode = GET_MODE (current_function_return_rtx);
13036 else
13037 mode = DECL_MODE (DECL_RESULT (current_function_decl));
13039 size = GET_MODE_SIZE (mode);
13041 if (size == 0)
13043 /* In a void function we can use any argument register.
13044 In a function that returns a structure on the stack
13045 we can use the second and third argument registers. */
13046 if (mode == VOIDmode)
13047 regs_available_for_popping =
13048 (1 << ARG_REGISTER (1))
13049 | (1 << ARG_REGISTER (2))
13050 | (1 << ARG_REGISTER (3));
13051 else
13052 regs_available_for_popping =
13053 (1 << ARG_REGISTER (2))
13054 | (1 << ARG_REGISTER (3));
13056 else if (size <= 4)
13057 regs_available_for_popping =
13058 (1 << ARG_REGISTER (2))
13059 | (1 << ARG_REGISTER (3));
13060 else if (size <= 8)
13061 regs_available_for_popping =
13062 (1 << ARG_REGISTER (3));
13065 /* Match registers to be popped with registers into which we pop them. */
13066 for (available = regs_available_for_popping,
13067 required = regs_to_pop;
13068 required != 0 && available != 0;
13069 available &= ~(available & - available),
13070 required &= ~(required & - required))
13071 -- pops_needed;
13073 /* If we have any popping registers left over, remove them. */
13074 if (available > 0)
13075 regs_available_for_popping &= ~available;
13077 /* Otherwise if we need another popping register we can use
13078 the fourth argument register. */
13079 else if (pops_needed)
13081 /* If we have not found any free argument registers and
13082 reg a4 contains the return address, we must move it. */
13083 if (regs_available_for_popping == 0
13084 && reg_containing_return_addr == LAST_ARG_REGNUM)
13086 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
13087 reg_containing_return_addr = LR_REGNUM;
13089 else if (size > 12)
13091 /* Register a4 is being used to hold part of the return value,
13092 but we have dire need of a free, low register. */
13093 restore_a4 = TRUE;
13095 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
13098 if (reg_containing_return_addr != LAST_ARG_REGNUM)
13100 /* The fourth argument register is available. */
13101 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
13103 --pops_needed;
13107 /* Pop as many registers as we can. */
13108 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13109 regs_available_for_popping);
13111 /* Process the registers we popped. */
13112 if (reg_containing_return_addr == -1)
13114 /* The return address was popped into the lowest numbered register. */
13115 regs_to_pop &= ~(1 << LR_REGNUM);
13117 reg_containing_return_addr =
13118 number_of_first_bit_set (regs_available_for_popping);
13120 /* Remove this register for the mask of available registers, so that
13121 the return address will not be corrupted by further pops. */
13122 regs_available_for_popping &= ~(1 << reg_containing_return_addr);
13125 /* If we popped other registers then handle them here. */
13126 if (regs_available_for_popping)
13128 int frame_pointer;
13130 /* Work out which register currently contains the frame pointer. */
13131 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
13133 /* Move it into the correct place. */
13134 asm_fprintf (f, "\tmov\t%r, %r\n",
13135 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
13137 /* (Temporarily) remove it from the mask of popped registers. */
13138 regs_available_for_popping &= ~(1 << frame_pointer);
13139 regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
13141 if (regs_available_for_popping)
13143 int stack_pointer;
13145 /* We popped the stack pointer as well,
13146 find the register that contains it. */
13147 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
13149 /* Move it into the stack register. */
13150 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
13152 /* At this point we have popped all necessary registers, so
13153 do not worry about restoring regs_available_for_popping
13154 to its correct value:
13156 assert (pops_needed == 0)
13157 assert (regs_available_for_popping == (1 << frame_pointer))
13158 assert (regs_to_pop == (1 << STACK_POINTER)) */
13160 else
13162 /* Since we have just move the popped value into the frame
13163 pointer, the popping register is available for reuse, and
13164 we know that we still have the stack pointer left to pop. */
13165 regs_available_for_popping |= (1 << frame_pointer);
13169 /* If we still have registers left on the stack, but we no longer have
13170 any registers into which we can pop them, then we must move the return
13171 address into the link register and make available the register that
13172 contained it. */
13173 if (regs_available_for_popping == 0 && pops_needed > 0)
13175 regs_available_for_popping |= 1 << reg_containing_return_addr;
13177 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
13178 reg_containing_return_addr);
13180 reg_containing_return_addr = LR_REGNUM;
13183 /* If we have registers left on the stack then pop some more.
13184 We know that at most we will want to pop FP and SP. */
13185 if (pops_needed > 0)
13187 int popped_into;
13188 int move_to;
13190 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13191 regs_available_for_popping);
13193 /* We have popped either FP or SP.
13194 Move whichever one it is into the correct register. */
13195 popped_into = number_of_first_bit_set (regs_available_for_popping);
13196 move_to = number_of_first_bit_set (regs_to_pop);
13198 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
13200 regs_to_pop &= ~(1 << move_to);
13202 --pops_needed;
13205 /* If we still have not popped everything then we must have only
13206 had one register available to us and we are now popping the SP. */
13207 if (pops_needed > 0)
13209 int popped_into;
13211 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13212 regs_available_for_popping);
13214 popped_into = number_of_first_bit_set (regs_available_for_popping);
13216 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
13218 assert (regs_to_pop == (1 << STACK_POINTER))
13219 assert (pops_needed == 1)
13223 /* If necessary restore the a4 register. */
13224 if (restore_a4)
13226 if (reg_containing_return_addr != LR_REGNUM)
13228 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
13229 reg_containing_return_addr = LR_REGNUM;
13232 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
13235 if (current_function_calls_eh_return)
13236 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
13238 /* Return to caller. */
13239 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
13243 void
13244 thumb_final_prescan_insn (rtx insn)
13246 if (flag_print_asm_name)
13247 asm_fprintf (asm_out_file, "%@ 0x%04x\n",
13248 INSN_ADDRESSES (INSN_UID (insn)));
13252 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
13254 unsigned HOST_WIDE_INT mask = 0xff;
13255 int i;
13257 if (val == 0) /* XXX */
13258 return 0;
13260 for (i = 0; i < 25; i++)
13261 if ((val & (mask << i)) == val)
13262 return 1;
13264 return 0;
13267 /* Returns nonzero if the current function contains,
13268 or might contain a far jump. */
13269 static int
13270 thumb_far_jump_used_p (void)
13272 rtx insn;
13274 /* This test is only important for leaf functions. */
13275 /* assert (!leaf_function_p ()); */
13277 /* If we have already decided that far jumps may be used,
13278 do not bother checking again, and always return true even if
13279 it turns out that they are not being used. Once we have made
13280 the decision that far jumps are present (and that hence the link
13281 register will be pushed onto the stack) we cannot go back on it. */
13282 if (cfun->machine->far_jump_used)
13283 return 1;
13285 /* If this function is not being called from the prologue/epilogue
13286 generation code then it must be being called from the
13287 INITIAL_ELIMINATION_OFFSET macro. */
13288 if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
13290 /* In this case we know that we are being asked about the elimination
13291 of the arg pointer register. If that register is not being used,
13292 then there are no arguments on the stack, and we do not have to
13293 worry that a far jump might force the prologue to push the link
13294 register, changing the stack offsets. In this case we can just
13295 return false, since the presence of far jumps in the function will
13296 not affect stack offsets.
13298 If the arg pointer is live (or if it was live, but has now been
13299 eliminated and so set to dead) then we do have to test to see if
13300 the function might contain a far jump. This test can lead to some
13301 false negatives, since before reload is completed, then length of
13302 branch instructions is not known, so gcc defaults to returning their
13303 longest length, which in turn sets the far jump attribute to true.
13305 A false negative will not result in bad code being generated, but it
13306 will result in a needless push and pop of the link register. We
13307 hope that this does not occur too often.
13309 If we need doubleword stack alignment this could affect the other
13310 elimination offsets so we can't risk getting it wrong. */
13311 if (regs_ever_live [ARG_POINTER_REGNUM])
13312 cfun->machine->arg_pointer_live = 1;
13313 else if (!cfun->machine->arg_pointer_live)
13314 return 0;
13317 /* Check to see if the function contains a branch
13318 insn with the far jump attribute set. */
13319 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
13321 if (GET_CODE (insn) == JUMP_INSN
13322 /* Ignore tablejump patterns. */
13323 && GET_CODE (PATTERN (insn)) != ADDR_VEC
13324 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
13325 && get_attr_far_jump (insn) == FAR_JUMP_YES
13328 /* Record the fact that we have decided that
13329 the function does use far jumps. */
13330 cfun->machine->far_jump_used = 1;
13331 return 1;
13335 return 0;
13338 /* Return nonzero if FUNC must be entered in ARM mode. */
13340 is_called_in_ARM_mode (tree func)
13342 gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
13344 /* Ignore the problem about functions whose address is taken. */
13345 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
13346 return TRUE;
13348 #ifdef ARM_PE
13349 return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
13350 #else
13351 return FALSE;
13352 #endif
13355 /* The bits which aren't usefully expanded as rtl. */
13356 const char *
13357 thumb_unexpanded_epilogue (void)
13359 int regno;
13360 unsigned long live_regs_mask = 0;
13361 int high_regs_pushed = 0;
13362 int had_to_push_lr;
13363 int size;
13365 if (return_used_this_function)
13366 return "";
13368 if (IS_NAKED (arm_current_func_type ()))
13369 return "";
13371 live_regs_mask = thumb_compute_save_reg_mask ();
13372 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13374 /* If we can deduce the registers used from the function's return value.
13375 This is more reliable that examining regs_ever_live[] because that
13376 will be set if the register is ever used in the function, not just if
13377 the register is used to hold a return value. */
13378 size = arm_size_return_regs ();
13380 /* The prolog may have pushed some high registers to use as
13381 work registers. e.g. the testsuite file:
13382 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
13383 compiles to produce:
13384 push {r4, r5, r6, r7, lr}
13385 mov r7, r9
13386 mov r6, r8
13387 push {r6, r7}
13388 as part of the prolog. We have to undo that pushing here. */
13390 if (high_regs_pushed)
13392 unsigned long mask = live_regs_mask & 0xff;
13393 int next_hi_reg;
13395 /* The available low registers depend on the size of the value we are
13396 returning. */
13397 if (size <= 12)
13398 mask |= 1 << 3;
13399 if (size <= 8)
13400 mask |= 1 << 2;
13402 if (mask == 0)
13403 /* Oh dear! We have no low registers into which we can pop
13404 high registers! */
13405 internal_error
13406 ("no low registers available for popping high registers");
13408 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
13409 if (live_regs_mask & (1 << next_hi_reg))
13410 break;
13412 while (high_regs_pushed)
13414 /* Find lo register(s) into which the high register(s) can
13415 be popped. */
13416 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13418 if (mask & (1 << regno))
13419 high_regs_pushed--;
13420 if (high_regs_pushed == 0)
13421 break;
13424 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
13426 /* Pop the values into the low register(s). */
13427 thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
13429 /* Move the value(s) into the high registers. */
13430 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13432 if (mask & (1 << regno))
13434 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
13435 regno);
13437 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
13438 if (live_regs_mask & (1 << next_hi_reg))
13439 break;
13443 live_regs_mask &= ~0x0f00;
13446 had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
13447 live_regs_mask &= 0xff;
13449 if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
13451 /* Pop the return address into the PC. */
13452 if (had_to_push_lr)
13453 live_regs_mask |= 1 << PC_REGNUM;
13455 /* Either no argument registers were pushed or a backtrace
13456 structure was created which includes an adjusted stack
13457 pointer, so just pop everything. */
13458 if (live_regs_mask)
13459 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13460 live_regs_mask);
13462 /* We have either just popped the return address into the
13463 PC or it is was kept in LR for the entire function. */
13464 if (!had_to_push_lr)
13465 thumb_exit (asm_out_file, LR_REGNUM);
13467 else
13469 /* Pop everything but the return address. */
13470 if (live_regs_mask)
13471 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13472 live_regs_mask);
13474 if (had_to_push_lr)
13476 if (size > 12)
13478 /* We have no free low regs, so save one. */
13479 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
13480 LAST_ARG_REGNUM);
13483 /* Get the return address into a temporary register. */
13484 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
13485 1 << LAST_ARG_REGNUM);
13487 if (size > 12)
13489 /* Move the return address to lr. */
13490 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
13491 LAST_ARG_REGNUM);
13492 /* Restore the low register. */
13493 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
13494 IP_REGNUM);
13495 regno = LR_REGNUM;
13497 else
13498 regno = LAST_ARG_REGNUM;
13500 else
13501 regno = LR_REGNUM;
13503 /* Remove the argument registers that were pushed onto the stack. */
13504 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
13505 SP_REGNUM, SP_REGNUM,
13506 current_function_pretend_args_size);
13508 thumb_exit (asm_out_file, regno);
13511 return "";
13514 /* Functions to save and restore machine-specific function data. */
13515 static struct machine_function *
13516 arm_init_machine_status (void)
13518 struct machine_function *machine;
13519 machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
13521 #if ARM_FT_UNKNOWN != 0
13522 machine->func_type = ARM_FT_UNKNOWN;
13523 #endif
13524 return machine;
13527 /* Return an RTX indicating where the return address to the
13528 calling function can be found. */
13530 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
13532 if (count != 0)
13533 return NULL_RTX;
13535 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13538 /* Do anything needed before RTL is emitted for each function. */
13539 void
13540 arm_init_expanders (void)
13542 /* Arrange to initialize and mark the machine per-function status. */
13543 init_machine_status = arm_init_machine_status;
13545 /* This is to stop the combine pass optimizing away the alignment
13546 adjustment of va_arg. */
13547 /* ??? It is claimed that this should not be necessary. */
13548 if (cfun)
13549 mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
13553 /* Like arm_compute_initial_elimination offset. Simpler because there
13554 isn't an ABI specified frame pointer for Thumb. Instead, we set it
13555 to point at the base of the local variables after static stack
13556 space for a function has been allocated. */
13558 HOST_WIDE_INT
13559 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13561 arm_stack_offsets *offsets;
13563 offsets = arm_get_frame_offsets ();
13565 switch (from)
13567 case ARG_POINTER_REGNUM:
13568 switch (to)
13570 case STACK_POINTER_REGNUM:
13571 return offsets->outgoing_args - offsets->saved_args;
13573 case FRAME_POINTER_REGNUM:
13574 return offsets->soft_frame - offsets->saved_args;
13576 case ARM_HARD_FRAME_POINTER_REGNUM:
13577 return offsets->saved_regs - offsets->saved_args;
13579 case THUMB_HARD_FRAME_POINTER_REGNUM:
13580 return offsets->locals_base - offsets->saved_args;
13582 default:
13583 gcc_unreachable ();
13585 break;
13587 case FRAME_POINTER_REGNUM:
13588 switch (to)
13590 case STACK_POINTER_REGNUM:
13591 return offsets->outgoing_args - offsets->soft_frame;
13593 case ARM_HARD_FRAME_POINTER_REGNUM:
13594 return offsets->saved_regs - offsets->soft_frame;
13596 case THUMB_HARD_FRAME_POINTER_REGNUM:
13597 return offsets->locals_base - offsets->soft_frame;
13599 default:
13600 gcc_unreachable ();
13602 break;
13604 default:
13605 gcc_unreachable ();
13610 /* Generate the rest of a function's prologue. */
13611 void
13612 thumb_expand_prologue (void)
13614 rtx insn, dwarf;
13616 HOST_WIDE_INT amount;
13617 arm_stack_offsets *offsets;
13618 unsigned long func_type;
13619 int regno;
13620 unsigned long live_regs_mask;
13622 func_type = arm_current_func_type ();
13624 /* Naked functions don't have prologues. */
13625 if (IS_NAKED (func_type))
13626 return;
13628 if (IS_INTERRUPT (func_type))
13630 error ("interrupt Service Routines cannot be coded in Thumb mode");
13631 return;
13634 live_regs_mask = thumb_compute_save_reg_mask ();
13635 /* Load the pic register before setting the frame pointer,
13636 so we can use r7 as a temporary work register. */
13637 if (flag_pic && arm_pic_register != INVALID_REGNUM)
13638 arm_load_pic_register (live_regs_mask);
13640 if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
13641 emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
13642 stack_pointer_rtx);
13644 offsets = arm_get_frame_offsets ();
13645 amount = offsets->outgoing_args - offsets->saved_regs;
13646 if (amount)
13648 if (amount < 512)
13650 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13651 GEN_INT (- amount)));
13652 RTX_FRAME_RELATED_P (insn) = 1;
13654 else
13656 rtx reg;
13658 /* The stack decrement is too big for an immediate value in a single
13659 insn. In theory we could issue multiple subtracts, but after
13660 three of them it becomes more space efficient to place the full
13661 value in the constant pool and load into a register. (Also the
13662 ARM debugger really likes to see only one stack decrement per
13663 function). So instead we look for a scratch register into which
13664 we can load the decrement, and then we subtract this from the
13665 stack pointer. Unfortunately on the thumb the only available
13666 scratch registers are the argument registers, and we cannot use
13667 these as they may hold arguments to the function. Instead we
13668 attempt to locate a call preserved register which is used by this
13669 function. If we can find one, then we know that it will have
13670 been pushed at the start of the prologue and so we can corrupt
13671 it now. */
13672 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13673 if (live_regs_mask & (1 << regno)
13674 && !(frame_pointer_needed
13675 && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13676 break;
13678 if (regno > LAST_LO_REGNUM) /* Very unlikely. */
13680 rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13682 /* Choose an arbitrary, non-argument low register. */
13683 reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13685 /* Save it by copying it into a high, scratch register. */
13686 emit_insn (gen_movsi (spare, reg));
13687 /* Add a USE to stop propagate_one_insn() from barfing. */
13688 emit_insn (gen_prologue_use (spare));
13690 /* Decrement the stack. */
13691 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13692 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13693 stack_pointer_rtx, reg));
13694 RTX_FRAME_RELATED_P (insn) = 1;
13695 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
13696 plus_constant (stack_pointer_rtx,
13697 -amount));
13698 RTX_FRAME_RELATED_P (dwarf) = 1;
13699 REG_NOTES (insn)
13700 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13701 REG_NOTES (insn));
13703 /* Restore the low register's original value. */
13704 emit_insn (gen_movsi (reg, spare));
13706 /* Emit a USE of the restored scratch register, so that flow
13707 analysis will not consider the restore redundant. The
13708 register won't be used again in this function and isn't
13709 restored by the epilogue. */
13710 emit_insn (gen_prologue_use (reg));
13712 else
13714 reg = gen_rtx_REG (SImode, regno);
13716 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13718 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13719 stack_pointer_rtx, reg));
13720 RTX_FRAME_RELATED_P (insn) = 1;
13721 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
13722 plus_constant (stack_pointer_rtx,
13723 -amount));
13724 RTX_FRAME_RELATED_P (dwarf) = 1;
13725 REG_NOTES (insn)
13726 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13727 REG_NOTES (insn));
13732 if (frame_pointer_needed)
13734 amount = offsets->outgoing_args - offsets->locals_base;
13736 if (amount < 1024)
13737 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13738 stack_pointer_rtx, GEN_INT (amount)));
13739 else
13741 emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
13742 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13743 hard_frame_pointer_rtx,
13744 stack_pointer_rtx));
13745 dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
13746 plus_constant (stack_pointer_rtx, amount));
13747 RTX_FRAME_RELATED_P (dwarf) = 1;
13748 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13749 REG_NOTES (insn));
13752 RTX_FRAME_RELATED_P (insn) = 1;
13755 /* If we are profiling, make sure no instructions are scheduled before
13756 the call to mcount. Similarly if the user has requested no
13757 scheduling in the prolog. Similarly if we want non-call exceptions
13758 using the EABI unwinder, to prevent faulting instructions from being
13759 swapped with a stack adjustment. */
13760 if (current_function_profile || !TARGET_SCHED_PROLOG
13761 || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
13762 emit_insn (gen_blockage ());
13764 cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13765 if (live_regs_mask & 0xff)
13766 cfun->machine->lr_save_eliminated = 0;
13768 /* If the link register is being kept alive, with the return address in it,
13769 then make sure that it does not get reused by the ce2 pass. */
13770 if (cfun->machine->lr_save_eliminated)
13771 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13775 void
13776 thumb_expand_epilogue (void)
13778 HOST_WIDE_INT amount;
13779 arm_stack_offsets *offsets;
13780 int regno;
13782 /* Naked functions don't have prologues. */
13783 if (IS_NAKED (arm_current_func_type ()))
13784 return;
13786 offsets = arm_get_frame_offsets ();
13787 amount = offsets->outgoing_args - offsets->saved_regs;
13789 if (frame_pointer_needed)
13791 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13792 amount = offsets->locals_base - offsets->saved_regs;
13795 if (amount)
13797 if (amount < 512)
13798 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13799 GEN_INT (amount)));
13800 else
13802 /* r3 is always free in the epilogue. */
13803 rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13805 emit_insn (gen_movsi (reg, GEN_INT (amount)));
13806 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13810 /* Emit a USE (stack_pointer_rtx), so that
13811 the stack adjustment will not be deleted. */
13812 emit_insn (gen_prologue_use (stack_pointer_rtx));
13814 if (current_function_profile || !TARGET_SCHED_PROLOG)
13815 emit_insn (gen_blockage ());
13817 /* Emit a clobber for each insn that will be restored in the epilogue,
13818 so that flow2 will get register lifetimes correct. */
13819 for (regno = 0; regno < 13; regno++)
13820 if (regs_ever_live[regno] && !call_used_regs[regno])
13821 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13823 if (! regs_ever_live[LR_REGNUM])
13824 emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13827 static void
13828 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13830 unsigned long live_regs_mask = 0;
13831 unsigned long l_mask;
13832 unsigned high_regs_pushed = 0;
13833 int cfa_offset = 0;
13834 int regno;
13836 if (IS_NAKED (arm_current_func_type ()))
13837 return;
13839 if (is_called_in_ARM_mode (current_function_decl))
13841 const char * name;
13843 gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
13844 gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
13845 == SYMBOL_REF);
13846 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
13848 /* Generate code sequence to switch us into Thumb mode. */
13849 /* The .code 32 directive has already been emitted by
13850 ASM_DECLARE_FUNCTION_NAME. */
13851 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13852 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13854 /* Generate a label, so that the debugger will notice the
13855 change in instruction sets. This label is also used by
13856 the assembler to bypass the ARM code when this function
13857 is called from a Thumb encoded function elsewhere in the
13858 same file. Hence the definition of STUB_NAME here must
13859 agree with the definition in gas/config/tc-arm.c. */
13861 #define STUB_NAME ".real_start_of"
13863 fprintf (f, "\t.code\t16\n");
13864 #ifdef ARM_PE
13865 if (arm_dllexport_name_p (name))
13866 name = arm_strip_name_encoding (name);
13867 #endif
13868 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13869 fprintf (f, "\t.thumb_func\n");
13870 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13873 if (current_function_pretend_args_size)
13875 /* Output unwind directive for the stack adjustment. */
13876 if (ARM_EABI_UNWIND_TABLES)
13877 fprintf (f, "\t.pad #%d\n",
13878 current_function_pretend_args_size);
13880 if (cfun->machine->uses_anonymous_args)
13882 int num_pushes;
13884 fprintf (f, "\tpush\t{");
13886 num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13888 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13889 regno <= LAST_ARG_REGNUM;
13890 regno++)
13891 asm_fprintf (f, "%r%s", regno,
13892 regno == LAST_ARG_REGNUM ? "" : ", ");
13894 fprintf (f, "}\n");
13896 else
13897 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13898 SP_REGNUM, SP_REGNUM,
13899 current_function_pretend_args_size);
13901 /* We don't need to record the stores for unwinding (would it
13902 help the debugger any if we did?), but record the change in
13903 the stack pointer. */
13904 if (dwarf2out_do_frame ())
13906 char *l = dwarf2out_cfi_label ();
13908 cfa_offset = cfa_offset + current_function_pretend_args_size;
13909 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13913 /* Get the registers we are going to push. */
13914 live_regs_mask = thumb_compute_save_reg_mask ();
13915 /* Extract a mask of the ones we can give to the Thumb's push instruction. */
13916 l_mask = live_regs_mask & 0x40ff;
13917 /* Then count how many other high registers will need to be pushed. */
13918 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13920 if (TARGET_BACKTRACE)
13922 unsigned offset;
13923 unsigned work_register;
13925 /* We have been asked to create a stack backtrace structure.
13926 The code looks like this:
13928 0 .align 2
13929 0 func:
13930 0 sub SP, #16 Reserve space for 4 registers.
13931 2 push {R7} Push low registers.
13932 4 add R7, SP, #20 Get the stack pointer before the push.
13933 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
13934 8 mov R7, PC Get hold of the start of this code plus 12.
13935 10 str R7, [SP, #16] Store it.
13936 12 mov R7, FP Get hold of the current frame pointer.
13937 14 str R7, [SP, #4] Store it.
13938 16 mov R7, LR Get hold of the current return address.
13939 18 str R7, [SP, #12] Store it.
13940 20 add R7, SP, #16 Point at the start of the backtrace structure.
13941 22 mov FP, R7 Put this value into the frame pointer. */
13943 work_register = thumb_find_work_register (live_regs_mask);
13945 if (ARM_EABI_UNWIND_TABLES)
13946 asm_fprintf (f, "\t.pad #16\n");
13948 asm_fprintf
13949 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13950 SP_REGNUM, SP_REGNUM);
13952 if (dwarf2out_do_frame ())
13954 char *l = dwarf2out_cfi_label ();
13956 cfa_offset = cfa_offset + 16;
13957 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13960 if (l_mask)
13962 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13963 offset = bit_count (l_mask) * UNITS_PER_WORD;
13965 else
13966 offset = 0;
13968 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13969 offset + 16 + current_function_pretend_args_size);
13971 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13972 offset + 4);
13974 /* Make sure that the instruction fetching the PC is in the right place
13975 to calculate "start of backtrace creation code + 12". */
13976 if (l_mask)
13978 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13979 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13980 offset + 12);
13981 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13982 ARM_HARD_FRAME_POINTER_REGNUM);
13983 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13984 offset);
13986 else
13988 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13989 ARM_HARD_FRAME_POINTER_REGNUM);
13990 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13991 offset);
13992 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13993 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13994 offset + 12);
13997 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13998 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13999 offset + 8);
14000 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
14001 offset + 12);
14002 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
14003 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
14005 /* Optimization: If we are not pushing any low registers but we are going
14006 to push some high registers then delay our first push. This will just
14007 be a push of LR and we can combine it with the push of the first high
14008 register. */
14009 else if ((l_mask & 0xff) != 0
14010 || (high_regs_pushed == 0 && l_mask))
14011 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
14013 if (high_regs_pushed)
14015 unsigned pushable_regs;
14016 unsigned next_hi_reg;
14018 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
14019 if (live_regs_mask & (1 << next_hi_reg))
14020 break;
14022 pushable_regs = l_mask & 0xff;
14024 if (pushable_regs == 0)
14025 pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
14027 while (high_regs_pushed > 0)
14029 unsigned long real_regs_mask = 0;
14031 for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
14033 if (pushable_regs & (1 << regno))
14035 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
14037 high_regs_pushed --;
14038 real_regs_mask |= (1 << next_hi_reg);
14040 if (high_regs_pushed)
14042 for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
14043 next_hi_reg --)
14044 if (live_regs_mask & (1 << next_hi_reg))
14045 break;
14047 else
14049 pushable_regs &= ~((1 << regno) - 1);
14050 break;
14055 /* If we had to find a work register and we have not yet
14056 saved the LR then add it to the list of regs to push. */
14057 if (l_mask == (1 << LR_REGNUM))
14059 thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
14060 1, &cfa_offset,
14061 real_regs_mask | (1 << LR_REGNUM));
14062 l_mask = 0;
14064 else
14065 thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
14070 /* Handle the case of a double word load into a low register from
14071 a computed memory address. The computed address may involve a
14072 register which is overwritten by the load. */
14073 const char *
14074 thumb_load_double_from_address (rtx *operands)
14076 rtx addr;
14077 rtx base;
14078 rtx offset;
14079 rtx arg1;
14080 rtx arg2;
14082 gcc_assert (GET_CODE (operands[0]) == REG);
14083 gcc_assert (GET_CODE (operands[1]) == MEM);
14085 /* Get the memory address. */
14086 addr = XEXP (operands[1], 0);
14088 /* Work out how the memory address is computed. */
14089 switch (GET_CODE (addr))
14091 case REG:
14092 operands[2] = adjust_address (operands[1], SImode, 4);
14094 if (REGNO (operands[0]) == REGNO (addr))
14096 output_asm_insn ("ldr\t%H0, %2", operands);
14097 output_asm_insn ("ldr\t%0, %1", operands);
14099 else
14101 output_asm_insn ("ldr\t%0, %1", operands);
14102 output_asm_insn ("ldr\t%H0, %2", operands);
14104 break;
14106 case CONST:
14107 /* Compute <address> + 4 for the high order load. */
14108 operands[2] = adjust_address (operands[1], SImode, 4);
14110 output_asm_insn ("ldr\t%0, %1", operands);
14111 output_asm_insn ("ldr\t%H0, %2", operands);
14112 break;
14114 case PLUS:
14115 arg1 = XEXP (addr, 0);
14116 arg2 = XEXP (addr, 1);
14118 if (CONSTANT_P (arg1))
14119 base = arg2, offset = arg1;
14120 else
14121 base = arg1, offset = arg2;
14123 gcc_assert (GET_CODE (base) == REG);
14125 /* Catch the case of <address> = <reg> + <reg> */
14126 if (GET_CODE (offset) == REG)
14128 int reg_offset = REGNO (offset);
14129 int reg_base = REGNO (base);
14130 int reg_dest = REGNO (operands[0]);
14132 /* Add the base and offset registers together into the
14133 higher destination register. */
14134 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
14135 reg_dest + 1, reg_base, reg_offset);
14137 /* Load the lower destination register from the address in
14138 the higher destination register. */
14139 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
14140 reg_dest, reg_dest + 1);
14142 /* Load the higher destination register from its own address
14143 plus 4. */
14144 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
14145 reg_dest + 1, reg_dest + 1);
14147 else
14149 /* Compute <address> + 4 for the high order load. */
14150 operands[2] = adjust_address (operands[1], SImode, 4);
14152 /* If the computed address is held in the low order register
14153 then load the high order register first, otherwise always
14154 load the low order register first. */
14155 if (REGNO (operands[0]) == REGNO (base))
14157 output_asm_insn ("ldr\t%H0, %2", operands);
14158 output_asm_insn ("ldr\t%0, %1", operands);
14160 else
14162 output_asm_insn ("ldr\t%0, %1", operands);
14163 output_asm_insn ("ldr\t%H0, %2", operands);
14166 break;
14168 case LABEL_REF:
14169 /* With no registers to worry about we can just load the value
14170 directly. */
14171 operands[2] = adjust_address (operands[1], SImode, 4);
14173 output_asm_insn ("ldr\t%H0, %2", operands);
14174 output_asm_insn ("ldr\t%0, %1", operands);
14175 break;
14177 default:
14178 gcc_unreachable ();
14181 return "";
14184 const char *
14185 thumb_output_move_mem_multiple (int n, rtx *operands)
14187 rtx tmp;
14189 switch (n)
14191 case 2:
14192 if (REGNO (operands[4]) > REGNO (operands[5]))
14194 tmp = operands[4];
14195 operands[4] = operands[5];
14196 operands[5] = tmp;
14198 output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
14199 output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
14200 break;
14202 case 3:
14203 if (REGNO (operands[4]) > REGNO (operands[5]))
14205 tmp = operands[4];
14206 operands[4] = operands[5];
14207 operands[5] = tmp;
14209 if (REGNO (operands[5]) > REGNO (operands[6]))
14211 tmp = operands[5];
14212 operands[5] = operands[6];
14213 operands[6] = tmp;
14215 if (REGNO (operands[4]) > REGNO (operands[5]))
14217 tmp = operands[4];
14218 operands[4] = operands[5];
14219 operands[5] = tmp;
14222 output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
14223 output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
14224 break;
14226 default:
14227 gcc_unreachable ();
14230 return "";
14233 /* Output a call-via instruction for thumb state. */
14234 const char *
14235 thumb_call_via_reg (rtx reg)
14237 int regno = REGNO (reg);
14238 rtx *labelp;
14240 gcc_assert (regno < LR_REGNUM);
14242 /* If we are in the normal text section we can use a single instance
14243 per compilation unit. If we are doing function sections, then we need
14244 an entry per section, since we can't rely on reachability. */
14245 if (in_section == text_section)
14247 thumb_call_reg_needed = 1;
14249 if (thumb_call_via_label[regno] == NULL)
14250 thumb_call_via_label[regno] = gen_label_rtx ();
14251 labelp = thumb_call_via_label + regno;
14253 else
14255 if (cfun->machine->call_via[regno] == NULL)
14256 cfun->machine->call_via[regno] = gen_label_rtx ();
14257 labelp = cfun->machine->call_via + regno;
14260 output_asm_insn ("bl\t%a0", labelp);
14261 return "";
14264 /* Routines for generating rtl. */
14265 void
14266 thumb_expand_movmemqi (rtx *operands)
14268 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
14269 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
14270 HOST_WIDE_INT len = INTVAL (operands[2]);
14271 HOST_WIDE_INT offset = 0;
14273 while (len >= 12)
14275 emit_insn (gen_movmem12b (out, in, out, in));
14276 len -= 12;
14279 if (len >= 8)
14281 emit_insn (gen_movmem8b (out, in, out, in));
14282 len -= 8;
14285 if (len >= 4)
14287 rtx reg = gen_reg_rtx (SImode);
14288 emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
14289 emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
14290 len -= 4;
14291 offset += 4;
14294 if (len >= 2)
14296 rtx reg = gen_reg_rtx (HImode);
14297 emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
14298 plus_constant (in, offset))));
14299 emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
14300 reg));
14301 len -= 2;
14302 offset += 2;
14305 if (len)
14307 rtx reg = gen_reg_rtx (QImode);
14308 emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
14309 plus_constant (in, offset))));
14310 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
14311 reg));
14315 void
14316 thumb_reload_out_hi (rtx *operands)
14318 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
14321 /* Handle reading a half-word from memory during reload. */
14322 void
14323 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
14325 gcc_unreachable ();
14328 /* Return the length of a function name prefix
14329 that starts with the character 'c'. */
14330 static int
14331 arm_get_strip_length (int c)
14333 switch (c)
14335 ARM_NAME_ENCODING_LENGTHS
14336 default: return 0;
14340 /* Return a pointer to a function's name with any
14341 and all prefix encodings stripped from it. */
14342 const char *
14343 arm_strip_name_encoding (const char *name)
14345 int skip;
14347 while ((skip = arm_get_strip_length (* name)))
14348 name += skip;
14350 return name;
14353 /* If there is a '*' anywhere in the name's prefix, then
14354 emit the stripped name verbatim, otherwise prepend an
14355 underscore if leading underscores are being used. */
14356 void
14357 arm_asm_output_labelref (FILE *stream, const char *name)
14359 int skip;
14360 int verbatim = 0;
14362 while ((skip = arm_get_strip_length (* name)))
14364 verbatim |= (*name == '*');
14365 name += skip;
14368 if (verbatim)
14369 fputs (name, stream);
14370 else
14371 asm_fprintf (stream, "%U%s", name);
14374 static void
14375 arm_file_end (void)
14377 int regno;
14379 if (! thumb_call_reg_needed)
14380 return;
14382 switch_to_section (text_section);
14383 asm_fprintf (asm_out_file, "\t.code 16\n");
14384 ASM_OUTPUT_ALIGN (asm_out_file, 1);
14386 for (regno = 0; regno < LR_REGNUM; regno++)
14388 rtx label = thumb_call_via_label[regno];
14390 if (label != 0)
14392 targetm.asm_out.internal_label (asm_out_file, "L",
14393 CODE_LABEL_NUMBER (label));
14394 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
14399 rtx aof_pic_label;
14401 #ifdef AOF_ASSEMBLER
14402 /* Special functions only needed when producing AOF syntax assembler. */
14404 struct pic_chain
14406 struct pic_chain * next;
14407 const char * symname;
14410 static struct pic_chain * aof_pic_chain = NULL;
14413 aof_pic_entry (rtx x)
14415 struct pic_chain ** chainp;
14416 int offset;
14418 if (aof_pic_label == NULL_RTX)
14420 aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
14423 for (offset = 0, chainp = &aof_pic_chain; *chainp;
14424 offset += 4, chainp = &(*chainp)->next)
14425 if ((*chainp)->symname == XSTR (x, 0))
14426 return plus_constant (aof_pic_label, offset);
14428 *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
14429 (*chainp)->next = NULL;
14430 (*chainp)->symname = XSTR (x, 0);
14431 return plus_constant (aof_pic_label, offset);
14434 void
14435 aof_dump_pic_table (FILE *f)
14437 struct pic_chain * chain;
14439 if (aof_pic_chain == NULL)
14440 return;
14442 asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
14443 PIC_OFFSET_TABLE_REGNUM,
14444 PIC_OFFSET_TABLE_REGNUM);
14445 fputs ("|x$adcons|\n", f);
14447 for (chain = aof_pic_chain; chain; chain = chain->next)
14449 fputs ("\tDCD\t", f);
14450 assemble_name (f, chain->symname);
14451 fputs ("\n", f);
14455 int arm_text_section_count = 1;
14457 /* A get_unnamed_section callback for switching to the text section. */
14459 static void
14460 aof_output_text_section_asm_op (const void *data ATTRIBUTE_UNUSED)
14462 fprintf (asm_out_file, "\tAREA |C$$code%d|, CODE, READONLY",
14463 arm_text_section_count++);
14464 if (flag_pic)
14465 fprintf (asm_out_file, ", PIC, REENTRANT");
14466 fprintf (asm_out_file, "\n");
14469 static int arm_data_section_count = 1;
14471 /* A get_unnamed_section callback for switching to the data section. */
14473 static void
14474 aof_output_data_section_asm_op (const void *data ATTRIBUTE_UNUSED)
14476 fprintf (asm_out_file, "\tAREA |C$$data%d|, DATA\n",
14477 arm_data_section_count++);
14480 /* Implement TARGET_ASM_INIT_SECTIONS.
14482 AOF Assembler syntax is a nightmare when it comes to areas, since once
14483 we change from one area to another, we can't go back again. Instead,
14484 we must create a new area with the same attributes and add the new output
14485 to that. Unfortunately, there is nothing we can do here to guarantee that
14486 two areas with the same attributes will be linked adjacently in the
14487 resulting executable, so we have to be careful not to do pc-relative
14488 addressing across such boundaries. */
14490 static void
14491 aof_asm_init_sections (void)
14493 text_section = get_unnamed_section (SECTION_CODE,
14494 aof_output_text_section_asm_op, NULL);
14495 data_section = get_unnamed_section (SECTION_WRITE,
14496 aof_output_data_section_asm_op, NULL);
14497 readonly_data_section = text_section;
14500 void
14501 zero_init_section (void)
14503 static int zero_init_count = 1;
14505 fprintf (asm_out_file, "\tAREA |C$$zidata%d|,NOINIT\n", zero_init_count++);
14506 in_section = NULL;
14509 /* The AOF assembler is religiously strict about declarations of
14510 imported and exported symbols, so that it is impossible to declare
14511 a function as imported near the beginning of the file, and then to
14512 export it later on. It is, however, possible to delay the decision
14513 until all the functions in the file have been compiled. To get
14514 around this, we maintain a list of the imports and exports, and
14515 delete from it any that are subsequently defined. At the end of
14516 compilation we spit the remainder of the list out before the END
14517 directive. */
14519 struct import
14521 struct import * next;
14522 const char * name;
14525 static struct import * imports_list = NULL;
14527 void
14528 aof_add_import (const char *name)
14530 struct import * new;
14532 for (new = imports_list; new; new = new->next)
14533 if (new->name == name)
14534 return;
14536 new = (struct import *) xmalloc (sizeof (struct import));
14537 new->next = imports_list;
14538 imports_list = new;
14539 new->name = name;
14542 void
14543 aof_delete_import (const char *name)
14545 struct import ** old;
14547 for (old = &imports_list; *old; old = & (*old)->next)
14549 if ((*old)->name == name)
14551 *old = (*old)->next;
14552 return;
14557 int arm_main_function = 0;
14559 static void
14560 aof_dump_imports (FILE *f)
14562 /* The AOF assembler needs this to cause the startup code to be extracted
14563 from the library. Brining in __main causes the whole thing to work
14564 automagically. */
14565 if (arm_main_function)
14567 switch_to_section (text_section);
14568 fputs ("\tIMPORT __main\n", f);
14569 fputs ("\tDCD __main\n", f);
14572 /* Now dump the remaining imports. */
14573 while (imports_list)
14575 fprintf (f, "\tIMPORT\t");
14576 assemble_name (f, imports_list->name);
14577 fputc ('\n', f);
14578 imports_list = imports_list->next;
14582 static void
14583 aof_globalize_label (FILE *stream, const char *name)
14585 default_globalize_label (stream, name);
14586 if (! strcmp (name, "main"))
14587 arm_main_function = 1;
14590 static void
14591 aof_file_start (void)
14593 fputs ("__r0\tRN\t0\n", asm_out_file);
14594 fputs ("__a1\tRN\t0\n", asm_out_file);
14595 fputs ("__a2\tRN\t1\n", asm_out_file);
14596 fputs ("__a3\tRN\t2\n", asm_out_file);
14597 fputs ("__a4\tRN\t3\n", asm_out_file);
14598 fputs ("__v1\tRN\t4\n", asm_out_file);
14599 fputs ("__v2\tRN\t5\n", asm_out_file);
14600 fputs ("__v3\tRN\t6\n", asm_out_file);
14601 fputs ("__v4\tRN\t7\n", asm_out_file);
14602 fputs ("__v5\tRN\t8\n", asm_out_file);
14603 fputs ("__v6\tRN\t9\n", asm_out_file);
14604 fputs ("__sl\tRN\t10\n", asm_out_file);
14605 fputs ("__fp\tRN\t11\n", asm_out_file);
14606 fputs ("__ip\tRN\t12\n", asm_out_file);
14607 fputs ("__sp\tRN\t13\n", asm_out_file);
14608 fputs ("__lr\tRN\t14\n", asm_out_file);
14609 fputs ("__pc\tRN\t15\n", asm_out_file);
14610 fputs ("__f0\tFN\t0\n", asm_out_file);
14611 fputs ("__f1\tFN\t1\n", asm_out_file);
14612 fputs ("__f2\tFN\t2\n", asm_out_file);
14613 fputs ("__f3\tFN\t3\n", asm_out_file);
14614 fputs ("__f4\tFN\t4\n", asm_out_file);
14615 fputs ("__f5\tFN\t5\n", asm_out_file);
14616 fputs ("__f6\tFN\t6\n", asm_out_file);
14617 fputs ("__f7\tFN\t7\n", asm_out_file);
14618 switch_to_section (text_section);
14621 static void
14622 aof_file_end (void)
14624 if (flag_pic)
14625 aof_dump_pic_table (asm_out_file);
14626 arm_file_end ();
14627 aof_dump_imports (asm_out_file);
14628 fputs ("\tEND\n", asm_out_file);
14630 #endif /* AOF_ASSEMBLER */
14632 #ifndef ARM_PE
14633 /* Symbols in the text segment can be accessed without indirecting via the
14634 constant pool; it may take an extra binary operation, but this is still
14635 faster than indirecting via memory. Don't do this when not optimizing,
14636 since we won't be calculating al of the offsets necessary to do this
14637 simplification. */
14639 static void
14640 arm_encode_section_info (tree decl, rtx rtl, int first)
14642 /* This doesn't work with AOF syntax, since the string table may be in
14643 a different AREA. */
14644 #ifndef AOF_ASSEMBLER
14645 if (optimize > 0 && TREE_CONSTANT (decl))
14646 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14647 #endif
14649 /* If we are referencing a function that is weak then encode a long call
14650 flag in the function name, otherwise if the function is static or
14651 or known to be defined in this file then encode a short call flag. */
14652 if (first && DECL_P (decl))
14654 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14655 arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14656 else if (! TREE_PUBLIC (decl))
14657 arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14660 default_encode_section_info (decl, rtl, first);
14662 #endif /* !ARM_PE */
14664 static void
14665 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14667 if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14668 && !strcmp (prefix, "L"))
14670 arm_ccfsm_state = 0;
14671 arm_target_insn = NULL;
14673 default_internal_label (stream, prefix, labelno);
14676 /* Output code to add DELTA to the first argument, and then jump
14677 to FUNCTION. Used for C++ multiple inheritance. */
14678 static void
14679 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14680 HOST_WIDE_INT delta,
14681 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14682 tree function)
14684 static int thunk_label = 0;
14685 char label[256];
14686 int mi_delta = delta;
14687 const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14688 int shift = 0;
14689 int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14690 ? 1 : 0);
14691 if (mi_delta < 0)
14692 mi_delta = - mi_delta;
14693 if (TARGET_THUMB)
14695 int labelno = thunk_label++;
14696 ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14697 fputs ("\tldr\tr12, ", file);
14698 assemble_name (file, label);
14699 fputc ('\n', file);
14701 while (mi_delta != 0)
14703 if ((mi_delta & (3 << shift)) == 0)
14704 shift += 2;
14705 else
14707 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14708 mi_op, this_regno, this_regno,
14709 mi_delta & (0xff << shift));
14710 mi_delta &= ~(0xff << shift);
14711 shift += 8;
14714 if (TARGET_THUMB)
14716 fprintf (file, "\tbx\tr12\n");
14717 ASM_OUTPUT_ALIGN (file, 2);
14718 assemble_name (file, label);
14719 fputs (":\n", file);
14720 assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14722 else
14724 fputs ("\tb\t", file);
14725 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14726 if (NEED_PLT_RELOC)
14727 fputs ("(PLT)", file);
14728 fputc ('\n', file);
14733 arm_emit_vector_const (FILE *file, rtx x)
14735 int i;
14736 const char * pattern;
14738 gcc_assert (GET_CODE (x) == CONST_VECTOR);
14740 switch (GET_MODE (x))
14742 case V2SImode: pattern = "%08x"; break;
14743 case V4HImode: pattern = "%04x"; break;
14744 case V8QImode: pattern = "%02x"; break;
14745 default: gcc_unreachable ();
14748 fprintf (file, "0x");
14749 for (i = CONST_VECTOR_NUNITS (x); i--;)
14751 rtx element;
14753 element = CONST_VECTOR_ELT (x, i);
14754 fprintf (file, pattern, INTVAL (element));
14757 return 1;
14760 const char *
14761 arm_output_load_gr (rtx *operands)
14763 rtx reg;
14764 rtx offset;
14765 rtx wcgr;
14766 rtx sum;
14768 if (GET_CODE (operands [1]) != MEM
14769 || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14770 || GET_CODE (reg = XEXP (sum, 0)) != REG
14771 || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14772 || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14773 return "wldrw%?\t%0, %1";
14775 /* Fix up an out-of-range load of a GR register. */
14776 output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14777 wcgr = operands[0];
14778 operands[0] = reg;
14779 output_asm_insn ("ldr%?\t%0, %1", operands);
14781 operands[0] = wcgr;
14782 operands[1] = reg;
14783 output_asm_insn ("tmcr%?\t%0, %1", operands);
14784 output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14786 return "";
14789 static rtx
14790 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14791 int incoming ATTRIBUTE_UNUSED)
14793 #if 0
14794 /* FIXME: The ARM backend has special code to handle structure
14795 returns, and will reserve its own hidden first argument. So
14796 if this macro is enabled a *second* hidden argument will be
14797 reserved, which will break binary compatibility with old
14798 toolchains and also thunk handling. One day this should be
14799 fixed. */
14800 return 0;
14801 #else
14802 /* Register in which address to store a structure value
14803 is passed to a function. */
14804 return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14805 #endif
14808 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14810 On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14811 named arg and all anonymous args onto the stack.
14812 XXX I know the prologue shouldn't be pushing registers, but it is faster
14813 that way. */
14815 static void
14816 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14817 enum machine_mode mode ATTRIBUTE_UNUSED,
14818 tree type ATTRIBUTE_UNUSED,
14819 int *pretend_size,
14820 int second_time ATTRIBUTE_UNUSED)
14822 cfun->machine->uses_anonymous_args = 1;
14823 if (cum->nregs < NUM_ARG_REGS)
14824 *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14827 /* Return nonzero if the CONSUMER instruction (a store) does not need
14828 PRODUCER's value to calculate the address. */
14831 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14833 rtx value = PATTERN (producer);
14834 rtx addr = PATTERN (consumer);
14836 if (GET_CODE (value) == COND_EXEC)
14837 value = COND_EXEC_CODE (value);
14838 if (GET_CODE (value) == PARALLEL)
14839 value = XVECEXP (value, 0, 0);
14840 value = XEXP (value, 0);
14841 if (GET_CODE (addr) == COND_EXEC)
14842 addr = COND_EXEC_CODE (addr);
14843 if (GET_CODE (addr) == PARALLEL)
14844 addr = XVECEXP (addr, 0, 0);
14845 addr = XEXP (addr, 0);
14847 return !reg_overlap_mentioned_p (value, addr);
14850 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14851 have an early register shift value or amount dependency on the
14852 result of PRODUCER. */
14855 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14857 rtx value = PATTERN (producer);
14858 rtx op = PATTERN (consumer);
14859 rtx early_op;
14861 if (GET_CODE (value) == COND_EXEC)
14862 value = COND_EXEC_CODE (value);
14863 if (GET_CODE (value) == PARALLEL)
14864 value = XVECEXP (value, 0, 0);
14865 value = XEXP (value, 0);
14866 if (GET_CODE (op) == COND_EXEC)
14867 op = COND_EXEC_CODE (op);
14868 if (GET_CODE (op) == PARALLEL)
14869 op = XVECEXP (op, 0, 0);
14870 op = XEXP (op, 1);
14872 early_op = XEXP (op, 0);
14873 /* This is either an actual independent shift, or a shift applied to
14874 the first operand of another operation. We want the whole shift
14875 operation. */
14876 if (GET_CODE (early_op) == REG)
14877 early_op = op;
14879 return !reg_overlap_mentioned_p (value, early_op);
14882 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14883 have an early register shift value dependency on the result of
14884 PRODUCER. */
14887 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14889 rtx value = PATTERN (producer);
14890 rtx op = PATTERN (consumer);
14891 rtx early_op;
14893 if (GET_CODE (value) == COND_EXEC)
14894 value = COND_EXEC_CODE (value);
14895 if (GET_CODE (value) == PARALLEL)
14896 value = XVECEXP (value, 0, 0);
14897 value = XEXP (value, 0);
14898 if (GET_CODE (op) == COND_EXEC)
14899 op = COND_EXEC_CODE (op);
14900 if (GET_CODE (op) == PARALLEL)
14901 op = XVECEXP (op, 0, 0);
14902 op = XEXP (op, 1);
14904 early_op = XEXP (op, 0);
14906 /* This is either an actual independent shift, or a shift applied to
14907 the first operand of another operation. We want the value being
14908 shifted, in either case. */
14909 if (GET_CODE (early_op) != REG)
14910 early_op = XEXP (early_op, 0);
14912 return !reg_overlap_mentioned_p (value, early_op);
14915 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14916 have an early register mult dependency on the result of
14917 PRODUCER. */
14920 arm_no_early_mul_dep (rtx producer, rtx consumer)
14922 rtx value = PATTERN (producer);
14923 rtx op = PATTERN (consumer);
14925 if (GET_CODE (value) == COND_EXEC)
14926 value = COND_EXEC_CODE (value);
14927 if (GET_CODE (value) == PARALLEL)
14928 value = XVECEXP (value, 0, 0);
14929 value = XEXP (value, 0);
14930 if (GET_CODE (op) == COND_EXEC)
14931 op = COND_EXEC_CODE (op);
14932 if (GET_CODE (op) == PARALLEL)
14933 op = XVECEXP (op, 0, 0);
14934 op = XEXP (op, 1);
14936 return (GET_CODE (op) == PLUS
14937 && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14941 /* We can't rely on the caller doing the proper promotion when
14942 using APCS or ATPCS. */
14944 static bool
14945 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14947 return !TARGET_AAPCS_BASED;
14951 /* AAPCS based ABIs use short enums by default. */
14953 static bool
14954 arm_default_short_enums (void)
14956 return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
14960 /* AAPCS requires that anonymous bitfields affect structure alignment. */
14962 static bool
14963 arm_align_anon_bitfield (void)
14965 return TARGET_AAPCS_BASED;
14969 /* The generic C++ ABI says 64-bit (long long). The EABI says 32-bit. */
14971 static tree
14972 arm_cxx_guard_type (void)
14974 return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14978 /* The EABI says test the least significant bit of a guard variable. */
14980 static bool
14981 arm_cxx_guard_mask_bit (void)
14983 return TARGET_AAPCS_BASED;
14987 /* The EABI specifies that all array cookies are 8 bytes long. */
14989 static tree
14990 arm_get_cookie_size (tree type)
14992 tree size;
14994 if (!TARGET_AAPCS_BASED)
14995 return default_cxx_get_cookie_size (type);
14997 size = build_int_cst (sizetype, 8);
14998 return size;
15002 /* The EABI says that array cookies should also contain the element size. */
15004 static bool
15005 arm_cookie_has_size (void)
15007 return TARGET_AAPCS_BASED;
15011 /* The EABI says constructors and destructors should return a pointer to
15012 the object constructed/destroyed. */
15014 static bool
15015 arm_cxx_cdtor_returns_this (void)
15017 return TARGET_AAPCS_BASED;
15020 /* The EABI says that an inline function may never be the key
15021 method. */
15023 static bool
15024 arm_cxx_key_method_may_be_inline (void)
15026 return !TARGET_AAPCS_BASED;
15029 static void
15030 arm_cxx_determine_class_data_visibility (tree decl)
15032 if (!TARGET_AAPCS_BASED)
15033 return;
15035 /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
15036 is exported. However, on systems without dynamic vague linkage,
15037 \S 3.2.5.6 says that COMDAT class data has hidden linkage. */
15038 if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
15039 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
15040 else
15041 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
15042 DECL_VISIBILITY_SPECIFIED (decl) = 1;
15045 static bool
15046 arm_cxx_class_data_always_comdat (void)
15048 /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
15049 vague linkage if the class has no key function. */
15050 return !TARGET_AAPCS_BASED;
15054 /* The EABI says __aeabi_atexit should be used to register static
15055 destructors. */
15057 static bool
15058 arm_cxx_use_aeabi_atexit (void)
15060 return TARGET_AAPCS_BASED;
15064 void
15065 arm_set_return_address (rtx source, rtx scratch)
15067 arm_stack_offsets *offsets;
15068 HOST_WIDE_INT delta;
15069 rtx addr;
15070 unsigned long saved_regs;
15072 saved_regs = arm_compute_save_reg_mask ();
15074 if ((saved_regs & (1 << LR_REGNUM)) == 0)
15075 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
15076 else
15078 if (frame_pointer_needed)
15079 addr = plus_constant(hard_frame_pointer_rtx, -4);
15080 else
15082 /* LR will be the first saved register. */
15083 offsets = arm_get_frame_offsets ();
15084 delta = offsets->outgoing_args - (offsets->frame + 4);
15087 if (delta >= 4096)
15089 emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
15090 GEN_INT (delta & ~4095)));
15091 addr = scratch;
15092 delta &= 4095;
15094 else
15095 addr = stack_pointer_rtx;
15097 addr = plus_constant (addr, delta);
15099 emit_move_insn (gen_frame_mem (Pmode, addr), source);
15104 void
15105 thumb_set_return_address (rtx source, rtx scratch)
15107 arm_stack_offsets *offsets;
15108 HOST_WIDE_INT delta;
15109 int reg;
15110 rtx addr;
15111 unsigned long mask;
15113 emit_insn (gen_rtx_USE (VOIDmode, source));
15115 mask = thumb_compute_save_reg_mask ();
15116 if (mask & (1 << LR_REGNUM))
15118 offsets = arm_get_frame_offsets ();
15120 /* Find the saved regs. */
15121 if (frame_pointer_needed)
15123 delta = offsets->soft_frame - offsets->saved_args;
15124 reg = THUMB_HARD_FRAME_POINTER_REGNUM;
15126 else
15128 delta = offsets->outgoing_args - offsets->saved_args;
15129 reg = SP_REGNUM;
15131 /* Allow for the stack frame. */
15132 if (TARGET_BACKTRACE)
15133 delta -= 16;
15134 /* The link register is always the first saved register. */
15135 delta -= 4;
15137 /* Construct the address. */
15138 addr = gen_rtx_REG (SImode, reg);
15139 if ((reg != SP_REGNUM && delta >= 128)
15140 || delta >= 1024)
15142 emit_insn (gen_movsi (scratch, GEN_INT (delta)));
15143 emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
15144 addr = scratch;
15146 else
15147 addr = plus_constant (addr, delta);
15149 emit_move_insn (gen_frame_mem (Pmode, addr), source);
15151 else
15152 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
15155 /* Implements target hook vector_mode_supported_p. */
15156 bool
15157 arm_vector_mode_supported_p (enum machine_mode mode)
15159 if ((mode == V2SImode)
15160 || (mode == V4HImode)
15161 || (mode == V8QImode))
15162 return true;
15164 return false;
15167 /* Implement TARGET_SHIFT_TRUNCATION_MASK. SImode shifts use normal
15168 ARM insns and therefore guarantee that the shift count is modulo 256.
15169 DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
15170 guarantee no particular behavior for out-of-range counts. */
15172 static unsigned HOST_WIDE_INT
15173 arm_shift_truncation_mask (enum machine_mode mode)
15175 return mode == SImode ? 255 : 0;
15179 /* Map internal gcc register numbers to DWARF2 register numbers. */
15181 unsigned int
15182 arm_dbx_register_number (unsigned int regno)
15184 if (regno < 16)
15185 return regno;
15187 /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
15188 compatibility. The EABI defines them as registers 96-103. */
15189 if (IS_FPA_REGNUM (regno))
15190 return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
15192 if (IS_VFP_REGNUM (regno))
15193 return 64 + regno - FIRST_VFP_REGNUM;
15195 if (IS_IWMMXT_GR_REGNUM (regno))
15196 return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
15198 if (IS_IWMMXT_REGNUM (regno))
15199 return 112 + regno - FIRST_IWMMXT_REGNUM;
15201 gcc_unreachable ();
15205 #ifdef TARGET_UNWIND_INFO
15206 /* Emit unwind directives for a store-multiple instruction. This should
15207 only ever be generated by the function prologue code, so we expect it
15208 to have a particular form. */
15210 static void
15211 arm_unwind_emit_stm (FILE * asm_out_file, rtx p)
15213 int i;
15214 HOST_WIDE_INT offset;
15215 HOST_WIDE_INT nregs;
15216 int reg_size;
15217 unsigned reg;
15218 unsigned lastreg;
15219 rtx e;
15221 /* First insn will adjust the stack pointer. */
15222 e = XVECEXP (p, 0, 0);
15223 if (GET_CODE (e) != SET
15224 || GET_CODE (XEXP (e, 0)) != REG
15225 || REGNO (XEXP (e, 0)) != SP_REGNUM
15226 || GET_CODE (XEXP (e, 1)) != PLUS)
15227 abort ();
15229 offset = -INTVAL (XEXP (XEXP (e, 1), 1));
15230 nregs = XVECLEN (p, 0) - 1;
15232 reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
15233 if (reg < 16)
15235 /* The function prologue may also push pc, but not annotate it as it is
15236 never restored. We turn this into a stack pointer adjustment. */
15237 if (nregs * 4 == offset - 4)
15239 fprintf (asm_out_file, "\t.pad #4\n");
15240 offset -= 4;
15242 reg_size = 4;
15244 else if (IS_VFP_REGNUM (reg))
15246 /* FPA register saves use an additional word. */
15247 offset -= 4;
15248 reg_size = 8;
15250 else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
15252 /* FPA registers are done differently. */
15253 asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
15254 return;
15256 else
15257 /* Unknown register type. */
15258 abort ();
15260 /* If the stack increment doesn't match the size of the saved registers,
15261 something has gone horribly wrong. */
15262 if (offset != nregs * reg_size)
15263 abort ();
15265 fprintf (asm_out_file, "\t.save {");
15267 offset = 0;
15268 lastreg = 0;
15269 /* The remaining insns will describe the stores. */
15270 for (i = 1; i <= nregs; i++)
15272 /* Expect (set (mem <addr>) (reg)).
15273 Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)). */
15274 e = XVECEXP (p, 0, i);
15275 if (GET_CODE (e) != SET
15276 || GET_CODE (XEXP (e, 0)) != MEM
15277 || GET_CODE (XEXP (e, 1)) != REG)
15278 abort ();
15280 reg = REGNO (XEXP (e, 1));
15281 if (reg < lastreg)
15282 abort ();
15284 if (i != 1)
15285 fprintf (asm_out_file, ", ");
15286 /* We can't use %r for vfp because we need to use the
15287 double precision register names. */
15288 if (IS_VFP_REGNUM (reg))
15289 asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
15290 else
15291 asm_fprintf (asm_out_file, "%r", reg);
15293 #ifdef ENABLE_CHECKING
15294 /* Check that the addresses are consecutive. */
15295 e = XEXP (XEXP (e, 0), 0);
15296 if (GET_CODE (e) == PLUS)
15298 offset += reg_size;
15299 if (GET_CODE (XEXP (e, 0)) != REG
15300 || REGNO (XEXP (e, 0)) != SP_REGNUM
15301 || GET_CODE (XEXP (e, 1)) != CONST_INT
15302 || offset != INTVAL (XEXP (e, 1)))
15303 abort ();
15305 else if (i != 1
15306 || GET_CODE (e) != REG
15307 || REGNO (e) != SP_REGNUM)
15308 abort ();
15309 #endif
15311 fprintf (asm_out_file, "}\n");
15314 /* Emit unwind directives for a SET. */
15316 static void
15317 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
15319 rtx e0;
15320 rtx e1;
15322 e0 = XEXP (p, 0);
15323 e1 = XEXP (p, 1);
15324 switch (GET_CODE (e0))
15326 case MEM:
15327 /* Pushing a single register. */
15328 if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
15329 || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
15330 || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
15331 abort ();
15333 asm_fprintf (asm_out_file, "\t.save ");
15334 if (IS_VFP_REGNUM (REGNO (e1)))
15335 asm_fprintf(asm_out_file, "{d%d}\n",
15336 (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
15337 else
15338 asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
15339 break;
15341 case REG:
15342 if (REGNO (e0) == SP_REGNUM)
15344 /* A stack increment. */
15345 if (GET_CODE (e1) != PLUS
15346 || GET_CODE (XEXP (e1, 0)) != REG
15347 || REGNO (XEXP (e1, 0)) != SP_REGNUM
15348 || GET_CODE (XEXP (e1, 1)) != CONST_INT)
15349 abort ();
15351 asm_fprintf (asm_out_file, "\t.pad #%wd\n",
15352 -INTVAL (XEXP (e1, 1)));
15354 else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
15356 HOST_WIDE_INT offset;
15357 unsigned reg;
15359 if (GET_CODE (e1) == PLUS)
15361 if (GET_CODE (XEXP (e1, 0)) != REG
15362 || GET_CODE (XEXP (e1, 1)) != CONST_INT)
15363 abort ();
15364 reg = REGNO (XEXP (e1, 0));
15365 offset = INTVAL (XEXP (e1, 1));
15366 asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
15367 HARD_FRAME_POINTER_REGNUM, reg,
15368 INTVAL (XEXP (e1, 1)));
15370 else if (GET_CODE (e1) == REG)
15372 reg = REGNO (e1);
15373 asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
15374 HARD_FRAME_POINTER_REGNUM, reg);
15376 else
15377 abort ();
15379 else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
15381 /* Move from sp to reg. */
15382 asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
15384 else
15385 abort ();
15386 break;
15388 default:
15389 abort ();
15394 /* Emit unwind directives for the given insn. */
15396 static void
15397 arm_unwind_emit (FILE * asm_out_file, rtx insn)
15399 rtx pat;
15401 if (!ARM_EABI_UNWIND_TABLES)
15402 return;
15404 if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
15405 return;
15407 pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
15408 if (pat)
15409 pat = XEXP (pat, 0);
15410 else
15411 pat = PATTERN (insn);
15413 switch (GET_CODE (pat))
15415 case SET:
15416 arm_unwind_emit_set (asm_out_file, pat);
15417 break;
15419 case SEQUENCE:
15420 /* Store multiple. */
15421 arm_unwind_emit_stm (asm_out_file, pat);
15422 break;
15424 default:
15425 abort();
15430 /* Output a reference from a function exception table to the type_info
15431 object X. The EABI specifies that the symbol should be relocated by
15432 an R_ARM_TARGET2 relocation. */
15434 static bool
15435 arm_output_ttype (rtx x)
15437 fputs ("\t.word\t", asm_out_file);
15438 output_addr_const (asm_out_file, x);
15439 /* Use special relocations for symbol references. */
15440 if (GET_CODE (x) != CONST_INT)
15441 fputs ("(TARGET2)", asm_out_file);
15442 fputc ('\n', asm_out_file);
15444 return TRUE;
15446 #endif /* TARGET_UNWIND_INFO */
15449 /* Output unwind directives for the start/end of a function. */
15451 void
15452 arm_output_fn_unwind (FILE * f, bool prologue)
15454 if (!ARM_EABI_UNWIND_TABLES)
15455 return;
15457 if (prologue)
15458 fputs ("\t.fnstart\n", f);
15459 else
15460 fputs ("\t.fnend\n", f);
15463 static bool
15464 arm_emit_tls_decoration (FILE *fp, rtx x)
15466 enum tls_reloc reloc;
15467 rtx val;
15469 val = XVECEXP (x, 0, 0);
15470 reloc = INTVAL (XVECEXP (x, 0, 1));
15472 output_addr_const (fp, val);
15474 switch (reloc)
15476 case TLS_GD32:
15477 fputs ("(tlsgd)", fp);
15478 break;
15479 case TLS_LDM32:
15480 fputs ("(tlsldm)", fp);
15481 break;
15482 case TLS_LDO32:
15483 fputs ("(tlsldo)", fp);
15484 break;
15485 case TLS_IE32:
15486 fputs ("(gottpoff)", fp);
15487 break;
15488 case TLS_LE32:
15489 fputs ("(tpoff)", fp);
15490 break;
15491 default:
15492 gcc_unreachable ();
15495 switch (reloc)
15497 case TLS_GD32:
15498 case TLS_LDM32:
15499 case TLS_IE32:
15500 fputs (" + (. - ", fp);
15501 output_addr_const (fp, XVECEXP (x, 0, 2));
15502 fputs (" - ", fp);
15503 output_addr_const (fp, XVECEXP (x, 0, 3));
15504 fputc (')', fp);
15505 break;
15506 default:
15507 break;
15510 return TRUE;
15513 bool
15514 arm_output_addr_const_extra (FILE *fp, rtx x)
15516 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
15517 return arm_emit_tls_decoration (fp, x);
15518 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
15520 char label[256];
15521 int labelno = INTVAL (XVECEXP (x, 0, 0));
15523 ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
15524 assemble_name_raw (fp, label);
15526 return TRUE;
15528 else if (GET_CODE (x) == CONST_VECTOR)
15529 return arm_emit_vector_const (fp, x);
15531 return FALSE;
15534 #include "gt-arm.h"