* config/arm/arm.c (emit_sfm): Only emit a single frame adjustment.
[official-gcc.git] / gcc / config / arm / arm.c
blob66fc8f88735de37896f8453dea3af7134d4f3b1f
1 /* Output routines for GCC for ARM.
2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004 Free Software Foundation, Inc.
4 Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
5 and Martin Simmons (@harleqn.co.uk).
6 More major hacks by Richard Earnshaw (rearnsha@arm.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published
12 by the Free Software Foundation; either version 2, or (at your
13 option) any later version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to
22 the Free Software Foundation, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "obstack.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "real.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "output.h"
38 #include "insn-attr.h"
39 #include "flags.h"
40 #include "reload.h"
41 #include "function.h"
42 #include "expr.h"
43 #include "optabs.h"
44 #include "toplev.h"
45 #include "recog.h"
46 #include "ggc.h"
47 #include "except.h"
48 #include "c-pragma.h"
49 #include "integrate.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "target-def.h"
53 #include "debug.h"
54 #include "langhooks.h"
56 /* Forward definitions of types. */
57 typedef struct minipool_node Mnode;
58 typedef struct minipool_fixup Mfix;
60 const struct attribute_spec arm_attribute_table[];
62 /* Forward function declarations. */
63 static arm_stack_offsets *arm_get_frame_offsets (void);
64 static void arm_add_gc_roots (void);
65 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
66 HOST_WIDE_INT, rtx, rtx, int, int);
67 static unsigned bit_count (unsigned long);
68 static int arm_address_register_rtx_p (rtx, int);
69 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
70 static int thumb_base_register_rtx_p (rtx, enum machine_mode, int);
71 inline static int thumb_index_register_rtx_p (rtx, int);
72 static int thumb_far_jump_used_p (void);
73 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
74 static rtx emit_multi_reg_push (int);
75 static rtx emit_sfm (int, int);
76 #ifndef AOF_ASSEMBLER
77 static bool arm_assemble_integer (rtx, unsigned int, int);
78 #endif
79 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
80 static arm_cc get_arm_condition_code (rtx);
81 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
82 static rtx is_jump_table (rtx);
83 static const char *output_multi_immediate (rtx *, const char *, const char *,
84 int, HOST_WIDE_INT);
85 static void print_multi_reg (FILE *, const char *, int, int);
86 static const char *shift_op (rtx, HOST_WIDE_INT *);
87 static struct machine_function *arm_init_machine_status (void);
88 static int number_of_first_bit_set (int);
89 static void replace_symbols_in_block (tree, rtx, rtx);
90 static void thumb_exit (FILE *, int, rtx);
91 static void thumb_pushpop (FILE *, int, int, int *, int);
92 static rtx is_jump_table (rtx);
93 static HOST_WIDE_INT get_jump_table_size (rtx);
94 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
95 static Mnode *add_minipool_forward_ref (Mfix *);
96 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
97 static Mnode *add_minipool_backward_ref (Mfix *);
98 static void assign_minipool_offsets (Mfix *);
99 static void arm_print_value (FILE *, rtx);
100 static void dump_minipool (rtx);
101 static int arm_barrier_cost (rtx);
102 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
103 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
104 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
105 rtx);
106 static void arm_reorg (void);
107 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
108 static int current_file_function_operand (rtx);
109 static unsigned long arm_compute_save_reg0_reg12_mask (void);
110 static unsigned long arm_compute_save_reg_mask (void);
111 static unsigned long arm_isr_value (tree);
112 static unsigned long arm_compute_func_type (void);
113 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
114 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
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_slowmul_rtx_costs (rtx, int, int, int *);
129 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
130 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
131 static bool arm_9e_rtx_costs (rtx, int, int, int *);
132 static int arm_address_cost (rtx);
133 static bool arm_memory_load_p (rtx);
134 static bool arm_cirrus_insn_p (rtx);
135 static void cirrus_reorg (rtx);
136 static void arm_init_builtins (void);
137 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
138 static void arm_init_iwmmxt_builtins (void);
139 static rtx safe_vector_operand (rtx, enum machine_mode);
140 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
141 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
142 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
143 static void emit_constant_insn (rtx cond, rtx pattern);
145 #ifdef OBJECT_FORMAT_ELF
146 static void arm_elf_asm_named_section (const char *, unsigned int);
147 #endif
148 #ifndef ARM_PE
149 static void arm_encode_section_info (tree, rtx, int);
150 #endif
151 #ifdef AOF_ASSEMBLER
152 static void aof_globalize_label (FILE *, const char *);
153 static void aof_dump_imports (FILE *);
154 static void aof_dump_pic_table (FILE *);
155 static void aof_file_start (void);
156 static void aof_file_end (void);
157 #endif
158 static rtx arm_struct_value_rtx (tree, int);
159 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
160 tree, int *, int);
161 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
162 enum machine_mode, tree, bool);
163 static bool arm_promote_prototypes (tree);
164 static bool arm_default_short_enums (void);
165 static bool arm_align_anon_bitfield (void);
167 static tree arm_cxx_guard_type (void);
168 static bool arm_cxx_guard_mask_bit (void);
169 static tree arm_get_cookie_size (tree);
170 static bool arm_cookie_has_size (void);
173 /* Initialize the GCC target structure. */
174 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
175 #undef TARGET_MERGE_DECL_ATTRIBUTES
176 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
177 #endif
179 #undef TARGET_ATTRIBUTE_TABLE
180 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
182 #ifdef AOF_ASSEMBLER
183 #undef TARGET_ASM_BYTE_OP
184 #define TARGET_ASM_BYTE_OP "\tDCB\t"
185 #undef TARGET_ASM_ALIGNED_HI_OP
186 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
187 #undef TARGET_ASM_ALIGNED_SI_OP
188 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
189 #undef TARGET_ASM_GLOBALIZE_LABEL
190 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
191 #undef TARGET_ASM_FILE_START
192 #define TARGET_ASM_FILE_START aof_file_start
193 #undef TARGET_ASM_FILE_END
194 #define TARGET_ASM_FILE_END aof_file_end
195 #else
196 #undef TARGET_ASM_ALIGNED_SI_OP
197 #define TARGET_ASM_ALIGNED_SI_OP NULL
198 #undef TARGET_ASM_INTEGER
199 #define TARGET_ASM_INTEGER arm_assemble_integer
200 #endif
202 #undef TARGET_ASM_FUNCTION_PROLOGUE
203 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
205 #undef TARGET_ASM_FUNCTION_EPILOGUE
206 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
208 #undef TARGET_COMP_TYPE_ATTRIBUTES
209 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
211 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
212 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
214 #undef TARGET_SCHED_ADJUST_COST
215 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
217 #undef TARGET_ENCODE_SECTION_INFO
218 #ifdef ARM_PE
219 #define TARGET_ENCODE_SECTION_INFO arm_pe_encode_section_info
220 #else
221 #define TARGET_ENCODE_SECTION_INFO arm_encode_section_info
222 #endif
224 #undef TARGET_STRIP_NAME_ENCODING
225 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
227 #undef TARGET_ASM_INTERNAL_LABEL
228 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
230 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
231 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
233 #undef TARGET_ASM_OUTPUT_MI_THUNK
234 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
235 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
236 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
238 /* This will be overridden in arm_override_options. */
239 #undef TARGET_RTX_COSTS
240 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
241 #undef TARGET_ADDRESS_COST
242 #define TARGET_ADDRESS_COST arm_address_cost
244 #undef TARGET_MACHINE_DEPENDENT_REORG
245 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
247 #undef TARGET_INIT_BUILTINS
248 #define TARGET_INIT_BUILTINS arm_init_builtins
249 #undef TARGET_EXPAND_BUILTIN
250 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
252 #undef TARGET_PROMOTE_FUNCTION_ARGS
253 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
254 #undef TARGET_PROMOTE_FUNCTION_RETURN
255 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
256 #undef TARGET_PROMOTE_PROTOTYPES
257 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
258 #undef TARGET_PASS_BY_REFERENCE
259 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
261 #undef TARGET_STRUCT_VALUE_RTX
262 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
264 #undef TARGET_SETUP_INCOMING_VARARGS
265 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
267 #undef TARGET_DEFAULT_SHORT_ENUMS
268 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
270 #undef TARGET_ALIGN_ANON_BITFIELD
271 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
273 #undef TARGET_CXX_GUARD_TYPE
274 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
276 #undef TARGET_CXX_GUARD_MASK_BIT
277 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
279 #undef TARGET_CXX_GET_COOKIE_SIZE
280 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
282 #undef TARGET_CXX_COOKIE_HAS_SIZE
283 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
285 struct gcc_target targetm = TARGET_INITIALIZER;
287 /* Obstack for minipool constant handling. */
288 static struct obstack minipool_obstack;
289 static char * minipool_startobj;
291 /* The maximum number of insns skipped which
292 will be conditionalised if possible. */
293 static int max_insns_skipped = 5;
295 extern FILE * asm_out_file;
297 /* True if we are currently building a constant table. */
298 int making_const_table;
300 /* Define the information needed to generate branch insns. This is
301 stored from the compare operation. */
302 rtx arm_compare_op0, arm_compare_op1;
304 /* The processor for which instructions should be scheduled. */
305 enum processor_type arm_tune = arm_none;
307 /* Which floating point model to use. */
308 enum arm_fp_model arm_fp_model;
310 /* Which floating point hardware is available. */
311 enum fputype arm_fpu_arch;
313 /* Which floating point hardware to schedule for. */
314 enum fputype arm_fpu_tune;
316 /* Whether to use floating point hardware. */
317 enum float_abi_type arm_float_abi;
319 /* Which ABI to use. */
320 enum arm_abi_type arm_abi;
322 /* Set by the -mfpu=... option. */
323 const char * target_fpu_name = NULL;
325 /* Set by the -mfpe=... option. */
326 const char * target_fpe_name = NULL;
328 /* Set by the -mfloat-abi=... option. */
329 const char * target_float_abi_name = NULL;
331 /* Set by the -mabi=... option. */
332 const char * target_abi_name = NULL;
334 /* Used to parse -mstructure_size_boundary command line option. */
335 const char * structure_size_string = NULL;
336 int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
338 /* Bit values used to identify processor capabilities. */
339 #define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
340 #define FL_ARCH3M (1 << 1) /* Extended multiply */
341 #define FL_MODE26 (1 << 2) /* 26-bit mode support */
342 #define FL_MODE32 (1 << 3) /* 32-bit mode support */
343 #define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
344 #define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
345 #define FL_THUMB (1 << 6) /* Thumb aware */
346 #define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
347 #define FL_STRONG (1 << 8) /* StrongARM */
348 #define FL_ARCH5E (1 << 9) /* DSP extensions to v5 */
349 #define FL_XSCALE (1 << 10) /* XScale */
350 #define FL_CIRRUS (1 << 11) /* Cirrus/DSP. */
351 #define FL_ARCH6 (1 << 12) /* Architecture rel 6. Adds
352 media instructions. */
353 #define FL_VFPV2 (1 << 13) /* Vector Floating Point V2. */
355 #define FL_IWMMXT (1 << 29) /* XScale v2 or "Intel Wireless MMX technology". */
357 #define FL_FOR_ARCH2 0
358 #define FL_FOR_ARCH3 FL_MODE32
359 #define FL_FOR_ARCH3M (FL_FOR_ARCH3 | FL_ARCH3M)
360 #define FL_FOR_ARCH4 (FL_FOR_ARCH3M | FL_ARCH4)
361 #define FL_FOR_ARCH4T (FL_FOR_ARCH4 | FL_THUMB)
362 #define FL_FOR_ARCH5 (FL_FOR_ARCH4 | FL_ARCH5)
363 #define FL_FOR_ARCH5T (FL_FOR_ARCH5 | FL_THUMB)
364 #define FL_FOR_ARCH5E (FL_FOR_ARCH5 | FL_ARCH5E)
365 #define FL_FOR_ARCH5TE (FL_FOR_ARCH5E | FL_THUMB)
366 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
367 #define FL_FOR_ARCH6 (FL_FOR_ARCH5TE | FL_ARCH6)
368 #define FL_FOR_ARCH6J FL_FOR_ARCH6
370 /* The bits in this mask specify which
371 instructions we are allowed to generate. */
372 static unsigned long insn_flags = 0;
374 /* The bits in this mask specify which instruction scheduling options should
375 be used. */
376 static unsigned long tune_flags = 0;
378 /* The following are used in the arm.md file as equivalents to bits
379 in the above two flag variables. */
381 /* Nonzero if this chip supports the ARM Architecture 3M extensions. */
382 int arm_arch3m = 0;
384 /* Nonzero if this chip supports the ARM Architecture 4 extensions. */
385 int arm_arch4 = 0;
387 /* Nonzero if this chip supports the ARM Architecture 4t extensions. */
388 int arm_arch4t = 0;
390 /* Nonzero if this chip supports the ARM Architecture 5 extensions. */
391 int arm_arch5 = 0;
393 /* Nonzero if this chip supports the ARM Architecture 5E extensions. */
394 int arm_arch5e = 0;
396 /* Nonzero if this chip supports the ARM Architecture 6 extensions. */
397 int arm_arch6 = 0;
399 /* Nonzero if this chip can benefit from load scheduling. */
400 int arm_ld_sched = 0;
402 /* Nonzero if this chip is a StrongARM. */
403 int arm_is_strong = 0;
405 /* Nonzero if this chip is a Cirrus variant. */
406 int arm_arch_cirrus = 0;
408 /* Nonzero if this chip supports Intel Wireless MMX technology. */
409 int arm_arch_iwmmxt = 0;
411 /* Nonzero if this chip is an XScale. */
412 int arm_arch_xscale = 0;
414 /* Nonzero if tuning for XScale */
415 int arm_tune_xscale = 0;
417 /* Nonzero if this chip is an ARM6 or an ARM7. */
418 int arm_is_6_or_7 = 0;
420 /* Nonzero if generating Thumb instructions. */
421 int thumb_code = 0;
423 /* Nonzero if we should define __THUMB_INTERWORK__ in the
424 preprocessor.
425 XXX This is a bit of a hack, it's intended to help work around
426 problems in GLD which doesn't understand that armv5t code is
427 interworking clean. */
428 int arm_cpp_interwork = 0;
430 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
431 must report the mode of the memory reference from PRINT_OPERAND to
432 PRINT_OPERAND_ADDRESS. */
433 enum machine_mode output_memory_reference_mode;
435 /* The register number to be used for the PIC offset register. */
436 const char * arm_pic_register_string = NULL;
437 int arm_pic_register = INVALID_REGNUM;
439 /* Set to 1 when a return insn is output, this means that the epilogue
440 is not needed. */
441 int return_used_this_function;
443 /* Set to 1 after arm_reorg has started. Reset to start at the start of
444 the next function. */
445 static int after_arm_reorg = 0;
447 /* The maximum number of insns to be used when loading a constant. */
448 static int arm_constant_limit = 3;
450 /* For an explanation of these variables, see final_prescan_insn below. */
451 int arm_ccfsm_state;
452 enum arm_cond_code arm_current_cc;
453 rtx arm_target_insn;
454 int arm_target_label;
456 /* The condition codes of the ARM, and the inverse function. */
457 static const char * const arm_condition_codes[] =
459 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
460 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
463 #define streq(string1, string2) (strcmp (string1, string2) == 0)
465 /* Initialization code. */
467 struct processors
469 const char *const name;
470 enum processor_type core;
471 const char *arch;
472 const unsigned long flags;
473 bool (* rtx_costs) (rtx, int, int, int *);
476 /* Not all of these give usefully different compilation alternatives,
477 but there is no simple way of generalizing them. */
478 static const struct processors all_cores[] =
480 /* ARM Cores */
481 #define ARM_CORE(NAME, ARCH, FLAGS, COSTS) \
482 {#NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
483 #include "arm-cores.def"
484 #undef ARM_CORE
485 {NULL, arm_none, NULL, 0, NULL}
488 static const struct processors all_architectures[] =
490 /* ARM Architectures */
491 /* We don't specify rtx_costs here as it will be figured out
492 from the core. */
494 {"armv2", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
495 {"armv2a", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
496 {"armv3", arm6, "3", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
497 {"armv3m", arm7m, "3M", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
498 {"armv4", arm7tdmi, "4", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
499 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
500 implementations that support it, so we will leave it out for now. */
501 {"armv4t", arm7tdmi, "4T", FL_CO_PROC | FL_FOR_ARCH4T, NULL},
502 {"armv5", arm10tdmi, "5", FL_CO_PROC | FL_FOR_ARCH5, NULL},
503 {"armv5t", arm10tdmi, "5T", FL_CO_PROC | FL_FOR_ARCH5T, NULL},
504 {"armv5e", arm1026ejs, "5E", FL_CO_PROC | FL_FOR_ARCH5E, NULL},
505 {"armv5te", arm1026ejs, "5TE", FL_CO_PROC | FL_FOR_ARCH5TE, NULL},
506 {"armv6", arm1136js, "6", FL_CO_PROC | FL_FOR_ARCH6, NULL},
507 {"armv6j", arm1136js, "6J", FL_CO_PROC | FL_FOR_ARCH6J, NULL},
508 {"ep9312", ep9312, "4T", FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
509 {"iwmmxt", iwmmxt, "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
510 {NULL, arm_none, NULL, 0 , NULL}
513 /* This is a magic structure. The 'string' field is magically filled in
514 with a pointer to the value specified by the user on the command line
515 assuming that the user has specified such a value. */
517 struct arm_cpu_select arm_select[] =
519 /* string name processors */
520 { NULL, "-mcpu=", all_cores },
521 { NULL, "-march=", all_architectures },
522 { NULL, "-mtune=", all_cores }
526 /* The name of the proprocessor macro to define for this architecture. */
528 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
530 struct fpu_desc
532 const char * name;
533 enum fputype fpu;
537 /* Available values for for -mfpu=. */
539 static const struct fpu_desc all_fpus[] =
541 {"fpa", FPUTYPE_FPA},
542 {"fpe2", FPUTYPE_FPA_EMU2},
543 {"fpe3", FPUTYPE_FPA_EMU2},
544 {"maverick", FPUTYPE_MAVERICK},
545 {"vfp", FPUTYPE_VFP}
549 /* Floating point models used by the different hardware.
550 See fputype in arm.h. */
552 static const enum fputype fp_model_for_fpu[] =
554 /* No FP hardware. */
555 ARM_FP_MODEL_UNKNOWN, /* FPUTYPE_NONE */
556 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA */
557 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU2 */
558 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU3 */
559 ARM_FP_MODEL_MAVERICK, /* FPUTYPE_MAVERICK */
560 ARM_FP_MODEL_VFP /* FPUTYPE_VFP */
564 struct float_abi
566 const char * name;
567 enum float_abi_type abi_type;
571 /* Available values for -mfloat-abi=. */
573 static const struct float_abi all_float_abis[] =
575 {"soft", ARM_FLOAT_ABI_SOFT},
576 {"softfp", ARM_FLOAT_ABI_SOFTFP},
577 {"hard", ARM_FLOAT_ABI_HARD}
581 struct abi_name
583 const char *name;
584 enum arm_abi_type abi_type;
588 /* Available values for -mabi=. */
590 static const struct abi_name arm_all_abis[] =
592 {"apcs-gnu", ARM_ABI_APCS},
593 {"atpcs", ARM_ABI_ATPCS},
594 {"aapcs", ARM_ABI_AAPCS},
595 {"iwmmxt", ARM_ABI_IWMMXT}
598 /* Return the number of bits set in VALUE. */
599 static unsigned
600 bit_count (unsigned long value)
602 unsigned long count = 0;
604 while (value)
606 count++;
607 value &= value - 1; /* Clear the least-significant set bit. */
610 return count;
613 /* Fix up any incompatible options that the user has specified.
614 This has now turned into a maze. */
615 void
616 arm_override_options (void)
618 unsigned i;
620 /* Set up the flags based on the cpu/architecture selected by the user. */
621 for (i = ARRAY_SIZE (arm_select); i--;)
623 struct arm_cpu_select * ptr = arm_select + i;
625 if (ptr->string != NULL && ptr->string[0] != '\0')
627 const struct processors * sel;
629 for (sel = ptr->processors; sel->name != NULL; sel++)
630 if (streq (ptr->string, sel->name))
632 /* Set the architecture define. */
633 if (i != 2)
634 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
636 /* Determine the processor core for which we should
637 tune code-generation. */
638 if (/* -mcpu= is a sensible default. */
639 i == 0
640 /* If -march= is used, and -mcpu= has not been used,
641 assume that we should tune for a representative
642 CPU from that architecture. */
643 || i == 1
644 /* -mtune= overrides -mcpu= and -march=. */
645 || i == 2)
646 arm_tune = (enum processor_type) (sel - ptr->processors);
648 if (i != 2)
650 /* If we have been given an architecture and a processor
651 make sure that they are compatible. We only generate
652 a warning though, and we prefer the CPU over the
653 architecture. */
654 if (insn_flags != 0 && (insn_flags ^ sel->flags))
655 warning ("switch -mcpu=%s conflicts with -march= switch",
656 ptr->string);
658 insn_flags = sel->flags;
661 break;
664 if (sel->name == NULL)
665 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
669 /* If the user did not specify a processor, choose one for them. */
670 if (insn_flags == 0)
672 const struct processors * sel;
673 unsigned int sought;
674 enum processor_type cpu;
676 cpu = TARGET_CPU_DEFAULT;
677 if (cpu == arm_none)
679 #ifdef SUBTARGET_CPU_DEFAULT
680 /* Use the subtarget default CPU if none was specified by
681 configure. */
682 cpu = SUBTARGET_CPU_DEFAULT;
683 #endif
684 /* Default to ARM6. */
685 if (cpu == arm_none)
686 cpu = arm6;
688 sel = &all_cores[cpu];
690 insn_flags = sel->flags;
692 /* Now check to see if the user has specified some command line
693 switch that require certain abilities from the cpu. */
694 sought = 0;
696 if (TARGET_INTERWORK || TARGET_THUMB)
698 sought |= (FL_THUMB | FL_MODE32);
700 /* There are no ARM processors that support both APCS-26 and
701 interworking. Therefore we force FL_MODE26 to be removed
702 from insn_flags here (if it was set), so that the search
703 below will always be able to find a compatible processor. */
704 insn_flags &= ~FL_MODE26;
707 if (sought != 0 && ((sought & insn_flags) != sought))
709 /* Try to locate a CPU type that supports all of the abilities
710 of the default CPU, plus the extra abilities requested by
711 the user. */
712 for (sel = all_cores; sel->name != NULL; sel++)
713 if ((sel->flags & sought) == (sought | insn_flags))
714 break;
716 if (sel->name == NULL)
718 unsigned current_bit_count = 0;
719 const struct processors * best_fit = NULL;
721 /* Ideally we would like to issue an error message here
722 saying that it was not possible to find a CPU compatible
723 with the default CPU, but which also supports the command
724 line options specified by the programmer, and so they
725 ought to use the -mcpu=<name> command line option to
726 override the default CPU type.
728 If we cannot find a cpu that has both the
729 characteristics of the default cpu and the given
730 command line options we scan the array again looking
731 for a best match. */
732 for (sel = all_cores; sel->name != NULL; sel++)
733 if ((sel->flags & sought) == sought)
735 unsigned count;
737 count = bit_count (sel->flags & insn_flags);
739 if (count >= current_bit_count)
741 best_fit = sel;
742 current_bit_count = count;
746 if (best_fit == NULL)
747 abort ();
748 else
749 sel = best_fit;
752 insn_flags = sel->flags;
754 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
755 if (arm_tune == arm_none)
756 arm_tune = (enum processor_type) (sel - all_cores);
759 /* The processor for which we should tune should now have been
760 chosen. */
761 if (arm_tune == arm_none)
762 abort ();
764 tune_flags = all_cores[(int)arm_tune].flags;
765 targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
767 /* Make sure that the processor choice does not conflict with any of the
768 other command line choices. */
769 if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
771 warning ("target CPU does not support interworking" );
772 target_flags &= ~ARM_FLAG_INTERWORK;
775 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
777 warning ("target CPU does not support THUMB instructions");
778 target_flags &= ~ARM_FLAG_THUMB;
781 if (TARGET_APCS_FRAME && TARGET_THUMB)
783 /* warning ("ignoring -mapcs-frame because -mthumb was used"); */
784 target_flags &= ~ARM_FLAG_APCS_FRAME;
787 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
788 from here where no function is being compiled currently. */
789 if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
790 && TARGET_ARM)
791 warning ("enabling backtrace support is only meaningful when compiling for the Thumb");
793 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
794 warning ("enabling callee interworking support is only meaningful when compiling for the Thumb");
796 if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
797 warning ("enabling caller interworking support is only meaningful when compiling for the Thumb");
799 if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
801 warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
802 target_flags |= ARM_FLAG_APCS_FRAME;
805 if (TARGET_POKE_FUNCTION_NAME)
806 target_flags |= ARM_FLAG_APCS_FRAME;
808 if (TARGET_APCS_REENT && flag_pic)
809 error ("-fpic and -mapcs-reent are incompatible");
811 if (TARGET_APCS_REENT)
812 warning ("APCS reentrant code not supported. Ignored");
814 /* If this target is normally configured to use APCS frames, warn if they
815 are turned off and debugging is turned on. */
816 if (TARGET_ARM
817 && write_symbols != NO_DEBUG
818 && !TARGET_APCS_FRAME
819 && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
820 warning ("-g with -mno-apcs-frame may not give sensible debugging");
822 /* If stack checking is disabled, we can use r10 as the PIC register,
823 which keeps r9 available. */
824 if (flag_pic)
825 arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
827 if (TARGET_APCS_FLOAT)
828 warning ("passing floating point arguments in fp regs not yet supported");
830 /* Initialize boolean versions of the flags, for use in the arm.md file. */
831 arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
832 arm_arch4 = (insn_flags & FL_ARCH4) != 0;
833 arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
834 arm_arch5 = (insn_flags & FL_ARCH5) != 0;
835 arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
836 arm_arch6 = (insn_flags & FL_ARCH6) != 0;
837 arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
838 arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
840 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
841 arm_is_strong = (tune_flags & FL_STRONG) != 0;
842 thumb_code = (TARGET_ARM == 0);
843 arm_is_6_or_7 = (((tune_flags & (FL_MODE26 | FL_MODE32))
844 && !(tune_flags & FL_ARCH4))) != 0;
845 arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
846 arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
848 /* V5 code we generate is completely interworking capable, so we turn off
849 TARGET_INTERWORK here to avoid many tests later on. */
851 /* XXX However, we must pass the right pre-processor defines to CPP
852 or GLD can get confused. This is a hack. */
853 if (TARGET_INTERWORK)
854 arm_cpp_interwork = 1;
856 if (arm_arch5)
857 target_flags &= ~ARM_FLAG_INTERWORK;
859 if (target_abi_name)
861 for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
863 if (streq (arm_all_abis[i].name, target_abi_name))
865 arm_abi = arm_all_abis[i].abi_type;
866 break;
869 if (i == ARRAY_SIZE (arm_all_abis))
870 error ("invalid ABI option: -mabi=%s", target_abi_name);
872 else
873 arm_abi = ARM_DEFAULT_ABI;
875 if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
876 error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
878 if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
879 error ("iwmmxt abi requires an iwmmxt capable cpu");
881 arm_fp_model = ARM_FP_MODEL_UNKNOWN;
882 if (target_fpu_name == NULL && target_fpe_name != NULL)
884 if (streq (target_fpe_name, "2"))
885 target_fpu_name = "fpe2";
886 else if (streq (target_fpe_name, "3"))
887 target_fpu_name = "fpe3";
888 else
889 error ("invalid floating point emulation option: -mfpe=%s",
890 target_fpe_name);
892 if (target_fpu_name != NULL)
894 /* The user specified a FPU. */
895 for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
897 if (streq (all_fpus[i].name, target_fpu_name))
899 arm_fpu_arch = all_fpus[i].fpu;
900 arm_fpu_tune = arm_fpu_arch;
901 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
902 break;
905 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
906 error ("invalid floating point option: -mfpu=%s", target_fpu_name);
908 else
910 #ifdef FPUTYPE_DEFAULT
911 /* Use the default if it is specified for this platform. */
912 arm_fpu_arch = FPUTYPE_DEFAULT;
913 arm_fpu_tune = FPUTYPE_DEFAULT;
914 #else
915 /* Pick one based on CPU type. */
916 /* ??? Some targets assume FPA is the default.
917 if ((insn_flags & FL_VFP) != 0)
918 arm_fpu_arch = FPUTYPE_VFP;
919 else
921 if (arm_arch_cirrus)
922 arm_fpu_arch = FPUTYPE_MAVERICK;
923 else
924 arm_fpu_arch = FPUTYPE_FPA_EMU2;
925 #endif
926 if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
927 arm_fpu_tune = FPUTYPE_FPA;
928 else
929 arm_fpu_tune = arm_fpu_arch;
930 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
931 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
932 abort ();
935 if (target_float_abi_name != NULL)
937 /* The user specified a FP ABI. */
938 for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
940 if (streq (all_float_abis[i].name, target_float_abi_name))
942 arm_float_abi = all_float_abis[i].abi_type;
943 break;
946 if (i == ARRAY_SIZE (all_float_abis))
947 error ("invalid floating point abi: -mfloat-abi=%s",
948 target_float_abi_name);
950 else
952 /* Use soft-float target flag. */
953 if (target_flags & ARM_FLAG_SOFT_FLOAT)
954 arm_float_abi = ARM_FLOAT_ABI_SOFT;
955 else
956 arm_float_abi = ARM_FLOAT_ABI_HARD;
959 if (arm_float_abi == ARM_FLOAT_ABI_SOFTFP)
960 sorry ("-mfloat-abi=softfp");
961 /* If soft-float is specified then don't use FPU. */
962 if (TARGET_SOFT_FLOAT)
963 arm_fpu_arch = FPUTYPE_NONE;
965 /* For arm2/3 there is no need to do any scheduling if there is only
966 a floating point emulator, or we are doing software floating-point. */
967 if ((TARGET_SOFT_FLOAT
968 || arm_fpu_tune == FPUTYPE_FPA_EMU2
969 || arm_fpu_tune == FPUTYPE_FPA_EMU3)
970 && (tune_flags & FL_MODE32) == 0)
971 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
973 /* Override the default structure alignment for AAPCS ABI. */
974 if (arm_abi == ARM_ABI_AAPCS)
975 arm_structure_size_boundary = 8;
977 if (structure_size_string != NULL)
979 int size = strtol (structure_size_string, NULL, 0);
981 if (size == 8 || size == 32
982 || (ARM_DOUBLEWORD_ALIGN && size == 64))
983 arm_structure_size_boundary = size;
984 else
985 warning ("structure size boundary can only be set to %s",
986 ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
989 if (arm_pic_register_string != NULL)
991 int pic_register = decode_reg_name (arm_pic_register_string);
993 if (!flag_pic)
994 warning ("-mpic-register= is useless without -fpic");
996 /* Prevent the user from choosing an obviously stupid PIC register. */
997 else if (pic_register < 0 || call_used_regs[pic_register]
998 || pic_register == HARD_FRAME_POINTER_REGNUM
999 || pic_register == STACK_POINTER_REGNUM
1000 || pic_register >= PC_REGNUM)
1001 error ("unable to use '%s' for PIC register", arm_pic_register_string);
1002 else
1003 arm_pic_register = pic_register;
1006 if (TARGET_THUMB && flag_schedule_insns)
1008 /* Don't warn since it's on by default in -O2. */
1009 flag_schedule_insns = 0;
1012 if (optimize_size)
1014 /* There's some dispute as to whether this should be 1 or 2. However,
1015 experiments seem to show that in pathological cases a setting of
1016 1 degrades less severely than a setting of 2. This could change if
1017 other parts of the compiler change their behavior. */
1018 arm_constant_limit = 1;
1020 /* If optimizing for size, bump the number of instructions that we
1021 are prepared to conditionally execute (even on a StrongARM). */
1022 max_insns_skipped = 6;
1024 else
1026 /* For processors with load scheduling, it never costs more than
1027 2 cycles to load a constant, and the load scheduler may well
1028 reduce that to 1. */
1029 if (tune_flags & FL_LDSCHED)
1030 arm_constant_limit = 1;
1032 /* On XScale the longer latency of a load makes it more difficult
1033 to achieve a good schedule, so it's faster to synthesize
1034 constants that can be done in two insns. */
1035 if (arm_tune_xscale)
1036 arm_constant_limit = 2;
1038 /* StrongARM has early execution of branches, so a sequence
1039 that is worth skipping is shorter. */
1040 if (arm_is_strong)
1041 max_insns_skipped = 3;
1044 /* Register global variables with the garbage collector. */
1045 arm_add_gc_roots ();
1048 static void
1049 arm_add_gc_roots (void)
1051 gcc_obstack_init(&minipool_obstack);
1052 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1055 /* A table of known ARM exception types.
1056 For use with the interrupt function attribute. */
1058 typedef struct
1060 const char *const arg;
1061 const unsigned long return_value;
1063 isr_attribute_arg;
1065 static const isr_attribute_arg isr_attribute_args [] =
1067 { "IRQ", ARM_FT_ISR },
1068 { "irq", ARM_FT_ISR },
1069 { "FIQ", ARM_FT_FIQ },
1070 { "fiq", ARM_FT_FIQ },
1071 { "ABORT", ARM_FT_ISR },
1072 { "abort", ARM_FT_ISR },
1073 { "ABORT", ARM_FT_ISR },
1074 { "abort", ARM_FT_ISR },
1075 { "UNDEF", ARM_FT_EXCEPTION },
1076 { "undef", ARM_FT_EXCEPTION },
1077 { "SWI", ARM_FT_EXCEPTION },
1078 { "swi", ARM_FT_EXCEPTION },
1079 { NULL, ARM_FT_NORMAL }
1082 /* Returns the (interrupt) function type of the current
1083 function, or ARM_FT_UNKNOWN if the type cannot be determined. */
1085 static unsigned long
1086 arm_isr_value (tree argument)
1088 const isr_attribute_arg * ptr;
1089 const char * arg;
1091 /* No argument - default to IRQ. */
1092 if (argument == NULL_TREE)
1093 return ARM_FT_ISR;
1095 /* Get the value of the argument. */
1096 if (TREE_VALUE (argument) == NULL_TREE
1097 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1098 return ARM_FT_UNKNOWN;
1100 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1102 /* Check it against the list of known arguments. */
1103 for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1104 if (streq (arg, ptr->arg))
1105 return ptr->return_value;
1107 /* An unrecognized interrupt type. */
1108 return ARM_FT_UNKNOWN;
1111 /* Computes the type of the current function. */
1113 static unsigned long
1114 arm_compute_func_type (void)
1116 unsigned long type = ARM_FT_UNKNOWN;
1117 tree a;
1118 tree attr;
1120 if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
1121 abort ();
1123 /* Decide if the current function is volatile. Such functions
1124 never return, and many memory cycles can be saved by not storing
1125 register values that will never be needed again. This optimization
1126 was added to speed up context switching in a kernel application. */
1127 if (optimize > 0
1128 && TREE_NOTHROW (current_function_decl)
1129 && TREE_THIS_VOLATILE (current_function_decl))
1130 type |= ARM_FT_VOLATILE;
1132 if (cfun->static_chain_decl != NULL)
1133 type |= ARM_FT_NESTED;
1135 attr = DECL_ATTRIBUTES (current_function_decl);
1137 a = lookup_attribute ("naked", attr);
1138 if (a != NULL_TREE)
1139 type |= ARM_FT_NAKED;
1141 if (cfun->machine->eh_epilogue_sp_ofs != NULL_RTX)
1142 type |= ARM_FT_EXCEPTION_HANDLER;
1143 else
1145 a = lookup_attribute ("isr", attr);
1146 if (a == NULL_TREE)
1147 a = lookup_attribute ("interrupt", attr);
1149 if (a == NULL_TREE)
1150 type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1151 else
1152 type |= arm_isr_value (TREE_VALUE (a));
1155 return type;
1158 /* Returns the type of the current function. */
1160 unsigned long
1161 arm_current_func_type (void)
1163 if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1164 cfun->machine->func_type = arm_compute_func_type ();
1166 return cfun->machine->func_type;
1169 /* Return 1 if it is possible to return using a single instruction.
1170 If SIBLING is non-null, this is a test for a return before a sibling
1171 call. SIBLING is the call insn, so we can examine its register usage. */
1174 use_return_insn (int iscond, rtx sibling)
1176 int regno;
1177 unsigned int func_type;
1178 unsigned long saved_int_regs;
1179 unsigned HOST_WIDE_INT stack_adjust;
1180 arm_stack_offsets *offsets;
1182 /* Never use a return instruction before reload has run. */
1183 if (!reload_completed)
1184 return 0;
1186 func_type = arm_current_func_type ();
1188 /* Naked functions and volatile functions need special
1189 consideration. */
1190 if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1191 return 0;
1193 /* So do interrupt functions that use the frame pointer. */
1194 if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1195 return 0;
1197 offsets = arm_get_frame_offsets ();
1198 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1200 /* As do variadic functions. */
1201 if (current_function_pretend_args_size
1202 || cfun->machine->uses_anonymous_args
1203 /* Or if the function calls __builtin_eh_return () */
1204 || ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
1205 /* Or if the function calls alloca */
1206 || current_function_calls_alloca
1207 /* Or if there is a stack adjustment. However, if the stack pointer
1208 is saved on the stack, we can use a pre-incrementing stack load. */
1209 || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1210 return 0;
1212 saved_int_regs = arm_compute_save_reg_mask ();
1214 /* Unfortunately, the insn
1216 ldmib sp, {..., sp, ...}
1218 triggers a bug on most SA-110 based devices, such that the stack
1219 pointer won't be correctly restored if the instruction takes a
1220 page fault. We work around this problem by popping r3 along with
1221 the other registers, since that is never slower than executing
1222 another instruction.
1224 We test for !arm_arch5 here, because code for any architecture
1225 less than this could potentially be run on one of the buggy
1226 chips. */
1227 if (stack_adjust == 4 && !arm_arch5)
1229 /* Validate that r3 is a call-clobbered register (always true in
1230 the default abi) ... */
1231 if (!call_used_regs[3])
1232 return 0;
1234 /* ... that it isn't being used for a return value (always true
1235 until we implement return-in-regs), or for a tail-call
1236 argument ... */
1237 if (sibling)
1239 if (GET_CODE (sibling) != CALL_INSN)
1240 abort ();
1242 if (find_regno_fusage (sibling, USE, 3))
1243 return 0;
1246 /* ... and that there are no call-saved registers in r0-r2
1247 (always true in the default ABI). */
1248 if (saved_int_regs & 0x7)
1249 return 0;
1252 /* Can't be done if interworking with Thumb, and any registers have been
1253 stacked. */
1254 if (TARGET_INTERWORK && saved_int_regs != 0)
1255 return 0;
1257 /* On StrongARM, conditional returns are expensive if they aren't
1258 taken and multiple registers have been stacked. */
1259 if (iscond && arm_is_strong)
1261 /* Conditional return when just the LR is stored is a simple
1262 conditional-load instruction, that's not expensive. */
1263 if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1264 return 0;
1266 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1267 return 0;
1270 /* If there are saved registers but the LR isn't saved, then we need
1271 two instructions for the return. */
1272 if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1273 return 0;
1275 /* Can't be done if any of the FPA regs are pushed,
1276 since this also requires an insn. */
1277 if (TARGET_HARD_FLOAT && TARGET_FPA)
1278 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1279 if (regs_ever_live[regno] && !call_used_regs[regno])
1280 return 0;
1282 /* Likewise VFP regs. */
1283 if (TARGET_HARD_FLOAT && TARGET_VFP)
1284 for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1285 if (regs_ever_live[regno] && !call_used_regs[regno])
1286 return 0;
1288 if (TARGET_REALLY_IWMMXT)
1289 for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1290 if (regs_ever_live[regno] && ! call_used_regs [regno])
1291 return 0;
1293 return 1;
1296 /* Return TRUE if int I is a valid immediate ARM constant. */
1299 const_ok_for_arm (HOST_WIDE_INT i)
1301 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1303 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1304 be all zero, or all one. */
1305 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1306 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1307 != ((~(unsigned HOST_WIDE_INT) 0)
1308 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1309 return FALSE;
1311 /* Fast return for 0 and powers of 2 */
1312 if ((i & (i - 1)) == 0)
1313 return TRUE;
1317 if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1318 return TRUE;
1319 mask =
1320 (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1321 >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1323 while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1325 return FALSE;
1328 /* Return true if I is a valid constant for the operation CODE. */
1329 static int
1330 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1332 if (const_ok_for_arm (i))
1333 return 1;
1335 switch (code)
1337 case PLUS:
1338 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1340 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
1341 case XOR:
1342 case IOR:
1343 return 0;
1345 case AND:
1346 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1348 default:
1349 abort ();
1353 /* Emit a sequence of insns to handle a large constant.
1354 CODE is the code of the operation required, it can be any of SET, PLUS,
1355 IOR, AND, XOR, MINUS;
1356 MODE is the mode in which the operation is being performed;
1357 VAL is the integer to operate on;
1358 SOURCE is the other operand (a register, or a null-pointer for SET);
1359 SUBTARGETS means it is safe to create scratch registers if that will
1360 either produce a simpler sequence, or we will want to cse the values.
1361 Return value is the number of insns emitted. */
1364 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1365 HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1367 rtx cond;
1369 if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1370 cond = COND_EXEC_TEST (PATTERN (insn));
1371 else
1372 cond = NULL_RTX;
1374 if (subtargets || code == SET
1375 || (GET_CODE (target) == REG && GET_CODE (source) == REG
1376 && REGNO (target) != REGNO (source)))
1378 /* After arm_reorg has been called, we can't fix up expensive
1379 constants by pushing them into memory so we must synthesize
1380 them in-line, regardless of the cost. This is only likely to
1381 be more costly on chips that have load delay slots and we are
1382 compiling without running the scheduler (so no splitting
1383 occurred before the final instruction emission).
1385 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1387 if (!after_arm_reorg
1388 && !cond
1389 && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1390 1, 0)
1391 > arm_constant_limit + (code != SET)))
1393 if (code == SET)
1395 /* Currently SET is the only monadic value for CODE, all
1396 the rest are diadic. */
1397 emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1398 return 1;
1400 else
1402 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1404 emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1405 /* For MINUS, the value is subtracted from, since we never
1406 have subtraction of a constant. */
1407 if (code == MINUS)
1408 emit_insn (gen_rtx_SET (VOIDmode, target,
1409 gen_rtx_MINUS (mode, temp, source)));
1410 else
1411 emit_insn (gen_rtx_SET (VOIDmode, target,
1412 gen_rtx_fmt_ee (code, mode, source, temp)));
1413 return 2;
1418 return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1422 static int
1423 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1425 HOST_WIDE_INT temp1;
1426 int num_insns = 0;
1429 int end;
1431 if (i <= 0)
1432 i += 32;
1433 if (remainder & (3 << (i - 2)))
1435 end = i - 8;
1436 if (end < 0)
1437 end += 32;
1438 temp1 = remainder & ((0x0ff << end)
1439 | ((i < end) ? (0xff >> (32 - end)) : 0));
1440 remainder &= ~temp1;
1441 num_insns++;
1442 i -= 6;
1444 i -= 2;
1445 } while (remainder);
1446 return num_insns;
1449 /* Emit an instruction with the indicated PATTERN. If COND is
1450 non-NULL, conditionalize the execution of the instruction on COND
1451 being true. */
1453 static void
1454 emit_constant_insn (rtx cond, rtx pattern)
1456 if (cond)
1457 pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1458 emit_insn (pattern);
1461 /* As above, but extra parameter GENERATE which, if clear, suppresses
1462 RTL generation. */
1464 static int
1465 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1466 HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1467 int generate)
1469 int can_invert = 0;
1470 int can_negate = 0;
1471 int can_negate_initial = 0;
1472 int can_shift = 0;
1473 int i;
1474 int num_bits_set = 0;
1475 int set_sign_bit_copies = 0;
1476 int clear_sign_bit_copies = 0;
1477 int clear_zero_bit_copies = 0;
1478 int set_zero_bit_copies = 0;
1479 int insns = 0;
1480 unsigned HOST_WIDE_INT temp1, temp2;
1481 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1483 /* Find out which operations are safe for a given CODE. Also do a quick
1484 check for degenerate cases; these can occur when DImode operations
1485 are split. */
1486 switch (code)
1488 case SET:
1489 can_invert = 1;
1490 can_shift = 1;
1491 can_negate = 1;
1492 break;
1494 case PLUS:
1495 can_negate = 1;
1496 can_negate_initial = 1;
1497 break;
1499 case IOR:
1500 if (remainder == 0xffffffff)
1502 if (generate)
1503 emit_constant_insn (cond,
1504 gen_rtx_SET (VOIDmode, target,
1505 GEN_INT (ARM_SIGN_EXTEND (val))));
1506 return 1;
1508 if (remainder == 0)
1510 if (reload_completed && rtx_equal_p (target, source))
1511 return 0;
1512 if (generate)
1513 emit_constant_insn (cond,
1514 gen_rtx_SET (VOIDmode, target, source));
1515 return 1;
1517 break;
1519 case AND:
1520 if (remainder == 0)
1522 if (generate)
1523 emit_constant_insn (cond,
1524 gen_rtx_SET (VOIDmode, target, const0_rtx));
1525 return 1;
1527 if (remainder == 0xffffffff)
1529 if (reload_completed && rtx_equal_p (target, source))
1530 return 0;
1531 if (generate)
1532 emit_constant_insn (cond,
1533 gen_rtx_SET (VOIDmode, target, source));
1534 return 1;
1536 can_invert = 1;
1537 break;
1539 case XOR:
1540 if (remainder == 0)
1542 if (reload_completed && rtx_equal_p (target, source))
1543 return 0;
1544 if (generate)
1545 emit_constant_insn (cond,
1546 gen_rtx_SET (VOIDmode, target, source));
1547 return 1;
1549 if (remainder == 0xffffffff)
1551 if (generate)
1552 emit_constant_insn (cond,
1553 gen_rtx_SET (VOIDmode, target,
1554 gen_rtx_NOT (mode, source)));
1555 return 1;
1558 /* We don't know how to handle this yet below. */
1559 abort ();
1561 case MINUS:
1562 /* We treat MINUS as (val - source), since (source - val) is always
1563 passed as (source + (-val)). */
1564 if (remainder == 0)
1566 if (generate)
1567 emit_constant_insn (cond,
1568 gen_rtx_SET (VOIDmode, target,
1569 gen_rtx_NEG (mode, source)));
1570 return 1;
1572 if (const_ok_for_arm (val))
1574 if (generate)
1575 emit_constant_insn (cond,
1576 gen_rtx_SET (VOIDmode, target,
1577 gen_rtx_MINUS (mode, GEN_INT (val),
1578 source)));
1579 return 1;
1581 can_negate = 1;
1583 break;
1585 default:
1586 abort ();
1589 /* If we can do it in one insn get out quickly. */
1590 if (const_ok_for_arm (val)
1591 || (can_negate_initial && const_ok_for_arm (-val))
1592 || (can_invert && const_ok_for_arm (~val)))
1594 if (generate)
1595 emit_constant_insn (cond,
1596 gen_rtx_SET (VOIDmode, target,
1597 (source
1598 ? gen_rtx_fmt_ee (code, mode, source,
1599 GEN_INT (val))
1600 : GEN_INT (val))));
1601 return 1;
1604 /* Calculate a few attributes that may be useful for specific
1605 optimizations. */
1606 for (i = 31; i >= 0; i--)
1608 if ((remainder & (1 << i)) == 0)
1609 clear_sign_bit_copies++;
1610 else
1611 break;
1614 for (i = 31; i >= 0; i--)
1616 if ((remainder & (1 << i)) != 0)
1617 set_sign_bit_copies++;
1618 else
1619 break;
1622 for (i = 0; i <= 31; i++)
1624 if ((remainder & (1 << i)) == 0)
1625 clear_zero_bit_copies++;
1626 else
1627 break;
1630 for (i = 0; i <= 31; i++)
1632 if ((remainder & (1 << i)) != 0)
1633 set_zero_bit_copies++;
1634 else
1635 break;
1638 switch (code)
1640 case SET:
1641 /* See if we can do this by sign_extending a constant that is known
1642 to be negative. This is a good, way of doing it, since the shift
1643 may well merge into a subsequent insn. */
1644 if (set_sign_bit_copies > 1)
1646 if (const_ok_for_arm
1647 (temp1 = ARM_SIGN_EXTEND (remainder
1648 << (set_sign_bit_copies - 1))))
1650 if (generate)
1652 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1653 emit_constant_insn (cond,
1654 gen_rtx_SET (VOIDmode, new_src,
1655 GEN_INT (temp1)));
1656 emit_constant_insn (cond,
1657 gen_ashrsi3 (target, new_src,
1658 GEN_INT (set_sign_bit_copies - 1)));
1660 return 2;
1662 /* For an inverted constant, we will need to set the low bits,
1663 these will be shifted out of harm's way. */
1664 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1665 if (const_ok_for_arm (~temp1))
1667 if (generate)
1669 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1670 emit_constant_insn (cond,
1671 gen_rtx_SET (VOIDmode, new_src,
1672 GEN_INT (temp1)));
1673 emit_constant_insn (cond,
1674 gen_ashrsi3 (target, new_src,
1675 GEN_INT (set_sign_bit_copies - 1)));
1677 return 2;
1681 /* See if we can generate this by setting the bottom (or the top)
1682 16 bits, and then shifting these into the other half of the
1683 word. We only look for the simplest cases, to do more would cost
1684 too much. Be careful, however, not to generate this when the
1685 alternative would take fewer insns. */
1686 if (val & 0xffff0000)
1688 temp1 = remainder & 0xffff0000;
1689 temp2 = remainder & 0x0000ffff;
1691 /* Overlaps outside this range are best done using other methods. */
1692 for (i = 9; i < 24; i++)
1694 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1695 && !const_ok_for_arm (temp2))
1697 rtx new_src = (subtargets
1698 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1699 : target);
1700 insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1701 source, subtargets, generate);
1702 source = new_src;
1703 if (generate)
1704 emit_constant_insn
1705 (cond,
1706 gen_rtx_SET
1707 (VOIDmode, target,
1708 gen_rtx_IOR (mode,
1709 gen_rtx_ASHIFT (mode, source,
1710 GEN_INT (i)),
1711 source)));
1712 return insns + 1;
1716 /* Don't duplicate cases already considered. */
1717 for (i = 17; i < 24; i++)
1719 if (((temp1 | (temp1 >> i)) == remainder)
1720 && !const_ok_for_arm (temp1))
1722 rtx new_src = (subtargets
1723 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1724 : target);
1725 insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1726 source, subtargets, generate);
1727 source = new_src;
1728 if (generate)
1729 emit_constant_insn
1730 (cond,
1731 gen_rtx_SET (VOIDmode, target,
1732 gen_rtx_IOR
1733 (mode,
1734 gen_rtx_LSHIFTRT (mode, source,
1735 GEN_INT (i)),
1736 source)));
1737 return insns + 1;
1741 break;
1743 case IOR:
1744 case XOR:
1745 /* If we have IOR or XOR, and the constant can be loaded in a
1746 single instruction, and we can find a temporary to put it in,
1747 then this can be done in two instructions instead of 3-4. */
1748 if (subtargets
1749 /* TARGET can't be NULL if SUBTARGETS is 0 */
1750 || (reload_completed && !reg_mentioned_p (target, source)))
1752 if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1754 if (generate)
1756 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1758 emit_constant_insn (cond,
1759 gen_rtx_SET (VOIDmode, sub,
1760 GEN_INT (val)));
1761 emit_constant_insn (cond,
1762 gen_rtx_SET (VOIDmode, target,
1763 gen_rtx_fmt_ee (code, mode,
1764 source, sub)));
1766 return 2;
1770 if (code == XOR)
1771 break;
1773 if (set_sign_bit_copies > 8
1774 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1776 if (generate)
1778 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1779 rtx shift = GEN_INT (set_sign_bit_copies);
1781 emit_constant_insn
1782 (cond,
1783 gen_rtx_SET (VOIDmode, sub,
1784 gen_rtx_NOT (mode,
1785 gen_rtx_ASHIFT (mode,
1786 source,
1787 shift))));
1788 emit_constant_insn
1789 (cond,
1790 gen_rtx_SET (VOIDmode, target,
1791 gen_rtx_NOT (mode,
1792 gen_rtx_LSHIFTRT (mode, sub,
1793 shift))));
1795 return 2;
1798 if (set_zero_bit_copies > 8
1799 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1801 if (generate)
1803 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1804 rtx shift = GEN_INT (set_zero_bit_copies);
1806 emit_constant_insn
1807 (cond,
1808 gen_rtx_SET (VOIDmode, sub,
1809 gen_rtx_NOT (mode,
1810 gen_rtx_LSHIFTRT (mode,
1811 source,
1812 shift))));
1813 emit_constant_insn
1814 (cond,
1815 gen_rtx_SET (VOIDmode, target,
1816 gen_rtx_NOT (mode,
1817 gen_rtx_ASHIFT (mode, sub,
1818 shift))));
1820 return 2;
1823 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1825 if (generate)
1827 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1828 emit_constant_insn (cond,
1829 gen_rtx_SET (VOIDmode, sub,
1830 gen_rtx_NOT (mode, source)));
1831 source = sub;
1832 if (subtargets)
1833 sub = gen_reg_rtx (mode);
1834 emit_constant_insn (cond,
1835 gen_rtx_SET (VOIDmode, sub,
1836 gen_rtx_AND (mode, source,
1837 GEN_INT (temp1))));
1838 emit_constant_insn (cond,
1839 gen_rtx_SET (VOIDmode, target,
1840 gen_rtx_NOT (mode, sub)));
1842 return 3;
1844 break;
1846 case AND:
1847 /* See if two shifts will do 2 or more insn's worth of work. */
1848 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1850 HOST_WIDE_INT shift_mask = ((0xffffffff
1851 << (32 - clear_sign_bit_copies))
1852 & 0xffffffff);
1854 if ((remainder | shift_mask) != 0xffffffff)
1856 if (generate)
1858 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1859 insns = arm_gen_constant (AND, mode, cond,
1860 remainder | shift_mask,
1861 new_src, source, subtargets, 1);
1862 source = new_src;
1864 else
1866 rtx targ = subtargets ? NULL_RTX : target;
1867 insns = arm_gen_constant (AND, mode, cond,
1868 remainder | shift_mask,
1869 targ, source, subtargets, 0);
1873 if (generate)
1875 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1876 rtx shift = GEN_INT (clear_sign_bit_copies);
1878 emit_insn (gen_ashlsi3 (new_src, source, shift));
1879 emit_insn (gen_lshrsi3 (target, new_src, shift));
1882 return insns + 2;
1885 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
1887 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
1889 if ((remainder | shift_mask) != 0xffffffff)
1891 if (generate)
1893 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1895 insns = arm_gen_constant (AND, mode, cond,
1896 remainder | shift_mask,
1897 new_src, source, subtargets, 1);
1898 source = new_src;
1900 else
1902 rtx targ = subtargets ? NULL_RTX : target;
1904 insns = arm_gen_constant (AND, mode, cond,
1905 remainder | shift_mask,
1906 targ, source, subtargets, 0);
1910 if (generate)
1912 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1913 rtx shift = GEN_INT (clear_zero_bit_copies);
1915 emit_insn (gen_lshrsi3 (new_src, source, shift));
1916 emit_insn (gen_ashlsi3 (target, new_src, shift));
1919 return insns + 2;
1922 break;
1924 default:
1925 break;
1928 for (i = 0; i < 32; i++)
1929 if (remainder & (1 << i))
1930 num_bits_set++;
1932 if (code == AND || (can_invert && num_bits_set > 16))
1933 remainder = (~remainder) & 0xffffffff;
1934 else if (code == PLUS && num_bits_set > 16)
1935 remainder = (-remainder) & 0xffffffff;
1936 else
1938 can_invert = 0;
1939 can_negate = 0;
1942 /* Now try and find a way of doing the job in either two or three
1943 instructions.
1944 We start by looking for the largest block of zeros that are aligned on
1945 a 2-bit boundary, we then fill up the temps, wrapping around to the
1946 top of the word when we drop off the bottom.
1947 In the worst case this code should produce no more than four insns. */
1949 int best_start = 0;
1950 int best_consecutive_zeros = 0;
1952 for (i = 0; i < 32; i += 2)
1954 int consecutive_zeros = 0;
1956 if (!(remainder & (3 << i)))
1958 while ((i < 32) && !(remainder & (3 << i)))
1960 consecutive_zeros += 2;
1961 i += 2;
1963 if (consecutive_zeros > best_consecutive_zeros)
1965 best_consecutive_zeros = consecutive_zeros;
1966 best_start = i - consecutive_zeros;
1968 i -= 2;
1972 /* So long as it won't require any more insns to do so, it's
1973 desirable to emit a small constant (in bits 0...9) in the last
1974 insn. This way there is more chance that it can be combined with
1975 a later addressing insn to form a pre-indexed load or store
1976 operation. Consider:
1978 *((volatile int *)0xe0000100) = 1;
1979 *((volatile int *)0xe0000110) = 2;
1981 We want this to wind up as:
1983 mov rA, #0xe0000000
1984 mov rB, #1
1985 str rB, [rA, #0x100]
1986 mov rB, #2
1987 str rB, [rA, #0x110]
1989 rather than having to synthesize both large constants from scratch.
1991 Therefore, we calculate how many insns would be required to emit
1992 the constant starting from `best_start', and also starting from
1993 zero (ie with bit 31 first to be output). If `best_start' doesn't
1994 yield a shorter sequence, we may as well use zero. */
1995 if (best_start != 0
1996 && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
1997 && (count_insns_for_constant (remainder, 0) <=
1998 count_insns_for_constant (remainder, best_start)))
1999 best_start = 0;
2001 /* Now start emitting the insns. */
2002 i = best_start;
2005 int end;
2007 if (i <= 0)
2008 i += 32;
2009 if (remainder & (3 << (i - 2)))
2011 end = i - 8;
2012 if (end < 0)
2013 end += 32;
2014 temp1 = remainder & ((0x0ff << end)
2015 | ((i < end) ? (0xff >> (32 - end)) : 0));
2016 remainder &= ~temp1;
2018 if (generate)
2020 rtx new_src, temp1_rtx;
2022 if (code == SET || code == MINUS)
2024 new_src = (subtargets ? gen_reg_rtx (mode) : target);
2025 if (can_invert && code != MINUS)
2026 temp1 = ~temp1;
2028 else
2030 if (remainder && subtargets)
2031 new_src = gen_reg_rtx (mode);
2032 else
2033 new_src = target;
2034 if (can_invert)
2035 temp1 = ~temp1;
2036 else if (can_negate)
2037 temp1 = -temp1;
2040 temp1 = trunc_int_for_mode (temp1, mode);
2041 temp1_rtx = GEN_INT (temp1);
2043 if (code == SET)
2045 else if (code == MINUS)
2046 temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2047 else
2048 temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2050 emit_constant_insn (cond,
2051 gen_rtx_SET (VOIDmode, new_src,
2052 temp1_rtx));
2053 source = new_src;
2056 if (code == SET)
2058 can_invert = 0;
2059 code = PLUS;
2061 else if (code == MINUS)
2062 code = PLUS;
2064 insns++;
2065 i -= 6;
2067 i -= 2;
2069 while (remainder);
2072 return insns;
2075 /* Canonicalize a comparison so that we are more likely to recognize it.
2076 This can be done for a few constant compares, where we can make the
2077 immediate value easier to load. */
2079 enum rtx_code
2080 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2082 unsigned HOST_WIDE_INT i = INTVAL (*op1);
2084 switch (code)
2086 case EQ:
2087 case NE:
2088 return code;
2090 case GT:
2091 case LE:
2092 if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2093 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2095 *op1 = GEN_INT (i + 1);
2096 return code == GT ? GE : LT;
2098 break;
2100 case GE:
2101 case LT:
2102 if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2103 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2105 *op1 = GEN_INT (i - 1);
2106 return code == GE ? GT : LE;
2108 break;
2110 case GTU:
2111 case LEU:
2112 if (i != ~((unsigned HOST_WIDE_INT) 0)
2113 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2115 *op1 = GEN_INT (i + 1);
2116 return code == GTU ? GEU : LTU;
2118 break;
2120 case GEU:
2121 case LTU:
2122 if (i != 0
2123 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2125 *op1 = GEN_INT (i - 1);
2126 return code == GEU ? GTU : LEU;
2128 break;
2130 default:
2131 abort ();
2134 return code;
2138 /* Define how to find the value returned by a function. */
2140 rtx arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2142 enum machine_mode mode;
2143 int unsignedp ATTRIBUTE_UNUSED;
2144 rtx r ATTRIBUTE_UNUSED;
2147 mode = TYPE_MODE (type);
2148 /* Promote integer types. */
2149 if (INTEGRAL_TYPE_P (type))
2150 PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2151 return LIBCALL_VALUE(mode);
2155 /* Decide whether a type should be returned in memory (true)
2156 or in a register (false). This is called by the macro
2157 RETURN_IN_MEMORY. */
2159 arm_return_in_memory (tree type)
2161 HOST_WIDE_INT size;
2163 if (!AGGREGATE_TYPE_P (type))
2164 /* All simple types are returned in registers. */
2165 return 0;
2167 size = int_size_in_bytes (type);
2169 if (arm_abi != ARM_ABI_APCS)
2171 /* ATPCS and later return aggregate types in memory only if they are
2172 larger than a word (or are variable size). */
2173 return (size < 0 || size > UNITS_PER_WORD);
2176 /* For the arm-wince targets we choose to be compatible with Microsoft's
2177 ARM and Thumb compilers, which always return aggregates in memory. */
2178 #ifndef ARM_WINCE
2179 /* All structures/unions bigger than one word are returned in memory.
2180 Also catch the case where int_size_in_bytes returns -1. In this case
2181 the aggregate is either huge or of variable size, and in either case
2182 we will want to return it via memory and not in a register. */
2183 if (size < 0 || size > UNITS_PER_WORD)
2184 return 1;
2186 if (TREE_CODE (type) == RECORD_TYPE)
2188 tree field;
2190 /* For a struct the APCS says that we only return in a register
2191 if the type is 'integer like' and every addressable element
2192 has an offset of zero. For practical purposes this means
2193 that the structure can have at most one non bit-field element
2194 and that this element must be the first one in the structure. */
2196 /* Find the first field, ignoring non FIELD_DECL things which will
2197 have been created by C++. */
2198 for (field = TYPE_FIELDS (type);
2199 field && TREE_CODE (field) != FIELD_DECL;
2200 field = TREE_CHAIN (field))
2201 continue;
2203 if (field == NULL)
2204 return 0; /* An empty structure. Allowed by an extension to ANSI C. */
2206 /* Check that the first field is valid for returning in a register. */
2208 /* ... Floats are not allowed */
2209 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2210 return 1;
2212 /* ... Aggregates that are not themselves valid for returning in
2213 a register are not allowed. */
2214 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2215 return 1;
2217 /* Now check the remaining fields, if any. Only bitfields are allowed,
2218 since they are not addressable. */
2219 for (field = TREE_CHAIN (field);
2220 field;
2221 field = TREE_CHAIN (field))
2223 if (TREE_CODE (field) != FIELD_DECL)
2224 continue;
2226 if (!DECL_BIT_FIELD_TYPE (field))
2227 return 1;
2230 return 0;
2233 if (TREE_CODE (type) == UNION_TYPE)
2235 tree field;
2237 /* Unions can be returned in registers if every element is
2238 integral, or can be returned in an integer register. */
2239 for (field = TYPE_FIELDS (type);
2240 field;
2241 field = TREE_CHAIN (field))
2243 if (TREE_CODE (field) != FIELD_DECL)
2244 continue;
2246 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2247 return 1;
2249 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2250 return 1;
2253 return 0;
2255 #endif /* not ARM_WINCE */
2257 /* Return all other types in memory. */
2258 return 1;
2261 /* Indicate whether or not words of a double are in big-endian order. */
2264 arm_float_words_big_endian (void)
2266 if (TARGET_MAVERICK)
2267 return 0;
2269 /* For FPA, float words are always big-endian. For VFP, floats words
2270 follow the memory system mode. */
2272 if (TARGET_FPA)
2274 return 1;
2277 if (TARGET_VFP)
2278 return (TARGET_BIG_END ? 1 : 0);
2280 return 1;
2283 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2284 for a call to a function whose data type is FNTYPE.
2285 For a library call, FNTYPE is NULL. */
2286 void
2287 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2288 rtx libname ATTRIBUTE_UNUSED,
2289 tree fndecl ATTRIBUTE_UNUSED)
2291 /* On the ARM, the offset starts at 0. */
2292 pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2293 pcum->iwmmxt_nregs = 0;
2294 pcum->can_split = true;
2296 pcum->call_cookie = CALL_NORMAL;
2298 if (TARGET_LONG_CALLS)
2299 pcum->call_cookie = CALL_LONG;
2301 /* Check for long call/short call attributes. The attributes
2302 override any command line option. */
2303 if (fntype)
2305 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2306 pcum->call_cookie = CALL_SHORT;
2307 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2308 pcum->call_cookie = CALL_LONG;
2311 /* Varargs vectors are treated the same as long long.
2312 named_count avoids having to change the way arm handles 'named' */
2313 pcum->named_count = 0;
2314 pcum->nargs = 0;
2316 if (TARGET_REALLY_IWMMXT && fntype)
2318 tree fn_arg;
2320 for (fn_arg = TYPE_ARG_TYPES (fntype);
2321 fn_arg;
2322 fn_arg = TREE_CHAIN (fn_arg))
2323 pcum->named_count += 1;
2325 if (! pcum->named_count)
2326 pcum->named_count = INT_MAX;
2331 /* Return true if mode/type need doubleword alignment. */
2332 bool
2333 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2335 return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2336 || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2340 /* Determine where to put an argument to a function.
2341 Value is zero to push the argument on the stack,
2342 or a hard register in which to store the argument.
2344 MODE is the argument's machine mode.
2345 TYPE is the data type of the argument (as a tree).
2346 This is null for libcalls where that information may
2347 not be available.
2348 CUM is a variable of type CUMULATIVE_ARGS which gives info about
2349 the preceding args and about the function being called.
2350 NAMED is nonzero if this argument is a named parameter
2351 (otherwise it is an extra parameter matching an ellipsis). */
2354 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2355 tree type, int named)
2357 int nregs;
2359 /* Varargs vectors are treated the same as long long.
2360 named_count avoids having to change the way arm handles 'named' */
2361 if (TARGET_IWMMXT_ABI
2362 && VECTOR_MODE_SUPPORTED_P (mode)
2363 && pcum->named_count > pcum->nargs + 1)
2365 if (pcum->iwmmxt_nregs <= 9)
2366 return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2367 else
2369 pcum->can_split = false;
2370 return NULL_RTX;
2374 /* Put doubleword aligned quantities in even register pairs. */
2375 if (pcum->nregs & 1
2376 && ARM_DOUBLEWORD_ALIGN
2377 && arm_needs_doubleword_align (mode, type))
2378 pcum->nregs++;
2380 if (mode == VOIDmode)
2381 /* Compute operand 2 of the call insn. */
2382 return GEN_INT (pcum->call_cookie);
2384 /* Only allow splitting an arg between regs and memory if all preceding
2385 args were allocated to regs. For args passed by reference we only count
2386 the reference pointer. */
2387 if (pcum->can_split)
2388 nregs = 1;
2389 else
2390 nregs = ARM_NUM_REGS2 (mode, type);
2392 if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2393 return NULL_RTX;
2395 return gen_rtx_REG (mode, pcum->nregs);
2398 /* Variable sized types are passed by reference. This is a GCC
2399 extension to the ARM ABI. */
2401 static bool
2402 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2403 enum machine_mode mode ATTRIBUTE_UNUSED,
2404 tree type, bool named ATTRIBUTE_UNUSED)
2406 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2409 /* Encode the current state of the #pragma [no_]long_calls. */
2410 typedef enum
2412 OFF, /* No #pramgma [no_]long_calls is in effect. */
2413 LONG, /* #pragma long_calls is in effect. */
2414 SHORT /* #pragma no_long_calls is in effect. */
2415 } arm_pragma_enum;
2417 static arm_pragma_enum arm_pragma_long_calls = OFF;
2419 void
2420 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2422 arm_pragma_long_calls = LONG;
2425 void
2426 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2428 arm_pragma_long_calls = SHORT;
2431 void
2432 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2434 arm_pragma_long_calls = OFF;
2437 /* Table of machine attributes. */
2438 const struct attribute_spec arm_attribute_table[] =
2440 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2441 /* Function calls made to this symbol must be done indirectly, because
2442 it may lie outside of the 26 bit addressing range of a normal function
2443 call. */
2444 { "long_call", 0, 0, false, true, true, NULL },
2445 /* Whereas these functions are always known to reside within the 26 bit
2446 addressing range. */
2447 { "short_call", 0, 0, false, true, true, NULL },
2448 /* Interrupt Service Routines have special prologue and epilogue requirements. */
2449 { "isr", 0, 1, false, false, false, arm_handle_isr_attribute },
2450 { "interrupt", 0, 1, false, false, false, arm_handle_isr_attribute },
2451 { "naked", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2452 #ifdef ARM_PE
2453 /* ARM/PE has three new attributes:
2454 interfacearm - ?
2455 dllexport - for exporting a function/variable that will live in a dll
2456 dllimport - for importing a function/variable from a dll
2458 Microsoft allows multiple declspecs in one __declspec, separating
2459 them with spaces. We do NOT support this. Instead, use __declspec
2460 multiple times.
2462 { "dllimport", 0, 0, true, false, false, NULL },
2463 { "dllexport", 0, 0, true, false, false, NULL },
2464 { "interfacearm", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2465 #endif
2466 { NULL, 0, 0, false, false, false, NULL }
2469 /* Handle an attribute requiring a FUNCTION_DECL;
2470 arguments as in struct attribute_spec.handler. */
2471 static tree
2472 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2473 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2475 if (TREE_CODE (*node) != FUNCTION_DECL)
2477 warning ("`%s' attribute only applies to functions",
2478 IDENTIFIER_POINTER (name));
2479 *no_add_attrs = true;
2482 return NULL_TREE;
2485 /* Handle an "interrupt" or "isr" attribute;
2486 arguments as in struct attribute_spec.handler. */
2487 static tree
2488 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2489 bool *no_add_attrs)
2491 if (DECL_P (*node))
2493 if (TREE_CODE (*node) != FUNCTION_DECL)
2495 warning ("`%s' attribute only applies to functions",
2496 IDENTIFIER_POINTER (name));
2497 *no_add_attrs = true;
2499 /* FIXME: the argument if any is checked for type attributes;
2500 should it be checked for decl ones? */
2502 else
2504 if (TREE_CODE (*node) == FUNCTION_TYPE
2505 || TREE_CODE (*node) == METHOD_TYPE)
2507 if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2509 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2510 *no_add_attrs = true;
2513 else if (TREE_CODE (*node) == POINTER_TYPE
2514 && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2515 || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2516 && arm_isr_value (args) != ARM_FT_UNKNOWN)
2518 *node = build_type_copy (*node);
2519 TREE_TYPE (*node) = build_type_attribute_variant
2520 (TREE_TYPE (*node),
2521 tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2522 *no_add_attrs = true;
2524 else
2526 /* Possibly pass this attribute on from the type to a decl. */
2527 if (flags & ((int) ATTR_FLAG_DECL_NEXT
2528 | (int) ATTR_FLAG_FUNCTION_NEXT
2529 | (int) ATTR_FLAG_ARRAY_NEXT))
2531 *no_add_attrs = true;
2532 return tree_cons (name, args, NULL_TREE);
2534 else
2536 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2541 return NULL_TREE;
2544 /* Return 0 if the attributes for two types are incompatible, 1 if they
2545 are compatible, and 2 if they are nearly compatible (which causes a
2546 warning to be generated). */
2547 static int
2548 arm_comp_type_attributes (tree type1, tree type2)
2550 int l1, l2, s1, s2;
2552 /* Check for mismatch of non-default calling convention. */
2553 if (TREE_CODE (type1) != FUNCTION_TYPE)
2554 return 1;
2556 /* Check for mismatched call attributes. */
2557 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2558 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2559 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2560 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2562 /* Only bother to check if an attribute is defined. */
2563 if (l1 | l2 | s1 | s2)
2565 /* If one type has an attribute, the other must have the same attribute. */
2566 if ((l1 != l2) || (s1 != s2))
2567 return 0;
2569 /* Disallow mixed attributes. */
2570 if ((l1 & s2) || (l2 & s1))
2571 return 0;
2574 /* Check for mismatched ISR attribute. */
2575 l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2576 if (! l1)
2577 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2578 l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2579 if (! l2)
2580 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2581 if (l1 != l2)
2582 return 0;
2584 return 1;
2587 /* Encode long_call or short_call attribute by prefixing
2588 symbol name in DECL with a special character FLAG. */
2589 void
2590 arm_encode_call_attribute (tree decl, int flag)
2592 const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2593 int len = strlen (str);
2594 char * newstr;
2596 /* Do not allow weak functions to be treated as short call. */
2597 if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2598 return;
2600 newstr = alloca (len + 2);
2601 newstr[0] = flag;
2602 strcpy (newstr + 1, str);
2604 newstr = (char *) ggc_alloc_string (newstr, len + 1);
2605 XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2608 /* Assigns default attributes to newly defined type. This is used to
2609 set short_call/long_call attributes for function types of
2610 functions defined inside corresponding #pragma scopes. */
2611 static void
2612 arm_set_default_type_attributes (tree type)
2614 /* Add __attribute__ ((long_call)) to all functions, when
2615 inside #pragma long_calls or __attribute__ ((short_call)),
2616 when inside #pragma no_long_calls. */
2617 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2619 tree type_attr_list, attr_name;
2620 type_attr_list = TYPE_ATTRIBUTES (type);
2622 if (arm_pragma_long_calls == LONG)
2623 attr_name = get_identifier ("long_call");
2624 else if (arm_pragma_long_calls == SHORT)
2625 attr_name = get_identifier ("short_call");
2626 else
2627 return;
2629 type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2630 TYPE_ATTRIBUTES (type) = type_attr_list;
2634 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2635 defined within the current compilation unit. If this cannot be
2636 determined, then 0 is returned. */
2637 static int
2638 current_file_function_operand (rtx sym_ref)
2640 /* This is a bit of a fib. A function will have a short call flag
2641 applied to its name if it has the short call attribute, or it has
2642 already been defined within the current compilation unit. */
2643 if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2644 return 1;
2646 /* The current function is always defined within the current compilation
2647 unit. if it s a weak definition however, then this may not be the real
2648 definition of the function, and so we have to say no. */
2649 if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2650 && !DECL_WEAK (current_function_decl))
2651 return 1;
2653 /* We cannot make the determination - default to returning 0. */
2654 return 0;
2657 /* Return nonzero if a 32 bit "long_call" should be generated for
2658 this call. We generate a long_call if the function:
2660 a. has an __attribute__((long call))
2661 or b. is within the scope of a #pragma long_calls
2662 or c. the -mlong-calls command line switch has been specified
2664 However we do not generate a long call if the function:
2666 d. has an __attribute__ ((short_call))
2667 or e. is inside the scope of a #pragma no_long_calls
2668 or f. has an __attribute__ ((section))
2669 or g. is defined within the current compilation unit.
2671 This function will be called by C fragments contained in the machine
2672 description file. CALL_REF and CALL_COOKIE correspond to the matched
2673 rtl operands. CALL_SYMBOL is used to distinguish between
2674 two different callers of the function. It is set to 1 in the
2675 "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2676 and "call_value" patterns. This is because of the difference in the
2677 SYM_REFs passed by these patterns. */
2679 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
2681 if (!call_symbol)
2683 if (GET_CODE (sym_ref) != MEM)
2684 return 0;
2686 sym_ref = XEXP (sym_ref, 0);
2689 if (GET_CODE (sym_ref) != SYMBOL_REF)
2690 return 0;
2692 if (call_cookie & CALL_SHORT)
2693 return 0;
2695 if (TARGET_LONG_CALLS && flag_function_sections)
2696 return 1;
2698 if (current_file_function_operand (sym_ref))
2699 return 0;
2701 return (call_cookie & CALL_LONG)
2702 || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2703 || TARGET_LONG_CALLS;
2706 /* Return nonzero if it is ok to make a tail-call to DECL. */
2707 static bool
2708 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
2710 int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2712 if (cfun->machine->sibcall_blocked)
2713 return false;
2715 /* Never tailcall something for which we have no decl, or if we
2716 are in Thumb mode. */
2717 if (decl == NULL || TARGET_THUMB)
2718 return false;
2720 /* Get the calling method. */
2721 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2722 call_type = CALL_SHORT;
2723 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2724 call_type = CALL_LONG;
2726 /* Cannot tail-call to long calls, since these are out of range of
2727 a branch instruction. However, if not compiling PIC, we know
2728 we can reach the symbol if it is in this compilation unit. */
2729 if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2730 return false;
2732 /* If we are interworking and the function is not declared static
2733 then we can't tail-call it unless we know that it exists in this
2734 compilation unit (since it might be a Thumb routine). */
2735 if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2736 return false;
2738 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
2739 if (IS_INTERRUPT (arm_current_func_type ()))
2740 return false;
2742 /* Everything else is ok. */
2743 return true;
2747 /* Addressing mode support functions. */
2749 /* Return nonzero if X is a legitimate immediate operand when compiling
2750 for PIC. */
2752 legitimate_pic_operand_p (rtx x)
2754 if (CONSTANT_P (x)
2755 && flag_pic
2756 && (GET_CODE (x) == SYMBOL_REF
2757 || (GET_CODE (x) == CONST
2758 && GET_CODE (XEXP (x, 0)) == PLUS
2759 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2760 return 0;
2762 return 1;
2766 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
2768 if (GET_CODE (orig) == SYMBOL_REF
2769 || GET_CODE (orig) == LABEL_REF)
2771 #ifndef AOF_ASSEMBLER
2772 rtx pic_ref, address;
2773 #endif
2774 rtx insn;
2775 int subregs = 0;
2777 if (reg == 0)
2779 if (no_new_pseudos)
2780 abort ();
2781 else
2782 reg = gen_reg_rtx (Pmode);
2784 subregs = 1;
2787 #ifdef AOF_ASSEMBLER
2788 /* The AOF assembler can generate relocations for these directly, and
2789 understands that the PIC register has to be added into the offset. */
2790 insn = emit_insn (gen_pic_load_addr_based (reg, orig));
2791 #else
2792 if (subregs)
2793 address = gen_reg_rtx (Pmode);
2794 else
2795 address = reg;
2797 if (TARGET_ARM)
2798 emit_insn (gen_pic_load_addr_arm (address, orig));
2799 else
2800 emit_insn (gen_pic_load_addr_thumb (address, orig));
2802 if ((GET_CODE (orig) == LABEL_REF
2803 || (GET_CODE (orig) == SYMBOL_REF &&
2804 SYMBOL_REF_LOCAL_P (orig)))
2805 && NEED_GOT_RELOC)
2806 pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
2807 else
2809 pic_ref = gen_rtx_MEM (Pmode,
2810 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
2811 address));
2812 RTX_UNCHANGING_P (pic_ref) = 1;
2815 insn = emit_move_insn (reg, pic_ref);
2816 #endif
2817 current_function_uses_pic_offset_table = 1;
2818 /* Put a REG_EQUAL note on this insn, so that it can be optimized
2819 by loop. */
2820 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
2821 REG_NOTES (insn));
2822 return reg;
2824 else if (GET_CODE (orig) == CONST)
2826 rtx base, offset;
2828 if (GET_CODE (XEXP (orig, 0)) == PLUS
2829 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
2830 return orig;
2832 if (reg == 0)
2834 if (no_new_pseudos)
2835 abort ();
2836 else
2837 reg = gen_reg_rtx (Pmode);
2840 if (GET_CODE (XEXP (orig, 0)) == PLUS)
2842 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
2843 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
2844 base == reg ? 0 : reg);
2846 else
2847 abort ();
2849 if (GET_CODE (offset) == CONST_INT)
2851 /* The base register doesn't really matter, we only want to
2852 test the index for the appropriate mode. */
2853 if (!arm_legitimate_index_p (mode, offset, SET, 0))
2855 if (!no_new_pseudos)
2856 offset = force_reg (Pmode, offset);
2857 else
2858 abort ();
2861 if (GET_CODE (offset) == CONST_INT)
2862 return plus_constant (base, INTVAL (offset));
2865 if (GET_MODE_SIZE (mode) > 4
2866 && (GET_MODE_CLASS (mode) == MODE_INT
2867 || TARGET_SOFT_FLOAT))
2869 emit_insn (gen_addsi3 (reg, base, offset));
2870 return reg;
2873 return gen_rtx_PLUS (Pmode, base, offset);
2876 return orig;
2879 /* Generate code to load the PIC register. PROLOGUE is true if
2880 called from arm_expand_prologue (in which case we want the
2881 generated insns at the start of the function); false if called
2882 by an exception receiver that needs the PIC register reloaded
2883 (in which case the insns are just dumped at the current location). */
2884 void
2885 arm_finalize_pic (int prologue ATTRIBUTE_UNUSED)
2887 #ifndef AOF_ASSEMBLER
2888 rtx l1, pic_tmp, pic_tmp2, seq, pic_rtx;
2889 rtx global_offset_table;
2891 if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
2892 return;
2894 if (!flag_pic)
2895 abort ();
2897 start_sequence ();
2898 l1 = gen_label_rtx ();
2900 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
2901 /* On the ARM the PC register contains 'dot + 8' at the time of the
2902 addition, on the Thumb it is 'dot + 4'. */
2903 pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
2904 if (GOT_PCREL)
2905 pic_tmp2 = gen_rtx_CONST (VOIDmode,
2906 gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
2907 else
2908 pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
2910 pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
2912 if (TARGET_ARM)
2914 emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
2915 emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
2917 else
2919 emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
2920 emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
2923 seq = get_insns ();
2924 end_sequence ();
2925 if (prologue)
2926 emit_insn_after (seq, get_insns ());
2927 else
2928 emit_insn (seq);
2930 /* Need to emit this whether or not we obey regdecls,
2931 since setjmp/longjmp can cause life info to screw up. */
2932 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
2933 #endif /* AOF_ASSEMBLER */
2936 /* Return nonzero if X is valid as an ARM state addressing register. */
2937 static int
2938 arm_address_register_rtx_p (rtx x, int strict_p)
2940 int regno;
2942 if (GET_CODE (x) != REG)
2943 return 0;
2945 regno = REGNO (x);
2947 if (strict_p)
2948 return ARM_REGNO_OK_FOR_BASE_P (regno);
2950 return (regno <= LAST_ARM_REGNUM
2951 || regno >= FIRST_PSEUDO_REGISTER
2952 || regno == FRAME_POINTER_REGNUM
2953 || regno == ARG_POINTER_REGNUM);
2956 /* Return nonzero if X is a valid ARM state address operand. */
2958 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
2959 int strict_p)
2961 bool use_ldrd;
2962 enum rtx_code code = GET_CODE (x);
2964 if (arm_address_register_rtx_p (x, strict_p))
2965 return 1;
2967 use_ldrd = (TARGET_LDRD
2968 && (mode == DImode
2969 || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
2971 if (code == POST_INC || code == PRE_DEC
2972 || ((code == PRE_INC || code == POST_DEC)
2973 && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
2974 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
2976 else if ((code == POST_MODIFY || code == PRE_MODIFY)
2977 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
2978 && GET_CODE (XEXP (x, 1)) == PLUS
2979 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
2981 rtx addend = XEXP (XEXP (x, 1), 1);
2983 /* Don't allow ldrd post increment by register becuase it's hard
2984 to fixup invalid register choices. */
2985 if (use_ldrd
2986 && GET_CODE (x) == POST_MODIFY
2987 && GET_CODE (addend) == REG)
2988 return 0;
2990 return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
2991 && arm_legitimate_index_p (mode, addend, outer, strict_p));
2994 /* After reload constants split into minipools will have addresses
2995 from a LABEL_REF. */
2996 else if (reload_completed
2997 && (code == LABEL_REF
2998 || (code == CONST
2999 && GET_CODE (XEXP (x, 0)) == PLUS
3000 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3001 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3002 return 1;
3004 else if (mode == TImode)
3005 return 0;
3007 else if (code == PLUS)
3009 rtx xop0 = XEXP (x, 0);
3010 rtx xop1 = XEXP (x, 1);
3012 return ((arm_address_register_rtx_p (xop0, strict_p)
3013 && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3014 || (arm_address_register_rtx_p (xop1, strict_p)
3015 && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3018 #if 0
3019 /* Reload currently can't handle MINUS, so disable this for now */
3020 else if (GET_CODE (x) == MINUS)
3022 rtx xop0 = XEXP (x, 0);
3023 rtx xop1 = XEXP (x, 1);
3025 return (arm_address_register_rtx_p (xop0, strict_p)
3026 && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3028 #endif
3030 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3031 && code == SYMBOL_REF
3032 && CONSTANT_POOL_ADDRESS_P (x)
3033 && ! (flag_pic
3034 && symbol_mentioned_p (get_pool_constant (x))))
3035 return 1;
3037 return 0;
3040 /* Return nonzero if INDEX is valid for an address index operand in
3041 ARM state. */
3042 static int
3043 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3044 int strict_p)
3046 HOST_WIDE_INT range;
3047 enum rtx_code code = GET_CODE (index);
3049 /* Standard coprocessor addressing modes. */
3050 if (TARGET_HARD_FLOAT
3051 && (TARGET_FPA || TARGET_MAVERICK)
3052 && (GET_MODE_CLASS (mode) == MODE_FLOAT
3053 || (TARGET_MAVERICK && mode == DImode)))
3054 return (code == CONST_INT && INTVAL (index) < 1024
3055 && INTVAL (index) > -1024
3056 && (INTVAL (index) & 3) == 0);
3058 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3059 return (code == CONST_INT
3060 && INTVAL (index) < 1024
3061 && INTVAL (index) > -1024
3062 && (INTVAL (index) & 3) == 0);
3064 if (arm_address_register_rtx_p (index, strict_p)
3065 && (GET_MODE_SIZE (mode) <= 4))
3066 return 1;
3068 if (mode == DImode || mode == DFmode)
3070 if (code == CONST_INT)
3072 HOST_WIDE_INT val = INTVAL (index);
3074 if (TARGET_LDRD)
3075 return val > -256 && val < 256;
3076 else
3077 return val > -4096 && val < 4092;
3080 return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3083 if (GET_MODE_SIZE (mode) <= 4
3084 && ! (arm_arch4
3085 && (mode == HImode
3086 || (mode == QImode && outer == SIGN_EXTEND))))
3088 if (code == MULT)
3090 rtx xiop0 = XEXP (index, 0);
3091 rtx xiop1 = XEXP (index, 1);
3093 return ((arm_address_register_rtx_p (xiop0, strict_p)
3094 && power_of_two_operand (xiop1, SImode))
3095 || (arm_address_register_rtx_p (xiop1, strict_p)
3096 && power_of_two_operand (xiop0, SImode)));
3098 else if (code == LSHIFTRT || code == ASHIFTRT
3099 || code == ASHIFT || code == ROTATERT)
3101 rtx op = XEXP (index, 1);
3103 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3104 && GET_CODE (op) == CONST_INT
3105 && INTVAL (op) > 0
3106 && INTVAL (op) <= 31);
3110 /* For ARM v4 we may be doing a sign-extend operation during the
3111 load. */
3112 if (arm_arch4)
3114 if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3115 range = 256;
3116 else
3117 range = 4096;
3119 else
3120 range = (mode == HImode) ? 4095 : 4096;
3122 return (code == CONST_INT
3123 && INTVAL (index) < range
3124 && INTVAL (index) > -range);
3127 /* Return nonzero if X is valid as a Thumb state base register. */
3128 static int
3129 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3131 int regno;
3133 if (GET_CODE (x) != REG)
3134 return 0;
3136 regno = REGNO (x);
3138 if (strict_p)
3139 return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3141 return (regno <= LAST_LO_REGNUM
3142 || regno > LAST_VIRTUAL_REGISTER
3143 || regno == FRAME_POINTER_REGNUM
3144 || (GET_MODE_SIZE (mode) >= 4
3145 && (regno == STACK_POINTER_REGNUM
3146 || regno >= FIRST_PSEUDO_REGISTER
3147 || x == hard_frame_pointer_rtx
3148 || x == arg_pointer_rtx)));
3151 /* Return nonzero if x is a legitimate index register. This is the case
3152 for any base register that can access a QImode object. */
3153 inline static int
3154 thumb_index_register_rtx_p (rtx x, int strict_p)
3156 return thumb_base_register_rtx_p (x, QImode, strict_p);
3159 /* Return nonzero if x is a legitimate Thumb-state address.
3161 The AP may be eliminated to either the SP or the FP, so we use the
3162 least common denominator, e.g. SImode, and offsets from 0 to 64.
3164 ??? Verify whether the above is the right approach.
3166 ??? Also, the FP may be eliminated to the SP, so perhaps that
3167 needs special handling also.
3169 ??? Look at how the mips16 port solves this problem. It probably uses
3170 better ways to solve some of these problems.
3172 Although it is not incorrect, we don't accept QImode and HImode
3173 addresses based on the frame pointer or arg pointer until the
3174 reload pass starts. This is so that eliminating such addresses
3175 into stack based ones won't produce impossible code. */
3177 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3179 /* ??? Not clear if this is right. Experiment. */
3180 if (GET_MODE_SIZE (mode) < 4
3181 && !(reload_in_progress || reload_completed)
3182 && (reg_mentioned_p (frame_pointer_rtx, x)
3183 || reg_mentioned_p (arg_pointer_rtx, x)
3184 || reg_mentioned_p (virtual_incoming_args_rtx, x)
3185 || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3186 || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3187 || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3188 return 0;
3190 /* Accept any base register. SP only in SImode or larger. */
3191 else if (thumb_base_register_rtx_p (x, mode, strict_p))
3192 return 1;
3194 /* This is PC relative data before arm_reorg runs. */
3195 else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3196 && GET_CODE (x) == SYMBOL_REF
3197 && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3198 return 1;
3200 /* This is PC relative data after arm_reorg runs. */
3201 else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3202 && (GET_CODE (x) == LABEL_REF
3203 || (GET_CODE (x) == CONST
3204 && GET_CODE (XEXP (x, 0)) == PLUS
3205 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3206 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3207 return 1;
3209 /* Post-inc indexing only supported for SImode and larger. */
3210 else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3211 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3212 return 1;
3214 else if (GET_CODE (x) == PLUS)
3216 /* REG+REG address can be any two index registers. */
3217 /* We disallow FRAME+REG addressing since we know that FRAME
3218 will be replaced with STACK, and SP relative addressing only
3219 permits SP+OFFSET. */
3220 if (GET_MODE_SIZE (mode) <= 4
3221 && XEXP (x, 0) != frame_pointer_rtx
3222 && XEXP (x, 1) != frame_pointer_rtx
3223 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3224 && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3225 return 1;
3227 /* REG+const has 5-7 bit offset for non-SP registers. */
3228 else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3229 || XEXP (x, 0) == arg_pointer_rtx)
3230 && GET_CODE (XEXP (x, 1)) == CONST_INT
3231 && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3232 return 1;
3234 /* REG+const has 10 bit offset for SP, but only SImode and
3235 larger is supported. */
3236 /* ??? Should probably check for DI/DFmode overflow here
3237 just like GO_IF_LEGITIMATE_OFFSET does. */
3238 else if (GET_CODE (XEXP (x, 0)) == REG
3239 && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3240 && GET_MODE_SIZE (mode) >= 4
3241 && GET_CODE (XEXP (x, 1)) == CONST_INT
3242 && INTVAL (XEXP (x, 1)) >= 0
3243 && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3244 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3245 return 1;
3247 else if (GET_CODE (XEXP (x, 0)) == REG
3248 && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3249 && GET_MODE_SIZE (mode) >= 4
3250 && GET_CODE (XEXP (x, 1)) == CONST_INT
3251 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3252 return 1;
3255 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3256 && GET_MODE_SIZE (mode) == 4
3257 && GET_CODE (x) == SYMBOL_REF
3258 && CONSTANT_POOL_ADDRESS_P (x)
3259 && !(flag_pic
3260 && symbol_mentioned_p (get_pool_constant (x))))
3261 return 1;
3263 return 0;
3266 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3267 instruction of mode MODE. */
3269 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3271 switch (GET_MODE_SIZE (mode))
3273 case 1:
3274 return val >= 0 && val < 32;
3276 case 2:
3277 return val >= 0 && val < 64 && (val & 1) == 0;
3279 default:
3280 return (val >= 0
3281 && (val + GET_MODE_SIZE (mode)) <= 128
3282 && (val & 3) == 0);
3286 /* Try machine-dependent ways of modifying an illegitimate address
3287 to be legitimate. If we find one, return the new, valid address. */
3289 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3291 if (GET_CODE (x) == PLUS)
3293 rtx xop0 = XEXP (x, 0);
3294 rtx xop1 = XEXP (x, 1);
3296 if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3297 xop0 = force_reg (SImode, xop0);
3299 if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3300 xop1 = force_reg (SImode, xop1);
3302 if (ARM_BASE_REGISTER_RTX_P (xop0)
3303 && GET_CODE (xop1) == CONST_INT)
3305 HOST_WIDE_INT n, low_n;
3306 rtx base_reg, val;
3307 n = INTVAL (xop1);
3309 /* VFP addressing modes actually allow greater offsets, but for
3310 now we just stick with the lowest common denominator. */
3311 if (mode == DImode
3312 || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3314 low_n = n & 0x0f;
3315 n &= ~0x0f;
3316 if (low_n > 4)
3318 n += 16;
3319 low_n -= 16;
3322 else
3324 low_n = ((mode) == TImode ? 0
3325 : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3326 n -= low_n;
3329 base_reg = gen_reg_rtx (SImode);
3330 val = force_operand (gen_rtx_PLUS (SImode, xop0,
3331 GEN_INT (n)), NULL_RTX);
3332 emit_move_insn (base_reg, val);
3333 x = (low_n == 0 ? base_reg
3334 : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3336 else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3337 x = gen_rtx_PLUS (SImode, xop0, xop1);
3340 /* XXX We don't allow MINUS any more -- see comment in
3341 arm_legitimate_address_p (). */
3342 else if (GET_CODE (x) == MINUS)
3344 rtx xop0 = XEXP (x, 0);
3345 rtx xop1 = XEXP (x, 1);
3347 if (CONSTANT_P (xop0))
3348 xop0 = force_reg (SImode, xop0);
3350 if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3351 xop1 = force_reg (SImode, xop1);
3353 if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3354 x = gen_rtx_MINUS (SImode, xop0, xop1);
3357 if (flag_pic)
3359 /* We need to find and carefully transform any SYMBOL and LABEL
3360 references; so go back to the original address expression. */
3361 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3363 if (new_x != orig_x)
3364 x = new_x;
3367 return x;
3371 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3372 to be legitimate. If we find one, return the new, valid address. */
3374 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3376 if (GET_CODE (x) == PLUS
3377 && GET_CODE (XEXP (x, 1)) == CONST_INT
3378 && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3379 || INTVAL (XEXP (x, 1)) < 0))
3381 rtx xop0 = XEXP (x, 0);
3382 rtx xop1 = XEXP (x, 1);
3383 HOST_WIDE_INT offset = INTVAL (xop1);
3385 /* Try and fold the offset into a biasing of the base register and
3386 then offsetting that. Don't do this when optimizing for space
3387 since it can cause too many CSEs. */
3388 if (optimize_size && offset >= 0
3389 && offset < 256 + 31 * GET_MODE_SIZE (mode))
3391 HOST_WIDE_INT delta;
3393 if (offset >= 256)
3394 delta = offset - (256 - GET_MODE_SIZE (mode));
3395 else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3396 delta = 31 * GET_MODE_SIZE (mode);
3397 else
3398 delta = offset & (~31 * GET_MODE_SIZE (mode));
3400 xop0 = force_operand (plus_constant (xop0, offset - delta),
3401 NULL_RTX);
3402 x = plus_constant (xop0, delta);
3404 else if (offset < 0 && offset > -256)
3405 /* Small negative offsets are best done with a subtract before the
3406 dereference, forcing these into a register normally takes two
3407 instructions. */
3408 x = force_operand (x, NULL_RTX);
3409 else
3411 /* For the remaining cases, force the constant into a register. */
3412 xop1 = force_reg (SImode, xop1);
3413 x = gen_rtx_PLUS (SImode, xop0, xop1);
3416 else if (GET_CODE (x) == PLUS
3417 && s_register_operand (XEXP (x, 1), SImode)
3418 && !s_register_operand (XEXP (x, 0), SImode))
3420 rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3422 x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3425 if (flag_pic)
3427 /* We need to find and carefully transform any SYMBOL and LABEL
3428 references; so go back to the original address expression. */
3429 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3431 if (new_x != orig_x)
3432 x = new_x;
3435 return x;
3440 #define REG_OR_SUBREG_REG(X) \
3441 (GET_CODE (X) == REG \
3442 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3444 #define REG_OR_SUBREG_RTX(X) \
3445 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3447 #ifndef COSTS_N_INSNS
3448 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3449 #endif
3450 static inline int
3451 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3453 enum machine_mode mode = GET_MODE (x);
3455 switch (code)
3457 case ASHIFT:
3458 case ASHIFTRT:
3459 case LSHIFTRT:
3460 case ROTATERT:
3461 case PLUS:
3462 case MINUS:
3463 case COMPARE:
3464 case NEG:
3465 case NOT:
3466 return COSTS_N_INSNS (1);
3468 case MULT:
3469 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3471 int cycles = 0;
3472 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3474 while (i)
3476 i >>= 2;
3477 cycles++;
3479 return COSTS_N_INSNS (2) + cycles;
3481 return COSTS_N_INSNS (1) + 16;
3483 case SET:
3484 return (COSTS_N_INSNS (1)
3485 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
3486 + GET_CODE (SET_DEST (x)) == MEM));
3488 case CONST_INT:
3489 if (outer == SET)
3491 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
3492 return 0;
3493 if (thumb_shiftable_const (INTVAL (x)))
3494 return COSTS_N_INSNS (2);
3495 return COSTS_N_INSNS (3);
3497 else if ((outer == PLUS || outer == COMPARE)
3498 && INTVAL (x) < 256 && INTVAL (x) > -256)
3499 return 0;
3500 else if (outer == AND
3501 && INTVAL (x) < 256 && INTVAL (x) >= -256)
3502 return COSTS_N_INSNS (1);
3503 else if (outer == ASHIFT || outer == ASHIFTRT
3504 || outer == LSHIFTRT)
3505 return 0;
3506 return COSTS_N_INSNS (2);
3508 case CONST:
3509 case CONST_DOUBLE:
3510 case LABEL_REF:
3511 case SYMBOL_REF:
3512 return COSTS_N_INSNS (3);
3514 case UDIV:
3515 case UMOD:
3516 case DIV:
3517 case MOD:
3518 return 100;
3520 case TRUNCATE:
3521 return 99;
3523 case AND:
3524 case XOR:
3525 case IOR:
3526 /* XXX guess. */
3527 return 8;
3529 case MEM:
3530 /* XXX another guess. */
3531 /* Memory costs quite a lot for the first word, but subsequent words
3532 load at the equivalent of a single insn each. */
3533 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3534 + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3535 ? 4 : 0));
3537 case IF_THEN_ELSE:
3538 /* XXX a guess. */
3539 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3540 return 14;
3541 return 2;
3543 case ZERO_EXTEND:
3544 /* XXX still guessing. */
3545 switch (GET_MODE (XEXP (x, 0)))
3547 case QImode:
3548 return (1 + (mode == DImode ? 4 : 0)
3549 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3551 case HImode:
3552 return (4 + (mode == DImode ? 4 : 0)
3553 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3555 case SImode:
3556 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3558 default:
3559 return 99;
3562 default:
3563 return 99;
3568 /* Worker routine for arm_rtx_costs. */
3569 static inline int
3570 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3572 enum machine_mode mode = GET_MODE (x);
3573 enum rtx_code subcode;
3574 int extra_cost;
3576 switch (code)
3578 case MEM:
3579 /* Memory costs quite a lot for the first word, but subsequent words
3580 load at the equivalent of a single insn each. */
3581 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3582 + (GET_CODE (x) == SYMBOL_REF
3583 && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3585 case DIV:
3586 case MOD:
3587 case UDIV:
3588 case UMOD:
3589 return optimize_size ? COSTS_N_INSNS (2) : 100;
3591 case ROTATE:
3592 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3593 return 4;
3594 /* Fall through */
3595 case ROTATERT:
3596 if (mode != SImode)
3597 return 8;
3598 /* Fall through */
3599 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3600 if (mode == DImode)
3601 return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3602 + ((GET_CODE (XEXP (x, 0)) == REG
3603 || (GET_CODE (XEXP (x, 0)) == SUBREG
3604 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3605 ? 0 : 8));
3606 return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3607 || (GET_CODE (XEXP (x, 0)) == SUBREG
3608 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3609 ? 0 : 4)
3610 + ((GET_CODE (XEXP (x, 1)) == REG
3611 || (GET_CODE (XEXP (x, 1)) == SUBREG
3612 && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3613 || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3614 ? 0 : 4));
3616 case MINUS:
3617 if (mode == DImode)
3618 return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3619 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3620 || (GET_CODE (XEXP (x, 0)) == CONST_INT
3621 && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3622 ? 0 : 8));
3624 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3625 return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3626 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3627 && arm_const_double_rtx (XEXP (x, 1))))
3628 ? 0 : 8)
3629 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3630 || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3631 && arm_const_double_rtx (XEXP (x, 0))))
3632 ? 0 : 8));
3634 if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3635 && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3636 && REG_OR_SUBREG_REG (XEXP (x, 1))))
3637 || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3638 || subcode == ASHIFTRT || subcode == LSHIFTRT
3639 || subcode == ROTATE || subcode == ROTATERT
3640 || (subcode == MULT
3641 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3642 && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3643 (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
3644 && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
3645 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
3646 || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
3647 && REG_OR_SUBREG_REG (XEXP (x, 0))))
3648 return 1;
3649 /* Fall through */
3651 case PLUS:
3652 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3653 return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3654 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3655 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3656 && arm_const_double_rtx (XEXP (x, 1))))
3657 ? 0 : 8));
3659 /* Fall through */
3660 case AND: case XOR: case IOR:
3661 extra_cost = 0;
3663 /* Normally the frame registers will be spilt into reg+const during
3664 reload, so it is a bad idea to combine them with other instructions,
3665 since then they might not be moved outside of loops. As a compromise
3666 we allow integration with ops that have a constant as their second
3667 operand. */
3668 if ((REG_OR_SUBREG_REG (XEXP (x, 0))
3669 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
3670 && GET_CODE (XEXP (x, 1)) != CONST_INT)
3671 || (REG_OR_SUBREG_REG (XEXP (x, 0))
3672 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
3673 extra_cost = 4;
3675 if (mode == DImode)
3676 return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3677 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3678 || (GET_CODE (XEXP (x, 1)) == CONST_INT
3679 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3680 ? 0 : 8));
3682 if (REG_OR_SUBREG_REG (XEXP (x, 0)))
3683 return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
3684 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3685 || (GET_CODE (XEXP (x, 1)) == CONST_INT
3686 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3687 ? 0 : 4));
3689 else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
3690 return (1 + extra_cost
3691 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
3692 || subcode == LSHIFTRT || subcode == ASHIFTRT
3693 || subcode == ROTATE || subcode == ROTATERT
3694 || (subcode == MULT
3695 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3696 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
3697 (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
3698 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
3699 && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
3700 || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
3701 ? 0 : 4));
3703 return 8;
3705 case MULT:
3706 /* This should have been handled by the CPU specific routines. */
3707 abort ();
3709 case TRUNCATE:
3710 if (arm_arch3m && mode == SImode
3711 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
3712 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
3713 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
3714 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
3715 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
3716 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
3717 return 8;
3718 return 99;
3720 case NEG:
3721 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3722 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
3723 /* Fall through */
3724 case NOT:
3725 if (mode == DImode)
3726 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3728 return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3730 case IF_THEN_ELSE:
3731 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3732 return 14;
3733 return 2;
3735 case COMPARE:
3736 return 1;
3738 case ABS:
3739 return 4 + (mode == DImode ? 4 : 0);
3741 case SIGN_EXTEND:
3742 if (GET_MODE (XEXP (x, 0)) == QImode)
3743 return (4 + (mode == DImode ? 4 : 0)
3744 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3745 /* Fall through */
3746 case ZERO_EXTEND:
3747 switch (GET_MODE (XEXP (x, 0)))
3749 case QImode:
3750 return (1 + (mode == DImode ? 4 : 0)
3751 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3753 case HImode:
3754 return (4 + (mode == DImode ? 4 : 0)
3755 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3757 case SImode:
3758 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3760 case V8QImode:
3761 case V4HImode:
3762 case V2SImode:
3763 case V4QImode:
3764 case V2HImode:
3765 return 1;
3767 default:
3768 break;
3770 abort ();
3772 case CONST_INT:
3773 if (const_ok_for_arm (INTVAL (x)))
3774 return outer == SET ? 2 : -1;
3775 else if (outer == AND
3776 && const_ok_for_arm (~INTVAL (x)))
3777 return -1;
3778 else if ((outer == COMPARE
3779 || outer == PLUS || outer == MINUS)
3780 && const_ok_for_arm (-INTVAL (x)))
3781 return -1;
3782 else
3783 return 5;
3785 case CONST:
3786 case LABEL_REF:
3787 case SYMBOL_REF:
3788 return 6;
3790 case CONST_DOUBLE:
3791 if (arm_const_double_rtx (x))
3792 return outer == SET ? 2 : -1;
3793 else if ((outer == COMPARE || outer == PLUS)
3794 && neg_const_double_rtx_ok_for_fpa (x))
3795 return -1;
3796 return 7;
3798 default:
3799 return 99;
3803 /* RTX costs for cores with a slow MUL implementation. */
3805 static bool
3806 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
3808 enum machine_mode mode = GET_MODE (x);
3810 if (TARGET_THUMB)
3812 *total = thumb_rtx_costs (x, code, outer_code);
3813 return true;
3816 switch (code)
3818 case MULT:
3819 if (GET_MODE_CLASS (mode) == MODE_FLOAT
3820 || mode == DImode)
3822 *total = 30;
3823 return true;
3826 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3828 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3829 & (unsigned HOST_WIDE_INT) 0xffffffff);
3830 int cost, const_ok = const_ok_for_arm (i);
3831 int j, booth_unit_size;
3833 /* Tune as appropriate. */
3834 cost = const_ok ? 4 : 8;
3835 booth_unit_size = 2;
3836 for (j = 0; i && j < 32; j += booth_unit_size)
3838 i >>= booth_unit_size;
3839 cost += 2;
3842 *total = cost;
3843 return true;
3846 *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
3847 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
3848 return true;
3850 default:
3851 *total = arm_rtx_costs_1 (x, code, outer_code);
3852 return true;
3857 /* RTX cost for cores with a fast multiply unit (M variants). */
3859 static bool
3860 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
3862 enum machine_mode mode = GET_MODE (x);
3864 if (TARGET_THUMB)
3866 *total = thumb_rtx_costs (x, code, outer_code);
3867 return true;
3870 switch (code)
3872 case MULT:
3873 /* There is no point basing this on the tuning, since it is always the
3874 fast variant if it exists at all. */
3875 if (mode == DImode
3876 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
3877 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
3878 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
3880 *total = 8;
3881 return true;
3885 if (GET_MODE_CLASS (mode) == MODE_FLOAT
3886 || mode == DImode)
3888 *total = 30;
3889 return true;
3892 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3894 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3895 & (unsigned HOST_WIDE_INT) 0xffffffff);
3896 int cost, const_ok = const_ok_for_arm (i);
3897 int j, booth_unit_size;
3899 /* Tune as appropriate. */
3900 cost = const_ok ? 4 : 8;
3901 booth_unit_size = 8;
3902 for (j = 0; i && j < 32; j += booth_unit_size)
3904 i >>= booth_unit_size;
3905 cost += 2;
3908 *total = cost;
3909 return true;
3912 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
3913 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
3914 return true;
3916 default:
3917 *total = arm_rtx_costs_1 (x, code, outer_code);
3918 return true;
3923 /* RTX cost for XScale CPUs. */
3925 static bool
3926 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
3928 enum machine_mode mode = GET_MODE (x);
3930 if (TARGET_THUMB)
3932 *total = thumb_rtx_costs (x, code, outer_code);
3933 return true;
3936 switch (code)
3938 case MULT:
3939 /* There is no point basing this on the tuning, since it is always the
3940 fast variant if it exists at all. */
3941 if (mode == DImode
3942 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
3943 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
3944 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
3946 *total = 8;
3947 return true;
3951 if (GET_MODE_CLASS (mode) == MODE_FLOAT
3952 || mode == DImode)
3954 *total = 30;
3955 return true;
3958 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3960 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3961 & (unsigned HOST_WIDE_INT) 0xffffffff);
3962 int cost, const_ok = const_ok_for_arm (i);
3963 unsigned HOST_WIDE_INT masked_const;
3965 /* The cost will be related to two insns.
3966 First a load of the constant (MOV or LDR), then a multiply. */
3967 cost = 2;
3968 if (! const_ok)
3969 cost += 1; /* LDR is probably more expensive because
3970 of longer result latency. */
3971 masked_const = i & 0xffff8000;
3972 if (masked_const != 0 && masked_const != 0xffff8000)
3974 masked_const = i & 0xf8000000;
3975 if (masked_const == 0 || masked_const == 0xf8000000)
3976 cost += 1;
3977 else
3978 cost += 2;
3980 *total = cost;
3981 return true;
3984 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
3985 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
3986 return true;
3988 default:
3989 *total = arm_rtx_costs_1 (x, code, outer_code);
3990 return true;
3995 /* RTX costs for 9e (and later) cores. */
3997 static bool
3998 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4000 enum machine_mode mode = GET_MODE (x);
4001 int nonreg_cost;
4002 int cost;
4004 if (TARGET_THUMB)
4006 switch (code)
4008 case MULT:
4009 *total = COSTS_N_INSNS (3);
4010 return true;
4012 default:
4013 *total = thumb_rtx_costs (x, code, outer_code);
4014 return true;
4018 switch (code)
4020 case MULT:
4021 /* There is no point basing this on the tuning, since it is always the
4022 fast variant if it exists at all. */
4023 if (mode == DImode
4024 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4025 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4026 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4028 *total = 3;
4029 return true;
4033 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4035 *total = 30;
4036 return true;
4038 if (mode == DImode)
4040 cost = 7;
4041 nonreg_cost = 8;
4043 else
4045 cost = 2;
4046 nonreg_cost = 4;
4050 *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4051 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4052 return true;
4054 default:
4055 *total = arm_rtx_costs_1 (x, code, outer_code);
4056 return true;
4059 /* All address computations that can be done are free, but rtx cost returns
4060 the same for practically all of them. So we weight the different types
4061 of address here in the order (most pref first):
4062 PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL. */
4063 static inline int
4064 arm_arm_address_cost (rtx x)
4066 enum rtx_code c = GET_CODE (x);
4068 if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4069 return 0;
4070 if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4071 return 10;
4073 if (c == PLUS || c == MINUS)
4075 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4076 return 2;
4078 if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4079 return 3;
4081 return 4;
4084 return 6;
4087 static inline int
4088 arm_thumb_address_cost (rtx x)
4090 enum rtx_code c = GET_CODE (x);
4092 if (c == REG)
4093 return 1;
4094 if (c == PLUS
4095 && GET_CODE (XEXP (x, 0)) == REG
4096 && GET_CODE (XEXP (x, 1)) == CONST_INT)
4097 return 1;
4099 return 2;
4102 static int
4103 arm_address_cost (rtx x)
4105 return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4108 static int
4109 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4111 rtx i_pat, d_pat;
4113 /* Some true dependencies can have a higher cost depending
4114 on precisely how certain input operands are used. */
4115 if (arm_tune_xscale
4116 && REG_NOTE_KIND (link) == 0
4117 && recog_memoized (insn) >= 0
4118 && recog_memoized (dep) >= 0)
4120 int shift_opnum = get_attr_shift (insn);
4121 enum attr_type attr_type = get_attr_type (dep);
4123 /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4124 operand for INSN. If we have a shifted input operand and the
4125 instruction we depend on is another ALU instruction, then we may
4126 have to account for an additional stall. */
4127 if (shift_opnum != 0
4128 && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4130 rtx shifted_operand;
4131 int opno;
4133 /* Get the shifted operand. */
4134 extract_insn (insn);
4135 shifted_operand = recog_data.operand[shift_opnum];
4137 /* Iterate over all the operands in DEP. If we write an operand
4138 that overlaps with SHIFTED_OPERAND, then we have increase the
4139 cost of this dependency. */
4140 extract_insn (dep);
4141 preprocess_constraints ();
4142 for (opno = 0; opno < recog_data.n_operands; opno++)
4144 /* We can ignore strict inputs. */
4145 if (recog_data.operand_type[opno] == OP_IN)
4146 continue;
4148 if (reg_overlap_mentioned_p (recog_data.operand[opno],
4149 shifted_operand))
4150 return 2;
4155 /* XXX This is not strictly true for the FPA. */
4156 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4157 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4158 return 0;
4160 /* Call insns don't incur a stall, even if they follow a load. */
4161 if (REG_NOTE_KIND (link) == 0
4162 && GET_CODE (insn) == CALL_INSN)
4163 return 1;
4165 if ((i_pat = single_set (insn)) != NULL
4166 && GET_CODE (SET_SRC (i_pat)) == MEM
4167 && (d_pat = single_set (dep)) != NULL
4168 && GET_CODE (SET_DEST (d_pat)) == MEM)
4170 rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4171 /* This is a load after a store, there is no conflict if the load reads
4172 from a cached area. Assume that loads from the stack, and from the
4173 constant pool are cached, and that others will miss. This is a
4174 hack. */
4176 if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4177 || reg_mentioned_p (stack_pointer_rtx, src_mem)
4178 || reg_mentioned_p (frame_pointer_rtx, src_mem)
4179 || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4180 return 1;
4183 return cost;
4186 static int fp_consts_inited = 0;
4188 /* Only zero is valid for VFP. Other values are also valid for FPA. */
4189 static const char * const strings_fp[8] =
4191 "0", "1", "2", "3",
4192 "4", "5", "0.5", "10"
4195 static REAL_VALUE_TYPE values_fp[8];
4197 static void
4198 init_fp_table (void)
4200 int i;
4201 REAL_VALUE_TYPE r;
4203 if (TARGET_VFP)
4204 fp_consts_inited = 1;
4205 else
4206 fp_consts_inited = 8;
4208 for (i = 0; i < fp_consts_inited; i++)
4210 r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4211 values_fp[i] = r;
4215 /* Return TRUE if rtx X is a valid immediate FP constant. */
4217 arm_const_double_rtx (rtx x)
4219 REAL_VALUE_TYPE r;
4220 int i;
4222 if (!fp_consts_inited)
4223 init_fp_table ();
4225 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4226 if (REAL_VALUE_MINUS_ZERO (r))
4227 return 0;
4229 for (i = 0; i < fp_consts_inited; i++)
4230 if (REAL_VALUES_EQUAL (r, values_fp[i]))
4231 return 1;
4233 return 0;
4236 /* Return TRUE if rtx X is a valid immediate FPA constant. */
4238 neg_const_double_rtx_ok_for_fpa (rtx x)
4240 REAL_VALUE_TYPE r;
4241 int i;
4243 if (!fp_consts_inited)
4244 init_fp_table ();
4246 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4247 r = REAL_VALUE_NEGATE (r);
4248 if (REAL_VALUE_MINUS_ZERO (r))
4249 return 0;
4251 for (i = 0; i < 8; i++)
4252 if (REAL_VALUES_EQUAL (r, values_fp[i]))
4253 return 1;
4255 return 0;
4258 /* Predicates for `match_operand' and `match_operator'. */
4260 /* s_register_operand is the same as register_operand, but it doesn't accept
4261 (SUBREG (MEM)...).
4263 This function exists because at the time it was put in it led to better
4264 code. SUBREG(MEM) always needs a reload in the places where
4265 s_register_operand is used, and this seemed to lead to excessive
4266 reloading. */
4268 s_register_operand (rtx op, enum machine_mode mode)
4270 if (GET_MODE (op) != mode && mode != VOIDmode)
4271 return 0;
4273 if (GET_CODE (op) == SUBREG)
4274 op = SUBREG_REG (op);
4276 /* We don't consider registers whose class is NO_REGS
4277 to be a register operand. */
4278 /* XXX might have to check for lo regs only for thumb ??? */
4279 return (GET_CODE (op) == REG
4280 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4281 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
4284 /* A hard register operand (even before reload. */
4286 arm_hard_register_operand (rtx op, enum machine_mode mode)
4288 if (GET_MODE (op) != mode && mode != VOIDmode)
4289 return 0;
4291 return (GET_CODE (op) == REG
4292 && REGNO (op) < FIRST_PSEUDO_REGISTER);
4295 /* An arm register operand. */
4297 arm_general_register_operand (rtx op, enum machine_mode mode)
4299 if (GET_MODE (op) != mode && mode != VOIDmode)
4300 return 0;
4302 if (GET_CODE (op) == SUBREG)
4303 op = SUBREG_REG (op);
4305 return (GET_CODE (op) == REG
4306 && (REGNO (op) <= LAST_ARM_REGNUM
4307 || REGNO (op) >= FIRST_PSEUDO_REGISTER));
4310 /* Only accept reg, subreg(reg), const_int. */
4312 reg_or_int_operand (rtx op, enum machine_mode mode)
4314 if (GET_CODE (op) == CONST_INT)
4315 return 1;
4317 if (GET_MODE (op) != mode && mode != VOIDmode)
4318 return 0;
4320 if (GET_CODE (op) == SUBREG)
4321 op = SUBREG_REG (op);
4323 /* We don't consider registers whose class is NO_REGS
4324 to be a register operand. */
4325 return (GET_CODE (op) == REG
4326 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4327 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
4330 /* Return 1 if OP is an item in memory, given that we are in reload. */
4332 arm_reload_memory_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4334 int regno = true_regnum (op);
4336 return (!CONSTANT_P (op)
4337 && (regno == -1
4338 || (GET_CODE (op) == REG
4339 && REGNO (op) >= FIRST_PSEUDO_REGISTER)));
4342 /* Return TRUE for valid operands for the rhs of an ARM instruction. */
4344 arm_rhs_operand (rtx op, enum machine_mode mode)
4346 return (s_register_operand (op, mode)
4347 || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op))));
4350 /* Return TRUE for valid operands for the
4351 rhs of an ARM instruction, or a load. */
4353 arm_rhsm_operand (rtx op, enum machine_mode mode)
4355 return (s_register_operand (op, mode)
4356 || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op)))
4357 || memory_operand (op, mode));
4360 /* Return TRUE for valid operands for the rhs of an ARM instruction, or if a
4361 constant that is valid when negated. */
4363 arm_add_operand (rtx op, enum machine_mode mode)
4365 if (TARGET_THUMB)
4366 return thumb_cmp_operand (op, mode);
4368 return (s_register_operand (op, mode)
4369 || (GET_CODE (op) == CONST_INT
4370 && (const_ok_for_arm (INTVAL (op))
4371 || const_ok_for_arm (-INTVAL (op)))));
4374 /* Return TRUE for valid ARM constants (or when valid if negated). */
4376 arm_addimm_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4378 return (GET_CODE (op) == CONST_INT
4379 && (const_ok_for_arm (INTVAL (op))
4380 || const_ok_for_arm (-INTVAL (op))));
4384 arm_not_operand (rtx op, enum machine_mode mode)
4386 return (s_register_operand (op, mode)
4387 || (GET_CODE (op) == CONST_INT
4388 && (const_ok_for_arm (INTVAL (op))
4389 || const_ok_for_arm (~INTVAL (op)))));
4392 /* Return TRUE if the operand is a memory reference which contains an
4393 offsettable address. */
4395 offsettable_memory_operand (rtx op, enum machine_mode mode)
4397 if (mode == VOIDmode)
4398 mode = GET_MODE (op);
4400 return (mode == GET_MODE (op)
4401 && GET_CODE (op) == MEM
4402 && offsettable_address_p (reload_completed | reload_in_progress,
4403 mode, XEXP (op, 0)));
4406 /* Return TRUE if the operand is a memory reference which is, or can be
4407 made word aligned by adjusting the offset. */
4409 alignable_memory_operand (rtx op, enum machine_mode mode)
4411 rtx reg;
4413 if (mode == VOIDmode)
4414 mode = GET_MODE (op);
4416 if (mode != GET_MODE (op) || GET_CODE (op) != MEM)
4417 return 0;
4419 op = XEXP (op, 0);
4421 return ((GET_CODE (reg = op) == REG
4422 || (GET_CODE (op) == SUBREG
4423 && GET_CODE (reg = SUBREG_REG (op)) == REG)
4424 || (GET_CODE (op) == PLUS
4425 && GET_CODE (XEXP (op, 1)) == CONST_INT
4426 && (GET_CODE (reg = XEXP (op, 0)) == REG
4427 || (GET_CODE (XEXP (op, 0)) == SUBREG
4428 && GET_CODE (reg = SUBREG_REG (XEXP (op, 0))) == REG))))
4429 && REGNO_POINTER_ALIGN (REGNO (reg)) >= 32);
4432 /* Similar to s_register_operand, but does not allow hard integer
4433 registers. */
4435 f_register_operand (rtx op, enum machine_mode mode)
4437 if (GET_MODE (op) != mode && mode != VOIDmode)
4438 return 0;
4440 if (GET_CODE (op) == SUBREG)
4441 op = SUBREG_REG (op);
4443 /* We don't consider registers whose class is NO_REGS
4444 to be a register operand. */
4445 return (GET_CODE (op) == REG
4446 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4447 || REGNO_REG_CLASS (REGNO (op)) == FPA_REGS));
4450 /* Return TRUE for valid operands for the rhs of an floating point insns.
4451 Allows regs or certain consts on FPA, just regs for everything else. */
4453 arm_float_rhs_operand (rtx op, enum machine_mode mode)
4455 if (s_register_operand (op, mode))
4456 return TRUE;
4458 if (GET_MODE (op) != mode && mode != VOIDmode)
4459 return FALSE;
4461 if (TARGET_FPA && GET_CODE (op) == CONST_DOUBLE)
4462 return arm_const_double_rtx (op);
4464 return FALSE;
4468 arm_float_add_operand (rtx op, enum machine_mode mode)
4470 if (s_register_operand (op, mode))
4471 return TRUE;
4473 if (GET_MODE (op) != mode && mode != VOIDmode)
4474 return FALSE;
4476 if (TARGET_FPA && GET_CODE (op) == CONST_DOUBLE)
4477 return (arm_const_double_rtx (op)
4478 || neg_const_double_rtx_ok_for_fpa (op));
4480 return FALSE;
4484 /* Return TRUE if OP is suitable for the rhs of a floating point comparison.
4485 Depends which fpu we are targeting. */
4488 arm_float_compare_operand (rtx op, enum machine_mode mode)
4490 if (TARGET_VFP)
4491 return vfp_compare_operand (op, mode);
4492 else
4493 return arm_float_rhs_operand (op, mode);
4497 /* Return nonzero if OP is a valid Cirrus memory address pattern. */
4499 cirrus_memory_offset (rtx op)
4501 /* Reject eliminable registers. */
4502 if (! (reload_in_progress || reload_completed)
4503 && ( reg_mentioned_p (frame_pointer_rtx, op)
4504 || reg_mentioned_p (arg_pointer_rtx, op)
4505 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4506 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4507 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4508 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4509 return 0;
4511 if (GET_CODE (op) == MEM)
4513 rtx ind;
4515 ind = XEXP (op, 0);
4517 /* Match: (mem (reg)). */
4518 if (GET_CODE (ind) == REG)
4519 return 1;
4521 /* Match:
4522 (mem (plus (reg)
4523 (const))). */
4524 if (GET_CODE (ind) == PLUS
4525 && GET_CODE (XEXP (ind, 0)) == REG
4526 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4527 && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4528 return 1;
4531 return 0;
4535 arm_extendqisi_mem_op (rtx op, enum machine_mode mode)
4537 if (!memory_operand (op, mode))
4538 return 0;
4540 return arm_legitimate_address_p (mode, XEXP (op, 0), SIGN_EXTEND, 0);
4543 /* Return nonzero if OP is a Cirrus or general register. */
4545 cirrus_register_operand (rtx op, enum machine_mode mode)
4547 if (GET_MODE (op) != mode && mode != VOIDmode)
4548 return FALSE;
4550 if (GET_CODE (op) == SUBREG)
4551 op = SUBREG_REG (op);
4553 return (GET_CODE (op) == REG
4554 && (REGNO_REG_CLASS (REGNO (op)) == CIRRUS_REGS
4555 || REGNO_REG_CLASS (REGNO (op)) == GENERAL_REGS));
4558 /* Return nonzero if OP is a cirrus FP register. */
4560 cirrus_fp_register (rtx op, enum machine_mode mode)
4562 if (GET_MODE (op) != mode && mode != VOIDmode)
4563 return FALSE;
4565 if (GET_CODE (op) == SUBREG)
4566 op = SUBREG_REG (op);
4568 return (GET_CODE (op) == REG
4569 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4570 || REGNO_REG_CLASS (REGNO (op)) == CIRRUS_REGS));
4573 /* Return nonzero if OP is a 6bit constant (0..63). */
4575 cirrus_shift_const (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4577 return (GET_CODE (op) == CONST_INT
4578 && INTVAL (op) >= 0
4579 && INTVAL (op) < 64);
4583 /* Return TRUE if OP is a valid VFP memory address pattern.
4584 WB if true if writeback address modes are allowed. */
4587 arm_coproc_mem_operand (rtx op, bool wb)
4589 rtx ind;
4591 /* Reject eliminable registers. */
4592 if (! (reload_in_progress || reload_completed)
4593 && ( reg_mentioned_p (frame_pointer_rtx, op)
4594 || reg_mentioned_p (arg_pointer_rtx, op)
4595 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4596 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4597 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4598 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4599 return FALSE;
4601 /* Constants are converted into offsets from labels. */
4602 if (GET_CODE (op) != MEM)
4603 return FALSE;
4605 ind = XEXP (op, 0);
4607 if (reload_completed
4608 && (GET_CODE (ind) == LABEL_REF
4609 || (GET_CODE (ind) == CONST
4610 && GET_CODE (XEXP (ind, 0)) == PLUS
4611 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4612 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4613 return TRUE;
4615 /* Match: (mem (reg)). */
4616 if (GET_CODE (ind) == REG)
4617 return arm_address_register_rtx_p (ind, 0);
4619 /* Autoincremment addressing modes. */
4620 if (wb
4621 && (GET_CODE (ind) == PRE_INC
4622 || GET_CODE (ind) == POST_INC
4623 || GET_CODE (ind) == PRE_DEC
4624 || GET_CODE (ind) == POST_DEC))
4625 return arm_address_register_rtx_p (XEXP (ind, 0), 0);
4627 if (wb
4628 && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
4629 && arm_address_register_rtx_p (XEXP (ind, 0), 0)
4630 && GET_CODE (XEXP (ind, 1)) == PLUS
4631 && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
4632 ind = XEXP (ind, 1);
4634 /* Match:
4635 (plus (reg)
4636 (const)). */
4637 if (GET_CODE (ind) == PLUS
4638 && GET_CODE (XEXP (ind, 0)) == REG
4639 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4640 && GET_CODE (XEXP (ind, 1)) == CONST_INT
4641 && INTVAL (XEXP (ind, 1)) > -1024
4642 && INTVAL (XEXP (ind, 1)) < 1024
4643 && (INTVAL (XEXP (ind, 1)) & 3) == 0)
4644 return TRUE;
4646 return FALSE;
4650 /* Return TRUE if OP is a REG or constant zero. */
4652 vfp_compare_operand (rtx op, enum machine_mode mode)
4654 if (s_register_operand (op, mode))
4655 return TRUE;
4657 return (GET_CODE (op) == CONST_DOUBLE
4658 && arm_const_double_rtx (op));
4662 /* Return GENERAL_REGS if a scratch register required to reload x to/from
4663 VFP registers. Otherwise return NO_REGS. */
4665 enum reg_class
4666 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4668 if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
4669 return NO_REGS;
4671 return GENERAL_REGS;
4675 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4676 Use by the Cirrus Maverick code which has to workaround
4677 a hardware bug triggered by such instructions. */
4678 static bool
4679 arm_memory_load_p (rtx insn)
4681 rtx body, lhs, rhs;;
4683 if (insn == NULL_RTX || GET_CODE (insn) != INSN)
4684 return false;
4686 body = PATTERN (insn);
4688 if (GET_CODE (body) != SET)
4689 return false;
4691 lhs = XEXP (body, 0);
4692 rhs = XEXP (body, 1);
4694 lhs = REG_OR_SUBREG_RTX (lhs);
4696 /* If the destination is not a general purpose
4697 register we do not have to worry. */
4698 if (GET_CODE (lhs) != REG
4699 || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
4700 return false;
4702 /* As well as loads from memory we also have to react
4703 to loads of invalid constants which will be turned
4704 into loads from the minipool. */
4705 return (GET_CODE (rhs) == MEM
4706 || GET_CODE (rhs) == SYMBOL_REF
4707 || note_invalid_constants (insn, -1, false));
4710 /* Return TRUE if INSN is a Cirrus instruction. */
4711 static bool
4712 arm_cirrus_insn_p (rtx insn)
4714 enum attr_cirrus attr;
4716 /* get_attr aborts on USE and CLOBBER. */
4717 if (!insn
4718 || GET_CODE (insn) != INSN
4719 || GET_CODE (PATTERN (insn)) == USE
4720 || GET_CODE (PATTERN (insn)) == CLOBBER)
4721 return 0;
4723 attr = get_attr_cirrus (insn);
4725 return attr != CIRRUS_NOT;
4728 /* Cirrus reorg for invalid instruction combinations. */
4729 static void
4730 cirrus_reorg (rtx first)
4732 enum attr_cirrus attr;
4733 rtx body = PATTERN (first);
4734 rtx t;
4735 int nops;
4737 /* Any branch must be followed by 2 non Cirrus instructions. */
4738 if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
4740 nops = 0;
4741 t = next_nonnote_insn (first);
4743 if (arm_cirrus_insn_p (t))
4744 ++ nops;
4746 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4747 ++ nops;
4749 while (nops --)
4750 emit_insn_after (gen_nop (), first);
4752 return;
4755 /* (float (blah)) is in parallel with a clobber. */
4756 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
4757 body = XVECEXP (body, 0, 0);
4759 if (GET_CODE (body) == SET)
4761 rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
4763 /* cfldrd, cfldr64, cfstrd, cfstr64 must
4764 be followed by a non Cirrus insn. */
4765 if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
4767 if (arm_cirrus_insn_p (next_nonnote_insn (first)))
4768 emit_insn_after (gen_nop (), first);
4770 return;
4772 else if (arm_memory_load_p (first))
4774 unsigned int arm_regno;
4776 /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
4777 ldr/cfmv64hr combination where the Rd field is the same
4778 in both instructions must be split with a non Cirrus
4779 insn. Example:
4781 ldr r0, blah
4783 cfmvsr mvf0, r0. */
4785 /* Get Arm register number for ldr insn. */
4786 if (GET_CODE (lhs) == REG)
4787 arm_regno = REGNO (lhs);
4788 else if (GET_CODE (rhs) == REG)
4789 arm_regno = REGNO (rhs);
4790 else
4791 abort ();
4793 /* Next insn. */
4794 first = next_nonnote_insn (first);
4796 if (! arm_cirrus_insn_p (first))
4797 return;
4799 body = PATTERN (first);
4801 /* (float (blah)) is in parallel with a clobber. */
4802 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
4803 body = XVECEXP (body, 0, 0);
4805 if (GET_CODE (body) == FLOAT)
4806 body = XEXP (body, 0);
4808 if (get_attr_cirrus (first) == CIRRUS_MOVE
4809 && GET_CODE (XEXP (body, 1)) == REG
4810 && arm_regno == REGNO (XEXP (body, 1)))
4811 emit_insn_after (gen_nop (), first);
4813 return;
4817 /* get_attr aborts on USE and CLOBBER. */
4818 if (!first
4819 || GET_CODE (first) != INSN
4820 || GET_CODE (PATTERN (first)) == USE
4821 || GET_CODE (PATTERN (first)) == CLOBBER)
4822 return;
4824 attr = get_attr_cirrus (first);
4826 /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
4827 must be followed by a non-coprocessor instruction. */
4828 if (attr == CIRRUS_COMPARE)
4830 nops = 0;
4832 t = next_nonnote_insn (first);
4834 if (arm_cirrus_insn_p (t))
4835 ++ nops;
4837 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4838 ++ nops;
4840 while (nops --)
4841 emit_insn_after (gen_nop (), first);
4843 return;
4847 /* Return nonzero if OP is a constant power of two. */
4849 power_of_two_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4851 if (GET_CODE (op) == CONST_INT)
4853 HOST_WIDE_INT value = INTVAL (op);
4855 return value != 0 && (value & (value - 1)) == 0;
4858 return FALSE;
4861 /* Return TRUE for a valid operand of a DImode operation.
4862 Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
4863 Note that this disallows MEM(REG+REG), but allows
4864 MEM(PRE/POST_INC/DEC(REG)). */
4866 di_operand (rtx op, enum machine_mode mode)
4868 if (s_register_operand (op, mode))
4869 return TRUE;
4871 if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
4872 return FALSE;
4874 if (GET_CODE (op) == SUBREG)
4875 op = SUBREG_REG (op);
4877 switch (GET_CODE (op))
4879 case CONST_DOUBLE:
4880 case CONST_INT:
4881 return TRUE;
4883 case MEM:
4884 return memory_address_p (DImode, XEXP (op, 0));
4886 default:
4887 return FALSE;
4891 /* Like di_operand, but don't accept constants. */
4893 nonimmediate_di_operand (rtx op, enum machine_mode mode)
4895 if (s_register_operand (op, mode))
4896 return TRUE;
4898 if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
4899 return FALSE;
4901 if (GET_CODE (op) == SUBREG)
4902 op = SUBREG_REG (op);
4904 if (GET_CODE (op) == MEM)
4905 return memory_address_p (DImode, XEXP (op, 0));
4907 return FALSE;
4910 /* Return TRUE for a valid operand of a DFmode operation when soft-float.
4911 Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
4912 Note that this disallows MEM(REG+REG), but allows
4913 MEM(PRE/POST_INC/DEC(REG)). */
4915 soft_df_operand (rtx op, enum machine_mode mode)
4917 if (s_register_operand (op, mode))
4918 return TRUE;
4920 if (mode != VOIDmode && GET_MODE (op) != mode)
4921 return FALSE;
4923 if (GET_CODE (op) == SUBREG && CONSTANT_P (SUBREG_REG (op)))
4924 return FALSE;
4926 if (GET_CODE (op) == SUBREG)
4927 op = SUBREG_REG (op);
4929 switch (GET_CODE (op))
4931 case CONST_DOUBLE:
4932 return TRUE;
4934 case MEM:
4935 return memory_address_p (DFmode, XEXP (op, 0));
4937 default:
4938 return FALSE;
4942 /* Like soft_df_operand, but don't accept constants. */
4944 nonimmediate_soft_df_operand (rtx op, enum machine_mode mode)
4946 if (s_register_operand (op, mode))
4947 return TRUE;
4949 if (mode != VOIDmode && GET_MODE (op) != mode)
4950 return FALSE;
4952 if (GET_CODE (op) == SUBREG)
4953 op = SUBREG_REG (op);
4955 if (GET_CODE (op) == MEM)
4956 return memory_address_p (DFmode, XEXP (op, 0));
4957 return FALSE;
4960 /* Return TRUE for valid index operands. */
4962 index_operand (rtx op, enum machine_mode mode)
4964 return (s_register_operand (op, mode)
4965 || (immediate_operand (op, mode)
4966 && (GET_CODE (op) != CONST_INT
4967 || (INTVAL (op) < 4096 && INTVAL (op) > -4096))));
4970 /* Return TRUE for valid shifts by a constant. This also accepts any
4971 power of two on the (somewhat overly relaxed) assumption that the
4972 shift operator in this case was a mult. */
4974 const_shift_operand (rtx op, enum machine_mode mode)
4976 return (power_of_two_operand (op, mode)
4977 || (immediate_operand (op, mode)
4978 && (GET_CODE (op) != CONST_INT
4979 || (INTVAL (op) < 32 && INTVAL (op) > 0))));
4982 /* Return TRUE for arithmetic operators which can be combined with a multiply
4983 (shift). */
4985 shiftable_operator (rtx x, enum machine_mode mode)
4987 enum rtx_code code;
4989 if (GET_MODE (x) != mode)
4990 return FALSE;
4992 code = GET_CODE (x);
4994 return (code == PLUS || code == MINUS
4995 || code == IOR || code == XOR || code == AND);
4998 /* Return TRUE for binary logical operators. */
5000 logical_binary_operator (rtx x, enum machine_mode mode)
5002 enum rtx_code code;
5004 if (GET_MODE (x) != mode)
5005 return FALSE;
5007 code = GET_CODE (x);
5009 return (code == IOR || code == XOR || code == AND);
5012 /* Return TRUE for shift operators. */
5014 shift_operator (rtx x,enum machine_mode mode)
5016 enum rtx_code code;
5018 if (GET_MODE (x) != mode)
5019 return FALSE;
5021 code = GET_CODE (x);
5023 if (code == MULT)
5024 return power_of_two_operand (XEXP (x, 1), mode);
5026 return (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT
5027 || code == ROTATERT);
5030 /* Return TRUE if x is EQ or NE. */
5032 equality_operator (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
5034 return GET_CODE (x) == EQ || GET_CODE (x) == NE;
5037 /* Return TRUE if x is a comparison operator other than LTGT or UNEQ. */
5039 arm_comparison_operator (rtx x, enum machine_mode mode)
5041 return (comparison_operator (x, mode)
5042 && GET_CODE (x) != LTGT
5043 && GET_CODE (x) != UNEQ);
5046 /* Return TRUE for SMIN SMAX UMIN UMAX operators. */
5048 minmax_operator (rtx x, enum machine_mode mode)
5050 enum rtx_code code = GET_CODE (x);
5052 if (GET_MODE (x) != mode)
5053 return FALSE;
5055 return code == SMIN || code == SMAX || code == UMIN || code == UMAX;
5058 /* Return TRUE if this is the condition code register, if we aren't given
5059 a mode, accept any class CCmode register. */
5061 cc_register (rtx x, enum machine_mode mode)
5063 if (mode == VOIDmode)
5065 mode = GET_MODE (x);
5067 if (GET_MODE_CLASS (mode) != MODE_CC)
5068 return FALSE;
5071 if ( GET_MODE (x) == mode
5072 && GET_CODE (x) == REG
5073 && REGNO (x) == CC_REGNUM)
5074 return TRUE;
5076 return FALSE;
5079 /* Return TRUE if this is the condition code register, if we aren't given
5080 a mode, accept any class CCmode register which indicates a dominance
5081 expression. */
5083 dominant_cc_register (rtx x, enum machine_mode mode)
5085 if (mode == VOIDmode)
5087 mode = GET_MODE (x);
5089 if (GET_MODE_CLASS (mode) != MODE_CC)
5090 return FALSE;
5093 if (mode != CC_DNEmode && mode != CC_DEQmode
5094 && mode != CC_DLEmode && mode != CC_DLTmode
5095 && mode != CC_DGEmode && mode != CC_DGTmode
5096 && mode != CC_DLEUmode && mode != CC_DLTUmode
5097 && mode != CC_DGEUmode && mode != CC_DGTUmode)
5098 return FALSE;
5100 return cc_register (x, mode);
5103 /* Return TRUE if X references a SYMBOL_REF. */
5105 symbol_mentioned_p (rtx x)
5107 const char * fmt;
5108 int i;
5110 if (GET_CODE (x) == SYMBOL_REF)
5111 return 1;
5113 fmt = GET_RTX_FORMAT (GET_CODE (x));
5115 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5117 if (fmt[i] == 'E')
5119 int j;
5121 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5122 if (symbol_mentioned_p (XVECEXP (x, i, j)))
5123 return 1;
5125 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5126 return 1;
5129 return 0;
5132 /* Return TRUE if X references a LABEL_REF. */
5134 label_mentioned_p (rtx x)
5136 const char * fmt;
5137 int i;
5139 if (GET_CODE (x) == LABEL_REF)
5140 return 1;
5142 fmt = GET_RTX_FORMAT (GET_CODE (x));
5143 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5145 if (fmt[i] == 'E')
5147 int j;
5149 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5150 if (label_mentioned_p (XVECEXP (x, i, j)))
5151 return 1;
5153 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5154 return 1;
5157 return 0;
5160 enum rtx_code
5161 minmax_code (rtx x)
5163 enum rtx_code code = GET_CODE (x);
5165 if (code == SMAX)
5166 return GE;
5167 else if (code == SMIN)
5168 return LE;
5169 else if (code == UMIN)
5170 return LEU;
5171 else if (code == UMAX)
5172 return GEU;
5174 abort ();
5177 /* Return 1 if memory locations are adjacent. */
5179 adjacent_mem_locations (rtx a, rtx b)
5181 if ((GET_CODE (XEXP (a, 0)) == REG
5182 || (GET_CODE (XEXP (a, 0)) == PLUS
5183 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5184 && (GET_CODE (XEXP (b, 0)) == REG
5185 || (GET_CODE (XEXP (b, 0)) == PLUS
5186 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5188 int val0 = 0, val1 = 0;
5189 int reg0, reg1;
5191 if (GET_CODE (XEXP (a, 0)) == PLUS)
5193 reg0 = REGNO (XEXP (XEXP (a, 0), 0));
5194 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5196 else
5197 reg0 = REGNO (XEXP (a, 0));
5199 if (GET_CODE (XEXP (b, 0)) == PLUS)
5201 reg1 = REGNO (XEXP (XEXP (b, 0), 0));
5202 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5204 else
5205 reg1 = REGNO (XEXP (b, 0));
5207 /* Don't accept any offset that will require multiple
5208 instructions to handle, since this would cause the
5209 arith_adjacentmem pattern to output an overlong sequence. */
5210 if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5211 return 0;
5213 return (reg0 == reg1) && ((val1 - val0) == 4 || (val0 - val1) == 4);
5215 return 0;
5218 /* Return 1 if OP is a load multiple operation. It is known to be
5219 parallel and the first section will be tested. */
5221 load_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
5223 HOST_WIDE_INT count = XVECLEN (op, 0);
5224 int dest_regno;
5225 rtx src_addr;
5226 HOST_WIDE_INT i = 1, base = 0;
5227 rtx elt;
5229 if (count <= 1
5230 || GET_CODE (XVECEXP (op, 0, 0)) != SET)
5231 return 0;
5233 /* Check to see if this might be a write-back. */
5234 if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
5236 i++;
5237 base = 1;
5239 /* Now check it more carefully. */
5240 if (GET_CODE (SET_DEST (elt)) != REG
5241 || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
5242 || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
5243 || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
5244 return 0;
5247 /* Perform a quick check so we don't blow up below. */
5248 if (count <= i
5249 || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
5250 || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != REG
5251 || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != MEM)
5252 return 0;
5254 dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, i - 1)));
5255 src_addr = XEXP (SET_SRC (XVECEXP (op, 0, i - 1)), 0);
5257 for (; i < count; i++)
5259 elt = XVECEXP (op, 0, i);
5261 if (GET_CODE (elt) != SET
5262 || GET_CODE (SET_DEST (elt)) != REG
5263 || GET_MODE (SET_DEST (elt)) != SImode
5264 || REGNO (SET_DEST (elt)) != (unsigned int)(dest_regno + i - base)
5265 || GET_CODE (SET_SRC (elt)) != MEM
5266 || GET_MODE (SET_SRC (elt)) != SImode
5267 || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
5268 || !rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
5269 || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
5270 || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != (i - base) * 4)
5271 return 0;
5274 return 1;
5277 /* Return 1 if OP is a store multiple operation. It is known to be
5278 parallel and the first section will be tested. */
5280 store_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
5282 HOST_WIDE_INT count = XVECLEN (op, 0);
5283 int src_regno;
5284 rtx dest_addr;
5285 HOST_WIDE_INT i = 1, base = 0;
5286 rtx elt;
5288 if (count <= 1
5289 || GET_CODE (XVECEXP (op, 0, 0)) != SET)
5290 return 0;
5292 /* Check to see if this might be a write-back. */
5293 if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
5295 i++;
5296 base = 1;
5298 /* Now check it more carefully. */
5299 if (GET_CODE (SET_DEST (elt)) != REG
5300 || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
5301 || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
5302 || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
5303 return 0;
5306 /* Perform a quick check so we don't blow up below. */
5307 if (count <= i
5308 || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
5309 || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != MEM
5310 || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != REG)
5311 return 0;
5313 src_regno = REGNO (SET_SRC (XVECEXP (op, 0, i - 1)));
5314 dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, i - 1)), 0);
5316 for (; i < count; i++)
5318 elt = XVECEXP (op, 0, i);
5320 if (GET_CODE (elt) != SET
5321 || GET_CODE (SET_SRC (elt)) != REG
5322 || GET_MODE (SET_SRC (elt)) != SImode
5323 || REGNO (SET_SRC (elt)) != (unsigned int)(src_regno + i - base)
5324 || GET_CODE (SET_DEST (elt)) != MEM
5325 || GET_MODE (SET_DEST (elt)) != SImode
5326 || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
5327 || !rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
5328 || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
5329 || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != (i - base) * 4)
5330 return 0;
5333 return 1;
5337 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5338 HOST_WIDE_INT *load_offset)
5340 int unsorted_regs[4];
5341 HOST_WIDE_INT unsorted_offsets[4];
5342 int order[4];
5343 int base_reg = -1;
5344 int i;
5346 /* Can only handle 2, 3, or 4 insns at present,
5347 though could be easily extended if required. */
5348 if (nops < 2 || nops > 4)
5349 abort ();
5351 /* Loop over the operands and check that the memory references are
5352 suitable (ie immediate offsets from the same base register). At
5353 the same time, extract the target register, and the memory
5354 offsets. */
5355 for (i = 0; i < nops; i++)
5357 rtx reg;
5358 rtx offset;
5360 /* Convert a subreg of a mem into the mem itself. */
5361 if (GET_CODE (operands[nops + i]) == SUBREG)
5362 operands[nops + i] = alter_subreg (operands + (nops + i));
5364 if (GET_CODE (operands[nops + i]) != MEM)
5365 abort ();
5367 /* Don't reorder volatile memory references; it doesn't seem worth
5368 looking for the case where the order is ok anyway. */
5369 if (MEM_VOLATILE_P (operands[nops + i]))
5370 return 0;
5372 offset = const0_rtx;
5374 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5375 || (GET_CODE (reg) == SUBREG
5376 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5377 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5378 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5379 == REG)
5380 || (GET_CODE (reg) == SUBREG
5381 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5382 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5383 == CONST_INT)))
5385 if (i == 0)
5387 base_reg = REGNO (reg);
5388 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5389 ? REGNO (operands[i])
5390 : REGNO (SUBREG_REG (operands[i])));
5391 order[0] = 0;
5393 else
5395 if (base_reg != (int) REGNO (reg))
5396 /* Not addressed from the same base register. */
5397 return 0;
5399 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5400 ? REGNO (operands[i])
5401 : REGNO (SUBREG_REG (operands[i])));
5402 if (unsorted_regs[i] < unsorted_regs[order[0]])
5403 order[0] = i;
5406 /* If it isn't an integer register, or if it overwrites the
5407 base register but isn't the last insn in the list, then
5408 we can't do this. */
5409 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5410 || (i != nops - 1 && unsorted_regs[i] == base_reg))
5411 return 0;
5413 unsorted_offsets[i] = INTVAL (offset);
5415 else
5416 /* Not a suitable memory address. */
5417 return 0;
5420 /* All the useful information has now been extracted from the
5421 operands into unsorted_regs and unsorted_offsets; additionally,
5422 order[0] has been set to the lowest numbered register in the
5423 list. Sort the registers into order, and check that the memory
5424 offsets are ascending and adjacent. */
5426 for (i = 1; i < nops; i++)
5428 int j;
5430 order[i] = order[i - 1];
5431 for (j = 0; j < nops; j++)
5432 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5433 && (order[i] == order[i - 1]
5434 || unsorted_regs[j] < unsorted_regs[order[i]]))
5435 order[i] = j;
5437 /* Have we found a suitable register? if not, one must be used more
5438 than once. */
5439 if (order[i] == order[i - 1])
5440 return 0;
5442 /* Is the memory address adjacent and ascending? */
5443 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5444 return 0;
5447 if (base)
5449 *base = base_reg;
5451 for (i = 0; i < nops; i++)
5452 regs[i] = unsorted_regs[order[i]];
5454 *load_offset = unsorted_offsets[order[0]];
5457 if (unsorted_offsets[order[0]] == 0)
5458 return 1; /* ldmia */
5460 if (unsorted_offsets[order[0]] == 4)
5461 return 2; /* ldmib */
5463 if (unsorted_offsets[order[nops - 1]] == 0)
5464 return 3; /* ldmda */
5466 if (unsorted_offsets[order[nops - 1]] == -4)
5467 return 4; /* ldmdb */
5469 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5470 if the offset isn't small enough. The reason 2 ldrs are faster
5471 is because these ARMs are able to do more than one cache access
5472 in a single cycle. The ARM9 and StrongARM have Harvard caches,
5473 whilst the ARM8 has a double bandwidth cache. This means that
5474 these cores can do both an instruction fetch and a data fetch in
5475 a single cycle, so the trick of calculating the address into a
5476 scratch register (one of the result regs) and then doing a load
5477 multiple actually becomes slower (and no smaller in code size).
5478 That is the transformation
5480 ldr rd1, [rbase + offset]
5481 ldr rd2, [rbase + offset + 4]
5485 add rd1, rbase, offset
5486 ldmia rd1, {rd1, rd2}
5488 produces worse code -- '3 cycles + any stalls on rd2' instead of
5489 '2 cycles + any stalls on rd2'. On ARMs with only one cache
5490 access per cycle, the first sequence could never complete in less
5491 than 6 cycles, whereas the ldm sequence would only take 5 and
5492 would make better use of sequential accesses if not hitting the
5493 cache.
5495 We cheat here and test 'arm_ld_sched' which we currently know to
5496 only be true for the ARM8, ARM9 and StrongARM. If this ever
5497 changes, then the test below needs to be reworked. */
5498 if (nops == 2 && arm_ld_sched)
5499 return 0;
5501 /* Can't do it without setting up the offset, only do this if it takes
5502 no more than one insn. */
5503 return (const_ok_for_arm (unsorted_offsets[order[0]])
5504 || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5507 const char *
5508 emit_ldm_seq (rtx *operands, int nops)
5510 int regs[4];
5511 int base_reg;
5512 HOST_WIDE_INT offset;
5513 char buf[100];
5514 int i;
5516 switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5518 case 1:
5519 strcpy (buf, "ldm%?ia\t");
5520 break;
5522 case 2:
5523 strcpy (buf, "ldm%?ib\t");
5524 break;
5526 case 3:
5527 strcpy (buf, "ldm%?da\t");
5528 break;
5530 case 4:
5531 strcpy (buf, "ldm%?db\t");
5532 break;
5534 case 5:
5535 if (offset >= 0)
5536 sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5537 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5538 (long) offset);
5539 else
5540 sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5541 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5542 (long) -offset);
5543 output_asm_insn (buf, operands);
5544 base_reg = regs[0];
5545 strcpy (buf, "ldm%?ia\t");
5546 break;
5548 default:
5549 abort ();
5552 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5553 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5555 for (i = 1; i < nops; i++)
5556 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5557 reg_names[regs[i]]);
5559 strcat (buf, "}\t%@ phole ldm");
5561 output_asm_insn (buf, operands);
5562 return "";
5566 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5567 HOST_WIDE_INT * load_offset)
5569 int unsorted_regs[4];
5570 HOST_WIDE_INT unsorted_offsets[4];
5571 int order[4];
5572 int base_reg = -1;
5573 int i;
5575 /* Can only handle 2, 3, or 4 insns at present, though could be easily
5576 extended if required. */
5577 if (nops < 2 || nops > 4)
5578 abort ();
5580 /* Loop over the operands and check that the memory references are
5581 suitable (ie immediate offsets from the same base register). At
5582 the same time, extract the target register, and the memory
5583 offsets. */
5584 for (i = 0; i < nops; i++)
5586 rtx reg;
5587 rtx offset;
5589 /* Convert a subreg of a mem into the mem itself. */
5590 if (GET_CODE (operands[nops + i]) == SUBREG)
5591 operands[nops + i] = alter_subreg (operands + (nops + i));
5593 if (GET_CODE (operands[nops + i]) != MEM)
5594 abort ();
5596 /* Don't reorder volatile memory references; it doesn't seem worth
5597 looking for the case where the order is ok anyway. */
5598 if (MEM_VOLATILE_P (operands[nops + i]))
5599 return 0;
5601 offset = const0_rtx;
5603 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5604 || (GET_CODE (reg) == SUBREG
5605 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5606 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5607 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5608 == REG)
5609 || (GET_CODE (reg) == SUBREG
5610 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5611 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5612 == CONST_INT)))
5614 if (i == 0)
5616 base_reg = REGNO (reg);
5617 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5618 ? REGNO (operands[i])
5619 : REGNO (SUBREG_REG (operands[i])));
5620 order[0] = 0;
5622 else
5624 if (base_reg != (int) REGNO (reg))
5625 /* Not addressed from the same base register. */
5626 return 0;
5628 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5629 ? REGNO (operands[i])
5630 : REGNO (SUBREG_REG (operands[i])));
5631 if (unsorted_regs[i] < unsorted_regs[order[0]])
5632 order[0] = i;
5635 /* If it isn't an integer register, then we can't do this. */
5636 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5637 return 0;
5639 unsorted_offsets[i] = INTVAL (offset);
5641 else
5642 /* Not a suitable memory address. */
5643 return 0;
5646 /* All the useful information has now been extracted from the
5647 operands into unsorted_regs and unsorted_offsets; additionally,
5648 order[0] has been set to the lowest numbered register in the
5649 list. Sort the registers into order, and check that the memory
5650 offsets are ascending and adjacent. */
5652 for (i = 1; i < nops; i++)
5654 int j;
5656 order[i] = order[i - 1];
5657 for (j = 0; j < nops; j++)
5658 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5659 && (order[i] == order[i - 1]
5660 || unsorted_regs[j] < unsorted_regs[order[i]]))
5661 order[i] = j;
5663 /* Have we found a suitable register? if not, one must be used more
5664 than once. */
5665 if (order[i] == order[i - 1])
5666 return 0;
5668 /* Is the memory address adjacent and ascending? */
5669 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5670 return 0;
5673 if (base)
5675 *base = base_reg;
5677 for (i = 0; i < nops; i++)
5678 regs[i] = unsorted_regs[order[i]];
5680 *load_offset = unsorted_offsets[order[0]];
5683 if (unsorted_offsets[order[0]] == 0)
5684 return 1; /* stmia */
5686 if (unsorted_offsets[order[0]] == 4)
5687 return 2; /* stmib */
5689 if (unsorted_offsets[order[nops - 1]] == 0)
5690 return 3; /* stmda */
5692 if (unsorted_offsets[order[nops - 1]] == -4)
5693 return 4; /* stmdb */
5695 return 0;
5698 const char *
5699 emit_stm_seq (rtx *operands, int nops)
5701 int regs[4];
5702 int base_reg;
5703 HOST_WIDE_INT offset;
5704 char buf[100];
5705 int i;
5707 switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5709 case 1:
5710 strcpy (buf, "stm%?ia\t");
5711 break;
5713 case 2:
5714 strcpy (buf, "stm%?ib\t");
5715 break;
5717 case 3:
5718 strcpy (buf, "stm%?da\t");
5719 break;
5721 case 4:
5722 strcpy (buf, "stm%?db\t");
5723 break;
5725 default:
5726 abort ();
5729 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5730 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5732 for (i = 1; i < nops; i++)
5733 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5734 reg_names[regs[i]]);
5736 strcat (buf, "}\t%@ phole stm");
5738 output_asm_insn (buf, operands);
5739 return "";
5743 multi_register_push (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
5745 if (GET_CODE (op) != PARALLEL
5746 || (GET_CODE (XVECEXP (op, 0, 0)) != SET)
5747 || (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC)
5748 || (XINT (SET_SRC (XVECEXP (op, 0, 0)), 1) != UNSPEC_PUSH_MULT))
5749 return 0;
5751 return 1;
5754 /* Routines for use in generating RTL. */
5757 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5758 int write_back, int unchanging_p, int in_struct_p,
5759 int scalar_p)
5761 int i = 0, j;
5762 rtx result;
5763 int sign = up ? 1 : -1;
5764 rtx mem;
5766 /* XScale has load-store double instructions, but they have stricter
5767 alignment requirements than load-store multiple, so we can not
5768 use them.
5770 For XScale ldm requires 2 + NREGS cycles to complete and blocks
5771 the pipeline until completion.
5773 NREGS CYCLES
5779 An ldr instruction takes 1-3 cycles, but does not block the
5780 pipeline.
5782 NREGS CYCLES
5783 1 1-3
5784 2 2-6
5785 3 3-9
5786 4 4-12
5788 Best case ldr will always win. However, the more ldr instructions
5789 we issue, the less likely we are to be able to schedule them well.
5790 Using ldr instructions also increases code size.
5792 As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5793 for counts of 3 or 4 regs. */
5794 if (arm_tune_xscale && count <= 2 && ! optimize_size)
5796 rtx seq;
5798 start_sequence ();
5800 for (i = 0; i < count; i++)
5802 mem = gen_rtx_MEM (SImode, plus_constant (from, i * 4 * sign));
5803 RTX_UNCHANGING_P (mem) = unchanging_p;
5804 MEM_IN_STRUCT_P (mem) = in_struct_p;
5805 MEM_SCALAR_P (mem) = scalar_p;
5806 emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5809 if (write_back)
5810 emit_move_insn (from, plus_constant (from, count * 4 * sign));
5812 seq = get_insns ();
5813 end_sequence ();
5815 return seq;
5818 result = gen_rtx_PARALLEL (VOIDmode,
5819 rtvec_alloc (count + (write_back ? 1 : 0)));
5820 if (write_back)
5822 XVECEXP (result, 0, 0)
5823 = gen_rtx_SET (GET_MODE (from), from,
5824 plus_constant (from, count * 4 * sign));
5825 i = 1;
5826 count++;
5829 for (j = 0; i < count; i++, j++)
5831 mem = gen_rtx_MEM (SImode, plus_constant (from, j * 4 * sign));
5832 RTX_UNCHANGING_P (mem) = unchanging_p;
5833 MEM_IN_STRUCT_P (mem) = in_struct_p;
5834 MEM_SCALAR_P (mem) = scalar_p;
5835 XVECEXP (result, 0, i)
5836 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5839 return result;
5843 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5844 int write_back, int unchanging_p, int in_struct_p,
5845 int scalar_p)
5847 int i = 0, j;
5848 rtx result;
5849 int sign = up ? 1 : -1;
5850 rtx mem;
5852 /* See arm_gen_load_multiple for discussion of
5853 the pros/cons of ldm/stm usage for XScale. */
5854 if (arm_tune_xscale && count <= 2 && ! optimize_size)
5856 rtx seq;
5858 start_sequence ();
5860 for (i = 0; i < count; i++)
5862 mem = gen_rtx_MEM (SImode, plus_constant (to, i * 4 * sign));
5863 RTX_UNCHANGING_P (mem) = unchanging_p;
5864 MEM_IN_STRUCT_P (mem) = in_struct_p;
5865 MEM_SCALAR_P (mem) = scalar_p;
5866 emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5869 if (write_back)
5870 emit_move_insn (to, plus_constant (to, count * 4 * sign));
5872 seq = get_insns ();
5873 end_sequence ();
5875 return seq;
5878 result = gen_rtx_PARALLEL (VOIDmode,
5879 rtvec_alloc (count + (write_back ? 1 : 0)));
5880 if (write_back)
5882 XVECEXP (result, 0, 0)
5883 = gen_rtx_SET (GET_MODE (to), to,
5884 plus_constant (to, count * 4 * sign));
5885 i = 1;
5886 count++;
5889 for (j = 0; i < count; i++, j++)
5891 mem = gen_rtx_MEM (SImode, plus_constant (to, j * 4 * sign));
5892 RTX_UNCHANGING_P (mem) = unchanging_p;
5893 MEM_IN_STRUCT_P (mem) = in_struct_p;
5894 MEM_SCALAR_P (mem) = scalar_p;
5896 XVECEXP (result, 0, i)
5897 = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5900 return result;
5904 arm_gen_movmemqi (rtx *operands)
5906 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5907 int i;
5908 rtx src, dst;
5909 rtx st_src, st_dst, fin_src, fin_dst;
5910 rtx part_bytes_reg = NULL;
5911 rtx mem;
5912 int dst_unchanging_p, dst_in_struct_p, src_unchanging_p, src_in_struct_p;
5913 int dst_scalar_p, src_scalar_p;
5915 if (GET_CODE (operands[2]) != CONST_INT
5916 || GET_CODE (operands[3]) != CONST_INT
5917 || INTVAL (operands[2]) > 64
5918 || INTVAL (operands[3]) & 3)
5919 return 0;
5921 st_dst = XEXP (operands[0], 0);
5922 st_src = XEXP (operands[1], 0);
5924 dst_unchanging_p = RTX_UNCHANGING_P (operands[0]);
5925 dst_in_struct_p = MEM_IN_STRUCT_P (operands[0]);
5926 dst_scalar_p = MEM_SCALAR_P (operands[0]);
5927 src_unchanging_p = RTX_UNCHANGING_P (operands[1]);
5928 src_in_struct_p = MEM_IN_STRUCT_P (operands[1]);
5929 src_scalar_p = MEM_SCALAR_P (operands[1]);
5931 fin_dst = dst = copy_to_mode_reg (SImode, st_dst);
5932 fin_src = src = copy_to_mode_reg (SImode, st_src);
5934 in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5935 out_words_to_go = INTVAL (operands[2]) / 4;
5936 last_bytes = INTVAL (operands[2]) & 3;
5938 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5939 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5941 for (i = 0; in_words_to_go >= 2; i+=4)
5943 if (in_words_to_go > 4)
5944 emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5945 src_unchanging_p,
5946 src_in_struct_p,
5947 src_scalar_p));
5948 else
5949 emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
5950 FALSE, src_unchanging_p,
5951 src_in_struct_p, src_scalar_p));
5953 if (out_words_to_go)
5955 if (out_words_to_go > 4)
5956 emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5957 dst_unchanging_p,
5958 dst_in_struct_p,
5959 dst_scalar_p));
5960 else if (out_words_to_go != 1)
5961 emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5962 dst, TRUE,
5963 (last_bytes == 0
5964 ? FALSE : TRUE),
5965 dst_unchanging_p,
5966 dst_in_struct_p,
5967 dst_scalar_p));
5968 else
5970 mem = gen_rtx_MEM (SImode, dst);
5971 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
5972 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
5973 MEM_SCALAR_P (mem) = dst_scalar_p;
5974 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5975 if (last_bytes != 0)
5976 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5980 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
5981 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
5984 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
5985 if (out_words_to_go)
5987 rtx sreg;
5989 mem = gen_rtx_MEM (SImode, src);
5990 RTX_UNCHANGING_P (mem) = src_unchanging_p;
5991 MEM_IN_STRUCT_P (mem) = src_in_struct_p;
5992 MEM_SCALAR_P (mem) = src_scalar_p;
5993 emit_move_insn (sreg = gen_reg_rtx (SImode), mem);
5994 emit_move_insn (fin_src = gen_reg_rtx (SImode), plus_constant (src, 4));
5996 mem = gen_rtx_MEM (SImode, dst);
5997 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
5998 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
5999 MEM_SCALAR_P (mem) = dst_scalar_p;
6000 emit_move_insn (mem, sreg);
6001 emit_move_insn (fin_dst = gen_reg_rtx (SImode), plus_constant (dst, 4));
6002 in_words_to_go--;
6004 if (in_words_to_go) /* Sanity check */
6005 abort ();
6008 if (in_words_to_go)
6010 if (in_words_to_go < 0)
6011 abort ();
6013 mem = gen_rtx_MEM (SImode, src);
6014 RTX_UNCHANGING_P (mem) = src_unchanging_p;
6015 MEM_IN_STRUCT_P (mem) = src_in_struct_p;
6016 MEM_SCALAR_P (mem) = src_scalar_p;
6017 part_bytes_reg = copy_to_mode_reg (SImode, mem);
6020 if (last_bytes && part_bytes_reg == NULL)
6021 abort ();
6023 if (BYTES_BIG_ENDIAN && last_bytes)
6025 rtx tmp = gen_reg_rtx (SImode);
6027 /* The bytes we want are in the top end of the word. */
6028 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
6029 GEN_INT (8 * (4 - last_bytes))));
6030 part_bytes_reg = tmp;
6032 while (last_bytes)
6034 mem = gen_rtx_MEM (QImode, plus_constant (dst, last_bytes - 1));
6035 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6036 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6037 MEM_SCALAR_P (mem) = dst_scalar_p;
6038 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6040 if (--last_bytes)
6042 tmp = gen_reg_rtx (SImode);
6043 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
6044 part_bytes_reg = tmp;
6049 else
6051 if (last_bytes > 1)
6053 mem = gen_rtx_MEM (HImode, dst);
6054 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6055 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6056 MEM_SCALAR_P (mem) = dst_scalar_p;
6057 emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
6058 last_bytes -= 2;
6059 if (last_bytes)
6061 rtx tmp = gen_reg_rtx (SImode);
6063 emit_insn (gen_addsi3 (dst, dst, const2_rtx));
6064 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
6065 part_bytes_reg = tmp;
6069 if (last_bytes)
6071 mem = gen_rtx_MEM (QImode, dst);
6072 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6073 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6074 MEM_SCALAR_P (mem) = dst_scalar_p;
6075 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6079 return 1;
6082 /* Generate a memory reference for a half word, such that it will be loaded
6083 into the top 16 bits of the word. We can assume that the address is
6084 known to be alignable and of the form reg, or plus (reg, const). */
6087 arm_gen_rotated_half_load (rtx memref)
6089 HOST_WIDE_INT offset = 0;
6090 rtx base = XEXP (memref, 0);
6092 if (GET_CODE (base) == PLUS)
6094 offset = INTVAL (XEXP (base, 1));
6095 base = XEXP (base, 0);
6098 /* If we aren't allowed to generate unaligned addresses, then fail. */
6099 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0))
6100 return NULL;
6102 base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
6104 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
6105 return base;
6107 return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
6110 /* Select a dominance comparison mode if possible for a test of the general
6111 form (OP (COND_OR (X) (Y)) (const_int 0)). We support three forms.
6112 COND_OR == DOM_CC_X_AND_Y => (X && Y)
6113 COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6114 COND_OR == DOM_CC_X_OR_Y => (X || Y)
6115 In all cases OP will be either EQ or NE, but we don't need to know which
6116 here. If we are unable to support a dominance comparison we return
6117 CC mode. This will then fail to match for the RTL expressions that
6118 generate this call. */
6119 enum machine_mode
6120 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
6122 enum rtx_code cond1, cond2;
6123 int swapped = 0;
6125 /* Currently we will probably get the wrong result if the individual
6126 comparisons are not simple. This also ensures that it is safe to
6127 reverse a comparison if necessary. */
6128 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6129 != CCmode)
6130 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6131 != CCmode))
6132 return CCmode;
6134 /* The if_then_else variant of this tests the second condition if the
6135 first passes, but is true if the first fails. Reverse the first
6136 condition to get a true "inclusive-or" expression. */
6137 if (cond_or == DOM_CC_NX_OR_Y)
6138 cond1 = reverse_condition (cond1);
6140 /* If the comparisons are not equal, and one doesn't dominate the other,
6141 then we can't do this. */
6142 if (cond1 != cond2
6143 && !comparison_dominates_p (cond1, cond2)
6144 && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6145 return CCmode;
6147 if (swapped)
6149 enum rtx_code temp = cond1;
6150 cond1 = cond2;
6151 cond2 = temp;
6154 switch (cond1)
6156 case EQ:
6157 if (cond2 == EQ || cond_or == DOM_CC_X_AND_Y)
6158 return CC_DEQmode;
6160 switch (cond2)
6162 case LE: return CC_DLEmode;
6163 case LEU: return CC_DLEUmode;
6164 case GE: return CC_DGEmode;
6165 case GEU: return CC_DGEUmode;
6166 default: break;
6169 break;
6171 case LT:
6172 if (cond2 == LT || cond_or == DOM_CC_X_AND_Y)
6173 return CC_DLTmode;
6174 if (cond2 == LE)
6175 return CC_DLEmode;
6176 if (cond2 == NE)
6177 return CC_DNEmode;
6178 break;
6180 case GT:
6181 if (cond2 == GT || cond_or == DOM_CC_X_AND_Y)
6182 return CC_DGTmode;
6183 if (cond2 == GE)
6184 return CC_DGEmode;
6185 if (cond2 == NE)
6186 return CC_DNEmode;
6187 break;
6189 case LTU:
6190 if (cond2 == LTU || cond_or == DOM_CC_X_AND_Y)
6191 return CC_DLTUmode;
6192 if (cond2 == LEU)
6193 return CC_DLEUmode;
6194 if (cond2 == NE)
6195 return CC_DNEmode;
6196 break;
6198 case GTU:
6199 if (cond2 == GTU || cond_or == DOM_CC_X_AND_Y)
6200 return CC_DGTUmode;
6201 if (cond2 == GEU)
6202 return CC_DGEUmode;
6203 if (cond2 == NE)
6204 return CC_DNEmode;
6205 break;
6207 /* The remaining cases only occur when both comparisons are the
6208 same. */
6209 case NE:
6210 return CC_DNEmode;
6212 case LE:
6213 return CC_DLEmode;
6215 case GE:
6216 return CC_DGEmode;
6218 case LEU:
6219 return CC_DLEUmode;
6221 case GEU:
6222 return CC_DGEUmode;
6224 default:
6225 break;
6228 abort ();
6231 enum machine_mode
6232 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6234 /* All floating point compares return CCFP if it is an equality
6235 comparison, and CCFPE otherwise. */
6236 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6238 switch (op)
6240 case EQ:
6241 case NE:
6242 case UNORDERED:
6243 case ORDERED:
6244 case UNLT:
6245 case UNLE:
6246 case UNGT:
6247 case UNGE:
6248 case UNEQ:
6249 case LTGT:
6250 return CCFPmode;
6252 case LT:
6253 case LE:
6254 case GT:
6255 case GE:
6256 if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6257 return CCFPmode;
6258 return CCFPEmode;
6260 default:
6261 abort ();
6265 /* A compare with a shifted operand. Because of canonicalization, the
6266 comparison will have to be swapped when we emit the assembler. */
6267 if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6268 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6269 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6270 || GET_CODE (x) == ROTATERT))
6271 return CC_SWPmode;
6273 /* This is a special case that is used by combine to allow a
6274 comparison of a shifted byte load to be split into a zero-extend
6275 followed by a comparison of the shifted integer (only valid for
6276 equalities and unsigned inequalities). */
6277 if (GET_MODE (x) == SImode
6278 && GET_CODE (x) == ASHIFT
6279 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6280 && GET_CODE (XEXP (x, 0)) == SUBREG
6281 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6282 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6283 && (op == EQ || op == NE
6284 || op == GEU || op == GTU || op == LTU || op == LEU)
6285 && GET_CODE (y) == CONST_INT)
6286 return CC_Zmode;
6288 /* A construct for a conditional compare, if the false arm contains
6289 0, then both conditions must be true, otherwise either condition
6290 must be true. Not all conditions are possible, so CCmode is
6291 returned if it can't be done. */
6292 if (GET_CODE (x) == IF_THEN_ELSE
6293 && (XEXP (x, 2) == const0_rtx
6294 || XEXP (x, 2) == const1_rtx)
6295 && COMPARISON_P (XEXP (x, 0))
6296 && COMPARISON_P (XEXP (x, 1)))
6297 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6298 INTVAL (XEXP (x, 2)));
6300 /* Alternate canonicalizations of the above. These are somewhat cleaner. */
6301 if (GET_CODE (x) == AND
6302 && COMPARISON_P (XEXP (x, 0))
6303 && COMPARISON_P (XEXP (x, 1)))
6304 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6305 DOM_CC_X_AND_Y);
6307 if (GET_CODE (x) == IOR
6308 && COMPARISON_P (XEXP (x, 0))
6309 && COMPARISON_P (XEXP (x, 1)))
6310 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6311 DOM_CC_X_OR_Y);
6313 /* An operation (on Thumb) where we want to test for a single bit.
6314 This is done by shifting that bit up into the top bit of a
6315 scratch register; we can then branch on the sign bit. */
6316 if (TARGET_THUMB
6317 && GET_MODE (x) == SImode
6318 && (op == EQ || op == NE)
6319 && (GET_CODE (x) == ZERO_EXTRACT))
6320 return CC_Nmode;
6322 /* An operation that sets the condition codes as a side-effect, the
6323 V flag is not set correctly, so we can only use comparisons where
6324 this doesn't matter. (For LT and GE we can use "mi" and "pl"
6325 instead.) */
6326 if (GET_MODE (x) == SImode
6327 && y == const0_rtx
6328 && (op == EQ || op == NE || op == LT || op == GE)
6329 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6330 || GET_CODE (x) == AND || GET_CODE (x) == IOR
6331 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6332 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6333 || GET_CODE (x) == LSHIFTRT
6334 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6335 || GET_CODE (x) == ROTATERT
6336 || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6337 return CC_NOOVmode;
6339 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6340 return CC_Zmode;
6342 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6343 && GET_CODE (x) == PLUS
6344 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6345 return CC_Cmode;
6347 return CCmode;
6350 /* X and Y are two things to compare using CODE. Emit the compare insn and
6351 return the rtx for register 0 in the proper mode. FP means this is a
6352 floating point compare: I don't think that it is needed on the arm. */
6354 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6356 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6357 rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6359 emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6360 gen_rtx_COMPARE (mode, x, y)));
6362 return cc_reg;
6365 /* Generate a sequence of insns that will generate the correct return
6366 address mask depending on the physical architecture that the program
6367 is running on. */
6369 arm_gen_return_addr_mask (void)
6371 rtx reg = gen_reg_rtx (Pmode);
6373 emit_insn (gen_return_addr_mask (reg));
6374 return reg;
6377 void
6378 arm_reload_in_hi (rtx *operands)
6380 rtx ref = operands[1];
6381 rtx base, scratch;
6382 HOST_WIDE_INT offset = 0;
6384 if (GET_CODE (ref) == SUBREG)
6386 offset = SUBREG_BYTE (ref);
6387 ref = SUBREG_REG (ref);
6390 if (GET_CODE (ref) == REG)
6392 /* We have a pseudo which has been spilt onto the stack; there
6393 are two cases here: the first where there is a simple
6394 stack-slot replacement and a second where the stack-slot is
6395 out of range, or is used as a subreg. */
6396 if (reg_equiv_mem[REGNO (ref)])
6398 ref = reg_equiv_mem[REGNO (ref)];
6399 base = find_replacement (&XEXP (ref, 0));
6401 else
6402 /* The slot is out of range, or was dressed up in a SUBREG. */
6403 base = reg_equiv_address[REGNO (ref)];
6405 else
6406 base = find_replacement (&XEXP (ref, 0));
6408 /* Handle the case where the address is too complex to be offset by 1. */
6409 if (GET_CODE (base) == MINUS
6410 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6412 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6414 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6415 base = base_plus;
6417 else if (GET_CODE (base) == PLUS)
6419 /* The addend must be CONST_INT, or we would have dealt with it above. */
6420 HOST_WIDE_INT hi, lo;
6422 offset += INTVAL (XEXP (base, 1));
6423 base = XEXP (base, 0);
6425 /* Rework the address into a legal sequence of insns. */
6426 /* Valid range for lo is -4095 -> 4095 */
6427 lo = (offset >= 0
6428 ? (offset & 0xfff)
6429 : -((-offset) & 0xfff));
6431 /* Corner case, if lo is the max offset then we would be out of range
6432 once we have added the additional 1 below, so bump the msb into the
6433 pre-loading insn(s). */
6434 if (lo == 4095)
6435 lo &= 0x7ff;
6437 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6438 ^ (HOST_WIDE_INT) 0x80000000)
6439 - (HOST_WIDE_INT) 0x80000000);
6441 if (hi + lo != offset)
6442 abort ();
6444 if (hi != 0)
6446 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6448 /* Get the base address; addsi3 knows how to handle constants
6449 that require more than one insn. */
6450 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6451 base = base_plus;
6452 offset = lo;
6456 /* Operands[2] may overlap operands[0] (though it won't overlap
6457 operands[1]), that's why we asked for a DImode reg -- so we can
6458 use the bit that does not overlap. */
6459 if (REGNO (operands[2]) == REGNO (operands[0]))
6460 scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6461 else
6462 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6464 emit_insn (gen_zero_extendqisi2 (scratch,
6465 gen_rtx_MEM (QImode,
6466 plus_constant (base,
6467 offset))));
6468 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6469 gen_rtx_MEM (QImode,
6470 plus_constant (base,
6471 offset + 1))));
6472 if (!BYTES_BIG_ENDIAN)
6473 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6474 gen_rtx_IOR (SImode,
6475 gen_rtx_ASHIFT
6476 (SImode,
6477 gen_rtx_SUBREG (SImode, operands[0], 0),
6478 GEN_INT (8)),
6479 scratch)));
6480 else
6481 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6482 gen_rtx_IOR (SImode,
6483 gen_rtx_ASHIFT (SImode, scratch,
6484 GEN_INT (8)),
6485 gen_rtx_SUBREG (SImode, operands[0],
6486 0))));
6489 /* Handle storing a half-word to memory during reload by synthesizing as two
6490 byte stores. Take care not to clobber the input values until after we
6491 have moved them somewhere safe. This code assumes that if the DImode
6492 scratch in operands[2] overlaps either the input value or output address
6493 in some way, then that value must die in this insn (we absolutely need
6494 two scratch registers for some corner cases). */
6495 void
6496 arm_reload_out_hi (rtx *operands)
6498 rtx ref = operands[0];
6499 rtx outval = operands[1];
6500 rtx base, scratch;
6501 HOST_WIDE_INT offset = 0;
6503 if (GET_CODE (ref) == SUBREG)
6505 offset = SUBREG_BYTE (ref);
6506 ref = SUBREG_REG (ref);
6509 if (GET_CODE (ref) == REG)
6511 /* We have a pseudo which has been spilt onto the stack; there
6512 are two cases here: the first where there is a simple
6513 stack-slot replacement and a second where the stack-slot is
6514 out of range, or is used as a subreg. */
6515 if (reg_equiv_mem[REGNO (ref)])
6517 ref = reg_equiv_mem[REGNO (ref)];
6518 base = find_replacement (&XEXP (ref, 0));
6520 else
6521 /* The slot is out of range, or was dressed up in a SUBREG. */
6522 base = reg_equiv_address[REGNO (ref)];
6524 else
6525 base = find_replacement (&XEXP (ref, 0));
6527 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6529 /* Handle the case where the address is too complex to be offset by 1. */
6530 if (GET_CODE (base) == MINUS
6531 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6533 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6535 /* Be careful not to destroy OUTVAL. */
6536 if (reg_overlap_mentioned_p (base_plus, outval))
6538 /* Updating base_plus might destroy outval, see if we can
6539 swap the scratch and base_plus. */
6540 if (!reg_overlap_mentioned_p (scratch, outval))
6542 rtx tmp = scratch;
6543 scratch = base_plus;
6544 base_plus = tmp;
6546 else
6548 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6550 /* Be conservative and copy OUTVAL into the scratch now,
6551 this should only be necessary if outval is a subreg
6552 of something larger than a word. */
6553 /* XXX Might this clobber base? I can't see how it can,
6554 since scratch is known to overlap with OUTVAL, and
6555 must be wider than a word. */
6556 emit_insn (gen_movhi (scratch_hi, outval));
6557 outval = scratch_hi;
6561 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6562 base = base_plus;
6564 else if (GET_CODE (base) == PLUS)
6566 /* The addend must be CONST_INT, or we would have dealt with it above. */
6567 HOST_WIDE_INT hi, lo;
6569 offset += INTVAL (XEXP (base, 1));
6570 base = XEXP (base, 0);
6572 /* Rework the address into a legal sequence of insns. */
6573 /* Valid range for lo is -4095 -> 4095 */
6574 lo = (offset >= 0
6575 ? (offset & 0xfff)
6576 : -((-offset) & 0xfff));
6578 /* Corner case, if lo is the max offset then we would be out of range
6579 once we have added the additional 1 below, so bump the msb into the
6580 pre-loading insn(s). */
6581 if (lo == 4095)
6582 lo &= 0x7ff;
6584 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6585 ^ (HOST_WIDE_INT) 0x80000000)
6586 - (HOST_WIDE_INT) 0x80000000);
6588 if (hi + lo != offset)
6589 abort ();
6591 if (hi != 0)
6593 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6595 /* Be careful not to destroy OUTVAL. */
6596 if (reg_overlap_mentioned_p (base_plus, outval))
6598 /* Updating base_plus might destroy outval, see if we
6599 can swap the scratch and base_plus. */
6600 if (!reg_overlap_mentioned_p (scratch, outval))
6602 rtx tmp = scratch;
6603 scratch = base_plus;
6604 base_plus = tmp;
6606 else
6608 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6610 /* Be conservative and copy outval into scratch now,
6611 this should only be necessary if outval is a
6612 subreg of something larger than a word. */
6613 /* XXX Might this clobber base? I can't see how it
6614 can, since scratch is known to overlap with
6615 outval. */
6616 emit_insn (gen_movhi (scratch_hi, outval));
6617 outval = scratch_hi;
6621 /* Get the base address; addsi3 knows how to handle constants
6622 that require more than one insn. */
6623 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6624 base = base_plus;
6625 offset = lo;
6629 if (BYTES_BIG_ENDIAN)
6631 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6632 plus_constant (base, offset + 1)),
6633 gen_lowpart (QImode, outval)));
6634 emit_insn (gen_lshrsi3 (scratch,
6635 gen_rtx_SUBREG (SImode, outval, 0),
6636 GEN_INT (8)));
6637 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6638 gen_lowpart (QImode, scratch)));
6640 else
6642 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6643 gen_lowpart (QImode, outval)));
6644 emit_insn (gen_lshrsi3 (scratch,
6645 gen_rtx_SUBREG (SImode, outval, 0),
6646 GEN_INT (8)));
6647 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6648 plus_constant (base, offset + 1)),
6649 gen_lowpart (QImode, scratch)));
6653 /* Print a symbolic form of X to the debug file, F. */
6654 static void
6655 arm_print_value (FILE *f, rtx x)
6657 switch (GET_CODE (x))
6659 case CONST_INT:
6660 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6661 return;
6663 case CONST_DOUBLE:
6664 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6665 return;
6667 case CONST_VECTOR:
6669 int i;
6671 fprintf (f, "<");
6672 for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6674 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6675 if (i < (CONST_VECTOR_NUNITS (x) - 1))
6676 fputc (',', f);
6678 fprintf (f, ">");
6680 return;
6682 case CONST_STRING:
6683 fprintf (f, "\"%s\"", XSTR (x, 0));
6684 return;
6686 case SYMBOL_REF:
6687 fprintf (f, "`%s'", XSTR (x, 0));
6688 return;
6690 case LABEL_REF:
6691 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6692 return;
6694 case CONST:
6695 arm_print_value (f, XEXP (x, 0));
6696 return;
6698 case PLUS:
6699 arm_print_value (f, XEXP (x, 0));
6700 fprintf (f, "+");
6701 arm_print_value (f, XEXP (x, 1));
6702 return;
6704 case PC:
6705 fprintf (f, "pc");
6706 return;
6708 default:
6709 fprintf (f, "????");
6710 return;
6714 /* Routines for manipulation of the constant pool. */
6716 /* Arm instructions cannot load a large constant directly into a
6717 register; they have to come from a pc relative load. The constant
6718 must therefore be placed in the addressable range of the pc
6719 relative load. Depending on the precise pc relative load
6720 instruction the range is somewhere between 256 bytes and 4k. This
6721 means that we often have to dump a constant inside a function, and
6722 generate code to branch around it.
6724 It is important to minimize this, since the branches will slow
6725 things down and make the code larger.
6727 Normally we can hide the table after an existing unconditional
6728 branch so that there is no interruption of the flow, but in the
6729 worst case the code looks like this:
6731 ldr rn, L1
6733 b L2
6734 align
6735 L1: .long value
6739 ldr rn, L3
6741 b L4
6742 align
6743 L3: .long value
6747 We fix this by performing a scan after scheduling, which notices
6748 which instructions need to have their operands fetched from the
6749 constant table and builds the table.
6751 The algorithm starts by building a table of all the constants that
6752 need fixing up and all the natural barriers in the function (places
6753 where a constant table can be dropped without breaking the flow).
6754 For each fixup we note how far the pc-relative replacement will be
6755 able to reach and the offset of the instruction into the function.
6757 Having built the table we then group the fixes together to form
6758 tables that are as large as possible (subject to addressing
6759 constraints) and emit each table of constants after the last
6760 barrier that is within range of all the instructions in the group.
6761 If a group does not contain a barrier, then we forcibly create one
6762 by inserting a jump instruction into the flow. Once the table has
6763 been inserted, the insns are then modified to reference the
6764 relevant entry in the pool.
6766 Possible enhancements to the algorithm (not implemented) are:
6768 1) For some processors and object formats, there may be benefit in
6769 aligning the pools to the start of cache lines; this alignment
6770 would need to be taken into account when calculating addressability
6771 of a pool. */
6773 /* These typedefs are located at the start of this file, so that
6774 they can be used in the prototypes there. This comment is to
6775 remind readers of that fact so that the following structures
6776 can be understood more easily.
6778 typedef struct minipool_node Mnode;
6779 typedef struct minipool_fixup Mfix; */
6781 struct minipool_node
6783 /* Doubly linked chain of entries. */
6784 Mnode * next;
6785 Mnode * prev;
6786 /* The maximum offset into the code that this entry can be placed. While
6787 pushing fixes for forward references, all entries are sorted in order
6788 of increasing max_address. */
6789 HOST_WIDE_INT max_address;
6790 /* Similarly for an entry inserted for a backwards ref. */
6791 HOST_WIDE_INT min_address;
6792 /* The number of fixes referencing this entry. This can become zero
6793 if we "unpush" an entry. In this case we ignore the entry when we
6794 come to emit the code. */
6795 int refcount;
6796 /* The offset from the start of the minipool. */
6797 HOST_WIDE_INT offset;
6798 /* The value in table. */
6799 rtx value;
6800 /* The mode of value. */
6801 enum machine_mode mode;
6802 /* The size of the value. With iWMMXt enabled
6803 sizes > 4 also imply an alignment of 8-bytes. */
6804 int fix_size;
6807 struct minipool_fixup
6809 Mfix * next;
6810 rtx insn;
6811 HOST_WIDE_INT address;
6812 rtx * loc;
6813 enum machine_mode mode;
6814 int fix_size;
6815 rtx value;
6816 Mnode * minipool;
6817 HOST_WIDE_INT forwards;
6818 HOST_WIDE_INT backwards;
6821 /* Fixes less than a word need padding out to a word boundary. */
6822 #define MINIPOOL_FIX_SIZE(mode) \
6823 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6825 static Mnode * minipool_vector_head;
6826 static Mnode * minipool_vector_tail;
6827 static rtx minipool_vector_label;
6829 /* The linked list of all minipool fixes required for this function. */
6830 Mfix * minipool_fix_head;
6831 Mfix * minipool_fix_tail;
6832 /* The fix entry for the current minipool, once it has been placed. */
6833 Mfix * minipool_barrier;
6835 /* Determines if INSN is the start of a jump table. Returns the end
6836 of the TABLE or NULL_RTX. */
6837 static rtx
6838 is_jump_table (rtx insn)
6840 rtx table;
6842 if (GET_CODE (insn) == JUMP_INSN
6843 && JUMP_LABEL (insn) != NULL
6844 && ((table = next_real_insn (JUMP_LABEL (insn)))
6845 == next_real_insn (insn))
6846 && table != NULL
6847 && GET_CODE (table) == JUMP_INSN
6848 && (GET_CODE (PATTERN (table)) == ADDR_VEC
6849 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6850 return table;
6852 return NULL_RTX;
6855 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6856 #define JUMP_TABLES_IN_TEXT_SECTION 0
6857 #endif
6859 static HOST_WIDE_INT
6860 get_jump_table_size (rtx insn)
6862 /* ADDR_VECs only take room if read-only data does into the text
6863 section. */
6864 if (JUMP_TABLES_IN_TEXT_SECTION
6865 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6866 || 1
6867 #endif
6870 rtx body = PATTERN (insn);
6871 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6873 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6876 return 0;
6879 /* Move a minipool fix MP from its current location to before MAX_MP.
6880 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6881 constraints may need updating. */
6882 static Mnode *
6883 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6884 HOST_WIDE_INT max_address)
6886 /* This should never be true and the code below assumes these are
6887 different. */
6888 if (mp == max_mp)
6889 abort ();
6891 if (max_mp == NULL)
6893 if (max_address < mp->max_address)
6894 mp->max_address = max_address;
6896 else
6898 if (max_address > max_mp->max_address - mp->fix_size)
6899 mp->max_address = max_mp->max_address - mp->fix_size;
6900 else
6901 mp->max_address = max_address;
6903 /* Unlink MP from its current position. Since max_mp is non-null,
6904 mp->prev must be non-null. */
6905 mp->prev->next = mp->next;
6906 if (mp->next != NULL)
6907 mp->next->prev = mp->prev;
6908 else
6909 minipool_vector_tail = mp->prev;
6911 /* Re-insert it before MAX_MP. */
6912 mp->next = max_mp;
6913 mp->prev = max_mp->prev;
6914 max_mp->prev = mp;
6916 if (mp->prev != NULL)
6917 mp->prev->next = mp;
6918 else
6919 minipool_vector_head = mp;
6922 /* Save the new entry. */
6923 max_mp = mp;
6925 /* Scan over the preceding entries and adjust their addresses as
6926 required. */
6927 while (mp->prev != NULL
6928 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6930 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6931 mp = mp->prev;
6934 return max_mp;
6937 /* Add a constant to the minipool for a forward reference. Returns the
6938 node added or NULL if the constant will not fit in this pool. */
6939 static Mnode *
6940 add_minipool_forward_ref (Mfix *fix)
6942 /* If set, max_mp is the first pool_entry that has a lower
6943 constraint than the one we are trying to add. */
6944 Mnode * max_mp = NULL;
6945 HOST_WIDE_INT max_address = fix->address + fix->forwards;
6946 Mnode * mp;
6948 /* If this fix's address is greater than the address of the first
6949 entry, then we can't put the fix in this pool. We subtract the
6950 size of the current fix to ensure that if the table is fully
6951 packed we still have enough room to insert this value by suffling
6952 the other fixes forwards. */
6953 if (minipool_vector_head &&
6954 fix->address >= minipool_vector_head->max_address - fix->fix_size)
6955 return NULL;
6957 /* Scan the pool to see if a constant with the same value has
6958 already been added. While we are doing this, also note the
6959 location where we must insert the constant if it doesn't already
6960 exist. */
6961 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6963 if (GET_CODE (fix->value) == GET_CODE (mp->value)
6964 && fix->mode == mp->mode
6965 && (GET_CODE (fix->value) != CODE_LABEL
6966 || (CODE_LABEL_NUMBER (fix->value)
6967 == CODE_LABEL_NUMBER (mp->value)))
6968 && rtx_equal_p (fix->value, mp->value))
6970 /* More than one fix references this entry. */
6971 mp->refcount++;
6972 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6975 /* Note the insertion point if necessary. */
6976 if (max_mp == NULL
6977 && mp->max_address > max_address)
6978 max_mp = mp;
6980 /* If we are inserting an 8-bytes aligned quantity and
6981 we have not already found an insertion point, then
6982 make sure that all such 8-byte aligned quantities are
6983 placed at the start of the pool. */
6984 if (ARM_DOUBLEWORD_ALIGN
6985 && max_mp == NULL
6986 && fix->fix_size == 8
6987 && mp->fix_size != 8)
6989 max_mp = mp;
6990 max_address = mp->max_address;
6994 /* The value is not currently in the minipool, so we need to create
6995 a new entry for it. If MAX_MP is NULL, the entry will be put on
6996 the end of the list since the placement is less constrained than
6997 any existing entry. Otherwise, we insert the new fix before
6998 MAX_MP and, if necessary, adjust the constraints on the other
6999 entries. */
7000 mp = xmalloc (sizeof (* mp));
7001 mp->fix_size = fix->fix_size;
7002 mp->mode = fix->mode;
7003 mp->value = fix->value;
7004 mp->refcount = 1;
7005 /* Not yet required for a backwards ref. */
7006 mp->min_address = -65536;
7008 if (max_mp == NULL)
7010 mp->max_address = max_address;
7011 mp->next = NULL;
7012 mp->prev = minipool_vector_tail;
7014 if (mp->prev == NULL)
7016 minipool_vector_head = mp;
7017 minipool_vector_label = gen_label_rtx ();
7019 else
7020 mp->prev->next = mp;
7022 minipool_vector_tail = mp;
7024 else
7026 if (max_address > max_mp->max_address - mp->fix_size)
7027 mp->max_address = max_mp->max_address - mp->fix_size;
7028 else
7029 mp->max_address = max_address;
7031 mp->next = max_mp;
7032 mp->prev = max_mp->prev;
7033 max_mp->prev = mp;
7034 if (mp->prev != NULL)
7035 mp->prev->next = mp;
7036 else
7037 minipool_vector_head = mp;
7040 /* Save the new entry. */
7041 max_mp = mp;
7043 /* Scan over the preceding entries and adjust their addresses as
7044 required. */
7045 while (mp->prev != NULL
7046 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7048 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7049 mp = mp->prev;
7052 return max_mp;
7055 static Mnode *
7056 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7057 HOST_WIDE_INT min_address)
7059 HOST_WIDE_INT offset;
7061 /* This should never be true, and the code below assumes these are
7062 different. */
7063 if (mp == min_mp)
7064 abort ();
7066 if (min_mp == NULL)
7068 if (min_address > mp->min_address)
7069 mp->min_address = min_address;
7071 else
7073 /* We will adjust this below if it is too loose. */
7074 mp->min_address = min_address;
7076 /* Unlink MP from its current position. Since min_mp is non-null,
7077 mp->next must be non-null. */
7078 mp->next->prev = mp->prev;
7079 if (mp->prev != NULL)
7080 mp->prev->next = mp->next;
7081 else
7082 minipool_vector_head = mp->next;
7084 /* Reinsert it after MIN_MP. */
7085 mp->prev = min_mp;
7086 mp->next = min_mp->next;
7087 min_mp->next = mp;
7088 if (mp->next != NULL)
7089 mp->next->prev = mp;
7090 else
7091 minipool_vector_tail = mp;
7094 min_mp = mp;
7096 offset = 0;
7097 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7099 mp->offset = offset;
7100 if (mp->refcount > 0)
7101 offset += mp->fix_size;
7103 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7104 mp->next->min_address = mp->min_address + mp->fix_size;
7107 return min_mp;
7110 /* Add a constant to the minipool for a backward reference. Returns the
7111 node added or NULL if the constant will not fit in this pool.
7113 Note that the code for insertion for a backwards reference can be
7114 somewhat confusing because the calculated offsets for each fix do
7115 not take into account the size of the pool (which is still under
7116 construction. */
7117 static Mnode *
7118 add_minipool_backward_ref (Mfix *fix)
7120 /* If set, min_mp is the last pool_entry that has a lower constraint
7121 than the one we are trying to add. */
7122 Mnode *min_mp = NULL;
7123 /* This can be negative, since it is only a constraint. */
7124 HOST_WIDE_INT min_address = fix->address - fix->backwards;
7125 Mnode *mp;
7127 /* If we can't reach the current pool from this insn, or if we can't
7128 insert this entry at the end of the pool without pushing other
7129 fixes out of range, then we don't try. This ensures that we
7130 can't fail later on. */
7131 if (min_address >= minipool_barrier->address
7132 || (minipool_vector_tail->min_address + fix->fix_size
7133 >= minipool_barrier->address))
7134 return NULL;
7136 /* Scan the pool to see if a constant with the same value has
7137 already been added. While we are doing this, also note the
7138 location where we must insert the constant if it doesn't already
7139 exist. */
7140 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7142 if (GET_CODE (fix->value) == GET_CODE (mp->value)
7143 && fix->mode == mp->mode
7144 && (GET_CODE (fix->value) != CODE_LABEL
7145 || (CODE_LABEL_NUMBER (fix->value)
7146 == CODE_LABEL_NUMBER (mp->value)))
7147 && rtx_equal_p (fix->value, mp->value)
7148 /* Check that there is enough slack to move this entry to the
7149 end of the table (this is conservative). */
7150 && (mp->max_address
7151 > (minipool_barrier->address
7152 + minipool_vector_tail->offset
7153 + minipool_vector_tail->fix_size)))
7155 mp->refcount++;
7156 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7159 if (min_mp != NULL)
7160 mp->min_address += fix->fix_size;
7161 else
7163 /* Note the insertion point if necessary. */
7164 if (mp->min_address < min_address)
7166 /* For now, we do not allow the insertion of 8-byte alignment
7167 requiring nodes anywhere but at the start of the pool. */
7168 if (ARM_DOUBLEWORD_ALIGN
7169 && fix->fix_size == 8 && mp->fix_size != 8)
7170 return NULL;
7171 else
7172 min_mp = mp;
7174 else if (mp->max_address
7175 < minipool_barrier->address + mp->offset + fix->fix_size)
7177 /* Inserting before this entry would push the fix beyond
7178 its maximum address (which can happen if we have
7179 re-located a forwards fix); force the new fix to come
7180 after it. */
7181 min_mp = mp;
7182 min_address = mp->min_address + fix->fix_size;
7184 /* If we are inserting an 8-bytes aligned quantity and
7185 we have not already found an insertion point, then
7186 make sure that all such 8-byte aligned quantities are
7187 placed at the start of the pool. */
7188 else if (ARM_DOUBLEWORD_ALIGN
7189 && min_mp == NULL
7190 && fix->fix_size == 8
7191 && mp->fix_size < 8)
7193 min_mp = mp;
7194 min_address = mp->min_address + fix->fix_size;
7199 /* We need to create a new entry. */
7200 mp = xmalloc (sizeof (* mp));
7201 mp->fix_size = fix->fix_size;
7202 mp->mode = fix->mode;
7203 mp->value = fix->value;
7204 mp->refcount = 1;
7205 mp->max_address = minipool_barrier->address + 65536;
7207 mp->min_address = min_address;
7209 if (min_mp == NULL)
7211 mp->prev = NULL;
7212 mp->next = minipool_vector_head;
7214 if (mp->next == NULL)
7216 minipool_vector_tail = mp;
7217 minipool_vector_label = gen_label_rtx ();
7219 else
7220 mp->next->prev = mp;
7222 minipool_vector_head = mp;
7224 else
7226 mp->next = min_mp->next;
7227 mp->prev = min_mp;
7228 min_mp->next = mp;
7230 if (mp->next != NULL)
7231 mp->next->prev = mp;
7232 else
7233 minipool_vector_tail = mp;
7236 /* Save the new entry. */
7237 min_mp = mp;
7239 if (mp->prev)
7240 mp = mp->prev;
7241 else
7242 mp->offset = 0;
7244 /* Scan over the following entries and adjust their offsets. */
7245 while (mp->next != NULL)
7247 if (mp->next->min_address < mp->min_address + mp->fix_size)
7248 mp->next->min_address = mp->min_address + mp->fix_size;
7250 if (mp->refcount)
7251 mp->next->offset = mp->offset + mp->fix_size;
7252 else
7253 mp->next->offset = mp->offset;
7255 mp = mp->next;
7258 return min_mp;
7261 static void
7262 assign_minipool_offsets (Mfix *barrier)
7264 HOST_WIDE_INT offset = 0;
7265 Mnode *mp;
7267 minipool_barrier = barrier;
7269 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7271 mp->offset = offset;
7273 if (mp->refcount > 0)
7274 offset += mp->fix_size;
7278 /* Output the literal table */
7279 static void
7280 dump_minipool (rtx scan)
7282 Mnode * mp;
7283 Mnode * nmp;
7284 int align64 = 0;
7286 if (ARM_DOUBLEWORD_ALIGN)
7287 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7288 if (mp->refcount > 0 && mp->fix_size == 8)
7290 align64 = 1;
7291 break;
7294 if (dump_file)
7295 fprintf (dump_file,
7296 ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7297 INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7299 scan = emit_label_after (gen_label_rtx (), scan);
7300 scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7301 scan = emit_label_after (minipool_vector_label, scan);
7303 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7305 if (mp->refcount > 0)
7307 if (dump_file)
7309 fprintf (dump_file,
7310 ";; Offset %u, min %ld, max %ld ",
7311 (unsigned) mp->offset, (unsigned long) mp->min_address,
7312 (unsigned long) mp->max_address);
7313 arm_print_value (dump_file, mp->value);
7314 fputc ('\n', dump_file);
7317 switch (mp->fix_size)
7319 #ifdef HAVE_consttable_1
7320 case 1:
7321 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7322 break;
7324 #endif
7325 #ifdef HAVE_consttable_2
7326 case 2:
7327 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7328 break;
7330 #endif
7331 #ifdef HAVE_consttable_4
7332 case 4:
7333 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7334 break;
7336 #endif
7337 #ifdef HAVE_consttable_8
7338 case 8:
7339 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7340 break;
7342 #endif
7343 default:
7344 abort ();
7345 break;
7349 nmp = mp->next;
7350 free (mp);
7353 minipool_vector_head = minipool_vector_tail = NULL;
7354 scan = emit_insn_after (gen_consttable_end (), scan);
7355 scan = emit_barrier_after (scan);
7358 /* Return the cost of forcibly inserting a barrier after INSN. */
7359 static int
7360 arm_barrier_cost (rtx insn)
7362 /* Basing the location of the pool on the loop depth is preferable,
7363 but at the moment, the basic block information seems to be
7364 corrupt by this stage of the compilation. */
7365 int base_cost = 50;
7366 rtx next = next_nonnote_insn (insn);
7368 if (next != NULL && GET_CODE (next) == CODE_LABEL)
7369 base_cost -= 20;
7371 switch (GET_CODE (insn))
7373 case CODE_LABEL:
7374 /* It will always be better to place the table before the label, rather
7375 than after it. */
7376 return 50;
7378 case INSN:
7379 case CALL_INSN:
7380 return base_cost;
7382 case JUMP_INSN:
7383 return base_cost - 10;
7385 default:
7386 return base_cost + 10;
7390 /* Find the best place in the insn stream in the range
7391 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7392 Create the barrier by inserting a jump and add a new fix entry for
7393 it. */
7394 static Mfix *
7395 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7397 HOST_WIDE_INT count = 0;
7398 rtx barrier;
7399 rtx from = fix->insn;
7400 rtx selected = from;
7401 int selected_cost;
7402 HOST_WIDE_INT selected_address;
7403 Mfix * new_fix;
7404 HOST_WIDE_INT max_count = max_address - fix->address;
7405 rtx label = gen_label_rtx ();
7407 selected_cost = arm_barrier_cost (from);
7408 selected_address = fix->address;
7410 while (from && count < max_count)
7412 rtx tmp;
7413 int new_cost;
7415 /* This code shouldn't have been called if there was a natural barrier
7416 within range. */
7417 if (GET_CODE (from) == BARRIER)
7418 abort ();
7420 /* Count the length of this insn. */
7421 count += get_attr_length (from);
7423 /* If there is a jump table, add its length. */
7424 tmp = is_jump_table (from);
7425 if (tmp != NULL)
7427 count += get_jump_table_size (tmp);
7429 /* Jump tables aren't in a basic block, so base the cost on
7430 the dispatch insn. If we select this location, we will
7431 still put the pool after the table. */
7432 new_cost = arm_barrier_cost (from);
7434 if (count < max_count && new_cost <= selected_cost)
7436 selected = tmp;
7437 selected_cost = new_cost;
7438 selected_address = fix->address + count;
7441 /* Continue after the dispatch table. */
7442 from = NEXT_INSN (tmp);
7443 continue;
7446 new_cost = arm_barrier_cost (from);
7448 if (count < max_count && new_cost <= selected_cost)
7450 selected = from;
7451 selected_cost = new_cost;
7452 selected_address = fix->address + count;
7455 from = NEXT_INSN (from);
7458 /* Create a new JUMP_INSN that branches around a barrier. */
7459 from = emit_jump_insn_after (gen_jump (label), selected);
7460 JUMP_LABEL (from) = label;
7461 barrier = emit_barrier_after (from);
7462 emit_label_after (label, barrier);
7464 /* Create a minipool barrier entry for the new barrier. */
7465 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7466 new_fix->insn = barrier;
7467 new_fix->address = selected_address;
7468 new_fix->next = fix->next;
7469 fix->next = new_fix;
7471 return new_fix;
7474 /* Record that there is a natural barrier in the insn stream at
7475 ADDRESS. */
7476 static void
7477 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7479 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7481 fix->insn = insn;
7482 fix->address = address;
7484 fix->next = NULL;
7485 if (minipool_fix_head != NULL)
7486 minipool_fix_tail->next = fix;
7487 else
7488 minipool_fix_head = fix;
7490 minipool_fix_tail = fix;
7493 /* Record INSN, which will need fixing up to load a value from the
7494 minipool. ADDRESS is the offset of the insn since the start of the
7495 function; LOC is a pointer to the part of the insn which requires
7496 fixing; VALUE is the constant that must be loaded, which is of type
7497 MODE. */
7498 static void
7499 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7500 enum machine_mode mode, rtx value)
7502 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7504 #ifdef AOF_ASSEMBLER
7505 /* PIC symbol references need to be converted into offsets into the
7506 based area. */
7507 /* XXX This shouldn't be done here. */
7508 if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7509 value = aof_pic_entry (value);
7510 #endif /* AOF_ASSEMBLER */
7512 fix->insn = insn;
7513 fix->address = address;
7514 fix->loc = loc;
7515 fix->mode = mode;
7516 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7517 fix->value = value;
7518 fix->forwards = get_attr_pool_range (insn);
7519 fix->backwards = get_attr_neg_pool_range (insn);
7520 fix->minipool = NULL;
7522 /* If an insn doesn't have a range defined for it, then it isn't
7523 expecting to be reworked by this code. Better to abort now than
7524 to generate duff assembly code. */
7525 if (fix->forwards == 0 && fix->backwards == 0)
7526 abort ();
7528 /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7529 So there might be an empty word before the start of the pool.
7530 Hence we reduce the forward range by 4 to allow for this
7531 possibility. */
7532 if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7533 fix->forwards -= 4;
7535 if (dump_file)
7537 fprintf (dump_file,
7538 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7539 GET_MODE_NAME (mode),
7540 INSN_UID (insn), (unsigned long) address,
7541 -1 * (long)fix->backwards, (long)fix->forwards);
7542 arm_print_value (dump_file, fix->value);
7543 fprintf (dump_file, "\n");
7546 /* Add it to the chain of fixes. */
7547 fix->next = NULL;
7549 if (minipool_fix_head != NULL)
7550 minipool_fix_tail->next = fix;
7551 else
7552 minipool_fix_head = fix;
7554 minipool_fix_tail = fix;
7557 /* Scan INSN and note any of its operands that need fixing.
7558 If DO_PUSHES is false we do not actually push any of the fixups
7559 needed. The function returns TRUE is any fixups were needed/pushed.
7560 This is used by arm_memory_load_p() which needs to know about loads
7561 of constants that will be converted into minipool loads. */
7562 static bool
7563 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7565 bool result = false;
7566 int opno;
7568 extract_insn (insn);
7570 if (!constrain_operands (1))
7571 fatal_insn_not_found (insn);
7573 if (recog_data.n_alternatives == 0)
7574 return false;
7576 /* Fill in recog_op_alt with information about the constraints of this insn. */
7577 preprocess_constraints ();
7579 for (opno = 0; opno < recog_data.n_operands; opno++)
7581 /* Things we need to fix can only occur in inputs. */
7582 if (recog_data.operand_type[opno] != OP_IN)
7583 continue;
7585 /* If this alternative is a memory reference, then any mention
7586 of constants in this alternative is really to fool reload
7587 into allowing us to accept one there. We need to fix them up
7588 now so that we output the right code. */
7589 if (recog_op_alt[opno][which_alternative].memory_ok)
7591 rtx op = recog_data.operand[opno];
7593 if (CONSTANT_P (op))
7595 if (do_pushes)
7596 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7597 recog_data.operand_mode[opno], op);
7598 result = true;
7600 else if (GET_CODE (op) == MEM
7601 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7602 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
7604 if (do_pushes)
7606 rtx cop = avoid_constant_pool_reference (op);
7608 /* Casting the address of something to a mode narrower
7609 than a word can cause avoid_constant_pool_reference()
7610 to return the pool reference itself. That's no good to
7611 us here. Lets just hope that we can use the
7612 constant pool value directly. */
7613 if (op == cop)
7614 cop = get_pool_constant (XEXP (op, 0));
7616 push_minipool_fix (insn, address,
7617 recog_data.operand_loc[opno],
7618 recog_data.operand_mode[opno], cop);
7621 result = true;
7626 return result;
7629 /* Gcc puts the pool in the wrong place for ARM, since we can only
7630 load addresses a limited distance around the pc. We do some
7631 special munging to move the constant pool values to the correct
7632 point in the code. */
7633 static void
7634 arm_reorg (void)
7636 rtx insn;
7637 HOST_WIDE_INT address = 0;
7638 Mfix * fix;
7640 minipool_fix_head = minipool_fix_tail = NULL;
7642 /* The first insn must always be a note, or the code below won't
7643 scan it properly. */
7644 insn = get_insns ();
7645 if (GET_CODE (insn) != NOTE)
7646 abort ();
7648 /* Scan all the insns and record the operands that will need fixing. */
7649 for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
7651 if (TARGET_CIRRUS_FIX_INVALID_INSNS
7652 && (arm_cirrus_insn_p (insn)
7653 || GET_CODE (insn) == JUMP_INSN
7654 || arm_memory_load_p (insn)))
7655 cirrus_reorg (insn);
7657 if (GET_CODE (insn) == BARRIER)
7658 push_minipool_barrier (insn, address);
7659 else if (INSN_P (insn))
7661 rtx table;
7663 note_invalid_constants (insn, address, true);
7664 address += get_attr_length (insn);
7666 /* If the insn is a vector jump, add the size of the table
7667 and skip the table. */
7668 if ((table = is_jump_table (insn)) != NULL)
7670 address += get_jump_table_size (table);
7671 insn = table;
7676 fix = minipool_fix_head;
7678 /* Now scan the fixups and perform the required changes. */
7679 while (fix)
7681 Mfix * ftmp;
7682 Mfix * fdel;
7683 Mfix * last_added_fix;
7684 Mfix * last_barrier = NULL;
7685 Mfix * this_fix;
7687 /* Skip any further barriers before the next fix. */
7688 while (fix && GET_CODE (fix->insn) == BARRIER)
7689 fix = fix->next;
7691 /* No more fixes. */
7692 if (fix == NULL)
7693 break;
7695 last_added_fix = NULL;
7697 for (ftmp = fix; ftmp; ftmp = ftmp->next)
7699 if (GET_CODE (ftmp->insn) == BARRIER)
7701 if (ftmp->address >= minipool_vector_head->max_address)
7702 break;
7704 last_barrier = ftmp;
7706 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7707 break;
7709 last_added_fix = ftmp; /* Keep track of the last fix added. */
7712 /* If we found a barrier, drop back to that; any fixes that we
7713 could have reached but come after the barrier will now go in
7714 the next mini-pool. */
7715 if (last_barrier != NULL)
7717 /* Reduce the refcount for those fixes that won't go into this
7718 pool after all. */
7719 for (fdel = last_barrier->next;
7720 fdel && fdel != ftmp;
7721 fdel = fdel->next)
7723 fdel->minipool->refcount--;
7724 fdel->minipool = NULL;
7727 ftmp = last_barrier;
7729 else
7731 /* ftmp is first fix that we can't fit into this pool and
7732 there no natural barriers that we could use. Insert a
7733 new barrier in the code somewhere between the previous
7734 fix and this one, and arrange to jump around it. */
7735 HOST_WIDE_INT max_address;
7737 /* The last item on the list of fixes must be a barrier, so
7738 we can never run off the end of the list of fixes without
7739 last_barrier being set. */
7740 if (ftmp == NULL)
7741 abort ();
7743 max_address = minipool_vector_head->max_address;
7744 /* Check that there isn't another fix that is in range that
7745 we couldn't fit into this pool because the pool was
7746 already too large: we need to put the pool before such an
7747 instruction. */
7748 if (ftmp->address < max_address)
7749 max_address = ftmp->address;
7751 last_barrier = create_fix_barrier (last_added_fix, max_address);
7754 assign_minipool_offsets (last_barrier);
7756 while (ftmp)
7758 if (GET_CODE (ftmp->insn) != BARRIER
7759 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7760 == NULL))
7761 break;
7763 ftmp = ftmp->next;
7766 /* Scan over the fixes we have identified for this pool, fixing them
7767 up and adding the constants to the pool itself. */
7768 for (this_fix = fix; this_fix && ftmp != this_fix;
7769 this_fix = this_fix->next)
7770 if (GET_CODE (this_fix->insn) != BARRIER)
7772 rtx addr
7773 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
7774 minipool_vector_label),
7775 this_fix->minipool->offset);
7776 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7779 dump_minipool (last_barrier->insn);
7780 fix = ftmp;
7783 /* From now on we must synthesize any constants that we can't handle
7784 directly. This can happen if the RTL gets split during final
7785 instruction generation. */
7786 after_arm_reorg = 1;
7788 /* Free the minipool memory. */
7789 obstack_free (&minipool_obstack, minipool_startobj);
7792 /* Routines to output assembly language. */
7794 /* If the rtx is the correct value then return the string of the number.
7795 In this way we can ensure that valid double constants are generated even
7796 when cross compiling. */
7797 const char *
7798 fp_immediate_constant (rtx x)
7800 REAL_VALUE_TYPE r;
7801 int i;
7803 if (!fp_consts_inited)
7804 init_fp_table ();
7806 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7807 for (i = 0; i < 8; i++)
7808 if (REAL_VALUES_EQUAL (r, values_fp[i]))
7809 return strings_fp[i];
7811 abort ();
7814 /* As for fp_immediate_constant, but value is passed directly, not in rtx. */
7815 static const char *
7816 fp_const_from_val (REAL_VALUE_TYPE *r)
7818 int i;
7820 if (!fp_consts_inited)
7821 init_fp_table ();
7823 for (i = 0; i < 8; i++)
7824 if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7825 return strings_fp[i];
7827 abort ();
7830 /* Output the operands of a LDM/STM instruction to STREAM.
7831 MASK is the ARM register set mask of which only bits 0-15 are important.
7832 REG is the base register, either the frame pointer or the stack pointer,
7833 INSTR is the possibly suffixed load or store instruction. */
7834 static void
7835 print_multi_reg (FILE *stream, const char *instr, int reg, int mask)
7837 int i;
7838 int not_first = FALSE;
7840 fputc ('\t', stream);
7841 asm_fprintf (stream, instr, reg);
7842 fputs (", {", stream);
7844 for (i = 0; i <= LAST_ARM_REGNUM; i++)
7845 if (mask & (1 << i))
7847 if (not_first)
7848 fprintf (stream, ", ");
7850 asm_fprintf (stream, "%r", i);
7851 not_first = TRUE;
7854 fprintf (stream, "}\n");
7858 /* Output a FLDMX instruction to STREAM.
7859 BASE if the register containing the address.
7860 REG and COUNT specify the register range.
7861 Extra registers may be added to avoid hardware bugs. */
7863 static void
7864 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
7866 int i;
7868 /* Workaround ARM10 VFPr1 bug. */
7869 if (count == 2 && !arm_arch6)
7871 if (reg == 15)
7872 reg--;
7873 count++;
7876 fputc ('\t', stream);
7877 asm_fprintf (stream, "fldmfdx\t%r!, {", base);
7879 for (i = reg; i < reg + count; i++)
7881 if (i > reg)
7882 fputs (", ", stream);
7883 asm_fprintf (stream, "d%d", i);
7885 fputs ("}\n", stream);
7890 /* Output the assembly for a store multiple. */
7892 const char *
7893 vfp_output_fstmx (rtx * operands)
7895 char pattern[100];
7896 int p;
7897 int base;
7898 int i;
7900 strcpy (pattern, "fstmfdx\t%m0!, {%P1");
7901 p = strlen (pattern);
7903 if (GET_CODE (operands[1]) != REG)
7904 abort ();
7906 base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
7907 for (i = 1; i < XVECLEN (operands[2], 0); i++)
7909 p += sprintf (&pattern[p], ", d%d", base + i);
7911 strcpy (&pattern[p], "}");
7913 output_asm_insn (pattern, operands);
7914 return "";
7918 /* Emit RTL to save block of VFP register pairs to the stack. Returns the
7919 number of bytes pushed. */
7921 static int
7922 vfp_emit_fstmx (int base_reg, int count)
7924 rtx par;
7925 rtx dwarf;
7926 rtx tmp, reg;
7927 int i;
7929 /* Workaround ARM10 VFPr1 bug. Data corruption can occur when exactly two
7930 register pairs are stored by a store multiple insn. We avoid this
7931 by pushing an extra pair. */
7932 if (count == 2 && !arm_arch6)
7934 if (base_reg == LAST_VFP_REGNUM - 3)
7935 base_reg -= 2;
7936 count++;
7939 /* ??? The frame layout is implementation defined. We describe
7940 standard format 1 (equivalent to a FSTMD insn and unused pad word).
7941 We really need some way of representing the whole block so that the
7942 unwinder can figure it out at runtime. */
7943 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7944 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
7946 reg = gen_rtx_REG (DFmode, base_reg);
7947 base_reg += 2;
7949 XVECEXP (par, 0, 0)
7950 = gen_rtx_SET (VOIDmode,
7951 gen_rtx_MEM (BLKmode,
7952 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7953 gen_rtx_UNSPEC (BLKmode,
7954 gen_rtvec (1, reg),
7955 UNSPEC_PUSH_MULT));
7957 tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
7958 gen_rtx_PLUS (SImode, stack_pointer_rtx,
7959 GEN_INT (-(count * 8 + 4))));
7960 RTX_FRAME_RELATED_P (tmp) = 1;
7961 XVECEXP (dwarf, 0, 0) = tmp;
7963 tmp = gen_rtx_SET (VOIDmode,
7964 gen_rtx_MEM (DFmode, stack_pointer_rtx),
7965 reg);
7966 RTX_FRAME_RELATED_P (tmp) = 1;
7967 XVECEXP (dwarf, 0, 1) = tmp;
7969 for (i = 1; i < count; i++)
7971 reg = gen_rtx_REG (DFmode, base_reg);
7972 base_reg += 2;
7973 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
7975 tmp = gen_rtx_SET (VOIDmode,
7976 gen_rtx_MEM (DFmode,
7977 gen_rtx_PLUS (SImode,
7978 stack_pointer_rtx,
7979 GEN_INT (i * 8))),
7980 reg);
7981 RTX_FRAME_RELATED_P (tmp) = 1;
7982 XVECEXP (dwarf, 0, i + 1) = tmp;
7985 par = emit_insn (par);
7986 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
7987 REG_NOTES (par));
7988 RTX_FRAME_RELATED_P (par) = 1;
7990 return count * 8 + 4;
7994 /* Output a 'call' insn. */
7995 const char *
7996 output_call (rtx *operands)
7998 if (arm_arch5)
7999 abort (); /* Patterns should call blx <reg> directly. */
8001 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
8002 if (REGNO (operands[0]) == LR_REGNUM)
8004 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8005 output_asm_insn ("mov%?\t%0, %|lr", operands);
8008 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8010 if (TARGET_INTERWORK || arm_arch4t)
8011 output_asm_insn ("bx%?\t%0", operands);
8012 else
8013 output_asm_insn ("mov%?\t%|pc, %0", operands);
8015 return "";
8018 /* Output a 'call' insn that is a reference in memory. */
8019 const char *
8020 output_call_mem (rtx *operands)
8022 if (TARGET_INTERWORK && !arm_arch5)
8024 output_asm_insn ("ldr%?\t%|ip, %0", operands);
8025 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8026 output_asm_insn ("bx%?\t%|ip", operands);
8028 else if (regno_use_in (LR_REGNUM, operands[0]))
8030 /* LR is used in the memory address. We load the address in the
8031 first instruction. It's safe to use IP as the target of the
8032 load since the call will kill it anyway. */
8033 output_asm_insn ("ldr%?\t%|ip, %0", operands);
8034 if (arm_arch5)
8035 output_asm_insn ("blx%?%|ip", operands);
8036 else
8038 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8039 if (arm_arch4t)
8040 output_asm_insn ("bx%?\t%|ip", operands);
8041 else
8042 output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8045 else
8047 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8048 output_asm_insn ("ldr%?\t%|pc, %0", operands);
8051 return "";
8055 /* Output a move from arm registers to an fpa registers.
8056 OPERANDS[0] is an fpa register.
8057 OPERANDS[1] is the first registers of an arm register pair. */
8058 const char *
8059 output_mov_long_double_fpa_from_arm (rtx *operands)
8061 int arm_reg0 = REGNO (operands[1]);
8062 rtx ops[3];
8064 if (arm_reg0 == IP_REGNUM)
8065 abort ();
8067 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8068 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8069 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8071 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
8072 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8074 return "";
8077 /* Output a move from an fpa register to arm registers.
8078 OPERANDS[0] is the first registers of an arm register pair.
8079 OPERANDS[1] is an fpa register. */
8080 const char *
8081 output_mov_long_double_arm_from_fpa (rtx *operands)
8083 int arm_reg0 = REGNO (operands[0]);
8084 rtx ops[3];
8086 if (arm_reg0 == IP_REGNUM)
8087 abort ();
8089 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8090 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8091 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8093 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8094 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8095 return "";
8098 /* Output a move from arm registers to arm registers of a long double
8099 OPERANDS[0] is the destination.
8100 OPERANDS[1] is the source. */
8101 const char *
8102 output_mov_long_double_arm_from_arm (rtx *operands)
8104 /* We have to be careful here because the two might overlap. */
8105 int dest_start = REGNO (operands[0]);
8106 int src_start = REGNO (operands[1]);
8107 rtx ops[2];
8108 int i;
8110 if (dest_start < src_start)
8112 for (i = 0; i < 3; i++)
8114 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8115 ops[1] = gen_rtx_REG (SImode, src_start + i);
8116 output_asm_insn ("mov%?\t%0, %1", ops);
8119 else
8121 for (i = 2; i >= 0; i--)
8123 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8124 ops[1] = gen_rtx_REG (SImode, src_start + i);
8125 output_asm_insn ("mov%?\t%0, %1", ops);
8129 return "";
8133 /* Output a move from arm registers to an fpa registers.
8134 OPERANDS[0] is an fpa register.
8135 OPERANDS[1] is the first registers of an arm register pair. */
8136 const char *
8137 output_mov_double_fpa_from_arm (rtx *operands)
8139 int arm_reg0 = REGNO (operands[1]);
8140 rtx ops[2];
8142 if (arm_reg0 == IP_REGNUM)
8143 abort ();
8145 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8146 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8147 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8148 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8149 return "";
8152 /* Output a move from an fpa register to arm registers.
8153 OPERANDS[0] is the first registers of an arm register pair.
8154 OPERANDS[1] is an fpa register. */
8155 const char *
8156 output_mov_double_arm_from_fpa (rtx *operands)
8158 int arm_reg0 = REGNO (operands[0]);
8159 rtx ops[2];
8161 if (arm_reg0 == IP_REGNUM)
8162 abort ();
8164 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8165 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8166 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8167 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8168 return "";
8171 /* Output a move between double words.
8172 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8173 or MEM<-REG and all MEMs must be offsettable addresses. */
8174 const char *
8175 output_move_double (rtx *operands)
8177 enum rtx_code code0 = GET_CODE (operands[0]);
8178 enum rtx_code code1 = GET_CODE (operands[1]);
8179 rtx otherops[3];
8181 if (code0 == REG)
8183 int reg0 = REGNO (operands[0]);
8185 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8187 if (code1 == REG)
8189 int reg1 = REGNO (operands[1]);
8190 if (reg1 == IP_REGNUM)
8191 abort ();
8193 /* Ensure the second source is not overwritten. */
8194 if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
8195 output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
8196 else
8197 output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
8199 else if (code1 == CONST_VECTOR)
8201 HOST_WIDE_INT hint = 0;
8203 switch (GET_MODE (operands[1]))
8205 case V2SImode:
8206 otherops[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 1)));
8207 operands[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 0)));
8208 break;
8210 case V4HImode:
8211 if (BYTES_BIG_ENDIAN)
8213 hint = INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8214 hint <<= 16;
8215 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8217 else
8219 hint = INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8220 hint <<= 16;
8221 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8224 otherops[1] = GEN_INT (hint);
8225 hint = 0;
8227 if (BYTES_BIG_ENDIAN)
8229 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8230 hint <<= 16;
8231 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8233 else
8235 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8236 hint <<= 16;
8237 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8240 operands[1] = GEN_INT (hint);
8241 break;
8243 case V8QImode:
8244 if (BYTES_BIG_ENDIAN)
8246 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8247 hint <<= 8;
8248 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8249 hint <<= 8;
8250 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8251 hint <<= 8;
8252 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8254 else
8256 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8257 hint <<= 8;
8258 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8259 hint <<= 8;
8260 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8261 hint <<= 8;
8262 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8265 otherops[1] = GEN_INT (hint);
8266 hint = 0;
8268 if (BYTES_BIG_ENDIAN)
8270 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8271 hint <<= 8;
8272 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8273 hint <<= 8;
8274 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8275 hint <<= 8;
8276 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8278 else
8280 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8281 hint <<= 8;
8282 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8283 hint <<= 8;
8284 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8285 hint <<= 8;
8286 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8289 operands[1] = GEN_INT (hint);
8290 break;
8292 default:
8293 abort ();
8295 output_mov_immediate (operands);
8296 output_mov_immediate (otherops);
8298 else if (code1 == CONST_DOUBLE)
8300 if (GET_MODE (operands[1]) == DFmode)
8302 REAL_VALUE_TYPE r;
8303 long l[2];
8305 REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
8306 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
8307 otherops[1] = GEN_INT (l[1]);
8308 operands[1] = GEN_INT (l[0]);
8310 else if (GET_MODE (operands[1]) != VOIDmode)
8311 abort ();
8312 else if (WORDS_BIG_ENDIAN)
8314 otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8315 operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8317 else
8319 otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8320 operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8323 output_mov_immediate (operands);
8324 output_mov_immediate (otherops);
8326 else if (code1 == CONST_INT)
8328 #if HOST_BITS_PER_WIDE_INT > 32
8329 /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
8330 what the upper word is. */
8331 if (WORDS_BIG_ENDIAN)
8333 otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8334 operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8336 else
8338 otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8339 operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8341 #else
8342 /* Sign extend the intval into the high-order word. */
8343 if (WORDS_BIG_ENDIAN)
8345 otherops[1] = operands[1];
8346 operands[1] = (INTVAL (operands[1]) < 0
8347 ? constm1_rtx : const0_rtx);
8349 else
8350 otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
8351 #endif
8352 output_mov_immediate (otherops);
8353 output_mov_immediate (operands);
8355 else if (code1 == MEM)
8357 switch (GET_CODE (XEXP (operands[1], 0)))
8359 case REG:
8360 output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8361 break;
8363 case PRE_INC:
8364 if (!TARGET_LDRD)
8365 abort (); /* Should never happen now. */
8366 output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8367 break;
8369 case PRE_DEC:
8370 output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8371 break;
8373 case POST_INC:
8374 output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8375 break;
8377 case POST_DEC:
8378 if (!TARGET_LDRD)
8379 abort (); /* Should never happen now. */
8380 output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8381 break;
8383 case PRE_MODIFY:
8384 case POST_MODIFY:
8385 otherops[0] = operands[0];
8386 otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8387 otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8389 if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8391 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8393 /* Registers overlap so split out the increment. */
8394 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8395 output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8397 else
8398 output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8400 else
8402 /* We only allow constant increments, so this is safe. */
8403 output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8405 break;
8407 case LABEL_REF:
8408 case CONST:
8409 output_asm_insn ("adr%?\t%0, %1", operands);
8410 output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8411 break;
8413 default:
8414 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8415 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8417 otherops[0] = operands[0];
8418 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8419 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8421 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8423 if (GET_CODE (otherops[2]) == CONST_INT)
8425 switch ((int) INTVAL (otherops[2]))
8427 case -8:
8428 output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8429 return "";
8430 case -4:
8431 output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8432 return "";
8433 case 4:
8434 output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8435 return "";
8438 if (TARGET_LDRD
8439 && (GET_CODE (otherops[2]) == REG
8440 || (GET_CODE (otherops[2]) == CONST_INT
8441 && INTVAL (otherops[2]) > -256
8442 && INTVAL (otherops[2]) < 256)))
8444 if (reg_overlap_mentioned_p (otherops[0],
8445 otherops[2]))
8447 /* Swap base and index registers over to
8448 avoid a conflict. */
8449 otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8450 otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8453 /* If both registers conflict, it will usually
8454 have been fixed by a splitter. */
8455 if (reg_overlap_mentioned_p (otherops[0],
8456 otherops[2]))
8458 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8459 output_asm_insn ("ldr%?d\t%0, [%1]",
8460 otherops);
8461 return "";
8463 else
8465 output_asm_insn ("ldr%?d\t%0, [%1, %2]",
8466 otherops);
8467 return "";
8470 if (GET_CODE (otherops[2]) == CONST_INT)
8472 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8473 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8474 else
8475 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8477 else
8478 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8480 else
8481 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8483 return "ldm%?ia\t%0, %M0";
8485 else
8487 otherops[1] = adjust_address (operands[1], SImode, 4);
8488 /* Take care of overlapping base/data reg. */
8489 if (reg_mentioned_p (operands[0], operands[1]))
8491 output_asm_insn ("ldr%?\t%0, %1", otherops);
8492 output_asm_insn ("ldr%?\t%0, %1", operands);
8494 else
8496 output_asm_insn ("ldr%?\t%0, %1", operands);
8497 output_asm_insn ("ldr%?\t%0, %1", otherops);
8502 else
8503 abort (); /* Constraints should prevent this. */
8505 else if (code0 == MEM && code1 == REG)
8507 if (REGNO (operands[1]) == IP_REGNUM)
8508 abort ();
8510 switch (GET_CODE (XEXP (operands[0], 0)))
8512 case REG:
8513 output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8514 break;
8516 case PRE_INC:
8517 if (!TARGET_LDRD)
8518 abort (); /* Should never happen now. */
8519 output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8520 break;
8522 case PRE_DEC:
8523 output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8524 break;
8526 case POST_INC:
8527 output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8528 break;
8530 case POST_DEC:
8531 if (!TARGET_LDRD)
8532 abort (); /* Should never happen now. */
8533 output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8534 break;
8536 case PRE_MODIFY:
8537 case POST_MODIFY:
8538 otherops[0] = operands[1];
8539 otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8540 otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8542 if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8543 output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8544 else
8545 output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8546 break;
8548 case PLUS:
8549 otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8550 if (GET_CODE (otherops[2]) == CONST_INT)
8552 switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8554 case -8:
8555 output_asm_insn ("stm%?db\t%m0, %M1", operands);
8556 return "";
8558 case -4:
8559 output_asm_insn ("stm%?da\t%m0, %M1", operands);
8560 return "";
8562 case 4:
8563 output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8564 return "";
8567 if (TARGET_LDRD
8568 && (GET_CODE (otherops[2]) == REG
8569 || (GET_CODE (otherops[2]) == CONST_INT
8570 && INTVAL (otherops[2]) > -256
8571 && INTVAL (otherops[2]) < 256)))
8573 otherops[0] = operands[1];
8574 otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8575 output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8576 return "";
8578 /* Fall through */
8580 default:
8581 otherops[0] = adjust_address (operands[0], SImode, 4);
8582 otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8583 output_asm_insn ("str%?\t%1, %0", operands);
8584 output_asm_insn ("str%?\t%1, %0", otherops);
8587 else
8588 /* Constraints should prevent this. */
8589 abort ();
8591 return "";
8595 /* Output an arbitrary MOV reg, #n.
8596 OPERANDS[0] is a register. OPERANDS[1] is a const_int. */
8597 const char *
8598 output_mov_immediate (rtx *operands)
8600 HOST_WIDE_INT n = INTVAL (operands[1]);
8602 /* Try to use one MOV. */
8603 if (const_ok_for_arm (n))
8604 output_asm_insn ("mov%?\t%0, %1", operands);
8606 /* Try to use one MVN. */
8607 else if (const_ok_for_arm (~n))
8609 operands[1] = GEN_INT (~n);
8610 output_asm_insn ("mvn%?\t%0, %1", operands);
8612 else
8614 int n_ones = 0;
8615 int i;
8617 /* If all else fails, make it out of ORRs or BICs as appropriate. */
8618 for (i = 0; i < 32; i++)
8619 if (n & 1 << i)
8620 n_ones++;
8622 if (n_ones > 16) /* Shorter to use MVN with BIC in this case. */
8623 output_multi_immediate (operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1, ~ n);
8624 else
8625 output_multi_immediate (operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1, n);
8628 return "";
8631 /* Output an ADD r, s, #n where n may be too big for one instruction.
8632 If adding zero to one register, output nothing. */
8633 const char *
8634 output_add_immediate (rtx *operands)
8636 HOST_WIDE_INT n = INTVAL (operands[2]);
8638 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8640 if (n < 0)
8641 output_multi_immediate (operands,
8642 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8643 -n);
8644 else
8645 output_multi_immediate (operands,
8646 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8650 return "";
8653 /* Output a multiple immediate operation.
8654 OPERANDS is the vector of operands referred to in the output patterns.
8655 INSTR1 is the output pattern to use for the first constant.
8656 INSTR2 is the output pattern to use for subsequent constants.
8657 IMMED_OP is the index of the constant slot in OPERANDS.
8658 N is the constant value. */
8659 static const char *
8660 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8661 int immed_op, HOST_WIDE_INT n)
8663 #if HOST_BITS_PER_WIDE_INT > 32
8664 n &= 0xffffffff;
8665 #endif
8667 if (n == 0)
8669 /* Quick and easy output. */
8670 operands[immed_op] = const0_rtx;
8671 output_asm_insn (instr1, operands);
8673 else
8675 int i;
8676 const char * instr = instr1;
8678 /* Note that n is never zero here (which would give no output). */
8679 for (i = 0; i < 32; i += 2)
8681 if (n & (3 << i))
8683 operands[immed_op] = GEN_INT (n & (255 << i));
8684 output_asm_insn (instr, operands);
8685 instr = instr2;
8686 i += 6;
8691 return "";
8694 /* Return the appropriate ARM instruction for the operation code.
8695 The returned result should not be overwritten. OP is the rtx of the
8696 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8697 was shifted. */
8698 const char *
8699 arithmetic_instr (rtx op, int shift_first_arg)
8701 switch (GET_CODE (op))
8703 case PLUS:
8704 return "add";
8706 case MINUS:
8707 return shift_first_arg ? "rsb" : "sub";
8709 case IOR:
8710 return "orr";
8712 case XOR:
8713 return "eor";
8715 case AND:
8716 return "and";
8718 default:
8719 abort ();
8723 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8724 for the operation code. The returned result should not be overwritten.
8725 OP is the rtx code of the shift.
8726 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8727 shift. */
8728 static const char *
8729 shift_op (rtx op, HOST_WIDE_INT *amountp)
8731 const char * mnem;
8732 enum rtx_code code = GET_CODE (op);
8734 if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
8735 *amountp = -1;
8736 else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
8737 *amountp = INTVAL (XEXP (op, 1));
8738 else
8739 abort ();
8741 switch (code)
8743 case ASHIFT:
8744 mnem = "asl";
8745 break;
8747 case ASHIFTRT:
8748 mnem = "asr";
8749 break;
8751 case LSHIFTRT:
8752 mnem = "lsr";
8753 break;
8755 case ROTATERT:
8756 mnem = "ror";
8757 break;
8759 case MULT:
8760 /* We never have to worry about the amount being other than a
8761 power of 2, since this case can never be reloaded from a reg. */
8762 if (*amountp != -1)
8763 *amountp = int_log2 (*amountp);
8764 else
8765 abort ();
8766 return "asl";
8768 default:
8769 abort ();
8772 if (*amountp != -1)
8774 /* This is not 100% correct, but follows from the desire to merge
8775 multiplication by a power of 2 with the recognizer for a
8776 shift. >=32 is not a valid shift for "asl", so we must try and
8777 output a shift that produces the correct arithmetical result.
8778 Using lsr #32 is identical except for the fact that the carry bit
8779 is not set correctly if we set the flags; but we never use the
8780 carry bit from such an operation, so we can ignore that. */
8781 if (code == ROTATERT)
8782 /* Rotate is just modulo 32. */
8783 *amountp &= 31;
8784 else if (*amountp != (*amountp & 31))
8786 if (code == ASHIFT)
8787 mnem = "lsr";
8788 *amountp = 32;
8791 /* Shifts of 0 are no-ops. */
8792 if (*amountp == 0)
8793 return NULL;
8796 return mnem;
8799 /* Obtain the shift from the POWER of two. */
8801 static HOST_WIDE_INT
8802 int_log2 (HOST_WIDE_INT power)
8804 HOST_WIDE_INT shift = 0;
8806 while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8808 if (shift > 31)
8809 abort ();
8810 shift++;
8813 return shift;
8816 /* Output a .ascii pseudo-op, keeping track of lengths. This is because
8817 /bin/as is horribly restrictive. */
8818 #define MAX_ASCII_LEN 51
8820 void
8821 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
8823 int i;
8824 int len_so_far = 0;
8826 fputs ("\t.ascii\t\"", stream);
8828 for (i = 0; i < len; i++)
8830 int c = p[i];
8832 if (len_so_far >= MAX_ASCII_LEN)
8834 fputs ("\"\n\t.ascii\t\"", stream);
8835 len_so_far = 0;
8838 switch (c)
8840 case TARGET_TAB:
8841 fputs ("\\t", stream);
8842 len_so_far += 2;
8843 break;
8845 case TARGET_FF:
8846 fputs ("\\f", stream);
8847 len_so_far += 2;
8848 break;
8850 case TARGET_BS:
8851 fputs ("\\b", stream);
8852 len_so_far += 2;
8853 break;
8855 case TARGET_CR:
8856 fputs ("\\r", stream);
8857 len_so_far += 2;
8858 break;
8860 case TARGET_NEWLINE:
8861 fputs ("\\n", stream);
8862 c = p [i + 1];
8863 if ((c >= ' ' && c <= '~')
8864 || c == TARGET_TAB)
8865 /* This is a good place for a line break. */
8866 len_so_far = MAX_ASCII_LEN;
8867 else
8868 len_so_far += 2;
8869 break;
8871 case '\"':
8872 case '\\':
8873 putc ('\\', stream);
8874 len_so_far++;
8875 /* Drop through. */
8877 default:
8878 if (c >= ' ' && c <= '~')
8880 putc (c, stream);
8881 len_so_far++;
8883 else
8885 fprintf (stream, "\\%03o", c);
8886 len_so_far += 4;
8888 break;
8892 fputs ("\"\n", stream);
8895 /* Compute the register sabe mask for registers 0 through 12
8896 inclusive. This code is used by arm_compute_save_reg_mask. */
8897 static unsigned long
8898 arm_compute_save_reg0_reg12_mask (void)
8900 unsigned long func_type = arm_current_func_type ();
8901 unsigned int save_reg_mask = 0;
8902 unsigned int reg;
8904 if (IS_INTERRUPT (func_type))
8906 unsigned int max_reg;
8907 /* Interrupt functions must not corrupt any registers,
8908 even call clobbered ones. If this is a leaf function
8909 we can just examine the registers used by the RTL, but
8910 otherwise we have to assume that whatever function is
8911 called might clobber anything, and so we have to save
8912 all the call-clobbered registers as well. */
8913 if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8914 /* FIQ handlers have registers r8 - r12 banked, so
8915 we only need to check r0 - r7, Normal ISRs only
8916 bank r14 and r15, so we must check up to r12.
8917 r13 is the stack pointer which is always preserved,
8918 so we do not need to consider it here. */
8919 max_reg = 7;
8920 else
8921 max_reg = 12;
8923 for (reg = 0; reg <= max_reg; reg++)
8924 if (regs_ever_live[reg]
8925 || (! current_function_is_leaf && call_used_regs [reg]))
8926 save_reg_mask |= (1 << reg);
8928 else
8930 /* In the normal case we only need to save those registers
8931 which are call saved and which are used by this function. */
8932 for (reg = 0; reg <= 10; reg++)
8933 if (regs_ever_live[reg] && ! call_used_regs [reg])
8934 save_reg_mask |= (1 << reg);
8936 /* Handle the frame pointer as a special case. */
8937 if (! TARGET_APCS_FRAME
8938 && ! frame_pointer_needed
8939 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8940 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8941 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8943 /* If we aren't loading the PIC register,
8944 don't stack it even though it may be live. */
8945 if (flag_pic
8946 && ! TARGET_SINGLE_PIC_BASE
8947 && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
8948 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8951 return save_reg_mask;
8954 /* Compute a bit mask of which registers need to be
8955 saved on the stack for the current function. */
8957 static unsigned long
8958 arm_compute_save_reg_mask (void)
8960 unsigned int save_reg_mask = 0;
8961 unsigned long func_type = arm_current_func_type ();
8963 if (IS_NAKED (func_type))
8964 /* This should never really happen. */
8965 return 0;
8967 /* If we are creating a stack frame, then we must save the frame pointer,
8968 IP (which will hold the old stack pointer), LR and the PC. */
8969 if (frame_pointer_needed)
8970 save_reg_mask |=
8971 (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8972 | (1 << IP_REGNUM)
8973 | (1 << LR_REGNUM)
8974 | (1 << PC_REGNUM);
8976 /* Volatile functions do not return, so there
8977 is no need to save any other registers. */
8978 if (IS_VOLATILE (func_type))
8979 return save_reg_mask;
8981 save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8983 /* Decide if we need to save the link register.
8984 Interrupt routines have their own banked link register,
8985 so they never need to save it.
8986 Otherwise if we do not use the link register we do not need to save
8987 it. If we are pushing other registers onto the stack however, we
8988 can save an instruction in the epilogue by pushing the link register
8989 now and then popping it back into the PC. This incurs extra memory
8990 accesses though, so we only do it when optimizing for size, and only
8991 if we know that we will not need a fancy return sequence. */
8992 if (regs_ever_live [LR_REGNUM]
8993 || (save_reg_mask
8994 && optimize_size
8995 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL))
8996 save_reg_mask |= 1 << LR_REGNUM;
8998 if (cfun->machine->lr_save_eliminated)
8999 save_reg_mask &= ~ (1 << LR_REGNUM);
9001 if (TARGET_REALLY_IWMMXT
9002 && ((bit_count (save_reg_mask)
9003 + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
9005 unsigned int reg;
9007 /* The total number of registers that are going to be pushed
9008 onto the stack is odd. We need to ensure that the stack
9009 is 64-bit aligned before we start to save iWMMXt registers,
9010 and also before we start to create locals. (A local variable
9011 might be a double or long long which we will load/store using
9012 an iWMMXt instruction). Therefore we need to push another
9013 ARM register, so that the stack will be 64-bit aligned. We
9014 try to avoid using the arg registers (r0 -r3) as they might be
9015 used to pass values in a tail call. */
9016 for (reg = 4; reg <= 12; reg++)
9017 if ((save_reg_mask & (1 << reg)) == 0)
9018 break;
9020 if (reg <= 12)
9021 save_reg_mask |= (1 << reg);
9022 else
9024 cfun->machine->sibcall_blocked = 1;
9025 save_reg_mask |= (1 << 3);
9029 return save_reg_mask;
9033 /* Return the number of bytes required to save VFP registers. */
9034 static int
9035 arm_get_vfp_saved_size (void)
9037 unsigned int regno;
9038 int count;
9039 int saved;
9041 saved = 0;
9042 /* Space for saved VFP registers. */
9043 if (TARGET_HARD_FLOAT && TARGET_VFP)
9045 count = 0;
9046 for (regno = FIRST_VFP_REGNUM;
9047 regno < LAST_VFP_REGNUM;
9048 regno += 2)
9050 if ((!regs_ever_live[regno] || call_used_regs[regno])
9051 && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
9053 if (count > 0)
9055 /* Workaround ARM10 VFPr1 bug. */
9056 if (count == 2 && !arm_arch6)
9057 count++;
9058 saved += count * 8 + 4;
9060 count = 0;
9062 else
9063 count++;
9065 if (count > 0)
9067 if (count == 2 && !arm_arch6)
9068 count++;
9069 saved += count * 8 + 4;
9072 return saved;
9076 /* Generate a function exit sequence. If REALLY_RETURN is false, then do
9077 everything bar the final return instruction. */
9078 const char *
9079 output_return_instruction (rtx operand, int really_return, int reverse)
9081 char conditional[10];
9082 char instr[100];
9083 int reg;
9084 unsigned long live_regs_mask;
9085 unsigned long func_type;
9086 arm_stack_offsets *offsets;
9088 func_type = arm_current_func_type ();
9090 if (IS_NAKED (func_type))
9091 return "";
9093 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9095 /* If this function was declared non-returning, and we have
9096 found a tail call, then we have to trust that the called
9097 function won't return. */
9098 if (really_return)
9100 rtx ops[2];
9102 /* Otherwise, trap an attempted return by aborting. */
9103 ops[0] = operand;
9104 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
9105 : "abort");
9106 assemble_external_libcall (ops[1]);
9107 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9110 return "";
9113 if (current_function_calls_alloca && !really_return)
9114 abort ();
9116 sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9118 return_used_this_function = 1;
9120 live_regs_mask = arm_compute_save_reg_mask ();
9122 if (live_regs_mask)
9124 const char * return_reg;
9126 /* If we do not have any special requirements for function exit
9127 (eg interworking, or ISR) then we can load the return address
9128 directly into the PC. Otherwise we must load it into LR. */
9129 if (really_return
9130 && ! TARGET_INTERWORK)
9131 return_reg = reg_names[PC_REGNUM];
9132 else
9133 return_reg = reg_names[LR_REGNUM];
9135 if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9137 /* There are three possible reasons for the IP register
9138 being saved. 1) a stack frame was created, in which case
9139 IP contains the old stack pointer, or 2) an ISR routine
9140 corrupted it, or 3) it was saved to align the stack on
9141 iWMMXt. In case 1, restore IP into SP, otherwise just
9142 restore IP. */
9143 if (frame_pointer_needed)
9145 live_regs_mask &= ~ (1 << IP_REGNUM);
9146 live_regs_mask |= (1 << SP_REGNUM);
9148 else
9150 if (! IS_INTERRUPT (func_type)
9151 && ! TARGET_REALLY_IWMMXT)
9152 abort ();
9156 /* On some ARM architectures it is faster to use LDR rather than
9157 LDM to load a single register. On other architectures, the
9158 cost is the same. In 26 bit mode, or for exception handlers,
9159 we have to use LDM to load the PC so that the CPSR is also
9160 restored. */
9161 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9163 if (live_regs_mask == (unsigned int)(1 << reg))
9164 break;
9166 if (reg <= LAST_ARM_REGNUM
9167 && (reg != LR_REGNUM
9168 || ! really_return
9169 || ! IS_INTERRUPT (func_type)))
9171 sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
9172 (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
9174 else
9176 char *p;
9177 int first = 1;
9179 /* Generate the load multiple instruction to restore the
9180 registers. Note we can get here, even if
9181 frame_pointer_needed is true, but only if sp already
9182 points to the base of the saved core registers. */
9183 if (live_regs_mask & (1 << SP_REGNUM))
9185 unsigned HOST_WIDE_INT stack_adjust;
9187 offsets = arm_get_frame_offsets ();
9188 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9189 if (stack_adjust != 0 && stack_adjust != 4)
9190 abort ();
9192 if (stack_adjust && arm_arch5)
9193 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9194 else
9196 /* If we can't use ldmib (SA110 bug), then try to pop r3
9197 instead. */
9198 if (stack_adjust)
9199 live_regs_mask |= 1 << 3;
9200 sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9203 else
9204 sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9206 p = instr + strlen (instr);
9208 for (reg = 0; reg <= SP_REGNUM; reg++)
9209 if (live_regs_mask & (1 << reg))
9211 int l = strlen (reg_names[reg]);
9213 if (first)
9214 first = 0;
9215 else
9217 memcpy (p, ", ", 2);
9218 p += 2;
9221 memcpy (p, "%|", 2);
9222 memcpy (p + 2, reg_names[reg], l);
9223 p += l + 2;
9226 if (live_regs_mask & (1 << LR_REGNUM))
9228 sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9229 /* If returning from an interrupt, restore the CPSR. */
9230 if (IS_INTERRUPT (func_type))
9231 strcat (p, "^");
9233 else
9234 strcpy (p, "}");
9237 output_asm_insn (instr, & operand);
9239 /* See if we need to generate an extra instruction to
9240 perform the actual function return. */
9241 if (really_return
9242 && func_type != ARM_FT_INTERWORKED
9243 && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9245 /* The return has already been handled
9246 by loading the LR into the PC. */
9247 really_return = 0;
9251 if (really_return)
9253 switch ((int) ARM_FUNC_TYPE (func_type))
9255 case ARM_FT_ISR:
9256 case ARM_FT_FIQ:
9257 sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9258 break;
9260 case ARM_FT_INTERWORKED:
9261 sprintf (instr, "bx%s\t%%|lr", conditional);
9262 break;
9264 case ARM_FT_EXCEPTION:
9265 sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9266 break;
9268 default:
9269 /* Use bx if it's available. */
9270 if (arm_arch5 || arm_arch4t)
9271 sprintf (instr, "bx%s\t%%|lr", conditional);
9272 else
9273 sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
9274 break;
9277 output_asm_insn (instr, & operand);
9280 return "";
9283 /* Write the function name into the code section, directly preceding
9284 the function prologue.
9286 Code will be output similar to this:
9288 .ascii "arm_poke_function_name", 0
9289 .align
9291 .word 0xff000000 + (t1 - t0)
9292 arm_poke_function_name
9293 mov ip, sp
9294 stmfd sp!, {fp, ip, lr, pc}
9295 sub fp, ip, #4
9297 When performing a stack backtrace, code can inspect the value
9298 of 'pc' stored at 'fp' + 0. If the trace function then looks
9299 at location pc - 12 and the top 8 bits are set, then we know
9300 that there is a function name embedded immediately preceding this
9301 location and has length ((pc[-3]) & 0xff000000).
9303 We assume that pc is declared as a pointer to an unsigned long.
9305 It is of no benefit to output the function name if we are assembling
9306 a leaf function. These function types will not contain a stack
9307 backtrace structure, therefore it is not possible to determine the
9308 function name. */
9309 void
9310 arm_poke_function_name (FILE *stream, const char *name)
9312 unsigned long alignlength;
9313 unsigned long length;
9314 rtx x;
9316 length = strlen (name) + 1;
9317 alignlength = ROUND_UP_WORD (length);
9319 ASM_OUTPUT_ASCII (stream, name, length);
9320 ASM_OUTPUT_ALIGN (stream, 2);
9321 x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9322 assemble_aligned_integer (UNITS_PER_WORD, x);
9325 /* Place some comments into the assembler stream
9326 describing the current function. */
9327 static void
9328 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9330 unsigned long func_type;
9332 if (!TARGET_ARM)
9334 thumb_output_function_prologue (f, frame_size);
9335 return;
9338 /* Sanity check. */
9339 if (arm_ccfsm_state || arm_target_insn)
9340 abort ();
9342 func_type = arm_current_func_type ();
9344 switch ((int) ARM_FUNC_TYPE (func_type))
9346 default:
9347 case ARM_FT_NORMAL:
9348 break;
9349 case ARM_FT_INTERWORKED:
9350 asm_fprintf (f, "\t%@ Function supports interworking.\n");
9351 break;
9352 case ARM_FT_EXCEPTION_HANDLER:
9353 asm_fprintf (f, "\t%@ C++ Exception Handler.\n");
9354 break;
9355 case ARM_FT_ISR:
9356 asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9357 break;
9358 case ARM_FT_FIQ:
9359 asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9360 break;
9361 case ARM_FT_EXCEPTION:
9362 asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9363 break;
9366 if (IS_NAKED (func_type))
9367 asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9369 if (IS_VOLATILE (func_type))
9370 asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9372 if (IS_NESTED (func_type))
9373 asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9375 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9376 current_function_args_size,
9377 current_function_pretend_args_size, frame_size);
9379 asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9380 frame_pointer_needed,
9381 cfun->machine->uses_anonymous_args);
9383 if (cfun->machine->lr_save_eliminated)
9384 asm_fprintf (f, "\t%@ link register save eliminated.\n");
9386 #ifdef AOF_ASSEMBLER
9387 if (flag_pic)
9388 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9389 #endif
9391 return_used_this_function = 0;
9394 const char *
9395 arm_output_epilogue (rtx sibling)
9397 int reg;
9398 unsigned long saved_regs_mask;
9399 unsigned long func_type;
9400 /* Floats_offset is the offset from the "virtual" frame. In an APCS
9401 frame that is $fp + 4 for a non-variadic function. */
9402 int floats_offset = 0;
9403 rtx operands[3];
9404 FILE * f = asm_out_file;
9405 rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
9406 unsigned int lrm_count = 0;
9407 int really_return = (sibling == NULL);
9408 int start_reg;
9409 arm_stack_offsets *offsets;
9411 /* If we have already generated the return instruction
9412 then it is futile to generate anything else. */
9413 if (use_return_insn (FALSE, sibling) && return_used_this_function)
9414 return "";
9416 func_type = arm_current_func_type ();
9418 if (IS_NAKED (func_type))
9419 /* Naked functions don't have epilogues. */
9420 return "";
9422 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9424 rtx op;
9426 /* A volatile function should never return. Call abort. */
9427 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9428 assemble_external_libcall (op);
9429 output_asm_insn ("bl\t%a0", &op);
9431 return "";
9434 if (ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
9435 && ! really_return)
9436 /* If we are throwing an exception, then we really must
9437 be doing a return, so we can't tail-call. */
9438 abort ();
9440 offsets = arm_get_frame_offsets ();
9441 saved_regs_mask = arm_compute_save_reg_mask ();
9443 if (TARGET_IWMMXT)
9444 lrm_count = bit_count (saved_regs_mask);
9446 floats_offset = offsets->saved_args;
9447 /* Compute how far away the floats will be. */
9448 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9449 if (saved_regs_mask & (1 << reg))
9450 floats_offset += 4;
9452 if (frame_pointer_needed)
9454 /* This variable is for the Virtual Frame Pointer, not VFP regs. */
9455 int vfp_offset = offsets->frame;
9457 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9459 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9460 if (regs_ever_live[reg] && !call_used_regs[reg])
9462 floats_offset += 12;
9463 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9464 reg, FP_REGNUM, floats_offset - vfp_offset);
9467 else
9469 start_reg = LAST_FPA_REGNUM;
9471 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9473 if (regs_ever_live[reg] && !call_used_regs[reg])
9475 floats_offset += 12;
9477 /* We can't unstack more than four registers at once. */
9478 if (start_reg - reg == 3)
9480 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9481 reg, FP_REGNUM, floats_offset - vfp_offset);
9482 start_reg = reg - 1;
9485 else
9487 if (reg != start_reg)
9488 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9489 reg + 1, start_reg - reg,
9490 FP_REGNUM, floats_offset - vfp_offset);
9491 start_reg = reg - 1;
9495 /* Just in case the last register checked also needs unstacking. */
9496 if (reg != start_reg)
9497 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9498 reg + 1, start_reg - reg,
9499 FP_REGNUM, floats_offset - vfp_offset);
9502 if (TARGET_HARD_FLOAT && TARGET_VFP)
9504 int saved_size;
9506 /* The fldmx insn does not have base+offset addressing modes,
9507 so we use IP to hold the address. */
9508 saved_size = arm_get_vfp_saved_size ();
9510 if (saved_size > 0)
9512 floats_offset += saved_size;
9513 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9514 FP_REGNUM, floats_offset - vfp_offset);
9516 start_reg = FIRST_VFP_REGNUM;
9517 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9519 if ((!regs_ever_live[reg] || call_used_regs[reg])
9520 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9522 if (start_reg != reg)
9523 arm_output_fldmx (f, IP_REGNUM,
9524 (start_reg - FIRST_VFP_REGNUM) / 2,
9525 (reg - start_reg) / 2);
9526 start_reg = reg + 2;
9529 if (start_reg != reg)
9530 arm_output_fldmx (f, IP_REGNUM,
9531 (start_reg - FIRST_VFP_REGNUM) / 2,
9532 (reg - start_reg) / 2);
9535 if (TARGET_IWMMXT)
9537 /* The frame pointer is guaranteed to be non-double-word aligned.
9538 This is because it is set to (old_stack_pointer - 4) and the
9539 old_stack_pointer was double word aligned. Thus the offset to
9540 the iWMMXt registers to be loaded must also be non-double-word
9541 sized, so that the resultant address *is* double-word aligned.
9542 We can ignore floats_offset since that was already included in
9543 the live_regs_mask. */
9544 lrm_count += (lrm_count % 2 ? 2 : 1);
9546 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9547 if (regs_ever_live[reg] && !call_used_regs[reg])
9549 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9550 reg, FP_REGNUM, lrm_count * 4);
9551 lrm_count += 2;
9555 /* saved_regs_mask should contain the IP, which at the time of stack
9556 frame generation actually contains the old stack pointer. So a
9557 quick way to unwind the stack is just pop the IP register directly
9558 into the stack pointer. */
9559 if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
9560 abort ();
9561 saved_regs_mask &= ~ (1 << IP_REGNUM);
9562 saved_regs_mask |= (1 << SP_REGNUM);
9564 /* There are two registers left in saved_regs_mask - LR and PC. We
9565 only need to restore the LR register (the return address), but to
9566 save time we can load it directly into the PC, unless we need a
9567 special function exit sequence, or we are not really returning. */
9568 if (really_return && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
9569 /* Delete the LR from the register mask, so that the LR on
9570 the stack is loaded into the PC in the register mask. */
9571 saved_regs_mask &= ~ (1 << LR_REGNUM);
9572 else
9573 saved_regs_mask &= ~ (1 << PC_REGNUM);
9575 /* We must use SP as the base register, because SP is one of the
9576 registers being restored. If an interrupt or page fault
9577 happens in the ldm instruction, the SP might or might not
9578 have been restored. That would be bad, as then SP will no
9579 longer indicate the safe area of stack, and we can get stack
9580 corruption. Using SP as the base register means that it will
9581 be reset correctly to the original value, should an interrupt
9582 occur. If the stack pointer already points at the right
9583 place, then omit the subtraction. */
9584 if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9585 || current_function_calls_alloca)
9586 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9587 4 * bit_count (saved_regs_mask));
9588 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9590 if (IS_INTERRUPT (func_type))
9591 /* Interrupt handlers will have pushed the
9592 IP onto the stack, so restore it now. */
9593 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9595 else
9597 /* Restore stack pointer if necessary. */
9598 if (offsets->outgoing_args != offsets->saved_regs)
9600 operands[0] = operands[1] = stack_pointer_rtx;
9601 operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9602 output_add_immediate (operands);
9605 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9607 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9608 if (regs_ever_live[reg] && !call_used_regs[reg])
9609 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9610 reg, SP_REGNUM);
9612 else
9614 start_reg = FIRST_FPA_REGNUM;
9616 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9618 if (regs_ever_live[reg] && !call_used_regs[reg])
9620 if (reg - start_reg == 3)
9622 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9623 start_reg, SP_REGNUM);
9624 start_reg = reg + 1;
9627 else
9629 if (reg != start_reg)
9630 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9631 start_reg, reg - start_reg,
9632 SP_REGNUM);
9634 start_reg = reg + 1;
9638 /* Just in case the last register checked also needs unstacking. */
9639 if (reg != start_reg)
9640 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9641 start_reg, reg - start_reg, SP_REGNUM);
9644 if (TARGET_HARD_FLOAT && TARGET_VFP)
9646 start_reg = FIRST_VFP_REGNUM;
9647 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9649 if ((!regs_ever_live[reg] || call_used_regs[reg])
9650 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9652 if (start_reg != reg)
9653 arm_output_fldmx (f, SP_REGNUM,
9654 (start_reg - FIRST_VFP_REGNUM) / 2,
9655 (reg - start_reg) / 2);
9656 start_reg = reg + 2;
9659 if (start_reg != reg)
9660 arm_output_fldmx (f, SP_REGNUM,
9661 (start_reg - FIRST_VFP_REGNUM) / 2,
9662 (reg - start_reg) / 2);
9664 if (TARGET_IWMMXT)
9665 for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9666 if (regs_ever_live[reg] && !call_used_regs[reg])
9667 asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9669 /* If we can, restore the LR into the PC. */
9670 if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9671 && really_return
9672 && current_function_pretend_args_size == 0
9673 && saved_regs_mask & (1 << LR_REGNUM))
9675 saved_regs_mask &= ~ (1 << LR_REGNUM);
9676 saved_regs_mask |= (1 << PC_REGNUM);
9679 /* Load the registers off the stack. If we only have one register
9680 to load use the LDR instruction - it is faster. */
9681 if (saved_regs_mask == (1 << LR_REGNUM))
9683 /* The exception handler ignores the LR, so we do
9684 not really need to load it off the stack. */
9685 if (eh_ofs)
9686 asm_fprintf (f, "\tadd\t%r, %r, #4\n", SP_REGNUM, SP_REGNUM);
9687 else
9688 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9690 else if (saved_regs_mask)
9692 if (saved_regs_mask & (1 << SP_REGNUM))
9693 /* Note - write back to the stack register is not enabled
9694 (ie "ldmfd sp!..."). We know that the stack pointer is
9695 in the list of registers and if we add writeback the
9696 instruction becomes UNPREDICTABLE. */
9697 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9698 else
9699 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9702 if (current_function_pretend_args_size)
9704 /* Unwind the pre-pushed regs. */
9705 operands[0] = operands[1] = stack_pointer_rtx;
9706 operands[2] = GEN_INT (current_function_pretend_args_size);
9707 output_add_immediate (operands);
9711 /* We may have already restored PC directly from the stack. */
9712 if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
9713 return "";
9715 /* Generate the return instruction. */
9716 switch ((int) ARM_FUNC_TYPE (func_type))
9718 case ARM_FT_EXCEPTION_HANDLER:
9719 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, EXCEPTION_LR_REGNUM);
9720 break;
9722 case ARM_FT_ISR:
9723 case ARM_FT_FIQ:
9724 asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9725 break;
9727 case ARM_FT_EXCEPTION:
9728 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9729 break;
9731 case ARM_FT_INTERWORKED:
9732 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9733 break;
9735 default:
9736 if (arm_arch5 || arm_arch4t)
9737 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9738 else
9739 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9740 break;
9743 return "";
9746 static void
9747 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9748 HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9750 arm_stack_offsets *offsets;
9752 if (TARGET_THUMB)
9754 /* ??? Probably not safe to set this here, since it assumes that a
9755 function will be emitted as assembly immediately after we generate
9756 RTL for it. This does not happen for inline functions. */
9757 return_used_this_function = 0;
9759 else
9761 /* We need to take into account any stack-frame rounding. */
9762 offsets = arm_get_frame_offsets ();
9764 if (use_return_insn (FALSE, NULL)
9765 && return_used_this_function
9766 && offsets->saved_regs != offsets->outgoing_args
9767 && !frame_pointer_needed)
9768 abort ();
9770 /* Reset the ARM-specific per-function variables. */
9771 after_arm_reorg = 0;
9775 /* Generate and emit an insn that we will recognize as a push_multi.
9776 Unfortunately, since this insn does not reflect very well the actual
9777 semantics of the operation, we need to annotate the insn for the benefit
9778 of DWARF2 frame unwind information. */
9779 static rtx
9780 emit_multi_reg_push (int mask)
9782 int num_regs = 0;
9783 int num_dwarf_regs;
9784 int i, j;
9785 rtx par;
9786 rtx dwarf;
9787 int dwarf_par_index;
9788 rtx tmp, reg;
9790 for (i = 0; i <= LAST_ARM_REGNUM; i++)
9791 if (mask & (1 << i))
9792 num_regs++;
9794 if (num_regs == 0 || num_regs > 16)
9795 abort ();
9797 /* We don't record the PC in the dwarf frame information. */
9798 num_dwarf_regs = num_regs;
9799 if (mask & (1 << PC_REGNUM))
9800 num_dwarf_regs--;
9802 /* For the body of the insn we are going to generate an UNSPEC in
9803 parallel with several USEs. This allows the insn to be recognized
9804 by the push_multi pattern in the arm.md file. The insn looks
9805 something like this:
9807 (parallel [
9808 (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9809 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9810 (use (reg:SI 11 fp))
9811 (use (reg:SI 12 ip))
9812 (use (reg:SI 14 lr))
9813 (use (reg:SI 15 pc))
9816 For the frame note however, we try to be more explicit and actually
9817 show each register being stored into the stack frame, plus a (single)
9818 decrement of the stack pointer. We do it this way in order to be
9819 friendly to the stack unwinding code, which only wants to see a single
9820 stack decrement per instruction. The RTL we generate for the note looks
9821 something like this:
9823 (sequence [
9824 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9825 (set (mem:SI (reg:SI sp)) (reg:SI r4))
9826 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9827 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9828 (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9831 This sequence is used both by the code to support stack unwinding for
9832 exceptions handlers and the code to generate dwarf2 frame debugging. */
9834 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9835 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9836 dwarf_par_index = 1;
9838 for (i = 0; i <= LAST_ARM_REGNUM; i++)
9840 if (mask & (1 << i))
9842 reg = gen_rtx_REG (SImode, i);
9844 XVECEXP (par, 0, 0)
9845 = gen_rtx_SET (VOIDmode,
9846 gen_rtx_MEM (BLKmode,
9847 gen_rtx_PRE_DEC (BLKmode,
9848 stack_pointer_rtx)),
9849 gen_rtx_UNSPEC (BLKmode,
9850 gen_rtvec (1, reg),
9851 UNSPEC_PUSH_MULT));
9853 if (i != PC_REGNUM)
9855 tmp = gen_rtx_SET (VOIDmode,
9856 gen_rtx_MEM (SImode, stack_pointer_rtx),
9857 reg);
9858 RTX_FRAME_RELATED_P (tmp) = 1;
9859 XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9860 dwarf_par_index++;
9863 break;
9867 for (j = 1, i++; j < num_regs; i++)
9869 if (mask & (1 << i))
9871 reg = gen_rtx_REG (SImode, i);
9873 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9875 if (i != PC_REGNUM)
9877 tmp = gen_rtx_SET (VOIDmode,
9878 gen_rtx_MEM (SImode,
9879 plus_constant (stack_pointer_rtx,
9880 4 * j)),
9881 reg);
9882 RTX_FRAME_RELATED_P (tmp) = 1;
9883 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9886 j++;
9890 par = emit_insn (par);
9892 tmp = gen_rtx_SET (SImode,
9893 stack_pointer_rtx,
9894 gen_rtx_PLUS (SImode,
9895 stack_pointer_rtx,
9896 GEN_INT (-4 * num_regs)));
9897 RTX_FRAME_RELATED_P (tmp) = 1;
9898 XVECEXP (dwarf, 0, 0) = tmp;
9900 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9901 REG_NOTES (par));
9902 return par;
9905 static rtx
9906 emit_sfm (int base_reg, int count)
9908 rtx par;
9909 rtx dwarf;
9910 rtx tmp, reg;
9911 int i;
9913 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9914 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9916 reg = gen_rtx_REG (XFmode, base_reg++);
9918 XVECEXP (par, 0, 0)
9919 = gen_rtx_SET (VOIDmode,
9920 gen_rtx_MEM (BLKmode,
9921 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9922 gen_rtx_UNSPEC (BLKmode,
9923 gen_rtvec (1, reg),
9924 UNSPEC_PUSH_MULT));
9925 tmp = gen_rtx_SET (VOIDmode,
9926 gen_rtx_MEM (XFmode, stack_pointer_rtx), reg);
9927 RTX_FRAME_RELATED_P (tmp) = 1;
9928 XVECEXP (dwarf, 0, 1) = tmp;
9930 for (i = 1; i < count; i++)
9932 reg = gen_rtx_REG (XFmode, base_reg++);
9933 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9935 tmp = gen_rtx_SET (VOIDmode,
9936 gen_rtx_MEM (XFmode,
9937 plus_constant (stack_pointer_rtx,
9938 i * 12)),
9939 reg);
9940 RTX_FRAME_RELATED_P (tmp) = 1;
9941 XVECEXP (dwarf, 0, i + 1) = tmp;
9944 tmp = gen_rtx_SET (VOIDmode,
9945 stack_pointer_rtx,
9946 gen_rtx_PLUS (SImode,
9947 stack_pointer_rtx,
9948 GEN_INT (-12 * count)));
9949 RTX_FRAME_RELATED_P (tmp) = 1;
9950 XVECEXP (dwarf, 0, 0) = tmp;
9952 par = emit_insn (par);
9953 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9954 REG_NOTES (par));
9955 return par;
9959 /* Return true if the current function needs to save/restore LR. */
9961 static bool
9962 thumb_force_lr_save (void)
9964 return !cfun->machine->lr_save_eliminated
9965 && (!leaf_function_p ()
9966 || thumb_far_jump_used_p ()
9967 || regs_ever_live [LR_REGNUM]);
9971 /* Compute the distance from register FROM to register TO.
9972 These can be the arg pointer (26), the soft frame pointer (25),
9973 the stack pointer (13) or the hard frame pointer (11).
9974 Typical stack layout looks like this:
9976 old stack pointer -> | |
9977 ----
9978 | | \
9979 | | saved arguments for
9980 | | vararg functions
9981 | | /
9983 hard FP & arg pointer -> | | \
9984 | | stack
9985 | | frame
9986 | | /
9988 | | \
9989 | | call saved
9990 | | registers
9991 soft frame pointer -> | | /
9993 | | \
9994 | | local
9995 | | variables
9996 | | /
9998 | | \
9999 | | outgoing
10000 | | arguments
10001 current stack pointer -> | | /
10004 For a given function some or all of these stack components
10005 may not be needed, giving rise to the possibility of
10006 eliminating some of the registers.
10008 The values returned by this function must reflect the behavior
10009 of arm_expand_prologue() and arm_compute_save_reg_mask().
10011 The sign of the number returned reflects the direction of stack
10012 growth, so the values are positive for all eliminations except
10013 from the soft frame pointer to the hard frame pointer.
10015 SFP may point just inside the local variables block to ensure correct
10016 alignment. */
10019 /* Calculate stack offsets. These are used to calculate register elimination
10020 offsets and in prologue/epilogue code. */
10022 static arm_stack_offsets *
10023 arm_get_frame_offsets (void)
10025 struct arm_stack_offsets *offsets;
10026 unsigned long func_type;
10027 int leaf;
10028 int saved;
10029 HOST_WIDE_INT frame_size;
10031 offsets = &cfun->machine->stack_offsets;
10033 /* We need to know if we are a leaf function. Unfortunately, it
10034 is possible to be called after start_sequence has been called,
10035 which causes get_insns to return the insns for the sequence,
10036 not the function, which will cause leaf_function_p to return
10037 the incorrect result.
10039 to know about leaf functions once reload has completed, and the
10040 frame size cannot be changed after that time, so we can safely
10041 use the cached value. */
10043 if (reload_completed)
10044 return offsets;
10046 /* Initially this is the size of the local variables. It will translated
10047 into an offset once we have determined the size of preceding data. */
10048 frame_size = ROUND_UP_WORD (get_frame_size ());
10050 leaf = leaf_function_p ();
10052 /* Space for variadic functions. */
10053 offsets->saved_args = current_function_pretend_args_size;
10055 offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
10057 if (TARGET_ARM)
10059 unsigned int regno;
10061 saved = bit_count (arm_compute_save_reg_mask ()) * 4;
10063 /* We know that SP will be doubleword aligned on entry, and we must
10064 preserve that condition at any subroutine call. We also require the
10065 soft frame pointer to be doubleword aligned. */
10067 if (TARGET_REALLY_IWMMXT)
10069 /* Check for the call-saved iWMMXt registers. */
10070 for (regno = FIRST_IWMMXT_REGNUM;
10071 regno <= LAST_IWMMXT_REGNUM;
10072 regno++)
10073 if (regs_ever_live [regno] && ! call_used_regs [regno])
10074 saved += 8;
10077 func_type = arm_current_func_type ();
10078 if (! IS_VOLATILE (func_type))
10080 /* Space for saved FPA registers. */
10081 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
10082 if (regs_ever_live[regno] && ! call_used_regs[regno])
10083 saved += 12;
10085 /* Space for saved VFP registers. */
10086 if (TARGET_HARD_FLOAT && TARGET_VFP)
10087 saved += arm_get_vfp_saved_size ();
10090 else /* TARGET_THUMB */
10092 int reg;
10093 int count_regs;
10095 saved = 0;
10096 count_regs = 0;
10097 for (reg = 8; reg < 13; reg ++)
10098 if (THUMB_REG_PUSHED_P (reg))
10099 count_regs ++;
10100 if (count_regs)
10101 saved += 4 * count_regs;
10102 count_regs = 0;
10103 for (reg = 0; reg <= LAST_LO_REGNUM; reg ++)
10104 if (THUMB_REG_PUSHED_P (reg))
10105 count_regs ++;
10106 if (count_regs || thumb_force_lr_save ())
10107 saved += 4 * (count_regs + 1);
10108 if (TARGET_BACKTRACE)
10110 if ((count_regs & 0xFF) == 0 && (regs_ever_live[3] != 0))
10111 saved += 20;
10112 else
10113 saved += 16;
10117 /* Saved registers include the stack frame. */
10118 offsets->saved_regs = offsets->saved_args + saved;
10119 offsets->soft_frame = offsets->saved_regs;
10120 /* A leaf function does not need any stack alignment if it has nothing
10121 on the stack. */
10122 if (leaf && frame_size == 0)
10124 offsets->outgoing_args = offsets->soft_frame;
10125 return offsets;
10128 /* Ensure SFP has the correct alignment. */
10129 if (ARM_DOUBLEWORD_ALIGN
10130 && (offsets->soft_frame & 7))
10131 offsets->soft_frame += 4;
10133 offsets->outgoing_args = offsets->soft_frame + frame_size
10134 + current_function_outgoing_args_size;
10136 if (ARM_DOUBLEWORD_ALIGN)
10138 /* Ensure SP remains doubleword aligned. */
10139 if (offsets->outgoing_args & 7)
10140 offsets->outgoing_args += 4;
10141 if (offsets->outgoing_args & 7)
10142 abort ();
10145 return offsets;
10149 /* Calculate the relative offsets for the different stack pointers. Positive
10150 offsets are in the direction of stack growth. */
10152 unsigned int
10153 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10155 arm_stack_offsets *offsets;
10157 offsets = arm_get_frame_offsets ();
10159 /* OK, now we have enough information to compute the distances.
10160 There must be an entry in these switch tables for each pair
10161 of registers in ELIMINABLE_REGS, even if some of the entries
10162 seem to be redundant or useless. */
10163 switch (from)
10165 case ARG_POINTER_REGNUM:
10166 switch (to)
10168 case THUMB_HARD_FRAME_POINTER_REGNUM:
10169 return 0;
10171 case FRAME_POINTER_REGNUM:
10172 /* This is the reverse of the soft frame pointer
10173 to hard frame pointer elimination below. */
10174 return offsets->soft_frame - offsets->saved_args;
10176 case ARM_HARD_FRAME_POINTER_REGNUM:
10177 /* If there is no stack frame then the hard
10178 frame pointer and the arg pointer coincide. */
10179 if (offsets->frame == offsets->saved_regs)
10180 return 0;
10181 /* FIXME: Not sure about this. Maybe we should always return 0 ? */
10182 return (frame_pointer_needed
10183 && cfun->static_chain_decl != NULL
10184 && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
10186 case STACK_POINTER_REGNUM:
10187 /* If nothing has been pushed on the stack at all
10188 then this will return -4. This *is* correct! */
10189 return offsets->outgoing_args - (offsets->saved_args + 4);
10191 default:
10192 abort ();
10194 break;
10196 case FRAME_POINTER_REGNUM:
10197 switch (to)
10199 case THUMB_HARD_FRAME_POINTER_REGNUM:
10200 return 0;
10202 case ARM_HARD_FRAME_POINTER_REGNUM:
10203 /* The hard frame pointer points to the top entry in the
10204 stack frame. The soft frame pointer to the bottom entry
10205 in the stack frame. If there is no stack frame at all,
10206 then they are identical. */
10208 return offsets->frame - offsets->soft_frame;
10210 case STACK_POINTER_REGNUM:
10211 return offsets->outgoing_args - offsets->soft_frame;
10213 default:
10214 abort ();
10216 break;
10218 default:
10219 /* You cannot eliminate from the stack pointer.
10220 In theory you could eliminate from the hard frame
10221 pointer to the stack pointer, but this will never
10222 happen, since if a stack frame is not needed the
10223 hard frame pointer will never be used. */
10224 abort ();
10229 /* Generate the prologue instructions for entry into an ARM function. */
10230 void
10231 arm_expand_prologue (void)
10233 int reg;
10234 rtx amount;
10235 rtx insn;
10236 rtx ip_rtx;
10237 unsigned long live_regs_mask;
10238 unsigned long func_type;
10239 int fp_offset = 0;
10240 int saved_pretend_args = 0;
10241 int saved_regs = 0;
10242 unsigned int args_to_push;
10243 arm_stack_offsets *offsets;
10245 func_type = arm_current_func_type ();
10247 /* Naked functions don't have prologues. */
10248 if (IS_NAKED (func_type))
10249 return;
10251 /* Make a copy of c_f_p_a_s as we may need to modify it locally. */
10252 args_to_push = current_function_pretend_args_size;
10254 /* Compute which register we will have to save onto the stack. */
10255 live_regs_mask = arm_compute_save_reg_mask ();
10257 ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10259 if (frame_pointer_needed)
10261 if (IS_INTERRUPT (func_type))
10263 /* Interrupt functions must not corrupt any registers.
10264 Creating a frame pointer however, corrupts the IP
10265 register, so we must push it first. */
10266 insn = emit_multi_reg_push (1 << IP_REGNUM);
10268 /* Do not set RTX_FRAME_RELATED_P on this insn.
10269 The dwarf stack unwinding code only wants to see one
10270 stack decrement per function, and this is not it. If
10271 this instruction is labeled as being part of the frame
10272 creation sequence then dwarf2out_frame_debug_expr will
10273 abort when it encounters the assignment of IP to FP
10274 later on, since the use of SP here establishes SP as
10275 the CFA register and not IP.
10277 Anyway this instruction is not really part of the stack
10278 frame creation although it is part of the prologue. */
10280 else if (IS_NESTED (func_type))
10282 /* The Static chain register is the same as the IP register
10283 used as a scratch register during stack frame creation.
10284 To get around this need to find somewhere to store IP
10285 whilst the frame is being created. We try the following
10286 places in order:
10288 1. The last argument register.
10289 2. A slot on the stack above the frame. (This only
10290 works if the function is not a varargs function).
10291 3. Register r3, after pushing the argument registers
10292 onto the stack.
10294 Note - we only need to tell the dwarf2 backend about the SP
10295 adjustment in the second variant; the static chain register
10296 doesn't need to be unwound, as it doesn't contain a value
10297 inherited from the caller. */
10299 if (regs_ever_live[3] == 0)
10301 insn = gen_rtx_REG (SImode, 3);
10302 insn = gen_rtx_SET (SImode, insn, ip_rtx);
10303 insn = emit_insn (insn);
10305 else if (args_to_push == 0)
10307 rtx dwarf;
10308 insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10309 insn = gen_rtx_MEM (SImode, insn);
10310 insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10311 insn = emit_insn (insn);
10313 fp_offset = 4;
10315 /* Just tell the dwarf backend that we adjusted SP. */
10316 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10317 gen_rtx_PLUS (SImode, stack_pointer_rtx,
10318 GEN_INT (-fp_offset)));
10319 RTX_FRAME_RELATED_P (insn) = 1;
10320 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10321 dwarf, REG_NOTES (insn));
10323 else
10325 /* Store the args on the stack. */
10326 if (cfun->machine->uses_anonymous_args)
10327 insn = emit_multi_reg_push
10328 ((0xf0 >> (args_to_push / 4)) & 0xf);
10329 else
10330 insn = emit_insn
10331 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10332 GEN_INT (- args_to_push)));
10334 RTX_FRAME_RELATED_P (insn) = 1;
10336 saved_pretend_args = 1;
10337 fp_offset = args_to_push;
10338 args_to_push = 0;
10340 /* Now reuse r3 to preserve IP. */
10341 insn = gen_rtx_REG (SImode, 3);
10342 insn = gen_rtx_SET (SImode, insn, ip_rtx);
10343 (void) emit_insn (insn);
10347 if (fp_offset)
10349 insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10350 insn = gen_rtx_SET (SImode, ip_rtx, insn);
10352 else
10353 insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10355 insn = emit_insn (insn);
10356 RTX_FRAME_RELATED_P (insn) = 1;
10359 if (args_to_push)
10361 /* Push the argument registers, or reserve space for them. */
10362 if (cfun->machine->uses_anonymous_args)
10363 insn = emit_multi_reg_push
10364 ((0xf0 >> (args_to_push / 4)) & 0xf);
10365 else
10366 insn = emit_insn
10367 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10368 GEN_INT (- args_to_push)));
10369 RTX_FRAME_RELATED_P (insn) = 1;
10372 /* If this is an interrupt service routine, and the link register
10373 is going to be pushed, and we are not creating a stack frame,
10374 (which would involve an extra push of IP and a pop in the epilogue)
10375 subtracting four from LR now will mean that the function return
10376 can be done with a single instruction. */
10377 if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10378 && (live_regs_mask & (1 << LR_REGNUM)) != 0
10379 && ! frame_pointer_needed)
10380 emit_insn (gen_rtx_SET (SImode,
10381 gen_rtx_REG (SImode, LR_REGNUM),
10382 gen_rtx_PLUS (SImode,
10383 gen_rtx_REG (SImode, LR_REGNUM),
10384 GEN_INT (-4))));
10386 if (live_regs_mask)
10388 insn = emit_multi_reg_push (live_regs_mask);
10389 saved_regs += bit_count (live_regs_mask) * 4;
10390 RTX_FRAME_RELATED_P (insn) = 1;
10393 if (TARGET_IWMMXT)
10394 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10395 if (regs_ever_live[reg] && ! call_used_regs [reg])
10397 insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10398 insn = gen_rtx_MEM (V2SImode, insn);
10399 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10400 gen_rtx_REG (V2SImode, reg)));
10401 RTX_FRAME_RELATED_P (insn) = 1;
10402 saved_regs += 8;
10405 if (! IS_VOLATILE (func_type))
10407 int start_reg;
10409 /* Save any floating point call-saved registers used by this
10410 function. */
10411 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10413 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10414 if (regs_ever_live[reg] && !call_used_regs[reg])
10416 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10417 insn = gen_rtx_MEM (XFmode, insn);
10418 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10419 gen_rtx_REG (XFmode, reg)));
10420 RTX_FRAME_RELATED_P (insn) = 1;
10421 saved_regs += 12;
10424 else
10426 start_reg = LAST_FPA_REGNUM;
10428 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10430 if (regs_ever_live[reg] && !call_used_regs[reg])
10432 if (start_reg - reg == 3)
10434 insn = emit_sfm (reg, 4);
10435 RTX_FRAME_RELATED_P (insn) = 1;
10436 saved_regs += 48;
10437 start_reg = reg - 1;
10440 else
10442 if (start_reg != reg)
10444 insn = emit_sfm (reg + 1, start_reg - reg);
10445 RTX_FRAME_RELATED_P (insn) = 1;
10446 saved_regs += (start_reg - reg) * 12;
10448 start_reg = reg - 1;
10452 if (start_reg != reg)
10454 insn = emit_sfm (reg + 1, start_reg - reg);
10455 saved_regs += (start_reg - reg) * 12;
10456 RTX_FRAME_RELATED_P (insn) = 1;
10459 if (TARGET_HARD_FLOAT && TARGET_VFP)
10461 start_reg = FIRST_VFP_REGNUM;
10463 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10465 if ((!regs_ever_live[reg] || call_used_regs[reg])
10466 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10468 if (start_reg != reg)
10469 saved_regs += vfp_emit_fstmx (start_reg,
10470 (reg - start_reg) / 2);
10471 start_reg = reg + 2;
10474 if (start_reg != reg)
10475 saved_regs += vfp_emit_fstmx (start_reg,
10476 (reg - start_reg) / 2);
10480 if (frame_pointer_needed)
10482 /* Create the new frame pointer. */
10483 insn = GEN_INT (-(4 + args_to_push + fp_offset));
10484 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10485 RTX_FRAME_RELATED_P (insn) = 1;
10487 if (IS_NESTED (func_type))
10489 /* Recover the static chain register. */
10490 if (regs_ever_live [3] == 0
10491 || saved_pretend_args)
10492 insn = gen_rtx_REG (SImode, 3);
10493 else /* if (current_function_pretend_args_size == 0) */
10495 insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10496 GEN_INT (4));
10497 insn = gen_rtx_MEM (SImode, insn);
10500 emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10501 /* Add a USE to stop propagate_one_insn() from barfing. */
10502 emit_insn (gen_prologue_use (ip_rtx));
10506 offsets = arm_get_frame_offsets ();
10507 if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10509 /* This add can produce multiple insns for a large constant, so we
10510 need to get tricky. */
10511 rtx last = get_last_insn ();
10513 amount = GEN_INT (offsets->saved_args + saved_regs
10514 - offsets->outgoing_args);
10516 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10517 amount));
10520 last = last ? NEXT_INSN (last) : get_insns ();
10521 RTX_FRAME_RELATED_P (last) = 1;
10523 while (last != insn);
10525 /* If the frame pointer is needed, emit a special barrier that
10526 will prevent the scheduler from moving stores to the frame
10527 before the stack adjustment. */
10528 if (frame_pointer_needed)
10529 insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10530 hard_frame_pointer_rtx));
10533 /* If we are profiling, make sure no instructions are scheduled before
10534 the call to mcount. Similarly if the user has requested no
10535 scheduling in the prolog. */
10536 if (current_function_profile || TARGET_NO_SCHED_PRO)
10537 emit_insn (gen_blockage ());
10539 /* If the link register is being kept alive, with the return address in it,
10540 then make sure that it does not get reused by the ce2 pass. */
10541 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10543 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10544 cfun->machine->lr_save_eliminated = 1;
10548 /* If CODE is 'd', then the X is a condition operand and the instruction
10549 should only be executed if the condition is true.
10550 if CODE is 'D', then the X is a condition operand and the instruction
10551 should only be executed if the condition is false: however, if the mode
10552 of the comparison is CCFPEmode, then always execute the instruction -- we
10553 do this because in these circumstances !GE does not necessarily imply LT;
10554 in these cases the instruction pattern will take care to make sure that
10555 an instruction containing %d will follow, thereby undoing the effects of
10556 doing this instruction unconditionally.
10557 If CODE is 'N' then X is a floating point operand that must be negated
10558 before output.
10559 If CODE is 'B' then output a bitwise inverted value of X (a const int).
10560 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
10561 void
10562 arm_print_operand (FILE *stream, rtx x, int code)
10564 switch (code)
10566 case '@':
10567 fputs (ASM_COMMENT_START, stream);
10568 return;
10570 case '_':
10571 fputs (user_label_prefix, stream);
10572 return;
10574 case '|':
10575 fputs (REGISTER_PREFIX, stream);
10576 return;
10578 case '?':
10579 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10581 if (TARGET_THUMB || current_insn_predicate != NULL)
10582 abort ();
10584 fputs (arm_condition_codes[arm_current_cc], stream);
10586 else if (current_insn_predicate)
10588 enum arm_cond_code code;
10590 if (TARGET_THUMB)
10591 abort ();
10593 code = get_arm_condition_code (current_insn_predicate);
10594 fputs (arm_condition_codes[code], stream);
10596 return;
10598 case 'N':
10600 REAL_VALUE_TYPE r;
10601 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10602 r = REAL_VALUE_NEGATE (r);
10603 fprintf (stream, "%s", fp_const_from_val (&r));
10605 return;
10607 case 'B':
10608 if (GET_CODE (x) == CONST_INT)
10610 HOST_WIDE_INT val;
10611 val = ARM_SIGN_EXTEND (~INTVAL (x));
10612 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10614 else
10616 putc ('~', stream);
10617 output_addr_const (stream, x);
10619 return;
10621 case 'i':
10622 fprintf (stream, "%s", arithmetic_instr (x, 1));
10623 return;
10625 /* Truncate Cirrus shift counts. */
10626 case 's':
10627 if (GET_CODE (x) == CONST_INT)
10629 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10630 return;
10632 arm_print_operand (stream, x, 0);
10633 return;
10635 case 'I':
10636 fprintf (stream, "%s", arithmetic_instr (x, 0));
10637 return;
10639 case 'S':
10641 HOST_WIDE_INT val;
10642 const char * shift = shift_op (x, &val);
10644 if (shift)
10646 fprintf (stream, ", %s ", shift_op (x, &val));
10647 if (val == -1)
10648 arm_print_operand (stream, XEXP (x, 1), 0);
10649 else
10650 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10653 return;
10655 /* An explanation of the 'Q', 'R' and 'H' register operands:
10657 In a pair of registers containing a DI or DF value the 'Q'
10658 operand returns the register number of the register containing
10659 the least significant part of the value. The 'R' operand returns
10660 the register number of the register containing the most
10661 significant part of the value.
10663 The 'H' operand returns the higher of the two register numbers.
10664 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10665 same as the 'Q' operand, since the most significant part of the
10666 value is held in the lower number register. The reverse is true
10667 on systems where WORDS_BIG_ENDIAN is false.
10669 The purpose of these operands is to distinguish between cases
10670 where the endian-ness of the values is important (for example
10671 when they are added together), and cases where the endian-ness
10672 is irrelevant, but the order of register operations is important.
10673 For example when loading a value from memory into a register
10674 pair, the endian-ness does not matter. Provided that the value
10675 from the lower memory address is put into the lower numbered
10676 register, and the value from the higher address is put into the
10677 higher numbered register, the load will work regardless of whether
10678 the value being loaded is big-wordian or little-wordian. The
10679 order of the two register loads can matter however, if the address
10680 of the memory location is actually held in one of the registers
10681 being overwritten by the load. */
10682 case 'Q':
10683 if (REGNO (x) > LAST_ARM_REGNUM)
10684 abort ();
10685 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10686 return;
10688 case 'R':
10689 if (REGNO (x) > LAST_ARM_REGNUM)
10690 abort ();
10691 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10692 return;
10694 case 'H':
10695 if (REGNO (x) > LAST_ARM_REGNUM)
10696 abort ();
10697 asm_fprintf (stream, "%r", REGNO (x) + 1);
10698 return;
10700 case 'm':
10701 asm_fprintf (stream, "%r",
10702 GET_CODE (XEXP (x, 0)) == REG
10703 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10704 return;
10706 case 'M':
10707 asm_fprintf (stream, "{%r-%r}",
10708 REGNO (x),
10709 REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10710 return;
10712 case 'd':
10713 /* CONST_TRUE_RTX means always -- that's the default. */
10714 if (x == const_true_rtx)
10715 return;
10717 fputs (arm_condition_codes[get_arm_condition_code (x)],
10718 stream);
10719 return;
10721 case 'D':
10722 /* CONST_TRUE_RTX means not always -- ie never. We shouldn't ever
10723 want to do that. */
10724 if (x == const_true_rtx)
10725 abort ();
10727 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10728 (get_arm_condition_code (x))],
10729 stream);
10730 return;
10732 /* Cirrus registers can be accessed in a variety of ways:
10733 single floating point (f)
10734 double floating point (d)
10735 32bit integer (fx)
10736 64bit integer (dx). */
10737 case 'W': /* Cirrus register in F mode. */
10738 case 'X': /* Cirrus register in D mode. */
10739 case 'Y': /* Cirrus register in FX mode. */
10740 case 'Z': /* Cirrus register in DX mode. */
10741 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10742 abort ();
10744 fprintf (stream, "mv%s%s",
10745 code == 'W' ? "f"
10746 : code == 'X' ? "d"
10747 : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10749 return;
10751 /* Print cirrus register in the mode specified by the register's mode. */
10752 case 'V':
10754 int mode = GET_MODE (x);
10756 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10757 abort ();
10759 fprintf (stream, "mv%s%s",
10760 mode == DFmode ? "d"
10761 : mode == SImode ? "fx"
10762 : mode == DImode ? "dx"
10763 : "f", reg_names[REGNO (x)] + 2);
10765 return;
10768 case 'U':
10769 if (GET_CODE (x) != REG
10770 || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10771 || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10772 /* Bad value for wCG register number. */
10773 abort ();
10774 else
10775 fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10776 return;
10778 /* Print an iWMMXt control register name. */
10779 case 'w':
10780 if (GET_CODE (x) != CONST_INT
10781 || INTVAL (x) < 0
10782 || INTVAL (x) >= 16)
10783 /* Bad value for wC register number. */
10784 abort ();
10785 else
10787 static const char * wc_reg_names [16] =
10789 "wCID", "wCon", "wCSSF", "wCASF",
10790 "wC4", "wC5", "wC6", "wC7",
10791 "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10792 "wC12", "wC13", "wC14", "wC15"
10795 fprintf (stream, wc_reg_names [INTVAL (x)]);
10797 return;
10799 /* Print a VFP double precision register name. */
10800 case 'P':
10802 int mode = GET_MODE (x);
10803 int num;
10805 if (mode != DImode && mode != DFmode)
10806 abort ();
10808 if (GET_CODE (x) != REG
10809 || !IS_VFP_REGNUM (REGNO (x)))
10810 abort ();
10812 num = REGNO(x) - FIRST_VFP_REGNUM;
10813 if (num & 1)
10814 abort ();
10816 fprintf (stream, "d%d", num >> 1);
10818 return;
10820 default:
10821 if (x == 0)
10822 abort ();
10824 if (GET_CODE (x) == REG)
10825 asm_fprintf (stream, "%r", REGNO (x));
10826 else if (GET_CODE (x) == MEM)
10828 output_memory_reference_mode = GET_MODE (x);
10829 output_address (XEXP (x, 0));
10831 else if (GET_CODE (x) == CONST_DOUBLE)
10832 fprintf (stream, "#%s", fp_immediate_constant (x));
10833 else if (GET_CODE (x) == NEG)
10834 abort (); /* This should never happen now. */
10835 else
10837 fputc ('#', stream);
10838 output_addr_const (stream, x);
10843 #ifndef AOF_ASSEMBLER
10844 /* Target hook for assembling integer objects. The ARM version needs to
10845 handle word-sized values specially. */
10846 static bool
10847 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10849 if (size == UNITS_PER_WORD && aligned_p)
10851 fputs ("\t.word\t", asm_out_file);
10852 output_addr_const (asm_out_file, x);
10854 /* Mark symbols as position independent. We only do this in the
10855 .text segment, not in the .data segment. */
10856 if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10857 (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10859 if (GET_CODE (x) == SYMBOL_REF
10860 && (CONSTANT_POOL_ADDRESS_P (x)
10861 || SYMBOL_REF_LOCAL_P (x)))
10862 fputs ("(GOTOFF)", asm_out_file);
10863 else if (GET_CODE (x) == LABEL_REF)
10864 fputs ("(GOTOFF)", asm_out_file);
10865 else
10866 fputs ("(GOT)", asm_out_file);
10868 fputc ('\n', asm_out_file);
10869 return true;
10872 if (VECTOR_MODE_SUPPORTED_P (GET_MODE (x)))
10874 int i, units;
10876 if (GET_CODE (x) != CONST_VECTOR)
10877 abort ();
10879 units = CONST_VECTOR_NUNITS (x);
10881 switch (GET_MODE (x))
10883 case V2SImode: size = 4; break;
10884 case V4HImode: size = 2; break;
10885 case V8QImode: size = 1; break;
10886 default:
10887 abort ();
10890 for (i = 0; i < units; i++)
10892 rtx elt;
10894 elt = CONST_VECTOR_ELT (x, i);
10895 assemble_integer
10896 (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10899 return true;
10902 return default_assemble_integer (x, size, aligned_p);
10904 #endif
10906 /* A finite state machine takes care of noticing whether or not instructions
10907 can be conditionally executed, and thus decrease execution time and code
10908 size by deleting branch instructions. The fsm is controlled by
10909 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
10911 /* The state of the fsm controlling condition codes are:
10912 0: normal, do nothing special
10913 1: make ASM_OUTPUT_OPCODE not output this instruction
10914 2: make ASM_OUTPUT_OPCODE not output this instruction
10915 3: make instructions conditional
10916 4: make instructions conditional
10918 State transitions (state->state by whom under condition):
10919 0 -> 1 final_prescan_insn if the `target' is a label
10920 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10921 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10922 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10923 3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10924 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10925 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10926 (the target insn is arm_target_insn).
10928 If the jump clobbers the conditions then we use states 2 and 4.
10930 A similar thing can be done with conditional return insns.
10932 XXX In case the `target' is an unconditional branch, this conditionalising
10933 of the instructions always reduces code size, but not always execution
10934 time. But then, I want to reduce the code size to somewhere near what
10935 /bin/cc produces. */
10937 /* Returns the index of the ARM condition code string in
10938 `arm_condition_codes'. COMPARISON should be an rtx like
10939 `(eq (...) (...))'. */
10940 static enum arm_cond_code
10941 get_arm_condition_code (rtx comparison)
10943 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
10944 int code;
10945 enum rtx_code comp_code = GET_CODE (comparison);
10947 if (GET_MODE_CLASS (mode) != MODE_CC)
10948 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
10949 XEXP (comparison, 1));
10951 switch (mode)
10953 case CC_DNEmode: code = ARM_NE; goto dominance;
10954 case CC_DEQmode: code = ARM_EQ; goto dominance;
10955 case CC_DGEmode: code = ARM_GE; goto dominance;
10956 case CC_DGTmode: code = ARM_GT; goto dominance;
10957 case CC_DLEmode: code = ARM_LE; goto dominance;
10958 case CC_DLTmode: code = ARM_LT; goto dominance;
10959 case CC_DGEUmode: code = ARM_CS; goto dominance;
10960 case CC_DGTUmode: code = ARM_HI; goto dominance;
10961 case CC_DLEUmode: code = ARM_LS; goto dominance;
10962 case CC_DLTUmode: code = ARM_CC;
10964 dominance:
10965 if (comp_code != EQ && comp_code != NE)
10966 abort ();
10968 if (comp_code == EQ)
10969 return ARM_INVERSE_CONDITION_CODE (code);
10970 return code;
10972 case CC_NOOVmode:
10973 switch (comp_code)
10975 case NE: return ARM_NE;
10976 case EQ: return ARM_EQ;
10977 case GE: return ARM_PL;
10978 case LT: return ARM_MI;
10979 default: abort ();
10982 case CC_Zmode:
10983 switch (comp_code)
10985 case NE: return ARM_NE;
10986 case EQ: return ARM_EQ;
10987 default: abort ();
10990 case CC_Nmode:
10991 switch (comp_code)
10993 case NE: return ARM_MI;
10994 case EQ: return ARM_PL;
10995 default: abort ();
10998 case CCFPEmode:
10999 case CCFPmode:
11000 /* These encodings assume that AC=1 in the FPA system control
11001 byte. This allows us to handle all cases except UNEQ and
11002 LTGT. */
11003 switch (comp_code)
11005 case GE: return ARM_GE;
11006 case GT: return ARM_GT;
11007 case LE: return ARM_LS;
11008 case LT: return ARM_MI;
11009 case NE: return ARM_NE;
11010 case EQ: return ARM_EQ;
11011 case ORDERED: return ARM_VC;
11012 case UNORDERED: return ARM_VS;
11013 case UNLT: return ARM_LT;
11014 case UNLE: return ARM_LE;
11015 case UNGT: return ARM_HI;
11016 case UNGE: return ARM_PL;
11017 /* UNEQ and LTGT do not have a representation. */
11018 case UNEQ: /* Fall through. */
11019 case LTGT: /* Fall through. */
11020 default: abort ();
11023 case CC_SWPmode:
11024 switch (comp_code)
11026 case NE: return ARM_NE;
11027 case EQ: return ARM_EQ;
11028 case GE: return ARM_LE;
11029 case GT: return ARM_LT;
11030 case LE: return ARM_GE;
11031 case LT: return ARM_GT;
11032 case GEU: return ARM_LS;
11033 case GTU: return ARM_CC;
11034 case LEU: return ARM_CS;
11035 case LTU: return ARM_HI;
11036 default: abort ();
11039 case CC_Cmode:
11040 switch (comp_code)
11042 case LTU: return ARM_CS;
11043 case GEU: return ARM_CC;
11044 default: abort ();
11047 case CCmode:
11048 switch (comp_code)
11050 case NE: return ARM_NE;
11051 case EQ: return ARM_EQ;
11052 case GE: return ARM_GE;
11053 case GT: return ARM_GT;
11054 case LE: return ARM_LE;
11055 case LT: return ARM_LT;
11056 case GEU: return ARM_CS;
11057 case GTU: return ARM_HI;
11058 case LEU: return ARM_LS;
11059 case LTU: return ARM_CC;
11060 default: abort ();
11063 default: abort ();
11066 abort ();
11069 void
11070 arm_final_prescan_insn (rtx insn)
11072 /* BODY will hold the body of INSN. */
11073 rtx body = PATTERN (insn);
11075 /* This will be 1 if trying to repeat the trick, and things need to be
11076 reversed if it appears to fail. */
11077 int reverse = 0;
11079 /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11080 taken are clobbered, even if the rtl suggests otherwise. It also
11081 means that we have to grub around within the jump expression to find
11082 out what the conditions are when the jump isn't taken. */
11083 int jump_clobbers = 0;
11085 /* If we start with a return insn, we only succeed if we find another one. */
11086 int seeking_return = 0;
11088 /* START_INSN will hold the insn from where we start looking. This is the
11089 first insn after the following code_label if REVERSE is true. */
11090 rtx start_insn = insn;
11092 /* If in state 4, check if the target branch is reached, in order to
11093 change back to state 0. */
11094 if (arm_ccfsm_state == 4)
11096 if (insn == arm_target_insn)
11098 arm_target_insn = NULL;
11099 arm_ccfsm_state = 0;
11101 return;
11104 /* If in state 3, it is possible to repeat the trick, if this insn is an
11105 unconditional branch to a label, and immediately following this branch
11106 is the previous target label which is only used once, and the label this
11107 branch jumps to is not too far off. */
11108 if (arm_ccfsm_state == 3)
11110 if (simplejump_p (insn))
11112 start_insn = next_nonnote_insn (start_insn);
11113 if (GET_CODE (start_insn) == BARRIER)
11115 /* XXX Isn't this always a barrier? */
11116 start_insn = next_nonnote_insn (start_insn);
11118 if (GET_CODE (start_insn) == CODE_LABEL
11119 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11120 && LABEL_NUSES (start_insn) == 1)
11121 reverse = TRUE;
11122 else
11123 return;
11125 else if (GET_CODE (body) == RETURN)
11127 start_insn = next_nonnote_insn (start_insn);
11128 if (GET_CODE (start_insn) == BARRIER)
11129 start_insn = next_nonnote_insn (start_insn);
11130 if (GET_CODE (start_insn) == CODE_LABEL
11131 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11132 && LABEL_NUSES (start_insn) == 1)
11134 reverse = TRUE;
11135 seeking_return = 1;
11137 else
11138 return;
11140 else
11141 return;
11144 if (arm_ccfsm_state != 0 && !reverse)
11145 abort ();
11146 if (GET_CODE (insn) != JUMP_INSN)
11147 return;
11149 /* This jump might be paralleled with a clobber of the condition codes
11150 the jump should always come first */
11151 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11152 body = XVECEXP (body, 0, 0);
11154 if (reverse
11155 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11156 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11158 int insns_skipped;
11159 int fail = FALSE, succeed = FALSE;
11160 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
11161 int then_not_else = TRUE;
11162 rtx this_insn = start_insn, label = 0;
11164 /* If the jump cannot be done with one instruction, we cannot
11165 conditionally execute the instruction in the inverse case. */
11166 if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11168 jump_clobbers = 1;
11169 return;
11172 /* Register the insn jumped to. */
11173 if (reverse)
11175 if (!seeking_return)
11176 label = XEXP (SET_SRC (body), 0);
11178 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11179 label = XEXP (XEXP (SET_SRC (body), 1), 0);
11180 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11182 label = XEXP (XEXP (SET_SRC (body), 2), 0);
11183 then_not_else = FALSE;
11185 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11186 seeking_return = 1;
11187 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11189 seeking_return = 1;
11190 then_not_else = FALSE;
11192 else
11193 abort ();
11195 /* See how many insns this branch skips, and what kind of insns. If all
11196 insns are okay, and the label or unconditional branch to the same
11197 label is not too far away, succeed. */
11198 for (insns_skipped = 0;
11199 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11201 rtx scanbody;
11203 this_insn = next_nonnote_insn (this_insn);
11204 if (!this_insn)
11205 break;
11207 switch (GET_CODE (this_insn))
11209 case CODE_LABEL:
11210 /* Succeed if it is the target label, otherwise fail since
11211 control falls in from somewhere else. */
11212 if (this_insn == label)
11214 if (jump_clobbers)
11216 arm_ccfsm_state = 2;
11217 this_insn = next_nonnote_insn (this_insn);
11219 else
11220 arm_ccfsm_state = 1;
11221 succeed = TRUE;
11223 else
11224 fail = TRUE;
11225 break;
11227 case BARRIER:
11228 /* Succeed if the following insn is the target label.
11229 Otherwise fail.
11230 If return insns are used then the last insn in a function
11231 will be a barrier. */
11232 this_insn = next_nonnote_insn (this_insn);
11233 if (this_insn && this_insn == label)
11235 if (jump_clobbers)
11237 arm_ccfsm_state = 2;
11238 this_insn = next_nonnote_insn (this_insn);
11240 else
11241 arm_ccfsm_state = 1;
11242 succeed = TRUE;
11244 else
11245 fail = TRUE;
11246 break;
11248 case CALL_INSN:
11249 /* The AAPCS says that conditional calls should not be
11250 used since they make interworking inefficient (the
11251 linker can't transform BL<cond> into BLX). That's
11252 only a problem if the machine has BLX. */
11253 if (arm_arch5)
11255 fail = TRUE;
11256 break;
11259 /* Succeed if the following insn is the target label, or
11260 if the following two insns are a barrier and the
11261 target label. */
11262 this_insn = next_nonnote_insn (this_insn);
11263 if (this_insn && GET_CODE (this_insn) == BARRIER)
11264 this_insn = next_nonnote_insn (this_insn);
11266 if (this_insn && this_insn == label
11267 && insns_skipped < max_insns_skipped)
11269 if (jump_clobbers)
11271 arm_ccfsm_state = 2;
11272 this_insn = next_nonnote_insn (this_insn);
11274 else
11275 arm_ccfsm_state = 1;
11276 succeed = TRUE;
11278 else
11279 fail = TRUE;
11280 break;
11282 case JUMP_INSN:
11283 /* If this is an unconditional branch to the same label, succeed.
11284 If it is to another label, do nothing. If it is conditional,
11285 fail. */
11286 /* XXX Probably, the tests for SET and the PC are
11287 unnecessary. */
11289 scanbody = PATTERN (this_insn);
11290 if (GET_CODE (scanbody) == SET
11291 && GET_CODE (SET_DEST (scanbody)) == PC)
11293 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11294 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11296 arm_ccfsm_state = 2;
11297 succeed = TRUE;
11299 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11300 fail = TRUE;
11302 /* Fail if a conditional return is undesirable (eg on a
11303 StrongARM), but still allow this if optimizing for size. */
11304 else if (GET_CODE (scanbody) == RETURN
11305 && !use_return_insn (TRUE, NULL)
11306 && !optimize_size)
11307 fail = TRUE;
11308 else if (GET_CODE (scanbody) == RETURN
11309 && seeking_return)
11311 arm_ccfsm_state = 2;
11312 succeed = TRUE;
11314 else if (GET_CODE (scanbody) == PARALLEL)
11316 switch (get_attr_conds (this_insn))
11318 case CONDS_NOCOND:
11319 break;
11320 default:
11321 fail = TRUE;
11322 break;
11325 else
11326 fail = TRUE; /* Unrecognized jump (eg epilogue). */
11328 break;
11330 case INSN:
11331 /* Instructions using or affecting the condition codes make it
11332 fail. */
11333 scanbody = PATTERN (this_insn);
11334 if (!(GET_CODE (scanbody) == SET
11335 || GET_CODE (scanbody) == PARALLEL)
11336 || get_attr_conds (this_insn) != CONDS_NOCOND)
11337 fail = TRUE;
11339 /* A conditional cirrus instruction must be followed by
11340 a non Cirrus instruction. However, since we
11341 conditionalize instructions in this function and by
11342 the time we get here we can't add instructions
11343 (nops), because shorten_branches() has already been
11344 called, we will disable conditionalizing Cirrus
11345 instructions to be safe. */
11346 if (GET_CODE (scanbody) != USE
11347 && GET_CODE (scanbody) != CLOBBER
11348 && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11349 fail = TRUE;
11350 break;
11352 default:
11353 break;
11356 if (succeed)
11358 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11359 arm_target_label = CODE_LABEL_NUMBER (label);
11360 else if (seeking_return || arm_ccfsm_state == 2)
11362 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11364 this_insn = next_nonnote_insn (this_insn);
11365 if (this_insn && (GET_CODE (this_insn) == BARRIER
11366 || GET_CODE (this_insn) == CODE_LABEL))
11367 abort ();
11369 if (!this_insn)
11371 /* Oh, dear! we ran off the end.. give up. */
11372 recog (PATTERN (insn), insn, NULL);
11373 arm_ccfsm_state = 0;
11374 arm_target_insn = NULL;
11375 return;
11377 arm_target_insn = this_insn;
11379 else
11380 abort ();
11381 if (jump_clobbers)
11383 if (reverse)
11384 abort ();
11385 arm_current_cc =
11386 get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11387 0), 0), 1));
11388 if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11389 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11390 if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11391 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11393 else
11395 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11396 what it was. */
11397 if (!reverse)
11398 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11399 0));
11402 if (reverse || then_not_else)
11403 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11406 /* Restore recog_data (getting the attributes of other insns can
11407 destroy this array, but final.c assumes that it remains intact
11408 across this call; since the insn has been recognized already we
11409 call recog direct). */
11410 recog (PATTERN (insn), insn, NULL);
11414 /* Returns true if REGNO is a valid register
11415 for holding a quantity of tyoe MODE. */
11417 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11419 if (GET_MODE_CLASS (mode) == MODE_CC)
11420 return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11422 if (TARGET_THUMB)
11423 /* For the Thumb we only allow values bigger than SImode in
11424 registers 0 - 6, so that there is always a second low
11425 register available to hold the upper part of the value.
11426 We probably we ought to ensure that the register is the
11427 start of an even numbered register pair. */
11428 return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11430 if (IS_CIRRUS_REGNUM (regno))
11431 /* We have outlawed SI values in Cirrus registers because they
11432 reside in the lower 32 bits, but SF values reside in the
11433 upper 32 bits. This causes gcc all sorts of grief. We can't
11434 even split the registers into pairs because Cirrus SI values
11435 get sign extended to 64bits-- aldyh. */
11436 return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11438 if (IS_VFP_REGNUM (regno))
11440 if (mode == SFmode || mode == SImode)
11441 return TRUE;
11443 /* DFmode values are only valid in even register pairs. */
11444 if (mode == DFmode)
11445 return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11446 return FALSE;
11449 if (IS_IWMMXT_GR_REGNUM (regno))
11450 return mode == SImode;
11452 if (IS_IWMMXT_REGNUM (regno))
11453 return VALID_IWMMXT_REG_MODE (mode);
11455 /* We allow any value to be stored in the general registers.
11456 Restrict doubleword quantities to even register pairs so that we can
11457 use ldrd. */
11458 if (regno <= LAST_ARM_REGNUM)
11459 return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11461 if ( regno == FRAME_POINTER_REGNUM
11462 || regno == ARG_POINTER_REGNUM)
11463 /* We only allow integers in the fake hard registers. */
11464 return GET_MODE_CLASS (mode) == MODE_INT;
11466 /* The only registers left are the FPA registers
11467 which we only allow to hold FP values. */
11468 return GET_MODE_CLASS (mode) == MODE_FLOAT
11469 && regno >= FIRST_FPA_REGNUM
11470 && regno <= LAST_FPA_REGNUM;
11474 arm_regno_class (int regno)
11476 if (TARGET_THUMB)
11478 if (regno == STACK_POINTER_REGNUM)
11479 return STACK_REG;
11480 if (regno == CC_REGNUM)
11481 return CC_REG;
11482 if (regno < 8)
11483 return LO_REGS;
11484 return HI_REGS;
11487 if ( regno <= LAST_ARM_REGNUM
11488 || regno == FRAME_POINTER_REGNUM
11489 || regno == ARG_POINTER_REGNUM)
11490 return GENERAL_REGS;
11492 if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11493 return NO_REGS;
11495 if (IS_CIRRUS_REGNUM (regno))
11496 return CIRRUS_REGS;
11498 if (IS_VFP_REGNUM (regno))
11499 return VFP_REGS;
11501 if (IS_IWMMXT_REGNUM (regno))
11502 return IWMMXT_REGS;
11504 if (IS_IWMMXT_GR_REGNUM (regno))
11505 return IWMMXT_GR_REGS;
11507 return FPA_REGS;
11510 /* Handle a special case when computing the offset
11511 of an argument from the frame pointer. */
11513 arm_debugger_arg_offset (int value, rtx addr)
11515 rtx insn;
11517 /* We are only interested if dbxout_parms() failed to compute the offset. */
11518 if (value != 0)
11519 return 0;
11521 /* We can only cope with the case where the address is held in a register. */
11522 if (GET_CODE (addr) != REG)
11523 return 0;
11525 /* If we are using the frame pointer to point at the argument, then
11526 an offset of 0 is correct. */
11527 if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11528 return 0;
11530 /* If we are using the stack pointer to point at the
11531 argument, then an offset of 0 is correct. */
11532 if ((TARGET_THUMB || !frame_pointer_needed)
11533 && REGNO (addr) == SP_REGNUM)
11534 return 0;
11536 /* Oh dear. The argument is pointed to by a register rather
11537 than being held in a register, or being stored at a known
11538 offset from the frame pointer. Since GDB only understands
11539 those two kinds of argument we must translate the address
11540 held in the register into an offset from the frame pointer.
11541 We do this by searching through the insns for the function
11542 looking to see where this register gets its value. If the
11543 register is initialized from the frame pointer plus an offset
11544 then we are in luck and we can continue, otherwise we give up.
11546 This code is exercised by producing debugging information
11547 for a function with arguments like this:
11549 double func (double a, double b, int c, double d) {return d;}
11551 Without this code the stab for parameter 'd' will be set to
11552 an offset of 0 from the frame pointer, rather than 8. */
11554 /* The if() statement says:
11556 If the insn is a normal instruction
11557 and if the insn is setting the value in a register
11558 and if the register being set is the register holding the address of the argument
11559 and if the address is computing by an addition
11560 that involves adding to a register
11561 which is the frame pointer
11562 a constant integer
11564 then... */
11566 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11568 if ( GET_CODE (insn) == INSN
11569 && GET_CODE (PATTERN (insn)) == SET
11570 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11571 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11572 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11573 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11574 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11577 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11579 break;
11583 if (value == 0)
11585 debug_rtx (addr);
11586 warning ("unable to compute real location of stacked parameter");
11587 value = 8; /* XXX magic hack */
11590 return value;
11593 #define def_mbuiltin(MASK, NAME, TYPE, CODE) \
11594 do \
11596 if ((MASK) & insn_flags) \
11597 lang_hooks.builtin_function ((NAME), (TYPE), (CODE), \
11598 BUILT_IN_MD, NULL, NULL_TREE); \
11600 while (0)
11602 struct builtin_description
11604 const unsigned int mask;
11605 const enum insn_code icode;
11606 const char * const name;
11607 const enum arm_builtins code;
11608 const enum rtx_code comparison;
11609 const unsigned int flag;
11612 static const struct builtin_description bdesc_2arg[] =
11614 #define IWMMXT_BUILTIN(code, string, builtin) \
11615 { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11616 ARM_BUILTIN_##builtin, 0, 0 },
11618 IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11619 IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11620 IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11621 IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11622 IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11623 IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11624 IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11625 IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11626 IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11627 IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11628 IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11629 IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11630 IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11631 IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11632 IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11633 IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11634 IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11635 IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11636 IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11637 IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11638 IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11639 IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11640 IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11641 IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11642 IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11643 IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11644 IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11645 IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11646 IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11647 IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11648 IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11649 IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11650 IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11651 IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11652 IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11653 IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11654 IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11655 IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11656 IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11657 IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11658 IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11659 IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11660 IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11661 IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11662 IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11663 IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11664 IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11665 IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11666 IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11667 IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11668 IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11669 IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11670 IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11671 IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11672 IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11673 IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11674 IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11675 IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11677 #define IWMMXT_BUILTIN2(code, builtin) \
11678 { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11680 IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11681 IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11682 IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11683 IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11684 IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11685 IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11686 IWMMXT_BUILTIN2 (ashlv4hi3_di, WSLLH)
11687 IWMMXT_BUILTIN2 (ashlv4hi3, WSLLHI)
11688 IWMMXT_BUILTIN2 (ashlv2si3_di, WSLLW)
11689 IWMMXT_BUILTIN2 (ashlv2si3, WSLLWI)
11690 IWMMXT_BUILTIN2 (ashldi3_di, WSLLD)
11691 IWMMXT_BUILTIN2 (ashldi3_iwmmxt, WSLLDI)
11692 IWMMXT_BUILTIN2 (lshrv4hi3_di, WSRLH)
11693 IWMMXT_BUILTIN2 (lshrv4hi3, WSRLHI)
11694 IWMMXT_BUILTIN2 (lshrv2si3_di, WSRLW)
11695 IWMMXT_BUILTIN2 (lshrv2si3, WSRLWI)
11696 IWMMXT_BUILTIN2 (lshrdi3_di, WSRLD)
11697 IWMMXT_BUILTIN2 (lshrdi3_iwmmxt, WSRLDI)
11698 IWMMXT_BUILTIN2 (ashrv4hi3_di, WSRAH)
11699 IWMMXT_BUILTIN2 (ashrv4hi3, WSRAHI)
11700 IWMMXT_BUILTIN2 (ashrv2si3_di, WSRAW)
11701 IWMMXT_BUILTIN2 (ashrv2si3, WSRAWI)
11702 IWMMXT_BUILTIN2 (ashrdi3_di, WSRAD)
11703 IWMMXT_BUILTIN2 (ashrdi3_iwmmxt, WSRADI)
11704 IWMMXT_BUILTIN2 (rorv4hi3_di, WRORH)
11705 IWMMXT_BUILTIN2 (rorv4hi3, WRORHI)
11706 IWMMXT_BUILTIN2 (rorv2si3_di, WRORW)
11707 IWMMXT_BUILTIN2 (rorv2si3, WRORWI)
11708 IWMMXT_BUILTIN2 (rordi3_di, WRORD)
11709 IWMMXT_BUILTIN2 (rordi3, WRORDI)
11710 IWMMXT_BUILTIN2 (iwmmxt_wmacuz, WMACUZ)
11711 IWMMXT_BUILTIN2 (iwmmxt_wmacsz, WMACSZ)
11714 static const struct builtin_description bdesc_1arg[] =
11716 IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11717 IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11718 IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11719 IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11720 IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11721 IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11722 IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11723 IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11724 IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11725 IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11726 IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11727 IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11728 IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11729 IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11730 IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11731 IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11732 IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11733 IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11736 /* Set up all the iWMMXt builtins. This is
11737 not called if TARGET_IWMMXT is zero. */
11739 static void
11740 arm_init_iwmmxt_builtins (void)
11742 const struct builtin_description * d;
11743 size_t i;
11744 tree endlink = void_list_node;
11746 tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11747 tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11748 tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11750 tree int_ftype_int
11751 = build_function_type (integer_type_node,
11752 tree_cons (NULL_TREE, integer_type_node, endlink));
11753 tree v8qi_ftype_v8qi_v8qi_int
11754 = build_function_type (V8QI_type_node,
11755 tree_cons (NULL_TREE, V8QI_type_node,
11756 tree_cons (NULL_TREE, V8QI_type_node,
11757 tree_cons (NULL_TREE,
11758 integer_type_node,
11759 endlink))));
11760 tree v4hi_ftype_v4hi_int
11761 = build_function_type (V4HI_type_node,
11762 tree_cons (NULL_TREE, V4HI_type_node,
11763 tree_cons (NULL_TREE, integer_type_node,
11764 endlink)));
11765 tree v2si_ftype_v2si_int
11766 = build_function_type (V2SI_type_node,
11767 tree_cons (NULL_TREE, V2SI_type_node,
11768 tree_cons (NULL_TREE, integer_type_node,
11769 endlink)));
11770 tree v2si_ftype_di_di
11771 = build_function_type (V2SI_type_node,
11772 tree_cons (NULL_TREE, long_long_integer_type_node,
11773 tree_cons (NULL_TREE, long_long_integer_type_node,
11774 endlink)));
11775 tree di_ftype_di_int
11776 = build_function_type (long_long_integer_type_node,
11777 tree_cons (NULL_TREE, long_long_integer_type_node,
11778 tree_cons (NULL_TREE, integer_type_node,
11779 endlink)));
11780 tree di_ftype_di_int_int
11781 = build_function_type (long_long_integer_type_node,
11782 tree_cons (NULL_TREE, long_long_integer_type_node,
11783 tree_cons (NULL_TREE, integer_type_node,
11784 tree_cons (NULL_TREE,
11785 integer_type_node,
11786 endlink))));
11787 tree int_ftype_v8qi
11788 = build_function_type (integer_type_node,
11789 tree_cons (NULL_TREE, V8QI_type_node,
11790 endlink));
11791 tree int_ftype_v4hi
11792 = build_function_type (integer_type_node,
11793 tree_cons (NULL_TREE, V4HI_type_node,
11794 endlink));
11795 tree int_ftype_v2si
11796 = build_function_type (integer_type_node,
11797 tree_cons (NULL_TREE, V2SI_type_node,
11798 endlink));
11799 tree int_ftype_v8qi_int
11800 = build_function_type (integer_type_node,
11801 tree_cons (NULL_TREE, V8QI_type_node,
11802 tree_cons (NULL_TREE, integer_type_node,
11803 endlink)));
11804 tree int_ftype_v4hi_int
11805 = build_function_type (integer_type_node,
11806 tree_cons (NULL_TREE, V4HI_type_node,
11807 tree_cons (NULL_TREE, integer_type_node,
11808 endlink)));
11809 tree int_ftype_v2si_int
11810 = build_function_type (integer_type_node,
11811 tree_cons (NULL_TREE, V2SI_type_node,
11812 tree_cons (NULL_TREE, integer_type_node,
11813 endlink)));
11814 tree v8qi_ftype_v8qi_int_int
11815 = build_function_type (V8QI_type_node,
11816 tree_cons (NULL_TREE, V8QI_type_node,
11817 tree_cons (NULL_TREE, integer_type_node,
11818 tree_cons (NULL_TREE,
11819 integer_type_node,
11820 endlink))));
11821 tree v4hi_ftype_v4hi_int_int
11822 = build_function_type (V4HI_type_node,
11823 tree_cons (NULL_TREE, V4HI_type_node,
11824 tree_cons (NULL_TREE, integer_type_node,
11825 tree_cons (NULL_TREE,
11826 integer_type_node,
11827 endlink))));
11828 tree v2si_ftype_v2si_int_int
11829 = build_function_type (V2SI_type_node,
11830 tree_cons (NULL_TREE, V2SI_type_node,
11831 tree_cons (NULL_TREE, integer_type_node,
11832 tree_cons (NULL_TREE,
11833 integer_type_node,
11834 endlink))));
11835 /* Miscellaneous. */
11836 tree v8qi_ftype_v4hi_v4hi
11837 = build_function_type (V8QI_type_node,
11838 tree_cons (NULL_TREE, V4HI_type_node,
11839 tree_cons (NULL_TREE, V4HI_type_node,
11840 endlink)));
11841 tree v4hi_ftype_v2si_v2si
11842 = build_function_type (V4HI_type_node,
11843 tree_cons (NULL_TREE, V2SI_type_node,
11844 tree_cons (NULL_TREE, V2SI_type_node,
11845 endlink)));
11846 tree v2si_ftype_v4hi_v4hi
11847 = build_function_type (V2SI_type_node,
11848 tree_cons (NULL_TREE, V4HI_type_node,
11849 tree_cons (NULL_TREE, V4HI_type_node,
11850 endlink)));
11851 tree v2si_ftype_v8qi_v8qi
11852 = build_function_type (V2SI_type_node,
11853 tree_cons (NULL_TREE, V8QI_type_node,
11854 tree_cons (NULL_TREE, V8QI_type_node,
11855 endlink)));
11856 tree v4hi_ftype_v4hi_di
11857 = build_function_type (V4HI_type_node,
11858 tree_cons (NULL_TREE, V4HI_type_node,
11859 tree_cons (NULL_TREE,
11860 long_long_integer_type_node,
11861 endlink)));
11862 tree v2si_ftype_v2si_di
11863 = build_function_type (V2SI_type_node,
11864 tree_cons (NULL_TREE, V2SI_type_node,
11865 tree_cons (NULL_TREE,
11866 long_long_integer_type_node,
11867 endlink)));
11868 tree void_ftype_int_int
11869 = build_function_type (void_type_node,
11870 tree_cons (NULL_TREE, integer_type_node,
11871 tree_cons (NULL_TREE, integer_type_node,
11872 endlink)));
11873 tree di_ftype_void
11874 = build_function_type (long_long_unsigned_type_node, endlink);
11875 tree di_ftype_v8qi
11876 = build_function_type (long_long_integer_type_node,
11877 tree_cons (NULL_TREE, V8QI_type_node,
11878 endlink));
11879 tree di_ftype_v4hi
11880 = build_function_type (long_long_integer_type_node,
11881 tree_cons (NULL_TREE, V4HI_type_node,
11882 endlink));
11883 tree di_ftype_v2si
11884 = build_function_type (long_long_integer_type_node,
11885 tree_cons (NULL_TREE, V2SI_type_node,
11886 endlink));
11887 tree v2si_ftype_v4hi
11888 = build_function_type (V2SI_type_node,
11889 tree_cons (NULL_TREE, V4HI_type_node,
11890 endlink));
11891 tree v4hi_ftype_v8qi
11892 = build_function_type (V4HI_type_node,
11893 tree_cons (NULL_TREE, V8QI_type_node,
11894 endlink));
11896 tree di_ftype_di_v4hi_v4hi
11897 = build_function_type (long_long_unsigned_type_node,
11898 tree_cons (NULL_TREE,
11899 long_long_unsigned_type_node,
11900 tree_cons (NULL_TREE, V4HI_type_node,
11901 tree_cons (NULL_TREE,
11902 V4HI_type_node,
11903 endlink))));
11905 tree di_ftype_v4hi_v4hi
11906 = build_function_type (long_long_unsigned_type_node,
11907 tree_cons (NULL_TREE, V4HI_type_node,
11908 tree_cons (NULL_TREE, V4HI_type_node,
11909 endlink)));
11911 /* Normal vector binops. */
11912 tree v8qi_ftype_v8qi_v8qi
11913 = build_function_type (V8QI_type_node,
11914 tree_cons (NULL_TREE, V8QI_type_node,
11915 tree_cons (NULL_TREE, V8QI_type_node,
11916 endlink)));
11917 tree v4hi_ftype_v4hi_v4hi
11918 = build_function_type (V4HI_type_node,
11919 tree_cons (NULL_TREE, V4HI_type_node,
11920 tree_cons (NULL_TREE, V4HI_type_node,
11921 endlink)));
11922 tree v2si_ftype_v2si_v2si
11923 = build_function_type (V2SI_type_node,
11924 tree_cons (NULL_TREE, V2SI_type_node,
11925 tree_cons (NULL_TREE, V2SI_type_node,
11926 endlink)));
11927 tree di_ftype_di_di
11928 = build_function_type (long_long_unsigned_type_node,
11929 tree_cons (NULL_TREE, long_long_unsigned_type_node,
11930 tree_cons (NULL_TREE,
11931 long_long_unsigned_type_node,
11932 endlink)));
11934 /* Add all builtins that are more or less simple operations on two
11935 operands. */
11936 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11938 /* Use one of the operands; the target can have a different mode for
11939 mask-generating compares. */
11940 enum machine_mode mode;
11941 tree type;
11943 if (d->name == 0)
11944 continue;
11946 mode = insn_data[d->icode].operand[1].mode;
11948 switch (mode)
11950 case V8QImode:
11951 type = v8qi_ftype_v8qi_v8qi;
11952 break;
11953 case V4HImode:
11954 type = v4hi_ftype_v4hi_v4hi;
11955 break;
11956 case V2SImode:
11957 type = v2si_ftype_v2si_v2si;
11958 break;
11959 case DImode:
11960 type = di_ftype_di_di;
11961 break;
11963 default:
11964 abort ();
11967 def_mbuiltin (d->mask, d->name, type, d->code);
11970 /* Add the remaining MMX insns with somewhat more complicated types. */
11971 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11972 def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11973 def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11975 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11976 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11977 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11978 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11979 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11980 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11982 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11983 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11984 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11985 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11986 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11987 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11989 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11990 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11991 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11992 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11993 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11994 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11996 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11997 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11998 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11999 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
12000 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
12001 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
12003 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
12005 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
12006 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
12007 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
12008 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
12010 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
12011 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
12012 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
12013 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
12014 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
12015 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
12016 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
12017 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
12018 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
12020 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
12021 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
12022 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
12024 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
12025 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
12026 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
12028 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
12029 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
12030 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
12031 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
12032 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
12033 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
12035 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
12036 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
12037 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
12038 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
12039 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
12040 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
12041 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
12042 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
12043 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
12044 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
12045 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
12046 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
12048 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
12049 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
12050 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
12051 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
12053 def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
12054 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
12055 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
12056 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
12057 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
12058 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
12059 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
12062 static void
12063 arm_init_builtins (void)
12065 if (TARGET_REALLY_IWMMXT)
12066 arm_init_iwmmxt_builtins ();
12069 /* Errors in the source file can cause expand_expr to return const0_rtx
12070 where we expect a vector. To avoid crashing, use one of the vector
12071 clear instructions. */
12073 static rtx
12074 safe_vector_operand (rtx x, enum machine_mode mode)
12076 if (x != const0_rtx)
12077 return x;
12078 x = gen_reg_rtx (mode);
12080 emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12081 : gen_rtx_SUBREG (DImode, x, 0)));
12082 return x;
12085 /* Subroutine of arm_expand_builtin to take care of binop insns. */
12087 static rtx
12088 arm_expand_binop_builtin (enum insn_code icode,
12089 tree arglist, rtx target)
12091 rtx pat;
12092 tree arg0 = TREE_VALUE (arglist);
12093 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12094 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12095 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12096 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12097 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12098 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12100 if (VECTOR_MODE_P (mode0))
12101 op0 = safe_vector_operand (op0, mode0);
12102 if (VECTOR_MODE_P (mode1))
12103 op1 = safe_vector_operand (op1, mode1);
12105 if (! target
12106 || GET_MODE (target) != tmode
12107 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12108 target = gen_reg_rtx (tmode);
12110 /* In case the insn wants input operands in modes different from
12111 the result, abort. */
12112 if (GET_MODE (op0) != mode0 || GET_MODE (op1) != mode1)
12113 abort ();
12115 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12116 op0 = copy_to_mode_reg (mode0, op0);
12117 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12118 op1 = copy_to_mode_reg (mode1, op1);
12120 pat = GEN_FCN (icode) (target, op0, op1);
12121 if (! pat)
12122 return 0;
12123 emit_insn (pat);
12124 return target;
12127 /* Subroutine of arm_expand_builtin to take care of unop insns. */
12129 static rtx
12130 arm_expand_unop_builtin (enum insn_code icode,
12131 tree arglist, rtx target, int do_load)
12133 rtx pat;
12134 tree arg0 = TREE_VALUE (arglist);
12135 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12136 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12137 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12139 if (! target
12140 || GET_MODE (target) != tmode
12141 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12142 target = gen_reg_rtx (tmode);
12143 if (do_load)
12144 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12145 else
12147 if (VECTOR_MODE_P (mode0))
12148 op0 = safe_vector_operand (op0, mode0);
12150 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12151 op0 = copy_to_mode_reg (mode0, op0);
12154 pat = GEN_FCN (icode) (target, op0);
12155 if (! pat)
12156 return 0;
12157 emit_insn (pat);
12158 return target;
12161 /* Expand an expression EXP that calls a built-in function,
12162 with result going to TARGET if that's convenient
12163 (and in mode MODE if that's convenient).
12164 SUBTARGET may be used as the target for computing one of EXP's operands.
12165 IGNORE is nonzero if the value is to be ignored. */
12167 static rtx
12168 arm_expand_builtin (tree exp,
12169 rtx target,
12170 rtx subtarget ATTRIBUTE_UNUSED,
12171 enum machine_mode mode ATTRIBUTE_UNUSED,
12172 int ignore ATTRIBUTE_UNUSED)
12174 const struct builtin_description * d;
12175 enum insn_code icode;
12176 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12177 tree arglist = TREE_OPERAND (exp, 1);
12178 tree arg0;
12179 tree arg1;
12180 tree arg2;
12181 rtx op0;
12182 rtx op1;
12183 rtx op2;
12184 rtx pat;
12185 int fcode = DECL_FUNCTION_CODE (fndecl);
12186 size_t i;
12187 enum machine_mode tmode;
12188 enum machine_mode mode0;
12189 enum machine_mode mode1;
12190 enum machine_mode mode2;
12192 switch (fcode)
12194 case ARM_BUILTIN_TEXTRMSB:
12195 case ARM_BUILTIN_TEXTRMUB:
12196 case ARM_BUILTIN_TEXTRMSH:
12197 case ARM_BUILTIN_TEXTRMUH:
12198 case ARM_BUILTIN_TEXTRMSW:
12199 case ARM_BUILTIN_TEXTRMUW:
12200 icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12201 : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12202 : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12203 : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12204 : CODE_FOR_iwmmxt_textrmw);
12206 arg0 = TREE_VALUE (arglist);
12207 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12208 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12209 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12210 tmode = insn_data[icode].operand[0].mode;
12211 mode0 = insn_data[icode].operand[1].mode;
12212 mode1 = insn_data[icode].operand[2].mode;
12214 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12215 op0 = copy_to_mode_reg (mode0, op0);
12216 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12218 /* @@@ better error message */
12219 error ("selector must be an immediate");
12220 return gen_reg_rtx (tmode);
12222 if (target == 0
12223 || GET_MODE (target) != tmode
12224 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12225 target = gen_reg_rtx (tmode);
12226 pat = GEN_FCN (icode) (target, op0, op1);
12227 if (! pat)
12228 return 0;
12229 emit_insn (pat);
12230 return target;
12232 case ARM_BUILTIN_TINSRB:
12233 case ARM_BUILTIN_TINSRH:
12234 case ARM_BUILTIN_TINSRW:
12235 icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12236 : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12237 : CODE_FOR_iwmmxt_tinsrw);
12238 arg0 = TREE_VALUE (arglist);
12239 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12240 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12241 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12242 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12243 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12244 tmode = insn_data[icode].operand[0].mode;
12245 mode0 = insn_data[icode].operand[1].mode;
12246 mode1 = insn_data[icode].operand[2].mode;
12247 mode2 = insn_data[icode].operand[3].mode;
12249 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12250 op0 = copy_to_mode_reg (mode0, op0);
12251 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12252 op1 = copy_to_mode_reg (mode1, op1);
12253 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12255 /* @@@ better error message */
12256 error ("selector must be an immediate");
12257 return const0_rtx;
12259 if (target == 0
12260 || GET_MODE (target) != tmode
12261 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12262 target = gen_reg_rtx (tmode);
12263 pat = GEN_FCN (icode) (target, op0, op1, op2);
12264 if (! pat)
12265 return 0;
12266 emit_insn (pat);
12267 return target;
12269 case ARM_BUILTIN_SETWCX:
12270 arg0 = TREE_VALUE (arglist);
12271 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12272 op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12273 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12274 emit_insn (gen_iwmmxt_tmcr (op1, op0));
12275 return 0;
12277 case ARM_BUILTIN_GETWCX:
12278 arg0 = TREE_VALUE (arglist);
12279 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12280 target = gen_reg_rtx (SImode);
12281 emit_insn (gen_iwmmxt_tmrc (target, op0));
12282 return target;
12284 case ARM_BUILTIN_WSHUFH:
12285 icode = CODE_FOR_iwmmxt_wshufh;
12286 arg0 = TREE_VALUE (arglist);
12287 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12288 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12289 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12290 tmode = insn_data[icode].operand[0].mode;
12291 mode1 = insn_data[icode].operand[1].mode;
12292 mode2 = insn_data[icode].operand[2].mode;
12294 if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12295 op0 = copy_to_mode_reg (mode1, op0);
12296 if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12298 /* @@@ better error message */
12299 error ("mask must be an immediate");
12300 return const0_rtx;
12302 if (target == 0
12303 || GET_MODE (target) != tmode
12304 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12305 target = gen_reg_rtx (tmode);
12306 pat = GEN_FCN (icode) (target, op0, op1);
12307 if (! pat)
12308 return 0;
12309 emit_insn (pat);
12310 return target;
12312 case ARM_BUILTIN_WSADB:
12313 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12314 case ARM_BUILTIN_WSADH:
12315 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12316 case ARM_BUILTIN_WSADBZ:
12317 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12318 case ARM_BUILTIN_WSADHZ:
12319 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12321 /* Several three-argument builtins. */
12322 case ARM_BUILTIN_WMACS:
12323 case ARM_BUILTIN_WMACU:
12324 case ARM_BUILTIN_WALIGN:
12325 case ARM_BUILTIN_TMIA:
12326 case ARM_BUILTIN_TMIAPH:
12327 case ARM_BUILTIN_TMIATT:
12328 case ARM_BUILTIN_TMIATB:
12329 case ARM_BUILTIN_TMIABT:
12330 case ARM_BUILTIN_TMIABB:
12331 icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12332 : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12333 : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12334 : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12335 : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12336 : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12337 : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12338 : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12339 : CODE_FOR_iwmmxt_walign);
12340 arg0 = TREE_VALUE (arglist);
12341 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12342 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12343 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12344 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12345 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12346 tmode = insn_data[icode].operand[0].mode;
12347 mode0 = insn_data[icode].operand[1].mode;
12348 mode1 = insn_data[icode].operand[2].mode;
12349 mode2 = insn_data[icode].operand[3].mode;
12351 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12352 op0 = copy_to_mode_reg (mode0, op0);
12353 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12354 op1 = copy_to_mode_reg (mode1, op1);
12355 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12356 op2 = copy_to_mode_reg (mode2, op2);
12357 if (target == 0
12358 || GET_MODE (target) != tmode
12359 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12360 target = gen_reg_rtx (tmode);
12361 pat = GEN_FCN (icode) (target, op0, op1, op2);
12362 if (! pat)
12363 return 0;
12364 emit_insn (pat);
12365 return target;
12367 case ARM_BUILTIN_WZERO:
12368 target = gen_reg_rtx (DImode);
12369 emit_insn (gen_iwmmxt_clrdi (target));
12370 return target;
12372 default:
12373 break;
12376 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12377 if (d->code == (const enum arm_builtins) fcode)
12378 return arm_expand_binop_builtin (d->icode, arglist, target);
12380 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12381 if (d->code == (const enum arm_builtins) fcode)
12382 return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12384 /* @@@ Should really do something sensible here. */
12385 return NULL_RTX;
12388 /* Recursively search through all of the blocks in a function
12389 checking to see if any of the variables created in that
12390 function match the RTX called 'orig'. If they do then
12391 replace them with the RTX called 'new'. */
12392 static void
12393 replace_symbols_in_block (tree block, rtx orig, rtx new)
12395 for (; block; block = BLOCK_CHAIN (block))
12397 tree sym;
12399 if (!TREE_USED (block))
12400 continue;
12402 for (sym = BLOCK_VARS (block); sym; sym = TREE_CHAIN (sym))
12404 if ( (DECL_NAME (sym) == 0 && TREE_CODE (sym) != TYPE_DECL)
12405 || DECL_IGNORED_P (sym)
12406 || TREE_CODE (sym) != VAR_DECL
12407 || DECL_EXTERNAL (sym)
12408 || !rtx_equal_p (DECL_RTL (sym), orig)
12410 continue;
12412 SET_DECL_RTL (sym, new);
12415 replace_symbols_in_block (BLOCK_SUBBLOCKS (block), orig, new);
12419 /* Return the number (counting from 0) of
12420 the least significant set bit in MASK. */
12422 inline static int
12423 number_of_first_bit_set (int mask)
12425 int bit;
12427 for (bit = 0;
12428 (mask & (1 << bit)) == 0;
12429 ++bit)
12430 continue;
12432 return bit;
12435 /* Generate code to return from a thumb function.
12436 If 'reg_containing_return_addr' is -1, then the return address is
12437 actually on the stack, at the stack pointer. */
12438 static void
12439 thumb_exit (FILE *f, int reg_containing_return_addr, rtx eh_ofs)
12441 unsigned regs_available_for_popping;
12442 unsigned regs_to_pop;
12443 int pops_needed;
12444 unsigned available;
12445 unsigned required;
12446 int mode;
12447 int size;
12448 int restore_a4 = FALSE;
12450 /* Compute the registers we need to pop. */
12451 regs_to_pop = 0;
12452 pops_needed = 0;
12454 /* There is an assumption here, that if eh_ofs is not NULL, the
12455 normal return address will have been pushed. */
12456 if (reg_containing_return_addr == -1 || eh_ofs)
12458 /* When we are generating a return for __builtin_eh_return,
12459 reg_containing_return_addr must specify the return regno. */
12460 if (eh_ofs && reg_containing_return_addr == -1)
12461 abort ();
12463 regs_to_pop |= 1 << LR_REGNUM;
12464 ++pops_needed;
12467 if (TARGET_BACKTRACE)
12469 /* Restore the (ARM) frame pointer and stack pointer. */
12470 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12471 pops_needed += 2;
12474 /* If there is nothing to pop then just emit the BX instruction and
12475 return. */
12476 if (pops_needed == 0)
12478 if (eh_ofs)
12479 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12481 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12482 return;
12484 /* Otherwise if we are not supporting interworking and we have not created
12485 a backtrace structure and the function was not entered in ARM mode then
12486 just pop the return address straight into the PC. */
12487 else if (!TARGET_INTERWORK
12488 && !TARGET_BACKTRACE
12489 && !is_called_in_ARM_mode (current_function_decl))
12491 if (eh_ofs)
12493 asm_fprintf (f, "\tadd\t%r, #4\n", SP_REGNUM);
12494 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12495 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12497 else
12498 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12500 return;
12503 /* Find out how many of the (return) argument registers we can corrupt. */
12504 regs_available_for_popping = 0;
12506 /* If returning via __builtin_eh_return, the bottom three registers
12507 all contain information needed for the return. */
12508 if (eh_ofs)
12509 size = 12;
12510 else
12512 /* If we can deduce the registers used from the function's
12513 return value. This is more reliable that examining
12514 regs_ever_live[] because that will be set if the register is
12515 ever used in the function, not just if the register is used
12516 to hold a return value. */
12518 if (current_function_return_rtx != 0)
12519 mode = GET_MODE (current_function_return_rtx);
12520 else
12521 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12523 size = GET_MODE_SIZE (mode);
12525 if (size == 0)
12527 /* In a void function we can use any argument register.
12528 In a function that returns a structure on the stack
12529 we can use the second and third argument registers. */
12530 if (mode == VOIDmode)
12531 regs_available_for_popping =
12532 (1 << ARG_REGISTER (1))
12533 | (1 << ARG_REGISTER (2))
12534 | (1 << ARG_REGISTER (3));
12535 else
12536 regs_available_for_popping =
12537 (1 << ARG_REGISTER (2))
12538 | (1 << ARG_REGISTER (3));
12540 else if (size <= 4)
12541 regs_available_for_popping =
12542 (1 << ARG_REGISTER (2))
12543 | (1 << ARG_REGISTER (3));
12544 else if (size <= 8)
12545 regs_available_for_popping =
12546 (1 << ARG_REGISTER (3));
12549 /* Match registers to be popped with registers into which we pop them. */
12550 for (available = regs_available_for_popping,
12551 required = regs_to_pop;
12552 required != 0 && available != 0;
12553 available &= ~(available & - available),
12554 required &= ~(required & - required))
12555 -- pops_needed;
12557 /* If we have any popping registers left over, remove them. */
12558 if (available > 0)
12559 regs_available_for_popping &= ~available;
12561 /* Otherwise if we need another popping register we can use
12562 the fourth argument register. */
12563 else if (pops_needed)
12565 /* If we have not found any free argument registers and
12566 reg a4 contains the return address, we must move it. */
12567 if (regs_available_for_popping == 0
12568 && reg_containing_return_addr == LAST_ARG_REGNUM)
12570 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12571 reg_containing_return_addr = LR_REGNUM;
12573 else if (size > 12)
12575 /* Register a4 is being used to hold part of the return value,
12576 but we have dire need of a free, low register. */
12577 restore_a4 = TRUE;
12579 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12582 if (reg_containing_return_addr != LAST_ARG_REGNUM)
12584 /* The fourth argument register is available. */
12585 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12587 --pops_needed;
12591 /* Pop as many registers as we can. */
12592 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12593 regs_available_for_popping);
12595 /* Process the registers we popped. */
12596 if (reg_containing_return_addr == -1)
12598 /* The return address was popped into the lowest numbered register. */
12599 regs_to_pop &= ~(1 << LR_REGNUM);
12601 reg_containing_return_addr =
12602 number_of_first_bit_set (regs_available_for_popping);
12604 /* Remove this register for the mask of available registers, so that
12605 the return address will not be corrupted by further pops. */
12606 regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12609 /* If we popped other registers then handle them here. */
12610 if (regs_available_for_popping)
12612 int frame_pointer;
12614 /* Work out which register currently contains the frame pointer. */
12615 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12617 /* Move it into the correct place. */
12618 asm_fprintf (f, "\tmov\t%r, %r\n",
12619 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12621 /* (Temporarily) remove it from the mask of popped registers. */
12622 regs_available_for_popping &= ~(1 << frame_pointer);
12623 regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12625 if (regs_available_for_popping)
12627 int stack_pointer;
12629 /* We popped the stack pointer as well,
12630 find the register that contains it. */
12631 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12633 /* Move it into the stack register. */
12634 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12636 /* At this point we have popped all necessary registers, so
12637 do not worry about restoring regs_available_for_popping
12638 to its correct value:
12640 assert (pops_needed == 0)
12641 assert (regs_available_for_popping == (1 << frame_pointer))
12642 assert (regs_to_pop == (1 << STACK_POINTER)) */
12644 else
12646 /* Since we have just move the popped value into the frame
12647 pointer, the popping register is available for reuse, and
12648 we know that we still have the stack pointer left to pop. */
12649 regs_available_for_popping |= (1 << frame_pointer);
12653 /* If we still have registers left on the stack, but we no longer have
12654 any registers into which we can pop them, then we must move the return
12655 address into the link register and make available the register that
12656 contained it. */
12657 if (regs_available_for_popping == 0 && pops_needed > 0)
12659 regs_available_for_popping |= 1 << reg_containing_return_addr;
12661 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12662 reg_containing_return_addr);
12664 reg_containing_return_addr = LR_REGNUM;
12667 /* If we have registers left on the stack then pop some more.
12668 We know that at most we will want to pop FP and SP. */
12669 if (pops_needed > 0)
12671 int popped_into;
12672 int move_to;
12674 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12675 regs_available_for_popping);
12677 /* We have popped either FP or SP.
12678 Move whichever one it is into the correct register. */
12679 popped_into = number_of_first_bit_set (regs_available_for_popping);
12680 move_to = number_of_first_bit_set (regs_to_pop);
12682 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12684 regs_to_pop &= ~(1 << move_to);
12686 --pops_needed;
12689 /* If we still have not popped everything then we must have only
12690 had one register available to us and we are now popping the SP. */
12691 if (pops_needed > 0)
12693 int popped_into;
12695 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12696 regs_available_for_popping);
12698 popped_into = number_of_first_bit_set (regs_available_for_popping);
12700 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12702 assert (regs_to_pop == (1 << STACK_POINTER))
12703 assert (pops_needed == 1)
12707 /* If necessary restore the a4 register. */
12708 if (restore_a4)
12710 if (reg_containing_return_addr != LR_REGNUM)
12712 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12713 reg_containing_return_addr = LR_REGNUM;
12716 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12719 if (eh_ofs)
12720 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12722 /* Return to caller. */
12723 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12726 /* Emit code to push or pop registers to or from the stack. F is the
12727 assembly file. MASK is the registers to push or pop. PUSH is
12728 nonzero if we should push, and zero if we should pop. For debugging
12729 output, if pushing, adjust CFA_OFFSET by the amount of space added
12730 to the stack. REAL_REGS should have the same number of bits set as
12731 MASK, and will be used instead (in the same order) to describe which
12732 registers were saved - this is used to mark the save slots when we
12733 push high registers after moving them to low registers. */
12734 static void
12735 thumb_pushpop (FILE *f, int mask, int push, int *cfa_offset, int real_regs)
12737 int regno;
12738 int lo_mask = mask & 0xFF;
12739 int pushed_words = 0;
12741 if (lo_mask == 0 && !push && (mask & (1 << 15)))
12743 /* Special case. Do not generate a POP PC statement here, do it in
12744 thumb_exit() */
12745 thumb_exit (f, -1, NULL_RTX);
12746 return;
12749 fprintf (f, "\t%s\t{", push ? "push" : "pop");
12751 /* Look at the low registers first. */
12752 for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12754 if (lo_mask & 1)
12756 asm_fprintf (f, "%r", regno);
12758 if ((lo_mask & ~1) != 0)
12759 fprintf (f, ", ");
12761 pushed_words++;
12765 if (push && (mask & (1 << LR_REGNUM)))
12767 /* Catch pushing the LR. */
12768 if (mask & 0xFF)
12769 fprintf (f, ", ");
12771 asm_fprintf (f, "%r", LR_REGNUM);
12773 pushed_words++;
12775 else if (!push && (mask & (1 << PC_REGNUM)))
12777 /* Catch popping the PC. */
12778 if (TARGET_INTERWORK || TARGET_BACKTRACE)
12780 /* The PC is never poped directly, instead
12781 it is popped into r3 and then BX is used. */
12782 fprintf (f, "}\n");
12784 thumb_exit (f, -1, NULL_RTX);
12786 return;
12788 else
12790 if (mask & 0xFF)
12791 fprintf (f, ", ");
12793 asm_fprintf (f, "%r", PC_REGNUM);
12797 fprintf (f, "}\n");
12799 if (push && pushed_words && dwarf2out_do_frame ())
12801 char *l = dwarf2out_cfi_label ();
12802 int pushed_mask = real_regs;
12804 *cfa_offset += pushed_words * 4;
12805 dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12807 pushed_words = 0;
12808 pushed_mask = real_regs;
12809 for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12811 if (pushed_mask & 1)
12812 dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12817 void
12818 thumb_final_prescan_insn (rtx insn)
12820 if (flag_print_asm_name)
12821 asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12822 INSN_ADDRESSES (INSN_UID (insn)));
12826 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12828 unsigned HOST_WIDE_INT mask = 0xff;
12829 int i;
12831 if (val == 0) /* XXX */
12832 return 0;
12834 for (i = 0; i < 25; i++)
12835 if ((val & (mask << i)) == val)
12836 return 1;
12838 return 0;
12841 /* Returns nonzero if the current function contains,
12842 or might contain a far jump. */
12843 static int
12844 thumb_far_jump_used_p (void)
12846 rtx insn;
12848 /* This test is only important for leaf functions. */
12849 /* assert (!leaf_function_p ()); */
12851 /* If we have already decided that far jumps may be used,
12852 do not bother checking again, and always return true even if
12853 it turns out that they are not being used. Once we have made
12854 the decision that far jumps are present (and that hence the link
12855 register will be pushed onto the stack) we cannot go back on it. */
12856 if (cfun->machine->far_jump_used)
12857 return 1;
12859 /* If this function is not being called from the prologue/epilogue
12860 generation code then it must be being called from the
12861 INITIAL_ELIMINATION_OFFSET macro. */
12862 if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12864 /* In this case we know that we are being asked about the elimination
12865 of the arg pointer register. If that register is not being used,
12866 then there are no arguments on the stack, and we do not have to
12867 worry that a far jump might force the prologue to push the link
12868 register, changing the stack offsets. In this case we can just
12869 return false, since the presence of far jumps in the function will
12870 not affect stack offsets.
12872 If the arg pointer is live (or if it was live, but has now been
12873 eliminated and so set to dead) then we do have to test to see if
12874 the function might contain a far jump. This test can lead to some
12875 false negatives, since before reload is completed, then length of
12876 branch instructions is not known, so gcc defaults to returning their
12877 longest length, which in turn sets the far jump attribute to true.
12879 A false negative will not result in bad code being generated, but it
12880 will result in a needless push and pop of the link register. We
12881 hope that this does not occur too often.
12883 If we need doubleword stack alignment this could affect the other
12884 elimination offsets so we can't risk getting it wrong. */
12885 if (regs_ever_live [ARG_POINTER_REGNUM])
12886 cfun->machine->arg_pointer_live = 1;
12887 else if (!cfun->machine->arg_pointer_live)
12888 return 0;
12891 /* Check to see if the function contains a branch
12892 insn with the far jump attribute set. */
12893 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12895 if (GET_CODE (insn) == JUMP_INSN
12896 /* Ignore tablejump patterns. */
12897 && GET_CODE (PATTERN (insn)) != ADDR_VEC
12898 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12899 && get_attr_far_jump (insn) == FAR_JUMP_YES
12902 /* Record the fact that we have decided that
12903 the function does use far jumps. */
12904 cfun->machine->far_jump_used = 1;
12905 return 1;
12909 return 0;
12912 /* Return nonzero if FUNC must be entered in ARM mode. */
12914 is_called_in_ARM_mode (tree func)
12916 if (TREE_CODE (func) != FUNCTION_DECL)
12917 abort ();
12919 /* Ignore the problem about functions whoes address is taken. */
12920 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12921 return TRUE;
12923 #ifdef ARM_PE
12924 return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12925 #else
12926 return FALSE;
12927 #endif
12930 /* The bits which aren't usefully expanded as rtl. */
12931 const char *
12932 thumb_unexpanded_epilogue (void)
12934 int regno;
12935 int live_regs_mask = 0;
12936 int high_regs_pushed = 0;
12937 int had_to_push_lr;
12938 rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
12940 if (return_used_this_function)
12941 return "";
12943 if (IS_NAKED (arm_current_func_type ()))
12944 return "";
12946 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12947 if (THUMB_REG_PUSHED_P (regno))
12948 live_regs_mask |= 1 << regno;
12950 for (regno = 8; regno < 13; regno++)
12951 if (THUMB_REG_PUSHED_P (regno))
12952 high_regs_pushed++;
12954 /* The prolog may have pushed some high registers to use as
12955 work registers. eg the testsuite file:
12956 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12957 compiles to produce:
12958 push {r4, r5, r6, r7, lr}
12959 mov r7, r9
12960 mov r6, r8
12961 push {r6, r7}
12962 as part of the prolog. We have to undo that pushing here. */
12964 if (high_regs_pushed)
12966 int mask = live_regs_mask;
12967 int next_hi_reg;
12968 int size;
12969 int mode;
12971 /* If we can deduce the registers used from the function's return value.
12972 This is more reliable that examining regs_ever_live[] because that
12973 will be set if the register is ever used in the function, not just if
12974 the register is used to hold a return value. */
12976 if (current_function_return_rtx != 0)
12977 mode = GET_MODE (current_function_return_rtx);
12978 else
12979 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12981 size = GET_MODE_SIZE (mode);
12983 /* Unless we are returning a type of size > 12 register r3 is
12984 available. */
12985 if (size < 13)
12986 mask |= 1 << 3;
12988 if (mask == 0)
12989 /* Oh dear! We have no low registers into which we can pop
12990 high registers! */
12991 internal_error
12992 ("no low registers available for popping high registers");
12994 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
12995 if (THUMB_REG_PUSHED_P (next_hi_reg))
12996 break;
12998 while (high_regs_pushed)
13000 /* Find lo register(s) into which the high register(s) can
13001 be popped. */
13002 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13004 if (mask & (1 << regno))
13005 high_regs_pushed--;
13006 if (high_regs_pushed == 0)
13007 break;
13010 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
13012 /* Pop the values into the low register(s). */
13013 thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
13015 /* Move the value(s) into the high registers. */
13016 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13018 if (mask & (1 << regno))
13020 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
13021 regno);
13023 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
13024 if (THUMB_REG_PUSHED_P (next_hi_reg))
13025 break;
13031 had_to_push_lr = (live_regs_mask || thumb_force_lr_save ());
13033 if (TARGET_BACKTRACE
13034 && ((live_regs_mask & 0xFF) == 0)
13035 && regs_ever_live [LAST_ARG_REGNUM] != 0)
13037 /* The stack backtrace structure creation code had to
13038 push R7 in order to get a work register, so we pop
13039 it now. */
13040 live_regs_mask |= (1 << LAST_LO_REGNUM);
13043 if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
13045 if (had_to_push_lr
13046 && !is_called_in_ARM_mode (current_function_decl)
13047 && !eh_ofs)
13048 live_regs_mask |= 1 << PC_REGNUM;
13050 /* Either no argument registers were pushed or a backtrace
13051 structure was created which includes an adjusted stack
13052 pointer, so just pop everything. */
13053 if (live_regs_mask)
13054 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13055 live_regs_mask);
13057 if (eh_ofs)
13058 thumb_exit (asm_out_file, 2, eh_ofs);
13059 /* We have either just popped the return address into the
13060 PC or it is was kept in LR for the entire function or
13061 it is still on the stack because we do not want to
13062 return by doing a pop {pc}. */
13063 else if ((live_regs_mask & (1 << PC_REGNUM)) == 0)
13064 thumb_exit (asm_out_file,
13065 (had_to_push_lr
13066 && is_called_in_ARM_mode (current_function_decl)) ?
13067 -1 : LR_REGNUM, NULL_RTX);
13069 else
13071 /* Pop everything but the return address. */
13072 live_regs_mask &= ~(1 << PC_REGNUM);
13074 if (live_regs_mask)
13075 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13076 live_regs_mask);
13078 if (had_to_push_lr)
13079 /* Get the return address into a temporary register. */
13080 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
13081 1 << LAST_ARG_REGNUM);
13083 /* Remove the argument registers that were pushed onto the stack. */
13084 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
13085 SP_REGNUM, SP_REGNUM,
13086 current_function_pretend_args_size);
13088 if (eh_ofs)
13089 thumb_exit (asm_out_file, 2, eh_ofs);
13090 else
13091 thumb_exit (asm_out_file,
13092 had_to_push_lr ? LAST_ARG_REGNUM : LR_REGNUM, NULL_RTX);
13095 return "";
13098 /* Functions to save and restore machine-specific function data. */
13099 static struct machine_function *
13100 arm_init_machine_status (void)
13102 struct machine_function *machine;
13103 machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
13105 #if ARM_FT_UNKNOWN != 0
13106 machine->func_type = ARM_FT_UNKNOWN;
13107 #endif
13108 return machine;
13111 /* Return an RTX indicating where the return address to the
13112 calling function can be found. */
13114 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
13116 if (count != 0)
13117 return NULL_RTX;
13119 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13122 /* Do anything needed before RTL is emitted for each function. */
13123 void
13124 arm_init_expanders (void)
13126 /* Arrange to initialize and mark the machine per-function status. */
13127 init_machine_status = arm_init_machine_status;
13129 /* This is to stop the combine pass optimizing away the alignment
13130 adjustment of va_arg. */
13131 /* ??? It is claimed that this should not be necessary. */
13132 if (cfun)
13133 mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
13137 /* Like arm_compute_initial_elimination offset. Simpler because
13138 THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer. */
13140 HOST_WIDE_INT
13141 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13143 arm_stack_offsets *offsets;
13145 offsets = arm_get_frame_offsets ();
13147 switch (from)
13149 case ARG_POINTER_REGNUM:
13150 switch (to)
13152 case STACK_POINTER_REGNUM:
13153 return offsets->outgoing_args - offsets->saved_args;
13155 case FRAME_POINTER_REGNUM:
13156 return offsets->soft_frame - offsets->saved_args;
13158 case THUMB_HARD_FRAME_POINTER_REGNUM:
13159 case ARM_HARD_FRAME_POINTER_REGNUM:
13160 return offsets->saved_regs - offsets->saved_args;
13162 default:
13163 abort();
13165 break;
13167 case FRAME_POINTER_REGNUM:
13168 switch (to)
13170 case STACK_POINTER_REGNUM:
13171 return offsets->outgoing_args - offsets->soft_frame;
13173 case THUMB_HARD_FRAME_POINTER_REGNUM:
13174 case ARM_HARD_FRAME_POINTER_REGNUM:
13175 return offsets->saved_regs - offsets->soft_frame;
13177 default:
13178 abort();
13180 break;
13182 default:
13183 abort ();
13188 /* Generate the rest of a function's prologue. */
13189 void
13190 thumb_expand_prologue (void)
13192 rtx insn, dwarf;
13194 HOST_WIDE_INT amount;
13195 arm_stack_offsets *offsets;
13196 unsigned long func_type;
13197 int regno;
13199 func_type = arm_current_func_type ();
13201 /* Naked functions don't have prologues. */
13202 if (IS_NAKED (func_type))
13203 return;
13205 if (IS_INTERRUPT (func_type))
13207 error ("interrupt Service Routines cannot be coded in Thumb mode");
13208 return;
13211 offsets = arm_get_frame_offsets ();
13213 if (frame_pointer_needed)
13215 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
13216 stack_pointer_rtx));
13217 RTX_FRAME_RELATED_P (insn) = 1;
13220 amount = offsets->outgoing_args - offsets->saved_regs;
13221 if (amount)
13223 if (amount < 512)
13225 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13226 GEN_INT (- amount)));
13227 RTX_FRAME_RELATED_P (insn) = 1;
13229 else
13231 rtx reg;
13233 /* The stack decrement is too big for an immediate value in a single
13234 insn. In theory we could issue multiple subtracts, but after
13235 three of them it becomes more space efficient to place the full
13236 value in the constant pool and load into a register. (Also the
13237 ARM debugger really likes to see only one stack decrement per
13238 function). So instead we look for a scratch register into which
13239 we can load the decrement, and then we subtract this from the
13240 stack pointer. Unfortunately on the thumb the only available
13241 scratch registers are the argument registers, and we cannot use
13242 these as they may hold arguments to the function. Instead we
13243 attempt to locate a call preserved register which is used by this
13244 function. If we can find one, then we know that it will have
13245 been pushed at the start of the prologue and so we can corrupt
13246 it now. */
13247 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13248 if (THUMB_REG_PUSHED_P (regno)
13249 && !(frame_pointer_needed
13250 && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13251 break;
13253 if (regno > LAST_LO_REGNUM) /* Very unlikely. */
13255 rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13257 /* Choose an arbitrary, non-argument low register. */
13258 reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13260 /* Save it by copying it into a high, scratch register. */
13261 emit_insn (gen_movsi (spare, reg));
13262 /* Add a USE to stop propagate_one_insn() from barfing. */
13263 emit_insn (gen_prologue_use (spare));
13265 /* Decrement the stack. */
13266 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13267 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13268 stack_pointer_rtx, reg));
13269 RTX_FRAME_RELATED_P (insn) = 1;
13270 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13271 plus_constant (stack_pointer_rtx,
13272 -amount));
13273 RTX_FRAME_RELATED_P (dwarf) = 1;
13274 REG_NOTES (insn)
13275 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13276 REG_NOTES (insn));
13278 /* Restore the low register's original value. */
13279 emit_insn (gen_movsi (reg, spare));
13281 /* Emit a USE of the restored scratch register, so that flow
13282 analysis will not consider the restore redundant. The
13283 register won't be used again in this function and isn't
13284 restored by the epilogue. */
13285 emit_insn (gen_prologue_use (reg));
13287 else
13289 reg = gen_rtx_REG (SImode, regno);
13291 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13293 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13294 stack_pointer_rtx, reg));
13295 RTX_FRAME_RELATED_P (insn) = 1;
13296 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13297 plus_constant (stack_pointer_rtx,
13298 -amount));
13299 RTX_FRAME_RELATED_P (dwarf) = 1;
13300 REG_NOTES (insn)
13301 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13302 REG_NOTES (insn));
13305 /* If the frame pointer is needed, emit a special barrier that
13306 will prevent the scheduler from moving stores to the frame
13307 before the stack adjustment. */
13308 if (frame_pointer_needed)
13309 emit_insn (gen_stack_tie (stack_pointer_rtx,
13310 hard_frame_pointer_rtx));
13313 if (current_function_profile || TARGET_NO_SCHED_PRO)
13314 emit_insn (gen_blockage ());
13316 cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13317 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13319 if (THUMB_REG_PUSHED_P (regno))
13321 cfun->machine->lr_save_eliminated = 0;
13322 break;
13326 /* If the link register is being kept alive, with the return address in it,
13327 then make sure that it does not get reused by the ce2 pass. */
13328 if (cfun->machine->lr_save_eliminated)
13329 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13332 void
13333 thumb_expand_epilogue (void)
13335 HOST_WIDE_INT amount;
13336 arm_stack_offsets *offsets;
13337 int regno;
13339 /* Naked functions don't have prologues. */
13340 if (IS_NAKED (arm_current_func_type ()))
13341 return;
13343 offsets = arm_get_frame_offsets ();
13344 amount = offsets->outgoing_args - offsets->saved_regs;
13346 if (frame_pointer_needed)
13347 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13348 else if (amount)
13350 if (amount < 512)
13351 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13352 GEN_INT (amount)));
13353 else
13355 /* r3 is always free in the epilogue. */
13356 rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13358 emit_insn (gen_movsi (reg, GEN_INT (amount)));
13359 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13363 /* Emit a USE (stack_pointer_rtx), so that
13364 the stack adjustment will not be deleted. */
13365 emit_insn (gen_prologue_use (stack_pointer_rtx));
13367 if (current_function_profile || TARGET_NO_SCHED_PRO)
13368 emit_insn (gen_blockage ());
13370 /* Emit a clobber for each insn that will be restored in the epilogue,
13371 so that flow2 will get register lifetimes correct. */
13372 for (regno = 0; regno < 13; regno++)
13373 if (regs_ever_live[regno] && !call_used_regs[regno])
13374 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13376 if (! regs_ever_live[LR_REGNUM])
13377 emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13380 static void
13381 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13383 int live_regs_mask = 0;
13384 int high_regs_pushed = 0;
13385 int cfa_offset = 0;
13386 int regno;
13388 if (IS_NAKED (arm_current_func_type ()))
13389 return;
13391 if (is_called_in_ARM_mode (current_function_decl))
13393 const char * name;
13395 if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
13396 abort ();
13397 if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
13398 abort ();
13399 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
13401 /* Generate code sequence to switch us into Thumb mode. */
13402 /* The .code 32 directive has already been emitted by
13403 ASM_DECLARE_FUNCTION_NAME. */
13404 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13405 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13407 /* Generate a label, so that the debugger will notice the
13408 change in instruction sets. This label is also used by
13409 the assembler to bypass the ARM code when this function
13410 is called from a Thumb encoded function elsewhere in the
13411 same file. Hence the definition of STUB_NAME here must
13412 agree with the definition in gas/config/tc-arm.c. */
13414 #define STUB_NAME ".real_start_of"
13416 fprintf (f, "\t.code\t16\n");
13417 #ifdef ARM_PE
13418 if (arm_dllexport_name_p (name))
13419 name = arm_strip_name_encoding (name);
13420 #endif
13421 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13422 fprintf (f, "\t.thumb_func\n");
13423 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13426 if (current_function_pretend_args_size)
13428 if (cfun->machine->uses_anonymous_args)
13430 int num_pushes;
13432 fprintf (f, "\tpush\t{");
13434 num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13436 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13437 regno <= LAST_ARG_REGNUM;
13438 regno++)
13439 asm_fprintf (f, "%r%s", regno,
13440 regno == LAST_ARG_REGNUM ? "" : ", ");
13442 fprintf (f, "}\n");
13444 else
13445 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13446 SP_REGNUM, SP_REGNUM,
13447 current_function_pretend_args_size);
13449 /* We don't need to record the stores for unwinding (would it
13450 help the debugger any if we did?), but record the change in
13451 the stack pointer. */
13452 if (dwarf2out_do_frame ())
13454 char *l = dwarf2out_cfi_label ();
13455 cfa_offset = cfa_offset + current_function_pretend_args_size;
13456 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13460 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13461 if (THUMB_REG_PUSHED_P (regno))
13462 live_regs_mask |= 1 << regno;
13464 if (live_regs_mask || thumb_force_lr_save ())
13465 live_regs_mask |= 1 << LR_REGNUM;
13467 if (TARGET_BACKTRACE)
13469 int offset;
13470 int work_register = 0;
13471 int wr;
13473 /* We have been asked to create a stack backtrace structure.
13474 The code looks like this:
13476 0 .align 2
13477 0 func:
13478 0 sub SP, #16 Reserve space for 4 registers.
13479 2 push {R7} Get a work register.
13480 4 add R7, SP, #20 Get the stack pointer before the push.
13481 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
13482 8 mov R7, PC Get hold of the start of this code plus 12.
13483 10 str R7, [SP, #16] Store it.
13484 12 mov R7, FP Get hold of the current frame pointer.
13485 14 str R7, [SP, #4] Store it.
13486 16 mov R7, LR Get hold of the current return address.
13487 18 str R7, [SP, #12] Store it.
13488 20 add R7, SP, #16 Point at the start of the backtrace structure.
13489 22 mov FP, R7 Put this value into the frame pointer. */
13491 if ((live_regs_mask & 0xFF) == 0)
13493 /* See if the a4 register is free. */
13495 if (regs_ever_live [LAST_ARG_REGNUM] == 0)
13496 work_register = LAST_ARG_REGNUM;
13497 else /* We must push a register of our own. */
13498 live_regs_mask |= (1 << LAST_LO_REGNUM);
13501 if (work_register == 0)
13503 /* Select a register from the list that will be pushed to
13504 use as our work register. */
13505 for (work_register = (LAST_LO_REGNUM + 1); work_register--;)
13506 if ((1 << work_register) & live_regs_mask)
13507 break;
13510 asm_fprintf
13511 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13512 SP_REGNUM, SP_REGNUM);
13514 if (dwarf2out_do_frame ())
13516 char *l = dwarf2out_cfi_label ();
13517 cfa_offset = cfa_offset + 16;
13518 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13521 if (live_regs_mask)
13522 thumb_pushpop (f, live_regs_mask, 1, &cfa_offset, live_regs_mask);
13524 for (offset = 0, wr = 1 << 15; wr != 0; wr >>= 1)
13525 if (wr & live_regs_mask)
13526 offset += 4;
13528 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13529 offset + 16 + current_function_pretend_args_size);
13531 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13532 offset + 4);
13534 /* Make sure that the instruction fetching the PC is in the right place
13535 to calculate "start of backtrace creation code + 12". */
13536 if (live_regs_mask)
13538 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13539 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13540 offset + 12);
13541 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13542 ARM_HARD_FRAME_POINTER_REGNUM);
13543 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13544 offset);
13546 else
13548 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13549 ARM_HARD_FRAME_POINTER_REGNUM);
13550 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13551 offset);
13552 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13553 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13554 offset + 12);
13557 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13558 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13559 offset + 8);
13560 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13561 offset + 12);
13562 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13563 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13565 else if (live_regs_mask)
13566 thumb_pushpop (f, live_regs_mask, 1, &cfa_offset, live_regs_mask);
13568 for (regno = 8; regno < 13; regno++)
13569 if (THUMB_REG_PUSHED_P (regno))
13570 high_regs_pushed++;
13572 if (high_regs_pushed)
13574 int pushable_regs = 0;
13575 int mask = live_regs_mask & 0xff;
13576 int next_hi_reg;
13578 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13579 if (THUMB_REG_PUSHED_P (next_hi_reg))
13580 break;
13582 pushable_regs = mask;
13584 if (pushable_regs == 0)
13586 /* Desperation time -- this probably will never happen. */
13587 if (THUMB_REG_PUSHED_P (LAST_ARG_REGNUM))
13588 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, LAST_ARG_REGNUM);
13589 mask = 1 << LAST_ARG_REGNUM;
13592 while (high_regs_pushed > 0)
13594 int real_regs_mask = 0;
13596 for (regno = LAST_LO_REGNUM; regno >= 0; regno--)
13598 if (mask & (1 << regno))
13600 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13602 high_regs_pushed--;
13603 real_regs_mask |= (1 << next_hi_reg);
13605 if (high_regs_pushed)
13607 for (next_hi_reg--; next_hi_reg > LAST_LO_REGNUM;
13608 next_hi_reg--)
13609 if (THUMB_REG_PUSHED_P (next_hi_reg))
13610 break;
13612 else
13614 mask &= ~((1 << regno) - 1);
13615 break;
13620 thumb_pushpop (f, mask, 1, &cfa_offset, real_regs_mask);
13623 if (pushable_regs == 0
13624 && (THUMB_REG_PUSHED_P (LAST_ARG_REGNUM)))
13625 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
13629 /* Handle the case of a double word load into a low register from
13630 a computed memory address. The computed address may involve a
13631 register which is overwritten by the load. */
13632 const char *
13633 thumb_load_double_from_address (rtx *operands)
13635 rtx addr;
13636 rtx base;
13637 rtx offset;
13638 rtx arg1;
13639 rtx arg2;
13641 if (GET_CODE (operands[0]) != REG)
13642 abort ();
13644 if (GET_CODE (operands[1]) != MEM)
13645 abort ();
13647 /* Get the memory address. */
13648 addr = XEXP (operands[1], 0);
13650 /* Work out how the memory address is computed. */
13651 switch (GET_CODE (addr))
13653 case REG:
13654 operands[2] = gen_rtx_MEM (SImode,
13655 plus_constant (XEXP (operands[1], 0), 4));
13657 if (REGNO (operands[0]) == REGNO (addr))
13659 output_asm_insn ("ldr\t%H0, %2", operands);
13660 output_asm_insn ("ldr\t%0, %1", operands);
13662 else
13664 output_asm_insn ("ldr\t%0, %1", operands);
13665 output_asm_insn ("ldr\t%H0, %2", operands);
13667 break;
13669 case CONST:
13670 /* Compute <address> + 4 for the high order load. */
13671 operands[2] = gen_rtx_MEM (SImode,
13672 plus_constant (XEXP (operands[1], 0), 4));
13674 output_asm_insn ("ldr\t%0, %1", operands);
13675 output_asm_insn ("ldr\t%H0, %2", operands);
13676 break;
13678 case PLUS:
13679 arg1 = XEXP (addr, 0);
13680 arg2 = XEXP (addr, 1);
13682 if (CONSTANT_P (arg1))
13683 base = arg2, offset = arg1;
13684 else
13685 base = arg1, offset = arg2;
13687 if (GET_CODE (base) != REG)
13688 abort ();
13690 /* Catch the case of <address> = <reg> + <reg> */
13691 if (GET_CODE (offset) == REG)
13693 int reg_offset = REGNO (offset);
13694 int reg_base = REGNO (base);
13695 int reg_dest = REGNO (operands[0]);
13697 /* Add the base and offset registers together into the
13698 higher destination register. */
13699 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13700 reg_dest + 1, reg_base, reg_offset);
13702 /* Load the lower destination register from the address in
13703 the higher destination register. */
13704 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13705 reg_dest, reg_dest + 1);
13707 /* Load the higher destination register from its own address
13708 plus 4. */
13709 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13710 reg_dest + 1, reg_dest + 1);
13712 else
13714 /* Compute <address> + 4 for the high order load. */
13715 operands[2] = gen_rtx_MEM (SImode,
13716 plus_constant (XEXP (operands[1], 0), 4));
13718 /* If the computed address is held in the low order register
13719 then load the high order register first, otherwise always
13720 load the low order register first. */
13721 if (REGNO (operands[0]) == REGNO (base))
13723 output_asm_insn ("ldr\t%H0, %2", operands);
13724 output_asm_insn ("ldr\t%0, %1", operands);
13726 else
13728 output_asm_insn ("ldr\t%0, %1", operands);
13729 output_asm_insn ("ldr\t%H0, %2", operands);
13732 break;
13734 case LABEL_REF:
13735 /* With no registers to worry about we can just load the value
13736 directly. */
13737 operands[2] = gen_rtx_MEM (SImode,
13738 plus_constant (XEXP (operands[1], 0), 4));
13740 output_asm_insn ("ldr\t%H0, %2", operands);
13741 output_asm_insn ("ldr\t%0, %1", operands);
13742 break;
13744 default:
13745 abort ();
13746 break;
13749 return "";
13752 const char *
13753 thumb_output_move_mem_multiple (int n, rtx *operands)
13755 rtx tmp;
13757 switch (n)
13759 case 2:
13760 if (REGNO (operands[4]) > REGNO (operands[5]))
13762 tmp = operands[4];
13763 operands[4] = operands[5];
13764 operands[5] = tmp;
13766 output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13767 output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13768 break;
13770 case 3:
13771 if (REGNO (operands[4]) > REGNO (operands[5]))
13773 tmp = operands[4];
13774 operands[4] = operands[5];
13775 operands[5] = tmp;
13777 if (REGNO (operands[5]) > REGNO (operands[6]))
13779 tmp = operands[5];
13780 operands[5] = operands[6];
13781 operands[6] = tmp;
13783 if (REGNO (operands[4]) > REGNO (operands[5]))
13785 tmp = operands[4];
13786 operands[4] = operands[5];
13787 operands[5] = tmp;
13790 output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13791 output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13792 break;
13794 default:
13795 abort ();
13798 return "";
13801 /* Routines for generating rtl. */
13802 void
13803 thumb_expand_movmemqi (rtx *operands)
13805 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13806 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13807 HOST_WIDE_INT len = INTVAL (operands[2]);
13808 HOST_WIDE_INT offset = 0;
13810 while (len >= 12)
13812 emit_insn (gen_movmem12b (out, in, out, in));
13813 len -= 12;
13816 if (len >= 8)
13818 emit_insn (gen_movmem8b (out, in, out, in));
13819 len -= 8;
13822 if (len >= 4)
13824 rtx reg = gen_reg_rtx (SImode);
13825 emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13826 emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13827 len -= 4;
13828 offset += 4;
13831 if (len >= 2)
13833 rtx reg = gen_reg_rtx (HImode);
13834 emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
13835 plus_constant (in, offset))));
13836 emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13837 reg));
13838 len -= 2;
13839 offset += 2;
13842 if (len)
13844 rtx reg = gen_reg_rtx (QImode);
13845 emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13846 plus_constant (in, offset))));
13847 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13848 reg));
13853 thumb_cmp_operand (rtx op, enum machine_mode mode)
13855 return ((GET_CODE (op) == CONST_INT
13856 && INTVAL (op) < 256
13857 && INTVAL (op) >= 0)
13858 || s_register_operand (op, mode));
13862 thumb_cmpneg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
13864 return (GET_CODE (op) == CONST_INT
13865 && INTVAL (op) < 0
13866 && INTVAL (op) > -256);
13869 /* Return TRUE if a result can be stored in OP without clobbering the
13870 condition code register. Prior to reload we only accept a
13871 register. After reload we have to be able to handle memory as
13872 well, since a pseudo may not get a hard reg and reload cannot
13873 handle output-reloads on jump insns.
13875 We could possibly handle mem before reload as well, but that might
13876 complicate things with the need to handle increment
13877 side-effects. */
13880 thumb_cbrch_target_operand (rtx op, enum machine_mode mode)
13882 return (s_register_operand (op, mode)
13883 || ((reload_in_progress || reload_completed)
13884 && memory_operand (op, mode)));
13887 /* Handle storing a half-word to memory during reload. */
13888 void
13889 thumb_reload_out_hi (rtx *operands)
13891 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13894 /* Handle reading a half-word from memory during reload. */
13895 void
13896 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13898 abort ();
13901 /* Return the length of a function name prefix
13902 that starts with the character 'c'. */
13903 static int
13904 arm_get_strip_length (int c)
13906 switch (c)
13908 ARM_NAME_ENCODING_LENGTHS
13909 default: return 0;
13913 /* Return a pointer to a function's name with any
13914 and all prefix encodings stripped from it. */
13915 const char *
13916 arm_strip_name_encoding (const char *name)
13918 int skip;
13920 while ((skip = arm_get_strip_length (* name)))
13921 name += skip;
13923 return name;
13926 /* If there is a '*' anywhere in the name's prefix, then
13927 emit the stripped name verbatim, otherwise prepend an
13928 underscore if leading underscores are being used. */
13929 void
13930 arm_asm_output_labelref (FILE *stream, const char *name)
13932 int skip;
13933 int verbatim = 0;
13935 while ((skip = arm_get_strip_length (* name)))
13937 verbatim |= (*name == '*');
13938 name += skip;
13941 if (verbatim)
13942 fputs (name, stream);
13943 else
13944 asm_fprintf (stream, "%U%s", name);
13947 rtx aof_pic_label;
13949 #ifdef AOF_ASSEMBLER
13950 /* Special functions only needed when producing AOF syntax assembler. */
13952 struct pic_chain
13954 struct pic_chain * next;
13955 const char * symname;
13958 static struct pic_chain * aof_pic_chain = NULL;
13961 aof_pic_entry (rtx x)
13963 struct pic_chain ** chainp;
13964 int offset;
13966 if (aof_pic_label == NULL_RTX)
13968 aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
13971 for (offset = 0, chainp = &aof_pic_chain; *chainp;
13972 offset += 4, chainp = &(*chainp)->next)
13973 if ((*chainp)->symname == XSTR (x, 0))
13974 return plus_constant (aof_pic_label, offset);
13976 *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13977 (*chainp)->next = NULL;
13978 (*chainp)->symname = XSTR (x, 0);
13979 return plus_constant (aof_pic_label, offset);
13982 void
13983 aof_dump_pic_table (FILE *f)
13985 struct pic_chain * chain;
13987 if (aof_pic_chain == NULL)
13988 return;
13990 asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13991 PIC_OFFSET_TABLE_REGNUM,
13992 PIC_OFFSET_TABLE_REGNUM);
13993 fputs ("|x$adcons|\n", f);
13995 for (chain = aof_pic_chain; chain; chain = chain->next)
13997 fputs ("\tDCD\t", f);
13998 assemble_name (f, chain->symname);
13999 fputs ("\n", f);
14003 int arm_text_section_count = 1;
14005 char *
14006 aof_text_section (void )
14008 static char buf[100];
14009 sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
14010 arm_text_section_count++);
14011 if (flag_pic)
14012 strcat (buf, ", PIC, REENTRANT");
14013 return buf;
14016 static int arm_data_section_count = 1;
14018 char *
14019 aof_data_section (void)
14021 static char buf[100];
14022 sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
14023 return buf;
14026 /* The AOF assembler is religiously strict about declarations of
14027 imported and exported symbols, so that it is impossible to declare
14028 a function as imported near the beginning of the file, and then to
14029 export it later on. It is, however, possible to delay the decision
14030 until all the functions in the file have been compiled. To get
14031 around this, we maintain a list of the imports and exports, and
14032 delete from it any that are subsequently defined. At the end of
14033 compilation we spit the remainder of the list out before the END
14034 directive. */
14036 struct import
14038 struct import * next;
14039 const char * name;
14042 static struct import * imports_list = NULL;
14044 void
14045 aof_add_import (const char *name)
14047 struct import * new;
14049 for (new = imports_list; new; new = new->next)
14050 if (new->name == name)
14051 return;
14053 new = (struct import *) xmalloc (sizeof (struct import));
14054 new->next = imports_list;
14055 imports_list = new;
14056 new->name = name;
14059 void
14060 aof_delete_import (const char *name)
14062 struct import ** old;
14064 for (old = &imports_list; *old; old = & (*old)->next)
14066 if ((*old)->name == name)
14068 *old = (*old)->next;
14069 return;
14074 int arm_main_function = 0;
14076 static void
14077 aof_dump_imports (FILE *f)
14079 /* The AOF assembler needs this to cause the startup code to be extracted
14080 from the library. Brining in __main causes the whole thing to work
14081 automagically. */
14082 if (arm_main_function)
14084 text_section ();
14085 fputs ("\tIMPORT __main\n", f);
14086 fputs ("\tDCD __main\n", f);
14089 /* Now dump the remaining imports. */
14090 while (imports_list)
14092 fprintf (f, "\tIMPORT\t");
14093 assemble_name (f, imports_list->name);
14094 fputc ('\n', f);
14095 imports_list = imports_list->next;
14099 static void
14100 aof_globalize_label (FILE *stream, const char *name)
14102 default_globalize_label (stream, name);
14103 if (! strcmp (name, "main"))
14104 arm_main_function = 1;
14107 static void
14108 aof_file_start (void)
14110 fputs ("__r0\tRN\t0\n", asm_out_file);
14111 fputs ("__a1\tRN\t0\n", asm_out_file);
14112 fputs ("__a2\tRN\t1\n", asm_out_file);
14113 fputs ("__a3\tRN\t2\n", asm_out_file);
14114 fputs ("__a4\tRN\t3\n", asm_out_file);
14115 fputs ("__v1\tRN\t4\n", asm_out_file);
14116 fputs ("__v2\tRN\t5\n", asm_out_file);
14117 fputs ("__v3\tRN\t6\n", asm_out_file);
14118 fputs ("__v4\tRN\t7\n", asm_out_file);
14119 fputs ("__v5\tRN\t8\n", asm_out_file);
14120 fputs ("__v6\tRN\t9\n", asm_out_file);
14121 fputs ("__sl\tRN\t10\n", asm_out_file);
14122 fputs ("__fp\tRN\t11\n", asm_out_file);
14123 fputs ("__ip\tRN\t12\n", asm_out_file);
14124 fputs ("__sp\tRN\t13\n", asm_out_file);
14125 fputs ("__lr\tRN\t14\n", asm_out_file);
14126 fputs ("__pc\tRN\t15\n", asm_out_file);
14127 fputs ("__f0\tFN\t0\n", asm_out_file);
14128 fputs ("__f1\tFN\t1\n", asm_out_file);
14129 fputs ("__f2\tFN\t2\n", asm_out_file);
14130 fputs ("__f3\tFN\t3\n", asm_out_file);
14131 fputs ("__f4\tFN\t4\n", asm_out_file);
14132 fputs ("__f5\tFN\t5\n", asm_out_file);
14133 fputs ("__f6\tFN\t6\n", asm_out_file);
14134 fputs ("__f7\tFN\t7\n", asm_out_file);
14135 text_section ();
14138 static void
14139 aof_file_end (void)
14141 if (flag_pic)
14142 aof_dump_pic_table (asm_out_file);
14143 aof_dump_imports (asm_out_file);
14144 fputs ("\tEND\n", asm_out_file);
14146 #endif /* AOF_ASSEMBLER */
14148 #ifdef OBJECT_FORMAT_ELF
14149 /* Switch to an arbitrary section NAME with attributes as specified
14150 by FLAGS. ALIGN specifies any known alignment requirements for
14151 the section; 0 if the default should be used.
14153 Differs from the default elf version only in the prefix character
14154 used before the section type. */
14156 static void
14157 arm_elf_asm_named_section (const char *name, unsigned int flags)
14159 char flagchars[10], *f = flagchars;
14161 if (! named_section_first_declaration (name))
14163 fprintf (asm_out_file, "\t.section\t%s\n", name);
14164 return;
14167 if (!(flags & SECTION_DEBUG))
14168 *f++ = 'a';
14169 if (flags & SECTION_WRITE)
14170 *f++ = 'w';
14171 if (flags & SECTION_CODE)
14172 *f++ = 'x';
14173 if (flags & SECTION_SMALL)
14174 *f++ = 's';
14175 if (flags & SECTION_MERGE)
14176 *f++ = 'M';
14177 if (flags & SECTION_STRINGS)
14178 *f++ = 'S';
14179 if (flags & SECTION_TLS)
14180 *f++ = 'T';
14181 *f = '\0';
14183 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
14185 if (!(flags & SECTION_NOTYPE))
14187 const char *type;
14189 if (flags & SECTION_BSS)
14190 type = "nobits";
14191 else
14192 type = "progbits";
14194 fprintf (asm_out_file, ",%%%s", type);
14196 if (flags & SECTION_ENTSIZE)
14197 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
14200 putc ('\n', asm_out_file);
14202 #endif
14204 #ifndef ARM_PE
14205 /* Symbols in the text segment can be accessed without indirecting via the
14206 constant pool; it may take an extra binary operation, but this is still
14207 faster than indirecting via memory. Don't do this when not optimizing,
14208 since we won't be calculating al of the offsets necessary to do this
14209 simplification. */
14211 static void
14212 arm_encode_section_info (tree decl, rtx rtl, int first)
14214 /* This doesn't work with AOF syntax, since the string table may be in
14215 a different AREA. */
14216 #ifndef AOF_ASSEMBLER
14217 if (optimize > 0 && TREE_CONSTANT (decl))
14218 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14219 #endif
14221 /* If we are referencing a function that is weak then encode a long call
14222 flag in the function name, otherwise if the function is static or
14223 or known to be defined in this file then encode a short call flag. */
14224 if (first && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
14226 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14227 arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14228 else if (! TREE_PUBLIC (decl))
14229 arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14232 #endif /* !ARM_PE */
14234 static void
14235 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14237 if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14238 && !strcmp (prefix, "L"))
14240 arm_ccfsm_state = 0;
14241 arm_target_insn = NULL;
14243 default_internal_label (stream, prefix, labelno);
14246 /* Output code to add DELTA to the first argument, and then jump
14247 to FUNCTION. Used for C++ multiple inheritance. */
14248 static void
14249 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14250 HOST_WIDE_INT delta,
14251 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14252 tree function)
14254 static int thunk_label = 0;
14255 char label[256];
14256 int mi_delta = delta;
14257 const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14258 int shift = 0;
14259 int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14260 ? 1 : 0);
14261 if (mi_delta < 0)
14262 mi_delta = - mi_delta;
14263 if (TARGET_THUMB)
14265 int labelno = thunk_label++;
14266 ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14267 fputs ("\tldr\tr12, ", file);
14268 assemble_name (file, label);
14269 fputc ('\n', file);
14271 while (mi_delta != 0)
14273 if ((mi_delta & (3 << shift)) == 0)
14274 shift += 2;
14275 else
14277 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14278 mi_op, this_regno, this_regno,
14279 mi_delta & (0xff << shift));
14280 mi_delta &= ~(0xff << shift);
14281 shift += 8;
14284 if (TARGET_THUMB)
14286 fprintf (file, "\tbx\tr12\n");
14287 ASM_OUTPUT_ALIGN (file, 2);
14288 assemble_name (file, label);
14289 fputs (":\n", file);
14290 assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14292 else
14294 fputs ("\tb\t", file);
14295 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14296 if (NEED_PLT_RELOC)
14297 fputs ("(PLT)", file);
14298 fputc ('\n', file);
14303 arm_emit_vector_const (FILE *file, rtx x)
14305 int i;
14306 const char * pattern;
14308 if (GET_CODE (x) != CONST_VECTOR)
14309 abort ();
14311 switch (GET_MODE (x))
14313 case V2SImode: pattern = "%08x"; break;
14314 case V4HImode: pattern = "%04x"; break;
14315 case V8QImode: pattern = "%02x"; break;
14316 default: abort ();
14319 fprintf (file, "0x");
14320 for (i = CONST_VECTOR_NUNITS (x); i--;)
14322 rtx element;
14324 element = CONST_VECTOR_ELT (x, i);
14325 fprintf (file, pattern, INTVAL (element));
14328 return 1;
14331 const char *
14332 arm_output_load_gr (rtx *operands)
14334 rtx reg;
14335 rtx offset;
14336 rtx wcgr;
14337 rtx sum;
14339 if (GET_CODE (operands [1]) != MEM
14340 || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14341 || GET_CODE (reg = XEXP (sum, 0)) != REG
14342 || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14343 || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14344 return "wldrw%?\t%0, %1";
14346 /* Fix up an out-of-range load of a GR register. */
14347 output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14348 wcgr = operands[0];
14349 operands[0] = reg;
14350 output_asm_insn ("ldr%?\t%0, %1", operands);
14352 operands[0] = wcgr;
14353 operands[1] = reg;
14354 output_asm_insn ("tmcr%?\t%0, %1", operands);
14355 output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14357 return "";
14360 static rtx
14361 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14362 int incoming ATTRIBUTE_UNUSED)
14364 #if 0
14365 /* FIXME: The ARM backend has special code to handle structure
14366 returns, and will reserve its own hidden first argument. So
14367 if this macro is enabled a *second* hidden argument will be
14368 reserved, which will break binary compatibility with old
14369 toolchains and also thunk handling. One day this should be
14370 fixed. */
14371 return 0;
14372 #else
14373 /* Register in which address to store a structure value
14374 is passed to a function. */
14375 return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14376 #endif
14379 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14381 On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14382 named arg and all anonymous args onto the stack.
14383 XXX I know the prologue shouldn't be pushing registers, but it is faster
14384 that way. */
14386 static void
14387 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14388 enum machine_mode mode ATTRIBUTE_UNUSED,
14389 tree type ATTRIBUTE_UNUSED,
14390 int *pretend_size,
14391 int second_time ATTRIBUTE_UNUSED)
14393 cfun->machine->uses_anonymous_args = 1;
14394 if (cum->nregs < NUM_ARG_REGS)
14395 *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14398 /* Return nonzero if the CONSUMER instruction (a store) does not need
14399 PRODUCER's value to calculate the address. */
14402 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14404 rtx value = PATTERN (producer);
14405 rtx addr = PATTERN (consumer);
14407 if (GET_CODE (value) == COND_EXEC)
14408 value = COND_EXEC_CODE (value);
14409 if (GET_CODE (value) == PARALLEL)
14410 value = XVECEXP (value, 0, 0);
14411 value = XEXP (value, 0);
14412 if (GET_CODE (addr) == COND_EXEC)
14413 addr = COND_EXEC_CODE (addr);
14414 if (GET_CODE (addr) == PARALLEL)
14415 addr = XVECEXP (addr, 0, 0);
14416 addr = XEXP (addr, 0);
14418 return !reg_overlap_mentioned_p (value, addr);
14421 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14422 have an early register shift value or amount dependency on the
14423 result of PRODUCER. */
14426 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14428 rtx value = PATTERN (producer);
14429 rtx op = PATTERN (consumer);
14430 rtx early_op;
14432 if (GET_CODE (value) == COND_EXEC)
14433 value = COND_EXEC_CODE (value);
14434 if (GET_CODE (value) == PARALLEL)
14435 value = XVECEXP (value, 0, 0);
14436 value = XEXP (value, 0);
14437 if (GET_CODE (op) == COND_EXEC)
14438 op = COND_EXEC_CODE (op);
14439 if (GET_CODE (op) == PARALLEL)
14440 op = XVECEXP (op, 0, 0);
14441 op = XEXP (op, 1);
14443 early_op = XEXP (op, 0);
14444 /* This is either an actual independent shift, or a shift applied to
14445 the first operand of another operation. We want the whole shift
14446 operation. */
14447 if (GET_CODE (early_op) == REG)
14448 early_op = op;
14450 return !reg_overlap_mentioned_p (value, early_op);
14453 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14454 have an early register shift value dependency on the result of
14455 PRODUCER. */
14458 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14460 rtx value = PATTERN (producer);
14461 rtx op = PATTERN (consumer);
14462 rtx early_op;
14464 if (GET_CODE (value) == COND_EXEC)
14465 value = COND_EXEC_CODE (value);
14466 if (GET_CODE (value) == PARALLEL)
14467 value = XVECEXP (value, 0, 0);
14468 value = XEXP (value, 0);
14469 if (GET_CODE (op) == COND_EXEC)
14470 op = COND_EXEC_CODE (op);
14471 if (GET_CODE (op) == PARALLEL)
14472 op = XVECEXP (op, 0, 0);
14473 op = XEXP (op, 1);
14475 early_op = XEXP (op, 0);
14477 /* This is either an actual independent shift, or a shift applied to
14478 the first operand of another operation. We want the value being
14479 shifted, in either case. */
14480 if (GET_CODE (early_op) != REG)
14481 early_op = XEXP (early_op, 0);
14483 return !reg_overlap_mentioned_p (value, early_op);
14486 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14487 have an early register mult dependency on the result of
14488 PRODUCER. */
14491 arm_no_early_mul_dep (rtx producer, rtx consumer)
14493 rtx value = PATTERN (producer);
14494 rtx op = PATTERN (consumer);
14496 if (GET_CODE (value) == COND_EXEC)
14497 value = COND_EXEC_CODE (value);
14498 if (GET_CODE (value) == PARALLEL)
14499 value = XVECEXP (value, 0, 0);
14500 value = XEXP (value, 0);
14501 if (GET_CODE (op) == COND_EXEC)
14502 op = COND_EXEC_CODE (op);
14503 if (GET_CODE (op) == PARALLEL)
14504 op = XVECEXP (op, 0, 0);
14505 op = XEXP (op, 1);
14507 return (GET_CODE (op) == PLUS
14508 && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14512 /* We can't rely on the caller doing the proper promotion when
14513 using APCS or ATPCS. */
14515 static bool
14516 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14518 return !TARGET_AAPCS_BASED;
14522 /* AAPCS based ABIs use short enums by default. */
14524 static bool
14525 arm_default_short_enums (void)
14527 return TARGET_AAPCS_BASED;
14531 /* AAPCS requires that anonymous bitfields affect structure alignment. */
14533 static bool
14534 arm_align_anon_bitfield (void)
14536 return TARGET_AAPCS_BASED;
14540 /* The generic C++ ABI says 64-bit (long long). The EABI says 32-bit. */
14542 static tree
14543 arm_cxx_guard_type (void)
14545 return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14549 /* The EABI says test the least significan bit of a guard variable. */
14551 static bool
14552 arm_cxx_guard_mask_bit (void)
14554 return TARGET_AAPCS_BASED;
14558 /* The EABI specifies that all array cookies are 8 bytes long. */
14560 static tree
14561 arm_get_cookie_size (tree type)
14563 tree size;
14565 if (!TARGET_AAPCS_BASED)
14566 return default_cxx_get_cookie_size (type);
14568 size = build_int_2 (8, 0);
14569 TREE_TYPE (size) = sizetype;
14570 return size;
14574 /* The EABI says that array cookies should also contain the element size. */
14576 static bool
14577 arm_cookie_has_size (void)
14579 return TARGET_AAPCS_BASED;