OSDN Git Service

2010-12-09 Yao Qi <yao@codesourcery.com>
[pf3gnuchains/gcc-fork.git] / gcc / config / arm / arm.c
1 /* Output routines for GCC for ARM.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5    Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
6    and Martin Simmons (@harleqn.co.uk).
7    More major hacks by Richard Earnshaw (rearnsha@arm.com).
8
9    This file is part of GCC.
10
11    GCC is free software; you can redistribute it and/or modify it
12    under the terms of the GNU General Public License as published
13    by the Free Software Foundation; either version 3, or (at your
14    option) any later version.
15
16    GCC is distributed in the hope that it will be useful, but WITHOUT
17    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
19    License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GCC; see the file COPYING3.  If not see
23    <http://www.gnu.org/licenses/>.  */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "obstack.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "insn-config.h"
35 #include "conditions.h"
36 #include "output.h"
37 #include "insn-attr.h"
38 #include "flags.h"
39 #include "reload.h"
40 #include "function.h"
41 #include "expr.h"
42 #include "optabs.h"
43 #include "diagnostic-core.h"
44 #include "recog.h"
45 #include "cgraph.h"
46 #include "ggc.h"
47 #include "except.h"
48 #include "c-family/c-pragma.h"  /* ??? */
49 #include "integrate.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "target-def.h"
53 #include "debug.h"
54 #include "langhooks.h"
55 #include "df.h"
56 #include "intl.h"
57 #include "libfuncs.h"
58 #include "params.h"
59
60 /* Forward definitions of types.  */
61 typedef struct minipool_node    Mnode;
62 typedef struct minipool_fixup   Mfix;
63
64 void (*arm_lang_output_object_attributes_hook)(void);
65
66 /* Forward function declarations.  */
67 static bool arm_needs_doubleword_align (enum machine_mode, const_tree);
68 static int arm_compute_static_chain_stack_bytes (void);
69 static arm_stack_offsets *arm_get_frame_offsets (void);
70 static void arm_add_gc_roots (void);
71 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
72                              HOST_WIDE_INT, rtx, rtx, int, int);
73 static unsigned bit_count (unsigned long);
74 static int arm_address_register_rtx_p (rtx, int);
75 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
76 static int thumb2_legitimate_index_p (enum machine_mode, rtx, int);
77 static int thumb1_base_register_rtx_p (rtx, enum machine_mode, int);
78 static rtx arm_legitimize_address (rtx, rtx, enum machine_mode);
79 static rtx thumb_legitimize_address (rtx, rtx, enum machine_mode);
80 inline static int thumb1_index_register_rtx_p (rtx, int);
81 static bool arm_legitimate_address_p (enum machine_mode, rtx, bool);
82 static int thumb_far_jump_used_p (void);
83 static bool thumb_force_lr_save (void);
84 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
85 static rtx emit_sfm (int, int);
86 static unsigned arm_size_return_regs (void);
87 static bool arm_assemble_integer (rtx, unsigned int, int);
88 static void arm_print_operand (FILE *, rtx, int);
89 static void arm_print_operand_address (FILE *, rtx);
90 static bool arm_print_operand_punct_valid_p (unsigned char code);
91 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
92 static arm_cc get_arm_condition_code (rtx);
93 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
94 static rtx is_jump_table (rtx);
95 static const char *output_multi_immediate (rtx *, const char *, const char *,
96                                            int, HOST_WIDE_INT);
97 static const char *shift_op (rtx, HOST_WIDE_INT *);
98 static struct machine_function *arm_init_machine_status (void);
99 static void thumb_exit (FILE *, int);
100 static rtx is_jump_table (rtx);
101 static HOST_WIDE_INT get_jump_table_size (rtx);
102 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
103 static Mnode *add_minipool_forward_ref (Mfix *);
104 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
105 static Mnode *add_minipool_backward_ref (Mfix *);
106 static void assign_minipool_offsets (Mfix *);
107 static void arm_print_value (FILE *, rtx);
108 static void dump_minipool (rtx);
109 static int arm_barrier_cost (rtx);
110 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
111 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
112 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
113                                rtx);
114 static void arm_reorg (void);
115 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
116 static unsigned long arm_compute_save_reg0_reg12_mask (void);
117 static unsigned long arm_compute_save_reg_mask (void);
118 static unsigned long arm_isr_value (tree);
119 static unsigned long arm_compute_func_type (void);
120 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
121 static tree arm_handle_pcs_attribute (tree *, tree, tree, int, bool *);
122 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
123 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
124 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
125 #endif
126 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
127 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
128 static void thumb1_output_function_prologue (FILE *, HOST_WIDE_INT);
129 static int arm_comp_type_attributes (const_tree, const_tree);
130 static void arm_set_default_type_attributes (tree);
131 static int arm_adjust_cost (rtx, rtx, rtx, int);
132 static int count_insns_for_constant (HOST_WIDE_INT, int);
133 static int arm_get_strip_length (int);
134 static bool arm_function_ok_for_sibcall (tree, tree);
135 static enum machine_mode arm_promote_function_mode (const_tree,
136                                                     enum machine_mode, int *,
137                                                     const_tree, int);
138 static bool arm_return_in_memory (const_tree, const_tree);
139 static rtx arm_function_value (const_tree, const_tree, bool);
140 static rtx arm_libcall_value (enum machine_mode, const_rtx);
141
142 static void arm_internal_label (FILE *, const char *, unsigned long);
143 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
144                                  tree);
145 static bool arm_have_conditional_execution (void);
146 static bool arm_rtx_costs_1 (rtx, enum rtx_code, int*, bool);
147 static bool arm_size_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *);
148 static bool arm_slowmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
149 static bool arm_fastmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
150 static bool arm_xscale_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
151 static bool arm_9e_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
152 static bool arm_rtx_costs (rtx, int, int, int *, bool);
153 static int arm_address_cost (rtx, bool);
154 static bool arm_memory_load_p (rtx);
155 static bool arm_cirrus_insn_p (rtx);
156 static void cirrus_reorg (rtx);
157 static void arm_init_builtins (void);
158 static void arm_init_iwmmxt_builtins (void);
159 static rtx safe_vector_operand (rtx, enum machine_mode);
160 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
161 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
162 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
163 static void emit_constant_insn (rtx cond, rtx pattern);
164 static rtx emit_set_insn (rtx, rtx);
165 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
166                                   tree, bool);
167 static rtx arm_function_arg (CUMULATIVE_ARGS *, enum machine_mode,
168                              const_tree, bool);
169 static void arm_function_arg_advance (CUMULATIVE_ARGS *, enum machine_mode,
170                                       const_tree, bool);
171 static unsigned int arm_function_arg_boundary (enum machine_mode, const_tree);
172 static rtx aapcs_allocate_return_reg (enum machine_mode, const_tree,
173                                       const_tree);
174 static int aapcs_select_return_coproc (const_tree, const_tree);
175
176 #ifdef OBJECT_FORMAT_ELF
177 static void arm_elf_asm_constructor (rtx, int) ATTRIBUTE_UNUSED;
178 static void arm_elf_asm_destructor (rtx, int) ATTRIBUTE_UNUSED;
179 #endif
180 #ifndef ARM_PE
181 static void arm_encode_section_info (tree, rtx, int);
182 #endif
183
184 static void arm_file_end (void);
185 static void arm_file_start (void);
186
187 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
188                                         tree, int *, int);
189 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
190                                    enum machine_mode, const_tree, bool);
191 static bool arm_promote_prototypes (const_tree);
192 static bool arm_default_short_enums (void);
193 static bool arm_align_anon_bitfield (void);
194 static bool arm_return_in_msb (const_tree);
195 static bool arm_must_pass_in_stack (enum machine_mode, const_tree);
196 static bool arm_return_in_memory (const_tree, const_tree);
197 #if ARM_UNWIND_INFO
198 static void arm_unwind_emit (FILE *, rtx);
199 static bool arm_output_ttype (rtx);
200 static void arm_asm_emit_except_personality (rtx);
201 static void arm_asm_init_sections (void);
202 #endif
203 static enum unwind_info_type arm_except_unwind_info (struct gcc_options *);
204 static void arm_dwarf_handle_frame_unspec (const char *, rtx, int);
205 static rtx arm_dwarf_register_span (rtx);
206
207 static tree arm_cxx_guard_type (void);
208 static bool arm_cxx_guard_mask_bit (void);
209 static tree arm_get_cookie_size (tree);
210 static bool arm_cookie_has_size (void);
211 static bool arm_cxx_cdtor_returns_this (void);
212 static bool arm_cxx_key_method_may_be_inline (void);
213 static void arm_cxx_determine_class_data_visibility (tree);
214 static bool arm_cxx_class_data_always_comdat (void);
215 static bool arm_cxx_use_aeabi_atexit (void);
216 static void arm_init_libfuncs (void);
217 static tree arm_build_builtin_va_list (void);
218 static void arm_expand_builtin_va_start (tree, rtx);
219 static tree arm_gimplify_va_arg_expr (tree, tree, gimple_seq *, gimple_seq *);
220 static void arm_option_override (void);
221 static bool arm_handle_option (size_t, const char *, int);
222 static void arm_target_help (void);
223 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
224 static bool arm_cannot_copy_insn_p (rtx);
225 static bool arm_tls_symbol_p (rtx x);
226 static int arm_issue_rate (void);
227 static void arm_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
228 static bool arm_output_addr_const_extra (FILE *, rtx);
229 static bool arm_allocate_stack_slots_for_args (void);
230 static const char *arm_invalid_parameter_type (const_tree t);
231 static const char *arm_invalid_return_type (const_tree t);
232 static tree arm_promoted_type (const_tree t);
233 static tree arm_convert_to_type (tree type, tree expr);
234 static bool arm_scalar_mode_supported_p (enum machine_mode);
235 static bool arm_frame_pointer_required (void);
236 static bool arm_can_eliminate (const int, const int);
237 static void arm_asm_trampoline_template (FILE *);
238 static void arm_trampoline_init (rtx, tree, rtx);
239 static rtx arm_trampoline_adjust_address (rtx);
240 static rtx arm_pic_static_addr (rtx orig, rtx reg);
241 static bool cortex_a9_sched_adjust_cost (rtx, rtx, rtx, int *);
242 static bool xscale_sched_adjust_cost (rtx, rtx, rtx, int *);
243 static enum machine_mode arm_preferred_simd_mode (enum machine_mode);
244 static bool arm_class_likely_spilled_p (reg_class_t);
245 static bool arm_vector_alignment_reachable (const_tree type, bool is_packed);
246 static bool arm_builtin_support_vector_misalignment (enum machine_mode mode,
247                                                      const_tree type,
248                                                      int misalignment,
249                                                      bool is_packed);
250 static void arm_conditional_register_usage (void);
251 static reg_class_t arm_preferred_rename_class (reg_class_t class);
252
253 \f
254 /* Table of machine attributes.  */
255 static const struct attribute_spec arm_attribute_table[] =
256 {
257   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
258   /* Function calls made to this symbol must be done indirectly, because
259      it may lie outside of the 26 bit addressing range of a normal function
260      call.  */
261   { "long_call",    0, 0, false, true,  true,  NULL },
262   /* Whereas these functions are always known to reside within the 26 bit
263      addressing range.  */
264   { "short_call",   0, 0, false, true,  true,  NULL },
265   /* Specify the procedure call conventions for a function.  */
266   { "pcs",          1, 1, false, true,  true,  arm_handle_pcs_attribute },
267   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
268   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
269   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
270   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
271 #ifdef ARM_PE
272   /* ARM/PE has three new attributes:
273      interfacearm - ?
274      dllexport - for exporting a function/variable that will live in a dll
275      dllimport - for importing a function/variable from a dll
276
277      Microsoft allows multiple declspecs in one __declspec, separating
278      them with spaces.  We do NOT support this.  Instead, use __declspec
279      multiple times.
280   */
281   { "dllimport",    0, 0, true,  false, false, NULL },
282   { "dllexport",    0, 0, true,  false, false, NULL },
283   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
284 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
285   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
286   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
287   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
288 #endif
289   { NULL,           0, 0, false, false, false, NULL }
290 };
291
292 /* Set default optimization options.  */
293 static const struct default_options arm_option_optimization_table[] =
294   {
295     /* Enable section anchors by default at -O1 or higher.  */
296     { OPT_LEVELS_1_PLUS, OPT_fsection_anchors, NULL, 1 },
297     { OPT_LEVELS_1_PLUS, OPT_fomit_frame_pointer, NULL, 1 },
298     { OPT_LEVELS_NONE, 0, NULL, 0 }
299   };
300 \f
301 /* Initialize the GCC target structure.  */
302 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
303 #undef  TARGET_MERGE_DECL_ATTRIBUTES
304 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
305 #endif
306
307 #undef TARGET_LEGITIMIZE_ADDRESS
308 #define TARGET_LEGITIMIZE_ADDRESS arm_legitimize_address
309
310 #undef  TARGET_ATTRIBUTE_TABLE
311 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
312
313 #undef TARGET_ASM_FILE_START
314 #define TARGET_ASM_FILE_START arm_file_start
315 #undef TARGET_ASM_FILE_END
316 #define TARGET_ASM_FILE_END arm_file_end
317
318 #undef  TARGET_ASM_ALIGNED_SI_OP
319 #define TARGET_ASM_ALIGNED_SI_OP NULL
320 #undef  TARGET_ASM_INTEGER
321 #define TARGET_ASM_INTEGER arm_assemble_integer
322
323 #undef TARGET_PRINT_OPERAND
324 #define TARGET_PRINT_OPERAND arm_print_operand
325 #undef TARGET_PRINT_OPERAND_ADDRESS
326 #define TARGET_PRINT_OPERAND_ADDRESS arm_print_operand_address
327 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
328 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P arm_print_operand_punct_valid_p
329
330 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
331 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA arm_output_addr_const_extra
332
333 #undef  TARGET_ASM_FUNCTION_PROLOGUE
334 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
335
336 #undef  TARGET_ASM_FUNCTION_EPILOGUE
337 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
338
339 #undef  TARGET_DEFAULT_TARGET_FLAGS
340 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
341 #undef  TARGET_HANDLE_OPTION
342 #define TARGET_HANDLE_OPTION arm_handle_option
343 #undef  TARGET_HELP
344 #define TARGET_HELP arm_target_help
345 #undef  TARGET_OPTION_OVERRIDE
346 #define TARGET_OPTION_OVERRIDE arm_option_override
347 #undef  TARGET_OPTION_OPTIMIZATION_TABLE
348 #define TARGET_OPTION_OPTIMIZATION_TABLE arm_option_optimization_table
349
350 #undef  TARGET_COMP_TYPE_ATTRIBUTES
351 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
352
353 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
354 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
355
356 #undef  TARGET_SCHED_ADJUST_COST
357 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
358
359 #undef TARGET_ENCODE_SECTION_INFO
360 #ifdef ARM_PE
361 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
362 #else
363 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
364 #endif
365
366 #undef  TARGET_STRIP_NAME_ENCODING
367 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
368
369 #undef  TARGET_ASM_INTERNAL_LABEL
370 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
371
372 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
373 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
374
375 #undef  TARGET_FUNCTION_VALUE
376 #define TARGET_FUNCTION_VALUE arm_function_value
377
378 #undef  TARGET_LIBCALL_VALUE
379 #define TARGET_LIBCALL_VALUE arm_libcall_value
380
381 #undef  TARGET_ASM_OUTPUT_MI_THUNK
382 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
383 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
384 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
385
386 #undef  TARGET_RTX_COSTS
387 #define TARGET_RTX_COSTS arm_rtx_costs
388 #undef  TARGET_ADDRESS_COST
389 #define TARGET_ADDRESS_COST arm_address_cost
390
391 #undef TARGET_SHIFT_TRUNCATION_MASK
392 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
393 #undef TARGET_VECTOR_MODE_SUPPORTED_P
394 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
395 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
396 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE arm_preferred_simd_mode
397
398 #undef  TARGET_MACHINE_DEPENDENT_REORG
399 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
400
401 #undef  TARGET_INIT_BUILTINS
402 #define TARGET_INIT_BUILTINS  arm_init_builtins
403 #undef  TARGET_EXPAND_BUILTIN
404 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
405
406 #undef TARGET_INIT_LIBFUNCS
407 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
408
409 #undef TARGET_PROMOTE_FUNCTION_MODE
410 #define TARGET_PROMOTE_FUNCTION_MODE arm_promote_function_mode
411 #undef TARGET_PROMOTE_PROTOTYPES
412 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
413 #undef TARGET_PASS_BY_REFERENCE
414 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
415 #undef TARGET_ARG_PARTIAL_BYTES
416 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
417 #undef TARGET_FUNCTION_ARG
418 #define TARGET_FUNCTION_ARG arm_function_arg
419 #undef TARGET_FUNCTION_ARG_ADVANCE
420 #define TARGET_FUNCTION_ARG_ADVANCE arm_function_arg_advance
421 #undef TARGET_FUNCTION_ARG_BOUNDARY
422 #define TARGET_FUNCTION_ARG_BOUNDARY arm_function_arg_boundary
423
424 #undef  TARGET_SETUP_INCOMING_VARARGS
425 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
426
427 #undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
428 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS arm_allocate_stack_slots_for_args
429
430 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
431 #define TARGET_ASM_TRAMPOLINE_TEMPLATE arm_asm_trampoline_template
432 #undef TARGET_TRAMPOLINE_INIT
433 #define TARGET_TRAMPOLINE_INIT arm_trampoline_init
434 #undef TARGET_TRAMPOLINE_ADJUST_ADDRESS
435 #define TARGET_TRAMPOLINE_ADJUST_ADDRESS arm_trampoline_adjust_address
436
437 #undef TARGET_DEFAULT_SHORT_ENUMS
438 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
439
440 #undef TARGET_ALIGN_ANON_BITFIELD
441 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
442
443 #undef TARGET_NARROW_VOLATILE_BITFIELD
444 #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
445
446 #undef TARGET_CXX_GUARD_TYPE
447 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
448
449 #undef TARGET_CXX_GUARD_MASK_BIT
450 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
451
452 #undef TARGET_CXX_GET_COOKIE_SIZE
453 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
454
455 #undef TARGET_CXX_COOKIE_HAS_SIZE
456 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
457
458 #undef TARGET_CXX_CDTOR_RETURNS_THIS
459 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
460
461 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
462 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
463
464 #undef TARGET_CXX_USE_AEABI_ATEXIT
465 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
466
467 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
468 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
469   arm_cxx_determine_class_data_visibility
470
471 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
472 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
473
474 #undef TARGET_RETURN_IN_MSB
475 #define TARGET_RETURN_IN_MSB arm_return_in_msb
476
477 #undef TARGET_RETURN_IN_MEMORY
478 #define TARGET_RETURN_IN_MEMORY arm_return_in_memory
479
480 #undef TARGET_MUST_PASS_IN_STACK
481 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
482
483 #if ARM_UNWIND_INFO
484 #undef TARGET_ASM_UNWIND_EMIT
485 #define TARGET_ASM_UNWIND_EMIT arm_unwind_emit
486
487 /* EABI unwinding tables use a different format for the typeinfo tables.  */
488 #undef TARGET_ASM_TTYPE
489 #define TARGET_ASM_TTYPE arm_output_ttype
490
491 #undef TARGET_ARM_EABI_UNWINDER
492 #define TARGET_ARM_EABI_UNWINDER true
493
494 #undef TARGET_ASM_EMIT_EXCEPT_PERSONALITY
495 #define TARGET_ASM_EMIT_EXCEPT_PERSONALITY arm_asm_emit_except_personality
496
497 #undef TARGET_ASM_INIT_SECTIONS
498 #define TARGET_ASM_INIT_SECTIONS arm_asm_init_sections
499 #endif /* ARM_UNWIND_INFO */
500
501 #undef TARGET_EXCEPT_UNWIND_INFO
502 #define TARGET_EXCEPT_UNWIND_INFO  arm_except_unwind_info
503
504 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
505 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC arm_dwarf_handle_frame_unspec
506
507 #undef TARGET_DWARF_REGISTER_SPAN
508 #define TARGET_DWARF_REGISTER_SPAN arm_dwarf_register_span
509
510 #undef  TARGET_CANNOT_COPY_INSN_P
511 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
512
513 #ifdef HAVE_AS_TLS
514 #undef TARGET_HAVE_TLS
515 #define TARGET_HAVE_TLS true
516 #endif
517
518 #undef TARGET_HAVE_CONDITIONAL_EXECUTION
519 #define TARGET_HAVE_CONDITIONAL_EXECUTION arm_have_conditional_execution
520
521 #undef TARGET_CANNOT_FORCE_CONST_MEM
522 #define TARGET_CANNOT_FORCE_CONST_MEM arm_cannot_force_const_mem
523
524 #undef TARGET_MAX_ANCHOR_OFFSET
525 #define TARGET_MAX_ANCHOR_OFFSET 4095
526
527 /* The minimum is set such that the total size of the block
528    for a particular anchor is -4088 + 1 + 4095 bytes, which is
529    divisible by eight, ensuring natural spacing of anchors.  */
530 #undef TARGET_MIN_ANCHOR_OFFSET
531 #define TARGET_MIN_ANCHOR_OFFSET -4088
532
533 #undef TARGET_SCHED_ISSUE_RATE
534 #define TARGET_SCHED_ISSUE_RATE arm_issue_rate
535
536 #undef TARGET_MANGLE_TYPE
537 #define TARGET_MANGLE_TYPE arm_mangle_type
538
539 #undef TARGET_BUILD_BUILTIN_VA_LIST
540 #define TARGET_BUILD_BUILTIN_VA_LIST arm_build_builtin_va_list
541 #undef TARGET_EXPAND_BUILTIN_VA_START
542 #define TARGET_EXPAND_BUILTIN_VA_START arm_expand_builtin_va_start
543 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
544 #define TARGET_GIMPLIFY_VA_ARG_EXPR arm_gimplify_va_arg_expr
545
546 #ifdef HAVE_AS_TLS
547 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
548 #define TARGET_ASM_OUTPUT_DWARF_DTPREL arm_output_dwarf_dtprel
549 #endif
550
551 #undef TARGET_LEGITIMATE_ADDRESS_P
552 #define TARGET_LEGITIMATE_ADDRESS_P     arm_legitimate_address_p
553
554 #undef TARGET_INVALID_PARAMETER_TYPE
555 #define TARGET_INVALID_PARAMETER_TYPE arm_invalid_parameter_type
556
557 #undef TARGET_INVALID_RETURN_TYPE
558 #define TARGET_INVALID_RETURN_TYPE arm_invalid_return_type
559
560 #undef TARGET_PROMOTED_TYPE
561 #define TARGET_PROMOTED_TYPE arm_promoted_type
562
563 #undef TARGET_CONVERT_TO_TYPE
564 #define TARGET_CONVERT_TO_TYPE arm_convert_to_type
565
566 #undef TARGET_SCALAR_MODE_SUPPORTED_P
567 #define TARGET_SCALAR_MODE_SUPPORTED_P arm_scalar_mode_supported_p
568
569 #undef TARGET_FRAME_POINTER_REQUIRED
570 #define TARGET_FRAME_POINTER_REQUIRED arm_frame_pointer_required
571
572 #undef TARGET_CAN_ELIMINATE
573 #define TARGET_CAN_ELIMINATE arm_can_eliminate
574
575 #undef TARGET_CONDITIONAL_REGISTER_USAGE
576 #define TARGET_CONDITIONAL_REGISTER_USAGE arm_conditional_register_usage
577
578 #undef TARGET_CLASS_LIKELY_SPILLED_P
579 #define TARGET_CLASS_LIKELY_SPILLED_P arm_class_likely_spilled_p
580
581 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
582 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE \
583   arm_vector_alignment_reachable
584
585 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
586 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
587   arm_builtin_support_vector_misalignment
588
589 #undef TARGET_PREFERRED_RENAME_CLASS
590 #define TARGET_PREFERRED_RENAME_CLASS \
591   arm_preferred_rename_class
592
593 struct gcc_target targetm = TARGET_INITIALIZER;
594 \f
595 /* Obstack for minipool constant handling.  */
596 static struct obstack minipool_obstack;
597 static char *         minipool_startobj;
598
599 /* The maximum number of insns skipped which
600    will be conditionalised if possible.  */
601 static int max_insns_skipped = 5;
602
603 extern FILE * asm_out_file;
604
605 /* True if we are currently building a constant table.  */
606 int making_const_table;
607
608 /* The processor for which instructions should be scheduled.  */
609 enum processor_type arm_tune = arm_none;
610
611 /* The current tuning set.  */
612 const struct tune_params *current_tune;
613
614 /* Which floating point hardware to schedule for.  */
615 int arm_fpu_attr;
616
617 /* Which floating popint hardware to use.  */
618 const struct arm_fpu_desc *arm_fpu_desc;
619
620 /* Whether to use floating point hardware.  */
621 enum float_abi_type arm_float_abi;
622
623 /* Which __fp16 format to use.  */
624 enum arm_fp16_format_type arm_fp16_format;
625
626 /* Which ABI to use.  */
627 enum arm_abi_type arm_abi;
628
629 /* Which thread pointer model to use.  */
630 enum arm_tp_type target_thread_pointer = TP_AUTO;
631
632 /* Used to parse -mstructure_size_boundary command line option.  */
633 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
634
635 /* Used for Thumb call_via trampolines.  */
636 rtx thumb_call_via_label[14];
637 static int thumb_call_reg_needed;
638
639 /* Bit values used to identify processor capabilities.  */
640 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
641 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
642 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
643 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
644 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
645 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
646 #define FL_THUMB      (1 << 6)        /* Thumb aware */
647 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
648 #define FL_STRONG     (1 << 8)        /* StrongARM */
649 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
650 #define FL_XSCALE     (1 << 10)       /* XScale */
651 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
652 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
653                                          media instructions.  */
654 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
655 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
656                                          Note: ARM6 & 7 derivatives only.  */
657 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
658 #define FL_THUMB2     (1 << 16)       /* Thumb-2.  */
659 #define FL_NOTM       (1 << 17)       /* Instructions not present in the 'M'
660                                          profile.  */
661 #define FL_DIV        (1 << 18)       /* Hardware divide.  */
662 #define FL_VFPV3      (1 << 19)       /* Vector Floating Point V3.  */
663 #define FL_NEON       (1 << 20)       /* Neon instructions.  */
664 #define FL_ARCH7EM    (1 << 21)       /* Instructions present in the ARMv7E-M
665                                          architecture.  */
666 #define FL_ARCH7      (1 << 22)       /* Architecture 7.  */
667
668 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
669
670 /* Flags that only effect tuning, not available instructions.  */
671 #define FL_TUNE         (FL_WBUF | FL_VFPV2 | FL_STRONG | FL_LDSCHED \
672                          | FL_CO_PROC)
673
674 #define FL_FOR_ARCH2    FL_NOTM
675 #define FL_FOR_ARCH3    (FL_FOR_ARCH2 | FL_MODE32)
676 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
677 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
678 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
679 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
680 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
681 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
682 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
683 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
684 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
685 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
686 #define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
687 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
688 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
689 #define FL_FOR_ARCH6T2  (FL_FOR_ARCH6 | FL_THUMB2)
690 #define FL_FOR_ARCH6M   (FL_FOR_ARCH6 & ~FL_NOTM)
691 #define FL_FOR_ARCH7    ((FL_FOR_ARCH6T2 & ~FL_NOTM) | FL_ARCH7)
692 #define FL_FOR_ARCH7A   (FL_FOR_ARCH7 | FL_NOTM | FL_ARCH6K)
693 #define FL_FOR_ARCH7R   (FL_FOR_ARCH7A | FL_DIV)
694 #define FL_FOR_ARCH7M   (FL_FOR_ARCH7 | FL_DIV)
695 #define FL_FOR_ARCH7EM  (FL_FOR_ARCH7M | FL_ARCH7EM)
696
697 /* The bits in this mask specify which
698    instructions we are allowed to generate.  */
699 static unsigned long insn_flags = 0;
700
701 /* The bits in this mask specify which instruction scheduling options should
702    be used.  */
703 static unsigned long tune_flags = 0;
704
705 /* The following are used in the arm.md file as equivalents to bits
706    in the above two flag variables.  */
707
708 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
709 int arm_arch3m = 0;
710
711 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
712 int arm_arch4 = 0;
713
714 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
715 int arm_arch4t = 0;
716
717 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
718 int arm_arch5 = 0;
719
720 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
721 int arm_arch5e = 0;
722
723 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
724 int arm_arch6 = 0;
725
726 /* Nonzero if this chip supports the ARM 6K extensions.  */
727 int arm_arch6k = 0;
728
729 /* Nonzero if this chip supports the ARM 7 extensions.  */
730 int arm_arch7 = 0;
731
732 /* Nonzero if instructions not present in the 'M' profile can be used.  */
733 int arm_arch_notm = 0;
734
735 /* Nonzero if instructions present in ARMv7E-M can be used.  */
736 int arm_arch7em = 0;
737
738 /* Nonzero if this chip can benefit from load scheduling.  */
739 int arm_ld_sched = 0;
740
741 /* Nonzero if this chip is a StrongARM.  */
742 int arm_tune_strongarm = 0;
743
744 /* Nonzero if this chip is a Cirrus variant.  */
745 int arm_arch_cirrus = 0;
746
747 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
748 int arm_arch_iwmmxt = 0;
749
750 /* Nonzero if this chip is an XScale.  */
751 int arm_arch_xscale = 0;
752
753 /* Nonzero if tuning for XScale  */
754 int arm_tune_xscale = 0;
755
756 /* Nonzero if we want to tune for stores that access the write-buffer.
757    This typically means an ARM6 or ARM7 with MMU or MPU.  */
758 int arm_tune_wbuf = 0;
759
760 /* Nonzero if tuning for Cortex-A9.  */
761 int arm_tune_cortex_a9 = 0;
762
763 /* Nonzero if generating Thumb instructions.  */
764 int thumb_code = 0;
765
766 /* Nonzero if generating Thumb-1 instructions.  */
767 int thumb1_code = 0;
768
769 /* Nonzero if we should define __THUMB_INTERWORK__ in the
770    preprocessor.
771    XXX This is a bit of a hack, it's intended to help work around
772    problems in GLD which doesn't understand that armv5t code is
773    interworking clean.  */
774 int arm_cpp_interwork = 0;
775
776 /* Nonzero if chip supports Thumb 2.  */
777 int arm_arch_thumb2;
778
779 /* Nonzero if chip supports integer division instruction.  */
780 int arm_arch_hwdiv;
781
782 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference,
783    we must report the mode of the memory reference from
784    TARGET_PRINT_OPERAND to TARGET_PRINT_OPERAND_ADDRESS.  */
785 enum machine_mode output_memory_reference_mode;
786
787 /* The register number to be used for the PIC offset register.  */
788 unsigned arm_pic_register = INVALID_REGNUM;
789
790 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
791    the next function.  */
792 static int after_arm_reorg = 0;
793
794 enum arm_pcs arm_pcs_default;
795
796 /* For an explanation of these variables, see final_prescan_insn below.  */
797 int arm_ccfsm_state;
798 /* arm_current_cc is also used for Thumb-2 cond_exec blocks.  */
799 enum arm_cond_code arm_current_cc;
800
801 rtx arm_target_insn;
802 int arm_target_label;
803 /* The number of conditionally executed insns, including the current insn.  */
804 int arm_condexec_count = 0;
805 /* A bitmask specifying the patterns for the IT block.
806    Zero means do not output an IT block before this insn. */
807 int arm_condexec_mask = 0;
808 /* The number of bits used in arm_condexec_mask.  */
809 int arm_condexec_masklen = 0;
810
811 /* The condition codes of the ARM, and the inverse function.  */
812 static const char * const arm_condition_codes[] =
813 {
814   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
815   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
816 };
817
818 /* The register numbers in sequence, for passing to arm_gen_load_multiple.  */
819 int arm_regs_in_sequence[] =
820 {
821   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
822 };
823
824 #define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
825 #define streq(string1, string2) (strcmp (string1, string2) == 0)
826
827 #define THUMB2_WORK_REGS (0xff & ~(  (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
828                                    | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
829                                    | (1 << PIC_OFFSET_TABLE_REGNUM)))
830 \f
831 /* Initialization code.  */
832
833 struct processors
834 {
835   const char *const name;
836   enum processor_type core;
837   const char *arch;
838   const unsigned long flags;
839   const struct tune_params *const tune;
840 };
841
842
843 #define ARM_PREFETCH_NOT_BENEFICIAL 0, -1, -1
844 #define ARM_PREFETCH_BENEFICIAL(prefetch_slots,l1_size,l1_line_size) \
845   prefetch_slots, \
846   l1_size, \
847   l1_line_size
848
849 const struct tune_params arm_slowmul_tune =
850 {
851   arm_slowmul_rtx_costs,
852   NULL,
853   3,
854   ARM_PREFETCH_NOT_BENEFICIAL
855 };
856
857 const struct tune_params arm_fastmul_tune =
858 {
859   arm_fastmul_rtx_costs,
860   NULL,
861   1,
862   ARM_PREFETCH_NOT_BENEFICIAL
863 };
864
865 const struct tune_params arm_xscale_tune =
866 {
867   arm_xscale_rtx_costs,
868   xscale_sched_adjust_cost,
869   2,
870   ARM_PREFETCH_NOT_BENEFICIAL
871 };
872
873 const struct tune_params arm_9e_tune =
874 {
875   arm_9e_rtx_costs,
876   NULL,
877   1,
878   ARM_PREFETCH_NOT_BENEFICIAL
879 };
880
881 const struct tune_params arm_cortex_a9_tune =
882 {
883   arm_9e_rtx_costs,
884   cortex_a9_sched_adjust_cost,
885   1,
886   ARM_PREFETCH_BENEFICIAL(4,32,32)
887 };
888
889
890 /* Not all of these give usefully different compilation alternatives,
891    but there is no simple way of generalizing them.  */
892 static const struct processors all_cores[] =
893 {
894   /* ARM Cores */
895 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
896   {NAME, IDENT, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, &arm_##COSTS##_tune},
897 #include "arm-cores.def"
898 #undef ARM_CORE
899   {NULL, arm_none, NULL, 0, NULL}
900 };
901
902 static const struct processors all_architectures[] =
903 {
904   /* ARM Architectures */
905   /* We don't specify tuning costs here as it will be figured out
906      from the core.  */
907
908   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
909   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
910   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
911   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
912   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
913   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
914      implementations that support it, so we will leave it out for now.  */
915   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
916   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
917   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
918   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
919   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
920   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
921   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
922   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
923   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
924   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
925   {"armv6t2", arm1156t2s, "6T2", FL_CO_PROC |             FL_FOR_ARCH6T2, NULL},
926   {"armv6-m", cortexm1,   "6M",                           FL_FOR_ARCH6M, NULL},
927   {"armv7",   cortexa8,   "7",   FL_CO_PROC |             FL_FOR_ARCH7, NULL},
928   {"armv7-a", cortexa8,   "7A",  FL_CO_PROC |             FL_FOR_ARCH7A, NULL},
929   {"armv7-r", cortexr4,   "7R",  FL_CO_PROC |             FL_FOR_ARCH7R, NULL},
930   {"armv7-m", cortexm3,   "7M",  FL_CO_PROC |             FL_FOR_ARCH7M, NULL},
931   {"armv7e-m", cortexm4,  "7EM", FL_CO_PROC |             FL_FOR_ARCH7EM, NULL},
932   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
933   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
934   {"iwmmxt2", iwmmxt2,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
935   {NULL, arm_none, NULL, 0 , NULL}
936 };
937
938
939 /* These are populated as commandline arguments are processed, or NULL
940    if not specified.  */
941 static const struct processors *arm_selected_arch;
942 static const struct processors *arm_selected_cpu;
943 static const struct processors *arm_selected_tune;
944
945 /* The name of the preprocessor macro to define for this architecture.  */
946
947 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
948
949 /* Available values for -mfpu=.  */
950
951 static const struct arm_fpu_desc all_fpus[] =
952 {
953   {"fpa",               ARM_FP_MODEL_FPA, 0, VFP_NONE, false, false},
954   {"fpe2",              ARM_FP_MODEL_FPA, 2, VFP_NONE, false, false},
955   {"fpe3",              ARM_FP_MODEL_FPA, 3, VFP_NONE, false, false},
956   {"maverick",          ARM_FP_MODEL_MAVERICK, 0, VFP_NONE, false, false},
957   {"vfp",               ARM_FP_MODEL_VFP, 2, VFP_REG_D16, false, false},
958   {"vfpv3",             ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, false},
959   {"vfpv3-fp16",        ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, true},
960   {"vfpv3-d16",         ARM_FP_MODEL_VFP, 3, VFP_REG_D16, false, false},
961   {"vfpv3-d16-fp16",    ARM_FP_MODEL_VFP, 3, VFP_REG_D16, false, true},
962   {"vfpv3xd",           ARM_FP_MODEL_VFP, 3, VFP_REG_SINGLE, false, false},
963   {"vfpv3xd-fp16",      ARM_FP_MODEL_VFP, 3, VFP_REG_SINGLE, false, true},
964   {"neon",              ARM_FP_MODEL_VFP, 3, VFP_REG_D32, true , false},
965   {"neon-fp16",         ARM_FP_MODEL_VFP, 3, VFP_REG_D32, true , true },
966   {"vfpv4",             ARM_FP_MODEL_VFP, 4, VFP_REG_D32, false, true},
967   {"vfpv4-d16",         ARM_FP_MODEL_VFP, 4, VFP_REG_D16, false, true},
968   {"fpv4-sp-d16",       ARM_FP_MODEL_VFP, 4, VFP_REG_SINGLE, false, true},
969   {"neon-vfpv4",        ARM_FP_MODEL_VFP, 4, VFP_REG_D32, true, true},
970   /* Compatibility aliases.  */
971   {"vfp3",              ARM_FP_MODEL_VFP, 3, VFP_REG_D32, false, false},
972 };
973
974
975 struct float_abi
976 {
977   const char * name;
978   enum float_abi_type abi_type;
979 };
980
981
982 /* Available values for -mfloat-abi=.  */
983
984 static const struct float_abi all_float_abis[] =
985 {
986   {"soft",      ARM_FLOAT_ABI_SOFT},
987   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
988   {"hard",      ARM_FLOAT_ABI_HARD}
989 };
990
991
992 struct fp16_format
993 {
994   const char *name;
995   enum arm_fp16_format_type fp16_format_type;
996 };
997
998
999 /* Available values for -mfp16-format=.  */
1000
1001 static const struct fp16_format all_fp16_formats[] =
1002 {
1003   {"none",              ARM_FP16_FORMAT_NONE},
1004   {"ieee",              ARM_FP16_FORMAT_IEEE},
1005   {"alternative",       ARM_FP16_FORMAT_ALTERNATIVE}
1006 };
1007
1008
1009 struct abi_name
1010 {
1011   const char *name;
1012   enum arm_abi_type abi_type;
1013 };
1014
1015
1016 /* Available values for -mabi=.  */
1017
1018 static const struct abi_name arm_all_abis[] =
1019 {
1020   {"apcs-gnu",    ARM_ABI_APCS},
1021   {"atpcs",   ARM_ABI_ATPCS},
1022   {"aapcs",   ARM_ABI_AAPCS},
1023   {"iwmmxt",  ARM_ABI_IWMMXT},
1024   {"aapcs-linux",   ARM_ABI_AAPCS_LINUX}
1025 };
1026
1027 /* Supported TLS relocations.  */
1028
1029 enum tls_reloc {
1030   TLS_GD32,
1031   TLS_LDM32,
1032   TLS_LDO32,
1033   TLS_IE32,
1034   TLS_LE32
1035 };
1036
1037 /* The maximum number of insns to be used when loading a constant.  */
1038 inline static int
1039 arm_constant_limit (bool size_p)
1040 {
1041   return size_p ? 1 : current_tune->constant_limit;
1042 }
1043
1044 /* Emit an insn that's a simple single-set.  Both the operands must be known
1045    to be valid.  */
1046 inline static rtx
1047 emit_set_insn (rtx x, rtx y)
1048 {
1049   return emit_insn (gen_rtx_SET (VOIDmode, x, y));
1050 }
1051
1052 /* Return the number of bits set in VALUE.  */
1053 static unsigned
1054 bit_count (unsigned long value)
1055 {
1056   unsigned long count = 0;
1057
1058   while (value)
1059     {
1060       count++;
1061       value &= value - 1;  /* Clear the least-significant set bit.  */
1062     }
1063
1064   return count;
1065 }
1066
1067 /* Set up library functions unique to ARM.  */
1068
1069 static void
1070 arm_init_libfuncs (void)
1071 {
1072   /* There are no special library functions unless we are using the
1073      ARM BPABI.  */
1074   if (!TARGET_BPABI)
1075     return;
1076
1077   /* The functions below are described in Section 4 of the "Run-Time
1078      ABI for the ARM architecture", Version 1.0.  */
1079
1080   /* Double-precision floating-point arithmetic.  Table 2.  */
1081   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
1082   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
1083   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
1084   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
1085   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
1086
1087   /* Double-precision comparisons.  Table 3.  */
1088   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
1089   set_optab_libfunc (ne_optab, DFmode, NULL);
1090   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
1091   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
1092   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
1093   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
1094   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
1095
1096   /* Single-precision floating-point arithmetic.  Table 4.  */
1097   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
1098   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
1099   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
1100   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
1101   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
1102
1103   /* Single-precision comparisons.  Table 5.  */
1104   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
1105   set_optab_libfunc (ne_optab, SFmode, NULL);
1106   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
1107   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
1108   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
1109   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
1110   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
1111
1112   /* Floating-point to integer conversions.  Table 6.  */
1113   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
1114   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
1115   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
1116   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
1117   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
1118   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
1119   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
1120   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
1121
1122   /* Conversions between floating types.  Table 7.  */
1123   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
1124   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
1125
1126   /* Integer to floating-point conversions.  Table 8.  */
1127   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
1128   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
1129   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
1130   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
1131   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
1132   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
1133   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
1134   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
1135
1136   /* Long long.  Table 9.  */
1137   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
1138   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
1139   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
1140   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
1141   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
1142   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
1143   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
1144   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
1145
1146   /* Integer (32/32->32) division.  \S 4.3.1.  */
1147   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
1148   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
1149
1150   /* The divmod functions are designed so that they can be used for
1151      plain division, even though they return both the quotient and the
1152      remainder.  The quotient is returned in the usual location (i.e.,
1153      r0 for SImode, {r0, r1} for DImode), just as would be expected
1154      for an ordinary division routine.  Because the AAPCS calling
1155      conventions specify that all of { r0, r1, r2, r3 } are
1156      callee-saved registers, there is no need to tell the compiler
1157      explicitly that those registers are clobbered by these
1158      routines.  */
1159   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
1160   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
1161
1162   /* For SImode division the ABI provides div-without-mod routines,
1163      which are faster.  */
1164   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
1165   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
1166
1167   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
1168      divmod libcalls instead.  */
1169   set_optab_libfunc (smod_optab, DImode, NULL);
1170   set_optab_libfunc (umod_optab, DImode, NULL);
1171   set_optab_libfunc (smod_optab, SImode, NULL);
1172   set_optab_libfunc (umod_optab, SImode, NULL);
1173
1174   /* Half-precision float operations.  The compiler handles all operations
1175      with NULL libfuncs by converting the SFmode.  */
1176   switch (arm_fp16_format)
1177     {
1178     case ARM_FP16_FORMAT_IEEE:
1179     case ARM_FP16_FORMAT_ALTERNATIVE:
1180
1181       /* Conversions.  */
1182       set_conv_libfunc (trunc_optab, HFmode, SFmode,
1183                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1184                          ? "__gnu_f2h_ieee"
1185                          : "__gnu_f2h_alternative"));
1186       set_conv_libfunc (sext_optab, SFmode, HFmode, 
1187                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1188                          ? "__gnu_h2f_ieee"
1189                          : "__gnu_h2f_alternative"));
1190       
1191       /* Arithmetic.  */
1192       set_optab_libfunc (add_optab, HFmode, NULL);
1193       set_optab_libfunc (sdiv_optab, HFmode, NULL);
1194       set_optab_libfunc (smul_optab, HFmode, NULL);
1195       set_optab_libfunc (neg_optab, HFmode, NULL);
1196       set_optab_libfunc (sub_optab, HFmode, NULL);
1197
1198       /* Comparisons.  */
1199       set_optab_libfunc (eq_optab, HFmode, NULL);
1200       set_optab_libfunc (ne_optab, HFmode, NULL);
1201       set_optab_libfunc (lt_optab, HFmode, NULL);
1202       set_optab_libfunc (le_optab, HFmode, NULL);
1203       set_optab_libfunc (ge_optab, HFmode, NULL);
1204       set_optab_libfunc (gt_optab, HFmode, NULL);
1205       set_optab_libfunc (unord_optab, HFmode, NULL);
1206       break;
1207
1208     default:
1209       break;
1210     }
1211
1212   if (TARGET_AAPCS_BASED)
1213     synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
1214 }
1215
1216 /* On AAPCS systems, this is the "struct __va_list".  */
1217 static GTY(()) tree va_list_type;
1218
1219 /* Return the type to use as __builtin_va_list.  */
1220 static tree
1221 arm_build_builtin_va_list (void)
1222 {
1223   tree va_list_name;
1224   tree ap_field;
1225   
1226   if (!TARGET_AAPCS_BASED)
1227     return std_build_builtin_va_list ();
1228
1229   /* AAPCS \S 7.1.4 requires that va_list be a typedef for a type
1230      defined as:
1231
1232        struct __va_list 
1233        {
1234          void *__ap;
1235        };
1236
1237      The C Library ABI further reinforces this definition in \S
1238      4.1.
1239
1240      We must follow this definition exactly.  The structure tag
1241      name is visible in C++ mangled names, and thus forms a part
1242      of the ABI.  The field name may be used by people who
1243      #include <stdarg.h>.  */
1244   /* Create the type.  */
1245   va_list_type = lang_hooks.types.make_type (RECORD_TYPE);
1246   /* Give it the required name.  */
1247   va_list_name = build_decl (BUILTINS_LOCATION,
1248                              TYPE_DECL,
1249                              get_identifier ("__va_list"),
1250                              va_list_type);
1251   DECL_ARTIFICIAL (va_list_name) = 1;
1252   TYPE_NAME (va_list_type) = va_list_name;
1253   TYPE_STUB_DECL (va_list_type) = va_list_name;
1254   /* Create the __ap field.  */
1255   ap_field = build_decl (BUILTINS_LOCATION,
1256                          FIELD_DECL, 
1257                          get_identifier ("__ap"),
1258                          ptr_type_node);
1259   DECL_ARTIFICIAL (ap_field) = 1;
1260   DECL_FIELD_CONTEXT (ap_field) = va_list_type;
1261   TYPE_FIELDS (va_list_type) = ap_field;
1262   /* Compute its layout.  */
1263   layout_type (va_list_type);
1264
1265   return va_list_type;
1266 }
1267
1268 /* Return an expression of type "void *" pointing to the next
1269    available argument in a variable-argument list.  VALIST is the
1270    user-level va_list object, of type __builtin_va_list.  */
1271 static tree
1272 arm_extract_valist_ptr (tree valist)
1273 {
1274   if (TREE_TYPE (valist) == error_mark_node)
1275     return error_mark_node;
1276
1277   /* On an AAPCS target, the pointer is stored within "struct
1278      va_list".  */
1279   if (TARGET_AAPCS_BASED)
1280     {
1281       tree ap_field = TYPE_FIELDS (TREE_TYPE (valist));
1282       valist = build3 (COMPONENT_REF, TREE_TYPE (ap_field), 
1283                        valist, ap_field, NULL_TREE);
1284     }
1285
1286   return valist;
1287 }
1288
1289 /* Implement TARGET_EXPAND_BUILTIN_VA_START.  */
1290 static void
1291 arm_expand_builtin_va_start (tree valist, rtx nextarg)
1292 {
1293   valist = arm_extract_valist_ptr (valist);
1294   std_expand_builtin_va_start (valist, nextarg);
1295 }
1296
1297 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR.  */
1298 static tree
1299 arm_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p, 
1300                           gimple_seq *post_p)
1301 {
1302   valist = arm_extract_valist_ptr (valist);
1303   return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
1304 }
1305
1306 /* Lookup NAME in SEL.  */
1307
1308 static const struct processors *
1309 arm_find_cpu (const char *name, const struct processors *sel, const char *desc)
1310 {
1311   if (!(name && *name))
1312     return NULL;
1313
1314   for (; sel->name != NULL; sel++)
1315     {
1316       if (streq (name, sel->name))
1317         return sel;
1318     }
1319
1320   error ("bad value (%s) for %s switch", name, desc);
1321   return NULL;
1322 }
1323
1324 /* Implement TARGET_HANDLE_OPTION.  */
1325
1326 static bool
1327 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
1328 {
1329   switch (code)
1330     {
1331     case OPT_march_:
1332       arm_selected_arch = arm_find_cpu(arg, all_architectures, "-march");
1333       return true;
1334
1335     case OPT_mcpu_:
1336       arm_selected_cpu = arm_find_cpu(arg, all_cores, "-mcpu");
1337       return true;
1338
1339     case OPT_mhard_float:
1340       target_float_abi_name = "hard";
1341       return true;
1342
1343     case OPT_msoft_float:
1344       target_float_abi_name = "soft";
1345       return true;
1346
1347     case OPT_mtune_:
1348       arm_selected_tune = arm_find_cpu(arg, all_cores, "-mtune");
1349       return true;
1350
1351     default:
1352       return true;
1353     }
1354 }
1355
1356 static void
1357 arm_target_help (void)
1358 {
1359   int i;
1360   static int columns = 0;
1361   int remaining;
1362
1363   /* If we have not done so already, obtain the desired maximum width of
1364      the output.  Note - this is a duplication of the code at the start of
1365      gcc/opts.c:print_specific_help() - the two copies should probably be
1366      replaced by a single function.  */
1367   if (columns == 0)
1368     {
1369       const char *p;
1370
1371       p = getenv ("COLUMNS");
1372       if (p != NULL)
1373         {
1374           int value = atoi (p);
1375
1376           if (value > 0)
1377             columns = value;
1378         }
1379
1380       if (columns == 0)
1381         /* Use a reasonable default.  */
1382         columns = 80;
1383     }
1384
1385   printf ("  Known ARM CPUs (for use with the -mcpu= and -mtune= options):\n");
1386
1387   /* The - 2 is because we know that the last entry in the array is NULL.  */
1388   i = ARRAY_SIZE (all_cores) - 2;
1389   gcc_assert (i > 0);
1390   printf ("    %s", all_cores[i].name);
1391   remaining = columns - (strlen (all_cores[i].name) + 4);
1392   gcc_assert (remaining >= 0);
1393
1394   while (i--)
1395     {
1396       int len = strlen (all_cores[i].name);
1397
1398       if (remaining > len + 2)
1399         {
1400           printf (", %s", all_cores[i].name);
1401           remaining -= len + 2;
1402         }
1403       else
1404         {
1405           if (remaining > 0)
1406             printf (",");
1407           printf ("\n    %s", all_cores[i].name);
1408           remaining = columns - (len + 4);
1409         }
1410     }
1411
1412   printf ("\n\n  Known ARM architectures (for use with the -march= option):\n");
1413
1414   i = ARRAY_SIZE (all_architectures) - 2;
1415   gcc_assert (i > 0);
1416   
1417   printf ("    %s", all_architectures[i].name);
1418   remaining = columns - (strlen (all_architectures[i].name) + 4);
1419   gcc_assert (remaining >= 0);
1420
1421   while (i--)
1422     {
1423       int len = strlen (all_architectures[i].name);
1424
1425       if (remaining > len + 2)
1426         {
1427           printf (", %s", all_architectures[i].name);
1428           remaining -= len + 2;
1429         }
1430       else
1431         {
1432           if (remaining > 0)
1433             printf (",");
1434           printf ("\n    %s", all_architectures[i].name);
1435           remaining = columns - (len + 4);
1436         }
1437     }
1438   printf ("\n");
1439
1440 }
1441
1442 /* Fix up any incompatible options that the user has specified.  */
1443 static void
1444 arm_option_override (void)
1445 {
1446   unsigned i;
1447
1448 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1449   SUBTARGET_OVERRIDE_OPTIONS;
1450 #endif
1451
1452   if (arm_selected_arch)
1453     {
1454       if (arm_selected_cpu)
1455         {
1456           /* Check for conflict between mcpu and march.  */
1457           if ((arm_selected_cpu->flags ^ arm_selected_arch->flags) & ~FL_TUNE)
1458             {
1459               warning (0, "switch -mcpu=%s conflicts with -march=%s switch",
1460                        arm_selected_cpu->name, arm_selected_arch->name);
1461               /* -march wins for code generation.
1462                  -mcpu wins for default tuning.  */
1463               if (!arm_selected_tune)
1464                 arm_selected_tune = arm_selected_cpu;
1465
1466               arm_selected_cpu = arm_selected_arch;
1467             }
1468           else
1469             /* -mcpu wins.  */
1470             arm_selected_arch = NULL;
1471         }
1472       else
1473         /* Pick a CPU based on the architecture.  */
1474         arm_selected_cpu = arm_selected_arch;
1475     }
1476
1477   /* If the user did not specify a processor, choose one for them.  */
1478   if (!arm_selected_cpu)
1479     {
1480       const struct processors * sel;
1481       unsigned int        sought;
1482
1483       arm_selected_cpu = &all_cores[TARGET_CPU_DEFAULT];
1484       if (!arm_selected_cpu->name)
1485         {
1486 #ifdef SUBTARGET_CPU_DEFAULT
1487           /* Use the subtarget default CPU if none was specified by
1488              configure.  */
1489           arm_selected_cpu = &all_cores[SUBTARGET_CPU_DEFAULT];
1490 #endif
1491           /* Default to ARM6.  */
1492           if (!arm_selected_cpu->name)
1493             arm_selected_cpu = &all_cores[arm6];
1494         }
1495
1496       sel = arm_selected_cpu;
1497       insn_flags = sel->flags;
1498
1499       /* Now check to see if the user has specified some command line
1500          switch that require certain abilities from the cpu.  */
1501       sought = 0;
1502
1503       if (TARGET_INTERWORK || TARGET_THUMB)
1504         {
1505           sought |= (FL_THUMB | FL_MODE32);
1506
1507           /* There are no ARM processors that support both APCS-26 and
1508              interworking.  Therefore we force FL_MODE26 to be removed
1509              from insn_flags here (if it was set), so that the search
1510              below will always be able to find a compatible processor.  */
1511           insn_flags &= ~FL_MODE26;
1512         }
1513
1514       if (sought != 0 && ((sought & insn_flags) != sought))
1515         {
1516           /* Try to locate a CPU type that supports all of the abilities
1517              of the default CPU, plus the extra abilities requested by
1518              the user.  */
1519           for (sel = all_cores; sel->name != NULL; sel++)
1520             if ((sel->flags & sought) == (sought | insn_flags))
1521               break;
1522
1523           if (sel->name == NULL)
1524             {
1525               unsigned current_bit_count = 0;
1526               const struct processors * best_fit = NULL;
1527
1528               /* Ideally we would like to issue an error message here
1529                  saying that it was not possible to find a CPU compatible
1530                  with the default CPU, but which also supports the command
1531                  line options specified by the programmer, and so they
1532                  ought to use the -mcpu=<name> command line option to
1533                  override the default CPU type.
1534
1535                  If we cannot find a cpu that has both the
1536                  characteristics of the default cpu and the given
1537                  command line options we scan the array again looking
1538                  for a best match.  */
1539               for (sel = all_cores; sel->name != NULL; sel++)
1540                 if ((sel->flags & sought) == sought)
1541                   {
1542                     unsigned count;
1543
1544                     count = bit_count (sel->flags & insn_flags);
1545
1546                     if (count >= current_bit_count)
1547                       {
1548                         best_fit = sel;
1549                         current_bit_count = count;
1550                       }
1551                   }
1552
1553               gcc_assert (best_fit);
1554               sel = best_fit;
1555             }
1556
1557           arm_selected_cpu = sel;
1558         }
1559     }
1560
1561   gcc_assert (arm_selected_cpu);
1562   /* The selected cpu may be an architecture, so lookup tuning by core ID.  */
1563   if (!arm_selected_tune)
1564     arm_selected_tune = &all_cores[arm_selected_cpu->core];
1565
1566   sprintf (arm_arch_name, "__ARM_ARCH_%s__", arm_selected_cpu->arch);
1567   insn_flags = arm_selected_cpu->flags;
1568
1569   arm_tune = arm_selected_tune->core;
1570   tune_flags = arm_selected_tune->flags;
1571   current_tune = arm_selected_tune->tune;
1572
1573   if (target_fp16_format_name)
1574     {
1575       for (i = 0; i < ARRAY_SIZE (all_fp16_formats); i++)
1576         {
1577           if (streq (all_fp16_formats[i].name, target_fp16_format_name))
1578             {
1579               arm_fp16_format = all_fp16_formats[i].fp16_format_type;
1580               break;
1581             }
1582         }
1583       if (i == ARRAY_SIZE (all_fp16_formats))
1584         error ("invalid __fp16 format option: -mfp16-format=%s",
1585                target_fp16_format_name);
1586     }
1587   else
1588     arm_fp16_format = ARM_FP16_FORMAT_NONE;
1589
1590   if (target_abi_name)
1591     {
1592       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1593         {
1594           if (streq (arm_all_abis[i].name, target_abi_name))
1595             {
1596               arm_abi = arm_all_abis[i].abi_type;
1597               break;
1598             }
1599         }
1600       if (i == ARRAY_SIZE (arm_all_abis))
1601         error ("invalid ABI option: -mabi=%s", target_abi_name);
1602     }
1603   else
1604     arm_abi = ARM_DEFAULT_ABI;
1605
1606   /* Make sure that the processor choice does not conflict with any of the
1607      other command line choices.  */
1608   if (TARGET_ARM && !(insn_flags & FL_NOTM))
1609     error ("target CPU does not support ARM mode");
1610
1611   /* BPABI targets use linker tricks to allow interworking on cores
1612      without thumb support.  */
1613   if (TARGET_INTERWORK && !((insn_flags & FL_THUMB) || TARGET_BPABI))
1614     {
1615       warning (0, "target CPU does not support interworking" );
1616       target_flags &= ~MASK_INTERWORK;
1617     }
1618
1619   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1620     {
1621       warning (0, "target CPU does not support THUMB instructions");
1622       target_flags &= ~MASK_THUMB;
1623     }
1624
1625   if (TARGET_APCS_FRAME && TARGET_THUMB)
1626     {
1627       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1628       target_flags &= ~MASK_APCS_FRAME;
1629     }
1630
1631   /* Callee super interworking implies thumb interworking.  Adding
1632      this to the flags here simplifies the logic elsewhere.  */
1633   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1634     target_flags |= MASK_INTERWORK;
1635
1636   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1637      from here where no function is being compiled currently.  */
1638   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1639     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1640
1641   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1642     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1643
1644   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1645     {
1646       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1647       target_flags |= MASK_APCS_FRAME;
1648     }
1649
1650   if (TARGET_POKE_FUNCTION_NAME)
1651     target_flags |= MASK_APCS_FRAME;
1652
1653   if (TARGET_APCS_REENT && flag_pic)
1654     error ("-fpic and -mapcs-reent are incompatible");
1655
1656   if (TARGET_APCS_REENT)
1657     warning (0, "APCS reentrant code not supported.  Ignored");
1658
1659   /* If this target is normally configured to use APCS frames, warn if they
1660      are turned off and debugging is turned on.  */
1661   if (TARGET_ARM
1662       && write_symbols != NO_DEBUG
1663       && !TARGET_APCS_FRAME
1664       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1665     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1666
1667   if (TARGET_APCS_FLOAT)
1668     warning (0, "passing floating point arguments in fp regs not yet supported");
1669
1670   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1671   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1672   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1673   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1674   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1675   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1676   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1677   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1678   arm_arch_notm = (insn_flags & FL_NOTM) != 0;
1679   arm_arch7 = (insn_flags & FL_ARCH7) != 0;
1680   arm_arch7em = (insn_flags & FL_ARCH7EM) != 0;
1681   arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
1682   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1683   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1684
1685   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1686   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1687   thumb_code = TARGET_ARM == 0;
1688   thumb1_code = TARGET_THUMB1 != 0;
1689   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1690   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1691   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1692   arm_arch_hwdiv = (insn_flags & FL_DIV) != 0;
1693   arm_tune_cortex_a9 = (arm_tune == cortexa9) != 0;
1694
1695   /* If we are not using the default (ARM mode) section anchor offset
1696      ranges, then set the correct ranges now.  */
1697   if (TARGET_THUMB1)
1698     {
1699       /* Thumb-1 LDR instructions cannot have negative offsets.
1700          Permissible positive offset ranges are 5-bit (for byte loads),
1701          6-bit (for halfword loads), or 7-bit (for word loads).
1702          Empirical results suggest a 7-bit anchor range gives the best
1703          overall code size.  */
1704       targetm.min_anchor_offset = 0;
1705       targetm.max_anchor_offset = 127;
1706     }
1707   else if (TARGET_THUMB2)
1708     {
1709       /* The minimum is set such that the total size of the block
1710          for a particular anchor is 248 + 1 + 4095 bytes, which is
1711          divisible by eight, ensuring natural spacing of anchors.  */
1712       targetm.min_anchor_offset = -248;
1713       targetm.max_anchor_offset = 4095;
1714     }
1715
1716   /* V5 code we generate is completely interworking capable, so we turn off
1717      TARGET_INTERWORK here to avoid many tests later on.  */
1718
1719   /* XXX However, we must pass the right pre-processor defines to CPP
1720      or GLD can get confused.  This is a hack.  */
1721   if (TARGET_INTERWORK)
1722     arm_cpp_interwork = 1;
1723
1724   if (arm_arch5)
1725     target_flags &= ~MASK_INTERWORK;
1726
1727   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1728     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1729
1730   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1731     error ("iwmmxt abi requires an iwmmxt capable cpu");
1732
1733   if (target_fpu_name == NULL && target_fpe_name != NULL)
1734     {
1735       if (streq (target_fpe_name, "2"))
1736         target_fpu_name = "fpe2";
1737       else if (streq (target_fpe_name, "3"))
1738         target_fpu_name = "fpe3";
1739       else
1740         error ("invalid floating point emulation option: -mfpe=%s",
1741                target_fpe_name);
1742     }
1743
1744   if (target_fpu_name == NULL)
1745     {
1746 #ifdef FPUTYPE_DEFAULT
1747       target_fpu_name = FPUTYPE_DEFAULT;
1748 #else
1749       if (arm_arch_cirrus)
1750         target_fpu_name = "maverick";
1751       else
1752         target_fpu_name = "fpe2";
1753 #endif
1754     }
1755
1756   arm_fpu_desc = NULL;
1757   for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1758     {
1759       if (streq (all_fpus[i].name, target_fpu_name))
1760         {
1761           arm_fpu_desc = &all_fpus[i];
1762           break;
1763         }
1764     }
1765
1766   if (!arm_fpu_desc)
1767     {
1768       error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1769       return;
1770     }
1771
1772   switch (arm_fpu_desc->model)
1773     {
1774     case ARM_FP_MODEL_FPA:
1775       if (arm_fpu_desc->rev == 2)
1776         arm_fpu_attr = FPU_FPE2;
1777       else if (arm_fpu_desc->rev == 3)
1778         arm_fpu_attr = FPU_FPE3;
1779       else
1780         arm_fpu_attr = FPU_FPA;
1781       break;
1782
1783     case ARM_FP_MODEL_MAVERICK:
1784       arm_fpu_attr = FPU_MAVERICK;
1785       break;
1786
1787     case ARM_FP_MODEL_VFP:
1788       arm_fpu_attr = FPU_VFP;
1789       break;
1790
1791     default:
1792       gcc_unreachable();
1793     }
1794
1795   if (target_float_abi_name != NULL)
1796     {
1797       /* The user specified a FP ABI.  */
1798       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1799         {
1800           if (streq (all_float_abis[i].name, target_float_abi_name))
1801             {
1802               arm_float_abi = all_float_abis[i].abi_type;
1803               break;
1804             }
1805         }
1806       if (i == ARRAY_SIZE (all_float_abis))
1807         error ("invalid floating point abi: -mfloat-abi=%s",
1808                target_float_abi_name);
1809     }
1810   else
1811     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1812
1813   if (TARGET_AAPCS_BASED
1814       && (arm_fpu_desc->model == ARM_FP_MODEL_FPA))
1815     error ("FPA is unsupported in the AAPCS");
1816
1817   if (TARGET_AAPCS_BASED)
1818     {
1819       if (TARGET_CALLER_INTERWORKING)
1820         error ("AAPCS does not support -mcaller-super-interworking");
1821       else
1822         if (TARGET_CALLEE_INTERWORKING)
1823           error ("AAPCS does not support -mcallee-super-interworking");
1824     }
1825
1826   /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1827      VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1828      will ever exist.  GCC makes no attempt to support this combination.  */
1829   if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1830     sorry ("iWMMXt and hardware floating point");
1831
1832   /* ??? iWMMXt insn patterns need auditing for Thumb-2.  */
1833   if (TARGET_THUMB2 && TARGET_IWMMXT)
1834     sorry ("Thumb-2 iWMMXt");
1835
1836   /* __fp16 support currently assumes the core has ldrh.  */
1837   if (!arm_arch4 && arm_fp16_format != ARM_FP16_FORMAT_NONE)
1838     sorry ("__fp16 and no ldrh");
1839
1840   /* If soft-float is specified then don't use FPU.  */
1841   if (TARGET_SOFT_FLOAT)
1842     arm_fpu_attr = FPU_NONE;
1843
1844   if (TARGET_AAPCS_BASED)
1845     {
1846       if (arm_abi == ARM_ABI_IWMMXT)
1847         arm_pcs_default = ARM_PCS_AAPCS_IWMMXT;
1848       else if (arm_float_abi == ARM_FLOAT_ABI_HARD
1849                && TARGET_HARD_FLOAT
1850                && TARGET_VFP)
1851         arm_pcs_default = ARM_PCS_AAPCS_VFP;
1852       else
1853         arm_pcs_default = ARM_PCS_AAPCS;
1854     }
1855   else
1856     {
1857       if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1858         sorry ("-mfloat-abi=hard and VFP");
1859
1860       if (arm_abi == ARM_ABI_APCS)
1861         arm_pcs_default = ARM_PCS_APCS;
1862       else
1863         arm_pcs_default = ARM_PCS_ATPCS;
1864     }
1865
1866   /* For arm2/3 there is no need to do any scheduling if there is only
1867      a floating point emulator, or we are doing software floating-point.  */
1868   if ((TARGET_SOFT_FLOAT
1869        || (TARGET_FPA && arm_fpu_desc->rev))
1870       && (tune_flags & FL_MODE32) == 0)
1871     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1872
1873   if (target_thread_switch)
1874     {
1875       if (strcmp (target_thread_switch, "soft") == 0)
1876         target_thread_pointer = TP_SOFT;
1877       else if (strcmp (target_thread_switch, "auto") == 0)
1878         target_thread_pointer = TP_AUTO;
1879       else if (strcmp (target_thread_switch, "cp15") == 0)
1880         target_thread_pointer = TP_CP15;
1881       else
1882         error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1883     }
1884
1885   /* Use the cp15 method if it is available.  */
1886   if (target_thread_pointer == TP_AUTO)
1887     {
1888       if (arm_arch6k && !TARGET_THUMB1)
1889         target_thread_pointer = TP_CP15;
1890       else
1891         target_thread_pointer = TP_SOFT;
1892     }
1893
1894   if (TARGET_HARD_TP && TARGET_THUMB1)
1895     error ("can not use -mtp=cp15 with 16-bit Thumb");
1896
1897   /* Override the default structure alignment for AAPCS ABI.  */
1898   if (TARGET_AAPCS_BASED)
1899     arm_structure_size_boundary = 8;
1900
1901   if (structure_size_string != NULL)
1902     {
1903       int size = strtol (structure_size_string, NULL, 0);
1904
1905       if (size == 8 || size == 32
1906           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1907         arm_structure_size_boundary = size;
1908       else
1909         warning (0, "structure size boundary can only be set to %s",
1910                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1911     }
1912
1913   if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
1914     {
1915       error ("RTP PIC is incompatible with Thumb");
1916       flag_pic = 0;
1917     }
1918
1919   /* If stack checking is disabled, we can use r10 as the PIC register,
1920      which keeps r9 available.  The EABI specifies r9 as the PIC register.  */
1921   if (flag_pic && TARGET_SINGLE_PIC_BASE)
1922     {
1923       if (TARGET_VXWORKS_RTP)
1924         warning (0, "RTP PIC is incompatible with -msingle-pic-base");
1925       arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1926     }
1927
1928   if (flag_pic && TARGET_VXWORKS_RTP)
1929     arm_pic_register = 9;
1930
1931   if (arm_pic_register_string != NULL)
1932     {
1933       int pic_register = decode_reg_name (arm_pic_register_string);
1934
1935       if (!flag_pic)
1936         warning (0, "-mpic-register= is useless without -fpic");
1937
1938       /* Prevent the user from choosing an obviously stupid PIC register.  */
1939       else if (pic_register < 0 || call_used_regs[pic_register]
1940                || pic_register == HARD_FRAME_POINTER_REGNUM
1941                || pic_register == STACK_POINTER_REGNUM
1942                || pic_register >= PC_REGNUM
1943                || (TARGET_VXWORKS_RTP
1944                    && (unsigned int) pic_register != arm_pic_register))
1945         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1946       else
1947         arm_pic_register = pic_register;
1948     }
1949
1950   /* Enable -mfix-cortex-m3-ldrd by default for Cortex-M3 cores.  */
1951   if (fix_cm3_ldrd == 2)
1952     {
1953       if (arm_selected_cpu->core == cortexm3)
1954         fix_cm3_ldrd = 1;
1955       else
1956         fix_cm3_ldrd = 0;
1957     }
1958
1959   if (TARGET_THUMB1 && flag_schedule_insns)
1960     {
1961       /* Don't warn since it's on by default in -O2.  */
1962       flag_schedule_insns = 0;
1963     }
1964
1965   if (optimize_size)
1966     {
1967       /* If optimizing for size, bump the number of instructions that we
1968          are prepared to conditionally execute (even on a StrongARM).  */
1969       max_insns_skipped = 6;
1970     }
1971   else
1972     {
1973       /* StrongARM has early execution of branches, so a sequence
1974          that is worth skipping is shorter.  */
1975       if (arm_tune_strongarm)
1976         max_insns_skipped = 3;
1977     }
1978
1979   /* Hot/Cold partitioning is not currently supported, since we can't
1980      handle literal pool placement in that case.  */
1981   if (flag_reorder_blocks_and_partition)
1982     {
1983       inform (input_location,
1984               "-freorder-blocks-and-partition not supported on this architecture");
1985       flag_reorder_blocks_and_partition = 0;
1986       flag_reorder_blocks = 1;
1987     }
1988
1989   if (flag_pic)
1990     /* Hoisting PIC address calculations more aggressively provides a small,
1991        but measurable, size reduction for PIC code.  Therefore, we decrease
1992        the bar for unrestricted expression hoisting to the cost of PIC address
1993        calculation, which is 2 instructions.  */
1994     maybe_set_param_value (PARAM_GCSE_UNRESTRICTED_COST, 2,
1995                            global_options.x_param_values,
1996                            global_options_set.x_param_values);
1997
1998   /* ARM EABI defaults to strict volatile bitfields.  */
1999   if (TARGET_AAPCS_BASED && flag_strict_volatile_bitfields < 0)
2000     flag_strict_volatile_bitfields = 1;
2001
2002   /* Enable sw prefetching at -O3 for CPUS that have prefetch, and we have deemed
2003      it beneficial (signified by setting num_prefetch_slots to 1 or more.)  */
2004   if (flag_prefetch_loop_arrays < 0
2005       && HAVE_prefetch
2006       && optimize >= 3
2007       && current_tune->num_prefetch_slots > 0)
2008     flag_prefetch_loop_arrays = 1;
2009
2010   /* Set up parameters to be used in prefetching algorithm.  Do not override the
2011      defaults unless we are tuning for a core we have researched values for.  */
2012   if (current_tune->num_prefetch_slots > 0)
2013     maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
2014                            current_tune->num_prefetch_slots,
2015                            global_options.x_param_values,
2016                            global_options_set.x_param_values);
2017   if (current_tune->l1_cache_line_size >= 0)
2018     maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
2019                            current_tune->l1_cache_line_size,
2020                            global_options.x_param_values,
2021                            global_options_set.x_param_values);
2022   if (current_tune->l1_cache_size >= 0)
2023     maybe_set_param_value (PARAM_L1_CACHE_SIZE,
2024                            current_tune->l1_cache_size,
2025                            global_options.x_param_values,
2026                            global_options_set.x_param_values);
2027
2028   /* Register global variables with the garbage collector.  */
2029   arm_add_gc_roots ();
2030 }
2031
2032 static void
2033 arm_add_gc_roots (void)
2034 {
2035   gcc_obstack_init(&minipool_obstack);
2036   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
2037 }
2038 \f
2039 /* A table of known ARM exception types.
2040    For use with the interrupt function attribute.  */
2041
2042 typedef struct
2043 {
2044   const char *const arg;
2045   const unsigned long return_value;
2046 }
2047 isr_attribute_arg;
2048
2049 static const isr_attribute_arg isr_attribute_args [] =
2050 {
2051   { "IRQ",   ARM_FT_ISR },
2052   { "irq",   ARM_FT_ISR },
2053   { "FIQ",   ARM_FT_FIQ },
2054   { "fiq",   ARM_FT_FIQ },
2055   { "ABORT", ARM_FT_ISR },
2056   { "abort", ARM_FT_ISR },
2057   { "ABORT", ARM_FT_ISR },
2058   { "abort", ARM_FT_ISR },
2059   { "UNDEF", ARM_FT_EXCEPTION },
2060   { "undef", ARM_FT_EXCEPTION },
2061   { "SWI",   ARM_FT_EXCEPTION },
2062   { "swi",   ARM_FT_EXCEPTION },
2063   { NULL,    ARM_FT_NORMAL }
2064 };
2065
2066 /* Returns the (interrupt) function type of the current
2067    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
2068
2069 static unsigned long
2070 arm_isr_value (tree argument)
2071 {
2072   const isr_attribute_arg * ptr;
2073   const char *              arg;
2074
2075   if (!arm_arch_notm)
2076     return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
2077
2078   /* No argument - default to IRQ.  */
2079   if (argument == NULL_TREE)
2080     return ARM_FT_ISR;
2081
2082   /* Get the value of the argument.  */
2083   if (TREE_VALUE (argument) == NULL_TREE
2084       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
2085     return ARM_FT_UNKNOWN;
2086
2087   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
2088
2089   /* Check it against the list of known arguments.  */
2090   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
2091     if (streq (arg, ptr->arg))
2092       return ptr->return_value;
2093
2094   /* An unrecognized interrupt type.  */
2095   return ARM_FT_UNKNOWN;
2096 }
2097
2098 /* Computes the type of the current function.  */
2099
2100 static unsigned long
2101 arm_compute_func_type (void)
2102 {
2103   unsigned long type = ARM_FT_UNKNOWN;
2104   tree a;
2105   tree attr;
2106
2107   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
2108
2109   /* Decide if the current function is volatile.  Such functions
2110      never return, and many memory cycles can be saved by not storing
2111      register values that will never be needed again.  This optimization
2112      was added to speed up context switching in a kernel application.  */
2113   if (optimize > 0
2114       && (TREE_NOTHROW (current_function_decl)
2115           || !(flag_unwind_tables
2116                || (flag_exceptions
2117                    && arm_except_unwind_info (&global_options) != UI_SJLJ)))
2118       && TREE_THIS_VOLATILE (current_function_decl))
2119     type |= ARM_FT_VOLATILE;
2120
2121   if (cfun->static_chain_decl != NULL)
2122     type |= ARM_FT_NESTED;
2123
2124   attr = DECL_ATTRIBUTES (current_function_decl);
2125
2126   a = lookup_attribute ("naked", attr);
2127   if (a != NULL_TREE)
2128     type |= ARM_FT_NAKED;
2129
2130   a = lookup_attribute ("isr", attr);
2131   if (a == NULL_TREE)
2132     a = lookup_attribute ("interrupt", attr);
2133
2134   if (a == NULL_TREE)
2135     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
2136   else
2137     type |= arm_isr_value (TREE_VALUE (a));
2138
2139   return type;
2140 }
2141
2142 /* Returns the type of the current function.  */
2143
2144 unsigned long
2145 arm_current_func_type (void)
2146 {
2147   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
2148     cfun->machine->func_type = arm_compute_func_type ();
2149
2150   return cfun->machine->func_type;
2151 }
2152
2153 bool
2154 arm_allocate_stack_slots_for_args (void)
2155 {
2156   /* Naked functions should not allocate stack slots for arguments.  */
2157   return !IS_NAKED (arm_current_func_type ());
2158 }
2159
2160 \f
2161 /* Output assembler code for a block containing the constant parts
2162    of a trampoline, leaving space for the variable parts.
2163
2164    On the ARM, (if r8 is the static chain regnum, and remembering that
2165    referencing pc adds an offset of 8) the trampoline looks like:
2166            ldr          r8, [pc, #0]
2167            ldr          pc, [pc]
2168            .word        static chain value
2169            .word        function's address
2170    XXX FIXME: When the trampoline returns, r8 will be clobbered.  */
2171
2172 static void
2173 arm_asm_trampoline_template (FILE *f)
2174 {
2175   if (TARGET_ARM)
2176     {
2177       asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", STATIC_CHAIN_REGNUM, PC_REGNUM);
2178       asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", PC_REGNUM, PC_REGNUM);
2179     }
2180   else if (TARGET_THUMB2)
2181     {
2182       /* The Thumb-2 trampoline is similar to the arm implementation.
2183          Unlike 16-bit Thumb, we enter the stub in thumb mode.  */
2184       asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n",
2185                    STATIC_CHAIN_REGNUM, PC_REGNUM);
2186       asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n", PC_REGNUM, PC_REGNUM);
2187     }
2188   else
2189     {
2190       ASM_OUTPUT_ALIGN (f, 2);
2191       fprintf (f, "\t.code\t16\n");
2192       fprintf (f, ".Ltrampoline_start:\n");
2193       asm_fprintf (f, "\tpush\t{r0, r1}\n");
2194       asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2195       asm_fprintf (f, "\tmov\t%r, r0\n", STATIC_CHAIN_REGNUM);
2196       asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2197       asm_fprintf (f, "\tstr\tr0, [%r, #4]\n", SP_REGNUM);
2198       asm_fprintf (f, "\tpop\t{r0, %r}\n", PC_REGNUM);
2199     }
2200   assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2201   assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2202 }
2203
2204 /* Emit RTL insns to initialize the variable parts of a trampoline.  */
2205
2206 static void
2207 arm_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2208 {
2209   rtx fnaddr, mem, a_tramp;
2210
2211   emit_block_move (m_tramp, assemble_trampoline_template (),
2212                    GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
2213
2214   mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 8 : 12);
2215   emit_move_insn (mem, chain_value);
2216
2217   mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 12 : 16);
2218   fnaddr = XEXP (DECL_RTL (fndecl), 0);
2219   emit_move_insn (mem, fnaddr);
2220
2221   a_tramp = XEXP (m_tramp, 0);
2222   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__clear_cache"),
2223                      LCT_NORMAL, VOIDmode, 2, a_tramp, Pmode,
2224                      plus_constant (a_tramp, TRAMPOLINE_SIZE), Pmode);
2225 }
2226
2227 /* Thumb trampolines should be entered in thumb mode, so set
2228    the bottom bit of the address.  */
2229
2230 static rtx
2231 arm_trampoline_adjust_address (rtx addr)
2232 {
2233   if (TARGET_THUMB)
2234     addr = expand_simple_binop (Pmode, IOR, addr, const1_rtx,
2235                                 NULL, 0, OPTAB_LIB_WIDEN);
2236   return addr;
2237 }
2238 \f
2239 /* Return 1 if it is possible to return using a single instruction.
2240    If SIBLING is non-null, this is a test for a return before a sibling
2241    call.  SIBLING is the call insn, so we can examine its register usage.  */
2242
2243 int
2244 use_return_insn (int iscond, rtx sibling)
2245 {
2246   int regno;
2247   unsigned int func_type;
2248   unsigned long saved_int_regs;
2249   unsigned HOST_WIDE_INT stack_adjust;
2250   arm_stack_offsets *offsets;
2251
2252   /* Never use a return instruction before reload has run.  */
2253   if (!reload_completed)
2254     return 0;
2255
2256   func_type = arm_current_func_type ();
2257
2258   /* Naked, volatile and stack alignment functions need special
2259      consideration.  */
2260   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
2261     return 0;
2262
2263   /* So do interrupt functions that use the frame pointer and Thumb
2264      interrupt functions.  */
2265   if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
2266     return 0;
2267
2268   offsets = arm_get_frame_offsets ();
2269   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
2270
2271   /* As do variadic functions.  */
2272   if (crtl->args.pretend_args_size
2273       || cfun->machine->uses_anonymous_args
2274       /* Or if the function calls __builtin_eh_return () */
2275       || crtl->calls_eh_return
2276       /* Or if the function calls alloca */
2277       || cfun->calls_alloca
2278       /* Or if there is a stack adjustment.  However, if the stack pointer
2279          is saved on the stack, we can use a pre-incrementing stack load.  */
2280       || !(stack_adjust == 0 || (TARGET_APCS_FRAME && frame_pointer_needed
2281                                  && stack_adjust == 4)))
2282     return 0;
2283
2284   saved_int_regs = offsets->saved_regs_mask;
2285
2286   /* Unfortunately, the insn
2287
2288        ldmib sp, {..., sp, ...}
2289
2290      triggers a bug on most SA-110 based devices, such that the stack
2291      pointer won't be correctly restored if the instruction takes a
2292      page fault.  We work around this problem by popping r3 along with
2293      the other registers, since that is never slower than executing
2294      another instruction.
2295
2296      We test for !arm_arch5 here, because code for any architecture
2297      less than this could potentially be run on one of the buggy
2298      chips.  */
2299   if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
2300     {
2301       /* Validate that r3 is a call-clobbered register (always true in
2302          the default abi) ...  */
2303       if (!call_used_regs[3])
2304         return 0;
2305
2306       /* ... that it isn't being used for a return value ... */
2307       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
2308         return 0;
2309
2310       /* ... or for a tail-call argument ...  */
2311       if (sibling)
2312         {
2313           gcc_assert (GET_CODE (sibling) == CALL_INSN);
2314
2315           if (find_regno_fusage (sibling, USE, 3))
2316             return 0;
2317         }
2318
2319       /* ... and that there are no call-saved registers in r0-r2
2320          (always true in the default ABI).  */
2321       if (saved_int_regs & 0x7)
2322         return 0;
2323     }
2324
2325   /* Can't be done if interworking with Thumb, and any registers have been
2326      stacked.  */
2327   if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
2328     return 0;
2329
2330   /* On StrongARM, conditional returns are expensive if they aren't
2331      taken and multiple registers have been stacked.  */
2332   if (iscond && arm_tune_strongarm)
2333     {
2334       /* Conditional return when just the LR is stored is a simple
2335          conditional-load instruction, that's not expensive.  */
2336       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
2337         return 0;
2338
2339       if (flag_pic 
2340           && arm_pic_register != INVALID_REGNUM
2341           && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
2342         return 0;
2343     }
2344
2345   /* If there are saved registers but the LR isn't saved, then we need
2346      two instructions for the return.  */
2347   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
2348     return 0;
2349
2350   /* Can't be done if any of the FPA regs are pushed,
2351      since this also requires an insn.  */
2352   if (TARGET_HARD_FLOAT && TARGET_FPA)
2353     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
2354       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2355         return 0;
2356
2357   /* Likewise VFP regs.  */
2358   if (TARGET_HARD_FLOAT && TARGET_VFP)
2359     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
2360       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2361         return 0;
2362
2363   if (TARGET_REALLY_IWMMXT)
2364     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
2365       if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2366         return 0;
2367
2368   return 1;
2369 }
2370
2371 /* Return TRUE if int I is a valid immediate ARM constant.  */
2372
2373 int
2374 const_ok_for_arm (HOST_WIDE_INT i)
2375 {
2376   int lowbit;
2377
2378   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
2379      be all zero, or all one.  */
2380   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
2381       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
2382           != ((~(unsigned HOST_WIDE_INT) 0)
2383               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
2384     return FALSE;
2385
2386   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
2387
2388   /* Fast return for 0 and small values.  We must do this for zero, since
2389      the code below can't handle that one case.  */
2390   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
2391     return TRUE;
2392
2393   /* Get the number of trailing zeros.  */
2394   lowbit = ffs((int) i) - 1;
2395   
2396   /* Only even shifts are allowed in ARM mode so round down to the
2397      nearest even number.  */
2398   if (TARGET_ARM)
2399     lowbit &= ~1;
2400
2401   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
2402     return TRUE;
2403
2404   if (TARGET_ARM)
2405     {
2406       /* Allow rotated constants in ARM mode.  */
2407       if (lowbit <= 4
2408            && ((i & ~0xc000003f) == 0
2409                || (i & ~0xf000000f) == 0
2410                || (i & ~0xfc000003) == 0))
2411         return TRUE;
2412     }
2413   else
2414     {
2415       HOST_WIDE_INT v;
2416
2417       /* Allow repeated patterns 0x00XY00XY or 0xXYXYXYXY.  */
2418       v = i & 0xff;
2419       v |= v << 16;
2420       if (i == v || i == (v | (v << 8)))
2421         return TRUE;
2422
2423       /* Allow repeated pattern 0xXY00XY00.  */
2424       v = i & 0xff00;
2425       v |= v << 16;
2426       if (i == v)
2427         return TRUE;
2428     }
2429
2430   return FALSE;
2431 }
2432
2433 /* Return true if I is a valid constant for the operation CODE.  */
2434 static int
2435 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
2436 {
2437   if (const_ok_for_arm (i))
2438     return 1;
2439
2440   switch (code)
2441     {
2442     case PLUS:
2443     case COMPARE:
2444     case EQ:
2445     case NE:
2446     case GT:
2447     case LE:
2448     case LT:
2449     case GE:
2450     case GEU:
2451     case LTU:
2452     case GTU:
2453     case LEU:
2454     case UNORDERED:
2455     case ORDERED:
2456     case UNEQ:
2457     case UNGE:
2458     case UNLT:
2459     case UNGT:
2460     case UNLE:
2461       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
2462
2463     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
2464     case XOR:
2465       return 0;
2466
2467     case IOR:
2468       if (TARGET_THUMB2)
2469         return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2470       return 0;
2471
2472     case AND:
2473       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2474
2475     default:
2476       gcc_unreachable ();
2477     }
2478 }
2479
2480 /* Emit a sequence of insns to handle a large constant.
2481    CODE is the code of the operation required, it can be any of SET, PLUS,
2482    IOR, AND, XOR, MINUS;
2483    MODE is the mode in which the operation is being performed;
2484    VAL is the integer to operate on;
2485    SOURCE is the other operand (a register, or a null-pointer for SET);
2486    SUBTARGETS means it is safe to create scratch registers if that will
2487    either produce a simpler sequence, or we will want to cse the values.
2488    Return value is the number of insns emitted.  */
2489
2490 /* ??? Tweak this for thumb2.  */
2491 int
2492 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
2493                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
2494 {
2495   rtx cond;
2496
2497   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
2498     cond = COND_EXEC_TEST (PATTERN (insn));
2499   else
2500     cond = NULL_RTX;
2501
2502   if (subtargets || code == SET
2503       || (GET_CODE (target) == REG && GET_CODE (source) == REG
2504           && REGNO (target) != REGNO (source)))
2505     {
2506       /* After arm_reorg has been called, we can't fix up expensive
2507          constants by pushing them into memory so we must synthesize
2508          them in-line, regardless of the cost.  This is only likely to
2509          be more costly on chips that have load delay slots and we are
2510          compiling without running the scheduler (so no splitting
2511          occurred before the final instruction emission).
2512
2513          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
2514       */
2515       if (!after_arm_reorg
2516           && !cond
2517           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
2518                                 1, 0)
2519               > (arm_constant_limit (optimize_function_for_size_p (cfun))
2520                  + (code != SET))))
2521         {
2522           if (code == SET)
2523             {
2524               /* Currently SET is the only monadic value for CODE, all
2525                  the rest are diadic.  */
2526               if (TARGET_USE_MOVT)
2527                 arm_emit_movpair (target, GEN_INT (val));
2528               else
2529                 emit_set_insn (target, GEN_INT (val));
2530
2531               return 1;
2532             }
2533           else
2534             {
2535               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
2536
2537               if (TARGET_USE_MOVT)
2538                 arm_emit_movpair (temp, GEN_INT (val));
2539               else
2540                 emit_set_insn (temp, GEN_INT (val));
2541
2542               /* For MINUS, the value is subtracted from, since we never
2543                  have subtraction of a constant.  */
2544               if (code == MINUS)
2545                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
2546               else
2547                 emit_set_insn (target,
2548                                gen_rtx_fmt_ee (code, mode, source, temp));
2549               return 2;
2550             }
2551         }
2552     }
2553
2554   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
2555                            1);
2556 }
2557
2558 /* Return the number of instructions required to synthesize the given
2559    constant, if we start emitting them from bit-position I.  */
2560 static int
2561 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
2562 {
2563   HOST_WIDE_INT temp1;
2564   int step_size = TARGET_ARM ? 2 : 1;
2565   int num_insns = 0;
2566
2567   gcc_assert (TARGET_ARM || i == 0);
2568
2569   do
2570     {
2571       int end;
2572
2573       if (i <= 0)
2574         i += 32;
2575       if (remainder & (((1 << step_size) - 1) << (i - step_size)))
2576         {
2577           end = i - 8;
2578           if (end < 0)
2579             end += 32;
2580           temp1 = remainder & ((0x0ff << end)
2581                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
2582           remainder &= ~temp1;
2583           num_insns++;
2584           i -= 8 - step_size;
2585         }
2586       i -= step_size;
2587     } while (remainder);
2588   return num_insns;
2589 }
2590
2591 static int
2592 find_best_start (unsigned HOST_WIDE_INT remainder)
2593 {
2594   int best_consecutive_zeros = 0;
2595   int i;
2596   int best_start = 0;
2597
2598   /* If we aren't targetting ARM, the best place to start is always at
2599      the bottom.  */
2600   if (! TARGET_ARM)
2601     return 0;
2602
2603   for (i = 0; i < 32; i += 2)
2604     {
2605       int consecutive_zeros = 0;
2606
2607       if (!(remainder & (3 << i)))
2608         {
2609           while ((i < 32) && !(remainder & (3 << i)))
2610             {
2611               consecutive_zeros += 2;
2612               i += 2;
2613             }
2614           if (consecutive_zeros > best_consecutive_zeros)
2615             {
2616               best_consecutive_zeros = consecutive_zeros;
2617               best_start = i - consecutive_zeros;
2618             }
2619           i -= 2;
2620         }
2621     }
2622
2623   /* So long as it won't require any more insns to do so, it's
2624      desirable to emit a small constant (in bits 0...9) in the last
2625      insn.  This way there is more chance that it can be combined with
2626      a later addressing insn to form a pre-indexed load or store
2627      operation.  Consider:
2628
2629            *((volatile int *)0xe0000100) = 1;
2630            *((volatile int *)0xe0000110) = 2;
2631
2632      We want this to wind up as:
2633
2634             mov rA, #0xe0000000
2635             mov rB, #1
2636             str rB, [rA, #0x100]
2637             mov rB, #2
2638             str rB, [rA, #0x110]
2639
2640      rather than having to synthesize both large constants from scratch.
2641
2642      Therefore, we calculate how many insns would be required to emit
2643      the constant starting from `best_start', and also starting from
2644      zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2645      yield a shorter sequence, we may as well use zero.  */
2646   if (best_start != 0
2647       && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2648       && (count_insns_for_constant (remainder, 0) <=
2649           count_insns_for_constant (remainder, best_start)))
2650     best_start = 0;
2651
2652   return best_start;
2653 }
2654
2655 /* Emit an instruction with the indicated PATTERN.  If COND is
2656    non-NULL, conditionalize the execution of the instruction on COND
2657    being true.  */
2658
2659 static void
2660 emit_constant_insn (rtx cond, rtx pattern)
2661 {
2662   if (cond)
2663     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
2664   emit_insn (pattern);
2665 }
2666
2667 /* As above, but extra parameter GENERATE which, if clear, suppresses
2668    RTL generation.  */
2669 /* ??? This needs more work for thumb2.  */
2670
2671 static int
2672 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
2673                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
2674                   int generate)
2675 {
2676   int can_invert = 0;
2677   int can_negate = 0;
2678   int final_invert = 0;
2679   int can_negate_initial = 0;
2680   int i;
2681   int num_bits_set = 0;
2682   int set_sign_bit_copies = 0;
2683   int clear_sign_bit_copies = 0;
2684   int clear_zero_bit_copies = 0;
2685   int set_zero_bit_copies = 0;
2686   int insns = 0;
2687   unsigned HOST_WIDE_INT temp1, temp2;
2688   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
2689   int step_size = TARGET_ARM ? 2 : 1;
2690
2691   /* Find out which operations are safe for a given CODE.  Also do a quick
2692      check for degenerate cases; these can occur when DImode operations
2693      are split.  */
2694   switch (code)
2695     {
2696     case SET:
2697       can_invert = 1;
2698       can_negate = 1;
2699       break;
2700
2701     case PLUS:
2702       can_negate = 1;
2703       can_negate_initial = 1;
2704       break;
2705
2706     case IOR:
2707       if (remainder == 0xffffffff)
2708         {
2709           if (generate)
2710             emit_constant_insn (cond,
2711                                 gen_rtx_SET (VOIDmode, target,
2712                                              GEN_INT (ARM_SIGN_EXTEND (val))));
2713           return 1;
2714         }
2715
2716       if (remainder == 0)
2717         {
2718           if (reload_completed && rtx_equal_p (target, source))
2719             return 0;
2720
2721           if (generate)
2722             emit_constant_insn (cond,
2723                                 gen_rtx_SET (VOIDmode, target, source));
2724           return 1;
2725         }
2726
2727       if (TARGET_THUMB2)
2728         can_invert = 1;
2729       break;
2730
2731     case AND:
2732       if (remainder == 0)
2733         {
2734           if (generate)
2735             emit_constant_insn (cond,
2736                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
2737           return 1;
2738         }
2739       if (remainder == 0xffffffff)
2740         {
2741           if (reload_completed && rtx_equal_p (target, source))
2742             return 0;
2743           if (generate)
2744             emit_constant_insn (cond,
2745                                 gen_rtx_SET (VOIDmode, target, source));
2746           return 1;
2747         }
2748       can_invert = 1;
2749       break;
2750
2751     case XOR:
2752       if (remainder == 0)
2753         {
2754           if (reload_completed && rtx_equal_p (target, source))
2755             return 0;
2756           if (generate)
2757             emit_constant_insn (cond,
2758                                 gen_rtx_SET (VOIDmode, target, source));
2759           return 1;
2760         }
2761
2762       if (remainder == 0xffffffff)
2763         {
2764           if (generate)
2765             emit_constant_insn (cond,
2766                                 gen_rtx_SET (VOIDmode, target,
2767                                              gen_rtx_NOT (mode, source)));
2768           return 1;
2769         }
2770       break;
2771
2772     case MINUS:
2773       /* We treat MINUS as (val - source), since (source - val) is always
2774          passed as (source + (-val)).  */
2775       if (remainder == 0)
2776         {
2777           if (generate)
2778             emit_constant_insn (cond,
2779                                 gen_rtx_SET (VOIDmode, target,
2780                                              gen_rtx_NEG (mode, source)));
2781           return 1;
2782         }
2783       if (const_ok_for_arm (val))
2784         {
2785           if (generate)
2786             emit_constant_insn (cond,
2787                                 gen_rtx_SET (VOIDmode, target,
2788                                              gen_rtx_MINUS (mode, GEN_INT (val),
2789                                                             source)));
2790           return 1;
2791         }
2792       can_negate = 1;
2793
2794       break;
2795
2796     default:
2797       gcc_unreachable ();
2798     }
2799
2800   /* If we can do it in one insn get out quickly.  */
2801   if (const_ok_for_arm (val)
2802       || (can_negate_initial && const_ok_for_arm (-val))
2803       || (can_invert && const_ok_for_arm (~val)))
2804     {
2805       if (generate)
2806         emit_constant_insn (cond,
2807                             gen_rtx_SET (VOIDmode, target,
2808                                          (source
2809                                           ? gen_rtx_fmt_ee (code, mode, source,
2810                                                             GEN_INT (val))
2811                                           : GEN_INT (val))));
2812       return 1;
2813     }
2814
2815   /* Calculate a few attributes that may be useful for specific
2816      optimizations.  */
2817   /* Count number of leading zeros.  */
2818   for (i = 31; i >= 0; i--)
2819     {
2820       if ((remainder & (1 << i)) == 0)
2821         clear_sign_bit_copies++;
2822       else
2823         break;
2824     }
2825
2826   /* Count number of leading 1's.  */
2827   for (i = 31; i >= 0; i--)
2828     {
2829       if ((remainder & (1 << i)) != 0)
2830         set_sign_bit_copies++;
2831       else
2832         break;
2833     }
2834
2835   /* Count number of trailing zero's.  */
2836   for (i = 0; i <= 31; i++)
2837     {
2838       if ((remainder & (1 << i)) == 0)
2839         clear_zero_bit_copies++;
2840       else
2841         break;
2842     }
2843
2844   /* Count number of trailing 1's.  */
2845   for (i = 0; i <= 31; i++)
2846     {
2847       if ((remainder & (1 << i)) != 0)
2848         set_zero_bit_copies++;
2849       else
2850         break;
2851     }
2852
2853   switch (code)
2854     {
2855     case SET:
2856       /* See if we can use movw.  */
2857       if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2858         {
2859           if (generate)
2860             emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2861                                                    GEN_INT (val)));
2862           return 1;
2863         }
2864
2865       /* See if we can do this by sign_extending a constant that is known
2866          to be negative.  This is a good, way of doing it, since the shift
2867          may well merge into a subsequent insn.  */
2868       if (set_sign_bit_copies > 1)
2869         {
2870           if (const_ok_for_arm
2871               (temp1 = ARM_SIGN_EXTEND (remainder
2872                                         << (set_sign_bit_copies - 1))))
2873             {
2874               if (generate)
2875                 {
2876                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2877                   emit_constant_insn (cond,
2878                                       gen_rtx_SET (VOIDmode, new_src,
2879                                                    GEN_INT (temp1)));
2880                   emit_constant_insn (cond,
2881                                       gen_ashrsi3 (target, new_src,
2882                                                    GEN_INT (set_sign_bit_copies - 1)));
2883                 }
2884               return 2;
2885             }
2886           /* For an inverted constant, we will need to set the low bits,
2887              these will be shifted out of harm's way.  */
2888           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2889           if (const_ok_for_arm (~temp1))
2890             {
2891               if (generate)
2892                 {
2893                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2894                   emit_constant_insn (cond,
2895                                       gen_rtx_SET (VOIDmode, new_src,
2896                                                    GEN_INT (temp1)));
2897                   emit_constant_insn (cond,
2898                                       gen_ashrsi3 (target, new_src,
2899                                                    GEN_INT (set_sign_bit_copies - 1)));
2900                 }
2901               return 2;
2902             }
2903         }
2904
2905       /* See if we can calculate the value as the difference between two
2906          valid immediates.  */
2907       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2908         {
2909           int topshift = clear_sign_bit_copies & ~1;
2910
2911           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2912                                    & (0xff000000 >> topshift));
2913
2914           /* If temp1 is zero, then that means the 9 most significant
2915              bits of remainder were 1 and we've caused it to overflow.
2916              When topshift is 0 we don't need to do anything since we
2917              can borrow from 'bit 32'.  */
2918           if (temp1 == 0 && topshift != 0)
2919             temp1 = 0x80000000 >> (topshift - 1);
2920
2921           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
2922
2923           if (const_ok_for_arm (temp2))
2924             {
2925               if (generate)
2926                 {
2927                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2928                   emit_constant_insn (cond,
2929                                       gen_rtx_SET (VOIDmode, new_src,
2930                                                    GEN_INT (temp1)));
2931                   emit_constant_insn (cond,
2932                                       gen_addsi3 (target, new_src,
2933                                                   GEN_INT (-temp2)));
2934                 }
2935
2936               return 2;
2937             }
2938         }
2939
2940       /* See if we can generate this by setting the bottom (or the top)
2941          16 bits, and then shifting these into the other half of the
2942          word.  We only look for the simplest cases, to do more would cost
2943          too much.  Be careful, however, not to generate this when the
2944          alternative would take fewer insns.  */
2945       if (val & 0xffff0000)
2946         {
2947           temp1 = remainder & 0xffff0000;
2948           temp2 = remainder & 0x0000ffff;
2949
2950           /* Overlaps outside this range are best done using other methods.  */
2951           for (i = 9; i < 24; i++)
2952             {
2953               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2954                   && !const_ok_for_arm (temp2))
2955                 {
2956                   rtx new_src = (subtargets
2957                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2958                                  : target);
2959                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2960                                             source, subtargets, generate);
2961                   source = new_src;
2962                   if (generate)
2963                     emit_constant_insn
2964                       (cond,
2965                        gen_rtx_SET
2966                        (VOIDmode, target,
2967                         gen_rtx_IOR (mode,
2968                                      gen_rtx_ASHIFT (mode, source,
2969                                                      GEN_INT (i)),
2970                                      source)));
2971                   return insns + 1;
2972                 }
2973             }
2974
2975           /* Don't duplicate cases already considered.  */
2976           for (i = 17; i < 24; i++)
2977             {
2978               if (((temp1 | (temp1 >> i)) == remainder)
2979                   && !const_ok_for_arm (temp1))
2980                 {
2981                   rtx new_src = (subtargets
2982                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2983                                  : target);
2984                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2985                                             source, subtargets, generate);
2986                   source = new_src;
2987                   if (generate)
2988                     emit_constant_insn
2989                       (cond,
2990                        gen_rtx_SET (VOIDmode, target,
2991                                     gen_rtx_IOR
2992                                     (mode,
2993                                      gen_rtx_LSHIFTRT (mode, source,
2994                                                        GEN_INT (i)),
2995                                      source)));
2996                   return insns + 1;
2997                 }
2998             }
2999         }
3000       break;
3001
3002     case IOR:
3003     case XOR:
3004       /* If we have IOR or XOR, and the constant can be loaded in a
3005          single instruction, and we can find a temporary to put it in,
3006          then this can be done in two instructions instead of 3-4.  */
3007       if (subtargets
3008           /* TARGET can't be NULL if SUBTARGETS is 0 */
3009           || (reload_completed && !reg_mentioned_p (target, source)))
3010         {
3011           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
3012             {
3013               if (generate)
3014                 {
3015                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3016
3017                   emit_constant_insn (cond,
3018                                       gen_rtx_SET (VOIDmode, sub,
3019                                                    GEN_INT (val)));
3020                   emit_constant_insn (cond,
3021                                       gen_rtx_SET (VOIDmode, target,
3022                                                    gen_rtx_fmt_ee (code, mode,
3023                                                                    source, sub)));
3024                 }
3025               return 2;
3026             }
3027         }
3028
3029       if (code == XOR)
3030         break;
3031
3032       /*  Convert.
3033           x = y | constant ( which is composed of set_sign_bit_copies of leading 1s
3034                              and the remainder 0s for e.g. 0xfff00000)
3035           x = ~(~(y ashift set_sign_bit_copies) lshiftrt set_sign_bit_copies)
3036
3037           This can be done in 2 instructions by using shifts with mov or mvn.
3038           e.g. for
3039           x = x | 0xfff00000;
3040           we generate.
3041           mvn   r0, r0, asl #12
3042           mvn   r0, r0, lsr #12  */
3043       if (set_sign_bit_copies > 8
3044           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
3045         {
3046           if (generate)
3047             {
3048               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3049               rtx shift = GEN_INT (set_sign_bit_copies);
3050
3051               emit_constant_insn
3052                 (cond,
3053                  gen_rtx_SET (VOIDmode, sub,
3054                               gen_rtx_NOT (mode,
3055                                            gen_rtx_ASHIFT (mode,
3056                                                            source,
3057                                                            shift))));
3058               emit_constant_insn
3059                 (cond,
3060                  gen_rtx_SET (VOIDmode, target,
3061                               gen_rtx_NOT (mode,
3062                                            gen_rtx_LSHIFTRT (mode, sub,
3063                                                              shift))));
3064             }
3065           return 2;
3066         }
3067
3068       /* Convert
3069           x = y | constant (which has set_zero_bit_copies number of trailing ones).
3070            to
3071           x = ~((~y lshiftrt set_zero_bit_copies) ashift set_zero_bit_copies).
3072
3073           For eg. r0 = r0 | 0xfff
3074                mvn      r0, r0, lsr #12
3075                mvn      r0, r0, asl #12
3076
3077       */
3078       if (set_zero_bit_copies > 8
3079           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
3080         {
3081           if (generate)
3082             {
3083               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3084               rtx shift = GEN_INT (set_zero_bit_copies);
3085
3086               emit_constant_insn
3087                 (cond,
3088                  gen_rtx_SET (VOIDmode, sub,
3089                               gen_rtx_NOT (mode,
3090                                            gen_rtx_LSHIFTRT (mode,
3091                                                              source,
3092                                                              shift))));
3093               emit_constant_insn
3094                 (cond,
3095                  gen_rtx_SET (VOIDmode, target,
3096                               gen_rtx_NOT (mode,
3097                                            gen_rtx_ASHIFT (mode, sub,
3098                                                            shift))));
3099             }
3100           return 2;
3101         }
3102
3103       /* This will never be reached for Thumb2 because orn is a valid
3104          instruction. This is for Thumb1 and the ARM 32 bit cases.
3105
3106          x = y | constant (such that ~constant is a valid constant)
3107          Transform this to
3108          x = ~(~y & ~constant).
3109       */
3110       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
3111         {
3112           if (generate)
3113             {
3114               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3115               emit_constant_insn (cond,
3116                                   gen_rtx_SET (VOIDmode, sub,
3117                                                gen_rtx_NOT (mode, source)));
3118               source = sub;
3119               if (subtargets)
3120                 sub = gen_reg_rtx (mode);
3121               emit_constant_insn (cond,
3122                                   gen_rtx_SET (VOIDmode, sub,
3123                                                gen_rtx_AND (mode, source,
3124                                                             GEN_INT (temp1))));
3125               emit_constant_insn (cond,
3126                                   gen_rtx_SET (VOIDmode, target,
3127                                                gen_rtx_NOT (mode, sub)));
3128             }
3129           return 3;
3130         }
3131       break;
3132
3133     case AND:
3134       /* See if two shifts will do 2 or more insn's worth of work.  */
3135       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
3136         {
3137           HOST_WIDE_INT shift_mask = ((0xffffffff
3138                                        << (32 - clear_sign_bit_copies))
3139                                       & 0xffffffff);
3140
3141           if ((remainder | shift_mask) != 0xffffffff)
3142             {
3143               if (generate)
3144                 {
3145                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3146                   insns = arm_gen_constant (AND, mode, cond,
3147                                             remainder | shift_mask,
3148                                             new_src, source, subtargets, 1);
3149                   source = new_src;
3150                 }
3151               else
3152                 {
3153                   rtx targ = subtargets ? NULL_RTX : target;
3154                   insns = arm_gen_constant (AND, mode, cond,
3155                                             remainder | shift_mask,
3156                                             targ, source, subtargets, 0);
3157                 }
3158             }
3159
3160           if (generate)
3161             {
3162               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3163               rtx shift = GEN_INT (clear_sign_bit_copies);
3164
3165               emit_insn (gen_ashlsi3 (new_src, source, shift));
3166               emit_insn (gen_lshrsi3 (target, new_src, shift));
3167             }
3168
3169           return insns + 2;
3170         }
3171
3172       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
3173         {
3174           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
3175
3176           if ((remainder | shift_mask) != 0xffffffff)
3177             {
3178               if (generate)
3179                 {
3180                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3181
3182                   insns = arm_gen_constant (AND, mode, cond,
3183                                             remainder | shift_mask,
3184                                             new_src, source, subtargets, 1);
3185                   source = new_src;
3186                 }
3187               else
3188                 {
3189                   rtx targ = subtargets ? NULL_RTX : target;
3190
3191                   insns = arm_gen_constant (AND, mode, cond,
3192                                             remainder | shift_mask,
3193                                             targ, source, subtargets, 0);
3194                 }
3195             }
3196
3197           if (generate)
3198             {
3199               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3200               rtx shift = GEN_INT (clear_zero_bit_copies);
3201
3202               emit_insn (gen_lshrsi3 (new_src, source, shift));
3203               emit_insn (gen_ashlsi3 (target, new_src, shift));
3204             }
3205
3206           return insns + 2;
3207         }
3208
3209       break;
3210
3211     default:
3212       break;
3213     }
3214
3215   for (i = 0; i < 32; i++)
3216     if (remainder & (1 << i))
3217       num_bits_set++;
3218
3219   if ((code == AND)
3220       || (code != IOR && can_invert && num_bits_set > 16))
3221     remainder ^= 0xffffffff;
3222   else if (code == PLUS && num_bits_set > 16)
3223     remainder = (-remainder) & 0xffffffff;
3224
3225   /* For XOR, if more than half the bits are set and there's a sequence
3226      of more than 8 consecutive ones in the pattern then we can XOR by the
3227      inverted constant and then invert the final result; this may save an
3228      instruction and might also lead to the final mvn being merged with
3229      some other operation.  */
3230   else if (code == XOR && num_bits_set > 16
3231            && (count_insns_for_constant (remainder ^ 0xffffffff,
3232                                          find_best_start
3233                                          (remainder ^ 0xffffffff))
3234                < count_insns_for_constant (remainder,
3235                                            find_best_start (remainder))))
3236     {
3237       remainder ^= 0xffffffff;
3238       final_invert = 1;
3239     }
3240   else
3241     {
3242       can_invert = 0;
3243       can_negate = 0;
3244     }
3245
3246   /* Now try and find a way of doing the job in either two or three
3247      instructions.
3248      We start by looking for the largest block of zeros that are aligned on
3249      a 2-bit boundary, we then fill up the temps, wrapping around to the
3250      top of the word when we drop off the bottom.
3251      In the worst case this code should produce no more than four insns.
3252      Thumb-2 constants are shifted, not rotated, so the MSB is always the
3253      best place to start.  */
3254
3255   /* ??? Use thumb2 replicated constants when the high and low halfwords are
3256      the same.  */
3257   {
3258     /* Now start emitting the insns.  */
3259     i = find_best_start (remainder);
3260     do
3261       {
3262         int end;
3263
3264         if (i <= 0)
3265           i += 32;
3266         if (remainder & (3 << (i - 2)))
3267           {
3268             end = i - 8;
3269             if (end < 0)
3270               end += 32;
3271             temp1 = remainder & ((0x0ff << end)
3272                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
3273             remainder &= ~temp1;
3274
3275             if (generate)
3276               {
3277                 rtx new_src, temp1_rtx;
3278
3279                 if (code == SET || code == MINUS)
3280                   {
3281                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
3282                     if (can_invert && code != MINUS)
3283                       temp1 = ~temp1;
3284                   }
3285                 else
3286                   {
3287                     if ((final_invert || remainder) && subtargets)
3288                       new_src = gen_reg_rtx (mode);
3289                     else
3290                       new_src = target;
3291                     if (can_invert)
3292                       temp1 = ~temp1;
3293                     else if (can_negate)
3294                       temp1 = -temp1;
3295                   }
3296
3297                 temp1 = trunc_int_for_mode (temp1, mode);
3298                 temp1_rtx = GEN_INT (temp1);
3299
3300                 if (code == SET)
3301                   ;
3302                 else if (code == MINUS)
3303                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
3304                 else
3305                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
3306
3307                 emit_constant_insn (cond,
3308                                     gen_rtx_SET (VOIDmode, new_src,
3309                                                  temp1_rtx));
3310                 source = new_src;
3311               }
3312
3313             if (code == SET)
3314               {
3315                 can_invert = 0;
3316                 code = PLUS;
3317               }
3318             else if (code == MINUS)
3319               code = PLUS;
3320
3321             insns++;
3322             i -= 8 - step_size;
3323           }
3324         /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
3325            shifts.  */
3326         i -= step_size;
3327       }
3328     while (remainder);
3329   }
3330
3331   if (final_invert)
3332     {
3333       if (generate)
3334         emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
3335                                                gen_rtx_NOT (mode, source)));
3336       insns++;
3337     }
3338
3339   return insns;
3340 }
3341
3342 /* Canonicalize a comparison so that we are more likely to recognize it.
3343    This can be done for a few constant compares, where we can make the
3344    immediate value easier to load.  */
3345
3346 enum rtx_code
3347 arm_canonicalize_comparison (enum rtx_code code, rtx *op0, rtx *op1)
3348 {
3349   enum machine_mode mode;
3350   unsigned HOST_WIDE_INT i, maxval;
3351
3352   mode = GET_MODE (*op0);
3353   if (mode == VOIDmode)
3354     mode = GET_MODE (*op1);
3355
3356   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
3357
3358   /* For DImode, we have GE/LT/GEU/LTU comparisons.  In ARM mode
3359      we can also use cmp/cmpeq for GTU/LEU.  GT/LE must be either
3360      reversed or (for constant OP1) adjusted to GE/LT.  Similarly
3361      for GTU/LEU in Thumb mode.  */
3362   if (mode == DImode)
3363     {
3364       rtx tem;
3365
3366       /* To keep things simple, always use the Cirrus cfcmp64 if it is
3367          available.  */
3368       if (TARGET_ARM && TARGET_HARD_FLOAT && TARGET_MAVERICK)
3369         return code;
3370
3371       if (code == GT || code == LE
3372           || (!TARGET_ARM && (code == GTU || code == LEU)))
3373         {
3374           /* Missing comparison.  First try to use an available
3375              comparison.  */
3376           if (GET_CODE (*op1) == CONST_INT)
3377             {
3378               i = INTVAL (*op1);
3379               switch (code)
3380                 {
3381                 case GT:
3382                 case LE:
3383                   if (i != maxval
3384                       && arm_const_double_by_immediates (GEN_INT (i + 1)))
3385                     {
3386                       *op1 = GEN_INT (i + 1);
3387                       return code == GT ? GE : LT;
3388                     }
3389                   break;
3390                 case GTU:
3391                 case LEU:
3392                   if (i != ~((unsigned HOST_WIDE_INT) 0)
3393                       && arm_const_double_by_immediates (GEN_INT (i + 1)))
3394                     {
3395                       *op1 = GEN_INT (i + 1);
3396                       return code == GTU ? GEU : LTU;
3397                     }
3398                   break;
3399                 default:
3400                   gcc_unreachable ();
3401                 }
3402             }
3403
3404           /* If that did not work, reverse the condition.  */
3405           tem = *op0;
3406           *op0 = *op1;
3407           *op1 = tem;
3408           return swap_condition (code);
3409         }
3410
3411       return code;
3412     }
3413
3414   /* Comparisons smaller than DImode.  Only adjust comparisons against
3415      an out-of-range constant.  */
3416   if (GET_CODE (*op1) != CONST_INT
3417       || const_ok_for_arm (INTVAL (*op1))
3418       || const_ok_for_arm (- INTVAL (*op1)))
3419     return code;
3420
3421   i = INTVAL (*op1);
3422
3423   switch (code)
3424     {
3425     case EQ:
3426     case NE:
3427       return code;
3428
3429     case GT:
3430     case LE:
3431       if (i != maxval
3432           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3433         {
3434           *op1 = GEN_INT (i + 1);
3435           return code == GT ? GE : LT;
3436         }
3437       break;