- Porting of address santizer patch for ARM in gcc-4.8.1 from gcc-4.9
[platform/upstream/gcc48.git] / gcc / config / arm / arm.c
1 /* Output routines for GCC for ARM.
2    Copyright (C) 1991-2013 Free Software Foundation, Inc.
3    Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
4    and Martin Simmons (@harleqn.co.uk).
5    More major hacks by Richard Earnshaw (rearnsha@arm.com).
6
7    This file is part of GCC.
8
9    GCC is free software; you can redistribute it and/or modify it
10    under the terms of the GNU General Public License as published
11    by the Free Software Foundation; either version 3, or (at your
12    option) any later version.
13
14    GCC is distributed in the hope that it will be useful, but WITHOUT
15    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
17    License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with GCC; see the file COPYING3.  If not see
21    <http://www.gnu.org/licenses/>.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "obstack.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "output.h"
35 #include "insn-attr.h"
36 #include "flags.h"
37 #include "reload.h"
38 #include "function.h"
39 #include "expr.h"
40 #include "optabs.h"
41 #include "diagnostic-core.h"
42 #include "recog.h"
43 #include "cgraph.h"
44 #include "ggc.h"
45 #include "except.h"
46 #include "tm_p.h"
47 #include "target.h"
48 #include "target-def.h"
49 #include "debug.h"
50 #include "langhooks.h"
51 #include "df.h"
52 #include "intl.h"
53 #include "libfuncs.h"
54 #include "params.h"
55 #include "opts.h"
56 #include "dumpfile.h"
57
58 /* Forward definitions of types.  */
59 typedef struct minipool_node    Mnode;
60 typedef struct minipool_fixup   Mfix;
61
62 void (*arm_lang_output_object_attributes_hook)(void);
63
64 struct four_ints
65 {
66   int i[4];
67 };
68
69 /* Forward function declarations.  */
70 static bool arm_needs_doubleword_align (enum machine_mode, const_tree);
71 static int arm_compute_static_chain_stack_bytes (void);
72 static arm_stack_offsets *arm_get_frame_offsets (void);
73 static void arm_add_gc_roots (void);
74 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
75                              HOST_WIDE_INT, rtx, rtx, int, int);
76 static unsigned bit_count (unsigned long);
77 static int arm_address_register_rtx_p (rtx, int);
78 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
79 static int thumb2_legitimate_index_p (enum machine_mode, rtx, int);
80 static int thumb1_base_register_rtx_p (rtx, enum machine_mode, int);
81 static rtx arm_legitimize_address (rtx, rtx, enum machine_mode);
82 static reg_class_t arm_preferred_reload_class (rtx, reg_class_t);
83 static rtx thumb_legitimize_address (rtx, rtx, enum machine_mode);
84 inline static int thumb1_index_register_rtx_p (rtx, int);
85 static bool arm_legitimate_address_p (enum machine_mode, rtx, bool);
86 static int thumb_far_jump_used_p (void);
87 static bool thumb_force_lr_save (void);
88 static unsigned arm_size_return_regs (void);
89 static bool arm_assemble_integer (rtx, unsigned int, int);
90 static void arm_print_operand (FILE *, rtx, int);
91 static void arm_print_operand_address (FILE *, rtx);
92 static bool arm_print_operand_punct_valid_p (unsigned char code);
93 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
94 static arm_cc get_arm_condition_code (rtx);
95 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
96 static rtx is_jump_table (rtx);
97 static const char *output_multi_immediate (rtx *, const char *, const char *,
98                                            int, HOST_WIDE_INT);
99 static const char *shift_op (rtx, HOST_WIDE_INT *);
100 static struct machine_function *arm_init_machine_status (void);
101 static void thumb_exit (FILE *, int);
102 static rtx is_jump_table (rtx);
103 static HOST_WIDE_INT get_jump_table_size (rtx);
104 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
105 static Mnode *add_minipool_forward_ref (Mfix *);
106 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
107 static Mnode *add_minipool_backward_ref (Mfix *);
108 static void assign_minipool_offsets (Mfix *);
109 static void arm_print_value (FILE *, rtx);
110 static void dump_minipool (rtx);
111 static int arm_barrier_cost (rtx);
112 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
113 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
114 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
115                                rtx);
116 static void arm_reorg (void);
117 static void note_invalid_constants (rtx, HOST_WIDE_INT, int);
118 static unsigned long arm_compute_save_reg0_reg12_mask (void);
119 static unsigned long arm_compute_save_reg_mask (void);
120 static unsigned long arm_isr_value (tree);
121 static unsigned long arm_compute_func_type (void);
122 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
123 static tree arm_handle_pcs_attribute (tree *, tree, tree, int, bool *);
124 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
125 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
126 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
127 #endif
128 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
129 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
130 static int arm_comp_type_attributes (const_tree, const_tree);
131 static void arm_set_default_type_attributes (tree);
132 static int arm_adjust_cost (rtx, rtx, rtx, int);
133 static int arm_sched_reorder (FILE *, int, rtx *, int *, int);
134 static int optimal_immediate_sequence (enum rtx_code code,
135                                        unsigned HOST_WIDE_INT val,
136                                        struct four_ints *return_sequence);
137 static int optimal_immediate_sequence_1 (enum rtx_code code,
138                                          unsigned HOST_WIDE_INT val,
139                                          struct four_ints *return_sequence,
140                                          int i);
141 static int arm_get_strip_length (int);
142 static bool arm_function_ok_for_sibcall (tree, tree);
143 static enum machine_mode arm_promote_function_mode (const_tree,
144                                                     enum machine_mode, int *,
145                                                     const_tree, int);
146 static bool arm_return_in_memory (const_tree, const_tree);
147 static rtx arm_function_value (const_tree, const_tree, bool);
148 static rtx arm_libcall_value_1 (enum machine_mode);
149 static rtx arm_libcall_value (enum machine_mode, const_rtx);
150 static bool arm_function_value_regno_p (const unsigned int);
151 static void arm_internal_label (FILE *, const char *, unsigned long);
152 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
153                                  tree);
154 static bool arm_have_conditional_execution (void);
155 static bool arm_cannot_force_const_mem (enum machine_mode, rtx);
156 static bool arm_legitimate_constant_p (enum machine_mode, rtx);
157 static bool arm_rtx_costs_1 (rtx, enum rtx_code, int*, bool);
158 static bool arm_size_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *);
159 static bool arm_slowmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
160 static bool arm_fastmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
161 static bool arm_xscale_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
162 static bool arm_9e_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
163 static bool arm_rtx_costs (rtx, int, int, int, int *, bool);
164 static int arm_address_cost (rtx, enum machine_mode, addr_space_t, bool);
165 static int arm_register_move_cost (enum machine_mode, reg_class_t, reg_class_t);
166 static int arm_memory_move_cost (enum machine_mode, reg_class_t, bool);
167 static void arm_init_builtins (void);
168 static void arm_init_iwmmxt_builtins (void);
169 static rtx safe_vector_operand (rtx, enum machine_mode);
170 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
171 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
172 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
173 static tree arm_builtin_decl (unsigned, bool);
174 static void emit_constant_insn (rtx cond, rtx pattern);
175 static rtx emit_set_insn (rtx, rtx);
176 static int arm_arg_partial_bytes (cumulative_args_t, enum machine_mode,
177                                   tree, bool);
178 static rtx arm_function_arg (cumulative_args_t, enum machine_mode,
179                              const_tree, bool);
180 static void arm_function_arg_advance (cumulative_args_t, enum machine_mode,
181                                       const_tree, bool);
182 static unsigned int arm_function_arg_boundary (enum machine_mode, const_tree);
183 static rtx aapcs_allocate_return_reg (enum machine_mode, const_tree,
184                                       const_tree);
185 static rtx aapcs_libcall_value (enum machine_mode);
186 static int aapcs_select_return_coproc (const_tree, const_tree);
187
188 #ifdef OBJECT_FORMAT_ELF
189 static void arm_elf_asm_constructor (rtx, int) ATTRIBUTE_UNUSED;
190 static void arm_elf_asm_destructor (rtx, int) ATTRIBUTE_UNUSED;
191 #endif
192 #ifndef ARM_PE
193 static void arm_encode_section_info (tree, rtx, int);
194 #endif
195
196 static void arm_file_end (void);
197 static void arm_file_start (void);
198
199 static void arm_setup_incoming_varargs (cumulative_args_t, enum machine_mode,
200                                         tree, int *, int);
201 static bool arm_pass_by_reference (cumulative_args_t,
202                                    enum machine_mode, const_tree, bool);
203 static bool arm_promote_prototypes (const_tree);
204 static bool arm_default_short_enums (void);
205 static bool arm_align_anon_bitfield (void);
206 static bool arm_return_in_msb (const_tree);
207 static bool arm_must_pass_in_stack (enum machine_mode, const_tree);
208 static bool arm_return_in_memory (const_tree, const_tree);
209 #if ARM_UNWIND_INFO
210 static void arm_unwind_emit (FILE *, rtx);
211 static bool arm_output_ttype (rtx);
212 static void arm_asm_emit_except_personality (rtx);
213 static void arm_asm_init_sections (void);
214 #endif
215 static rtx arm_dwarf_register_span (rtx);
216
217 static tree arm_cxx_guard_type (void);
218 static bool arm_cxx_guard_mask_bit (void);
219 static tree arm_get_cookie_size (tree);
220 static bool arm_cookie_has_size (void);
221 static bool arm_cxx_cdtor_returns_this (void);
222 static bool arm_cxx_key_method_may_be_inline (void);
223 static void arm_cxx_determine_class_data_visibility (tree);
224 static bool arm_cxx_class_data_always_comdat (void);
225 static bool arm_cxx_use_aeabi_atexit (void);
226 static void arm_init_libfuncs (void);
227 static tree arm_build_builtin_va_list (void);
228 static void arm_expand_builtin_va_start (tree, rtx);
229 static tree arm_gimplify_va_arg_expr (tree, tree, gimple_seq *, gimple_seq *);
230 static void arm_option_override (void);
231 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
232 static bool arm_cannot_copy_insn_p (rtx);
233 static bool arm_tls_symbol_p (rtx x);
234 static int arm_issue_rate (void);
235 static void arm_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
236 static bool arm_output_addr_const_extra (FILE *, rtx);
237 static bool arm_allocate_stack_slots_for_args (void);
238 static bool arm_warn_func_return (tree);
239 static const char *arm_invalid_parameter_type (const_tree t);
240 static const char *arm_invalid_return_type (const_tree t);
241 static tree arm_promoted_type (const_tree t);
242 static tree arm_convert_to_type (tree type, tree expr);
243 static bool arm_scalar_mode_supported_p (enum machine_mode);
244 static bool arm_frame_pointer_required (void);
245 static bool arm_can_eliminate (const int, const int);
246 static void arm_asm_trampoline_template (FILE *);
247 static void arm_trampoline_init (rtx, tree, rtx);
248 static rtx arm_trampoline_adjust_address (rtx);
249 static rtx arm_pic_static_addr (rtx orig, rtx reg);
250 static bool cortex_a9_sched_adjust_cost (rtx, rtx, rtx, int *);
251 static bool xscale_sched_adjust_cost (rtx, rtx, rtx, int *);
252 static bool fa726te_sched_adjust_cost (rtx, rtx, rtx, int *);
253 static bool arm_array_mode_supported_p (enum machine_mode,
254                                         unsigned HOST_WIDE_INT);
255 static enum machine_mode arm_preferred_simd_mode (enum machine_mode);
256 static bool arm_class_likely_spilled_p (reg_class_t);
257 static HOST_WIDE_INT arm_vector_alignment (const_tree type);
258 static bool arm_vector_alignment_reachable (const_tree type, bool is_packed);
259 static bool arm_builtin_support_vector_misalignment (enum machine_mode mode,
260                                                      const_tree type,
261                                                      int misalignment,
262                                                      bool is_packed);
263 static void arm_conditional_register_usage (void);
264 static reg_class_t arm_preferred_rename_class (reg_class_t rclass);
265 static unsigned int arm_autovectorize_vector_sizes (void);
266 static int arm_default_branch_cost (bool, bool);
267 static int arm_cortex_a5_branch_cost (bool, bool);
268
269 static bool arm_vectorize_vec_perm_const_ok (enum machine_mode vmode,
270                                              const unsigned char *sel);
271
272 static int arm_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
273                                            tree vectype,
274                                            int misalign ATTRIBUTE_UNUSED);
275 static unsigned arm_add_stmt_cost (void *data, int count,
276                                    enum vect_cost_for_stmt kind,
277                                    struct _stmt_vec_info *stmt_info,
278                                    int misalign,
279                                    enum vect_cost_model_location where);
280
281 static void arm_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
282                                          bool op0_preserve_value);
283 static unsigned HOST_WIDE_INT arm_asan_shadow_offset (void);
284 \f
285 /* Table of machine attributes.  */
286 static const struct attribute_spec arm_attribute_table[] =
287 {
288   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
289        affects_type_identity } */
290   /* Function calls made to this symbol must be done indirectly, because
291      it may lie outside of the 26 bit addressing range of a normal function
292      call.  */
293   { "long_call",    0, 0, false, true,  true,  NULL, false },
294   /* Whereas these functions are always known to reside within the 26 bit
295      addressing range.  */
296   { "short_call",   0, 0, false, true,  true,  NULL, false },
297   /* Specify the procedure call conventions for a function.  */
298   { "pcs",          1, 1, false, true,  true,  arm_handle_pcs_attribute,
299     false },
300   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
301   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute,
302     false },
303   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute,
304     false },
305   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute,
306     false },
307 #ifdef ARM_PE
308   /* ARM/PE has three new attributes:
309      interfacearm - ?
310      dllexport - for exporting a function/variable that will live in a dll
311      dllimport - for importing a function/variable from a dll
312
313      Microsoft allows multiple declspecs in one __declspec, separating
314      them with spaces.  We do NOT support this.  Instead, use __declspec
315      multiple times.
316   */
317   { "dllimport",    0, 0, true,  false, false, NULL, false },
318   { "dllexport",    0, 0, true,  false, false, NULL, false },
319   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute,
320     false },
321 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
322   { "dllimport",    0, 0, false, false, false, handle_dll_attribute, false },
323   { "dllexport",    0, 0, false, false, false, handle_dll_attribute, false },
324   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute,
325     false },
326 #endif
327   { NULL,           0, 0, false, false, false, NULL, false }
328 };
329 \f
330 /* Initialize the GCC target structure.  */
331 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
332 #undef  TARGET_MERGE_DECL_ATTRIBUTES
333 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
334 #endif
335
336 #undef TARGET_LEGITIMIZE_ADDRESS
337 #define TARGET_LEGITIMIZE_ADDRESS arm_legitimize_address
338
339 #undef  TARGET_ATTRIBUTE_TABLE
340 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
341
342 #undef TARGET_ASM_FILE_START
343 #define TARGET_ASM_FILE_START arm_file_start
344 #undef TARGET_ASM_FILE_END
345 #define TARGET_ASM_FILE_END arm_file_end
346
347 #undef  TARGET_ASM_ALIGNED_SI_OP
348 #define TARGET_ASM_ALIGNED_SI_OP NULL
349 #undef  TARGET_ASM_INTEGER
350 #define TARGET_ASM_INTEGER arm_assemble_integer
351
352 #undef TARGET_PRINT_OPERAND
353 #define TARGET_PRINT_OPERAND arm_print_operand
354 #undef TARGET_PRINT_OPERAND_ADDRESS
355 #define TARGET_PRINT_OPERAND_ADDRESS arm_print_operand_address
356 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
357 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P arm_print_operand_punct_valid_p
358
359 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
360 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA arm_output_addr_const_extra
361
362 #undef  TARGET_ASM_FUNCTION_PROLOGUE
363 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
364
365 #undef  TARGET_ASM_FUNCTION_EPILOGUE
366 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
367
368 #undef  TARGET_OPTION_OVERRIDE
369 #define TARGET_OPTION_OVERRIDE arm_option_override
370
371 #undef  TARGET_COMP_TYPE_ATTRIBUTES
372 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
373
374 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
375 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
376
377 #undef  TARGET_SCHED_ADJUST_COST
378 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
379
380 #undef TARGET_SCHED_REORDER
381 #define TARGET_SCHED_REORDER arm_sched_reorder
382
383 #undef TARGET_REGISTER_MOVE_COST
384 #define TARGET_REGISTER_MOVE_COST arm_register_move_cost
385
386 #undef TARGET_MEMORY_MOVE_COST
387 #define TARGET_MEMORY_MOVE_COST arm_memory_move_cost
388
389 #undef TARGET_ENCODE_SECTION_INFO
390 #ifdef ARM_PE
391 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
392 #else
393 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
394 #endif
395
396 #undef  TARGET_STRIP_NAME_ENCODING
397 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
398
399 #undef  TARGET_ASM_INTERNAL_LABEL
400 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
401
402 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
403 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
404
405 #undef  TARGET_FUNCTION_VALUE
406 #define TARGET_FUNCTION_VALUE arm_function_value
407
408 #undef  TARGET_LIBCALL_VALUE
409 #define TARGET_LIBCALL_VALUE arm_libcall_value
410
411 #undef TARGET_FUNCTION_VALUE_REGNO_P
412 #define TARGET_FUNCTION_VALUE_REGNO_P arm_function_value_regno_p
413
414 #undef  TARGET_ASM_OUTPUT_MI_THUNK
415 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
416 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
417 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
418
419 #undef  TARGET_RTX_COSTS
420 #define TARGET_RTX_COSTS arm_rtx_costs
421 #undef  TARGET_ADDRESS_COST
422 #define TARGET_ADDRESS_COST arm_address_cost
423
424 #undef TARGET_SHIFT_TRUNCATION_MASK
425 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
426 #undef TARGET_VECTOR_MODE_SUPPORTED_P
427 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
428 #undef TARGET_ARRAY_MODE_SUPPORTED_P
429 #define TARGET_ARRAY_MODE_SUPPORTED_P arm_array_mode_supported_p
430 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
431 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE arm_preferred_simd_mode
432 #undef TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES
433 #define TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES \
434   arm_autovectorize_vector_sizes
435
436 #undef  TARGET_MACHINE_DEPENDENT_REORG
437 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
438
439 #undef  TARGET_INIT_BUILTINS
440 #define TARGET_INIT_BUILTINS  arm_init_builtins
441 #undef  TARGET_EXPAND_BUILTIN
442 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
443 #undef  TARGET_BUILTIN_DECL
444 #define TARGET_BUILTIN_DECL arm_builtin_decl
445
446 #undef TARGET_INIT_LIBFUNCS
447 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
448
449 #undef TARGET_PROMOTE_FUNCTION_MODE
450 #define TARGET_PROMOTE_FUNCTION_MODE arm_promote_function_mode
451 #undef TARGET_PROMOTE_PROTOTYPES
452 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
453 #undef TARGET_PASS_BY_REFERENCE
454 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
455 #undef TARGET_ARG_PARTIAL_BYTES
456 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
457 #undef TARGET_FUNCTION_ARG
458 #define TARGET_FUNCTION_ARG arm_function_arg
459 #undef TARGET_FUNCTION_ARG_ADVANCE
460 #define TARGET_FUNCTION_ARG_ADVANCE arm_function_arg_advance
461 #undef TARGET_FUNCTION_ARG_BOUNDARY
462 #define TARGET_FUNCTION_ARG_BOUNDARY arm_function_arg_boundary
463
464 #undef  TARGET_SETUP_INCOMING_VARARGS
465 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
466
467 #undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
468 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS arm_allocate_stack_slots_for_args
469
470 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
471 #define TARGET_ASM_TRAMPOLINE_TEMPLATE arm_asm_trampoline_template
472 #undef TARGET_TRAMPOLINE_INIT
473 #define TARGET_TRAMPOLINE_INIT arm_trampoline_init
474 #undef TARGET_TRAMPOLINE_ADJUST_ADDRESS
475 #define TARGET_TRAMPOLINE_ADJUST_ADDRESS arm_trampoline_adjust_address
476
477 #undef TARGET_WARN_FUNC_RETURN
478 #define TARGET_WARN_FUNC_RETURN arm_warn_func_return
479
480 #undef TARGET_DEFAULT_SHORT_ENUMS
481 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
482
483 #undef TARGET_ALIGN_ANON_BITFIELD
484 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
485
486 #undef TARGET_NARROW_VOLATILE_BITFIELD
487 #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
488
489 #undef TARGET_CXX_GUARD_TYPE
490 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
491
492 #undef TARGET_CXX_GUARD_MASK_BIT
493 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
494
495 #undef TARGET_CXX_GET_COOKIE_SIZE
496 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
497
498 #undef TARGET_CXX_COOKIE_HAS_SIZE
499 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
500
501 #undef TARGET_CXX_CDTOR_RETURNS_THIS
502 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
503
504 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
505 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
506
507 #undef TARGET_CXX_USE_AEABI_ATEXIT
508 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
509
510 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
511 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
512   arm_cxx_determine_class_data_visibility
513
514 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
515 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
516
517 #undef TARGET_RETURN_IN_MSB
518 #define TARGET_RETURN_IN_MSB arm_return_in_msb
519
520 #undef TARGET_RETURN_IN_MEMORY
521 #define TARGET_RETURN_IN_MEMORY arm_return_in_memory
522
523 #undef TARGET_MUST_PASS_IN_STACK
524 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
525
526 #if ARM_UNWIND_INFO
527 #undef TARGET_ASM_UNWIND_EMIT
528 #define TARGET_ASM_UNWIND_EMIT arm_unwind_emit
529
530 /* EABI unwinding tables use a different format for the typeinfo tables.  */
531 #undef TARGET_ASM_TTYPE
532 #define TARGET_ASM_TTYPE arm_output_ttype
533
534 #undef TARGET_ARM_EABI_UNWINDER
535 #define TARGET_ARM_EABI_UNWINDER true
536
537 #undef TARGET_ASM_EMIT_EXCEPT_PERSONALITY
538 #define TARGET_ASM_EMIT_EXCEPT_PERSONALITY arm_asm_emit_except_personality
539
540 #undef TARGET_ASM_INIT_SECTIONS
541 #define TARGET_ASM_INIT_SECTIONS arm_asm_init_sections
542 #endif /* ARM_UNWIND_INFO */
543
544 #undef TARGET_DWARF_REGISTER_SPAN
545 #define TARGET_DWARF_REGISTER_SPAN arm_dwarf_register_span
546
547 #undef  TARGET_CANNOT_COPY_INSN_P
548 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
549
550 #ifdef HAVE_AS_TLS
551 #undef TARGET_HAVE_TLS
552 #define TARGET_HAVE_TLS true
553 #endif
554
555 #undef TARGET_HAVE_CONDITIONAL_EXECUTION
556 #define TARGET_HAVE_CONDITIONAL_EXECUTION arm_have_conditional_execution
557
558 #undef TARGET_LEGITIMATE_CONSTANT_P
559 #define TARGET_LEGITIMATE_CONSTANT_P arm_legitimate_constant_p
560
561 #undef TARGET_CANNOT_FORCE_CONST_MEM
562 #define TARGET_CANNOT_FORCE_CONST_MEM arm_cannot_force_const_mem
563
564 #undef TARGET_MAX_ANCHOR_OFFSET
565 #define TARGET_MAX_ANCHOR_OFFSET 4095
566
567 /* The minimum is set such that the total size of the block
568    for a particular anchor is -4088 + 1 + 4095 bytes, which is
569    divisible by eight, ensuring natural spacing of anchors.  */
570 #undef TARGET_MIN_ANCHOR_OFFSET
571 #define TARGET_MIN_ANCHOR_OFFSET -4088
572
573 #undef TARGET_SCHED_ISSUE_RATE
574 #define TARGET_SCHED_ISSUE_RATE arm_issue_rate
575
576 #undef TARGET_MANGLE_TYPE
577 #define TARGET_MANGLE_TYPE arm_mangle_type
578
579 #undef TARGET_BUILD_BUILTIN_VA_LIST
580 #define TARGET_BUILD_BUILTIN_VA_LIST arm_build_builtin_va_list
581 #undef TARGET_EXPAND_BUILTIN_VA_START
582 #define TARGET_EXPAND_BUILTIN_VA_START arm_expand_builtin_va_start
583 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
584 #define TARGET_GIMPLIFY_VA_ARG_EXPR arm_gimplify_va_arg_expr
585
586 #ifdef HAVE_AS_TLS
587 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
588 #define TARGET_ASM_OUTPUT_DWARF_DTPREL arm_output_dwarf_dtprel
589 #endif
590
591 #undef TARGET_LEGITIMATE_ADDRESS_P
592 #define TARGET_LEGITIMATE_ADDRESS_P     arm_legitimate_address_p
593
594 #undef TARGET_PREFERRED_RELOAD_CLASS
595 #define TARGET_PREFERRED_RELOAD_CLASS arm_preferred_reload_class
596
597 #undef TARGET_INVALID_PARAMETER_TYPE
598 #define TARGET_INVALID_PARAMETER_TYPE arm_invalid_parameter_type
599
600 #undef TARGET_INVALID_RETURN_TYPE
601 #define TARGET_INVALID_RETURN_TYPE arm_invalid_return_type
602
603 #undef TARGET_PROMOTED_TYPE
604 #define TARGET_PROMOTED_TYPE arm_promoted_type
605
606 #undef TARGET_CONVERT_TO_TYPE
607 #define TARGET_CONVERT_TO_TYPE arm_convert_to_type
608
609 #undef TARGET_SCALAR_MODE_SUPPORTED_P
610 #define TARGET_SCALAR_MODE_SUPPORTED_P arm_scalar_mode_supported_p
611
612 #undef TARGET_FRAME_POINTER_REQUIRED
613 #define TARGET_FRAME_POINTER_REQUIRED arm_frame_pointer_required
614
615 #undef TARGET_CAN_ELIMINATE
616 #define TARGET_CAN_ELIMINATE arm_can_eliminate
617
618 #undef TARGET_CONDITIONAL_REGISTER_USAGE
619 #define TARGET_CONDITIONAL_REGISTER_USAGE arm_conditional_register_usage
620
621 #undef TARGET_CLASS_LIKELY_SPILLED_P
622 #define TARGET_CLASS_LIKELY_SPILLED_P arm_class_likely_spilled_p
623
624 #undef TARGET_VECTOR_ALIGNMENT
625 #define TARGET_VECTOR_ALIGNMENT arm_vector_alignment
626
627 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
628 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE \
629   arm_vector_alignment_reachable
630
631 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
632 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
633   arm_builtin_support_vector_misalignment
634
635 #undef TARGET_PREFERRED_RENAME_CLASS
636 #define TARGET_PREFERRED_RENAME_CLASS \
637   arm_preferred_rename_class
638
639 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
640 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK \
641   arm_vectorize_vec_perm_const_ok
642
643 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
644 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
645   arm_builtin_vectorization_cost
646 #undef TARGET_VECTORIZE_ADD_STMT_COST
647 #define TARGET_VECTORIZE_ADD_STMT_COST arm_add_stmt_cost
648
649 #undef TARGET_CANONICALIZE_COMPARISON
650 #define TARGET_CANONICALIZE_COMPARISON \
651   arm_canonicalize_comparison
652
653 #undef TARGET_ASAN_SHADOW_OFFSET
654 #define TARGET_ASAN_SHADOW_OFFSET arm_asan_shadow_offset
655
656 struct gcc_target targetm = TARGET_INITIALIZER;
657 \f
658 /* Obstack for minipool constant handling.  */
659 static struct obstack minipool_obstack;
660 static char *         minipool_startobj;
661
662 /* The maximum number of insns skipped which
663    will be conditionalised if possible.  */
664 static int max_insns_skipped = 5;
665
666 extern FILE * asm_out_file;
667
668 /* True if we are currently building a constant table.  */
669 int making_const_table;
670
671 /* The processor for which instructions should be scheduled.  */
672 enum processor_type arm_tune = arm_none;
673
674 /* The current tuning set.  */
675 const struct tune_params *current_tune;
676
677 /* Which floating point hardware to schedule for.  */
678 int arm_fpu_attr;
679
680 /* Which floating popint hardware to use.  */
681 const struct arm_fpu_desc *arm_fpu_desc;
682
683 /* Used for Thumb call_via trampolines.  */
684 rtx thumb_call_via_label[14];
685 static int thumb_call_reg_needed;
686
687 /* Bit values used to identify processor capabilities.  */
688 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
689 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
690 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
691 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
692 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
693 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
694 #define FL_THUMB      (1 << 6)        /* Thumb aware */
695 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
696 #define FL_STRONG     (1 << 8)        /* StrongARM */
697 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
698 #define FL_XSCALE     (1 << 10)       /* XScale */
699 /* spare              (1 << 11) */
700 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
701                                          media instructions.  */
702 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
703 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
704                                          Note: ARM6 & 7 derivatives only.  */
705 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
706 #define FL_THUMB2     (1 << 16)       /* Thumb-2.  */
707 #define FL_NOTM       (1 << 17)       /* Instructions not present in the 'M'
708                                          profile.  */
709 #define FL_THUMB_DIV  (1 << 18)       /* Hardware divide (Thumb mode).  */
710 #define FL_VFPV3      (1 << 19)       /* Vector Floating Point V3.  */
711 #define FL_NEON       (1 << 20)       /* Neon instructions.  */
712 #define FL_ARCH7EM    (1 << 21)       /* Instructions present in the ARMv7E-M
713                                          architecture.  */
714 #define FL_ARCH7      (1 << 22)       /* Architecture 7.  */
715 #define FL_ARM_DIV    (1 << 23)       /* Hardware divide (ARM mode).  */
716 #define FL_ARCH8      (1 << 24)       /* Architecture 8.  */
717
718 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
719 #define FL_IWMMXT2    (1 << 30)       /* "Intel Wireless MMX2 technology".  */
720
721 /* Flags that only effect tuning, not available instructions.  */
722 #define FL_TUNE         (FL_WBUF | FL_VFPV2 | FL_STRONG | FL_LDSCHED \
723                          | FL_CO_PROC)
724
725 #define FL_FOR_ARCH2    FL_NOTM
726 #define FL_FOR_ARCH3    (FL_FOR_ARCH2 | FL_MODE32)
727 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
728 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
729 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
730 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
731 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
732 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
733 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
734 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
735 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
736 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
737 #define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
738 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
739 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
740 #define FL_FOR_ARCH6T2  (FL_FOR_ARCH6 | FL_THUMB2)
741 #define FL_FOR_ARCH6M   (FL_FOR_ARCH6 & ~FL_NOTM)
742 #define FL_FOR_ARCH7    ((FL_FOR_ARCH6T2 & ~FL_NOTM) | FL_ARCH7)
743 #define FL_FOR_ARCH7A   (FL_FOR_ARCH7 | FL_NOTM | FL_ARCH6K)
744 #define FL_FOR_ARCH7R   (FL_FOR_ARCH7A | FL_THUMB_DIV)
745 #define FL_FOR_ARCH7M   (FL_FOR_ARCH7 | FL_THUMB_DIV)
746 #define FL_FOR_ARCH7EM  (FL_FOR_ARCH7M | FL_ARCH7EM)
747 #define FL_FOR_ARCH8A   (FL_FOR_ARCH7 | FL_ARCH6K | FL_ARCH8 | FL_THUMB_DIV \
748                          | FL_ARM_DIV | FL_NOTM)
749
750 /* The bits in this mask specify which
751    instructions we are allowed to generate.  */
752 static unsigned long insn_flags = 0;
753
754 /* The bits in this mask specify which instruction scheduling options should
755    be used.  */
756 static unsigned long tune_flags = 0;
757
758 /* The highest ARM architecture version supported by the
759    target.  */
760 enum base_architecture arm_base_arch = BASE_ARCH_0;
761
762 /* The following are used in the arm.md file as equivalents to bits
763    in the above two flag variables.  */
764
765 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
766 int arm_arch3m = 0;
767
768 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
769 int arm_arch4 = 0;
770
771 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
772 int arm_arch4t = 0;
773
774 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
775 int arm_arch5 = 0;
776
777 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
778 int arm_arch5e = 0;
779
780 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
781 int arm_arch6 = 0;
782
783 /* Nonzero if this chip supports the ARM 6K extensions.  */
784 int arm_arch6k = 0;
785
786 /* Nonzero if instructions present in ARMv6-M can be used.  */
787 int arm_arch6m = 0;
788
789 /* Nonzero if this chip supports the ARM 7 extensions.  */
790 int arm_arch7 = 0;
791
792 /* Nonzero if instructions not present in the 'M' profile can be used.  */
793 int arm_arch_notm = 0;
794
795 /* Nonzero if instructions present in ARMv7E-M can be used.  */
796 int arm_arch7em = 0;
797
798 /* Nonzero if instructions present in ARMv8 can be used.  */
799 int arm_arch8 = 0;
800
801 /* Nonzero if this chip can benefit from load scheduling.  */
802 int arm_ld_sched = 0;
803
804 /* Nonzero if this chip is a StrongARM.  */
805 int arm_tune_strongarm = 0;
806
807 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
808 int arm_arch_iwmmxt = 0;
809
810 /* Nonzero if this chip supports Intel Wireless MMX2 technology.  */
811 int arm_arch_iwmmxt2 = 0;
812
813 /* Nonzero if this chip is an XScale.  */
814 int arm_arch_xscale = 0;
815
816 /* Nonzero if tuning for XScale  */
817 int arm_tune_xscale = 0;
818
819 /* Nonzero if we want to tune for stores that access the write-buffer.
820    This typically means an ARM6 or ARM7 with MMU or MPU.  */
821 int arm_tune_wbuf = 0;
822
823 /* Nonzero if tuning for Cortex-A9.  */
824 int arm_tune_cortex_a9 = 0;
825
826 /* Nonzero if generating Thumb instructions.  */
827 int thumb_code = 0;
828
829 /* Nonzero if generating Thumb-1 instructions.  */
830 int thumb1_code = 0;
831
832 /* Nonzero if we should define __THUMB_INTERWORK__ in the
833    preprocessor.
834    XXX This is a bit of a hack, it's intended to help work around
835    problems in GLD which doesn't understand that armv5t code is
836    interworking clean.  */
837 int arm_cpp_interwork = 0;
838
839 /* Nonzero if chip supports Thumb 2.  */
840 int arm_arch_thumb2;
841
842 /* Nonzero if chip supports integer division instruction.  */
843 int arm_arch_arm_hwdiv;
844 int arm_arch_thumb_hwdiv;
845
846 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference,
847    we must report the mode of the memory reference from
848    TARGET_PRINT_OPERAND to TARGET_PRINT_OPERAND_ADDRESS.  */
849 enum machine_mode output_memory_reference_mode;
850
851 /* The register number to be used for the PIC offset register.  */
852 unsigned arm_pic_register = INVALID_REGNUM;
853
854 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
855    the next function.  */
856 static int after_arm_reorg = 0;
857
858 enum arm_pcs arm_pcs_default;
859
860 /* For an explanation of these variables, see final_prescan_insn below.  */
861 int arm_ccfsm_state;
862 /* arm_current_cc is also used for Thumb-2 cond_exec blocks.  */
863 enum arm_cond_code arm_current_cc;
864
865 rtx arm_target_insn;
866 int arm_target_label;
867 /* The number of conditionally executed insns, including the current insn.  */
868 int arm_condexec_count = 0;
869 /* A bitmask specifying the patterns for the IT block.
870    Zero means do not output an IT block before this insn. */
871 int arm_condexec_mask = 0;
872 /* The number of bits used in arm_condexec_mask.  */
873 int arm_condexec_masklen = 0;
874
875 /* The condition codes of the ARM, and the inverse function.  */
876 static const char * const arm_condition_codes[] =
877 {
878   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
879   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
880 };
881
882 /* The register numbers in sequence, for passing to arm_gen_load_multiple.  */
883 int arm_regs_in_sequence[] =
884 {
885   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
886 };
887
888 #define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
889 #define streq(string1, string2) (strcmp (string1, string2) == 0)
890
891 #define THUMB2_WORK_REGS (0xff & ~(  (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
892                                    | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
893                                    | (1 << PIC_OFFSET_TABLE_REGNUM)))
894 \f
895 /* Initialization code.  */
896
897 struct processors
898 {
899   const char *const name;
900   enum processor_type core;
901   const char *arch;
902   enum base_architecture base_arch;
903   const unsigned long flags;
904   const struct tune_params *const tune;
905 };
906
907
908 #define ARM_PREFETCH_NOT_BENEFICIAL 0, -1, -1
909 #define ARM_PREFETCH_BENEFICIAL(prefetch_slots,l1_size,l1_line_size) \
910   prefetch_slots, \
911   l1_size, \
912   l1_line_size
913
914 /* arm generic vectorizer costs.  */
915 static const
916 struct cpu_vec_costs arm_default_vec_cost = {
917   1,                                    /* scalar_stmt_cost.  */
918   1,                                    /* scalar load_cost.  */
919   1,                                    /* scalar_store_cost.  */
920   1,                                    /* vec_stmt_cost.  */
921   1,                                    /* vec_to_scalar_cost.  */
922   1,                                    /* scalar_to_vec_cost.  */
923   1,                                    /* vec_align_load_cost.  */
924   1,                                    /* vec_unalign_load_cost.  */
925   1,                                    /* vec_unalign_store_cost.  */
926   1,                                    /* vec_store_cost.  */
927   3,                                    /* cond_taken_branch_cost.  */
928   1,                                    /* cond_not_taken_branch_cost.  */
929 };
930
931 const struct tune_params arm_slowmul_tune =
932 {
933   arm_slowmul_rtx_costs,
934   NULL,
935   3,                                            /* Constant limit.  */
936   5,                                            /* Max cond insns.  */
937   ARM_PREFETCH_NOT_BENEFICIAL,
938   true,                                         /* Prefer constant pool.  */
939   arm_default_branch_cost,
940   false,                                        /* Prefer LDRD/STRD.  */
941   {true, true},                                 /* Prefer non short circuit.  */
942   &arm_default_vec_cost,                        /* Vectorizer costs.  */
943 };
944
945 const struct tune_params arm_fastmul_tune =
946 {
947   arm_fastmul_rtx_costs,
948   NULL,
949   1,                                            /* Constant limit.  */
950   5,                                            /* Max cond insns.  */
951   ARM_PREFETCH_NOT_BENEFICIAL,
952   true,                                         /* Prefer constant pool.  */
953   arm_default_branch_cost,
954   false,                                        /* Prefer LDRD/STRD.  */
955   {true, true},                                 /* Prefer non short circuit.  */
956   &arm_default_vec_cost,                        /* Vectorizer costs.  */
957 };
958
959 /* StrongARM has early execution of branches, so a sequence that is worth
960    skipping is shorter.  Set max_insns_skipped to a lower value.  */
961
962 const struct tune_params arm_strongarm_tune =
963 {
964   arm_fastmul_rtx_costs,
965   NULL,
966   1,                                            /* Constant limit.  */
967   3,                                            /* Max cond insns.  */
968   ARM_PREFETCH_NOT_BENEFICIAL,
969   true,                                         /* Prefer constant pool.  */
970   arm_default_branch_cost,
971   false,                                        /* Prefer LDRD/STRD.  */
972   {true, true},                                 /* Prefer non short circuit.  */
973   &arm_default_vec_cost,                        /* Vectorizer costs.  */
974 };
975
976 const struct tune_params arm_xscale_tune =
977 {
978   arm_xscale_rtx_costs,
979   xscale_sched_adjust_cost,
980   2,                                            /* Constant limit.  */
981   3,                                            /* Max cond insns.  */
982   ARM_PREFETCH_NOT_BENEFICIAL,
983   true,                                         /* Prefer constant pool.  */
984   arm_default_branch_cost,
985   false,                                        /* Prefer LDRD/STRD.  */
986   {true, true},                                 /* Prefer non short circuit.  */
987   &arm_default_vec_cost,                        /* Vectorizer costs.  */
988 };
989
990 const struct tune_params arm_9e_tune =
991 {
992   arm_9e_rtx_costs,
993   NULL,
994   1,                                            /* Constant limit.  */
995   5,                                            /* Max cond insns.  */
996   ARM_PREFETCH_NOT_BENEFICIAL,
997   true,                                         /* Prefer constant pool.  */
998   arm_default_branch_cost,
999   false,                                        /* Prefer LDRD/STRD.  */
1000   {true, true},                                 /* Prefer non short circuit.  */
1001   &arm_default_vec_cost,                        /* Vectorizer costs.  */
1002 };
1003
1004 const struct tune_params arm_v6t2_tune =
1005 {
1006   arm_9e_rtx_costs,
1007   NULL,
1008   1,                                            /* Constant limit.  */
1009   5,                                            /* Max cond insns.  */
1010   ARM_PREFETCH_NOT_BENEFICIAL,
1011   false,                                        /* Prefer constant pool.  */
1012   arm_default_branch_cost,
1013   false,                                        /* Prefer LDRD/STRD.  */
1014   {true, true},                                 /* Prefer non short circuit.  */
1015   &arm_default_vec_cost,                        /* Vectorizer costs.  */
1016 };
1017
1018 /* Generic Cortex tuning.  Use more specific tunings if appropriate.  */
1019 const struct tune_params arm_cortex_tune =
1020 {
1021   arm_9e_rtx_costs,
1022   NULL,
1023   1,                                            /* Constant limit.  */
1024   5,                                            /* Max cond insns.  */
1025   ARM_PREFETCH_NOT_BENEFICIAL,
1026   false,                                        /* Prefer constant pool.  */
1027   arm_default_branch_cost,
1028   false,                                        /* Prefer LDRD/STRD.  */
1029   {true, true},                                 /* Prefer non short circuit.  */
1030   &arm_default_vec_cost,                        /* Vectorizer costs.  */
1031 };
1032
1033 const struct tune_params arm_cortex_a15_tune =
1034 {
1035   arm_9e_rtx_costs,
1036   NULL,
1037   1,                                            /* Constant limit.  */
1038   5,                                            /* Max cond insns.  */
1039   ARM_PREFETCH_NOT_BENEFICIAL,
1040   false,                                        /* Prefer constant pool.  */
1041   arm_default_branch_cost,
1042   true,                                         /* Prefer LDRD/STRD.  */
1043   {true, true},                                 /* Prefer non short circuit.  */
1044   &arm_default_vec_cost,                        /* Vectorizer costs.  */
1045 };
1046
1047 /* Branches can be dual-issued on Cortex-A5, so conditional execution is
1048    less appealing.  Set max_insns_skipped to a low value.  */
1049
1050 const struct tune_params arm_cortex_a5_tune =
1051 {
1052   arm_9e_rtx_costs,
1053   NULL,
1054   1,                                            /* Constant limit.  */
1055   1,                                            /* Max cond insns.  */
1056   ARM_PREFETCH_NOT_BENEFICIAL,
1057   false,                                        /* Prefer constant pool.  */
1058   arm_cortex_a5_branch_cost,
1059   false,                                        /* Prefer LDRD/STRD.  */
1060   {false, false},                               /* Prefer non short circuit.  */
1061   &arm_default_vec_cost,                        /* Vectorizer costs.  */
1062 };
1063
1064 const struct tune_params arm_cortex_a9_tune =
1065 {
1066   arm_9e_rtx_costs,
1067   cortex_a9_sched_adjust_cost,
1068   1,                                            /* Constant limit.  */
1069   5,                                            /* Max cond insns.  */
1070   ARM_PREFETCH_BENEFICIAL(4,32,32),
1071   false,                                        /* Prefer constant pool.  */
1072   arm_default_branch_cost,
1073   false,                                        /* Prefer LDRD/STRD.  */
1074   {true, true},                                 /* Prefer non short circuit.  */
1075   &arm_default_vec_cost,                        /* Vectorizer costs.  */
1076 };
1077
1078 /* The arm_v6m_tune is duplicated from arm_cortex_tune, rather than
1079    arm_v6t2_tune. It is used for cortex-m0, cortex-m1 and cortex-m0plus.  */
1080 const struct tune_params arm_v6m_tune =
1081 {
1082   arm_9e_rtx_costs,
1083   NULL,
1084   1,                                            /* Constant limit.  */
1085   5,                                            /* Max cond insns.  */
1086   ARM_PREFETCH_NOT_BENEFICIAL,
1087   false,                                        /* Prefer constant pool.  */
1088   arm_default_branch_cost,
1089   false,                                        /* Prefer LDRD/STRD.  */
1090   {false, false},                               /* Prefer non short circuit.  */
1091   &arm_default_vec_cost,                        /* Vectorizer costs.  */
1092 };
1093
1094 const struct tune_params arm_fa726te_tune =
1095 {
1096   arm_9e_rtx_costs,
1097   fa726te_sched_adjust_cost,
1098   1,                                            /* Constant limit.  */
1099   5,                                            /* Max cond insns.  */
1100   ARM_PREFETCH_NOT_BENEFICIAL,
1101   true,                                         /* Prefer constant pool.  */
1102   arm_default_branch_cost,
1103   false,                                        /* Prefer LDRD/STRD.  */
1104   {true, true},                                 /* Prefer non short circuit.  */
1105   &arm_default_vec_cost,                        /* Vectorizer costs.  */
1106 };
1107
1108
1109 /* Not all of these give usefully different compilation alternatives,
1110    but there is no simple way of generalizing them.  */
1111 static const struct processors all_cores[] =
1112 {
1113   /* ARM Cores */
1114 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
1115   {NAME, IDENT, #ARCH, BASE_ARCH_##ARCH,          \
1116     FLAGS | FL_FOR_ARCH##ARCH, &arm_##COSTS##_tune},
1117 #include "arm-cores.def"
1118 #undef ARM_CORE
1119   {NULL, arm_none, NULL, BASE_ARCH_0, 0, NULL}
1120 };
1121
1122 static const struct processors all_architectures[] =
1123 {
1124   /* ARM Architectures */
1125   /* We don't specify tuning costs here as it will be figured out
1126      from the core.  */
1127
1128 #define ARM_ARCH(NAME, CORE, ARCH, FLAGS) \
1129   {NAME, CORE, #ARCH, BASE_ARCH_##ARCH, FLAGS, NULL},
1130 #include "arm-arches.def"
1131 #undef ARM_ARCH
1132   {NULL, arm_none, NULL, BASE_ARCH_0, 0, NULL}
1133 };
1134
1135
1136 /* These are populated as commandline arguments are processed, or NULL
1137    if not specified.  */
1138 static const struct processors *arm_selected_arch;
1139 static const struct processors *arm_selected_cpu;
1140 static const struct processors *arm_selected_tune;
1141
1142 /* The name of the preprocessor macro to define for this architecture.  */
1143
1144 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
1145
1146 /* Available values for -mfpu=.  */
1147
1148 static const struct arm_fpu_desc all_fpus[] =
1149 {
1150 #define ARM_FPU(NAME, MODEL, REV, VFP_REGS, NEON, FP16, CRYPTO) \
1151   { NAME, MODEL, REV, VFP_REGS, NEON, FP16, CRYPTO },
1152 #include "arm-fpus.def"
1153 #undef ARM_FPU
1154 };
1155
1156
1157 /* Supported TLS relocations.  */
1158
1159 enum tls_reloc {
1160   TLS_GD32,
1161   TLS_LDM32,
1162   TLS_LDO32,
1163   TLS_IE32,
1164   TLS_LE32,
1165   TLS_DESCSEQ   /* GNU scheme */
1166 };
1167
1168 /* The maximum number of insns to be used when loading a constant.  */
1169 inline static int
1170 arm_constant_limit (bool size_p)
1171 {
1172   return size_p ? 1 : current_tune->constant_limit;
1173 }
1174
1175 /* Emit an insn that's a simple single-set.  Both the operands must be known
1176    to be valid.  */
1177 inline static rtx
1178 emit_set_insn (rtx x, rtx y)
1179 {
1180   return emit_insn (gen_rtx_SET (VOIDmode, x, y));
1181 }
1182
1183 /* Return the number of bits set in VALUE.  */
1184 static unsigned
1185 bit_count (unsigned long value)
1186 {
1187   unsigned long count = 0;
1188
1189   while (value)
1190     {
1191       count++;
1192       value &= value - 1;  /* Clear the least-significant set bit.  */
1193     }
1194
1195   return count;
1196 }
1197
1198 typedef struct
1199 {
1200   enum machine_mode mode;
1201   const char *name;
1202 } arm_fixed_mode_set;
1203
1204 /* A small helper for setting fixed-point library libfuncs.  */
1205
1206 static void
1207 arm_set_fixed_optab_libfunc (optab optable, enum machine_mode mode,
1208                              const char *funcname, const char *modename,
1209                              int num_suffix)
1210 {
1211   char buffer[50];
1212
1213   if (num_suffix == 0)
1214     sprintf (buffer, "__gnu_%s%s", funcname, modename);
1215   else
1216     sprintf (buffer, "__gnu_%s%s%d", funcname, modename, num_suffix);
1217
1218   set_optab_libfunc (optable, mode, buffer);
1219 }
1220
1221 static void
1222 arm_set_fixed_conv_libfunc (convert_optab optable, enum machine_mode to,
1223                             enum machine_mode from, const char *funcname,
1224                             const char *toname, const char *fromname)
1225 {
1226   char buffer[50];
1227   const char *maybe_suffix_2 = "";
1228
1229   /* Follow the logic for selecting a "2" suffix in fixed-bit.h.  */
1230   if (ALL_FIXED_POINT_MODE_P (from) && ALL_FIXED_POINT_MODE_P (to)
1231       && UNSIGNED_FIXED_POINT_MODE_P (from) == UNSIGNED_FIXED_POINT_MODE_P (to)
1232       && ALL_FRACT_MODE_P (from) == ALL_FRACT_MODE_P (to))
1233     maybe_suffix_2 = "2";
1234
1235   sprintf (buffer, "__gnu_%s%s%s%s", funcname, fromname, toname,
1236            maybe_suffix_2);
1237
1238   set_conv_libfunc (optable, to, from, buffer);
1239 }
1240
1241 /* Set up library functions unique to ARM.  */
1242
1243 static void
1244 arm_init_libfuncs (void)
1245 {
1246   /* For Linux, we have access to kernel support for atomic operations.  */
1247   if (arm_abi == ARM_ABI_AAPCS_LINUX)
1248     init_sync_libfuncs (2 * UNITS_PER_WORD);
1249
1250   /* There are no special library functions unless we are using the
1251      ARM BPABI.  */
1252   if (!TARGET_BPABI)
1253     return;
1254
1255   /* The functions below are described in Section 4 of the "Run-Time
1256      ABI for the ARM architecture", Version 1.0.  */
1257
1258   /* Double-precision floating-point arithmetic.  Table 2.  */
1259   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
1260   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
1261   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
1262   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
1263   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
1264
1265   /* Double-precision comparisons.  Table 3.  */
1266   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
1267   set_optab_libfunc (ne_optab, DFmode, NULL);
1268   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
1269   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
1270   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
1271   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
1272   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
1273
1274   /* Single-precision floating-point arithmetic.  Table 4.  */
1275   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
1276   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
1277   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
1278   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
1279   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
1280
1281   /* Single-precision comparisons.  Table 5.  */
1282   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
1283   set_optab_libfunc (ne_optab, SFmode, NULL);
1284   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
1285   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
1286   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
1287   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
1288   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
1289
1290   /* Floating-point to integer conversions.  Table 6.  */
1291   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
1292   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
1293   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
1294   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
1295   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
1296   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
1297   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
1298   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
1299
1300   /* Conversions between floating types.  Table 7.  */
1301   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
1302   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
1303
1304   /* Integer to floating-point conversions.  Table 8.  */
1305   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
1306   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
1307   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
1308   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
1309   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
1310   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
1311   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
1312   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
1313
1314   /* Long long.  Table 9.  */
1315   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
1316   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
1317   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
1318   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
1319   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
1320   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
1321   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
1322   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
1323
1324   /* Integer (32/32->32) division.  \S 4.3.1.  */
1325   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
1326   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
1327
1328   /* The divmod functions are designed so that they can be used for
1329      plain division, even though they return both the quotient and the
1330      remainder.  The quotient is returned in the usual location (i.e.,
1331      r0 for SImode, {r0, r1} for DImode), just as would be expected
1332      for an ordinary division routine.  Because the AAPCS calling
1333      conventions specify that all of { r0, r1, r2, r3 } are
1334      callee-saved registers, there is no need to tell the compiler
1335      explicitly that those registers are clobbered by these
1336      routines.  */
1337   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
1338   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
1339
1340   /* For SImode division the ABI provides div-without-mod routines,
1341      which are faster.  */
1342   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
1343   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
1344
1345   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
1346      divmod libcalls instead.  */
1347   set_optab_libfunc (smod_optab, DImode, NULL);
1348   set_optab_libfunc (umod_optab, DImode, NULL);
1349   set_optab_libfunc (smod_optab, SImode, NULL);
1350   set_optab_libfunc (umod_optab, SImode, NULL);
1351
1352   /* Half-precision float operations.  The compiler handles all operations
1353      with NULL libfuncs by converting the SFmode.  */
1354   switch (arm_fp16_format)
1355     {
1356     case ARM_FP16_FORMAT_IEEE:
1357     case ARM_FP16_FORMAT_ALTERNATIVE:
1358
1359       /* Conversions.  */
1360       set_conv_libfunc (trunc_optab, HFmode, SFmode,
1361                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1362                          ? "__gnu_f2h_ieee"
1363                          : "__gnu_f2h_alternative"));
1364       set_conv_libfunc (sext_optab, SFmode, HFmode,
1365                         (arm_fp16_format == ARM_FP16_FORMAT_IEEE
1366                          ? "__gnu_h2f_ieee"
1367                          : "__gnu_h2f_alternative"));
1368
1369       /* Arithmetic.  */
1370       set_optab_libfunc (add_optab, HFmode, NULL);
1371       set_optab_libfunc (sdiv_optab, HFmode, NULL);
1372       set_optab_libfunc (smul_optab, HFmode, NULL);
1373       set_optab_libfunc (neg_optab, HFmode, NULL);
1374       set_optab_libfunc (sub_optab, HFmode, NULL);
1375
1376       /* Comparisons.  */
1377       set_optab_libfunc (eq_optab, HFmode, NULL);
1378       set_optab_libfunc (ne_optab, HFmode, NULL);
1379       set_optab_libfunc (lt_optab, HFmode, NULL);
1380       set_optab_libfunc (le_optab, HFmode, NULL);
1381       set_optab_libfunc (ge_optab, HFmode, NULL);
1382       set_optab_libfunc (gt_optab, HFmode, NULL);
1383       set_optab_libfunc (unord_optab, HFmode, NULL);
1384       break;
1385
1386     default:
1387       break;
1388     }
1389
1390   /* Use names prefixed with __gnu_ for fixed-point helper functions.  */
1391   {
1392     const arm_fixed_mode_set fixed_arith_modes[] =
1393       {
1394         { QQmode, "qq" },
1395         { UQQmode, "uqq" },
1396         { HQmode, "hq" },
1397         { UHQmode, "uhq" },
1398         { SQmode, "sq" },
1399         { USQmode, "usq" },
1400         { DQmode, "dq" },
1401         { UDQmode, "udq" },
1402         { TQmode, "tq" },
1403         { UTQmode, "utq" },
1404         { HAmode, "ha" },
1405         { UHAmode, "uha" },
1406         { SAmode, "sa" },
1407         { USAmode, "usa" },
1408         { DAmode, "da" },
1409         { UDAmode, "uda" },
1410         { TAmode, "ta" },
1411         { UTAmode, "uta" }
1412       };
1413     const arm_fixed_mode_set fixed_conv_modes[] =
1414       {
1415         { QQmode, "qq" },
1416         { UQQmode, "uqq" },
1417         { HQmode, "hq" },
1418         { UHQmode, "uhq" },
1419         { SQmode, "sq" },
1420         { USQmode, "usq" },
1421         { DQmode, "dq" },
1422         { UDQmode, "udq" },
1423         { TQmode, "tq" },
1424         { UTQmode, "utq" },
1425         { HAmode, "ha" },
1426         { UHAmode, "uha" },
1427         { SAmode, "sa" },
1428         { USAmode, "usa" },
1429         { DAmode, "da" },
1430         { UDAmode, "uda" },
1431         { TAmode, "ta" },
1432         { UTAmode, "uta" },
1433         { QImode, "qi" },
1434         { HImode, "hi" },
1435         { SImode, "si" },
1436         { DImode, "di" },
1437         { TImode, "ti" },
1438         { SFmode, "sf" },
1439         { DFmode, "df" }
1440       };
1441     unsigned int i, j;
1442
1443     for (i = 0; i < ARRAY_SIZE (fixed_arith_modes); i++)
1444       {
1445         arm_set_fixed_optab_libfunc (add_optab, fixed_arith_modes[i].mode,
1446                                      "add", fixed_arith_modes[i].name, 3);
1447         arm_set_fixed_optab_libfunc (ssadd_optab, fixed_arith_modes[i].mode,
1448                                      "ssadd", fixed_arith_modes[i].name, 3);
1449         arm_set_fixed_optab_libfunc (usadd_optab, fixed_arith_modes[i].mode,
1450                                      "usadd", fixed_arith_modes[i].name, 3);
1451         arm_set_fixed_optab_libfunc (sub_optab, fixed_arith_modes[i].mode,
1452                                      "sub", fixed_arith_modes[i].name, 3);
1453         arm_set_fixed_optab_libfunc (sssub_optab, fixed_arith_modes[i].mode,
1454                                      "sssub", fixed_arith_modes[i].name, 3);
1455         arm_set_fixed_optab_libfunc (ussub_optab, fixed_arith_modes[i].mode,
1456                                      "ussub", fixed_arith_modes[i].name, 3);
1457         arm_set_fixed_optab_libfunc (smul_optab, fixed_arith_modes[i].mode,
1458                                      "mul", fixed_arith_modes[i].name, 3);
1459         arm_set_fixed_optab_libfunc (ssmul_optab, fixed_arith_modes[i].mode,
1460                                      "ssmul", fixed_arith_modes[i].name, 3);
1461         arm_set_fixed_optab_libfunc (usmul_optab, fixed_arith_modes[i].mode,
1462                                      "usmul", fixed_arith_modes[i].name, 3);
1463         arm_set_fixed_optab_libfunc (sdiv_optab, fixed_arith_modes[i].mode,
1464                                      "div", fixed_arith_modes[i].name, 3);
1465         arm_set_fixed_optab_libfunc (udiv_optab, fixed_arith_modes[i].mode,
1466                                      "udiv", fixed_arith_modes[i].name, 3);
1467         arm_set_fixed_optab_libfunc (ssdiv_optab, fixed_arith_modes[i].mode,
1468                                      "ssdiv", fixed_arith_modes[i].name, 3);
1469         arm_set_fixed_optab_libfunc (usdiv_optab, fixed_arith_modes[i].mode,
1470                                      "usdiv", fixed_arith_modes[i].name, 3);
1471         arm_set_fixed_optab_libfunc (neg_optab, fixed_arith_modes[i].mode,
1472                                      "neg", fixed_arith_modes[i].name, 2);
1473         arm_set_fixed_optab_libfunc (ssneg_optab, fixed_arith_modes[i].mode,
1474                                      "ssneg", fixed_arith_modes[i].name, 2);
1475         arm_set_fixed_optab_libfunc (usneg_optab, fixed_arith_modes[i].mode,
1476                                      "usneg", fixed_arith_modes[i].name, 2);
1477         arm_set_fixed_optab_libfunc (ashl_optab, fixed_arith_modes[i].mode,
1478                                      "ashl", fixed_arith_modes[i].name, 3);
1479         arm_set_fixed_optab_libfunc (ashr_optab, fixed_arith_modes[i].mode,
1480                                      "ashr", fixed_arith_modes[i].name, 3);
1481         arm_set_fixed_optab_libfunc (lshr_optab, fixed_arith_modes[i].mode,
1482                                      "lshr", fixed_arith_modes[i].name, 3);
1483         arm_set_fixed_optab_libfunc (ssashl_optab, fixed_arith_modes[i].mode,
1484                                      "ssashl", fixed_arith_modes[i].name, 3);
1485         arm_set_fixed_optab_libfunc (usashl_optab, fixed_arith_modes[i].mode,
1486                                      "usashl", fixed_arith_modes[i].name, 3);
1487         arm_set_fixed_optab_libfunc (cmp_optab, fixed_arith_modes[i].mode,
1488                                      "cmp", fixed_arith_modes[i].name, 2);
1489       }
1490
1491     for (i = 0; i < ARRAY_SIZE (fixed_conv_modes); i++)
1492       for (j = 0; j < ARRAY_SIZE (fixed_conv_modes); j++)
1493         {
1494           if (i == j
1495               || (!ALL_FIXED_POINT_MODE_P (fixed_conv_modes[i].mode)
1496                   && !ALL_FIXED_POINT_MODE_P (fixed_conv_modes[j].mode)))
1497             continue;
1498
1499           arm_set_fixed_conv_libfunc (fract_optab, fixed_conv_modes[i].mode,
1500                                       fixed_conv_modes[j].mode, "fract",
1501                                       fixed_conv_modes[i].name,
1502                                       fixed_conv_modes[j].name);
1503           arm_set_fixed_conv_libfunc (satfract_optab,
1504                                       fixed_conv_modes[i].mode,
1505                                       fixed_conv_modes[j].mode, "satfract",
1506                                       fixed_conv_modes[i].name,
1507                                       fixed_conv_modes[j].name);
1508           arm_set_fixed_conv_libfunc (fractuns_optab,
1509                                       fixed_conv_modes[i].mode,
1510                                       fixed_conv_modes[j].mode, "fractuns",
1511                                       fixed_conv_modes[i].name,
1512                                       fixed_conv_modes[j].name);
1513           arm_set_fixed_conv_libfunc (satfractuns_optab,
1514                                       fixed_conv_modes[i].mode,
1515                                       fixed_conv_modes[j].mode, "satfractuns",
1516                                       fixed_conv_modes[i].name,
1517                                       fixed_conv_modes[j].name);
1518         }
1519   }
1520
1521   if (TARGET_AAPCS_BASED)
1522     synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
1523 }
1524
1525 /* On AAPCS systems, this is the "struct __va_list".  */
1526 static GTY(()) tree va_list_type;
1527
1528 /* Return the type to use as __builtin_va_list.  */
1529 static tree
1530 arm_build_builtin_va_list (void)
1531 {
1532   tree va_list_name;
1533   tree ap_field;
1534
1535   if (!TARGET_AAPCS_BASED)
1536     return std_build_builtin_va_list ();
1537
1538   /* AAPCS \S 7.1.4 requires that va_list be a typedef for a type
1539      defined as:
1540
1541        struct __va_list
1542        {
1543          void *__ap;
1544        };
1545
1546      The C Library ABI further reinforces this definition in \S
1547      4.1.
1548
1549      We must follow this definition exactly.  The structure tag
1550      name is visible in C++ mangled names, and thus forms a part
1551      of the ABI.  The field name may be used by people who
1552      #include <stdarg.h>.  */
1553   /* Create the type.  */
1554   va_list_type = lang_hooks.types.make_type (RECORD_TYPE);
1555   /* Give it the required name.  */
1556   va_list_name = build_decl (BUILTINS_LOCATION,
1557                              TYPE_DECL,
1558                              get_identifier ("__va_list"),
1559                              va_list_type);
1560   DECL_ARTIFICIAL (va_list_name) = 1;
1561   TYPE_NAME (va_list_type) = va_list_name;
1562   TYPE_STUB_DECL (va_list_type) = va_list_name;
1563   /* Create the __ap field.  */
1564   ap_field = build_decl (BUILTINS_LOCATION,
1565                          FIELD_DECL,
1566                          get_identifier ("__ap"),
1567                          ptr_type_node);
1568   DECL_ARTIFICIAL (ap_field) = 1;
1569   DECL_FIELD_CONTEXT (ap_field) = va_list_type;
1570   TYPE_FIELDS (va_list_type) = ap_field;
1571   /* Compute its layout.  */
1572   layout_type (va_list_type);
1573
1574   return va_list_type;
1575 }
1576
1577 /* Return an expression of type "void *" pointing to the next
1578    available argument in a variable-argument list.  VALIST is the
1579    user-level va_list object, of type __builtin_va_list.  */
1580 static tree
1581 arm_extract_valist_ptr (tree valist)
1582 {
1583   if (TREE_TYPE (valist) == error_mark_node)
1584     return error_mark_node;
1585
1586   /* On an AAPCS target, the pointer is stored within "struct
1587      va_list".  */
1588   if (TARGET_AAPCS_BASED)
1589     {
1590       tree ap_field = TYPE_FIELDS (TREE_TYPE (valist));
1591       valist = build3 (COMPONENT_REF, TREE_TYPE (ap_field),
1592                        valist, ap_field, NULL_TREE);
1593     }
1594
1595   return valist;
1596 }
1597
1598 /* Implement TARGET_EXPAND_BUILTIN_VA_START.  */
1599 static void
1600 arm_expand_builtin_va_start (tree valist, rtx nextarg)
1601 {
1602   valist = arm_extract_valist_ptr (valist);
1603   std_expand_builtin_va_start (valist, nextarg);
1604 }
1605
1606 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR.  */
1607 static tree
1608 arm_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
1609                           gimple_seq *post_p)
1610 {
1611   valist = arm_extract_valist_ptr (valist);
1612   return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
1613 }
1614
1615 /* Fix up any incompatible options that the user has specified.  */
1616 static void
1617 arm_option_override (void)
1618 {
1619   if (global_options_set.x_arm_arch_option)
1620     arm_selected_arch = &all_architectures[arm_arch_option];
1621
1622   if (global_options_set.x_arm_cpu_option)
1623     arm_selected_cpu = &all_cores[(int) arm_cpu_option];
1624
1625   if (global_options_set.x_arm_tune_option)
1626     arm_selected_tune = &all_cores[(int) arm_tune_option];
1627
1628 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1629   SUBTARGET_OVERRIDE_OPTIONS;
1630 #endif
1631
1632   if (arm_selected_arch)
1633     {
1634       if (arm_selected_cpu)
1635         {
1636           /* Check for conflict between mcpu and march.  */
1637           if ((arm_selected_cpu->flags ^ arm_selected_arch->flags) & ~FL_TUNE)
1638             {
1639               warning (0, "switch -mcpu=%s conflicts with -march=%s switch",
1640                        arm_selected_cpu->name, arm_selected_arch->name);
1641               /* -march wins for code generation.
1642                  -mcpu wins for default tuning.  */
1643               if (!arm_selected_tune)
1644                 arm_selected_tune = arm_selected_cpu;
1645
1646               arm_selected_cpu = arm_selected_arch;
1647             }
1648           else
1649             /* -mcpu wins.  */
1650             arm_selected_arch = NULL;
1651         }
1652       else
1653         /* Pick a CPU based on the architecture.  */
1654         arm_selected_cpu = arm_selected_arch;
1655     }
1656
1657   /* If the user did not specify a processor, choose one for them.  */
1658   if (!arm_selected_cpu)
1659     {
1660       const struct processors * sel;
1661       unsigned int        sought;
1662
1663       arm_selected_cpu = &all_cores[TARGET_CPU_DEFAULT];
1664       if (!arm_selected_cpu->name)
1665         {
1666 #ifdef SUBTARGET_CPU_DEFAULT
1667           /* Use the subtarget default CPU if none was specified by
1668              configure.  */
1669           arm_selected_cpu = &all_cores[SUBTARGET_CPU_DEFAULT];
1670 #endif
1671           /* Default to ARM6.  */
1672           if (!arm_selected_cpu->name)
1673             arm_selected_cpu = &all_cores[arm6];
1674         }
1675
1676       sel = arm_selected_cpu;
1677       insn_flags = sel->flags;
1678
1679       /* Now check to see if the user has specified some command line
1680          switch that require certain abilities from the cpu.  */
1681       sought = 0;
1682
1683       if (TARGET_INTERWORK || TARGET_THUMB)
1684         {
1685           sought |= (FL_THUMB | FL_MODE32);
1686
1687           /* There are no ARM processors that support both APCS-26 and
1688              interworking.  Therefore we force FL_MODE26 to be removed
1689              from insn_flags here (if it was set), so that the search
1690              below will always be able to find a compatible processor.  */
1691           insn_flags &= ~FL_MODE26;
1692         }
1693
1694       if (sought != 0 && ((sought & insn_flags) != sought))
1695         {
1696           /* Try to locate a CPU type that supports all of the abilities
1697              of the default CPU, plus the extra abilities requested by
1698              the user.  */
1699           for (sel = all_cores; sel->name != NULL; sel++)
1700             if ((sel->flags & sought) == (sought | insn_flags))
1701               break;
1702
1703           if (sel->name == NULL)
1704             {
1705               unsigned current_bit_count = 0;
1706               const struct processors * best_fit = NULL;
1707
1708               /* Ideally we would like to issue an error message here
1709                  saying that it was not possible to find a CPU compatible
1710                  with the default CPU, but which also supports the command
1711                  line options specified by the programmer, and so they
1712                  ought to use the -mcpu=<name> command line option to
1713                  override the default CPU type.
1714
1715                  If we cannot find a cpu that has both the
1716                  characteristics of the default cpu and the given
1717                  command line options we scan the array again looking
1718                  for a best match.  */
1719               for (sel = all_cores; sel->name != NULL; sel++)
1720                 if ((sel->flags & sought) == sought)
1721                   {
1722                     unsigned count;
1723
1724                     count = bit_count (sel->flags & insn_flags);
1725
1726                     if (count >= current_bit_count)
1727                       {
1728                         best_fit = sel;
1729                         current_bit_count = count;
1730                       }
1731                   }
1732
1733               gcc_assert (best_fit);
1734               sel = best_fit;
1735             }
1736
1737           arm_selected_cpu = sel;
1738         }
1739     }
1740
1741   gcc_assert (arm_selected_cpu);
1742   /* The selected cpu may be an architecture, so lookup tuning by core ID.  */
1743   if (!arm_selected_tune)
1744     arm_selected_tune = &all_cores[arm_selected_cpu->core];
1745
1746   sprintf (arm_arch_name, "__ARM_ARCH_%s__", arm_selected_cpu->arch);
1747   insn_flags = arm_selected_cpu->flags;
1748   arm_base_arch = arm_selected_cpu->base_arch;
1749
1750   arm_tune = arm_selected_tune->core;
1751   tune_flags = arm_selected_tune->flags;
1752   current_tune = arm_selected_tune->tune;
1753
1754   /* Make sure that the processor choice does not conflict with any of the
1755      other command line choices.  */
1756   if (TARGET_ARM && !(insn_flags & FL_NOTM))
1757     error ("target CPU does not support ARM mode");
1758
1759   /* BPABI targets use linker tricks to allow interworking on cores
1760      without thumb support.  */
1761   if (TARGET_INTERWORK && !((insn_flags & FL_THUMB) || TARGET_BPABI))
1762     {
1763       warning (0, "target CPU does not support interworking" );
1764       target_flags &= ~MASK_INTERWORK;
1765     }
1766
1767   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1768     {
1769       warning (0, "target CPU does not support THUMB instructions");
1770       target_flags &= ~MASK_THUMB;
1771     }
1772
1773   if (TARGET_APCS_FRAME && TARGET_THUMB)
1774     {
1775       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1776       target_flags &= ~MASK_APCS_FRAME;
1777     }
1778
1779   /* Callee super interworking implies thumb interworking.  Adding
1780      this to the flags here simplifies the logic elsewhere.  */
1781   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1782     target_flags |= MASK_INTERWORK;
1783
1784   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1785      from here where no function is being compiled currently.  */
1786   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1787     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1788
1789   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1790     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1791
1792   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1793     {
1794       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1795       target_flags |= MASK_APCS_FRAME;
1796     }
1797
1798   if (TARGET_POKE_FUNCTION_NAME)
1799     target_flags |= MASK_APCS_FRAME;
1800
1801   if (TARGET_APCS_REENT && flag_pic)
1802     error ("-fpic and -mapcs-reent are incompatible");
1803
1804   if (TARGET_APCS_REENT)
1805     warning (0, "APCS reentrant code not supported.  Ignored");
1806
1807   /* If this target is normally configured to use APCS frames, warn if they
1808      are turned off and debugging is turned on.  */
1809   if (TARGET_ARM
1810       && write_symbols != NO_DEBUG
1811       && !TARGET_APCS_FRAME
1812       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1813     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1814
1815   if (TARGET_APCS_FLOAT)
1816     warning (0, "passing floating point arguments in fp regs not yet supported");
1817
1818   if (TARGET_LITTLE_WORDS)
1819     warning (OPT_Wdeprecated, "%<mwords-little-endian%> is deprecated and "
1820              "will be removed in a future release");
1821
1822   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1823   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1824   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1825   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1826   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1827   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1828   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1829   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1830   arm_arch_notm = (insn_flags & FL_NOTM) != 0;
1831   arm_arch6m = arm_arch6 && !arm_arch_notm;
1832   arm_arch7 = (insn_flags & FL_ARCH7) != 0;
1833   arm_arch7em = (insn_flags & FL_ARCH7EM) != 0;
1834   arm_arch8 = (insn_flags & FL_ARCH8) != 0;
1835   arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
1836   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1837
1838   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1839   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1840   thumb_code = TARGET_ARM == 0;
1841   thumb1_code = TARGET_THUMB1 != 0;
1842   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1843   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1844   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1845   arm_arch_iwmmxt2 = (insn_flags & FL_IWMMXT2) != 0;
1846   arm_arch_thumb_hwdiv = (insn_flags & FL_THUMB_DIV) != 0;
1847   arm_arch_arm_hwdiv = (insn_flags & FL_ARM_DIV) != 0;
1848   arm_tune_cortex_a9 = (arm_tune == cortexa9) != 0;
1849
1850   /* If we are not using the default (ARM mode) section anchor offset
1851      ranges, then set the correct ranges now.  */
1852   if (TARGET_THUMB1)
1853     {
1854       /* Thumb-1 LDR instructions cannot have negative offsets.
1855          Permissible positive offset ranges are 5-bit (for byte loads),
1856          6-bit (for halfword loads), or 7-bit (for word loads).
1857          Empirical results suggest a 7-bit anchor range gives the best
1858          overall code size.  */
1859       targetm.min_anchor_offset = 0;
1860       targetm.max_anchor_offset = 127;
1861     }
1862   else if (TARGET_THUMB2)
1863     {
1864       /* The minimum is set such that the total size of the block
1865          for a particular anchor is 248 + 1 + 4095 bytes, which is
1866          divisible by eight, ensuring natural spacing of anchors.  */
1867       targetm.min_anchor_offset = -248;
1868       targetm.max_anchor_offset = 4095;
1869     }
1870
1871   /* V5 code we generate is completely interworking capable, so we turn off
1872      TARGET_INTERWORK here to avoid many tests later on.  */
1873
1874   /* XXX However, we must pass the right pre-processor defines to CPP
1875      or GLD can get confused.  This is a hack.  */
1876   if (TARGET_INTERWORK)
1877     arm_cpp_interwork = 1;
1878
1879   if (arm_arch5)
1880     target_flags &= ~MASK_INTERWORK;
1881
1882   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1883     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1884
1885   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1886     error ("iwmmxt abi requires an iwmmxt capable cpu");
1887
1888   if (!global_options_set.x_arm_fpu_index)
1889     {
1890       const char *target_fpu_name;
1891       bool ok;
1892
1893 #ifdef FPUTYPE_DEFAULT
1894       target_fpu_name = FPUTYPE_DEFAULT;
1895 #else
1896       target_fpu_name = "vfp";
1897 #endif
1898
1899       ok = opt_enum_arg_to_value (OPT_mfpu_, target_fpu_name, &arm_fpu_index,
1900                                   CL_TARGET);
1901       gcc_assert (ok);
1902     }
1903
1904   arm_fpu_desc = &all_fpus[arm_fpu_index];
1905
1906   switch (arm_fpu_desc->model)
1907     {
1908     case ARM_FP_MODEL_VFP:
1909       arm_fpu_attr = FPU_VFP;
1910       break;
1911
1912     default:
1913       gcc_unreachable();
1914     }
1915
1916   if (TARGET_AAPCS_BASED)
1917     {
1918       if (TARGET_CALLER_INTERWORKING)
1919         error ("AAPCS does not support -mcaller-super-interworking");
1920       else
1921         if (TARGET_CALLEE_INTERWORKING)
1922           error ("AAPCS does not support -mcallee-super-interworking");
1923     }
1924
1925   /* iWMMXt and NEON are incompatible.  */
1926   if (TARGET_IWMMXT && TARGET_NEON)
1927     error ("iWMMXt and NEON are incompatible");
1928
1929   /* iWMMXt unsupported under Thumb mode.  */
1930   if (TARGET_THUMB && TARGET_IWMMXT)
1931     error ("iWMMXt unsupported under Thumb mode");
1932
1933   /* __fp16 support currently assumes the core has ldrh.  */
1934   if (!arm_arch4 && arm_fp16_format != ARM_FP16_FORMAT_NONE)
1935     sorry ("__fp16 and no ldrh");
1936
1937   /* If soft-float is specified then don't use FPU.  */
1938   if (TARGET_SOFT_FLOAT)
1939     arm_fpu_attr = FPU_NONE;
1940
1941   if (TARGET_AAPCS_BASED)
1942     {
1943       if (arm_abi == ARM_ABI_IWMMXT)
1944         arm_pcs_default = ARM_PCS_AAPCS_IWMMXT;
1945       else if (arm_float_abi == ARM_FLOAT_ABI_HARD
1946                && TARGET_HARD_FLOAT
1947                && TARGET_VFP)
1948         arm_pcs_default = ARM_PCS_AAPCS_VFP;
1949       else
1950         arm_pcs_default = ARM_PCS_AAPCS;
1951     }
1952   else
1953     {
1954       if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1955         sorry ("-mfloat-abi=hard and VFP");
1956
1957       if (arm_abi == ARM_ABI_APCS)
1958         arm_pcs_default = ARM_PCS_APCS;
1959       else
1960         arm_pcs_default = ARM_PCS_ATPCS;
1961     }
1962
1963   /* For arm2/3 there is no need to do any scheduling if we are doing
1964      software floating-point.  */
1965   if (TARGET_SOFT_FLOAT && (tune_flags & FL_MODE32) == 0)
1966     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1967
1968   /* Use the cp15 method if it is available.  */
1969   if (target_thread_pointer == TP_AUTO)
1970     {
1971       if (arm_arch6k && !TARGET_THUMB1)
1972         target_thread_pointer = TP_CP15;
1973       else
1974         target_thread_pointer = TP_SOFT;
1975     }
1976
1977   if (TARGET_HARD_TP && TARGET_THUMB1)
1978     error ("can not use -mtp=cp15 with 16-bit Thumb");
1979
1980   /* Override the default structure alignment for AAPCS ABI.  */
1981   if (!global_options_set.x_arm_structure_size_boundary)
1982     {
1983       if (TARGET_AAPCS_BASED)
1984         arm_structure_size_boundary = 8;
1985     }
1986   else
1987     {
1988       if (arm_structure_size_boundary != 8
1989           && arm_structure_size_boundary != 32
1990           && !(ARM_DOUBLEWORD_ALIGN && arm_structure_size_boundary == 64))
1991         {
1992           if (ARM_DOUBLEWORD_ALIGN)
1993             warning (0,
1994                      "structure size boundary can only be set to 8, 32 or 64");
1995           else
1996             warning (0, "structure size boundary can only be set to 8 or 32");
1997           arm_structure_size_boundary
1998             = (TARGET_AAPCS_BASED ? 8 : DEFAULT_STRUCTURE_SIZE_BOUNDARY);
1999         }
2000     }
2001
2002   if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
2003     {
2004       error ("RTP PIC is incompatible with Thumb");
2005       flag_pic = 0;
2006     }
2007
2008   /* If stack checking is disabled, we can use r10 as the PIC register,
2009      which keeps r9 available.  The EABI specifies r9 as the PIC register.  */
2010   if (flag_pic && TARGET_SINGLE_PIC_BASE)
2011     {
2012       if (TARGET_VXWORKS_RTP)
2013         warning (0, "RTP PIC is incompatible with -msingle-pic-base");
2014       arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
2015     }
2016
2017   if (flag_pic && TARGET_VXWORKS_RTP)
2018     arm_pic_register = 9;
2019
2020   if (arm_pic_register_string != NULL)
2021     {
2022       int pic_register = decode_reg_name (arm_pic_register_string);
2023
2024       if (!flag_pic)
2025         warning (0, "-mpic-register= is useless without -fpic");
2026
2027       /* Prevent the user from choosing an obviously stupid PIC register.  */
2028       else if (pic_register < 0 || call_used_regs[pic_register]
2029                || pic_register == HARD_FRAME_POINTER_REGNUM
2030                || pic_register == STACK_POINTER_REGNUM
2031                || pic_register >= PC_REGNUM
2032                || (TARGET_VXWORKS_RTP
2033                    && (unsigned int) pic_register != arm_pic_register))
2034         error ("unable to use '%s' for PIC register", arm_pic_register_string);
2035       else
2036         arm_pic_register = pic_register;
2037     }
2038
2039   /* Enable -mfix-cortex-m3-ldrd by default for Cortex-M3 cores.  */
2040   if (fix_cm3_ldrd == 2)
2041     {
2042       if (arm_selected_cpu->core == cortexm3)
2043         fix_cm3_ldrd = 1;
2044       else
2045         fix_cm3_ldrd = 0;
2046     }
2047
2048   /* Enable -munaligned-access by default for
2049      - all ARMv6 architecture-based processors
2050      - ARMv7-A, ARMv7-R, and ARMv7-M architecture-based processors.
2051      - ARMv8 architecture-base processors.
2052
2053      Disable -munaligned-access by default for
2054      - all pre-ARMv6 architecture-based processors
2055      - ARMv6-M architecture-based processors.  */
2056
2057   if (unaligned_access == 2)
2058     {
2059       if (arm_arch6 && (arm_arch_notm || arm_arch7))
2060         unaligned_access = 1;
2061       else
2062         unaligned_access = 0;
2063     }
2064   else if (unaligned_access == 1
2065            && !(arm_arch6 && (arm_arch_notm || arm_arch7)))
2066     {
2067       warning (0, "target CPU does not support unaligned accesses");
2068       unaligned_access = 0;
2069     }
2070
2071   if (TARGET_THUMB1 && flag_schedule_insns)
2072     {
2073       /* Don't warn since it's on by default in -O2.  */
2074       flag_schedule_insns = 0;
2075     }
2076
2077   if (optimize_size)
2078     {
2079       /* If optimizing for size, bump the number of instructions that we
2080          are prepared to conditionally execute (even on a StrongARM).  */
2081       max_insns_skipped = 6;
2082     }
2083   else
2084     max_insns_skipped = current_tune->max_insns_skipped;
2085
2086   /* Hot/Cold partitioning is not currently supported, since we can't
2087      handle literal pool placement in that case.  */
2088   if (flag_reorder_blocks_and_partition)
2089     {
2090       inform (input_location,
2091               "-freorder-blocks-and-partition not supported on this architecture");
2092       flag_reorder_blocks_and_partition = 0;
2093       flag_reorder_blocks = 1;
2094     }
2095
2096   if (flag_pic)
2097     /* Hoisting PIC address calculations more aggressively provides a small,
2098        but measurable, size reduction for PIC code.  Therefore, we decrease
2099        the bar for unrestricted expression hoisting to the cost of PIC address
2100        calculation, which is 2 instructions.  */
2101     maybe_set_param_value (PARAM_GCSE_UNRESTRICTED_COST, 2,
2102                            global_options.x_param_values,
2103                            global_options_set.x_param_values);
2104
2105   /* ARM EABI defaults to strict volatile bitfields.  */
2106   if (TARGET_AAPCS_BASED && flag_strict_volatile_bitfields < 0
2107       && abi_version_at_least(2))
2108     flag_strict_volatile_bitfields = 1;
2109
2110   /* Enable sw prefetching at -O3 for CPUS that have prefetch, and we have deemed
2111      it beneficial (signified by setting num_prefetch_slots to 1 or more.)  */
2112   if (flag_prefetch_loop_arrays < 0
2113       && HAVE_prefetch
2114       && optimize >= 3
2115       && current_tune->num_prefetch_slots > 0)
2116     flag_prefetch_loop_arrays = 1;
2117
2118   /* Set up parameters to be used in prefetching algorithm.  Do not override the
2119      defaults unless we are tuning for a core we have researched values for.  */
2120   if (current_tune->num_prefetch_slots > 0)
2121     maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
2122                            current_tune->num_prefetch_slots,
2123                            global_options.x_param_values,
2124                            global_options_set.x_param_values);
2125   if (current_tune->l1_cache_line_size >= 0)
2126     maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
2127                            current_tune->l1_cache_line_size,
2128                            global_options.x_param_values,
2129                            global_options_set.x_param_values);
2130   if (current_tune->l1_cache_size >= 0)
2131     maybe_set_param_value (PARAM_L1_CACHE_SIZE,
2132                            current_tune->l1_cache_size,
2133                            global_options.x_param_values,
2134                            global_options_set.x_param_values);
2135
2136   /* Use the alternative scheduling-pressure algorithm by default.  */
2137   maybe_set_param_value (PARAM_SCHED_PRESSURE_ALGORITHM, 2,
2138                          global_options.x_param_values,
2139                          global_options_set.x_param_values);
2140
2141   /* Register global variables with the garbage collector.  */
2142   arm_add_gc_roots ();
2143 }
2144
2145 static void
2146 arm_add_gc_roots (void)
2147 {
2148   gcc_obstack_init(&minipool_obstack);
2149   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
2150 }
2151 \f
2152 /* A table of known ARM exception types.
2153    For use with the interrupt function attribute.  */
2154
2155 typedef struct
2156 {
2157   const char *const arg;
2158   const unsigned long return_value;
2159 }
2160 isr_attribute_arg;
2161
2162 static const isr_attribute_arg isr_attribute_args [] =
2163 {
2164   { "IRQ",   ARM_FT_ISR },
2165   { "irq",   ARM_FT_ISR },
2166   { "FIQ",   ARM_FT_FIQ },
2167   { "fiq",   ARM_FT_FIQ },
2168   { "ABORT", ARM_FT_ISR },
2169   { "abort", ARM_FT_ISR },
2170   { "ABORT", ARM_FT_ISR },
2171   { "abort", ARM_FT_ISR },
2172   { "UNDEF", ARM_FT_EXCEPTION },
2173   { "undef", ARM_FT_EXCEPTION },
2174   { "SWI",   ARM_FT_EXCEPTION },
2175   { "swi",   ARM_FT_EXCEPTION },
2176   { NULL,    ARM_FT_NORMAL }
2177 };
2178
2179 /* Returns the (interrupt) function type of the current
2180    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
2181
2182 static unsigned long
2183 arm_isr_value (tree argument)
2184 {
2185   const isr_attribute_arg * ptr;
2186   const char *              arg;
2187
2188   if (!arm_arch_notm)
2189     return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
2190
2191   /* No argument - default to IRQ.  */
2192   if (argument == NULL_TREE)
2193     return ARM_FT_ISR;
2194
2195   /* Get the value of the argument.  */
2196   if (TREE_VALUE (argument) == NULL_TREE
2197       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
2198     return ARM_FT_UNKNOWN;
2199
2200   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
2201
2202   /* Check it against the list of known arguments.  */
2203   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
2204     if (streq (arg, ptr->arg))
2205       return ptr->return_value;
2206
2207   /* An unrecognized interrupt type.  */
2208   return ARM_FT_UNKNOWN;
2209 }
2210
2211 /* Computes the type of the current function.  */
2212
2213 static unsigned long
2214 arm_compute_func_type (void)
2215 {
2216   unsigned long type = ARM_FT_UNKNOWN;
2217   tree a;
2218   tree attr;
2219
2220   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
2221
2222   /* Decide if the current function is volatile.  Such functions
2223      never return, and many memory cycles can be saved by not storing
2224      register values that will never be needed again.  This optimization
2225      was added to speed up context switching in a kernel application.  */
2226   if (optimize > 0
2227       && (TREE_NOTHROW (current_function_decl)
2228           || !(flag_unwind_tables
2229                || (flag_exceptions
2230                    && arm_except_unwind_info (&global_options) != UI_SJLJ)))
2231       && TREE_THIS_VOLATILE (current_function_decl))
2232     type |= ARM_FT_VOLATILE;
2233
2234   if (cfun->static_chain_decl != NULL)
2235     type |= ARM_FT_NESTED;
2236
2237   attr = DECL_ATTRIBUTES (current_function_decl);
2238
2239   a = lookup_attribute ("naked", attr);
2240   if (a != NULL_TREE)
2241     type |= ARM_FT_NAKED;
2242
2243   a = lookup_attribute ("isr", attr);
2244   if (a == NULL_TREE)
2245     a = lookup_attribute ("interrupt", attr);
2246
2247   if (a == NULL_TREE)
2248     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
2249   else
2250     type |= arm_isr_value (TREE_VALUE (a));
2251
2252   return type;
2253 }
2254
2255 /* Returns the type of the current function.  */
2256
2257 unsigned long
2258 arm_current_func_type (void)
2259 {
2260   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
2261     cfun->machine->func_type = arm_compute_func_type ();
2262
2263   return cfun->machine->func_type;
2264 }
2265
2266 bool
2267 arm_allocate_stack_slots_for_args (void)
2268 {
2269   /* Naked functions should not allocate stack slots for arguments.  */
2270   return !IS_NAKED (arm_current_func_type ());
2271 }
2272
2273 static bool
2274 arm_warn_func_return (tree decl)
2275 {
2276   /* Naked functions are implemented entirely in assembly, including the
2277      return sequence, so suppress warnings about this.  */
2278   return lookup_attribute ("naked", DECL_ATTRIBUTES (decl)) == NULL_TREE;
2279 }
2280
2281 \f
2282 /* Output assembler code for a block containing the constant parts
2283    of a trampoline, leaving space for the variable parts.
2284
2285    On the ARM, (if r8 is the static chain regnum, and remembering that
2286    referencing pc adds an offset of 8) the trampoline looks like:
2287            ldr          r8, [pc, #0]
2288            ldr          pc, [pc]
2289            .word        static chain value
2290            .word        function's address
2291    XXX FIXME: When the trampoline returns, r8 will be clobbered.  */
2292
2293 static void
2294 arm_asm_trampoline_template (FILE *f)
2295 {
2296   if (TARGET_ARM)
2297     {
2298       asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", STATIC_CHAIN_REGNUM, PC_REGNUM);
2299       asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", PC_REGNUM, PC_REGNUM);
2300     }
2301   else if (TARGET_THUMB2)
2302     {
2303       /* The Thumb-2 trampoline is similar to the arm implementation.
2304          Unlike 16-bit Thumb, we enter the stub in thumb mode.  */
2305       asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n",
2306                    STATIC_CHAIN_REGNUM, PC_REGNUM);
2307       asm_fprintf (f, "\tldr.w\t%r, [%r, #4]\n", PC_REGNUM, PC_REGNUM);
2308     }
2309   else
2310     {
2311       ASM_OUTPUT_ALIGN (f, 2);
2312       fprintf (f, "\t.code\t16\n");
2313       fprintf (f, ".Ltrampoline_start:\n");
2314       asm_fprintf (f, "\tpush\t{r0, r1}\n");
2315       asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2316       asm_fprintf (f, "\tmov\t%r, r0\n", STATIC_CHAIN_REGNUM);
2317       asm_fprintf (f, "\tldr\tr0, [%r, #8]\n", PC_REGNUM);
2318       asm_fprintf (f, "\tstr\tr0, [%r, #4]\n", SP_REGNUM);
2319       asm_fprintf (f, "\tpop\t{r0, %r}\n", PC_REGNUM);
2320     }
2321   assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2322   assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
2323 }
2324
2325 /* Emit RTL insns to initialize the variable parts of a trampoline.  */
2326
2327 static void
2328 arm_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2329 {
2330   rtx fnaddr, mem, a_tramp;
2331
2332   emit_block_move (m_tramp, assemble_trampoline_template (),
2333                    GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
2334
2335   mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 8 : 12);
2336   emit_move_insn (mem, chain_value);
2337
2338   mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 12 : 16);
2339   fnaddr = XEXP (DECL_RTL (fndecl), 0);
2340   emit_move_insn (mem, fnaddr);
2341
2342   a_tramp = XEXP (m_tramp, 0);
2343   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__clear_cache"),
2344                      LCT_NORMAL, VOIDmode, 2, a_tramp, Pmode,
2345                      plus_constant (Pmode, a_tramp, TRAMPOLINE_SIZE), Pmode);
2346 }
2347
2348 /* Thumb trampolines should be entered in thumb mode, so set
2349    the bottom bit of the address.  */
2350
2351 static rtx
2352 arm_trampoline_adjust_address (rtx addr)
2353 {
2354   if (TARGET_THUMB)
2355     addr = expand_simple_binop (Pmode, IOR, addr, const1_rtx,
2356                                 NULL, 0, OPTAB_LIB_WIDEN);
2357   return addr;
2358 }
2359 \f
2360 /* Return 1 if it is possible to return using a single instruction.
2361    If SIBLING is non-null, this is a test for a return before a sibling
2362    call.  SIBLING is the call insn, so we can examine its register usage.  */
2363
2364 int
2365 use_return_insn (int iscond, rtx sibling)
2366 {
2367   int regno;
2368   unsigned int func_type;
2369   unsigned long saved_int_regs;
2370   unsigned HOST_WIDE_INT stack_adjust;
2371   arm_stack_offsets *offsets;
2372
2373   /* Never use a return instruction before reload has run.  */
2374   if (!reload_completed)
2375     return 0;
2376
2377   func_type = arm_current_func_type ();
2378
2379   /* Naked, volatile and stack alignment functions need special
2380      consideration.  */
2381   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
2382     return 0;
2383
2384   /* So do interrupt functions that use the frame pointer and Thumb
2385      interrupt functions.  */
2386   if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
2387     return 0;
2388
2389   offsets = arm_get_frame_offsets ();
2390   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
2391
2392   /* As do variadic functions.  */
2393   if (crtl->args.pretend_args_size
2394       || cfun->machine->uses_anonymous_args
2395       /* Or if the function calls __builtin_eh_return () */
2396       || crtl->calls_eh_return
2397       /* Or if the function calls alloca */
2398       || cfun->calls_alloca
2399       /* Or if there is a stack adjustment.  However, if the stack pointer
2400          is saved on the stack, we can use a pre-incrementing stack load.  */
2401       || !(stack_adjust == 0 || (TARGET_APCS_FRAME && frame_pointer_needed
2402                                  && stack_adjust == 4)))
2403     return 0;
2404
2405   saved_int_regs = offsets->saved_regs_mask;
2406
2407   /* Unfortunately, the insn
2408
2409        ldmib sp, {..., sp, ...}
2410
2411      triggers a bug on most SA-110 based devices, such that the stack
2412      pointer won't be correctly restored if the instruction takes a
2413      page fault.  We work around this problem by popping r3 along with
2414      the other registers, since that is never slower than executing
2415      another instruction.
2416
2417      We test for !arm_arch5 here, because code for any architecture
2418      less than this could potentially be run on one of the buggy
2419      chips.  */
2420   if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
2421     {
2422       /* Validate that r3 is a call-clobbered register (always true in
2423          the default abi) ...  */
2424       if (!call_used_regs[3])
2425         return 0;
2426
2427       /* ... that it isn't being used for a return value ... */
2428       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
2429         return 0;
2430
2431       /* ... or for a tail-call argument ...  */
2432       if (sibling)
2433         {
2434           gcc_assert (CALL_P (sibling));
2435
2436           if (find_regno_fusage (sibling, USE, 3))
2437             return 0;
2438         }
2439
2440       /* ... and that there are no call-saved registers in r0-r2
2441          (always true in the default ABI).  */
2442       if (saved_int_regs & 0x7)
2443         return 0;
2444     }
2445
2446   /* Can't be done if interworking with Thumb, and any registers have been
2447      stacked.  */
2448   if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
2449     return 0;
2450
2451   /* On StrongARM, conditional returns are expensive if they aren't
2452      taken and multiple registers have been stacked.  */
2453   if (iscond && arm_tune_strongarm)
2454     {
2455       /* Conditional return when just the LR is stored is a simple
2456          conditional-load instruction, that's not expensive.  */
2457       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
2458         return 0;
2459
2460       if (flag_pic
2461           && arm_pic_register != INVALID_REGNUM
2462           && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
2463         return 0;
2464     }
2465
2466   /* If there are saved registers but the LR isn't saved, then we need
2467      two instructions for the return.  */
2468   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
2469     return 0;
2470
2471   /* Can't be done if any of the VFP regs are pushed,
2472      since this also requires an insn.  */
2473   if (TARGET_HARD_FLOAT && TARGET_VFP)
2474     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
2475       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2476         return 0;
2477
2478   if (TARGET_REALLY_IWMMXT)
2479     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
2480       if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2481         return 0;
2482
2483   return 1;
2484 }
2485
2486 /* Return TRUE if int I is a valid immediate ARM constant.  */
2487
2488 int
2489 const_ok_for_arm (HOST_WIDE_INT i)
2490 {
2491   int lowbit;
2492
2493   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
2494      be all zero, or all one.  */
2495   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
2496       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
2497           != ((~(unsigned HOST_WIDE_INT) 0)
2498               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
2499     return FALSE;
2500
2501   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
2502
2503   /* Fast return for 0 and small values.  We must do this for zero, since
2504      the code below can't handle that one case.  */
2505   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
2506     return TRUE;
2507
2508   /* Get the number of trailing zeros.  */
2509   lowbit = ffs((int) i) - 1;
2510
2511   /* Only even shifts are allowed in ARM mode so round down to the
2512      nearest even number.  */
2513   if (TARGET_ARM)
2514     lowbit &= ~1;
2515
2516   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
2517     return TRUE;
2518
2519   if (TARGET_ARM)
2520     {
2521       /* Allow rotated constants in ARM mode.  */
2522       if (lowbit <= 4
2523            && ((i & ~0xc000003f) == 0
2524                || (i & ~0xf000000f) == 0
2525                || (i & ~0xfc000003) == 0))
2526         return TRUE;
2527     }
2528   else
2529     {
2530       HOST_WIDE_INT v;
2531
2532       /* Allow repeated patterns 0x00XY00XY or 0xXYXYXYXY.  */
2533       v = i & 0xff;
2534       v |= v << 16;
2535       if (i == v || i == (v | (v << 8)))
2536         return TRUE;
2537
2538       /* Allow repeated pattern 0xXY00XY00.  */
2539       v = i & 0xff00;
2540       v |= v << 16;
2541       if (i == v)
2542         return TRUE;
2543     }
2544
2545   return FALSE;
2546 }
2547
2548 /* Return true if I is a valid constant for the operation CODE.  */
2549 int
2550 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
2551 {
2552   if (const_ok_for_arm (i))
2553     return 1;
2554
2555   switch (code)
2556     {
2557     case SET:
2558       /* See if we can use movw.  */
2559       if (arm_arch_thumb2 && (i & 0xffff0000) == 0)
2560         return 1;
2561       else
2562         /* Otherwise, try mvn.  */
2563         return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2564
2565     case PLUS:
2566       /* See if we can use addw or subw.  */
2567       if (TARGET_THUMB2
2568           && ((i & 0xfffff000) == 0
2569               || ((-i) & 0xfffff000) == 0))
2570         return 1;
2571       /* else fall through.  */
2572
2573     case COMPARE:
2574     case EQ:
2575     case NE:
2576     case GT:
2577     case LE:
2578     case LT:
2579     case GE:
2580     case GEU:
2581     case LTU:
2582     case GTU:
2583     case LEU:
2584     case UNORDERED:
2585     case ORDERED:
2586     case UNEQ:
2587     case UNGE:
2588     case UNLT:
2589     case UNGT:
2590     case UNLE:
2591       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
2592
2593     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
2594     case XOR:
2595       return 0;
2596
2597     case IOR:
2598       if (TARGET_THUMB2)
2599         return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2600       return 0;
2601
2602     case AND:
2603       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2604
2605     default:
2606       gcc_unreachable ();
2607     }
2608 }
2609
2610 /* Return true if I is a valid di mode constant for the operation CODE.  */
2611 int
2612 const_ok_for_dimode_op (HOST_WIDE_INT i, enum rtx_code code)
2613 {
2614   HOST_WIDE_INT hi_val = (i >> 32) & 0xFFFFFFFF;
2615   HOST_WIDE_INT lo_val = i & 0xFFFFFFFF;
2616   rtx hi = GEN_INT (hi_val);
2617   rtx lo = GEN_INT (lo_val);
2618
2619   if (TARGET_THUMB1)
2620     return 0;
2621
2622   switch (code)
2623     {
2624     case PLUS:
2625       return arm_not_operand (hi, SImode) && arm_add_operand (lo, SImode);
2626
2627     default:
2628       return 0;
2629     }
2630 }
2631
2632 /* Emit a sequence of insns to handle a large constant.
2633    CODE is the code of the operation required, it can be any of SET, PLUS,
2634    IOR, AND, XOR, MINUS;
2635    MODE is the mode in which the operation is being performed;
2636    VAL is the integer to operate on;
2637    SOURCE is the other operand (a register, or a null-pointer for SET);
2638    SUBTARGETS means it is safe to create scratch registers if that will
2639    either produce a simpler sequence, or we will want to cse the values.
2640    Return value is the number of insns emitted.  */
2641
2642 /* ??? Tweak this for thumb2.  */
2643 int
2644 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
2645                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
2646 {
2647   rtx cond;
2648
2649   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
2650     cond = COND_EXEC_TEST (PATTERN (insn));
2651   else
2652     cond = NULL_RTX;
2653
2654   if (subtargets || code == SET
2655       || (REG_P (target) && REG_P (source)
2656           && REGNO (target) != REGNO (source)))
2657     {
2658       /* After arm_reorg has been called, we can't fix up expensive
2659          constants by pushing them into memory so we must synthesize
2660          them in-line, regardless of the cost.  This is only likely to
2661          be more costly on chips that have load delay slots and we are
2662          compiling without running the scheduler (so no splitting
2663          occurred before the final instruction emission).
2664
2665          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
2666       */
2667       if (!after_arm_reorg
2668           && !cond
2669           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
2670                                 1, 0)
2671               > (arm_constant_limit (optimize_function_for_size_p (cfun))
2672                  + (code != SET))))
2673         {
2674           if (code == SET)
2675             {
2676               /* Currently SET is the only monadic value for CODE, all
2677                  the rest are diadic.  */
2678               if (TARGET_USE_MOVT)
2679                 arm_emit_movpair (target, GEN_INT (val));
2680               else
2681                 emit_set_insn (target, GEN_INT (val));
2682
2683               return 1;
2684             }
2685           else
2686             {
2687               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
2688
2689               if (TARGET_USE_MOVT)
2690                 arm_emit_movpair (temp, GEN_INT (val));
2691               else
2692                 emit_set_insn (temp, GEN_INT (val));
2693
2694               /* For MINUS, the value is subtracted from, since we never
2695                  have subtraction of a constant.  */
2696               if (code == MINUS)
2697                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
2698               else
2699                 emit_set_insn (target,
2700                                gen_rtx_fmt_ee (code, mode, source, temp));
2701               return 2;
2702             }
2703         }
2704     }
2705
2706   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
2707                            1);
2708 }
2709
2710 /* Return a sequence of integers, in RETURN_SEQUENCE that fit into
2711    ARM/THUMB2 immediates, and add up to VAL.
2712    Thr function return value gives the number of insns required.  */
2713 static int
2714 optimal_immediate_sequence (enum rtx_code code, unsigned HOST_WIDE_INT val,
2715                             struct four_ints *return_sequence)
2716 {
2717   int best_consecutive_zeros = 0;
2718   int i;
2719   int best_start = 0;
2720   int insns1, insns2;
2721   struct four_ints tmp_sequence;
2722
2723   /* If we aren't targeting ARM, the best place to start is always at
2724      the bottom, otherwise look more closely.  */
2725   if (TARGET_ARM)
2726     {
2727       for (i = 0; i < 32; i += 2)
2728         {
2729           int consecutive_zeros = 0;
2730
2731           if (!(val & (3 << i)))
2732             {
2733               while ((i < 32) && !(val & (3 << i)))
2734                 {
2735                   consecutive_zeros += 2;
2736                   i += 2;
2737                 }
2738               if (consecutive_zeros > best_consecutive_zeros)
2739                 {
2740                   best_consecutive_zeros = consecutive_zeros;
2741                   best_start = i - consecutive_zeros;
2742                 }
2743               i -= 2;
2744             }
2745         }
2746     }
2747
2748   /* So long as it won't require any more insns to do so, it's
2749      desirable to emit a small constant (in bits 0...9) in the last
2750      insn.  This way there is more chance that it can be combined with
2751      a later addressing insn to form a pre-indexed load or store
2752      operation.  Consider:
2753
2754            *((volatile int *)0xe0000100) = 1;
2755            *((volatile int *)0xe0000110) = 2;
2756
2757      We want this to wind up as:
2758
2759             mov rA, #0xe0000000
2760             mov rB, #1
2761             str rB, [rA, #0x100]
2762             mov rB, #2
2763             str rB, [rA, #0x110]
2764
2765      rather than having to synthesize both large constants from scratch.
2766
2767      Therefore, we calculate how many insns would be required to emit
2768      the constant starting from `best_start', and also starting from
2769      zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2770      yield a shorter sequence, we may as well use zero.  */
2771   insns1 = optimal_immediate_sequence_1 (code, val, return_sequence, best_start);
2772   if (best_start != 0
2773       && ((((unsigned HOST_WIDE_INT) 1) << best_start) < val))
2774     {
2775       insns2 = optimal_immediate_sequence_1 (code, val, &tmp_sequence, 0);
2776       if (insns2 <= insns1)
2777         {
2778           *return_sequence = tmp_sequence;
2779           insns1 = insns2;
2780         }
2781     }
2782
2783   return insns1;
2784 }
2785
2786 /* As for optimal_immediate_sequence, but starting at bit-position I.  */
2787 static int
2788 optimal_immediate_sequence_1 (enum rtx_code code, unsigned HOST_WIDE_INT val,
2789                              struct four_ints *return_sequence, int i)
2790 {
2791   int remainder = val & 0xffffffff;
2792   int insns = 0;
2793
2794   /* Try and find a way of doing the job in either two or three
2795      instructions.
2796
2797      In ARM mode we can use 8-bit constants, rotated to any 2-bit aligned
2798      location.  We start at position I.  This may be the MSB, or
2799      optimial_immediate_sequence may have positioned it at the largest block
2800      of zeros that are aligned on a 2-bit boundary. We then fill up the temps,
2801      wrapping around to the top of the word when we drop off the bottom.
2802      In the worst case this code should produce no more than four insns.
2803
2804      In Thumb2 mode, we can use 32/16-bit replicated constants, and 8-bit
2805      constants, shifted to any arbitrary location.  We should always start
2806      at the MSB.  */
2807   do
2808     {
2809       int end;
2810       unsigned int b1, b2, b3, b4;
2811       unsigned HOST_WIDE_INT result;
2812       int loc;
2813
2814       gcc_assert (insns < 4);
2815
2816       if (i <= 0)
2817         i += 32;
2818
2819       /* First, find the next normal 12/8-bit shifted/rotated immediate.  */
2820       if (remainder & ((TARGET_ARM ? (3 << (i - 2)) : (1 << (i - 1)))))
2821         {
2822           loc = i;
2823           if (i <= 12 && TARGET_THUMB2 && code == PLUS)
2824             /* We can use addw/subw for the last 12 bits.  */
2825             result = remainder;
2826           else
2827             {
2828               /* Use an 8-bit shifted/rotated immediate.  */
2829               end = i - 8;
2830               if (end < 0)
2831                 end += 32;
2832               result = remainder & ((0x0ff << end)
2833                                    | ((i < end) ? (0xff >> (32 - end))
2834                                                 : 0));
2835               i -= 8;
2836             }
2837         }
2838       else
2839         {
2840           /* Arm allows rotates by a multiple of two. Thumb-2 allows
2841              arbitrary shifts.  */
2842           i -= TARGET_ARM ? 2 : 1;
2843           continue;
2844         }
2845
2846       /* Next, see if we can do a better job with a thumb2 replicated
2847          constant.
2848
2849          We do it this way around to catch the cases like 0x01F001E0 where
2850          two 8-bit immediates would work, but a replicated constant would
2851          make it worse.
2852
2853          TODO: 16-bit constants that don't clear all the bits, but still win.
2854          TODO: Arithmetic splitting for set/add/sub, rather than bitwise.  */
2855       if (TARGET_THUMB2)
2856         {
2857           b1 = (remainder & 0xff000000) >> 24;
2858           b2 = (remainder & 0x00ff0000) >> 16;
2859           b3 = (remainder & 0x0000ff00) >> 8;
2860           b4 = remainder & 0xff;
2861
2862           if (loc > 24)
2863             {
2864               /* The 8-bit immediate already found clears b1 (and maybe b2),
2865                  but must leave b3 and b4 alone.  */
2866
2867               /* First try to find a 32-bit replicated constant that clears
2868                  almost everything.  We can assume that we can't do it in one,
2869                  or else we wouldn't be here.  */
2870               unsigned int tmp = b1 & b2 & b3 & b4;
2871               unsigned int tmp2 = tmp + (tmp << 8) + (tmp << 16)
2872                                   + (tmp << 24);
2873               unsigned int matching_bytes = (tmp == b1) + (tmp == b2)
2874                                             + (tmp == b3) + (tmp == b4);
2875               if (tmp
2876                   && (matching_bytes >= 3
2877                       || (matching_bytes == 2
2878                           && const_ok_for_op (remainder & ~tmp2, code))))
2879                 {
2880                   /* At least 3 of the bytes match, and the fourth has at
2881                      least as many bits set, or two of the bytes match
2882                      and it will only require one more insn to finish.  */
2883                   result = tmp2;
2884                   i = tmp != b1 ? 32
2885                       : tmp != b2 ? 24
2886                       : tmp != b3 ? 16
2887                       : 8;
2888                 }
2889
2890               /* Second, try to find a 16-bit replicated constant that can
2891                  leave three of the bytes clear.  If b2 or b4 is already
2892                  zero, then we can.  If the 8-bit from above would not
2893                  clear b2 anyway, then we still win.  */
2894               else if (b1 == b3 && (!b2 || !b4
2895                                || (remainder & 0x00ff0000 & ~result)))
2896                 {
2897                   result = remainder & 0xff00ff00;
2898                   i = 24;
2899                 }
2900             }
2901           else if (loc > 16)
2902             {
2903               /* The 8-bit immediate already found clears b2 (and maybe b3)
2904                  and we don't get here unless b1 is alredy clear, but it will
2905                  leave b4 unchanged.  */
2906
2907               /* If we can clear b2 and b4 at once, then we win, since the
2908                  8-bits couldn't possibly reach that far.  */
2909               if (b2 == b4)
2910                 {
2911                   result = remainder & 0x00ff00ff;
2912                   i = 16;
2913                 }
2914             }
2915         }
2916
2917       return_sequence->i[insns++] = result;
2918       remainder &= ~result;
2919
2920       if (code == SET || code == MINUS)
2921         code = PLUS;
2922     }
2923   while (remainder);
2924
2925   return insns;
2926 }
2927
2928 /* Emit an instruction with the indicated PATTERN.  If COND is
2929    non-NULL, conditionalize the execution of the instruction on COND
2930    being true.  */
2931
2932 static void
2933 emit_constant_insn (rtx cond, rtx pattern)
2934 {
2935   if (cond)
2936     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
2937   emit_insn (pattern);
2938 }
2939
2940 /* As above, but extra parameter GENERATE which, if clear, suppresses
2941    RTL generation.  */
2942
2943 static int
2944 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
2945                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
2946                   int generate)
2947 {
2948   int can_invert = 0;
2949   int can_negate = 0;
2950   int final_invert = 0;
2951   int i;
2952   int set_sign_bit_copies = 0;
2953   int clear_sign_bit_copies = 0;
2954   int clear_zero_bit_copies = 0;
2955   int set_zero_bit_copies = 0;
2956   int insns = 0, neg_insns, inv_insns;
2957   unsigned HOST_WIDE_INT temp1, temp2;
2958   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
2959   struct four_ints *immediates;
2960   struct four_ints pos_immediates, neg_immediates, inv_immediates;
2961
2962   /* Find out which operations are safe for a given CODE.  Also do a quick
2963      check for degenerate cases; these can occur when DImode operations
2964      are split.  */
2965   switch (code)
2966     {
2967     case SET:
2968       can_invert = 1;
2969       break;
2970
2971     case PLUS:
2972       can_negate = 1;
2973       break;
2974
2975     case IOR:
2976       if (remainder == 0xffffffff)
2977         {
2978           if (generate)
2979             emit_constant_insn (cond,
2980                                 gen_rtx_SET (VOIDmode, target,
2981                                              GEN_INT (ARM_SIGN_EXTEND (val))));
2982           return 1;
2983         }
2984
2985       if (remainder == 0)
2986         {
2987           if (reload_completed && rtx_equal_p (target, source))
2988             return 0;
2989
2990           if (generate)
2991             emit_constant_insn (cond,
2992                                 gen_rtx_SET (VOIDmode, target, source));
2993           return 1;
2994         }
2995       break;
2996
2997     case AND:
2998       if (remainder == 0)
2999         {
3000           if (generate)
3001             emit_constant_insn (cond,
3002                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
3003           return 1;
3004         }
3005       if (remainder == 0xffffffff)
3006         {
3007           if (reload_completed && rtx_equal_p (target, source))
3008             return 0;
3009           if (generate)
3010             emit_constant_insn (cond,
3011                                 gen_rtx_SET (VOIDmode, target, source));
3012           return 1;
3013         }
3014       can_invert = 1;
3015       break;
3016
3017     case XOR:
3018       if (remainder == 0)
3019         {
3020           if (reload_completed && rtx_equal_p (target, source))
3021             return 0;
3022           if (generate)
3023             emit_constant_insn (cond,
3024                                 gen_rtx_SET (VOIDmode, target, source));
3025           return 1;
3026         }
3027
3028       if (remainder == 0xffffffff)
3029         {
3030           if (generate)
3031             emit_constant_insn (cond,
3032                                 gen_rtx_SET (VOIDmode, target,
3033                                              gen_rtx_NOT (mode, source)));
3034           return 1;
3035         }
3036       final_invert = 1;
3037       break;
3038
3039     case MINUS:
3040       /* We treat MINUS as (val - source), since (source - val) is always
3041          passed as (source + (-val)).  */
3042       if (remainder == 0)
3043         {
3044           if (generate)
3045             emit_constant_insn (cond,
3046                                 gen_rtx_SET (VOIDmode, target,
3047                                              gen_rtx_NEG (mode, source)));
3048           return 1;
3049         }
3050       if (const_ok_for_arm (val))
3051         {
3052           if (generate)
3053             emit_constant_insn (cond,
3054                                 gen_rtx_SET (VOIDmode, target,
3055                                              gen_rtx_MINUS (mode, GEN_INT (val),
3056                                                             source)));
3057           return 1;
3058         }
3059
3060       break;
3061
3062     default:
3063       gcc_unreachable ();
3064     }
3065
3066   /* If we can do it in one insn get out quickly.  */
3067   if (const_ok_for_op (val, code))
3068     {
3069       if (generate)
3070         emit_constant_insn (cond,
3071                             gen_rtx_SET (VOIDmode, target,
3072                                          (source
3073                                           ? gen_rtx_fmt_ee (code, mode, source,
3074                                                             GEN_INT (val))
3075                                           : GEN_INT (val))));
3076       return 1;
3077     }
3078
3079   /* On targets with UXTH/UBFX, we can deal with AND (2^N)-1 in a single
3080      insn.  */
3081   if (code == AND && (i = exact_log2 (remainder + 1)) > 0
3082       && (arm_arch_thumb2 || (i == 16 && arm_arch6 && mode == SImode)))
3083     {
3084       if (generate)
3085         {
3086           if (mode == SImode && i == 16)
3087             /* Use UXTH in preference to UBFX, since on Thumb2 it's a
3088                smaller insn.  */
3089             emit_constant_insn (cond,
3090                                 gen_zero_extendhisi2
3091                                 (target, gen_lowpart (HImode, source)));
3092           else
3093             /* Extz only supports SImode, but we can coerce the operands
3094                into that mode.  */
3095             emit_constant_insn (cond,
3096                                 gen_extzv_t2 (gen_lowpart (SImode, target),
3097                                               gen_lowpart (SImode, source),
3098                                               GEN_INT (i), const0_rtx));
3099         }
3100
3101       return 1;
3102     }
3103
3104   /* Calculate a few attributes that may be useful for specific
3105      optimizations.  */
3106   /* Count number of leading zeros.  */
3107   for (i = 31; i >= 0; i--)
3108     {
3109       if ((remainder & (1 << i)) == 0)
3110         clear_sign_bit_copies++;
3111       else
3112         break;
3113     }
3114
3115   /* Count number of leading 1's.  */
3116   for (i = 31; i >= 0; i--)
3117     {
3118       if ((remainder & (1 << i)) != 0)
3119         set_sign_bit_copies++;
3120       else
3121         break;
3122     }
3123
3124   /* Count number of trailing zero's.  */
3125   for (i = 0; i <= 31; i++)
3126     {
3127       if ((remainder & (1 << i)) == 0)
3128         clear_zero_bit_copies++;
3129       else
3130         break;
3131     }
3132
3133   /* Count number of trailing 1's.  */
3134   for (i = 0; i <= 31; i++)
3135     {
3136       if ((remainder & (1 << i)) != 0)
3137         set_zero_bit_copies++;
3138       else
3139         break;
3140     }
3141
3142   switch (code)
3143     {
3144     case SET:
3145       /* See if we can do this by sign_extending a constant that is known
3146          to be negative.  This is a good, way of doing it, since the shift
3147          may well merge into a subsequent insn.  */
3148       if (set_sign_bit_copies > 1)
3149         {
3150           if (const_ok_for_arm
3151               (temp1 = ARM_SIGN_EXTEND (remainder
3152                                         << (set_sign_bit_copies - 1))))
3153             {
3154               if (generate)
3155                 {
3156                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3157                   emit_constant_insn (cond,
3158                                       gen_rtx_SET (VOIDmode, new_src,
3159                                                    GEN_INT (temp1)));
3160                   emit_constant_insn (cond,
3161                                       gen_ashrsi3 (target, new_src,
3162                                                    GEN_INT (set_sign_bit_copies - 1)));
3163                 }
3164               return 2;
3165             }
3166           /* For an inverted constant, we will need to set the low bits,
3167              these will be shifted out of harm's way.  */
3168           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
3169           if (const_ok_for_arm (~temp1))
3170             {
3171               if (generate)
3172                 {
3173                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3174                   emit_constant_insn (cond,
3175                                       gen_rtx_SET (VOIDmode, new_src,
3176                                                    GEN_INT (temp1)));
3177                   emit_constant_insn (cond,
3178                                       gen_ashrsi3 (target, new_src,
3179                                                    GEN_INT (set_sign_bit_copies - 1)));
3180                 }
3181               return 2;
3182             }
3183         }
3184
3185       /* See if we can calculate the value as the difference between two
3186          valid immediates.  */
3187       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
3188         {
3189           int topshift = clear_sign_bit_copies & ~1;
3190
3191           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
3192                                    & (0xff000000 >> topshift));
3193
3194           /* If temp1 is zero, then that means the 9 most significant
3195              bits of remainder were 1 and we've caused it to overflow.
3196              When topshift is 0 we don't need to do anything since we
3197              can borrow from 'bit 32'.  */
3198           if (temp1 == 0 && topshift != 0)
3199             temp1 = 0x80000000 >> (topshift - 1);
3200
3201           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
3202
3203           if (const_ok_for_arm (temp2))
3204             {
3205               if (generate)
3206                 {
3207                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3208                   emit_constant_insn (cond,
3209                                       gen_rtx_SET (VOIDmode, new_src,
3210                                                    GEN_INT (temp1)));
3211                   emit_constant_insn (cond,
3212                                       gen_addsi3 (target, new_src,
3213                                                   GEN_INT (-temp2)));
3214                 }
3215
3216               return 2;
3217             }
3218         }
3219
3220       /* See if we can generate this by setting the bottom (or the top)
3221          16 bits, and then shifting these into the other half of the
3222          word.  We only look for the simplest cases, to do more would cost
3223          too much.  Be careful, however, not to generate this when the
3224          alternative would take fewer insns.  */
3225       if (val & 0xffff0000)
3226         {
3227           temp1 = remainder & 0xffff0000;
3228           temp2 = remainder & 0x0000ffff;
3229
3230           /* Overlaps outside this range are best done using other methods.  */
3231           for (i = 9; i < 24; i++)
3232             {
3233               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
3234                   && !const_ok_for_arm (temp2))
3235                 {
3236                   rtx new_src = (subtargets
3237                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
3238                                  : target);
3239                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
3240                                             source, subtargets, generate);
3241                   source = new_src;
3242                   if (generate)
3243                     emit_constant_insn
3244                       (cond,
3245                        gen_rtx_SET
3246                        (VOIDmode, target,
3247                         gen_rtx_IOR (mode,
3248                                      gen_rtx_ASHIFT (mode, source,
3249                                                      GEN_INT (i)),
3250                                      source)));
3251                   return insns + 1;
3252                 }
3253             }
3254
3255           /* Don't duplicate cases already considered.  */
3256           for (i = 17; i < 24; i++)
3257             {
3258               if (((temp1 | (temp1 >> i)) == remainder)
3259                   && !const_ok_for_arm (temp1))
3260                 {
3261                   rtx new_src = (subtargets
3262                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
3263                                  : target);
3264                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
3265                                             source, subtargets, generate);
3266                   source = new_src;
3267                   if (generate)
3268                     emit_constant_insn
3269                       (cond,
3270                        gen_rtx_SET (VOIDmode, target,
3271                                     gen_rtx_IOR
3272                                     (mode,
3273                                      gen_rtx_LSHIFTRT (mode, source,
3274                                                        GEN_INT (i)),
3275                                      source)));
3276                   return insns + 1;
3277                 }
3278             }
3279         }
3280       break;
3281
3282     case IOR:
3283     case XOR:
3284       /* If we have IOR or XOR, and the constant can be loaded in a
3285          single instruction, and we can find a temporary to put it in,
3286          then this can be done in two instructions instead of 3-4.  */
3287       if (subtargets
3288           /* TARGET can't be NULL if SUBTARGETS is 0 */
3289           || (reload_completed && !reg_mentioned_p (target, source)))
3290         {
3291           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
3292             {
3293               if (generate)
3294                 {
3295                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3296
3297                   emit_constant_insn (cond,
3298                                       gen_rtx_SET (VOIDmode, sub,
3299                                                    GEN_INT (val)));
3300                   emit_constant_insn (cond,
3301                                       gen_rtx_SET (VOIDmode, target,
3302                                                    gen_rtx_fmt_ee (code, mode,
3303                                                                    source, sub)));
3304                 }
3305               return 2;
3306             }
3307         }
3308
3309       if (code == XOR)
3310         break;
3311
3312       /*  Convert.
3313           x = y | constant ( which is composed of set_sign_bit_copies of leading 1s
3314                              and the remainder 0s for e.g. 0xfff00000)
3315           x = ~(~(y ashift set_sign_bit_copies) lshiftrt set_sign_bit_copies)
3316
3317           This can be done in 2 instructions by using shifts with mov or mvn.
3318           e.g. for
3319           x = x | 0xfff00000;
3320           we generate.
3321           mvn   r0, r0, asl #12
3322           mvn   r0, r0, lsr #12  */
3323       if (set_sign_bit_copies > 8
3324           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
3325         {
3326           if (generate)
3327             {
3328               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3329               rtx shift = GEN_INT (set_sign_bit_copies);
3330
3331               emit_constant_insn
3332                 (cond,
3333                  gen_rtx_SET (VOIDmode, sub,
3334                               gen_rtx_NOT (mode,
3335                                            gen_rtx_ASHIFT (mode,
3336                                                            source,
3337                                                            shift))));
3338               emit_constant_insn
3339                 (cond,
3340                  gen_rtx_SET (VOIDmode, target,
3341                               gen_rtx_NOT (mode,
3342                                            gen_rtx_LSHIFTRT (mode, sub,
3343                                                              shift))));
3344             }
3345           return 2;
3346         }
3347
3348       /* Convert
3349           x = y | constant (which has set_zero_bit_copies number of trailing ones).
3350            to
3351           x = ~((~y lshiftrt set_zero_bit_copies) ashift set_zero_bit_copies).
3352
3353           For eg. r0 = r0 | 0xfff
3354                mvn      r0, r0, lsr #12
3355                mvn      r0, r0, asl #12
3356
3357       */
3358       if (set_zero_bit_copies > 8
3359           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
3360         {
3361           if (generate)
3362             {
3363               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3364               rtx shift = GEN_INT (set_zero_bit_copies);
3365
3366               emit_constant_insn
3367                 (cond,
3368                  gen_rtx_SET (VOIDmode, sub,
3369                               gen_rtx_NOT (mode,
3370                                            gen_rtx_LSHIFTRT (mode,
3371                                                              source,
3372                                                              shift))));
3373               emit_constant_insn
3374                 (cond,
3375                  gen_rtx_SET (VOIDmode, target,
3376                               gen_rtx_NOT (mode,
3377                                            gen_rtx_ASHIFT (mode, sub,
3378                                                            shift))));
3379             }
3380           return 2;
3381         }
3382
3383       /* This will never be reached for Thumb2 because orn is a valid
3384          instruction. This is for Thumb1 and the ARM 32 bit cases.
3385
3386          x = y | constant (such that ~constant is a valid constant)
3387          Transform this to
3388          x = ~(~y & ~constant).
3389       */
3390       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
3391         {
3392           if (generate)
3393             {
3394               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
3395               emit_constant_insn (cond,
3396                                   gen_rtx_SET (VOIDmode, sub,
3397                                                gen_rtx_NOT (mode, source)));
3398               source = sub;
3399               if (subtargets)
3400                 sub = gen_reg_rtx (mode);
3401               emit_constant_insn (cond,
3402                                   gen_rtx_SET (VOIDmode, sub,
3403                                                gen_rtx_AND (mode, source,
3404                                                             GEN_INT (temp1))));
3405               emit_constant_insn (cond,
3406                                   gen_rtx_SET (VOIDmode, target,
3407                                                gen_rtx_NOT (mode, sub)));
3408             }
3409           return 3;
3410         }
3411       break;
3412
3413     case AND:
3414       /* See if two shifts will do 2 or more insn's worth of work.  */
3415       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
3416         {
3417           HOST_WIDE_INT shift_mask = ((0xffffffff
3418                                        << (32 - clear_sign_bit_copies))
3419                                       & 0xffffffff);
3420
3421           if ((remainder | shift_mask) != 0xffffffff)
3422             {
3423               if (generate)
3424                 {
3425                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3426                   insns = arm_gen_constant (AND, mode, cond,
3427                                             remainder | shift_mask,
3428                                             new_src, source, subtargets, 1);
3429                   source = new_src;
3430                 }
3431               else
3432                 {
3433                   rtx targ = subtargets ? NULL_RTX : target;
3434                   insns = arm_gen_constant (AND, mode, cond,
3435                                             remainder | shift_mask,
3436                                             targ, source, subtargets, 0);
3437                 }
3438             }
3439
3440           if (generate)
3441             {
3442               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3443               rtx shift = GEN_INT (clear_sign_bit_copies);
3444
3445               emit_insn (gen_ashlsi3 (new_src, source, shift));
3446               emit_insn (gen_lshrsi3 (target, new_src, shift));
3447             }
3448
3449           return insns + 2;
3450         }
3451
3452       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
3453         {
3454           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
3455
3456           if ((remainder | shift_mask) != 0xffffffff)
3457             {
3458               if (generate)
3459                 {
3460                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3461
3462                   insns = arm_gen_constant (AND, mode, cond,
3463                                             remainder | shift_mask,
3464                                             new_src, source, subtargets, 1);
3465                   source = new_src;
3466                 }
3467               else
3468                 {
3469                   rtx targ = subtargets ? NULL_RTX : target;
3470
3471                   insns = arm_gen_constant (AND, mode, cond,
3472                                             remainder | shift_mask,
3473                                             targ, source, subtargets, 0);
3474                 }
3475             }
3476
3477           if (generate)
3478             {
3479               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
3480               rtx shift = GEN_INT (clear_zero_bit_copies);
3481
3482               emit_insn (gen_lshrsi3 (new_src, source, shift));
3483               emit_insn (gen_ashlsi3 (target, new_src, shift));
3484             }
3485
3486           return insns + 2;
3487         }
3488
3489       break;
3490
3491     default:
3492       break;
3493     }
3494
3495   /* Calculate what the instruction sequences would be if we generated it
3496      normally, negated, or inverted.  */
3497   if (code == AND)
3498     /* AND cannot be split into multiple insns, so invert and use BIC.  */
3499     insns = 99;
3500   else
3501     insns = optimal_immediate_sequence (code, remainder, &pos_immediates);
3502
3503   if (can_negate)
3504     neg_insns = optimal_immediate_sequence (code, (-remainder) & 0xffffffff,
3505                                             &neg_immediates);
3506   else
3507     neg_insns = 99;
3508
3509   if (can_invert || final_invert)
3510     inv_insns = optimal_immediate_sequence (code, remainder ^ 0xffffffff,
3511                                             &inv_immediates);
3512   else
3513     inv_insns = 99;
3514
3515   immediates = &pos_immediates;
3516
3517   /* Is the negated immediate sequence more efficient?  */
3518   if (neg_insns < insns && neg_insns <= inv_insns)
3519     {
3520       insns = neg_insns;
3521       immediates = &neg_immediates;
3522     }
3523   else
3524     can_negate = 0;
3525
3526   /* Is the inverted immediate sequence more efficient?
3527      We must allow for an extra NOT instruction for XOR operations, although
3528      there is some chance that the final 'mvn' will get optimized later.  */
3529   if ((inv_insns + 1) < insns || (!final_invert && inv_insns < insns))
3530     {
3531       insns = inv_insns;
3532       immediates = &inv_immediates;
3533     }
3534   else
3535     {
3536       can_invert = 0;
3537       final_invert = 0;
3538     }
3539
3540   /* Now output the chosen sequence as instructions.  */
3541   if (generate)
3542     {
3543       for (i = 0; i < insns; i++)
3544         {
3545           rtx new_src, temp1_rtx;
3546
3547           temp1 = immediates->i[i];
3548
3549           if (code == SET || code == MINUS)
3550             new_src = (subtargets ? gen_reg_rtx (mode) : target);
3551           else if ((final_invert || i < (insns - 1)) && subtargets)
3552             new_src = gen_reg_rtx (mode);
3553           else
3554             new_src = target;
3555
3556           if (can_invert)
3557             temp1 = ~temp1;
3558           else if (can_negate)
3559             temp1 = -temp1;
3560
3561           temp1 = trunc_int_for_mode (temp1, mode);
3562           temp1_rtx = GEN_INT (temp1);
3563
3564           if (code == SET)
3565             ;
3566           else if (code == MINUS)
3567             temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
3568           else
3569             temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
3570
3571           emit_constant_insn (cond,
3572                               gen_rtx_SET (VOIDmode, new_src,
3573                                            temp1_rtx));
3574           source = new_src;
3575
3576           if (code == SET)
3577             {
3578               can_negate = can_invert;
3579               can_invert = 0;
3580               code = PLUS;
3581             }
3582           else if (code == MINUS)
3583             code = PLUS;
3584         }
3585     }
3586
3587   if (final_invert)
3588     {
3589       if (generate)
3590         emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
3591                                                gen_rtx_NOT (mode, source)));
3592       insns++;
3593     }
3594
3595   return insns;
3596 }
3597
3598 /* Canonicalize a comparison so that we are more likely to recognize it.
3599    This can be done for a few constant compares, where we can make the
3600    immediate value easier to load.  */
3601
3602 static void
3603 arm_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
3604                              bool op0_preserve_value)
3605 {
3606   enum machine_mode mode;
3607   unsigned HOST_WIDE_INT i, maxval;
3608
3609   mode = GET_MODE (*op0);
3610   if (mode == VOIDmode)
3611     mode = GET_MODE (*op1);
3612
3613   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
3614
3615   /* For DImode, we have GE/LT/GEU/LTU comparisons.  In ARM mode
3616      we can also use cmp/cmpeq for GTU/LEU.  GT/LE must be either
3617      reversed or (for constant OP1) adjusted to GE/LT.  Similarly
3618      for GTU/LEU in Thumb mode.  */
3619   if (mode == DImode)
3620     {
3621       rtx tem;
3622
3623       if (*code == GT || *code == LE
3624           || (!TARGET_ARM && (*code == GTU || *code == LEU)))
3625         {
3626           /* Missing comparison.  First try to use an available
3627              comparison.  */
3628           if (CONST_INT_P (*op1))
3629             {
3630               i = INTVAL (*op1);
3631               switch (*code)
3632                 {
3633                 case GT:
3634                 case LE:
3635                   if (i != maxval
3636                       && arm_const_double_by_immediates (GEN_INT (i + 1)))
3637                     {
3638                       *op1 = GEN_INT (i + 1);
3639                       *code = *code == GT ? GE : LT;
3640                       return;
3641                     }
3642                   break;
3643                 case GTU:
3644                 case LEU:
3645                   if (i != ~((unsigned HOST_WIDE_INT) 0)
3646                       && arm_const_double_by_immediates (GEN_INT (i + 1)))
3647                     {
3648                       *op1 = GEN_INT (i + 1);
3649                       *code = *code == GTU ? GEU : LTU;
3650                       return;
3651                     }
3652                   break;
3653                 default:
3654                   gcc_unreachable ();
3655                 }
3656             }
3657
3658           /* If that did not work, reverse the condition.  */
3659           if (!op0_preserve_value)
3660             {
3661               tem = *op0;
3662               *op0 = *op1;
3663               *op1 = tem;
3664               *code = (int)swap_condition ((enum rtx_code)*code);
3665             }
3666         }
3667       return;
3668     }
3669
3670   /* If *op0 is (zero_extend:SI (subreg:QI (reg:SI) 0)) and comparing
3671      with const0_rtx, change it to (and:SI (reg:SI) (const_int 255)),
3672      to facilitate possible combining with a cmp into 'ands'.  */
3673   if (mode == SImode
3674       && GET_CODE (*op0) == ZERO_EXTEND
3675       && GET_CODE (XEXP (*op0, 0)) == SUBREG
3676       && GET_MODE (XEXP (*op0, 0)) == QImode
3677       && GET_MODE (SUBREG_REG (XEXP (*op0, 0))) == SImode
3678       && subreg_lowpart_p (XEXP (*op0, 0))
3679       && *op1 == const0_rtx)
3680     *op0 = gen_rtx_AND (SImode, SUBREG_REG (XEXP (*op0, 0)),
3681                         GEN_INT (255));
3682
3683   /* Comparisons smaller than DImode.  Only adjust comparisons against
3684      an out-of-range constant.  */
3685   if (!CONST_INT_P (*op1)
3686       || const_ok_for_arm (INTVAL (*op1))
3687       || const_ok_for_arm (- INTVAL (*op1)))
3688     return;
3689
3690   i = INTVAL (*op1);
3691
3692   switch (*code)
3693     {
3694     case EQ:
3695     case NE:
3696       return;
3697
3698     case GT:
3699     case LE:
3700       if (i != maxval
3701           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3702         {
3703           *op1 = GEN_INT (i + 1);
3704           *code = *code == GT ? GE : LT;
3705           return;
3706         }
3707       break;
3708
3709     case GE:
3710     case LT:
3711       if (i != ~maxval
3712           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3713         {
3714           *op1 = GEN_INT (i - 1);
3715           *code = *code == GE ? GT : LE;
3716           return;
3717         }
3718       break;
3719
3720     case GTU:
3721     case LEU:
3722       if (i != ~((unsigned HOST_WIDE_INT) 0)
3723           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3724         {
3725           *op1 = GEN_INT (i + 1);
3726           *code = *code == GTU ? GEU : LTU;
3727           return;
3728         }
3729       break;
3730
3731     case GEU:
3732     case LTU:
3733       if (i != 0
3734           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3735         {
3736           *op1 = GEN_INT (i - 1);
3737           *code = *code == GEU ? GTU : LEU;
3738           return;
3739         }
3740       break;
3741
3742     default:
3743       gcc_unreachable ();
3744     }
3745 }
3746
3747
3748 /* Define how to find the value returned by a function.  */
3749
3750 static rtx
3751 arm_function_value(const_tree type, const_tree func,
3752                    bool outgoing ATTRIBUTE_UNUSED)
3753 {
3754   enum machine_mode mode;
3755   int unsignedp ATTRIBUTE_UNUSED;
3756   rtx r ATTRIBUTE_UNUSED;
3757
3758   mode = TYPE_MODE (type);
3759
3760   if (TARGET_AAPCS_BASED)
3761     return aapcs_allocate_return_reg (mode, type, func);
3762
3763   /* Promote integer types.  */
3764   if (INTEGRAL_TYPE_P (type))
3765     mode = arm_promote_function_mode (type, mode, &unsignedp, func, 1);
3766
3767   /* Promotes small structs returned in a register to full-word size
3768      for big-endian AAPCS.  */
3769   if (arm_return_in_msb (type))
3770     {
3771       HOST_WIDE_INT size = int_size_in_bytes (type);
3772       if (size % UNITS_PER_WORD != 0)
3773         {
3774           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
3775           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
3776         }
3777     }
3778
3779   return arm_libcall_value_1 (mode);
3780 }
3781
3782 static int
3783 libcall_eq (const void *p1, const void *p2)
3784 {
3785   return rtx_equal_p ((const_rtx) p1, (const_rtx) p2);
3786 }
3787
3788 static hashval_t
3789 libcall_hash (const void *p1)
3790 {
3791   return hash_rtx ((const_rtx) p1, VOIDmode, NULL, NULL, FALSE);
3792 }
3793
3794 static void
3795 add_libcall (htab_t htab, rtx libcall)
3796 {
3797   *htab_find_slot (htab, libcall, INSERT) = libcall;
3798 }
3799
3800 static bool
3801 arm_libcall_uses_aapcs_base (const_rtx libcall)
3802 {
3803   static bool init_done = false;
3804   static htab_t libcall_htab;
3805
3806   if (!init_done)
3807     {
3808       init_done = true;
3809
3810       libcall_htab = htab_create (31, libcall_hash, libcall_eq,
3811                                   NULL);
3812       add_libcall (libcall_htab,
3813                    convert_optab_libfunc (sfloat_optab, SFmode, SImode));
3814       add_libcall (libcall_htab,
3815                    convert_optab_libfunc (sfloat_optab, DFmode, SImode));
3816       add_libcall (libcall_htab,
3817                    convert_optab_libfunc (sfloat_optab, SFmode, DImode));
3818       add_libcall (libcall_htab,
3819                    convert_optab_libfunc (sfloat_optab, DFmode, DImode));
3820
3821       add_libcall (libcall_htab,
3822                    convert_optab_libfunc (ufloat_optab, SFmode, SImode));
3823       add_libcall (libcall_htab,
3824                    convert_optab_libfunc (ufloat_optab, DFmode, SImode));
3825       add_libcall (libcall_htab,
3826                    convert_optab_libfunc (ufloat_optab, SFmode, DImode));
3827       add_libcall (libcall_htab,
3828                    convert_optab_libfunc (ufloat_optab, DFmode, DImode));
3829
3830       add_libcall (libcall_htab,
3831                    convert_optab_libfunc (sext_optab, SFmode, HFmode));
3832       add_libcall (libcall_htab,
3833                    convert_optab_libfunc (trunc_optab, HFmode, SFmode));
3834       add_libcall (libcall_htab,
3835                    convert_optab_libfunc (sfix_optab, SImode, DFmode));
3836       add_libcall (libcall_htab,
3837                    convert_optab_libfunc (ufix_optab, SImode, DFmode));
3838       add_libcall (libcall_htab,
3839                    convert_optab_libfunc (sfix_optab, DImode, DFmode));
3840       add_libcall (libcall_htab,
3841                    convert_optab_libfunc (ufix_optab, DImode, DFmode));
3842       add_libcall (libcall_htab,
3843                    convert_optab_libfunc (sfix_optab, DImode, SFmode));
3844       add_libcall (libcall_htab,
3845                    convert_optab_libfunc (ufix_optab, DImode, SFmode));
3846
3847       /* Values from double-precision helper functions are returned in core
3848          registers if the selected core only supports single-precision
3849          arithmetic, even if we are using the hard-float ABI.  The same is
3850          true for single-precision helpers, but we will never be using the
3851          hard-float ABI on a CPU which doesn't support single-precision
3852          operations in hardware.  */
3853       add_libcall (libcall_htab, optab_libfunc (add_optab, DFmode));
3854       add_libcall (libcall_htab, optab_libfunc (sdiv_optab, DFmode));
3855       add_libcall (libcall_htab, optab_libfunc (smul_optab, DFmode));
3856       add_libcall (libcall_htab, optab_libfunc (neg_optab, DFmode));
3857       add_libcall (libcall_htab, optab_libfunc (sub_optab, DFmode));
3858       add_libcall (libcall_htab, optab_libfunc (eq_optab, DFmode));
3859       add_libcall (libcall_htab, optab_libfunc (lt_optab, DFmode));
3860       add_libcall (libcall_htab, optab_libfunc (le_optab, DFmode));
3861       add_libcall (libcall_htab, optab_libfunc (ge_optab, DFmode));
3862       add_libcall (libcall_htab, optab_libfunc (gt_optab, DFmode));
3863       add_libcall (libcall_htab, optab_libfunc (unord_optab, DFmode));
3864       add_libcall (libcall_htab, convert_optab_libfunc (sext_optab, DFmode,
3865                                                         SFmode));
3866       add_libcall (libcall_htab, convert_optab_libfunc (trunc_optab, SFmode,
3867                                                         DFmode));
3868     }
3869
3870   return libcall && htab_find (libcall_htab, libcall) != NULL;
3871 }
3872
3873 static rtx
3874 arm_libcall_value_1 (enum machine_mode mode)
3875 {
3876   if (TARGET_AAPCS_BASED)
3877     return aapcs_libcall_value (mode);
3878   else if (TARGET_IWMMXT_ABI
3879            && arm_vector_mode_supported_p (mode))
3880     return gen_rtx_REG (mode, FIRST_IWMMXT_REGNUM);
3881   else
3882     return gen_rtx_REG (mode, ARG_REGISTER (1));
3883 }
3884
3885 /* Define how to find the value returned by a library function
3886    assuming the value has mode MODE.  */
3887
3888 static rtx
3889 arm_libcall_value (enum machine_mode mode, const_rtx libcall)
3890 {
3891   if (TARGET_AAPCS_BASED && arm_pcs_default != ARM_PCS_AAPCS
3892       && GET_MODE_CLASS (mode) == MODE_FLOAT)
3893     {
3894       /* The following libcalls return their result in integer registers,
3895          even though they return a floating point value.  */
3896       if (arm_libcall_uses_aapcs_base (libcall))
3897         return gen_rtx_REG (mode, ARG_REGISTER(1));
3898
3899     }
3900
3901   return arm_libcall_value_1 (mode);
3902 }
3903
3904 /* Implement TARGET_FUNCTION_VALUE_REGNO_P.  */
3905
3906 static bool
3907 arm_function_value_regno_p (const unsigned int regno)
3908 {
3909   if (regno == ARG_REGISTER (1)
3910       || (TARGET_32BIT
3911           && TARGET_AAPCS_BASED
3912           && TARGET_VFP
3913           && TARGET_HARD_FLOAT
3914           && regno == FIRST_VFP_REGNUM)
3915       || (TARGET_IWMMXT_ABI
3916           && regno == FIRST_IWMMXT_REGNUM))
3917     return true;
3918
3919   return false;
3920 }
3921
3922 /* Determine the amount of memory needed to store the possible return
3923    registers of an untyped call.  */
3924 int
3925 arm_apply_result_size (void)
3926 {
3927   int size = 16;
3928
3929   if (TARGET_32BIT)
3930     {
3931       if (TARGET_HARD_FLOAT_ABI && TARGET_VFP)
3932         size += 32;
3933       if (TARGET_IWMMXT_ABI)
3934         size += 8;
3935     }
3936
3937   return size;
3938 }
3939
3940 /* Decide whether TYPE should be returned in memory (true)
3941    or in a register (false).  FNTYPE is the type of the function making
3942    the call.  */
3943 static bool
3944 arm_return_in_memory (const_tree type, const_tree fntype)
3945 {
3946   HOST_WIDE_INT size;
3947
3948   size = int_size_in_bytes (type);  /* Negative if not fixed size.  */
3949
3950   if (TARGET_AAPCS_BASED)
3951     {
3952       /* Simple, non-aggregate types (ie not including vectors and
3953          complex) are always returned in a register (or registers).
3954          We don't care about which register here, so we can short-cut
3955          some of the detail.  */
3956       if (!AGGREGATE_TYPE_P (type)
3957           && TREE_CODE (type) != VECTOR_TYPE
3958           && TREE_CODE (type) != COMPLEX_TYPE)
3959         return false;
3960
3961       /* Any return value that is no larger than one word can be
3962          returned in r0.  */
3963       if (((unsigned HOST_WIDE_INT) size) <= UNITS_PER_WORD)
3964         return false;
3965
3966       /* Check any available co-processors to see if they accept the
3967          type as a register candidate (VFP, for example, can return
3968          some aggregates in consecutive registers).  These aren't
3969          available if the call is variadic.  */
3970       if (aapcs_select_return_coproc (type, fntype) >= 0)
3971         return false;
3972
3973       /* Vector values should be returned using ARM registers, not
3974          memory (unless they're over 16 bytes, which will break since
3975          we only have four call-clobbered registers to play with).  */
3976       if (TREE_CODE (type) == VECTOR_TYPE)
3977         return (size < 0 || size > (4 * UNITS_PER_WORD));
3978
3979       /* The rest go in memory.  */
3980       return true;
3981     }
3982
3983   if (TREE_CODE (type) == VECTOR_TYPE)
3984     return (size < 0 || size > (4 * UNITS_PER_WORD));
3985
3986   if (!AGGREGATE_TYPE_P (type) &&
3987       (TREE_CODE (type) != VECTOR_TYPE))
3988     /* All simple types are returned in registers.  */
3989     return false;
3990
3991   if (arm_abi != ARM_ABI_APCS)
3992     {
3993       /* ATPCS and later return aggregate types in memory only if they are
3994          larger than a word (or are variable size).  */
3995       return (size < 0 || size > UNITS_PER_WORD);
3996     }
3997
3998   /* For the arm-wince targets we choose to be compatible with Microsoft's
3999      ARM and Thumb compilers, which always return aggregates in memory.  */
4000 #ifndef ARM_WINCE
4001   /* All structures/unions bigger than one word are returned in memory.
4002      Also catch the case where int_size_in_bytes returns -1.  In this case
4003      the aggregate is either huge or of variable size, and in either case
4004      we will want to return it via memory and not in a register.  */
4005   if (size < 0 || size > UNITS_PER_WORD)
4006     return true;
4007
4008   if (TREE_CODE (type) == RECORD_TYPE)
4009     {
4010       tree field;
4011
4012       /* For a struct the APCS says that we only return in a register
4013          if the type is 'integer like' and every addressable element
4014          has an offset of zero.  For practical purposes this means
4015          that the structure can have at most one non bit-field element
4016          and that this element must be the first one in the structure.  */
4017
4018       /* Find the first field, ignoring non FIELD_DECL things which will
4019          have been created by C++.  */
4020       for (field = TYPE_FIELDS (type);
4021            field && TREE_CODE (field) != FIELD_DECL;
4022            field = DECL_CHAIN (field))
4023         continue;
4024
4025       if (field == NULL)
4026         return false; /* An empty structure.  Allowed by an extension to ANSI C.  */
4027
4028       /* Check that the first field is valid for returning in a register.  */
4029
4030       /* ... Floats are not allowed */
4031       if (FLOAT_TYPE_P (TREE_TYPE (field)))
4032         return true;
4033
4034       /* ... Aggregates that are not themselves valid for returning in
4035          a register are not allowed.  */
4036       if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
4037         return true;
4038
4039       /* Now check the remaining fields, if any.  Only bitfields are allowed,
4040          since they are not addressable.  */
4041       for (field = DECL_CHAIN (field);
4042            field;
4043            field = DECL_CHAIN (field))
4044         {
4045           if (TREE_CODE (field) != FIELD_DECL)
4046             continue;
4047
4048           if (!DECL_BIT_FIELD_TYPE (field))
4049             return true;
4050         }
4051
4052       return false;
4053     }
4054
4055   if (TREE_CODE (type) == UNION_TYPE)
4056     {
4057       tree field;
4058
4059       /* Unions can be returned in registers if every element is
4060          integral, or can be returned in an integer register.  */
4061       for (field = TYPE_FIELDS (type);
4062            field;
4063            field = DECL_CHAIN (field))
4064         {
4065           if (TREE_CODE (field) != FIELD_DECL)
4066             continue;
4067
4068           if (FLOAT_TYPE_P (TREE_TYPE (field)))
4069             return true;
4070
4071           if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
4072             return true;
4073         }
4074
4075       return false;
4076     }
4077 #endif /* not ARM_WINCE */
4078
4079   /* Return all other types in memory.  */
4080   return true;
4081 }
4082
4083 const struct pcs_attribute_arg
4084 {
4085   const char *arg;
4086   enum arm_pcs value;
4087 } pcs_attribute_args[] =
4088   {
4089     {"aapcs", ARM_PCS_AAPCS},
4090     {"aapcs-vfp", ARM_PCS_AAPCS_VFP},
4091 #if 0
4092     /* We could recognize these, but changes would be needed elsewhere
4093      * to implement them.  */
4094     {"aapcs-iwmmxt", ARM_PCS_AAPCS_IWMMXT},
4095     {"atpcs", ARM_PCS_ATPCS},
4096     {"apcs", ARM_PCS_APCS},
4097 #endif
4098     {NULL, ARM_PCS_UNKNOWN}
4099   };
4100
4101 static enum arm_pcs
4102 arm_pcs_from_attribute (tree attr)
4103 {
4104   const struct pcs_attribute_arg *ptr;
4105   const char *arg;
4106
4107   /* Get the value of the argument.  */
4108   if (TREE_VALUE (attr) == NULL_TREE
4109       || TREE_CODE (TREE_VALUE (attr)) != STRING_CST)
4110     return ARM_PCS_UNKNOWN;
4111
4112   arg = TREE_STRING_POINTER (TREE_VALUE (attr));
4113
4114   /* Check it against the list of known arguments.  */
4115   for (ptr = pcs_attribute_args; ptr->arg != NULL; ptr++)
4116     if (streq (arg, ptr->arg))
4117       return ptr->value;
4118
4119   /* An unrecognized interrupt type.  */
4120   return ARM_PCS_UNKNOWN;
4121 }
4122
4123 /* Get the PCS variant to use for this call.  TYPE is the function's type
4124    specification, DECL is the specific declartion.  DECL may be null if
4125    the call could be indirect or if this is a library call.  */
4126 static enum arm_pcs
4127 arm_get_pcs_model (const_tree type, const_tree decl)
4128 {
4129   bool user_convention = false;
4130   enum arm_pcs user_pcs = arm_pcs_default;
4131   tree attr;
4132
4133   gcc_assert (type);
4134
4135   attr = lookup_attribute ("pcs", TYPE_ATTRIBUTES (type));
4136   if (attr)
4137     {
4138       user_pcs = arm_pcs_from_attribute (TREE_VALUE (attr));
4139       user_convention = true;
4140     }
4141
4142   if (TARGET_AAPCS_BASED)
4143     {
4144       /* Detect varargs functions.  These always use the base rules
4145          (no argument is ever a candidate for a co-processor
4146          register).  */
4147       bool base_rules = stdarg_p (type);
4148
4149       if (user_convention)
4150         {
4151           if (user_pcs > ARM_PCS_AAPCS_LOCAL)
4152             sorry ("non-AAPCS derived PCS variant");
4153           else if (base_rules && user_pcs != ARM_PCS_AAPCS)
4154             error ("variadic functions must use the base AAPCS variant");
4155         }
4156
4157       if (base_rules)
4158         return ARM_PCS_AAPCS;
4159       else if (user_convention)
4160         return user_pcs;
4161       else if (decl && flag_unit_at_a_time)
4162         {
4163           /* Local functions never leak outside this compilation unit,
4164              so we are free to use whatever conventions are
4165              appropriate.  */
4166           /* FIXME: remove CONST_CAST_TREE when cgraph is constified.  */
4167           struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
4168           if (i && i->local)
4169             return ARM_PCS_AAPCS_LOCAL;
4170         }
4171     }
4172   else if (user_convention && user_pcs != arm_pcs_default)
4173     sorry ("PCS variant");
4174
4175   /* For everything else we use the target's default.  */
4176   return arm_pcs_default;
4177 }
4178
4179
4180 static void
4181 aapcs_vfp_cum_init (CUMULATIVE_ARGS *pcum  ATTRIBUTE_UNUSED,
4182                     const_tree fntype ATTRIBUTE_UNUSED,
4183                     rtx libcall ATTRIBUTE_UNUSED,
4184                     const_tree fndecl ATTRIBUTE_UNUSED)
4185 {
4186   /* Record the unallocated VFP registers.  */
4187   pcum->aapcs_vfp_regs_free = (1 << NUM_VFP_ARG_REGS) - 1;
4188   pcum->aapcs_vfp_reg_alloc = 0;
4189 }
4190
4191 /* Walk down the type tree of TYPE counting consecutive base elements.
4192    If *MODEP is VOIDmode, then set it to the first valid floating point
4193    type.  If a non-floating point type is found, or if a floating point
4194    type that doesn't match a non-VOIDmode *MODEP is found, then return -1,
4195    otherwise return the count in the sub-tree.  */
4196 static int
4197 aapcs_vfp_sub_candidate (const_tree type, enum machine_mode *modep)
4198 {
4199   enum machine_mode mode;
4200   HOST_WIDE_INT size;
4201
4202   switch (TREE_CODE (type))
4203     {
4204     case REAL_TYPE:
4205       mode = TYPE_MODE (type);
4206       if (mode != DFmode && mode != SFmode)
4207         return -1;
4208
4209       if (*modep == VOIDmode)
4210         *modep = mode;
4211
4212       if (*modep == mode)
4213         return 1;
4214
4215       break;
4216
4217     case COMPLEX_TYPE:
4218       mode = TYPE_MODE (TREE_TYPE (type));
4219       if (mode != DFmode && mode != SFmode)
4220         return -1;
4221
4222       if (*modep == VOIDmode)
4223         *modep = mode;
4224
4225       if (*modep == mode)
4226         return 2;
4227
4228       break;
4229
4230     case VECTOR_TYPE:
4231       /* Use V2SImode and V4SImode as representatives of all 64-bit
4232          and 128-bit vector types, whether or not those modes are
4233          supported with the present options.  */
4234       size = int_size_in_bytes (type);
4235       switch (size)
4236         {
4237         case 8:
4238           mode = V2SImode;
4239           break;
4240         case 16:
4241           mode = V4SImode;
4242           break;
4243         default:
4244           return -1;
4245         }
4246
4247       if (*modep == VOIDmode)
4248         *modep = mode;
4249
4250       /* Vector modes are considered to be opaque: two vectors are
4251          equivalent for the purposes of being homogeneous aggregates
4252          if they are the same size.  */
4253       if (*modep == mode)
4254         return 1;
4255
4256       break;
4257
4258     case ARRAY_TYPE:
4259       {
4260         int count;
4261         tree index = TYPE_DOMAIN (type);
4262
4263         /* Can't handle incomplete types.  */
4264         if (!COMPLETE_TYPE_P (type))
4265           return -1;
4266
4267         count = aapcs_vfp_sub_candidate (TREE_TYPE (type), modep);
4268         if (count == -1
4269             || !index
4270             || !TYPE_MAX_VALUE (index)
4271             || !host_integerp (TYPE_MAX_VALUE (index), 1)
4272             || !TYPE_MIN_VALUE (index)
4273             || !host_integerp (TYPE_MIN_VALUE (index), 1)
4274             || count < 0)
4275           return -1;
4276
4277         count *= (1 + tree_low_cst (TYPE_MAX_VALUE (index), 1)
4278                       - tree_low_cst (TYPE_MIN_VALUE (index), 1));
4279
4280         /* There must be no padding.  */
4281         if (!host_integerp (TYPE_SIZE (type), 1)
4282             || (tree_low_cst (TYPE_SIZE (type), 1)
4283                 != count * GET_MODE_BITSIZE (*modep)))
4284           return -1;
4285
4286         return count;
4287       }
4288
4289     case RECORD_TYPE:
4290       {
4291         int count = 0;
4292         int sub_count;
4293         tree field;
4294
4295         /* Can't handle incomplete types.  */
4296         if (!COMPLETE_TYPE_P (type))
4297           return -1;
4298
4299         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4300           {
4301             if (TREE_CODE (field) != FIELD_DECL)
4302               continue;
4303
4304             sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
4305             if (sub_count < 0)
4306               return -1;
4307             count += sub_count;
4308           }
4309
4310         /* There must be no padding.  */
4311         if (!host_integerp (TYPE_SIZE (type), 1)
4312             || (tree_low_cst (TYPE_SIZE (type), 1)
4313                 != count * GET_MODE_BITSIZE (*modep)))
4314           return -1;
4315
4316         return count;
4317       }
4318
4319     case UNION_TYPE:
4320     case QUAL_UNION_TYPE:
4321       {
4322         /* These aren't very interesting except in a degenerate case.  */
4323         int count = 0;
4324         int sub_count;
4325         tree field;
4326
4327         /* Can't handle incomplete types.  */
4328         if (!COMPLETE_TYPE_P (type))
4329           return -1;
4330
4331         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4332           {
4333             if (TREE_CODE (field) != FIELD_DECL)
4334               continue;
4335
4336             sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep);
4337             if (sub_count < 0)
4338               return -1;
4339             count = count > sub_count ? count : sub_count;
4340           }
4341
4342         /* There must be no padding.  */
4343         if (!host_integerp (TYPE_SIZE (type), 1)
4344             || (tree_low_cst (TYPE_SIZE (type), 1)
4345                 != count * GET_MODE_BITSIZE (*modep)))
4346           return -1;
4347
4348         return count;
4349       }
4350
4351     default:
4352       break;
4353     }
4354
4355   return -1;
4356 }
4357
4358 /* Return true if PCS_VARIANT should use VFP registers.  */
4359 static bool
4360 use_vfp_abi (enum arm_pcs pcs_variant, bool is_double)
4361 {
4362   if (pcs_variant == ARM_PCS_AAPCS_VFP)
4363     {
4364       static bool seen_thumb1_vfp = false;
4365
4366       if (TARGET_THUMB1 && !seen_thumb1_vfp)
4367         {
4368           sorry ("Thumb-1 hard-float VFP ABI");
4369           /* sorry() is not immediately fatal, so only display this once.  */
4370           seen_thumb1_vfp = true;
4371         }
4372
4373       return true;
4374     }
4375
4376   if (pcs_variant != ARM_PCS_AAPCS_LOCAL)
4377     return false;
4378
4379   return (TARGET_32BIT && TARGET_VFP && TARGET_HARD_FLOAT &&
4380           (TARGET_VFP_DOUBLE || !is_double));
4381 }
4382
4383 /* Return true if an argument whose type is TYPE, or mode is MODE, is
4384    suitable for passing or returning in VFP registers for the PCS
4385    variant selected.  If it is, then *BASE_MODE is updated to contain
4386    a machine mode describing each element of the argument's type and
4387    *COUNT to hold the number of such elements.  */
4388 static bool
4389 aapcs_vfp_is_call_or_return_candidate (enum arm_pcs pcs_variant,
4390                                        enum machine_mode mode, const_tree type,
4391                                        enum machine_mode *base_mode, int *count)
4392 {
4393   enum machine_mode new_mode = VOIDmode;
4394
4395   /* If we have the type information, prefer that to working things
4396      out from the mode.  */
4397   if (type)
4398     {
4399       int ag_count = aapcs_vfp_sub_candidate (type, &new_mode);
4400
4401       if (ag_count > 0 && ag_count <= 4)
4402         *count = ag_count;
4403       else
4404         return false;
4405     }
4406   else if (GET_MODE_CLASS (mode) == MODE_FLOAT
4407            || GET_MODE_CLASS (mode) == MODE_VECTOR_INT
4408            || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
4409     {
4410       *count = 1;
4411       new_mode = mode;
4412     }
4413   else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
4414     {
4415       *count = 2;
4416       new_mode = (mode == DCmode ? DFmode : SFmode);
4417     }
4418   else
4419     return false;
4420
4421
4422   if (!use_vfp_abi (pcs_variant, ARM_NUM_REGS (new_mode) > 1))
4423     return false;
4424
4425   *base_mode = new_mode;
4426   return true;
4427 }
4428
4429 static bool
4430 aapcs_vfp_is_return_candidate (enum arm_pcs pcs_variant,
4431                                enum machine_mode mode, const_tree type)
4432 {
4433   int count ATTRIBUTE_UNUSED;
4434   enum machine_mode ag_mode ATTRIBUTE_UNUSED;
4435
4436   if (!use_vfp_abi (pcs_variant, false))
4437     return false;
4438   return aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
4439                                                 &ag_mode, &count);
4440 }
4441
4442 static bool
4443 aapcs_vfp_is_call_candidate (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4444                              const_tree type)
4445 {
4446   if (!use_vfp_abi (pcum->pcs_variant, false))
4447     return false;
4448
4449   return aapcs_vfp_is_call_or_return_candidate (pcum->pcs_variant, mode, type,
4450                                                 &pcum->aapcs_vfp_rmode,
4451                                                 &pcum->aapcs_vfp_rcount);
4452 }
4453
4454 static bool
4455 aapcs_vfp_allocate (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4456                     const_tree type  ATTRIBUTE_UNUSED)
4457 {
4458   int shift = GET_MODE_SIZE (pcum->aapcs_vfp_rmode) / GET_MODE_SIZE (SFmode);
4459   unsigned mask = (1 << (shift * pcum->aapcs_vfp_rcount)) - 1;
4460   int regno;
4461
4462   for (regno = 0; regno < NUM_VFP_ARG_REGS; regno += shift)
4463     if (((pcum->aapcs_vfp_regs_free >> regno) & mask) == mask)
4464       {
4465         pcum->aapcs_vfp_reg_alloc = mask << regno;
4466         if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4467           {
4468             int i;
4469             int rcount = pcum->aapcs_vfp_rcount;
4470             int rshift = shift;
4471             enum machine_mode rmode = pcum->aapcs_vfp_rmode;
4472             rtx par;
4473             if (!TARGET_NEON)
4474               {
4475                 /* Avoid using unsupported vector modes.  */
4476                 if (rmode == V2SImode)
4477                   rmode = DImode;
4478                 else if (rmode == V4SImode)
4479                   {
4480                     rmode = DImode;
4481                     rcount *= 2;
4482                     rshift /= 2;
4483                   }
4484               }
4485             par = gen_rtx_PARALLEL (mode, rtvec_alloc (rcount));
4486             for (i = 0; i < rcount; i++)
4487               {
4488                 rtx tmp = gen_rtx_REG (rmode,
4489                                        FIRST_VFP_REGNUM + regno + i * rshift);
4490                 tmp = gen_rtx_EXPR_LIST
4491                   (VOIDmode, tmp,
4492                    GEN_INT (i * GET_MODE_SIZE (rmode)));
4493                 XVECEXP (par, 0, i) = tmp;
4494               }
4495
4496             pcum->aapcs_reg = par;
4497           }
4498         else
4499           pcum->aapcs_reg = gen_rtx_REG (mode, FIRST_VFP_REGNUM + regno);
4500         return true;
4501       }
4502   return false;
4503 }
4504
4505 static rtx
4506 aapcs_vfp_allocate_return_reg (enum arm_pcs pcs_variant ATTRIBUTE_UNUSED,
4507                                enum machine_mode mode,
4508                                const_tree type ATTRIBUTE_UNUSED)
4509 {
4510   if (!use_vfp_abi (pcs_variant, false))
4511     return NULL;
4512
4513   if (mode == BLKmode || (mode == TImode && !TARGET_NEON))
4514     {
4515       int count;
4516       enum machine_mode ag_mode;
4517       int i;
4518       rtx par;
4519       int shift;
4520
4521       aapcs_vfp_is_call_or_return_candidate (pcs_variant, mode, type,
4522                                              &ag_mode, &count);
4523
4524       if (!TARGET_NEON)
4525         {
4526           if (ag_mode == V2SImode)
4527             ag_mode = DImode;
4528           else if (ag_mode == V4SImode)
4529             {
4530               ag_mode = DImode;
4531               count *= 2;
4532             }
4533         }
4534       shift = GET_MODE_SIZE(ag_mode) / GET_MODE_SIZE(SFmode);
4535       par = gen_rtx_PARALLEL (mode, rtvec_alloc (count));
4536       for (i = 0; i < count; i++)
4537         {
4538           rtx tmp = gen_rtx_REG (ag_mode, FIRST_VFP_REGNUM + i * shift);
4539           tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp,
4540                                    GEN_INT (i * GET_MODE_SIZE (ag_mode)));
4541           XVECEXP (par, 0, i) = tmp;
4542         }
4543
4544       return par;
4545     }
4546
4547   return gen_rtx_REG (mode, FIRST_VFP_REGNUM);
4548 }
4549
4550 static void
4551 aapcs_vfp_advance (CUMULATIVE_ARGS *pcum  ATTRIBUTE_UNUSED,
4552                    enum machine_mode mode  ATTRIBUTE_UNUSED,
4553                    const_tree type  ATTRIBUTE_UNUSED)
4554 {
4555   pcum->aapcs_vfp_regs_free &= ~pcum->aapcs_vfp_reg_alloc;
4556   pcum->aapcs_vfp_reg_alloc = 0;
4557   return;
4558 }
4559
4560 #define AAPCS_CP(X)                             \
4561   {                                             \
4562     aapcs_ ## X ## _cum_init,                   \
4563     aapcs_ ## X ## _is_call_candidate,          \
4564     aapcs_ ## X ## _allocate,                   \
4565     aapcs_ ## X ## _is_return_candidate,        \
4566     aapcs_ ## X ## _allocate_return_reg,        \
4567     aapcs_ ## X ## _advance                     \
4568   }
4569
4570 /* Table of co-processors that can be used to pass arguments in
4571    registers.  Idealy no arugment should be a candidate for more than
4572    one co-processor table entry, but the table is processed in order
4573    and stops after the first match.  If that entry then fails to put
4574    the argument into a co-processor register, the argument will go on
4575    the stack.  */
4576 static struct
4577 {
4578   /* Initialize co-processor related state in CUMULATIVE_ARGS structure.  */
4579   void (*cum_init) (CUMULATIVE_ARGS *, const_tree, rtx, const_tree);
4580
4581   /* Return true if an argument of mode MODE (or type TYPE if MODE is
4582      BLKmode) is a candidate for this co-processor's registers; this
4583      function should ignore any position-dependent state in
4584      CUMULATIVE_ARGS and only use call-type dependent information.  */
4585   bool (*is_call_candidate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4586
4587   /* Return true if the argument does get a co-processor register; it
4588      should set aapcs_reg to an RTX of the register allocated as is
4589      required for a return from FUNCTION_ARG.  */
4590   bool (*allocate) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4591
4592   /* Return true if a result of mode MODE (or type TYPE if MODE is
4593      BLKmode) is can be returned in this co-processor's registers.  */
4594   bool (*is_return_candidate) (enum arm_pcs, enum machine_mode, const_tree);
4595
4596   /* Allocate and return an RTX element to hold the return type of a
4597      call, this routine must not fail and will only be called if
4598      is_return_candidate returned true with the same parameters.  */
4599   rtx (*allocate_return_reg) (enum arm_pcs, enum machine_mode, const_tree);
4600
4601   /* Finish processing this argument and prepare to start processing
4602      the next one.  */
4603   void (*advance) (CUMULATIVE_ARGS *, enum machine_mode, const_tree);
4604 } aapcs_cp_arg_layout[ARM_NUM_COPROC_SLOTS] =
4605   {
4606     AAPCS_CP(vfp)
4607   };
4608
4609 #undef AAPCS_CP
4610
4611 static int
4612 aapcs_select_call_coproc (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4613                           const_tree type)
4614 {
4615   int i;
4616
4617   for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4618     if (aapcs_cp_arg_layout[i].is_call_candidate (pcum, mode, type))
4619       return i;
4620
4621   return -1;
4622 }
4623
4624 static int
4625 aapcs_select_return_coproc (const_tree type, const_tree fntype)
4626 {
4627   /* We aren't passed a decl, so we can't check that a call is local.
4628      However, it isn't clear that that would be a win anyway, since it
4629      might limit some tail-calling opportunities.  */
4630   enum arm_pcs pcs_variant;
4631
4632   if (fntype)
4633     {
4634       const_tree fndecl = NULL_TREE;
4635
4636       if (TREE_CODE (fntype) == FUNCTION_DECL)
4637         {
4638           fndecl = fntype;
4639           fntype = TREE_TYPE (fntype);
4640         }
4641
4642       pcs_variant = arm_get_pcs_model (fntype, fndecl);
4643     }
4644   else
4645     pcs_variant = arm_pcs_default;
4646
4647   if (pcs_variant != ARM_PCS_AAPCS)
4648     {
4649       int i;
4650
4651       for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4652         if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant,
4653                                                         TYPE_MODE (type),
4654                                                         type))
4655           return i;
4656     }
4657   return -1;
4658 }
4659
4660 static rtx
4661 aapcs_allocate_return_reg (enum machine_mode mode, const_tree type,
4662                            const_tree fntype)
4663 {
4664   /* We aren't passed a decl, so we can't check that a call is local.
4665      However, it isn't clear that that would be a win anyway, since it
4666      might limit some tail-calling opportunities.  */
4667   enum arm_pcs pcs_variant;
4668   int unsignedp ATTRIBUTE_UNUSED;
4669
4670   if (fntype)
4671     {
4672       const_tree fndecl = NULL_TREE;
4673
4674       if (TREE_CODE (fntype) == FUNCTION_DECL)
4675         {
4676           fndecl = fntype;
4677           fntype = TREE_TYPE (fntype);
4678         }
4679
4680       pcs_variant = arm_get_pcs_model (fntype, fndecl);
4681     }
4682   else
4683     pcs_variant = arm_pcs_default;
4684
4685   /* Promote integer types.  */
4686   if (type && INTEGRAL_TYPE_P (type))
4687     mode = arm_promote_function_mode (type, mode, &unsignedp, fntype, 1);
4688
4689   if (pcs_variant != ARM_PCS_AAPCS)
4690     {
4691       int i;
4692
4693       for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4694         if (aapcs_cp_arg_layout[i].is_return_candidate (pcs_variant, mode,
4695                                                         type))
4696           return aapcs_cp_arg_layout[i].allocate_return_reg (pcs_variant,
4697                                                              mode, type);
4698     }
4699
4700   /* Promotes small structs returned in a register to full-word size
4701      for big-endian AAPCS.  */
4702   if (type && arm_return_in_msb (type))
4703     {
4704       HOST_WIDE_INT size = int_size_in_bytes (type);
4705       if (size % UNITS_PER_WORD != 0)
4706         {
4707           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
4708           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
4709         }
4710     }
4711
4712   return gen_rtx_REG (mode, R0_REGNUM);
4713 }
4714
4715 static rtx
4716 aapcs_libcall_value (enum machine_mode mode)
4717 {
4718   if (BYTES_BIG_ENDIAN && ALL_FIXED_POINT_MODE_P (mode)
4719       && GET_MODE_SIZE (mode) <= 4)
4720     mode = SImode;
4721
4722   return aapcs_allocate_return_reg (mode, NULL_TREE, NULL_TREE);
4723 }
4724
4725 /* Lay out a function argument using the AAPCS rules.  The rule
4726    numbers referred to here are those in the AAPCS.  */
4727 static void
4728 aapcs_layout_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
4729                   const_tree type, bool named)
4730 {
4731   int nregs, nregs2;
4732   int ncrn;
4733
4734   /* We only need to do this once per argument.  */
4735   if (pcum->aapcs_arg_processed)
4736     return;
4737
4738   pcum->aapcs_arg_processed = true;
4739
4740   /* Special case: if named is false then we are handling an incoming
4741      anonymous argument which is on the stack.  */
4742   if (!named)
4743     return;
4744
4745   /* Is this a potential co-processor register candidate?  */
4746   if (pcum->pcs_variant != ARM_PCS_AAPCS)
4747     {
4748       int slot = aapcs_select_call_coproc (pcum, mode, type);
4749       pcum->aapcs_cprc_slot = slot;
4750
4751       /* We don't have to apply any of the rules from part B of the
4752          preparation phase, these are handled elsewhere in the
4753          compiler.  */
4754
4755       if (slot >= 0)
4756         {
4757           /* A Co-processor register candidate goes either in its own
4758              class of registers or on the stack.  */
4759           if (!pcum->aapcs_cprc_failed[slot])
4760             {
4761               /* C1.cp - Try to allocate the argument to co-processor
4762                  registers.  */
4763               if (aapcs_cp_arg_layout[slot].allocate (pcum, mode, type))
4764                 return;
4765
4766               /* C2.cp - Put the argument on the stack and note that we
4767                  can't assign any more candidates in this slot.  We also
4768                  need to note that we have allocated stack space, so that
4769                  we won't later try to split a non-cprc candidate between
4770                  core registers and the stack.  */
4771               pcum->aapcs_cprc_failed[slot] = true;
4772               pcum->can_split = false;
4773             }
4774
4775           /* We didn't get a register, so this argument goes on the
4776              stack.  */
4777           gcc_assert (pcum->can_split == false);
4778           return;
4779         }
4780     }
4781
4782   /* C3 - For double-word aligned arguments, round the NCRN up to the
4783      next even number.  */
4784   ncrn = pcum->aapcs_ncrn;
4785   if ((ncrn & 1) && arm_needs_doubleword_align (mode, type))
4786     ncrn++;
4787
4788   nregs = ARM_NUM_REGS2(mode, type);
4789
4790   /* Sigh, this test should really assert that nregs > 0, but a GCC
4791      extension allows empty structs and then gives them empty size; it
4792      then allows such a structure to be passed by value.  For some of
4793      the code below we have to pretend that such an argument has
4794      non-zero size so that we 'locate' it correctly either in
4795      registers or on the stack.  */
4796   gcc_assert (nregs >= 0);
4797
4798   nregs2 = nregs ? nregs : 1;
4799
4800   /* C4 - Argument fits entirely in core registers.  */
4801   if (ncrn + nregs2 <= NUM_ARG_REGS)
4802     {
4803       pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4804       pcum->aapcs_next_ncrn = ncrn + nregs;
4805       return;
4806     }
4807
4808   /* C5 - Some core registers left and there are no arguments already
4809      on the stack: split this argument between the remaining core
4810      registers and the stack.  */
4811   if (ncrn < NUM_ARG_REGS && pcum->can_split)
4812     {
4813       pcum->aapcs_reg = gen_rtx_REG (mode, ncrn);
4814       pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4815       pcum->aapcs_partial = (NUM_ARG_REGS - ncrn) * UNITS_PER_WORD;
4816       return;
4817     }
4818
4819   /* C6 - NCRN is set to 4.  */
4820   pcum->aapcs_next_ncrn = NUM_ARG_REGS;
4821
4822   /* C7,C8 - arugment goes on the stack.  We have nothing to do here.  */
4823   return;
4824 }
4825
4826 /* Initialize a variable CUM of type CUMULATIVE_ARGS
4827    for a call to a function whose data type is FNTYPE.
4828    For a library call, FNTYPE is NULL.  */
4829 void
4830 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
4831                           rtx libname,
4832                           tree fndecl ATTRIBUTE_UNUSED)
4833 {
4834   /* Long call handling.  */
4835   if (fntype)
4836     pcum->pcs_variant = arm_get_pcs_model (fntype, fndecl);
4837   else
4838     pcum->pcs_variant = arm_pcs_default;
4839
4840   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4841     {
4842       if (arm_libcall_uses_aapcs_base (libname))
4843         pcum->pcs_variant = ARM_PCS_AAPCS;
4844
4845       pcum->aapcs_ncrn = pcum->aapcs_next_ncrn = 0;
4846       pcum->aapcs_reg = NULL_RTX;
4847       pcum->aapcs_partial = 0;
4848       pcum->aapcs_arg_processed = false;
4849       pcum->aapcs_cprc_slot = -1;
4850       pcum->can_split = true;
4851
4852       if (pcum->pcs_variant != ARM_PCS_AAPCS)
4853         {
4854           int i;
4855
4856           for (i = 0; i < ARM_NUM_COPROC_SLOTS; i++)
4857             {
4858               pcum->aapcs_cprc_failed[i] = false;
4859               aapcs_cp_arg_layout[i].cum_init (pcum, fntype, libname, fndecl);
4860             }
4861         }
4862       return;
4863     }
4864
4865   /* Legacy ABIs */
4866
4867   /* On the ARM, the offset starts at 0.  */
4868   pcum->nregs = 0;
4869   pcum->iwmmxt_nregs = 0;
4870   pcum->can_split = true;
4871
4872   /* Varargs vectors are treated the same as long long.
4873      named_count avoids having to change the way arm handles 'named' */
4874   pcum->named_count = 0;
4875   pcum->nargs = 0;
4876
4877   if (TARGET_REALLY_IWMMXT && fntype)
4878     {
4879       tree fn_arg;
4880
4881       for (fn_arg = TYPE_ARG_TYPES (fntype);
4882            fn_arg;
4883            fn_arg = TREE_CHAIN (fn_arg))
4884         pcum->named_count += 1;
4885
4886       if (! pcum->named_count)
4887         pcum->named_count = INT_MAX;
4888     }
4889 }
4890
4891
4892 /* Return true if mode/type need doubleword alignment.  */
4893 static bool
4894 arm_needs_doubleword_align (enum machine_mode mode, const_tree type)
4895 {
4896   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
4897           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
4898 }
4899
4900
4901 /* Determine where to put an argument to a function.
4902    Value is zero to push the argument on the stack,
4903    or a hard register in which to store the argument.
4904
4905    MODE is the argument's machine mode.
4906    TYPE is the data type of the argument (as a tree).
4907     This is null for libcalls where that information may
4908     not be available.
4909    CUM is a variable of type CUMULATIVE_ARGS which gives info about
4910     the preceding args and about the function being called.
4911    NAMED is nonzero if this argument is a named parameter
4912     (otherwise it is an extra parameter matching an ellipsis).
4913
4914    On the ARM, normally the first 16 bytes are passed in registers r0-r3; all
4915    other arguments are passed on the stack.  If (NAMED == 0) (which happens
4916    only in assign_parms, since TARGET_SETUP_INCOMING_VARARGS is
4917    defined), say it is passed in the stack (function_prologue will
4918    indeed make it pass in the stack if necessary).  */
4919
4920 static rtx
4921 arm_function_arg (cumulative_args_t pcum_v, enum machine_mode mode,
4922                   const_tree type, bool named)
4923 {
4924   CUMULATIVE_ARGS *pcum = get_cumulative_args (pcum_v);
4925   int nregs;
4926
4927   /* Handle the special case quickly.  Pick an arbitrary value for op2 of
4928      a call insn (op3 of a call_value insn).  */
4929   if (mode == VOIDmode)
4930     return const0_rtx;
4931
4932   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4933     {
4934       aapcs_layout_arg (pcum, mode, type, named);
4935       return pcum->aapcs_reg;
4936     }
4937
4938   /* Varargs vectors are treated the same as long long.
4939      named_count avoids having to change the way arm handles 'named' */
4940   if (TARGET_IWMMXT_ABI
4941       && arm_vector_mode_supported_p (mode)
4942       && pcum->named_count > pcum->nargs + 1)
4943     {
4944       if (pcum->iwmmxt_nregs <= 9)
4945         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
4946       else
4947         {
4948           pcum->can_split = false;
4949           return NULL_RTX;
4950         }
4951     }
4952
4953   /* Put doubleword aligned quantities in even register pairs.  */
4954   if (pcum->nregs & 1
4955       && ARM_DOUBLEWORD_ALIGN
4956       && arm_needs_doubleword_align (mode, type))
4957     pcum->nregs++;
4958
4959   /* Only allow splitting an arg between regs and memory if all preceding
4960      args were allocated to regs.  For args passed by reference we only count
4961      the reference pointer.  */
4962   if (pcum->can_split)
4963     nregs = 1;
4964   else
4965     nregs = ARM_NUM_REGS2 (mode, type);
4966
4967   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
4968     return NULL_RTX;
4969
4970   return gen_rtx_REG (mode, pcum->nregs);
4971 }
4972
4973 static unsigned int
4974 arm_function_arg_boundary (enum machine_mode mode, const_tree type)
4975 {
4976   return (ARM_DOUBLEWORD_ALIGN && arm_needs_doubleword_align (mode, type)
4977           ? DOUBLEWORD_ALIGNMENT
4978           : PARM_BOUNDARY);
4979 }
4980
4981 static int
4982 arm_arg_partial_bytes (cumulative_args_t pcum_v, enum machine_mode mode,
4983                        tree type, bool named)
4984 {
4985   CUMULATIVE_ARGS *pcum = get_cumulative_args (pcum_v);
4986   int nregs = pcum->nregs;
4987
4988   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
4989     {
4990       aapcs_layout_arg (pcum, mode, type, named);
4991       return pcum->aapcs_partial;
4992     }
4993
4994   if (TARGET_IWMMXT_ABI && arm_vector_mode_supported_p (mode))
4995     return 0;
4996
4997   if (NUM_ARG_REGS > nregs
4998       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
4999       && pcum->can_split)
5000     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
5001
5002   return 0;
5003 }
5004
5005 /* Update the data in PCUM to advance over an argument
5006    of mode MODE and data type TYPE.
5007    (TYPE is null for libcalls where that information may not be available.)  */
5008
5009 static void
5010 arm_function_arg_advance (cumulative_args_t pcum_v, enum machine_mode mode,
5011                           const_tree type, bool named)
5012 {
5013   CUMULATIVE_ARGS *pcum = get_cumulative_args (pcum_v);
5014
5015   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
5016     {
5017       aapcs_layout_arg (pcum, mode, type, named);
5018
5019       if (pcum->aapcs_cprc_slot >= 0)
5020         {
5021           aapcs_cp_arg_layout[pcum->aapcs_cprc_slot].advance (pcum, mode,
5022                                                               type);
5023           pcum->aapcs_cprc_slot = -1;
5024         }
5025
5026       /* Generic stuff.  */
5027       pcum->aapcs_arg_processed = false;
5028       pcum->aapcs_ncrn = pcum->aapcs_next_ncrn;
5029       pcum->aapcs_reg = NULL_RTX;
5030       pcum->aapcs_partial = 0;
5031     }
5032   else
5033     {
5034       pcum->nargs += 1;
5035       if (arm_vector_mode_supported_p (mode)
5036           && pcum->named_count > pcum->nargs
5037           && TARGET_IWMMXT_ABI)
5038         pcum->iwmmxt_nregs += 1;
5039       else
5040         pcum->nregs += ARM_NUM_REGS2 (mode, type);
5041     }
5042 }
5043
5044 /* Variable sized types are passed by reference.  This is a GCC
5045    extension to the ARM ABI.  */
5046
5047 static bool
5048 arm_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
5049                        enum machine_mode mode ATTRIBUTE_UNUSED,
5050                        const_tree type, bool named ATTRIBUTE_UNUSED)
5051 {
5052   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
5053 }
5054 \f
5055 /* Encode the current state of the #pragma [no_]long_calls.  */
5056 typedef enum
5057 {
5058   OFF,          /* No #pragma [no_]long_calls is in effect.  */
5059   LONG,         /* #pragma long_calls is in effect.  */
5060   SHORT         /* #pragma no_long_calls is in effect.  */
5061 } arm_pragma_enum;
5062
5063 static arm_pragma_enum arm_pragma_long_calls = OFF;
5064
5065 void
5066 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
5067 {
5068   arm_pragma_long_calls = LONG;
5069 }
5070
5071 void
5072 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
5073 {
5074   arm_pragma_long_calls = SHORT;
5075 }
5076
5077 void
5078 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
5079 {
5080   arm_pragma_long_calls = OFF;
5081 }
5082 \f
5083 /* Handle an attribute requiring a FUNCTION_DECL;
5084    arguments as in struct attribute_spec.handler.  */
5085 static tree
5086 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
5087                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
5088 {
5089   if (TREE_CODE (*node) != FUNCTION_DECL)
5090     {
5091       warning (OPT_Wattributes, "%qE attribute only applies to functions",
5092                name);
5093       *no_add_attrs = true;
5094     }
5095
5096   return NULL_TREE;
5097 }
5098
5099 /* Handle an "interrupt" or "isr" attribute;
5100    arguments as in struct attribute_spec.handler.  */
5101 static tree
5102 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
5103                           bool *no_add_attrs)
5104 {
5105   if (DECL_P (*node))
5106     {
5107       if (TREE_CODE (*node) != FUNCTION_DECL)
5108         {
5109           warning (OPT_Wattributes, "%qE attribute only applies to functions",
5110                    name);
5111           *no_add_attrs = true;
5112         }
5113       /* FIXME: the argument if any is checked for type attributes;
5114          should it be checked for decl ones?  */
5115     }
5116   else
5117     {
5118       if (TREE_CODE (*node) == FUNCTION_TYPE
5119           || TREE_CODE (*node) == METHOD_TYPE)
5120         {
5121           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
5122             {
5123               warning (OPT_Wattributes, "%qE attribute ignored",
5124                        name);
5125               *no_add_attrs = true;
5126             }
5127         }
5128       else if (TREE_CODE (*node) == POINTER_TYPE
5129                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
5130                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
5131                && arm_isr_value (args) != ARM_FT_UNKNOWN)
5132         {
5133           *node = build_variant_type_copy (*node);
5134           TREE_TYPE (*node) = build_type_attribute_variant
5135             (TREE_TYPE (*node),
5136              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
5137           *no_add_attrs = true;
5138         }
5139       else
5140         {
5141           /* Possibly pass this attribute on from the type to a decl.  */
5142           if (flags & ((int) ATTR_FLAG_DECL_NEXT
5143                        | (int) ATTR_FLAG_FUNCTION_NEXT
5144                        | (int) ATTR_FLAG_ARRAY_NEXT))
5145             {
5146               *no_add_attrs = true;
5147               return tree_cons (name, args, NULL_TREE);
5148             }
5149           else
5150             {
5151               warning (OPT_Wattributes, "%qE attribute ignored",
5152                        name);
5153             }
5154         }
5155     }
5156
5157   return NULL_TREE;
5158 }
5159
5160 /* Handle a "pcs" attribute; arguments as in struct
5161    attribute_spec.handler.  */
5162 static tree
5163 arm_handle_pcs_attribute (tree *node ATTRIBUTE_UNUSED, tree name, tree args,
5164                           int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
5165 {
5166   if (arm_pcs_from_attribute (args) == ARM_PCS_UNKNOWN)
5167     {
5168       warning (OPT_Wattributes, "%qE attribute ignored", name);
5169       *no_add_attrs = true;
5170     }
5171   return NULL_TREE;
5172 }
5173
5174 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5175 /* Handle the "notshared" attribute.  This attribute is another way of
5176    requesting hidden visibility.  ARM's compiler supports
5177    "__declspec(notshared)"; we support the same thing via an
5178    attribute.  */
5179
5180 static tree
5181 arm_handle_notshared_attribute (tree *node,
5182                                 tree name ATTRIBUTE_UNUSED,
5183                                 tree args ATTRIBUTE_UNUSED,
5184                                 int flags ATTRIBUTE_UNUSED,
5185                                 bool *no_add_attrs)
5186 {
5187   tree decl = TYPE_NAME (*node);
5188
5189   if (decl)
5190     {
5191       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
5192       DECL_VISIBILITY_SPECIFIED (decl) = 1;
5193       *no_add_attrs = false;
5194     }
5195   return NULL_TREE;
5196 }
5197 #endif
5198
5199 /* Return 0 if the attributes for two types are incompatible, 1 if they
5200    are compatible, and 2 if they are nearly compatible (which causes a
5201    warning to be generated).  */
5202 static int
5203 arm_comp_type_attributes (const_tree type1, const_tree type2)
5204 {
5205   int l1, l2, s1, s2;
5206
5207   /* Check for mismatch of non-default calling convention.  */
5208   if (TREE_CODE (type1) != FUNCTION_TYPE)
5209     return 1;
5210
5211   /* Check for mismatched call attributes.  */
5212   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
5213   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
5214   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
5215   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
5216
5217   /* Only bother to check if an attribute is defined.  */
5218   if (l1 | l2 | s1 | s2)
5219     {
5220       /* If one type has an attribute, the other must have the same attribute.  */
5221       if ((l1 != l2) || (s1 != s2))
5222         return 0;
5223
5224       /* Disallow mixed attributes.  */
5225       if ((l1 & s2) || (l2 & s1))
5226         return 0;
5227     }
5228
5229   /* Check for mismatched ISR attribute.  */
5230   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
5231   if (! l1)
5232     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
5233   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
5234   if (! l2)
5235     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
5236   if (l1 != l2)
5237     return 0;
5238
5239   return 1;
5240 }
5241
5242 /*  Assigns default attributes to newly defined type.  This is used to
5243     set short_call/long_call attributes for function types of
5244     functions defined inside corresponding #pragma scopes.  */
5245 static void
5246 arm_set_default_type_attributes (tree type)
5247 {
5248   /* Add __attribute__ ((long_call)) to all functions, when
5249      inside #pragma long_calls or __attribute__ ((short_call)),
5250      when inside #pragma no_long_calls.  */
5251   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
5252     {
5253       tree type_attr_list, attr_name;
5254       type_attr_list = TYPE_ATTRIBUTES (type);
5255
5256       if (arm_pragma_long_calls == LONG)
5257         attr_name = get_identifier ("long_call");
5258       else if (arm_pragma_long_calls == SHORT)
5259         attr_name = get_identifier ("short_call");
5260       else
5261         return;
5262
5263       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
5264       TYPE_ATTRIBUTES (type) = type_attr_list;
5265     }
5266 }
5267 \f
5268 /* Return true if DECL is known to be linked into section SECTION.  */
5269
5270 static bool
5271 arm_function_in_section_p (tree decl, section *section)
5272 {
5273   /* We can only be certain about functions defined in the same
5274      compilation unit.  */
5275   if (!TREE_STATIC (decl))
5276     return false;
5277
5278   /* Make sure that SYMBOL always binds to the definition in this
5279      compilation unit.  */
5280   if (!targetm.binds_local_p (decl))
5281     return false;
5282
5283   /* If DECL_SECTION_NAME is set, assume it is trustworthy.  */
5284   if (!DECL_SECTION_NAME (decl))
5285     {
5286       /* Make sure that we will not create a unique section for DECL.  */
5287       if (flag_function_sections || DECL_ONE_ONLY (decl))
5288         return false;
5289     }
5290
5291   return function_section (decl) == section;
5292 }
5293
5294 /* Return nonzero if a 32-bit "long_call" should be generated for
5295    a call from the current function to DECL.  We generate a long_call
5296    if the function:
5297
5298         a.  has an __attribute__((long call))
5299      or b.  is within the scope of a #pragma long_calls
5300      or c.  the -mlong-calls command line switch has been specified
5301
5302    However we do not generate a long call if the function:
5303
5304         d.  has an __attribute__ ((short_call))
5305      or e.  is inside the scope of a #pragma no_long_calls
5306      or f.  is defined in the same section as the current function.  */
5307
5308 bool
5309 arm_is_long_call_p (tree decl)
5310 {
5311   tree attrs;
5312
5313   if (!decl)
5314     return TARGET_LONG_CALLS;
5315
5316   attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
5317   if (lookup_attribute ("short_call", attrs))
5318     return false;
5319
5320   /* For "f", be conservative, and only cater for cases in which the
5321      whole of the current function is placed in the same section.  */
5322   if (!flag_reorder_blocks_and_partition
5323       && TREE_CODE (decl) == FUNCTION_DECL
5324       && arm_function_in_section_p (decl, current_function_section ()))
5325     return false;
5326
5327   if (lookup_attribute ("long_call", attrs))
5328     return true;
5329
5330   return TARGET_LONG_CALLS;
5331 }
5332
5333 /* Return nonzero if it is ok to make a tail-call to DECL.  */
5334 static bool
5335 arm_function_ok_for_sibcall (tree decl, tree exp)
5336 {
5337   unsigned long func_type;
5338
5339   if (cfun->machine->sibcall_blocked)
5340     return false;
5341
5342   /* Never tailcall something for which we have no decl, or if we
5343      are generating code for Thumb-1.  */
5344   if (decl == NULL || TARGET_THUMB1)
5345     return false;
5346
5347   /* The PIC register is live on entry to VxWorks PLT entries, so we
5348      must make the call before restoring the PIC register.  */
5349   if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
5350     return false;
5351
5352   /* Cannot tail-call to long calls, since these are out of range of
5353      a branch instruction.  */
5354   if (arm_is_long_call_p (decl))
5355     return false;
5356
5357   /* If we are interworking and the function is not declared static
5358      then we can't tail-call it unless we know that it exists in this
5359      compilation unit (since it might be a Thumb routine).  */
5360   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
5361     return false;
5362
5363   func_type = arm_current_func_type ();
5364   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
5365   if (IS_INTERRUPT (func_type))
5366     return false;
5367
5368   if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
5369     {
5370       /* Check that the return value locations are the same.  For
5371          example that we aren't returning a value from the sibling in
5372          a VFP register but then need to transfer it to a core
5373          register.  */
5374       rtx a, b;
5375
5376       a = arm_function_value (TREE_TYPE (exp), decl, false);
5377       b = arm_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
5378                               cfun->decl, false);
5379       if (!rtx_equal_p (a, b))
5380         return false;
5381     }
5382
5383   /* Never tailcall if function may be called with a misaligned SP.  */
5384   if (IS_STACKALIGN (func_type))
5385     return false;
5386
5387   /* The AAPCS says that, on bare-metal, calls to unresolved weak
5388      references should become a NOP.  Don't convert such calls into
5389      sibling calls.  */
5390   if (TARGET_AAPCS_BASED
5391       && arm_abi == ARM_ABI_AAPCS
5392       && DECL_WEAK (decl))
5393     return false;
5394
5395   /* Everything else is ok.  */
5396   return true;
5397 }
5398
5399 \f
5400 /* Addressing mode support functions.  */
5401
5402 /* Return nonzero if X is a legitimate immediate operand when compiling
5403    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
5404 int
5405 legitimate_pic_operand_p (rtx x)
5406 {
5407   if (GET_CODE (x) == SYMBOL_REF
5408       || (GET_CODE (x) == CONST
5409           && GET_CODE (XEXP (x, 0)) == PLUS
5410           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
5411     return 0;
5412
5413   return 1;
5414 }
5415
5416 /* Record that the current function needs a PIC register.  Initialize
5417    cfun->machine->pic_reg if we have not already done so.  */
5418
5419 static void
5420 require_pic_register (void)
5421 {
5422   /* A lot of the logic here is made obscure by the fact that this
5423      routine gets called as part of the rtx cost estimation process.
5424      We don't want those calls to affect any assumptions about the real
5425      function; and further, we can't call entry_of_function() until we
5426      start the real expansion process.  */
5427   if (!crtl->uses_pic_offset_table)
5428     {
5429       gcc_assert (can_create_pseudo_p ());
5430       if (arm_pic_register != INVALID_REGNUM)
5431         {
5432           if (!cfun->machine->pic_reg)
5433             cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
5434
5435           /* Play games to avoid marking the function as needing pic
5436              if we are being called as part of the cost-estimation
5437              process.  */
5438           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
5439             crtl->uses_pic_offset_table = 1;
5440         }
5441       else
5442         {
5443           rtx seq, insn;
5444
5445           if (!cfun->machine->pic_reg)
5446             cfun->machine->pic_reg = gen_reg_rtx (Pmode);
5447
5448           /* Play games to avoid marking the function as needing pic
5449              if we are being called as part of the cost-estimation
5450              process.  */
5451           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
5452             {
5453               crtl->uses_pic_offset_table = 1;
5454               start_sequence ();
5455
5456               arm_load_pic_register (0UL);
5457
5458               seq = get_insns ();
5459               end_sequence ();
5460
5461               for (insn = seq; insn; insn = NEXT_INSN (insn))
5462                 if (INSN_P (insn))
5463                   INSN_LOCATION (insn) = prologue_location;
5464
5465               /* We can be called during expansion of PHI nodes, where
5466                  we can't yet emit instructions directly in the final
5467                  insn stream.  Queue the insns on the entry edge, they will
5468                  be committed after everything else is expanded.  */
5469               insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
5470             }
5471         }
5472     }
5473 }
5474
5475 rtx
5476 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
5477 {
5478   if (GET_CODE (orig) == SYMBOL_REF
5479       || GET_CODE (orig) == LABEL_REF)
5480     {
5481       rtx insn;
5482
5483       if (reg == 0)
5484         {
5485           gcc_assert (can_create_pseudo_p ());
5486           reg = gen_reg_rtx (Pmode);
5487         }
5488
5489       /* VxWorks does not impose a fixed gap between segments; the run-time
5490          gap can be different from the object-file gap.  We therefore can't
5491          use GOTOFF unless we are absolutely sure that the symbol is in the
5492          same segment as the GOT.  Unfortunately, the flexibility of linker
5493          scripts means that we can't be sure of that in general, so assume
5494          that GOTOFF is never valid on VxWorks.  */
5495       if ((GET_CODE (orig) == LABEL_REF
5496            || (GET_CODE (orig) == SYMBOL_REF &&
5497                SYMBOL_REF_LOCAL_P (orig)))
5498           && NEED_GOT_RELOC
5499           && !TARGET_VXWORKS_RTP)
5500         insn = arm_pic_static_addr (orig, reg);
5501       else
5502         {
5503           rtx pat;
5504           rtx mem;
5505
5506           /* If this function doesn't have a pic register, create one now.  */
5507           require_pic_register ();
5508
5509           pat = gen_calculate_pic_address (reg, cfun->machine->pic_reg, orig);
5510
5511           /* Make the MEM as close to a constant as possible.  */
5512           mem = SET_SRC (pat);
5513           gcc_assert (MEM_P (mem) && !MEM_VOLATILE_P (mem));
5514           MEM_READONLY_P (mem) = 1;
5515           MEM_NOTRAP_P (mem) = 1;
5516
5517           insn = emit_insn (pat);
5518         }
5519
5520       /* Put a REG_EQUAL note on this insn, so that it can be optimized
5521          by loop.  */
5522       set_unique_reg_note (insn, REG_EQUAL, orig);
5523
5524       return reg;
5525     }
5526   else if (GET_CODE (orig) == CONST)
5527     {
5528       rtx base, offset;
5529
5530       if (GET_CODE (XEXP (orig, 0)) == PLUS
5531           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
5532         return orig;
5533
5534       /* Handle the case where we have: const (UNSPEC_TLS).  */
5535       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
5536           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
5537         return orig;
5538
5539       /* Handle the case where we have:
5540          const (plus (UNSPEC_TLS) (ADDEND)).  The ADDEND must be a
5541          CONST_INT.  */
5542       if (GET_CODE (XEXP (orig, 0)) == PLUS
5543           && GET_CODE (XEXP (XEXP (orig, 0), 0)) == UNSPEC
5544           && XINT (XEXP (XEXP (orig, 0), 0), 1) == UNSPEC_TLS)
5545         {
5546           gcc_assert (CONST_INT_P (XEXP (XEXP (orig, 0), 1)));
5547           return orig;
5548         }
5549
5550       if (reg == 0)
5551         {
5552           gcc_assert (can_create_pseudo_p ());
5553           reg = gen_reg_rtx (Pmode);
5554         }
5555
5556       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
5557
5558       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
5559       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
5560                                        base == reg ? 0 : reg);
5561
5562       if (CONST_INT_P (offset))
5563         {
5564           /* The base register doesn't really matter, we only want to
5565              test the index for the appropriate mode.  */
5566           if (!arm_legitimate_index_p (mode, offset, SET, 0))
5567             {
5568               gcc_assert (can_create_pseudo_p ());
5569               offset = force_reg (Pmode, offset);
5570             }
5571
5572           if (CONST_INT_P (offset))
5573             return plus_constant (Pmode, base, INTVAL (offset));
5574         }
5575
5576       if (GET_MODE_SIZE (mode) > 4
5577           && (GET_MODE_CLASS (mode) == MODE_INT
5578               || TARGET_SOFT_FLOAT))
5579         {
5580           emit_insn (gen_addsi3 (reg, base, offset));
5581           return reg;
5582         }
5583
5584       return gen_rtx_PLUS (Pmode, base, offset);
5585     }
5586
5587   return orig;
5588 }
5589
5590
5591 /* Find a spare register to use during the prolog of a function.  */
5592
5593 static int
5594 thumb_find_work_register (unsigned long pushed_regs_mask)
5595 {
5596   int reg;
5597
5598   /* Check the argument registers first as these are call-used.  The
5599      register allocation order means that sometimes r3 might be used
5600      but earlier argument registers might not, so check them all.  */
5601   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
5602     if (!df_regs_ever_live_p (reg))
5603       return reg;
5604
5605   /* Before going on to check the call-saved registers we can try a couple
5606      more ways of deducing that r3 is available.  The first is when we are
5607      pushing anonymous arguments onto the stack and we have less than 4
5608      registers worth of fixed arguments(*).  In this case r3 will be part of
5609      the variable argument list and so we can be sure that it will be
5610      pushed right at the start of the function.  Hence it will be available
5611      for the rest of the prologue.
5612      (*): ie crtl->args.pretend_args_size is greater than 0.  */
5613   if (cfun->machine->uses_anonymous_args
5614       && crtl->args.pretend_args_size > 0)
5615     return LAST_ARG_REGNUM;
5616
5617   /* The other case is when we have fixed arguments but less than 4 registers
5618      worth.  In this case r3 might be used in the body of the function, but
5619      it is not being used to convey an argument into the function.  In theory
5620      we could just check crtl->args.size to see how many bytes are
5621      being passed in argument registers, but it seems that it is unreliable.
5622      Sometimes it will have the value 0 when in fact arguments are being
5623      passed.  (See testcase execute/20021111-1.c for an example).  So we also
5624      check the args_info.nregs field as well.  The problem with this field is
5625      that it makes no allowances for arguments that are passed to the
5626      function but which are not used.  Hence we could miss an opportunity
5627      when a function has an unused argument in r3.  But it is better to be
5628      safe than to be sorry.  */
5629   if (! cfun->machine->uses_anonymous_args
5630       && crtl->args.size >= 0
5631       && crtl->args.size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
5632       && (TARGET_AAPCS_BASED
5633           ? crtl->args.info.aapcs_ncrn < 4
5634           : crtl->args.info.nregs < 4))
5635     return LAST_ARG_REGNUM;
5636
5637   /* Otherwise look for a call-saved register that is going to be pushed.  */
5638   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
5639     if (pushed_regs_mask & (1 << reg))
5640       return reg;
5641
5642   if (TARGET_THUMB2)
5643     {
5644       /* Thumb-2 can use high regs.  */
5645       for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
5646         if (pushed_regs_mask & (1 << reg))
5647           return reg;
5648     }
5649   /* Something went wrong - thumb_compute_save_reg_mask()
5650      should have arranged for a suitable register to be pushed.  */
5651   gcc_unreachable ();
5652 }
5653
5654 static GTY(()) int pic_labelno;
5655
5656 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
5657    low register.  */
5658
5659 void
5660 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
5661 {
5662   rtx l1, labelno, pic_tmp, pic_rtx, pic_reg;
5663
5664   if (crtl->uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
5665     return;
5666
5667   gcc_assert (flag_pic);
5668
5669   pic_reg = cfun->machine->pic_reg;
5670   if (TARGET_VXWORKS_RTP)
5671     {
5672       pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
5673       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
5674       emit_insn (gen_pic_load_addr_32bit (pic_reg, pic_rtx));
5675
5676       emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
5677
5678       pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
5679       emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
5680     }
5681   else
5682     {
5683       /* We use an UNSPEC rather than a LABEL_REF because this label
5684          never appears in the code stream.  */
5685
5686       labelno = GEN_INT (pic_labelno++);
5687       l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5688       l1 = gen_rtx_CONST (VOIDmode, l1);
5689
5690       /* On the ARM the PC register contains 'dot + 8' at the time of the
5691          addition, on the Thumb it is 'dot + 4'.  */
5692       pic_rtx = plus_constant (Pmode, l1, TARGET_ARM ? 8 : 4);
5693       pic_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, pic_rtx),
5694                                 UNSPEC_GOTSYM_OFF);
5695       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
5696
5697       if (TARGET_32BIT)
5698         {
5699           emit_insn (gen_pic_load_addr_unified (pic_reg, pic_rtx, labelno));
5700         }
5701       else /* TARGET_THUMB1 */
5702         {
5703           if (arm_pic_register != INVALID_REGNUM
5704               && REGNO (pic_reg) > LAST_LO_REGNUM)
5705             {
5706               /* We will have pushed the pic register, so we should always be
5707                  able to find a work register.  */
5708               pic_tmp = gen_rtx_REG (SImode,
5709                                      thumb_find_work_register (saved_regs));
5710               emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
5711               emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
5712               emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
5713             }
5714           else
5715             emit_insn (gen_pic_load_addr_unified (pic_reg, pic_rtx, labelno));
5716         }
5717     }
5718
5719   /* Need to emit this whether or not we obey regdecls,
5720      since setjmp/longjmp can cause life info to screw up.  */
5721   emit_use (pic_reg);
5722 }
5723
5724 /* Generate code to load the address of a static var when flag_pic is set.  */
5725 static rtx
5726 arm_pic_static_addr (rtx orig, rtx reg)
5727 {
5728   rtx l1, labelno, offset_rtx, insn;
5729
5730   gcc_assert (flag_pic);
5731
5732   /* We use an UNSPEC rather than a LABEL_REF because this label
5733      never appears in the code stream.  */
5734   labelno = GEN_INT (pic_labelno++);
5735   l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
5736   l1 = gen_rtx_CONST (VOIDmode, l1);
5737
5738   /* On the ARM the PC register contains 'dot + 8' at the time of the
5739      addition, on the Thumb it is 'dot + 4'.  */
5740   offset_rtx = plus_constant (Pmode, l1, TARGET_ARM ? 8 : 4);
5741   offset_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, orig, offset_rtx),
5742                                UNSPEC_SYMBOL_OFFSET);
5743   offset_rtx = gen_rtx_CONST (Pmode, offset_rtx);
5744
5745   insn = emit_insn (gen_pic_load_addr_unified (reg, offset_rtx, labelno));
5746   return insn;
5747 }
5748
5749 /* Return nonzero if X is valid as an ARM state addressing register.  */
5750 static int
5751 arm_address_register_rtx_p (rtx x, int strict_p)
5752 {
5753   int regno;
5754
5755   if (!REG_P (x))
5756     return 0;
5757
5758   regno = REGNO (x);
5759
5760   if (strict_p)
5761     return ARM_REGNO_OK_FOR_BASE_P (regno);
5762
5763   return (regno <= LAST_ARM_REGNUM
5764           || regno >= FIRST_PSEUDO_REGISTER
5765           || regno == FRAME_POINTER_REGNUM
5766           || regno == ARG_POINTER_REGNUM);
5767 }
5768
5769 /* Return TRUE if this rtx is the difference of a symbol and a label,
5770    and will reduce to a PC-relative relocation in the object file.
5771    Expressions like this can be left alone when generating PIC, rather
5772    than forced through the GOT.  */
5773 static int
5774 pcrel_constant_p (rtx x)
5775 {
5776   if (GET_CODE (x) == MINUS)
5777     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
5778
5779   return FALSE;
5780 }
5781
5782 /* Return true if X will surely end up in an index register after next
5783    splitting pass.  */
5784 static bool
5785 will_be_in_index_register (const_rtx x)
5786 {
5787   /* arm.md: calculate_pic_address will split this into a register.  */
5788   return GET_CODE (x) == UNSPEC && (XINT (x, 1) == UNSPEC_PIC_SYM);
5789 }
5790
5791 /* Return nonzero if X is a valid ARM state address operand.  */
5792 int
5793 arm_legitimate_address_outer_p (enum machine_mode mode, rtx x, RTX_CODE outer,
5794                                 int strict_p)
5795 {
5796   bool use_ldrd;
5797   enum rtx_code code = GET_CODE (x);
5798
5799   if (arm_address_register_rtx_p (x, strict_p))
5800     return 1;
5801
5802   use_ldrd = (TARGET_LDRD
5803               && (mode == DImode
5804                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5805
5806   if (code == POST_INC || code == PRE_DEC
5807       || ((code == PRE_INC || code == POST_DEC)
5808           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5809     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5810
5811   else if ((code == POST_MODIFY || code == PRE_MODIFY)
5812            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5813            && GET_CODE (XEXP (x, 1)) == PLUS
5814            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5815     {
5816       rtx addend = XEXP (XEXP (x, 1), 1);
5817
5818       /* Don't allow ldrd post increment by register because it's hard
5819          to fixup invalid register choices.  */
5820       if (use_ldrd
5821           && GET_CODE (x) == POST_MODIFY
5822           && REG_P (addend))
5823         return 0;
5824
5825       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
5826               && arm_legitimate_index_p (mode, addend, outer, strict_p));
5827     }
5828
5829   /* After reload constants split into minipools will have addresses
5830      from a LABEL_REF.  */
5831   else if (reload_completed
5832            && (code == LABEL_REF
5833                || (code == CONST
5834                    && GET_CODE (XEXP (x, 0)) == PLUS
5835                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5836                    && CONST_INT_P (XEXP (XEXP (x, 0), 1)))))
5837     return 1;
5838
5839   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5840     return 0;
5841
5842   else if (code == PLUS)
5843     {
5844       rtx xop0 = XEXP (x, 0);
5845       rtx xop1 = XEXP (x, 1);
5846
5847       return ((arm_address_register_rtx_p (xop0, strict_p)
5848                && ((CONST_INT_P (xop1)
5849                     && arm_legitimate_index_p (mode, xop1, outer, strict_p))
5850                    || (!strict_p && will_be_in_index_register (xop1))))
5851               || (arm_address_register_rtx_p (xop1, strict_p)
5852                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
5853     }
5854
5855 #if 0
5856   /* Reload currently can't handle MINUS, so disable this for now */
5857   else if (GET_CODE (x) == MINUS)
5858     {
5859       rtx xop0 = XEXP (x, 0);
5860       rtx xop1 = XEXP (x, 1);
5861
5862       return (arm_address_register_rtx_p (xop0, strict_p)
5863               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
5864     }
5865 #endif
5866
5867   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5868            && code == SYMBOL_REF
5869            && CONSTANT_POOL_ADDRESS_P (x)
5870            && ! (flag_pic
5871                  && symbol_mentioned_p (get_pool_constant (x))
5872                  && ! pcrel_constant_p (get_pool_constant (x))))
5873     return 1;
5874
5875   return 0;
5876 }
5877
5878 /* Return nonzero if X is a valid Thumb-2 address operand.  */
5879 static int
5880 thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
5881 {
5882   bool use_ldrd;
5883   enum rtx_code code = GET_CODE (x);
5884
5885   if (arm_address_register_rtx_p (x, strict_p))
5886     return 1;
5887
5888   use_ldrd = (TARGET_LDRD
5889               && (mode == DImode
5890                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
5891
5892   if (code == POST_INC || code == PRE_DEC
5893       || ((code == PRE_INC || code == POST_DEC)
5894           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
5895     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
5896
5897   else if ((code == POST_MODIFY || code == PRE_MODIFY)
5898            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
5899            && GET_CODE (XEXP (x, 1)) == PLUS
5900            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5901     {
5902       /* Thumb-2 only has autoincrement by constant.  */
5903       rtx addend = XEXP (XEXP (x, 1), 1);
5904       HOST_WIDE_INT offset;
5905
5906       if (!CONST_INT_P (addend))
5907         return 0;
5908
5909       offset = INTVAL(addend);
5910       if (GET_MODE_SIZE (mode) <= 4)
5911         return (offset > -256 && offset < 256);
5912
5913       return (use_ldrd && offset > -1024 && offset < 1024
5914               && (offset & 3) == 0);
5915     }
5916
5917   /* After reload constants split into minipools will have addresses
5918      from a LABEL_REF.  */
5919   else if (reload_completed
5920            && (code == LABEL_REF
5921                || (code == CONST
5922                    && GET_CODE (XEXP (x, 0)) == PLUS
5923                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5924                    && CONST_INT_P (XEXP (XEXP (x, 0), 1)))))
5925     return 1;
5926
5927   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
5928     return 0;
5929
5930   else if (code == PLUS)
5931     {
5932       rtx xop0 = XEXP (x, 0);
5933       rtx xop1 = XEXP (x, 1);
5934
5935       return ((arm_address_register_rtx_p (xop0, strict_p)
5936                && (thumb2_legitimate_index_p (mode, xop1, strict_p)
5937                    || (!strict_p && will_be_in_index_register (xop1))))
5938               || (arm_address_register_rtx_p (xop1, strict_p)
5939                   && thumb2_legitimate_index_p (mode, xop0, strict_p)));
5940     }
5941
5942   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
5943            && code == SYMBOL_REF
5944            && CONSTANT_POOL_ADDRESS_P (x)
5945            && ! (flag_pic
5946                  && symbol_mentioned_p (get_pool_constant (x))
5947                  && ! pcrel_constant_p (get_pool_constant (x))))
5948     return 1;
5949
5950   return 0;
5951 }
5952
5953 /* Return nonzero if INDEX is valid for an address index operand in
5954    ARM state.  */
5955 static int
5956 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
5957                         int strict_p)
5958 {
5959   HOST_WIDE_INT range;
5960   enum rtx_code code = GET_CODE (index);
5961
5962   /* Standard coprocessor addressing modes.  */
5963   if (TARGET_HARD_FLOAT
5964       && TARGET_VFP
5965       && (mode == SFmode || mode == DFmode))
5966     return (code == CONST_INT && INTVAL (index) < 1024
5967             && INTVAL (index) > -1024
5968             && (INTVAL (index) & 3) == 0);
5969
5970   /* For quad modes, we restrict the constant offset to be slightly less
5971      than what the instruction format permits.  We do this because for
5972      quad mode moves, we will actually decompose them into two separate
5973      double-mode reads or writes.  INDEX must therefore be a valid
5974      (double-mode) offset and so should INDEX+8.  */
5975   if (TARGET_NEON && VALID_NEON_QREG_MODE (mode))
5976     return (code == CONST_INT
5977             && INTVAL (index) < 1016
5978             && INTVAL (index) > -1024
5979             && (INTVAL (index) & 3) == 0);
5980
5981   /* We have no such constraint on double mode offsets, so we permit the
5982      full range of the instruction format.  */
5983   if (TARGET_NEON && VALID_NEON_DREG_MODE (mode))
5984     return (code == CONST_INT
5985             && INTVAL (index) < 1024
5986             && INTVAL (index) > -1024
5987             && (INTVAL (index) & 3) == 0);
5988
5989   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
5990     return (code == CONST_INT
5991             && INTVAL (index) < 1024
5992             && INTVAL (index) > -1024
5993             && (INTVAL (index) & 3) == 0);
5994
5995   if (arm_address_register_rtx_p (index, strict_p)
5996       && (GET_MODE_SIZE (mode) <= 4))
5997     return 1;
5998
5999   if (mode == DImode || mode == DFmode)
6000     {
6001       if (code == CONST_INT)
6002         {
6003           HOST_WIDE_INT val = INTVAL (index);
6004
6005           if (TARGET_LDRD)
6006             return val > -256 && val < 256;
6007           else
6008             return val > -4096 && val < 4092;
6009         }
6010
6011       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
6012     }
6013
6014   if (GET_MODE_SIZE (mode) <= 4
6015       && ! (arm_arch4
6016             && (mode == HImode
6017                 || mode == HFmode
6018                 || (mode == QImode && outer == SIGN_EXTEND))))
6019     {
6020       if (code == MULT)
6021         {
6022           rtx xiop0 = XEXP (index, 0);
6023           rtx xiop1 = XEXP (index, 1);
6024
6025           return ((arm_address_register_rtx_p (xiop0, strict_p)
6026                    && power_of_two_operand (xiop1, SImode))
6027                   || (arm_address_register_rtx_p (xiop1, strict_p)
6028                       && power_of_two_operand (xiop0, SImode)));
6029         }
6030       else if (code == LSHIFTRT || code == ASHIFTRT
6031                || code == ASHIFT || code == ROTATERT)
6032         {
6033           rtx op = XEXP (index, 1);
6034
6035           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
6036                   && CONST_INT_P (op)
6037                   && INTVAL (op) > 0
6038                   && INTVAL (op) <= 31);
6039         }
6040     }
6041
6042   /* For ARM v4 we may be doing a sign-extend operation during the
6043      load.  */
6044   if (arm_arch4)
6045     {
6046       if (mode == HImode
6047           || mode == HFmode
6048           || (outer == SIGN_EXTEND && mode == QImode))
6049         range = 256;
6050       else
6051         range = 4096;
6052     }
6053   else
6054     range = (mode == HImode || mode == HFmode) ? 4095 : 4096;
6055
6056   return (code == CONST_INT
6057           && INTVAL (index) < range
6058           && INTVAL (index) > -range);
6059 }
6060
6061 /* Return true if OP is a valid index scaling factor for Thumb-2 address
6062    index operand.  i.e. 1, 2, 4 or 8.  */
6063 static bool
6064 thumb2_index_mul_operand (rtx op)
6065 {
6066   HOST_WIDE_INT val;
6067
6068   if (!CONST_INT_P (op))
6069     return false;
6070
6071   val = INTVAL(op);
6072   return (val == 1 || val == 2 || val == 4 || val == 8);
6073 }
6074
6075 /* Return nonzero if INDEX is a valid Thumb-2 address index operand.  */
6076 static int
6077 thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
6078 {
6079   enum rtx_code code = GET_CODE (index);
6080
6081   /* ??? Combine arm and thumb2 coprocessor addressing modes.  */
6082   /* Standard coprocessor addressing modes.  */
6083   if (TARGET_HARD_FLOAT
6084       && TARGET_VFP
6085       && (mode == SFmode || mode == DFmode))
6086     return (code == CONST_INT && INTVAL (index) < 1024
6087             /* Thumb-2 allows only > -256 index range for it's core register
6088                load/stores. Since we allow SF/DF in core registers, we have
6089                to use the intersection between -256~4096 (core) and -1024~1024
6090                (coprocessor).  */
6091             && INTVAL (index) > -256
6092             && (INTVAL (index) & 3) == 0);
6093
6094   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
6095     {
6096       /* For DImode assume values will usually live in core regs
6097          and only allow LDRD addressing modes.  */
6098       if (!TARGET_LDRD || mode != DImode)
6099         return (code == CONST_INT
6100                 && INTVAL (index) < 1024
6101                 && INTVAL (index) > -1024
6102                 && (INTVAL (index) & 3) == 0);
6103     }
6104
6105   /* For quad modes, we restrict the constant offset to be slightly less
6106      than what the instruction format permits.  We do this because for
6107      quad mode moves, we will actually decompose them into two separate
6108      double-mode reads or writes.  INDEX must therefore be a valid
6109      (double-mode) offset and so should INDEX+8.  */
6110   if (TARGET_NEON && VALID_NEON_QREG_MODE (mode))
6111     return (code == CONST_INT
6112             && INTVAL (index) < 1016
6113             && INTVAL (index) > -1024
6114             && (INTVAL (index) & 3) == 0);
6115
6116   /* We have no such constraint on double mode offsets, so we permit the
6117      full range of the instruction format.  */
6118   if (TARGET_NEON && VALID_NEON_DREG_MODE (mode))
6119     return (code == CONST_INT
6120             && INTVAL (index) < 1024
6121             && INTVAL (index) > -1024
6122             && (INTVAL (index) & 3) == 0);
6123
6124   if (arm_address_register_rtx_p (index, strict_p)
6125       && (GET_MODE_SIZE (mode) <= 4))
6126     return 1;
6127
6128   if (mode == DImode || mode == DFmode)
6129     {
6130       if (code == CONST_INT)
6131         {
6132           HOST_WIDE_INT val = INTVAL (index);
6133           /* ??? Can we assume ldrd for thumb2?  */
6134           /* Thumb-2 ldrd only has reg+const addressing modes.  */
6135           /* ldrd supports offsets of +-1020.
6136              However the ldr fallback does not.  */
6137           return val > -256 && val < 256 && (val & 3) == 0;
6138         }
6139       else
6140         return 0;
6141     }
6142
6143   if (code == MULT)
6144     {
6145       rtx xiop0 = XEXP (index, 0);
6146       rtx xiop1 = XEXP (index, 1);
6147
6148       return ((arm_address_register_rtx_p (xiop0, strict_p)
6149                && thumb2_index_mul_operand (xiop1))
6150               || (arm_address_register_rtx_p (xiop1, strict_p)
6151                   && thumb2_index_mul_operand (xiop0)));
6152     }
6153   else if (code == ASHIFT)
6154     {
6155       rtx op = XEXP (index, 1);
6156
6157       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
6158               && CONST_INT_P (op)
6159               && INTVAL (op) > 0
6160               && INTVAL (op) <= 3);
6161     }
6162
6163   return (code == CONST_INT
6164           && INTVAL (index) < 4096
6165           && INTVAL (index) > -256);
6166 }
6167
6168 /* Return nonzero if X is valid as a 16-bit Thumb state base register.  */
6169 static int
6170 thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
6171 {
6172   int regno;
6173
6174   if (!REG_P (x))
6175     return 0;
6176
6177   regno = REGNO (x);
6178
6179   if (strict_p)
6180     return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
6181
6182   return (regno <= LAST_LO_REGNUM
6183           || regno > LAST_VIRTUAL_REGISTER
6184           || regno == FRAME_POINTER_REGNUM
6185           || (GET_MODE_SIZE (mode) >= 4
6186               && (regno == STACK_POINTER_REGNUM
6187                   || regno >= FIRST_PSEUDO_REGISTER
6188                   || x == hard_frame_pointer_rtx
6189                   || x == arg_pointer_rtx)));
6190 }
6191
6192 /* Return nonzero if x is a legitimate index register.  This is the case
6193    for any base register that can access a QImode object.  */
6194 inline static int
6195 thumb1_index_register_rtx_p (rtx x, int strict_p)
6196 {
6197   return thumb1_base_register_rtx_p (x, QImode, strict_p);
6198 }
6199
6200 /* Return nonzero if x is a legitimate 16-bit Thumb-state address.
6201
6202    The AP may be eliminated to either the SP or the FP, so we use the
6203    least common denominator, e.g. SImode, and offsets from 0 to 64.
6204
6205    ??? Verify whether the above is the right approach.
6206
6207    ??? Also, the FP may be eliminated to the SP, so perhaps that
6208    needs special handling also.
6209
6210    ??? Look at how the mips16 port solves this problem.  It probably uses
6211    better ways to solve some of these problems.
6212
6213    Although it is not incorrect, we don't accept QImode and HImode
6214    addresses based on the frame pointer or arg pointer until the
6215    reload pass starts.  This is so that eliminating such addresses
6216    into stack based ones won't produce impossible code.  */
6217 int
6218 thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
6219 {
6220   /* ??? Not clear if this is right.  Experiment.  */
6221   if (GET_MODE_SIZE (mode) < 4
6222       && !(reload_in_progress || reload_completed)
6223       && (reg_mentioned_p (frame_pointer_rtx, x)
6224           || reg_mentioned_p (arg_pointer_rtx, x)
6225           || reg_mentioned_p (virtual_incoming_args_rtx, x)
6226           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
6227           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
6228           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
6229     return 0;
6230
6231   /* Accept any base register.  SP only in SImode or larger.  */
6232   else if (thumb1_base_register_rtx_p (x, mode, strict_p))
6233     return 1;
6234
6235   /* This is PC relative data before arm_reorg runs.  */
6236   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
6237            && GET_CODE (x) == SYMBOL_REF
6238            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
6239     return 1;
6240
6241   /* This is PC relative data after arm_reorg runs.  */
6242   else if ((GET_MODE_SIZE (mode) >= 4 || mode == HFmode)
6243            && reload_completed
6244            && (GET_CODE (x) == LABEL_REF
6245                || (GET_CODE (x) == CONST
6246                    && GET_CODE (XEXP (x, 0)) == PLUS
6247                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
6248                    && CONST_INT_P (XEXP (XEXP (x, 0), 1)))))
6249     return 1;
6250
6251   /* Post-inc indexing only supported for SImode and larger.  */
6252   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
6253            && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
6254     return 1;
6255
6256   else if (GET_CODE (x) == PLUS)
6257     {
6258       /* REG+REG address can be any two index registers.  */
6259       /* We disallow FRAME+REG addressing since we know that FRAME
6260          will be replaced with STACK, and SP relative addressing only
6261          permits SP+OFFSET.  */
6262       if (GET_MODE_SIZE (mode) <= 4
6263           && XEXP (x, 0) != frame_pointer_rtx
6264           && XEXP (x, 1) != frame_pointer_rtx
6265           && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
6266           && (thumb1_index_register_rtx_p (XEXP (x, 1), strict_p)
6267               || (!strict_p && will_be_in_index_register (XEXP (x, 1)))))
6268         return 1;
6269
6270       /* REG+const has 5-7 bit offset for non-SP registers.  */
6271       else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
6272                 || XEXP (x, 0) == arg_pointer_rtx)
6273                && CONST_INT_P (XEXP (x, 1))
6274                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
6275         return 1;
6276
6277       /* REG+const has 10-bit offset for SP, but only SImode and
6278          larger is supported.  */
6279       /* ??? Should probably check for DI/DFmode overflow here
6280          just like GO_IF_LEGITIMATE_OFFSET does.  */
6281       else if (REG_P (XEXP (x, 0))
6282                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
6283                && GET_MODE_SIZE (mode) >= 4
6284                && CONST_INT_P (XEXP (x, 1))
6285                && INTVAL (XEXP (x, 1)) >= 0
6286                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
6287                && (INTVAL (XEXP (x, 1)) & 3) == 0)
6288         return 1;
6289
6290       else if (REG_P (XEXP (x, 0))
6291                && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
6292                    || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
6293                    || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
6294                        && REGNO (XEXP (x, 0))
6295                           <= LAST_VIRTUAL_POINTER_REGISTER))
6296                && GET_MODE_SIZE (mode) >= 4
6297                && CONST_INT_P (XEXP (x, 1))
6298                && (INTVAL (XEXP (x, 1)) & 3) == 0)
6299         return 1;
6300     }
6301
6302   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
6303            && GET_MODE_SIZE (mode) == 4
6304            && GET_CODE (x) == SYMBOL_REF
6305            && CONSTANT_POOL_ADDRESS_P (x)
6306            && ! (flag_pic
6307                  && symbol_mentioned_p (get_pool_constant (x))
6308                  && ! pcrel_constant_p (get_pool_constant (x))))
6309     return 1;
6310
6311   return 0;
6312 }
6313
6314 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
6315    instruction of mode MODE.  */
6316 int
6317 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
6318 {
6319   switch (GET_MODE_SIZE (mode))
6320     {
6321     case 1:
6322       return val >= 0 && val < 32;
6323
6324     case 2:
6325       return val >= 0 && val < 64 && (val & 1) == 0;
6326
6327     default:
6328       return (val >= 0
6329               && (val + GET_MODE_SIZE (mode)) <= 128
6330               && (val & 3) == 0);
6331     }
6332 }
6333
6334 bool
6335 arm_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
6336 {
6337   if (TARGET_ARM)
6338     return arm_legitimate_address_outer_p (mode, x, SET, strict_p);
6339   else if (TARGET_THUMB2)
6340     return thumb2_legitimate_address_p (mode, x, strict_p);
6341   else /* if (TARGET_THUMB1) */
6342     return thumb1_legitimate_address_p (mode, x, strict_p);
6343 }
6344
6345 /* Worker function for TARGET_PREFERRED_RELOAD_CLASS.
6346
6347    Given an rtx X being reloaded into a reg required to be
6348    in class CLASS, return the class of reg to actually use.
6349    In general this is just CLASS, but for the Thumb core registers and
6350    immediate constants we prefer a LO_REGS class or a subset.  */
6351
6352 static reg_class_t
6353 arm_preferred_reload_class (rtx x ATTRIBUTE_UNUSED, reg_class_t rclass)
6354 {
6355   if (TARGET_32BIT)
6356     return rclass;
6357   else
6358     {
6359       if (rclass == GENERAL_REGS
6360           || rclass == HI_REGS
6361           || rclass == NO_REGS
6362           || rclass == STACK_REG)
6363         return LO_REGS;
6364       else
6365         return rclass;
6366     }
6367 }
6368
6369 /* Build the SYMBOL_REF for __tls_get_addr.  */
6370
6371 static GTY(()) rtx tls_get_addr_libfunc;
6372
6373 static rtx
6374 get_tls_get_addr (void)
6375 {
6376   if (!tls_get_addr_libfunc)
6377     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
6378   return tls_get_addr_libfunc;
6379 }
6380
6381 rtx
6382 arm_load_tp (rtx target)
6383 {
6384   if (!target)
6385     target = gen_reg_rtx (SImode);
6386
6387   if (TARGET_HARD_TP)
6388     {
6389       /* Can return in any reg.  */
6390       emit_insn (gen_load_tp_hard (target));
6391     }
6392   else
6393     {
6394       /* Always returned in r0.  Immediately copy the result into a pseudo,
6395          otherwise other uses of r0 (e.g. setting up function arguments) may
6396          clobber the value.  */
6397
6398       rtx tmp;
6399
6400       emit_insn (gen_load_tp_soft ());
6401
6402       tmp = gen_rtx_REG (SImode, 0);
6403       emit_move_insn (target, tmp);
6404     }
6405   return target;
6406 }
6407
6408 static rtx
6409 load_tls_operand (rtx x, rtx reg)
6410 {
6411   rtx tmp;
6412
6413   if (reg == NULL_RTX)
6414     reg = gen_reg_rtx (SImode);
6415
6416   tmp = gen_rtx_CONST (SImode, x);
6417
6418   emit_move_insn (reg, tmp);
6419
6420   return reg;
6421 }
6422
6423 static rtx
6424 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
6425 {
6426   rtx insns, label, labelno, sum;
6427
6428   gcc_assert (reloc != TLS_DESCSEQ);
6429   start_sequence ();
6430
6431   labelno = GEN_INT (pic_labelno++);
6432   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
6433   label = gen_rtx_CONST (VOIDmode, label);
6434
6435   sum = gen_rtx_UNSPEC (Pmode,
6436                         gen_rtvec (4, x, GEN_INT (reloc), label,
6437                                    GEN_INT (TARGET_ARM ? 8 : 4)),
6438                         UNSPEC_TLS);
6439   reg = load_tls_operand (sum, reg);
6440
6441   if (TARGET_ARM)
6442     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
6443   else
6444     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
6445
6446   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX,
6447                                      LCT_PURE, /* LCT_CONST?  */
6448                                      Pmode, 1, reg, Pmode);
6449
6450   insns = get_insns ();
6451   end_sequence ();
6452
6453   return insns;
6454 }
6455
6456 static rtx
6457 arm_tls_descseq_addr (rtx x, rtx reg)
6458 {
6459   rtx labelno = GEN_INT (pic_labelno++);
6460   rtx label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
6461   rtx sum = gen_rtx_UNSPEC (Pmode,
6462                             gen_rtvec (4, x, GEN_INT (TLS_DESCSEQ),
6463                                        gen_rtx_CONST (VOIDmode, label),
6464                                        GEN_INT (!TARGET_ARM)),
6465                             UNSPEC_TLS);
6466   rtx reg0 = load_tls_operand (sum, gen_rtx_REG (SImode, 0));
6467
6468   emit_insn (gen_tlscall (x, labelno));
6469   if (!reg)
6470     reg = gen_reg_rtx (SImode);
6471   else
6472     gcc_assert (REGNO (reg) != 0);
6473
6474   emit_move_insn (reg, reg0);
6475
6476   return reg;
6477 }
6478
6479 rtx
6480 legitimize_tls_address (rtx x, rtx reg)
6481 {
6482   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
6483   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
6484
6485   switch (model)
6486     {
6487     case TLS_MODEL_GLOBAL_DYNAMIC:
6488       if (TARGET_GNU2_TLS)
6489         {
6490           reg = arm_tls_descseq_addr (x, reg);
6491
6492           tp = arm_load_tp (NULL_RTX);
6493
6494           dest = gen_rtx_PLUS (Pmode, tp, reg);
6495         }
6496       else
6497         {
6498           /* Original scheme */
6499           insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
6500           dest = gen_reg_rtx (Pmode);
6501           emit_libcall_block (insns, dest, ret, x);
6502         }
6503       return dest;
6504
6505     case TLS_MODEL_LOCAL_DYNAMIC:
6506       if (TARGET_GNU2_TLS)
6507         {
6508           reg = arm_tls_descseq_addr (x, reg);
6509
6510           tp = arm_load_tp (NULL_RTX);
6511
6512           dest = gen_rtx_PLUS (Pmode, tp, reg);
6513         }
6514       else
6515         {
6516           insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
6517
6518           /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
6519              share the LDM result with other LD model accesses.  */
6520           eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
6521                                 UNSPEC_TLS);
6522           dest = gen_reg_rtx (Pmode);
6523           emit_libcall_block (insns, dest, ret, eqv);
6524
6525           /* Load the addend.  */
6526           addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x,
6527                                                      GEN_INT (TLS_LDO32)),
6528                                    UNSPEC_TLS);
6529           addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
6530           dest = gen_rtx_PLUS (Pmode, dest, addend);
6531         }
6532       return dest;
6533
6534     case TLS_MODEL_INITIAL_EXEC:
6535       labelno = GEN_INT (pic_labelno++);
6536       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
6537       label = gen_rtx_CONST (VOIDmode, label);
6538       sum = gen_rtx_UNSPEC (Pmode,
6539                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
6540                                        GEN_INT (TARGET_ARM ? 8 : 4)),
6541                             UNSPEC_TLS);
6542       reg = load_tls_operand (sum, reg);
6543
6544       if (TARGET_ARM)
6545         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
6546       else if (TARGET_THUMB2)
6547         emit_insn (gen_tls_load_dot_plus_four (reg, NULL, reg, labelno));
6548       else
6549         {
6550           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
6551           emit_move_insn (reg, gen_const_mem (SImode, reg));
6552         }
6553
6554       tp = arm_load_tp (NULL_RTX);
6555
6556       return gen_rtx_PLUS (Pmode, tp, reg);
6557
6558     case TLS_MODEL_LOCAL_EXEC:
6559       tp = arm_load_tp (NULL_RTX);
6560
6561       reg = gen_rtx_UNSPEC (Pmode,
6562                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
6563                             UNSPEC_TLS);
6564       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
6565
6566       return gen_rtx_PLUS (Pmode, tp, reg);
6567
6568     default:
6569       abort ();
6570     }
6571 }
6572
6573 /* Try machine-dependent ways of modifying an illegitimate address
6574    to be legitimate.  If we find one, return the new, valid address.  */
6575 rtx
6576 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
6577 {
6578   if (!TARGET_ARM)
6579     {
6580       /* TODO: legitimize_address for Thumb2.  */
6581       if (TARGET_THUMB2)
6582         return x;
6583       return thumb_legitimize_address (x, orig_x, mode);
6584     }
6585
6586   if (arm_tls_symbol_p (x))
6587     return legitimize_tls_address (x, NULL_RTX);
6588
6589   if (GET_CODE (x) == PLUS)
6590     {
6591       rtx xop0 = XEXP (x, 0);
6592       rtx xop1 = XEXP (x, 1);
6593
6594       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
6595         xop0 = force_reg (SImode, xop0);
6596
6597       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
6598         xop1 = force_reg (SImode, xop1);
6599
6600       if (ARM_BASE_REGISTER_RTX_P (xop0)
6601           && CONST_INT_P (xop1))
6602         {
6603           HOST_WIDE_INT n, low_n;
6604           rtx base_reg, val;
6605           n = INTVAL (xop1);
6606
6607           /* VFP addressing modes actually allow greater offsets, but for
6608              now we just stick with the lowest common denominator.  */
6609           if (mode == DImode
6610               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
6611             {
6612               low_n = n & 0x0f;
6613               n &= ~0x0f;
6614               if (low_n > 4)
6615                 {
6616                   n += 16;
6617                   low_n -= 16;
6618                 }
6619             }
6620           else
6621             {
6622               low_n = ((mode) == TImode ? 0
6623                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
6624               n -= low_n;
6625             }
6626
6627           base_reg = gen_reg_rtx (SImode);
6628           val = force_operand (plus_constant (Pmode, xop0, n), NULL_RTX);
6629           emit_move_insn (base_reg, val);
6630           x = plus_constant (Pmode, base_reg, low_n);
6631         }
6632       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6633         x = gen_rtx_PLUS (SImode, xop0, xop1);
6634     }
6635
6636   /* XXX We don't allow MINUS any more -- see comment in
6637      arm_legitimate_address_outer_p ().  */
6638   else if (GET_CODE (x) == MINUS)
6639     {
6640       rtx xop0 = XEXP (x, 0);
6641       rtx xop1 = XEXP (x, 1);
6642
6643       if (CONSTANT_P (xop0))
6644         xop0 = force_reg (SImode, xop0);
6645
6646       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
6647         xop1 = force_reg (SImode, xop1);
6648
6649       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
6650         x = gen_rtx_MINUS (SImode, xop0, xop1);
6651     }
6652
6653   /* Make sure to take full advantage of the pre-indexed addressing mode
6654      with absolute addresses which often allows for the base register to
6655      be factorized for multiple adjacent memory references, and it might
6656      even allows for the mini pool to be avoided entirely. */
6657   else if (CONST_INT_P (x) && optimize > 0)
6658     {
6659       unsigned int bits;
6660       HOST_WIDE_INT mask, base, index;
6661       rtx base_reg;
6662
6663       /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
6664          use a 8-bit index. So let's use a 12-bit index for SImode only and
6665          hope that arm_gen_constant will enable ldrb to use more bits. */
6666       bits = (mode == SImode) ? 12 : 8;
6667       mask = (1 << bits) - 1;
6668       base = INTVAL (x) & ~mask;
6669       index = INTVAL (x) & mask;
6670       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
6671         {
6672           /* It'll most probably be more efficient to generate the base
6673              with more bits set and use a negative index instead. */
6674           base |= mask;
6675           index -= mask;
6676         }
6677       base_reg = force_reg (SImode, GEN_INT (base));
6678       x = plus_constant (Pmode, base_reg, index);
6679     }
6680
6681   if (flag_pic)
6682     {
6683       /* We need to find and carefully transform any SYMBOL and LABEL
6684          references; so go back to the original address expression.  */
6685       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6686
6687       if (new_x != orig_x)
6688         x = new_x;
6689     }
6690
6691   return x;
6692 }
6693
6694
6695 /* Try machine-dependent ways of modifying an illegitimate Thumb address
6696    to be legitimate.  If we find one, return the new, valid address.  */
6697 rtx
6698 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
6699 {
6700   if (arm_tls_symbol_p (x))
6701     return legitimize_tls_address (x, NULL_RTX);
6702
6703   if (GET_CODE (x) == PLUS
6704       && CONST_INT_P (XEXP (x, 1))
6705       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
6706           || INTVAL (XEXP (x, 1)) < 0))
6707     {
6708       rtx xop0 = XEXP (x, 0);
6709       rtx xop1 = XEXP (x, 1);
6710       HOST_WIDE_INT offset = INTVAL (xop1);
6711
6712       /* Try and fold the offset into a biasing of the base register and
6713          then offsetting that.  Don't do this when optimizing for space
6714          since it can cause too many CSEs.  */
6715       if (optimize_size && offset >= 0
6716           && offset < 256 + 31 * GET_MODE_SIZE (mode))
6717         {
6718           HOST_WIDE_INT delta;
6719
6720           if (offset >= 256)
6721             delta = offset - (256 - GET_MODE_SIZE (mode));
6722           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
6723             delta = 31 * GET_MODE_SIZE (mode);
6724           else
6725             delta = offset & (~31 * GET_MODE_SIZE (mode));
6726
6727           xop0 = force_operand (plus_constant (Pmode, xop0, offset - delta),
6728                                 NULL_RTX);
6729           x = plus_constant (Pmode, xop0, delta);
6730         }
6731       else if (offset < 0 && offset > -256)
6732         /* Small negative offsets are best done with a subtract before the
6733            dereference, forcing these into a register normally takes two
6734            instructions.  */
6735         x = force_operand (x, NULL_RTX);
6736       else
6737         {
6738           /* For the remaining cases, force the constant into a register.  */
6739           xop1 = force_reg (SImode, xop1);
6740           x = gen_rtx_PLUS (SImode, xop0, xop1);
6741         }
6742     }
6743   else if (GET_CODE (x) == PLUS
6744            && s_register_operand (XEXP (x, 1), SImode)
6745            && !s_register_operand (XEXP (x, 0), SImode))
6746     {
6747       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
6748
6749       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
6750     }
6751
6752   if (flag_pic)
6753     {
6754       /* We need to find and carefully transform any SYMBOL and LABEL
6755          references; so go back to the original address expression.  */
6756       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
6757
6758       if (new_x != orig_x)
6759         x = new_x;
6760     }
6761
6762   return x;
6763 }
6764
6765 bool
6766 arm_legitimize_reload_address (rtx *p,
6767                                enum machine_mode mode,
6768                                int opnum, int type,
6769                                int ind_levels ATTRIBUTE_UNUSED)
6770 {
6771   /* We must recognize output that we have already generated ourselves.  */
6772   if (GET_CODE (*p) == PLUS
6773       && GET_CODE (XEXP (*p, 0)) == PLUS
6774       && REG_P (XEXP (XEXP (*p, 0), 0))
6775       && CONST_INT_P (XEXP (XEXP (*p, 0), 1))
6776       && CONST_INT_P (XEXP (*p, 1)))
6777     {
6778       push_reload (XEXP (*p, 0), NULL_RTX, &XEXP (*p, 0), NULL,
6779                    MODE_BASE_REG_CLASS (mode), GET_MODE (*p),
6780                    VOIDmode, 0, 0, opnum, (enum reload_type) type);
6781       return true;
6782     }
6783
6784   if (GET_CODE (*p) == PLUS
6785       && REG_P (XEXP (*p, 0))
6786       && ARM_REGNO_OK_FOR_BASE_P (REGNO (XEXP (*p, 0)))
6787       /* If the base register is equivalent to a constant, let the generic
6788          code handle it.  Otherwise we will run into problems if a future
6789          reload pass decides to rematerialize the constant.  */
6790       && !reg_equiv_constant (ORIGINAL_REGNO (XEXP (*p, 0)))
6791       && CONST_INT_P (XEXP (*p, 1)))
6792     {
6793       HOST_WIDE_INT val = INTVAL (XEXP (*p, 1));
6794       HOST_WIDE_INT low, high;
6795
6796       /* Detect coprocessor load/stores.  */
6797       bool coproc_p = ((TARGET_HARD_FLOAT
6798                         && TARGET_VFP
6799                         && (mode == SFmode || mode == DFmode))
6800                        || (TARGET_REALLY_IWMMXT
6801                            && VALID_IWMMXT_REG_MODE (mode))
6802                        || (TARGET_NEON
6803                            && (VALID_NEON_DREG_MODE (mode)
6804                                || VALID_NEON_QREG_MODE (mode))));
6805
6806       /* For some conditions, bail out when lower two bits are unaligned.  */
6807       if ((val & 0x3) != 0
6808           /* Coprocessor load/store indexes are 8-bits + '00' appended.  */
6809           && (coproc_p
6810               /* For DI, and DF under soft-float: */
6811               || ((mode == DImode || mode == DFmode)
6812                   /* Without ldrd, we use stm/ldm, which does not
6813                      fair well with unaligned bits.  */
6814                   && (! TARGET_LDRD
6815                       /* Thumb-2 ldrd/strd is [-1020,+1020] in steps of 4.  */
6816                       || TARGET_THUMB2))))
6817         return false;
6818
6819       /* When breaking down a [reg+index] reload address into [(reg+high)+low],
6820          of which the (reg+high) gets turned into a reload add insn,
6821          we try to decompose the index into high/low values that can often
6822          also lead to better reload CSE.
6823          For example:
6824                  ldr r0, [r2, #4100]  // Offset too large
6825                  ldr r1, [r2, #4104]  // Offset too large
6826
6827          is best reloaded as:
6828                  add t1, r2, #4096
6829                  ldr r0, [t1, #4]
6830                  add t2, r2, #4096
6831                  ldr r1, [t2, #8]
6832
6833          which post-reload CSE can simplify in most cases to eliminate the
6834          second add instruction:
6835                  add t1, r2, #4096
6836                  ldr r0, [t1, #4]
6837                  ldr r1, [t1, #8]
6838
6839          The idea here is that we want to split out the bits of the constant
6840          as a mask, rather than as subtracting the maximum offset that the
6841          respective type of load/store used can handle.
6842
6843          When encountering negative offsets, we can still utilize it even if
6844          the overall offset is positive; sometimes this may lead to an immediate
6845          that can be constructed with fewer instructions.
6846          For example:
6847                  ldr r0, [r2, #0x3FFFFC]
6848
6849          This is best reloaded as:
6850                  add t1, r2, #0x400000
6851                  ldr r0, [t1, #-4]
6852
6853          The trick for spotting this for a load insn with N bits of offset
6854          (i.e. bits N-1:0) is to look at bit N; if it is set, then chose a
6855          negative offset that is going to make bit N and all the bits below
6856          it become zero in the remainder part.
6857
6858          The SIGN_MAG_LOW_ADDR_BITS macro below implements this, with respect
6859          to sign-magnitude addressing (i.e. separate +- bit, or 1's complement),
6860          used in most cases of ARM load/store instructions.  */
6861
6862 #define SIGN_MAG_LOW_ADDR_BITS(VAL, N)                                  \
6863       (((VAL) & ((1 << (N)) - 1))                                       \
6864        ? (((VAL) & ((1 << ((N) + 1)) - 1)) ^ (1 << (N))) - (1 << (N))   \
6865        : 0)
6866
6867       if (coproc_p)
6868         {
6869           low = SIGN_MAG_LOW_ADDR_BITS (val, 10);
6870
6871           /* NEON quad-word load/stores are made of two double-word accesses,
6872              so the valid index range is reduced by 8. Treat as 9-bit range if
6873              we go over it.  */
6874           if (TARGET_NEON && VALID_NEON_QREG_MODE (mode) && low >= 1016)
6875             low = SIGN_MAG_LOW_ADDR_BITS (val, 9);
6876         }
6877       else if (GET_MODE_SIZE (mode) == 8)
6878         {
6879           if (TARGET_LDRD)
6880             low = (TARGET_THUMB2
6881                    ? SIGN_MAG_LOW_ADDR_BITS (val, 10)
6882                    : SIGN_MAG_LOW_ADDR_BITS (val, 8));
6883           else
6884             /* For pre-ARMv5TE (without ldrd), we use ldm/stm(db/da/ib)
6885                to access doublewords. The supported load/store offsets are
6886                -8, -4, and 4, which we try to produce here.  */
6887             low = ((val & 0xf) ^ 0x8) - 0x8;
6888         }
6889       else if (GET_MODE_SIZE (mode) < 8)
6890         {
6891           /* NEON element load/stores do not have an offset.  */
6892           if (TARGET_NEON_FP16 && mode == HFmode)
6893             return false;
6894
6895           if (TARGET_THUMB2)
6896             {
6897               /* Thumb-2 has an asymmetrical index range of (-256,4096).
6898                  Try the wider 12-bit range first, and re-try if the result
6899                  is out of range.  */
6900               low = SIGN_MAG_LOW_ADDR_BITS (val, 12);
6901               if (low < -255)
6902                 low = SIGN_MAG_LOW_ADDR_BITS (val, 8);
6903             }
6904           else
6905             {
6906               if (mode == HImode || mode == HFmode)
6907                 {
6908                   if (arm_arch4)
6909                     low = SIGN_MAG_LOW_ADDR_BITS (val, 8);
6910                   else
6911                     {
6912                       /* The storehi/movhi_bytes fallbacks can use only
6913                          [-4094,+4094] of the full ldrb/strb index range.  */
6914                       low = SIGN_MAG_LOW_ADDR_BITS (val, 12);
6915                       if (low == 4095 || low == -4095)
6916                         return false;
6917                     }
6918                 }
6919               else
6920                 low = SIGN_MAG_LOW_ADDR_BITS (val, 12);
6921             }
6922         }
6923       else
6924         return false;
6925
6926       high = ((((val - low) & (unsigned HOST_WIDE_INT) 0xffffffff)
6927                ^ (unsigned HOST_WIDE_INT) 0x80000000)
6928               - (unsigned HOST_WIDE_INT) 0x80000000);
6929       /* Check for overflow or zero */
6930       if (low == 0 || high == 0 || (high + low != val))
6931         return false;
6932
6933       /* Reload the high part into a base reg; leave the low part
6934          in the mem.  */
6935       *p = gen_rtx_PLUS (GET_MODE (*p),
6936                          gen_rtx_PLUS (GET_MODE (*p), XEXP (*p, 0),
6937                                        GEN_INT (high)),
6938                          GEN_INT (low));
6939       push_reload (XEXP (*p, 0), NULL_RTX, &XEXP (*p, 0), NULL,
6940                    MODE_BASE_REG_CLASS (mode), GET_MODE (*p),
6941                    VOIDmode, 0, 0, opnum, (enum reload_type) type);
6942       return true;
6943     }
6944
6945   return false;
6946 }
6947
6948 rtx
6949 thumb_legitimize_reload_address (rtx *x_p,
6950                                  enum machine_mode mode,
6951                                  int opnum, int type,
6952                                  int ind_levels ATTRIBUTE_UNUSED)
6953 {
6954   rtx x = *x_p;
6955
6956   if (GET_CODE (x) == PLUS
6957       && GET_MODE_SIZE (mode) < 4
6958       && REG_P (XEXP (x, 0))
6959       && XEXP (x, 0) == stack_pointer_rtx
6960       && CONST_INT_P (XEXP (x, 1))
6961       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
6962     {
6963       rtx orig_x = x;
6964
6965       x = copy_rtx (x);
6966       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
6967                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
6968       return x;
6969     }
6970
6971   /* If both registers are hi-regs, then it's better to reload the
6972      entire expression rather than each register individually.  That
6973      only requires one reload register rather than two.  */
6974   if (GET_CODE (x) == PLUS
6975       && REG_P (XEXP (x, 0))
6976       && REG_P (XEXP (x, 1))
6977       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
6978       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
6979     {
6980       rtx orig_x = x;
6981
6982       x = copy_rtx (x);
6983       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
6984                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
6985       return x;
6986     }
6987
6988   return NULL;
6989 }
6990
6991 /* Test for various thread-local symbols.  */
6992
6993 /* Return TRUE if X is a thread-local symbol.  */
6994
6995 static bool
6996 arm_tls_symbol_p (rtx x)
6997 {
6998   if (! TARGET_HAVE_TLS)
6999     return false;
7000
7001   if (GET_CODE (x) != SYMBOL_REF)
7002     return false;
7003
7004   return SYMBOL_REF_TLS_MODEL (x) != 0;
7005 }
7006
7007 /* Helper for arm_tls_referenced_p.  */
7008
7009 static int
7010 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
7011 {
7012   if (GET_CODE (*x) == SYMBOL_REF)
7013     return SYMBOL_REF_TLS_MODEL (*x) != 0;
7014
7015   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
7016      TLS offsets, not real symbol references.  */
7017   if (GET_CODE (*x) == UNSPEC
7018       && XINT (*x, 1) == UNSPEC_TLS)
7019     return -1;
7020
7021   return 0;
7022 }
7023
7024 /* Return TRUE if X contains any TLS symbol references.  */
7025
7026 bool
7027 arm_tls_referenced_p (rtx x)
7028 {
7029   if (! TARGET_HAVE_TLS)
7030     return false;
7031
7032   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
7033 }
7034
7035 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
7036
7037    On the ARM, allow any integer (invalid ones are removed later by insn
7038    patterns), nice doubles and symbol_refs which refer to the function's
7039    constant pool XXX.
7040
7041    When generating pic allow anything.  */
7042
7043 static bool
7044 arm_legitimate_constant_p_1 (enum machine_mode mode, rtx x)
7045 {
7046   /* At present, we have no support for Neon structure constants, so forbid
7047      them here.  It might be possible to handle simple cases like 0 and -1
7048      in future.  */
7049   if (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode))
7050     return false;
7051
7052   return flag_pic || !label_mentioned_p (x);
7053 }
7054
7055 static bool
7056 thumb_legitimate_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
7057 {
7058   return (CONST_INT_P (x)
7059           || CONST_DOUBLE_P (x)
7060           || CONSTANT_ADDRESS_P (x)
7061           || flag_pic);
7062 }
7063
7064 static bool
7065 arm_legitimate_constant_p (enum machine_mode mode, rtx x)
7066 {
7067   return (!arm_cannot_force_const_mem (mode, x)
7068           && (TARGET_32BIT
7069               ? arm_legitimate_constant_p_1 (mode, x)
7070               : thumb_legitimate_constant_p (mode, x)));
7071 }
7072
7073 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
7074
7075 static bool
7076 arm_cannot_force_const_mem (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
7077 {
7078   rtx base, offset;
7079
7080   if (ARM_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
7081     {
7082       split_const (x, &base, &offset);
7083       if (GET_CODE (base) == SYMBOL_REF
7084           && !offset_within_block_p (base, INTVAL (offset)))
7085         return true;
7086     }
7087   return arm_tls_referenced_p (x);
7088 }
7089 \f
7090 #define REG_OR_SUBREG_REG(X)                                            \
7091   (REG_P (X)                                                    \
7092    || (GET_CODE (X) == SUBREG && REG_P (SUBREG_REG (X))))
7093
7094 #define REG_OR_SUBREG_RTX(X)                    \
7095    (REG_P (X) ? (X) : SUBREG_REG (X))
7096
7097 static inline int
7098 thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
7099 {
7100   enum machine_mode mode = GET_MODE (x);
7101   int total;
7102
7103   switch (code)
7104     {
7105     case ASHIFT:
7106     case ASHIFTRT:
7107     case LSHIFTRT:
7108     case ROTATERT:
7109     case PLUS:
7110     case MINUS:
7111     case COMPARE:
7112     case NEG:
7113     case NOT:
7114       return COSTS_N_INSNS (1);
7115
7116     case MULT:
7117       if (CONST_INT_P (XEXP (x, 1)))
7118         {
7119           int cycles = 0;
7120           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
7121
7122           while (i)
7123             {
7124               i >>= 2;
7125               cycles++;
7126             }
7127           return COSTS_N_INSNS (2) + cycles;
7128         }
7129       return COSTS_N_INSNS (1) + 16;
7130
7131     case SET:
7132       return (COSTS_N_INSNS (1)
7133               + 4 * ((MEM_P (SET_SRC (x)))
7134                      + MEM_P (SET_DEST (x))));
7135
7136     case CONST_INT:
7137       if (outer == SET)
7138         {
7139           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
7140             return 0;
7141           if (thumb_shiftable_const (INTVAL (x)))
7142             return COSTS_N_INSNS (2);
7143           return COSTS_N_INSNS (3);
7144         }
7145       else if ((outer == PLUS || outer == COMPARE)
7146                && INTVAL (x) < 256 && INTVAL (x) > -256)
7147         return 0;
7148       else if ((outer == IOR || outer == XOR || outer == AND)
7149                && INTVAL (x) < 256 && INTVAL (x) >= -256)
7150         return COSTS_N_INSNS (1);
7151       else if (outer == AND)
7152         {
7153           int i;
7154           /* This duplicates the tests in the andsi3 expander.  */
7155           for (i = 9; i <= 31; i++)
7156             if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
7157                 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
7158               return COSTS_N_INSNS (2);
7159         }
7160       else if (outer == ASHIFT || outer == ASHIFTRT
7161                || outer == LSHIFTRT)
7162         return 0;
7163       return COSTS_N_INSNS (2);
7164
7165     case CONST:
7166     case CONST_DOUBLE:
7167     case LABEL_REF:
7168     case SYMBOL_REF:
7169       return COSTS_N_INSNS (3);
7170
7171     case UDIV:
7172     case UMOD:
7173     case DIV:
7174     case MOD:
7175       return 100;
7176
7177     case TRUNCATE:
7178       return 99;
7179
7180     case AND:
7181     case XOR:
7182     case IOR:
7183       /* XXX guess.  */
7184       return 8;
7185
7186     case MEM:
7187       /* XXX another guess.  */
7188       /* Memory costs quite a lot for the first word, but subsequent words
7189          load at the equivalent of a single insn each.  */
7190       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
7191               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
7192                  ? 4 : 0));
7193
7194     case IF_THEN_ELSE:
7195       /* XXX a guess.  */
7196       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
7197         return 14;
7198       return 2;
7199
7200     case SIGN_EXTEND:
7201     case ZERO_EXTEND:
7202       total = mode == DImode ? COSTS_N_INSNS (1) : 0;
7203       total += thumb1_rtx_costs (XEXP (x, 0), GET_CODE (XEXP (x, 0)), code);
7204
7205       if (mode == SImode)
7206         return total;
7207
7208       if (arm_arch6)
7209         return total + COSTS_N_INSNS (1);
7210
7211       /* Assume a two-shift sequence.  Increase the cost slightly so
7212          we prefer actual shifts over an extend operation.  */
7213       return total + 1 + COSTS_N_INSNS (2);
7214
7215     default:
7216       return 99;
7217     }
7218 }
7219
7220 static inline bool
7221 arm_rtx_costs_1 (rtx x, enum rtx_code outer, int* total, bool speed)
7222 {
7223   enum machine_mode mode = GET_MODE (x);
7224   enum rtx_code subcode;
7225   rtx operand;
7226   enum rtx_code code = GET_CODE (x);
7227   *total = 0;
7228
7229   switch (code)
7230     {
7231     case MEM:
7232       /* Memory costs quite a lot for the first word, but subsequent words
7233          load at the equivalent of a single insn each.  */
7234       *total = COSTS_N_INSNS (2 + ARM_NUM_REGS (mode));
7235       return true;
7236
7237     case DIV:
7238     case MOD:
7239     case UDIV:
7240     case UMOD:
7241       if (TARGET_HARD_FLOAT && mode == SFmode)
7242         *total = COSTS_N_INSNS (2);
7243       else if (TARGET_HARD_FLOAT && mode == DFmode && !TARGET_VFP_SINGLE)
7244         *total = COSTS_N_INSNS (4);
7245       else
7246         *total = COSTS_N_INSNS (20);
7247       return false;
7248
7249     case ROTATE:
7250       if (REG_P (XEXP (x, 1)))
7251         *total = COSTS_N_INSNS (1); /* Need to subtract from 32 */
7252       else if (!CONST_INT_P (XEXP (x, 1)))
7253         *total = rtx_cost (XEXP (x, 1), code, 1, speed);
7254
7255       /* Fall through */
7256     case ROTATERT:
7257       if (mode != SImode)
7258         {
7259           *total += COSTS_N_INSNS (4);
7260           return true;
7261         }
7262
7263       /* Fall through */
7264     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
7265       *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7266       if (mode == DImode)
7267         {
7268           *total += COSTS_N_INSNS (3);
7269           return true;
7270         }
7271
7272       *total += COSTS_N_INSNS (1);
7273       /* Increase the cost of complex shifts because they aren't any faster,
7274          and reduce dual issue opportunities.  */
7275       if (arm_tune_cortex_a9
7276           && outer != SET && !CONST_INT_P (XEXP (x, 1)))
7277         ++*total;
7278
7279       return true;
7280
7281     case MINUS:
7282       if (mode == DImode)
7283         {
7284           *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7285           if (CONST_INT_P (XEXP (x, 0))
7286               && const_ok_for_arm (INTVAL (XEXP (x, 0))))
7287             {
7288               *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7289               return true;
7290             }
7291
7292           if (CONST_INT_P (XEXP (x, 1))
7293               && const_ok_for_arm (INTVAL (XEXP (x, 1))))
7294             {
7295               *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7296               return true;
7297             }
7298
7299           return false;
7300         }
7301
7302       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7303         {
7304           if (TARGET_HARD_FLOAT
7305               && (mode == SFmode
7306                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7307             {
7308               *total = COSTS_N_INSNS (1);
7309               if (CONST_DOUBLE_P (XEXP (x, 0))
7310                   && arm_const_double_rtx (XEXP (x, 0)))
7311                 {
7312                   *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7313                   return true;
7314                 }
7315
7316               if (CONST_DOUBLE_P (XEXP (x, 1))
7317                   && arm_const_double_rtx (XEXP (x, 1)))
7318                 {
7319                   *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7320                   return true;
7321                 }
7322
7323               return false;
7324             }
7325           *total = COSTS_N_INSNS (20);
7326           return false;
7327         }
7328
7329       *total = COSTS_N_INSNS (1);
7330       if (CONST_INT_P (XEXP (x, 0))
7331           && const_ok_for_arm (INTVAL (XEXP (x, 0))))
7332         {
7333           *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7334           return true;
7335         }
7336
7337       subcode = GET_CODE (XEXP (x, 1));
7338       if (subcode == ASHIFT || subcode == ASHIFTRT
7339           || subcode == LSHIFTRT
7340           || subcode == ROTATE || subcode == ROTATERT)
7341         {
7342           *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7343           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, 0, speed);
7344           return true;
7345         }
7346
7347       /* A shift as a part of RSB costs no more than RSB itself.  */
7348       if (GET_CODE (XEXP (x, 0)) == MULT
7349           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7350         {
7351           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, 0, speed);
7352           *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7353           return true;
7354         }
7355
7356       if (subcode == MULT
7357           && power_of_two_operand (XEXP (XEXP (x, 1), 1), SImode))
7358         {
7359           *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7360           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, 0, speed);
7361           return true;
7362         }
7363
7364       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMPARE
7365           || GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMM_COMPARE)
7366         {
7367           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, 0, speed);
7368           if (REG_P (XEXP (XEXP (x, 1), 0))
7369               && REGNO (XEXP (XEXP (x, 1), 0)) != CC_REGNUM)
7370             *total += COSTS_N_INSNS (1);
7371
7372           return true;
7373         }
7374
7375       /* Fall through */
7376
7377     case PLUS:
7378       if (code == PLUS && arm_arch6 && mode == SImode
7379           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
7380               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
7381         {
7382           *total = COSTS_N_INSNS (1);
7383           *total += rtx_cost (XEXP (XEXP (x, 0), 0), GET_CODE (XEXP (x, 0)),
7384                               0, speed);
7385           *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7386           return true;
7387         }
7388
7389       /* MLA: All arguments must be registers.  We filter out
7390          multiplication by a power of two, so that we fall down into
7391          the code below.  */
7392       if (GET_CODE (XEXP (x, 0)) == MULT
7393           && !power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7394         {
7395           /* The cost comes from the cost of the multiply.  */
7396           return false;
7397         }
7398
7399       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7400         {
7401           if (TARGET_HARD_FLOAT
7402               && (mode == SFmode
7403                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7404             {
7405               *total = COSTS_N_INSNS (1);
7406               if (CONST_DOUBLE_P (XEXP (x, 1))
7407                   && arm_const_double_rtx (XEXP (x, 1)))
7408                 {
7409                   *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7410                   return true;
7411                 }
7412
7413               return false;
7414             }
7415
7416           *total = COSTS_N_INSNS (20);
7417           return false;
7418         }
7419
7420       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMPARE
7421           || GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMM_COMPARE)
7422         {
7423           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 1), code, 1, speed);
7424           if (REG_P (XEXP (XEXP (x, 0), 0))
7425               && REGNO (XEXP (XEXP (x, 0), 0)) != CC_REGNUM)
7426             *total += COSTS_N_INSNS (1);
7427           return true;
7428         }
7429
7430       /* Fall through */
7431
7432     case AND: case XOR: case IOR:
7433
7434       /* Normally the frame registers will be spilt into reg+const during
7435          reload, so it is a bad idea to combine them with other instructions,
7436          since then they might not be moved outside of loops.  As a compromise
7437          we allow integration with ops that have a constant as their second
7438          operand.  */
7439       if (REG_OR_SUBREG_REG (XEXP (x, 0))
7440           && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
7441           && !CONST_INT_P (XEXP (x, 1)))
7442         *total = COSTS_N_INSNS (1);
7443
7444       if (mode == DImode)
7445         {
7446           *total += COSTS_N_INSNS (2);
7447           if (CONST_INT_P (XEXP (x, 1))
7448               && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
7449             {
7450               *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7451               return true;
7452             }
7453
7454           return false;
7455         }
7456
7457       *total += COSTS_N_INSNS (1);
7458       if (CONST_INT_P (XEXP (x, 1))
7459           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
7460         {
7461           *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7462           return true;
7463         }
7464       subcode = GET_CODE (XEXP (x, 0));
7465       if (subcode == ASHIFT || subcode == ASHIFTRT
7466           || subcode == LSHIFTRT
7467           || subcode == ROTATE || subcode == ROTATERT)
7468         {
7469           *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7470           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, 0, speed);
7471           return true;
7472         }
7473
7474       if (subcode == MULT
7475           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7476         {
7477           *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7478           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, 0, speed);
7479           return true;
7480         }
7481
7482       if (subcode == UMIN || subcode == UMAX
7483           || subcode == SMIN || subcode == SMAX)
7484         {
7485           *total = COSTS_N_INSNS (3);
7486           return true;
7487         }
7488
7489       return false;
7490
7491     case MULT:
7492       /* This should have been handled by the CPU specific routines.  */
7493       gcc_unreachable ();
7494
7495     case TRUNCATE:
7496       if (arm_arch3m && mode == SImode
7497           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
7498           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
7499           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
7500               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
7501           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
7502               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
7503         {
7504           *total = rtx_cost (XEXP (XEXP (x, 0), 0), LSHIFTRT, 0, speed);
7505           return true;
7506         }
7507       *total = COSTS_N_INSNS (2); /* Plus the cost of the MULT */
7508       return false;
7509
7510     case NEG:
7511       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7512         {
7513           if (TARGET_HARD_FLOAT
7514               && (mode == SFmode
7515                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7516             {
7517               *total = COSTS_N_INSNS (1);
7518               return false;
7519             }
7520           *total = COSTS_N_INSNS (2);
7521           return false;
7522         }
7523
7524       /* Fall through */
7525     case NOT:
7526       *total = COSTS_N_INSNS (ARM_NUM_REGS(mode));
7527       if (mode == SImode && code == NOT)
7528         {
7529           subcode = GET_CODE (XEXP (x, 0));
7530           if (subcode == ASHIFT || subcode == ASHIFTRT
7531               || subcode == LSHIFTRT
7532               || subcode == ROTATE || subcode == ROTATERT
7533               || (subcode == MULT
7534                   && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode)))
7535             {
7536               *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, 0, speed);
7537               /* Register shifts cost an extra cycle.  */
7538               if (!CONST_INT_P (XEXP (XEXP (x, 0), 1)))
7539                 *total += COSTS_N_INSNS (1) + rtx_cost (XEXP (XEXP (x, 0), 1),
7540                                                         subcode, 1, speed);
7541               return true;
7542             }
7543         }
7544
7545       return false;
7546
7547     case IF_THEN_ELSE:
7548       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
7549         {
7550           *total = COSTS_N_INSNS (4);
7551           return true;
7552         }
7553
7554       operand = XEXP (x, 0);
7555
7556       if (!((GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMPARE
7557              || GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMM_COMPARE)
7558             && REG_P (XEXP (operand, 0))
7559             && REGNO (XEXP (operand, 0)) == CC_REGNUM))
7560         *total += COSTS_N_INSNS (1);
7561       *total += (rtx_cost (XEXP (x, 1), code, 1, speed)
7562                  + rtx_cost (XEXP (x, 2), code, 2, speed));
7563       return true;
7564
7565     case NE:
7566       if (mode == SImode && XEXP (x, 1) == const0_rtx)
7567         {
7568           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, 0, speed);
7569           return true;
7570         }
7571       goto scc_insn;
7572
7573     case GE:
7574       if ((!REG_P (XEXP (x, 0)) || REGNO (XEXP (x, 0)) != CC_REGNUM)
7575           && mode == SImode && XEXP (x, 1) == const0_rtx)
7576         {
7577           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, 0, speed);
7578           return true;
7579         }
7580       goto scc_insn;
7581
7582     case LT:
7583       if ((!REG_P (XEXP (x, 0)) || REGNO (XEXP (x, 0)) != CC_REGNUM)
7584           && mode == SImode && XEXP (x, 1) == const0_rtx)
7585         {
7586           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, 0, speed);
7587           return true;
7588         }
7589       goto scc_insn;
7590
7591     case EQ:
7592     case GT:
7593     case LE:
7594     case GEU:
7595     case LTU:
7596     case GTU:
7597     case LEU:
7598     case UNORDERED:
7599     case ORDERED:
7600     case UNEQ:
7601     case UNGE:
7602     case UNLT:
7603     case UNGT:
7604     case UNLE:
7605     scc_insn:
7606       /* SCC insns.  In the case where the comparison has already been
7607          performed, then they cost 2 instructions.  Otherwise they need
7608          an additional comparison before them.  */
7609       *total = COSTS_N_INSNS (2);
7610       if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) == CC_REGNUM)
7611         {
7612           return true;
7613         }
7614
7615       /* Fall through */
7616     case COMPARE:
7617       if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) == CC_REGNUM)
7618         {
7619           *total = 0;
7620           return true;
7621         }
7622
7623       *total += COSTS_N_INSNS (1);
7624       if (CONST_INT_P (XEXP (x, 1))
7625           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
7626         {
7627           *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7628           return true;
7629         }
7630
7631       subcode = GET_CODE (XEXP (x, 0));
7632       if (subcode == ASHIFT || subcode == ASHIFTRT
7633           || subcode == LSHIFTRT
7634           || subcode == ROTATE || subcode == ROTATERT)
7635         {
7636           *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7637           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, 0, speed);
7638           return true;
7639         }
7640
7641       if (subcode == MULT
7642           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
7643         {
7644           *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7645           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, 0, speed);
7646           return true;
7647         }
7648
7649       return false;
7650
7651     case UMIN:
7652     case UMAX:
7653     case SMIN:
7654     case SMAX:
7655       *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, 0, speed);
7656       if (!CONST_INT_P (XEXP (x, 1))
7657           || !const_ok_for_arm (INTVAL (XEXP (x, 1))))
7658         *total += rtx_cost (XEXP (x, 1), code, 1, speed);
7659       return true;
7660
7661     case ABS:
7662       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7663         {
7664           if (TARGET_HARD_FLOAT
7665               && (mode == SFmode
7666                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
7667             {
7668               *total = COSTS_N_INSNS (1);
7669               return false;
7670             }
7671           *total = COSTS_N_INSNS (20);
7672           return false;
7673         }
7674       *total = COSTS_N_INSNS (1);
7675       if (mode == DImode)
7676         *total += COSTS_N_INSNS (3);
7677       return false;
7678
7679     case SIGN_EXTEND:
7680     case ZERO_EXTEND:
7681       *total = 0;
7682       if (GET_MODE_CLASS (mode) == MODE_INT)
7683         {
7684           rtx op = XEXP (x, 0);
7685           enum machine_mode opmode = GET_MODE (op);
7686
7687           if (mode == DImode)
7688             *total += COSTS_N_INSNS (1);
7689
7690           if (opmode != SImode)
7691             {
7692               if (MEM_P (op))
7693                 {
7694                   /* If !arm_arch4, we use one of the extendhisi2_mem
7695                      or movhi_bytes patterns for HImode.  For a QImode
7696                      sign extension, we first zero-extend from memory
7697                      and then perform a shift sequence.  */
7698                   if (!arm_arch4 && (opmode != QImode || code == SIGN_EXTEND))
7699                     *total += COSTS_N_INSNS (2);
7700                 }
7701               else if (arm_arch6)
7702                 *total += COSTS_N_INSNS (1);
7703
7704               /* We don't have the necessary insn, so we need to perform some
7705                  other operation.  */
7706               else if (TARGET_ARM && code == ZERO_EXTEND && mode == QImode)
7707                 /* An and with constant 255.  */
7708                 *total += COSTS_N_INSNS (1);
7709               else
7710                 /* A shift sequence.  Increase costs slightly to avoid
7711                    combining two shifts into an extend operation.  */
7712                 *total += COSTS_N_INSNS (2) + 1;
7713             }
7714
7715           return false;
7716         }
7717
7718       switch (GET_MODE (XEXP (x, 0)))
7719         {
7720         case V8QImode:
7721         case V4HImode:
7722         case V2SImode:
7723         case V4QImode:
7724         case V2HImode:
7725           *total = COSTS_N_INSNS (1);
7726           return false;
7727
7728         default:
7729           gcc_unreachable ();
7730         }
7731       gcc_unreachable ();
7732
7733     case ZERO_EXTRACT:
7734     case SIGN_EXTRACT:
7735       *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, 0, speed);
7736       return true;
7737
7738     case CONST_INT:
7739       if (const_ok_for_arm (INTVAL (x))
7740           || const_ok_for_arm (~INTVAL (x)))
7741         *total = COSTS_N_INSNS (1);
7742       else
7743         *total = COSTS_N_INSNS (arm_gen_constant (SET, mode, NULL_RTX,
7744                                                   INTVAL (x), NULL_RTX,
7745                                                   NULL_RTX, 0, 0));
7746       return true;
7747
7748     case CONST:
7749     case LABEL_REF:
7750     case SYMBOL_REF:
7751       *total = COSTS_N_INSNS (3);
7752       return true;
7753
7754     case HIGH:
7755       *total = COSTS_N_INSNS (1);
7756       return true;
7757
7758     case LO_SUM:
7759       *total = COSTS_N_INSNS (1);
7760       *total += rtx_cost (XEXP (x, 0), code, 0, speed);
7761       return true;
7762
7763     case CONST_DOUBLE:
7764       if (TARGET_HARD_FLOAT && vfp3_const_double_rtx (x)
7765           && (mode == SFmode || !TARGET_VFP_SINGLE))
7766         *total = COSTS_N_INSNS (1);
7767       else
7768         *total = COSTS_N_INSNS (4);
7769       return true;
7770
7771     case SET:
7772       /* The vec_extract patterns accept memory operands that require an
7773          address reload.  Account for the cost of that reload to give the
7774          auto-inc-dec pass an incentive to try to replace them.  */
7775       if (TARGET_NEON && MEM_P (SET_DEST (x))
7776           && GET_CODE (SET_SRC (x)) == VEC_SELECT)
7777         {
7778           *total = rtx_cost (SET_DEST (x), code, 0, speed);
7779           if (!neon_vector_mem_operand (SET_DEST (x), 2))
7780             *total += COSTS_N_INSNS (1);
7781           return true;
7782         }
7783       /* Likewise for the vec_set patterns.  */
7784       if (TARGET_NEON && GET_CODE (SET_SRC (x)) == VEC_MERGE
7785           && GET_CODE (XEXP (SET_SRC (x), 0)) == VEC_DUPLICATE
7786           && MEM_P (XEXP (XEXP (SET_SRC (x), 0), 0)))
7787         {
7788           rtx mem = XEXP (XEXP (SET_SRC (x), 0), 0);
7789           *total = rtx_cost (mem, code, 0, speed);
7790           if (!neon_vector_mem_operand (mem, 2))
7791             *total += COSTS_N_INSNS (1);
7792           return true;
7793         }
7794       return false;
7795
7796     case UNSPEC:
7797       /* We cost this as high as our memory costs to allow this to
7798          be hoisted from loops.  */
7799       if (XINT (x, 1) == UNSPEC_PIC_UNIFIED)
7800         {
7801           *total = COSTS_N_INSNS (2 + ARM_NUM_REGS (mode));
7802         }
7803       return true;
7804
7805     case CONST_VECTOR:
7806       if (TARGET_NEON
7807           && TARGET_HARD_FLOAT
7808           && outer == SET
7809           && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode))
7810           && neon_immediate_valid_for_move (x, mode, NULL, NULL))
7811         *total = COSTS_N_INSNS (1);
7812       else
7813         *total = COSTS_N_INSNS (4);
7814       return true;
7815
7816     default:
7817       *total = COSTS_N_INSNS (4);
7818       return false;
7819     }
7820 }
7821
7822 /* Estimates the size cost of thumb1 instructions.
7823    For now most of the code is copied from thumb1_rtx_costs. We need more
7824    fine grain tuning when we have more related test cases.  */
7825 static inline int
7826 thumb1_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
7827 {
7828   enum machine_mode mode = GET_MODE (x);
7829
7830   switch (code)
7831     {
7832     case ASHIFT:
7833     case ASHIFTRT:
7834     case LSHIFTRT:
7835     case ROTATERT:
7836     case PLUS:
7837     case MINUS:
7838     case COMPARE:
7839     case NEG:
7840     case NOT:
7841       return COSTS_N_INSNS (1);
7842
7843     case MULT:
7844       if (CONST_INT_P (XEXP (x, 1)))
7845         {
7846           /* Thumb1 mul instruction can't operate on const. We must Load it
7847              into a register first.  */
7848           int const_size = thumb1_size_rtx_costs (XEXP (x, 1), CONST_INT, SET);
7849           return COSTS_N_INSNS (1) + const_size;
7850         }
7851       return COSTS_N_INSNS (1);
7852
7853     case SET:
7854       return (COSTS_N_INSNS (1)
7855               + 4 * ((MEM_P (SET_SRC (x)))
7856                      + MEM_P (SET_DEST (x))));
7857
7858     case CONST_INT:
7859       if (outer == SET)
7860         {
7861           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
7862             return COSTS_N_INSNS (1);
7863           /* See split "TARGET_THUMB1 && satisfies_constraint_J".  */
7864           if (INTVAL (x) >= -255 && INTVAL (x) <= -1)
7865             return COSTS_N_INSNS (2);
7866           /* See split "TARGET_THUMB1 && satisfies_constraint_K".  */
7867           if (thumb_shiftable_const (INTVAL (x)))
7868             return COSTS_N_INSNS (2);
7869           return COSTS_N_INSNS (3);
7870         }
7871       else if ((outer == PLUS || outer == COMPARE)
7872                && INTVAL (x) < 256 && INTVAL (x) > -256)
7873         return 0;
7874       else if ((outer == IOR || outer == XOR || outer == AND)
7875                && INTVAL (x) < 256 && INTVAL (x) >= -256)
7876         return COSTS_N_INSNS (1);
7877       else if (outer == AND)
7878         {
7879           int i;
7880           /* This duplicates the tests in the andsi3 expander.  */
7881           for (i = 9; i <= 31; i++)
7882             if ((((HOST_WIDE_INT) 1) << i) - 1 == INTVAL (x)
7883                 || (((HOST_WIDE_INT) 1) << i) - 1 == ~INTVAL (x))
7884               return COSTS_N_INSNS (2);
7885         }
7886       else if (outer == ASHIFT || outer == ASHIFTRT
7887                || outer == LSHIFTRT)
7888         return 0;
7889       return COSTS_N_INSNS (2);
7890
7891     case CONST:
7892     case CONST_DOUBLE:
7893     case LABEL_REF:
7894     case SYMBOL_REF:
7895       return COSTS_N_INSNS (3);
7896
7897     case UDIV:
7898     case UMOD:
7899     case DIV:
7900     case MOD:
7901       return 100;
7902
7903     case TRUNCATE:
7904       return 99;
7905
7906     case AND:
7907     case XOR:
7908     case IOR:
7909       /* XXX guess.  */
7910       return 8;
7911
7912     case MEM:
7913       /* XXX another guess.  */
7914       /* Memory costs quite a lot for the first word, but subsequent words
7915          load at the equivalent of a single insn each.  */
7916       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
7917               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
7918                  ? 4 : 0));
7919
7920     case IF_THEN_ELSE:
7921       /* XXX a guess.  */
7922       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
7923         return 14;
7924       return 2;
7925
7926     case ZERO_EXTEND:
7927       /* XXX still guessing.  */
7928       switch (GET_MODE (XEXP (x, 0)))
7929         {
7930           case QImode:
7931             return (1 + (mode == DImode ? 4 : 0)
7932                     + (MEM_P (XEXP (x, 0)) ? 10 : 0));
7933
7934           case HImode:
7935             return (4 + (mode == DImode ? 4 : 0)
7936                     + (MEM_P (XEXP (x, 0)) ? 10 : 0));
7937
7938           case SImode:
7939             return (1 + (MEM_P (XEXP (x, 0)) ? 10 : 0));
7940
7941           default:
7942             return 99;
7943         }
7944
7945     default:
7946       return 99;
7947     }
7948 }
7949
7950 /* RTX costs when optimizing for size.  */
7951 static bool
7952 arm_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
7953                     int *total)
7954 {
7955   enum machine_mode mode = GET_MODE (x);
7956   if (TARGET_THUMB1)
7957     {
7958       *total = thumb1_size_rtx_costs (x, code, outer_code);
7959       return true;
7960     }
7961
7962   /* FIXME: This makes no attempt to prefer narrow Thumb-2 instructions.  */
7963   switch (code)
7964     {
7965     case MEM:
7966       /* A memory access costs 1 insn if the mode is small, or the address is
7967          a single register, otherwise it costs one insn per word.  */
7968       if (REG_P (XEXP (x, 0)))
7969         *total = COSTS_N_INSNS (1);
7970       else if (flag_pic
7971                && GET_CODE (XEXP (x, 0)) == PLUS
7972                && will_be_in_index_register (XEXP (XEXP (x, 0), 1)))
7973         /* This will be split into two instructions.
7974            See arm.md:calculate_pic_address.  */
7975         *total = COSTS_N_INSNS (2);
7976       else
7977         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
7978       return true;
7979
7980     case DIV:
7981     case MOD:
7982     case UDIV:
7983     case UMOD:
7984       /* Needs a libcall, so it costs about this.  */
7985       *total = COSTS_N_INSNS (2);
7986       return false;
7987
7988     case ROTATE:
7989       if (mode == SImode && REG_P (XEXP (x, 1)))
7990         {
7991           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, 0, false);
7992           return true;
7993         }
7994       /* Fall through */
7995     case ROTATERT:
7996     case ASHIFT:
7997     case LSHIFTRT:
7998     case ASHIFTRT:
7999       if (mode == DImode && CONST_INT_P (XEXP (x, 1)))
8000         {
8001           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code, 0, false);
8002           return true;
8003         }
8004       else if (mode == SImode)
8005         {
8006           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, 0, false);
8007           /* Slightly disparage register shifts, but not by much.  */
8008           if (!CONST_INT_P (XEXP (x, 1)))
8009             *total += 1 + rtx_cost (XEXP (x, 1), code, 1, false);
8010           return true;
8011         }
8012
8013       /* Needs a libcall.  */
8014       *total = COSTS_N_INSNS (2);
8015       return false;
8016
8017     case MINUS:
8018       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
8019           && (mode == SFmode || !TARGET_VFP_SINGLE))
8020         {
8021           *total = COSTS_N_INSNS (1);
8022           return false;
8023         }
8024
8025       if (mode == SImode)
8026         {
8027           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
8028           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
8029
8030           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
8031               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
8032               || subcode1 == ROTATE || subcode1 == ROTATERT
8033               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
8034               || subcode1 == ASHIFTRT)
8035             {
8036               /* It's just the cost of the two operands.  */
8037               *total = 0;
8038               return false;
8039             }
8040
8041           *total = COSTS_N_INSNS (1);
8042           return false;
8043         }
8044
8045       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
8046       return false;
8047
8048     case PLUS:
8049       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
8050           && (mode == SFmode || !TARGET_VFP_SINGLE))
8051         {
8052           *total = COSTS_N_INSNS (1);
8053           return false;
8054         }
8055
8056       /* A shift as a part of ADD costs nothing.  */
8057       if (GET_CODE (XEXP (x, 0)) == MULT
8058           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
8059         {
8060           *total = COSTS_N_INSNS (TARGET_THUMB2 ? 2 : 1);
8061           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, 0, false);
8062           *total += rtx_cost (XEXP (x, 1), code, 1, false);
8063           return true;
8064         }
8065
8066       /* Fall through */
8067     case AND: case XOR: case IOR:
8068       if (mode == SImode)
8069         {
8070           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
8071
8072           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
8073               || subcode == LSHIFTRT || subcode == ASHIFTRT
8074               || (code == AND && subcode == NOT))
8075             {
8076               /* It's just the cost of the two operands.  */
8077               *total = 0;
8078               return false;
8079             }
8080         }
8081
8082       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
8083       return false;
8084
8085     case MULT:
8086       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
8087       return false;
8088
8089     case NEG:
8090       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
8091           && (mode == SFmode || !TARGET_VFP_SINGLE))
8092         {
8093           *total = COSTS_N_INSNS (1);
8094           return false;
8095         }
8096
8097       /* Fall through */
8098     case NOT:
8099       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
8100
8101       return false;
8102
8103     case IF_THEN_ELSE:
8104       *total = 0;
8105       return false;
8106
8107     case COMPARE:
8108       if (cc_register (XEXP (x, 0), VOIDmode))
8109         * total = 0;
8110       else
8111         *total = COSTS_N_INSNS (1);
8112       return false;
8113
8114     case ABS:
8115       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT
8116           && (mode == SFmode || !TARGET_VFP_SINGLE))
8117         *total = COSTS_N_INSNS (1);
8118       else
8119         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
8120       return false;
8121
8122     case SIGN_EXTEND:
8123     case ZERO_EXTEND:
8124       return arm_rtx_costs_1 (x, outer_code, total, 0);
8125
8126     case CONST_INT:
8127       if (const_ok_for_arm (INTVAL (x)))
8128         /* A multiplication by a constant requires another instruction
8129            to load the constant to a register.  */
8130         *total = COSTS_N_INSNS ((outer_code == SET || outer_code == MULT)
8131                                 ? 1 : 0);
8132       else if (const_ok_for_arm (~INTVAL (x)))
8133         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
8134       else if (const_ok_for_arm (-INTVAL (x)))
8135         {
8136           if (outer_code == COMPARE || outer_code == PLUS
8137               || outer_code == MINUS)
8138             *total = 0;
8139           else
8140             *total = COSTS_N_INSNS (1);
8141         }
8142       else
8143         *total = COSTS_N_INSNS (2);
8144       return true;
8145
8146     case CONST:
8147     case LABEL_REF:
8148     case SYMBOL_REF:
8149       *total = COSTS_N_INSNS (2);
8150       return true;
8151
8152     case CONST_DOUBLE:
8153       *total = COSTS_N_INSNS (4);
8154       return true;
8155
8156     case CONST_VECTOR:
8157       if (TARGET_NEON
8158           && TARGET_HARD_FLOAT
8159           && outer_code == SET
8160           && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode))
8161           && neon_immediate_valid_for_move (x, mode, NULL, NULL))
8162         *total = COSTS_N_INSNS (1);
8163       else
8164         *total = COSTS_N_INSNS (4);
8165       return true;
8166
8167     case HIGH:
8168     case LO_SUM:
8169       /* We prefer constant pool entries to MOVW/MOVT pairs, so bump the
8170          cost of these slightly.  */
8171       *total = COSTS_N_INSNS (1) + 1;
8172       return true;
8173
8174     case SET:
8175       return false;
8176
8177     default:
8178       if (mode != VOIDmode)
8179         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
8180       else
8181         *total = COSTS_N_INSNS (4); /* How knows?  */
8182       return false;
8183     }
8184 }
8185
8186 /* RTX costs when optimizing for size.  */
8187 static bool
8188 arm_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
8189                int *total, bool speed)
8190 {
8191   if (!speed)
8192     return arm_size_rtx_costs (x, (enum rtx_code) code,
8193                                (enum rtx_code) outer_code, total);
8194   else
8195     return current_tune->rtx_costs (x, (enum rtx_code) code,
8196                                     (enum rtx_code) outer_code,
8197                                     total, speed);
8198 }
8199
8200 /* RTX costs for cores with a slow MUL implementation.  Thumb-2 is not
8201    supported on any "slowmul" cores, so it can be ignored.  */
8202
8203 static bool
8204 arm_slowmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
8205                        int *total, bool speed)
8206 {
8207   enum machine_mode mode = GET_MODE (x);
8208
8209   if (TARGET_THUMB)
8210     {
8211       *total = thumb1_rtx_costs (x, code, outer_code);
8212       return true;
8213     }
8214
8215   switch (code)
8216     {
8217     case MULT:
8218       if (GET_MODE_CLASS (mode) == MODE_FLOAT
8219           || mode == DImode)
8220         {
8221           *total = COSTS_N_INSNS (20);
8222           return false;
8223         }
8224
8225       if (CONST_INT_P (XEXP (x, 1)))
8226         {
8227           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
8228                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
8229           int cost, const_ok = const_ok_for_arm (i);
8230           int j, booth_unit_size;
8231
8232           /* Tune as appropriate.  */
8233           cost = const_ok ? 4 : 8;
8234           booth_unit_size = 2;
8235           for (j = 0; i && j < 32; j += booth_unit_size)
8236             {
8237               i >>= booth_unit_size;
8238               cost++;
8239             }
8240
8241           *total = COSTS_N_INSNS (cost);
8242           *total += rtx_cost (XEXP (x, 0), code, 0, speed);
8243           return true;
8244         }
8245
8246       *total = COSTS_N_INSNS (20);
8247       return false;
8248
8249     default:
8250       return arm_rtx_costs_1 (x, outer_code, total, speed);;
8251     }
8252 }
8253
8254
8255 /* RTX cost for cores with a fast multiply unit (M variants).  */
8256
8257 static bool
8258 arm_fastmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
8259                        int *total, bool speed)
8260 {
8261   enum machine_mode mode = GET_MODE (x);
8262
8263   if (TARGET_THUMB1)
8264     {
8265       *total = thumb1_rtx_costs (x, code, outer_code);
8266       return true;
8267     }
8268
8269   /* ??? should thumb2 use different costs?  */
8270   switch (code)
8271     {
8272     case MULT:
8273       /* There is no point basing this on the tuning, since it is always the
8274          fast variant if it exists at all.  */
8275       if (mode == DImode
8276           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
8277           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
8278               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
8279         {
8280           *total = COSTS_N_INSNS(2);
8281           return false;
8282         }
8283
8284
8285       if (mode == DImode)
8286         {
8287           *total = COSTS_N_INSNS (5);
8288           return false;
8289         }
8290
8291       if (CONST_INT_P (XEXP (x, 1)))
8292         {
8293           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
8294                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
8295           int cost, const_ok = const_ok_for_arm (i);
8296           int j, booth_unit_size;
8297
8298           /* Tune as appropriate.  */
8299           cost = const_ok ? 4 : 8;
8300           booth_unit_size = 8;
8301           for (j = 0; i && j < 32; j += booth_unit_size)
8302             {
8303               i >>= booth_unit_size;
8304               cost++;
8305             }
8306
8307           *total = COSTS_N_INSNS(cost);
8308           return false;
8309         }
8310
8311       if (mode == SImode)
8312         {
8313           *total = COSTS_N_INSNS (4);
8314           return false;
8315         }
8316
8317       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
8318         {
8319           if (TARGET_HARD_FLOAT
8320               && (mode == SFmode
8321                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
8322             {
8323               *total = COSTS_N_INSNS (1);
8324               return false;
8325             }
8326         }
8327
8328       /* Requires a lib call */
8329       *total = COSTS_N_INSNS (20);
8330       return false;
8331
8332     default:
8333       return arm_rtx_costs_1 (x, outer_code, total, speed);
8334     }
8335 }
8336
8337
8338 /* RTX cost for XScale CPUs.  Thumb-2 is not supported on any xscale cores,
8339    so it can be ignored.  */
8340
8341 static bool
8342 arm_xscale_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
8343                       int *total, bool speed)
8344 {
8345   enum machine_mode mode = GET_MODE (x);
8346
8347   if (TARGET_THUMB)
8348     {
8349       *total = thumb1_rtx_costs (x, code, outer_code);
8350       return true;
8351     }
8352
8353   switch (code)
8354     {
8355     case COMPARE:
8356       if (GET_CODE (XEXP (x, 0)) != MULT)
8357         return arm_rtx_costs_1 (x, outer_code, total, speed);
8358
8359       /* A COMPARE of a MULT is slow on XScale; the muls instruction
8360          will stall until the multiplication is complete.  */
8361       *total = COSTS_N_INSNS (3);
8362       return false;
8363
8364     case MULT:
8365       /* There is no point basing this on the tuning, since it is always the
8366          fast variant if it exists at all.  */
8367       if (mode == DImode
8368           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
8369           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
8370               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
8371         {
8372           *total = COSTS_N_INSNS (2);
8373           return false;
8374         }
8375
8376
8377       if (mode == DImode)
8378         {
8379           *total = COSTS_N_INSNS (5);
8380           return false;
8381         }
8382
8383       if (CONST_INT_P (XEXP (x, 1)))
8384         {
8385           /* If operand 1 is a constant we can more accurately
8386              calculate the cost of the multiply.  The multiplier can
8387              retire 15 bits on the first cycle and a further 12 on the
8388              second.  We do, of course, have to load the constant into
8389              a register first.  */
8390           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
8391           /* There's a general overhead of one cycle.  */
8392           int cost = 1;
8393           unsigned HOST_WIDE_INT masked_const;
8394
8395           if (i & 0x80000000)
8396             i = ~i;
8397
8398           i &= (unsigned HOST_WIDE_INT) 0xffffffff;
8399
8400           masked_const = i & 0xffff8000;
8401           if (masked_const != 0)
8402             {
8403               cost++;
8404               masked_const = i & 0xf8000000;
8405               if (masked_const != 0)
8406                 cost++;
8407             }
8408           *total = COSTS_N_INSNS (cost);
8409           return false;
8410         }
8411
8412       if (mode == SImode)
8413         {
8414           *total = COSTS_N_INSNS (3);
8415           return false;
8416         }
8417
8418       /* Requires a lib call */
8419       *total = COSTS_N_INSNS (20);
8420       return false;
8421
8422     default:
8423       return arm_rtx_costs_1 (x, outer_code, total, speed);
8424     }
8425 }
8426
8427
8428 /* RTX costs for 9e (and later) cores.  */
8429
8430 static bool
8431 arm_9e_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
8432                   int *total, bool speed)
8433 {
8434   enum machine_mode mode = GET_MODE (x);
8435
8436   if (TARGET_THUMB1)
8437     {
8438       switch (code)
8439         {
8440         case MULT:
8441           *total = COSTS_N_INSNS (3);
8442           return true;
8443
8444         default:
8445           *total = thumb1_rtx_costs (x, code, outer_code);
8446           return true;
8447         }
8448     }
8449
8450   switch (code)
8451     {
8452     case MULT:
8453       /* There is no point basing this on the tuning, since it is always the
8454          fast variant if it exists at all.  */
8455       if (mode == DImode
8456           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
8457           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
8458               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
8459         {
8460           *total = COSTS_N_INSNS (2);
8461           return false;
8462         }
8463
8464
8465       if (mode == DImode)
8466         {
8467           *total = COSTS_N_INSNS (5);
8468           return false;
8469         }
8470
8471       if (mode == SImode)
8472         {
8473           *total = COSTS_N_INSNS (2);
8474           return false;
8475         }
8476
8477       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
8478         {
8479           if (TARGET_HARD_FLOAT
8480               && (mode == SFmode
8481                   || (mode == DFmode && !TARGET_VFP_SINGLE)))
8482             {
8483               *total = COSTS_N_INSNS (1);
8484               return false;
8485             }
8486         }
8487
8488       *total = COSTS_N_INSNS (20);
8489       return false;
8490
8491     default:
8492       return arm_rtx_costs_1 (x, outer_code, total, speed);
8493     }
8494 }
8495 /* All address computations that can be done are free, but rtx cost returns
8496    the same for practically all of them.  So we weight the different types
8497    of address here in the order (most pref first):
8498    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
8499 static inline int
8500 arm_arm_address_cost (rtx x)
8501 {
8502   enum rtx_code c  = GET_CODE (x);
8503
8504   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
8505     return 0;
8506   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
8507     return 10;
8508
8509   if (c == PLUS)
8510     {
8511       if (CONST_INT_P (XEXP (x, 1)))
8512         return 2;
8513
8514       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
8515         return 3;
8516
8517       return 4;
8518     }
8519
8520   return 6;
8521 }
8522
8523 static inline int
8524 arm_thumb_address_cost (rtx x)
8525 {
8526   enum rtx_code c  = GET_CODE (x);
8527
8528   if (c == REG)
8529     return 1;
8530   if (c == PLUS
8531       && REG_P (XEXP (x, 0))
8532       && CONST_INT_P (XEXP (x, 1)))
8533     return 1;
8534
8535   return 2;
8536 }
8537
8538 static int
8539 arm_address_cost (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED,
8540                   addr_space_t as ATTRIBUTE_UNUSED, bool speed ATTRIBUTE_UNUSED)
8541 {
8542   return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
8543 }
8544
8545 /* Adjust cost hook for XScale.  */
8546 static bool
8547 xscale_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
8548 {
8549   /* Some true dependencies can have a higher cost depending
8550      on precisely how certain input operands are used.  */
8551   if (REG_NOTE_KIND(link) == 0
8552       && recog_memoized (insn) >= 0
8553       && recog_memoized (dep) >= 0)
8554     {
8555       int shift_opnum = get_attr_shift (insn);
8556       enum attr_type attr_type = get_attr_type (dep);
8557
8558       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
8559          operand for INSN.  If we have a shifted input operand and the
8560          instruction we depend on is another ALU instruction, then we may
8561          have to account for an additional stall.  */
8562       if (shift_opnum != 0
8563           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
8564         {
8565           rtx shifted_operand;
8566           int opno;
8567
8568           /* Get the shifted operand.  */
8569           extract_insn (insn);
8570           shifted_operand = recog_data.operand[shift_opnum];
8571
8572           /* Iterate over all the operands in DEP.  If we write an operand
8573              that overlaps with SHIFTED_OPERAND, then we have increase the
8574              cost of this dependency.  */
8575           extract_insn (dep);
8576           preprocess_constraints ();
8577           for (opno = 0; opno < recog_data.n_operands; opno++)
8578             {
8579               /* We can ignore strict inputs.  */
8580               if (recog_data.operand_type[opno] == OP_IN)
8581                 continue;
8582
8583               if (reg_overlap_mentioned_p (recog_data.operand[opno],
8584                                            shifted_operand))
8585                 {
8586                   *cost = 2;
8587                   return false;
8588                 }
8589             }
8590         }
8591     }
8592   return true;
8593 }
8594
8595 /* Adjust cost hook for Cortex A9.  */
8596 static bool
8597 cortex_a9_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
8598 {
8599   switch (REG_NOTE_KIND (link))
8600     {
8601     case REG_DEP_ANTI:
8602       *cost = 0;
8603       return false;
8604
8605     case REG_DEP_TRUE:
8606     case REG_DEP_OUTPUT:
8607         if (recog_memoized (insn) >= 0
8608             && recog_memoized (dep) >= 0)
8609           {
8610             if (GET_CODE (PATTERN (insn)) == SET)
8611               {
8612                 if (GET_MODE_CLASS
8613                     (GET_MODE (SET_DEST (PATTERN (insn)))) == MODE_FLOAT
8614                   || GET_MODE_CLASS
8615                     (GET_MODE (SET_SRC (PATTERN (insn)))) == MODE_FLOAT)
8616                   {
8617                     enum attr_type attr_type_insn = get_attr_type (insn);
8618                     enum attr_type attr_type_dep = get_attr_type (dep);
8619
8620                     /* By default all dependencies of the form
8621                        s0 = s0 <op> s1
8622                        s0 = s0 <op> s2
8623                        have an extra latency of 1 cycle because
8624                        of the input and output dependency in this
8625                        case. However this gets modeled as an true
8626                        dependency and hence all these checks.  */
8627                     if (REG_P (SET_DEST (PATTERN (insn)))
8628                         && REG_P (SET_DEST (PATTERN (dep)))
8629                         && reg_overlap_mentioned_p (SET_DEST (PATTERN (insn)),
8630                                                     SET_DEST (PATTERN (dep))))
8631                       {
8632                         /* FMACS is a special case where the dependent
8633                            instruction can be issued 3 cycles before
8634                            the normal latency in case of an output
8635                            dependency.  */
8636                         if ((attr_type_insn == TYPE_FMACS
8637                              || attr_type_insn == TYPE_FMACD)
8638                             && (attr_type_dep == TYPE_FMACS
8639                                 || attr_type_dep == TYPE_FMACD))
8640                           {
8641                             if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
8642                               *cost = insn_default_latency (dep) - 3;
8643                             else
8644                               *cost = insn_default_latency (dep);
8645                             return false;
8646                           }
8647                         else
8648                           {
8649                             if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
8650                               *cost = insn_default_latency (dep) + 1;
8651                             else
8652                               *cost = insn_default_latency (dep);
8653                           }
8654                         return false;
8655                       }
8656                   }
8657               }
8658           }
8659         break;
8660
8661     default:
8662       gcc_unreachable ();
8663     }
8664
8665   return true;
8666 }
8667
8668 /* Adjust cost hook for FA726TE.  */
8669 static bool
8670 fa726te_sched_adjust_cost (rtx insn, rtx link, rtx dep, int * cost)
8671 {
8672   /* For FA726TE, true dependency on CPSR (i.e. set cond followed by predicated)
8673      have penalty of 3.  */
8674   if (REG_NOTE_KIND (link) == REG_DEP_TRUE
8675       && recog_memoized (insn) >= 0
8676       && recog_memoized (dep) >= 0
8677       && get_attr_conds (dep) == CONDS_SET)
8678     {
8679       /* Use of carry (e.g. 64-bit arithmetic) in ALU: 3-cycle latency.  */
8680       if (get_attr_conds (insn) == CONDS_USE
8681           && get_attr_type (insn) != TYPE_BRANCH)
8682         {
8683           *cost = 3;
8684           return false;
8685         }
8686
8687       if (GET_CODE (PATTERN (insn)) == COND_EXEC
8688           || get_attr_conds (insn) == CONDS_USE)
8689         {
8690           *cost = 0;
8691           return false;
8692         }
8693     }
8694
8695   return true;
8696 }
8697
8698 /* Implement TARGET_REGISTER_MOVE_COST.
8699
8700    Moves between VFP_REGS and GENERAL_REGS are a single insn, but
8701    it is typically more expensive than a single memory access.  We set
8702    the cost to less than two memory accesses so that floating
8703    point to integer conversion does not go through memory.  */
8704
8705 int
8706 arm_register_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
8707                         reg_class_t from, reg_class_t to)
8708 {
8709   if (TARGET_32BIT)
8710     {
8711       if ((IS_VFP_CLASS (from) && !IS_VFP_CLASS (to))
8712           || (!IS_VFP_CLASS (from) && IS_VFP_CLASS (to)))
8713         return 15;
8714       else if ((from == IWMMXT_REGS && to != IWMMXT_REGS)
8715                || (from != IWMMXT_REGS && to == IWMMXT_REGS))
8716         return 4;
8717       else if (from == IWMMXT_GR_REGS || to == IWMMXT_GR_REGS)
8718         return 20;
8719       else
8720         return 2;
8721     }
8722   else
8723     {
8724       if (from == HI_REGS || to == HI_REGS)
8725         return 4;
8726       else
8727         return 2;
8728     }
8729 }
8730
8731 /* Implement TARGET_MEMORY_MOVE_COST.  */
8732
8733 int
8734 arm_memory_move_cost (enum machine_mode mode, reg_class_t rclass,
8735                       bool in ATTRIBUTE_UNUSED)
8736 {
8737   if (TARGET_32BIT)
8738     return 10;
8739   else
8740     {
8741       if (GET_MODE_SIZE (mode) < 4)
8742         return 8;
8743       else
8744         return ((2 * GET_MODE_SIZE (mode)) * (rclass == LO_REGS ? 1 : 2));
8745     }
8746 }
8747
8748 /* Vectorizer cost model implementation.  */
8749
8750 /* Implement targetm.vectorize.builtin_vectorization_cost.  */
8751 static int
8752 arm_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
8753                                 tree vectype,
8754                                 int misalign ATTRIBUTE_UNUSED)
8755 {
8756   unsigned elements;
8757
8758   switch (type_of_cost)
8759     {
8760       case scalar_stmt:
8761         return current_tune->vec_costs->scalar_stmt_cost;
8762
8763       case scalar_load:
8764         return current_tune->vec_costs->scalar_load_cost;
8765
8766       case scalar_store:
8767         return current_tune->vec_costs->scalar_store_cost;
8768
8769       case vector_stmt:
8770         return current_tune->vec_costs->vec_stmt_cost;
8771
8772       case vector_load:
8773         return current_tune->vec_costs->vec_align_load_cost;
8774
8775       case vector_store:
8776         return current_tune->vec_costs->vec_store_cost;
8777
8778       case vec_to_scalar:
8779         return current_tune->vec_costs->vec_to_scalar_cost;
8780
8781       case scalar_to_vec:
8782         return current_tune->vec_costs->scalar_to_vec_cost;
8783
8784       case unaligned_load:
8785         return current_tune->vec_costs->vec_unalign_load_cost;
8786
8787       case unaligned_store:
8788         return current_tune->vec_costs->vec_unalign_store_cost;
8789
8790       case cond_branch_taken:
8791         return current_tune->vec_costs->cond_taken_branch_cost;
8792
8793       case cond_branch_not_taken:
8794         return current_tune->vec_costs->cond_not_taken_branch_cost;
8795
8796       case vec_perm:
8797       case vec_promote_demote:
8798         return current_tune->vec_costs->vec_stmt_cost;
8799
8800       case vec_construct:
8801         elements = TYPE_VECTOR_SUBPARTS (vectype);
8802         return elements / 2 + 1;
8803
8804       default:
8805         gcc_unreachable ();
8806     }
8807 }
8808
8809 /* Implement targetm.vectorize.add_stmt_cost.  */
8810
8811 static unsigned
8812 arm_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
8813                    struct _stmt_vec_info *stmt_info, int misalign,
8814                    enum vect_cost_model_location where)
8815 {
8816   unsigned *cost = (unsigned *) data;
8817   unsigned retval = 0;
8818
8819   if (flag_vect_cost_model)
8820     {
8821       tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
8822       int stmt_cost = arm_builtin_vectorization_cost (kind, vectype, misalign);
8823
8824       /* Statements in an inner loop relative to the loop being
8825          vectorized are weighted more heavily.  The value here is
8826          arbitrary and could potentially be improved with analysis.  */
8827       if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
8828         count *= 50;  /* FIXME.  */
8829
8830       retval = (unsigned) (count * stmt_cost);
8831       cost[where] += retval;
8832     }
8833
8834   return retval;
8835 }
8836
8837 /* Return true if and only if this insn can dual-issue only as older.  */
8838 static bool
8839 cortexa7_older_only (rtx insn)
8840 {
8841   if (recog_memoized (insn) < 0)
8842     return false;
8843
8844   if (get_attr_insn (insn) == INSN_MOV)
8845     return false;
8846
8847   switch (get_attr_type (insn))
8848     {
8849     case TYPE_ALU_REG:
8850     case TYPE_LOAD_BYTE:
8851     case TYPE_LOAD1:
8852     case TYPE_STORE1:
8853     case TYPE_FFARITHS:
8854     case TYPE_FADDS:
8855     case TYPE_FFARITHD:
8856     case TYPE_FADDD:
8857     case TYPE_FCPYS:
8858     case TYPE_F_CVT:
8859     case TYPE_FCMPS:
8860     case TYPE_FCMPD:
8861     case TYPE_FCONSTS:
8862     case TYPE_FCONSTD:
8863     case TYPE_FMULS:
8864     case TYPE_FMACS:
8865     case TYPE_FMULD:
8866     case TYPE_FMACD:
8867     case TYPE_FDIVS:
8868     case TYPE_FDIVD:
8869     case TYPE_F_2_R:
8870     case TYPE_F_FLAG:
8871     case TYPE_F_LOADS:
8872     case TYPE_F_STORES:
8873       return true;
8874     default:
8875       return false;
8876     }
8877 }
8878
8879 /* Return true if and only if this insn can dual-issue as younger.  */
8880 static bool
8881 cortexa7_younger (FILE *file, int verbose, rtx insn)
8882 {
8883   if (recog_memoized (insn) < 0)
8884     {
8885       if (verbose > 5)
8886         fprintf (file, ";; not cortexa7_younger %d\n", INSN_UID (insn));
8887       return false;
8888     }
8889
8890   if (get_attr_insn (insn) == INSN_MOV)
8891     return true;
8892
8893   switch (get_attr_type (insn))
8894     {
8895     case TYPE_SIMPLE_ALU_IMM:
8896     case TYPE_SIMPLE_ALU_SHIFT:
8897     case TYPE_BRANCH:
8898     case TYPE_CALL:
8899       return true;
8900     default:
8901       return false;
8902     }
8903 }
8904
8905
8906 /* Look for an instruction that can dual issue only as an older
8907    instruction, and move it in front of any instructions that can
8908    dual-issue as younger, while preserving the relative order of all
8909    other instructions in the ready list.  This is a hueuristic to help
8910    dual-issue in later cycles, by postponing issue of more flexible
8911    instructions.  This heuristic may affect dual issue opportunities
8912    in the current cycle.  */
8913 static void
8914 cortexa7_sched_reorder (FILE *file, int verbose, rtx *ready, int *n_readyp,
8915                         int clock)
8916 {
8917   int i;
8918   int first_older_only = -1, first_younger = -1;
8919
8920   if (verbose > 5)
8921     fprintf (file,
8922              ";; sched_reorder for cycle %d with %d insns in ready list\n",
8923              clock,
8924              *n_readyp);
8925
8926   /* Traverse the ready list from the head (the instruction to issue
8927      first), and looking for the first instruction that can issue as
8928      younger and the first instruction that can dual-issue only as
8929      older.  */
8930   for (i = *n_readyp - 1; i >= 0; i--)
8931     {
8932       rtx insn = ready[i];
8933       if (cortexa7_older_only (insn))
8934         {
8935           first_older_only = i;
8936           if (verbose > 5)
8937             fprintf (file, ";; reorder older found %d\n", INSN_UID (insn));
8938           break;
8939         }
8940       else if (cortexa7_younger (file, verbose, insn) && first_younger == -1)
8941         first_younger = i;
8942     }
8943
8944   /* Nothing to reorder because either no younger insn found or insn
8945      that can dual-issue only as older appears before any insn that
8946      can dual-issue as younger.  */
8947   if (first_younger == -1)
8948     {
8949       if (verbose > 5)
8950         fprintf (file, ";; sched_reorder nothing to reorder as no younger\n");
8951       return;
8952     }
8953
8954   /* Nothing to reorder because no older-only insn in the ready list.  */
8955   if (first_older_only == -1)
8956     {
8957       if (verbose > 5)
8958         fprintf (file, ";; sched_reorder nothing to reorder as no older_only\n");
8959       return;
8960     }
8961
8962   /* Move first_older_only insn before first_younger.  */
8963   if (verbose > 5)
8964     fprintf (file, ";; cortexa7_sched_reorder insn %d before %d\n",
8965              INSN_UID(ready [first_older_only]),
8966              INSN_UID(ready [first_younger]));
8967   rtx first_older_only_insn = ready [first_older_only];
8968   for (i = first_older_only; i < first_younger; i++)
8969     {
8970       ready[i] = ready[i+1];
8971     }
8972
8973   ready[i] = first_older_only_insn;
8974   return;
8975 }
8976
8977 /* Implement TARGET_SCHED_REORDER. */
8978 static int
8979 arm_sched_reorder (FILE *file, int verbose, rtx *ready, int *n_readyp,
8980                    int clock)
8981 {
8982   switch (arm_tune)
8983     {
8984     case cortexa7:
8985       cortexa7_sched_reorder (file, verbose, ready, n_readyp, clock);
8986       break;
8987     default:
8988       /* Do nothing for other cores.  */
8989       break;
8990     }
8991
8992   return arm_issue_rate ();
8993 }
8994
8995 /* This function implements the target macro TARGET_SCHED_ADJUST_COST.
8996    It corrects the value of COST based on the relationship between
8997    INSN and DEP through the dependence LINK.  It returns the new
8998    value. There is a per-core adjust_cost hook to adjust scheduler costs
8999    and the per-core hook can choose to completely override the generic
9000    adjust_cost function. Only put bits of code into arm_adjust_cost that
9001    are common across all cores.  */
9002 static int
9003 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
9004 {
9005   rtx i_pat, d_pat;
9006
9007  /* When generating Thumb-1 code, we want to place flag-setting operations
9008     close to a conditional branch which depends on them, so that we can
9009     omit the comparison. */
9010   if (TARGET_THUMB1
9011       && REG_NOTE_KIND (link) == 0
9012       && recog_memoized (insn) == CODE_FOR_cbranchsi4_insn
9013       && recog_memoized (dep) >= 0
9014       && get_attr_conds (dep) == CONDS_SET)
9015     return 0;
9016
9017   if (current_tune->sched_adjust_cost != NULL)
9018     {
9019       if (!current_tune->sched_adjust_cost (insn, link, dep, &cost))
9020         return cost;
9021     }
9022
9023   /* XXX Is this strictly true?  */
9024   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
9025       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
9026     return 0;
9027
9028   /* Call insns don't incur a stall, even if they follow a load.  */
9029   if (REG_NOTE_KIND (link) == 0
9030       && CALL_P (insn))
9031     return 1;
9032
9033   if ((i_pat = single_set (insn)) != NULL
9034       && MEM_P (SET_SRC (i_pat))
9035       && (d_pat = single_set (dep)) != NULL
9036       && MEM_P (SET_DEST (d_pat)))
9037     {
9038       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
9039       /* This is a load after a store, there is no conflict if the load reads
9040          from a cached area.  Assume that loads from the stack, and from the
9041          constant pool are cached, and that others will miss.  This is a
9042          hack.  */
9043
9044       if ((GET_CODE (src_mem) == SYMBOL_REF
9045            && CONSTANT_POOL_ADDRESS_P (src_mem))
9046           || reg_mentioned_p (stack_pointer_rtx, src_mem)
9047           || reg_mentioned_p (frame_pointer_rtx, src_mem)
9048           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
9049         return 1;
9050     }
9051
9052   return cost;
9053 }
9054
9055 static int
9056 arm_default_branch_cost (bool speed_p, bool predictable_p ATTRIBUTE_UNUSED)
9057 {
9058   if (TARGET_32BIT)
9059     return (TARGET_THUMB2 && !speed_p) ? 1 : 4;
9060   else
9061     return (optimize > 0) ? 2 : 0;
9062 }
9063
9064 static int
9065 arm_cortex_a5_branch_cost (bool speed_p, bool predictable_p)
9066 {
9067   return speed_p ? 0 : arm_default_branch_cost (speed_p, predictable_p);
9068 }
9069
9070 static bool fp_consts_inited = false;
9071
9072 static REAL_VALUE_TYPE value_fp0;
9073
9074 static void
9075 init_fp_table (void)
9076 {
9077   REAL_VALUE_TYPE r;
9078
9079   r = REAL_VALUE_ATOF ("0", DFmode);
9080   value_fp0 = r;
9081   fp_consts_inited = true;
9082 }
9083
9084 /* Return TRUE if rtx X is a valid immediate FP constant.  */
9085 int
9086 arm_const_double_rtx (rtx x)
9087 {
9088   REAL_VALUE_TYPE r;
9089
9090   if (!fp_consts_inited)
9091     init_fp_table ();
9092
9093   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
9094   if (REAL_VALUE_MINUS_ZERO (r))
9095     return 0;
9096
9097   if (REAL_VALUES_EQUAL (r, value_fp0))
9098     return 1;
9099
9100   return 0;
9101 }
9102
9103 /* VFPv3 has a fairly wide range of representable immediates, formed from
9104    "quarter-precision" floating-point values. These can be evaluated using this
9105    formula (with ^ for exponentiation):
9106
9107      -1^s * n * 2^-r
9108
9109    Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
9110    16 <= n <= 31 and 0 <= r <= 7.
9111
9112    These values are mapped onto an 8-bit integer ABCDEFGH s.t.
9113
9114      - A (most-significant) is the sign bit.
9115      - BCD are the exponent (encoded as r XOR 3).
9116      - EFGH are the mantissa (encoded as n - 16).
9117 */
9118
9119 /* Return an integer index for a VFPv3 immediate operand X suitable for the
9120    fconst[sd] instruction, or -1 if X isn't suitable.  */
9121 static int
9122 vfp3_const_double_index (rtx x)
9123 {
9124   REAL_VALUE_TYPE r, m;
9125   int sign, exponent;
9126   unsigned HOST_WIDE_INT mantissa, mant_hi;
9127   unsigned HOST_WIDE_INT mask;
9128   HOST_WIDE_INT m1, m2;
9129   int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
9130
9131   if (!TARGET_VFP3 || !CONST_DOUBLE_P (x))
9132     return -1;
9133
9134   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
9135
9136   /* We can't represent these things, so detect them first.  */
9137   if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
9138     return -1;
9139
9140   /* Extract sign, exponent and mantissa.  */
9141   sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
9142   r = real_value_abs (&r);
9143   exponent = REAL_EXP (&r);
9144   /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
9145      highest (sign) bit, with a fixed binary point at bit point_pos.
9146      WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
9147      bits for the mantissa, this may fail (low bits would be lost).  */
9148   real_ldexp (&m, &r, point_pos - exponent);
9149   REAL_VALUE_TO_INT (&m1, &m2, m);
9150   mantissa = m1;
9151   mant_hi = m2;
9152
9153   /* If there are bits set in the low part of the mantissa, we can't
9154      represent this value.  */
9155   if (mantissa != 0)
9156     return -1;
9157
9158   /* Now make it so that mantissa contains the most-significant bits, and move
9159      the point_pos to indicate that the least-significant bits have been
9160      discarded.  */
9161   point_pos -= HOST_BITS_PER_WIDE_INT;
9162   mantissa = mant_hi;
9163
9164   /* We can permit four significant bits of mantissa only, plus a high bit
9165      which is always 1.  */
9166   mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
9167   if ((mantissa & mask) != 0)
9168     return -1;
9169
9170   /* Now we know the mantissa is in range, chop off the unneeded bits.  */
9171   mantissa >>= point_pos - 5;
9172
9173   /* The mantissa may be zero. Disallow that case. (It's possible to load the
9174      floating-point immediate zero with Neon using an integer-zero load, but
9175      that case is handled elsewhere.)  */
9176   if (mantissa == 0)
9177     return -1;
9178
9179   gcc_assert (mantissa >= 16 && mantissa <= 31);
9180
9181   /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
9182      normalized significands are in the range [1, 2). (Our mantissa is shifted
9183      left 4 places at this point relative to normalized IEEE754 values).  GCC
9184      internally uses [0.5, 1) (see real.c), so the exponent returned from
9185      REAL_EXP must be altered.  */
9186   exponent = 5 - exponent;
9187
9188   if (exponent < 0 || exponent > 7)
9189     return -1;
9190
9191   /* Sign, mantissa and exponent are now in the correct form to plug into the
9192      formula described in the comment above.  */
9193   return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
9194 }
9195
9196 /* Return TRUE if rtx X is a valid immediate VFPv3 constant.  */
9197 int
9198 vfp3_const_double_rtx (rtx x)
9199 {
9200   if (!TARGET_VFP3)
9201     return 0;
9202
9203   return vfp3_const_double_index (x) != -1;
9204 }
9205
9206 /* Recognize immediates which can be used in various Neon instructions. Legal
9207    immediates are described by the following table (for VMVN variants, the
9208    bitwise inverse of the constant shown is recognized. In either case, VMOV
9209    is output and the correct instruction to use for a given constant is chosen
9210    by the assembler). The constant shown is replicated across all elements of
9211    the destination vector.
9212
9213    insn elems variant constant (binary)
9214    ---- ----- ------- -----------------
9215    vmov  i32     0    00000000 00000000 00000000 abcdefgh
9216    vmov  i32     1    00000000 00000000 abcdefgh 00000000
9217    vmov  i32     2    00000000 abcdefgh 00000000 00000000
9218    vmov  i32     3    abcdefgh 00000000 00000000 00000000
9219    vmov  i16     4    00000000 abcdefgh
9220    vmov  i16     5    abcdefgh 00000000
9221    vmvn  i32     6    00000000 00000000 00000000 abcdefgh
9222    vmvn  i32     7    00000000 00000000 abcdefgh 00000000
9223    vmvn  i32     8    00000000 abcdefgh 00000000 00000000
9224    vmvn  i32     9    abcdefgh 00000000 00000000 00000000
9225    vmvn  i16    10    00000000 abcdefgh
9226    vmvn  i16    11    abcdefgh 00000000
9227    vmov  i32    12    00000000 00000000 abcdefgh 11111111
9228    vmvn  i32    13    00000000 00000000 abcdefgh 11111111
9229    vmov  i32    14    00000000 abcdefgh 11111111 11111111
9230    vmvn  i32    15    00000000 abcdefgh 11111111 11111111
9231    vmov   i8    16    abcdefgh
9232    vmov  i64    17    aaaaaaaa bbbbbbbb cccccccc dddddddd
9233                       eeeeeeee ffffffff gggggggg hhhhhhhh
9234    vmov  f32    18    aBbbbbbc defgh000 00000000 00000000
9235    vmov  f32    19    00000000 00000000 00000000 00000000
9236
9237    For case 18, B = !b. Representable values are exactly those accepted by
9238    vfp3_const_double_index, but are output as floating-point numbers rather
9239    than indices.
9240
9241    For case 19, we will change it to vmov.i32 when assembling.
9242
9243    Variants 0-5 (inclusive) may also be used as immediates for the second
9244    operand of VORR/VBIC instructions.
9245
9246    The INVERSE argument causes the bitwise inverse of the given operand to be
9247    recognized instead (used for recognizing legal immediates for the VAND/VORN
9248    pseudo-instructions). If INVERSE is true, the value placed in *MODCONST is
9249    *not* inverted (i.e. the pseudo-instruction forms vand/vorn should still be
9250    output, rather than the real insns vbic/vorr).
9251
9252    INVERSE makes no difference to the recognition of float vectors.
9253
9254    The return value is the variant of immediate as shown in the above table, or
9255    -1 if the given value doesn't match any of the listed patterns.
9256 */
9257 static int
9258 neon_valid_immediate (rtx op, enum machine_mode mode, int inverse,
9259                       rtx *modconst, int *elementwidth)
9260 {
9261 #define CHECK(STRIDE, ELSIZE, CLASS, TEST)      \
9262   matches = 1;                                  \
9263   for (i = 0; i < idx; i += (STRIDE))           \
9264     if (!(TEST))                                \
9265       matches = 0;                              \
9266   if (matches)                                  \
9267     {                                           \
9268       immtype = (CLASS);                        \
9269       elsize = (ELSIZE);                        \
9270       break;                                    \
9271     }
9272
9273   unsigned int i, elsize = 0, idx = 0, n_elts;
9274   unsigned int innersize;
9275   unsigned char bytes[16];
9276   int immtype = -1, matches;
9277   unsigned int invmask = inverse ? 0xff : 0;
9278   bool vector = GET_CODE (op) == CONST_VECTOR;
9279
9280   if (vector)
9281     {
9282       n_elts = CONST_VECTOR_NUNITS (op);
9283       innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
9284     }
9285   else
9286     {
9287       n_elts = 1;
9288       if (mode == VOIDmode)
9289         mode = DImode;
9290       innersize = GET_MODE_SIZE (mode);
9291     }
9292
9293   /* Vectors of float constants.  */
9294   if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
9295     {
9296       rtx el0 = CONST_VECTOR_ELT (op, 0);
9297       REAL_VALUE_TYPE r0;
9298
9299       if (!vfp3_const_double_rtx (el0) && el0 != CONST0_RTX (GET_MODE (el0)))
9300         return -1;
9301
9302       REAL_VALUE_FROM_CONST_DOUBLE (r0, el0);
9303
9304       for (i = 1; i < n_elts; i++)
9305         {
9306           rtx elt = CONST_VECTOR_ELT (op, i);
9307           REAL_VALUE_TYPE re;
9308
9309           REAL_VALUE_FROM_CONST_DOUBLE (re, elt);
9310
9311           if (!REAL_VALUES_EQUAL (r0, re))
9312             return -1;
9313         }
9314
9315       if (modconst)
9316         *modconst = CONST_VECTOR_ELT (op, 0);
9317
9318       if (elementwidth)
9319         *elementwidth = 0;
9320
9321       if (el0 == CONST0_RTX (GET_MODE (el0)))
9322         return 19;
9323       else
9324         return 18;
9325     }
9326
9327   /* Splat vector constant out into a byte vector.  */
9328   for (i = 0; i < n_elts; i++)
9329     {
9330       rtx el = vector ? CONST_VECTOR_ELT (op, i) : op;
9331       unsigned HOST_WIDE_INT elpart;
9332       unsigned int part, parts;
9333
9334       if (CONST_INT_P (el))
9335         {
9336           elpart = INTVAL (el);
9337           parts = 1;
9338         }
9339       else if (CONST_DOUBLE_P (el))
9340         {
9341           elpart = CONST_DOUBLE_LOW (el);
9342           parts = 2;
9343         }
9344       else
9345         gcc_unreachable ();
9346
9347       for (part = 0; part < parts; part++)
9348         {
9349           unsigned int byte;
9350           for (byte = 0; byte < innersize; byte++)
9351             {
9352               bytes[idx++] = (elpart & 0xff) ^ invmask;
9353               elpart >>= BITS_PER_UNIT;
9354             }
9355           if (CONST_DOUBLE_P (el))
9356             elpart = CONST_DOUBLE_HIGH (el);
9357         }
9358     }
9359
9360   /* Sanity check.  */
9361   gcc_assert (idx == GET_MODE_SIZE (mode));
9362
9363   do
9364     {
9365       CHECK (4, 32, 0, bytes[i] == bytes[0] && bytes[i + 1] == 0
9366                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
9367
9368       CHECK (4, 32, 1, bytes[i] == 0 && bytes[i + 1] == bytes[1]
9369                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
9370
9371       CHECK (4, 32, 2, bytes[i] == 0 && bytes[i + 1] == 0
9372                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
9373
9374       CHECK (4, 32, 3, bytes[i] == 0 && bytes[i + 1] == 0
9375                        && bytes[i + 2] == 0 && bytes[i + 3] == bytes[3]);
9376
9377       CHECK (2, 16, 4, bytes[i] == bytes[0] && bytes[i + 1] == 0);
9378
9379       CHECK (2, 16, 5, bytes[i] == 0 && bytes[i + 1] == bytes[1]);
9380
9381       CHECK (4, 32, 6, bytes[i] == bytes[0] && bytes[i + 1] == 0xff
9382                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
9383
9384       CHECK (4, 32, 7, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
9385                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
9386
9387       CHECK (4, 32, 8, bytes[i] == 0xff && bytes[i + 1] == 0xff
9388                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
9389
9390       CHECK (4, 32, 9, bytes[i] == 0xff && bytes[i + 1] == 0xff
9391                        && bytes[i + 2] == 0xff && bytes[i + 3] == bytes[3]);
9392
9393       CHECK (2, 16, 10, bytes[i] == bytes[0] && bytes[i + 1] == 0xff);
9394
9395       CHECK (2, 16, 11, bytes[i] == 0xff && bytes[i + 1] == bytes[1]);
9396
9397       CHECK (4, 32, 12, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
9398                         && bytes[i + 2] == 0 && bytes[i + 3] == 0);
9399
9400       CHECK (4, 32, 13, bytes[i] == 0 && bytes[i + 1] == bytes[1]
9401                         && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
9402
9403       CHECK (4, 32, 14, bytes[i] == 0xff && bytes[i + 1] == 0xff
9404                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
9405
9406       CHECK (4, 32, 15, bytes[i] == 0 && bytes[i + 1] == 0
9407                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
9408
9409       CHECK (1, 8, 16, bytes[i] == bytes[0]);
9410
9411       CHECK (1, 64, 17, (bytes[i] == 0 || bytes[i] == 0xff)
9412                         && bytes[i] == bytes[(i + 8) % idx]);
9413     }
9414   while (0);
9415
9416   if (immtype == -1)
9417     return -1;
9418
9419   if (elementwidth)
9420     *elementwidth = elsize;
9421
9422   if (modconst)
9423     {
9424       unsigned HOST_WIDE_INT imm = 0;
9425
9426       /* Un-invert bytes of recognized vector, if necessary.  */
9427       if (invmask != 0)
9428         for (i = 0; i < idx; i++)
9429           bytes[i] ^= invmask;
9430
9431       if (immtype == 17)
9432         {
9433           /* FIXME: Broken on 32-bit H_W_I hosts.  */
9434           gcc_assert (sizeof (HOST_WIDE_INT) == 8);
9435
9436           for (i = 0; i < 8; i++)
9437             imm |= (unsigned HOST_WIDE_INT) (bytes[i] ? 0xff : 0)
9438                    << (i * BITS_PER_UNIT);
9439
9440           *modconst = GEN_INT (imm);
9441         }
9442       else
9443         {
9444           unsigned HOST_WIDE_INT imm = 0;
9445
9446           for (i = 0; i < elsize / BITS_PER_UNIT; i++)
9447             imm |= (unsigned HOST_WIDE_INT) bytes[i] << (i * BITS_PER_UNIT);
9448
9449           *modconst = GEN_INT (imm);
9450         }
9451     }
9452
9453   return immtype;
9454 #undef CHECK
9455 }
9456
9457 /* Return TRUE if rtx X is legal for use as either a Neon VMOV (or, implicitly,
9458    VMVN) immediate. Write back width per element to *ELEMENTWIDTH (or zero for
9459    float elements), and a modified constant (whatever should be output for a
9460    VMOV) in *MODCONST.  */
9461
9462 int
9463 neon_immediate_valid_for_move (rtx op, enum machine_mode mode,
9464                                rtx *modconst, int *elementwidth)
9465 {
9466   rtx tmpconst;
9467   int tmpwidth;
9468   int retval = neon_valid_immediate (op, mode, 0, &tmpconst, &tmpwidth);
9469
9470   if (retval == -1)
9471     return 0;
9472
9473   if (modconst)
9474     *modconst = tmpconst;
9475
9476   if (elementwidth)
9477     *elementwidth = tmpwidth;
9478
9479   return 1;
9480 }
9481
9482 /* Return TRUE if rtx X is legal for use in a VORR or VBIC instruction.  If
9483    the immediate is valid, write a constant suitable for using as an operand
9484    to VORR/VBIC/VAND/VORN to *MODCONST and the corresponding element width to
9485    *ELEMENTWIDTH. See neon_valid_immediate for description of INVERSE.  */
9486
9487 int
9488 neon_immediate_valid_for_logic (rtx op, enum machine_mode mode, int inverse,
9489                                 rtx *modconst, int *elementwidth)
9490 {
9491   rtx tmpconst;
9492   int tmpwidth;
9493   int retval = neon_valid_immediate (op, mode, inverse, &tmpconst, &tmpwidth);
9494
9495   if (retval < 0 || retval > 5)
9496     return 0;
9497
9498   if (modconst)
9499     *modconst = tmpconst;
9500
9501   if (elementwidth)
9502     *elementwidth = tmpwidth;
9503
9504   return 1;
9505 }
9506
9507 /* Return TRUE if rtx OP is legal for use in a VSHR or VSHL instruction.  If
9508    the immediate is valid, write a constant suitable for using as an operand
9509    to VSHR/VSHL to *MODCONST and the corresponding element width to
9510    *ELEMENTWIDTH. ISLEFTSHIFT is for determine left or right shift,
9511    because they have different limitations.  */
9512
9513 int
9514 neon_immediate_valid_for_shift (rtx op, enum machine_mode mode,
9515                                 rtx *modconst, int *elementwidth,
9516                                 bool isleftshift)
9517 {
9518   unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
9519   unsigned int n_elts = CONST_VECTOR_NUNITS (op), i;
9520   unsigned HOST_WIDE_INT last_elt = 0;
9521   unsigned HOST_WIDE_INT maxshift;
9522
9523   /* Split vector constant out into a byte vector.  */
9524   for (i = 0; i < n_elts; i++)
9525     {
9526       rtx el = CONST_VECTOR_ELT (op, i);
9527       unsigned HOST_WIDE_INT elpart;
9528
9529       if (CONST_INT_P (el))
9530         elpart = INTVAL (el);
9531       else if (CONST_DOUBLE_P (el))
9532         return 0;
9533       else
9534         gcc_unreachable ();
9535
9536       if (i != 0 && elpart != last_elt)
9537         return 0;
9538
9539       last_elt = elpart;
9540     }
9541
9542   /* Shift less than element size.  */
9543   maxshift = innersize * 8;
9544
9545   if (isleftshift)
9546     {
9547       /* Left shift immediate value can be from 0 to <size>-1.  */
9548       if (last_elt >= maxshift)
9549         return 0;
9550     }
9551   else
9552     {
9553       /* Right shift immediate value can be from 1 to <size>.  */
9554       if (last_elt == 0 || last_elt > maxshift)
9555         return 0;
9556     }
9557
9558   if (elementwidth)
9559     *elementwidth = innersize * 8;
9560
9561   if (modconst)
9562     *modconst = CONST_VECTOR_ELT (op, 0);
9563
9564   return 1;
9565 }
9566
9567 /* Return a string suitable for output of Neon immediate logic operation
9568    MNEM.  */
9569
9570 char *
9571 neon_output_logic_immediate (const char *mnem, rtx *op2, enum machine_mode mode,
9572                              int inverse, int quad)
9573 {
9574   int width, is_valid;
9575   static char templ[40];
9576
9577   is_valid = neon_immediate_valid_for_logic (*op2, mode, inverse, op2, &width);
9578
9579   gcc_assert (is_valid != 0);
9580
9581   if (quad)
9582     sprintf (templ, "%s.i%d\t%%q0, %%2", mnem, width);
9583   else
9584     sprintf (templ, "%s.i%d\t%%P0, %%2", mnem, width);
9585
9586   return templ;
9587 }
9588
9589 /* Return a string suitable for output of Neon immediate shift operation
9590    (VSHR or VSHL) MNEM.  */
9591
9592 char *
9593 neon_output_shift_immediate (const char *mnem, char sign, rtx *op2,
9594                              enum machine_mode mode, int quad,
9595                              bool isleftshift)
9596 {
9597   int width, is_valid;
9598   static char templ[40];
9599
9600   is_valid = neon_immediate_valid_for_shift (*op2, mode, op2, &width, isleftshift);
9601   gcc_assert (is_valid != 0);
9602
9603   if (quad)
9604     sprintf (templ, "%s.%c%d\t%%q0, %%q1, %%2", mnem, sign, width);
9605   else
9606     sprintf (templ, "%s.%c%d\t%%P0, %%P1, %%2", mnem, sign, width);
9607
9608   return templ;
9609 }
9610
9611 /* Output a sequence of pairwise operations to implement a reduction.
9612    NOTE: We do "too much work" here, because pairwise operations work on two
9613    registers-worth of operands in one go. Unfortunately we can't exploit those
9614    extra calculations to do the full operation in fewer steps, I don't think.
9615    Although all vector elements of the result but the first are ignored, we
9616    actually calculate the same result in each of the elements. An alternative
9617    such as initially loading a vector with zero to use as each of the second
9618    operands would use up an additional register and take an extra instruction,
9619    for no particular gain.  */
9620
9621 void
9622 neon_pairwise_reduce (rtx op0, rtx op1, enum machine_mode mode,
9623                       rtx (*reduc) (rtx, rtx, rtx))
9624 {
9625   enum machine_mode inner = GET_MODE_INNER (mode);
9626   unsigned int i, parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (inner);
9627   rtx tmpsum = op1;
9628
9629   for (i = parts / 2; i >= 1; i /= 2)
9630     {
9631       rtx dest = (i == 1) ? op0 : gen_reg_rtx (mode);
9632       emit_insn (reduc (dest, tmpsum, tmpsum));
9633       tmpsum = dest;
9634     }
9635 }
9636
9637 /* If VALS is a vector constant that can be loaded into a register
9638    using VDUP, generate instructions to do so and return an RTX to
9639    assign to the register.  Otherwise return NULL_RTX.  */
9640
9641 static rtx
9642 neon_vdup_constant (rtx vals)
9643 {
9644   enum machine_mode mode = GET_MODE (vals);
9645   enum machine_mode inner_mode = GET_MODE_INNER (mode);
9646   int n_elts = GET_MODE_NUNITS (mode);
9647   bool all_same = true;
9648   rtx x;
9649   int i;
9650
9651   if (GET_CODE (vals) != CONST_VECTOR || GET_MODE_SIZE (inner_mode) > 4)
9652     return NULL_RTX;
9653
9654   for (i = 0; i < n_elts; ++i)
9655     {
9656       x = XVECEXP (vals, 0, i);
9657       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
9658         all_same = false;
9659     }
9660
9661   if (!all_same)
9662     /* The elements are not all the same.  We could handle repeating
9663        patterns of a mode larger than INNER_MODE here (e.g. int8x8_t
9664        {0, C, 0, C, 0, C, 0, C} which can be loaded using
9665        vdup.i16).  */
9666     return NULL_RTX;
9667
9668   /* We can load this constant by using VDUP and a constant in a
9669      single ARM register.  This will be cheaper than a vector
9670      load.  */
9671
9672   x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
9673   return gen_rtx_VEC_DUPLICATE (mode, x);
9674 }
9675
9676 /* Generate code to load VALS, which is a PARALLEL containing only
9677    constants (for vec_init) or CONST_VECTOR, efficiently into a
9678    register.  Returns an RTX to copy into the register, or NULL_RTX
9679    for a PARALLEL that can not be converted into a CONST_VECTOR.  */
9680
9681 rtx
9682 neon_make_constant (rtx vals)
9683 {
9684   enum machine_mode mode = GET_MODE (vals);
9685   rtx target;
9686   rtx const_vec = NULL_RTX;
9687   int n_elts = GET_MODE_NUNITS (mode);
9688   int n_const = 0;
9689   int i;
9690
9691   if (GET_CODE (vals) == CONST_VECTOR)
9692     const_vec = vals;
9693   else if (GET_CODE (vals) == PARALLEL)
9694     {
9695       /* A CONST_VECTOR must contain only CONST_INTs and
9696          CONST_DOUBLEs, but CONSTANT_P allows more (e.g. SYMBOL_REF).
9697          Only store valid constants in a CONST_VECTOR.  */
9698       for (i = 0; i < n_elts; ++i)
9699         {
9700           rtx x = XVECEXP (vals, 0, i);
9701           if (CONST_INT_P (x) || CONST_DOUBLE_P (x))
9702             n_const++;
9703         }
9704       if (n_const == n_elts)
9705         const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
9706     }
9707   else
9708     gcc_unreachable ();
9709
9710   if (const_vec != NULL
9711       && neon_immediate_valid_for_move (const_vec, mode, NULL, NULL))
9712     /* Load using VMOV.  On Cortex-A8 this takes one cycle.  */
9713     return const_vec;
9714   else if ((target = neon_vdup_constant (vals)) != NULL_RTX)
9715     /* Loaded using VDUP.  On Cortex-A8 the VDUP takes one NEON
9716        pipeline cycle; creating the constant takes one or two ARM
9717        pipeline cycles.  */
9718     return target;
9719   else if (const_vec != NULL_RTX)
9720     /* Load from constant pool.  On Cortex-A8 this takes two cycles
9721        (for either double or quad vectors).  We can not take advantage
9722        of single-cycle VLD1 because we need a PC-relative addressing
9723        mode.  */
9724     return const_vec;
9725   else
9726     /* A PARALLEL containing something not valid inside CONST_VECTOR.
9727        We can not construct an initializer.  */
9728     return NULL_RTX;
9729 }
9730
9731 /* Initialize vector TARGET to VALS.  */
9732
9733 void
9734 neon_expand_vector_init (rtx target, rtx vals)
9735 {
9736   enum machine_mode mode = GET_MODE (target);
9737   enum machine_mode inner_mode = GET_MODE_INNER (mode);
9738   int n_elts = GET_MODE_NUNITS (mode);
9739   int n_var = 0, one_var = -1;
9740   bool all_same = true;
9741   rtx x, mem;
9742   int i;
9743
9744   for (i = 0; i < n_elts; ++i)
9745     {
9746       x = XVECEXP (vals, 0, i);
9747       if (!CONSTANT_P (x))
9748         ++n_var, one_var = i;
9749
9750       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
9751         all_same = false;
9752     }
9753
9754   if (n_var == 0)
9755     {
9756       rtx constant = neon_make_constant (vals);
9757       if (constant != NULL_RTX)
9758         {
9759           emit_move_insn (target, constant);
9760           return;
9761         }
9762     }
9763
9764   /* Splat a single non-constant element if we can.  */
9765   if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
9766     {
9767       x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, 0));
9768       emit_insn (gen_rtx_SET (VOIDmode, target,
9769                               gen_rtx_VEC_DUPLICATE (mode, x)));
9770       return;
9771     }
9772
9773   /* One field is non-constant.  Load constant then overwrite varying
9774      field.  This is more efficient than using the stack.  */
9775   if (n_var == 1)
9776     {
9777       rtx copy = copy_rtx (vals);
9778       rtx index = GEN_INT (one_var);
9779
9780       /* Load constant part of vector, substitute neighboring value for
9781          varying element.  */
9782       XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
9783       neon_expand_vector_init (target, copy);
9784
9785       /* Insert variable.  */
9786       x = copy_to_mode_reg (inner_mode, XVECEXP (vals, 0, one_var));
9787       switch (mode)
9788         {
9789         case V8QImode:
9790           emit_insn (gen_neon_vset_lanev8qi (target, x, target, index));
9791           break;
9792         case V16QImode:
9793           emit_insn (gen_neon_vset_lanev16qi (target, x, target, index));
9794           break;
9795         case V4HImode:
9796           emit_insn (gen_neon_vset_lanev4hi (target, x, target, index));
9797           break;
9798         case V8HImode:
9799           emit_insn (gen_neon_vset_lanev8hi (target, x, target, index));
9800           break;
9801         case V2SImode:
9802           emit_insn (gen_neon_vset_lanev2si (target, x, target, index));
9803           break;
9804         case V4SImode:
9805           emit_insn (gen_neon_vset_lanev4si (target, x, target, index));
9806           break;
9807         case V2SFmode:
9808           emit_insn (gen_neon_vset_lanev2sf (target, x, target, index));
9809           break;
9810         case V4SFmode:
9811           emit_insn (gen_neon_vset_lanev4sf (target, x, target, index));
9812           break;
9813         case V2DImode:
9814           emit_insn (gen_neon_vset_lanev2di (target, x, target, index));
9815           break;
9816         default:
9817           gcc_unreachable ();
9818         }
9819       return;
9820     }
9821
9822   /* Construct the vector in memory one field at a time
9823      and load the whole vector.  */
9824   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
9825   for (i = 0; i < n_elts; i++)
9826     emit_move_insn (adjust_address_nv (mem, inner_mode,
9827                                     i * GET_MODE_SIZE (inner_mode)),
9828                     XVECEXP (vals, 0, i));
9829   emit_move_insn (target, mem);
9830 }
9831
9832 /* Ensure OPERAND lies between LOW (inclusive) and HIGH (exclusive).  Raise
9833    ERR if it doesn't.  FIXME: NEON bounds checks occur late in compilation, so
9834    reported source locations are bogus.  */
9835
9836 static void
9837 bounds_check (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high,
9838               const char *err)
9839 {
9840   HOST_WIDE_INT lane;
9841
9842   gcc_assert (CONST_INT_P (operand));
9843
9844   lane = INTVAL (operand);
9845
9846   if (lane < low || lane >= high)
9847     error (err);
9848 }
9849
9850 /* Bounds-check lanes.  */
9851
9852 void
9853 neon_lane_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
9854 {
9855   bounds_check (operand, low, high, "lane out of range");
9856 }
9857
9858 /* Bounds-check constants.  */
9859
9860 void
9861 neon_const_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
9862 {
9863   bounds_check (operand, low, high, "constant out of range");
9864 }
9865
9866 HOST_WIDE_INT
9867 neon_element_bits (enum machine_mode mode)
9868 {
9869   if (mode == DImode)
9870     return GET_MODE_BITSIZE (mode);
9871   else
9872     return GET_MODE_BITSIZE (GET_MODE_INNER (mode));
9873 }
9874
9875 \f
9876 /* Predicates for `match_operand' and `match_operator'.  */
9877
9878 /* Return TRUE if OP is a valid coprocessor memory address pattern.
9879    WB is true if full writeback address modes are allowed and is false
9880    if limited writeback address modes (POST_INC and PRE_DEC) are
9881    allowed.  */
9882
9883 int
9884 arm_coproc_mem_operand (rtx op, bool wb)
9885 {
9886   rtx ind;
9887
9888   /* Reject eliminable registers.  */
9889   if (! (reload_in_progress || reload_completed)
9890       && (   reg_mentioned_p (frame_pointer_rtx, op)
9891           || reg_mentioned_p (arg_pointer_rtx, op)
9892           || reg_mentioned_p (virtual_incoming_args_rtx, op)
9893           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
9894           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
9895           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
9896     return FALSE;
9897
9898   /* Constants are converted into offsets from labels.  */
9899   if (!MEM_P (op))
9900     return FALSE;
9901
9902   ind = XEXP (op, 0);
9903
9904   if (reload_completed
9905       && (GET_CODE (ind) == LABEL_REF
9906           || (GET_CODE (ind) == CONST
9907               && GET_CODE (XEXP (ind, 0)) == PLUS
9908               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
9909               && CONST_INT_P (XEXP (XEXP (ind, 0), 1)))))
9910     return TRUE;
9911
9912   /* Match: (mem (reg)).  */
9913   if (REG_P (ind))
9914     return arm_address_register_rtx_p (ind, 0);
9915
9916   /* Autoincremment addressing modes.  POST_INC and PRE_DEC are
9917      acceptable in any case (subject to verification by
9918      arm_address_register_rtx_p).  We need WB to be true to accept
9919      PRE_INC and POST_DEC.  */
9920   if (GET_CODE (ind) == POST_INC
9921       || GET_CODE (ind) == PRE_DEC
9922       || (wb
9923           && (GET_CODE (ind) == PRE_INC
9924               || GET_CODE (ind) == POST_DEC)))
9925     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
9926
9927   if (wb
9928       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
9929       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
9930       && GET_CODE (XEXP (ind, 1)) == PLUS
9931       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
9932     ind = XEXP (ind, 1);
9933
9934   /* Match:
9935      (plus (reg)
9936            (const)).  */
9937   if (GET_CODE (ind) == PLUS
9938       && REG_P (XEXP (ind, 0))
9939       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
9940       && CONST_INT_P (XEXP (ind, 1))
9941       && INTVAL (XEXP (ind, 1)) > -1024
9942       && INTVAL (XEXP (ind, 1)) <  1024
9943       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
9944     return TRUE;
9945
9946   return FALSE;
9947 }
9948
9949 /* Return TRUE if OP is a memory operand which we can load or store a vector
9950    to/from. TYPE is one of the following values:
9951     0 - Vector load/stor (vldr)
9952     1 - Core registers (ldm)
9953     2 - Element/structure loads (vld1)
9954  */
9955 int
9956 neon_vector_mem_operand (rtx op, int type)
9957 {
9958   rtx ind;
9959
9960   /* Reject eliminable registers.  */
9961   if (! (reload_in_progress || reload_completed)
9962       && (   reg_mentioned_p (frame_pointer_rtx, op)
9963           || reg_mentioned_p (arg_pointer_rtx, op)
9964           || reg_mentioned_p (virtual_incoming_args_rtx, op)
9965           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
9966           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
9967           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
9968     return FALSE;
9969
9970   /* Constants are converted into offsets from labels.  */
9971   if (!MEM_P (op))
9972     return FALSE;
9973
9974   ind = XEXP (op, 0);
9975
9976   if (reload_completed
9977       && (GET_CODE (ind) == LABEL_REF
9978           || (GET_CODE (ind) == CONST
9979               && GET_CODE (XEXP (ind, 0)) == PLUS
9980               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
9981               && CONST_INT_P (XEXP (XEXP (ind, 0), 1)))))
9982     return TRUE;
9983
9984   /* Match: (mem (reg)).  */
9985   if (REG_P (ind))
9986     return arm_address_register_rtx_p (ind, 0);
9987
9988   /* Allow post-increment with Neon registers.  */
9989   if ((type != 1 && GET_CODE (ind) == POST_INC)
9990       || (type == 0 && GET_CODE (ind) == PRE_DEC))
9991     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
9992
9993   /* FIXME: vld1 allows register post-modify.  */
9994
9995   /* Match:
9996      (plus (reg)
9997           (const)).  */
9998   if (type == 0
9999       && GET_CODE (ind) == PLUS
10000       && REG_P (XEXP (ind, 0))
10001       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
10002       && CONST_INT_P (XEXP (ind, 1))
10003       && INTVAL (XEXP (ind, 1)) > -1024
10004       /* For quad modes, we restrict the constant offset to be slightly less
10005          than what the instruction format permits.  We have no such constraint
10006          on double mode offsets.  (This must match arm_legitimate_index_p.)  */
10007       && (INTVAL (XEXP (ind, 1))
10008           < (VALID_NEON_QREG_MODE (GET_MODE (op))? 1016 : 1024))
10009       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
10010     return TRUE;
10011
10012   return FALSE;
10013 }
10014
10015 /* Return TRUE if OP is a mem suitable for loading/storing a Neon struct
10016    type.  */
10017 int
10018 neon_struct_mem_operand (rtx op)
10019 {
10020   rtx ind;
10021
10022   /* Reject eliminable registers.  */
10023   if (! (reload_in_progress || reload_completed)
10024       && (   reg_mentioned_p (frame_pointer_rtx, op)
10025           || reg_mentioned_p (arg_pointer_rtx, op)
10026           || reg_mentioned_p (virtual_incoming_args_rtx, op)
10027           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
10028           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
10029           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
10030     return FALSE;
10031
10032   /* Constants are converted into offsets from labels.  */
10033   if (!MEM_P (op))
10034     return FALSE;
10035
10036   ind = XEXP (op, 0);
10037
10038   if (reload_completed
10039       && (GET_CODE (ind) == LABEL_REF
10040           || (GET_CODE (ind) == CONST
10041               && GET_CODE (XEXP (ind, 0)) == PLUS
10042               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
10043               && CONST_INT_P (XEXP (XEXP (ind, 0), 1)))))
10044     return TRUE;
10045
10046   /* Match: (mem (reg)).  */
10047   if (REG_P (ind))
10048     return arm_address_register_rtx_p (ind, 0);
10049
10050   /* vldm/vstm allows POST_INC (ia) and PRE_DEC (db).  */
10051   if (GET_CODE (ind) == POST_INC
10052       || GET_CODE (ind) == PRE_DEC)
10053     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
10054
10055   return FALSE;
10056 }
10057
10058 /* Return true if X is a register that will be eliminated later on.  */
10059 int
10060 arm_eliminable_register (rtx x)
10061 {
10062   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
10063                        || REGNO (x) == ARG_POINTER_REGNUM
10064                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
10065                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
10066 }
10067
10068 /* Return GENERAL_REGS if a scratch register required to reload x to/from
10069    coprocessor registers.  Otherwise return NO_REGS.  */
10070
10071 enum reg_class
10072 coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
10073 {
10074   if (mode == HFmode)
10075     {
10076       if (!TARGET_NEON_FP16)
10077         return GENERAL_REGS;
10078       if (s_register_operand (x, mode) || neon_vector_mem_operand (x, 2))
10079         return NO_REGS;
10080       return GENERAL_REGS;
10081     }
10082
10083   /* The neon move patterns handle all legitimate vector and struct
10084      addresses.  */
10085   if (TARGET_NEON
10086       && (MEM_P (x) || GET_CODE (x) == CONST_VECTOR)
10087       && (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
10088           || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
10089           || VALID_NEON_STRUCT_MODE (mode)))
10090     return NO_REGS;
10091
10092   if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
10093     return NO_REGS;
10094
10095   return GENERAL_REGS;
10096 }
10097
10098 /* Values which must be returned in the most-significant end of the return
10099    register.  */
10100
10101 static bool
10102 arm_return_in_msb (const_tree valtype)
10103 {
10104   return (TARGET_AAPCS_BASED
10105           && BYTES_BIG_ENDIAN
10106           && (AGGREGATE_TYPE_P (valtype)
10107               || TREE_CODE (valtype) == COMPLEX_TYPE
10108               || FIXED_POINT_TYPE_P (valtype)));
10109 }
10110
10111 /* Return TRUE if X references a SYMBOL_REF.  */
10112 int
10113 symbol_mentioned_p (rtx x)
10114 {
10115   const char * fmt;
10116   int i;
10117
10118   if (GET_CODE (x) == SYMBOL_REF)
10119     return 1;
10120
10121   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
10122      are constant offsets, not symbols.  */
10123   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
10124     return 0;
10125
10126   fmt = GET_RTX_FORMAT (GET_CODE (x));
10127
10128   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
10129     {
10130       if (fmt[i] == 'E')
10131         {
10132           int j;
10133
10134           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10135             if (symbol_mentioned_p (XVECEXP (x, i, j)))
10136               return 1;
10137         }
10138       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
10139         return 1;
10140     }
10141
10142   return 0;
10143 }
10144
10145 /* Return TRUE if X references a LABEL_REF.  */
10146 int
10147 label_mentioned_p (rtx x)
10148 {
10149   const char * fmt;
10150   int i;
10151
10152   if (GET_CODE (x) == LABEL_REF)
10153     return 1;
10154
10155   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
10156      instruction, but they are constant offsets, not symbols.  */
10157   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
10158     return 0;
10159
10160   fmt = GET_RTX_FORMAT (GET_CODE (x));
10161   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
10162     {
10163       if (fmt[i] == 'E')
10164         {
10165           int j;
10166
10167           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10168             if (label_mentioned_p (XVECEXP (x, i, j)))
10169               return 1;
10170         }
10171       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
10172         return 1;
10173     }
10174
10175   return 0;
10176 }
10177
10178 int
10179 tls_mentioned_p (rtx x)
10180 {
10181   switch (GET_CODE (x))
10182     {
10183     case CONST:
10184       return tls_mentioned_p (XEXP (x, 0));
10185
10186     case UNSPEC:
10187       if (XINT (x, 1) == UNSPEC_TLS)
10188         return 1;
10189
10190     default:
10191       return 0;
10192     }
10193 }
10194
10195 /* Must not copy any rtx that uses a pc-relative address.  */
10196
10197 static int
10198 arm_note_pic_base (rtx *x, void *date ATTRIBUTE_UNUSED)
10199 {
10200   if (GET_CODE (*x) == UNSPEC
10201       && (XINT (*x, 1) == UNSPEC_PIC_BASE
10202           || XINT (*x, 1) == UNSPEC_PIC_UNIFIED))
10203     return 1;
10204   return 0;
10205 }
10206
10207 static bool
10208 arm_cannot_copy_insn_p (rtx insn)
10209 {
10210   /* The tls call insn cannot be copied, as it is paired with a data
10211      word.  */
10212   if (recog_memoized (insn) == CODE_FOR_tlscall)
10213     return true;
10214
10215   return for_each_rtx (&PATTERN (insn), arm_note_pic_base, NULL);
10216 }
10217
10218 enum rtx_code
10219 minmax_code (rtx x)
10220 {
10221   enum rtx_code code = GET_CODE (x);
10222
10223   switch (code)
10224     {
10225     case SMAX:
10226       return GE;
10227     case SMIN:
10228       return LE;
10229     case UMIN:
10230       return LEU;
10231     case UMAX:
10232       return GEU;
10233     default:
10234       gcc_unreachable ();
10235     }
10236 }
10237
10238 /* Match pair of min/max operators that can be implemented via usat/ssat.  */
10239
10240 bool
10241 arm_sat_operator_match (rtx lo_bound, rtx hi_bound,
10242                         int *mask, bool *signed_sat)
10243 {
10244   /* The high bound must be a power of two minus one.  */
10245   int log = exact_log2 (INTVAL (hi_bound) + 1);
10246   if (log == -1)
10247     return false;
10248
10249   /* The low bound is either zero (for usat) or one less than the
10250      negation of the high bound (for ssat).  */
10251   if (INTVAL (lo_bound) == 0)
10252     {
10253       if (mask)
10254         *mask = log;
10255       if (signed_sat)
10256         *signed_sat = false;
10257
10258       return true;
10259     }
10260
10261   if (INTVAL (lo_bound) == -INTVAL (hi_bound) - 1)
10262     {
10263       if (mask)
10264         *mask = log + 1;
10265       if (signed_sat)
10266         *signed_sat = true;
10267
10268       return true;
10269     }
10270
10271   return false;
10272 }
10273
10274 /* Return 1 if memory locations are adjacent.  */
10275 int
10276 adjacent_mem_locations (rtx a, rtx b)
10277 {
10278   /* We don't guarantee to preserve the order of these memory refs.  */
10279   if (volatile_refs_p (a) || volatile_refs_p (b))
10280     return 0;
10281
10282   if ((REG_P (XEXP (a, 0))
10283        || (GET_CODE (XEXP (a, 0)) == PLUS
10284            && CONST_INT_P (XEXP (XEXP (a, 0), 1))))
10285       && (REG_P (XEXP (b, 0))
10286           || (GET_CODE (XEXP (b, 0)) == PLUS
10287               && CONST_INT_P (XEXP (XEXP (b, 0), 1)))))
10288     {
10289       HOST_WIDE_INT val0 = 0, val1 = 0;
10290       rtx reg0, reg1;
10291       int val_diff;
10292
10293       if (GET_CODE (XEXP (a, 0)) == PLUS)
10294         {
10295           reg0 = XEXP (XEXP (a, 0), 0);
10296           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
10297         }
10298       else
10299         reg0 = XEXP (a, 0);
10300
10301       if (GET_CODE (XEXP (b, 0)) == PLUS)
10302         {
10303           reg1 = XEXP (XEXP (b, 0), 0);
10304           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
10305         }
10306       else
10307         reg1 = XEXP (b, 0);
10308
10309       /* Don't accept any offset that will require multiple
10310          instructions to handle, since this would cause the
10311          arith_adjacentmem pattern to output an overlong sequence.  */
10312       if (!const_ok_for_op (val0, PLUS) || !const_ok_for_op (val1, PLUS))
10313         return 0;
10314
10315       /* Don't allow an eliminable register: register elimination can make
10316          the offset too large.  */
10317       if (arm_eliminable_register (reg0))
10318         return 0;
10319
10320       val_diff = val1 - val0;
10321
10322       if (arm_ld_sched)
10323         {
10324           /* If the target has load delay slots, then there's no benefit
10325              to using an ldm instruction unless the offset is zero and
10326              we are optimizing for size.  */
10327           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
10328                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
10329                   && (val_diff == 4 || val_diff == -4));
10330         }
10331
10332       return ((REGNO (reg0) == REGNO (reg1))
10333               && (val_diff == 4 || val_diff == -4));
10334     }
10335
10336   return 0;
10337 }
10338
10339 /* Return true if OP is a valid load or store multiple operation.  LOAD is true
10340    for load operations, false for store operations.  CONSECUTIVE is true
10341    if the register numbers in the operation must be consecutive in the register
10342    bank. RETURN_PC is true if value is to be loaded in PC.
10343    The pattern we are trying to match for load is:
10344      [(SET (R_d0) (MEM (PLUS (addr) (offset))))
10345       (SET (R_d1) (MEM (PLUS (addr) (offset + <reg_increment>))))
10346        :
10347        :
10348       (SET (R_dn) (MEM (PLUS (addr) (offset + n * <reg_increment>))))
10349      ]
10350      where
10351      1.  If offset is 0, first insn should be (SET (R_d0) (MEM (src_addr))).
10352      2.  REGNO (R_d0) < REGNO (R_d1) < ... < REGNO (R_dn).
10353      3.  If consecutive is TRUE, then for kth register being loaded,
10354          REGNO (R_dk) = REGNO (R_d0) + k.
10355    The pattern for store is similar.  */
10356 bool
10357 ldm_stm_operation_p (rtx op, bool load, enum machine_mode mode,
10358                      bool consecutive, bool return_pc)
10359 {
10360   HOST_WIDE_INT count = XVECLEN (op, 0);
10361   rtx reg, mem, addr;
10362   unsigned regno;
10363   unsigned first_regno;
10364   HOST_WIDE_INT i = 1, base = 0, offset = 0;
10365   rtx elt;
10366   bool addr_reg_in_reglist = false;
10367   bool update = false;
10368   int reg_increment;
10369   int offset_adj;
10370   int regs_per_val;
10371
10372   /* If not in SImode, then registers must be consecutive
10373      (e.g., VLDM instructions for DFmode).  */
10374   gcc_assert ((mode == SImode) || consecutive);
10375   /* Setting return_pc for stores is illegal.  */
10376   gcc_assert (!return_pc || load);
10377
10378   /* Set up the increments and the regs per val based on the mode.  */
10379   reg_increment = GET_MODE_SIZE (mode);
10380   regs_per_val = reg_increment / 4;
10381   offset_adj = return_pc ? 1 : 0;
10382
10383   if (count <= 1
10384       || GET_CODE (XVECEXP (op, 0, offset_adj)) != SET
10385       || (load && !REG_P (SET_DEST (XVECEXP (op, 0, offset_adj)))))
10386     return false;
10387
10388   /* Check if this is a write-back.  */
10389   elt = XVECEXP (op, 0, offset_adj);
10390   if (GET_CODE (SET_SRC (elt)) == PLUS)
10391     {
10392       i++;
10393       base = 1;
10394       update = true;
10395
10396       /* The offset adjustment must be the number of registers being
10397          popped times the size of a single register.  */
10398       if (!REG_P (SET_DEST (elt))
10399           || !REG_P (XEXP (SET_SRC (elt), 0))
10400           || (REGNO (SET_DEST (elt)) != REGNO (XEXP (SET_SRC (elt), 0)))
10401           || !CONST_INT_P (XEXP (SET_SRC (elt), 1))
10402           || INTVAL (XEXP (SET_SRC (elt), 1)) !=
10403              ((count - 1 - offset_adj) * reg_increment))
10404         return false;
10405     }
10406
10407   i = i + offset_adj;
10408   base = base + offset_adj;
10409   /* Perform a quick check so we don't blow up below. If only one reg is loaded,
10410      success depends on the type: VLDM can do just one reg,
10411      LDM must do at least two.  */
10412   if ((count <= i) && (mode == SImode))
10413       return false;
10414
10415   elt = XVECEXP (op, 0, i - 1);
10416   if (GET_CODE (elt) != SET)
10417     return false;
10418
10419   if (load)
10420     {
10421       reg = SET_DEST (elt);
10422       mem = SET_SRC (elt);
10423     }
10424   else
10425     {
10426       reg = SET_SRC (elt);
10427       mem = SET_DEST (elt);
10428     }
10429
10430   if (!REG_P (reg) || !MEM_P (mem))
10431     return false;
10432
10433   regno = REGNO (reg);
10434   first_regno = regno;
10435   addr = XEXP (mem, 0);
10436   if (GET_CODE (addr) == PLUS)
10437     {
10438       if (!CONST_INT_P (XEXP (addr, 1)))
10439         return false;
10440
10441       offset = INTVAL (XEXP (addr, 1));
10442       addr = XEXP (addr, 0);
10443     }
10444
10445   if (!REG_P (addr))
10446     return false;
10447
10448   /* Don't allow SP to be loaded unless it is also the base register. It
10449      guarantees that SP is reset correctly when an LDM instruction
10450      is interruptted. Otherwise, we might end up with a corrupt stack.  */
10451   if (load && (REGNO (reg) == SP_REGNUM) && (REGNO (addr) != SP_REGNUM))
10452     return false;
10453
10454   for (; i < count; i++)
10455     {
10456       elt = XVECEXP (op, 0, i);
10457       if (GET_CODE (elt) != SET)
10458         return false;
10459
10460       if (load)
10461         {
10462           reg = SET_DEST (elt);
10463           mem = SET_SRC (elt);
10464         }
10465       else
10466         {
10467           reg = SET_SRC (elt);
10468           mem = SET_DEST (elt);
10469         }
10470
10471       if (!REG_P (reg)
10472           || GET_MODE (reg) != mode
10473           || REGNO (reg) <= regno
10474           || (consecutive
10475               && (REGNO (reg) !=
10476                   (unsigned int) (first_regno + regs_per_val * (i - base))))
10477           /* Don't allow SP to be loaded unless it is also the base register. It
10478              guarantees that SP is reset correctly when an LDM instruction
10479              is interrupted. Otherwise, we might end up with a corrupt stack.  */
10480           || (load && (REGNO (reg) == SP_REGNUM) && (REGNO (addr) != SP_REGNUM))
10481           || !MEM_P (mem)
10482           || GET_MODE (mem) != mode
10483           || ((GET_CODE (XEXP (mem, 0)) != PLUS
10484                || !rtx_equal_p (XEXP (XEXP (mem, 0), 0), addr)
10485                || !CONST_INT_P (XEXP (XEXP (mem, 0), 1))
10486                || (INTVAL (XEXP (XEXP (mem, 0), 1)) !=
10487                    offset + (i - base) * reg_increment))
10488               && (!REG_P (XEXP (mem, 0))
10489                   || offset + (i - base) * reg_increment != 0)))
10490         return false;
10491
10492       regno = REGNO (reg);
10493       if (regno == REGNO (addr))
10494         addr_reg_in_reglist = true;
10495     }
10496
10497   if (load)
10498     {
10499       if (update && addr_reg_in_reglist)
10500         return false;
10501
10502       /* For Thumb-1, address register is always modified - either by write-back
10503          or by explicit load.  If the pattern does not describe an update,
10504          then the address register must be in the list of loaded registers.  */
10505       if (TARGET_THUMB1)
10506         return update || addr_reg_in_reglist;
10507     }
10508
10509   return true;
10510 }
10511
10512 /* Return true iff it would be profitable to turn a sequence of NOPS loads
10513    or stores (depending on IS_STORE) into a load-multiple or store-multiple
10514    instruction.  ADD_OFFSET is nonzero if the base address register needs
10515    to be modified with an add instruction before we can use it.  */
10516
10517 static bool
10518 multiple_operation_profitable_p (bool is_store ATTRIBUTE_UNUSED,
10519                                  int nops, HOST_WIDE_INT add_offset)
10520  {
10521   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
10522      if the offset isn't small enough.  The reason 2 ldrs are faster
10523      is because these ARMs are able to do more than one cache access
10524      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
10525      whilst the ARM8 has a double bandwidth cache.  This means that
10526      these cores can do both an instruction fetch and a data fetch in
10527      a single cycle, so the trick of calculating the address into a
10528      scratch register (one of the result regs) and then doing a load
10529      multiple actually becomes slower (and no smaller in code size).
10530      That is the transformation
10531
10532         ldr     rd1, [rbase + offset]
10533         ldr     rd2, [rbase + offset + 4]
10534
10535      to
10536
10537         add     rd1, rbase, offset
10538         ldmia   rd1, {rd1, rd2}
10539
10540      produces worse code -- '3 cycles + any stalls on rd2' instead of
10541      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
10542      access per cycle, the first sequence could never complete in less
10543      than 6 cycles, whereas the ldm sequence would only take 5 and
10544      would make better use of sequential accesses if not hitting the
10545      cache.
10546
10547      We cheat here and test 'arm_ld_sched' which we currently know to
10548      only be true for the ARM8, ARM9 and StrongARM.  If this ever
10549      changes, then the test below needs to be reworked.  */
10550   if (nops == 2 && arm_ld_sched && add_offset != 0)
10551     return false;
10552
10553   /* XScale has load-store double instructions, but they have stricter
10554      alignment requirements than load-store multiple, so we cannot
10555      use them.
10556
10557      For XScale ldm requires 2 + NREGS cycles to complete and blocks
10558      the pipeline until completion.
10559
10560         NREGS           CYCLES
10561           1               3
10562           2               4
10563           3               5
10564           4               6
10565
10566      An ldr instruction takes 1-3 cycles, but does not block the
10567      pipeline.
10568
10569         NREGS           CYCLES
10570           1              1-3
10571           2              2-6
10572           3              3-9
10573           4              4-12
10574
10575      Best case ldr will always win.  However, the more ldr instructions
10576      we issue, the less likely we are to be able to schedule them well.
10577      Using ldr instructions also increases code size.
10578
10579      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
10580      for counts of 3 or 4 regs.  */
10581   if (nops <= 2 && arm_tune_xscale && !optimize_size)
10582     return false;
10583   return true;
10584 }
10585
10586 /* Subroutine of load_multiple_sequence and store_multiple_sequence.
10587    Given an array of UNSORTED_OFFSETS, of which there are NOPS, compute
10588    an array ORDER which describes the sequence to use when accessing the
10589    offsets that produces an ascending order.  In this sequence, each
10590    offset must be larger by exactly 4 than the previous one.  ORDER[0]
10591    must have been filled in with the lowest offset by the caller.
10592    If UNSORTED_REGS is nonnull, it is an array of register numbers that
10593    we use to verify that ORDER produces an ascending order of registers.
10594    Return true if it was possible to construct such an order, false if
10595    not.  */
10596
10597 static bool
10598 compute_offset_order (int nops, HOST_WIDE_INT *unsorted_offsets, int *order,
10599                       int *unsorted_regs)
10600 {
10601   int i;
10602   for (i = 1; i < nops; i++)
10603     {
10604       int j;
10605
10606       order[i] = order[i - 1];
10607       for (j = 0; j < nops; j++)
10608         if (unsorted_offsets[j] == unsorted_offsets[order[i - 1]] + 4)
10609           {
10610             /* We must find exactly one offset that is higher than the
10611                previous one by 4.  */
10612             if (order[i] != order[i - 1])
10613               return false;
10614             order[i] = j;
10615           }
10616       if (order[i] == order[i - 1])
10617         return false;
10618       /* The register numbers must be ascending.  */
10619       if (unsorted_regs != NULL
10620           && unsorted_regs[order[i]] <= unsorted_regs[order[i - 1]])
10621         return false;
10622     }
10623   return true;
10624 }
10625
10626 /* Used to determine in a peephole whether a sequence of load
10627    instructions can be changed into a load-multiple instruction.
10628    NOPS is the number of separate load instructions we are examining.  The
10629    first NOPS entries in OPERANDS are the destination registers, the
10630    next NOPS entries are memory operands.  If this function is
10631    successful, *BASE is set to the common base register of the memory
10632    accesses; *LOAD_OFFSET is set to the first memory location's offset
10633    from that base register.
10634    REGS is an array filled in with the destination register numbers.
10635    SAVED_ORDER (if nonnull), is an array filled in with an order that maps
10636    insn numbers to an ascending order of stores.  If CHECK_REGS is true,
10637    the sequence of registers in REGS matches the loads from ascending memory
10638    locations, and the function verifies that the register numbers are
10639    themselves ascending.  If CHECK_REGS is false, the register numbers
10640    are stored in the order they are found in the operands.  */
10641 static int
10642 load_multiple_sequence (rtx *operands, int nops, int *regs, int *saved_order,
10643                         int *base, HOST_WIDE_INT *load_offset, bool check_regs)
10644 {
10645   int unsorted_regs[MAX_LDM_STM_OPS];
10646   HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
10647   int order[MAX_LDM_STM_OPS];
10648   rtx base_reg_rtx = NULL;
10649   int base_reg = -1;
10650   int i, ldm_case;
10651
10652   /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
10653      easily extended if required.  */
10654   gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
10655
10656   memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
10657
10658   /* Loop over the operands and check that the memory references are
10659      suitable (i.e. immediate offsets from the same base register).  At
10660      the same time, extract the target register, and the memory
10661      offsets.  */
10662   for (i = 0; i < nops; i++)
10663     {
10664       rtx reg;
10665       rtx offset;
10666
10667       /* Convert a subreg of a mem into the mem itself.  */
10668       if (GET_CODE (operands[nops + i]) == SUBREG)
10669         operands[nops + i] = alter_subreg (operands + (nops + i), true);
10670
10671       gcc_assert (MEM_P (operands[nops + i]));
10672
10673       /* Don't reorder volatile memory references; it doesn't seem worth
10674          looking for the case where the order is ok anyway.  */
10675       if (MEM_VOLATILE_P (operands[nops + i]))
10676         return 0;
10677
10678       offset = const0_rtx;
10679
10680       if ((REG_P (reg = XEXP (operands[nops + i], 0))
10681            || (GET_CODE (reg) == SUBREG
10682                && REG_P (reg = SUBREG_REG (reg))))
10683           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
10684               && ((REG_P (reg = XEXP (XEXP (operands[nops + i], 0), 0)))
10685                   || (GET_CODE (reg) == SUBREG
10686                       && REG_P (reg = SUBREG_REG (reg))))
10687               && (CONST_INT_P (offset
10688                   = XEXP (XEXP (operands[nops + i], 0), 1)))))
10689         {
10690           if (i == 0)
10691             {
10692               base_reg = REGNO (reg);
10693               base_reg_rtx = reg;
10694               if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
10695                 return 0;
10696             }
10697           else if (base_reg != (int) REGNO (reg))
10698             /* Not addressed from the same base register.  */
10699             return 0;
10700
10701           unsorted_regs[i] = (REG_P (operands[i])
10702                               ? REGNO (operands[i])
10703                               : REGNO (SUBREG_REG (operands[i])));
10704
10705           /* If it isn't an integer register, or if it overwrites the
10706              base register but isn't the last insn in the list, then
10707              we can't do this.  */
10708           if (unsorted_regs[i] < 0
10709               || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
10710               || unsorted_regs[i] > 14
10711               || (i != nops - 1 && unsorted_regs[i] == base_reg))
10712             return 0;
10713
10714           /* Don't allow SP to be loaded unless it is also the base
10715              register.  It guarantees that SP is reset correctly when
10716              an LDM instruction is interrupted.  Otherwise, we might
10717              end up with a corrupt stack.  */
10718           if (unsorted_regs[i] == SP_REGNUM && base_reg != SP_REGNUM)
10719             return 0;
10720
10721           unsorted_offsets[i] = INTVAL (offset);
10722           if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
10723             order[0] = i;
10724         }
10725       else
10726         /* Not a suitable memory address.  */
10727         return 0;
10728     }
10729
10730   /* All the useful information has now been extracted from the
10731      operands into unsorted_regs and unsorted_offsets; additionally,
10732      order[0] has been set to the lowest offset in the list.  Sort
10733      the offsets into order, verifying that they are adjacent, and
10734      check that the register numbers are ascending.  */
10735   if (!compute_offset_order (nops, unsorted_offsets, order,
10736                              check_regs ? unsorted_regs : NULL))
10737     return 0;
10738
10739   if (saved_order)
10740     memcpy (saved_order, order, sizeof order);
10741
10742   if (base)
10743     {
10744       *base = base_reg;
10745
10746       for (i = 0; i < nops; i++)
10747         regs[i] = unsorted_regs[check_regs ? order[i] : i];
10748
10749       *load_offset = unsorted_offsets[order[0]];
10750     }
10751
10752   if (TARGET_THUMB1
10753       && !peep2_reg_dead_p (nops, base_reg_rtx))
10754     return 0;
10755
10756   if (unsorted_offsets[order[0]] == 0)
10757     ldm_case = 1; /* ldmia */
10758   else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
10759     ldm_case = 2; /* ldmib */
10760   else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
10761     ldm_case = 3; /* ldmda */
10762   else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
10763     ldm_case = 4; /* ldmdb */
10764   else if (const_ok_for_arm (unsorted_offsets[order[0]])
10765            || const_ok_for_arm (-unsorted_offsets[order[0]]))
10766     ldm_case = 5;
10767   else
10768     return 0;
10769
10770   if (!multiple_operation_profitable_p (false, nops,
10771                                         ldm_case == 5
10772                                         ? unsorted_offsets[order[0]] : 0))
10773     return 0;
10774
10775   return ldm_case;
10776 }
10777
10778 /* Used to determine in a peephole whether a sequence of store instructions can
10779    be changed into a store-multiple instruction.
10780    NOPS is the number of separate store instructions we are examining.
10781    NOPS_TOTAL is the total number of instructions recognized by the peephole
10782    pattern.
10783    The first NOPS entries in OPERANDS are the source registers, the next
10784    NOPS entries are memory operands.  If this function is successful, *BASE is
10785    set to the common base register of the memory accesses; *LOAD_OFFSET is set
10786    to the first memory location's offset from that base register.  REGS is an
10787    array filled in with the source register numbers, REG_RTXS (if nonnull) is
10788    likewise filled with the corresponding rtx's.
10789    SAVED_ORDER (if nonnull), is an array filled in with an order that maps insn
10790    numbers to an ascending order of stores.
10791    If CHECK_REGS is true, the sequence of registers in *REGS matches the stores
10792    from ascending memory locations, and the function verifies that the register
10793    numbers are themselves ascending.  If CHECK_REGS is false, the register
10794    numbers are stored in the order they are found in the operands.  */
10795 static int
10796 store_multiple_sequence (rtx *operands, int nops, int nops_total,
10797                          int *regs, rtx *reg_rtxs, int *saved_order, int *base,
10798                          HOST_WIDE_INT *load_offset, bool check_regs)
10799 {
10800   int unsorted_regs[MAX_LDM_STM_OPS];
10801   rtx unsorted_reg_rtxs[MAX_LDM_STM_OPS];
10802   HOST_WIDE_INT unsorted_offsets[MAX_LDM_STM_OPS];
10803   int order[MAX_LDM_STM_OPS];
10804   int base_reg = -1;
10805   rtx base_reg_rtx = NULL;
10806   int i, stm_case;
10807
10808   /* Write back of base register is currently only supported for Thumb 1.  */
10809   int base_writeback = TARGET_THUMB1;
10810
10811   /* Can only handle up to MAX_LDM_STM_OPS insns at present, though could be
10812      easily extended if required.  */
10813   gcc_assert (nops >= 2 && nops <= MAX_LDM_STM_OPS);
10814
10815   memset (order, 0, MAX_LDM_STM_OPS * sizeof (int));
10816
10817   /* Loop over the operands and check that the memory references are
10818      suitable (i.e. immediate offsets from the same base register).  At
10819      the same time, extract the target register, and the memory
10820      offsets.  */
10821   for (i = 0; i < nops; i++)
10822     {
10823       rtx reg;
10824       rtx offset;
10825
10826       /* Convert a subreg of a mem into the mem itself.  */
10827       if (GET_CODE (operands[nops + i]) == SUBREG)
10828         operands[nops + i] = alter_subreg (operands + (nops + i), true);
10829
10830       gcc_assert (MEM_P (operands[nops + i]));
10831
10832       /* Don't reorder volatile memory references; it doesn't seem worth
10833          looking for the case where the order is ok anyway.  */
10834       if (MEM_VOLATILE_P (operands[nops + i]))
10835         return 0;
10836
10837       offset = const0_rtx;
10838
10839       if ((REG_P (reg = XEXP (operands[nops + i], 0))
10840            || (GET_CODE (reg) == SUBREG
10841                && REG_P (reg = SUBREG_REG (reg))))
10842           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
10843               && ((REG_P (reg = XEXP (XEXP (operands[nops + i], 0), 0)))
10844                   || (GET_CODE (reg) == SUBREG
10845                       && REG_P (reg = SUBREG_REG (reg))))
10846               && (CONST_INT_P (offset
10847                   = XEXP (XEXP (operands[nops + i], 0), 1)))))
10848         {
10849           unsorted_reg_rtxs[i] = (REG_P (operands[i])
10850                                   ? operands[i] : SUBREG_REG (operands[i]));
10851           unsorted_regs[i] = REGNO (unsorted_reg_rtxs[i]);
10852
10853           if (i == 0)
10854             {
10855               base_reg = REGNO (reg);
10856               base_reg_rtx = reg;
10857               if (TARGET_THUMB1 && base_reg > LAST_LO_REGNUM)
10858                 return 0;
10859             }
10860           else if (base_reg != (int) REGNO (reg))
10861             /* Not addressed from the same base register.  */
10862             return 0;
10863
10864           /* If it isn't an integer register, then we can't do this.  */
10865           if (unsorted_regs[i] < 0
10866               || (TARGET_THUMB1 && unsorted_regs[i] > LAST_LO_REGNUM)
10867               /* The effects are unpredictable if the base register is
10868                  both updated and stored.  */
10869               || (base_writeback && unsorted_regs[i] == base_reg)
10870               || (TARGET_THUMB2 && unsorted_regs[i] == SP_REGNUM)
10871               || unsorted_regs[i] > 14)
10872             return 0;
10873
10874           unsorted_offsets[i] = INTVAL (offset);
10875           if (i == 0 || unsorted_offsets[i] < unsorted_offsets[order[0]])
10876             order[0] = i;
10877         }
10878       else
10879         /* Not a suitable memory address.  */
10880         return 0;
10881     }
10882
10883   /* All the useful information has now been extracted from the
10884      operands into unsorted_regs and unsorted_offsets; additionally,
10885      order[0] has been set to the lowest offset in the list.  Sort
10886      the offsets into order, verifying that they are adjacent, and
10887      check that the register numbers are ascending.  */
10888   if (!compute_offset_order (nops, unsorted_offsets, order,
10889                              check_regs ? unsorted_regs : NULL))
10890     return 0;
10891
10892   if (saved_order)
10893     memcpy (saved_order, order, sizeof order);
10894
10895   if (base)
10896     {
10897       *base = base_reg;
10898
10899       for (i = 0; i < nops; i++)
10900         {
10901           regs[i] = unsorted_regs[check_regs ? order[i] : i];
10902           if (reg_rtxs)
10903             reg_rtxs[i] = unsorted_reg_rtxs[check_regs ? order[i] : i];
10904         }
10905
10906       *load_offset = unsorted_offsets[order[0]];
10907     }
10908
10909   if (TARGET_THUMB1
10910       && !peep2_reg_dead_p (nops_total, base_reg_rtx))
10911     return 0;
10912
10913   if (unsorted_offsets[order[0]] == 0)
10914     stm_case = 1; /* stmia */
10915   else if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
10916     stm_case = 2; /* stmib */
10917   else if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
10918     stm_case = 3; /* stmda */
10919   else if (TARGET_32BIT && unsorted_offsets[order[nops - 1]] == -4)
10920     stm_case = 4; /* stmdb */
10921   else
10922     return 0;
10923
10924   if (!multiple_operation_profitable_p (false, nops, 0))
10925     return 0;
10926
10927   return stm_case;
10928 }
10929 \f
10930 /* Routines for use in generating RTL.  */
10931
10932 /* Generate a load-multiple instruction.  COUNT is the number of loads in
10933    the instruction; REGS and MEMS are arrays containing the operands.
10934    BASEREG is the base register to be used in addressing the memory operands.
10935    WBACK_OFFSET is nonzero if the instruction should update the base
10936    register.  */
10937
10938 static rtx
10939 arm_gen_load_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
10940                          HOST_WIDE_INT wback_offset)
10941 {
10942   int i = 0, j;
10943   rtx result;
10944
10945   if (!multiple_operation_profitable_p (false, count, 0))
10946     {
10947       rtx seq;
10948
10949       start_sequence ();
10950
10951       for (i = 0; i < count; i++)
10952         emit_move_insn (gen_rtx_REG (SImode, regs[i]), mems[i]);
10953
10954       if (wback_offset != 0)
10955         emit_move_insn (basereg, plus_constant (Pmode, basereg, wback_offset));
10956
10957       seq = get_insns ();
10958       end_sequence ();
10959
10960       return seq;
10961     }
10962
10963   result = gen_rtx_PARALLEL (VOIDmode,
10964                              rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
10965   if (wback_offset != 0)
10966     {
10967       XVECEXP (result, 0, 0)
10968         = gen_rtx_SET (VOIDmode, basereg,
10969                        plus_constant (Pmode, basereg, wback_offset));
10970       i = 1;
10971       count++;
10972     }
10973
10974   for (j = 0; i < count; i++, j++)
10975     XVECEXP (result, 0, i)
10976       = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, regs[j]), mems[j]);
10977
10978   return result;
10979 }
10980
10981 /* Generate a store-multiple instruction.  COUNT is the number of stores in
10982    the instruction; REGS and MEMS are arrays containing the operands.
10983    BASEREG is the base register to be used in addressing the memory operands.
10984    WBACK_OFFSET is nonzero if the instruction should update the base
10985    register.  */
10986
10987 static rtx
10988 arm_gen_store_multiple_1 (int count, int *regs, rtx *mems, rtx basereg,
10989                           HOST_WIDE_INT wback_offset)
10990 {
10991   int i = 0, j;
10992   rtx result;
10993
10994   if (GET_CODE (basereg) == PLUS)
10995     basereg = XEXP (basereg, 0);
10996
10997   if (!multiple_operation_profitable_p (false, count, 0))
10998     {
10999       rtx seq;
11000
11001       start_sequence ();
11002
11003       for (i = 0; i < count; i++)
11004         emit_move_insn (mems[i], gen_rtx_REG (SImode, regs[i]));
11005
11006       if (wback_offset != 0)
11007         emit_move_insn (basereg, plus_constant (Pmode, basereg, wback_offset));
11008
11009       seq = get_insns ();
11010       end_sequence ();
11011
11012       return seq;
11013     }
11014
11015   result = gen_rtx_PARALLEL (VOIDmode,
11016                              rtvec_alloc (count + (wback_offset != 0 ? 1 : 0)));
11017   if (wback_offset != 0)
11018     {
11019       XVECEXP (result, 0, 0)
11020         = gen_rtx_SET (VOIDmode, basereg,
11021                        plus_constant (Pmode, basereg, wback_offset));
11022       i = 1;
11023       count++;
11024     }
11025
11026   for (j = 0; i < count; i++, j++)
11027     XVECEXP (result, 0, i)
11028       = gen_rtx_SET (VOIDmode, mems[j], gen_rtx_REG (SImode, regs[j]));
11029
11030   return result;
11031 }
11032
11033 /* Generate either a load-multiple or a store-multiple instruction.  This
11034    function can be used in situations where we can start with a single MEM
11035    rtx and adjust its address upwards.
11036    COUNT is the number of operations in the instruction, not counting a
11037    possible update of the base register.  REGS is an array containing the
11038    register operands.
11039    BASEREG is the base register to be used in addressing the memory operands,
11040    which are constructed from BASEMEM.
11041    WRITE_BACK specifies whether the generated instruction should include an
11042    update of the base register.
11043    OFFSETP is used to pass an offset to and from this function; this offset
11044    is not used when constructing the address (instead BASEMEM should have an
11045    appropriate offset in its address), it is used only for setting
11046    MEM_OFFSET.  It is updated only if WRITE_BACK is true.*/
11047
11048 static rtx
11049 arm_gen_multiple_op (bool is_load, int *regs, int count, rtx basereg,
11050                      bool write_back, rtx basemem, HOST_WIDE_INT *offsetp)
11051 {
11052   rtx mems[MAX_LDM_STM_OPS];
11053   HOST_WIDE_INT offset = *offsetp;
11054   int i;
11055
11056   gcc_assert (count <= MAX_LDM_STM_OPS);
11057
11058   if (GET_CODE (basereg) == PLUS)
11059     basereg = XEXP (basereg, 0);
11060
11061   for (i = 0; i < count; i++)
11062     {
11063       rtx addr = plus_constant (Pmode, basereg, i * 4);
11064       mems[i] = adjust_automodify_address_nv (basemem, SImode, addr, offset);
11065       offset += 4;
11066     }
11067
11068   if (write_back)
11069     *offsetp = offset;
11070
11071   if (is_load)
11072     return arm_gen_load_multiple_1 (count, regs, mems, basereg,
11073                                     write_back ? 4 * count : 0);
11074   else
11075     return arm_gen_store_multiple_1 (count, regs, mems, basereg,
11076                                      write_back ? 4 * count : 0);
11077 }
11078
11079 rtx
11080 arm_gen_load_multiple (int *regs, int count, rtx basereg, int write_back,
11081                        rtx basemem, HOST_WIDE_INT *offsetp)
11082 {
11083   return arm_gen_multiple_op (TRUE, regs, count, basereg, write_back, basemem,
11084                               offsetp);
11085 }
11086
11087 rtx
11088 arm_gen_store_multiple (int *regs, int count, rtx basereg, int write_back,
11089                         rtx basemem, HOST_WIDE_INT *offsetp)
11090 {
11091   return arm_gen_multiple_op (FALSE, regs, count, basereg, write_back, basemem,
11092                               offsetp);
11093 }
11094
11095 /* Called from a peephole2 expander to turn a sequence of loads into an
11096    LDM instruction.  OPERANDS are the operands found by the peephole matcher;
11097    NOPS indicates how many separate loads we are trying to combine.  SORT_REGS
11098    is true if we can reorder the registers because they are used commutatively
11099    subsequently.
11100    Returns true iff we could generate a new instruction.  */
11101
11102 bool
11103 gen_ldm_seq (rtx *operands, int nops, bool sort_regs)
11104 {
11105   int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
11106   rtx mems[MAX_LDM_STM_OPS];
11107   int i, j, base_reg;
11108   rtx base_reg_rtx;
11109   HOST_WIDE_INT offset;
11110   int write_back = FALSE;
11111   int ldm_case;
11112   rtx addr;
11113
11114   ldm_case = load_multiple_sequence (operands, nops, regs, mem_order,
11115                                      &base_reg, &offset, !sort_regs);
11116
11117   if (ldm_case == 0)
11118     return false;
11119
11120   if (sort_regs)
11121     for (i = 0; i < nops - 1; i++)
11122       for (j = i + 1; j < nops; j++)
11123         if (regs[i] > regs[j])
11124           {
11125             int t = regs[i];
11126             regs[i] = regs[j];
11127             regs[j] = t;
11128           }
11129   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
11130
11131   if (TARGET_THUMB1)
11132     {
11133       gcc_assert (peep2_reg_dead_p (nops, base_reg_rtx));
11134       gcc_assert (ldm_case == 1 || ldm_case == 5);
11135       write_back = TRUE;
11136     }
11137
11138   if (ldm_case == 5)
11139     {
11140       rtx newbase = TARGET_THUMB1 ? base_reg_rtx : gen_rtx_REG (SImode, regs[0]);
11141       emit_insn (gen_addsi3 (newbase, base_reg_rtx, GEN_INT (offset)));
11142       offset = 0;
11143       if (!TARGET_THUMB1)
11144         {
11145           base_reg = regs[0];
11146           base_reg_rtx = newbase;
11147         }
11148     }
11149
11150   for (i = 0; i < nops; i++)
11151     {
11152       addr = plus_constant (Pmode, base_reg_rtx, offset + i * 4);
11153       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
11154                                               SImode, addr, 0);
11155     }
11156   emit_insn (arm_gen_load_multiple_1 (nops, regs, mems, base_reg_rtx,
11157                                       write_back ? offset + i * 4 : 0));
11158   return true;
11159 }
11160
11161 /* Called from a peephole2 expander to turn a sequence of stores into an
11162    STM instruction.  OPERANDS are the operands found by the peephole matcher;
11163    NOPS indicates how many separate stores we are trying to combine.
11164    Returns true iff we could generate a new instruction.  */
11165
11166 bool
11167 gen_stm_seq (rtx *operands, int nops)
11168 {
11169   int i;
11170   int regs[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
11171   rtx mems[MAX_LDM_STM_OPS];
11172   int base_reg;
11173   rtx base_reg_rtx;
11174   HOST_WIDE_INT offset;
11175   int write_back = FALSE;
11176   int stm_case;
11177   rtx addr;
11178   bool base_reg_dies;
11179
11180   stm_case = store_multiple_sequence (operands, nops, nops, regs, NULL,
11181                                       mem_order, &base_reg, &offset, true);
11182
11183   if (stm_case == 0)
11184     return false;
11185
11186   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
11187
11188   base_reg_dies = peep2_reg_dead_p (nops, base_reg_rtx);
11189   if (TARGET_THUMB1)
11190     {
11191       gcc_assert (base_reg_dies);
11192       write_back = TRUE;
11193     }
11194
11195   if (stm_case == 5)
11196     {
11197       gcc_assert (base_reg_dies);
11198       emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
11199       offset = 0;
11200     }
11201
11202   addr = plus_constant (Pmode, base_reg_rtx, offset);
11203
11204   for (i = 0; i < nops; i++)
11205     {
11206       addr = plus_constant (Pmode, base_reg_rtx, offset + i * 4);
11207       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
11208                                               SImode, addr, 0);
11209     }
11210   emit_insn (arm_gen_store_multiple_1 (nops, regs, mems, base_reg_rtx,
11211                                        write_back ? offset + i * 4 : 0));
11212   return true;
11213 }
11214
11215 /* Called from a peephole2 expander to turn a sequence of stores that are
11216    preceded by constant loads into an STM instruction.  OPERANDS are the
11217    operands found by the peephole matcher; NOPS indicates how many
11218    separate stores we are trying to combine; there are 2 * NOPS
11219    instructions in the peephole.
11220    Returns true iff we could generate a new instruction.  */
11221
11222 bool
11223 gen_const_stm_seq (rtx *operands, int nops)
11224 {
11225   int regs[MAX_LDM_STM_OPS], sorted_regs[MAX_LDM_STM_OPS];
11226   int reg_order[MAX_LDM_STM_OPS], mem_order[MAX_LDM_STM_OPS];
11227   rtx reg_rtxs[MAX_LDM_STM_OPS], orig_reg_rtxs[MAX_LDM_STM_OPS];
11228   rtx mems[MAX_LDM_STM_OPS];
11229   int base_reg;
11230   rtx base_reg_rtx;
11231   HOST_WIDE_INT offset;
11232   int write_back = FALSE;
11233   int stm_case;
11234   rtx addr;
11235   bool base_reg_dies;
11236   int i, j;
11237   HARD_REG_SET allocated;
11238
11239   stm_case = store_multiple_sequence (operands, nops, 2 * nops, regs, reg_rtxs,
11240                                       mem_order, &base_reg, &offset, false);
11241
11242   if (stm_case == 0)
11243     return false;
11244
11245   memcpy (orig_reg_rtxs, reg_rtxs, sizeof orig_reg_rtxs);
11246
11247   /* If the same register is used more than once, try to find a free
11248      register.  */
11249   CLEAR_HARD_REG_SET (allocated);
11250   for (i = 0; i < nops; i++)
11251     {
11252       for (j = i + 1; j < nops; j++)
11253         if (regs[i] == regs[j])
11254           {
11255             rtx t = peep2_find_free_register (0, nops * 2,
11256                                               TARGET_THUMB1 ? "l" : "r",
11257                                               SImode, &allocated);
11258             if (t == NULL_RTX)
11259               return false;
11260             reg_rtxs[i] = t;
11261             regs[i] = REGNO (t);
11262           }
11263     }
11264
11265   /* Compute an ordering that maps the register numbers to an ascending
11266      sequence.  */
11267   reg_order[0] = 0;
11268   for (i = 0; i < nops; i++)
11269     if (regs[i] < regs[reg_order[0]])
11270       reg_order[0] = i;
11271
11272   for (i = 1; i < nops; i++)
11273     {
11274       int this_order = reg_order[i - 1];
11275       for (j = 0; j < nops; j++)
11276         if (regs[j] > regs[reg_order[i - 1]]
11277             && (this_order == reg_order[i - 1]
11278                 || regs[j] < regs[this_order]))
11279           this_order = j;
11280       reg_order[i] = this_order;
11281     }
11282
11283   /* Ensure that registers that must be live after the instruction end
11284      up with the correct value.  */
11285   for (i = 0; i < nops; i++)
11286     {
11287       int this_order = reg_order[i];
11288       if ((this_order != mem_order[i]
11289            || orig_reg_rtxs[this_order] != reg_rtxs[this_order])
11290           && !peep2_reg_dead_p (nops * 2, orig_reg_rtxs[this_order]))
11291         return false;
11292     }
11293
11294   /* Load the constants.  */
11295   for (i = 0; i < nops; i++)
11296     {
11297       rtx op = operands[2 * nops + mem_order[i]];
11298       sorted_regs[i] = regs[reg_order[i]];
11299       emit_move_insn (reg_rtxs[reg_order[i]], op);
11300     }
11301
11302   base_reg_rtx = gen_rtx_REG (Pmode, base_reg);
11303
11304   base_reg_dies = peep2_reg_dead_p (nops * 2, base_reg_rtx);
11305   if (TARGET_THUMB1)
11306     {
11307       gcc_assert (base_reg_dies);
11308       write_back = TRUE;
11309     }
11310
11311   if (stm_case == 5)
11312     {
11313       gcc_assert (base_reg_dies);
11314       emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx, GEN_INT (offset)));
11315       offset = 0;
11316     }
11317
11318   addr = plus_constant (Pmode, base_reg_rtx, offset);
11319
11320   for (i = 0; i < nops; i++)
11321     {
11322       addr = plus_constant (Pmode, base_reg_rtx, offset + i * 4);
11323       mems[i] = adjust_automodify_address_nv (operands[nops + mem_order[i]],
11324                                               SImode, addr, 0);
11325     }
11326   emit_insn (arm_gen_store_multiple_1 (nops, sorted_regs, mems, base_reg_rtx,
11327                                        write_back ? offset + i * 4 : 0));
11328   return true;
11329 }
11330
11331 /* Copy a block of memory using plain ldr/str/ldrh/strh instructions, to permit
11332    unaligned copies on processors which support unaligned semantics for those
11333    instructions.  INTERLEAVE_FACTOR can be used to attempt to hide load latency
11334    (using more registers) by doing e.g. load/load/store/store for a factor of 2.
11335    An interleave factor of 1 (the minimum) will perform no interleaving.
11336    Load/store multiple are used for aligned addresses where possible.  */
11337
11338 static void
11339 arm_block_move_unaligned_straight (rtx dstbase, rtx srcbase,
11340                                    HOST_WIDE_INT length,
11341                                    unsigned int interleave_factor)
11342 {
11343   rtx *regs = XALLOCAVEC (rtx, interleave_factor);
11344   int *regnos = XALLOCAVEC (int, interleave_factor);
11345   HOST_WIDE_INT block_size_bytes = interleave_factor * UNITS_PER_WORD;
11346   HOST_WIDE_INT i, j;
11347   HOST_WIDE_INT remaining = length, words;
11348   rtx halfword_tmp = NULL, byte_tmp = NULL;
11349   rtx dst, src;
11350   bool src_aligned = MEM_ALIGN (srcbase) >= BITS_PER_WORD;
11351   bool dst_aligned = MEM_ALIGN (dstbase) >= BITS_PER_WORD;
11352   HOST_WIDE_INT srcoffset, dstoffset;
11353   HOST_WIDE_INT src_autoinc, dst_autoinc;
11354   rtx mem, addr;
11355   
11356   gcc_assert (1 <= interleave_factor && interleave_factor <= 4);
11357   
11358   /* Use hard registers if we have aligned source or destination so we can use
11359      load/store multiple with contiguous registers.  */
11360   if (dst_aligned || src_aligned)
11361     for (i = 0; i < interleave_factor; i++)
11362       regs[i] = gen_rtx_REG (SImode, i);
11363   else
11364     for (i = 0; i < interleave_factor; i++)
11365       regs[i] = gen_reg_rtx (SImode);
11366
11367   dst = copy_addr_to_reg (XEXP (dstbase, 0));
11368   src = copy_addr_to_reg (XEXP (srcbase, 0));
11369
11370   srcoffset = dstoffset = 0;
11371   
11372   /* Calls to arm_gen_load_multiple and arm_gen_store_multiple update SRC/DST.
11373      For copying the last bytes we want to subtract this offset again.  */
11374   src_autoinc = dst_autoinc = 0;
11375
11376   for (i = 0; i < interleave_factor; i++)
11377     regnos[i] = i;
11378
11379   /* Copy BLOCK_SIZE_BYTES chunks.  */
11380
11381   for (i = 0; i + block_size_bytes <= length; i += block_size_bytes)
11382     {
11383       /* Load words.  */
11384       if (src_aligned && interleave_factor > 1)
11385         {
11386           emit_insn (arm_gen_load_multiple (regnos, interleave_factor, src,
11387                                             TRUE, srcbase, &srcoffset));
11388           src_autoinc += UNITS_PER_WORD * interleave_factor;
11389         }
11390       else
11391         {
11392           for (j = 0; j < interleave_factor; j++)
11393             {
11394               addr = plus_constant (Pmode, src, (srcoffset + j * UNITS_PER_WORD
11395                                                  - src_autoinc));
11396               mem = adjust_automodify_address (srcbase, SImode, addr,
11397                                                srcoffset + j * UNITS_PER_WORD);
11398               emit_insn (gen_unaligned_loadsi (regs[j], mem));
11399             }
11400           srcoffset += block_size_bytes;
11401         }
11402
11403       /* Store words.  */
11404       if (dst_aligned && interleave_factor > 1)
11405         {
11406           emit_insn (arm_gen_store_multiple (regnos, interleave_factor, dst,
11407                                              TRUE, dstbase, &dstoffset));
11408           dst_autoinc += UNITS_PER_WORD * interleave_factor;
11409         }
11410       else
11411         {
11412           for (j = 0; j < interleave_factor; j++)
11413             {
11414               addr = plus_constant (Pmode, dst, (dstoffset + j * UNITS_PER_WORD
11415                                                  - dst_autoinc));
11416               mem = adjust_automodify_address (dstbase, SImode, addr,
11417                                                dstoffset + j * UNITS_PER_WORD);
11418               emit_insn (gen_unaligned_storesi (mem, regs[j]));
11419             }
11420           dstoffset += block_size_bytes;
11421         }
11422
11423       remaining -= block_size_bytes;
11424     }
11425   
11426   /* Copy any whole words left (note these aren't interleaved with any
11427      subsequent halfword/byte load/stores in the interests of simplicity).  */
11428   
11429   words = remaining / UNITS_PER_WORD;
11430
11431   gcc_assert (words < interleave_factor);
11432   
11433   if (src_aligned && words > 1)
11434     {
11435       emit_insn (arm_gen_load_multiple (regnos, words, src, TRUE, srcbase,
11436                                         &srcoffset));
11437       src_autoinc += UNITS_PER_WORD * words;
11438     }
11439   else
11440     {
11441       for (j = 0; j < words; j++)
11442         {
11443           addr = plus_constant (Pmode, src,
11444                                 srcoffset + j * UNITS_PER_WORD - src_autoinc);
11445           mem = adjust_automodify_address (srcbase, SImode, addr,
11446                                            srcoffset + j * UNITS_PER_WORD);
11447           emit_insn (gen_unaligned_loadsi (regs[j], mem));
11448         }
11449       srcoffset += words * UNITS_PER_WORD;
11450     }
11451
11452   if (dst_aligned && words > 1)
11453     {
11454       emit_insn (arm_gen_store_multiple (regnos, words, dst, TRUE, dstbase,
11455                                          &dstoffset));
11456       dst_autoinc += words * UNITS_PER_WORD;
11457     }
11458   else
11459     {
11460       for (j = 0; j < words; j++)
11461         {
11462           addr = plus_constant (Pmode, dst,
11463                                 dstoffset + j * UNITS_PER_WORD - dst_autoinc);
11464           mem = adjust_automodify_address (dstbase, SImode, addr,
11465                                            dstoffset + j * UNITS_PER_WORD);
11466           emit_insn (gen_unaligned_storesi (mem, regs[j]));
11467         }
11468       dstoffset += words * UNITS_PER_WORD;
11469     }
11470
11471   remaining -= words * UNITS_PER_WORD;
11472   
11473   gcc_assert (remaining < 4);
11474   
11475   /* Copy a halfword if necessary.  */
11476   
11477   if (remaining >= 2)
11478     {
11479       halfword_tmp = gen_reg_rtx (SImode);
11480
11481       addr = plus_constant (Pmode, src, srcoffset - src_autoinc);
11482       mem = adjust_automodify_address (srcbase, HImode, addr, srcoffset);
11483       emit_insn (gen_unaligned_loadhiu (halfword_tmp, mem));
11484
11485       /* Either write out immediately, or delay until we've loaded the last
11486          byte, depending on interleave factor.  */
11487       if (interleave_factor == 1)
11488         {
11489           addr = plus_constant (Pmode, dst, dstoffset - dst_autoinc);
11490           mem = adjust_automodify_address (dstbase, HImode, addr, dstoffset);
11491           emit_insn (gen_unaligned_storehi (mem,
11492                        gen_lowpart (HImode, halfword_tmp)));
11493           halfword_tmp = NULL;
11494           dstoffset += 2;
11495         }
11496
11497       remaining -= 2;
11498       srcoffset += 2;
11499     }
11500   
11501   gcc_assert (remaining < 2);
11502   
11503   /* Copy last byte.  */
11504   
11505   if ((remaining & 1) != 0)
11506     {
11507       byte_tmp = gen_reg_rtx (SImode);
11508
11509       addr = plus_constant (Pmode, src, srcoffset - src_autoinc);
11510       mem = adjust_automodify_address (srcbase, QImode, addr, srcoffset);
11511       emit_move_insn (gen_lowpart (QImode, byte_tmp), mem);
11512
11513       if (interleave_factor == 1)
11514         {
11515           addr = plus_constant (Pmode, dst, dstoffset - dst_autoinc);
11516           mem = adjust_automodify_address (dstbase, QImode, addr, dstoffset);
11517           emit_move_insn (mem, gen_lowpart (QImode, byte_tmp));
11518           byte_tmp = NULL;
11519           dstoffset++;
11520         }
11521
11522       remaining--;
11523       srcoffset++;
11524     }
11525   
11526   /* Store last halfword if we haven't done so already.  */
11527   
11528   if (halfword_tmp)
11529     {
11530       addr = plus_constant (Pmode, dst, dstoffset - dst_autoinc);
11531       mem = adjust_automodify_address (dstbase, HImode, addr, dstoffset);
11532       emit_insn (gen_unaligned_storehi (mem,
11533                    gen_lowpart (HImode, halfword_tmp)));
11534       dstoffset += 2;
11535     }
11536
11537   /* Likewise for last byte.  */
11538
11539   if (byte_tmp)
11540     {
11541       addr = plus_constant (Pmode, dst, dstoffset - dst_autoinc);
11542       mem = adjust_automodify_address (dstbase, QImode, addr, dstoffset);
11543       emit_move_insn (mem, gen_lowpart (QImode, byte_tmp));
11544       dstoffset++;
11545     }
11546   
11547   gcc_assert (remaining == 0 && srcoffset == dstoffset);
11548 }
11549
11550 /* From mips_adjust_block_mem:
11551
11552    Helper function for doing a loop-based block operation on memory
11553    reference MEM.  Each iteration of the loop will operate on LENGTH
11554    bytes of MEM.
11555
11556    Create a new base register for use within the loop and point it to
11557    the start of MEM.  Create a new memory reference that uses this
11558    register.  Store them in *LOOP_REG and *LOOP_MEM respectively.  */
11559
11560 static void
11561 arm_adjust_block_mem (rtx mem, HOST_WIDE_INT length, rtx *loop_reg,
11562                       rtx *loop_mem)
11563 {
11564   *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
11565   
11566   /* Although the new mem does not refer to a known location,
11567      it does keep up to LENGTH bytes of alignment.  */
11568   *loop_mem = change_address (mem, BLKmode, *loop_reg);
11569   set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
11570 }
11571
11572 /* From mips_block_move_loop:
11573
11574    Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
11575    bytes at a time.  LENGTH must be at least BYTES_PER_ITER.  Assume that
11576    the memory regions do not overlap.  */
11577
11578 static void
11579 arm_block_move_unaligned_loop (rtx dest, rtx src, HOST_WIDE_INT length,
11580                                unsigned int interleave_factor,
11581                                HOST_WIDE_INT bytes_per_iter)
11582 {
11583   rtx label, src_reg, dest_reg, final_src, test;
11584   HOST_WIDE_INT leftover;
11585   
11586   leftover = length % bytes_per_iter;
11587   length -= leftover;
11588   
11589   /* Create registers and memory references for use within the loop.  */
11590   arm_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
11591   arm_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
11592   
11593   /* Calculate the value that SRC_REG should have after the last iteration of
11594      the loop.  */
11595   final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
11596                                    0, 0, OPTAB_WIDEN);
11597
11598   /* Emit the start of the loop.  */
11599   label = gen_label_rtx ();
11600   emit_label (label);
11601   
11602   /* Emit the loop body.  */
11603   arm_block_move_unaligned_straight (dest, src, bytes_per_iter,
11604                                      interleave_factor);
11605
11606   /* Move on to the next block.  */
11607   emit_move_insn (src_reg, plus_constant (Pmode, src_reg, bytes_per_iter));
11608   emit_move_insn (dest_reg, plus_constant (Pmode, dest_reg, bytes_per_iter));
11609   
11610   /* Emit the loop condition.  */
11611   test = gen_rtx_NE (VOIDmode, src_reg, final_src);
11612   emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
11613   
11614   /* Mop up any left-over bytes.  */
11615   if (leftover)
11616     arm_block_move_unaligned_straight (dest, src, leftover, interleave_factor);
11617 }
11618
11619 /* Emit a block move when either the source or destination is unaligned (not
11620    aligned to a four-byte boundary).  This may need further tuning depending on
11621    core type, optimize_size setting, etc.  */
11622
11623 static int
11624 arm_movmemqi_unaligned (rtx *operands)
11625 {
11626   HOST_WIDE_INT length = INTVAL (operands[2]);
11627   
11628   if (optimize_size)
11629     {
11630       bool src_aligned = MEM_ALIGN (operands[1]) >= BITS_PER_WORD;
11631       bool dst_aligned = MEM_ALIGN (operands[0]) >= BITS_PER_WORD;
11632       /* Inlined memcpy using ldr/str/ldrh/strh can be quite big: try to limit
11633          size of code if optimizing for size.  We'll use ldm/stm if src_aligned
11634          or dst_aligned though: allow more interleaving in those cases since the
11635          resulting code can be smaller.  */
11636       unsigned int interleave_factor = (src_aligned || dst_aligned) ? 2 : 1;
11637       HOST_WIDE_INT bytes_per_iter = (src_aligned || dst_aligned) ? 8 : 4;
11638       
11639       if (length > 12)
11640         arm_block_move_unaligned_loop (operands[0], operands[1], length,
11641                                        interleave_factor, bytes_per_iter);
11642       else
11643         arm_block_move_unaligned_straight (operands[0], operands[1], length,
11644                                            interleave_factor);
11645     }
11646   else
11647     {
11648       /* Note that the loop created by arm_block_move_unaligned_loop may be
11649          subject to loop unrolling, which makes tuning this condition a little
11650          redundant.  */
11651       if (length > 32)
11652         arm_block_move_unaligned_loop (operands[0], operands[1], length, 4, 16);
11653       else
11654         arm_block_move_unaligned_straight (operands[0], operands[1], length, 4);
11655     }
11656   
11657   return 1;
11658 }
11659
11660 int
11661 arm_gen_movmemqi (rtx *operands)
11662 {
11663   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
11664   HOST_WIDE_INT srcoffset, dstoffset;
11665   int i;
11666   rtx src, dst, srcbase, dstbase;
11667   rtx part_bytes_reg = NULL;
11668   rtx mem;
11669
11670   if (!CONST_INT_P (operands[2])
11671       || !CONST_INT_P (operands[3])
11672       || INTVAL (operands[2]) > 64)
11673     return 0;
11674
11675   if (unaligned_access && (INTVAL (operands[3]) & 3) != 0)
11676     return arm_movmemqi_unaligned (operands);
11677
11678   if (INTVAL (operands[3]) & 3)
11679     return 0;
11680
11681   dstbase = operands[0];
11682   srcbase = operands[1];
11683
11684   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
11685   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
11686
11687   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
11688   out_words_to_go = INTVAL (operands[2]) / 4;
11689   last_bytes = INTVAL (operands[2]) & 3;
11690   dstoffset = srcoffset = 0;
11691
11692   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
11693     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
11694
11695   for (i = 0; in_words_to_go >= 2; i+=4)
11696     {
11697       if (in_words_to_go > 4)
11698         emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, 4, src,
11699                                           TRUE, srcbase, &srcoffset));
11700       else
11701         emit_insn (arm_gen_load_multiple (arm_regs_in_sequence, in_words_to_go,
11702                                           src, FALSE, srcbase,
11703                                           &srcoffset));
11704
11705       if (out_words_to_go)
11706         {
11707           if (out_words_to_go > 4)
11708             emit_insn (arm_gen_store_multiple (arm_regs_in_sequence, 4, dst,
11709                                                TRUE, dstbase, &dstoffset));
11710           else if (out_words_to_go != 1)
11711             emit_insn (arm_gen_store_multiple (arm_regs_in_sequence,
11712                                                out_words_to_go, dst,
11713                                                (last_bytes == 0
11714                                                 ? FALSE : TRUE),
11715                                                dstbase, &dstoffset));
11716           else
11717             {
11718               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
11719               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
11720               if (last_bytes != 0)
11721                 {
11722                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
11723                   dstoffset += 4;
11724                 }
11725             }
11726         }
11727
11728       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
11729       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
11730     }
11731
11732   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
11733   if (out_words_to_go)
11734     {
11735       rtx sreg;
11736
11737       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
11738       sreg = copy_to_reg (mem);
11739
11740       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
11741       emit_move_insn (mem, sreg);
11742       in_words_to_go--;
11743
11744       gcc_assert (!in_words_to_go);     /* Sanity check */
11745     }
11746
11747   if (in_words_to_go)
11748     {
11749       gcc_assert (in_words_to_go > 0);
11750
11751       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
11752       part_bytes_reg = copy_to_mode_reg (SImode, mem);
11753     }
11754
11755   gcc_assert (!last_bytes || part_bytes_reg);
11756
11757   if (BYTES_BIG_ENDIAN && last_bytes)
11758     {
11759       rtx tmp = gen_reg_rtx (SImode);
11760
11761       /* The bytes we want are in the top end of the word.  */
11762       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
11763                               GEN_INT (8 * (4 - last_bytes))));
11764       part_bytes_reg = tmp;
11765
11766       while (last_bytes)
11767         {
11768           mem = adjust_automodify_address (dstbase, QImode,
11769                                            plus_constant (Pmode, dst,
11770                                                           last_bytes - 1),
11771                                            dstoffset + last_bytes - 1);
11772           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
11773
11774           if (--last_bytes)
11775             {
11776               tmp = gen_reg_rtx (SImode);
11777               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
11778               part_bytes_reg = tmp;
11779             }
11780         }
11781
11782     }
11783   else
11784     {
11785       if (last_bytes > 1)
11786         {
11787           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
11788           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
11789           last_bytes -= 2;
11790           if (last_bytes)
11791             {
11792               rtx tmp = gen_reg_rtx (SImode);
11793               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
11794               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
11795               part_bytes_reg = tmp;
11796               dstoffset += 2;
11797             }
11798         }
11799
11800       if (last_bytes)
11801         {
11802           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
11803           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
11804         }
11805     }
11806
11807   return 1;
11808 }
11809
11810 /* Select a dominance comparison mode if possible for a test of the general
11811    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
11812    COND_OR == DOM_CC_X_AND_Y => (X && Y)
11813    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
11814    COND_OR == DOM_CC_X_OR_Y => (X || Y)
11815    In all cases OP will be either EQ or NE, but we don't need to know which
11816    here.  If we are unable to support a dominance comparison we return
11817    CC mode.  This will then fail to match for the RTL expressions that
11818    generate this call.  */
11819 enum machine_mode
11820 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
11821 {
11822   enum rtx_code cond1, cond2;
11823   int swapped = 0;
11824
11825   /* Currently we will probably get the wrong result if the individual
11826      comparisons are not simple.  This also ensures that it is safe to
11827      reverse a comparison if necessary.  */
11828   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
11829        != CCmode)
11830       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
11831           != CCmode))
11832     return CCmode;
11833
11834   /* The if_then_else variant of this tests the second condition if the
11835      first passes, but is true if the first fails.  Reverse the first
11836      condition to get a true "inclusive-or" expression.  */
11837   if (cond_or == DOM_CC_NX_OR_Y)
11838     cond1 = reverse_condition (cond1);
11839
11840   /* If the comparisons are not equal, and one doesn't dominate the other,
11841      then we can't do this.  */
11842   if (cond1 != cond2
11843       && !comparison_dominates_p (cond1, cond2)
11844       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
11845     return CCmode;
11846
11847   if (swapped)
11848     {
11849       enum rtx_code temp = cond1;
11850       cond1 = cond2;
11851       cond2 = temp;
11852     }
11853
11854   switch (cond1)
11855     {
11856     case EQ:
11857       if (cond_or == DOM_CC_X_AND_Y)
11858         return CC_DEQmode;
11859
11860       switch (cond2)
11861         {
11862         case EQ: return CC_DEQmode;
11863         case LE: return CC_DLEmode;
11864         case LEU: return CC_DLEUmode;
11865         case GE: return CC_DGEmode;
11866         case GEU: return CC_DGEUmode;
11867         default: gcc_unreachable ();
11868         }
11869
11870     case LT:
11871       if (cond_or == DOM_CC_X_AND_Y)
11872         return CC_DLTmode;
11873
11874       switch (cond2)
11875         {
11876         case  LT:
11877             return CC_DLTmode;
11878         case LE:
11879           return CC_DLEmode;
11880         case NE:
11881           return CC_DNEmode;
11882         default:
11883           gcc_unreachable ();
11884         }
11885
11886     case GT:
11887       if (cond_or == DOM_CC_X_AND_Y)
11888         return CC_DGTmode;
11889
11890       switch (cond2)
11891         {
11892         case GT:
11893           return CC_DGTmode;
11894         case GE:
11895           return CC_DGEmode;
11896         case NE:
11897           return CC_DNEmode;
11898         default:
11899           gcc_unreachable ();
11900         }
11901
11902     case LTU:
11903       if (cond_or == DOM_CC_X_AND_Y)
11904         return CC_DLTUmode;
11905
11906       switch (cond2)
11907         {
11908         case LTU:
11909           return CC_DLTUmode;
11910         case LEU:
11911           return CC_DLEUmode;
11912         case NE:
11913           return CC_DNEmode;
11914         default:
11915           gcc_unreachable ();
11916         }
11917
11918     case GTU:
11919       if (cond_or == DOM_CC_X_AND_Y)
11920         return CC_DGTUmode;
11921
11922       switch (cond2)
11923         {
11924         case GTU:
11925           return CC_DGTUmode;
11926         case GEU:
11927           return CC_DGEUmode;
11928         case NE:
11929           return CC_DNEmode;
11930         default:
11931           gcc_unreachable ();
11932         }
11933
11934     /* The remaining cases only occur when both comparisons are the
11935        same.  */
11936     case NE:
11937       gcc_assert (cond1 == cond2);
11938       return CC_DNEmode;
11939
11940     case LE:
11941       gcc_assert (cond1 == cond2);
11942       return CC_DLEmode;
11943
11944     case GE:
11945       gcc_assert (cond1 == cond2);
11946       return CC_DGEmode;
11947
11948     case LEU:
11949       gcc_assert (cond1 == cond2);
11950       return CC_DLEUmode;
11951
11952     case GEU:
11953       gcc_assert (cond1 == cond2);
11954       return CC_DGEUmode;
11955
11956     default:
11957       gcc_unreachable ();
11958     }
11959 }
11960
11961 enum machine_mode
11962 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
11963 {
11964   /* All floating point compares return CCFP if it is an equality
11965      comparison, and CCFPE otherwise.  */
11966   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
11967     {
11968       switch (op)
11969         {
11970         case EQ:
11971         case NE:
11972         case UNORDERED:
11973         case ORDERED:
11974         case UNLT:
11975         case UNLE:
11976         case UNGT:
11977         case UNGE:
11978         case UNEQ:
11979         case LTGT:
11980           return CCFPmode;
11981
11982         case LT:
11983         case LE:
11984         case GT:
11985         case GE:
11986           return CCFPEmode;
11987
11988         default:
11989           gcc_unreachable ();
11990         }
11991     }
11992
11993   /* A compare with a shifted operand.  Because of canonicalization, the
11994      comparison will have to be swapped when we emit the assembler.  */
11995   if (GET_MODE (y) == SImode
11996       && (REG_P (y) || (GET_CODE (y) == SUBREG))
11997       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
11998           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
11999           || GET_CODE (x) == ROTATERT))
12000     return CC_SWPmode;
12001
12002   /* This operation is performed swapped, but since we only rely on the Z
12003      flag we don't need an additional mode.  */
12004   if (GET_MODE (y) == SImode
12005       && (REG_P (y) || (GET_CODE (y) == SUBREG))
12006       && GET_CODE (x) == NEG
12007       && (op == EQ || op == NE))
12008     return CC_Zmode;
12009
12010   /* This is a special case that is used by combine to allow a
12011      comparison of a shifted byte load to be split into a zero-extend
12012      followed by a comparison of the shifted integer (only valid for
12013      equalities and unsigned inequalities).  */
12014   if (GET_MODE (x) == SImode
12015       && GET_CODE (x) == ASHIFT
12016       && CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) == 24
12017       && GET_CODE (XEXP (x, 0)) == SUBREG
12018       && MEM_P (SUBREG_REG (XEXP (x, 0)))
12019       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
12020       && (op == EQ || op == NE
12021           || op == GEU || op == GTU || op == LTU || op == LEU)
12022       && CONST_INT_P (y))
12023     return CC_Zmode;
12024
12025   /* A construct for a conditional compare, if the false arm contains
12026      0, then both conditions must be true, otherwise either condition
12027      must be true.  Not all conditions are possible, so CCmode is
12028      returned if it can't be done.  */
12029   if (GET_CODE (x) == IF_THEN_ELSE
12030       && (XEXP (x, 2) == const0_rtx
12031           || XEXP (x, 2) == const1_rtx)
12032       && COMPARISON_P (XEXP (x, 0))
12033       && COMPARISON_P (XEXP (x, 1)))
12034     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
12035                                          INTVAL (XEXP (x, 2)));
12036
12037   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
12038   if (GET_CODE (x) == AND
12039       && (op == EQ || op == NE)
12040       && COMPARISON_P (XEXP (x, 0))
12041       && COMPARISON_P (XEXP (x, 1)))
12042     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
12043                                          DOM_CC_X_AND_Y);
12044
12045   if (GET_CODE (x) == IOR
12046       && (op == EQ || op == NE)
12047       && COMPARISON_P (XEXP (x, 0))
12048       && COMPARISON_P (XEXP (x, 1)))
12049     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
12050                                          DOM_CC_X_OR_Y);
12051
12052   /* An operation (on Thumb) where we want to test for a single bit.
12053      This is done by shifting that bit up into the top bit of a
12054      scratch register; we can then branch on the sign bit.  */
12055   if (TARGET_THUMB1
12056       && GET_MODE (x) == SImode
12057       && (op == EQ || op == NE)
12058       && GET_CODE (x) == ZERO_EXTRACT
12059       && XEXP (x, 1) == const1_rtx)
12060     return CC_Nmode;
12061
12062   /* An operation that sets the condition codes as a side-effect, the
12063      V flag is not set correctly, so we can only use comparisons where
12064      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
12065      instead.)  */
12066   /* ??? Does the ZERO_EXTRACT case really apply to thumb2?  */
12067   if (GET_MODE (x) == SImode
12068       && y == const0_rtx
12069       && (op == EQ || op == NE || op == LT || op == GE)
12070       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
12071           || GET_CODE (x) == AND || GET_CODE (x) == IOR
12072           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
12073           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
12074           || GET_CODE (x) == LSHIFTRT
12075           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
12076           || GET_CODE (x) == ROTATERT
12077           || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
12078     return CC_NOOVmode;
12079
12080   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
12081     return CC_Zmode;
12082
12083   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
12084       && GET_CODE (x) == PLUS
12085       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
12086     return CC_Cmode;
12087
12088   if (GET_MODE (x) == DImode || GET_MODE (y) == DImode)
12089     {
12090       switch (op)
12091         {
12092         case EQ:
12093         case NE:
12094           /* A DImode comparison against zero can be implemented by
12095              or'ing the two halves together.  */
12096           if (y == const0_rtx)
12097             return CC_Zmode;
12098
12099           /* We can do an equality test in three Thumb instructions.  */
12100           if (!TARGET_32BIT)
12101             return CC_Zmode;
12102
12103           /* FALLTHROUGH */
12104
12105         case LTU:
12106         case LEU:
12107         case GTU:
12108         case GEU:
12109           /* DImode unsigned comparisons can be implemented by cmp +
12110              cmpeq without a scratch register.  Not worth doing in
12111              Thumb-2.  */
12112           if (TARGET_32BIT)
12113             return CC_CZmode;
12114
12115           /* FALLTHROUGH */
12116
12117         case LT:
12118         case LE:
12119         case GT:
12120         case GE:
12121           /* DImode signed and unsigned comparisons can be implemented
12122              by cmp + sbcs with a scratch register, but that does not
12123              set the Z flag - we must reverse GT/LE/GTU/LEU.  */
12124           gcc_assert (op != EQ && op != NE);
12125           return CC_NCVmode;
12126
12127         default:
12128           gcc_unreachable ();
12129         }
12130     }
12131
12132   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
12133     return GET_MODE (x);
12134
12135   return CCmode;
12136 }
12137
12138 /* X and Y are two things to compare using CODE.  Emit the compare insn and
12139    return the rtx for register 0 in the proper mode.  FP means this is a
12140    floating point compare: I don't think that it is needed on the arm.  */
12141 rtx
12142 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y, rtx scratch)
12143 {
12144   enum machine_mode mode;
12145   rtx cc_reg;
12146   int dimode_comparison = GET_MODE (x) == DImode || GET_MODE (y) == DImode;
12147
12148   /* We might have X as a constant, Y as a register because of the predicates
12149      used for cmpdi.  If so, force X to a register here.  */
12150   if (dimode_comparison && !REG_P (x))
12151     x = force_reg (DImode, x);
12152
12153   mode = SELECT_CC_MODE (code, x, y);
12154   cc_reg = gen_rtx_REG (mode, CC_REGNUM);
12155
12156   if (dimode_comparison
12157       && mode != CC_CZmode)
12158     {
12159       rtx clobber, set;
12160
12161       /* To compare two non-zero values for equality, XOR them and
12162          then compare against zero.  Not used for ARM mode; there
12163          CC_CZmode is cheaper.  */
12164       if (mode == CC_Zmode && y != const0_rtx)
12165         {
12166           gcc_assert (!reload_completed);
12167           x = expand_binop (DImode, xor_optab, x, y, NULL_RTX, 0, OPTAB_WIDEN);
12168           y = const0_rtx;
12169         }
12170
12171       /* A scratch register is required.  */
12172       if (reload_completed)
12173         gcc_assert (scratch != NULL && GET_MODE (scratch) == SImode);
12174       else
12175         scratch = gen_rtx_SCRATCH (SImode);
12176
12177       clobber = gen_rtx_CLOBBER (VOIDmode, scratch);
12178       set = gen_rtx_SET (VOIDmode, cc_reg, gen_rtx_COMPARE (mode, x, y));
12179       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
12180     }
12181   else
12182     emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
12183
12184   return cc_reg;
12185 }
12186
12187 /* Generate a sequence of insns that will generate the correct return
12188    address mask depending on the physical architecture that the program
12189    is running on.  */
12190 rtx
12191 arm_gen_return_addr_mask (void)
12192 {
12193   rtx reg = gen_reg_rtx (Pmode);
12194
12195   emit_insn (gen_return_addr_mask (reg));
12196   return reg;
12197 }
12198
12199 void
12200 arm_reload_in_hi (rtx *operands)
12201 {
12202   rtx ref = operands[1];
12203   rtx base, scratch;
12204   HOST_WIDE_INT offset = 0;
12205
12206   if (GET_CODE (ref) == SUBREG)
12207     {
12208       offset = SUBREG_BYTE (ref);
12209       ref = SUBREG_REG (ref);
12210     }
12211
12212   if (REG_P (ref))
12213     {
12214       /* We have a pseudo which has been spilt onto the stack; there
12215          are two cases here: the first where there is a simple
12216          stack-slot replacement and a second where the stack-slot is
12217          out of range, or is used as a subreg.  */
12218       if (reg_equiv_mem (REGNO (ref)))
12219         {
12220           ref = reg_equiv_mem (REGNO (ref));
12221           base = find_replacement (&XEXP (ref, 0));
12222         }
12223       else
12224         /* The slot is out of range, or was dressed up in a SUBREG.  */
12225         base = reg_equiv_address (REGNO (ref));
12226     }
12227   else
12228     base = find_replacement (&XEXP (ref, 0));
12229
12230   /* Handle the case where the address is too complex to be offset by 1.  */
12231   if (GET_CODE (base) == MINUS
12232       || (GET_CODE (base) == PLUS && !CONST_INT_P (XEXP (base, 1))))
12233     {
12234       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
12235
12236       emit_set_insn (base_plus, base);
12237       base = base_plus;
12238     }
12239   else if (GET_CODE (base) == PLUS)
12240     {
12241       /* The addend must be CONST_INT, or we would have dealt with it above.  */
12242       HOST_WIDE_INT hi, lo;
12243
12244       offset += INTVAL (XEXP (base, 1));
12245       base = XEXP (base, 0);
12246
12247       /* Rework the address into a legal sequence of insns.  */
12248       /* Valid range for lo is -4095 -> 4095 */
12249       lo = (offset >= 0
12250             ? (offset & 0xfff)
12251             : -((-offset) & 0xfff));
12252
12253       /* Corner case, if lo is the max offset then we would be out of range
12254          once we have added the additional 1 below, so bump the msb into the
12255          pre-loading insn(s).  */
12256       if (lo == 4095)
12257         lo &= 0x7ff;
12258
12259       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
12260              ^ (HOST_WIDE_INT) 0x80000000)
12261             - (HOST_WIDE_INT) 0x80000000);
12262
12263       gcc_assert (hi + lo == offset);
12264
12265       if (hi != 0)
12266         {
12267           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
12268
12269           /* Get the base address; addsi3 knows how to handle constants
12270              that require more than one insn.  */
12271           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
12272           base = base_plus;
12273           offset = lo;
12274         }
12275     }
12276
12277   /* Operands[2] may overlap operands[0] (though it won't overlap
12278      operands[1]), that's why we asked for a DImode reg -- so we can
12279      use the bit that does not overlap.  */
12280   if (REGNO (operands[2]) == REGNO (operands[0]))
12281     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
12282   else
12283     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
12284
12285   emit_insn (gen_zero_extendqisi2 (scratch,
12286                                    gen_rtx_MEM (QImode,
12287                                                 plus_constant (Pmode, base,
12288                                                                offset))));
12289   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
12290                                    gen_rtx_MEM (QImode,
12291                                                 plus_constant (Pmode, base,
12292                                                                offset + 1))));
12293   if (!BYTES_BIG_ENDIAN)
12294     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
12295                    gen_rtx_IOR (SImode,
12296                                 gen_rtx_ASHIFT
12297                                 (SImode,
12298                                  gen_rtx_SUBREG (SImode, operands[0], 0),
12299                                  GEN_INT (8)),
12300                                 scratch));
12301   else
12302     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
12303                    gen_rtx_IOR (SImode,
12304                                 gen_rtx_ASHIFT (SImode, scratch,
12305                                                 GEN_INT (8)),
12306                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
12307 }
12308
12309 /* Handle storing a half-word to memory during reload by synthesizing as two
12310    byte stores.  Take care not to clobber the input values until after we
12311    have moved them somewhere safe.  This code assumes that if the DImode
12312    scratch in operands[2] overlaps either the input value or output address
12313    in some way, then that value must die in this insn (we absolutely need
12314    two scratch registers for some corner cases).  */
12315 void
12316 arm_reload_out_hi (rtx *operands)
12317 {
12318   rtx ref = operands[0];
12319   rtx outval = operands[1];
12320   rtx base, scratch;
12321   HOST_WIDE_INT offset = 0;
12322
12323   if (GET_CODE (ref) == SUBREG)
12324     {
12325       offset = SUBREG_BYTE (ref);
12326       ref = SUBREG_REG (ref);
12327     }
12328
12329   if (REG_P (ref))
12330     {
12331       /* We have a pseudo which has been spilt onto the stack; there
12332          are two cases here: the first where there is a simple
12333          stack-slot replacement and a second where the stack-slot is
12334          out of range, or is used as a subreg.  */
12335       if (reg_equiv_mem (REGNO (ref)))
12336         {
12337           ref = reg_equiv_mem (REGNO (ref));
12338           base = find_replacement (&XEXP (ref, 0));
12339         }
12340       else
12341         /* The slot is out of range, or was dressed up in a SUBREG.  */
12342         base = reg_equiv_address (REGNO (ref));
12343     }
12344   else
12345     base = find_replacement (&XEXP (ref, 0));
12346
12347   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
12348
12349   /* Handle the case where the address is too complex to be offset by 1.  */
12350   if (GET_CODE (base) == MINUS
12351       || (GET_CODE (base) == PLUS && !CONST_INT_P (XEXP (base, 1))))
12352     {
12353       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
12354
12355       /* Be careful not to destroy OUTVAL.  */
12356       if (reg_overlap_mentioned_p (base_plus, outval))
12357         {
12358           /* Updating base_plus might destroy outval, see if we can
12359              swap the scratch and base_plus.  */
12360           if (!reg_overlap_mentioned_p (scratch, outval))
12361             {
12362               rtx tmp = scratch;
12363               scratch = base_plus;
12364               base_plus = tmp;
12365             }
12366           else
12367             {
12368               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
12369
12370               /* Be conservative and copy OUTVAL into the scratch now,
12371                  this should only be necessary if outval is a subreg
12372                  of something larger than a word.  */
12373               /* XXX Might this clobber base?  I can't see how it can,
12374                  since scratch is known to overlap with OUTVAL, and
12375                  must be wider than a word.  */
12376               emit_insn (gen_movhi (scratch_hi, outval));
12377               outval = scratch_hi;
12378             }
12379         }
12380
12381       emit_set_insn (base_plus, base);
12382       base = base_plus;
12383     }
12384   else if (GET_CODE (base) == PLUS)
12385     {
12386       /* The addend must be CONST_INT, or we would have dealt with it above.  */
12387       HOST_WIDE_INT hi, lo;
12388
12389       offset += INTVAL (XEXP (base, 1));
12390       base = XEXP (base, 0);
12391
12392       /* Rework the address into a legal sequence of insns.  */
12393       /* Valid range for lo is -4095 -> 4095 */
12394       lo = (offset >= 0
12395             ? (offset & 0xfff)
12396             : -((-offset) & 0xfff));
12397
12398       /* Corner case, if lo is the max offset then we would be out of range
12399          once we have added the additional 1 below, so bump the msb into the
12400          pre-loading insn(s).  */
12401       if (lo == 4095)
12402         lo &= 0x7ff;
12403
12404       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
12405              ^ (HOST_WIDE_INT) 0x80000000)
12406             - (HOST_WIDE_INT) 0x80000000);
12407
12408       gcc_assert (hi + lo == offset);
12409
12410       if (hi != 0)
12411         {
12412           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
12413
12414           /* Be careful not to destroy OUTVAL.  */
12415           if (reg_overlap_mentioned_p (base_plus, outval))
12416             {
12417               /* Updating base_plus might destroy outval, see if we
12418                  can swap the scratch and base_plus.  */
12419               if (!reg_overlap_mentioned_p (scratch, outval))
12420                 {
12421                   rtx tmp = scratch;
12422                   scratch = base_plus;
12423                   base_plus = tmp;
12424                 }
12425               else
12426                 {
12427                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
12428
12429                   /* Be conservative and copy outval into scratch now,
12430                      this should only be necessary if outval is a
12431                      subreg of something larger than a word.  */
12432                   /* XXX Might this clobber base?  I can't see how it
12433                      can, since scratch is known to overlap with
12434                      outval.  */
12435                   emit_insn (gen_movhi (scratch_hi, outval));
12436                   outval = scratch_hi;
12437                 }
12438             }
12439
12440           /* Get the base address; addsi3 knows how to handle constants
12441              that require more than one insn.  */
12442           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
12443           base = base_plus;
12444           offset = lo;
12445         }
12446     }
12447
12448   if (BYTES_BIG_ENDIAN)
12449     {
12450       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
12451                                          plus_constant (Pmode, base,
12452                                                         offset + 1)),
12453                             gen_lowpart (QImode, outval)));
12454       emit_insn (gen_lshrsi3 (scratch,
12455                               gen_rtx_SUBREG (SImode, outval, 0),
12456                               GEN_INT (8)));
12457       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (Pmode, base,
12458                                                                 offset)),
12459                             gen_lowpart (QImode, scratch)));
12460     }
12461   else
12462     {
12463       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (Pmode, base,
12464                                                                 offset)),
12465                             gen_lowpart (QImode, outval)));
12466       emit_insn (gen_lshrsi3 (scratch,
12467                               gen_rtx_SUBREG (SImode, outval, 0),
12468                               GEN_INT (8)));
12469       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
12470                                          plus_constant (Pmode, base,
12471                                                         offset + 1)),
12472                             gen_lowpart (QImode, scratch)));
12473     }
12474 }
12475
12476 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
12477    (padded to the size of a word) should be passed in a register.  */
12478
12479 static bool
12480 arm_must_pass_in_stack (enum machine_mode mode, const_tree type)
12481 {
12482   if (TARGET_AAPCS_BASED)
12483     return must_pass_in_stack_var_size (mode, type);
12484   else
12485     return must_pass_in_stack_var_size_or_pad (mode, type);
12486 }
12487
12488
12489 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
12490    Return true if an argument passed on the stack should be padded upwards,
12491    i.e. if the least-significant byte has useful data.
12492    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
12493    aggregate types are placed in the lowest memory address.  */
12494
12495 bool
12496 arm_pad_arg_upward (enum machine_mode mode ATTRIBUTE_UNUSED, const_tree type)
12497 {
12498   if (!TARGET_AAPCS_BASED)
12499     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
12500
12501   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
12502     return false;
12503
12504   return true;
12505 }
12506
12507
12508 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
12509    Return !BYTES_BIG_ENDIAN if the least significant byte of the
12510    register has useful data, and return the opposite if the most
12511    significant byte does.  */
12512
12513 bool
12514 arm_pad_reg_upward (enum machine_mode mode,
12515                     tree type, int first ATTRIBUTE_UNUSED)
12516 {
12517   if (TARGET_AAPCS_BASED && BYTES_BIG_ENDIAN)
12518     {
12519       /* For AAPCS, small aggregates, small fixed-point types,
12520          and small complex types are always padded upwards.  */
12521       if (type)
12522         {
12523           if ((AGGREGATE_TYPE_P (type)
12524                || TREE_CODE (type) == COMPLEX_TYPE
12525                || FIXED_POINT_TYPE_P (type))
12526               && int_size_in_bytes (type) <= 4)
12527             return true;
12528         }
12529       else
12530         {
12531           if ((COMPLEX_MODE_P (mode) || ALL_FIXED_POINT_MODE_P (mode))
12532               && GET_MODE_SIZE (mode) <= 4)
12533             return true;
12534         }
12535     }
12536
12537   /* Otherwise, use default padding.  */
12538   return !BYTES_BIG_ENDIAN;
12539 }
12540
12541 /* Returns true iff OFFSET is valid for use in an LDRD/STRD instruction,
12542    assuming that the address in the base register is word aligned.  */
12543 bool
12544 offset_ok_for_ldrd_strd (HOST_WIDE_INT offset)
12545 {
12546   HOST_WIDE_INT max_offset;
12547
12548   /* Offset must be a multiple of 4 in Thumb mode.  */
12549   if (TARGET_THUMB2 && ((offset & 3) != 0))
12550     return false;
12551
12552   if (TARGET_THUMB2)
12553     max_offset = 1020;
12554   else if (TARGET_ARM)
12555     max_offset = 255;
12556   else
12557     return false;
12558
12559   return ((offset <= max_offset) && (offset >= -max_offset));
12560 }
12561
12562 /* Checks whether the operands are valid for use in an LDRD/STRD instruction.
12563    Assumes that RT, RT2, and RN are REG.  This is guaranteed by the patterns.
12564    Assumes that the address in the base register RN is word aligned.  Pattern
12565    guarantees that both memory accesses use the same base register,
12566    the offsets are constants within the range, and the gap between the offsets is 4.
12567    If preload complete then check that registers are legal.  WBACK indicates whether
12568    address is updated.  LOAD indicates whether memory access is load or store.  */
12569 bool
12570 operands_ok_ldrd_strd (rtx rt, rtx rt2, rtx rn, HOST_WIDE_INT offset,
12571                        bool wback, bool load)
12572 {
12573   unsigned int t, t2, n;
12574
12575   if (!reload_completed)
12576     return true;
12577
12578   if (!offset_ok_for_ldrd_strd (offset))
12579     return false;
12580
12581   t = REGNO (rt);
12582   t2 = REGNO (rt2);
12583   n = REGNO (rn);
12584
12585   if ((TARGET_THUMB2)
12586       && ((wback && (n == t || n == t2))
12587           || (t == SP_REGNUM)
12588           || (t == PC_REGNUM)
12589           || (t2 == SP_REGNUM)
12590           || (t2 == PC_REGNUM)
12591           || (!load && (n == PC_REGNUM))
12592           || (load && (t == t2))
12593           /* Triggers Cortex-M3 LDRD errata.  */
12594           || (!wback && load && fix_cm3_ldrd && (n == t))))
12595     return false;
12596
12597   if ((TARGET_ARM)
12598       && ((wback && (n == t || n == t2))
12599           || (t2 == PC_REGNUM)
12600           || (t % 2 != 0)   /* First destination register is not even.  */
12601           || (t2 != t + 1)
12602           /* PC can be used as base register (for offset addressing only),
12603              but it is depricated.  */
12604           || (n == PC_REGNUM)))
12605     return false;
12606
12607   return true;
12608 }
12609
12610 \f
12611 /* Print a symbolic form of X to the debug file, F.  */
12612 static void
12613 arm_print_value (FILE *f, rtx x)
12614 {
12615   switch (GET_CODE (x))
12616     {
12617     case CONST_INT:
12618       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
12619       return;
12620
12621     case CONST_DOUBLE:
12622       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
12623       return;
12624
12625     case CONST_VECTOR:
12626       {
12627         int i;
12628
12629         fprintf (f, "<");
12630         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
12631           {
12632             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
12633             if (i < (CONST_VECTOR_NUNITS (x) - 1))
12634               fputc (',', f);
12635           }
12636         fprintf (f, ">");
12637       }
12638       return;
12639
12640     case CONST_STRING:
12641       fprintf (f, "\"%s\"", XSTR (x, 0));
12642       return;
12643
12644     case SYMBOL_REF:
12645       fprintf (f, "`%s'", XSTR (x, 0));
12646       return;
12647
12648     case LABEL_REF:
12649       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
12650       return;
12651
12652     case CONST:
12653       arm_print_value (f, XEXP (x, 0));
12654       return;
12655
12656     case PLUS:
12657       arm_print_value (f, XEXP (x, 0));
12658       fprintf (f, "+");
12659       arm_print_value (f, XEXP (x, 1));
12660       return;
12661
12662     case PC:
12663       fprintf (f, "pc");
12664       return;
12665
12666     default:
12667       fprintf (f, "????");
12668       return;
12669     }
12670 }
12671 \f
12672 /* Routines for manipulation of the constant pool.  */
12673
12674 /* Arm instructions cannot load a large constant directly into a
12675    register; they have to come from a pc relative load.  The constant
12676    must therefore be placed in the addressable range of the pc
12677    relative load.  Depending on the precise pc relative load
12678    instruction the range is somewhere between 256 bytes and 4k.  This
12679    means that we often have to dump a constant inside a function, and
12680    generate code to branch around it.
12681
12682    It is important to minimize this, since the branches will slow
12683    things down and make the code larger.
12684
12685    Normally we can hide the table after an existing unconditional
12686    branch so that there is no interruption of the flow, but in the
12687    worst case the code looks like this:
12688
12689         ldr     rn, L1
12690         ...
12691         b       L2
12692         align
12693         L1:     .long value
12694         L2:
12695         ...
12696
12697         ldr     rn, L3
12698         ...
12699         b       L4
12700         align
12701         L3:     .long value
12702         L4:
12703         ...
12704
12705    We fix this by performing a scan after scheduling, which notices
12706    which instructions need to have their operands fetched from the
12707    constant table and builds the table.
12708
12709    The algorithm starts by building a table of all the constants that
12710    need fixing up and all the natural barriers in the function (places
12711    where a constant table can be dropped without breaking the flow).
12712    For each fixup we note how far the pc-relative replacement will be
12713    able to reach and the offset of the instruction into the function.
12714
12715    Having built the table we then group the fixes together to form
12716    tables that are as large as possible (subject to addressing
12717    constraints) and emit each table of constants after the last
12718    barrier that is within range of all the instructions in the group.
12719    If a group does not contain a barrier, then we forcibly create one
12720    by inserting a jump instruction into the flow.  Once the table has
12721    been inserted, the insns are then modified to reference the
12722    relevant entry in the pool.
12723
12724    Possible enhancements to the algorithm (not implemented) are:
12725
12726    1) For some processors and object formats, there may be benefit in
12727    aligning the pools to the start of cache lines; this alignment
12728    would need to be taken into account when calculating addressability
12729    of a pool.  */
12730
12731 /* These typedefs are located at the start of this file, so that
12732    they can be used in the prototypes there.  This comment is to
12733    remind readers of that fact so that the following structures
12734    can be understood more easily.
12735
12736      typedef struct minipool_node    Mnode;
12737      typedef struct minipool_fixup   Mfix;  */
12738
12739 struct minipool_node
12740 {
12741   /* Doubly linked chain of entries.  */
12742   Mnode * next;
12743   Mnode * prev;
12744   /* The maximum offset into the code that this entry can be placed.  While
12745      pushing fixes for forward references, all entries are sorted in order
12746      of increasing max_address.  */
12747   HOST_WIDE_INT max_address;
12748   /* Similarly for an entry inserted for a backwards ref.  */
12749   HOST_WIDE_INT min_address;
12750   /* The number of fixes referencing this entry.  This can become zero
12751      if we "unpush" an entry.  In this case we ignore the entry when we
12752      come to emit the code.  */
12753   int refcount;
12754   /* The offset from the start of the minipool.  */
12755   HOST_WIDE_INT offset;
12756   /* The value in table.  */
12757   rtx value;
12758   /* The mode of value.  */
12759   enum machine_mode mode;
12760   /* The size of the value.  With iWMMXt enabled
12761      sizes > 4 also imply an alignment of 8-bytes.  */
12762   int fix_size;
12763 };
12764
12765 struct minipool_fixup
12766 {
12767   Mfix *            next;
12768   rtx               insn;
12769   HOST_WIDE_INT     address;
12770   rtx *             loc;
12771   enum machine_mode mode;
12772   int               fix_size;
12773   rtx               value;
12774   Mnode *           minipool;
12775   HOST_WIDE_INT     forwards;
12776   HOST_WIDE_INT     backwards;
12777 };
12778
12779 /* Fixes less than a word need padding out to a word boundary.  */
12780 #define MINIPOOL_FIX_SIZE(mode) \
12781   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
12782
12783 static Mnode *  minipool_vector_head;
12784 static Mnode *  minipool_vector_tail;
12785 static rtx      minipool_vector_label;
12786 static int      minipool_pad;
12787
12788 /* The linked list of all minipool fixes required for this function.  */
12789 Mfix *          minipool_fix_head;
12790 Mfix *          minipool_fix_tail;
12791 /* The fix entry for the current minipool, once it has been placed.  */
12792 Mfix *          minipool_barrier;
12793
12794 /* Determines if INSN is the start of a jump table.  Returns the end
12795    of the TABLE or NULL_RTX.  */
12796 static rtx
12797 is_jump_table (rtx insn)
12798 {
12799   rtx table;
12800
12801   if (jump_to_label_p (insn)
12802       && ((table = next_real_insn (JUMP_LABEL (insn)))
12803           == next_real_insn (insn))
12804       && table != NULL
12805       && JUMP_P (table)
12806       && (GET_CODE (PATTERN (table)) == ADDR_VEC
12807           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
12808     return table;
12809
12810   return NULL_RTX;
12811 }
12812
12813 #ifndef JUMP_TABLES_IN_TEXT_SECTION
12814 #define JUMP_TABLES_IN_TEXT_SECTION 0
12815 #endif
12816
12817 static HOST_WIDE_INT
12818 get_jump_table_size (rtx insn)
12819 {
12820   /* ADDR_VECs only take room if read-only data does into the text
12821      section.  */
12822   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
12823     {
12824       rtx body = PATTERN (insn);
12825       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
12826       HOST_WIDE_INT size;
12827       HOST_WIDE_INT modesize;
12828
12829       modesize = GET_MODE_SIZE (GET_MODE (body));
12830       size = modesize * XVECLEN (body, elt);
12831       switch (modesize)
12832         {
12833         case 1:
12834           /* Round up size  of TBB table to a halfword boundary.  */
12835           size = (size + 1) & ~(HOST_WIDE_INT)1;
12836           break;
12837         case 2:
12838           /* No padding necessary for TBH.  */
12839           break;
12840         case 4:
12841           /* Add two bytes for alignment on Thumb.  */
12842           if (TARGET_THUMB)
12843             size += 2;
12844           break;
12845         default:
12846           gcc_unreachable ();
12847         }
12848       return size;
12849     }
12850
12851   return 0;
12852 }
12853
12854 /* Return the maximum amount of padding that will be inserted before
12855    label LABEL.  */
12856
12857 static HOST_WIDE_INT
12858 get_label_padding (rtx label)
12859 {
12860   HOST_WIDE_INT align, min_insn_size;
12861
12862   align = 1 << label_to_alignment (label);
12863   min_insn_size = TARGET_THUMB ? 2 : 4;
12864   return align > min_insn_size ? align - min_insn_size : 0;
12865 }
12866
12867 /* Move a minipool fix MP from its current location to before MAX_MP.
12868    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
12869    constraints may need updating.  */
12870 static Mnode *
12871 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
12872                                HOST_WIDE_INT max_address)
12873 {
12874   /* The code below assumes these are different.  */
12875   gcc_assert (mp != max_mp);
12876
12877   if (max_mp == NULL)
12878     {
12879       if (max_address < mp->max_address)
12880         mp->max_address = max_address;
12881     }
12882   else
12883     {
12884       if (max_address > max_mp->max_address - mp->fix_size)
12885         mp->max_address = max_mp->max_address - mp->fix_size;
12886       else
12887         mp->max_address = max_address;
12888
12889       /* Unlink MP from its current position.  Since max_mp is non-null,
12890        mp->prev must be non-null.  */
12891       mp->prev->next = mp->next;
12892       if (mp->next != NULL)
12893         mp->next->prev = mp->prev;
12894       else
12895         minipool_vector_tail = mp->prev;
12896
12897       /* Re-insert it before MAX_MP.  */
12898       mp->next = max_mp;
12899       mp->prev = max_mp->prev;
12900       max_mp->prev = mp;
12901
12902       if (mp->prev != NULL)
12903         mp->prev->next = mp;
12904       else
12905         minipool_vector_head = mp;
12906     }
12907
12908   /* Save the new entry.  */
12909   max_mp = mp;
12910
12911   /* Scan over the preceding entries and adjust their addresses as
12912      required.  */
12913   while (mp->prev != NULL
12914          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
12915     {
12916       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
12917       mp = mp->prev;
12918     }
12919
12920   return max_mp;
12921 }
12922
12923 /* Add a constant to the minipool for a forward reference.  Returns the
12924    node added or NULL if the constant will not fit in this pool.  */
12925 static Mnode *
12926 add_minipool_forward_ref (Mfix *fix)
12927 {
12928   /* If set, max_mp is the first pool_entry that has a lower
12929      constraint than the one we are trying to add.  */
12930   Mnode *       max_mp = NULL;
12931   HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
12932   Mnode *       mp;
12933
12934   /* If the minipool starts before the end of FIX->INSN then this FIX
12935      can not be placed into the current pool.  Furthermore, adding the
12936      new constant pool entry may cause the pool to start FIX_SIZE bytes
12937      earlier.  */
12938   if (minipool_vector_head &&
12939       (fix->address + get_attr_length (fix->insn)
12940        >= minipool_vector_head->max_address - fix->fix_size))
12941     return NULL;
12942
12943   /* Scan the pool to see if a constant with the same value has
12944      already been added.  While we are doing this, also note the
12945      location where we must insert the constant if it doesn't already
12946      exist.  */
12947   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
12948     {
12949       if (GET_CODE (fix->value) == GET_CODE (mp->value)
12950           && fix->mode == mp->mode
12951           && (!LABEL_P (fix->value)
12952               || (CODE_LABEL_NUMBER (fix->value)
12953                   == CODE_LABEL_NUMBER (mp->value)))
12954           && rtx_equal_p (fix->value, mp->value))
12955         {
12956           /* More than one fix references this entry.  */
12957           mp->refcount++;
12958           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
12959         }
12960
12961       /* Note the insertion point if necessary.  */
12962       if (max_mp == NULL
12963           && mp->max_address > max_address)
12964         max_mp = mp;
12965
12966       /* If we are inserting an 8-bytes aligned quantity and
12967          we have not already found an insertion point, then
12968          make sure that all such 8-byte aligned quantities are
12969          placed at the start of the pool.  */
12970       if (ARM_DOUBLEWORD_ALIGN
12971           && max_mp == NULL
12972           && fix->fix_size >= 8
12973           && mp->fix_size < 8)
12974         {
12975           max_mp = mp;
12976           max_address = mp->max_address;
12977         }
12978     }
12979
12980   /* The value is not currently in the minipool, so we need to create
12981      a new entry for it.  If MAX_MP is NULL, the entry will be put on
12982      the end of the list since the placement is less constrained than
12983      any existing entry.  Otherwise, we insert the new fix before
12984      MAX_MP and, if necessary, adjust the constraints on the other
12985      entries.  */
12986   mp = XNEW (Mnode);
12987   mp->fix_size = fix->fix_size;
12988   mp->mode = fix->mode;
12989   mp->value = fix->value;
12990   mp->refcount = 1;
12991   /* Not yet required for a backwards ref.  */
12992   mp->min_address = -65536;
12993
12994   if (max_mp == NULL)
12995     {
12996       mp->max_address = max_address;
12997       mp->next = NULL;
12998       mp->prev = minipool_vector_tail;
12999
13000       if (mp->prev == NULL)
13001         {
13002           minipool_vector_head = mp;
13003           minipool_vector_label = gen_label_rtx ();
13004         }
13005       else
13006         mp->prev->next = mp;
13007
13008       minipool_vector_tail = mp;
13009     }
13010   else
13011     {
13012       if (max_address > max_mp->max_address - mp->fix_size)
13013         mp->max_address = max_mp->max_address - mp->fix_size;
13014       else
13015         mp->max_address = max_address;
13016
13017       mp->next = max_mp;
13018       mp->prev = max_mp->prev;
13019       max_mp->prev = mp;
13020       if (mp->prev != NULL)
13021         mp->prev->next = mp;
13022       else
13023         minipool_vector_head = mp;
13024     }
13025
13026   /* Save the new entry.  */
13027   max_mp = mp;
13028
13029   /* Scan over the preceding entries and adjust their addresses as
13030      required.  */
13031   while (mp->prev != NULL
13032          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
13033     {
13034       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
13035       mp = mp->prev;
13036     }
13037
13038   return max_mp;
13039 }
13040
13041 static Mnode *
13042 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
13043                                 HOST_WIDE_INT  min_address)
13044 {
13045   HOST_WIDE_INT offset;
13046
13047   /* The code below assumes these are different.  */
13048   gcc_assert (mp != min_mp);
13049
13050   if (min_mp == NULL)
13051     {
13052       if (min_address > mp->min_address)
13053         mp->min_address = min_address;
13054     }
13055   else
13056     {
13057       /* We will adjust this below if it is too loose.  */
13058       mp->min_address = min_address;
13059
13060       /* Unlink MP from its current position.  Since min_mp is non-null,
13061          mp->next must be non-null.  */
13062       mp->next->prev = mp->prev;
13063       if (mp->prev != NULL)
13064         mp->prev->next = mp->next;
13065       else
13066         minipool_vector_head = mp->next;
13067
13068       /* Reinsert it after MIN_MP.  */
13069       mp->prev = min_mp;
13070       mp->next = min_mp->next;
13071       min_mp->next = mp;
13072       if (mp->next != NULL)
13073         mp->next->prev = mp;
13074       else
13075         minipool_vector_tail = mp;
13076     }
13077
13078   min_mp = mp;
13079
13080   offset = 0;
13081   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
13082     {
13083       mp->offset = offset;
13084       if (mp->refcount > 0)
13085         offset += mp->fix_size;
13086
13087       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
13088         mp->next->min_address = mp->min_address + mp->fix_size;
13089     }
13090
13091   return min_mp;
13092 }
13093
13094 /* Add a constant to the minipool for a backward reference.  Returns the
13095    node added or NULL if the constant will not fit in this pool.
13096
13097    Note that the code for insertion for a backwards reference can be
13098    somewhat confusing because the calculated offsets for each fix do
13099    not take into account the size of the pool (which is still under
13100    construction.  */
13101 static Mnode *
13102 add_minipool_backward_ref (Mfix *fix)
13103 {
13104   /* If set, min_mp is the last pool_entry that has a lower constraint
13105      than the one we are trying to add.  */
13106   Mnode *min_mp = NULL;
13107   /* This can be negative, since it is only a constraint.  */
13108   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
13109   Mnode *mp;
13110
13111   /* If we can't reach the current pool from this insn, or if we can't
13112      insert this entry at the end of the pool without pushing other
13113      fixes out of range, then we don't try.  This ensures that we
13114      can't fail later on.  */
13115   if (min_address >= minipool_barrier->address
13116       || (minipool_vector_tail->min_address + fix->fix_size
13117           >= minipool_barrier->address))
13118     return NULL;
13119
13120   /* Scan the pool to see if a constant with the same value has
13121      already been added.  While we are doing this, also note the
13122      location where we must insert the constant if it doesn't already
13123      exist.  */
13124   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
13125     {
13126       if (GET_CODE (fix->value) == GET_CODE (mp->value)
13127           && fix->mode == mp->mode
13128           && (!LABEL_P (fix->value)
13129               || (CODE_LABEL_NUMBER (fix->value)
13130                   == CODE_LABEL_NUMBER (mp->value)))
13131           && rtx_equal_p (fix->value, mp->value)
13132           /* Check that there is enough slack to move this entry to the
13133              end of the table (this is conservative).  */
13134           && (mp->max_address
13135               > (minipool_barrier->address
13136                  + minipool_vector_tail->offset
13137                  + minipool_vector_tail->fix_size)))
13138         {
13139           mp->refcount++;
13140           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
13141         }
13142
13143       if (min_mp != NULL)
13144         mp->min_address += fix->fix_size;
13145       else
13146         {
13147           /* Note the insertion point if necessary.  */
13148           if (mp->min_address < min_address)
13149             {
13150               /* For now, we do not allow the insertion of 8-byte alignment
13151                  requiring nodes anywhere but at the start of the pool.  */
13152               if (ARM_DOUBLEWORD_ALIGN
13153                   && fix->fix_size >= 8 && mp->fix_size < 8)
13154                 return NULL;
13155               else
13156                 min_mp = mp;
13157             }
13158           else if (mp->max_address
13159                    < minipool_barrier->address + mp->offset + fix->fix_size)
13160             {
13161               /* Inserting before this entry would push the fix beyond
13162                  its maximum address (which can happen if we have
13163                  re-located a forwards fix); force the new fix to come
13164                  after it.  */
13165               if (ARM_DOUBLEWORD_ALIGN
13166                   && fix->fix_size >= 8 && mp->fix_size < 8)
13167                 return NULL;
13168               else
13169                 {
13170                   min_mp = mp;
13171                   min_address = mp->min_address + fix->fix_size;
13172                 }
13173             }
13174           /* Do not insert a non-8-byte aligned quantity before 8-byte
13175              aligned quantities.  */
13176           else if (ARM_DOUBLEWORD_ALIGN
13177                    && fix->fix_size < 8
13178                    && mp->fix_size >= 8)
13179             {
13180               min_mp = mp;
13181               min_address = mp->min_address + fix->fix_size;
13182             }
13183         }
13184     }
13185
13186   /* We need to create a new entry.  */
13187   mp = XNEW (Mnode);
13188   mp->fix_size = fix->fix_size;
13189   mp->mode = fix->mode;
13190   mp->value = fix->value;
13191   mp->refcount = 1;
13192   mp->max_address = minipool_barrier->address + 65536;
13193
13194   mp->min_address = min_address;
13195
13196   if (min_mp == NULL)
13197     {
13198       mp->prev = NULL;
13199       mp->next = minipool_vector_head;
13200
13201       if (mp->next == NULL)
13202         {
13203           minipool_vector_tail = mp;
13204           minipool_vector_label = gen_label_rtx ();
13205         }
13206       else
13207         mp->next->prev = mp;
13208
13209       minipool_vector_head = mp;
13210     }
13211   else
13212     {
13213       mp->next = min_mp->next;
13214       mp->prev = min_mp;
13215       min_mp->next = mp;
13216
13217       if (mp->next != NULL)
13218         mp->next->prev = mp;
13219       else
13220         minipool_vector_tail = mp;
13221     }
13222
13223   /* Save the new entry.  */
13224   min_mp = mp;
13225
13226   if (mp->prev)
13227     mp = mp->prev;
13228   else
13229     mp->offset = 0;
13230
13231   /* Scan over the following entries and adjust their offsets.  */
13232   while (mp->next != NULL)
13233     {
13234       if (mp->next->min_address < mp->min_address + mp->fix_size)
13235         mp->next->min_address = mp->min_address + mp->fix_size;
13236
13237       if (mp->refcount)
13238         mp->next->offset = mp->offset + mp->fix_size;
13239       else
13240         mp->next->offset = mp->offset;
13241
13242       mp = mp->next;
13243     }
13244
13245   return min_mp;
13246 }
13247
13248 static void
13249 assign_minipool_offsets (Mfix *barrier)
13250 {
13251   HOST_WIDE_INT offset = 0;
13252   Mnode *mp;
13253
13254   minipool_barrier = barrier;
13255
13256   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
13257     {
13258       mp->offset = offset;
13259
13260       if (mp->refcount > 0)
13261         offset += mp->fix_size;
13262     }
13263 }
13264
13265 /* Output the literal table */
13266 static void
13267 dump_minipool (rtx scan)
13268 {
13269   Mnode * mp;
13270   Mnode * nmp;
13271   int align64 = 0;
13272
13273   if (ARM_DOUBLEWORD_ALIGN)
13274     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
13275       if (mp->refcount > 0 && mp->fix_size >= 8)
13276         {
13277           align64 = 1;
13278           break;
13279         }
13280
13281   if (dump_file)
13282     fprintf (dump_file,
13283              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
13284              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
13285
13286   scan = emit_label_after (gen_label_rtx (), scan);
13287   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
13288   scan = emit_label_after (minipool_vector_label, scan);
13289
13290   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
13291     {
13292       if (mp->refcount > 0)
13293         {
13294           if (dump_file)
13295             {
13296               fprintf (dump_file,
13297                        ";;  Offset %u, min %ld, max %ld ",
13298                        (unsigned) mp->offset, (unsigned long) mp->min_address,
13299                        (unsigned long) mp->max_address);
13300               arm_print_value (dump_file, mp->value);
13301               fputc ('\n', dump_file);
13302             }
13303
13304           switch (mp->fix_size)
13305             {
13306 #ifdef HAVE_consttable_1
13307             case 1:
13308               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
13309               break;
13310
13311 #endif
13312 #ifdef HAVE_consttable_2
13313             case 2:
13314               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
13315               break;
13316
13317 #endif
13318 #ifdef HAVE_consttable_4
13319             case 4:
13320               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
13321               break;
13322
13323 #endif
13324 #ifdef HAVE_consttable_8
13325             case 8:
13326               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
13327               break;
13328
13329 #endif
13330 #ifdef HAVE_consttable_16
13331             case 16:
13332               scan = emit_insn_after (gen_consttable_16 (mp->value), scan);
13333               break;
13334
13335 #endif
13336             default:
13337               gcc_unreachable ();
13338             }
13339         }
13340
13341       nmp = mp->next;
13342       free (mp);
13343     }
13344
13345   minipool_vector_head = minipool_vector_tail = NULL;
13346   scan = emit_insn_after (gen_consttable_end (), scan);
13347   scan = emit_barrier_after (scan);
13348 }
13349
13350 /* Return the cost of forcibly inserting a barrier after INSN.  */
13351 static int
13352 arm_barrier_cost (rtx insn)
13353 {
13354   /* Basing the location of the pool on the loop depth is preferable,
13355      but at the moment, the basic block information seems to be
13356      corrupt by this stage of the compilation.  */
13357   int base_cost = 50;
13358   rtx next = next_nonnote_insn (insn);
13359
13360   if (next != NULL && LABEL_P (next))
13361     base_cost -= 20;
13362
13363   switch (GET_CODE (insn))
13364     {
13365     case CODE_LABEL:
13366       /* It will always be better to place the table before the label, rather
13367          than after it.  */
13368       return 50;
13369
13370     case INSN:
13371     case CALL_INSN:
13372       return base_cost;
13373
13374     case JUMP_INSN:
13375       return base_cost - 10;
13376
13377     default:
13378       return base_cost + 10;
13379     }
13380 }
13381
13382 /* Find the best place in the insn stream in the range
13383    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
13384    Create the barrier by inserting a jump and add a new fix entry for
13385    it.  */
13386 static Mfix *
13387 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
13388 {
13389   HOST_WIDE_INT count = 0;
13390   rtx barrier;
13391   rtx from = fix->insn;
13392   /* The instruction after which we will insert the jump.  */
13393   rtx selected = NULL;
13394   int selected_cost;
13395   /* The address at which the jump instruction will be placed.  */
13396   HOST_WIDE_INT selected_address;
13397   Mfix * new_fix;
13398   HOST_WIDE_INT max_count = max_address - fix->address;
13399   rtx label = gen_label_rtx ();
13400
13401   selected_cost = arm_barrier_cost (from);
13402   selected_address = fix->address;
13403
13404   while (from && count < max_count)
13405     {
13406       rtx tmp;
13407       int new_cost;
13408
13409       /* This code shouldn't have been called if there was a natural barrier
13410          within range.  */
13411       gcc_assert (!BARRIER_P (from));
13412
13413       /* Count the length of this insn.  This must stay in sync with the
13414          code that pushes minipool fixes.  */
13415       if (LABEL_P (from))
13416         count += get_label_padding (from);
13417       else
13418         count += get_attr_length (from);
13419
13420       /* If there is a jump table, add its length.  */
13421       tmp = is_jump_table (from);
13422       if (tmp != NULL)
13423         {
13424           count += get_jump_table_size (tmp);
13425
13426           /* Jump tables aren't in a basic block, so base the cost on
13427              the dispatch insn.  If we select this location, we will
13428              still put the pool after the table.  */
13429           new_cost = arm_barrier_cost (from);
13430
13431           if (count < max_count
13432               && (!selected || new_cost <= selected_cost))
13433             {
13434               selected = tmp;
13435               selected_cost = new_cost;
13436               selected_address = fix->address + count;
13437             }
13438
13439           /* Continue after the dispatch table.  */
13440           from = NEXT_INSN (tmp);
13441           continue;
13442         }
13443
13444       new_cost = arm_barrier_cost (from);
13445
13446       if (count < max_count
13447           && (!selected || new_cost <= selected_cost))
13448         {
13449           selected = from;
13450           selected_cost = new_cost;
13451           selected_address = fix->address + count;
13452         }
13453
13454       from = NEXT_INSN (from);
13455     }
13456
13457   /* Make sure that we found a place to insert the jump.  */
13458   gcc_assert (selected);
13459
13460   /* Make sure we do not split a call and its corresponding
13461      CALL_ARG_LOCATION note.  */
13462   if (CALL_P (selected))
13463     {
13464       rtx next = NEXT_INSN (selected);
13465       if (next && NOTE_P (next)
13466           && NOTE_KIND (next) == NOTE_INSN_CALL_ARG_LOCATION)
13467           selected = next;
13468     }
13469
13470   /* Create a new JUMP_INSN that branches around a barrier.  */
13471   from = emit_jump_insn_after (gen_jump (label), selected);
13472   JUMP_LABEL (from) = label;
13473   barrier = emit_barrier_after (from);
13474   emit_label_after (label, barrier);
13475
13476   /* Create a minipool barrier entry for the new barrier.  */
13477   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
13478   new_fix->insn = barrier;
13479   new_fix->address = selected_address;
13480   new_fix->next = fix->next;
13481   fix->next = new_fix;
13482
13483   return new_fix;
13484 }
13485
13486 /* Record that there is a natural barrier in the insn stream at
13487    ADDRESS.  */
13488 static void
13489 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
13490 {
13491   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
13492
13493   fix->insn = insn;
13494   fix->address = address;
13495
13496   fix->next = NULL;
13497   if (minipool_fix_head != NULL)
13498     minipool_fix_tail->next = fix;
13499   else
13500     minipool_fix_head = fix;
13501
13502   minipool_fix_tail = fix;
13503 }
13504
13505 /* Record INSN, which will need fixing up to load a value from the
13506    minipool.  ADDRESS is the offset of the insn since the start of the
13507    function; LOC is a pointer to the part of the insn which requires
13508    fixing; VALUE is the constant that must be loaded, which is of type
13509    MODE.  */
13510 static void
13511 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
13512                    enum machine_mode mode, rtx value)
13513 {
13514   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
13515
13516   fix->insn = insn;
13517   fix->address = address;
13518   fix->loc = loc;
13519   fix->mode = mode;
13520   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
13521   fix->value = value;
13522   fix->forwards = get_attr_pool_range (insn);
13523   fix->backwards = get_attr_neg_pool_range (insn);
13524   fix->minipool = NULL;
13525
13526   /* If an insn doesn't have a range defined for it, then it isn't
13527      expecting to be reworked by this code.  Better to stop now than
13528      to generate duff assembly code.  */
13529   gcc_assert (fix->forwards || fix->backwards);
13530
13531   /* If an entry requires 8-byte alignment then assume all constant pools
13532      require 4 bytes of padding.  Trying to do this later on a per-pool
13533      basis is awkward because existing pool entries have to be modified.  */
13534   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size >= 8)
13535     minipool_pad = 4;
13536
13537   if (dump_file)
13538     {
13539       fprintf (dump_file,
13540                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
13541                GET_MODE_NAME (mode),
13542                INSN_UID (insn), (unsigned long) address,
13543                -1 * (long)fix->backwards, (long)fix->forwards);
13544       arm_print_value (dump_file, fix->value);
13545       fprintf (dump_file, "\n");
13546     }
13547
13548   /* Add it to the chain of fixes.  */
13549   fix->next = NULL;
13550
13551   if (minipool_fix_head != NULL)
13552     minipool_fix_tail->next = fix;
13553   else
13554     minipool_fix_head = fix;
13555
13556   minipool_fix_tail = fix;
13557 }
13558
13559 /* Return the cost of synthesizing a 64-bit constant VAL inline.
13560    Returns the number of insns needed, or 99 if we don't know how to
13561    do it.  */
13562 int
13563 arm_const_double_inline_cost (rtx val)
13564 {
13565   rtx lowpart, highpart;
13566   enum machine_mode mode;
13567
13568   mode = GET_MODE (val);
13569
13570   if (mode == VOIDmode)
13571     mode = DImode;
13572
13573   gcc_assert (GET_MODE_SIZE (mode) == 8);
13574
13575   lowpart = gen_lowpart (SImode, val);
13576   highpart = gen_highpart_mode (SImode, mode, val);
13577
13578   gcc_assert (CONST_INT_P (lowpart));
13579   gcc_assert (CONST_INT_P (highpart));
13580
13581   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
13582                             NULL_RTX, NULL_RTX, 0, 0)
13583           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
13584                               NULL_RTX, NULL_RTX, 0, 0));
13585 }
13586
13587 /* Return true if it is worthwhile to split a 64-bit constant into two
13588    32-bit operations.  This is the case if optimizing for size, or
13589    if we have load delay slots, or if one 32-bit part can be done with
13590    a single data operation.  */
13591 bool
13592 arm_const_double_by_parts (rtx val)
13593 {
13594   enum machine_mode mode = GET_MODE (val);
13595   rtx part;
13596
13597   if (optimize_size || arm_ld_sched)
13598     return true;
13599
13600   if (mode == VOIDmode)
13601     mode = DImode;
13602
13603   part = gen_highpart_mode (SImode, mode, val);
13604
13605   gcc_assert (CONST_INT_P (part));
13606
13607   if (const_ok_for_arm (INTVAL (part))
13608       || const_ok_for_arm (~INTVAL (part)))
13609     return true;
13610
13611   part = gen_lowpart (SImode, val);
13612
13613   gcc_assert (CONST_INT_P (part));
13614
13615   if (const_ok_for_arm (INTVAL (part))
13616       || const_ok_for_arm (~INTVAL (part)))
13617     return true;
13618
13619   return false;
13620 }
13621
13622 /* Return true if it is possible to inline both the high and low parts
13623    of a 64-bit constant into 32-bit data processing instructions.  */
13624 bool
13625 arm_const_double_by_immediates (rtx val)
13626 {
13627   enum machine_mode mode = GET_MODE (val);
13628   rtx part;
13629
13630   if (mode == VOIDmode)
13631     mode = DImode;
13632
13633   part = gen_highpart_mode (SImode, mode, val);
13634
13635   gcc_assert (CONST_INT_P (part));
13636
13637   if (!const_ok_for_arm (INTVAL (part)))
13638     return false;
13639
13640   part = gen_lowpart (SImode, val);
13641
13642   gcc_assert (CONST_INT_P (part));
13643
13644   if (!const_ok_for_arm (INTVAL (part)))
13645     return false;
13646
13647   return true;
13648 }
13649
13650 /* Scan INSN and note any of its operands that need fixing.
13651    If DO_PUSHES is false we do not actually push any of the fixups
13652    needed.  */
13653 static void
13654 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
13655 {
13656   int opno;
13657
13658   extract_insn (insn);
13659
13660   if (!constrain_operands (1))
13661     fatal_insn_not_found (insn);
13662
13663   if (recog_data.n_alternatives == 0)
13664     return;
13665
13666   /* Fill in recog_op_alt with information about the constraints of
13667      this insn.  */
13668   preprocess_constraints ();
13669
13670   for (opno = 0; opno < recog_data.n_operands; opno++)
13671     {
13672       /* Things we need to fix can only occur in inputs.  */
13673       if (recog_data.operand_type[opno] != OP_IN)
13674         continue;
13675
13676       /* If this alternative is a memory reference, then any mention
13677          of constants in this alternative is really to fool reload
13678          into allowing us to accept one there.  We need to fix them up
13679          now so that we output the right code.  */
13680       if (recog_op_alt[opno][which_alternative].memory_ok)
13681         {
13682           rtx op = recog_data.operand[opno];
13683
13684           if (CONSTANT_P (op))
13685             {
13686               if (do_pushes)
13687                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
13688                                    recog_data.operand_mode[opno], op);
13689             }
13690           else if (MEM_P (op)
13691                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
13692                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
13693             {
13694               if (do_pushes)
13695                 {
13696                   rtx cop = avoid_constant_pool_reference (op);
13697
13698                   /* Casting the address of something to a mode narrower
13699                      than a word can cause avoid_constant_pool_reference()
13700                      to return the pool reference itself.  That's no good to
13701                      us here.  Lets just hope that we can use the
13702                      constant pool value directly.  */
13703                   if (op == cop)
13704                     cop = get_pool_constant (XEXP (op, 0));
13705
13706                   push_minipool_fix (insn, address,
13707                                      recog_data.operand_loc[opno],
13708                                      recog_data.operand_mode[opno], cop);
13709                 }
13710
13711             }
13712         }
13713     }
13714
13715   return;
13716 }
13717
13718 /* Rewrite move insn into subtract of 0 if the condition codes will
13719    be useful in next conditional jump insn.  */
13720
13721 static void
13722 thumb1_reorg (void)
13723 {
13724   basic_block bb;
13725
13726   FOR_EACH_BB (bb)
13727     {
13728       rtx set, dest, src;
13729       rtx pat, op0;
13730       rtx prev, insn = BB_END (bb);
13731
13732       while (insn != BB_HEAD (bb) && DEBUG_INSN_P (insn))
13733         insn = PREV_INSN (insn);
13734
13735       /* Find the last cbranchsi4_insn in basic block BB.  */
13736       if (INSN_CODE (insn) != CODE_FOR_cbranchsi4_insn)
13737         continue;
13738
13739       /* Find the first non-note insn before INSN in basic block BB.  */
13740       gcc_assert (insn != BB_HEAD (bb));
13741       prev = PREV_INSN (insn);
13742       while (prev != BB_HEAD (bb) && (NOTE_P (prev) || DEBUG_INSN_P (prev)))
13743         prev = PREV_INSN (prev);
13744
13745       set = single_set (prev);
13746       if (!set)
13747         continue;
13748
13749       dest = SET_DEST (set);
13750       src = SET_SRC (set);
13751       if (!low_register_operand (dest, SImode)
13752           || !low_register_operand (src, SImode))
13753         continue;
13754
13755       pat = PATTERN (insn);
13756       op0 = XEXP (XEXP (SET_SRC (pat), 0), 0);
13757       /* Rewrite move into subtract of 0 if its operand is compared with ZERO
13758          in INSN. Don't need to check dest since cprop_hardreg pass propagates
13759          src into INSN.  */
13760       if (REGNO (op0) == REGNO (src))
13761         {
13762           dest = copy_rtx (dest);
13763           src = copy_rtx (src);
13764           src = gen_rtx_MINUS (SImode, src, const0_rtx);
13765           PATTERN (prev) = gen_rtx_SET (VOIDmode, dest, src);
13766           INSN_CODE (prev) = -1;
13767           /* Set test register in INSN to dest.  */
13768           XEXP (XEXP (SET_SRC (pat), 0), 0) = copy_rtx (dest);
13769           INSN_CODE (insn) = -1;
13770         }
13771     }
13772 }
13773
13774 /* Convert instructions to their cc-clobbering variant if possible, since
13775    that allows us to use smaller encodings.  */
13776
13777 static void
13778 thumb2_reorg (void)
13779 {
13780   basic_block bb;
13781   regset_head live;
13782
13783   INIT_REG_SET (&live);
13784
13785   /* We are freeing block_for_insn in the toplev to keep compatibility
13786      with old MDEP_REORGS that are not CFG based.  Recompute it now.  */
13787   compute_bb_for_insn ();
13788   df_analyze ();
13789
13790   FOR_EACH_BB (bb)
13791     {
13792       rtx insn;
13793
13794       COPY_REG_SET (&live, DF_LR_OUT (bb));
13795       df_simulate_initialize_backwards (bb, &live);
13796       FOR_BB_INSNS_REVERSE (bb, insn)
13797         {
13798           if (NONJUMP_INSN_P (insn)
13799               && !REGNO_REG_SET_P (&live, CC_REGNUM)
13800               && GET_CODE (PATTERN (insn)) == SET)
13801             {
13802               enum {SKIP, CONV, SWAP_CONV} action = SKIP;
13803               rtx pat = PATTERN (insn);
13804               rtx dst = XEXP (pat, 0);
13805               rtx src = XEXP (pat, 1);
13806               rtx op0 = NULL_RTX, op1 = NULL_RTX;
13807
13808               if (!OBJECT_P (src))
13809                   op0 = XEXP (src, 0);
13810
13811               if (BINARY_P (src))
13812                   op1 = XEXP (src, 1);
13813
13814               if (low_register_operand (dst, SImode))
13815                 {
13816                   switch (GET_CODE (src))
13817                     {
13818                     case PLUS:
13819                       /* Adding two registers and storing the result
13820                          in the first source is already a 16-bit
13821                          operation.  */
13822                       if (rtx_equal_p (dst, op0)
13823                           && register_operand (op1, SImode))
13824                         break;
13825
13826                       if (low_register_operand (op0, SImode))
13827                         {
13828                           /* ADDS <Rd>,<Rn>,<Rm>  */
13829                           if (low_register_operand (op1, SImode))
13830                             action = CONV;
13831                           /* ADDS <Rdn>,#<imm8>  */
13832                           /* SUBS <Rdn>,#<imm8>  */
13833                           else if (rtx_equal_p (dst, op0)
13834                                    && CONST_INT_P (op1)
13835                                    && IN_RANGE (INTVAL (op1), -255, 255))
13836                             action = CONV;
13837                           /* ADDS <Rd>,<Rn>,#<imm3>  */
13838                           /* SUBS <Rd>,<Rn>,#<imm3>  */
13839                           else if (CONST_INT_P (op1)
13840                                    && IN_RANGE (INTVAL (op1), -7, 7))
13841                             action = CONV;
13842                         }
13843                       break;
13844
13845                     case MINUS:
13846                       /* RSBS <Rd>,<Rn>,#0
13847                          Not handled here: see NEG below.  */
13848                       /* SUBS <Rd>,<Rn>,#<imm3>
13849                          SUBS <Rdn>,#<imm8>
13850                          Not handled here: see PLUS above.  */
13851                       /* SUBS <Rd>,<Rn>,<Rm>  */
13852                       if (low_register_operand (op0, SImode)
13853                           && low_register_operand (op1, SImode))
13854                             action = CONV;
13855                       break;
13856
13857                     case MULT:
13858                       /* MULS <Rdm>,<Rn>,<Rdm>
13859                          As an exception to the rule, this is only used
13860                          when optimizing for size since MULS is slow on all
13861                          known implementations.  We do not even want to use
13862                          MULS in cold code, if optimizing for speed, so we
13863                          test the global flag here.  */
13864                       if (!optimize_size)
13865                         break;
13866                       /* else fall through.  */
13867                     case AND:
13868                     case IOR:
13869                     case XOR:
13870                       /* ANDS <Rdn>,<Rm>  */
13871                       if (rtx_equal_p (dst, op0)
13872                           && low_register_operand (op1, SImode))
13873                         action = CONV;
13874                       else if (rtx_equal_p (dst, op1)
13875                                && low_register_operand (op0, SImode))
13876                         action = SWAP_CONV;
13877                       break;
13878
13879                     case ASHIFTRT:
13880                     case ASHIFT:
13881                     case LSHIFTRT:
13882                       /* ASRS <Rdn>,<Rm> */
13883                       /* LSRS <Rdn>,<Rm> */
13884                       /* LSLS <Rdn>,<Rm> */
13885                       if (rtx_equal_p (dst, op0)
13886                           && low_register_operand (op1, SImode))
13887                         action = CONV;
13888                       /* ASRS <Rd>,<Rm>,#<imm5> */
13889                       /* LSRS <Rd>,<Rm>,#<imm5> */
13890                       /* LSLS <Rd>,<Rm>,#<imm5> */
13891                       else if (low_register_operand (op0, SImode)
13892                                && CONST_INT_P (op1)
13893                                && IN_RANGE (INTVAL (op1), 0, 31))
13894                         action = CONV;
13895                       break;
13896
13897                     case ROTATERT:
13898                       /* RORS <Rdn>,<Rm>  */
13899                       if (rtx_equal_p (dst, op0)
13900                           && low_register_operand (op1, SImode))
13901                         action = CONV;
13902                       break;
13903
13904                     case NOT:
13905                     case NEG:
13906                       /* MVNS <Rd>,<Rm>  */
13907                       /* NEGS <Rd>,<Rm>  (a.k.a RSBS)  */
13908                       if (low_register_operand (op0, SImode))
13909                         action = CONV;
13910                       break;
13911
13912                     case CONST_INT:
13913                       /* MOVS <Rd>,#<imm8>  */
13914                       if (CONST_INT_P (src)
13915                           && IN_RANGE (INTVAL (src), 0, 255))
13916                         action = CONV;
13917                       break;
13918
13919                     case REG:
13920                       /* MOVS and MOV<c> with registers have different
13921                          encodings, so are not relevant here.  */
13922                       break;
13923
13924                     default:
13925                       break;
13926                     }
13927                 }
13928
13929               if (action != SKIP)
13930                 {
13931                   rtx ccreg = gen_rtx_REG (CCmode, CC_REGNUM);
13932                   rtx clobber = gen_rtx_CLOBBER (VOIDmode, ccreg);
13933                   rtvec vec;
13934
13935                   if (action == SWAP_CONV)
13936                     {
13937                       src = copy_rtx (src);
13938                       XEXP (src, 0) = op1;
13939                       XEXP (src, 1) = op0;
13940                       pat = gen_rtx_SET (VOIDmode, dst, src);
13941                       vec = gen_rtvec (2, pat, clobber);
13942                     }
13943                   else /* action == CONV */
13944                     vec = gen_rtvec (2, pat, clobber);
13945
13946                   PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
13947                   INSN_CODE (insn) = -1;
13948                 }
13949             }
13950
13951           if (NONDEBUG_INSN_P (insn))
13952             df_simulate_one_insn_backwards (bb, insn, &live);
13953         }
13954     }
13955
13956   CLEAR_REG_SET (&live);
13957 }
13958
13959 /* Gcc puts the pool in the wrong place for ARM, since we can only
13960    load addresses a limited distance around the pc.  We do some
13961    special munging to move the constant pool values to the correct
13962    point in the code.  */
13963 static void
13964 arm_reorg (void)
13965 {
13966   rtx insn;
13967   HOST_WIDE_INT address = 0;
13968   Mfix * fix;
13969
13970   if (TARGET_THUMB1)
13971     thumb1_reorg ();
13972   else if (TARGET_THUMB2)
13973     thumb2_reorg ();
13974
13975   /* Ensure all insns that must be split have been split at this point.
13976      Otherwise, the pool placement code below may compute incorrect
13977      insn lengths.  Note that when optimizing, all insns have already
13978      been split at this point.  */
13979   if (!optimize)
13980     split_all_insns_noflow ();
13981
13982   minipool_fix_head = minipool_fix_tail = NULL;
13983
13984   /* The first insn must always be a note, or the code below won't
13985      scan it properly.  */
13986   insn = get_insns ();
13987   gcc_assert (NOTE_P (insn));
13988   minipool_pad = 0;
13989
13990   /* Scan all the insns and record the operands that will need fixing.  */
13991   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
13992     {
13993       if (BARRIER_P (insn))
13994         push_minipool_barrier (insn, address);
13995       else if (INSN_P (insn))
13996         {
13997           rtx table;
13998
13999           note_invalid_constants (insn, address, true);
14000           address += get_attr_length (insn);
14001
14002           /* If the insn is a vector jump, add the size of the table
14003              and skip the table.  */
14004           if ((table = is_jump_table (insn)) != NULL)
14005             {
14006               address += get_jump_table_size (table);
14007               insn = table;
14008             }
14009         }
14010       else if (LABEL_P (insn))
14011         /* Add the worst-case padding due to alignment.  We don't add
14012            the _current_ padding because the minipool insertions
14013            themselves might change it.  */
14014         address += get_label_padding (insn);
14015     }
14016
14017   fix = minipool_fix_head;
14018
14019   /* Now scan the fixups and perform the required changes.  */
14020   while (fix)
14021     {
14022       Mfix * ftmp;
14023       Mfix * fdel;
14024       Mfix *  last_added_fix;
14025       Mfix * last_barrier = NULL;
14026       Mfix * this_fix;
14027
14028       /* Skip any further barriers before the next fix.  */
14029       while (fix && BARRIER_P (fix->insn))
14030         fix = fix->next;
14031
14032       /* No more fixes.  */
14033       if (fix == NULL)
14034         break;
14035
14036       last_added_fix = NULL;
14037
14038       for (ftmp = fix; ftmp; ftmp = ftmp->next)
14039         {
14040           if (BARRIER_P (ftmp->insn))
14041             {
14042               if (ftmp->address >= minipool_vector_head->max_address)
14043                 break;
14044
14045               last_barrier = ftmp;
14046             }
14047           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
14048             break;
14049
14050           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
14051         }
14052
14053       /* If we found a barrier, drop back to that; any fixes that we
14054          could have reached but come after the barrier will now go in
14055          the next mini-pool.  */
14056       if (last_barrier != NULL)
14057         {
14058           /* Reduce the refcount for those fixes that won't go into this
14059              pool after all.  */
14060           for (fdel = last_barrier->next;
14061                fdel && fdel != ftmp;
14062                fdel = fdel->next)
14063             {
14064               fdel->minipool->refcount--;
14065               fdel->minipool = NULL;
14066             }
14067
14068           ftmp = last_barrier;
14069         }
14070       else
14071         {
14072           /* ftmp is first fix that we can't fit into this pool and
14073              there no natural barriers that we could use.  Insert a
14074              new barrier in the code somewhere between the previous
14075              fix and this one, and arrange to jump around it.  */
14076           HOST_WIDE_INT max_address;
14077
14078           /* The last item on the list of fixes must be a barrier, so
14079              we can never run off the end of the list of fixes without
14080              last_barrier being set.  */
14081           gcc_assert (ftmp);
14082
14083           max_address = minipool_vector_head->max_address;
14084           /* Check that there isn't another fix that is in range that
14085              we couldn't fit into this pool because the pool was
14086              already too large: we need to put the pool before such an
14087              instruction.  The pool itself may come just after the
14088              fix because create_fix_barrier also allows space for a
14089              jump instruction.  */
14090           if (ftmp->address < max_address)
14091             max_address = ftmp->address + 1;
14092
14093           last_barrier = create_fix_barrier (last_added_fix, max_address);
14094         }
14095
14096       assign_minipool_offsets (last_barrier);
14097
14098       while (ftmp)
14099         {
14100           if (!BARRIER_P (ftmp->insn)
14101               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
14102                   == NULL))
14103             break;
14104
14105           ftmp = ftmp->next;
14106         }
14107
14108       /* Scan over the fixes we have identified for this pool, fixing them
14109          up and adding the constants to the pool itself.  */
14110       for (this_fix = fix; this_fix && ftmp != this_fix;
14111            this_fix = this_fix->next)
14112         if (!BARRIER_P (this_fix->insn))
14113           {
14114             rtx addr
14115               = plus_constant (Pmode,
14116                                gen_rtx_LABEL_REF (VOIDmode,
14117                                                   minipool_vector_label),
14118                                this_fix->minipool->offset);
14119             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
14120           }
14121
14122       dump_minipool (last_barrier->insn);
14123       fix = ftmp;
14124     }
14125
14126   /* From now on we must synthesize any constants that we can't handle
14127      directly.  This can happen if the RTL gets split during final
14128      instruction generation.  */
14129   after_arm_reorg = 1;
14130
14131   /* Free the minipool memory.  */
14132   obstack_free (&minipool_obstack, minipool_startobj);
14133 }
14134 \f
14135 /* Routines to output assembly language.  */
14136
14137 /* If the rtx is the correct value then return the string of the number.
14138    In this way we can ensure that valid double constants are generated even
14139    when cross compiling.  */
14140 const char *
14141 fp_immediate_constant (rtx x)
14142 {
14143   REAL_VALUE_TYPE r;
14144
14145   if (!fp_consts_inited)
14146     init_fp_table ();
14147
14148   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
14149
14150   gcc_assert (REAL_VALUES_EQUAL (r, value_fp0));
14151   return "0";
14152 }
14153
14154 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
14155 static const char *
14156 fp_const_from_val (REAL_VALUE_TYPE *r)
14157 {
14158   if (!fp_consts_inited)
14159     init_fp_table ();
14160
14161   gcc_assert (REAL_VALUES_EQUAL (*r, value_fp0));
14162   return "0";
14163 }
14164
14165 /* OPERANDS[0] is the entire list of insns that constitute pop,
14166    OPERANDS[1] is the base register, RETURN_PC is true iff return insn
14167    is in the list, UPDATE is true iff the list contains explicit
14168    update of base register.  */
14169 void
14170 arm_output_multireg_pop (rtx *operands, bool return_pc, rtx cond, bool reverse,
14171                          bool update)
14172 {
14173   int i;
14174   char pattern[100];
14175   int offset;
14176   const char *conditional;
14177   int num_saves = XVECLEN (operands[0], 0);
14178   unsigned int regno;
14179   unsigned int regno_base = REGNO (operands[1]);
14180
14181   offset = 0;
14182   offset += update ? 1 : 0;
14183   offset += return_pc ? 1 : 0;
14184
14185   /* Is the base register in the list?  */
14186   for (i = offset; i < num_saves; i++)
14187     {
14188       regno = REGNO (XEXP (XVECEXP (operands[0], 0, i), 0));
14189       /* If SP is in the list, then the base register must be SP.  */
14190       gcc_assert ((regno != SP_REGNUM) || (regno_base == SP_REGNUM));
14191       /* If base register is in the list, there must be no explicit update.  */
14192       if (regno == regno_base)
14193         gcc_assert (!update);
14194     }
14195
14196   conditional = reverse ? "%?%D0" : "%?%d0";
14197   if ((regno_base == SP_REGNUM) && TARGET_UNIFIED_ASM)
14198     {
14199       /* Output pop (not stmfd) because it has a shorter encoding.  */
14200       gcc_assert (update);
14201       sprintf (pattern, "pop%s\t{", conditional);
14202     }
14203   else
14204     {
14205       /* Output ldmfd when the base register is SP, otherwise output ldmia.
14206          It's just a convention, their semantics are identical.  */
14207       if (regno_base == SP_REGNUM)
14208         sprintf (pattern, "ldm%sfd\t", conditional);
14209       else if (TARGET_UNIFIED_ASM)
14210         sprintf (pattern, "ldmia%s\t", conditional);
14211       else
14212         sprintf (pattern, "ldm%sia\t", conditional);
14213
14214       strcat (pattern, reg_names[regno_base]);
14215       if (update)
14216         strcat (pattern, "!, {");
14217       else
14218         strcat (pattern, ", {");
14219     }
14220
14221   /* Output the first destination register.  */
14222   strcat (pattern,
14223           reg_names[REGNO (XEXP (XVECEXP (operands[0], 0, offset), 0))]);
14224
14225   /* Output the rest of the destination registers.  */
14226   for (i = offset + 1; i < num_saves; i++)
14227     {
14228       strcat (pattern, ", ");
14229       strcat (pattern,
14230               reg_names[REGNO (XEXP (XVECEXP (operands[0], 0, i), 0))]);
14231     }
14232
14233   strcat (pattern, "}");
14234
14235   if (IS_INTERRUPT (arm_current_func_type ()) && return_pc)
14236     strcat (pattern, "^");
14237
14238   output_asm_insn (pattern, &cond);
14239 }
14240
14241
14242 /* Output the assembly for a store multiple.  */
14243
14244 const char *
14245 vfp_output_fstmd (rtx * operands)
14246 {
14247   char pattern[100];
14248   int p;
14249   int base;
14250   int i;
14251
14252   strcpy (pattern, "fstmfdd%?\t%m0!, {%P1");
14253   p = strlen (pattern);
14254
14255   gcc_assert (REG_P (operands[1]));
14256
14257   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
14258   for (i = 1; i < XVECLEN (operands[2], 0); i++)
14259     {
14260       p += sprintf (&pattern[p], ", d%d", base + i);
14261     }
14262   strcpy (&pattern[p], "}");
14263
14264   output_asm_insn (pattern, operands);
14265   return "";
14266 }
14267
14268
14269 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
14270    number of bytes pushed.  */
14271
14272 static int
14273 vfp_emit_fstmd (int base_reg, int count)
14274 {
14275   rtx par;
14276   rtx dwarf;
14277   rtx tmp, reg;
14278   int i;
14279
14280   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
14281      register pairs are stored by a store multiple insn.  We avoid this
14282      by pushing an extra pair.  */
14283   if (count == 2 && !arm_arch6)
14284     {
14285       if (base_reg == LAST_VFP_REGNUM - 3)
14286         base_reg -= 2;
14287       count++;
14288     }
14289
14290   /* FSTMD may not store more than 16 doubleword registers at once.  Split
14291      larger stores into multiple parts (up to a maximum of two, in
14292      practice).  */
14293   if (count > 16)
14294     {
14295       int saved;
14296       /* NOTE: base_reg is an internal register number, so each D register
14297          counts as 2.  */
14298       saved = vfp_emit_fstmd (base_reg + 32, count - 16);
14299       saved += vfp_emit_fstmd (base_reg, 16);
14300       return saved;
14301     }
14302
14303   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
14304   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
14305
14306   reg = gen_rtx_REG (DFmode, base_reg);
14307   base_reg += 2;
14308
14309   XVECEXP (par, 0, 0)
14310     = gen_rtx_SET (VOIDmode,
14311                    gen_frame_mem
14312                    (BLKmode,
14313                     gen_rtx_PRE_MODIFY (Pmode,
14314                                         stack_pointer_rtx,
14315                                         plus_constant
14316                                         (Pmode, stack_pointer_rtx,
14317                                          - (count * 8)))
14318                     ),
14319                    gen_rtx_UNSPEC (BLKmode,
14320                                    gen_rtvec (1, reg),
14321                                    UNSPEC_PUSH_MULT));
14322
14323   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
14324                      plus_constant (Pmode, stack_pointer_rtx, -(count * 8)));
14325   RTX_FRAME_RELATED_P (tmp) = 1;
14326   XVECEXP (dwarf, 0, 0) = tmp;
14327
14328   tmp = gen_rtx_SET (VOIDmode,
14329                      gen_frame_mem (DFmode, stack_pointer_rtx),
14330                      reg);
14331   RTX_FRAME_RELATED_P (tmp) = 1;
14332   XVECEXP (dwarf, 0, 1) = tmp;
14333
14334   for (i = 1; i < count; i++)
14335     {
14336       reg = gen_rtx_REG (DFmode, base_reg);
14337       base_reg += 2;
14338       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
14339
14340       tmp = gen_rtx_SET (VOIDmode,
14341                          gen_frame_mem (DFmode,
14342                                         plus_constant (Pmode,
14343                                                        stack_pointer_rtx,
14344                                                        i * 8)),
14345                          reg);
14346       RTX_FRAME_RELATED_P (tmp) = 1;
14347       XVECEXP (dwarf, 0, i + 1) = tmp;
14348     }
14349
14350   par = emit_insn (par);
14351   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
14352   RTX_FRAME_RELATED_P (par) = 1;
14353
14354   return count * 8;
14355 }
14356
14357 /* Emit a call instruction with pattern PAT.  ADDR is the address of
14358    the call target.  */
14359
14360 void
14361 arm_emit_call_insn (rtx pat, rtx addr)
14362 {
14363   rtx insn;
14364
14365   insn = emit_call_insn (pat);
14366
14367   /* The PIC register is live on entry to VxWorks PIC PLT entries.
14368      If the call might use such an entry, add a use of the PIC register
14369      to the instruction's CALL_INSN_FUNCTION_USAGE.  */
14370   if (TARGET_VXWORKS_RTP
14371       && flag_pic
14372       && GET_CODE (addr) == SYMBOL_REF
14373       && (SYMBOL_REF_DECL (addr)
14374           ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
14375           : !SYMBOL_REF_LOCAL_P (addr)))
14376     {
14377       require_pic_register ();
14378       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
14379     }
14380 }
14381
14382 /* Output a 'call' insn.  */
14383 const char *
14384 output_call (rtx *operands)
14385 {
14386   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
14387
14388   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
14389   if (REGNO (operands[0]) == LR_REGNUM)
14390     {
14391       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
14392       output_asm_insn ("mov%?\t%0, %|lr", operands);
14393     }
14394
14395   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
14396
14397   if (TARGET_INTERWORK || arm_arch4t)
14398     output_asm_insn ("bx%?\t%0", operands);
14399   else
14400     output_asm_insn ("mov%?\t%|pc, %0", operands);
14401
14402   return "";
14403 }
14404
14405 /* Output a 'call' insn that is a reference in memory. This is
14406    disabled for ARMv5 and we prefer a blx instead because otherwise
14407    there's a significant performance overhead.  */
14408 const char *
14409 output_call_mem (rtx *operands)
14410 {
14411   gcc_assert (!arm_arch5);
14412   if (TARGET_INTERWORK)
14413     {
14414       output_asm_insn ("ldr%?\t%|ip, %0", operands);
14415       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
14416       output_asm_insn ("bx%?\t%|ip", operands);
14417     }
14418   else if (regno_use_in (LR_REGNUM, operands[0]))
14419     {
14420       /* LR is used in the memory address.  We load the address in the
14421          first instruction.  It's safe to use IP as the target of the
14422          load since the call will kill it anyway.  */
14423       output_asm_insn ("ldr%?\t%|ip, %0", operands);
14424       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
14425       if (arm_arch4t)
14426         output_asm_insn ("bx%?\t%|ip", operands);
14427       else
14428         output_asm_insn ("mov%?\t%|pc, %|ip", operands);
14429     }
14430   else
14431     {
14432       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
14433       output_asm_insn ("ldr%?\t%|pc, %0", operands);
14434     }
14435
14436   return "";
14437 }
14438
14439
14440 /* Output a move from arm registers to arm registers of a long double
14441    OPERANDS[0] is the destination.
14442    OPERANDS[1] is the source.  */
14443 const char *
14444 output_mov_long_double_arm_from_arm (rtx *operands)
14445 {
14446   /* We have to be careful here because the two might overlap.  */
14447   int dest_start = REGNO (operands[0]);
14448   int src_start = REGNO (operands[1]);
14449   rtx ops[2];
14450   int i;
14451
14452   if (dest_start < src_start)
14453     {
14454       for (i = 0; i < 3; i++)
14455         {
14456           ops[0] = gen_rtx_REG (SImode, dest_start + i);
14457           ops[1] = gen_rtx_REG (SImode, src_start + i);
14458           output_asm_insn ("mov%?\t%0, %1", ops);
14459         }
14460     }
14461   else
14462     {
14463       for (i = 2; i >= 0; i--)
14464         {
14465           ops[0] = gen_rtx_REG (SImode, dest_start + i);
14466           ops[1] = gen_rtx_REG (SImode, src_start + i);
14467           output_asm_insn ("mov%?\t%0, %1", ops);
14468         }
14469     }
14470
14471   return "";
14472 }
14473
14474 void
14475 arm_emit_movpair (rtx dest, rtx src)
14476  {
14477   /* If the src is an immediate, simplify it.  */
14478   if (CONST_INT_P (src))
14479     {
14480       HOST_WIDE_INT val = INTVAL (src);
14481       emit_set_insn (dest, GEN_INT (val & 0x0000ffff));
14482       if ((val >> 16) & 0x0000ffff)
14483         emit_set_insn (gen_rtx_ZERO_EXTRACT (SImode, dest, GEN_INT (16),
14484                                              GEN_INT (16)),
14485                        GEN_INT ((val >> 16) & 0x0000ffff));
14486       return;
14487     }
14488    emit_set_insn (dest, gen_rtx_HIGH (SImode, src));
14489    emit_set_insn (dest, gen_rtx_LO_SUM (SImode, dest, src));
14490  }
14491
14492 /* Output a move between double words.  It must be REG<-MEM
14493    or MEM<-REG.  */
14494 const char *
14495 output_move_double (rtx *operands, bool emit, int *count)
14496 {
14497   enum rtx_code code0 = GET_CODE (operands[0]);
14498   enum rtx_code code1 = GET_CODE (operands[1]);
14499   rtx otherops[3];
14500   if (count)
14501     *count = 1;
14502
14503   /* The only case when this might happen is when
14504      you are looking at the length of a DImode instruction
14505      that has an invalid constant in it.  */
14506   if (code0 == REG && code1 != MEM)
14507     {
14508       gcc_assert (!emit);
14509       *count = 2;
14510       return "";
14511     }
14512
14513   if (code0 == REG)
14514     {
14515       unsigned int reg0 = REGNO (operands[0]);
14516
14517       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
14518
14519       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
14520
14521       switch (GET_CODE (XEXP (operands[1], 0)))
14522         {
14523         case REG:
14524
14525           if (emit)
14526             {
14527               if (TARGET_LDRD
14528                   && !(fix_cm3_ldrd && reg0 == REGNO(XEXP (operands[1], 0))))
14529                 output_asm_insn ("ldr%(d%)\t%0, [%m1]", operands);
14530               else
14531                 output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
14532             }
14533           break;
14534
14535         case PRE_INC:
14536           gcc_assert (TARGET_LDRD);
14537           if (emit)
14538             output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
14539           break;
14540
14541         case PRE_DEC:
14542           if (emit)
14543             {
14544               if (TARGET_LDRD)
14545                 output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
14546               else
14547                 output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
14548             }
14549           break;
14550
14551         case POST_INC:
14552           if (emit)
14553             {
14554               if (TARGET_LDRD)
14555                 output_asm_insn ("ldr%(d%)\t%0, [%m1], #8", operands);
14556               else
14557                 output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
14558             }
14559           break;
14560
14561         case POST_DEC:
14562           gcc_assert (TARGET_LDRD);
14563           if (emit)
14564             output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
14565           break;
14566
14567         case PRE_MODIFY:
14568         case POST_MODIFY:
14569           /* Autoicrement addressing modes should never have overlapping
14570              base and destination registers, and overlapping index registers
14571              are already prohibited, so this doesn't need to worry about
14572              fix_cm3_ldrd.  */
14573           otherops[0] = operands[0];
14574           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
14575           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
14576
14577           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
14578             {
14579               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
14580                 {
14581                   /* Registers overlap so split out the increment.  */
14582                   if (emit)
14583                     {
14584                       output_asm_insn ("add%?\t%1, %1, %2", otherops);
14585                       output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
14586                     }
14587                   if (count)
14588                     *count = 2;
14589                 }
14590               else
14591                 {
14592                   /* Use a single insn if we can.
14593                      FIXME: IWMMXT allows offsets larger than ldrd can
14594                      handle, fix these up with a pair of ldr.  */
14595                   if (TARGET_THUMB2
14596                       || !CONST_INT_P (otherops[2])
14597                       || (INTVAL (otherops[2]) > -256
14598                           && INTVAL (otherops[2]) < 256))
14599                     {
14600                       if (emit)
14601                         output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
14602                     }
14603                   else
14604                     {
14605                       if (emit)
14606                         {
14607                           output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
14608                           output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
14609                         }
14610                       if (count)
14611                         *count = 2;
14612
14613                     }
14614                 }
14615             }
14616           else
14617             {
14618               /* Use a single insn if we can.
14619                  FIXME: IWMMXT allows offsets larger than ldrd can handle,
14620                  fix these up with a pair of ldr.  */
14621               if (TARGET_THUMB2
14622                   || !CONST_INT_P (otherops[2])
14623                   || (INTVAL (otherops[2]) > -256
14624                       && INTVAL (otherops[2]) < 256))
14625                 {
14626                   if (emit)
14627                     output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
14628                 }
14629               else
14630                 {
14631                   if (emit)
14632                     {
14633                       output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
14634                       output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
14635                     }
14636                   if (count)
14637                     *count = 2;
14638                 }
14639             }
14640           break;
14641
14642         case LABEL_REF:
14643         case CONST:
14644           /* We might be able to use ldrd %0, %1 here.  However the range is
14645              different to ldr/adr, and it is broken on some ARMv7-M
14646              implementations.  */
14647           /* Use the second register of the pair to avoid problematic
14648              overlap.  */
14649           otherops[1] = operands[1];
14650           if (emit)
14651             output_asm_insn ("adr%?\t%0, %1", otherops);
14652           operands[1] = otherops[0];
14653           if (emit)
14654             {
14655               if (TARGET_LDRD)
14656                 output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
14657               else
14658                 output_asm_insn ("ldm%(ia%)\t%1, %M0", operands);
14659             }
14660
14661           if (count)
14662             *count = 2;
14663           break;
14664
14665           /* ??? This needs checking for thumb2.  */
14666         default:
14667           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
14668                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
14669             {
14670               otherops[0] = operands[0];
14671               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
14672               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
14673
14674               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
14675                 {
14676                   if (CONST_INT_P (otherops[2]) && !TARGET_LDRD)
14677                     {
14678                       switch ((int) INTVAL (otherops[2]))
14679                         {
14680                         case -8:
14681                           if (emit)
14682                             output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
14683                           return "";
14684                         case -4:
14685                           if (TARGET_THUMB2)
14686                             break;
14687                           if (emit)
14688                             output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
14689                           return "";
14690                         case 4:
14691                           if (TARGET_THUMB2)
14692                             break;
14693                           if (emit)
14694                             output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
14695                           return "";
14696                         }
14697                     }
14698                   otherops[0] = gen_rtx_REG(SImode, REGNO(operands[0]) + 1);
14699                   operands[1] = otherops[0];
14700                   if (TARGET_LDRD
14701                       && (REG_P (otherops[2])
14702                           || TARGET_THUMB2
14703                           || (CONST_INT_P (otherops[2])
14704                               && INTVAL (otherops[2]) > -256
14705                               && INTVAL (otherops[2]) < 256)))
14706                     {
14707                       if (reg_overlap_mentioned_p (operands[0],
14708                                                    otherops[2]))
14709                         {
14710                           rtx tmp;
14711                           /* Swap base and index registers over to
14712                              avoid a conflict.  */
14713                           tmp = otherops[1];
14714                           otherops[1] = otherops[2];
14715                           otherops[2] = tmp;
14716                         }
14717                       /* If both registers conflict, it will usually
14718                          have been fixed by a splitter.  */
14719                       if (reg_overlap_mentioned_p (operands[0], otherops[2])
14720                           || (fix_cm3_ldrd && reg0 == REGNO (otherops[1])))
14721                         {
14722                           if (emit)
14723                             {
14724                               output_asm_insn ("add%?\t%0, %1, %2", otherops);
14725                               output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
14726                             }
14727                           if (count)
14728                             *count = 2;
14729                         }
14730                       else
14731                         {
14732                           otherops[0] = operands[0];
14733                           if (emit)
14734                             output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
14735                         }
14736                       return "";
14737                     }
14738
14739                   if (CONST_INT_P (otherops[2]))
14740                     {
14741                       if (emit)
14742                         {
14743                           if (!(const_ok_for_arm (INTVAL (otherops[2]))))
14744                             output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
14745                           else
14746                             output_asm_insn ("add%?\t%0, %1, %2", otherops);
14747                         }
14748                     }
14749                   else
14750                     {
14751                       if (emit)
14752                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
14753                     }
14754                 }
14755               else
14756                 {
14757                   if (emit)
14758                     output_asm_insn ("sub%?\t%0, %1, %2", otherops);
14759                 }
14760
14761               if (count)
14762                 *count = 2;
14763
14764               if (TARGET_LDRD)
14765                 return "ldr%(d%)\t%0, [%1]";
14766
14767               return "ldm%(ia%)\t%1, %M0";
14768             }
14769           else
14770             {
14771               otherops[1] = adjust_address (operands[1], SImode, 4);
14772               /* Take care of overlapping base/data reg.  */
14773               if (reg_mentioned_p (operands[0], operands[1]))
14774                 {
14775                   if (emit)
14776                     {
14777                       output_asm_insn ("ldr%?\t%0, %1", otherops);
14778                       output_asm_insn ("ldr%?\t%0, %1", operands);
14779                     }
14780                   if (count)
14781                     *count = 2;
14782
14783                 }
14784               else
14785                 {
14786                   if (emit)
14787                     {
14788                       output_asm_insn ("ldr%?\t%0, %1", operands);
14789                       output_asm_insn ("ldr%?\t%0, %1", otherops);
14790                     }
14791                   if (count)
14792                     *count = 2;
14793                 }
14794             }
14795         }
14796     }
14797   else
14798     {
14799       /* Constraints should ensure this.  */
14800       gcc_assert (code0 == MEM && code1 == REG);
14801       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
14802
14803       switch (GET_CODE (XEXP (operands[0], 0)))
14804         {
14805         case REG:
14806           if (emit)
14807             {
14808               if (TARGET_LDRD)
14809                 output_asm_insn ("str%(d%)\t%1, [%m0]", operands);
14810               else
14811                 output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
14812             }
14813           break;
14814
14815         case PRE_INC:
14816           gcc_assert (TARGET_LDRD);
14817           if (emit)
14818             output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
14819           break;
14820
14821         case PRE_DEC:
14822           if (emit)
14823             {
14824               if (TARGET_LDRD)
14825                 output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
14826               else
14827                 output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
14828             }
14829           break;
14830
14831         case POST_INC:
14832           if (emit)
14833             {
14834               if (TARGET_LDRD)
14835                 output_asm_insn ("str%(d%)\t%1, [%m0], #8", operands);
14836               else
14837                 output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
14838             }
14839           break;
14840
14841         case POST_DEC:
14842           gcc_assert (TARGET_LDRD);
14843           if (emit)
14844             output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
14845           break;
14846
14847         case PRE_MODIFY:
14848         case POST_MODIFY:
14849           otherops[0] = operands[1];
14850           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
14851           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
14852
14853           /* IWMMXT allows offsets larger than ldrd can handle,
14854              fix these up with a pair of ldr.  */
14855           if (!TARGET_THUMB2
14856               && CONST_INT_P (otherops[2])
14857               && (INTVAL(otherops[2]) <= -256
14858                   || INTVAL(otherops[2]) >= 256))
14859             {
14860               if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
14861                 {
14862                   if (emit)
14863                     {
14864                       output_asm_insn ("str%?\t%0, [%1, %2]!", otherops);
14865                       output_asm_insn ("str%?\t%H0, [%1, #4]", otherops);
14866                     }
14867                   if (count)
14868                     *count = 2;
14869                 }
14870               else
14871                 {
14872                   if (emit)
14873                     {
14874                       output_asm_insn ("str%?\t%H0, [%1, #4]", otherops);
14875                       output_asm_insn ("str%?\t%0, [%1], %2", otherops);
14876                     }
14877                   if (count)
14878                     *count = 2;
14879                 }
14880             }
14881           else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
14882             {
14883               if (emit)
14884                 output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
14885             }
14886           else
14887             {
14888               if (emit)
14889                 output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
14890             }
14891           break;
14892
14893         case PLUS:
14894           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
14895           if (CONST_INT_P (otherops[2]) && !TARGET_LDRD)
14896             {
14897               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
14898                 {
14899                 case -8:
14900                   if (emit)
14901                     output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
14902                   return "";
14903
14904                 case -4:
14905                   if (TARGET_THUMB2)
14906                     break;
14907                   if (emit)
14908                     output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
14909                   return "";
14910
14911                 case 4:
14912                   if (TARGET_THUMB2)
14913                     break;
14914                   if (emit)
14915                     output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
14916                   return "";
14917                 }
14918             }
14919           if (TARGET_LDRD
14920               && (REG_P (otherops[2])
14921                   || TARGET_THUMB2
14922                   || (CONST_INT_P (otherops[2])
14923                       && INTVAL (otherops[2]) > -256
14924                       && INTVAL (otherops[2]) < 256)))
14925             {
14926               otherops[0] = operands[1];
14927               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
14928               if (emit)
14929                 output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
14930               return "";
14931             }
14932           /* Fall through */
14933
14934         default:
14935           otherops[0] = adjust_address (operands[0], SImode, 4);
14936           otherops[1] = operands[1];
14937           if (emit)
14938             {
14939               output_asm_insn ("str%?\t%1, %0", operands);
14940               output_asm_insn ("str%?\t%H1, %0", otherops);
14941             }
14942           if (count)
14943             *count = 2;
14944         }
14945     }
14946
14947   return "";
14948 }
14949
14950 /* Output a move, load or store for quad-word vectors in ARM registers.  Only
14951    handles MEMs accepted by neon_vector_mem_operand with TYPE=1.  */
14952
14953 const char *
14954 output_move_quad (rtx *operands)
14955 {
14956   if (REG_P (operands[0]))
14957     {
14958       /* Load, or reg->reg move.  */
14959
14960       if (MEM_P (operands[1]))
14961         {
14962           switch (GET_CODE (XEXP (operands[1], 0)))
14963             {
14964             case REG:
14965               output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
14966               break;
14967
14968             case LABEL_REF:
14969             case CONST:
14970               output_asm_insn ("adr%?\t%0, %1", operands);
14971               output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
14972               break;
14973
14974             default:
14975               gcc_unreachable ();
14976             }
14977         }
14978       else
14979         {
14980           rtx ops[2];
14981           int dest, src, i;
14982
14983           gcc_assert (REG_P (operands[1]));
14984
14985           dest = REGNO (operands[0]);
14986           src = REGNO (operands[1]);
14987
14988           /* This seems pretty dumb, but hopefully GCC won't try to do it
14989              very often.  */
14990           if (dest < src)
14991             for (i = 0; i < 4; i++)
14992               {
14993                 ops[0] = gen_rtx_REG (SImode, dest + i);
14994                 ops[1] = gen_rtx_REG (SImode, src + i);
14995                 output_asm_insn ("mov%?\t%0, %1", ops);
14996               }
14997           else
14998             for (i = 3; i >= 0; i--)
14999               {
15000                 ops[0] = gen_rtx_REG (SImode, dest + i);
15001                 ops[1] = gen_rtx_REG (SImode, src + i);
15002                 output_asm_insn ("mov%?\t%0, %1", ops);
15003               }
15004         }
15005     }
15006   else
15007     {
15008       gcc_assert (MEM_P (operands[0]));
15009       gcc_assert (REG_P (operands[1]));
15010       gcc_assert (!reg_overlap_mentioned_p (operands[1], operands[0]));
15011
15012       switch (GET_CODE (XEXP (operands[0], 0)))
15013         {
15014         case REG:
15015           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
15016           break;
15017
15018         default:
15019           gcc_unreachable ();
15020         }
15021     }
15022
15023   return "";
15024 }
15025
15026 /* Output a VFP load or store instruction.  */
15027
15028 const char *
15029 output_move_vfp (rtx *operands)
15030 {
15031   rtx reg, mem, addr, ops[2];
15032   int load = REG_P (operands[0]);
15033   int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
15034   int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
15035   const char *templ;
15036   char buff[50];
15037   enum machine_mode mode;
15038
15039   reg = operands[!load];
15040   mem = operands[load];
15041
15042   mode = GET_MODE (reg);
15043
15044   gcc_assert (REG_P (reg));
15045   gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
15046   gcc_assert (mode == SFmode
15047               || mode == DFmode
15048               || mode == SImode
15049               || mode == DImode
15050               || (TARGET_NEON && VALID_NEON_DREG_MODE (mode)));
15051   gcc_assert (MEM_P (mem));
15052
15053   addr = XEXP (mem, 0);
15054
15055   switch (GET_CODE (addr))
15056     {
15057     case PRE_DEC:
15058       templ = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
15059       ops[0] = XEXP (addr, 0);
15060       ops[1] = reg;
15061       break;
15062
15063     case POST_INC:
15064       templ = "f%smia%c%%?\t%%0!, {%%%s1}%s";
15065       ops[0] = XEXP (addr, 0);
15066       ops[1] = reg;
15067       break;
15068
15069     default:
15070       templ = "f%s%c%%?\t%%%s0, %%1%s";
15071       ops[0] = reg;
15072       ops[1] = mem;
15073       break;
15074     }
15075
15076   sprintf (buff, templ,
15077            load ? "ld" : "st",
15078            dp ? 'd' : 's',
15079            dp ? "P" : "",
15080            integer_p ? "\t%@ int" : "");
15081   output_asm_insn (buff, ops);
15082
15083   return "";
15084 }
15085
15086 /* Output a Neon double-word or quad-word load or store, or a load
15087    or store for larger structure modes.
15088
15089    WARNING: The ordering of elements is weird in big-endian mode,
15090    because the EABI requires that vectors stored in memory appear
15091    as though they were stored by a VSTM, as required by the EABI.
15092    GCC RTL defines element ordering based on in-memory order.
15093    This can be different from the architectural ordering of elements
15094    within a NEON register. The intrinsics defined in arm_neon.h use the
15095    NEON register element ordering, not the GCC RTL element ordering.
15096
15097    For example, the in-memory ordering of a big-endian a quadword
15098    vector with 16-bit elements when stored from register pair {d0,d1}
15099    will be (lowest address first, d0[N] is NEON register element N):
15100
15101      [d0[3], d0[2], d0[1], d0[0], d1[7], d1[6], d1[5], d1[4]]
15102
15103    When necessary, quadword registers (dN, dN+1) are moved to ARM
15104    registers from rN in the order:
15105
15106      dN -> (rN+1, rN), dN+1 -> (rN+3, rN+2)
15107
15108    So that STM/LDM can be used on vectors in ARM registers, and the
15109    same memory layout will result as if VSTM/VLDM were used.
15110
15111    Instead of VSTM/VLDM we prefer to use VST1.64/VLD1.64 where
15112    possible, which allows use of appropriate alignment tags.
15113    Note that the choice of "64" is independent of the actual vector
15114    element size; this size simply ensures that the behavior is
15115    equivalent to VSTM/VLDM in both little-endian and big-endian mode.
15116
15117    Due to limitations of those instructions, use of VST1.64/VLD1.64
15118    is not possible if:
15119     - the address contains PRE_DEC, or
15120     - the mode refers to more than 4 double-word registers
15121
15122    In those cases, it would be possible to replace VSTM/VLDM by a
15123    sequence of instructions; this is not currently implemented since
15124    this is not certain to actually improve performance.  */
15125
15126 const char *
15127 output_move_neon (rtx *operands)
15128 {
15129   rtx reg, mem, addr, ops[2];
15130   int regno, nregs, load = REG_P (operands[0]);
15131   const char *templ;
15132   char buff[50];
15133   enum machine_mode mode;
15134
15135   reg = operands[!load];
15136   mem = operands[load];
15137
15138   mode = GET_MODE (reg);
15139
15140   gcc_assert (REG_P (reg));
15141   regno = REGNO (reg);
15142   nregs = HARD_REGNO_NREGS (regno, mode) / 2;
15143   gcc_assert (VFP_REGNO_OK_FOR_DOUBLE (regno)
15144               || NEON_REGNO_OK_FOR_QUAD (regno));
15145   gcc_assert (VALID_NEON_DREG_MODE (mode)
15146               || VALID_NEON_QREG_MODE (mode)
15147               || VALID_NEON_STRUCT_MODE (mode));
15148   gcc_assert (MEM_P (mem));
15149
15150   addr = XEXP (mem, 0);
15151
15152   /* Strip off const from addresses like (const (plus (...))).  */
15153   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
15154     addr = XEXP (addr, 0);
15155
15156   switch (GET_CODE (addr))
15157     {
15158     case POST_INC:
15159       /* We have to use vldm / vstm for too-large modes.  */
15160       if (nregs > 4)
15161         {
15162           templ = "v%smia%%?\t%%0!, %%h1";
15163           ops[0] = XEXP (addr, 0);
15164         }
15165       else
15166         {
15167           templ = "v%s1.64\t%%h1, %%A0";
15168           ops[0] = mem;
15169         }
15170       ops[1] = reg;
15171       break;
15172
15173     case PRE_DEC:
15174       /* We have to use vldm / vstm in this case, since there is no
15175          pre-decrement form of the vld1 / vst1 instructions.  */
15176       templ = "v%smdb%%?\t%%0!, %%h1";
15177       ops[0] = XEXP (addr, 0);
15178       ops[1] = reg;
15179       break;
15180
15181     case POST_MODIFY:
15182       /* FIXME: Not currently enabled in neon_vector_mem_operand.  */
15183       gcc_unreachable ();
15184
15185     case LABEL_REF:
15186     case PLUS:
15187       {
15188         int i;
15189         int overlap = -1;
15190         for (i = 0; i < nregs; i++)
15191           {
15192             /* We're only using DImode here because it's a convenient size.  */
15193             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * i);
15194             ops[1] = adjust_address (mem, DImode, 8 * i);
15195             if (reg_overlap_mentioned_p (ops[0], mem))
15196               {
15197                 gcc_assert (overlap == -1);
15198                 overlap = i;
15199               }
15200             else
15201               {
15202                 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
15203                 output_asm_insn (buff, ops);
15204               }
15205           }
15206         if (overlap != -1)
15207           {
15208             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * overlap);
15209             ops[1] = adjust_address (mem, SImode, 8 * overlap);
15210             sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
15211             output_asm_insn (buff, ops);
15212           }
15213
15214         return "";
15215       }
15216
15217     default:
15218       /* We have to use vldm / vstm for too-large modes.  */
15219       if (nregs > 4)
15220         templ = "v%smia%%?\t%%m0, %%h1";
15221       else
15222         templ = "v%s1.64\t%%h1, %%A0";
15223
15224       ops[0] = mem;
15225       ops[1] = reg;
15226     }
15227
15228   sprintf (buff, templ, load ? "ld" : "st");
15229   output_asm_insn (buff, ops);
15230
15231   return "";
15232 }
15233
15234 /* Compute and return the length of neon_mov<mode>, where <mode> is
15235    one of VSTRUCT modes: EI, OI, CI or XI.  */
15236 int
15237 arm_attr_length_move_neon (rtx insn)
15238 {
15239   rtx reg, mem, addr;
15240   int load;
15241   enum machine_mode mode;
15242
15243   extract_insn_cached (insn);
15244
15245   if (REG_P (recog_data.operand[0]) && REG_P (recog_data.operand[1]))
15246     {
15247       mode = GET_MODE (recog_data.operand[0]);
15248       switch (mode)
15249         {
15250         case EImode:
15251         case OImode:
15252           return 8;
15253         case CImode:
15254           return 12;
15255         case XImode:
15256           return 16;
15257         default:
15258           gcc_unreachable ();
15259         }
15260     }
15261
15262   load = REG_P (recog_data.operand[0]);
15263   reg = recog_data.operand[!load];
15264   mem = recog_data.operand[load];
15265
15266   gcc_assert (MEM_P (mem));
15267
15268   mode = GET_MODE (reg);
15269   addr = XEXP (mem, 0);
15270
15271   /* Strip off const from addresses like (const (plus (...))).  */
15272   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
15273     addr = XEXP (addr, 0);
15274
15275   if (GET_CODE (addr) == LABEL_REF || GET_CODE (addr) == PLUS)
15276     {
15277       int insns = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
15278       return insns * 4;
15279     }
15280   else
15281     return 4;
15282 }
15283
15284 /* Return nonzero if the offset in the address is an immediate.  Otherwise,
15285    return zero.  */
15286
15287 int
15288 arm_address_offset_is_imm (rtx insn)
15289 {
15290   rtx mem, addr;
15291
15292   extract_insn_cached (insn);
15293
15294   if (REG_P (recog_data.operand[0]))
15295     return 0;
15296
15297   mem = recog_data.operand[0];
15298
15299   gcc_assert (MEM_P (mem));
15300
15301   addr = XEXP (mem, 0);
15302
15303   if (REG_P (addr)
15304       || (GET_CODE (addr) == PLUS
15305           && REG_P (XEXP (addr, 0))
15306           && CONST_INT_P (XEXP (addr, 1))))
15307     return 1;
15308   else
15309     return 0;
15310 }
15311
15312 /* Output an ADD r, s, #n where n may be too big for one instruction.
15313    If adding zero to one register, output nothing.  */
15314 const char *
15315 output_add_immediate (rtx *operands)
15316 {
15317   HOST_WIDE_INT n = INTVAL (operands[2]);
15318
15319   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
15320     {
15321       if (n < 0)
15322         output_multi_immediate (operands,
15323                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
15324                                 -n);
15325       else
15326         output_multi_immediate (operands,
15327                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
15328                                 n);
15329     }
15330
15331   return "";
15332 }
15333
15334 /* Output a multiple immediate operation.
15335    OPERANDS is the vector of operands referred to in the output patterns.
15336    INSTR1 is the output pattern to use for the first constant.
15337    INSTR2 is the output pattern to use for subsequent constants.
15338    IMMED_OP is the index of the constant slot in OPERANDS.
15339    N is the constant value.  */
15340 static const char *
15341 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
15342                         int immed_op, HOST_WIDE_INT n)
15343 {
15344 #if HOST_BITS_PER_WIDE_INT > 32
15345   n &= 0xffffffff;
15346 #endif
15347
15348   if (n == 0)
15349     {
15350       /* Quick and easy output.  */
15351       operands[immed_op] = const0_rtx;
15352       output_asm_insn (instr1, operands);
15353     }
15354   else
15355     {
15356       int i;
15357       const char * instr = instr1;
15358
15359       /* Note that n is never zero here (which would give no output).  */
15360       for (i = 0; i < 32; i += 2)
15361         {
15362           if (n & (3 << i))
15363             {
15364               operands[immed_op] = GEN_INT (n & (255 << i));
15365               output_asm_insn (instr, operands);
15366               instr = instr2;
15367               i += 6;
15368             }
15369         }
15370     }
15371
15372   return "";
15373 }
15374
15375 /* Return the name of a shifter operation.  */
15376 static const char *
15377 arm_shift_nmem(enum rtx_code code)
15378 {
15379   switch (code)
15380     {
15381     case ASHIFT:
15382       return ARM_LSL_NAME;
15383
15384     case ASHIFTRT:
15385       return "asr";
15386
15387     case LSHIFTRT:
15388       return "lsr";
15389
15390     case ROTATERT:
15391       return "ror";
15392
15393     default:
15394       abort();
15395     }
15396 }
15397
15398 /* Return the appropriate ARM instruction for the operation code.
15399    The returned result should not be overwritten.  OP is the rtx of the
15400    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
15401    was shifted.  */
15402 const char *
15403 arithmetic_instr (rtx op, int shift_first_arg)
15404 {
15405   switch (GET_CODE (op))
15406     {
15407     case PLUS:
15408       return "add";
15409
15410     case MINUS:
15411       return shift_first_arg ? "rsb" : "sub";
15412
15413     case IOR:
15414       return "orr";
15415
15416     case XOR:
15417       return "eor";
15418
15419     case AND:
15420       return "and";
15421
15422     case ASHIFT:
15423     case ASHIFTRT:
15424     case LSHIFTRT:
15425     case ROTATERT:
15426       return arm_shift_nmem(GET_CODE(op));
15427
15428     default:
15429       gcc_unreachable ();
15430     }
15431 }
15432
15433 /* Ensure valid constant shifts and return the appropriate shift mnemonic
15434    for the operation code.  The returned result should not be overwritten.
15435    OP is the rtx code of the shift.
15436    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
15437    shift.  */
15438 static const char *
15439 shift_op (rtx op, HOST_WIDE_INT *amountp)
15440 {
15441   const char * mnem;
15442   enum rtx_code code = GET_CODE (op);
15443
15444   switch (code)
15445     {
15446     case ROTATE:
15447       if (!CONST_INT_P (XEXP (op, 1)))
15448         {
15449           output_operand_lossage ("invalid shift operand");
15450           return NULL;
15451         }
15452
15453       code = ROTATERT;
15454       *amountp = 32 - INTVAL (XEXP (op, 1));
15455       mnem = "ror";
15456       break;
15457
15458     case ASHIFT:
15459     case ASHIFTRT:
15460     case LSHIFTRT:
15461     case ROTATERT:
15462       mnem = arm_shift_nmem(code);
15463       if (CONST_INT_P (XEXP (op, 1)))
15464         {
15465           *amountp = INTVAL (XEXP (op, 1));
15466         }
15467       else if (REG_P (XEXP (op, 1)))
15468         {
15469           *amountp = -1;
15470           return mnem;
15471         }
15472       else
15473         {
15474           output_operand_lossage ("invalid shift operand");
15475           return NULL;
15476         }
15477       break;
15478
15479     case MULT:
15480       /* We never have to worry about the amount being other than a
15481          power of 2, since this case can never be reloaded from a reg.  */
15482       if (!CONST_INT_P (XEXP (op, 1)))
15483         {
15484           output_operand_lossage ("invalid shift operand");
15485           return NULL;
15486         }
15487
15488       *amountp = INTVAL (XEXP (op, 1)) & 0xFFFFFFFF;
15489
15490       /* Amount must be a power of two.  */
15491       if (*amountp & (*amountp - 1))
15492         {
15493           output_operand_lossage ("invalid shift operand");
15494           return NULL;
15495         }
15496
15497       *amountp = int_log2 (*amountp);
15498       return ARM_LSL_NAME;
15499
15500     default:
15501       output_operand_lossage ("invalid shift operand");
15502       return NULL;
15503     }
15504
15505   /* This is not 100% correct, but follows from the desire to merge
15506      multiplication by a power of 2 with the recognizer for a
15507      shift.  >=32 is not a valid shift for "lsl", so we must try and
15508      output a shift that produces the correct arithmetical result.
15509      Using lsr #32 is identical except for the fact that the carry bit
15510      is not set correctly if we set the flags; but we never use the
15511      carry bit from such an operation, so we can ignore that.  */
15512   if (code == ROTATERT)
15513     /* Rotate is just modulo 32.  */
15514     *amountp &= 31;
15515   else if (*amountp != (*amountp & 31))
15516     {
15517       if (code == ASHIFT)
15518         mnem = "lsr";
15519       *amountp = 32;
15520     }
15521
15522   /* Shifts of 0 are no-ops.  */
15523   if (*amountp == 0)
15524     return NULL;
15525
15526   return mnem;
15527 }
15528
15529 /* Obtain the shift from the POWER of two.  */
15530
15531 static HOST_WIDE_INT
15532 int_log2 (HOST_WIDE_INT power)
15533 {
15534   HOST_WIDE_INT shift = 0;
15535
15536   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
15537     {
15538       gcc_assert (shift <= 31);
15539       shift++;
15540     }
15541
15542   return shift;
15543 }
15544
15545 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
15546    because /bin/as is horribly restrictive.  The judgement about
15547    whether or not each character is 'printable' (and can be output as
15548    is) or not (and must be printed with an octal escape) must be made
15549    with reference to the *host* character set -- the situation is
15550    similar to that discussed in the comments above pp_c_char in
15551    c-pretty-print.c.  */
15552
15553 #define MAX_ASCII_LEN 51
15554
15555 void
15556 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
15557 {
15558   int i;
15559   int len_so_far = 0;
15560
15561   fputs ("\t.ascii\t\"", stream);
15562
15563   for (i = 0; i < len; i++)
15564     {
15565       int c = p[i];
15566
15567       if (len_so_far >= MAX_ASCII_LEN)
15568         {
15569           fputs ("\"\n\t.ascii\t\"", stream);
15570           len_so_far = 0;
15571         }
15572
15573       if (ISPRINT (c))
15574         {
15575           if (c == '\\' || c == '\"')
15576             {
15577               putc ('\\', stream);
15578               len_so_far++;
15579             }
15580           putc (c, stream);
15581           len_so_far++;
15582         }
15583       else
15584         {
15585           fprintf (stream, "\\%03o", c);
15586           len_so_far += 4;
15587         }
15588     }
15589
15590   fputs ("\"\n", stream);
15591 }
15592 \f
15593 /* Compute the register save mask for registers 0 through 12
15594    inclusive.  This code is used by arm_compute_save_reg_mask.  */
15595
15596 static unsigned long
15597 arm_compute_save_reg0_reg12_mask (void)
15598 {
15599   unsigned long func_type = arm_current_func_type ();
15600   unsigned long save_reg_mask = 0;
15601   unsigned int reg;
15602
15603   if (IS_INTERRUPT (func_type))
15604     {
15605       unsigned int max_reg;
15606       /* Interrupt functions must not corrupt any registers,
15607          even call clobbered ones.  If this is a leaf function
15608          we can just examine the registers used by the RTL, but
15609          otherwise we have to assume that whatever function is
15610          called might clobber anything, and so we have to save
15611          all the call-clobbered registers as well.  */
15612       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
15613         /* FIQ handlers have registers r8 - r12 banked, so
15614            we only need to check r0 - r7, Normal ISRs only
15615            bank r14 and r15, so we must check up to r12.
15616            r13 is the stack pointer which is always preserved,
15617            so we do not need to consider it here.  */
15618         max_reg = 7;
15619       else
15620         max_reg = 12;
15621
15622       for (reg = 0; reg <= max_reg; reg++)
15623         if (df_regs_ever_live_p (reg)
15624             || (! crtl->is_leaf && call_used_regs[reg]))
15625           save_reg_mask |= (1 << reg);
15626
15627       /* Also save the pic base register if necessary.  */
15628       if (flag_pic
15629           && !TARGET_SINGLE_PIC_BASE
15630           && arm_pic_register != INVALID_REGNUM
15631           && crtl->uses_pic_offset_table)
15632         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
15633     }
15634   else if (IS_VOLATILE(func_type))
15635     {
15636       /* For noreturn functions we historically omitted register saves
15637          altogether.  However this really messes up debugging.  As a
15638          compromise save just the frame pointers.  Combined with the link
15639          register saved elsewhere this should be sufficient to get
15640          a backtrace.  */
15641       if (frame_pointer_needed)
15642         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
15643       if (df_regs_ever_live_p (ARM_HARD_FRAME_POINTER_REGNUM))
15644         save_reg_mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
15645       if (df_regs_ever_live_p (THUMB_HARD_FRAME_POINTER_REGNUM))
15646         save_reg_mask |= 1 << THUMB_HARD_FRAME_POINTER_REGNUM;
15647     }
15648   else
15649     {
15650       /* In the normal case we only need to save those registers
15651          which are call saved and which are used by this function.  */
15652       for (reg = 0; reg <= 11; reg++)
15653         if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
15654           save_reg_mask |= (1 << reg);
15655
15656       /* Handle the frame pointer as a special case.  */
15657       if (frame_pointer_needed)
15658         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
15659
15660       /* If we aren't loading the PIC register,
15661          don't stack it even though it may be live.  */
15662       if (flag_pic
15663           && !TARGET_SINGLE_PIC_BASE
15664           && arm_pic_register != INVALID_REGNUM
15665           && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
15666               || crtl->uses_pic_offset_table))
15667         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
15668
15669       /* The prologue will copy SP into R0, so save it.  */
15670       if (IS_STACKALIGN (func_type))
15671         save_reg_mask |= 1;
15672     }
15673
15674   /* Save registers so the exception handler can modify them.  */
15675   if (crtl->calls_eh_return)
15676     {
15677       unsigned int i;
15678
15679       for (i = 0; ; i++)
15680         {
15681           reg = EH_RETURN_DATA_REGNO (i);
15682           if (reg == INVALID_REGNUM)
15683             break;
15684           save_reg_mask |= 1 << reg;
15685         }
15686     }
15687
15688   return save_reg_mask;
15689 }
15690
15691
15692 /* Compute the number of bytes used to store the static chain register on the
15693    stack, above the stack frame. We need to know this accurately to get the
15694    alignment of the rest of the stack frame correct. */
15695
15696 static int arm_compute_static_chain_stack_bytes (void)
15697 {
15698   unsigned long func_type = arm_current_func_type ();
15699   int static_chain_stack_bytes = 0;
15700
15701   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM &&
15702       IS_NESTED (func_type) &&
15703       df_regs_ever_live_p (3) && crtl->args.pretend_args_size == 0)
15704     static_chain_stack_bytes = 4;
15705
15706   return static_chain_stack_bytes;
15707 }
15708
15709
15710 /* Compute a bit mask of which registers need to be
15711    saved on the stack for the current function.
15712    This is used by arm_get_frame_offsets, which may add extra registers.  */
15713
15714 static unsigned long
15715 arm_compute_save_reg_mask (void)
15716 {
15717   unsigned int save_reg_mask = 0;
15718   unsigned long func_type = arm_current_func_type ();
15719   unsigned int reg;
15720
15721   if (IS_NAKED (func_type))
15722     /* This should never really happen.  */
15723     return 0;
15724
15725   /* If we are creating a stack frame, then we must save the frame pointer,
15726      IP (which will hold the old stack pointer), LR and the PC.  */
15727   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
15728     save_reg_mask |=
15729       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
15730       | (1 << IP_REGNUM)
15731       | (1 << LR_REGNUM)
15732       | (1 << PC_REGNUM);
15733
15734   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
15735
15736   /* Decide if we need to save the link register.
15737      Interrupt routines have their own banked link register,
15738      so they never need to save it.
15739      Otherwise if we do not use the link register we do not need to save
15740      it.  If we are pushing other registers onto the stack however, we
15741      can save an instruction in the epilogue by pushing the link register
15742      now and then popping it back into the PC.  This incurs extra memory
15743      accesses though, so we only do it when optimizing for size, and only
15744      if we know that we will not need a fancy return sequence.  */
15745   if (df_regs_ever_live_p (LR_REGNUM)
15746       || (save_reg_mask
15747           && optimize_size
15748           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
15749           && !crtl->calls_eh_return))
15750     save_reg_mask |= 1 << LR_REGNUM;
15751
15752   if (cfun->machine->lr_save_eliminated)
15753     save_reg_mask &= ~ (1 << LR_REGNUM);
15754
15755   if (TARGET_REALLY_IWMMXT
15756       && ((bit_count (save_reg_mask)
15757            + ARM_NUM_INTS (crtl->args.pretend_args_size +
15758                            arm_compute_static_chain_stack_bytes())
15759            ) % 2) != 0)
15760     {
15761       /* The total number of registers that are going to be pushed
15762          onto the stack is odd.  We need to ensure that the stack
15763          is 64-bit aligned before we start to save iWMMXt registers,
15764          and also before we start to create locals.  (A local variable
15765          might be a double or long long which we will load/store using
15766          an iWMMXt instruction).  Therefore we need to push another
15767          ARM register, so that the stack will be 64-bit aligned.  We
15768          try to avoid using the arg registers (r0 -r3) as they might be
15769          used to pass values in a tail call.  */
15770       for (reg = 4; reg <= 12; reg++)
15771         if ((save_reg_mask & (1 << reg)) == 0)
15772           break;
15773
15774       if (reg <= 12)
15775         save_reg_mask |= (1 << reg);
15776       else
15777         {
15778           cfun->machine->sibcall_blocked = 1;
15779           save_reg_mask |= (1 << 3);
15780         }
15781     }
15782
15783   /* We may need to push an additional register for use initializing the
15784      PIC base register.  */
15785   if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
15786       && (save_reg_mask & THUMB2_WORK_REGS) == 0)
15787     {
15788       reg = thumb_find_work_register (1 << 4);
15789       if (!call_used_regs[reg])
15790         save_reg_mask |= (1 << reg);
15791     }
15792
15793   return save_reg_mask;
15794 }
15795
15796
15797 /* Compute a bit mask of which registers need to be
15798    saved on the stack for the current function.  */
15799 static unsigned long
15800 thumb1_compute_save_reg_mask (void)
15801 {
15802   unsigned long mask;
15803   unsigned reg;
15804
15805   mask = 0;
15806   for (reg = 0; reg < 12; reg ++)
15807     if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
15808       mask |= 1 << reg;
15809
15810   if (flag_pic
15811       && !TARGET_SINGLE_PIC_BASE
15812       && arm_pic_register != INVALID_REGNUM
15813       && crtl->uses_pic_offset_table)
15814     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
15815
15816   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
15817   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
15818     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
15819
15820   /* LR will also be pushed if any lo regs are pushed.  */
15821   if (mask & 0xff || thumb_force_lr_save ())
15822     mask |= (1 << LR_REGNUM);
15823
15824   /* Make sure we have a low work register if we need one.
15825      We will need one if we are going to push a high register,
15826      but we are not currently intending to push a low register.  */
15827   if ((mask & 0xff) == 0
15828       && ((mask & 0x0f00) || TARGET_BACKTRACE))
15829     {
15830       /* Use thumb_find_work_register to choose which register
15831          we will use.  If the register is live then we will
15832          have to push it.  Use LAST_LO_REGNUM as our fallback
15833          choice for the register to select.  */
15834       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
15835       /* Make sure the register returned by thumb_find_work_register is
15836          not part of the return value.  */
15837       if (reg * UNITS_PER_WORD <= (unsigned) arm_size_return_regs ())
15838         reg = LAST_LO_REGNUM;
15839
15840       if (! call_used_regs[reg])
15841         mask |= 1 << reg;
15842     }
15843
15844   /* The 504 below is 8 bytes less than 512 because there are two possible
15845      alignment words.  We can't tell here if they will be present or not so we
15846      have to play it safe and assume that they are. */
15847   if ((CALLER_INTERWORKING_SLOT_SIZE +
15848        ROUND_UP_WORD (get_frame_size ()) +
15849        crtl->outgoing_args_size) >= 504)
15850     {
15851       /* This is the same as the code in thumb1_expand_prologue() which
15852          determines which register to use for stack decrement. */
15853       for (reg = LAST_ARG_REGNUM + 1; reg <= LAST_LO_REGNUM; reg++)
15854         if (mask & (1 << reg))
15855           break;
15856
15857       if (reg > LAST_LO_REGNUM)
15858         {
15859           /* Make sure we have a register available for stack decrement. */
15860           mask |= 1 << LAST_LO_REGNUM;
15861         }
15862     }
15863
15864   return mask;
15865 }
15866
15867
15868 /* Return the number of bytes required to save VFP registers.  */
15869 static int
15870 arm_get_vfp_saved_size (void)
15871 {
15872   unsigned int regno;
15873   int count;
15874   int saved;
15875
15876   saved = 0;
15877   /* Space for saved VFP registers.  */
15878   if (TARGET_HARD_FLOAT && TARGET_VFP)
15879     {
15880       count = 0;
15881       for (regno = FIRST_VFP_REGNUM;
15882            regno < LAST_VFP_REGNUM;
15883            regno += 2)
15884         {
15885           if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
15886               && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
15887             {
15888               if (count > 0)
15889                 {
15890                   /* Workaround ARM10 VFPr1 bug.  */
15891                   if (count == 2 && !arm_arch6)
15892                     count++;
15893                   saved += count * 8;
15894                 }
15895               count = 0;
15896             }
15897           else
15898             count++;
15899         }
15900       if (count > 0)
15901         {
15902           if (count == 2 && !arm_arch6)
15903             count++;
15904           saved += count * 8;
15905         }
15906     }
15907   return saved;
15908 }
15909
15910
15911 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
15912    everything bar the final return instruction.  If simple_return is true,
15913    then do not output epilogue, because it has already been emitted in RTL.  */
15914 const char *
15915 output_return_instruction (rtx operand, bool really_return, bool reverse,
15916                            bool simple_return)
15917 {
15918   char conditional[10];
15919   char instr[100];
15920   unsigned reg;
15921   unsigned long live_regs_mask;
15922   unsigned long func_type;
15923   arm_stack_offsets *offsets;
15924
15925   func_type = arm_current_func_type ();
15926
15927   if (IS_NAKED (func_type))
15928     return "";
15929
15930   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
15931     {
15932       /* If this function was declared non-returning, and we have
15933          found a tail call, then we have to trust that the called
15934          function won't return.  */
15935       if (really_return)
15936         {
15937           rtx ops[2];
15938
15939           /* Otherwise, trap an attempted return by aborting.  */
15940           ops[0] = operand;
15941           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
15942                                        : "abort");
15943           assemble_external_libcall (ops[1]);
15944           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
15945         }
15946
15947       return "";
15948     }
15949
15950   gcc_assert (!cfun->calls_alloca || really_return);
15951
15952   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
15953
15954   cfun->machine->return_used_this_function = 1;
15955
15956   offsets = arm_get_frame_offsets ();
15957   live_regs_mask = offsets->saved_regs_mask;
15958
15959   if (!simple_return && live_regs_mask)
15960     {
15961       const char * return_reg;
15962
15963       /* If we do not have any special requirements for function exit
15964          (e.g. interworking) then we can load the return address
15965          directly into the PC.  Otherwise we must load it into LR.  */
15966       if (really_return
15967           && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
15968         return_reg = reg_names[PC_REGNUM];
15969       else
15970         return_reg = reg_names[LR_REGNUM];
15971
15972       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
15973         {
15974           /* There are three possible reasons for the IP register
15975              being saved.  1) a stack frame was created, in which case
15976              IP contains the old stack pointer, or 2) an ISR routine
15977              corrupted it, or 3) it was saved to align the stack on
15978              iWMMXt.  In case 1, restore IP into SP, otherwise just
15979              restore IP.  */
15980           if (frame_pointer_needed)
15981             {
15982               live_regs_mask &= ~ (1 << IP_REGNUM);
15983               live_regs_mask |=   (1 << SP_REGNUM);
15984             }
15985           else
15986             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
15987         }
15988
15989       /* On some ARM architectures it is faster to use LDR rather than
15990          LDM to load a single register.  On other architectures, the
15991          cost is the same.  In 26 bit mode, or for exception handlers,
15992          we have to use LDM to load the PC so that the CPSR is also
15993          restored.  */
15994       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
15995         if (live_regs_mask == (1U << reg))
15996           break;
15997
15998       if (reg <= LAST_ARM_REGNUM
15999           && (reg != LR_REGNUM
16000               || ! really_return
16001               || ! IS_INTERRUPT (func_type)))
16002         {
16003           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
16004                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
16005         }
16006       else
16007         {
16008           char *p;
16009           int first = 1;
16010
16011           /* Generate the load multiple instruction to restore the
16012              registers.  Note we can get here, even if
16013              frame_pointer_needed is true, but only if sp already
16014              points to the base of the saved core registers.  */
16015           if (live_regs_mask & (1 << SP_REGNUM))
16016             {
16017               unsigned HOST_WIDE_INT stack_adjust;
16018
16019               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
16020               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
16021
16022               if (stack_adjust && arm_arch5 && TARGET_ARM)
16023                 if (TARGET_UNIFIED_ASM)
16024                   sprintf (instr, "ldmib%s\t%%|sp, {", conditional);
16025                 else
16026                   sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
16027               else
16028                 {
16029                   /* If we can't use ldmib (SA110 bug),
16030                      then try to pop r3 instead.  */
16031                   if (stack_adjust)
16032                     live_regs_mask |= 1 << 3;
16033
16034                   if (TARGET_UNIFIED_ASM)
16035                     sprintf (instr, "ldmfd%s\t%%|sp, {", conditional);
16036                   else
16037                     sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
16038                 }
16039             }
16040           else
16041             if (TARGET_UNIFIED_ASM)
16042               sprintf (instr, "pop%s\t{", conditional);
16043             else
16044               sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
16045
16046           p = instr + strlen (instr);
16047
16048           for (reg = 0; reg <= SP_REGNUM; reg++)
16049             if (live_regs_mask & (1 << reg))
16050               {
16051                 int l = strlen (reg_names[reg]);
16052
16053                 if (first)
16054                   first = 0;
16055                 else
16056                   {
16057                     memcpy (p, ", ", 2);
16058                     p += 2;
16059                   }
16060
16061                 memcpy (p, "%|", 2);
16062                 memcpy (p + 2, reg_names[reg], l);
16063                 p += l + 2;
16064               }
16065
16066           if (live_regs_mask & (1 << LR_REGNUM))
16067             {
16068               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
16069               /* If returning from an interrupt, restore the CPSR.  */
16070               if (IS_INTERRUPT (func_type))
16071                 strcat (p, "^");
16072             }
16073           else
16074             strcpy (p, "}");
16075         }
16076
16077       output_asm_insn (instr, & operand);
16078
16079       /* See if we need to generate an extra instruction to
16080          perform the actual function return.  */
16081       if (really_return
16082           && func_type != ARM_FT_INTERWORKED
16083           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
16084         {
16085           /* The return has already been handled
16086              by loading the LR into the PC.  */
16087           return "";
16088         }
16089     }
16090
16091   if (really_return)
16092     {
16093       switch ((int) ARM_FUNC_TYPE (func_type))
16094         {
16095         case ARM_FT_ISR:
16096         case ARM_FT_FIQ:
16097           /* ??? This is wrong for unified assembly syntax.  */
16098           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
16099           break;
16100
16101         case ARM_FT_INTERWORKED:
16102           sprintf (instr, "bx%s\t%%|lr", conditional);
16103           break;
16104
16105         case ARM_FT_EXCEPTION:
16106           /* ??? This is wrong for unified assembly syntax.  */
16107           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
16108           break;
16109
16110         default:
16111           /* Use bx if it's available.  */
16112           if (arm_arch5 || arm_arch4t)
16113             sprintf (instr, "bx%s\t%%|lr", conditional);
16114           else
16115             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
16116           break;
16117         }
16118
16119       output_asm_insn (instr, & operand);
16120     }
16121
16122   return "";
16123 }
16124
16125 /* Write the function name into the code section, directly preceding
16126    the function prologue.
16127
16128    Code will be output similar to this:
16129      t0
16130          .ascii "arm_poke_function_name", 0
16131          .align
16132      t1
16133          .word 0xff000000 + (t1 - t0)
16134      arm_poke_function_name
16135          mov     ip, sp
16136          stmfd   sp!, {fp, ip, lr, pc}
16137          sub     fp, ip, #4
16138
16139    When performing a stack backtrace, code can inspect the value
16140    of 'pc' stored at 'fp' + 0.  If the trace function then looks
16141    at location pc - 12 and the top 8 bits are set, then we know
16142    that there is a function name embedded immediately preceding this
16143    location and has length ((pc[-3]) & 0xff000000).
16144
16145    We assume that pc is declared as a pointer to an unsigned long.
16146
16147    It is of no benefit to output the function name if we are assembling
16148    a leaf function.  These function types will not contain a stack
16149    backtrace structure, therefore it is not possible to determine the
16150    function name.  */
16151 void
16152 arm_poke_function_name (FILE *stream, const char *name)
16153 {
16154   unsigned long alignlength;
16155   unsigned long length;
16156   rtx           x;
16157
16158   length      = strlen (name) + 1;
16159   alignlength = ROUND_UP_WORD (length);
16160
16161   ASM_OUTPUT_ASCII (stream, name, length);
16162   ASM_OUTPUT_ALIGN (stream, 2);
16163   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
16164   assemble_aligned_integer (UNITS_PER_WORD, x);
16165 }
16166
16167 /* Place some comments into the assembler stream
16168    describing the current function.  */
16169 static void
16170 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
16171 {
16172   unsigned long func_type;
16173
16174   /* ??? Do we want to print some of the below anyway?  */
16175   if (TARGET_THUMB1)
16176     return;
16177
16178   /* Sanity check.  */
16179   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
16180
16181   func_type = arm_current_func_type ();
16182
16183   switch ((int) ARM_FUNC_TYPE (func_type))
16184     {
16185     default:
16186     case ARM_FT_NORMAL:
16187       break;
16188     case ARM_FT_INTERWORKED:
16189       asm_fprintf (f, "\t%@ Function supports interworking.\n");
16190       break;
16191     case ARM_FT_ISR:
16192       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
16193       break;
16194     case ARM_FT_FIQ:
16195       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
16196       break;
16197     case ARM_FT_EXCEPTION:
16198       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
16199       break;
16200     }
16201
16202   if (IS_NAKED (func_type))
16203     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
16204
16205   if (IS_VOLATILE (func_type))
16206     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
16207
16208   if (IS_NESTED (func_type))
16209     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
16210   if (IS_STACKALIGN (func_type))
16211     asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
16212
16213   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
16214                crtl->args.size,
16215                crtl->args.pretend_args_size, frame_size);
16216
16217   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
16218                frame_pointer_needed,
16219                cfun->machine->uses_anonymous_args);
16220
16221   if (cfun->machine->lr_save_eliminated)
16222     asm_fprintf (f, "\t%@ link register save eliminated.\n");
16223
16224   if (crtl->calls_eh_return)
16225     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
16226
16227 }
16228
16229 static void
16230 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
16231                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
16232 {
16233   arm_stack_offsets *offsets;
16234
16235   if (TARGET_THUMB1)
16236     {
16237       int regno;
16238
16239       /* Emit any call-via-reg trampolines that are needed for v4t support
16240          of call_reg and call_value_reg type insns.  */
16241       for (regno = 0; regno < LR_REGNUM; regno++)
16242         {
16243           rtx label = cfun->machine->call_via[regno];
16244
16245           if (label != NULL)
16246             {
16247               switch_to_section (function_section (current_function_decl));
16248               targetm.asm_out.internal_label (asm_out_file, "L",
16249                                               CODE_LABEL_NUMBER (label));
16250               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
16251             }
16252         }
16253
16254       /* ??? Probably not safe to set this here, since it assumes that a
16255          function will be emitted as assembly immediately after we generate
16256          RTL for it.  This does not happen for inline functions.  */
16257       cfun->machine->return_used_this_function = 0;
16258     }
16259   else /* TARGET_32BIT */
16260     {
16261       /* We need to take into account any stack-frame rounding.  */
16262       offsets = arm_get_frame_offsets ();
16263
16264       gcc_assert (!use_return_insn (FALSE, NULL)
16265                   || (cfun->machine->return_used_this_function != 0)
16266                   || offsets->saved_regs == offsets->outgoing_args
16267                   || frame_pointer_needed);
16268
16269       /* Reset the ARM-specific per-function variables.  */
16270       after_arm_reorg = 0;
16271     }
16272 }
16273
16274 /* Generate and emit a pattern that will be recognized as STRD pattern.  If even
16275    number of registers are being pushed, multiple STRD patterns are created for
16276    all register pairs.  If odd number of registers are pushed, emit a
16277    combination of STRDs and STR for the prologue saves.  */
16278 static void
16279 thumb2_emit_strd_push (unsigned long saved_regs_mask)
16280 {
16281   int num_regs = 0;
16282   int i, j;
16283   rtx par = NULL_RTX;
16284   rtx insn = NULL_RTX;
16285   rtx dwarf = NULL_RTX;
16286   rtx tmp, reg, tmp1;
16287
16288   for (i = 0; i <= LAST_ARM_REGNUM; i++)
16289     if (saved_regs_mask & (1 << i))
16290       num_regs++;
16291
16292   gcc_assert (num_regs && num_regs <= 16);
16293
16294   /* Pre-decrement the stack pointer, based on there being num_regs 4-byte
16295      registers to push.  */
16296   tmp = gen_rtx_SET (VOIDmode,
16297                      stack_pointer_rtx,
16298                      plus_constant (Pmode, stack_pointer_rtx, -4 * num_regs));
16299   RTX_FRAME_RELATED_P (tmp) = 1;
16300   insn = emit_insn (tmp);
16301
16302   /* Create sequence for DWARF info.  */
16303   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_regs + 1));
16304
16305   /* RTLs cannot be shared, hence create new copy for dwarf.  */
16306   tmp1 = gen_rtx_SET (VOIDmode,
16307                      stack_pointer_rtx,
16308                      plus_constant (Pmode, stack_pointer_rtx, -4 * num_regs));
16309   RTX_FRAME_RELATED_P (tmp1) = 1;
16310   XVECEXP (dwarf, 0, 0) = tmp1;
16311
16312   gcc_assert (!(saved_regs_mask & (1 << SP_REGNUM)));
16313   gcc_assert (!(saved_regs_mask & (1 << PC_REGNUM)));
16314
16315   /* Var j iterates over all the registers to gather all the registers in
16316      saved_regs_mask.  Var i gives index of register R_j in stack frame.
16317      A PARALLEL RTX of register-pair is created here, so that pattern for
16318      STRD can be matched.  If num_regs is odd, 1st register will be pushed
16319      using STR and remaining registers will be pushed with STRD in pairs.
16320      If num_regs is even, all registers are pushed with STRD in pairs.
16321      Hence, skip first element for odd num_regs.  */
16322   for (i = num_regs - 1, j = LAST_ARM_REGNUM; i >= (num_regs % 2); j--)
16323     if (saved_regs_mask & (1 << j))
16324       {
16325         /* Create RTX for store.  New RTX is created for dwarf as
16326            they are not sharable.  */
16327         reg = gen_rtx_REG (SImode, j);
16328         tmp = gen_rtx_SET (SImode,
16329                            gen_frame_mem
16330                            (SImode,
16331                             plus_constant (Pmode, stack_pointer_rtx, 4 * i)),
16332                            reg);
16333
16334         tmp1 = gen_rtx_SET (SImode,
16335                            gen_frame_mem
16336                            (SImode,
16337                             plus_constant (Pmode, stack_pointer_rtx, 4 * i)),
16338                            reg);
16339         RTX_FRAME_RELATED_P (tmp) = 1;
16340         RTX_FRAME_RELATED_P (tmp1) = 1;
16341
16342         if (((i - (num_regs % 2)) % 2) == 1)
16343           /* When (i - (num_regs % 2)) is odd, the RTX to be emitted is yet to
16344              be created.  Hence create it first.  The STRD pattern we are
16345              generating is :
16346              [ (SET (MEM (PLUS (SP) (NUM))) (reg_t1))
16347                (SET (MEM (PLUS (SP) (NUM + 4))) (reg_t2)) ]
16348              where the target registers need not be consecutive.  */
16349           par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (2));
16350
16351         /* Register R_j is added in PARALLEL RTX.  If (i - (num_regs % 2)) is
16352            even, the reg_j is added as 0th element and if it is odd, reg_i is
16353            added as 1st element of STRD pattern shown above.  */
16354         XVECEXP (par, 0, ((i - (num_regs % 2)) % 2)) = tmp;
16355         XVECEXP (dwarf, 0, (i + 1)) = tmp1;
16356
16357         if (((i - (num_regs % 2)) % 2) == 0)
16358           /* When (i - (num_regs % 2)) is even, RTXs for both the registers
16359              to be loaded are generated in above given STRD pattern, and the
16360              pattern can be emitted now.  */
16361           emit_insn (par);
16362
16363         i--;
16364       }
16365
16366   if ((num_regs % 2) == 1)
16367     {
16368       /* If odd number of registers are pushed, generate STR pattern to store
16369          lone register.  */
16370       for (; (saved_regs_mask & (1 << j)) == 0; j--);
16371
16372       tmp1 = gen_frame_mem (SImode, plus_constant (Pmode,
16373                                                    stack_pointer_rtx, 4 * i));
16374       reg = gen_rtx_REG (SImode, j);
16375       tmp = gen_rtx_SET (SImode, tmp1, reg);
16376       RTX_FRAME_RELATED_P (tmp) = 1;
16377
16378       emit_insn (tmp);
16379
16380       tmp1 = gen_rtx_SET (SImode,
16381                          gen_frame_mem
16382                          (SImode,
16383                           plus_constant (Pmode, stack_pointer_rtx, 4 * i)),
16384                           reg);
16385       RTX_FRAME_RELATED_P (tmp1) = 1;
16386       XVECEXP (dwarf, 0, (i + 1)) = tmp1;
16387     }
16388
16389   add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
16390   RTX_FRAME_RELATED_P (insn) = 1;
16391   return;
16392 }
16393
16394 /* Generate and emit an insn that we will recognize as a push_multi.
16395    Unfortunately, since this insn does not reflect very well the actual
16396    semantics of the operation, we need to annotate the insn for the benefit
16397    of DWARF2 frame unwind information.  */
16398 static rtx
16399 emit_multi_reg_push (unsigned long mask)
16400 {
16401   int num_regs = 0;
16402   int num_dwarf_regs;
16403   int i, j;
16404   rtx par;
16405   rtx dwarf;
16406   int dwarf_par_index;
16407   rtx tmp, reg;
16408
16409   for (i = 0; i <= LAST_ARM_REGNUM; i++)
16410     if (mask & (1 << i))
16411       num_regs++;
16412
16413   gcc_assert (num_regs && num_regs <= 16);
16414
16415   /* We don't record the PC in the dwarf frame information.  */
16416   num_dwarf_regs = num_regs;
16417   if (mask & (1 << PC_REGNUM))
16418     num_dwarf_regs--;
16419
16420   /* For the body of the insn we are going to generate an UNSPEC in
16421      parallel with several USEs.  This allows the insn to be recognized
16422      by the push_multi pattern in the arm.md file.
16423
16424      The body of the insn looks something like this:
16425
16426        (parallel [
16427            (set (mem:BLK (pre_modify:SI (reg:SI sp)
16428                                         (const_int:SI <num>)))
16429                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
16430            (use (reg:SI XX))
16431            (use (reg:SI YY))
16432            ...
16433         ])
16434
16435      For the frame note however, we try to be more explicit and actually
16436      show each register being stored into the stack frame, plus a (single)
16437      decrement of the stack pointer.  We do it this way in order to be
16438      friendly to the stack unwinding code, which only wants to see a single
16439      stack decrement per instruction.  The RTL we generate for the note looks
16440      something like this:
16441
16442       (sequence [
16443            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
16444            (set (mem:SI (reg:SI sp)) (reg:SI r4))
16445            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI XX))
16446            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI YY))
16447            ...
16448         ])
16449
16450      FIXME:: In an ideal world the PRE_MODIFY would not exist and
16451      instead we'd have a parallel expression detailing all
16452      the stores to the various memory addresses so that debug
16453      information is more up-to-date. Remember however while writing
16454      this to take care of the constraints with the push instruction.
16455
16456      Note also that this has to be taken care of for the VFP registers.
16457
16458      For more see PR43399.  */
16459
16460   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
16461   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
16462   dwarf_par_index = 1;
16463
16464   for (i = 0; i <= LAST_ARM_REGNUM; i++)
16465     {
16466       if (mask & (1 << i))
16467         {
16468           reg = gen_rtx_REG (SImode, i);
16469
16470           XVECEXP (par, 0, 0)
16471             = gen_rtx_SET (VOIDmode,
16472                            gen_frame_mem
16473                            (BLKmode,
16474                             gen_rtx_PRE_MODIFY (Pmode,
16475                                                 stack_pointer_rtx,
16476                                                 plus_constant
16477                                                 (Pmode, stack_pointer_rtx,
16478                                                  -4 * num_regs))
16479                             ),
16480                            gen_rtx_UNSPEC (BLKmode,
16481                                            gen_rtvec (1, reg),
16482                                            UNSPEC_PUSH_MULT));
16483
16484           if (i != PC_REGNUM)
16485             {
16486               tmp = gen_rtx_SET (VOIDmode,
16487                                  gen_frame_mem (SImode, stack_pointer_rtx),
16488                                  reg);
16489               RTX_FRAME_RELATED_P (tmp) = 1;
16490               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
16491               dwarf_par_index++;
16492             }
16493
16494           break;
16495         }
16496     }
16497
16498   for (j = 1, i++; j < num_regs; i++)
16499     {
16500       if (mask & (1 << i))
16501         {
16502           reg = gen_rtx_REG (SImode, i);
16503
16504           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
16505
16506           if (i != PC_REGNUM)
16507             {
16508               tmp
16509                 = gen_rtx_SET (VOIDmode,
16510                                gen_frame_mem
16511                                (SImode,
16512                                 plus_constant (Pmode, stack_pointer_rtx,
16513                                                4 * j)),
16514                                reg);
16515               RTX_FRAME_RELATED_P (tmp) = 1;
16516               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
16517             }
16518
16519           j++;
16520         }
16521     }
16522
16523   par = emit_insn (par);
16524
16525   tmp = gen_rtx_SET (VOIDmode,
16526                      stack_pointer_rtx,
16527                      plus_constant (Pmode, stack_pointer_rtx, -4 * num_regs));
16528   RTX_FRAME_RELATED_P (tmp) = 1;
16529   XVECEXP (dwarf, 0, 0) = tmp;
16530
16531   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
16532
16533   return par;
16534 }
16535
16536 /* Generate and emit an insn pattern that we will recognize as a pop_multi.
16537    SAVED_REGS_MASK shows which registers need to be restored.
16538
16539    Unfortunately, since this insn does not reflect very well the actual
16540    semantics of the operation, we need to annotate the insn for the benefit
16541    of DWARF2 frame unwind information.  */
16542 static void
16543 arm_emit_multi_reg_pop (unsigned long saved_regs_mask)
16544 {
16545   int num_regs = 0;
16546   int i, j;
16547   rtx par;
16548   rtx dwarf = NULL_RTX;
16549   rtx tmp, reg;
16550   bool return_in_pc;
16551   int offset_adj;
16552   int emit_update;
16553
16554   return_in_pc = (saved_regs_mask & (1 << PC_REGNUM)) ? true : false;
16555   offset_adj = return_in_pc ? 1 : 0;
16556   for (i = 0; i <= LAST_ARM_REGNUM; i++)
16557     if (saved_regs_mask & (1 << i))
16558       num_regs++;
16559
16560   gcc_assert (num_regs && num_regs <= 16);
16561
16562   /* If SP is in reglist, then we don't emit SP update insn.  */
16563   emit_update = (saved_regs_mask & (1 << SP_REGNUM)) ? 0 : 1;
16564
16565   /* The parallel needs to hold num_regs SETs
16566      and one SET for the stack update.  */
16567   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs + emit_update + offset_adj));
16568
16569   if (return_in_pc)
16570     {
16571       tmp = ret_rtx;
16572       XVECEXP (par, 0, 0) = tmp;
16573     }
16574
16575   if (emit_update)
16576     {
16577       /* Increment the stack pointer, based on there being
16578          num_regs 4-byte registers to restore.  */
16579       tmp = gen_rtx_SET (VOIDmode,
16580                          stack_pointer_rtx,
16581                          plus_constant (Pmode,
16582                                         stack_pointer_rtx,
16583                                         4 * num_regs));
16584       RTX_FRAME_RELATED_P (tmp) = 1;
16585       XVECEXP (par, 0, offset_adj) = tmp;
16586     }
16587
16588   /* Now restore every reg, which may include PC.  */
16589   for (j = 0, i = 0; j < num_regs; i++)
16590     if (saved_regs_mask & (1 << i))
16591       {
16592         reg = gen_rtx_REG (SImode, i);
16593         tmp = gen_rtx_SET (VOIDmode,
16594                            reg,
16595                            gen_frame_mem
16596                            (SImode,
16597                             plus_constant (Pmode, stack_pointer_rtx, 4 * j)));
16598         RTX_FRAME_RELATED_P (tmp) = 1;
16599         XVECEXP (par, 0, j + emit_update + offset_adj) = tmp;
16600
16601         /* We need to maintain a sequence for DWARF info too.  As dwarf info
16602            should not have PC, skip PC.  */
16603         if (i != PC_REGNUM)
16604           dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
16605
16606         j++;
16607       }
16608
16609   if (return_in_pc)
16610     par = emit_jump_insn (par);
16611   else
16612     par = emit_insn (par);
16613
16614   REG_NOTES (par) = dwarf;
16615 }
16616
16617 /* Generate and emit an insn pattern that we will recognize as a pop_multi
16618    of NUM_REGS consecutive VFP regs, starting at FIRST_REG.
16619
16620    Unfortunately, since this insn does not reflect very well the actual
16621    semantics of the operation, we need to annotate the insn for the benefit
16622    of DWARF2 frame unwind information.  */
16623 static void
16624 arm_emit_vfp_multi_reg_pop (int first_reg, int num_regs, rtx base_reg)
16625 {
16626   int i, j;
16627   rtx par;
16628   rtx dwarf = NULL_RTX;
16629   rtx tmp, reg;
16630
16631   gcc_assert (num_regs && num_regs <= 32);
16632
16633     /* Workaround ARM10 VFPr1 bug.  */
16634   if (num_regs == 2 && !arm_arch6)
16635     {
16636       if (first_reg == 15)
16637         first_reg--;
16638
16639       num_regs++;
16640     }
16641
16642   /* We can emit at most 16 D-registers in a single pop_multi instruction, and
16643      there could be up to 32 D-registers to restore.
16644      If there are more than 16 D-registers, make two recursive calls,
16645      each of which emits one pop_multi instruction.  */
16646   if (num_regs > 16)
16647     {
16648       arm_emit_vfp_multi_reg_pop (first_reg, 16, base_reg);
16649       arm_emit_vfp_multi_reg_pop (first_reg + 16, num_regs - 16, base_reg);
16650       return;
16651     }
16652
16653   /* The parallel needs to hold num_regs SETs
16654      and one SET for the stack update.  */
16655   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs + 1));
16656
16657   /* Increment the stack pointer, based on there being
16658      num_regs 8-byte registers to restore.  */
16659   tmp = gen_rtx_SET (VOIDmode,
16660                      base_reg,
16661                      plus_constant (Pmode, base_reg, 8 * num_regs));
16662   RTX_FRAME_RELATED_P (tmp) = 1;
16663   XVECEXP (par, 0, 0) = tmp;
16664
16665   /* Now show every reg that will be restored, using a SET for each.  */
16666   for (j = 0, i=first_reg; j < num_regs; i += 2)
16667     {
16668       reg = gen_rtx_REG (DFmode, i);
16669
16670       tmp = gen_rtx_SET (VOIDmode,
16671                          reg,
16672                          gen_frame_mem
16673                          (DFmode,
16674                           plus_constant (Pmode, base_reg, 8 * j)));
16675       RTX_FRAME_RELATED_P (tmp) = 1;
16676       XVECEXP (par, 0, j + 1) = tmp;
16677
16678       dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
16679
16680       j++;
16681     }
16682
16683   par = emit_insn (par);
16684   REG_NOTES (par) = dwarf;
16685 }
16686
16687 /* Generate and emit a pattern that will be recognized as LDRD pattern.  If even
16688    number of registers are being popped, multiple LDRD patterns are created for
16689    all register pairs.  If odd number of registers are popped, last register is
16690    loaded by using LDR pattern.  */
16691 static void
16692 thumb2_emit_ldrd_pop (unsigned long saved_regs_mask)
16693 {
16694   int num_regs = 0;
16695   int i, j;
16696   rtx par = NULL_RTX;
16697   rtx dwarf = NULL_RTX;
16698   rtx tmp, reg, tmp1;
16699   bool return_in_pc;
16700
16701   return_in_pc = (saved_regs_mask & (1 << PC_REGNUM)) ? true : false;
16702   for (i = 0; i <= LAST_ARM_REGNUM; i++)
16703     if (saved_regs_mask & (1 << i))
16704       num_regs++;
16705
16706   gcc_assert (num_regs && num_regs <= 16);
16707
16708   /* We cannot generate ldrd for PC.  Hence, reduce the count if PC is
16709      to be popped.  So, if num_regs is even, now it will become odd,
16710      and we can generate pop with PC.  If num_regs is odd, it will be
16711      even now, and ldr with return can be generated for PC.  */
16712   if (return_in_pc)
16713     num_regs--;
16714
16715   gcc_assert (!(saved_regs_mask & (1 << SP_REGNUM)));
16716
16717   /* Var j iterates over all the registers to gather all the registers in
16718      saved_regs_mask.  Var i gives index of saved registers in stack frame.
16719      A PARALLEL RTX of register-pair is created here, so that pattern for
16720      LDRD can be matched.  As PC is always last register to be popped, and
16721      we have already decremented num_regs if PC, we don't have to worry
16722      about PC in this loop.  */
16723   for (i = 0, j = 0; i < (num_regs - (num_regs % 2)); j++)
16724     if (saved_regs_mask & (1 << j))
16725       {
16726         /* Create RTX for memory load.  */
16727         reg = gen_rtx_REG (SImode, j);
16728         tmp = gen_rtx_SET (SImode,
16729                            reg,
16730                            gen_frame_mem (SImode,
16731                                plus_constant (Pmode,
16732                                               stack_pointer_rtx, 4 * i)));
16733         RTX_FRAME_RELATED_P (tmp) = 1;
16734
16735         if (i % 2 == 0)
16736           {
16737             /* When saved-register index (i) is even, the RTX to be emitted is
16738                yet to be created.  Hence create it first.  The LDRD pattern we
16739                are generating is :
16740                [ (SET (reg_t0) (MEM (PLUS (SP) (NUM))))
16741                  (SET (reg_t1) (MEM (PLUS (SP) (NUM + 4)))) ]
16742                where target registers need not be consecutive.  */
16743             par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (2));
16744             dwarf = NULL_RTX;
16745           }
16746
16747         /* ith register is added in PARALLEL RTX.  If i is even, the reg_i is
16748            added as 0th element and if i is odd, reg_i is added as 1st element
16749            of LDRD pattern shown above.  */
16750         XVECEXP (par, 0, (i % 2)) = tmp;
16751         dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
16752
16753         if ((i % 2) == 1)
16754           {
16755             /* When saved-register index (i) is odd, RTXs for both the registers
16756                to be loaded are generated in above given LDRD pattern, and the
16757                pattern can be emitted now.  */
16758             par = emit_insn (par);
16759             REG_NOTES (par) = dwarf;
16760           }
16761
16762         i++;
16763       }
16764
16765   /* If the number of registers pushed is odd AND return_in_pc is false OR
16766      number of registers are even AND return_in_pc is true, last register is
16767      popped using LDR.  It can be PC as well.  Hence, adjust the stack first and
16768      then LDR with post increment.  */
16769
16770   /* Increment the stack pointer, based on there being
16771      num_regs 4-byte registers to restore.  */
16772   tmp = gen_rtx_SET (VOIDmode,
16773                      stack_pointer_rtx,
16774                      plus_constant (Pmode, stack_pointer_rtx, 4 * i));
16775   RTX_FRAME_RELATED_P (tmp) = 1;
16776   emit_insn (tmp);
16777
16778   dwarf = NULL_RTX;
16779
16780   if (((num_regs % 2) == 1 && !return_in_pc)
16781       || ((num_regs % 2) == 0 && return_in_pc))
16782     {
16783       /* Scan for the single register to be popped.  Skip until the saved
16784          register is found.  */
16785       for (; (saved_regs_mask & (1 << j)) == 0; j++);
16786
16787       /* Gen LDR with post increment here.  */
16788       tmp1 = gen_rtx_MEM (SImode,
16789                           gen_rtx_POST_INC (SImode,
16790                                             stack_pointer_rtx));
16791       set_mem_alias_set (tmp1, get_frame_alias_set ());
16792
16793       reg = gen_rtx_REG (SImode, j);
16794       tmp = gen_rtx_SET (SImode, reg, tmp1);
16795       RTX_FRAME_RELATED_P (tmp) = 1;
16796       dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
16797
16798       if (return_in_pc)
16799         {
16800           /* If return_in_pc, j must be PC_REGNUM.  */
16801           gcc_assert (j == PC_REGNUM);
16802           par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (2));
16803           XVECEXP (par, 0, 0) = ret_rtx;
16804           XVECEXP (par, 0, 1) = tmp;
16805           par = emit_jump_insn (par);
16806         }
16807       else
16808         {
16809           par = emit_insn (tmp);
16810         }
16811
16812       REG_NOTES (par) = dwarf;
16813     }
16814   else if ((num_regs % 2) == 1 && return_in_pc)
16815     {
16816       /* There are 2 registers to be popped.  So, generate the pattern
16817          pop_multiple_with_stack_update_and_return to pop in PC.  */
16818       arm_emit_multi_reg_pop (saved_regs_mask & (~((1 << j) - 1)));
16819     }
16820
16821   return;
16822 }
16823
16824 /* Calculate the size of the return value that is passed in registers.  */
16825 static unsigned
16826 arm_size_return_regs (void)
16827 {
16828   enum machine_mode mode;
16829
16830   if (crtl->return_rtx != 0)
16831     mode = GET_MODE (crtl->return_rtx);
16832   else
16833     mode = DECL_MODE (DECL_RESULT (current_function_decl));
16834
16835   return GET_MODE_SIZE (mode);
16836 }
16837
16838 /* Return true if the current function needs to save/restore LR.  */
16839 static bool
16840 thumb_force_lr_save (void)
16841 {
16842   return !cfun->machine->lr_save_eliminated
16843          && (!leaf_function_p ()
16844              || thumb_far_jump_used_p ()
16845              || df_regs_ever_live_p (LR_REGNUM));
16846 }
16847
16848
16849 /* Return true if r3 is used by any of the tail call insns in the
16850    current function.  */
16851 static bool
16852 any_sibcall_uses_r3 (void)
16853 {
16854   edge_iterator ei;
16855   edge e;
16856
16857   if (!crtl->tail_call_emit)
16858     return false;
16859   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
16860     if (e->flags & EDGE_SIBCALL)
16861       {
16862         rtx call = BB_END (e->src);
16863         if (!CALL_P (call))
16864           call = prev_nonnote_nondebug_insn (call);
16865         gcc_assert (CALL_P (call) && SIBLING_CALL_P (call));
16866         if (find_regno_fusage (call, USE, 3))
16867           return true;
16868       }
16869   return false;
16870 }
16871
16872
16873 /* Compute the distance from register FROM to register TO.
16874    These can be the arg pointer (26), the soft frame pointer (25),
16875    the stack pointer (13) or the hard frame pointer (11).
16876    In thumb mode r7 is used as the soft frame pointer, if needed.
16877    Typical stack layout looks like this:
16878
16879        old stack pointer -> |    |
16880                              ----
16881                             |    | \
16882                             |    |   saved arguments for
16883                             |    |   vararg functions
16884                             |    | /
16885                               --
16886    hard FP & arg pointer -> |    | \
16887                             |    |   stack
16888                             |    |   frame
16889                             |    | /
16890                               --
16891                             |    | \
16892                             |    |   call saved
16893                             |    |   registers
16894       soft frame pointer -> |    | /
16895                               --
16896                             |    | \
16897                             |    |   local
16898                             |    |   variables
16899      locals base pointer -> |    | /
16900                               --
16901                             |    | \
16902                             |    |   outgoing
16903                             |    |   arguments
16904    current stack pointer -> |    | /
16905                               --
16906
16907   For a given function some or all of these stack components
16908   may not be needed, giving rise to the possibility of
16909   eliminating some of the registers.
16910
16911   The values returned by this function must reflect the behavior
16912   of arm_expand_prologue() and arm_compute_save_reg_mask().
16913
16914   The sign of the number returned reflects the direction of stack
16915   growth, so the values are positive for all eliminations except
16916   from the soft frame pointer to the hard frame pointer.
16917
16918   SFP may point just inside the local variables block to ensure correct
16919   alignment.  */
16920
16921
16922 /* Calculate stack offsets.  These are used to calculate register elimination
16923    offsets and in prologue/epilogue code.  Also calculates which registers
16924    should be saved.  */
16925
16926 static arm_stack_offsets *
16927 arm_get_frame_offsets (void)
16928 {
16929   struct arm_stack_offsets *offsets;
16930   unsigned long func_type;
16931   int leaf;
16932   int saved;
16933   int core_saved;
16934   HOST_WIDE_INT frame_size;
16935   int i;
16936
16937   offsets = &cfun->machine->stack_offsets;
16938
16939   /* We need to know if we are a leaf function.  Unfortunately, it
16940      is possible to be called after start_sequence has been called,
16941      which causes get_insns to return the insns for the sequence,
16942      not the function, which will cause leaf_function_p to return
16943      the incorrect result.
16944
16945      to know about leaf functions once reload has completed, and the
16946      frame size cannot be changed after that time, so we can safely
16947      use the cached value.  */
16948
16949   if (reload_completed)
16950     return offsets;
16951
16952   /* Initially this is the size of the local variables.  It will translated
16953      into an offset once we have determined the size of preceding data.  */
16954   frame_size = ROUND_UP_WORD (get_frame_size ());
16955
16956   leaf = leaf_function_p ();
16957
16958   /* Space for variadic functions.  */
16959   offsets->saved_args = crtl->args.pretend_args_size;
16960
16961   /* In Thumb mode this is incorrect, but never used.  */
16962   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0) +
16963                    arm_compute_static_chain_stack_bytes();
16964
16965   if (TARGET_32BIT)
16966     {
16967       unsigned int regno;
16968
16969       offsets->saved_regs_mask = arm_compute_save_reg_mask ();
16970       core_saved = bit_count (offsets->saved_regs_mask) * 4;
16971       saved = core_saved;
16972
16973       /* We know that SP will be doubleword aligned on entry, and we must
16974          preserve that condition at any subroutine call.  We also require the
16975          soft frame pointer to be doubleword aligned.  */
16976
16977       if (TARGET_REALLY_IWMMXT)
16978         {
16979           /* Check for the call-saved iWMMXt registers.  */
16980           for (regno = FIRST_IWMMXT_REGNUM;
16981                regno <= LAST_IWMMXT_REGNUM;
16982                regno++)
16983             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
16984               saved += 8;
16985         }
16986
16987       func_type = arm_current_func_type ();
16988       /* Space for saved VFP registers.  */
16989       if (! IS_VOLATILE (func_type)
16990           && TARGET_HARD_FLOAT && TARGET_VFP)
16991         saved += arm_get_vfp_saved_size ();
16992     }
16993   else /* TARGET_THUMB1 */
16994     {
16995       offsets->saved_regs_mask = thumb1_compute_save_reg_mask ();
16996       core_saved = bit_count (offsets->saved_regs_mask) * 4;
16997       saved = core_saved;
16998       if (TARGET_BACKTRACE)
16999         saved += 16;
17000     }
17001
17002   /* Saved registers include the stack frame.  */
17003   offsets->saved_regs = offsets->saved_args + saved +
17004                         arm_compute_static_chain_stack_bytes();
17005   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
17006   /* A leaf function does not need any stack alignment if it has nothing
17007      on the stack.  */
17008   if (leaf && frame_size == 0
17009       /* However if it calls alloca(), we have a dynamically allocated
17010          block of BIGGEST_ALIGNMENT on stack, so still do stack alignment.  */
17011       && ! cfun->calls_alloca)
17012     {
17013       offsets->outgoing_args = offsets->soft_frame;
17014       offsets->locals_base = offsets->soft_frame;
17015       return offsets;
17016     }
17017
17018   /* Ensure SFP has the correct alignment.  */
17019   if (ARM_DOUBLEWORD_ALIGN
17020       && (offsets->soft_frame & 7))
17021     {
17022       offsets->soft_frame += 4;
17023       /* Try to align stack by pushing an extra reg.  Don't bother doing this
17024          when there is a stack frame as the alignment will be rolled into
17025          the normal stack adjustment.  */
17026       if (frame_size + crtl->outgoing_args_size == 0)
17027         {
17028           int reg = -1;
17029
17030           /* If it is safe to use r3, then do so.  This sometimes
17031              generates better code on Thumb-2 by avoiding the need to
17032              use 32-bit push/pop instructions.  */
17033           if (! any_sibcall_uses_r3 ()
17034               && arm_size_return_regs () <= 12
17035               && (offsets->saved_regs_mask & (1 << 3)) == 0)
17036             {
17037               reg = 3;
17038             }
17039           else
17040             for (i = 4; i <= (TARGET_THUMB1 ? LAST_LO_REGNUM : 11); i++)
17041               {
17042                 /* Avoid fixed registers; they may be changed at
17043                    arbitrary times so it's unsafe to restore them
17044                    during the epilogue.  */
17045                 if (!fixed_regs[i]
17046                     && (offsets->saved_regs_mask & (1 << i)) == 0)
17047                   {
17048                     reg = i;
17049                     break;
17050                   }
17051               }
17052
17053           if (reg != -1)
17054             {
17055               offsets->saved_regs += 4;
17056               offsets->saved_regs_mask |= (1 << reg);
17057             }
17058         }
17059     }
17060
17061   offsets->locals_base = offsets->soft_frame + frame_size;
17062   offsets->outgoing_args = (offsets->locals_base
17063                             + crtl->outgoing_args_size);
17064
17065   if (ARM_DOUBLEWORD_ALIGN)
17066     {
17067       /* Ensure SP remains doubleword aligned.  */
17068       if (offsets->outgoing_args & 7)
17069         offsets->outgoing_args += 4;
17070       gcc_assert (!(offsets->outgoing_args & 7));
17071     }
17072
17073   return offsets;
17074 }
17075
17076
17077 /* Calculate the relative offsets for the different stack pointers.  Positive
17078    offsets are in the direction of stack growth.  */
17079
17080 HOST_WIDE_INT
17081 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
17082 {
17083   arm_stack_offsets *offsets;
17084
17085   offsets = arm_get_frame_offsets ();
17086
17087   /* OK, now we have enough information to compute the distances.
17088      There must be an entry in these switch tables for each pair
17089      of registers in ELIMINABLE_REGS, even if some of the entries
17090      seem to be redundant or useless.  */
17091   switch (from)
17092     {
17093     case ARG_POINTER_REGNUM:
17094       switch (to)
17095         {
17096         case THUMB_HARD_FRAME_POINTER_REGNUM:
17097           return 0;
17098
17099         case FRAME_POINTER_REGNUM:
17100           /* This is the reverse of the soft frame pointer
17101              to hard frame pointer elimination below.  */
17102           return offsets->soft_frame - offsets->saved_args;
17103
17104         case ARM_HARD_FRAME_POINTER_REGNUM:
17105           /* This is only non-zero in the case where the static chain register
17106              is stored above the frame.  */
17107           return offsets->frame - offsets->saved_args - 4;
17108
17109         case STACK_POINTER_REGNUM:
17110           /* If nothing has been pushed on the stack at all
17111              then this will return -4.  This *is* correct!  */
17112           return offsets->outgoing_args - (offsets->saved_args + 4);
17113
17114         default:
17115           gcc_unreachable ();
17116         }
17117       gcc_unreachable ();
17118
17119     case FRAME_POINTER_REGNUM:
17120       switch (to)
17121         {
17122         case THUMB_HARD_FRAME_POINTER_REGNUM:
17123           return 0;
17124
17125         case ARM_HARD_FRAME_POINTER_REGNUM:
17126           /* The hard frame pointer points to the top entry in the
17127              stack frame.  The soft frame pointer to the bottom entry
17128              in the stack frame.  If there is no stack frame at all,
17129              then they are identical.  */
17130
17131           return offsets->frame - offsets->soft_frame;
17132
17133         case STACK_POINTER_REGNUM:
17134           return offsets->outgoing_args - offsets->soft_frame;
17135
17136         default:
17137           gcc_unreachable ();
17138         }
17139       gcc_unreachable ();
17140
17141     default:
17142       /* You cannot eliminate from the stack pointer.
17143          In theory you could eliminate from the hard frame
17144          pointer to the stack pointer, but this will never
17145          happen, since if a stack frame is not needed the
17146          hard frame pointer will never be used.  */
17147       gcc_unreachable ();
17148     }
17149 }
17150
17151 /* Given FROM and TO register numbers, say whether this elimination is
17152    allowed.  Frame pointer elimination is automatically handled.
17153
17154    All eliminations are permissible.  Note that ARG_POINTER_REGNUM and
17155    HARD_FRAME_POINTER_REGNUM are in fact the same thing.  If we need a frame
17156    pointer, we must eliminate FRAME_POINTER_REGNUM into
17157    HARD_FRAME_POINTER_REGNUM and not into STACK_POINTER_REGNUM or
17158    ARG_POINTER_REGNUM.  */
17159
17160 bool
17161 arm_can_eliminate (const int from, const int to)
17162 {
17163   return ((to == FRAME_POINTER_REGNUM && from == ARG_POINTER_REGNUM) ? false :
17164           (to == STACK_POINTER_REGNUM && frame_pointer_needed) ? false :
17165           (to == ARM_HARD_FRAME_POINTER_REGNUM && TARGET_THUMB) ? false :
17166           (to == THUMB_HARD_FRAME_POINTER_REGNUM && TARGET_ARM) ? false :
17167            true);
17168 }
17169
17170 /* Emit RTL to save coprocessor registers on function entry.  Returns the
17171    number of bytes pushed.  */
17172
17173 static int
17174 arm_save_coproc_regs(void)
17175 {
17176   int saved_size = 0;
17177   unsigned reg;
17178   unsigned start_reg;
17179   rtx insn;
17180
17181   for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
17182     if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
17183       {
17184         insn = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
17185         insn = gen_rtx_MEM (V2SImode, insn);
17186         insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
17187         RTX_FRAME_RELATED_P (insn) = 1;
17188         saved_size += 8;
17189       }
17190
17191   if (TARGET_HARD_FLOAT && TARGET_VFP)
17192     {
17193       start_reg = FIRST_VFP_REGNUM;
17194
17195       for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
17196         {
17197           if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
17198               && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
17199             {
17200               if (start_reg != reg)
17201                 saved_size += vfp_emit_fstmd (start_reg,
17202                                               (reg - start_reg) / 2);
17203               start_reg = reg + 2;
17204             }
17205         }
17206       if (start_reg != reg)
17207         saved_size += vfp_emit_fstmd (start_reg,
17208                                       (reg - start_reg) / 2);
17209     }
17210   return saved_size;
17211 }
17212
17213
17214 /* Set the Thumb frame pointer from the stack pointer.  */
17215
17216 static void
17217 thumb_set_frame_pointer (arm_stack_offsets *offsets)
17218 {
17219   HOST_WIDE_INT amount;
17220   rtx insn, dwarf;
17221
17222   amount = offsets->outgoing_args - offsets->locals_base;
17223   if (amount < 1024)
17224     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
17225                                   stack_pointer_rtx, GEN_INT (amount)));
17226   else
17227     {
17228       emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
17229       /* Thumb-2 RTL patterns expect sp as the first input.  Thumb-1
17230          expects the first two operands to be the same.  */
17231       if (TARGET_THUMB2)
17232         {
17233           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
17234                                         stack_pointer_rtx,
17235                                         hard_frame_pointer_rtx));
17236         }
17237       else
17238         {
17239           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
17240                                         hard_frame_pointer_rtx,
17241                                         stack_pointer_rtx));
17242         }
17243       dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
17244                            plus_constant (Pmode, stack_pointer_rtx, amount));
17245       RTX_FRAME_RELATED_P (dwarf) = 1;
17246       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
17247     }
17248
17249   RTX_FRAME_RELATED_P (insn) = 1;
17250 }
17251
17252 /* Generate the prologue instructions for entry into an ARM or Thumb-2
17253    function.  */
17254 void
17255 arm_expand_prologue (void)
17256 {
17257   rtx amount;
17258   rtx insn;
17259   rtx ip_rtx;
17260   unsigned long live_regs_mask;
17261   unsigned long func_type;
17262   int fp_offset = 0;
17263   int saved_pretend_args = 0;
17264   int saved_regs = 0;
17265   unsigned HOST_WIDE_INT args_to_push;
17266   arm_stack_offsets *offsets;
17267
17268   func_type = arm_current_func_type ();
17269
17270   /* Naked functions don't have prologues.  */
17271   if (IS_NAKED (func_type))
17272     return;
17273
17274   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
17275   args_to_push = crtl->args.pretend_args_size;
17276
17277   /* Compute which register we will have to save onto the stack.  */
17278   offsets = arm_get_frame_offsets ();
17279   live_regs_mask = offsets->saved_regs_mask;
17280
17281   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
17282
17283   if (IS_STACKALIGN (func_type))
17284     {
17285       rtx r0, r1;
17286
17287       /* Handle a word-aligned stack pointer.  We generate the following:
17288
17289           mov r0, sp
17290           bic r1, r0, #7
17291           mov sp, r1
17292           <save and restore r0 in normal prologue/epilogue>
17293           mov sp, r0
17294           bx lr
17295
17296          The unwinder doesn't need to know about the stack realignment.
17297          Just tell it we saved SP in r0.  */
17298       gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
17299
17300       r0 = gen_rtx_REG (SImode, 0);
17301       r1 = gen_rtx_REG (SImode, 1);
17302
17303       insn = emit_insn (gen_movsi (r0, stack_pointer_rtx));
17304       RTX_FRAME_RELATED_P (insn) = 1;
17305       add_reg_note (insn, REG_CFA_REGISTER, NULL);
17306
17307       emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
17308
17309       /* ??? The CFA changes here, which may cause GDB to conclude that it
17310          has entered a different function.  That said, the unwind info is
17311          correct, individually, before and after this instruction because
17312          we've described the save of SP, which will override the default
17313          handling of SP as restoring from the CFA.  */
17314       emit_insn (gen_movsi (stack_pointer_rtx, r1));
17315     }
17316
17317   /* For APCS frames, if IP register is clobbered
17318      when creating frame, save that register in a special
17319      way.  */
17320   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
17321     {
17322       if (IS_INTERRUPT (func_type))
17323         {
17324           /* Interrupt functions must not corrupt any registers.
17325              Creating a frame pointer however, corrupts the IP
17326              register, so we must push it first.  */
17327           emit_multi_reg_push (1 << IP_REGNUM);
17328
17329           /* Do not set RTX_FRAME_RELATED_P on this insn.
17330              The dwarf stack unwinding code only wants to see one
17331              stack decrement per function, and this is not it.  If
17332              this instruction is labeled as being part of the frame
17333              creation sequence then dwarf2out_frame_debug_expr will
17334              die when it encounters the assignment of IP to FP
17335              later on, since the use of SP here establishes SP as
17336              the CFA register and not IP.
17337
17338              Anyway this instruction is not really part of the stack
17339              frame creation although it is part of the prologue.  */
17340         }
17341       else if (IS_NESTED (func_type))
17342         {
17343           /* The Static chain register is the same as the IP register
17344              used as a scratch register during stack frame creation.
17345              To get around this need to find somewhere to store IP
17346              whilst the frame is being created.  We try the following
17347              places in order:
17348
17349                1. The last argument register.
17350                2. A slot on the stack above the frame.  (This only
17351                   works if the function is not a varargs function).
17352                3. Register r3, after pushing the argument registers
17353                   onto the stack.
17354
17355              Note - we only need to tell the dwarf2 backend about the SP
17356              adjustment in the second variant; the static chain register
17357              doesn't need to be unwound, as it doesn't contain a value
17358              inherited from the caller.  */
17359
17360           if (df_regs_ever_live_p (3) == false)
17361             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
17362           else if (args_to_push == 0)
17363             {
17364               rtx dwarf;
17365
17366               gcc_assert(arm_compute_static_chain_stack_bytes() == 4);
17367               saved_regs += 4;
17368
17369               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
17370               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
17371               fp_offset = 4;
17372
17373               /* Just tell the dwarf backend that we adjusted SP.  */
17374               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
17375                                    plus_constant (Pmode, stack_pointer_rtx,
17376                                                   -fp_offset));
17377               RTX_FRAME_RELATED_P (insn) = 1;
17378               add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
17379             }
17380           else
17381             {
17382               /* Store the args on the stack.  */
17383               if (cfun->machine->uses_anonymous_args)
17384                 insn = emit_multi_reg_push
17385                   ((0xf0 >> (args_to_push / 4)) & 0xf);
17386               else
17387                 insn = emit_insn
17388                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
17389                                GEN_INT (- args_to_push)));
17390
17391               RTX_FRAME_RELATED_P (insn) = 1;
17392
17393               saved_pretend_args = 1;
17394               fp_offset = args_to_push;
17395               args_to_push = 0;
17396
17397               /* Now reuse r3 to preserve IP.  */
17398               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
17399             }
17400         }
17401
17402       insn = emit_set_insn (ip_rtx,
17403                             plus_constant (Pmode, stack_pointer_rtx,
17404                                            fp_offset));
17405       RTX_FRAME_RELATED_P (insn) = 1;
17406     }
17407
17408   if (args_to_push)
17409     {
17410       /* Push the argument registers, or reserve space for them.  */
17411       if (cfun->machine->uses_anonymous_args)
17412         insn = emit_multi_reg_push
17413           ((0xf0 >> (args_to_push / 4)) & 0xf);
17414       else
17415         insn = emit_insn
17416           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
17417                        GEN_INT (- args_to_push)));
17418       RTX_FRAME_RELATED_P (insn) = 1;
17419     }
17420
17421   /* If this is an interrupt service routine, and the link register
17422      is going to be pushed, and we're not generating extra
17423      push of IP (needed when frame is needed and frame layout if apcs),
17424      subtracting four from LR now will mean that the function return
17425      can be done with a single instruction.  */
17426   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
17427       && (live_regs_mask & (1 << LR_REGNUM)) != 0
17428       && !(frame_pointer_needed && TARGET_APCS_FRAME)
17429       && TARGET_ARM)
17430     {
17431       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
17432
17433       emit_set_insn (lr, plus_constant (SImode, lr, -4));
17434     }
17435
17436   if (live_regs_mask)
17437     {
17438       saved_regs += bit_count (live_regs_mask) * 4;
17439       if (optimize_size && !frame_pointer_needed
17440           && saved_regs == offsets->saved_regs - offsets->saved_args)
17441         {
17442           /* If no coprocessor registers are being pushed and we don't have
17443              to worry about a frame pointer then push extra registers to
17444              create the stack frame.  This is done is a way that does not
17445              alter the frame layout, so is independent of the epilogue.  */
17446           int n;
17447           int frame;
17448           n = 0;
17449           while (n < 8 && (live_regs_mask & (1 << n)) == 0)
17450             n++;
17451           frame = offsets->outgoing_args - (offsets->saved_args + saved_regs);
17452           if (frame && n * 4 >= frame)
17453             {
17454               n = frame / 4;
17455               live_regs_mask |= (1 << n) - 1;
17456               saved_regs += frame;
17457             }
17458         }
17459
17460       if (current_tune->prefer_ldrd_strd
17461           && !optimize_function_for_size_p (cfun))
17462         {
17463           if (TARGET_THUMB2)
17464             {
17465               thumb2_emit_strd_push (live_regs_mask);
17466             }
17467           else
17468             {
17469               insn = emit_multi_reg_push (live_regs_mask);
17470               RTX_FRAME_RELATED_P (insn) = 1;
17471             }
17472         }
17473       else
17474         {
17475           insn = emit_multi_reg_push (live_regs_mask);
17476           RTX_FRAME_RELATED_P (insn) = 1;
17477         }
17478     }
17479
17480   if (! IS_VOLATILE (func_type))
17481     saved_regs += arm_save_coproc_regs ();
17482
17483   if (frame_pointer_needed && TARGET_ARM)
17484     {
17485       /* Create the new frame pointer.  */
17486       if (TARGET_APCS_FRAME)
17487         {
17488           insn = GEN_INT (-(4 + args_to_push + fp_offset));
17489           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
17490           RTX_FRAME_RELATED_P (insn) = 1;
17491
17492           if (IS_NESTED (func_type))
17493             {
17494               /* Recover the static chain register.  */
17495               if (!df_regs_ever_live_p (3)
17496                   || saved_pretend_args)
17497                 insn = gen_rtx_REG (SImode, 3);
17498               else /* if (crtl->args.pretend_args_size == 0) */
17499                 {
17500                   insn = plus_constant (Pmode, hard_frame_pointer_rtx, 4);
17501                   insn = gen_frame_mem (SImode, insn);
17502                 }
17503               emit_set_insn (ip_rtx, insn);
17504               /* Add a USE to stop propagate_one_insn() from barfing.  */
17505               emit_insn (gen_force_register_use (ip_rtx));
17506             }
17507         }
17508       else
17509         {
17510           insn = GEN_INT (saved_regs - 4);
17511           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
17512                                         stack_pointer_rtx, insn));
17513           RTX_FRAME_RELATED_P (insn) = 1;
17514         }
17515     }
17516
17517   if (flag_stack_usage_info)
17518     current_function_static_stack_size
17519       = offsets->outgoing_args - offsets->saved_args;
17520
17521   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
17522     {
17523       /* This add can produce multiple insns for a large constant, so we
17524          need to get tricky.  */
17525       rtx last = get_last_insn ();
17526
17527       amount = GEN_INT (offsets->saved_args + saved_regs
17528                         - offsets->outgoing_args);
17529
17530       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
17531                                     amount));
17532       do
17533         {
17534           last = last ? NEXT_INSN (last) : get_insns ();
17535           RTX_FRAME_RELATED_P (last) = 1;
17536         }
17537       while (last != insn);
17538
17539       /* If the frame pointer is needed, emit a special barrier that
17540          will prevent the scheduler from moving stores to the frame
17541          before the stack adjustment.  */
17542       if (frame_pointer_needed)
17543         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
17544                                          hard_frame_pointer_rtx));
17545     }
17546
17547
17548   if (frame_pointer_needed && TARGET_THUMB2)
17549     thumb_set_frame_pointer (offsets);
17550
17551   if (flag_pic && arm_pic_register != INVALID_REGNUM)
17552     {
17553       unsigned long mask;
17554
17555       mask = live_regs_mask;
17556       mask &= THUMB2_WORK_REGS;
17557       if (!IS_NESTED (func_type))
17558         mask |= (1 << IP_REGNUM);
17559       arm_load_pic_register (mask);
17560     }
17561
17562   /* If we are profiling, make sure no instructions are scheduled before
17563      the call to mcount.  Similarly if the user has requested no
17564      scheduling in the prolog.  Similarly if we want non-call exceptions
17565      using the EABI unwinder, to prevent faulting instructions from being
17566      swapped with a stack adjustment.  */
17567   if (crtl->profile || !TARGET_SCHED_PROLOG
17568       || (arm_except_unwind_info (&global_options) == UI_TARGET
17569           && cfun->can_throw_non_call_exceptions))
17570     emit_insn (gen_blockage ());
17571
17572   /* If the link register is being kept alive, with the return address in it,
17573      then make sure that it does not get reused by the ce2 pass.  */
17574   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
17575     cfun->machine->lr_save_eliminated = 1;
17576 }
17577 \f
17578 /* Print condition code to STREAM.  Helper function for arm_print_operand.  */
17579 static void
17580 arm_print_condition (FILE *stream)
17581 {
17582   if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
17583     {
17584       /* Branch conversion is not implemented for Thumb-2.  */
17585       if (TARGET_THUMB)
17586         {
17587           output_operand_lossage ("predicated Thumb instruction");
17588           return;
17589         }
17590       if (current_insn_predicate != NULL)
17591         {
17592           output_operand_lossage
17593             ("predicated instruction in conditional sequence");
17594           return;
17595         }
17596
17597       fputs (arm_condition_codes[arm_current_cc], stream);
17598     }
17599   else if (current_insn_predicate)
17600     {
17601       enum arm_cond_code code;
17602
17603       if (TARGET_THUMB1)
17604         {
17605           output_operand_lossage ("predicated Thumb instruction");
17606           return;
17607         }
17608
17609       code = get_arm_condition_code (current_insn_predicate);
17610       fputs (arm_condition_codes[code], stream);
17611     }
17612 }
17613
17614
17615 /* If CODE is 'd', then the X is a condition operand and the instruction
17616    should only be executed if the condition is true.
17617    if CODE is 'D', then the X is a condition operand and the instruction
17618    should only be executed if the condition is false: however, if the mode
17619    of the comparison is CCFPEmode, then always execute the instruction -- we
17620    do this because in these circumstances !GE does not necessarily imply LT;
17621    in these cases the instruction pattern will take care to make sure that
17622    an instruction containing %d will follow, thereby undoing the effects of
17623    doing this instruction unconditionally.
17624    If CODE is 'N' then X is a floating point operand that must be negated
17625    before output.
17626    If CODE is 'B' then output a bitwise inverted value of X (a const int).
17627    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
17628 static void
17629 arm_print_operand (FILE *stream, rtx x, int code)
17630 {
17631   switch (code)
17632     {
17633     case '@':
17634       fputs (ASM_COMMENT_START, stream);
17635       return;
17636
17637     case '_':
17638       fputs (user_label_prefix, stream);
17639       return;
17640
17641     case '|':
17642       fputs (REGISTER_PREFIX, stream);
17643       return;
17644
17645     case '?':
17646       arm_print_condition (stream);
17647       return;
17648
17649     case '(':
17650       /* Nothing in unified syntax, otherwise the current condition code.  */
17651       if (!TARGET_UNIFIED_ASM)
17652         arm_print_condition (stream);
17653       break;
17654
17655     case ')':
17656       /* The current condition code in unified syntax, otherwise nothing.  */
17657       if (TARGET_UNIFIED_ASM)
17658         arm_print_condition (stream);
17659       break;
17660
17661     case '.':
17662       /* The current condition code for a condition code setting instruction.
17663          Preceded by 's' in unified syntax, otherwise followed by 's'.  */
17664       if (TARGET_UNIFIED_ASM)
17665         {
17666           fputc('s', stream);
17667           arm_print_condition (stream);
17668         }
17669       else
17670         {
17671           arm_print_condition (stream);
17672           fputc('s', stream);
17673         }
17674       return;
17675
17676     case '!':
17677       /* If the instruction is conditionally executed then print
17678          the current condition code, otherwise print 's'.  */
17679       gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
17680       if (current_insn_predicate)
17681         arm_print_condition (stream);
17682       else
17683         fputc('s', stream);
17684       break;
17685
17686     /* %# is a "break" sequence. It doesn't output anything, but is used to
17687        separate e.g. operand numbers from following text, if that text consists
17688        of further digits which we don't want to be part of the operand
17689        number.  */
17690     case '#':
17691       return;
17692
17693     case 'N':
17694       {
17695         REAL_VALUE_TYPE r;
17696         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
17697         r = real_value_negate (&r);
17698         fprintf (stream, "%s", fp_const_from_val (&r));
17699       }
17700       return;
17701
17702     /* An integer or symbol address without a preceding # sign.  */
17703     case 'c':
17704       switch (GET_CODE (x))
17705         {
17706         case CONST_INT:
17707           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
17708           break;
17709
17710         case SYMBOL_REF:
17711           output_addr_const (stream, x);
17712           break;
17713
17714         case CONST:
17715           if (GET_CODE (XEXP (x, 0)) == PLUS
17716               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)
17717             {
17718               output_addr_const (stream, x);
17719               break;
17720             }
17721           /* Fall through.  */
17722
17723         default:
17724           output_operand_lossage ("Unsupported operand for code '%c'", code);
17725         }
17726       return;
17727
17728     /* An integer that we want to print in HEX.  */
17729     case 'x':
17730       switch (GET_CODE (x))
17731         {
17732         case CONST_INT:
17733           fprintf (stream, "#" HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
17734           break;
17735
17736         default:
17737           output_operand_lossage ("Unsupported operand for code '%c'", code);
17738         }
17739       return;
17740
17741     case 'B':
17742       if (CONST_INT_P (x))
17743         {
17744           HOST_WIDE_INT val;
17745           val = ARM_SIGN_EXTEND (~INTVAL (x));
17746           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
17747         }
17748       else
17749         {
17750           putc ('~', stream);
17751           output_addr_const (stream, x);
17752         }
17753       return;
17754
17755     case 'L':
17756       /* The low 16 bits of an immediate constant.  */
17757       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
17758       return;
17759
17760     case 'i':
17761       fprintf (stream, "%s", arithmetic_instr (x, 1));
17762       return;
17763
17764     case 'I':
17765       fprintf (stream, "%s", arithmetic_instr (x, 0));
17766       return;
17767
17768     case 'S':
17769       {
17770         HOST_WIDE_INT val;
17771         const char *shift;
17772
17773         shift = shift_op (x, &val);
17774
17775         if (shift)
17776           {
17777             fprintf (stream, ", %s ", shift);
17778             if (val == -1)
17779               arm_print_operand (stream, XEXP (x, 1), 0);
17780             else
17781               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
17782           }
17783       }
17784       return;
17785
17786       /* An explanation of the 'Q', 'R' and 'H' register operands:
17787
17788          In a pair of registers containing a DI or DF value the 'Q'
17789          operand returns the register number of the register containing
17790          the least significant part of the value.  The 'R' operand returns
17791          the register number of the register containing the most
17792          significant part of the value.
17793
17794          The 'H' operand returns the higher of the two register numbers.
17795          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
17796          same as the 'Q' operand, since the most significant part of the
17797          value is held in the lower number register.  The reverse is true
17798          on systems where WORDS_BIG_ENDIAN is false.
17799
17800          The purpose of these operands is to distinguish between cases
17801          where the endian-ness of the values is important (for example
17802          when they are added together), and cases where the endian-ness
17803          is irrelevant, but the order of register operations is important.
17804          For example when loading a value from memory into a register
17805          pair, the endian-ness does not matter.  Provided that the value
17806          from the lower memory address is put into the lower numbered
17807          register, and the value from the higher address is put into the
17808          higher numbered register, the load will work regardless of whether
17809          the value being loaded is big-wordian or little-wordian.  The
17810          order of the two register loads can matter however, if the address
17811          of the memory location is actually held in one of the registers
17812          being overwritten by the load.
17813
17814          The 'Q' and 'R' constraints are also available for 64-bit
17815          constants.  */
17816     case 'Q':
17817       if (CONST_INT_P (x) || CONST_DOUBLE_P (x))
17818         {
17819           rtx part = gen_lowpart (SImode, x);
17820           fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
17821           return;
17822         }
17823
17824       if (!REG_P (x) || REGNO (x) > LAST_ARM_REGNUM)
17825         {
17826           output_operand_lossage ("invalid operand for code '%c'", code);
17827           return;
17828         }
17829
17830       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
17831       return;
17832
17833     case 'R':
17834       if (CONST_INT_P (x) || CONST_DOUBLE_P (x))
17835         {
17836           enum machine_mode mode = GET_MODE (x);
17837           rtx part;
17838
17839           if (mode == VOIDmode)
17840             mode = DImode;
17841           part = gen_highpart_mode (SImode, mode, x);
17842           fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, INTVAL (part));
17843           return;
17844         }
17845
17846       if (!REG_P (x) || REGNO (x) > LAST_ARM_REGNUM)
17847         {
17848           output_operand_lossage ("invalid operand for code '%c'", code);
17849           return;
17850         }
17851
17852       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
17853       return;
17854
17855     case 'H':
17856       if (!REG_P (x) || REGNO (x) > LAST_ARM_REGNUM)
17857         {
17858           output_operand_lossage ("invalid operand for code '%c'", code);
17859           return;
17860         }
17861
17862       asm_fprintf (stream, "%r", REGNO (x) + 1);
17863       return;
17864
17865     case 'J':
17866       if (!REG_P (x) || REGNO (x) > LAST_ARM_REGNUM)
17867         {
17868           output_operand_lossage ("invalid operand for code '%c'", code);
17869           return;
17870         }
17871
17872       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 3 : 2));
17873       return;
17874
17875     case 'K':
17876       if (!REG_P (x) || REGNO (x) > LAST_ARM_REGNUM)
17877         {
17878           output_operand_lossage ("invalid operand for code '%c'", code);
17879           return;
17880         }
17881
17882       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 2 : 3));
17883       return;
17884
17885     case 'm':
17886       asm_fprintf (stream, "%r",
17887                    REG_P (XEXP (x, 0))
17888                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
17889       return;
17890
17891     case 'M':
17892       asm_fprintf (stream, "{%r-%r}",
17893                    REGNO (x),
17894                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
17895       return;
17896
17897     /* Like 'M', but writing doubleword vector registers, for use by Neon
17898        insns.  */
17899     case 'h':
17900       {
17901         int regno = (REGNO (x) - FIRST_VFP_REGNUM) / 2;
17902         int numregs = ARM_NUM_REGS (GET_MODE (x)) / 2;
17903         if (numregs == 1)
17904           asm_fprintf (stream, "{d%d}", regno);
17905         else
17906           asm_fprintf (stream, "{d%d-d%d}", regno, regno + numregs - 1);
17907       }
17908       return;
17909
17910     case 'd':
17911       /* CONST_TRUE_RTX means always -- that's the default.  */
17912       if (x == const_true_rtx)
17913         return;
17914
17915       if (!COMPARISON_P (x))
17916         {
17917           output_operand_lossage ("invalid operand for code '%c'", code);
17918           return;
17919         }
17920
17921       fputs (arm_condition_codes[get_arm_condition_code (x)],
17922              stream);
17923       return;
17924
17925     case 'D':
17926       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
17927          want to do that.  */
17928       if (x == const_true_rtx)
17929         {
17930           output_operand_lossage ("instruction never executed");
17931           return;
17932         }
17933       if (!COMPARISON_P (x))
17934         {
17935           output_operand_lossage ("invalid operand for code '%c'", code);
17936           return;
17937         }
17938
17939       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
17940                                  (get_arm_condition_code (x))],
17941              stream);
17942       return;
17943
17944     case 's':
17945     case 'V':
17946     case 'W':
17947     case 'X':
17948     case 'Y':
17949     case 'Z':
17950       /* Former Maverick support, removed after GCC-4.7.  */
17951       output_operand_lossage ("obsolete Maverick format code '%c'", code);
17952       return;
17953
17954     case 'U':
17955       if (!REG_P (x)
17956           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
17957           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
17958         /* Bad value for wCG register number.  */
17959         {
17960           output_operand_lossage ("invalid operand for code '%c'", code);
17961           return;
17962         }
17963
17964       else
17965         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
17966       return;
17967
17968       /* Print an iWMMXt control register name.  */
17969     case 'w':
17970       if (!CONST_INT_P (x)
17971           || INTVAL (x) < 0
17972           || INTVAL (x) >= 16)
17973         /* Bad value for wC register number.  */
17974         {
17975           output_operand_lossage ("invalid operand for code '%c'", code);
17976           return;
17977         }
17978
17979       else
17980         {
17981           static const char * wc_reg_names [16] =
17982             {
17983               "wCID",  "wCon",  "wCSSF", "wCASF",
17984               "wC4",   "wC5",   "wC6",   "wC7",
17985               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
17986               "wC12",  "wC13",  "wC14",  "wC15"
17987             };
17988
17989           fputs (wc_reg_names [INTVAL (x)], stream);
17990         }
17991       return;
17992
17993     /* Print the high single-precision register of a VFP double-precision
17994        register.  */
17995     case 'p':
17996       {
17997         int mode = GET_MODE (x);
17998         int regno;
17999
18000         if (GET_MODE_SIZE (mode) != 8 || !REG_P (x))
18001           {
18002             output_operand_lossage ("invalid operand for code '%c'", code);
18003             return;
18004           }
18005
18006         regno = REGNO (x);
18007         if (!VFP_REGNO_OK_FOR_DOUBLE (regno))
18008           {
18009             output_operand_lossage ("invalid operand for code '%c'", code);
18010             return;
18011           }
18012
18013         fprintf (stream, "s%d", regno - FIRST_VFP_REGNUM + 1);
18014       }
18015       return;
18016
18017     /* Print a VFP/Neon double precision or quad precision register name.  */
18018     case 'P':
18019     case 'q':
18020       {
18021         int mode = GET_MODE (x);
18022         int is_quad = (code == 'q');
18023         int regno;
18024
18025         if (GET_MODE_SIZE (mode) != (is_quad ? 16 : 8))
18026           {
18027             output_operand_lossage ("invalid operand for code '%c'", code);
18028             return;
18029           }
18030
18031         if (!REG_P (x)
18032             || !IS_VFP_REGNUM (REGNO (x)))
18033           {
18034             output_operand_lossage ("invalid operand for code '%c'", code);
18035             return;
18036           }
18037
18038         regno = REGNO (x);
18039         if ((is_quad && !NEON_REGNO_OK_FOR_QUAD (regno))
18040             || (!is_quad && !VFP_REGNO_OK_FOR_DOUBLE (regno)))
18041           {
18042             output_operand_lossage ("invalid operand for code '%c'", code);
18043             return;
18044           }
18045
18046         fprintf (stream, "%c%d", is_quad ? 'q' : 'd',
18047           (regno - FIRST_VFP_REGNUM) >> (is_quad ? 2 : 1));
18048       }
18049       return;
18050
18051     /* These two codes print the low/high doubleword register of a Neon quad
18052        register, respectively.  For pair-structure types, can also print
18053        low/high quadword registers.  */
18054     case 'e':
18055     case 'f':
18056       {
18057         int mode = GET_MODE (x);
18058         int regno;
18059
18060         if ((GET_MODE_SIZE (mode) != 16
18061              && GET_MODE_SIZE (mode) != 32) || !REG_P (x))
18062           {
18063             output_operand_lossage ("invalid operand for code '%c'", code);
18064             return;
18065           }
18066
18067         regno = REGNO (x);
18068         if (!NEON_REGNO_OK_FOR_QUAD (regno))
18069           {
18070             output_operand_lossage ("invalid operand for code '%c'", code);
18071             return;
18072           }
18073
18074         if (GET_MODE_SIZE (mode) == 16)
18075           fprintf (stream, "d%d", ((regno - FIRST_VFP_REGNUM) >> 1)
18076                                   + (code == 'f' ? 1 : 0));
18077         else
18078           fprintf (stream, "q%d", ((regno - FIRST_VFP_REGNUM) >> 2)
18079                                   + (code == 'f' ? 1 : 0));
18080       }
18081       return;
18082
18083     /* Print a VFPv3 floating-point constant, represented as an integer
18084        index.  */
18085     case 'G':
18086       {
18087         int index = vfp3_const_double_index (x);
18088         gcc_assert (index != -1);
18089         fprintf (stream, "%d", index);
18090       }
18091       return;
18092
18093     /* Print bits representing opcode features for Neon.
18094
18095        Bit 0 is 1 for signed, 0 for unsigned.  Floats count as signed
18096        and polynomials as unsigned.
18097
18098        Bit 1 is 1 for floats and polynomials, 0 for ordinary integers.
18099
18100        Bit 2 is 1 for rounding functions, 0 otherwise.  */
18101
18102     /* Identify the type as 's', 'u', 'p' or 'f'.  */
18103     case 'T':
18104       {
18105         HOST_WIDE_INT bits = INTVAL (x);
18106         fputc ("uspf"[bits & 3], stream);
18107       }
18108       return;
18109
18110     /* Likewise, but signed and unsigned integers are both 'i'.  */
18111     case 'F':
18112       {
18113         HOST_WIDE_INT bits = INTVAL (x);
18114         fputc ("iipf"[bits & 3], stream);
18115       }
18116       return;
18117
18118     /* As for 'T', but emit 'u' instead of 'p'.  */
18119     case 't':
18120       {
18121         HOST_WIDE_INT bits = INTVAL (x);
18122         fputc ("usuf"[bits & 3], stream);
18123       }
18124       return;
18125
18126     /* Bit 2: rounding (vs none).  */
18127     case 'O':
18128       {
18129         HOST_WIDE_INT bits = INTVAL (x);
18130         fputs ((bits & 4) != 0 ? "r" : "", stream);
18131       }
18132       return;
18133
18134     /* Memory operand for vld1/vst1 instruction.  */
18135     case 'A':
18136       {
18137         rtx addr;
18138         bool postinc = FALSE;
18139         unsigned align, memsize, align_bits;
18140
18141         gcc_assert (MEM_P (x));
18142         addr = XEXP (x, 0);
18143         if (GET_CODE (addr) == POST_INC)
18144           {
18145             postinc = 1;
18146             addr = XEXP (addr, 0);
18147           }
18148         asm_fprintf (stream, "[%r", REGNO (addr));
18149
18150         /* We know the alignment of this access, so we can emit a hint in the
18151            instruction (for some alignments) as an aid to the memory subsystem
18152            of the target.  */
18153         align = MEM_ALIGN (x) >> 3;
18154         memsize = MEM_SIZE (x);
18155
18156         /* Only certain alignment specifiers are supported by the hardware.  */
18157         if (memsize == 32 && (align % 32) == 0)
18158           align_bits = 256;
18159         else if ((memsize == 16 || memsize == 32) && (align % 16) == 0)
18160           align_bits = 128;
18161         else if (memsize >= 8 && (align % 8) == 0)
18162           align_bits = 64;
18163         else
18164           align_bits = 0;
18165
18166         if (align_bits != 0)
18167           asm_fprintf (stream, ":%d", align_bits);
18168
18169         asm_fprintf (stream, "]");
18170
18171         if (postinc)
18172           fputs("!", stream);
18173       }
18174       return;
18175
18176     case 'C':
18177       {
18178         rtx addr;
18179
18180         gcc_assert (MEM_P (x));
18181         addr = XEXP (x, 0);
18182         gcc_assert (REG_P (addr));
18183         asm_fprintf (stream, "[%r]", REGNO (addr));
18184       }
18185       return;
18186
18187     /* Translate an S register number into a D register number and element index.  */
18188     case 'y':
18189       {
18190         int mode = GET_MODE (x);
18191         int regno;
18192
18193         if (GET_MODE_SIZE (mode) != 4 || !REG_P (x))
18194           {
18195             output_operand_lossage ("invalid operand for code '%c'", code);
18196             return;
18197           }
18198
18199         regno = REGNO (x);
18200         if (!VFP_REGNO_OK_FOR_SINGLE (regno))
18201           {
18202             output_operand_lossage ("invalid operand for code '%c'", code);
18203             return;
18204           }
18205
18206         regno = regno - FIRST_VFP_REGNUM;
18207         fprintf (stream, "d%d[%d]", regno / 2, regno % 2);
18208       }
18209       return;
18210
18211     case 'v':
18212         gcc_assert (CONST_DOUBLE_P (x));
18213         fprintf (stream, "#%d", vfp3_const_double_for_fract_bits (x));
18214         return;
18215
18216     /* Register specifier for vld1.16/vst1.16.  Translate the S register
18217        number into a D register number and element index.  */
18218     case 'z':
18219       {
18220         int mode = GET_MODE (x);
18221         int regno;
18222
18223         if (GET_MODE_SIZE (mode) != 2 || !REG_P (x))
18224           {
18225             output_operand_lossage ("invalid operand for code '%c'", code);
18226             return;
18227           }
18228
18229         regno = REGNO (x);
18230         if (!VFP_REGNO_OK_FOR_SINGLE (regno))
18231           {
18232             output_operand_lossage ("invalid operand for code '%c'", code);
18233             return;
18234           }
18235
18236         regno = regno - FIRST_VFP_REGNUM;
18237         fprintf (stream, "d%d[%d]", regno/2, ((regno % 2) ? 2 : 0));
18238       }
18239       return;
18240
18241     default:
18242       if (x == 0)
18243         {
18244           output_operand_lossage ("missing operand");
18245           return;
18246         }
18247
18248       switch (GET_CODE (x))
18249         {
18250         case REG:
18251           asm_fprintf (stream, "%r", REGNO (x));
18252           break;
18253
18254         case MEM:
18255           output_memory_reference_mode = GET_MODE (x);
18256           output_address (XEXP (x, 0));
18257           break;
18258
18259         case CONST_DOUBLE:
18260           if (TARGET_NEON)
18261             {
18262               char fpstr[20];
18263               real_to_decimal (fpstr, CONST_DOUBLE_REAL_VALUE (x),
18264                                sizeof (fpstr), 0, 1);
18265               fprintf (stream, "#%s", fpstr);
18266             }
18267           else
18268             fprintf (stream, "#%s", fp_immediate_constant (x));
18269           break;
18270
18271         default:
18272           gcc_assert (GET_CODE (x) != NEG);
18273           fputc ('#', stream);
18274           if (GET_CODE (x) == HIGH)
18275             {
18276               fputs (":lower16:", stream);
18277               x = XEXP (x, 0);
18278             }
18279
18280           output_addr_const (stream, x);
18281           break;
18282         }
18283     }
18284 }
18285 \f
18286 /* Target hook for printing a memory address.  */
18287 static void
18288 arm_print_operand_address (FILE *stream, rtx x)
18289 {
18290   if (TARGET_32BIT)
18291     {
18292       int is_minus = GET_CODE (x) == MINUS;
18293
18294       if (REG_P (x))
18295         asm_fprintf (stream, "[%r]", REGNO (x));
18296       else if (GET_CODE (x) == PLUS || is_minus)
18297         {
18298           rtx base = XEXP (x, 0);
18299           rtx index = XEXP (x, 1);
18300           HOST_WIDE_INT offset = 0;
18301           if (!REG_P (base)
18302               || (REG_P (index) && REGNO (index) == SP_REGNUM))
18303             {
18304               /* Ensure that BASE is a register.  */
18305               /* (one of them must be).  */
18306               /* Also ensure the SP is not used as in index register.  */
18307               rtx temp = base;
18308               base = index;
18309               index = temp;
18310             }
18311           switch (GET_CODE (index))
18312             {
18313             case CONST_INT:
18314               offset = INTVAL (index);
18315               if (is_minus)
18316                 offset = -offset;
18317               asm_fprintf (stream, "[%r, #%wd]",
18318                            REGNO (base), offset);
18319               break;
18320
18321             case REG:
18322               asm_fprintf (stream, "[%r, %s%r]",
18323                            REGNO (base), is_minus ? "-" : "",
18324                            REGNO (index));
18325               break;
18326
18327             case MULT:
18328             case ASHIFTRT:
18329             case LSHIFTRT:
18330             case ASHIFT:
18331             case ROTATERT:
18332               {
18333                 asm_fprintf (stream, "[%r, %s%r",
18334                              REGNO (base), is_minus ? "-" : "",
18335                              REGNO (XEXP (index, 0)));
18336                 arm_print_operand (stream, index, 'S');
18337                 fputs ("]", stream);
18338                 break;
18339               }
18340
18341             default:
18342               gcc_unreachable ();
18343             }
18344         }
18345       else if (GET_CODE (x) == PRE_INC || GET_CODE (x) == POST_INC
18346                || GET_CODE (x) == PRE_DEC || GET_CODE (x) == POST_DEC)
18347         {
18348           extern enum machine_mode output_memory_reference_mode;
18349
18350           gcc_assert (REG_P (XEXP (x, 0)));
18351
18352           if (GET_CODE (x) == PRE_DEC || GET_CODE (x) == PRE_INC)
18353             asm_fprintf (stream, "[%r, #%s%d]!",
18354                          REGNO (XEXP (x, 0)),
18355                          GET_CODE (x) == PRE_DEC ? "-" : "",
18356                          GET_MODE_SIZE (output_memory_reference_mode));
18357           else
18358             asm_fprintf (stream, "[%r], #%s%d",
18359                          REGNO (XEXP (x, 0)),
18360                          GET_CODE (x) == POST_DEC ? "-" : "",
18361                          GET_MODE_SIZE (output_memory_reference_mode));
18362         }
18363       else if (GET_CODE (x) == PRE_MODIFY)
18364         {
18365           asm_fprintf (stream, "[%r, ", REGNO (XEXP (x, 0)));
18366           if (CONST_INT_P (XEXP (XEXP (x, 1), 1)))
18367             asm_fprintf (stream, "#%wd]!",
18368                          INTVAL (XEXP (XEXP (x, 1), 1)));
18369           else
18370             asm_fprintf (stream, "%r]!",
18371                          REGNO (XEXP (XEXP (x, 1), 1)));
18372         }
18373       else if (GET_CODE (x) == POST_MODIFY)
18374         {
18375           asm_fprintf (stream, "[%r], ", REGNO (XEXP (x, 0)));
18376           if (CONST_INT_P (XEXP (XEXP (x, 1), 1)))
18377             asm_fprintf (stream, "#%wd",
18378                          INTVAL (XEXP (XEXP (x, 1), 1)));
18379           else
18380             asm_fprintf (stream, "%r",
18381                          REGNO (XEXP (XEXP (x, 1), 1)));
18382         }
18383       else output_addr_const (stream, x);
18384     }
18385   else
18386     {
18387       if (REG_P (x))
18388         asm_fprintf (stream, "[%r]", REGNO (x));
18389       else if (GET_CODE (x) == POST_INC)
18390         asm_fprintf (stream, "%r!", REGNO (XEXP (x, 0)));
18391       else if (GET_CODE (x) == PLUS)
18392         {
18393           gcc_assert (REG_P (XEXP (x, 0)));
18394           if (CONST_INT_P (XEXP (x, 1)))
18395             asm_fprintf (stream, "[%r, #%wd]",
18396                          REGNO (XEXP (x, 0)),
18397                          INTVAL (XEXP (x, 1)));
18398           else
18399             asm_fprintf (stream, "[%r, %r]",
18400                          REGNO (XEXP (x, 0)),
18401                          REGNO (XEXP (x, 1)));
18402         }
18403       else
18404         output_addr_const (stream, x);
18405     }
18406 }
18407 \f
18408 /* Target hook for indicating whether a punctuation character for
18409    TARGET_PRINT_OPERAND is valid.  */
18410 static bool
18411 arm_print_operand_punct_valid_p (unsigned char code)
18412 {
18413   return (code == '@' || code == '|' || code == '.'
18414           || code == '(' || code == ')' || code == '#'
18415           || (TARGET_32BIT && (code == '?'))
18416           || (TARGET_THUMB2 && (code == '!'))
18417           || (TARGET_THUMB && (code == '_')));
18418 }
18419 \f
18420 /* Target hook for assembling integer objects.  The ARM version needs to
18421    handle word-sized values specially.  */
18422 static bool
18423 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
18424 {
18425   enum machine_mode mode;
18426
18427   if (size == UNITS_PER_WORD && aligned_p)
18428     {
18429       fputs ("\t.word\t", asm_out_file);
18430       output_addr_const (asm_out_file, x);
18431
18432       /* Mark symbols as position independent.  We only do this in the
18433          .text segment, not in the .data segment.  */
18434       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
18435           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
18436         {
18437           /* See legitimize_pic_address for an explanation of the
18438              TARGET_VXWORKS_RTP check.  */
18439           if (TARGET_VXWORKS_RTP
18440               || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
18441             fputs ("(GOT)", asm_out_file);
18442           else
18443             fputs ("(GOTOFF)", asm_out_file);
18444         }
18445       fputc ('\n', asm_out_file);
18446       return true;
18447     }
18448
18449   mode = GET_MODE (x);
18450
18451   if (arm_vector_mode_supported_p (mode))
18452     {
18453       int i, units;
18454
18455       gcc_assert (GET_CODE (x) == CONST_VECTOR);
18456
18457       units = CONST_VECTOR_NUNITS (x);
18458       size = GET_MODE_SIZE (GET_MODE_INNER (mode));
18459
18460       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
18461         for (i = 0; i < units; i++)
18462           {
18463             rtx elt = CONST_VECTOR_ELT (x, i);
18464             assemble_integer
18465               (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
18466           }
18467       else
18468         for (i = 0; i < units; i++)
18469           {
18470             rtx elt = CONST_VECTOR_ELT (x, i);
18471             REAL_VALUE_TYPE rval;
18472
18473             REAL_VALUE_FROM_CONST_DOUBLE (rval, elt);
18474
18475             assemble_real
18476               (rval, GET_MODE_INNER (mode),
18477               i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT);
18478           }
18479
18480       return true;
18481     }
18482
18483   return default_assemble_integer (x, size, aligned_p);
18484 }
18485
18486 static void
18487 arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
18488 {
18489   section *s;
18490
18491   if (!TARGET_AAPCS_BASED)
18492     {
18493       (is_ctor ?
18494        default_named_section_asm_out_constructor
18495        : default_named_section_asm_out_destructor) (symbol, priority);
18496       return;
18497     }
18498
18499   /* Put these in the .init_array section, using a special relocation.  */
18500   if (priority != DEFAULT_INIT_PRIORITY)
18501     {
18502       char buf[18];
18503       sprintf (buf, "%s.%.5u",
18504                is_ctor ? ".init_array" : ".fini_array",
18505                priority);
18506       s = get_section (buf, SECTION_WRITE, NULL_TREE);
18507     }
18508   else if (is_ctor)
18509     s = ctors_section;
18510   else
18511     s = dtors_section;
18512
18513   switch_to_section (s);
18514   assemble_align (POINTER_SIZE);
18515   fputs ("\t.word\t", asm_out_file);
18516   output_addr_const (asm_out_file, symbol);
18517   fputs ("(target1)\n", asm_out_file);
18518 }
18519
18520 /* Add a function to the list of static constructors.  */
18521
18522 static void
18523 arm_elf_asm_constructor (rtx symbol, int priority)
18524 {
18525   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
18526 }
18527
18528 /* Add a function to the list of static destructors.  */
18529
18530 static void
18531 arm_elf_asm_destructor (rtx symbol, int priority)
18532 {
18533   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
18534 }
18535 \f
18536 /* A finite state machine takes care of noticing whether or not instructions
18537    can be conditionally executed, and thus decrease execution time and code
18538    size by deleting branch instructions.  The fsm is controlled by
18539    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
18540
18541 /* The state of the fsm controlling condition codes are:
18542    0: normal, do nothing special
18543    1: make ASM_OUTPUT_OPCODE not output this instruction
18544    2: make ASM_OUTPUT_OPCODE not output this instruction
18545    3: make instructions conditional
18546    4: make instructions conditional
18547
18548    State transitions (state->state by whom under condition):
18549    0 -> 1 final_prescan_insn if the `target' is a label
18550    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
18551    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
18552    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
18553    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
18554           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
18555    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
18556           (the target insn is arm_target_insn).
18557
18558    If the jump clobbers the conditions then we use states 2 and 4.
18559
18560    A similar thing can be done with conditional return insns.
18561
18562    XXX In case the `target' is an unconditional branch, this conditionalising
18563    of the instructions always reduces code size, but not always execution
18564    time.  But then, I want to reduce the code size to somewhere near what
18565    /bin/cc produces.  */
18566
18567 /* In addition to this, state is maintained for Thumb-2 COND_EXEC
18568    instructions.  When a COND_EXEC instruction is seen the subsequent
18569    instructions are scanned so that multiple conditional instructions can be
18570    combined into a single IT block.  arm_condexec_count and arm_condexec_mask
18571    specify the length and true/false mask for the IT block.  These will be
18572    decremented/zeroed by arm_asm_output_opcode as the insns are output.  */
18573
18574 /* Returns the index of the ARM condition code string in
18575    `arm_condition_codes', or ARM_NV if the comparison is invalid.
18576    COMPARISON should be an rtx like `(eq (...) (...))'.  */
18577
18578 enum arm_cond_code
18579 maybe_get_arm_condition_code (rtx comparison)
18580 {
18581   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
18582   enum arm_cond_code code;
18583   enum rtx_code comp_code = GET_CODE (comparison);
18584
18585   if (GET_MODE_CLASS (mode) != MODE_CC)
18586     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
18587                            XEXP (comparison, 1));
18588
18589   switch (mode)
18590     {
18591     case CC_DNEmode: code = ARM_NE; goto dominance;
18592     case CC_DEQmode: code = ARM_EQ; goto dominance;
18593     case CC_DGEmode: code = ARM_GE; goto dominance;
18594     case CC_DGTmode: code = ARM_GT; goto dominance;
18595     case CC_DLEmode: code = ARM_LE; goto dominance;
18596     case CC_DLTmode: code = ARM_LT; goto dominance;
18597     case CC_DGEUmode: code = ARM_CS; goto dominance;
18598     case CC_DGTUmode: code = ARM_HI; goto dominance;
18599     case CC_DLEUmode: code = ARM_LS; goto dominance;
18600     case CC_DLTUmode: code = ARM_CC;
18601
18602     dominance:
18603       if (comp_code == EQ)
18604         return ARM_INVERSE_CONDITION_CODE (code);
18605       if (comp_code == NE)
18606         return code;
18607       return ARM_NV;
18608
18609     case CC_NOOVmode:
18610       switch (comp_code)
18611         {
18612         case NE: return ARM_NE;
18613         case EQ: return ARM_EQ;
18614         case GE: return ARM_PL;
18615         case LT: return ARM_MI;
18616         default: return ARM_NV;
18617         }
18618
18619     case CC_Zmode:
18620       switch (comp_code)
18621         {
18622         case NE: return ARM_NE;
18623         case EQ: return ARM_EQ;
18624         default: return ARM_NV;
18625         }
18626
18627     case CC_Nmode:
18628       switch (comp_code)
18629         {
18630         case NE: return ARM_MI;
18631         case EQ: return ARM_PL;
18632         default: return ARM_NV;
18633         }
18634
18635     case CCFPEmode:
18636     case CCFPmode:
18637       /* We can handle all cases except UNEQ and LTGT.  */
18638       switch (comp_code)
18639         {
18640         case GE: return ARM_GE;
18641         case GT: return ARM_GT;
18642         case LE: return ARM_LS;
18643         case LT: return ARM_MI;
18644         case NE: return ARM_NE;
18645         case EQ: return ARM_EQ;
18646         case ORDERED: return ARM_VC;
18647         case UNORDERED: return ARM_VS;
18648         case UNLT: return ARM_LT;
18649         case UNLE: return ARM_LE;
18650         case UNGT: return ARM_HI;
18651         case UNGE: return ARM_PL;
18652           /* UNEQ and LTGT do not have a representation.  */
18653         case UNEQ: /* Fall through.  */
18654         case LTGT: /* Fall through.  */
18655         default: return ARM_NV;
18656         }
18657
18658     case CC_SWPmode:
18659       switch (comp_code)
18660         {
18661         case NE: return ARM_NE;
18662         case EQ: return ARM_EQ;
18663         case GE: return ARM_LE;
18664         case GT: return ARM_LT;
18665         case LE: return ARM_GE;
18666         case LT: return ARM_GT;
18667         case GEU: return ARM_LS;
18668         case GTU: return ARM_CC;
18669         case LEU: return ARM_CS;
18670         case LTU: return ARM_HI;
18671         default: return ARM_NV;
18672         }
18673
18674     case CC_Cmode:
18675       switch (comp_code)
18676         {
18677         case LTU: return ARM_CS;
18678         case GEU: return ARM_CC;
18679         default: return ARM_NV;
18680         }
18681
18682     case CC_CZmode:
18683       switch (comp_code)
18684         {
18685         case NE: return ARM_NE;
18686         case EQ: return ARM_EQ;
18687         case GEU: return ARM_CS;
18688         case GTU: return ARM_HI;
18689         case LEU: return ARM_LS;
18690         case LTU: return ARM_CC;
18691         default: return ARM_NV;
18692         }
18693
18694     case CC_NCVmode:
18695       switch (comp_code)
18696         {
18697         case GE: return ARM_GE;
18698         case LT: return ARM_LT;
18699         case GEU: return ARM_CS;
18700         case LTU: return ARM_CC;
18701         default: return ARM_NV;
18702         }
18703
18704     case CCmode:
18705       switch (comp_code)
18706         {
18707         case NE: return ARM_NE;
18708         case EQ: return ARM_EQ;
18709         case GE: return ARM_GE;
18710         case GT: return ARM_GT;
18711         case LE: return ARM_LE;
18712         case LT: return ARM_LT;
18713         case GEU: return ARM_CS;
18714         case GTU: return ARM_HI;
18715         case LEU: return ARM_LS;
18716         case LTU: return ARM_CC;
18717         default: return ARM_NV;
18718         }
18719
18720     default: gcc_unreachable ();
18721     }
18722 }
18723
18724 /* Like maybe_get_arm_condition_code, but never return ARM_NV.  */
18725 static enum arm_cond_code
18726 get_arm_condition_code (rtx comparison)
18727 {
18728   enum arm_cond_code code = maybe_get_arm_condition_code (comparison);
18729   gcc_assert (code != ARM_NV);
18730   return code;
18731 }
18732
18733 /* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
18734    instructions.  */
18735 void
18736 thumb2_final_prescan_insn (rtx insn)
18737 {
18738   rtx first_insn = insn;
18739   rtx body = PATTERN (insn);
18740   rtx predicate;
18741   enum arm_cond_code code;
18742   int n;
18743   int mask;
18744
18745   /* Remove the previous insn from the count of insns to be output.  */
18746   if (arm_condexec_count)
18747       arm_condexec_count--;
18748
18749   /* Nothing to do if we are already inside a conditional block.  */
18750   if (arm_condexec_count)
18751     return;
18752
18753   if (GET_CODE (body) != COND_EXEC)
18754     return;
18755
18756   /* Conditional jumps are implemented directly.  */
18757   if (JUMP_P (insn))
18758     return;
18759
18760   predicate = COND_EXEC_TEST (body);
18761   arm_current_cc = get_arm_condition_code (predicate);
18762
18763   n = get_attr_ce_count (insn);
18764   arm_condexec_count = 1;
18765   arm_condexec_mask = (1 << n) - 1;
18766   arm_condexec_masklen = n;
18767   /* See if subsequent instructions can be combined into the same block.  */
18768   for (;;)
18769     {
18770       insn = next_nonnote_insn (insn);
18771
18772       /* Jumping into the middle of an IT block is illegal, so a label or
18773          barrier terminates the block.  */
18774       if (!NONJUMP_INSN_P (insn) && !JUMP_P (insn))
18775         break;
18776
18777       body = PATTERN (insn);
18778       /* USE and CLOBBER aren't really insns, so just skip them.  */
18779       if (GET_CODE (body) == USE
18780           || GET_CODE (body) == CLOBBER)
18781         continue;
18782
18783       /* ??? Recognize conditional jumps, and combine them with IT blocks.  */
18784       if (GET_CODE (body) != COND_EXEC)
18785         break;
18786       /* Allow up to 4 conditionally executed instructions in a block.  */
18787       n = get_attr_ce_count (insn);
18788       if (arm_condexec_masklen + n > 4)
18789         break;
18790
18791       predicate = COND_EXEC_TEST (body);
18792       code = get_arm_condition_code (predicate);
18793       mask = (1 << n) - 1;
18794       if (arm_current_cc == code)
18795         arm_condexec_mask |= (mask << arm_condexec_masklen);
18796       else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
18797         break;
18798
18799       arm_condexec_count++;
18800       arm_condexec_masklen += n;
18801
18802       /* A jump must be the last instruction in a conditional block.  */
18803       if (JUMP_P (insn))
18804         break;
18805     }
18806   /* Restore recog_data (getting the attributes of other insns can
18807      destroy this array, but final.c assumes that it remains intact
18808      across this call).  */
18809   extract_constrain_insn_cached (first_insn);
18810 }
18811
18812 void
18813 arm_final_prescan_insn (rtx insn)
18814 {
18815   /* BODY will hold the body of INSN.  */
18816   rtx body = PATTERN (insn);
18817
18818   /* This will be 1 if trying to repeat the trick, and things need to be
18819      reversed if it appears to fail.  */
18820   int reverse = 0;
18821
18822   /* If we start with a return insn, we only succeed if we find another one.  */
18823   int seeking_return = 0;
18824   enum rtx_code return_code = UNKNOWN;
18825
18826   /* START_INSN will hold the insn from where we start looking.  This is the
18827      first insn after the following code_label if REVERSE is true.  */
18828   rtx start_insn = insn;
18829
18830   /* If in state 4, check if the target branch is reached, in order to
18831      change back to state 0.  */
18832   if (arm_ccfsm_state == 4)
18833     {
18834       if (insn == arm_target_insn)
18835         {
18836           arm_target_insn = NULL;
18837           arm_ccfsm_state = 0;
18838         }
18839       return;
18840     }
18841
18842   /* If in state 3, it is possible to repeat the trick, if this insn is an
18843      unconditional branch to a label, and immediately following this branch
18844      is the previous target label which is only used once, and the label this
18845      branch jumps to is not too far off.  */
18846   if (arm_ccfsm_state == 3)
18847     {
18848       if (simplejump_p (insn))
18849         {
18850           start_insn = next_nonnote_insn (start_insn);
18851           if (BARRIER_P (start_insn))
18852             {
18853               /* XXX Isn't this always a barrier?  */
18854               start_insn = next_nonnote_insn (start_insn);
18855             }
18856           if (LABEL_P (start_insn)
18857               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
18858               && LABEL_NUSES (start_insn) == 1)
18859             reverse = TRUE;
18860           else
18861             return;
18862         }
18863       else if (ANY_RETURN_P (body))
18864         {
18865           start_insn = next_nonnote_insn (start_insn);
18866           if (BARRIER_P (start_insn))
18867             start_insn = next_nonnote_insn (start_insn);
18868           if (LABEL_P (start_insn)
18869               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
18870               && LABEL_NUSES (start_insn) == 1)
18871             {
18872               reverse = TRUE;
18873               seeking_return = 1;
18874               return_code = GET_CODE (body);
18875             }
18876           else
18877             return;
18878         }
18879       else
18880         return;
18881     }
18882
18883   gcc_assert (!arm_ccfsm_state || reverse);
18884   if (!JUMP_P (insn))
18885     return;
18886
18887   /* This jump might be paralleled with a clobber of the condition codes
18888      the jump should always come first */
18889   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
18890     body = XVECEXP (body, 0, 0);
18891
18892   if (reverse
18893       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
18894           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
18895     {
18896       int insns_skipped;
18897       int fail = FALSE, succeed = FALSE;
18898       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
18899       int then_not_else = TRUE;
18900       rtx this_insn = start_insn, label = 0;
18901
18902       /* Register the insn jumped to.  */
18903       if (reverse)
18904         {
18905           if (!seeking_return)
18906             label = XEXP (SET_SRC (body), 0);
18907         }
18908       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
18909         label = XEXP (XEXP (SET_SRC (body), 1), 0);
18910       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
18911         {
18912           label = XEXP (XEXP (SET_SRC (body), 2), 0);
18913           then_not_else = FALSE;
18914         }
18915       else if (ANY_RETURN_P (XEXP (SET_SRC (body), 1)))
18916         {
18917           seeking_return = 1;
18918           return_code = GET_CODE (XEXP (SET_SRC (body), 1));
18919         }
18920       else if (ANY_RETURN_P (XEXP (SET_SRC (body), 2)))
18921         {
18922           seeking_return = 1;
18923           return_code = GET_CODE (XEXP (SET_SRC (body), 2));
18924           then_not_else = FALSE;
18925         }
18926       else
18927         gcc_unreachable ();
18928
18929       /* See how many insns this branch skips, and what kind of insns.  If all
18930          insns are okay, and the label or unconditional branch to the same
18931          label is not too far away, succeed.  */
18932       for (insns_skipped = 0;
18933            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
18934         {
18935           rtx scanbody;
18936
18937           this_insn = next_nonnote_insn (this_insn);
18938           if (!this_insn)
18939             break;
18940
18941           switch (GET_CODE (this_insn))
18942             {
18943             case CODE_LABEL:
18944               /* Succeed if it is the target label, otherwise fail since
18945                  control falls in from somewhere else.  */
18946               if (this_insn == label)
18947                 {
18948                   arm_ccfsm_state = 1;
18949                   succeed = TRUE;
18950                 }
18951               else
18952                 fail = TRUE;
18953               break;
18954
18955             case BARRIER:
18956               /* Succeed if the following insn is the target label.
18957                  Otherwise fail.
18958                  If return insns are used then the last insn in a function
18959                  will be a barrier.  */
18960               this_insn = next_nonnote_insn (this_insn);
18961               if (this_insn && this_insn == label)
18962                 {
18963                   arm_ccfsm_state = 1;
18964                   succeed = TRUE;
18965                 }
18966               else
18967                 fail = TRUE;
18968               break;
18969
18970             case CALL_INSN:
18971               /* The AAPCS says that conditional calls should not be
18972                  used since they make interworking inefficient (the
18973                  linker can't transform BL<cond> into BLX).  That's
18974                  only a problem if the machine has BLX.  */
18975               if (arm_arch5)
18976                 {
18977                   fail = TRUE;
18978                   break;
18979                 }
18980
18981               /* Succeed if the following insn is the target label, or
18982                  if the following two insns are a barrier and the
18983                  target label.  */
18984               this_insn = next_nonnote_insn (this_insn);
18985               if (this_insn && BARRIER_P (this_insn))
18986                 this_insn = next_nonnote_insn (this_insn);
18987
18988               if (this_insn && this_insn == label
18989                   && insns_skipped < max_insns_skipped)
18990                 {
18991                   arm_ccfsm_state = 1;
18992                   succeed = TRUE;
18993                 }
18994               else
18995                 fail = TRUE;
18996               break;
18997
18998             case JUMP_INSN:
18999               /* If this is an unconditional branch to the same label, succeed.
19000                  If it is to another label, do nothing.  If it is conditional,
19001                  fail.  */
19002               /* XXX Probably, the tests for SET and the PC are
19003                  unnecessary.  */
19004
19005               scanbody = PATTERN (this_insn);
19006               if (GET_CODE (scanbody) == SET
19007                   && GET_CODE (SET_DEST (scanbody)) == PC)
19008                 {
19009                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
19010                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
19011                     {
19012                       arm_ccfsm_state = 2;
19013                       succeed = TRUE;
19014                     }
19015                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
19016                     fail = TRUE;
19017                 }
19018               /* Fail if a conditional return is undesirable (e.g. on a
19019                  StrongARM), but still allow this if optimizing for size.  */
19020               else if (GET_CODE (scanbody) == return_code
19021                        && !use_return_insn (TRUE, NULL)
19022                        && !optimize_size)
19023                 fail = TRUE;
19024               else if (GET_CODE (scanbody) == return_code)
19025                 {
19026                   arm_ccfsm_state = 2;
19027                   succeed = TRUE;
19028                 }
19029               else if (GET_CODE (scanbody) == PARALLEL)
19030                 {
19031                   switch (get_attr_conds (this_insn))
19032                     {
19033                     case CONDS_NOCOND:
19034                       break;
19035                     default:
19036                       fail = TRUE;
19037                       break;
19038                     }
19039                 }
19040               else
19041                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
19042
19043               break;
19044
19045             case INSN:
19046               /* Instructions using or affecting the condition codes make it
19047                  fail.  */
19048               scanbody = PATTERN (this_insn);
19049               if (!(GET_CODE (scanbody) == SET
19050                     || GET_CODE (scanbody) == PARALLEL)
19051                   || get_attr_conds (this_insn) != CONDS_NOCOND)
19052                 fail = TRUE;
19053               break;
19054
19055             default:
19056               break;
19057             }
19058         }
19059       if (succeed)
19060         {
19061           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
19062             arm_target_label = CODE_LABEL_NUMBER (label);
19063           else
19064             {
19065               gcc_assert (seeking_return || arm_ccfsm_state == 2);
19066
19067               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
19068                 {
19069                   this_insn = next_nonnote_insn (this_insn);
19070                   gcc_assert (!this_insn
19071                               || (!BARRIER_P (this_insn)
19072                                   && !LABEL_P (this_insn)));
19073                 }
19074               if (!this_insn)
19075                 {
19076                   /* Oh, dear! we ran off the end.. give up.  */
19077                   extract_constrain_insn_cached (insn);
19078                   arm_ccfsm_state = 0;
19079                   arm_target_insn = NULL;
19080                   return;
19081                 }
19082               arm_target_insn = this_insn;
19083             }
19084
19085           /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
19086              what it was.  */
19087           if (!reverse)
19088             arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body), 0));
19089
19090           if (reverse || then_not_else)
19091             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
19092         }
19093
19094       /* Restore recog_data (getting the attributes of other insns can
19095          destroy this array, but final.c assumes that it remains intact
19096          across this call.  */
19097       extract_constrain_insn_cached (insn);
19098     }
19099 }
19100
19101 /* Output IT instructions.  */
19102 void
19103 thumb2_asm_output_opcode (FILE * stream)
19104 {
19105   char buff[5];
19106   int n;
19107
19108   if (arm_condexec_mask)
19109     {
19110       for (n = 0; n < arm_condexec_masklen; n++)
19111         buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
19112       buff[n] = 0;
19113       asm_fprintf(stream, "i%s\t%s\n\t", buff,
19114                   arm_condition_codes[arm_current_cc]);
19115       arm_condexec_mask = 0;
19116     }
19117 }
19118
19119 /* Returns true if REGNO is a valid register
19120    for holding a quantity of type MODE.  */
19121 int
19122 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
19123 {
19124   if (GET_MODE_CLASS (mode) == MODE_CC)
19125     return (regno == CC_REGNUM
19126             || (TARGET_HARD_FLOAT && TARGET_VFP
19127                 && regno == VFPCC_REGNUM));
19128
19129   if (TARGET_THUMB1)
19130     /* For the Thumb we only allow values bigger than SImode in
19131        registers 0 - 6, so that there is always a second low
19132        register available to hold the upper part of the value.
19133        We probably we ought to ensure that the register is the
19134        start of an even numbered register pair.  */
19135     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
19136
19137   if (TARGET_HARD_FLOAT && TARGET_VFP
19138       && IS_VFP_REGNUM (regno))
19139     {
19140       if (mode == SFmode || mode == SImode)
19141         return VFP_REGNO_OK_FOR_SINGLE (regno);
19142
19143       if (mode == DFmode)
19144         return VFP_REGNO_OK_FOR_DOUBLE (regno);
19145
19146       /* VFP registers can hold HFmode values, but there is no point in
19147          putting them there unless we have hardware conversion insns. */
19148       if (mode == HFmode)
19149         return TARGET_FP16 && VFP_REGNO_OK_FOR_SINGLE (regno);
19150
19151       if (TARGET_NEON)
19152         return (VALID_NEON_DREG_MODE (mode) && VFP_REGNO_OK_FOR_DOUBLE (regno))
19153                || (VALID_NEON_QREG_MODE (mode)
19154                    && NEON_REGNO_OK_FOR_QUAD (regno))
19155                || (mode == TImode && NEON_REGNO_OK_FOR_NREGS (regno, 2))
19156                || (mode == EImode && NEON_REGNO_OK_FOR_NREGS (regno, 3))
19157                || (mode == OImode && NEON_REGNO_OK_FOR_NREGS (regno, 4))
19158                || (mode == CImode && NEON_REGNO_OK_FOR_NREGS (regno, 6))
19159                || (mode == XImode && NEON_REGNO_OK_FOR_NREGS (regno, 8));
19160
19161       return FALSE;
19162     }
19163
19164   if (TARGET_REALLY_IWMMXT)
19165     {
19166       if (IS_IWMMXT_GR_REGNUM (regno))
19167         return mode == SImode;
19168
19169       if (IS_IWMMXT_REGNUM (regno))
19170         return VALID_IWMMXT_REG_MODE (mode);
19171     }
19172
19173   /* We allow almost any value to be stored in the general registers.
19174      Restrict doubleword quantities to even register pairs so that we can
19175      use ldrd.  Do not allow very large Neon structure opaque modes in
19176      general registers; they would use too many.  */
19177   if (regno <= LAST_ARM_REGNUM)
19178     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0)
19179       && ARM_NUM_REGS (mode) <= 4;
19180
19181   if (regno == FRAME_POINTER_REGNUM
19182       || regno == ARG_POINTER_REGNUM)
19183     /* We only allow integers in the fake hard registers.  */
19184     return GET_MODE_CLASS (mode) == MODE_INT;
19185
19186   return FALSE;
19187 }
19188
19189 /* Implement MODES_TIEABLE_P.  */
19190
19191 bool
19192 arm_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
19193 {
19194   if (GET_MODE_CLASS (mode1) == GET_MODE_CLASS (mode2))
19195     return true;
19196
19197   /* We specifically want to allow elements of "structure" modes to
19198      be tieable to the structure.  This more general condition allows
19199      other rarer situations too.  */
19200   if (TARGET_NEON
19201       && (VALID_NEON_DREG_MODE (mode1)
19202           || VALID_NEON_QREG_MODE (mode1)
19203           || VALID_NEON_STRUCT_MODE (mode1))
19204       && (VALID_NEON_DREG_MODE (mode2)
19205           || VALID_NEON_QREG_MODE (mode2)
19206           || VALID_NEON_STRUCT_MODE (mode2)))
19207     return true;
19208
19209   return false;
19210 }
19211
19212 /* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
19213    not used in arm mode.  */
19214
19215 enum reg_class
19216 arm_regno_class (int regno)
19217 {
19218   if (TARGET_THUMB1)
19219     {
19220       if (regno == STACK_POINTER_REGNUM)
19221         return STACK_REG;
19222       if (regno == CC_REGNUM)
19223         return CC_REG;
19224       if (regno < 8)
19225         return LO_REGS;
19226       return HI_REGS;
19227     }
19228
19229   if (TARGET_THUMB2 && regno < 8)
19230     return LO_REGS;
19231
19232   if (   regno <= LAST_ARM_REGNUM
19233       || regno == FRAME_POINTER_REGNUM
19234       || regno == ARG_POINTER_REGNUM)
19235     return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
19236
19237   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
19238     return TARGET_THUMB2 ? CC_REG : NO_REGS;
19239
19240   if (IS_VFP_REGNUM (regno))
19241     {
19242       if (regno <= D7_VFP_REGNUM)
19243         return VFP_D0_D7_REGS;
19244       else if (regno <= LAST_LO_VFP_REGNUM)
19245         return VFP_LO_REGS;
19246       else
19247         return VFP_HI_REGS;
19248     }
19249
19250   if (IS_IWMMXT_REGNUM (regno))
19251     return IWMMXT_REGS;
19252
19253   if (IS_IWMMXT_GR_REGNUM (regno))
19254     return IWMMXT_GR_REGS;
19255
19256   return NO_REGS;
19257 }
19258
19259 /* Handle a special case when computing the offset
19260    of an argument from the frame pointer.  */
19261 int
19262 arm_debugger_arg_offset (int value, rtx addr)
19263 {
19264   rtx insn;
19265
19266   /* We are only interested if dbxout_parms() failed to compute the offset.  */
19267   if (value != 0)
19268     return 0;
19269
19270   /* We can only cope with the case where the address is held in a register.  */
19271   if (!REG_P (addr))
19272     return 0;
19273
19274   /* If we are using the frame pointer to point at the argument, then
19275      an offset of 0 is correct.  */
19276   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
19277     return 0;
19278
19279   /* If we are using the stack pointer to point at the
19280      argument, then an offset of 0 is correct.  */
19281   /* ??? Check this is consistent with thumb2 frame layout.  */
19282   if ((TARGET_THUMB || !frame_pointer_needed)
19283       && REGNO (addr) == SP_REGNUM)
19284     return 0;
19285
19286   /* Oh dear.  The argument is pointed to by a register rather
19287      than being held in a register, or being stored at a known
19288      offset from the frame pointer.  Since GDB only understands
19289      those two kinds of argument we must translate the address
19290      held in the register into an offset from the frame pointer.
19291      We do this by searching through the insns for the function
19292      looking to see where this register gets its value.  If the
19293      register is initialized from the frame pointer plus an offset
19294      then we are in luck and we can continue, otherwise we give up.
19295
19296      This code is exercised by producing debugging information
19297      for a function with arguments like this:
19298
19299            double func (double a, double b, int c, double d) {return d;}
19300
19301      Without this code the stab for parameter 'd' will be set to
19302      an offset of 0 from the frame pointer, rather than 8.  */
19303
19304   /* The if() statement says:
19305
19306      If the insn is a normal instruction
19307      and if the insn is setting the value in a register
19308      and if the register being set is the register holding the address of the argument
19309      and if the address is computing by an addition
19310      that involves adding to a register
19311      which is the frame pointer
19312      a constant integer
19313
19314      then...  */
19315
19316   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
19317     {
19318       if (   NONJUMP_INSN_P (insn)
19319           && GET_CODE (PATTERN (insn)) == SET
19320           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
19321           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
19322           && REG_P (XEXP (XEXP (PATTERN (insn), 1), 0))
19323           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
19324           && CONST_INT_P (XEXP (XEXP (PATTERN (insn), 1), 1))
19325              )
19326         {
19327           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
19328
19329           break;
19330         }
19331     }
19332
19333   if (value == 0)
19334     {
19335       debug_rtx (addr);
19336       warning (0, "unable to compute real location of stacked parameter");
19337       value = 8; /* XXX magic hack */
19338     }
19339
19340   return value;
19341 }
19342 \f
19343 typedef enum {
19344   T_V8QI,
19345   T_V4HI,
19346   T_V2SI,
19347   T_V2SF,
19348   T_DI,
19349   T_V16QI,
19350   T_V8HI,
19351   T_V4SI,
19352   T_V4SF,
19353   T_V2DI,
19354   T_TI,
19355   T_EI,
19356   T_OI,
19357   T_MAX         /* Size of enum.  Keep last.  */
19358 } neon_builtin_type_mode;
19359
19360 #define TYPE_MODE_BIT(X) (1 << (X))
19361
19362 #define TB_DREG (TYPE_MODE_BIT (T_V8QI) | TYPE_MODE_BIT (T_V4HI)        \
19363                  | TYPE_MODE_BIT (T_V2SI) | TYPE_MODE_BIT (T_V2SF)      \
19364                  | TYPE_MODE_BIT (T_DI))
19365 #define TB_QREG (TYPE_MODE_BIT (T_V16QI) | TYPE_MODE_BIT (T_V8HI)       \
19366                  | TYPE_MODE_BIT (T_V4SI) | TYPE_MODE_BIT (T_V4SF)      \
19367                  | TYPE_MODE_BIT (T_V2DI) | TYPE_MODE_BIT (T_TI))
19368
19369 #define v8qi_UP  T_V8QI
19370 #define v4hi_UP  T_V4HI
19371 #define v2si_UP  T_V2SI
19372 #define v2sf_UP  T_V2SF
19373 #define di_UP    T_DI
19374 #define v16qi_UP T_V16QI
19375 #define v8hi_UP  T_V8HI
19376 #define v4si_UP  T_V4SI
19377 #define v4sf_UP  T_V4SF
19378 #define v2di_UP  T_V2DI
19379 #define ti_UP    T_TI
19380 #define ei_UP    T_EI
19381 #define oi_UP    T_OI
19382
19383 #define UP(X) X##_UP
19384
19385 typedef enum {
19386   NEON_BINOP,
19387   NEON_TERNOP,
19388   NEON_UNOP,
19389   NEON_GETLANE,
19390   NEON_SETLANE,
19391   NEON_CREATE,
19392   NEON_RINT,
19393   NEON_DUP,
19394   NEON_DUPLANE,
19395   NEON_COMBINE,
19396   NEON_SPLIT,
19397   NEON_LANEMUL,
19398   NEON_LANEMULL,
19399   NEON_LANEMULH,
19400   NEON_LANEMAC,
19401   NEON_SCALARMUL,
19402   NEON_SCALARMULL,
19403   NEON_SCALARMULH,
19404   NEON_SCALARMAC,
19405   NEON_CONVERT,
19406   NEON_FIXCONV,
19407   NEON_SELECT,
19408   NEON_RESULTPAIR,
19409   NEON_REINTERP,
19410   NEON_VTBL,
19411   NEON_VTBX,
19412   NEON_LOAD1,
19413   NEON_LOAD1LANE,
19414   NEON_STORE1,
19415   NEON_STORE1LANE,
19416   NEON_LOADSTRUCT,
19417   NEON_LOADSTRUCTLANE,
19418   NEON_STORESTRUCT,
19419   NEON_STORESTRUCTLANE,
19420   NEON_LOGICBINOP,
19421   NEON_SHIFTINSERT,
19422   NEON_SHIFTIMM,
19423   NEON_SHIFTACC
19424 } neon_itype;
19425
19426 typedef struct {
19427   const char *name;
19428   const neon_itype itype;
19429   const neon_builtin_type_mode mode;
19430   const enum insn_code code;
19431   unsigned int fcode;
19432 } neon_builtin_datum;
19433
19434 #define CF(N,X) CODE_FOR_neon_##N##X
19435
19436 #define VAR1(T, N, A) \
19437   {#N, NEON_##T, UP (A), CF (N, A), 0}
19438 #define VAR2(T, N, A, B) \
19439   VAR1 (T, N, A), \
19440   {#N, NEON_##T, UP (B), CF (N, B), 0}
19441 #define VAR3(T, N, A, B, C) \
19442   VAR2 (T, N, A, B), \
19443   {#N, NEON_##T, UP (C), CF (N, C), 0}
19444 #define VAR4(T, N, A, B, C, D) \
19445   VAR3 (T, N, A, B, C), \
19446   {#N, NEON_##T, UP (D), CF (N, D), 0}
19447 #define VAR5(T, N, A, B, C, D, E) \
19448   VAR4 (T, N, A, B, C, D), \
19449   {#N, NEON_##T, UP (E), CF (N, E), 0}
19450 #define VAR6(T, N, A, B, C, D, E, F) \
19451   VAR5 (T, N, A, B, C, D, E), \
19452   {#N, NEON_##T, UP (F), CF (N, F), 0}
19453 #define VAR7(T, N, A, B, C, D, E, F, G) \
19454   VAR6 (T, N, A, B, C, D, E, F), \
19455   {#N, NEON_##T, UP (G), CF (N, G), 0}
19456 #define VAR8(T, N, A, B, C, D, E, F, G, H) \
19457   VAR7 (T, N, A, B, C, D, E, F, G), \
19458   {#N, NEON_##T, UP (H), CF (N, H), 0}
19459 #define VAR9(T, N, A, B, C, D, E, F, G, H, I) \
19460   VAR8 (T, N, A, B, C, D, E, F, G, H), \
19461   {#N, NEON_##T, UP (I), CF (N, I), 0}
19462 #define VAR10(T, N, A, B, C, D, E, F, G, H, I, J) \
19463   VAR9 (T, N, A, B, C, D, E, F, G, H, I), \
19464   {#N, NEON_##T, UP (J), CF (N, J), 0}
19465
19466 /* The mode entries in the following table correspond to the "key" type of the
19467    instruction variant, i.e. equivalent to that which would be specified after
19468    the assembler mnemonic, which usually refers to the last vector operand.
19469    (Signed/unsigned/polynomial types are not differentiated between though, and
19470    are all mapped onto the same mode for a given element size.) The modes
19471    listed per instruction should be the same as those defined for that
19472    instruction's pattern in neon.md.  */
19473
19474 static neon_builtin_datum neon_builtin_data[] =
19475 {
19476   VAR10 (BINOP, vadd,
19477          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19478   VAR3 (BINOP, vaddl, v8qi, v4hi, v2si),
19479   VAR3 (BINOP, vaddw, v8qi, v4hi, v2si),
19480   VAR6 (BINOP, vhadd, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
19481   VAR8 (BINOP, vqadd, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
19482   VAR3 (BINOP, vaddhn, v8hi, v4si, v2di),
19483   VAR8 (BINOP, vmul, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
19484   VAR8 (TERNOP, vmla, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
19485   VAR3 (TERNOP, vmlal, v8qi, v4hi, v2si),
19486   VAR2 (TERNOP, vfma, v2sf, v4sf),
19487   VAR2 (TERNOP, vfms, v2sf, v4sf),
19488   VAR8 (TERNOP, vmls, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
19489   VAR3 (TERNOP, vmlsl, v8qi, v4hi, v2si),
19490   VAR4 (BINOP, vqdmulh, v4hi, v2si, v8hi, v4si),
19491   VAR2 (TERNOP, vqdmlal, v4hi, v2si),
19492   VAR2 (TERNOP, vqdmlsl, v4hi, v2si),
19493   VAR3 (BINOP, vmull, v8qi, v4hi, v2si),
19494   VAR2 (SCALARMULL, vmull_n, v4hi, v2si),
19495   VAR2 (LANEMULL, vmull_lane, v4hi, v2si),
19496   VAR2 (SCALARMULL, vqdmull_n, v4hi, v2si),
19497   VAR2 (LANEMULL, vqdmull_lane, v4hi, v2si),
19498   VAR4 (SCALARMULH, vqdmulh_n, v4hi, v2si, v8hi, v4si),
19499   VAR4 (LANEMULH, vqdmulh_lane, v4hi, v2si, v8hi, v4si),
19500   VAR2 (BINOP, vqdmull, v4hi, v2si),
19501   VAR8 (BINOP, vshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
19502   VAR8 (BINOP, vqshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
19503   VAR8 (SHIFTIMM, vshr_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
19504   VAR3 (SHIFTIMM, vshrn_n, v8hi, v4si, v2di),
19505   VAR3 (SHIFTIMM, vqshrn_n, v8hi, v4si, v2di),
19506   VAR3 (SHIFTIMM, vqshrun_n, v8hi, v4si, v2di),
19507   VAR8 (SHIFTIMM, vshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
19508   VAR8 (SHIFTIMM, vqshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
19509   VAR8 (SHIFTIMM, vqshlu_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
19510   VAR3 (SHIFTIMM, vshll_n, v8qi, v4hi, v2si),
19511   VAR8 (SHIFTACC, vsra_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
19512   VAR10 (BINOP, vsub,
19513          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19514   VAR3 (BINOP, vsubl, v8qi, v4hi, v2si),
19515   VAR3 (BINOP, vsubw, v8qi, v4hi, v2si),
19516   VAR8 (BINOP, vqsub, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
19517   VAR6 (BINOP, vhsub, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
19518   VAR3 (BINOP, vsubhn, v8hi, v4si, v2di),
19519   VAR8 (BINOP, vceq, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
19520   VAR8 (BINOP, vcge, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
19521   VAR6 (BINOP, vcgeu, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
19522   VAR8 (BINOP, vcgt, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
19523   VAR6 (BINOP, vcgtu, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
19524   VAR2 (BINOP, vcage, v2sf, v4sf),
19525   VAR2 (BINOP, vcagt, v2sf, v4sf),
19526   VAR6 (BINOP, vtst, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
19527   VAR8 (BINOP, vabd, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
19528   VAR3 (BINOP, vabdl, v8qi, v4hi, v2si),
19529   VAR6 (TERNOP, vaba, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
19530   VAR3 (TERNOP, vabal, v8qi, v4hi, v2si),
19531   VAR8 (BINOP, vmax, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
19532   VAR8 (BINOP, vmin, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
19533   VAR4 (BINOP, vpadd, v8qi, v4hi, v2si, v2sf),
19534   VAR6 (UNOP, vpaddl, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
19535   VAR6 (BINOP, vpadal, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
19536   VAR4 (BINOP, vpmax, v8qi, v4hi, v2si, v2sf),
19537   VAR4 (BINOP, vpmin, v8qi, v4hi, v2si, v2sf),
19538   VAR2 (BINOP, vrecps, v2sf, v4sf),
19539   VAR2 (BINOP, vrsqrts, v2sf, v4sf),
19540   VAR8 (SHIFTINSERT, vsri_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
19541   VAR8 (SHIFTINSERT, vsli_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di),
19542   VAR8 (UNOP, vabs, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
19543   VAR6 (UNOP, vqabs, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
19544   VAR8 (UNOP, vneg, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
19545   VAR6 (UNOP, vqneg, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
19546   VAR6 (UNOP, vcls, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
19547   VAR6 (UNOP, vclz, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
19548   VAR2 (UNOP, vcnt, v8qi, v16qi),
19549   VAR4 (UNOP, vrecpe, v2si, v2sf, v4si, v4sf),
19550   VAR4 (UNOP, vrsqrte, v2si, v2sf, v4si, v4sf),
19551   VAR6 (UNOP, vmvn, v8qi, v4hi, v2si, v16qi, v8hi, v4si),
19552   /* FIXME: vget_lane supports more variants than this!  */
19553   VAR10 (GETLANE, vget_lane,
19554          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19555   VAR10 (SETLANE, vset_lane,
19556          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19557   VAR5 (CREATE, vcreate, v8qi, v4hi, v2si, v2sf, di),
19558   VAR10 (DUP, vdup_n,
19559          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19560   VAR10 (DUPLANE, vdup_lane,
19561          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19562   VAR5 (COMBINE, vcombine, v8qi, v4hi, v2si, v2sf, di),
19563   VAR5 (SPLIT, vget_high, v16qi, v8hi, v4si, v4sf, v2di),
19564   VAR5 (SPLIT, vget_low, v16qi, v8hi, v4si, v4sf, v2di),
19565   VAR3 (UNOP, vmovn, v8hi, v4si, v2di),
19566   VAR3 (UNOP, vqmovn, v8hi, v4si, v2di),
19567   VAR3 (UNOP, vqmovun, v8hi, v4si, v2di),
19568   VAR3 (UNOP, vmovl, v8qi, v4hi, v2si),
19569   VAR6 (LANEMUL, vmul_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
19570   VAR6 (LANEMAC, vmla_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
19571   VAR2 (LANEMAC, vmlal_lane, v4hi, v2si),
19572   VAR2 (LANEMAC, vqdmlal_lane, v4hi, v2si),
19573   VAR6 (LANEMAC, vmls_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
19574   VAR2 (LANEMAC, vmlsl_lane, v4hi, v2si),
19575   VAR2 (LANEMAC, vqdmlsl_lane, v4hi, v2si),
19576   VAR6 (SCALARMUL, vmul_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
19577   VAR6 (SCALARMAC, vmla_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
19578   VAR2 (SCALARMAC, vmlal_n, v4hi, v2si),
19579   VAR2 (SCALARMAC, vqdmlal_n, v4hi, v2si),
19580   VAR6 (SCALARMAC, vmls_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
19581   VAR2 (SCALARMAC, vmlsl_n, v4hi, v2si),
19582   VAR2 (SCALARMAC, vqdmlsl_n, v4hi, v2si),
19583   VAR10 (BINOP, vext,
19584          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19585   VAR8 (UNOP, vrev64, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
19586   VAR4 (UNOP, vrev32, v8qi, v4hi, v16qi, v8hi),
19587   VAR2 (UNOP, vrev16, v8qi, v16qi),
19588   VAR4 (CONVERT, vcvt, v2si, v2sf, v4si, v4sf),
19589   VAR4 (FIXCONV, vcvt_n, v2si, v2sf, v4si, v4sf),
19590   VAR10 (SELECT, vbsl,
19591          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19592   VAR2 (RINT, vrintn, v2sf, v4sf),
19593   VAR2 (RINT, vrinta, v2sf, v4sf),
19594   VAR2 (RINT, vrintp, v2sf, v4sf),
19595   VAR2 (RINT, vrintm, v2sf, v4sf),
19596   VAR2 (RINT, vrintz, v2sf, v4sf),
19597   VAR2 (RINT, vrintx, v2sf, v4sf),
19598   VAR1 (VTBL, vtbl1, v8qi),
19599   VAR1 (VTBL, vtbl2, v8qi),
19600   VAR1 (VTBL, vtbl3, v8qi),
19601   VAR1 (VTBL, vtbl4, v8qi),
19602   VAR1 (VTBX, vtbx1, v8qi),
19603   VAR1 (VTBX, vtbx2, v8qi),
19604   VAR1 (VTBX, vtbx3, v8qi),
19605   VAR1 (VTBX, vtbx4, v8qi),
19606   VAR8 (RESULTPAIR, vtrn, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
19607   VAR8 (RESULTPAIR, vzip, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
19608   VAR8 (RESULTPAIR, vuzp, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf),
19609   VAR5 (REINTERP, vreinterpretv8qi, v8qi, v4hi, v2si, v2sf, di),
19610   VAR5 (REINTERP, vreinterpretv4hi, v8qi, v4hi, v2si, v2sf, di),
19611   VAR5 (REINTERP, vreinterpretv2si, v8qi, v4hi, v2si, v2sf, di),
19612   VAR5 (REINTERP, vreinterpretv2sf, v8qi, v4hi, v2si, v2sf, di),
19613   VAR5 (REINTERP, vreinterpretdi, v8qi, v4hi, v2si, v2sf, di),
19614   VAR5 (REINTERP, vreinterpretv16qi, v16qi, v8hi, v4si, v4sf, v2di),
19615   VAR5 (REINTERP, vreinterpretv8hi, v16qi, v8hi, v4si, v4sf, v2di),
19616   VAR5 (REINTERP, vreinterpretv4si, v16qi, v8hi, v4si, v4sf, v2di),
19617   VAR5 (REINTERP, vreinterpretv4sf, v16qi, v8hi, v4si, v4sf, v2di),
19618   VAR5 (REINTERP, vreinterpretv2di, v16qi, v8hi, v4si, v4sf, v2di),
19619   VAR10 (LOAD1, vld1,
19620          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19621   VAR10 (LOAD1LANE, vld1_lane,
19622          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19623   VAR10 (LOAD1, vld1_dup,
19624          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19625   VAR10 (STORE1, vst1,
19626          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19627   VAR10 (STORE1LANE, vst1_lane,
19628          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19629   VAR9 (LOADSTRUCT,
19630         vld2, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
19631   VAR7 (LOADSTRUCTLANE, vld2_lane,
19632         v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
19633   VAR5 (LOADSTRUCT, vld2_dup, v8qi, v4hi, v2si, v2sf, di),
19634   VAR9 (STORESTRUCT, vst2,
19635         v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
19636   VAR7 (STORESTRUCTLANE, vst2_lane,
19637         v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
19638   VAR9 (LOADSTRUCT,
19639         vld3, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
19640   VAR7 (LOADSTRUCTLANE, vld3_lane,
19641         v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
19642   VAR5 (LOADSTRUCT, vld3_dup, v8qi, v4hi, v2si, v2sf, di),
19643   VAR9 (STORESTRUCT, vst3,
19644         v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
19645   VAR7 (STORESTRUCTLANE, vst3_lane,
19646         v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
19647   VAR9 (LOADSTRUCT, vld4,
19648         v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
19649   VAR7 (LOADSTRUCTLANE, vld4_lane,
19650         v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
19651   VAR5 (LOADSTRUCT, vld4_dup, v8qi, v4hi, v2si, v2sf, di),
19652   VAR9 (STORESTRUCT, vst4,
19653         v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf),
19654   VAR7 (STORESTRUCTLANE, vst4_lane,
19655         v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf),
19656   VAR10 (LOGICBINOP, vand,
19657          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19658   VAR10 (LOGICBINOP, vorr,
19659          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19660   VAR10 (BINOP, veor,
19661          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19662   VAR10 (LOGICBINOP, vbic,
19663          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di),
19664   VAR10 (LOGICBINOP, vorn,
19665          v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di)
19666 };
19667
19668 #undef CF
19669 #undef VAR1
19670 #undef VAR2
19671 #undef VAR3
19672 #undef VAR4
19673 #undef VAR5
19674 #undef VAR6
19675 #undef VAR7
19676 #undef VAR8
19677 #undef VAR9
19678 #undef VAR10
19679
19680 /* Neon defines builtins from ARM_BUILTIN_MAX upwards, though they don't have
19681    symbolic names defined here (which would require too much duplication).
19682    FIXME?  */
19683 enum arm_builtins
19684 {
19685   ARM_BUILTIN_GETWCGR0,
19686   ARM_BUILTIN_GETWCGR1,
19687   ARM_BUILTIN_GETWCGR2,
19688   ARM_BUILTIN_GETWCGR3,
19689
19690   ARM_BUILTIN_SETWCGR0,
19691   ARM_BUILTIN_SETWCGR1,
19692   ARM_BUILTIN_SETWCGR2,
19693   ARM_BUILTIN_SETWCGR3,
19694
19695   ARM_BUILTIN_WZERO,
19696
19697   ARM_BUILTIN_WAVG2BR,
19698   ARM_BUILTIN_WAVG2HR,
19699   ARM_BUILTIN_WAVG2B,
19700   ARM_BUILTIN_WAVG2H,
19701
19702   ARM_BUILTIN_WACCB,
19703   ARM_BUILTIN_WACCH,
19704   ARM_BUILTIN_WACCW,
19705
19706   ARM_BUILTIN_WMACS,
19707   ARM_BUILTIN_WMACSZ,
19708   ARM_BUILTIN_WMACU,
19709   ARM_BUILTIN_WMACUZ,
19710
19711   ARM_BUILTIN_WSADB,
19712   ARM_BUILTIN_WSADBZ,
19713   ARM_BUILTIN_WSADH,
19714   ARM_BUILTIN_WSADHZ,
19715
19716   ARM_BUILTIN_WALIGNI,
19717   ARM_BUILTIN_WALIGNR0,
19718   ARM_BUILTIN_WALIGNR1,
19719   ARM_BUILTIN_WALIGNR2,
19720   ARM_BUILTIN_WALIGNR3,
19721
19722   ARM_BUILTIN_TMIA,
19723   ARM_BUILTIN_TMIAPH,
19724   ARM_BUILTIN_TMIABB,
19725   ARM_BUILTIN_TMIABT,
19726   ARM_BUILTIN_TMIATB,
19727   ARM_BUILTIN_TMIATT,
19728
19729   ARM_BUILTIN_TMOVMSKB,
19730   ARM_BUILTIN_TMOVMSKH,
19731   ARM_BUILTIN_TMOVMSKW,
19732
19733   ARM_BUILTIN_TBCSTB,
19734   ARM_BUILTIN_TBCSTH,
19735   ARM_BUILTIN_TBCSTW,
19736
19737   ARM_BUILTIN_WMADDS,
19738   ARM_BUILTIN_WMADDU,
19739
19740   ARM_BUILTIN_WPACKHSS,
19741   ARM_BUILTIN_WPACKWSS,
19742   ARM_BUILTIN_WPACKDSS,
19743   ARM_BUILTIN_WPACKHUS,
19744   ARM_BUILTIN_WPACKWUS,
19745   ARM_BUILTIN_WPACKDUS,
19746
19747   ARM_BUILTIN_WADDB,
19748   ARM_BUILTIN_WADDH,
19749   ARM_BUILTIN_WADDW,
19750   ARM_BUILTIN_WADDSSB,
19751   ARM_BUILTIN_WADDSSH,
19752   ARM_BUILTIN_WADDSSW,
19753   ARM_BUILTIN_WADDUSB,
19754   ARM_BUILTIN_WADDUSH,
19755   ARM_BUILTIN_WADDUSW,
19756   ARM_BUILTIN_WSUBB,
19757   ARM_BUILTIN_WSUBH,
19758   ARM_BUILTIN_WSUBW,
19759   ARM_BUILTIN_WSUBSSB,
19760   ARM_BUILTIN_WSUBSSH,
19761   ARM_BUILTIN_WSUBSSW,
19762   ARM_BUILTIN_WSUBUSB,
19763   ARM_BUILTIN_WSUBUSH,
19764   ARM_BUILTIN_WSUBUSW,
19765
19766   ARM_BUILTIN_WAND,
19767   ARM_BUILTIN_WANDN,
19768   ARM_BUILTIN_WOR,
19769   ARM_BUILTIN_WXOR,
19770
19771   ARM_BUILTIN_WCMPEQB,
19772   ARM_BUILTIN_WCMPEQH,
19773   ARM_BUILTIN_WCMPEQW,
19774   ARM_BUILTIN_WCMPGTUB,
19775   ARM_BUILTIN_WCMPGTUH,
19776   ARM_BUILTIN_WCMPGTUW,
19777   ARM_BUILTIN_WCMPGTSB,
19778   ARM_BUILTIN_WCMPGTSH,
19779   ARM_BUILTIN_WCMPGTSW,
19780
19781   ARM_BUILTIN_TEXTRMSB,
19782   ARM_BUILTIN_TEXTRMSH,
19783   ARM_BUILTIN_TEXTRMSW,
19784   ARM_BUILTIN_TEXTRMUB,
19785   ARM_BUILTIN_TEXTRMUH,
19786   ARM_BUILTIN_TEXTRMUW,
19787   ARM_BUILTIN_TINSRB,
19788   ARM_BUILTIN_TINSRH,
19789   ARM_BUILTIN_TINSRW,
19790
19791   ARM_BUILTIN_WMAXSW,
19792   ARM_BUILTIN_WMAXSH,
19793   ARM_BUILTIN_WMAXSB,
19794   ARM_BUILTIN_WMAXUW,
19795   ARM_BUILTIN_WMAXUH,
19796   ARM_BUILTIN_WMAXUB,
19797   ARM_BUILTIN_WMINSW,
19798   ARM_BUILTIN_WMINSH,
19799   ARM_BUILTIN_WMINSB,
19800   ARM_BUILTIN_WMINUW,
19801   ARM_BUILTIN_WMINUH,
19802   ARM_BUILTIN_WMINUB,
19803
19804   ARM_BUILTIN_WMULUM,
19805   ARM_BUILTIN_WMULSM,
19806   ARM_BUILTIN_WMULUL,
19807
19808   ARM_BUILTIN_PSADBH,
19809   ARM_BUILTIN_WSHUFH,
19810
19811   ARM_BUILTIN_WSLLH,
19812   ARM_BUILTIN_WSLLW,
19813   ARM_BUILTIN_WSLLD,
19814   ARM_BUILTIN_WSRAH,
19815   ARM_BUILTIN_WSRAW,
19816   ARM_BUILTIN_WSRAD,
19817   ARM_BUILTIN_WSRLH,
19818   ARM_BUILTIN_WSRLW,
19819   ARM_BUILTIN_WSRLD,
19820   ARM_BUILTIN_WRORH,
19821   ARM_BUILTIN_WRORW,
19822   ARM_BUILTIN_WRORD,
19823   ARM_BUILTIN_WSLLHI,
19824   ARM_BUILTIN_WSLLWI,
19825   ARM_BUILTIN_WSLLDI,
19826   ARM_BUILTIN_WSRAHI,
19827   ARM_BUILTIN_WSRAWI,
19828   ARM_BUILTIN_WSRADI,
19829   ARM_BUILTIN_WSRLHI,
19830   ARM_BUILTIN_WSRLWI,
19831   ARM_BUILTIN_WSRLDI,
19832   ARM_BUILTIN_WRORHI,
19833   ARM_BUILTIN_WRORWI,
19834   ARM_BUILTIN_WRORDI,
19835
19836   ARM_BUILTIN_WUNPCKIHB,
19837   ARM_BUILTIN_WUNPCKIHH,
19838   ARM_BUILTIN_WUNPCKIHW,
19839   ARM_BUILTIN_WUNPCKILB,
19840   ARM_BUILTIN_WUNPCKILH,
19841   ARM_BUILTIN_WUNPCKILW,
19842
19843   ARM_BUILTIN_WUNPCKEHSB,
19844   ARM_BUILTIN_WUNPCKEHSH,
19845   ARM_BUILTIN_WUNPCKEHSW,
19846   ARM_BUILTIN_WUNPCKEHUB,
19847   ARM_BUILTIN_WUNPCKEHUH,
19848   ARM_BUILTIN_WUNPCKEHUW,
19849   ARM_BUILTIN_WUNPCKELSB,
19850   ARM_BUILTIN_WUNPCKELSH,
19851   ARM_BUILTIN_WUNPCKELSW,
19852   ARM_BUILTIN_WUNPCKELUB,
19853   ARM_BUILTIN_WUNPCKELUH,
19854   ARM_BUILTIN_WUNPCKELUW,
19855
19856   ARM_BUILTIN_WABSB,
19857   ARM_BUILTIN_WABSH,
19858   ARM_BUILTIN_WABSW,
19859
19860   ARM_BUILTIN_WADDSUBHX,
19861   ARM_BUILTIN_WSUBADDHX,
19862
19863   ARM_BUILTIN_WABSDIFFB,
19864   ARM_BUILTIN_WABSDIFFH,
19865   ARM_BUILTIN_WABSDIFFW,
19866
19867   ARM_BUILTIN_WADDCH,
19868   ARM_BUILTIN_WADDCW,
19869
19870   ARM_BUILTIN_WAVG4,
19871   ARM_BUILTIN_WAVG4R,
19872
19873   ARM_BUILTIN_WMADDSX,
19874   ARM_BUILTIN_WMADDUX,
19875
19876   ARM_BUILTIN_WMADDSN,
19877   ARM_BUILTIN_WMADDUN,
19878
19879   ARM_BUILTIN_WMULWSM,
19880   ARM_BUILTIN_WMULWUM,
19881
19882   ARM_BUILTIN_WMULWSMR,
19883   ARM_BUILTIN_WMULWUMR,
19884
19885   ARM_BUILTIN_WMULWL,
19886
19887   ARM_BUILTIN_WMULSMR,
19888   ARM_BUILTIN_WMULUMR,
19889
19890   ARM_BUILTIN_WQMULM,
19891   ARM_BUILTIN_WQMULMR,
19892
19893   ARM_BUILTIN_WQMULWM,
19894   ARM_BUILTIN_WQMULWMR,
19895
19896   ARM_BUILTIN_WADDBHUSM,
19897   ARM_BUILTIN_WADDBHUSL,
19898
19899   ARM_BUILTIN_WQMIABB,
19900   ARM_BUILTIN_WQMIABT,
19901   ARM_BUILTIN_WQMIATB,
19902   ARM_BUILTIN_WQMIATT,
19903
19904   ARM_BUILTIN_WQMIABBN,
19905   ARM_BUILTIN_WQMIABTN,
19906   ARM_BUILTIN_WQMIATBN,
19907   ARM_BUILTIN_WQMIATTN,
19908
19909   ARM_BUILTIN_WMIABB,
19910   ARM_BUILTIN_WMIABT,
19911   ARM_BUILTIN_WMIATB,
19912   ARM_BUILTIN_WMIATT,
19913
19914   ARM_BUILTIN_WMIABBN,
19915   ARM_BUILTIN_WMIABTN,
19916   ARM_BUILTIN_WMIATBN,
19917   ARM_BUILTIN_WMIATTN,
19918
19919   ARM_BUILTIN_WMIAWBB,
19920   ARM_BUILTIN_WMIAWBT,
19921   ARM_BUILTIN_WMIAWTB,
19922   ARM_BUILTIN_WMIAWTT,
19923
19924   ARM_BUILTIN_WMIAWBBN,
19925   ARM_BUILTIN_WMIAWBTN,
19926   ARM_BUILTIN_WMIAWTBN,
19927   ARM_BUILTIN_WMIAWTTN,
19928
19929   ARM_BUILTIN_WMERGE,
19930
19931   ARM_BUILTIN_NEON_BASE,
19932
19933   ARM_BUILTIN_MAX = ARM_BUILTIN_NEON_BASE + ARRAY_SIZE (neon_builtin_data)
19934 };
19935
19936 static GTY(()) tree arm_builtin_decls[ARM_BUILTIN_MAX];
19937
19938 static void
19939 arm_init_neon_builtins (void)
19940 {
19941   unsigned int i, fcode;
19942   tree decl;
19943
19944   tree neon_intQI_type_node;
19945   tree neon_intHI_type_node;
19946   tree neon_polyQI_type_node;
19947   tree neon_polyHI_type_node;
19948   tree neon_intSI_type_node;
19949   tree neon_intDI_type_node;
19950   tree neon_float_type_node;
19951
19952   tree intQI_pointer_node;
19953   tree intHI_pointer_node;
19954   tree intSI_pointer_node;
19955   tree intDI_pointer_node;
19956   tree float_pointer_node;
19957
19958   tree const_intQI_node;
19959   tree const_intHI_node;
19960   tree const_intSI_node;
19961   tree const_intDI_node;
19962   tree const_float_node;
19963
19964   tree const_intQI_pointer_node;
19965   tree const_intHI_pointer_node;
19966   tree const_intSI_pointer_node;
19967   tree const_intDI_pointer_node;
19968   tree const_float_pointer_node;
19969
19970   tree V8QI_type_node;
19971   tree V4HI_type_node;
19972   tree V2SI_type_node;
19973   tree V2SF_type_node;
19974   tree V16QI_type_node;
19975   tree V8HI_type_node;
19976   tree V4SI_type_node;
19977   tree V4SF_type_node;
19978   tree V2DI_type_node;
19979
19980   tree intUQI_type_node;
19981   tree intUHI_type_node;
19982   tree intUSI_type_node;
19983   tree intUDI_type_node;
19984
19985   tree intEI_type_node;
19986   tree intOI_type_node;
19987   tree intCI_type_node;
19988   tree intXI_type_node;
19989
19990   tree V8QI_pointer_node;
19991   tree V4HI_pointer_node;
19992   tree V2SI_pointer_node;
19993   tree V2SF_pointer_node;
19994   tree V16QI_pointer_node;
19995   tree V8HI_pointer_node;
19996   tree V4SI_pointer_node;
19997   tree V4SF_pointer_node;
19998   tree V2DI_pointer_node;
19999
20000   tree void_ftype_pv8qi_v8qi_v8qi;
20001   tree void_ftype_pv4hi_v4hi_v4hi;
20002   tree void_ftype_pv2si_v2si_v2si;
20003   tree void_ftype_pv2sf_v2sf_v2sf;
20004   tree void_ftype_pdi_di_di;
20005   tree void_ftype_pv16qi_v16qi_v16qi;
20006   tree void_ftype_pv8hi_v8hi_v8hi;
20007   tree void_ftype_pv4si_v4si_v4si;
20008   tree void_ftype_pv4sf_v4sf_v4sf;
20009   tree void_ftype_pv2di_v2di_v2di;
20010
20011   tree reinterp_ftype_dreg[5][5];
20012   tree reinterp_ftype_qreg[5][5];
20013   tree dreg_types[5], qreg_types[5];
20014
20015   /* Create distinguished type nodes for NEON vector element types,
20016      and pointers to values of such types, so we can detect them later.  */
20017   neon_intQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
20018   neon_intHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
20019   neon_polyQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
20020   neon_polyHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
20021   neon_intSI_type_node = make_signed_type (GET_MODE_PRECISION (SImode));
20022   neon_intDI_type_node = make_signed_type (GET_MODE_PRECISION (DImode));
20023   neon_float_type_node = make_node (REAL_TYPE);
20024   TYPE_PRECISION (neon_float_type_node) = FLOAT_TYPE_SIZE;
20025   layout_type (neon_float_type_node);
20026
20027   /* Define typedefs which exactly correspond to the modes we are basing vector
20028      types on.  If you change these names you'll need to change
20029      the table used by arm_mangle_type too.  */
20030   (*lang_hooks.types.register_builtin_type) (neon_intQI_type_node,
20031                                              "__builtin_neon_qi");
20032   (*lang_hooks.types.register_builtin_type) (neon_intHI_type_node,
20033                                              "__builtin_neon_hi");
20034   (*lang_hooks.types.register_builtin_type) (neon_intSI_type_node,
20035                                              "__builtin_neon_si");
20036   (*lang_hooks.types.register_builtin_type) (neon_float_type_node,
20037                                              "__builtin_neon_sf");
20038   (*lang_hooks.types.register_builtin_type) (neon_intDI_type_node,
20039                                              "__builtin_neon_di");
20040   (*lang_hooks.types.register_builtin_type) (neon_polyQI_type_node,
20041                                              "__builtin_neon_poly8");
20042   (*lang_hooks.types.register_builtin_type) (neon_polyHI_type_node,
20043                                              "__builtin_neon_poly16");
20044
20045   intQI_pointer_node = build_pointer_type (neon_intQI_type_node);
20046   intHI_pointer_node = build_pointer_type (neon_intHI_type_node);
20047   intSI_pointer_node = build_pointer_type (neon_intSI_type_node);
20048   intDI_pointer_node = build_pointer_type (neon_intDI_type_node);
20049   float_pointer_node = build_pointer_type (neon_float_type_node);
20050
20051   /* Next create constant-qualified versions of the above types.  */
20052   const_intQI_node = build_qualified_type (neon_intQI_type_node,
20053                                            TYPE_QUAL_CONST);
20054   const_intHI_node = build_qualified_type (neon_intHI_type_node,
20055                                            TYPE_QUAL_CONST);
20056   const_intSI_node = build_qualified_type (neon_intSI_type_node,
20057                                            TYPE_QUAL_CONST);
20058   const_intDI_node = build_qualified_type (neon_intDI_type_node,
20059                                            TYPE_QUAL_CONST);
20060   const_float_node = build_qualified_type (neon_float_type_node,
20061                                            TYPE_QUAL_CONST);
20062
20063   const_intQI_pointer_node = build_pointer_type (const_intQI_node);
20064   const_intHI_pointer_node = build_pointer_type (const_intHI_node);
20065   const_intSI_pointer_node = build_pointer_type (const_intSI_node);
20066   const_intDI_pointer_node = build_pointer_type (const_intDI_node);
20067   const_float_pointer_node = build_pointer_type (const_float_node);
20068
20069   /* Now create vector types based on our NEON element types.  */
20070   /* 64-bit vectors.  */
20071   V8QI_type_node =
20072     build_vector_type_for_mode (neon_intQI_type_node, V8QImode);
20073   V4HI_type_node =
20074     build_vector_type_for_mode (neon_intHI_type_node, V4HImode);
20075   V2SI_type_node =
20076     build_vector_type_for_mode (neon_intSI_type_node, V2SImode);
20077   V2SF_type_node =
20078     build_vector_type_for_mode (neon_float_type_node, V2SFmode);
20079   /* 128-bit vectors.  */
20080   V16QI_type_node =
20081     build_vector_type_for_mode (neon_intQI_type_node, V16QImode);
20082   V8HI_type_node =
20083     build_vector_type_for_mode (neon_intHI_type_node, V8HImode);
20084   V4SI_type_node =
20085     build_vector_type_for_mode (neon_intSI_type_node, V4SImode);
20086   V4SF_type_node =
20087     build_vector_type_for_mode (neon_float_type_node, V4SFmode);
20088   V2DI_type_node =
20089     build_vector_type_for_mode (neon_intDI_type_node, V2DImode);
20090
20091   /* Unsigned integer types for various mode sizes.  */
20092   intUQI_type_node = make_unsigned_type (GET_MODE_PRECISION (QImode));
20093   intUHI_type_node = make_unsigned_type (GET_MODE_PRECISION (HImode));
20094   intUSI_type_node = make_unsigned_type (GET_MODE_PRECISION (SImode));
20095   intUDI_type_node = make_unsigned_type (GET_MODE_PRECISION (DImode));
20096
20097   (*lang_hooks.types.register_builtin_type) (intUQI_type_node,
20098                                              "__builtin_neon_uqi");
20099   (*lang_hooks.types.register_builtin_type) (intUHI_type_node,
20100                                              "__builtin_neon_uhi");
20101   (*lang_hooks.types.register_builtin_type) (intUSI_type_node,
20102                                              "__builtin_neon_usi");
20103   (*lang_hooks.types.register_builtin_type) (intUDI_type_node,
20104                                              "__builtin_neon_udi");
20105
20106   /* Opaque integer types for structures of vectors.  */
20107   intEI_type_node = make_signed_type (GET_MODE_PRECISION (EImode));
20108   intOI_type_node = make_signed_type (GET_MODE_PRECISION (OImode));
20109   intCI_type_node = make_signed_type (GET_MODE_PRECISION (CImode));
20110   intXI_type_node = make_signed_type (GET_MODE_PRECISION (XImode));
20111
20112   (*lang_hooks.types.register_builtin_type) (intTI_type_node,
20113                                              "__builtin_neon_ti");
20114   (*lang_hooks.types.register_builtin_type) (intEI_type_node,
20115                                              "__builtin_neon_ei");
20116   (*lang_hooks.types.register_builtin_type) (intOI_type_node,
20117                                              "__builtin_neon_oi");
20118   (*lang_hooks.types.register_builtin_type) (intCI_type_node,
20119                                              "__builtin_neon_ci");
20120   (*lang_hooks.types.register_builtin_type) (intXI_type_node,
20121                                              "__builtin_neon_xi");
20122
20123   /* Pointers to vector types.  */
20124   V8QI_pointer_node = build_pointer_type (V8QI_type_node);
20125   V4HI_pointer_node = build_pointer_type (V4HI_type_node);
20126   V2SI_pointer_node = build_pointer_type (V2SI_type_node);
20127   V2SF_pointer_node = build_pointer_type (V2SF_type_node);
20128   V16QI_pointer_node = build_pointer_type (V16QI_type_node);
20129   V8HI_pointer_node = build_pointer_type (V8HI_type_node);
20130   V4SI_pointer_node = build_pointer_type (V4SI_type_node);
20131   V4SF_pointer_node = build_pointer_type (V4SF_type_node);
20132   V2DI_pointer_node = build_pointer_type (V2DI_type_node);
20133
20134   /* Operations which return results as pairs.  */
20135   void_ftype_pv8qi_v8qi_v8qi =
20136     build_function_type_list (void_type_node, V8QI_pointer_node, V8QI_type_node,
20137                               V8QI_type_node, NULL);
20138   void_ftype_pv4hi_v4hi_v4hi =
20139     build_function_type_list (void_type_node, V4HI_pointer_node, V4HI_type_node,
20140                               V4HI_type_node, NULL);
20141   void_ftype_pv2si_v2si_v2si =
20142     build_function_type_list (void_type_node, V2SI_pointer_node, V2SI_type_node,
20143                               V2SI_type_node, NULL);
20144   void_ftype_pv2sf_v2sf_v2sf =
20145     build_function_type_list (void_type_node, V2SF_pointer_node, V2SF_type_node,
20146                               V2SF_type_node, NULL);
20147   void_ftype_pdi_di_di =
20148     build_function_type_list (void_type_node, intDI_pointer_node,
20149                               neon_intDI_type_node, neon_intDI_type_node, NULL);
20150   void_ftype_pv16qi_v16qi_v16qi =
20151     build_function_type_list (void_type_node, V16QI_pointer_node,
20152                               V16QI_type_node, V16QI_type_node, NULL);
20153   void_ftype_pv8hi_v8hi_v8hi =
20154     build_function_type_list (void_type_node, V8HI_pointer_node, V8HI_type_node,
20155                               V8HI_type_node, NULL);
20156   void_ftype_pv4si_v4si_v4si =
20157     build_function_type_list (void_type_node, V4SI_pointer_node, V4SI_type_node,
20158                               V4SI_type_node, NULL);
20159   void_ftype_pv4sf_v4sf_v4sf =
20160     build_function_type_list (void_type_node, V4SF_pointer_node, V4SF_type_node,
20161                               V4SF_type_node, NULL);
20162   void_ftype_pv2di_v2di_v2di =
20163     build_function_type_list (void_type_node, V2DI_pointer_node, V2DI_type_node,
20164                               V2DI_type_node, NULL);
20165
20166   dreg_types[0] = V8QI_type_node;
20167   dreg_types[1] = V4HI_type_node;
20168   dreg_types[2] = V2SI_type_node;
20169   dreg_types[3] = V2SF_type_node;
20170   dreg_types[4] = neon_intDI_type_node;
20171
20172   qreg_types[0] = V16QI_type_node;
20173   qreg_types[1] = V8HI_type_node;
20174   qreg_types[2] = V4SI_type_node;
20175   qreg_types[3] = V4SF_type_node;
20176   qreg_types[4] = V2DI_type_node;
20177
20178   for (i = 0; i < 5; i++)
20179     {
20180       int j;
20181       for (j = 0; j < 5; j++)
20182         {
20183           reinterp_ftype_dreg[i][j]
20184             = build_function_type_list (dreg_types[i], dreg_types[j], NULL);
20185           reinterp_ftype_qreg[i][j]
20186             = build_function_type_list (qreg_types[i], qreg_types[j], NULL);
20187         }
20188     }
20189
20190   for (i = 0, fcode = ARM_BUILTIN_NEON_BASE;
20191        i < ARRAY_SIZE (neon_builtin_data);
20192        i++, fcode++)
20193     {
20194       neon_builtin_datum *d = &neon_builtin_data[i];
20195
20196       const char* const modenames[] = {
20197         "v8qi", "v4hi", "v2si", "v2sf", "di",
20198         "v16qi", "v8hi", "v4si", "v4sf", "v2di",
20199         "ti", "ei", "oi"
20200       };
20201       char namebuf[60];
20202       tree ftype = NULL;
20203       int is_load = 0, is_store = 0;
20204
20205       gcc_assert (ARRAY_SIZE (modenames) == T_MAX);
20206
20207       d->fcode = fcode;
20208
20209       switch (d->itype)
20210         {
20211         case NEON_LOAD1:
20212         case NEON_LOAD1LANE:
20213         case NEON_LOADSTRUCT:
20214         case NEON_LOADSTRUCTLANE:
20215           is_load = 1;
20216           /* Fall through.  */
20217         case NEON_STORE1:
20218         case NEON_STORE1LANE:
20219         case NEON_STORESTRUCT:
20220         case NEON_STORESTRUCTLANE:
20221           if (!is_load)
20222             is_store = 1;
20223           /* Fall through.  */
20224         case NEON_UNOP:
20225         case NEON_RINT:
20226         case NEON_BINOP:
20227         case NEON_LOGICBINOP:
20228         case NEON_SHIFTINSERT:
20229         case NEON_TERNOP:
20230         case NEON_GETLANE:
20231         case NEON_SETLANE:
20232         case NEON_CREATE:
20233         case NEON_DUP:
20234         case NEON_DUPLANE:
20235         case NEON_SHIFTIMM:
20236         case NEON_SHIFTACC:
20237         case NEON_COMBINE:
20238         case NEON_SPLIT:
20239         case NEON_CONVERT:
20240         case NEON_FIXCONV:
20241         case NEON_LANEMUL:
20242         case NEON_LANEMULL:
20243         case NEON_LANEMULH:
20244         case NEON_LANEMAC:
20245         case NEON_SCALARMUL:
20246         case NEON_SCALARMULL:
20247         case NEON_SCALARMULH:
20248         case NEON_SCALARMAC:
20249         case NEON_SELECT:
20250         case NEON_VTBL:
20251         case NEON_VTBX:
20252           {
20253             int k;
20254             tree return_type = void_type_node, args = void_list_node;
20255
20256             /* Build a function type directly from the insn_data for
20257                this builtin.  The build_function_type() function takes
20258                care of removing duplicates for us.  */
20259             for (k = insn_data[d->code].n_generator_args - 1; k >= 0; k--)
20260               {
20261                 tree eltype;
20262
20263                 if (is_load && k == 1)
20264                   {
20265                     /* Neon load patterns always have the memory
20266                        operand in the operand 1 position.  */
20267                     gcc_assert (insn_data[d->code].operand[k].predicate
20268                                 == neon_struct_operand);
20269
20270                     switch (d->mode)
20271                       {
20272                       case T_V8QI:
20273                       case T_V16QI:
20274                         eltype = const_intQI_pointer_node;
20275                         break;
20276
20277                       case T_V4HI:
20278                       case T_V8HI:
20279                         eltype = const_intHI_pointer_node;
20280                         break;
20281
20282                       case T_V2SI:
20283                       case T_V4SI:
20284                         eltype = const_intSI_pointer_node;
20285                         break;
20286
20287                       case T_V2SF:
20288                       case T_V4SF:
20289                         eltype = const_float_pointer_node;
20290                         break;
20291
20292                       case T_DI:
20293                       case T_V2DI:
20294                         eltype = const_intDI_pointer_node;
20295                         break;
20296
20297                       default: gcc_unreachable ();
20298                       }
20299                   }
20300                 else if (is_store && k == 0)
20301                   {
20302                     /* Similarly, Neon store patterns use operand 0 as
20303                        the memory location to store to.  */
20304                     gcc_assert (insn_data[d->code].operand[k].predicate
20305                                 == neon_struct_operand);
20306
20307                     switch (d->mode)
20308                       {
20309                       case T_V8QI:
20310                       case T_V16QI:
20311                         eltype = intQI_pointer_node;
20312                         break;
20313
20314                       case T_V4HI:
20315                       case T_V8HI:
20316                         eltype = intHI_pointer_node;
20317                         break;
20318
20319                       case T_V2SI:
20320                       case T_V4SI:
20321                         eltype = intSI_pointer_node;
20322                         break;
20323
20324                       case T_V2SF:
20325                       case T_V4SF:
20326                         eltype = float_pointer_node;
20327                         break;
20328
20329                       case T_DI:
20330                       case T_V2DI:
20331                         eltype = intDI_pointer_node;
20332                         break;
20333
20334                       default: gcc_unreachable ();
20335                       }
20336                   }
20337                 else
20338                   {
20339                     switch (insn_data[d->code].operand[k].mode)
20340                       {
20341                       case VOIDmode: eltype = void_type_node; break;
20342                         /* Scalars.  */
20343                       case QImode: eltype = neon_intQI_type_node; break;
20344                       case HImode: eltype = neon_intHI_type_node; break;
20345                       case SImode: eltype = neon_intSI_type_node; break;
20346                       case SFmode: eltype = neon_float_type_node; break;
20347                       case DImode: eltype = neon_intDI_type_node; break;
20348                       case TImode: eltype = intTI_type_node; break;
20349                       case EImode: eltype = intEI_type_node; break;
20350                       case OImode: eltype = intOI_type_node; break;
20351                       case CImode: eltype = intCI_type_node; break;
20352                       case XImode: eltype = intXI_type_node; break;
20353                         /* 64-bit vectors.  */
20354                       case V8QImode: eltype = V8QI_type_node; break;
20355                       case V4HImode: eltype = V4HI_type_node; break;
20356                       case V2SImode: eltype = V2SI_type_node; break;
20357                       case V2SFmode: eltype = V2SF_type_node; break;
20358                         /* 128-bit vectors.  */
20359                       case V16QImode: eltype = V16QI_type_node; break;
20360                       case V8HImode: eltype = V8HI_type_node; break;
20361                       case V4SImode: eltype = V4SI_type_node; break;
20362                       case V4SFmode: eltype = V4SF_type_node; break;
20363                       case V2DImode: eltype = V2DI_type_node; break;
20364                       default: gcc_unreachable ();
20365                       }
20366                   }
20367
20368                 if (k == 0 && !is_store)
20369                   return_type = eltype;
20370                 else
20371                   args = tree_cons (NULL_TREE, eltype, args);
20372               }
20373
20374             ftype = build_function_type (return_type, args);
20375           }
20376           break;
20377
20378         case NEON_RESULTPAIR:
20379           {
20380             switch (insn_data[d->code].operand[1].mode)
20381               {
20382               case V8QImode: ftype = void_ftype_pv8qi_v8qi_v8qi; break;
20383               case V4HImode: ftype = void_ftype_pv4hi_v4hi_v4hi; break;
20384               case V2SImode: ftype = void_ftype_pv2si_v2si_v2si; break;
20385               case V2SFmode: ftype = void_ftype_pv2sf_v2sf_v2sf; break;
20386               case DImode: ftype = void_ftype_pdi_di_di; break;
20387               case V16QImode: ftype = void_ftype_pv16qi_v16qi_v16qi; break;
20388               case V8HImode: ftype = void_ftype_pv8hi_v8hi_v8hi; break;
20389               case V4SImode: ftype = void_ftype_pv4si_v4si_v4si; break;
20390               case V4SFmode: ftype = void_ftype_pv4sf_v4sf_v4sf; break;
20391               case V2DImode: ftype = void_ftype_pv2di_v2di_v2di; break;
20392               default: gcc_unreachable ();
20393               }
20394           }
20395           break;
20396
20397         case NEON_REINTERP:
20398           {
20399             /* We iterate over 5 doubleword types, then 5 quadword
20400                types.  */
20401             int rhs = d->mode % 5;
20402             switch (insn_data[d->code].operand[0].mode)
20403               {
20404               case V8QImode: ftype = reinterp_ftype_dreg[0][rhs]; break;
20405               case V4HImode: ftype = reinterp_ftype_dreg[1][rhs]; break;
20406               case V2SImode: ftype = reinterp_ftype_dreg[2][rhs]; break;
20407               case V2SFmode: ftype = reinterp_ftype_dreg[3][rhs]; break;
20408               case DImode: ftype = reinterp_ftype_dreg[4][rhs]; break;
20409               case V16QImode: ftype = reinterp_ftype_qreg[0][rhs]; break;
20410               case V8HImode: ftype = reinterp_ftype_qreg[1][rhs]; break;
20411               case V4SImode: ftype = reinterp_ftype_qreg[2][rhs]; break;
20412               case V4SFmode: ftype = reinterp_ftype_qreg[3][rhs]; break;
20413               case V2DImode: ftype = reinterp_ftype_qreg[4][rhs]; break;
20414               default: gcc_unreachable ();
20415               }
20416           }
20417           break;
20418
20419         default:
20420           gcc_unreachable ();
20421         }
20422
20423       gcc_assert (ftype != NULL);
20424
20425       sprintf (namebuf, "__builtin_neon_%s%s", d->name, modenames[d->mode]);
20426
20427       decl = add_builtin_function (namebuf, ftype, fcode, BUILT_IN_MD, NULL,
20428                                    NULL_TREE);
20429       arm_builtin_decls[fcode] = decl;
20430     }
20431 }
20432
20433 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
20434   do                                                                    \
20435     {                                                                   \
20436       if ((MASK) & insn_flags)                                          \
20437         {                                                               \
20438           tree bdecl;                                                   \
20439           bdecl = add_builtin_function ((NAME), (TYPE), (CODE),         \
20440                                         BUILT_IN_MD, NULL, NULL_TREE);  \
20441           arm_builtin_decls[CODE] = bdecl;                              \
20442         }                                                               \
20443     }                                                                   \
20444   while (0)
20445
20446 struct builtin_description
20447 {
20448   const unsigned int       mask;
20449   const enum insn_code     icode;
20450   const char * const       name;
20451   const enum arm_builtins  code;
20452   const enum rtx_code      comparison;
20453   const unsigned int       flag;
20454 };
20455   
20456 static const struct builtin_description bdesc_2arg[] =
20457 {
20458 #define IWMMXT_BUILTIN(code, string, builtin) \
20459   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
20460     ARM_BUILTIN_##builtin, UNKNOWN, 0 },
20461
20462 #define IWMMXT2_BUILTIN(code, string, builtin) \
20463   { FL_IWMMXT2, CODE_FOR_##code, "__builtin_arm_" string, \
20464     ARM_BUILTIN_##builtin, UNKNOWN, 0 },
20465
20466   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
20467   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
20468   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
20469   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
20470   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
20471   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
20472   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
20473   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
20474   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
20475   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
20476   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
20477   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
20478   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
20479   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
20480   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
20481   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
20482   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
20483   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
20484   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
20485   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
20486   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
20487   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
20488   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
20489   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
20490   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
20491   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
20492   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
20493   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
20494   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
20495   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
20496   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
20497   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
20498   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
20499   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
20500   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
20501   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
20502   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
20503   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
20504   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
20505   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
20506   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
20507   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
20508   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
20509   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
20510   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
20511   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
20512   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
20513   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
20514   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
20515   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
20516   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
20517   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
20518   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
20519   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
20520   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
20521   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
20522   IWMMXT2_BUILTIN (iwmmxt_waddsubhx, "waddsubhx", WADDSUBHX)
20523   IWMMXT2_BUILTIN (iwmmxt_wsubaddhx, "wsubaddhx", WSUBADDHX)
20524   IWMMXT2_BUILTIN (iwmmxt_wabsdiffb, "wabsdiffb", WABSDIFFB)
20525   IWMMXT2_BUILTIN (iwmmxt_wabsdiffh, "wabsdiffh", WABSDIFFH)
20526   IWMMXT2_BUILTIN (iwmmxt_wabsdiffw, "wabsdiffw", WABSDIFFW)
20527   IWMMXT2_BUILTIN (iwmmxt_avg4, "wavg4", WAVG4)
20528   IWMMXT2_BUILTIN (iwmmxt_avg4r, "wavg4r", WAVG4R)
20529   IWMMXT2_BUILTIN (iwmmxt_wmulwsm, "wmulwsm", WMULWSM)
20530   IWMMXT2_BUILTIN (iwmmxt_wmulwum, "wmulwum", WMULWUM)
20531   IWMMXT2_BUILTIN (iwmmxt_wmulwsmr, "wmulwsmr", WMULWSMR)
20532   IWMMXT2_BUILTIN (iwmmxt_wmulwumr, "wmulwumr", WMULWUMR)
20533   IWMMXT2_BUILTIN (iwmmxt_wmulwl, "wmulwl", WMULWL)
20534   IWMMXT2_BUILTIN (iwmmxt_wmulsmr, "wmulsmr", WMULSMR)
20535   IWMMXT2_BUILTIN (iwmmxt_wmulumr, "wmulumr", WMULUMR)
20536   IWMMXT2_BUILTIN (iwmmxt_wqmulm, "wqmulm", WQMULM)
20537   IWMMXT2_BUILTIN (iwmmxt_wqmulmr, "wqmulmr", WQMULMR)
20538   IWMMXT2_BUILTIN (iwmmxt_wqmulwm, "wqmulwm", WQMULWM)
20539   IWMMXT2_BUILTIN (iwmmxt_wqmulwmr, "wqmulwmr", WQMULWMR)
20540   IWMMXT_BUILTIN (iwmmxt_walignr0, "walignr0", WALIGNR0)
20541   IWMMXT_BUILTIN (iwmmxt_walignr1, "walignr1", WALIGNR1)
20542   IWMMXT_BUILTIN (iwmmxt_walignr2, "walignr2", WALIGNR2)
20543   IWMMXT_BUILTIN (iwmmxt_walignr3, "walignr3", WALIGNR3)
20544
20545 #define IWMMXT_BUILTIN2(code, builtin) \
20546   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, UNKNOWN, 0 },
20547
20548 #define IWMMXT2_BUILTIN2(code, builtin) \
20549   { FL_IWMMXT2, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, UNKNOWN, 0 },
20550
20551   IWMMXT2_BUILTIN2 (iwmmxt_waddbhusm, WADDBHUSM)
20552   IWMMXT2_BUILTIN2 (iwmmxt_waddbhusl, WADDBHUSL)
20553   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
20554   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
20555   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
20556   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
20557   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
20558   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
20559   IWMMXT_BUILTIN2 (iwmmxt_wmacuz, WMACUZ)
20560   IWMMXT_BUILTIN2 (iwmmxt_wmacsz, WMACSZ)
20561 };
20562
20563 static const struct builtin_description bdesc_1arg[] =
20564 {
20565   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
20566   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
20567   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
20568   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
20569   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
20570   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
20571   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
20572   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
20573   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
20574   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
20575   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
20576   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
20577   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
20578   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
20579   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
20580   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
20581   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
20582   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
20583   IWMMXT2_BUILTIN (iwmmxt_wabsv8qi3, "wabsb", WABSB)
20584   IWMMXT2_BUILTIN (iwmmxt_wabsv4hi3, "wabsh", WABSH)
20585   IWMMXT2_BUILTIN (iwmmxt_wabsv2si3, "wabsw", WABSW)
20586   IWMMXT_BUILTIN (tbcstv8qi, "tbcstb", TBCSTB)
20587   IWMMXT_BUILTIN (tbcstv4hi, "tbcsth", TBCSTH)
20588   IWMMXT_BUILTIN (tbcstv2si, "tbcstw", TBCSTW)
20589 };
20590
20591 /* Set up all the iWMMXt builtins.  This is not called if
20592    TARGET_IWMMXT is zero.  */
20593
20594 static void
20595 arm_init_iwmmxt_builtins (void)
20596 {
20597   const struct builtin_description * d;
20598   size_t i;
20599
20600   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
20601   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
20602   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
20603
20604   tree v8qi_ftype_v8qi_v8qi_int
20605     = build_function_type_list (V8QI_type_node,
20606                                 V8QI_type_node, V8QI_type_node,
20607                                 integer_type_node, NULL_TREE);
20608   tree v4hi_ftype_v4hi_int
20609     = build_function_type_list (V4HI_type_node,
20610                                 V4HI_type_node, integer_type_node, NULL_TREE);
20611   tree v2si_ftype_v2si_int
20612     = build_function_type_list (V2SI_type_node,
20613                                 V2SI_type_node, integer_type_node, NULL_TREE);
20614   tree v2si_ftype_di_di
20615     = build_function_type_list (V2SI_type_node,
20616                                 long_long_integer_type_node,
20617                                 long_long_integer_type_node,
20618                                 NULL_TREE);
20619   tree di_ftype_di_int
20620     = build_function_type_list (long_long_integer_type_node,
20621                                 long_long_integer_type_node,
20622                                 integer_type_node, NULL_TREE);
20623   tree di_ftype_di_int_int
20624     = build_function_type_list (long_long_integer_type_node,
20625                                 long_long_integer_type_node,
20626                                 integer_type_node,
20627                                 integer_type_node, NULL_TREE);
20628   tree int_ftype_v8qi
20629     = build_function_type_list (integer_type_node,
20630                                 V8QI_type_node, NULL_TREE);
20631   tree int_ftype_v4hi
20632     = build_function_type_list (integer_type_node,
20633                                 V4HI_type_node, NULL_TREE);
20634   tree int_ftype_v2si
20635     = build_function_type_list (integer_type_node,
20636                                 V2SI_type_node, NULL_TREE);
20637   tree int_ftype_v8qi_int
20638     = build_function_type_list (integer_type_node,
20639                                 V8QI_type_node, integer_type_node, NULL_TREE);
20640   tree int_ftype_v4hi_int
20641     = build_function_type_list (integer_type_node,
20642                                 V4HI_type_node, integer_type_node, NULL_TREE);
20643   tree int_ftype_v2si_int
20644     = build_function_type_list (integer_type_node,
20645                                 V2SI_type_node, integer_type_node, NULL_TREE);
20646   tree v8qi_ftype_v8qi_int_int
20647     = build_function_type_list (V8QI_type_node,
20648                                 V8QI_type_node, integer_type_node,
20649                                 integer_type_node, NULL_TREE);
20650   tree v4hi_ftype_v4hi_int_int
20651     = build_function_type_list (V4HI_type_node,
20652                                 V4HI_type_node, integer_type_node,
20653                                 integer_type_node, NULL_TREE);
20654   tree v2si_ftype_v2si_int_int
20655     = build_function_type_list (V2SI_type_node,
20656                                 V2SI_type_node, integer_type_node,
20657                                 integer_type_node, NULL_TREE);
20658   /* Miscellaneous.  */
20659   tree v8qi_ftype_v4hi_v4hi
20660     = build_function_type_list (V8QI_type_node,
20661                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
20662   tree v4hi_ftype_v2si_v2si
20663     = build_function_type_list (V4HI_type_node,
20664                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
20665   tree v8qi_ftype_v4hi_v8qi
20666     = build_function_type_list (V8QI_type_node,
20667                                 V4HI_type_node, V8QI_type_node, NULL_TREE);
20668   tree v2si_ftype_v4hi_v4hi
20669     = build_function_type_list (V2SI_type_node,
20670                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
20671   tree v2si_ftype_v8qi_v8qi
20672     = build_function_type_list (V2SI_type_node,
20673                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
20674   tree v4hi_ftype_v4hi_di
20675     = build_function_type_list (V4HI_type_node,
20676                                 V4HI_type_node, long_long_integer_type_node,
20677                                 NULL_TREE);
20678   tree v2si_ftype_v2si_di
20679     = build_function_type_list (V2SI_type_node,
20680                                 V2SI_type_node, long_long_integer_type_node,
20681                                 NULL_TREE);
20682   tree di_ftype_void
20683     = build_function_type_list (long_long_unsigned_type_node, NULL_TREE);
20684   tree int_ftype_void
20685     = build_function_type_list (integer_type_node, NULL_TREE);
20686   tree di_ftype_v8qi
20687     = build_function_type_list (long_long_integer_type_node,
20688                                 V8QI_type_node, NULL_TREE);
20689   tree di_ftype_v4hi
20690     = build_function_type_list (long_long_integer_type_node,
20691                                 V4HI_type_node, NULL_TREE);
20692   tree di_ftype_v2si
20693     = build_function_type_list (long_long_integer_type_node,
20694                                 V2SI_type_node, NULL_TREE);
20695   tree v2si_ftype_v4hi
20696     = build_function_type_list (V2SI_type_node,
20697                                 V4HI_type_node, NULL_TREE);
20698   tree v4hi_ftype_v8qi
20699     = build_function_type_list (V4HI_type_node,
20700                                 V8QI_type_node, NULL_TREE);
20701   tree v8qi_ftype_v8qi
20702     = build_function_type_list (V8QI_type_node,
20703                                 V8QI_type_node, NULL_TREE);
20704   tree v4hi_ftype_v4hi
20705     = build_function_type_list (V4HI_type_node,
20706                                 V4HI_type_node, NULL_TREE);
20707   tree v2si_ftype_v2si
20708     = build_function_type_list (V2SI_type_node,
20709                                 V2SI_type_node, NULL_TREE);
20710
20711   tree di_ftype_di_v4hi_v4hi
20712     = build_function_type_list (long_long_unsigned_type_node,
20713                                 long_long_unsigned_type_node,
20714                                 V4HI_type_node, V4HI_type_node,
20715                                 NULL_TREE);
20716
20717   tree di_ftype_v4hi_v4hi
20718     = build_function_type_list (long_long_unsigned_type_node,
20719                                 V4HI_type_node,V4HI_type_node,
20720                                 NULL_TREE);
20721
20722   tree v2si_ftype_v2si_v4hi_v4hi
20723     = build_function_type_list (V2SI_type_node,
20724                                 V2SI_type_node, V4HI_type_node,
20725                                 V4HI_type_node, NULL_TREE);
20726
20727   tree v2si_ftype_v2si_v8qi_v8qi
20728     = build_function_type_list (V2SI_type_node,
20729                                 V2SI_type_node, V8QI_type_node,
20730                                 V8QI_type_node, NULL_TREE);
20731
20732   tree di_ftype_di_v2si_v2si
20733      = build_function_type_list (long_long_unsigned_type_node,
20734                                  long_long_unsigned_type_node,
20735                                  V2SI_type_node, V2SI_type_node,
20736                                  NULL_TREE);
20737
20738    tree di_ftype_di_di_int
20739      = build_function_type_list (long_long_unsigned_type_node,
20740                                  long_long_unsigned_type_node,
20741                                  long_long_unsigned_type_node,
20742                                  integer_type_node, NULL_TREE);
20743
20744    tree void_ftype_int
20745      = build_function_type_list (void_type_node,
20746                                  integer_type_node, NULL_TREE);
20747
20748    tree v8qi_ftype_char
20749      = build_function_type_list (V8QI_type_node,
20750                                  signed_char_type_node, NULL_TREE);
20751
20752    tree v4hi_ftype_short
20753      = build_function_type_list (V4HI_type_node,
20754                                  short_integer_type_node, NULL_TREE);
20755
20756    tree v2si_ftype_int
20757      = build_function_type_list (V2SI_type_node,
20758                                  integer_type_node, NULL_TREE);
20759
20760   /* Normal vector binops.  */
20761   tree v8qi_ftype_v8qi_v8qi
20762     = build_function_type_list (V8QI_type_node,
20763                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
20764   tree v4hi_ftype_v4hi_v4hi
20765     = build_function_type_list (V4HI_type_node,
20766                                 V4HI_type_node,V4HI_type_node, NULL_TREE);
20767   tree v2si_ftype_v2si_v2si
20768     = build_function_type_list (V2SI_type_node,
20769                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
20770   tree di_ftype_di_di
20771     = build_function_type_list (long_long_unsigned_type_node,
20772                                 long_long_unsigned_type_node,
20773                                 long_long_unsigned_type_node,
20774                                 NULL_TREE);
20775
20776   /* Add all builtins that are more or less simple operations on two
20777      operands.  */
20778   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
20779     {
20780       /* Use one of the operands; the target can have a different mode for
20781          mask-generating compares.  */
20782       enum machine_mode mode;
20783       tree type;
20784
20785       if (d->name == 0)
20786         continue;
20787
20788       mode = insn_data[d->icode].operand[1].mode;
20789
20790       switch (mode)
20791         {
20792         case V8QImode:
20793           type = v8qi_ftype_v8qi_v8qi;
20794           break;
20795         case V4HImode:
20796           type = v4hi_ftype_v4hi_v4hi;
20797           break;
20798         case V2SImode:
20799           type = v2si_ftype_v2si_v2si;
20800           break;
20801         case DImode:
20802           type = di_ftype_di_di;
20803           break;
20804
20805         default:
20806           gcc_unreachable ();
20807         }
20808
20809       def_mbuiltin (d->mask, d->name, type, d->code);
20810     }
20811
20812   /* Add the remaining MMX insns with somewhat more complicated types.  */
20813 #define iwmmx_mbuiltin(NAME, TYPE, CODE)                        \
20814   def_mbuiltin (FL_IWMMXT, "__builtin_arm_" NAME, (TYPE),       \
20815                 ARM_BUILTIN_ ## CODE)
20816
20817 #define iwmmx2_mbuiltin(NAME, TYPE, CODE)                      \
20818   def_mbuiltin (FL_IWMMXT2, "__builtin_arm_" NAME, (TYPE),     \
20819                ARM_BUILTIN_ ## CODE)
20820
20821   iwmmx_mbuiltin ("wzero", di_ftype_void, WZERO);
20822   iwmmx_mbuiltin ("setwcgr0", void_ftype_int, SETWCGR0);
20823   iwmmx_mbuiltin ("setwcgr1", void_ftype_int, SETWCGR1);
20824   iwmmx_mbuiltin ("setwcgr2", void_ftype_int, SETWCGR2);
20825   iwmmx_mbuiltin ("setwcgr3", void_ftype_int, SETWCGR3);
20826   iwmmx_mbuiltin ("getwcgr0", int_ftype_void, GETWCGR0);
20827   iwmmx_mbuiltin ("getwcgr1", int_ftype_void, GETWCGR1);
20828   iwmmx_mbuiltin ("getwcgr2", int_ftype_void, GETWCGR2);
20829   iwmmx_mbuiltin ("getwcgr3", int_ftype_void, GETWCGR3);
20830
20831   iwmmx_mbuiltin ("wsllh", v4hi_ftype_v4hi_di, WSLLH);
20832   iwmmx_mbuiltin ("wsllw", v2si_ftype_v2si_di, WSLLW);
20833   iwmmx_mbuiltin ("wslld", di_ftype_di_di, WSLLD);
20834   iwmmx_mbuiltin ("wsllhi", v4hi_ftype_v4hi_int, WSLLHI);
20835   iwmmx_mbuiltin ("wsllwi", v2si_ftype_v2si_int, WSLLWI);
20836   iwmmx_mbuiltin ("wslldi", di_ftype_di_int, WSLLDI);
20837
20838   iwmmx_mbuiltin ("wsrlh", v4hi_ftype_v4hi_di, WSRLH);
20839   iwmmx_mbuiltin ("wsrlw", v2si_ftype_v2si_di, WSRLW);
20840   iwmmx_mbuiltin ("wsrld", di_ftype_di_di, WSRLD);
20841   iwmmx_mbuiltin ("wsrlhi", v4hi_ftype_v4hi_int, WSRLHI);
20842   iwmmx_mbuiltin ("wsrlwi", v2si_ftype_v2si_int, WSRLWI);
20843   iwmmx_mbuiltin ("wsrldi", di_ftype_di_int, WSRLDI);
20844
20845   iwmmx_mbuiltin ("wsrah", v4hi_ftype_v4hi_di, WSRAH);
20846   iwmmx_mbuiltin ("wsraw", v2si_ftype_v2si_di, WSRAW);
20847   iwmmx_mbuiltin ("wsrad", di_ftype_di_di, WSRAD);
20848   iwmmx_mbuiltin ("wsrahi", v4hi_ftype_v4hi_int, WSRAHI);
20849   iwmmx_mbuiltin ("wsrawi", v2si_ftype_v2si_int, WSRAWI);
20850   iwmmx_mbuiltin ("wsradi", di_ftype_di_int, WSRADI);
20851
20852   iwmmx_mbuiltin ("wrorh", v4hi_ftype_v4hi_di, WRORH);
20853   iwmmx_mbuiltin ("wrorw", v2si_ftype_v2si_di, WRORW);
20854   iwmmx_mbuiltin ("wrord", di_ftype_di_di, WRORD);
20855   iwmmx_mbuiltin ("wrorhi", v4hi_ftype_v4hi_int, WRORHI);
20856   iwmmx_mbuiltin ("wrorwi", v2si_ftype_v2si_int, WRORWI);
20857   iwmmx_mbuiltin ("wrordi", di_ftype_di_int, WRORDI);
20858
20859   iwmmx_mbuiltin ("wshufh", v4hi_ftype_v4hi_int, WSHUFH);
20860
20861   iwmmx_mbuiltin ("wsadb", v2si_ftype_v2si_v8qi_v8qi, WSADB);
20862   iwmmx_mbuiltin ("wsadh", v2si_ftype_v2si_v4hi_v4hi, WSADH);
20863   iwmmx_mbuiltin ("wmadds", v2si_ftype_v4hi_v4hi, WMADDS);
20864   iwmmx2_mbuiltin ("wmaddsx", v2si_ftype_v4hi_v4hi, WMADDSX);
20865   iwmmx2_mbuiltin ("wmaddsn", v2si_ftype_v4hi_v4hi, WMADDSN);
20866   iwmmx_mbuiltin ("wmaddu", v2si_ftype_v4hi_v4hi, WMADDU);
20867   iwmmx2_mbuiltin ("wmaddux", v2si_ftype_v4hi_v4hi, WMADDUX);
20868   iwmmx2_mbuiltin ("wmaddun", v2si_ftype_v4hi_v4hi, WMADDUN);
20869   iwmmx_mbuiltin ("wsadbz", v2si_ftype_v8qi_v8qi, WSADBZ);
20870   iwmmx_mbuiltin ("wsadhz", v2si_ftype_v4hi_v4hi, WSADHZ);
20871
20872   iwmmx_mbuiltin ("textrmsb", int_ftype_v8qi_int, TEXTRMSB);
20873   iwmmx_mbuiltin ("textrmsh", int_ftype_v4hi_int, TEXTRMSH);
20874   iwmmx_mbuiltin ("textrmsw", int_ftype_v2si_int, TEXTRMSW);
20875   iwmmx_mbuiltin ("textrmub", int_ftype_v8qi_int, TEXTRMUB);
20876   iwmmx_mbuiltin ("textrmuh", int_ftype_v4hi_int, TEXTRMUH);
20877   iwmmx_mbuiltin ("textrmuw", int_ftype_v2si_int, TEXTRMUW);
20878   iwmmx_mbuiltin ("tinsrb", v8qi_ftype_v8qi_int_int, TINSRB);
20879   iwmmx_mbuiltin ("tinsrh", v4hi_ftype_v4hi_int_int, TINSRH);
20880   iwmmx_mbuiltin ("tinsrw", v2si_ftype_v2si_int_int, TINSRW);
20881
20882   iwmmx_mbuiltin ("waccb", di_ftype_v8qi, WACCB);
20883   iwmmx_mbuiltin ("wacch", di_ftype_v4hi, WACCH);
20884   iwmmx_mbuiltin ("waccw", di_ftype_v2si, WACCW);
20885
20886   iwmmx_mbuiltin ("tmovmskb", int_ftype_v8qi, TMOVMSKB);
20887   iwmmx_mbuiltin ("tmovmskh", int_ftype_v4hi, TMOVMSKH);
20888   iwmmx_mbuiltin ("tmovmskw", int_ftype_v2si, TMOVMSKW);
20889
20890   iwmmx2_mbuiltin ("waddbhusm", v8qi_ftype_v4hi_v8qi, WADDBHUSM);
20891   iwmmx2_mbuiltin ("waddbhusl", v8qi_ftype_v4hi_v8qi, WADDBHUSL);
20892
20893   iwmmx_mbuiltin ("wpackhss", v8qi_ftype_v4hi_v4hi, WPACKHSS);
20894   iwmmx_mbuiltin ("wpackhus", v8qi_ftype_v4hi_v4hi, WPACKHUS);
20895   iwmmx_mbuiltin ("wpackwus", v4hi_ftype_v2si_v2si, WPACKWUS);
20896   iwmmx_mbuiltin ("wpackwss", v4hi_ftype_v2si_v2si, WPACKWSS);
20897   iwmmx_mbuiltin ("wpackdus", v2si_ftype_di_di, WPACKDUS);
20898   iwmmx_mbuiltin ("wpackdss", v2si_ftype_di_di, WPACKDSS);
20899
20900   iwmmx_mbuiltin ("wunpckehub", v4hi_ftype_v8qi, WUNPCKEHUB);
20901   iwmmx_mbuiltin ("wunpckehuh", v2si_ftype_v4hi, WUNPCKEHUH);
20902   iwmmx_mbuiltin ("wunpckehuw", di_ftype_v2si, WUNPCKEHUW);
20903   iwmmx_mbuiltin ("wunpckehsb", v4hi_ftype_v8qi, WUNPCKEHSB);
20904   iwmmx_mbuiltin ("wunpckehsh", v2si_ftype_v4hi, WUNPCKEHSH);
20905   iwmmx_mbuiltin ("wunpckehsw", di_ftype_v2si, WUNPCKEHSW);
20906   iwmmx_mbuiltin ("wunpckelub", v4hi_ftype_v8qi, WUNPCKELUB);
20907   iwmmx_mbuiltin ("wunpckeluh", v2si_ftype_v4hi, WUNPCKELUH);
20908   iwmmx_mbuiltin ("wunpckeluw", di_ftype_v2si, WUNPCKELUW);
20909   iwmmx_mbuiltin ("wunpckelsb", v4hi_ftype_v8qi, WUNPCKELSB);
20910   iwmmx_mbuiltin ("wunpckelsh", v2si_ftype_v4hi, WUNPCKELSH);
20911   iwmmx_mbuiltin ("wunpckelsw", di_ftype_v2si, WUNPCKELSW);
20912
20913   iwmmx_mbuiltin ("wmacs", di_ftype_di_v4hi_v4hi, WMACS);
20914   iwmmx_mbuiltin ("wmacsz", di_ftype_v4hi_v4hi, WMACSZ);
20915   iwmmx_mbuiltin ("wmacu", di_ftype_di_v4hi_v4hi, WMACU);
20916   iwmmx_mbuiltin ("wmacuz", di_ftype_v4hi_v4hi, WMACUZ);
20917
20918   iwmmx_mbuiltin ("walign", v8qi_ftype_v8qi_v8qi_int, WALIGNI);
20919   iwmmx_mbuiltin ("tmia", di_ftype_di_int_int, TMIA);
20920   iwmmx_mbuiltin ("tmiaph", di_ftype_di_int_int, TMIAPH);
20921   iwmmx_mbuiltin ("tmiabb", di_ftype_di_int_int, TMIABB);
20922   iwmmx_mbuiltin ("tmiabt", di_ftype_di_int_int, TMIABT);
20923   iwmmx_mbuiltin ("tmiatb", di_ftype_di_int_int, TMIATB);
20924   iwmmx_mbuiltin ("tmiatt", di_ftype_di_int_int, TMIATT);
20925
20926   iwmmx2_mbuiltin ("wabsb", v8qi_ftype_v8qi, WABSB);
20927   iwmmx2_mbuiltin ("wabsh", v4hi_ftype_v4hi, WABSH);
20928   iwmmx2_mbuiltin ("wabsw", v2si_ftype_v2si, WABSW);
20929
20930   iwmmx2_mbuiltin ("wqmiabb", v2si_ftype_v2si_v4hi_v4hi, WQMIABB);
20931   iwmmx2_mbuiltin ("wqmiabt", v2si_ftype_v2si_v4hi_v4hi, WQMIABT);
20932   iwmmx2_mbuiltin ("wqmiatb", v2si_ftype_v2si_v4hi_v4hi, WQMIATB);
20933   iwmmx2_mbuiltin ("wqmiatt", v2si_ftype_v2si_v4hi_v4hi, WQMIATT);
20934
20935   iwmmx2_mbuiltin ("wqmiabbn", v2si_ftype_v2si_v4hi_v4hi, WQMIABBN);
20936   iwmmx2_mbuiltin ("wqmiabtn", v2si_ftype_v2si_v4hi_v4hi, WQMIABTN);
20937   iwmmx2_mbuiltin ("wqmiatbn", v2si_ftype_v2si_v4hi_v4hi, WQMIATBN);
20938   iwmmx2_mbuiltin ("wqmiattn", v2si_ftype_v2si_v4hi_v4hi, WQMIATTN);
20939
20940   iwmmx2_mbuiltin ("wmiabb", di_ftype_di_v4hi_v4hi, WMIABB);
20941   iwmmx2_mbuiltin ("wmiabt", di_ftype_di_v4hi_v4hi, WMIABT);
20942   iwmmx2_mbuiltin ("wmiatb", di_ftype_di_v4hi_v4hi, WMIATB);
20943   iwmmx2_mbuiltin ("wmiatt", di_ftype_di_v4hi_v4hi, WMIATT);
20944
20945   iwmmx2_mbuiltin ("wmiabbn", di_ftype_di_v4hi_v4hi, WMIABBN);
20946   iwmmx2_mbuiltin ("wmiabtn", di_ftype_di_v4hi_v4hi, WMIABTN);
20947   iwmmx2_mbuiltin ("wmiatbn", di_ftype_di_v4hi_v4hi, WMIATBN);
20948   iwmmx2_mbuiltin ("wmiattn", di_ftype_di_v4hi_v4hi, WMIATTN);
20949
20950   iwmmx2_mbuiltin ("wmiawbb", di_ftype_di_v2si_v2si, WMIAWBB);
20951   iwmmx2_mbuiltin ("wmiawbt", di_ftype_di_v2si_v2si, WMIAWBT);
20952   iwmmx2_mbuiltin ("wmiawtb", di_ftype_di_v2si_v2si, WMIAWTB);
20953   iwmmx2_mbuiltin ("wmiawtt", di_ftype_di_v2si_v2si, WMIAWTT);
20954
20955   iwmmx2_mbuiltin ("wmiawbbn", di_ftype_di_v2si_v2si, WMIAWBBN);
20956   iwmmx2_mbuiltin ("wmiawbtn", di_ftype_di_v2si_v2si, WMIAWBTN);
20957   iwmmx2_mbuiltin ("wmiawtbn", di_ftype_di_v2si_v2si, WMIAWTBN);
20958   iwmmx2_mbuiltin ("wmiawttn", di_ftype_di_v2si_v2si, WMIAWTTN);
20959
20960   iwmmx2_mbuiltin ("wmerge", di_ftype_di_di_int, WMERGE);
20961
20962   iwmmx_mbuiltin ("tbcstb", v8qi_ftype_char, TBCSTB);
20963   iwmmx_mbuiltin ("tbcsth", v4hi_ftype_short, TBCSTH);
20964   iwmmx_mbuiltin ("tbcstw", v2si_ftype_int, TBCSTW);
20965
20966 #undef iwmmx_mbuiltin
20967 #undef iwmmx2_mbuiltin
20968 }
20969
20970 static void
20971 arm_init_fp16_builtins (void)
20972 {
20973   tree fp16_type = make_node (REAL_TYPE);
20974   TYPE_PRECISION (fp16_type) = 16;
20975   layout_type (fp16_type);
20976   (*lang_hooks.types.register_builtin_type) (fp16_type, "__fp16");
20977 }
20978
20979 static void
20980 arm_init_builtins (void)
20981 {
20982   if (TARGET_REALLY_IWMMXT)
20983     arm_init_iwmmxt_builtins ();
20984
20985   if (TARGET_NEON)
20986     arm_init_neon_builtins ();
20987
20988   if (arm_fp16_format)
20989     arm_init_fp16_builtins ();
20990 }
20991
20992 /* Return the ARM builtin for CODE.  */
20993
20994 static tree
20995 arm_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
20996 {
20997   if (code >= ARM_BUILTIN_MAX)
20998     return error_mark_node;
20999
21000   return arm_builtin_decls[code];
21001 }
21002
21003 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
21004
21005 static const char *
21006 arm_invalid_parameter_type (const_tree t)
21007 {
21008   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
21009     return N_("function parameters cannot have __fp16 type");
21010   return NULL;
21011 }
21012
21013 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
21014
21015 static const char *
21016 arm_invalid_return_type (const_tree t)
21017 {
21018   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
21019     return N_("functions cannot return __fp16 type");
21020   return NULL;
21021 }
21022
21023 /* Implement TARGET_PROMOTED_TYPE.  */
21024
21025 static tree
21026 arm_promoted_type (const_tree t)
21027 {
21028   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
21029     return float_type_node;
21030   return NULL_TREE;
21031 }
21032
21033 /* Implement TARGET_CONVERT_TO_TYPE.
21034    Specifically, this hook implements the peculiarity of the ARM
21035    half-precision floating-point C semantics that requires conversions between
21036    __fp16 to or from double to do an intermediate conversion to float.  */
21037
21038 static tree
21039 arm_convert_to_type (tree type, tree expr)
21040 {
21041   tree fromtype = TREE_TYPE (expr);
21042   if (!SCALAR_FLOAT_TYPE_P (fromtype) || !SCALAR_FLOAT_TYPE_P (type))
21043     return NULL_TREE;
21044   if ((TYPE_PRECISION (fromtype) == 16 && TYPE_PRECISION (type) > 32)
21045       || (TYPE_PRECISION (type) == 16 && TYPE_PRECISION (fromtype) > 32))
21046     return convert (type, convert (float_type_node, expr));
21047   return NULL_TREE;
21048 }
21049
21050 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P.
21051    This simply adds HFmode as a supported mode; even though we don't
21052    implement arithmetic on this type directly, it's supported by
21053    optabs conversions, much the way the double-word arithmetic is
21054    special-cased in the default hook.  */
21055
21056 static bool
21057 arm_scalar_mode_supported_p (enum machine_mode mode)
21058 {
21059   if (mode == HFmode)
21060     return (arm_fp16_format != ARM_FP16_FORMAT_NONE);
21061   else if (ALL_FIXED_POINT_MODE_P (mode))
21062     return true;
21063   else
21064     return default_scalar_mode_supported_p (mode);
21065 }
21066
21067 /* Errors in the source file can cause expand_expr to return const0_rtx
21068    where we expect a vector.  To avoid crashing, use one of the vector
21069    clear instructions.  */
21070
21071 static rtx
21072 safe_vector_operand (rtx x, enum machine_mode mode)
21073 {
21074   if (x != const0_rtx)
21075     return x;
21076   x = gen_reg_rtx (mode);
21077
21078   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
21079                                : gen_rtx_SUBREG (DImode, x, 0)));
21080   return x;
21081 }
21082
21083 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
21084
21085 static rtx
21086 arm_expand_binop_builtin (enum insn_code icode,
21087                           tree exp, rtx target)
21088 {
21089   rtx pat;
21090   tree arg0 = CALL_EXPR_ARG (exp, 0);
21091   tree arg1 = CALL_EXPR_ARG (exp, 1);
21092   rtx op0 = expand_normal (arg0);
21093   rtx op1 = expand_normal (arg1);
21094   enum machine_mode tmode = insn_data[icode].operand[0].mode;
21095   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
21096   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
21097
21098   if (VECTOR_MODE_P (mode0))
21099     op0 = safe_vector_operand (op0, mode0);
21100   if (VECTOR_MODE_P (mode1))
21101     op1 = safe_vector_operand (op1, mode1);
21102
21103   if (! target
21104       || GET_MODE (target) != tmode
21105       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
21106     target = gen_reg_rtx (tmode);
21107
21108   gcc_assert ((GET_MODE (op0) == mode0 || GET_MODE (op0) == VOIDmode)
21109               && (GET_MODE (op1) == mode1 || GET_MODE (op1) == VOIDmode));
21110
21111   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
21112     op0 = copy_to_mode_reg (mode0, op0);
21113   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
21114     op1 = copy_to_mode_reg (mode1, op1);
21115
21116   pat = GEN_FCN (icode) (target, op0, op1);
21117   if (! pat)
21118     return 0;
21119   emit_insn (pat);
21120   return target;
21121 }
21122
21123 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
21124
21125 static rtx
21126 arm_expand_unop_builtin (enum insn_code icode,
21127                          tree exp, rtx target, int do_load)
21128 {
21129   rtx pat;
21130   tree arg0 = CALL_EXPR_ARG (exp, 0);
21131   rtx op0 = expand_normal (arg0);
21132   enum machine_mode tmode = insn_data[icode].operand[0].mode;
21133   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
21134
21135   if (! target
21136       || GET_MODE (target) != tmode
21137       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
21138     target = gen_reg_rtx (tmode);
21139   if (do_load)
21140     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
21141   else
21142     {
21143       if (VECTOR_MODE_P (mode0))
21144         op0 = safe_vector_operand (op0, mode0);
21145
21146       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
21147         op0 = copy_to_mode_reg (mode0, op0);
21148     }
21149
21150   pat = GEN_FCN (icode) (target, op0);
21151   if (! pat)
21152     return 0;
21153   emit_insn (pat);
21154   return target;
21155 }
21156
21157 typedef enum {
21158   NEON_ARG_COPY_TO_REG,
21159   NEON_ARG_CONSTANT,
21160   NEON_ARG_MEMORY,
21161   NEON_ARG_STOP
21162 } builtin_arg;
21163
21164 #define NEON_MAX_BUILTIN_ARGS 5
21165
21166 /* EXP is a pointer argument to a Neon load or store intrinsic.  Derive
21167    and return an expression for the accessed memory.
21168
21169    The intrinsic function operates on a block of registers that has
21170    mode REG_MODE.  This block contains vectors of type TYPE_MODE.  The
21171    function references the memory at EXP of type TYPE and in mode
21172    MEM_MODE; this mode may be BLKmode if no more suitable mode is
21173    available.  */
21174
21175 static tree
21176 neon_dereference_pointer (tree exp, tree type, enum machine_mode mem_mode,
21177                           enum machine_mode reg_mode,
21178                           neon_builtin_type_mode type_mode)
21179 {
21180   HOST_WIDE_INT reg_size, vector_size, nvectors, nelems;
21181   tree elem_type, upper_bound, array_type;
21182
21183   /* Work out the size of the register block in bytes.  */
21184   reg_size = GET_MODE_SIZE (reg_mode);
21185
21186   /* Work out the size of each vector in bytes.  */
21187   gcc_assert (TYPE_MODE_BIT (type_mode) & (TB_DREG | TB_QREG));
21188   vector_size = (TYPE_MODE_BIT (type_mode) & TB_QREG ? 16 : 8);
21189
21190   /* Work out how many vectors there are.  */
21191   gcc_assert (reg_size % vector_size == 0);
21192   nvectors = reg_size / vector_size;
21193
21194   /* Work out the type of each element.  */
21195   gcc_assert (POINTER_TYPE_P (type));
21196   elem_type = TREE_TYPE (type);
21197
21198   /* Work out how many elements are being loaded or stored.
21199      MEM_MODE == REG_MODE implies a one-to-one mapping between register
21200      and memory elements; anything else implies a lane load or store.  */
21201   if (mem_mode == reg_mode)
21202     nelems = vector_size * nvectors / int_size_in_bytes (elem_type);
21203   else
21204     nelems = nvectors;
21205
21206   /* Create a type that describes the full access.  */
21207   upper_bound = build_int_cst (size_type_node, nelems - 1);
21208   array_type = build_array_type (elem_type, build_index_type (upper_bound));
21209
21210   /* Dereference EXP using that type.  */
21211   return fold_build2 (MEM_REF, array_type, exp,
21212                       build_int_cst (build_pointer_type (array_type), 0));
21213 }
21214
21215 /* Expand a Neon builtin.  */
21216 static rtx
21217 arm_expand_neon_args (rtx target, int icode, int have_retval,
21218                       neon_builtin_type_mode type_mode,
21219                       tree exp, int fcode, ...)
21220 {
21221   va_list ap;
21222   rtx pat;
21223   tree arg[NEON_MAX_BUILTIN_ARGS];
21224   rtx op[NEON_MAX_BUILTIN_ARGS];
21225   tree arg_type;
21226   tree formals;
21227   enum machine_mode tmode = insn_data[icode].operand[0].mode;
21228   enum machine_mode mode[NEON_MAX_BUILTIN_ARGS];
21229   enum machine_mode other_mode;
21230   int argc = 0;
21231   int opno;
21232
21233   if (have_retval
21234       && (!target
21235           || GET_MODE (target) != tmode
21236           || !(*insn_data[icode].operand[0].predicate) (target, tmode)))
21237     target = gen_reg_rtx (tmode);
21238
21239   va_start (ap, fcode);
21240
21241   formals = TYPE_ARG_TYPES (TREE_TYPE (arm_builtin_decls[fcode]));
21242
21243   for (;;)
21244     {
21245       builtin_arg thisarg = (builtin_arg) va_arg (ap, int);
21246
21247       if (thisarg == NEON_ARG_STOP)
21248         break;
21249       else
21250         {
21251           opno = argc + have_retval;
21252           mode[argc] = insn_data[icode].operand[opno].mode;
21253           arg[argc] = CALL_EXPR_ARG (exp, argc);
21254           arg_type = TREE_VALUE (formals);
21255           if (thisarg == NEON_ARG_MEMORY)
21256             {
21257               other_mode = insn_data[icode].operand[1 - opno].mode;
21258               arg[argc] = neon_dereference_pointer (arg[argc], arg_type,
21259                                                     mode[argc], other_mode,
21260                                                     type_mode);
21261             }
21262
21263           op[argc] = expand_normal (arg[argc]);
21264
21265           switch (thisarg)
21266             {
21267             case NEON_ARG_COPY_TO_REG:
21268               /*gcc_assert (GET_MODE (op[argc]) == mode[argc]);*/
21269               if (!(*insn_data[icode].operand[opno].predicate)
21270                      (op[argc], mode[argc]))
21271                 op[argc] = copy_to_mode_reg (mode[argc], op[argc]);
21272               break;
21273
21274             case NEON_ARG_CONSTANT:
21275               /* FIXME: This error message is somewhat unhelpful.  */
21276               if (!(*insn_data[icode].operand[opno].predicate)
21277                     (op[argc], mode[argc]))
21278                 error ("argument must be a constant");
21279               break;
21280
21281             case NEON_ARG_MEMORY:
21282               gcc_assert (MEM_P (op[argc]));
21283               PUT_MODE (op[argc], mode[argc]);
21284               /* ??? arm_neon.h uses the same built-in functions for signed
21285                  and unsigned accesses, casting where necessary.  This isn't
21286                  alias safe.  */
21287               set_mem_alias_set (op[argc], 0);
21288               if (!(*insn_data[icode].operand[opno].predicate)
21289                     (op[argc], mode[argc]))
21290                 op[argc] = (replace_equiv_address
21291                             (op[argc], force_reg (Pmode, XEXP (op[argc], 0))));
21292               break;
21293
21294             case NEON_ARG_STOP:
21295               gcc_unreachable ();
21296             }
21297
21298           argc++;
21299           formals = TREE_CHAIN (formals);
21300         }
21301     }
21302
21303   va_end (ap);
21304
21305   if (have_retval)
21306     switch (argc)
21307       {
21308       case 1:
21309         pat = GEN_FCN (icode) (target, op[0]);
21310         break;
21311
21312       case 2:
21313         pat = GEN_FCN (icode) (target, op[0], op[1]);
21314         break;
21315
21316       case 3:
21317         pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
21318         break;
21319
21320       case 4:
21321         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
21322         break;
21323
21324       case 5:
21325         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
21326         break;
21327
21328       default:
21329         gcc_unreachable ();
21330       }
21331   else
21332     switch (argc)
21333       {
21334       case 1:
21335         pat = GEN_FCN (icode) (op[0]);
21336         break;
21337
21338       case 2:
21339         pat = GEN_FCN (icode) (op[0], op[1]);
21340         break;
21341
21342       case 3:
21343         pat = GEN_FCN (icode) (op[0], op[1], op[2]);
21344         break;
21345
21346       case 4:
21347         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
21348         break;
21349
21350       case 5:
21351         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
21352         break;
21353
21354       default:
21355         gcc_unreachable ();
21356       }
21357
21358   if (!pat)
21359     return 0;
21360
21361   emit_insn (pat);
21362
21363   return target;
21364 }
21365
21366 /* Expand a Neon builtin. These are "special" because they don't have symbolic
21367    constants defined per-instruction or per instruction-variant. Instead, the
21368    required info is looked up in the table neon_builtin_data.  */
21369 static rtx
21370 arm_expand_neon_builtin (int fcode, tree exp, rtx target)
21371 {
21372   neon_builtin_datum *d = &neon_builtin_data[fcode - ARM_BUILTIN_NEON_BASE];
21373   neon_itype itype = d->itype;
21374   enum insn_code icode = d->code;
21375   neon_builtin_type_mode type_mode = d->mode;
21376
21377   switch (itype)
21378     {
21379     case NEON_UNOP:
21380     case NEON_CONVERT:
21381     case NEON_DUPLANE:
21382       return arm_expand_neon_args (target, icode, 1, type_mode, exp, fcode,
21383         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_STOP);
21384
21385     case NEON_BINOP:
21386     case NEON_SETLANE:
21387     case NEON_SCALARMUL:
21388     case NEON_SCALARMULL:
21389     case NEON_SCALARMULH:
21390     case NEON_SHIFTINSERT:
21391     case NEON_LOGICBINOP:
21392       return arm_expand_neon_args (target, icode, 1, type_mode, exp, fcode,
21393         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
21394         NEON_ARG_STOP);
21395
21396     case NEON_TERNOP:
21397       return arm_expand_neon_args (target, icode, 1, type_mode, exp, fcode,
21398         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
21399         NEON_ARG_CONSTANT, NEON_ARG_STOP);
21400
21401     case NEON_GETLANE:
21402     case NEON_FIXCONV:
21403     case NEON_SHIFTIMM:
21404       return arm_expand_neon_args (target, icode, 1, type_mode, exp, fcode,
21405         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_CONSTANT,
21406         NEON_ARG_STOP);
21407
21408     case NEON_CREATE:
21409       return arm_expand_neon_args (target, icode, 1, type_mode, exp, fcode,
21410         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
21411
21412     case NEON_DUP:
21413     case NEON_RINT:
21414     case NEON_SPLIT:
21415     case NEON_REINTERP:
21416       return arm_expand_neon_args (target, icode, 1, type_mode, exp, fcode,
21417         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
21418
21419     case NEON_COMBINE:
21420     case NEON_VTBL:
21421       return arm_expand_neon_args (target, icode, 1, type_mode, exp, fcode,
21422         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
21423
21424     case NEON_RESULTPAIR:
21425       return arm_expand_neon_args (target, icode, 0, type_mode, exp, fcode,
21426         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
21427         NEON_ARG_STOP);
21428
21429     case NEON_LANEMUL:
21430     case NEON_LANEMULL:
21431     case NEON_LANEMULH:
21432       return arm_expand_neon_args (target, icode, 1, type_mode, exp, fcode,
21433         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
21434         NEON_ARG_CONSTANT, NEON_ARG_STOP);
21435
21436     case NEON_LANEMAC:
21437       return arm_expand_neon_args (target, icode, 1, type_mode, exp, fcode,
21438         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
21439         NEON_ARG_CONSTANT, NEON_ARG_CONSTANT, NEON_ARG_STOP);
21440
21441     case NEON_SHIFTACC:
21442       return arm_expand_neon_args (target, icode, 1, type_mode, exp, fcode,
21443         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
21444         NEON_ARG_CONSTANT, NEON_ARG_STOP);
21445
21446     case NEON_SCALARMAC:
21447       return arm_expand_neon_args (target, icode, 1, type_mode, exp, fcode,
21448         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
21449         NEON_ARG_CONSTANT, NEON_ARG_STOP);
21450
21451     case NEON_SELECT:
21452     case NEON_VTBX:
21453       return arm_expand_neon_args (target, icode, 1, type_mode, exp, fcode,
21454         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
21455         NEON_ARG_STOP);
21456
21457     case NEON_LOAD1:
21458     case NEON_LOADSTRUCT:
21459       return arm_expand_neon_args (target, icode, 1, type_mode, exp, fcode,
21460         NEON_ARG_MEMORY, NEON_ARG_STOP);
21461
21462     case NEON_LOAD1LANE:
21463     case NEON_LOADSTRUCTLANE:
21464       return arm_expand_neon_args (target, icode, 1, type_mode, exp, fcode,
21465         NEON_ARG_MEMORY, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
21466         NEON_ARG_STOP);
21467
21468     case NEON_STORE1:
21469     case NEON_STORESTRUCT:
21470       return arm_expand_neon_args (target, icode, 0, type_mode, exp, fcode,
21471         NEON_ARG_MEMORY, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
21472
21473     case NEON_STORE1LANE:
21474     case NEON_STORESTRUCTLANE:
21475       return arm_expand_neon_args (target, icode, 0, type_mode, exp, fcode,
21476         NEON_ARG_MEMORY, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
21477         NEON_ARG_STOP);
21478     }
21479
21480   gcc_unreachable ();
21481 }
21482
21483 /* Emit code to reinterpret one Neon type as another, without altering bits.  */
21484 void
21485 neon_reinterpret (rtx dest, rtx src)
21486 {
21487   emit_move_insn (dest, gen_lowpart (GET_MODE (dest), src));
21488 }
21489
21490 /* Emit code to place a Neon pair result in memory locations (with equal
21491    registers).  */
21492 void
21493 neon_emit_pair_result_insn (enum machine_mode mode,
21494                             rtx (*intfn) (rtx, rtx, rtx, rtx), rtx destaddr,
21495                             rtx op1, rtx op2)
21496 {
21497   rtx mem = gen_rtx_MEM (mode, destaddr);
21498   rtx tmp1 = gen_reg_rtx (mode);
21499   rtx tmp2 = gen_reg_rtx (mode);
21500
21501   emit_insn (intfn (tmp1, op1, op2, tmp2));
21502
21503   emit_move_insn (mem, tmp1);
21504   mem = adjust_address (mem, mode, GET_MODE_SIZE (mode));
21505   emit_move_insn (mem, tmp2);
21506 }
21507
21508 /* Set up OPERANDS for a register copy from SRC to DEST, taking care
21509    not to early-clobber SRC registers in the process.
21510
21511    We assume that the operands described by SRC and DEST represent a
21512    decomposed copy of OPERANDS[1] into OPERANDS[0].  COUNT is the
21513    number of components into which the copy has been decomposed.  */
21514 void
21515 neon_disambiguate_copy (rtx *operands, rtx *dest, rtx *src, unsigned int count)
21516 {
21517   unsigned int i;
21518
21519   if (!reg_overlap_mentioned_p (operands[0], operands[1])
21520       || REGNO (operands[0]) < REGNO (operands[1]))
21521     {
21522       for (i = 0; i < count; i++)
21523         {
21524           operands[2 * i] = dest[i];
21525           operands[2 * i + 1] = src[i];
21526         }
21527     }
21528   else
21529     {
21530       for (i = 0; i < count; i++)
21531         {
21532           operands[2 * i] = dest[count - i - 1];
21533           operands[2 * i + 1] = src[count - i - 1];
21534         }
21535     }
21536 }
21537
21538 /* Split operands into moves from op[1] + op[2] into op[0].  */
21539
21540 void
21541 neon_split_vcombine (rtx operands[3])
21542 {
21543   unsigned int dest = REGNO (operands[0]);
21544   unsigned int src1 = REGNO (operands[1]);
21545   unsigned int src2 = REGNO (operands[2]);
21546   enum machine_mode halfmode = GET_MODE (operands[1]);
21547   unsigned int halfregs = HARD_REGNO_NREGS (src1, halfmode);
21548   rtx destlo, desthi;
21549
21550   if (src1 == dest && src2 == dest + halfregs)
21551     {
21552       /* No-op move.  Can't split to nothing; emit something.  */
21553       emit_note (NOTE_INSN_DELETED);
21554       return;
21555     }
21556
21557   /* Preserve register attributes for variable tracking.  */
21558   destlo = gen_rtx_REG_offset (operands[0], halfmode, dest, 0);
21559   desthi = gen_rtx_REG_offset (operands[0], halfmode, dest + halfregs,
21560                                GET_MODE_SIZE (halfmode));
21561
21562   /* Special case of reversed high/low parts.  Use VSWP.  */
21563   if (src2 == dest && src1 == dest + halfregs)
21564     {
21565       rtx x = gen_rtx_SET (VOIDmode, destlo, operands[1]);
21566       rtx y = gen_rtx_SET (VOIDmode, desthi, operands[2]);
21567       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x, y)));
21568       return;
21569     }
21570
21571   if (!reg_overlap_mentioned_p (operands[2], destlo))
21572     {
21573       /* Try to avoid unnecessary moves if part of the result
21574          is in the right place already.  */
21575       if (src1 != dest)
21576         emit_move_insn (destlo, operands[1]);
21577       if (src2 != dest + halfregs)
21578         emit_move_insn (desthi, operands[2]);
21579     }
21580   else
21581     {
21582       if (src2 != dest + halfregs)
21583         emit_move_insn (desthi, operands[2]);
21584       if (src1 != dest)
21585         emit_move_insn (destlo, operands[1]);
21586     }
21587 }
21588
21589 /* Expand an expression EXP that calls a built-in function,
21590    with result going to TARGET if that's convenient
21591    (and in mode MODE if that's convenient).
21592    SUBTARGET may be used as the target for computing one of EXP's operands.
21593    IGNORE is nonzero if the value is to be ignored.  */
21594
21595 static rtx
21596 arm_expand_builtin (tree exp,
21597                     rtx target,
21598                     rtx subtarget ATTRIBUTE_UNUSED,
21599                     enum machine_mode mode ATTRIBUTE_UNUSED,
21600                     int ignore ATTRIBUTE_UNUSED)
21601 {
21602   const struct builtin_description * d;
21603   enum insn_code    icode;
21604   tree              fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
21605   tree              arg0;
21606   tree              arg1;
21607   tree              arg2;
21608   rtx               op0;
21609   rtx               op1;
21610   rtx               op2;
21611   rtx               pat;
21612   int               fcode = DECL_FUNCTION_CODE (fndecl);
21613   size_t            i;
21614   enum machine_mode tmode;
21615   enum machine_mode mode0;
21616   enum machine_mode mode1;
21617   enum machine_mode mode2;
21618   int opint;
21619   int selector;
21620   int mask;
21621   int imm;
21622
21623   if (fcode >= ARM_BUILTIN_NEON_BASE)
21624     return arm_expand_neon_builtin (fcode, exp, target);
21625
21626   switch (fcode)
21627     {
21628     case ARM_BUILTIN_TEXTRMSB:
21629     case ARM_BUILTIN_TEXTRMUB:
21630     case ARM_BUILTIN_TEXTRMSH:
21631     case ARM_BUILTIN_TEXTRMUH:
21632     case ARM_BUILTIN_TEXTRMSW:
21633     case ARM_BUILTIN_TEXTRMUW:
21634       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
21635                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
21636                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
21637                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
21638                : CODE_FOR_iwmmxt_textrmw);
21639
21640       arg0 = CALL_EXPR_ARG (exp, 0);
21641       arg1 = CALL_EXPR_ARG (exp, 1);
21642       op0 = expand_normal (arg0);
21643       op1 = expand_normal (arg1);
21644       tmode = insn_data[icode].operand[0].mode;
21645       mode0 = insn_data[icode].operand[1].mode;
21646       mode1 = insn_data[icode].operand[2].mode;
21647
21648       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
21649         op0 = copy_to_mode_reg (mode0, op0);
21650       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
21651         {
21652           /* @@@ better error message */
21653           error ("selector must be an immediate");
21654           return gen_reg_rtx (tmode);
21655         }
21656
21657       opint = INTVAL (op1);
21658       if (fcode == ARM_BUILTIN_TEXTRMSB || fcode == ARM_BUILTIN_TEXTRMUB)
21659         {
21660           if (opint > 7 || opint < 0)
21661             error ("the range of selector should be in 0 to 7");
21662         }
21663       else if (fcode == ARM_BUILTIN_TEXTRMSH || fcode == ARM_BUILTIN_TEXTRMUH)
21664         {
21665           if (opint > 3 || opint < 0)
21666             error ("the range of selector should be in 0 to 3");
21667         }
21668       else /* ARM_BUILTIN_TEXTRMSW || ARM_BUILTIN_TEXTRMUW.  */
21669         {
21670           if (opint > 1 || opint < 0)
21671             error ("the range of selector should be in 0 to 1");
21672         }
21673
21674       if (target == 0
21675           || GET_MODE (target) != tmode
21676           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
21677         target = gen_reg_rtx (tmode);
21678       pat = GEN_FCN (icode) (target, op0, op1);
21679       if (! pat)
21680         return 0;
21681       emit_insn (pat);
21682       return target;
21683
21684     case ARM_BUILTIN_WALIGNI:
21685       /* If op2 is immediate, call walighi, else call walighr.  */
21686       arg0 = CALL_EXPR_ARG (exp, 0);
21687       arg1 = CALL_EXPR_ARG (exp, 1);
21688       arg2 = CALL_EXPR_ARG (exp, 2);
21689       op0 = expand_normal (arg0);
21690       op1 = expand_normal (arg1);
21691       op2 = expand_normal (arg2);
21692       if (CONST_INT_P (op2))
21693         {
21694           icode = CODE_FOR_iwmmxt_waligni;
21695           tmode = insn_data[icode].operand[0].mode;
21696           mode0 = insn_data[icode].operand[1].mode;
21697           mode1 = insn_data[icode].operand[2].mode;
21698           mode2 = insn_data[icode].operand[3].mode;
21699           if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
21700             op0 = copy_to_mode_reg (mode0, op0);
21701           if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
21702             op1 = copy_to_mode_reg (mode1, op1);
21703           gcc_assert ((*insn_data[icode].operand[3].predicate) (op2, mode2));
21704           selector = INTVAL (op2);
21705           if (selector > 7 || selector < 0)
21706             error ("the range of selector should be in 0 to 7");
21707         }
21708       else
21709         {
21710           icode = CODE_FOR_iwmmxt_walignr;
21711           tmode = insn_data[icode].operand[0].mode;
21712           mode0 = insn_data[icode].operand[1].mode;
21713           mode1 = insn_data[icode].operand[2].mode;
21714           mode2 = insn_data[icode].operand[3].mode;
21715           if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
21716             op0 = copy_to_mode_reg (mode0, op0);
21717           if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
21718             op1 = copy_to_mode_reg (mode1, op1);
21719           if (!(*insn_data[icode].operand[3].predicate) (op2, mode2))
21720             op2 = copy_to_mode_reg (mode2, op2);
21721         }
21722       if (target == 0
21723           || GET_MODE (target) != tmode
21724           || !(*insn_data[icode].operand[0].predicate) (target, tmode))
21725         target = gen_reg_rtx (tmode);
21726       pat = GEN_FCN (icode) (target, op0, op1, op2);
21727       if (!pat)
21728         return 0;
21729       emit_insn (pat);
21730       return target;
21731
21732     case ARM_BUILTIN_TINSRB:
21733     case ARM_BUILTIN_TINSRH:
21734     case ARM_BUILTIN_TINSRW:
21735     case ARM_BUILTIN_WMERGE:
21736       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
21737                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
21738                : fcode == ARM_BUILTIN_WMERGE ? CODE_FOR_iwmmxt_wmerge
21739                : CODE_FOR_iwmmxt_tinsrw);
21740       arg0 = CALL_EXPR_ARG (exp, 0);
21741       arg1 = CALL_EXPR_ARG (exp, 1);
21742       arg2 = CALL_EXPR_ARG (exp, 2);
21743       op0 = expand_normal (arg0);
21744       op1 = expand_normal (arg1);
21745       op2 = expand_normal (arg2);
21746       tmode = insn_data[icode].operand[0].mode;
21747       mode0 = insn_data[icode].operand[1].mode;
21748       mode1 = insn_data[icode].operand[2].mode;
21749       mode2 = insn_data[icode].operand[3].mode;
21750
21751       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
21752         op0 = copy_to_mode_reg (mode0, op0);
21753       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
21754         op1 = copy_to_mode_reg (mode1, op1);
21755       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
21756         {
21757           error ("selector must be an immediate");
21758           return const0_rtx;
21759         }
21760       if (icode == CODE_FOR_iwmmxt_wmerge)
21761         {
21762           selector = INTVAL (op2);
21763           if (selector > 7 || selector < 0)
21764             error ("the range of selector should be in 0 to 7");
21765         }
21766       if ((icode == CODE_FOR_iwmmxt_tinsrb)
21767           || (icode == CODE_FOR_iwmmxt_tinsrh)
21768           || (icode == CODE_FOR_iwmmxt_tinsrw))
21769         {
21770           mask = 0x01;
21771           selector= INTVAL (op2);
21772           if (icode == CODE_FOR_iwmmxt_tinsrb && (selector < 0 || selector > 7))
21773             error ("the range of selector should be in 0 to 7");
21774           else if (icode == CODE_FOR_iwmmxt_tinsrh && (selector < 0 ||selector > 3))
21775             error ("the range of selector should be in 0 to 3");
21776           else if (icode == CODE_FOR_iwmmxt_tinsrw && (selector < 0 ||selector > 1))
21777             error ("the range of selector should be in 0 to 1");
21778           mask <<= selector;
21779           op2 = GEN_INT (mask);
21780         }
21781       if (target == 0
21782           || GET_MODE (target) != tmode
21783           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
21784         target = gen_reg_rtx (tmode);
21785       pat = GEN_FCN (icode) (target, op0, op1, op2);
21786       if (! pat)
21787         return 0;
21788       emit_insn (pat);
21789       return target;
21790
21791     case ARM_BUILTIN_SETWCGR0:
21792     case ARM_BUILTIN_SETWCGR1:
21793     case ARM_BUILTIN_SETWCGR2:
21794     case ARM_BUILTIN_SETWCGR3:
21795       icode = (fcode == ARM_BUILTIN_SETWCGR0 ? CODE_FOR_iwmmxt_setwcgr0
21796                : fcode == ARM_BUILTIN_SETWCGR1 ? CODE_FOR_iwmmxt_setwcgr1
21797                : fcode == ARM_BUILTIN_SETWCGR2 ? CODE_FOR_iwmmxt_setwcgr2
21798                : CODE_FOR_iwmmxt_setwcgr3);
21799       arg0 = CALL_EXPR_ARG (exp, 0);
21800       op0 = expand_normal (arg0);
21801       mode0 = insn_data[icode].operand[0].mode;
21802       if (!(*insn_data[icode].operand[0].predicate) (op0, mode0))
21803         op0 = copy_to_mode_reg (mode0, op0);
21804       pat = GEN_FCN (icode) (op0);
21805       if (!pat)
21806         return 0;
21807       emit_insn (pat);
21808       return 0;
21809
21810     case ARM_BUILTIN_GETWCGR0:
21811     case ARM_BUILTIN_GETWCGR1:
21812     case ARM_BUILTIN_GETWCGR2:
21813     case ARM_BUILTIN_GETWCGR3:
21814       icode = (fcode == ARM_BUILTIN_GETWCGR0 ? CODE_FOR_iwmmxt_getwcgr0
21815                : fcode == ARM_BUILTIN_GETWCGR1 ? CODE_FOR_iwmmxt_getwcgr1
21816                : fcode == ARM_BUILTIN_GETWCGR2 ? CODE_FOR_iwmmxt_getwcgr2
21817                : CODE_FOR_iwmmxt_getwcgr3);
21818       tmode = insn_data[icode].operand[0].mode;
21819       if (target == 0
21820           || GET_MODE (target) != tmode
21821           || !(*insn_data[icode].operand[0].predicate) (target, tmode))
21822         target = gen_reg_rtx (tmode);
21823       pat = GEN_FCN (icode) (target);
21824       if (!pat)
21825         return 0;
21826       emit_insn (pat);
21827       return target;
21828
21829     case ARM_BUILTIN_WSHUFH:
21830       icode = CODE_FOR_iwmmxt_wshufh;
21831       arg0 = CALL_EXPR_ARG (exp, 0);
21832       arg1 = CALL_EXPR_ARG (exp, 1);
21833       op0 = expand_normal (arg0);
21834       op1 = expand_normal (arg1);
21835       tmode = insn_data[icode].operand[0].mode;
21836       mode1 = insn_data[icode].operand[1].mode;
21837       mode2 = insn_data[icode].operand[2].mode;
21838
21839       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
21840         op0 = copy_to_mode_reg (mode1, op0);
21841       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
21842         {
21843           error ("mask must be an immediate");
21844           return const0_rtx;
21845         }
21846       selector = INTVAL (op1);
21847       if (selector < 0 || selector > 255)
21848         error ("the range of mask should be in 0 to 255");
21849       if (target == 0
21850           || GET_MODE (target) != tmode
21851           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
21852         target = gen_reg_rtx (tmode);
21853       pat = GEN_FCN (icode) (target, op0, op1);
21854       if (! pat)
21855         return 0;
21856       emit_insn (pat);
21857       return target;
21858
21859     case ARM_BUILTIN_WMADDS:
21860       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wmadds, exp, target);
21861     case ARM_BUILTIN_WMADDSX:
21862       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wmaddsx, exp, target);
21863     case ARM_BUILTIN_WMADDSN:
21864       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wmaddsn, exp, target);
21865     case ARM_BUILTIN_WMADDU:
21866       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wmaddu, exp, target);
21867     case ARM_BUILTIN_WMADDUX:
21868       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wmaddux, exp, target);
21869     case ARM_BUILTIN_WMADDUN:
21870       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wmaddun, exp, target);
21871     case ARM_BUILTIN_WSADBZ:
21872       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
21873     case ARM_BUILTIN_WSADHZ:
21874       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
21875
21876       /* Several three-argument builtins.  */
21877     case ARM_BUILTIN_WMACS:
21878     case ARM_BUILTIN_WMACU:
21879     case ARM_BUILTIN_TMIA:
21880     case ARM_BUILTIN_TMIAPH:
21881     case ARM_BUILTIN_TMIATT:
21882     case ARM_BUILTIN_TMIATB:
21883     case ARM_BUILTIN_TMIABT:
21884     case ARM_BUILTIN_TMIABB:
21885     case ARM_BUILTIN_WQMIABB:
21886     case ARM_BUILTIN_WQMIABT:
21887     case ARM_BUILTIN_WQMIATB:
21888     case ARM_BUILTIN_WQMIATT:
21889     case ARM_BUILTIN_WQMIABBN:
21890     case ARM_BUILTIN_WQMIABTN:
21891     case ARM_BUILTIN_WQMIATBN:
21892     case ARM_BUILTIN_WQMIATTN:
21893     case ARM_BUILTIN_WMIABB:
21894     case ARM_BUILTIN_WMIABT:
21895     case ARM_BUILTIN_WMIATB:
21896     case ARM_BUILTIN_WMIATT:
21897     case ARM_BUILTIN_WMIABBN:
21898     case ARM_BUILTIN_WMIABTN:
21899     case ARM_BUILTIN_WMIATBN:
21900     case ARM_BUILTIN_WMIATTN:
21901     case ARM_BUILTIN_WMIAWBB:
21902     case ARM_BUILTIN_WMIAWBT:
21903     case ARM_BUILTIN_WMIAWTB:
21904     case ARM_BUILTIN_WMIAWTT:
21905     case ARM_BUILTIN_WMIAWBBN:
21906     case ARM_BUILTIN_WMIAWBTN:
21907     case ARM_BUILTIN_WMIAWTBN:
21908     case ARM_BUILTIN_WMIAWTTN:
21909     case ARM_BUILTIN_WSADB:
21910     case ARM_BUILTIN_WSADH:
21911       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
21912                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
21913                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
21914                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
21915                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
21916                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
21917                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
21918                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
21919                : fcode == ARM_BUILTIN_WQMIABB ? CODE_FOR_iwmmxt_wqmiabb
21920                : fcode == ARM_BUILTIN_WQMIABT ? CODE_FOR_iwmmxt_wqmiabt
21921                : fcode == ARM_BUILTIN_WQMIATB ? CODE_FOR_iwmmxt_wqmiatb
21922                : fcode == ARM_BUILTIN_WQMIATT ? CODE_FOR_iwmmxt_wqmiatt
21923                : fcode == ARM_BUILTIN_WQMIABBN ? CODE_FOR_iwmmxt_wqmiabbn
21924                : fcode == ARM_BUILTIN_WQMIABTN ? CODE_FOR_iwmmxt_wqmiabtn
21925                : fcode == ARM_BUILTIN_WQMIATBN ? CODE_FOR_iwmmxt_wqmiatbn
21926                : fcode == ARM_BUILTIN_WQMIATTN ? CODE_FOR_iwmmxt_wqmiattn
21927                : fcode == ARM_BUILTIN_WMIABB ? CODE_FOR_iwmmxt_wmiabb
21928                : fcode == ARM_BUILTIN_WMIABT ? CODE_FOR_iwmmxt_wmiabt
21929                : fcode == ARM_BUILTIN_WMIATB ? CODE_FOR_iwmmxt_wmiatb
21930                : fcode == ARM_BUILTIN_WMIATT ? CODE_FOR_iwmmxt_wmiatt
21931                : fcode == ARM_BUILTIN_WMIABBN ? CODE_FOR_iwmmxt_wmiabbn
21932                : fcode == ARM_BUILTIN_WMIABTN ? CODE_FOR_iwmmxt_wmiabtn
21933                : fcode == ARM_BUILTIN_WMIATBN ? CODE_FOR_iwmmxt_wmiatbn
21934                : fcode == ARM_BUILTIN_WMIATTN ? CODE_FOR_iwmmxt_wmiattn
21935                : fcode == ARM_BUILTIN_WMIAWBB ? CODE_FOR_iwmmxt_wmiawbb
21936                : fcode == ARM_BUILTIN_WMIAWBT ? CODE_FOR_iwmmxt_wmiawbt
21937                : fcode == ARM_BUILTIN_WMIAWTB ? CODE_FOR_iwmmxt_wmiawtb
21938                : fcode == ARM_BUILTIN_WMIAWTT ? CODE_FOR_iwmmxt_wmiawtt
21939                : fcode == ARM_BUILTIN_WMIAWBBN ? CODE_FOR_iwmmxt_wmiawbbn
21940                : fcode == ARM_BUILTIN_WMIAWBTN ? CODE_FOR_iwmmxt_wmiawbtn
21941                : fcode == ARM_BUILTIN_WMIAWTBN ? CODE_FOR_iwmmxt_wmiawtbn
21942                : fcode == ARM_BUILTIN_WMIAWTTN ? CODE_FOR_iwmmxt_wmiawttn
21943                : fcode == ARM_BUILTIN_WSADB ? CODE_FOR_iwmmxt_wsadb
21944                : CODE_FOR_iwmmxt_wsadh);
21945       arg0 = CALL_EXPR_ARG (exp, 0);
21946       arg1 = CALL_EXPR_ARG (exp, 1);
21947       arg2 = CALL_EXPR_ARG (exp, 2);
21948       op0 = expand_normal (arg0);
21949       op1 = expand_normal (arg1);
21950       op2 = expand_normal (arg2);
21951       tmode = insn_data[icode].operand[0].mode;
21952       mode0 = insn_data[icode].operand[1].mode;
21953       mode1 = insn_data[icode].operand[2].mode;
21954       mode2 = insn_data[icode].operand[3].mode;
21955
21956       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
21957         op0 = copy_to_mode_reg (mode0, op0);
21958       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
21959         op1 = copy_to_mode_reg (mode1, op1);
21960       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
21961         op2 = copy_to_mode_reg (mode2, op2);
21962       if (target == 0
21963           || GET_MODE (target) != tmode
21964           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
21965         target = gen_reg_rtx (tmode);
21966       pat = GEN_FCN (icode) (target, op0, op1, op2);
21967       if (! pat)
21968         return 0;
21969       emit_insn (pat);
21970       return target;
21971
21972     case ARM_BUILTIN_WZERO:
21973       target = gen_reg_rtx (DImode);
21974       emit_insn (gen_iwmmxt_clrdi (target));
21975       return target;
21976
21977     case ARM_BUILTIN_WSRLHI:
21978     case ARM_BUILTIN_WSRLWI:
21979     case ARM_BUILTIN_WSRLDI:
21980     case ARM_BUILTIN_WSLLHI:
21981     case ARM_BUILTIN_WSLLWI:
21982     case ARM_BUILTIN_WSLLDI:
21983     case ARM_BUILTIN_WSRAHI:
21984     case ARM_BUILTIN_WSRAWI:
21985     case ARM_BUILTIN_WSRADI:
21986     case ARM_BUILTIN_WRORHI:
21987     case ARM_BUILTIN_WRORWI:
21988     case ARM_BUILTIN_WRORDI:
21989     case ARM_BUILTIN_WSRLH:
21990     case ARM_BUILTIN_WSRLW:
21991     case ARM_BUILTIN_WSRLD:
21992     case ARM_BUILTIN_WSLLH:
21993     case ARM_BUILTIN_WSLLW:
21994     case ARM_BUILTIN_WSLLD:
21995     case ARM_BUILTIN_WSRAH:
21996     case ARM_BUILTIN_WSRAW:
21997     case ARM_BUILTIN_WSRAD:
21998     case ARM_BUILTIN_WRORH:
21999     case ARM_BUILTIN_WRORW:
22000     case ARM_BUILTIN_WRORD:
22001       icode = (fcode == ARM_BUILTIN_WSRLHI ? CODE_FOR_lshrv4hi3_iwmmxt
22002                : fcode == ARM_BUILTIN_WSRLWI ? CODE_FOR_lshrv2si3_iwmmxt
22003                : fcode == ARM_BUILTIN_WSRLDI ? CODE_FOR_lshrdi3_iwmmxt
22004                : fcode == ARM_BUILTIN_WSLLHI ? CODE_FOR_ashlv4hi3_iwmmxt
22005                : fcode == ARM_BUILTIN_WSLLWI ? CODE_FOR_ashlv2si3_iwmmxt
22006                : fcode == ARM_BUILTIN_WSLLDI ? CODE_FOR_ashldi3_iwmmxt
22007                : fcode == ARM_BUILTIN_WSRAHI ? CODE_FOR_ashrv4hi3_iwmmxt
22008                : fcode == ARM_BUILTIN_WSRAWI ? CODE_FOR_ashrv2si3_iwmmxt
22009                : fcode == ARM_BUILTIN_WSRADI ? CODE_FOR_ashrdi3_iwmmxt
22010                : fcode == ARM_BUILTIN_WRORHI ? CODE_FOR_rorv4hi3
22011                : fcode == ARM_BUILTIN_WRORWI ? CODE_FOR_rorv2si3
22012                : fcode == ARM_BUILTIN_WRORDI ? CODE_FOR_rordi3
22013                : fcode == ARM_BUILTIN_WSRLH  ? CODE_FOR_lshrv4hi3_di
22014                : fcode == ARM_BUILTIN_WSRLW  ? CODE_FOR_lshrv2si3_di
22015                : fcode == ARM_BUILTIN_WSRLD  ? CODE_FOR_lshrdi3_di
22016                : fcode == ARM_BUILTIN_WSLLH  ? CODE_FOR_ashlv4hi3_di
22017                : fcode == ARM_BUILTIN_WSLLW  ? CODE_FOR_ashlv2si3_di
22018                : fcode == ARM_BUILTIN_WSLLD  ? CODE_FOR_ashldi3_di
22019                : fcode == ARM_BUILTIN_WSRAH  ? CODE_FOR_ashrv4hi3_di
22020                : fcode == ARM_BUILTIN_WSRAW  ? CODE_FOR_ashrv2si3_di
22021                : fcode == ARM_BUILTIN_WSRAD  ? CODE_FOR_ashrdi3_di
22022                : fcode == ARM_BUILTIN_WRORH  ? CODE_FOR_rorv4hi3_di
22023                : fcode == ARM_BUILTIN_WRORW  ? CODE_FOR_rorv2si3_di
22024                : fcode == ARM_BUILTIN_WRORD  ? CODE_FOR_rordi3_di
22025                : CODE_FOR_nothing);
22026       arg1 = CALL_EXPR_ARG (exp, 1);
22027       op1 = expand_normal (arg1);
22028       if (GET_MODE (op1) == VOIDmode)
22029         {
22030           imm = INTVAL (op1);
22031           if ((fcode == ARM_BUILTIN_WRORHI || fcode == ARM_BUILTIN_WRORWI
22032                || fcode == ARM_BUILTIN_WRORH || fcode == ARM_BUILTIN_WRORW)
22033               && (imm < 0 || imm > 32))
22034             {
22035               if (fcode == ARM_BUILTIN_WRORHI)
22036                 error ("the range of count should be in 0 to 32.  please check the intrinsic _mm_rori_pi16 in code.");
22037               else if (fcode == ARM_BUILTIN_WRORWI)
22038                 error ("the range of count should be in 0 to 32.  please check the intrinsic _mm_rori_pi32 in code.");
22039               else if (fcode == ARM_BUILTIN_WRORH)
22040                 error ("the range of count should be in 0 to 32.  please check the intrinsic _mm_ror_pi16 in code.");
22041               else
22042                 error ("the range of count should be in 0 to 32.  please check the intrinsic _mm_ror_pi32 in code.");
22043             }
22044           else if ((fcode == ARM_BUILTIN_WRORDI || fcode == ARM_BUILTIN_WRORD)
22045                    && (imm < 0 || imm > 64))
22046             {
22047               if (fcode == ARM_BUILTIN_WRORDI)
22048                 error ("the range of count should be in 0 to 64.  please check the intrinsic _mm_rori_si64 in code.");
22049               else
22050                 error ("the range of count should be in 0 to 64.  please check the intrinsic _mm_ror_si64 in code.");
22051             }
22052           else if (imm < 0)
22053             {
22054               if (fcode == ARM_BUILTIN_WSRLHI)
22055                 error ("the count should be no less than 0.  please check the intrinsic _mm_srli_pi16 in code.");
22056               else if (fcode == ARM_BUILTIN_WSRLWI)
22057                 error ("the count should be no less than 0.  please check the intrinsic _mm_srli_pi32 in code.");
22058               else if (fcode == ARM_BUILTIN_WSRLDI)
22059                 error ("the count should be no less than 0.  please check the intrinsic _mm_srli_si64 in code.");
22060               else if (fcode == ARM_BUILTIN_WSLLHI)
22061                 error ("the count should be no less than 0.  please check the intrinsic _mm_slli_pi16 in code.");
22062               else if (fcode == ARM_BUILTIN_WSLLWI)
22063                 error ("the count should be no less than 0.  please check the intrinsic _mm_slli_pi32 in code.");
22064               else if (fcode == ARM_BUILTIN_WSLLDI)
22065                 error ("the count should be no less than 0.  please check the intrinsic _mm_slli_si64 in code.");
22066               else if (fcode == ARM_BUILTIN_WSRAHI)
22067                 error ("the count should be no less than 0.  please check the intrinsic _mm_srai_pi16 in code.");
22068               else if (fcode == ARM_BUILTIN_WSRAWI)
22069                 error ("the count should be no less than 0.  please check the intrinsic _mm_srai_pi32 in code.");
22070               else if (fcode == ARM_BUILTIN_WSRADI)
22071                 error ("the count should be no less than 0.  please check the intrinsic _mm_srai_si64 in code.");
22072               else if (fcode == ARM_BUILTIN_WSRLH)
22073                 error ("the count should be no less than 0.  please check the intrinsic _mm_srl_pi16 in code.");
22074               else if (fcode == ARM_BUILTIN_WSRLW)
22075                 error ("the count should be no less than 0.  please check the intrinsic _mm_srl_pi32 in code.");
22076               else if (fcode == ARM_BUILTIN_WSRLD)
22077                 error ("the count should be no less than 0.  please check the intrinsic _mm_srl_si64 in code.");
22078               else if (fcode == ARM_BUILTIN_WSLLH)
22079                 error ("the count should be no less than 0.  please check the intrinsic _mm_sll_pi16 in code.");
22080               else if (fcode == ARM_BUILTIN_WSLLW)
22081                 error ("the count should be no less than 0.  please check the intrinsic _mm_sll_pi32 in code.");
22082               else if (fcode == ARM_BUILTIN_WSLLD)
22083                 error ("the count should be no less than 0.  please check the intrinsic _mm_sll_si64 in code.");
22084               else if (fcode == ARM_BUILTIN_WSRAH)
22085                 error ("the count should be no less than 0.  please check the intrinsic _mm_sra_pi16 in code.");
22086               else if (fcode == ARM_BUILTIN_WSRAW)
22087                 error ("the count should be no less than 0.  please check the intrinsic _mm_sra_pi32 in code.");
22088               else
22089                 error ("the count should be no less than 0.  please check the intrinsic _mm_sra_si64 in code.");
22090             }
22091         }
22092       return arm_expand_binop_builtin (icode, exp, target);
22093
22094     default:
22095       break;
22096     }
22097
22098   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
22099     if (d->code == (const enum arm_builtins) fcode)
22100       return arm_expand_binop_builtin (d->icode, exp, target);
22101
22102   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
22103     if (d->code == (const enum arm_builtins) fcode)
22104       return arm_expand_unop_builtin (d->icode, exp, target, 0);
22105
22106   /* @@@ Should really do something sensible here.  */
22107   return NULL_RTX;
22108 }
22109 \f
22110 /* Return the number (counting from 0) of
22111    the least significant set bit in MASK.  */
22112
22113 inline static int
22114 number_of_first_bit_set (unsigned mask)
22115 {
22116   return ctz_hwi (mask);
22117 }
22118
22119 /* Like emit_multi_reg_push, but allowing for a different set of
22120    registers to be described as saved.  MASK is the set of registers
22121    to be saved; REAL_REGS is the set of registers to be described as
22122    saved.  If REAL_REGS is 0, only describe the stack adjustment.  */
22123
22124 static rtx
22125 thumb1_emit_multi_reg_push (unsigned long mask, unsigned long real_regs)
22126 {
22127   unsigned long regno;
22128   rtx par[10], tmp, reg, insn;
22129   int i, j;
22130
22131   /* Build the parallel of the registers actually being stored.  */
22132   for (i = 0; mask; ++i, mask &= mask - 1)
22133     {
22134       regno = ctz_hwi (mask);
22135       reg = gen_rtx_REG (SImode, regno);
22136
22137       if (i == 0)
22138         tmp = gen_rtx_UNSPEC (BLKmode, gen_rtvec (1, reg), UNSPEC_PUSH_MULT);
22139       else
22140         tmp = gen_rtx_USE (VOIDmode, reg);
22141
22142       par[i] = tmp;
22143     }
22144
22145   tmp = plus_constant (Pmode, stack_pointer_rtx, -4 * i);
22146   tmp = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, tmp);
22147   tmp = gen_frame_mem (BLKmode, tmp);
22148   tmp = gen_rtx_SET (VOIDmode, tmp, par[0]);
22149   par[0] = tmp;
22150
22151   tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (i, par));
22152   insn = emit_insn (tmp);
22153
22154   /* Always build the stack adjustment note for unwind info.  */
22155   tmp = plus_constant (Pmode, stack_pointer_rtx, -4 * i);
22156   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp);
22157   par[0] = tmp;
22158
22159   /* Build the parallel of the registers recorded as saved for unwind.  */
22160   for (j = 0; real_regs; ++j, real_regs &= real_regs - 1)
22161     {
22162       regno = ctz_hwi (real_regs);
22163       reg = gen_rtx_REG (SImode, regno);
22164
22165       tmp = plus_constant (Pmode, stack_pointer_rtx, j * 4);
22166       tmp = gen_frame_mem (SImode, tmp);
22167       tmp = gen_rtx_SET (VOIDmode, tmp, reg);
22168       RTX_FRAME_RELATED_P (tmp) = 1;
22169       par[j + 1] = tmp;
22170     }
22171
22172   if (j == 0)
22173     tmp = par[0];
22174   else
22175     {
22176       RTX_FRAME_RELATED_P (par[0]) = 1;
22177       tmp = gen_rtx_SEQUENCE (VOIDmode, gen_rtvec_v (j + 1, par));
22178     }
22179
22180   add_reg_note (insn, REG_FRAME_RELATED_EXPR, tmp);
22181
22182   return insn;
22183 }
22184
22185 /* Emit code to push or pop registers to or from the stack.  F is the
22186    assembly file.  MASK is the registers to pop.  */
22187 static void
22188 thumb_pop (FILE *f, unsigned long mask)
22189 {
22190   int regno;
22191   int lo_mask = mask & 0xFF;
22192   int pushed_words = 0;
22193
22194   gcc_assert (mask);
22195
22196   if (lo_mask == 0 && (mask & (1 << PC_REGNUM)))
22197     {
22198       /* Special case.  Do not generate a POP PC statement here, do it in
22199          thumb_exit() */
22200       thumb_exit (f, -1);
22201       return;
22202     }
22203
22204   fprintf (f, "\tpop\t{");
22205
22206   /* Look at the low registers first.  */
22207   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
22208     {
22209       if (lo_mask & 1)
22210         {
22211           asm_fprintf (f, "%r", regno);
22212
22213           if ((lo_mask & ~1) != 0)
22214             fprintf (f, ", ");
22215
22216           pushed_words++;
22217         }
22218     }
22219
22220   if (mask & (1 << PC_REGNUM))
22221     {
22222       /* Catch popping the PC.  */
22223       if (TARGET_INTERWORK || TARGET_BACKTRACE
22224           || crtl->calls_eh_return)
22225         {
22226           /* The PC is never poped directly, instead
22227              it is popped into r3 and then BX is used.  */
22228           fprintf (f, "}\n");
22229
22230           thumb_exit (f, -1);
22231
22232           return;
22233         }
22234       else
22235         {
22236           if (mask & 0xFF)
22237             fprintf (f, ", ");
22238
22239           asm_fprintf (f, "%r", PC_REGNUM);
22240         }
22241     }
22242
22243   fprintf (f, "}\n");
22244 }
22245
22246 /* Generate code to return from a thumb function.
22247    If 'reg_containing_return_addr' is -1, then the return address is
22248    actually on the stack, at the stack pointer.  */
22249 static void
22250 thumb_exit (FILE *f, int reg_containing_return_addr)
22251 {
22252   unsigned regs_available_for_popping;
22253   unsigned regs_to_pop;
22254   int pops_needed;
22255   unsigned available;
22256   unsigned required;
22257   int mode;
22258   int size;
22259   int restore_a4 = FALSE;
22260
22261   /* Compute the registers we need to pop.  */
22262   regs_to_pop = 0;
22263   pops_needed = 0;
22264
22265   if (reg_containing_return_addr == -1)
22266     {
22267       regs_to_pop |= 1 << LR_REGNUM;
22268       ++pops_needed;
22269     }
22270
22271   if (TARGET_BACKTRACE)
22272     {
22273       /* Restore the (ARM) frame pointer and stack pointer.  */
22274       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
22275       pops_needed += 2;
22276     }
22277
22278   /* If there is nothing to pop then just emit the BX instruction and
22279      return.  */
22280   if (pops_needed == 0)
22281     {
22282       if (crtl->calls_eh_return)
22283         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
22284
22285       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
22286       return;
22287     }
22288   /* Otherwise if we are not supporting interworking and we have not created
22289      a backtrace structure and the function was not entered in ARM mode then
22290      just pop the return address straight into the PC.  */
22291   else if (!TARGET_INTERWORK
22292            && !TARGET_BACKTRACE
22293            && !is_called_in_ARM_mode (current_function_decl)
22294            && !crtl->calls_eh_return)
22295     {
22296       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
22297       return;
22298     }
22299
22300   /* Find out how many of the (return) argument registers we can corrupt.  */
22301   regs_available_for_popping = 0;
22302
22303   /* If returning via __builtin_eh_return, the bottom three registers
22304      all contain information needed for the return.  */
22305   if (crtl->calls_eh_return)
22306     size = 12;
22307   else
22308     {
22309       /* If we can deduce the registers used from the function's
22310          return value.  This is more reliable that examining
22311          df_regs_ever_live_p () because that will be set if the register is
22312          ever used in the function, not just if the register is used
22313          to hold a return value.  */
22314
22315       if (crtl->return_rtx != 0)
22316         mode = GET_MODE (crtl->return_rtx);
22317       else
22318         mode = DECL_MODE (DECL_RESULT (current_function_decl));
22319
22320       size = GET_MODE_SIZE (mode);
22321
22322       if (size == 0)
22323         {
22324           /* In a void function we can use any argument register.
22325              In a function that returns a structure on the stack
22326              we can use the second and third argument registers.  */
22327           if (mode == VOIDmode)
22328             regs_available_for_popping =
22329               (1 << ARG_REGISTER (1))
22330               | (1 << ARG_REGISTER (2))
22331               | (1 << ARG_REGISTER (3));
22332           else
22333             regs_available_for_popping =
22334               (1 << ARG_REGISTER (2))
22335               | (1 << ARG_REGISTER (3));
22336         }
22337       else if (size <= 4)
22338         regs_available_for_popping =
22339           (1 << ARG_REGISTER (2))
22340           | (1 << ARG_REGISTER (3));
22341       else if (size <= 8)
22342         regs_available_for_popping =
22343           (1 << ARG_REGISTER (3));
22344     }
22345
22346   /* Match registers to be popped with registers into which we pop them.  */
22347   for (available = regs_available_for_popping,
22348        required  = regs_to_pop;
22349        required != 0 && available != 0;
22350        available &= ~(available & - available),
22351        required  &= ~(required  & - required))
22352     -- pops_needed;
22353
22354   /* If we have any popping registers left over, remove them.  */
22355   if (available > 0)
22356     regs_available_for_popping &= ~available;
22357
22358   /* Otherwise if we need another popping register we can use
22359      the fourth argument register.  */
22360   else if (pops_needed)
22361     {
22362       /* If we have not found any free argument registers and
22363          reg a4 contains the return address, we must move it.  */
22364       if (regs_available_for_popping == 0
22365           && reg_containing_return_addr == LAST_ARG_REGNUM)
22366         {
22367           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
22368           reg_containing_return_addr = LR_REGNUM;
22369         }
22370       else if (size > 12)
22371         {
22372           /* Register a4 is being used to hold part of the return value,
22373              but we have dire need of a free, low register.  */
22374           restore_a4 = TRUE;
22375
22376           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
22377         }
22378
22379       if (reg_containing_return_addr != LAST_ARG_REGNUM)
22380         {
22381           /* The fourth argument register is available.  */
22382           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
22383
22384           --pops_needed;
22385         }
22386     }
22387
22388   /* Pop as many registers as we can.  */
22389   thumb_pop (f, regs_available_for_popping);
22390
22391   /* Process the registers we popped.  */
22392   if (reg_containing_return_addr == -1)
22393     {
22394       /* The return address was popped into the lowest numbered register.  */
22395       regs_to_pop &= ~(1 << LR_REGNUM);
22396
22397       reg_containing_return_addr =
22398         number_of_first_bit_set (regs_available_for_popping);
22399
22400       /* Remove this register for the mask of available registers, so that
22401          the return address will not be corrupted by further pops.  */
22402       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
22403     }
22404
22405   /* If we popped other registers then handle them here.  */
22406   if (regs_available_for_popping)
22407     {
22408       int frame_pointer;
22409
22410       /* Work out which register currently contains the frame pointer.  */
22411       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
22412
22413       /* Move it into the correct place.  */
22414       asm_fprintf (f, "\tmov\t%r, %r\n",
22415                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
22416
22417       /* (Temporarily) remove it from the mask of popped registers.  */
22418       regs_available_for_popping &= ~(1 << frame_pointer);
22419       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
22420
22421       if (regs_available_for_popping)
22422         {
22423           int stack_pointer;
22424
22425           /* We popped the stack pointer as well,
22426              find the register that contains it.  */
22427           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
22428
22429           /* Move it into the stack register.  */
22430           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
22431
22432           /* At this point we have popped all necessary registers, so
22433              do not worry about restoring regs_available_for_popping
22434              to its correct value:
22435
22436              assert (pops_needed == 0)
22437              assert (regs_available_for_popping == (1 << frame_pointer))
22438              assert (regs_to_pop == (1 << STACK_POINTER))  */
22439         }
22440       else
22441         {
22442           /* Since we have just move the popped value into the frame
22443              pointer, the popping register is available for reuse, and
22444              we know that we still have the stack pointer left to pop.  */
22445           regs_available_for_popping |= (1 << frame_pointer);
22446         }
22447     }
22448
22449   /* If we still have registers left on the stack, but we no longer have
22450      any registers into which we can pop them, then we must move the return
22451      address into the link register and make available the register that
22452      contained it.  */
22453   if (regs_available_for_popping == 0 && pops_needed > 0)
22454     {
22455       regs_available_for_popping |= 1 << reg_containing_return_addr;
22456
22457       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
22458                    reg_containing_return_addr);
22459
22460       reg_containing_return_addr = LR_REGNUM;
22461     }
22462
22463   /* If we have registers left on the stack then pop some more.
22464      We know that at most we will want to pop FP and SP.  */
22465   if (pops_needed > 0)
22466     {
22467       int  popped_into;
22468       int  move_to;
22469
22470       thumb_pop (f, regs_available_for_popping);
22471
22472       /* We have popped either FP or SP.
22473          Move whichever one it is into the correct register.  */
22474       popped_into = number_of_first_bit_set (regs_available_for_popping);
22475       move_to     = number_of_first_bit_set (regs_to_pop);
22476
22477       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
22478
22479       regs_to_pop &= ~(1 << move_to);
22480
22481       --pops_needed;
22482     }
22483
22484   /* If we still have not popped everything then we must have only
22485      had one register available to us and we are now popping the SP.  */
22486   if (pops_needed > 0)
22487     {
22488       int  popped_into;
22489
22490       thumb_pop (f, regs_available_for_popping);
22491
22492       popped_into = number_of_first_bit_set (regs_available_for_popping);
22493
22494       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
22495       /*
22496         assert (regs_to_pop == (1 << STACK_POINTER))
22497         assert (pops_needed == 1)
22498       */
22499     }
22500
22501   /* If necessary restore the a4 register.  */
22502   if (restore_a4)
22503     {
22504       if (reg_containing_return_addr != LR_REGNUM)
22505         {
22506           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
22507           reg_containing_return_addr = LR_REGNUM;
22508         }
22509
22510       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
22511     }
22512
22513   if (crtl->calls_eh_return)
22514     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
22515
22516   /* Return to caller.  */
22517   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
22518 }
22519 \f
22520 /* Scan INSN just before assembler is output for it.
22521    For Thumb-1, we track the status of the condition codes; this
22522    information is used in the cbranchsi4_insn pattern.  */
22523 void
22524 thumb1_final_prescan_insn (rtx insn)
22525 {
22526   if (flag_print_asm_name)
22527     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
22528                  INSN_ADDRESSES (INSN_UID (insn)));
22529   /* Don't overwrite the previous setter when we get to a cbranch.  */
22530   if (INSN_CODE (insn) != CODE_FOR_cbranchsi4_insn)
22531     {
22532       enum attr_conds conds;
22533
22534       if (cfun->machine->thumb1_cc_insn)
22535         {
22536           if (modified_in_p (cfun->machine->thumb1_cc_op0, insn)
22537               || modified_in_p (cfun->machine->thumb1_cc_op1, insn))
22538             CC_STATUS_INIT;
22539         }
22540       conds = get_attr_conds (insn);
22541       if (conds == CONDS_SET)
22542         {
22543           rtx set = single_set (insn);
22544           cfun->machine->thumb1_cc_insn = insn;
22545           cfun->machine->thumb1_cc_op0 = SET_DEST (set);
22546           cfun->machine->thumb1_cc_op1 = const0_rtx;
22547           cfun->machine->thumb1_cc_mode = CC_NOOVmode;
22548           if (INSN_CODE (insn) == CODE_FOR_thumb1_subsi3_insn)
22549             {
22550               rtx src1 = XEXP (SET_SRC (set), 1);
22551               if (src1 == const0_rtx)
22552                 cfun->machine->thumb1_cc_mode = CCmode;
22553             }
22554           else if (REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
22555             {
22556               /* Record the src register operand instead of dest because
22557                  cprop_hardreg pass propagates src.  */
22558               cfun->machine->thumb1_cc_op0 = SET_SRC (set);
22559             }
22560         }
22561       else if (conds != CONDS_NOCOND)
22562         cfun->machine->thumb1_cc_insn = NULL_RTX;
22563     }
22564 }
22565
22566 int
22567 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
22568 {
22569   unsigned HOST_WIDE_INT mask = 0xff;
22570   int i;
22571
22572   val = val & (unsigned HOST_WIDE_INT)0xffffffffu;
22573   if (val == 0) /* XXX */
22574     return 0;
22575
22576   for (i = 0; i < 25; i++)
22577     if ((val & (mask << i)) == val)
22578       return 1;
22579
22580   return 0;
22581 }
22582
22583 /* Returns nonzero if the current function contains,
22584    or might contain a far jump.  */
22585 static int
22586 thumb_far_jump_used_p (void)
22587 {
22588   rtx insn;
22589
22590   /* This test is only important for leaf functions.  */
22591   /* assert (!leaf_function_p ()); */
22592
22593   /* If we have already decided that far jumps may be used,
22594      do not bother checking again, and always return true even if
22595      it turns out that they are not being used.  Once we have made
22596      the decision that far jumps are present (and that hence the link
22597      register will be pushed onto the stack) we cannot go back on it.  */
22598   if (cfun->machine->far_jump_used)
22599     return 1;
22600
22601   /* If this function is not being called from the prologue/epilogue
22602      generation code then it must be being called from the
22603      INITIAL_ELIMINATION_OFFSET macro.  */
22604   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
22605     {
22606       /* In this case we know that we are being asked about the elimination
22607          of the arg pointer register.  If that register is not being used,
22608          then there are no arguments on the stack, and we do not have to
22609          worry that a far jump might force the prologue to push the link
22610          register, changing the stack offsets.  In this case we can just
22611          return false, since the presence of far jumps in the function will
22612          not affect stack offsets.
22613
22614          If the arg pointer is live (or if it was live, but has now been
22615          eliminated and so set to dead) then we do have to test to see if
22616          the function might contain a far jump.  This test can lead to some
22617          false negatives, since before reload is completed, then length of
22618          branch instructions is not known, so gcc defaults to returning their
22619          longest length, which in turn sets the far jump attribute to true.
22620
22621          A false negative will not result in bad code being generated, but it
22622          will result in a needless push and pop of the link register.  We
22623          hope that this does not occur too often.
22624
22625          If we need doubleword stack alignment this could affect the other
22626          elimination offsets so we can't risk getting it wrong.  */
22627       if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
22628         cfun->machine->arg_pointer_live = 1;
22629       else if (!cfun->machine->arg_pointer_live)
22630         return 0;
22631     }
22632
22633   /* Check to see if the function contains a branch
22634      insn with the far jump attribute set.  */
22635   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
22636     {
22637       if (JUMP_P (insn)
22638           /* Ignore tablejump patterns.  */
22639           && GET_CODE (PATTERN (insn)) != ADDR_VEC
22640           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
22641           && get_attr_far_jump (insn) == FAR_JUMP_YES
22642           )
22643         {
22644           /* Record the fact that we have decided that
22645              the function does use far jumps.  */
22646           cfun->machine->far_jump_used = 1;
22647           return 1;
22648         }
22649     }
22650
22651   return 0;
22652 }
22653
22654 /* Return nonzero if FUNC must be entered in ARM mode.  */
22655 int
22656 is_called_in_ARM_mode (tree func)
22657 {
22658   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
22659
22660   /* Ignore the problem about functions whose address is taken.  */
22661   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
22662     return TRUE;
22663
22664 #ifdef ARM_PE
22665   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
22666 #else
22667   return FALSE;
22668 #endif
22669 }
22670
22671 /* Given the stack offsets and register mask in OFFSETS, decide how
22672    many additional registers to push instead of subtracting a constant
22673    from SP.  For epilogues the principle is the same except we use pop.
22674    FOR_PROLOGUE indicates which we're generating.  */
22675 static int
22676 thumb1_extra_regs_pushed (arm_stack_offsets *offsets, bool for_prologue)
22677 {
22678   HOST_WIDE_INT amount;
22679   unsigned long live_regs_mask = offsets->saved_regs_mask;
22680   /* Extract a mask of the ones we can give to the Thumb's push/pop
22681      instruction.  */
22682   unsigned long l_mask = live_regs_mask & (for_prologue ? 0x40ff : 0xff);
22683   /* Then count how many other high registers will need to be pushed.  */
22684   unsigned long high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
22685   int n_free, reg_base, size;
22686
22687   if (!for_prologue && frame_pointer_needed)
22688     amount = offsets->locals_base - offsets->saved_regs;
22689   else
22690     amount = offsets->outgoing_args - offsets->saved_regs;
22691
22692   /* If the stack frame size is 512 exactly, we can save one load
22693      instruction, which should make this a win even when optimizing
22694      for speed.  */
22695   if (!optimize_size && amount != 512)
22696     return 0;
22697
22698   /* Can't do this if there are high registers to push.  */
22699   if (high_regs_pushed != 0)
22700     return 0;
22701
22702   /* Shouldn't do it in the prologue if no registers would normally
22703      be pushed at all.  In the epilogue, also allow it if we'll have
22704      a pop insn for the PC.  */
22705   if  (l_mask == 0
22706        && (for_prologue
22707            || TARGET_BACKTRACE
22708            || (live_regs_mask & 1 << LR_REGNUM) == 0
22709            || TARGET_INTERWORK
22710            || crtl->args.pretend_args_size != 0))
22711     return 0;
22712
22713   /* Don't do this if thumb_expand_prologue wants to emit instructions
22714      between the push and the stack frame allocation.  */
22715   if (for_prologue
22716       && ((flag_pic && arm_pic_register != INVALID_REGNUM)
22717           || (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)))
22718     return 0;
22719
22720   reg_base = 0;
22721   n_free = 0;
22722   if (!for_prologue)
22723     {
22724       size = arm_size_return_regs ();
22725       reg_base = ARM_NUM_INTS (size);
22726       live_regs_mask >>= reg_base;
22727     }
22728
22729   while (reg_base + n_free < 8 && !(live_regs_mask & 1)
22730          && (for_prologue || call_used_regs[reg_base + n_free]))
22731     {
22732       live_regs_mask >>= 1;
22733       n_free++;
22734     }
22735
22736   if (n_free == 0)
22737     return 0;
22738   gcc_assert (amount / 4 * 4 == amount);
22739
22740   if (amount >= 512 && (amount - n_free * 4) < 512)
22741     return (amount - 508) / 4;
22742   if (amount <= n_free * 4)
22743     return amount / 4;
22744   return 0;
22745 }
22746
22747 /* The bits which aren't usefully expanded as rtl.  */
22748 const char *
22749 thumb1_unexpanded_epilogue (void)
22750 {
22751   arm_stack_offsets *offsets;
22752   int regno;
22753   unsigned long live_regs_mask = 0;
22754   int high_regs_pushed = 0;
22755   int extra_pop;
22756   int had_to_push_lr;
22757   int size;
22758
22759   if (cfun->machine->return_used_this_function != 0)
22760     return "";
22761
22762   if (IS_NAKED (arm_current_func_type ()))
22763     return "";
22764
22765   offsets = arm_get_frame_offsets ();
22766   live_regs_mask = offsets->saved_regs_mask;
22767   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
22768
22769   /* If we can deduce the registers used from the function's return value.
22770      This is more reliable that examining df_regs_ever_live_p () because that
22771      will be set if the register is ever used in the function, not just if
22772      the register is used to hold a return value.  */
22773   size = arm_size_return_regs ();
22774
22775   extra_pop = thumb1_extra_regs_pushed (offsets, false);
22776   if (extra_pop > 0)
22777     {
22778       unsigned long extra_mask = (1 << extra_pop) - 1;
22779       live_regs_mask |= extra_mask << ARM_NUM_INTS (size);
22780     }
22781
22782   /* The prolog may have pushed some high registers to use as
22783      work registers.  e.g. the testsuite file:
22784      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
22785      compiles to produce:
22786         push    {r4, r5, r6, r7, lr}
22787         mov     r7, r9
22788         mov     r6, r8
22789         push    {r6, r7}
22790      as part of the prolog.  We have to undo that pushing here.  */
22791
22792   if (high_regs_pushed)
22793     {
22794       unsigned long mask = live_regs_mask & 0xff;
22795       int next_hi_reg;
22796
22797       /* The available low registers depend on the size of the value we are
22798          returning.  */
22799       if (size <= 12)
22800         mask |=  1 << 3;
22801       if (size <= 8)
22802         mask |= 1 << 2;
22803
22804       if (mask == 0)
22805         /* Oh dear!  We have no low registers into which we can pop
22806            high registers!  */
22807         internal_error
22808           ("no low registers available for popping high registers");
22809
22810       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
22811         if (live_regs_mask & (1 << next_hi_reg))
22812           break;
22813
22814       while (high_regs_pushed)
22815         {
22816           /* Find lo register(s) into which the high register(s) can
22817              be popped.  */
22818           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
22819             {
22820               if (mask & (1 << regno))
22821                 high_regs_pushed--;
22822               if (high_regs_pushed == 0)
22823                 break;
22824             }
22825
22826           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
22827
22828           /* Pop the values into the low register(s).  */
22829           thumb_pop (asm_out_file, mask);
22830
22831           /* Move the value(s) into the high registers.  */
22832           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
22833             {
22834               if (mask & (1 << regno))
22835                 {
22836                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
22837                                regno);
22838
22839                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
22840                     if (live_regs_mask & (1 << next_hi_reg))
22841                       break;
22842                 }
22843             }
22844         }
22845       live_regs_mask &= ~0x0f00;
22846     }
22847
22848   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
22849   live_regs_mask &= 0xff;
22850
22851   if (crtl->args.pretend_args_size == 0 || TARGET_BACKTRACE)
22852     {
22853       /* Pop the return address into the PC.  */
22854       if (had_to_push_lr)
22855         live_regs_mask |= 1 << PC_REGNUM;
22856
22857       /* Either no argument registers were pushed or a backtrace
22858          structure was created which includes an adjusted stack
22859          pointer, so just pop everything.  */
22860       if (live_regs_mask)
22861         thumb_pop (asm_out_file, live_regs_mask);
22862
22863       /* We have either just popped the return address into the
22864          PC or it is was kept in LR for the entire function.
22865          Note that thumb_pop has already called thumb_exit if the
22866          PC was in the list.  */
22867       if (!had_to_push_lr)
22868         thumb_exit (asm_out_file, LR_REGNUM);
22869     }
22870   else
22871     {
22872       /* Pop everything but the return address.  */
22873       if (live_regs_mask)
22874         thumb_pop (asm_out_file, live_regs_mask);
22875
22876       if (had_to_push_lr)
22877         {
22878           if (size > 12)
22879             {
22880               /* We have no free low regs, so save one.  */
22881               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
22882                            LAST_ARG_REGNUM);
22883             }
22884
22885           /* Get the return address into a temporary register.  */
22886           thumb_pop (asm_out_file, 1 << LAST_ARG_REGNUM);
22887
22888           if (size > 12)
22889             {
22890               /* Move the return address to lr.  */
22891               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
22892                            LAST_ARG_REGNUM);
22893               /* Restore the low register.  */
22894               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
22895                            IP_REGNUM);
22896               regno = LR_REGNUM;
22897             }
22898           else
22899             regno = LAST_ARG_REGNUM;
22900         }
22901       else
22902         regno = LR_REGNUM;
22903
22904       /* Remove the argument registers that were pushed onto the stack.  */
22905       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
22906                    SP_REGNUM, SP_REGNUM,
22907                    crtl->args.pretend_args_size);
22908
22909       thumb_exit (asm_out_file, regno);
22910     }
22911
22912   return "";
22913 }
22914
22915 /* Functions to save and restore machine-specific function data.  */
22916 static struct machine_function *
22917 arm_init_machine_status (void)
22918 {
22919   struct machine_function *machine;
22920   machine = ggc_alloc_cleared_machine_function ();
22921
22922 #if ARM_FT_UNKNOWN != 0
22923   machine->func_type = ARM_FT_UNKNOWN;
22924 #endif
22925   return machine;
22926 }
22927
22928 /* Return an RTX indicating where the return address to the
22929    calling function can be found.  */
22930 rtx
22931 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
22932 {
22933   if (count != 0)
22934     return NULL_RTX;
22935
22936   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
22937 }
22938
22939 /* Do anything needed before RTL is emitted for each function.  */
22940 void
22941 arm_init_expanders (void)
22942 {
22943   /* Arrange to initialize and mark the machine per-function status.  */
22944   init_machine_status = arm_init_machine_status;
22945
22946   /* This is to stop the combine pass optimizing away the alignment
22947      adjustment of va_arg.  */
22948   /* ??? It is claimed that this should not be necessary.  */
22949   if (cfun)
22950     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
22951 }
22952
22953
22954 /* Like arm_compute_initial_elimination offset.  Simpler because there
22955    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
22956    to point at the base of the local variables after static stack
22957    space for a function has been allocated.  */
22958
22959 HOST_WIDE_INT
22960 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
22961 {
22962   arm_stack_offsets *offsets;
22963
22964   offsets = arm_get_frame_offsets ();
22965
22966   switch (from)
22967     {
22968     case ARG_POINTER_REGNUM:
22969       switch (to)
22970         {
22971         case STACK_POINTER_REGNUM:
22972           return offsets->outgoing_args - offsets->saved_args;
22973
22974         case FRAME_POINTER_REGNUM:
22975           return offsets->soft_frame - offsets->saved_args;
22976
22977         case ARM_HARD_FRAME_POINTER_REGNUM:
22978           return offsets->saved_regs - offsets->saved_args;
22979
22980         case THUMB_HARD_FRAME_POINTER_REGNUM:
22981           return offsets->locals_base - offsets->saved_args;
22982
22983         default:
22984           gcc_unreachable ();
22985         }
22986       break;
22987
22988     case FRAME_POINTER_REGNUM:
22989       switch (to)
22990         {
22991         case STACK_POINTER_REGNUM:
22992           return offsets->outgoing_args - offsets->soft_frame;
22993
22994         case ARM_HARD_FRAME_POINTER_REGNUM:
22995           return offsets->saved_regs - offsets->soft_frame;
22996
22997         case THUMB_HARD_FRAME_POINTER_REGNUM:
22998           return offsets->locals_base - offsets->soft_frame;
22999
23000         default:
23001           gcc_unreachable ();
23002         }
23003       break;
23004
23005     default:
23006       gcc_unreachable ();
23007     }
23008 }
23009
23010 /* Generate the function's prologue.  */
23011
23012 void
23013 thumb1_expand_prologue (void)
23014 {
23015   rtx insn;
23016
23017   HOST_WIDE_INT amount;
23018   arm_stack_offsets *offsets;
23019   unsigned long func_type;
23020   int regno;
23021   unsigned long live_regs_mask;
23022   unsigned long l_mask;
23023   unsigned high_regs_pushed = 0;
23024
23025   func_type = arm_current_func_type ();
23026
23027   /* Naked functions don't have prologues.  */
23028   if (IS_NAKED (func_type))
23029     return;
23030
23031   if (IS_INTERRUPT (func_type))
23032     {
23033       error ("interrupt Service Routines cannot be coded in Thumb mode");
23034       return;
23035     }
23036
23037   if (is_called_in_ARM_mode (current_function_decl))
23038     emit_insn (gen_prologue_thumb1_interwork ());
23039
23040   offsets = arm_get_frame_offsets ();
23041   live_regs_mask = offsets->saved_regs_mask;
23042
23043   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
23044   l_mask = live_regs_mask & 0x40ff;
23045   /* Then count how many other high registers will need to be pushed.  */
23046   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
23047
23048   if (crtl->args.pretend_args_size)
23049     {
23050       rtx x = GEN_INT (-crtl->args.pretend_args_size);
23051
23052       if (cfun->machine->uses_anonymous_args)
23053         {
23054           int num_pushes = ARM_NUM_INTS (crtl->args.pretend_args_size);
23055           unsigned long mask;
23056
23057           mask = 1ul << (LAST_ARG_REGNUM + 1);
23058           mask -= 1ul << (LAST_ARG_REGNUM + 1 - num_pushes);
23059
23060           insn = thumb1_emit_multi_reg_push (mask, 0);
23061         }
23062       else
23063         {
23064           insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
23065                                         stack_pointer_rtx, x));
23066         }
23067       RTX_FRAME_RELATED_P (insn) = 1;
23068     }
23069
23070   if (TARGET_BACKTRACE)
23071     {
23072       HOST_WIDE_INT offset = 0;
23073       unsigned work_register;
23074       rtx work_reg, x, arm_hfp_rtx;
23075
23076       /* We have been asked to create a stack backtrace structure.
23077          The code looks like this:
23078
23079          0   .align 2
23080          0   func:
23081          0     sub   SP, #16         Reserve space for 4 registers.
23082          2     push  {R7}            Push low registers.
23083          4     add   R7, SP, #20     Get the stack pointer before the push.
23084          6     str   R7, [SP, #8]    Store the stack pointer
23085                                         (before reserving the space).
23086          8     mov   R7, PC          Get hold of the start of this code + 12.
23087         10     str   R7, [SP, #16]   Store it.
23088         12     mov   R7, FP          Get hold of the current frame pointer.
23089         14     str   R7, [SP, #4]    Store it.
23090         16     mov   R7, LR          Get hold of the current return address.
23091         18     str   R7, [SP, #12]   Store it.
23092         20     add   R7, SP, #16     Point at the start of the
23093                                         backtrace structure.
23094         22     mov   FP, R7          Put this value into the frame pointer.  */
23095
23096       work_register = thumb_find_work_register (live_regs_mask);
23097       work_reg = gen_rtx_REG (SImode, work_register);
23098       arm_hfp_rtx = gen_rtx_REG (SImode, ARM_HARD_FRAME_POINTER_REGNUM);
23099
23100       insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
23101                                     stack_pointer_rtx, GEN_INT (-16)));
23102       RTX_FRAME_RELATED_P (insn) = 1;
23103
23104       if (l_mask)
23105         {
23106           insn = thumb1_emit_multi_reg_push (l_mask, l_mask);
23107           RTX_FRAME_RELATED_P (insn) = 1;
23108
23109           offset = bit_count (l_mask) * UNITS_PER_WORD;
23110         }
23111
23112       x = GEN_INT (offset + 16 + crtl->args.pretend_args_size);
23113       emit_insn (gen_addsi3 (work_reg, stack_pointer_rtx, x));
23114
23115       x = plus_constant (Pmode, stack_pointer_rtx, offset + 4);
23116       x = gen_frame_mem (SImode, x);
23117       emit_move_insn (x, work_reg);
23118
23119       /* Make sure that the instruction fetching the PC is in the right place
23120          to calculate "start of backtrace creation code + 12".  */
23121       /* ??? The stores using the common WORK_REG ought to be enough to
23122          prevent the scheduler from doing anything weird.  Failing that
23123          we could always move all of the following into an UNSPEC_VOLATILE.  */
23124       if (l_mask)
23125         {
23126           x = gen_rtx_REG (SImode, PC_REGNUM);
23127           emit_move_insn (work_reg, x);
23128
23129           x = plus_constant (Pmode, stack_pointer_rtx, offset + 12);
23130           x = gen_frame_mem (SImode, x);
23131           emit_move_insn (x, work_reg);
23132
23133           emit_move_insn (work_reg, arm_hfp_rtx);
23134
23135           x = plus_constant (Pmode, stack_pointer_rtx, offset);
23136           x = gen_frame_mem (SImode, x);
23137           emit_move_insn (x, work_reg);
23138         }
23139       else
23140         {
23141           emit_move_insn (work_reg, arm_hfp_rtx);
23142
23143           x = plus_constant (Pmode, stack_pointer_rtx, offset);
23144           x = gen_frame_mem (SImode, x);
23145           emit_move_insn (x, work_reg);
23146
23147           x = gen_rtx_REG (SImode, PC_REGNUM);
23148           emit_move_insn (work_reg, x);
23149
23150           x = plus_constant (Pmode, stack_pointer_rtx, offset + 12);
23151           x = gen_frame_mem (SImode, x);
23152           emit_move_insn (x, work_reg);
23153         }
23154
23155       x = gen_rtx_REG (SImode, LR_REGNUM);
23156       emit_move_insn (work_reg, x);
23157
23158       x = plus_constant (Pmode, stack_pointer_rtx, offset + 8);
23159       x = gen_frame_mem (SImode, x);
23160       emit_move_insn (x, work_reg);
23161
23162       x = GEN_INT (offset + 12);
23163       emit_insn (gen_addsi3 (work_reg, stack_pointer_rtx, x));
23164
23165       emit_move_insn (arm_hfp_rtx, work_reg);
23166     }
23167   /* Optimization:  If we are not pushing any low registers but we are going
23168      to push some high registers then delay our first push.  This will just
23169      be a push of LR and we can combine it with the push of the first high
23170      register.  */
23171   else if ((l_mask & 0xff) != 0
23172            || (high_regs_pushed == 0 && l_mask))
23173     {
23174       unsigned long mask = l_mask;
23175       mask |= (1 << thumb1_extra_regs_pushed (offsets, true)) - 1;
23176       insn = thumb1_emit_multi_reg_push (mask, mask);
23177       RTX_FRAME_RELATED_P (insn) = 1;
23178     }
23179
23180   if (high_regs_pushed)
23181     {
23182       unsigned pushable_regs;
23183       unsigned next_hi_reg;
23184       unsigned arg_regs_num = TARGET_AAPCS_BASED ? crtl->args.info.aapcs_ncrn
23185                                                  : crtl->args.info.nregs;
23186       unsigned arg_regs_mask = (1 << arg_regs_num) - 1;
23187
23188       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
23189         if (live_regs_mask & (1 << next_hi_reg))
23190           break;
23191
23192       /* Here we need to mask out registers used for passing arguments
23193          even if they can be pushed.  This is to avoid using them to stash the high
23194          registers.  Such kind of stash may clobber the use of arguments.  */
23195       pushable_regs = l_mask & (~arg_regs_mask) & 0xff;
23196
23197       if (pushable_regs == 0)
23198         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
23199
23200       while (high_regs_pushed > 0)
23201         {
23202           unsigned long real_regs_mask = 0;
23203
23204           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
23205             {
23206               if (pushable_regs & (1 << regno))
23207                 {
23208                   emit_move_insn (gen_rtx_REG (SImode, regno),
23209                                   gen_rtx_REG (SImode, next_hi_reg));
23210
23211                   high_regs_pushed --;
23212                   real_regs_mask |= (1 << next_hi_reg);
23213
23214                   if (high_regs_pushed)
23215                     {
23216                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
23217                            next_hi_reg --)
23218                         if (live_regs_mask & (1 << next_hi_reg))
23219                           break;
23220                     }
23221                   else
23222                     {
23223                       pushable_regs &= ~((1 << regno) - 1);
23224                       break;
23225                     }
23226                 }
23227             }
23228
23229           /* If we had to find a work register and we have not yet
23230              saved the LR then add it to the list of regs to push.  */
23231           if (l_mask == (1 << LR_REGNUM))
23232             {
23233               pushable_regs |= l_mask;
23234               real_regs_mask |= l_mask;
23235               l_mask = 0;
23236             }
23237
23238           insn = thumb1_emit_multi_reg_push (pushable_regs, real_regs_mask);
23239           RTX_FRAME_RELATED_P (insn) = 1;
23240         }
23241     }
23242
23243   /* Load the pic register before setting the frame pointer,
23244      so we can use r7 as a temporary work register.  */
23245   if (flag_pic && arm_pic_register != INVALID_REGNUM)
23246     arm_load_pic_register (live_regs_mask);
23247
23248   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
23249     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
23250                     stack_pointer_rtx);
23251
23252   if (flag_stack_usage_info)
23253     current_function_static_stack_size
23254       = offsets->outgoing_args - offsets->saved_args;
23255
23256   amount = offsets->outgoing_args - offsets->saved_regs;
23257   amount -= 4 * thumb1_extra_regs_pushed (offsets, true);
23258   if (amount)
23259     {
23260       if (amount < 512)
23261         {
23262           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
23263                                         GEN_INT (- amount)));
23264           RTX_FRAME_RELATED_P (insn) = 1;
23265         }
23266       else
23267         {
23268           rtx reg, dwarf;
23269
23270           /* The stack decrement is too big for an immediate value in a single
23271              insn.  In theory we could issue multiple subtracts, but after
23272              three of them it becomes more space efficient to place the full
23273              value in the constant pool and load into a register.  (Also the
23274              ARM debugger really likes to see only one stack decrement per
23275              function).  So instead we look for a scratch register into which
23276              we can load the decrement, and then we subtract this from the
23277              stack pointer.  Unfortunately on the thumb the only available
23278              scratch registers are the argument registers, and we cannot use
23279              these as they may hold arguments to the function.  Instead we
23280              attempt to locate a call preserved register which is used by this
23281              function.  If we can find one, then we know that it will have
23282              been pushed at the start of the prologue and so we can corrupt
23283              it now.  */
23284           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
23285             if (live_regs_mask & (1 << regno))
23286               break;
23287
23288           gcc_assert(regno <= LAST_LO_REGNUM);
23289
23290           reg = gen_rtx_REG (SImode, regno);
23291
23292           emit_insn (gen_movsi (reg, GEN_INT (- amount)));
23293
23294           insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
23295                                         stack_pointer_rtx, reg));
23296
23297           dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
23298                                plus_constant (Pmode, stack_pointer_rtx,
23299                                               -amount));
23300           add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
23301           RTX_FRAME_RELATED_P (insn) = 1;
23302         }
23303     }
23304
23305   if (frame_pointer_needed)
23306     thumb_set_frame_pointer (offsets);
23307
23308   /* If we are profiling, make sure no instructions are scheduled before
23309      the call to mcount.  Similarly if the user has requested no
23310      scheduling in the prolog.  Similarly if we want non-call exceptions
23311      using the EABI unwinder, to prevent faulting instructions from being
23312      swapped with a stack adjustment.  */
23313   if (crtl->profile || !TARGET_SCHED_PROLOG
23314       || (arm_except_unwind_info (&global_options) == UI_TARGET
23315           && cfun->can_throw_non_call_exceptions))
23316     emit_insn (gen_blockage ());
23317
23318   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
23319   if (live_regs_mask & 0xff)
23320     cfun->machine->lr_save_eliminated = 0;
23321 }
23322
23323 /* Generate pattern *pop_multiple_with_stack_update_and_return if single
23324    POP instruction can be generated.  LR should be replaced by PC.  All
23325    the checks required are already done by  USE_RETURN_INSN ().  Hence,
23326    all we really need to check here is if single register is to be
23327    returned, or multiple register return.  */
23328 void
23329 thumb2_expand_return (void)
23330 {
23331   int i, num_regs;
23332   unsigned long saved_regs_mask;
23333   arm_stack_offsets *offsets;
23334
23335   offsets = arm_get_frame_offsets ();
23336   saved_regs_mask = offsets->saved_regs_mask;
23337
23338   for (i = 0, num_regs = 0; i <= LAST_ARM_REGNUM; i++)
23339     if (saved_regs_mask & (1 << i))
23340       num_regs++;
23341
23342   if (saved_regs_mask)
23343     {
23344       if (num_regs == 1)
23345         {
23346           rtx par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (2));
23347           rtx reg = gen_rtx_REG (SImode, PC_REGNUM);
23348           rtx addr = gen_rtx_MEM (SImode,
23349                                   gen_rtx_POST_INC (SImode,
23350                                                     stack_pointer_rtx));
23351           set_mem_alias_set (addr, get_frame_alias_set ());
23352           XVECEXP (par, 0, 0) = ret_rtx;
23353           XVECEXP (par, 0, 1) = gen_rtx_SET (SImode, reg, addr);
23354           RTX_FRAME_RELATED_P (XVECEXP (par, 0, 1)) = 1;
23355           emit_jump_insn (par);
23356         }
23357       else
23358         {
23359           saved_regs_mask &= ~ (1 << LR_REGNUM);
23360           saved_regs_mask |=   (1 << PC_REGNUM);
23361           arm_emit_multi_reg_pop (saved_regs_mask);
23362         }
23363     }
23364   else
23365     {
23366       emit_jump_insn (simple_return_rtx);
23367     }
23368 }
23369
23370 void
23371 thumb1_expand_epilogue (void)
23372 {
23373   HOST_WIDE_INT amount;
23374   arm_stack_offsets *offsets;
23375   int regno;
23376
23377   /* Naked functions don't have prologues.  */
23378   if (IS_NAKED (arm_current_func_type ()))
23379     return;
23380
23381   offsets = arm_get_frame_offsets ();
23382   amount = offsets->outgoing_args - offsets->saved_regs;
23383
23384   if (frame_pointer_needed)
23385     {
23386       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
23387       amount = offsets->locals_base - offsets->saved_regs;
23388     }
23389   amount -= 4 * thumb1_extra_regs_pushed (offsets, false);
23390
23391   gcc_assert (amount >= 0);
23392   if (amount)
23393     {
23394       emit_insn (gen_blockage ());
23395
23396       if (amount < 512)
23397         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
23398                                GEN_INT (amount)));
23399       else
23400         {
23401           /* r3 is always free in the epilogue.  */
23402           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
23403
23404           emit_insn (gen_movsi (reg, GEN_INT (amount)));
23405           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
23406         }
23407     }
23408
23409   /* Emit a USE (stack_pointer_rtx), so that
23410      the stack adjustment will not be deleted.  */
23411   emit_insn (gen_force_register_use (stack_pointer_rtx));
23412
23413   if (crtl->profile || !TARGET_SCHED_PROLOG)
23414     emit_insn (gen_blockage ());
23415
23416   /* Emit a clobber for each insn that will be restored in the epilogue,
23417      so that flow2 will get register lifetimes correct.  */
23418   for (regno = 0; regno < 13; regno++)
23419     if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
23420       emit_clobber (gen_rtx_REG (SImode, regno));
23421
23422   if (! df_regs_ever_live_p (LR_REGNUM))
23423     emit_use (gen_rtx_REG (SImode, LR_REGNUM));
23424 }
23425
23426 /* Epilogue code for APCS frame.  */
23427 static void
23428 arm_expand_epilogue_apcs_frame (bool really_return)
23429 {
23430   unsigned long func_type;
23431   unsigned long saved_regs_mask;
23432   int num_regs = 0;
23433   int i;
23434   int floats_from_frame = 0;
23435   arm_stack_offsets *offsets;
23436
23437   gcc_assert (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM);
23438   func_type = arm_current_func_type ();
23439
23440   /* Get frame offsets for ARM.  */
23441   offsets = arm_get_frame_offsets ();
23442   saved_regs_mask = offsets->saved_regs_mask;
23443
23444   /* Find the offset of the floating-point save area in the frame.  */
23445   floats_from_frame = offsets->saved_args - offsets->frame;
23446
23447   /* Compute how many core registers saved and how far away the floats are.  */
23448   for (i = 0; i <= LAST_ARM_REGNUM; i++)
23449     if (saved_regs_mask & (1 << i))
23450       {
23451         num_regs++;
23452         floats_from_frame += 4;
23453       }
23454
23455   if (TARGET_HARD_FLOAT && TARGET_VFP)
23456     {
23457       int start_reg;
23458
23459       /* The offset is from IP_REGNUM.  */
23460       int saved_size = arm_get_vfp_saved_size ();
23461       if (saved_size > 0)
23462         {
23463           floats_from_frame += saved_size;
23464           emit_insn (gen_addsi3 (gen_rtx_REG (SImode, IP_REGNUM),
23465                                  hard_frame_pointer_rtx,
23466                                  GEN_INT (-floats_from_frame)));
23467         }
23468
23469       /* Generate VFP register multi-pop.  */
23470       start_reg = FIRST_VFP_REGNUM;
23471
23472       for (i = FIRST_VFP_REGNUM; i < LAST_VFP_REGNUM; i += 2)
23473         /* Look for a case where a reg does not need restoring.  */
23474         if ((!df_regs_ever_live_p (i) || call_used_regs[i])
23475             && (!df_regs_ever_live_p (i + 1)
23476                 || call_used_regs[i + 1]))
23477           {
23478             if (start_reg != i)
23479               arm_emit_vfp_multi_reg_pop (start_reg,
23480                                           (i - start_reg) / 2,
23481                                           gen_rtx_REG (SImode,
23482                                                        IP_REGNUM));
23483             start_reg = i + 2;
23484           }
23485
23486       /* Restore the remaining regs that we have discovered (or possibly
23487          even all of them, if the conditional in the for loop never
23488          fired).  */
23489       if (start_reg != i)
23490         arm_emit_vfp_multi_reg_pop (start_reg,
23491                                     (i - start_reg) / 2,
23492                                     gen_rtx_REG (SImode, IP_REGNUM));
23493     }
23494
23495   if (TARGET_IWMMXT)
23496     {
23497       /* The frame pointer is guaranteed to be non-double-word aligned, as
23498          it is set to double-word-aligned old_stack_pointer - 4.  */
23499       rtx insn;
23500       int lrm_count = (num_regs % 2) ? (num_regs + 2) : (num_regs + 1);
23501
23502       for (i = LAST_IWMMXT_REGNUM; i >= FIRST_IWMMXT_REGNUM; i--)
23503         if (df_regs_ever_live_p (i) && !call_used_regs[i])
23504           {
23505             rtx addr = gen_frame_mem (V2SImode,
23506                                  plus_constant (Pmode, hard_frame_pointer_rtx,
23507                                                 - lrm_count * 4));
23508             insn = emit_insn (gen_movsi (gen_rtx_REG (V2SImode, i), addr));
23509             REG_NOTES (insn) = alloc_reg_note (REG_CFA_RESTORE,
23510                                                gen_rtx_REG (V2SImode, i),
23511                                                NULL_RTX);
23512             lrm_count += 2;
23513           }
23514     }
23515
23516   /* saved_regs_mask should contain IP which contains old stack pointer
23517      at the time of activation creation.  Since SP and IP are adjacent registers,
23518      we can restore the value directly into SP.  */
23519   gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
23520   saved_regs_mask &= ~(1 << IP_REGNUM);
23521   saved_regs_mask |= (1 << SP_REGNUM);
23522
23523   /* There are two registers left in saved_regs_mask - LR and PC.  We
23524      only need to restore LR (the return address), but to
23525      save time we can load it directly into PC, unless we need a
23526      special function exit sequence, or we are not really returning.  */
23527   if (really_return
23528       && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
23529       && !crtl->calls_eh_return)
23530     /* Delete LR from the register mask, so that LR on
23531        the stack is loaded into the PC in the register mask.  */
23532     saved_regs_mask &= ~(1 << LR_REGNUM);
23533   else
23534     saved_regs_mask &= ~(1 << PC_REGNUM);
23535
23536   num_regs = bit_count (saved_regs_mask);
23537   if ((offsets->outgoing_args != (1 + num_regs)) || cfun->calls_alloca)
23538     {
23539       /* Unwind the stack to just below the saved registers.  */
23540       emit_insn (gen_addsi3 (stack_pointer_rtx,
23541                              hard_frame_pointer_rtx,
23542                              GEN_INT (- 4 * num_regs)));
23543     }
23544
23545   arm_emit_multi_reg_pop (saved_regs_mask);
23546
23547   if (IS_INTERRUPT (func_type))
23548     {
23549       /* Interrupt handlers will have pushed the
23550          IP onto the stack, so restore it now.  */
23551       rtx insn;
23552       rtx addr = gen_rtx_MEM (SImode,
23553                               gen_rtx_POST_INC (SImode,
23554                               stack_pointer_rtx));
23555       set_mem_alias_set (addr, get_frame_alias_set ());
23556       insn = emit_insn (gen_movsi (gen_rtx_REG (SImode, IP_REGNUM), addr));
23557       REG_NOTES (insn) = alloc_reg_note (REG_CFA_RESTORE,
23558                                          gen_rtx_REG (SImode, IP_REGNUM),
23559                                          NULL_RTX);
23560     }
23561
23562   if (!really_return || (saved_regs_mask & (1 << PC_REGNUM)))
23563     return;
23564
23565   if (crtl->calls_eh_return)
23566     emit_insn (gen_addsi3 (stack_pointer_rtx,
23567                stack_pointer_rtx,
23568                GEN_INT (ARM_EH_STACKADJ_REGNUM)));
23569
23570   if (IS_STACKALIGN (func_type))
23571     /* Restore the original stack pointer.  Before prologue, the stack was
23572        realigned and the original stack pointer saved in r0.  For details,
23573        see comment in arm_expand_prologue.  */
23574     emit_insn (gen_movsi (stack_pointer_rtx, gen_rtx_REG (SImode, 0)));
23575
23576   emit_jump_insn (simple_return_rtx);
23577 }
23578
23579 /* Generate RTL to represent ARM epilogue.  Really_return is true if the
23580    function is not a sibcall.  */
23581 void
23582 arm_expand_epilogue (bool really_return)
23583 {
23584   unsigned long func_type;
23585   unsigned long saved_regs_mask;
23586   int num_regs = 0;
23587   int i;
23588   int amount;
23589   arm_stack_offsets *offsets;
23590
23591   func_type = arm_current_func_type ();
23592
23593   /* Naked functions don't have epilogue.  Hence, generate return pattern, and
23594      let output_return_instruction take care of instruction emition if any.  */
23595   if (IS_NAKED (func_type)
23596       || (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN))
23597     {
23598       if (really_return)
23599         emit_jump_insn (simple_return_rtx);
23600       return;
23601     }
23602
23603   /* If we are throwing an exception, then we really must be doing a
23604      return, so we can't tail-call.  */
23605   gcc_assert (!crtl->calls_eh_return || really_return);
23606
23607   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
23608     {
23609       arm_expand_epilogue_apcs_frame (really_return);
23610       return;
23611     }
23612
23613   /* Get frame offsets for ARM.  */
23614   offsets = arm_get_frame_offsets ();
23615   saved_regs_mask = offsets->saved_regs_mask;
23616   num_regs = bit_count (saved_regs_mask);
23617
23618   if (frame_pointer_needed)
23619     {
23620       /* Restore stack pointer if necessary.  */
23621       if (TARGET_ARM)
23622         {
23623           /* In ARM mode, frame pointer points to first saved register.
23624              Restore stack pointer to last saved register.  */
23625           amount = offsets->frame - offsets->saved_regs;
23626
23627           /* Force out any pending memory operations that reference stacked data
23628              before stack de-allocation occurs.  */
23629           emit_insn (gen_blockage ());
23630           emit_insn (gen_addsi3 (stack_pointer_rtx,
23631                                  hard_frame_pointer_rtx,
23632                                  GEN_INT (amount)));
23633
23634           /* Emit USE(stack_pointer_rtx) to ensure that stack adjustment is not
23635              deleted.  */
23636           emit_insn (gen_force_register_use (stack_pointer_rtx));
23637         }
23638       else
23639         {
23640           /* In Thumb-2 mode, the frame pointer points to the last saved
23641              register.  */
23642           amount = offsets->locals_base - offsets->saved_regs;
23643           if (amount)
23644             emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
23645                                    hard_frame_pointer_rtx,
23646                                    GEN_INT (amount)));
23647
23648           /* Force out any pending memory operations that reference stacked data
23649              before stack de-allocation occurs.  */
23650           emit_insn (gen_blockage ());
23651           emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
23652           /* Emit USE(stack_pointer_rtx) to ensure that stack adjustment is not
23653              deleted.  */
23654           emit_insn (gen_force_register_use (stack_pointer_rtx));
23655         }
23656     }
23657   else
23658     {
23659       /* Pop off outgoing args and local frame to adjust stack pointer to
23660          last saved register.  */
23661       amount = offsets->outgoing_args - offsets->saved_regs;
23662       if (amount)
23663         {
23664           /* Force out any pending memory operations that reference stacked data
23665              before stack de-allocation occurs.  */
23666           emit_insn (gen_blockage ());
23667           emit_insn (gen_addsi3 (stack_pointer_rtx,
23668                                  stack_pointer_rtx,
23669                                  GEN_INT (amount)));
23670           /* Emit USE(stack_pointer_rtx) to ensure that stack adjustment is
23671              not deleted.  */
23672           emit_insn (gen_force_register_use (stack_pointer_rtx));
23673         }
23674     }
23675
23676   if (TARGET_HARD_FLOAT && TARGET_VFP)
23677     {
23678       /* Generate VFP register multi-pop.  */
23679       int end_reg = LAST_VFP_REGNUM + 1;
23680
23681       /* Scan the registers in reverse order.  We need to match
23682          any groupings made in the prologue and generate matching
23683          vldm operations.  The need to match groups is because,
23684          unlike pop, vldm can only do consecutive regs.  */
23685       for (i = LAST_VFP_REGNUM - 1; i >= FIRST_VFP_REGNUM; i -= 2)
23686         /* Look for a case where a reg does not need restoring.  */
23687         if ((!df_regs_ever_live_p (i) || call_used_regs[i])
23688             && (!df_regs_ever_live_p (i + 1)
23689                 || call_used_regs[i + 1]))
23690           {
23691             /* Restore the regs discovered so far (from reg+2 to
23692                end_reg).  */
23693             if (end_reg > i + 2)
23694               arm_emit_vfp_multi_reg_pop (i + 2,
23695                                           (end_reg - (i + 2)) / 2,
23696                                           stack_pointer_rtx);
23697             end_reg = i;
23698           }
23699
23700       /* Restore the remaining regs that we have discovered (or possibly
23701          even all of them, if the conditional in the for loop never
23702          fired).  */
23703       if (end_reg > i + 2)
23704         arm_emit_vfp_multi_reg_pop (i + 2,
23705                                     (end_reg - (i + 2)) / 2,
23706                                     stack_pointer_rtx);
23707     }
23708
23709   if (TARGET_IWMMXT)
23710     for (i = FIRST_IWMMXT_REGNUM; i <= LAST_IWMMXT_REGNUM; i++)
23711       if (df_regs_ever_live_p (i) && !call_used_regs[i])
23712         {
23713           rtx insn;
23714           rtx addr = gen_rtx_MEM (V2SImode,
23715                                   gen_rtx_POST_INC (SImode,
23716                                                     stack_pointer_rtx));
23717           set_mem_alias_set (addr, get_frame_alias_set ());
23718           insn = emit_insn (gen_movsi (gen_rtx_REG (V2SImode, i), addr));
23719           REG_NOTES (insn) = alloc_reg_note (REG_CFA_RESTORE,
23720                                              gen_rtx_REG (V2SImode, i),
23721                                              NULL_RTX);
23722         }
23723
23724   if (saved_regs_mask)
23725     {
23726       rtx insn;
23727       bool return_in_pc = false;
23728
23729       if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
23730           && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
23731           && !IS_STACKALIGN (func_type)
23732           && really_return
23733           && crtl->args.pretend_args_size == 0
23734           && saved_regs_mask & (1 << LR_REGNUM)
23735           && !crtl->calls_eh_return)
23736         {
23737           saved_regs_mask &= ~(1 << LR_REGNUM);
23738           saved_regs_mask |= (1 << PC_REGNUM);
23739           return_in_pc = true;
23740         }
23741
23742       if (num_regs == 1 && (!IS_INTERRUPT (func_type) || !return_in_pc))
23743         {
23744           for (i = 0; i <= LAST_ARM_REGNUM; i++)
23745             if (saved_regs_mask & (1 << i))
23746               {
23747                 rtx addr = gen_rtx_MEM (SImode,
23748                                         gen_rtx_POST_INC (SImode,
23749                                                           stack_pointer_rtx));
23750                 set_mem_alias_set (addr, get_frame_alias_set ());
23751
23752                 if (i == PC_REGNUM)
23753                   {
23754                     insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (2));
23755                     XVECEXP (insn, 0, 0) = ret_rtx;
23756                     XVECEXP (insn, 0, 1) = gen_rtx_SET (SImode,
23757                                                         gen_rtx_REG (SImode, i),
23758                                                         addr);
23759                     RTX_FRAME_RELATED_P (XVECEXP (insn, 0, 1)) = 1;
23760                     insn = emit_jump_insn (insn);
23761                   }
23762                 else
23763                   {
23764                     insn = emit_insn (gen_movsi (gen_rtx_REG (SImode, i),
23765                                                  addr));
23766                     REG_NOTES (insn) = alloc_reg_note (REG_CFA_RESTORE,
23767                                                        gen_rtx_REG (SImode, i),
23768                                                        NULL_RTX);
23769                   }
23770               }
23771         }
23772       else
23773         {
23774           if (current_tune->prefer_ldrd_strd
23775               && !optimize_function_for_size_p (cfun))
23776             {
23777               if (TARGET_THUMB2)
23778                 thumb2_emit_ldrd_pop (saved_regs_mask);
23779               else
23780                 arm_emit_multi_reg_pop (saved_regs_mask);
23781             }
23782           else
23783             arm_emit_multi_reg_pop (saved_regs_mask);
23784         }
23785
23786       if (return_in_pc == true)
23787         return;
23788     }
23789
23790   if (crtl->args.pretend_args_size)
23791     emit_insn (gen_addsi3 (stack_pointer_rtx,
23792                            stack_pointer_rtx,
23793                            GEN_INT (crtl->args.pretend_args_size)));
23794
23795   if (!really_return)
23796     return;
23797
23798   if (crtl->calls_eh_return)
23799     emit_insn (gen_addsi3 (stack_pointer_rtx,
23800                            stack_pointer_rtx,
23801                            gen_rtx_REG (SImode, ARM_EH_STACKADJ_REGNUM)));
23802
23803   if (IS_STACKALIGN (func_type))
23804     /* Restore the original stack pointer.  Before prologue, the stack was
23805        realigned and the original stack pointer saved in r0.  For details,
23806        see comment in arm_expand_prologue.  */
23807     emit_insn (gen_movsi (stack_pointer_rtx, gen_rtx_REG (SImode, 0)));
23808
23809   emit_jump_insn (simple_return_rtx);
23810 }
23811
23812 /* Implementation of insn prologue_thumb1_interwork.  This is the first
23813    "instruction" of a function called in ARM mode.  Swap to thumb mode.  */
23814
23815 const char *
23816 thumb1_output_interwork (void)
23817 {
23818   const char * name;
23819   FILE *f = asm_out_file;
23820
23821   gcc_assert (MEM_P (DECL_RTL (current_function_decl)));
23822   gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
23823               == SYMBOL_REF);
23824   name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
23825
23826   /* Generate code sequence to switch us into Thumb mode.  */
23827   /* The .code 32 directive has already been emitted by
23828      ASM_DECLARE_FUNCTION_NAME.  */
23829   asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
23830   asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
23831
23832   /* Generate a label, so that the debugger will notice the
23833      change in instruction sets.  This label is also used by
23834      the assembler to bypass the ARM code when this function
23835      is called from a Thumb encoded function elsewhere in the
23836      same file.  Hence the definition of STUB_NAME here must
23837      agree with the definition in gas/config/tc-arm.c.  */
23838
23839 #define STUB_NAME ".real_start_of"
23840
23841   fprintf (f, "\t.code\t16\n");
23842 #ifdef ARM_PE
23843   if (arm_dllexport_name_p (name))
23844     name = arm_strip_name_encoding (name);
23845 #endif
23846   asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
23847   fprintf (f, "\t.thumb_func\n");
23848   asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
23849
23850   return "";
23851 }
23852
23853 /* Handle the case of a double word load into a low register from
23854    a computed memory address.  The computed address may involve a
23855    register which is overwritten by the load.  */
23856 const char *
23857 thumb_load_double_from_address (rtx *operands)
23858 {
23859   rtx addr;
23860   rtx base;
23861   rtx offset;
23862   rtx arg1;
23863   rtx arg2;
23864
23865   gcc_assert (REG_P (operands[0]));
23866   gcc_assert (MEM_P (operands[1]));
23867
23868   /* Get the memory address.  */
23869   addr = XEXP (operands[1], 0);
23870
23871   /* Work out how the memory address is computed.  */
23872   switch (GET_CODE (addr))
23873     {
23874     case REG:
23875       operands[2] = adjust_address (operands[1], SImode, 4);
23876
23877       if (REGNO (operands[0]) == REGNO (addr))
23878         {
23879           output_asm_insn ("ldr\t%H0, %2", operands);
23880           output_asm_insn ("ldr\t%0, %1", operands);
23881         }
23882       else
23883         {
23884           output_asm_insn ("ldr\t%0, %1", operands);
23885           output_asm_insn ("ldr\t%H0, %2", operands);
23886         }
23887       break;
23888
23889     case CONST:
23890       /* Compute <address> + 4 for the high order load.  */
23891       operands[2] = adjust_address (operands[1], SImode, 4);
23892
23893       output_asm_insn ("ldr\t%0, %1", operands);
23894       output_asm_insn ("ldr\t%H0, %2", operands);
23895       break;
23896
23897     case PLUS:
23898       arg1   = XEXP (addr, 0);
23899       arg2   = XEXP (addr, 1);
23900
23901       if (CONSTANT_P (arg1))
23902         base = arg2, offset = arg1;
23903       else
23904         base = arg1, offset = arg2;
23905
23906       gcc_assert (REG_P (base));
23907
23908       /* Catch the case of <address> = <reg> + <reg> */
23909       if (REG_P (offset))
23910         {
23911           int reg_offset = REGNO (offset);
23912           int reg_base   = REGNO (base);
23913           int reg_dest   = REGNO (operands[0]);
23914
23915           /* Add the base and offset registers together into the
23916              higher destination register.  */
23917           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
23918                        reg_dest + 1, reg_base, reg_offset);
23919
23920           /* Load the lower destination register from the address in
23921              the higher destination register.  */
23922           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
23923                        reg_dest, reg_dest + 1);
23924
23925           /* Load the higher destination register from its own address
23926              plus 4.  */
23927           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
23928                        reg_dest + 1, reg_dest + 1);
23929         }
23930       else
23931         {
23932           /* Compute <address> + 4 for the high order load.  */
23933           operands[2] = adjust_address (operands[1], SImode, 4);
23934
23935           /* If the computed address is held in the low order register
23936              then load the high order register first, otherwise always
23937              load the low order register first.  */
23938           if (REGNO (operands[0]) == REGNO (base))
23939             {
23940               output_asm_insn ("ldr\t%H0, %2", operands);
23941               output_asm_insn ("ldr\t%0, %1", operands);
23942             }
23943           else
23944             {
23945               output_asm_insn ("ldr\t%0, %1", operands);
23946               output_asm_insn ("ldr\t%H0, %2", operands);
23947             }
23948         }
23949       break;
23950
23951     case LABEL_REF:
23952       /* With no registers to worry about we can just load the value
23953          directly.  */
23954       operands[2] = adjust_address (operands[1], SImode, 4);
23955
23956       output_asm_insn ("ldr\t%H0, %2", operands);
23957       output_asm_insn ("ldr\t%0, %1", operands);
23958       break;
23959
23960     default:
23961       gcc_unreachable ();
23962     }
23963
23964   return "";
23965 }
23966
23967 const char *
23968 thumb_output_move_mem_multiple (int n, rtx *operands)
23969 {
23970   rtx tmp;
23971
23972   switch (n)
23973     {
23974     case 2:
23975       if (REGNO (operands[4]) > REGNO (operands[5]))
23976         {
23977           tmp = operands[4];
23978           operands[4] = operands[5];
23979           operands[5] = tmp;
23980         }
23981       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
23982       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
23983       break;
23984
23985     case 3:
23986       if (REGNO (operands[4]) > REGNO (operands[5]))
23987         {
23988           tmp = operands[4];
23989           operands[4] = operands[5];
23990           operands[5] = tmp;
23991         }
23992       if (REGNO (operands[5]) > REGNO (operands[6]))
23993         {
23994           tmp = operands[5];
23995           operands[5] = operands[6];
23996           operands[6] = tmp;
23997         }
23998       if (REGNO (operands[4]) > REGNO (operands[5]))
23999         {
24000           tmp = operands[4];
24001           operands[4] = operands[5];
24002           operands[5] = tmp;
24003         }
24004
24005       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
24006       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
24007       break;
24008
24009     default:
24010       gcc_unreachable ();
24011     }
24012
24013   return "";
24014 }
24015
24016 /* Output a call-via instruction for thumb state.  */
24017 const char *
24018 thumb_call_via_reg (rtx reg)
24019 {
24020   int regno = REGNO (reg);
24021   rtx *labelp;
24022
24023   gcc_assert (regno < LR_REGNUM);
24024
24025   /* If we are in the normal text section we can use a single instance
24026      per compilation unit.  If we are doing function sections, then we need
24027      an entry per section, since we can't rely on reachability.  */
24028   if (in_section == text_section)
24029     {
24030       thumb_call_reg_needed = 1;
24031
24032       if (thumb_call_via_label[regno] == NULL)
24033         thumb_call_via_label[regno] = gen_label_rtx ();
24034       labelp = thumb_call_via_label + regno;
24035     }
24036   else
24037     {
24038       if (cfun->machine->call_via[regno] == NULL)
24039         cfun->machine->call_via[regno] = gen_label_rtx ();
24040       labelp = cfun->machine->call_via + regno;
24041     }
24042
24043   output_asm_insn ("bl\t%a0", labelp);
24044   return "";
24045 }
24046
24047 /* Routines for generating rtl.  */
24048 void
24049 thumb_expand_movmemqi (rtx *operands)
24050 {
24051   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
24052   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
24053   HOST_WIDE_INT len = INTVAL (operands[2]);
24054   HOST_WIDE_INT offset = 0;
24055
24056   while (len >= 12)
24057     {
24058       emit_insn (gen_movmem12b (out, in, out, in));
24059       len -= 12;
24060     }
24061
24062   if (len >= 8)
24063     {
24064       emit_insn (gen_movmem8b (out, in, out, in));
24065       len -= 8;
24066     }
24067
24068   if (len >= 4)
24069     {
24070       rtx reg = gen_reg_rtx (SImode);
24071       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
24072       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
24073       len -= 4;
24074       offset += 4;
24075     }
24076
24077   if (len >= 2)
24078     {
24079       rtx reg = gen_reg_rtx (HImode);
24080       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
24081                                               plus_constant (Pmode, in,
24082                                                              offset))));
24083       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (Pmode, out,
24084                                                                 offset)),
24085                             reg));
24086       len -= 2;
24087       offset += 2;
24088     }
24089
24090   if (len)
24091     {
24092       rtx reg = gen_reg_rtx (QImode);
24093       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
24094                                               plus_constant (Pmode, in,
24095                                                              offset))));
24096       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (Pmode, out,
24097                                                                 offset)),
24098                             reg));
24099     }
24100 }
24101
24102 void
24103 thumb_reload_out_hi (rtx *operands)
24104 {
24105   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
24106 }
24107
24108 /* Handle reading a half-word from memory during reload.  */
24109 void
24110 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
24111 {
24112   gcc_unreachable ();
24113 }
24114
24115 /* Return the length of a function name prefix
24116     that starts with the character 'c'.  */
24117 static int
24118 arm_get_strip_length (int c)
24119 {
24120   switch (c)
24121     {
24122     ARM_NAME_ENCODING_LENGTHS
24123       default: return 0;
24124     }
24125 }
24126
24127 /* Return a pointer to a function's name with any
24128    and all prefix encodings stripped from it.  */
24129 const char *
24130 arm_strip_name_encoding (const char *name)
24131 {
24132   int skip;
24133
24134   while ((skip = arm_get_strip_length (* name)))
24135     name += skip;
24136
24137   return name;
24138 }
24139
24140 /* If there is a '*' anywhere in the name's prefix, then
24141    emit the stripped name verbatim, otherwise prepend an
24142    underscore if leading underscores are being used.  */
24143 void
24144 arm_asm_output_labelref (FILE *stream, const char *name)
24145 {
24146   int skip;
24147   int verbatim = 0;
24148
24149   while ((skip = arm_get_strip_length (* name)))
24150     {
24151       verbatim |= (*name == '*');
24152       name += skip;
24153     }
24154
24155   if (verbatim)
24156     fputs (name, stream);
24157   else
24158     asm_fprintf (stream, "%U%s", name);
24159 }
24160
24161 /* This function is used to emit an EABI tag and its associated value.
24162    We emit the numerical value of the tag in case the assembler does not
24163    support textual tags.  (Eg gas prior to 2.20).  If requested we include
24164    the tag name in a comment so that anyone reading the assembler output
24165    will know which tag is being set.
24166
24167    This function is not static because arm-c.c needs it too.  */
24168
24169 void
24170 arm_emit_eabi_attribute (const char *name, int num, int val)
24171 {
24172   asm_fprintf (asm_out_file, "\t.eabi_attribute %d, %d", num, val);
24173   if (flag_verbose_asm || flag_debug_asm)
24174     asm_fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, name);
24175   asm_fprintf (asm_out_file, "\n");
24176 }
24177
24178 static void
24179 arm_file_start (void)
24180 {
24181   int val;
24182
24183   if (TARGET_UNIFIED_ASM)
24184     asm_fprintf (asm_out_file, "\t.syntax unified\n");
24185
24186   if (TARGET_BPABI)
24187     {
24188       const char *fpu_name;
24189       if (arm_selected_arch)
24190         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_selected_arch->name);
24191       else if (strncmp (arm_selected_cpu->name, "generic", 7) == 0)
24192         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_selected_cpu->name + 8);
24193       else
24194         asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_selected_cpu->name);
24195
24196       if (TARGET_SOFT_FLOAT)
24197         {
24198           fpu_name = "softvfp";
24199         }
24200       else
24201         {
24202           fpu_name = arm_fpu_desc->name;
24203           if (arm_fpu_desc->model == ARM_FP_MODEL_VFP)
24204             {
24205               if (TARGET_HARD_FLOAT)
24206                 arm_emit_eabi_attribute ("Tag_ABI_HardFP_use", 27, 3);
24207               if (TARGET_HARD_FLOAT_ABI)
24208                 arm_emit_eabi_attribute ("Tag_ABI_VFP_args", 28, 1);
24209             }
24210         }
24211       asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
24212
24213       /* Some of these attributes only apply when the corresponding features
24214          are used.  However we don't have any easy way of figuring this out.
24215          Conservatively record the setting that would have been used.  */
24216
24217       if (flag_rounding_math)
24218         arm_emit_eabi_attribute ("Tag_ABI_FP_rounding", 19, 1);
24219
24220       if (!flag_unsafe_math_optimizations)
24221         {
24222           arm_emit_eabi_attribute ("Tag_ABI_FP_denormal", 20, 1);
24223           arm_emit_eabi_attribute ("Tag_ABI_FP_exceptions", 21, 1);
24224         }
24225       if (flag_signaling_nans)
24226         arm_emit_eabi_attribute ("Tag_ABI_FP_user_exceptions", 22, 1);
24227
24228       arm_emit_eabi_attribute ("Tag_ABI_FP_number_model", 23,
24229                            flag_finite_math_only ? 1 : 3);
24230
24231       arm_emit_eabi_attribute ("Tag_ABI_align8_needed", 24, 1);
24232       arm_emit_eabi_attribute ("Tag_ABI_align8_preserved", 25, 1);
24233       arm_emit_eabi_attribute ("Tag_ABI_enum_size", 26,
24234                                flag_short_enums ? 1 : 2);
24235
24236       /* Tag_ABI_optimization_goals.  */
24237       if (optimize_size)
24238         val = 4;
24239       else if (optimize >= 2)
24240         val = 2;
24241       else if (optimize)
24242         val = 1;
24243       else
24244         val = 6;
24245       arm_emit_eabi_attribute ("Tag_ABI_optimization_goals", 30, val);
24246
24247       arm_emit_eabi_attribute ("Tag_CPU_unaligned_access", 34,
24248                                unaligned_access);
24249
24250       if (arm_fp16_format)
24251         arm_emit_eabi_attribute ("Tag_ABI_FP_16bit_format", 38,
24252                              (int) arm_fp16_format);
24253
24254       if (arm_lang_output_object_attributes_hook)
24255         arm_lang_output_object_attributes_hook();
24256     }
24257
24258   default_file_start ();
24259 }
24260
24261 static void
24262 arm_file_end (void)
24263 {
24264   int regno;
24265
24266   if (NEED_INDICATE_EXEC_STACK)
24267     /* Add .note.GNU-stack.  */
24268     file_end_indicate_exec_stack ();
24269
24270   if (! thumb_call_reg_needed)
24271     return;
24272
24273   switch_to_section (text_section);
24274   asm_fprintf (asm_out_file, "\t.code 16\n");
24275   ASM_OUTPUT_ALIGN (asm_out_file, 1);
24276
24277   for (regno = 0; regno < LR_REGNUM; regno++)
24278     {
24279       rtx label = thumb_call_via_label[regno];
24280
24281       if (label != 0)
24282         {
24283           targetm.asm_out.internal_label (asm_out_file, "L",
24284                                           CODE_LABEL_NUMBER (label));
24285           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
24286         }
24287     }
24288 }
24289
24290 #ifndef ARM_PE
24291 /* Symbols in the text segment can be accessed without indirecting via the
24292    constant pool; it may take an extra binary operation, but this is still
24293    faster than indirecting via memory.  Don't do this when not optimizing,
24294    since we won't be calculating al of the offsets necessary to do this
24295    simplification.  */
24296
24297 static void
24298 arm_encode_section_info (tree decl, rtx rtl, int first)
24299 {
24300   if (optimize > 0 && TREE_CONSTANT (decl))
24301     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
24302
24303   default_encode_section_info (decl, rtl, first);
24304 }
24305 #endif /* !ARM_PE */
24306
24307 static void
24308 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
24309 {
24310   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
24311       && !strcmp (prefix, "L"))
24312     {
24313       arm_ccfsm_state = 0;
24314       arm_target_insn = NULL;
24315     }
24316   default_internal_label (stream, prefix, labelno);
24317 }
24318
24319 /* Output code to add DELTA to the first argument, and then jump
24320    to FUNCTION.  Used for C++ multiple inheritance.  */
24321 static void
24322 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
24323                      HOST_WIDE_INT delta,
24324                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
24325                      tree function)
24326 {
24327   static int thunk_label = 0;
24328   char label[256];
24329   char labelpc[256];
24330   int mi_delta = delta;
24331   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
24332   int shift = 0;
24333   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
24334                     ? 1 : 0);
24335   if (mi_delta < 0)
24336     mi_delta = - mi_delta;
24337
24338   final_start_function (emit_barrier (), file, 1);
24339
24340   if (TARGET_THUMB1)
24341     {
24342       int labelno = thunk_label++;
24343       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
24344       /* Thunks are entered in arm mode when avaiable.  */
24345       if (TARGET_THUMB1_ONLY)
24346         {
24347           /* push r3 so we can use it as a temporary.  */
24348           /* TODO: Omit this save if r3 is not used.  */
24349           fputs ("\tpush {r3}\n", file);
24350           fputs ("\tldr\tr3, ", file);
24351         }
24352       else
24353         {
24354           fputs ("\tldr\tr12, ", file);
24355         }
24356       assemble_name (file, label);
24357       fputc ('\n', file);
24358       if (flag_pic)
24359         {
24360           /* If we are generating PIC, the ldr instruction below loads
24361              "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
24362              the address of the add + 8, so we have:
24363
24364              r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
24365                  = target + 1.
24366
24367              Note that we have "+ 1" because some versions of GNU ld
24368              don't set the low bit of the result for R_ARM_REL32
24369              relocations against thumb function symbols.
24370              On ARMv6M this is +4, not +8.  */
24371           ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
24372           assemble_name (file, labelpc);
24373           fputs (":\n", file);
24374           if (TARGET_THUMB1_ONLY)
24375             {
24376               /* This is 2 insns after the start of the thunk, so we know it
24377                  is 4-byte aligned.  */
24378               fputs ("\tadd\tr3, pc, r3\n", file);
24379               fputs ("\tmov r12, r3\n", file);
24380             }
24381           else
24382             fputs ("\tadd\tr12, pc, r12\n", file);
24383         }
24384       else if (TARGET_THUMB1_ONLY)
24385         fputs ("\tmov r12, r3\n", file);
24386     }
24387   if (TARGET_THUMB1_ONLY)
24388     {
24389       if (mi_delta > 255)
24390         {
24391           fputs ("\tldr\tr3, ", file);
24392           assemble_name (file, label);
24393           fputs ("+4\n", file);
24394           asm_fprintf (file, "\t%s\t%r, %r, r3\n",
24395                        mi_op, this_regno, this_regno);
24396         }
24397       else if (mi_delta != 0)
24398         {
24399           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
24400                        mi_op, this_regno, this_regno,
24401                        mi_delta);
24402         }
24403     }
24404   else
24405     {
24406       /* TODO: Use movw/movt for large constants when available.  */
24407       while (mi_delta != 0)
24408         {
24409           if ((mi_delta & (3 << shift)) == 0)
24410             shift += 2;
24411           else
24412             {
24413               asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
24414                            mi_op, this_regno, this_regno,
24415                            mi_delta & (0xff << shift));
24416               mi_delta &= ~(0xff << shift);
24417               shift += 8;
24418             }
24419         }
24420     }
24421   if (TARGET_THUMB1)
24422     {
24423       if (TARGET_THUMB1_ONLY)
24424         fputs ("\tpop\t{r3}\n", file);
24425
24426       fprintf (file, "\tbx\tr12\n");
24427       ASM_OUTPUT_ALIGN (file, 2);
24428       assemble_name (file, label);
24429       fputs (":\n", file);
24430       if (flag_pic)
24431         {
24432           /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
24433           rtx tem = XEXP (DECL_RTL (function), 0);
24434           tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
24435           tem = gen_rtx_MINUS (GET_MODE (tem),
24436                                tem,
24437                                gen_rtx_SYMBOL_REF (Pmode,
24438                                                    ggc_strdup (labelpc)));
24439           assemble_integer (tem, 4, BITS_PER_WORD, 1);
24440         }
24441       else
24442         /* Output ".word .LTHUNKn".  */
24443         assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
24444
24445       if (TARGET_THUMB1_ONLY && mi_delta > 255)
24446         assemble_integer (GEN_INT(mi_delta), 4, BITS_PER_WORD, 1);
24447     }
24448   else
24449     {
24450       fputs ("\tb\t", file);
24451       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
24452       if (NEED_PLT_RELOC)
24453         fputs ("(PLT)", file);
24454       fputc ('\n', file);
24455     }
24456
24457   final_end_function ();
24458 }
24459
24460 int
24461 arm_emit_vector_const (FILE *file, rtx x)
24462 {
24463   int i;
24464   const char * pattern;
24465
24466   gcc_assert (GET_CODE (x) == CONST_VECTOR);
24467
24468   switch (GET_MODE (x))
24469     {
24470     case V2SImode: pattern = "%08x"; break;
24471     case V4HImode: pattern = "%04x"; break;
24472     case V8QImode: pattern = "%02x"; break;
24473     default:       gcc_unreachable ();
24474     }
24475
24476   fprintf (file, "0x");
24477   for (i = CONST_VECTOR_NUNITS (x); i--;)
24478     {
24479       rtx element;
24480
24481       element = CONST_VECTOR_ELT (x, i);
24482       fprintf (file, pattern, INTVAL (element));
24483     }
24484
24485   return 1;
24486 }
24487
24488 /* Emit a fp16 constant appropriately padded to occupy a 4-byte word.
24489    HFmode constant pool entries are actually loaded with ldr.  */
24490 void
24491 arm_emit_fp16_const (rtx c)
24492 {
24493   REAL_VALUE_TYPE r;
24494   long bits;
24495
24496   REAL_VALUE_FROM_CONST_DOUBLE (r, c);
24497   bits = real_to_target (NULL, &r, HFmode);
24498   if (WORDS_BIG_ENDIAN)
24499     assemble_zeros (2);
24500   assemble_integer (GEN_INT (bits), 2, BITS_PER_WORD, 1);
24501   if (!WORDS_BIG_ENDIAN)
24502     assemble_zeros (2);
24503 }
24504
24505 const char *
24506 arm_output_load_gr (rtx *operands)
24507 {
24508   rtx reg;
24509   rtx offset;
24510   rtx wcgr;
24511   rtx sum;
24512
24513   if (!MEM_P (operands [1])
24514       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
24515       || !REG_P (reg = XEXP (sum, 0))
24516       || !CONST_INT_P (offset = XEXP (sum, 1))
24517       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
24518     return "wldrw%?\t%0, %1";
24519
24520   /* Fix up an out-of-range load of a GR register.  */
24521   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
24522   wcgr = operands[0];
24523   operands[0] = reg;
24524   output_asm_insn ("ldr%?\t%0, %1", operands);
24525
24526   operands[0] = wcgr;
24527   operands[1] = reg;
24528   output_asm_insn ("tmcr%?\t%0, %1", operands);
24529   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
24530
24531   return "";
24532 }
24533
24534 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
24535
24536    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
24537    named arg and all anonymous args onto the stack.
24538    XXX I know the prologue shouldn't be pushing registers, but it is faster
24539    that way.  */
24540
24541 static void
24542 arm_setup_incoming_varargs (cumulative_args_t pcum_v,
24543                             enum machine_mode mode,
24544                             tree type,
24545                             int *pretend_size,
24546                             int second_time ATTRIBUTE_UNUSED)
24547 {
24548   CUMULATIVE_ARGS *pcum = get_cumulative_args (pcum_v);
24549   int nregs;
24550
24551   cfun->machine->uses_anonymous_args = 1;
24552   if (pcum->pcs_variant <= ARM_PCS_AAPCS_LOCAL)
24553     {
24554       nregs = pcum->aapcs_ncrn;
24555       if ((nregs & 1) && arm_needs_doubleword_align (mode, type))
24556         nregs++;
24557     }
24558   else
24559     nregs = pcum->nregs;
24560
24561   if (nregs < NUM_ARG_REGS)
24562     *pretend_size = (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
24563 }
24564
24565 /* Return nonzero if the CONSUMER instruction (a store) does not need
24566    PRODUCER's value to calculate the address.  */
24567
24568 int
24569 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
24570 {
24571   rtx value = PATTERN (producer);
24572   rtx addr = PATTERN (consumer);
24573
24574   if (GET_CODE (value) == COND_EXEC)
24575     value = COND_EXEC_CODE (value);
24576   if (GET_CODE (value) == PARALLEL)
24577     value = XVECEXP (value, 0, 0);
24578   value = XEXP (value, 0);
24579   if (GET_CODE (addr) == COND_EXEC)
24580     addr = COND_EXEC_CODE (addr);
24581   if (GET_CODE (addr) == PARALLEL)
24582     addr = XVECEXP (addr, 0, 0);
24583   addr = XEXP (addr, 0);
24584
24585   return !reg_overlap_mentioned_p (value, addr);
24586 }
24587
24588 /* Return nonzero if the CONSUMER instruction (a store) does need
24589    PRODUCER's value to calculate the address.  */
24590
24591 int
24592 arm_early_store_addr_dep (rtx producer, rtx consumer)
24593 {
24594   return !arm_no_early_store_addr_dep (producer, consumer);
24595 }
24596
24597 /* Return nonzero if the CONSUMER instruction (a load) does need
24598    PRODUCER's value to calculate the address.  */
24599
24600 int
24601 arm_early_load_addr_dep (rtx producer, rtx consumer)
24602 {
24603   rtx value = PATTERN (producer);
24604   rtx addr = PATTERN (consumer);
24605
24606   if (GET_CODE (value) == COND_EXEC)
24607     value = COND_EXEC_CODE (value);
24608   if (GET_CODE (value) == PARALLEL)
24609     value = XVECEXP (value, 0, 0);
24610   value = XEXP (value, 0);
24611   if (GET_CODE (addr) == COND_EXEC)
24612     addr = COND_EXEC_CODE (addr);
24613   if (GET_CODE (addr) == PARALLEL)
24614     {
24615       if (GET_CODE (XVECEXP (addr, 0, 0)) == RETURN)
24616         addr = XVECEXP (addr, 0, 1);
24617       else
24618         addr = XVECEXP (addr, 0, 0);
24619     }
24620   addr = XEXP (addr, 1);
24621
24622   return reg_overlap_mentioned_p (value, addr);
24623 }
24624
24625 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
24626    have an early register shift value or amount dependency on the
24627    result of PRODUCER.  */
24628
24629 int
24630 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
24631 {
24632   rtx value = PATTERN (producer);
24633   rtx op = PATTERN (consumer);
24634   rtx early_op;
24635
24636   if (GET_CODE (value) == COND_EXEC)
24637     value = COND_EXEC_CODE (value);
24638   if (GET_CODE (value) == PARALLEL)
24639     value = XVECEXP (value, 0, 0);
24640   value = XEXP (value, 0);
24641   if (GET_CODE (op) == COND_EXEC)
24642     op = COND_EXEC_CODE (op);
24643   if (GET_CODE (op) == PARALLEL)
24644     op = XVECEXP (op, 0, 0);
24645   op = XEXP (op, 1);
24646
24647   early_op = XEXP (op, 0);
24648   /* This is either an actual independent shift, or a shift applied to
24649      the first operand of another operation.  We want the whole shift
24650      operation.  */
24651   if (REG_P (early_op))
24652     early_op = op;
24653
24654   return !reg_overlap_mentioned_p (value, early_op);
24655 }
24656
24657 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
24658    have an early register shift value dependency on the result of
24659    PRODUCER.  */
24660
24661 int
24662 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
24663 {
24664   rtx value = PATTERN (producer);
24665   rtx op = PATTERN (consumer);
24666   rtx early_op;
24667
24668   if (GET_CODE (value) == COND_EXEC)
24669     value = COND_EXEC_CODE (value);
24670   if (GET_CODE (value) == PARALLEL)
24671     value = XVECEXP (value, 0, 0);
24672   value = XEXP (value, 0);
24673   if (GET_CODE (op) == COND_EXEC)
24674     op = COND_EXEC_CODE (op);
24675   if (GET_CODE (op) == PARALLEL)
24676     op = XVECEXP (op, 0, 0);
24677   op = XEXP (op, 1);
24678
24679   early_op = XEXP (op, 0);
24680
24681   /* This is either an actual independent shift, or a shift applied to
24682      the first operand of another operation.  We want the value being
24683      shifted, in either case.  */
24684   if (!REG_P (early_op))
24685     early_op = XEXP (early_op, 0);
24686
24687   return !reg_overlap_mentioned_p (value, early_op);
24688 }
24689
24690 /* Return nonzero if the CONSUMER (a mul or mac op) does not
24691    have an early register mult dependency on the result of
24692    PRODUCER.  */
24693
24694 int
24695 arm_no_early_mul_dep (rtx producer, rtx consumer)
24696 {
24697   rtx value = PATTERN (producer);
24698   rtx op = PATTERN (consumer);
24699
24700   if (GET_CODE (value) == COND_EXEC)
24701     value = COND_EXEC_CODE (value);
24702   if (GET_CODE (value) == PARALLEL)
24703     value = XVECEXP (value, 0, 0);
24704   value = XEXP (value, 0);
24705   if (GET_CODE (op) == COND_EXEC)
24706     op = COND_EXEC_CODE (op);
24707   if (GET_CODE (op) == PARALLEL)
24708     op = XVECEXP (op, 0, 0);
24709   op = XEXP (op, 1);
24710
24711   if (GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
24712     {
24713       if (GET_CODE (XEXP (op, 0)) == MULT)
24714         return !reg_overlap_mentioned_p (value, XEXP (op, 0));
24715       else
24716         return !reg_overlap_mentioned_p (value, XEXP (op, 1));
24717     }
24718
24719   return 0;
24720 }
24721
24722 /* We can't rely on the caller doing the proper promotion when
24723    using APCS or ATPCS.  */
24724
24725 static bool
24726 arm_promote_prototypes (const_tree t ATTRIBUTE_UNUSED)
24727 {
24728     return !TARGET_AAPCS_BASED;
24729 }
24730
24731 static enum machine_mode
24732 arm_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
24733                            enum machine_mode mode,
24734                            int *punsignedp ATTRIBUTE_UNUSED,
24735                            const_tree fntype ATTRIBUTE_UNUSED,
24736                            int for_return ATTRIBUTE_UNUSED)
24737 {
24738   if (GET_MODE_CLASS (mode) == MODE_INT
24739       && GET_MODE_SIZE (mode) < 4)
24740     return SImode;
24741
24742   return mode;
24743 }
24744
24745 /* AAPCS based ABIs use short enums by default.  */
24746
24747 static bool
24748 arm_default_short_enums (void)
24749 {
24750   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
24751 }
24752
24753
24754 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
24755
24756 static bool
24757 arm_align_anon_bitfield (void)
24758 {
24759   return TARGET_AAPCS_BASED;
24760 }
24761
24762
24763 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
24764
24765 static tree
24766 arm_cxx_guard_type (void)
24767 {
24768   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
24769 }
24770
24771 /* Return non-zero iff the consumer (a multiply-accumulate or a
24772    multiple-subtract instruction) has an accumulator dependency on the
24773    result of the producer and no other dependency on that result.  It
24774    does not check if the producer is multiply-accumulate instruction.  */
24775 int
24776 arm_mac_accumulator_is_result (rtx producer, rtx consumer)
24777 {
24778   rtx result;
24779   rtx op0, op1, acc;
24780
24781   producer = PATTERN (producer);
24782   consumer = PATTERN (consumer);
24783
24784   if (GET_CODE (producer) == COND_EXEC)
24785     producer = COND_EXEC_CODE (producer);
24786   if (GET_CODE (consumer) == COND_EXEC)
24787     consumer = COND_EXEC_CODE (consumer);
24788
24789   if (GET_CODE (producer) != SET)
24790     return 0;
24791
24792   result = XEXP (producer, 0);
24793
24794   if (GET_CODE (consumer) != SET)
24795     return 0;
24796
24797   /* Check that the consumer is of the form
24798      (set (...) (plus (mult ...) (...)))
24799      or
24800      (set (...) (minus (...) (mult ...))).  */
24801   if (GET_CODE (XEXP (consumer, 1)) == PLUS)
24802     {
24803       if (GET_CODE (XEXP (XEXP (consumer, 1), 0)) != MULT)
24804         return 0;
24805
24806       op0 = XEXP (XEXP (XEXP (consumer, 1), 0), 0);
24807       op1 = XEXP (XEXP (XEXP (consumer, 1), 0), 1);
24808       acc = XEXP (XEXP (consumer, 1), 1);
24809     }
24810   else if (GET_CODE (XEXP (consumer, 1)) == MINUS)
24811     {
24812       if (GET_CODE (XEXP (XEXP (consumer, 1), 1)) != MULT)
24813         return 0;
24814
24815       op0 = XEXP (XEXP (XEXP (consumer, 1), 1), 0);
24816       op1 = XEXP (XEXP (XEXP (consumer, 1), 1), 1);
24817       acc = XEXP (XEXP (consumer, 1), 0);
24818     }
24819   else
24820     return 0;
24821
24822   return (reg_overlap_mentioned_p (result, acc)
24823           && !reg_overlap_mentioned_p (result, op0)
24824           && !reg_overlap_mentioned_p (result, op1));
24825 }
24826
24827 /* Return non-zero if the consumer (a multiply-accumulate instruction)
24828    has an accumulator dependency on the result of the producer (a
24829    multiplication instruction) and no other dependency on that result.  */
24830 int
24831 arm_mac_accumulator_is_mul_result (rtx producer, rtx consumer)
24832 {
24833   rtx mul = PATTERN (producer);
24834   rtx mac = PATTERN (consumer);
24835   rtx mul_result;
24836   rtx mac_op0, mac_op1, mac_acc;
24837
24838   if (GET_CODE (mul) == COND_EXEC)
24839     mul = COND_EXEC_CODE (mul);
24840   if (GET_CODE (mac) == COND_EXEC)
24841     mac = COND_EXEC_CODE (mac);
24842
24843   /* Check that mul is of the form (set (...) (mult ...))
24844      and mla is of the form (set (...) (plus (mult ...) (...))).  */
24845   if ((GET_CODE (mul) != SET || GET_CODE (XEXP (mul, 1)) != MULT)
24846       || (GET_CODE (mac) != SET || GET_CODE (XEXP (mac, 1)) != PLUS
24847           || GET_CODE (XEXP (XEXP (mac, 1), 0)) != MULT))
24848     return 0;
24849
24850   mul_result = XEXP (mul, 0);
24851   mac_op0 = XEXP (XEXP (XEXP (mac, 1), 0), 0);
24852   mac_op1 = XEXP (XEXP (XEXP (mac, 1), 0), 1);
24853   mac_acc = XEXP (XEXP (mac, 1), 1);
24854
24855   return (reg_overlap_mentioned_p (mul_result, mac_acc)
24856           && !reg_overlap_mentioned_p (mul_result, mac_op0)
24857           && !reg_overlap_mentioned_p (mul_result, mac_op1));
24858 }
24859
24860
24861 /* The EABI says test the least significant bit of a guard variable.  */
24862
24863 static bool
24864 arm_cxx_guard_mask_bit (void)
24865 {
24866   return TARGET_AAPCS_BASED;
24867 }
24868
24869
24870 /* The EABI specifies that all array cookies are 8 bytes long.  */
24871
24872 static tree
24873 arm_get_cookie_size (tree type)
24874 {
24875   tree size;
24876
24877   if (!TARGET_AAPCS_BASED)
24878     return default_cxx_get_cookie_size (type);
24879
24880   size = build_int_cst (sizetype, 8);
24881   return size;
24882 }
24883
24884
24885 /* The EABI says that array cookies should also contain the element size.  */
24886
24887 static bool
24888 arm_cookie_has_size (void)
24889 {
24890   return TARGET_AAPCS_BASED;
24891 }
24892
24893
24894 /* The EABI says constructors and destructors should return a pointer to
24895    the object constructed/destroyed.  */
24896
24897 static bool
24898 arm_cxx_cdtor_returns_this (void)
24899 {
24900   return TARGET_AAPCS_BASED;
24901 }
24902
24903 /* The EABI says that an inline function may never be the key
24904    method.  */
24905
24906 static bool
24907 arm_cxx_key_method_may_be_inline (void)
24908 {
24909   return !TARGET_AAPCS_BASED;
24910 }
24911
24912 static void
24913 arm_cxx_determine_class_data_visibility (tree decl)
24914 {
24915   if (!TARGET_AAPCS_BASED
24916       || !TARGET_DLLIMPORT_DECL_ATTRIBUTES)
24917     return;
24918
24919   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
24920      is exported.  However, on systems without dynamic vague linkage,
24921      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
24922   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
24923     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
24924   else
24925     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
24926   DECL_VISIBILITY_SPECIFIED (decl) = 1;
24927 }
24928
24929 static bool
24930 arm_cxx_class_data_always_comdat (void)
24931 {
24932   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
24933      vague linkage if the class has no key function.  */
24934   return !TARGET_AAPCS_BASED;
24935 }
24936
24937
24938 /* The EABI says __aeabi_atexit should be used to register static
24939    destructors.  */
24940
24941 static bool
24942 arm_cxx_use_aeabi_atexit (void)
24943 {
24944   return TARGET_AAPCS_BASED;
24945 }
24946
24947
24948 void
24949 arm_set_return_address (rtx source, rtx scratch)
24950 {
24951   arm_stack_offsets *offsets;
24952   HOST_WIDE_INT delta;
24953   rtx addr;
24954   unsigned long saved_regs;
24955
24956   offsets = arm_get_frame_offsets ();
24957   saved_regs = offsets->saved_regs_mask;
24958
24959   if ((saved_regs & (1 << LR_REGNUM)) == 0)
24960     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
24961   else
24962     {
24963       if (frame_pointer_needed)
24964         addr = plus_constant (Pmode, hard_frame_pointer_rtx, -4);
24965       else
24966         {
24967           /* LR will be the first saved register.  */
24968           delta = offsets->outgoing_args - (offsets->frame + 4);
24969
24970
24971           if (delta >= 4096)
24972             {
24973               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
24974                                      GEN_INT (delta & ~4095)));
24975               addr = scratch;
24976               delta &= 4095;
24977             }
24978           else
24979             addr = stack_pointer_rtx;
24980
24981           addr = plus_constant (Pmode, addr, delta);
24982         }
24983       emit_move_insn (gen_frame_mem (Pmode, addr), source);
24984     }
24985 }
24986
24987
24988 void
24989 thumb_set_return_address (rtx source, rtx scratch)
24990 {
24991   arm_stack_offsets *offsets;
24992   HOST_WIDE_INT delta;
24993   HOST_WIDE_INT limit;
24994   int reg;
24995   rtx addr;
24996   unsigned long mask;
24997
24998   emit_use (source);
24999
25000   offsets = arm_get_frame_offsets ();
25001   mask = offsets->saved_regs_mask;
25002   if (mask & (1 << LR_REGNUM))
25003     {
25004       limit = 1024;
25005       /* Find the saved regs.  */
25006       if (frame_pointer_needed)
25007         {
25008           delta = offsets->soft_frame - offsets->saved_args;
25009           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
25010           if (TARGET_THUMB1)
25011             limit = 128;
25012         }
25013       else
25014         {
25015           delta = offsets->outgoing_args - offsets->saved_args;
25016           reg = SP_REGNUM;
25017         }
25018       /* Allow for the stack frame.  */
25019       if (TARGET_THUMB1 && TARGET_BACKTRACE)
25020         delta -= 16;
25021       /* The link register is always the first saved register.  */
25022       delta -= 4;
25023
25024       /* Construct the address.  */
25025       addr = gen_rtx_REG (SImode, reg);
25026       if (delta > limit)
25027         {
25028           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
25029           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
25030           addr = scratch;
25031         }
25032       else
25033         addr = plus_constant (Pmode, addr, delta);
25034
25035       emit_move_insn (gen_frame_mem (Pmode, addr), source);
25036     }
25037   else
25038     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
25039 }
25040
25041 /* Implements target hook vector_mode_supported_p.  */
25042 bool
25043 arm_vector_mode_supported_p (enum machine_mode mode)
25044 {
25045   /* Neon also supports V2SImode, etc. listed in the clause below.  */
25046   if (TARGET_NEON && (mode == V2SFmode || mode == V4SImode || mode == V8HImode
25047       || mode == V16QImode || mode == V4SFmode || mode == V2DImode))
25048     return true;
25049
25050   if ((TARGET_NEON || TARGET_IWMMXT)
25051       && ((mode == V2SImode)
25052           || (mode == V4HImode)
25053           || (mode == V8QImode)))
25054     return true;
25055
25056   if (TARGET_INT_SIMD && (mode == V4UQQmode || mode == V4QQmode
25057       || mode == V2UHQmode || mode == V2HQmode || mode == V2UHAmode
25058       || mode == V2HAmode))
25059     return true;
25060
25061   return false;
25062 }
25063
25064 /* Implements target hook array_mode_supported_p.  */
25065
25066 static bool
25067 arm_array_mode_supported_p (enum machine_mode mode,
25068                             unsigned HOST_WIDE_INT nelems)
25069 {
25070   if (TARGET_NEON
25071       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode))
25072       && (nelems >= 2 && nelems <= 4))
25073     return true;
25074
25075   return false;
25076 }
25077
25078 /* Use the option -mvectorize-with-neon-double to override the use of quardword
25079    registers when autovectorizing for Neon, at least until multiple vector
25080    widths are supported properly by the middle-end.  */
25081
25082 static enum machine_mode
25083 arm_preferred_simd_mode (enum machine_mode mode)
25084 {
25085   if (TARGET_NEON)
25086     switch (mode)
25087       {
25088       case SFmode:
25089         return TARGET_NEON_VECTORIZE_DOUBLE ? V2SFmode : V4SFmode;
25090       case SImode:
25091         return TARGET_NEON_VECTORIZE_DOUBLE ? V2SImode : V4SImode;
25092       case HImode:
25093         return TARGET_NEON_VECTORIZE_DOUBLE ? V4HImode : V8HImode;
25094       case QImode:
25095         return TARGET_NEON_VECTORIZE_DOUBLE ? V8QImode : V16QImode;
25096       case DImode:
25097         if (!TARGET_NEON_VECTORIZE_DOUBLE)
25098           return V2DImode;
25099         break;
25100
25101       default:;
25102       }
25103
25104   if (TARGET_REALLY_IWMMXT)
25105     switch (mode)
25106       {
25107       case SImode:
25108         return V2SImode;
25109       case HImode:
25110         return V4HImode;
25111       case QImode:
25112         return V8QImode;
25113
25114       default:;
25115       }
25116
25117   return word_mode;
25118 }
25119
25120 /* Implement TARGET_CLASS_LIKELY_SPILLED_P.
25121
25122    We need to define this for LO_REGS on Thumb-1.  Otherwise we can end up
25123    using r0-r4 for function arguments, r7 for the stack frame and don't have
25124    enough left over to do doubleword arithmetic.  For Thumb-2 all the
25125    potentially problematic instructions accept high registers so this is not
25126    necessary.  Care needs to be taken to avoid adding new Thumb-2 patterns
25127    that require many low registers.  */
25128 static bool
25129 arm_class_likely_spilled_p (reg_class_t rclass)
25130 {
25131   if ((TARGET_THUMB1 && rclass == LO_REGS)
25132       || rclass  == CC_REG)
25133     return true;
25134
25135   return false;
25136 }
25137
25138 /* Implements target hook small_register_classes_for_mode_p.  */
25139 bool
25140 arm_small_register_classes_for_mode_p (enum machine_mode mode ATTRIBUTE_UNUSED)
25141 {
25142   return TARGET_THUMB1;
25143 }
25144
25145 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
25146    ARM insns and therefore guarantee that the shift count is modulo 256.
25147    DImode shifts (those implemented by lib1funcs.S or by optabs.c)
25148    guarantee no particular behavior for out-of-range counts.  */
25149
25150 static unsigned HOST_WIDE_INT
25151 arm_shift_truncation_mask (enum machine_mode mode)
25152 {
25153   return mode == SImode ? 255 : 0;
25154 }
25155
25156
25157 /* Map internal gcc register numbers to DWARF2 register numbers.  */
25158
25159 unsigned int
25160 arm_dbx_register_number (unsigned int regno)
25161 {
25162   if (regno < 16)
25163     return regno;
25164
25165   if (IS_VFP_REGNUM (regno))
25166     {
25167       /* See comment in arm_dwarf_register_span.  */
25168       if (VFP_REGNO_OK_FOR_SINGLE (regno))
25169         return 64 + regno - FIRST_VFP_REGNUM;
25170       else
25171         return 256 + (regno - FIRST_VFP_REGNUM) / 2;
25172     }
25173
25174   if (IS_IWMMXT_GR_REGNUM (regno))
25175     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
25176
25177   if (IS_IWMMXT_REGNUM (regno))
25178     return 112 + regno - FIRST_IWMMXT_REGNUM;
25179
25180   gcc_unreachable ();
25181 }
25182
25183 /* Dwarf models VFPv3 registers as 32 64-bit registers.
25184    GCC models tham as 64 32-bit registers, so we need to describe this to
25185    the DWARF generation code.  Other registers can use the default.  */
25186 static rtx
25187 arm_dwarf_register_span (rtx rtl)
25188 {
25189   unsigned regno;
25190   int nregs;
25191   int i;
25192   rtx p;
25193
25194   regno = REGNO (rtl);
25195   if (!IS_VFP_REGNUM (regno))
25196     return NULL_RTX;
25197
25198   /* XXX FIXME: The EABI defines two VFP register ranges:
25199         64-95: Legacy VFPv2 numbering for S0-S31 (obsolescent)
25200         256-287: D0-D31
25201      The recommended encoding for S0-S31 is a DW_OP_bit_piece of the
25202      corresponding D register.  Until GDB supports this, we shall use the
25203      legacy encodings.  We also use these encodings for D0-D15 for
25204      compatibility with older debuggers.  */
25205   if (VFP_REGNO_OK_FOR_SINGLE (regno))
25206     return NULL_RTX;
25207
25208   nregs = GET_MODE_SIZE (GET_MODE (rtl)) / 8;
25209   p = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
25210   regno = (regno - FIRST_VFP_REGNUM) / 2;
25211   for (i = 0; i < nregs; i++)
25212     XVECEXP (p, 0, i) = gen_rtx_REG (DImode, 256 + regno + i);
25213
25214   return p;
25215 }
25216
25217 #if ARM_UNWIND_INFO
25218 /* Emit unwind directives for a store-multiple instruction or stack pointer
25219    push during alignment.
25220    These should only ever be generated by the function prologue code, so
25221    expect them to have a particular form.  */
25222
25223 static void
25224 arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
25225 {
25226   int i;
25227   HOST_WIDE_INT offset;
25228   HOST_WIDE_INT nregs;
25229   int reg_size;
25230   unsigned reg;
25231   unsigned lastreg;
25232   rtx e;
25233
25234   e = XVECEXP (p, 0, 0);
25235   if (GET_CODE (e) != SET)
25236     abort ();
25237
25238   /* First insn will adjust the stack pointer.  */
25239   if (GET_CODE (e) != SET
25240       || !REG_P (XEXP (e, 0))
25241       || REGNO (XEXP (e, 0)) != SP_REGNUM
25242       || GET_CODE (XEXP (e, 1)) != PLUS)
25243     abort ();
25244
25245   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
25246   nregs = XVECLEN (p, 0) - 1;
25247
25248   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
25249   if (reg < 16)
25250     {
25251       /* The function prologue may also push pc, but not annotate it as it is
25252          never restored.  We turn this into a stack pointer adjustment.  */
25253       if (nregs * 4 == offset - 4)
25254         {
25255           fprintf (asm_out_file, "\t.pad #4\n");
25256           offset -= 4;
25257         }
25258       reg_size = 4;
25259       fprintf (asm_out_file, "\t.save {");
25260     }
25261   else if (IS_VFP_REGNUM (reg))
25262     {
25263       reg_size = 8;
25264       fprintf (asm_out_file, "\t.vsave {");
25265     }
25266   else
25267     /* Unknown register type.  */
25268     abort ();
25269
25270   /* If the stack increment doesn't match the size of the saved registers,
25271      something has gone horribly wrong.  */
25272   if (offset != nregs * reg_size)
25273     abort ();
25274
25275   offset = 0;
25276   lastreg = 0;
25277   /* The remaining insns will describe the stores.  */
25278   for (i = 1; i <= nregs; i++)
25279     {
25280       /* Expect (set (mem <addr>) (reg)).
25281          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
25282       e = XVECEXP (p, 0, i);
25283       if (GET_CODE (e) != SET
25284           || !MEM_P (XEXP (e, 0))
25285           || !REG_P (XEXP (e, 1)))
25286         abort ();
25287
25288       reg = REGNO (XEXP (e, 1));
25289       if (reg < lastreg)
25290         abort ();
25291
25292       if (i != 1)
25293         fprintf (asm_out_file, ", ");
25294       /* We can't use %r for vfp because we need to use the
25295          double precision register names.  */
25296       if (IS_VFP_REGNUM (reg))
25297         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
25298       else
25299         asm_fprintf (asm_out_file, "%r", reg);
25300
25301 #ifdef ENABLE_CHECKING
25302       /* Check that the addresses are consecutive.  */
25303       e = XEXP (XEXP (e, 0), 0);
25304       if (GET_CODE (e) == PLUS)
25305         {
25306           offset += reg_size;
25307           if (!REG_P (XEXP (e, 0))
25308               || REGNO (XEXP (e, 0)) != SP_REGNUM
25309               || !CONST_INT_P (XEXP (e, 1))
25310               || offset != INTVAL (XEXP (e, 1)))
25311             abort ();
25312         }
25313       else if (i != 1
25314                || !REG_P (e)
25315                || REGNO (e) != SP_REGNUM)
25316         abort ();
25317 #endif
25318     }
25319   fprintf (asm_out_file, "}\n");
25320 }
25321
25322 /*  Emit unwind directives for a SET.  */
25323
25324 static void
25325 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
25326 {
25327   rtx e0;
25328   rtx e1;
25329   unsigned reg;
25330
25331   e0 = XEXP (p, 0);
25332   e1 = XEXP (p, 1);
25333   switch (GET_CODE (e0))
25334     {
25335     case MEM:
25336       /* Pushing a single register.  */
25337       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
25338           || !REG_P (XEXP (XEXP (e0, 0), 0))
25339           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
25340         abort ();
25341
25342       asm_fprintf (asm_out_file, "\t.save ");
25343       if (IS_VFP_REGNUM (REGNO (e1)))
25344         asm_fprintf(asm_out_file, "{d%d}\n",
25345                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
25346       else
25347         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
25348       break;
25349
25350     case REG:
25351       if (REGNO (e0) == SP_REGNUM)
25352         {
25353           /* A stack increment.  */
25354           if (GET_CODE (e1) != PLUS
25355               || !REG_P (XEXP (e1, 0))
25356               || REGNO (XEXP (e1, 0)) != SP_REGNUM
25357               || !CONST_INT_P (XEXP (e1, 1)))
25358             abort ();
25359
25360           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
25361                        -INTVAL (XEXP (e1, 1)));
25362         }
25363       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
25364         {
25365           HOST_WIDE_INT offset;
25366
25367           if (GET_CODE (e1) == PLUS)
25368             {
25369               if (!REG_P (XEXP (e1, 0))
25370                   || !CONST_INT_P (XEXP (e1, 1)))
25371                 abort ();
25372               reg = REGNO (XEXP (e1, 0));
25373               offset = INTVAL (XEXP (e1, 1));
25374               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
25375                            HARD_FRAME_POINTER_REGNUM, reg,
25376                            offset);
25377             }
25378           else if (REG_P (e1))
25379             {
25380               reg = REGNO (e1);
25381               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
25382                            HARD_FRAME_POINTER_REGNUM, reg);
25383             }
25384           else
25385             abort ();
25386         }
25387       else if (REG_P (e1) && REGNO (e1) == SP_REGNUM)
25388         {
25389           /* Move from sp to reg.  */
25390           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
25391         }
25392      else if (GET_CODE (e1) == PLUS
25393               && REG_P (XEXP (e1, 0))
25394               && REGNO (XEXP (e1, 0)) == SP_REGNUM
25395               && CONST_INT_P (XEXP (e1, 1)))
25396         {
25397           /* Set reg to offset from sp.  */
25398           asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
25399                        REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
25400         }
25401       else
25402         abort ();
25403       break;
25404
25405     default:
25406       abort ();
25407     }
25408 }
25409
25410
25411 /* Emit unwind directives for the given insn.  */
25412
25413 static void
25414 arm_unwind_emit (FILE * asm_out_file, rtx insn)
25415 {
25416   rtx note, pat;
25417   bool handled_one = false;
25418
25419   if (arm_except_unwind_info (&global_options) != UI_TARGET)
25420     return;
25421
25422   if (!(flag_unwind_tables || crtl->uses_eh_lsda)
25423       && (TREE_NOTHROW (current_function_decl)
25424           || crtl->all_throwers_are_sibcalls))
25425     return;
25426
25427   if (NOTE_P (insn) || !RTX_FRAME_RELATED_P (insn))
25428     return;
25429
25430   for (note = REG_NOTES (insn); note ; note = XEXP (note, 1))
25431     {
25432       pat = XEXP (note, 0);
25433       switch (REG_NOTE_KIND (note))
25434         {
25435         case REG_FRAME_RELATED_EXPR:
25436           goto found;
25437
25438         case REG_CFA_REGISTER:
25439           if (pat == NULL)
25440             {
25441               pat = PATTERN (insn);
25442               if (GET_CODE (pat) == PARALLEL)
25443                 pat = XVECEXP (pat, 0, 0);
25444             }
25445
25446           /* Only emitted for IS_STACKALIGN re-alignment.  */
25447           {
25448             rtx dest, src;
25449             unsigned reg;
25450
25451             src = SET_SRC (pat);
25452             dest = SET_DEST (pat);
25453
25454             gcc_assert (src == stack_pointer_rtx);
25455             reg = REGNO (dest);
25456             asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
25457                          reg + 0x90, reg);
25458           }
25459           handled_one = true;
25460           break;
25461
25462         case REG_CFA_DEF_CFA:
25463         case REG_CFA_EXPRESSION:
25464         case REG_CFA_ADJUST_CFA:
25465         case REG_CFA_OFFSET:
25466           /* ??? Only handling here what we actually emit.  */
25467           gcc_unreachable ();
25468
25469         default:
25470           break;
25471         }
25472     }
25473   if (handled_one)
25474     return;
25475   pat = PATTERN (insn);
25476  found:
25477
25478   switch (GET_CODE (pat))
25479     {
25480     case SET:
25481       arm_unwind_emit_set (asm_out_file, pat);
25482       break;
25483
25484     case SEQUENCE:
25485       /* Store multiple.  */
25486       arm_unwind_emit_sequence (asm_out_file, pat);
25487       break;
25488
25489     default:
25490       abort();
25491     }
25492 }
25493
25494
25495 /* Output a reference from a function exception table to the type_info
25496    object X.  The EABI specifies that the symbol should be relocated by
25497    an R_ARM_TARGET2 relocation.  */
25498
25499 static bool
25500 arm_output_ttype (rtx x)
25501 {
25502   fputs ("\t.word\t", asm_out_file);
25503   output_addr_const (asm_out_file, x);
25504   /* Use special relocations for symbol references.  */
25505   if (!CONST_INT_P (x))
25506     fputs ("(TARGET2)", asm_out_file);
25507   fputc ('\n', asm_out_file);
25508
25509   return TRUE;
25510 }
25511
25512 /* Implement TARGET_ASM_EMIT_EXCEPT_PERSONALITY.  */
25513
25514 static void
25515 arm_asm_emit_except_personality (rtx personality)
25516 {
25517   fputs ("\t.personality\t", asm_out_file);
25518   output_addr_const (asm_out_file, personality);
25519   fputc ('\n', asm_out_file);
25520 }
25521
25522 /* Implement TARGET_ASM_INITIALIZE_SECTIONS.  */
25523
25524 static void
25525 arm_asm_init_sections (void)
25526 {
25527   exception_section = get_unnamed_section (0, output_section_asm_op,
25528                                            "\t.handlerdata");
25529 }
25530 #endif /* ARM_UNWIND_INFO */
25531
25532 /* Output unwind directives for the start/end of a function.  */
25533
25534 void
25535 arm_output_fn_unwind (FILE * f, bool prologue)
25536 {
25537   if (arm_except_unwind_info (&global_options) != UI_TARGET)
25538     return;
25539
25540   if (prologue)
25541     fputs ("\t.fnstart\n", f);
25542   else
25543     {
25544       /* If this function will never be unwound, then mark it as such.
25545          The came condition is used in arm_unwind_emit to suppress
25546          the frame annotations.  */
25547       if (!(flag_unwind_tables || crtl->uses_eh_lsda)
25548           && (TREE_NOTHROW (current_function_decl)
25549               || crtl->all_throwers_are_sibcalls))
25550         fputs("\t.cantunwind\n", f);
25551
25552       fputs ("\t.fnend\n", f);
25553     }
25554 }
25555
25556 static bool
25557 arm_emit_tls_decoration (FILE *fp, rtx x)
25558 {
25559   enum tls_reloc reloc;
25560   rtx val;
25561
25562   val = XVECEXP (x, 0, 0);
25563   reloc = (enum tls_reloc) INTVAL (XVECEXP (x, 0, 1));
25564
25565   output_addr_const (fp, val);
25566
25567   switch (reloc)
25568     {
25569     case TLS_GD32:
25570       fputs ("(tlsgd)", fp);
25571       break;
25572     case TLS_LDM32:
25573       fputs ("(tlsldm)", fp);
25574       break;
25575     case TLS_LDO32:
25576       fputs ("(tlsldo)", fp);
25577       break;
25578     case TLS_IE32:
25579       fputs ("(gottpoff)", fp);
25580       break;
25581     case TLS_LE32:
25582       fputs ("(tpoff)", fp);
25583       break;
25584     case TLS_DESCSEQ:
25585       fputs ("(tlsdesc)", fp);
25586       break;
25587     default:
25588       gcc_unreachable ();
25589     }
25590
25591   switch (reloc)
25592     {
25593     case TLS_GD32:
25594     case TLS_LDM32:
25595     case TLS_IE32:
25596     case TLS_DESCSEQ:
25597       fputs (" + (. - ", fp);
25598       output_addr_const (fp, XVECEXP (x, 0, 2));
25599       /* For DESCSEQ the 3rd operand encodes thumbness, and is added */
25600       fputs (reloc == TLS_DESCSEQ ? " + " : " - ", fp);
25601       output_addr_const (fp, XVECEXP (x, 0, 3));
25602       fputc (')', fp);
25603       break;
25604     default:
25605       break;
25606     }
25607
25608   return TRUE;
25609 }
25610
25611 /* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
25612
25613 static void
25614 arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
25615 {
25616   gcc_assert (size == 4);
25617   fputs ("\t.word\t", file);
25618   output_addr_const (file, x);
25619   fputs ("(tlsldo)", file);
25620 }
25621
25622 /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA.  */
25623
25624 static bool
25625 arm_output_addr_const_extra (FILE *fp, rtx x)
25626 {
25627   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
25628     return arm_emit_tls_decoration (fp, x);
25629   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
25630     {
25631       char label[256];
25632       int labelno = INTVAL (XVECEXP (x, 0, 0));
25633
25634       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
25635       assemble_name_raw (fp, label);
25636
25637       return TRUE;
25638     }
25639   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOTSYM_OFF)
25640     {
25641       assemble_name (fp, "_GLOBAL_OFFSET_TABLE_");
25642       if (GOT_PCREL)
25643         fputs ("+.", fp);
25644       fputs ("-(", fp);
25645       output_addr_const (fp, XVECEXP (x, 0, 0));
25646       fputc (')', fp);
25647       return TRUE;
25648     }
25649   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_SYMBOL_OFFSET)
25650     {
25651       output_addr_const (fp, XVECEXP (x, 0, 0));
25652       if (GOT_PCREL)
25653         fputs ("+.", fp);
25654       fputs ("-(", fp);
25655       output_addr_const (fp, XVECEXP (x, 0, 1));
25656       fputc (')', fp);
25657       return TRUE;
25658     }
25659   else if (GET_CODE (x) == CONST_VECTOR)
25660     return arm_emit_vector_const (fp, x);
25661
25662   return FALSE;
25663 }
25664
25665 /* Output assembly for a shift instruction.
25666    SET_FLAGS determines how the instruction modifies the condition codes.
25667    0 - Do not set condition codes.
25668    1 - Set condition codes.
25669    2 - Use smallest instruction.  */
25670 const char *
25671 arm_output_shift(rtx * operands, int set_flags)
25672 {
25673   char pattern[100];
25674   static const char flag_chars[3] = {'?', '.', '!'};
25675   const char *shift;
25676   HOST_WIDE_INT val;
25677   char c;
25678
25679   c = flag_chars[set_flags];
25680   if (TARGET_UNIFIED_ASM)
25681     {
25682       shift = shift_op(operands[3], &val);
25683       if (shift)
25684         {
25685           if (val != -1)
25686             operands[2] = GEN_INT(val);
25687           sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
25688         }
25689       else
25690         sprintf (pattern, "mov%%%c\t%%0, %%1", c);
25691     }
25692   else
25693     sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
25694   output_asm_insn (pattern, operands);
25695   return "";
25696 }
25697
25698 /* Output assembly for a WMMX immediate shift instruction.  */
25699 const char *
25700 arm_output_iwmmxt_shift_immediate (const char *insn_name, rtx *operands, bool wror_or_wsra)
25701 {
25702   int shift = INTVAL (operands[2]);
25703   char templ[50];
25704   enum machine_mode opmode = GET_MODE (operands[0]);
25705
25706   gcc_assert (shift >= 0);
25707
25708   /* If the shift value in the register versions is > 63 (for D qualifier),
25709      31 (for W qualifier) or 15 (for H qualifier).  */
25710   if (((opmode == V4HImode) && (shift > 15))
25711         || ((opmode == V2SImode) && (shift > 31))
25712         || ((opmode == DImode) && (shift > 63)))
25713   {
25714     if (wror_or_wsra)
25715       {
25716         sprintf (templ, "%s\t%%0, %%1, #%d", insn_name, 32);
25717         output_asm_insn (templ, operands);
25718         if (opmode == DImode)
25719           {
25720             sprintf (templ, "%s\t%%0, %%0, #%d", insn_name, 32);
25721             output_asm_insn (templ, operands);
25722           }
25723       }
25724     else
25725       {
25726         /* The destination register will contain all zeros.  */
25727         sprintf (templ, "wzero\t%%0");
25728         output_asm_insn (templ, operands);
25729       }
25730     return "";
25731   }
25732
25733   if ((opmode == DImode) && (shift > 32))
25734     {
25735       sprintf (templ, "%s\t%%0, %%1, #%d", insn_name, 32);
25736       output_asm_insn (templ, operands);
25737       sprintf (templ, "%s\t%%0, %%0, #%d", insn_name, shift - 32);
25738       output_asm_insn (templ, operands);
25739     }
25740   else
25741     {
25742       sprintf (templ, "%s\t%%0, %%1, #%d", insn_name, shift);
25743       output_asm_insn (templ, operands);
25744     }
25745   return "";
25746 }
25747
25748 /* Output assembly for a WMMX tinsr instruction.  */
25749 const char *
25750 arm_output_iwmmxt_tinsr (rtx *operands)
25751 {
25752   int mask = INTVAL (operands[3]);
25753   int i;
25754   char templ[50];
25755   int units = mode_nunits[GET_MODE (operands[0])];
25756   gcc_assert ((mask & (mask - 1)) == 0);
25757   for (i = 0; i < units; ++i)
25758     {
25759       if ((mask & 0x01) == 1)
25760         {
25761           break;
25762         }
25763       mask >>= 1;
25764     }
25765   gcc_assert (i < units);
25766   {
25767     switch (GET_MODE (operands[0]))
25768       {
25769       case V8QImode:
25770         sprintf (templ, "tinsrb%%?\t%%0, %%2, #%d", i);
25771         break;
25772       case V4HImode:
25773         sprintf (templ, "tinsrh%%?\t%%0, %%2, #%d", i);
25774         break;
25775       case V2SImode:
25776         sprintf (templ, "tinsrw%%?\t%%0, %%2, #%d", i);
25777         break;
25778       default:
25779         gcc_unreachable ();
25780         break;
25781       }
25782     output_asm_insn (templ, operands);
25783   }
25784   return "";
25785 }
25786
25787 /* Output a Thumb-1 casesi dispatch sequence.  */
25788 const char *
25789 thumb1_output_casesi (rtx *operands)
25790 {
25791   rtx diff_vec = PATTERN (next_real_insn (operands[0]));
25792
25793   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
25794
25795   switch (GET_MODE(diff_vec))
25796     {
25797     case QImode:
25798       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ?
25799               "bl\t%___gnu_thumb1_case_uqi" : "bl\t%___gnu_thumb1_case_sqi");
25800     case HImode:
25801       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ?
25802               "bl\t%___gnu_thumb1_case_uhi" : "bl\t%___gnu_thumb1_case_shi");
25803     case SImode:
25804       return "bl\t%___gnu_thumb1_case_si";
25805     default:
25806       gcc_unreachable ();
25807     }
25808 }
25809
25810 /* Output a Thumb-2 casesi instruction.  */
25811 const char *
25812 thumb2_output_casesi (rtx *operands)
25813 {
25814   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
25815
25816   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
25817
25818   output_asm_insn ("cmp\t%0, %1", operands);
25819   output_asm_insn ("bhi\t%l3", operands);
25820   switch (GET_MODE(diff_vec))
25821     {
25822     case QImode:
25823       return "tbb\t[%|pc, %0]";
25824     case HImode:
25825       return "tbh\t[%|pc, %0, lsl #1]";
25826     case SImode:
25827       if (flag_pic)
25828         {
25829           output_asm_insn ("adr\t%4, %l2", operands);
25830           output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
25831           output_asm_insn ("add\t%4, %4, %5", operands);
25832           return "bx\t%4";
25833         }
25834       else
25835         {
25836           output_asm_insn ("adr\t%4, %l2", operands);
25837           return "ldr\t%|pc, [%4, %0, lsl #2]";
25838         }
25839     default:
25840       gcc_unreachable ();
25841     }
25842 }
25843
25844 /* Most ARM cores are single issue, but some newer ones can dual issue.
25845    The scheduler descriptions rely on this being correct.  */
25846 static int
25847 arm_issue_rate (void)
25848 {
25849   switch (arm_tune)
25850     {
25851     case cortexa15:
25852       return 3;
25853
25854     case cortexr4:
25855     case cortexr4f:
25856     case cortexr5:
25857     case genericv7a:
25858     case cortexa5:
25859     case cortexa7:
25860     case cortexa8:
25861     case cortexa9:
25862     case fa726te:
25863     case marvell_pj4:
25864       return 2;
25865
25866     default:
25867       return 1;
25868     }
25869 }
25870
25871 /* A table and a function to perform ARM-specific name mangling for
25872    NEON vector types in order to conform to the AAPCS (see "Procedure
25873    Call Standard for the ARM Architecture", Appendix A).  To qualify
25874    for emission with the mangled names defined in that document, a
25875    vector type must not only be of the correct mode but also be
25876    composed of NEON vector element types (e.g. __builtin_neon_qi).  */
25877 typedef struct
25878 {
25879   enum machine_mode mode;
25880   const char *element_type_name;
25881   const char *aapcs_name;
25882 } arm_mangle_map_entry;
25883
25884 static arm_mangle_map_entry arm_mangle_map[] = {
25885   /* 64-bit containerized types.  */
25886   { V8QImode,  "__builtin_neon_qi",     "15__simd64_int8_t" },
25887   { V8QImode,  "__builtin_neon_uqi",    "16__simd64_uint8_t" },
25888   { V4HImode,  "__builtin_neon_hi",     "16__simd64_int16_t" },
25889   { V4HImode,  "__builtin_neon_uhi",    "17__simd64_uint16_t" },
25890   { V2SImode,  "__builtin_neon_si",     "16__simd64_int32_t" },
25891   { V2SImode,  "__builtin_neon_usi",    "17__simd64_uint32_t" },
25892   { V2SFmode,  "__builtin_neon_sf",     "18__simd64_float32_t" },
25893   { V8QImode,  "__builtin_neon_poly8",  "16__simd64_poly8_t" },
25894   { V4HImode,  "__builtin_neon_poly16", "17__simd64_poly16_t" },
25895   /* 128-bit containerized types.  */
25896   { V16QImode, "__builtin_neon_qi",     "16__simd128_int8_t" },
25897   { V16QImode, "__builtin_neon_uqi",    "17__simd128_uint8_t" },
25898   { V8HImode,  "__builtin_neon_hi",     "17__simd128_int16_t" },
25899   { V8HImode,  "__builtin_neon_uhi",    "18__simd128_uint16_t" },
25900   { V4SImode,  "__builtin_neon_si",     "17__simd128_int32_t" },
25901   { V4SImode,  "__builtin_neon_usi",    "18__simd128_uint32_t" },
25902   { V4SFmode,  "__builtin_neon_sf",     "19__simd128_float32_t" },
25903   { V16QImode, "__builtin_neon_poly8",  "17__simd128_poly8_t" },
25904   { V8HImode,  "__builtin_neon_poly16", "18__simd128_poly16_t" },
25905   { VOIDmode, NULL, NULL }
25906 };
25907
25908 const char *
25909 arm_mangle_type (const_tree type)
25910 {
25911   arm_mangle_map_entry *pos = arm_mangle_map;
25912
25913   /* The ARM ABI documents (10th October 2008) say that "__va_list"
25914      has to be managled as if it is in the "std" namespace.  */
25915   if (TARGET_AAPCS_BASED
25916       && lang_hooks.types_compatible_p (CONST_CAST_TREE (type), va_list_type))
25917     return "St9__va_list";
25918
25919   /* Half-precision float.  */
25920   if (TREE_CODE (type) == REAL_TYPE && TYPE_PRECISION (type) == 16)
25921     return "Dh";
25922
25923   if (TREE_CODE (type) != VECTOR_TYPE)
25924     return NULL;
25925
25926   /* Check the mode of the vector type, and the name of the vector
25927      element type, against the table.  */
25928   while (pos->mode != VOIDmode)
25929     {
25930       tree elt_type = TREE_TYPE (type);
25931
25932       if (pos->mode == TYPE_MODE (type)
25933           && TREE_CODE (TYPE_NAME (elt_type)) == TYPE_DECL
25934           && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (elt_type))),
25935                       pos->element_type_name))
25936         return pos->aapcs_name;
25937
25938       pos++;
25939     }
25940
25941   /* Use the default mangling for unrecognized (possibly user-defined)
25942      vector types.  */
25943   return NULL;
25944 }
25945
25946 /* Order of allocation of core registers for Thumb: this allocation is
25947    written over the corresponding initial entries of the array
25948    initialized with REG_ALLOC_ORDER.  We allocate all low registers
25949    first.  Saving and restoring a low register is usually cheaper than
25950    using a call-clobbered high register.  */
25951
25952 static const int thumb_core_reg_alloc_order[] =
25953 {
25954    3,  2,  1,  0,  4,  5,  6,  7,
25955   14, 12,  8,  9, 10, 11
25956 };
25957
25958 /* Adjust register allocation order when compiling for Thumb.  */
25959
25960 void
25961 arm_order_regs_for_local_alloc (void)
25962 {
25963   const int arm_reg_alloc_order[] = REG_ALLOC_ORDER;
25964   memcpy(reg_alloc_order, arm_reg_alloc_order, sizeof (reg_alloc_order));
25965   if (TARGET_THUMB)
25966     memcpy (reg_alloc_order, thumb_core_reg_alloc_order,
25967             sizeof (thumb_core_reg_alloc_order));
25968 }
25969
25970 /* Implement TARGET_FRAME_POINTER_REQUIRED.  */
25971
25972 bool
25973 arm_frame_pointer_required (void)
25974 {
25975   return (cfun->has_nonlocal_label
25976           || SUBTARGET_FRAME_POINTER_REQUIRED
25977           || (TARGET_ARM && TARGET_APCS_FRAME && ! leaf_function_p ()));
25978 }
25979
25980 /* Only thumb1 can't support conditional execution, so return true if
25981    the target is not thumb1.  */
25982 static bool
25983 arm_have_conditional_execution (void)
25984 {
25985   return !TARGET_THUMB1;
25986 }
25987
25988 /* The AAPCS sets the maximum alignment of a vector to 64 bits.  */
25989 static HOST_WIDE_INT
25990 arm_vector_alignment (const_tree type)
25991 {
25992   HOST_WIDE_INT align = tree_low_cst (TYPE_SIZE (type), 0);
25993
25994   if (TARGET_AAPCS_BASED)
25995     align = MIN (align, 64);
25996
25997   return align;
25998 }
25999
26000 static unsigned int
26001 arm_autovectorize_vector_sizes (void)
26002 {
26003   return TARGET_NEON_VECTORIZE_DOUBLE ? 0 : (16 | 8);
26004 }
26005
26006 static bool
26007 arm_vector_alignment_reachable (const_tree type, bool is_packed)
26008 {
26009   /* Vectors which aren't in packed structures will not be less aligned than
26010      the natural alignment of their element type, so this is safe.  */
26011   if (TARGET_NEON && !BYTES_BIG_ENDIAN)
26012     return !is_packed;
26013
26014   return default_builtin_vector_alignment_reachable (type, is_packed);
26015 }
26016
26017 static bool
26018 arm_builtin_support_vector_misalignment (enum machine_mode mode,
26019                                          const_tree type, int misalignment,
26020                                          bool is_packed)
26021 {
26022   if (TARGET_NEON && !BYTES_BIG_ENDIAN)
26023     {
26024       HOST_WIDE_INT align = TYPE_ALIGN_UNIT (type);
26025
26026       if (is_packed)
26027         return align == 1;
26028
26029       /* If the misalignment is unknown, we should be able to handle the access
26030          so long as it is not to a member of a packed data structure.  */
26031       if (misalignment == -1)
26032         return true;
26033
26034       /* Return true if the misalignment is a multiple of the natural alignment
26035          of the vector's element type.  This is probably always going to be
26036          true in practice, since we've already established that this isn't a
26037          packed access.  */
26038       return ((misalignment % align) == 0);
26039     }
26040
26041   return default_builtin_support_vector_misalignment (mode, type, misalignment,
26042                                                       is_packed);
26043 }
26044
26045 static void
26046 arm_conditional_register_usage (void)
26047 {
26048   int regno;
26049
26050   if (TARGET_THUMB1 && optimize_size)
26051     {
26052       /* When optimizing for size on Thumb-1, it's better not
26053         to use the HI regs, because of the overhead of
26054         stacking them.  */
26055       for (regno = FIRST_HI_REGNUM;
26056            regno <= LAST_HI_REGNUM; ++regno)
26057         fixed_regs[regno] = call_used_regs[regno] = 1;
26058     }
26059
26060   /* The link register can be clobbered by any branch insn,
26061      but we have no way to track that at present, so mark
26062      it as unavailable.  */
26063   if (TARGET_THUMB1)
26064     fixed_regs[LR_REGNUM] = call_used_regs[LR_REGNUM] = 1;
26065
26066   if (TARGET_32BIT && TARGET_HARD_FLOAT && TARGET_VFP)
26067     {
26068       /* VFPv3 registers are disabled when earlier VFP
26069          versions are selected due to the definition of
26070          LAST_VFP_REGNUM.  */
26071       for (regno = FIRST_VFP_REGNUM;
26072            regno <= LAST_VFP_REGNUM; ++ regno)
26073         {
26074           fixed_regs[regno] = 0;
26075           call_used_regs[regno] = regno < FIRST_VFP_REGNUM + 16
26076             || regno >= FIRST_VFP_REGNUM + 32;
26077         }
26078     }
26079
26080   if (TARGET_REALLY_IWMMXT)
26081     {
26082       regno = FIRST_IWMMXT_GR_REGNUM;
26083       /* The 2002/10/09 revision of the XScale ABI has wCG0
26084          and wCG1 as call-preserved registers.  The 2002/11/21
26085          revision changed this so that all wCG registers are
26086          scratch registers.  */
26087       for (regno = FIRST_IWMMXT_GR_REGNUM;
26088            regno <= LAST_IWMMXT_GR_REGNUM; ++ regno)
26089         fixed_regs[regno] = 0;
26090       /* The XScale ABI has wR0 - wR9 as scratch registers,
26091          the rest as call-preserved registers.  */
26092       for (regno = FIRST_IWMMXT_REGNUM;
26093            regno <= LAST_IWMMXT_REGNUM; ++ regno)
26094         {
26095           fixed_regs[regno] = 0;
26096           call_used_regs[regno] = regno < FIRST_IWMMXT_REGNUM + 10;
26097         }
26098     }
26099
26100   if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
26101     {
26102       fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
26103       call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
26104     }
26105   else if (TARGET_APCS_STACK)
26106     {
26107       fixed_regs[10]     = 1;
26108       call_used_regs[10] = 1;
26109     }
26110   /* -mcaller-super-interworking reserves r11 for calls to
26111      _interwork_r11_call_via_rN().  Making the register global
26112      is an easy way of ensuring that it remains valid for all
26113      calls.  */
26114   if (TARGET_APCS_FRAME || TARGET_CALLER_INTERWORKING
26115       || TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME)
26116     {
26117       fixed_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
26118       call_used_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
26119       if (TARGET_CALLER_INTERWORKING)
26120         global_regs[ARM_HARD_FRAME_POINTER_REGNUM] = 1;
26121     }
26122   SUBTARGET_CONDITIONAL_REGISTER_USAGE
26123 }
26124
26125 static reg_class_t
26126 arm_preferred_rename_class (reg_class_t rclass)
26127 {
26128   /* Thumb-2 instructions using LO_REGS may be smaller than instructions
26129      using GENERIC_REGS.  During register rename pass, we prefer LO_REGS,
26130      and code size can be reduced.  */
26131   if (TARGET_THUMB2 && rclass == GENERAL_REGS)
26132     return LO_REGS;
26133   else
26134     return NO_REGS;
26135 }
26136
26137 /* Compute the atrribute "length" of insn "*push_multi".
26138    So this function MUST be kept in sync with that insn pattern.  */
26139 int
26140 arm_attr_length_push_multi(rtx parallel_op, rtx first_op)
26141 {
26142   int i, regno, hi_reg;
26143   int num_saves = XVECLEN (parallel_op, 0);
26144
26145   /* ARM mode.  */
26146   if (TARGET_ARM)
26147     return 4;
26148   /* Thumb1 mode.  */
26149   if (TARGET_THUMB1)
26150     return 2;
26151
26152   /* Thumb2 mode.  */
26153   regno = REGNO (first_op);
26154   hi_reg = (REGNO_REG_CLASS (regno) == HI_REGS) && (regno != LR_REGNUM);
26155   for (i = 1; i < num_saves && !hi_reg; i++)
26156     {
26157       regno = REGNO (XEXP (XVECEXP (parallel_op, 0, i), 0));
26158       hi_reg |= (REGNO_REG_CLASS (regno) == HI_REGS) && (regno != LR_REGNUM);
26159     }
26160
26161   if (!hi_reg)
26162     return 2;
26163   return 4;
26164 }
26165
26166 /* Compute the number of instructions emitted by output_move_double.  */
26167 int
26168 arm_count_output_move_double_insns (rtx *operands)
26169 {
26170   int count;
26171   rtx ops[2];
26172   /* output_move_double may modify the operands array, so call it
26173      here on a copy of the array.  */
26174   ops[0] = operands[0];
26175   ops[1] = operands[1];
26176   output_move_double (ops, false, &count);
26177   return count;
26178 }
26179
26180 int
26181 vfp3_const_double_for_fract_bits (rtx operand)
26182 {
26183   REAL_VALUE_TYPE r0;
26184   
26185   if (!CONST_DOUBLE_P (operand))
26186     return 0;
26187   
26188   REAL_VALUE_FROM_CONST_DOUBLE (r0, operand);
26189   if (exact_real_inverse (DFmode, &r0))
26190     {
26191       if (exact_real_truncate (DFmode, &r0))
26192         {
26193           HOST_WIDE_INT value = real_to_integer (&r0);
26194           value = value & 0xffffffff;
26195           if ((value != 0) && ( (value & (value - 1)) == 0))
26196             return int_log2 (value);
26197         }
26198     }
26199   return 0;
26200 }
26201 \f
26202 /* Emit a memory barrier around an atomic sequence according to MODEL.  */
26203
26204 static void
26205 arm_pre_atomic_barrier (enum memmodel model)
26206 {
26207   if (need_atomic_barrier_p (model, true))
26208     emit_insn (gen_memory_barrier ());
26209 }
26210
26211 static void
26212 arm_post_atomic_barrier (enum memmodel model)
26213 {
26214   if (need_atomic_barrier_p (model, false))
26215     emit_insn (gen_memory_barrier ());
26216 }
26217
26218 /* Emit the load-exclusive and store-exclusive instructions.  */
26219
26220 static void
26221 arm_emit_load_exclusive (enum machine_mode mode, rtx rval, rtx mem)
26222 {
26223   rtx (*gen) (rtx, rtx);
26224
26225   switch (mode)
26226     {
26227     case QImode: gen = gen_arm_load_exclusiveqi; break;
26228     case HImode: gen = gen_arm_load_exclusivehi; break;
26229     case SImode: gen = gen_arm_load_exclusivesi; break;
26230     case DImode: gen = gen_arm_load_exclusivedi; break;
26231     default:
26232       gcc_unreachable ();
26233     }
26234
26235   emit_insn (gen (rval, mem));
26236 }
26237
26238 static void
26239 arm_emit_store_exclusive (enum machine_mode mode, rtx bval, rtx rval, rtx mem)
26240 {
26241   rtx (*gen) (rtx, rtx, rtx);
26242
26243   switch (mode)
26244     {
26245     case QImode: gen = gen_arm_store_exclusiveqi; break;
26246     case HImode: gen = gen_arm_store_exclusivehi; break;
26247     case SImode: gen = gen_arm_store_exclusivesi; break;
26248     case DImode: gen = gen_arm_store_exclusivedi; break;
26249     default:
26250       gcc_unreachable ();
26251     }
26252
26253   emit_insn (gen (bval, rval, mem));
26254 }
26255
26256 /* Mark the previous jump instruction as unlikely.  */
26257
26258 static void
26259 emit_unlikely_jump (rtx insn)
26260 {
26261   rtx very_unlikely = GEN_INT (REG_BR_PROB_BASE / 100 - 1);
26262
26263   insn = emit_jump_insn (insn);
26264   add_reg_note (insn, REG_BR_PROB, very_unlikely);
26265 }
26266
26267 /* Expand a compare and swap pattern.  */
26268
26269 void
26270 arm_expand_compare_and_swap (rtx operands[])
26271 {
26272   rtx bval, rval, mem, oldval, newval, is_weak, mod_s, mod_f, x;
26273   enum machine_mode mode;
26274   rtx (*gen) (rtx, rtx, rtx, rtx, rtx, rtx, rtx);
26275
26276   bval = operands[0];
26277   rval = operands[1];
26278   mem = operands[2];
26279   oldval = operands[3];
26280   newval = operands[4];
26281   is_weak = operands[5];
26282   mod_s = operands[6];
26283   mod_f = operands[7];
26284   mode = GET_MODE (mem);
26285
26286   switch (mode)
26287     {
26288     case QImode:
26289     case HImode:
26290       /* For narrow modes, we're going to perform the comparison in SImode,
26291          so do the zero-extension now.  */
26292       rval = gen_reg_rtx (SImode);
26293       oldval = convert_modes (SImode, mode, oldval, true);
26294       /* FALLTHRU */
26295
26296     case SImode:
26297       /* Force the value into a register if needed.  We waited until after
26298          the zero-extension above to do this properly.  */
26299       if (!arm_add_operand (oldval, SImode))
26300         oldval = force_reg (SImode, oldval);
26301       break;
26302
26303     case DImode:
26304       if (!cmpdi_operand (oldval, mode))
26305         oldval = force_reg (mode, oldval);
26306       break;
26307
26308     default:
26309       gcc_unreachable ();
26310     }
26311
26312   switch (mode)
26313     {
26314     case QImode: gen = gen_atomic_compare_and_swapqi_1; break;
26315     case HImode: gen = gen_atomic_compare_and_swaphi_1; break;
26316     case SImode: gen = gen_atomic_compare_and_swapsi_1; break;
26317     case DImode: gen = gen_atomic_compare_and_swapdi_1; break;
26318     default:
26319       gcc_unreachable ();
26320     }
26321
26322   emit_insn (gen (rval, mem, oldval, newval, is_weak, mod_s, mod_f));
26323
26324   if (mode == QImode || mode == HImode)
26325     emit_move_insn (operands[1], gen_lowpart (mode, rval));
26326
26327   /* In all cases, we arrange for success to be signaled by Z set.
26328      This arrangement allows for the boolean result to be used directly
26329      in a subsequent branch, post optimization.  */
26330   x = gen_rtx_REG (CCmode, CC_REGNUM);
26331   x = gen_rtx_EQ (SImode, x, const0_rtx);
26332   emit_insn (gen_rtx_SET (VOIDmode, bval, x));
26333 }
26334
26335 /* Split a compare and swap pattern.  It is IMPLEMENTATION DEFINED whether
26336    another memory store between the load-exclusive and store-exclusive can
26337    reset the monitor from Exclusive to Open state.  This means we must wait
26338    until after reload to split the pattern, lest we get a register spill in
26339    the middle of the atomic sequence.  */
26340
26341 void
26342 arm_split_compare_and_swap (rtx operands[])
26343 {
26344   rtx rval, mem, oldval, newval, scratch;
26345   enum machine_mode mode;
26346   enum memmodel mod_s, mod_f;
26347   bool is_weak;
26348   rtx label1, label2, x, cond;
26349
26350   rval = operands[0];
26351   mem = operands[1];
26352   oldval = operands[2];
26353   newval = operands[3];
26354   is_weak = (operands[4] != const0_rtx);
26355   mod_s = (enum memmodel) INTVAL (operands[5]);
26356   mod_f = (enum memmodel) INTVAL (operands[6]);
26357   scratch = operands[7];
26358   mode = GET_MODE (mem);
26359
26360   arm_pre_atomic_barrier (mod_s);
26361
26362   label1 = NULL_RTX;
26363   if (!is_weak)
26364     {
26365       label1 = gen_label_rtx ();
26366       emit_label (label1);
26367     }
26368   label2 = gen_label_rtx ();
26369
26370   arm_emit_load_exclusive (mode, rval, mem);
26371
26372   cond = arm_gen_compare_reg (NE, rval, oldval, scratch);
26373   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
26374   x = gen_rtx_IF_THEN_ELSE (VOIDmode, x,
26375                             gen_rtx_LABEL_REF (Pmode, label2), pc_rtx);
26376   emit_unlikely_jump (gen_rtx_SET (VOIDmode, pc_rtx, x));
26377
26378   arm_emit_store_exclusive (mode, scratch, mem, newval);
26379
26380   /* Weak or strong, we want EQ to be true for success, so that we
26381      match the flags that we got from the compare above.  */
26382   cond = gen_rtx_REG (CCmode, CC_REGNUM);
26383   x = gen_rtx_COMPARE (CCmode, scratch, const0_rtx);
26384   emit_insn (gen_rtx_SET (VOIDmode, cond, x));
26385
26386   if (!is_weak)
26387     {
26388       x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
26389       x = gen_rtx_IF_THEN_ELSE (VOIDmode, x,
26390                                 gen_rtx_LABEL_REF (Pmode, label1), pc_rtx);
26391       emit_unlikely_jump (gen_rtx_SET (VOIDmode, pc_rtx, x));
26392     }
26393
26394   if (mod_f != MEMMODEL_RELAXED)
26395     emit_label (label2);
26396
26397   arm_post_atomic_barrier (mod_s);
26398
26399   if (mod_f == MEMMODEL_RELAXED)
26400     emit_label (label2);
26401 }
26402
26403 void
26404 arm_split_atomic_op (enum rtx_code code, rtx old_out, rtx new_out, rtx mem,
26405                      rtx value, rtx model_rtx, rtx cond)
26406 {
26407   enum memmodel model = (enum memmodel) INTVAL (model_rtx);
26408   enum machine_mode mode = GET_MODE (mem);
26409   enum machine_mode wmode = (mode == DImode ? DImode : SImode);
26410   rtx label, x;
26411
26412   arm_pre_atomic_barrier (model);
26413
26414   label = gen_label_rtx ();
26415   emit_label (label);
26416
26417   if (new_out)
26418     new_out = gen_lowpart (wmode, new_out);
26419   if (old_out)
26420     old_out = gen_lowpart (wmode, old_out);
26421   else
26422     old_out = new_out;
26423   value = simplify_gen_subreg (wmode, value, mode, 0);
26424
26425   arm_emit_load_exclusive (mode, old_out, mem);
26426
26427   switch (code)
26428     {
26429     case SET:
26430       new_out = value;
26431       break;
26432
26433     case NOT:
26434       x = gen_rtx_AND (wmode, old_out, value);
26435       emit_insn (gen_rtx_SET (VOIDmode, new_out, x));
26436       x = gen_rtx_NOT (wmode, new_out);
26437       emit_insn (gen_rtx_SET (VOIDmode, new_out, x));
26438       break;
26439
26440     case MINUS:
26441       if (CONST_INT_P (value))
26442         {
26443           value = GEN_INT (-INTVAL (value));
26444           code = PLUS;
26445         }
26446       /* FALLTHRU */
26447
26448     case PLUS:
26449       if (mode == DImode)
26450         {
26451           /* DImode plus/minus need to clobber flags.  */
26452           /* The adddi3 and subdi3 patterns are incorrectly written so that
26453              they require matching operands, even when we could easily support
26454              three operands.  Thankfully, this can be fixed up post-splitting,
26455              as the individual add+adc patterns do accept three operands and
26456              post-reload cprop can make these moves go away.  */
26457           emit_move_insn (new_out, old_out);
26458           if (code == PLUS)
26459             x = gen_adddi3 (new_out, new_out, value);
26460           else
26461             x = gen_subdi3 (new_out, new_out, value);
26462           emit_insn (x);
26463           break;
26464         }
26465       /* FALLTHRU */
26466
26467     default:
26468       x = gen_rtx_fmt_ee (code, wmode, old_out, value);
26469       emit_insn (gen_rtx_SET (VOIDmode, new_out, x));
26470       break;
26471     }
26472
26473   arm_emit_store_exclusive (mode, cond, mem, gen_lowpart (mode, new_out));
26474
26475   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
26476   emit_unlikely_jump (gen_cbranchsi4 (x, cond, const0_rtx, label));
26477
26478   arm_post_atomic_barrier (model);
26479 }
26480 \f
26481 #define MAX_VECT_LEN 16
26482
26483 struct expand_vec_perm_d
26484 {
26485   rtx target, op0, op1;
26486   unsigned char perm[MAX_VECT_LEN];
26487   enum machine_mode vmode;
26488   unsigned char nelt;
26489   bool one_vector_p;
26490   bool testing_p;
26491 };
26492
26493 /* Generate a variable permutation.  */
26494
26495 static void
26496 arm_expand_vec_perm_1 (rtx target, rtx op0, rtx op1, rtx sel)
26497 {
26498   enum machine_mode vmode = GET_MODE (target);
26499   bool one_vector_p = rtx_equal_p (op0, op1);
26500
26501   gcc_checking_assert (vmode == V8QImode || vmode == V16QImode);
26502   gcc_checking_assert (GET_MODE (op0) == vmode);
26503   gcc_checking_assert (GET_MODE (op1) == vmode);
26504   gcc_checking_assert (GET_MODE (sel) == vmode);
26505   gcc_checking_assert (TARGET_NEON);
26506
26507   if (one_vector_p)
26508     {
26509       if (vmode == V8QImode)
26510         emit_insn (gen_neon_vtbl1v8qi (target, op0, sel));
26511       else
26512         emit_insn (gen_neon_vtbl1v16qi (target, op0, sel));
26513     }
26514   else
26515     {
26516       rtx pair;
26517
26518       if (vmode == V8QImode)
26519         {
26520           pair = gen_reg_rtx (V16QImode);
26521           emit_insn (gen_neon_vcombinev8qi (pair, op0, op1));
26522           pair = gen_lowpart (TImode, pair);
26523           emit_insn (gen_neon_vtbl2v8qi (target, pair, sel));
26524         }
26525       else
26526         {
26527           pair = gen_reg_rtx (OImode);
26528           emit_insn (gen_neon_vcombinev16qi (pair, op0, op1));
26529           emit_insn (gen_neon_vtbl2v16qi (target, pair, sel));
26530         }
26531     }
26532 }
26533
26534 void
26535 arm_expand_vec_perm (rtx target, rtx op0, rtx op1, rtx sel)
26536 {
26537   enum machine_mode vmode = GET_MODE (target);
26538   unsigned int i, nelt = GET_MODE_NUNITS (vmode);
26539   bool one_vector_p = rtx_equal_p (op0, op1);
26540   rtx rmask[MAX_VECT_LEN], mask;
26541
26542   /* TODO: ARM's VTBL indexing is little-endian.  In order to handle GCC's
26543      numbering of elements for big-endian, we must reverse the order.  */
26544   gcc_checking_assert (!BYTES_BIG_ENDIAN);
26545
26546   /* The VTBL instruction does not use a modulo index, so we must take care
26547      of that ourselves.  */
26548   mask = GEN_INT (one_vector_p ? nelt - 1 : 2 * nelt - 1);
26549   for (i = 0; i < nelt; ++i)
26550     rmask[i] = mask;
26551   mask = gen_rtx_CONST_VECTOR (vmode, gen_rtvec_v (nelt, rmask));
26552   sel = expand_simple_binop (vmode, AND, sel, mask, NULL, 0, OPTAB_LIB_WIDEN);
26553
26554   arm_expand_vec_perm_1 (target, op0, op1, sel);
26555 }
26556
26557 /* Generate or test for an insn that supports a constant permutation.  */
26558
26559 /* Recognize patterns for the VUZP insns.  */
26560
26561 static bool
26562 arm_evpc_neon_vuzp (struct expand_vec_perm_d *d)
26563 {
26564   unsigned int i, odd, mask, nelt = d->nelt;
26565   rtx out0, out1, in0, in1, x;
26566   rtx (*gen)(rtx, rtx, rtx, rtx);
26567
26568   if (GET_MODE_UNIT_SIZE (d->vmode) >= 8)
26569     return false;
26570
26571   /* Note that these are little-endian tests.  Adjust for big-endian later.  */
26572   if (d->perm[0] == 0)
26573     odd = 0;
26574   else if (d->perm[0] == 1)
26575     odd = 1;
26576   else
26577     return false;
26578   mask = (d->one_vector_p ? nelt - 1 : 2 * nelt - 1);
26579
26580   for (i = 0; i < nelt; i++)
26581     {
26582       unsigned elt = (i * 2 + odd) & mask;
26583       if (d->perm[i] != elt)
26584         return false;
26585     }
26586
26587   /* Success!  */
26588   if (d->testing_p)
26589     return true;
26590
26591   switch (d->vmode)
26592     {
26593     case V16QImode: gen = gen_neon_vuzpv16qi_internal; break;
26594     case V8QImode:  gen = gen_neon_vuzpv8qi_internal;  break;
26595     case V8HImode:  gen = gen_neon_vuzpv8hi_internal;  break;
26596     case V4HImode:  gen = gen_neon_vuzpv4hi_internal;  break;
26597     case V4SImode:  gen = gen_neon_vuzpv4si_internal;  break;
26598     case V2SImode:  gen = gen_neon_vuzpv2si_internal;  break;
26599     case V2SFmode:  gen = gen_neon_vuzpv2sf_internal;  break;
26600     case V4SFmode:  gen = gen_neon_vuzpv4sf_internal;  break;
26601     default:
26602       gcc_unreachable ();
26603     }
26604
26605   in0 = d->op0;
26606   in1 = d->op1;
26607   if (BYTES_BIG_ENDIAN)
26608     {
26609       x = in0, in0 = in1, in1 = x;
26610       odd = !odd;
26611     }
26612
26613   out0 = d->target;
26614   out1 = gen_reg_rtx (d->vmode);
26615   if (odd)
26616     x = out0, out0 = out1, out1 = x;
26617
26618   emit_insn (gen (out0, in0, in1, out1));
26619   return true;
26620 }
26621
26622 /* Recognize patterns for the VZIP insns.  */
26623
26624 static bool
26625 arm_evpc_neon_vzip (struct expand_vec_perm_d *d)
26626 {
26627   unsigned int i, high, mask, nelt = d->nelt;
26628   rtx out0, out1, in0, in1, x;
26629   rtx (*gen)(rtx, rtx, rtx, rtx);
26630
26631   if (GET_MODE_UNIT_SIZE (d->vmode) >= 8)
26632     return false;
26633
26634   /* Note that these are little-endian tests.  Adjust for big-endian later.  */
26635   high = nelt / 2;
26636   if (d->perm[0] == high)
26637     ;
26638   else if (d->perm[0] == 0)
26639     high = 0;
26640   else
26641     return false;
26642   mask = (d->one_vector_p ? nelt - 1 : 2 * nelt - 1);
26643
26644   for (i = 0; i < nelt / 2; i++)
26645     {
26646       unsigned elt = (i + high) & mask;
26647       if (d->perm[i * 2] != elt)
26648         return false;
26649       elt = (elt + nelt) & mask;
26650       if (d->perm[i * 2 + 1] != elt)
26651         return false;
26652     }
26653
26654   /* Success!  */
26655   if (d->testing_p)
26656     return true;
26657
26658   switch (d->vmode)
26659     {
26660     case V16QImode: gen = gen_neon_vzipv16qi_internal; break;
26661     case V8QImode:  gen = gen_neon_vzipv8qi_internal;  break;
26662     case V8HImode:  gen = gen_neon_vzipv8hi_internal;  break;
26663     case V4HImode:  gen = gen_neon_vzipv4hi_internal;  break;
26664     case V4SImode:  gen = gen_neon_vzipv4si_internal;  break;
26665     case V2SImode:  gen = gen_neon_vzipv2si_internal;  break;
26666     case V2SFmode:  gen = gen_neon_vzipv2sf_internal;  break;
26667     case V4SFmode:  gen = gen_neon_vzipv4sf_internal;  break;
26668     default:
26669       gcc_unreachable ();
26670     }
26671
26672   in0 = d->op0;
26673   in1 = d->op1;
26674   if (BYTES_BIG_ENDIAN)
26675     {
26676       x = in0, in0 = in1, in1 = x;
26677       high = !high;
26678     }
26679
26680   out0 = d->target;
26681   out1 = gen_reg_rtx (d->vmode);
26682   if (high)
26683     x = out0, out0 = out1, out1 = x;
26684
26685   emit_insn (gen (out0, in0, in1, out1));
26686   return true;
26687 }
26688
26689 /* Recognize patterns for the VREV insns.  */
26690
26691 static bool
26692 arm_evpc_neon_vrev (struct expand_vec_perm_d *d)
26693 {
26694   unsigned int i, j, diff, nelt = d->nelt;
26695   rtx (*gen)(rtx, rtx, rtx);
26696
26697   if (!d->one_vector_p)
26698     return false;
26699
26700   diff = d->perm[0];
26701   switch (diff)
26702     {
26703     case 7:
26704       switch (d->vmode)
26705         {
26706         case V16QImode: gen = gen_neon_vrev64v16qi; break;
26707         case V8QImode:  gen = gen_neon_vrev64v8qi;  break;
26708         default:
26709           return false;
26710         }
26711       break;
26712     case 3:
26713       switch (d->vmode)
26714         {
26715         case V16QImode: gen = gen_neon_vrev32v16qi; break;
26716         case V8QImode:  gen = gen_neon_vrev32v8qi;  break;
26717         case V8HImode:  gen = gen_neon_vrev64v8hi;  break;
26718         case V4HImode:  gen = gen_neon_vrev64v4hi;  break;
26719         default:
26720           return false;
26721         }
26722       break;
26723     case 1:
26724       switch (d->vmode)
26725         {
26726         case V16QImode: gen = gen_neon_vrev16v16qi; break;
26727         case V8QImode:  gen = gen_neon_vrev16v8qi;  break;
26728         case V8HImode:  gen = gen_neon_vrev32v8hi;  break;
26729         case V4HImode:  gen = gen_neon_vrev32v4hi;  break;
26730         case V4SImode:  gen = gen_neon_vrev64v4si;  break;
26731         case V2SImode:  gen = gen_neon_vrev64v2si;  break;
26732         case V4SFmode:  gen = gen_neon_vrev64v4sf;  break;
26733         case V2SFmode:  gen = gen_neon_vrev64v2sf;  break;
26734         default:
26735           return false;
26736         }
26737       break;
26738     default:
26739       return false;
26740     }
26741
26742   for (i = 0; i < nelt ; i += diff + 1)
26743     for (j = 0; j <= diff; j += 1)
26744       {
26745         /* This is guaranteed to be true as the value of diff
26746            is 7, 3, 1 and we should have enough elements in the
26747            queue to generate this. Getting a vector mask with a
26748            value of diff other than these values implies that
26749            something is wrong by the time we get here.  */
26750         gcc_assert (i + j < nelt);
26751         if (d->perm[i + j] != i + diff - j)
26752           return false;
26753       }
26754
26755   /* Success! */
26756   if (d->testing_p)
26757     return true;
26758
26759   /* ??? The third operand is an artifact of the builtin infrastructure
26760      and is ignored by the actual instruction.  */
26761   emit_insn (gen (d->target, d->op0, const0_rtx));
26762   return true;
26763 }
26764
26765 /* Recognize patterns for the VTRN insns.  */
26766
26767 static bool
26768 arm_evpc_neon_vtrn (struct expand_vec_perm_d *d)
26769 {
26770   unsigned int i, odd, mask, nelt = d->nelt;
26771   rtx out0, out1, in0, in1, x;
26772   rtx (*gen)(rtx, rtx, rtx, rtx);
26773
26774   if (GET_MODE_UNIT_SIZE (d->vmode) >= 8)
26775     return false;
26776
26777   /* Note that these are little-endian tests.  Adjust for big-endian later.  */
26778   if (d->perm[0] == 0)
26779     odd = 0;
26780   else if (d->perm[0] == 1)
26781     odd = 1;
26782   else
26783     return false;
26784   mask = (d->one_vector_p ? nelt - 1 : 2 * nelt - 1);
26785
26786   for (i = 0; i < nelt; i += 2)
26787     {
26788       if (d->perm[i] != i + odd)
26789         return false;
26790       if (d->perm[i + 1] != ((i + nelt + odd) & mask))
26791         return false;
26792     }
26793
26794   /* Success!  */
26795   if (d->testing_p)
26796     return true;
26797
26798   switch (d->vmode)
26799     {
26800     case V16QImode: gen = gen_neon_vtrnv16qi_internal; break;
26801     case V8QImode:  gen = gen_neon_vtrnv8qi_internal;  break;
26802     case V8HImode:  gen = gen_neon_vtrnv8hi_internal;  break;
26803     case V4HImode:  gen = gen_neon_vtrnv4hi_internal;  break;
26804     case V4SImode:  gen = gen_neon_vtrnv4si_internal;  break;
26805     case V2SImode:  gen = gen_neon_vtrnv2si_internal;  break;
26806     case V2SFmode:  gen = gen_neon_vtrnv2sf_internal;  break;
26807     case V4SFmode:  gen = gen_neon_vtrnv4sf_internal;  break;
26808     default:
26809       gcc_unreachable ();
26810     }
26811
26812   in0 = d->op0;
26813   in1 = d->op1;
26814   if (BYTES_BIG_ENDIAN)
26815     {
26816       x = in0, in0 = in1, in1 = x;
26817       odd = !odd;
26818     }
26819
26820   out0 = d->target;
26821   out1 = gen_reg_rtx (d->vmode);
26822   if (odd)
26823     x = out0, out0 = out1, out1 = x;
26824
26825   emit_insn (gen (out0, in0, in1, out1));
26826   return true;
26827 }
26828
26829 /* Recognize patterns for the VEXT insns.  */
26830
26831 static bool
26832 arm_evpc_neon_vext (struct expand_vec_perm_d *d)
26833 {
26834   unsigned int i, nelt = d->nelt;
26835   rtx (*gen) (rtx, rtx, rtx, rtx);
26836   rtx offset;
26837
26838   unsigned int location;
26839
26840   unsigned int next  = d->perm[0] + 1;
26841
26842   /* TODO: Handle GCC's numbering of elements for big-endian.  */
26843   if (BYTES_BIG_ENDIAN)
26844     return false;
26845
26846   /* Check if the extracted indexes are increasing by one.  */
26847   for (i = 1; i < nelt; next++, i++)
26848     {
26849       /* If we hit the most significant element of the 2nd vector in
26850          the previous iteration, no need to test further.  */
26851       if (next == 2 * nelt)
26852         return false;
26853
26854       /* If we are operating on only one vector: it could be a
26855          rotation.  If there are only two elements of size < 64, let
26856          arm_evpc_neon_vrev catch it.  */
26857       if (d->one_vector_p && (next == nelt))
26858         {
26859           if ((nelt == 2) && (d->vmode != V2DImode))
26860             return false;
26861           else
26862             next = 0;
26863         }
26864
26865       if (d->perm[i] != next)
26866         return false;
26867     }
26868
26869   location = d->perm[0];
26870
26871   switch (d->vmode)
26872     {
26873     case V16QImode: gen = gen_neon_vextv16qi; break;
26874     case V8QImode: gen = gen_neon_vextv8qi; break;
26875     case V4HImode: gen = gen_neon_vextv4hi; break;
26876     case V8HImode: gen = gen_neon_vextv8hi; break;
26877     case V2SImode: gen = gen_neon_vextv2si; break;
26878     case V4SImode: gen = gen_neon_vextv4si; break;
26879     case V2SFmode: gen = gen_neon_vextv2sf; break;
26880     case V4SFmode: gen = gen_neon_vextv4sf; break;
26881     case V2DImode: gen = gen_neon_vextv2di; break;
26882     default:
26883       return false;
26884     }
26885
26886   /* Success! */
26887   if (d->testing_p)
26888     return true;
26889
26890   offset = GEN_INT (location);
26891   emit_insn (gen (d->target, d->op0, d->op1, offset));
26892   return true;
26893 }
26894
26895 /* The NEON VTBL instruction is a fully variable permuation that's even
26896    stronger than what we expose via VEC_PERM_EXPR.  What it doesn't do
26897    is mask the index operand as VEC_PERM_EXPR requires.  Therefore we
26898    can do slightly better by expanding this as a constant where we don't
26899    have to apply a mask.  */
26900
26901 static bool
26902 arm_evpc_neon_vtbl (struct expand_vec_perm_d *d)
26903 {
26904   rtx rperm[MAX_VECT_LEN], sel;
26905   enum machine_mode vmode = d->vmode;
26906   unsigned int i, nelt = d->nelt;
26907
26908   /* TODO: ARM's VTBL indexing is little-endian.  In order to handle GCC's
26909      numbering of elements for big-endian, we must reverse the order.  */
26910   if (BYTES_BIG_ENDIAN)
26911     return false;
26912
26913   if (d->testing_p)
26914     return true;
26915
26916   /* Generic code will try constant permutation twice.  Once with the
26917      original mode and again with the elements lowered to QImode.
26918      So wait and don't do the selector expansion ourselves.  */
26919   if (vmode != V8QImode && vmode != V16QImode)
26920     return false;
26921
26922   for (i = 0; i < nelt; ++i)
26923     rperm[i] = GEN_INT (d->perm[i]);
26924   sel = gen_rtx_CONST_VECTOR (vmode, gen_rtvec_v (nelt, rperm));
26925   sel = force_reg (vmode, sel);
26926
26927   arm_expand_vec_perm_1 (d->target, d->op0, d->op1, sel);
26928   return true;
26929 }
26930
26931 static bool
26932 arm_expand_vec_perm_const_1 (struct expand_vec_perm_d *d)
26933 {
26934   /* Check if the input mask matches vext before reordering the
26935      operands.  */
26936   if (TARGET_NEON)
26937     if (arm_evpc_neon_vext (d))
26938       return true;
26939
26940   /* The pattern matching functions above are written to look for a small
26941      number to begin the sequence (0, 1, N/2).  If we begin with an index
26942      from the second operand, we can swap the operands.  */
26943   if (d->perm[0] >= d->nelt)
26944     {
26945       unsigned i, nelt = d->nelt;
26946       rtx x;
26947
26948       for (i = 0; i < nelt; ++i)
26949         d->perm[i] = (d->perm[i] + nelt) & (2 * nelt - 1);
26950
26951       x = d->op0;
26952       d->op0 = d->op1;
26953       d->op1 = x;
26954     }
26955
26956   if (TARGET_NEON)
26957     {
26958       if (arm_evpc_neon_vuzp (d))
26959         return true;
26960       if (arm_evpc_neon_vzip (d))
26961         return true;
26962       if (arm_evpc_neon_vrev (d))
26963         return true;
26964       if (arm_evpc_neon_vtrn (d))
26965         return true;
26966       return arm_evpc_neon_vtbl (d);
26967     }
26968   return false;
26969 }
26970
26971 /* Expand a vec_perm_const pattern.  */
26972
26973 bool
26974 arm_expand_vec_perm_const (rtx target, rtx op0, rtx op1, rtx sel)
26975 {
26976   struct expand_vec_perm_d d;
26977   int i, nelt, which;
26978
26979   d.target = target;
26980   d.op0 = op0;
26981   d.op1 = op1;
26982
26983   d.vmode = GET_MODE (target);
26984   gcc_assert (VECTOR_MODE_P (d.vmode));
26985   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
26986   d.testing_p = false;
26987
26988   for (i = which = 0; i < nelt; ++i)
26989     {
26990       rtx e = XVECEXP (sel, 0, i);
26991       int ei = INTVAL (e) & (2 * nelt - 1);
26992       which |= (ei < nelt ? 1 : 2);
26993       d.perm[i] = ei;
26994     }
26995
26996   switch (which)
26997     {
26998     default:
26999       gcc_unreachable();
27000
27001     case 3:
27002       d.one_vector_p = false;
27003       if (!rtx_equal_p (op0, op1))
27004         break;
27005
27006       /* The elements of PERM do not suggest that only the first operand
27007          is used, but both operands are identical.  Allow easier matching
27008          of the permutation by folding the permutation into the single
27009          input vector.  */
27010       /* FALLTHRU */
27011     case 2:
27012       for (i = 0; i < nelt; ++i)
27013         d.perm[i] &= nelt - 1;
27014       d.op0 = op1;
27015       d.one_vector_p = true;
27016       break;
27017
27018     case 1:
27019       d.op1 = op0;
27020       d.one_vector_p = true;
27021       break;
27022     }
27023
27024   return arm_expand_vec_perm_const_1 (&d);
27025 }
27026
27027 /* Implement TARGET_VECTORIZE_VEC_PERM_CONST_OK.  */
27028
27029 static bool
27030 arm_vectorize_vec_perm_const_ok (enum machine_mode vmode,
27031                                  const unsigned char *sel)
27032 {
27033   struct expand_vec_perm_d d;
27034   unsigned int i, nelt, which;
27035   bool ret;
27036
27037   d.vmode = vmode;
27038   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
27039   d.testing_p = true;
27040   memcpy (d.perm, sel, nelt);
27041
27042   /* Categorize the set of elements in the selector.  */
27043   for (i = which = 0; i < nelt; ++i)
27044     {
27045       unsigned char e = d.perm[i];
27046       gcc_assert (e < 2 * nelt);
27047       which |= (e < nelt ? 1 : 2);
27048     }
27049
27050   /* For all elements from second vector, fold the elements to first.  */
27051   if (which == 2)
27052     for (i = 0; i < nelt; ++i)
27053       d.perm[i] -= nelt;
27054
27055   /* Check whether the mask can be applied to the vector type.  */
27056   d.one_vector_p = (which != 3);
27057
27058   d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
27059   d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
27060   if (!d.one_vector_p)
27061     d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
27062
27063   start_sequence ();
27064   ret = arm_expand_vec_perm_const_1 (&d);
27065   end_sequence ();
27066
27067   return ret;
27068 }
27069
27070 bool
27071 arm_autoinc_modes_ok_p (enum machine_mode mode, enum arm_auto_incmodes code)
27072 {
27073   /* If we are soft float and we do not have ldrd
27074      then all auto increment forms are ok.  */
27075   if (TARGET_SOFT_FLOAT && (TARGET_LDRD || GET_MODE_SIZE (mode) <= 4))
27076     return true;
27077
27078   switch (code)
27079     {
27080       /* Post increment and Pre Decrement are supported for all
27081          instruction forms except for vector forms.  */
27082     case ARM_POST_INC:
27083     case ARM_PRE_DEC:
27084       if (VECTOR_MODE_P (mode))
27085         {
27086           if (code != ARM_PRE_DEC)
27087             return true;
27088           else
27089             return false;
27090         }
27091       
27092       return true;
27093
27094     case ARM_POST_DEC:
27095     case ARM_PRE_INC:
27096       /* Without LDRD and mode size greater than
27097          word size, there is no point in auto-incrementing
27098          because ldm and stm will not have these forms.  */
27099       if (!TARGET_LDRD && GET_MODE_SIZE (mode) > 4)
27100         return false;
27101
27102       /* Vector and floating point modes do not support
27103          these auto increment forms.  */
27104       if (FLOAT_MODE_P (mode) || VECTOR_MODE_P (mode))
27105         return false;
27106
27107       return true;
27108      
27109     default:
27110       return false;
27111       
27112     }
27113
27114   return false;
27115 }
27116
27117 /* The default expansion of general 64-bit shifts in core-regs is suboptimal,
27118    on ARM, since we know that shifts by negative amounts are no-ops.
27119    Additionally, the default expansion code is not available or suitable
27120    for post-reload insn splits (this can occur when the register allocator
27121    chooses not to do a shift in NEON).
27122    
27123    This function is used in both initial expand and post-reload splits, and
27124    handles all kinds of 64-bit shifts.
27125
27126    Input requirements:
27127     - It is safe for the input and output to be the same register, but
27128       early-clobber rules apply for the shift amount and scratch registers.
27129     - Shift by register requires both scratch registers.  In all other cases
27130       the scratch registers may be NULL.
27131     - Ashiftrt by a register also clobbers the CC register.  */
27132 void
27133 arm_emit_coreregs_64bit_shift (enum rtx_code code, rtx out, rtx in,
27134                                rtx amount, rtx scratch1, rtx scratch2)
27135 {
27136   rtx out_high = gen_highpart (SImode, out);
27137   rtx out_low = gen_lowpart (SImode, out);
27138   rtx in_high = gen_highpart (SImode, in);
27139   rtx in_low = gen_lowpart (SImode, in);
27140
27141   /* Terminology:
27142         in = the register pair containing the input value.
27143         out = the destination register pair.
27144         up = the high- or low-part of each pair.
27145         down = the opposite part to "up".
27146      In a shift, we can consider bits to shift from "up"-stream to
27147      "down"-stream, so in a left-shift "up" is the low-part and "down"
27148      is the high-part of each register pair.  */
27149
27150   rtx out_up   = code == ASHIFT ? out_low : out_high;
27151   rtx out_down = code == ASHIFT ? out_high : out_low;
27152   rtx in_up   = code == ASHIFT ? in_low : in_high;
27153   rtx in_down = code == ASHIFT ? in_high : in_low;
27154
27155   gcc_assert (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT);
27156   gcc_assert (out
27157               && (REG_P (out) || GET_CODE (out) == SUBREG)
27158               && GET_MODE (out) == DImode);
27159   gcc_assert (in
27160               && (REG_P (in) || GET_CODE (in) == SUBREG)
27161               && GET_MODE (in) == DImode);
27162   gcc_assert (amount
27163               && (((REG_P (amount) || GET_CODE (amount) == SUBREG)
27164                    && GET_MODE (amount) == SImode)
27165                   || CONST_INT_P (amount)));
27166   gcc_assert (scratch1 == NULL
27167               || (GET_CODE (scratch1) == SCRATCH)
27168               || (GET_MODE (scratch1) == SImode
27169                   && REG_P (scratch1)));
27170   gcc_assert (scratch2 == NULL
27171               || (GET_CODE (scratch2) == SCRATCH)
27172               || (GET_MODE (scratch2) == SImode
27173                   && REG_P (scratch2)));
27174   gcc_assert (!REG_P (out) || !REG_P (amount)
27175               || !HARD_REGISTER_P (out)
27176               || (REGNO (out) != REGNO (amount)
27177                   && REGNO (out) + 1 != REGNO (amount)));
27178
27179   /* Macros to make following code more readable.  */
27180   #define SUB_32(DEST,SRC) \
27181             gen_addsi3 ((DEST), (SRC), GEN_INT (-32))
27182   #define RSB_32(DEST,SRC) \
27183             gen_subsi3 ((DEST), GEN_INT (32), (SRC))
27184   #define SUB_S_32(DEST,SRC) \
27185             gen_addsi3_compare0 ((DEST), (SRC), \
27186                                  GEN_INT (-32))
27187   #define SET(DEST,SRC) \
27188             gen_rtx_SET (SImode, (DEST), (SRC))
27189   #define SHIFT(CODE,SRC,AMOUNT) \
27190             gen_rtx_fmt_ee ((CODE), SImode, (SRC), (AMOUNT))
27191   #define LSHIFT(CODE,SRC,AMOUNT) \
27192             gen_rtx_fmt_ee ((CODE) == ASHIFT ? ASHIFT : LSHIFTRT, \
27193                             SImode, (SRC), (AMOUNT))
27194   #define REV_LSHIFT(CODE,SRC,AMOUNT) \
27195             gen_rtx_fmt_ee ((CODE) == ASHIFT ? LSHIFTRT : ASHIFT, \
27196                             SImode, (SRC), (AMOUNT))
27197   #define ORR(A,B) \
27198             gen_rtx_IOR (SImode, (A), (B))
27199   #define BRANCH(COND,LABEL) \
27200             gen_arm_cond_branch ((LABEL), \
27201                                  gen_rtx_ ## COND (CCmode, cc_reg, \
27202                                                    const0_rtx), \
27203                                  cc_reg)
27204
27205   /* Shifts by register and shifts by constant are handled separately.  */
27206   if (CONST_INT_P (amount))
27207     {
27208       /* We have a shift-by-constant.  */
27209
27210       /* First, handle out-of-range shift amounts.
27211          In both cases we try to match the result an ARM instruction in a
27212          shift-by-register would give.  This helps reduce execution
27213          differences between optimization levels, but it won't stop other
27214          parts of the compiler doing different things.  This is "undefined
27215          behaviour, in any case.  */
27216       if (INTVAL (amount) <= 0)
27217         emit_insn (gen_movdi (out, in));
27218       else if (INTVAL (amount) >= 64)
27219         {
27220           if (code == ASHIFTRT)
27221             {
27222               rtx const31_rtx = GEN_INT (31);
27223               emit_insn (SET (out_down, SHIFT (code, in_up, const31_rtx)));
27224               emit_insn (SET (out_up, SHIFT (code, in_up, const31_rtx)));
27225             }
27226           else
27227             emit_insn (gen_movdi (out, const0_rtx));
27228         }
27229
27230       /* Now handle valid shifts. */
27231       else if (INTVAL (amount) < 32)
27232         {
27233           /* Shifts by a constant less than 32.  */
27234           rtx reverse_amount = GEN_INT (32 - INTVAL (amount));
27235
27236           emit_insn (SET (out_down, LSHIFT (code, in_down, amount)));
27237           emit_insn (SET (out_down,
27238                           ORR (REV_LSHIFT (code, in_up, reverse_amount),
27239                                out_down)));
27240           emit_insn (SET (out_up, SHIFT (code, in_up, amount)));
27241         }
27242       else
27243         {
27244           /* Shifts by a constant greater than 31.  */
27245           rtx adj_amount = GEN_INT (INTVAL (amount) - 32);
27246
27247           emit_insn (SET (out_down, SHIFT (code, in_up, adj_amount)));
27248           if (code == ASHIFTRT)
27249             emit_insn (gen_ashrsi3 (out_up, in_up,
27250                                     GEN_INT (31)));
27251           else
27252             emit_insn (SET (out_up, const0_rtx));
27253         }
27254     }
27255   else
27256     {
27257       /* We have a shift-by-register.  */
27258       rtx cc_reg = gen_rtx_REG (CC_NOOVmode, CC_REGNUM);
27259
27260       /* This alternative requires the scratch registers.  */
27261       gcc_assert (scratch1 && REG_P (scratch1));
27262       gcc_assert (scratch2 && REG_P (scratch2));
27263
27264       /* We will need the values "amount-32" and "32-amount" later.
27265          Swapping them around now allows the later code to be more general. */
27266       switch (code)
27267         {
27268         case ASHIFT:
27269           emit_insn (SUB_32 (scratch1, amount));
27270           emit_insn (RSB_32 (scratch2, amount));
27271           break;
27272         case ASHIFTRT:
27273           emit_insn (RSB_32 (scratch1, amount));
27274           /* Also set CC = amount > 32.  */
27275           emit_insn (SUB_S_32 (scratch2, amount));
27276           break;
27277         case LSHIFTRT:
27278           emit_insn (RSB_32 (scratch1, amount));
27279           emit_insn (SUB_32 (scratch2, amount));
27280           break;
27281         default:
27282           gcc_unreachable ();
27283         }
27284
27285       /* Emit code like this:
27286
27287          arithmetic-left:
27288             out_down = in_down << amount;
27289             out_down = (in_up << (amount - 32)) | out_down;
27290             out_down = ((unsigned)in_up >> (32 - amount)) | out_down;
27291             out_up = in_up << amount;
27292
27293          arithmetic-right:
27294             out_down = in_down >> amount;
27295             out_down = (in_up << (32 - amount)) | out_down;
27296             if (amount < 32)
27297               out_down = ((signed)in_up >> (amount - 32)) | out_down;
27298             out_up = in_up << amount;
27299
27300          logical-right:
27301             out_down = in_down >> amount;
27302             out_down = (in_up << (32 - amount)) | out_down;
27303             if (amount < 32)
27304               out_down = ((unsigned)in_up >> (amount - 32)) | out_down;
27305             out_up = in_up << amount;
27306
27307           The ARM and Thumb2 variants are the same but implemented slightly
27308           differently.  If this were only called during expand we could just
27309           use the Thumb2 case and let combine do the right thing, but this
27310           can also be called from post-reload splitters.  */
27311
27312       emit_insn (SET (out_down, LSHIFT (code, in_down, amount)));
27313
27314       if (!TARGET_THUMB2)
27315         {
27316           /* Emit code for ARM mode.  */
27317           emit_insn (SET (out_down,
27318                           ORR (SHIFT (ASHIFT, in_up, scratch1), out_down)));
27319           if (code == ASHIFTRT)
27320             {
27321               rtx done_label = gen_label_rtx ();
27322               emit_jump_insn (BRANCH (LT, done_label));
27323               emit_insn (SET (out_down, ORR (SHIFT (ASHIFTRT, in_up, scratch2),
27324                                              out_down)));
27325               emit_label (done_label);
27326             }
27327           else
27328             emit_insn (SET (out_down, ORR (SHIFT (LSHIFTRT, in_up, scratch2),
27329                                            out_down)));
27330         }
27331       else
27332         {
27333           /* Emit code for Thumb2 mode.
27334              Thumb2 can't do shift and or in one insn.  */
27335           emit_insn (SET (scratch1, SHIFT (ASHIFT, in_up, scratch1)));
27336           emit_insn (gen_iorsi3 (out_down, out_down, scratch1));
27337
27338           if (code == ASHIFTRT)
27339             {
27340               rtx done_label = gen_label_rtx ();
27341               emit_jump_insn (BRANCH (LT, done_label));
27342               emit_insn (SET (scratch2, SHIFT (ASHIFTRT, in_up, scratch2)));
27343               emit_insn (SET (out_down, ORR (out_down, scratch2)));
27344               emit_label (done_label);
27345             }
27346           else
27347             {
27348               emit_insn (SET (scratch2, SHIFT (LSHIFTRT, in_up, scratch2)));
27349               emit_insn (gen_iorsi3 (out_down, out_down, scratch2));
27350             }
27351         }
27352
27353       emit_insn (SET (out_up, SHIFT (code, in_up, amount)));
27354     }
27355
27356   #undef SUB_32
27357   #undef RSB_32
27358   #undef SUB_S_32
27359   #undef SET
27360   #undef SHIFT
27361   #undef LSHIFT
27362   #undef REV_LSHIFT
27363   #undef ORR
27364   #undef BRANCH
27365 }
27366
27367
27368 /* Returns true if a valid comparison operation and makes
27369    the operands in a form that is valid.  */
27370 bool
27371 arm_validize_comparison (rtx *comparison, rtx * op1, rtx * op2)
27372 {
27373   enum rtx_code code = GET_CODE (*comparison);
27374   int code_int;
27375   enum machine_mode mode = (GET_MODE (*op1) == VOIDmode) 
27376     ? GET_MODE (*op2) : GET_MODE (*op1);
27377
27378   gcc_assert (GET_MODE (*op1) != VOIDmode || GET_MODE (*op2) != VOIDmode);
27379
27380   if (code == UNEQ || code == LTGT)
27381     return false;
27382
27383   code_int = (int)code;
27384   arm_canonicalize_comparison (&code_int, op1, op2, 0);
27385   PUT_CODE (*comparison, (enum rtx_code)code_int);
27386
27387   switch (mode)
27388     {
27389     case SImode:
27390       if (!arm_add_operand (*op1, mode))
27391         *op1 = force_reg (mode, *op1);
27392       if (!arm_add_operand (*op2, mode))
27393         *op2 = force_reg (mode, *op2);
27394       return true;
27395
27396     case DImode:
27397       if (!cmpdi_operand (*op1, mode))
27398         *op1 = force_reg (mode, *op1);
27399       if (!cmpdi_operand (*op2, mode))
27400         *op2 = force_reg (mode, *op2);
27401       return true;
27402
27403     case SFmode:
27404     case DFmode:
27405       if (!arm_float_compare_operand (*op1, mode))
27406         *op1 = force_reg (mode, *op1);
27407       if (!arm_float_compare_operand (*op2, mode))
27408         *op2 = force_reg (mode, *op2);
27409       return true;
27410     default:
27411       break;
27412     }
27413
27414   return false;
27415
27416 }
27417
27418 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook.  */
27419
27420 static unsigned HOST_WIDE_INT
27421 arm_asan_shadow_offset (void)
27422 {
27423   return (unsigned HOST_WIDE_INT) 1 << 29;
27424 }
27425
27426 #include "gt-arm.h"