]> gcc.gnu.org Git - gcc.git/blame - gcc/config/arm/arm.c
[multiple changes]
[gcc.git] / gcc / config / arm / arm.c
CommitLineData
b36ba79f 1/* Output routines for GCC for ARM.
f954388e
RE
2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004 Free Software Foundation, Inc.
cce8749e 4 Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
956d6950 5 and Martin Simmons (@harleqn.co.uk).
b36ba79f 6 More major hacks by Richard Earnshaw (rearnsha@arm.com).
cce8749e 7
4f448245 8 This file is part of GCC.
cce8749e 9
4f448245
NC
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.
cce8749e 14
4f448245
NC
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.
cce8749e 19
4f448245
NC
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. */
ff9940b0 24
56636818 25#include "config.h"
43cffd11 26#include "system.h"
4977bab6
ZW
27#include "coretypes.h"
28#include "tm.h"
cce8749e 29#include "rtl.h"
d5b7b3ae 30#include "tree.h"
c7319d87 31#include "obstack.h"
cce8749e
CH
32#include "regs.h"
33#include "hard-reg-set.h"
34#include "real.h"
35#include "insn-config.h"
36#include "conditions.h"
cce8749e
CH
37#include "output.h"
38#include "insn-attr.h"
39#include "flags.h"
af48348a 40#include "reload.h"
49ad7cfa 41#include "function.h"
bee06f3d 42#include "expr.h"
e78d8e51 43#include "optabs.h"
ad076f4e 44#include "toplev.h"
aec3cfba 45#include "recog.h"
92a432f4 46#include "ggc.h"
d5b7b3ae 47#include "except.h"
8b97c5f8 48#include "c-pragma.h"
7b8b8ade 49#include "integrate.h"
c27ba912 50#include "tm_p.h"
672a6f42
NB
51#include "target.h"
52#include "target-def.h"
980e61bb 53#include "debug.h"
cce8749e 54
d5b7b3ae
RE
55/* Forward definitions of types. */
56typedef struct minipool_node Mnode;
57typedef struct minipool_fixup Mfix;
58
1d6e90ac
NC
59const struct attribute_spec arm_attribute_table[];
60
d5b7b3ae 61/* Forward function declarations. */
5848830f 62static arm_stack_offsets *arm_get_frame_offsets (void);
e32bac5b 63static void arm_add_gc_roots (void);
a406f566
MM
64static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
65 HOST_WIDE_INT, rtx, rtx, int, int);
e32bac5b
RE
66static unsigned bit_count (unsigned long);
67static int arm_address_register_rtx_p (rtx, int);
1e1ab407 68static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
e32bac5b
RE
69static int thumb_base_register_rtx_p (rtx, enum machine_mode, int);
70inline static int thumb_index_register_rtx_p (rtx, int);
5848830f 71static int thumb_far_jump_used_p (void);
e32bac5b 72static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
e32bac5b
RE
73static rtx emit_multi_reg_push (int);
74static rtx emit_sfm (int, int);
301d03af 75#ifndef AOF_ASSEMBLER
e32bac5b 76static bool arm_assemble_integer (rtx, unsigned int, int);
301d03af 77#endif
e32bac5b
RE
78static const char *fp_const_from_val (REAL_VALUE_TYPE *);
79static arm_cc get_arm_condition_code (rtx);
e32bac5b
RE
80static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
81static rtx is_jump_table (rtx);
82static const char *output_multi_immediate (rtx *, const char *, const char *,
83 int, HOST_WIDE_INT);
84static void print_multi_reg (FILE *, const char *, int, int);
85static const char *shift_op (rtx, HOST_WIDE_INT *);
86static struct machine_function *arm_init_machine_status (void);
87static int number_of_first_bit_set (int);
88static void replace_symbols_in_block (tree, rtx, rtx);
89static void thumb_exit (FILE *, int, rtx);
980e61bb 90static void thumb_pushpop (FILE *, int, int, int *, int);
e32bac5b
RE
91static rtx is_jump_table (rtx);
92static HOST_WIDE_INT get_jump_table_size (rtx);
93static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
94static Mnode *add_minipool_forward_ref (Mfix *);
95static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
96static Mnode *add_minipool_backward_ref (Mfix *);
97static void assign_minipool_offsets (Mfix *);
98static void arm_print_value (FILE *, rtx);
99static void dump_minipool (rtx);
100static int arm_barrier_cost (rtx);
101static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
102static void push_minipool_barrier (rtx, HOST_WIDE_INT);
103static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
104 rtx);
105static void arm_reorg (void);
106static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
107static int current_file_function_operand (rtx);
108static unsigned long arm_compute_save_reg0_reg12_mask (void);
109static unsigned long arm_compute_save_reg_mask (void);
110static unsigned long arm_isr_value (tree);
111static unsigned long arm_compute_func_type (void);
112static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
113static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
114static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
115static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
116static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
117static int arm_comp_type_attributes (tree, tree);
118static void arm_set_default_type_attributes (tree);
119static int arm_adjust_cost (rtx, rtx, rtx, int);
120static int arm_use_dfa_pipeline_interface (void);
121static int count_insns_for_constant (HOST_WIDE_INT, int);
122static int arm_get_strip_length (int);
123static bool arm_function_ok_for_sibcall (tree, tree);
124static void arm_internal_label (FILE *, const char *, unsigned long);
125static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
126 tree);
127static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
9b66ebb1
PB
128static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
129static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
130static bool arm_xscale_rtx_costs (rtx, int, int, int *);
131static bool arm_9e_rtx_costs (rtx, int, int, int *);
e32bac5b
RE
132static int arm_address_cost (rtx);
133static bool arm_memory_load_p (rtx);
134static bool arm_cirrus_insn_p (rtx);
135static void cirrus_reorg (rtx);
5a9335ef
NC
136static void arm_init_builtins (void);
137static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
138static void arm_init_iwmmxt_builtins (void);
139static rtx safe_vector_operand (rtx, enum machine_mode);
140static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
141static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
142static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
a406f566 143static void emit_constant_insn (rtx cond, rtx pattern);
5a9335ef 144
ebe413e5 145#ifdef OBJECT_FORMAT_ELF
e32bac5b 146static void arm_elf_asm_named_section (const char *, unsigned int);
ebe413e5 147#endif
fb49053f 148#ifndef ARM_PE
e32bac5b 149static void arm_encode_section_info (tree, rtx, int);
fb49053f 150#endif
5eb99654 151#ifdef AOF_ASSEMBLER
e32bac5b
RE
152static void aof_globalize_label (FILE *, const char *);
153static void aof_dump_imports (FILE *);
154static void aof_dump_pic_table (FILE *);
1bc7c5b6 155static void aof_file_start (void);
e32bac5b 156static void aof_file_end (void);
5eb99654 157#endif
f9ba5949 158static rtx arm_struct_value_rtx (tree, int);
1cc9f5f5
KH
159static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
160 tree, int *, int);
c237e94a 161
672a6f42
NB
162\f
163/* Initialize the GCC target structure. */
164#ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
1d6e90ac 165#undef TARGET_MERGE_DECL_ATTRIBUTES
672a6f42
NB
166#define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
167#endif
f3bb6135 168
1d6e90ac 169#undef TARGET_ATTRIBUTE_TABLE
91d231cb 170#define TARGET_ATTRIBUTE_TABLE arm_attribute_table
672a6f42 171
301d03af 172#ifdef AOF_ASSEMBLER
1d6e90ac 173#undef TARGET_ASM_BYTE_OP
301d03af 174#define TARGET_ASM_BYTE_OP "\tDCB\t"
1d6e90ac 175#undef TARGET_ASM_ALIGNED_HI_OP
301d03af 176#define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
1d6e90ac 177#undef TARGET_ASM_ALIGNED_SI_OP
301d03af 178#define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
5eb99654
KG
179#undef TARGET_ASM_GLOBALIZE_LABEL
180#define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
1bc7c5b6
ZW
181#undef TARGET_ASM_FILE_START
182#define TARGET_ASM_FILE_START aof_file_start
a5fe455b
ZW
183#undef TARGET_ASM_FILE_END
184#define TARGET_ASM_FILE_END aof_file_end
301d03af 185#else
1d6e90ac 186#undef TARGET_ASM_ALIGNED_SI_OP
301d03af 187#define TARGET_ASM_ALIGNED_SI_OP NULL
1d6e90ac 188#undef TARGET_ASM_INTEGER
301d03af
RS
189#define TARGET_ASM_INTEGER arm_assemble_integer
190#endif
191
1d6e90ac 192#undef TARGET_ASM_FUNCTION_PROLOGUE
08c148a8
NB
193#define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
194
1d6e90ac 195#undef TARGET_ASM_FUNCTION_EPILOGUE
08c148a8
NB
196#define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
197
1d6e90ac 198#undef TARGET_COMP_TYPE_ATTRIBUTES
8d8e52be
JM
199#define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
200
1d6e90ac 201#undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
8d8e52be
JM
202#define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
203
1d6e90ac 204#undef TARGET_SCHED_ADJUST_COST
c237e94a
ZW
205#define TARGET_SCHED_ADJUST_COST arm_adjust_cost
206
103fc15d
BE
207#undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE
208#define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE arm_use_dfa_pipeline_interface
209
fb49053f
RH
210#undef TARGET_ENCODE_SECTION_INFO
211#ifdef ARM_PE
212#define TARGET_ENCODE_SECTION_INFO arm_pe_encode_section_info
213#else
214#define TARGET_ENCODE_SECTION_INFO arm_encode_section_info
215#endif
216
5a9335ef 217#undef TARGET_STRIP_NAME_ENCODING
772c5265
RH
218#define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
219
5a9335ef 220#undef TARGET_ASM_INTERNAL_LABEL
4977bab6
ZW
221#define TARGET_ASM_INTERNAL_LABEL arm_internal_label
222
5a9335ef 223#undef TARGET_FUNCTION_OK_FOR_SIBCALL
4977bab6
ZW
224#define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
225
5a9335ef 226#undef TARGET_ASM_OUTPUT_MI_THUNK
c590b625 227#define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
5a9335ef 228#undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
3961e8fe 229#define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
c590b625 230
9b66ebb1 231/* This will be overridden in arm_override_options. */
5a9335ef 232#undef TARGET_RTX_COSTS
9b66ebb1 233#define TARGET_RTX_COSTS arm_slowmul_rtx_costs
5a9335ef 234#undef TARGET_ADDRESS_COST
dcefdf67 235#define TARGET_ADDRESS_COST arm_address_cost
3c50106f 236
5a9335ef 237#undef TARGET_MACHINE_DEPENDENT_REORG
18dbd950
RS
238#define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
239
5a9335ef
NC
240#undef TARGET_INIT_BUILTINS
241#define TARGET_INIT_BUILTINS arm_init_builtins
242#undef TARGET_EXPAND_BUILTIN
243#define TARGET_EXPAND_BUILTIN arm_expand_builtin
244
f9ba5949
KH
245#undef TARGET_PROMOTE_FUNCTION_ARGS
246#define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
d4453b7a
PB
247#undef TARGET_PROMOTE_FUNCTION_RETURN
248#define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
f9ba5949 249#undef TARGET_PROMOTE_PROTOTYPES
d4453b7a 250#define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_false
f9ba5949
KH
251
252#undef TARGET_STRUCT_VALUE_RTX
253#define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
254
1cc9f5f5
KH
255#undef TARGET_SETUP_INCOMING_VARARGS
256#define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
257
f6897b10 258struct gcc_target targetm = TARGET_INITIALIZER;
672a6f42 259\f
c7319d87
RE
260/* Obstack for minipool constant handling. */
261static struct obstack minipool_obstack;
1d6e90ac 262static char * minipool_startobj;
c7319d87 263
1d6e90ac
NC
264/* The maximum number of insns skipped which
265 will be conditionalised if possible. */
c27ba912
DM
266static int max_insns_skipped = 5;
267
268extern FILE * asm_out_file;
269
6354dc9b 270/* True if we are currently building a constant table. */
13bd191d
PB
271int making_const_table;
272
60d0536b 273/* Define the information needed to generate branch insns. This is
6354dc9b 274 stored from the compare operation. */
ff9940b0 275rtx arm_compare_op0, arm_compare_op1;
ff9940b0 276
9b66ebb1
PB
277/* The processor for which instructions should be scheduled. */
278enum processor_type arm_tune = arm_none;
279
280/* Which floating point model to use. */
281enum arm_fp_model arm_fp_model;
bee06f3d 282
9b66ebb1 283/* Which floating point hardware is available. */
29ad9694 284enum fputype arm_fpu_arch;
b111229a 285
9b66ebb1
PB
286/* Which floating point hardware to schedule for. */
287enum fputype arm_fpu_tune;
288
289/* Whether to use floating point hardware. */
290enum float_abi_type arm_float_abi;
291
5848830f
PB
292/* Which ABI to use. */
293enum arm_abi_type arm_abi;
294
6354dc9b 295/* What program mode is the cpu running in? 26-bit mode or 32-bit mode. */
2b835d68
RE
296enum prog_mode_type arm_prgmode;
297
9b66ebb1
PB
298/* Set by the -mfpu=... option. */
299const char * target_fpu_name = NULL;
300
301/* Set by the -mfpe=... option. */
302const char * target_fpe_name = NULL;
303
304/* Set by the -mfloat-abi=... option. */
305const char * target_float_abi_name = NULL;
2b835d68 306
5848830f
PB
307/* Set by the -mabi=... option. */
308const char * target_abi_name = NULL;
309
b355a481 310/* Used to parse -mstructure_size_boundary command line option. */
f9cc092a 311const char * structure_size_string = NULL;
723ae7c1 312int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
b355a481 313
aec3cfba 314/* Bit values used to identify processor capabilities. */
62b10bbc 315#define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
9b66ebb1 316#define FL_ARCH3M (1 << 1) /* Extended multiply */
62b10bbc
NC
317#define FL_MODE26 (1 << 2) /* 26-bit mode support */
318#define FL_MODE32 (1 << 3) /* 32-bit mode support */
319#define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
320#define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
321#define FL_THUMB (1 << 6) /* Thumb aware */
322#define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
323#define FL_STRONG (1 << 8) /* StrongARM */
6bc82793 324#define FL_ARCH5E (1 << 9) /* DSP extensions to v5 */
d19fb8e3 325#define FL_XSCALE (1 << 10) /* XScale */
9b6b54e2 326#define FL_CIRRUS (1 << 11) /* Cirrus/DSP. */
9b66ebb1 327#define FL_ARCH6 (1 << 12) /* Architecture rel 6. Adds
81f9037c
MM
328 media instructions. */
329#define FL_VFPV2 (1 << 13) /* Vector Floating Point V2. */
aec3cfba 330
9b66ebb1
PB
331#define FL_IWMMXT (1 << 29) /* XScale v2 or "Intel Wireless MMX technology". */
332
1d6e90ac
NC
333/* The bits in this mask specify which
334 instructions we are allowed to generate. */
0977774b 335static unsigned long insn_flags = 0;
d5b7b3ae 336
aec3cfba 337/* The bits in this mask specify which instruction scheduling options should
9b66ebb1 338 be used. */
0977774b 339static unsigned long tune_flags = 0;
aec3cfba
NC
340
341/* The following are used in the arm.md file as equivalents to bits
342 in the above two flag variables. */
343
9b66ebb1
PB
344/* Nonzero if this chip supports the ARM Architecture 3M extensions. */
345int arm_arch3m = 0;
2b835d68 346
6354dc9b 347/* Nonzero if this chip supports the ARM Architecture 4 extensions. */
2b835d68
RE
348int arm_arch4 = 0;
349
6354dc9b 350/* Nonzero if this chip supports the ARM Architecture 5 extensions. */
62b10bbc
NC
351int arm_arch5 = 0;
352
b15bca31
RE
353/* Nonzero if this chip supports the ARM Architecture 5E extensions. */
354int arm_arch5e = 0;
355
9b66ebb1
PB
356/* Nonzero if this chip supports the ARM Architecture 6 extensions. */
357int arm_arch6 = 0;
358
aec3cfba 359/* Nonzero if this chip can benefit from load scheduling. */
f5a1b0d2
NC
360int arm_ld_sched = 0;
361
362/* Nonzero if this chip is a StrongARM. */
363int arm_is_strong = 0;
364
5a9335ef
NC
365/* Nonzero if this chip supports Intel Wireless MMX technology. */
366int arm_arch_iwmmxt = 0;
367
d19fb8e3 368/* Nonzero if this chip is an XScale. */
4b3c2e48
PB
369int arm_arch_xscale = 0;
370
371/* Nonzero if tuning for XScale */
372int arm_tune_xscale = 0;
d19fb8e3 373
3569057d 374/* Nonzero if this chip is an ARM6 or an ARM7. */
f5a1b0d2 375int arm_is_6_or_7 = 0;
b111229a 376
0616531f
RE
377/* Nonzero if generating Thumb instructions. */
378int thumb_code = 0;
379
cce8749e
CH
380/* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
381 must report the mode of the memory reference from PRINT_OPERAND to
382 PRINT_OPERAND_ADDRESS. */
f3bb6135 383enum machine_mode output_memory_reference_mode;
cce8749e 384
32de079a 385/* The register number to be used for the PIC offset register. */
ed0e6530 386const char * arm_pic_register_string = NULL;
5b43fed1 387int arm_pic_register = INVALID_REGNUM;
32de079a 388
ff9940b0 389/* Set to 1 when a return insn is output, this means that the epilogue
6354dc9b 390 is not needed. */
d5b7b3ae 391int return_used_this_function;
ff9940b0 392
aec3cfba
NC
393/* Set to 1 after arm_reorg has started. Reset to start at the start of
394 the next function. */
4b632bf1
RE
395static int after_arm_reorg = 0;
396
aec3cfba 397/* The maximum number of insns to be used when loading a constant. */
2b835d68
RE
398static int arm_constant_limit = 3;
399
cce8749e
CH
400/* For an explanation of these variables, see final_prescan_insn below. */
401int arm_ccfsm_state;
84ed5e79 402enum arm_cond_code arm_current_cc;
cce8749e
CH
403rtx arm_target_insn;
404int arm_target_label;
9997d19d
RE
405
406/* The condition codes of the ARM, and the inverse function. */
1d6e90ac 407static const char * const arm_condition_codes[] =
9997d19d
RE
408{
409 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
410 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
411};
412
f5a1b0d2 413#define streq(string1, string2) (strcmp (string1, string2) == 0)
2b835d68 414\f
6354dc9b 415/* Initialization code. */
2b835d68 416
2b835d68
RE
417struct processors
418{
8b60264b 419 const char *const name;
9b66ebb1 420 enum processor_type core;
0977774b 421 const unsigned long flags;
9b66ebb1 422 bool (* rtx_costs) (rtx, int, int, int *);
2b835d68
RE
423};
424
425/* Not all of these give usefully different compilation alternatives,
426 but there is no simple way of generalizing them. */
8b60264b 427static const struct processors all_cores[] =
f5a1b0d2
NC
428{
429 /* ARM Cores */
9b66ebb1
PB
430#define ARM_CORE(NAME, FLAGS, COSTS) \
431 {#NAME, arm_none, FLAGS, arm_##COSTS##_rtx_costs},
432#include "arm-cores.def"
433#undef ARM_CORE
434 {NULL, arm_none, 0, NULL}
f5a1b0d2
NC
435};
436
8b60264b 437static const struct processors all_architectures[] =
2b835d68 438{
f5a1b0d2 439 /* ARM Architectures */
9b66ebb1
PB
440 /* We don't specify rtx_costs here as it will be figured out
441 from the core. */
f5a1b0d2 442
9b66ebb1
PB
443 { "armv2", arm2, FL_CO_PROC | FL_MODE26 , NULL},
444 { "armv2a", arm2, FL_CO_PROC | FL_MODE26 , NULL},
445 { "armv3", arm6, FL_CO_PROC | FL_MODE26 | FL_MODE32 , NULL},
446 { "armv3m", arm7m, FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_ARCH3M , NULL},
447 { "armv4", arm7tdmi, FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_ARCH3M | FL_ARCH4 , NULL},
b111229a
RE
448 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
449 implementations that support it, so we will leave it out for now. */
9b66ebb1
PB
450 { "armv4t", arm7tdmi, FL_CO_PROC | FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB , NULL},
451 { "armv5", arm10tdmi, FL_CO_PROC | FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_ARCH5 , NULL},
452 { "armv5t", arm10tdmi, FL_CO_PROC | FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_ARCH5 , NULL},
453 { "armv5te", arm1026ejs, FL_CO_PROC | FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_ARCH5 | FL_ARCH5E , NULL},
454 { "armv6", arm1136js, FL_CO_PROC | FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_ARCH5 | FL_ARCH5E | FL_ARCH6 , NULL},
455 { "armv6j", arm1136js, FL_CO_PROC | FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_ARCH5 | FL_ARCH5E | FL_ARCH6 , NULL},
456 { "ep9312", ep9312, FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_LDSCHED | FL_CIRRUS , NULL},
457 {"iwmmxt", iwmmxt, FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_LDSCHED | FL_STRONG | FL_ARCH5 | FL_ARCH5E | FL_XSCALE | FL_IWMMXT , NULL},
458 { NULL, arm_none, 0 , NULL}
f5a1b0d2
NC
459};
460
9a9f7594 461/* This is a magic structure. The 'string' field is magically filled in
f5a1b0d2
NC
462 with a pointer to the value specified by the user on the command line
463 assuming that the user has specified such a value. */
464
465struct arm_cpu_select arm_select[] =
466{
467 /* string name processors */
468 { NULL, "-mcpu=", all_cores },
469 { NULL, "-march=", all_architectures },
470 { NULL, "-mtune=", all_cores }
2b835d68
RE
471};
472
9b66ebb1
PB
473struct fpu_desc
474{
475 const char * name;
476 enum fputype fpu;
477};
478
479
480/* Available values for for -mfpu=. */
481
482static const struct fpu_desc all_fpus[] =
483{
484 {"fpa", FPUTYPE_FPA},
485 {"fpe2", FPUTYPE_FPA_EMU2},
486 {"fpe3", FPUTYPE_FPA_EMU2},
487 {"maverick", FPUTYPE_MAVERICK},
488 {"vfp", FPUTYPE_VFP}
489};
490
491
492/* Floating point models used by the different hardware.
493 See fputype in arm.h. */
494
495static const enum fputype fp_model_for_fpu[] =
496{
497 /* No FP hardware. */
498 ARM_FP_MODEL_UNKNOWN, /* FPUTYPE_NONE */
499 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA */
500 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU2 */
501 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU3 */
502 ARM_FP_MODEL_MAVERICK, /* FPUTYPE_MAVERICK */
503 ARM_FP_MODEL_VFP /* FPUTYPE_VFP */
504};
505
506
507struct float_abi
508{
509 const char * name;
510 enum float_abi_type abi_type;
511};
512
513
514/* Available values for -mfloat-abi=. */
515
516static const struct float_abi all_float_abis[] =
517{
518 {"soft", ARM_FLOAT_ABI_SOFT},
519 {"softfp", ARM_FLOAT_ABI_SOFTFP},
520 {"hard", ARM_FLOAT_ABI_HARD}
521};
522
523
5848830f
PB
524struct abi_name
525{
526 const char *name;
527 enum arm_abi_type abi_type;
528};
529
530
531/* Available values for -mabi=. */
532
533static const struct abi_name arm_all_abis[] =
534{
535 {"apcs-gnu", ARM_ABI_APCS},
536 {"atpcs", ARM_ABI_ATPCS},
537 {"aapcs", ARM_ABI_AAPCS},
538 {"iwmmxt", ARM_ABI_IWMMXT}
539};
540
0977774b
JT
541/* Return the number of bits set in VALUE. */
542static unsigned
e32bac5b 543bit_count (unsigned long value)
aec3cfba 544{
d5b7b3ae 545 unsigned long count = 0;
aec3cfba
NC
546
547 while (value)
548 {
0977774b
JT
549 count++;
550 value &= value - 1; /* Clear the least-significant set bit. */
aec3cfba
NC
551 }
552
553 return count;
554}
555
2b835d68
RE
556/* Fix up any incompatible options that the user has specified.
557 This has now turned into a maze. */
558void
e32bac5b 559arm_override_options (void)
2b835d68 560{
ed4c4348 561 unsigned i;
9b66ebb1 562
f5a1b0d2 563 /* Set up the flags based on the cpu/architecture selected by the user. */
b6a1cbae 564 for (i = ARRAY_SIZE (arm_select); i--;)
bd9c7e23 565 {
f5a1b0d2
NC
566 struct arm_cpu_select * ptr = arm_select + i;
567
568 if (ptr->string != NULL && ptr->string[0] != '\0')
bd9c7e23 569 {
13bd191d 570 const struct processors * sel;
bd9c7e23 571
5895f793 572 for (sel = ptr->processors; sel->name != NULL; sel++)
f5a1b0d2 573 if (streq (ptr->string, sel->name))
bd9c7e23 574 {
9b66ebb1
PB
575 /* Determine the processor core for which we should
576 tune code-generation. */
577 if (/* -mcpu= is a sensible default. */
578 i == 0
579 /* If -march= is used, and -mcpu= has not been used,
580 assume that we should tune for a representative
581 CPU from that architecture. */
582 || i == 1
583 /* -mtune= overrides -mcpu= and -march=. */
584 || i == 2)
585 arm_tune = (enum processor_type) (sel - ptr->processors);
586
587 if (i != 2)
b111229a 588 {
aec3cfba
NC
589 /* If we have been given an architecture and a processor
590 make sure that they are compatible. We only generate
591 a warning though, and we prefer the CPU over the
6354dc9b 592 architecture. */
aec3cfba 593 if (insn_flags != 0 && (insn_flags ^ sel->flags))
6cf32035 594 warning ("switch -mcpu=%s conflicts with -march= switch",
aec3cfba
NC
595 ptr->string);
596
597 insn_flags = sel->flags;
b111229a 598 }
f5a1b0d2 599
bd9c7e23
RE
600 break;
601 }
602
603 if (sel->name == NULL)
604 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
605 }
606 }
aec3cfba 607
f5a1b0d2 608 /* If the user did not specify a processor, choose one for them. */
aec3cfba 609 if (insn_flags == 0)
f5a1b0d2 610 {
8b60264b 611 const struct processors * sel;
aec3cfba 612 unsigned int sought;
8b60264b 613 static const struct cpu_default
aec3cfba 614 {
8b60264b
KG
615 const int cpu;
616 const char *const name;
aec3cfba
NC
617 }
618 cpu_defaults[] =
619 {
620 { TARGET_CPU_arm2, "arm2" },
621 { TARGET_CPU_arm6, "arm6" },
622 { TARGET_CPU_arm610, "arm610" },
2aa0c933 623 { TARGET_CPU_arm710, "arm710" },
aec3cfba
NC
624 { TARGET_CPU_arm7m, "arm7m" },
625 { TARGET_CPU_arm7500fe, "arm7500fe" },
626 { TARGET_CPU_arm7tdmi, "arm7tdmi" },
627 { TARGET_CPU_arm8, "arm8" },
628 { TARGET_CPU_arm810, "arm810" },
629 { TARGET_CPU_arm9, "arm9" },
630 { TARGET_CPU_strongarm, "strongarm" },
d19fb8e3 631 { TARGET_CPU_xscale, "xscale" },
9b6b54e2 632 { TARGET_CPU_ep9312, "ep9312" },
5a9335ef 633 { TARGET_CPU_iwmmxt, "iwmmxt" },
9b66ebb1
PB
634 { TARGET_CPU_arm926ejs, "arm926ejs" },
635 { TARGET_CPU_arm1026ejs, "arm1026ejs" },
636 { TARGET_CPU_arm1136js, "arm1136js" },
637 { TARGET_CPU_arm1136jfs, "arm1136jfs" },
aec3cfba
NC
638 { TARGET_CPU_generic, "arm" },
639 { 0, 0 }
640 };
8b60264b 641 const struct cpu_default * def;
aec3cfba
NC
642
643 /* Find the default. */
5895f793 644 for (def = cpu_defaults; def->name; def++)
aec3cfba
NC
645 if (def->cpu == TARGET_CPU_DEFAULT)
646 break;
647
648 /* Make sure we found the default CPU. */
649 if (def->name == NULL)
650 abort ();
651
652 /* Find the default CPU's flags. */
5895f793 653 for (sel = all_cores; sel->name != NULL; sel++)
aec3cfba
NC
654 if (streq (def->name, sel->name))
655 break;
656
657 if (sel->name == NULL)
658 abort ();
659
660 insn_flags = sel->flags;
9b66ebb1 661
aec3cfba
NC
662 /* Now check to see if the user has specified some command line
663 switch that require certain abilities from the cpu. */
664 sought = 0;
f5a1b0d2 665
d5b7b3ae 666 if (TARGET_INTERWORK || TARGET_THUMB)
f5a1b0d2 667 {
aec3cfba
NC
668 sought |= (FL_THUMB | FL_MODE32);
669
670 /* Force apcs-32 to be used for interworking. */
f5a1b0d2 671 target_flags |= ARM_FLAG_APCS_32;
aec3cfba 672
d5b7b3ae 673 /* There are no ARM processors that support both APCS-26 and
aec3cfba
NC
674 interworking. Therefore we force FL_MODE26 to be removed
675 from insn_flags here (if it was set), so that the search
676 below will always be able to find a compatible processor. */
5895f793 677 insn_flags &= ~FL_MODE26;
f5a1b0d2 678 }
5895f793 679 else if (!TARGET_APCS_32)
f5a1b0d2 680 sought |= FL_MODE26;
d5b7b3ae 681
aec3cfba 682 if (sought != 0 && ((sought & insn_flags) != sought))
f5a1b0d2 683 {
aec3cfba
NC
684 /* Try to locate a CPU type that supports all of the abilities
685 of the default CPU, plus the extra abilities requested by
686 the user. */
5895f793 687 for (sel = all_cores; sel->name != NULL; sel++)
aec3cfba 688 if ((sel->flags & sought) == (sought | insn_flags))
f5a1b0d2
NC
689 break;
690
691 if (sel->name == NULL)
aec3cfba 692 {
0977774b 693 unsigned current_bit_count = 0;
8b60264b 694 const struct processors * best_fit = NULL;
aec3cfba
NC
695
696 /* Ideally we would like to issue an error message here
697 saying that it was not possible to find a CPU compatible
698 with the default CPU, but which also supports the command
699 line options specified by the programmer, and so they
700 ought to use the -mcpu=<name> command line option to
701 override the default CPU type.
702
703 Unfortunately this does not work with multilibing. We
704 need to be able to support multilibs for -mapcs-26 and for
705 -mthumb-interwork and there is no CPU that can support both
706 options. Instead if we cannot find a cpu that has both the
707 characteristics of the default cpu and the given command line
708 options we scan the array again looking for a best match. */
5895f793 709 for (sel = all_cores; sel->name != NULL; sel++)
aec3cfba
NC
710 if ((sel->flags & sought) == sought)
711 {
0977774b 712 unsigned count;
aec3cfba
NC
713
714 count = bit_count (sel->flags & insn_flags);
715
716 if (count >= current_bit_count)
717 {
718 best_fit = sel;
719 current_bit_count = count;
720 }
721 }
f5a1b0d2 722
aec3cfba
NC
723 if (best_fit == NULL)
724 abort ();
725 else
726 sel = best_fit;
727 }
728
729 insn_flags = sel->flags;
f5a1b0d2 730 }
9b66ebb1
PB
731 if (arm_tune == arm_none)
732 arm_tune = (enum processor_type) (sel - all_cores);
f5a1b0d2 733 }
aec3cfba 734
9b66ebb1
PB
735 /* The processor for which we should tune should now have been
736 chosen. */
737 if (arm_tune == arm_none)
738 abort ();
739
740 tune_flags = all_cores[(int)arm_tune].flags;
741 targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
e26053d1 742
f5a1b0d2
NC
743 /* Make sure that the processor choice does not conflict with any of the
744 other command line choices. */
aec3cfba 745 if (TARGET_APCS_32 && !(insn_flags & FL_MODE32))
f5a1b0d2 746 {
aec3cfba
NC
747 /* If APCS-32 was not the default then it must have been set by the
748 user, so issue a warning message. If the user has specified
749 "-mapcs-32 -mcpu=arm2" then we loose here. */
750 if ((TARGET_DEFAULT & ARM_FLAG_APCS_32) == 0)
751 warning ("target CPU does not support APCS-32" );
5895f793 752 target_flags &= ~ARM_FLAG_APCS_32;
f5a1b0d2 753 }
5895f793 754 else if (!TARGET_APCS_32 && !(insn_flags & FL_MODE26))
f5a1b0d2
NC
755 {
756 warning ("target CPU does not support APCS-26" );
757 target_flags |= ARM_FLAG_APCS_32;
758 }
759
6cfc7210 760 if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
f5a1b0d2
NC
761 {
762 warning ("target CPU does not support interworking" );
6cfc7210 763 target_flags &= ~ARM_FLAG_INTERWORK;
f5a1b0d2
NC
764 }
765
d5b7b3ae
RE
766 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
767 {
c725bd79 768 warning ("target CPU does not support THUMB instructions");
d5b7b3ae
RE
769 target_flags &= ~ARM_FLAG_THUMB;
770 }
771
772 if (TARGET_APCS_FRAME && TARGET_THUMB)
773 {
c725bd79 774 /* warning ("ignoring -mapcs-frame because -mthumb was used"); */
d5b7b3ae
RE
775 target_flags &= ~ARM_FLAG_APCS_FRAME;
776 }
d19fb8e3 777
d5b7b3ae
RE
778 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
779 from here where no function is being compiled currently. */
780 if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
781 && TARGET_ARM)
c725bd79 782 warning ("enabling backtrace support is only meaningful when compiling for the Thumb");
d5b7b3ae
RE
783
784 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
c725bd79 785 warning ("enabling callee interworking support is only meaningful when compiling for the Thumb");
d5b7b3ae
RE
786
787 if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
c725bd79 788 warning ("enabling caller interworking support is only meaningful when compiling for the Thumb");
d5b7b3ae 789
f5a1b0d2 790 /* If interworking is enabled then APCS-32 must be selected as well. */
6cfc7210 791 if (TARGET_INTERWORK)
f5a1b0d2 792 {
5895f793 793 if (!TARGET_APCS_32)
f5a1b0d2
NC
794 warning ("interworking forces APCS-32 to be used" );
795 target_flags |= ARM_FLAG_APCS_32;
796 }
797
5895f793 798 if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
f5a1b0d2
NC
799 {
800 warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
801 target_flags |= ARM_FLAG_APCS_FRAME;
802 }
aec3cfba 803
2b835d68
RE
804 if (TARGET_POKE_FUNCTION_NAME)
805 target_flags |= ARM_FLAG_APCS_FRAME;
aec3cfba 806
2b835d68 807 if (TARGET_APCS_REENT && flag_pic)
400500c4 808 error ("-fpic and -mapcs-reent are incompatible");
aec3cfba 809
2b835d68 810 if (TARGET_APCS_REENT)
f5a1b0d2 811 warning ("APCS reentrant code not supported. Ignored");
aec3cfba 812
d5b7b3ae
RE
813 /* If this target is normally configured to use APCS frames, warn if they
814 are turned off and debugging is turned on. */
815 if (TARGET_ARM
816 && write_symbols != NO_DEBUG
5895f793 817 && !TARGET_APCS_FRAME
d5b7b3ae
RE
818 && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
819 warning ("-g with -mno-apcs-frame may not give sensible debugging");
6cfc7210 820
32de079a
RE
821 /* If stack checking is disabled, we can use r10 as the PIC register,
822 which keeps r9 available. */
5b43fed1
RH
823 if (flag_pic)
824 arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
aec3cfba 825
2b835d68 826 if (TARGET_APCS_FLOAT)
c725bd79 827 warning ("passing floating point arguments in fp regs not yet supported");
f5a1b0d2 828
4912a07c 829 /* Initialize boolean versions of the flags, for use in the arm.md file. */
9b66ebb1
PB
830 arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
831 arm_arch4 = (insn_flags & FL_ARCH4) != 0;
832 arm_arch5 = (insn_flags & FL_ARCH5) != 0;
833 arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
834 arm_arch6 = (insn_flags & FL_ARCH6) != 0;
835 arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
836
837 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
838 arm_is_strong = (tune_flags & FL_STRONG) != 0;
839 thumb_code = (TARGET_ARM == 0);
840 arm_is_6_or_7 = (((tune_flags & (FL_MODE26 | FL_MODE32))
841 && !(tune_flags & FL_ARCH4))) != 0;
842 arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
843 arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
5a9335ef 844
5848830f
PB
845 if (target_abi_name)
846 {
847 for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
848 {
849 if (streq (arm_all_abis[i].name, target_abi_name))
850 {
851 arm_abi = arm_all_abis[i].abi_type;
852 break;
853 }
854 }
855 if (i == ARRAY_SIZE (arm_all_abis))
856 error ("invalid ABI option: -mabi=%s", target_abi_name);
857 }
858 else
c805f22e 859 arm_abi = ARM_DEFAULT_ABI;
5848830f
PB
860
861 if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
862 error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
863
864 if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
865 error ("iwmmxt abi requires an iwmmxt capable cpu");
6f7ebcbb 866
9b66ebb1
PB
867 arm_fp_model = ARM_FP_MODEL_UNKNOWN;
868 if (target_fpu_name == NULL && target_fpe_name != NULL)
9b6b54e2 869 {
9b66ebb1
PB
870 if (streq (target_fpe_name, "2"))
871 target_fpu_name = "fpe2";
872 else if (streq (target_fpe_name, "3"))
873 target_fpu_name = "fpe3";
874 else
875 error ("invalid floating point emulation option: -mfpe=%s",
876 target_fpe_name);
877 }
878 if (target_fpu_name != NULL)
879 {
880 /* The user specified a FPU. */
881 for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
882 {
883 if (streq (all_fpus[i].name, target_fpu_name))
884 {
885 arm_fpu_arch = all_fpus[i].fpu;
886 arm_fpu_tune = arm_fpu_arch;
887 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
888 break;
889 }
890 }
891 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
892 error ("invalid floating point option: -mfpu=%s", target_fpu_name);
9b6b54e2
NC
893 }
894 else
2b835d68 895 {
9b66ebb1
PB
896#ifdef FPUTYPE_DEFAULT
897 /* Use the default is it is specified for this platform. */
898 arm_fpu_arch = FPUTYPE_DEFAULT;
899 arm_fpu_tune = FPUTYPE_DEFAULT;
900#else
901 /* Pick one based on CPU type. */
902 if ((insn_flags & FL_VFP) != 0)
903 arm_fpu_arch = FPUTYPE_VFP;
904 else if (insn_flags & FL_CIRRUS)
905 arm_fpu_arch = FPUTYPE_MAVERICK;
906 else
29ad9694 907 arm_fpu_arch = FPUTYPE_FPA_EMU2;
9b66ebb1
PB
908#endif
909 if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
910 arm_fpu_tune = FPUTYPE_FPA;
2b835d68 911 else
9b66ebb1
PB
912 arm_fpu_tune = arm_fpu_arch;
913 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
914 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
915 abort ();
916 }
917
918 if (target_float_abi_name != NULL)
919 {
920 /* The user specified a FP ABI. */
921 for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
922 {
923 if (streq (all_float_abis[i].name, target_float_abi_name))
924 {
925 arm_float_abi = all_float_abis[i].abi_type;
926 break;
927 }
928 }
929 if (i == ARRAY_SIZE (all_float_abis))
930 error ("invalid floating point abi: -mfloat-abi=%s",
931 target_float_abi_name);
2b835d68 932 }
b111229a 933 else
9b6b54e2 934 {
9b66ebb1
PB
935 /* Use soft-float target flag. */
936 if (target_flags & ARM_FLAG_SOFT_FLOAT)
937 arm_float_abi = ARM_FLOAT_ABI_SOFT;
938 else
939 arm_float_abi = ARM_FLOAT_ABI_HARD;
9b6b54e2 940 }
9b66ebb1
PB
941
942 if (arm_float_abi == ARM_FLOAT_ABI_SOFTFP)
943 sorry ("-mfloat-abi=softfp");
944 /* If soft-float is specified then don't use FPU. */
945 if (TARGET_SOFT_FLOAT)
946 arm_fpu_arch = FPUTYPE_NONE;
aec3cfba 947
f5a1b0d2
NC
948 /* For arm2/3 there is no need to do any scheduling if there is only
949 a floating point emulator, or we are doing software floating-point. */
9b66ebb1
PB
950 if ((TARGET_SOFT_FLOAT
951 || arm_fpu_tune == FPUTYPE_FPA_EMU2
952 || arm_fpu_tune == FPUTYPE_FPA_EMU3)
ed0e6530 953 && (tune_flags & FL_MODE32) == 0)
f5a1b0d2 954 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
aec3cfba 955
cd2b33d0 956 arm_prgmode = TARGET_APCS_32 ? PROG_MODE_PROG32 : PROG_MODE_PROG26;
b355a481 957
5848830f
PB
958 /* Override the default structure alignment for AAPCS ABI. */
959 if (arm_abi == ARM_ABI_AAPCS)
960 arm_structure_size_boundary = 8;
961
b355a481
NC
962 if (structure_size_string != NULL)
963 {
964 int size = strtol (structure_size_string, NULL, 0);
5848830f
PB
965
966 if (size == 8 || size == 32
967 || (ARM_DOUBLEWORD_ALIGN && size == 64))
b355a481
NC
968 arm_structure_size_boundary = size;
969 else
5848830f
PB
970 warning ("structure size boundary can only be set to %s",
971 ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
b355a481 972 }
ed0e6530
PB
973
974 if (arm_pic_register_string != NULL)
975 {
5b43fed1 976 int pic_register = decode_reg_name (arm_pic_register_string);
e26053d1 977
5895f793 978 if (!flag_pic)
ed0e6530
PB
979 warning ("-mpic-register= is useless without -fpic");
980
ed0e6530 981 /* Prevent the user from choosing an obviously stupid PIC register. */
5b43fed1
RH
982 else if (pic_register < 0 || call_used_regs[pic_register]
983 || pic_register == HARD_FRAME_POINTER_REGNUM
984 || pic_register == STACK_POINTER_REGNUM
985 || pic_register >= PC_REGNUM)
c725bd79 986 error ("unable to use '%s' for PIC register", arm_pic_register_string);
ed0e6530
PB
987 else
988 arm_pic_register = pic_register;
989 }
d5b7b3ae
RE
990
991 if (TARGET_THUMB && flag_schedule_insns)
992 {
993 /* Don't warn since it's on by default in -O2. */
994 flag_schedule_insns = 0;
995 }
996
f5a1b0d2 997 if (optimize_size)
be03ccc9 998 {
577d6328
RE
999 /* There's some dispute as to whether this should be 1 or 2. However,
1000 experiments seem to show that in pathological cases a setting of
839a4992 1001 1 degrades less severely than a setting of 2. This could change if
577d6328
RE
1002 other parts of the compiler change their behavior. */
1003 arm_constant_limit = 1;
be03ccc9
NP
1004
1005 /* If optimizing for size, bump the number of instructions that we
d6b4baa4 1006 are prepared to conditionally execute (even on a StrongARM). */
be03ccc9
NP
1007 max_insns_skipped = 6;
1008 }
1009 else
1010 {
1011 /* For processors with load scheduling, it never costs more than
1012 2 cycles to load a constant, and the load scheduler may well
1013 reduce that to 1. */
1014 if (tune_flags & FL_LDSCHED)
1015 arm_constant_limit = 1;
1016
1017 /* On XScale the longer latency of a load makes it more difficult
1018 to achieve a good schedule, so it's faster to synthesize
d6b4baa4 1019 constants that can be done in two insns. */
be03ccc9
NP
1020 if (arm_tune_xscale)
1021 arm_constant_limit = 2;
1022
1023 /* StrongARM has early execution of branches, so a sequence
1024 that is worth skipping is shorter. */
1025 if (arm_is_strong)
1026 max_insns_skipped = 3;
1027 }
92a432f4
RE
1028
1029 /* Register global variables with the garbage collector. */
1030 arm_add_gc_roots ();
1031}
1032
1033static void
e32bac5b 1034arm_add_gc_roots (void)
92a432f4 1035{
c7319d87
RE
1036 gcc_obstack_init(&minipool_obstack);
1037 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
2b835d68 1038}
cce8749e 1039\f
6d3d9133
NC
1040/* A table of known ARM exception types.
1041 For use with the interrupt function attribute. */
1042
1043typedef struct
1044{
8b60264b
KG
1045 const char *const arg;
1046 const unsigned long return_value;
6d3d9133
NC
1047}
1048isr_attribute_arg;
1049
8b60264b 1050static const isr_attribute_arg isr_attribute_args [] =
6d3d9133
NC
1051{
1052 { "IRQ", ARM_FT_ISR },
1053 { "irq", ARM_FT_ISR },
1054 { "FIQ", ARM_FT_FIQ },
1055 { "fiq", ARM_FT_FIQ },
1056 { "ABORT", ARM_FT_ISR },
1057 { "abort", ARM_FT_ISR },
1058 { "ABORT", ARM_FT_ISR },
1059 { "abort", ARM_FT_ISR },
1060 { "UNDEF", ARM_FT_EXCEPTION },
1061 { "undef", ARM_FT_EXCEPTION },
1062 { "SWI", ARM_FT_EXCEPTION },
1063 { "swi", ARM_FT_EXCEPTION },
1064 { NULL, ARM_FT_NORMAL }
1065};
1066
1067/* Returns the (interrupt) function type of the current
1068 function, or ARM_FT_UNKNOWN if the type cannot be determined. */
1069
1070static unsigned long
e32bac5b 1071arm_isr_value (tree argument)
6d3d9133 1072{
8b60264b 1073 const isr_attribute_arg * ptr;
1d6e90ac 1074 const char * arg;
6d3d9133
NC
1075
1076 /* No argument - default to IRQ. */
1077 if (argument == NULL_TREE)
1078 return ARM_FT_ISR;
1079
1080 /* Get the value of the argument. */
1081 if (TREE_VALUE (argument) == NULL_TREE
1082 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1083 return ARM_FT_UNKNOWN;
1084
1085 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1086
1087 /* Check it against the list of known arguments. */
5a9335ef 1088 for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1d6e90ac
NC
1089 if (streq (arg, ptr->arg))
1090 return ptr->return_value;
6d3d9133 1091
05713b80 1092 /* An unrecognized interrupt type. */
6d3d9133
NC
1093 return ARM_FT_UNKNOWN;
1094}
1095
1096/* Computes the type of the current function. */
1097
1098static unsigned long
e32bac5b 1099arm_compute_func_type (void)
6d3d9133
NC
1100{
1101 unsigned long type = ARM_FT_UNKNOWN;
1102 tree a;
1103 tree attr;
1104
1105 if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
1106 abort ();
1107
1108 /* Decide if the current function is volatile. Such functions
1109 never return, and many memory cycles can be saved by not storing
1110 register values that will never be needed again. This optimization
1111 was added to speed up context switching in a kernel application. */
1112 if (optimize > 0
1113 && current_function_nothrow
1114 && TREE_THIS_VOLATILE (current_function_decl))
1115 type |= ARM_FT_VOLATILE;
1116
1117 if (current_function_needs_context)
1118 type |= ARM_FT_NESTED;
1119
91d231cb 1120 attr = DECL_ATTRIBUTES (current_function_decl);
6d3d9133
NC
1121
1122 a = lookup_attribute ("naked", attr);
1123 if (a != NULL_TREE)
1124 type |= ARM_FT_NAKED;
1125
1126 if (cfun->machine->eh_epilogue_sp_ofs != NULL_RTX)
1127 type |= ARM_FT_EXCEPTION_HANDLER;
1128 else
1129 {
1130 a = lookup_attribute ("isr", attr);
1131 if (a == NULL_TREE)
1132 a = lookup_attribute ("interrupt", attr);
1133
1134 if (a == NULL_TREE)
1135 type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1136 else
1137 type |= arm_isr_value (TREE_VALUE (a));
1138 }
1139
1140 return type;
1141}
1142
1143/* Returns the type of the current function. */
1144
1145unsigned long
e32bac5b 1146arm_current_func_type (void)
6d3d9133
NC
1147{
1148 if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1149 cfun->machine->func_type = arm_compute_func_type ();
1150
1151 return cfun->machine->func_type;
1152}
1153\f
a72d4945
RE
1154/* Return 1 if it is possible to return using a single instruction.
1155 If SIBLING is non-null, this is a test for a return before a sibling
1156 call. SIBLING is the call insn, so we can examine its register usage. */
6d3d9133 1157
ff9940b0 1158int
a72d4945 1159use_return_insn (int iscond, rtx sibling)
ff9940b0
RE
1160{
1161 int regno;
9b598fa0 1162 unsigned int func_type;
d5db54a1 1163 unsigned long saved_int_regs;
a72d4945 1164 unsigned HOST_WIDE_INT stack_adjust;
5848830f 1165 arm_stack_offsets *offsets;
ff9940b0 1166
d5b7b3ae 1167 /* Never use a return instruction before reload has run. */
6d3d9133
NC
1168 if (!reload_completed)
1169 return 0;
efc2515b 1170
9b598fa0
RE
1171 func_type = arm_current_func_type ();
1172
3a7731fd
PB
1173 /* Naked functions and volatile functions need special
1174 consideration. */
1175 if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
6d3d9133 1176 return 0;
06bea5aa
NC
1177
1178 /* So do interrupt functions that use the frame pointer. */
1179 if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1180 return 0;
a72d4945 1181
5848830f
PB
1182 offsets = arm_get_frame_offsets ();
1183 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
a72d4945 1184
6d3d9133
NC
1185 /* As do variadic functions. */
1186 if (current_function_pretend_args_size
3cb66fd7 1187 || cfun->machine->uses_anonymous_args
699a4925 1188 /* Or if the function calls __builtin_eh_return () */
6d3d9133 1189 || ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
699a4925
RE
1190 /* Or if the function calls alloca */
1191 || current_function_calls_alloca
a72d4945
RE
1192 /* Or if there is a stack adjustment. However, if the stack pointer
1193 is saved on the stack, we can use a pre-incrementing stack load. */
1194 || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
ff9940b0
RE
1195 return 0;
1196
d5db54a1
RE
1197 saved_int_regs = arm_compute_save_reg_mask ();
1198
a72d4945
RE
1199 /* Unfortunately, the insn
1200
1201 ldmib sp, {..., sp, ...}
1202
1203 triggers a bug on most SA-110 based devices, such that the stack
1204 pointer won't be correctly restored if the instruction takes a
839a4992 1205 page fault. We work around this problem by popping r3 along with
a72d4945
RE
1206 the other registers, since that is never slower than executing
1207 another instruction.
1208
1209 We test for !arm_arch5 here, because code for any architecture
1210 less than this could potentially be run on one of the buggy
1211 chips. */
1212 if (stack_adjust == 4 && !arm_arch5)
1213 {
1214 /* Validate that r3 is a call-clobbered register (always true in
d6b4baa4 1215 the default abi) ... */
a72d4945
RE
1216 if (!call_used_regs[3])
1217 return 0;
1218
1219 /* ... that it isn't being used for a return value (always true
1220 until we implement return-in-regs), or for a tail-call
d6b4baa4 1221 argument ... */
a72d4945
RE
1222 if (sibling)
1223 {
1224 if (GET_CODE (sibling) != CALL_INSN)
1225 abort ();
1226
1227 if (find_regno_fusage (sibling, USE, 3))
1228 return 0;
1229 }
1230
1231 /* ... and that there are no call-saved registers in r0-r2
1232 (always true in the default ABI). */
1233 if (saved_int_regs & 0x7)
1234 return 0;
1235 }
1236
b111229a 1237 /* Can't be done if interworking with Thumb, and any registers have been
d5db54a1
RE
1238 stacked. */
1239 if (TARGET_INTERWORK && saved_int_regs != 0)
b36ba79f 1240 return 0;
d5db54a1
RE
1241
1242 /* On StrongARM, conditional returns are expensive if they aren't
1243 taken and multiple registers have been stacked. */
1244 if (iscond && arm_is_strong)
6ed30148 1245 {
d5db54a1
RE
1246 /* Conditional return when just the LR is stored is a simple
1247 conditional-load instruction, that's not expensive. */
1248 if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1249 return 0;
6ed30148
RE
1250
1251 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
b111229a 1252 return 0;
6ed30148 1253 }
d5db54a1
RE
1254
1255 /* If there are saved registers but the LR isn't saved, then we need
1256 two instructions for the return. */
1257 if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1258 return 0;
1259
3b684012 1260 /* Can't be done if any of the FPA regs are pushed,
6d3d9133 1261 since this also requires an insn. */
9b66ebb1
PB
1262 if (TARGET_HARD_FLOAT && TARGET_FPA)
1263 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1264 if (regs_ever_live[regno] && !call_used_regs[regno])
1265 return 0;
1266
1267 /* Likewise VFP regs. */
1268 if (TARGET_HARD_FLOAT && TARGET_VFP)
1269 for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
5895f793 1270 if (regs_ever_live[regno] && !call_used_regs[regno])
d5b7b3ae 1271 return 0;
ff9940b0 1272
5a9335ef
NC
1273 if (TARGET_REALLY_IWMMXT)
1274 for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1275 if (regs_ever_live[regno] && ! call_used_regs [regno])
1276 return 0;
1277
ff9940b0
RE
1278 return 1;
1279}
1280
cce8749e
CH
1281/* Return TRUE if int I is a valid immediate ARM constant. */
1282
1283int
e32bac5b 1284const_ok_for_arm (HOST_WIDE_INT i)
cce8749e 1285{
30cf4896 1286 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
cce8749e 1287
56636818
JL
1288 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1289 be all zero, or all one. */
30cf4896
KG
1290 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1291 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1292 != ((~(unsigned HOST_WIDE_INT) 0)
1293 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
56636818
JL
1294 return FALSE;
1295
e2c671ba
RE
1296 /* Fast return for 0 and powers of 2 */
1297 if ((i & (i - 1)) == 0)
1298 return TRUE;
1299
cce8749e
CH
1300 do
1301 {
30cf4896 1302 if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
f3bb6135 1303 return TRUE;
abaa26e5 1304 mask =
30cf4896
KG
1305 (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1306 >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
ebe413e5
NC
1307 }
1308 while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
cce8749e 1309
f3bb6135
RE
1310 return FALSE;
1311}
cce8749e 1312
6354dc9b 1313/* Return true if I is a valid constant for the operation CODE. */
74bbc178 1314static int
e32bac5b 1315const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
e2c671ba
RE
1316{
1317 if (const_ok_for_arm (i))
1318 return 1;
1319
1320 switch (code)
1321 {
1322 case PLUS:
1323 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1324
1325 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
1326 case XOR:
1327 case IOR:
1328 return 0;
1329
1330 case AND:
1331 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1332
1333 default:
1334 abort ();
1335 }
1336}
1337
1338/* Emit a sequence of insns to handle a large constant.
1339 CODE is the code of the operation required, it can be any of SET, PLUS,
1340 IOR, AND, XOR, MINUS;
1341 MODE is the mode in which the operation is being performed;
1342 VAL is the integer to operate on;
1343 SOURCE is the other operand (a register, or a null-pointer for SET);
1344 SUBTARGETS means it is safe to create scratch registers if that will
2b835d68
RE
1345 either produce a simpler sequence, or we will want to cse the values.
1346 Return value is the number of insns emitted. */
e2c671ba
RE
1347
1348int
a406f566 1349arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
e32bac5b 1350 HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
2b835d68 1351{
a406f566
MM
1352 rtx cond;
1353
1354 if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1355 cond = COND_EXEC_TEST (PATTERN (insn));
1356 else
1357 cond = NULL_RTX;
1358
2b835d68
RE
1359 if (subtargets || code == SET
1360 || (GET_CODE (target) == REG && GET_CODE (source) == REG
1361 && REGNO (target) != REGNO (source)))
1362 {
4b632bf1 1363 /* After arm_reorg has been called, we can't fix up expensive
05713b80 1364 constants by pushing them into memory so we must synthesize
4b632bf1
RE
1365 them in-line, regardless of the cost. This is only likely to
1366 be more costly on chips that have load delay slots and we are
1367 compiling without running the scheduler (so no splitting
aec3cfba
NC
1368 occurred before the final instruction emission).
1369
1370 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
aec3cfba 1371 */
5895f793 1372 if (!after_arm_reorg
a406f566
MM
1373 && !cond
1374 && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1375 1, 0)
4b632bf1 1376 > arm_constant_limit + (code != SET)))
2b835d68
RE
1377 {
1378 if (code == SET)
1379 {
1380 /* Currently SET is the only monadic value for CODE, all
1381 the rest are diadic. */
43cffd11 1382 emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
2b835d68
RE
1383 return 1;
1384 }
1385 else
1386 {
1387 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1388
43cffd11 1389 emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
2b835d68
RE
1390 /* For MINUS, the value is subtracted from, since we never
1391 have subtraction of a constant. */
1392 if (code == MINUS)
43cffd11 1393 emit_insn (gen_rtx_SET (VOIDmode, target,
d5b7b3ae 1394 gen_rtx_MINUS (mode, temp, source)));
2b835d68 1395 else
43cffd11 1396 emit_insn (gen_rtx_SET (VOIDmode, target,
1c563bed 1397 gen_rtx_fmt_ee (code, mode, source, temp)));
2b835d68
RE
1398 return 2;
1399 }
1400 }
1401 }
1402
a406f566
MM
1403 return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1404 1);
2b835d68
RE
1405}
1406
ceebdb09 1407static int
e32bac5b 1408count_insns_for_constant (HOST_WIDE_INT remainder, int i)
ceebdb09
PB
1409{
1410 HOST_WIDE_INT temp1;
1411 int num_insns = 0;
1412 do
1413 {
1414 int end;
1415
1416 if (i <= 0)
1417 i += 32;
1418 if (remainder & (3 << (i - 2)))
1419 {
1420 end = i - 8;
1421 if (end < 0)
1422 end += 32;
1423 temp1 = remainder & ((0x0ff << end)
1424 | ((i < end) ? (0xff >> (32 - end)) : 0));
1425 remainder &= ~temp1;
1426 num_insns++;
1427 i -= 6;
1428 }
1429 i -= 2;
1430 } while (remainder);
1431 return num_insns;
1432}
1433
a406f566
MM
1434/* Emit an instruction with the indicated PATTERN. If COND is
1435 non-NULL, conditionalize the execution of the instruction on COND
1436 being true. */
1437
1438static void
1439emit_constant_insn (rtx cond, rtx pattern)
1440{
1441 if (cond)
1442 pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1443 emit_insn (pattern);
1444}
1445
2b835d68
RE
1446/* As above, but extra parameter GENERATE which, if clear, suppresses
1447 RTL generation. */
1d6e90ac 1448
d5b7b3ae 1449static int
a406f566 1450arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
e32bac5b
RE
1451 HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1452 int generate)
e2c671ba 1453{
e2c671ba
RE
1454 int can_invert = 0;
1455 int can_negate = 0;
1456 int can_negate_initial = 0;
1457 int can_shift = 0;
1458 int i;
1459 int num_bits_set = 0;
1460 int set_sign_bit_copies = 0;
1461 int clear_sign_bit_copies = 0;
1462 int clear_zero_bit_copies = 0;
1463 int set_zero_bit_copies = 0;
1464 int insns = 0;
e2c671ba 1465 unsigned HOST_WIDE_INT temp1, temp2;
30cf4896 1466 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
e2c671ba 1467
d5b7b3ae 1468 /* Find out which operations are safe for a given CODE. Also do a quick
e2c671ba
RE
1469 check for degenerate cases; these can occur when DImode operations
1470 are split. */
1471 switch (code)
1472 {
1473 case SET:
1474 can_invert = 1;
1475 can_shift = 1;
1476 can_negate = 1;
1477 break;
1478
1479 case PLUS:
1480 can_negate = 1;
1481 can_negate_initial = 1;
1482 break;
1483
1484 case IOR:
30cf4896 1485 if (remainder == 0xffffffff)
e2c671ba 1486 {
2b835d68 1487 if (generate)
a406f566
MM
1488 emit_constant_insn (cond,
1489 gen_rtx_SET (VOIDmode, target,
1490 GEN_INT (ARM_SIGN_EXTEND (val))));
e2c671ba
RE
1491 return 1;
1492 }
1493 if (remainder == 0)
1494 {
1495 if (reload_completed && rtx_equal_p (target, source))
1496 return 0;
2b835d68 1497 if (generate)
a406f566
MM
1498 emit_constant_insn (cond,
1499 gen_rtx_SET (VOIDmode, target, source));
e2c671ba
RE
1500 return 1;
1501 }
1502 break;
1503
1504 case AND:
1505 if (remainder == 0)
1506 {
2b835d68 1507 if (generate)
a406f566
MM
1508 emit_constant_insn (cond,
1509 gen_rtx_SET (VOIDmode, target, const0_rtx));
e2c671ba
RE
1510 return 1;
1511 }
30cf4896 1512 if (remainder == 0xffffffff)
e2c671ba
RE
1513 {
1514 if (reload_completed && rtx_equal_p (target, source))
1515 return 0;
2b835d68 1516 if (generate)
a406f566
MM
1517 emit_constant_insn (cond,
1518 gen_rtx_SET (VOIDmode, target, source));
e2c671ba
RE
1519 return 1;
1520 }
1521 can_invert = 1;
1522 break;
1523
1524 case XOR:
1525 if (remainder == 0)
1526 {
1527 if (reload_completed && rtx_equal_p (target, source))
1528 return 0;
2b835d68 1529 if (generate)
a406f566
MM
1530 emit_constant_insn (cond,
1531 gen_rtx_SET (VOIDmode, target, source));
e2c671ba
RE
1532 return 1;
1533 }
30cf4896 1534 if (remainder == 0xffffffff)
e2c671ba 1535 {
2b835d68 1536 if (generate)
a406f566
MM
1537 emit_constant_insn (cond,
1538 gen_rtx_SET (VOIDmode, target,
1539 gen_rtx_NOT (mode, source)));
e2c671ba
RE
1540 return 1;
1541 }
1542
1543 /* We don't know how to handle this yet below. */
1544 abort ();
1545
1546 case MINUS:
1547 /* We treat MINUS as (val - source), since (source - val) is always
1548 passed as (source + (-val)). */
1549 if (remainder == 0)
1550 {
2b835d68 1551 if (generate)
a406f566
MM
1552 emit_constant_insn (cond,
1553 gen_rtx_SET (VOIDmode, target,
1554 gen_rtx_NEG (mode, source)));
e2c671ba
RE
1555 return 1;
1556 }
1557 if (const_ok_for_arm (val))
1558 {
2b835d68 1559 if (generate)
a406f566
MM
1560 emit_constant_insn (cond,
1561 gen_rtx_SET (VOIDmode, target,
1562 gen_rtx_MINUS (mode, GEN_INT (val),
1563 source)));
e2c671ba
RE
1564 return 1;
1565 }
1566 can_negate = 1;
1567
1568 break;
1569
1570 default:
1571 abort ();
1572 }
1573
6354dc9b 1574 /* If we can do it in one insn get out quickly. */
e2c671ba
RE
1575 if (const_ok_for_arm (val)
1576 || (can_negate_initial && const_ok_for_arm (-val))
1577 || (can_invert && const_ok_for_arm (~val)))
1578 {
2b835d68 1579 if (generate)
a406f566
MM
1580 emit_constant_insn (cond,
1581 gen_rtx_SET (VOIDmode, target,
1582 (source
1583 ? gen_rtx_fmt_ee (code, mode, source,
1584 GEN_INT (val))
1585 : GEN_INT (val))));
e2c671ba
RE
1586 return 1;
1587 }
1588
e2c671ba 1589 /* Calculate a few attributes that may be useful for specific
6354dc9b 1590 optimizations. */
e2c671ba
RE
1591 for (i = 31; i >= 0; i--)
1592 {
1593 if ((remainder & (1 << i)) == 0)
1594 clear_sign_bit_copies++;
1595 else
1596 break;
1597 }
1598
1599 for (i = 31; i >= 0; i--)
1600 {
1601 if ((remainder & (1 << i)) != 0)
1602 set_sign_bit_copies++;
1603 else
1604 break;
1605 }
1606
1607 for (i = 0; i <= 31; i++)
1608 {
1609 if ((remainder & (1 << i)) == 0)
1610 clear_zero_bit_copies++;
1611 else
1612 break;
1613 }
1614
1615 for (i = 0; i <= 31; i++)
1616 {
1617 if ((remainder & (1 << i)) != 0)
1618 set_zero_bit_copies++;
1619 else
1620 break;
1621 }
1622
1623 switch (code)
1624 {
1625 case SET:
1626 /* See if we can do this by sign_extending a constant that is known
1627 to be negative. This is a good, way of doing it, since the shift
1628 may well merge into a subsequent insn. */
1629 if (set_sign_bit_copies > 1)
1630 {
1631 if (const_ok_for_arm
1632 (temp1 = ARM_SIGN_EXTEND (remainder
1633 << (set_sign_bit_copies - 1))))
1634 {
2b835d68
RE
1635 if (generate)
1636 {
d499463f 1637 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
a406f566
MM
1638 emit_constant_insn (cond,
1639 gen_rtx_SET (VOIDmode, new_src,
1640 GEN_INT (temp1)));
1641 emit_constant_insn (cond,
1642 gen_ashrsi3 (target, new_src,
1643 GEN_INT (set_sign_bit_copies - 1)));
2b835d68 1644 }
e2c671ba
RE
1645 return 2;
1646 }
1647 /* For an inverted constant, we will need to set the low bits,
1648 these will be shifted out of harm's way. */
1649 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1650 if (const_ok_for_arm (~temp1))
1651 {
2b835d68
RE
1652 if (generate)
1653 {
d499463f 1654 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
a406f566
MM
1655 emit_constant_insn (cond,
1656 gen_rtx_SET (VOIDmode, new_src,
1657 GEN_INT (temp1)));
1658 emit_constant_insn (cond,
1659 gen_ashrsi3 (target, new_src,
1660 GEN_INT (set_sign_bit_copies - 1)));
2b835d68 1661 }
e2c671ba
RE
1662 return 2;
1663 }
1664 }
1665
1666 /* See if we can generate this by setting the bottom (or the top)
1667 16 bits, and then shifting these into the other half of the
1668 word. We only look for the simplest cases, to do more would cost
1669 too much. Be careful, however, not to generate this when the
1670 alternative would take fewer insns. */
30cf4896 1671 if (val & 0xffff0000)
e2c671ba 1672 {
30cf4896 1673 temp1 = remainder & 0xffff0000;
e2c671ba
RE
1674 temp2 = remainder & 0x0000ffff;
1675
6354dc9b 1676 /* Overlaps outside this range are best done using other methods. */
e2c671ba
RE
1677 for (i = 9; i < 24; i++)
1678 {
30cf4896 1679 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
5895f793 1680 && !const_ok_for_arm (temp2))
e2c671ba 1681 {
d499463f
RE
1682 rtx new_src = (subtargets
1683 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1684 : target);
a406f566 1685 insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2b835d68 1686 source, subtargets, generate);
e2c671ba 1687 source = new_src;
2b835d68 1688 if (generate)
a406f566
MM
1689 emit_constant_insn
1690 (cond,
1691 gen_rtx_SET
1692 (VOIDmode, target,
1693 gen_rtx_IOR (mode,
1694 gen_rtx_ASHIFT (mode, source,
1695 GEN_INT (i)),
1696 source)));
e2c671ba
RE
1697 return insns + 1;
1698 }
1699 }
1700
6354dc9b 1701 /* Don't duplicate cases already considered. */
e2c671ba
RE
1702 for (i = 17; i < 24; i++)
1703 {
1704 if (((temp1 | (temp1 >> i)) == remainder)
5895f793 1705 && !const_ok_for_arm (temp1))
e2c671ba 1706 {
d499463f
RE
1707 rtx new_src = (subtargets
1708 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1709 : target);
a406f566 1710 insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2b835d68 1711 source, subtargets, generate);
e2c671ba 1712 source = new_src;
2b835d68 1713 if (generate)
a406f566
MM
1714 emit_constant_insn
1715 (cond,
1716 gen_rtx_SET (VOIDmode, target,
43cffd11
RE
1717 gen_rtx_IOR
1718 (mode,
1719 gen_rtx_LSHIFTRT (mode, source,
1720 GEN_INT (i)),
1721 source)));
e2c671ba
RE
1722 return insns + 1;
1723 }
1724 }
1725 }
1726 break;
1727
1728 case IOR:
1729 case XOR:
7b64da89
RE
1730 /* If we have IOR or XOR, and the constant can be loaded in a
1731 single instruction, and we can find a temporary to put it in,
e2c671ba
RE
1732 then this can be done in two instructions instead of 3-4. */
1733 if (subtargets
d499463f 1734 /* TARGET can't be NULL if SUBTARGETS is 0 */
5895f793 1735 || (reload_completed && !reg_mentioned_p (target, source)))
e2c671ba 1736 {
5895f793 1737 if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
e2c671ba 1738 {
2b835d68
RE
1739 if (generate)
1740 {
1741 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
e2c671ba 1742
a406f566
MM
1743 emit_constant_insn (cond,
1744 gen_rtx_SET (VOIDmode, sub,
1745 GEN_INT (val)));
1746 emit_constant_insn (cond,
1747 gen_rtx_SET (VOIDmode, target,
1748 gen_rtx_fmt_ee (code, mode,
1749 source, sub)));
2b835d68 1750 }
e2c671ba
RE
1751 return 2;
1752 }
1753 }
1754
1755 if (code == XOR)
1756 break;
1757
1758 if (set_sign_bit_copies > 8
1759 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1760 {
2b835d68
RE
1761 if (generate)
1762 {
1763 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1764 rtx shift = GEN_INT (set_sign_bit_copies);
1765
a406f566
MM
1766 emit_constant_insn
1767 (cond,
1768 gen_rtx_SET (VOIDmode, sub,
1769 gen_rtx_NOT (mode,
1770 gen_rtx_ASHIFT (mode,
1771 source,
1772 shift))));
1773 emit_constant_insn
1774 (cond,
1775 gen_rtx_SET (VOIDmode, target,
1776 gen_rtx_NOT (mode,
1777 gen_rtx_LSHIFTRT (mode, sub,
1778 shift))));
2b835d68 1779 }
e2c671ba
RE
1780 return 2;
1781 }
1782
1783 if (set_zero_bit_copies > 8
1784 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1785 {
2b835d68
RE
1786 if (generate)
1787 {
1788 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1789 rtx shift = GEN_INT (set_zero_bit_copies);
1790
a406f566
MM
1791 emit_constant_insn
1792 (cond,
1793 gen_rtx_SET (VOIDmode, sub,
1794 gen_rtx_NOT (mode,
1795 gen_rtx_LSHIFTRT (mode,
1796 source,
1797 shift))));
1798 emit_constant_insn
1799 (cond,
1800 gen_rtx_SET (VOIDmode, target,
1801 gen_rtx_NOT (mode,
1802 gen_rtx_ASHIFT (mode, sub,
1803 shift))));
2b835d68 1804 }
e2c671ba
RE
1805 return 2;
1806 }
1807
5895f793 1808 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
e2c671ba 1809 {
2b835d68
RE
1810 if (generate)
1811 {
1812 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
a406f566
MM
1813 emit_constant_insn (cond,
1814 gen_rtx_SET (VOIDmode, sub,
1815 gen_rtx_NOT (mode, source)));
2b835d68
RE
1816 source = sub;
1817 if (subtargets)
1818 sub = gen_reg_rtx (mode);
a406f566
MM
1819 emit_constant_insn (cond,
1820 gen_rtx_SET (VOIDmode, sub,
1821 gen_rtx_AND (mode, source,
1822 GEN_INT (temp1))));
1823 emit_constant_insn (cond,
1824 gen_rtx_SET (VOIDmode, target,
1825 gen_rtx_NOT (mode, sub)));
2b835d68 1826 }
e2c671ba
RE
1827 return 3;
1828 }
1829 break;
1830
1831 case AND:
1832 /* See if two shifts will do 2 or more insn's worth of work. */
1833 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1834 {
30cf4896 1835 HOST_WIDE_INT shift_mask = ((0xffffffff
e2c671ba 1836 << (32 - clear_sign_bit_copies))
30cf4896 1837 & 0xffffffff);
e2c671ba 1838
30cf4896 1839 if ((remainder | shift_mask) != 0xffffffff)
e2c671ba 1840 {
2b835d68
RE
1841 if (generate)
1842 {
d499463f 1843 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
a406f566
MM
1844 insns = arm_gen_constant (AND, mode, cond,
1845 remainder | shift_mask,
d499463f
RE
1846 new_src, source, subtargets, 1);
1847 source = new_src;
2b835d68
RE
1848 }
1849 else
d499463f
RE
1850 {
1851 rtx targ = subtargets ? NULL_RTX : target;
a406f566
MM
1852 insns = arm_gen_constant (AND, mode, cond,
1853 remainder | shift_mask,
d499463f
RE
1854 targ, source, subtargets, 0);
1855 }
2b835d68
RE
1856 }
1857
1858 if (generate)
1859 {
d499463f
RE
1860 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1861 rtx shift = GEN_INT (clear_sign_bit_copies);
1862
1863 emit_insn (gen_ashlsi3 (new_src, source, shift));
1864 emit_insn (gen_lshrsi3 (target, new_src, shift));
e2c671ba
RE
1865 }
1866
e2c671ba
RE
1867 return insns + 2;
1868 }
1869
1870 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
1871 {
1872 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
e2c671ba 1873
30cf4896 1874 if ((remainder | shift_mask) != 0xffffffff)
e2c671ba 1875 {
2b835d68
RE
1876 if (generate)
1877 {
d499463f
RE
1878 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1879
a406f566
MM
1880 insns = arm_gen_constant (AND, mode, cond,
1881 remainder | shift_mask,
d499463f
RE
1882 new_src, source, subtargets, 1);
1883 source = new_src;
2b835d68
RE
1884 }
1885 else
d499463f
RE
1886 {
1887 rtx targ = subtargets ? NULL_RTX : target;
1888
a406f566
MM
1889 insns = arm_gen_constant (AND, mode, cond,
1890 remainder | shift_mask,
d499463f
RE
1891 targ, source, subtargets, 0);
1892 }
2b835d68
RE
1893 }
1894
1895 if (generate)
1896 {
d499463f
RE
1897 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1898 rtx shift = GEN_INT (clear_zero_bit_copies);
1899
1900 emit_insn (gen_lshrsi3 (new_src, source, shift));
1901 emit_insn (gen_ashlsi3 (target, new_src, shift));
e2c671ba
RE
1902 }
1903
e2c671ba
RE
1904 return insns + 2;
1905 }
1906
1907 break;
1908
1909 default:
1910 break;
1911 }
1912
1913 for (i = 0; i < 32; i++)
1914 if (remainder & (1 << i))
1915 num_bits_set++;
1916
1917 if (code == AND || (can_invert && num_bits_set > 16))
30cf4896 1918 remainder = (~remainder) & 0xffffffff;
e2c671ba 1919 else if (code == PLUS && num_bits_set > 16)
30cf4896 1920 remainder = (-remainder) & 0xffffffff;
e2c671ba
RE
1921 else
1922 {
1923 can_invert = 0;
1924 can_negate = 0;
1925 }
1926
1927 /* Now try and find a way of doing the job in either two or three
1928 instructions.
1929 We start by looking for the largest block of zeros that are aligned on
1930 a 2-bit boundary, we then fill up the temps, wrapping around to the
1931 top of the word when we drop off the bottom.
6354dc9b 1932 In the worst case this code should produce no more than four insns. */
e2c671ba
RE
1933 {
1934 int best_start = 0;
1935 int best_consecutive_zeros = 0;
1936
1937 for (i = 0; i < 32; i += 2)
1938 {
1939 int consecutive_zeros = 0;
1940
5895f793 1941 if (!(remainder & (3 << i)))
e2c671ba 1942 {
5895f793 1943 while ((i < 32) && !(remainder & (3 << i)))
e2c671ba
RE
1944 {
1945 consecutive_zeros += 2;
1946 i += 2;
1947 }
1948 if (consecutive_zeros > best_consecutive_zeros)
1949 {
1950 best_consecutive_zeros = consecutive_zeros;
1951 best_start = i - consecutive_zeros;
1952 }
1953 i -= 2;
1954 }
1955 }
1956
ceebdb09
PB
1957 /* So long as it won't require any more insns to do so, it's
1958 desirable to emit a small constant (in bits 0...9) in the last
1959 insn. This way there is more chance that it can be combined with
1960 a later addressing insn to form a pre-indexed load or store
1961 operation. Consider:
1962
1963 *((volatile int *)0xe0000100) = 1;
1964 *((volatile int *)0xe0000110) = 2;
1965
1966 We want this to wind up as:
1967
1968 mov rA, #0xe0000000
1969 mov rB, #1
1970 str rB, [rA, #0x100]
1971 mov rB, #2
1972 str rB, [rA, #0x110]
1973
1974 rather than having to synthesize both large constants from scratch.
1975
1976 Therefore, we calculate how many insns would be required to emit
1977 the constant starting from `best_start', and also starting from
1978 zero (ie with bit 31 first to be output). If `best_start' doesn't
1979 yield a shorter sequence, we may as well use zero. */
1980 if (best_start != 0
1981 && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
1982 && (count_insns_for_constant (remainder, 0) <=
1983 count_insns_for_constant (remainder, best_start)))
1984 best_start = 0;
1985
1986 /* Now start emitting the insns. */
e2c671ba
RE
1987 i = best_start;
1988 do
1989 {
1990 int end;
1991
1992 if (i <= 0)
1993 i += 32;
1994 if (remainder & (3 << (i - 2)))
1995 {
1996 end = i - 8;
1997 if (end < 0)
1998 end += 32;
1999 temp1 = remainder & ((0x0ff << end)
2000 | ((i < end) ? (0xff >> (32 - end)) : 0));
2001 remainder &= ~temp1;
2002
d499463f 2003 if (generate)
e2c671ba 2004 {
9503f3d1
RH
2005 rtx new_src, temp1_rtx;
2006
2007 if (code == SET || code == MINUS)
2008 {
2009 new_src = (subtargets ? gen_reg_rtx (mode) : target);
96ae8197 2010 if (can_invert && code != MINUS)
9503f3d1
RH
2011 temp1 = ~temp1;
2012 }
2013 else
2014 {
96ae8197 2015 if (remainder && subtargets)
9503f3d1 2016 new_src = gen_reg_rtx (mode);
96ae8197
NC
2017 else
2018 new_src = target;
9503f3d1
RH
2019 if (can_invert)
2020 temp1 = ~temp1;
2021 else if (can_negate)
2022 temp1 = -temp1;
2023 }
2024
2025 temp1 = trunc_int_for_mode (temp1, mode);
2026 temp1_rtx = GEN_INT (temp1);
d499463f
RE
2027
2028 if (code == SET)
9503f3d1 2029 ;
d499463f 2030 else if (code == MINUS)
9503f3d1 2031 temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
d499463f 2032 else
9503f3d1
RH
2033 temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2034
a406f566
MM
2035 emit_constant_insn (cond,
2036 gen_rtx_SET (VOIDmode, new_src,
2037 temp1_rtx));
d499463f 2038 source = new_src;
e2c671ba
RE
2039 }
2040
d499463f
RE
2041 if (code == SET)
2042 {
2043 can_invert = 0;
2044 code = PLUS;
2045 }
2046 else if (code == MINUS)
2047 code = PLUS;
2048
e2c671ba 2049 insns++;
e2c671ba
RE
2050 i -= 6;
2051 }
2052 i -= 2;
1d6e90ac
NC
2053 }
2054 while (remainder);
e2c671ba 2055 }
1d6e90ac 2056
e2c671ba
RE
2057 return insns;
2058}
2059
bd9c7e23
RE
2060/* Canonicalize a comparison so that we are more likely to recognize it.
2061 This can be done for a few constant compares, where we can make the
2062 immediate value easier to load. */
1d6e90ac 2063
bd9c7e23 2064enum rtx_code
e32bac5b 2065arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
bd9c7e23 2066{
ad076f4e 2067 unsigned HOST_WIDE_INT i = INTVAL (*op1);
bd9c7e23
RE
2068
2069 switch (code)
2070 {
2071 case EQ:
2072 case NE:
2073 return code;
2074
2075 case GT:
2076 case LE:
30cf4896 2077 if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
5895f793 2078 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
bd9c7e23 2079 {
5895f793 2080 *op1 = GEN_INT (i + 1);
bd9c7e23
RE
2081 return code == GT ? GE : LT;
2082 }
2083 break;
2084
2085 case GE:
2086 case LT:
30cf4896 2087 if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
5895f793 2088 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
bd9c7e23 2089 {
5895f793 2090 *op1 = GEN_INT (i - 1);
bd9c7e23
RE
2091 return code == GE ? GT : LE;
2092 }
2093 break;
2094
2095 case GTU:
2096 case LEU:
30cf4896 2097 if (i != ~((unsigned HOST_WIDE_INT) 0)
5895f793 2098 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
bd9c7e23
RE
2099 {
2100 *op1 = GEN_INT (i + 1);
2101 return code == GTU ? GEU : LTU;
2102 }
2103 break;
2104
2105 case GEU:
2106 case LTU:
2107 if (i != 0
5895f793 2108 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
bd9c7e23
RE
2109 {
2110 *op1 = GEN_INT (i - 1);
2111 return code == GEU ? GTU : LEU;
2112 }
2113 break;
2114
2115 default:
2116 abort ();
2117 }
2118
2119 return code;
2120}
bd9c7e23 2121
d4453b7a
PB
2122
2123/* Define how to find the value returned by a function. */
2124
2125rtx arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2126{
2127 enum machine_mode mode;
2128 int unsignedp ATTRIBUTE_UNUSED;
2129 rtx r ATTRIBUTE_UNUSED;
2130
2131
2132 mode = TYPE_MODE (type);
2133 /* Promote integer types. */
2134 if (INTEGRAL_TYPE_P (type))
2135 PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2136 return LIBCALL_VALUE(mode);
2137}
2138
2139
f5a1b0d2
NC
2140/* Decide whether a type should be returned in memory (true)
2141 or in a register (false). This is called by the macro
2142 RETURN_IN_MEMORY. */
2b835d68 2143int
e32bac5b 2144arm_return_in_memory (tree type)
2b835d68 2145{
dc0ba55a
JT
2146 HOST_WIDE_INT size;
2147
5895f793 2148 if (!AGGREGATE_TYPE_P (type))
9e291dbe 2149 /* All simple types are returned in registers. */
d7d01975 2150 return 0;
dc0ba55a
JT
2151
2152 size = int_size_in_bytes (type);
2153
5848830f 2154 if (arm_abi != ARM_ABI_APCS)
dc0ba55a 2155 {
5848830f 2156 /* ATPCS and later return aggregate types in memory only if they are
dc0ba55a
JT
2157 larger than a word (or are variable size). */
2158 return (size < 0 || size > UNITS_PER_WORD);
2159 }
d5b7b3ae 2160
6bc82793 2161 /* For the arm-wince targets we choose to be compatible with Microsoft's
d5b7b3ae
RE
2162 ARM and Thumb compilers, which always return aggregates in memory. */
2163#ifndef ARM_WINCE
e529bd42
NC
2164 /* All structures/unions bigger than one word are returned in memory.
2165 Also catch the case where int_size_in_bytes returns -1. In this case
6bc82793 2166 the aggregate is either huge or of variable size, and in either case
e529bd42 2167 we will want to return it via memory and not in a register. */
dc0ba55a 2168 if (size < 0 || size > UNITS_PER_WORD)
d7d01975 2169 return 1;
d5b7b3ae 2170
d7d01975 2171 if (TREE_CODE (type) == RECORD_TYPE)
2b835d68
RE
2172 {
2173 tree field;
2174
3a2ea258
RE
2175 /* For a struct the APCS says that we only return in a register
2176 if the type is 'integer like' and every addressable element
2177 has an offset of zero. For practical purposes this means
2178 that the structure can have at most one non bit-field element
2179 and that this element must be the first one in the structure. */
2180
f5a1b0d2
NC
2181 /* Find the first field, ignoring non FIELD_DECL things which will
2182 have been created by C++. */
2183 for (field = TYPE_FIELDS (type);
2184 field && TREE_CODE (field) != FIELD_DECL;
2185 field = TREE_CHAIN (field))
2186 continue;
2187
2188 if (field == NULL)
9e291dbe 2189 return 0; /* An empty structure. Allowed by an extension to ANSI C. */
f5a1b0d2 2190
d5b7b3ae
RE
2191 /* Check that the first field is valid for returning in a register. */
2192
2193 /* ... Floats are not allowed */
9e291dbe 2194 if (FLOAT_TYPE_P (TREE_TYPE (field)))
3a2ea258
RE
2195 return 1;
2196
d5b7b3ae
RE
2197 /* ... Aggregates that are not themselves valid for returning in
2198 a register are not allowed. */
9e291dbe 2199 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
3a2ea258 2200 return 1;
6f7ebcbb 2201
3a2ea258
RE
2202 /* Now check the remaining fields, if any. Only bitfields are allowed,
2203 since they are not addressable. */
f5a1b0d2
NC
2204 for (field = TREE_CHAIN (field);
2205 field;
2206 field = TREE_CHAIN (field))
2207 {
2208 if (TREE_CODE (field) != FIELD_DECL)
2209 continue;
2210
5895f793 2211 if (!DECL_BIT_FIELD_TYPE (field))
f5a1b0d2
NC
2212 return 1;
2213 }
2b835d68
RE
2214
2215 return 0;
2216 }
d7d01975
NC
2217
2218 if (TREE_CODE (type) == UNION_TYPE)
2b835d68
RE
2219 {
2220 tree field;
2221
2222 /* Unions can be returned in registers if every element is
2223 integral, or can be returned in an integer register. */
f5a1b0d2
NC
2224 for (field = TYPE_FIELDS (type);
2225 field;
2226 field = TREE_CHAIN (field))
2b835d68 2227 {
f5a1b0d2
NC
2228 if (TREE_CODE (field) != FIELD_DECL)
2229 continue;
2230
6cc8c0b3
NC
2231 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2232 return 1;
2233
f5a1b0d2 2234 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2b835d68
RE
2235 return 1;
2236 }
f5a1b0d2 2237
2b835d68
RE
2238 return 0;
2239 }
d5b7b3ae 2240#endif /* not ARM_WINCE */
f5a1b0d2 2241
d5b7b3ae 2242 /* Return all other types in memory. */
2b835d68
RE
2243 return 1;
2244}
2245
d6b4baa4 2246/* Indicate whether or not words of a double are in big-endian order. */
3717da94
JT
2247
2248int
e32bac5b 2249arm_float_words_big_endian (void)
3717da94 2250{
9b66ebb1 2251 if (TARGET_MAVERICK)
9b6b54e2 2252 return 0;
3717da94
JT
2253
2254 /* For FPA, float words are always big-endian. For VFP, floats words
2255 follow the memory system mode. */
2256
9b66ebb1 2257 if (TARGET_FPA)
3717da94 2258 {
3717da94
JT
2259 return 1;
2260 }
2261
2262 if (TARGET_VFP)
2263 return (TARGET_BIG_END ? 1 : 0);
2264
2265 return 1;
2266}
2267
82e9d970
PB
2268/* Initialize a variable CUM of type CUMULATIVE_ARGS
2269 for a call to a function whose data type is FNTYPE.
2270 For a library call, FNTYPE is NULL. */
2271void
e32bac5b
RE
2272arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2273 rtx libname ATTRIBUTE_UNUSED,
2274 tree fndecl ATTRIBUTE_UNUSED)
82e9d970
PB
2275{
2276 /* On the ARM, the offset starts at 0. */
61f71b34 2277 pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
5a9335ef 2278 pcum->iwmmxt_nregs = 0;
5848830f 2279 pcum->can_split = true;
c27ba912 2280
82e9d970
PB
2281 pcum->call_cookie = CALL_NORMAL;
2282
2283 if (TARGET_LONG_CALLS)
2284 pcum->call_cookie = CALL_LONG;
2285
2286 /* Check for long call/short call attributes. The attributes
2287 override any command line option. */
2288 if (fntype)
2289 {
2290 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2291 pcum->call_cookie = CALL_SHORT;
2292 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2293 pcum->call_cookie = CALL_LONG;
2294 }
5a9335ef
NC
2295
2296 /* Varargs vectors are treated the same as long long.
2297 named_count avoids having to change the way arm handles 'named' */
2298 pcum->named_count = 0;
2299 pcum->nargs = 0;
2300
2301 if (TARGET_REALLY_IWMMXT && fntype)
2302 {
2303 tree fn_arg;
2304
2305 for (fn_arg = TYPE_ARG_TYPES (fntype);
2306 fn_arg;
2307 fn_arg = TREE_CHAIN (fn_arg))
2308 pcum->named_count += 1;
2309
2310 if (! pcum->named_count)
2311 pcum->named_count = INT_MAX;
2312 }
82e9d970
PB
2313}
2314
5848830f
PB
2315
2316/* Return true if mode/type need doubleword alignment. */
2317bool
2318arm_needs_doubleword_align (enum machine_mode mode, tree type)
2319{
2320 return (mode == DImode
2321 || mode == DFmode
2322 || VECTOR_MODE_SUPPORTED_P (mode)
2323 || (mode == BLKmode
2324 && TYPE_ALIGN (type) > PARM_BOUNDARY));
2325}
2326
2327
82e9d970
PB
2328/* Determine where to put an argument to a function.
2329 Value is zero to push the argument on the stack,
2330 or a hard register in which to store the argument.
2331
2332 MODE is the argument's machine mode.
2333 TYPE is the data type of the argument (as a tree).
2334 This is null for libcalls where that information may
2335 not be available.
2336 CUM is a variable of type CUMULATIVE_ARGS which gives info about
2337 the preceding args and about the function being called.
2338 NAMED is nonzero if this argument is a named parameter
2339 (otherwise it is an extra parameter matching an ellipsis). */
1d6e90ac 2340
82e9d970 2341rtx
e32bac5b 2342arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
5848830f 2343 tree type, int named)
82e9d970 2344{
5848830f
PB
2345 int nregs;
2346
2347 /* Varargs vectors are treated the same as long long.
2348 named_count avoids having to change the way arm handles 'named' */
2349 if (TARGET_IWMMXT_ABI
2350 && VECTOR_MODE_SUPPORTED_P (mode)
2351 && pcum->named_count > pcum->nargs + 1)
5a9335ef 2352 {
5848830f
PB
2353 if (pcum->iwmmxt_nregs <= 9)
2354 return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2355 else
5a9335ef 2356 {
5848830f
PB
2357 pcum->can_split = false;
2358 return NULL_RTX;
5a9335ef 2359 }
5a9335ef
NC
2360 }
2361
5848830f
PB
2362 /* Put doubleword aligned quantities in even register pairs. */
2363 if (pcum->nregs & 1
2364 && ARM_DOUBLEWORD_ALIGN
2365 && arm_needs_doubleword_align (mode, type))
2366 pcum->nregs++;
2367
82e9d970
PB
2368 if (mode == VOIDmode)
2369 /* Compute operand 2 of the call insn. */
2370 return GEN_INT (pcum->call_cookie);
5848830f 2371
666c27b9 2372 /* Only allow splitting an arg between regs and memory if all preceding
5848830f
PB
2373 args were allocated to regs. For args passed by reference we only count
2374 the reference pointer. */
2375 if (pcum->can_split)
2376 nregs = 1;
2377 else
2378 nregs = ARM_NUM_REGS2 (mode, type);
2379
2380 if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
82e9d970
PB
2381 return NULL_RTX;
2382
2383 return gen_rtx_REG (mode, pcum->nregs);
2384}
1741620c
JD
2385
2386/* Variable sized types are passed by reference. This is a GCC
2387 extension to the ARM ABI. */
2388
2389int
e32bac5b
RE
2390arm_function_arg_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2391 enum machine_mode mode ATTRIBUTE_UNUSED,
2392 tree type, int named ATTRIBUTE_UNUSED)
1741620c
JD
2393{
2394 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2395}
2396
2397/* Implement va_arg. */
2398
2399rtx
e32bac5b 2400arm_va_arg (tree valist, tree type)
1741620c 2401{
5848830f
PB
2402 int align;
2403
1741620c
JD
2404 /* Variable sized types are passed by reference. */
2405 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
2406 {
2407 rtx addr = std_expand_builtin_va_arg (valist, build_pointer_type (type));
2408 return gen_rtx_MEM (ptr_mode, force_reg (Pmode, addr));
2409 }
2410
5848830f
PB
2411 align = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type);
2412 if (align > PARM_BOUNDARY)
5a9335ef 2413 {
5848830f 2414 tree mask;
5a9335ef
NC
2415 tree t;
2416
2417 /* Maintain 64-bit alignment of the valist pointer by
093354e0 2418 constructing: valist = ((valist + (8 - 1)) & -8). */
5848830f
PB
2419 mask = build_int_2 (- (align / BITS_PER_UNIT), -1);
2420 t = build_int_2 ((align / BITS_PER_UNIT) - 1, 0);
5a9335ef 2421 t = build (PLUS_EXPR, TREE_TYPE (valist), valist, t);
5848830f 2422 t = build (BIT_AND_EXPR, TREE_TYPE (t), t, mask);
5a9335ef
NC
2423 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
2424 TREE_SIDE_EFFECTS (t) = 1;
2425 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2426
f1ba665b 2427 /* This is to stop the combine pass optimizing
5a9335ef
NC
2428 away the alignment adjustment. */
2429 mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
2430 }
2431
1741620c
JD
2432 return std_expand_builtin_va_arg (valist, type);
2433}
82e9d970 2434\f
c27ba912
DM
2435/* Encode the current state of the #pragma [no_]long_calls. */
2436typedef enum
82e9d970 2437{
c27ba912
DM
2438 OFF, /* No #pramgma [no_]long_calls is in effect. */
2439 LONG, /* #pragma long_calls is in effect. */
2440 SHORT /* #pragma no_long_calls is in effect. */
2441} arm_pragma_enum;
82e9d970 2442
c27ba912 2443static arm_pragma_enum arm_pragma_long_calls = OFF;
82e9d970 2444
8b97c5f8 2445void
e32bac5b 2446arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
82e9d970 2447{
8b97c5f8
ZW
2448 arm_pragma_long_calls = LONG;
2449}
2450
2451void
e32bac5b 2452arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
8b97c5f8
ZW
2453{
2454 arm_pragma_long_calls = SHORT;
2455}
2456
2457void
e32bac5b 2458arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
8b97c5f8
ZW
2459{
2460 arm_pragma_long_calls = OFF;
82e9d970
PB
2461}
2462\f
91d231cb
JM
2463/* Table of machine attributes. */
2464const struct attribute_spec arm_attribute_table[] =
82e9d970 2465{
91d231cb 2466 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
82e9d970
PB
2467 /* Function calls made to this symbol must be done indirectly, because
2468 it may lie outside of the 26 bit addressing range of a normal function
2469 call. */
91d231cb 2470 { "long_call", 0, 0, false, true, true, NULL },
82e9d970
PB
2471 /* Whereas these functions are always known to reside within the 26 bit
2472 addressing range. */
91d231cb 2473 { "short_call", 0, 0, false, true, true, NULL },
6d3d9133 2474 /* Interrupt Service Routines have special prologue and epilogue requirements. */
91d231cb
JM
2475 { "isr", 0, 1, false, false, false, arm_handle_isr_attribute },
2476 { "interrupt", 0, 1, false, false, false, arm_handle_isr_attribute },
2477 { "naked", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2478#ifdef ARM_PE
2479 /* ARM/PE has three new attributes:
2480 interfacearm - ?
2481 dllexport - for exporting a function/variable that will live in a dll
2482 dllimport - for importing a function/variable from a dll
2483
2484 Microsoft allows multiple declspecs in one __declspec, separating
2485 them with spaces. We do NOT support this. Instead, use __declspec
2486 multiple times.
2487 */
2488 { "dllimport", 0, 0, true, false, false, NULL },
2489 { "dllexport", 0, 0, true, false, false, NULL },
2490 { "interfacearm", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2491#endif
2492 { NULL, 0, 0, false, false, false, NULL }
2493};
6d3d9133 2494
91d231cb
JM
2495/* Handle an attribute requiring a FUNCTION_DECL;
2496 arguments as in struct attribute_spec.handler. */
2497static tree
e32bac5b
RE
2498arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2499 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
91d231cb
JM
2500{
2501 if (TREE_CODE (*node) != FUNCTION_DECL)
2502 {
2503 warning ("`%s' attribute only applies to functions",
2504 IDENTIFIER_POINTER (name));
2505 *no_add_attrs = true;
2506 }
2507
2508 return NULL_TREE;
2509}
2510
2511/* Handle an "interrupt" or "isr" attribute;
2512 arguments as in struct attribute_spec.handler. */
2513static tree
e32bac5b
RE
2514arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2515 bool *no_add_attrs)
91d231cb
JM
2516{
2517 if (DECL_P (*node))
2518 {
2519 if (TREE_CODE (*node) != FUNCTION_DECL)
2520 {
2521 warning ("`%s' attribute only applies to functions",
2522 IDENTIFIER_POINTER (name));
2523 *no_add_attrs = true;
2524 }
2525 /* FIXME: the argument if any is checked for type attributes;
2526 should it be checked for decl ones? */
2527 }
2528 else
2529 {
2530 if (TREE_CODE (*node) == FUNCTION_TYPE
2531 || TREE_CODE (*node) == METHOD_TYPE)
2532 {
2533 if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2534 {
2535 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2536 *no_add_attrs = true;
2537 }
2538 }
2539 else if (TREE_CODE (*node) == POINTER_TYPE
2540 && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2541 || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2542 && arm_isr_value (args) != ARM_FT_UNKNOWN)
2543 {
2544 *node = build_type_copy (*node);
1d6e90ac
NC
2545 TREE_TYPE (*node) = build_type_attribute_variant
2546 (TREE_TYPE (*node),
2547 tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
91d231cb
JM
2548 *no_add_attrs = true;
2549 }
2550 else
2551 {
2552 /* Possibly pass this attribute on from the type to a decl. */
2553 if (flags & ((int) ATTR_FLAG_DECL_NEXT
2554 | (int) ATTR_FLAG_FUNCTION_NEXT
2555 | (int) ATTR_FLAG_ARRAY_NEXT))
2556 {
2557 *no_add_attrs = true;
2558 return tree_cons (name, args, NULL_TREE);
2559 }
2560 else
2561 {
2562 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2563 }
2564 }
2565 }
2566
2567 return NULL_TREE;
82e9d970
PB
2568}
2569
2570/* Return 0 if the attributes for two types are incompatible, 1 if they
2571 are compatible, and 2 if they are nearly compatible (which causes a
2572 warning to be generated). */
8d8e52be 2573static int
e32bac5b 2574arm_comp_type_attributes (tree type1, tree type2)
82e9d970 2575{
1cb8d58a 2576 int l1, l2, s1, s2;
bd7fc26f 2577
82e9d970
PB
2578 /* Check for mismatch of non-default calling convention. */
2579 if (TREE_CODE (type1) != FUNCTION_TYPE)
2580 return 1;
2581
2582 /* Check for mismatched call attributes. */
1cb8d58a
NC
2583 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2584 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2585 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2586 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
bd7fc26f
NC
2587
2588 /* Only bother to check if an attribute is defined. */
2589 if (l1 | l2 | s1 | s2)
2590 {
2591 /* If one type has an attribute, the other must have the same attribute. */
1cb8d58a 2592 if ((l1 != l2) || (s1 != s2))
bd7fc26f 2593 return 0;
82e9d970 2594
bd7fc26f
NC
2595 /* Disallow mixed attributes. */
2596 if ((l1 & s2) || (l2 & s1))
2597 return 0;
2598 }
2599
6d3d9133
NC
2600 /* Check for mismatched ISR attribute. */
2601 l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2602 if (! l1)
2603 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2604 l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2605 if (! l2)
2606 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2607 if (l1 != l2)
2608 return 0;
2609
bd7fc26f 2610 return 1;
82e9d970
PB
2611}
2612
c27ba912
DM
2613/* Encode long_call or short_call attribute by prefixing
2614 symbol name in DECL with a special character FLAG. */
2615void
e32bac5b 2616arm_encode_call_attribute (tree decl, int flag)
c27ba912 2617{
3cce094d 2618 const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6354dc9b 2619 int len = strlen (str);
d19fb8e3 2620 char * newstr;
c27ba912 2621
c27ba912
DM
2622 /* Do not allow weak functions to be treated as short call. */
2623 if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2624 return;
c27ba912 2625
520a57c8
ZW
2626 newstr = alloca (len + 2);
2627 newstr[0] = flag;
2628 strcpy (newstr + 1, str);
c27ba912 2629
6d3d9133 2630 newstr = (char *) ggc_alloc_string (newstr, len + 1);
c27ba912
DM
2631 XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2632}
2633
2634/* Assigns default attributes to newly defined type. This is used to
2635 set short_call/long_call attributes for function types of
2636 functions defined inside corresponding #pragma scopes. */
8d8e52be 2637static void
e32bac5b 2638arm_set_default_type_attributes (tree type)
c27ba912
DM
2639{
2640 /* Add __attribute__ ((long_call)) to all functions, when
2641 inside #pragma long_calls or __attribute__ ((short_call)),
2642 when inside #pragma no_long_calls. */
2643 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2644 {
2645 tree type_attr_list, attr_name;
2646 type_attr_list = TYPE_ATTRIBUTES (type);
2647
2648 if (arm_pragma_long_calls == LONG)
2649 attr_name = get_identifier ("long_call");
2650 else if (arm_pragma_long_calls == SHORT)
2651 attr_name = get_identifier ("short_call");
2652 else
2653 return;
2654
2655 type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2656 TYPE_ATTRIBUTES (type) = type_attr_list;
2657 }
2658}
2659\f
2660/* Return 1 if the operand is a SYMBOL_REF for a function known to be
6bc82793 2661 defined within the current compilation unit. If this cannot be
c27ba912
DM
2662 determined, then 0 is returned. */
2663static int
e32bac5b 2664current_file_function_operand (rtx sym_ref)
c27ba912
DM
2665{
2666 /* This is a bit of a fib. A function will have a short call flag
2667 applied to its name if it has the short call attribute, or it has
2668 already been defined within the current compilation unit. */
2669 if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2670 return 1;
2671
6d77b53e 2672 /* The current function is always defined within the current compilation
d6a7951f
JM
2673 unit. if it s a weak definition however, then this may not be the real
2674 definition of the function, and so we have to say no. */
c27ba912 2675 if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
5895f793 2676 && !DECL_WEAK (current_function_decl))
c27ba912
DM
2677 return 1;
2678
2679 /* We cannot make the determination - default to returning 0. */
2680 return 0;
2681}
2682
825dda42 2683/* Return nonzero if a 32 bit "long_call" should be generated for
c27ba912
DM
2684 this call. We generate a long_call if the function:
2685
2686 a. has an __attribute__((long call))
2687 or b. is within the scope of a #pragma long_calls
2688 or c. the -mlong-calls command line switch has been specified
2689
2690 However we do not generate a long call if the function:
2691
2692 d. has an __attribute__ ((short_call))
2693 or e. is inside the scope of a #pragma no_long_calls
2694 or f. has an __attribute__ ((section))
2695 or g. is defined within the current compilation unit.
2696
2697 This function will be called by C fragments contained in the machine
2698 description file. CALL_REF and CALL_COOKIE correspond to the matched
2699 rtl operands. CALL_SYMBOL is used to distinguish between
2700 two different callers of the function. It is set to 1 in the
2701 "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2702 and "call_value" patterns. This is because of the difference in the
2703 SYM_REFs passed by these patterns. */
2704int
e32bac5b 2705arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
c27ba912 2706{
5895f793 2707 if (!call_symbol)
c27ba912
DM
2708 {
2709 if (GET_CODE (sym_ref) != MEM)
2710 return 0;
2711
2712 sym_ref = XEXP (sym_ref, 0);
2713 }
2714
2715 if (GET_CODE (sym_ref) != SYMBOL_REF)
2716 return 0;
2717
2718 if (call_cookie & CALL_SHORT)
2719 return 0;
2720
2721 if (TARGET_LONG_CALLS && flag_function_sections)
2722 return 1;
2723
87e27392 2724 if (current_file_function_operand (sym_ref))
c27ba912
DM
2725 return 0;
2726
2727 return (call_cookie & CALL_LONG)
2728 || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2729 || TARGET_LONG_CALLS;
2730}
f99fce0c 2731
825dda42 2732/* Return nonzero if it is ok to make a tail-call to DECL. */
4977bab6 2733static bool
e32bac5b 2734arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
f99fce0c
RE
2735{
2736 int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2737
5a9335ef
NC
2738 if (cfun->machine->sibcall_blocked)
2739 return false;
2740
f99fce0c
RE
2741 /* Never tailcall something for which we have no decl, or if we
2742 are in Thumb mode. */
2743 if (decl == NULL || TARGET_THUMB)
4977bab6 2744 return false;
f99fce0c
RE
2745
2746 /* Get the calling method. */
2747 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2748 call_type = CALL_SHORT;
2749 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2750 call_type = CALL_LONG;
2751
2752 /* Cannot tail-call to long calls, since these are out of range of
2753 a branch instruction. However, if not compiling PIC, we know
2754 we can reach the symbol if it is in this compilation unit. */
5895f793 2755 if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
4977bab6 2756 return false;
f99fce0c
RE
2757
2758 /* If we are interworking and the function is not declared static
2759 then we can't tail-call it unless we know that it exists in this
2760 compilation unit (since it might be a Thumb routine). */
5895f793 2761 if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
4977bab6 2762 return false;
f99fce0c 2763
6d3d9133
NC
2764 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
2765 if (IS_INTERRUPT (arm_current_func_type ()))
4977bab6 2766 return false;
6d3d9133 2767
f99fce0c 2768 /* Everything else is ok. */
4977bab6 2769 return true;
f99fce0c
RE
2770}
2771
82e9d970 2772\f
6b990f6b
RE
2773/* Addressing mode support functions. */
2774
0b4be7de 2775/* Return nonzero if X is a legitimate immediate operand when compiling
6b990f6b 2776 for PIC. */
32de079a 2777int
e32bac5b 2778legitimate_pic_operand_p (rtx x)
32de079a 2779{
d5b7b3ae
RE
2780 if (CONSTANT_P (x)
2781 && flag_pic
32de079a
RE
2782 && (GET_CODE (x) == SYMBOL_REF
2783 || (GET_CODE (x) == CONST
2784 && GET_CODE (XEXP (x, 0)) == PLUS
2785 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2786 return 0;
2787
2788 return 1;
2789}
2790
2791rtx
e32bac5b 2792legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
32de079a 2793{
a3c48721
RE
2794 if (GET_CODE (orig) == SYMBOL_REF
2795 || GET_CODE (orig) == LABEL_REF)
32de079a 2796 {
5f37d07c 2797#ifndef AOF_ASSEMBLER
32de079a 2798 rtx pic_ref, address;
5f37d07c 2799#endif
32de079a
RE
2800 rtx insn;
2801 int subregs = 0;
2802
2803 if (reg == 0)
2804 {
893f3d5b 2805 if (no_new_pseudos)
32de079a
RE
2806 abort ();
2807 else
2808 reg = gen_reg_rtx (Pmode);
2809
2810 subregs = 1;
2811 }
2812
2813#ifdef AOF_ASSEMBLER
2814 /* The AOF assembler can generate relocations for these directly, and
6354dc9b 2815 understands that the PIC register has to be added into the offset. */
32de079a
RE
2816 insn = emit_insn (gen_pic_load_addr_based (reg, orig));
2817#else
2818 if (subregs)
2819 address = gen_reg_rtx (Pmode);
2820 else
2821 address = reg;
2822
4bec9f7d
NC
2823 if (TARGET_ARM)
2824 emit_insn (gen_pic_load_addr_arm (address, orig));
2825 else
2826 emit_insn (gen_pic_load_addr_thumb (address, orig));
32de079a 2827
14f583b8
PB
2828 if ((GET_CODE (orig) == LABEL_REF
2829 || (GET_CODE (orig) == SYMBOL_REF &&
94428622 2830 SYMBOL_REF_LOCAL_P (orig)))
14f583b8 2831 && NEED_GOT_RELOC)
a3c48721
RE
2832 pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
2833 else
2834 {
2835 pic_ref = gen_rtx_MEM (Pmode,
2836 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
2837 address));
2838 RTX_UNCHANGING_P (pic_ref) = 1;
2839 }
2840
32de079a
RE
2841 insn = emit_move_insn (reg, pic_ref);
2842#endif
2843 current_function_uses_pic_offset_table = 1;
2844 /* Put a REG_EQUAL note on this insn, so that it can be optimized
2845 by loop. */
43cffd11
RE
2846 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
2847 REG_NOTES (insn));
32de079a
RE
2848 return reg;
2849 }
2850 else if (GET_CODE (orig) == CONST)
2851 {
2852 rtx base, offset;
2853
2854 if (GET_CODE (XEXP (orig, 0)) == PLUS
2855 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
2856 return orig;
2857
2858 if (reg == 0)
2859 {
893f3d5b 2860 if (no_new_pseudos)
32de079a
RE
2861 abort ();
2862 else
2863 reg = gen_reg_rtx (Pmode);
2864 }
2865
2866 if (GET_CODE (XEXP (orig, 0)) == PLUS)
2867 {
2868 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
2869 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
2870 base == reg ? 0 : reg);
2871 }
2872 else
2873 abort ();
2874
2875 if (GET_CODE (offset) == CONST_INT)
2876 {
2877 /* The base register doesn't really matter, we only want to
2878 test the index for the appropriate mode. */
1e1ab407 2879 if (!arm_legitimate_index_p (mode, offset, SET, 0))
6b990f6b
RE
2880 {
2881 if (!no_new_pseudos)
2882 offset = force_reg (Pmode, offset);
2883 else
2884 abort ();
2885 }
32de079a 2886
32de079a 2887 if (GET_CODE (offset) == CONST_INT)
ed8908e7 2888 return plus_constant (base, INTVAL (offset));
32de079a
RE
2889 }
2890
2891 if (GET_MODE_SIZE (mode) > 4
2892 && (GET_MODE_CLASS (mode) == MODE_INT
2893 || TARGET_SOFT_FLOAT))
2894 {
2895 emit_insn (gen_addsi3 (reg, base, offset));
2896 return reg;
2897 }
2898
43cffd11 2899 return gen_rtx_PLUS (Pmode, base, offset);
32de079a 2900 }
32de079a
RE
2901
2902 return orig;
2903}
2904
c1163e75
PB
2905/* Generate code to load the PIC register. PROLOGUE is true if
2906 called from arm_expand_prologue (in which case we want the
2907 generated insns at the start of the function); false if called
2908 by an exception receiver that needs the PIC register reloaded
2909 (in which case the insns are just dumped at the current location). */
32de079a 2910void
e32bac5b 2911arm_finalize_pic (int prologue ATTRIBUTE_UNUSED)
32de079a
RE
2912{
2913#ifndef AOF_ASSEMBLER
c1163e75 2914 rtx l1, pic_tmp, pic_tmp2, seq, pic_rtx;
32de079a
RE
2915 rtx global_offset_table;
2916
ed0e6530 2917 if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
32de079a
RE
2918 return;
2919
5895f793 2920 if (!flag_pic)
32de079a
RE
2921 abort ();
2922
2923 start_sequence ();
2924 l1 = gen_label_rtx ();
2925
43cffd11 2926 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
dfa08768 2927 /* On the ARM the PC register contains 'dot + 8' at the time of the
d5b7b3ae
RE
2928 addition, on the Thumb it is 'dot + 4'. */
2929 pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
84306176
PB
2930 if (GOT_PCREL)
2931 pic_tmp2 = gen_rtx_CONST (VOIDmode,
43cffd11 2932 gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
84306176
PB
2933 else
2934 pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
43cffd11
RE
2935
2936 pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
f5a1b0d2 2937
d5b7b3ae 2938 if (TARGET_ARM)
4bec9f7d
NC
2939 {
2940 emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
2941 emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
2942 }
d5b7b3ae 2943 else
4bec9f7d
NC
2944 {
2945 emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
2946 emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
2947 }
32de079a 2948
2f937369 2949 seq = get_insns ();
32de079a 2950 end_sequence ();
c1163e75
PB
2951 if (prologue)
2952 emit_insn_after (seq, get_insns ());
2953 else
2954 emit_insn (seq);
32de079a
RE
2955
2956 /* Need to emit this whether or not we obey regdecls,
2957 since setjmp/longjmp can cause life info to screw up. */
43cffd11 2958 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
32de079a
RE
2959#endif /* AOF_ASSEMBLER */
2960}
2961
6b990f6b
RE
2962/* Return nonzero if X is valid as an ARM state addressing register. */
2963static int
e32bac5b 2964arm_address_register_rtx_p (rtx x, int strict_p)
6b990f6b
RE
2965{
2966 int regno;
2967
2968 if (GET_CODE (x) != REG)
2969 return 0;
2970
2971 regno = REGNO (x);
2972
2973 if (strict_p)
2974 return ARM_REGNO_OK_FOR_BASE_P (regno);
2975
2976 return (regno <= LAST_ARM_REGNUM
2977 || regno >= FIRST_PSEUDO_REGISTER
2978 || regno == FRAME_POINTER_REGNUM
2979 || regno == ARG_POINTER_REGNUM);
2980}
2981
2982/* Return nonzero if X is a valid ARM state address operand. */
2983int
1e1ab407
RE
2984arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
2985 int strict_p)
6b990f6b
RE
2986{
2987 if (arm_address_register_rtx_p (x, strict_p))
2988 return 1;
2989
2990 else if (GET_CODE (x) == POST_INC || GET_CODE (x) == PRE_DEC)
2991 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
2992
2993 else if ((GET_CODE (x) == POST_MODIFY || GET_CODE (x) == PRE_MODIFY)
2994 && GET_MODE_SIZE (mode) <= 4
2995 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
2996 && GET_CODE (XEXP (x, 1)) == PLUS
2997 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
1e1ab407
RE
2998 return arm_legitimate_index_p (mode, XEXP (XEXP (x, 1), 1), outer,
2999 strict_p);
6b990f6b
RE
3000
3001 /* After reload constants split into minipools will have addresses
3002 from a LABEL_REF. */
0bfb39ef 3003 else if (reload_completed
6b990f6b
RE
3004 && (GET_CODE (x) == LABEL_REF
3005 || (GET_CODE (x) == CONST
3006 && GET_CODE (XEXP (x, 0)) == PLUS
3007 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3008 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3009 return 1;
3010
3011 else if (mode == TImode)
3012 return 0;
3013
3014 else if (mode == DImode || (TARGET_SOFT_FLOAT && mode == DFmode))
3015 {
3016 if (GET_CODE (x) == PLUS
3017 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3018 && GET_CODE (XEXP (x, 1)) == CONST_INT)
3019 {
3020 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
3021
3022 if (val == 4 || val == -4 || val == -8)
3023 return 1;
3024 }
3025 }
3026
9b66ebb1
PB
3027 else if (TARGET_HARD_FLOAT && TARGET_VFP && mode == DFmode)
3028 {
3029 if (GET_CODE (x) == PLUS
3030 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3031 && GET_CODE (XEXP (x, 1)) == CONST_INT)
3032 {
3033 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
3034
3035 /* ??? valid arm offsets are a subset of VFP offsets.
3036 For now only allow this subset. Proper fix is to add an
3037 additional memory constraint for arm address modes.
3038 Alternatively allow full vfp addressing and let
3039 output_move_double fix it up with a sub-optimal sequence. */
3040 if (val == 4 || val == -4 || val == -8)
3041 return 1;
3042 }
3043 }
3044
6b990f6b
RE
3045 else if (GET_CODE (x) == PLUS)
3046 {
3047 rtx xop0 = XEXP (x, 0);
3048 rtx xop1 = XEXP (x, 1);
3049
3050 return ((arm_address_register_rtx_p (xop0, strict_p)
1e1ab407 3051 && arm_legitimate_index_p (mode, xop1, outer, strict_p))
6b990f6b 3052 || (arm_address_register_rtx_p (xop1, strict_p)
1e1ab407 3053 && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
6b990f6b
RE
3054 }
3055
3056#if 0
3057 /* Reload currently can't handle MINUS, so disable this for now */
3058 else if (GET_CODE (x) == MINUS)
3059 {
3060 rtx xop0 = XEXP (x, 0);
3061 rtx xop1 = XEXP (x, 1);
3062
3063 return (arm_address_register_rtx_p (xop0, strict_p)
1e1ab407 3064 && arm_legitimate_index_p (mode, xop1, outer, strict_p));
6b990f6b
RE
3065 }
3066#endif
3067
3068 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3069 && GET_CODE (x) == SYMBOL_REF
3070 && CONSTANT_POOL_ADDRESS_P (x)
3071 && ! (flag_pic
3072 && symbol_mentioned_p (get_pool_constant (x))))
3073 return 1;
3074
3075 else if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == POST_DEC)
3076 && (GET_MODE_SIZE (mode) <= 4)
3077 && arm_address_register_rtx_p (XEXP (x, 0), strict_p))
3078 return 1;
3079
3080 return 0;
3081}
3082
3083/* Return nonzero if INDEX is valid for an address index operand in
3084 ARM state. */
3085static int
1e1ab407
RE
3086arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3087 int strict_p)
6b990f6b
RE
3088{
3089 HOST_WIDE_INT range;
3090 enum rtx_code code = GET_CODE (index);
3091
9b66ebb1 3092 if (TARGET_HARD_FLOAT && TARGET_FPA && GET_MODE_CLASS (mode) == MODE_FLOAT)
6b990f6b
RE
3093 return (code == CONST_INT && INTVAL (index) < 1024
3094 && INTVAL (index) > -1024
3095 && (INTVAL (index) & 3) == 0);
3096
9b66ebb1 3097 if (TARGET_HARD_FLOAT && TARGET_MAVERICK
9b6b54e2
NC
3098 && (GET_MODE_CLASS (mode) == MODE_FLOAT || mode == DImode))
3099 return (code == CONST_INT
3100 && INTVAL (index) < 255
3101 && INTVAL (index) > -255);
3102
6b990f6b
RE
3103 if (arm_address_register_rtx_p (index, strict_p)
3104 && GET_MODE_SIZE (mode) <= 4)
3105 return 1;
3106
5a9335ef
NC
3107 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3108 return (code == CONST_INT
3109 && INTVAL (index) < 256
3110 && INTVAL (index) > -256);
3111
6b990f6b 3112 if (GET_MODE_SIZE (mode) <= 4
1e1ab407
RE
3113 && ! (arm_arch4
3114 && (mode == HImode
3115 || (mode == QImode && outer == SIGN_EXTEND))))
6b990f6b 3116 {
1e1ab407
RE
3117 if (code == MULT)
3118 {
3119 rtx xiop0 = XEXP (index, 0);
3120 rtx xiop1 = XEXP (index, 1);
3121
3122 return ((arm_address_register_rtx_p (xiop0, strict_p)
3123 && power_of_two_operand (xiop1, SImode))
3124 || (arm_address_register_rtx_p (xiop1, strict_p)
3125 && power_of_two_operand (xiop0, SImode)));
3126 }
3127 else if (code == LSHIFTRT || code == ASHIFTRT
3128 || code == ASHIFT || code == ROTATERT)
3129 {
3130 rtx op = XEXP (index, 1);
6b990f6b 3131
1e1ab407
RE
3132 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3133 && GET_CODE (op) == CONST_INT
3134 && INTVAL (op) > 0
3135 && INTVAL (op) <= 31);
3136 }
6b990f6b
RE
3137 }
3138
1e1ab407
RE
3139 /* For ARM v4 we may be doing a sign-extend operation during the
3140 load. */
e1471c91 3141 if (arm_arch4)
1e1ab407
RE
3142 {
3143 if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3144 range = 256;
3145 else
3146 range = 4096;
3147 }
e1471c91
RE
3148 else
3149 range = (mode == HImode) ? 4095 : 4096;
6b990f6b
RE
3150
3151 return (code == CONST_INT
3152 && INTVAL (index) < range
3153 && INTVAL (index) > -range);
76a318e9
RE
3154}
3155
edf7cee8 3156/* Return nonzero if X is valid as a Thumb state base register. */
76a318e9 3157static int
e32bac5b 3158thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
76a318e9
RE
3159{
3160 int regno;
3161
3162 if (GET_CODE (x) != REG)
3163 return 0;
3164
3165 regno = REGNO (x);
3166
3167 if (strict_p)
3168 return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3169
3170 return (regno <= LAST_LO_REGNUM
07e58265 3171 || regno > LAST_VIRTUAL_REGISTER
76a318e9
RE
3172 || regno == FRAME_POINTER_REGNUM
3173 || (GET_MODE_SIZE (mode) >= 4
3174 && (regno == STACK_POINTER_REGNUM
edf7cee8 3175 || regno >= FIRST_PSEUDO_REGISTER
76a318e9
RE
3176 || x == hard_frame_pointer_rtx
3177 || x == arg_pointer_rtx)));
3178}
3179
3180/* Return nonzero if x is a legitimate index register. This is the case
3181 for any base register that can access a QImode object. */
3182inline static int
e32bac5b 3183thumb_index_register_rtx_p (rtx x, int strict_p)
76a318e9
RE
3184{
3185 return thumb_base_register_rtx_p (x, QImode, strict_p);
3186}
3187
3188/* Return nonzero if x is a legitimate Thumb-state address.
3189
3190 The AP may be eliminated to either the SP or the FP, so we use the
3191 least common denominator, e.g. SImode, and offsets from 0 to 64.
3192
3193 ??? Verify whether the above is the right approach.
3194
3195 ??? Also, the FP may be eliminated to the SP, so perhaps that
3196 needs special handling also.
3197
3198 ??? Look at how the mips16 port solves this problem. It probably uses
3199 better ways to solve some of these problems.
3200
3201 Although it is not incorrect, we don't accept QImode and HImode
3202 addresses based on the frame pointer or arg pointer until the
3203 reload pass starts. This is so that eliminating such addresses
3204 into stack based ones won't produce impossible code. */
3205int
e32bac5b 3206thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
76a318e9
RE
3207{
3208 /* ??? Not clear if this is right. Experiment. */
3209 if (GET_MODE_SIZE (mode) < 4
3210 && !(reload_in_progress || reload_completed)
3211 && (reg_mentioned_p (frame_pointer_rtx, x)
3212 || reg_mentioned_p (arg_pointer_rtx, x)
3213 || reg_mentioned_p (virtual_incoming_args_rtx, x)
3214 || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3215 || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3216 || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3217 return 0;
3218
3219 /* Accept any base register. SP only in SImode or larger. */
3220 else if (thumb_base_register_rtx_p (x, mode, strict_p))
3221 return 1;
3222
18dbd950 3223 /* This is PC relative data before arm_reorg runs. */
76a318e9
RE
3224 else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3225 && GET_CODE (x) == SYMBOL_REF
3226 && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3227 return 1;
3228
18dbd950 3229 /* This is PC relative data after arm_reorg runs. */
76a318e9
RE
3230 else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3231 && (GET_CODE (x) == LABEL_REF
3232 || (GET_CODE (x) == CONST
3233 && GET_CODE (XEXP (x, 0)) == PLUS
3234 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3235 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3236 return 1;
3237
3238 /* Post-inc indexing only supported for SImode and larger. */
3239 else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3240 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3241 return 1;
3242
3243 else if (GET_CODE (x) == PLUS)
3244 {
3245 /* REG+REG address can be any two index registers. */
3246 /* We disallow FRAME+REG addressing since we know that FRAME
3247 will be replaced with STACK, and SP relative addressing only
3248 permits SP+OFFSET. */
3249 if (GET_MODE_SIZE (mode) <= 4
3250 && XEXP (x, 0) != frame_pointer_rtx
3251 && XEXP (x, 1) != frame_pointer_rtx
76a318e9
RE
3252 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3253 && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3254 return 1;
3255
3256 /* REG+const has 5-7 bit offset for non-SP registers. */
3257 else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3258 || XEXP (x, 0) == arg_pointer_rtx)
3259 && GET_CODE (XEXP (x, 1)) == CONST_INT
3260 && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3261 return 1;
3262
3263 /* REG+const has 10 bit offset for SP, but only SImode and
3264 larger is supported. */
3265 /* ??? Should probably check for DI/DFmode overflow here
3266 just like GO_IF_LEGITIMATE_OFFSET does. */
3267 else if (GET_CODE (XEXP (x, 0)) == REG
3268 && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3269 && GET_MODE_SIZE (mode) >= 4
3270 && GET_CODE (XEXP (x, 1)) == CONST_INT
3271 && INTVAL (XEXP (x, 1)) >= 0
3272 && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3273 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3274 return 1;
3275
3276 else if (GET_CODE (XEXP (x, 0)) == REG
3277 && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3278 && GET_MODE_SIZE (mode) >= 4
3279 && GET_CODE (XEXP (x, 1)) == CONST_INT
3280 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3281 return 1;
3282 }
3283
3284 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
f954388e 3285 && GET_MODE_SIZE (mode) == 4
76a318e9
RE
3286 && GET_CODE (x) == SYMBOL_REF
3287 && CONSTANT_POOL_ADDRESS_P (x)
3288 && !(flag_pic
3289 && symbol_mentioned_p (get_pool_constant (x))))
3290 return 1;
3291
3292 return 0;
3293}
3294
3295/* Return nonzero if VAL can be used as an offset in a Thumb-state address
3296 instruction of mode MODE. */
3297int
e32bac5b 3298thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
76a318e9
RE
3299{
3300 switch (GET_MODE_SIZE (mode))
3301 {
3302 case 1:
3303 return val >= 0 && val < 32;
3304
3305 case 2:
3306 return val >= 0 && val < 64 && (val & 1) == 0;
3307
3308 default:
3309 return (val >= 0
3310 && (val + GET_MODE_SIZE (mode)) <= 128
3311 && (val & 3) == 0);
3312 }
3313}
3314
ccf4d512
RE
3315/* Try machine-dependent ways of modifying an illegitimate address
3316 to be legitimate. If we find one, return the new, valid address. */
ccf4d512 3317rtx
e32bac5b 3318arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
ccf4d512
RE
3319{
3320 if (GET_CODE (x) == PLUS)
3321 {
3322 rtx xop0 = XEXP (x, 0);
3323 rtx xop1 = XEXP (x, 1);
3324
3325 if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3326 xop0 = force_reg (SImode, xop0);
3327
3328 if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3329 xop1 = force_reg (SImode, xop1);
3330
3331 if (ARM_BASE_REGISTER_RTX_P (xop0)
3332 && GET_CODE (xop1) == CONST_INT)
3333 {
3334 HOST_WIDE_INT n, low_n;
3335 rtx base_reg, val;
3336 n = INTVAL (xop1);
3337
9b66ebb1
PB
3338 /* VFP addressing modes actually allow greater offsets, but for
3339 now we just stick with the lowest common denominator. */
3340 if (mode == DImode
3341 || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
ccf4d512
RE
3342 {
3343 low_n = n & 0x0f;
3344 n &= ~0x0f;
3345 if (low_n > 4)
3346 {
3347 n += 16;
3348 low_n -= 16;
3349 }
3350 }
3351 else
3352 {
3353 low_n = ((mode) == TImode ? 0
3354 : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3355 n -= low_n;
3356 }
3357
3358 base_reg = gen_reg_rtx (SImode);
3359 val = force_operand (gen_rtx_PLUS (SImode, xop0,
3360 GEN_INT (n)), NULL_RTX);
3361 emit_move_insn (base_reg, val);
3362 x = (low_n == 0 ? base_reg
3363 : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3364 }
3365 else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3366 x = gen_rtx_PLUS (SImode, xop0, xop1);
3367 }
3368
3369 /* XXX We don't allow MINUS any more -- see comment in
3370 arm_legitimate_address_p (). */
3371 else if (GET_CODE (x) == MINUS)
3372 {
3373 rtx xop0 = XEXP (x, 0);
3374 rtx xop1 = XEXP (x, 1);
3375
3376 if (CONSTANT_P (xop0))
3377 xop0 = force_reg (SImode, xop0);
3378
3379 if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3380 xop1 = force_reg (SImode, xop1);
3381
3382 if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3383 x = gen_rtx_MINUS (SImode, xop0, xop1);
3384 }
3385
3386 if (flag_pic)
3387 {
3388 /* We need to find and carefully transform any SYMBOL and LABEL
3389 references; so go back to the original address expression. */
3390 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3391
3392 if (new_x != orig_x)
3393 x = new_x;
3394 }
3395
3396 return x;
3397}
3398
6f5b4f3e
RE
3399
3400/* Try machine-dependent ways of modifying an illegitimate Thumb address
3401 to be legitimate. If we find one, return the new, valid address. */
3402rtx
3403thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3404{
3405 if (GET_CODE (x) == PLUS
3406 && GET_CODE (XEXP (x, 1)) == CONST_INT
3407 && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3408 || INTVAL (XEXP (x, 1)) < 0))
3409 {
3410 rtx xop0 = XEXP (x, 0);
3411 rtx xop1 = XEXP (x, 1);
3412 HOST_WIDE_INT offset = INTVAL (xop1);
3413
3414 /* Try and fold the offset into a biasing of the base register and
3415 then offsetting that. Don't do this when optimizing for space
3416 since it can cause too many CSEs. */
3417 if (optimize_size && offset >= 0
3418 && offset < 256 + 31 * GET_MODE_SIZE (mode))
3419 {
3420 HOST_WIDE_INT delta;
3421
3422 if (offset >= 256)
3423 delta = offset - (256 - GET_MODE_SIZE (mode));
3424 else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3425 delta = 31 * GET_MODE_SIZE (mode);
3426 else
3427 delta = offset & (~31 * GET_MODE_SIZE (mode));
3428
3429 xop0 = force_operand (plus_constant (xop0, offset - delta),
3430 NULL_RTX);
3431 x = plus_constant (xop0, delta);
3432 }
3433 else if (offset < 0 && offset > -256)
3434 /* Small negative offsets are best done with a subtract before the
3435 dereference, forcing these into a register normally takes two
3436 instructions. */
3437 x = force_operand (x, NULL_RTX);
3438 else
3439 {
3440 /* For the remaining cases, force the constant into a register. */
3441 xop1 = force_reg (SImode, xop1);
3442 x = gen_rtx_PLUS (SImode, xop0, xop1);
3443 }
3444 }
3445 else if (GET_CODE (x) == PLUS
3446 && s_register_operand (XEXP (x, 1), SImode)
3447 && !s_register_operand (XEXP (x, 0), SImode))
3448 {
3449 rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3450
3451 x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3452 }
3453
3454 if (flag_pic)
3455 {
3456 /* We need to find and carefully transform any SYMBOL and LABEL
3457 references; so go back to the original address expression. */
3458 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3459
3460 if (new_x != orig_x)
3461 x = new_x;
3462 }
3463
3464 return x;
3465}
3466
6b990f6b
RE
3467\f
3468
e2c671ba
RE
3469#define REG_OR_SUBREG_REG(X) \
3470 (GET_CODE (X) == REG \
3471 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3472
3473#define REG_OR_SUBREG_RTX(X) \
3474 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3475
d5b7b3ae
RE
3476#ifndef COSTS_N_INSNS
3477#define COSTS_N_INSNS(N) ((N) * 4 - 2)
3478#endif
3c50106f 3479static inline int
9b66ebb1 3480thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
e2c671ba
RE
3481{
3482 enum machine_mode mode = GET_MODE (x);
e2c671ba 3483
9b66ebb1 3484 switch (code)
d5b7b3ae 3485 {
9b66ebb1
PB
3486 case ASHIFT:
3487 case ASHIFTRT:
3488 case LSHIFTRT:
3489 case ROTATERT:
3490 case PLUS:
3491 case MINUS:
3492 case COMPARE:
3493 case NEG:
3494 case NOT:
3495 return COSTS_N_INSNS (1);
3496
3497 case MULT:
3498 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3499 {
3500 int cycles = 0;
3501 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
d5b7b3ae 3502
9b66ebb1 3503 while (i)
d5b7b3ae 3504 {
9b66ebb1
PB
3505 i >>= 2;
3506 cycles++;
3507 }
3508 return COSTS_N_INSNS (2) + cycles;
3509 }
3510 return COSTS_N_INSNS (1) + 16;
3511
3512 case SET:
3513 return (COSTS_N_INSNS (1)
3514 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
3515 + GET_CODE (SET_DEST (x)) == MEM));
3516
3517 case CONST_INT:
3518 if (outer == SET)
3519 {
3520 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
3521 return 0;
3522 if (thumb_shiftable_const (INTVAL (x)))
3523 return COSTS_N_INSNS (2);
3524 return COSTS_N_INSNS (3);
3525 }
3526 else if ((outer == PLUS || outer == COMPARE)
3527 && INTVAL (x) < 256 && INTVAL (x) > -256)
3528 return 0;
3529 else if (outer == AND
3530 && INTVAL (x) < 256 && INTVAL (x) >= -256)
3531 return COSTS_N_INSNS (1);
3532 else if (outer == ASHIFT || outer == ASHIFTRT
3533 || outer == LSHIFTRT)
3534 return 0;
3535 return COSTS_N_INSNS (2);
3536
3537 case CONST:
3538 case CONST_DOUBLE:
3539 case LABEL_REF:
3540 case SYMBOL_REF:
3541 return COSTS_N_INSNS (3);
3542
3543 case UDIV:
3544 case UMOD:
3545 case DIV:
3546 case MOD:
3547 return 100;
d5b7b3ae 3548
9b66ebb1
PB
3549 case TRUNCATE:
3550 return 99;
d5b7b3ae 3551
9b66ebb1
PB
3552 case AND:
3553 case XOR:
3554 case IOR:
ff482c8d 3555 /* XXX guess. */
9b66ebb1 3556 return 8;
d5b7b3ae 3557
9b66ebb1
PB
3558 case ADDRESSOF:
3559 case MEM:
3560 /* XXX another guess. */
3561 /* Memory costs quite a lot for the first word, but subsequent words
3562 load at the equivalent of a single insn each. */
3563 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3564 + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3565 ? 4 : 0));
3566
3567 case IF_THEN_ELSE:
ff482c8d 3568 /* XXX a guess. */
9b66ebb1
PB
3569 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3570 return 14;
3571 return 2;
3572
3573 case ZERO_EXTEND:
3574 /* XXX still guessing. */
3575 switch (GET_MODE (XEXP (x, 0)))
3576 {
3577 case QImode:
3578 return (1 + (mode == DImode ? 4 : 0)
3579 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
d5b7b3ae 3580
9b66ebb1
PB
3581 case HImode:
3582 return (4 + (mode == DImode ? 4 : 0)
3583 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
d5b7b3ae 3584
9b66ebb1
PB
3585 case SImode:
3586 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3587
d5b7b3ae
RE
3588 default:
3589 return 99;
d5b7b3ae 3590 }
9b66ebb1
PB
3591
3592 default:
3593 return 99;
d5b7b3ae 3594 }
9b66ebb1
PB
3595}
3596
3597
3598/* Worker routine for arm_rtx_costs. */
3599static inline int
3600arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3601{
3602 enum machine_mode mode = GET_MODE (x);
3603 enum rtx_code subcode;
3604 int extra_cost;
3605
e2c671ba
RE
3606 switch (code)
3607 {
3608 case MEM:
3609 /* Memory costs quite a lot for the first word, but subsequent words
3610 load at the equivalent of a single insn each. */
3611 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
48f6efae
NC
3612 + (GET_CODE (x) == SYMBOL_REF
3613 && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
e2c671ba
RE
3614
3615 case DIV:
3616 case MOD:
b9c53150
RS
3617 case UDIV:
3618 case UMOD:
3619 return optimize_size ? COSTS_N_INSNS (2) : 100;
e2c671ba
RE
3620
3621 case ROTATE:
3622 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3623 return 4;
3624 /* Fall through */
3625 case ROTATERT:
3626 if (mode != SImode)
3627 return 8;
3628 /* Fall through */
3629 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3630 if (mode == DImode)
3631 return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3632 + ((GET_CODE (XEXP (x, 0)) == REG
3633 || (GET_CODE (XEXP (x, 0)) == SUBREG
3634 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3635 ? 0 : 8));
3636 return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3637 || (GET_CODE (XEXP (x, 0)) == SUBREG
3638 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3639 ? 0 : 4)
3640 + ((GET_CODE (XEXP (x, 1)) == REG
3641 || (GET_CODE (XEXP (x, 1)) == SUBREG
3642 && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3643 || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3644 ? 0 : 4));
3645
3646 case MINUS:
3647 if (mode == DImode)
3648 return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3649 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3650 || (GET_CODE (XEXP (x, 0)) == CONST_INT
3651 && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3652 ? 0 : 8));
3653
3654 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3655 return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3656 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
9b66ebb1 3657 && arm_const_double_rtx (XEXP (x, 1))))
e2c671ba
RE
3658 ? 0 : 8)
3659 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3660 || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
9b66ebb1 3661 && arm_const_double_rtx (XEXP (x, 0))))
e2c671ba
RE
3662 ? 0 : 8));
3663
3664 if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3665 && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3666 && REG_OR_SUBREG_REG (XEXP (x, 1))))
3667 || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3668 || subcode == ASHIFTRT || subcode == LSHIFTRT
3669 || subcode == ROTATE || subcode == ROTATERT
3670 || (subcode == MULT
3671 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3672 && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3673 (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
3674 && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
3675 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
3676 || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
3677 && REG_OR_SUBREG_REG (XEXP (x, 0))))
3678 return 1;
3679 /* Fall through */
3680
3681 case PLUS:
3682 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3683 return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3684 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3685 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
9b66ebb1 3686 && arm_const_double_rtx (XEXP (x, 1))))
e2c671ba
RE
3687 ? 0 : 8));
3688
3689 /* Fall through */
3690 case AND: case XOR: case IOR:
3691 extra_cost = 0;
3692
3693 /* Normally the frame registers will be spilt into reg+const during
3694 reload, so it is a bad idea to combine them with other instructions,
3695 since then they might not be moved outside of loops. As a compromise
3696 we allow integration with ops that have a constant as their second
3697 operand. */
3698 if ((REG_OR_SUBREG_REG (XEXP (x, 0))
3699 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
3700 && GET_CODE (XEXP (x, 1)) != CONST_INT)
3701 || (REG_OR_SUBREG_REG (XEXP (x, 0))
3702 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
3703 extra_cost = 4;
3704
3705 if (mode == DImode)
3706 return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3707 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3708 || (GET_CODE (XEXP (x, 1)) == CONST_INT
74bbc178 3709 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
e2c671ba
RE
3710 ? 0 : 8));
3711
3712 if (REG_OR_SUBREG_REG (XEXP (x, 0)))
3713 return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
3714 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3715 || (GET_CODE (XEXP (x, 1)) == CONST_INT
74bbc178 3716 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
e2c671ba
RE
3717 ? 0 : 4));
3718
3719 else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
3720 return (1 + extra_cost
3721 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
3722 || subcode == LSHIFTRT || subcode == ASHIFTRT
3723 || subcode == ROTATE || subcode == ROTATERT
3724 || (subcode == MULT
3725 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3726 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
ad076f4e 3727 (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
e2c671ba
RE
3728 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
3729 && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
ad076f4e 3730 || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
e2c671ba
RE
3731 ? 0 : 4));
3732
3733 return 8;
3734
3735 case MULT:
9b66ebb1
PB
3736 /* This should have been handled by the CPU specific routines. */
3737 abort ();
e2c671ba 3738
56636818 3739 case TRUNCATE:
9b66ebb1 3740 if (arm_arch3m && mode == SImode
56636818
JL
3741 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
3742 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
3743 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
3744 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
3745 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
3746 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
3747 return 8;
3748 return 99;
3749
e2c671ba
RE
3750 case NEG:
3751 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3752 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
3753 /* Fall through */
3754 case NOT:
3755 if (mode == DImode)
3756 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3757
3758 return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3759
3760 case IF_THEN_ELSE:
3761 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3762 return 14;
3763 return 2;
3764
3765 case COMPARE:
3766 return 1;
3767
3768 case ABS:
3769 return 4 + (mode == DImode ? 4 : 0);
3770
3771 case SIGN_EXTEND:
3772 if (GET_MODE (XEXP (x, 0)) == QImode)
3773 return (4 + (mode == DImode ? 4 : 0)
3774 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3775 /* Fall through */
3776 case ZERO_EXTEND:
3777 switch (GET_MODE (XEXP (x, 0)))
3778 {
3779 case QImode:
3780 return (1 + (mode == DImode ? 4 : 0)
3781 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3782
3783 case HImode:
3784 return (4 + (mode == DImode ? 4 : 0)
3785 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3786
3787 case SImode:
3788 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
ad076f4e 3789
5a9335ef
NC
3790 case V8QImode:
3791 case V4HImode:
3792 case V2SImode:
3793 case V4QImode:
3794 case V2HImode:
3795 return 1;
3796
ad076f4e
RE
3797 default:
3798 break;
e2c671ba
RE
3799 }
3800 abort ();
3801
d5b7b3ae
RE
3802 case CONST_INT:
3803 if (const_ok_for_arm (INTVAL (x)))
3804 return outer == SET ? 2 : -1;
3805 else if (outer == AND
5895f793 3806 && const_ok_for_arm (~INTVAL (x)))
d5b7b3ae
RE
3807 return -1;
3808 else if ((outer == COMPARE
3809 || outer == PLUS || outer == MINUS)
5895f793 3810 && const_ok_for_arm (-INTVAL (x)))
d5b7b3ae
RE
3811 return -1;
3812 else
3813 return 5;
3814
3815 case CONST:
3816 case LABEL_REF:
3817 case SYMBOL_REF:
3818 return 6;
3819
3820 case CONST_DOUBLE:
9b66ebb1 3821 if (arm_const_double_rtx (x))
d5b7b3ae
RE
3822 return outer == SET ? 2 : -1;
3823 else if ((outer == COMPARE || outer == PLUS)
3b684012 3824 && neg_const_double_rtx_ok_for_fpa (x))
d5b7b3ae
RE
3825 return -1;
3826 return 7;
3827
e2c671ba
RE
3828 default:
3829 return 99;
3830 }
3831}
32de079a 3832
59b9a953 3833/* RTX costs for cores with a slow MUL implementation. */
9b66ebb1 3834
3c50106f 3835static bool
9b66ebb1 3836arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
3c50106f 3837{
9b66ebb1
PB
3838 enum machine_mode mode = GET_MODE (x);
3839
3840 if (TARGET_THUMB)
3841 {
3842 *total = thumb_rtx_costs (x, code, outer_code);
3843 return true;
3844 }
3845
3846 switch (code)
3847 {
3848 case MULT:
3849 if (GET_MODE_CLASS (mode) == MODE_FLOAT
3850 || mode == DImode)
3851 {
3852 *total = 30;
3853 return true;
3854 }
3855
3856 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3857 {
3858 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3859 & (unsigned HOST_WIDE_INT) 0xffffffff);
3860 int cost, const_ok = const_ok_for_arm (i);
3861 int j, booth_unit_size;
3862
3863 /* Tune as appropriate. */
3864 cost = const_ok ? 4 : 8;
3865 booth_unit_size = 2;
3866 for (j = 0; i && j < 32; j += booth_unit_size)
3867 {
3868 i >>= booth_unit_size;
3869 cost += 2;
3870 }
3871
3872 *total = cost;
3873 return true;
3874 }
3875
3876 *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
3877 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
3878 return true;
3879
3880 default:
3881 *total = arm_rtx_costs_1 (x, code, outer_code);
3882 return true;
3883 }
3c50106f
RH
3884}
3885
9b66ebb1
PB
3886
3887/* RTX cost for cores with a fast multiply unit (M variants). */
3888
3889static bool
3890arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
3891{
3892 enum machine_mode mode = GET_MODE (x);
3893
3894 if (TARGET_THUMB)
3895 {
3896 *total = thumb_rtx_costs (x, code, outer_code);
3897 return true;
3898 }
3899
3900 switch (code)
3901 {
3902 case MULT:
3903 /* There is no point basing this on the tuning, since it is always the
3904 fast variant if it exists at all. */
3905 if (mode == DImode
3906 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
3907 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
3908 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
3909 {
3910 *total = 8;
3911 return true;
3912 }
3913
3914
3915 if (GET_MODE_CLASS (mode) == MODE_FLOAT
3916 || mode == DImode)
3917 {
3918 *total = 30;
3919 return true;
3920 }
3921
3922 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3923 {
3924 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3925 & (unsigned HOST_WIDE_INT) 0xffffffff);
3926 int cost, const_ok = const_ok_for_arm (i);
3927 int j, booth_unit_size;
3928
3929 /* Tune as appropriate. */
3930 cost = const_ok ? 4 : 8;
3931 booth_unit_size = 8;
3932 for (j = 0; i && j < 32; j += booth_unit_size)
3933 {
3934 i >>= booth_unit_size;
3935 cost += 2;
3936 }
3937
3938 *total = cost;
3939 return true;
3940 }
3941
3942 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
3943 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
3944 return true;
3945
3946 default:
3947 *total = arm_rtx_costs_1 (x, code, outer_code);
3948 return true;
3949 }
3950}
3951
3952
3953/* RTX cost for XScale CPUs. */
3954
3955static bool
3956arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
3957{
3958 enum machine_mode mode = GET_MODE (x);
3959
3960 if (TARGET_THUMB)
3961 {
3962 *total = thumb_rtx_costs (x, code, outer_code);
3963 return true;
3964 }
3965
3966 switch (code)
3967 {
3968 case MULT:
3969 /* There is no point basing this on the tuning, since it is always the
3970 fast variant if it exists at all. */
3971 if (mode == DImode
3972 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
3973 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
3974 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
3975 {
3976 *total = 8;
3977 return true;
3978 }
3979
3980
3981 if (GET_MODE_CLASS (mode) == MODE_FLOAT
3982 || mode == DImode)
3983 {
3984 *total = 30;
3985 return true;
3986 }
3987
3988 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3989 {
3990 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3991 & (unsigned HOST_WIDE_INT) 0xffffffff);
3992 int cost, const_ok = const_ok_for_arm (i);
3993 unsigned HOST_WIDE_INT masked_const;
3994
3995 /* The cost will be related to two insns.
ff482c8d 3996 First a load of the constant (MOV or LDR), then a multiply. */
9b66ebb1
PB
3997 cost = 2;
3998 if (! const_ok)
3999 cost += 1; /* LDR is probably more expensive because
ff482c8d 4000 of longer result latency. */
9b66ebb1
PB
4001 masked_const = i & 0xffff8000;
4002 if (masked_const != 0 && masked_const != 0xffff8000)
4003 {
4004 masked_const = i & 0xf8000000;
4005 if (masked_const == 0 || masked_const == 0xf8000000)
4006 cost += 1;
4007 else
4008 cost += 2;
4009 }
4010 *total = cost;
4011 return true;
4012 }
4013
4014 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4015 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4016 return true;
4017
4018 default:
4019 *total = arm_rtx_costs_1 (x, code, outer_code);
4020 return true;
4021 }
4022}
4023
4024
4025/* RTX costs for 9e (and later) cores. */
4026
4027static bool
4028arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4029{
4030 enum machine_mode mode = GET_MODE (x);
4031 int nonreg_cost;
4032 int cost;
4033
4034 if (TARGET_THUMB)
4035 {
4036 switch (code)
4037 {
4038 case MULT:
4039 *total = COSTS_N_INSNS (3);
4040 return true;
4041
4042 default:
4043 *total = thumb_rtx_costs (x, code, outer_code);
4044 return true;
4045 }
4046 }
4047
4048 switch (code)
4049 {
4050 case MULT:
4051 /* There is no point basing this on the tuning, since it is always the
4052 fast variant if it exists at all. */
4053 if (mode == DImode
4054 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4055 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4056 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4057 {
4058 *total = 3;
4059 return true;
4060 }
4061
4062
4063 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4064 {
4065 *total = 30;
4066 return true;
4067 }
4068 if (mode == DImode)
4069 {
4070 cost = 7;
4071 nonreg_cost = 8;
4072 }
4073 else
4074 {
4075 cost = 2;
4076 nonreg_cost = 4;
4077 }
4078
4079
4080 *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4081 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4082 return true;
4083
4084 default:
4085 *total = arm_rtx_costs_1 (x, code, outer_code);
4086 return true;
4087 }
4088}
dcefdf67
RH
4089/* All address computations that can be done are free, but rtx cost returns
4090 the same for practically all of them. So we weight the different types
4091 of address here in the order (most pref first):
d6b4baa4 4092 PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL. */
d2b6eb76
ZW
4093static inline int
4094arm_arm_address_cost (rtx x)
4095{
4096 enum rtx_code c = GET_CODE (x);
4097
4098 if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4099 return 0;
4100 if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4101 return 10;
4102
4103 if (c == PLUS || c == MINUS)
4104 {
d2b6eb76
ZW
4105 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4106 return 2;
4107
ec8e098d 4108 if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
d2b6eb76
ZW
4109 return 3;
4110
4111 return 4;
4112 }
4113
4114 return 6;
4115}
4116
4117static inline int
4118arm_thumb_address_cost (rtx x)
4119{
4120 enum rtx_code c = GET_CODE (x);
4121
4122 if (c == REG)
4123 return 1;
4124 if (c == PLUS
4125 && GET_CODE (XEXP (x, 0)) == REG
4126 && GET_CODE (XEXP (x, 1)) == CONST_INT)
4127 return 1;
4128
4129 return 2;
4130}
4131
dcefdf67 4132static int
e32bac5b 4133arm_address_cost (rtx x)
dcefdf67 4134{
d2b6eb76 4135 return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
dcefdf67
RH
4136}
4137
103fc15d 4138static int
e32bac5b 4139arm_use_dfa_pipeline_interface (void)
103fc15d
BE
4140{
4141 return true;
4142}
4143
c237e94a 4144static int
e32bac5b 4145arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
32de079a
RE
4146{
4147 rtx i_pat, d_pat;
4148
d19fb8e3
NC
4149 /* Some true dependencies can have a higher cost depending
4150 on precisely how certain input operands are used. */
4b3c2e48 4151 if (arm_tune_xscale
d19fb8e3 4152 && REG_NOTE_KIND (link) == 0
eda833e3
BE
4153 && recog_memoized (insn) >= 0
4154 && recog_memoized (dep) >= 0)
d19fb8e3
NC
4155 {
4156 int shift_opnum = get_attr_shift (insn);
4157 enum attr_type attr_type = get_attr_type (dep);
4158
4159 /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4160 operand for INSN. If we have a shifted input operand and the
4161 instruction we depend on is another ALU instruction, then we may
4162 have to account for an additional stall. */
9b66ebb1
PB
4163 if (shift_opnum != 0
4164 && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
d19fb8e3
NC
4165 {
4166 rtx shifted_operand;
4167 int opno;
4168
4169 /* Get the shifted operand. */
4170 extract_insn (insn);
4171 shifted_operand = recog_data.operand[shift_opnum];
4172
4173 /* Iterate over all the operands in DEP. If we write an operand
4174 that overlaps with SHIFTED_OPERAND, then we have increase the
4175 cost of this dependency. */
4176 extract_insn (dep);
4177 preprocess_constraints ();
4178 for (opno = 0; opno < recog_data.n_operands; opno++)
4179 {
4180 /* We can ignore strict inputs. */
4181 if (recog_data.operand_type[opno] == OP_IN)
4182 continue;
4183
4184 if (reg_overlap_mentioned_p (recog_data.operand[opno],
4185 shifted_operand))
4186 return 2;
4187 }
4188 }
4189 }
4190
6354dc9b 4191 /* XXX This is not strictly true for the FPA. */
d5b7b3ae
RE
4192 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4193 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
b36ba79f
RE
4194 return 0;
4195
d5b7b3ae
RE
4196 /* Call insns don't incur a stall, even if they follow a load. */
4197 if (REG_NOTE_KIND (link) == 0
4198 && GET_CODE (insn) == CALL_INSN)
4199 return 1;
4200
32de079a
RE
4201 if ((i_pat = single_set (insn)) != NULL
4202 && GET_CODE (SET_SRC (i_pat)) == MEM
4203 && (d_pat = single_set (dep)) != NULL
4204 && GET_CODE (SET_DEST (d_pat)) == MEM)
4205 {
48f6efae 4206 rtx src_mem = XEXP (SET_SRC (i_pat), 0);
32de079a
RE
4207 /* This is a load after a store, there is no conflict if the load reads
4208 from a cached area. Assume that loads from the stack, and from the
4209 constant pool are cached, and that others will miss. This is a
6354dc9b 4210 hack. */
32de079a 4211
48f6efae
NC
4212 if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4213 || reg_mentioned_p (stack_pointer_rtx, src_mem)
4214 || reg_mentioned_p (frame_pointer_rtx, src_mem)
4215 || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
949d79eb 4216 return 1;
32de079a
RE
4217 }
4218
4219 return cost;
4220}
4221
9b66ebb1 4222static int fp_consts_inited = 0;
ff9940b0 4223
9b66ebb1
PB
4224/* Only zero is valid for VFP. Other values are also valid for FPA. */
4225static const char * const strings_fp[8] =
62b10bbc 4226{
2b835d68
RE
4227 "0", "1", "2", "3",
4228 "4", "5", "0.5", "10"
4229};
ff9940b0 4230
9b66ebb1 4231static REAL_VALUE_TYPE values_fp[8];
ff9940b0
RE
4232
4233static void
9b66ebb1 4234init_fp_table (void)
ff9940b0
RE
4235{
4236 int i;
4237 REAL_VALUE_TYPE r;
4238
9b66ebb1
PB
4239 if (TARGET_VFP)
4240 fp_consts_inited = 1;
4241 else
4242 fp_consts_inited = 8;
4243
4244 for (i = 0; i < fp_consts_inited; i++)
ff9940b0 4245 {
9b66ebb1
PB
4246 r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4247 values_fp[i] = r;
ff9940b0 4248 }
ff9940b0
RE
4249}
4250
9b66ebb1 4251/* Return TRUE if rtx X is a valid immediate FP constant. */
cce8749e 4252int
9b66ebb1 4253arm_const_double_rtx (rtx x)
cce8749e 4254{
ff9940b0
RE
4255 REAL_VALUE_TYPE r;
4256 int i;
4257
9b66ebb1
PB
4258 if (!fp_consts_inited)
4259 init_fp_table ();
ff9940b0
RE
4260
4261 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4262 if (REAL_VALUE_MINUS_ZERO (r))
4263 return 0;
f3bb6135 4264
9b66ebb1
PB
4265 for (i = 0; i < fp_consts_inited; i++)
4266 if (REAL_VALUES_EQUAL (r, values_fp[i]))
ff9940b0 4267 return 1;
f3bb6135 4268
ff9940b0 4269 return 0;
f3bb6135 4270}
ff9940b0 4271
3b684012 4272/* Return TRUE if rtx X is a valid immediate FPA constant. */
ff9940b0 4273int
e32bac5b 4274neg_const_double_rtx_ok_for_fpa (rtx x)
ff9940b0
RE
4275{
4276 REAL_VALUE_TYPE r;
4277 int i;
4278
9b66ebb1
PB
4279 if (!fp_consts_inited)
4280 init_fp_table ();
ff9940b0
RE
4281
4282 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4283 r = REAL_VALUE_NEGATE (r);
4284 if (REAL_VALUE_MINUS_ZERO (r))
4285 return 0;
f3bb6135 4286
ff9940b0 4287 for (i = 0; i < 8; i++)
9b66ebb1 4288 if (REAL_VALUES_EQUAL (r, values_fp[i]))
ff9940b0 4289 return 1;
f3bb6135 4290
ff9940b0 4291 return 0;
f3bb6135 4292}
cce8749e
CH
4293\f
4294/* Predicates for `match_operand' and `match_operator'. */
4295
ff9940b0 4296/* s_register_operand is the same as register_operand, but it doesn't accept
56a38cec
DE
4297 (SUBREG (MEM)...).
4298
4299 This function exists because at the time it was put in it led to better
4300 code. SUBREG(MEM) always needs a reload in the places where
4301 s_register_operand is used, and this seemed to lead to excessive
4302 reloading. */
ff9940b0 4303int
e32bac5b 4304s_register_operand (rtx op, enum machine_mode mode)
ff9940b0
RE
4305{
4306 if (GET_MODE (op) != mode && mode != VOIDmode)
4307 return 0;
4308
4309 if (GET_CODE (op) == SUBREG)
f3bb6135 4310 op = SUBREG_REG (op);
ff9940b0
RE
4311
4312 /* We don't consider registers whose class is NO_REGS
4313 to be a register operand. */
d5b7b3ae 4314 /* XXX might have to check for lo regs only for thumb ??? */
ff9940b0
RE
4315 return (GET_CODE (op) == REG
4316 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4317 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
4318}
4319
b0888988
RE
4320/* A hard register operand (even before reload. */
4321int
e32bac5b 4322arm_hard_register_operand (rtx op, enum machine_mode mode)
b0888988
RE
4323{
4324 if (GET_MODE (op) != mode && mode != VOIDmode)
4325 return 0;
4326
4327 return (GET_CODE (op) == REG
4328 && REGNO (op) < FIRST_PSEUDO_REGISTER);
4329}
4330
9b66ebb1
PB
4331/* An arm register operand. */
4332int
4333arm_general_register_operand (rtx op, enum machine_mode mode)
4334{
4335 if (GET_MODE (op) != mode && mode != VOIDmode)
4336 return 0;
4337
4338 if (GET_CODE (op) == SUBREG)
4339 op = SUBREG_REG (op);
4340
4341 return (GET_CODE (op) == REG
4342 && (REGNO (op) <= LAST_ARM_REGNUM
4343 || REGNO (op) >= FIRST_PSEUDO_REGISTER));
4344}
4345
e2c671ba 4346/* Only accept reg, subreg(reg), const_int. */
e2c671ba 4347int
e32bac5b 4348reg_or_int_operand (rtx op, enum machine_mode mode)
e2c671ba
RE
4349{
4350 if (GET_CODE (op) == CONST_INT)
4351 return 1;
4352
4353 if (GET_MODE (op) != mode && mode != VOIDmode)
4354 return 0;
4355
4356 if (GET_CODE (op) == SUBREG)
4357 op = SUBREG_REG (op);
4358
4359 /* We don't consider registers whose class is NO_REGS
4360 to be a register operand. */
4361 return (GET_CODE (op) == REG
4362 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4363 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
4364}
4365
ff9940b0 4366/* Return 1 if OP is an item in memory, given that we are in reload. */
ff9940b0 4367int
e32bac5b 4368arm_reload_memory_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
ff9940b0
RE
4369{
4370 int regno = true_regnum (op);
4371
5895f793 4372 return (!CONSTANT_P (op)
ff9940b0
RE
4373 && (regno == -1
4374 || (GET_CODE (op) == REG
4375 && REGNO (op) >= FIRST_PSEUDO_REGISTER)));
4376}
4377
cce8749e 4378/* Return TRUE for valid operands for the rhs of an ARM instruction. */
cce8749e 4379int
e32bac5b 4380arm_rhs_operand (rtx op, enum machine_mode mode)
cce8749e 4381{
ff9940b0 4382 return (s_register_operand (op, mode)
cce8749e 4383 || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op))));
f3bb6135 4384}
cce8749e 4385
1d6e90ac
NC
4386/* Return TRUE for valid operands for the
4387 rhs of an ARM instruction, or a load. */
ff9940b0 4388int
e32bac5b 4389arm_rhsm_operand (rtx op, enum machine_mode mode)
ff9940b0
RE
4390{
4391 return (s_register_operand (op, mode)
4392 || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op)))
4393 || memory_operand (op, mode));
f3bb6135 4394}
ff9940b0
RE
4395
4396/* Return TRUE for valid operands for the rhs of an ARM instruction, or if a
4397 constant that is valid when negated. */
ff9940b0 4398int
e32bac5b 4399arm_add_operand (rtx op, enum machine_mode mode)
ff9940b0 4400{
d5b7b3ae
RE
4401 if (TARGET_THUMB)
4402 return thumb_cmp_operand (op, mode);
4403
ff9940b0
RE
4404 return (s_register_operand (op, mode)
4405 || (GET_CODE (op) == CONST_INT
4406 && (const_ok_for_arm (INTVAL (op))
4407 || const_ok_for_arm (-INTVAL (op)))));
f3bb6135 4408}
ff9940b0 4409
f9b9980e
RE
4410/* Return TRUE for valid ARM constants (or when valid if negated). */
4411int
91de08c3 4412arm_addimm_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
f9b9980e
RE
4413{
4414 return (GET_CODE (op) == CONST_INT
4415 && (const_ok_for_arm (INTVAL (op))
4416 || const_ok_for_arm (-INTVAL (op))));
4417}
4418
ff9940b0 4419int
e32bac5b 4420arm_not_operand (rtx op, enum machine_mode mode)
ff9940b0
RE
4421{
4422 return (s_register_operand (op, mode)
4423 || (GET_CODE (op) == CONST_INT
4424 && (const_ok_for_arm (INTVAL (op))
4425 || const_ok_for_arm (~INTVAL (op)))));
f3bb6135 4426}
ff9940b0 4427
5165176d
RE
4428/* Return TRUE if the operand is a memory reference which contains an
4429 offsettable address. */
4430int
e32bac5b 4431offsettable_memory_operand (rtx op, enum machine_mode mode)
5165176d
RE
4432{
4433 if (mode == VOIDmode)
4434 mode = GET_MODE (op);
4435
4436 return (mode == GET_MODE (op)
4437 && GET_CODE (op) == MEM
4438 && offsettable_address_p (reload_completed | reload_in_progress,
4439 mode, XEXP (op, 0)));
4440}
4441
4442/* Return TRUE if the operand is a memory reference which is, or can be
4443 made word aligned by adjusting the offset. */
4444int
e32bac5b 4445alignable_memory_operand (rtx op, enum machine_mode mode)
5165176d
RE
4446{
4447 rtx reg;
4448
4449 if (mode == VOIDmode)
4450 mode = GET_MODE (op);
4451
4452 if (mode != GET_MODE (op) || GET_CODE (op) != MEM)
4453 return 0;
4454
4455 op = XEXP (op, 0);
4456
4457 return ((GET_CODE (reg = op) == REG
4458 || (GET_CODE (op) == SUBREG
4459 && GET_CODE (reg = SUBREG_REG (op)) == REG)
4460 || (GET_CODE (op) == PLUS
4461 && GET_CODE (XEXP (op, 1)) == CONST_INT
4462 && (GET_CODE (reg = XEXP (op, 0)) == REG
4463 || (GET_CODE (XEXP (op, 0)) == SUBREG
4464 && GET_CODE (reg = SUBREG_REG (XEXP (op, 0))) == REG))))
bdb429a5 4465 && REGNO_POINTER_ALIGN (REGNO (reg)) >= 32);
5165176d
RE
4466}
4467
b111229a
RE
4468/* Similar to s_register_operand, but does not allow hard integer
4469 registers. */
4470int
e32bac5b 4471f_register_operand (rtx op, enum machine_mode mode)
b111229a
RE
4472{
4473 if (GET_MODE (op) != mode && mode != VOIDmode)
4474 return 0;
4475
4476 if (GET_CODE (op) == SUBREG)
4477 op = SUBREG_REG (op);
4478
4479 /* We don't consider registers whose class is NO_REGS
4480 to be a register operand. */
4481 return (GET_CODE (op) == REG
4482 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
3b684012 4483 || REGNO_REG_CLASS (REGNO (op)) == FPA_REGS));
b111229a
RE
4484}
4485
9b66ebb1
PB
4486/* Return TRUE for valid operands for the rhs of an floating point insns.
4487 Allows regs or certain consts on FPA, just regs for everything else. */
cce8749e 4488int
9b66ebb1 4489arm_float_rhs_operand (rtx op, enum machine_mode mode)
cce8749e 4490{
ff9940b0 4491 if (s_register_operand (op, mode))
f3bb6135 4492 return TRUE;
9ce71c6f
BS
4493
4494 if (GET_MODE (op) != mode && mode != VOIDmode)
4495 return FALSE;
4496
9b66ebb1
PB
4497 if (TARGET_FPA && GET_CODE (op) == CONST_DOUBLE)
4498 return arm_const_double_rtx (op);
f3bb6135
RE
4499
4500 return FALSE;
4501}
cce8749e 4502
ff9940b0 4503int
9b66ebb1 4504arm_float_add_operand (rtx op, enum machine_mode mode)
ff9940b0
RE
4505{
4506 if (s_register_operand (op, mode))
f3bb6135 4507 return TRUE;
9ce71c6f
BS
4508
4509 if (GET_MODE (op) != mode && mode != VOIDmode)
4510 return FALSE;
4511
9b66ebb1
PB
4512 if (TARGET_FPA && GET_CODE (op) == CONST_DOUBLE)
4513 return (arm_const_double_rtx (op)
3b684012 4514 || neg_const_double_rtx_ok_for_fpa (op));
f3bb6135
RE
4515
4516 return FALSE;
ff9940b0
RE
4517}
4518
9b66ebb1
PB
4519
4520/* Return TRUE if OP is suitable for the rhs of a floating point comparison.
4521 Depends which fpu we are targeting. */
4522
4523int
4524arm_float_compare_operand (rtx op, enum machine_mode mode)
4525{
4526 if (TARGET_VFP)
4527 return vfp_compare_operand (op, mode);
4528 else
4529 return arm_float_rhs_operand (op, mode);
4530}
4531
4532
9b6b54e2 4533/* Return nonzero if OP is a valid Cirrus memory address pattern. */
9b6b54e2 4534int
e32bac5b 4535cirrus_memory_offset (rtx op)
9b6b54e2
NC
4536{
4537 /* Reject eliminable registers. */
4538 if (! (reload_in_progress || reload_completed)
4539 && ( reg_mentioned_p (frame_pointer_rtx, op)
4540 || reg_mentioned_p (arg_pointer_rtx, op)
4541 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4542 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4543 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4544 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4545 return 0;
4546
4547 if (GET_CODE (op) == MEM)
4548 {
4549 rtx ind;
4550
4551 ind = XEXP (op, 0);
4552
4553 /* Match: (mem (reg)). */
4554 if (GET_CODE (ind) == REG)
4555 return 1;
4556
4557 /* Match:
4558 (mem (plus (reg)
4559 (const))). */
4560 if (GET_CODE (ind) == PLUS
4561 && GET_CODE (XEXP (ind, 0)) == REG
4562 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4563 && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4564 return 1;
4565 }
4566
4567 return 0;
4568}
4569
1e1ab407
RE
4570int
4571arm_extendqisi_mem_op (rtx op, enum machine_mode mode)
4572{
4573 if (!memory_operand (op, mode))
4574 return 0;
4575
4576 return arm_legitimate_address_p (mode, XEXP (op, 0), SIGN_EXTEND, 0);
4577}
4578
9b6b54e2 4579/* Return nonzero if OP is a Cirrus or general register. */
9b6b54e2 4580int
e32bac5b 4581cirrus_register_operand (rtx op, enum machine_mode mode)
9b6b54e2
NC
4582{
4583 if (GET_MODE (op) != mode && mode != VOIDmode)
4584 return FALSE;
4585
4586 if (GET_CODE (op) == SUBREG)
4587 op = SUBREG_REG (op);
4588
4589 return (GET_CODE (op) == REG
4590 && (REGNO_REG_CLASS (REGNO (op)) == CIRRUS_REGS
4591 || REGNO_REG_CLASS (REGNO (op)) == GENERAL_REGS));
4592}
4593
4594/* Return nonzero if OP is a cirrus FP register. */
9b6b54e2 4595int
e32bac5b 4596cirrus_fp_register (rtx op, enum machine_mode mode)
9b6b54e2
NC
4597{
4598 if (GET_MODE (op) != mode && mode != VOIDmode)
4599 return FALSE;
4600
4601 if (GET_CODE (op) == SUBREG)
4602 op = SUBREG_REG (op);
4603
4604 return (GET_CODE (op) == REG
4605 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4606 || REGNO_REG_CLASS (REGNO (op)) == CIRRUS_REGS));
4607}
4608
4609/* Return nonzero if OP is a 6bit constant (0..63). */
9b6b54e2 4610int
e32bac5b 4611cirrus_shift_const (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
9b6b54e2
NC
4612{
4613 return (GET_CODE (op) == CONST_INT
4614 && INTVAL (op) >= 0
4615 && INTVAL (op) < 64);
4616}
4617
9b66ebb1
PB
4618
4619/* Return TRUE if OP is a valid VFP memory address pattern. */
4620/* Copied from cirrus_memory_offset but with restricted offset range. */
4621
4622int
4623vfp_mem_operand (rtx op)
4624{
4625 /* Reject eliminable registers. */
4626
4627 if (! (reload_in_progress || reload_completed)
4628 && ( reg_mentioned_p (frame_pointer_rtx, op)
4629 || reg_mentioned_p (arg_pointer_rtx, op)
4630 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4631 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4632 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4633 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4634 return FALSE;
4635
59b9a953 4636 /* Constants are converted into offsets from labels. */
9b66ebb1
PB
4637 if (GET_CODE (op) == MEM)
4638 {
4639 rtx ind;
4640
4641 ind = XEXP (op, 0);
4642
4643 if (reload_completed
4644 && (GET_CODE (ind) == LABEL_REF
4645 || (GET_CODE (ind) == CONST
4646 && GET_CODE (XEXP (ind, 0)) == PLUS
4647 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4648 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4649 return TRUE;
4650
4651 /* Match: (mem (reg)). */
4652 if (GET_CODE (ind) == REG)
4653 return arm_address_register_rtx_p (ind, 0);
4654
4655 /* Match:
4656 (mem (plus (reg)
4657 (const))). */
4658 if (GET_CODE (ind) == PLUS
4659 && GET_CODE (XEXP (ind, 0)) == REG
4660 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4661 && GET_CODE (XEXP (ind, 1)) == CONST_INT
4662 && INTVAL (XEXP (ind, 1)) > -1024
4663 && INTVAL (XEXP (ind, 1)) < 1024)
4664 return TRUE;
4665 }
4666
4667 return FALSE;
4668}
4669
4670
4671/* Return TRUE if OP is a REG or constant zero. */
4672int
4673vfp_compare_operand (rtx op, enum machine_mode mode)
4674{
4675 if (s_register_operand (op, mode))
4676 return TRUE;
4677
4678 return (GET_CODE (op) == CONST_DOUBLE
4679 && arm_const_double_rtx (op));
4680}
4681
4682
4683/* Return GENERAL_REGS if a scratch register required to reload x to/from
4684 VFP registers. Otherwise return NO_REGS. */
4685
4686enum reg_class
4687vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4688{
4689 if (vfp_mem_operand (x) || s_register_operand (x, mode))
4690 return NO_REGS;
4691
4692 return GENERAL_REGS;
4693}
4694
4695
f0375c66
NC
4696/* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4697 Use by the Cirrus Maverick code which has to workaround
4698 a hardware bug triggered by such instructions. */
f0375c66 4699static bool
e32bac5b 4700arm_memory_load_p (rtx insn)
9b6b54e2
NC
4701{
4702 rtx body, lhs, rhs;;
4703
f0375c66
NC
4704 if (insn == NULL_RTX || GET_CODE (insn) != INSN)
4705 return false;
9b6b54e2
NC
4706
4707 body = PATTERN (insn);
4708
4709 if (GET_CODE (body) != SET)
f0375c66 4710 return false;
9b6b54e2
NC
4711
4712 lhs = XEXP (body, 0);
4713 rhs = XEXP (body, 1);
4714
f0375c66
NC
4715 lhs = REG_OR_SUBREG_RTX (lhs);
4716
4717 /* If the destination is not a general purpose
4718 register we do not have to worry. */
4719 if (GET_CODE (lhs) != REG
4720 || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
4721 return false;
4722
4723 /* As well as loads from memory we also have to react
4724 to loads of invalid constants which will be turned
4725 into loads from the minipool. */
4726 return (GET_CODE (rhs) == MEM
4727 || GET_CODE (rhs) == SYMBOL_REF
4728 || note_invalid_constants (insn, -1, false));
9b6b54e2
NC
4729}
4730
f0375c66 4731/* Return TRUE if INSN is a Cirrus instruction. */
f0375c66 4732static bool
e32bac5b 4733arm_cirrus_insn_p (rtx insn)
9b6b54e2
NC
4734{
4735 enum attr_cirrus attr;
4736
4737 /* get_attr aborts on USE and CLOBBER. */
4738 if (!insn
4739 || GET_CODE (insn) != INSN
4740 || GET_CODE (PATTERN (insn)) == USE
4741 || GET_CODE (PATTERN (insn)) == CLOBBER)
4742 return 0;
4743
4744 attr = get_attr_cirrus (insn);
4745
f0375c66 4746 return attr != CIRRUS_NOT;
9b6b54e2
NC
4747}
4748
4749/* Cirrus reorg for invalid instruction combinations. */
9b6b54e2 4750static void
e32bac5b 4751cirrus_reorg (rtx first)
9b6b54e2
NC
4752{
4753 enum attr_cirrus attr;
4754 rtx body = PATTERN (first);
4755 rtx t;
4756 int nops;
4757
4758 /* Any branch must be followed by 2 non Cirrus instructions. */
4759 if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
4760 {
4761 nops = 0;
4762 t = next_nonnote_insn (first);
4763
f0375c66 4764 if (arm_cirrus_insn_p (t))
9b6b54e2
NC
4765 ++ nops;
4766
f0375c66 4767 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9b6b54e2
NC
4768 ++ nops;
4769
4770 while (nops --)
4771 emit_insn_after (gen_nop (), first);
4772
4773 return;
4774 }
4775
4776 /* (float (blah)) is in parallel with a clobber. */
4777 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
4778 body = XVECEXP (body, 0, 0);
4779
4780 if (GET_CODE (body) == SET)
4781 {
4782 rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
4783
4784 /* cfldrd, cfldr64, cfstrd, cfstr64 must
4785 be followed by a non Cirrus insn. */
4786 if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
4787 {
f0375c66 4788 if (arm_cirrus_insn_p (next_nonnote_insn (first)))
9b6b54e2
NC
4789 emit_insn_after (gen_nop (), first);
4790
4791 return;
4792 }
f0375c66 4793 else if (arm_memory_load_p (first))
9b6b54e2
NC
4794 {
4795 unsigned int arm_regno;
4796
4797 /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
4798 ldr/cfmv64hr combination where the Rd field is the same
4799 in both instructions must be split with a non Cirrus
4800 insn. Example:
4801
4802 ldr r0, blah
4803 nop
4804 cfmvsr mvf0, r0. */
4805
4806 /* Get Arm register number for ldr insn. */
4807 if (GET_CODE (lhs) == REG)
4808 arm_regno = REGNO (lhs);
4809 else if (GET_CODE (rhs) == REG)
4810 arm_regno = REGNO (rhs);
4811 else
4812 abort ();
4813
4814 /* Next insn. */
4815 first = next_nonnote_insn (first);
4816
f0375c66 4817 if (! arm_cirrus_insn_p (first))
9b6b54e2
NC
4818 return;
4819
4820 body = PATTERN (first);
4821
4822 /* (float (blah)) is in parallel with a clobber. */
4823 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
4824 body = XVECEXP (body, 0, 0);
4825
4826 if (GET_CODE (body) == FLOAT)
4827 body = XEXP (body, 0);
4828
4829 if (get_attr_cirrus (first) == CIRRUS_MOVE
4830 && GET_CODE (XEXP (body, 1)) == REG
4831 && arm_regno == REGNO (XEXP (body, 1)))
4832 emit_insn_after (gen_nop (), first);
4833
4834 return;
4835 }
4836 }
4837
4838 /* get_attr aborts on USE and CLOBBER. */
4839 if (!first
4840 || GET_CODE (first) != INSN
4841 || GET_CODE (PATTERN (first)) == USE
4842 || GET_CODE (PATTERN (first)) == CLOBBER)
4843 return;
4844
4845 attr = get_attr_cirrus (first);
4846
4847 /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
4848 must be followed by a non-coprocessor instruction. */
4849 if (attr == CIRRUS_COMPARE)
4850 {
4851 nops = 0;
4852
4853 t = next_nonnote_insn (first);
4854
f0375c66 4855 if (arm_cirrus_insn_p (t))
9b6b54e2
NC
4856 ++ nops;
4857
f0375c66 4858 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
9b6b54e2
NC
4859 ++ nops;
4860
4861 while (nops --)
4862 emit_insn_after (gen_nop (), first);
4863
4864 return;
4865 }
4866}
4867
cce8749e 4868/* Return nonzero if OP is a constant power of two. */
cce8749e 4869int
e32bac5b 4870power_of_two_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
cce8749e
CH
4871{
4872 if (GET_CODE (op) == CONST_INT)
4873 {
d5b7b3ae 4874 HOST_WIDE_INT value = INTVAL (op);
1d6e90ac 4875
f3bb6135 4876 return value != 0 && (value & (value - 1)) == 0;
cce8749e 4877 }
1d6e90ac 4878
f3bb6135
RE
4879 return FALSE;
4880}
cce8749e
CH
4881
4882/* Return TRUE for a valid operand of a DImode operation.
e9c6b69b 4883 Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
ff9940b0
RE
4884 Note that this disallows MEM(REG+REG), but allows
4885 MEM(PRE/POST_INC/DEC(REG)). */
cce8749e 4886int
e32bac5b 4887di_operand (rtx op, enum machine_mode mode)
cce8749e 4888{
ff9940b0 4889 if (s_register_operand (op, mode))
f3bb6135 4890 return TRUE;
cce8749e 4891
9ce71c6f
BS
4892 if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
4893 return FALSE;
4894
e9c6b69b
NC
4895 if (GET_CODE (op) == SUBREG)
4896 op = SUBREG_REG (op);
4897
cce8749e
CH
4898 switch (GET_CODE (op))
4899 {
4900 case CONST_DOUBLE:
4901 case CONST_INT:
f3bb6135
RE
4902 return TRUE;
4903
cce8749e 4904 case MEM:
f3bb6135
RE
4905 return memory_address_p (DImode, XEXP (op, 0));
4906
cce8749e 4907 default:
f3bb6135 4908 return FALSE;
cce8749e 4909 }
f3bb6135 4910}
cce8749e 4911
d5b7b3ae
RE
4912/* Like di_operand, but don't accept constants. */
4913int
e32bac5b 4914nonimmediate_di_operand (rtx op, enum machine_mode mode)
d5b7b3ae
RE
4915{
4916 if (s_register_operand (op, mode))
4917 return TRUE;
4918
4919 if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
4920 return FALSE;
4921
4922 if (GET_CODE (op) == SUBREG)
4923 op = SUBREG_REG (op);
4924
4925 if (GET_CODE (op) == MEM)
4926 return memory_address_p (DImode, XEXP (op, 0));
4927
4928 return FALSE;
4929}
4930
9b66ebb1 4931/* Return TRUE for a valid operand of a DFmode operation when soft-float.
e9c6b69b 4932 Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
f3139301
DE
4933 Note that this disallows MEM(REG+REG), but allows
4934 MEM(PRE/POST_INC/DEC(REG)). */
f3139301 4935int
e32bac5b 4936soft_df_operand (rtx op, enum machine_mode mode)
f3139301
DE
4937{
4938 if (s_register_operand (op, mode))
4b02997f 4939 return TRUE;
f3139301 4940
9ce71c6f
BS
4941 if (mode != VOIDmode && GET_MODE (op) != mode)
4942 return FALSE;
4943
37b80d2e
BS
4944 if (GET_CODE (op) == SUBREG && CONSTANT_P (SUBREG_REG (op)))
4945 return FALSE;
4946
e9c6b69b
NC
4947 if (GET_CODE (op) == SUBREG)
4948 op = SUBREG_REG (op);
9ce71c6f 4949
f3139301
DE
4950 switch (GET_CODE (op))
4951 {
4952 case CONST_DOUBLE:
4953 return TRUE;
4954
4955 case MEM:
4956 return memory_address_p (DFmode, XEXP (op, 0));
4957
4958 default:
4959 return FALSE;
4960 }
4961}
4962
d5b7b3ae
RE
4963/* Like soft_df_operand, but don't accept constants. */
4964int
e32bac5b 4965nonimmediate_soft_df_operand (rtx op, enum machine_mode mode)
d5b7b3ae
RE
4966{
4967 if (s_register_operand (op, mode))
4b02997f 4968 return TRUE;
d5b7b3ae
RE
4969
4970 if (mode != VOIDmode && GET_MODE (op) != mode)
4971 return FALSE;
4972
4973 if (GET_CODE (op) == SUBREG)
4974 op = SUBREG_REG (op);
4975
4976 if (GET_CODE (op) == MEM)
4977 return memory_address_p (DFmode, XEXP (op, 0));
4978 return FALSE;
4979}
cce8749e 4980
d5b7b3ae 4981/* Return TRUE for valid index operands. */
cce8749e 4982int
e32bac5b 4983index_operand (rtx op, enum machine_mode mode)
cce8749e 4984{
d5b7b3ae 4985 return (s_register_operand (op, mode)
ff9940b0 4986 || (immediate_operand (op, mode)
d5b7b3ae
RE
4987 && (GET_CODE (op) != CONST_INT
4988 || (INTVAL (op) < 4096 && INTVAL (op) > -4096))));
f3bb6135 4989}
cce8749e 4990
ff9940b0
RE
4991/* Return TRUE for valid shifts by a constant. This also accepts any
4992 power of two on the (somewhat overly relaxed) assumption that the
6354dc9b 4993 shift operator in this case was a mult. */
ff9940b0 4994int
e32bac5b 4995const_shift_operand (rtx op, enum machine_mode mode)
ff9940b0
RE
4996{
4997 return (power_of_two_operand (op, mode)
4998 || (immediate_operand (op, mode)
d5b7b3ae
RE
4999 && (GET_CODE (op) != CONST_INT
5000 || (INTVAL (op) < 32 && INTVAL (op) > 0))));
f3bb6135 5001}
ff9940b0 5002
cce8749e
CH
5003/* Return TRUE for arithmetic operators which can be combined with a multiply
5004 (shift). */
cce8749e 5005int
e32bac5b 5006shiftable_operator (rtx x, enum machine_mode mode)
cce8749e 5007{
1d6e90ac
NC
5008 enum rtx_code code;
5009
cce8749e
CH
5010 if (GET_MODE (x) != mode)
5011 return FALSE;
cce8749e 5012
1d6e90ac
NC
5013 code = GET_CODE (x);
5014
5015 return (code == PLUS || code == MINUS
5016 || code == IOR || code == XOR || code == AND);
f3bb6135 5017}
cce8749e 5018
6ab589e0 5019/* Return TRUE for binary logical operators. */
6ab589e0 5020int
e32bac5b 5021logical_binary_operator (rtx x, enum machine_mode mode)
6ab589e0 5022{
1d6e90ac
NC
5023 enum rtx_code code;
5024
6ab589e0
JL
5025 if (GET_MODE (x) != mode)
5026 return FALSE;
6ab589e0 5027
1d6e90ac
NC
5028 code = GET_CODE (x);
5029
5030 return (code == IOR || code == XOR || code == AND);
6ab589e0
JL
5031}
5032
6354dc9b 5033/* Return TRUE for shift operators. */
cce8749e 5034int
e32bac5b 5035shift_operator (rtx x,enum machine_mode mode)
cce8749e 5036{
1d6e90ac
NC
5037 enum rtx_code code;
5038
cce8749e
CH
5039 if (GET_MODE (x) != mode)
5040 return FALSE;
cce8749e 5041
1d6e90ac 5042 code = GET_CODE (x);
f3bb6135 5043
1d6e90ac
NC
5044 if (code == MULT)
5045 return power_of_two_operand (XEXP (x, 1), mode);
5046
5047 return (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT
5048 || code == ROTATERT);
f3bb6135 5049}
ff9940b0 5050
6354dc9b
NC
5051/* Return TRUE if x is EQ or NE. */
5052int
e32bac5b 5053equality_operator (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
ff9940b0 5054{
f3bb6135 5055 return GET_CODE (x) == EQ || GET_CODE (x) == NE;
ff9940b0
RE
5056}
5057
e45b72c4
RE
5058/* Return TRUE if x is a comparison operator other than LTGT or UNEQ. */
5059int
e32bac5b 5060arm_comparison_operator (rtx x, enum machine_mode mode)
e45b72c4
RE
5061{
5062 return (comparison_operator (x, mode)
5063 && GET_CODE (x) != LTGT
5064 && GET_CODE (x) != UNEQ);
5065}
5066
6354dc9b 5067/* Return TRUE for SMIN SMAX UMIN UMAX operators. */
ff9940b0 5068int
e32bac5b 5069minmax_operator (rtx x, enum machine_mode mode)
ff9940b0
RE
5070{
5071 enum rtx_code code = GET_CODE (x);
5072
5073 if (GET_MODE (x) != mode)
5074 return FALSE;
f3bb6135 5075
ff9940b0 5076 return code == SMIN || code == SMAX || code == UMIN || code == UMAX;
f3bb6135 5077}
ff9940b0 5078
ff9940b0 5079/* Return TRUE if this is the condition code register, if we aren't given
6354dc9b 5080 a mode, accept any class CCmode register. */
ff9940b0 5081int
e32bac5b 5082cc_register (rtx x, enum machine_mode mode)
ff9940b0
RE
5083{
5084 if (mode == VOIDmode)
5085 {
5086 mode = GET_MODE (x);
d5b7b3ae 5087
ff9940b0
RE
5088 if (GET_MODE_CLASS (mode) != MODE_CC)
5089 return FALSE;
5090 }
f3bb6135 5091
d5b7b3ae
RE
5092 if ( GET_MODE (x) == mode
5093 && GET_CODE (x) == REG
5094 && REGNO (x) == CC_REGNUM)
ff9940b0 5095 return TRUE;
f3bb6135 5096
ff9940b0
RE
5097 return FALSE;
5098}
5bbe2d40
RE
5099
5100/* Return TRUE if this is the condition code register, if we aren't given
84ed5e79
RE
5101 a mode, accept any class CCmode register which indicates a dominance
5102 expression. */
5bbe2d40 5103int
e32bac5b 5104dominant_cc_register (rtx x, enum machine_mode mode)
5bbe2d40
RE
5105{
5106 if (mode == VOIDmode)
5107 {
5108 mode = GET_MODE (x);
d5b7b3ae 5109
84ed5e79 5110 if (GET_MODE_CLASS (mode) != MODE_CC)
5bbe2d40
RE
5111 return FALSE;
5112 }
5113
e32bac5b 5114 if (mode != CC_DNEmode && mode != CC_DEQmode
84ed5e79
RE
5115 && mode != CC_DLEmode && mode != CC_DLTmode
5116 && mode != CC_DGEmode && mode != CC_DGTmode
5117 && mode != CC_DLEUmode && mode != CC_DLTUmode
5118 && mode != CC_DGEUmode && mode != CC_DGTUmode)
5119 return FALSE;
5120
d5b7b3ae 5121 return cc_register (x, mode);
5bbe2d40
RE
5122}
5123
2b835d68
RE
5124/* Return TRUE if X references a SYMBOL_REF. */
5125int
e32bac5b 5126symbol_mentioned_p (rtx x)
2b835d68 5127{
1d6e90ac
NC
5128 const char * fmt;
5129 int i;
2b835d68
RE
5130
5131 if (GET_CODE (x) == SYMBOL_REF)
5132 return 1;
5133
5134 fmt = GET_RTX_FORMAT (GET_CODE (x));
d5b7b3ae 5135
2b835d68
RE
5136 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5137 {
5138 if (fmt[i] == 'E')
5139 {
1d6e90ac 5140 int j;
2b835d68
RE
5141
5142 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5143 if (symbol_mentioned_p (XVECEXP (x, i, j)))
5144 return 1;
5145 }
5146 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5147 return 1;
5148 }
5149
5150 return 0;
5151}
5152
5153/* Return TRUE if X references a LABEL_REF. */
5154int
e32bac5b 5155label_mentioned_p (rtx x)
2b835d68 5156{
1d6e90ac
NC
5157 const char * fmt;
5158 int i;
2b835d68
RE
5159
5160 if (GET_CODE (x) == LABEL_REF)
5161 return 1;
5162
5163 fmt = GET_RTX_FORMAT (GET_CODE (x));
5164 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5165 {
5166 if (fmt[i] == 'E')
5167 {
1d6e90ac 5168 int j;
2b835d68
RE
5169
5170 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5171 if (label_mentioned_p (XVECEXP (x, i, j)))
5172 return 1;
5173 }
5174 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5175 return 1;
5176 }
5177
5178 return 0;
5179}
5180
ff9940b0 5181enum rtx_code
e32bac5b 5182minmax_code (rtx x)
ff9940b0
RE
5183{
5184 enum rtx_code code = GET_CODE (x);
5185
5186 if (code == SMAX)
5187 return GE;
f3bb6135 5188 else if (code == SMIN)
ff9940b0 5189 return LE;
f3bb6135 5190 else if (code == UMIN)
ff9940b0 5191 return LEU;
f3bb6135 5192 else if (code == UMAX)
ff9940b0 5193 return GEU;
f3bb6135 5194
ff9940b0
RE
5195 abort ();
5196}
5197
6354dc9b 5198/* Return 1 if memory locations are adjacent. */
f3bb6135 5199int
e32bac5b 5200adjacent_mem_locations (rtx a, rtx b)
ff9940b0 5201{
ff9940b0
RE
5202 if ((GET_CODE (XEXP (a, 0)) == REG
5203 || (GET_CODE (XEXP (a, 0)) == PLUS
5204 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5205 && (GET_CODE (XEXP (b, 0)) == REG
5206 || (GET_CODE (XEXP (b, 0)) == PLUS
5207 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5208 {
1d6e90ac
NC
5209 int val0 = 0, val1 = 0;
5210 int reg0, reg1;
5211
ff9940b0
RE
5212 if (GET_CODE (XEXP (a, 0)) == PLUS)
5213 {
1d6e90ac 5214 reg0 = REGNO (XEXP (XEXP (a, 0), 0));
ff9940b0
RE
5215 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5216 }
5217 else
5218 reg0 = REGNO (XEXP (a, 0));
1d6e90ac 5219
ff9940b0
RE
5220 if (GET_CODE (XEXP (b, 0)) == PLUS)
5221 {
1d6e90ac 5222 reg1 = REGNO (XEXP (XEXP (b, 0), 0));
ff9940b0
RE
5223 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5224 }
5225 else
5226 reg1 = REGNO (XEXP (b, 0));
1d6e90ac 5227
e32bac5b
RE
5228 /* Don't accept any offset that will require multiple
5229 instructions to handle, since this would cause the
5230 arith_adjacentmem pattern to output an overlong sequence. */
c75a3ddc
PB
5231 if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5232 return 0;
5233
ff9940b0
RE
5234 return (reg0 == reg1) && ((val1 - val0) == 4 || (val0 - val1) == 4);
5235 }
5236 return 0;
5237}
5238
5239/* Return 1 if OP is a load multiple operation. It is known to be
6354dc9b 5240 parallel and the first section will be tested. */
f3bb6135 5241int
e32bac5b 5242load_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
ff9940b0 5243{
f3bb6135 5244 HOST_WIDE_INT count = XVECLEN (op, 0);
ff9940b0
RE
5245 int dest_regno;
5246 rtx src_addr;
f3bb6135 5247 HOST_WIDE_INT i = 1, base = 0;
ff9940b0
RE
5248 rtx elt;
5249
5250 if (count <= 1
5251 || GET_CODE (XVECEXP (op, 0, 0)) != SET)
5252 return 0;
5253
6354dc9b 5254 /* Check to see if this might be a write-back. */
ff9940b0
RE
5255 if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
5256 {
5257 i++;
5258 base = 1;
5259
6354dc9b 5260 /* Now check it more carefully. */
ff9940b0
RE
5261 if (GET_CODE (SET_DEST (elt)) != REG
5262 || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
ff9940b0 5263 || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
41e3f998 5264 || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
ff9940b0 5265 return 0;
ff9940b0
RE
5266 }
5267
5268 /* Perform a quick check so we don't blow up below. */
5269 if (count <= i
5270 || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
5271 || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != REG
5272 || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != MEM)
5273 return 0;
5274
5275 dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, i - 1)));
5276 src_addr = XEXP (SET_SRC (XVECEXP (op, 0, i - 1)), 0);
5277
5278 for (; i < count; i++)
5279 {
ed4c4348 5280 elt = XVECEXP (op, 0, i);
ff9940b0
RE
5281
5282 if (GET_CODE (elt) != SET
5283 || GET_CODE (SET_DEST (elt)) != REG
5284 || GET_MODE (SET_DEST (elt)) != SImode
6354dc9b 5285 || REGNO (SET_DEST (elt)) != (unsigned int)(dest_regno + i - base)
ff9940b0
RE
5286 || GET_CODE (SET_SRC (elt)) != MEM
5287 || GET_MODE (SET_SRC (elt)) != SImode
5288 || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
5895f793 5289 || !rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
ff9940b0
RE
5290 || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
5291 || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != (i - base) * 4)
5292 return 0;
5293 }
5294
5295 return 1;
5296}
5297
5298/* Return 1 if OP is a store multiple operation. It is known to be
6354dc9b 5299 parallel and the first section will be tested. */
f3bb6135 5300int
e32bac5b 5301store_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
ff9940b0 5302{
f3bb6135 5303 HOST_WIDE_INT count = XVECLEN (op, 0);
ff9940b0
RE
5304 int src_regno;
5305 rtx dest_addr;
f3bb6135 5306 HOST_WIDE_INT i = 1, base = 0;
ff9940b0
RE
5307 rtx elt;
5308
5309 if (count <= 1
5310 || GET_CODE (XVECEXP (op, 0, 0)) != SET)
5311 return 0;
5312
6354dc9b 5313 /* Check to see if this might be a write-back. */
ff9940b0
RE
5314 if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
5315 {
5316 i++;
5317 base = 1;
5318
6354dc9b 5319 /* Now check it more carefully. */
ff9940b0
RE
5320 if (GET_CODE (SET_DEST (elt)) != REG
5321 || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
ff9940b0 5322 || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
41e3f998 5323 || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
ff9940b0 5324 return 0;
ff9940b0
RE
5325 }
5326
5327 /* Perform a quick check so we don't blow up below. */
5328 if (count <= i
5329 || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
5330 || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != MEM
5331 || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != REG)
5332 return 0;
5333
5334 src_regno = REGNO (SET_SRC (XVECEXP (op, 0, i - 1)));
5335 dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, i - 1)), 0);
5336
5337 for (; i < count; i++)
5338 {
5339 elt = XVECEXP (op, 0, i);
5340
5341 if (GET_CODE (elt) != SET
5342 || GET_CODE (SET_SRC (elt)) != REG
5343 || GET_MODE (SET_SRC (elt)) != SImode
6354dc9b 5344 || REGNO (SET_SRC (elt)) != (unsigned int)(src_regno + i - base)
ff9940b0
RE
5345 || GET_CODE (SET_DEST (elt)) != MEM
5346 || GET_MODE (SET_DEST (elt)) != SImode
5347 || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
5895f793 5348 || !rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
ff9940b0
RE
5349 || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
5350 || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != (i - base) * 4)
5351 return 0;
5352 }
5353
5354 return 1;
5355}
e2c671ba 5356
84ed5e79 5357int
e32bac5b
RE
5358load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5359 HOST_WIDE_INT *load_offset)
84ed5e79
RE
5360{
5361 int unsorted_regs[4];
5362 HOST_WIDE_INT unsorted_offsets[4];
5363 int order[4];
ad076f4e 5364 int base_reg = -1;
84ed5e79
RE
5365 int i;
5366
1d6e90ac
NC
5367 /* Can only handle 2, 3, or 4 insns at present,
5368 though could be easily extended if required. */
84ed5e79
RE
5369 if (nops < 2 || nops > 4)
5370 abort ();
5371
5372 /* Loop over the operands and check that the memory references are
5373 suitable (ie immediate offsets from the same base register). At
5374 the same time, extract the target register, and the memory
5375 offsets. */
5376 for (i = 0; i < nops; i++)
5377 {
5378 rtx reg;
5379 rtx offset;
5380
56636818
JL
5381 /* Convert a subreg of a mem into the mem itself. */
5382 if (GET_CODE (operands[nops + i]) == SUBREG)
4e26a7af 5383 operands[nops + i] = alter_subreg (operands + (nops + i));
56636818 5384
84ed5e79
RE
5385 if (GET_CODE (operands[nops + i]) != MEM)
5386 abort ();
5387
5388 /* Don't reorder volatile memory references; it doesn't seem worth
5389 looking for the case where the order is ok anyway. */
5390 if (MEM_VOLATILE_P (operands[nops + i]))
5391 return 0;
5392
5393 offset = const0_rtx;
5394
5395 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5396 || (GET_CODE (reg) == SUBREG
5397 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5398 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5399 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5400 == REG)
5401 || (GET_CODE (reg) == SUBREG
5402 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5403 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5404 == CONST_INT)))
5405 {
5406 if (i == 0)
5407 {
d5b7b3ae 5408 base_reg = REGNO (reg);
84ed5e79
RE
5409 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5410 ? REGNO (operands[i])
5411 : REGNO (SUBREG_REG (operands[i])));
5412 order[0] = 0;
5413 }
5414 else
5415 {
6354dc9b 5416 if (base_reg != (int) REGNO (reg))
84ed5e79
RE
5417 /* Not addressed from the same base register. */
5418 return 0;
5419
5420 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5421 ? REGNO (operands[i])
5422 : REGNO (SUBREG_REG (operands[i])));
5423 if (unsorted_regs[i] < unsorted_regs[order[0]])
5424 order[0] = i;
5425 }
5426
5427 /* If it isn't an integer register, or if it overwrites the
5428 base register but isn't the last insn in the list, then
5429 we can't do this. */
5430 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5431 || (i != nops - 1 && unsorted_regs[i] == base_reg))
5432 return 0;
5433
5434 unsorted_offsets[i] = INTVAL (offset);
5435 }
5436 else
5437 /* Not a suitable memory address. */
5438 return 0;
5439 }
5440
5441 /* All the useful information has now been extracted from the
5442 operands into unsorted_regs and unsorted_offsets; additionally,
5443 order[0] has been set to the lowest numbered register in the
5444 list. Sort the registers into order, and check that the memory
5445 offsets are ascending and adjacent. */
5446
5447 for (i = 1; i < nops; i++)
5448 {
5449 int j;
5450
5451 order[i] = order[i - 1];
5452 for (j = 0; j < nops; j++)
5453 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5454 && (order[i] == order[i - 1]
5455 || unsorted_regs[j] < unsorted_regs[order[i]]))
5456 order[i] = j;
5457
5458 /* Have we found a suitable register? if not, one must be used more
5459 than once. */
5460 if (order[i] == order[i - 1])
5461 return 0;
5462
5463 /* Is the memory address adjacent and ascending? */
5464 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5465 return 0;
5466 }
5467
5468 if (base)
5469 {
5470 *base = base_reg;
5471
5472 for (i = 0; i < nops; i++)
5473 regs[i] = unsorted_regs[order[i]];
5474
5475 *load_offset = unsorted_offsets[order[0]];
5476 }
5477
5478 if (unsorted_offsets[order[0]] == 0)
5479 return 1; /* ldmia */
5480
5481 if (unsorted_offsets[order[0]] == 4)
5482 return 2; /* ldmib */
5483
5484 if (unsorted_offsets[order[nops - 1]] == 0)
5485 return 3; /* ldmda */
5486
5487 if (unsorted_offsets[order[nops - 1]] == -4)
5488 return 4; /* ldmdb */
5489
949d79eb
RE
5490 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5491 if the offset isn't small enough. The reason 2 ldrs are faster
5492 is because these ARMs are able to do more than one cache access
5493 in a single cycle. The ARM9 and StrongARM have Harvard caches,
5494 whilst the ARM8 has a double bandwidth cache. This means that
5495 these cores can do both an instruction fetch and a data fetch in
5496 a single cycle, so the trick of calculating the address into a
5497 scratch register (one of the result regs) and then doing a load
5498 multiple actually becomes slower (and no smaller in code size).
5499 That is the transformation
6cc8c0b3
NC
5500
5501 ldr rd1, [rbase + offset]
5502 ldr rd2, [rbase + offset + 4]
5503
5504 to
5505
5506 add rd1, rbase, offset
5507 ldmia rd1, {rd1, rd2}
5508
949d79eb
RE
5509 produces worse code -- '3 cycles + any stalls on rd2' instead of
5510 '2 cycles + any stalls on rd2'. On ARMs with only one cache
5511 access per cycle, the first sequence could never complete in less
5512 than 6 cycles, whereas the ldm sequence would only take 5 and
5513 would make better use of sequential accesses if not hitting the
5514 cache.
5515
5516 We cheat here and test 'arm_ld_sched' which we currently know to
5517 only be true for the ARM8, ARM9 and StrongARM. If this ever
5518 changes, then the test below needs to be reworked. */
f5a1b0d2 5519 if (nops == 2 && arm_ld_sched)
b36ba79f
RE
5520 return 0;
5521
84ed5e79
RE
5522 /* Can't do it without setting up the offset, only do this if it takes
5523 no more than one insn. */
5524 return (const_ok_for_arm (unsorted_offsets[order[0]])
5525 || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5526}
5527
cd2b33d0 5528const char *
e32bac5b 5529emit_ldm_seq (rtx *operands, int nops)
84ed5e79
RE
5530{
5531 int regs[4];
5532 int base_reg;
5533 HOST_WIDE_INT offset;
5534 char buf[100];
5535 int i;
5536
5537 switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5538 {
5539 case 1:
5540 strcpy (buf, "ldm%?ia\t");
5541 break;
5542
5543 case 2:
5544 strcpy (buf, "ldm%?ib\t");
5545 break;
5546
5547 case 3:
5548 strcpy (buf, "ldm%?da\t");
5549 break;
5550
5551 case 4:
5552 strcpy (buf, "ldm%?db\t");
5553 break;
5554
5555 case 5:
5556 if (offset >= 0)
5557 sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5558 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5559 (long) offset);
5560 else
5561 sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5562 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5563 (long) -offset);
5564 output_asm_insn (buf, operands);
5565 base_reg = regs[0];
5566 strcpy (buf, "ldm%?ia\t");
5567 break;
5568
5569 default:
5570 abort ();
5571 }
5572
5573 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5574 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5575
5576 for (i = 1; i < nops; i++)
5577 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5578 reg_names[regs[i]]);
5579
5580 strcat (buf, "}\t%@ phole ldm");
5581
5582 output_asm_insn (buf, operands);
5583 return "";
5584}
5585
5586int
e32bac5b
RE
5587store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5588 HOST_WIDE_INT * load_offset)
84ed5e79
RE
5589{
5590 int unsorted_regs[4];
5591 HOST_WIDE_INT unsorted_offsets[4];
5592 int order[4];
ad076f4e 5593 int base_reg = -1;
84ed5e79
RE
5594 int i;
5595
5596 /* Can only handle 2, 3, or 4 insns at present, though could be easily
5597 extended if required. */
5598 if (nops < 2 || nops > 4)
5599 abort ();
5600
5601 /* Loop over the operands and check that the memory references are
5602 suitable (ie immediate offsets from the same base register). At
5603 the same time, extract the target register, and the memory
5604 offsets. */
5605 for (i = 0; i < nops; i++)
5606 {
5607 rtx reg;
5608 rtx offset;
5609
56636818
JL
5610 /* Convert a subreg of a mem into the mem itself. */
5611 if (GET_CODE (operands[nops + i]) == SUBREG)
4e26a7af 5612 operands[nops + i] = alter_subreg (operands + (nops + i));
56636818 5613
84ed5e79
RE
5614 if (GET_CODE (operands[nops + i]) != MEM)
5615 abort ();
5616
5617 /* Don't reorder volatile memory references; it doesn't seem worth
5618 looking for the case where the order is ok anyway. */
5619 if (MEM_VOLATILE_P (operands[nops + i]))
5620 return 0;
5621
5622 offset = const0_rtx;
5623
5624 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5625 || (GET_CODE (reg) == SUBREG
5626 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5627 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5628 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5629 == REG)
5630 || (GET_CODE (reg) == SUBREG
5631 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5632 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5633 == CONST_INT)))
5634 {
5635 if (i == 0)
5636 {
62b10bbc 5637 base_reg = REGNO (reg);
84ed5e79
RE
5638 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5639 ? REGNO (operands[i])
5640 : REGNO (SUBREG_REG (operands[i])));
5641 order[0] = 0;
5642 }
5643 else
5644 {
6354dc9b 5645 if (base_reg != (int) REGNO (reg))
84ed5e79
RE
5646 /* Not addressed from the same base register. */
5647 return 0;
5648
5649 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5650 ? REGNO (operands[i])
5651 : REGNO (SUBREG_REG (operands[i])));
5652 if (unsorted_regs[i] < unsorted_regs[order[0]])
5653 order[0] = i;
5654 }
5655
5656 /* If it isn't an integer register, then we can't do this. */
5657 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5658 return 0;
5659
5660 unsorted_offsets[i] = INTVAL (offset);
5661 }
5662 else
5663 /* Not a suitable memory address. */
5664 return 0;
5665 }
5666
5667 /* All the useful information has now been extracted from the
5668 operands into unsorted_regs and unsorted_offsets; additionally,
5669 order[0] has been set to the lowest numbered register in the
5670 list. Sort the registers into order, and check that the memory
5671 offsets are ascending and adjacent. */
5672
5673 for (i = 1; i < nops; i++)
5674 {
5675 int j;
5676
5677 order[i] = order[i - 1];
5678 for (j = 0; j < nops; j++)
5679 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5680 && (order[i] == order[i - 1]
5681 || unsorted_regs[j] < unsorted_regs[order[i]]))
5682 order[i] = j;
5683
5684 /* Have we found a suitable register? if not, one must be used more
5685 than once. */
5686 if (order[i] == order[i - 1])
5687 return 0;
5688
5689 /* Is the memory address adjacent and ascending? */
5690 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5691 return 0;
5692 }
5693
5694 if (base)
5695 {
5696 *base = base_reg;
5697
5698 for (i = 0; i < nops; i++)
5699 regs[i] = unsorted_regs[order[i]];
5700
5701 *load_offset = unsorted_offsets[order[0]];
5702 }
5703
5704 if (unsorted_offsets[order[0]] == 0)
5705 return 1; /* stmia */
5706
5707 if (unsorted_offsets[order[0]] == 4)
5708 return 2; /* stmib */
5709
5710 if (unsorted_offsets[order[nops - 1]] == 0)
5711 return 3; /* stmda */
5712
5713 if (unsorted_offsets[order[nops - 1]] == -4)
5714 return 4; /* stmdb */
5715
5716 return 0;
5717}
5718
cd2b33d0 5719const char *
e32bac5b 5720emit_stm_seq (rtx *operands, int nops)
84ed5e79
RE
5721{
5722 int regs[4];
5723 int base_reg;
5724 HOST_WIDE_INT offset;
5725 char buf[100];
5726 int i;
5727
5728 switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5729 {
5730 case 1:
5731 strcpy (buf, "stm%?ia\t");
5732 break;
5733
5734 case 2:
5735 strcpy (buf, "stm%?ib\t");
5736 break;
5737
5738 case 3:
5739 strcpy (buf, "stm%?da\t");
5740 break;
5741
5742 case 4:
5743 strcpy (buf, "stm%?db\t");
5744 break;
5745
5746 default:
5747 abort ();
5748 }
5749
5750 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5751 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5752
5753 for (i = 1; i < nops; i++)
5754 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5755 reg_names[regs[i]]);
5756
5757 strcat (buf, "}\t%@ phole stm");
5758
5759 output_asm_insn (buf, operands);
5760 return "";
5761}
5762
e2c671ba 5763int
e32bac5b 5764multi_register_push (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
e2c671ba
RE
5765{
5766 if (GET_CODE (op) != PARALLEL
5767 || (GET_CODE (XVECEXP (op, 0, 0)) != SET)
5768 || (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC)
b15bca31 5769 || (XINT (SET_SRC (XVECEXP (op, 0, 0)), 1) != UNSPEC_PUSH_MULT))
e2c671ba
RE
5770 return 0;
5771
5772 return 1;
5773}
ff9940b0 5774\f
6354dc9b 5775/* Routines for use in generating RTL. */
1d6e90ac 5776
f3bb6135 5777rtx
e32bac5b
RE
5778arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5779 int write_back, int unchanging_p, int in_struct_p,
5780 int scalar_p)
ff9940b0
RE
5781{
5782 int i = 0, j;
5783 rtx result;
5784 int sign = up ? 1 : -1;
56636818 5785 rtx mem;
ff9940b0 5786
d19fb8e3
NC
5787 /* XScale has load-store double instructions, but they have stricter
5788 alignment requirements than load-store multiple, so we can not
5789 use them.
5790
5791 For XScale ldm requires 2 + NREGS cycles to complete and blocks
5792 the pipeline until completion.
5793
5794 NREGS CYCLES
5795 1 3
5796 2 4
5797 3 5
5798 4 6
5799
5800 An ldr instruction takes 1-3 cycles, but does not block the
5801 pipeline.
5802
5803 NREGS CYCLES
5804 1 1-3
5805 2 2-6
5806 3 3-9
5807 4 4-12
5808
5809 Best case ldr will always win. However, the more ldr instructions
5810 we issue, the less likely we are to be able to schedule them well.
5811 Using ldr instructions also increases code size.
5812
5813 As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5814 for counts of 3 or 4 regs. */
4b3c2e48 5815 if (arm_tune_xscale && count <= 2 && ! optimize_size)
d19fb8e3
NC
5816 {
5817 rtx seq;
5818
5819 start_sequence ();
5820
5821 for (i = 0; i < count; i++)
5822 {
5823 mem = gen_rtx_MEM (SImode, plus_constant (from, i * 4 * sign));
5824 RTX_UNCHANGING_P (mem) = unchanging_p;
5825 MEM_IN_STRUCT_P (mem) = in_struct_p;
5826 MEM_SCALAR_P (mem) = scalar_p;
5827 emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5828 }
5829
5830 if (write_back)
5831 emit_move_insn (from, plus_constant (from, count * 4 * sign));
5832
2f937369 5833 seq = get_insns ();
d19fb8e3
NC
5834 end_sequence ();
5835
5836 return seq;
5837 }
5838
43cffd11 5839 result = gen_rtx_PARALLEL (VOIDmode,
41e3f998 5840 rtvec_alloc (count + (write_back ? 1 : 0)));
ff9940b0 5841 if (write_back)
f3bb6135 5842 {
ff9940b0 5843 XVECEXP (result, 0, 0)
43cffd11
RE
5844 = gen_rtx_SET (GET_MODE (from), from,
5845 plus_constant (from, count * 4 * sign));
ff9940b0
RE
5846 i = 1;
5847 count++;
f3bb6135
RE
5848 }
5849
ff9940b0 5850 for (j = 0; i < count; i++, j++)
f3bb6135 5851 {
43cffd11 5852 mem = gen_rtx_MEM (SImode, plus_constant (from, j * 4 * sign));
56636818
JL
5853 RTX_UNCHANGING_P (mem) = unchanging_p;
5854 MEM_IN_STRUCT_P (mem) = in_struct_p;
c6df88cb 5855 MEM_SCALAR_P (mem) = scalar_p;
43cffd11
RE
5856 XVECEXP (result, 0, i)
5857 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
f3bb6135
RE
5858 }
5859
ff9940b0
RE
5860 return result;
5861}
5862
f3bb6135 5863rtx
e32bac5b
RE
5864arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5865 int write_back, int unchanging_p, int in_struct_p,
5866 int scalar_p)
ff9940b0
RE
5867{
5868 int i = 0, j;
5869 rtx result;
5870 int sign = up ? 1 : -1;
56636818 5871 rtx mem;
ff9940b0 5872
d19fb8e3
NC
5873 /* See arm_gen_load_multiple for discussion of
5874 the pros/cons of ldm/stm usage for XScale. */
4b3c2e48 5875 if (arm_tune_xscale && count <= 2 && ! optimize_size)
d19fb8e3
NC
5876 {
5877 rtx seq;
5878
5879 start_sequence ();
5880
5881 for (i = 0; i < count; i++)
5882 {
5883 mem = gen_rtx_MEM (SImode, plus_constant (to, i * 4 * sign));
5884 RTX_UNCHANGING_P (mem) = unchanging_p;
5885 MEM_IN_STRUCT_P (mem) = in_struct_p;
5886 MEM_SCALAR_P (mem) = scalar_p;
5887 emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5888 }
5889
5890 if (write_back)
5891 emit_move_insn (to, plus_constant (to, count * 4 * sign));
5892
2f937369 5893 seq = get_insns ();
d19fb8e3
NC
5894 end_sequence ();
5895
5896 return seq;
5897 }
5898
43cffd11 5899 result = gen_rtx_PARALLEL (VOIDmode,
41e3f998 5900 rtvec_alloc (count + (write_back ? 1 : 0)));
ff9940b0 5901 if (write_back)
f3bb6135 5902 {
ff9940b0 5903 XVECEXP (result, 0, 0)
43cffd11
RE
5904 = gen_rtx_SET (GET_MODE (to), to,
5905 plus_constant (to, count * 4 * sign));
ff9940b0
RE
5906 i = 1;
5907 count++;
f3bb6135
RE
5908 }
5909
ff9940b0 5910 for (j = 0; i < count; i++, j++)
f3bb6135 5911 {
43cffd11 5912 mem = gen_rtx_MEM (SImode, plus_constant (to, j * 4 * sign));
56636818
JL
5913 RTX_UNCHANGING_P (mem) = unchanging_p;
5914 MEM_IN_STRUCT_P (mem) = in_struct_p;
c6df88cb 5915 MEM_SCALAR_P (mem) = scalar_p;
56636818 5916
43cffd11
RE
5917 XVECEXP (result, 0, i)
5918 = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
f3bb6135
RE
5919 }
5920
ff9940b0
RE
5921 return result;
5922}
5923
880e2516 5924int
e32bac5b 5925arm_gen_movstrqi (rtx *operands)
880e2516
RE
5926{
5927 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
ad076f4e 5928 int i;
880e2516 5929 rtx src, dst;
ad076f4e 5930 rtx st_src, st_dst, fin_src, fin_dst;
880e2516 5931 rtx part_bytes_reg = NULL;
56636818
JL
5932 rtx mem;
5933 int dst_unchanging_p, dst_in_struct_p, src_unchanging_p, src_in_struct_p;
c6df88cb 5934 int dst_scalar_p, src_scalar_p;
880e2516
RE
5935
5936 if (GET_CODE (operands[2]) != CONST_INT
5937 || GET_CODE (operands[3]) != CONST_INT
5938 || INTVAL (operands[2]) > 64
5939 || INTVAL (operands[3]) & 3)
5940 return 0;
5941
5942 st_dst = XEXP (operands[0], 0);
5943 st_src = XEXP (operands[1], 0);
56636818
JL
5944
5945 dst_unchanging_p = RTX_UNCHANGING_P (operands[0]);
5946 dst_in_struct_p = MEM_IN_STRUCT_P (operands[0]);
c6df88cb 5947 dst_scalar_p = MEM_SCALAR_P (operands[0]);
56636818
JL
5948 src_unchanging_p = RTX_UNCHANGING_P (operands[1]);
5949 src_in_struct_p = MEM_IN_STRUCT_P (operands[1]);
c6df88cb 5950 src_scalar_p = MEM_SCALAR_P (operands[1]);
56636818 5951
880e2516
RE
5952 fin_dst = dst = copy_to_mode_reg (SImode, st_dst);
5953 fin_src = src = copy_to_mode_reg (SImode, st_src);
5954
e9d7b180 5955 in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
880e2516
RE
5956 out_words_to_go = INTVAL (operands[2]) / 4;
5957 last_bytes = INTVAL (operands[2]) & 3;
5958
5959 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
43cffd11 5960 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
880e2516
RE
5961
5962 for (i = 0; in_words_to_go >= 2; i+=4)
5963 {
bd9c7e23 5964 if (in_words_to_go > 4)
56636818 5965 emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
c6df88cb
MM
5966 src_unchanging_p,
5967 src_in_struct_p,
5968 src_scalar_p));
bd9c7e23
RE
5969 else
5970 emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
56636818 5971 FALSE, src_unchanging_p,
c6df88cb 5972 src_in_struct_p, src_scalar_p));
bd9c7e23 5973
880e2516
RE
5974 if (out_words_to_go)
5975 {
bd9c7e23 5976 if (out_words_to_go > 4)
56636818
JL
5977 emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5978 dst_unchanging_p,
c6df88cb
MM
5979 dst_in_struct_p,
5980 dst_scalar_p));
bd9c7e23
RE
5981 else if (out_words_to_go != 1)
5982 emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5983 dst, TRUE,
5984 (last_bytes == 0
56636818
JL
5985 ? FALSE : TRUE),
5986 dst_unchanging_p,
c6df88cb
MM
5987 dst_in_struct_p,
5988 dst_scalar_p));
880e2516
RE
5989 else
5990 {
43cffd11 5991 mem = gen_rtx_MEM (SImode, dst);
56636818
JL
5992 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
5993 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
c6df88cb 5994 MEM_SCALAR_P (mem) = dst_scalar_p;
43cffd11 5995 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
bd9c7e23
RE
5996 if (last_bytes != 0)
5997 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
880e2516
RE
5998 }
5999 }
6000
6001 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
6002 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
6003 }
6004
6005 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
6006 if (out_words_to_go)
62b10bbc
NC
6007 {
6008 rtx sreg;
6009
6010 mem = gen_rtx_MEM (SImode, src);
6011 RTX_UNCHANGING_P (mem) = src_unchanging_p;
6012 MEM_IN_STRUCT_P (mem) = src_in_struct_p;
6013 MEM_SCALAR_P (mem) = src_scalar_p;
6014 emit_move_insn (sreg = gen_reg_rtx (SImode), mem);
6015 emit_move_insn (fin_src = gen_reg_rtx (SImode), plus_constant (src, 4));
6016
6017 mem = gen_rtx_MEM (SImode, dst);
6018 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6019 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6020 MEM_SCALAR_P (mem) = dst_scalar_p;
6021 emit_move_insn (mem, sreg);
6022 emit_move_insn (fin_dst = gen_reg_rtx (SImode), plus_constant (dst, 4));
6023 in_words_to_go--;
6024
6025 if (in_words_to_go) /* Sanity check */
6026 abort ();
6027 }
880e2516
RE
6028
6029 if (in_words_to_go)
6030 {
6031 if (in_words_to_go < 0)
6032 abort ();
6033
43cffd11 6034 mem = gen_rtx_MEM (SImode, src);
56636818
JL
6035 RTX_UNCHANGING_P (mem) = src_unchanging_p;
6036 MEM_IN_STRUCT_P (mem) = src_in_struct_p;
c6df88cb 6037 MEM_SCALAR_P (mem) = src_scalar_p;
56636818 6038 part_bytes_reg = copy_to_mode_reg (SImode, mem);
880e2516
RE
6039 }
6040
d5b7b3ae
RE
6041 if (last_bytes && part_bytes_reg == NULL)
6042 abort ();
6043
880e2516
RE
6044 if (BYTES_BIG_ENDIAN && last_bytes)
6045 {
6046 rtx tmp = gen_reg_rtx (SImode);
6047
6354dc9b 6048 /* The bytes we want are in the top end of the word. */
bee06f3d
RE
6049 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
6050 GEN_INT (8 * (4 - last_bytes))));
880e2516
RE
6051 part_bytes_reg = tmp;
6052
6053 while (last_bytes)
6054 {
43cffd11 6055 mem = gen_rtx_MEM (QImode, plus_constant (dst, last_bytes - 1));
56636818
JL
6056 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6057 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
c6df88cb 6058 MEM_SCALAR_P (mem) = dst_scalar_p;
5d5603e2
BS
6059 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6060
880e2516
RE
6061 if (--last_bytes)
6062 {
6063 tmp = gen_reg_rtx (SImode);
6064 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
6065 part_bytes_reg = tmp;
6066 }
6067 }
6068
6069 }
6070 else
6071 {
d5b7b3ae 6072 if (last_bytes > 1)
880e2516 6073 {
d5b7b3ae 6074 mem = gen_rtx_MEM (HImode, dst);
56636818
JL
6075 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6076 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
c6df88cb 6077 MEM_SCALAR_P (mem) = dst_scalar_p;
5d5603e2 6078 emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
d5b7b3ae
RE
6079 last_bytes -= 2;
6080 if (last_bytes)
880e2516
RE
6081 {
6082 rtx tmp = gen_reg_rtx (SImode);
bd9c7e23 6083
a556fd39 6084 emit_insn (gen_addsi3 (dst, dst, const2_rtx));
d5b7b3ae 6085 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
880e2516
RE
6086 part_bytes_reg = tmp;
6087 }
6088 }
d5b7b3ae
RE
6089
6090 if (last_bytes)
6091 {
6092 mem = gen_rtx_MEM (QImode, dst);
6093 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6094 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6095 MEM_SCALAR_P (mem) = dst_scalar_p;
5d5603e2 6096 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
d5b7b3ae 6097 }
880e2516
RE
6098 }
6099
6100 return 1;
6101}
6102
5165176d
RE
6103/* Generate a memory reference for a half word, such that it will be loaded
6104 into the top 16 bits of the word. We can assume that the address is
6105 known to be alignable and of the form reg, or plus (reg, const). */
1d6e90ac 6106
5165176d 6107rtx
e32bac5b 6108arm_gen_rotated_half_load (rtx memref)
5165176d
RE
6109{
6110 HOST_WIDE_INT offset = 0;
6111 rtx base = XEXP (memref, 0);
6112
6113 if (GET_CODE (base) == PLUS)
6114 {
6115 offset = INTVAL (XEXP (base, 1));
6116 base = XEXP (base, 0);
6117 }
6118
956d6950 6119 /* If we aren't allowed to generate unaligned addresses, then fail. */
5f1e6755 6120 if (TARGET_MMU_TRAPS
5165176d
RE
6121 && ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0)))
6122 return NULL;
6123
43cffd11 6124 base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
5165176d
RE
6125
6126 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
6127 return base;
6128
43cffd11 6129 return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
5165176d
RE
6130}
6131
03f1640c
RE
6132/* Select a dominance comparison mode if possible for a test of the general
6133 form (OP (COND_OR (X) (Y)) (const_int 0)). We support three forms.
6134 COND_OR == DOM_CC_X_AND_Y => (X && Y)
6135 COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6136 COND_OR == DOM_CC_X_OR_Y => (X || Y)
6137 In all cases OP will be either EQ or NE, but we don't need to know which
6138 here. If we are unable to support a dominance comparison we return
6139 CC mode. This will then fail to match for the RTL expressions that
6140 generate this call. */
03f1640c 6141enum machine_mode
e32bac5b 6142arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
84ed5e79
RE
6143{
6144 enum rtx_code cond1, cond2;
6145 int swapped = 0;
6146
6147 /* Currently we will probably get the wrong result if the individual
6148 comparisons are not simple. This also ensures that it is safe to
956d6950 6149 reverse a comparison if necessary. */
84ed5e79
RE
6150 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6151 != CCmode)
6152 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6153 != CCmode))
6154 return CCmode;
6155
1646cf41
RE
6156 /* The if_then_else variant of this tests the second condition if the
6157 first passes, but is true if the first fails. Reverse the first
6158 condition to get a true "inclusive-or" expression. */
03f1640c 6159 if (cond_or == DOM_CC_NX_OR_Y)
84ed5e79
RE
6160 cond1 = reverse_condition (cond1);
6161
6162 /* If the comparisons are not equal, and one doesn't dominate the other,
6163 then we can't do this. */
6164 if (cond1 != cond2
5895f793
RE
6165 && !comparison_dominates_p (cond1, cond2)
6166 && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
84ed5e79
RE
6167 return CCmode;
6168
6169 if (swapped)
6170 {
6171 enum rtx_code temp = cond1;
6172 cond1 = cond2;
6173 cond2 = temp;
6174 }
6175
6176 switch (cond1)
6177 {
6178 case EQ:
03f1640c 6179 if (cond2 == EQ || cond_or == DOM_CC_X_AND_Y)
84ed5e79
RE
6180 return CC_DEQmode;
6181
6182 switch (cond2)
6183 {
6184 case LE: return CC_DLEmode;
6185 case LEU: return CC_DLEUmode;
6186 case GE: return CC_DGEmode;
6187 case GEU: return CC_DGEUmode;
ad076f4e 6188 default: break;
84ed5e79
RE
6189 }
6190
6191 break;
6192
6193 case LT:
03f1640c 6194 if (cond2 == LT || cond_or == DOM_CC_X_AND_Y)
84ed5e79
RE
6195 return CC_DLTmode;
6196 if (cond2 == LE)
6197 return CC_DLEmode;
6198 if (cond2 == NE)
6199 return CC_DNEmode;
6200 break;
6201
6202 case GT:
03f1640c 6203 if (cond2 == GT || cond_or == DOM_CC_X_AND_Y)
84ed5e79
RE
6204 return CC_DGTmode;
6205 if (cond2 == GE)
6206 return CC_DGEmode;
6207 if (cond2 == NE)
6208 return CC_DNEmode;
6209 break;
6210
6211 case LTU:
03f1640c 6212 if (cond2 == LTU || cond_or == DOM_CC_X_AND_Y)
84ed5e79
RE
6213 return CC_DLTUmode;
6214 if (cond2 == LEU)
6215 return CC_DLEUmode;
6216 if (cond2 == NE)
6217 return CC_DNEmode;
6218 break;
6219
6220 case GTU:
03f1640c 6221 if (cond2 == GTU || cond_or == DOM_CC_X_AND_Y)
84ed5e79
RE
6222 return CC_DGTUmode;
6223 if (cond2 == GEU)
6224 return CC_DGEUmode;
6225 if (cond2 == NE)
6226 return CC_DNEmode;
6227 break;
6228
6229 /* The remaining cases only occur when both comparisons are the
6230 same. */
6231 case NE:
6232 return CC_DNEmode;
6233
6234 case LE:
6235 return CC_DLEmode;
6236
6237 case GE:
6238 return CC_DGEmode;
6239
6240 case LEU:
6241 return CC_DLEUmode;
6242
6243 case GEU:
6244 return CC_DGEUmode;
ad076f4e
RE
6245
6246 default:
6247 break;
84ed5e79
RE
6248 }
6249
6250 abort ();
6251}
6252
6253enum machine_mode
e32bac5b 6254arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
84ed5e79
RE
6255{
6256 /* All floating point compares return CCFP if it is an equality
6257 comparison, and CCFPE otherwise. */
6258 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
e45b72c4
RE
6259 {
6260 switch (op)
6261 {
6262 case EQ:
6263 case NE:
6264 case UNORDERED:
6265 case ORDERED:
6266 case UNLT:
6267 case UNLE:
6268 case UNGT:
6269 case UNGE:
6270 case UNEQ:
6271 case LTGT:
6272 return CCFPmode;
6273
6274 case LT:
6275 case LE:
6276 case GT:
6277 case GE:
9b66ebb1 6278 if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
9b6b54e2 6279 return CCFPmode;
e45b72c4
RE
6280 return CCFPEmode;
6281
6282 default:
6283 abort ();
6284 }
6285 }
84ed5e79
RE
6286
6287 /* A compare with a shifted operand. Because of canonicalization, the
6288 comparison will have to be swapped when we emit the assembler. */
6289 if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6290 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6291 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6292 || GET_CODE (x) == ROTATERT))
6293 return CC_SWPmode;
6294
956d6950
JL
6295 /* This is a special case that is used by combine to allow a
6296 comparison of a shifted byte load to be split into a zero-extend
84ed5e79 6297 followed by a comparison of the shifted integer (only valid for
956d6950 6298 equalities and unsigned inequalities). */
84ed5e79
RE
6299 if (GET_MODE (x) == SImode
6300 && GET_CODE (x) == ASHIFT
6301 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6302 && GET_CODE (XEXP (x, 0)) == SUBREG
6303 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6304 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6305 && (op == EQ || op == NE
6306 || op == GEU || op == GTU || op == LTU || op == LEU)
6307 && GET_CODE (y) == CONST_INT)
6308 return CC_Zmode;
6309
1646cf41
RE
6310 /* A construct for a conditional compare, if the false arm contains
6311 0, then both conditions must be true, otherwise either condition
6312 must be true. Not all conditions are possible, so CCmode is
6313 returned if it can't be done. */
6314 if (GET_CODE (x) == IF_THEN_ELSE
6315 && (XEXP (x, 2) == const0_rtx
6316 || XEXP (x, 2) == const1_rtx)
ec8e098d
PB
6317 && COMPARISON_P (XEXP (x, 0))
6318 && COMPARISON_P (XEXP (x, 1)))
03f1640c
RE
6319 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6320 INTVAL (XEXP (x, 2)));
1646cf41
RE
6321
6322 /* Alternate canonicalizations of the above. These are somewhat cleaner. */
6323 if (GET_CODE (x) == AND
ec8e098d
PB
6324 && COMPARISON_P (XEXP (x, 0))
6325 && COMPARISON_P (XEXP (x, 1)))
03f1640c
RE
6326 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6327 DOM_CC_X_AND_Y);
1646cf41
RE
6328
6329 if (GET_CODE (x) == IOR
ec8e098d
PB
6330 && COMPARISON_P (XEXP (x, 0))
6331 && COMPARISON_P (XEXP (x, 1)))
03f1640c
RE
6332 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6333 DOM_CC_X_OR_Y);
1646cf41 6334
defc0463
RE
6335 /* An operation (on Thumb) where we want to test for a single bit.
6336 This is done by shifting that bit up into the top bit of a
6337 scratch register; we can then branch on the sign bit. */
6338 if (TARGET_THUMB
6339 && GET_MODE (x) == SImode
6340 && (op == EQ || op == NE)
6341 && (GET_CODE (x) == ZERO_EXTRACT))
6342 return CC_Nmode;
6343
84ed5e79
RE
6344 /* An operation that sets the condition codes as a side-effect, the
6345 V flag is not set correctly, so we can only use comparisons where
6346 this doesn't matter. (For LT and GE we can use "mi" and "pl"
defc0463 6347 instead.) */
84ed5e79
RE
6348 if (GET_MODE (x) == SImode
6349 && y == const0_rtx
6350 && (op == EQ || op == NE || op == LT || op == GE)
6351 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6352 || GET_CODE (x) == AND || GET_CODE (x) == IOR
6353 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6354 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6355 || GET_CODE (x) == LSHIFTRT
6356 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
defc0463
RE
6357 || GET_CODE (x) == ROTATERT
6358 || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
84ed5e79
RE
6359 return CC_NOOVmode;
6360
84ed5e79
RE
6361 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6362 return CC_Zmode;
6363
bd9c7e23
RE
6364 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6365 && GET_CODE (x) == PLUS
6366 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6367 return CC_Cmode;
6368
84ed5e79
RE
6369 return CCmode;
6370}
6371
ff9940b0
RE
6372/* X and Y are two things to compare using CODE. Emit the compare insn and
6373 return the rtx for register 0 in the proper mode. FP means this is a
6374 floating point compare: I don't think that it is needed on the arm. */
ff9940b0 6375rtx
e32bac5b 6376arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
ff9940b0
RE
6377{
6378 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
d5b7b3ae 6379 rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
ff9940b0 6380
43cffd11
RE
6381 emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6382 gen_rtx_COMPARE (mode, x, y)));
ff9940b0
RE
6383
6384 return cc_reg;
6385}
6386
fcd53748
JT
6387/* Generate a sequence of insns that will generate the correct return
6388 address mask depending on the physical architecture that the program
6389 is running on. */
fcd53748 6390rtx
e32bac5b 6391arm_gen_return_addr_mask (void)
fcd53748
JT
6392{
6393 rtx reg = gen_reg_rtx (Pmode);
6394
6395 emit_insn (gen_return_addr_mask (reg));
6396 return reg;
6397}
6398
0a81f500 6399void
e32bac5b 6400arm_reload_in_hi (rtx *operands)
0a81f500 6401{
f9cc092a
RE
6402 rtx ref = operands[1];
6403 rtx base, scratch;
6404 HOST_WIDE_INT offset = 0;
6405
6406 if (GET_CODE (ref) == SUBREG)
6407 {
ddef6bc7 6408 offset = SUBREG_BYTE (ref);
f9cc092a
RE
6409 ref = SUBREG_REG (ref);
6410 }
6411
6412 if (GET_CODE (ref) == REG)
6413 {
6414 /* We have a pseudo which has been spilt onto the stack; there
6415 are two cases here: the first where there is a simple
6416 stack-slot replacement and a second where the stack-slot is
6417 out of range, or is used as a subreg. */
6418 if (reg_equiv_mem[REGNO (ref)])
6419 {
6420 ref = reg_equiv_mem[REGNO (ref)];
6421 base = find_replacement (&XEXP (ref, 0));
6422 }
6423 else
6354dc9b 6424 /* The slot is out of range, or was dressed up in a SUBREG. */
f9cc092a
RE
6425 base = reg_equiv_address[REGNO (ref)];
6426 }
6427 else
6428 base = find_replacement (&XEXP (ref, 0));
0a81f500 6429
e5e809f4
JL
6430 /* Handle the case where the address is too complex to be offset by 1. */
6431 if (GET_CODE (base) == MINUS
6432 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6433 {
f9cc092a 6434 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
e5e809f4 6435
43cffd11 6436 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
e5e809f4
JL
6437 base = base_plus;
6438 }
f9cc092a
RE
6439 else if (GET_CODE (base) == PLUS)
6440 {
6354dc9b 6441 /* The addend must be CONST_INT, or we would have dealt with it above. */
f9cc092a
RE
6442 HOST_WIDE_INT hi, lo;
6443
6444 offset += INTVAL (XEXP (base, 1));
6445 base = XEXP (base, 0);
6446
6354dc9b 6447 /* Rework the address into a legal sequence of insns. */
f9cc092a
RE
6448 /* Valid range for lo is -4095 -> 4095 */
6449 lo = (offset >= 0
6450 ? (offset & 0xfff)
6451 : -((-offset) & 0xfff));
6452
6453 /* Corner case, if lo is the max offset then we would be out of range
6454 once we have added the additional 1 below, so bump the msb into the
6455 pre-loading insn(s). */
6456 if (lo == 4095)
6457 lo &= 0x7ff;
6458
30cf4896
KG
6459 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6460 ^ (HOST_WIDE_INT) 0x80000000)
6461 - (HOST_WIDE_INT) 0x80000000);
f9cc092a
RE
6462
6463 if (hi + lo != offset)
6464 abort ();
6465
6466 if (hi != 0)
6467 {
6468 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6469
6470 /* Get the base address; addsi3 knows how to handle constants
6354dc9b 6471 that require more than one insn. */
f9cc092a
RE
6472 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6473 base = base_plus;
6474 offset = lo;
6475 }
6476 }
e5e809f4 6477
3a1944a6
RE
6478 /* Operands[2] may overlap operands[0] (though it won't overlap
6479 operands[1]), that's why we asked for a DImode reg -- so we can
6480 use the bit that does not overlap. */
6481 if (REGNO (operands[2]) == REGNO (operands[0]))
6482 scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6483 else
6484 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6485
f9cc092a
RE
6486 emit_insn (gen_zero_extendqisi2 (scratch,
6487 gen_rtx_MEM (QImode,
6488 plus_constant (base,
6489 offset))));
43cffd11
RE
6490 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6491 gen_rtx_MEM (QImode,
f9cc092a
RE
6492 plus_constant (base,
6493 offset + 1))));
5895f793 6494 if (!BYTES_BIG_ENDIAN)
43cffd11
RE
6495 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6496 gen_rtx_IOR (SImode,
6497 gen_rtx_ASHIFT
6498 (SImode,
6499 gen_rtx_SUBREG (SImode, operands[0], 0),
6500 GEN_INT (8)),
f9cc092a 6501 scratch)));
0a81f500 6502 else
43cffd11
RE
6503 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6504 gen_rtx_IOR (SImode,
f9cc092a 6505 gen_rtx_ASHIFT (SImode, scratch,
43cffd11
RE
6506 GEN_INT (8)),
6507 gen_rtx_SUBREG (SImode, operands[0],
6508 0))));
0a81f500
RE
6509}
6510
72ac76be 6511/* Handle storing a half-word to memory during reload by synthesizing as two
f9cc092a
RE
6512 byte stores. Take care not to clobber the input values until after we
6513 have moved them somewhere safe. This code assumes that if the DImode
6514 scratch in operands[2] overlaps either the input value or output address
6515 in some way, then that value must die in this insn (we absolutely need
6516 two scratch registers for some corner cases). */
f3bb6135 6517void
e32bac5b 6518arm_reload_out_hi (rtx *operands)
af48348a 6519{
f9cc092a
RE
6520 rtx ref = operands[0];
6521 rtx outval = operands[1];
6522 rtx base, scratch;
6523 HOST_WIDE_INT offset = 0;
6524
6525 if (GET_CODE (ref) == SUBREG)
6526 {
ddef6bc7 6527 offset = SUBREG_BYTE (ref);
f9cc092a
RE
6528 ref = SUBREG_REG (ref);
6529 }
6530
f9cc092a
RE
6531 if (GET_CODE (ref) == REG)
6532 {
6533 /* We have a pseudo which has been spilt onto the stack; there
6534 are two cases here: the first where there is a simple
6535 stack-slot replacement and a second where the stack-slot is
6536 out of range, or is used as a subreg. */
6537 if (reg_equiv_mem[REGNO (ref)])
6538 {
6539 ref = reg_equiv_mem[REGNO (ref)];
6540 base = find_replacement (&XEXP (ref, 0));
6541 }
6542 else
6354dc9b 6543 /* The slot is out of range, or was dressed up in a SUBREG. */
f9cc092a
RE
6544 base = reg_equiv_address[REGNO (ref)];
6545 }
6546 else
6547 base = find_replacement (&XEXP (ref, 0));
6548
6549 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6550
6551 /* Handle the case where the address is too complex to be offset by 1. */
6552 if (GET_CODE (base) == MINUS
6553 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6554 {
6555 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6556
6557 /* Be careful not to destroy OUTVAL. */
6558 if (reg_overlap_mentioned_p (base_plus, outval))
6559 {
6560 /* Updating base_plus might destroy outval, see if we can
6561 swap the scratch and base_plus. */
5895f793 6562 if (!reg_overlap_mentioned_p (scratch, outval))
f9cc092a
RE
6563 {
6564 rtx tmp = scratch;
6565 scratch = base_plus;
6566 base_plus = tmp;
6567 }
6568 else
6569 {
6570 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6571
6572 /* Be conservative and copy OUTVAL into the scratch now,
6573 this should only be necessary if outval is a subreg
6574 of something larger than a word. */
6575 /* XXX Might this clobber base? I can't see how it can,
6576 since scratch is known to overlap with OUTVAL, and
6577 must be wider than a word. */
6578 emit_insn (gen_movhi (scratch_hi, outval));
6579 outval = scratch_hi;
6580 }
6581 }
6582
6583 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6584 base = base_plus;
6585 }
6586 else if (GET_CODE (base) == PLUS)
6587 {
6354dc9b 6588 /* The addend must be CONST_INT, or we would have dealt with it above. */
f9cc092a
RE
6589 HOST_WIDE_INT hi, lo;
6590
6591 offset += INTVAL (XEXP (base, 1));
6592 base = XEXP (base, 0);
6593
6354dc9b 6594 /* Rework the address into a legal sequence of insns. */
f9cc092a
RE
6595 /* Valid range for lo is -4095 -> 4095 */
6596 lo = (offset >= 0
6597 ? (offset & 0xfff)
6598 : -((-offset) & 0xfff));
6599
6600 /* Corner case, if lo is the max offset then we would be out of range
6601 once we have added the additional 1 below, so bump the msb into the
6602 pre-loading insn(s). */
6603 if (lo == 4095)
6604 lo &= 0x7ff;
6605
30cf4896
KG
6606 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6607 ^ (HOST_WIDE_INT) 0x80000000)
6608 - (HOST_WIDE_INT) 0x80000000);
f9cc092a
RE
6609
6610 if (hi + lo != offset)
6611 abort ();
6612
6613 if (hi != 0)
6614 {
6615 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6616
6617 /* Be careful not to destroy OUTVAL. */
6618 if (reg_overlap_mentioned_p (base_plus, outval))
6619 {
6620 /* Updating base_plus might destroy outval, see if we
6621 can swap the scratch and base_plus. */
5895f793 6622 if (!reg_overlap_mentioned_p (scratch, outval))
f9cc092a
RE
6623 {
6624 rtx tmp = scratch;
6625 scratch = base_plus;
6626 base_plus = tmp;
6627 }
6628 else
6629 {
6630 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6631
6632 /* Be conservative and copy outval into scratch now,
6633 this should only be necessary if outval is a
6634 subreg of something larger than a word. */
6635 /* XXX Might this clobber base? I can't see how it
6636 can, since scratch is known to overlap with
6637 outval. */
6638 emit_insn (gen_movhi (scratch_hi, outval));
6639 outval = scratch_hi;
6640 }
6641 }
6642
6643 /* Get the base address; addsi3 knows how to handle constants
6354dc9b 6644 that require more than one insn. */
f9cc092a
RE
6645 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6646 base = base_plus;
6647 offset = lo;
6648 }
6649 }
af48348a 6650
b5cc037f
RE
6651 if (BYTES_BIG_ENDIAN)
6652 {
f9cc092a
RE
6653 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6654 plus_constant (base, offset + 1)),
5d5603e2 6655 gen_lowpart (QImode, outval)));
f9cc092a
RE
6656 emit_insn (gen_lshrsi3 (scratch,
6657 gen_rtx_SUBREG (SImode, outval, 0),
b5cc037f 6658 GEN_INT (8)));
f9cc092a 6659 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
5d5603e2 6660 gen_lowpart (QImode, scratch)));
b5cc037f
RE
6661 }
6662 else
6663 {
f9cc092a 6664 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
5d5603e2 6665 gen_lowpart (QImode, outval)));
f9cc092a
RE
6666 emit_insn (gen_lshrsi3 (scratch,
6667 gen_rtx_SUBREG (SImode, outval, 0),
b5cc037f 6668 GEN_INT (8)));
f9cc092a
RE
6669 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6670 plus_constant (base, offset + 1)),
5d5603e2 6671 gen_lowpart (QImode, scratch)));
b5cc037f 6672 }
af48348a 6673}
2b835d68 6674\f
d5b7b3ae
RE
6675/* Print a symbolic form of X to the debug file, F. */
6676static void
e32bac5b 6677arm_print_value (FILE *f, rtx x)
d5b7b3ae
RE
6678{
6679 switch (GET_CODE (x))
6680 {
6681 case CONST_INT:
6682 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6683 return;
6684
6685 case CONST_DOUBLE:
6686 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6687 return;
6688
5a9335ef
NC
6689 case CONST_VECTOR:
6690 {
6691 int i;
6692
6693 fprintf (f, "<");
6694 for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6695 {
6696 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6697 if (i < (CONST_VECTOR_NUNITS (x) - 1))
6698 fputc (',', f);
6699 }
6700 fprintf (f, ">");
6701 }
6702 return;
6703
d5b7b3ae
RE
6704 case CONST_STRING:
6705 fprintf (f, "\"%s\"", XSTR (x, 0));
6706 return;
6707
6708 case SYMBOL_REF:
6709 fprintf (f, "`%s'", XSTR (x, 0));
6710 return;
6711
6712 case LABEL_REF:
6713 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6714 return;
6715
6716 case CONST:
6717 arm_print_value (f, XEXP (x, 0));
6718 return;
6719
6720 case PLUS:
6721 arm_print_value (f, XEXP (x, 0));
6722 fprintf (f, "+");
6723 arm_print_value (f, XEXP (x, 1));
6724 return;
6725
6726 case PC:
6727 fprintf (f, "pc");
6728 return;
6729
6730 default:
6731 fprintf (f, "????");
6732 return;
6733 }
6734}
6735\f
2b835d68 6736/* Routines for manipulation of the constant pool. */
2b835d68 6737
949d79eb
RE
6738/* Arm instructions cannot load a large constant directly into a
6739 register; they have to come from a pc relative load. The constant
6740 must therefore be placed in the addressable range of the pc
6741 relative load. Depending on the precise pc relative load
6742 instruction the range is somewhere between 256 bytes and 4k. This
6743 means that we often have to dump a constant inside a function, and
2b835d68
RE
6744 generate code to branch around it.
6745
949d79eb
RE
6746 It is important to minimize this, since the branches will slow
6747 things down and make the code larger.
2b835d68 6748
949d79eb
RE
6749 Normally we can hide the table after an existing unconditional
6750 branch so that there is no interruption of the flow, but in the
6751 worst case the code looks like this:
2b835d68
RE
6752
6753 ldr rn, L1
949d79eb 6754 ...
2b835d68
RE
6755 b L2
6756 align
6757 L1: .long value
6758 L2:
949d79eb 6759 ...
2b835d68 6760
2b835d68 6761 ldr rn, L3
949d79eb 6762 ...
2b835d68
RE
6763 b L4
6764 align
2b835d68
RE
6765 L3: .long value
6766 L4:
949d79eb
RE
6767 ...
6768
6769 We fix this by performing a scan after scheduling, which notices
6770 which instructions need to have their operands fetched from the
6771 constant table and builds the table.
6772
6773 The algorithm starts by building a table of all the constants that
6774 need fixing up and all the natural barriers in the function (places
6775 where a constant table can be dropped without breaking the flow).
6776 For each fixup we note how far the pc-relative replacement will be
6777 able to reach and the offset of the instruction into the function.
6778
6779 Having built the table we then group the fixes together to form
6780 tables that are as large as possible (subject to addressing
6781 constraints) and emit each table of constants after the last
6782 barrier that is within range of all the instructions in the group.
6783 If a group does not contain a barrier, then we forcibly create one
6784 by inserting a jump instruction into the flow. Once the table has
6785 been inserted, the insns are then modified to reference the
6786 relevant entry in the pool.
6787
6354dc9b 6788 Possible enhancements to the algorithm (not implemented) are:
949d79eb 6789
d5b7b3ae 6790 1) For some processors and object formats, there may be benefit in
949d79eb
RE
6791 aligning the pools to the start of cache lines; this alignment
6792 would need to be taken into account when calculating addressability
6354dc9b 6793 of a pool. */
2b835d68 6794
d5b7b3ae
RE
6795/* These typedefs are located at the start of this file, so that
6796 they can be used in the prototypes there. This comment is to
6797 remind readers of that fact so that the following structures
6798 can be understood more easily.
6799
6800 typedef struct minipool_node Mnode;
6801 typedef struct minipool_fixup Mfix; */
6802
6803struct minipool_node
6804{
6805 /* Doubly linked chain of entries. */
6806 Mnode * next;
6807 Mnode * prev;
6808 /* The maximum offset into the code that this entry can be placed. While
6809 pushing fixes for forward references, all entries are sorted in order
6810 of increasing max_address. */
6811 HOST_WIDE_INT max_address;
5519a4f9 6812 /* Similarly for an entry inserted for a backwards ref. */
d5b7b3ae
RE
6813 HOST_WIDE_INT min_address;
6814 /* The number of fixes referencing this entry. This can become zero
6815 if we "unpush" an entry. In this case we ignore the entry when we
6816 come to emit the code. */
6817 int refcount;
6818 /* The offset from the start of the minipool. */
6819 HOST_WIDE_INT offset;
6820 /* The value in table. */
6821 rtx value;
6822 /* The mode of value. */
6823 enum machine_mode mode;
5a9335ef
NC
6824 /* The size of the value. With iWMMXt enabled
6825 sizes > 4 also imply an alignment of 8-bytes. */
d5b7b3ae
RE
6826 int fix_size;
6827};
6828
6829struct minipool_fixup
2b835d68 6830{
d5b7b3ae
RE
6831 Mfix * next;
6832 rtx insn;
6833 HOST_WIDE_INT address;
6834 rtx * loc;
6835 enum machine_mode mode;
6836 int fix_size;
6837 rtx value;
6838 Mnode * minipool;
6839 HOST_WIDE_INT forwards;
6840 HOST_WIDE_INT backwards;
6841};
2b835d68 6842
d5b7b3ae
RE
6843/* Fixes less than a word need padding out to a word boundary. */
6844#define MINIPOOL_FIX_SIZE(mode) \
6845 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
2b835d68 6846
d5b7b3ae
RE
6847static Mnode * minipool_vector_head;
6848static Mnode * minipool_vector_tail;
6849static rtx minipool_vector_label;
332072db 6850
d5b7b3ae
RE
6851/* The linked list of all minipool fixes required for this function. */
6852Mfix * minipool_fix_head;
6853Mfix * minipool_fix_tail;
6854/* The fix entry for the current minipool, once it has been placed. */
6855Mfix * minipool_barrier;
6856
6857/* Determines if INSN is the start of a jump table. Returns the end
6858 of the TABLE or NULL_RTX. */
6859static rtx
e32bac5b 6860is_jump_table (rtx insn)
2b835d68 6861{
d5b7b3ae 6862 rtx table;
da6558fd 6863
d5b7b3ae
RE
6864 if (GET_CODE (insn) == JUMP_INSN
6865 && JUMP_LABEL (insn) != NULL
6866 && ((table = next_real_insn (JUMP_LABEL (insn)))
6867 == next_real_insn (insn))
6868 && table != NULL
6869 && GET_CODE (table) == JUMP_INSN
6870 && (GET_CODE (PATTERN (table)) == ADDR_VEC
6871 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6872 return table;
6873
6874 return NULL_RTX;
2b835d68
RE
6875}
6876
657d9449
RE
6877#ifndef JUMP_TABLES_IN_TEXT_SECTION
6878#define JUMP_TABLES_IN_TEXT_SECTION 0
6879#endif
6880
d5b7b3ae 6881static HOST_WIDE_INT
e32bac5b 6882get_jump_table_size (rtx insn)
2b835d68 6883{
657d9449
RE
6884 /* ADDR_VECs only take room if read-only data does into the text
6885 section. */
6886 if (JUMP_TABLES_IN_TEXT_SECTION
d48bc59a 6887#if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
657d9449
RE
6888 || 1
6889#endif
6890 )
6891 {
6892 rtx body = PATTERN (insn);
6893 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
2b835d68 6894
657d9449
RE
6895 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6896 }
6897
6898 return 0;
d5b7b3ae 6899}
2b835d68 6900
d5b7b3ae
RE
6901/* Move a minipool fix MP from its current location to before MAX_MP.
6902 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
093354e0 6903 constraints may need updating. */
d5b7b3ae 6904static Mnode *
e32bac5b
RE
6905move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6906 HOST_WIDE_INT max_address)
d5b7b3ae
RE
6907{
6908 /* This should never be true and the code below assumes these are
6909 different. */
6910 if (mp == max_mp)
6911 abort ();
6912
6913 if (max_mp == NULL)
6914 {
6915 if (max_address < mp->max_address)
6916 mp->max_address = max_address;
6917 }
6918 else
2b835d68 6919 {
d5b7b3ae
RE
6920 if (max_address > max_mp->max_address - mp->fix_size)
6921 mp->max_address = max_mp->max_address - mp->fix_size;
6922 else
6923 mp->max_address = max_address;
2b835d68 6924
d5b7b3ae
RE
6925 /* Unlink MP from its current position. Since max_mp is non-null,
6926 mp->prev must be non-null. */
6927 mp->prev->next = mp->next;
6928 if (mp->next != NULL)
6929 mp->next->prev = mp->prev;
6930 else
6931 minipool_vector_tail = mp->prev;
2b835d68 6932
d5b7b3ae
RE
6933 /* Re-insert it before MAX_MP. */
6934 mp->next = max_mp;
6935 mp->prev = max_mp->prev;
6936 max_mp->prev = mp;
6937
6938 if (mp->prev != NULL)
6939 mp->prev->next = mp;
6940 else
6941 minipool_vector_head = mp;
6942 }
2b835d68 6943
d5b7b3ae
RE
6944 /* Save the new entry. */
6945 max_mp = mp;
6946
d6a7951f 6947 /* Scan over the preceding entries and adjust their addresses as
d5b7b3ae
RE
6948 required. */
6949 while (mp->prev != NULL
6950 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6951 {
6952 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6953 mp = mp->prev;
2b835d68
RE
6954 }
6955
d5b7b3ae 6956 return max_mp;
2b835d68
RE
6957}
6958
d5b7b3ae
RE
6959/* Add a constant to the minipool for a forward reference. Returns the
6960 node added or NULL if the constant will not fit in this pool. */
6961static Mnode *
e32bac5b 6962add_minipool_forward_ref (Mfix *fix)
d5b7b3ae
RE
6963{
6964 /* If set, max_mp is the first pool_entry that has a lower
6965 constraint than the one we are trying to add. */
6966 Mnode * max_mp = NULL;
6967 HOST_WIDE_INT max_address = fix->address + fix->forwards;
6968 Mnode * mp;
6969
6970 /* If this fix's address is greater than the address of the first
6971 entry, then we can't put the fix in this pool. We subtract the
6972 size of the current fix to ensure that if the table is fully
6973 packed we still have enough room to insert this value by suffling
6974 the other fixes forwards. */
6975 if (minipool_vector_head &&
6976 fix->address >= minipool_vector_head->max_address - fix->fix_size)
6977 return NULL;
2b835d68 6978
d5b7b3ae
RE
6979 /* Scan the pool to see if a constant with the same value has
6980 already been added. While we are doing this, also note the
6981 location where we must insert the constant if it doesn't already
6982 exist. */
6983 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6984 {
6985 if (GET_CODE (fix->value) == GET_CODE (mp->value)
6986 && fix->mode == mp->mode
6987 && (GET_CODE (fix->value) != CODE_LABEL
6988 || (CODE_LABEL_NUMBER (fix->value)
6989 == CODE_LABEL_NUMBER (mp->value)))
6990 && rtx_equal_p (fix->value, mp->value))
6991 {
6992 /* More than one fix references this entry. */
6993 mp->refcount++;
6994 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6995 }
6996
6997 /* Note the insertion point if necessary. */
6998 if (max_mp == NULL
6999 && mp->max_address > max_address)
7000 max_mp = mp;
5a9335ef
NC
7001
7002 /* If we are inserting an 8-bytes aligned quantity and
7003 we have not already found an insertion point, then
7004 make sure that all such 8-byte aligned quantities are
7005 placed at the start of the pool. */
5848830f 7006 if (ARM_DOUBLEWORD_ALIGN
5a9335ef
NC
7007 && max_mp == NULL
7008 && fix->fix_size == 8
7009 && mp->fix_size != 8)
7010 {
7011 max_mp = mp;
7012 max_address = mp->max_address;
7013 }
d5b7b3ae
RE
7014 }
7015
7016 /* The value is not currently in the minipool, so we need to create
7017 a new entry for it. If MAX_MP is NULL, the entry will be put on
7018 the end of the list since the placement is less constrained than
7019 any existing entry. Otherwise, we insert the new fix before
6bc82793 7020 MAX_MP and, if necessary, adjust the constraints on the other
d5b7b3ae
RE
7021 entries. */
7022 mp = xmalloc (sizeof (* mp));
7023 mp->fix_size = fix->fix_size;
7024 mp->mode = fix->mode;
7025 mp->value = fix->value;
7026 mp->refcount = 1;
7027 /* Not yet required for a backwards ref. */
7028 mp->min_address = -65536;
7029
7030 if (max_mp == NULL)
7031 {
7032 mp->max_address = max_address;
7033 mp->next = NULL;
7034 mp->prev = minipool_vector_tail;
7035
7036 if (mp->prev == NULL)
7037 {
7038 minipool_vector_head = mp;
7039 minipool_vector_label = gen_label_rtx ();
7551cbc7 7040 }
2b835d68 7041 else
d5b7b3ae 7042 mp->prev->next = mp;
2b835d68 7043
d5b7b3ae
RE
7044 minipool_vector_tail = mp;
7045 }
7046 else
7047 {
7048 if (max_address > max_mp->max_address - mp->fix_size)
7049 mp->max_address = max_mp->max_address - mp->fix_size;
7050 else
7051 mp->max_address = max_address;
7052
7053 mp->next = max_mp;
7054 mp->prev = max_mp->prev;
7055 max_mp->prev = mp;
7056 if (mp->prev != NULL)
7057 mp->prev->next = mp;
7058 else
7059 minipool_vector_head = mp;
7060 }
7061
7062 /* Save the new entry. */
7063 max_mp = mp;
7064
d6a7951f 7065 /* Scan over the preceding entries and adjust their addresses as
d5b7b3ae
RE
7066 required. */
7067 while (mp->prev != NULL
7068 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7069 {
7070 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7071 mp = mp->prev;
2b835d68
RE
7072 }
7073
d5b7b3ae
RE
7074 return max_mp;
7075}
7076
7077static Mnode *
e32bac5b
RE
7078move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7079 HOST_WIDE_INT min_address)
d5b7b3ae
RE
7080{
7081 HOST_WIDE_INT offset;
7082
7083 /* This should never be true, and the code below assumes these are
7084 different. */
7085 if (mp == min_mp)
7086 abort ();
7087
7088 if (min_mp == NULL)
2b835d68 7089 {
d5b7b3ae
RE
7090 if (min_address > mp->min_address)
7091 mp->min_address = min_address;
7092 }
7093 else
7094 {
7095 /* We will adjust this below if it is too loose. */
7096 mp->min_address = min_address;
7097
7098 /* Unlink MP from its current position. Since min_mp is non-null,
7099 mp->next must be non-null. */
7100 mp->next->prev = mp->prev;
7101 if (mp->prev != NULL)
7102 mp->prev->next = mp->next;
7103 else
7104 minipool_vector_head = mp->next;
7105
7106 /* Reinsert it after MIN_MP. */
7107 mp->prev = min_mp;
7108 mp->next = min_mp->next;
7109 min_mp->next = mp;
7110 if (mp->next != NULL)
7111 mp->next->prev = mp;
2b835d68 7112 else
d5b7b3ae
RE
7113 minipool_vector_tail = mp;
7114 }
7115
7116 min_mp = mp;
7117
7118 offset = 0;
7119 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7120 {
7121 mp->offset = offset;
7122 if (mp->refcount > 0)
7123 offset += mp->fix_size;
7124
7125 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7126 mp->next->min_address = mp->min_address + mp->fix_size;
7127 }
7128
7129 return min_mp;
7130}
7131
7132/* Add a constant to the minipool for a backward reference. Returns the
7133 node added or NULL if the constant will not fit in this pool.
7134
7135 Note that the code for insertion for a backwards reference can be
7136 somewhat confusing because the calculated offsets for each fix do
7137 not take into account the size of the pool (which is still under
7138 construction. */
7139static Mnode *
e32bac5b 7140add_minipool_backward_ref (Mfix *fix)
d5b7b3ae
RE
7141{
7142 /* If set, min_mp is the last pool_entry that has a lower constraint
7143 than the one we are trying to add. */
e32bac5b 7144 Mnode *min_mp = NULL;
d5b7b3ae
RE
7145 /* This can be negative, since it is only a constraint. */
7146 HOST_WIDE_INT min_address = fix->address - fix->backwards;
e32bac5b 7147 Mnode *mp;
d5b7b3ae
RE
7148
7149 /* If we can't reach the current pool from this insn, or if we can't
7150 insert this entry at the end of the pool without pushing other
7151 fixes out of range, then we don't try. This ensures that we
7152 can't fail later on. */
7153 if (min_address >= minipool_barrier->address
7154 || (minipool_vector_tail->min_address + fix->fix_size
7155 >= minipool_barrier->address))
7156 return NULL;
7157
7158 /* Scan the pool to see if a constant with the same value has
7159 already been added. While we are doing this, also note the
7160 location where we must insert the constant if it doesn't already
7161 exist. */
7162 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7163 {
7164 if (GET_CODE (fix->value) == GET_CODE (mp->value)
7165 && fix->mode == mp->mode
7166 && (GET_CODE (fix->value) != CODE_LABEL
7167 || (CODE_LABEL_NUMBER (fix->value)
7168 == CODE_LABEL_NUMBER (mp->value)))
7169 && rtx_equal_p (fix->value, mp->value)
7170 /* Check that there is enough slack to move this entry to the
7171 end of the table (this is conservative). */
7172 && (mp->max_address
7173 > (minipool_barrier->address
7174 + minipool_vector_tail->offset
7175 + minipool_vector_tail->fix_size)))
7176 {
7177 mp->refcount++;
7178 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7179 }
7180
7181 if (min_mp != NULL)
7182 mp->min_address += fix->fix_size;
7183 else
7184 {
7185 /* Note the insertion point if necessary. */
7186 if (mp->min_address < min_address)
5a9335ef
NC
7187 {
7188 /* For now, we do not allow the insertion of 8-byte alignment
7189 requiring nodes anywhere but at the start of the pool. */
5848830f
PB
7190 if (ARM_DOUBLEWORD_ALIGN
7191 && fix->fix_size == 8 && mp->fix_size != 8)
5a9335ef
NC
7192 return NULL;
7193 else
7194 min_mp = mp;
7195 }
d5b7b3ae
RE
7196 else if (mp->max_address
7197 < minipool_barrier->address + mp->offset + fix->fix_size)
7198 {
7199 /* Inserting before this entry would push the fix beyond
7200 its maximum address (which can happen if we have
7201 re-located a forwards fix); force the new fix to come
7202 after it. */
7203 min_mp = mp;
7204 min_address = mp->min_address + fix->fix_size;
7205 }
5a9335ef
NC
7206 /* If we are inserting an 8-bytes aligned quantity and
7207 we have not already found an insertion point, then
7208 make sure that all such 8-byte aligned quantities are
7209 placed at the start of the pool. */
5848830f 7210 else if (ARM_DOUBLEWORD_ALIGN
5a9335ef
NC
7211 && min_mp == NULL
7212 && fix->fix_size == 8
7213 && mp->fix_size < 8)
7214 {
7215 min_mp = mp;
7216 min_address = mp->min_address + fix->fix_size;
7217 }
d5b7b3ae
RE
7218 }
7219 }
7220
7221 /* We need to create a new entry. */
7222 mp = xmalloc (sizeof (* mp));
7223 mp->fix_size = fix->fix_size;
7224 mp->mode = fix->mode;
7225 mp->value = fix->value;
7226 mp->refcount = 1;
7227 mp->max_address = minipool_barrier->address + 65536;
7228
7229 mp->min_address = min_address;
7230
7231 if (min_mp == NULL)
7232 {
7233 mp->prev = NULL;
7234 mp->next = minipool_vector_head;
7235
7236 if (mp->next == NULL)
7237 {
7238 minipool_vector_tail = mp;
7239 minipool_vector_label = gen_label_rtx ();
7240 }
7241 else
7242 mp->next->prev = mp;
7243
7244 minipool_vector_head = mp;
7245 }
7246 else
7247 {
7248 mp->next = min_mp->next;
7249 mp->prev = min_mp;
7250 min_mp->next = mp;
da6558fd 7251
d5b7b3ae
RE
7252 if (mp->next != NULL)
7253 mp->next->prev = mp;
7254 else
7255 minipool_vector_tail = mp;
7256 }
7257
7258 /* Save the new entry. */
7259 min_mp = mp;
7260
7261 if (mp->prev)
7262 mp = mp->prev;
7263 else
7264 mp->offset = 0;
7265
7266 /* Scan over the following entries and adjust their offsets. */
7267 while (mp->next != NULL)
7268 {
7269 if (mp->next->min_address < mp->min_address + mp->fix_size)
7270 mp->next->min_address = mp->min_address + mp->fix_size;
7271
7272 if (mp->refcount)
7273 mp->next->offset = mp->offset + mp->fix_size;
7274 else
7275 mp->next->offset = mp->offset;
7276
7277 mp = mp->next;
7278 }
7279
7280 return min_mp;
7281}
7282
7283static void
e32bac5b 7284assign_minipool_offsets (Mfix *barrier)
d5b7b3ae
RE
7285{
7286 HOST_WIDE_INT offset = 0;
e32bac5b 7287 Mnode *mp;
d5b7b3ae
RE
7288
7289 minipool_barrier = barrier;
7290
7291 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7292 {
7293 mp->offset = offset;
da6558fd 7294
d5b7b3ae
RE
7295 if (mp->refcount > 0)
7296 offset += mp->fix_size;
7297 }
7298}
7299
7300/* Output the literal table */
7301static void
e32bac5b 7302dump_minipool (rtx scan)
d5b7b3ae 7303{
5a9335ef
NC
7304 Mnode * mp;
7305 Mnode * nmp;
7306 int align64 = 0;
7307
5848830f 7308 if (ARM_DOUBLEWORD_ALIGN)
5a9335ef
NC
7309 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7310 if (mp->refcount > 0 && mp->fix_size == 8)
7311 {
7312 align64 = 1;
7313 break;
7314 }
d5b7b3ae 7315
c263766c
RH
7316 if (dump_file)
7317 fprintf (dump_file,
5a9335ef
NC
7318 ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7319 INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
d5b7b3ae
RE
7320
7321 scan = emit_label_after (gen_label_rtx (), scan);
5a9335ef 7322 scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
d5b7b3ae
RE
7323 scan = emit_label_after (minipool_vector_label, scan);
7324
7325 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7326 {
7327 if (mp->refcount > 0)
7328 {
c263766c 7329 if (dump_file)
d5b7b3ae 7330 {
c263766c 7331 fprintf (dump_file,
d5b7b3ae
RE
7332 ";; Offset %u, min %ld, max %ld ",
7333 (unsigned) mp->offset, (unsigned long) mp->min_address,
7334 (unsigned long) mp->max_address);
c263766c
RH
7335 arm_print_value (dump_file, mp->value);
7336 fputc ('\n', dump_file);
d5b7b3ae
RE
7337 }
7338
7339 switch (mp->fix_size)
7340 {
7341#ifdef HAVE_consttable_1
7342 case 1:
7343 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7344 break;
7345
7346#endif
7347#ifdef HAVE_consttable_2
7348 case 2:
7349 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7350 break;
7351
7352#endif
7353#ifdef HAVE_consttable_4
7354 case 4:
7355 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7356 break;
7357
7358#endif
7359#ifdef HAVE_consttable_8
7360 case 8:
7361 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7362 break;
7363
7364#endif
7365 default:
7366 abort ();
7367 break;
7368 }
7369 }
7370
7371 nmp = mp->next;
7372 free (mp);
2b835d68
RE
7373 }
7374
d5b7b3ae
RE
7375 minipool_vector_head = minipool_vector_tail = NULL;
7376 scan = emit_insn_after (gen_consttable_end (), scan);
7377 scan = emit_barrier_after (scan);
2b835d68
RE
7378}
7379
d5b7b3ae
RE
7380/* Return the cost of forcibly inserting a barrier after INSN. */
7381static int
e32bac5b 7382arm_barrier_cost (rtx insn)
949d79eb 7383{
d5b7b3ae
RE
7384 /* Basing the location of the pool on the loop depth is preferable,
7385 but at the moment, the basic block information seems to be
7386 corrupt by this stage of the compilation. */
7387 int base_cost = 50;
7388 rtx next = next_nonnote_insn (insn);
7389
7390 if (next != NULL && GET_CODE (next) == CODE_LABEL)
7391 base_cost -= 20;
7392
7393 switch (GET_CODE (insn))
7394 {
7395 case CODE_LABEL:
7396 /* It will always be better to place the table before the label, rather
7397 than after it. */
7398 return 50;
949d79eb 7399
d5b7b3ae
RE
7400 case INSN:
7401 case CALL_INSN:
7402 return base_cost;
7403
7404 case JUMP_INSN:
7405 return base_cost - 10;
7406
7407 default:
7408 return base_cost + 10;
7409 }
7410}
7411
7412/* Find the best place in the insn stream in the range
7413 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7414 Create the barrier by inserting a jump and add a new fix entry for
7415 it. */
7416static Mfix *
e32bac5b 7417create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
d5b7b3ae
RE
7418{
7419 HOST_WIDE_INT count = 0;
7420 rtx barrier;
7421 rtx from = fix->insn;
7422 rtx selected = from;
7423 int selected_cost;
7424 HOST_WIDE_INT selected_address;
7425 Mfix * new_fix;
7426 HOST_WIDE_INT max_count = max_address - fix->address;
7427 rtx label = gen_label_rtx ();
7428
7429 selected_cost = arm_barrier_cost (from);
7430 selected_address = fix->address;
7431
7432 while (from && count < max_count)
7433 {
7434 rtx tmp;
7435 int new_cost;
7436
7437 /* This code shouldn't have been called if there was a natural barrier
7438 within range. */
7439 if (GET_CODE (from) == BARRIER)
7440 abort ();
7441
7442 /* Count the length of this insn. */
7443 count += get_attr_length (from);
7444
7445 /* If there is a jump table, add its length. */
7446 tmp = is_jump_table (from);
7447 if (tmp != NULL)
7448 {
7449 count += get_jump_table_size (tmp);
7450
7451 /* Jump tables aren't in a basic block, so base the cost on
7452 the dispatch insn. If we select this location, we will
7453 still put the pool after the table. */
7454 new_cost = arm_barrier_cost (from);
7455
7456 if (count < max_count && new_cost <= selected_cost)
7457 {
7458 selected = tmp;
7459 selected_cost = new_cost;
7460 selected_address = fix->address + count;
7461 }
7462
7463 /* Continue after the dispatch table. */
7464 from = NEXT_INSN (tmp);
7465 continue;
7466 }
7467
7468 new_cost = arm_barrier_cost (from);
7469
7470 if (count < max_count && new_cost <= selected_cost)
7471 {
7472 selected = from;
7473 selected_cost = new_cost;
7474 selected_address = fix->address + count;
7475 }
7476
7477 from = NEXT_INSN (from);
7478 }
7479
7480 /* Create a new JUMP_INSN that branches around a barrier. */
7481 from = emit_jump_insn_after (gen_jump (label), selected);
7482 JUMP_LABEL (from) = label;
7483 barrier = emit_barrier_after (from);
7484 emit_label_after (label, barrier);
7485
7486 /* Create a minipool barrier entry for the new barrier. */
c7319d87 7487 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
d5b7b3ae
RE
7488 new_fix->insn = barrier;
7489 new_fix->address = selected_address;
7490 new_fix->next = fix->next;
7491 fix->next = new_fix;
7492
7493 return new_fix;
7494}
7495
7496/* Record that there is a natural barrier in the insn stream at
7497 ADDRESS. */
949d79eb 7498static void
e32bac5b 7499push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
2b835d68 7500{
c7319d87 7501 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
ad076f4e 7502
949d79eb
RE
7503 fix->insn = insn;
7504 fix->address = address;
2b835d68 7505
949d79eb
RE
7506 fix->next = NULL;
7507 if (minipool_fix_head != NULL)
7508 minipool_fix_tail->next = fix;
7509 else
7510 minipool_fix_head = fix;
7511
7512 minipool_fix_tail = fix;
7513}
2b835d68 7514
d5b7b3ae
RE
7515/* Record INSN, which will need fixing up to load a value from the
7516 minipool. ADDRESS is the offset of the insn since the start of the
7517 function; LOC is a pointer to the part of the insn which requires
7518 fixing; VALUE is the constant that must be loaded, which is of type
7519 MODE. */
949d79eb 7520static void
e32bac5b
RE
7521push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7522 enum machine_mode mode, rtx value)
949d79eb 7523{
c7319d87 7524 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
949d79eb
RE
7525
7526#ifdef AOF_ASSEMBLER
093354e0 7527 /* PIC symbol references need to be converted into offsets into the
949d79eb 7528 based area. */
d5b7b3ae
RE
7529 /* XXX This shouldn't be done here. */
7530 if (flag_pic && GET_CODE (value) == SYMBOL_REF)
949d79eb
RE
7531 value = aof_pic_entry (value);
7532#endif /* AOF_ASSEMBLER */
7533
7534 fix->insn = insn;
7535 fix->address = address;
7536 fix->loc = loc;
7537 fix->mode = mode;
d5b7b3ae 7538 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
949d79eb 7539 fix->value = value;
d5b7b3ae
RE
7540 fix->forwards = get_attr_pool_range (insn);
7541 fix->backwards = get_attr_neg_pool_range (insn);
7542 fix->minipool = NULL;
949d79eb
RE
7543
7544 /* If an insn doesn't have a range defined for it, then it isn't
7545 expecting to be reworked by this code. Better to abort now than
7546 to generate duff assembly code. */
d5b7b3ae 7547 if (fix->forwards == 0 && fix->backwards == 0)
949d79eb
RE
7548 abort ();
7549
5848830f 7550 /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
5a9335ef
NC
7551 So there might be an empty word before the start of the pool.
7552 Hence we reduce the forward range by 4 to allow for this
7553 possibility. */
5848830f 7554 if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
5a9335ef
NC
7555 fix->forwards -= 4;
7556
c263766c 7557 if (dump_file)
d5b7b3ae 7558 {
c263766c 7559 fprintf (dump_file,
d5b7b3ae
RE
7560 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7561 GET_MODE_NAME (mode),
7562 INSN_UID (insn), (unsigned long) address,
7563 -1 * (long)fix->backwards, (long)fix->forwards);
c263766c
RH
7564 arm_print_value (dump_file, fix->value);
7565 fprintf (dump_file, "\n");
d5b7b3ae
RE
7566 }
7567
6354dc9b 7568 /* Add it to the chain of fixes. */
949d79eb 7569 fix->next = NULL;
d5b7b3ae 7570
949d79eb
RE
7571 if (minipool_fix_head != NULL)
7572 minipool_fix_tail->next = fix;
7573 else
7574 minipool_fix_head = fix;
7575
7576 minipool_fix_tail = fix;
7577}
7578
f0375c66
NC
7579/* Scan INSN and note any of its operands that need fixing.
7580 If DO_PUSHES is false we do not actually push any of the fixups
7581 needed. The function returns TRUE is any fixups were needed/pushed.
7582 This is used by arm_memory_load_p() which needs to know about loads
7583 of constants that will be converted into minipool loads. */
f0375c66 7584static bool
e32bac5b 7585note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
949d79eb 7586{
f0375c66 7587 bool result = false;
949d79eb
RE
7588 int opno;
7589
d5b7b3ae 7590 extract_insn (insn);
949d79eb 7591
5895f793 7592 if (!constrain_operands (1))
949d79eb
RE
7593 fatal_insn_not_found (insn);
7594
8c2a5582
RE
7595 if (recog_data.n_alternatives == 0)
7596 return false;
7597
f0375c66 7598 /* Fill in recog_op_alt with information about the constraints of this insn. */
949d79eb
RE
7599 preprocess_constraints ();
7600
1ccbefce 7601 for (opno = 0; opno < recog_data.n_operands; opno++)
949d79eb 7602 {
6354dc9b 7603 /* Things we need to fix can only occur in inputs. */
36ab44c7 7604 if (recog_data.operand_type[opno] != OP_IN)
949d79eb
RE
7605 continue;
7606
7607 /* If this alternative is a memory reference, then any mention
7608 of constants in this alternative is really to fool reload
7609 into allowing us to accept one there. We need to fix them up
7610 now so that we output the right code. */
7611 if (recog_op_alt[opno][which_alternative].memory_ok)
7612 {
1ccbefce 7613 rtx op = recog_data.operand[opno];
949d79eb
RE
7614
7615 if (CONSTANT_P (op))
f0375c66
NC
7616 {
7617 if (do_pushes)
7618 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7619 recog_data.operand_mode[opno], op);
7620 result = true;
7621 }
d5b7b3ae 7622 else if (GET_CODE (op) == MEM
949d79eb
RE
7623 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7624 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
f0375c66
NC
7625 {
7626 if (do_pushes)
244b1afb
RE
7627 {
7628 rtx cop = avoid_constant_pool_reference (op);
7629
7630 /* Casting the address of something to a mode narrower
7631 than a word can cause avoid_constant_pool_reference()
7632 to return the pool reference itself. That's no good to
7633 us here. Lets just hope that we can use the
7634 constant pool value directly. */
7635 if (op == cop)
c769a35d 7636 cop = get_pool_constant (XEXP (op, 0));
244b1afb
RE
7637
7638 push_minipool_fix (insn, address,
7639 recog_data.operand_loc[opno],
c769a35d 7640 recog_data.operand_mode[opno], cop);
244b1afb 7641 }
f0375c66
NC
7642
7643 result = true;
7644 }
949d79eb 7645 }
2b835d68 7646 }
f0375c66
NC
7647
7648 return result;
2b835d68
RE
7649}
7650
18dbd950
RS
7651/* Gcc puts the pool in the wrong place for ARM, since we can only
7652 load addresses a limited distance around the pc. We do some
7653 special munging to move the constant pool values to the correct
7654 point in the code. */
18dbd950 7655static void
e32bac5b 7656arm_reorg (void)
2b835d68
RE
7657{
7658 rtx insn;
d5b7b3ae
RE
7659 HOST_WIDE_INT address = 0;
7660 Mfix * fix;
ad076f4e 7661
949d79eb 7662 minipool_fix_head = minipool_fix_tail = NULL;
2b835d68 7663
949d79eb
RE
7664 /* The first insn must always be a note, or the code below won't
7665 scan it properly. */
18dbd950
RS
7666 insn = get_insns ();
7667 if (GET_CODE (insn) != NOTE)
949d79eb
RE
7668 abort ();
7669
7670 /* Scan all the insns and record the operands that will need fixing. */
18dbd950 7671 for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
2b835d68 7672 {
9b6b54e2 7673 if (TARGET_CIRRUS_FIX_INVALID_INSNS
f0375c66 7674 && (arm_cirrus_insn_p (insn)
9b6b54e2 7675 || GET_CODE (insn) == JUMP_INSN
f0375c66 7676 || arm_memory_load_p (insn)))
9b6b54e2
NC
7677 cirrus_reorg (insn);
7678
949d79eb 7679 if (GET_CODE (insn) == BARRIER)
d5b7b3ae 7680 push_minipool_barrier (insn, address);
f0375c66 7681 else if (INSN_P (insn))
949d79eb
RE
7682 {
7683 rtx table;
7684
f0375c66 7685 note_invalid_constants (insn, address, true);
949d79eb 7686 address += get_attr_length (insn);
d5b7b3ae 7687
949d79eb
RE
7688 /* If the insn is a vector jump, add the size of the table
7689 and skip the table. */
d5b7b3ae 7690 if ((table = is_jump_table (insn)) != NULL)
2b835d68 7691 {
d5b7b3ae 7692 address += get_jump_table_size (table);
949d79eb
RE
7693 insn = table;
7694 }
7695 }
7696 }
332072db 7697
d5b7b3ae
RE
7698 fix = minipool_fix_head;
7699
949d79eb 7700 /* Now scan the fixups and perform the required changes. */
d5b7b3ae 7701 while (fix)
949d79eb 7702 {
d5b7b3ae
RE
7703 Mfix * ftmp;
7704 Mfix * fdel;
7705 Mfix * last_added_fix;
7706 Mfix * last_barrier = NULL;
7707 Mfix * this_fix;
949d79eb
RE
7708
7709 /* Skip any further barriers before the next fix. */
7710 while (fix && GET_CODE (fix->insn) == BARRIER)
7711 fix = fix->next;
7712
d5b7b3ae 7713 /* No more fixes. */
949d79eb
RE
7714 if (fix == NULL)
7715 break;
332072db 7716
d5b7b3ae 7717 last_added_fix = NULL;
2b835d68 7718
d5b7b3ae 7719 for (ftmp = fix; ftmp; ftmp = ftmp->next)
949d79eb 7720 {
949d79eb 7721 if (GET_CODE (ftmp->insn) == BARRIER)
949d79eb 7722 {
d5b7b3ae
RE
7723 if (ftmp->address >= minipool_vector_head->max_address)
7724 break;
2b835d68 7725
d5b7b3ae 7726 last_barrier = ftmp;
2b835d68 7727 }
d5b7b3ae
RE
7728 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7729 break;
7730
7731 last_added_fix = ftmp; /* Keep track of the last fix added. */
2b835d68 7732 }
949d79eb 7733
d5b7b3ae
RE
7734 /* If we found a barrier, drop back to that; any fixes that we
7735 could have reached but come after the barrier will now go in
7736 the next mini-pool. */
949d79eb
RE
7737 if (last_barrier != NULL)
7738 {
d5b7b3ae
RE
7739 /* Reduce the refcount for those fixes that won't go into this
7740 pool after all. */
7741 for (fdel = last_barrier->next;
7742 fdel && fdel != ftmp;
7743 fdel = fdel->next)
7744 {
7745 fdel->minipool->refcount--;
7746 fdel->minipool = NULL;
7747 }
7748
949d79eb
RE
7749 ftmp = last_barrier;
7750 }
7751 else
2bfa88dc 7752 {
d5b7b3ae
RE
7753 /* ftmp is first fix that we can't fit into this pool and
7754 there no natural barriers that we could use. Insert a
7755 new barrier in the code somewhere between the previous
7756 fix and this one, and arrange to jump around it. */
7757 HOST_WIDE_INT max_address;
7758
7759 /* The last item on the list of fixes must be a barrier, so
7760 we can never run off the end of the list of fixes without
7761 last_barrier being set. */
7762 if (ftmp == NULL)
7763 abort ();
7764
7765 max_address = minipool_vector_head->max_address;
2bfa88dc
RE
7766 /* Check that there isn't another fix that is in range that
7767 we couldn't fit into this pool because the pool was
7768 already too large: we need to put the pool before such an
7769 instruction. */
d5b7b3ae
RE
7770 if (ftmp->address < max_address)
7771 max_address = ftmp->address;
7772
7773 last_barrier = create_fix_barrier (last_added_fix, max_address);
7774 }
7775
7776 assign_minipool_offsets (last_barrier);
7777
7778 while (ftmp)
7779 {
7780 if (GET_CODE (ftmp->insn) != BARRIER
7781 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7782 == NULL))
7783 break;
2bfa88dc 7784
d5b7b3ae 7785 ftmp = ftmp->next;
2bfa88dc 7786 }
949d79eb
RE
7787
7788 /* Scan over the fixes we have identified for this pool, fixing them
7789 up and adding the constants to the pool itself. */
d5b7b3ae 7790 for (this_fix = fix; this_fix && ftmp != this_fix;
949d79eb
RE
7791 this_fix = this_fix->next)
7792 if (GET_CODE (this_fix->insn) != BARRIER)
7793 {
949d79eb
RE
7794 rtx addr
7795 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
7796 minipool_vector_label),
d5b7b3ae 7797 this_fix->minipool->offset);
949d79eb
RE
7798 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7799 }
7800
d5b7b3ae 7801 dump_minipool (last_barrier->insn);
949d79eb 7802 fix = ftmp;
2b835d68 7803 }
4b632bf1 7804
949d79eb
RE
7805 /* From now on we must synthesize any constants that we can't handle
7806 directly. This can happen if the RTL gets split during final
7807 instruction generation. */
4b632bf1 7808 after_arm_reorg = 1;
c7319d87
RE
7809
7810 /* Free the minipool memory. */
7811 obstack_free (&minipool_obstack, minipool_startobj);
2b835d68 7812}
cce8749e
CH
7813\f
7814/* Routines to output assembly language. */
7815
f3bb6135 7816/* If the rtx is the correct value then return the string of the number.
ff9940b0 7817 In this way we can ensure that valid double constants are generated even
6354dc9b 7818 when cross compiling. */
cd2b33d0 7819const char *
e32bac5b 7820fp_immediate_constant (rtx x)
ff9940b0
RE
7821{
7822 REAL_VALUE_TYPE r;
7823 int i;
7824
9b66ebb1
PB
7825 if (!fp_consts_inited)
7826 init_fp_table ();
ff9940b0
RE
7827
7828 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7829 for (i = 0; i < 8; i++)
9b66ebb1
PB
7830 if (REAL_VALUES_EQUAL (r, values_fp[i]))
7831 return strings_fp[i];
f3bb6135 7832
ff9940b0
RE
7833 abort ();
7834}
7835
9997d19d 7836/* As for fp_immediate_constant, but value is passed directly, not in rtx. */
cd2b33d0 7837static const char *
e32bac5b 7838fp_const_from_val (REAL_VALUE_TYPE *r)
9997d19d
RE
7839{
7840 int i;
7841
9b66ebb1
PB
7842 if (!fp_consts_inited)
7843 init_fp_table ();
9997d19d
RE
7844
7845 for (i = 0; i < 8; i++)
9b66ebb1
PB
7846 if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7847 return strings_fp[i];
9997d19d
RE
7848
7849 abort ();
7850}
ff9940b0 7851
cce8749e
CH
7852/* Output the operands of a LDM/STM instruction to STREAM.
7853 MASK is the ARM register set mask of which only bits 0-15 are important.
6d3d9133
NC
7854 REG is the base register, either the frame pointer or the stack pointer,
7855 INSTR is the possibly suffixed load or store instruction. */
d5b7b3ae 7856static void
e32bac5b 7857print_multi_reg (FILE *stream, const char *instr, int reg, int mask)
cce8749e
CH
7858{
7859 int i;
7860 int not_first = FALSE;
7861
1d5473cb 7862 fputc ('\t', stream);
dd18ae56 7863 asm_fprintf (stream, instr, reg);
1d5473cb 7864 fputs (", {", stream);
62b10bbc 7865
d5b7b3ae 7866 for (i = 0; i <= LAST_ARM_REGNUM; i++)
cce8749e
CH
7867 if (mask & (1 << i))
7868 {
7869 if (not_first)
7870 fprintf (stream, ", ");
62b10bbc 7871
dd18ae56 7872 asm_fprintf (stream, "%r", i);
cce8749e
CH
7873 not_first = TRUE;
7874 }
f3bb6135 7875
b17fe233
NC
7876 fprintf (stream, "}");
7877
7878 /* Add a ^ character for the 26-bit ABI, but only if we were loading
1ce53769
NC
7879 the PC. Otherwise we would generate an UNPREDICTABLE instruction.
7880 Strictly speaking the instruction would be unpredicatble only if
7881 we were writing back the base register as well, but since we never
7882 want to generate an LDM type 2 instruction (register bank switching)
7883 which is what you get if the PC is not being loaded, we do not need
7884 to check for writeback. */
b17fe233 7885 if (! TARGET_APCS_32
1ce53769 7886 && ((mask & (1 << PC_REGNUM)) != 0))
b17fe233
NC
7887 fprintf (stream, "^");
7888
7889 fprintf (stream, "\n");
f3bb6135 7890}
cce8749e 7891
9b66ebb1 7892
9728c9d1
PB
7893/* Output a FLDMX instruction to STREAM.
7894 BASE if the register containing the address.
7895 REG and COUNT specify the register range.
7896 Extra registers may be added to avoid hardware bugs. */
9b66ebb1
PB
7897
7898static void
9728c9d1 7899arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
9b66ebb1
PB
7900{
7901 int i;
7902
9728c9d1
PB
7903 /* Workaround ARM10 VFPr1 bug. */
7904 if (count == 2 && !arm_arch6)
7905 {
7906 if (reg == 15)
7907 reg--;
7908 count++;
7909 }
7910
9b66ebb1 7911 fputc ('\t', stream);
9728c9d1 7912 asm_fprintf (stream, "fldmfdx\t%r!, {", base);
9b66ebb1 7913
9728c9d1 7914 for (i = reg; i < reg + count; i++)
9b66ebb1 7915 {
9728c9d1 7916 if (i > reg)
9b66ebb1 7917 fputs (", ", stream);
9728c9d1 7918 asm_fprintf (stream, "d%d", i);
9b66ebb1
PB
7919 }
7920 fputs ("}\n", stream);
9728c9d1 7921
9b66ebb1
PB
7922}
7923
7924
7925/* Output the assembly for a store multiple. */
7926
7927const char *
7928vfp_output_fstmx (rtx * operands)
7929{
7930 char pattern[100];
7931 int p;
7932 int base;
7933 int i;
7934
7935 strcpy (pattern, "fstmfdx\t%m0!, {%P1");
7936 p = strlen (pattern);
7937
7938 if (GET_CODE (operands[1]) != REG)
7939 abort ();
7940
7941 base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
7942 for (i = 1; i < XVECLEN (operands[2], 0); i++)
7943 {
7944 p += sprintf (&pattern[p], ", d%d", base + i);
7945 }
7946 strcpy (&pattern[p], "}");
7947
7948 output_asm_insn (pattern, operands);
7949 return "";
7950}
7951
7952
9728c9d1
PB
7953/* Emit RTL to save block of VFP register pairs to the stack. Returns the
7954 number of bytes pushed. */
9b66ebb1 7955
9728c9d1 7956static int
9b66ebb1
PB
7957vfp_emit_fstmx (int base_reg, int count)
7958{
7959 rtx par;
7960 rtx dwarf;
7961 rtx tmp, reg;
7962 int i;
7963
9728c9d1
PB
7964 /* Workaround ARM10 VFPr1 bug. Data corruption can occur when exactly two
7965 register pairs are stored by a store multiple insn. We avoid this
7966 by pushing an extra pair. */
7967 if (count == 2 && !arm_arch6)
7968 {
7969 if (base_reg == LAST_VFP_REGNUM - 3)
7970 base_reg -= 2;
7971 count++;
7972 }
7973
9b66ebb1
PB
7974 /* ??? The frame layout is implementation defined. We describe
7975 standard format 1 (equivalent to a FSTMD insn and unused pad word).
7976 We really need some way of representing the whole block so that the
7977 unwinder can figure it out at runtime. */
7978 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7979 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
7980
7981 reg = gen_rtx_REG (DFmode, base_reg);
7982 base_reg += 2;
7983
7984 XVECEXP (par, 0, 0)
7985 = gen_rtx_SET (VOIDmode,
7986 gen_rtx_MEM (BLKmode,
7987 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7988 gen_rtx_UNSPEC (BLKmode,
7989 gen_rtvec (1, reg),
7990 UNSPEC_PUSH_MULT));
7991
7992 tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
7993 gen_rtx_PLUS (SImode, stack_pointer_rtx,
7994 GEN_INT (-(count * 8 + 4))));
7995 RTX_FRAME_RELATED_P (tmp) = 1;
7996 XVECEXP (dwarf, 0, 0) = tmp;
7997
7998 tmp = gen_rtx_SET (VOIDmode,
7999 gen_rtx_MEM (DFmode, stack_pointer_rtx),
8000 reg);
8001 RTX_FRAME_RELATED_P (tmp) = 1;
8002 XVECEXP (dwarf, 0, 1) = tmp;
8003
8004 for (i = 1; i < count; i++)
8005 {
8006 reg = gen_rtx_REG (DFmode, base_reg);
8007 base_reg += 2;
8008 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
8009
8010 tmp = gen_rtx_SET (VOIDmode,
8011 gen_rtx_MEM (DFmode,
8012 gen_rtx_PLUS (SImode,
8013 stack_pointer_rtx,
8014 GEN_INT (i * 8))),
8015 reg);
8016 RTX_FRAME_RELATED_P (tmp) = 1;
8017 XVECEXP (dwarf, 0, i + 1) = tmp;
8018 }
8019
8020 par = emit_insn (par);
8021 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8022 REG_NOTES (par));
9728c9d1
PB
8023 RTX_FRAME_RELATED_P (par) = 1;
8024
8025 return count * 8 + 4;
9b66ebb1
PB
8026}
8027
8028
6354dc9b 8029/* Output a 'call' insn. */
cd2b33d0 8030const char *
e32bac5b 8031output_call (rtx *operands)
cce8749e 8032{
6354dc9b 8033 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
cce8749e 8034
62b10bbc 8035 if (REGNO (operands[0]) == LR_REGNUM)
cce8749e 8036 {
62b10bbc 8037 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
1d5473cb 8038 output_asm_insn ("mov%?\t%0, %|lr", operands);
cce8749e 8039 }
62b10bbc 8040
1d5473cb 8041 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
da6558fd 8042
6cfc7210 8043 if (TARGET_INTERWORK)
da6558fd
NC
8044 output_asm_insn ("bx%?\t%0", operands);
8045 else
8046 output_asm_insn ("mov%?\t%|pc, %0", operands);
8047
f3bb6135
RE
8048 return "";
8049}
cce8749e 8050
6354dc9b 8051/* Output a 'call' insn that is a reference in memory. */
cd2b33d0 8052const char *
e32bac5b 8053output_call_mem (rtx *operands)
ff9940b0 8054{
6cfc7210 8055 if (TARGET_INTERWORK)
da6558fd
NC
8056 {
8057 output_asm_insn ("ldr%?\t%|ip, %0", operands);
8058 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8059 output_asm_insn ("bx%?\t%|ip", operands);
8060 }
6ab5da80
RE
8061 else if (regno_use_in (LR_REGNUM, operands[0]))
8062 {
8063 /* LR is used in the memory address. We load the address in the
8064 first instruction. It's safe to use IP as the target of the
8065 load since the call will kill it anyway. */
8066 output_asm_insn ("ldr%?\t%|ip, %0", operands);
8067 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8068 output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8069 }
da6558fd
NC
8070 else
8071 {
8072 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8073 output_asm_insn ("ldr%?\t%|pc, %0", operands);
8074 }
8075
f3bb6135
RE
8076 return "";
8077}
ff9940b0
RE
8078
8079
3b684012
RE
8080/* Output a move from arm registers to an fpa registers.
8081 OPERANDS[0] is an fpa register.
ff9940b0 8082 OPERANDS[1] is the first registers of an arm register pair. */
cd2b33d0 8083const char *
e32bac5b 8084output_mov_long_double_fpa_from_arm (rtx *operands)
ff9940b0
RE
8085{
8086 int arm_reg0 = REGNO (operands[1]);
8087 rtx ops[3];
8088
62b10bbc
NC
8089 if (arm_reg0 == IP_REGNUM)
8090 abort ();
f3bb6135 8091
43cffd11
RE
8092 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8093 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8094 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
ff9940b0 8095
1d5473cb
RE
8096 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
8097 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
62b10bbc 8098
f3bb6135
RE
8099 return "";
8100}
ff9940b0 8101
3b684012 8102/* Output a move from an fpa register to arm registers.
ff9940b0 8103 OPERANDS[0] is the first registers of an arm register pair.
3b684012 8104 OPERANDS[1] is an fpa register. */
cd2b33d0 8105const char *
e32bac5b 8106output_mov_long_double_arm_from_fpa (rtx *operands)
ff9940b0
RE
8107{
8108 int arm_reg0 = REGNO (operands[0]);
8109 rtx ops[3];
8110
62b10bbc
NC
8111 if (arm_reg0 == IP_REGNUM)
8112 abort ();
f3bb6135 8113
43cffd11
RE
8114 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8115 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8116 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
ff9940b0 8117
1d5473cb
RE
8118 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8119 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
f3bb6135
RE
8120 return "";
8121}
ff9940b0
RE
8122
8123/* Output a move from arm registers to arm registers of a long double
8124 OPERANDS[0] is the destination.
8125 OPERANDS[1] is the source. */
cd2b33d0 8126const char *
e32bac5b 8127output_mov_long_double_arm_from_arm (rtx *operands)
ff9940b0 8128{
6354dc9b 8129 /* We have to be careful here because the two might overlap. */
ff9940b0
RE
8130 int dest_start = REGNO (operands[0]);
8131 int src_start = REGNO (operands[1]);
8132 rtx ops[2];
8133 int i;
8134
8135 if (dest_start < src_start)
8136 {
8137 for (i = 0; i < 3; i++)
8138 {
43cffd11
RE
8139 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8140 ops[1] = gen_rtx_REG (SImode, src_start + i);
9997d19d 8141 output_asm_insn ("mov%?\t%0, %1", ops);
ff9940b0
RE
8142 }
8143 }
8144 else
8145 {
8146 for (i = 2; i >= 0; i--)
8147 {
43cffd11
RE
8148 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8149 ops[1] = gen_rtx_REG (SImode, src_start + i);
9997d19d 8150 output_asm_insn ("mov%?\t%0, %1", ops);
ff9940b0
RE
8151 }
8152 }
f3bb6135 8153
ff9940b0
RE
8154 return "";
8155}
8156
8157
3b684012
RE
8158/* Output a move from arm registers to an fpa registers.
8159 OPERANDS[0] is an fpa register.
cce8749e 8160 OPERANDS[1] is the first registers of an arm register pair. */
cd2b33d0 8161const char *
e32bac5b 8162output_mov_double_fpa_from_arm (rtx *operands)
cce8749e
CH
8163{
8164 int arm_reg0 = REGNO (operands[1]);
8165 rtx ops[2];
8166
62b10bbc
NC
8167 if (arm_reg0 == IP_REGNUM)
8168 abort ();
8169
43cffd11
RE
8170 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8171 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
1d5473cb
RE
8172 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8173 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
f3bb6135
RE
8174 return "";
8175}
cce8749e 8176
3b684012 8177/* Output a move from an fpa register to arm registers.
cce8749e 8178 OPERANDS[0] is the first registers of an arm register pair.
3b684012 8179 OPERANDS[1] is an fpa register. */
cd2b33d0 8180const char *
e32bac5b 8181output_mov_double_arm_from_fpa (rtx *operands)
cce8749e
CH
8182{
8183 int arm_reg0 = REGNO (operands[0]);
8184 rtx ops[2];
8185
62b10bbc
NC
8186 if (arm_reg0 == IP_REGNUM)
8187 abort ();
f3bb6135 8188
43cffd11
RE
8189 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8190 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
1d5473cb
RE
8191 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8192 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
f3bb6135
RE
8193 return "";
8194}
cce8749e
CH
8195
8196/* Output a move between double words.
8197 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8198 or MEM<-REG and all MEMs must be offsettable addresses. */
cd2b33d0 8199const char *
e32bac5b 8200output_move_double (rtx *operands)
cce8749e
CH
8201{
8202 enum rtx_code code0 = GET_CODE (operands[0]);
8203 enum rtx_code code1 = GET_CODE (operands[1]);
56636818 8204 rtx otherops[3];
cce8749e
CH
8205
8206 if (code0 == REG)
8207 {
8208 int reg0 = REGNO (operands[0]);
8209
43cffd11 8210 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
aec3cfba 8211
cce8749e
CH
8212 if (code1 == REG)
8213 {
8214 int reg1 = REGNO (operands[1]);
62b10bbc
NC
8215 if (reg1 == IP_REGNUM)
8216 abort ();
f3bb6135 8217
6354dc9b 8218 /* Ensure the second source is not overwritten. */
c1c2bc04 8219 if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
6cfc7210 8220 output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
cce8749e 8221 else
6cfc7210 8222 output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
cce8749e 8223 }
5a9335ef
NC
8224 else if (code1 == CONST_VECTOR)
8225 {
8226 HOST_WIDE_INT hint = 0;
8227
8228 switch (GET_MODE (operands[1]))
8229 {
8230 case V2SImode:
8231 otherops[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 1)));
8232 operands[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 0)));
8233 break;
8234
8235 case V4HImode:
8236 if (BYTES_BIG_ENDIAN)
8237 {
8238 hint = INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8239 hint <<= 16;
8240 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8241 }
8242 else
8243 {
8244 hint = INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8245 hint <<= 16;
8246 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8247 }
8248
8249 otherops[1] = GEN_INT (hint);
8250 hint = 0;
8251
8252 if (BYTES_BIG_ENDIAN)
8253 {
8254 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8255 hint <<= 16;
8256 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8257 }
8258 else
8259 {
8260 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8261 hint <<= 16;
8262 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8263 }
8264
8265 operands[1] = GEN_INT (hint);
8266 break;
8267
8268 case V8QImode:
8269 if (BYTES_BIG_ENDIAN)
8270 {
8271 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8272 hint <<= 8;
8273 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8274 hint <<= 8;
8275 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8276 hint <<= 8;
8277 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8278 }
8279 else
8280 {
8281 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8282 hint <<= 8;
8283 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8284 hint <<= 8;
8285 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8286 hint <<= 8;
8287 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8288 }
8289
8290 otherops[1] = GEN_INT (hint);
8291 hint = 0;
8292
8293 if (BYTES_BIG_ENDIAN)
8294 {
8295 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8296 hint <<= 8;
8297 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8298 hint <<= 8;
8299 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8300 hint <<= 8;
8301 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8302 }
8303 else
8304 {
8305 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8306 hint <<= 8;
8307 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8308 hint <<= 8;
8309 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8310 hint <<= 8;
8311 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8312 }
8313
8314 operands[1] = GEN_INT (hint);
8315 break;
8316
8317 default:
8318 abort ();
8319 }
8320 output_mov_immediate (operands);
8321 output_mov_immediate (otherops);
8322 }
cce8749e
CH
8323 else if (code1 == CONST_DOUBLE)
8324 {
226a5051
RE
8325 if (GET_MODE (operands[1]) == DFmode)
8326 {
b216cd4a 8327 REAL_VALUE_TYPE r;
226a5051 8328 long l[2];
226a5051 8329
b216cd4a
ZW
8330 REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
8331 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
d5b7b3ae
RE
8332 otherops[1] = GEN_INT (l[1]);
8333 operands[1] = GEN_INT (l[0]);
226a5051 8334 }
c1c2bc04
RE
8335 else if (GET_MODE (operands[1]) != VOIDmode)
8336 abort ();
8337 else if (WORDS_BIG_ENDIAN)
8338 {
c1c2bc04
RE
8339 otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8340 operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8341 }
226a5051
RE
8342 else
8343 {
8344 otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8345 operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8346 }
6cfc7210 8347
c1c2bc04
RE
8348 output_mov_immediate (operands);
8349 output_mov_immediate (otherops);
cce8749e
CH
8350 }
8351 else if (code1 == CONST_INT)
8352 {
56636818
JL
8353#if HOST_BITS_PER_WIDE_INT > 32
8354 /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
8355 what the upper word is. */
8356 if (WORDS_BIG_ENDIAN)
8357 {
8358 otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8359 operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8360 }
8361 else
8362 {
8363 otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8364 operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8365 }
8366#else
6354dc9b 8367 /* Sign extend the intval into the high-order word. */
c1c2bc04
RE
8368 if (WORDS_BIG_ENDIAN)
8369 {
8370 otherops[1] = operands[1];
8371 operands[1] = (INTVAL (operands[1]) < 0
8372 ? constm1_rtx : const0_rtx);
8373 }
ff9940b0 8374 else
c1c2bc04 8375 otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
56636818 8376#endif
c1c2bc04
RE
8377 output_mov_immediate (otherops);
8378 output_mov_immediate (operands);
cce8749e
CH
8379 }
8380 else if (code1 == MEM)
8381 {
ff9940b0 8382 switch (GET_CODE (XEXP (operands[1], 0)))
cce8749e 8383 {
ff9940b0 8384 case REG:
9997d19d 8385 output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
ff9940b0 8386 break;
2b835d68 8387
ff9940b0 8388 case PRE_INC:
6354dc9b 8389 abort (); /* Should never happen now. */
ff9940b0 8390 break;
2b835d68 8391
ff9940b0 8392 case PRE_DEC:
2b835d68 8393 output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
ff9940b0 8394 break;
2b835d68 8395
ff9940b0 8396 case POST_INC:
9997d19d 8397 output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
ff9940b0 8398 break;
2b835d68 8399
ff9940b0 8400 case POST_DEC:
6354dc9b 8401 abort (); /* Should never happen now. */
ff9940b0 8402 break;
2b835d68
RE
8403
8404 case LABEL_REF:
8405 case CONST:
8406 output_asm_insn ("adr%?\t%0, %1", operands);
8407 output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8408 break;
8409
ff9940b0 8410 default:
aec3cfba
NC
8411 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8412 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
cce8749e 8413 {
2b835d68
RE
8414 otherops[0] = operands[0];
8415 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8416 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
1d6e90ac 8417
2b835d68
RE
8418 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8419 {
8420 if (GET_CODE (otherops[2]) == CONST_INT)
8421 {
06bea5aa 8422 switch ((int) INTVAL (otherops[2]))
2b835d68
RE
8423 {
8424 case -8:
8425 output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8426 return "";
8427 case -4:
8428 output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8429 return "";
8430 case 4:
8431 output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8432 return "";
8433 }
1d6e90ac 8434
2b835d68
RE
8435 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8436 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8437 else
8438 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8439 }
8440 else
8441 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8442 }
8443 else
8444 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
6cfc7210 8445
2b835d68
RE
8446 return "ldm%?ia\t%0, %M0";
8447 }
8448 else
8449 {
a4a37b30 8450 otherops[1] = adjust_address (operands[1], SImode, 4);
2b835d68
RE
8451 /* Take care of overlapping base/data reg. */
8452 if (reg_mentioned_p (operands[0], operands[1]))
8453 {
8454 output_asm_insn ("ldr%?\t%0, %1", otherops);
8455 output_asm_insn ("ldr%?\t%0, %1", operands);
8456 }
8457 else
8458 {
8459 output_asm_insn ("ldr%?\t%0, %1", operands);
8460 output_asm_insn ("ldr%?\t%0, %1", otherops);
8461 }
cce8749e
CH
8462 }
8463 }
8464 }
2b835d68 8465 else
6354dc9b 8466 abort (); /* Constraints should prevent this. */
cce8749e
CH
8467 }
8468 else if (code0 == MEM && code1 == REG)
8469 {
62b10bbc
NC
8470 if (REGNO (operands[1]) == IP_REGNUM)
8471 abort ();
2b835d68 8472
ff9940b0
RE
8473 switch (GET_CODE (XEXP (operands[0], 0)))
8474 {
8475 case REG:
9997d19d 8476 output_asm_insn ("stm%?ia\t%m0, %M1", operands);
ff9940b0 8477 break;
2b835d68 8478
ff9940b0 8479 case PRE_INC:
6354dc9b 8480 abort (); /* Should never happen now. */
ff9940b0 8481 break;
2b835d68 8482
ff9940b0 8483 case PRE_DEC:
2b835d68 8484 output_asm_insn ("stm%?db\t%m0!, %M1", operands);
ff9940b0 8485 break;
2b835d68 8486
ff9940b0 8487 case POST_INC:
9997d19d 8488 output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
ff9940b0 8489 break;
2b835d68 8490
ff9940b0 8491 case POST_DEC:
6354dc9b 8492 abort (); /* Should never happen now. */
ff9940b0 8493 break;
2b835d68
RE
8494
8495 case PLUS:
8496 if (GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == CONST_INT)
8497 {
06bea5aa 8498 switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
2b835d68
RE
8499 {
8500 case -8:
8501 output_asm_insn ("stm%?db\t%m0, %M1", operands);
8502 return "";
8503
8504 case -4:
8505 output_asm_insn ("stm%?da\t%m0, %M1", operands);
8506 return "";
8507
8508 case 4:
8509 output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8510 return "";
8511 }
8512 }
8513 /* Fall through */
8514
ff9940b0 8515 default:
a4a37b30 8516 otherops[0] = adjust_address (operands[0], SImode, 4);
43cffd11 8517 otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
9997d19d
RE
8518 output_asm_insn ("str%?\t%1, %0", operands);
8519 output_asm_insn ("str%?\t%1, %0", otherops);
cce8749e
CH
8520 }
8521 }
2b835d68 8522 else
1d6e90ac
NC
8523 /* Constraints should prevent this. */
8524 abort ();
cce8749e 8525
9997d19d
RE
8526 return "";
8527}
cce8749e
CH
8528
8529
8530/* Output an arbitrary MOV reg, #n.
8531 OPERANDS[0] is a register. OPERANDS[1] is a const_int. */
cd2b33d0 8532const char *
e32bac5b 8533output_mov_immediate (rtx *operands)
cce8749e 8534{
f3bb6135 8535 HOST_WIDE_INT n = INTVAL (operands[1]);
cce8749e 8536
1d6e90ac 8537 /* Try to use one MOV. */
cce8749e 8538 if (const_ok_for_arm (n))
1d6e90ac 8539 output_asm_insn ("mov%?\t%0, %1", operands);
cce8749e 8540
1d6e90ac
NC
8541 /* Try to use one MVN. */
8542 else if (const_ok_for_arm (~n))
cce8749e 8543 {
f3bb6135 8544 operands[1] = GEN_INT (~n);
9997d19d 8545 output_asm_insn ("mvn%?\t%0, %1", operands);
cce8749e 8546 }
1d6e90ac
NC
8547 else
8548 {
8549 int n_ones = 0;
8550 int i;
cce8749e 8551
1d6e90ac 8552 /* If all else fails, make it out of ORRs or BICs as appropriate. */
5a9335ef 8553 for (i = 0; i < 32; i++)
1d6e90ac 8554 if (n & 1 << i)
5a9335ef 8555 n_ones++;
cce8749e 8556
1d6e90ac
NC
8557 if (n_ones > 16) /* Shorter to use MVN with BIC in this case. */
8558 output_multi_immediate (operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1, ~ n);
8559 else
8560 output_multi_immediate (operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1, n);
8561 }
f3bb6135
RE
8562
8563 return "";
8564}
cce8749e 8565
1d6e90ac
NC
8566/* Output an ADD r, s, #n where n may be too big for one instruction.
8567 If adding zero to one register, output nothing. */
cd2b33d0 8568const char *
e32bac5b 8569output_add_immediate (rtx *operands)
cce8749e 8570{
f3bb6135 8571 HOST_WIDE_INT n = INTVAL (operands[2]);
cce8749e
CH
8572
8573 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8574 {
8575 if (n < 0)
8576 output_multi_immediate (operands,
9997d19d
RE
8577 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8578 -n);
cce8749e
CH
8579 else
8580 output_multi_immediate (operands,
9997d19d
RE
8581 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8582 n);
cce8749e 8583 }
f3bb6135
RE
8584
8585 return "";
8586}
cce8749e 8587
cce8749e
CH
8588/* Output a multiple immediate operation.
8589 OPERANDS is the vector of operands referred to in the output patterns.
8590 INSTR1 is the output pattern to use for the first constant.
8591 INSTR2 is the output pattern to use for subsequent constants.
8592 IMMED_OP is the index of the constant slot in OPERANDS.
8593 N is the constant value. */
cd2b33d0 8594static const char *
e32bac5b
RE
8595output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8596 int immed_op, HOST_WIDE_INT n)
cce8749e 8597{
f3bb6135 8598#if HOST_BITS_PER_WIDE_INT > 32
30cf4896 8599 n &= 0xffffffff;
f3bb6135
RE
8600#endif
8601
cce8749e
CH
8602 if (n == 0)
8603 {
1d6e90ac 8604 /* Quick and easy output. */
cce8749e 8605 operands[immed_op] = const0_rtx;
1d6e90ac 8606 output_asm_insn (instr1, operands);
cce8749e
CH
8607 }
8608 else
8609 {
8610 int i;
cd2b33d0 8611 const char * instr = instr1;
cce8749e 8612
6354dc9b 8613 /* Note that n is never zero here (which would give no output). */
cce8749e
CH
8614 for (i = 0; i < 32; i += 2)
8615 {
8616 if (n & (3 << i))
8617 {
f3bb6135
RE
8618 operands[immed_op] = GEN_INT (n & (255 << i));
8619 output_asm_insn (instr, operands);
cce8749e
CH
8620 instr = instr2;
8621 i += 6;
8622 }
8623 }
8624 }
cd2b33d0 8625
f3bb6135 8626 return "";
9997d19d 8627}
cce8749e 8628
cce8749e
CH
8629/* Return the appropriate ARM instruction for the operation code.
8630 The returned result should not be overwritten. OP is the rtx of the
8631 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8632 was shifted. */
cd2b33d0 8633const char *
e32bac5b 8634arithmetic_instr (rtx op, int shift_first_arg)
cce8749e 8635{
9997d19d 8636 switch (GET_CODE (op))
cce8749e
CH
8637 {
8638 case PLUS:
f3bb6135
RE
8639 return "add";
8640
cce8749e 8641 case MINUS:
f3bb6135
RE
8642 return shift_first_arg ? "rsb" : "sub";
8643
cce8749e 8644 case IOR:
f3bb6135
RE
8645 return "orr";
8646
cce8749e 8647 case XOR:
f3bb6135
RE
8648 return "eor";
8649
cce8749e 8650 case AND:
f3bb6135
RE
8651 return "and";
8652
cce8749e 8653 default:
f3bb6135 8654 abort ();
cce8749e 8655 }
f3bb6135 8656}
cce8749e 8657
cce8749e
CH
8658/* Ensure valid constant shifts and return the appropriate shift mnemonic
8659 for the operation code. The returned result should not be overwritten.
8660 OP is the rtx code of the shift.
9997d19d 8661 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
6354dc9b 8662 shift. */
cd2b33d0 8663static const char *
e32bac5b 8664shift_op (rtx op, HOST_WIDE_INT *amountp)
cce8749e 8665{
cd2b33d0 8666 const char * mnem;
e2c671ba 8667 enum rtx_code code = GET_CODE (op);
cce8749e 8668
9997d19d
RE
8669 if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
8670 *amountp = -1;
8671 else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
8672 *amountp = INTVAL (XEXP (op, 1));
8673 else
8674 abort ();
8675
e2c671ba 8676 switch (code)
cce8749e
CH
8677 {
8678 case ASHIFT:
8679 mnem = "asl";
8680 break;
f3bb6135 8681
cce8749e
CH
8682 case ASHIFTRT:
8683 mnem = "asr";
cce8749e 8684 break;
f3bb6135 8685
cce8749e
CH
8686 case LSHIFTRT:
8687 mnem = "lsr";
cce8749e 8688 break;
f3bb6135 8689
9997d19d
RE
8690 case ROTATERT:
8691 mnem = "ror";
9997d19d
RE
8692 break;
8693
ff9940b0 8694 case MULT:
e2c671ba
RE
8695 /* We never have to worry about the amount being other than a
8696 power of 2, since this case can never be reloaded from a reg. */
9997d19d
RE
8697 if (*amountp != -1)
8698 *amountp = int_log2 (*amountp);
8699 else
8700 abort ();
f3bb6135
RE
8701 return "asl";
8702
cce8749e 8703 default:
f3bb6135 8704 abort ();
cce8749e
CH
8705 }
8706
e2c671ba
RE
8707 if (*amountp != -1)
8708 {
8709 /* This is not 100% correct, but follows from the desire to merge
8710 multiplication by a power of 2 with the recognizer for a
8711 shift. >=32 is not a valid shift for "asl", so we must try and
8712 output a shift that produces the correct arithmetical result.
ddd5a7c1 8713 Using lsr #32 is identical except for the fact that the carry bit
e2c671ba
RE
8714 is not set correctly if we set the flags; but we never use the
8715 carry bit from such an operation, so we can ignore that. */
8716 if (code == ROTATERT)
1d6e90ac
NC
8717 /* Rotate is just modulo 32. */
8718 *amountp &= 31;
e2c671ba
RE
8719 else if (*amountp != (*amountp & 31))
8720 {
8721 if (code == ASHIFT)
8722 mnem = "lsr";
8723 *amountp = 32;
8724 }
8725
8726 /* Shifts of 0 are no-ops. */
8727 if (*amountp == 0)
8728 return NULL;
8729 }
8730
9997d19d
RE
8731 return mnem;
8732}
cce8749e 8733
6354dc9b 8734/* Obtain the shift from the POWER of two. */
1d6e90ac 8735
18af7313 8736static HOST_WIDE_INT
e32bac5b 8737int_log2 (HOST_WIDE_INT power)
cce8749e 8738{
f3bb6135 8739 HOST_WIDE_INT shift = 0;
cce8749e 8740
30cf4896 8741 while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
cce8749e
CH
8742 {
8743 if (shift > 31)
f3bb6135 8744 abort ();
e32bac5b 8745 shift++;
cce8749e 8746 }
f3bb6135
RE
8747
8748 return shift;
8749}
cce8749e 8750
cce8749e
CH
8751/* Output a .ascii pseudo-op, keeping track of lengths. This is because
8752 /bin/as is horribly restrictive. */
6cfc7210 8753#define MAX_ASCII_LEN 51
cce8749e
CH
8754
8755void
e32bac5b 8756output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
cce8749e
CH
8757{
8758 int i;
6cfc7210 8759 int len_so_far = 0;
cce8749e 8760
6cfc7210
NC
8761 fputs ("\t.ascii\t\"", stream);
8762
cce8749e
CH
8763 for (i = 0; i < len; i++)
8764 {
1d6e90ac 8765 int c = p[i];
cce8749e 8766
6cfc7210 8767 if (len_so_far >= MAX_ASCII_LEN)
cce8749e 8768 {
6cfc7210 8769 fputs ("\"\n\t.ascii\t\"", stream);
cce8749e 8770 len_so_far = 0;
cce8749e
CH
8771 }
8772
6cfc7210 8773 switch (c)
cce8749e 8774 {
6cfc7210
NC
8775 case TARGET_TAB:
8776 fputs ("\\t", stream);
8777 len_so_far += 2;
8778 break;
8779
8780 case TARGET_FF:
8781 fputs ("\\f", stream);
8782 len_so_far += 2;
8783 break;
8784
8785 case TARGET_BS:
8786 fputs ("\\b", stream);
8787 len_so_far += 2;
8788 break;
8789
8790 case TARGET_CR:
8791 fputs ("\\r", stream);
8792 len_so_far += 2;
8793 break;
8794
8795 case TARGET_NEWLINE:
8796 fputs ("\\n", stream);
8797 c = p [i + 1];
8798 if ((c >= ' ' && c <= '~')
8799 || c == TARGET_TAB)
8800 /* This is a good place for a line break. */
8801 len_so_far = MAX_ASCII_LEN;
8802 else
8803 len_so_far += 2;
8804 break;
8805
8806 case '\"':
8807 case '\\':
8808 putc ('\\', stream);
5895f793 8809 len_so_far++;
d6b4baa4 8810 /* Drop through. */
f3bb6135 8811
6cfc7210
NC
8812 default:
8813 if (c >= ' ' && c <= '~')
8814 {
8815 putc (c, stream);
5895f793 8816 len_so_far++;
6cfc7210
NC
8817 }
8818 else
8819 {
8820 fprintf (stream, "\\%03o", c);
8821 len_so_far += 4;
8822 }
8823 break;
cce8749e 8824 }
cce8749e 8825 }
f3bb6135 8826
cce8749e 8827 fputs ("\"\n", stream);
f3bb6135 8828}
cce8749e 8829\f
121308d4 8830/* Compute the register sabe mask for registers 0 through 12
5848830f 8831 inclusive. This code is used by arm_compute_save_reg_mask. */
6d3d9133 8832static unsigned long
e32bac5b 8833arm_compute_save_reg0_reg12_mask (void)
6d3d9133 8834{
121308d4 8835 unsigned long func_type = arm_current_func_type ();
6d3d9133
NC
8836 unsigned int save_reg_mask = 0;
8837 unsigned int reg;
6d3d9133 8838
7b8b8ade 8839 if (IS_INTERRUPT (func_type))
6d3d9133 8840 {
7b8b8ade 8841 unsigned int max_reg;
7b8b8ade
NC
8842 /* Interrupt functions must not corrupt any registers,
8843 even call clobbered ones. If this is a leaf function
8844 we can just examine the registers used by the RTL, but
8845 otherwise we have to assume that whatever function is
8846 called might clobber anything, and so we have to save
8847 all the call-clobbered registers as well. */
8848 if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8849 /* FIQ handlers have registers r8 - r12 banked, so
8850 we only need to check r0 - r7, Normal ISRs only
121308d4 8851 bank r14 and r15, so we must check up to r12.
7b8b8ade
NC
8852 r13 is the stack pointer which is always preserved,
8853 so we do not need to consider it here. */
8854 max_reg = 7;
8855 else
8856 max_reg = 12;
8857
8858 for (reg = 0; reg <= max_reg; reg++)
8859 if (regs_ever_live[reg]
8860 || (! current_function_is_leaf && call_used_regs [reg]))
6d3d9133
NC
8861 save_reg_mask |= (1 << reg);
8862 }
8863 else
8864 {
8865 /* In the normal case we only need to save those registers
8866 which are call saved and which are used by this function. */
8867 for (reg = 0; reg <= 10; reg++)
8868 if (regs_ever_live[reg] && ! call_used_regs [reg])
8869 save_reg_mask |= (1 << reg);
8870
8871 /* Handle the frame pointer as a special case. */
8872 if (! TARGET_APCS_FRAME
8873 && ! frame_pointer_needed
8874 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8875 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8876 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8877
8878 /* If we aren't loading the PIC register,
8879 don't stack it even though it may be live. */
8880 if (flag_pic
8881 && ! TARGET_SINGLE_PIC_BASE
8882 && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
8883 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8884 }
8885
121308d4
NC
8886 return save_reg_mask;
8887}
8888
8889/* Compute a bit mask of which registers need to be
8890 saved on the stack for the current function. */
8891
8892static unsigned long
e32bac5b 8893arm_compute_save_reg_mask (void)
121308d4
NC
8894{
8895 unsigned int save_reg_mask = 0;
8896 unsigned long func_type = arm_current_func_type ();
8897
8898 if (IS_NAKED (func_type))
8899 /* This should never really happen. */
8900 return 0;
8901
8902 /* If we are creating a stack frame, then we must save the frame pointer,
8903 IP (which will hold the old stack pointer), LR and the PC. */
8904 if (frame_pointer_needed)
8905 save_reg_mask |=
8906 (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8907 | (1 << IP_REGNUM)
8908 | (1 << LR_REGNUM)
8909 | (1 << PC_REGNUM);
8910
8911 /* Volatile functions do not return, so there
8912 is no need to save any other registers. */
8913 if (IS_VOLATILE (func_type))
8914 return save_reg_mask;
8915
8916 save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8917
6d3d9133
NC
8918 /* Decide if we need to save the link register.
8919 Interrupt routines have their own banked link register,
8920 so they never need to save it.
1768c26f 8921 Otherwise if we do not use the link register we do not need to save
6d3d9133
NC
8922 it. If we are pushing other registers onto the stack however, we
8923 can save an instruction in the epilogue by pushing the link register
8924 now and then popping it back into the PC. This incurs extra memory
72ac76be 8925 accesses though, so we only do it when optimizing for size, and only
6d3d9133 8926 if we know that we will not need a fancy return sequence. */
3a7731fd 8927 if (regs_ever_live [LR_REGNUM]
6d3d9133
NC
8928 || (save_reg_mask
8929 && optimize_size
3a7731fd 8930 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL))
6d3d9133
NC
8931 save_reg_mask |= 1 << LR_REGNUM;
8932
6f7ebcbb
NC
8933 if (cfun->machine->lr_save_eliminated)
8934 save_reg_mask &= ~ (1 << LR_REGNUM);
8935
5a9335ef
NC
8936 if (TARGET_REALLY_IWMMXT
8937 && ((bit_count (save_reg_mask)
8938 + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
8939 {
8940 unsigned int reg;
8941
8942 /* The total number of registers that are going to be pushed
8943 onto the stack is odd. We need to ensure that the stack
8944 is 64-bit aligned before we start to save iWMMXt registers,
8945 and also before we start to create locals. (A local variable
8946 might be a double or long long which we will load/store using
8947 an iWMMXt instruction). Therefore we need to push another
8948 ARM register, so that the stack will be 64-bit aligned. We
8949 try to avoid using the arg registers (r0 -r3) as they might be
8950 used to pass values in a tail call. */
8951 for (reg = 4; reg <= 12; reg++)
8952 if ((save_reg_mask & (1 << reg)) == 0)
8953 break;
8954
8955 if (reg <= 12)
8956 save_reg_mask |= (1 << reg);
8957 else
8958 {
8959 cfun->machine->sibcall_blocked = 1;
8960 save_reg_mask |= (1 << 3);
8961 }
8962 }
8963
6d3d9133
NC
8964 return save_reg_mask;
8965}
8966
9728c9d1
PB
8967
8968/* Return the number of bytes required to save VFP registers. */
8969static int
8970arm_get_vfp_saved_size (void)
8971{
8972 unsigned int regno;
8973 int count;
8974 int saved;
8975
8976 saved = 0;
8977 /* Space for saved VFP registers. */
8978 if (TARGET_HARD_FLOAT && TARGET_VFP)
8979 {
8980 count = 0;
8981 for (regno = FIRST_VFP_REGNUM;
8982 regno < LAST_VFP_REGNUM;
8983 regno += 2)
8984 {
8985 if ((!regs_ever_live[regno] || call_used_regs[regno])
8986 && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
8987 {
8988 if (count > 0)
8989 {
8990 /* Workaround ARM10 VFPr1 bug. */
8991 if (count == 2 && !arm_arch6)
8992 count++;
8993 saved += count * 8 + 4;
8994 }
8995 count = 0;
8996 }
8997 else
8998 count++;
8999 }
9000 if (count > 0)
9001 {
9002 if (count == 2 && !arm_arch6)
9003 count++;
9004 saved += count * 8 + 4;
9005 }
9006 }
9007 return saved;
9008}
9009
9010
699a4925 9011/* Generate a function exit sequence. If REALLY_RETURN is false, then do
6d3d9133 9012 everything bar the final return instruction. */
cd2b33d0 9013const char *
e32bac5b 9014output_return_instruction (rtx operand, int really_return, int reverse)
ff9940b0 9015{
6d3d9133 9016 char conditional[10];
ff9940b0 9017 char instr[100];
6d3d9133
NC
9018 int reg;
9019 unsigned long live_regs_mask;
9020 unsigned long func_type;
5848830f 9021 arm_stack_offsets *offsets;
e26053d1 9022
6d3d9133 9023 func_type = arm_current_func_type ();
e2c671ba 9024
6d3d9133 9025 if (IS_NAKED (func_type))
d5b7b3ae 9026 return "";
6d3d9133
NC
9027
9028 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
e2c671ba 9029 {
699a4925
RE
9030 /* If this function was declared non-returning, and we have
9031 found a tail call, then we have to trust that the called
9032 function won't return. */
3a5a4282
PB
9033 if (really_return)
9034 {
9035 rtx ops[2];
9036
9037 /* Otherwise, trap an attempted return by aborting. */
9038 ops[0] = operand;
9039 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
9040 : "abort");
9041 assemble_external_libcall (ops[1]);
9042 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9043 }
9044
e2c671ba
RE
9045 return "";
9046 }
6d3d9133 9047
5895f793 9048 if (current_function_calls_alloca && !really_return)
62b10bbc 9049 abort ();
ff9940b0 9050
c414f8a9 9051 sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
d5b7b3ae 9052
6d3d9133 9053 return_used_this_function = 1;
ff9940b0 9054
6d3d9133 9055 live_regs_mask = arm_compute_save_reg_mask ();
ff9940b0 9056
1768c26f 9057 if (live_regs_mask)
6d3d9133 9058 {
1768c26f
PB
9059 const char * return_reg;
9060
9061 /* If we do not have any special requirements for function exit
9062 (eg interworking, or ISR) then we can load the return address
9063 directly into the PC. Otherwise we must load it into LR. */
9064 if (really_return
1768c26f
PB
9065 && ! TARGET_INTERWORK)
9066 return_reg = reg_names[PC_REGNUM];
6d3d9133 9067 else
1768c26f
PB
9068 return_reg = reg_names[LR_REGNUM];
9069
6d3d9133 9070 if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
b034930f
ILT
9071 {
9072 /* There are three possible reasons for the IP register
9073 being saved. 1) a stack frame was created, in which case
9074 IP contains the old stack pointer, or 2) an ISR routine
9075 corrupted it, or 3) it was saved to align the stack on
9076 iWMMXt. In case 1, restore IP into SP, otherwise just
9077 restore IP. */
9078 if (frame_pointer_needed)
9079 {
9080 live_regs_mask &= ~ (1 << IP_REGNUM);
9081 live_regs_mask |= (1 << SP_REGNUM);
9082 }
9083 else
9084 {
9085 if (! IS_INTERRUPT (func_type)
9086 && ! TARGET_REALLY_IWMMXT)
9087 abort ();
9088 }
9089 }
f3bb6135 9090
3a7731fd
PB
9091 /* On some ARM architectures it is faster to use LDR rather than
9092 LDM to load a single register. On other architectures, the
9093 cost is the same. In 26 bit mode, or for exception handlers,
9094 we have to use LDM to load the PC so that the CPSR is also
9095 restored. */
9096 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
6d3d9133 9097 {
3a7731fd
PB
9098 if (live_regs_mask == (unsigned int)(1 << reg))
9099 break;
9100 }
9101 if (reg <= LAST_ARM_REGNUM
9102 && (reg != LR_REGNUM
9103 || ! really_return
9104 || (TARGET_APCS_32 && ! IS_INTERRUPT (func_type))))
9105 {
9106 sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
9107 (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
6d3d9133 9108 }
ff9940b0 9109 else
1d5473cb 9110 {
1768c26f
PB
9111 char *p;
9112 int first = 1;
6d3d9133 9113
699a4925
RE
9114 /* Generate the load multiple instruction to restore the
9115 registers. Note we can get here, even if
9116 frame_pointer_needed is true, but only if sp already
9117 points to the base of the saved core registers. */
9118 if (live_regs_mask & (1 << SP_REGNUM))
a72d4945 9119 {
5848830f
PB
9120 unsigned HOST_WIDE_INT stack_adjust;
9121
9122 offsets = arm_get_frame_offsets ();
9123 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
a72d4945
RE
9124 if (stack_adjust != 0 && stack_adjust != 4)
9125 abort ();
9126
9127 if (stack_adjust && arm_arch5)
9128 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9129 else
9130 {
9131 /* If we can't use ldmib (SA110 bug), then try to pop r3
9132 instead. */
9133 if (stack_adjust)
9134 live_regs_mask |= 1 << 3;
9135 sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9136 }
9137 }
da6558fd 9138 else
1768c26f
PB
9139 sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9140
9141 p = instr + strlen (instr);
6d3d9133 9142
1768c26f
PB
9143 for (reg = 0; reg <= SP_REGNUM; reg++)
9144 if (live_regs_mask & (1 << reg))
9145 {
9146 int l = strlen (reg_names[reg]);
9147
9148 if (first)
9149 first = 0;
9150 else
9151 {
9152 memcpy (p, ", ", 2);
9153 p += 2;
9154 }
9155
9156 memcpy (p, "%|", 2);
9157 memcpy (p + 2, reg_names[reg], l);
9158 p += l + 2;
9159 }
9160
9161 if (live_regs_mask & (1 << LR_REGNUM))
9162 {
b17fe233
NC
9163 sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9164 /* Decide if we need to add the ^ symbol to the end of the
9165 register list. This causes the saved condition codes
9166 register to be copied into the current condition codes
9167 register. We do the copy if we are conforming to the 32-bit
9168 ABI and this is an interrupt function, or if we are
9169 conforming to the 26-bit ABI. There is a special case for
9170 the 26-bit ABI however, which is if we are writing back the
9171 stack pointer but not loading the PC. In this case adding
9172 the ^ symbol would create a type 2 LDM instruction, where
9173 writeback is UNPREDICTABLE. We are safe in leaving the ^
9174 character off in this case however, since the actual return
9175 instruction will be a MOVS which will restore the CPSR. */
9176 if ((TARGET_APCS_32 && IS_INTERRUPT (func_type))
13eedc5a 9177 || (! TARGET_APCS_32 && really_return))
b17fe233 9178 strcat (p, "^");
1768c26f
PB
9179 }
9180 else
9181 strcpy (p, "}");
1d5473cb 9182 }
da6558fd 9183
1768c26f
PB
9184 output_asm_insn (instr, & operand);
9185
3a7731fd
PB
9186 /* See if we need to generate an extra instruction to
9187 perform the actual function return. */
9188 if (really_return
9189 && func_type != ARM_FT_INTERWORKED
9190 && (live_regs_mask & (1 << LR_REGNUM)) != 0)
da6558fd 9191 {
3a7731fd
PB
9192 /* The return has already been handled
9193 by loading the LR into the PC. */
9194 really_return = 0;
da6558fd 9195 }
ff9940b0 9196 }
e26053d1 9197
1768c26f 9198 if (really_return)
ff9940b0 9199 {
6d3d9133
NC
9200 switch ((int) ARM_FUNC_TYPE (func_type))
9201 {
9202 case ARM_FT_ISR:
9203 case ARM_FT_FIQ:
9204 sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9205 break;
9206
9207 case ARM_FT_INTERWORKED:
9208 sprintf (instr, "bx%s\t%%|lr", conditional);
9209 break;
9210
9211 case ARM_FT_EXCEPTION:
9212 sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9213 break;
9214
9215 default:
1768c26f
PB
9216 /* ARMv5 implementations always provide BX, so interworking
9217 is the default unless APCS-26 is in use. */
9218 if ((insn_flags & FL_ARCH5) != 0 && TARGET_APCS_32)
9219 sprintf (instr, "bx%s\t%%|lr", conditional);
9220 else
9221 sprintf (instr, "mov%s%s\t%%|pc, %%|lr",
9222 conditional, TARGET_APCS_32 ? "" : "s");
6d3d9133
NC
9223 break;
9224 }
1768c26f
PB
9225
9226 output_asm_insn (instr, & operand);
ff9940b0 9227 }
f3bb6135 9228
ff9940b0
RE
9229 return "";
9230}
9231
ef179a26
NC
9232/* Write the function name into the code section, directly preceding
9233 the function prologue.
9234
9235 Code will be output similar to this:
9236 t0
9237 .ascii "arm_poke_function_name", 0
9238 .align
9239 t1
9240 .word 0xff000000 + (t1 - t0)
9241 arm_poke_function_name
9242 mov ip, sp
9243 stmfd sp!, {fp, ip, lr, pc}
9244 sub fp, ip, #4
9245
9246 When performing a stack backtrace, code can inspect the value
9247 of 'pc' stored at 'fp' + 0. If the trace function then looks
9248 at location pc - 12 and the top 8 bits are set, then we know
9249 that there is a function name embedded immediately preceding this
9250 location and has length ((pc[-3]) & 0xff000000).
9251
9252 We assume that pc is declared as a pointer to an unsigned long.
9253
9254 It is of no benefit to output the function name if we are assembling
9255 a leaf function. These function types will not contain a stack
9256 backtrace structure, therefore it is not possible to determine the
9257 function name. */
ef179a26 9258void
e32bac5b 9259arm_poke_function_name (FILE *stream, const char *name)
ef179a26
NC
9260{
9261 unsigned long alignlength;
9262 unsigned long length;
9263 rtx x;
9264
d5b7b3ae 9265 length = strlen (name) + 1;
0c2ca901 9266 alignlength = ROUND_UP_WORD (length);
ef179a26 9267
949d79eb 9268 ASM_OUTPUT_ASCII (stream, name, length);
ef179a26 9269 ASM_OUTPUT_ALIGN (stream, 2);
30cf4896 9270 x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
301d03af 9271 assemble_aligned_integer (UNITS_PER_WORD, x);
ef179a26
NC
9272}
9273
6d3d9133
NC
9274/* Place some comments into the assembler stream
9275 describing the current function. */
08c148a8 9276static void
e32bac5b 9277arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
cce8749e 9278{
6d3d9133 9279 unsigned long func_type;
08c148a8
NB
9280
9281 if (!TARGET_ARM)
9282 {
9283 thumb_output_function_prologue (f, frame_size);
9284 return;
9285 }
6d3d9133
NC
9286
9287 /* Sanity check. */
abaa26e5 9288 if (arm_ccfsm_state || arm_target_insn)
6d3d9133 9289 abort ();
31fdb4d5 9290
6d3d9133
NC
9291 func_type = arm_current_func_type ();
9292
9293 switch ((int) ARM_FUNC_TYPE (func_type))
9294 {
9295 default:
9296 case ARM_FT_NORMAL:
9297 break;
9298 case ARM_FT_INTERWORKED:
9299 asm_fprintf (f, "\t%@ Function supports interworking.\n");
9300 break;
9301 case ARM_FT_EXCEPTION_HANDLER:
9302 asm_fprintf (f, "\t%@ C++ Exception Handler.\n");
9303 break;
9304 case ARM_FT_ISR:
9305 asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9306 break;
9307 case ARM_FT_FIQ:
9308 asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9309 break;
9310 case ARM_FT_EXCEPTION:
9311 asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9312 break;
9313 }
ff9940b0 9314
6d3d9133
NC
9315 if (IS_NAKED (func_type))
9316 asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9317
9318 if (IS_VOLATILE (func_type))
9319 asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9320
9321 if (IS_NESTED (func_type))
9322 asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9323
c53dddc2 9324 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
dd18ae56
NC
9325 current_function_args_size,
9326 current_function_pretend_args_size, frame_size);
6d3d9133 9327
3cb66fd7 9328 asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
dd18ae56 9329 frame_pointer_needed,
3cb66fd7 9330 cfun->machine->uses_anonymous_args);
cce8749e 9331
6f7ebcbb
NC
9332 if (cfun->machine->lr_save_eliminated)
9333 asm_fprintf (f, "\t%@ link register save eliminated.\n");
9334
32de079a
RE
9335#ifdef AOF_ASSEMBLER
9336 if (flag_pic)
dd18ae56 9337 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
32de079a 9338#endif
6d3d9133
NC
9339
9340 return_used_this_function = 0;
f3bb6135 9341}
cce8749e 9342
cd2b33d0 9343const char *
a72d4945 9344arm_output_epilogue (rtx sibling)
cce8749e 9345{
949d79eb 9346 int reg;
6f7ebcbb 9347 unsigned long saved_regs_mask;
6d3d9133 9348 unsigned long func_type;
c882c7ac
RE
9349 /* Floats_offset is the offset from the "virtual" frame. In an APCS
9350 frame that is $fp + 4 for a non-variadic function. */
9351 int floats_offset = 0;
cce8749e 9352 rtx operands[3];
d5b7b3ae 9353 FILE * f = asm_out_file;
6d3d9133 9354 rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
5a9335ef 9355 unsigned int lrm_count = 0;
a72d4945 9356 int really_return = (sibling == NULL);
9b66ebb1 9357 int start_reg;
5848830f 9358 arm_stack_offsets *offsets;
cce8749e 9359
6d3d9133
NC
9360 /* If we have already generated the return instruction
9361 then it is futile to generate anything else. */
a72d4945 9362 if (use_return_insn (FALSE, sibling) && return_used_this_function)
949d79eb 9363 return "";
cce8749e 9364
6d3d9133 9365 func_type = arm_current_func_type ();
d5b7b3ae 9366
6d3d9133
NC
9367 if (IS_NAKED (func_type))
9368 /* Naked functions don't have epilogues. */
9369 return "";
0616531f 9370
6d3d9133 9371 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
e2c671ba 9372 {
86efdc8e 9373 rtx op;
6d3d9133
NC
9374
9375 /* A volatile function should never return. Call abort. */
ed0e6530 9376 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
2b835d68 9377 assemble_external_libcall (op);
e2c671ba 9378 output_asm_insn ("bl\t%a0", &op);
6d3d9133 9379
949d79eb 9380 return "";
e2c671ba
RE
9381 }
9382
6d3d9133
NC
9383 if (ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
9384 && ! really_return)
9385 /* If we are throwing an exception, then we really must
9386 be doing a return, so we can't tail-call. */
9387 abort ();
9388
5848830f 9389 offsets = arm_get_frame_offsets ();
6f7ebcbb 9390 saved_regs_mask = arm_compute_save_reg_mask ();
5a9335ef
NC
9391
9392 if (TARGET_IWMMXT)
9393 lrm_count = bit_count (saved_regs_mask);
9394
5848830f 9395 floats_offset = offsets->saved_args;
6d3d9133 9396 /* Compute how far away the floats will be. */
5a9335ef 9397 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
6f7ebcbb 9398 if (saved_regs_mask & (1 << reg))
6ed30148 9399 floats_offset += 4;
6d3d9133 9400
ff9940b0 9401 if (frame_pointer_needed)
cce8749e 9402 {
9b66ebb1 9403 /* This variable is for the Virtual Frame Pointer, not VFP regs. */
5848830f 9404 int vfp_offset = offsets->frame;
c882c7ac 9405
29ad9694 9406 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
b111229a 9407 {
9b66ebb1 9408 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
5895f793 9409 if (regs_ever_live[reg] && !call_used_regs[reg])
b111229a
RE
9410 {
9411 floats_offset += 12;
dd18ae56 9412 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
c882c7ac 9413 reg, FP_REGNUM, floats_offset - vfp_offset);
b111229a
RE
9414 }
9415 }
9416 else
9417 {
9b66ebb1 9418 start_reg = LAST_FPA_REGNUM;
b111229a 9419
9b66ebb1 9420 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
b111229a 9421 {
5895f793 9422 if (regs_ever_live[reg] && !call_used_regs[reg])
b111229a
RE
9423 {
9424 floats_offset += 12;
6cfc7210 9425
6354dc9b 9426 /* We can't unstack more than four registers at once. */
b111229a
RE
9427 if (start_reg - reg == 3)
9428 {
dd18ae56 9429 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
c882c7ac 9430 reg, FP_REGNUM, floats_offset - vfp_offset);
b111229a
RE
9431 start_reg = reg - 1;
9432 }
9433 }
9434 else
9435 {
9436 if (reg != start_reg)
dd18ae56
NC
9437 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9438 reg + 1, start_reg - reg,
c882c7ac 9439 FP_REGNUM, floats_offset - vfp_offset);
b111229a
RE
9440 start_reg = reg - 1;
9441 }
9442 }
9443
9444 /* Just in case the last register checked also needs unstacking. */
9445 if (reg != start_reg)
dd18ae56
NC
9446 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9447 reg + 1, start_reg - reg,
c882c7ac 9448 FP_REGNUM, floats_offset - vfp_offset);
b111229a 9449 }
6d3d9133 9450
9b66ebb1
PB
9451 if (TARGET_HARD_FLOAT && TARGET_VFP)
9452 {
9728c9d1 9453 int saved_size;
9b66ebb1 9454
9728c9d1
PB
9455 /* The fldmx insn does not have base+offset addressing modes,
9456 so we use IP to hold the address. */
9457 saved_size = arm_get_vfp_saved_size ();
9b66ebb1 9458
9728c9d1 9459 if (saved_size > 0)
9b66ebb1 9460 {
9728c9d1 9461 floats_offset += saved_size;
9b66ebb1
PB
9462 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9463 FP_REGNUM, floats_offset - vfp_offset);
9464 }
9465 start_reg = FIRST_VFP_REGNUM;
9466 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9467 {
9468 if ((!regs_ever_live[reg] || call_used_regs[reg])
9469 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9470 {
9471 if (start_reg != reg)
9728c9d1
PB
9472 arm_output_fldmx (f, IP_REGNUM,
9473 (start_reg - FIRST_VFP_REGNUM) / 2,
9474 (reg - start_reg) / 2);
9b66ebb1
PB
9475 start_reg = reg + 2;
9476 }
9477 }
9478 if (start_reg != reg)
9728c9d1
PB
9479 arm_output_fldmx (f, IP_REGNUM,
9480 (start_reg - FIRST_VFP_REGNUM) / 2,
9481 (reg - start_reg) / 2);
9b66ebb1
PB
9482 }
9483
5a9335ef
NC
9484 if (TARGET_IWMMXT)
9485 {
9486 /* The frame pointer is guaranteed to be non-double-word aligned.
9487 This is because it is set to (old_stack_pointer - 4) and the
9488 old_stack_pointer was double word aligned. Thus the offset to
9489 the iWMMXt registers to be loaded must also be non-double-word
9490 sized, so that the resultant address *is* double-word aligned.
9491 We can ignore floats_offset since that was already included in
9492 the live_regs_mask. */
9493 lrm_count += (lrm_count % 2 ? 2 : 1);
9494
9495 for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9496 if (regs_ever_live[reg] && !call_used_regs[reg])
9497 {
9498 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9499 reg, FP_REGNUM, lrm_count * 4);
9500 lrm_count += 2;
9501 }
9502 }
9503
6f7ebcbb 9504 /* saved_regs_mask should contain the IP, which at the time of stack
6d3d9133
NC
9505 frame generation actually contains the old stack pointer. So a
9506 quick way to unwind the stack is just pop the IP register directly
9507 into the stack pointer. */
6f7ebcbb 9508 if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
6d3d9133 9509 abort ();
6f7ebcbb
NC
9510 saved_regs_mask &= ~ (1 << IP_REGNUM);
9511 saved_regs_mask |= (1 << SP_REGNUM);
6d3d9133 9512
6f7ebcbb 9513 /* There are two registers left in saved_regs_mask - LR and PC. We
6d3d9133
NC
9514 only need to restore the LR register (the return address), but to
9515 save time we can load it directly into the PC, unless we need a
9516 special function exit sequence, or we are not really returning. */
9517 if (really_return && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
9518 /* Delete the LR from the register mask, so that the LR on
9519 the stack is loaded into the PC in the register mask. */
6f7ebcbb 9520 saved_regs_mask &= ~ (1 << LR_REGNUM);
b111229a 9521 else
6f7ebcbb 9522 saved_regs_mask &= ~ (1 << PC_REGNUM);
efc2515b
RE
9523
9524 /* We must use SP as the base register, because SP is one of the
9525 registers being restored. If an interrupt or page fault
9526 happens in the ldm instruction, the SP might or might not
9527 have been restored. That would be bad, as then SP will no
9528 longer indicate the safe area of stack, and we can get stack
9529 corruption. Using SP as the base register means that it will
9530 be reset correctly to the original value, should an interrupt
699a4925
RE
9531 occur. If the stack pointer already points at the right
9532 place, then omit the subtraction. */
5848830f 9533 if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
699a4925
RE
9534 || current_function_calls_alloca)
9535 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9536 4 * bit_count (saved_regs_mask));
efc2515b 9537 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
7b8b8ade
NC
9538
9539 if (IS_INTERRUPT (func_type))
9540 /* Interrupt handlers will have pushed the
9541 IP onto the stack, so restore it now. */
f55d7103 9542 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
cce8749e
CH
9543 }
9544 else
9545 {
d2288d8d 9546 /* Restore stack pointer if necessary. */
5848830f 9547 if (offsets->outgoing_args != offsets->saved_regs)
d2288d8d
TG
9548 {
9549 operands[0] = operands[1] = stack_pointer_rtx;
5848830f 9550 operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
d2288d8d
TG
9551 output_add_immediate (operands);
9552 }
9553
29ad9694 9554 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
b111229a 9555 {
9b66ebb1 9556 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
5895f793 9557 if (regs_ever_live[reg] && !call_used_regs[reg])
dd18ae56
NC
9558 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9559 reg, SP_REGNUM);
b111229a
RE
9560 }
9561 else
9562 {
9b66ebb1 9563 start_reg = FIRST_FPA_REGNUM;
b111229a 9564
9b66ebb1 9565 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
b111229a 9566 {
5895f793 9567 if (regs_ever_live[reg] && !call_used_regs[reg])
b111229a
RE
9568 {
9569 if (reg - start_reg == 3)
9570 {
dd18ae56
NC
9571 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9572 start_reg, SP_REGNUM);
b111229a
RE
9573 start_reg = reg + 1;
9574 }
9575 }
9576 else
9577 {
9578 if (reg != start_reg)
dd18ae56
NC
9579 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9580 start_reg, reg - start_reg,
9581 SP_REGNUM);
6cfc7210 9582
b111229a
RE
9583 start_reg = reg + 1;
9584 }
9585 }
9586
9587 /* Just in case the last register checked also needs unstacking. */
9588 if (reg != start_reg)
dd18ae56
NC
9589 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9590 start_reg, reg - start_reg, SP_REGNUM);
b111229a
RE
9591 }
9592
9b66ebb1
PB
9593 if (TARGET_HARD_FLOAT && TARGET_VFP)
9594 {
9595 start_reg = FIRST_VFP_REGNUM;
9596 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9597 {
9598 if ((!regs_ever_live[reg] || call_used_regs[reg])
9599 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9600 {
9601 if (start_reg != reg)
9728c9d1
PB
9602 arm_output_fldmx (f, SP_REGNUM,
9603 (start_reg - FIRST_VFP_REGNUM) / 2,
9604 (reg - start_reg) / 2);
9b66ebb1
PB
9605 start_reg = reg + 2;
9606 }
9607 }
9608 if (start_reg != reg)
9728c9d1
PB
9609 arm_output_fldmx (f, SP_REGNUM,
9610 (start_reg - FIRST_VFP_REGNUM) / 2,
9611 (reg - start_reg) / 2);
9b66ebb1 9612 }
5a9335ef
NC
9613 if (TARGET_IWMMXT)
9614 for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9615 if (regs_ever_live[reg] && !call_used_regs[reg])
9616 asm_fprintf (f, "\twldrd\t%r, [%r, #+8]!\n", reg, SP_REGNUM);
9617
6d3d9133
NC
9618 /* If we can, restore the LR into the PC. */
9619 if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9620 && really_return
9621 && current_function_pretend_args_size == 0
6f7ebcbb 9622 && saved_regs_mask & (1 << LR_REGNUM))
cce8749e 9623 {
6f7ebcbb
NC
9624 saved_regs_mask &= ~ (1 << LR_REGNUM);
9625 saved_regs_mask |= (1 << PC_REGNUM);
6d3d9133 9626 }
d5b7b3ae 9627
6d3d9133
NC
9628 /* Load the registers off the stack. If we only have one register
9629 to load use the LDR instruction - it is faster. */
6f7ebcbb 9630 if (saved_regs_mask == (1 << LR_REGNUM))
6d3d9133 9631 {
f4864588 9632 /* The exception handler ignores the LR, so we do
6d3d9133
NC
9633 not really need to load it off the stack. */
9634 if (eh_ofs)
9635 asm_fprintf (f, "\tadd\t%r, %r, #4\n", SP_REGNUM, SP_REGNUM);
32de079a 9636 else
6d3d9133 9637 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
cce8749e 9638 }
6f7ebcbb 9639 else if (saved_regs_mask)
f1acdf8b
NC
9640 {
9641 if (saved_regs_mask & (1 << SP_REGNUM))
9642 /* Note - write back to the stack register is not enabled
9643 (ie "ldmfd sp!..."). We know that the stack pointer is
9644 in the list of registers and if we add writeback the
9645 instruction becomes UNPREDICTABLE. */
9646 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9647 else
9648 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9649 }
6d3d9133
NC
9650
9651 if (current_function_pretend_args_size)
cce8749e 9652 {
6d3d9133
NC
9653 /* Unwind the pre-pushed regs. */
9654 operands[0] = operands[1] = stack_pointer_rtx;
9655 operands[2] = GEN_INT (current_function_pretend_args_size);
9656 output_add_immediate (operands);
9657 }
9658 }
32de079a 9659
f4864588
PB
9660 if (! really_return
9661 || (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9662 && current_function_pretend_args_size == 0
9663 && saved_regs_mask & (1 << PC_REGNUM)))
6d3d9133 9664 return "";
d5b7b3ae 9665
6d3d9133
NC
9666 /* Generate the return instruction. */
9667 switch ((int) ARM_FUNC_TYPE (func_type))
9668 {
9669 case ARM_FT_EXCEPTION_HANDLER:
9670 /* Even in 26-bit mode we do a mov (rather than a movs)
9671 because we don't have the PSR bits set in the address. */
9672 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, EXCEPTION_LR_REGNUM);
9673 break;
0616531f 9674
6d3d9133
NC
9675 case ARM_FT_ISR:
9676 case ARM_FT_FIQ:
9677 asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9678 break;
9679
9680 case ARM_FT_EXCEPTION:
9681 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9682 break;
9683
9684 case ARM_FT_INTERWORKED:
9685 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9686 break;
9687
9688 default:
9689 if (frame_pointer_needed)
6bc82793 9690 /* If we used the frame pointer then the return address
6d3d9133
NC
9691 will have been loaded off the stack directly into the
9692 PC, so there is no need to issue a MOV instruction
9693 here. */
9694 ;
9695 else if (current_function_pretend_args_size == 0
6f7ebcbb 9696 && (saved_regs_mask & (1 << LR_REGNUM)))
6d3d9133
NC
9697 /* Similarly we may have been able to load LR into the PC
9698 even if we did not create a stack frame. */
9699 ;
9700 else if (TARGET_APCS_32)
9701 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9702 else
9703 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9704 break;
cce8749e 9705 }
f3bb6135 9706
949d79eb
RE
9707 return "";
9708}
9709
08c148a8 9710static void
e32bac5b 9711arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
5848830f 9712 HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
949d79eb 9713{
5848830f
PB
9714 arm_stack_offsets *offsets;
9715
d5b7b3ae
RE
9716 if (TARGET_THUMB)
9717 {
9718 /* ??? Probably not safe to set this here, since it assumes that a
9719 function will be emitted as assembly immediately after we generate
9720 RTL for it. This does not happen for inline functions. */
9721 return_used_this_function = 0;
9722 }
9723 else
9724 {
0977774b 9725 /* We need to take into account any stack-frame rounding. */
5848830f 9726 offsets = arm_get_frame_offsets ();
0977774b 9727
a72d4945 9728 if (use_return_insn (FALSE, NULL)
d5b7b3ae 9729 && return_used_this_function
5848830f 9730 && offsets->saved_regs != offsets->outgoing_args
5895f793 9731 && !frame_pointer_needed)
d5b7b3ae 9732 abort ();
f3bb6135 9733
d5b7b3ae 9734 /* Reset the ARM-specific per-function variables. */
d5b7b3ae
RE
9735 after_arm_reorg = 0;
9736 }
f3bb6135 9737}
e2c671ba 9738
2c849145
JM
9739/* Generate and emit an insn that we will recognize as a push_multi.
9740 Unfortunately, since this insn does not reflect very well the actual
9741 semantics of the operation, we need to annotate the insn for the benefit
9742 of DWARF2 frame unwind information. */
2c849145 9743static rtx
e32bac5b 9744emit_multi_reg_push (int mask)
e2c671ba
RE
9745{
9746 int num_regs = 0;
9b598fa0 9747 int num_dwarf_regs;
e2c671ba
RE
9748 int i, j;
9749 rtx par;
2c849145 9750 rtx dwarf;
87e27392 9751 int dwarf_par_index;
2c849145 9752 rtx tmp, reg;
e2c671ba 9753
d5b7b3ae 9754 for (i = 0; i <= LAST_ARM_REGNUM; i++)
e2c671ba 9755 if (mask & (1 << i))
5895f793 9756 num_regs++;
e2c671ba
RE
9757
9758 if (num_regs == 0 || num_regs > 16)
9759 abort ();
9760
9b598fa0
RE
9761 /* We don't record the PC in the dwarf frame information. */
9762 num_dwarf_regs = num_regs;
9763 if (mask & (1 << PC_REGNUM))
9764 num_dwarf_regs--;
9765
87e27392 9766 /* For the body of the insn we are going to generate an UNSPEC in
05713b80 9767 parallel with several USEs. This allows the insn to be recognized
87e27392
NC
9768 by the push_multi pattern in the arm.md file. The insn looks
9769 something like this:
9770
9771 (parallel [
b15bca31
RE
9772 (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9773 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
87e27392
NC
9774 (use (reg:SI 11 fp))
9775 (use (reg:SI 12 ip))
9776 (use (reg:SI 14 lr))
9777 (use (reg:SI 15 pc))
9778 ])
9779
9780 For the frame note however, we try to be more explicit and actually
9781 show each register being stored into the stack frame, plus a (single)
9782 decrement of the stack pointer. We do it this way in order to be
9783 friendly to the stack unwinding code, which only wants to see a single
9784 stack decrement per instruction. The RTL we generate for the note looks
9785 something like this:
9786
9787 (sequence [
9788 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9789 (set (mem:SI (reg:SI sp)) (reg:SI r4))
9790 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9791 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9792 (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
87e27392
NC
9793 ])
9794
9795 This sequence is used both by the code to support stack unwinding for
9796 exceptions handlers and the code to generate dwarf2 frame debugging. */
9797
43cffd11 9798 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9b598fa0 9799 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
87e27392 9800 dwarf_par_index = 1;
e2c671ba 9801
d5b7b3ae 9802 for (i = 0; i <= LAST_ARM_REGNUM; i++)
e2c671ba
RE
9803 {
9804 if (mask & (1 << i))
9805 {
2c849145
JM
9806 reg = gen_rtx_REG (SImode, i);
9807
e2c671ba 9808 XVECEXP (par, 0, 0)
43cffd11
RE
9809 = gen_rtx_SET (VOIDmode,
9810 gen_rtx_MEM (BLKmode,
9811 gen_rtx_PRE_DEC (BLKmode,
9812 stack_pointer_rtx)),
9813 gen_rtx_UNSPEC (BLKmode,
2c849145 9814 gen_rtvec (1, reg),
9b598fa0 9815 UNSPEC_PUSH_MULT));
2c849145 9816
9b598fa0
RE
9817 if (i != PC_REGNUM)
9818 {
9819 tmp = gen_rtx_SET (VOIDmode,
9820 gen_rtx_MEM (SImode, stack_pointer_rtx),
9821 reg);
9822 RTX_FRAME_RELATED_P (tmp) = 1;
9823 XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9824 dwarf_par_index++;
9825 }
2c849145 9826
e2c671ba
RE
9827 break;
9828 }
9829 }
9830
9831 for (j = 1, i++; j < num_regs; i++)
9832 {
9833 if (mask & (1 << i))
9834 {
2c849145
JM
9835 reg = gen_rtx_REG (SImode, i);
9836
9837 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9838
9b598fa0
RE
9839 if (i != PC_REGNUM)
9840 {
9841 tmp = gen_rtx_SET (VOIDmode,
9842 gen_rtx_MEM (SImode,
9843 plus_constant (stack_pointer_rtx,
9844 4 * j)),
9845 reg);
9846 RTX_FRAME_RELATED_P (tmp) = 1;
9847 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9848 }
9849
e2c671ba
RE
9850 j++;
9851 }
9852 }
b111229a 9853
2c849145 9854 par = emit_insn (par);
87e27392
NC
9855
9856 tmp = gen_rtx_SET (SImode,
9857 stack_pointer_rtx,
9858 gen_rtx_PLUS (SImode,
9859 stack_pointer_rtx,
9860 GEN_INT (-4 * num_regs)));
9861 RTX_FRAME_RELATED_P (tmp) = 1;
9862 XVECEXP (dwarf, 0, 0) = tmp;
9863
2c849145
JM
9864 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9865 REG_NOTES (par));
9866 return par;
b111229a
RE
9867}
9868
2c849145 9869static rtx
e32bac5b 9870emit_sfm (int base_reg, int count)
b111229a
RE
9871{
9872 rtx par;
2c849145
JM
9873 rtx dwarf;
9874 rtx tmp, reg;
b111229a
RE
9875 int i;
9876
43cffd11 9877 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
2c849145 9878 dwarf = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
2c849145
JM
9879
9880 reg = gen_rtx_REG (XFmode, base_reg++);
43cffd11
RE
9881
9882 XVECEXP (par, 0, 0)
9883 = gen_rtx_SET (VOIDmode,
9884 gen_rtx_MEM (BLKmode,
9885 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9886 gen_rtx_UNSPEC (BLKmode,
2c849145 9887 gen_rtvec (1, reg),
b15bca31 9888 UNSPEC_PUSH_MULT));
2c849145
JM
9889 tmp
9890 = gen_rtx_SET (VOIDmode,
9891 gen_rtx_MEM (XFmode,
9892 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9893 reg);
9894 RTX_FRAME_RELATED_P (tmp) = 1;
9895 XVECEXP (dwarf, 0, count - 1) = tmp;
9896
b111229a 9897 for (i = 1; i < count; i++)
2c849145
JM
9898 {
9899 reg = gen_rtx_REG (XFmode, base_reg++);
9900 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9901
9902 tmp = gen_rtx_SET (VOIDmode,
9903 gen_rtx_MEM (XFmode,
9904 gen_rtx_PRE_DEC (BLKmode,
9905 stack_pointer_rtx)),
9906 reg);
9907 RTX_FRAME_RELATED_P (tmp) = 1;
9908 XVECEXP (dwarf, 0, count - i - 1) = tmp;
9909 }
b111229a 9910
2c849145
JM
9911 par = emit_insn (par);
9912 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9913 REG_NOTES (par));
9914 return par;
e2c671ba
RE
9915}
9916
9b66ebb1 9917
095bb276
NC
9918/* Compute the distance from register FROM to register TO.
9919 These can be the arg pointer (26), the soft frame pointer (25),
9920 the stack pointer (13) or the hard frame pointer (11).
9921 Typical stack layout looks like this:
9922
9923 old stack pointer -> | |
9924 ----
9925 | | \
9926 | | saved arguments for
9927 | | vararg functions
9928 | | /
9929 --
9930 hard FP & arg pointer -> | | \
9931 | | stack
9932 | | frame
9933 | | /
9934 --
9935 | | \
9936 | | call saved
9937 | | registers
9938 soft frame pointer -> | | /
9939 --
9940 | | \
9941 | | local
9942 | | variables
9943 | | /
9944 --
9945 | | \
9946 | | outgoing
9947 | | arguments
9948 current stack pointer -> | | /
9949 --
9950
43aa4e05 9951 For a given function some or all of these stack components
095bb276
NC
9952 may not be needed, giving rise to the possibility of
9953 eliminating some of the registers.
9954
825dda42 9955 The values returned by this function must reflect the behavior
095bb276
NC
9956 of arm_expand_prologue() and arm_compute_save_reg_mask().
9957
9958 The sign of the number returned reflects the direction of stack
9959 growth, so the values are positive for all eliminations except
5848830f
PB
9960 from the soft frame pointer to the hard frame pointer.
9961
9962 SFP may point just inside the local variables block to ensure correct
9963 alignment. */
9964
9965
9966/* Calculate stack offsets. These are used to calculate register elimination
9967 offsets and in prologue/epilogue code. */
9968
9969static arm_stack_offsets *
9970arm_get_frame_offsets (void)
095bb276 9971{
5848830f 9972 struct arm_stack_offsets *offsets;
095bb276 9973 unsigned long func_type;
5848830f 9974 int leaf;
5848830f
PB
9975 int saved;
9976 HOST_WIDE_INT frame_size;
9977
9978 offsets = &cfun->machine->stack_offsets;
095bb276 9979
5848830f
PB
9980 /* We need to know if we are a leaf function. Unfortunately, it
9981 is possible to be called after start_sequence has been called,
9982 which causes get_insns to return the insns for the sequence,
9983 not the function, which will cause leaf_function_p to return
9984 the incorrect result.
095bb276 9985
5848830f
PB
9986 to know about leaf functions once reload has completed, and the
9987 frame size cannot be changed after that time, so we can safely
9988 use the cached value. */
9989
9990 if (reload_completed)
9991 return offsets;
9992
666c27b9
KH
9993 /* Initially this is the size of the local variables. It will translated
9994 into an offset once we have determined the size of preceding data. */
5848830f
PB
9995 frame_size = ROUND_UP_WORD (get_frame_size ());
9996
9997 leaf = leaf_function_p ();
9998
9999 /* Space for variadic functions. */
10000 offsets->saved_args = current_function_pretend_args_size;
10001
10002 offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
10003
10004 if (TARGET_ARM)
095bb276 10005 {
5848830f 10006 unsigned int regno;
ef7112de 10007
5848830f 10008 saved = bit_count (arm_compute_save_reg_mask ()) * 4;
5a9335ef 10009
5848830f
PB
10010 /* We know that SP will be doubleword aligned on entry, and we must
10011 preserve that condition at any subroutine call. We also require the
10012 soft frame pointer to be doubleword aligned. */
10013
10014 if (TARGET_REALLY_IWMMXT)
9b66ebb1 10015 {
5848830f
PB
10016 /* Check for the call-saved iWMMXt registers. */
10017 for (regno = FIRST_IWMMXT_REGNUM;
10018 regno <= LAST_IWMMXT_REGNUM;
10019 regno++)
10020 if (regs_ever_live [regno] && ! call_used_regs [regno])
10021 saved += 8;
10022 }
10023
10024 func_type = arm_current_func_type ();
10025 if (! IS_VOLATILE (func_type))
10026 {
10027 /* Space for saved FPA registers. */
10028 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
10029 if (regs_ever_live[regno] && ! call_used_regs[regno])
10030 saved += 12;
10031
10032 /* Space for saved VFP registers. */
10033 if (TARGET_HARD_FLOAT && TARGET_VFP)
9728c9d1 10034 saved += arm_get_vfp_saved_size ();
9b66ebb1 10035 }
5848830f
PB
10036 }
10037 else /* TARGET_THUMB */
10038 {
10039 int reg;
10040 int count_regs;
10041
10042 saved = 0;
10043 count_regs = 0;
10044 for (reg = 8; reg < 13; reg ++)
10045 if (THUMB_REG_PUSHED_P (reg))
10046 count_regs ++;
10047 if (count_regs)
10048 saved += 4 * count_regs;
10049 count_regs = 0;
10050 for (reg = 0; reg <= LAST_LO_REGNUM; reg ++)
10051 if (THUMB_REG_PUSHED_P (reg))
10052 count_regs ++;
10053 if (count_regs || ! leaf_function_p ()
10054 || thumb_far_jump_used_p ())
10055 saved += 4 * (count_regs + 1);
10056 if (TARGET_BACKTRACE)
10057 {
10058 if ((count_regs & 0xFF) == 0 && (regs_ever_live[3] != 0))
10059 saved += 20;
10060 else
10061 saved += 16;
10062 }
10063 }
9b66ebb1 10064
5848830f
PB
10065 /* Saved registers include the stack frame. */
10066 offsets->saved_regs = offsets->saved_args + saved;
10067 offsets->soft_frame = offsets->saved_regs;
10068 /* A leaf function does not need any stack alignment if it has nothing
10069 on the stack. */
10070 if (leaf && frame_size == 0)
10071 {
10072 offsets->outgoing_args = offsets->soft_frame;
10073 return offsets;
10074 }
10075
10076 /* Ensure SFP has the correct alignment. */
10077 if (ARM_DOUBLEWORD_ALIGN
10078 && (offsets->soft_frame & 7))
10079 offsets->soft_frame += 4;
10080
10081 offsets->outgoing_args = offsets->soft_frame + frame_size
10082 + current_function_outgoing_args_size;
10083
10084 if (ARM_DOUBLEWORD_ALIGN)
10085 {
10086 /* Ensure SP remains doubleword aligned. */
10087 if (offsets->outgoing_args & 7)
10088 offsets->outgoing_args += 4;
10089 if (offsets->outgoing_args & 7)
10090 abort ();
095bb276
NC
10091 }
10092
5848830f
PB
10093 return offsets;
10094}
10095
10096
666c27b9 10097/* Calculate the relative offsets for the different stack pointers. Positive
5848830f
PB
10098 offsets are in the direction of stack growth. */
10099
10100unsigned int
10101arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10102{
10103 arm_stack_offsets *offsets;
10104
10105 offsets = arm_get_frame_offsets ();
095bb276 10106
095bb276
NC
10107 /* OK, now we have enough information to compute the distances.
10108 There must be an entry in these switch tables for each pair
10109 of registers in ELIMINABLE_REGS, even if some of the entries
10110 seem to be redundant or useless. */
10111 switch (from)
10112 {
10113 case ARG_POINTER_REGNUM:
10114 switch (to)
10115 {
10116 case THUMB_HARD_FRAME_POINTER_REGNUM:
10117 return 0;
10118
10119 case FRAME_POINTER_REGNUM:
10120 /* This is the reverse of the soft frame pointer
10121 to hard frame pointer elimination below. */
5848830f 10122 return offsets->soft_frame - offsets->saved_args;
095bb276
NC
10123
10124 case ARM_HARD_FRAME_POINTER_REGNUM:
10125 /* If there is no stack frame then the hard
10126 frame pointer and the arg pointer coincide. */
5848830f 10127 if (offsets->frame == offsets->saved_regs)
095bb276 10128 return 0;
5848830f
PB
10129 /* FIXME: Not sure about this. Maybe we should always return 0 ? */
10130 return (frame_pointer_needed
10131 && current_function_needs_context
10132 && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
095bb276
NC
10133
10134 case STACK_POINTER_REGNUM:
10135 /* If nothing has been pushed on the stack at all
10136 then this will return -4. This *is* correct! */
5848830f 10137 return offsets->outgoing_args - (offsets->saved_args + 4);
095bb276
NC
10138
10139 default:
10140 abort ();
10141 }
10142 break;
10143
10144 case FRAME_POINTER_REGNUM:
10145 switch (to)
10146 {
10147 case THUMB_HARD_FRAME_POINTER_REGNUM:
10148 return 0;
10149
10150 case ARM_HARD_FRAME_POINTER_REGNUM:
10151 /* The hard frame pointer points to the top entry in the
10152 stack frame. The soft frame pointer to the bottom entry
10153 in the stack frame. If there is no stack frame at all,
10154 then they are identical. */
5848830f
PB
10155
10156 return offsets->frame - offsets->soft_frame;
095bb276
NC
10157
10158 case STACK_POINTER_REGNUM:
5848830f 10159 return offsets->outgoing_args - offsets->soft_frame;
095bb276
NC
10160
10161 default:
10162 abort ();
10163 }
10164 break;
10165
10166 default:
10167 /* You cannot eliminate from the stack pointer.
10168 In theory you could eliminate from the hard frame
10169 pointer to the stack pointer, but this will never
10170 happen, since if a stack frame is not needed the
10171 hard frame pointer will never be used. */
10172 abort ();
10173 }
10174}
10175
0977774b 10176
6d3d9133 10177/* Generate the prologue instructions for entry into an ARM function. */
e2c671ba 10178void
e32bac5b 10179arm_expand_prologue (void)
e2c671ba
RE
10180{
10181 int reg;
6d3d9133 10182 rtx amount;
2c849145 10183 rtx insn;
68dfd979 10184 rtx ip_rtx;
6d3d9133
NC
10185 unsigned long live_regs_mask;
10186 unsigned long func_type;
68dfd979 10187 int fp_offset = 0;
095bb276 10188 int saved_pretend_args = 0;
5848830f 10189 int saved_regs = 0;
095bb276 10190 unsigned int args_to_push;
5848830f 10191 arm_stack_offsets *offsets;
d3236b4d 10192
6d3d9133 10193 func_type = arm_current_func_type ();
e2c671ba 10194
31fdb4d5 10195 /* Naked functions don't have prologues. */
6d3d9133 10196 if (IS_NAKED (func_type))
31fdb4d5
DE
10197 return;
10198
095bb276
NC
10199 /* Make a copy of c_f_p_a_s as we may need to modify it locally. */
10200 args_to_push = current_function_pretend_args_size;
10201
6d3d9133
NC
10202 /* Compute which register we will have to save onto the stack. */
10203 live_regs_mask = arm_compute_save_reg_mask ();
e2c671ba 10204
68dfd979 10205 ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
d3236b4d 10206
e2c671ba
RE
10207 if (frame_pointer_needed)
10208 {
7b8b8ade
NC
10209 if (IS_INTERRUPT (func_type))
10210 {
10211 /* Interrupt functions must not corrupt any registers.
10212 Creating a frame pointer however, corrupts the IP
10213 register, so we must push it first. */
10214 insn = emit_multi_reg_push (1 << IP_REGNUM);
121308d4
NC
10215
10216 /* Do not set RTX_FRAME_RELATED_P on this insn.
10217 The dwarf stack unwinding code only wants to see one
10218 stack decrement per function, and this is not it. If
10219 this instruction is labeled as being part of the frame
10220 creation sequence then dwarf2out_frame_debug_expr will
10221 abort when it encounters the assignment of IP to FP
10222 later on, since the use of SP here establishes SP as
10223 the CFA register and not IP.
10224
10225 Anyway this instruction is not really part of the stack
10226 frame creation although it is part of the prologue. */
7b8b8ade
NC
10227 }
10228 else if (IS_NESTED (func_type))
68dfd979
NC
10229 {
10230 /* The Static chain register is the same as the IP register
10231 used as a scratch register during stack frame creation.
10232 To get around this need to find somewhere to store IP
10233 whilst the frame is being created. We try the following
10234 places in order:
10235
6d3d9133 10236 1. The last argument register.
68dfd979
NC
10237 2. A slot on the stack above the frame. (This only
10238 works if the function is not a varargs function).
095bb276
NC
10239 3. Register r3, after pushing the argument registers
10240 onto the stack.
6d3d9133 10241
34ce3d7b
JM
10242 Note - we only need to tell the dwarf2 backend about the SP
10243 adjustment in the second variant; the static chain register
10244 doesn't need to be unwound, as it doesn't contain a value
10245 inherited from the caller. */
d3236b4d 10246
68dfd979
NC
10247 if (regs_ever_live[3] == 0)
10248 {
10249 insn = gen_rtx_REG (SImode, 3);
10250 insn = gen_rtx_SET (SImode, insn, ip_rtx);
d3236b4d 10251 insn = emit_insn (insn);
68dfd979 10252 }
095bb276 10253 else if (args_to_push == 0)
68dfd979 10254 {
34ce3d7b 10255 rtx dwarf;
68dfd979
NC
10256 insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10257 insn = gen_rtx_MEM (SImode, insn);
10258 insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10259 insn = emit_insn (insn);
34ce3d7b 10260
68dfd979 10261 fp_offset = 4;
34ce3d7b
JM
10262
10263 /* Just tell the dwarf backend that we adjusted SP. */
10264 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10265 gen_rtx_PLUS (SImode, stack_pointer_rtx,
10266 GEN_INT (-fp_offset)));
10267 RTX_FRAME_RELATED_P (insn) = 1;
10268 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10269 dwarf, REG_NOTES (insn));
68dfd979
NC
10270 }
10271 else
095bb276
NC
10272 {
10273 /* Store the args on the stack. */
3cb66fd7 10274 if (cfun->machine->uses_anonymous_args)
095bb276
NC
10275 insn = emit_multi_reg_push
10276 ((0xf0 >> (args_to_push / 4)) & 0xf);
10277 else
10278 insn = emit_insn
10279 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10280 GEN_INT (- args_to_push)));
10281
10282 RTX_FRAME_RELATED_P (insn) = 1;
10283
10284 saved_pretend_args = 1;
10285 fp_offset = args_to_push;
10286 args_to_push = 0;
10287
10288 /* Now reuse r3 to preserve IP. */
10289 insn = gen_rtx_REG (SImode, 3);
10290 insn = gen_rtx_SET (SImode, insn, ip_rtx);
10291 (void) emit_insn (insn);
10292 }
68dfd979
NC
10293 }
10294
68dfd979
NC
10295 if (fp_offset)
10296 {
10297 insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10298 insn = gen_rtx_SET (SImode, ip_rtx, insn);
10299 }
10300 else
10301 insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10302
6d3d9133 10303 insn = emit_insn (insn);
8e56560e 10304 RTX_FRAME_RELATED_P (insn) = 1;
e2c671ba
RE
10305 }
10306
095bb276 10307 if (args_to_push)
e2c671ba 10308 {
6d3d9133 10309 /* Push the argument registers, or reserve space for them. */
3cb66fd7 10310 if (cfun->machine->uses_anonymous_args)
2c849145 10311 insn = emit_multi_reg_push
095bb276 10312 ((0xf0 >> (args_to_push / 4)) & 0xf);
e2c671ba 10313 else
2c849145
JM
10314 insn = emit_insn
10315 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
095bb276 10316 GEN_INT (- args_to_push)));
2c849145 10317 RTX_FRAME_RELATED_P (insn) = 1;
e2c671ba
RE
10318 }
10319
06bea5aa
NC
10320 /* If this is an interrupt service routine, and the link register
10321 is going to be pushed, and we are not creating a stack frame,
10322 (which would involve an extra push of IP and a pop in the epilogue)
10323 subtracting four from LR now will mean that the function return
10324 can be done with a single instruction. */
3a7731fd 10325 if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
06bea5aa
NC
10326 && (live_regs_mask & (1 << LR_REGNUM)) != 0
10327 && ! frame_pointer_needed)
10328 emit_insn (gen_rtx_SET (SImode,
10329 gen_rtx_REG (SImode, LR_REGNUM),
10330 gen_rtx_PLUS (SImode,
10331 gen_rtx_REG (SImode, LR_REGNUM),
10332 GEN_INT (-4))));
3a7731fd 10333
e2c671ba
RE
10334 if (live_regs_mask)
10335 {
2c849145 10336 insn = emit_multi_reg_push (live_regs_mask);
5848830f 10337 saved_regs += bit_count (live_regs_mask) * 4;
2c849145 10338 RTX_FRAME_RELATED_P (insn) = 1;
e2c671ba 10339 }
d5b7b3ae 10340
5a9335ef
NC
10341 if (TARGET_IWMMXT)
10342 for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
10343 if (regs_ever_live[reg] && ! call_used_regs [reg])
10344 {
10345 insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10346 insn = gen_rtx_MEM (V2SImode, insn);
10347 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10348 gen_rtx_REG (V2SImode, reg)));
10349 RTX_FRAME_RELATED_P (insn) = 1;
5848830f 10350 saved_regs += 8;
5a9335ef
NC
10351 }
10352
6d3d9133 10353 if (! IS_VOLATILE (func_type))
b111229a 10354 {
9b66ebb1
PB
10355 int start_reg;
10356
29ad9694
RE
10357 /* Save any floating point call-saved registers used by this
10358 function. */
10359 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
b111229a 10360 {
9b66ebb1 10361 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
5895f793 10362 if (regs_ever_live[reg] && !call_used_regs[reg])
2c849145
JM
10363 {
10364 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10365 insn = gen_rtx_MEM (XFmode, insn);
10366 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10367 gen_rtx_REG (XFmode, reg)));
10368 RTX_FRAME_RELATED_P (insn) = 1;
5848830f 10369 saved_regs += 12;
2c849145 10370 }
b111229a
RE
10371 }
10372 else
10373 {
9b66ebb1 10374 start_reg = LAST_FPA_REGNUM;
b111229a 10375
9b66ebb1 10376 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
b111229a 10377 {
5895f793 10378 if (regs_ever_live[reg] && !call_used_regs[reg])
b111229a
RE
10379 {
10380 if (start_reg - reg == 3)
10381 {
2c849145
JM
10382 insn = emit_sfm (reg, 4);
10383 RTX_FRAME_RELATED_P (insn) = 1;
b111229a
RE
10384 start_reg = reg - 1;
10385 }
10386 }
10387 else
10388 {
10389 if (start_reg != reg)
2c849145
JM
10390 {
10391 insn = emit_sfm (reg + 1, start_reg - reg);
10392 RTX_FRAME_RELATED_P (insn) = 1;
5848830f 10393 saved_regs += (reg - start_reg) * 12;
2c849145 10394 }
b111229a
RE
10395 start_reg = reg - 1;
10396 }
10397 }
10398
10399 if (start_reg != reg)
2c849145
JM
10400 {
10401 insn = emit_sfm (reg + 1, start_reg - reg);
5848830f 10402 saved_regs += (reg - start_reg) * 12;
2c849145
JM
10403 RTX_FRAME_RELATED_P (insn) = 1;
10404 }
b111229a 10405 }
9b66ebb1
PB
10406 if (TARGET_HARD_FLOAT && TARGET_VFP)
10407 {
10408 start_reg = FIRST_VFP_REGNUM;
10409
10410 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10411 {
10412 if ((!regs_ever_live[reg] || call_used_regs[reg])
10413 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10414 {
10415 if (start_reg != reg)
9728c9d1
PB
10416 saved_regs += vfp_emit_fstmx (start_reg,
10417 (reg - start_reg) / 2);
9b66ebb1
PB
10418 start_reg = reg + 2;
10419 }
10420 }
10421 if (start_reg != reg)
9728c9d1
PB
10422 saved_regs += vfp_emit_fstmx (start_reg,
10423 (reg - start_reg) / 2);
9b66ebb1 10424 }
b111229a 10425 }
e2c671ba
RE
10426
10427 if (frame_pointer_needed)
2c849145 10428 {
6d3d9133 10429 /* Create the new frame pointer. */
095bb276 10430 insn = GEN_INT (-(4 + args_to_push + fp_offset));
68dfd979 10431 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
2c849145 10432 RTX_FRAME_RELATED_P (insn) = 1;
68dfd979 10433
6d3d9133 10434 if (IS_NESTED (func_type))
68dfd979
NC
10435 {
10436 /* Recover the static chain register. */
095bb276
NC
10437 if (regs_ever_live [3] == 0
10438 || saved_pretend_args)
1d6e90ac 10439 insn = gen_rtx_REG (SImode, 3);
68dfd979
NC
10440 else /* if (current_function_pretend_args_size == 0) */
10441 {
29ad9694
RE
10442 insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10443 GEN_INT (4));
68dfd979 10444 insn = gen_rtx_MEM (SImode, insn);
68dfd979 10445 }
1d6e90ac 10446
c14a3a45
NC
10447 emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10448 /* Add a USE to stop propagate_one_insn() from barfing. */
6bacc7b0 10449 emit_insn (gen_prologue_use (ip_rtx));
68dfd979 10450 }
2c849145 10451 }
e2c671ba 10452
5848830f
PB
10453 offsets = arm_get_frame_offsets ();
10454 if (offsets->outgoing_args != offsets->saved_args + saved_regs)
e2c671ba 10455 {
745b9093
JM
10456 /* This add can produce multiple insns for a large constant, so we
10457 need to get tricky. */
10458 rtx last = get_last_insn ();
5848830f
PB
10459
10460 amount = GEN_INT (offsets->saved_args + saved_regs
10461 - offsets->outgoing_args);
10462
2c849145
JM
10463 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10464 amount));
745b9093
JM
10465 do
10466 {
10467 last = last ? NEXT_INSN (last) : get_insns ();
10468 RTX_FRAME_RELATED_P (last) = 1;
10469 }
10470 while (last != insn);
e04c2d6c
RE
10471
10472 /* If the frame pointer is needed, emit a special barrier that
10473 will prevent the scheduler from moving stores to the frame
10474 before the stack adjustment. */
10475 if (frame_pointer_needed)
3894f59e
RE
10476 insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10477 hard_frame_pointer_rtx));
e2c671ba
RE
10478 }
10479
10480 /* If we are profiling, make sure no instructions are scheduled before
f5a1b0d2
NC
10481 the call to mcount. Similarly if the user has requested no
10482 scheduling in the prolog. */
70f4f91c 10483 if (current_function_profile || TARGET_NO_SCHED_PRO)
e2c671ba 10484 emit_insn (gen_blockage ());
6f7ebcbb
NC
10485
10486 /* If the link register is being kept alive, with the return address in it,
10487 then make sure that it does not get reused by the ce2 pass. */
10488 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10489 {
6bacc7b0 10490 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
6f7ebcbb
NC
10491 cfun->machine->lr_save_eliminated = 1;
10492 }
e2c671ba 10493}
cce8749e 10494\f
9997d19d
RE
10495/* If CODE is 'd', then the X is a condition operand and the instruction
10496 should only be executed if the condition is true.
ddd5a7c1 10497 if CODE is 'D', then the X is a condition operand and the instruction
9997d19d
RE
10498 should only be executed if the condition is false: however, if the mode
10499 of the comparison is CCFPEmode, then always execute the instruction -- we
10500 do this because in these circumstances !GE does not necessarily imply LT;
10501 in these cases the instruction pattern will take care to make sure that
10502 an instruction containing %d will follow, thereby undoing the effects of
ddd5a7c1 10503 doing this instruction unconditionally.
9997d19d
RE
10504 If CODE is 'N' then X is a floating point operand that must be negated
10505 before output.
10506 If CODE is 'B' then output a bitwise inverted value of X (a const int).
10507 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
9997d19d 10508void
e32bac5b 10509arm_print_operand (FILE *stream, rtx x, int code)
9997d19d
RE
10510{
10511 switch (code)
10512 {
10513 case '@':
f3139301 10514 fputs (ASM_COMMENT_START, stream);
9997d19d
RE
10515 return;
10516
d5b7b3ae
RE
10517 case '_':
10518 fputs (user_label_prefix, stream);
10519 return;
10520
9997d19d 10521 case '|':
f3139301 10522 fputs (REGISTER_PREFIX, stream);
9997d19d
RE
10523 return;
10524
10525 case '?':
10526 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
cca0a211
RE
10527 {
10528 if (TARGET_THUMB || current_insn_predicate != NULL)
10529 abort ();
10530
10531 fputs (arm_condition_codes[arm_current_cc], stream);
10532 }
10533 else if (current_insn_predicate)
10534 {
10535 enum arm_cond_code code;
10536
10537 if (TARGET_THUMB)
10538 abort ();
10539
10540 code = get_arm_condition_code (current_insn_predicate);
10541 fputs (arm_condition_codes[code], stream);
10542 }
9997d19d
RE
10543 return;
10544
10545 case 'N':
10546 {
10547 REAL_VALUE_TYPE r;
10548 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10549 r = REAL_VALUE_NEGATE (r);
10550 fprintf (stream, "%s", fp_const_from_val (&r));
10551 }
10552 return;
10553
10554 case 'B':
10555 if (GET_CODE (x) == CONST_INT)
4bc74ece
NC
10556 {
10557 HOST_WIDE_INT val;
5895f793 10558 val = ARM_SIGN_EXTEND (~INTVAL (x));
36ba9cb8 10559 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
4bc74ece 10560 }
9997d19d
RE
10561 else
10562 {
10563 putc ('~', stream);
10564 output_addr_const (stream, x);
10565 }
10566 return;
10567
10568 case 'i':
10569 fprintf (stream, "%s", arithmetic_instr (x, 1));
10570 return;
10571
9b6b54e2
NC
10572 /* Truncate Cirrus shift counts. */
10573 case 's':
10574 if (GET_CODE (x) == CONST_INT)
10575 {
10576 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10577 return;
10578 }
10579 arm_print_operand (stream, x, 0);
10580 return;
10581
9997d19d
RE
10582 case 'I':
10583 fprintf (stream, "%s", arithmetic_instr (x, 0));
10584 return;
10585
10586 case 'S':
10587 {
10588 HOST_WIDE_INT val;
5895f793 10589 const char * shift = shift_op (x, &val);
9997d19d 10590
e2c671ba
RE
10591 if (shift)
10592 {
5895f793 10593 fprintf (stream, ", %s ", shift_op (x, &val));
e2c671ba
RE
10594 if (val == -1)
10595 arm_print_operand (stream, XEXP (x, 1), 0);
10596 else
4a0a75dd 10597 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
e2c671ba 10598 }
9997d19d
RE
10599 }
10600 return;
10601
d5b7b3ae
RE
10602 /* An explanation of the 'Q', 'R' and 'H' register operands:
10603
10604 In a pair of registers containing a DI or DF value the 'Q'
10605 operand returns the register number of the register containing
093354e0 10606 the least significant part of the value. The 'R' operand returns
d5b7b3ae
RE
10607 the register number of the register containing the most
10608 significant part of the value.
10609
10610 The 'H' operand returns the higher of the two register numbers.
10611 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
093354e0 10612 same as the 'Q' operand, since the most significant part of the
d5b7b3ae
RE
10613 value is held in the lower number register. The reverse is true
10614 on systems where WORDS_BIG_ENDIAN is false.
10615
10616 The purpose of these operands is to distinguish between cases
10617 where the endian-ness of the values is important (for example
10618 when they are added together), and cases where the endian-ness
10619 is irrelevant, but the order of register operations is important.
10620 For example when loading a value from memory into a register
10621 pair, the endian-ness does not matter. Provided that the value
10622 from the lower memory address is put into the lower numbered
10623 register, and the value from the higher address is put into the
10624 higher numbered register, the load will work regardless of whether
10625 the value being loaded is big-wordian or little-wordian. The
10626 order of the two register loads can matter however, if the address
10627 of the memory location is actually held in one of the registers
10628 being overwritten by the load. */
c1c2bc04 10629 case 'Q':
d5b7b3ae 10630 if (REGNO (x) > LAST_ARM_REGNUM)
c1c2bc04 10631 abort ();
d5b7b3ae 10632 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
c1c2bc04
RE
10633 return;
10634
9997d19d 10635 case 'R':
d5b7b3ae 10636 if (REGNO (x) > LAST_ARM_REGNUM)
9997d19d 10637 abort ();
d5b7b3ae
RE
10638 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10639 return;
10640
10641 case 'H':
10642 if (REGNO (x) > LAST_ARM_REGNUM)
10643 abort ();
10644 asm_fprintf (stream, "%r", REGNO (x) + 1);
9997d19d
RE
10645 return;
10646
10647 case 'm':
d5b7b3ae
RE
10648 asm_fprintf (stream, "%r",
10649 GET_CODE (XEXP (x, 0)) == REG
10650 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
9997d19d
RE
10651 return;
10652
10653 case 'M':
dd18ae56 10654 asm_fprintf (stream, "{%r-%r}",
d5b7b3ae 10655 REGNO (x),
e9d7b180 10656 REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
9997d19d
RE
10657 return;
10658
10659 case 'd':
64e92a26
RE
10660 /* CONST_TRUE_RTX means always -- that's the default. */
10661 if (x == const_true_rtx)
d5b7b3ae
RE
10662 return;
10663
defc0463
RE
10664 fputs (arm_condition_codes[get_arm_condition_code (x)],
10665 stream);
9997d19d
RE
10666 return;
10667
10668 case 'D':
64e92a26
RE
10669 /* CONST_TRUE_RTX means not always -- ie never. We shouldn't ever
10670 want to do that. */
10671 if (x == const_true_rtx)
10672 abort ();
d5b7b3ae 10673
defc0463
RE
10674 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10675 (get_arm_condition_code (x))],
10676 stream);
9997d19d
RE
10677 return;
10678
9b6b54e2
NC
10679 /* Cirrus registers can be accessed in a variety of ways:
10680 single floating point (f)
10681 double floating point (d)
10682 32bit integer (fx)
10683 64bit integer (dx). */
10684 case 'W': /* Cirrus register in F mode. */
10685 case 'X': /* Cirrus register in D mode. */
10686 case 'Y': /* Cirrus register in FX mode. */
10687 case 'Z': /* Cirrus register in DX mode. */
10688 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10689 abort ();
10690
10691 fprintf (stream, "mv%s%s",
10692 code == 'W' ? "f"
10693 : code == 'X' ? "d"
10694 : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10695
10696 return;
10697
10698 /* Print cirrus register in the mode specified by the register's mode. */
10699 case 'V':
10700 {
10701 int mode = GET_MODE (x);
10702
10703 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10704 abort ();
10705
10706 fprintf (stream, "mv%s%s",
10707 mode == DFmode ? "d"
10708 : mode == SImode ? "fx"
10709 : mode == DImode ? "dx"
10710 : "f", reg_names[REGNO (x)] + 2);
10711
10712 return;
10713 }
10714
5a9335ef
NC
10715 case 'U':
10716 if (GET_CODE (x) != REG
10717 || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10718 || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10719 /* Bad value for wCG register number. */
10720 abort ();
10721 else
10722 fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10723 return;
10724
10725 /* Print an iWMMXt control register name. */
10726 case 'w':
10727 if (GET_CODE (x) != CONST_INT
10728 || INTVAL (x) < 0
10729 || INTVAL (x) >= 16)
10730 /* Bad value for wC register number. */
10731 abort ();
10732 else
10733 {
10734 static const char * wc_reg_names [16] =
10735 {
10736 "wCID", "wCon", "wCSSF", "wCASF",
10737 "wC4", "wC5", "wC6", "wC7",
10738 "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10739 "wC12", "wC13", "wC14", "wC15"
10740 };
10741
10742 fprintf (stream, wc_reg_names [INTVAL (x)]);
10743 }
10744 return;
10745
9b66ebb1
PB
10746 /* Print a VFP double precision register name. */
10747 case 'P':
10748 {
10749 int mode = GET_MODE (x);
10750 int num;
10751
10752 if (mode != DImode && mode != DFmode)
10753 abort ();
10754
10755 if (GET_CODE (x) != REG
10756 || !IS_VFP_REGNUM (REGNO (x)))
10757 abort ();
10758
10759 num = REGNO(x) - FIRST_VFP_REGNUM;
10760 if (num & 1)
10761 abort ();
10762
10763 fprintf (stream, "d%d", num >> 1);
10764 }
10765 return;
10766
9997d19d
RE
10767 default:
10768 if (x == 0)
10769 abort ();
10770
10771 if (GET_CODE (x) == REG)
d5b7b3ae 10772 asm_fprintf (stream, "%r", REGNO (x));
9997d19d
RE
10773 else if (GET_CODE (x) == MEM)
10774 {
10775 output_memory_reference_mode = GET_MODE (x);
10776 output_address (XEXP (x, 0));
10777 }
10778 else if (GET_CODE (x) == CONST_DOUBLE)
10779 fprintf (stream, "#%s", fp_immediate_constant (x));
10780 else if (GET_CODE (x) == NEG)
6354dc9b 10781 abort (); /* This should never happen now. */
9997d19d
RE
10782 else
10783 {
10784 fputc ('#', stream);
10785 output_addr_const (stream, x);
10786 }
10787 }
10788}
cce8749e 10789\f
301d03af
RS
10790#ifndef AOF_ASSEMBLER
10791/* Target hook for assembling integer objects. The ARM version needs to
10792 handle word-sized values specially. */
301d03af 10793static bool
e32bac5b 10794arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
301d03af
RS
10795{
10796 if (size == UNITS_PER_WORD && aligned_p)
10797 {
10798 fputs ("\t.word\t", asm_out_file);
10799 output_addr_const (asm_out_file, x);
10800
10801 /* Mark symbols as position independent. We only do this in the
d6b4baa4 10802 .text segment, not in the .data segment. */
301d03af
RS
10803 if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10804 (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10805 {
e26053d1 10806 if (GET_CODE (x) == SYMBOL_REF
14f583b8 10807 && (CONSTANT_POOL_ADDRESS_P (x)
94428622 10808 || SYMBOL_REF_LOCAL_P (x)))
301d03af
RS
10809 fputs ("(GOTOFF)", asm_out_file);
10810 else if (GET_CODE (x) == LABEL_REF)
10811 fputs ("(GOTOFF)", asm_out_file);
10812 else
10813 fputs ("(GOT)", asm_out_file);
10814 }
10815 fputc ('\n', asm_out_file);
10816 return true;
10817 }
1d6e90ac 10818
5a9335ef
NC
10819 if (VECTOR_MODE_SUPPORTED_P (GET_MODE (x)))
10820 {
10821 int i, units;
10822
10823 if (GET_CODE (x) != CONST_VECTOR)
10824 abort ();
10825
10826 units = CONST_VECTOR_NUNITS (x);
10827
10828 switch (GET_MODE (x))
10829 {
10830 case V2SImode: size = 4; break;
10831 case V4HImode: size = 2; break;
10832 case V8QImode: size = 1; break;
10833 default:
10834 abort ();
10835 }
10836
10837 for (i = 0; i < units; i++)
10838 {
10839 rtx elt;
10840
10841 elt = CONST_VECTOR_ELT (x, i);
10842 assemble_integer
10843 (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10844 }
10845
10846 return true;
10847 }
10848
301d03af
RS
10849 return default_assemble_integer (x, size, aligned_p);
10850}
10851#endif
10852\f
cce8749e
CH
10853/* A finite state machine takes care of noticing whether or not instructions
10854 can be conditionally executed, and thus decrease execution time and code
10855 size by deleting branch instructions. The fsm is controlled by
10856 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
10857
10858/* The state of the fsm controlling condition codes are:
10859 0: normal, do nothing special
10860 1: make ASM_OUTPUT_OPCODE not output this instruction
10861 2: make ASM_OUTPUT_OPCODE not output this instruction
10862 3: make instructions conditional
10863 4: make instructions conditional
10864
10865 State transitions (state->state by whom under condition):
10866 0 -> 1 final_prescan_insn if the `target' is a label
10867 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10868 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10869 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
4977bab6 10870 3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
cce8749e
CH
10871 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10872 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10873 (the target insn is arm_target_insn).
10874
ff9940b0
RE
10875 If the jump clobbers the conditions then we use states 2 and 4.
10876
10877 A similar thing can be done with conditional return insns.
10878
cce8749e
CH
10879 XXX In case the `target' is an unconditional branch, this conditionalising
10880 of the instructions always reduces code size, but not always execution
10881 time. But then, I want to reduce the code size to somewhere near what
10882 /bin/cc produces. */
10883
cce8749e
CH
10884/* Returns the index of the ARM condition code string in
10885 `arm_condition_codes'. COMPARISON should be an rtx like
10886 `(eq (...) (...))'. */
84ed5e79 10887static enum arm_cond_code
e32bac5b 10888get_arm_condition_code (rtx comparison)
cce8749e 10889{
5165176d 10890 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
1d6e90ac
NC
10891 int code;
10892 enum rtx_code comp_code = GET_CODE (comparison);
5165176d
RE
10893
10894 if (GET_MODE_CLASS (mode) != MODE_CC)
84ed5e79 10895 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
5165176d
RE
10896 XEXP (comparison, 1));
10897
10898 switch (mode)
cce8749e 10899 {
84ed5e79
RE
10900 case CC_DNEmode: code = ARM_NE; goto dominance;
10901 case CC_DEQmode: code = ARM_EQ; goto dominance;
10902 case CC_DGEmode: code = ARM_GE; goto dominance;
10903 case CC_DGTmode: code = ARM_GT; goto dominance;
10904 case CC_DLEmode: code = ARM_LE; goto dominance;
10905 case CC_DLTmode: code = ARM_LT; goto dominance;
10906 case CC_DGEUmode: code = ARM_CS; goto dominance;
10907 case CC_DGTUmode: code = ARM_HI; goto dominance;
10908 case CC_DLEUmode: code = ARM_LS; goto dominance;
10909 case CC_DLTUmode: code = ARM_CC;
10910
10911 dominance:
10912 if (comp_code != EQ && comp_code != NE)
10913 abort ();
10914
10915 if (comp_code == EQ)
10916 return ARM_INVERSE_CONDITION_CODE (code);
10917 return code;
10918
5165176d 10919 case CC_NOOVmode:
84ed5e79 10920 switch (comp_code)
5165176d 10921 {
84ed5e79
RE
10922 case NE: return ARM_NE;
10923 case EQ: return ARM_EQ;
10924 case GE: return ARM_PL;
10925 case LT: return ARM_MI;
5165176d
RE
10926 default: abort ();
10927 }
10928
10929 case CC_Zmode:
84ed5e79 10930 switch (comp_code)
5165176d 10931 {
84ed5e79
RE
10932 case NE: return ARM_NE;
10933 case EQ: return ARM_EQ;
5165176d
RE
10934 default: abort ();
10935 }
10936
defc0463
RE
10937 case CC_Nmode:
10938 switch (comp_code)
10939 {
10940 case NE: return ARM_MI;
10941 case EQ: return ARM_PL;
10942 default: abort ();
10943 }
10944
5165176d 10945 case CCFPEmode:
e45b72c4
RE
10946 case CCFPmode:
10947 /* These encodings assume that AC=1 in the FPA system control
10948 byte. This allows us to handle all cases except UNEQ and
10949 LTGT. */
84ed5e79
RE
10950 switch (comp_code)
10951 {
10952 case GE: return ARM_GE;
10953 case GT: return ARM_GT;
10954 case LE: return ARM_LS;
10955 case LT: return ARM_MI;
e45b72c4
RE
10956 case NE: return ARM_NE;
10957 case EQ: return ARM_EQ;
10958 case ORDERED: return ARM_VC;
10959 case UNORDERED: return ARM_VS;
10960 case UNLT: return ARM_LT;
10961 case UNLE: return ARM_LE;
10962 case UNGT: return ARM_HI;
10963 case UNGE: return ARM_PL;
10964 /* UNEQ and LTGT do not have a representation. */
10965 case UNEQ: /* Fall through. */
10966 case LTGT: /* Fall through. */
84ed5e79
RE
10967 default: abort ();
10968 }
10969
10970 case CC_SWPmode:
10971 switch (comp_code)
10972 {
10973 case NE: return ARM_NE;
10974 case EQ: return ARM_EQ;
10975 case GE: return ARM_LE;
10976 case GT: return ARM_LT;
10977 case LE: return ARM_GE;
10978 case LT: return ARM_GT;
10979 case GEU: return ARM_LS;
10980 case GTU: return ARM_CC;
10981 case LEU: return ARM_CS;
10982 case LTU: return ARM_HI;
10983 default: abort ();
10984 }
10985
bd9c7e23
RE
10986 case CC_Cmode:
10987 switch (comp_code)
10988 {
10989 case LTU: return ARM_CS;
10990 case GEU: return ARM_CC;
10991 default: abort ();
10992 }
10993
5165176d 10994 case CCmode:
84ed5e79 10995 switch (comp_code)
5165176d 10996 {
84ed5e79
RE
10997 case NE: return ARM_NE;
10998 case EQ: return ARM_EQ;
10999 case GE: return ARM_GE;
11000 case GT: return ARM_GT;
11001 case LE: return ARM_LE;
11002 case LT: return ARM_LT;
11003 case GEU: return ARM_CS;
11004 case GTU: return ARM_HI;
11005 case LEU: return ARM_LS;
11006 case LTU: return ARM_CC;
5165176d
RE
11007 default: abort ();
11008 }
11009
cce8749e
CH
11010 default: abort ();
11011 }
84ed5e79
RE
11012
11013 abort ();
f3bb6135 11014}
cce8749e 11015
cce8749e 11016void
e32bac5b 11017arm_final_prescan_insn (rtx insn)
cce8749e
CH
11018{
11019 /* BODY will hold the body of INSN. */
1d6e90ac 11020 rtx body = PATTERN (insn);
cce8749e
CH
11021
11022 /* This will be 1 if trying to repeat the trick, and things need to be
11023 reversed if it appears to fail. */
11024 int reverse = 0;
11025
ff9940b0
RE
11026 /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11027 taken are clobbered, even if the rtl suggests otherwise. It also
11028 means that we have to grub around within the jump expression to find
11029 out what the conditions are when the jump isn't taken. */
11030 int jump_clobbers = 0;
11031
6354dc9b 11032 /* If we start with a return insn, we only succeed if we find another one. */
ff9940b0
RE
11033 int seeking_return = 0;
11034
cce8749e
CH
11035 /* START_INSN will hold the insn from where we start looking. This is the
11036 first insn after the following code_label if REVERSE is true. */
11037 rtx start_insn = insn;
11038
11039 /* If in state 4, check if the target branch is reached, in order to
11040 change back to state 0. */
11041 if (arm_ccfsm_state == 4)
11042 {
11043 if (insn == arm_target_insn)
f5a1b0d2
NC
11044 {
11045 arm_target_insn = NULL;
11046 arm_ccfsm_state = 0;
11047 }
cce8749e
CH
11048 return;
11049 }
11050
11051 /* If in state 3, it is possible to repeat the trick, if this insn is an
11052 unconditional branch to a label, and immediately following this branch
11053 is the previous target label which is only used once, and the label this
11054 branch jumps to is not too far off. */
11055 if (arm_ccfsm_state == 3)
11056 {
11057 if (simplejump_p (insn))
11058 {
11059 start_insn = next_nonnote_insn (start_insn);
11060 if (GET_CODE (start_insn) == BARRIER)
11061 {
11062 /* XXX Isn't this always a barrier? */
11063 start_insn = next_nonnote_insn (start_insn);
11064 }
11065 if (GET_CODE (start_insn) == CODE_LABEL
11066 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11067 && LABEL_NUSES (start_insn) == 1)
11068 reverse = TRUE;
11069 else
11070 return;
11071 }
ff9940b0
RE
11072 else if (GET_CODE (body) == RETURN)
11073 {
11074 start_insn = next_nonnote_insn (start_insn);
11075 if (GET_CODE (start_insn) == BARRIER)
11076 start_insn = next_nonnote_insn (start_insn);
11077 if (GET_CODE (start_insn) == CODE_LABEL
11078 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11079 && LABEL_NUSES (start_insn) == 1)
11080 {
11081 reverse = TRUE;
11082 seeking_return = 1;
11083 }
11084 else
11085 return;
11086 }
cce8749e
CH
11087 else
11088 return;
11089 }
11090
11091 if (arm_ccfsm_state != 0 && !reverse)
11092 abort ();
11093 if (GET_CODE (insn) != JUMP_INSN)
11094 return;
11095
ddd5a7c1 11096 /* This jump might be paralleled with a clobber of the condition codes
ff9940b0
RE
11097 the jump should always come first */
11098 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11099 body = XVECEXP (body, 0, 0);
11100
cce8749e
CH
11101 if (reverse
11102 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11103 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11104 {
bd9c7e23
RE
11105 int insns_skipped;
11106 int fail = FALSE, succeed = FALSE;
cce8749e
CH
11107 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
11108 int then_not_else = TRUE;
ff9940b0 11109 rtx this_insn = start_insn, label = 0;
cce8749e 11110
e45b72c4
RE
11111 /* If the jump cannot be done with one instruction, we cannot
11112 conditionally execute the instruction in the inverse case. */
ff9940b0 11113 if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
5bbe2d40 11114 {
5bbe2d40
RE
11115 jump_clobbers = 1;
11116 return;
11117 }
ff9940b0 11118
cce8749e
CH
11119 /* Register the insn jumped to. */
11120 if (reverse)
ff9940b0
RE
11121 {
11122 if (!seeking_return)
11123 label = XEXP (SET_SRC (body), 0);
11124 }
cce8749e
CH
11125 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11126 label = XEXP (XEXP (SET_SRC (body), 1), 0);
11127 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11128 {
11129 label = XEXP (XEXP (SET_SRC (body), 2), 0);
11130 then_not_else = FALSE;
11131 }
ff9940b0
RE
11132 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11133 seeking_return = 1;
11134 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11135 {
11136 seeking_return = 1;
11137 then_not_else = FALSE;
11138 }
cce8749e
CH
11139 else
11140 abort ();
11141
11142 /* See how many insns this branch skips, and what kind of insns. If all
11143 insns are okay, and the label or unconditional branch to the same
11144 label is not too far away, succeed. */
11145 for (insns_skipped = 0;
b36ba79f 11146 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
cce8749e
CH
11147 {
11148 rtx scanbody;
11149
11150 this_insn = next_nonnote_insn (this_insn);
11151 if (!this_insn)
11152 break;
11153
cce8749e
CH
11154 switch (GET_CODE (this_insn))
11155 {
11156 case CODE_LABEL:
11157 /* Succeed if it is the target label, otherwise fail since
11158 control falls in from somewhere else. */
11159 if (this_insn == label)
11160 {
ff9940b0
RE
11161 if (jump_clobbers)
11162 {
11163 arm_ccfsm_state = 2;
11164 this_insn = next_nonnote_insn (this_insn);
11165 }
11166 else
11167 arm_ccfsm_state = 1;
cce8749e
CH
11168 succeed = TRUE;
11169 }
11170 else
11171 fail = TRUE;
11172 break;
11173
ff9940b0 11174 case BARRIER:
cce8749e 11175 /* Succeed if the following insn is the target label.
ff9940b0
RE
11176 Otherwise fail.
11177 If return insns are used then the last insn in a function
6354dc9b 11178 will be a barrier. */
cce8749e 11179 this_insn = next_nonnote_insn (this_insn);
ff9940b0 11180 if (this_insn && this_insn == label)
cce8749e 11181 {
ff9940b0
RE
11182 if (jump_clobbers)
11183 {
11184 arm_ccfsm_state = 2;
11185 this_insn = next_nonnote_insn (this_insn);
11186 }
11187 else
11188 arm_ccfsm_state = 1;
cce8749e
CH
11189 succeed = TRUE;
11190 }
11191 else
11192 fail = TRUE;
11193 break;
11194
ff9940b0 11195 case CALL_INSN:
2b835d68 11196 /* If using 32-bit addresses the cc is not preserved over
914a3b8c 11197 calls. */
2b835d68 11198 if (TARGET_APCS_32)
bd9c7e23
RE
11199 {
11200 /* Succeed if the following insn is the target label,
11201 or if the following two insns are a barrier and
11202 the target label. */
11203 this_insn = next_nonnote_insn (this_insn);
11204 if (this_insn && GET_CODE (this_insn) == BARRIER)
11205 this_insn = next_nonnote_insn (this_insn);
11206
11207 if (this_insn && this_insn == label
b36ba79f 11208 && insns_skipped < max_insns_skipped)
bd9c7e23
RE
11209 {
11210 if (jump_clobbers)
11211 {
11212 arm_ccfsm_state = 2;
11213 this_insn = next_nonnote_insn (this_insn);
11214 }
11215 else
11216 arm_ccfsm_state = 1;
11217 succeed = TRUE;
11218 }
11219 else
11220 fail = TRUE;
11221 }
ff9940b0 11222 break;
2b835d68 11223
cce8749e
CH
11224 case JUMP_INSN:
11225 /* If this is an unconditional branch to the same label, succeed.
11226 If it is to another label, do nothing. If it is conditional,
11227 fail. */
e32bac5b
RE
11228 /* XXX Probably, the tests for SET and the PC are
11229 unnecessary. */
cce8749e 11230
ed4c4348 11231 scanbody = PATTERN (this_insn);
ff9940b0
RE
11232 if (GET_CODE (scanbody) == SET
11233 && GET_CODE (SET_DEST (scanbody)) == PC)
cce8749e
CH
11234 {
11235 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11236 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11237 {
11238 arm_ccfsm_state = 2;
11239 succeed = TRUE;
11240 }
11241 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11242 fail = TRUE;
11243 }
b36ba79f
RE
11244 /* Fail if a conditional return is undesirable (eg on a
11245 StrongARM), but still allow this if optimizing for size. */
11246 else if (GET_CODE (scanbody) == RETURN
a72d4945 11247 && !use_return_insn (TRUE, NULL)
5895f793 11248 && !optimize_size)
b36ba79f 11249 fail = TRUE;
ff9940b0
RE
11250 else if (GET_CODE (scanbody) == RETURN
11251 && seeking_return)
11252 {
11253 arm_ccfsm_state = 2;
11254 succeed = TRUE;
11255 }
11256 else if (GET_CODE (scanbody) == PARALLEL)
11257 {
11258 switch (get_attr_conds (this_insn))
11259 {
11260 case CONDS_NOCOND:
11261 break;
11262 default:
11263 fail = TRUE;
11264 break;
11265 }
11266 }
4e67550b
RE
11267 else
11268 fail = TRUE; /* Unrecognized jump (eg epilogue). */
11269
cce8749e
CH
11270 break;
11271
11272 case INSN:
ff9940b0
RE
11273 /* Instructions using or affecting the condition codes make it
11274 fail. */
ed4c4348 11275 scanbody = PATTERN (this_insn);
5895f793
RE
11276 if (!(GET_CODE (scanbody) == SET
11277 || GET_CODE (scanbody) == PARALLEL)
74641843 11278 || get_attr_conds (this_insn) != CONDS_NOCOND)
cce8749e 11279 fail = TRUE;
9b6b54e2
NC
11280
11281 /* A conditional cirrus instruction must be followed by
11282 a non Cirrus instruction. However, since we
11283 conditionalize instructions in this function and by
11284 the time we get here we can't add instructions
11285 (nops), because shorten_branches() has already been
11286 called, we will disable conditionalizing Cirrus
11287 instructions to be safe. */
11288 if (GET_CODE (scanbody) != USE
11289 && GET_CODE (scanbody) != CLOBBER
f0375c66 11290 && get_attr_cirrus (this_insn) != CIRRUS_NOT)
9b6b54e2 11291 fail = TRUE;
cce8749e
CH
11292 break;
11293
11294 default:
11295 break;
11296 }
11297 }
11298 if (succeed)
11299 {
ff9940b0 11300 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
cce8749e 11301 arm_target_label = CODE_LABEL_NUMBER (label);
ff9940b0
RE
11302 else if (seeking_return || arm_ccfsm_state == 2)
11303 {
11304 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11305 {
11306 this_insn = next_nonnote_insn (this_insn);
11307 if (this_insn && (GET_CODE (this_insn) == BARRIER
11308 || GET_CODE (this_insn) == CODE_LABEL))
11309 abort ();
11310 }
11311 if (!this_insn)
11312 {
d6b4baa4 11313 /* Oh, dear! we ran off the end.. give up. */
df4ae160 11314 recog (PATTERN (insn), insn, NULL);
ff9940b0 11315 arm_ccfsm_state = 0;
abaa26e5 11316 arm_target_insn = NULL;
ff9940b0
RE
11317 return;
11318 }
11319 arm_target_insn = this_insn;
11320 }
cce8749e
CH
11321 else
11322 abort ();
ff9940b0
RE
11323 if (jump_clobbers)
11324 {
11325 if (reverse)
11326 abort ();
11327 arm_current_cc =
11328 get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11329 0), 0), 1));
11330 if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11331 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11332 if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11333 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11334 }
11335 else
11336 {
11337 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11338 what it was. */
11339 if (!reverse)
11340 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11341 0));
11342 }
cce8749e 11343
cce8749e
CH
11344 if (reverse || then_not_else)
11345 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11346 }
d5b7b3ae 11347
1ccbefce 11348 /* Restore recog_data (getting the attributes of other insns can
ff9940b0 11349 destroy this array, but final.c assumes that it remains intact
ddd5a7c1 11350 across this call; since the insn has been recognized already we
b020fd92 11351 call recog direct). */
df4ae160 11352 recog (PATTERN (insn), insn, NULL);
cce8749e 11353 }
f3bb6135 11354}
cce8749e 11355
4b02997f
NC
11356/* Returns true if REGNO is a valid register
11357 for holding a quantity of tyoe MODE. */
4b02997f 11358int
e32bac5b 11359arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
4b02997f
NC
11360{
11361 if (GET_MODE_CLASS (mode) == MODE_CC)
9b66ebb1 11362 return regno == CC_REGNUM || regno == VFPCC_REGNUM;
4b02997f
NC
11363
11364 if (TARGET_THUMB)
11365 /* For the Thumb we only allow values bigger than SImode in
11366 registers 0 - 6, so that there is always a second low
11367 register available to hold the upper part of the value.
11368 We probably we ought to ensure that the register is the
11369 start of an even numbered register pair. */
e9d7b180 11370 return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
4b02997f 11371
9b6b54e2
NC
11372 if (IS_CIRRUS_REGNUM (regno))
11373 /* We have outlawed SI values in Cirrus registers because they
11374 reside in the lower 32 bits, but SF values reside in the
11375 upper 32 bits. This causes gcc all sorts of grief. We can't
11376 even split the registers into pairs because Cirrus SI values
11377 get sign extended to 64bits-- aldyh. */
11378 return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11379
9b66ebb1
PB
11380 if (IS_VFP_REGNUM (regno))
11381 {
11382 if (mode == SFmode || mode == SImode)
11383 return TRUE;
11384
11385 /* DFmode values are only valid in even register pairs. */
11386 if (mode == DFmode)
11387 return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11388 return FALSE;
11389 }
11390
5a9335ef
NC
11391 if (IS_IWMMXT_GR_REGNUM (regno))
11392 return mode == SImode;
11393
11394 if (IS_IWMMXT_REGNUM (regno))
11395 return VALID_IWMMXT_REG_MODE (mode);
11396
4b02997f 11397 if (regno <= LAST_ARM_REGNUM)
9a9f7594 11398 /* We allow any value to be stored in the general registers. */
3cb66fd7 11399 return 1;
4b02997f
NC
11400
11401 if ( regno == FRAME_POINTER_REGNUM
11402 || regno == ARG_POINTER_REGNUM)
11403 /* We only allow integers in the fake hard registers. */
11404 return GET_MODE_CLASS (mode) == MODE_INT;
11405
3b684012 11406 /* The only registers left are the FPA registers
4b02997f
NC
11407 which we only allow to hold FP values. */
11408 return GET_MODE_CLASS (mode) == MODE_FLOAT
9b66ebb1
PB
11409 && regno >= FIRST_FPA_REGNUM
11410 && regno <= LAST_FPA_REGNUM;
4b02997f
NC
11411}
11412
d5b7b3ae 11413int
e32bac5b 11414arm_regno_class (int regno)
d5b7b3ae
RE
11415{
11416 if (TARGET_THUMB)
11417 {
11418 if (regno == STACK_POINTER_REGNUM)
11419 return STACK_REG;
11420 if (regno == CC_REGNUM)
11421 return CC_REG;
11422 if (regno < 8)
11423 return LO_REGS;
11424 return HI_REGS;
11425 }
11426
11427 if ( regno <= LAST_ARM_REGNUM
11428 || regno == FRAME_POINTER_REGNUM
11429 || regno == ARG_POINTER_REGNUM)
11430 return GENERAL_REGS;
11431
9b66ebb1 11432 if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
d5b7b3ae
RE
11433 return NO_REGS;
11434
9b6b54e2
NC
11435 if (IS_CIRRUS_REGNUM (regno))
11436 return CIRRUS_REGS;
11437
9b66ebb1
PB
11438 if (IS_VFP_REGNUM (regno))
11439 return VFP_REGS;
11440
5a9335ef
NC
11441 if (IS_IWMMXT_REGNUM (regno))
11442 return IWMMXT_REGS;
11443
e99faaaa
ILT
11444 if (IS_IWMMXT_GR_REGNUM (regno))
11445 return IWMMXT_GR_REGS;
11446
3b684012 11447 return FPA_REGS;
d5b7b3ae
RE
11448}
11449
11450/* Handle a special case when computing the offset
11451 of an argument from the frame pointer. */
11452int
e32bac5b 11453arm_debugger_arg_offset (int value, rtx addr)
d5b7b3ae
RE
11454{
11455 rtx insn;
11456
11457 /* We are only interested if dbxout_parms() failed to compute the offset. */
11458 if (value != 0)
11459 return 0;
11460
11461 /* We can only cope with the case where the address is held in a register. */
11462 if (GET_CODE (addr) != REG)
11463 return 0;
11464
11465 /* If we are using the frame pointer to point at the argument, then
11466 an offset of 0 is correct. */
cd2b33d0 11467 if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
d5b7b3ae
RE
11468 return 0;
11469
11470 /* If we are using the stack pointer to point at the
11471 argument, then an offset of 0 is correct. */
5895f793 11472 if ((TARGET_THUMB || !frame_pointer_needed)
d5b7b3ae
RE
11473 && REGNO (addr) == SP_REGNUM)
11474 return 0;
11475
11476 /* Oh dear. The argument is pointed to by a register rather
11477 than being held in a register, or being stored at a known
11478 offset from the frame pointer. Since GDB only understands
11479 those two kinds of argument we must translate the address
11480 held in the register into an offset from the frame pointer.
11481 We do this by searching through the insns for the function
11482 looking to see where this register gets its value. If the
4912a07c 11483 register is initialized from the frame pointer plus an offset
d5b7b3ae
RE
11484 then we are in luck and we can continue, otherwise we give up.
11485
11486 This code is exercised by producing debugging information
11487 for a function with arguments like this:
11488
11489 double func (double a, double b, int c, double d) {return d;}
11490
11491 Without this code the stab for parameter 'd' will be set to
11492 an offset of 0 from the frame pointer, rather than 8. */
11493
11494 /* The if() statement says:
11495
11496 If the insn is a normal instruction
11497 and if the insn is setting the value in a register
11498 and if the register being set is the register holding the address of the argument
11499 and if the address is computing by an addition
11500 that involves adding to a register
11501 which is the frame pointer
11502 a constant integer
11503
d6b4baa4 11504 then... */
d5b7b3ae
RE
11505
11506 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11507 {
11508 if ( GET_CODE (insn) == INSN
11509 && GET_CODE (PATTERN (insn)) == SET
11510 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11511 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11512 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
cd2b33d0 11513 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
d5b7b3ae
RE
11514 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11515 )
11516 {
11517 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11518
11519 break;
11520 }
11521 }
11522
11523 if (value == 0)
11524 {
11525 debug_rtx (addr);
c725bd79 11526 warning ("unable to compute real location of stacked parameter");
d5b7b3ae
RE
11527 value = 8; /* XXX magic hack */
11528 }
11529
11530 return value;
11531}
d5b7b3ae 11532\f
5a9335ef
NC
11533#define def_mbuiltin(MASK, NAME, TYPE, CODE) \
11534 do \
11535 { \
11536 if ((MASK) & insn_flags) \
11537 builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD, NULL, NULL_TREE); \
11538 } \
11539 while (0)
11540
11541struct builtin_description
11542{
11543 const unsigned int mask;
11544 const enum insn_code icode;
11545 const char * const name;
11546 const enum arm_builtins code;
11547 const enum rtx_code comparison;
11548 const unsigned int flag;
11549};
11550
11551static const struct builtin_description bdesc_2arg[] =
11552{
11553#define IWMMXT_BUILTIN(code, string, builtin) \
11554 { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11555 ARM_BUILTIN_##builtin, 0, 0 },
11556
11557 IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11558 IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11559 IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11560 IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11561 IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11562 IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11563 IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11564 IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11565 IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11566 IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11567 IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11568 IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11569 IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11570 IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11571 IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11572 IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11573 IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11574 IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11575 IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
f07a6b21
BE
11576 IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11577 IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
5a9335ef
NC
11578 IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11579 IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11580 IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11581 IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11582 IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11583 IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11584 IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11585 IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11586 IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11587 IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11588 IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11589 IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11590 IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11591 IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11592 IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11593 IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11594 IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11595 IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11596 IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11597 IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11598 IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11599 IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11600 IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11601 IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11602 IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11603 IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11604 IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11605 IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11606 IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11607 IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11608 IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11609 IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11610 IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11611 IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11612 IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11613 IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11614 IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11615
11616#define IWMMXT_BUILTIN2(code, builtin) \
11617 { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11618
11619 IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11620 IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11621 IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11622 IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11623 IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11624 IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11625 IWMMXT_BUILTIN2 (ashlv4hi3_di, WSLLH)
11626 IWMMXT_BUILTIN2 (ashlv4hi3, WSLLHI)
11627 IWMMXT_BUILTIN2 (ashlv2si3_di, WSLLW)
11628 IWMMXT_BUILTIN2 (ashlv2si3, WSLLWI)
11629 IWMMXT_BUILTIN2 (ashldi3_di, WSLLD)
11630 IWMMXT_BUILTIN2 (ashldi3_iwmmxt, WSLLDI)
11631 IWMMXT_BUILTIN2 (lshrv4hi3_di, WSRLH)
11632 IWMMXT_BUILTIN2 (lshrv4hi3, WSRLHI)
11633 IWMMXT_BUILTIN2 (lshrv2si3_di, WSRLW)
11634 IWMMXT_BUILTIN2 (lshrv2si3, WSRLWI)
11635 IWMMXT_BUILTIN2 (lshrdi3_di, WSRLD)
9b66ebb1 11636 IWMMXT_BUILTIN2 (lshrdi3_iwmmxt, WSRLDI)
5a9335ef
NC
11637 IWMMXT_BUILTIN2 (ashrv4hi3_di, WSRAH)
11638 IWMMXT_BUILTIN2 (ashrv4hi3, WSRAHI)
11639 IWMMXT_BUILTIN2 (ashrv2si3_di, WSRAW)
11640 IWMMXT_BUILTIN2 (ashrv2si3, WSRAWI)
11641 IWMMXT_BUILTIN2 (ashrdi3_di, WSRAD)
9b66ebb1 11642 IWMMXT_BUILTIN2 (ashrdi3_iwmmxt, WSRADI)
5a9335ef
NC
11643 IWMMXT_BUILTIN2 (rorv4hi3_di, WRORH)
11644 IWMMXT_BUILTIN2 (rorv4hi3, WRORHI)
11645 IWMMXT_BUILTIN2 (rorv2si3_di, WRORW)
11646 IWMMXT_BUILTIN2 (rorv2si3, WRORWI)
11647 IWMMXT_BUILTIN2 (rordi3_di, WRORD)
11648 IWMMXT_BUILTIN2 (rordi3, WRORDI)
11649 IWMMXT_BUILTIN2 (iwmmxt_wmacuz, WMACUZ)
11650 IWMMXT_BUILTIN2 (iwmmxt_wmacsz, WMACSZ)
11651};
11652
11653static const struct builtin_description bdesc_1arg[] =
11654{
11655 IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11656 IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11657 IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11658 IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11659 IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11660 IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11661 IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11662 IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11663 IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11664 IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11665 IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11666 IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11667 IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11668 IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11669 IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11670 IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11671 IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11672 IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11673};
11674
11675/* Set up all the iWMMXt builtins. This is
11676 not called if TARGET_IWMMXT is zero. */
11677
11678static void
11679arm_init_iwmmxt_builtins (void)
11680{
11681 const struct builtin_description * d;
11682 size_t i;
11683 tree endlink = void_list_node;
11684
4a5eab38
PB
11685 tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11686 tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11687 tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11688
5a9335ef
NC
11689 tree int_ftype_int
11690 = build_function_type (integer_type_node,
11691 tree_cons (NULL_TREE, integer_type_node, endlink));
11692 tree v8qi_ftype_v8qi_v8qi_int
11693 = build_function_type (V8QI_type_node,
11694 tree_cons (NULL_TREE, V8QI_type_node,
11695 tree_cons (NULL_TREE, V8QI_type_node,
11696 tree_cons (NULL_TREE,
11697 integer_type_node,
11698 endlink))));
11699 tree v4hi_ftype_v4hi_int
11700 = build_function_type (V4HI_type_node,
11701 tree_cons (NULL_TREE, V4HI_type_node,
11702 tree_cons (NULL_TREE, integer_type_node,
11703 endlink)));
11704 tree v2si_ftype_v2si_int
11705 = build_function_type (V2SI_type_node,
11706 tree_cons (NULL_TREE, V2SI_type_node,
11707 tree_cons (NULL_TREE, integer_type_node,
11708 endlink)));
11709 tree v2si_ftype_di_di
11710 = build_function_type (V2SI_type_node,
11711 tree_cons (NULL_TREE, long_long_integer_type_node,
11712 tree_cons (NULL_TREE, long_long_integer_type_node,
11713 endlink)));
11714 tree di_ftype_di_int
11715 = build_function_type (long_long_integer_type_node,
11716 tree_cons (NULL_TREE, long_long_integer_type_node,
11717 tree_cons (NULL_TREE, integer_type_node,
11718 endlink)));
11719 tree di_ftype_di_int_int
11720 = build_function_type (long_long_integer_type_node,
11721 tree_cons (NULL_TREE, long_long_integer_type_node,
11722 tree_cons (NULL_TREE, integer_type_node,
11723 tree_cons (NULL_TREE,
11724 integer_type_node,
11725 endlink))));
11726 tree int_ftype_v8qi
11727 = build_function_type (integer_type_node,
11728 tree_cons (NULL_TREE, V8QI_type_node,
11729 endlink));
11730 tree int_ftype_v4hi
11731 = build_function_type (integer_type_node,
11732 tree_cons (NULL_TREE, V4HI_type_node,
11733 endlink));
11734 tree int_ftype_v2si
11735 = build_function_type (integer_type_node,
11736 tree_cons (NULL_TREE, V2SI_type_node,
11737 endlink));
11738 tree int_ftype_v8qi_int
11739 = build_function_type (integer_type_node,
11740 tree_cons (NULL_TREE, V8QI_type_node,
11741 tree_cons (NULL_TREE, integer_type_node,
11742 endlink)));
11743 tree int_ftype_v4hi_int
11744 = build_function_type (integer_type_node,
11745 tree_cons (NULL_TREE, V4HI_type_node,
11746 tree_cons (NULL_TREE, integer_type_node,
11747 endlink)));
11748 tree int_ftype_v2si_int
11749 = build_function_type (integer_type_node,
11750 tree_cons (NULL_TREE, V2SI_type_node,
11751 tree_cons (NULL_TREE, integer_type_node,
11752 endlink)));
11753 tree v8qi_ftype_v8qi_int_int
11754 = build_function_type (V8QI_type_node,
11755 tree_cons (NULL_TREE, V8QI_type_node,
11756 tree_cons (NULL_TREE, integer_type_node,
11757 tree_cons (NULL_TREE,
11758 integer_type_node,
11759 endlink))));
11760 tree v4hi_ftype_v4hi_int_int
11761 = build_function_type (V4HI_type_node,
11762 tree_cons (NULL_TREE, V4HI_type_node,
11763 tree_cons (NULL_TREE, integer_type_node,
11764 tree_cons (NULL_TREE,
11765 integer_type_node,
11766 endlink))));
11767 tree v2si_ftype_v2si_int_int
11768 = build_function_type (V2SI_type_node,
11769 tree_cons (NULL_TREE, V2SI_type_node,
11770 tree_cons (NULL_TREE, integer_type_node,
11771 tree_cons (NULL_TREE,
11772 integer_type_node,
11773 endlink))));
11774 /* Miscellaneous. */
11775 tree v8qi_ftype_v4hi_v4hi
11776 = build_function_type (V8QI_type_node,
11777 tree_cons (NULL_TREE, V4HI_type_node,
11778 tree_cons (NULL_TREE, V4HI_type_node,
11779 endlink)));
11780 tree v4hi_ftype_v2si_v2si
11781 = build_function_type (V4HI_type_node,
11782 tree_cons (NULL_TREE, V2SI_type_node,
11783 tree_cons (NULL_TREE, V2SI_type_node,
11784 endlink)));
11785 tree v2si_ftype_v4hi_v4hi
11786 = build_function_type (V2SI_type_node,
11787 tree_cons (NULL_TREE, V4HI_type_node,
11788 tree_cons (NULL_TREE, V4HI_type_node,
11789 endlink)));
11790 tree v2si_ftype_v8qi_v8qi
11791 = build_function_type (V2SI_type_node,
11792 tree_cons (NULL_TREE, V8QI_type_node,
11793 tree_cons (NULL_TREE, V8QI_type_node,
11794 endlink)));
11795 tree v4hi_ftype_v4hi_di
11796 = build_function_type (V4HI_type_node,
11797 tree_cons (NULL_TREE, V4HI_type_node,
11798 tree_cons (NULL_TREE,
11799 long_long_integer_type_node,
11800 endlink)));
11801 tree v2si_ftype_v2si_di
11802 = build_function_type (V2SI_type_node,
11803 tree_cons (NULL_TREE, V2SI_type_node,
11804 tree_cons (NULL_TREE,
11805 long_long_integer_type_node,
11806 endlink)));
11807 tree void_ftype_int_int
11808 = build_function_type (void_type_node,
11809 tree_cons (NULL_TREE, integer_type_node,
11810 tree_cons (NULL_TREE, integer_type_node,
11811 endlink)));
11812 tree di_ftype_void
11813 = build_function_type (long_long_unsigned_type_node, endlink);
11814 tree di_ftype_v8qi
11815 = build_function_type (long_long_integer_type_node,
11816 tree_cons (NULL_TREE, V8QI_type_node,
11817 endlink));
11818 tree di_ftype_v4hi
11819 = build_function_type (long_long_integer_type_node,
11820 tree_cons (NULL_TREE, V4HI_type_node,
11821 endlink));
11822 tree di_ftype_v2si
11823 = build_function_type (long_long_integer_type_node,
11824 tree_cons (NULL_TREE, V2SI_type_node,
11825 endlink));
11826 tree v2si_ftype_v4hi
11827 = build_function_type (V2SI_type_node,
11828 tree_cons (NULL_TREE, V4HI_type_node,
11829 endlink));
11830 tree v4hi_ftype_v8qi
11831 = build_function_type (V4HI_type_node,
11832 tree_cons (NULL_TREE, V8QI_type_node,
11833 endlink));
11834
11835 tree di_ftype_di_v4hi_v4hi
11836 = build_function_type (long_long_unsigned_type_node,
11837 tree_cons (NULL_TREE,
11838 long_long_unsigned_type_node,
11839 tree_cons (NULL_TREE, V4HI_type_node,
11840 tree_cons (NULL_TREE,
11841 V4HI_type_node,
11842 endlink))));
11843
11844 tree di_ftype_v4hi_v4hi
11845 = build_function_type (long_long_unsigned_type_node,
11846 tree_cons (NULL_TREE, V4HI_type_node,
11847 tree_cons (NULL_TREE, V4HI_type_node,
11848 endlink)));
11849
11850 /* Normal vector binops. */
11851 tree v8qi_ftype_v8qi_v8qi
11852 = build_function_type (V8QI_type_node,
11853 tree_cons (NULL_TREE, V8QI_type_node,
11854 tree_cons (NULL_TREE, V8QI_type_node,
11855 endlink)));
11856 tree v4hi_ftype_v4hi_v4hi
11857 = build_function_type (V4HI_type_node,
11858 tree_cons (NULL_TREE, V4HI_type_node,
11859 tree_cons (NULL_TREE, V4HI_type_node,
11860 endlink)));
11861 tree v2si_ftype_v2si_v2si
11862 = build_function_type (V2SI_type_node,
11863 tree_cons (NULL_TREE, V2SI_type_node,
11864 tree_cons (NULL_TREE, V2SI_type_node,
11865 endlink)));
11866 tree di_ftype_di_di
11867 = build_function_type (long_long_unsigned_type_node,
11868 tree_cons (NULL_TREE, long_long_unsigned_type_node,
11869 tree_cons (NULL_TREE,
11870 long_long_unsigned_type_node,
11871 endlink)));
11872
11873 /* Add all builtins that are more or less simple operations on two
11874 operands. */
e97a46ce 11875 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
5a9335ef
NC
11876 {
11877 /* Use one of the operands; the target can have a different mode for
11878 mask-generating compares. */
11879 enum machine_mode mode;
11880 tree type;
11881
11882 if (d->name == 0)
11883 continue;
11884
11885 mode = insn_data[d->icode].operand[1].mode;
11886
11887 switch (mode)
11888 {
11889 case V8QImode:
11890 type = v8qi_ftype_v8qi_v8qi;
11891 break;
11892 case V4HImode:
11893 type = v4hi_ftype_v4hi_v4hi;
11894 break;
11895 case V2SImode:
11896 type = v2si_ftype_v2si_v2si;
11897 break;
11898 case DImode:
11899 type = di_ftype_di_di;
11900 break;
11901
11902 default:
11903 abort ();
11904 }
11905
11906 def_mbuiltin (d->mask, d->name, type, d->code);
11907 }
11908
11909 /* Add the remaining MMX insns with somewhat more complicated types. */
11910 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11911 def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11912 def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11913
11914 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11915 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11916 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11917 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11918 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11919 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11920
11921 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11922 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11923 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11924 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11925 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11926 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11927
11928 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11929 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11930 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11931 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11932 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11933 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11934
11935 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11936 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11937 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11938 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
11939 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
11940 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
11941
11942 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
11943
11944 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
11945 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
11946 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
11947 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
11948
11949 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
11950 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
11951 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
11952 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
11953 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
11954 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
11955 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
11956 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
11957 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
11958
11959 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
11960 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
11961 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
11962
11963 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
11964 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
11965 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
11966
11967 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
11968 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
11969 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
11970 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
11971 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
11972 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
11973
11974 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
11975 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
11976 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
11977 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
11978 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
11979 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
11980 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
11981 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
11982 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
11983 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
11984 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
11985 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
11986
11987 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
11988 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
11989 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
11990 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
11991
11992 def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
11993 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
11994 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
11995 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
11996 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
11997 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
11998 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
11999}
12000
12001static void
12002arm_init_builtins (void)
12003{
12004 if (TARGET_REALLY_IWMMXT)
12005 arm_init_iwmmxt_builtins ();
12006}
12007
12008/* Errors in the source file can cause expand_expr to return const0_rtx
12009 where we expect a vector. To avoid crashing, use one of the vector
12010 clear instructions. */
12011
12012static rtx
12013safe_vector_operand (rtx x, enum machine_mode mode)
12014{
12015 if (x != const0_rtx)
12016 return x;
12017 x = gen_reg_rtx (mode);
12018
12019 emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12020 : gen_rtx_SUBREG (DImode, x, 0)));
12021 return x;
12022}
12023
12024/* Subroutine of arm_expand_builtin to take care of binop insns. */
12025
12026static rtx
12027arm_expand_binop_builtin (enum insn_code icode,
12028 tree arglist, rtx target)
12029{
12030 rtx pat;
12031 tree arg0 = TREE_VALUE (arglist);
12032 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12033 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12034 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12035 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12036 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12037 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12038
12039 if (VECTOR_MODE_P (mode0))
12040 op0 = safe_vector_operand (op0, mode0);
12041 if (VECTOR_MODE_P (mode1))
12042 op1 = safe_vector_operand (op1, mode1);
12043
12044 if (! target
12045 || GET_MODE (target) != tmode
12046 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12047 target = gen_reg_rtx (tmode);
12048
12049 /* In case the insn wants input operands in modes different from
12050 the result, abort. */
12051 if (GET_MODE (op0) != mode0 || GET_MODE (op1) != mode1)
12052 abort ();
12053
12054 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12055 op0 = copy_to_mode_reg (mode0, op0);
12056 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12057 op1 = copy_to_mode_reg (mode1, op1);
12058
12059 pat = GEN_FCN (icode) (target, op0, op1);
12060 if (! pat)
12061 return 0;
12062 emit_insn (pat);
12063 return target;
12064}
12065
12066/* Subroutine of arm_expand_builtin to take care of unop insns. */
12067
12068static rtx
12069arm_expand_unop_builtin (enum insn_code icode,
12070 tree arglist, rtx target, int do_load)
12071{
12072 rtx pat;
12073 tree arg0 = TREE_VALUE (arglist);
12074 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12075 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12076 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12077
12078 if (! target
12079 || GET_MODE (target) != tmode
12080 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12081 target = gen_reg_rtx (tmode);
12082 if (do_load)
12083 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12084 else
12085 {
12086 if (VECTOR_MODE_P (mode0))
12087 op0 = safe_vector_operand (op0, mode0);
12088
12089 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12090 op0 = copy_to_mode_reg (mode0, op0);
12091 }
12092
12093 pat = GEN_FCN (icode) (target, op0);
12094 if (! pat)
12095 return 0;
12096 emit_insn (pat);
12097 return target;
12098}
12099
12100/* Expand an expression EXP that calls a built-in function,
12101 with result going to TARGET if that's convenient
12102 (and in mode MODE if that's convenient).
12103 SUBTARGET may be used as the target for computing one of EXP's operands.
12104 IGNORE is nonzero if the value is to be ignored. */
12105
12106static rtx
12107arm_expand_builtin (tree exp,
12108 rtx target,
12109 rtx subtarget ATTRIBUTE_UNUSED,
12110 enum machine_mode mode ATTRIBUTE_UNUSED,
12111 int ignore ATTRIBUTE_UNUSED)
12112{
12113 const struct builtin_description * d;
12114 enum insn_code icode;
12115 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12116 tree arglist = TREE_OPERAND (exp, 1);
12117 tree arg0;
12118 tree arg1;
12119 tree arg2;
12120 rtx op0;
12121 rtx op1;
12122 rtx op2;
12123 rtx pat;
12124 int fcode = DECL_FUNCTION_CODE (fndecl);
12125 size_t i;
12126 enum machine_mode tmode;
12127 enum machine_mode mode0;
12128 enum machine_mode mode1;
12129 enum machine_mode mode2;
12130
12131 switch (fcode)
12132 {
12133 case ARM_BUILTIN_TEXTRMSB:
12134 case ARM_BUILTIN_TEXTRMUB:
12135 case ARM_BUILTIN_TEXTRMSH:
12136 case ARM_BUILTIN_TEXTRMUH:
12137 case ARM_BUILTIN_TEXTRMSW:
12138 case ARM_BUILTIN_TEXTRMUW:
12139 icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12140 : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12141 : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12142 : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12143 : CODE_FOR_iwmmxt_textrmw);
12144
12145 arg0 = TREE_VALUE (arglist);
12146 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12147 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12148 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12149 tmode = insn_data[icode].operand[0].mode;
12150 mode0 = insn_data[icode].operand[1].mode;
12151 mode1 = insn_data[icode].operand[2].mode;
12152
12153 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12154 op0 = copy_to_mode_reg (mode0, op0);
12155 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12156 {
12157 /* @@@ better error message */
12158 error ("selector must be an immediate");
12159 return gen_reg_rtx (tmode);
12160 }
12161 if (target == 0
12162 || GET_MODE (target) != tmode
12163 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12164 target = gen_reg_rtx (tmode);
12165 pat = GEN_FCN (icode) (target, op0, op1);
12166 if (! pat)
12167 return 0;
12168 emit_insn (pat);
12169 return target;
12170
12171 case ARM_BUILTIN_TINSRB:
12172 case ARM_BUILTIN_TINSRH:
12173 case ARM_BUILTIN_TINSRW:
12174 icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12175 : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12176 : CODE_FOR_iwmmxt_tinsrw);
12177 arg0 = TREE_VALUE (arglist);
12178 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12179 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12180 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12181 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12182 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12183 tmode = insn_data[icode].operand[0].mode;
12184 mode0 = insn_data[icode].operand[1].mode;
12185 mode1 = insn_data[icode].operand[2].mode;
12186 mode2 = insn_data[icode].operand[3].mode;
12187
12188 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12189 op0 = copy_to_mode_reg (mode0, op0);
12190 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12191 op1 = copy_to_mode_reg (mode1, op1);
12192 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12193 {
12194 /* @@@ better error message */
12195 error ("selector must be an immediate");
12196 return const0_rtx;
12197 }
12198 if (target == 0
12199 || GET_MODE (target) != tmode
12200 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12201 target = gen_reg_rtx (tmode);
12202 pat = GEN_FCN (icode) (target, op0, op1, op2);
12203 if (! pat)
12204 return 0;
12205 emit_insn (pat);
12206 return target;
12207
12208 case ARM_BUILTIN_SETWCX:
12209 arg0 = TREE_VALUE (arglist);
12210 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
f07a6b21
BE
12211 op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12212 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12213 emit_insn (gen_iwmmxt_tmcr (op1, op0));
5a9335ef
NC
12214 return 0;
12215
12216 case ARM_BUILTIN_GETWCX:
12217 arg0 = TREE_VALUE (arglist);
12218 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12219 target = gen_reg_rtx (SImode);
12220 emit_insn (gen_iwmmxt_tmrc (target, op0));
12221 return target;
12222
12223 case ARM_BUILTIN_WSHUFH:
12224 icode = CODE_FOR_iwmmxt_wshufh;
12225 arg0 = TREE_VALUE (arglist);
12226 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12227 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12228 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12229 tmode = insn_data[icode].operand[0].mode;
12230 mode1 = insn_data[icode].operand[1].mode;
12231 mode2 = insn_data[icode].operand[2].mode;
12232
12233 if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12234 op0 = copy_to_mode_reg (mode1, op0);
12235 if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12236 {
12237 /* @@@ better error message */
12238 error ("mask must be an immediate");
12239 return const0_rtx;
12240 }
12241 if (target == 0
12242 || GET_MODE (target) != tmode
12243 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12244 target = gen_reg_rtx (tmode);
12245 pat = GEN_FCN (icode) (target, op0, op1);
12246 if (! pat)
12247 return 0;
12248 emit_insn (pat);
12249 return target;
12250
12251 case ARM_BUILTIN_WSADB:
12252 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12253 case ARM_BUILTIN_WSADH:
12254 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12255 case ARM_BUILTIN_WSADBZ:
12256 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12257 case ARM_BUILTIN_WSADHZ:
12258 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12259
12260 /* Several three-argument builtins. */
12261 case ARM_BUILTIN_WMACS:
12262 case ARM_BUILTIN_WMACU:
12263 case ARM_BUILTIN_WALIGN:
12264 case ARM_BUILTIN_TMIA:
12265 case ARM_BUILTIN_TMIAPH:
12266 case ARM_BUILTIN_TMIATT:
12267 case ARM_BUILTIN_TMIATB:
12268 case ARM_BUILTIN_TMIABT:
12269 case ARM_BUILTIN_TMIABB:
12270 icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12271 : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12272 : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12273 : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12274 : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12275 : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12276 : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12277 : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12278 : CODE_FOR_iwmmxt_walign);
12279 arg0 = TREE_VALUE (arglist);
12280 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12281 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12282 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12283 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12284 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12285 tmode = insn_data[icode].operand[0].mode;
12286 mode0 = insn_data[icode].operand[1].mode;
12287 mode1 = insn_data[icode].operand[2].mode;
12288 mode2 = insn_data[icode].operand[3].mode;
12289
12290 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12291 op0 = copy_to_mode_reg (mode0, op0);
12292 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12293 op1 = copy_to_mode_reg (mode1, op1);
12294 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12295 op2 = copy_to_mode_reg (mode2, op2);
12296 if (target == 0
12297 || GET_MODE (target) != tmode
12298 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12299 target = gen_reg_rtx (tmode);
12300 pat = GEN_FCN (icode) (target, op0, op1, op2);
12301 if (! pat)
12302 return 0;
12303 emit_insn (pat);
12304 return target;
12305
12306 case ARM_BUILTIN_WZERO:
12307 target = gen_reg_rtx (DImode);
12308 emit_insn (gen_iwmmxt_clrdi (target));
12309 return target;
12310
12311 default:
12312 break;
12313 }
12314
e97a46ce 12315 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
5a9335ef
NC
12316 if (d->code == (const enum arm_builtins) fcode)
12317 return arm_expand_binop_builtin (d->icode, arglist, target);
12318
e97a46ce 12319 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
5a9335ef
NC
12320 if (d->code == (const enum arm_builtins) fcode)
12321 return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12322
12323 /* @@@ Should really do something sensible here. */
12324 return NULL_RTX;
12325}
12326\f
d5b7b3ae
RE
12327/* Recursively search through all of the blocks in a function
12328 checking to see if any of the variables created in that
12329 function match the RTX called 'orig'. If they do then
12330 replace them with the RTX called 'new'. */
d5b7b3ae 12331static void
e32bac5b 12332replace_symbols_in_block (tree block, rtx orig, rtx new)
d5b7b3ae
RE
12333{
12334 for (; block; block = BLOCK_CHAIN (block))
12335 {
12336 tree sym;
12337
5895f793 12338 if (!TREE_USED (block))
d5b7b3ae
RE
12339 continue;
12340
12341 for (sym = BLOCK_VARS (block); sym; sym = TREE_CHAIN (sym))
12342 {
12343 if ( (DECL_NAME (sym) == 0 && TREE_CODE (sym) != TYPE_DECL)
12344 || DECL_IGNORED_P (sym)
12345 || TREE_CODE (sym) != VAR_DECL
12346 || DECL_EXTERNAL (sym)
5895f793 12347 || !rtx_equal_p (DECL_RTL (sym), orig)
d5b7b3ae
RE
12348 )
12349 continue;
12350
7b8b8ade 12351 SET_DECL_RTL (sym, new);
d5b7b3ae
RE
12352 }
12353
12354 replace_symbols_in_block (BLOCK_SUBBLOCKS (block), orig, new);
12355 }
12356}
12357
1d6e90ac
NC
12358/* Return the number (counting from 0) of
12359 the least significant set bit in MASK. */
12360
e32bac5b
RE
12361inline static int
12362number_of_first_bit_set (int mask)
d5b7b3ae
RE
12363{
12364 int bit;
12365
12366 for (bit = 0;
12367 (mask & (1 << bit)) == 0;
5895f793 12368 ++bit)
d5b7b3ae
RE
12369 continue;
12370
12371 return bit;
12372}
12373
12374/* Generate code to return from a thumb function.
12375 If 'reg_containing_return_addr' is -1, then the return address is
12376 actually on the stack, at the stack pointer. */
12377static void
e32bac5b 12378thumb_exit (FILE *f, int reg_containing_return_addr, rtx eh_ofs)
d5b7b3ae
RE
12379{
12380 unsigned regs_available_for_popping;
12381 unsigned regs_to_pop;
12382 int pops_needed;
12383 unsigned available;
12384 unsigned required;
12385 int mode;
12386 int size;
12387 int restore_a4 = FALSE;
12388
12389 /* Compute the registers we need to pop. */
12390 regs_to_pop = 0;
12391 pops_needed = 0;
12392
12393 /* There is an assumption here, that if eh_ofs is not NULL, the
12394 normal return address will have been pushed. */
12395 if (reg_containing_return_addr == -1 || eh_ofs)
12396 {
12397 /* When we are generating a return for __builtin_eh_return,
12398 reg_containing_return_addr must specify the return regno. */
12399 if (eh_ofs && reg_containing_return_addr == -1)
12400 abort ();
12401
12402 regs_to_pop |= 1 << LR_REGNUM;
5895f793 12403 ++pops_needed;
d5b7b3ae
RE
12404 }
12405
12406 if (TARGET_BACKTRACE)
12407 {
12408 /* Restore the (ARM) frame pointer and stack pointer. */
12409 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12410 pops_needed += 2;
12411 }
12412
12413 /* If there is nothing to pop then just emit the BX instruction and
12414 return. */
12415 if (pops_needed == 0)
12416 {
12417 if (eh_ofs)
12418 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12419
12420 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12421 return;
12422 }
12423 /* Otherwise if we are not supporting interworking and we have not created
12424 a backtrace structure and the function was not entered in ARM mode then
12425 just pop the return address straight into the PC. */
5895f793
RE
12426 else if (!TARGET_INTERWORK
12427 && !TARGET_BACKTRACE
12428 && !is_called_in_ARM_mode (current_function_decl))
d5b7b3ae
RE
12429 {
12430 if (eh_ofs)
12431 {
12432 asm_fprintf (f, "\tadd\t%r, #4\n", SP_REGNUM);
12433 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12434 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12435 }
12436 else
12437 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12438
12439 return;
12440 }
12441
12442 /* Find out how many of the (return) argument registers we can corrupt. */
12443 regs_available_for_popping = 0;
12444
12445 /* If returning via __builtin_eh_return, the bottom three registers
12446 all contain information needed for the return. */
12447 if (eh_ofs)
12448 size = 12;
12449 else
12450 {
12451#ifdef RTX_CODE
12452 /* If we can deduce the registers used from the function's
12453 return value. This is more reliable that examining
12454 regs_ever_live[] because that will be set if the register is
12455 ever used in the function, not just if the register is used
12456 to hold a return value. */
12457
12458 if (current_function_return_rtx != 0)
12459 mode = GET_MODE (current_function_return_rtx);
12460 else
12461#endif
12462 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12463
12464 size = GET_MODE_SIZE (mode);
12465
12466 if (size == 0)
12467 {
12468 /* In a void function we can use any argument register.
12469 In a function that returns a structure on the stack
12470 we can use the second and third argument registers. */
12471 if (mode == VOIDmode)
12472 regs_available_for_popping =
12473 (1 << ARG_REGISTER (1))
12474 | (1 << ARG_REGISTER (2))
12475 | (1 << ARG_REGISTER (3));
12476 else
12477 regs_available_for_popping =
12478 (1 << ARG_REGISTER (2))
12479 | (1 << ARG_REGISTER (3));
12480 }
12481 else if (size <= 4)
12482 regs_available_for_popping =
12483 (1 << ARG_REGISTER (2))
12484 | (1 << ARG_REGISTER (3));
12485 else if (size <= 8)
12486 regs_available_for_popping =
12487 (1 << ARG_REGISTER (3));
12488 }
12489
12490 /* Match registers to be popped with registers into which we pop them. */
12491 for (available = regs_available_for_popping,
12492 required = regs_to_pop;
12493 required != 0 && available != 0;
12494 available &= ~(available & - available),
12495 required &= ~(required & - required))
12496 -- pops_needed;
12497
12498 /* If we have any popping registers left over, remove them. */
12499 if (available > 0)
5895f793 12500 regs_available_for_popping &= ~available;
d5b7b3ae
RE
12501
12502 /* Otherwise if we need another popping register we can use
12503 the fourth argument register. */
12504 else if (pops_needed)
12505 {
12506 /* If we have not found any free argument registers and
12507 reg a4 contains the return address, we must move it. */
12508 if (regs_available_for_popping == 0
12509 && reg_containing_return_addr == LAST_ARG_REGNUM)
12510 {
12511 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12512 reg_containing_return_addr = LR_REGNUM;
12513 }
12514 else if (size > 12)
12515 {
12516 /* Register a4 is being used to hold part of the return value,
12517 but we have dire need of a free, low register. */
12518 restore_a4 = TRUE;
12519
12520 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12521 }
12522
12523 if (reg_containing_return_addr != LAST_ARG_REGNUM)
12524 {
12525 /* The fourth argument register is available. */
12526 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12527
5895f793 12528 --pops_needed;
d5b7b3ae
RE
12529 }
12530 }
12531
12532 /* Pop as many registers as we can. */
980e61bb
DJ
12533 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12534 regs_available_for_popping);
d5b7b3ae
RE
12535
12536 /* Process the registers we popped. */
12537 if (reg_containing_return_addr == -1)
12538 {
12539 /* The return address was popped into the lowest numbered register. */
5895f793 12540 regs_to_pop &= ~(1 << LR_REGNUM);
d5b7b3ae
RE
12541
12542 reg_containing_return_addr =
12543 number_of_first_bit_set (regs_available_for_popping);
12544
12545 /* Remove this register for the mask of available registers, so that
6bc82793 12546 the return address will not be corrupted by further pops. */
5895f793 12547 regs_available_for_popping &= ~(1 << reg_containing_return_addr);
d5b7b3ae
RE
12548 }
12549
12550 /* If we popped other registers then handle them here. */
12551 if (regs_available_for_popping)
12552 {
12553 int frame_pointer;
12554
12555 /* Work out which register currently contains the frame pointer. */
12556 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12557
12558 /* Move it into the correct place. */
12559 asm_fprintf (f, "\tmov\t%r, %r\n",
12560 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12561
12562 /* (Temporarily) remove it from the mask of popped registers. */
5895f793
RE
12563 regs_available_for_popping &= ~(1 << frame_pointer);
12564 regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
d5b7b3ae
RE
12565
12566 if (regs_available_for_popping)
12567 {
12568 int stack_pointer;
12569
12570 /* We popped the stack pointer as well,
12571 find the register that contains it. */
12572 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12573
12574 /* Move it into the stack register. */
12575 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12576
12577 /* At this point we have popped all necessary registers, so
12578 do not worry about restoring regs_available_for_popping
12579 to its correct value:
12580
12581 assert (pops_needed == 0)
12582 assert (regs_available_for_popping == (1 << frame_pointer))
12583 assert (regs_to_pop == (1 << STACK_POINTER)) */
12584 }
12585 else
12586 {
12587 /* Since we have just move the popped value into the frame
12588 pointer, the popping register is available for reuse, and
12589 we know that we still have the stack pointer left to pop. */
12590 regs_available_for_popping |= (1 << frame_pointer);
12591 }
12592 }
12593
12594 /* If we still have registers left on the stack, but we no longer have
12595 any registers into which we can pop them, then we must move the return
12596 address into the link register and make available the register that
12597 contained it. */
12598 if (regs_available_for_popping == 0 && pops_needed > 0)
12599 {
12600 regs_available_for_popping |= 1 << reg_containing_return_addr;
12601
12602 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12603 reg_containing_return_addr);
12604
12605 reg_containing_return_addr = LR_REGNUM;
12606 }
12607
12608 /* If we have registers left on the stack then pop some more.
12609 We know that at most we will want to pop FP and SP. */
12610 if (pops_needed > 0)
12611 {
12612 int popped_into;
12613 int move_to;
12614
980e61bb
DJ
12615 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12616 regs_available_for_popping);
d5b7b3ae
RE
12617
12618 /* We have popped either FP or SP.
12619 Move whichever one it is into the correct register. */
12620 popped_into = number_of_first_bit_set (regs_available_for_popping);
12621 move_to = number_of_first_bit_set (regs_to_pop);
12622
12623 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12624
5895f793 12625 regs_to_pop &= ~(1 << move_to);
d5b7b3ae 12626
5895f793 12627 --pops_needed;
d5b7b3ae
RE
12628 }
12629
12630 /* If we still have not popped everything then we must have only
12631 had one register available to us and we are now popping the SP. */
12632 if (pops_needed > 0)
12633 {
12634 int popped_into;
12635
980e61bb
DJ
12636 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12637 regs_available_for_popping);
d5b7b3ae
RE
12638
12639 popped_into = number_of_first_bit_set (regs_available_for_popping);
12640
12641 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12642 /*
12643 assert (regs_to_pop == (1 << STACK_POINTER))
12644 assert (pops_needed == 1)
12645 */
12646 }
12647
12648 /* If necessary restore the a4 register. */
12649 if (restore_a4)
12650 {
12651 if (reg_containing_return_addr != LR_REGNUM)
12652 {
12653 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12654 reg_containing_return_addr = LR_REGNUM;
12655 }
12656
12657 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12658 }
12659
12660 if (eh_ofs)
12661 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12662
12663 /* Return to caller. */
12664 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12665}
12666
980e61bb
DJ
12667/* Emit code to push or pop registers to or from the stack. F is the
12668 assembly file. MASK is the registers to push or pop. PUSH is
59b9a953 12669 nonzero if we should push, and zero if we should pop. For debugging
980e61bb
DJ
12670 output, if pushing, adjust CFA_OFFSET by the amount of space added
12671 to the stack. REAL_REGS should have the same number of bits set as
12672 MASK, and will be used instead (in the same order) to describe which
12673 registers were saved - this is used to mark the save slots when we
12674 push high registers after moving them to low registers. */
d5b7b3ae 12675static void
980e61bb 12676thumb_pushpop (FILE *f, int mask, int push, int *cfa_offset, int real_regs)
d5b7b3ae
RE
12677{
12678 int regno;
12679 int lo_mask = mask & 0xFF;
980e61bb 12680 int pushed_words = 0;
d5b7b3ae 12681
5895f793 12682 if (lo_mask == 0 && !push && (mask & (1 << 15)))
d5b7b3ae
RE
12683 {
12684 /* Special case. Do not generate a POP PC statement here, do it in
12685 thumb_exit() */
12686 thumb_exit (f, -1, NULL_RTX);
12687 return;
12688 }
12689
12690 fprintf (f, "\t%s\t{", push ? "push" : "pop");
12691
12692 /* Look at the low registers first. */
5895f793 12693 for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
d5b7b3ae
RE
12694 {
12695 if (lo_mask & 1)
12696 {
12697 asm_fprintf (f, "%r", regno);
12698
12699 if ((lo_mask & ~1) != 0)
12700 fprintf (f, ", ");
980e61bb
DJ
12701
12702 pushed_words++;
d5b7b3ae
RE
12703 }
12704 }
12705
12706 if (push && (mask & (1 << LR_REGNUM)))
12707 {
12708 /* Catch pushing the LR. */
12709 if (mask & 0xFF)
12710 fprintf (f, ", ");
12711
12712 asm_fprintf (f, "%r", LR_REGNUM);
980e61bb
DJ
12713
12714 pushed_words++;
d5b7b3ae
RE
12715 }
12716 else if (!push && (mask & (1 << PC_REGNUM)))
12717 {
12718 /* Catch popping the PC. */
12719 if (TARGET_INTERWORK || TARGET_BACKTRACE)
12720 {
12721 /* The PC is never poped directly, instead
12722 it is popped into r3 and then BX is used. */
12723 fprintf (f, "}\n");
12724
12725 thumb_exit (f, -1, NULL_RTX);
12726
12727 return;
12728 }
12729 else
12730 {
12731 if (mask & 0xFF)
12732 fprintf (f, ", ");
12733
12734 asm_fprintf (f, "%r", PC_REGNUM);
12735 }
12736 }
12737
12738 fprintf (f, "}\n");
980e61bb
DJ
12739
12740 if (push && pushed_words && dwarf2out_do_frame ())
12741 {
12742 char *l = dwarf2out_cfi_label ();
12743 int pushed_mask = real_regs;
12744
12745 *cfa_offset += pushed_words * 4;
12746 dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12747
12748 pushed_words = 0;
12749 pushed_mask = real_regs;
12750 for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12751 {
12752 if (pushed_mask & 1)
12753 dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12754 }
12755 }
d5b7b3ae
RE
12756}
12757\f
12758void
e32bac5b 12759thumb_final_prescan_insn (rtx insn)
d5b7b3ae 12760{
d5b7b3ae 12761 if (flag_print_asm_name)
9d98a694
AO
12762 asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12763 INSN_ADDRESSES (INSN_UID (insn)));
d5b7b3ae
RE
12764}
12765
12766int
e32bac5b 12767thumb_shiftable_const (unsigned HOST_WIDE_INT val)
d5b7b3ae
RE
12768{
12769 unsigned HOST_WIDE_INT mask = 0xff;
12770 int i;
12771
12772 if (val == 0) /* XXX */
12773 return 0;
12774
12775 for (i = 0; i < 25; i++)
12776 if ((val & (mask << i)) == val)
12777 return 1;
12778
12779 return 0;
12780}
12781
825dda42 12782/* Returns nonzero if the current function contains,
d5b7b3ae 12783 or might contain a far jump. */
5848830f
PB
12784static int
12785thumb_far_jump_used_p (void)
d5b7b3ae
RE
12786{
12787 rtx insn;
12788
12789 /* This test is only important for leaf functions. */
5895f793 12790 /* assert (!leaf_function_p ()); */
d5b7b3ae
RE
12791
12792 /* If we have already decided that far jumps may be used,
12793 do not bother checking again, and always return true even if
12794 it turns out that they are not being used. Once we have made
12795 the decision that far jumps are present (and that hence the link
12796 register will be pushed onto the stack) we cannot go back on it. */
12797 if (cfun->machine->far_jump_used)
12798 return 1;
12799
12800 /* If this function is not being called from the prologue/epilogue
12801 generation code then it must be being called from the
12802 INITIAL_ELIMINATION_OFFSET macro. */
5848830f 12803 if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
d5b7b3ae
RE
12804 {
12805 /* In this case we know that we are being asked about the elimination
12806 of the arg pointer register. If that register is not being used,
12807 then there are no arguments on the stack, and we do not have to
12808 worry that a far jump might force the prologue to push the link
12809 register, changing the stack offsets. In this case we can just
12810 return false, since the presence of far jumps in the function will
12811 not affect stack offsets.
12812
12813 If the arg pointer is live (or if it was live, but has now been
12814 eliminated and so set to dead) then we do have to test to see if
12815 the function might contain a far jump. This test can lead to some
12816 false negatives, since before reload is completed, then length of
12817 branch instructions is not known, so gcc defaults to returning their
12818 longest length, which in turn sets the far jump attribute to true.
12819
12820 A false negative will not result in bad code being generated, but it
12821 will result in a needless push and pop of the link register. We
5848830f
PB
12822 hope that this does not occur too often.
12823
12824 If we need doubleword stack alignment this could affect the other
12825 elimination offsets so we can't risk getting it wrong. */
d5b7b3ae
RE
12826 if (regs_ever_live [ARG_POINTER_REGNUM])
12827 cfun->machine->arg_pointer_live = 1;
5895f793 12828 else if (!cfun->machine->arg_pointer_live)
d5b7b3ae
RE
12829 return 0;
12830 }
12831
12832 /* Check to see if the function contains a branch
12833 insn with the far jump attribute set. */
12834 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12835 {
12836 if (GET_CODE (insn) == JUMP_INSN
12837 /* Ignore tablejump patterns. */
12838 && GET_CODE (PATTERN (insn)) != ADDR_VEC
12839 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12840 && get_attr_far_jump (insn) == FAR_JUMP_YES
12841 )
12842 {
9a9f7594 12843 /* Record the fact that we have decided that
d5b7b3ae
RE
12844 the function does use far jumps. */
12845 cfun->machine->far_jump_used = 1;
12846 return 1;
12847 }
12848 }
12849
12850 return 0;
12851}
12852
825dda42 12853/* Return nonzero if FUNC must be entered in ARM mode. */
d5b7b3ae 12854int
e32bac5b 12855is_called_in_ARM_mode (tree func)
d5b7b3ae
RE
12856{
12857 if (TREE_CODE (func) != FUNCTION_DECL)
12858 abort ();
12859
12860 /* Ignore the problem about functions whoes address is taken. */
12861 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12862 return TRUE;
12863
12864#ifdef ARM_PE
91d231cb 12865 return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
d5b7b3ae
RE
12866#else
12867 return FALSE;
12868#endif
12869}
12870
d6b4baa4 12871/* The bits which aren't usefully expanded as rtl. */
cd2b33d0 12872const char *
e32bac5b 12873thumb_unexpanded_epilogue (void)
d5b7b3ae
RE
12874{
12875 int regno;
12876 int live_regs_mask = 0;
12877 int high_regs_pushed = 0;
12878 int leaf_function = leaf_function_p ();
12879 int had_to_push_lr;
12880 rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
12881
12882 if (return_used_this_function)
12883 return "";
12884
58e60158
AN
12885 if (IS_NAKED (arm_current_func_type ()))
12886 return "";
12887
d5b7b3ae 12888 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
aeaf4d25 12889 if (THUMB_REG_PUSHED_P (regno))
d5b7b3ae
RE
12890 live_regs_mask |= 1 << regno;
12891
12892 for (regno = 8; regno < 13; regno++)
aeaf4d25
AN
12893 if (THUMB_REG_PUSHED_P (regno))
12894 high_regs_pushed++;
d5b7b3ae
RE
12895
12896 /* The prolog may have pushed some high registers to use as
093354e0 12897 work registers. eg the testsuite file:
d5b7b3ae
RE
12898 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12899 compiles to produce:
12900 push {r4, r5, r6, r7, lr}
12901 mov r7, r9
12902 mov r6, r8
12903 push {r6, r7}
12904 as part of the prolog. We have to undo that pushing here. */
12905
12906 if (high_regs_pushed)
12907 {
12908 int mask = live_regs_mask;
12909 int next_hi_reg;
12910 int size;
12911 int mode;
12912
12913#ifdef RTX_CODE
12914 /* If we can deduce the registers used from the function's return value.
12915 This is more reliable that examining regs_ever_live[] because that
12916 will be set if the register is ever used in the function, not just if
12917 the register is used to hold a return value. */
12918
12919 if (current_function_return_rtx != 0)
12920 mode = GET_MODE (current_function_return_rtx);
12921 else
12922#endif
12923 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12924
12925 size = GET_MODE_SIZE (mode);
12926
12927 /* Unless we are returning a type of size > 12 register r3 is
12928 available. */
12929 if (size < 13)
12930 mask |= 1 << 3;
12931
12932 if (mask == 0)
12933 /* Oh dear! We have no low registers into which we can pop
12934 high registers! */
400500c4
RK
12935 internal_error
12936 ("no low registers available for popping high registers");
d5b7b3ae
RE
12937
12938 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
aeaf4d25 12939 if (THUMB_REG_PUSHED_P (next_hi_reg))
d5b7b3ae
RE
12940 break;
12941
12942 while (high_regs_pushed)
12943 {
12944 /* Find lo register(s) into which the high register(s) can
12945 be popped. */
12946 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12947 {
12948 if (mask & (1 << regno))
12949 high_regs_pushed--;
12950 if (high_regs_pushed == 0)
12951 break;
12952 }
12953
12954 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
12955
d6b4baa4 12956 /* Pop the values into the low register(s). */
980e61bb 12957 thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
d5b7b3ae
RE
12958
12959 /* Move the value(s) into the high registers. */
12960 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12961 {
12962 if (mask & (1 << regno))
12963 {
12964 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
12965 regno);
12966
12967 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
aeaf4d25 12968 if (THUMB_REG_PUSHED_P (next_hi_reg))
d5b7b3ae
RE
12969 break;
12970 }
12971 }
12972 }
12973 }
12974
5895f793 12975 had_to_push_lr = (live_regs_mask || !leaf_function
5848830f 12976 || thumb_far_jump_used_p ());
d5b7b3ae
RE
12977
12978 if (TARGET_BACKTRACE
12979 && ((live_regs_mask & 0xFF) == 0)
12980 && regs_ever_live [LAST_ARG_REGNUM] != 0)
12981 {
12982 /* The stack backtrace structure creation code had to
12983 push R7 in order to get a work register, so we pop
d6b4baa4 12984 it now. */
d5b7b3ae
RE
12985 live_regs_mask |= (1 << LAST_LO_REGNUM);
12986 }
12987
12988 if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
12989 {
12990 if (had_to_push_lr
5895f793
RE
12991 && !is_called_in_ARM_mode (current_function_decl)
12992 && !eh_ofs)
d5b7b3ae
RE
12993 live_regs_mask |= 1 << PC_REGNUM;
12994
12995 /* Either no argument registers were pushed or a backtrace
12996 structure was created which includes an adjusted stack
12997 pointer, so just pop everything. */
12998 if (live_regs_mask)
980e61bb
DJ
12999 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13000 live_regs_mask);
d5b7b3ae
RE
13001
13002 if (eh_ofs)
13003 thumb_exit (asm_out_file, 2, eh_ofs);
13004 /* We have either just popped the return address into the
13005 PC or it is was kept in LR for the entire function or
13006 it is still on the stack because we do not want to
13007 return by doing a pop {pc}. */
13008 else if ((live_regs_mask & (1 << PC_REGNUM)) == 0)
13009 thumb_exit (asm_out_file,
13010 (had_to_push_lr
13011 && is_called_in_ARM_mode (current_function_decl)) ?
13012 -1 : LR_REGNUM, NULL_RTX);
13013 }
13014 else
13015 {
13016 /* Pop everything but the return address. */
5895f793 13017 live_regs_mask &= ~(1 << PC_REGNUM);
d5b7b3ae
RE
13018
13019 if (live_regs_mask)
980e61bb
DJ
13020 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13021 live_regs_mask);
d5b7b3ae
RE
13022
13023 if (had_to_push_lr)
13024 /* Get the return address into a temporary register. */
980e61bb
DJ
13025 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
13026 1 << LAST_ARG_REGNUM);
d5b7b3ae
RE
13027
13028 /* Remove the argument registers that were pushed onto the stack. */
13029 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
13030 SP_REGNUM, SP_REGNUM,
13031 current_function_pretend_args_size);
13032
13033 if (eh_ofs)
13034 thumb_exit (asm_out_file, 2, eh_ofs);
13035 else
13036 thumb_exit (asm_out_file,
13037 had_to_push_lr ? LAST_ARG_REGNUM : LR_REGNUM, NULL_RTX);
13038 }
13039
13040 return "";
13041}
13042
13043/* Functions to save and restore machine-specific function data. */
e2500fed 13044static struct machine_function *
e32bac5b 13045arm_init_machine_status (void)
d5b7b3ae 13046{
e2500fed
GK
13047 struct machine_function *machine;
13048 machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
6d3d9133 13049
e2500fed
GK
13050#if ARM_FT_UNKNOWN != 0
13051 machine->func_type = ARM_FT_UNKNOWN;
6d3d9133 13052#endif
e2500fed 13053 return machine;
f7a80099
NC
13054}
13055
d5b7b3ae
RE
13056/* Return an RTX indicating where the return address to the
13057 calling function can be found. */
13058rtx
e32bac5b 13059arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
d5b7b3ae 13060{
d5b7b3ae
RE
13061 if (count != 0)
13062 return NULL_RTX;
13063
9e2f7ec7
DD
13064 if (TARGET_APCS_32)
13065 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13066 else
d5b7b3ae 13067 {
9e2f7ec7 13068 rtx lr = gen_rtx_AND (Pmode, gen_rtx_REG (Pmode, LR_REGNUM),
d5b7b3ae 13069 GEN_INT (RETURN_ADDR_MASK26));
9e2f7ec7 13070 return get_func_hard_reg_initial_val (cfun, lr);
d5b7b3ae 13071 }
d5b7b3ae
RE
13072}
13073
13074/* Do anything needed before RTL is emitted for each function. */
13075void
e32bac5b 13076arm_init_expanders (void)
d5b7b3ae
RE
13077{
13078 /* Arrange to initialize and mark the machine per-function status. */
13079 init_machine_status = arm_init_machine_status;
d5b7b3ae
RE
13080}
13081
0977774b 13082
5848830f
PB
13083/* Like arm_compute_initial_elimination offset. Simpler because
13084 THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer. */
0977774b 13085
5848830f
PB
13086HOST_WIDE_INT
13087thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13088{
13089 arm_stack_offsets *offsets;
0977774b 13090
5848830f 13091 offsets = arm_get_frame_offsets ();
0977774b 13092
5848830f 13093 switch (from)
0977774b 13094 {
5848830f
PB
13095 case ARG_POINTER_REGNUM:
13096 switch (to)
13097 {
13098 case STACK_POINTER_REGNUM:
13099 return offsets->outgoing_args - offsets->saved_args;
0977774b 13100
5848830f
PB
13101 case FRAME_POINTER_REGNUM:
13102 return offsets->soft_frame - offsets->saved_args;
0977774b 13103
5848830f
PB
13104 case THUMB_HARD_FRAME_POINTER_REGNUM:
13105 case ARM_HARD_FRAME_POINTER_REGNUM:
13106 return offsets->saved_regs - offsets->saved_args;
0977774b 13107
5848830f
PB
13108 default:
13109 abort();
13110 }
13111 break;
0977774b 13112
5848830f
PB
13113 case FRAME_POINTER_REGNUM:
13114 switch (to)
13115 {
13116 case STACK_POINTER_REGNUM:
13117 return offsets->outgoing_args - offsets->soft_frame;
0977774b 13118
5848830f
PB
13119 case THUMB_HARD_FRAME_POINTER_REGNUM:
13120 case ARM_HARD_FRAME_POINTER_REGNUM:
13121 return offsets->saved_regs - offsets->soft_frame;
0977774b 13122
5848830f
PB
13123 default:
13124 abort();
13125 }
13126 break;
0977774b 13127
5848830f
PB
13128 default:
13129 abort ();
13130 }
0977774b
JT
13131}
13132
5848830f 13133
d5b7b3ae
RE
13134/* Generate the rest of a function's prologue. */
13135void
e32bac5b 13136thumb_expand_prologue (void)
d5b7b3ae 13137{
980e61bb
DJ
13138 rtx insn, dwarf;
13139
5848830f
PB
13140 HOST_WIDE_INT amount;
13141 arm_stack_offsets *offsets;
6d3d9133
NC
13142 unsigned long func_type;
13143
13144 func_type = arm_current_func_type ();
d5b7b3ae
RE
13145
13146 /* Naked functions don't have prologues. */
6d3d9133 13147 if (IS_NAKED (func_type))
d5b7b3ae
RE
13148 return;
13149
6d3d9133
NC
13150 if (IS_INTERRUPT (func_type))
13151 {
c725bd79 13152 error ("interrupt Service Routines cannot be coded in Thumb mode");
6d3d9133
NC
13153 return;
13154 }
13155
5848830f
PB
13156 offsets = arm_get_frame_offsets ();
13157
d5b7b3ae 13158 if (frame_pointer_needed)
980e61bb 13159 {
5848830f
PB
13160 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
13161 stack_pointer_rtx));
980e61bb
DJ
13162 RTX_FRAME_RELATED_P (insn) = 1;
13163 }
d5b7b3ae 13164
5848830f 13165 amount = offsets->outgoing_args - offsets->saved_regs;
d5b7b3ae
RE
13166 if (amount)
13167 {
d5b7b3ae 13168 if (amount < 512)
980e61bb
DJ
13169 {
13170 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13171 GEN_INT (- amount)));
13172 RTX_FRAME_RELATED_P (insn) = 1;
13173 }
d5b7b3ae
RE
13174 else
13175 {
13176 int regno;
13177 rtx reg;
13178
13179 /* The stack decrement is too big for an immediate value in a single
13180 insn. In theory we could issue multiple subtracts, but after
13181 three of them it becomes more space efficient to place the full
13182 value in the constant pool and load into a register. (Also the
13183 ARM debugger really likes to see only one stack decrement per
13184 function). So instead we look for a scratch register into which
13185 we can load the decrement, and then we subtract this from the
13186 stack pointer. Unfortunately on the thumb the only available
13187 scratch registers are the argument registers, and we cannot use
13188 these as they may hold arguments to the function. Instead we
13189 attempt to locate a call preserved register which is used by this
13190 function. If we can find one, then we know that it will have
13191 been pushed at the start of the prologue and so we can corrupt
13192 it now. */
13193 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
aeaf4d25 13194 if (THUMB_REG_PUSHED_P (regno)
5895f793
RE
13195 && !(frame_pointer_needed
13196 && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
d5b7b3ae
RE
13197 break;
13198
aeaf4d25 13199 if (regno > LAST_LO_REGNUM) /* Very unlikely. */
d5b7b3ae 13200 {
f1c25d3b 13201 rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
d5b7b3ae 13202
6bc82793 13203 /* Choose an arbitrary, non-argument low register. */
f1c25d3b 13204 reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
d5b7b3ae
RE
13205
13206 /* Save it by copying it into a high, scratch register. */
c14a3a45
NC
13207 emit_insn (gen_movsi (spare, reg));
13208 /* Add a USE to stop propagate_one_insn() from barfing. */
6bacc7b0 13209 emit_insn (gen_prologue_use (spare));
d5b7b3ae
RE
13210
13211 /* Decrement the stack. */
1d6e90ac 13212 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
980e61bb
DJ
13213 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13214 stack_pointer_rtx, reg));
13215 RTX_FRAME_RELATED_P (insn) = 1;
13216 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13217 plus_constant (stack_pointer_rtx,
13218 GEN_INT (- amount)));
13219 RTX_FRAME_RELATED_P (dwarf) = 1;
13220 REG_NOTES (insn)
13221 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13222 REG_NOTES (insn));
d5b7b3ae
RE
13223
13224 /* Restore the low register's original value. */
13225 emit_insn (gen_movsi (reg, spare));
13226
13227 /* Emit a USE of the restored scratch register, so that flow
13228 analysis will not consider the restore redundant. The
13229 register won't be used again in this function and isn't
13230 restored by the epilogue. */
6bacc7b0 13231 emit_insn (gen_prologue_use (reg));
d5b7b3ae
RE
13232 }
13233 else
13234 {
f1c25d3b 13235 reg = gen_rtx_REG (SImode, regno);
d5b7b3ae 13236
1d6e90ac 13237 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
980e61bb
DJ
13238
13239 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13240 stack_pointer_rtx, reg));
13241 RTX_FRAME_RELATED_P (insn) = 1;
13242 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13243 plus_constant (stack_pointer_rtx,
13244 GEN_INT (- amount)));
13245 RTX_FRAME_RELATED_P (dwarf) = 1;
13246 REG_NOTES (insn)
13247 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13248 REG_NOTES (insn));
d5b7b3ae
RE
13249 }
13250 }
983e6484
PB
13251 /* If the frame pointer is needed, emit a special barrier that
13252 will prevent the scheduler from moving stores to the frame
13253 before the stack adjustment. */
13254 if (frame_pointer_needed)
13255 emit_insn (gen_stack_tie (stack_pointer_rtx,
13256 hard_frame_pointer_rtx));
d5b7b3ae
RE
13257 }
13258
70f4f91c 13259 if (current_function_profile || TARGET_NO_SCHED_PRO)
d5b7b3ae
RE
13260 emit_insn (gen_blockage ());
13261}
13262
13263void
e32bac5b 13264thumb_expand_epilogue (void)
d5b7b3ae 13265{
5848830f
PB
13266 HOST_WIDE_INT amount;
13267 arm_stack_offsets *offsets;
defc0463
RE
13268 int regno;
13269
6d3d9133
NC
13270 /* Naked functions don't have prologues. */
13271 if (IS_NAKED (arm_current_func_type ()))
d5b7b3ae
RE
13272 return;
13273
5848830f
PB
13274 offsets = arm_get_frame_offsets ();
13275 amount = offsets->outgoing_args - offsets->saved_regs;
13276
d5b7b3ae
RE
13277 if (frame_pointer_needed)
13278 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13279 else if (amount)
13280 {
d5b7b3ae
RE
13281 if (amount < 512)
13282 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13283 GEN_INT (amount)));
13284 else
13285 {
13286 /* r3 is always free in the epilogue. */
f1c25d3b 13287 rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
d5b7b3ae
RE
13288
13289 emit_insn (gen_movsi (reg, GEN_INT (amount)));
13290 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13291 }
13292 }
13293
13294 /* Emit a USE (stack_pointer_rtx), so that
13295 the stack adjustment will not be deleted. */
6bacc7b0 13296 emit_insn (gen_prologue_use (stack_pointer_rtx));
d5b7b3ae 13297
70f4f91c 13298 if (current_function_profile || TARGET_NO_SCHED_PRO)
d5b7b3ae 13299 emit_insn (gen_blockage ());
defc0463
RE
13300
13301 /* Emit a clobber for each insn that will be restored in the epilogue,
13302 so that flow2 will get register lifetimes correct. */
13303 for (regno = 0; regno < 13; regno++)
13304 if (regs_ever_live[regno] && !call_used_regs[regno])
13305 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13306
13307 if (! regs_ever_live[LR_REGNUM])
13308 emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
d5b7b3ae
RE
13309}
13310
08c148a8 13311static void
e32bac5b 13312thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
d5b7b3ae
RE
13313{
13314 int live_regs_mask = 0;
13315 int high_regs_pushed = 0;
980e61bb 13316 int cfa_offset = 0;
d5b7b3ae
RE
13317 int regno;
13318
6d3d9133 13319 if (IS_NAKED (arm_current_func_type ()))
d5b7b3ae
RE
13320 return;
13321
13322 if (is_called_in_ARM_mode (current_function_decl))
13323 {
13324 const char * name;
13325
13326 if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
13327 abort ();
13328 if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
13329 abort ();
13330 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
13331
13332 /* Generate code sequence to switch us into Thumb mode. */
13333 /* The .code 32 directive has already been emitted by
6d77b53e 13334 ASM_DECLARE_FUNCTION_NAME. */
d5b7b3ae
RE
13335 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13336 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13337
13338 /* Generate a label, so that the debugger will notice the
13339 change in instruction sets. This label is also used by
13340 the assembler to bypass the ARM code when this function
13341 is called from a Thumb encoded function elsewhere in the
13342 same file. Hence the definition of STUB_NAME here must
d6b4baa4 13343 agree with the definition in gas/config/tc-arm.c. */
d5b7b3ae
RE
13344
13345#define STUB_NAME ".real_start_of"
13346
761c70aa 13347 fprintf (f, "\t.code\t16\n");
d5b7b3ae
RE
13348#ifdef ARM_PE
13349 if (arm_dllexport_name_p (name))
e5951263 13350 name = arm_strip_name_encoding (name);
d5b7b3ae
RE
13351#endif
13352 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
761c70aa 13353 fprintf (f, "\t.thumb_func\n");
d5b7b3ae
RE
13354 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13355 }
13356
d5b7b3ae
RE
13357 if (current_function_pretend_args_size)
13358 {
3cb66fd7 13359 if (cfun->machine->uses_anonymous_args)
d5b7b3ae
RE
13360 {
13361 int num_pushes;
13362
761c70aa 13363 fprintf (f, "\tpush\t{");
d5b7b3ae 13364
e9d7b180 13365 num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
d5b7b3ae
RE
13366
13367 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13368 regno <= LAST_ARG_REGNUM;
5895f793 13369 regno++)
d5b7b3ae
RE
13370 asm_fprintf (f, "%r%s", regno,
13371 regno == LAST_ARG_REGNUM ? "" : ", ");
13372
761c70aa 13373 fprintf (f, "}\n");
d5b7b3ae
RE
13374 }
13375 else
13376 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13377 SP_REGNUM, SP_REGNUM,
13378 current_function_pretend_args_size);
980e61bb
DJ
13379
13380 /* We don't need to record the stores for unwinding (would it
13381 help the debugger any if we did?), but record the change in
13382 the stack pointer. */
13383 if (dwarf2out_do_frame ())
13384 {
13385 char *l = dwarf2out_cfi_label ();
13386 cfa_offset = cfa_offset + current_function_pretend_args_size;
13387 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13388 }
d5b7b3ae
RE
13389 }
13390
5895f793 13391 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
aeaf4d25 13392 if (THUMB_REG_PUSHED_P (regno))
d5b7b3ae
RE
13393 live_regs_mask |= 1 << regno;
13394
5848830f 13395 if (live_regs_mask || !leaf_function_p () || thumb_far_jump_used_p ())
d5b7b3ae
RE
13396 live_regs_mask |= 1 << LR_REGNUM;
13397
13398 if (TARGET_BACKTRACE)
13399 {
13400 int offset;
13401 int work_register = 0;
13402 int wr;
13403
13404 /* We have been asked to create a stack backtrace structure.
13405 The code looks like this:
13406
13407 0 .align 2
13408 0 func:
13409 0 sub SP, #16 Reserve space for 4 registers.
13410 2 push {R7} Get a work register.
13411 4 add R7, SP, #20 Get the stack pointer before the push.
13412 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
13413 8 mov R7, PC Get hold of the start of this code plus 12.
13414 10 str R7, [SP, #16] Store it.
13415 12 mov R7, FP Get hold of the current frame pointer.
13416 14 str R7, [SP, #4] Store it.
13417 16 mov R7, LR Get hold of the current return address.
13418 18 str R7, [SP, #12] Store it.
13419 20 add R7, SP, #16 Point at the start of the backtrace structure.
13420 22 mov FP, R7 Put this value into the frame pointer. */
13421
13422 if ((live_regs_mask & 0xFF) == 0)
13423 {
13424 /* See if the a4 register is free. */
13425
13426 if (regs_ever_live [LAST_ARG_REGNUM] == 0)
13427 work_register = LAST_ARG_REGNUM;
d6b4baa4 13428 else /* We must push a register of our own. */
d5b7b3ae
RE
13429 live_regs_mask |= (1 << LAST_LO_REGNUM);
13430 }
13431
13432 if (work_register == 0)
13433 {
13434 /* Select a register from the list that will be pushed to
13435 use as our work register. */
13436 for (work_register = (LAST_LO_REGNUM + 1); work_register--;)
13437 if ((1 << work_register) & live_regs_mask)
13438 break;
13439 }
13440
13441 asm_fprintf
13442 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13443 SP_REGNUM, SP_REGNUM);
980e61bb
DJ
13444
13445 if (dwarf2out_do_frame ())
13446 {
13447 char *l = dwarf2out_cfi_label ();
13448 cfa_offset = cfa_offset + 16;
13449 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13450 }
13451
d5b7b3ae 13452 if (live_regs_mask)
980e61bb 13453 thumb_pushpop (f, live_regs_mask, 1, &cfa_offset, live_regs_mask);
d5b7b3ae
RE
13454
13455 for (offset = 0, wr = 1 << 15; wr != 0; wr >>= 1)
13456 if (wr & live_regs_mask)
13457 offset += 4;
13458
13459 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13460 offset + 16 + current_function_pretend_args_size);
13461
13462 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13463 offset + 4);
13464
13465 /* Make sure that the instruction fetching the PC is in the right place
13466 to calculate "start of backtrace creation code + 12". */
13467 if (live_regs_mask)
13468 {
13469 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13470 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13471 offset + 12);
13472 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13473 ARM_HARD_FRAME_POINTER_REGNUM);
13474 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13475 offset);
13476 }
13477 else
13478 {
13479 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13480 ARM_HARD_FRAME_POINTER_REGNUM);
13481 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13482 offset);
13483 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13484 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13485 offset + 12);
13486 }
13487
13488 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13489 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13490 offset + 8);
13491 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13492 offset + 12);
13493 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13494 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13495 }
13496 else if (live_regs_mask)
980e61bb 13497 thumb_pushpop (f, live_regs_mask, 1, &cfa_offset, live_regs_mask);
d5b7b3ae
RE
13498
13499 for (regno = 8; regno < 13; regno++)
e26053d1
NC
13500 if (THUMB_REG_PUSHED_P (regno))
13501 high_regs_pushed++;
d5b7b3ae
RE
13502
13503 if (high_regs_pushed)
13504 {
13505 int pushable_regs = 0;
13506 int mask = live_regs_mask & 0xff;
13507 int next_hi_reg;
13508
13509 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
e26053d1
NC
13510 if (THUMB_REG_PUSHED_P (next_hi_reg))
13511 break;
d5b7b3ae
RE
13512
13513 pushable_regs = mask;
13514
13515 if (pushable_regs == 0)
13516 {
13517 /* Desperation time -- this probably will never happen. */
aeaf4d25 13518 if (THUMB_REG_PUSHED_P (LAST_ARG_REGNUM))
d5b7b3ae
RE
13519 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, LAST_ARG_REGNUM);
13520 mask = 1 << LAST_ARG_REGNUM;
13521 }
13522
13523 while (high_regs_pushed > 0)
13524 {
980e61bb
DJ
13525 int real_regs_mask = 0;
13526
d5b7b3ae
RE
13527 for (regno = LAST_LO_REGNUM; regno >= 0; regno--)
13528 {
13529 if (mask & (1 << regno))
13530 {
13531 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13532
5895f793 13533 high_regs_pushed--;
980e61bb 13534 real_regs_mask |= (1 << next_hi_reg);
d5b7b3ae
RE
13535
13536 if (high_regs_pushed)
aeaf4d25
AN
13537 {
13538 for (next_hi_reg--; next_hi_reg > LAST_LO_REGNUM;
13539 next_hi_reg--)
13540 if (THUMB_REG_PUSHED_P (next_hi_reg))
d5b7b3ae 13541 break;
aeaf4d25 13542 }
d5b7b3ae
RE
13543 else
13544 {
5895f793 13545 mask &= ~((1 << regno) - 1);
d5b7b3ae
RE
13546 break;
13547 }
13548 }
13549 }
980e61bb
DJ
13550
13551 thumb_pushpop (f, mask, 1, &cfa_offset, real_regs_mask);
d5b7b3ae
RE
13552 }
13553
13554 if (pushable_regs == 0
aeaf4d25 13555 && (THUMB_REG_PUSHED_P (LAST_ARG_REGNUM)))
d5b7b3ae
RE
13556 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
13557 }
13558}
13559
13560/* Handle the case of a double word load into a low register from
13561 a computed memory address. The computed address may involve a
13562 register which is overwritten by the load. */
cd2b33d0 13563const char *
e32bac5b 13564thumb_load_double_from_address (rtx *operands)
d5b7b3ae
RE
13565{
13566 rtx addr;
13567 rtx base;
13568 rtx offset;
13569 rtx arg1;
13570 rtx arg2;
13571
13572 if (GET_CODE (operands[0]) != REG)
400500c4 13573 abort ();
d5b7b3ae
RE
13574
13575 if (GET_CODE (operands[1]) != MEM)
400500c4 13576 abort ();
d5b7b3ae
RE
13577
13578 /* Get the memory address. */
13579 addr = XEXP (operands[1], 0);
13580
13581 /* Work out how the memory address is computed. */
13582 switch (GET_CODE (addr))
13583 {
13584 case REG:
f1c25d3b
KH
13585 operands[2] = gen_rtx_MEM (SImode,
13586 plus_constant (XEXP (operands[1], 0), 4));
d5b7b3ae
RE
13587
13588 if (REGNO (operands[0]) == REGNO (addr))
13589 {
13590 output_asm_insn ("ldr\t%H0, %2", operands);
13591 output_asm_insn ("ldr\t%0, %1", operands);
13592 }
13593 else
13594 {
13595 output_asm_insn ("ldr\t%0, %1", operands);
13596 output_asm_insn ("ldr\t%H0, %2", operands);
13597 }
13598 break;
13599
13600 case CONST:
13601 /* Compute <address> + 4 for the high order load. */
f1c25d3b
KH
13602 operands[2] = gen_rtx_MEM (SImode,
13603 plus_constant (XEXP (operands[1], 0), 4));
d5b7b3ae
RE
13604
13605 output_asm_insn ("ldr\t%0, %1", operands);
13606 output_asm_insn ("ldr\t%H0, %2", operands);
13607 break;
13608
13609 case PLUS:
13610 arg1 = XEXP (addr, 0);
13611 arg2 = XEXP (addr, 1);
13612
13613 if (CONSTANT_P (arg1))
13614 base = arg2, offset = arg1;
13615 else
13616 base = arg1, offset = arg2;
13617
13618 if (GET_CODE (base) != REG)
400500c4 13619 abort ();
d5b7b3ae
RE
13620
13621 /* Catch the case of <address> = <reg> + <reg> */
13622 if (GET_CODE (offset) == REG)
13623 {
13624 int reg_offset = REGNO (offset);
13625 int reg_base = REGNO (base);
13626 int reg_dest = REGNO (operands[0]);
13627
13628 /* Add the base and offset registers together into the
13629 higher destination register. */
13630 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13631 reg_dest + 1, reg_base, reg_offset);
13632
13633 /* Load the lower destination register from the address in
13634 the higher destination register. */
13635 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13636 reg_dest, reg_dest + 1);
13637
13638 /* Load the higher destination register from its own address
13639 plus 4. */
13640 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13641 reg_dest + 1, reg_dest + 1);
13642 }
13643 else
13644 {
13645 /* Compute <address> + 4 for the high order load. */
f1c25d3b
KH
13646 operands[2] = gen_rtx_MEM (SImode,
13647 plus_constant (XEXP (operands[1], 0), 4));
d5b7b3ae
RE
13648
13649 /* If the computed address is held in the low order register
13650 then load the high order register first, otherwise always
13651 load the low order register first. */
13652 if (REGNO (operands[0]) == REGNO (base))
13653 {
13654 output_asm_insn ("ldr\t%H0, %2", operands);
13655 output_asm_insn ("ldr\t%0, %1", operands);
13656 }
13657 else
13658 {
13659 output_asm_insn ("ldr\t%0, %1", operands);
13660 output_asm_insn ("ldr\t%H0, %2", operands);
13661 }
13662 }
13663 break;
13664
13665 case LABEL_REF:
13666 /* With no registers to worry about we can just load the value
13667 directly. */
f1c25d3b
KH
13668 operands[2] = gen_rtx_MEM (SImode,
13669 plus_constant (XEXP (operands[1], 0), 4));
d5b7b3ae
RE
13670
13671 output_asm_insn ("ldr\t%H0, %2", operands);
13672 output_asm_insn ("ldr\t%0, %1", operands);
13673 break;
13674
13675 default:
400500c4 13676 abort ();
d5b7b3ae
RE
13677 break;
13678 }
13679
13680 return "";
13681}
13682
cd2b33d0 13683const char *
e32bac5b 13684thumb_output_move_mem_multiple (int n, rtx *operands)
d5b7b3ae
RE
13685{
13686 rtx tmp;
13687
13688 switch (n)
13689 {
13690 case 2:
ca356f3a 13691 if (REGNO (operands[4]) > REGNO (operands[5]))
d5b7b3ae 13692 {
ca356f3a
RE
13693 tmp = operands[4];
13694 operands[4] = operands[5];
13695 operands[5] = tmp;
d5b7b3ae 13696 }
ca356f3a
RE
13697 output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13698 output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
d5b7b3ae
RE
13699 break;
13700
13701 case 3:
ca356f3a 13702 if (REGNO (operands[4]) > REGNO (operands[5]))
d5b7b3ae 13703 {
ca356f3a
RE
13704 tmp = operands[4];
13705 operands[4] = operands[5];
13706 operands[5] = tmp;
d5b7b3ae 13707 }
ca356f3a 13708 if (REGNO (operands[5]) > REGNO (operands[6]))
d5b7b3ae 13709 {
ca356f3a
RE
13710 tmp = operands[5];
13711 operands[5] = operands[6];
13712 operands[6] = tmp;
d5b7b3ae 13713 }
ca356f3a 13714 if (REGNO (operands[4]) > REGNO (operands[5]))
d5b7b3ae 13715 {
ca356f3a
RE
13716 tmp = operands[4];
13717 operands[4] = operands[5];
13718 operands[5] = tmp;
d5b7b3ae
RE
13719 }
13720
ca356f3a
RE
13721 output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13722 output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
d5b7b3ae
RE
13723 break;
13724
13725 default:
13726 abort ();
13727 }
13728
13729 return "";
13730}
13731
1d6e90ac 13732/* Routines for generating rtl. */
d5b7b3ae 13733void
e32bac5b 13734thumb_expand_movstrqi (rtx *operands)
d5b7b3ae
RE
13735{
13736 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13737 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13738 HOST_WIDE_INT len = INTVAL (operands[2]);
13739 HOST_WIDE_INT offset = 0;
13740
13741 while (len >= 12)
13742 {
ca356f3a 13743 emit_insn (gen_movmem12b (out, in, out, in));
d5b7b3ae
RE
13744 len -= 12;
13745 }
13746
13747 if (len >= 8)
13748 {
ca356f3a 13749 emit_insn (gen_movmem8b (out, in, out, in));
d5b7b3ae
RE
13750 len -= 8;
13751 }
13752
13753 if (len >= 4)
13754 {
13755 rtx reg = gen_reg_rtx (SImode);
f1c25d3b
KH
13756 emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13757 emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
d5b7b3ae
RE
13758 len -= 4;
13759 offset += 4;
13760 }
13761
13762 if (len >= 2)
13763 {
13764 rtx reg = gen_reg_rtx (HImode);
f1c25d3b
KH
13765 emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
13766 plus_constant (in, offset))));
13767 emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
d5b7b3ae
RE
13768 reg));
13769 len -= 2;
13770 offset += 2;
13771 }
13772
13773 if (len)
13774 {
13775 rtx reg = gen_reg_rtx (QImode);
f1c25d3b
KH
13776 emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13777 plus_constant (in, offset))));
13778 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
d5b7b3ae
RE
13779 reg));
13780 }
13781}
13782
13783int
e32bac5b 13784thumb_cmp_operand (rtx op, enum machine_mode mode)
d5b7b3ae
RE
13785{
13786 return ((GET_CODE (op) == CONST_INT
c769a35d
RE
13787 && INTVAL (op) < 256
13788 && INTVAL (op) >= 0)
defc0463 13789 || s_register_operand (op, mode));
d5b7b3ae
RE
13790}
13791
c769a35d
RE
13792int
13793thumb_cmpneg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
13794{
13795 return (GET_CODE (op) == CONST_INT
13796 && INTVAL (op) < 0
13797 && INTVAL (op) > -256);
13798}
13799
defc0463
RE
13800/* Return TRUE if a result can be stored in OP without clobbering the
13801 condition code register. Prior to reload we only accept a
13802 register. After reload we have to be able to handle memory as
13803 well, since a pseudo may not get a hard reg and reload cannot
13804 handle output-reloads on jump insns.
d5b7b3ae 13805
defc0463
RE
13806 We could possibly handle mem before reload as well, but that might
13807 complicate things with the need to handle increment
13808 side-effects. */
d5b7b3ae 13809
defc0463
RE
13810int
13811thumb_cbrch_target_operand (rtx op, enum machine_mode mode)
13812{
13813 return (s_register_operand (op, mode)
13814 || ((reload_in_progress || reload_completed)
13815 && memory_operand (op, mode)));
d5b7b3ae
RE
13816}
13817
13818/* Handle storing a half-word to memory during reload. */
13819void
e32bac5b 13820thumb_reload_out_hi (rtx *operands)
d5b7b3ae
RE
13821{
13822 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13823}
13824
e32bac5b 13825/* Handle reading a half-word from memory during reload. */
d5b7b3ae 13826void
e32bac5b 13827thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
d5b7b3ae
RE
13828{
13829 abort ();
13830}
13831
c27ba912
DM
13832/* Return the length of a function name prefix
13833 that starts with the character 'c'. */
13834static int
e32bac5b 13835arm_get_strip_length (int c)
c27ba912
DM
13836{
13837 switch (c)
13838 {
13839 ARM_NAME_ENCODING_LENGTHS
13840 default: return 0;
13841 }
13842}
13843
13844/* Return a pointer to a function's name with any
13845 and all prefix encodings stripped from it. */
13846const char *
e32bac5b 13847arm_strip_name_encoding (const char *name)
c27ba912
DM
13848{
13849 int skip;
13850
13851 while ((skip = arm_get_strip_length (* name)))
13852 name += skip;
13853
13854 return name;
13855}
13856
e1944073
KW
13857/* If there is a '*' anywhere in the name's prefix, then
13858 emit the stripped name verbatim, otherwise prepend an
13859 underscore if leading underscores are being used. */
e1944073 13860void
e32bac5b 13861arm_asm_output_labelref (FILE *stream, const char *name)
e1944073
KW
13862{
13863 int skip;
13864 int verbatim = 0;
13865
13866 while ((skip = arm_get_strip_length (* name)))
13867 {
13868 verbatim |= (*name == '*');
13869 name += skip;
13870 }
13871
13872 if (verbatim)
13873 fputs (name, stream);
13874 else
13875 asm_fprintf (stream, "%U%s", name);
13876}
13877
e2500fed
GK
13878rtx aof_pic_label;
13879
2b835d68 13880#ifdef AOF_ASSEMBLER
6354dc9b 13881/* Special functions only needed when producing AOF syntax assembler. */
2b835d68 13882
32de079a
RE
13883struct pic_chain
13884{
62b10bbc 13885 struct pic_chain * next;
5f37d07c 13886 const char * symname;
32de079a
RE
13887};
13888
62b10bbc 13889static struct pic_chain * aof_pic_chain = NULL;
32de079a
RE
13890
13891rtx
e32bac5b 13892aof_pic_entry (rtx x)
32de079a 13893{
62b10bbc 13894 struct pic_chain ** chainp;
32de079a
RE
13895 int offset;
13896
13897 if (aof_pic_label == NULL_RTX)
13898 {
43cffd11 13899 aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
32de079a
RE
13900 }
13901
13902 for (offset = 0, chainp = &aof_pic_chain; *chainp;
13903 offset += 4, chainp = &(*chainp)->next)
13904 if ((*chainp)->symname == XSTR (x, 0))
13905 return plus_constant (aof_pic_label, offset);
13906
13907 *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13908 (*chainp)->next = NULL;
13909 (*chainp)->symname = XSTR (x, 0);
13910 return plus_constant (aof_pic_label, offset);
13911}
13912
13913void
e32bac5b 13914aof_dump_pic_table (FILE *f)
32de079a 13915{
62b10bbc 13916 struct pic_chain * chain;
32de079a
RE
13917
13918 if (aof_pic_chain == NULL)
13919 return;
13920
dd18ae56
NC
13921 asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13922 PIC_OFFSET_TABLE_REGNUM,
13923 PIC_OFFSET_TABLE_REGNUM);
32de079a
RE
13924 fputs ("|x$adcons|\n", f);
13925
13926 for (chain = aof_pic_chain; chain; chain = chain->next)
13927 {
13928 fputs ("\tDCD\t", f);
13929 assemble_name (f, chain->symname);
13930 fputs ("\n", f);
13931 }
13932}
13933
2b835d68
RE
13934int arm_text_section_count = 1;
13935
13936char *
e32bac5b 13937aof_text_section (void )
2b835d68
RE
13938{
13939 static char buf[100];
2b835d68
RE
13940 sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
13941 arm_text_section_count++);
13942 if (flag_pic)
13943 strcat (buf, ", PIC, REENTRANT");
13944 return buf;
13945}
13946
13947static int arm_data_section_count = 1;
13948
13949char *
e32bac5b 13950aof_data_section (void)
2b835d68
RE
13951{
13952 static char buf[100];
13953 sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
13954 return buf;
13955}
13956
13957/* The AOF assembler is religiously strict about declarations of
13958 imported and exported symbols, so that it is impossible to declare
956d6950 13959 a function as imported near the beginning of the file, and then to
2b835d68
RE
13960 export it later on. It is, however, possible to delay the decision
13961 until all the functions in the file have been compiled. To get
13962 around this, we maintain a list of the imports and exports, and
13963 delete from it any that are subsequently defined. At the end of
13964 compilation we spit the remainder of the list out before the END
13965 directive. */
13966
13967struct import
13968{
62b10bbc 13969 struct import * next;
5f37d07c 13970 const char * name;
2b835d68
RE
13971};
13972
62b10bbc 13973static struct import * imports_list = NULL;
2b835d68
RE
13974
13975void
e32bac5b 13976aof_add_import (const char *name)
2b835d68 13977{
62b10bbc 13978 struct import * new;
2b835d68
RE
13979
13980 for (new = imports_list; new; new = new->next)
13981 if (new->name == name)
13982 return;
13983
13984 new = (struct import *) xmalloc (sizeof (struct import));
13985 new->next = imports_list;
13986 imports_list = new;
13987 new->name = name;
13988}
13989
13990void
e32bac5b 13991aof_delete_import (const char *name)
2b835d68 13992{
62b10bbc 13993 struct import ** old;
2b835d68
RE
13994
13995 for (old = &imports_list; *old; old = & (*old)->next)
13996 {
13997 if ((*old)->name == name)
13998 {
13999 *old = (*old)->next;
14000 return;
14001 }
14002 }
14003}
14004
14005int arm_main_function = 0;
14006
a5fe455b 14007static void
e32bac5b 14008aof_dump_imports (FILE *f)
2b835d68
RE
14009{
14010 /* The AOF assembler needs this to cause the startup code to be extracted
14011 from the library. Brining in __main causes the whole thing to work
14012 automagically. */
14013 if (arm_main_function)
14014 {
14015 text_section ();
14016 fputs ("\tIMPORT __main\n", f);
14017 fputs ("\tDCD __main\n", f);
14018 }
14019
14020 /* Now dump the remaining imports. */
14021 while (imports_list)
14022 {
14023 fprintf (f, "\tIMPORT\t");
14024 assemble_name (f, imports_list->name);
14025 fputc ('\n', f);
14026 imports_list = imports_list->next;
14027 }
14028}
5eb99654
KG
14029
14030static void
e32bac5b 14031aof_globalize_label (FILE *stream, const char *name)
5eb99654
KG
14032{
14033 default_globalize_label (stream, name);
14034 if (! strcmp (name, "main"))
14035 arm_main_function = 1;
14036}
a5fe455b 14037
1bc7c5b6 14038static void
f1777882 14039aof_file_start (void)
1bc7c5b6
ZW
14040{
14041 fputs ("__r0\tRN\t0\n", asm_out_file);
14042 fputs ("__a1\tRN\t0\n", asm_out_file);
14043 fputs ("__a2\tRN\t1\n", asm_out_file);
14044 fputs ("__a3\tRN\t2\n", asm_out_file);
14045 fputs ("__a4\tRN\t3\n", asm_out_file);
14046 fputs ("__v1\tRN\t4\n", asm_out_file);
14047 fputs ("__v2\tRN\t5\n", asm_out_file);
14048 fputs ("__v3\tRN\t6\n", asm_out_file);
14049 fputs ("__v4\tRN\t7\n", asm_out_file);
14050 fputs ("__v5\tRN\t8\n", asm_out_file);
14051 fputs ("__v6\tRN\t9\n", asm_out_file);
14052 fputs ("__sl\tRN\t10\n", asm_out_file);
14053 fputs ("__fp\tRN\t11\n", asm_out_file);
14054 fputs ("__ip\tRN\t12\n", asm_out_file);
14055 fputs ("__sp\tRN\t13\n", asm_out_file);
14056 fputs ("__lr\tRN\t14\n", asm_out_file);
14057 fputs ("__pc\tRN\t15\n", asm_out_file);
14058 fputs ("__f0\tFN\t0\n", asm_out_file);
14059 fputs ("__f1\tFN\t1\n", asm_out_file);
14060 fputs ("__f2\tFN\t2\n", asm_out_file);
14061 fputs ("__f3\tFN\t3\n", asm_out_file);
14062 fputs ("__f4\tFN\t4\n", asm_out_file);
14063 fputs ("__f5\tFN\t5\n", asm_out_file);
14064 fputs ("__f6\tFN\t6\n", asm_out_file);
14065 fputs ("__f7\tFN\t7\n", asm_out_file);
14066 text_section ();
14067}
14068
a5fe455b 14069static void
e32bac5b 14070aof_file_end (void)
a5fe455b
ZW
14071{
14072 if (flag_pic)
14073 aof_dump_pic_table (asm_out_file);
14074 aof_dump_imports (asm_out_file);
14075 fputs ("\tEND\n", asm_out_file);
14076}
2b835d68 14077#endif /* AOF_ASSEMBLER */
7c262518 14078
ebe413e5 14079#ifdef OBJECT_FORMAT_ELF
7c262518
RH
14080/* Switch to an arbitrary section NAME with attributes as specified
14081 by FLAGS. ALIGN specifies any known alignment requirements for
14082 the section; 0 if the default should be used.
14083
14084 Differs from the default elf version only in the prefix character
14085 used before the section type. */
14086
14087static void
e32bac5b 14088arm_elf_asm_named_section (const char *name, unsigned int flags)
7c262518 14089{
6a0a6ac4
AM
14090 char flagchars[10], *f = flagchars;
14091
14092 if (! named_section_first_declaration (name))
14093 {
14094 fprintf (asm_out_file, "\t.section\t%s\n", name);
14095 return;
14096 }
7c262518
RH
14097
14098 if (!(flags & SECTION_DEBUG))
14099 *f++ = 'a';
14100 if (flags & SECTION_WRITE)
14101 *f++ = 'w';
14102 if (flags & SECTION_CODE)
14103 *f++ = 'x';
14104 if (flags & SECTION_SMALL)
14105 *f++ = 's';
201556f0
JJ
14106 if (flags & SECTION_MERGE)
14107 *f++ = 'M';
14108 if (flags & SECTION_STRINGS)
14109 *f++ = 'S';
6a0a6ac4
AM
14110 if (flags & SECTION_TLS)
14111 *f++ = 'T';
7c262518
RH
14112 *f = '\0';
14113
6a0a6ac4 14114 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
7c262518 14115
6a0a6ac4
AM
14116 if (!(flags & SECTION_NOTYPE))
14117 {
14118 const char *type;
14119
14120 if (flags & SECTION_BSS)
14121 type = "nobits";
14122 else
14123 type = "progbits";
14124
14125 fprintf (asm_out_file, ",%%%s", type);
14126
14127 if (flags & SECTION_ENTSIZE)
14128 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
14129 }
14130
14131 putc ('\n', asm_out_file);
7c262518 14132}
ebe413e5 14133#endif
fb49053f
RH
14134
14135#ifndef ARM_PE
14136/* Symbols in the text segment can be accessed without indirecting via the
14137 constant pool; it may take an extra binary operation, but this is still
14138 faster than indirecting via memory. Don't do this when not optimizing,
14139 since we won't be calculating al of the offsets necessary to do this
14140 simplification. */
14141
14142static void
e32bac5b 14143arm_encode_section_info (tree decl, rtx rtl, int first)
fb49053f
RH
14144{
14145 /* This doesn't work with AOF syntax, since the string table may be in
14146 a different AREA. */
14147#ifndef AOF_ASSEMBLER
3521b33c 14148 if (optimize > 0 && TREE_CONSTANT (decl))
c6a2438a 14149 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
fb49053f
RH
14150#endif
14151
14152 /* If we are referencing a function that is weak then encode a long call
14153 flag in the function name, otherwise if the function is static or
14154 or known to be defined in this file then encode a short call flag. */
14155 if (first && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
14156 {
14157 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14158 arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14159 else if (! TREE_PUBLIC (decl))
14160 arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14161 }
14162}
14163#endif /* !ARM_PE */
483ab821 14164
4977bab6 14165static void
e32bac5b 14166arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
4977bab6
ZW
14167{
14168 if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14169 && !strcmp (prefix, "L"))
14170 {
14171 arm_ccfsm_state = 0;
14172 arm_target_insn = NULL;
14173 }
14174 default_internal_label (stream, prefix, labelno);
14175}
14176
c590b625
RH
14177/* Output code to add DELTA to the first argument, and then jump
14178 to FUNCTION. Used for C++ multiple inheritance. */
c590b625 14179static void
e32bac5b
RE
14180arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14181 HOST_WIDE_INT delta,
14182 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14183 tree function)
483ab821 14184{
9b66ebb1
PB
14185 static int thunk_label = 0;
14186 char label[256];
483ab821
MM
14187 int mi_delta = delta;
14188 const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14189 int shift = 0;
61f71b34 14190 int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
483ab821
MM
14191 ? 1 : 0);
14192 if (mi_delta < 0)
14193 mi_delta = - mi_delta;
9b66ebb1
PB
14194 if (TARGET_THUMB)
14195 {
14196 int labelno = thunk_label++;
14197 ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14198 fputs ("\tldr\tr12, ", file);
14199 assemble_name (file, label);
14200 fputc ('\n', file);
14201 }
483ab821
MM
14202 while (mi_delta != 0)
14203 {
14204 if ((mi_delta & (3 << shift)) == 0)
14205 shift += 2;
14206 else
14207 {
14208 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14209 mi_op, this_regno, this_regno,
14210 mi_delta & (0xff << shift));
14211 mi_delta &= ~(0xff << shift);
14212 shift += 8;
14213 }
14214 }
9b66ebb1
PB
14215 if (TARGET_THUMB)
14216 {
14217 fprintf (file, "\tbx\tr12\n");
14218 ASM_OUTPUT_ALIGN (file, 2);
14219 assemble_name (file, label);
14220 fputs (":\n", file);
14221 assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14222 }
14223 else
14224 {
14225 fputs ("\tb\t", file);
14226 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14227 if (NEED_PLT_RELOC)
14228 fputs ("(PLT)", file);
14229 fputc ('\n', file);
14230 }
483ab821 14231}
5a9335ef
NC
14232
14233int
6f5f2481 14234arm_emit_vector_const (FILE *file, rtx x)
5a9335ef
NC
14235{
14236 int i;
14237 const char * pattern;
14238
14239 if (GET_CODE (x) != CONST_VECTOR)
14240 abort ();
14241
14242 switch (GET_MODE (x))
14243 {
14244 case V2SImode: pattern = "%08x"; break;
14245 case V4HImode: pattern = "%04x"; break;
14246 case V8QImode: pattern = "%02x"; break;
14247 default: abort ();
14248 }
14249
14250 fprintf (file, "0x");
14251 for (i = CONST_VECTOR_NUNITS (x); i--;)
14252 {
14253 rtx element;
14254
14255 element = CONST_VECTOR_ELT (x, i);
14256 fprintf (file, pattern, INTVAL (element));
14257 }
14258
14259 return 1;
14260}
14261
14262const char *
6f5f2481 14263arm_output_load_gr (rtx *operands)
5a9335ef
NC
14264{
14265 rtx reg;
14266 rtx offset;
14267 rtx wcgr;
14268 rtx sum;
14269
14270 if (GET_CODE (operands [1]) != MEM
14271 || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14272 || GET_CODE (reg = XEXP (sum, 0)) != REG
14273 || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14274 || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14275 return "wldrw%?\t%0, %1";
14276
14277 /* Fix up an out-of-range load of a GR register. */
14278 output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14279 wcgr = operands[0];
14280 operands[0] = reg;
14281 output_asm_insn ("ldr%?\t%0, %1", operands);
14282
14283 operands[0] = wcgr;
14284 operands[1] = reg;
14285 output_asm_insn ("tmcr%?\t%0, %1", operands);
14286 output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14287
14288 return "";
14289}
f9ba5949
KH
14290
14291static rtx
14292arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14293 int incoming ATTRIBUTE_UNUSED)
14294{
14295#if 0
14296 /* FIXME: The ARM backend has special code to handle structure
14297 returns, and will reserve its own hidden first argument. So
14298 if this macro is enabled a *second* hidden argument will be
14299 reserved, which will break binary compatibility with old
14300 toolchains and also thunk handling. One day this should be
14301 fixed. */
14302 return 0;
14303#else
14304 /* Register in which address to store a structure value
14305 is passed to a function. */
14306 return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14307#endif
14308}
1cc9f5f5
KH
14309
14310/* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14311
14312 On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14313 named arg and all anonymous args onto the stack.
14314 XXX I know the prologue shouldn't be pushing registers, but it is faster
14315 that way. */
14316
14317static void
14318arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14319 enum machine_mode mode ATTRIBUTE_UNUSED,
14320 tree type ATTRIBUTE_UNUSED,
14321 int *pretend_size,
14322 int second_time ATTRIBUTE_UNUSED)
14323{
14324 cfun->machine->uses_anonymous_args = 1;
14325 if (cum->nregs < NUM_ARG_REGS)
14326 *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14327}
9b66ebb1 14328
59b9a953 14329/* Return nonzero if the CONSUMER instruction (a store) does not need
9b66ebb1
PB
14330 PRODUCER's value to calculate the address. */
14331
14332int
14333arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14334{
14335 rtx value = PATTERN (producer);
14336 rtx addr = PATTERN (consumer);
14337
14338 if (GET_CODE (value) == COND_EXEC)
14339 value = COND_EXEC_CODE (value);
14340 if (GET_CODE (value) == PARALLEL)
14341 value = XVECEXP (value, 0, 0);
14342 value = XEXP (value, 0);
14343 if (GET_CODE (addr) == COND_EXEC)
14344 addr = COND_EXEC_CODE (addr);
14345 if (GET_CODE (addr) == PARALLEL)
14346 addr = XVECEXP (addr, 0, 0);
14347 addr = XEXP (addr, 0);
14348
14349 return !reg_overlap_mentioned_p (value, addr);
14350}
14351
59b9a953 14352/* Return nonzero if the CONSUMER instruction (an ALU op) does not
9b66ebb1
PB
14353 have an early register shift value or amount dependency on the
14354 result of PRODUCER. */
14355
14356int
14357arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14358{
14359 rtx value = PATTERN (producer);
14360 rtx op = PATTERN (consumer);
14361 rtx early_op;
14362
14363 if (GET_CODE (value) == COND_EXEC)
14364 value = COND_EXEC_CODE (value);
14365 if (GET_CODE (value) == PARALLEL)
14366 value = XVECEXP (value, 0, 0);
14367 value = XEXP (value, 0);
14368 if (GET_CODE (op) == COND_EXEC)
14369 op = COND_EXEC_CODE (op);
14370 if (GET_CODE (op) == PARALLEL)
14371 op = XVECEXP (op, 0, 0);
14372 op = XEXP (op, 1);
14373
14374 early_op = XEXP (op, 0);
14375 /* This is either an actual independent shift, or a shift applied to
14376 the first operand of another operation. We want the whole shift
14377 operation. */
14378 if (GET_CODE (early_op) == REG)
14379 early_op = op;
14380
14381 return !reg_overlap_mentioned_p (value, early_op);
14382}
14383
59b9a953 14384/* Return nonzero if the CONSUMER instruction (an ALU op) does not
9b66ebb1
PB
14385 have an early register shift value dependency on the result of
14386 PRODUCER. */
14387
14388int
14389arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14390{
14391 rtx value = PATTERN (producer);
14392 rtx op = PATTERN (consumer);
14393 rtx early_op;
14394
14395 if (GET_CODE (value) == COND_EXEC)
14396 value = COND_EXEC_CODE (value);
14397 if (GET_CODE (value) == PARALLEL)
14398 value = XVECEXP (value, 0, 0);
14399 value = XEXP (value, 0);
14400 if (GET_CODE (op) == COND_EXEC)
14401 op = COND_EXEC_CODE (op);
14402 if (GET_CODE (op) == PARALLEL)
14403 op = XVECEXP (op, 0, 0);
14404 op = XEXP (op, 1);
14405
14406 early_op = XEXP (op, 0);
14407
14408 /* This is either an actual independent shift, or a shift applied to
14409 the first operand of another operation. We want the value being
14410 shifted, in either case. */
14411 if (GET_CODE (early_op) != REG)
14412 early_op = XEXP (early_op, 0);
14413
14414 return !reg_overlap_mentioned_p (value, early_op);
14415}
14416
59b9a953 14417/* Return nonzero if the CONSUMER (a mul or mac op) does not
9b66ebb1
PB
14418 have an early register mult dependency on the result of
14419 PRODUCER. */
14420
14421int
14422arm_no_early_mul_dep (rtx producer, rtx consumer)
14423{
14424 rtx value = PATTERN (producer);
14425 rtx op = PATTERN (consumer);
14426
14427 if (GET_CODE (value) == COND_EXEC)
14428 value = COND_EXEC_CODE (value);
14429 if (GET_CODE (value) == PARALLEL)
14430 value = XVECEXP (value, 0, 0);
14431 value = XEXP (value, 0);
14432 if (GET_CODE (op) == COND_EXEC)
14433 op = COND_EXEC_CODE (op);
14434 if (GET_CODE (op) == PARALLEL)
14435 op = XVECEXP (op, 0, 0);
14436 op = XEXP (op, 1);
14437
14438 return (GET_CODE (op) == PLUS
14439 && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14440}
14441
This page took 3.755921 seconds and 5 git commands to generate.