* arm.c (arm_needs_doubleword_align): Use mode alignment.
[official-gcc.git] / gcc / config / arm / arm.c
blobf4a7ca77c85fa08945dd517ddfa7c1ca3742b67d
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"
55 /* Forward definitions of types. */
56 typedef struct minipool_node Mnode;
57 typedef struct minipool_fixup Mfix;
59 const struct attribute_spec arm_attribute_table[];
61 /* Forward function declarations. */
62 static arm_stack_offsets *arm_get_frame_offsets (void);
63 static void arm_add_gc_roots (void);
64 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
65 HOST_WIDE_INT, rtx, rtx, int, int);
66 static unsigned bit_count (unsigned long);
67 static int arm_address_register_rtx_p (rtx, int);
68 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
69 static int thumb_base_register_rtx_p (rtx, enum machine_mode, int);
70 inline static int thumb_index_register_rtx_p (rtx, int);
71 static int thumb_far_jump_used_p (void);
72 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
73 static rtx emit_multi_reg_push (int);
74 static rtx emit_sfm (int, int);
75 #ifndef AOF_ASSEMBLER
76 static bool arm_assemble_integer (rtx, unsigned int, int);
77 #endif
78 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
79 static arm_cc get_arm_condition_code (rtx);
80 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
81 static rtx is_jump_table (rtx);
82 static const char *output_multi_immediate (rtx *, const char *, const char *,
83 int, HOST_WIDE_INT);
84 static void print_multi_reg (FILE *, const char *, int, int);
85 static const char *shift_op (rtx, HOST_WIDE_INT *);
86 static struct machine_function *arm_init_machine_status (void);
87 static int number_of_first_bit_set (int);
88 static void replace_symbols_in_block (tree, rtx, rtx);
89 static void thumb_exit (FILE *, int, rtx);
90 static void thumb_pushpop (FILE *, int, int, int *, int);
91 static rtx is_jump_table (rtx);
92 static HOST_WIDE_INT get_jump_table_size (rtx);
93 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
94 static Mnode *add_minipool_forward_ref (Mfix *);
95 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
96 static Mnode *add_minipool_backward_ref (Mfix *);
97 static void assign_minipool_offsets (Mfix *);
98 static void arm_print_value (FILE *, rtx);
99 static void dump_minipool (rtx);
100 static int arm_barrier_cost (rtx);
101 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
102 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
103 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
104 rtx);
105 static void arm_reorg (void);
106 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
107 static int current_file_function_operand (rtx);
108 static unsigned long arm_compute_save_reg0_reg12_mask (void);
109 static unsigned long arm_compute_save_reg_mask (void);
110 static unsigned long arm_isr_value (tree);
111 static unsigned long arm_compute_func_type (void);
112 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
113 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
114 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
115 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
116 static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
117 static int arm_comp_type_attributes (tree, tree);
118 static void arm_set_default_type_attributes (tree);
119 static int arm_adjust_cost (rtx, rtx, rtx, int);
120 static int arm_use_dfa_pipeline_interface (void);
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_promote_prototypes (tree);
164 /* Initialize the GCC target structure. */
165 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
166 #undef TARGET_MERGE_DECL_ATTRIBUTES
167 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
168 #endif
170 #undef TARGET_ATTRIBUTE_TABLE
171 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
173 #ifdef AOF_ASSEMBLER
174 #undef TARGET_ASM_BYTE_OP
175 #define TARGET_ASM_BYTE_OP "\tDCB\t"
176 #undef TARGET_ASM_ALIGNED_HI_OP
177 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
178 #undef TARGET_ASM_ALIGNED_SI_OP
179 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
180 #undef TARGET_ASM_GLOBALIZE_LABEL
181 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
182 #undef TARGET_ASM_FILE_START
183 #define TARGET_ASM_FILE_START aof_file_start
184 #undef TARGET_ASM_FILE_END
185 #define TARGET_ASM_FILE_END aof_file_end
186 #else
187 #undef TARGET_ASM_ALIGNED_SI_OP
188 #define TARGET_ASM_ALIGNED_SI_OP NULL
189 #undef TARGET_ASM_INTEGER
190 #define TARGET_ASM_INTEGER arm_assemble_integer
191 #endif
193 #undef TARGET_ASM_FUNCTION_PROLOGUE
194 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
196 #undef TARGET_ASM_FUNCTION_EPILOGUE
197 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
199 #undef TARGET_COMP_TYPE_ATTRIBUTES
200 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
202 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
203 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
205 #undef TARGET_SCHED_ADJUST_COST
206 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
208 #undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE
209 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE arm_use_dfa_pipeline_interface
211 #undef TARGET_ENCODE_SECTION_INFO
212 #ifdef ARM_PE
213 #define TARGET_ENCODE_SECTION_INFO arm_pe_encode_section_info
214 #else
215 #define TARGET_ENCODE_SECTION_INFO arm_encode_section_info
216 #endif
218 #undef TARGET_STRIP_NAME_ENCODING
219 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
221 #undef TARGET_ASM_INTERNAL_LABEL
222 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
224 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
225 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
227 #undef TARGET_ASM_OUTPUT_MI_THUNK
228 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
229 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
230 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
232 /* This will be overridden in arm_override_options. */
233 #undef TARGET_RTX_COSTS
234 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
235 #undef TARGET_ADDRESS_COST
236 #define TARGET_ADDRESS_COST arm_address_cost
238 #undef TARGET_MACHINE_DEPENDENT_REORG
239 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
241 #undef TARGET_INIT_BUILTINS
242 #define TARGET_INIT_BUILTINS arm_init_builtins
243 #undef TARGET_EXPAND_BUILTIN
244 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
246 #undef TARGET_PROMOTE_FUNCTION_ARGS
247 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
248 #undef TARGET_PROMOTE_FUNCTION_RETURN
249 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
250 #undef TARGET_PROMOTE_PROTOTYPES
251 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
253 #undef TARGET_STRUCT_VALUE_RTX
254 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
256 #undef TARGET_SETUP_INCOMING_VARARGS
257 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
259 struct gcc_target targetm = TARGET_INITIALIZER;
261 /* Obstack for minipool constant handling. */
262 static struct obstack minipool_obstack;
263 static char * minipool_startobj;
265 /* The maximum number of insns skipped which
266 will be conditionalised if possible. */
267 static int max_insns_skipped = 5;
269 extern FILE * asm_out_file;
271 /* True if we are currently building a constant table. */
272 int making_const_table;
274 /* Define the information needed to generate branch insns. This is
275 stored from the compare operation. */
276 rtx arm_compare_op0, arm_compare_op1;
278 /* The processor for which instructions should be scheduled. */
279 enum processor_type arm_tune = arm_none;
281 /* Which floating point model to use. */
282 enum arm_fp_model arm_fp_model;
284 /* Which floating point hardware is available. */
285 enum fputype arm_fpu_arch;
287 /* Which floating point hardware to schedule for. */
288 enum fputype arm_fpu_tune;
290 /* Whether to use floating point hardware. */
291 enum float_abi_type arm_float_abi;
293 /* Which ABI to use. */
294 enum arm_abi_type arm_abi;
296 /* What program mode is the cpu running in? 26-bit mode or 32-bit mode. */
297 enum prog_mode_type arm_prgmode;
299 /* Set by the -mfpu=... option. */
300 const char * target_fpu_name = NULL;
302 /* Set by the -mfpe=... option. */
303 const char * target_fpe_name = NULL;
305 /* Set by the -mfloat-abi=... option. */
306 const char * target_float_abi_name = NULL;
308 /* Set by the -mabi=... option. */
309 const char * target_abi_name = NULL;
311 /* Used to parse -mstructure_size_boundary command line option. */
312 const char * structure_size_string = NULL;
313 int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
315 /* Bit values used to identify processor capabilities. */
316 #define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
317 #define FL_ARCH3M (1 << 1) /* Extended multiply */
318 #define FL_MODE26 (1 << 2) /* 26-bit mode support */
319 #define FL_MODE32 (1 << 3) /* 32-bit mode support */
320 #define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
321 #define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
322 #define FL_THUMB (1 << 6) /* Thumb aware */
323 #define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
324 #define FL_STRONG (1 << 8) /* StrongARM */
325 #define FL_ARCH5E (1 << 9) /* DSP extensions to v5 */
326 #define FL_XSCALE (1 << 10) /* XScale */
327 #define FL_CIRRUS (1 << 11) /* Cirrus/DSP. */
328 #define FL_ARCH6 (1 << 12) /* Architecture rel 6. Adds
329 media instructions. */
330 #define FL_VFPV2 (1 << 13) /* Vector Floating Point V2. */
332 #define FL_IWMMXT (1 << 29) /* XScale v2 or "Intel Wireless MMX technology". */
334 /* The bits in this mask specify which
335 instructions we are allowed to generate. */
336 static unsigned long insn_flags = 0;
338 /* The bits in this mask specify which instruction scheduling options should
339 be used. */
340 static unsigned long tune_flags = 0;
342 /* The following are used in the arm.md file as equivalents to bits
343 in the above two flag variables. */
345 /* Nonzero if this chip supports the ARM Architecture 3M extensions. */
346 int arm_arch3m = 0;
348 /* Nonzero if this chip supports the ARM Architecture 4 extensions. */
349 int arm_arch4 = 0;
351 /* Nonzero if this chip supports the ARM Architecture 5 extensions. */
352 int arm_arch5 = 0;
354 /* Nonzero if this chip supports the ARM Architecture 5E extensions. */
355 int arm_arch5e = 0;
357 /* Nonzero if this chip supports the ARM Architecture 6 extensions. */
358 int arm_arch6 = 0;
360 /* Nonzero if this chip can benefit from load scheduling. */
361 int arm_ld_sched = 0;
363 /* Nonzero if this chip is a StrongARM. */
364 int arm_is_strong = 0;
366 /* Nonzero if this chip supports Intel Wireless MMX technology. */
367 int arm_arch_iwmmxt = 0;
369 /* Nonzero if this chip is an XScale. */
370 int arm_arch_xscale = 0;
372 /* Nonzero if tuning for XScale */
373 int arm_tune_xscale = 0;
375 /* Nonzero if this chip is an ARM6 or an ARM7. */
376 int arm_is_6_or_7 = 0;
378 /* Nonzero if generating Thumb instructions. */
379 int thumb_code = 0;
381 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
382 must report the mode of the memory reference from PRINT_OPERAND to
383 PRINT_OPERAND_ADDRESS. */
384 enum machine_mode output_memory_reference_mode;
386 /* The register number to be used for the PIC offset register. */
387 const char * arm_pic_register_string = NULL;
388 int arm_pic_register = INVALID_REGNUM;
390 /* Set to 1 when a return insn is output, this means that the epilogue
391 is not needed. */
392 int return_used_this_function;
394 /* Set to 1 after arm_reorg has started. Reset to start at the start of
395 the next function. */
396 static int after_arm_reorg = 0;
398 /* The maximum number of insns to be used when loading a constant. */
399 static int arm_constant_limit = 3;
401 /* For an explanation of these variables, see final_prescan_insn below. */
402 int arm_ccfsm_state;
403 enum arm_cond_code arm_current_cc;
404 rtx arm_target_insn;
405 int arm_target_label;
407 /* The condition codes of the ARM, and the inverse function. */
408 static const char * const arm_condition_codes[] =
410 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
411 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
414 #define streq(string1, string2) (strcmp (string1, string2) == 0)
416 /* Initialization code. */
418 struct processors
420 const char *const name;
421 enum processor_type core;
422 const unsigned long flags;
423 bool (* rtx_costs) (rtx, int, int, int *);
426 /* Not all of these give usefully different compilation alternatives,
427 but there is no simple way of generalizing them. */
428 static const struct processors all_cores[] =
430 /* ARM Cores */
431 #define ARM_CORE(NAME, FLAGS, COSTS) \
432 {#NAME, arm_none, FLAGS, arm_##COSTS##_rtx_costs},
433 #include "arm-cores.def"
434 #undef ARM_CORE
435 {NULL, arm_none, 0, NULL}
438 static const struct processors all_architectures[] =
440 /* ARM Architectures */
441 /* We don't specify rtx_costs here as it will be figured out
442 from the core. */
444 { "armv2", arm2, FL_CO_PROC | FL_MODE26 , NULL},
445 { "armv2a", arm2, FL_CO_PROC | FL_MODE26 , NULL},
446 { "armv3", arm6, FL_CO_PROC | FL_MODE26 | FL_MODE32 , NULL},
447 { "armv3m", arm7m, FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_ARCH3M , NULL},
448 { "armv4", arm7tdmi, FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_ARCH3M | FL_ARCH4 , NULL},
449 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
450 implementations that support it, so we will leave it out for now. */
451 { "armv4t", arm7tdmi, FL_CO_PROC | FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB , NULL},
452 { "armv5", arm10tdmi, FL_CO_PROC | FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_ARCH5 , NULL},
453 { "armv5t", arm10tdmi, FL_CO_PROC | FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_ARCH5 , NULL},
454 { "armv5te", arm1026ejs, FL_CO_PROC | FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_ARCH5 | FL_ARCH5E , NULL},
455 { "armv6", arm1136js, FL_CO_PROC | FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_ARCH5 | FL_ARCH5E | FL_ARCH6 , NULL},
456 { "armv6j", arm1136js, FL_CO_PROC | FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_ARCH5 | FL_ARCH5E | FL_ARCH6 , NULL},
457 { "ep9312", ep9312, FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_LDSCHED | FL_CIRRUS , NULL},
458 {"iwmmxt", iwmmxt, FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_LDSCHED | FL_STRONG | FL_ARCH5 | FL_ARCH5E | FL_XSCALE | FL_IWMMXT , NULL},
459 { NULL, arm_none, 0 , NULL}
462 /* This is a magic structure. The 'string' field is magically filled in
463 with a pointer to the value specified by the user on the command line
464 assuming that the user has specified such a value. */
466 struct arm_cpu_select arm_select[] =
468 /* string name processors */
469 { NULL, "-mcpu=", all_cores },
470 { NULL, "-march=", all_architectures },
471 { NULL, "-mtune=", all_cores }
474 struct fpu_desc
476 const char * name;
477 enum fputype fpu;
481 /* Available values for for -mfpu=. */
483 static const struct fpu_desc all_fpus[] =
485 {"fpa", FPUTYPE_FPA},
486 {"fpe2", FPUTYPE_FPA_EMU2},
487 {"fpe3", FPUTYPE_FPA_EMU2},
488 {"maverick", FPUTYPE_MAVERICK},
489 {"vfp", FPUTYPE_VFP}
493 /* Floating point models used by the different hardware.
494 See fputype in arm.h. */
496 static const enum fputype fp_model_for_fpu[] =
498 /* No FP hardware. */
499 ARM_FP_MODEL_UNKNOWN, /* FPUTYPE_NONE */
500 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA */
501 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU2 */
502 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU3 */
503 ARM_FP_MODEL_MAVERICK, /* FPUTYPE_MAVERICK */
504 ARM_FP_MODEL_VFP /* FPUTYPE_VFP */
508 struct float_abi
510 const char * name;
511 enum float_abi_type abi_type;
515 /* Available values for -mfloat-abi=. */
517 static const struct float_abi all_float_abis[] =
519 {"soft", ARM_FLOAT_ABI_SOFT},
520 {"softfp", ARM_FLOAT_ABI_SOFTFP},
521 {"hard", ARM_FLOAT_ABI_HARD}
525 struct abi_name
527 const char *name;
528 enum arm_abi_type abi_type;
532 /* Available values for -mabi=. */
534 static const struct abi_name arm_all_abis[] =
536 {"apcs-gnu", ARM_ABI_APCS},
537 {"atpcs", ARM_ABI_ATPCS},
538 {"aapcs", ARM_ABI_AAPCS},
539 {"iwmmxt", ARM_ABI_IWMMXT}
542 /* Return the number of bits set in VALUE. */
543 static unsigned
544 bit_count (unsigned long value)
546 unsigned long count = 0;
548 while (value)
550 count++;
551 value &= value - 1; /* Clear the least-significant set bit. */
554 return count;
557 /* Fix up any incompatible options that the user has specified.
558 This has now turned into a maze. */
559 void
560 arm_override_options (void)
562 unsigned i;
564 /* Set up the flags based on the cpu/architecture selected by the user. */
565 for (i = ARRAY_SIZE (arm_select); i--;)
567 struct arm_cpu_select * ptr = arm_select + i;
569 if (ptr->string != NULL && ptr->string[0] != '\0')
571 const struct processors * sel;
573 for (sel = ptr->processors; sel->name != NULL; sel++)
574 if (streq (ptr->string, sel->name))
576 /* Determine the processor core for which we should
577 tune code-generation. */
578 if (/* -mcpu= is a sensible default. */
579 i == 0
580 /* If -march= is used, and -mcpu= has not been used,
581 assume that we should tune for a representative
582 CPU from that architecture. */
583 || i == 1
584 /* -mtune= overrides -mcpu= and -march=. */
585 || i == 2)
586 arm_tune = (enum processor_type) (sel - ptr->processors);
588 if (i != 2)
590 /* If we have been given an architecture and a processor
591 make sure that they are compatible. We only generate
592 a warning though, and we prefer the CPU over the
593 architecture. */
594 if (insn_flags != 0 && (insn_flags ^ sel->flags))
595 warning ("switch -mcpu=%s conflicts with -march= switch",
596 ptr->string);
598 insn_flags = sel->flags;
601 break;
604 if (sel->name == NULL)
605 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
609 /* If the user did not specify a processor, choose one for them. */
610 if (insn_flags == 0)
612 const struct processors * sel;
613 unsigned int sought;
614 static const struct cpu_default
616 const int cpu;
617 const char *const name;
619 cpu_defaults[] =
621 { TARGET_CPU_arm2, "arm2" },
622 { TARGET_CPU_arm6, "arm6" },
623 { TARGET_CPU_arm610, "arm610" },
624 { TARGET_CPU_arm710, "arm710" },
625 { TARGET_CPU_arm7m, "arm7m" },
626 { TARGET_CPU_arm7500fe, "arm7500fe" },
627 { TARGET_CPU_arm7tdmi, "arm7tdmi" },
628 { TARGET_CPU_arm8, "arm8" },
629 { TARGET_CPU_arm810, "arm810" },
630 { TARGET_CPU_arm9, "arm9" },
631 { TARGET_CPU_strongarm, "strongarm" },
632 { TARGET_CPU_xscale, "xscale" },
633 { TARGET_CPU_ep9312, "ep9312" },
634 { TARGET_CPU_iwmmxt, "iwmmxt" },
635 { TARGET_CPU_arm926ejs, "arm926ejs" },
636 { TARGET_CPU_arm1026ejs, "arm1026ejs" },
637 { TARGET_CPU_arm1136js, "arm1136js" },
638 { TARGET_CPU_arm1136jfs, "arm1136jfs" },
639 { TARGET_CPU_generic, "arm" },
640 { 0, 0 }
642 const struct cpu_default * def;
644 /* Find the default. */
645 for (def = cpu_defaults; def->name; def++)
646 if (def->cpu == TARGET_CPU_DEFAULT)
647 break;
649 /* Make sure we found the default CPU. */
650 if (def->name == NULL)
651 abort ();
653 /* Find the default CPU's flags. */
654 for (sel = all_cores; sel->name != NULL; sel++)
655 if (streq (def->name, sel->name))
656 break;
658 if (sel->name == NULL)
659 abort ();
661 insn_flags = sel->flags;
663 /* Now check to see if the user has specified some command line
664 switch that require certain abilities from the cpu. */
665 sought = 0;
667 if (TARGET_INTERWORK || TARGET_THUMB)
669 sought |= (FL_THUMB | FL_MODE32);
671 /* Force apcs-32 to be used for interworking. */
672 target_flags |= ARM_FLAG_APCS_32;
674 /* There are no ARM processors that support both APCS-26 and
675 interworking. Therefore we force FL_MODE26 to be removed
676 from insn_flags here (if it was set), so that the search
677 below will always be able to find a compatible processor. */
678 insn_flags &= ~FL_MODE26;
680 else if (!TARGET_APCS_32)
681 sought |= FL_MODE26;
683 if (sought != 0 && ((sought & insn_flags) != sought))
685 /* Try to locate a CPU type that supports all of the abilities
686 of the default CPU, plus the extra abilities requested by
687 the user. */
688 for (sel = all_cores; sel->name != NULL; sel++)
689 if ((sel->flags & sought) == (sought | insn_flags))
690 break;
692 if (sel->name == NULL)
694 unsigned current_bit_count = 0;
695 const struct processors * best_fit = NULL;
697 /* Ideally we would like to issue an error message here
698 saying that it was not possible to find a CPU compatible
699 with the default CPU, but which also supports the command
700 line options specified by the programmer, and so they
701 ought to use the -mcpu=<name> command line option to
702 override the default CPU type.
704 Unfortunately this does not work with multilibing. We
705 need to be able to support multilibs for -mapcs-26 and for
706 -mthumb-interwork and there is no CPU that can support both
707 options. Instead if we cannot find a cpu that has both the
708 characteristics of the default cpu and the given command line
709 options we scan the array again looking for a best match. */
710 for (sel = all_cores; sel->name != NULL; sel++)
711 if ((sel->flags & sought) == sought)
713 unsigned count;
715 count = bit_count (sel->flags & insn_flags);
717 if (count >= current_bit_count)
719 best_fit = sel;
720 current_bit_count = count;
724 if (best_fit == NULL)
725 abort ();
726 else
727 sel = best_fit;
730 insn_flags = sel->flags;
732 if (arm_tune == arm_none)
733 arm_tune = (enum processor_type) (sel - all_cores);
736 /* The processor for which we should tune should now have been
737 chosen. */
738 if (arm_tune == arm_none)
739 abort ();
741 tune_flags = all_cores[(int)arm_tune].flags;
742 targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
744 /* Make sure that the processor choice does not conflict with any of the
745 other command line choices. */
746 if (TARGET_APCS_32 && !(insn_flags & FL_MODE32))
748 /* If APCS-32 was not the default then it must have been set by the
749 user, so issue a warning message. If the user has specified
750 "-mapcs-32 -mcpu=arm2" then we loose here. */
751 if ((TARGET_DEFAULT & ARM_FLAG_APCS_32) == 0)
752 warning ("target CPU does not support APCS-32" );
753 target_flags &= ~ARM_FLAG_APCS_32;
755 else if (!TARGET_APCS_32 && !(insn_flags & FL_MODE26))
757 warning ("target CPU does not support APCS-26" );
758 target_flags |= ARM_FLAG_APCS_32;
761 if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
763 warning ("target CPU does not support interworking" );
764 target_flags &= ~ARM_FLAG_INTERWORK;
767 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
769 warning ("target CPU does not support THUMB instructions");
770 target_flags &= ~ARM_FLAG_THUMB;
773 if (TARGET_APCS_FRAME && TARGET_THUMB)
775 /* warning ("ignoring -mapcs-frame because -mthumb was used"); */
776 target_flags &= ~ARM_FLAG_APCS_FRAME;
779 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
780 from here where no function is being compiled currently. */
781 if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
782 && TARGET_ARM)
783 warning ("enabling backtrace support is only meaningful when compiling for the Thumb");
785 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
786 warning ("enabling callee interworking support is only meaningful when compiling for the Thumb");
788 if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
789 warning ("enabling caller interworking support is only meaningful when compiling for the Thumb");
791 /* If interworking is enabled then APCS-32 must be selected as well. */
792 if (TARGET_INTERWORK)
794 if (!TARGET_APCS_32)
795 warning ("interworking forces APCS-32 to be used" );
796 target_flags |= ARM_FLAG_APCS_32;
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_arch5 = (insn_flags & FL_ARCH5) != 0;
834 arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
835 arm_arch6 = (insn_flags & FL_ARCH6) != 0;
836 arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
838 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
839 arm_is_strong = (tune_flags & FL_STRONG) != 0;
840 thumb_code = (TARGET_ARM == 0);
841 arm_is_6_or_7 = (((tune_flags & (FL_MODE26 | FL_MODE32))
842 && !(tune_flags & FL_ARCH4))) != 0;
843 arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
844 arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
846 if (target_abi_name)
848 for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
850 if (streq (arm_all_abis[i].name, target_abi_name))
852 arm_abi = arm_all_abis[i].abi_type;
853 break;
856 if (i == ARRAY_SIZE (arm_all_abis))
857 error ("invalid ABI option: -mabi=%s", target_abi_name);
859 else
860 arm_abi = ARM_DEFAULT_ABI;
862 if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
863 error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
865 if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
866 error ("iwmmxt abi requires an iwmmxt capable cpu");
868 arm_fp_model = ARM_FP_MODEL_UNKNOWN;
869 if (target_fpu_name == NULL && target_fpe_name != NULL)
871 if (streq (target_fpe_name, "2"))
872 target_fpu_name = "fpe2";
873 else if (streq (target_fpe_name, "3"))
874 target_fpu_name = "fpe3";
875 else
876 error ("invalid floating point emulation option: -mfpe=%s",
877 target_fpe_name);
879 if (target_fpu_name != NULL)
881 /* The user specified a FPU. */
882 for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
884 if (streq (all_fpus[i].name, target_fpu_name))
886 arm_fpu_arch = all_fpus[i].fpu;
887 arm_fpu_tune = arm_fpu_arch;
888 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
889 break;
892 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
893 error ("invalid floating point option: -mfpu=%s", target_fpu_name);
895 else
897 #ifdef FPUTYPE_DEFAULT
898 /* Use the default is it is specified for this platform. */
899 arm_fpu_arch = FPUTYPE_DEFAULT;
900 arm_fpu_tune = FPUTYPE_DEFAULT;
901 #else
902 /* Pick one based on CPU type. */
903 if ((insn_flags & FL_VFP) != 0)
904 arm_fpu_arch = FPUTYPE_VFP;
905 else if (insn_flags & FL_CIRRUS)
906 arm_fpu_arch = FPUTYPE_MAVERICK;
907 else
908 arm_fpu_arch = FPUTYPE_FPA_EMU2;
909 #endif
910 if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
911 arm_fpu_tune = FPUTYPE_FPA;
912 else
913 arm_fpu_tune = arm_fpu_arch;
914 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
915 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
916 abort ();
919 if (target_float_abi_name != NULL)
921 /* The user specified a FP ABI. */
922 for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
924 if (streq (all_float_abis[i].name, target_float_abi_name))
926 arm_float_abi = all_float_abis[i].abi_type;
927 break;
930 if (i == ARRAY_SIZE (all_float_abis))
931 error ("invalid floating point abi: -mfloat-abi=%s",
932 target_float_abi_name);
934 else
936 /* Use soft-float target flag. */
937 if (target_flags & ARM_FLAG_SOFT_FLOAT)
938 arm_float_abi = ARM_FLOAT_ABI_SOFT;
939 else
940 arm_float_abi = ARM_FLOAT_ABI_HARD;
943 if (arm_float_abi == ARM_FLOAT_ABI_SOFTFP)
944 sorry ("-mfloat-abi=softfp");
945 /* If soft-float is specified then don't use FPU. */
946 if (TARGET_SOFT_FLOAT)
947 arm_fpu_arch = FPUTYPE_NONE;
949 /* For arm2/3 there is no need to do any scheduling if there is only
950 a floating point emulator, or we are doing software floating-point. */
951 if ((TARGET_SOFT_FLOAT
952 || arm_fpu_tune == FPUTYPE_FPA_EMU2
953 || arm_fpu_tune == FPUTYPE_FPA_EMU3)
954 && (tune_flags & FL_MODE32) == 0)
955 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
957 arm_prgmode = TARGET_APCS_32 ? PROG_MODE_PROG32 : PROG_MODE_PROG26;
959 /* Override the default structure alignment for AAPCS ABI. */
960 if (arm_abi == ARM_ABI_AAPCS)
961 arm_structure_size_boundary = 8;
963 if (structure_size_string != NULL)
965 int size = strtol (structure_size_string, NULL, 0);
967 if (size == 8 || size == 32
968 || (ARM_DOUBLEWORD_ALIGN && size == 64))
969 arm_structure_size_boundary = size;
970 else
971 warning ("structure size boundary can only be set to %s",
972 ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
975 if (arm_pic_register_string != NULL)
977 int pic_register = decode_reg_name (arm_pic_register_string);
979 if (!flag_pic)
980 warning ("-mpic-register= is useless without -fpic");
982 /* Prevent the user from choosing an obviously stupid PIC register. */
983 else if (pic_register < 0 || call_used_regs[pic_register]
984 || pic_register == HARD_FRAME_POINTER_REGNUM
985 || pic_register == STACK_POINTER_REGNUM
986 || pic_register >= PC_REGNUM)
987 error ("unable to use '%s' for PIC register", arm_pic_register_string);
988 else
989 arm_pic_register = pic_register;
992 if (TARGET_THUMB && flag_schedule_insns)
994 /* Don't warn since it's on by default in -O2. */
995 flag_schedule_insns = 0;
998 if (optimize_size)
1000 /* There's some dispute as to whether this should be 1 or 2. However,
1001 experiments seem to show that in pathological cases a setting of
1002 1 degrades less severely than a setting of 2. This could change if
1003 other parts of the compiler change their behavior. */
1004 arm_constant_limit = 1;
1006 /* If optimizing for size, bump the number of instructions that we
1007 are prepared to conditionally execute (even on a StrongARM). */
1008 max_insns_skipped = 6;
1010 else
1012 /* For processors with load scheduling, it never costs more than
1013 2 cycles to load a constant, and the load scheduler may well
1014 reduce that to 1. */
1015 if (tune_flags & FL_LDSCHED)
1016 arm_constant_limit = 1;
1018 /* On XScale the longer latency of a load makes it more difficult
1019 to achieve a good schedule, so it's faster to synthesize
1020 constants that can be done in two insns. */
1021 if (arm_tune_xscale)
1022 arm_constant_limit = 2;
1024 /* StrongARM has early execution of branches, so a sequence
1025 that is worth skipping is shorter. */
1026 if (arm_is_strong)
1027 max_insns_skipped = 3;
1030 /* Register global variables with the garbage collector. */
1031 arm_add_gc_roots ();
1034 static void
1035 arm_add_gc_roots (void)
1037 gcc_obstack_init(&minipool_obstack);
1038 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1041 /* A table of known ARM exception types.
1042 For use with the interrupt function attribute. */
1044 typedef struct
1046 const char *const arg;
1047 const unsigned long return_value;
1049 isr_attribute_arg;
1051 static const isr_attribute_arg isr_attribute_args [] =
1053 { "IRQ", ARM_FT_ISR },
1054 { "irq", ARM_FT_ISR },
1055 { "FIQ", ARM_FT_FIQ },
1056 { "fiq", ARM_FT_FIQ },
1057 { "ABORT", ARM_FT_ISR },
1058 { "abort", ARM_FT_ISR },
1059 { "ABORT", ARM_FT_ISR },
1060 { "abort", ARM_FT_ISR },
1061 { "UNDEF", ARM_FT_EXCEPTION },
1062 { "undef", ARM_FT_EXCEPTION },
1063 { "SWI", ARM_FT_EXCEPTION },
1064 { "swi", ARM_FT_EXCEPTION },
1065 { NULL, ARM_FT_NORMAL }
1068 /* Returns the (interrupt) function type of the current
1069 function, or ARM_FT_UNKNOWN if the type cannot be determined. */
1071 static unsigned long
1072 arm_isr_value (tree argument)
1074 const isr_attribute_arg * ptr;
1075 const char * arg;
1077 /* No argument - default to IRQ. */
1078 if (argument == NULL_TREE)
1079 return ARM_FT_ISR;
1081 /* Get the value of the argument. */
1082 if (TREE_VALUE (argument) == NULL_TREE
1083 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1084 return ARM_FT_UNKNOWN;
1086 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1088 /* Check it against the list of known arguments. */
1089 for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1090 if (streq (arg, ptr->arg))
1091 return ptr->return_value;
1093 /* An unrecognized interrupt type. */
1094 return ARM_FT_UNKNOWN;
1097 /* Computes the type of the current function. */
1099 static unsigned long
1100 arm_compute_func_type (void)
1102 unsigned long type = ARM_FT_UNKNOWN;
1103 tree a;
1104 tree attr;
1106 if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
1107 abort ();
1109 /* Decide if the current function is volatile. Such functions
1110 never return, and many memory cycles can be saved by not storing
1111 register values that will never be needed again. This optimization
1112 was added to speed up context switching in a kernel application. */
1113 if (optimize > 0
1114 && current_function_nothrow
1115 && TREE_THIS_VOLATILE (current_function_decl))
1116 type |= ARM_FT_VOLATILE;
1118 if (current_function_needs_context)
1119 type |= ARM_FT_NESTED;
1121 attr = DECL_ATTRIBUTES (current_function_decl);
1123 a = lookup_attribute ("naked", attr);
1124 if (a != NULL_TREE)
1125 type |= ARM_FT_NAKED;
1127 if (cfun->machine->eh_epilogue_sp_ofs != NULL_RTX)
1128 type |= ARM_FT_EXCEPTION_HANDLER;
1129 else
1131 a = lookup_attribute ("isr", attr);
1132 if (a == NULL_TREE)
1133 a = lookup_attribute ("interrupt", attr);
1135 if (a == NULL_TREE)
1136 type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1137 else
1138 type |= arm_isr_value (TREE_VALUE (a));
1141 return type;
1144 /* Returns the type of the current function. */
1146 unsigned long
1147 arm_current_func_type (void)
1149 if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1150 cfun->machine->func_type = arm_compute_func_type ();
1152 return cfun->machine->func_type;
1155 /* Return 1 if it is possible to return using a single instruction.
1156 If SIBLING is non-null, this is a test for a return before a sibling
1157 call. SIBLING is the call insn, so we can examine its register usage. */
1160 use_return_insn (int iscond, rtx sibling)
1162 int regno;
1163 unsigned int func_type;
1164 unsigned long saved_int_regs;
1165 unsigned HOST_WIDE_INT stack_adjust;
1166 arm_stack_offsets *offsets;
1168 /* Never use a return instruction before reload has run. */
1169 if (!reload_completed)
1170 return 0;
1172 func_type = arm_current_func_type ();
1174 /* Naked functions and volatile functions need special
1175 consideration. */
1176 if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1177 return 0;
1179 /* So do interrupt functions that use the frame pointer. */
1180 if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1181 return 0;
1183 offsets = arm_get_frame_offsets ();
1184 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1186 /* As do variadic functions. */
1187 if (current_function_pretend_args_size
1188 || cfun->machine->uses_anonymous_args
1189 /* Or if the function calls __builtin_eh_return () */
1190 || ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
1191 /* Or if the function calls alloca */
1192 || current_function_calls_alloca
1193 /* Or if there is a stack adjustment. However, if the stack pointer
1194 is saved on the stack, we can use a pre-incrementing stack load. */
1195 || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1196 return 0;
1198 saved_int_regs = arm_compute_save_reg_mask ();
1200 /* Unfortunately, the insn
1202 ldmib sp, {..., sp, ...}
1204 triggers a bug on most SA-110 based devices, such that the stack
1205 pointer won't be correctly restored if the instruction takes a
1206 page fault. We work around this problem by popping r3 along with
1207 the other registers, since that is never slower than executing
1208 another instruction.
1210 We test for !arm_arch5 here, because code for any architecture
1211 less than this could potentially be run on one of the buggy
1212 chips. */
1213 if (stack_adjust == 4 && !arm_arch5)
1215 /* Validate that r3 is a call-clobbered register (always true in
1216 the default abi) ... */
1217 if (!call_used_regs[3])
1218 return 0;
1220 /* ... that it isn't being used for a return value (always true
1221 until we implement return-in-regs), or for a tail-call
1222 argument ... */
1223 if (sibling)
1225 if (GET_CODE (sibling) != CALL_INSN)
1226 abort ();
1228 if (find_regno_fusage (sibling, USE, 3))
1229 return 0;
1232 /* ... and that there are no call-saved registers in r0-r2
1233 (always true in the default ABI). */
1234 if (saved_int_regs & 0x7)
1235 return 0;
1238 /* Can't be done if interworking with Thumb, and any registers have been
1239 stacked. */
1240 if (TARGET_INTERWORK && saved_int_regs != 0)
1241 return 0;
1243 /* On StrongARM, conditional returns are expensive if they aren't
1244 taken and multiple registers have been stacked. */
1245 if (iscond && arm_is_strong)
1247 /* Conditional return when just the LR is stored is a simple
1248 conditional-load instruction, that's not expensive. */
1249 if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1250 return 0;
1252 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1253 return 0;
1256 /* If there are saved registers but the LR isn't saved, then we need
1257 two instructions for the return. */
1258 if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1259 return 0;
1261 /* Can't be done if any of the FPA regs are pushed,
1262 since this also requires an insn. */
1263 if (TARGET_HARD_FLOAT && TARGET_FPA)
1264 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1265 if (regs_ever_live[regno] && !call_used_regs[regno])
1266 return 0;
1268 /* Likewise VFP regs. */
1269 if (TARGET_HARD_FLOAT && TARGET_VFP)
1270 for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1271 if (regs_ever_live[regno] && !call_used_regs[regno])
1272 return 0;
1274 if (TARGET_REALLY_IWMMXT)
1275 for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1276 if (regs_ever_live[regno] && ! call_used_regs [regno])
1277 return 0;
1279 return 1;
1282 /* Return TRUE if int I is a valid immediate ARM constant. */
1285 const_ok_for_arm (HOST_WIDE_INT i)
1287 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1289 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1290 be all zero, or all one. */
1291 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1292 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1293 != ((~(unsigned HOST_WIDE_INT) 0)
1294 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1295 return FALSE;
1297 /* Fast return for 0 and powers of 2 */
1298 if ((i & (i - 1)) == 0)
1299 return TRUE;
1303 if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1304 return TRUE;
1305 mask =
1306 (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1307 >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1309 while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1311 return FALSE;
1314 /* Return true if I is a valid constant for the operation CODE. */
1315 static int
1316 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1318 if (const_ok_for_arm (i))
1319 return 1;
1321 switch (code)
1323 case PLUS:
1324 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1326 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
1327 case XOR:
1328 case IOR:
1329 return 0;
1331 case AND:
1332 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1334 default:
1335 abort ();
1339 /* Emit a sequence of insns to handle a large constant.
1340 CODE is the code of the operation required, it can be any of SET, PLUS,
1341 IOR, AND, XOR, MINUS;
1342 MODE is the mode in which the operation is being performed;
1343 VAL is the integer to operate on;
1344 SOURCE is the other operand (a register, or a null-pointer for SET);
1345 SUBTARGETS means it is safe to create scratch registers if that will
1346 either produce a simpler sequence, or we will want to cse the values.
1347 Return value is the number of insns emitted. */
1350 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1351 HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1353 rtx cond;
1355 if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1356 cond = COND_EXEC_TEST (PATTERN (insn));
1357 else
1358 cond = NULL_RTX;
1360 if (subtargets || code == SET
1361 || (GET_CODE (target) == REG && GET_CODE (source) == REG
1362 && REGNO (target) != REGNO (source)))
1364 /* After arm_reorg has been called, we can't fix up expensive
1365 constants by pushing them into memory so we must synthesize
1366 them in-line, regardless of the cost. This is only likely to
1367 be more costly on chips that have load delay slots and we are
1368 compiling without running the scheduler (so no splitting
1369 occurred before the final instruction emission).
1371 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1373 if (!after_arm_reorg
1374 && !cond
1375 && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1376 1, 0)
1377 > arm_constant_limit + (code != SET)))
1379 if (code == SET)
1381 /* Currently SET is the only monadic value for CODE, all
1382 the rest are diadic. */
1383 emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1384 return 1;
1386 else
1388 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1390 emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1391 /* For MINUS, the value is subtracted from, since we never
1392 have subtraction of a constant. */
1393 if (code == MINUS)
1394 emit_insn (gen_rtx_SET (VOIDmode, target,
1395 gen_rtx_MINUS (mode, temp, source)));
1396 else
1397 emit_insn (gen_rtx_SET (VOIDmode, target,
1398 gen_rtx_fmt_ee (code, mode, source, temp)));
1399 return 2;
1404 return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1408 static int
1409 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1411 HOST_WIDE_INT temp1;
1412 int num_insns = 0;
1415 int end;
1417 if (i <= 0)
1418 i += 32;
1419 if (remainder & (3 << (i - 2)))
1421 end = i - 8;
1422 if (end < 0)
1423 end += 32;
1424 temp1 = remainder & ((0x0ff << end)
1425 | ((i < end) ? (0xff >> (32 - end)) : 0));
1426 remainder &= ~temp1;
1427 num_insns++;
1428 i -= 6;
1430 i -= 2;
1431 } while (remainder);
1432 return num_insns;
1435 /* Emit an instruction with the indicated PATTERN. If COND is
1436 non-NULL, conditionalize the execution of the instruction on COND
1437 being true. */
1439 static void
1440 emit_constant_insn (rtx cond, rtx pattern)
1442 if (cond)
1443 pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1444 emit_insn (pattern);
1447 /* As above, but extra parameter GENERATE which, if clear, suppresses
1448 RTL generation. */
1450 static int
1451 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1452 HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1453 int generate)
1455 int can_invert = 0;
1456 int can_negate = 0;
1457 int can_negate_initial = 0;
1458 int can_shift = 0;
1459 int i;
1460 int num_bits_set = 0;
1461 int set_sign_bit_copies = 0;
1462 int clear_sign_bit_copies = 0;
1463 int clear_zero_bit_copies = 0;
1464 int set_zero_bit_copies = 0;
1465 int insns = 0;
1466 unsigned HOST_WIDE_INT temp1, temp2;
1467 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1469 /* Find out which operations are safe for a given CODE. Also do a quick
1470 check for degenerate cases; these can occur when DImode operations
1471 are split. */
1472 switch (code)
1474 case SET:
1475 can_invert = 1;
1476 can_shift = 1;
1477 can_negate = 1;
1478 break;
1480 case PLUS:
1481 can_negate = 1;
1482 can_negate_initial = 1;
1483 break;
1485 case IOR:
1486 if (remainder == 0xffffffff)
1488 if (generate)
1489 emit_constant_insn (cond,
1490 gen_rtx_SET (VOIDmode, target,
1491 GEN_INT (ARM_SIGN_EXTEND (val))));
1492 return 1;
1494 if (remainder == 0)
1496 if (reload_completed && rtx_equal_p (target, source))
1497 return 0;
1498 if (generate)
1499 emit_constant_insn (cond,
1500 gen_rtx_SET (VOIDmode, target, source));
1501 return 1;
1503 break;
1505 case AND:
1506 if (remainder == 0)
1508 if (generate)
1509 emit_constant_insn (cond,
1510 gen_rtx_SET (VOIDmode, target, const0_rtx));
1511 return 1;
1513 if (remainder == 0xffffffff)
1515 if (reload_completed && rtx_equal_p (target, source))
1516 return 0;
1517 if (generate)
1518 emit_constant_insn (cond,
1519 gen_rtx_SET (VOIDmode, target, source));
1520 return 1;
1522 can_invert = 1;
1523 break;
1525 case XOR:
1526 if (remainder == 0)
1528 if (reload_completed && rtx_equal_p (target, source))
1529 return 0;
1530 if (generate)
1531 emit_constant_insn (cond,
1532 gen_rtx_SET (VOIDmode, target, source));
1533 return 1;
1535 if (remainder == 0xffffffff)
1537 if (generate)
1538 emit_constant_insn (cond,
1539 gen_rtx_SET (VOIDmode, target,
1540 gen_rtx_NOT (mode, source)));
1541 return 1;
1544 /* We don't know how to handle this yet below. */
1545 abort ();
1547 case MINUS:
1548 /* We treat MINUS as (val - source), since (source - val) is always
1549 passed as (source + (-val)). */
1550 if (remainder == 0)
1552 if (generate)
1553 emit_constant_insn (cond,
1554 gen_rtx_SET (VOIDmode, target,
1555 gen_rtx_NEG (mode, source)));
1556 return 1;
1558 if (const_ok_for_arm (val))
1560 if (generate)
1561 emit_constant_insn (cond,
1562 gen_rtx_SET (VOIDmode, target,
1563 gen_rtx_MINUS (mode, GEN_INT (val),
1564 source)));
1565 return 1;
1567 can_negate = 1;
1569 break;
1571 default:
1572 abort ();
1575 /* If we can do it in one insn get out quickly. */
1576 if (const_ok_for_arm (val)
1577 || (can_negate_initial && const_ok_for_arm (-val))
1578 || (can_invert && const_ok_for_arm (~val)))
1580 if (generate)
1581 emit_constant_insn (cond,
1582 gen_rtx_SET (VOIDmode, target,
1583 (source
1584 ? gen_rtx_fmt_ee (code, mode, source,
1585 GEN_INT (val))
1586 : GEN_INT (val))));
1587 return 1;
1590 /* Calculate a few attributes that may be useful for specific
1591 optimizations. */
1592 for (i = 31; i >= 0; i--)
1594 if ((remainder & (1 << i)) == 0)
1595 clear_sign_bit_copies++;
1596 else
1597 break;
1600 for (i = 31; i >= 0; i--)
1602 if ((remainder & (1 << i)) != 0)
1603 set_sign_bit_copies++;
1604 else
1605 break;
1608 for (i = 0; i <= 31; i++)
1610 if ((remainder & (1 << i)) == 0)
1611 clear_zero_bit_copies++;
1612 else
1613 break;
1616 for (i = 0; i <= 31; i++)
1618 if ((remainder & (1 << i)) != 0)
1619 set_zero_bit_copies++;
1620 else
1621 break;
1624 switch (code)
1626 case SET:
1627 /* See if we can do this by sign_extending a constant that is known
1628 to be negative. This is a good, way of doing it, since the shift
1629 may well merge into a subsequent insn. */
1630 if (set_sign_bit_copies > 1)
1632 if (const_ok_for_arm
1633 (temp1 = ARM_SIGN_EXTEND (remainder
1634 << (set_sign_bit_copies - 1))))
1636 if (generate)
1638 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1639 emit_constant_insn (cond,
1640 gen_rtx_SET (VOIDmode, new_src,
1641 GEN_INT (temp1)));
1642 emit_constant_insn (cond,
1643 gen_ashrsi3 (target, new_src,
1644 GEN_INT (set_sign_bit_copies - 1)));
1646 return 2;
1648 /* For an inverted constant, we will need to set the low bits,
1649 these will be shifted out of harm's way. */
1650 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1651 if (const_ok_for_arm (~temp1))
1653 if (generate)
1655 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1656 emit_constant_insn (cond,
1657 gen_rtx_SET (VOIDmode, new_src,
1658 GEN_INT (temp1)));
1659 emit_constant_insn (cond,
1660 gen_ashrsi3 (target, new_src,
1661 GEN_INT (set_sign_bit_copies - 1)));
1663 return 2;
1667 /* See if we can generate this by setting the bottom (or the top)
1668 16 bits, and then shifting these into the other half of the
1669 word. We only look for the simplest cases, to do more would cost
1670 too much. Be careful, however, not to generate this when the
1671 alternative would take fewer insns. */
1672 if (val & 0xffff0000)
1674 temp1 = remainder & 0xffff0000;
1675 temp2 = remainder & 0x0000ffff;
1677 /* Overlaps outside this range are best done using other methods. */
1678 for (i = 9; i < 24; i++)
1680 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1681 && !const_ok_for_arm (temp2))
1683 rtx new_src = (subtargets
1684 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1685 : target);
1686 insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1687 source, subtargets, generate);
1688 source = new_src;
1689 if (generate)
1690 emit_constant_insn
1691 (cond,
1692 gen_rtx_SET
1693 (VOIDmode, target,
1694 gen_rtx_IOR (mode,
1695 gen_rtx_ASHIFT (mode, source,
1696 GEN_INT (i)),
1697 source)));
1698 return insns + 1;
1702 /* Don't duplicate cases already considered. */
1703 for (i = 17; i < 24; i++)
1705 if (((temp1 | (temp1 >> i)) == remainder)
1706 && !const_ok_for_arm (temp1))
1708 rtx new_src = (subtargets
1709 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1710 : target);
1711 insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1712 source, subtargets, generate);
1713 source = new_src;
1714 if (generate)
1715 emit_constant_insn
1716 (cond,
1717 gen_rtx_SET (VOIDmode, target,
1718 gen_rtx_IOR
1719 (mode,
1720 gen_rtx_LSHIFTRT (mode, source,
1721 GEN_INT (i)),
1722 source)));
1723 return insns + 1;
1727 break;
1729 case IOR:
1730 case XOR:
1731 /* If we have IOR or XOR, and the constant can be loaded in a
1732 single instruction, and we can find a temporary to put it in,
1733 then this can be done in two instructions instead of 3-4. */
1734 if (subtargets
1735 /* TARGET can't be NULL if SUBTARGETS is 0 */
1736 || (reload_completed && !reg_mentioned_p (target, source)))
1738 if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1740 if (generate)
1742 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1744 emit_constant_insn (cond,
1745 gen_rtx_SET (VOIDmode, sub,
1746 GEN_INT (val)));
1747 emit_constant_insn (cond,
1748 gen_rtx_SET (VOIDmode, target,
1749 gen_rtx_fmt_ee (code, mode,
1750 source, sub)));
1752 return 2;
1756 if (code == XOR)
1757 break;
1759 if (set_sign_bit_copies > 8
1760 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1762 if (generate)
1764 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1765 rtx shift = GEN_INT (set_sign_bit_copies);
1767 emit_constant_insn
1768 (cond,
1769 gen_rtx_SET (VOIDmode, sub,
1770 gen_rtx_NOT (mode,
1771 gen_rtx_ASHIFT (mode,
1772 source,
1773 shift))));
1774 emit_constant_insn
1775 (cond,
1776 gen_rtx_SET (VOIDmode, target,
1777 gen_rtx_NOT (mode,
1778 gen_rtx_LSHIFTRT (mode, sub,
1779 shift))));
1781 return 2;
1784 if (set_zero_bit_copies > 8
1785 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1787 if (generate)
1789 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1790 rtx shift = GEN_INT (set_zero_bit_copies);
1792 emit_constant_insn
1793 (cond,
1794 gen_rtx_SET (VOIDmode, sub,
1795 gen_rtx_NOT (mode,
1796 gen_rtx_LSHIFTRT (mode,
1797 source,
1798 shift))));
1799 emit_constant_insn
1800 (cond,
1801 gen_rtx_SET (VOIDmode, target,
1802 gen_rtx_NOT (mode,
1803 gen_rtx_ASHIFT (mode, sub,
1804 shift))));
1806 return 2;
1809 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1811 if (generate)
1813 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1814 emit_constant_insn (cond,
1815 gen_rtx_SET (VOIDmode, sub,
1816 gen_rtx_NOT (mode, source)));
1817 source = sub;
1818 if (subtargets)
1819 sub = gen_reg_rtx (mode);
1820 emit_constant_insn (cond,
1821 gen_rtx_SET (VOIDmode, sub,
1822 gen_rtx_AND (mode, source,
1823 GEN_INT (temp1))));
1824 emit_constant_insn (cond,
1825 gen_rtx_SET (VOIDmode, target,
1826 gen_rtx_NOT (mode, sub)));
1828 return 3;
1830 break;
1832 case AND:
1833 /* See if two shifts will do 2 or more insn's worth of work. */
1834 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1836 HOST_WIDE_INT shift_mask = ((0xffffffff
1837 << (32 - clear_sign_bit_copies))
1838 & 0xffffffff);
1840 if ((remainder | shift_mask) != 0xffffffff)
1842 if (generate)
1844 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1845 insns = arm_gen_constant (AND, mode, cond,
1846 remainder | shift_mask,
1847 new_src, source, subtargets, 1);
1848 source = new_src;
1850 else
1852 rtx targ = subtargets ? NULL_RTX : target;
1853 insns = arm_gen_constant (AND, mode, cond,
1854 remainder | shift_mask,
1855 targ, source, subtargets, 0);
1859 if (generate)
1861 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1862 rtx shift = GEN_INT (clear_sign_bit_copies);
1864 emit_insn (gen_ashlsi3 (new_src, source, shift));
1865 emit_insn (gen_lshrsi3 (target, new_src, shift));
1868 return insns + 2;
1871 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
1873 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
1875 if ((remainder | shift_mask) != 0xffffffff)
1877 if (generate)
1879 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1881 insns = arm_gen_constant (AND, mode, cond,
1882 remainder | shift_mask,
1883 new_src, source, subtargets, 1);
1884 source = new_src;
1886 else
1888 rtx targ = subtargets ? NULL_RTX : target;
1890 insns = arm_gen_constant (AND, mode, cond,
1891 remainder | shift_mask,
1892 targ, source, subtargets, 0);
1896 if (generate)
1898 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1899 rtx shift = GEN_INT (clear_zero_bit_copies);
1901 emit_insn (gen_lshrsi3 (new_src, source, shift));
1902 emit_insn (gen_ashlsi3 (target, new_src, shift));
1905 return insns + 2;
1908 break;
1910 default:
1911 break;
1914 for (i = 0; i < 32; i++)
1915 if (remainder & (1 << i))
1916 num_bits_set++;
1918 if (code == AND || (can_invert && num_bits_set > 16))
1919 remainder = (~remainder) & 0xffffffff;
1920 else if (code == PLUS && num_bits_set > 16)
1921 remainder = (-remainder) & 0xffffffff;
1922 else
1924 can_invert = 0;
1925 can_negate = 0;
1928 /* Now try and find a way of doing the job in either two or three
1929 instructions.
1930 We start by looking for the largest block of zeros that are aligned on
1931 a 2-bit boundary, we then fill up the temps, wrapping around to the
1932 top of the word when we drop off the bottom.
1933 In the worst case this code should produce no more than four insns. */
1935 int best_start = 0;
1936 int best_consecutive_zeros = 0;
1938 for (i = 0; i < 32; i += 2)
1940 int consecutive_zeros = 0;
1942 if (!(remainder & (3 << i)))
1944 while ((i < 32) && !(remainder & (3 << i)))
1946 consecutive_zeros += 2;
1947 i += 2;
1949 if (consecutive_zeros > best_consecutive_zeros)
1951 best_consecutive_zeros = consecutive_zeros;
1952 best_start = i - consecutive_zeros;
1954 i -= 2;
1958 /* So long as it won't require any more insns to do so, it's
1959 desirable to emit a small constant (in bits 0...9) in the last
1960 insn. This way there is more chance that it can be combined with
1961 a later addressing insn to form a pre-indexed load or store
1962 operation. Consider:
1964 *((volatile int *)0xe0000100) = 1;
1965 *((volatile int *)0xe0000110) = 2;
1967 We want this to wind up as:
1969 mov rA, #0xe0000000
1970 mov rB, #1
1971 str rB, [rA, #0x100]
1972 mov rB, #2
1973 str rB, [rA, #0x110]
1975 rather than having to synthesize both large constants from scratch.
1977 Therefore, we calculate how many insns would be required to emit
1978 the constant starting from `best_start', and also starting from
1979 zero (ie with bit 31 first to be output). If `best_start' doesn't
1980 yield a shorter sequence, we may as well use zero. */
1981 if (best_start != 0
1982 && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
1983 && (count_insns_for_constant (remainder, 0) <=
1984 count_insns_for_constant (remainder, best_start)))
1985 best_start = 0;
1987 /* Now start emitting the insns. */
1988 i = best_start;
1991 int end;
1993 if (i <= 0)
1994 i += 32;
1995 if (remainder & (3 << (i - 2)))
1997 end = i - 8;
1998 if (end < 0)
1999 end += 32;
2000 temp1 = remainder & ((0x0ff << end)
2001 | ((i < end) ? (0xff >> (32 - end)) : 0));
2002 remainder &= ~temp1;
2004 if (generate)
2006 rtx new_src, temp1_rtx;
2008 if (code == SET || code == MINUS)
2010 new_src = (subtargets ? gen_reg_rtx (mode) : target);
2011 if (can_invert && code != MINUS)
2012 temp1 = ~temp1;
2014 else
2016 if (remainder && subtargets)
2017 new_src = gen_reg_rtx (mode);
2018 else
2019 new_src = target;
2020 if (can_invert)
2021 temp1 = ~temp1;
2022 else if (can_negate)
2023 temp1 = -temp1;
2026 temp1 = trunc_int_for_mode (temp1, mode);
2027 temp1_rtx = GEN_INT (temp1);
2029 if (code == SET)
2031 else if (code == MINUS)
2032 temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2033 else
2034 temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2036 emit_constant_insn (cond,
2037 gen_rtx_SET (VOIDmode, new_src,
2038 temp1_rtx));
2039 source = new_src;
2042 if (code == SET)
2044 can_invert = 0;
2045 code = PLUS;
2047 else if (code == MINUS)
2048 code = PLUS;
2050 insns++;
2051 i -= 6;
2053 i -= 2;
2055 while (remainder);
2058 return insns;
2061 /* Canonicalize a comparison so that we are more likely to recognize it.
2062 This can be done for a few constant compares, where we can make the
2063 immediate value easier to load. */
2065 enum rtx_code
2066 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2068 unsigned HOST_WIDE_INT i = INTVAL (*op1);
2070 switch (code)
2072 case EQ:
2073 case NE:
2074 return code;
2076 case GT:
2077 case LE:
2078 if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2079 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2081 *op1 = GEN_INT (i + 1);
2082 return code == GT ? GE : LT;
2084 break;
2086 case GE:
2087 case LT:
2088 if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2089 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2091 *op1 = GEN_INT (i - 1);
2092 return code == GE ? GT : LE;
2094 break;
2096 case GTU:
2097 case LEU:
2098 if (i != ~((unsigned HOST_WIDE_INT) 0)
2099 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2101 *op1 = GEN_INT (i + 1);
2102 return code == GTU ? GEU : LTU;
2104 break;
2106 case GEU:
2107 case LTU:
2108 if (i != 0
2109 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2111 *op1 = GEN_INT (i - 1);
2112 return code == GEU ? GTU : LEU;
2114 break;
2116 default:
2117 abort ();
2120 return code;
2124 /* Define how to find the value returned by a function. */
2126 rtx arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2128 enum machine_mode mode;
2129 int unsignedp ATTRIBUTE_UNUSED;
2130 rtx r ATTRIBUTE_UNUSED;
2133 mode = TYPE_MODE (type);
2134 /* Promote integer types. */
2135 if (INTEGRAL_TYPE_P (type))
2136 PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2137 return LIBCALL_VALUE(mode);
2141 /* Decide whether a type should be returned in memory (true)
2142 or in a register (false). This is called by the macro
2143 RETURN_IN_MEMORY. */
2145 arm_return_in_memory (tree type)
2147 HOST_WIDE_INT size;
2149 if (!AGGREGATE_TYPE_P (type))
2150 /* All simple types are returned in registers. */
2151 return 0;
2153 size = int_size_in_bytes (type);
2155 if (arm_abi != ARM_ABI_APCS)
2157 /* ATPCS and later return aggregate types in memory only if they are
2158 larger than a word (or are variable size). */
2159 return (size < 0 || size > UNITS_PER_WORD);
2162 /* For the arm-wince targets we choose to be compatible with Microsoft's
2163 ARM and Thumb compilers, which always return aggregates in memory. */
2164 #ifndef ARM_WINCE
2165 /* All structures/unions bigger than one word are returned in memory.
2166 Also catch the case where int_size_in_bytes returns -1. In this case
2167 the aggregate is either huge or of variable size, and in either case
2168 we will want to return it via memory and not in a register. */
2169 if (size < 0 || size > UNITS_PER_WORD)
2170 return 1;
2172 if (TREE_CODE (type) == RECORD_TYPE)
2174 tree field;
2176 /* For a struct the APCS says that we only return in a register
2177 if the type is 'integer like' and every addressable element
2178 has an offset of zero. For practical purposes this means
2179 that the structure can have at most one non bit-field element
2180 and that this element must be the first one in the structure. */
2182 /* Find the first field, ignoring non FIELD_DECL things which will
2183 have been created by C++. */
2184 for (field = TYPE_FIELDS (type);
2185 field && TREE_CODE (field) != FIELD_DECL;
2186 field = TREE_CHAIN (field))
2187 continue;
2189 if (field == NULL)
2190 return 0; /* An empty structure. Allowed by an extension to ANSI C. */
2192 /* Check that the first field is valid for returning in a register. */
2194 /* ... Floats are not allowed */
2195 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2196 return 1;
2198 /* ... Aggregates that are not themselves valid for returning in
2199 a register are not allowed. */
2200 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2201 return 1;
2203 /* Now check the remaining fields, if any. Only bitfields are allowed,
2204 since they are not addressable. */
2205 for (field = TREE_CHAIN (field);
2206 field;
2207 field = TREE_CHAIN (field))
2209 if (TREE_CODE (field) != FIELD_DECL)
2210 continue;
2212 if (!DECL_BIT_FIELD_TYPE (field))
2213 return 1;
2216 return 0;
2219 if (TREE_CODE (type) == UNION_TYPE)
2221 tree field;
2223 /* Unions can be returned in registers if every element is
2224 integral, or can be returned in an integer register. */
2225 for (field = TYPE_FIELDS (type);
2226 field;
2227 field = TREE_CHAIN (field))
2229 if (TREE_CODE (field) != FIELD_DECL)
2230 continue;
2232 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2233 return 1;
2235 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2236 return 1;
2239 return 0;
2241 #endif /* not ARM_WINCE */
2243 /* Return all other types in memory. */
2244 return 1;
2247 /* Indicate whether or not words of a double are in big-endian order. */
2250 arm_float_words_big_endian (void)
2252 if (TARGET_MAVERICK)
2253 return 0;
2255 /* For FPA, float words are always big-endian. For VFP, floats words
2256 follow the memory system mode. */
2258 if (TARGET_FPA)
2260 return 1;
2263 if (TARGET_VFP)
2264 return (TARGET_BIG_END ? 1 : 0);
2266 return 1;
2269 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2270 for a call to a function whose data type is FNTYPE.
2271 For a library call, FNTYPE is NULL. */
2272 void
2273 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2274 rtx libname ATTRIBUTE_UNUSED,
2275 tree fndecl ATTRIBUTE_UNUSED)
2277 /* On the ARM, the offset starts at 0. */
2278 pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2279 pcum->iwmmxt_nregs = 0;
2280 pcum->can_split = true;
2282 pcum->call_cookie = CALL_NORMAL;
2284 if (TARGET_LONG_CALLS)
2285 pcum->call_cookie = CALL_LONG;
2287 /* Check for long call/short call attributes. The attributes
2288 override any command line option. */
2289 if (fntype)
2291 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2292 pcum->call_cookie = CALL_SHORT;
2293 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2294 pcum->call_cookie = CALL_LONG;
2297 /* Varargs vectors are treated the same as long long.
2298 named_count avoids having to change the way arm handles 'named' */
2299 pcum->named_count = 0;
2300 pcum->nargs = 0;
2302 if (TARGET_REALLY_IWMMXT && fntype)
2304 tree fn_arg;
2306 for (fn_arg = TYPE_ARG_TYPES (fntype);
2307 fn_arg;
2308 fn_arg = TREE_CHAIN (fn_arg))
2309 pcum->named_count += 1;
2311 if (! pcum->named_count)
2312 pcum->named_count = INT_MAX;
2317 /* Return true if mode/type need doubleword alignment. */
2318 bool
2319 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2321 return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2322 || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2326 /* Determine where to put an argument to a function.
2327 Value is zero to push the argument on the stack,
2328 or a hard register in which to store the argument.
2330 MODE is the argument's machine mode.
2331 TYPE is the data type of the argument (as a tree).
2332 This is null for libcalls where that information may
2333 not be available.
2334 CUM is a variable of type CUMULATIVE_ARGS which gives info about
2335 the preceding args and about the function being called.
2336 NAMED is nonzero if this argument is a named parameter
2337 (otherwise it is an extra parameter matching an ellipsis). */
2340 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2341 tree type, int named)
2343 int nregs;
2345 /* Varargs vectors are treated the same as long long.
2346 named_count avoids having to change the way arm handles 'named' */
2347 if (TARGET_IWMMXT_ABI
2348 && VECTOR_MODE_SUPPORTED_P (mode)
2349 && pcum->named_count > pcum->nargs + 1)
2351 if (pcum->iwmmxt_nregs <= 9)
2352 return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2353 else
2355 pcum->can_split = false;
2356 return NULL_RTX;
2360 /* Put doubleword aligned quantities in even register pairs. */
2361 if (pcum->nregs & 1
2362 && ARM_DOUBLEWORD_ALIGN
2363 && arm_needs_doubleword_align (mode, type))
2364 pcum->nregs++;
2366 if (mode == VOIDmode)
2367 /* Compute operand 2 of the call insn. */
2368 return GEN_INT (pcum->call_cookie);
2370 /* Only allow splitting an arg between regs and memory if all preceding
2371 args were allocated to regs. For args passed by reference we only count
2372 the reference pointer. */
2373 if (pcum->can_split)
2374 nregs = 1;
2375 else
2376 nregs = ARM_NUM_REGS2 (mode, type);
2378 if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2379 return NULL_RTX;
2381 return gen_rtx_REG (mode, pcum->nregs);
2384 /* Variable sized types are passed by reference. This is a GCC
2385 extension to the ARM ABI. */
2388 arm_function_arg_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2389 enum machine_mode mode ATTRIBUTE_UNUSED,
2390 tree type, int named ATTRIBUTE_UNUSED)
2392 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2395 /* Implement va_arg. */
2398 arm_va_arg (tree valist, tree type)
2400 int align;
2402 /* Variable sized types are passed by reference. */
2403 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
2405 rtx addr = std_expand_builtin_va_arg (valist, build_pointer_type (type));
2406 return gen_rtx_MEM (ptr_mode, force_reg (Pmode, addr));
2409 align = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type);
2410 if (align > PARM_BOUNDARY)
2412 tree mask;
2413 tree t;
2415 /* Maintain 64-bit alignment of the valist pointer by
2416 constructing: valist = ((valist + (8 - 1)) & -8). */
2417 mask = build_int_2 (- (align / BITS_PER_UNIT), -1);
2418 t = build_int_2 ((align / BITS_PER_UNIT) - 1, 0);
2419 t = build (PLUS_EXPR, TREE_TYPE (valist), valist, t);
2420 t = build (BIT_AND_EXPR, TREE_TYPE (t), t, mask);
2421 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
2422 TREE_SIDE_EFFECTS (t) = 1;
2423 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2425 /* This is to stop the combine pass optimizing
2426 away the alignment adjustment. */
2427 mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
2430 return std_expand_builtin_va_arg (valist, type);
2433 /* Encode the current state of the #pragma [no_]long_calls. */
2434 typedef enum
2436 OFF, /* No #pramgma [no_]long_calls is in effect. */
2437 LONG, /* #pragma long_calls is in effect. */
2438 SHORT /* #pragma no_long_calls is in effect. */
2439 } arm_pragma_enum;
2441 static arm_pragma_enum arm_pragma_long_calls = OFF;
2443 void
2444 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2446 arm_pragma_long_calls = LONG;
2449 void
2450 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2452 arm_pragma_long_calls = SHORT;
2455 void
2456 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2458 arm_pragma_long_calls = OFF;
2461 /* Table of machine attributes. */
2462 const struct attribute_spec arm_attribute_table[] =
2464 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2465 /* Function calls made to this symbol must be done indirectly, because
2466 it may lie outside of the 26 bit addressing range of a normal function
2467 call. */
2468 { "long_call", 0, 0, false, true, true, NULL },
2469 /* Whereas these functions are always known to reside within the 26 bit
2470 addressing range. */
2471 { "short_call", 0, 0, false, true, true, NULL },
2472 /* Interrupt Service Routines have special prologue and epilogue requirements. */
2473 { "isr", 0, 1, false, false, false, arm_handle_isr_attribute },
2474 { "interrupt", 0, 1, false, false, false, arm_handle_isr_attribute },
2475 { "naked", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2476 #ifdef ARM_PE
2477 /* ARM/PE has three new attributes:
2478 interfacearm - ?
2479 dllexport - for exporting a function/variable that will live in a dll
2480 dllimport - for importing a function/variable from a dll
2482 Microsoft allows multiple declspecs in one __declspec, separating
2483 them with spaces. We do NOT support this. Instead, use __declspec
2484 multiple times.
2486 { "dllimport", 0, 0, true, false, false, NULL },
2487 { "dllexport", 0, 0, true, false, false, NULL },
2488 { "interfacearm", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2489 #endif
2490 { NULL, 0, 0, false, false, false, NULL }
2493 /* Handle an attribute requiring a FUNCTION_DECL;
2494 arguments as in struct attribute_spec.handler. */
2495 static tree
2496 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2497 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2499 if (TREE_CODE (*node) != FUNCTION_DECL)
2501 warning ("`%s' attribute only applies to functions",
2502 IDENTIFIER_POINTER (name));
2503 *no_add_attrs = true;
2506 return NULL_TREE;
2509 /* Handle an "interrupt" or "isr" attribute;
2510 arguments as in struct attribute_spec.handler. */
2511 static tree
2512 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2513 bool *no_add_attrs)
2515 if (DECL_P (*node))
2517 if (TREE_CODE (*node) != FUNCTION_DECL)
2519 warning ("`%s' attribute only applies to functions",
2520 IDENTIFIER_POINTER (name));
2521 *no_add_attrs = true;
2523 /* FIXME: the argument if any is checked for type attributes;
2524 should it be checked for decl ones? */
2526 else
2528 if (TREE_CODE (*node) == FUNCTION_TYPE
2529 || TREE_CODE (*node) == METHOD_TYPE)
2531 if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2533 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2534 *no_add_attrs = true;
2537 else if (TREE_CODE (*node) == POINTER_TYPE
2538 && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2539 || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2540 && arm_isr_value (args) != ARM_FT_UNKNOWN)
2542 *node = build_type_copy (*node);
2543 TREE_TYPE (*node) = build_type_attribute_variant
2544 (TREE_TYPE (*node),
2545 tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2546 *no_add_attrs = true;
2548 else
2550 /* Possibly pass this attribute on from the type to a decl. */
2551 if (flags & ((int) ATTR_FLAG_DECL_NEXT
2552 | (int) ATTR_FLAG_FUNCTION_NEXT
2553 | (int) ATTR_FLAG_ARRAY_NEXT))
2555 *no_add_attrs = true;
2556 return tree_cons (name, args, NULL_TREE);
2558 else
2560 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2565 return NULL_TREE;
2568 /* Return 0 if the attributes for two types are incompatible, 1 if they
2569 are compatible, and 2 if they are nearly compatible (which causes a
2570 warning to be generated). */
2571 static int
2572 arm_comp_type_attributes (tree type1, tree type2)
2574 int l1, l2, s1, s2;
2576 /* Check for mismatch of non-default calling convention. */
2577 if (TREE_CODE (type1) != FUNCTION_TYPE)
2578 return 1;
2580 /* Check for mismatched call attributes. */
2581 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2582 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2583 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2584 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2586 /* Only bother to check if an attribute is defined. */
2587 if (l1 | l2 | s1 | s2)
2589 /* If one type has an attribute, the other must have the same attribute. */
2590 if ((l1 != l2) || (s1 != s2))
2591 return 0;
2593 /* Disallow mixed attributes. */
2594 if ((l1 & s2) || (l2 & s1))
2595 return 0;
2598 /* Check for mismatched ISR attribute. */
2599 l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2600 if (! l1)
2601 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2602 l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2603 if (! l2)
2604 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2605 if (l1 != l2)
2606 return 0;
2608 return 1;
2611 /* Encode long_call or short_call attribute by prefixing
2612 symbol name in DECL with a special character FLAG. */
2613 void
2614 arm_encode_call_attribute (tree decl, int flag)
2616 const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2617 int len = strlen (str);
2618 char * newstr;
2620 /* Do not allow weak functions to be treated as short call. */
2621 if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2622 return;
2624 newstr = alloca (len + 2);
2625 newstr[0] = flag;
2626 strcpy (newstr + 1, str);
2628 newstr = (char *) ggc_alloc_string (newstr, len + 1);
2629 XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2632 /* Assigns default attributes to newly defined type. This is used to
2633 set short_call/long_call attributes for function types of
2634 functions defined inside corresponding #pragma scopes. */
2635 static void
2636 arm_set_default_type_attributes (tree type)
2638 /* Add __attribute__ ((long_call)) to all functions, when
2639 inside #pragma long_calls or __attribute__ ((short_call)),
2640 when inside #pragma no_long_calls. */
2641 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2643 tree type_attr_list, attr_name;
2644 type_attr_list = TYPE_ATTRIBUTES (type);
2646 if (arm_pragma_long_calls == LONG)
2647 attr_name = get_identifier ("long_call");
2648 else if (arm_pragma_long_calls == SHORT)
2649 attr_name = get_identifier ("short_call");
2650 else
2651 return;
2653 type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2654 TYPE_ATTRIBUTES (type) = type_attr_list;
2658 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2659 defined within the current compilation unit. If this cannot be
2660 determined, then 0 is returned. */
2661 static int
2662 current_file_function_operand (rtx sym_ref)
2664 /* This is a bit of a fib. A function will have a short call flag
2665 applied to its name if it has the short call attribute, or it has
2666 already been defined within the current compilation unit. */
2667 if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2668 return 1;
2670 /* The current function is always defined within the current compilation
2671 unit. if it s a weak definition however, then this may not be the real
2672 definition of the function, and so we have to say no. */
2673 if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2674 && !DECL_WEAK (current_function_decl))
2675 return 1;
2677 /* We cannot make the determination - default to returning 0. */
2678 return 0;
2681 /* Return nonzero if a 32 bit "long_call" should be generated for
2682 this call. We generate a long_call if the function:
2684 a. has an __attribute__((long call))
2685 or b. is within the scope of a #pragma long_calls
2686 or c. the -mlong-calls command line switch has been specified
2688 However we do not generate a long call if the function:
2690 d. has an __attribute__ ((short_call))
2691 or e. is inside the scope of a #pragma no_long_calls
2692 or f. has an __attribute__ ((section))
2693 or g. is defined within the current compilation unit.
2695 This function will be called by C fragments contained in the machine
2696 description file. CALL_REF and CALL_COOKIE correspond to the matched
2697 rtl operands. CALL_SYMBOL is used to distinguish between
2698 two different callers of the function. It is set to 1 in the
2699 "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2700 and "call_value" patterns. This is because of the difference in the
2701 SYM_REFs passed by these patterns. */
2703 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
2705 if (!call_symbol)
2707 if (GET_CODE (sym_ref) != MEM)
2708 return 0;
2710 sym_ref = XEXP (sym_ref, 0);
2713 if (GET_CODE (sym_ref) != SYMBOL_REF)
2714 return 0;
2716 if (call_cookie & CALL_SHORT)
2717 return 0;
2719 if (TARGET_LONG_CALLS && flag_function_sections)
2720 return 1;
2722 if (current_file_function_operand (sym_ref))
2723 return 0;
2725 return (call_cookie & CALL_LONG)
2726 || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2727 || TARGET_LONG_CALLS;
2730 /* Return nonzero if it is ok to make a tail-call to DECL. */
2731 static bool
2732 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
2734 int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2736 if (cfun->machine->sibcall_blocked)
2737 return false;
2739 /* Never tailcall something for which we have no decl, or if we
2740 are in Thumb mode. */
2741 if (decl == NULL || TARGET_THUMB)
2742 return false;
2744 /* Get the calling method. */
2745 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2746 call_type = CALL_SHORT;
2747 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2748 call_type = CALL_LONG;
2750 /* Cannot tail-call to long calls, since these are out of range of
2751 a branch instruction. However, if not compiling PIC, we know
2752 we can reach the symbol if it is in this compilation unit. */
2753 if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2754 return false;
2756 /* If we are interworking and the function is not declared static
2757 then we can't tail-call it unless we know that it exists in this
2758 compilation unit (since it might be a Thumb routine). */
2759 if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2760 return false;
2762 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
2763 if (IS_INTERRUPT (arm_current_func_type ()))
2764 return false;
2766 /* Everything else is ok. */
2767 return true;
2771 /* Addressing mode support functions. */
2773 /* Return nonzero if X is a legitimate immediate operand when compiling
2774 for PIC. */
2776 legitimate_pic_operand_p (rtx x)
2778 if (CONSTANT_P (x)
2779 && flag_pic
2780 && (GET_CODE (x) == SYMBOL_REF
2781 || (GET_CODE (x) == CONST
2782 && GET_CODE (XEXP (x, 0)) == PLUS
2783 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2784 return 0;
2786 return 1;
2790 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
2792 if (GET_CODE (orig) == SYMBOL_REF
2793 || GET_CODE (orig) == LABEL_REF)
2795 #ifndef AOF_ASSEMBLER
2796 rtx pic_ref, address;
2797 #endif
2798 rtx insn;
2799 int subregs = 0;
2801 if (reg == 0)
2803 if (no_new_pseudos)
2804 abort ();
2805 else
2806 reg = gen_reg_rtx (Pmode);
2808 subregs = 1;
2811 #ifdef AOF_ASSEMBLER
2812 /* The AOF assembler can generate relocations for these directly, and
2813 understands that the PIC register has to be added into the offset. */
2814 insn = emit_insn (gen_pic_load_addr_based (reg, orig));
2815 #else
2816 if (subregs)
2817 address = gen_reg_rtx (Pmode);
2818 else
2819 address = reg;
2821 if (TARGET_ARM)
2822 emit_insn (gen_pic_load_addr_arm (address, orig));
2823 else
2824 emit_insn (gen_pic_load_addr_thumb (address, orig));
2826 if ((GET_CODE (orig) == LABEL_REF
2827 || (GET_CODE (orig) == SYMBOL_REF &&
2828 SYMBOL_REF_LOCAL_P (orig)))
2829 && NEED_GOT_RELOC)
2830 pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
2831 else
2833 pic_ref = gen_rtx_MEM (Pmode,
2834 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
2835 address));
2836 RTX_UNCHANGING_P (pic_ref) = 1;
2839 insn = emit_move_insn (reg, pic_ref);
2840 #endif
2841 current_function_uses_pic_offset_table = 1;
2842 /* Put a REG_EQUAL note on this insn, so that it can be optimized
2843 by loop. */
2844 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
2845 REG_NOTES (insn));
2846 return reg;
2848 else if (GET_CODE (orig) == CONST)
2850 rtx base, offset;
2852 if (GET_CODE (XEXP (orig, 0)) == PLUS
2853 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
2854 return orig;
2856 if (reg == 0)
2858 if (no_new_pseudos)
2859 abort ();
2860 else
2861 reg = gen_reg_rtx (Pmode);
2864 if (GET_CODE (XEXP (orig, 0)) == PLUS)
2866 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
2867 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
2868 base == reg ? 0 : reg);
2870 else
2871 abort ();
2873 if (GET_CODE (offset) == CONST_INT)
2875 /* The base register doesn't really matter, we only want to
2876 test the index for the appropriate mode. */
2877 if (!arm_legitimate_index_p (mode, offset, SET, 0))
2879 if (!no_new_pseudos)
2880 offset = force_reg (Pmode, offset);
2881 else
2882 abort ();
2885 if (GET_CODE (offset) == CONST_INT)
2886 return plus_constant (base, INTVAL (offset));
2889 if (GET_MODE_SIZE (mode) > 4
2890 && (GET_MODE_CLASS (mode) == MODE_INT
2891 || TARGET_SOFT_FLOAT))
2893 emit_insn (gen_addsi3 (reg, base, offset));
2894 return reg;
2897 return gen_rtx_PLUS (Pmode, base, offset);
2900 return orig;
2903 /* Generate code to load the PIC register. PROLOGUE is true if
2904 called from arm_expand_prologue (in which case we want the
2905 generated insns at the start of the function); false if called
2906 by an exception receiver that needs the PIC register reloaded
2907 (in which case the insns are just dumped at the current location). */
2908 void
2909 arm_finalize_pic (int prologue ATTRIBUTE_UNUSED)
2911 #ifndef AOF_ASSEMBLER
2912 rtx l1, pic_tmp, pic_tmp2, seq, pic_rtx;
2913 rtx global_offset_table;
2915 if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
2916 return;
2918 if (!flag_pic)
2919 abort ();
2921 start_sequence ();
2922 l1 = gen_label_rtx ();
2924 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
2925 /* On the ARM the PC register contains 'dot + 8' at the time of the
2926 addition, on the Thumb it is 'dot + 4'. */
2927 pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
2928 if (GOT_PCREL)
2929 pic_tmp2 = gen_rtx_CONST (VOIDmode,
2930 gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
2931 else
2932 pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
2934 pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
2936 if (TARGET_ARM)
2938 emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
2939 emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
2941 else
2943 emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
2944 emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
2947 seq = get_insns ();
2948 end_sequence ();
2949 if (prologue)
2950 emit_insn_after (seq, get_insns ());
2951 else
2952 emit_insn (seq);
2954 /* Need to emit this whether or not we obey regdecls,
2955 since setjmp/longjmp can cause life info to screw up. */
2956 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
2957 #endif /* AOF_ASSEMBLER */
2960 /* Return nonzero if X is valid as an ARM state addressing register. */
2961 static int
2962 arm_address_register_rtx_p (rtx x, int strict_p)
2964 int regno;
2966 if (GET_CODE (x) != REG)
2967 return 0;
2969 regno = REGNO (x);
2971 if (strict_p)
2972 return ARM_REGNO_OK_FOR_BASE_P (regno);
2974 return (regno <= LAST_ARM_REGNUM
2975 || regno >= FIRST_PSEUDO_REGISTER
2976 || regno == FRAME_POINTER_REGNUM
2977 || regno == ARG_POINTER_REGNUM);
2980 /* Return nonzero if X is a valid ARM state address operand. */
2982 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
2983 int strict_p)
2985 if (arm_address_register_rtx_p (x, strict_p))
2986 return 1;
2988 else if (GET_CODE (x) == POST_INC || GET_CODE (x) == PRE_DEC)
2989 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
2991 else if ((GET_CODE (x) == POST_MODIFY || GET_CODE (x) == PRE_MODIFY)
2992 && GET_MODE_SIZE (mode) <= 4
2993 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
2994 && GET_CODE (XEXP (x, 1)) == PLUS
2995 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
2996 return arm_legitimate_index_p (mode, XEXP (XEXP (x, 1), 1), outer,
2997 strict_p);
2999 /* After reload constants split into minipools will have addresses
3000 from a LABEL_REF. */
3001 else if (reload_completed
3002 && (GET_CODE (x) == LABEL_REF
3003 || (GET_CODE (x) == CONST
3004 && GET_CODE (XEXP (x, 0)) == PLUS
3005 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3006 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3007 return 1;
3009 else if (mode == TImode)
3010 return 0;
3012 else if (mode == DImode || (TARGET_SOFT_FLOAT && mode == DFmode))
3014 if (GET_CODE (x) == PLUS
3015 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3016 && GET_CODE (XEXP (x, 1)) == CONST_INT)
3018 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
3020 if (val == 4 || val == -4 || val == -8)
3021 return 1;
3025 else if (TARGET_HARD_FLOAT && TARGET_VFP && mode == DFmode)
3027 if (GET_CODE (x) == PLUS
3028 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3029 && GET_CODE (XEXP (x, 1)) == CONST_INT)
3031 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
3033 /* ??? valid arm offsets are a subset of VFP offsets.
3034 For now only allow this subset. Proper fix is to add an
3035 additional memory constraint for arm address modes.
3036 Alternatively allow full vfp addressing and let
3037 output_move_double fix it up with a sub-optimal sequence. */
3038 if (val == 4 || val == -4 || val == -8)
3039 return 1;
3043 else if (GET_CODE (x) == PLUS)
3045 rtx xop0 = XEXP (x, 0);
3046 rtx xop1 = XEXP (x, 1);
3048 return ((arm_address_register_rtx_p (xop0, strict_p)
3049 && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3050 || (arm_address_register_rtx_p (xop1, strict_p)
3051 && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3054 #if 0
3055 /* Reload currently can't handle MINUS, so disable this for now */
3056 else if (GET_CODE (x) == MINUS)
3058 rtx xop0 = XEXP (x, 0);
3059 rtx xop1 = XEXP (x, 1);
3061 return (arm_address_register_rtx_p (xop0, strict_p)
3062 && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3064 #endif
3066 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3067 && GET_CODE (x) == SYMBOL_REF
3068 && CONSTANT_POOL_ADDRESS_P (x)
3069 && ! (flag_pic
3070 && symbol_mentioned_p (get_pool_constant (x))))
3071 return 1;
3073 else if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == POST_DEC)
3074 && (GET_MODE_SIZE (mode) <= 4)
3075 && arm_address_register_rtx_p (XEXP (x, 0), strict_p))
3076 return 1;
3078 return 0;
3081 /* Return nonzero if INDEX is valid for an address index operand in
3082 ARM state. */
3083 static int
3084 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3085 int strict_p)
3087 HOST_WIDE_INT range;
3088 enum rtx_code code = GET_CODE (index);
3090 /* Standard coprocessor addressing modes. */
3091 if (TARGET_HARD_FLOAT
3092 && (TARGET_FPA || TARGET_MAVERICK)
3093 && (GET_MODE_CLASS (mode) == MODE_FLOAT
3094 || (TARGET_MAVERICK && mode == DImode)))
3095 return (code == CONST_INT && INTVAL (index) < 1024
3096 && INTVAL (index) > -1024
3097 && (INTVAL (index) & 3) == 0);
3099 if (arm_address_register_rtx_p (index, strict_p)
3100 && GET_MODE_SIZE (mode) <= 4)
3101 return 1;
3103 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3104 return (code == CONST_INT
3105 && INTVAL (index) < 1024
3106 && INTVAL (index) > -1024
3107 && (INTVAL (index) & 3) == 0);
3109 if (GET_MODE_SIZE (mode) <= 4
3110 && ! (arm_arch4
3111 && (mode == HImode
3112 || (mode == QImode && outer == SIGN_EXTEND))))
3114 if (code == MULT)
3116 rtx xiop0 = XEXP (index, 0);
3117 rtx xiop1 = XEXP (index, 1);
3119 return ((arm_address_register_rtx_p (xiop0, strict_p)
3120 && power_of_two_operand (xiop1, SImode))
3121 || (arm_address_register_rtx_p (xiop1, strict_p)
3122 && power_of_two_operand (xiop0, SImode)));
3124 else if (code == LSHIFTRT || code == ASHIFTRT
3125 || code == ASHIFT || code == ROTATERT)
3127 rtx op = XEXP (index, 1);
3129 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3130 && GET_CODE (op) == CONST_INT
3131 && INTVAL (op) > 0
3132 && INTVAL (op) <= 31);
3136 /* For ARM v4 we may be doing a sign-extend operation during the
3137 load. */
3138 if (arm_arch4)
3140 if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3141 range = 256;
3142 else
3143 range = 4096;
3145 else
3146 range = (mode == HImode) ? 4095 : 4096;
3148 return (code == CONST_INT
3149 && INTVAL (index) < range
3150 && INTVAL (index) > -range);
3153 /* Return nonzero if X is valid as a Thumb state base register. */
3154 static int
3155 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3157 int regno;
3159 if (GET_CODE (x) != REG)
3160 return 0;
3162 regno = REGNO (x);
3164 if (strict_p)
3165 return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3167 return (regno <= LAST_LO_REGNUM
3168 || regno > LAST_VIRTUAL_REGISTER
3169 || regno == FRAME_POINTER_REGNUM
3170 || (GET_MODE_SIZE (mode) >= 4
3171 && (regno == STACK_POINTER_REGNUM
3172 || regno >= FIRST_PSEUDO_REGISTER
3173 || x == hard_frame_pointer_rtx
3174 || x == arg_pointer_rtx)));
3177 /* Return nonzero if x is a legitimate index register. This is the case
3178 for any base register that can access a QImode object. */
3179 inline static int
3180 thumb_index_register_rtx_p (rtx x, int strict_p)
3182 return thumb_base_register_rtx_p (x, QImode, strict_p);
3185 /* Return nonzero if x is a legitimate Thumb-state address.
3187 The AP may be eliminated to either the SP or the FP, so we use the
3188 least common denominator, e.g. SImode, and offsets from 0 to 64.
3190 ??? Verify whether the above is the right approach.
3192 ??? Also, the FP may be eliminated to the SP, so perhaps that
3193 needs special handling also.
3195 ??? Look at how the mips16 port solves this problem. It probably uses
3196 better ways to solve some of these problems.
3198 Although it is not incorrect, we don't accept QImode and HImode
3199 addresses based on the frame pointer or arg pointer until the
3200 reload pass starts. This is so that eliminating such addresses
3201 into stack based ones won't produce impossible code. */
3203 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3205 /* ??? Not clear if this is right. Experiment. */
3206 if (GET_MODE_SIZE (mode) < 4
3207 && !(reload_in_progress || reload_completed)
3208 && (reg_mentioned_p (frame_pointer_rtx, x)
3209 || reg_mentioned_p (arg_pointer_rtx, x)
3210 || reg_mentioned_p (virtual_incoming_args_rtx, x)
3211 || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3212 || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3213 || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3214 return 0;
3216 /* Accept any base register. SP only in SImode or larger. */
3217 else if (thumb_base_register_rtx_p (x, mode, strict_p))
3218 return 1;
3220 /* This is PC relative data before arm_reorg runs. */
3221 else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3222 && GET_CODE (x) == SYMBOL_REF
3223 && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3224 return 1;
3226 /* This is PC relative data after arm_reorg runs. */
3227 else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3228 && (GET_CODE (x) == LABEL_REF
3229 || (GET_CODE (x) == CONST
3230 && GET_CODE (XEXP (x, 0)) == PLUS
3231 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3232 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3233 return 1;
3235 /* Post-inc indexing only supported for SImode and larger. */
3236 else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3237 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3238 return 1;
3240 else if (GET_CODE (x) == PLUS)
3242 /* REG+REG address can be any two index registers. */
3243 /* We disallow FRAME+REG addressing since we know that FRAME
3244 will be replaced with STACK, and SP relative addressing only
3245 permits SP+OFFSET. */
3246 if (GET_MODE_SIZE (mode) <= 4
3247 && XEXP (x, 0) != frame_pointer_rtx
3248 && XEXP (x, 1) != frame_pointer_rtx
3249 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3250 && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3251 return 1;
3253 /* REG+const has 5-7 bit offset for non-SP registers. */
3254 else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3255 || XEXP (x, 0) == arg_pointer_rtx)
3256 && GET_CODE (XEXP (x, 1)) == CONST_INT
3257 && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3258 return 1;
3260 /* REG+const has 10 bit offset for SP, but only SImode and
3261 larger is supported. */
3262 /* ??? Should probably check for DI/DFmode overflow here
3263 just like GO_IF_LEGITIMATE_OFFSET does. */
3264 else if (GET_CODE (XEXP (x, 0)) == REG
3265 && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3266 && GET_MODE_SIZE (mode) >= 4
3267 && GET_CODE (XEXP (x, 1)) == CONST_INT
3268 && INTVAL (XEXP (x, 1)) >= 0
3269 && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3270 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3271 return 1;
3273 else if (GET_CODE (XEXP (x, 0)) == REG
3274 && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3275 && GET_MODE_SIZE (mode) >= 4
3276 && GET_CODE (XEXP (x, 1)) == CONST_INT
3277 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3278 return 1;
3281 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3282 && GET_MODE_SIZE (mode) == 4
3283 && GET_CODE (x) == SYMBOL_REF
3284 && CONSTANT_POOL_ADDRESS_P (x)
3285 && !(flag_pic
3286 && symbol_mentioned_p (get_pool_constant (x))))
3287 return 1;
3289 return 0;
3292 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3293 instruction of mode MODE. */
3295 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3297 switch (GET_MODE_SIZE (mode))
3299 case 1:
3300 return val >= 0 && val < 32;
3302 case 2:
3303 return val >= 0 && val < 64 && (val & 1) == 0;
3305 default:
3306 return (val >= 0
3307 && (val + GET_MODE_SIZE (mode)) <= 128
3308 && (val & 3) == 0);
3312 /* Try machine-dependent ways of modifying an illegitimate address
3313 to be legitimate. If we find one, return the new, valid address. */
3315 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3317 if (GET_CODE (x) == PLUS)
3319 rtx xop0 = XEXP (x, 0);
3320 rtx xop1 = XEXP (x, 1);
3322 if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3323 xop0 = force_reg (SImode, xop0);
3325 if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3326 xop1 = force_reg (SImode, xop1);
3328 if (ARM_BASE_REGISTER_RTX_P (xop0)
3329 && GET_CODE (xop1) == CONST_INT)
3331 HOST_WIDE_INT n, low_n;
3332 rtx base_reg, val;
3333 n = INTVAL (xop1);
3335 /* VFP addressing modes actually allow greater offsets, but for
3336 now we just stick with the lowest common denominator. */
3337 if (mode == DImode
3338 || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3340 low_n = n & 0x0f;
3341 n &= ~0x0f;
3342 if (low_n > 4)
3344 n += 16;
3345 low_n -= 16;
3348 else
3350 low_n = ((mode) == TImode ? 0
3351 : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3352 n -= low_n;
3355 base_reg = gen_reg_rtx (SImode);
3356 val = force_operand (gen_rtx_PLUS (SImode, xop0,
3357 GEN_INT (n)), NULL_RTX);
3358 emit_move_insn (base_reg, val);
3359 x = (low_n == 0 ? base_reg
3360 : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3362 else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3363 x = gen_rtx_PLUS (SImode, xop0, xop1);
3366 /* XXX We don't allow MINUS any more -- see comment in
3367 arm_legitimate_address_p (). */
3368 else if (GET_CODE (x) == MINUS)
3370 rtx xop0 = XEXP (x, 0);
3371 rtx xop1 = XEXP (x, 1);
3373 if (CONSTANT_P (xop0))
3374 xop0 = force_reg (SImode, xop0);
3376 if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3377 xop1 = force_reg (SImode, xop1);
3379 if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3380 x = gen_rtx_MINUS (SImode, xop0, xop1);
3383 if (flag_pic)
3385 /* We need to find and carefully transform any SYMBOL and LABEL
3386 references; so go back to the original address expression. */
3387 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3389 if (new_x != orig_x)
3390 x = new_x;
3393 return x;
3397 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3398 to be legitimate. If we find one, return the new, valid address. */
3400 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3402 if (GET_CODE (x) == PLUS
3403 && GET_CODE (XEXP (x, 1)) == CONST_INT
3404 && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3405 || INTVAL (XEXP (x, 1)) < 0))
3407 rtx xop0 = XEXP (x, 0);
3408 rtx xop1 = XEXP (x, 1);
3409 HOST_WIDE_INT offset = INTVAL (xop1);
3411 /* Try and fold the offset into a biasing of the base register and
3412 then offsetting that. Don't do this when optimizing for space
3413 since it can cause too many CSEs. */
3414 if (optimize_size && offset >= 0
3415 && offset < 256 + 31 * GET_MODE_SIZE (mode))
3417 HOST_WIDE_INT delta;
3419 if (offset >= 256)
3420 delta = offset - (256 - GET_MODE_SIZE (mode));
3421 else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3422 delta = 31 * GET_MODE_SIZE (mode);
3423 else
3424 delta = offset & (~31 * GET_MODE_SIZE (mode));
3426 xop0 = force_operand (plus_constant (xop0, offset - delta),
3427 NULL_RTX);
3428 x = plus_constant (xop0, delta);
3430 else if (offset < 0 && offset > -256)
3431 /* Small negative offsets are best done with a subtract before the
3432 dereference, forcing these into a register normally takes two
3433 instructions. */
3434 x = force_operand (x, NULL_RTX);
3435 else
3437 /* For the remaining cases, force the constant into a register. */
3438 xop1 = force_reg (SImode, xop1);
3439 x = gen_rtx_PLUS (SImode, xop0, xop1);
3442 else if (GET_CODE (x) == PLUS
3443 && s_register_operand (XEXP (x, 1), SImode)
3444 && !s_register_operand (XEXP (x, 0), SImode))
3446 rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3448 x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3451 if (flag_pic)
3453 /* We need to find and carefully transform any SYMBOL and LABEL
3454 references; so go back to the original address expression. */
3455 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3457 if (new_x != orig_x)
3458 x = new_x;
3461 return x;
3466 #define REG_OR_SUBREG_REG(X) \
3467 (GET_CODE (X) == REG \
3468 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3470 #define REG_OR_SUBREG_RTX(X) \
3471 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3473 #ifndef COSTS_N_INSNS
3474 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3475 #endif
3476 static inline int
3477 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3479 enum machine_mode mode = GET_MODE (x);
3481 switch (code)
3483 case ASHIFT:
3484 case ASHIFTRT:
3485 case LSHIFTRT:
3486 case ROTATERT:
3487 case PLUS:
3488 case MINUS:
3489 case COMPARE:
3490 case NEG:
3491 case NOT:
3492 return COSTS_N_INSNS (1);
3494 case MULT:
3495 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3497 int cycles = 0;
3498 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3500 while (i)
3502 i >>= 2;
3503 cycles++;
3505 return COSTS_N_INSNS (2) + cycles;
3507 return COSTS_N_INSNS (1) + 16;
3509 case SET:
3510 return (COSTS_N_INSNS (1)
3511 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
3512 + GET_CODE (SET_DEST (x)) == MEM));
3514 case CONST_INT:
3515 if (outer == SET)
3517 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
3518 return 0;
3519 if (thumb_shiftable_const (INTVAL (x)))
3520 return COSTS_N_INSNS (2);
3521 return COSTS_N_INSNS (3);
3523 else if ((outer == PLUS || outer == COMPARE)
3524 && INTVAL (x) < 256 && INTVAL (x) > -256)
3525 return 0;
3526 else if (outer == AND
3527 && INTVAL (x) < 256 && INTVAL (x) >= -256)
3528 return COSTS_N_INSNS (1);
3529 else if (outer == ASHIFT || outer == ASHIFTRT
3530 || outer == LSHIFTRT)
3531 return 0;
3532 return COSTS_N_INSNS (2);
3534 case CONST:
3535 case CONST_DOUBLE:
3536 case LABEL_REF:
3537 case SYMBOL_REF:
3538 return COSTS_N_INSNS (3);
3540 case UDIV:
3541 case UMOD:
3542 case DIV:
3543 case MOD:
3544 return 100;
3546 case TRUNCATE:
3547 return 99;
3549 case AND:
3550 case XOR:
3551 case IOR:
3552 /* XXX guess. */
3553 return 8;
3555 case ADDRESSOF:
3556 case MEM:
3557 /* XXX another guess. */
3558 /* Memory costs quite a lot for the first word, but subsequent words
3559 load at the equivalent of a single insn each. */
3560 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3561 + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3562 ? 4 : 0));
3564 case IF_THEN_ELSE:
3565 /* XXX a guess. */
3566 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3567 return 14;
3568 return 2;
3570 case ZERO_EXTEND:
3571 /* XXX still guessing. */
3572 switch (GET_MODE (XEXP (x, 0)))
3574 case QImode:
3575 return (1 + (mode == DImode ? 4 : 0)
3576 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3578 case HImode:
3579 return (4 + (mode == DImode ? 4 : 0)
3580 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3582 case SImode:
3583 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3585 default:
3586 return 99;
3589 default:
3590 return 99;
3595 /* Worker routine for arm_rtx_costs. */
3596 static inline int
3597 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3599 enum machine_mode mode = GET_MODE (x);
3600 enum rtx_code subcode;
3601 int extra_cost;
3603 switch (code)
3605 case MEM:
3606 /* Memory costs quite a lot for the first word, but subsequent words
3607 load at the equivalent of a single insn each. */
3608 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3609 + (GET_CODE (x) == SYMBOL_REF
3610 && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3612 case DIV:
3613 case MOD:
3614 case UDIV:
3615 case UMOD:
3616 return optimize_size ? COSTS_N_INSNS (2) : 100;
3618 case ROTATE:
3619 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3620 return 4;
3621 /* Fall through */
3622 case ROTATERT:
3623 if (mode != SImode)
3624 return 8;
3625 /* Fall through */
3626 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3627 if (mode == DImode)
3628 return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3629 + ((GET_CODE (XEXP (x, 0)) == REG
3630 || (GET_CODE (XEXP (x, 0)) == SUBREG
3631 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3632 ? 0 : 8));
3633 return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3634 || (GET_CODE (XEXP (x, 0)) == SUBREG
3635 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3636 ? 0 : 4)
3637 + ((GET_CODE (XEXP (x, 1)) == REG
3638 || (GET_CODE (XEXP (x, 1)) == SUBREG
3639 && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3640 || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3641 ? 0 : 4));
3643 case MINUS:
3644 if (mode == DImode)
3645 return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3646 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3647 || (GET_CODE (XEXP (x, 0)) == CONST_INT
3648 && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3649 ? 0 : 8));
3651 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3652 return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3653 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3654 && arm_const_double_rtx (XEXP (x, 1))))
3655 ? 0 : 8)
3656 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3657 || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3658 && arm_const_double_rtx (XEXP (x, 0))))
3659 ? 0 : 8));
3661 if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3662 && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3663 && REG_OR_SUBREG_REG (XEXP (x, 1))))
3664 || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3665 || subcode == ASHIFTRT || subcode == LSHIFTRT
3666 || subcode == ROTATE || subcode == ROTATERT
3667 || (subcode == MULT
3668 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3669 && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3670 (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
3671 && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
3672 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
3673 || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
3674 && REG_OR_SUBREG_REG (XEXP (x, 0))))
3675 return 1;
3676 /* Fall through */
3678 case PLUS:
3679 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3680 return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3681 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3682 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3683 && arm_const_double_rtx (XEXP (x, 1))))
3684 ? 0 : 8));
3686 /* Fall through */
3687 case AND: case XOR: case IOR:
3688 extra_cost = 0;
3690 /* Normally the frame registers will be spilt into reg+const during
3691 reload, so it is a bad idea to combine them with other instructions,
3692 since then they might not be moved outside of loops. As a compromise
3693 we allow integration with ops that have a constant as their second
3694 operand. */
3695 if ((REG_OR_SUBREG_REG (XEXP (x, 0))
3696 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
3697 && GET_CODE (XEXP (x, 1)) != CONST_INT)
3698 || (REG_OR_SUBREG_REG (XEXP (x, 0))
3699 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
3700 extra_cost = 4;
3702 if (mode == DImode)
3703 return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3704 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3705 || (GET_CODE (XEXP (x, 1)) == CONST_INT
3706 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3707 ? 0 : 8));
3709 if (REG_OR_SUBREG_REG (XEXP (x, 0)))
3710 return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
3711 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3712 || (GET_CODE (XEXP (x, 1)) == CONST_INT
3713 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3714 ? 0 : 4));
3716 else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
3717 return (1 + extra_cost
3718 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
3719 || subcode == LSHIFTRT || subcode == ASHIFTRT
3720 || subcode == ROTATE || subcode == ROTATERT
3721 || (subcode == MULT
3722 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3723 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
3724 (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
3725 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
3726 && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
3727 || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
3728 ? 0 : 4));
3730 return 8;
3732 case MULT:
3733 /* This should have been handled by the CPU specific routines. */
3734 abort ();
3736 case TRUNCATE:
3737 if (arm_arch3m && mode == SImode
3738 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
3739 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
3740 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
3741 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
3742 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
3743 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
3744 return 8;
3745 return 99;
3747 case NEG:
3748 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3749 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
3750 /* Fall through */
3751 case NOT:
3752 if (mode == DImode)
3753 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3755 return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3757 case IF_THEN_ELSE:
3758 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3759 return 14;
3760 return 2;
3762 case COMPARE:
3763 return 1;
3765 case ABS:
3766 return 4 + (mode == DImode ? 4 : 0);
3768 case SIGN_EXTEND:
3769 if (GET_MODE (XEXP (x, 0)) == QImode)
3770 return (4 + (mode == DImode ? 4 : 0)
3771 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3772 /* Fall through */
3773 case ZERO_EXTEND:
3774 switch (GET_MODE (XEXP (x, 0)))
3776 case QImode:
3777 return (1 + (mode == DImode ? 4 : 0)
3778 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3780 case HImode:
3781 return (4 + (mode == DImode ? 4 : 0)
3782 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3784 case SImode:
3785 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3787 case V8QImode:
3788 case V4HImode:
3789 case V2SImode:
3790 case V4QImode:
3791 case V2HImode:
3792 return 1;
3794 default:
3795 break;
3797 abort ();
3799 case CONST_INT:
3800 if (const_ok_for_arm (INTVAL (x)))
3801 return outer == SET ? 2 : -1;
3802 else if (outer == AND
3803 && const_ok_for_arm (~INTVAL (x)))
3804 return -1;
3805 else if ((outer == COMPARE
3806 || outer == PLUS || outer == MINUS)
3807 && const_ok_for_arm (-INTVAL (x)))
3808 return -1;
3809 else
3810 return 5;
3812 case CONST:
3813 case LABEL_REF:
3814 case SYMBOL_REF:
3815 return 6;
3817 case CONST_DOUBLE:
3818 if (arm_const_double_rtx (x))
3819 return outer == SET ? 2 : -1;
3820 else if ((outer == COMPARE || outer == PLUS)
3821 && neg_const_double_rtx_ok_for_fpa (x))
3822 return -1;
3823 return 7;
3825 default:
3826 return 99;
3830 /* RTX costs for cores with a slow MUL implementation. */
3832 static bool
3833 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
3835 enum machine_mode mode = GET_MODE (x);
3837 if (TARGET_THUMB)
3839 *total = thumb_rtx_costs (x, code, outer_code);
3840 return true;
3843 switch (code)
3845 case MULT:
3846 if (GET_MODE_CLASS (mode) == MODE_FLOAT
3847 || mode == DImode)
3849 *total = 30;
3850 return true;
3853 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3855 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3856 & (unsigned HOST_WIDE_INT) 0xffffffff);
3857 int cost, const_ok = const_ok_for_arm (i);
3858 int j, booth_unit_size;
3860 /* Tune as appropriate. */
3861 cost = const_ok ? 4 : 8;
3862 booth_unit_size = 2;
3863 for (j = 0; i && j < 32; j += booth_unit_size)
3865 i >>= booth_unit_size;
3866 cost += 2;
3869 *total = cost;
3870 return true;
3873 *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
3874 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
3875 return true;
3877 default:
3878 *total = arm_rtx_costs_1 (x, code, outer_code);
3879 return true;
3884 /* RTX cost for cores with a fast multiply unit (M variants). */
3886 static bool
3887 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
3889 enum machine_mode mode = GET_MODE (x);
3891 if (TARGET_THUMB)
3893 *total = thumb_rtx_costs (x, code, outer_code);
3894 return true;
3897 switch (code)
3899 case MULT:
3900 /* There is no point basing this on the tuning, since it is always the
3901 fast variant if it exists at all. */
3902 if (mode == DImode
3903 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
3904 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
3905 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
3907 *total = 8;
3908 return true;
3912 if (GET_MODE_CLASS (mode) == MODE_FLOAT
3913 || mode == DImode)
3915 *total = 30;
3916 return true;
3919 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3921 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3922 & (unsigned HOST_WIDE_INT) 0xffffffff);
3923 int cost, const_ok = const_ok_for_arm (i);
3924 int j, booth_unit_size;
3926 /* Tune as appropriate. */
3927 cost = const_ok ? 4 : 8;
3928 booth_unit_size = 8;
3929 for (j = 0; i && j < 32; j += booth_unit_size)
3931 i >>= booth_unit_size;
3932 cost += 2;
3935 *total = cost;
3936 return true;
3939 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
3940 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
3941 return true;
3943 default:
3944 *total = arm_rtx_costs_1 (x, code, outer_code);
3945 return true;
3950 /* RTX cost for XScale CPUs. */
3952 static bool
3953 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
3955 enum machine_mode mode = GET_MODE (x);
3957 if (TARGET_THUMB)
3959 *total = thumb_rtx_costs (x, code, outer_code);
3960 return true;
3963 switch (code)
3965 case MULT:
3966 /* There is no point basing this on the tuning, since it is always the
3967 fast variant if it exists at all. */
3968 if (mode == DImode
3969 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
3970 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
3971 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
3973 *total = 8;
3974 return true;
3978 if (GET_MODE_CLASS (mode) == MODE_FLOAT
3979 || mode == DImode)
3981 *total = 30;
3982 return true;
3985 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3987 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3988 & (unsigned HOST_WIDE_INT) 0xffffffff);
3989 int cost, const_ok = const_ok_for_arm (i);
3990 unsigned HOST_WIDE_INT masked_const;
3992 /* The cost will be related to two insns.
3993 First a load of the constant (MOV or LDR), then a multiply. */
3994 cost = 2;
3995 if (! const_ok)
3996 cost += 1; /* LDR is probably more expensive because
3997 of longer result latency. */
3998 masked_const = i & 0xffff8000;
3999 if (masked_const != 0 && masked_const != 0xffff8000)
4001 masked_const = i & 0xf8000000;
4002 if (masked_const == 0 || masked_const == 0xf8000000)
4003 cost += 1;
4004 else
4005 cost += 2;
4007 *total = cost;
4008 return true;
4011 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4012 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4013 return true;
4015 default:
4016 *total = arm_rtx_costs_1 (x, code, outer_code);
4017 return true;
4022 /* RTX costs for 9e (and later) cores. */
4024 static bool
4025 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4027 enum machine_mode mode = GET_MODE (x);
4028 int nonreg_cost;
4029 int cost;
4031 if (TARGET_THUMB)
4033 switch (code)
4035 case MULT:
4036 *total = COSTS_N_INSNS (3);
4037 return true;
4039 default:
4040 *total = thumb_rtx_costs (x, code, outer_code);
4041 return true;
4045 switch (code)
4047 case MULT:
4048 /* There is no point basing this on the tuning, since it is always the
4049 fast variant if it exists at all. */
4050 if (mode == DImode
4051 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4052 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4053 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4055 *total = 3;
4056 return true;
4060 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4062 *total = 30;
4063 return true;
4065 if (mode == DImode)
4067 cost = 7;
4068 nonreg_cost = 8;
4070 else
4072 cost = 2;
4073 nonreg_cost = 4;
4077 *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4078 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4079 return true;
4081 default:
4082 *total = arm_rtx_costs_1 (x, code, outer_code);
4083 return true;
4086 /* All address computations that can be done are free, but rtx cost returns
4087 the same for practically all of them. So we weight the different types
4088 of address here in the order (most pref first):
4089 PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL. */
4090 static inline int
4091 arm_arm_address_cost (rtx x)
4093 enum rtx_code c = GET_CODE (x);
4095 if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4096 return 0;
4097 if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4098 return 10;
4100 if (c == PLUS || c == MINUS)
4102 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4103 return 2;
4105 if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4106 return 3;
4108 return 4;
4111 return 6;
4114 static inline int
4115 arm_thumb_address_cost (rtx x)
4117 enum rtx_code c = GET_CODE (x);
4119 if (c == REG)
4120 return 1;
4121 if (c == PLUS
4122 && GET_CODE (XEXP (x, 0)) == REG
4123 && GET_CODE (XEXP (x, 1)) == CONST_INT)
4124 return 1;
4126 return 2;
4129 static int
4130 arm_address_cost (rtx x)
4132 return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4135 static int
4136 arm_use_dfa_pipeline_interface (void)
4138 return true;
4141 static int
4142 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4144 rtx i_pat, d_pat;
4146 /* Some true dependencies can have a higher cost depending
4147 on precisely how certain input operands are used. */
4148 if (arm_tune_xscale
4149 && REG_NOTE_KIND (link) == 0
4150 && recog_memoized (insn) >= 0
4151 && recog_memoized (dep) >= 0)
4153 int shift_opnum = get_attr_shift (insn);
4154 enum attr_type attr_type = get_attr_type (dep);
4156 /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4157 operand for INSN. If we have a shifted input operand and the
4158 instruction we depend on is another ALU instruction, then we may
4159 have to account for an additional stall. */
4160 if (shift_opnum != 0
4161 && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4163 rtx shifted_operand;
4164 int opno;
4166 /* Get the shifted operand. */
4167 extract_insn (insn);
4168 shifted_operand = recog_data.operand[shift_opnum];
4170 /* Iterate over all the operands in DEP. If we write an operand
4171 that overlaps with SHIFTED_OPERAND, then we have increase the
4172 cost of this dependency. */
4173 extract_insn (dep);
4174 preprocess_constraints ();
4175 for (opno = 0; opno < recog_data.n_operands; opno++)
4177 /* We can ignore strict inputs. */
4178 if (recog_data.operand_type[opno] == OP_IN)
4179 continue;
4181 if (reg_overlap_mentioned_p (recog_data.operand[opno],
4182 shifted_operand))
4183 return 2;
4188 /* XXX This is not strictly true for the FPA. */
4189 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4190 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4191 return 0;
4193 /* Call insns don't incur a stall, even if they follow a load. */
4194 if (REG_NOTE_KIND (link) == 0
4195 && GET_CODE (insn) == CALL_INSN)
4196 return 1;
4198 if ((i_pat = single_set (insn)) != NULL
4199 && GET_CODE (SET_SRC (i_pat)) == MEM
4200 && (d_pat = single_set (dep)) != NULL
4201 && GET_CODE (SET_DEST (d_pat)) == MEM)
4203 rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4204 /* This is a load after a store, there is no conflict if the load reads
4205 from a cached area. Assume that loads from the stack, and from the
4206 constant pool are cached, and that others will miss. This is a
4207 hack. */
4209 if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4210 || reg_mentioned_p (stack_pointer_rtx, src_mem)
4211 || reg_mentioned_p (frame_pointer_rtx, src_mem)
4212 || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4213 return 1;
4216 return cost;
4219 static int fp_consts_inited = 0;
4221 /* Only zero is valid for VFP. Other values are also valid for FPA. */
4222 static const char * const strings_fp[8] =
4224 "0", "1", "2", "3",
4225 "4", "5", "0.5", "10"
4228 static REAL_VALUE_TYPE values_fp[8];
4230 static void
4231 init_fp_table (void)
4233 int i;
4234 REAL_VALUE_TYPE r;
4236 if (TARGET_VFP)
4237 fp_consts_inited = 1;
4238 else
4239 fp_consts_inited = 8;
4241 for (i = 0; i < fp_consts_inited; i++)
4243 r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4244 values_fp[i] = r;
4248 /* Return TRUE if rtx X is a valid immediate FP constant. */
4250 arm_const_double_rtx (rtx x)
4252 REAL_VALUE_TYPE r;
4253 int i;
4255 if (!fp_consts_inited)
4256 init_fp_table ();
4258 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4259 if (REAL_VALUE_MINUS_ZERO (r))
4260 return 0;
4262 for (i = 0; i < fp_consts_inited; i++)
4263 if (REAL_VALUES_EQUAL (r, values_fp[i]))
4264 return 1;
4266 return 0;
4269 /* Return TRUE if rtx X is a valid immediate FPA constant. */
4271 neg_const_double_rtx_ok_for_fpa (rtx x)
4273 REAL_VALUE_TYPE r;
4274 int i;
4276 if (!fp_consts_inited)
4277 init_fp_table ();
4279 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4280 r = REAL_VALUE_NEGATE (r);
4281 if (REAL_VALUE_MINUS_ZERO (r))
4282 return 0;
4284 for (i = 0; i < 8; i++)
4285 if (REAL_VALUES_EQUAL (r, values_fp[i]))
4286 return 1;
4288 return 0;
4291 /* Predicates for `match_operand' and `match_operator'. */
4293 /* s_register_operand is the same as register_operand, but it doesn't accept
4294 (SUBREG (MEM)...).
4296 This function exists because at the time it was put in it led to better
4297 code. SUBREG(MEM) always needs a reload in the places where
4298 s_register_operand is used, and this seemed to lead to excessive
4299 reloading. */
4301 s_register_operand (rtx op, enum machine_mode mode)
4303 if (GET_MODE (op) != mode && mode != VOIDmode)
4304 return 0;
4306 if (GET_CODE (op) == SUBREG)
4307 op = SUBREG_REG (op);
4309 /* We don't consider registers whose class is NO_REGS
4310 to be a register operand. */
4311 /* XXX might have to check for lo regs only for thumb ??? */
4312 return (GET_CODE (op) == REG
4313 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4314 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
4317 /* A hard register operand (even before reload. */
4319 arm_hard_register_operand (rtx op, enum machine_mode mode)
4321 if (GET_MODE (op) != mode && mode != VOIDmode)
4322 return 0;
4324 return (GET_CODE (op) == REG
4325 && REGNO (op) < FIRST_PSEUDO_REGISTER);
4328 /* An arm register operand. */
4330 arm_general_register_operand (rtx op, enum machine_mode mode)
4332 if (GET_MODE (op) != mode && mode != VOIDmode)
4333 return 0;
4335 if (GET_CODE (op) == SUBREG)
4336 op = SUBREG_REG (op);
4338 return (GET_CODE (op) == REG
4339 && (REGNO (op) <= LAST_ARM_REGNUM
4340 || REGNO (op) >= FIRST_PSEUDO_REGISTER));
4343 /* Only accept reg, subreg(reg), const_int. */
4345 reg_or_int_operand (rtx op, enum machine_mode mode)
4347 if (GET_CODE (op) == CONST_INT)
4348 return 1;
4350 if (GET_MODE (op) != mode && mode != VOIDmode)
4351 return 0;
4353 if (GET_CODE (op) == SUBREG)
4354 op = SUBREG_REG (op);
4356 /* We don't consider registers whose class is NO_REGS
4357 to be a register operand. */
4358 return (GET_CODE (op) == REG
4359 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4360 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
4363 /* Return 1 if OP is an item in memory, given that we are in reload. */
4365 arm_reload_memory_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4367 int regno = true_regnum (op);
4369 return (!CONSTANT_P (op)
4370 && (regno == -1
4371 || (GET_CODE (op) == REG
4372 && REGNO (op) >= FIRST_PSEUDO_REGISTER)));
4375 /* Return TRUE for valid operands for the rhs of an ARM instruction. */
4377 arm_rhs_operand (rtx op, enum machine_mode mode)
4379 return (s_register_operand (op, mode)
4380 || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op))));
4383 /* Return TRUE for valid operands for the
4384 rhs of an ARM instruction, or a load. */
4386 arm_rhsm_operand (rtx op, enum machine_mode mode)
4388 return (s_register_operand (op, mode)
4389 || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op)))
4390 || memory_operand (op, mode));
4393 /* Return TRUE for valid operands for the rhs of an ARM instruction, or if a
4394 constant that is valid when negated. */
4396 arm_add_operand (rtx op, enum machine_mode mode)
4398 if (TARGET_THUMB)
4399 return thumb_cmp_operand (op, mode);
4401 return (s_register_operand (op, mode)
4402 || (GET_CODE (op) == CONST_INT
4403 && (const_ok_for_arm (INTVAL (op))
4404 || const_ok_for_arm (-INTVAL (op)))));
4407 /* Return TRUE for valid ARM constants (or when valid if negated). */
4409 arm_addimm_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4411 return (GET_CODE (op) == CONST_INT
4412 && (const_ok_for_arm (INTVAL (op))
4413 || const_ok_for_arm (-INTVAL (op))));
4417 arm_not_operand (rtx op, enum machine_mode mode)
4419 return (s_register_operand (op, mode)
4420 || (GET_CODE (op) == CONST_INT
4421 && (const_ok_for_arm (INTVAL (op))
4422 || const_ok_for_arm (~INTVAL (op)))));
4425 /* Return TRUE if the operand is a memory reference which contains an
4426 offsettable address. */
4428 offsettable_memory_operand (rtx op, enum machine_mode mode)
4430 if (mode == VOIDmode)
4431 mode = GET_MODE (op);
4433 return (mode == GET_MODE (op)
4434 && GET_CODE (op) == MEM
4435 && offsettable_address_p (reload_completed | reload_in_progress,
4436 mode, XEXP (op, 0)));
4439 /* Return TRUE if the operand is a memory reference which is, or can be
4440 made word aligned by adjusting the offset. */
4442 alignable_memory_operand (rtx op, enum machine_mode mode)
4444 rtx reg;
4446 if (mode == VOIDmode)
4447 mode = GET_MODE (op);
4449 if (mode != GET_MODE (op) || GET_CODE (op) != MEM)
4450 return 0;
4452 op = XEXP (op, 0);
4454 return ((GET_CODE (reg = op) == REG
4455 || (GET_CODE (op) == SUBREG
4456 && GET_CODE (reg = SUBREG_REG (op)) == REG)
4457 || (GET_CODE (op) == PLUS
4458 && GET_CODE (XEXP (op, 1)) == CONST_INT
4459 && (GET_CODE (reg = XEXP (op, 0)) == REG
4460 || (GET_CODE (XEXP (op, 0)) == SUBREG
4461 && GET_CODE (reg = SUBREG_REG (XEXP (op, 0))) == REG))))
4462 && REGNO_POINTER_ALIGN (REGNO (reg)) >= 32);
4465 /* Similar to s_register_operand, but does not allow hard integer
4466 registers. */
4468 f_register_operand (rtx op, enum machine_mode mode)
4470 if (GET_MODE (op) != mode && mode != VOIDmode)
4471 return 0;
4473 if (GET_CODE (op) == SUBREG)
4474 op = SUBREG_REG (op);
4476 /* We don't consider registers whose class is NO_REGS
4477 to be a register operand. */
4478 return (GET_CODE (op) == REG
4479 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4480 || REGNO_REG_CLASS (REGNO (op)) == FPA_REGS));
4483 /* Return TRUE for valid operands for the rhs of an floating point insns.
4484 Allows regs or certain consts on FPA, just regs for everything else. */
4486 arm_float_rhs_operand (rtx op, enum machine_mode mode)
4488 if (s_register_operand (op, mode))
4489 return TRUE;
4491 if (GET_MODE (op) != mode && mode != VOIDmode)
4492 return FALSE;
4494 if (TARGET_FPA && GET_CODE (op) == CONST_DOUBLE)
4495 return arm_const_double_rtx (op);
4497 return FALSE;
4501 arm_float_add_operand (rtx op, enum machine_mode mode)
4503 if (s_register_operand (op, mode))
4504 return TRUE;
4506 if (GET_MODE (op) != mode && mode != VOIDmode)
4507 return FALSE;
4509 if (TARGET_FPA && GET_CODE (op) == CONST_DOUBLE)
4510 return (arm_const_double_rtx (op)
4511 || neg_const_double_rtx_ok_for_fpa (op));
4513 return FALSE;
4517 /* Return TRUE if OP is suitable for the rhs of a floating point comparison.
4518 Depends which fpu we are targeting. */
4521 arm_float_compare_operand (rtx op, enum machine_mode mode)
4523 if (TARGET_VFP)
4524 return vfp_compare_operand (op, mode);
4525 else
4526 return arm_float_rhs_operand (op, mode);
4530 /* Return nonzero if OP is a valid Cirrus memory address pattern. */
4532 cirrus_memory_offset (rtx op)
4534 /* Reject eliminable registers. */
4535 if (! (reload_in_progress || reload_completed)
4536 && ( reg_mentioned_p (frame_pointer_rtx, op)
4537 || reg_mentioned_p (arg_pointer_rtx, op)
4538 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4539 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4540 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4541 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4542 return 0;
4544 if (GET_CODE (op) == MEM)
4546 rtx ind;
4548 ind = XEXP (op, 0);
4550 /* Match: (mem (reg)). */
4551 if (GET_CODE (ind) == REG)
4552 return 1;
4554 /* Match:
4555 (mem (plus (reg)
4556 (const))). */
4557 if (GET_CODE (ind) == PLUS
4558 && GET_CODE (XEXP (ind, 0)) == REG
4559 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4560 && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4561 return 1;
4564 return 0;
4568 arm_extendqisi_mem_op (rtx op, enum machine_mode mode)
4570 if (!memory_operand (op, mode))
4571 return 0;
4573 return arm_legitimate_address_p (mode, XEXP (op, 0), SIGN_EXTEND, 0);
4576 /* Return nonzero if OP is a Cirrus or general register. */
4578 cirrus_register_operand (rtx op, enum machine_mode mode)
4580 if (GET_MODE (op) != mode && mode != VOIDmode)
4581 return FALSE;
4583 if (GET_CODE (op) == SUBREG)
4584 op = SUBREG_REG (op);
4586 return (GET_CODE (op) == REG
4587 && (REGNO_REG_CLASS (REGNO (op)) == CIRRUS_REGS
4588 || REGNO_REG_CLASS (REGNO (op)) == GENERAL_REGS));
4591 /* Return nonzero if OP is a cirrus FP register. */
4593 cirrus_fp_register (rtx op, enum machine_mode mode)
4595 if (GET_MODE (op) != mode && mode != VOIDmode)
4596 return FALSE;
4598 if (GET_CODE (op) == SUBREG)
4599 op = SUBREG_REG (op);
4601 return (GET_CODE (op) == REG
4602 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4603 || REGNO_REG_CLASS (REGNO (op)) == CIRRUS_REGS));
4606 /* Return nonzero if OP is a 6bit constant (0..63). */
4608 cirrus_shift_const (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4610 return (GET_CODE (op) == CONST_INT
4611 && INTVAL (op) >= 0
4612 && INTVAL (op) < 64);
4616 /* Return TRUE if OP is a valid VFP memory address pattern. */
4617 /* Copied from cirrus_memory_offset but with restricted offset range. */
4620 vfp_mem_operand (rtx op)
4622 /* Reject eliminable registers. */
4624 if (! (reload_in_progress || reload_completed)
4625 && ( reg_mentioned_p (frame_pointer_rtx, op)
4626 || reg_mentioned_p (arg_pointer_rtx, op)
4627 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4628 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4629 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4630 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4631 return FALSE;
4633 /* Constants are converted into offsets from labels. */
4634 if (GET_CODE (op) == MEM)
4636 rtx ind;
4638 ind = XEXP (op, 0);
4640 if (reload_completed
4641 && (GET_CODE (ind) == LABEL_REF
4642 || (GET_CODE (ind) == CONST
4643 && GET_CODE (XEXP (ind, 0)) == PLUS
4644 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4645 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4646 return TRUE;
4648 /* Match: (mem (reg)). */
4649 if (GET_CODE (ind) == REG)
4650 return arm_address_register_rtx_p (ind, 0);
4652 /* Match:
4653 (mem (plus (reg)
4654 (const))). */
4655 if (GET_CODE (ind) == PLUS
4656 && GET_CODE (XEXP (ind, 0)) == REG
4657 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4658 && GET_CODE (XEXP (ind, 1)) == CONST_INT
4659 && INTVAL (XEXP (ind, 1)) > -1024
4660 && INTVAL (XEXP (ind, 1)) < 1024)
4661 return TRUE;
4664 return FALSE;
4668 /* Return TRUE if OP is a REG or constant zero. */
4670 vfp_compare_operand (rtx op, enum machine_mode mode)
4672 if (s_register_operand (op, mode))
4673 return TRUE;
4675 return (GET_CODE (op) == CONST_DOUBLE
4676 && arm_const_double_rtx (op));
4680 /* Return GENERAL_REGS if a scratch register required to reload x to/from
4681 VFP registers. Otherwise return NO_REGS. */
4683 enum reg_class
4684 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4686 if (vfp_mem_operand (x) || s_register_operand (x, mode))
4687 return NO_REGS;
4689 return GENERAL_REGS;
4693 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4694 Use by the Cirrus Maverick code which has to workaround
4695 a hardware bug triggered by such instructions. */
4696 static bool
4697 arm_memory_load_p (rtx insn)
4699 rtx body, lhs, rhs;;
4701 if (insn == NULL_RTX || GET_CODE (insn) != INSN)
4702 return false;
4704 body = PATTERN (insn);
4706 if (GET_CODE (body) != SET)
4707 return false;
4709 lhs = XEXP (body, 0);
4710 rhs = XEXP (body, 1);
4712 lhs = REG_OR_SUBREG_RTX (lhs);
4714 /* If the destination is not a general purpose
4715 register we do not have to worry. */
4716 if (GET_CODE (lhs) != REG
4717 || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
4718 return false;
4720 /* As well as loads from memory we also have to react
4721 to loads of invalid constants which will be turned
4722 into loads from the minipool. */
4723 return (GET_CODE (rhs) == MEM
4724 || GET_CODE (rhs) == SYMBOL_REF
4725 || note_invalid_constants (insn, -1, false));
4728 /* Return TRUE if INSN is a Cirrus instruction. */
4729 static bool
4730 arm_cirrus_insn_p (rtx insn)
4732 enum attr_cirrus attr;
4734 /* get_attr aborts on USE and CLOBBER. */
4735 if (!insn
4736 || GET_CODE (insn) != INSN
4737 || GET_CODE (PATTERN (insn)) == USE
4738 || GET_CODE (PATTERN (insn)) == CLOBBER)
4739 return 0;
4741 attr = get_attr_cirrus (insn);
4743 return attr != CIRRUS_NOT;
4746 /* Cirrus reorg for invalid instruction combinations. */
4747 static void
4748 cirrus_reorg (rtx first)
4750 enum attr_cirrus attr;
4751 rtx body = PATTERN (first);
4752 rtx t;
4753 int nops;
4755 /* Any branch must be followed by 2 non Cirrus instructions. */
4756 if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
4758 nops = 0;
4759 t = next_nonnote_insn (first);
4761 if (arm_cirrus_insn_p (t))
4762 ++ nops;
4764 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4765 ++ nops;
4767 while (nops --)
4768 emit_insn_after (gen_nop (), first);
4770 return;
4773 /* (float (blah)) is in parallel with a clobber. */
4774 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
4775 body = XVECEXP (body, 0, 0);
4777 if (GET_CODE (body) == SET)
4779 rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
4781 /* cfldrd, cfldr64, cfstrd, cfstr64 must
4782 be followed by a non Cirrus insn. */
4783 if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
4785 if (arm_cirrus_insn_p (next_nonnote_insn (first)))
4786 emit_insn_after (gen_nop (), first);
4788 return;
4790 else if (arm_memory_load_p (first))
4792 unsigned int arm_regno;
4794 /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
4795 ldr/cfmv64hr combination where the Rd field is the same
4796 in both instructions must be split with a non Cirrus
4797 insn. Example:
4799 ldr r0, blah
4801 cfmvsr mvf0, r0. */
4803 /* Get Arm register number for ldr insn. */
4804 if (GET_CODE (lhs) == REG)
4805 arm_regno = REGNO (lhs);
4806 else if (GET_CODE (rhs) == REG)
4807 arm_regno = REGNO (rhs);
4808 else
4809 abort ();
4811 /* Next insn. */
4812 first = next_nonnote_insn (first);
4814 if (! arm_cirrus_insn_p (first))
4815 return;
4817 body = PATTERN (first);
4819 /* (float (blah)) is in parallel with a clobber. */
4820 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
4821 body = XVECEXP (body, 0, 0);
4823 if (GET_CODE (body) == FLOAT)
4824 body = XEXP (body, 0);
4826 if (get_attr_cirrus (first) == CIRRUS_MOVE
4827 && GET_CODE (XEXP (body, 1)) == REG
4828 && arm_regno == REGNO (XEXP (body, 1)))
4829 emit_insn_after (gen_nop (), first);
4831 return;
4835 /* get_attr aborts on USE and CLOBBER. */
4836 if (!first
4837 || GET_CODE (first) != INSN
4838 || GET_CODE (PATTERN (first)) == USE
4839 || GET_CODE (PATTERN (first)) == CLOBBER)
4840 return;
4842 attr = get_attr_cirrus (first);
4844 /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
4845 must be followed by a non-coprocessor instruction. */
4846 if (attr == CIRRUS_COMPARE)
4848 nops = 0;
4850 t = next_nonnote_insn (first);
4852 if (arm_cirrus_insn_p (t))
4853 ++ nops;
4855 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4856 ++ nops;
4858 while (nops --)
4859 emit_insn_after (gen_nop (), first);
4861 return;
4865 /* Return nonzero if OP is a constant power of two. */
4867 power_of_two_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4869 if (GET_CODE (op) == CONST_INT)
4871 HOST_WIDE_INT value = INTVAL (op);
4873 return value != 0 && (value & (value - 1)) == 0;
4876 return FALSE;
4879 /* Return TRUE for a valid operand of a DImode operation.
4880 Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
4881 Note that this disallows MEM(REG+REG), but allows
4882 MEM(PRE/POST_INC/DEC(REG)). */
4884 di_operand (rtx op, enum machine_mode mode)
4886 if (s_register_operand (op, mode))
4887 return TRUE;
4889 if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
4890 return FALSE;
4892 if (GET_CODE (op) == SUBREG)
4893 op = SUBREG_REG (op);
4895 switch (GET_CODE (op))
4897 case CONST_DOUBLE:
4898 case CONST_INT:
4899 return TRUE;
4901 case MEM:
4902 return memory_address_p (DImode, XEXP (op, 0));
4904 default:
4905 return FALSE;
4909 /* Like di_operand, but don't accept constants. */
4911 nonimmediate_di_operand (rtx op, enum machine_mode mode)
4913 if (s_register_operand (op, mode))
4914 return TRUE;
4916 if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
4917 return FALSE;
4919 if (GET_CODE (op) == SUBREG)
4920 op = SUBREG_REG (op);
4922 if (GET_CODE (op) == MEM)
4923 return memory_address_p (DImode, XEXP (op, 0));
4925 return FALSE;
4928 /* Return TRUE for a valid operand of a DFmode operation when soft-float.
4929 Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
4930 Note that this disallows MEM(REG+REG), but allows
4931 MEM(PRE/POST_INC/DEC(REG)). */
4933 soft_df_operand (rtx op, enum machine_mode mode)
4935 if (s_register_operand (op, mode))
4936 return TRUE;
4938 if (mode != VOIDmode && GET_MODE (op) != mode)
4939 return FALSE;
4941 if (GET_CODE (op) == SUBREG && CONSTANT_P (SUBREG_REG (op)))
4942 return FALSE;
4944 if (GET_CODE (op) == SUBREG)
4945 op = SUBREG_REG (op);
4947 switch (GET_CODE (op))
4949 case CONST_DOUBLE:
4950 return TRUE;
4952 case MEM:
4953 return memory_address_p (DFmode, XEXP (op, 0));
4955 default:
4956 return FALSE;
4960 /* Like soft_df_operand, but don't accept constants. */
4962 nonimmediate_soft_df_operand (rtx op, enum machine_mode mode)
4964 if (s_register_operand (op, mode))
4965 return TRUE;
4967 if (mode != VOIDmode && GET_MODE (op) != mode)
4968 return FALSE;
4970 if (GET_CODE (op) == SUBREG)
4971 op = SUBREG_REG (op);
4973 if (GET_CODE (op) == MEM)
4974 return memory_address_p (DFmode, XEXP (op, 0));
4975 return FALSE;
4978 /* Return TRUE for valid index operands. */
4980 index_operand (rtx op, enum machine_mode mode)
4982 return (s_register_operand (op, mode)
4983 || (immediate_operand (op, mode)
4984 && (GET_CODE (op) != CONST_INT
4985 || (INTVAL (op) < 4096 && INTVAL (op) > -4096))));
4988 /* Return TRUE for valid shifts by a constant. This also accepts any
4989 power of two on the (somewhat overly relaxed) assumption that the
4990 shift operator in this case was a mult. */
4992 const_shift_operand (rtx op, enum machine_mode mode)
4994 return (power_of_two_operand (op, mode)
4995 || (immediate_operand (op, mode)
4996 && (GET_CODE (op) != CONST_INT
4997 || (INTVAL (op) < 32 && INTVAL (op) > 0))));
5000 /* Return TRUE for arithmetic operators which can be combined with a multiply
5001 (shift). */
5003 shiftable_operator (rtx x, enum machine_mode mode)
5005 enum rtx_code code;
5007 if (GET_MODE (x) != mode)
5008 return FALSE;
5010 code = GET_CODE (x);
5012 return (code == PLUS || code == MINUS
5013 || code == IOR || code == XOR || code == AND);
5016 /* Return TRUE for binary logical operators. */
5018 logical_binary_operator (rtx x, enum machine_mode mode)
5020 enum rtx_code code;
5022 if (GET_MODE (x) != mode)
5023 return FALSE;
5025 code = GET_CODE (x);
5027 return (code == IOR || code == XOR || code == AND);
5030 /* Return TRUE for shift operators. */
5032 shift_operator (rtx x,enum machine_mode mode)
5034 enum rtx_code code;
5036 if (GET_MODE (x) != mode)
5037 return FALSE;
5039 code = GET_CODE (x);
5041 if (code == MULT)
5042 return power_of_two_operand (XEXP (x, 1), mode);
5044 return (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT
5045 || code == ROTATERT);
5048 /* Return TRUE if x is EQ or NE. */
5050 equality_operator (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
5052 return GET_CODE (x) == EQ || GET_CODE (x) == NE;
5055 /* Return TRUE if x is a comparison operator other than LTGT or UNEQ. */
5057 arm_comparison_operator (rtx x, enum machine_mode mode)
5059 return (comparison_operator (x, mode)
5060 && GET_CODE (x) != LTGT
5061 && GET_CODE (x) != UNEQ);
5064 /* Return TRUE for SMIN SMAX UMIN UMAX operators. */
5066 minmax_operator (rtx x, enum machine_mode mode)
5068 enum rtx_code code = GET_CODE (x);
5070 if (GET_MODE (x) != mode)
5071 return FALSE;
5073 return code == SMIN || code == SMAX || code == UMIN || code == UMAX;
5076 /* Return TRUE if this is the condition code register, if we aren't given
5077 a mode, accept any class CCmode register. */
5079 cc_register (rtx x, enum machine_mode mode)
5081 if (mode == VOIDmode)
5083 mode = GET_MODE (x);
5085 if (GET_MODE_CLASS (mode) != MODE_CC)
5086 return FALSE;
5089 if ( GET_MODE (x) == mode
5090 && GET_CODE (x) == REG
5091 && REGNO (x) == CC_REGNUM)
5092 return TRUE;
5094 return FALSE;
5097 /* Return TRUE if this is the condition code register, if we aren't given
5098 a mode, accept any class CCmode register which indicates a dominance
5099 expression. */
5101 dominant_cc_register (rtx x, enum machine_mode mode)
5103 if (mode == VOIDmode)
5105 mode = GET_MODE (x);
5107 if (GET_MODE_CLASS (mode) != MODE_CC)
5108 return FALSE;
5111 if (mode != CC_DNEmode && mode != CC_DEQmode
5112 && mode != CC_DLEmode && mode != CC_DLTmode
5113 && mode != CC_DGEmode && mode != CC_DGTmode
5114 && mode != CC_DLEUmode && mode != CC_DLTUmode
5115 && mode != CC_DGEUmode && mode != CC_DGTUmode)
5116 return FALSE;
5118 return cc_register (x, mode);
5121 /* Return TRUE if X references a SYMBOL_REF. */
5123 symbol_mentioned_p (rtx x)
5125 const char * fmt;
5126 int i;
5128 if (GET_CODE (x) == SYMBOL_REF)
5129 return 1;
5131 fmt = GET_RTX_FORMAT (GET_CODE (x));
5133 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5135 if (fmt[i] == 'E')
5137 int j;
5139 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5140 if (symbol_mentioned_p (XVECEXP (x, i, j)))
5141 return 1;
5143 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5144 return 1;
5147 return 0;
5150 /* Return TRUE if X references a LABEL_REF. */
5152 label_mentioned_p (rtx x)
5154 const char * fmt;
5155 int i;
5157 if (GET_CODE (x) == LABEL_REF)
5158 return 1;
5160 fmt = GET_RTX_FORMAT (GET_CODE (x));
5161 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5163 if (fmt[i] == 'E')
5165 int j;
5167 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5168 if (label_mentioned_p (XVECEXP (x, i, j)))
5169 return 1;
5171 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5172 return 1;
5175 return 0;
5178 enum rtx_code
5179 minmax_code (rtx x)
5181 enum rtx_code code = GET_CODE (x);
5183 if (code == SMAX)
5184 return GE;
5185 else if (code == SMIN)
5186 return LE;
5187 else if (code == UMIN)
5188 return LEU;
5189 else if (code == UMAX)
5190 return GEU;
5192 abort ();
5195 /* Return 1 if memory locations are adjacent. */
5197 adjacent_mem_locations (rtx a, rtx b)
5199 if ((GET_CODE (XEXP (a, 0)) == REG
5200 || (GET_CODE (XEXP (a, 0)) == PLUS
5201 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5202 && (GET_CODE (XEXP (b, 0)) == REG
5203 || (GET_CODE (XEXP (b, 0)) == PLUS
5204 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5206 int val0 = 0, val1 = 0;
5207 int reg0, reg1;
5209 if (GET_CODE (XEXP (a, 0)) == PLUS)
5211 reg0 = REGNO (XEXP (XEXP (a, 0), 0));
5212 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5214 else
5215 reg0 = REGNO (XEXP (a, 0));
5217 if (GET_CODE (XEXP (b, 0)) == PLUS)
5219 reg1 = REGNO (XEXP (XEXP (b, 0), 0));
5220 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5222 else
5223 reg1 = REGNO (XEXP (b, 0));
5225 /* Don't accept any offset that will require multiple
5226 instructions to handle, since this would cause the
5227 arith_adjacentmem pattern to output an overlong sequence. */
5228 if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5229 return 0;
5231 return (reg0 == reg1) && ((val1 - val0) == 4 || (val0 - val1) == 4);
5233 return 0;
5236 /* Return 1 if OP is a load multiple operation. It is known to be
5237 parallel and the first section will be tested. */
5239 load_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
5241 HOST_WIDE_INT count = XVECLEN (op, 0);
5242 int dest_regno;
5243 rtx src_addr;
5244 HOST_WIDE_INT i = 1, base = 0;
5245 rtx elt;
5247 if (count <= 1
5248 || GET_CODE (XVECEXP (op, 0, 0)) != SET)
5249 return 0;
5251 /* Check to see if this might be a write-back. */
5252 if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
5254 i++;
5255 base = 1;
5257 /* Now check it more carefully. */
5258 if (GET_CODE (SET_DEST (elt)) != REG
5259 || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
5260 || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
5261 || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
5262 return 0;
5265 /* Perform a quick check so we don't blow up below. */
5266 if (count <= i
5267 || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
5268 || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != REG
5269 || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != MEM)
5270 return 0;
5272 dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, i - 1)));
5273 src_addr = XEXP (SET_SRC (XVECEXP (op, 0, i - 1)), 0);
5275 for (; i < count; i++)
5277 elt = XVECEXP (op, 0, i);
5279 if (GET_CODE (elt) != SET
5280 || GET_CODE (SET_DEST (elt)) != REG
5281 || GET_MODE (SET_DEST (elt)) != SImode
5282 || REGNO (SET_DEST (elt)) != (unsigned int)(dest_regno + i - base)
5283 || GET_CODE (SET_SRC (elt)) != MEM
5284 || GET_MODE (SET_SRC (elt)) != SImode
5285 || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
5286 || !rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
5287 || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
5288 || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != (i - base) * 4)
5289 return 0;
5292 return 1;
5295 /* Return 1 if OP is a store multiple operation. It is known to be
5296 parallel and the first section will be tested. */
5298 store_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
5300 HOST_WIDE_INT count = XVECLEN (op, 0);
5301 int src_regno;
5302 rtx dest_addr;
5303 HOST_WIDE_INT i = 1, base = 0;
5304 rtx elt;
5306 if (count <= 1
5307 || GET_CODE (XVECEXP (op, 0, 0)) != SET)
5308 return 0;
5310 /* Check to see if this might be a write-back. */
5311 if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
5313 i++;
5314 base = 1;
5316 /* Now check it more carefully. */
5317 if (GET_CODE (SET_DEST (elt)) != REG
5318 || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
5319 || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
5320 || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
5321 return 0;
5324 /* Perform a quick check so we don't blow up below. */
5325 if (count <= i
5326 || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
5327 || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != MEM
5328 || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != REG)
5329 return 0;
5331 src_regno = REGNO (SET_SRC (XVECEXP (op, 0, i - 1)));
5332 dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, i - 1)), 0);
5334 for (; i < count; i++)
5336 elt = XVECEXP (op, 0, i);
5338 if (GET_CODE (elt) != SET
5339 || GET_CODE (SET_SRC (elt)) != REG
5340 || GET_MODE (SET_SRC (elt)) != SImode
5341 || REGNO (SET_SRC (elt)) != (unsigned int)(src_regno + i - base)
5342 || GET_CODE (SET_DEST (elt)) != MEM
5343 || GET_MODE (SET_DEST (elt)) != SImode
5344 || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
5345 || !rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
5346 || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
5347 || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != (i - base) * 4)
5348 return 0;
5351 return 1;
5355 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5356 HOST_WIDE_INT *load_offset)
5358 int unsorted_regs[4];
5359 HOST_WIDE_INT unsorted_offsets[4];
5360 int order[4];
5361 int base_reg = -1;
5362 int i;
5364 /* Can only handle 2, 3, or 4 insns at present,
5365 though could be easily extended if required. */
5366 if (nops < 2 || nops > 4)
5367 abort ();
5369 /* Loop over the operands and check that the memory references are
5370 suitable (ie immediate offsets from the same base register). At
5371 the same time, extract the target register, and the memory
5372 offsets. */
5373 for (i = 0; i < nops; i++)
5375 rtx reg;
5376 rtx offset;
5378 /* Convert a subreg of a mem into the mem itself. */
5379 if (GET_CODE (operands[nops + i]) == SUBREG)
5380 operands[nops + i] = alter_subreg (operands + (nops + i));
5382 if (GET_CODE (operands[nops + i]) != MEM)
5383 abort ();
5385 /* Don't reorder volatile memory references; it doesn't seem worth
5386 looking for the case where the order is ok anyway. */
5387 if (MEM_VOLATILE_P (operands[nops + i]))
5388 return 0;
5390 offset = const0_rtx;
5392 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5393 || (GET_CODE (reg) == SUBREG
5394 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5395 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5396 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5397 == REG)
5398 || (GET_CODE (reg) == SUBREG
5399 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5400 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5401 == CONST_INT)))
5403 if (i == 0)
5405 base_reg = REGNO (reg);
5406 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5407 ? REGNO (operands[i])
5408 : REGNO (SUBREG_REG (operands[i])));
5409 order[0] = 0;
5411 else
5413 if (base_reg != (int) REGNO (reg))
5414 /* Not addressed from the same base register. */
5415 return 0;
5417 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5418 ? REGNO (operands[i])
5419 : REGNO (SUBREG_REG (operands[i])));
5420 if (unsorted_regs[i] < unsorted_regs[order[0]])
5421 order[0] = i;
5424 /* If it isn't an integer register, or if it overwrites the
5425 base register but isn't the last insn in the list, then
5426 we can't do this. */
5427 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5428 || (i != nops - 1 && unsorted_regs[i] == base_reg))
5429 return 0;
5431 unsorted_offsets[i] = INTVAL (offset);
5433 else
5434 /* Not a suitable memory address. */
5435 return 0;
5438 /* All the useful information has now been extracted from the
5439 operands into unsorted_regs and unsorted_offsets; additionally,
5440 order[0] has been set to the lowest numbered register in the
5441 list. Sort the registers into order, and check that the memory
5442 offsets are ascending and adjacent. */
5444 for (i = 1; i < nops; i++)
5446 int j;
5448 order[i] = order[i - 1];
5449 for (j = 0; j < nops; j++)
5450 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5451 && (order[i] == order[i - 1]
5452 || unsorted_regs[j] < unsorted_regs[order[i]]))
5453 order[i] = j;
5455 /* Have we found a suitable register? if not, one must be used more
5456 than once. */
5457 if (order[i] == order[i - 1])
5458 return 0;
5460 /* Is the memory address adjacent and ascending? */
5461 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5462 return 0;
5465 if (base)
5467 *base = base_reg;
5469 for (i = 0; i < nops; i++)
5470 regs[i] = unsorted_regs[order[i]];
5472 *load_offset = unsorted_offsets[order[0]];
5475 if (unsorted_offsets[order[0]] == 0)
5476 return 1; /* ldmia */
5478 if (unsorted_offsets[order[0]] == 4)
5479 return 2; /* ldmib */
5481 if (unsorted_offsets[order[nops - 1]] == 0)
5482 return 3; /* ldmda */
5484 if (unsorted_offsets[order[nops - 1]] == -4)
5485 return 4; /* ldmdb */
5487 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5488 if the offset isn't small enough. The reason 2 ldrs are faster
5489 is because these ARMs are able to do more than one cache access
5490 in a single cycle. The ARM9 and StrongARM have Harvard caches,
5491 whilst the ARM8 has a double bandwidth cache. This means that
5492 these cores can do both an instruction fetch and a data fetch in
5493 a single cycle, so the trick of calculating the address into a
5494 scratch register (one of the result regs) and then doing a load
5495 multiple actually becomes slower (and no smaller in code size).
5496 That is the transformation
5498 ldr rd1, [rbase + offset]
5499 ldr rd2, [rbase + offset + 4]
5503 add rd1, rbase, offset
5504 ldmia rd1, {rd1, rd2}
5506 produces worse code -- '3 cycles + any stalls on rd2' instead of
5507 '2 cycles + any stalls on rd2'. On ARMs with only one cache
5508 access per cycle, the first sequence could never complete in less
5509 than 6 cycles, whereas the ldm sequence would only take 5 and
5510 would make better use of sequential accesses if not hitting the
5511 cache.
5513 We cheat here and test 'arm_ld_sched' which we currently know to
5514 only be true for the ARM8, ARM9 and StrongARM. If this ever
5515 changes, then the test below needs to be reworked. */
5516 if (nops == 2 && arm_ld_sched)
5517 return 0;
5519 /* Can't do it without setting up the offset, only do this if it takes
5520 no more than one insn. */
5521 return (const_ok_for_arm (unsorted_offsets[order[0]])
5522 || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5525 const char *
5526 emit_ldm_seq (rtx *operands, int nops)
5528 int regs[4];
5529 int base_reg;
5530 HOST_WIDE_INT offset;
5531 char buf[100];
5532 int i;
5534 switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5536 case 1:
5537 strcpy (buf, "ldm%?ia\t");
5538 break;
5540 case 2:
5541 strcpy (buf, "ldm%?ib\t");
5542 break;
5544 case 3:
5545 strcpy (buf, "ldm%?da\t");
5546 break;
5548 case 4:
5549 strcpy (buf, "ldm%?db\t");
5550 break;
5552 case 5:
5553 if (offset >= 0)
5554 sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5555 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5556 (long) offset);
5557 else
5558 sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5559 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5560 (long) -offset);
5561 output_asm_insn (buf, operands);
5562 base_reg = regs[0];
5563 strcpy (buf, "ldm%?ia\t");
5564 break;
5566 default:
5567 abort ();
5570 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5571 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5573 for (i = 1; i < nops; i++)
5574 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5575 reg_names[regs[i]]);
5577 strcat (buf, "}\t%@ phole ldm");
5579 output_asm_insn (buf, operands);
5580 return "";
5584 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5585 HOST_WIDE_INT * load_offset)
5587 int unsorted_regs[4];
5588 HOST_WIDE_INT unsorted_offsets[4];
5589 int order[4];
5590 int base_reg = -1;
5591 int i;
5593 /* Can only handle 2, 3, or 4 insns at present, though could be easily
5594 extended if required. */
5595 if (nops < 2 || nops > 4)
5596 abort ();
5598 /* Loop over the operands and check that the memory references are
5599 suitable (ie immediate offsets from the same base register). At
5600 the same time, extract the target register, and the memory
5601 offsets. */
5602 for (i = 0; i < nops; i++)
5604 rtx reg;
5605 rtx offset;
5607 /* Convert a subreg of a mem into the mem itself. */
5608 if (GET_CODE (operands[nops + i]) == SUBREG)
5609 operands[nops + i] = alter_subreg (operands + (nops + i));
5611 if (GET_CODE (operands[nops + i]) != MEM)
5612 abort ();
5614 /* Don't reorder volatile memory references; it doesn't seem worth
5615 looking for the case where the order is ok anyway. */
5616 if (MEM_VOLATILE_P (operands[nops + i]))
5617 return 0;
5619 offset = const0_rtx;
5621 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5622 || (GET_CODE (reg) == SUBREG
5623 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5624 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5625 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5626 == REG)
5627 || (GET_CODE (reg) == SUBREG
5628 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5629 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5630 == CONST_INT)))
5632 if (i == 0)
5634 base_reg = REGNO (reg);
5635 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5636 ? REGNO (operands[i])
5637 : REGNO (SUBREG_REG (operands[i])));
5638 order[0] = 0;
5640 else
5642 if (base_reg != (int) REGNO (reg))
5643 /* Not addressed from the same base register. */
5644 return 0;
5646 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5647 ? REGNO (operands[i])
5648 : REGNO (SUBREG_REG (operands[i])));
5649 if (unsorted_regs[i] < unsorted_regs[order[0]])
5650 order[0] = i;
5653 /* If it isn't an integer register, then we can't do this. */
5654 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5655 return 0;
5657 unsorted_offsets[i] = INTVAL (offset);
5659 else
5660 /* Not a suitable memory address. */
5661 return 0;
5664 /* All the useful information has now been extracted from the
5665 operands into unsorted_regs and unsorted_offsets; additionally,
5666 order[0] has been set to the lowest numbered register in the
5667 list. Sort the registers into order, and check that the memory
5668 offsets are ascending and adjacent. */
5670 for (i = 1; i < nops; i++)
5672 int j;
5674 order[i] = order[i - 1];
5675 for (j = 0; j < nops; j++)
5676 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5677 && (order[i] == order[i - 1]
5678 || unsorted_regs[j] < unsorted_regs[order[i]]))
5679 order[i] = j;
5681 /* Have we found a suitable register? if not, one must be used more
5682 than once. */
5683 if (order[i] == order[i - 1])
5684 return 0;
5686 /* Is the memory address adjacent and ascending? */
5687 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5688 return 0;
5691 if (base)
5693 *base = base_reg;
5695 for (i = 0; i < nops; i++)
5696 regs[i] = unsorted_regs[order[i]];
5698 *load_offset = unsorted_offsets[order[0]];
5701 if (unsorted_offsets[order[0]] == 0)
5702 return 1; /* stmia */
5704 if (unsorted_offsets[order[0]] == 4)
5705 return 2; /* stmib */
5707 if (unsorted_offsets[order[nops - 1]] == 0)
5708 return 3; /* stmda */
5710 if (unsorted_offsets[order[nops - 1]] == -4)
5711 return 4; /* stmdb */
5713 return 0;
5716 const char *
5717 emit_stm_seq (rtx *operands, int nops)
5719 int regs[4];
5720 int base_reg;
5721 HOST_WIDE_INT offset;
5722 char buf[100];
5723 int i;
5725 switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5727 case 1:
5728 strcpy (buf, "stm%?ia\t");
5729 break;
5731 case 2:
5732 strcpy (buf, "stm%?ib\t");
5733 break;
5735 case 3:
5736 strcpy (buf, "stm%?da\t");
5737 break;
5739 case 4:
5740 strcpy (buf, "stm%?db\t");
5741 break;
5743 default:
5744 abort ();
5747 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5748 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5750 for (i = 1; i < nops; i++)
5751 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5752 reg_names[regs[i]]);
5754 strcat (buf, "}\t%@ phole stm");
5756 output_asm_insn (buf, operands);
5757 return "";
5761 multi_register_push (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
5763 if (GET_CODE (op) != PARALLEL
5764 || (GET_CODE (XVECEXP (op, 0, 0)) != SET)
5765 || (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC)
5766 || (XINT (SET_SRC (XVECEXP (op, 0, 0)), 1) != UNSPEC_PUSH_MULT))
5767 return 0;
5769 return 1;
5772 /* Routines for use in generating RTL. */
5775 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5776 int write_back, int unchanging_p, int in_struct_p,
5777 int scalar_p)
5779 int i = 0, j;
5780 rtx result;
5781 int sign = up ? 1 : -1;
5782 rtx mem;
5784 /* XScale has load-store double instructions, but they have stricter
5785 alignment requirements than load-store multiple, so we can not
5786 use them.
5788 For XScale ldm requires 2 + NREGS cycles to complete and blocks
5789 the pipeline until completion.
5791 NREGS CYCLES
5797 An ldr instruction takes 1-3 cycles, but does not block the
5798 pipeline.
5800 NREGS CYCLES
5801 1 1-3
5802 2 2-6
5803 3 3-9
5804 4 4-12
5806 Best case ldr will always win. However, the more ldr instructions
5807 we issue, the less likely we are to be able to schedule them well.
5808 Using ldr instructions also increases code size.
5810 As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5811 for counts of 3 or 4 regs. */
5812 if (arm_tune_xscale && count <= 2 && ! optimize_size)
5814 rtx seq;
5816 start_sequence ();
5818 for (i = 0; i < count; i++)
5820 mem = gen_rtx_MEM (SImode, plus_constant (from, i * 4 * sign));
5821 RTX_UNCHANGING_P (mem) = unchanging_p;
5822 MEM_IN_STRUCT_P (mem) = in_struct_p;
5823 MEM_SCALAR_P (mem) = scalar_p;
5824 emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5827 if (write_back)
5828 emit_move_insn (from, plus_constant (from, count * 4 * sign));
5830 seq = get_insns ();
5831 end_sequence ();
5833 return seq;
5836 result = gen_rtx_PARALLEL (VOIDmode,
5837 rtvec_alloc (count + (write_back ? 1 : 0)));
5838 if (write_back)
5840 XVECEXP (result, 0, 0)
5841 = gen_rtx_SET (GET_MODE (from), from,
5842 plus_constant (from, count * 4 * sign));
5843 i = 1;
5844 count++;
5847 for (j = 0; i < count; i++, j++)
5849 mem = gen_rtx_MEM (SImode, plus_constant (from, j * 4 * sign));
5850 RTX_UNCHANGING_P (mem) = unchanging_p;
5851 MEM_IN_STRUCT_P (mem) = in_struct_p;
5852 MEM_SCALAR_P (mem) = scalar_p;
5853 XVECEXP (result, 0, i)
5854 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5857 return result;
5861 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5862 int write_back, int unchanging_p, int in_struct_p,
5863 int scalar_p)
5865 int i = 0, j;
5866 rtx result;
5867 int sign = up ? 1 : -1;
5868 rtx mem;
5870 /* See arm_gen_load_multiple for discussion of
5871 the pros/cons of ldm/stm usage for XScale. */
5872 if (arm_tune_xscale && count <= 2 && ! optimize_size)
5874 rtx seq;
5876 start_sequence ();
5878 for (i = 0; i < count; i++)
5880 mem = gen_rtx_MEM (SImode, plus_constant (to, i * 4 * sign));
5881 RTX_UNCHANGING_P (mem) = unchanging_p;
5882 MEM_IN_STRUCT_P (mem) = in_struct_p;
5883 MEM_SCALAR_P (mem) = scalar_p;
5884 emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5887 if (write_back)
5888 emit_move_insn (to, plus_constant (to, count * 4 * sign));
5890 seq = get_insns ();
5891 end_sequence ();
5893 return seq;
5896 result = gen_rtx_PARALLEL (VOIDmode,
5897 rtvec_alloc (count + (write_back ? 1 : 0)));
5898 if (write_back)
5900 XVECEXP (result, 0, 0)
5901 = gen_rtx_SET (GET_MODE (to), to,
5902 plus_constant (to, count * 4 * sign));
5903 i = 1;
5904 count++;
5907 for (j = 0; i < count; i++, j++)
5909 mem = gen_rtx_MEM (SImode, plus_constant (to, j * 4 * sign));
5910 RTX_UNCHANGING_P (mem) = unchanging_p;
5911 MEM_IN_STRUCT_P (mem) = in_struct_p;
5912 MEM_SCALAR_P (mem) = scalar_p;
5914 XVECEXP (result, 0, i)
5915 = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5918 return result;
5922 arm_gen_movstrqi (rtx *operands)
5924 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5925 int i;
5926 rtx src, dst;
5927 rtx st_src, st_dst, fin_src, fin_dst;
5928 rtx part_bytes_reg = NULL;
5929 rtx mem;
5930 int dst_unchanging_p, dst_in_struct_p, src_unchanging_p, src_in_struct_p;
5931 int dst_scalar_p, src_scalar_p;
5933 if (GET_CODE (operands[2]) != CONST_INT
5934 || GET_CODE (operands[3]) != CONST_INT
5935 || INTVAL (operands[2]) > 64
5936 || INTVAL (operands[3]) & 3)
5937 return 0;
5939 st_dst = XEXP (operands[0], 0);
5940 st_src = XEXP (operands[1], 0);
5942 dst_unchanging_p = RTX_UNCHANGING_P (operands[0]);
5943 dst_in_struct_p = MEM_IN_STRUCT_P (operands[0]);
5944 dst_scalar_p = MEM_SCALAR_P (operands[0]);
5945 src_unchanging_p = RTX_UNCHANGING_P (operands[1]);
5946 src_in_struct_p = MEM_IN_STRUCT_P (operands[1]);
5947 src_scalar_p = MEM_SCALAR_P (operands[1]);
5949 fin_dst = dst = copy_to_mode_reg (SImode, st_dst);
5950 fin_src = src = copy_to_mode_reg (SImode, st_src);
5952 in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5953 out_words_to_go = INTVAL (operands[2]) / 4;
5954 last_bytes = INTVAL (operands[2]) & 3;
5956 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5957 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5959 for (i = 0; in_words_to_go >= 2; i+=4)
5961 if (in_words_to_go > 4)
5962 emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5963 src_unchanging_p,
5964 src_in_struct_p,
5965 src_scalar_p));
5966 else
5967 emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
5968 FALSE, src_unchanging_p,
5969 src_in_struct_p, src_scalar_p));
5971 if (out_words_to_go)
5973 if (out_words_to_go > 4)
5974 emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5975 dst_unchanging_p,
5976 dst_in_struct_p,
5977 dst_scalar_p));
5978 else if (out_words_to_go != 1)
5979 emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5980 dst, TRUE,
5981 (last_bytes == 0
5982 ? FALSE : TRUE),
5983 dst_unchanging_p,
5984 dst_in_struct_p,
5985 dst_scalar_p));
5986 else
5988 mem = gen_rtx_MEM (SImode, dst);
5989 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
5990 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
5991 MEM_SCALAR_P (mem) = dst_scalar_p;
5992 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5993 if (last_bytes != 0)
5994 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5998 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
5999 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
6002 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
6003 if (out_words_to_go)
6005 rtx sreg;
6007 mem = gen_rtx_MEM (SImode, src);
6008 RTX_UNCHANGING_P (mem) = src_unchanging_p;
6009 MEM_IN_STRUCT_P (mem) = src_in_struct_p;
6010 MEM_SCALAR_P (mem) = src_scalar_p;
6011 emit_move_insn (sreg = gen_reg_rtx (SImode), mem);
6012 emit_move_insn (fin_src = gen_reg_rtx (SImode), plus_constant (src, 4));
6014 mem = gen_rtx_MEM (SImode, dst);
6015 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6016 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6017 MEM_SCALAR_P (mem) = dst_scalar_p;
6018 emit_move_insn (mem, sreg);
6019 emit_move_insn (fin_dst = gen_reg_rtx (SImode), plus_constant (dst, 4));
6020 in_words_to_go--;
6022 if (in_words_to_go) /* Sanity check */
6023 abort ();
6026 if (in_words_to_go)
6028 if (in_words_to_go < 0)
6029 abort ();
6031 mem = gen_rtx_MEM (SImode, src);
6032 RTX_UNCHANGING_P (mem) = src_unchanging_p;
6033 MEM_IN_STRUCT_P (mem) = src_in_struct_p;
6034 MEM_SCALAR_P (mem) = src_scalar_p;
6035 part_bytes_reg = copy_to_mode_reg (SImode, mem);
6038 if (last_bytes && part_bytes_reg == NULL)
6039 abort ();
6041 if (BYTES_BIG_ENDIAN && last_bytes)
6043 rtx tmp = gen_reg_rtx (SImode);
6045 /* The bytes we want are in the top end of the word. */
6046 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
6047 GEN_INT (8 * (4 - last_bytes))));
6048 part_bytes_reg = tmp;
6050 while (last_bytes)
6052 mem = gen_rtx_MEM (QImode, plus_constant (dst, last_bytes - 1));
6053 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6054 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6055 MEM_SCALAR_P (mem) = dst_scalar_p;
6056 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6058 if (--last_bytes)
6060 tmp = gen_reg_rtx (SImode);
6061 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
6062 part_bytes_reg = tmp;
6067 else
6069 if (last_bytes > 1)
6071 mem = gen_rtx_MEM (HImode, 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 (HImode, part_bytes_reg));
6076 last_bytes -= 2;
6077 if (last_bytes)
6079 rtx tmp = gen_reg_rtx (SImode);
6081 emit_insn (gen_addsi3 (dst, dst, const2_rtx));
6082 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
6083 part_bytes_reg = tmp;
6087 if (last_bytes)
6089 mem = gen_rtx_MEM (QImode, dst);
6090 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6091 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6092 MEM_SCALAR_P (mem) = dst_scalar_p;
6093 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6097 return 1;
6100 /* Generate a memory reference for a half word, such that it will be loaded
6101 into the top 16 bits of the word. We can assume that the address is
6102 known to be alignable and of the form reg, or plus (reg, const). */
6105 arm_gen_rotated_half_load (rtx memref)
6107 HOST_WIDE_INT offset = 0;
6108 rtx base = XEXP (memref, 0);
6110 if (GET_CODE (base) == PLUS)
6112 offset = INTVAL (XEXP (base, 1));
6113 base = XEXP (base, 0);
6116 /* If we aren't allowed to generate unaligned addresses, then fail. */
6117 if (TARGET_MMU_TRAPS
6118 && ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0)))
6119 return NULL;
6121 base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
6123 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
6124 return base;
6126 return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
6129 /* Select a dominance comparison mode if possible for a test of the general
6130 form (OP (COND_OR (X) (Y)) (const_int 0)). We support three forms.
6131 COND_OR == DOM_CC_X_AND_Y => (X && Y)
6132 COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6133 COND_OR == DOM_CC_X_OR_Y => (X || Y)
6134 In all cases OP will be either EQ or NE, but we don't need to know which
6135 here. If we are unable to support a dominance comparison we return
6136 CC mode. This will then fail to match for the RTL expressions that
6137 generate this call. */
6138 enum machine_mode
6139 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
6141 enum rtx_code cond1, cond2;
6142 int swapped = 0;
6144 /* Currently we will probably get the wrong result if the individual
6145 comparisons are not simple. This also ensures that it is safe to
6146 reverse a comparison if necessary. */
6147 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6148 != CCmode)
6149 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6150 != CCmode))
6151 return CCmode;
6153 /* The if_then_else variant of this tests the second condition if the
6154 first passes, but is true if the first fails. Reverse the first
6155 condition to get a true "inclusive-or" expression. */
6156 if (cond_or == DOM_CC_NX_OR_Y)
6157 cond1 = reverse_condition (cond1);
6159 /* If the comparisons are not equal, and one doesn't dominate the other,
6160 then we can't do this. */
6161 if (cond1 != cond2
6162 && !comparison_dominates_p (cond1, cond2)
6163 && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6164 return CCmode;
6166 if (swapped)
6168 enum rtx_code temp = cond1;
6169 cond1 = cond2;
6170 cond2 = temp;
6173 switch (cond1)
6175 case EQ:
6176 if (cond2 == EQ || cond_or == DOM_CC_X_AND_Y)
6177 return CC_DEQmode;
6179 switch (cond2)
6181 case LE: return CC_DLEmode;
6182 case LEU: return CC_DLEUmode;
6183 case GE: return CC_DGEmode;
6184 case GEU: return CC_DGEUmode;
6185 default: break;
6188 break;
6190 case LT:
6191 if (cond2 == LT || cond_or == DOM_CC_X_AND_Y)
6192 return CC_DLTmode;
6193 if (cond2 == LE)
6194 return CC_DLEmode;
6195 if (cond2 == NE)
6196 return CC_DNEmode;
6197 break;
6199 case GT:
6200 if (cond2 == GT || cond_or == DOM_CC_X_AND_Y)
6201 return CC_DGTmode;
6202 if (cond2 == GE)
6203 return CC_DGEmode;
6204 if (cond2 == NE)
6205 return CC_DNEmode;
6206 break;
6208 case LTU:
6209 if (cond2 == LTU || cond_or == DOM_CC_X_AND_Y)
6210 return CC_DLTUmode;
6211 if (cond2 == LEU)
6212 return CC_DLEUmode;
6213 if (cond2 == NE)
6214 return CC_DNEmode;
6215 break;
6217 case GTU:
6218 if (cond2 == GTU || cond_or == DOM_CC_X_AND_Y)
6219 return CC_DGTUmode;
6220 if (cond2 == GEU)
6221 return CC_DGEUmode;
6222 if (cond2 == NE)
6223 return CC_DNEmode;
6224 break;
6226 /* The remaining cases only occur when both comparisons are the
6227 same. */
6228 case NE:
6229 return CC_DNEmode;
6231 case LE:
6232 return CC_DLEmode;
6234 case GE:
6235 return CC_DGEmode;
6237 case LEU:
6238 return CC_DLEUmode;
6240 case GEU:
6241 return CC_DGEUmode;
6243 default:
6244 break;
6247 abort ();
6250 enum machine_mode
6251 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6253 /* All floating point compares return CCFP if it is an equality
6254 comparison, and CCFPE otherwise. */
6255 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6257 switch (op)
6259 case EQ:
6260 case NE:
6261 case UNORDERED:
6262 case ORDERED:
6263 case UNLT:
6264 case UNLE:
6265 case UNGT:
6266 case UNGE:
6267 case UNEQ:
6268 case LTGT:
6269 return CCFPmode;
6271 case LT:
6272 case LE:
6273 case GT:
6274 case GE:
6275 if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6276 return CCFPmode;
6277 return CCFPEmode;
6279 default:
6280 abort ();
6284 /* A compare with a shifted operand. Because of canonicalization, the
6285 comparison will have to be swapped when we emit the assembler. */
6286 if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6287 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6288 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6289 || GET_CODE (x) == ROTATERT))
6290 return CC_SWPmode;
6292 /* This is a special case that is used by combine to allow a
6293 comparison of a shifted byte load to be split into a zero-extend
6294 followed by a comparison of the shifted integer (only valid for
6295 equalities and unsigned inequalities). */
6296 if (GET_MODE (x) == SImode
6297 && GET_CODE (x) == ASHIFT
6298 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6299 && GET_CODE (XEXP (x, 0)) == SUBREG
6300 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6301 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6302 && (op == EQ || op == NE
6303 || op == GEU || op == GTU || op == LTU || op == LEU)
6304 && GET_CODE (y) == CONST_INT)
6305 return CC_Zmode;
6307 /* A construct for a conditional compare, if the false arm contains
6308 0, then both conditions must be true, otherwise either condition
6309 must be true. Not all conditions are possible, so CCmode is
6310 returned if it can't be done. */
6311 if (GET_CODE (x) == IF_THEN_ELSE
6312 && (XEXP (x, 2) == const0_rtx
6313 || XEXP (x, 2) == const1_rtx)
6314 && COMPARISON_P (XEXP (x, 0))
6315 && COMPARISON_P (XEXP (x, 1)))
6316 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6317 INTVAL (XEXP (x, 2)));
6319 /* Alternate canonicalizations of the above. These are somewhat cleaner. */
6320 if (GET_CODE (x) == AND
6321 && COMPARISON_P (XEXP (x, 0))
6322 && COMPARISON_P (XEXP (x, 1)))
6323 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6324 DOM_CC_X_AND_Y);
6326 if (GET_CODE (x) == IOR
6327 && COMPARISON_P (XEXP (x, 0))
6328 && COMPARISON_P (XEXP (x, 1)))
6329 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6330 DOM_CC_X_OR_Y);
6332 /* An operation (on Thumb) where we want to test for a single bit.
6333 This is done by shifting that bit up into the top bit of a
6334 scratch register; we can then branch on the sign bit. */
6335 if (TARGET_THUMB
6336 && GET_MODE (x) == SImode
6337 && (op == EQ || op == NE)
6338 && (GET_CODE (x) == ZERO_EXTRACT))
6339 return CC_Nmode;
6341 /* An operation that sets the condition codes as a side-effect, the
6342 V flag is not set correctly, so we can only use comparisons where
6343 this doesn't matter. (For LT and GE we can use "mi" and "pl"
6344 instead.) */
6345 if (GET_MODE (x) == SImode
6346 && y == const0_rtx
6347 && (op == EQ || op == NE || op == LT || op == GE)
6348 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6349 || GET_CODE (x) == AND || GET_CODE (x) == IOR
6350 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6351 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6352 || GET_CODE (x) == LSHIFTRT
6353 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6354 || GET_CODE (x) == ROTATERT
6355 || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6356 return CC_NOOVmode;
6358 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6359 return CC_Zmode;
6361 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6362 && GET_CODE (x) == PLUS
6363 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6364 return CC_Cmode;
6366 return CCmode;
6369 /* X and Y are two things to compare using CODE. Emit the compare insn and
6370 return the rtx for register 0 in the proper mode. FP means this is a
6371 floating point compare: I don't think that it is needed on the arm. */
6373 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6375 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6376 rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6378 emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6379 gen_rtx_COMPARE (mode, x, y)));
6381 return cc_reg;
6384 /* Generate a sequence of insns that will generate the correct return
6385 address mask depending on the physical architecture that the program
6386 is running on. */
6388 arm_gen_return_addr_mask (void)
6390 rtx reg = gen_reg_rtx (Pmode);
6392 emit_insn (gen_return_addr_mask (reg));
6393 return reg;
6396 void
6397 arm_reload_in_hi (rtx *operands)
6399 rtx ref = operands[1];
6400 rtx base, scratch;
6401 HOST_WIDE_INT offset = 0;
6403 if (GET_CODE (ref) == SUBREG)
6405 offset = SUBREG_BYTE (ref);
6406 ref = SUBREG_REG (ref);
6409 if (GET_CODE (ref) == REG)
6411 /* We have a pseudo which has been spilt onto the stack; there
6412 are two cases here: the first where there is a simple
6413 stack-slot replacement and a second where the stack-slot is
6414 out of range, or is used as a subreg. */
6415 if (reg_equiv_mem[REGNO (ref)])
6417 ref = reg_equiv_mem[REGNO (ref)];
6418 base = find_replacement (&XEXP (ref, 0));
6420 else
6421 /* The slot is out of range, or was dressed up in a SUBREG. */
6422 base = reg_equiv_address[REGNO (ref)];
6424 else
6425 base = find_replacement (&XEXP (ref, 0));
6427 /* Handle the case where the address is too complex to be offset by 1. */
6428 if (GET_CODE (base) == MINUS
6429 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6431 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6433 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6434 base = base_plus;
6436 else if (GET_CODE (base) == PLUS)
6438 /* The addend must be CONST_INT, or we would have dealt with it above. */
6439 HOST_WIDE_INT hi, lo;
6441 offset += INTVAL (XEXP (base, 1));
6442 base = XEXP (base, 0);
6444 /* Rework the address into a legal sequence of insns. */
6445 /* Valid range for lo is -4095 -> 4095 */
6446 lo = (offset >= 0
6447 ? (offset & 0xfff)
6448 : -((-offset) & 0xfff));
6450 /* Corner case, if lo is the max offset then we would be out of range
6451 once we have added the additional 1 below, so bump the msb into the
6452 pre-loading insn(s). */
6453 if (lo == 4095)
6454 lo &= 0x7ff;
6456 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6457 ^ (HOST_WIDE_INT) 0x80000000)
6458 - (HOST_WIDE_INT) 0x80000000);
6460 if (hi + lo != offset)
6461 abort ();
6463 if (hi != 0)
6465 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6467 /* Get the base address; addsi3 knows how to handle constants
6468 that require more than one insn. */
6469 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6470 base = base_plus;
6471 offset = lo;
6475 /* Operands[2] may overlap operands[0] (though it won't overlap
6476 operands[1]), that's why we asked for a DImode reg -- so we can
6477 use the bit that does not overlap. */
6478 if (REGNO (operands[2]) == REGNO (operands[0]))
6479 scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6480 else
6481 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6483 emit_insn (gen_zero_extendqisi2 (scratch,
6484 gen_rtx_MEM (QImode,
6485 plus_constant (base,
6486 offset))));
6487 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6488 gen_rtx_MEM (QImode,
6489 plus_constant (base,
6490 offset + 1))));
6491 if (!BYTES_BIG_ENDIAN)
6492 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6493 gen_rtx_IOR (SImode,
6494 gen_rtx_ASHIFT
6495 (SImode,
6496 gen_rtx_SUBREG (SImode, operands[0], 0),
6497 GEN_INT (8)),
6498 scratch)));
6499 else
6500 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6501 gen_rtx_IOR (SImode,
6502 gen_rtx_ASHIFT (SImode, scratch,
6503 GEN_INT (8)),
6504 gen_rtx_SUBREG (SImode, operands[0],
6505 0))));
6508 /* Handle storing a half-word to memory during reload by synthesizing as two
6509 byte stores. Take care not to clobber the input values until after we
6510 have moved them somewhere safe. This code assumes that if the DImode
6511 scratch in operands[2] overlaps either the input value or output address
6512 in some way, then that value must die in this insn (we absolutely need
6513 two scratch registers for some corner cases). */
6514 void
6515 arm_reload_out_hi (rtx *operands)
6517 rtx ref = operands[0];
6518 rtx outval = operands[1];
6519 rtx base, scratch;
6520 HOST_WIDE_INT offset = 0;
6522 if (GET_CODE (ref) == SUBREG)
6524 offset = SUBREG_BYTE (ref);
6525 ref = SUBREG_REG (ref);
6528 if (GET_CODE (ref) == REG)
6530 /* We have a pseudo which has been spilt onto the stack; there
6531 are two cases here: the first where there is a simple
6532 stack-slot replacement and a second where the stack-slot is
6533 out of range, or is used as a subreg. */
6534 if (reg_equiv_mem[REGNO (ref)])
6536 ref = reg_equiv_mem[REGNO (ref)];
6537 base = find_replacement (&XEXP (ref, 0));
6539 else
6540 /* The slot is out of range, or was dressed up in a SUBREG. */
6541 base = reg_equiv_address[REGNO (ref)];
6543 else
6544 base = find_replacement (&XEXP (ref, 0));
6546 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6548 /* Handle the case where the address is too complex to be offset by 1. */
6549 if (GET_CODE (base) == MINUS
6550 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6552 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6554 /* Be careful not to destroy OUTVAL. */
6555 if (reg_overlap_mentioned_p (base_plus, outval))
6557 /* Updating base_plus might destroy outval, see if we can
6558 swap the scratch and base_plus. */
6559 if (!reg_overlap_mentioned_p (scratch, outval))
6561 rtx tmp = scratch;
6562 scratch = base_plus;
6563 base_plus = tmp;
6565 else
6567 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6569 /* Be conservative and copy OUTVAL into the scratch now,
6570 this should only be necessary if outval is a subreg
6571 of something larger than a word. */
6572 /* XXX Might this clobber base? I can't see how it can,
6573 since scratch is known to overlap with OUTVAL, and
6574 must be wider than a word. */
6575 emit_insn (gen_movhi (scratch_hi, outval));
6576 outval = scratch_hi;
6580 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6581 base = base_plus;
6583 else if (GET_CODE (base) == PLUS)
6585 /* The addend must be CONST_INT, or we would have dealt with it above. */
6586 HOST_WIDE_INT hi, lo;
6588 offset += INTVAL (XEXP (base, 1));
6589 base = XEXP (base, 0);
6591 /* Rework the address into a legal sequence of insns. */
6592 /* Valid range for lo is -4095 -> 4095 */
6593 lo = (offset >= 0
6594 ? (offset & 0xfff)
6595 : -((-offset) & 0xfff));
6597 /* Corner case, if lo is the max offset then we would be out of range
6598 once we have added the additional 1 below, so bump the msb into the
6599 pre-loading insn(s). */
6600 if (lo == 4095)
6601 lo &= 0x7ff;
6603 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6604 ^ (HOST_WIDE_INT) 0x80000000)
6605 - (HOST_WIDE_INT) 0x80000000);
6607 if (hi + lo != offset)
6608 abort ();
6610 if (hi != 0)
6612 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6614 /* Be careful not to destroy OUTVAL. */
6615 if (reg_overlap_mentioned_p (base_plus, outval))
6617 /* Updating base_plus might destroy outval, see if we
6618 can swap the scratch and base_plus. */
6619 if (!reg_overlap_mentioned_p (scratch, outval))
6621 rtx tmp = scratch;
6622 scratch = base_plus;
6623 base_plus = tmp;
6625 else
6627 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6629 /* Be conservative and copy outval into scratch now,
6630 this should only be necessary if outval is a
6631 subreg of something larger than a word. */
6632 /* XXX Might this clobber base? I can't see how it
6633 can, since scratch is known to overlap with
6634 outval. */
6635 emit_insn (gen_movhi (scratch_hi, outval));
6636 outval = scratch_hi;
6640 /* Get the base address; addsi3 knows how to handle constants
6641 that require more than one insn. */
6642 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6643 base = base_plus;
6644 offset = lo;
6648 if (BYTES_BIG_ENDIAN)
6650 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6651 plus_constant (base, offset + 1)),
6652 gen_lowpart (QImode, outval)));
6653 emit_insn (gen_lshrsi3 (scratch,
6654 gen_rtx_SUBREG (SImode, outval, 0),
6655 GEN_INT (8)));
6656 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6657 gen_lowpart (QImode, scratch)));
6659 else
6661 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6662 gen_lowpart (QImode, outval)));
6663 emit_insn (gen_lshrsi3 (scratch,
6664 gen_rtx_SUBREG (SImode, outval, 0),
6665 GEN_INT (8)));
6666 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6667 plus_constant (base, offset + 1)),
6668 gen_lowpart (QImode, scratch)));
6672 /* Print a symbolic form of X to the debug file, F. */
6673 static void
6674 arm_print_value (FILE *f, rtx x)
6676 switch (GET_CODE (x))
6678 case CONST_INT:
6679 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6680 return;
6682 case CONST_DOUBLE:
6683 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6684 return;
6686 case CONST_VECTOR:
6688 int i;
6690 fprintf (f, "<");
6691 for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6693 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6694 if (i < (CONST_VECTOR_NUNITS (x) - 1))
6695 fputc (',', f);
6697 fprintf (f, ">");
6699 return;
6701 case CONST_STRING:
6702 fprintf (f, "\"%s\"", XSTR (x, 0));
6703 return;
6705 case SYMBOL_REF:
6706 fprintf (f, "`%s'", XSTR (x, 0));
6707 return;
6709 case LABEL_REF:
6710 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6711 return;
6713 case CONST:
6714 arm_print_value (f, XEXP (x, 0));
6715 return;
6717 case PLUS:
6718 arm_print_value (f, XEXP (x, 0));
6719 fprintf (f, "+");
6720 arm_print_value (f, XEXP (x, 1));
6721 return;
6723 case PC:
6724 fprintf (f, "pc");
6725 return;
6727 default:
6728 fprintf (f, "????");
6729 return;
6733 /* Routines for manipulation of the constant pool. */
6735 /* Arm instructions cannot load a large constant directly into a
6736 register; they have to come from a pc relative load. The constant
6737 must therefore be placed in the addressable range of the pc
6738 relative load. Depending on the precise pc relative load
6739 instruction the range is somewhere between 256 bytes and 4k. This
6740 means that we often have to dump a constant inside a function, and
6741 generate code to branch around it.
6743 It is important to minimize this, since the branches will slow
6744 things down and make the code larger.
6746 Normally we can hide the table after an existing unconditional
6747 branch so that there is no interruption of the flow, but in the
6748 worst case the code looks like this:
6750 ldr rn, L1
6752 b L2
6753 align
6754 L1: .long value
6758 ldr rn, L3
6760 b L4
6761 align
6762 L3: .long value
6766 We fix this by performing a scan after scheduling, which notices
6767 which instructions need to have their operands fetched from the
6768 constant table and builds the table.
6770 The algorithm starts by building a table of all the constants that
6771 need fixing up and all the natural barriers in the function (places
6772 where a constant table can be dropped without breaking the flow).
6773 For each fixup we note how far the pc-relative replacement will be
6774 able to reach and the offset of the instruction into the function.
6776 Having built the table we then group the fixes together to form
6777 tables that are as large as possible (subject to addressing
6778 constraints) and emit each table of constants after the last
6779 barrier that is within range of all the instructions in the group.
6780 If a group does not contain a barrier, then we forcibly create one
6781 by inserting a jump instruction into the flow. Once the table has
6782 been inserted, the insns are then modified to reference the
6783 relevant entry in the pool.
6785 Possible enhancements to the algorithm (not implemented) are:
6787 1) For some processors and object formats, there may be benefit in
6788 aligning the pools to the start of cache lines; this alignment
6789 would need to be taken into account when calculating addressability
6790 of a pool. */
6792 /* These typedefs are located at the start of this file, so that
6793 they can be used in the prototypes there. This comment is to
6794 remind readers of that fact so that the following structures
6795 can be understood more easily.
6797 typedef struct minipool_node Mnode;
6798 typedef struct minipool_fixup Mfix; */
6800 struct minipool_node
6802 /* Doubly linked chain of entries. */
6803 Mnode * next;
6804 Mnode * prev;
6805 /* The maximum offset into the code that this entry can be placed. While
6806 pushing fixes for forward references, all entries are sorted in order
6807 of increasing max_address. */
6808 HOST_WIDE_INT max_address;
6809 /* Similarly for an entry inserted for a backwards ref. */
6810 HOST_WIDE_INT min_address;
6811 /* The number of fixes referencing this entry. This can become zero
6812 if we "unpush" an entry. In this case we ignore the entry when we
6813 come to emit the code. */
6814 int refcount;
6815 /* The offset from the start of the minipool. */
6816 HOST_WIDE_INT offset;
6817 /* The value in table. */
6818 rtx value;
6819 /* The mode of value. */
6820 enum machine_mode mode;
6821 /* The size of the value. With iWMMXt enabled
6822 sizes > 4 also imply an alignment of 8-bytes. */
6823 int fix_size;
6826 struct minipool_fixup
6828 Mfix * next;
6829 rtx insn;
6830 HOST_WIDE_INT address;
6831 rtx * loc;
6832 enum machine_mode mode;
6833 int fix_size;
6834 rtx value;
6835 Mnode * minipool;
6836 HOST_WIDE_INT forwards;
6837 HOST_WIDE_INT backwards;
6840 /* Fixes less than a word need padding out to a word boundary. */
6841 #define MINIPOOL_FIX_SIZE(mode) \
6842 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6844 static Mnode * minipool_vector_head;
6845 static Mnode * minipool_vector_tail;
6846 static rtx minipool_vector_label;
6848 /* The linked list of all minipool fixes required for this function. */
6849 Mfix * minipool_fix_head;
6850 Mfix * minipool_fix_tail;
6851 /* The fix entry for the current minipool, once it has been placed. */
6852 Mfix * minipool_barrier;
6854 /* Determines if INSN is the start of a jump table. Returns the end
6855 of the TABLE or NULL_RTX. */
6856 static rtx
6857 is_jump_table (rtx insn)
6859 rtx table;
6861 if (GET_CODE (insn) == JUMP_INSN
6862 && JUMP_LABEL (insn) != NULL
6863 && ((table = next_real_insn (JUMP_LABEL (insn)))
6864 == next_real_insn (insn))
6865 && table != NULL
6866 && GET_CODE (table) == JUMP_INSN
6867 && (GET_CODE (PATTERN (table)) == ADDR_VEC
6868 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6869 return table;
6871 return NULL_RTX;
6874 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6875 #define JUMP_TABLES_IN_TEXT_SECTION 0
6876 #endif
6878 static HOST_WIDE_INT
6879 get_jump_table_size (rtx insn)
6881 /* ADDR_VECs only take room if read-only data does into the text
6882 section. */
6883 if (JUMP_TABLES_IN_TEXT_SECTION
6884 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6885 || 1
6886 #endif
6889 rtx body = PATTERN (insn);
6890 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6892 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6895 return 0;
6898 /* Move a minipool fix MP from its current location to before MAX_MP.
6899 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6900 constraints may need updating. */
6901 static Mnode *
6902 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6903 HOST_WIDE_INT max_address)
6905 /* This should never be true and the code below assumes these are
6906 different. */
6907 if (mp == max_mp)
6908 abort ();
6910 if (max_mp == NULL)
6912 if (max_address < mp->max_address)
6913 mp->max_address = max_address;
6915 else
6917 if (max_address > max_mp->max_address - mp->fix_size)
6918 mp->max_address = max_mp->max_address - mp->fix_size;
6919 else
6920 mp->max_address = max_address;
6922 /* Unlink MP from its current position. Since max_mp is non-null,
6923 mp->prev must be non-null. */
6924 mp->prev->next = mp->next;
6925 if (mp->next != NULL)
6926 mp->next->prev = mp->prev;
6927 else
6928 minipool_vector_tail = mp->prev;
6930 /* Re-insert it before MAX_MP. */
6931 mp->next = max_mp;
6932 mp->prev = max_mp->prev;
6933 max_mp->prev = mp;
6935 if (mp->prev != NULL)
6936 mp->prev->next = mp;
6937 else
6938 minipool_vector_head = mp;
6941 /* Save the new entry. */
6942 max_mp = mp;
6944 /* Scan over the preceding entries and adjust their addresses as
6945 required. */
6946 while (mp->prev != NULL
6947 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6949 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6950 mp = mp->prev;
6953 return max_mp;
6956 /* Add a constant to the minipool for a forward reference. Returns the
6957 node added or NULL if the constant will not fit in this pool. */
6958 static Mnode *
6959 add_minipool_forward_ref (Mfix *fix)
6961 /* If set, max_mp is the first pool_entry that has a lower
6962 constraint than the one we are trying to add. */
6963 Mnode * max_mp = NULL;
6964 HOST_WIDE_INT max_address = fix->address + fix->forwards;
6965 Mnode * mp;
6967 /* If this fix's address is greater than the address of the first
6968 entry, then we can't put the fix in this pool. We subtract the
6969 size of the current fix to ensure that if the table is fully
6970 packed we still have enough room to insert this value by suffling
6971 the other fixes forwards. */
6972 if (minipool_vector_head &&
6973 fix->address >= minipool_vector_head->max_address - fix->fix_size)
6974 return NULL;
6976 /* Scan the pool to see if a constant with the same value has
6977 already been added. While we are doing this, also note the
6978 location where we must insert the constant if it doesn't already
6979 exist. */
6980 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6982 if (GET_CODE (fix->value) == GET_CODE (mp->value)
6983 && fix->mode == mp->mode
6984 && (GET_CODE (fix->value) != CODE_LABEL
6985 || (CODE_LABEL_NUMBER (fix->value)
6986 == CODE_LABEL_NUMBER (mp->value)))
6987 && rtx_equal_p (fix->value, mp->value))
6989 /* More than one fix references this entry. */
6990 mp->refcount++;
6991 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6994 /* Note the insertion point if necessary. */
6995 if (max_mp == NULL
6996 && mp->max_address > max_address)
6997 max_mp = mp;
6999 /* If we are inserting an 8-bytes aligned quantity and
7000 we have not already found an insertion point, then
7001 make sure that all such 8-byte aligned quantities are
7002 placed at the start of the pool. */
7003 if (ARM_DOUBLEWORD_ALIGN
7004 && max_mp == NULL
7005 && fix->fix_size == 8
7006 && mp->fix_size != 8)
7008 max_mp = mp;
7009 max_address = mp->max_address;
7013 /* The value is not currently in the minipool, so we need to create
7014 a new entry for it. If MAX_MP is NULL, the entry will be put on
7015 the end of the list since the placement is less constrained than
7016 any existing entry. Otherwise, we insert the new fix before
7017 MAX_MP and, if necessary, adjust the constraints on the other
7018 entries. */
7019 mp = xmalloc (sizeof (* mp));
7020 mp->fix_size = fix->fix_size;
7021 mp->mode = fix->mode;
7022 mp->value = fix->value;
7023 mp->refcount = 1;
7024 /* Not yet required for a backwards ref. */
7025 mp->min_address = -65536;
7027 if (max_mp == NULL)
7029 mp->max_address = max_address;
7030 mp->next = NULL;
7031 mp->prev = minipool_vector_tail;
7033 if (mp->prev == NULL)
7035 minipool_vector_head = mp;
7036 minipool_vector_label = gen_label_rtx ();
7038 else
7039 mp->prev->next = mp;
7041 minipool_vector_tail = mp;
7043 else
7045 if (max_address > max_mp->max_address - mp->fix_size)
7046 mp->max_address = max_mp->max_address - mp->fix_size;
7047 else
7048 mp->max_address = max_address;
7050 mp->next = max_mp;
7051 mp->prev = max_mp->prev;
7052 max_mp->prev = mp;
7053 if (mp->prev != NULL)
7054 mp->prev->next = mp;
7055 else
7056 minipool_vector_head = mp;
7059 /* Save the new entry. */
7060 max_mp = mp;
7062 /* Scan over the preceding entries and adjust their addresses as
7063 required. */
7064 while (mp->prev != NULL
7065 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7067 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7068 mp = mp->prev;
7071 return max_mp;
7074 static Mnode *
7075 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7076 HOST_WIDE_INT min_address)
7078 HOST_WIDE_INT offset;
7080 /* This should never be true, and the code below assumes these are
7081 different. */
7082 if (mp == min_mp)
7083 abort ();
7085 if (min_mp == NULL)
7087 if (min_address > mp->min_address)
7088 mp->min_address = min_address;
7090 else
7092 /* We will adjust this below if it is too loose. */
7093 mp->min_address = min_address;
7095 /* Unlink MP from its current position. Since min_mp is non-null,
7096 mp->next must be non-null. */
7097 mp->next->prev = mp->prev;
7098 if (mp->prev != NULL)
7099 mp->prev->next = mp->next;
7100 else
7101 minipool_vector_head = mp->next;
7103 /* Reinsert it after MIN_MP. */
7104 mp->prev = min_mp;
7105 mp->next = min_mp->next;
7106 min_mp->next = mp;
7107 if (mp->next != NULL)
7108 mp->next->prev = mp;
7109 else
7110 minipool_vector_tail = mp;
7113 min_mp = mp;
7115 offset = 0;
7116 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7118 mp->offset = offset;
7119 if (mp->refcount > 0)
7120 offset += mp->fix_size;
7122 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7123 mp->next->min_address = mp->min_address + mp->fix_size;
7126 return min_mp;
7129 /* Add a constant to the minipool for a backward reference. Returns the
7130 node added or NULL if the constant will not fit in this pool.
7132 Note that the code for insertion for a backwards reference can be
7133 somewhat confusing because the calculated offsets for each fix do
7134 not take into account the size of the pool (which is still under
7135 construction. */
7136 static Mnode *
7137 add_minipool_backward_ref (Mfix *fix)
7139 /* If set, min_mp is the last pool_entry that has a lower constraint
7140 than the one we are trying to add. */
7141 Mnode *min_mp = NULL;
7142 /* This can be negative, since it is only a constraint. */
7143 HOST_WIDE_INT min_address = fix->address - fix->backwards;
7144 Mnode *mp;
7146 /* If we can't reach the current pool from this insn, or if we can't
7147 insert this entry at the end of the pool without pushing other
7148 fixes out of range, then we don't try. This ensures that we
7149 can't fail later on. */
7150 if (min_address >= minipool_barrier->address
7151 || (minipool_vector_tail->min_address + fix->fix_size
7152 >= minipool_barrier->address))
7153 return NULL;
7155 /* Scan the pool to see if a constant with the same value has
7156 already been added. While we are doing this, also note the
7157 location where we must insert the constant if it doesn't already
7158 exist. */
7159 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7161 if (GET_CODE (fix->value) == GET_CODE (mp->value)
7162 && fix->mode == mp->mode
7163 && (GET_CODE (fix->value) != CODE_LABEL
7164 || (CODE_LABEL_NUMBER (fix->value)
7165 == CODE_LABEL_NUMBER (mp->value)))
7166 && rtx_equal_p (fix->value, mp->value)
7167 /* Check that there is enough slack to move this entry to the
7168 end of the table (this is conservative). */
7169 && (mp->max_address
7170 > (minipool_barrier->address
7171 + minipool_vector_tail->offset
7172 + minipool_vector_tail->fix_size)))
7174 mp->refcount++;
7175 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7178 if (min_mp != NULL)
7179 mp->min_address += fix->fix_size;
7180 else
7182 /* Note the insertion point if necessary. */
7183 if (mp->min_address < min_address)
7185 /* For now, we do not allow the insertion of 8-byte alignment
7186 requiring nodes anywhere but at the start of the pool. */
7187 if (ARM_DOUBLEWORD_ALIGN
7188 && fix->fix_size == 8 && mp->fix_size != 8)
7189 return NULL;
7190 else
7191 min_mp = mp;
7193 else if (mp->max_address
7194 < minipool_barrier->address + mp->offset + fix->fix_size)
7196 /* Inserting before this entry would push the fix beyond
7197 its maximum address (which can happen if we have
7198 re-located a forwards fix); force the new fix to come
7199 after it. */
7200 min_mp = mp;
7201 min_address = mp->min_address + fix->fix_size;
7203 /* If we are inserting an 8-bytes aligned quantity and
7204 we have not already found an insertion point, then
7205 make sure that all such 8-byte aligned quantities are
7206 placed at the start of the pool. */
7207 else if (ARM_DOUBLEWORD_ALIGN
7208 && min_mp == NULL
7209 && fix->fix_size == 8
7210 && mp->fix_size < 8)
7212 min_mp = mp;
7213 min_address = mp->min_address + fix->fix_size;
7218 /* We need to create a new entry. */
7219 mp = xmalloc (sizeof (* mp));
7220 mp->fix_size = fix->fix_size;
7221 mp->mode = fix->mode;
7222 mp->value = fix->value;
7223 mp->refcount = 1;
7224 mp->max_address = minipool_barrier->address + 65536;
7226 mp->min_address = min_address;
7228 if (min_mp == NULL)
7230 mp->prev = NULL;
7231 mp->next = minipool_vector_head;
7233 if (mp->next == NULL)
7235 minipool_vector_tail = mp;
7236 minipool_vector_label = gen_label_rtx ();
7238 else
7239 mp->next->prev = mp;
7241 minipool_vector_head = mp;
7243 else
7245 mp->next = min_mp->next;
7246 mp->prev = min_mp;
7247 min_mp->next = mp;
7249 if (mp->next != NULL)
7250 mp->next->prev = mp;
7251 else
7252 minipool_vector_tail = mp;
7255 /* Save the new entry. */
7256 min_mp = mp;
7258 if (mp->prev)
7259 mp = mp->prev;
7260 else
7261 mp->offset = 0;
7263 /* Scan over the following entries and adjust their offsets. */
7264 while (mp->next != NULL)
7266 if (mp->next->min_address < mp->min_address + mp->fix_size)
7267 mp->next->min_address = mp->min_address + mp->fix_size;
7269 if (mp->refcount)
7270 mp->next->offset = mp->offset + mp->fix_size;
7271 else
7272 mp->next->offset = mp->offset;
7274 mp = mp->next;
7277 return min_mp;
7280 static void
7281 assign_minipool_offsets (Mfix *barrier)
7283 HOST_WIDE_INT offset = 0;
7284 Mnode *mp;
7286 minipool_barrier = barrier;
7288 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7290 mp->offset = offset;
7292 if (mp->refcount > 0)
7293 offset += mp->fix_size;
7297 /* Output the literal table */
7298 static void
7299 dump_minipool (rtx scan)
7301 Mnode * mp;
7302 Mnode * nmp;
7303 int align64 = 0;
7305 if (ARM_DOUBLEWORD_ALIGN)
7306 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7307 if (mp->refcount > 0 && mp->fix_size == 8)
7309 align64 = 1;
7310 break;
7313 if (dump_file)
7314 fprintf (dump_file,
7315 ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7316 INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7318 scan = emit_label_after (gen_label_rtx (), scan);
7319 scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7320 scan = emit_label_after (minipool_vector_label, scan);
7322 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7324 if (mp->refcount > 0)
7326 if (dump_file)
7328 fprintf (dump_file,
7329 ";; Offset %u, min %ld, max %ld ",
7330 (unsigned) mp->offset, (unsigned long) mp->min_address,
7331 (unsigned long) mp->max_address);
7332 arm_print_value (dump_file, mp->value);
7333 fputc ('\n', dump_file);
7336 switch (mp->fix_size)
7338 #ifdef HAVE_consttable_1
7339 case 1:
7340 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7341 break;
7343 #endif
7344 #ifdef HAVE_consttable_2
7345 case 2:
7346 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7347 break;
7349 #endif
7350 #ifdef HAVE_consttable_4
7351 case 4:
7352 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7353 break;
7355 #endif
7356 #ifdef HAVE_consttable_8
7357 case 8:
7358 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7359 break;
7361 #endif
7362 default:
7363 abort ();
7364 break;
7368 nmp = mp->next;
7369 free (mp);
7372 minipool_vector_head = minipool_vector_tail = NULL;
7373 scan = emit_insn_after (gen_consttable_end (), scan);
7374 scan = emit_barrier_after (scan);
7377 /* Return the cost of forcibly inserting a barrier after INSN. */
7378 static int
7379 arm_barrier_cost (rtx insn)
7381 /* Basing the location of the pool on the loop depth is preferable,
7382 but at the moment, the basic block information seems to be
7383 corrupt by this stage of the compilation. */
7384 int base_cost = 50;
7385 rtx next = next_nonnote_insn (insn);
7387 if (next != NULL && GET_CODE (next) == CODE_LABEL)
7388 base_cost -= 20;
7390 switch (GET_CODE (insn))
7392 case CODE_LABEL:
7393 /* It will always be better to place the table before the label, rather
7394 than after it. */
7395 return 50;
7397 case INSN:
7398 case CALL_INSN:
7399 return base_cost;
7401 case JUMP_INSN:
7402 return base_cost - 10;
7404 default:
7405 return base_cost + 10;
7409 /* Find the best place in the insn stream in the range
7410 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7411 Create the barrier by inserting a jump and add a new fix entry for
7412 it. */
7413 static Mfix *
7414 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7416 HOST_WIDE_INT count = 0;
7417 rtx barrier;
7418 rtx from = fix->insn;
7419 rtx selected = from;
7420 int selected_cost;
7421 HOST_WIDE_INT selected_address;
7422 Mfix * new_fix;
7423 HOST_WIDE_INT max_count = max_address - fix->address;
7424 rtx label = gen_label_rtx ();
7426 selected_cost = arm_barrier_cost (from);
7427 selected_address = fix->address;
7429 while (from && count < max_count)
7431 rtx tmp;
7432 int new_cost;
7434 /* This code shouldn't have been called if there was a natural barrier
7435 within range. */
7436 if (GET_CODE (from) == BARRIER)
7437 abort ();
7439 /* Count the length of this insn. */
7440 count += get_attr_length (from);
7442 /* If there is a jump table, add its length. */
7443 tmp = is_jump_table (from);
7444 if (tmp != NULL)
7446 count += get_jump_table_size (tmp);
7448 /* Jump tables aren't in a basic block, so base the cost on
7449 the dispatch insn. If we select this location, we will
7450 still put the pool after the table. */
7451 new_cost = arm_barrier_cost (from);
7453 if (count < max_count && new_cost <= selected_cost)
7455 selected = tmp;
7456 selected_cost = new_cost;
7457 selected_address = fix->address + count;
7460 /* Continue after the dispatch table. */
7461 from = NEXT_INSN (tmp);
7462 continue;
7465 new_cost = arm_barrier_cost (from);
7467 if (count < max_count && new_cost <= selected_cost)
7469 selected = from;
7470 selected_cost = new_cost;
7471 selected_address = fix->address + count;
7474 from = NEXT_INSN (from);
7477 /* Create a new JUMP_INSN that branches around a barrier. */
7478 from = emit_jump_insn_after (gen_jump (label), selected);
7479 JUMP_LABEL (from) = label;
7480 barrier = emit_barrier_after (from);
7481 emit_label_after (label, barrier);
7483 /* Create a minipool barrier entry for the new barrier. */
7484 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7485 new_fix->insn = barrier;
7486 new_fix->address = selected_address;
7487 new_fix->next = fix->next;
7488 fix->next = new_fix;
7490 return new_fix;
7493 /* Record that there is a natural barrier in the insn stream at
7494 ADDRESS. */
7495 static void
7496 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7498 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7500 fix->insn = insn;
7501 fix->address = address;
7503 fix->next = NULL;
7504 if (minipool_fix_head != NULL)
7505 minipool_fix_tail->next = fix;
7506 else
7507 minipool_fix_head = fix;
7509 minipool_fix_tail = fix;
7512 /* Record INSN, which will need fixing up to load a value from the
7513 minipool. ADDRESS is the offset of the insn since the start of the
7514 function; LOC is a pointer to the part of the insn which requires
7515 fixing; VALUE is the constant that must be loaded, which is of type
7516 MODE. */
7517 static void
7518 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7519 enum machine_mode mode, rtx value)
7521 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7523 #ifdef AOF_ASSEMBLER
7524 /* PIC symbol references need to be converted into offsets into the
7525 based area. */
7526 /* XXX This shouldn't be done here. */
7527 if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7528 value = aof_pic_entry (value);
7529 #endif /* AOF_ASSEMBLER */
7531 fix->insn = insn;
7532 fix->address = address;
7533 fix->loc = loc;
7534 fix->mode = mode;
7535 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7536 fix->value = value;
7537 fix->forwards = get_attr_pool_range (insn);
7538 fix->backwards = get_attr_neg_pool_range (insn);
7539 fix->minipool = NULL;
7541 /* If an insn doesn't have a range defined for it, then it isn't
7542 expecting to be reworked by this code. Better to abort now than
7543 to generate duff assembly code. */
7544 if (fix->forwards == 0 && fix->backwards == 0)
7545 abort ();
7547 /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7548 So there might be an empty word before the start of the pool.
7549 Hence we reduce the forward range by 4 to allow for this
7550 possibility. */
7551 if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7552 fix->forwards -= 4;
7554 if (dump_file)
7556 fprintf (dump_file,
7557 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7558 GET_MODE_NAME (mode),
7559 INSN_UID (insn), (unsigned long) address,
7560 -1 * (long)fix->backwards, (long)fix->forwards);
7561 arm_print_value (dump_file, fix->value);
7562 fprintf (dump_file, "\n");
7565 /* Add it to the chain of fixes. */
7566 fix->next = NULL;
7568 if (minipool_fix_head != NULL)
7569 minipool_fix_tail->next = fix;
7570 else
7571 minipool_fix_head = fix;
7573 minipool_fix_tail = fix;
7576 /* Scan INSN and note any of its operands that need fixing.
7577 If DO_PUSHES is false we do not actually push any of the fixups
7578 needed. The function returns TRUE is any fixups were needed/pushed.
7579 This is used by arm_memory_load_p() which needs to know about loads
7580 of constants that will be converted into minipool loads. */
7581 static bool
7582 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7584 bool result = false;
7585 int opno;
7587 extract_insn (insn);
7589 if (!constrain_operands (1))
7590 fatal_insn_not_found (insn);
7592 if (recog_data.n_alternatives == 0)
7593 return false;
7595 /* Fill in recog_op_alt with information about the constraints of this insn. */
7596 preprocess_constraints ();
7598 for (opno = 0; opno < recog_data.n_operands; opno++)
7600 /* Things we need to fix can only occur in inputs. */
7601 if (recog_data.operand_type[opno] != OP_IN)
7602 continue;
7604 /* If this alternative is a memory reference, then any mention
7605 of constants in this alternative is really to fool reload
7606 into allowing us to accept one there. We need to fix them up
7607 now so that we output the right code. */
7608 if (recog_op_alt[opno][which_alternative].memory_ok)
7610 rtx op = recog_data.operand[opno];
7612 if (CONSTANT_P (op))
7614 if (do_pushes)
7615 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7616 recog_data.operand_mode[opno], op);
7617 result = true;
7619 else if (GET_CODE (op) == MEM
7620 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7621 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
7623 if (do_pushes)
7625 rtx cop = avoid_constant_pool_reference (op);
7627 /* Casting the address of something to a mode narrower
7628 than a word can cause avoid_constant_pool_reference()
7629 to return the pool reference itself. That's no good to
7630 us here. Lets just hope that we can use the
7631 constant pool value directly. */
7632 if (op == cop)
7633 cop = get_pool_constant (XEXP (op, 0));
7635 push_minipool_fix (insn, address,
7636 recog_data.operand_loc[opno],
7637 recog_data.operand_mode[opno], cop);
7640 result = true;
7645 return result;
7648 /* Gcc puts the pool in the wrong place for ARM, since we can only
7649 load addresses a limited distance around the pc. We do some
7650 special munging to move the constant pool values to the correct
7651 point in the code. */
7652 static void
7653 arm_reorg (void)
7655 rtx insn;
7656 HOST_WIDE_INT address = 0;
7657 Mfix * fix;
7659 minipool_fix_head = minipool_fix_tail = NULL;
7661 /* The first insn must always be a note, or the code below won't
7662 scan it properly. */
7663 insn = get_insns ();
7664 if (GET_CODE (insn) != NOTE)
7665 abort ();
7667 /* Scan all the insns and record the operands that will need fixing. */
7668 for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
7670 if (TARGET_CIRRUS_FIX_INVALID_INSNS
7671 && (arm_cirrus_insn_p (insn)
7672 || GET_CODE (insn) == JUMP_INSN
7673 || arm_memory_load_p (insn)))
7674 cirrus_reorg (insn);
7676 if (GET_CODE (insn) == BARRIER)
7677 push_minipool_barrier (insn, address);
7678 else if (INSN_P (insn))
7680 rtx table;
7682 note_invalid_constants (insn, address, true);
7683 address += get_attr_length (insn);
7685 /* If the insn is a vector jump, add the size of the table
7686 and skip the table. */
7687 if ((table = is_jump_table (insn)) != NULL)
7689 address += get_jump_table_size (table);
7690 insn = table;
7695 fix = minipool_fix_head;
7697 /* Now scan the fixups and perform the required changes. */
7698 while (fix)
7700 Mfix * ftmp;
7701 Mfix * fdel;
7702 Mfix * last_added_fix;
7703 Mfix * last_barrier = NULL;
7704 Mfix * this_fix;
7706 /* Skip any further barriers before the next fix. */
7707 while (fix && GET_CODE (fix->insn) == BARRIER)
7708 fix = fix->next;
7710 /* No more fixes. */
7711 if (fix == NULL)
7712 break;
7714 last_added_fix = NULL;
7716 for (ftmp = fix; ftmp; ftmp = ftmp->next)
7718 if (GET_CODE (ftmp->insn) == BARRIER)
7720 if (ftmp->address >= minipool_vector_head->max_address)
7721 break;
7723 last_barrier = ftmp;
7725 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7726 break;
7728 last_added_fix = ftmp; /* Keep track of the last fix added. */
7731 /* If we found a barrier, drop back to that; any fixes that we
7732 could have reached but come after the barrier will now go in
7733 the next mini-pool. */
7734 if (last_barrier != NULL)
7736 /* Reduce the refcount for those fixes that won't go into this
7737 pool after all. */
7738 for (fdel = last_barrier->next;
7739 fdel && fdel != ftmp;
7740 fdel = fdel->next)
7742 fdel->minipool->refcount--;
7743 fdel->minipool = NULL;
7746 ftmp = last_barrier;
7748 else
7750 /* ftmp is first fix that we can't fit into this pool and
7751 there no natural barriers that we could use. Insert a
7752 new barrier in the code somewhere between the previous
7753 fix and this one, and arrange to jump around it. */
7754 HOST_WIDE_INT max_address;
7756 /* The last item on the list of fixes must be a barrier, so
7757 we can never run off the end of the list of fixes without
7758 last_barrier being set. */
7759 if (ftmp == NULL)
7760 abort ();
7762 max_address = minipool_vector_head->max_address;
7763 /* Check that there isn't another fix that is in range that
7764 we couldn't fit into this pool because the pool was
7765 already too large: we need to put the pool before such an
7766 instruction. */
7767 if (ftmp->address < max_address)
7768 max_address = ftmp->address;
7770 last_barrier = create_fix_barrier (last_added_fix, max_address);
7773 assign_minipool_offsets (last_barrier);
7775 while (ftmp)
7777 if (GET_CODE (ftmp->insn) != BARRIER
7778 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7779 == NULL))
7780 break;
7782 ftmp = ftmp->next;
7785 /* Scan over the fixes we have identified for this pool, fixing them
7786 up and adding the constants to the pool itself. */
7787 for (this_fix = fix; this_fix && ftmp != this_fix;
7788 this_fix = this_fix->next)
7789 if (GET_CODE (this_fix->insn) != BARRIER)
7791 rtx addr
7792 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
7793 minipool_vector_label),
7794 this_fix->minipool->offset);
7795 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7798 dump_minipool (last_barrier->insn);
7799 fix = ftmp;
7802 /* From now on we must synthesize any constants that we can't handle
7803 directly. This can happen if the RTL gets split during final
7804 instruction generation. */
7805 after_arm_reorg = 1;
7807 /* Free the minipool memory. */
7808 obstack_free (&minipool_obstack, minipool_startobj);
7811 /* Routines to output assembly language. */
7813 /* If the rtx is the correct value then return the string of the number.
7814 In this way we can ensure that valid double constants are generated even
7815 when cross compiling. */
7816 const char *
7817 fp_immediate_constant (rtx x)
7819 REAL_VALUE_TYPE r;
7820 int i;
7822 if (!fp_consts_inited)
7823 init_fp_table ();
7825 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7826 for (i = 0; i < 8; i++)
7827 if (REAL_VALUES_EQUAL (r, values_fp[i]))
7828 return strings_fp[i];
7830 abort ();
7833 /* As for fp_immediate_constant, but value is passed directly, not in rtx. */
7834 static const char *
7835 fp_const_from_val (REAL_VALUE_TYPE *r)
7837 int i;
7839 if (!fp_consts_inited)
7840 init_fp_table ();
7842 for (i = 0; i < 8; i++)
7843 if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7844 return strings_fp[i];
7846 abort ();
7849 /* Output the operands of a LDM/STM instruction to STREAM.
7850 MASK is the ARM register set mask of which only bits 0-15 are important.
7851 REG is the base register, either the frame pointer or the stack pointer,
7852 INSTR is the possibly suffixed load or store instruction. */
7853 static void
7854 print_multi_reg (FILE *stream, const char *instr, int reg, int mask)
7856 int i;
7857 int not_first = FALSE;
7859 fputc ('\t', stream);
7860 asm_fprintf (stream, instr, reg);
7861 fputs (", {", stream);
7863 for (i = 0; i <= LAST_ARM_REGNUM; i++)
7864 if (mask & (1 << i))
7866 if (not_first)
7867 fprintf (stream, ", ");
7869 asm_fprintf (stream, "%r", i);
7870 not_first = TRUE;
7873 fprintf (stream, "}");
7875 /* Add a ^ character for the 26-bit ABI, but only if we were loading
7876 the PC. Otherwise we would generate an UNPREDICTABLE instruction.
7877 Strictly speaking the instruction would be unpredicatble only if
7878 we were writing back the base register as well, but since we never
7879 want to generate an LDM type 2 instruction (register bank switching)
7880 which is what you get if the PC is not being loaded, we do not need
7881 to check for writeback. */
7882 if (! TARGET_APCS_32
7883 && ((mask & (1 << PC_REGNUM)) != 0))
7884 fprintf (stream, "^");
7886 fprintf (stream, "\n");
7890 /* Output a FLDMX instruction to STREAM.
7891 BASE if the register containing the address.
7892 REG and COUNT specify the register range.
7893 Extra registers may be added to avoid hardware bugs. */
7895 static void
7896 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
7898 int i;
7900 /* Workaround ARM10 VFPr1 bug. */
7901 if (count == 2 && !arm_arch6)
7903 if (reg == 15)
7904 reg--;
7905 count++;
7908 fputc ('\t', stream);
7909 asm_fprintf (stream, "fldmfdx\t%r!, {", base);
7911 for (i = reg; i < reg + count; i++)
7913 if (i > reg)
7914 fputs (", ", stream);
7915 asm_fprintf (stream, "d%d", i);
7917 fputs ("}\n", stream);
7922 /* Output the assembly for a store multiple. */
7924 const char *
7925 vfp_output_fstmx (rtx * operands)
7927 char pattern[100];
7928 int p;
7929 int base;
7930 int i;
7932 strcpy (pattern, "fstmfdx\t%m0!, {%P1");
7933 p = strlen (pattern);
7935 if (GET_CODE (operands[1]) != REG)
7936 abort ();
7938 base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
7939 for (i = 1; i < XVECLEN (operands[2], 0); i++)
7941 p += sprintf (&pattern[p], ", d%d", base + i);
7943 strcpy (&pattern[p], "}");
7945 output_asm_insn (pattern, operands);
7946 return "";
7950 /* Emit RTL to save block of VFP register pairs to the stack. Returns the
7951 number of bytes pushed. */
7953 static int
7954 vfp_emit_fstmx (int base_reg, int count)
7956 rtx par;
7957 rtx dwarf;
7958 rtx tmp, reg;
7959 int i;
7961 /* Workaround ARM10 VFPr1 bug. Data corruption can occur when exactly two
7962 register pairs are stored by a store multiple insn. We avoid this
7963 by pushing an extra pair. */
7964 if (count == 2 && !arm_arch6)
7966 if (base_reg == LAST_VFP_REGNUM - 3)
7967 base_reg -= 2;
7968 count++;
7971 /* ??? The frame layout is implementation defined. We describe
7972 standard format 1 (equivalent to a FSTMD insn and unused pad word).
7973 We really need some way of representing the whole block so that the
7974 unwinder can figure it out at runtime. */
7975 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7976 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
7978 reg = gen_rtx_REG (DFmode, base_reg);
7979 base_reg += 2;
7981 XVECEXP (par, 0, 0)
7982 = gen_rtx_SET (VOIDmode,
7983 gen_rtx_MEM (BLKmode,
7984 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7985 gen_rtx_UNSPEC (BLKmode,
7986 gen_rtvec (1, reg),
7987 UNSPEC_PUSH_MULT));
7989 tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
7990 gen_rtx_PLUS (SImode, stack_pointer_rtx,
7991 GEN_INT (-(count * 8 + 4))));
7992 RTX_FRAME_RELATED_P (tmp) = 1;
7993 XVECEXP (dwarf, 0, 0) = tmp;
7995 tmp = gen_rtx_SET (VOIDmode,
7996 gen_rtx_MEM (DFmode, stack_pointer_rtx),
7997 reg);
7998 RTX_FRAME_RELATED_P (tmp) = 1;
7999 XVECEXP (dwarf, 0, 1) = tmp;
8001 for (i = 1; i < count; i++)
8003 reg = gen_rtx_REG (DFmode, base_reg);
8004 base_reg += 2;
8005 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
8007 tmp = gen_rtx_SET (VOIDmode,
8008 gen_rtx_MEM (DFmode,
8009 gen_rtx_PLUS (SImode,
8010 stack_pointer_rtx,
8011 GEN_INT (i * 8))),
8012 reg);
8013 RTX_FRAME_RELATED_P (tmp) = 1;
8014 XVECEXP (dwarf, 0, i + 1) = tmp;
8017 par = emit_insn (par);
8018 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8019 REG_NOTES (par));
8020 RTX_FRAME_RELATED_P (par) = 1;
8022 return count * 8 + 4;
8026 /* Output a 'call' insn. */
8027 const char *
8028 output_call (rtx *operands)
8030 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
8032 if (REGNO (operands[0]) == LR_REGNUM)
8034 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8035 output_asm_insn ("mov%?\t%0, %|lr", operands);
8038 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8040 if (TARGET_INTERWORK)
8041 output_asm_insn ("bx%?\t%0", operands);
8042 else
8043 output_asm_insn ("mov%?\t%|pc, %0", operands);
8045 return "";
8048 /* Output a 'call' insn that is a reference in memory. */
8049 const char *
8050 output_call_mem (rtx *operands)
8052 if (TARGET_INTERWORK)
8054 output_asm_insn ("ldr%?\t%|ip, %0", operands);
8055 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8056 output_asm_insn ("bx%?\t%|ip", operands);
8058 else if (regno_use_in (LR_REGNUM, operands[0]))
8060 /* LR is used in the memory address. We load the address in the
8061 first instruction. It's safe to use IP as the target of the
8062 load since the call will kill it anyway. */
8063 output_asm_insn ("ldr%?\t%|ip, %0", operands);
8064 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8065 output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8067 else
8069 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8070 output_asm_insn ("ldr%?\t%|pc, %0", operands);
8073 return "";
8077 /* Output a move from arm registers to an fpa registers.
8078 OPERANDS[0] is an fpa register.
8079 OPERANDS[1] is the first registers of an arm register pair. */
8080 const char *
8081 output_mov_long_double_fpa_from_arm (rtx *operands)
8083 int arm_reg0 = REGNO (operands[1]);
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 ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
8094 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8096 return "";
8099 /* Output a move from an fpa register to arm registers.
8100 OPERANDS[0] is the first registers of an arm register pair.
8101 OPERANDS[1] is an fpa register. */
8102 const char *
8103 output_mov_long_double_arm_from_fpa (rtx *operands)
8105 int arm_reg0 = REGNO (operands[0]);
8106 rtx ops[3];
8108 if (arm_reg0 == IP_REGNUM)
8109 abort ();
8111 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8112 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8113 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8115 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8116 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8117 return "";
8120 /* Output a move from arm registers to arm registers of a long double
8121 OPERANDS[0] is the destination.
8122 OPERANDS[1] is the source. */
8123 const char *
8124 output_mov_long_double_arm_from_arm (rtx *operands)
8126 /* We have to be careful here because the two might overlap. */
8127 int dest_start = REGNO (operands[0]);
8128 int src_start = REGNO (operands[1]);
8129 rtx ops[2];
8130 int i;
8132 if (dest_start < src_start)
8134 for (i = 0; i < 3; i++)
8136 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8137 ops[1] = gen_rtx_REG (SImode, src_start + i);
8138 output_asm_insn ("mov%?\t%0, %1", ops);
8141 else
8143 for (i = 2; i >= 0; i--)
8145 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8146 ops[1] = gen_rtx_REG (SImode, src_start + i);
8147 output_asm_insn ("mov%?\t%0, %1", ops);
8151 return "";
8155 /* Output a move from arm registers to an fpa registers.
8156 OPERANDS[0] is an fpa register.
8157 OPERANDS[1] is the first registers of an arm register pair. */
8158 const char *
8159 output_mov_double_fpa_from_arm (rtx *operands)
8161 int arm_reg0 = REGNO (operands[1]);
8162 rtx ops[2];
8164 if (arm_reg0 == IP_REGNUM)
8165 abort ();
8167 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8168 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8169 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8170 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8171 return "";
8174 /* Output a move from an fpa register to arm registers.
8175 OPERANDS[0] is the first registers of an arm register pair.
8176 OPERANDS[1] is an fpa register. */
8177 const char *
8178 output_mov_double_arm_from_fpa (rtx *operands)
8180 int arm_reg0 = REGNO (operands[0]);
8181 rtx ops[2];
8183 if (arm_reg0 == IP_REGNUM)
8184 abort ();
8186 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8187 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8188 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8189 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8190 return "";
8193 /* Output a move between double words.
8194 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8195 or MEM<-REG and all MEMs must be offsettable addresses. */
8196 const char *
8197 output_move_double (rtx *operands)
8199 enum rtx_code code0 = GET_CODE (operands[0]);
8200 enum rtx_code code1 = GET_CODE (operands[1]);
8201 rtx otherops[3];
8203 if (code0 == REG)
8205 int reg0 = REGNO (operands[0]);
8207 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8209 if (code1 == REG)
8211 int reg1 = REGNO (operands[1]);
8212 if (reg1 == IP_REGNUM)
8213 abort ();
8215 /* Ensure the second source is not overwritten. */
8216 if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
8217 output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
8218 else
8219 output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
8221 else if (code1 == CONST_VECTOR)
8223 HOST_WIDE_INT hint = 0;
8225 switch (GET_MODE (operands[1]))
8227 case V2SImode:
8228 otherops[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 1)));
8229 operands[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 0)));
8230 break;
8232 case V4HImode:
8233 if (BYTES_BIG_ENDIAN)
8235 hint = INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8236 hint <<= 16;
8237 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8239 else
8241 hint = INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8242 hint <<= 16;
8243 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8246 otherops[1] = GEN_INT (hint);
8247 hint = 0;
8249 if (BYTES_BIG_ENDIAN)
8251 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8252 hint <<= 16;
8253 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8255 else
8257 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8258 hint <<= 16;
8259 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8262 operands[1] = GEN_INT (hint);
8263 break;
8265 case V8QImode:
8266 if (BYTES_BIG_ENDIAN)
8268 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8269 hint <<= 8;
8270 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8271 hint <<= 8;
8272 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8273 hint <<= 8;
8274 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8276 else
8278 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8279 hint <<= 8;
8280 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8281 hint <<= 8;
8282 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8283 hint <<= 8;
8284 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8287 otherops[1] = GEN_INT (hint);
8288 hint = 0;
8290 if (BYTES_BIG_ENDIAN)
8292 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8293 hint <<= 8;
8294 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8295 hint <<= 8;
8296 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8297 hint <<= 8;
8298 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8300 else
8302 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8303 hint <<= 8;
8304 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8305 hint <<= 8;
8306 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8307 hint <<= 8;
8308 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8311 operands[1] = GEN_INT (hint);
8312 break;
8314 default:
8315 abort ();
8317 output_mov_immediate (operands);
8318 output_mov_immediate (otherops);
8320 else if (code1 == CONST_DOUBLE)
8322 if (GET_MODE (operands[1]) == DFmode)
8324 REAL_VALUE_TYPE r;
8325 long l[2];
8327 REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
8328 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
8329 otherops[1] = GEN_INT (l[1]);
8330 operands[1] = GEN_INT (l[0]);
8332 else if (GET_MODE (operands[1]) != VOIDmode)
8333 abort ();
8334 else if (WORDS_BIG_ENDIAN)
8336 otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8337 operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8339 else
8341 otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8342 operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8345 output_mov_immediate (operands);
8346 output_mov_immediate (otherops);
8348 else if (code1 == CONST_INT)
8350 #if HOST_BITS_PER_WIDE_INT > 32
8351 /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
8352 what the upper word is. */
8353 if (WORDS_BIG_ENDIAN)
8355 otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8356 operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8358 else
8360 otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8361 operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8363 #else
8364 /* Sign extend the intval into the high-order word. */
8365 if (WORDS_BIG_ENDIAN)
8367 otherops[1] = operands[1];
8368 operands[1] = (INTVAL (operands[1]) < 0
8369 ? constm1_rtx : const0_rtx);
8371 else
8372 otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
8373 #endif
8374 output_mov_immediate (otherops);
8375 output_mov_immediate (operands);
8377 else if (code1 == MEM)
8379 switch (GET_CODE (XEXP (operands[1], 0)))
8381 case REG:
8382 output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8383 break;
8385 case PRE_INC:
8386 abort (); /* Should never happen now. */
8387 break;
8389 case PRE_DEC:
8390 output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8391 break;
8393 case POST_INC:
8394 output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8395 break;
8397 case POST_DEC:
8398 abort (); /* Should never happen now. */
8399 break;
8401 case LABEL_REF:
8402 case CONST:
8403 output_asm_insn ("adr%?\t%0, %1", operands);
8404 output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8405 break;
8407 default:
8408 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8409 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8411 otherops[0] = operands[0];
8412 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8413 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8415 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8417 if (GET_CODE (otherops[2]) == CONST_INT)
8419 switch ((int) INTVAL (otherops[2]))
8421 case -8:
8422 output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8423 return "";
8424 case -4:
8425 output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8426 return "";
8427 case 4:
8428 output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8429 return "";
8432 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8433 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8434 else
8435 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8437 else
8438 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8440 else
8441 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8443 return "ldm%?ia\t%0, %M0";
8445 else
8447 otherops[1] = adjust_address (operands[1], SImode, 4);
8448 /* Take care of overlapping base/data reg. */
8449 if (reg_mentioned_p (operands[0], operands[1]))
8451 output_asm_insn ("ldr%?\t%0, %1", otherops);
8452 output_asm_insn ("ldr%?\t%0, %1", operands);
8454 else
8456 output_asm_insn ("ldr%?\t%0, %1", operands);
8457 output_asm_insn ("ldr%?\t%0, %1", otherops);
8462 else
8463 abort (); /* Constraints should prevent this. */
8465 else if (code0 == MEM && code1 == REG)
8467 if (REGNO (operands[1]) == IP_REGNUM)
8468 abort ();
8470 switch (GET_CODE (XEXP (operands[0], 0)))
8472 case REG:
8473 output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8474 break;
8476 case PRE_INC:
8477 abort (); /* Should never happen now. */
8478 break;
8480 case PRE_DEC:
8481 output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8482 break;
8484 case POST_INC:
8485 output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8486 break;
8488 case POST_DEC:
8489 abort (); /* Should never happen now. */
8490 break;
8492 case PLUS:
8493 if (GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == CONST_INT)
8495 switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8497 case -8:
8498 output_asm_insn ("stm%?db\t%m0, %M1", operands);
8499 return "";
8501 case -4:
8502 output_asm_insn ("stm%?da\t%m0, %M1", operands);
8503 return "";
8505 case 4:
8506 output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8507 return "";
8510 /* Fall through */
8512 default:
8513 otherops[0] = adjust_address (operands[0], SImode, 4);
8514 otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8515 output_asm_insn ("str%?\t%1, %0", operands);
8516 output_asm_insn ("str%?\t%1, %0", otherops);
8519 else
8520 /* Constraints should prevent this. */
8521 abort ();
8523 return "";
8527 /* Output an arbitrary MOV reg, #n.
8528 OPERANDS[0] is a register. OPERANDS[1] is a const_int. */
8529 const char *
8530 output_mov_immediate (rtx *operands)
8532 HOST_WIDE_INT n = INTVAL (operands[1]);
8534 /* Try to use one MOV. */
8535 if (const_ok_for_arm (n))
8536 output_asm_insn ("mov%?\t%0, %1", operands);
8538 /* Try to use one MVN. */
8539 else if (const_ok_for_arm (~n))
8541 operands[1] = GEN_INT (~n);
8542 output_asm_insn ("mvn%?\t%0, %1", operands);
8544 else
8546 int n_ones = 0;
8547 int i;
8549 /* If all else fails, make it out of ORRs or BICs as appropriate. */
8550 for (i = 0; i < 32; i++)
8551 if (n & 1 << i)
8552 n_ones++;
8554 if (n_ones > 16) /* Shorter to use MVN with BIC in this case. */
8555 output_multi_immediate (operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1, ~ n);
8556 else
8557 output_multi_immediate (operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1, n);
8560 return "";
8563 /* Output an ADD r, s, #n where n may be too big for one instruction.
8564 If adding zero to one register, output nothing. */
8565 const char *
8566 output_add_immediate (rtx *operands)
8568 HOST_WIDE_INT n = INTVAL (operands[2]);
8570 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8572 if (n < 0)
8573 output_multi_immediate (operands,
8574 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8575 -n);
8576 else
8577 output_multi_immediate (operands,
8578 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8582 return "";
8585 /* Output a multiple immediate operation.
8586 OPERANDS is the vector of operands referred to in the output patterns.
8587 INSTR1 is the output pattern to use for the first constant.
8588 INSTR2 is the output pattern to use for subsequent constants.
8589 IMMED_OP is the index of the constant slot in OPERANDS.
8590 N is the constant value. */
8591 static const char *
8592 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8593 int immed_op, HOST_WIDE_INT n)
8595 #if HOST_BITS_PER_WIDE_INT > 32
8596 n &= 0xffffffff;
8597 #endif
8599 if (n == 0)
8601 /* Quick and easy output. */
8602 operands[immed_op] = const0_rtx;
8603 output_asm_insn (instr1, operands);
8605 else
8607 int i;
8608 const char * instr = instr1;
8610 /* Note that n is never zero here (which would give no output). */
8611 for (i = 0; i < 32; i += 2)
8613 if (n & (3 << i))
8615 operands[immed_op] = GEN_INT (n & (255 << i));
8616 output_asm_insn (instr, operands);
8617 instr = instr2;
8618 i += 6;
8623 return "";
8626 /* Return the appropriate ARM instruction for the operation code.
8627 The returned result should not be overwritten. OP is the rtx of the
8628 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8629 was shifted. */
8630 const char *
8631 arithmetic_instr (rtx op, int shift_first_arg)
8633 switch (GET_CODE (op))
8635 case PLUS:
8636 return "add";
8638 case MINUS:
8639 return shift_first_arg ? "rsb" : "sub";
8641 case IOR:
8642 return "orr";
8644 case XOR:
8645 return "eor";
8647 case AND:
8648 return "and";
8650 default:
8651 abort ();
8655 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8656 for the operation code. The returned result should not be overwritten.
8657 OP is the rtx code of the shift.
8658 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8659 shift. */
8660 static const char *
8661 shift_op (rtx op, HOST_WIDE_INT *amountp)
8663 const char * mnem;
8664 enum rtx_code code = GET_CODE (op);
8666 if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
8667 *amountp = -1;
8668 else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
8669 *amountp = INTVAL (XEXP (op, 1));
8670 else
8671 abort ();
8673 switch (code)
8675 case ASHIFT:
8676 mnem = "asl";
8677 break;
8679 case ASHIFTRT:
8680 mnem = "asr";
8681 break;
8683 case LSHIFTRT:
8684 mnem = "lsr";
8685 break;
8687 case ROTATERT:
8688 mnem = "ror";
8689 break;
8691 case MULT:
8692 /* We never have to worry about the amount being other than a
8693 power of 2, since this case can never be reloaded from a reg. */
8694 if (*amountp != -1)
8695 *amountp = int_log2 (*amountp);
8696 else
8697 abort ();
8698 return "asl";
8700 default:
8701 abort ();
8704 if (*amountp != -1)
8706 /* This is not 100% correct, but follows from the desire to merge
8707 multiplication by a power of 2 with the recognizer for a
8708 shift. >=32 is not a valid shift for "asl", so we must try and
8709 output a shift that produces the correct arithmetical result.
8710 Using lsr #32 is identical except for the fact that the carry bit
8711 is not set correctly if we set the flags; but we never use the
8712 carry bit from such an operation, so we can ignore that. */
8713 if (code == ROTATERT)
8714 /* Rotate is just modulo 32. */
8715 *amountp &= 31;
8716 else if (*amountp != (*amountp & 31))
8718 if (code == ASHIFT)
8719 mnem = "lsr";
8720 *amountp = 32;
8723 /* Shifts of 0 are no-ops. */
8724 if (*amountp == 0)
8725 return NULL;
8728 return mnem;
8731 /* Obtain the shift from the POWER of two. */
8733 static HOST_WIDE_INT
8734 int_log2 (HOST_WIDE_INT power)
8736 HOST_WIDE_INT shift = 0;
8738 while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8740 if (shift > 31)
8741 abort ();
8742 shift++;
8745 return shift;
8748 /* Output a .ascii pseudo-op, keeping track of lengths. This is because
8749 /bin/as is horribly restrictive. */
8750 #define MAX_ASCII_LEN 51
8752 void
8753 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
8755 int i;
8756 int len_so_far = 0;
8758 fputs ("\t.ascii\t\"", stream);
8760 for (i = 0; i < len; i++)
8762 int c = p[i];
8764 if (len_so_far >= MAX_ASCII_LEN)
8766 fputs ("\"\n\t.ascii\t\"", stream);
8767 len_so_far = 0;
8770 switch (c)
8772 case TARGET_TAB:
8773 fputs ("\\t", stream);
8774 len_so_far += 2;
8775 break;
8777 case TARGET_FF:
8778 fputs ("\\f", stream);
8779 len_so_far += 2;
8780 break;
8782 case TARGET_BS:
8783 fputs ("\\b", stream);
8784 len_so_far += 2;
8785 break;
8787 case TARGET_CR:
8788 fputs ("\\r", stream);
8789 len_so_far += 2;
8790 break;
8792 case TARGET_NEWLINE:
8793 fputs ("\\n", stream);
8794 c = p [i + 1];
8795 if ((c >= ' ' && c <= '~')
8796 || c == TARGET_TAB)
8797 /* This is a good place for a line break. */
8798 len_so_far = MAX_ASCII_LEN;
8799 else
8800 len_so_far += 2;
8801 break;
8803 case '\"':
8804 case '\\':
8805 putc ('\\', stream);
8806 len_so_far++;
8807 /* Drop through. */
8809 default:
8810 if (c >= ' ' && c <= '~')
8812 putc (c, stream);
8813 len_so_far++;
8815 else
8817 fprintf (stream, "\\%03o", c);
8818 len_so_far += 4;
8820 break;
8824 fputs ("\"\n", stream);
8827 /* Compute the register sabe mask for registers 0 through 12
8828 inclusive. This code is used by arm_compute_save_reg_mask. */
8829 static unsigned long
8830 arm_compute_save_reg0_reg12_mask (void)
8832 unsigned long func_type = arm_current_func_type ();
8833 unsigned int save_reg_mask = 0;
8834 unsigned int reg;
8836 if (IS_INTERRUPT (func_type))
8838 unsigned int max_reg;
8839 /* Interrupt functions must not corrupt any registers,
8840 even call clobbered ones. If this is a leaf function
8841 we can just examine the registers used by the RTL, but
8842 otherwise we have to assume that whatever function is
8843 called might clobber anything, and so we have to save
8844 all the call-clobbered registers as well. */
8845 if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8846 /* FIQ handlers have registers r8 - r12 banked, so
8847 we only need to check r0 - r7, Normal ISRs only
8848 bank r14 and r15, so we must check up to r12.
8849 r13 is the stack pointer which is always preserved,
8850 so we do not need to consider it here. */
8851 max_reg = 7;
8852 else
8853 max_reg = 12;
8855 for (reg = 0; reg <= max_reg; reg++)
8856 if (regs_ever_live[reg]
8857 || (! current_function_is_leaf && call_used_regs [reg]))
8858 save_reg_mask |= (1 << reg);
8860 else
8862 /* In the normal case we only need to save those registers
8863 which are call saved and which are used by this function. */
8864 for (reg = 0; reg <= 10; reg++)
8865 if (regs_ever_live[reg] && ! call_used_regs [reg])
8866 save_reg_mask |= (1 << reg);
8868 /* Handle the frame pointer as a special case. */
8869 if (! TARGET_APCS_FRAME
8870 && ! frame_pointer_needed
8871 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8872 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8873 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8875 /* If we aren't loading the PIC register,
8876 don't stack it even though it may be live. */
8877 if (flag_pic
8878 && ! TARGET_SINGLE_PIC_BASE
8879 && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
8880 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8883 return save_reg_mask;
8886 /* Compute a bit mask of which registers need to be
8887 saved on the stack for the current function. */
8889 static unsigned long
8890 arm_compute_save_reg_mask (void)
8892 unsigned int save_reg_mask = 0;
8893 unsigned long func_type = arm_current_func_type ();
8895 if (IS_NAKED (func_type))
8896 /* This should never really happen. */
8897 return 0;
8899 /* If we are creating a stack frame, then we must save the frame pointer,
8900 IP (which will hold the old stack pointer), LR and the PC. */
8901 if (frame_pointer_needed)
8902 save_reg_mask |=
8903 (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8904 | (1 << IP_REGNUM)
8905 | (1 << LR_REGNUM)
8906 | (1 << PC_REGNUM);
8908 /* Volatile functions do not return, so there
8909 is no need to save any other registers. */
8910 if (IS_VOLATILE (func_type))
8911 return save_reg_mask;
8913 save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8915 /* Decide if we need to save the link register.
8916 Interrupt routines have their own banked link register,
8917 so they never need to save it.
8918 Otherwise if we do not use the link register we do not need to save
8919 it. If we are pushing other registers onto the stack however, we
8920 can save an instruction in the epilogue by pushing the link register
8921 now and then popping it back into the PC. This incurs extra memory
8922 accesses though, so we only do it when optimizing for size, and only
8923 if we know that we will not need a fancy return sequence. */
8924 if (regs_ever_live [LR_REGNUM]
8925 || (save_reg_mask
8926 && optimize_size
8927 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL))
8928 save_reg_mask |= 1 << LR_REGNUM;
8930 if (cfun->machine->lr_save_eliminated)
8931 save_reg_mask &= ~ (1 << LR_REGNUM);
8933 if (TARGET_REALLY_IWMMXT
8934 && ((bit_count (save_reg_mask)
8935 + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
8937 unsigned int reg;
8939 /* The total number of registers that are going to be pushed
8940 onto the stack is odd. We need to ensure that the stack
8941 is 64-bit aligned before we start to save iWMMXt registers,
8942 and also before we start to create locals. (A local variable
8943 might be a double or long long which we will load/store using
8944 an iWMMXt instruction). Therefore we need to push another
8945 ARM register, so that the stack will be 64-bit aligned. We
8946 try to avoid using the arg registers (r0 -r3) as they might be
8947 used to pass values in a tail call. */
8948 for (reg = 4; reg <= 12; reg++)
8949 if ((save_reg_mask & (1 << reg)) == 0)
8950 break;
8952 if (reg <= 12)
8953 save_reg_mask |= (1 << reg);
8954 else
8956 cfun->machine->sibcall_blocked = 1;
8957 save_reg_mask |= (1 << 3);
8961 return save_reg_mask;
8965 /* Return the number of bytes required to save VFP registers. */
8966 static int
8967 arm_get_vfp_saved_size (void)
8969 unsigned int regno;
8970 int count;
8971 int saved;
8973 saved = 0;
8974 /* Space for saved VFP registers. */
8975 if (TARGET_HARD_FLOAT && TARGET_VFP)
8977 count = 0;
8978 for (regno = FIRST_VFP_REGNUM;
8979 regno < LAST_VFP_REGNUM;
8980 regno += 2)
8982 if ((!regs_ever_live[regno] || call_used_regs[regno])
8983 && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
8985 if (count > 0)
8987 /* Workaround ARM10 VFPr1 bug. */
8988 if (count == 2 && !arm_arch6)
8989 count++;
8990 saved += count * 8 + 4;
8992 count = 0;
8994 else
8995 count++;
8997 if (count > 0)
8999 if (count == 2 && !arm_arch6)
9000 count++;
9001 saved += count * 8 + 4;
9004 return saved;
9008 /* Generate a function exit sequence. If REALLY_RETURN is false, then do
9009 everything bar the final return instruction. */
9010 const char *
9011 output_return_instruction (rtx operand, int really_return, int reverse)
9013 char conditional[10];
9014 char instr[100];
9015 int reg;
9016 unsigned long live_regs_mask;
9017 unsigned long func_type;
9018 arm_stack_offsets *offsets;
9020 func_type = arm_current_func_type ();
9022 if (IS_NAKED (func_type))
9023 return "";
9025 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9027 /* If this function was declared non-returning, and we have
9028 found a tail call, then we have to trust that the called
9029 function won't return. */
9030 if (really_return)
9032 rtx ops[2];
9034 /* Otherwise, trap an attempted return by aborting. */
9035 ops[0] = operand;
9036 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
9037 : "abort");
9038 assemble_external_libcall (ops[1]);
9039 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9042 return "";
9045 if (current_function_calls_alloca && !really_return)
9046 abort ();
9048 sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9050 return_used_this_function = 1;
9052 live_regs_mask = arm_compute_save_reg_mask ();
9054 if (live_regs_mask)
9056 const char * return_reg;
9058 /* If we do not have any special requirements for function exit
9059 (eg interworking, or ISR) then we can load the return address
9060 directly into the PC. Otherwise we must load it into LR. */
9061 if (really_return
9062 && ! TARGET_INTERWORK)
9063 return_reg = reg_names[PC_REGNUM];
9064 else
9065 return_reg = reg_names[LR_REGNUM];
9067 if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9069 /* There are three possible reasons for the IP register
9070 being saved. 1) a stack frame was created, in which case
9071 IP contains the old stack pointer, or 2) an ISR routine
9072 corrupted it, or 3) it was saved to align the stack on
9073 iWMMXt. In case 1, restore IP into SP, otherwise just
9074 restore IP. */
9075 if (frame_pointer_needed)
9077 live_regs_mask &= ~ (1 << IP_REGNUM);
9078 live_regs_mask |= (1 << SP_REGNUM);
9080 else
9082 if (! IS_INTERRUPT (func_type)
9083 && ! TARGET_REALLY_IWMMXT)
9084 abort ();
9088 /* On some ARM architectures it is faster to use LDR rather than
9089 LDM to load a single register. On other architectures, the
9090 cost is the same. In 26 bit mode, or for exception handlers,
9091 we have to use LDM to load the PC so that the CPSR is also
9092 restored. */
9093 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9095 if (live_regs_mask == (unsigned int)(1 << reg))
9096 break;
9098 if (reg <= LAST_ARM_REGNUM
9099 && (reg != LR_REGNUM
9100 || ! really_return
9101 || (TARGET_APCS_32 && ! IS_INTERRUPT (func_type))))
9103 sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
9104 (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
9106 else
9108 char *p;
9109 int first = 1;
9111 /* Generate the load multiple instruction to restore the
9112 registers. Note we can get here, even if
9113 frame_pointer_needed is true, but only if sp already
9114 points to the base of the saved core registers. */
9115 if (live_regs_mask & (1 << SP_REGNUM))
9117 unsigned HOST_WIDE_INT stack_adjust;
9119 offsets = arm_get_frame_offsets ();
9120 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9121 if (stack_adjust != 0 && stack_adjust != 4)
9122 abort ();
9124 if (stack_adjust && arm_arch5)
9125 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9126 else
9128 /* If we can't use ldmib (SA110 bug), then try to pop r3
9129 instead. */
9130 if (stack_adjust)
9131 live_regs_mask |= 1 << 3;
9132 sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9135 else
9136 sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9138 p = instr + strlen (instr);
9140 for (reg = 0; reg <= SP_REGNUM; reg++)
9141 if (live_regs_mask & (1 << reg))
9143 int l = strlen (reg_names[reg]);
9145 if (first)
9146 first = 0;
9147 else
9149 memcpy (p, ", ", 2);
9150 p += 2;
9153 memcpy (p, "%|", 2);
9154 memcpy (p + 2, reg_names[reg], l);
9155 p += l + 2;
9158 if (live_regs_mask & (1 << LR_REGNUM))
9160 sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9161 /* Decide if we need to add the ^ symbol to the end of the
9162 register list. This causes the saved condition codes
9163 register to be copied into the current condition codes
9164 register. We do the copy if we are conforming to the 32-bit
9165 ABI and this is an interrupt function, or if we are
9166 conforming to the 26-bit ABI. There is a special case for
9167 the 26-bit ABI however, which is if we are writing back the
9168 stack pointer but not loading the PC. In this case adding
9169 the ^ symbol would create a type 2 LDM instruction, where
9170 writeback is UNPREDICTABLE. We are safe in leaving the ^
9171 character off in this case however, since the actual return
9172 instruction will be a MOVS which will restore the CPSR. */
9173 if ((TARGET_APCS_32 && IS_INTERRUPT (func_type))
9174 || (! TARGET_APCS_32 && really_return))
9175 strcat (p, "^");
9177 else
9178 strcpy (p, "}");
9181 output_asm_insn (instr, & operand);
9183 /* See if we need to generate an extra instruction to
9184 perform the actual function return. */
9185 if (really_return
9186 && func_type != ARM_FT_INTERWORKED
9187 && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9189 /* The return has already been handled
9190 by loading the LR into the PC. */
9191 really_return = 0;
9195 if (really_return)
9197 switch ((int) ARM_FUNC_TYPE (func_type))
9199 case ARM_FT_ISR:
9200 case ARM_FT_FIQ:
9201 sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9202 break;
9204 case ARM_FT_INTERWORKED:
9205 sprintf (instr, "bx%s\t%%|lr", conditional);
9206 break;
9208 case ARM_FT_EXCEPTION:
9209 sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9210 break;
9212 default:
9213 /* ARMv5 implementations always provide BX, so interworking
9214 is the default unless APCS-26 is in use. */
9215 if ((insn_flags & FL_ARCH5) != 0 && TARGET_APCS_32)
9216 sprintf (instr, "bx%s\t%%|lr", conditional);
9217 else
9218 sprintf (instr, "mov%s%s\t%%|pc, %%|lr",
9219 conditional, TARGET_APCS_32 ? "" : "s");
9220 break;
9223 output_asm_insn (instr, & operand);
9226 return "";
9229 /* Write the function name into the code section, directly preceding
9230 the function prologue.
9232 Code will be output similar to this:
9234 .ascii "arm_poke_function_name", 0
9235 .align
9237 .word 0xff000000 + (t1 - t0)
9238 arm_poke_function_name
9239 mov ip, sp
9240 stmfd sp!, {fp, ip, lr, pc}
9241 sub fp, ip, #4
9243 When performing a stack backtrace, code can inspect the value
9244 of 'pc' stored at 'fp' + 0. If the trace function then looks
9245 at location pc - 12 and the top 8 bits are set, then we know
9246 that there is a function name embedded immediately preceding this
9247 location and has length ((pc[-3]) & 0xff000000).
9249 We assume that pc is declared as a pointer to an unsigned long.
9251 It is of no benefit to output the function name if we are assembling
9252 a leaf function. These function types will not contain a stack
9253 backtrace structure, therefore it is not possible to determine the
9254 function name. */
9255 void
9256 arm_poke_function_name (FILE *stream, const char *name)
9258 unsigned long alignlength;
9259 unsigned long length;
9260 rtx x;
9262 length = strlen (name) + 1;
9263 alignlength = ROUND_UP_WORD (length);
9265 ASM_OUTPUT_ASCII (stream, name, length);
9266 ASM_OUTPUT_ALIGN (stream, 2);
9267 x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9268 assemble_aligned_integer (UNITS_PER_WORD, x);
9271 /* Place some comments into the assembler stream
9272 describing the current function. */
9273 static void
9274 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9276 unsigned long func_type;
9278 if (!TARGET_ARM)
9280 thumb_output_function_prologue (f, frame_size);
9281 return;
9284 /* Sanity check. */
9285 if (arm_ccfsm_state || arm_target_insn)
9286 abort ();
9288 func_type = arm_current_func_type ();
9290 switch ((int) ARM_FUNC_TYPE (func_type))
9292 default:
9293 case ARM_FT_NORMAL:
9294 break;
9295 case ARM_FT_INTERWORKED:
9296 asm_fprintf (f, "\t%@ Function supports interworking.\n");
9297 break;
9298 case ARM_FT_EXCEPTION_HANDLER:
9299 asm_fprintf (f, "\t%@ C++ Exception Handler.\n");
9300 break;
9301 case ARM_FT_ISR:
9302 asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9303 break;
9304 case ARM_FT_FIQ:
9305 asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9306 break;
9307 case ARM_FT_EXCEPTION:
9308 asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9309 break;
9312 if (IS_NAKED (func_type))
9313 asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9315 if (IS_VOLATILE (func_type))
9316 asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9318 if (IS_NESTED (func_type))
9319 asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9321 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9322 current_function_args_size,
9323 current_function_pretend_args_size, frame_size);
9325 asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9326 frame_pointer_needed,
9327 cfun->machine->uses_anonymous_args);
9329 if (cfun->machine->lr_save_eliminated)
9330 asm_fprintf (f, "\t%@ link register save eliminated.\n");
9332 #ifdef AOF_ASSEMBLER
9333 if (flag_pic)
9334 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9335 #endif
9337 return_used_this_function = 0;
9340 const char *
9341 arm_output_epilogue (rtx sibling)
9343 int reg;
9344 unsigned long saved_regs_mask;
9345 unsigned long func_type;
9346 /* Floats_offset is the offset from the "virtual" frame. In an APCS
9347 frame that is $fp + 4 for a non-variadic function. */
9348 int floats_offset = 0;
9349 rtx operands[3];
9350 FILE * f = asm_out_file;
9351 rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
9352 unsigned int lrm_count = 0;
9353 int really_return = (sibling == NULL);
9354 int start_reg;
9355 arm_stack_offsets *offsets;
9357 /* If we have already generated the return instruction
9358 then it is futile to generate anything else. */
9359 if (use_return_insn (FALSE, sibling) && return_used_this_function)
9360 return "";
9362 func_type = arm_current_func_type ();
9364 if (IS_NAKED (func_type))
9365 /* Naked functions don't have epilogues. */
9366 return "";
9368 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9370 rtx op;
9372 /* A volatile function should never return. Call abort. */
9373 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9374 assemble_external_libcall (op);
9375 output_asm_insn ("bl\t%a0", &op);
9377 return "";
9380 if (ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
9381 && ! really_return)
9382 /* If we are throwing an exception, then we really must
9383 be doing a return, so we can't tail-call. */
9384 abort ();
9386 offsets = arm_get_frame_offsets ();
9387 saved_regs_mask = arm_compute_save_reg_mask ();
9389 if (TARGET_IWMMXT)
9390 lrm_count = bit_count (saved_regs_mask);
9392 floats_offset = offsets->saved_args;
9393 /* Compute how far away the floats will be. */
9394 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9395 if (saved_regs_mask & (1 << reg))
9396 floats_offset += 4;
9398 if (frame_pointer_needed)
9400 /* This variable is for the Virtual Frame Pointer, not VFP regs. */
9401 int vfp_offset = offsets->frame;
9403 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9405 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9406 if (regs_ever_live[reg] && !call_used_regs[reg])
9408 floats_offset += 12;
9409 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9410 reg, FP_REGNUM, floats_offset - vfp_offset);
9413 else
9415 start_reg = LAST_FPA_REGNUM;
9417 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9419 if (regs_ever_live[reg] && !call_used_regs[reg])
9421 floats_offset += 12;
9423 /* We can't unstack more than four registers at once. */
9424 if (start_reg - reg == 3)
9426 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9427 reg, FP_REGNUM, floats_offset - vfp_offset);
9428 start_reg = reg - 1;
9431 else
9433 if (reg != start_reg)
9434 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9435 reg + 1, start_reg - reg,
9436 FP_REGNUM, floats_offset - vfp_offset);
9437 start_reg = reg - 1;
9441 /* Just in case the last register checked also needs unstacking. */
9442 if (reg != start_reg)
9443 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9444 reg + 1, start_reg - reg,
9445 FP_REGNUM, floats_offset - vfp_offset);
9448 if (TARGET_HARD_FLOAT && TARGET_VFP)
9450 int saved_size;
9452 /* The fldmx insn does not have base+offset addressing modes,
9453 so we use IP to hold the address. */
9454 saved_size = arm_get_vfp_saved_size ();
9456 if (saved_size > 0)
9458 floats_offset += saved_size;
9459 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9460 FP_REGNUM, floats_offset - vfp_offset);
9462 start_reg = FIRST_VFP_REGNUM;
9463 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9465 if ((!regs_ever_live[reg] || call_used_regs[reg])
9466 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9468 if (start_reg != reg)
9469 arm_output_fldmx (f, IP_REGNUM,
9470 (start_reg - FIRST_VFP_REGNUM) / 2,
9471 (reg - start_reg) / 2);
9472 start_reg = reg + 2;
9475 if (start_reg != reg)
9476 arm_output_fldmx (f, IP_REGNUM,
9477 (start_reg - FIRST_VFP_REGNUM) / 2,
9478 (reg - start_reg) / 2);
9481 if (TARGET_IWMMXT)
9483 /* The frame pointer is guaranteed to be non-double-word aligned.
9484 This is because it is set to (old_stack_pointer - 4) and the
9485 old_stack_pointer was double word aligned. Thus the offset to
9486 the iWMMXt registers to be loaded must also be non-double-word
9487 sized, so that the resultant address *is* double-word aligned.
9488 We can ignore floats_offset since that was already included in
9489 the live_regs_mask. */
9490 lrm_count += (lrm_count % 2 ? 2 : 1);
9492 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9493 if (regs_ever_live[reg] && !call_used_regs[reg])
9495 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9496 reg, FP_REGNUM, lrm_count * 4);
9497 lrm_count += 2;
9501 /* saved_regs_mask should contain the IP, which at the time of stack
9502 frame generation actually contains the old stack pointer. So a
9503 quick way to unwind the stack is just pop the IP register directly
9504 into the stack pointer. */
9505 if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
9506 abort ();
9507 saved_regs_mask &= ~ (1 << IP_REGNUM);
9508 saved_regs_mask |= (1 << SP_REGNUM);
9510 /* There are two registers left in saved_regs_mask - LR and PC. We
9511 only need to restore the LR register (the return address), but to
9512 save time we can load it directly into the PC, unless we need a
9513 special function exit sequence, or we are not really returning. */
9514 if (really_return && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
9515 /* Delete the LR from the register mask, so that the LR on
9516 the stack is loaded into the PC in the register mask. */
9517 saved_regs_mask &= ~ (1 << LR_REGNUM);
9518 else
9519 saved_regs_mask &= ~ (1 << PC_REGNUM);
9521 /* We must use SP as the base register, because SP is one of the
9522 registers being restored. If an interrupt or page fault
9523 happens in the ldm instruction, the SP might or might not
9524 have been restored. That would be bad, as then SP will no
9525 longer indicate the safe area of stack, and we can get stack
9526 corruption. Using SP as the base register means that it will
9527 be reset correctly to the original value, should an interrupt
9528 occur. If the stack pointer already points at the right
9529 place, then omit the subtraction. */
9530 if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9531 || current_function_calls_alloca)
9532 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9533 4 * bit_count (saved_regs_mask));
9534 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9536 if (IS_INTERRUPT (func_type))
9537 /* Interrupt handlers will have pushed the
9538 IP onto the stack, so restore it now. */
9539 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9541 else
9543 /* Restore stack pointer if necessary. */
9544 if (offsets->outgoing_args != offsets->saved_regs)
9546 operands[0] = operands[1] = stack_pointer_rtx;
9547 operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9548 output_add_immediate (operands);
9551 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9553 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9554 if (regs_ever_live[reg] && !call_used_regs[reg])
9555 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9556 reg, SP_REGNUM);
9558 else
9560 start_reg = FIRST_FPA_REGNUM;
9562 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9564 if (regs_ever_live[reg] && !call_used_regs[reg])
9566 if (reg - start_reg == 3)
9568 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9569 start_reg, SP_REGNUM);
9570 start_reg = reg + 1;
9573 else
9575 if (reg != start_reg)
9576 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9577 start_reg, reg - start_reg,
9578 SP_REGNUM);
9580 start_reg = reg + 1;
9584 /* Just in case the last register checked also needs unstacking. */
9585 if (reg != start_reg)
9586 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9587 start_reg, reg - start_reg, SP_REGNUM);
9590 if (TARGET_HARD_FLOAT && TARGET_VFP)
9592 start_reg = FIRST_VFP_REGNUM;
9593 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9595 if ((!regs_ever_live[reg] || call_used_regs[reg])
9596 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9598 if (start_reg != reg)
9599 arm_output_fldmx (f, SP_REGNUM,
9600 (start_reg - FIRST_VFP_REGNUM) / 2,
9601 (reg - start_reg) / 2);
9602 start_reg = reg + 2;
9605 if (start_reg != reg)
9606 arm_output_fldmx (f, SP_REGNUM,
9607 (start_reg - FIRST_VFP_REGNUM) / 2,
9608 (reg - start_reg) / 2);
9610 if (TARGET_IWMMXT)
9611 for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9612 if (regs_ever_live[reg] && !call_used_regs[reg])
9613 asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9615 /* If we can, restore the LR into the PC. */
9616 if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9617 && really_return
9618 && current_function_pretend_args_size == 0
9619 && saved_regs_mask & (1 << LR_REGNUM))
9621 saved_regs_mask &= ~ (1 << LR_REGNUM);
9622 saved_regs_mask |= (1 << PC_REGNUM);
9625 /* Load the registers off the stack. If we only have one register
9626 to load use the LDR instruction - it is faster. */
9627 if (saved_regs_mask == (1 << LR_REGNUM))
9629 /* The exception handler ignores the LR, so we do
9630 not really need to load it off the stack. */
9631 if (eh_ofs)
9632 asm_fprintf (f, "\tadd\t%r, %r, #4\n", SP_REGNUM, SP_REGNUM);
9633 else
9634 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9636 else if (saved_regs_mask)
9638 if (saved_regs_mask & (1 << SP_REGNUM))
9639 /* Note - write back to the stack register is not enabled
9640 (ie "ldmfd sp!..."). We know that the stack pointer is
9641 in the list of registers and if we add writeback the
9642 instruction becomes UNPREDICTABLE. */
9643 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9644 else
9645 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9648 if (current_function_pretend_args_size)
9650 /* Unwind the pre-pushed regs. */
9651 operands[0] = operands[1] = stack_pointer_rtx;
9652 operands[2] = GEN_INT (current_function_pretend_args_size);
9653 output_add_immediate (operands);
9657 if (! really_return
9658 || (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9659 && current_function_pretend_args_size == 0
9660 && saved_regs_mask & (1 << PC_REGNUM)))
9661 return "";
9663 /* Generate the return instruction. */
9664 switch ((int) ARM_FUNC_TYPE (func_type))
9666 case ARM_FT_EXCEPTION_HANDLER:
9667 /* Even in 26-bit mode we do a mov (rather than a movs)
9668 because we don't have the PSR bits set in the address. */
9669 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, EXCEPTION_LR_REGNUM);
9670 break;
9672 case ARM_FT_ISR:
9673 case ARM_FT_FIQ:
9674 asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9675 break;
9677 case ARM_FT_EXCEPTION:
9678 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9679 break;
9681 case ARM_FT_INTERWORKED:
9682 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9683 break;
9685 default:
9686 if (frame_pointer_needed)
9687 /* If we used the frame pointer then the return address
9688 will have been loaded off the stack directly into the
9689 PC, so there is no need to issue a MOV instruction
9690 here. */
9692 else if (current_function_pretend_args_size == 0
9693 && (saved_regs_mask & (1 << LR_REGNUM)))
9694 /* Similarly we may have been able to load LR into the PC
9695 even if we did not create a stack frame. */
9697 else if (TARGET_APCS_32)
9698 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9699 else
9700 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9701 break;
9704 return "";
9707 static void
9708 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9709 HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9711 arm_stack_offsets *offsets;
9713 if (TARGET_THUMB)
9715 /* ??? Probably not safe to set this here, since it assumes that a
9716 function will be emitted as assembly immediately after we generate
9717 RTL for it. This does not happen for inline functions. */
9718 return_used_this_function = 0;
9720 else
9722 /* We need to take into account any stack-frame rounding. */
9723 offsets = arm_get_frame_offsets ();
9725 if (use_return_insn (FALSE, NULL)
9726 && return_used_this_function
9727 && offsets->saved_regs != offsets->outgoing_args
9728 && !frame_pointer_needed)
9729 abort ();
9731 /* Reset the ARM-specific per-function variables. */
9732 after_arm_reorg = 0;
9736 /* Generate and emit an insn that we will recognize as a push_multi.
9737 Unfortunately, since this insn does not reflect very well the actual
9738 semantics of the operation, we need to annotate the insn for the benefit
9739 of DWARF2 frame unwind information. */
9740 static rtx
9741 emit_multi_reg_push (int mask)
9743 int num_regs = 0;
9744 int num_dwarf_regs;
9745 int i, j;
9746 rtx par;
9747 rtx dwarf;
9748 int dwarf_par_index;
9749 rtx tmp, reg;
9751 for (i = 0; i <= LAST_ARM_REGNUM; i++)
9752 if (mask & (1 << i))
9753 num_regs++;
9755 if (num_regs == 0 || num_regs > 16)
9756 abort ();
9758 /* We don't record the PC in the dwarf frame information. */
9759 num_dwarf_regs = num_regs;
9760 if (mask & (1 << PC_REGNUM))
9761 num_dwarf_regs--;
9763 /* For the body of the insn we are going to generate an UNSPEC in
9764 parallel with several USEs. This allows the insn to be recognized
9765 by the push_multi pattern in the arm.md file. The insn looks
9766 something like this:
9768 (parallel [
9769 (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9770 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9771 (use (reg:SI 11 fp))
9772 (use (reg:SI 12 ip))
9773 (use (reg:SI 14 lr))
9774 (use (reg:SI 15 pc))
9777 For the frame note however, we try to be more explicit and actually
9778 show each register being stored into the stack frame, plus a (single)
9779 decrement of the stack pointer. We do it this way in order to be
9780 friendly to the stack unwinding code, which only wants to see a single
9781 stack decrement per instruction. The RTL we generate for the note looks
9782 something like this:
9784 (sequence [
9785 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9786 (set (mem:SI (reg:SI sp)) (reg:SI r4))
9787 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9788 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9789 (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9792 This sequence is used both by the code to support stack unwinding for
9793 exceptions handlers and the code to generate dwarf2 frame debugging. */
9795 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9796 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9797 dwarf_par_index = 1;
9799 for (i = 0; i <= LAST_ARM_REGNUM; i++)
9801 if (mask & (1 << i))
9803 reg = gen_rtx_REG (SImode, i);
9805 XVECEXP (par, 0, 0)
9806 = gen_rtx_SET (VOIDmode,
9807 gen_rtx_MEM (BLKmode,
9808 gen_rtx_PRE_DEC (BLKmode,
9809 stack_pointer_rtx)),
9810 gen_rtx_UNSPEC (BLKmode,
9811 gen_rtvec (1, reg),
9812 UNSPEC_PUSH_MULT));
9814 if (i != PC_REGNUM)
9816 tmp = gen_rtx_SET (VOIDmode,
9817 gen_rtx_MEM (SImode, stack_pointer_rtx),
9818 reg);
9819 RTX_FRAME_RELATED_P (tmp) = 1;
9820 XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9821 dwarf_par_index++;
9824 break;
9828 for (j = 1, i++; j < num_regs; i++)
9830 if (mask & (1 << i))
9832 reg = gen_rtx_REG (SImode, i);
9834 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9836 if (i != PC_REGNUM)
9838 tmp = gen_rtx_SET (VOIDmode,
9839 gen_rtx_MEM (SImode,
9840 plus_constant (stack_pointer_rtx,
9841 4 * j)),
9842 reg);
9843 RTX_FRAME_RELATED_P (tmp) = 1;
9844 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9847 j++;
9851 par = emit_insn (par);
9853 tmp = gen_rtx_SET (SImode,
9854 stack_pointer_rtx,
9855 gen_rtx_PLUS (SImode,
9856 stack_pointer_rtx,
9857 GEN_INT (-4 * num_regs)));
9858 RTX_FRAME_RELATED_P (tmp) = 1;
9859 XVECEXP (dwarf, 0, 0) = tmp;
9861 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9862 REG_NOTES (par));
9863 return par;
9866 static rtx
9867 emit_sfm (int base_reg, int count)
9869 rtx par;
9870 rtx dwarf;
9871 rtx tmp, reg;
9872 int i;
9874 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9875 dwarf = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9877 reg = gen_rtx_REG (XFmode, base_reg++);
9879 XVECEXP (par, 0, 0)
9880 = gen_rtx_SET (VOIDmode,
9881 gen_rtx_MEM (BLKmode,
9882 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9883 gen_rtx_UNSPEC (BLKmode,
9884 gen_rtvec (1, reg),
9885 UNSPEC_PUSH_MULT));
9887 = gen_rtx_SET (VOIDmode,
9888 gen_rtx_MEM (XFmode,
9889 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9890 reg);
9891 RTX_FRAME_RELATED_P (tmp) = 1;
9892 XVECEXP (dwarf, 0, count - 1) = tmp;
9894 for (i = 1; i < count; i++)
9896 reg = gen_rtx_REG (XFmode, base_reg++);
9897 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9899 tmp = gen_rtx_SET (VOIDmode,
9900 gen_rtx_MEM (XFmode,
9901 gen_rtx_PRE_DEC (BLKmode,
9902 stack_pointer_rtx)),
9903 reg);
9904 RTX_FRAME_RELATED_P (tmp) = 1;
9905 XVECEXP (dwarf, 0, count - i - 1) = tmp;
9908 par = emit_insn (par);
9909 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9910 REG_NOTES (par));
9911 return par;
9915 /* Compute the distance from register FROM to register TO.
9916 These can be the arg pointer (26), the soft frame pointer (25),
9917 the stack pointer (13) or the hard frame pointer (11).
9918 Typical stack layout looks like this:
9920 old stack pointer -> | |
9921 ----
9922 | | \
9923 | | saved arguments for
9924 | | vararg functions
9925 | | /
9927 hard FP & arg pointer -> | | \
9928 | | stack
9929 | | frame
9930 | | /
9932 | | \
9933 | | call saved
9934 | | registers
9935 soft frame pointer -> | | /
9937 | | \
9938 | | local
9939 | | variables
9940 | | /
9942 | | \
9943 | | outgoing
9944 | | arguments
9945 current stack pointer -> | | /
9948 For a given function some or all of these stack components
9949 may not be needed, giving rise to the possibility of
9950 eliminating some of the registers.
9952 The values returned by this function must reflect the behavior
9953 of arm_expand_prologue() and arm_compute_save_reg_mask().
9955 The sign of the number returned reflects the direction of stack
9956 growth, so the values are positive for all eliminations except
9957 from the soft frame pointer to the hard frame pointer.
9959 SFP may point just inside the local variables block to ensure correct
9960 alignment. */
9963 /* Calculate stack offsets. These are used to calculate register elimination
9964 offsets and in prologue/epilogue code. */
9966 static arm_stack_offsets *
9967 arm_get_frame_offsets (void)
9969 struct arm_stack_offsets *offsets;
9970 unsigned long func_type;
9971 int leaf;
9972 int saved;
9973 HOST_WIDE_INT frame_size;
9975 offsets = &cfun->machine->stack_offsets;
9977 /* We need to know if we are a leaf function. Unfortunately, it
9978 is possible to be called after start_sequence has been called,
9979 which causes get_insns to return the insns for the sequence,
9980 not the function, which will cause leaf_function_p to return
9981 the incorrect result.
9983 to know about leaf functions once reload has completed, and the
9984 frame size cannot be changed after that time, so we can safely
9985 use the cached value. */
9987 if (reload_completed)
9988 return offsets;
9990 /* Initially this is the size of the local variables. It will translated
9991 into an offset once we have determined the size of preceding data. */
9992 frame_size = ROUND_UP_WORD (get_frame_size ());
9994 leaf = leaf_function_p ();
9996 /* Space for variadic functions. */
9997 offsets->saved_args = current_function_pretend_args_size;
9999 offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
10001 if (TARGET_ARM)
10003 unsigned int regno;
10005 saved = bit_count (arm_compute_save_reg_mask ()) * 4;
10007 /* We know that SP will be doubleword aligned on entry, and we must
10008 preserve that condition at any subroutine call. We also require the
10009 soft frame pointer to be doubleword aligned. */
10011 if (TARGET_REALLY_IWMMXT)
10013 /* Check for the call-saved iWMMXt registers. */
10014 for (regno = FIRST_IWMMXT_REGNUM;
10015 regno <= LAST_IWMMXT_REGNUM;
10016 regno++)
10017 if (regs_ever_live [regno] && ! call_used_regs [regno])
10018 saved += 8;
10021 func_type = arm_current_func_type ();
10022 if (! IS_VOLATILE (func_type))
10024 /* Space for saved FPA registers. */
10025 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
10026 if (regs_ever_live[regno] && ! call_used_regs[regno])
10027 saved += 12;
10029 /* Space for saved VFP registers. */
10030 if (TARGET_HARD_FLOAT && TARGET_VFP)
10031 saved += arm_get_vfp_saved_size ();
10034 else /* TARGET_THUMB */
10036 int reg;
10037 int count_regs;
10039 saved = 0;
10040 count_regs = 0;
10041 for (reg = 8; reg < 13; reg ++)
10042 if (THUMB_REG_PUSHED_P (reg))
10043 count_regs ++;
10044 if (count_regs)
10045 saved += 4 * count_regs;
10046 count_regs = 0;
10047 for (reg = 0; reg <= LAST_LO_REGNUM; reg ++)
10048 if (THUMB_REG_PUSHED_P (reg))
10049 count_regs ++;
10050 if (count_regs || ! leaf_function_p ()
10051 || thumb_far_jump_used_p ())
10052 saved += 4 * (count_regs + 1);
10053 if (TARGET_BACKTRACE)
10055 if ((count_regs & 0xFF) == 0 && (regs_ever_live[3] != 0))
10056 saved += 20;
10057 else
10058 saved += 16;
10062 /* Saved registers include the stack frame. */
10063 offsets->saved_regs = offsets->saved_args + saved;
10064 offsets->soft_frame = offsets->saved_regs;
10065 /* A leaf function does not need any stack alignment if it has nothing
10066 on the stack. */
10067 if (leaf && frame_size == 0)
10069 offsets->outgoing_args = offsets->soft_frame;
10070 return offsets;
10073 /* Ensure SFP has the correct alignment. */
10074 if (ARM_DOUBLEWORD_ALIGN
10075 && (offsets->soft_frame & 7))
10076 offsets->soft_frame += 4;
10078 offsets->outgoing_args = offsets->soft_frame + frame_size
10079 + current_function_outgoing_args_size;
10081 if (ARM_DOUBLEWORD_ALIGN)
10083 /* Ensure SP remains doubleword aligned. */
10084 if (offsets->outgoing_args & 7)
10085 offsets->outgoing_args += 4;
10086 if (offsets->outgoing_args & 7)
10087 abort ();
10090 return offsets;
10094 /* Calculate the relative offsets for the different stack pointers. Positive
10095 offsets are in the direction of stack growth. */
10097 unsigned int
10098 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10100 arm_stack_offsets *offsets;
10102 offsets = arm_get_frame_offsets ();
10104 /* OK, now we have enough information to compute the distances.
10105 There must be an entry in these switch tables for each pair
10106 of registers in ELIMINABLE_REGS, even if some of the entries
10107 seem to be redundant or useless. */
10108 switch (from)
10110 case ARG_POINTER_REGNUM:
10111 switch (to)
10113 case THUMB_HARD_FRAME_POINTER_REGNUM:
10114 return 0;
10116 case FRAME_POINTER_REGNUM:
10117 /* This is the reverse of the soft frame pointer
10118 to hard frame pointer elimination below. */
10119 return offsets->soft_frame - offsets->saved_args;
10121 case ARM_HARD_FRAME_POINTER_REGNUM:
10122 /* If there is no stack frame then the hard
10123 frame pointer and the arg pointer coincide. */
10124 if (offsets->frame == offsets->saved_regs)
10125 return 0;
10126 /* FIXME: Not sure about this. Maybe we should always return 0 ? */
10127 return (frame_pointer_needed
10128 && current_function_needs_context
10129 && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
10131 case STACK_POINTER_REGNUM:
10132 /* If nothing has been pushed on the stack at all
10133 then this will return -4. This *is* correct! */
10134 return offsets->outgoing_args - (offsets->saved_args + 4);
10136 default:
10137 abort ();
10139 break;
10141 case FRAME_POINTER_REGNUM:
10142 switch (to)
10144 case THUMB_HARD_FRAME_POINTER_REGNUM:
10145 return 0;
10147 case ARM_HARD_FRAME_POINTER_REGNUM:
10148 /* The hard frame pointer points to the top entry in the
10149 stack frame. The soft frame pointer to the bottom entry
10150 in the stack frame. If there is no stack frame at all,
10151 then they are identical. */
10153 return offsets->frame - offsets->soft_frame;
10155 case STACK_POINTER_REGNUM:
10156 return offsets->outgoing_args - offsets->soft_frame;
10158 default:
10159 abort ();
10161 break;
10163 default:
10164 /* You cannot eliminate from the stack pointer.
10165 In theory you could eliminate from the hard frame
10166 pointer to the stack pointer, but this will never
10167 happen, since if a stack frame is not needed the
10168 hard frame pointer will never be used. */
10169 abort ();
10174 /* Generate the prologue instructions for entry into an ARM function. */
10175 void
10176 arm_expand_prologue (void)
10178 int reg;
10179 rtx amount;
10180 rtx insn;
10181 rtx ip_rtx;
10182 unsigned long live_regs_mask;
10183 unsigned long func_type;
10184 int fp_offset = 0;
10185 int saved_pretend_args = 0;
10186 int saved_regs = 0;
10187 unsigned int args_to_push;
10188 arm_stack_offsets *offsets;
10190 func_type = arm_current_func_type ();
10192 /* Naked functions don't have prologues. */
10193 if (IS_NAKED (func_type))
10194 return;
10196 /* Make a copy of c_f_p_a_s as we may need to modify it locally. */
10197 args_to_push = current_function_pretend_args_size;
10199 /* Compute which register we will have to save onto the stack. */
10200 live_regs_mask = arm_compute_save_reg_mask ();
10202 ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10204 if (frame_pointer_needed)
10206 if (IS_INTERRUPT (func_type))
10208 /* Interrupt functions must not corrupt any registers.
10209 Creating a frame pointer however, corrupts the IP
10210 register, so we must push it first. */
10211 insn = emit_multi_reg_push (1 << IP_REGNUM);
10213 /* Do not set RTX_FRAME_RELATED_P on this insn.
10214 The dwarf stack unwinding code only wants to see one
10215 stack decrement per function, and this is not it. If
10216 this instruction is labeled as being part of the frame
10217 creation sequence then dwarf2out_frame_debug_expr will
10218 abort when it encounters the assignment of IP to FP
10219 later on, since the use of SP here establishes SP as
10220 the CFA register and not IP.
10222 Anyway this instruction is not really part of the stack
10223 frame creation although it is part of the prologue. */
10225 else if (IS_NESTED (func_type))
10227 /* The Static chain register is the same as the IP register
10228 used as a scratch register during stack frame creation.
10229 To get around this need to find somewhere to store IP
10230 whilst the frame is being created. We try the following
10231 places in order:
10233 1. The last argument register.
10234 2. A slot on the stack above the frame. (This only
10235 works if the function is not a varargs function).
10236 3. Register r3, after pushing the argument registers
10237 onto the stack.
10239 Note - we only need to tell the dwarf2 backend about the SP
10240 adjustment in the second variant; the static chain register
10241 doesn't need to be unwound, as it doesn't contain a value
10242 inherited from the caller. */
10244 if (regs_ever_live[3] == 0)
10246 insn = gen_rtx_REG (SImode, 3);
10247 insn = gen_rtx_SET (SImode, insn, ip_rtx);
10248 insn = emit_insn (insn);
10250 else if (args_to_push == 0)
10252 rtx dwarf;
10253 insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10254 insn = gen_rtx_MEM (SImode, insn);
10255 insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10256 insn = emit_insn (insn);
10258 fp_offset = 4;
10260 /* Just tell the dwarf backend that we adjusted SP. */
10261 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10262 gen_rtx_PLUS (SImode, stack_pointer_rtx,
10263 GEN_INT (-fp_offset)));
10264 RTX_FRAME_RELATED_P (insn) = 1;
10265 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10266 dwarf, REG_NOTES (insn));
10268 else
10270 /* Store the args on the stack. */
10271 if (cfun->machine->uses_anonymous_args)
10272 insn = emit_multi_reg_push
10273 ((0xf0 >> (args_to_push / 4)) & 0xf);
10274 else
10275 insn = emit_insn
10276 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10277 GEN_INT (- args_to_push)));
10279 RTX_FRAME_RELATED_P (insn) = 1;
10281 saved_pretend_args = 1;
10282 fp_offset = args_to_push;
10283 args_to_push = 0;
10285 /* Now reuse r3 to preserve IP. */
10286 insn = gen_rtx_REG (SImode, 3);
10287 insn = gen_rtx_SET (SImode, insn, ip_rtx);
10288 (void) emit_insn (insn);
10292 if (fp_offset)
10294 insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10295 insn = gen_rtx_SET (SImode, ip_rtx, insn);
10297 else
10298 insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10300 insn = emit_insn (insn);
10301 RTX_FRAME_RELATED_P (insn) = 1;
10304 if (args_to_push)
10306 /* Push the argument registers, or reserve space for them. */
10307 if (cfun->machine->uses_anonymous_args)
10308 insn = emit_multi_reg_push
10309 ((0xf0 >> (args_to_push / 4)) & 0xf);
10310 else
10311 insn = emit_insn
10312 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10313 GEN_INT (- args_to_push)));
10314 RTX_FRAME_RELATED_P (insn) = 1;
10317 /* If this is an interrupt service routine, and the link register
10318 is going to be pushed, and we are not creating a stack frame,
10319 (which would involve an extra push of IP and a pop in the epilogue)
10320 subtracting four from LR now will mean that the function return
10321 can be done with a single instruction. */
10322 if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10323 && (live_regs_mask & (1 << LR_REGNUM)) != 0
10324 && ! frame_pointer_needed)
10325 emit_insn (gen_rtx_SET (SImode,
10326 gen_rtx_REG (SImode, LR_REGNUM),
10327 gen_rtx_PLUS (SImode,
10328 gen_rtx_REG (SImode, LR_REGNUM),
10329 GEN_INT (-4))));
10331 if (live_regs_mask)
10333 insn = emit_multi_reg_push (live_regs_mask);
10334 saved_regs += bit_count (live_regs_mask) * 4;
10335 RTX_FRAME_RELATED_P (insn) = 1;
10338 if (TARGET_IWMMXT)
10339 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10340 if (regs_ever_live[reg] && ! call_used_regs [reg])
10342 insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10343 insn = gen_rtx_MEM (V2SImode, insn);
10344 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10345 gen_rtx_REG (V2SImode, reg)));
10346 RTX_FRAME_RELATED_P (insn) = 1;
10347 saved_regs += 8;
10350 if (! IS_VOLATILE (func_type))
10352 int start_reg;
10354 /* Save any floating point call-saved registers used by this
10355 function. */
10356 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10358 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10359 if (regs_ever_live[reg] && !call_used_regs[reg])
10361 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10362 insn = gen_rtx_MEM (XFmode, insn);
10363 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10364 gen_rtx_REG (XFmode, reg)));
10365 RTX_FRAME_RELATED_P (insn) = 1;
10366 saved_regs += 12;
10369 else
10371 start_reg = LAST_FPA_REGNUM;
10373 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10375 if (regs_ever_live[reg] && !call_used_regs[reg])
10377 if (start_reg - reg == 3)
10379 insn = emit_sfm (reg, 4);
10380 RTX_FRAME_RELATED_P (insn) = 1;
10381 saved_regs += 48;
10382 start_reg = reg - 1;
10385 else
10387 if (start_reg != reg)
10389 insn = emit_sfm (reg + 1, start_reg - reg);
10390 RTX_FRAME_RELATED_P (insn) = 1;
10391 saved_regs += (start_reg - reg) * 12;
10393 start_reg = reg - 1;
10397 if (start_reg != reg)
10399 insn = emit_sfm (reg + 1, start_reg - reg);
10400 saved_regs += (start_reg - reg) * 12;
10401 RTX_FRAME_RELATED_P (insn) = 1;
10404 if (TARGET_HARD_FLOAT && TARGET_VFP)
10406 start_reg = FIRST_VFP_REGNUM;
10408 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10410 if ((!regs_ever_live[reg] || call_used_regs[reg])
10411 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10413 if (start_reg != reg)
10414 saved_regs += vfp_emit_fstmx (start_reg,
10415 (reg - start_reg) / 2);
10416 start_reg = reg + 2;
10419 if (start_reg != reg)
10420 saved_regs += vfp_emit_fstmx (start_reg,
10421 (reg - start_reg) / 2);
10425 if (frame_pointer_needed)
10427 /* Create the new frame pointer. */
10428 insn = GEN_INT (-(4 + args_to_push + fp_offset));
10429 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10430 RTX_FRAME_RELATED_P (insn) = 1;
10432 if (IS_NESTED (func_type))
10434 /* Recover the static chain register. */
10435 if (regs_ever_live [3] == 0
10436 || saved_pretend_args)
10437 insn = gen_rtx_REG (SImode, 3);
10438 else /* if (current_function_pretend_args_size == 0) */
10440 insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10441 GEN_INT (4));
10442 insn = gen_rtx_MEM (SImode, insn);
10445 emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10446 /* Add a USE to stop propagate_one_insn() from barfing. */
10447 emit_insn (gen_prologue_use (ip_rtx));
10451 offsets = arm_get_frame_offsets ();
10452 if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10454 /* This add can produce multiple insns for a large constant, so we
10455 need to get tricky. */
10456 rtx last = get_last_insn ();
10458 amount = GEN_INT (offsets->saved_args + saved_regs
10459 - offsets->outgoing_args);
10461 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10462 amount));
10465 last = last ? NEXT_INSN (last) : get_insns ();
10466 RTX_FRAME_RELATED_P (last) = 1;
10468 while (last != insn);
10470 /* If the frame pointer is needed, emit a special barrier that
10471 will prevent the scheduler from moving stores to the frame
10472 before the stack adjustment. */
10473 if (frame_pointer_needed)
10474 insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10475 hard_frame_pointer_rtx));
10478 /* If we are profiling, make sure no instructions are scheduled before
10479 the call to mcount. Similarly if the user has requested no
10480 scheduling in the prolog. */
10481 if (current_function_profile || TARGET_NO_SCHED_PRO)
10482 emit_insn (gen_blockage ());
10484 /* If the link register is being kept alive, with the return address in it,
10485 then make sure that it does not get reused by the ce2 pass. */
10486 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10488 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10489 cfun->machine->lr_save_eliminated = 1;
10493 /* If CODE is 'd', then the X is a condition operand and the instruction
10494 should only be executed if the condition is true.
10495 if CODE is 'D', then the X is a condition operand and the instruction
10496 should only be executed if the condition is false: however, if the mode
10497 of the comparison is CCFPEmode, then always execute the instruction -- we
10498 do this because in these circumstances !GE does not necessarily imply LT;
10499 in these cases the instruction pattern will take care to make sure that
10500 an instruction containing %d will follow, thereby undoing the effects of
10501 doing this instruction unconditionally.
10502 If CODE is 'N' then X is a floating point operand that must be negated
10503 before output.
10504 If CODE is 'B' then output a bitwise inverted value of X (a const int).
10505 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
10506 void
10507 arm_print_operand (FILE *stream, rtx x, int code)
10509 switch (code)
10511 case '@':
10512 fputs (ASM_COMMENT_START, stream);
10513 return;
10515 case '_':
10516 fputs (user_label_prefix, stream);
10517 return;
10519 case '|':
10520 fputs (REGISTER_PREFIX, stream);
10521 return;
10523 case '?':
10524 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10526 if (TARGET_THUMB || current_insn_predicate != NULL)
10527 abort ();
10529 fputs (arm_condition_codes[arm_current_cc], stream);
10531 else if (current_insn_predicate)
10533 enum arm_cond_code code;
10535 if (TARGET_THUMB)
10536 abort ();
10538 code = get_arm_condition_code (current_insn_predicate);
10539 fputs (arm_condition_codes[code], stream);
10541 return;
10543 case 'N':
10545 REAL_VALUE_TYPE r;
10546 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10547 r = REAL_VALUE_NEGATE (r);
10548 fprintf (stream, "%s", fp_const_from_val (&r));
10550 return;
10552 case 'B':
10553 if (GET_CODE (x) == CONST_INT)
10555 HOST_WIDE_INT val;
10556 val = ARM_SIGN_EXTEND (~INTVAL (x));
10557 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10559 else
10561 putc ('~', stream);
10562 output_addr_const (stream, x);
10564 return;
10566 case 'i':
10567 fprintf (stream, "%s", arithmetic_instr (x, 1));
10568 return;
10570 /* Truncate Cirrus shift counts. */
10571 case 's':
10572 if (GET_CODE (x) == CONST_INT)
10574 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10575 return;
10577 arm_print_operand (stream, x, 0);
10578 return;
10580 case 'I':
10581 fprintf (stream, "%s", arithmetic_instr (x, 0));
10582 return;
10584 case 'S':
10586 HOST_WIDE_INT val;
10587 const char * shift = shift_op (x, &val);
10589 if (shift)
10591 fprintf (stream, ", %s ", shift_op (x, &val));
10592 if (val == -1)
10593 arm_print_operand (stream, XEXP (x, 1), 0);
10594 else
10595 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10598 return;
10600 /* An explanation of the 'Q', 'R' and 'H' register operands:
10602 In a pair of registers containing a DI or DF value the 'Q'
10603 operand returns the register number of the register containing
10604 the least significant part of the value. The 'R' operand returns
10605 the register number of the register containing the most
10606 significant part of the value.
10608 The 'H' operand returns the higher of the two register numbers.
10609 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10610 same as the 'Q' operand, since the most significant part of the
10611 value is held in the lower number register. The reverse is true
10612 on systems where WORDS_BIG_ENDIAN is false.
10614 The purpose of these operands is to distinguish between cases
10615 where the endian-ness of the values is important (for example
10616 when they are added together), and cases where the endian-ness
10617 is irrelevant, but the order of register operations is important.
10618 For example when loading a value from memory into a register
10619 pair, the endian-ness does not matter. Provided that the value
10620 from the lower memory address is put into the lower numbered
10621 register, and the value from the higher address is put into the
10622 higher numbered register, the load will work regardless of whether
10623 the value being loaded is big-wordian or little-wordian. The
10624 order of the two register loads can matter however, if the address
10625 of the memory location is actually held in one of the registers
10626 being overwritten by the load. */
10627 case 'Q':
10628 if (REGNO (x) > LAST_ARM_REGNUM)
10629 abort ();
10630 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10631 return;
10633 case 'R':
10634 if (REGNO (x) > LAST_ARM_REGNUM)
10635 abort ();
10636 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10637 return;
10639 case 'H':
10640 if (REGNO (x) > LAST_ARM_REGNUM)
10641 abort ();
10642 asm_fprintf (stream, "%r", REGNO (x) + 1);
10643 return;
10645 case 'm':
10646 asm_fprintf (stream, "%r",
10647 GET_CODE (XEXP (x, 0)) == REG
10648 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10649 return;
10651 case 'M':
10652 asm_fprintf (stream, "{%r-%r}",
10653 REGNO (x),
10654 REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10655 return;
10657 case 'd':
10658 /* CONST_TRUE_RTX means always -- that's the default. */
10659 if (x == const_true_rtx)
10660 return;
10662 fputs (arm_condition_codes[get_arm_condition_code (x)],
10663 stream);
10664 return;
10666 case 'D':
10667 /* CONST_TRUE_RTX means not always -- ie never. We shouldn't ever
10668 want to do that. */
10669 if (x == const_true_rtx)
10670 abort ();
10672 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10673 (get_arm_condition_code (x))],
10674 stream);
10675 return;
10677 /* Cirrus registers can be accessed in a variety of ways:
10678 single floating point (f)
10679 double floating point (d)
10680 32bit integer (fx)
10681 64bit integer (dx). */
10682 case 'W': /* Cirrus register in F mode. */
10683 case 'X': /* Cirrus register in D mode. */
10684 case 'Y': /* Cirrus register in FX mode. */
10685 case 'Z': /* Cirrus register in DX mode. */
10686 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10687 abort ();
10689 fprintf (stream, "mv%s%s",
10690 code == 'W' ? "f"
10691 : code == 'X' ? "d"
10692 : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10694 return;
10696 /* Print cirrus register in the mode specified by the register's mode. */
10697 case 'V':
10699 int mode = GET_MODE (x);
10701 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10702 abort ();
10704 fprintf (stream, "mv%s%s",
10705 mode == DFmode ? "d"
10706 : mode == SImode ? "fx"
10707 : mode == DImode ? "dx"
10708 : "f", reg_names[REGNO (x)] + 2);
10710 return;
10713 case 'U':
10714 if (GET_CODE (x) != REG
10715 || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10716 || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10717 /* Bad value for wCG register number. */
10718 abort ();
10719 else
10720 fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10721 return;
10723 /* Print an iWMMXt control register name. */
10724 case 'w':
10725 if (GET_CODE (x) != CONST_INT
10726 || INTVAL (x) < 0
10727 || INTVAL (x) >= 16)
10728 /* Bad value for wC register number. */
10729 abort ();
10730 else
10732 static const char * wc_reg_names [16] =
10734 "wCID", "wCon", "wCSSF", "wCASF",
10735 "wC4", "wC5", "wC6", "wC7",
10736 "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10737 "wC12", "wC13", "wC14", "wC15"
10740 fprintf (stream, wc_reg_names [INTVAL (x)]);
10742 return;
10744 /* Print a VFP double precision register name. */
10745 case 'P':
10747 int mode = GET_MODE (x);
10748 int num;
10750 if (mode != DImode && mode != DFmode)
10751 abort ();
10753 if (GET_CODE (x) != REG
10754 || !IS_VFP_REGNUM (REGNO (x)))
10755 abort ();
10757 num = REGNO(x) - FIRST_VFP_REGNUM;
10758 if (num & 1)
10759 abort ();
10761 fprintf (stream, "d%d", num >> 1);
10763 return;
10765 default:
10766 if (x == 0)
10767 abort ();
10769 if (GET_CODE (x) == REG)
10770 asm_fprintf (stream, "%r", REGNO (x));
10771 else if (GET_CODE (x) == MEM)
10773 output_memory_reference_mode = GET_MODE (x);
10774 output_address (XEXP (x, 0));
10776 else if (GET_CODE (x) == CONST_DOUBLE)
10777 fprintf (stream, "#%s", fp_immediate_constant (x));
10778 else if (GET_CODE (x) == NEG)
10779 abort (); /* This should never happen now. */
10780 else
10782 fputc ('#', stream);
10783 output_addr_const (stream, x);
10788 #ifndef AOF_ASSEMBLER
10789 /* Target hook for assembling integer objects. The ARM version needs to
10790 handle word-sized values specially. */
10791 static bool
10792 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10794 if (size == UNITS_PER_WORD && aligned_p)
10796 fputs ("\t.word\t", asm_out_file);
10797 output_addr_const (asm_out_file, x);
10799 /* Mark symbols as position independent. We only do this in the
10800 .text segment, not in the .data segment. */
10801 if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10802 (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10804 if (GET_CODE (x) == SYMBOL_REF
10805 && (CONSTANT_POOL_ADDRESS_P (x)
10806 || SYMBOL_REF_LOCAL_P (x)))
10807 fputs ("(GOTOFF)", asm_out_file);
10808 else if (GET_CODE (x) == LABEL_REF)
10809 fputs ("(GOTOFF)", asm_out_file);
10810 else
10811 fputs ("(GOT)", asm_out_file);
10813 fputc ('\n', asm_out_file);
10814 return true;
10817 if (VECTOR_MODE_SUPPORTED_P (GET_MODE (x)))
10819 int i, units;
10821 if (GET_CODE (x) != CONST_VECTOR)
10822 abort ();
10824 units = CONST_VECTOR_NUNITS (x);
10826 switch (GET_MODE (x))
10828 case V2SImode: size = 4; break;
10829 case V4HImode: size = 2; break;
10830 case V8QImode: size = 1; break;
10831 default:
10832 abort ();
10835 for (i = 0; i < units; i++)
10837 rtx elt;
10839 elt = CONST_VECTOR_ELT (x, i);
10840 assemble_integer
10841 (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10844 return true;
10847 return default_assemble_integer (x, size, aligned_p);
10849 #endif
10851 /* A finite state machine takes care of noticing whether or not instructions
10852 can be conditionally executed, and thus decrease execution time and code
10853 size by deleting branch instructions. The fsm is controlled by
10854 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
10856 /* The state of the fsm controlling condition codes are:
10857 0: normal, do nothing special
10858 1: make ASM_OUTPUT_OPCODE not output this instruction
10859 2: make ASM_OUTPUT_OPCODE not output this instruction
10860 3: make instructions conditional
10861 4: make instructions conditional
10863 State transitions (state->state by whom under condition):
10864 0 -> 1 final_prescan_insn if the `target' is a label
10865 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10866 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10867 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10868 3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10869 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10870 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10871 (the target insn is arm_target_insn).
10873 If the jump clobbers the conditions then we use states 2 and 4.
10875 A similar thing can be done with conditional return insns.
10877 XXX In case the `target' is an unconditional branch, this conditionalising
10878 of the instructions always reduces code size, but not always execution
10879 time. But then, I want to reduce the code size to somewhere near what
10880 /bin/cc produces. */
10882 /* Returns the index of the ARM condition code string in
10883 `arm_condition_codes'. COMPARISON should be an rtx like
10884 `(eq (...) (...))'. */
10885 static enum arm_cond_code
10886 get_arm_condition_code (rtx comparison)
10888 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
10889 int code;
10890 enum rtx_code comp_code = GET_CODE (comparison);
10892 if (GET_MODE_CLASS (mode) != MODE_CC)
10893 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
10894 XEXP (comparison, 1));
10896 switch (mode)
10898 case CC_DNEmode: code = ARM_NE; goto dominance;
10899 case CC_DEQmode: code = ARM_EQ; goto dominance;
10900 case CC_DGEmode: code = ARM_GE; goto dominance;
10901 case CC_DGTmode: code = ARM_GT; goto dominance;
10902 case CC_DLEmode: code = ARM_LE; goto dominance;
10903 case CC_DLTmode: code = ARM_LT; goto dominance;
10904 case CC_DGEUmode: code = ARM_CS; goto dominance;
10905 case CC_DGTUmode: code = ARM_HI; goto dominance;
10906 case CC_DLEUmode: code = ARM_LS; goto dominance;
10907 case CC_DLTUmode: code = ARM_CC;
10909 dominance:
10910 if (comp_code != EQ && comp_code != NE)
10911 abort ();
10913 if (comp_code == EQ)
10914 return ARM_INVERSE_CONDITION_CODE (code);
10915 return code;
10917 case CC_NOOVmode:
10918 switch (comp_code)
10920 case NE: return ARM_NE;
10921 case EQ: return ARM_EQ;
10922 case GE: return ARM_PL;
10923 case LT: return ARM_MI;
10924 default: abort ();
10927 case CC_Zmode:
10928 switch (comp_code)
10930 case NE: return ARM_NE;
10931 case EQ: return ARM_EQ;
10932 default: abort ();
10935 case CC_Nmode:
10936 switch (comp_code)
10938 case NE: return ARM_MI;
10939 case EQ: return ARM_PL;
10940 default: abort ();
10943 case CCFPEmode:
10944 case CCFPmode:
10945 /* These encodings assume that AC=1 in the FPA system control
10946 byte. This allows us to handle all cases except UNEQ and
10947 LTGT. */
10948 switch (comp_code)
10950 case GE: return ARM_GE;
10951 case GT: return ARM_GT;
10952 case LE: return ARM_LS;
10953 case LT: return ARM_MI;
10954 case NE: return ARM_NE;
10955 case EQ: return ARM_EQ;
10956 case ORDERED: return ARM_VC;
10957 case UNORDERED: return ARM_VS;
10958 case UNLT: return ARM_LT;
10959 case UNLE: return ARM_LE;
10960 case UNGT: return ARM_HI;
10961 case UNGE: return ARM_PL;
10962 /* UNEQ and LTGT do not have a representation. */
10963 case UNEQ: /* Fall through. */
10964 case LTGT: /* Fall through. */
10965 default: abort ();
10968 case CC_SWPmode:
10969 switch (comp_code)
10971 case NE: return ARM_NE;
10972 case EQ: return ARM_EQ;
10973 case GE: return ARM_LE;
10974 case GT: return ARM_LT;
10975 case LE: return ARM_GE;
10976 case LT: return ARM_GT;
10977 case GEU: return ARM_LS;
10978 case GTU: return ARM_CC;
10979 case LEU: return ARM_CS;
10980 case LTU: return ARM_HI;
10981 default: abort ();
10984 case CC_Cmode:
10985 switch (comp_code)
10987 case LTU: return ARM_CS;
10988 case GEU: return ARM_CC;
10989 default: abort ();
10992 case CCmode:
10993 switch (comp_code)
10995 case NE: return ARM_NE;
10996 case EQ: return ARM_EQ;
10997 case GE: return ARM_GE;
10998 case GT: return ARM_GT;
10999 case LE: return ARM_LE;
11000 case LT: return ARM_LT;
11001 case GEU: return ARM_CS;
11002 case GTU: return ARM_HI;
11003 case LEU: return ARM_LS;
11004 case LTU: return ARM_CC;
11005 default: abort ();
11008 default: abort ();
11011 abort ();
11014 void
11015 arm_final_prescan_insn (rtx insn)
11017 /* BODY will hold the body of INSN. */
11018 rtx body = PATTERN (insn);
11020 /* This will be 1 if trying to repeat the trick, and things need to be
11021 reversed if it appears to fail. */
11022 int reverse = 0;
11024 /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11025 taken are clobbered, even if the rtl suggests otherwise. It also
11026 means that we have to grub around within the jump expression to find
11027 out what the conditions are when the jump isn't taken. */
11028 int jump_clobbers = 0;
11030 /* If we start with a return insn, we only succeed if we find another one. */
11031 int seeking_return = 0;
11033 /* START_INSN will hold the insn from where we start looking. This is the
11034 first insn after the following code_label if REVERSE is true. */
11035 rtx start_insn = insn;
11037 /* If in state 4, check if the target branch is reached, in order to
11038 change back to state 0. */
11039 if (arm_ccfsm_state == 4)
11041 if (insn == arm_target_insn)
11043 arm_target_insn = NULL;
11044 arm_ccfsm_state = 0;
11046 return;
11049 /* If in state 3, it is possible to repeat the trick, if this insn is an
11050 unconditional branch to a label, and immediately following this branch
11051 is the previous target label which is only used once, and the label this
11052 branch jumps to is not too far off. */
11053 if (arm_ccfsm_state == 3)
11055 if (simplejump_p (insn))
11057 start_insn = next_nonnote_insn (start_insn);
11058 if (GET_CODE (start_insn) == BARRIER)
11060 /* XXX Isn't this always a barrier? */
11061 start_insn = next_nonnote_insn (start_insn);
11063 if (GET_CODE (start_insn) == CODE_LABEL
11064 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11065 && LABEL_NUSES (start_insn) == 1)
11066 reverse = TRUE;
11067 else
11068 return;
11070 else if (GET_CODE (body) == RETURN)
11072 start_insn = next_nonnote_insn (start_insn);
11073 if (GET_CODE (start_insn) == BARRIER)
11074 start_insn = next_nonnote_insn (start_insn);
11075 if (GET_CODE (start_insn) == CODE_LABEL
11076 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11077 && LABEL_NUSES (start_insn) == 1)
11079 reverse = TRUE;
11080 seeking_return = 1;
11082 else
11083 return;
11085 else
11086 return;
11089 if (arm_ccfsm_state != 0 && !reverse)
11090 abort ();
11091 if (GET_CODE (insn) != JUMP_INSN)
11092 return;
11094 /* This jump might be paralleled with a clobber of the condition codes
11095 the jump should always come first */
11096 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11097 body = XVECEXP (body, 0, 0);
11099 if (reverse
11100 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11101 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11103 int insns_skipped;
11104 int fail = FALSE, succeed = FALSE;
11105 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
11106 int then_not_else = TRUE;
11107 rtx this_insn = start_insn, label = 0;
11109 /* If the jump cannot be done with one instruction, we cannot
11110 conditionally execute the instruction in the inverse case. */
11111 if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11113 jump_clobbers = 1;
11114 return;
11117 /* Register the insn jumped to. */
11118 if (reverse)
11120 if (!seeking_return)
11121 label = XEXP (SET_SRC (body), 0);
11123 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11124 label = XEXP (XEXP (SET_SRC (body), 1), 0);
11125 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11127 label = XEXP (XEXP (SET_SRC (body), 2), 0);
11128 then_not_else = FALSE;
11130 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11131 seeking_return = 1;
11132 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11134 seeking_return = 1;
11135 then_not_else = FALSE;
11137 else
11138 abort ();
11140 /* See how many insns this branch skips, and what kind of insns. If all
11141 insns are okay, and the label or unconditional branch to the same
11142 label is not too far away, succeed. */
11143 for (insns_skipped = 0;
11144 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11146 rtx scanbody;
11148 this_insn = next_nonnote_insn (this_insn);
11149 if (!this_insn)
11150 break;
11152 switch (GET_CODE (this_insn))
11154 case CODE_LABEL:
11155 /* Succeed if it is the target label, otherwise fail since
11156 control falls in from somewhere else. */
11157 if (this_insn == label)
11159 if (jump_clobbers)
11161 arm_ccfsm_state = 2;
11162 this_insn = next_nonnote_insn (this_insn);
11164 else
11165 arm_ccfsm_state = 1;
11166 succeed = TRUE;
11168 else
11169 fail = TRUE;
11170 break;
11172 case BARRIER:
11173 /* Succeed if the following insn is the target label.
11174 Otherwise fail.
11175 If return insns are used then the last insn in a function
11176 will be a barrier. */
11177 this_insn = next_nonnote_insn (this_insn);
11178 if (this_insn && this_insn == label)
11180 if (jump_clobbers)
11182 arm_ccfsm_state = 2;
11183 this_insn = next_nonnote_insn (this_insn);
11185 else
11186 arm_ccfsm_state = 1;
11187 succeed = TRUE;
11189 else
11190 fail = TRUE;
11191 break;
11193 case CALL_INSN:
11194 /* If using 32-bit addresses the cc is not preserved over
11195 calls. */
11196 if (TARGET_APCS_32)
11198 /* Succeed if the following insn is the target label,
11199 or if the following two insns are a barrier and
11200 the target label. */
11201 this_insn = next_nonnote_insn (this_insn);
11202 if (this_insn && GET_CODE (this_insn) == BARRIER)
11203 this_insn = next_nonnote_insn (this_insn);
11205 if (this_insn && this_insn == label
11206 && insns_skipped < max_insns_skipped)
11208 if (jump_clobbers)
11210 arm_ccfsm_state = 2;
11211 this_insn = next_nonnote_insn (this_insn);
11213 else
11214 arm_ccfsm_state = 1;
11215 succeed = TRUE;
11217 else
11218 fail = TRUE;
11220 break;
11222 case JUMP_INSN:
11223 /* If this is an unconditional branch to the same label, succeed.
11224 If it is to another label, do nothing. If it is conditional,
11225 fail. */
11226 /* XXX Probably, the tests for SET and the PC are
11227 unnecessary. */
11229 scanbody = PATTERN (this_insn);
11230 if (GET_CODE (scanbody) == SET
11231 && GET_CODE (SET_DEST (scanbody)) == PC)
11233 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11234 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11236 arm_ccfsm_state = 2;
11237 succeed = TRUE;
11239 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11240 fail = TRUE;
11242 /* Fail if a conditional return is undesirable (eg on a
11243 StrongARM), but still allow this if optimizing for size. */
11244 else if (GET_CODE (scanbody) == RETURN
11245 && !use_return_insn (TRUE, NULL)
11246 && !optimize_size)
11247 fail = TRUE;
11248 else if (GET_CODE (scanbody) == RETURN
11249 && seeking_return)
11251 arm_ccfsm_state = 2;
11252 succeed = TRUE;
11254 else if (GET_CODE (scanbody) == PARALLEL)
11256 switch (get_attr_conds (this_insn))
11258 case CONDS_NOCOND:
11259 break;
11260 default:
11261 fail = TRUE;
11262 break;
11265 else
11266 fail = TRUE; /* Unrecognized jump (eg epilogue). */
11268 break;
11270 case INSN:
11271 /* Instructions using or affecting the condition codes make it
11272 fail. */
11273 scanbody = PATTERN (this_insn);
11274 if (!(GET_CODE (scanbody) == SET
11275 || GET_CODE (scanbody) == PARALLEL)
11276 || get_attr_conds (this_insn) != CONDS_NOCOND)
11277 fail = TRUE;
11279 /* A conditional cirrus instruction must be followed by
11280 a non Cirrus instruction. However, since we
11281 conditionalize instructions in this function and by
11282 the time we get here we can't add instructions
11283 (nops), because shorten_branches() has already been
11284 called, we will disable conditionalizing Cirrus
11285 instructions to be safe. */
11286 if (GET_CODE (scanbody) != USE
11287 && GET_CODE (scanbody) != CLOBBER
11288 && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11289 fail = TRUE;
11290 break;
11292 default:
11293 break;
11296 if (succeed)
11298 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11299 arm_target_label = CODE_LABEL_NUMBER (label);
11300 else if (seeking_return || arm_ccfsm_state == 2)
11302 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11304 this_insn = next_nonnote_insn (this_insn);
11305 if (this_insn && (GET_CODE (this_insn) == BARRIER
11306 || GET_CODE (this_insn) == CODE_LABEL))
11307 abort ();
11309 if (!this_insn)
11311 /* Oh, dear! we ran off the end.. give up. */
11312 recog (PATTERN (insn), insn, NULL);
11313 arm_ccfsm_state = 0;
11314 arm_target_insn = NULL;
11315 return;
11317 arm_target_insn = this_insn;
11319 else
11320 abort ();
11321 if (jump_clobbers)
11323 if (reverse)
11324 abort ();
11325 arm_current_cc =
11326 get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11327 0), 0), 1));
11328 if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11329 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11330 if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11331 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11333 else
11335 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11336 what it was. */
11337 if (!reverse)
11338 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11339 0));
11342 if (reverse || then_not_else)
11343 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11346 /* Restore recog_data (getting the attributes of other insns can
11347 destroy this array, but final.c assumes that it remains intact
11348 across this call; since the insn has been recognized already we
11349 call recog direct). */
11350 recog (PATTERN (insn), insn, NULL);
11354 /* Returns true if REGNO is a valid register
11355 for holding a quantity of tyoe MODE. */
11357 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11359 if (GET_MODE_CLASS (mode) == MODE_CC)
11360 return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11362 if (TARGET_THUMB)
11363 /* For the Thumb we only allow values bigger than SImode in
11364 registers 0 - 6, so that there is always a second low
11365 register available to hold the upper part of the value.
11366 We probably we ought to ensure that the register is the
11367 start of an even numbered register pair. */
11368 return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11370 if (IS_CIRRUS_REGNUM (regno))
11371 /* We have outlawed SI values in Cirrus registers because they
11372 reside in the lower 32 bits, but SF values reside in the
11373 upper 32 bits. This causes gcc all sorts of grief. We can't
11374 even split the registers into pairs because Cirrus SI values
11375 get sign extended to 64bits-- aldyh. */
11376 return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11378 if (IS_VFP_REGNUM (regno))
11380 if (mode == SFmode || mode == SImode)
11381 return TRUE;
11383 /* DFmode values are only valid in even register pairs. */
11384 if (mode == DFmode)
11385 return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11386 return FALSE;
11389 if (IS_IWMMXT_GR_REGNUM (regno))
11390 return mode == SImode;
11392 if (IS_IWMMXT_REGNUM (regno))
11393 return VALID_IWMMXT_REG_MODE (mode);
11395 if (regno <= LAST_ARM_REGNUM)
11396 /* We allow any value to be stored in the general registers. */
11397 return 1;
11399 if ( regno == FRAME_POINTER_REGNUM
11400 || regno == ARG_POINTER_REGNUM)
11401 /* We only allow integers in the fake hard registers. */
11402 return GET_MODE_CLASS (mode) == MODE_INT;
11404 /* The only registers left are the FPA registers
11405 which we only allow to hold FP values. */
11406 return GET_MODE_CLASS (mode) == MODE_FLOAT
11407 && regno >= FIRST_FPA_REGNUM
11408 && regno <= LAST_FPA_REGNUM;
11412 arm_regno_class (int regno)
11414 if (TARGET_THUMB)
11416 if (regno == STACK_POINTER_REGNUM)
11417 return STACK_REG;
11418 if (regno == CC_REGNUM)
11419 return CC_REG;
11420 if (regno < 8)
11421 return LO_REGS;
11422 return HI_REGS;
11425 if ( regno <= LAST_ARM_REGNUM
11426 || regno == FRAME_POINTER_REGNUM
11427 || regno == ARG_POINTER_REGNUM)
11428 return GENERAL_REGS;
11430 if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11431 return NO_REGS;
11433 if (IS_CIRRUS_REGNUM (regno))
11434 return CIRRUS_REGS;
11436 if (IS_VFP_REGNUM (regno))
11437 return VFP_REGS;
11439 if (IS_IWMMXT_REGNUM (regno))
11440 return IWMMXT_REGS;
11442 if (IS_IWMMXT_GR_REGNUM (regno))
11443 return IWMMXT_GR_REGS;
11445 return FPA_REGS;
11448 /* Handle a special case when computing the offset
11449 of an argument from the frame pointer. */
11451 arm_debugger_arg_offset (int value, rtx addr)
11453 rtx insn;
11455 /* We are only interested if dbxout_parms() failed to compute the offset. */
11456 if (value != 0)
11457 return 0;
11459 /* We can only cope with the case where the address is held in a register. */
11460 if (GET_CODE (addr) != REG)
11461 return 0;
11463 /* If we are using the frame pointer to point at the argument, then
11464 an offset of 0 is correct. */
11465 if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11466 return 0;
11468 /* If we are using the stack pointer to point at the
11469 argument, then an offset of 0 is correct. */
11470 if ((TARGET_THUMB || !frame_pointer_needed)
11471 && REGNO (addr) == SP_REGNUM)
11472 return 0;
11474 /* Oh dear. The argument is pointed to by a register rather
11475 than being held in a register, or being stored at a known
11476 offset from the frame pointer. Since GDB only understands
11477 those two kinds of argument we must translate the address
11478 held in the register into an offset from the frame pointer.
11479 We do this by searching through the insns for the function
11480 looking to see where this register gets its value. If the
11481 register is initialized from the frame pointer plus an offset
11482 then we are in luck and we can continue, otherwise we give up.
11484 This code is exercised by producing debugging information
11485 for a function with arguments like this:
11487 double func (double a, double b, int c, double d) {return d;}
11489 Without this code the stab for parameter 'd' will be set to
11490 an offset of 0 from the frame pointer, rather than 8. */
11492 /* The if() statement says:
11494 If the insn is a normal instruction
11495 and if the insn is setting the value in a register
11496 and if the register being set is the register holding the address of the argument
11497 and if the address is computing by an addition
11498 that involves adding to a register
11499 which is the frame pointer
11500 a constant integer
11502 then... */
11504 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11506 if ( GET_CODE (insn) == INSN
11507 && GET_CODE (PATTERN (insn)) == SET
11508 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11509 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11510 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11511 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11512 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11515 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11517 break;
11521 if (value == 0)
11523 debug_rtx (addr);
11524 warning ("unable to compute real location of stacked parameter");
11525 value = 8; /* XXX magic hack */
11528 return value;
11531 #define def_mbuiltin(MASK, NAME, TYPE, CODE) \
11532 do \
11534 if ((MASK) & insn_flags) \
11535 builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD, NULL, NULL_TREE); \
11537 while (0)
11539 struct builtin_description
11541 const unsigned int mask;
11542 const enum insn_code icode;
11543 const char * const name;
11544 const enum arm_builtins code;
11545 const enum rtx_code comparison;
11546 const unsigned int flag;
11549 static const struct builtin_description bdesc_2arg[] =
11551 #define IWMMXT_BUILTIN(code, string, builtin) \
11552 { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11553 ARM_BUILTIN_##builtin, 0, 0 },
11555 IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11556 IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11557 IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11558 IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11559 IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11560 IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11561 IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11562 IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11563 IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11564 IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11565 IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11566 IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11567 IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11568 IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11569 IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11570 IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11571 IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11572 IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11573 IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11574 IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11575 IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11576 IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11577 IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11578 IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11579 IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11580 IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11581 IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11582 IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11583 IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11584 IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11585 IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11586 IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11587 IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11588 IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11589 IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11590 IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11591 IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11592 IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11593 IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11594 IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11595 IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11596 IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11597 IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11598 IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11599 IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11600 IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11601 IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11602 IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11603 IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11604 IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11605 IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11606 IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11607 IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11608 IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11609 IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11610 IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11611 IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11612 IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11614 #define IWMMXT_BUILTIN2(code, builtin) \
11615 { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11617 IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11618 IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11619 IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11620 IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11621 IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11622 IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11623 IWMMXT_BUILTIN2 (ashlv4hi3_di, WSLLH)
11624 IWMMXT_BUILTIN2 (ashlv4hi3, WSLLHI)
11625 IWMMXT_BUILTIN2 (ashlv2si3_di, WSLLW)
11626 IWMMXT_BUILTIN2 (ashlv2si3, WSLLWI)
11627 IWMMXT_BUILTIN2 (ashldi3_di, WSLLD)
11628 IWMMXT_BUILTIN2 (ashldi3_iwmmxt, WSLLDI)
11629 IWMMXT_BUILTIN2 (lshrv4hi3_di, WSRLH)
11630 IWMMXT_BUILTIN2 (lshrv4hi3, WSRLHI)
11631 IWMMXT_BUILTIN2 (lshrv2si3_di, WSRLW)
11632 IWMMXT_BUILTIN2 (lshrv2si3, WSRLWI)
11633 IWMMXT_BUILTIN2 (lshrdi3_di, WSRLD)
11634 IWMMXT_BUILTIN2 (lshrdi3_iwmmxt, WSRLDI)
11635 IWMMXT_BUILTIN2 (ashrv4hi3_di, WSRAH)
11636 IWMMXT_BUILTIN2 (ashrv4hi3, WSRAHI)
11637 IWMMXT_BUILTIN2 (ashrv2si3_di, WSRAW)
11638 IWMMXT_BUILTIN2 (ashrv2si3, WSRAWI)
11639 IWMMXT_BUILTIN2 (ashrdi3_di, WSRAD)
11640 IWMMXT_BUILTIN2 (ashrdi3_iwmmxt, WSRADI)
11641 IWMMXT_BUILTIN2 (rorv4hi3_di, WRORH)
11642 IWMMXT_BUILTIN2 (rorv4hi3, WRORHI)
11643 IWMMXT_BUILTIN2 (rorv2si3_di, WRORW)
11644 IWMMXT_BUILTIN2 (rorv2si3, WRORWI)
11645 IWMMXT_BUILTIN2 (rordi3_di, WRORD)
11646 IWMMXT_BUILTIN2 (rordi3, WRORDI)
11647 IWMMXT_BUILTIN2 (iwmmxt_wmacuz, WMACUZ)
11648 IWMMXT_BUILTIN2 (iwmmxt_wmacsz, WMACSZ)
11651 static const struct builtin_description bdesc_1arg[] =
11653 IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11654 IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11655 IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11656 IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11657 IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11658 IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11659 IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11660 IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11661 IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11662 IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11663 IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11664 IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11665 IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11666 IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11667 IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11668 IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11669 IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11670 IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11673 /* Set up all the iWMMXt builtins. This is
11674 not called if TARGET_IWMMXT is zero. */
11676 static void
11677 arm_init_iwmmxt_builtins (void)
11679 const struct builtin_description * d;
11680 size_t i;
11681 tree endlink = void_list_node;
11683 tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11684 tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11685 tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11687 tree int_ftype_int
11688 = build_function_type (integer_type_node,
11689 tree_cons (NULL_TREE, integer_type_node, endlink));
11690 tree v8qi_ftype_v8qi_v8qi_int
11691 = build_function_type (V8QI_type_node,
11692 tree_cons (NULL_TREE, V8QI_type_node,
11693 tree_cons (NULL_TREE, V8QI_type_node,
11694 tree_cons (NULL_TREE,
11695 integer_type_node,
11696 endlink))));
11697 tree v4hi_ftype_v4hi_int
11698 = build_function_type (V4HI_type_node,
11699 tree_cons (NULL_TREE, V4HI_type_node,
11700 tree_cons (NULL_TREE, integer_type_node,
11701 endlink)));
11702 tree v2si_ftype_v2si_int
11703 = build_function_type (V2SI_type_node,
11704 tree_cons (NULL_TREE, V2SI_type_node,
11705 tree_cons (NULL_TREE, integer_type_node,
11706 endlink)));
11707 tree v2si_ftype_di_di
11708 = build_function_type (V2SI_type_node,
11709 tree_cons (NULL_TREE, long_long_integer_type_node,
11710 tree_cons (NULL_TREE, long_long_integer_type_node,
11711 endlink)));
11712 tree di_ftype_di_int
11713 = build_function_type (long_long_integer_type_node,
11714 tree_cons (NULL_TREE, long_long_integer_type_node,
11715 tree_cons (NULL_TREE, integer_type_node,
11716 endlink)));
11717 tree di_ftype_di_int_int
11718 = build_function_type (long_long_integer_type_node,
11719 tree_cons (NULL_TREE, long_long_integer_type_node,
11720 tree_cons (NULL_TREE, integer_type_node,
11721 tree_cons (NULL_TREE,
11722 integer_type_node,
11723 endlink))));
11724 tree int_ftype_v8qi
11725 = build_function_type (integer_type_node,
11726 tree_cons (NULL_TREE, V8QI_type_node,
11727 endlink));
11728 tree int_ftype_v4hi
11729 = build_function_type (integer_type_node,
11730 tree_cons (NULL_TREE, V4HI_type_node,
11731 endlink));
11732 tree int_ftype_v2si
11733 = build_function_type (integer_type_node,
11734 tree_cons (NULL_TREE, V2SI_type_node,
11735 endlink));
11736 tree int_ftype_v8qi_int
11737 = build_function_type (integer_type_node,
11738 tree_cons (NULL_TREE, V8QI_type_node,
11739 tree_cons (NULL_TREE, integer_type_node,
11740 endlink)));
11741 tree int_ftype_v4hi_int
11742 = build_function_type (integer_type_node,
11743 tree_cons (NULL_TREE, V4HI_type_node,
11744 tree_cons (NULL_TREE, integer_type_node,
11745 endlink)));
11746 tree int_ftype_v2si_int
11747 = build_function_type (integer_type_node,
11748 tree_cons (NULL_TREE, V2SI_type_node,
11749 tree_cons (NULL_TREE, integer_type_node,
11750 endlink)));
11751 tree v8qi_ftype_v8qi_int_int
11752 = build_function_type (V8QI_type_node,
11753 tree_cons (NULL_TREE, V8QI_type_node,
11754 tree_cons (NULL_TREE, integer_type_node,
11755 tree_cons (NULL_TREE,
11756 integer_type_node,
11757 endlink))));
11758 tree v4hi_ftype_v4hi_int_int
11759 = build_function_type (V4HI_type_node,
11760 tree_cons (NULL_TREE, V4HI_type_node,
11761 tree_cons (NULL_TREE, integer_type_node,
11762 tree_cons (NULL_TREE,
11763 integer_type_node,
11764 endlink))));
11765 tree v2si_ftype_v2si_int_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 tree_cons (NULL_TREE,
11770 integer_type_node,
11771 endlink))));
11772 /* Miscellaneous. */
11773 tree v8qi_ftype_v4hi_v4hi
11774 = build_function_type (V8QI_type_node,
11775 tree_cons (NULL_TREE, V4HI_type_node,
11776 tree_cons (NULL_TREE, V4HI_type_node,
11777 endlink)));
11778 tree v4hi_ftype_v2si_v2si
11779 = build_function_type (V4HI_type_node,
11780 tree_cons (NULL_TREE, V2SI_type_node,
11781 tree_cons (NULL_TREE, V2SI_type_node,
11782 endlink)));
11783 tree v2si_ftype_v4hi_v4hi
11784 = build_function_type (V2SI_type_node,
11785 tree_cons (NULL_TREE, V4HI_type_node,
11786 tree_cons (NULL_TREE, V4HI_type_node,
11787 endlink)));
11788 tree v2si_ftype_v8qi_v8qi
11789 = build_function_type (V2SI_type_node,
11790 tree_cons (NULL_TREE, V8QI_type_node,
11791 tree_cons (NULL_TREE, V8QI_type_node,
11792 endlink)));
11793 tree v4hi_ftype_v4hi_di
11794 = build_function_type (V4HI_type_node,
11795 tree_cons (NULL_TREE, V4HI_type_node,
11796 tree_cons (NULL_TREE,
11797 long_long_integer_type_node,
11798 endlink)));
11799 tree v2si_ftype_v2si_di
11800 = build_function_type (V2SI_type_node,
11801 tree_cons (NULL_TREE, V2SI_type_node,
11802 tree_cons (NULL_TREE,
11803 long_long_integer_type_node,
11804 endlink)));
11805 tree void_ftype_int_int
11806 = build_function_type (void_type_node,
11807 tree_cons (NULL_TREE, integer_type_node,
11808 tree_cons (NULL_TREE, integer_type_node,
11809 endlink)));
11810 tree di_ftype_void
11811 = build_function_type (long_long_unsigned_type_node, endlink);
11812 tree di_ftype_v8qi
11813 = build_function_type (long_long_integer_type_node,
11814 tree_cons (NULL_TREE, V8QI_type_node,
11815 endlink));
11816 tree di_ftype_v4hi
11817 = build_function_type (long_long_integer_type_node,
11818 tree_cons (NULL_TREE, V4HI_type_node,
11819 endlink));
11820 tree di_ftype_v2si
11821 = build_function_type (long_long_integer_type_node,
11822 tree_cons (NULL_TREE, V2SI_type_node,
11823 endlink));
11824 tree v2si_ftype_v4hi
11825 = build_function_type (V2SI_type_node,
11826 tree_cons (NULL_TREE, V4HI_type_node,
11827 endlink));
11828 tree v4hi_ftype_v8qi
11829 = build_function_type (V4HI_type_node,
11830 tree_cons (NULL_TREE, V8QI_type_node,
11831 endlink));
11833 tree di_ftype_di_v4hi_v4hi
11834 = build_function_type (long_long_unsigned_type_node,
11835 tree_cons (NULL_TREE,
11836 long_long_unsigned_type_node,
11837 tree_cons (NULL_TREE, V4HI_type_node,
11838 tree_cons (NULL_TREE,
11839 V4HI_type_node,
11840 endlink))));
11842 tree di_ftype_v4hi_v4hi
11843 = build_function_type (long_long_unsigned_type_node,
11844 tree_cons (NULL_TREE, V4HI_type_node,
11845 tree_cons (NULL_TREE, V4HI_type_node,
11846 endlink)));
11848 /* Normal vector binops. */
11849 tree v8qi_ftype_v8qi_v8qi
11850 = build_function_type (V8QI_type_node,
11851 tree_cons (NULL_TREE, V8QI_type_node,
11852 tree_cons (NULL_TREE, V8QI_type_node,
11853 endlink)));
11854 tree v4hi_ftype_v4hi_v4hi
11855 = build_function_type (V4HI_type_node,
11856 tree_cons (NULL_TREE, V4HI_type_node,
11857 tree_cons (NULL_TREE, V4HI_type_node,
11858 endlink)));
11859 tree v2si_ftype_v2si_v2si
11860 = build_function_type (V2SI_type_node,
11861 tree_cons (NULL_TREE, V2SI_type_node,
11862 tree_cons (NULL_TREE, V2SI_type_node,
11863 endlink)));
11864 tree di_ftype_di_di
11865 = build_function_type (long_long_unsigned_type_node,
11866 tree_cons (NULL_TREE, long_long_unsigned_type_node,
11867 tree_cons (NULL_TREE,
11868 long_long_unsigned_type_node,
11869 endlink)));
11871 /* Add all builtins that are more or less simple operations on two
11872 operands. */
11873 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11875 /* Use one of the operands; the target can have a different mode for
11876 mask-generating compares. */
11877 enum machine_mode mode;
11878 tree type;
11880 if (d->name == 0)
11881 continue;
11883 mode = insn_data[d->icode].operand[1].mode;
11885 switch (mode)
11887 case V8QImode:
11888 type = v8qi_ftype_v8qi_v8qi;
11889 break;
11890 case V4HImode:
11891 type = v4hi_ftype_v4hi_v4hi;
11892 break;
11893 case V2SImode:
11894 type = v2si_ftype_v2si_v2si;
11895 break;
11896 case DImode:
11897 type = di_ftype_di_di;
11898 break;
11900 default:
11901 abort ();
11904 def_mbuiltin (d->mask, d->name, type, d->code);
11907 /* Add the remaining MMX insns with somewhat more complicated types. */
11908 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11909 def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11910 def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11912 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11913 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11914 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11915 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11916 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11917 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11919 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11920 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11921 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11922 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11923 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11924 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11926 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11927 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11928 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11929 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11930 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11931 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11933 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11934 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11935 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11936 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
11937 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
11938 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
11940 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
11942 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
11943 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
11944 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
11945 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
11947 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
11948 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
11949 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
11950 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
11951 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
11952 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
11953 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
11954 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
11955 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
11957 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
11958 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
11959 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
11961 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
11962 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
11963 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
11965 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
11966 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
11967 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
11968 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
11969 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
11970 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
11972 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
11973 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
11974 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
11975 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
11976 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
11977 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
11978 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
11979 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
11980 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
11981 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
11982 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
11983 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
11985 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
11986 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
11987 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
11988 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
11990 def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
11991 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
11992 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
11993 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
11994 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
11995 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
11996 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
11999 static void
12000 arm_init_builtins (void)
12002 if (TARGET_REALLY_IWMMXT)
12003 arm_init_iwmmxt_builtins ();
12006 /* Errors in the source file can cause expand_expr to return const0_rtx
12007 where we expect a vector. To avoid crashing, use one of the vector
12008 clear instructions. */
12010 static rtx
12011 safe_vector_operand (rtx x, enum machine_mode mode)
12013 if (x != const0_rtx)
12014 return x;
12015 x = gen_reg_rtx (mode);
12017 emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12018 : gen_rtx_SUBREG (DImode, x, 0)));
12019 return x;
12022 /* Subroutine of arm_expand_builtin to take care of binop insns. */
12024 static rtx
12025 arm_expand_binop_builtin (enum insn_code icode,
12026 tree arglist, rtx target)
12028 rtx pat;
12029 tree arg0 = TREE_VALUE (arglist);
12030 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12031 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12032 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12033 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12034 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12035 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12037 if (VECTOR_MODE_P (mode0))
12038 op0 = safe_vector_operand (op0, mode0);
12039 if (VECTOR_MODE_P (mode1))
12040 op1 = safe_vector_operand (op1, mode1);
12042 if (! target
12043 || GET_MODE (target) != tmode
12044 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12045 target = gen_reg_rtx (tmode);
12047 /* In case the insn wants input operands in modes different from
12048 the result, abort. */
12049 if (GET_MODE (op0) != mode0 || GET_MODE (op1) != mode1)
12050 abort ();
12052 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12053 op0 = copy_to_mode_reg (mode0, op0);
12054 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12055 op1 = copy_to_mode_reg (mode1, op1);
12057 pat = GEN_FCN (icode) (target, op0, op1);
12058 if (! pat)
12059 return 0;
12060 emit_insn (pat);
12061 return target;
12064 /* Subroutine of arm_expand_builtin to take care of unop insns. */
12066 static rtx
12067 arm_expand_unop_builtin (enum insn_code icode,
12068 tree arglist, rtx target, int do_load)
12070 rtx pat;
12071 tree arg0 = TREE_VALUE (arglist);
12072 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12073 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12074 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12076 if (! target
12077 || GET_MODE (target) != tmode
12078 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12079 target = gen_reg_rtx (tmode);
12080 if (do_load)
12081 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12082 else
12084 if (VECTOR_MODE_P (mode0))
12085 op0 = safe_vector_operand (op0, mode0);
12087 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12088 op0 = copy_to_mode_reg (mode0, op0);
12091 pat = GEN_FCN (icode) (target, op0);
12092 if (! pat)
12093 return 0;
12094 emit_insn (pat);
12095 return target;
12098 /* Expand an expression EXP that calls a built-in function,
12099 with result going to TARGET if that's convenient
12100 (and in mode MODE if that's convenient).
12101 SUBTARGET may be used as the target for computing one of EXP's operands.
12102 IGNORE is nonzero if the value is to be ignored. */
12104 static rtx
12105 arm_expand_builtin (tree exp,
12106 rtx target,
12107 rtx subtarget ATTRIBUTE_UNUSED,
12108 enum machine_mode mode ATTRIBUTE_UNUSED,
12109 int ignore ATTRIBUTE_UNUSED)
12111 const struct builtin_description * d;
12112 enum insn_code icode;
12113 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12114 tree arglist = TREE_OPERAND (exp, 1);
12115 tree arg0;
12116 tree arg1;
12117 tree arg2;
12118 rtx op0;
12119 rtx op1;
12120 rtx op2;
12121 rtx pat;
12122 int fcode = DECL_FUNCTION_CODE (fndecl);
12123 size_t i;
12124 enum machine_mode tmode;
12125 enum machine_mode mode0;
12126 enum machine_mode mode1;
12127 enum machine_mode mode2;
12129 switch (fcode)
12131 case ARM_BUILTIN_TEXTRMSB:
12132 case ARM_BUILTIN_TEXTRMUB:
12133 case ARM_BUILTIN_TEXTRMSH:
12134 case ARM_BUILTIN_TEXTRMUH:
12135 case ARM_BUILTIN_TEXTRMSW:
12136 case ARM_BUILTIN_TEXTRMUW:
12137 icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12138 : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12139 : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12140 : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12141 : CODE_FOR_iwmmxt_textrmw);
12143 arg0 = TREE_VALUE (arglist);
12144 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12145 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12146 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12147 tmode = insn_data[icode].operand[0].mode;
12148 mode0 = insn_data[icode].operand[1].mode;
12149 mode1 = insn_data[icode].operand[2].mode;
12151 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12152 op0 = copy_to_mode_reg (mode0, op0);
12153 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12155 /* @@@ better error message */
12156 error ("selector must be an immediate");
12157 return gen_reg_rtx (tmode);
12159 if (target == 0
12160 || GET_MODE (target) != tmode
12161 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12162 target = gen_reg_rtx (tmode);
12163 pat = GEN_FCN (icode) (target, op0, op1);
12164 if (! pat)
12165 return 0;
12166 emit_insn (pat);
12167 return target;
12169 case ARM_BUILTIN_TINSRB:
12170 case ARM_BUILTIN_TINSRH:
12171 case ARM_BUILTIN_TINSRW:
12172 icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12173 : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12174 : CODE_FOR_iwmmxt_tinsrw);
12175 arg0 = TREE_VALUE (arglist);
12176 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12177 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12178 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12179 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12180 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12181 tmode = insn_data[icode].operand[0].mode;
12182 mode0 = insn_data[icode].operand[1].mode;
12183 mode1 = insn_data[icode].operand[2].mode;
12184 mode2 = insn_data[icode].operand[3].mode;
12186 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12187 op0 = copy_to_mode_reg (mode0, op0);
12188 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12189 op1 = copy_to_mode_reg (mode1, op1);
12190 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12192 /* @@@ better error message */
12193 error ("selector must be an immediate");
12194 return const0_rtx;
12196 if (target == 0
12197 || GET_MODE (target) != tmode
12198 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12199 target = gen_reg_rtx (tmode);
12200 pat = GEN_FCN (icode) (target, op0, op1, op2);
12201 if (! pat)
12202 return 0;
12203 emit_insn (pat);
12204 return target;
12206 case ARM_BUILTIN_SETWCX:
12207 arg0 = TREE_VALUE (arglist);
12208 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12209 op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12210 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12211 emit_insn (gen_iwmmxt_tmcr (op1, op0));
12212 return 0;
12214 case ARM_BUILTIN_GETWCX:
12215 arg0 = TREE_VALUE (arglist);
12216 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12217 target = gen_reg_rtx (SImode);
12218 emit_insn (gen_iwmmxt_tmrc (target, op0));
12219 return target;
12221 case ARM_BUILTIN_WSHUFH:
12222 icode = CODE_FOR_iwmmxt_wshufh;
12223 arg0 = TREE_VALUE (arglist);
12224 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12225 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12226 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12227 tmode = insn_data[icode].operand[0].mode;
12228 mode1 = insn_data[icode].operand[1].mode;
12229 mode2 = insn_data[icode].operand[2].mode;
12231 if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12232 op0 = copy_to_mode_reg (mode1, op0);
12233 if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12235 /* @@@ better error message */
12236 error ("mask must be an immediate");
12237 return const0_rtx;
12239 if (target == 0
12240 || GET_MODE (target) != tmode
12241 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12242 target = gen_reg_rtx (tmode);
12243 pat = GEN_FCN (icode) (target, op0, op1);
12244 if (! pat)
12245 return 0;
12246 emit_insn (pat);
12247 return target;
12249 case ARM_BUILTIN_WSADB:
12250 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12251 case ARM_BUILTIN_WSADH:
12252 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12253 case ARM_BUILTIN_WSADBZ:
12254 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12255 case ARM_BUILTIN_WSADHZ:
12256 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12258 /* Several three-argument builtins. */
12259 case ARM_BUILTIN_WMACS:
12260 case ARM_BUILTIN_WMACU:
12261 case ARM_BUILTIN_WALIGN:
12262 case ARM_BUILTIN_TMIA:
12263 case ARM_BUILTIN_TMIAPH:
12264 case ARM_BUILTIN_TMIATT:
12265 case ARM_BUILTIN_TMIATB:
12266 case ARM_BUILTIN_TMIABT:
12267 case ARM_BUILTIN_TMIABB:
12268 icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12269 : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12270 : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12271 : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12272 : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12273 : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12274 : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12275 : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12276 : CODE_FOR_iwmmxt_walign);
12277 arg0 = TREE_VALUE (arglist);
12278 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12279 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12280 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12281 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12282 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12283 tmode = insn_data[icode].operand[0].mode;
12284 mode0 = insn_data[icode].operand[1].mode;
12285 mode1 = insn_data[icode].operand[2].mode;
12286 mode2 = insn_data[icode].operand[3].mode;
12288 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12289 op0 = copy_to_mode_reg (mode0, op0);
12290 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12291 op1 = copy_to_mode_reg (mode1, op1);
12292 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12293 op2 = copy_to_mode_reg (mode2, op2);
12294 if (target == 0
12295 || GET_MODE (target) != tmode
12296 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12297 target = gen_reg_rtx (tmode);
12298 pat = GEN_FCN (icode) (target, op0, op1, op2);
12299 if (! pat)
12300 return 0;
12301 emit_insn (pat);
12302 return target;
12304 case ARM_BUILTIN_WZERO:
12305 target = gen_reg_rtx (DImode);
12306 emit_insn (gen_iwmmxt_clrdi (target));
12307 return target;
12309 default:
12310 break;
12313 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12314 if (d->code == (const enum arm_builtins) fcode)
12315 return arm_expand_binop_builtin (d->icode, arglist, target);
12317 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12318 if (d->code == (const enum arm_builtins) fcode)
12319 return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12321 /* @@@ Should really do something sensible here. */
12322 return NULL_RTX;
12325 /* Recursively search through all of the blocks in a function
12326 checking to see if any of the variables created in that
12327 function match the RTX called 'orig'. If they do then
12328 replace them with the RTX called 'new'. */
12329 static void
12330 replace_symbols_in_block (tree block, rtx orig, rtx new)
12332 for (; block; block = BLOCK_CHAIN (block))
12334 tree sym;
12336 if (!TREE_USED (block))
12337 continue;
12339 for (sym = BLOCK_VARS (block); sym; sym = TREE_CHAIN (sym))
12341 if ( (DECL_NAME (sym) == 0 && TREE_CODE (sym) != TYPE_DECL)
12342 || DECL_IGNORED_P (sym)
12343 || TREE_CODE (sym) != VAR_DECL
12344 || DECL_EXTERNAL (sym)
12345 || !rtx_equal_p (DECL_RTL (sym), orig)
12347 continue;
12349 SET_DECL_RTL (sym, new);
12352 replace_symbols_in_block (BLOCK_SUBBLOCKS (block), orig, new);
12356 /* Return the number (counting from 0) of
12357 the least significant set bit in MASK. */
12359 inline static int
12360 number_of_first_bit_set (int mask)
12362 int bit;
12364 for (bit = 0;
12365 (mask & (1 << bit)) == 0;
12366 ++bit)
12367 continue;
12369 return bit;
12372 /* Generate code to return from a thumb function.
12373 If 'reg_containing_return_addr' is -1, then the return address is
12374 actually on the stack, at the stack pointer. */
12375 static void
12376 thumb_exit (FILE *f, int reg_containing_return_addr, rtx eh_ofs)
12378 unsigned regs_available_for_popping;
12379 unsigned regs_to_pop;
12380 int pops_needed;
12381 unsigned available;
12382 unsigned required;
12383 int mode;
12384 int size;
12385 int restore_a4 = FALSE;
12387 /* Compute the registers we need to pop. */
12388 regs_to_pop = 0;
12389 pops_needed = 0;
12391 /* There is an assumption here, that if eh_ofs is not NULL, the
12392 normal return address will have been pushed. */
12393 if (reg_containing_return_addr == -1 || eh_ofs)
12395 /* When we are generating a return for __builtin_eh_return,
12396 reg_containing_return_addr must specify the return regno. */
12397 if (eh_ofs && reg_containing_return_addr == -1)
12398 abort ();
12400 regs_to_pop |= 1 << LR_REGNUM;
12401 ++pops_needed;
12404 if (TARGET_BACKTRACE)
12406 /* Restore the (ARM) frame pointer and stack pointer. */
12407 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12408 pops_needed += 2;
12411 /* If there is nothing to pop then just emit the BX instruction and
12412 return. */
12413 if (pops_needed == 0)
12415 if (eh_ofs)
12416 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12418 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12419 return;
12421 /* Otherwise if we are not supporting interworking and we have not created
12422 a backtrace structure and the function was not entered in ARM mode then
12423 just pop the return address straight into the PC. */
12424 else if (!TARGET_INTERWORK
12425 && !TARGET_BACKTRACE
12426 && !is_called_in_ARM_mode (current_function_decl))
12428 if (eh_ofs)
12430 asm_fprintf (f, "\tadd\t%r, #4\n", SP_REGNUM);
12431 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12432 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12434 else
12435 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12437 return;
12440 /* Find out how many of the (return) argument registers we can corrupt. */
12441 regs_available_for_popping = 0;
12443 /* If returning via __builtin_eh_return, the bottom three registers
12444 all contain information needed for the return. */
12445 if (eh_ofs)
12446 size = 12;
12447 else
12449 #ifdef RTX_CODE
12450 /* If we can deduce the registers used from the function's
12451 return value. This is more reliable that examining
12452 regs_ever_live[] because that will be set if the register is
12453 ever used in the function, not just if the register is used
12454 to hold a return value. */
12456 if (current_function_return_rtx != 0)
12457 mode = GET_MODE (current_function_return_rtx);
12458 else
12459 #endif
12460 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12462 size = GET_MODE_SIZE (mode);
12464 if (size == 0)
12466 /* In a void function we can use any argument register.
12467 In a function that returns a structure on the stack
12468 we can use the second and third argument registers. */
12469 if (mode == VOIDmode)
12470 regs_available_for_popping =
12471 (1 << ARG_REGISTER (1))
12472 | (1 << ARG_REGISTER (2))
12473 | (1 << ARG_REGISTER (3));
12474 else
12475 regs_available_for_popping =
12476 (1 << ARG_REGISTER (2))
12477 | (1 << ARG_REGISTER (3));
12479 else if (size <= 4)
12480 regs_available_for_popping =
12481 (1 << ARG_REGISTER (2))
12482 | (1 << ARG_REGISTER (3));
12483 else if (size <= 8)
12484 regs_available_for_popping =
12485 (1 << ARG_REGISTER (3));
12488 /* Match registers to be popped with registers into which we pop them. */
12489 for (available = regs_available_for_popping,
12490 required = regs_to_pop;
12491 required != 0 && available != 0;
12492 available &= ~(available & - available),
12493 required &= ~(required & - required))
12494 -- pops_needed;
12496 /* If we have any popping registers left over, remove them. */
12497 if (available > 0)
12498 regs_available_for_popping &= ~available;
12500 /* Otherwise if we need another popping register we can use
12501 the fourth argument register. */
12502 else if (pops_needed)
12504 /* If we have not found any free argument registers and
12505 reg a4 contains the return address, we must move it. */
12506 if (regs_available_for_popping == 0
12507 && reg_containing_return_addr == LAST_ARG_REGNUM)
12509 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12510 reg_containing_return_addr = LR_REGNUM;
12512 else if (size > 12)
12514 /* Register a4 is being used to hold part of the return value,
12515 but we have dire need of a free, low register. */
12516 restore_a4 = TRUE;
12518 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12521 if (reg_containing_return_addr != LAST_ARG_REGNUM)
12523 /* The fourth argument register is available. */
12524 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12526 --pops_needed;
12530 /* Pop as many registers as we can. */
12531 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12532 regs_available_for_popping);
12534 /* Process the registers we popped. */
12535 if (reg_containing_return_addr == -1)
12537 /* The return address was popped into the lowest numbered register. */
12538 regs_to_pop &= ~(1 << LR_REGNUM);
12540 reg_containing_return_addr =
12541 number_of_first_bit_set (regs_available_for_popping);
12543 /* Remove this register for the mask of available registers, so that
12544 the return address will not be corrupted by further pops. */
12545 regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12548 /* If we popped other registers then handle them here. */
12549 if (regs_available_for_popping)
12551 int frame_pointer;
12553 /* Work out which register currently contains the frame pointer. */
12554 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12556 /* Move it into the correct place. */
12557 asm_fprintf (f, "\tmov\t%r, %r\n",
12558 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12560 /* (Temporarily) remove it from the mask of popped registers. */
12561 regs_available_for_popping &= ~(1 << frame_pointer);
12562 regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12564 if (regs_available_for_popping)
12566 int stack_pointer;
12568 /* We popped the stack pointer as well,
12569 find the register that contains it. */
12570 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12572 /* Move it into the stack register. */
12573 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12575 /* At this point we have popped all necessary registers, so
12576 do not worry about restoring regs_available_for_popping
12577 to its correct value:
12579 assert (pops_needed == 0)
12580 assert (regs_available_for_popping == (1 << frame_pointer))
12581 assert (regs_to_pop == (1 << STACK_POINTER)) */
12583 else
12585 /* Since we have just move the popped value into the frame
12586 pointer, the popping register is available for reuse, and
12587 we know that we still have the stack pointer left to pop. */
12588 regs_available_for_popping |= (1 << frame_pointer);
12592 /* If we still have registers left on the stack, but we no longer have
12593 any registers into which we can pop them, then we must move the return
12594 address into the link register and make available the register that
12595 contained it. */
12596 if (regs_available_for_popping == 0 && pops_needed > 0)
12598 regs_available_for_popping |= 1 << reg_containing_return_addr;
12600 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12601 reg_containing_return_addr);
12603 reg_containing_return_addr = LR_REGNUM;
12606 /* If we have registers left on the stack then pop some more.
12607 We know that at most we will want to pop FP and SP. */
12608 if (pops_needed > 0)
12610 int popped_into;
12611 int move_to;
12613 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12614 regs_available_for_popping);
12616 /* We have popped either FP or SP.
12617 Move whichever one it is into the correct register. */
12618 popped_into = number_of_first_bit_set (regs_available_for_popping);
12619 move_to = number_of_first_bit_set (regs_to_pop);
12621 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12623 regs_to_pop &= ~(1 << move_to);
12625 --pops_needed;
12628 /* If we still have not popped everything then we must have only
12629 had one register available to us and we are now popping the SP. */
12630 if (pops_needed > 0)
12632 int popped_into;
12634 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12635 regs_available_for_popping);
12637 popped_into = number_of_first_bit_set (regs_available_for_popping);
12639 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12641 assert (regs_to_pop == (1 << STACK_POINTER))
12642 assert (pops_needed == 1)
12646 /* If necessary restore the a4 register. */
12647 if (restore_a4)
12649 if (reg_containing_return_addr != LR_REGNUM)
12651 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12652 reg_containing_return_addr = LR_REGNUM;
12655 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12658 if (eh_ofs)
12659 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12661 /* Return to caller. */
12662 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12665 /* Emit code to push or pop registers to or from the stack. F is the
12666 assembly file. MASK is the registers to push or pop. PUSH is
12667 nonzero if we should push, and zero if we should pop. For debugging
12668 output, if pushing, adjust CFA_OFFSET by the amount of space added
12669 to the stack. REAL_REGS should have the same number of bits set as
12670 MASK, and will be used instead (in the same order) to describe which
12671 registers were saved - this is used to mark the save slots when we
12672 push high registers after moving them to low registers. */
12673 static void
12674 thumb_pushpop (FILE *f, int mask, int push, int *cfa_offset, int real_regs)
12676 int regno;
12677 int lo_mask = mask & 0xFF;
12678 int pushed_words = 0;
12680 if (lo_mask == 0 && !push && (mask & (1 << 15)))
12682 /* Special case. Do not generate a POP PC statement here, do it in
12683 thumb_exit() */
12684 thumb_exit (f, -1, NULL_RTX);
12685 return;
12688 fprintf (f, "\t%s\t{", push ? "push" : "pop");
12690 /* Look at the low registers first. */
12691 for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12693 if (lo_mask & 1)
12695 asm_fprintf (f, "%r", regno);
12697 if ((lo_mask & ~1) != 0)
12698 fprintf (f, ", ");
12700 pushed_words++;
12704 if (push && (mask & (1 << LR_REGNUM)))
12706 /* Catch pushing the LR. */
12707 if (mask & 0xFF)
12708 fprintf (f, ", ");
12710 asm_fprintf (f, "%r", LR_REGNUM);
12712 pushed_words++;
12714 else if (!push && (mask & (1 << PC_REGNUM)))
12716 /* Catch popping the PC. */
12717 if (TARGET_INTERWORK || TARGET_BACKTRACE)
12719 /* The PC is never poped directly, instead
12720 it is popped into r3 and then BX is used. */
12721 fprintf (f, "}\n");
12723 thumb_exit (f, -1, NULL_RTX);
12725 return;
12727 else
12729 if (mask & 0xFF)
12730 fprintf (f, ", ");
12732 asm_fprintf (f, "%r", PC_REGNUM);
12736 fprintf (f, "}\n");
12738 if (push && pushed_words && dwarf2out_do_frame ())
12740 char *l = dwarf2out_cfi_label ();
12741 int pushed_mask = real_regs;
12743 *cfa_offset += pushed_words * 4;
12744 dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12746 pushed_words = 0;
12747 pushed_mask = real_regs;
12748 for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12750 if (pushed_mask & 1)
12751 dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12756 void
12757 thumb_final_prescan_insn (rtx insn)
12759 if (flag_print_asm_name)
12760 asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12761 INSN_ADDRESSES (INSN_UID (insn)));
12765 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12767 unsigned HOST_WIDE_INT mask = 0xff;
12768 int i;
12770 if (val == 0) /* XXX */
12771 return 0;
12773 for (i = 0; i < 25; i++)
12774 if ((val & (mask << i)) == val)
12775 return 1;
12777 return 0;
12780 /* Returns nonzero if the current function contains,
12781 or might contain a far jump. */
12782 static int
12783 thumb_far_jump_used_p (void)
12785 rtx insn;
12787 /* This test is only important for leaf functions. */
12788 /* assert (!leaf_function_p ()); */
12790 /* If we have already decided that far jumps may be used,
12791 do not bother checking again, and always return true even if
12792 it turns out that they are not being used. Once we have made
12793 the decision that far jumps are present (and that hence the link
12794 register will be pushed onto the stack) we cannot go back on it. */
12795 if (cfun->machine->far_jump_used)
12796 return 1;
12798 /* If this function is not being called from the prologue/epilogue
12799 generation code then it must be being called from the
12800 INITIAL_ELIMINATION_OFFSET macro. */
12801 if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12803 /* In this case we know that we are being asked about the elimination
12804 of the arg pointer register. If that register is not being used,
12805 then there are no arguments on the stack, and we do not have to
12806 worry that a far jump might force the prologue to push the link
12807 register, changing the stack offsets. In this case we can just
12808 return false, since the presence of far jumps in the function will
12809 not affect stack offsets.
12811 If the arg pointer is live (or if it was live, but has now been
12812 eliminated and so set to dead) then we do have to test to see if
12813 the function might contain a far jump. This test can lead to some
12814 false negatives, since before reload is completed, then length of
12815 branch instructions is not known, so gcc defaults to returning their
12816 longest length, which in turn sets the far jump attribute to true.
12818 A false negative will not result in bad code being generated, but it
12819 will result in a needless push and pop of the link register. We
12820 hope that this does not occur too often.
12822 If we need doubleword stack alignment this could affect the other
12823 elimination offsets so we can't risk getting it wrong. */
12824 if (regs_ever_live [ARG_POINTER_REGNUM])
12825 cfun->machine->arg_pointer_live = 1;
12826 else if (!cfun->machine->arg_pointer_live)
12827 return 0;
12830 /* Check to see if the function contains a branch
12831 insn with the far jump attribute set. */
12832 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12834 if (GET_CODE (insn) == JUMP_INSN
12835 /* Ignore tablejump patterns. */
12836 && GET_CODE (PATTERN (insn)) != ADDR_VEC
12837 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12838 && get_attr_far_jump (insn) == FAR_JUMP_YES
12841 /* Record the fact that we have decided that
12842 the function does use far jumps. */
12843 cfun->machine->far_jump_used = 1;
12844 return 1;
12848 return 0;
12851 /* Return nonzero if FUNC must be entered in ARM mode. */
12853 is_called_in_ARM_mode (tree func)
12855 if (TREE_CODE (func) != FUNCTION_DECL)
12856 abort ();
12858 /* Ignore the problem about functions whoes address is taken. */
12859 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12860 return TRUE;
12862 #ifdef ARM_PE
12863 return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12864 #else
12865 return FALSE;
12866 #endif
12869 /* The bits which aren't usefully expanded as rtl. */
12870 const char *
12871 thumb_unexpanded_epilogue (void)
12873 int regno;
12874 int live_regs_mask = 0;
12875 int high_regs_pushed = 0;
12876 int leaf_function = leaf_function_p ();
12877 int had_to_push_lr;
12878 rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
12880 if (return_used_this_function)
12881 return "";
12883 if (IS_NAKED (arm_current_func_type ()))
12884 return "";
12886 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12887 if (THUMB_REG_PUSHED_P (regno))
12888 live_regs_mask |= 1 << regno;
12890 for (regno = 8; regno < 13; regno++)
12891 if (THUMB_REG_PUSHED_P (regno))
12892 high_regs_pushed++;
12894 /* The prolog may have pushed some high registers to use as
12895 work registers. eg the testsuite file:
12896 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12897 compiles to produce:
12898 push {r4, r5, r6, r7, lr}
12899 mov r7, r9
12900 mov r6, r8
12901 push {r6, r7}
12902 as part of the prolog. We have to undo that pushing here. */
12904 if (high_regs_pushed)
12906 int mask = live_regs_mask;
12907 int next_hi_reg;
12908 int size;
12909 int mode;
12911 #ifdef RTX_CODE
12912 /* If we can deduce the registers used from the function's return value.
12913 This is more reliable that examining regs_ever_live[] because that
12914 will be set if the register is ever used in the function, not just if
12915 the register is used to hold a return value. */
12917 if (current_function_return_rtx != 0)
12918 mode = GET_MODE (current_function_return_rtx);
12919 else
12920 #endif
12921 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12923 size = GET_MODE_SIZE (mode);
12925 /* Unless we are returning a type of size > 12 register r3 is
12926 available. */
12927 if (size < 13)
12928 mask |= 1 << 3;
12930 if (mask == 0)
12931 /* Oh dear! We have no low registers into which we can pop
12932 high registers! */
12933 internal_error
12934 ("no low registers available for popping high registers");
12936 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
12937 if (THUMB_REG_PUSHED_P (next_hi_reg))
12938 break;
12940 while (high_regs_pushed)
12942 /* Find lo register(s) into which the high register(s) can
12943 be popped. */
12944 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12946 if (mask & (1 << regno))
12947 high_regs_pushed--;
12948 if (high_regs_pushed == 0)
12949 break;
12952 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
12954 /* Pop the values into the low register(s). */
12955 thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
12957 /* Move the value(s) into the high registers. */
12958 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12960 if (mask & (1 << regno))
12962 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
12963 regno);
12965 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
12966 if (THUMB_REG_PUSHED_P (next_hi_reg))
12967 break;
12973 had_to_push_lr = (live_regs_mask || !leaf_function
12974 || thumb_far_jump_used_p ());
12976 if (TARGET_BACKTRACE
12977 && ((live_regs_mask & 0xFF) == 0)
12978 && regs_ever_live [LAST_ARG_REGNUM] != 0)
12980 /* The stack backtrace structure creation code had to
12981 push R7 in order to get a work register, so we pop
12982 it now. */
12983 live_regs_mask |= (1 << LAST_LO_REGNUM);
12986 if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
12988 if (had_to_push_lr
12989 && !is_called_in_ARM_mode (current_function_decl)
12990 && !eh_ofs)
12991 live_regs_mask |= 1 << PC_REGNUM;
12993 /* Either no argument registers were pushed or a backtrace
12994 structure was created which includes an adjusted stack
12995 pointer, so just pop everything. */
12996 if (live_regs_mask)
12997 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12998 live_regs_mask);
13000 if (eh_ofs)
13001 thumb_exit (asm_out_file, 2, eh_ofs);
13002 /* We have either just popped the return address into the
13003 PC or it is was kept in LR for the entire function or
13004 it is still on the stack because we do not want to
13005 return by doing a pop {pc}. */
13006 else if ((live_regs_mask & (1 << PC_REGNUM)) == 0)
13007 thumb_exit (asm_out_file,
13008 (had_to_push_lr
13009 && is_called_in_ARM_mode (current_function_decl)) ?
13010 -1 : LR_REGNUM, NULL_RTX);
13012 else
13014 /* Pop everything but the return address. */
13015 live_regs_mask &= ~(1 << PC_REGNUM);
13017 if (live_regs_mask)
13018 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13019 live_regs_mask);
13021 if (had_to_push_lr)
13022 /* Get the return address into a temporary register. */
13023 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
13024 1 << LAST_ARG_REGNUM);
13026 /* Remove the argument registers that were pushed onto the stack. */
13027 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
13028 SP_REGNUM, SP_REGNUM,
13029 current_function_pretend_args_size);
13031 if (eh_ofs)
13032 thumb_exit (asm_out_file, 2, eh_ofs);
13033 else
13034 thumb_exit (asm_out_file,
13035 had_to_push_lr ? LAST_ARG_REGNUM : LR_REGNUM, NULL_RTX);
13038 return "";
13041 /* Functions to save and restore machine-specific function data. */
13042 static struct machine_function *
13043 arm_init_machine_status (void)
13045 struct machine_function *machine;
13046 machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
13048 #if ARM_FT_UNKNOWN != 0
13049 machine->func_type = ARM_FT_UNKNOWN;
13050 #endif
13051 return machine;
13054 /* Return an RTX indicating where the return address to the
13055 calling function can be found. */
13057 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
13059 if (count != 0)
13060 return NULL_RTX;
13062 if (TARGET_APCS_32)
13063 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13064 else
13066 rtx lr = gen_rtx_AND (Pmode, gen_rtx_REG (Pmode, LR_REGNUM),
13067 GEN_INT (RETURN_ADDR_MASK26));
13068 return get_func_hard_reg_initial_val (cfun, lr);
13072 /* Do anything needed before RTL is emitted for each function. */
13073 void
13074 arm_init_expanders (void)
13076 /* Arrange to initialize and mark the machine per-function status. */
13077 init_machine_status = arm_init_machine_status;
13081 /* Like arm_compute_initial_elimination offset. Simpler because
13082 THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer. */
13084 HOST_WIDE_INT
13085 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13087 arm_stack_offsets *offsets;
13089 offsets = arm_get_frame_offsets ();
13091 switch (from)
13093 case ARG_POINTER_REGNUM:
13094 switch (to)
13096 case STACK_POINTER_REGNUM:
13097 return offsets->outgoing_args - offsets->saved_args;
13099 case FRAME_POINTER_REGNUM:
13100 return offsets->soft_frame - offsets->saved_args;
13102 case THUMB_HARD_FRAME_POINTER_REGNUM:
13103 case ARM_HARD_FRAME_POINTER_REGNUM:
13104 return offsets->saved_regs - offsets->saved_args;
13106 default:
13107 abort();
13109 break;
13111 case FRAME_POINTER_REGNUM:
13112 switch (to)
13114 case STACK_POINTER_REGNUM:
13115 return offsets->outgoing_args - offsets->soft_frame;
13117 case THUMB_HARD_FRAME_POINTER_REGNUM:
13118 case ARM_HARD_FRAME_POINTER_REGNUM:
13119 return offsets->saved_regs - offsets->soft_frame;
13121 default:
13122 abort();
13124 break;
13126 default:
13127 abort ();
13132 /* Generate the rest of a function's prologue. */
13133 void
13134 thumb_expand_prologue (void)
13136 rtx insn, dwarf;
13138 HOST_WIDE_INT amount;
13139 arm_stack_offsets *offsets;
13140 unsigned long func_type;
13142 func_type = arm_current_func_type ();
13144 /* Naked functions don't have prologues. */
13145 if (IS_NAKED (func_type))
13146 return;
13148 if (IS_INTERRUPT (func_type))
13150 error ("interrupt Service Routines cannot be coded in Thumb mode");
13151 return;
13154 offsets = arm_get_frame_offsets ();
13156 if (frame_pointer_needed)
13158 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
13159 stack_pointer_rtx));
13160 RTX_FRAME_RELATED_P (insn) = 1;
13163 amount = offsets->outgoing_args - offsets->saved_regs;
13164 if (amount)
13166 if (amount < 512)
13168 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13169 GEN_INT (- amount)));
13170 RTX_FRAME_RELATED_P (insn) = 1;
13172 else
13174 int regno;
13175 rtx reg;
13177 /* The stack decrement is too big for an immediate value in a single
13178 insn. In theory we could issue multiple subtracts, but after
13179 three of them it becomes more space efficient to place the full
13180 value in the constant pool and load into a register. (Also the
13181 ARM debugger really likes to see only one stack decrement per
13182 function). So instead we look for a scratch register into which
13183 we can load the decrement, and then we subtract this from the
13184 stack pointer. Unfortunately on the thumb the only available
13185 scratch registers are the argument registers, and we cannot use
13186 these as they may hold arguments to the function. Instead we
13187 attempt to locate a call preserved register which is used by this
13188 function. If we can find one, then we know that it will have
13189 been pushed at the start of the prologue and so we can corrupt
13190 it now. */
13191 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13192 if (THUMB_REG_PUSHED_P (regno)
13193 && !(frame_pointer_needed
13194 && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13195 break;
13197 if (regno > LAST_LO_REGNUM) /* Very unlikely. */
13199 rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13201 /* Choose an arbitrary, non-argument low register. */
13202 reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13204 /* Save it by copying it into a high, scratch register. */
13205 emit_insn (gen_movsi (spare, reg));
13206 /* Add a USE to stop propagate_one_insn() from barfing. */
13207 emit_insn (gen_prologue_use (spare));
13209 /* Decrement the stack. */
13210 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13211 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13212 stack_pointer_rtx, reg));
13213 RTX_FRAME_RELATED_P (insn) = 1;
13214 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13215 plus_constant (stack_pointer_rtx,
13216 GEN_INT (- amount)));
13217 RTX_FRAME_RELATED_P (dwarf) = 1;
13218 REG_NOTES (insn)
13219 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13220 REG_NOTES (insn));
13222 /* Restore the low register's original value. */
13223 emit_insn (gen_movsi (reg, spare));
13225 /* Emit a USE of the restored scratch register, so that flow
13226 analysis will not consider the restore redundant. The
13227 register won't be used again in this function and isn't
13228 restored by the epilogue. */
13229 emit_insn (gen_prologue_use (reg));
13231 else
13233 reg = gen_rtx_REG (SImode, regno);
13235 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13237 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13238 stack_pointer_rtx, reg));
13239 RTX_FRAME_RELATED_P (insn) = 1;
13240 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13241 plus_constant (stack_pointer_rtx,
13242 GEN_INT (- amount)));
13243 RTX_FRAME_RELATED_P (dwarf) = 1;
13244 REG_NOTES (insn)
13245 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13246 REG_NOTES (insn));
13249 /* If the frame pointer is needed, emit a special barrier that
13250 will prevent the scheduler from moving stores to the frame
13251 before the stack adjustment. */
13252 if (frame_pointer_needed)
13253 emit_insn (gen_stack_tie (stack_pointer_rtx,
13254 hard_frame_pointer_rtx));
13257 if (current_function_profile || TARGET_NO_SCHED_PRO)
13258 emit_insn (gen_blockage ());
13261 void
13262 thumb_expand_epilogue (void)
13264 HOST_WIDE_INT amount;
13265 arm_stack_offsets *offsets;
13266 int regno;
13268 /* Naked functions don't have prologues. */
13269 if (IS_NAKED (arm_current_func_type ()))
13270 return;
13272 offsets = arm_get_frame_offsets ();
13273 amount = offsets->outgoing_args - offsets->saved_regs;
13275 if (frame_pointer_needed)
13276 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13277 else if (amount)
13279 if (amount < 512)
13280 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13281 GEN_INT (amount)));
13282 else
13284 /* r3 is always free in the epilogue. */
13285 rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13287 emit_insn (gen_movsi (reg, GEN_INT (amount)));
13288 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13292 /* Emit a USE (stack_pointer_rtx), so that
13293 the stack adjustment will not be deleted. */
13294 emit_insn (gen_prologue_use (stack_pointer_rtx));
13296 if (current_function_profile || TARGET_NO_SCHED_PRO)
13297 emit_insn (gen_blockage ());
13299 /* Emit a clobber for each insn that will be restored in the epilogue,
13300 so that flow2 will get register lifetimes correct. */
13301 for (regno = 0; regno < 13; regno++)
13302 if (regs_ever_live[regno] && !call_used_regs[regno])
13303 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13305 if (! regs_ever_live[LR_REGNUM])
13306 emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13309 static void
13310 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13312 int live_regs_mask = 0;
13313 int high_regs_pushed = 0;
13314 int cfa_offset = 0;
13315 int regno;
13317 if (IS_NAKED (arm_current_func_type ()))
13318 return;
13320 if (is_called_in_ARM_mode (current_function_decl))
13322 const char * name;
13324 if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
13325 abort ();
13326 if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
13327 abort ();
13328 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
13330 /* Generate code sequence to switch us into Thumb mode. */
13331 /* The .code 32 directive has already been emitted by
13332 ASM_DECLARE_FUNCTION_NAME. */
13333 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13334 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13336 /* Generate a label, so that the debugger will notice the
13337 change in instruction sets. This label is also used by
13338 the assembler to bypass the ARM code when this function
13339 is called from a Thumb encoded function elsewhere in the
13340 same file. Hence the definition of STUB_NAME here must
13341 agree with the definition in gas/config/tc-arm.c. */
13343 #define STUB_NAME ".real_start_of"
13345 fprintf (f, "\t.code\t16\n");
13346 #ifdef ARM_PE
13347 if (arm_dllexport_name_p (name))
13348 name = arm_strip_name_encoding (name);
13349 #endif
13350 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13351 fprintf (f, "\t.thumb_func\n");
13352 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13355 if (current_function_pretend_args_size)
13357 if (cfun->machine->uses_anonymous_args)
13359 int num_pushes;
13361 fprintf (f, "\tpush\t{");
13363 num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13365 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13366 regno <= LAST_ARG_REGNUM;
13367 regno++)
13368 asm_fprintf (f, "%r%s", regno,
13369 regno == LAST_ARG_REGNUM ? "" : ", ");
13371 fprintf (f, "}\n");
13373 else
13374 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13375 SP_REGNUM, SP_REGNUM,
13376 current_function_pretend_args_size);
13378 /* We don't need to record the stores for unwinding (would it
13379 help the debugger any if we did?), but record the change in
13380 the stack pointer. */
13381 if (dwarf2out_do_frame ())
13383 char *l = dwarf2out_cfi_label ();
13384 cfa_offset = cfa_offset + current_function_pretend_args_size;
13385 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13389 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13390 if (THUMB_REG_PUSHED_P (regno))
13391 live_regs_mask |= 1 << regno;
13393 if (live_regs_mask || !leaf_function_p () || thumb_far_jump_used_p ())
13394 live_regs_mask |= 1 << LR_REGNUM;
13396 if (TARGET_BACKTRACE)
13398 int offset;
13399 int work_register = 0;
13400 int wr;
13402 /* We have been asked to create a stack backtrace structure.
13403 The code looks like this:
13405 0 .align 2
13406 0 func:
13407 0 sub SP, #16 Reserve space for 4 registers.
13408 2 push {R7} Get a work register.
13409 4 add R7, SP, #20 Get the stack pointer before the push.
13410 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
13411 8 mov R7, PC Get hold of the start of this code plus 12.
13412 10 str R7, [SP, #16] Store it.
13413 12 mov R7, FP Get hold of the current frame pointer.
13414 14 str R7, [SP, #4] Store it.
13415 16 mov R7, LR Get hold of the current return address.
13416 18 str R7, [SP, #12] Store it.
13417 20 add R7, SP, #16 Point at the start of the backtrace structure.
13418 22 mov FP, R7 Put this value into the frame pointer. */
13420 if ((live_regs_mask & 0xFF) == 0)
13422 /* See if the a4 register is free. */
13424 if (regs_ever_live [LAST_ARG_REGNUM] == 0)
13425 work_register = LAST_ARG_REGNUM;
13426 else /* We must push a register of our own. */
13427 live_regs_mask |= (1 << LAST_LO_REGNUM);
13430 if (work_register == 0)
13432 /* Select a register from the list that will be pushed to
13433 use as our work register. */
13434 for (work_register = (LAST_LO_REGNUM + 1); work_register--;)
13435 if ((1 << work_register) & live_regs_mask)
13436 break;
13439 asm_fprintf
13440 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13441 SP_REGNUM, SP_REGNUM);
13443 if (dwarf2out_do_frame ())
13445 char *l = dwarf2out_cfi_label ();
13446 cfa_offset = cfa_offset + 16;
13447 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13450 if (live_regs_mask)
13451 thumb_pushpop (f, live_regs_mask, 1, &cfa_offset, live_regs_mask);
13453 for (offset = 0, wr = 1 << 15; wr != 0; wr >>= 1)
13454 if (wr & live_regs_mask)
13455 offset += 4;
13457 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13458 offset + 16 + current_function_pretend_args_size);
13460 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13461 offset + 4);
13463 /* Make sure that the instruction fetching the PC is in the right place
13464 to calculate "start of backtrace creation code + 12". */
13465 if (live_regs_mask)
13467 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13468 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13469 offset + 12);
13470 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13471 ARM_HARD_FRAME_POINTER_REGNUM);
13472 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13473 offset);
13475 else
13477 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13478 ARM_HARD_FRAME_POINTER_REGNUM);
13479 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13480 offset);
13481 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13482 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13483 offset + 12);
13486 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13487 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13488 offset + 8);
13489 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13490 offset + 12);
13491 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13492 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13494 else if (live_regs_mask)
13495 thumb_pushpop (f, live_regs_mask, 1, &cfa_offset, live_regs_mask);
13497 for (regno = 8; regno < 13; regno++)
13498 if (THUMB_REG_PUSHED_P (regno))
13499 high_regs_pushed++;
13501 if (high_regs_pushed)
13503 int pushable_regs = 0;
13504 int mask = live_regs_mask & 0xff;
13505 int next_hi_reg;
13507 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13508 if (THUMB_REG_PUSHED_P (next_hi_reg))
13509 break;
13511 pushable_regs = mask;
13513 if (pushable_regs == 0)
13515 /* Desperation time -- this probably will never happen. */
13516 if (THUMB_REG_PUSHED_P (LAST_ARG_REGNUM))
13517 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, LAST_ARG_REGNUM);
13518 mask = 1 << LAST_ARG_REGNUM;
13521 while (high_regs_pushed > 0)
13523 int real_regs_mask = 0;
13525 for (regno = LAST_LO_REGNUM; regno >= 0; regno--)
13527 if (mask & (1 << regno))
13529 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13531 high_regs_pushed--;
13532 real_regs_mask |= (1 << next_hi_reg);
13534 if (high_regs_pushed)
13536 for (next_hi_reg--; next_hi_reg > LAST_LO_REGNUM;
13537 next_hi_reg--)
13538 if (THUMB_REG_PUSHED_P (next_hi_reg))
13539 break;
13541 else
13543 mask &= ~((1 << regno) - 1);
13544 break;
13549 thumb_pushpop (f, mask, 1, &cfa_offset, real_regs_mask);
13552 if (pushable_regs == 0
13553 && (THUMB_REG_PUSHED_P (LAST_ARG_REGNUM)))
13554 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
13558 /* Handle the case of a double word load into a low register from
13559 a computed memory address. The computed address may involve a
13560 register which is overwritten by the load. */
13561 const char *
13562 thumb_load_double_from_address (rtx *operands)
13564 rtx addr;
13565 rtx base;
13566 rtx offset;
13567 rtx arg1;
13568 rtx arg2;
13570 if (GET_CODE (operands[0]) != REG)
13571 abort ();
13573 if (GET_CODE (operands[1]) != MEM)
13574 abort ();
13576 /* Get the memory address. */
13577 addr = XEXP (operands[1], 0);
13579 /* Work out how the memory address is computed. */
13580 switch (GET_CODE (addr))
13582 case REG:
13583 operands[2] = gen_rtx_MEM (SImode,
13584 plus_constant (XEXP (operands[1], 0), 4));
13586 if (REGNO (operands[0]) == REGNO (addr))
13588 output_asm_insn ("ldr\t%H0, %2", operands);
13589 output_asm_insn ("ldr\t%0, %1", operands);
13591 else
13593 output_asm_insn ("ldr\t%0, %1", operands);
13594 output_asm_insn ("ldr\t%H0, %2", operands);
13596 break;
13598 case CONST:
13599 /* Compute <address> + 4 for the high order load. */
13600 operands[2] = gen_rtx_MEM (SImode,
13601 plus_constant (XEXP (operands[1], 0), 4));
13603 output_asm_insn ("ldr\t%0, %1", operands);
13604 output_asm_insn ("ldr\t%H0, %2", operands);
13605 break;
13607 case PLUS:
13608 arg1 = XEXP (addr, 0);
13609 arg2 = XEXP (addr, 1);
13611 if (CONSTANT_P (arg1))
13612 base = arg2, offset = arg1;
13613 else
13614 base = arg1, offset = arg2;
13616 if (GET_CODE (base) != REG)
13617 abort ();
13619 /* Catch the case of <address> = <reg> + <reg> */
13620 if (GET_CODE (offset) == REG)
13622 int reg_offset = REGNO (offset);
13623 int reg_base = REGNO (base);
13624 int reg_dest = REGNO (operands[0]);
13626 /* Add the base and offset registers together into the
13627 higher destination register. */
13628 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13629 reg_dest + 1, reg_base, reg_offset);
13631 /* Load the lower destination register from the address in
13632 the higher destination register. */
13633 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13634 reg_dest, reg_dest + 1);
13636 /* Load the higher destination register from its own address
13637 plus 4. */
13638 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13639 reg_dest + 1, reg_dest + 1);
13641 else
13643 /* Compute <address> + 4 for the high order load. */
13644 operands[2] = gen_rtx_MEM (SImode,
13645 plus_constant (XEXP (operands[1], 0), 4));
13647 /* If the computed address is held in the low order register
13648 then load the high order register first, otherwise always
13649 load the low order register first. */
13650 if (REGNO (operands[0]) == REGNO (base))
13652 output_asm_insn ("ldr\t%H0, %2", operands);
13653 output_asm_insn ("ldr\t%0, %1", operands);
13655 else
13657 output_asm_insn ("ldr\t%0, %1", operands);
13658 output_asm_insn ("ldr\t%H0, %2", operands);
13661 break;
13663 case LABEL_REF:
13664 /* With no registers to worry about we can just load the value
13665 directly. */
13666 operands[2] = gen_rtx_MEM (SImode,
13667 plus_constant (XEXP (operands[1], 0), 4));
13669 output_asm_insn ("ldr\t%H0, %2", operands);
13670 output_asm_insn ("ldr\t%0, %1", operands);
13671 break;
13673 default:
13674 abort ();
13675 break;
13678 return "";
13681 const char *
13682 thumb_output_move_mem_multiple (int n, rtx *operands)
13684 rtx tmp;
13686 switch (n)
13688 case 2:
13689 if (REGNO (operands[4]) > REGNO (operands[5]))
13691 tmp = operands[4];
13692 operands[4] = operands[5];
13693 operands[5] = tmp;
13695 output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13696 output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13697 break;
13699 case 3:
13700 if (REGNO (operands[4]) > REGNO (operands[5]))
13702 tmp = operands[4];
13703 operands[4] = operands[5];
13704 operands[5] = tmp;
13706 if (REGNO (operands[5]) > REGNO (operands[6]))
13708 tmp = operands[5];
13709 operands[5] = operands[6];
13710 operands[6] = tmp;
13712 if (REGNO (operands[4]) > REGNO (operands[5]))
13714 tmp = operands[4];
13715 operands[4] = operands[5];
13716 operands[5] = tmp;
13719 output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13720 output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13721 break;
13723 default:
13724 abort ();
13727 return "";
13730 /* Routines for generating rtl. */
13731 void
13732 thumb_expand_movstrqi (rtx *operands)
13734 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13735 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13736 HOST_WIDE_INT len = INTVAL (operands[2]);
13737 HOST_WIDE_INT offset = 0;
13739 while (len >= 12)
13741 emit_insn (gen_movmem12b (out, in, out, in));
13742 len -= 12;
13745 if (len >= 8)
13747 emit_insn (gen_movmem8b (out, in, out, in));
13748 len -= 8;
13751 if (len >= 4)
13753 rtx reg = gen_reg_rtx (SImode);
13754 emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13755 emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13756 len -= 4;
13757 offset += 4;
13760 if (len >= 2)
13762 rtx reg = gen_reg_rtx (HImode);
13763 emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
13764 plus_constant (in, offset))));
13765 emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13766 reg));
13767 len -= 2;
13768 offset += 2;
13771 if (len)
13773 rtx reg = gen_reg_rtx (QImode);
13774 emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13775 plus_constant (in, offset))));
13776 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13777 reg));
13782 thumb_cmp_operand (rtx op, enum machine_mode mode)
13784 return ((GET_CODE (op) == CONST_INT
13785 && INTVAL (op) < 256
13786 && INTVAL (op) >= 0)
13787 || s_register_operand (op, mode));
13791 thumb_cmpneg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
13793 return (GET_CODE (op) == CONST_INT
13794 && INTVAL (op) < 0
13795 && INTVAL (op) > -256);
13798 /* Return TRUE if a result can be stored in OP without clobbering the
13799 condition code register. Prior to reload we only accept a
13800 register. After reload we have to be able to handle memory as
13801 well, since a pseudo may not get a hard reg and reload cannot
13802 handle output-reloads on jump insns.
13804 We could possibly handle mem before reload as well, but that might
13805 complicate things with the need to handle increment
13806 side-effects. */
13809 thumb_cbrch_target_operand (rtx op, enum machine_mode mode)
13811 return (s_register_operand (op, mode)
13812 || ((reload_in_progress || reload_completed)
13813 && memory_operand (op, mode)));
13816 /* Handle storing a half-word to memory during reload. */
13817 void
13818 thumb_reload_out_hi (rtx *operands)
13820 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13823 /* Handle reading a half-word from memory during reload. */
13824 void
13825 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13827 abort ();
13830 /* Return the length of a function name prefix
13831 that starts with the character 'c'. */
13832 static int
13833 arm_get_strip_length (int c)
13835 switch (c)
13837 ARM_NAME_ENCODING_LENGTHS
13838 default: return 0;
13842 /* Return a pointer to a function's name with any
13843 and all prefix encodings stripped from it. */
13844 const char *
13845 arm_strip_name_encoding (const char *name)
13847 int skip;
13849 while ((skip = arm_get_strip_length (* name)))
13850 name += skip;
13852 return name;
13855 /* If there is a '*' anywhere in the name's prefix, then
13856 emit the stripped name verbatim, otherwise prepend an
13857 underscore if leading underscores are being used. */
13858 void
13859 arm_asm_output_labelref (FILE *stream, const char *name)
13861 int skip;
13862 int verbatim = 0;
13864 while ((skip = arm_get_strip_length (* name)))
13866 verbatim |= (*name == '*');
13867 name += skip;
13870 if (verbatim)
13871 fputs (name, stream);
13872 else
13873 asm_fprintf (stream, "%U%s", name);
13876 rtx aof_pic_label;
13878 #ifdef AOF_ASSEMBLER
13879 /* Special functions only needed when producing AOF syntax assembler. */
13881 struct pic_chain
13883 struct pic_chain * next;
13884 const char * symname;
13887 static struct pic_chain * aof_pic_chain = NULL;
13890 aof_pic_entry (rtx x)
13892 struct pic_chain ** chainp;
13893 int offset;
13895 if (aof_pic_label == NULL_RTX)
13897 aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
13900 for (offset = 0, chainp = &aof_pic_chain; *chainp;
13901 offset += 4, chainp = &(*chainp)->next)
13902 if ((*chainp)->symname == XSTR (x, 0))
13903 return plus_constant (aof_pic_label, offset);
13905 *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13906 (*chainp)->next = NULL;
13907 (*chainp)->symname = XSTR (x, 0);
13908 return plus_constant (aof_pic_label, offset);
13911 void
13912 aof_dump_pic_table (FILE *f)
13914 struct pic_chain * chain;
13916 if (aof_pic_chain == NULL)
13917 return;
13919 asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13920 PIC_OFFSET_TABLE_REGNUM,
13921 PIC_OFFSET_TABLE_REGNUM);
13922 fputs ("|x$adcons|\n", f);
13924 for (chain = aof_pic_chain; chain; chain = chain->next)
13926 fputs ("\tDCD\t", f);
13927 assemble_name (f, chain->symname);
13928 fputs ("\n", f);
13932 int arm_text_section_count = 1;
13934 char *
13935 aof_text_section (void )
13937 static char buf[100];
13938 sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
13939 arm_text_section_count++);
13940 if (flag_pic)
13941 strcat (buf, ", PIC, REENTRANT");
13942 return buf;
13945 static int arm_data_section_count = 1;
13947 char *
13948 aof_data_section (void)
13950 static char buf[100];
13951 sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
13952 return buf;
13955 /* The AOF assembler is religiously strict about declarations of
13956 imported and exported symbols, so that it is impossible to declare
13957 a function as imported near the beginning of the file, and then to
13958 export it later on. It is, however, possible to delay the decision
13959 until all the functions in the file have been compiled. To get
13960 around this, we maintain a list of the imports and exports, and
13961 delete from it any that are subsequently defined. At the end of
13962 compilation we spit the remainder of the list out before the END
13963 directive. */
13965 struct import
13967 struct import * next;
13968 const char * name;
13971 static struct import * imports_list = NULL;
13973 void
13974 aof_add_import (const char *name)
13976 struct import * new;
13978 for (new = imports_list; new; new = new->next)
13979 if (new->name == name)
13980 return;
13982 new = (struct import *) xmalloc (sizeof (struct import));
13983 new->next = imports_list;
13984 imports_list = new;
13985 new->name = name;
13988 void
13989 aof_delete_import (const char *name)
13991 struct import ** old;
13993 for (old = &imports_list; *old; old = & (*old)->next)
13995 if ((*old)->name == name)
13997 *old = (*old)->next;
13998 return;
14003 int arm_main_function = 0;
14005 static void
14006 aof_dump_imports (FILE *f)
14008 /* The AOF assembler needs this to cause the startup code to be extracted
14009 from the library. Brining in __main causes the whole thing to work
14010 automagically. */
14011 if (arm_main_function)
14013 text_section ();
14014 fputs ("\tIMPORT __main\n", f);
14015 fputs ("\tDCD __main\n", f);
14018 /* Now dump the remaining imports. */
14019 while (imports_list)
14021 fprintf (f, "\tIMPORT\t");
14022 assemble_name (f, imports_list->name);
14023 fputc ('\n', f);
14024 imports_list = imports_list->next;
14028 static void
14029 aof_globalize_label (FILE *stream, const char *name)
14031 default_globalize_label (stream, name);
14032 if (! strcmp (name, "main"))
14033 arm_main_function = 1;
14036 static void
14037 aof_file_start (void)
14039 fputs ("__r0\tRN\t0\n", asm_out_file);
14040 fputs ("__a1\tRN\t0\n", asm_out_file);
14041 fputs ("__a2\tRN\t1\n", asm_out_file);
14042 fputs ("__a3\tRN\t2\n", asm_out_file);
14043 fputs ("__a4\tRN\t3\n", asm_out_file);
14044 fputs ("__v1\tRN\t4\n", asm_out_file);
14045 fputs ("__v2\tRN\t5\n", asm_out_file);
14046 fputs ("__v3\tRN\t6\n", asm_out_file);
14047 fputs ("__v4\tRN\t7\n", asm_out_file);
14048 fputs ("__v5\tRN\t8\n", asm_out_file);
14049 fputs ("__v6\tRN\t9\n", asm_out_file);
14050 fputs ("__sl\tRN\t10\n", asm_out_file);
14051 fputs ("__fp\tRN\t11\n", asm_out_file);
14052 fputs ("__ip\tRN\t12\n", asm_out_file);
14053 fputs ("__sp\tRN\t13\n", asm_out_file);
14054 fputs ("__lr\tRN\t14\n", asm_out_file);
14055 fputs ("__pc\tRN\t15\n", asm_out_file);
14056 fputs ("__f0\tFN\t0\n", asm_out_file);
14057 fputs ("__f1\tFN\t1\n", asm_out_file);
14058 fputs ("__f2\tFN\t2\n", asm_out_file);
14059 fputs ("__f3\tFN\t3\n", asm_out_file);
14060 fputs ("__f4\tFN\t4\n", asm_out_file);
14061 fputs ("__f5\tFN\t5\n", asm_out_file);
14062 fputs ("__f6\tFN\t6\n", asm_out_file);
14063 fputs ("__f7\tFN\t7\n", asm_out_file);
14064 text_section ();
14067 static void
14068 aof_file_end (void)
14070 if (flag_pic)
14071 aof_dump_pic_table (asm_out_file);
14072 aof_dump_imports (asm_out_file);
14073 fputs ("\tEND\n", asm_out_file);
14075 #endif /* AOF_ASSEMBLER */
14077 #ifdef OBJECT_FORMAT_ELF
14078 /* Switch to an arbitrary section NAME with attributes as specified
14079 by FLAGS. ALIGN specifies any known alignment requirements for
14080 the section; 0 if the default should be used.
14082 Differs from the default elf version only in the prefix character
14083 used before the section type. */
14085 static void
14086 arm_elf_asm_named_section (const char *name, unsigned int flags)
14088 char flagchars[10], *f = flagchars;
14090 if (! named_section_first_declaration (name))
14092 fprintf (asm_out_file, "\t.section\t%s\n", name);
14093 return;
14096 if (!(flags & SECTION_DEBUG))
14097 *f++ = 'a';
14098 if (flags & SECTION_WRITE)
14099 *f++ = 'w';
14100 if (flags & SECTION_CODE)
14101 *f++ = 'x';
14102 if (flags & SECTION_SMALL)
14103 *f++ = 's';
14104 if (flags & SECTION_MERGE)
14105 *f++ = 'M';
14106 if (flags & SECTION_STRINGS)
14107 *f++ = 'S';
14108 if (flags & SECTION_TLS)
14109 *f++ = 'T';
14110 *f = '\0';
14112 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
14114 if (!(flags & SECTION_NOTYPE))
14116 const char *type;
14118 if (flags & SECTION_BSS)
14119 type = "nobits";
14120 else
14121 type = "progbits";
14123 fprintf (asm_out_file, ",%%%s", type);
14125 if (flags & SECTION_ENTSIZE)
14126 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
14129 putc ('\n', asm_out_file);
14131 #endif
14133 #ifndef ARM_PE
14134 /* Symbols in the text segment can be accessed without indirecting via the
14135 constant pool; it may take an extra binary operation, but this is still
14136 faster than indirecting via memory. Don't do this when not optimizing,
14137 since we won't be calculating al of the offsets necessary to do this
14138 simplification. */
14140 static void
14141 arm_encode_section_info (tree decl, rtx rtl, int first)
14143 /* This doesn't work with AOF syntax, since the string table may be in
14144 a different AREA. */
14145 #ifndef AOF_ASSEMBLER
14146 if (optimize > 0 && TREE_CONSTANT (decl))
14147 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14148 #endif
14150 /* If we are referencing a function that is weak then encode a long call
14151 flag in the function name, otherwise if the function is static or
14152 or known to be defined in this file then encode a short call flag. */
14153 if (first && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
14155 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14156 arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14157 else if (! TREE_PUBLIC (decl))
14158 arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14161 #endif /* !ARM_PE */
14163 static void
14164 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14166 if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14167 && !strcmp (prefix, "L"))
14169 arm_ccfsm_state = 0;
14170 arm_target_insn = NULL;
14172 default_internal_label (stream, prefix, labelno);
14175 /* Output code to add DELTA to the first argument, and then jump
14176 to FUNCTION. Used for C++ multiple inheritance. */
14177 static void
14178 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14179 HOST_WIDE_INT delta,
14180 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14181 tree function)
14183 static int thunk_label = 0;
14184 char label[256];
14185 int mi_delta = delta;
14186 const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14187 int shift = 0;
14188 int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14189 ? 1 : 0);
14190 if (mi_delta < 0)
14191 mi_delta = - mi_delta;
14192 if (TARGET_THUMB)
14194 int labelno = thunk_label++;
14195 ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14196 fputs ("\tldr\tr12, ", file);
14197 assemble_name (file, label);
14198 fputc ('\n', file);
14200 while (mi_delta != 0)
14202 if ((mi_delta & (3 << shift)) == 0)
14203 shift += 2;
14204 else
14206 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14207 mi_op, this_regno, this_regno,
14208 mi_delta & (0xff << shift));
14209 mi_delta &= ~(0xff << shift);
14210 shift += 8;
14213 if (TARGET_THUMB)
14215 fprintf (file, "\tbx\tr12\n");
14216 ASM_OUTPUT_ALIGN (file, 2);
14217 assemble_name (file, label);
14218 fputs (":\n", file);
14219 assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14221 else
14223 fputs ("\tb\t", file);
14224 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14225 if (NEED_PLT_RELOC)
14226 fputs ("(PLT)", file);
14227 fputc ('\n', file);
14232 arm_emit_vector_const (FILE *file, rtx x)
14234 int i;
14235 const char * pattern;
14237 if (GET_CODE (x) != CONST_VECTOR)
14238 abort ();
14240 switch (GET_MODE (x))
14242 case V2SImode: pattern = "%08x"; break;
14243 case V4HImode: pattern = "%04x"; break;
14244 case V8QImode: pattern = "%02x"; break;
14245 default: abort ();
14248 fprintf (file, "0x");
14249 for (i = CONST_VECTOR_NUNITS (x); i--;)
14251 rtx element;
14253 element = CONST_VECTOR_ELT (x, i);
14254 fprintf (file, pattern, INTVAL (element));
14257 return 1;
14260 const char *
14261 arm_output_load_gr (rtx *operands)
14263 rtx reg;
14264 rtx offset;
14265 rtx wcgr;
14266 rtx sum;
14268 if (GET_CODE (operands [1]) != MEM
14269 || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14270 || GET_CODE (reg = XEXP (sum, 0)) != REG
14271 || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14272 || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14273 return "wldrw%?\t%0, %1";
14275 /* Fix up an out-of-range load of a GR register. */
14276 output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14277 wcgr = operands[0];
14278 operands[0] = reg;
14279 output_asm_insn ("ldr%?\t%0, %1", operands);
14281 operands[0] = wcgr;
14282 operands[1] = reg;
14283 output_asm_insn ("tmcr%?\t%0, %1", operands);
14284 output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14286 return "";
14289 static rtx
14290 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14291 int incoming ATTRIBUTE_UNUSED)
14293 #if 0
14294 /* FIXME: The ARM backend has special code to handle structure
14295 returns, and will reserve its own hidden first argument. So
14296 if this macro is enabled a *second* hidden argument will be
14297 reserved, which will break binary compatibility with old
14298 toolchains and also thunk handling. One day this should be
14299 fixed. */
14300 return 0;
14301 #else
14302 /* Register in which address to store a structure value
14303 is passed to a function. */
14304 return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14305 #endif
14308 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14310 On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14311 named arg and all anonymous args onto the stack.
14312 XXX I know the prologue shouldn't be pushing registers, but it is faster
14313 that way. */
14315 static void
14316 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14317 enum machine_mode mode ATTRIBUTE_UNUSED,
14318 tree type ATTRIBUTE_UNUSED,
14319 int *pretend_size,
14320 int second_time ATTRIBUTE_UNUSED)
14322 cfun->machine->uses_anonymous_args = 1;
14323 if (cum->nregs < NUM_ARG_REGS)
14324 *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14327 /* Return nonzero if the CONSUMER instruction (a store) does not need
14328 PRODUCER's value to calculate the address. */
14331 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14333 rtx value = PATTERN (producer);
14334 rtx addr = PATTERN (consumer);
14336 if (GET_CODE (value) == COND_EXEC)
14337 value = COND_EXEC_CODE (value);
14338 if (GET_CODE (value) == PARALLEL)
14339 value = XVECEXP (value, 0, 0);
14340 value = XEXP (value, 0);
14341 if (GET_CODE (addr) == COND_EXEC)
14342 addr = COND_EXEC_CODE (addr);
14343 if (GET_CODE (addr) == PARALLEL)
14344 addr = XVECEXP (addr, 0, 0);
14345 addr = XEXP (addr, 0);
14347 return !reg_overlap_mentioned_p (value, addr);
14350 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14351 have an early register shift value or amount dependency on the
14352 result of PRODUCER. */
14355 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14357 rtx value = PATTERN (producer);
14358 rtx op = PATTERN (consumer);
14359 rtx early_op;
14361 if (GET_CODE (value) == COND_EXEC)
14362 value = COND_EXEC_CODE (value);
14363 if (GET_CODE (value) == PARALLEL)
14364 value = XVECEXP (value, 0, 0);
14365 value = XEXP (value, 0);
14366 if (GET_CODE (op) == COND_EXEC)
14367 op = COND_EXEC_CODE (op);
14368 if (GET_CODE (op) == PARALLEL)
14369 op = XVECEXP (op, 0, 0);
14370 op = XEXP (op, 1);
14372 early_op = XEXP (op, 0);
14373 /* This is either an actual independent shift, or a shift applied to
14374 the first operand of another operation. We want the whole shift
14375 operation. */
14376 if (GET_CODE (early_op) == REG)
14377 early_op = op;
14379 return !reg_overlap_mentioned_p (value, early_op);
14382 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14383 have an early register shift value dependency on the result of
14384 PRODUCER. */
14387 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14389 rtx value = PATTERN (producer);
14390 rtx op = PATTERN (consumer);
14391 rtx early_op;
14393 if (GET_CODE (value) == COND_EXEC)
14394 value = COND_EXEC_CODE (value);
14395 if (GET_CODE (value) == PARALLEL)
14396 value = XVECEXP (value, 0, 0);
14397 value = XEXP (value, 0);
14398 if (GET_CODE (op) == COND_EXEC)
14399 op = COND_EXEC_CODE (op);
14400 if (GET_CODE (op) == PARALLEL)
14401 op = XVECEXP (op, 0, 0);
14402 op = XEXP (op, 1);
14404 early_op = XEXP (op, 0);
14406 /* This is either an actual independent shift, or a shift applied to
14407 the first operand of another operation. We want the value being
14408 shifted, in either case. */
14409 if (GET_CODE (early_op) != REG)
14410 early_op = XEXP (early_op, 0);
14412 return !reg_overlap_mentioned_p (value, early_op);
14415 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14416 have an early register mult dependency on the result of
14417 PRODUCER. */
14420 arm_no_early_mul_dep (rtx producer, rtx consumer)
14422 rtx value = PATTERN (producer);
14423 rtx op = PATTERN (consumer);
14425 if (GET_CODE (value) == COND_EXEC)
14426 value = COND_EXEC_CODE (value);
14427 if (GET_CODE (value) == PARALLEL)
14428 value = XVECEXP (value, 0, 0);
14429 value = XEXP (value, 0);
14430 if (GET_CODE (op) == COND_EXEC)
14431 op = COND_EXEC_CODE (op);
14432 if (GET_CODE (op) == PARALLEL)
14433 op = XVECEXP (op, 0, 0);
14434 op = XEXP (op, 1);
14436 return (GET_CODE (op) == PLUS
14437 && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14441 /* We can't rely on the caller doing the proper promotion when
14442 using APCS or ATPCS. */
14444 static bool
14445 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14447 return arm_abi == ARM_ABI_APCS || arm_abi == ARM_ABI_ATPCS;