recog_memoized works on an rtx_insn *
[official-gcc.git] / gcc / config / ia64 / ia64.c
blob7f9675cdc684a88235b083da37a2b7c44ec734b7
1 /* Definitions of target machine for GNU compiler.
2 Copyright (C) 1999-2014 Free Software Foundation, Inc.
3 Contributed by James E. Wilson <wilson@cygnus.com> and
4 David Mosberger <davidm@hpl.hp.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "stringpool.h"
29 #include "stor-layout.h"
30 #include "calls.h"
31 #include "varasm.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "insn-config.h"
35 #include "conditions.h"
36 #include "output.h"
37 #include "insn-attr.h"
38 #include "flags.h"
39 #include "recog.h"
40 #include "expr.h"
41 #include "optabs.h"
42 #include "except.h"
43 #include "function.h"
44 #include "ggc.h"
45 #include "basic-block.h"
46 #include "libfuncs.h"
47 #include "diagnostic-core.h"
48 #include "sched-int.h"
49 #include "timevar.h"
50 #include "target.h"
51 #include "target-def.h"
52 #include "common/common-target.h"
53 #include "tm_p.h"
54 #include "hash-table.h"
55 #include "langhooks.h"
56 #include "vec.h"
57 #include "basic-block.h"
58 #include "tree-ssa-alias.h"
59 #include "internal-fn.h"
60 #include "gimple-fold.h"
61 #include "tree-eh.h"
62 #include "gimple-expr.h"
63 #include "is-a.h"
64 #include "gimple.h"
65 #include "gimplify.h"
66 #include "intl.h"
67 #include "df.h"
68 #include "debug.h"
69 #include "params.h"
70 #include "dbgcnt.h"
71 #include "tm-constrs.h"
72 #include "sel-sched.h"
73 #include "reload.h"
74 #include "opts.h"
75 #include "dumpfile.h"
76 #include "builtins.h"
78 /* This is used for communication between ASM_OUTPUT_LABEL and
79 ASM_OUTPUT_LABELREF. */
80 int ia64_asm_output_label = 0;
82 /* Register names for ia64_expand_prologue. */
83 static const char * const ia64_reg_numbers[96] =
84 { "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
85 "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
86 "r48", "r49", "r50", "r51", "r52", "r53", "r54", "r55",
87 "r56", "r57", "r58", "r59", "r60", "r61", "r62", "r63",
88 "r64", "r65", "r66", "r67", "r68", "r69", "r70", "r71",
89 "r72", "r73", "r74", "r75", "r76", "r77", "r78", "r79",
90 "r80", "r81", "r82", "r83", "r84", "r85", "r86", "r87",
91 "r88", "r89", "r90", "r91", "r92", "r93", "r94", "r95",
92 "r96", "r97", "r98", "r99", "r100","r101","r102","r103",
93 "r104","r105","r106","r107","r108","r109","r110","r111",
94 "r112","r113","r114","r115","r116","r117","r118","r119",
95 "r120","r121","r122","r123","r124","r125","r126","r127"};
97 /* ??? These strings could be shared with REGISTER_NAMES. */
98 static const char * const ia64_input_reg_names[8] =
99 { "in0", "in1", "in2", "in3", "in4", "in5", "in6", "in7" };
101 /* ??? These strings could be shared with REGISTER_NAMES. */
102 static const char * const ia64_local_reg_names[80] =
103 { "loc0", "loc1", "loc2", "loc3", "loc4", "loc5", "loc6", "loc7",
104 "loc8", "loc9", "loc10","loc11","loc12","loc13","loc14","loc15",
105 "loc16","loc17","loc18","loc19","loc20","loc21","loc22","loc23",
106 "loc24","loc25","loc26","loc27","loc28","loc29","loc30","loc31",
107 "loc32","loc33","loc34","loc35","loc36","loc37","loc38","loc39",
108 "loc40","loc41","loc42","loc43","loc44","loc45","loc46","loc47",
109 "loc48","loc49","loc50","loc51","loc52","loc53","loc54","loc55",
110 "loc56","loc57","loc58","loc59","loc60","loc61","loc62","loc63",
111 "loc64","loc65","loc66","loc67","loc68","loc69","loc70","loc71",
112 "loc72","loc73","loc74","loc75","loc76","loc77","loc78","loc79" };
114 /* ??? These strings could be shared with REGISTER_NAMES. */
115 static const char * const ia64_output_reg_names[8] =
116 { "out0", "out1", "out2", "out3", "out4", "out5", "out6", "out7" };
118 /* Variables which are this size or smaller are put in the sdata/sbss
119 sections. */
121 unsigned int ia64_section_threshold;
123 /* The following variable is used by the DFA insn scheduler. The value is
124 TRUE if we do insn bundling instead of insn scheduling. */
125 int bundling_p = 0;
127 enum ia64_frame_regs
129 reg_fp,
130 reg_save_b0,
131 reg_save_pr,
132 reg_save_ar_pfs,
133 reg_save_ar_unat,
134 reg_save_ar_lc,
135 reg_save_gp,
136 number_of_ia64_frame_regs
139 /* Structure to be filled in by ia64_compute_frame_size with register
140 save masks and offsets for the current function. */
142 struct ia64_frame_info
144 HOST_WIDE_INT total_size; /* size of the stack frame, not including
145 the caller's scratch area. */
146 HOST_WIDE_INT spill_cfa_off; /* top of the reg spill area from the cfa. */
147 HOST_WIDE_INT spill_size; /* size of the gr/br/fr spill area. */
148 HOST_WIDE_INT extra_spill_size; /* size of spill area for others. */
149 HARD_REG_SET mask; /* mask of saved registers. */
150 unsigned int gr_used_mask; /* mask of registers in use as gr spill
151 registers or long-term scratches. */
152 int n_spilled; /* number of spilled registers. */
153 int r[number_of_ia64_frame_regs]; /* Frame related registers. */
154 int n_input_regs; /* number of input registers used. */
155 int n_local_regs; /* number of local registers used. */
156 int n_output_regs; /* number of output registers used. */
157 int n_rotate_regs; /* number of rotating registers used. */
159 char need_regstk; /* true if a .regstk directive needed. */
160 char initialized; /* true if the data is finalized. */
163 /* Current frame information calculated by ia64_compute_frame_size. */
164 static struct ia64_frame_info current_frame_info;
165 /* The actual registers that are emitted. */
166 static int emitted_frame_related_regs[number_of_ia64_frame_regs];
168 static int ia64_first_cycle_multipass_dfa_lookahead (void);
169 static void ia64_dependencies_evaluation_hook (rtx_insn *, rtx_insn *);
170 static void ia64_init_dfa_pre_cycle_insn (void);
171 static rtx ia64_dfa_pre_cycle_insn (void);
172 static int ia64_first_cycle_multipass_dfa_lookahead_guard (rtx_insn *, int);
173 static int ia64_dfa_new_cycle (FILE *, int, rtx_insn *, int, int, int *);
174 static void ia64_h_i_d_extended (void);
175 static void * ia64_alloc_sched_context (void);
176 static void ia64_init_sched_context (void *, bool);
177 static void ia64_set_sched_context (void *);
178 static void ia64_clear_sched_context (void *);
179 static void ia64_free_sched_context (void *);
180 static int ia64_mode_to_int (enum machine_mode);
181 static void ia64_set_sched_flags (spec_info_t);
182 static ds_t ia64_get_insn_spec_ds (rtx_insn *);
183 static ds_t ia64_get_insn_checked_ds (rtx_insn *);
184 static bool ia64_skip_rtx_p (const_rtx);
185 static int ia64_speculate_insn (rtx_insn *, ds_t, rtx *);
186 static bool ia64_needs_block_p (ds_t);
187 static rtx ia64_gen_spec_check (rtx_insn *, rtx_insn *, ds_t);
188 static int ia64_spec_check_p (rtx);
189 static int ia64_spec_check_src_p (rtx);
190 static rtx gen_tls_get_addr (void);
191 static rtx gen_thread_pointer (void);
192 static int find_gr_spill (enum ia64_frame_regs, int);
193 static int next_scratch_gr_reg (void);
194 static void mark_reg_gr_used_mask (rtx, void *);
195 static void ia64_compute_frame_size (HOST_WIDE_INT);
196 static void setup_spill_pointers (int, rtx, HOST_WIDE_INT);
197 static void finish_spill_pointers (void);
198 static rtx spill_restore_mem (rtx, HOST_WIDE_INT);
199 static void do_spill (rtx (*)(rtx, rtx, rtx), rtx, HOST_WIDE_INT, rtx);
200 static void do_restore (rtx (*)(rtx, rtx, rtx), rtx, HOST_WIDE_INT);
201 static rtx gen_movdi_x (rtx, rtx, rtx);
202 static rtx gen_fr_spill_x (rtx, rtx, rtx);
203 static rtx gen_fr_restore_x (rtx, rtx, rtx);
205 static void ia64_option_override (void);
206 static bool ia64_can_eliminate (const int, const int);
207 static enum machine_mode hfa_element_mode (const_tree, bool);
208 static void ia64_setup_incoming_varargs (cumulative_args_t, enum machine_mode,
209 tree, int *, int);
210 static int ia64_arg_partial_bytes (cumulative_args_t, enum machine_mode,
211 tree, bool);
212 static rtx ia64_function_arg_1 (cumulative_args_t, enum machine_mode,
213 const_tree, bool, bool);
214 static rtx ia64_function_arg (cumulative_args_t, enum machine_mode,
215 const_tree, bool);
216 static rtx ia64_function_incoming_arg (cumulative_args_t,
217 enum machine_mode, const_tree, bool);
218 static void ia64_function_arg_advance (cumulative_args_t, enum machine_mode,
219 const_tree, bool);
220 static unsigned int ia64_function_arg_boundary (enum machine_mode,
221 const_tree);
222 static bool ia64_function_ok_for_sibcall (tree, tree);
223 static bool ia64_return_in_memory (const_tree, const_tree);
224 static rtx ia64_function_value (const_tree, const_tree, bool);
225 static rtx ia64_libcall_value (enum machine_mode, const_rtx);
226 static bool ia64_function_value_regno_p (const unsigned int);
227 static int ia64_register_move_cost (enum machine_mode, reg_class_t,
228 reg_class_t);
229 static int ia64_memory_move_cost (enum machine_mode mode, reg_class_t,
230 bool);
231 static bool ia64_rtx_costs (rtx, int, int, int, int *, bool);
232 static int ia64_unspec_may_trap_p (const_rtx, unsigned);
233 static void fix_range (const char *);
234 static struct machine_function * ia64_init_machine_status (void);
235 static void emit_insn_group_barriers (FILE *);
236 static void emit_all_insn_group_barriers (FILE *);
237 static void final_emit_insn_group_barriers (FILE *);
238 static void emit_predicate_relation_info (void);
239 static void ia64_reorg (void);
240 static bool ia64_in_small_data_p (const_tree);
241 static void process_epilogue (FILE *, rtx, bool, bool);
243 static bool ia64_assemble_integer (rtx, unsigned int, int);
244 static void ia64_output_function_prologue (FILE *, HOST_WIDE_INT);
245 static void ia64_output_function_epilogue (FILE *, HOST_WIDE_INT);
246 static void ia64_output_function_end_prologue (FILE *);
248 static void ia64_print_operand (FILE *, rtx, int);
249 static void ia64_print_operand_address (FILE *, rtx);
250 static bool ia64_print_operand_punct_valid_p (unsigned char code);
252 static int ia64_issue_rate (void);
253 static int ia64_adjust_cost_2 (rtx_insn *, int, rtx_insn *, int, dw_t);
254 static void ia64_sched_init (FILE *, int, int);
255 static void ia64_sched_init_global (FILE *, int, int);
256 static void ia64_sched_finish_global (FILE *, int);
257 static void ia64_sched_finish (FILE *, int);
258 static int ia64_dfa_sched_reorder (FILE *, int, rtx_insn **, int *, int, int);
259 static int ia64_sched_reorder (FILE *, int, rtx_insn **, int *, int);
260 static int ia64_sched_reorder2 (FILE *, int, rtx_insn **, int *, int);
261 static int ia64_variable_issue (FILE *, int, rtx_insn *, int);
263 static void ia64_asm_unwind_emit (FILE *, rtx_insn *);
264 static void ia64_asm_emit_except_personality (rtx);
265 static void ia64_asm_init_sections (void);
267 static enum unwind_info_type ia64_debug_unwind_info (void);
269 static struct bundle_state *get_free_bundle_state (void);
270 static void free_bundle_state (struct bundle_state *);
271 static void initiate_bundle_states (void);
272 static void finish_bundle_states (void);
273 static int insert_bundle_state (struct bundle_state *);
274 static void initiate_bundle_state_table (void);
275 static void finish_bundle_state_table (void);
276 static int try_issue_nops (struct bundle_state *, int);
277 static int try_issue_insn (struct bundle_state *, rtx);
278 static void issue_nops_and_insn (struct bundle_state *, int, rtx_insn *,
279 int, int);
280 static int get_max_pos (state_t);
281 static int get_template (state_t, int);
283 static rtx_insn *get_next_important_insn (rtx_insn *, rtx_insn *);
284 static bool important_for_bundling_p (rtx_insn *);
285 static bool unknown_for_bundling_p (rtx_insn *);
286 static void bundling (FILE *, int, rtx_insn *, rtx_insn *);
288 static void ia64_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
289 HOST_WIDE_INT, tree);
290 static void ia64_file_start (void);
291 static void ia64_globalize_decl_name (FILE *, tree);
293 static int ia64_hpux_reloc_rw_mask (void) ATTRIBUTE_UNUSED;
294 static int ia64_reloc_rw_mask (void) ATTRIBUTE_UNUSED;
295 static section *ia64_select_rtx_section (enum machine_mode, rtx,
296 unsigned HOST_WIDE_INT);
297 static void ia64_output_dwarf_dtprel (FILE *, int, rtx)
298 ATTRIBUTE_UNUSED;
299 static unsigned int ia64_section_type_flags (tree, const char *, int);
300 static void ia64_init_libfuncs (void)
301 ATTRIBUTE_UNUSED;
302 static void ia64_hpux_init_libfuncs (void)
303 ATTRIBUTE_UNUSED;
304 static void ia64_sysv4_init_libfuncs (void)
305 ATTRIBUTE_UNUSED;
306 static void ia64_vms_init_libfuncs (void)
307 ATTRIBUTE_UNUSED;
308 static void ia64_soft_fp_init_libfuncs (void)
309 ATTRIBUTE_UNUSED;
310 static bool ia64_vms_valid_pointer_mode (enum machine_mode mode)
311 ATTRIBUTE_UNUSED;
312 static tree ia64_vms_common_object_attribute (tree *, tree, tree, int, bool *)
313 ATTRIBUTE_UNUSED;
315 static tree ia64_handle_model_attribute (tree *, tree, tree, int, bool *);
316 static tree ia64_handle_version_id_attribute (tree *, tree, tree, int, bool *);
317 static void ia64_encode_section_info (tree, rtx, int);
318 static rtx ia64_struct_value_rtx (tree, int);
319 static tree ia64_gimplify_va_arg (tree, tree, gimple_seq *, gimple_seq *);
320 static bool ia64_scalar_mode_supported_p (enum machine_mode mode);
321 static bool ia64_vector_mode_supported_p (enum machine_mode mode);
322 static bool ia64_legitimate_constant_p (enum machine_mode, rtx);
323 static bool ia64_legitimate_address_p (enum machine_mode, rtx, bool);
324 static bool ia64_cannot_force_const_mem (enum machine_mode, rtx);
325 static const char *ia64_mangle_type (const_tree);
326 static const char *ia64_invalid_conversion (const_tree, const_tree);
327 static const char *ia64_invalid_unary_op (int, const_tree);
328 static const char *ia64_invalid_binary_op (int, const_tree, const_tree);
329 static enum machine_mode ia64_c_mode_for_suffix (char);
330 static void ia64_trampoline_init (rtx, tree, rtx);
331 static void ia64_override_options_after_change (void);
332 static bool ia64_member_type_forces_blk (const_tree, enum machine_mode);
334 static tree ia64_builtin_decl (unsigned, bool);
336 static reg_class_t ia64_preferred_reload_class (rtx, reg_class_t);
337 static enum machine_mode ia64_get_reg_raw_mode (int regno);
338 static section * ia64_hpux_function_section (tree, enum node_frequency,
339 bool, bool);
341 static bool ia64_vectorize_vec_perm_const_ok (enum machine_mode vmode,
342 const unsigned char *sel);
344 #define MAX_VECT_LEN 8
346 struct expand_vec_perm_d
348 rtx target, op0, op1;
349 unsigned char perm[MAX_VECT_LEN];
350 enum machine_mode vmode;
351 unsigned char nelt;
352 bool one_operand_p;
353 bool testing_p;
356 static bool ia64_expand_vec_perm_const_1 (struct expand_vec_perm_d *d);
359 /* Table of valid machine attributes. */
360 static const struct attribute_spec ia64_attribute_table[] =
362 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
363 affects_type_identity } */
364 { "syscall_linkage", 0, 0, false, true, true, NULL, false },
365 { "model", 1, 1, true, false, false, ia64_handle_model_attribute,
366 false },
367 #if TARGET_ABI_OPEN_VMS
368 { "common_object", 1, 1, true, false, false,
369 ia64_vms_common_object_attribute, false },
370 #endif
371 { "version_id", 1, 1, true, false, false,
372 ia64_handle_version_id_attribute, false },
373 { NULL, 0, 0, false, false, false, NULL, false }
376 /* Initialize the GCC target structure. */
377 #undef TARGET_ATTRIBUTE_TABLE
378 #define TARGET_ATTRIBUTE_TABLE ia64_attribute_table
380 #undef TARGET_INIT_BUILTINS
381 #define TARGET_INIT_BUILTINS ia64_init_builtins
383 #undef TARGET_EXPAND_BUILTIN
384 #define TARGET_EXPAND_BUILTIN ia64_expand_builtin
386 #undef TARGET_BUILTIN_DECL
387 #define TARGET_BUILTIN_DECL ia64_builtin_decl
389 #undef TARGET_ASM_BYTE_OP
390 #define TARGET_ASM_BYTE_OP "\tdata1\t"
391 #undef TARGET_ASM_ALIGNED_HI_OP
392 #define TARGET_ASM_ALIGNED_HI_OP "\tdata2\t"
393 #undef TARGET_ASM_ALIGNED_SI_OP
394 #define TARGET_ASM_ALIGNED_SI_OP "\tdata4\t"
395 #undef TARGET_ASM_ALIGNED_DI_OP
396 #define TARGET_ASM_ALIGNED_DI_OP "\tdata8\t"
397 #undef TARGET_ASM_UNALIGNED_HI_OP
398 #define TARGET_ASM_UNALIGNED_HI_OP "\tdata2.ua\t"
399 #undef TARGET_ASM_UNALIGNED_SI_OP
400 #define TARGET_ASM_UNALIGNED_SI_OP "\tdata4.ua\t"
401 #undef TARGET_ASM_UNALIGNED_DI_OP
402 #define TARGET_ASM_UNALIGNED_DI_OP "\tdata8.ua\t"
403 #undef TARGET_ASM_INTEGER
404 #define TARGET_ASM_INTEGER ia64_assemble_integer
406 #undef TARGET_OPTION_OVERRIDE
407 #define TARGET_OPTION_OVERRIDE ia64_option_override
409 #undef TARGET_ASM_FUNCTION_PROLOGUE
410 #define TARGET_ASM_FUNCTION_PROLOGUE ia64_output_function_prologue
411 #undef TARGET_ASM_FUNCTION_END_PROLOGUE
412 #define TARGET_ASM_FUNCTION_END_PROLOGUE ia64_output_function_end_prologue
413 #undef TARGET_ASM_FUNCTION_EPILOGUE
414 #define TARGET_ASM_FUNCTION_EPILOGUE ia64_output_function_epilogue
416 #undef TARGET_PRINT_OPERAND
417 #define TARGET_PRINT_OPERAND ia64_print_operand
418 #undef TARGET_PRINT_OPERAND_ADDRESS
419 #define TARGET_PRINT_OPERAND_ADDRESS ia64_print_operand_address
420 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
421 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P ia64_print_operand_punct_valid_p
423 #undef TARGET_IN_SMALL_DATA_P
424 #define TARGET_IN_SMALL_DATA_P ia64_in_small_data_p
426 #undef TARGET_SCHED_ADJUST_COST_2
427 #define TARGET_SCHED_ADJUST_COST_2 ia64_adjust_cost_2
428 #undef TARGET_SCHED_ISSUE_RATE
429 #define TARGET_SCHED_ISSUE_RATE ia64_issue_rate
430 #undef TARGET_SCHED_VARIABLE_ISSUE
431 #define TARGET_SCHED_VARIABLE_ISSUE ia64_variable_issue
432 #undef TARGET_SCHED_INIT
433 #define TARGET_SCHED_INIT ia64_sched_init
434 #undef TARGET_SCHED_FINISH
435 #define TARGET_SCHED_FINISH ia64_sched_finish
436 #undef TARGET_SCHED_INIT_GLOBAL
437 #define TARGET_SCHED_INIT_GLOBAL ia64_sched_init_global
438 #undef TARGET_SCHED_FINISH_GLOBAL
439 #define TARGET_SCHED_FINISH_GLOBAL ia64_sched_finish_global
440 #undef TARGET_SCHED_REORDER
441 #define TARGET_SCHED_REORDER ia64_sched_reorder
442 #undef TARGET_SCHED_REORDER2
443 #define TARGET_SCHED_REORDER2 ia64_sched_reorder2
445 #undef TARGET_SCHED_DEPENDENCIES_EVALUATION_HOOK
446 #define TARGET_SCHED_DEPENDENCIES_EVALUATION_HOOK ia64_dependencies_evaluation_hook
448 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
449 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD ia64_first_cycle_multipass_dfa_lookahead
451 #undef TARGET_SCHED_INIT_DFA_PRE_CYCLE_INSN
452 #define TARGET_SCHED_INIT_DFA_PRE_CYCLE_INSN ia64_init_dfa_pre_cycle_insn
453 #undef TARGET_SCHED_DFA_PRE_CYCLE_INSN
454 #define TARGET_SCHED_DFA_PRE_CYCLE_INSN ia64_dfa_pre_cycle_insn
456 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
457 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD\
458 ia64_first_cycle_multipass_dfa_lookahead_guard
460 #undef TARGET_SCHED_DFA_NEW_CYCLE
461 #define TARGET_SCHED_DFA_NEW_CYCLE ia64_dfa_new_cycle
463 #undef TARGET_SCHED_H_I_D_EXTENDED
464 #define TARGET_SCHED_H_I_D_EXTENDED ia64_h_i_d_extended
466 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
467 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT ia64_alloc_sched_context
469 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
470 #define TARGET_SCHED_INIT_SCHED_CONTEXT ia64_init_sched_context
472 #undef TARGET_SCHED_SET_SCHED_CONTEXT
473 #define TARGET_SCHED_SET_SCHED_CONTEXT ia64_set_sched_context
475 #undef TARGET_SCHED_CLEAR_SCHED_CONTEXT
476 #define TARGET_SCHED_CLEAR_SCHED_CONTEXT ia64_clear_sched_context
478 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
479 #define TARGET_SCHED_FREE_SCHED_CONTEXT ia64_free_sched_context
481 #undef TARGET_SCHED_SET_SCHED_FLAGS
482 #define TARGET_SCHED_SET_SCHED_FLAGS ia64_set_sched_flags
484 #undef TARGET_SCHED_GET_INSN_SPEC_DS
485 #define TARGET_SCHED_GET_INSN_SPEC_DS ia64_get_insn_spec_ds
487 #undef TARGET_SCHED_GET_INSN_CHECKED_DS
488 #define TARGET_SCHED_GET_INSN_CHECKED_DS ia64_get_insn_checked_ds
490 #undef TARGET_SCHED_SPECULATE_INSN
491 #define TARGET_SCHED_SPECULATE_INSN ia64_speculate_insn
493 #undef TARGET_SCHED_NEEDS_BLOCK_P
494 #define TARGET_SCHED_NEEDS_BLOCK_P ia64_needs_block_p
496 #undef TARGET_SCHED_GEN_SPEC_CHECK
497 #define TARGET_SCHED_GEN_SPEC_CHECK ia64_gen_spec_check
499 #undef TARGET_SCHED_SKIP_RTX_P
500 #define TARGET_SCHED_SKIP_RTX_P ia64_skip_rtx_p
502 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
503 #define TARGET_FUNCTION_OK_FOR_SIBCALL ia64_function_ok_for_sibcall
504 #undef TARGET_ARG_PARTIAL_BYTES
505 #define TARGET_ARG_PARTIAL_BYTES ia64_arg_partial_bytes
506 #undef TARGET_FUNCTION_ARG
507 #define TARGET_FUNCTION_ARG ia64_function_arg
508 #undef TARGET_FUNCTION_INCOMING_ARG
509 #define TARGET_FUNCTION_INCOMING_ARG ia64_function_incoming_arg
510 #undef TARGET_FUNCTION_ARG_ADVANCE
511 #define TARGET_FUNCTION_ARG_ADVANCE ia64_function_arg_advance
512 #undef TARGET_FUNCTION_ARG_BOUNDARY
513 #define TARGET_FUNCTION_ARG_BOUNDARY ia64_function_arg_boundary
515 #undef TARGET_ASM_OUTPUT_MI_THUNK
516 #define TARGET_ASM_OUTPUT_MI_THUNK ia64_output_mi_thunk
517 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
518 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
520 #undef TARGET_ASM_FILE_START
521 #define TARGET_ASM_FILE_START ia64_file_start
523 #undef TARGET_ASM_GLOBALIZE_DECL_NAME
524 #define TARGET_ASM_GLOBALIZE_DECL_NAME ia64_globalize_decl_name
526 #undef TARGET_REGISTER_MOVE_COST
527 #define TARGET_REGISTER_MOVE_COST ia64_register_move_cost
528 #undef TARGET_MEMORY_MOVE_COST
529 #define TARGET_MEMORY_MOVE_COST ia64_memory_move_cost
530 #undef TARGET_RTX_COSTS
531 #define TARGET_RTX_COSTS ia64_rtx_costs
532 #undef TARGET_ADDRESS_COST
533 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
535 #undef TARGET_UNSPEC_MAY_TRAP_P
536 #define TARGET_UNSPEC_MAY_TRAP_P ia64_unspec_may_trap_p
538 #undef TARGET_MACHINE_DEPENDENT_REORG
539 #define TARGET_MACHINE_DEPENDENT_REORG ia64_reorg
541 #undef TARGET_ENCODE_SECTION_INFO
542 #define TARGET_ENCODE_SECTION_INFO ia64_encode_section_info
544 #undef TARGET_SECTION_TYPE_FLAGS
545 #define TARGET_SECTION_TYPE_FLAGS ia64_section_type_flags
547 #ifdef HAVE_AS_TLS
548 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
549 #define TARGET_ASM_OUTPUT_DWARF_DTPREL ia64_output_dwarf_dtprel
550 #endif
552 /* ??? Investigate. */
553 #if 0
554 #undef TARGET_PROMOTE_PROTOTYPES
555 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
556 #endif
558 #undef TARGET_FUNCTION_VALUE
559 #define TARGET_FUNCTION_VALUE ia64_function_value
560 #undef TARGET_LIBCALL_VALUE
561 #define TARGET_LIBCALL_VALUE ia64_libcall_value
562 #undef TARGET_FUNCTION_VALUE_REGNO_P
563 #define TARGET_FUNCTION_VALUE_REGNO_P ia64_function_value_regno_p
565 #undef TARGET_STRUCT_VALUE_RTX
566 #define TARGET_STRUCT_VALUE_RTX ia64_struct_value_rtx
567 #undef TARGET_RETURN_IN_MEMORY
568 #define TARGET_RETURN_IN_MEMORY ia64_return_in_memory
569 #undef TARGET_SETUP_INCOMING_VARARGS
570 #define TARGET_SETUP_INCOMING_VARARGS ia64_setup_incoming_varargs
571 #undef TARGET_STRICT_ARGUMENT_NAMING
572 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
573 #undef TARGET_MUST_PASS_IN_STACK
574 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
575 #undef TARGET_GET_RAW_RESULT_MODE
576 #define TARGET_GET_RAW_RESULT_MODE ia64_get_reg_raw_mode
577 #undef TARGET_GET_RAW_ARG_MODE
578 #define TARGET_GET_RAW_ARG_MODE ia64_get_reg_raw_mode
580 #undef TARGET_MEMBER_TYPE_FORCES_BLK
581 #define TARGET_MEMBER_TYPE_FORCES_BLK ia64_member_type_forces_blk
583 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
584 #define TARGET_GIMPLIFY_VA_ARG_EXPR ia64_gimplify_va_arg
586 #undef TARGET_ASM_UNWIND_EMIT
587 #define TARGET_ASM_UNWIND_EMIT ia64_asm_unwind_emit
588 #undef TARGET_ASM_EMIT_EXCEPT_PERSONALITY
589 #define TARGET_ASM_EMIT_EXCEPT_PERSONALITY ia64_asm_emit_except_personality
590 #undef TARGET_ASM_INIT_SECTIONS
591 #define TARGET_ASM_INIT_SECTIONS ia64_asm_init_sections
593 #undef TARGET_DEBUG_UNWIND_INFO
594 #define TARGET_DEBUG_UNWIND_INFO ia64_debug_unwind_info
596 #undef TARGET_SCALAR_MODE_SUPPORTED_P
597 #define TARGET_SCALAR_MODE_SUPPORTED_P ia64_scalar_mode_supported_p
598 #undef TARGET_VECTOR_MODE_SUPPORTED_P
599 #define TARGET_VECTOR_MODE_SUPPORTED_P ia64_vector_mode_supported_p
601 /* ia64 architecture manual 4.4.7: ... reads, writes, and flushes may occur
602 in an order different from the specified program order. */
603 #undef TARGET_RELAXED_ORDERING
604 #define TARGET_RELAXED_ORDERING true
606 #undef TARGET_LEGITIMATE_CONSTANT_P
607 #define TARGET_LEGITIMATE_CONSTANT_P ia64_legitimate_constant_p
608 #undef TARGET_LEGITIMATE_ADDRESS_P
609 #define TARGET_LEGITIMATE_ADDRESS_P ia64_legitimate_address_p
611 #undef TARGET_CANNOT_FORCE_CONST_MEM
612 #define TARGET_CANNOT_FORCE_CONST_MEM ia64_cannot_force_const_mem
614 #undef TARGET_MANGLE_TYPE
615 #define TARGET_MANGLE_TYPE ia64_mangle_type
617 #undef TARGET_INVALID_CONVERSION
618 #define TARGET_INVALID_CONVERSION ia64_invalid_conversion
619 #undef TARGET_INVALID_UNARY_OP
620 #define TARGET_INVALID_UNARY_OP ia64_invalid_unary_op
621 #undef TARGET_INVALID_BINARY_OP
622 #define TARGET_INVALID_BINARY_OP ia64_invalid_binary_op
624 #undef TARGET_C_MODE_FOR_SUFFIX
625 #define TARGET_C_MODE_FOR_SUFFIX ia64_c_mode_for_suffix
627 #undef TARGET_CAN_ELIMINATE
628 #define TARGET_CAN_ELIMINATE ia64_can_eliminate
630 #undef TARGET_TRAMPOLINE_INIT
631 #define TARGET_TRAMPOLINE_INIT ia64_trampoline_init
633 #undef TARGET_CAN_USE_DOLOOP_P
634 #define TARGET_CAN_USE_DOLOOP_P can_use_doloop_if_innermost
635 #undef TARGET_INVALID_WITHIN_DOLOOP
636 #define TARGET_INVALID_WITHIN_DOLOOP hook_constcharptr_const_rtx_insn_null
638 #undef TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE
639 #define TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE ia64_override_options_after_change
641 #undef TARGET_PREFERRED_RELOAD_CLASS
642 #define TARGET_PREFERRED_RELOAD_CLASS ia64_preferred_reload_class
644 #undef TARGET_DELAY_SCHED2
645 #define TARGET_DELAY_SCHED2 true
647 /* Variable tracking should be run after all optimizations which
648 change order of insns. It also needs a valid CFG. */
649 #undef TARGET_DELAY_VARTRACK
650 #define TARGET_DELAY_VARTRACK true
652 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
653 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK ia64_vectorize_vec_perm_const_ok
655 struct gcc_target targetm = TARGET_INITIALIZER;
657 typedef enum
659 ADDR_AREA_NORMAL, /* normal address area */
660 ADDR_AREA_SMALL /* addressable by "addl" (-2MB < addr < 2MB) */
662 ia64_addr_area;
664 static GTY(()) tree small_ident1;
665 static GTY(()) tree small_ident2;
667 static void
668 init_idents (void)
670 if (small_ident1 == 0)
672 small_ident1 = get_identifier ("small");
673 small_ident2 = get_identifier ("__small__");
677 /* Retrieve the address area that has been chosen for the given decl. */
679 static ia64_addr_area
680 ia64_get_addr_area (tree decl)
682 tree model_attr;
684 model_attr = lookup_attribute ("model", DECL_ATTRIBUTES (decl));
685 if (model_attr)
687 tree id;
689 init_idents ();
690 id = TREE_VALUE (TREE_VALUE (model_attr));
691 if (id == small_ident1 || id == small_ident2)
692 return ADDR_AREA_SMALL;
694 return ADDR_AREA_NORMAL;
697 static tree
698 ia64_handle_model_attribute (tree *node, tree name, tree args,
699 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
701 ia64_addr_area addr_area = ADDR_AREA_NORMAL;
702 ia64_addr_area area;
703 tree arg, decl = *node;
705 init_idents ();
706 arg = TREE_VALUE (args);
707 if (arg == small_ident1 || arg == small_ident2)
709 addr_area = ADDR_AREA_SMALL;
711 else
713 warning (OPT_Wattributes, "invalid argument of %qE attribute",
714 name);
715 *no_add_attrs = true;
718 switch (TREE_CODE (decl))
720 case VAR_DECL:
721 if ((DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl))
722 == FUNCTION_DECL)
723 && !TREE_STATIC (decl))
725 error_at (DECL_SOURCE_LOCATION (decl),
726 "an address area attribute cannot be specified for "
727 "local variables");
728 *no_add_attrs = true;
730 area = ia64_get_addr_area (decl);
731 if (area != ADDR_AREA_NORMAL && addr_area != area)
733 error ("address area of %q+D conflicts with previous "
734 "declaration", decl);
735 *no_add_attrs = true;
737 break;
739 case FUNCTION_DECL:
740 error_at (DECL_SOURCE_LOCATION (decl),
741 "address area attribute cannot be specified for "
742 "functions");
743 *no_add_attrs = true;
744 break;
746 default:
747 warning (OPT_Wattributes, "%qE attribute ignored",
748 name);
749 *no_add_attrs = true;
750 break;
753 return NULL_TREE;
756 /* Part of the low level implementation of DEC Ada pragma Common_Object which
757 enables the shared use of variables stored in overlaid linker areas
758 corresponding to the use of Fortran COMMON. */
760 static tree
761 ia64_vms_common_object_attribute (tree *node, tree name, tree args,
762 int flags ATTRIBUTE_UNUSED,
763 bool *no_add_attrs)
765 tree decl = *node;
766 tree id;
768 gcc_assert (DECL_P (decl));
770 DECL_COMMON (decl) = 1;
771 id = TREE_VALUE (args);
772 if (TREE_CODE (id) != IDENTIFIER_NODE && TREE_CODE (id) != STRING_CST)
774 error ("%qE attribute requires a string constant argument", name);
775 *no_add_attrs = true;
776 return NULL_TREE;
778 return NULL_TREE;
781 /* Part of the low level implementation of DEC Ada pragma Common_Object. */
783 void
784 ia64_vms_output_aligned_decl_common (FILE *file, tree decl, const char *name,
785 unsigned HOST_WIDE_INT size,
786 unsigned int align)
788 tree attr = DECL_ATTRIBUTES (decl);
790 if (attr)
791 attr = lookup_attribute ("common_object", attr);
792 if (attr)
794 tree id = TREE_VALUE (TREE_VALUE (attr));
795 const char *name;
797 if (TREE_CODE (id) == IDENTIFIER_NODE)
798 name = IDENTIFIER_POINTER (id);
799 else if (TREE_CODE (id) == STRING_CST)
800 name = TREE_STRING_POINTER (id);
801 else
802 abort ();
804 fprintf (file, "\t.vms_common\t\"%s\",", name);
806 else
807 fprintf (file, "%s", COMMON_ASM_OP);
809 /* Code from elfos.h. */
810 assemble_name (file, name);
811 fprintf (file, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u",
812 size, align / BITS_PER_UNIT);
814 fputc ('\n', file);
817 static void
818 ia64_encode_addr_area (tree decl, rtx symbol)
820 int flags;
822 flags = SYMBOL_REF_FLAGS (symbol);
823 switch (ia64_get_addr_area (decl))
825 case ADDR_AREA_NORMAL: break;
826 case ADDR_AREA_SMALL: flags |= SYMBOL_FLAG_SMALL_ADDR; break;
827 default: gcc_unreachable ();
829 SYMBOL_REF_FLAGS (symbol) = flags;
832 static void
833 ia64_encode_section_info (tree decl, rtx rtl, int first)
835 default_encode_section_info (decl, rtl, first);
837 /* Careful not to prod global register variables. */
838 if (TREE_CODE (decl) == VAR_DECL
839 && GET_CODE (DECL_RTL (decl)) == MEM
840 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF
841 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
842 ia64_encode_addr_area (decl, XEXP (rtl, 0));
845 /* Return 1 if the operands of a move are ok. */
848 ia64_move_ok (rtx dst, rtx src)
850 /* If we're under init_recog_no_volatile, we'll not be able to use
851 memory_operand. So check the code directly and don't worry about
852 the validity of the underlying address, which should have been
853 checked elsewhere anyway. */
854 if (GET_CODE (dst) != MEM)
855 return 1;
856 if (GET_CODE (src) == MEM)
857 return 0;
858 if (register_operand (src, VOIDmode))
859 return 1;
861 /* Otherwise, this must be a constant, and that either 0 or 0.0 or 1.0. */
862 if (INTEGRAL_MODE_P (GET_MODE (dst)))
863 return src == const0_rtx;
864 else
865 return satisfies_constraint_G (src);
868 /* Return 1 if the operands are ok for a floating point load pair. */
871 ia64_load_pair_ok (rtx dst, rtx src)
873 /* ??? There is a thinko in the implementation of the "x" constraint and the
874 FP_REGS class. The constraint will also reject (reg f30:TI) so we must
875 also return false for it. */
876 if (GET_CODE (dst) != REG
877 || !(FP_REGNO_P (REGNO (dst)) && FP_REGNO_P (REGNO (dst) + 1)))
878 return 0;
879 if (GET_CODE (src) != MEM || MEM_VOLATILE_P (src))
880 return 0;
881 switch (GET_CODE (XEXP (src, 0)))
883 case REG:
884 case POST_INC:
885 break;
886 case POST_DEC:
887 return 0;
888 case POST_MODIFY:
890 rtx adjust = XEXP (XEXP (XEXP (src, 0), 1), 1);
892 if (GET_CODE (adjust) != CONST_INT
893 || INTVAL (adjust) != GET_MODE_SIZE (GET_MODE (src)))
894 return 0;
896 break;
897 default:
898 abort ();
900 return 1;
904 addp4_optimize_ok (rtx op1, rtx op2)
906 return (basereg_operand (op1, GET_MODE(op1)) !=
907 basereg_operand (op2, GET_MODE(op2)));
910 /* Check if OP is a mask suitable for use with SHIFT in a dep.z instruction.
911 Return the length of the field, or <= 0 on failure. */
914 ia64_depz_field_mask (rtx rop, rtx rshift)
916 unsigned HOST_WIDE_INT op = INTVAL (rop);
917 unsigned HOST_WIDE_INT shift = INTVAL (rshift);
919 /* Get rid of the zero bits we're shifting in. */
920 op >>= shift;
922 /* We must now have a solid block of 1's at bit 0. */
923 return exact_log2 (op + 1);
926 /* Return the TLS model to use for ADDR. */
928 static enum tls_model
929 tls_symbolic_operand_type (rtx addr)
931 enum tls_model tls_kind = TLS_MODEL_NONE;
933 if (GET_CODE (addr) == CONST)
935 if (GET_CODE (XEXP (addr, 0)) == PLUS
936 && GET_CODE (XEXP (XEXP (addr, 0), 0)) == SYMBOL_REF)
937 tls_kind = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (addr, 0), 0));
939 else if (GET_CODE (addr) == SYMBOL_REF)
940 tls_kind = SYMBOL_REF_TLS_MODEL (addr);
942 return tls_kind;
945 /* Returns true if REG (assumed to be a `reg' RTX) is valid for use
946 as a base register. */
948 static inline bool
949 ia64_reg_ok_for_base_p (const_rtx reg, bool strict)
951 if (strict
952 && REGNO_OK_FOR_BASE_P (REGNO (reg)))
953 return true;
954 else if (!strict
955 && (GENERAL_REGNO_P (REGNO (reg))
956 || !HARD_REGISTER_P (reg)))
957 return true;
958 else
959 return false;
962 static bool
963 ia64_legitimate_address_reg (const_rtx reg, bool strict)
965 if ((REG_P (reg) && ia64_reg_ok_for_base_p (reg, strict))
966 || (GET_CODE (reg) == SUBREG && REG_P (XEXP (reg, 0))
967 && ia64_reg_ok_for_base_p (XEXP (reg, 0), strict)))
968 return true;
970 return false;
973 static bool
974 ia64_legitimate_address_disp (const_rtx reg, const_rtx disp, bool strict)
976 if (GET_CODE (disp) == PLUS
977 && rtx_equal_p (reg, XEXP (disp, 0))
978 && (ia64_legitimate_address_reg (XEXP (disp, 1), strict)
979 || (CONST_INT_P (XEXP (disp, 1))
980 && IN_RANGE (INTVAL (XEXP (disp, 1)), -256, 255))))
981 return true;
983 return false;
986 /* Implement TARGET_LEGITIMATE_ADDRESS_P. */
988 static bool
989 ia64_legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
990 rtx x, bool strict)
992 if (ia64_legitimate_address_reg (x, strict))
993 return true;
994 else if ((GET_CODE (x) == POST_INC || GET_CODE (x) == POST_DEC)
995 && ia64_legitimate_address_reg (XEXP (x, 0), strict)
996 && XEXP (x, 0) != arg_pointer_rtx)
997 return true;
998 else if (GET_CODE (x) == POST_MODIFY
999 && ia64_legitimate_address_reg (XEXP (x, 0), strict)
1000 && XEXP (x, 0) != arg_pointer_rtx
1001 && ia64_legitimate_address_disp (XEXP (x, 0), XEXP (x, 1), strict))
1002 return true;
1003 else
1004 return false;
1007 /* Return true if X is a constant that is valid for some immediate
1008 field in an instruction. */
1010 static bool
1011 ia64_legitimate_constant_p (enum machine_mode mode, rtx x)
1013 switch (GET_CODE (x))
1015 case CONST_INT:
1016 case LABEL_REF:
1017 return true;
1019 case CONST_DOUBLE:
1020 if (GET_MODE (x) == VOIDmode || mode == SFmode || mode == DFmode)
1021 return true;
1022 return satisfies_constraint_G (x);
1024 case CONST:
1025 case SYMBOL_REF:
1026 /* ??? Short term workaround for PR 28490. We must make the code here
1027 match the code in ia64_expand_move and move_operand, even though they
1028 are both technically wrong. */
1029 if (tls_symbolic_operand_type (x) == 0)
1031 HOST_WIDE_INT addend = 0;
1032 rtx op = x;
1034 if (GET_CODE (op) == CONST
1035 && GET_CODE (XEXP (op, 0)) == PLUS
1036 && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT)
1038 addend = INTVAL (XEXP (XEXP (op, 0), 1));
1039 op = XEXP (XEXP (op, 0), 0);
1042 if (any_offset_symbol_operand (op, mode)
1043 || function_operand (op, mode))
1044 return true;
1045 if (aligned_offset_symbol_operand (op, mode))
1046 return (addend & 0x3fff) == 0;
1047 return false;
1049 return false;
1051 case CONST_VECTOR:
1052 if (mode == V2SFmode)
1053 return satisfies_constraint_Y (x);
1055 return (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
1056 && GET_MODE_SIZE (mode) <= 8);
1058 default:
1059 return false;
1063 /* Don't allow TLS addresses to get spilled to memory. */
1065 static bool
1066 ia64_cannot_force_const_mem (enum machine_mode mode, rtx x)
1068 if (mode == RFmode)
1069 return true;
1070 return tls_symbolic_operand_type (x) != 0;
1073 /* Expand a symbolic constant load. */
1075 bool
1076 ia64_expand_load_address (rtx dest, rtx src)
1078 gcc_assert (GET_CODE (dest) == REG);
1080 /* ILP32 mode still loads 64-bits of data from the GOT. This avoids
1081 having to pointer-extend the value afterward. Other forms of address
1082 computation below are also more natural to compute as 64-bit quantities.
1083 If we've been given an SImode destination register, change it. */
1084 if (GET_MODE (dest) != Pmode)
1085 dest = gen_rtx_REG_offset (dest, Pmode, REGNO (dest),
1086 byte_lowpart_offset (Pmode, GET_MODE (dest)));
1088 if (TARGET_NO_PIC)
1089 return false;
1090 if (small_addr_symbolic_operand (src, VOIDmode))
1091 return false;
1093 if (TARGET_AUTO_PIC)
1094 emit_insn (gen_load_gprel64 (dest, src));
1095 else if (GET_CODE (src) == SYMBOL_REF && SYMBOL_REF_FUNCTION_P (src))
1096 emit_insn (gen_load_fptr (dest, src));
1097 else if (sdata_symbolic_operand (src, VOIDmode))
1098 emit_insn (gen_load_gprel (dest, src));
1099 else
1101 HOST_WIDE_INT addend = 0;
1102 rtx tmp;
1104 /* We did split constant offsets in ia64_expand_move, and we did try
1105 to keep them split in move_operand, but we also allowed reload to
1106 rematerialize arbitrary constants rather than spill the value to
1107 the stack and reload it. So we have to be prepared here to split
1108 them apart again. */
1109 if (GET_CODE (src) == CONST)
1111 HOST_WIDE_INT hi, lo;
1113 hi = INTVAL (XEXP (XEXP (src, 0), 1));
1114 lo = ((hi & 0x3fff) ^ 0x2000) - 0x2000;
1115 hi = hi - lo;
1117 if (lo != 0)
1119 addend = lo;
1120 src = plus_constant (Pmode, XEXP (XEXP (src, 0), 0), hi);
1124 tmp = gen_rtx_HIGH (Pmode, src);
1125 tmp = gen_rtx_PLUS (Pmode, tmp, pic_offset_table_rtx);
1126 emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
1128 tmp = gen_rtx_LO_SUM (Pmode, gen_const_mem (Pmode, dest), src);
1129 emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
1131 if (addend)
1133 tmp = gen_rtx_PLUS (Pmode, dest, GEN_INT (addend));
1134 emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
1138 return true;
1141 static GTY(()) rtx gen_tls_tga;
1142 static rtx
1143 gen_tls_get_addr (void)
1145 if (!gen_tls_tga)
1146 gen_tls_tga = init_one_libfunc ("__tls_get_addr");
1147 return gen_tls_tga;
1150 static GTY(()) rtx thread_pointer_rtx;
1151 static rtx
1152 gen_thread_pointer (void)
1154 if (!thread_pointer_rtx)
1155 thread_pointer_rtx = gen_rtx_REG (Pmode, 13);
1156 return thread_pointer_rtx;
1159 static rtx
1160 ia64_expand_tls_address (enum tls_model tls_kind, rtx op0, rtx op1,
1161 rtx orig_op1, HOST_WIDE_INT addend)
1163 rtx tga_op1, tga_op2, tga_ret, tga_eqv, tmp;
1164 rtx_insn *insns;
1165 rtx orig_op0 = op0;
1166 HOST_WIDE_INT addend_lo, addend_hi;
1168 switch (tls_kind)
1170 case TLS_MODEL_GLOBAL_DYNAMIC:
1171 start_sequence ();
1173 tga_op1 = gen_reg_rtx (Pmode);
1174 emit_insn (gen_load_dtpmod (tga_op1, op1));
1176 tga_op2 = gen_reg_rtx (Pmode);
1177 emit_insn (gen_load_dtprel (tga_op2, op1));
1179 tga_ret = emit_library_call_value (gen_tls_get_addr (), NULL_RTX,
1180 LCT_CONST, Pmode, 2, tga_op1,
1181 Pmode, tga_op2, Pmode);
1183 insns = get_insns ();
1184 end_sequence ();
1186 if (GET_MODE (op0) != Pmode)
1187 op0 = tga_ret;
1188 emit_libcall_block (insns, op0, tga_ret, op1);
1189 break;
1191 case TLS_MODEL_LOCAL_DYNAMIC:
1192 /* ??? This isn't the completely proper way to do local-dynamic
1193 If the call to __tls_get_addr is used only by a single symbol,
1194 then we should (somehow) move the dtprel to the second arg
1195 to avoid the extra add. */
1196 start_sequence ();
1198 tga_op1 = gen_reg_rtx (Pmode);
1199 emit_insn (gen_load_dtpmod (tga_op1, op1));
1201 tga_op2 = const0_rtx;
1203 tga_ret = emit_library_call_value (gen_tls_get_addr (), NULL_RTX,
1204 LCT_CONST, Pmode, 2, tga_op1,
1205 Pmode, tga_op2, Pmode);
1207 insns = get_insns ();
1208 end_sequence ();
1210 tga_eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
1211 UNSPEC_LD_BASE);
1212 tmp = gen_reg_rtx (Pmode);
1213 emit_libcall_block (insns, tmp, tga_ret, tga_eqv);
1215 if (!register_operand (op0, Pmode))
1216 op0 = gen_reg_rtx (Pmode);
1217 if (TARGET_TLS64)
1219 emit_insn (gen_load_dtprel (op0, op1));
1220 emit_insn (gen_adddi3 (op0, tmp, op0));
1222 else
1223 emit_insn (gen_add_dtprel (op0, op1, tmp));
1224 break;
1226 case TLS_MODEL_INITIAL_EXEC:
1227 addend_lo = ((addend & 0x3fff) ^ 0x2000) - 0x2000;
1228 addend_hi = addend - addend_lo;
1230 op1 = plus_constant (Pmode, op1, addend_hi);
1231 addend = addend_lo;
1233 tmp = gen_reg_rtx (Pmode);
1234 emit_insn (gen_load_tprel (tmp, op1));
1236 if (!register_operand (op0, Pmode))
1237 op0 = gen_reg_rtx (Pmode);
1238 emit_insn (gen_adddi3 (op0, tmp, gen_thread_pointer ()));
1239 break;
1241 case TLS_MODEL_LOCAL_EXEC:
1242 if (!register_operand (op0, Pmode))
1243 op0 = gen_reg_rtx (Pmode);
1245 op1 = orig_op1;
1246 addend = 0;
1247 if (TARGET_TLS64)
1249 emit_insn (gen_load_tprel (op0, op1));
1250 emit_insn (gen_adddi3 (op0, op0, gen_thread_pointer ()));
1252 else
1253 emit_insn (gen_add_tprel (op0, op1, gen_thread_pointer ()));
1254 break;
1256 default:
1257 gcc_unreachable ();
1260 if (addend)
1261 op0 = expand_simple_binop (Pmode, PLUS, op0, GEN_INT (addend),
1262 orig_op0, 1, OPTAB_DIRECT);
1263 if (orig_op0 == op0)
1264 return NULL_RTX;
1265 if (GET_MODE (orig_op0) == Pmode)
1266 return op0;
1267 return gen_lowpart (GET_MODE (orig_op0), op0);
1271 ia64_expand_move (rtx op0, rtx op1)
1273 enum machine_mode mode = GET_MODE (op0);
1275 if (!reload_in_progress && !reload_completed && !ia64_move_ok (op0, op1))
1276 op1 = force_reg (mode, op1);
1278 if ((mode == Pmode || mode == ptr_mode) && symbolic_operand (op1, VOIDmode))
1280 HOST_WIDE_INT addend = 0;
1281 enum tls_model tls_kind;
1282 rtx sym = op1;
1284 if (GET_CODE (op1) == CONST
1285 && GET_CODE (XEXP (op1, 0)) == PLUS
1286 && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT)
1288 addend = INTVAL (XEXP (XEXP (op1, 0), 1));
1289 sym = XEXP (XEXP (op1, 0), 0);
1292 tls_kind = tls_symbolic_operand_type (sym);
1293 if (tls_kind)
1294 return ia64_expand_tls_address (tls_kind, op0, sym, op1, addend);
1296 if (any_offset_symbol_operand (sym, mode))
1297 addend = 0;
1298 else if (aligned_offset_symbol_operand (sym, mode))
1300 HOST_WIDE_INT addend_lo, addend_hi;
1302 addend_lo = ((addend & 0x3fff) ^ 0x2000) - 0x2000;
1303 addend_hi = addend - addend_lo;
1305 if (addend_lo != 0)
1307 op1 = plus_constant (mode, sym, addend_hi);
1308 addend = addend_lo;
1310 else
1311 addend = 0;
1313 else
1314 op1 = sym;
1316 if (reload_completed)
1318 /* We really should have taken care of this offset earlier. */
1319 gcc_assert (addend == 0);
1320 if (ia64_expand_load_address (op0, op1))
1321 return NULL_RTX;
1324 if (addend)
1326 rtx subtarget = !can_create_pseudo_p () ? op0 : gen_reg_rtx (mode);
1328 emit_insn (gen_rtx_SET (VOIDmode, subtarget, op1));
1330 op1 = expand_simple_binop (mode, PLUS, subtarget,
1331 GEN_INT (addend), op0, 1, OPTAB_DIRECT);
1332 if (op0 == op1)
1333 return NULL_RTX;
1337 return op1;
1340 /* Split a move from OP1 to OP0 conditional on COND. */
1342 void
1343 ia64_emit_cond_move (rtx op0, rtx op1, rtx cond)
1345 rtx_insn *insn, *first = get_last_insn ();
1347 emit_move_insn (op0, op1);
1349 for (insn = get_last_insn (); insn != first; insn = PREV_INSN (insn))
1350 if (INSN_P (insn))
1351 PATTERN (insn) = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond),
1352 PATTERN (insn));
1355 /* Split a post-reload TImode or TFmode reference into two DImode
1356 components. This is made extra difficult by the fact that we do
1357 not get any scratch registers to work with, because reload cannot
1358 be prevented from giving us a scratch that overlaps the register
1359 pair involved. So instead, when addressing memory, we tweak the
1360 pointer register up and back down with POST_INCs. Or up and not
1361 back down when we can get away with it.
1363 REVERSED is true when the loads must be done in reversed order
1364 (high word first) for correctness. DEAD is true when the pointer
1365 dies with the second insn we generate and therefore the second
1366 address must not carry a postmodify.
1368 May return an insn which is to be emitted after the moves. */
1370 static rtx
1371 ia64_split_tmode (rtx out[2], rtx in, bool reversed, bool dead)
1373 rtx fixup = 0;
1375 switch (GET_CODE (in))
1377 case REG:
1378 out[reversed] = gen_rtx_REG (DImode, REGNO (in));
1379 out[!reversed] = gen_rtx_REG (DImode, REGNO (in) + 1);
1380 break;
1382 case CONST_INT:
1383 case CONST_DOUBLE:
1384 /* Cannot occur reversed. */
1385 gcc_assert (!reversed);
1387 if (GET_MODE (in) != TFmode)
1388 split_double (in, &out[0], &out[1]);
1389 else
1390 /* split_double does not understand how to split a TFmode
1391 quantity into a pair of DImode constants. */
1393 REAL_VALUE_TYPE r;
1394 unsigned HOST_WIDE_INT p[2];
1395 long l[4]; /* TFmode is 128 bits */
1397 REAL_VALUE_FROM_CONST_DOUBLE (r, in);
1398 real_to_target (l, &r, TFmode);
1400 if (FLOAT_WORDS_BIG_ENDIAN)
1402 p[0] = (((unsigned HOST_WIDE_INT) l[0]) << 32) + l[1];
1403 p[1] = (((unsigned HOST_WIDE_INT) l[2]) << 32) + l[3];
1405 else
1407 p[0] = (((unsigned HOST_WIDE_INT) l[1]) << 32) + l[0];
1408 p[1] = (((unsigned HOST_WIDE_INT) l[3]) << 32) + l[2];
1410 out[0] = GEN_INT (p[0]);
1411 out[1] = GEN_INT (p[1]);
1413 break;
1415 case MEM:
1417 rtx base = XEXP (in, 0);
1418 rtx offset;
1420 switch (GET_CODE (base))
1422 case REG:
1423 if (!reversed)
1425 out[0] = adjust_automodify_address
1426 (in, DImode, gen_rtx_POST_INC (Pmode, base), 0);
1427 out[1] = adjust_automodify_address
1428 (in, DImode, dead ? 0 : gen_rtx_POST_DEC (Pmode, base), 8);
1430 else
1432 /* Reversal requires a pre-increment, which can only
1433 be done as a separate insn. */
1434 emit_insn (gen_adddi3 (base, base, GEN_INT (8)));
1435 out[0] = adjust_automodify_address
1436 (in, DImode, gen_rtx_POST_DEC (Pmode, base), 8);
1437 out[1] = adjust_address (in, DImode, 0);
1439 break;
1441 case POST_INC:
1442 gcc_assert (!reversed && !dead);
1444 /* Just do the increment in two steps. */
1445 out[0] = adjust_automodify_address (in, DImode, 0, 0);
1446 out[1] = adjust_automodify_address (in, DImode, 0, 8);
1447 break;
1449 case POST_DEC:
1450 gcc_assert (!reversed && !dead);
1452 /* Add 8, subtract 24. */
1453 base = XEXP (base, 0);
1454 out[0] = adjust_automodify_address
1455 (in, DImode, gen_rtx_POST_INC (Pmode, base), 0);
1456 out[1] = adjust_automodify_address
1457 (in, DImode,
1458 gen_rtx_POST_MODIFY (Pmode, base,
1459 plus_constant (Pmode, base, -24)),
1461 break;
1463 case POST_MODIFY:
1464 gcc_assert (!reversed && !dead);
1466 /* Extract and adjust the modification. This case is
1467 trickier than the others, because we might have an
1468 index register, or we might have a combined offset that
1469 doesn't fit a signed 9-bit displacement field. We can
1470 assume the incoming expression is already legitimate. */
1471 offset = XEXP (base, 1);
1472 base = XEXP (base, 0);
1474 out[0] = adjust_automodify_address
1475 (in, DImode, gen_rtx_POST_INC (Pmode, base), 0);
1477 if (GET_CODE (XEXP (offset, 1)) == REG)
1479 /* Can't adjust the postmodify to match. Emit the
1480 original, then a separate addition insn. */
1481 out[1] = adjust_automodify_address (in, DImode, 0, 8);
1482 fixup = gen_adddi3 (base, base, GEN_INT (-8));
1484 else
1486 gcc_assert (GET_CODE (XEXP (offset, 1)) == CONST_INT);
1487 if (INTVAL (XEXP (offset, 1)) < -256 + 8)
1489 /* Again the postmodify cannot be made to match,
1490 but in this case it's more efficient to get rid
1491 of the postmodify entirely and fix up with an
1492 add insn. */
1493 out[1] = adjust_automodify_address (in, DImode, base, 8);
1494 fixup = gen_adddi3
1495 (base, base, GEN_INT (INTVAL (XEXP (offset, 1)) - 8));
1497 else
1499 /* Combined offset still fits in the displacement field.
1500 (We cannot overflow it at the high end.) */
1501 out[1] = adjust_automodify_address
1502 (in, DImode, gen_rtx_POST_MODIFY
1503 (Pmode, base, gen_rtx_PLUS
1504 (Pmode, base,
1505 GEN_INT (INTVAL (XEXP (offset, 1)) - 8))),
1509 break;
1511 default:
1512 gcc_unreachable ();
1514 break;
1517 default:
1518 gcc_unreachable ();
1521 return fixup;
1524 /* Split a TImode or TFmode move instruction after reload.
1525 This is used by *movtf_internal and *movti_internal. */
1526 void
1527 ia64_split_tmode_move (rtx operands[])
1529 rtx in[2], out[2], insn;
1530 rtx fixup[2];
1531 bool dead = false;
1532 bool reversed = false;
1534 /* It is possible for reload to decide to overwrite a pointer with
1535 the value it points to. In that case we have to do the loads in
1536 the appropriate order so that the pointer is not destroyed too
1537 early. Also we must not generate a postmodify for that second
1538 load, or rws_access_regno will die. And we must not generate a
1539 postmodify for the second load if the destination register
1540 overlaps with the base register. */
1541 if (GET_CODE (operands[1]) == MEM
1542 && reg_overlap_mentioned_p (operands[0], operands[1]))
1544 rtx base = XEXP (operands[1], 0);
1545 while (GET_CODE (base) != REG)
1546 base = XEXP (base, 0);
1548 if (REGNO (base) == REGNO (operands[0]))
1549 reversed = true;
1551 if (refers_to_regno_p (REGNO (operands[0]),
1552 REGNO (operands[0])+2,
1553 base, 0))
1554 dead = true;
1556 /* Another reason to do the moves in reversed order is if the first
1557 element of the target register pair is also the second element of
1558 the source register pair. */
1559 if (GET_CODE (operands[0]) == REG && GET_CODE (operands[1]) == REG
1560 && REGNO (operands[0]) == REGNO (operands[1]) + 1)
1561 reversed = true;
1563 fixup[0] = ia64_split_tmode (in, operands[1], reversed, dead);
1564 fixup[1] = ia64_split_tmode (out, operands[0], reversed, dead);
1566 #define MAYBE_ADD_REG_INC_NOTE(INSN, EXP) \
1567 if (GET_CODE (EXP) == MEM \
1568 && (GET_CODE (XEXP (EXP, 0)) == POST_MODIFY \
1569 || GET_CODE (XEXP (EXP, 0)) == POST_INC \
1570 || GET_CODE (XEXP (EXP, 0)) == POST_DEC)) \
1571 add_reg_note (insn, REG_INC, XEXP (XEXP (EXP, 0), 0))
1573 insn = emit_insn (gen_rtx_SET (VOIDmode, out[0], in[0]));
1574 MAYBE_ADD_REG_INC_NOTE (insn, in[0]);
1575 MAYBE_ADD_REG_INC_NOTE (insn, out[0]);
1577 insn = emit_insn (gen_rtx_SET (VOIDmode, out[1], in[1]));
1578 MAYBE_ADD_REG_INC_NOTE (insn, in[1]);
1579 MAYBE_ADD_REG_INC_NOTE (insn, out[1]);
1581 if (fixup[0])
1582 emit_insn (fixup[0]);
1583 if (fixup[1])
1584 emit_insn (fixup[1]);
1586 #undef MAYBE_ADD_REG_INC_NOTE
1589 /* ??? Fixing GR->FR XFmode moves during reload is hard. You need to go
1590 through memory plus an extra GR scratch register. Except that you can
1591 either get the first from SECONDARY_MEMORY_NEEDED or the second from
1592 SECONDARY_RELOAD_CLASS, but not both.
1594 We got into problems in the first place by allowing a construct like
1595 (subreg:XF (reg:TI)), which we got from a union containing a long double.
1596 This solution attempts to prevent this situation from occurring. When
1597 we see something like the above, we spill the inner register to memory. */
1599 static rtx
1600 spill_xfmode_rfmode_operand (rtx in, int force, enum machine_mode mode)
1602 if (GET_CODE (in) == SUBREG
1603 && GET_MODE (SUBREG_REG (in)) == TImode
1604 && GET_CODE (SUBREG_REG (in)) == REG)
1606 rtx memt = assign_stack_temp (TImode, 16);
1607 emit_move_insn (memt, SUBREG_REG (in));
1608 return adjust_address (memt, mode, 0);
1610 else if (force && GET_CODE (in) == REG)
1612 rtx memx = assign_stack_temp (mode, 16);
1613 emit_move_insn (memx, in);
1614 return memx;
1616 else
1617 return in;
1620 /* Expand the movxf or movrf pattern (MODE says which) with the given
1621 OPERANDS, returning true if the pattern should then invoke
1622 DONE. */
1624 bool
1625 ia64_expand_movxf_movrf (enum machine_mode mode, rtx operands[])
1627 rtx op0 = operands[0];
1629 if (GET_CODE (op0) == SUBREG)
1630 op0 = SUBREG_REG (op0);
1632 /* We must support XFmode loads into general registers for stdarg/vararg,
1633 unprototyped calls, and a rare case where a long double is passed as
1634 an argument after a float HFA fills the FP registers. We split them into
1635 DImode loads for convenience. We also need to support XFmode stores
1636 for the last case. This case does not happen for stdarg/vararg routines,
1637 because we do a block store to memory of unnamed arguments. */
1639 if (GET_CODE (op0) == REG && GR_REGNO_P (REGNO (op0)))
1641 rtx out[2];
1643 /* We're hoping to transform everything that deals with XFmode
1644 quantities and GR registers early in the compiler. */
1645 gcc_assert (can_create_pseudo_p ());
1647 /* Struct to register can just use TImode instead. */
1648 if ((GET_CODE (operands[1]) == SUBREG
1649 && GET_MODE (SUBREG_REG (operands[1])) == TImode)
1650 || (GET_CODE (operands[1]) == REG
1651 && GR_REGNO_P (REGNO (operands[1]))))
1653 rtx op1 = operands[1];
1655 if (GET_CODE (op1) == SUBREG)
1656 op1 = SUBREG_REG (op1);
1657 else
1658 op1 = gen_rtx_REG (TImode, REGNO (op1));
1660 emit_move_insn (gen_rtx_REG (TImode, REGNO (op0)), op1);
1661 return true;
1664 if (GET_CODE (operands[1]) == CONST_DOUBLE)
1666 /* Don't word-swap when reading in the constant. */
1667 emit_move_insn (gen_rtx_REG (DImode, REGNO (op0)),
1668 operand_subword (operands[1], WORDS_BIG_ENDIAN,
1669 0, mode));
1670 emit_move_insn (gen_rtx_REG (DImode, REGNO (op0) + 1),
1671 operand_subword (operands[1], !WORDS_BIG_ENDIAN,
1672 0, mode));
1673 return true;
1676 /* If the quantity is in a register not known to be GR, spill it. */
1677 if (register_operand (operands[1], mode))
1678 operands[1] = spill_xfmode_rfmode_operand (operands[1], 1, mode);
1680 gcc_assert (GET_CODE (operands[1]) == MEM);
1682 /* Don't word-swap when reading in the value. */
1683 out[0] = gen_rtx_REG (DImode, REGNO (op0));
1684 out[1] = gen_rtx_REG (DImode, REGNO (op0) + 1);
1686 emit_move_insn (out[0], adjust_address (operands[1], DImode, 0));
1687 emit_move_insn (out[1], adjust_address (operands[1], DImode, 8));
1688 return true;
1691 if (GET_CODE (operands[1]) == REG && GR_REGNO_P (REGNO (operands[1])))
1693 /* We're hoping to transform everything that deals with XFmode
1694 quantities and GR registers early in the compiler. */
1695 gcc_assert (can_create_pseudo_p ());
1697 /* Op0 can't be a GR_REG here, as that case is handled above.
1698 If op0 is a register, then we spill op1, so that we now have a
1699 MEM operand. This requires creating an XFmode subreg of a TImode reg
1700 to force the spill. */
1701 if (register_operand (operands[0], mode))
1703 rtx op1 = gen_rtx_REG (TImode, REGNO (operands[1]));
1704 op1 = gen_rtx_SUBREG (mode, op1, 0);
1705 operands[1] = spill_xfmode_rfmode_operand (op1, 0, mode);
1708 else
1710 rtx in[2];
1712 gcc_assert (GET_CODE (operands[0]) == MEM);
1714 /* Don't word-swap when writing out the value. */
1715 in[0] = gen_rtx_REG (DImode, REGNO (operands[1]));
1716 in[1] = gen_rtx_REG (DImode, REGNO (operands[1]) + 1);
1718 emit_move_insn (adjust_address (operands[0], DImode, 0), in[0]);
1719 emit_move_insn (adjust_address (operands[0], DImode, 8), in[1]);
1720 return true;
1724 if (!reload_in_progress && !reload_completed)
1726 operands[1] = spill_xfmode_rfmode_operand (operands[1], 0, mode);
1728 if (GET_MODE (op0) == TImode && GET_CODE (op0) == REG)
1730 rtx memt, memx, in = operands[1];
1731 if (CONSTANT_P (in))
1732 in = validize_mem (force_const_mem (mode, in));
1733 if (GET_CODE (in) == MEM)
1734 memt = adjust_address (in, TImode, 0);
1735 else
1737 memt = assign_stack_temp (TImode, 16);
1738 memx = adjust_address (memt, mode, 0);
1739 emit_move_insn (memx, in);
1741 emit_move_insn (op0, memt);
1742 return true;
1745 if (!ia64_move_ok (operands[0], operands[1]))
1746 operands[1] = force_reg (mode, operands[1]);
1749 return false;
1752 /* Emit comparison instruction if necessary, replacing *EXPR, *OP0, *OP1
1753 with the expression that holds the compare result (in VOIDmode). */
1755 static GTY(()) rtx cmptf_libfunc;
1757 void
1758 ia64_expand_compare (rtx *expr, rtx *op0, rtx *op1)
1760 enum rtx_code code = GET_CODE (*expr);
1761 rtx cmp;
1763 /* If we have a BImode input, then we already have a compare result, and
1764 do not need to emit another comparison. */
1765 if (GET_MODE (*op0) == BImode)
1767 gcc_assert ((code == NE || code == EQ) && *op1 == const0_rtx);
1768 cmp = *op0;
1770 /* HPUX TFmode compare requires a library call to _U_Qfcmp, which takes a
1771 magic number as its third argument, that indicates what to do.
1772 The return value is an integer to be compared against zero. */
1773 else if (TARGET_HPUX && GET_MODE (*op0) == TFmode)
1775 enum qfcmp_magic {
1776 QCMP_INV = 1, /* Raise FP_INVALID on NaNs as a side effect. */
1777 QCMP_UNORD = 2,
1778 QCMP_EQ = 4,
1779 QCMP_LT = 8,
1780 QCMP_GT = 16
1782 int magic;
1783 enum rtx_code ncode;
1784 rtx ret, insns;
1786 gcc_assert (cmptf_libfunc && GET_MODE (*op1) == TFmode);
1787 switch (code)
1789 /* 1 = equal, 0 = not equal. Equality operators do
1790 not raise FP_INVALID when given a NaN operand. */
1791 case EQ: magic = QCMP_EQ; ncode = NE; break;
1792 case NE: magic = QCMP_EQ; ncode = EQ; break;
1793 /* isunordered() from C99. */
1794 case UNORDERED: magic = QCMP_UNORD; ncode = NE; break;
1795 case ORDERED: magic = QCMP_UNORD; ncode = EQ; break;
1796 /* Relational operators raise FP_INVALID when given
1797 a NaN operand. */
1798 case LT: magic = QCMP_LT |QCMP_INV; ncode = NE; break;
1799 case LE: magic = QCMP_LT|QCMP_EQ|QCMP_INV; ncode = NE; break;
1800 case GT: magic = QCMP_GT |QCMP_INV; ncode = NE; break;
1801 case GE: magic = QCMP_GT|QCMP_EQ|QCMP_INV; ncode = NE; break;
1802 /* Unordered relational operators do not raise FP_INVALID
1803 when given a NaN operand. */
1804 case UNLT: magic = QCMP_LT |QCMP_UNORD; ncode = NE; break;
1805 case UNLE: magic = QCMP_LT|QCMP_EQ|QCMP_UNORD; ncode = NE; break;
1806 case UNGT: magic = QCMP_GT |QCMP_UNORD; ncode = NE; break;
1807 case UNGE: magic = QCMP_GT|QCMP_EQ|QCMP_UNORD; ncode = NE; break;
1808 /* Not supported. */
1809 case UNEQ:
1810 case LTGT:
1811 default: gcc_unreachable ();
1814 start_sequence ();
1816 ret = emit_library_call_value (cmptf_libfunc, 0, LCT_CONST, DImode, 3,
1817 *op0, TFmode, *op1, TFmode,
1818 GEN_INT (magic), DImode);
1819 cmp = gen_reg_rtx (BImode);
1820 emit_insn (gen_rtx_SET (VOIDmode, cmp,
1821 gen_rtx_fmt_ee (ncode, BImode,
1822 ret, const0_rtx)));
1824 insns = get_insns ();
1825 end_sequence ();
1827 emit_libcall_block (insns, cmp, cmp,
1828 gen_rtx_fmt_ee (code, BImode, *op0, *op1));
1829 code = NE;
1831 else
1833 cmp = gen_reg_rtx (BImode);
1834 emit_insn (gen_rtx_SET (VOIDmode, cmp,
1835 gen_rtx_fmt_ee (code, BImode, *op0, *op1)));
1836 code = NE;
1839 *expr = gen_rtx_fmt_ee (code, VOIDmode, cmp, const0_rtx);
1840 *op0 = cmp;
1841 *op1 = const0_rtx;
1844 /* Generate an integral vector comparison. Return true if the condition has
1845 been reversed, and so the sense of the comparison should be inverted. */
1847 static bool
1848 ia64_expand_vecint_compare (enum rtx_code code, enum machine_mode mode,
1849 rtx dest, rtx op0, rtx op1)
1851 bool negate = false;
1852 rtx x;
1854 /* Canonicalize the comparison to EQ, GT, GTU. */
1855 switch (code)
1857 case EQ:
1858 case GT:
1859 case GTU:
1860 break;
1862 case NE:
1863 case LE:
1864 case LEU:
1865 code = reverse_condition (code);
1866 negate = true;
1867 break;
1869 case GE:
1870 case GEU:
1871 code = reverse_condition (code);
1872 negate = true;
1873 /* FALLTHRU */
1875 case LT:
1876 case LTU:
1877 code = swap_condition (code);
1878 x = op0, op0 = op1, op1 = x;
1879 break;
1881 default:
1882 gcc_unreachable ();
1885 /* Unsigned parallel compare is not supported by the hardware. Play some
1886 tricks to turn this into a signed comparison against 0. */
1887 if (code == GTU)
1889 switch (mode)
1891 case V2SImode:
1893 rtx t1, t2, mask;
1895 /* Subtract (-(INT MAX) - 1) from both operands to make
1896 them signed. */
1897 mask = GEN_INT (0x80000000);
1898 mask = gen_rtx_CONST_VECTOR (V2SImode, gen_rtvec (2, mask, mask));
1899 mask = force_reg (mode, mask);
1900 t1 = gen_reg_rtx (mode);
1901 emit_insn (gen_subv2si3 (t1, op0, mask));
1902 t2 = gen_reg_rtx (mode);
1903 emit_insn (gen_subv2si3 (t2, op1, mask));
1904 op0 = t1;
1905 op1 = t2;
1906 code = GT;
1908 break;
1910 case V8QImode:
1911 case V4HImode:
1912 /* Perform a parallel unsigned saturating subtraction. */
1913 x = gen_reg_rtx (mode);
1914 emit_insn (gen_rtx_SET (VOIDmode, x,
1915 gen_rtx_US_MINUS (mode, op0, op1)));
1917 code = EQ;
1918 op0 = x;
1919 op1 = CONST0_RTX (mode);
1920 negate = !negate;
1921 break;
1923 default:
1924 gcc_unreachable ();
1928 x = gen_rtx_fmt_ee (code, mode, op0, op1);
1929 emit_insn (gen_rtx_SET (VOIDmode, dest, x));
1931 return negate;
1934 /* Emit an integral vector conditional move. */
1936 void
1937 ia64_expand_vecint_cmov (rtx operands[])
1939 enum machine_mode mode = GET_MODE (operands[0]);
1940 enum rtx_code code = GET_CODE (operands[3]);
1941 bool negate;
1942 rtx cmp, x, ot, of;
1944 cmp = gen_reg_rtx (mode);
1945 negate = ia64_expand_vecint_compare (code, mode, cmp,
1946 operands[4], operands[5]);
1948 ot = operands[1+negate];
1949 of = operands[2-negate];
1951 if (ot == CONST0_RTX (mode))
1953 if (of == CONST0_RTX (mode))
1955 emit_move_insn (operands[0], ot);
1956 return;
1959 x = gen_rtx_NOT (mode, cmp);
1960 x = gen_rtx_AND (mode, x, of);
1961 emit_insn (gen_rtx_SET (VOIDmode, operands[0], x));
1963 else if (of == CONST0_RTX (mode))
1965 x = gen_rtx_AND (mode, cmp, ot);
1966 emit_insn (gen_rtx_SET (VOIDmode, operands[0], x));
1968 else
1970 rtx t, f;
1972 t = gen_reg_rtx (mode);
1973 x = gen_rtx_AND (mode, cmp, operands[1+negate]);
1974 emit_insn (gen_rtx_SET (VOIDmode, t, x));
1976 f = gen_reg_rtx (mode);
1977 x = gen_rtx_NOT (mode, cmp);
1978 x = gen_rtx_AND (mode, x, operands[2-negate]);
1979 emit_insn (gen_rtx_SET (VOIDmode, f, x));
1981 x = gen_rtx_IOR (mode, t, f);
1982 emit_insn (gen_rtx_SET (VOIDmode, operands[0], x));
1986 /* Emit an integral vector min or max operation. Return true if all done. */
1988 bool
1989 ia64_expand_vecint_minmax (enum rtx_code code, enum machine_mode mode,
1990 rtx operands[])
1992 rtx xops[6];
1994 /* These four combinations are supported directly. */
1995 if (mode == V8QImode && (code == UMIN || code == UMAX))
1996 return false;
1997 if (mode == V4HImode && (code == SMIN || code == SMAX))
1998 return false;
2000 /* This combination can be implemented with only saturating subtraction. */
2001 if (mode == V4HImode && code == UMAX)
2003 rtx x, tmp = gen_reg_rtx (mode);
2005 x = gen_rtx_US_MINUS (mode, operands[1], operands[2]);
2006 emit_insn (gen_rtx_SET (VOIDmode, tmp, x));
2008 emit_insn (gen_addv4hi3 (operands[0], tmp, operands[2]));
2009 return true;
2012 /* Everything else implemented via vector comparisons. */
2013 xops[0] = operands[0];
2014 xops[4] = xops[1] = operands[1];
2015 xops[5] = xops[2] = operands[2];
2017 switch (code)
2019 case UMIN:
2020 code = LTU;
2021 break;
2022 case UMAX:
2023 code = GTU;
2024 break;
2025 case SMIN:
2026 code = LT;
2027 break;
2028 case SMAX:
2029 code = GT;
2030 break;
2031 default:
2032 gcc_unreachable ();
2034 xops[3] = gen_rtx_fmt_ee (code, VOIDmode, operands[1], operands[2]);
2036 ia64_expand_vecint_cmov (xops);
2037 return true;
2040 /* The vectors LO and HI each contain N halves of a double-wide vector.
2041 Reassemble either the first N/2 or the second N/2 elements. */
2043 void
2044 ia64_unpack_assemble (rtx out, rtx lo, rtx hi, bool highp)
2046 enum machine_mode vmode = GET_MODE (lo);
2047 unsigned int i, high, nelt = GET_MODE_NUNITS (vmode);
2048 struct expand_vec_perm_d d;
2049 bool ok;
2051 d.target = gen_lowpart (vmode, out);
2052 d.op0 = (TARGET_BIG_ENDIAN ? hi : lo);
2053 d.op1 = (TARGET_BIG_ENDIAN ? lo : hi);
2054 d.vmode = vmode;
2055 d.nelt = nelt;
2056 d.one_operand_p = false;
2057 d.testing_p = false;
2059 high = (highp ? nelt / 2 : 0);
2060 for (i = 0; i < nelt / 2; ++i)
2062 d.perm[i * 2] = i + high;
2063 d.perm[i * 2 + 1] = i + high + nelt;
2066 ok = ia64_expand_vec_perm_const_1 (&d);
2067 gcc_assert (ok);
2070 /* Return a vector of the sign-extension of VEC. */
2072 static rtx
2073 ia64_unpack_sign (rtx vec, bool unsignedp)
2075 enum machine_mode mode = GET_MODE (vec);
2076 rtx zero = CONST0_RTX (mode);
2078 if (unsignedp)
2079 return zero;
2080 else
2082 rtx sign = gen_reg_rtx (mode);
2083 bool neg;
2085 neg = ia64_expand_vecint_compare (LT, mode, sign, vec, zero);
2086 gcc_assert (!neg);
2088 return sign;
2092 /* Emit an integral vector unpack operation. */
2094 void
2095 ia64_expand_unpack (rtx operands[3], bool unsignedp, bool highp)
2097 rtx sign = ia64_unpack_sign (operands[1], unsignedp);
2098 ia64_unpack_assemble (operands[0], operands[1], sign, highp);
2101 /* Emit an integral vector widening sum operations. */
2103 void
2104 ia64_expand_widen_sum (rtx operands[3], bool unsignedp)
2106 enum machine_mode wmode;
2107 rtx l, h, t, sign;
2109 sign = ia64_unpack_sign (operands[1], unsignedp);
2111 wmode = GET_MODE (operands[0]);
2112 l = gen_reg_rtx (wmode);
2113 h = gen_reg_rtx (wmode);
2115 ia64_unpack_assemble (l, operands[1], sign, false);
2116 ia64_unpack_assemble (h, operands[1], sign, true);
2118 t = expand_binop (wmode, add_optab, l, operands[2], NULL, 0, OPTAB_DIRECT);
2119 t = expand_binop (wmode, add_optab, h, t, operands[0], 0, OPTAB_DIRECT);
2120 if (t != operands[0])
2121 emit_move_insn (operands[0], t);
2124 /* Emit the appropriate sequence for a call. */
2126 void
2127 ia64_expand_call (rtx retval, rtx addr, rtx nextarg ATTRIBUTE_UNUSED,
2128 int sibcall_p)
2130 rtx insn, b0;
2132 addr = XEXP (addr, 0);
2133 addr = convert_memory_address (DImode, addr);
2134 b0 = gen_rtx_REG (DImode, R_BR (0));
2136 /* ??? Should do this for functions known to bind local too. */
2137 if (TARGET_NO_PIC || TARGET_AUTO_PIC)
2139 if (sibcall_p)
2140 insn = gen_sibcall_nogp (addr);
2141 else if (! retval)
2142 insn = gen_call_nogp (addr, b0);
2143 else
2144 insn = gen_call_value_nogp (retval, addr, b0);
2145 insn = emit_call_insn (insn);
2147 else
2149 if (sibcall_p)
2150 insn = gen_sibcall_gp (addr);
2151 else if (! retval)
2152 insn = gen_call_gp (addr, b0);
2153 else
2154 insn = gen_call_value_gp (retval, addr, b0);
2155 insn = emit_call_insn (insn);
2157 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
2160 if (sibcall_p)
2161 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), b0);
2163 if (TARGET_ABI_OPEN_VMS)
2164 use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
2165 gen_rtx_REG (DImode, GR_REG (25)));
2168 static void
2169 reg_emitted (enum ia64_frame_regs r)
2171 if (emitted_frame_related_regs[r] == 0)
2172 emitted_frame_related_regs[r] = current_frame_info.r[r];
2173 else
2174 gcc_assert (emitted_frame_related_regs[r] == current_frame_info.r[r]);
2177 static int
2178 get_reg (enum ia64_frame_regs r)
2180 reg_emitted (r);
2181 return current_frame_info.r[r];
2184 static bool
2185 is_emitted (int regno)
2187 unsigned int r;
2189 for (r = reg_fp; r < number_of_ia64_frame_regs; r++)
2190 if (emitted_frame_related_regs[r] == regno)
2191 return true;
2192 return false;
2195 void
2196 ia64_reload_gp (void)
2198 rtx tmp;
2200 if (current_frame_info.r[reg_save_gp])
2202 tmp = gen_rtx_REG (DImode, get_reg (reg_save_gp));
2204 else
2206 HOST_WIDE_INT offset;
2207 rtx offset_r;
2209 offset = (current_frame_info.spill_cfa_off
2210 + current_frame_info.spill_size);
2211 if (frame_pointer_needed)
2213 tmp = hard_frame_pointer_rtx;
2214 offset = -offset;
2216 else
2218 tmp = stack_pointer_rtx;
2219 offset = current_frame_info.total_size - offset;
2222 offset_r = GEN_INT (offset);
2223 if (satisfies_constraint_I (offset_r))
2224 emit_insn (gen_adddi3 (pic_offset_table_rtx, tmp, offset_r));
2225 else
2227 emit_move_insn (pic_offset_table_rtx, offset_r);
2228 emit_insn (gen_adddi3 (pic_offset_table_rtx,
2229 pic_offset_table_rtx, tmp));
2232 tmp = gen_rtx_MEM (DImode, pic_offset_table_rtx);
2235 emit_move_insn (pic_offset_table_rtx, tmp);
2238 void
2239 ia64_split_call (rtx retval, rtx addr, rtx retaddr, rtx scratch_r,
2240 rtx scratch_b, int noreturn_p, int sibcall_p)
2242 rtx insn;
2243 bool is_desc = false;
2245 /* If we find we're calling through a register, then we're actually
2246 calling through a descriptor, so load up the values. */
2247 if (REG_P (addr) && GR_REGNO_P (REGNO (addr)))
2249 rtx tmp;
2250 bool addr_dead_p;
2252 /* ??? We are currently constrained to *not* use peep2, because
2253 we can legitimately change the global lifetime of the GP
2254 (in the form of killing where previously live). This is
2255 because a call through a descriptor doesn't use the previous
2256 value of the GP, while a direct call does, and we do not
2257 commit to either form until the split here.
2259 That said, this means that we lack precise life info for
2260 whether ADDR is dead after this call. This is not terribly
2261 important, since we can fix things up essentially for free
2262 with the POST_DEC below, but it's nice to not use it when we
2263 can immediately tell it's not necessary. */
2264 addr_dead_p = ((noreturn_p || sibcall_p
2265 || TEST_HARD_REG_BIT (regs_invalidated_by_call,
2266 REGNO (addr)))
2267 && !FUNCTION_ARG_REGNO_P (REGNO (addr)));
2269 /* Load the code address into scratch_b. */
2270 tmp = gen_rtx_POST_INC (Pmode, addr);
2271 tmp = gen_rtx_MEM (Pmode, tmp);
2272 emit_move_insn (scratch_r, tmp);
2273 emit_move_insn (scratch_b, scratch_r);
2275 /* Load the GP address. If ADDR is not dead here, then we must
2276 revert the change made above via the POST_INCREMENT. */
2277 if (!addr_dead_p)
2278 tmp = gen_rtx_POST_DEC (Pmode, addr);
2279 else
2280 tmp = addr;
2281 tmp = gen_rtx_MEM (Pmode, tmp);
2282 emit_move_insn (pic_offset_table_rtx, tmp);
2284 is_desc = true;
2285 addr = scratch_b;
2288 if (sibcall_p)
2289 insn = gen_sibcall_nogp (addr);
2290 else if (retval)
2291 insn = gen_call_value_nogp (retval, addr, retaddr);
2292 else
2293 insn = gen_call_nogp (addr, retaddr);
2294 emit_call_insn (insn);
2296 if ((!TARGET_CONST_GP || is_desc) && !noreturn_p && !sibcall_p)
2297 ia64_reload_gp ();
2300 /* Expand an atomic operation. We want to perform MEM <CODE>= VAL atomically.
2302 This differs from the generic code in that we know about the zero-extending
2303 properties of cmpxchg, and the zero-extending requirements of ar.ccv. We
2304 also know that ld.acq+cmpxchg.rel equals a full barrier.
2306 The loop we want to generate looks like
2308 cmp_reg = mem;
2309 label:
2310 old_reg = cmp_reg;
2311 new_reg = cmp_reg op val;
2312 cmp_reg = compare-and-swap(mem, old_reg, new_reg)
2313 if (cmp_reg != old_reg)
2314 goto label;
2316 Note that we only do the plain load from memory once. Subsequent
2317 iterations use the value loaded by the compare-and-swap pattern. */
2319 void
2320 ia64_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
2321 rtx old_dst, rtx new_dst, enum memmodel model)
2323 enum machine_mode mode = GET_MODE (mem);
2324 rtx old_reg, new_reg, cmp_reg, ar_ccv, label;
2325 enum insn_code icode;
2327 /* Special case for using fetchadd. */
2328 if ((mode == SImode || mode == DImode)
2329 && (code == PLUS || code == MINUS)
2330 && fetchadd_operand (val, mode))
2332 if (code == MINUS)
2333 val = GEN_INT (-INTVAL (val));
2335 if (!old_dst)
2336 old_dst = gen_reg_rtx (mode);
2338 switch (model)
2340 case MEMMODEL_ACQ_REL:
2341 case MEMMODEL_SEQ_CST:
2342 emit_insn (gen_memory_barrier ());
2343 /* FALLTHRU */
2344 case MEMMODEL_RELAXED:
2345 case MEMMODEL_ACQUIRE:
2346 case MEMMODEL_CONSUME:
2347 if (mode == SImode)
2348 icode = CODE_FOR_fetchadd_acq_si;
2349 else
2350 icode = CODE_FOR_fetchadd_acq_di;
2351 break;
2352 case MEMMODEL_RELEASE:
2353 if (mode == SImode)
2354 icode = CODE_FOR_fetchadd_rel_si;
2355 else
2356 icode = CODE_FOR_fetchadd_rel_di;
2357 break;
2359 default:
2360 gcc_unreachable ();
2363 emit_insn (GEN_FCN (icode) (old_dst, mem, val));
2365 if (new_dst)
2367 new_reg = expand_simple_binop (mode, PLUS, old_dst, val, new_dst,
2368 true, OPTAB_WIDEN);
2369 if (new_reg != new_dst)
2370 emit_move_insn (new_dst, new_reg);
2372 return;
2375 /* Because of the volatile mem read, we get an ld.acq, which is the
2376 front half of the full barrier. The end half is the cmpxchg.rel.
2377 For relaxed and release memory models, we don't need this. But we
2378 also don't bother trying to prevent it either. */
2379 gcc_assert (model == MEMMODEL_RELAXED
2380 || model == MEMMODEL_RELEASE
2381 || MEM_VOLATILE_P (mem));
2383 old_reg = gen_reg_rtx (DImode);
2384 cmp_reg = gen_reg_rtx (DImode);
2385 label = gen_label_rtx ();
2387 if (mode != DImode)
2389 val = simplify_gen_subreg (DImode, val, mode, 0);
2390 emit_insn (gen_extend_insn (cmp_reg, mem, DImode, mode, 1));
2392 else
2393 emit_move_insn (cmp_reg, mem);
2395 emit_label (label);
2397 ar_ccv = gen_rtx_REG (DImode, AR_CCV_REGNUM);
2398 emit_move_insn (old_reg, cmp_reg);
2399 emit_move_insn (ar_ccv, cmp_reg);
2401 if (old_dst)
2402 emit_move_insn (old_dst, gen_lowpart (mode, cmp_reg));
2404 new_reg = cmp_reg;
2405 if (code == NOT)
2407 new_reg = expand_simple_binop (DImode, AND, new_reg, val, NULL_RTX,
2408 true, OPTAB_DIRECT);
2409 new_reg = expand_simple_unop (DImode, code, new_reg, NULL_RTX, true);
2411 else
2412 new_reg = expand_simple_binop (DImode, code, new_reg, val, NULL_RTX,
2413 true, OPTAB_DIRECT);
2415 if (mode != DImode)
2416 new_reg = gen_lowpart (mode, new_reg);
2417 if (new_dst)
2418 emit_move_insn (new_dst, new_reg);
2420 switch (model)
2422 case MEMMODEL_RELAXED:
2423 case MEMMODEL_ACQUIRE:
2424 case MEMMODEL_CONSUME:
2425 switch (mode)
2427 case QImode: icode = CODE_FOR_cmpxchg_acq_qi; break;
2428 case HImode: icode = CODE_FOR_cmpxchg_acq_hi; break;
2429 case SImode: icode = CODE_FOR_cmpxchg_acq_si; break;
2430 case DImode: icode = CODE_FOR_cmpxchg_acq_di; break;
2431 default:
2432 gcc_unreachable ();
2434 break;
2436 case MEMMODEL_RELEASE:
2437 case MEMMODEL_ACQ_REL:
2438 case MEMMODEL_SEQ_CST:
2439 switch (mode)
2441 case QImode: icode = CODE_FOR_cmpxchg_rel_qi; break;
2442 case HImode: icode = CODE_FOR_cmpxchg_rel_hi; break;
2443 case SImode: icode = CODE_FOR_cmpxchg_rel_si; break;
2444 case DImode: icode = CODE_FOR_cmpxchg_rel_di; break;
2445 default:
2446 gcc_unreachable ();
2448 break;
2450 default:
2451 gcc_unreachable ();
2454 emit_insn (GEN_FCN (icode) (cmp_reg, mem, ar_ccv, new_reg));
2456 emit_cmp_and_jump_insns (cmp_reg, old_reg, NE, NULL, DImode, true, label);
2459 /* Begin the assembly file. */
2461 static void
2462 ia64_file_start (void)
2464 default_file_start ();
2465 emit_safe_across_calls ();
2468 void
2469 emit_safe_across_calls (void)
2471 unsigned int rs, re;
2472 int out_state;
2474 rs = 1;
2475 out_state = 0;
2476 while (1)
2478 while (rs < 64 && call_used_regs[PR_REG (rs)])
2479 rs++;
2480 if (rs >= 64)
2481 break;
2482 for (re = rs + 1; re < 64 && ! call_used_regs[PR_REG (re)]; re++)
2483 continue;
2484 if (out_state == 0)
2486 fputs ("\t.pred.safe_across_calls ", asm_out_file);
2487 out_state = 1;
2489 else
2490 fputc (',', asm_out_file);
2491 if (re == rs + 1)
2492 fprintf (asm_out_file, "p%u", rs);
2493 else
2494 fprintf (asm_out_file, "p%u-p%u", rs, re - 1);
2495 rs = re + 1;
2497 if (out_state)
2498 fputc ('\n', asm_out_file);
2501 /* Globalize a declaration. */
2503 static void
2504 ia64_globalize_decl_name (FILE * stream, tree decl)
2506 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2507 tree version_attr = lookup_attribute ("version_id", DECL_ATTRIBUTES (decl));
2508 if (version_attr)
2510 tree v = TREE_VALUE (TREE_VALUE (version_attr));
2511 const char *p = TREE_STRING_POINTER (v);
2512 fprintf (stream, "\t.alias %s#, \"%s{%s}\"\n", name, name, p);
2514 targetm.asm_out.globalize_label (stream, name);
2515 if (TREE_CODE (decl) == FUNCTION_DECL)
2516 ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "function");
2519 /* Helper function for ia64_compute_frame_size: find an appropriate general
2520 register to spill some special register to. SPECIAL_SPILL_MASK contains
2521 bits in GR0 to GR31 that have already been allocated by this routine.
2522 TRY_LOCALS is true if we should attempt to locate a local regnum. */
2524 static int
2525 find_gr_spill (enum ia64_frame_regs r, int try_locals)
2527 int regno;
2529 if (emitted_frame_related_regs[r] != 0)
2531 regno = emitted_frame_related_regs[r];
2532 if (regno >= LOC_REG (0) && regno < LOC_REG (80 - frame_pointer_needed)
2533 && current_frame_info.n_local_regs < regno - LOC_REG (0) + 1)
2534 current_frame_info.n_local_regs = regno - LOC_REG (0) + 1;
2535 else if (crtl->is_leaf
2536 && regno >= GR_REG (1) && regno <= GR_REG (31))
2537 current_frame_info.gr_used_mask |= 1 << regno;
2539 return regno;
2542 /* If this is a leaf function, first try an otherwise unused
2543 call-clobbered register. */
2544 if (crtl->is_leaf)
2546 for (regno = GR_REG (1); regno <= GR_REG (31); regno++)
2547 if (! df_regs_ever_live_p (regno)
2548 && call_used_regs[regno]
2549 && ! fixed_regs[regno]
2550 && ! global_regs[regno]
2551 && ((current_frame_info.gr_used_mask >> regno) & 1) == 0
2552 && ! is_emitted (regno))
2554 current_frame_info.gr_used_mask |= 1 << regno;
2555 return regno;
2559 if (try_locals)
2561 regno = current_frame_info.n_local_regs;
2562 /* If there is a frame pointer, then we can't use loc79, because
2563 that is HARD_FRAME_POINTER_REGNUM. In particular, see the
2564 reg_name switching code in ia64_expand_prologue. */
2565 while (regno < (80 - frame_pointer_needed))
2566 if (! is_emitted (LOC_REG (regno++)))
2568 current_frame_info.n_local_regs = regno;
2569 return LOC_REG (regno - 1);
2573 /* Failed to find a general register to spill to. Must use stack. */
2574 return 0;
2577 /* In order to make for nice schedules, we try to allocate every temporary
2578 to a different register. We must of course stay away from call-saved,
2579 fixed, and global registers. We must also stay away from registers
2580 allocated in current_frame_info.gr_used_mask, since those include regs
2581 used all through the prologue.
2583 Any register allocated here must be used immediately. The idea is to
2584 aid scheduling, not to solve data flow problems. */
2586 static int last_scratch_gr_reg;
2588 static int
2589 next_scratch_gr_reg (void)
2591 int i, regno;
2593 for (i = 0; i < 32; ++i)
2595 regno = (last_scratch_gr_reg + i + 1) & 31;
2596 if (call_used_regs[regno]
2597 && ! fixed_regs[regno]
2598 && ! global_regs[regno]
2599 && ((current_frame_info.gr_used_mask >> regno) & 1) == 0)
2601 last_scratch_gr_reg = regno;
2602 return regno;
2606 /* There must be _something_ available. */
2607 gcc_unreachable ();
2610 /* Helper function for ia64_compute_frame_size, called through
2611 diddle_return_value. Mark REG in current_frame_info.gr_used_mask. */
2613 static void
2614 mark_reg_gr_used_mask (rtx reg, void *data ATTRIBUTE_UNUSED)
2616 unsigned int regno = REGNO (reg);
2617 if (regno < 32)
2619 unsigned int i, n = hard_regno_nregs[regno][GET_MODE (reg)];
2620 for (i = 0; i < n; ++i)
2621 current_frame_info.gr_used_mask |= 1 << (regno + i);
2626 /* Returns the number of bytes offset between the frame pointer and the stack
2627 pointer for the current function. SIZE is the number of bytes of space
2628 needed for local variables. */
2630 static void
2631 ia64_compute_frame_size (HOST_WIDE_INT size)
2633 HOST_WIDE_INT total_size;
2634 HOST_WIDE_INT spill_size = 0;
2635 HOST_WIDE_INT extra_spill_size = 0;
2636 HOST_WIDE_INT pretend_args_size;
2637 HARD_REG_SET mask;
2638 int n_spilled = 0;
2639 int spilled_gr_p = 0;
2640 int spilled_fr_p = 0;
2641 unsigned int regno;
2642 int min_regno;
2643 int max_regno;
2644 int i;
2646 if (current_frame_info.initialized)
2647 return;
2649 memset (&current_frame_info, 0, sizeof current_frame_info);
2650 CLEAR_HARD_REG_SET (mask);
2652 /* Don't allocate scratches to the return register. */
2653 diddle_return_value (mark_reg_gr_used_mask, NULL);
2655 /* Don't allocate scratches to the EH scratch registers. */
2656 if (cfun->machine->ia64_eh_epilogue_sp)
2657 mark_reg_gr_used_mask (cfun->machine->ia64_eh_epilogue_sp, NULL);
2658 if (cfun->machine->ia64_eh_epilogue_bsp)
2659 mark_reg_gr_used_mask (cfun->machine->ia64_eh_epilogue_bsp, NULL);
2661 /* Static stack checking uses r2 and r3. */
2662 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
2663 current_frame_info.gr_used_mask |= 0xc;
2665 /* Find the size of the register stack frame. We have only 80 local
2666 registers, because we reserve 8 for the inputs and 8 for the
2667 outputs. */
2669 /* Skip HARD_FRAME_POINTER_REGNUM (loc79) when frame_pointer_needed,
2670 since we'll be adjusting that down later. */
2671 regno = LOC_REG (78) + ! frame_pointer_needed;
2672 for (; regno >= LOC_REG (0); regno--)
2673 if (df_regs_ever_live_p (regno) && !is_emitted (regno))
2674 break;
2675 current_frame_info.n_local_regs = regno - LOC_REG (0) + 1;
2677 /* For functions marked with the syscall_linkage attribute, we must mark
2678 all eight input registers as in use, so that locals aren't visible to
2679 the caller. */
2681 if (cfun->machine->n_varargs > 0
2682 || lookup_attribute ("syscall_linkage",
2683 TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
2684 current_frame_info.n_input_regs = 8;
2685 else
2687 for (regno = IN_REG (7); regno >= IN_REG (0); regno--)
2688 if (df_regs_ever_live_p (regno))
2689 break;
2690 current_frame_info.n_input_regs = regno - IN_REG (0) + 1;
2693 for (regno = OUT_REG (7); regno >= OUT_REG (0); regno--)
2694 if (df_regs_ever_live_p (regno))
2695 break;
2696 i = regno - OUT_REG (0) + 1;
2698 #ifndef PROFILE_HOOK
2699 /* When -p profiling, we need one output register for the mcount argument.
2700 Likewise for -a profiling for the bb_init_func argument. For -ax
2701 profiling, we need two output registers for the two bb_init_trace_func
2702 arguments. */
2703 if (crtl->profile)
2704 i = MAX (i, 1);
2705 #endif
2706 current_frame_info.n_output_regs = i;
2708 /* ??? No rotating register support yet. */
2709 current_frame_info.n_rotate_regs = 0;
2711 /* Discover which registers need spilling, and how much room that
2712 will take. Begin with floating point and general registers,
2713 which will always wind up on the stack. */
2715 for (regno = FR_REG (2); regno <= FR_REG (127); regno++)
2716 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2718 SET_HARD_REG_BIT (mask, regno);
2719 spill_size += 16;
2720 n_spilled += 1;
2721 spilled_fr_p = 1;
2724 for (regno = GR_REG (1); regno <= GR_REG (31); regno++)
2725 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2727 SET_HARD_REG_BIT (mask, regno);
2728 spill_size += 8;
2729 n_spilled += 1;
2730 spilled_gr_p = 1;
2733 for (regno = BR_REG (1); regno <= BR_REG (7); regno++)
2734 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2736 SET_HARD_REG_BIT (mask, regno);
2737 spill_size += 8;
2738 n_spilled += 1;
2741 /* Now come all special registers that might get saved in other
2742 general registers. */
2744 if (frame_pointer_needed)
2746 current_frame_info.r[reg_fp] = find_gr_spill (reg_fp, 1);
2747 /* If we did not get a register, then we take LOC79. This is guaranteed
2748 to be free, even if regs_ever_live is already set, because this is
2749 HARD_FRAME_POINTER_REGNUM. This requires incrementing n_local_regs,
2750 as we don't count loc79 above. */
2751 if (current_frame_info.r[reg_fp] == 0)
2753 current_frame_info.r[reg_fp] = LOC_REG (79);
2754 current_frame_info.n_local_regs = LOC_REG (79) - LOC_REG (0) + 1;
2758 if (! crtl->is_leaf)
2760 /* Emit a save of BR0 if we call other functions. Do this even
2761 if this function doesn't return, as EH depends on this to be
2762 able to unwind the stack. */
2763 SET_HARD_REG_BIT (mask, BR_REG (0));
2765 current_frame_info.r[reg_save_b0] = find_gr_spill (reg_save_b0, 1);
2766 if (current_frame_info.r[reg_save_b0] == 0)
2768 extra_spill_size += 8;
2769 n_spilled += 1;
2772 /* Similarly for ar.pfs. */
2773 SET_HARD_REG_BIT (mask, AR_PFS_REGNUM);
2774 current_frame_info.r[reg_save_ar_pfs] = find_gr_spill (reg_save_ar_pfs, 1);
2775 if (current_frame_info.r[reg_save_ar_pfs] == 0)
2777 extra_spill_size += 8;
2778 n_spilled += 1;
2781 /* Similarly for gp. Note that if we're calling setjmp, the stacked
2782 registers are clobbered, so we fall back to the stack. */
2783 current_frame_info.r[reg_save_gp]
2784 = (cfun->calls_setjmp ? 0 : find_gr_spill (reg_save_gp, 1));
2785 if (current_frame_info.r[reg_save_gp] == 0)
2787 SET_HARD_REG_BIT (mask, GR_REG (1));
2788 spill_size += 8;
2789 n_spilled += 1;
2792 else
2794 if (df_regs_ever_live_p (BR_REG (0)) && ! call_used_regs[BR_REG (0)])
2796 SET_HARD_REG_BIT (mask, BR_REG (0));
2797 extra_spill_size += 8;
2798 n_spilled += 1;
2801 if (df_regs_ever_live_p (AR_PFS_REGNUM))
2803 SET_HARD_REG_BIT (mask, AR_PFS_REGNUM);
2804 current_frame_info.r[reg_save_ar_pfs]
2805 = find_gr_spill (reg_save_ar_pfs, 1);
2806 if (current_frame_info.r[reg_save_ar_pfs] == 0)
2808 extra_spill_size += 8;
2809 n_spilled += 1;
2814 /* Unwind descriptor hackery: things are most efficient if we allocate
2815 consecutive GR save registers for RP, PFS, FP in that order. However,
2816 it is absolutely critical that FP get the only hard register that's
2817 guaranteed to be free, so we allocated it first. If all three did
2818 happen to be allocated hard regs, and are consecutive, rearrange them
2819 into the preferred order now.
2821 If we have already emitted code for any of those registers,
2822 then it's already too late to change. */
2823 min_regno = MIN (current_frame_info.r[reg_fp],
2824 MIN (current_frame_info.r[reg_save_b0],
2825 current_frame_info.r[reg_save_ar_pfs]));
2826 max_regno = MAX (current_frame_info.r[reg_fp],
2827 MAX (current_frame_info.r[reg_save_b0],
2828 current_frame_info.r[reg_save_ar_pfs]));
2829 if (min_regno > 0
2830 && min_regno + 2 == max_regno
2831 && (current_frame_info.r[reg_fp] == min_regno + 1
2832 || current_frame_info.r[reg_save_b0] == min_regno + 1
2833 || current_frame_info.r[reg_save_ar_pfs] == min_regno + 1)
2834 && (emitted_frame_related_regs[reg_save_b0] == 0
2835 || emitted_frame_related_regs[reg_save_b0] == min_regno)
2836 && (emitted_frame_related_regs[reg_save_ar_pfs] == 0
2837 || emitted_frame_related_regs[reg_save_ar_pfs] == min_regno + 1)
2838 && (emitted_frame_related_regs[reg_fp] == 0
2839 || emitted_frame_related_regs[reg_fp] == min_regno + 2))
2841 current_frame_info.r[reg_save_b0] = min_regno;
2842 current_frame_info.r[reg_save_ar_pfs] = min_regno + 1;
2843 current_frame_info.r[reg_fp] = min_regno + 2;
2846 /* See if we need to store the predicate register block. */
2847 for (regno = PR_REG (0); regno <= PR_REG (63); regno++)
2848 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2849 break;
2850 if (regno <= PR_REG (63))
2852 SET_HARD_REG_BIT (mask, PR_REG (0));
2853 current_frame_info.r[reg_save_pr] = find_gr_spill (reg_save_pr, 1);
2854 if (current_frame_info.r[reg_save_pr] == 0)
2856 extra_spill_size += 8;
2857 n_spilled += 1;
2860 /* ??? Mark them all as used so that register renaming and such
2861 are free to use them. */
2862 for (regno = PR_REG (0); regno <= PR_REG (63); regno++)
2863 df_set_regs_ever_live (regno, true);
2866 /* If we're forced to use st8.spill, we're forced to save and restore
2867 ar.unat as well. The check for existing liveness allows inline asm
2868 to touch ar.unat. */
2869 if (spilled_gr_p || cfun->machine->n_varargs
2870 || df_regs_ever_live_p (AR_UNAT_REGNUM))
2872 df_set_regs_ever_live (AR_UNAT_REGNUM, true);
2873 SET_HARD_REG_BIT (mask, AR_UNAT_REGNUM);
2874 current_frame_info.r[reg_save_ar_unat]
2875 = find_gr_spill (reg_save_ar_unat, spill_size == 0);
2876 if (current_frame_info.r[reg_save_ar_unat] == 0)
2878 extra_spill_size += 8;
2879 n_spilled += 1;
2883 if (df_regs_ever_live_p (AR_LC_REGNUM))
2885 SET_HARD_REG_BIT (mask, AR_LC_REGNUM);
2886 current_frame_info.r[reg_save_ar_lc]
2887 = find_gr_spill (reg_save_ar_lc, spill_size == 0);
2888 if (current_frame_info.r[reg_save_ar_lc] == 0)
2890 extra_spill_size += 8;
2891 n_spilled += 1;
2895 /* If we have an odd number of words of pretend arguments written to
2896 the stack, then the FR save area will be unaligned. We round the
2897 size of this area up to keep things 16 byte aligned. */
2898 if (spilled_fr_p)
2899 pretend_args_size = IA64_STACK_ALIGN (crtl->args.pretend_args_size);
2900 else
2901 pretend_args_size = crtl->args.pretend_args_size;
2903 total_size = (spill_size + extra_spill_size + size + pretend_args_size
2904 + crtl->outgoing_args_size);
2905 total_size = IA64_STACK_ALIGN (total_size);
2907 /* We always use the 16-byte scratch area provided by the caller, but
2908 if we are a leaf function, there's no one to which we need to provide
2909 a scratch area. However, if the function allocates dynamic stack space,
2910 the dynamic offset is computed early and contains STACK_POINTER_OFFSET,
2911 so we need to cope. */
2912 if (crtl->is_leaf && !cfun->calls_alloca)
2913 total_size = MAX (0, total_size - 16);
2915 current_frame_info.total_size = total_size;
2916 current_frame_info.spill_cfa_off = pretend_args_size - 16;
2917 current_frame_info.spill_size = spill_size;
2918 current_frame_info.extra_spill_size = extra_spill_size;
2919 COPY_HARD_REG_SET (current_frame_info.mask, mask);
2920 current_frame_info.n_spilled = n_spilled;
2921 current_frame_info.initialized = reload_completed;
2924 /* Worker function for TARGET_CAN_ELIMINATE. */
2926 bool
2927 ia64_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
2929 return (to == BR_REG (0) ? crtl->is_leaf : true);
2932 /* Compute the initial difference between the specified pair of registers. */
2934 HOST_WIDE_INT
2935 ia64_initial_elimination_offset (int from, int to)
2937 HOST_WIDE_INT offset;
2939 ia64_compute_frame_size (get_frame_size ());
2940 switch (from)
2942 case FRAME_POINTER_REGNUM:
2943 switch (to)
2945 case HARD_FRAME_POINTER_REGNUM:
2946 offset = -current_frame_info.total_size;
2947 if (!crtl->is_leaf || cfun->calls_alloca)
2948 offset += 16 + crtl->outgoing_args_size;
2949 break;
2951 case STACK_POINTER_REGNUM:
2952 offset = 0;
2953 if (!crtl->is_leaf || cfun->calls_alloca)
2954 offset += 16 + crtl->outgoing_args_size;
2955 break;
2957 default:
2958 gcc_unreachable ();
2960 break;
2962 case ARG_POINTER_REGNUM:
2963 /* Arguments start above the 16 byte save area, unless stdarg
2964 in which case we store through the 16 byte save area. */
2965 switch (to)
2967 case HARD_FRAME_POINTER_REGNUM:
2968 offset = 16 - crtl->args.pretend_args_size;
2969 break;
2971 case STACK_POINTER_REGNUM:
2972 offset = (current_frame_info.total_size
2973 + 16 - crtl->args.pretend_args_size);
2974 break;
2976 default:
2977 gcc_unreachable ();
2979 break;
2981 default:
2982 gcc_unreachable ();
2985 return offset;
2988 /* If there are more than a trivial number of register spills, we use
2989 two interleaved iterators so that we can get two memory references
2990 per insn group.
2992 In order to simplify things in the prologue and epilogue expanders,
2993 we use helper functions to fix up the memory references after the
2994 fact with the appropriate offsets to a POST_MODIFY memory mode.
2995 The following data structure tracks the state of the two iterators
2996 while insns are being emitted. */
2998 struct spill_fill_data
3000 rtx_insn *init_after; /* point at which to emit initializations */
3001 rtx init_reg[2]; /* initial base register */
3002 rtx iter_reg[2]; /* the iterator registers */
3003 rtx *prev_addr[2]; /* address of last memory use */
3004 rtx_insn *prev_insn[2]; /* the insn corresponding to prev_addr */
3005 HOST_WIDE_INT prev_off[2]; /* last offset */
3006 int n_iter; /* number of iterators in use */
3007 int next_iter; /* next iterator to use */
3008 unsigned int save_gr_used_mask;
3011 static struct spill_fill_data spill_fill_data;
3013 static void
3014 setup_spill_pointers (int n_spills, rtx init_reg, HOST_WIDE_INT cfa_off)
3016 int i;
3018 spill_fill_data.init_after = get_last_insn ();
3019 spill_fill_data.init_reg[0] = init_reg;
3020 spill_fill_data.init_reg[1] = init_reg;
3021 spill_fill_data.prev_addr[0] = NULL;
3022 spill_fill_data.prev_addr[1] = NULL;
3023 spill_fill_data.prev_insn[0] = NULL;
3024 spill_fill_data.prev_insn[1] = NULL;
3025 spill_fill_data.prev_off[0] = cfa_off;
3026 spill_fill_data.prev_off[1] = cfa_off;
3027 spill_fill_data.next_iter = 0;
3028 spill_fill_data.save_gr_used_mask = current_frame_info.gr_used_mask;
3030 spill_fill_data.n_iter = 1 + (n_spills > 2);
3031 for (i = 0; i < spill_fill_data.n_iter; ++i)
3033 int regno = next_scratch_gr_reg ();
3034 spill_fill_data.iter_reg[i] = gen_rtx_REG (DImode, regno);
3035 current_frame_info.gr_used_mask |= 1 << regno;
3039 static void
3040 finish_spill_pointers (void)
3042 current_frame_info.gr_used_mask = spill_fill_data.save_gr_used_mask;
3045 static rtx
3046 spill_restore_mem (rtx reg, HOST_WIDE_INT cfa_off)
3048 int iter = spill_fill_data.next_iter;
3049 HOST_WIDE_INT disp = spill_fill_data.prev_off[iter] - cfa_off;
3050 rtx disp_rtx = GEN_INT (disp);
3051 rtx mem;
3053 if (spill_fill_data.prev_addr[iter])
3055 if (satisfies_constraint_N (disp_rtx))
3057 *spill_fill_data.prev_addr[iter]
3058 = gen_rtx_POST_MODIFY (DImode, spill_fill_data.iter_reg[iter],
3059 gen_rtx_PLUS (DImode,
3060 spill_fill_data.iter_reg[iter],
3061 disp_rtx));
3062 add_reg_note (spill_fill_data.prev_insn[iter],
3063 REG_INC, spill_fill_data.iter_reg[iter]);
3065 else
3067 /* ??? Could use register post_modify for loads. */
3068 if (!satisfies_constraint_I (disp_rtx))
3070 rtx tmp = gen_rtx_REG (DImode, next_scratch_gr_reg ());
3071 emit_move_insn (tmp, disp_rtx);
3072 disp_rtx = tmp;
3074 emit_insn (gen_adddi3 (spill_fill_data.iter_reg[iter],
3075 spill_fill_data.iter_reg[iter], disp_rtx));
3078 /* Micro-optimization: if we've created a frame pointer, it's at
3079 CFA 0, which may allow the real iterator to be initialized lower,
3080 slightly increasing parallelism. Also, if there are few saves
3081 it may eliminate the iterator entirely. */
3082 else if (disp == 0
3083 && spill_fill_data.init_reg[iter] == stack_pointer_rtx
3084 && frame_pointer_needed)
3086 mem = gen_rtx_MEM (GET_MODE (reg), hard_frame_pointer_rtx);
3087 set_mem_alias_set (mem, get_varargs_alias_set ());
3088 return mem;
3090 else
3092 rtx seq;
3093 rtx_insn *insn;
3095 if (disp == 0)
3096 seq = gen_movdi (spill_fill_data.iter_reg[iter],
3097 spill_fill_data.init_reg[iter]);
3098 else
3100 start_sequence ();
3102 if (!satisfies_constraint_I (disp_rtx))
3104 rtx tmp = gen_rtx_REG (DImode, next_scratch_gr_reg ());
3105 emit_move_insn (tmp, disp_rtx);
3106 disp_rtx = tmp;
3109 emit_insn (gen_adddi3 (spill_fill_data.iter_reg[iter],
3110 spill_fill_data.init_reg[iter],
3111 disp_rtx));
3113 seq = get_insns ();
3114 end_sequence ();
3117 /* Careful for being the first insn in a sequence. */
3118 if (spill_fill_data.init_after)
3119 insn = emit_insn_after (seq, spill_fill_data.init_after);
3120 else
3122 rtx_insn *first = get_insns ();
3123 if (first)
3124 insn = emit_insn_before (seq, first);
3125 else
3126 insn = emit_insn (seq);
3128 spill_fill_data.init_after = insn;
3131 mem = gen_rtx_MEM (GET_MODE (reg), spill_fill_data.iter_reg[iter]);
3133 /* ??? Not all of the spills are for varargs, but some of them are.
3134 The rest of the spills belong in an alias set of their own. But
3135 it doesn't actually hurt to include them here. */
3136 set_mem_alias_set (mem, get_varargs_alias_set ());
3138 spill_fill_data.prev_addr[iter] = &XEXP (mem, 0);
3139 spill_fill_data.prev_off[iter] = cfa_off;
3141 if (++iter >= spill_fill_data.n_iter)
3142 iter = 0;
3143 spill_fill_data.next_iter = iter;
3145 return mem;
3148 static void
3149 do_spill (rtx (*move_fn) (rtx, rtx, rtx), rtx reg, HOST_WIDE_INT cfa_off,
3150 rtx frame_reg)
3152 int iter = spill_fill_data.next_iter;
3153 rtx mem;
3154 rtx_insn *insn;
3156 mem = spill_restore_mem (reg, cfa_off);
3157 insn = emit_insn ((*move_fn) (mem, reg, GEN_INT (cfa_off)));
3158 spill_fill_data.prev_insn[iter] = insn;
3160 if (frame_reg)
3162 rtx base;
3163 HOST_WIDE_INT off;
3165 RTX_FRAME_RELATED_P (insn) = 1;
3167 /* Don't even pretend that the unwind code can intuit its way
3168 through a pair of interleaved post_modify iterators. Just
3169 provide the correct answer. */
3171 if (frame_pointer_needed)
3173 base = hard_frame_pointer_rtx;
3174 off = - cfa_off;
3176 else
3178 base = stack_pointer_rtx;
3179 off = current_frame_info.total_size - cfa_off;
3182 add_reg_note (insn, REG_CFA_OFFSET,
3183 gen_rtx_SET (VOIDmode,
3184 gen_rtx_MEM (GET_MODE (reg),
3185 plus_constant (Pmode,
3186 base, off)),
3187 frame_reg));
3191 static void
3192 do_restore (rtx (*move_fn) (rtx, rtx, rtx), rtx reg, HOST_WIDE_INT cfa_off)
3194 int iter = spill_fill_data.next_iter;
3195 rtx_insn *insn;
3197 insn = emit_insn ((*move_fn) (reg, spill_restore_mem (reg, cfa_off),
3198 GEN_INT (cfa_off)));
3199 spill_fill_data.prev_insn[iter] = insn;
3202 /* Wrapper functions that discards the CONST_INT spill offset. These
3203 exist so that we can give gr_spill/gr_fill the offset they need and
3204 use a consistent function interface. */
3206 static rtx
3207 gen_movdi_x (rtx dest, rtx src, rtx offset ATTRIBUTE_UNUSED)
3209 return gen_movdi (dest, src);
3212 static rtx
3213 gen_fr_spill_x (rtx dest, rtx src, rtx offset ATTRIBUTE_UNUSED)
3215 return gen_fr_spill (dest, src);
3218 static rtx
3219 gen_fr_restore_x (rtx dest, rtx src, rtx offset ATTRIBUTE_UNUSED)
3221 return gen_fr_restore (dest, src);
3224 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
3226 /* See Table 6.2 of the IA-64 Software Developer Manual, Volume 2. */
3227 #define BACKING_STORE_SIZE(N) ((N) > 0 ? ((N) + (N)/63 + 1) * 8 : 0)
3229 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
3230 inclusive. These are offsets from the current stack pointer. BS_SIZE
3231 is the size of the backing store. ??? This clobbers r2 and r3. */
3233 static void
3234 ia64_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size,
3235 int bs_size)
3237 rtx r2 = gen_rtx_REG (Pmode, GR_REG (2));
3238 rtx r3 = gen_rtx_REG (Pmode, GR_REG (3));
3239 rtx p6 = gen_rtx_REG (BImode, PR_REG (6));
3241 /* On the IA-64 there is a second stack in memory, namely the Backing Store
3242 of the Register Stack Engine. We also need to probe it after checking
3243 that the 2 stacks don't overlap. */
3244 emit_insn (gen_bsp_value (r3));
3245 emit_move_insn (r2, GEN_INT (-(first + size)));
3247 /* Compare current value of BSP and SP registers. */
3248 emit_insn (gen_rtx_SET (VOIDmode, p6,
3249 gen_rtx_fmt_ee (LTU, BImode,
3250 r3, stack_pointer_rtx)));
3252 /* Compute the address of the probe for the Backing Store (which grows
3253 towards higher addresses). We probe only at the first offset of
3254 the next page because some OS (eg Linux/ia64) only extend the
3255 backing store when this specific address is hit (but generate a SEGV
3256 on other address). Page size is the worst case (4KB). The reserve
3257 size is at least 4096 - (96 + 2) * 8 = 3312 bytes, which is enough.
3258 Also compute the address of the last probe for the memory stack
3259 (which grows towards lower addresses). */
3260 emit_insn (gen_rtx_SET (VOIDmode, r3, plus_constant (Pmode, r3, 4095)));
3261 emit_insn (gen_rtx_SET (VOIDmode, r2,
3262 gen_rtx_PLUS (Pmode, stack_pointer_rtx, r2)));
3264 /* Compare them and raise SEGV if the former has topped the latter. */
3265 emit_insn (gen_rtx_COND_EXEC (VOIDmode,
3266 gen_rtx_fmt_ee (NE, VOIDmode, p6, const0_rtx),
3267 gen_rtx_SET (VOIDmode, p6,
3268 gen_rtx_fmt_ee (GEU, BImode,
3269 r3, r2))));
3270 emit_insn (gen_rtx_SET (VOIDmode,
3271 gen_rtx_ZERO_EXTRACT (DImode, r3, GEN_INT (12),
3272 const0_rtx),
3273 const0_rtx));
3274 emit_insn (gen_rtx_COND_EXEC (VOIDmode,
3275 gen_rtx_fmt_ee (NE, VOIDmode, p6, const0_rtx),
3276 gen_rtx_TRAP_IF (VOIDmode, const1_rtx,
3277 GEN_INT (11))));
3279 /* Probe the Backing Store if necessary. */
3280 if (bs_size > 0)
3281 emit_stack_probe (r3);
3283 /* Probe the memory stack if necessary. */
3284 if (size == 0)
3287 /* See if we have a constant small number of probes to generate. If so,
3288 that's the easy case. */
3289 else if (size <= PROBE_INTERVAL)
3290 emit_stack_probe (r2);
3292 /* The run-time loop is made up of 8 insns in the generic case while this
3293 compile-time loop is made up of 5+2*(n-2) insns for n # of intervals. */
3294 else if (size <= 4 * PROBE_INTERVAL)
3296 HOST_WIDE_INT i;
3298 emit_move_insn (r2, GEN_INT (-(first + PROBE_INTERVAL)));
3299 emit_insn (gen_rtx_SET (VOIDmode, r2,
3300 gen_rtx_PLUS (Pmode, stack_pointer_rtx, r2)));
3301 emit_stack_probe (r2);
3303 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 2 until
3304 it exceeds SIZE. If only two probes are needed, this will not
3305 generate any code. Then probe at FIRST + SIZE. */
3306 for (i = 2 * PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
3308 emit_insn (gen_rtx_SET (VOIDmode, r2,
3309 plus_constant (Pmode, r2, -PROBE_INTERVAL)));
3310 emit_stack_probe (r2);
3313 emit_insn (gen_rtx_SET (VOIDmode, r2,
3314 plus_constant (Pmode, r2,
3315 (i - PROBE_INTERVAL) - size)));
3316 emit_stack_probe (r2);
3319 /* Otherwise, do the same as above, but in a loop. Note that we must be
3320 extra careful with variables wrapping around because we might be at
3321 the very top (or the very bottom) of the address space and we have
3322 to be able to handle this case properly; in particular, we use an
3323 equality test for the loop condition. */
3324 else
3326 HOST_WIDE_INT rounded_size;
3328 emit_move_insn (r2, GEN_INT (-first));
3331 /* Step 1: round SIZE to the previous multiple of the interval. */
3333 rounded_size = size & -PROBE_INTERVAL;
3336 /* Step 2: compute initial and final value of the loop counter. */
3338 /* TEST_ADDR = SP + FIRST. */
3339 emit_insn (gen_rtx_SET (VOIDmode, r2,
3340 gen_rtx_PLUS (Pmode, stack_pointer_rtx, r2)));
3342 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
3343 if (rounded_size > (1 << 21))
3345 emit_move_insn (r3, GEN_INT (-rounded_size));
3346 emit_insn (gen_rtx_SET (VOIDmode, r3, gen_rtx_PLUS (Pmode, r2, r3)));
3348 else
3349 emit_insn (gen_rtx_SET (VOIDmode, r3,
3350 gen_rtx_PLUS (Pmode, r2,
3351 GEN_INT (-rounded_size))));
3354 /* Step 3: the loop
3356 while (TEST_ADDR != LAST_ADDR)
3358 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
3359 probe at TEST_ADDR
3362 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
3363 until it is equal to ROUNDED_SIZE. */
3365 emit_insn (gen_probe_stack_range (r2, r2, r3));
3368 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
3369 that SIZE is equal to ROUNDED_SIZE. */
3371 /* TEMP = SIZE - ROUNDED_SIZE. */
3372 if (size != rounded_size)
3374 emit_insn (gen_rtx_SET (VOIDmode, r2,
3375 plus_constant (Pmode, r2,
3376 rounded_size - size)));
3377 emit_stack_probe (r2);
3381 /* Make sure nothing is scheduled before we are done. */
3382 emit_insn (gen_blockage ());
3385 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
3386 absolute addresses. */
3388 const char *
3389 output_probe_stack_range (rtx reg1, rtx reg2)
3391 static int labelno = 0;
3392 char loop_lab[32], end_lab[32];
3393 rtx xops[3];
3395 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
3396 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
3398 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
3400 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
3401 xops[0] = reg1;
3402 xops[1] = reg2;
3403 xops[2] = gen_rtx_REG (BImode, PR_REG (6));
3404 output_asm_insn ("cmp.eq %2, %I2 = %0, %1", xops);
3405 fprintf (asm_out_file, "\t(%s) br.cond.dpnt ", reg_names [REGNO (xops[2])]);
3406 assemble_name_raw (asm_out_file, end_lab);
3407 fputc ('\n', asm_out_file);
3409 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
3410 xops[1] = GEN_INT (-PROBE_INTERVAL);
3411 output_asm_insn ("addl %0 = %1, %0", xops);
3412 fputs ("\t;;\n", asm_out_file);
3414 /* Probe at TEST_ADDR and branch. */
3415 output_asm_insn ("probe.w.fault %0, 0", xops);
3416 fprintf (asm_out_file, "\tbr ");
3417 assemble_name_raw (asm_out_file, loop_lab);
3418 fputc ('\n', asm_out_file);
3420 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
3422 return "";
3425 /* Called after register allocation to add any instructions needed for the
3426 prologue. Using a prologue insn is favored compared to putting all of the
3427 instructions in output_function_prologue(), since it allows the scheduler
3428 to intermix instructions with the saves of the caller saved registers. In
3429 some cases, it might be necessary to emit a barrier instruction as the last
3430 insn to prevent such scheduling.
3432 Also any insns generated here should have RTX_FRAME_RELATED_P(insn) = 1
3433 so that the debug info generation code can handle them properly.
3435 The register save area is laid out like so:
3436 cfa+16
3437 [ varargs spill area ]
3438 [ fr register spill area ]
3439 [ br register spill area ]
3440 [ ar register spill area ]
3441 [ pr register spill area ]
3442 [ gr register spill area ] */
3444 /* ??? Get inefficient code when the frame size is larger than can fit in an
3445 adds instruction. */
3447 void
3448 ia64_expand_prologue (void)
3450 rtx_insn *insn;
3451 rtx ar_pfs_save_reg, ar_unat_save_reg;
3452 int i, epilogue_p, regno, alt_regno, cfa_off, n_varargs;
3453 rtx reg, alt_reg;
3455 ia64_compute_frame_size (get_frame_size ());
3456 last_scratch_gr_reg = 15;
3458 if (flag_stack_usage_info)
3459 current_function_static_stack_size = current_frame_info.total_size;
3461 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
3463 HOST_WIDE_INT size = current_frame_info.total_size;
3464 int bs_size = BACKING_STORE_SIZE (current_frame_info.n_input_regs
3465 + current_frame_info.n_local_regs);
3467 if (crtl->is_leaf && !cfun->calls_alloca)
3469 if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
3470 ia64_emit_probe_stack_range (STACK_CHECK_PROTECT,
3471 size - STACK_CHECK_PROTECT,
3472 bs_size);
3473 else if (size + bs_size > STACK_CHECK_PROTECT)
3474 ia64_emit_probe_stack_range (STACK_CHECK_PROTECT, 0, bs_size);
3476 else if (size + bs_size > 0)
3477 ia64_emit_probe_stack_range (STACK_CHECK_PROTECT, size, bs_size);
3480 if (dump_file)
3482 fprintf (dump_file, "ia64 frame related registers "
3483 "recorded in current_frame_info.r[]:\n");
3484 #define PRINTREG(a) if (current_frame_info.r[a]) \
3485 fprintf(dump_file, "%s = %d\n", #a, current_frame_info.r[a])
3486 PRINTREG(reg_fp);
3487 PRINTREG(reg_save_b0);
3488 PRINTREG(reg_save_pr);
3489 PRINTREG(reg_save_ar_pfs);
3490 PRINTREG(reg_save_ar_unat);
3491 PRINTREG(reg_save_ar_lc);
3492 PRINTREG(reg_save_gp);
3493 #undef PRINTREG
3496 /* If there is no epilogue, then we don't need some prologue insns.
3497 We need to avoid emitting the dead prologue insns, because flow
3498 will complain about them. */
3499 if (optimize)
3501 edge e;
3502 edge_iterator ei;
3504 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
3505 if ((e->flags & EDGE_FAKE) == 0
3506 && (e->flags & EDGE_FALLTHRU) != 0)
3507 break;
3508 epilogue_p = (e != NULL);
3510 else
3511 epilogue_p = 1;
3513 /* Set the local, input, and output register names. We need to do this
3514 for GNU libc, which creates crti.S/crtn.S by splitting initfini.c in
3515 half. If we use in/loc/out register names, then we get assembler errors
3516 in crtn.S because there is no alloc insn or regstk directive in there. */
3517 if (! TARGET_REG_NAMES)
3519 int inputs = current_frame_info.n_input_regs;
3520 int locals = current_frame_info.n_local_regs;
3521 int outputs = current_frame_info.n_output_regs;
3523 for (i = 0; i < inputs; i++)
3524 reg_names[IN_REG (i)] = ia64_reg_numbers[i];
3525 for (i = 0; i < locals; i++)
3526 reg_names[LOC_REG (i)] = ia64_reg_numbers[inputs + i];
3527 for (i = 0; i < outputs; i++)
3528 reg_names[OUT_REG (i)] = ia64_reg_numbers[inputs + locals + i];
3531 /* Set the frame pointer register name. The regnum is logically loc79,
3532 but of course we'll not have allocated that many locals. Rather than
3533 worrying about renumbering the existing rtxs, we adjust the name. */
3534 /* ??? This code means that we can never use one local register when
3535 there is a frame pointer. loc79 gets wasted in this case, as it is
3536 renamed to a register that will never be used. See also the try_locals
3537 code in find_gr_spill. */
3538 if (current_frame_info.r[reg_fp])
3540 const char *tmp = reg_names[HARD_FRAME_POINTER_REGNUM];
3541 reg_names[HARD_FRAME_POINTER_REGNUM]
3542 = reg_names[current_frame_info.r[reg_fp]];
3543 reg_names[current_frame_info.r[reg_fp]] = tmp;
3546 /* We don't need an alloc instruction if we've used no outputs or locals. */
3547 if (current_frame_info.n_local_regs == 0
3548 && current_frame_info.n_output_regs == 0
3549 && current_frame_info.n_input_regs <= crtl->args.info.int_regs
3550 && !TEST_HARD_REG_BIT (current_frame_info.mask, AR_PFS_REGNUM))
3552 /* If there is no alloc, but there are input registers used, then we
3553 need a .regstk directive. */
3554 current_frame_info.need_regstk = (TARGET_REG_NAMES != 0);
3555 ar_pfs_save_reg = NULL_RTX;
3557 else
3559 current_frame_info.need_regstk = 0;
3561 if (current_frame_info.r[reg_save_ar_pfs])
3563 regno = current_frame_info.r[reg_save_ar_pfs];
3564 reg_emitted (reg_save_ar_pfs);
3566 else
3567 regno = next_scratch_gr_reg ();
3568 ar_pfs_save_reg = gen_rtx_REG (DImode, regno);
3570 insn = emit_insn (gen_alloc (ar_pfs_save_reg,
3571 GEN_INT (current_frame_info.n_input_regs),
3572 GEN_INT (current_frame_info.n_local_regs),
3573 GEN_INT (current_frame_info.n_output_regs),
3574 GEN_INT (current_frame_info.n_rotate_regs)));
3575 if (current_frame_info.r[reg_save_ar_pfs])
3577 RTX_FRAME_RELATED_P (insn) = 1;
3578 add_reg_note (insn, REG_CFA_REGISTER,
3579 gen_rtx_SET (VOIDmode,
3580 ar_pfs_save_reg,
3581 gen_rtx_REG (DImode, AR_PFS_REGNUM)));
3585 /* Set up frame pointer, stack pointer, and spill iterators. */
3587 n_varargs = cfun->machine->n_varargs;
3588 setup_spill_pointers (current_frame_info.n_spilled + n_varargs,
3589 stack_pointer_rtx, 0);
3591 if (frame_pointer_needed)
3593 insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
3594 RTX_FRAME_RELATED_P (insn) = 1;
3596 /* Force the unwind info to recognize this as defining a new CFA,
3597 rather than some temp register setup. */
3598 add_reg_note (insn, REG_CFA_ADJUST_CFA, NULL_RTX);
3601 if (current_frame_info.total_size != 0)
3603 rtx frame_size_rtx = GEN_INT (- current_frame_info.total_size);
3604 rtx offset;
3606 if (satisfies_constraint_I (frame_size_rtx))
3607 offset = frame_size_rtx;
3608 else
3610 regno = next_scratch_gr_reg ();
3611 offset = gen_rtx_REG (DImode, regno);
3612 emit_move_insn (offset, frame_size_rtx);
3615 insn = emit_insn (gen_adddi3 (stack_pointer_rtx,
3616 stack_pointer_rtx, offset));
3618 if (! frame_pointer_needed)
3620 RTX_FRAME_RELATED_P (insn) = 1;
3621 add_reg_note (insn, REG_CFA_ADJUST_CFA,
3622 gen_rtx_SET (VOIDmode,
3623 stack_pointer_rtx,
3624 gen_rtx_PLUS (DImode,
3625 stack_pointer_rtx,
3626 frame_size_rtx)));
3629 /* ??? At this point we must generate a magic insn that appears to
3630 modify the stack pointer, the frame pointer, and all spill
3631 iterators. This would allow the most scheduling freedom. For
3632 now, just hard stop. */
3633 emit_insn (gen_blockage ());
3636 /* Must copy out ar.unat before doing any integer spills. */
3637 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
3639 if (current_frame_info.r[reg_save_ar_unat])
3641 ar_unat_save_reg
3642 = gen_rtx_REG (DImode, current_frame_info.r[reg_save_ar_unat]);
3643 reg_emitted (reg_save_ar_unat);
3645 else
3647 alt_regno = next_scratch_gr_reg ();
3648 ar_unat_save_reg = gen_rtx_REG (DImode, alt_regno);
3649 current_frame_info.gr_used_mask |= 1 << alt_regno;
3652 reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
3653 insn = emit_move_insn (ar_unat_save_reg, reg);
3654 if (current_frame_info.r[reg_save_ar_unat])
3656 RTX_FRAME_RELATED_P (insn) = 1;
3657 add_reg_note (insn, REG_CFA_REGISTER, NULL_RTX);
3660 /* Even if we're not going to generate an epilogue, we still
3661 need to save the register so that EH works. */
3662 if (! epilogue_p && current_frame_info.r[reg_save_ar_unat])
3663 emit_insn (gen_prologue_use (ar_unat_save_reg));
3665 else
3666 ar_unat_save_reg = NULL_RTX;
3668 /* Spill all varargs registers. Do this before spilling any GR registers,
3669 since we want the UNAT bits for the GR registers to override the UNAT
3670 bits from varargs, which we don't care about. */
3672 cfa_off = -16;
3673 for (regno = GR_ARG_FIRST + 7; n_varargs > 0; --n_varargs, --regno)
3675 reg = gen_rtx_REG (DImode, regno);
3676 do_spill (gen_gr_spill, reg, cfa_off += 8, NULL_RTX);
3679 /* Locate the bottom of the register save area. */
3680 cfa_off = (current_frame_info.spill_cfa_off
3681 + current_frame_info.spill_size
3682 + current_frame_info.extra_spill_size);
3684 /* Save the predicate register block either in a register or in memory. */
3685 if (TEST_HARD_REG_BIT (current_frame_info.mask, PR_REG (0)))
3687 reg = gen_rtx_REG (DImode, PR_REG (0));
3688 if (current_frame_info.r[reg_save_pr] != 0)
3690 alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_pr]);
3691 reg_emitted (reg_save_pr);
3692 insn = emit_move_insn (alt_reg, reg);
3694 /* ??? Denote pr spill/fill by a DImode move that modifies all
3695 64 hard registers. */
3696 RTX_FRAME_RELATED_P (insn) = 1;
3697 add_reg_note (insn, REG_CFA_REGISTER, NULL_RTX);
3699 /* Even if we're not going to generate an epilogue, we still
3700 need to save the register so that EH works. */
3701 if (! epilogue_p)
3702 emit_insn (gen_prologue_use (alt_reg));
3704 else
3706 alt_regno = next_scratch_gr_reg ();
3707 alt_reg = gen_rtx_REG (DImode, alt_regno);
3708 insn = emit_move_insn (alt_reg, reg);
3709 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
3710 cfa_off -= 8;
3714 /* Handle AR regs in numerical order. All of them get special handling. */
3715 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM)
3716 && current_frame_info.r[reg_save_ar_unat] == 0)
3718 reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
3719 do_spill (gen_movdi_x, ar_unat_save_reg, cfa_off, reg);
3720 cfa_off -= 8;
3723 /* The alloc insn already copied ar.pfs into a general register. The
3724 only thing we have to do now is copy that register to a stack slot
3725 if we'd not allocated a local register for the job. */
3726 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_PFS_REGNUM)
3727 && current_frame_info.r[reg_save_ar_pfs] == 0)
3729 reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
3730 do_spill (gen_movdi_x, ar_pfs_save_reg, cfa_off, reg);
3731 cfa_off -= 8;
3734 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_LC_REGNUM))
3736 reg = gen_rtx_REG (DImode, AR_LC_REGNUM);
3737 if (current_frame_info.r[reg_save_ar_lc] != 0)
3739 alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_ar_lc]);
3740 reg_emitted (reg_save_ar_lc);
3741 insn = emit_move_insn (alt_reg, reg);
3742 RTX_FRAME_RELATED_P (insn) = 1;
3743 add_reg_note (insn, REG_CFA_REGISTER, NULL_RTX);
3745 /* Even if we're not going to generate an epilogue, we still
3746 need to save the register so that EH works. */
3747 if (! epilogue_p)
3748 emit_insn (gen_prologue_use (alt_reg));
3750 else
3752 alt_regno = next_scratch_gr_reg ();
3753 alt_reg = gen_rtx_REG (DImode, alt_regno);
3754 emit_move_insn (alt_reg, reg);
3755 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
3756 cfa_off -= 8;
3760 /* Save the return pointer. */
3761 if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
3763 reg = gen_rtx_REG (DImode, BR_REG (0));
3764 if (current_frame_info.r[reg_save_b0] != 0)
3766 alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_b0]);
3767 reg_emitted (reg_save_b0);
3768 insn = emit_move_insn (alt_reg, reg);
3769 RTX_FRAME_RELATED_P (insn) = 1;
3770 add_reg_note (insn, REG_CFA_REGISTER,
3771 gen_rtx_SET (VOIDmode, alt_reg, pc_rtx));
3773 /* Even if we're not going to generate an epilogue, we still
3774 need to save the register so that EH works. */
3775 if (! epilogue_p)
3776 emit_insn (gen_prologue_use (alt_reg));
3778 else
3780 alt_regno = next_scratch_gr_reg ();
3781 alt_reg = gen_rtx_REG (DImode, alt_regno);
3782 emit_move_insn (alt_reg, reg);
3783 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
3784 cfa_off -= 8;
3788 if (current_frame_info.r[reg_save_gp])
3790 reg_emitted (reg_save_gp);
3791 insn = emit_move_insn (gen_rtx_REG (DImode,
3792 current_frame_info.r[reg_save_gp]),
3793 pic_offset_table_rtx);
3796 /* We should now be at the base of the gr/br/fr spill area. */
3797 gcc_assert (cfa_off == (current_frame_info.spill_cfa_off
3798 + current_frame_info.spill_size));
3800 /* Spill all general registers. */
3801 for (regno = GR_REG (1); regno <= GR_REG (31); ++regno)
3802 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3804 reg = gen_rtx_REG (DImode, regno);
3805 do_spill (gen_gr_spill, reg, cfa_off, reg);
3806 cfa_off -= 8;
3809 /* Spill the rest of the BR registers. */
3810 for (regno = BR_REG (1); regno <= BR_REG (7); ++regno)
3811 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3813 alt_regno = next_scratch_gr_reg ();
3814 alt_reg = gen_rtx_REG (DImode, alt_regno);
3815 reg = gen_rtx_REG (DImode, regno);
3816 emit_move_insn (alt_reg, reg);
3817 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
3818 cfa_off -= 8;
3821 /* Align the frame and spill all FR registers. */
3822 for (regno = FR_REG (2); regno <= FR_REG (127); ++regno)
3823 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3825 gcc_assert (!(cfa_off & 15));
3826 reg = gen_rtx_REG (XFmode, regno);
3827 do_spill (gen_fr_spill_x, reg, cfa_off, reg);
3828 cfa_off -= 16;
3831 gcc_assert (cfa_off == current_frame_info.spill_cfa_off);
3833 finish_spill_pointers ();
3836 /* Output the textual info surrounding the prologue. */
3838 void
3839 ia64_start_function (FILE *file, const char *fnname,
3840 tree decl ATTRIBUTE_UNUSED)
3842 #if TARGET_ABI_OPEN_VMS
3843 vms_start_function (fnname);
3844 #endif
3846 fputs ("\t.proc ", file);
3847 assemble_name (file, fnname);
3848 fputc ('\n', file);
3849 ASM_OUTPUT_LABEL (file, fnname);
3852 /* Called after register allocation to add any instructions needed for the
3853 epilogue. Using an epilogue insn is favored compared to putting all of the
3854 instructions in output_function_prologue(), since it allows the scheduler
3855 to intermix instructions with the saves of the caller saved registers. In
3856 some cases, it might be necessary to emit a barrier instruction as the last
3857 insn to prevent such scheduling. */
3859 void
3860 ia64_expand_epilogue (int sibcall_p)
3862 rtx_insn *insn;
3863 rtx reg, alt_reg, ar_unat_save_reg;
3864 int regno, alt_regno, cfa_off;
3866 ia64_compute_frame_size (get_frame_size ());
3868 /* If there is a frame pointer, then we use it instead of the stack
3869 pointer, so that the stack pointer does not need to be valid when
3870 the epilogue starts. See EXIT_IGNORE_STACK. */
3871 if (frame_pointer_needed)
3872 setup_spill_pointers (current_frame_info.n_spilled,
3873 hard_frame_pointer_rtx, 0);
3874 else
3875 setup_spill_pointers (current_frame_info.n_spilled, stack_pointer_rtx,
3876 current_frame_info.total_size);
3878 if (current_frame_info.total_size != 0)
3880 /* ??? At this point we must generate a magic insn that appears to
3881 modify the spill iterators and the frame pointer. This would
3882 allow the most scheduling freedom. For now, just hard stop. */
3883 emit_insn (gen_blockage ());
3886 /* Locate the bottom of the register save area. */
3887 cfa_off = (current_frame_info.spill_cfa_off
3888 + current_frame_info.spill_size
3889 + current_frame_info.extra_spill_size);
3891 /* Restore the predicate registers. */
3892 if (TEST_HARD_REG_BIT (current_frame_info.mask, PR_REG (0)))
3894 if (current_frame_info.r[reg_save_pr] != 0)
3896 alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_pr]);
3897 reg_emitted (reg_save_pr);
3899 else
3901 alt_regno = next_scratch_gr_reg ();
3902 alt_reg = gen_rtx_REG (DImode, alt_regno);
3903 do_restore (gen_movdi_x, alt_reg, cfa_off);
3904 cfa_off -= 8;
3906 reg = gen_rtx_REG (DImode, PR_REG (0));
3907 emit_move_insn (reg, alt_reg);
3910 /* Restore the application registers. */
3912 /* Load the saved unat from the stack, but do not restore it until
3913 after the GRs have been restored. */
3914 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
3916 if (current_frame_info.r[reg_save_ar_unat] != 0)
3918 ar_unat_save_reg
3919 = gen_rtx_REG (DImode, current_frame_info.r[reg_save_ar_unat]);
3920 reg_emitted (reg_save_ar_unat);
3922 else
3924 alt_regno = next_scratch_gr_reg ();
3925 ar_unat_save_reg = gen_rtx_REG (DImode, alt_regno);
3926 current_frame_info.gr_used_mask |= 1 << alt_regno;
3927 do_restore (gen_movdi_x, ar_unat_save_reg, cfa_off);
3928 cfa_off -= 8;
3931 else
3932 ar_unat_save_reg = NULL_RTX;
3934 if (current_frame_info.r[reg_save_ar_pfs] != 0)
3936 reg_emitted (reg_save_ar_pfs);
3937 alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_ar_pfs]);
3938 reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
3939 emit_move_insn (reg, alt_reg);
3941 else if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_PFS_REGNUM))
3943 alt_regno = next_scratch_gr_reg ();
3944 alt_reg = gen_rtx_REG (DImode, alt_regno);
3945 do_restore (gen_movdi_x, alt_reg, cfa_off);
3946 cfa_off -= 8;
3947 reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
3948 emit_move_insn (reg, alt_reg);
3951 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_LC_REGNUM))
3953 if (current_frame_info.r[reg_save_ar_lc] != 0)
3955 alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_ar_lc]);
3956 reg_emitted (reg_save_ar_lc);
3958 else
3960 alt_regno = next_scratch_gr_reg ();
3961 alt_reg = gen_rtx_REG (DImode, alt_regno);
3962 do_restore (gen_movdi_x, alt_reg, cfa_off);
3963 cfa_off -= 8;
3965 reg = gen_rtx_REG (DImode, AR_LC_REGNUM);
3966 emit_move_insn (reg, alt_reg);
3969 /* Restore the return pointer. */
3970 if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
3972 if (current_frame_info.r[reg_save_b0] != 0)
3974 alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_b0]);
3975 reg_emitted (reg_save_b0);
3977 else
3979 alt_regno = next_scratch_gr_reg ();
3980 alt_reg = gen_rtx_REG (DImode, alt_regno);
3981 do_restore (gen_movdi_x, alt_reg, cfa_off);
3982 cfa_off -= 8;
3984 reg = gen_rtx_REG (DImode, BR_REG (0));
3985 emit_move_insn (reg, alt_reg);
3988 /* We should now be at the base of the gr/br/fr spill area. */
3989 gcc_assert (cfa_off == (current_frame_info.spill_cfa_off
3990 + current_frame_info.spill_size));
3992 /* The GP may be stored on the stack in the prologue, but it's
3993 never restored in the epilogue. Skip the stack slot. */
3994 if (TEST_HARD_REG_BIT (current_frame_info.mask, GR_REG (1)))
3995 cfa_off -= 8;
3997 /* Restore all general registers. */
3998 for (regno = GR_REG (2); regno <= GR_REG (31); ++regno)
3999 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
4001 reg = gen_rtx_REG (DImode, regno);
4002 do_restore (gen_gr_restore, reg, cfa_off);
4003 cfa_off -= 8;
4006 /* Restore the branch registers. */
4007 for (regno = BR_REG (1); regno <= BR_REG (7); ++regno)
4008 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
4010 alt_regno = next_scratch_gr_reg ();
4011 alt_reg = gen_rtx_REG (DImode, alt_regno);
4012 do_restore (gen_movdi_x, alt_reg, cfa_off);
4013 cfa_off -= 8;
4014 reg = gen_rtx_REG (DImode, regno);
4015 emit_move_insn (reg, alt_reg);
4018 /* Restore floating point registers. */
4019 for (regno = FR_REG (2); regno <= FR_REG (127); ++regno)
4020 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
4022 gcc_assert (!(cfa_off & 15));
4023 reg = gen_rtx_REG (XFmode, regno);
4024 do_restore (gen_fr_restore_x, reg, cfa_off);
4025 cfa_off -= 16;
4028 /* Restore ar.unat for real. */
4029 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
4031 reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
4032 emit_move_insn (reg, ar_unat_save_reg);
4035 gcc_assert (cfa_off == current_frame_info.spill_cfa_off);
4037 finish_spill_pointers ();
4039 if (current_frame_info.total_size
4040 || cfun->machine->ia64_eh_epilogue_sp
4041 || frame_pointer_needed)
4043 /* ??? At this point we must generate a magic insn that appears to
4044 modify the spill iterators, the stack pointer, and the frame
4045 pointer. This would allow the most scheduling freedom. For now,
4046 just hard stop. */
4047 emit_insn (gen_blockage ());
4050 if (cfun->machine->ia64_eh_epilogue_sp)
4051 emit_move_insn (stack_pointer_rtx, cfun->machine->ia64_eh_epilogue_sp);
4052 else if (frame_pointer_needed)
4054 insn = emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
4055 RTX_FRAME_RELATED_P (insn) = 1;
4056 add_reg_note (insn, REG_CFA_ADJUST_CFA, NULL);
4058 else if (current_frame_info.total_size)
4060 rtx offset, frame_size_rtx;
4062 frame_size_rtx = GEN_INT (current_frame_info.total_size);
4063 if (satisfies_constraint_I (frame_size_rtx))
4064 offset = frame_size_rtx;
4065 else
4067 regno = next_scratch_gr_reg ();
4068 offset = gen_rtx_REG (DImode, regno);
4069 emit_move_insn (offset, frame_size_rtx);
4072 insn = emit_insn (gen_adddi3 (stack_pointer_rtx, stack_pointer_rtx,
4073 offset));
4075 RTX_FRAME_RELATED_P (insn) = 1;
4076 add_reg_note (insn, REG_CFA_ADJUST_CFA,
4077 gen_rtx_SET (VOIDmode,
4078 stack_pointer_rtx,
4079 gen_rtx_PLUS (DImode,
4080 stack_pointer_rtx,
4081 frame_size_rtx)));
4084 if (cfun->machine->ia64_eh_epilogue_bsp)
4085 emit_insn (gen_set_bsp (cfun->machine->ia64_eh_epilogue_bsp));
4087 if (! sibcall_p)
4088 emit_jump_insn (gen_return_internal (gen_rtx_REG (DImode, BR_REG (0))));
4089 else
4091 int fp = GR_REG (2);
4092 /* We need a throw away register here, r0 and r1 are reserved,
4093 so r2 is the first available call clobbered register. If
4094 there was a frame_pointer register, we may have swapped the
4095 names of r2 and HARD_FRAME_POINTER_REGNUM, so we have to make
4096 sure we're using the string "r2" when emitting the register
4097 name for the assembler. */
4098 if (current_frame_info.r[reg_fp]
4099 && current_frame_info.r[reg_fp] == GR_REG (2))
4100 fp = HARD_FRAME_POINTER_REGNUM;
4102 /* We must emit an alloc to force the input registers to become output
4103 registers. Otherwise, if the callee tries to pass its parameters
4104 through to another call without an intervening alloc, then these
4105 values get lost. */
4106 /* ??? We don't need to preserve all input registers. We only need to
4107 preserve those input registers used as arguments to the sibling call.
4108 It is unclear how to compute that number here. */
4109 if (current_frame_info.n_input_regs != 0)
4111 rtx n_inputs = GEN_INT (current_frame_info.n_input_regs);
4113 insn = emit_insn (gen_alloc (gen_rtx_REG (DImode, fp),
4114 const0_rtx, const0_rtx,
4115 n_inputs, const0_rtx));
4116 RTX_FRAME_RELATED_P (insn) = 1;
4118 /* ??? We need to mark the alloc as frame-related so that it gets
4119 passed into ia64_asm_unwind_emit for ia64-specific unwinding.
4120 But there's nothing dwarf2 related to be done wrt the register
4121 windows. If we do nothing, dwarf2out will abort on the UNSPEC;
4122 the empty parallel means dwarf2out will not see anything. */
4123 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
4124 gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (0)));
4129 /* Return 1 if br.ret can do all the work required to return from a
4130 function. */
4133 ia64_direct_return (void)
4135 if (reload_completed && ! frame_pointer_needed)
4137 ia64_compute_frame_size (get_frame_size ());
4139 return (current_frame_info.total_size == 0
4140 && current_frame_info.n_spilled == 0
4141 && current_frame_info.r[reg_save_b0] == 0
4142 && current_frame_info.r[reg_save_pr] == 0
4143 && current_frame_info.r[reg_save_ar_pfs] == 0
4144 && current_frame_info.r[reg_save_ar_unat] == 0
4145 && current_frame_info.r[reg_save_ar_lc] == 0);
4147 return 0;
4150 /* Return the magic cookie that we use to hold the return address
4151 during early compilation. */
4154 ia64_return_addr_rtx (HOST_WIDE_INT count, rtx frame ATTRIBUTE_UNUSED)
4156 if (count != 0)
4157 return NULL;
4158 return gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_RET_ADDR);
4161 /* Split this value after reload, now that we know where the return
4162 address is saved. */
4164 void
4165 ia64_split_return_addr_rtx (rtx dest)
4167 rtx src;
4169 if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
4171 if (current_frame_info.r[reg_save_b0] != 0)
4173 src = gen_rtx_REG (DImode, current_frame_info.r[reg_save_b0]);
4174 reg_emitted (reg_save_b0);
4176 else
4178 HOST_WIDE_INT off;
4179 unsigned int regno;
4180 rtx off_r;
4182 /* Compute offset from CFA for BR0. */
4183 /* ??? Must be kept in sync with ia64_expand_prologue. */
4184 off = (current_frame_info.spill_cfa_off
4185 + current_frame_info.spill_size);
4186 for (regno = GR_REG (1); regno <= GR_REG (31); ++regno)
4187 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
4188 off -= 8;
4190 /* Convert CFA offset to a register based offset. */
4191 if (frame_pointer_needed)
4192 src = hard_frame_pointer_rtx;
4193 else
4195 src = stack_pointer_rtx;
4196 off += current_frame_info.total_size;
4199 /* Load address into scratch register. */
4200 off_r = GEN_INT (off);
4201 if (satisfies_constraint_I (off_r))
4202 emit_insn (gen_adddi3 (dest, src, off_r));
4203 else
4205 emit_move_insn (dest, off_r);
4206 emit_insn (gen_adddi3 (dest, src, dest));
4209 src = gen_rtx_MEM (Pmode, dest);
4212 else
4213 src = gen_rtx_REG (DImode, BR_REG (0));
4215 emit_move_insn (dest, src);
4219 ia64_hard_regno_rename_ok (int from, int to)
4221 /* Don't clobber any of the registers we reserved for the prologue. */
4222 unsigned int r;
4224 for (r = reg_fp; r <= reg_save_ar_lc; r++)
4225 if (to == current_frame_info.r[r]
4226 || from == current_frame_info.r[r]
4227 || to == emitted_frame_related_regs[r]
4228 || from == emitted_frame_related_regs[r])
4229 return 0;
4231 /* Don't use output registers outside the register frame. */
4232 if (OUT_REGNO_P (to) && to >= OUT_REG (current_frame_info.n_output_regs))
4233 return 0;
4235 /* Retain even/oddness on predicate register pairs. */
4236 if (PR_REGNO_P (from) && PR_REGNO_P (to))
4237 return (from & 1) == (to & 1);
4239 return 1;
4242 /* Target hook for assembling integer objects. Handle word-sized
4243 aligned objects and detect the cases when @fptr is needed. */
4245 static bool
4246 ia64_assemble_integer (rtx x, unsigned int size, int aligned_p)
4248 if (size == POINTER_SIZE / BITS_PER_UNIT
4249 && !(TARGET_NO_PIC || TARGET_AUTO_PIC)
4250 && GET_CODE (x) == SYMBOL_REF
4251 && SYMBOL_REF_FUNCTION_P (x))
4253 static const char * const directive[2][2] = {
4254 /* 64-bit pointer */ /* 32-bit pointer */
4255 { "\tdata8.ua\t@fptr(", "\tdata4.ua\t@fptr("}, /* unaligned */
4256 { "\tdata8\t@fptr(", "\tdata4\t@fptr("} /* aligned */
4258 fputs (directive[(aligned_p != 0)][POINTER_SIZE == 32], asm_out_file);
4259 output_addr_const (asm_out_file, x);
4260 fputs (")\n", asm_out_file);
4261 return true;
4263 return default_assemble_integer (x, size, aligned_p);
4266 /* Emit the function prologue. */
4268 static void
4269 ia64_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
4271 int mask, grsave, grsave_prev;
4273 if (current_frame_info.need_regstk)
4274 fprintf (file, "\t.regstk %d, %d, %d, %d\n",
4275 current_frame_info.n_input_regs,
4276 current_frame_info.n_local_regs,
4277 current_frame_info.n_output_regs,
4278 current_frame_info.n_rotate_regs);
4280 if (ia64_except_unwind_info (&global_options) != UI_TARGET)
4281 return;
4283 /* Emit the .prologue directive. */
4285 mask = 0;
4286 grsave = grsave_prev = 0;
4287 if (current_frame_info.r[reg_save_b0] != 0)
4289 mask |= 8;
4290 grsave = grsave_prev = current_frame_info.r[reg_save_b0];
4292 if (current_frame_info.r[reg_save_ar_pfs] != 0
4293 && (grsave_prev == 0
4294 || current_frame_info.r[reg_save_ar_pfs] == grsave_prev + 1))
4296 mask |= 4;
4297 if (grsave_prev == 0)
4298 grsave = current_frame_info.r[reg_save_ar_pfs];
4299 grsave_prev = current_frame_info.r[reg_save_ar_pfs];
4301 if (current_frame_info.r[reg_fp] != 0
4302 && (grsave_prev == 0
4303 || current_frame_info.r[reg_fp] == grsave_prev + 1))
4305 mask |= 2;
4306 if (grsave_prev == 0)
4307 grsave = HARD_FRAME_POINTER_REGNUM;
4308 grsave_prev = current_frame_info.r[reg_fp];
4310 if (current_frame_info.r[reg_save_pr] != 0
4311 && (grsave_prev == 0
4312 || current_frame_info.r[reg_save_pr] == grsave_prev + 1))
4314 mask |= 1;
4315 if (grsave_prev == 0)
4316 grsave = current_frame_info.r[reg_save_pr];
4319 if (mask && TARGET_GNU_AS)
4320 fprintf (file, "\t.prologue %d, %d\n", mask,
4321 ia64_dbx_register_number (grsave));
4322 else
4323 fputs ("\t.prologue\n", file);
4325 /* Emit a .spill directive, if necessary, to relocate the base of
4326 the register spill area. */
4327 if (current_frame_info.spill_cfa_off != -16)
4328 fprintf (file, "\t.spill %ld\n",
4329 (long) (current_frame_info.spill_cfa_off
4330 + current_frame_info.spill_size));
4333 /* Emit the .body directive at the scheduled end of the prologue. */
4335 static void
4336 ia64_output_function_end_prologue (FILE *file)
4338 if (ia64_except_unwind_info (&global_options) != UI_TARGET)
4339 return;
4341 fputs ("\t.body\n", file);
4344 /* Emit the function epilogue. */
4346 static void
4347 ia64_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
4348 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
4350 int i;
4352 if (current_frame_info.r[reg_fp])
4354 const char *tmp = reg_names[HARD_FRAME_POINTER_REGNUM];
4355 reg_names[HARD_FRAME_POINTER_REGNUM]
4356 = reg_names[current_frame_info.r[reg_fp]];
4357 reg_names[current_frame_info.r[reg_fp]] = tmp;
4358 reg_emitted (reg_fp);
4360 if (! TARGET_REG_NAMES)
4362 for (i = 0; i < current_frame_info.n_input_regs; i++)
4363 reg_names[IN_REG (i)] = ia64_input_reg_names[i];
4364 for (i = 0; i < current_frame_info.n_local_regs; i++)
4365 reg_names[LOC_REG (i)] = ia64_local_reg_names[i];
4366 for (i = 0; i < current_frame_info.n_output_regs; i++)
4367 reg_names[OUT_REG (i)] = ia64_output_reg_names[i];
4370 current_frame_info.initialized = 0;
4374 ia64_dbx_register_number (int regno)
4376 /* In ia64_expand_prologue we quite literally renamed the frame pointer
4377 from its home at loc79 to something inside the register frame. We
4378 must perform the same renumbering here for the debug info. */
4379 if (current_frame_info.r[reg_fp])
4381 if (regno == HARD_FRAME_POINTER_REGNUM)
4382 regno = current_frame_info.r[reg_fp];
4383 else if (regno == current_frame_info.r[reg_fp])
4384 regno = HARD_FRAME_POINTER_REGNUM;
4387 if (IN_REGNO_P (regno))
4388 return 32 + regno - IN_REG (0);
4389 else if (LOC_REGNO_P (regno))
4390 return 32 + current_frame_info.n_input_regs + regno - LOC_REG (0);
4391 else if (OUT_REGNO_P (regno))
4392 return (32 + current_frame_info.n_input_regs
4393 + current_frame_info.n_local_regs + regno - OUT_REG (0));
4394 else
4395 return regno;
4398 /* Implement TARGET_TRAMPOLINE_INIT.
4400 The trampoline should set the static chain pointer to value placed
4401 into the trampoline and should branch to the specified routine.
4402 To make the normal indirect-subroutine calling convention work,
4403 the trampoline must look like a function descriptor; the first
4404 word being the target address and the second being the target's
4405 global pointer.
4407 We abuse the concept of a global pointer by arranging for it
4408 to point to the data we need to load. The complete trampoline
4409 has the following form:
4411 +-------------------+ \
4412 TRAMP: | __ia64_trampoline | |
4413 +-------------------+ > fake function descriptor
4414 | TRAMP+16 | |
4415 +-------------------+ /
4416 | target descriptor |
4417 +-------------------+
4418 | static link |
4419 +-------------------+
4422 static void
4423 ia64_trampoline_init (rtx m_tramp, tree fndecl, rtx static_chain)
4425 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
4426 rtx addr, addr_reg, tramp, eight = GEN_INT (8);
4428 /* The Intel assembler requires that the global __ia64_trampoline symbol
4429 be declared explicitly */
4430 if (!TARGET_GNU_AS)
4432 static bool declared_ia64_trampoline = false;
4434 if (!declared_ia64_trampoline)
4436 declared_ia64_trampoline = true;
4437 (*targetm.asm_out.globalize_label) (asm_out_file,
4438 "__ia64_trampoline");
4442 /* Make sure addresses are Pmode even if we are in ILP32 mode. */
4443 addr = convert_memory_address (Pmode, XEXP (m_tramp, 0));
4444 fnaddr = convert_memory_address (Pmode, fnaddr);
4445 static_chain = convert_memory_address (Pmode, static_chain);
4447 /* Load up our iterator. */
4448 addr_reg = copy_to_reg (addr);
4449 m_tramp = adjust_automodify_address (m_tramp, Pmode, addr_reg, 0);
4451 /* The first two words are the fake descriptor:
4452 __ia64_trampoline, ADDR+16. */
4453 tramp = gen_rtx_SYMBOL_REF (Pmode, "__ia64_trampoline");
4454 if (TARGET_ABI_OPEN_VMS)
4456 /* HP decided to break the ELF ABI on VMS (to deal with an ambiguity
4457 in the Macro-32 compiler) and changed the semantics of the LTOFF22
4458 relocation against function symbols to make it identical to the
4459 LTOFF_FPTR22 relocation. Emit the latter directly to stay within
4460 strict ELF and dereference to get the bare code address. */
4461 rtx reg = gen_reg_rtx (Pmode);
4462 SYMBOL_REF_FLAGS (tramp) |= SYMBOL_FLAG_FUNCTION;
4463 emit_move_insn (reg, tramp);
4464 emit_move_insn (reg, gen_rtx_MEM (Pmode, reg));
4465 tramp = reg;
4467 emit_move_insn (m_tramp, tramp);
4468 emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
4469 m_tramp = adjust_automodify_address (m_tramp, VOIDmode, NULL, 8);
4471 emit_move_insn (m_tramp, force_reg (Pmode, plus_constant (Pmode, addr, 16)));
4472 emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
4473 m_tramp = adjust_automodify_address (m_tramp, VOIDmode, NULL, 8);
4475 /* The third word is the target descriptor. */
4476 emit_move_insn (m_tramp, force_reg (Pmode, fnaddr));
4477 emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
4478 m_tramp = adjust_automodify_address (m_tramp, VOIDmode, NULL, 8);
4480 /* The fourth word is the static chain. */
4481 emit_move_insn (m_tramp, static_chain);
4484 /* Do any needed setup for a variadic function. CUM has not been updated
4485 for the last named argument which has type TYPE and mode MODE.
4487 We generate the actual spill instructions during prologue generation. */
4489 static void
4490 ia64_setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
4491 tree type, int * pretend_size,
4492 int second_time ATTRIBUTE_UNUSED)
4494 CUMULATIVE_ARGS next_cum = *get_cumulative_args (cum);
4496 /* Skip the current argument. */
4497 ia64_function_arg_advance (pack_cumulative_args (&next_cum), mode, type, 1);
4499 if (next_cum.words < MAX_ARGUMENT_SLOTS)
4501 int n = MAX_ARGUMENT_SLOTS - next_cum.words;
4502 *pretend_size = n * UNITS_PER_WORD;
4503 cfun->machine->n_varargs = n;
4507 /* Check whether TYPE is a homogeneous floating point aggregate. If
4508 it is, return the mode of the floating point type that appears
4509 in all leafs. If it is not, return VOIDmode.
4511 An aggregate is a homogeneous floating point aggregate is if all
4512 fields/elements in it have the same floating point type (e.g,
4513 SFmode). 128-bit quad-precision floats are excluded.
4515 Variable sized aggregates should never arrive here, since we should
4516 have already decided to pass them by reference. Top-level zero-sized
4517 aggregates are excluded because our parallels crash the middle-end. */
4519 static enum machine_mode
4520 hfa_element_mode (const_tree type, bool nested)
4522 enum machine_mode element_mode = VOIDmode;
4523 enum machine_mode mode;
4524 enum tree_code code = TREE_CODE (type);
4525 int know_element_mode = 0;
4526 tree t;
4528 if (!nested && (!TYPE_SIZE (type) || integer_zerop (TYPE_SIZE (type))))
4529 return VOIDmode;
4531 switch (code)
4533 case VOID_TYPE: case INTEGER_TYPE: case ENUMERAL_TYPE:
4534 case BOOLEAN_TYPE: case POINTER_TYPE:
4535 case OFFSET_TYPE: case REFERENCE_TYPE: case METHOD_TYPE:
4536 case LANG_TYPE: case FUNCTION_TYPE:
4537 return VOIDmode;
4539 /* Fortran complex types are supposed to be HFAs, so we need to handle
4540 gcc's COMPLEX_TYPEs as HFAs. We need to exclude the integral complex
4541 types though. */
4542 case COMPLEX_TYPE:
4543 if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_COMPLEX_FLOAT
4544 && TYPE_MODE (type) != TCmode)
4545 return GET_MODE_INNER (TYPE_MODE (type));
4546 else
4547 return VOIDmode;
4549 case REAL_TYPE:
4550 /* We want to return VOIDmode for raw REAL_TYPEs, but the actual
4551 mode if this is contained within an aggregate. */
4552 if (nested && TYPE_MODE (type) != TFmode)
4553 return TYPE_MODE (type);
4554 else
4555 return VOIDmode;
4557 case ARRAY_TYPE:
4558 return hfa_element_mode (TREE_TYPE (type), 1);
4560 case RECORD_TYPE:
4561 case UNION_TYPE:
4562 case QUAL_UNION_TYPE:
4563 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
4565 if (TREE_CODE (t) != FIELD_DECL)
4566 continue;
4568 mode = hfa_element_mode (TREE_TYPE (t), 1);
4569 if (know_element_mode)
4571 if (mode != element_mode)
4572 return VOIDmode;
4574 else if (GET_MODE_CLASS (mode) != MODE_FLOAT)
4575 return VOIDmode;
4576 else
4578 know_element_mode = 1;
4579 element_mode = mode;
4582 return element_mode;
4584 default:
4585 /* If we reach here, we probably have some front-end specific type
4586 that the backend doesn't know about. This can happen via the
4587 aggregate_value_p call in init_function_start. All we can do is
4588 ignore unknown tree types. */
4589 return VOIDmode;
4592 return VOIDmode;
4595 /* Return the number of words required to hold a quantity of TYPE and MODE
4596 when passed as an argument. */
4597 static int
4598 ia64_function_arg_words (const_tree type, enum machine_mode mode)
4600 int words;
4602 if (mode == BLKmode)
4603 words = int_size_in_bytes (type);
4604 else
4605 words = GET_MODE_SIZE (mode);
4607 return (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD; /* round up */
4610 /* Return the number of registers that should be skipped so the current
4611 argument (described by TYPE and WORDS) will be properly aligned.
4613 Integer and float arguments larger than 8 bytes start at the next
4614 even boundary. Aggregates larger than 8 bytes start at the next
4615 even boundary if the aggregate has 16 byte alignment. Note that
4616 in the 32-bit ABI, TImode and TFmode have only 8-byte alignment
4617 but are still to be aligned in registers.
4619 ??? The ABI does not specify how to handle aggregates with
4620 alignment from 9 to 15 bytes, or greater than 16. We handle them
4621 all as if they had 16 byte alignment. Such aggregates can occur
4622 only if gcc extensions are used. */
4623 static int
4624 ia64_function_arg_offset (const CUMULATIVE_ARGS *cum,
4625 const_tree type, int words)
4627 /* No registers are skipped on VMS. */
4628 if (TARGET_ABI_OPEN_VMS || (cum->words & 1) == 0)
4629 return 0;
4631 if (type
4632 && TREE_CODE (type) != INTEGER_TYPE
4633 && TREE_CODE (type) != REAL_TYPE)
4634 return TYPE_ALIGN (type) > 8 * BITS_PER_UNIT;
4635 else
4636 return words > 1;
4639 /* Return rtx for register where argument is passed, or zero if it is passed
4640 on the stack. */
4641 /* ??? 128-bit quad-precision floats are always passed in general
4642 registers. */
4644 static rtx
4645 ia64_function_arg_1 (cumulative_args_t cum_v, enum machine_mode mode,
4646 const_tree type, bool named, bool incoming)
4648 const CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
4650 int basereg = (incoming ? GR_ARG_FIRST : AR_ARG_FIRST);
4651 int words = ia64_function_arg_words (type, mode);
4652 int offset = ia64_function_arg_offset (cum, type, words);
4653 enum machine_mode hfa_mode = VOIDmode;
4655 /* For OPEN VMS, emit the instruction setting up the argument register here,
4656 when we know this will be together with the other arguments setup related
4657 insns. This is not the conceptually best place to do this, but this is
4658 the easiest as we have convenient access to cumulative args info. */
4660 if (TARGET_ABI_OPEN_VMS && mode == VOIDmode && type == void_type_node
4661 && named == 1)
4663 unsigned HOST_WIDE_INT regval = cum->words;
4664 int i;
4666 for (i = 0; i < 8; i++)
4667 regval |= ((int) cum->atypes[i]) << (i * 3 + 8);
4669 emit_move_insn (gen_rtx_REG (DImode, GR_REG (25)),
4670 GEN_INT (regval));
4673 /* If all argument slots are used, then it must go on the stack. */
4674 if (cum->words + offset >= MAX_ARGUMENT_SLOTS)
4675 return 0;
4677 /* On OpenVMS argument is either in Rn or Fn. */
4678 if (TARGET_ABI_OPEN_VMS)
4680 if (FLOAT_MODE_P (mode))
4681 return gen_rtx_REG (mode, FR_ARG_FIRST + cum->words);
4682 else
4683 return gen_rtx_REG (mode, basereg + cum->words);
4686 /* Check for and handle homogeneous FP aggregates. */
4687 if (type)
4688 hfa_mode = hfa_element_mode (type, 0);
4690 /* Unnamed prototyped hfas are passed as usual. Named prototyped hfas
4691 and unprototyped hfas are passed specially. */
4692 if (hfa_mode != VOIDmode && (! cum->prototype || named))
4694 rtx loc[16];
4695 int i = 0;
4696 int fp_regs = cum->fp_regs;
4697 int int_regs = cum->words + offset;
4698 int hfa_size = GET_MODE_SIZE (hfa_mode);
4699 int byte_size;
4700 int args_byte_size;
4702 /* If prototyped, pass it in FR regs then GR regs.
4703 If not prototyped, pass it in both FR and GR regs.
4705 If this is an SFmode aggregate, then it is possible to run out of
4706 FR regs while GR regs are still left. In that case, we pass the
4707 remaining part in the GR regs. */
4709 /* Fill the FP regs. We do this always. We stop if we reach the end
4710 of the argument, the last FP register, or the last argument slot. */
4712 byte_size = ((mode == BLKmode)
4713 ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
4714 args_byte_size = int_regs * UNITS_PER_WORD;
4715 offset = 0;
4716 for (; (offset < byte_size && fp_regs < MAX_ARGUMENT_SLOTS
4717 && args_byte_size < (MAX_ARGUMENT_SLOTS * UNITS_PER_WORD)); i++)
4719 loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
4720 gen_rtx_REG (hfa_mode, (FR_ARG_FIRST
4721 + fp_regs)),
4722 GEN_INT (offset));
4723 offset += hfa_size;
4724 args_byte_size += hfa_size;
4725 fp_regs++;
4728 /* If no prototype, then the whole thing must go in GR regs. */
4729 if (! cum->prototype)
4730 offset = 0;
4731 /* If this is an SFmode aggregate, then we might have some left over
4732 that needs to go in GR regs. */
4733 else if (byte_size != offset)
4734 int_regs += offset / UNITS_PER_WORD;
4736 /* Fill in the GR regs. We must use DImode here, not the hfa mode. */
4738 for (; offset < byte_size && int_regs < MAX_ARGUMENT_SLOTS; i++)
4740 enum machine_mode gr_mode = DImode;
4741 unsigned int gr_size;
4743 /* If we have an odd 4 byte hunk because we ran out of FR regs,
4744 then this goes in a GR reg left adjusted/little endian, right
4745 adjusted/big endian. */
4746 /* ??? Currently this is handled wrong, because 4-byte hunks are
4747 always right adjusted/little endian. */
4748 if (offset & 0x4)
4749 gr_mode = SImode;
4750 /* If we have an even 4 byte hunk because the aggregate is a
4751 multiple of 4 bytes in size, then this goes in a GR reg right
4752 adjusted/little endian. */
4753 else if (byte_size - offset == 4)
4754 gr_mode = SImode;
4756 loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
4757 gen_rtx_REG (gr_mode, (basereg
4758 + int_regs)),
4759 GEN_INT (offset));
4761 gr_size = GET_MODE_SIZE (gr_mode);
4762 offset += gr_size;
4763 if (gr_size == UNITS_PER_WORD
4764 || (gr_size < UNITS_PER_WORD && offset % UNITS_PER_WORD == 0))
4765 int_regs++;
4766 else if (gr_size > UNITS_PER_WORD)
4767 int_regs += gr_size / UNITS_PER_WORD;
4769 return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
4772 /* Integral and aggregates go in general registers. If we have run out of
4773 FR registers, then FP values must also go in general registers. This can
4774 happen when we have a SFmode HFA. */
4775 else if (mode == TFmode || mode == TCmode
4776 || (! FLOAT_MODE_P (mode) || cum->fp_regs == MAX_ARGUMENT_SLOTS))
4778 int byte_size = ((mode == BLKmode)
4779 ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
4780 if (BYTES_BIG_ENDIAN
4781 && (mode == BLKmode || (type && AGGREGATE_TYPE_P (type)))
4782 && byte_size < UNITS_PER_WORD
4783 && byte_size > 0)
4785 rtx gr_reg = gen_rtx_EXPR_LIST (VOIDmode,
4786 gen_rtx_REG (DImode,
4787 (basereg + cum->words
4788 + offset)),
4789 const0_rtx);
4790 return gen_rtx_PARALLEL (mode, gen_rtvec (1, gr_reg));
4792 else
4793 return gen_rtx_REG (mode, basereg + cum->words + offset);
4797 /* If there is a prototype, then FP values go in a FR register when
4798 named, and in a GR register when unnamed. */
4799 else if (cum->prototype)
4801 if (named)
4802 return gen_rtx_REG (mode, FR_ARG_FIRST + cum->fp_regs);
4803 /* In big-endian mode, an anonymous SFmode value must be represented
4804 as (parallel:SF [(expr_list (reg:DI n) (const_int 0))]) to force
4805 the value into the high half of the general register. */
4806 else if (BYTES_BIG_ENDIAN && mode == SFmode)
4807 return gen_rtx_PARALLEL (mode,
4808 gen_rtvec (1,
4809 gen_rtx_EXPR_LIST (VOIDmode,
4810 gen_rtx_REG (DImode, basereg + cum->words + offset),
4811 const0_rtx)));
4812 else
4813 return gen_rtx_REG (mode, basereg + cum->words + offset);
4815 /* If there is no prototype, then FP values go in both FR and GR
4816 registers. */
4817 else
4819 /* See comment above. */
4820 enum machine_mode inner_mode =
4821 (BYTES_BIG_ENDIAN && mode == SFmode) ? DImode : mode;
4823 rtx fp_reg = gen_rtx_EXPR_LIST (VOIDmode,
4824 gen_rtx_REG (mode, (FR_ARG_FIRST
4825 + cum->fp_regs)),
4826 const0_rtx);
4827 rtx gr_reg = gen_rtx_EXPR_LIST (VOIDmode,
4828 gen_rtx_REG (inner_mode,
4829 (basereg + cum->words
4830 + offset)),
4831 const0_rtx);
4833 return gen_rtx_PARALLEL (mode, gen_rtvec (2, fp_reg, gr_reg));
4837 /* Implement TARGET_FUNCION_ARG target hook. */
4839 static rtx
4840 ia64_function_arg (cumulative_args_t cum, enum machine_mode mode,
4841 const_tree type, bool named)
4843 return ia64_function_arg_1 (cum, mode, type, named, false);
4846 /* Implement TARGET_FUNCION_INCOMING_ARG target hook. */
4848 static rtx
4849 ia64_function_incoming_arg (cumulative_args_t cum,
4850 enum machine_mode mode,
4851 const_tree type, bool named)
4853 return ia64_function_arg_1 (cum, mode, type, named, true);
4856 /* Return number of bytes, at the beginning of the argument, that must be
4857 put in registers. 0 is the argument is entirely in registers or entirely
4858 in memory. */
4860 static int
4861 ia64_arg_partial_bytes (cumulative_args_t cum_v, enum machine_mode mode,
4862 tree type, bool named ATTRIBUTE_UNUSED)
4864 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
4866 int words = ia64_function_arg_words (type, mode);
4867 int offset = ia64_function_arg_offset (cum, type, words);
4869 /* If all argument slots are used, then it must go on the stack. */
4870 if (cum->words + offset >= MAX_ARGUMENT_SLOTS)
4871 return 0;
4873 /* It doesn't matter whether the argument goes in FR or GR regs. If
4874 it fits within the 8 argument slots, then it goes entirely in
4875 registers. If it extends past the last argument slot, then the rest
4876 goes on the stack. */
4878 if (words + cum->words + offset <= MAX_ARGUMENT_SLOTS)
4879 return 0;
4881 return (MAX_ARGUMENT_SLOTS - cum->words - offset) * UNITS_PER_WORD;
4884 /* Return ivms_arg_type based on machine_mode. */
4886 static enum ivms_arg_type
4887 ia64_arg_type (enum machine_mode mode)
4889 switch (mode)
4891 case SFmode:
4892 return FS;
4893 case DFmode:
4894 return FT;
4895 default:
4896 return I64;
4900 /* Update CUM to point after this argument. This is patterned after
4901 ia64_function_arg. */
4903 static void
4904 ia64_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
4905 const_tree type, bool named)
4907 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
4908 int words = ia64_function_arg_words (type, mode);
4909 int offset = ia64_function_arg_offset (cum, type, words);
4910 enum machine_mode hfa_mode = VOIDmode;
4912 /* If all arg slots are already full, then there is nothing to do. */
4913 if (cum->words >= MAX_ARGUMENT_SLOTS)
4915 cum->words += words + offset;
4916 return;
4919 cum->atypes[cum->words] = ia64_arg_type (mode);
4920 cum->words += words + offset;
4922 /* On OpenVMS argument is either in Rn or Fn. */
4923 if (TARGET_ABI_OPEN_VMS)
4925 cum->int_regs = cum->words;
4926 cum->fp_regs = cum->words;
4927 return;
4930 /* Check for and handle homogeneous FP aggregates. */
4931 if (type)
4932 hfa_mode = hfa_element_mode (type, 0);
4934 /* Unnamed prototyped hfas are passed as usual. Named prototyped hfas
4935 and unprototyped hfas are passed specially. */
4936 if (hfa_mode != VOIDmode && (! cum->prototype || named))
4938 int fp_regs = cum->fp_regs;
4939 /* This is the original value of cum->words + offset. */
4940 int int_regs = cum->words - words;
4941 int hfa_size = GET_MODE_SIZE (hfa_mode);
4942 int byte_size;
4943 int args_byte_size;
4945 /* If prototyped, pass it in FR regs then GR regs.
4946 If not prototyped, pass it in both FR and GR regs.
4948 If this is an SFmode aggregate, then it is possible to run out of
4949 FR regs while GR regs are still left. In that case, we pass the
4950 remaining part in the GR regs. */
4952 /* Fill the FP regs. We do this always. We stop if we reach the end
4953 of the argument, the last FP register, or the last argument slot. */
4955 byte_size = ((mode == BLKmode)
4956 ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
4957 args_byte_size = int_regs * UNITS_PER_WORD;
4958 offset = 0;
4959 for (; (offset < byte_size && fp_regs < MAX_ARGUMENT_SLOTS
4960 && args_byte_size < (MAX_ARGUMENT_SLOTS * UNITS_PER_WORD));)
4962 offset += hfa_size;
4963 args_byte_size += hfa_size;
4964 fp_regs++;
4967 cum->fp_regs = fp_regs;
4970 /* Integral and aggregates go in general registers. So do TFmode FP values.
4971 If we have run out of FR registers, then other FP values must also go in
4972 general registers. This can happen when we have a SFmode HFA. */
4973 else if (mode == TFmode || mode == TCmode
4974 || (! FLOAT_MODE_P (mode) || cum->fp_regs == MAX_ARGUMENT_SLOTS))
4975 cum->int_regs = cum->words;
4977 /* If there is a prototype, then FP values go in a FR register when
4978 named, and in a GR register when unnamed. */
4979 else if (cum->prototype)
4981 if (! named)
4982 cum->int_regs = cum->words;
4983 else
4984 /* ??? Complex types should not reach here. */
4985 cum->fp_regs += (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT ? 2 : 1);
4987 /* If there is no prototype, then FP values go in both FR and GR
4988 registers. */
4989 else
4991 /* ??? Complex types should not reach here. */
4992 cum->fp_regs += (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT ? 2 : 1);
4993 cum->int_regs = cum->words;
4997 /* Arguments with alignment larger than 8 bytes start at the next even
4998 boundary. On ILP32 HPUX, TFmode arguments start on next even boundary
4999 even though their normal alignment is 8 bytes. See ia64_function_arg. */
5001 static unsigned int
5002 ia64_function_arg_boundary (enum machine_mode mode, const_tree type)
5004 if (mode == TFmode && TARGET_HPUX && TARGET_ILP32)
5005 return PARM_BOUNDARY * 2;
5007 if (type)
5009 if (TYPE_ALIGN (type) > PARM_BOUNDARY)
5010 return PARM_BOUNDARY * 2;
5011 else
5012 return PARM_BOUNDARY;
5015 if (GET_MODE_BITSIZE (mode) > PARM_BOUNDARY)
5016 return PARM_BOUNDARY * 2;
5017 else
5018 return PARM_BOUNDARY;
5021 /* True if it is OK to do sibling call optimization for the specified
5022 call expression EXP. DECL will be the called function, or NULL if
5023 this is an indirect call. */
5024 static bool
5025 ia64_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
5027 /* We can't perform a sibcall if the current function has the syscall_linkage
5028 attribute. */
5029 if (lookup_attribute ("syscall_linkage",
5030 TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
5031 return false;
5033 /* We must always return with our current GP. This means we can
5034 only sibcall to functions defined in the current module unless
5035 TARGET_CONST_GP is set to true. */
5036 return (decl && (*targetm.binds_local_p) (decl)) || TARGET_CONST_GP;
5040 /* Implement va_arg. */
5042 static tree
5043 ia64_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
5044 gimple_seq *post_p)
5046 /* Variable sized types are passed by reference. */
5047 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
5049 tree ptrtype = build_pointer_type (type);
5050 tree addr = std_gimplify_va_arg_expr (valist, ptrtype, pre_p, post_p);
5051 return build_va_arg_indirect_ref (addr);
5054 /* Aggregate arguments with alignment larger than 8 bytes start at
5055 the next even boundary. Integer and floating point arguments
5056 do so if they are larger than 8 bytes, whether or not they are
5057 also aligned larger than 8 bytes. */
5058 if ((TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == INTEGER_TYPE)
5059 ? int_size_in_bytes (type) > 8 : TYPE_ALIGN (type) > 8 * BITS_PER_UNIT)
5061 tree t = fold_build_pointer_plus_hwi (valist, 2 * UNITS_PER_WORD - 1);
5062 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
5063 build_int_cst (TREE_TYPE (t), -2 * UNITS_PER_WORD));
5064 gimplify_assign (unshare_expr (valist), t, pre_p);
5067 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
5070 /* Return 1 if function return value returned in memory. Return 0 if it is
5071 in a register. */
5073 static bool
5074 ia64_return_in_memory (const_tree valtype, const_tree fntype ATTRIBUTE_UNUSED)
5076 enum machine_mode mode;
5077 enum machine_mode hfa_mode;
5078 HOST_WIDE_INT byte_size;
5080 mode = TYPE_MODE (valtype);
5081 byte_size = GET_MODE_SIZE (mode);
5082 if (mode == BLKmode)
5084 byte_size = int_size_in_bytes (valtype);
5085 if (byte_size < 0)
5086 return true;
5089 /* Hfa's with up to 8 elements are returned in the FP argument registers. */
5091 hfa_mode = hfa_element_mode (valtype, 0);
5092 if (hfa_mode != VOIDmode)
5094 int hfa_size = GET_MODE_SIZE (hfa_mode);
5096 if (byte_size / hfa_size > MAX_ARGUMENT_SLOTS)
5097 return true;
5098 else
5099 return false;
5101 else if (byte_size > UNITS_PER_WORD * MAX_INT_RETURN_SLOTS)
5102 return true;
5103 else
5104 return false;
5107 /* Return rtx for register that holds the function return value. */
5109 static rtx
5110 ia64_function_value (const_tree valtype,
5111 const_tree fn_decl_or_type,
5112 bool outgoing ATTRIBUTE_UNUSED)
5114 enum machine_mode mode;
5115 enum machine_mode hfa_mode;
5116 int unsignedp;
5117 const_tree func = fn_decl_or_type;
5119 if (fn_decl_or_type
5120 && !DECL_P (fn_decl_or_type))
5121 func = NULL;
5123 mode = TYPE_MODE (valtype);
5124 hfa_mode = hfa_element_mode (valtype, 0);
5126 if (hfa_mode != VOIDmode)
5128 rtx loc[8];
5129 int i;
5130 int hfa_size;
5131 int byte_size;
5132 int offset;
5134 hfa_size = GET_MODE_SIZE (hfa_mode);
5135 byte_size = ((mode == BLKmode)
5136 ? int_size_in_bytes (valtype) : GET_MODE_SIZE (mode));
5137 offset = 0;
5138 for (i = 0; offset < byte_size; i++)
5140 loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
5141 gen_rtx_REG (hfa_mode, FR_ARG_FIRST + i),
5142 GEN_INT (offset));
5143 offset += hfa_size;
5145 return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
5147 else if (FLOAT_TYPE_P (valtype) && mode != TFmode && mode != TCmode)
5148 return gen_rtx_REG (mode, FR_ARG_FIRST);
5149 else
5151 bool need_parallel = false;
5153 /* In big-endian mode, we need to manage the layout of aggregates
5154 in the registers so that we get the bits properly aligned in
5155 the highpart of the registers. */
5156 if (BYTES_BIG_ENDIAN
5157 && (mode == BLKmode || (valtype && AGGREGATE_TYPE_P (valtype))))
5158 need_parallel = true;
5160 /* Something like struct S { long double x; char a[0] } is not an
5161 HFA structure, and therefore doesn't go in fp registers. But
5162 the middle-end will give it XFmode anyway, and XFmode values
5163 don't normally fit in integer registers. So we need to smuggle
5164 the value inside a parallel. */
5165 else if (mode == XFmode || mode == XCmode || mode == RFmode)
5166 need_parallel = true;
5168 if (need_parallel)
5170 rtx loc[8];
5171 int offset;
5172 int bytesize;
5173 int i;
5175 offset = 0;
5176 bytesize = int_size_in_bytes (valtype);
5177 /* An empty PARALLEL is invalid here, but the return value
5178 doesn't matter for empty structs. */
5179 if (bytesize == 0)
5180 return gen_rtx_REG (mode, GR_RET_FIRST);
5181 for (i = 0; offset < bytesize; i++)
5183 loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
5184 gen_rtx_REG (DImode,
5185 GR_RET_FIRST + i),
5186 GEN_INT (offset));
5187 offset += UNITS_PER_WORD;
5189 return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
5192 mode = promote_function_mode (valtype, mode, &unsignedp,
5193 func ? TREE_TYPE (func) : NULL_TREE,
5194 true);
5196 return gen_rtx_REG (mode, GR_RET_FIRST);
5200 /* Worker function for TARGET_LIBCALL_VALUE. */
5202 static rtx
5203 ia64_libcall_value (enum machine_mode mode,
5204 const_rtx fun ATTRIBUTE_UNUSED)
5206 return gen_rtx_REG (mode,
5207 (((GET_MODE_CLASS (mode) == MODE_FLOAT
5208 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5209 && (mode) != TFmode)
5210 ? FR_RET_FIRST : GR_RET_FIRST));
5213 /* Worker function for FUNCTION_VALUE_REGNO_P. */
5215 static bool
5216 ia64_function_value_regno_p (const unsigned int regno)
5218 return ((regno >= GR_RET_FIRST && regno <= GR_RET_LAST)
5219 || (regno >= FR_RET_FIRST && regno <= FR_RET_LAST));
5222 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
5223 We need to emit DTP-relative relocations. */
5225 static void
5226 ia64_output_dwarf_dtprel (FILE *file, int size, rtx x)
5228 gcc_assert (size == 4 || size == 8);
5229 if (size == 4)
5230 fputs ("\tdata4.ua\t@dtprel(", file);
5231 else
5232 fputs ("\tdata8.ua\t@dtprel(", file);
5233 output_addr_const (file, x);
5234 fputs (")", file);
5237 /* Print a memory address as an operand to reference that memory location. */
5239 /* ??? Do we need this? It gets used only for 'a' operands. We could perhaps
5240 also call this from ia64_print_operand for memory addresses. */
5242 static void
5243 ia64_print_operand_address (FILE * stream ATTRIBUTE_UNUSED,
5244 rtx address ATTRIBUTE_UNUSED)
5248 /* Print an operand to an assembler instruction.
5249 C Swap and print a comparison operator.
5250 D Print an FP comparison operator.
5251 E Print 32 - constant, for SImode shifts as extract.
5252 e Print 64 - constant, for DImode rotates.
5253 F A floating point constant 0.0 emitted as f0, or 1.0 emitted as f1, or
5254 a floating point register emitted normally.
5255 G A floating point constant.
5256 I Invert a predicate register by adding 1.
5257 J Select the proper predicate register for a condition.
5258 j Select the inverse predicate register for a condition.
5259 O Append .acq for volatile load.
5260 P Postincrement of a MEM.
5261 Q Append .rel for volatile store.
5262 R Print .s .d or nothing for a single, double or no truncation.
5263 S Shift amount for shladd instruction.
5264 T Print an 8-bit sign extended number (K) as a 32-bit unsigned number
5265 for Intel assembler.
5266 U Print an 8-bit sign extended number (K) as a 64-bit unsigned number
5267 for Intel assembler.
5268 X A pair of floating point registers.
5269 r Print register name, or constant 0 as r0. HP compatibility for
5270 Linux kernel.
5271 v Print vector constant value as an 8-byte integer value. */
5273 static void
5274 ia64_print_operand (FILE * file, rtx x, int code)
5276 const char *str;
5278 switch (code)
5280 case 0:
5281 /* Handled below. */
5282 break;
5284 case 'C':
5286 enum rtx_code c = swap_condition (GET_CODE (x));
5287 fputs (GET_RTX_NAME (c), file);
5288 return;
5291 case 'D':
5292 switch (GET_CODE (x))
5294 case NE:
5295 str = "neq";
5296 break;
5297 case UNORDERED:
5298 str = "unord";
5299 break;
5300 case ORDERED:
5301 str = "ord";
5302 break;
5303 case UNLT:
5304 str = "nge";
5305 break;
5306 case UNLE:
5307 str = "ngt";
5308 break;
5309 case UNGT:
5310 str = "nle";
5311 break;
5312 case UNGE:
5313 str = "nlt";
5314 break;
5315 case UNEQ:
5316 case LTGT:
5317 gcc_unreachable ();
5318 default:
5319 str = GET_RTX_NAME (GET_CODE (x));
5320 break;
5322 fputs (str, file);
5323 return;
5325 case 'E':
5326 fprintf (file, HOST_WIDE_INT_PRINT_DEC, 32 - INTVAL (x));
5327 return;
5329 case 'e':
5330 fprintf (file, HOST_WIDE_INT_PRINT_DEC, 64 - INTVAL (x));
5331 return;
5333 case 'F':
5334 if (x == CONST0_RTX (GET_MODE (x)))
5335 str = reg_names [FR_REG (0)];
5336 else if (x == CONST1_RTX (GET_MODE (x)))
5337 str = reg_names [FR_REG (1)];
5338 else
5340 gcc_assert (GET_CODE (x) == REG);
5341 str = reg_names [REGNO (x)];
5343 fputs (str, file);
5344 return;
5346 case 'G':
5348 long val[4];
5349 REAL_VALUE_TYPE rv;
5350 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
5351 real_to_target (val, &rv, GET_MODE (x));
5352 if (GET_MODE (x) == SFmode)
5353 fprintf (file, "0x%08lx", val[0] & 0xffffffff);
5354 else if (GET_MODE (x) == DFmode)
5355 fprintf (file, "0x%08lx%08lx", (WORDS_BIG_ENDIAN ? val[0] : val[1])
5356 & 0xffffffff,
5357 (WORDS_BIG_ENDIAN ? val[1] : val[0])
5358 & 0xffffffff);
5359 else
5360 output_operand_lossage ("invalid %%G mode");
5362 return;
5364 case 'I':
5365 fputs (reg_names [REGNO (x) + 1], file);
5366 return;
5368 case 'J':
5369 case 'j':
5371 unsigned int regno = REGNO (XEXP (x, 0));
5372 if (GET_CODE (x) == EQ)
5373 regno += 1;
5374 if (code == 'j')
5375 regno ^= 1;
5376 fputs (reg_names [regno], file);
5378 return;
5380 case 'O':
5381 if (MEM_VOLATILE_P (x))
5382 fputs(".acq", file);
5383 return;
5385 case 'P':
5387 HOST_WIDE_INT value;
5389 switch (GET_CODE (XEXP (x, 0)))
5391 default:
5392 return;
5394 case POST_MODIFY:
5395 x = XEXP (XEXP (XEXP (x, 0), 1), 1);
5396 if (GET_CODE (x) == CONST_INT)
5397 value = INTVAL (x);
5398 else
5400 gcc_assert (GET_CODE (x) == REG);
5401 fprintf (file, ", %s", reg_names[REGNO (x)]);
5402 return;
5404 break;
5406 case POST_INC:
5407 value = GET_MODE_SIZE (GET_MODE (x));
5408 break;
5410 case POST_DEC:
5411 value = - (HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (x));
5412 break;
5415 fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC, value);
5416 return;
5419 case 'Q':
5420 if (MEM_VOLATILE_P (x))
5421 fputs(".rel", file);
5422 return;
5424 case 'R':
5425 if (x == CONST0_RTX (GET_MODE (x)))
5426 fputs(".s", file);
5427 else if (x == CONST1_RTX (GET_MODE (x)))
5428 fputs(".d", file);
5429 else if (x == CONST2_RTX (GET_MODE (x)))
5431 else
5432 output_operand_lossage ("invalid %%R value");
5433 return;
5435 case 'S':
5436 fprintf (file, "%d", exact_log2 (INTVAL (x)));
5437 return;
5439 case 'T':
5440 if (! TARGET_GNU_AS && GET_CODE (x) == CONST_INT)
5442 fprintf (file, "0x%x", (int) INTVAL (x) & 0xffffffff);
5443 return;
5445 break;
5447 case 'U':
5448 if (! TARGET_GNU_AS && GET_CODE (x) == CONST_INT)
5450 const char *prefix = "0x";
5451 if (INTVAL (x) & 0x80000000)
5453 fprintf (file, "0xffffffff");
5454 prefix = "";
5456 fprintf (file, "%s%x", prefix, (int) INTVAL (x) & 0xffffffff);
5457 return;
5459 break;
5461 case 'X':
5463 unsigned int regno = REGNO (x);
5464 fprintf (file, "%s, %s", reg_names [regno], reg_names [regno + 1]);
5466 return;
5468 case 'r':
5469 /* If this operand is the constant zero, write it as register zero.
5470 Any register, zero, or CONST_INT value is OK here. */
5471 if (GET_CODE (x) == REG)
5472 fputs (reg_names[REGNO (x)], file);
5473 else if (x == CONST0_RTX (GET_MODE (x)))
5474 fputs ("r0", file);
5475 else if (GET_CODE (x) == CONST_INT)
5476 output_addr_const (file, x);
5477 else
5478 output_operand_lossage ("invalid %%r value");
5479 return;
5481 case 'v':
5482 gcc_assert (GET_CODE (x) == CONST_VECTOR);
5483 x = simplify_subreg (DImode, x, GET_MODE (x), 0);
5484 break;
5486 case '+':
5488 const char *which;
5490 /* For conditional branches, returns or calls, substitute
5491 sptk, dptk, dpnt, or spnt for %s. */
5492 x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
5493 if (x)
5495 int pred_val = XINT (x, 0);
5497 /* Guess top and bottom 10% statically predicted. */
5498 if (pred_val < REG_BR_PROB_BASE / 50
5499 && br_prob_note_reliable_p (x))
5500 which = ".spnt";
5501 else if (pred_val < REG_BR_PROB_BASE / 2)
5502 which = ".dpnt";
5503 else if (pred_val < REG_BR_PROB_BASE / 100 * 98
5504 || !br_prob_note_reliable_p (x))
5505 which = ".dptk";
5506 else
5507 which = ".sptk";
5509 else if (CALL_P (current_output_insn))
5510 which = ".sptk";
5511 else
5512 which = ".dptk";
5514 fputs (which, file);
5515 return;
5518 case ',':
5519 x = current_insn_predicate;
5520 if (x)
5522 unsigned int regno = REGNO (XEXP (x, 0));
5523 if (GET_CODE (x) == EQ)
5524 regno += 1;
5525 fprintf (file, "(%s) ", reg_names [regno]);
5527 return;
5529 default:
5530 output_operand_lossage ("ia64_print_operand: unknown code");
5531 return;
5534 switch (GET_CODE (x))
5536 /* This happens for the spill/restore instructions. */
5537 case POST_INC:
5538 case POST_DEC:
5539 case POST_MODIFY:
5540 x = XEXP (x, 0);
5541 /* ... fall through ... */
5543 case REG:
5544 fputs (reg_names [REGNO (x)], file);
5545 break;
5547 case MEM:
5549 rtx addr = XEXP (x, 0);
5550 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
5551 addr = XEXP (addr, 0);
5552 fprintf (file, "[%s]", reg_names [REGNO (addr)]);
5553 break;
5556 default:
5557 output_addr_const (file, x);
5558 break;
5561 return;
5564 /* Worker function for TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
5566 static bool
5567 ia64_print_operand_punct_valid_p (unsigned char code)
5569 return (code == '+' || code == ',');
5572 /* Compute a (partial) cost for rtx X. Return true if the complete
5573 cost has been computed, and false if subexpressions should be
5574 scanned. In either case, *TOTAL contains the cost result. */
5575 /* ??? This is incomplete. */
5577 static bool
5578 ia64_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
5579 int *total, bool speed ATTRIBUTE_UNUSED)
5581 switch (code)
5583 case CONST_INT:
5584 switch (outer_code)
5586 case SET:
5587 *total = satisfies_constraint_J (x) ? 0 : COSTS_N_INSNS (1);
5588 return true;
5589 case PLUS:
5590 if (satisfies_constraint_I (x))
5591 *total = 0;
5592 else if (satisfies_constraint_J (x))
5593 *total = 1;
5594 else
5595 *total = COSTS_N_INSNS (1);
5596 return true;
5597 default:
5598 if (satisfies_constraint_K (x) || satisfies_constraint_L (x))
5599 *total = 0;
5600 else
5601 *total = COSTS_N_INSNS (1);
5602 return true;
5605 case CONST_DOUBLE:
5606 *total = COSTS_N_INSNS (1);
5607 return true;
5609 case CONST:
5610 case SYMBOL_REF:
5611 case LABEL_REF:
5612 *total = COSTS_N_INSNS (3);
5613 return true;
5615 case FMA:
5616 *total = COSTS_N_INSNS (4);
5617 return true;
5619 case MULT:
5620 /* For multiplies wider than HImode, we have to go to the FPU,
5621 which normally involves copies. Plus there's the latency
5622 of the multiply itself, and the latency of the instructions to
5623 transfer integer regs to FP regs. */
5624 if (FLOAT_MODE_P (GET_MODE (x)))
5625 *total = COSTS_N_INSNS (4);
5626 else if (GET_MODE_SIZE (GET_MODE (x)) > 2)
5627 *total = COSTS_N_INSNS (10);
5628 else
5629 *total = COSTS_N_INSNS (2);
5630 return true;
5632 case PLUS:
5633 case MINUS:
5634 if (FLOAT_MODE_P (GET_MODE (x)))
5636 *total = COSTS_N_INSNS (4);
5637 return true;
5639 /* FALLTHRU */
5641 case ASHIFT:
5642 case ASHIFTRT:
5643 case LSHIFTRT:
5644 *total = COSTS_N_INSNS (1);
5645 return true;
5647 case DIV:
5648 case UDIV:
5649 case MOD:
5650 case UMOD:
5651 /* We make divide expensive, so that divide-by-constant will be
5652 optimized to a multiply. */
5653 *total = COSTS_N_INSNS (60);
5654 return true;
5656 default:
5657 return false;
5661 /* Calculate the cost of moving data from a register in class FROM to
5662 one in class TO, using MODE. */
5664 static int
5665 ia64_register_move_cost (enum machine_mode mode, reg_class_t from,
5666 reg_class_t to)
5668 /* ADDL_REGS is the same as GR_REGS for movement purposes. */
5669 if (to == ADDL_REGS)
5670 to = GR_REGS;
5671 if (from == ADDL_REGS)
5672 from = GR_REGS;
5674 /* All costs are symmetric, so reduce cases by putting the
5675 lower number class as the destination. */
5676 if (from < to)
5678 reg_class_t tmp = to;
5679 to = from, from = tmp;
5682 /* Moving from FR<->GR in XFmode must be more expensive than 2,
5683 so that we get secondary memory reloads. Between FR_REGS,
5684 we have to make this at least as expensive as memory_move_cost
5685 to avoid spectacularly poor register class preferencing. */
5686 if (mode == XFmode || mode == RFmode)
5688 if (to != GR_REGS || from != GR_REGS)
5689 return memory_move_cost (mode, to, false);
5690 else
5691 return 3;
5694 switch (to)
5696 case PR_REGS:
5697 /* Moving between PR registers takes two insns. */
5698 if (from == PR_REGS)
5699 return 3;
5700 /* Moving between PR and anything but GR is impossible. */
5701 if (from != GR_REGS)
5702 return memory_move_cost (mode, to, false);
5703 break;
5705 case BR_REGS:
5706 /* Moving between BR and anything but GR is impossible. */
5707 if (from != GR_REGS && from != GR_AND_BR_REGS)
5708 return memory_move_cost (mode, to, false);
5709 break;
5711 case AR_I_REGS:
5712 case AR_M_REGS:
5713 /* Moving between AR and anything but GR is impossible. */
5714 if (from != GR_REGS)
5715 return memory_move_cost (mode, to, false);
5716 break;
5718 case GR_REGS:
5719 case FR_REGS:
5720 case FP_REGS:
5721 case GR_AND_FR_REGS:
5722 case GR_AND_BR_REGS:
5723 case ALL_REGS:
5724 break;
5726 default:
5727 gcc_unreachable ();
5730 return 2;
5733 /* Calculate the cost of moving data of MODE from a register to or from
5734 memory. */
5736 static int
5737 ia64_memory_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
5738 reg_class_t rclass,
5739 bool in ATTRIBUTE_UNUSED)
5741 if (rclass == GENERAL_REGS
5742 || rclass == FR_REGS
5743 || rclass == FP_REGS
5744 || rclass == GR_AND_FR_REGS)
5745 return 4;
5746 else
5747 return 10;
5750 /* Implement TARGET_PREFERRED_RELOAD_CLASS. Place additional restrictions
5751 on RCLASS to use when copying X into that class. */
5753 static reg_class_t
5754 ia64_preferred_reload_class (rtx x, reg_class_t rclass)
5756 switch (rclass)
5758 case FR_REGS:
5759 case FP_REGS:
5760 /* Don't allow volatile mem reloads into floating point registers.
5761 This is defined to force reload to choose the r/m case instead
5762 of the f/f case when reloading (set (reg fX) (mem/v)). */
5763 if (MEM_P (x) && MEM_VOLATILE_P (x))
5764 return NO_REGS;
5766 /* Force all unrecognized constants into the constant pool. */
5767 if (CONSTANT_P (x))
5768 return NO_REGS;
5769 break;
5771 case AR_M_REGS:
5772 case AR_I_REGS:
5773 if (!OBJECT_P (x))
5774 return NO_REGS;
5775 break;
5777 default:
5778 break;
5781 return rclass;
5784 /* This function returns the register class required for a secondary
5785 register when copying between one of the registers in RCLASS, and X,
5786 using MODE. A return value of NO_REGS means that no secondary register
5787 is required. */
5789 enum reg_class
5790 ia64_secondary_reload_class (enum reg_class rclass,
5791 enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
5793 int regno = -1;
5795 if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
5796 regno = true_regnum (x);
5798 switch (rclass)
5800 case BR_REGS:
5801 case AR_M_REGS:
5802 case AR_I_REGS:
5803 /* ??? BR<->BR register copies can happen due to a bad gcse/cse/global
5804 interaction. We end up with two pseudos with overlapping lifetimes
5805 both of which are equiv to the same constant, and both which need
5806 to be in BR_REGS. This seems to be a cse bug. cse_basic_block_end
5807 changes depending on the path length, which means the qty_first_reg
5808 check in make_regs_eqv can give different answers at different times.
5809 At some point I'll probably need a reload_indi pattern to handle
5810 this.
5812 We can also get GR_AND_FR_REGS to BR_REGS/AR_REGS copies, where we
5813 wound up with a FP register from GR_AND_FR_REGS. Extend that to all
5814 non-general registers for good measure. */
5815 if (regno >= 0 && ! GENERAL_REGNO_P (regno))
5816 return GR_REGS;
5818 /* This is needed if a pseudo used as a call_operand gets spilled to a
5819 stack slot. */
5820 if (GET_CODE (x) == MEM)
5821 return GR_REGS;
5822 break;
5824 case FR_REGS:
5825 case FP_REGS:
5826 /* Need to go through general registers to get to other class regs. */
5827 if (regno >= 0 && ! (FR_REGNO_P (regno) || GENERAL_REGNO_P (regno)))
5828 return GR_REGS;
5830 /* This can happen when a paradoxical subreg is an operand to the
5831 muldi3 pattern. */
5832 /* ??? This shouldn't be necessary after instruction scheduling is
5833 enabled, because paradoxical subregs are not accepted by
5834 register_operand when INSN_SCHEDULING is defined. Or alternatively,
5835 stop the paradoxical subreg stupidity in the *_operand functions
5836 in recog.c. */
5837 if (GET_CODE (x) == MEM
5838 && (GET_MODE (x) == SImode || GET_MODE (x) == HImode
5839 || GET_MODE (x) == QImode))
5840 return GR_REGS;
5842 /* This can happen because of the ior/and/etc patterns that accept FP
5843 registers as operands. If the third operand is a constant, then it
5844 needs to be reloaded into a FP register. */
5845 if (GET_CODE (x) == CONST_INT)
5846 return GR_REGS;
5848 /* This can happen because of register elimination in a muldi3 insn.
5849 E.g. `26107 * (unsigned long)&u'. */
5850 if (GET_CODE (x) == PLUS)
5851 return GR_REGS;
5852 break;
5854 case PR_REGS:
5855 /* ??? This happens if we cse/gcse a BImode value across a call,
5856 and the function has a nonlocal goto. This is because global
5857 does not allocate call crossing pseudos to hard registers when
5858 crtl->has_nonlocal_goto is true. This is relatively
5859 common for C++ programs that use exceptions. To reproduce,
5860 return NO_REGS and compile libstdc++. */
5861 if (GET_CODE (x) == MEM)
5862 return GR_REGS;
5864 /* This can happen when we take a BImode subreg of a DImode value,
5865 and that DImode value winds up in some non-GR register. */
5866 if (regno >= 0 && ! GENERAL_REGNO_P (regno) && ! PR_REGNO_P (regno))
5867 return GR_REGS;
5868 break;
5870 default:
5871 break;
5874 return NO_REGS;
5878 /* Implement targetm.unspec_may_trap_p hook. */
5879 static int
5880 ia64_unspec_may_trap_p (const_rtx x, unsigned flags)
5882 switch (XINT (x, 1))
5884 case UNSPEC_LDA:
5885 case UNSPEC_LDS:
5886 case UNSPEC_LDSA:
5887 case UNSPEC_LDCCLR:
5888 case UNSPEC_CHKACLR:
5889 case UNSPEC_CHKS:
5890 /* These unspecs are just wrappers. */
5891 return may_trap_p_1 (XVECEXP (x, 0, 0), flags);
5894 return default_unspec_may_trap_p (x, flags);
5898 /* Parse the -mfixed-range= option string. */
5900 static void
5901 fix_range (const char *const_str)
5903 int i, first, last;
5904 char *str, *dash, *comma;
5906 /* str must be of the form REG1'-'REG2{,REG1'-'REG} where REG1 and
5907 REG2 are either register names or register numbers. The effect
5908 of this option is to mark the registers in the range from REG1 to
5909 REG2 as ``fixed'' so they won't be used by the compiler. This is
5910 used, e.g., to ensure that kernel mode code doesn't use f32-f127. */
5912 i = strlen (const_str);
5913 str = (char *) alloca (i + 1);
5914 memcpy (str, const_str, i + 1);
5916 while (1)
5918 dash = strchr (str, '-');
5919 if (!dash)
5921 warning (0, "value of -mfixed-range must have form REG1-REG2");
5922 return;
5924 *dash = '\0';
5926 comma = strchr (dash + 1, ',');
5927 if (comma)
5928 *comma = '\0';
5930 first = decode_reg_name (str);
5931 if (first < 0)
5933 warning (0, "unknown register name: %s", str);
5934 return;
5937 last = decode_reg_name (dash + 1);
5938 if (last < 0)
5940 warning (0, "unknown register name: %s", dash + 1);
5941 return;
5944 *dash = '-';
5946 if (first > last)
5948 warning (0, "%s-%s is an empty range", str, dash + 1);
5949 return;
5952 for (i = first; i <= last; ++i)
5953 fixed_regs[i] = call_used_regs[i] = 1;
5955 if (!comma)
5956 break;
5958 *comma = ',';
5959 str = comma + 1;
5963 /* Implement TARGET_OPTION_OVERRIDE. */
5965 static void
5966 ia64_option_override (void)
5968 unsigned int i;
5969 cl_deferred_option *opt;
5970 vec<cl_deferred_option> *v
5971 = (vec<cl_deferred_option> *) ia64_deferred_options;
5973 if (v)
5974 FOR_EACH_VEC_ELT (*v, i, opt)
5976 switch (opt->opt_index)
5978 case OPT_mfixed_range_:
5979 fix_range (opt->arg);
5980 break;
5982 default:
5983 gcc_unreachable ();
5987 if (TARGET_AUTO_PIC)
5988 target_flags |= MASK_CONST_GP;
5990 /* Numerous experiment shows that IRA based loop pressure
5991 calculation works better for RTL loop invariant motion on targets
5992 with enough (>= 32) registers. It is an expensive optimization.
5993 So it is on only for peak performance. */
5994 if (optimize >= 3)
5995 flag_ira_loop_pressure = 1;
5998 ia64_section_threshold = (global_options_set.x_g_switch_value
5999 ? g_switch_value
6000 : IA64_DEFAULT_GVALUE);
6002 init_machine_status = ia64_init_machine_status;
6004 if (align_functions <= 0)
6005 align_functions = 64;
6006 if (align_loops <= 0)
6007 align_loops = 32;
6008 if (TARGET_ABI_OPEN_VMS)
6009 flag_no_common = 1;
6011 ia64_override_options_after_change();
6014 /* Implement targetm.override_options_after_change. */
6016 static void
6017 ia64_override_options_after_change (void)
6019 if (optimize >= 3
6020 && !global_options_set.x_flag_selective_scheduling
6021 && !global_options_set.x_flag_selective_scheduling2)
6023 flag_selective_scheduling2 = 1;
6024 flag_sel_sched_pipelining = 1;
6026 if (mflag_sched_control_spec == 2)
6028 /* Control speculation is on by default for the selective scheduler,
6029 but not for the Haifa scheduler. */
6030 mflag_sched_control_spec = flag_selective_scheduling2 ? 1 : 0;
6032 if (flag_sel_sched_pipelining && flag_auto_inc_dec)
6034 /* FIXME: remove this when we'd implement breaking autoinsns as
6035 a transformation. */
6036 flag_auto_inc_dec = 0;
6040 /* Initialize the record of emitted frame related registers. */
6042 void ia64_init_expanders (void)
6044 memset (&emitted_frame_related_regs, 0, sizeof (emitted_frame_related_regs));
6047 static struct machine_function *
6048 ia64_init_machine_status (void)
6050 return ggc_cleared_alloc<machine_function> ();
6053 static enum attr_itanium_class ia64_safe_itanium_class (rtx_insn *);
6054 static enum attr_type ia64_safe_type (rtx_insn *);
6056 static enum attr_itanium_class
6057 ia64_safe_itanium_class (rtx_insn *insn)
6059 if (recog_memoized (insn) >= 0)
6060 return get_attr_itanium_class (insn);
6061 else if (DEBUG_INSN_P (insn))
6062 return ITANIUM_CLASS_IGNORE;
6063 else
6064 return ITANIUM_CLASS_UNKNOWN;
6067 static enum attr_type
6068 ia64_safe_type (rtx_insn *insn)
6070 if (recog_memoized (insn) >= 0)
6071 return get_attr_type (insn);
6072 else
6073 return TYPE_UNKNOWN;
6076 /* The following collection of routines emit instruction group stop bits as
6077 necessary to avoid dependencies. */
6079 /* Need to track some additional registers as far as serialization is
6080 concerned so we can properly handle br.call and br.ret. We could
6081 make these registers visible to gcc, but since these registers are
6082 never explicitly used in gcc generated code, it seems wasteful to
6083 do so (plus it would make the call and return patterns needlessly
6084 complex). */
6085 #define REG_RP (BR_REG (0))
6086 #define REG_AR_CFM (FIRST_PSEUDO_REGISTER + 1)
6087 /* This is used for volatile asms which may require a stop bit immediately
6088 before and after them. */
6089 #define REG_VOLATILE (FIRST_PSEUDO_REGISTER + 2)
6090 #define AR_UNAT_BIT_0 (FIRST_PSEUDO_REGISTER + 3)
6091 #define NUM_REGS (AR_UNAT_BIT_0 + 64)
6093 /* For each register, we keep track of how it has been written in the
6094 current instruction group.
6096 If a register is written unconditionally (no qualifying predicate),
6097 WRITE_COUNT is set to 2 and FIRST_PRED is ignored.
6099 If a register is written if its qualifying predicate P is true, we
6100 set WRITE_COUNT to 1 and FIRST_PRED to P. Later on, the same register
6101 may be written again by the complement of P (P^1) and when this happens,
6102 WRITE_COUNT gets set to 2.
6104 The result of this is that whenever an insn attempts to write a register
6105 whose WRITE_COUNT is two, we need to issue an insn group barrier first.
6107 If a predicate register is written by a floating-point insn, we set
6108 WRITTEN_BY_FP to true.
6110 If a predicate register is written by an AND.ORCM we set WRITTEN_BY_AND
6111 to true; if it was written by an OR.ANDCM we set WRITTEN_BY_OR to true. */
6113 #if GCC_VERSION >= 4000
6114 #define RWS_FIELD_TYPE __extension__ unsigned short
6115 #else
6116 #define RWS_FIELD_TYPE unsigned int
6117 #endif
6118 struct reg_write_state
6120 RWS_FIELD_TYPE write_count : 2;
6121 RWS_FIELD_TYPE first_pred : 10;
6122 RWS_FIELD_TYPE written_by_fp : 1;
6123 RWS_FIELD_TYPE written_by_and : 1;
6124 RWS_FIELD_TYPE written_by_or : 1;
6127 /* Cumulative info for the current instruction group. */
6128 struct reg_write_state rws_sum[NUM_REGS];
6129 #ifdef ENABLE_CHECKING
6130 /* Bitmap whether a register has been written in the current insn. */
6131 HARD_REG_ELT_TYPE rws_insn[(NUM_REGS + HOST_BITS_PER_WIDEST_FAST_INT - 1)
6132 / HOST_BITS_PER_WIDEST_FAST_INT];
6134 static inline void
6135 rws_insn_set (int regno)
6137 gcc_assert (!TEST_HARD_REG_BIT (rws_insn, regno));
6138 SET_HARD_REG_BIT (rws_insn, regno);
6141 static inline int
6142 rws_insn_test (int regno)
6144 return TEST_HARD_REG_BIT (rws_insn, regno);
6146 #else
6147 /* When not checking, track just REG_AR_CFM and REG_VOLATILE. */
6148 unsigned char rws_insn[2];
6150 static inline void
6151 rws_insn_set (int regno)
6153 if (regno == REG_AR_CFM)
6154 rws_insn[0] = 1;
6155 else if (regno == REG_VOLATILE)
6156 rws_insn[1] = 1;
6159 static inline int
6160 rws_insn_test (int regno)
6162 if (regno == REG_AR_CFM)
6163 return rws_insn[0];
6164 if (regno == REG_VOLATILE)
6165 return rws_insn[1];
6166 return 0;
6168 #endif
6170 /* Indicates whether this is the first instruction after a stop bit,
6171 in which case we don't need another stop bit. Without this,
6172 ia64_variable_issue will die when scheduling an alloc. */
6173 static int first_instruction;
6175 /* Misc flags needed to compute RAW/WAW dependencies while we are traversing
6176 RTL for one instruction. */
6177 struct reg_flags
6179 unsigned int is_write : 1; /* Is register being written? */
6180 unsigned int is_fp : 1; /* Is register used as part of an fp op? */
6181 unsigned int is_branch : 1; /* Is register used as part of a branch? */
6182 unsigned int is_and : 1; /* Is register used as part of and.orcm? */
6183 unsigned int is_or : 1; /* Is register used as part of or.andcm? */
6184 unsigned int is_sibcall : 1; /* Is this a sibling or normal call? */
6187 static void rws_update (int, struct reg_flags, int);
6188 static int rws_access_regno (int, struct reg_flags, int);
6189 static int rws_access_reg (rtx, struct reg_flags, int);
6190 static void update_set_flags (rtx, struct reg_flags *);
6191 static int set_src_needs_barrier (rtx, struct reg_flags, int);
6192 static int rtx_needs_barrier (rtx, struct reg_flags, int);
6193 static void init_insn_group_barriers (void);
6194 static int group_barrier_needed (rtx_insn *);
6195 static int safe_group_barrier_needed (rtx_insn *);
6196 static int in_safe_group_barrier;
6198 /* Update *RWS for REGNO, which is being written by the current instruction,
6199 with predicate PRED, and associated register flags in FLAGS. */
6201 static void
6202 rws_update (int regno, struct reg_flags flags, int pred)
6204 if (pred)
6205 rws_sum[regno].write_count++;
6206 else
6207 rws_sum[regno].write_count = 2;
6208 rws_sum[regno].written_by_fp |= flags.is_fp;
6209 /* ??? Not tracking and/or across differing predicates. */
6210 rws_sum[regno].written_by_and = flags.is_and;
6211 rws_sum[regno].written_by_or = flags.is_or;
6212 rws_sum[regno].first_pred = pred;
6215 /* Handle an access to register REGNO of type FLAGS using predicate register
6216 PRED. Update rws_sum array. Return 1 if this access creates
6217 a dependency with an earlier instruction in the same group. */
6219 static int
6220 rws_access_regno (int regno, struct reg_flags flags, int pred)
6222 int need_barrier = 0;
6224 gcc_assert (regno < NUM_REGS);
6226 if (! PR_REGNO_P (regno))
6227 flags.is_and = flags.is_or = 0;
6229 if (flags.is_write)
6231 int write_count;
6233 rws_insn_set (regno);
6234 write_count = rws_sum[regno].write_count;
6236 switch (write_count)
6238 case 0:
6239 /* The register has not been written yet. */
6240 if (!in_safe_group_barrier)
6241 rws_update (regno, flags, pred);
6242 break;
6244 case 1:
6245 /* The register has been written via a predicate. Treat
6246 it like a unconditional write and do not try to check
6247 for complementary pred reg in earlier write. */
6248 if (flags.is_and && rws_sum[regno].written_by_and)
6250 else if (flags.is_or && rws_sum[regno].written_by_or)
6252 else
6253 need_barrier = 1;
6254 if (!in_safe_group_barrier)
6255 rws_update (regno, flags, pred);
6256 break;
6258 case 2:
6259 /* The register has been unconditionally written already. We
6260 need a barrier. */
6261 if (flags.is_and && rws_sum[regno].written_by_and)
6263 else if (flags.is_or && rws_sum[regno].written_by_or)
6265 else
6266 need_barrier = 1;
6267 if (!in_safe_group_barrier)
6269 rws_sum[regno].written_by_and = flags.is_and;
6270 rws_sum[regno].written_by_or = flags.is_or;
6272 break;
6274 default:
6275 gcc_unreachable ();
6278 else
6280 if (flags.is_branch)
6282 /* Branches have several RAW exceptions that allow to avoid
6283 barriers. */
6285 if (REGNO_REG_CLASS (regno) == BR_REGS || regno == AR_PFS_REGNUM)
6286 /* RAW dependencies on branch regs are permissible as long
6287 as the writer is a non-branch instruction. Since we
6288 never generate code that uses a branch register written
6289 by a branch instruction, handling this case is
6290 easy. */
6291 return 0;
6293 if (REGNO_REG_CLASS (regno) == PR_REGS
6294 && ! rws_sum[regno].written_by_fp)
6295 /* The predicates of a branch are available within the
6296 same insn group as long as the predicate was written by
6297 something other than a floating-point instruction. */
6298 return 0;
6301 if (flags.is_and && rws_sum[regno].written_by_and)
6302 return 0;
6303 if (flags.is_or && rws_sum[regno].written_by_or)
6304 return 0;
6306 switch (rws_sum[regno].write_count)
6308 case 0:
6309 /* The register has not been written yet. */
6310 break;
6312 case 1:
6313 /* The register has been written via a predicate, assume we
6314 need a barrier (don't check for complementary regs). */
6315 need_barrier = 1;
6316 break;
6318 case 2:
6319 /* The register has been unconditionally written already. We
6320 need a barrier. */
6321 need_barrier = 1;
6322 break;
6324 default:
6325 gcc_unreachable ();
6329 return need_barrier;
6332 static int
6333 rws_access_reg (rtx reg, struct reg_flags flags, int pred)
6335 int regno = REGNO (reg);
6336 int n = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
6338 if (n == 1)
6339 return rws_access_regno (regno, flags, pred);
6340 else
6342 int need_barrier = 0;
6343 while (--n >= 0)
6344 need_barrier |= rws_access_regno (regno + n, flags, pred);
6345 return need_barrier;
6349 /* Examine X, which is a SET rtx, and update the flags, the predicate, and
6350 the condition, stored in *PFLAGS, *PPRED and *PCOND. */
6352 static void
6353 update_set_flags (rtx x, struct reg_flags *pflags)
6355 rtx src = SET_SRC (x);
6357 switch (GET_CODE (src))
6359 case CALL:
6360 return;
6362 case IF_THEN_ELSE:
6363 /* There are four cases here:
6364 (1) The destination is (pc), in which case this is a branch,
6365 nothing here applies.
6366 (2) The destination is ar.lc, in which case this is a
6367 doloop_end_internal,
6368 (3) The destination is an fp register, in which case this is
6369 an fselect instruction.
6370 (4) The condition has (unspec [(reg)] UNSPEC_LDC), in which case
6371 this is a check load.
6372 In all cases, nothing we do in this function applies. */
6373 return;
6375 default:
6376 if (COMPARISON_P (src)
6377 && SCALAR_FLOAT_MODE_P (GET_MODE (XEXP (src, 0))))
6378 /* Set pflags->is_fp to 1 so that we know we're dealing
6379 with a floating point comparison when processing the
6380 destination of the SET. */
6381 pflags->is_fp = 1;
6383 /* Discover if this is a parallel comparison. We only handle
6384 and.orcm and or.andcm at present, since we must retain a
6385 strict inverse on the predicate pair. */
6386 else if (GET_CODE (src) == AND)
6387 pflags->is_and = 1;
6388 else if (GET_CODE (src) == IOR)
6389 pflags->is_or = 1;
6391 break;
6395 /* Subroutine of rtx_needs_barrier; this function determines whether the
6396 source of a given SET rtx found in X needs a barrier. FLAGS and PRED
6397 are as in rtx_needs_barrier. COND is an rtx that holds the condition
6398 for this insn. */
6400 static int
6401 set_src_needs_barrier (rtx x, struct reg_flags flags, int pred)
6403 int need_barrier = 0;
6404 rtx dst;
6405 rtx src = SET_SRC (x);
6407 if (GET_CODE (src) == CALL)
6408 /* We don't need to worry about the result registers that
6409 get written by subroutine call. */
6410 return rtx_needs_barrier (src, flags, pred);
6411 else if (SET_DEST (x) == pc_rtx)
6413 /* X is a conditional branch. */
6414 /* ??? This seems redundant, as the caller sets this bit for
6415 all JUMP_INSNs. */
6416 if (!ia64_spec_check_src_p (src))
6417 flags.is_branch = 1;
6418 return rtx_needs_barrier (src, flags, pred);
6421 if (ia64_spec_check_src_p (src))
6422 /* Avoid checking one register twice (in condition
6423 and in 'then' section) for ldc pattern. */
6425 gcc_assert (REG_P (XEXP (src, 2)));
6426 need_barrier = rtx_needs_barrier (XEXP (src, 2), flags, pred);
6428 /* We process MEM below. */
6429 src = XEXP (src, 1);
6432 need_barrier |= rtx_needs_barrier (src, flags, pred);
6434 dst = SET_DEST (x);
6435 if (GET_CODE (dst) == ZERO_EXTRACT)
6437 need_barrier |= rtx_needs_barrier (XEXP (dst, 1), flags, pred);
6438 need_barrier |= rtx_needs_barrier (XEXP (dst, 2), flags, pred);
6440 return need_barrier;
6443 /* Handle an access to rtx X of type FLAGS using predicate register
6444 PRED. Return 1 if this access creates a dependency with an earlier
6445 instruction in the same group. */
6447 static int
6448 rtx_needs_barrier (rtx x, struct reg_flags flags, int pred)
6450 int i, j;
6451 int is_complemented = 0;
6452 int need_barrier = 0;
6453 const char *format_ptr;
6454 struct reg_flags new_flags;
6455 rtx cond;
6457 if (! x)
6458 return 0;
6460 new_flags = flags;
6462 switch (GET_CODE (x))
6464 case SET:
6465 update_set_flags (x, &new_flags);
6466 need_barrier = set_src_needs_barrier (x, new_flags, pred);
6467 if (GET_CODE (SET_SRC (x)) != CALL)
6469 new_flags.is_write = 1;
6470 need_barrier |= rtx_needs_barrier (SET_DEST (x), new_flags, pred);
6472 break;
6474 case CALL:
6475 new_flags.is_write = 0;
6476 need_barrier |= rws_access_regno (AR_EC_REGNUM, new_flags, pred);
6478 /* Avoid multiple register writes, in case this is a pattern with
6479 multiple CALL rtx. This avoids a failure in rws_access_reg. */
6480 if (! flags.is_sibcall && ! rws_insn_test (REG_AR_CFM))
6482 new_flags.is_write = 1;
6483 need_barrier |= rws_access_regno (REG_RP, new_flags, pred);
6484 need_barrier |= rws_access_regno (AR_PFS_REGNUM, new_flags, pred);
6485 need_barrier |= rws_access_regno (REG_AR_CFM, new_flags, pred);
6487 break;
6489 case COND_EXEC:
6490 /* X is a predicated instruction. */
6492 cond = COND_EXEC_TEST (x);
6493 gcc_assert (!pred);
6494 need_barrier = rtx_needs_barrier (cond, flags, 0);
6496 if (GET_CODE (cond) == EQ)
6497 is_complemented = 1;
6498 cond = XEXP (cond, 0);
6499 gcc_assert (GET_CODE (cond) == REG
6500 && REGNO_REG_CLASS (REGNO (cond)) == PR_REGS);
6501 pred = REGNO (cond);
6502 if (is_complemented)
6503 ++pred;
6505 need_barrier |= rtx_needs_barrier (COND_EXEC_CODE (x), flags, pred);
6506 return need_barrier;
6508 case CLOBBER:
6509 case USE:
6510 /* Clobber & use are for earlier compiler-phases only. */
6511 break;
6513 case ASM_OPERANDS:
6514 case ASM_INPUT:
6515 /* We always emit stop bits for traditional asms. We emit stop bits
6516 for volatile extended asms if TARGET_VOL_ASM_STOP is true. */
6517 if (GET_CODE (x) != ASM_OPERANDS
6518 || (MEM_VOLATILE_P (x) && TARGET_VOL_ASM_STOP))
6520 /* Avoid writing the register multiple times if we have multiple
6521 asm outputs. This avoids a failure in rws_access_reg. */
6522 if (! rws_insn_test (REG_VOLATILE))
6524 new_flags.is_write = 1;
6525 rws_access_regno (REG_VOLATILE, new_flags, pred);
6527 return 1;
6530 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
6531 We cannot just fall through here since then we would be confused
6532 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
6533 traditional asms unlike their normal usage. */
6535 for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; --i)
6536 if (rtx_needs_barrier (ASM_OPERANDS_INPUT (x, i), flags, pred))
6537 need_barrier = 1;
6538 break;
6540 case PARALLEL:
6541 for (i = XVECLEN (x, 0) - 1; i >= 0; --i)
6543 rtx pat = XVECEXP (x, 0, i);
6544 switch (GET_CODE (pat))
6546 case SET:
6547 update_set_flags (pat, &new_flags);
6548 need_barrier |= set_src_needs_barrier (pat, new_flags, pred);
6549 break;
6551 case USE:
6552 case CALL:
6553 case ASM_OPERANDS:
6554 need_barrier |= rtx_needs_barrier (pat, flags, pred);
6555 break;
6557 case CLOBBER:
6558 if (REG_P (XEXP (pat, 0))
6559 && extract_asm_operands (x) != NULL_RTX
6560 && REGNO (XEXP (pat, 0)) != AR_UNAT_REGNUM)
6562 new_flags.is_write = 1;
6563 need_barrier |= rtx_needs_barrier (XEXP (pat, 0),
6564 new_flags, pred);
6565 new_flags = flags;
6567 break;
6569 case RETURN:
6570 break;
6572 default:
6573 gcc_unreachable ();
6576 for (i = XVECLEN (x, 0) - 1; i >= 0; --i)
6578 rtx pat = XVECEXP (x, 0, i);
6579 if (GET_CODE (pat) == SET)
6581 if (GET_CODE (SET_SRC (pat)) != CALL)
6583 new_flags.is_write = 1;
6584 need_barrier |= rtx_needs_barrier (SET_DEST (pat), new_flags,
6585 pred);
6588 else if (GET_CODE (pat) == CLOBBER || GET_CODE (pat) == RETURN)
6589 need_barrier |= rtx_needs_barrier (pat, flags, pred);
6591 break;
6593 case SUBREG:
6594 need_barrier |= rtx_needs_barrier (SUBREG_REG (x), flags, pred);
6595 break;
6596 case REG:
6597 if (REGNO (x) == AR_UNAT_REGNUM)
6599 for (i = 0; i < 64; ++i)
6600 need_barrier |= rws_access_regno (AR_UNAT_BIT_0 + i, flags, pred);
6602 else
6603 need_barrier = rws_access_reg (x, flags, pred);
6604 break;
6606 case MEM:
6607 /* Find the regs used in memory address computation. */
6608 new_flags.is_write = 0;
6609 need_barrier = rtx_needs_barrier (XEXP (x, 0), new_flags, pred);
6610 break;
6612 case CONST_INT: case CONST_DOUBLE: case CONST_VECTOR:
6613 case SYMBOL_REF: case LABEL_REF: case CONST:
6614 break;
6616 /* Operators with side-effects. */
6617 case POST_INC: case POST_DEC:
6618 gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
6620 new_flags.is_write = 0;
6621 need_barrier = rws_access_reg (XEXP (x, 0), new_flags, pred);
6622 new_flags.is_write = 1;
6623 need_barrier |= rws_access_reg (XEXP (x, 0), new_flags, pred);
6624 break;
6626 case POST_MODIFY:
6627 gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
6629 new_flags.is_write = 0;
6630 need_barrier = rws_access_reg (XEXP (x, 0), new_flags, pred);
6631 need_barrier |= rtx_needs_barrier (XEXP (x, 1), new_flags, pred);
6632 new_flags.is_write = 1;
6633 need_barrier |= rws_access_reg (XEXP (x, 0), new_flags, pred);
6634 break;
6636 /* Handle common unary and binary ops for efficiency. */
6637 case COMPARE: case PLUS: case MINUS: case MULT: case DIV:
6638 case MOD: case UDIV: case UMOD: case AND: case IOR:
6639 case XOR: case ASHIFT: case ROTATE: case ASHIFTRT: case LSHIFTRT:
6640 case ROTATERT: case SMIN: case SMAX: case UMIN: case UMAX:
6641 case NE: case EQ: case GE: case GT: case LE:
6642 case LT: case GEU: case GTU: case LEU: case LTU:
6643 need_barrier = rtx_needs_barrier (XEXP (x, 0), new_flags, pred);
6644 need_barrier |= rtx_needs_barrier (XEXP (x, 1), new_flags, pred);
6645 break;
6647 case NEG: case NOT: case SIGN_EXTEND: case ZERO_EXTEND:
6648 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE: case FLOAT:
6649 case FIX: case UNSIGNED_FLOAT: case UNSIGNED_FIX: case ABS:
6650 case SQRT: case FFS: case POPCOUNT:
6651 need_barrier = rtx_needs_barrier (XEXP (x, 0), flags, pred);
6652 break;
6654 case VEC_SELECT:
6655 /* VEC_SELECT's second argument is a PARALLEL with integers that
6656 describe the elements selected. On ia64, those integers are
6657 always constants. Avoid walking the PARALLEL so that we don't
6658 get confused with "normal" parallels and then die. */
6659 need_barrier = rtx_needs_barrier (XEXP (x, 0), flags, pred);
6660 break;
6662 case UNSPEC:
6663 switch (XINT (x, 1))
6665 case UNSPEC_LTOFF_DTPMOD:
6666 case UNSPEC_LTOFF_DTPREL:
6667 case UNSPEC_DTPREL:
6668 case UNSPEC_LTOFF_TPREL:
6669 case UNSPEC_TPREL:
6670 case UNSPEC_PRED_REL_MUTEX:
6671 case UNSPEC_PIC_CALL:
6672 case UNSPEC_MF:
6673 case UNSPEC_FETCHADD_ACQ:
6674 case UNSPEC_FETCHADD_REL:
6675 case UNSPEC_BSP_VALUE:
6676 case UNSPEC_FLUSHRS:
6677 case UNSPEC_BUNDLE_SELECTOR:
6678 break;
6680 case UNSPEC_GR_SPILL:
6681 case UNSPEC_GR_RESTORE:
6683 HOST_WIDE_INT offset = INTVAL (XVECEXP (x, 0, 1));
6684 HOST_WIDE_INT bit = (offset >> 3) & 63;
6686 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
6687 new_flags.is_write = (XINT (x, 1) == UNSPEC_GR_SPILL);
6688 need_barrier |= rws_access_regno (AR_UNAT_BIT_0 + bit,
6689 new_flags, pred);
6690 break;
6693 case UNSPEC_FR_SPILL:
6694 case UNSPEC_FR_RESTORE:
6695 case UNSPEC_GETF_EXP:
6696 case UNSPEC_SETF_EXP:
6697 case UNSPEC_ADDP4:
6698 case UNSPEC_FR_SQRT_RECIP_APPROX:
6699 case UNSPEC_FR_SQRT_RECIP_APPROX_RES:
6700 case UNSPEC_LDA:
6701 case UNSPEC_LDS:
6702 case UNSPEC_LDS_A:
6703 case UNSPEC_LDSA:
6704 case UNSPEC_CHKACLR:
6705 case UNSPEC_CHKS:
6706 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
6707 break;
6709 case UNSPEC_FR_RECIP_APPROX:
6710 case UNSPEC_SHRP:
6711 case UNSPEC_COPYSIGN:
6712 case UNSPEC_FR_RECIP_APPROX_RES:
6713 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
6714 need_barrier |= rtx_needs_barrier (XVECEXP (x, 0, 1), flags, pred);
6715 break;
6717 case UNSPEC_CMPXCHG_ACQ:
6718 case UNSPEC_CMPXCHG_REL:
6719 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 1), flags, pred);
6720 need_barrier |= rtx_needs_barrier (XVECEXP (x, 0, 2), flags, pred);
6721 break;
6723 default:
6724 gcc_unreachable ();
6726 break;
6728 case UNSPEC_VOLATILE:
6729 switch (XINT (x, 1))
6731 case UNSPECV_ALLOC:
6732 /* Alloc must always be the first instruction of a group.
6733 We force this by always returning true. */
6734 /* ??? We might get better scheduling if we explicitly check for
6735 input/local/output register dependencies, and modify the
6736 scheduler so that alloc is always reordered to the start of
6737 the current group. We could then eliminate all of the
6738 first_instruction code. */
6739 rws_access_regno (AR_PFS_REGNUM, flags, pred);
6741 new_flags.is_write = 1;
6742 rws_access_regno (REG_AR_CFM, new_flags, pred);
6743 return 1;
6745 case UNSPECV_SET_BSP:
6746 case UNSPECV_PROBE_STACK_RANGE:
6747 need_barrier = 1;
6748 break;
6750 case UNSPECV_BLOCKAGE:
6751 case UNSPECV_INSN_GROUP_BARRIER:
6752 case UNSPECV_BREAK:
6753 case UNSPECV_PSAC_ALL:
6754 case UNSPECV_PSAC_NORMAL:
6755 return 0;
6757 case UNSPECV_PROBE_STACK_ADDRESS:
6758 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
6759 break;
6761 default:
6762 gcc_unreachable ();
6764 break;
6766 case RETURN:
6767 new_flags.is_write = 0;
6768 need_barrier = rws_access_regno (REG_RP, flags, pred);
6769 need_barrier |= rws_access_regno (AR_PFS_REGNUM, flags, pred);
6771 new_flags.is_write = 1;
6772 need_barrier |= rws_access_regno (AR_EC_REGNUM, new_flags, pred);
6773 need_barrier |= rws_access_regno (REG_AR_CFM, new_flags, pred);
6774 break;
6776 default:
6777 format_ptr = GET_RTX_FORMAT (GET_CODE (x));
6778 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6779 switch (format_ptr[i])
6781 case '0': /* unused field */
6782 case 'i': /* integer */
6783 case 'n': /* note */
6784 case 'w': /* wide integer */
6785 case 's': /* pointer to string */
6786 case 'S': /* optional pointer to string */
6787 break;
6789 case 'e':
6790 if (rtx_needs_barrier (XEXP (x, i), flags, pred))
6791 need_barrier = 1;
6792 break;
6794 case 'E':
6795 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
6796 if (rtx_needs_barrier (XVECEXP (x, i, j), flags, pred))
6797 need_barrier = 1;
6798 break;
6800 default:
6801 gcc_unreachable ();
6803 break;
6805 return need_barrier;
6808 /* Clear out the state for group_barrier_needed at the start of a
6809 sequence of insns. */
6811 static void
6812 init_insn_group_barriers (void)
6814 memset (rws_sum, 0, sizeof (rws_sum));
6815 first_instruction = 1;
6818 /* Given the current state, determine whether a group barrier (a stop bit) is
6819 necessary before INSN. Return nonzero if so. This modifies the state to
6820 include the effects of INSN as a side-effect. */
6822 static int
6823 group_barrier_needed (rtx_insn *insn)
6825 rtx pat;
6826 int need_barrier = 0;
6827 struct reg_flags flags;
6829 memset (&flags, 0, sizeof (flags));
6830 switch (GET_CODE (insn))
6832 case NOTE:
6833 case DEBUG_INSN:
6834 break;
6836 case BARRIER:
6837 /* A barrier doesn't imply an instruction group boundary. */
6838 break;
6840 case CODE_LABEL:
6841 memset (rws_insn, 0, sizeof (rws_insn));
6842 return 1;
6844 case CALL_INSN:
6845 flags.is_branch = 1;
6846 flags.is_sibcall = SIBLING_CALL_P (insn);
6847 memset (rws_insn, 0, sizeof (rws_insn));
6849 /* Don't bundle a call following another call. */
6850 if ((pat = prev_active_insn (insn)) && CALL_P (pat))
6852 need_barrier = 1;
6853 break;
6856 need_barrier = rtx_needs_barrier (PATTERN (insn), flags, 0);
6857 break;
6859 case JUMP_INSN:
6860 if (!ia64_spec_check_p (insn))
6861 flags.is_branch = 1;
6863 /* Don't bundle a jump following a call. */
6864 if ((pat = prev_active_insn (insn)) && CALL_P (pat))
6866 need_barrier = 1;
6867 break;
6869 /* FALLTHRU */
6871 case INSN:
6872 if (GET_CODE (PATTERN (insn)) == USE
6873 || GET_CODE (PATTERN (insn)) == CLOBBER)
6874 /* Don't care about USE and CLOBBER "insns"---those are used to
6875 indicate to the optimizer that it shouldn't get rid of
6876 certain operations. */
6877 break;
6879 pat = PATTERN (insn);
6881 /* Ug. Hack hacks hacked elsewhere. */
6882 switch (recog_memoized (insn))
6884 /* We play dependency tricks with the epilogue in order
6885 to get proper schedules. Undo this for dv analysis. */
6886 case CODE_FOR_epilogue_deallocate_stack:
6887 case CODE_FOR_prologue_allocate_stack:
6888 pat = XVECEXP (pat, 0, 0);
6889 break;
6891 /* The pattern we use for br.cloop confuses the code above.
6892 The second element of the vector is representative. */
6893 case CODE_FOR_doloop_end_internal:
6894 pat = XVECEXP (pat, 0, 1);
6895 break;
6897 /* Doesn't generate code. */
6898 case CODE_FOR_pred_rel_mutex:
6899 case CODE_FOR_prologue_use:
6900 return 0;
6902 default:
6903 break;
6906 memset (rws_insn, 0, sizeof (rws_insn));
6907 need_barrier = rtx_needs_barrier (pat, flags, 0);
6909 /* Check to see if the previous instruction was a volatile
6910 asm. */
6911 if (! need_barrier)
6912 need_barrier = rws_access_regno (REG_VOLATILE, flags, 0);
6914 break;
6916 default:
6917 gcc_unreachable ();
6920 if (first_instruction && important_for_bundling_p (insn))
6922 need_barrier = 0;
6923 first_instruction = 0;
6926 return need_barrier;
6929 /* Like group_barrier_needed, but do not clobber the current state. */
6931 static int
6932 safe_group_barrier_needed (rtx_insn *insn)
6934 int saved_first_instruction;
6935 int t;
6937 saved_first_instruction = first_instruction;
6938 in_safe_group_barrier = 1;
6940 t = group_barrier_needed (insn);
6942 first_instruction = saved_first_instruction;
6943 in_safe_group_barrier = 0;
6945 return t;
6948 /* Scan the current function and insert stop bits as necessary to
6949 eliminate dependencies. This function assumes that a final
6950 instruction scheduling pass has been run which has already
6951 inserted most of the necessary stop bits. This function only
6952 inserts new ones at basic block boundaries, since these are
6953 invisible to the scheduler. */
6955 static void
6956 emit_insn_group_barriers (FILE *dump)
6958 rtx_insn *insn;
6959 rtx_insn *last_label = 0;
6960 int insns_since_last_label = 0;
6962 init_insn_group_barriers ();
6964 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6966 if (LABEL_P (insn))
6968 if (insns_since_last_label)
6969 last_label = insn;
6970 insns_since_last_label = 0;
6972 else if (NOTE_P (insn)
6973 && NOTE_KIND (insn) == NOTE_INSN_BASIC_BLOCK)
6975 if (insns_since_last_label)
6976 last_label = insn;
6977 insns_since_last_label = 0;
6979 else if (NONJUMP_INSN_P (insn)
6980 && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
6981 && XINT (PATTERN (insn), 1) == UNSPECV_INSN_GROUP_BARRIER)
6983 init_insn_group_barriers ();
6984 last_label = 0;
6986 else if (NONDEBUG_INSN_P (insn))
6988 insns_since_last_label = 1;
6990 if (group_barrier_needed (insn))
6992 if (last_label)
6994 if (dump)
6995 fprintf (dump, "Emitting stop before label %d\n",
6996 INSN_UID (last_label));
6997 emit_insn_before (gen_insn_group_barrier (GEN_INT (3)), last_label);
6998 insn = last_label;
7000 init_insn_group_barriers ();
7001 last_label = 0;
7008 /* Like emit_insn_group_barriers, but run if no final scheduling pass was run.
7009 This function has to emit all necessary group barriers. */
7011 static void
7012 emit_all_insn_group_barriers (FILE *dump ATTRIBUTE_UNUSED)
7014 rtx_insn *insn;
7016 init_insn_group_barriers ();
7018 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
7020 if (BARRIER_P (insn))
7022 rtx_insn *last = prev_active_insn (insn);
7024 if (! last)
7025 continue;
7026 if (JUMP_TABLE_DATA_P (last))
7027 last = prev_active_insn (last);
7028 if (recog_memoized (last) != CODE_FOR_insn_group_barrier)
7029 emit_insn_after (gen_insn_group_barrier (GEN_INT (3)), last);
7031 init_insn_group_barriers ();
7033 else if (NONDEBUG_INSN_P (insn))
7035 if (recog_memoized (insn) == CODE_FOR_insn_group_barrier)
7036 init_insn_group_barriers ();
7037 else if (group_barrier_needed (insn))
7039 emit_insn_before (gen_insn_group_barrier (GEN_INT (3)), insn);
7040 init_insn_group_barriers ();
7041 group_barrier_needed (insn);
7049 /* Instruction scheduling support. */
7051 #define NR_BUNDLES 10
7053 /* A list of names of all available bundles. */
7055 static const char *bundle_name [NR_BUNDLES] =
7057 ".mii",
7058 ".mmi",
7059 ".mfi",
7060 ".mmf",
7061 #if NR_BUNDLES == 10
7062 ".bbb",
7063 ".mbb",
7064 #endif
7065 ".mib",
7066 ".mmb",
7067 ".mfb",
7068 ".mlx"
7071 /* Nonzero if we should insert stop bits into the schedule. */
7073 int ia64_final_schedule = 0;
7075 /* Codes of the corresponding queried units: */
7077 static int _0mii_, _0mmi_, _0mfi_, _0mmf_;
7078 static int _0bbb_, _0mbb_, _0mib_, _0mmb_, _0mfb_, _0mlx_;
7080 static int _1mii_, _1mmi_, _1mfi_, _1mmf_;
7081 static int _1bbb_, _1mbb_, _1mib_, _1mmb_, _1mfb_, _1mlx_;
7083 static int pos_1, pos_2, pos_3, pos_4, pos_5, pos_6;
7085 /* The following variable value is an insn group barrier. */
7087 static rtx_insn *dfa_stop_insn;
7089 /* The following variable value is the last issued insn. */
7091 static rtx_insn *last_scheduled_insn;
7093 /* The following variable value is pointer to a DFA state used as
7094 temporary variable. */
7096 static state_t temp_dfa_state = NULL;
7098 /* The following variable value is DFA state after issuing the last
7099 insn. */
7101 static state_t prev_cycle_state = NULL;
7103 /* The following array element values are TRUE if the corresponding
7104 insn requires to add stop bits before it. */
7106 static char *stops_p = NULL;
7108 /* The following variable is used to set up the mentioned above array. */
7110 static int stop_before_p = 0;
7112 /* The following variable value is length of the arrays `clocks' and
7113 `add_cycles'. */
7115 static int clocks_length;
7117 /* The following variable value is number of data speculations in progress. */
7118 static int pending_data_specs = 0;
7120 /* Number of memory references on current and three future processor cycles. */
7121 static char mem_ops_in_group[4];
7123 /* Number of current processor cycle (from scheduler's point of view). */
7124 static int current_cycle;
7126 static rtx ia64_single_set (rtx_insn *);
7127 static void ia64_emit_insn_before (rtx, rtx);
7129 /* Map a bundle number to its pseudo-op. */
7131 const char *
7132 get_bundle_name (int b)
7134 return bundle_name[b];
7138 /* Return the maximum number of instructions a cpu can issue. */
7140 static int
7141 ia64_issue_rate (void)
7143 return 6;
7146 /* Helper function - like single_set, but look inside COND_EXEC. */
7148 static rtx
7149 ia64_single_set (rtx_insn *insn)
7151 rtx x = PATTERN (insn), ret;
7152 if (GET_CODE (x) == COND_EXEC)
7153 x = COND_EXEC_CODE (x);
7154 if (GET_CODE (x) == SET)
7155 return x;
7157 /* Special case here prologue_allocate_stack and epilogue_deallocate_stack.
7158 Although they are not classical single set, the second set is there just
7159 to protect it from moving past FP-relative stack accesses. */
7160 switch (recog_memoized (insn))
7162 case CODE_FOR_prologue_allocate_stack:
7163 case CODE_FOR_prologue_allocate_stack_pr:
7164 case CODE_FOR_epilogue_deallocate_stack:
7165 case CODE_FOR_epilogue_deallocate_stack_pr:
7166 ret = XVECEXP (x, 0, 0);
7167 break;
7169 default:
7170 ret = single_set_2 (insn, x);
7171 break;
7174 return ret;
7177 /* Adjust the cost of a scheduling dependency.
7178 Return the new cost of a dependency of type DEP_TYPE or INSN on DEP_INSN.
7179 COST is the current cost, DW is dependency weakness. */
7180 static int
7181 ia64_adjust_cost_2 (rtx_insn *insn, int dep_type1, rtx_insn *dep_insn,
7182 int cost, dw_t dw)
7184 enum reg_note dep_type = (enum reg_note) dep_type1;
7185 enum attr_itanium_class dep_class;
7186 enum attr_itanium_class insn_class;
7188 insn_class = ia64_safe_itanium_class (insn);
7189 dep_class = ia64_safe_itanium_class (dep_insn);
7191 /* Treat true memory dependencies separately. Ignore apparent true
7192 dependence between store and call (call has a MEM inside a SYMBOL_REF). */
7193 if (dep_type == REG_DEP_TRUE
7194 && (dep_class == ITANIUM_CLASS_ST || dep_class == ITANIUM_CLASS_STF)
7195 && (insn_class == ITANIUM_CLASS_BR || insn_class == ITANIUM_CLASS_SCALL))
7196 return 0;
7198 if (dw == MIN_DEP_WEAK)
7199 /* Store and load are likely to alias, use higher cost to avoid stall. */
7200 return PARAM_VALUE (PARAM_SCHED_MEM_TRUE_DEP_COST);
7201 else if (dw > MIN_DEP_WEAK)
7203 /* Store and load are less likely to alias. */
7204 if (mflag_sched_fp_mem_deps_zero_cost && dep_class == ITANIUM_CLASS_STF)
7205 /* Assume there will be no cache conflict for floating-point data.
7206 For integer data, L1 conflict penalty is huge (17 cycles), so we
7207 never assume it will not cause a conflict. */
7208 return 0;
7209 else
7210 return cost;
7213 if (dep_type != REG_DEP_OUTPUT)
7214 return cost;
7216 if (dep_class == ITANIUM_CLASS_ST || dep_class == ITANIUM_CLASS_STF
7217 || insn_class == ITANIUM_CLASS_ST || insn_class == ITANIUM_CLASS_STF)
7218 return 0;
7220 return cost;
7223 /* Like emit_insn_before, but skip cycle_display notes.
7224 ??? When cycle display notes are implemented, update this. */
7226 static void
7227 ia64_emit_insn_before (rtx insn, rtx before)
7229 emit_insn_before (insn, before);
7232 /* The following function marks insns who produce addresses for load
7233 and store insns. Such insns will be placed into M slots because it
7234 decrease latency time for Itanium1 (see function
7235 `ia64_produce_address_p' and the DFA descriptions). */
7237 static void
7238 ia64_dependencies_evaluation_hook (rtx_insn *head, rtx_insn *tail)
7240 rtx_insn *insn, *next, *next_tail;
7242 /* Before reload, which_alternative is not set, which means that
7243 ia64_safe_itanium_class will produce wrong results for (at least)
7244 move instructions. */
7245 if (!reload_completed)
7246 return;
7248 next_tail = NEXT_INSN (tail);
7249 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
7250 if (INSN_P (insn))
7251 insn->call = 0;
7252 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
7253 if (INSN_P (insn)
7254 && ia64_safe_itanium_class (insn) == ITANIUM_CLASS_IALU)
7256 sd_iterator_def sd_it;
7257 dep_t dep;
7258 bool has_mem_op_consumer_p = false;
7260 FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
7262 enum attr_itanium_class c;
7264 if (DEP_TYPE (dep) != REG_DEP_TRUE)
7265 continue;
7267 next = DEP_CON (dep);
7268 c = ia64_safe_itanium_class (next);
7269 if ((c == ITANIUM_CLASS_ST
7270 || c == ITANIUM_CLASS_STF)
7271 && ia64_st_address_bypass_p (insn, next))
7273 has_mem_op_consumer_p = true;
7274 break;
7276 else if ((c == ITANIUM_CLASS_LD
7277 || c == ITANIUM_CLASS_FLD
7278 || c == ITANIUM_CLASS_FLDP)
7279 && ia64_ld_address_bypass_p (insn, next))
7281 has_mem_op_consumer_p = true;
7282 break;
7286 insn->call = has_mem_op_consumer_p;
7290 /* We're beginning a new block. Initialize data structures as necessary. */
7292 static void
7293 ia64_sched_init (FILE *dump ATTRIBUTE_UNUSED,
7294 int sched_verbose ATTRIBUTE_UNUSED,
7295 int max_ready ATTRIBUTE_UNUSED)
7297 #ifdef ENABLE_CHECKING
7298 rtx_insn *insn;
7300 if (!sel_sched_p () && reload_completed)
7301 for (insn = NEXT_INSN (current_sched_info->prev_head);
7302 insn != current_sched_info->next_tail;
7303 insn = NEXT_INSN (insn))
7304 gcc_assert (!SCHED_GROUP_P (insn));
7305 #endif
7306 last_scheduled_insn = NULL;
7307 init_insn_group_barriers ();
7309 current_cycle = 0;
7310 memset (mem_ops_in_group, 0, sizeof (mem_ops_in_group));
7313 /* We're beginning a scheduling pass. Check assertion. */
7315 static void
7316 ia64_sched_init_global (FILE *dump ATTRIBUTE_UNUSED,
7317 int sched_verbose ATTRIBUTE_UNUSED,
7318 int max_ready ATTRIBUTE_UNUSED)
7320 gcc_assert (pending_data_specs == 0);
7323 /* Scheduling pass is now finished. Free/reset static variable. */
7324 static void
7325 ia64_sched_finish_global (FILE *dump ATTRIBUTE_UNUSED,
7326 int sched_verbose ATTRIBUTE_UNUSED)
7328 gcc_assert (pending_data_specs == 0);
7331 /* Return TRUE if INSN is a load (either normal or speculative, but not a
7332 speculation check), FALSE otherwise. */
7333 static bool
7334 is_load_p (rtx_insn *insn)
7336 enum attr_itanium_class insn_class = ia64_safe_itanium_class (insn);
7338 return
7339 ((insn_class == ITANIUM_CLASS_LD || insn_class == ITANIUM_CLASS_FLD)
7340 && get_attr_check_load (insn) == CHECK_LOAD_NO);
7343 /* If INSN is a memory reference, memoize it in MEM_OPS_IN_GROUP global array
7344 (taking account for 3-cycle cache reference postponing for stores: Intel
7345 Itanium 2 Reference Manual for Software Development and Optimization,
7346 6.7.3.1). */
7347 static void
7348 record_memory_reference (rtx_insn *insn)
7350 enum attr_itanium_class insn_class = ia64_safe_itanium_class (insn);
7352 switch (insn_class) {
7353 case ITANIUM_CLASS_FLD:
7354 case ITANIUM_CLASS_LD:
7355 mem_ops_in_group[current_cycle % 4]++;
7356 break;
7357 case ITANIUM_CLASS_STF:
7358 case ITANIUM_CLASS_ST:
7359 mem_ops_in_group[(current_cycle + 3) % 4]++;
7360 break;
7361 default:;
7365 /* We are about to being issuing insns for this clock cycle.
7366 Override the default sort algorithm to better slot instructions. */
7368 static int
7369 ia64_dfa_sched_reorder (FILE *dump, int sched_verbose, rtx_insn **ready,
7370 int *pn_ready, int clock_var,
7371 int reorder_type)
7373 int n_asms;
7374 int n_ready = *pn_ready;
7375 rtx_insn **e_ready = ready + n_ready;
7376 rtx_insn **insnp;
7378 if (sched_verbose)
7379 fprintf (dump, "// ia64_dfa_sched_reorder (type %d):\n", reorder_type);
7381 if (reorder_type == 0)
7383 /* First, move all USEs, CLOBBERs and other crud out of the way. */
7384 n_asms = 0;
7385 for (insnp = ready; insnp < e_ready; insnp++)
7386 if (insnp < e_ready)
7388 rtx_insn *insn = *insnp;
7389 enum attr_type t = ia64_safe_type (insn);
7390 if (t == TYPE_UNKNOWN)
7392 if (GET_CODE (PATTERN (insn)) == ASM_INPUT
7393 || asm_noperands (PATTERN (insn)) >= 0)
7395 rtx_insn *lowest = ready[n_asms];
7396 ready[n_asms] = insn;
7397 *insnp = lowest;
7398 n_asms++;
7400 else
7402 rtx_insn *highest = ready[n_ready - 1];
7403 ready[n_ready - 1] = insn;
7404 *insnp = highest;
7405 return 1;
7410 if (n_asms < n_ready)
7412 /* Some normal insns to process. Skip the asms. */
7413 ready += n_asms;
7414 n_ready -= n_asms;
7416 else if (n_ready > 0)
7417 return 1;
7420 if (ia64_final_schedule)
7422 int deleted = 0;
7423 int nr_need_stop = 0;
7425 for (insnp = ready; insnp < e_ready; insnp++)
7426 if (safe_group_barrier_needed (*insnp))
7427 nr_need_stop++;
7429 if (reorder_type == 1 && n_ready == nr_need_stop)
7430 return 0;
7431 if (reorder_type == 0)
7432 return 1;
7433 insnp = e_ready;
7434 /* Move down everything that needs a stop bit, preserving
7435 relative order. */
7436 while (insnp-- > ready + deleted)
7437 while (insnp >= ready + deleted)
7439 rtx_insn *insn = *insnp;
7440 if (! safe_group_barrier_needed (insn))
7441 break;
7442 memmove (ready + 1, ready, (insnp - ready) * sizeof (rtx));
7443 *ready = insn;
7444 deleted++;
7446 n_ready -= deleted;
7447 ready += deleted;
7450 current_cycle = clock_var;
7451 if (reload_completed && mem_ops_in_group[clock_var % 4] >= ia64_max_memory_insns)
7453 int moved = 0;
7455 insnp = e_ready;
7456 /* Move down loads/stores, preserving relative order. */
7457 while (insnp-- > ready + moved)
7458 while (insnp >= ready + moved)
7460 rtx_insn *insn = *insnp;
7461 if (! is_load_p (insn))
7462 break;
7463 memmove (ready + 1, ready, (insnp - ready) * sizeof (rtx));
7464 *ready = insn;
7465 moved++;
7467 n_ready -= moved;
7468 ready += moved;
7471 return 1;
7474 /* We are about to being issuing insns for this clock cycle. Override
7475 the default sort algorithm to better slot instructions. */
7477 static int
7478 ia64_sched_reorder (FILE *dump, int sched_verbose, rtx_insn **ready,
7479 int *pn_ready, int clock_var)
7481 return ia64_dfa_sched_reorder (dump, sched_verbose, ready,
7482 pn_ready, clock_var, 0);
7485 /* Like ia64_sched_reorder, but called after issuing each insn.
7486 Override the default sort algorithm to better slot instructions. */
7488 static int
7489 ia64_sched_reorder2 (FILE *dump ATTRIBUTE_UNUSED,
7490 int sched_verbose ATTRIBUTE_UNUSED, rtx_insn **ready,
7491 int *pn_ready, int clock_var)
7493 return ia64_dfa_sched_reorder (dump, sched_verbose, ready, pn_ready,
7494 clock_var, 1);
7497 /* We are about to issue INSN. Return the number of insns left on the
7498 ready queue that can be issued this cycle. */
7500 static int
7501 ia64_variable_issue (FILE *dump ATTRIBUTE_UNUSED,
7502 int sched_verbose ATTRIBUTE_UNUSED,
7503 rtx_insn *insn,
7504 int can_issue_more ATTRIBUTE_UNUSED)
7506 if (sched_deps_info->generate_spec_deps && !sel_sched_p ())
7507 /* Modulo scheduling does not extend h_i_d when emitting
7508 new instructions. Don't use h_i_d, if we don't have to. */
7510 if (DONE_SPEC (insn) & BEGIN_DATA)
7511 pending_data_specs++;
7512 if (CHECK_SPEC (insn) & BEGIN_DATA)
7513 pending_data_specs--;
7516 if (DEBUG_INSN_P (insn))
7517 return 1;
7519 last_scheduled_insn = insn;
7520 memcpy (prev_cycle_state, curr_state, dfa_state_size);
7521 if (reload_completed)
7523 int needed = group_barrier_needed (insn);
7525 gcc_assert (!needed);
7526 if (CALL_P (insn))
7527 init_insn_group_barriers ();
7528 stops_p [INSN_UID (insn)] = stop_before_p;
7529 stop_before_p = 0;
7531 record_memory_reference (insn);
7533 return 1;
7536 /* We are choosing insn from the ready queue. Return zero if INSN
7537 can be chosen. */
7539 static int
7540 ia64_first_cycle_multipass_dfa_lookahead_guard (rtx_insn *insn, int ready_index)
7542 gcc_assert (insn && INSN_P (insn));
7544 /* Size of ALAT is 32. As far as we perform conservative
7545 data speculation, we keep ALAT half-empty. */
7546 if (pending_data_specs >= 16 && (TODO_SPEC (insn) & BEGIN_DATA))
7547 return ready_index == 0 ? -1 : 1;
7549 if (ready_index == 0)
7550 return 0;
7552 if ((!reload_completed
7553 || !safe_group_barrier_needed (insn))
7554 && (!mflag_sched_mem_insns_hard_limit
7555 || !is_load_p (insn)
7556 || mem_ops_in_group[current_cycle % 4] < ia64_max_memory_insns))
7557 return 0;
7559 return 1;
7562 /* The following variable value is pseudo-insn used by the DFA insn
7563 scheduler to change the DFA state when the simulated clock is
7564 increased. */
7566 static rtx_insn *dfa_pre_cycle_insn;
7568 /* Returns 1 when a meaningful insn was scheduled between the last group
7569 barrier and LAST. */
7570 static int
7571 scheduled_good_insn (rtx_insn *last)
7573 if (last && recog_memoized (last) >= 0)
7574 return 1;
7576 for ( ;
7577 last != NULL && !NOTE_INSN_BASIC_BLOCK_P (last)
7578 && !stops_p[INSN_UID (last)];
7579 last = PREV_INSN (last))
7580 /* We could hit a NOTE_INSN_DELETED here which is actually outside
7581 the ebb we're scheduling. */
7582 if (INSN_P (last) && recog_memoized (last) >= 0)
7583 return 1;
7585 return 0;
7588 /* We are about to being issuing INSN. Return nonzero if we cannot
7589 issue it on given cycle CLOCK and return zero if we should not sort
7590 the ready queue on the next clock start. */
7592 static int
7593 ia64_dfa_new_cycle (FILE *dump, int verbose, rtx_insn *insn, int last_clock,
7594 int clock, int *sort_p)
7596 gcc_assert (insn && INSN_P (insn));
7598 if (DEBUG_INSN_P (insn))
7599 return 0;
7601 /* When a group barrier is needed for insn, last_scheduled_insn
7602 should be set. */
7603 gcc_assert (!(reload_completed && safe_group_barrier_needed (insn))
7604 || last_scheduled_insn);
7606 if ((reload_completed
7607 && (safe_group_barrier_needed (insn)
7608 || (mflag_sched_stop_bits_after_every_cycle
7609 && last_clock != clock
7610 && last_scheduled_insn
7611 && scheduled_good_insn (last_scheduled_insn))))
7612 || (last_scheduled_insn
7613 && (CALL_P (last_scheduled_insn)
7614 || unknown_for_bundling_p (last_scheduled_insn))))
7616 init_insn_group_barriers ();
7618 if (verbose && dump)
7619 fprintf (dump, "// Stop should be before %d%s\n", INSN_UID (insn),
7620 last_clock == clock ? " + cycle advance" : "");
7622 stop_before_p = 1;
7623 current_cycle = clock;
7624 mem_ops_in_group[current_cycle % 4] = 0;
7626 if (last_clock == clock)
7628 state_transition (curr_state, dfa_stop_insn);
7629 if (TARGET_EARLY_STOP_BITS)
7630 *sort_p = (last_scheduled_insn == NULL_RTX
7631 || ! CALL_P (last_scheduled_insn));
7632 else
7633 *sort_p = 0;
7634 return 1;
7637 if (last_scheduled_insn)
7639 if (unknown_for_bundling_p (last_scheduled_insn))
7640 state_reset (curr_state);
7641 else
7643 memcpy (curr_state, prev_cycle_state, dfa_state_size);
7644 state_transition (curr_state, dfa_stop_insn);
7645 state_transition (curr_state, dfa_pre_cycle_insn);
7646 state_transition (curr_state, NULL);
7650 return 0;
7653 /* Implement targetm.sched.h_i_d_extended hook.
7654 Extend internal data structures. */
7655 static void
7656 ia64_h_i_d_extended (void)
7658 if (stops_p != NULL)
7660 int new_clocks_length = get_max_uid () * 3 / 2;
7661 stops_p = (char *) xrecalloc (stops_p, new_clocks_length, clocks_length, 1);
7662 clocks_length = new_clocks_length;
7667 /* This structure describes the data used by the backend to guide scheduling.
7668 When the current scheduling point is switched, this data should be saved
7669 and restored later, if the scheduler returns to this point. */
7670 struct _ia64_sched_context
7672 state_t prev_cycle_state;
7673 rtx_insn *last_scheduled_insn;
7674 struct reg_write_state rws_sum[NUM_REGS];
7675 struct reg_write_state rws_insn[NUM_REGS];
7676 int first_instruction;
7677 int pending_data_specs;
7678 int current_cycle;
7679 char mem_ops_in_group[4];
7681 typedef struct _ia64_sched_context *ia64_sched_context_t;
7683 /* Allocates a scheduling context. */
7684 static void *
7685 ia64_alloc_sched_context (void)
7687 return xmalloc (sizeof (struct _ia64_sched_context));
7690 /* Initializes the _SC context with clean data, if CLEAN_P, and from
7691 the global context otherwise. */
7692 static void
7693 ia64_init_sched_context (void *_sc, bool clean_p)
7695 ia64_sched_context_t sc = (ia64_sched_context_t) _sc;
7697 sc->prev_cycle_state = xmalloc (dfa_state_size);
7698 if (clean_p)
7700 state_reset (sc->prev_cycle_state);
7701 sc->last_scheduled_insn = NULL;
7702 memset (sc->rws_sum, 0, sizeof (rws_sum));
7703 memset (sc->rws_insn, 0, sizeof (rws_insn));
7704 sc->first_instruction = 1;
7705 sc->pending_data_specs = 0;
7706 sc->current_cycle = 0;
7707 memset (sc->mem_ops_in_group, 0, sizeof (mem_ops_in_group));
7709 else
7711 memcpy (sc->prev_cycle_state, prev_cycle_state, dfa_state_size);
7712 sc->last_scheduled_insn = last_scheduled_insn;
7713 memcpy (sc->rws_sum, rws_sum, sizeof (rws_sum));
7714 memcpy (sc->rws_insn, rws_insn, sizeof (rws_insn));
7715 sc->first_instruction = first_instruction;
7716 sc->pending_data_specs = pending_data_specs;
7717 sc->current_cycle = current_cycle;
7718 memcpy (sc->mem_ops_in_group, mem_ops_in_group, sizeof (mem_ops_in_group));
7722 /* Sets the global scheduling context to the one pointed to by _SC. */
7723 static void
7724 ia64_set_sched_context (void *_sc)
7726 ia64_sched_context_t sc = (ia64_sched_context_t) _sc;
7728 gcc_assert (sc != NULL);
7730 memcpy (prev_cycle_state, sc->prev_cycle_state, dfa_state_size);
7731 last_scheduled_insn = sc->last_scheduled_insn;
7732 memcpy (rws_sum, sc->rws_sum, sizeof (rws_sum));
7733 memcpy (rws_insn, sc->rws_insn, sizeof (rws_insn));
7734 first_instruction = sc->first_instruction;
7735 pending_data_specs = sc->pending_data_specs;
7736 current_cycle = sc->current_cycle;
7737 memcpy (mem_ops_in_group, sc->mem_ops_in_group, sizeof (mem_ops_in_group));
7740 /* Clears the data in the _SC scheduling context. */
7741 static void
7742 ia64_clear_sched_context (void *_sc)
7744 ia64_sched_context_t sc = (ia64_sched_context_t) _sc;
7746 free (sc->prev_cycle_state);
7747 sc->prev_cycle_state = NULL;
7750 /* Frees the _SC scheduling context. */
7751 static void
7752 ia64_free_sched_context (void *_sc)
7754 gcc_assert (_sc != NULL);
7756 free (_sc);
7759 typedef rtx (* gen_func_t) (rtx, rtx);
7761 /* Return a function that will generate a load of mode MODE_NO
7762 with speculation types TS. */
7763 static gen_func_t
7764 get_spec_load_gen_function (ds_t ts, int mode_no)
7766 static gen_func_t gen_ld_[] = {
7767 gen_movbi,
7768 gen_movqi_internal,
7769 gen_movhi_internal,
7770 gen_movsi_internal,
7771 gen_movdi_internal,
7772 gen_movsf_internal,
7773 gen_movdf_internal,
7774 gen_movxf_internal,
7775 gen_movti_internal,
7776 gen_zero_extendqidi2,
7777 gen_zero_extendhidi2,
7778 gen_zero_extendsidi2,
7781 static gen_func_t gen_ld_a[] = {
7782 gen_movbi_advanced,
7783 gen_movqi_advanced,
7784 gen_movhi_advanced,
7785 gen_movsi_advanced,
7786 gen_movdi_advanced,
7787 gen_movsf_advanced,
7788 gen_movdf_advanced,
7789 gen_movxf_advanced,
7790 gen_movti_advanced,
7791 gen_zero_extendqidi2_advanced,
7792 gen_zero_extendhidi2_advanced,
7793 gen_zero_extendsidi2_advanced,
7795 static gen_func_t gen_ld_s[] = {
7796 gen_movbi_speculative,
7797 gen_movqi_speculative,
7798 gen_movhi_speculative,
7799 gen_movsi_speculative,
7800 gen_movdi_speculative,
7801 gen_movsf_speculative,
7802 gen_movdf_speculative,
7803 gen_movxf_speculative,
7804 gen_movti_speculative,
7805 gen_zero_extendqidi2_speculative,
7806 gen_zero_extendhidi2_speculative,
7807 gen_zero_extendsidi2_speculative,
7809 static gen_func_t gen_ld_sa[] = {
7810 gen_movbi_speculative_advanced,
7811 gen_movqi_speculative_advanced,
7812 gen_movhi_speculative_advanced,
7813 gen_movsi_speculative_advanced,
7814 gen_movdi_speculative_advanced,
7815 gen_movsf_speculative_advanced,
7816 gen_movdf_speculative_advanced,
7817 gen_movxf_speculative_advanced,
7818 gen_movti_speculative_advanced,
7819 gen_zero_extendqidi2_speculative_advanced,
7820 gen_zero_extendhidi2_speculative_advanced,
7821 gen_zero_extendsidi2_speculative_advanced,
7823 static gen_func_t gen_ld_s_a[] = {
7824 gen_movbi_speculative_a,
7825 gen_movqi_speculative_a,
7826 gen_movhi_speculative_a,
7827 gen_movsi_speculative_a,
7828 gen_movdi_speculative_a,
7829 gen_movsf_speculative_a,
7830 gen_movdf_speculative_a,
7831 gen_movxf_speculative_a,
7832 gen_movti_speculative_a,
7833 gen_zero_extendqidi2_speculative_a,
7834 gen_zero_extendhidi2_speculative_a,
7835 gen_zero_extendsidi2_speculative_a,
7838 gen_func_t *gen_ld;
7840 if (ts & BEGIN_DATA)
7842 if (ts & BEGIN_CONTROL)
7843 gen_ld = gen_ld_sa;
7844 else
7845 gen_ld = gen_ld_a;
7847 else if (ts & BEGIN_CONTROL)
7849 if ((spec_info->flags & SEL_SCHED_SPEC_DONT_CHECK_CONTROL)
7850 || ia64_needs_block_p (ts))
7851 gen_ld = gen_ld_s;
7852 else
7853 gen_ld = gen_ld_s_a;
7855 else if (ts == 0)
7856 gen_ld = gen_ld_;
7857 else
7858 gcc_unreachable ();
7860 return gen_ld[mode_no];
7863 /* Constants that help mapping 'enum machine_mode' to int. */
7864 enum SPEC_MODES
7866 SPEC_MODE_INVALID = -1,
7867 SPEC_MODE_FIRST = 0,
7868 SPEC_MODE_FOR_EXTEND_FIRST = 1,
7869 SPEC_MODE_FOR_EXTEND_LAST = 3,
7870 SPEC_MODE_LAST = 8
7873 enum
7875 /* Offset to reach ZERO_EXTEND patterns. */
7876 SPEC_GEN_EXTEND_OFFSET = SPEC_MODE_LAST - SPEC_MODE_FOR_EXTEND_FIRST + 1
7879 /* Return index of the MODE. */
7880 static int
7881 ia64_mode_to_int (enum machine_mode mode)
7883 switch (mode)
7885 case BImode: return 0; /* SPEC_MODE_FIRST */
7886 case QImode: return 1; /* SPEC_MODE_FOR_EXTEND_FIRST */
7887 case HImode: return 2;
7888 case SImode: return 3; /* SPEC_MODE_FOR_EXTEND_LAST */
7889 case DImode: return 4;
7890 case SFmode: return 5;
7891 case DFmode: return 6;
7892 case XFmode: return 7;
7893 case TImode:
7894 /* ??? This mode needs testing. Bypasses for ldfp8 instruction are not
7895 mentioned in itanium[12].md. Predicate fp_register_operand also
7896 needs to be defined. Bottom line: better disable for now. */
7897 return SPEC_MODE_INVALID;
7898 default: return SPEC_MODE_INVALID;
7902 /* Provide information about speculation capabilities. */
7903 static void
7904 ia64_set_sched_flags (spec_info_t spec_info)
7906 unsigned int *flags = &(current_sched_info->flags);
7908 if (*flags & SCHED_RGN
7909 || *flags & SCHED_EBB
7910 || *flags & SEL_SCHED)
7912 int mask = 0;
7914 if ((mflag_sched_br_data_spec && !reload_completed && optimize > 0)
7915 || (mflag_sched_ar_data_spec && reload_completed))
7917 mask |= BEGIN_DATA;
7919 if (!sel_sched_p ()
7920 && ((mflag_sched_br_in_data_spec && !reload_completed)
7921 || (mflag_sched_ar_in_data_spec && reload_completed)))
7922 mask |= BE_IN_DATA;
7925 if (mflag_sched_control_spec
7926 && (!sel_sched_p ()
7927 || reload_completed))
7929 mask |= BEGIN_CONTROL;
7931 if (!sel_sched_p () && mflag_sched_in_control_spec)
7932 mask |= BE_IN_CONTROL;
7935 spec_info->mask = mask;
7937 if (mask)
7939 *flags |= USE_DEPS_LIST | DO_SPECULATION;
7941 if (mask & BE_IN_SPEC)
7942 *flags |= NEW_BBS;
7944 spec_info->flags = 0;
7946 if ((mask & CONTROL_SPEC)
7947 && sel_sched_p () && mflag_sel_sched_dont_check_control_spec)
7948 spec_info->flags |= SEL_SCHED_SPEC_DONT_CHECK_CONTROL;
7950 if (sched_verbose >= 1)
7951 spec_info->dump = sched_dump;
7952 else
7953 spec_info->dump = 0;
7955 if (mflag_sched_count_spec_in_critical_path)
7956 spec_info->flags |= COUNT_SPEC_IN_CRITICAL_PATH;
7959 else
7960 spec_info->mask = 0;
7963 /* If INSN is an appropriate load return its mode.
7964 Return -1 otherwise. */
7965 static int
7966 get_mode_no_for_insn (rtx_insn *insn)
7968 rtx reg, mem, mode_rtx;
7969 int mode_no;
7970 bool extend_p;
7972 extract_insn_cached (insn);
7974 /* We use WHICH_ALTERNATIVE only after reload. This will
7975 guarantee that reload won't touch a speculative insn. */
7977 if (recog_data.n_operands != 2)
7978 return -1;
7980 reg = recog_data.operand[0];
7981 mem = recog_data.operand[1];
7983 /* We should use MEM's mode since REG's mode in presence of
7984 ZERO_EXTEND will always be DImode. */
7985 if (get_attr_speculable1 (insn) == SPECULABLE1_YES)
7986 /* Process non-speculative ld. */
7988 if (!reload_completed)
7990 /* Do not speculate into regs like ar.lc. */
7991 if (!REG_P (reg) || AR_REGNO_P (REGNO (reg)))
7992 return -1;
7994 if (!MEM_P (mem))
7995 return -1;
7998 rtx mem_reg = XEXP (mem, 0);
8000 if (!REG_P (mem_reg))
8001 return -1;
8004 mode_rtx = mem;
8006 else if (get_attr_speculable2 (insn) == SPECULABLE2_YES)
8008 gcc_assert (REG_P (reg) && MEM_P (mem));
8009 mode_rtx = mem;
8011 else
8012 return -1;
8014 else if (get_attr_data_speculative (insn) == DATA_SPECULATIVE_YES
8015 || get_attr_control_speculative (insn) == CONTROL_SPECULATIVE_YES
8016 || get_attr_check_load (insn) == CHECK_LOAD_YES)
8017 /* Process speculative ld or ld.c. */
8019 gcc_assert (REG_P (reg) && MEM_P (mem));
8020 mode_rtx = mem;
8022 else
8024 enum attr_itanium_class attr_class = get_attr_itanium_class (insn);
8026 if (attr_class == ITANIUM_CLASS_CHK_A
8027 || attr_class == ITANIUM_CLASS_CHK_S_I
8028 || attr_class == ITANIUM_CLASS_CHK_S_F)
8029 /* Process chk. */
8030 mode_rtx = reg;
8031 else
8032 return -1;
8035 mode_no = ia64_mode_to_int (GET_MODE (mode_rtx));
8037 if (mode_no == SPEC_MODE_INVALID)
8038 return -1;
8040 extend_p = (GET_MODE (reg) != GET_MODE (mode_rtx));
8042 if (extend_p)
8044 if (!(SPEC_MODE_FOR_EXTEND_FIRST <= mode_no
8045 && mode_no <= SPEC_MODE_FOR_EXTEND_LAST))
8046 return -1;
8048 mode_no += SPEC_GEN_EXTEND_OFFSET;
8051 return mode_no;
8054 /* If X is an unspec part of a speculative load, return its code.
8055 Return -1 otherwise. */
8056 static int
8057 get_spec_unspec_code (const_rtx x)
8059 if (GET_CODE (x) != UNSPEC)
8060 return -1;
8063 int code;
8065 code = XINT (x, 1);
8067 switch (code)
8069 case UNSPEC_LDA:
8070 case UNSPEC_LDS:
8071 case UNSPEC_LDS_A:
8072 case UNSPEC_LDSA:
8073 return code;
8075 default:
8076 return -1;
8081 /* Implement skip_rtx_p hook. */
8082 static bool
8083 ia64_skip_rtx_p (const_rtx x)
8085 return get_spec_unspec_code (x) != -1;
8088 /* If INSN is a speculative load, return its UNSPEC code.
8089 Return -1 otherwise. */
8090 static int
8091 get_insn_spec_code (const_rtx insn)
8093 rtx pat, reg, mem;
8095 pat = PATTERN (insn);
8097 if (GET_CODE (pat) == COND_EXEC)
8098 pat = COND_EXEC_CODE (pat);
8100 if (GET_CODE (pat) != SET)
8101 return -1;
8103 reg = SET_DEST (pat);
8104 if (!REG_P (reg))
8105 return -1;
8107 mem = SET_SRC (pat);
8108 if (GET_CODE (mem) == ZERO_EXTEND)
8109 mem = XEXP (mem, 0);
8111 return get_spec_unspec_code (mem);
8114 /* If INSN is a speculative load, return a ds with the speculation types.
8115 Otherwise [if INSN is a normal instruction] return 0. */
8116 static ds_t
8117 ia64_get_insn_spec_ds (rtx_insn *insn)
8119 int code = get_insn_spec_code (insn);
8121 switch (code)
8123 case UNSPEC_LDA:
8124 return BEGIN_DATA;
8126 case UNSPEC_LDS:
8127 case UNSPEC_LDS_A:
8128 return BEGIN_CONTROL;
8130 case UNSPEC_LDSA:
8131 return BEGIN_DATA | BEGIN_CONTROL;
8133 default:
8134 return 0;
8138 /* If INSN is a speculative load return a ds with the speculation types that
8139 will be checked.
8140 Otherwise [if INSN is a normal instruction] return 0. */
8141 static ds_t
8142 ia64_get_insn_checked_ds (rtx_insn *insn)
8144 int code = get_insn_spec_code (insn);
8146 switch (code)
8148 case UNSPEC_LDA:
8149 return BEGIN_DATA | BEGIN_CONTROL;
8151 case UNSPEC_LDS:
8152 return BEGIN_CONTROL;
8154 case UNSPEC_LDS_A:
8155 case UNSPEC_LDSA:
8156 return BEGIN_DATA | BEGIN_CONTROL;
8158 default:
8159 return 0;
8163 /* If GEN_P is true, calculate the index of needed speculation check and return
8164 speculative pattern for INSN with speculative mode TS, machine mode
8165 MODE_NO and with ZERO_EXTEND (if EXTEND_P is true).
8166 If GEN_P is false, just calculate the index of needed speculation check. */
8167 static rtx
8168 ia64_gen_spec_load (rtx insn, ds_t ts, int mode_no)
8170 rtx pat, new_pat;
8171 gen_func_t gen_load;
8173 gen_load = get_spec_load_gen_function (ts, mode_no);
8175 new_pat = gen_load (copy_rtx (recog_data.operand[0]),
8176 copy_rtx (recog_data.operand[1]));
8178 pat = PATTERN (insn);
8179 if (GET_CODE (pat) == COND_EXEC)
8180 new_pat = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (COND_EXEC_TEST (pat)),
8181 new_pat);
8183 return new_pat;
8186 static bool
8187 insn_can_be_in_speculative_p (rtx insn ATTRIBUTE_UNUSED,
8188 ds_t ds ATTRIBUTE_UNUSED)
8190 return false;
8193 /* Implement targetm.sched.speculate_insn hook.
8194 Check if the INSN can be TS speculative.
8195 If 'no' - return -1.
8196 If 'yes' - generate speculative pattern in the NEW_PAT and return 1.
8197 If current pattern of the INSN already provides TS speculation,
8198 return 0. */
8199 static int
8200 ia64_speculate_insn (rtx_insn *insn, ds_t ts, rtx *new_pat)
8202 int mode_no;
8203 int res;
8205 gcc_assert (!(ts & ~SPECULATIVE));
8207 if (ia64_spec_check_p (insn))
8208 return -1;
8210 if ((ts & BE_IN_SPEC)
8211 && !insn_can_be_in_speculative_p (insn, ts))
8212 return -1;
8214 mode_no = get_mode_no_for_insn (insn);
8216 if (mode_no != SPEC_MODE_INVALID)
8218 if (ia64_get_insn_spec_ds (insn) == ds_get_speculation_types (ts))
8219 res = 0;
8220 else
8222 res = 1;
8223 *new_pat = ia64_gen_spec_load (insn, ts, mode_no);
8226 else
8227 res = -1;
8229 return res;
8232 /* Return a function that will generate a check for speculation TS with mode
8233 MODE_NO.
8234 If simple check is needed, pass true for SIMPLE_CHECK_P.
8235 If clearing check is needed, pass true for CLEARING_CHECK_P. */
8236 static gen_func_t
8237 get_spec_check_gen_function (ds_t ts, int mode_no,
8238 bool simple_check_p, bool clearing_check_p)
8240 static gen_func_t gen_ld_c_clr[] = {
8241 gen_movbi_clr,
8242 gen_movqi_clr,
8243 gen_movhi_clr,
8244 gen_movsi_clr,
8245 gen_movdi_clr,
8246 gen_movsf_clr,
8247 gen_movdf_clr,
8248 gen_movxf_clr,
8249 gen_movti_clr,
8250 gen_zero_extendqidi2_clr,
8251 gen_zero_extendhidi2_clr,
8252 gen_zero_extendsidi2_clr,
8254 static gen_func_t gen_ld_c_nc[] = {
8255 gen_movbi_nc,
8256 gen_movqi_nc,
8257 gen_movhi_nc,
8258 gen_movsi_nc,
8259 gen_movdi_nc,
8260 gen_movsf_nc,
8261 gen_movdf_nc,
8262 gen_movxf_nc,
8263 gen_movti_nc,
8264 gen_zero_extendqidi2_nc,
8265 gen_zero_extendhidi2_nc,
8266 gen_zero_extendsidi2_nc,
8268 static gen_func_t gen_chk_a_clr[] = {
8269 gen_advanced_load_check_clr_bi,
8270 gen_advanced_load_check_clr_qi,
8271 gen_advanced_load_check_clr_hi,
8272 gen_advanced_load_check_clr_si,
8273 gen_advanced_load_check_clr_di,
8274 gen_advanced_load_check_clr_sf,
8275 gen_advanced_load_check_clr_df,
8276 gen_advanced_load_check_clr_xf,
8277 gen_advanced_load_check_clr_ti,
8278 gen_advanced_load_check_clr_di,
8279 gen_advanced_load_check_clr_di,
8280 gen_advanced_load_check_clr_di,
8282 static gen_func_t gen_chk_a_nc[] = {
8283 gen_advanced_load_check_nc_bi,
8284 gen_advanced_load_check_nc_qi,
8285 gen_advanced_load_check_nc_hi,
8286 gen_advanced_load_check_nc_si,
8287 gen_advanced_load_check_nc_di,
8288 gen_advanced_load_check_nc_sf,
8289 gen_advanced_load_check_nc_df,
8290 gen_advanced_load_check_nc_xf,
8291 gen_advanced_load_check_nc_ti,
8292 gen_advanced_load_check_nc_di,
8293 gen_advanced_load_check_nc_di,
8294 gen_advanced_load_check_nc_di,
8296 static gen_func_t gen_chk_s[] = {
8297 gen_speculation_check_bi,
8298 gen_speculation_check_qi,
8299 gen_speculation_check_hi,
8300 gen_speculation_check_si,
8301 gen_speculation_check_di,
8302 gen_speculation_check_sf,
8303 gen_speculation_check_df,
8304 gen_speculation_check_xf,
8305 gen_speculation_check_ti,
8306 gen_speculation_check_di,
8307 gen_speculation_check_di,
8308 gen_speculation_check_di,
8311 gen_func_t *gen_check;
8313 if (ts & BEGIN_DATA)
8315 /* We don't need recovery because even if this is ld.sa
8316 ALAT entry will be allocated only if NAT bit is set to zero.
8317 So it is enough to use ld.c here. */
8319 if (simple_check_p)
8321 gcc_assert (mflag_sched_spec_ldc);
8323 if (clearing_check_p)
8324 gen_check = gen_ld_c_clr;
8325 else
8326 gen_check = gen_ld_c_nc;
8328 else
8330 if (clearing_check_p)
8331 gen_check = gen_chk_a_clr;
8332 else
8333 gen_check = gen_chk_a_nc;
8336 else if (ts & BEGIN_CONTROL)
8338 if (simple_check_p)
8339 /* We might want to use ld.sa -> ld.c instead of
8340 ld.s -> chk.s. */
8342 gcc_assert (!ia64_needs_block_p (ts));
8344 if (clearing_check_p)
8345 gen_check = gen_ld_c_clr;
8346 else
8347 gen_check = gen_ld_c_nc;
8349 else
8351 gen_check = gen_chk_s;
8354 else
8355 gcc_unreachable ();
8357 gcc_assert (mode_no >= 0);
8358 return gen_check[mode_no];
8361 /* Return nonzero, if INSN needs branchy recovery check. */
8362 static bool
8363 ia64_needs_block_p (ds_t ts)
8365 if (ts & BEGIN_DATA)
8366 return !mflag_sched_spec_ldc;
8368 gcc_assert ((ts & BEGIN_CONTROL) != 0);
8370 return !(mflag_sched_spec_control_ldc && mflag_sched_spec_ldc);
8373 /* Generate (or regenerate) a recovery check for INSN. */
8374 static rtx
8375 ia64_gen_spec_check (rtx_insn *insn, rtx_insn *label, ds_t ds)
8377 rtx op1, pat, check_pat;
8378 gen_func_t gen_check;
8379 int mode_no;
8381 mode_no = get_mode_no_for_insn (insn);
8382 gcc_assert (mode_no >= 0);
8384 if (label)
8385 op1 = label;
8386 else
8388 gcc_assert (!ia64_needs_block_p (ds));
8389 op1 = copy_rtx (recog_data.operand[1]);
8392 gen_check = get_spec_check_gen_function (ds, mode_no, label == NULL_RTX,
8393 true);
8395 check_pat = gen_check (copy_rtx (recog_data.operand[0]), op1);
8397 pat = PATTERN (insn);
8398 if (GET_CODE (pat) == COND_EXEC)
8399 check_pat = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (COND_EXEC_TEST (pat)),
8400 check_pat);
8402 return check_pat;
8405 /* Return nonzero, if X is branchy recovery check. */
8406 static int
8407 ia64_spec_check_p (rtx x)
8409 x = PATTERN (x);
8410 if (GET_CODE (x) == COND_EXEC)
8411 x = COND_EXEC_CODE (x);
8412 if (GET_CODE (x) == SET)
8413 return ia64_spec_check_src_p (SET_SRC (x));
8414 return 0;
8417 /* Return nonzero, if SRC belongs to recovery check. */
8418 static int
8419 ia64_spec_check_src_p (rtx src)
8421 if (GET_CODE (src) == IF_THEN_ELSE)
8423 rtx t;
8425 t = XEXP (src, 0);
8426 if (GET_CODE (t) == NE)
8428 t = XEXP (t, 0);
8430 if (GET_CODE (t) == UNSPEC)
8432 int code;
8434 code = XINT (t, 1);
8436 if (code == UNSPEC_LDCCLR
8437 || code == UNSPEC_LDCNC
8438 || code == UNSPEC_CHKACLR
8439 || code == UNSPEC_CHKANC
8440 || code == UNSPEC_CHKS)
8442 gcc_assert (code != 0);
8443 return code;
8448 return 0;
8452 /* The following page contains abstract data `bundle states' which are
8453 used for bundling insns (inserting nops and template generation). */
8455 /* The following describes state of insn bundling. */
8457 struct bundle_state
8459 /* Unique bundle state number to identify them in the debugging
8460 output */
8461 int unique_num;
8462 rtx_insn *insn; /* corresponding insn, NULL for the 1st and the last state */
8463 /* number nops before and after the insn */
8464 short before_nops_num, after_nops_num;
8465 int insn_num; /* insn number (0 - for initial state, 1 - for the 1st
8466 insn */
8467 int cost; /* cost of the state in cycles */
8468 int accumulated_insns_num; /* number of all previous insns including
8469 nops. L is considered as 2 insns */
8470 int branch_deviation; /* deviation of previous branches from 3rd slots */
8471 int middle_bundle_stops; /* number of stop bits in the middle of bundles */
8472 struct bundle_state *next; /* next state with the same insn_num */
8473 struct bundle_state *originator; /* originator (previous insn state) */
8474 /* All bundle states are in the following chain. */
8475 struct bundle_state *allocated_states_chain;
8476 /* The DFA State after issuing the insn and the nops. */
8477 state_t dfa_state;
8480 /* The following is map insn number to the corresponding bundle state. */
8482 static struct bundle_state **index_to_bundle_states;
8484 /* The unique number of next bundle state. */
8486 static int bundle_states_num;
8488 /* All allocated bundle states are in the following chain. */
8490 static struct bundle_state *allocated_bundle_states_chain;
8492 /* All allocated but not used bundle states are in the following
8493 chain. */
8495 static struct bundle_state *free_bundle_state_chain;
8498 /* The following function returns a free bundle state. */
8500 static struct bundle_state *
8501 get_free_bundle_state (void)
8503 struct bundle_state *result;
8505 if (free_bundle_state_chain != NULL)
8507 result = free_bundle_state_chain;
8508 free_bundle_state_chain = result->next;
8510 else
8512 result = XNEW (struct bundle_state);
8513 result->dfa_state = xmalloc (dfa_state_size);
8514 result->allocated_states_chain = allocated_bundle_states_chain;
8515 allocated_bundle_states_chain = result;
8517 result->unique_num = bundle_states_num++;
8518 return result;
8522 /* The following function frees given bundle state. */
8524 static void
8525 free_bundle_state (struct bundle_state *state)
8527 state->next = free_bundle_state_chain;
8528 free_bundle_state_chain = state;
8531 /* Start work with abstract data `bundle states'. */
8533 static void
8534 initiate_bundle_states (void)
8536 bundle_states_num = 0;
8537 free_bundle_state_chain = NULL;
8538 allocated_bundle_states_chain = NULL;
8541 /* Finish work with abstract data `bundle states'. */
8543 static void
8544 finish_bundle_states (void)
8546 struct bundle_state *curr_state, *next_state;
8548 for (curr_state = allocated_bundle_states_chain;
8549 curr_state != NULL;
8550 curr_state = next_state)
8552 next_state = curr_state->allocated_states_chain;
8553 free (curr_state->dfa_state);
8554 free (curr_state);
8558 /* Hashtable helpers. */
8560 struct bundle_state_hasher : typed_noop_remove <bundle_state>
8562 typedef bundle_state value_type;
8563 typedef bundle_state compare_type;
8564 static inline hashval_t hash (const value_type *);
8565 static inline bool equal (const value_type *, const compare_type *);
8568 /* The function returns hash of BUNDLE_STATE. */
8570 inline hashval_t
8571 bundle_state_hasher::hash (const value_type *state)
8573 unsigned result, i;
8575 for (result = i = 0; i < dfa_state_size; i++)
8576 result += (((unsigned char *) state->dfa_state) [i]
8577 << ((i % CHAR_BIT) * 3 + CHAR_BIT));
8578 return result + state->insn_num;
8581 /* The function returns nonzero if the bundle state keys are equal. */
8583 inline bool
8584 bundle_state_hasher::equal (const value_type *state1,
8585 const compare_type *state2)
8587 return (state1->insn_num == state2->insn_num
8588 && memcmp (state1->dfa_state, state2->dfa_state,
8589 dfa_state_size) == 0);
8592 /* Hash table of the bundle states. The key is dfa_state and insn_num
8593 of the bundle states. */
8595 static hash_table<bundle_state_hasher> *bundle_state_table;
8597 /* The function inserts the BUNDLE_STATE into the hash table. The
8598 function returns nonzero if the bundle has been inserted into the
8599 table. The table contains the best bundle state with given key. */
8601 static int
8602 insert_bundle_state (struct bundle_state *bundle_state)
8604 struct bundle_state **entry_ptr;
8606 entry_ptr = bundle_state_table->find_slot (bundle_state, INSERT);
8607 if (*entry_ptr == NULL)
8609 bundle_state->next = index_to_bundle_states [bundle_state->insn_num];
8610 index_to_bundle_states [bundle_state->insn_num] = bundle_state;
8611 *entry_ptr = bundle_state;
8612 return TRUE;
8614 else if (bundle_state->cost < (*entry_ptr)->cost
8615 || (bundle_state->cost == (*entry_ptr)->cost
8616 && ((*entry_ptr)->accumulated_insns_num
8617 > bundle_state->accumulated_insns_num
8618 || ((*entry_ptr)->accumulated_insns_num
8619 == bundle_state->accumulated_insns_num
8620 && ((*entry_ptr)->branch_deviation
8621 > bundle_state->branch_deviation
8622 || ((*entry_ptr)->branch_deviation
8623 == bundle_state->branch_deviation
8624 && (*entry_ptr)->middle_bundle_stops
8625 > bundle_state->middle_bundle_stops))))))
8628 struct bundle_state temp;
8630 temp = **entry_ptr;
8631 **entry_ptr = *bundle_state;
8632 (*entry_ptr)->next = temp.next;
8633 *bundle_state = temp;
8635 return FALSE;
8638 /* Start work with the hash table. */
8640 static void
8641 initiate_bundle_state_table (void)
8643 bundle_state_table = new hash_table<bundle_state_hasher> (50);
8646 /* Finish work with the hash table. */
8648 static void
8649 finish_bundle_state_table (void)
8651 delete bundle_state_table;
8652 bundle_state_table = NULL;
8657 /* The following variable is a insn `nop' used to check bundle states
8658 with different number of inserted nops. */
8660 static rtx_insn *ia64_nop;
8662 /* The following function tries to issue NOPS_NUM nops for the current
8663 state without advancing processor cycle. If it failed, the
8664 function returns FALSE and frees the current state. */
8666 static int
8667 try_issue_nops (struct bundle_state *curr_state, int nops_num)
8669 int i;
8671 for (i = 0; i < nops_num; i++)
8672 if (state_transition (curr_state->dfa_state, ia64_nop) >= 0)
8674 free_bundle_state (curr_state);
8675 return FALSE;
8677 return TRUE;
8680 /* The following function tries to issue INSN for the current
8681 state without advancing processor cycle. If it failed, the
8682 function returns FALSE and frees the current state. */
8684 static int
8685 try_issue_insn (struct bundle_state *curr_state, rtx insn)
8687 if (insn && state_transition (curr_state->dfa_state, insn) >= 0)
8689 free_bundle_state (curr_state);
8690 return FALSE;
8692 return TRUE;
8695 /* The following function tries to issue BEFORE_NOPS_NUM nops and INSN
8696 starting with ORIGINATOR without advancing processor cycle. If
8697 TRY_BUNDLE_END_P is TRUE, the function also/only (if
8698 ONLY_BUNDLE_END_P is TRUE) tries to issue nops to fill all bundle.
8699 If it was successful, the function creates new bundle state and
8700 insert into the hash table and into `index_to_bundle_states'. */
8702 static void
8703 issue_nops_and_insn (struct bundle_state *originator, int before_nops_num,
8704 rtx_insn *insn, int try_bundle_end_p,
8705 int only_bundle_end_p)
8707 struct bundle_state *curr_state;
8709 curr_state = get_free_bundle_state ();
8710 memcpy (curr_state->dfa_state, originator->dfa_state, dfa_state_size);
8711 curr_state->insn = insn;
8712 curr_state->insn_num = originator->insn_num + 1;
8713 curr_state->cost = originator->cost;
8714 curr_state->originator = originator;
8715 curr_state->before_nops_num = before_nops_num;
8716 curr_state->after_nops_num = 0;
8717 curr_state->accumulated_insns_num
8718 = originator->accumulated_insns_num + before_nops_num;
8719 curr_state->branch_deviation = originator->branch_deviation;
8720 curr_state->middle_bundle_stops = originator->middle_bundle_stops;
8721 gcc_assert (insn);
8722 if (INSN_CODE (insn) == CODE_FOR_insn_group_barrier)
8724 gcc_assert (GET_MODE (insn) != TImode);
8725 if (!try_issue_nops (curr_state, before_nops_num))
8726 return;
8727 if (!try_issue_insn (curr_state, insn))
8728 return;
8729 memcpy (temp_dfa_state, curr_state->dfa_state, dfa_state_size);
8730 if (curr_state->accumulated_insns_num % 3 != 0)
8731 curr_state->middle_bundle_stops++;
8732 if (state_transition (temp_dfa_state, dfa_pre_cycle_insn) >= 0
8733 && curr_state->accumulated_insns_num % 3 != 0)
8735 free_bundle_state (curr_state);
8736 return;
8739 else if (GET_MODE (insn) != TImode)
8741 if (!try_issue_nops (curr_state, before_nops_num))
8742 return;
8743 if (!try_issue_insn (curr_state, insn))
8744 return;
8745 curr_state->accumulated_insns_num++;
8746 gcc_assert (!unknown_for_bundling_p (insn));
8748 if (ia64_safe_type (insn) == TYPE_L)
8749 curr_state->accumulated_insns_num++;
8751 else
8753 /* If this is an insn that must be first in a group, then don't allow
8754 nops to be emitted before it. Currently, alloc is the only such
8755 supported instruction. */
8756 /* ??? The bundling automatons should handle this for us, but they do
8757 not yet have support for the first_insn attribute. */
8758 if (before_nops_num > 0 && get_attr_first_insn (insn) == FIRST_INSN_YES)
8760 free_bundle_state (curr_state);
8761 return;
8764 state_transition (curr_state->dfa_state, dfa_pre_cycle_insn);
8765 state_transition (curr_state->dfa_state, NULL);
8766 curr_state->cost++;
8767 if (!try_issue_nops (curr_state, before_nops_num))
8768 return;
8769 if (!try_issue_insn (curr_state, insn))
8770 return;
8771 curr_state->accumulated_insns_num++;
8772 if (unknown_for_bundling_p (insn))
8774 /* Finish bundle containing asm insn. */
8775 curr_state->after_nops_num
8776 = 3 - curr_state->accumulated_insns_num % 3;
8777 curr_state->accumulated_insns_num
8778 += 3 - curr_state->accumulated_insns_num % 3;
8780 else if (ia64_safe_type (insn) == TYPE_L)
8781 curr_state->accumulated_insns_num++;
8783 if (ia64_safe_type (insn) == TYPE_B)
8784 curr_state->branch_deviation
8785 += 2 - (curr_state->accumulated_insns_num - 1) % 3;
8786 if (try_bundle_end_p && curr_state->accumulated_insns_num % 3 != 0)
8788 if (!only_bundle_end_p && insert_bundle_state (curr_state))
8790 state_t dfa_state;
8791 struct bundle_state *curr_state1;
8792 struct bundle_state *allocated_states_chain;
8794 curr_state1 = get_free_bundle_state ();
8795 dfa_state = curr_state1->dfa_state;
8796 allocated_states_chain = curr_state1->allocated_states_chain;
8797 *curr_state1 = *curr_state;
8798 curr_state1->dfa_state = dfa_state;
8799 curr_state1->allocated_states_chain = allocated_states_chain;
8800 memcpy (curr_state1->dfa_state, curr_state->dfa_state,
8801 dfa_state_size);
8802 curr_state = curr_state1;
8804 if (!try_issue_nops (curr_state,
8805 3 - curr_state->accumulated_insns_num % 3))
8806 return;
8807 curr_state->after_nops_num
8808 = 3 - curr_state->accumulated_insns_num % 3;
8809 curr_state->accumulated_insns_num
8810 += 3 - curr_state->accumulated_insns_num % 3;
8812 if (!insert_bundle_state (curr_state))
8813 free_bundle_state (curr_state);
8814 return;
8817 /* The following function returns position in the two window bundle
8818 for given STATE. */
8820 static int
8821 get_max_pos (state_t state)
8823 if (cpu_unit_reservation_p (state, pos_6))
8824 return 6;
8825 else if (cpu_unit_reservation_p (state, pos_5))
8826 return 5;
8827 else if (cpu_unit_reservation_p (state, pos_4))
8828 return 4;
8829 else if (cpu_unit_reservation_p (state, pos_3))
8830 return 3;
8831 else if (cpu_unit_reservation_p (state, pos_2))
8832 return 2;
8833 else if (cpu_unit_reservation_p (state, pos_1))
8834 return 1;
8835 else
8836 return 0;
8839 /* The function returns code of a possible template for given position
8840 and state. The function should be called only with 2 values of
8841 position equal to 3 or 6. We avoid generating F NOPs by putting
8842 templates containing F insns at the end of the template search
8843 because undocumented anomaly in McKinley derived cores which can
8844 cause stalls if an F-unit insn (including a NOP) is issued within a
8845 six-cycle window after reading certain application registers (such
8846 as ar.bsp). Furthermore, power-considerations also argue against
8847 the use of F-unit instructions unless they're really needed. */
8849 static int
8850 get_template (state_t state, int pos)
8852 switch (pos)
8854 case 3:
8855 if (cpu_unit_reservation_p (state, _0mmi_))
8856 return 1;
8857 else if (cpu_unit_reservation_p (state, _0mii_))
8858 return 0;
8859 else if (cpu_unit_reservation_p (state, _0mmb_))
8860 return 7;
8861 else if (cpu_unit_reservation_p (state, _0mib_))
8862 return 6;
8863 else if (cpu_unit_reservation_p (state, _0mbb_))
8864 return 5;
8865 else if (cpu_unit_reservation_p (state, _0bbb_))
8866 return 4;
8867 else if (cpu_unit_reservation_p (state, _0mmf_))
8868 return 3;
8869 else if (cpu_unit_reservation_p (state, _0mfi_))
8870 return 2;
8871 else if (cpu_unit_reservation_p (state, _0mfb_))
8872 return 8;
8873 else if (cpu_unit_reservation_p (state, _0mlx_))
8874 return 9;
8875 else
8876 gcc_unreachable ();
8877 case 6:
8878 if (cpu_unit_reservation_p (state, _1mmi_))
8879 return 1;
8880 else if (cpu_unit_reservation_p (state, _1mii_))
8881 return 0;
8882 else if (cpu_unit_reservation_p (state, _1mmb_))
8883 return 7;
8884 else if (cpu_unit_reservation_p (state, _1mib_))
8885 return 6;
8886 else if (cpu_unit_reservation_p (state, _1mbb_))
8887 return 5;
8888 else if (cpu_unit_reservation_p (state, _1bbb_))
8889 return 4;
8890 else if (_1mmf_ >= 0 && cpu_unit_reservation_p (state, _1mmf_))
8891 return 3;
8892 else if (cpu_unit_reservation_p (state, _1mfi_))
8893 return 2;
8894 else if (cpu_unit_reservation_p (state, _1mfb_))
8895 return 8;
8896 else if (cpu_unit_reservation_p (state, _1mlx_))
8897 return 9;
8898 else
8899 gcc_unreachable ();
8900 default:
8901 gcc_unreachable ();
8905 /* True when INSN is important for bundling. */
8907 static bool
8908 important_for_bundling_p (rtx_insn *insn)
8910 return (INSN_P (insn)
8911 && ia64_safe_itanium_class (insn) != ITANIUM_CLASS_IGNORE
8912 && GET_CODE (PATTERN (insn)) != USE
8913 && GET_CODE (PATTERN (insn)) != CLOBBER);
8916 /* The following function returns an insn important for insn bundling
8917 followed by INSN and before TAIL. */
8919 static rtx_insn *
8920 get_next_important_insn (rtx_insn *insn, rtx_insn *tail)
8922 for (; insn && insn != tail; insn = NEXT_INSN (insn))
8923 if (important_for_bundling_p (insn))
8924 return insn;
8925 return NULL;
8928 /* True when INSN is unknown, but important, for bundling. */
8930 static bool
8931 unknown_for_bundling_p (rtx_insn *insn)
8933 return (INSN_P (insn)
8934 && ia64_safe_itanium_class (insn) == ITANIUM_CLASS_UNKNOWN
8935 && GET_CODE (PATTERN (insn)) != USE
8936 && GET_CODE (PATTERN (insn)) != CLOBBER);
8939 /* Add a bundle selector TEMPLATE0 before INSN. */
8941 static void
8942 ia64_add_bundle_selector_before (int template0, rtx_insn *insn)
8944 rtx b = gen_bundle_selector (GEN_INT (template0));
8946 ia64_emit_insn_before (b, insn);
8947 #if NR_BUNDLES == 10
8948 if ((template0 == 4 || template0 == 5)
8949 && ia64_except_unwind_info (&global_options) == UI_TARGET)
8951 int i;
8952 rtx note = NULL_RTX;
8954 /* In .mbb and .bbb bundles, check if CALL_INSN isn't in the
8955 first or second slot. If it is and has REG_EH_NOTE set, copy it
8956 to following nops, as br.call sets rp to the address of following
8957 bundle and therefore an EH region end must be on a bundle
8958 boundary. */
8959 insn = PREV_INSN (insn);
8960 for (i = 0; i < 3; i++)
8963 insn = next_active_insn (insn);
8964 while (NONJUMP_INSN_P (insn)
8965 && get_attr_empty (insn) == EMPTY_YES);
8966 if (CALL_P (insn))
8967 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
8968 else if (note)
8970 int code;
8972 gcc_assert ((code = recog_memoized (insn)) == CODE_FOR_nop
8973 || code == CODE_FOR_nop_b);
8974 if (find_reg_note (insn, REG_EH_REGION, NULL_RTX))
8975 note = NULL_RTX;
8976 else
8977 add_reg_note (insn, REG_EH_REGION, XEXP (note, 0));
8981 #endif
8984 /* The following function does insn bundling. Bundling means
8985 inserting templates and nop insns to fit insn groups into permitted
8986 templates. Instruction scheduling uses NDFA (non-deterministic
8987 finite automata) encoding informations about the templates and the
8988 inserted nops. Nondeterminism of the automata permits follows
8989 all possible insn sequences very fast.
8991 Unfortunately it is not possible to get information about inserting
8992 nop insns and used templates from the automata states. The
8993 automata only says that we can issue an insn possibly inserting
8994 some nops before it and using some template. Therefore insn
8995 bundling in this function is implemented by using DFA
8996 (deterministic finite automata). We follow all possible insn
8997 sequences by inserting 0-2 nops (that is what the NDFA describe for
8998 insn scheduling) before/after each insn being bundled. We know the
8999 start of simulated processor cycle from insn scheduling (insn
9000 starting a new cycle has TImode).
9002 Simple implementation of insn bundling would create enormous
9003 number of possible insn sequences satisfying information about new
9004 cycle ticks taken from the insn scheduling. To make the algorithm
9005 practical we use dynamic programming. Each decision (about
9006 inserting nops and implicitly about previous decisions) is described
9007 by structure bundle_state (see above). If we generate the same
9008 bundle state (key is automaton state after issuing the insns and
9009 nops for it), we reuse already generated one. As consequence we
9010 reject some decisions which cannot improve the solution and
9011 reduce memory for the algorithm.
9013 When we reach the end of EBB (extended basic block), we choose the
9014 best sequence and then, moving back in EBB, insert templates for
9015 the best alternative. The templates are taken from querying
9016 automaton state for each insn in chosen bundle states.
9018 So the algorithm makes two (forward and backward) passes through
9019 EBB. */
9021 static void
9022 bundling (FILE *dump, int verbose, rtx_insn *prev_head_insn, rtx_insn *tail)
9024 struct bundle_state *curr_state, *next_state, *best_state;
9025 rtx_insn *insn, *next_insn;
9026 int insn_num;
9027 int i, bundle_end_p, only_bundle_end_p, asm_p;
9028 int pos = 0, max_pos, template0, template1;
9029 rtx_insn *b;
9030 enum attr_type type;
9032 insn_num = 0;
9033 /* Count insns in the EBB. */
9034 for (insn = NEXT_INSN (prev_head_insn);
9035 insn && insn != tail;
9036 insn = NEXT_INSN (insn))
9037 if (INSN_P (insn))
9038 insn_num++;
9039 if (insn_num == 0)
9040 return;
9041 bundling_p = 1;
9042 dfa_clean_insn_cache ();
9043 initiate_bundle_state_table ();
9044 index_to_bundle_states = XNEWVEC (struct bundle_state *, insn_num + 2);
9045 /* First (forward) pass -- generation of bundle states. */
9046 curr_state = get_free_bundle_state ();
9047 curr_state->insn = NULL;
9048 curr_state->before_nops_num = 0;
9049 curr_state->after_nops_num = 0;
9050 curr_state->insn_num = 0;
9051 curr_state->cost = 0;
9052 curr_state->accumulated_insns_num = 0;
9053 curr_state->branch_deviation = 0;
9054 curr_state->middle_bundle_stops = 0;
9055 curr_state->next = NULL;
9056 curr_state->originator = NULL;
9057 state_reset (curr_state->dfa_state);
9058 index_to_bundle_states [0] = curr_state;
9059 insn_num = 0;
9060 /* Shift cycle mark if it is put on insn which could be ignored. */
9061 for (insn = NEXT_INSN (prev_head_insn);
9062 insn != tail;
9063 insn = NEXT_INSN (insn))
9064 if (INSN_P (insn)
9065 && !important_for_bundling_p (insn)
9066 && GET_MODE (insn) == TImode)
9068 PUT_MODE (insn, VOIDmode);
9069 for (next_insn = NEXT_INSN (insn);
9070 next_insn != tail;
9071 next_insn = NEXT_INSN (next_insn))
9072 if (important_for_bundling_p (next_insn)
9073 && INSN_CODE (next_insn) != CODE_FOR_insn_group_barrier)
9075 PUT_MODE (next_insn, TImode);
9076 break;
9079 /* Forward pass: generation of bundle states. */
9080 for (insn = get_next_important_insn (NEXT_INSN (prev_head_insn), tail);
9081 insn != NULL_RTX;
9082 insn = next_insn)
9084 gcc_assert (important_for_bundling_p (insn));
9085 type = ia64_safe_type (insn);
9086 next_insn = get_next_important_insn (NEXT_INSN (insn), tail);
9087 insn_num++;
9088 index_to_bundle_states [insn_num] = NULL;
9089 for (curr_state = index_to_bundle_states [insn_num - 1];
9090 curr_state != NULL;
9091 curr_state = next_state)
9093 pos = curr_state->accumulated_insns_num % 3;
9094 next_state = curr_state->next;
9095 /* We must fill up the current bundle in order to start a
9096 subsequent asm insn in a new bundle. Asm insn is always
9097 placed in a separate bundle. */
9098 only_bundle_end_p
9099 = (next_insn != NULL_RTX
9100 && INSN_CODE (insn) == CODE_FOR_insn_group_barrier
9101 && unknown_for_bundling_p (next_insn));
9102 /* We may fill up the current bundle if it is the cycle end
9103 without a group barrier. */
9104 bundle_end_p
9105 = (only_bundle_end_p || next_insn == NULL_RTX
9106 || (GET_MODE (next_insn) == TImode
9107 && INSN_CODE (insn) != CODE_FOR_insn_group_barrier));
9108 if (type == TYPE_F || type == TYPE_B || type == TYPE_L
9109 || type == TYPE_S)
9110 issue_nops_and_insn (curr_state, 2, insn, bundle_end_p,
9111 only_bundle_end_p);
9112 issue_nops_and_insn (curr_state, 1, insn, bundle_end_p,
9113 only_bundle_end_p);
9114 issue_nops_and_insn (curr_state, 0, insn, bundle_end_p,
9115 only_bundle_end_p);
9117 gcc_assert (index_to_bundle_states [insn_num]);
9118 for (curr_state = index_to_bundle_states [insn_num];
9119 curr_state != NULL;
9120 curr_state = curr_state->next)
9121 if (verbose >= 2 && dump)
9123 /* This structure is taken from generated code of the
9124 pipeline hazard recognizer (see file insn-attrtab.c).
9125 Please don't forget to change the structure if a new
9126 automaton is added to .md file. */
9127 struct DFA_chip
9129 unsigned short one_automaton_state;
9130 unsigned short oneb_automaton_state;
9131 unsigned short two_automaton_state;
9132 unsigned short twob_automaton_state;
9135 fprintf
9136 (dump,
9137 "// Bundle state %d (orig %d, cost %d, nops %d/%d, insns %d, branch %d, mid.stops %d state %d) for %d\n",
9138 curr_state->unique_num,
9139 (curr_state->originator == NULL
9140 ? -1 : curr_state->originator->unique_num),
9141 curr_state->cost,
9142 curr_state->before_nops_num, curr_state->after_nops_num,
9143 curr_state->accumulated_insns_num, curr_state->branch_deviation,
9144 curr_state->middle_bundle_stops,
9145 ((struct DFA_chip *) curr_state->dfa_state)->twob_automaton_state,
9146 INSN_UID (insn));
9150 /* We should find a solution because the 2nd insn scheduling has
9151 found one. */
9152 gcc_assert (index_to_bundle_states [insn_num]);
9153 /* Find a state corresponding to the best insn sequence. */
9154 best_state = NULL;
9155 for (curr_state = index_to_bundle_states [insn_num];
9156 curr_state != NULL;
9157 curr_state = curr_state->next)
9158 /* We are just looking at the states with fully filled up last
9159 bundle. The first we prefer insn sequences with minimal cost
9160 then with minimal inserted nops and finally with branch insns
9161 placed in the 3rd slots. */
9162 if (curr_state->accumulated_insns_num % 3 == 0
9163 && (best_state == NULL || best_state->cost > curr_state->cost
9164 || (best_state->cost == curr_state->cost
9165 && (curr_state->accumulated_insns_num
9166 < best_state->accumulated_insns_num
9167 || (curr_state->accumulated_insns_num
9168 == best_state->accumulated_insns_num
9169 && (curr_state->branch_deviation
9170 < best_state->branch_deviation
9171 || (curr_state->branch_deviation
9172 == best_state->branch_deviation
9173 && curr_state->middle_bundle_stops
9174 < best_state->middle_bundle_stops)))))))
9175 best_state = curr_state;
9176 /* Second (backward) pass: adding nops and templates. */
9177 gcc_assert (best_state);
9178 insn_num = best_state->before_nops_num;
9179 template0 = template1 = -1;
9180 for (curr_state = best_state;
9181 curr_state->originator != NULL;
9182 curr_state = curr_state->originator)
9184 insn = curr_state->insn;
9185 asm_p = unknown_for_bundling_p (insn);
9186 insn_num++;
9187 if (verbose >= 2 && dump)
9189 struct DFA_chip
9191 unsigned short one_automaton_state;
9192 unsigned short oneb_automaton_state;
9193 unsigned short two_automaton_state;
9194 unsigned short twob_automaton_state;
9197 fprintf
9198 (dump,
9199 "// Best %d (orig %d, cost %d, nops %d/%d, insns %d, branch %d, mid.stops %d, state %d) for %d\n",
9200 curr_state->unique_num,
9201 (curr_state->originator == NULL
9202 ? -1 : curr_state->originator->unique_num),
9203 curr_state->cost,
9204 curr_state->before_nops_num, curr_state->after_nops_num,
9205 curr_state->accumulated_insns_num, curr_state->branch_deviation,
9206 curr_state->middle_bundle_stops,
9207 ((struct DFA_chip *) curr_state->dfa_state)->twob_automaton_state,
9208 INSN_UID (insn));
9210 /* Find the position in the current bundle window. The window can
9211 contain at most two bundles. Two bundle window means that
9212 the processor will make two bundle rotation. */
9213 max_pos = get_max_pos (curr_state->dfa_state);
9214 if (max_pos == 6
9215 /* The following (negative template number) means that the
9216 processor did one bundle rotation. */
9217 || (max_pos == 3 && template0 < 0))
9219 /* We are at the end of the window -- find template(s) for
9220 its bundle(s). */
9221 pos = max_pos;
9222 if (max_pos == 3)
9223 template0 = get_template (curr_state->dfa_state, 3);
9224 else
9226 template1 = get_template (curr_state->dfa_state, 3);
9227 template0 = get_template (curr_state->dfa_state, 6);
9230 if (max_pos > 3 && template1 < 0)
9231 /* It may happen when we have the stop inside a bundle. */
9233 gcc_assert (pos <= 3);
9234 template1 = get_template (curr_state->dfa_state, 3);
9235 pos += 3;
9237 if (!asm_p)
9238 /* Emit nops after the current insn. */
9239 for (i = 0; i < curr_state->after_nops_num; i++)
9241 rtx nop_pat = gen_nop ();
9242 rtx_insn *nop = emit_insn_after (nop_pat, insn);
9243 pos--;
9244 gcc_assert (pos >= 0);
9245 if (pos % 3 == 0)
9247 /* We are at the start of a bundle: emit the template
9248 (it should be defined). */
9249 gcc_assert (template0 >= 0);
9250 ia64_add_bundle_selector_before (template0, nop);
9251 /* If we have two bundle window, we make one bundle
9252 rotation. Otherwise template0 will be undefined
9253 (negative value). */
9254 template0 = template1;
9255 template1 = -1;
9258 /* Move the position backward in the window. Group barrier has
9259 no slot. Asm insn takes all bundle. */
9260 if (INSN_CODE (insn) != CODE_FOR_insn_group_barrier
9261 && !unknown_for_bundling_p (insn))
9262 pos--;
9263 /* Long insn takes 2 slots. */
9264 if (ia64_safe_type (insn) == TYPE_L)
9265 pos--;
9266 gcc_assert (pos >= 0);
9267 if (pos % 3 == 0
9268 && INSN_CODE (insn) != CODE_FOR_insn_group_barrier
9269 && !unknown_for_bundling_p (insn))
9271 /* The current insn is at the bundle start: emit the
9272 template. */
9273 gcc_assert (template0 >= 0);
9274 ia64_add_bundle_selector_before (template0, insn);
9275 b = PREV_INSN (insn);
9276 insn = b;
9277 /* See comment above in analogous place for emitting nops
9278 after the insn. */
9279 template0 = template1;
9280 template1 = -1;
9282 /* Emit nops after the current insn. */
9283 for (i = 0; i < curr_state->before_nops_num; i++)
9285 rtx nop_pat = gen_nop ();
9286 ia64_emit_insn_before (nop_pat, insn);
9287 rtx_insn *nop = PREV_INSN (insn);
9288 insn = nop;
9289 pos--;
9290 gcc_assert (pos >= 0);
9291 if (pos % 3 == 0)
9293 /* See comment above in analogous place for emitting nops
9294 after the insn. */
9295 gcc_assert (template0 >= 0);
9296 ia64_add_bundle_selector_before (template0, insn);
9297 b = PREV_INSN (insn);
9298 insn = b;
9299 template0 = template1;
9300 template1 = -1;
9305 #ifdef ENABLE_CHECKING
9307 /* Assert right calculation of middle_bundle_stops. */
9308 int num = best_state->middle_bundle_stops;
9309 bool start_bundle = true, end_bundle = false;
9311 for (insn = NEXT_INSN (prev_head_insn);
9312 insn && insn != tail;
9313 insn = NEXT_INSN (insn))
9315 if (!INSN_P (insn))
9316 continue;
9317 if (recog_memoized (insn) == CODE_FOR_bundle_selector)
9318 start_bundle = true;
9319 else
9321 rtx_insn *next_insn;
9323 for (next_insn = NEXT_INSN (insn);
9324 next_insn && next_insn != tail;
9325 next_insn = NEXT_INSN (next_insn))
9326 if (INSN_P (next_insn)
9327 && (ia64_safe_itanium_class (next_insn)
9328 != ITANIUM_CLASS_IGNORE
9329 || recog_memoized (next_insn)
9330 == CODE_FOR_bundle_selector)
9331 && GET_CODE (PATTERN (next_insn)) != USE
9332 && GET_CODE (PATTERN (next_insn)) != CLOBBER)
9333 break;
9335 end_bundle = next_insn == NULL_RTX
9336 || next_insn == tail
9337 || (INSN_P (next_insn)
9338 && recog_memoized (next_insn)
9339 == CODE_FOR_bundle_selector);
9340 if (recog_memoized (insn) == CODE_FOR_insn_group_barrier
9341 && !start_bundle && !end_bundle
9342 && next_insn
9343 && !unknown_for_bundling_p (next_insn))
9344 num--;
9346 start_bundle = false;
9350 gcc_assert (num == 0);
9352 #endif
9354 free (index_to_bundle_states);
9355 finish_bundle_state_table ();
9356 bundling_p = 0;
9357 dfa_clean_insn_cache ();
9360 /* The following function is called at the end of scheduling BB or
9361 EBB. After reload, it inserts stop bits and does insn bundling. */
9363 static void
9364 ia64_sched_finish (FILE *dump, int sched_verbose)
9366 if (sched_verbose)
9367 fprintf (dump, "// Finishing schedule.\n");
9368 if (!reload_completed)
9369 return;
9370 if (reload_completed)
9372 final_emit_insn_group_barriers (dump);
9373 bundling (dump, sched_verbose, current_sched_info->prev_head,
9374 current_sched_info->next_tail);
9375 if (sched_verbose && dump)
9376 fprintf (dump, "// finishing %d-%d\n",
9377 INSN_UID (NEXT_INSN (current_sched_info->prev_head)),
9378 INSN_UID (PREV_INSN (current_sched_info->next_tail)));
9380 return;
9384 /* The following function inserts stop bits in scheduled BB or EBB. */
9386 static void
9387 final_emit_insn_group_barriers (FILE *dump ATTRIBUTE_UNUSED)
9389 rtx_insn *insn;
9390 int need_barrier_p = 0;
9391 int seen_good_insn = 0;
9393 init_insn_group_barriers ();
9395 for (insn = NEXT_INSN (current_sched_info->prev_head);
9396 insn != current_sched_info->next_tail;
9397 insn = NEXT_INSN (insn))
9399 if (BARRIER_P (insn))
9401 rtx_insn *last = prev_active_insn (insn);
9403 if (! last)
9404 continue;
9405 if (JUMP_TABLE_DATA_P (last))
9406 last = prev_active_insn (last);
9407 if (recog_memoized (last) != CODE_FOR_insn_group_barrier)
9408 emit_insn_after (gen_insn_group_barrier (GEN_INT (3)), last);
9410 init_insn_group_barriers ();
9411 seen_good_insn = 0;
9412 need_barrier_p = 0;
9414 else if (NONDEBUG_INSN_P (insn))
9416 if (recog_memoized (insn) == CODE_FOR_insn_group_barrier)
9418 init_insn_group_barriers ();
9419 seen_good_insn = 0;
9420 need_barrier_p = 0;
9422 else if (need_barrier_p || group_barrier_needed (insn)
9423 || (mflag_sched_stop_bits_after_every_cycle
9424 && GET_MODE (insn) == TImode
9425 && seen_good_insn))
9427 if (TARGET_EARLY_STOP_BITS)
9429 rtx_insn *last;
9431 for (last = insn;
9432 last != current_sched_info->prev_head;
9433 last = PREV_INSN (last))
9434 if (INSN_P (last) && GET_MODE (last) == TImode
9435 && stops_p [INSN_UID (last)])
9436 break;
9437 if (last == current_sched_info->prev_head)
9438 last = insn;
9439 last = prev_active_insn (last);
9440 if (last
9441 && recog_memoized (last) != CODE_FOR_insn_group_barrier)
9442 emit_insn_after (gen_insn_group_barrier (GEN_INT (3)),
9443 last);
9444 init_insn_group_barriers ();
9445 for (last = NEXT_INSN (last);
9446 last != insn;
9447 last = NEXT_INSN (last))
9448 if (INSN_P (last))
9450 group_barrier_needed (last);
9451 if (recog_memoized (last) >= 0
9452 && important_for_bundling_p (last))
9453 seen_good_insn = 1;
9456 else
9458 emit_insn_before (gen_insn_group_barrier (GEN_INT (3)),
9459 insn);
9460 init_insn_group_barriers ();
9461 seen_good_insn = 0;
9463 group_barrier_needed (insn);
9464 if (recog_memoized (insn) >= 0
9465 && important_for_bundling_p (insn))
9466 seen_good_insn = 1;
9468 else if (recog_memoized (insn) >= 0
9469 && important_for_bundling_p (insn))
9470 seen_good_insn = 1;
9471 need_barrier_p = (CALL_P (insn) || unknown_for_bundling_p (insn));
9478 /* If the following function returns TRUE, we will use the DFA
9479 insn scheduler. */
9481 static int
9482 ia64_first_cycle_multipass_dfa_lookahead (void)
9484 return (reload_completed ? 6 : 4);
9487 /* The following function initiates variable `dfa_pre_cycle_insn'. */
9489 static void
9490 ia64_init_dfa_pre_cycle_insn (void)
9492 if (temp_dfa_state == NULL)
9494 dfa_state_size = state_size ();
9495 temp_dfa_state = xmalloc (dfa_state_size);
9496 prev_cycle_state = xmalloc (dfa_state_size);
9498 dfa_pre_cycle_insn = make_insn_raw (gen_pre_cycle ());
9499 SET_PREV_INSN (dfa_pre_cycle_insn) = SET_NEXT_INSN (dfa_pre_cycle_insn) = NULL_RTX;
9500 recog_memoized (dfa_pre_cycle_insn);
9501 dfa_stop_insn = make_insn_raw (gen_insn_group_barrier (GEN_INT (3)));
9502 SET_PREV_INSN (dfa_stop_insn) = SET_NEXT_INSN (dfa_stop_insn) = NULL_RTX;
9503 recog_memoized (dfa_stop_insn);
9506 /* The following function returns the pseudo insn DFA_PRE_CYCLE_INSN
9507 used by the DFA insn scheduler. */
9509 static rtx
9510 ia64_dfa_pre_cycle_insn (void)
9512 return dfa_pre_cycle_insn;
9515 /* The following function returns TRUE if PRODUCER (of type ilog or
9516 ld) produces address for CONSUMER (of type st or stf). */
9519 ia64_st_address_bypass_p (rtx_insn *producer, rtx_insn *consumer)
9521 rtx dest, reg, mem;
9523 gcc_assert (producer && consumer);
9524 dest = ia64_single_set (producer);
9525 gcc_assert (dest);
9526 reg = SET_DEST (dest);
9527 gcc_assert (reg);
9528 if (GET_CODE (reg) == SUBREG)
9529 reg = SUBREG_REG (reg);
9530 gcc_assert (GET_CODE (reg) == REG);
9532 dest = ia64_single_set (consumer);
9533 gcc_assert (dest);
9534 mem = SET_DEST (dest);
9535 gcc_assert (mem && GET_CODE (mem) == MEM);
9536 return reg_mentioned_p (reg, mem);
9539 /* The following function returns TRUE if PRODUCER (of type ilog or
9540 ld) produces address for CONSUMER (of type ld or fld). */
9543 ia64_ld_address_bypass_p (rtx_insn *producer, rtx_insn *consumer)
9545 rtx dest, src, reg, mem;
9547 gcc_assert (producer && consumer);
9548 dest = ia64_single_set (producer);
9549 gcc_assert (dest);
9550 reg = SET_DEST (dest);
9551 gcc_assert (reg);
9552 if (GET_CODE (reg) == SUBREG)
9553 reg = SUBREG_REG (reg);
9554 gcc_assert (GET_CODE (reg) == REG);
9556 src = ia64_single_set (consumer);
9557 gcc_assert (src);
9558 mem = SET_SRC (src);
9559 gcc_assert (mem);
9561 if (GET_CODE (mem) == UNSPEC && XVECLEN (mem, 0) > 0)
9562 mem = XVECEXP (mem, 0, 0);
9563 else if (GET_CODE (mem) == IF_THEN_ELSE)
9564 /* ??? Is this bypass necessary for ld.c? */
9566 gcc_assert (XINT (XEXP (XEXP (mem, 0), 0), 1) == UNSPEC_LDCCLR);
9567 mem = XEXP (mem, 1);
9570 while (GET_CODE (mem) == SUBREG || GET_CODE (mem) == ZERO_EXTEND)
9571 mem = XEXP (mem, 0);
9573 if (GET_CODE (mem) == UNSPEC)
9575 int c = XINT (mem, 1);
9577 gcc_assert (c == UNSPEC_LDA || c == UNSPEC_LDS || c == UNSPEC_LDS_A
9578 || c == UNSPEC_LDSA);
9579 mem = XVECEXP (mem, 0, 0);
9582 /* Note that LO_SUM is used for GOT loads. */
9583 gcc_assert (GET_CODE (mem) == LO_SUM || GET_CODE (mem) == MEM);
9585 return reg_mentioned_p (reg, mem);
9588 /* The following function returns TRUE if INSN produces address for a
9589 load/store insn. We will place such insns into M slot because it
9590 decreases its latency time. */
9593 ia64_produce_address_p (rtx insn)
9595 return insn->call;
9599 /* Emit pseudo-ops for the assembler to describe predicate relations.
9600 At present this assumes that we only consider predicate pairs to
9601 be mutex, and that the assembler can deduce proper values from
9602 straight-line code. */
9604 static void
9605 emit_predicate_relation_info (void)
9607 basic_block bb;
9609 FOR_EACH_BB_REVERSE_FN (bb, cfun)
9611 int r;
9612 rtx_insn *head = BB_HEAD (bb);
9614 /* We only need such notes at code labels. */
9615 if (! LABEL_P (head))
9616 continue;
9617 if (NOTE_INSN_BASIC_BLOCK_P (NEXT_INSN (head)))
9618 head = NEXT_INSN (head);
9620 /* Skip p0, which may be thought to be live due to (reg:DI p0)
9621 grabbing the entire block of predicate registers. */
9622 for (r = PR_REG (2); r < PR_REG (64); r += 2)
9623 if (REGNO_REG_SET_P (df_get_live_in (bb), r))
9625 rtx p = gen_rtx_REG (BImode, r);
9626 rtx_insn *n = emit_insn_after (gen_pred_rel_mutex (p), head);
9627 if (head == BB_END (bb))
9628 BB_END (bb) = n;
9629 head = n;
9633 /* Look for conditional calls that do not return, and protect predicate
9634 relations around them. Otherwise the assembler will assume the call
9635 returns, and complain about uses of call-clobbered predicates after
9636 the call. */
9637 FOR_EACH_BB_REVERSE_FN (bb, cfun)
9639 rtx_insn *insn = BB_HEAD (bb);
9641 while (1)
9643 if (CALL_P (insn)
9644 && GET_CODE (PATTERN (insn)) == COND_EXEC
9645 && find_reg_note (insn, REG_NORETURN, NULL_RTX))
9647 rtx_insn *b =
9648 emit_insn_before (gen_safe_across_calls_all (), insn);
9649 rtx_insn *a = emit_insn_after (gen_safe_across_calls_normal (), insn);
9650 if (BB_HEAD (bb) == insn)
9651 BB_HEAD (bb) = b;
9652 if (BB_END (bb) == insn)
9653 BB_END (bb) = a;
9656 if (insn == BB_END (bb))
9657 break;
9658 insn = NEXT_INSN (insn);
9663 /* Perform machine dependent operations on the rtl chain INSNS. */
9665 static void
9666 ia64_reorg (void)
9668 /* We are freeing block_for_insn in the toplev to keep compatibility
9669 with old MDEP_REORGS that are not CFG based. Recompute it now. */
9670 compute_bb_for_insn ();
9672 /* If optimizing, we'll have split before scheduling. */
9673 if (optimize == 0)
9674 split_all_insns ();
9676 if (optimize && flag_schedule_insns_after_reload
9677 && dbg_cnt (ia64_sched2))
9679 basic_block bb;
9680 timevar_push (TV_SCHED2);
9681 ia64_final_schedule = 1;
9683 /* We can't let modulo-sched prevent us from scheduling any bbs,
9684 since we need the final schedule to produce bundle information. */
9685 FOR_EACH_BB_FN (bb, cfun)
9686 bb->flags &= ~BB_DISABLE_SCHEDULE;
9688 initiate_bundle_states ();
9689 ia64_nop = make_insn_raw (gen_nop ());
9690 SET_PREV_INSN (ia64_nop) = SET_NEXT_INSN (ia64_nop) = NULL_RTX;
9691 recog_memoized (ia64_nop);
9692 clocks_length = get_max_uid () + 1;
9693 stops_p = XCNEWVEC (char, clocks_length);
9695 if (ia64_tune == PROCESSOR_ITANIUM2)
9697 pos_1 = get_cpu_unit_code ("2_1");
9698 pos_2 = get_cpu_unit_code ("2_2");
9699 pos_3 = get_cpu_unit_code ("2_3");
9700 pos_4 = get_cpu_unit_code ("2_4");
9701 pos_5 = get_cpu_unit_code ("2_5");
9702 pos_6 = get_cpu_unit_code ("2_6");
9703 _0mii_ = get_cpu_unit_code ("2b_0mii.");
9704 _0mmi_ = get_cpu_unit_code ("2b_0mmi.");
9705 _0mfi_ = get_cpu_unit_code ("2b_0mfi.");
9706 _0mmf_ = get_cpu_unit_code ("2b_0mmf.");
9707 _0bbb_ = get_cpu_unit_code ("2b_0bbb.");
9708 _0mbb_ = get_cpu_unit_code ("2b_0mbb.");
9709 _0mib_ = get_cpu_unit_code ("2b_0mib.");
9710 _0mmb_ = get_cpu_unit_code ("2b_0mmb.");
9711 _0mfb_ = get_cpu_unit_code ("2b_0mfb.");
9712 _0mlx_ = get_cpu_unit_code ("2b_0mlx.");
9713 _1mii_ = get_cpu_unit_code ("2b_1mii.");
9714 _1mmi_ = get_cpu_unit_code ("2b_1mmi.");
9715 _1mfi_ = get_cpu_unit_code ("2b_1mfi.");
9716 _1mmf_ = get_cpu_unit_code ("2b_1mmf.");
9717 _1bbb_ = get_cpu_unit_code ("2b_1bbb.");
9718 _1mbb_ = get_cpu_unit_code ("2b_1mbb.");
9719 _1mib_ = get_cpu_unit_code ("2b_1mib.");
9720 _1mmb_ = get_cpu_unit_code ("2b_1mmb.");
9721 _1mfb_ = get_cpu_unit_code ("2b_1mfb.");
9722 _1mlx_ = get_cpu_unit_code ("2b_1mlx.");
9724 else
9726 pos_1 = get_cpu_unit_code ("1_1");
9727 pos_2 = get_cpu_unit_code ("1_2");
9728 pos_3 = get_cpu_unit_code ("1_3");
9729 pos_4 = get_cpu_unit_code ("1_4");
9730 pos_5 = get_cpu_unit_code ("1_5");
9731 pos_6 = get_cpu_unit_code ("1_6");
9732 _0mii_ = get_cpu_unit_code ("1b_0mii.");
9733 _0mmi_ = get_cpu_unit_code ("1b_0mmi.");
9734 _0mfi_ = get_cpu_unit_code ("1b_0mfi.");
9735 _0mmf_ = get_cpu_unit_code ("1b_0mmf.");
9736 _0bbb_ = get_cpu_unit_code ("1b_0bbb.");
9737 _0mbb_ = get_cpu_unit_code ("1b_0mbb.");
9738 _0mib_ = get_cpu_unit_code ("1b_0mib.");
9739 _0mmb_ = get_cpu_unit_code ("1b_0mmb.");
9740 _0mfb_ = get_cpu_unit_code ("1b_0mfb.");
9741 _0mlx_ = get_cpu_unit_code ("1b_0mlx.");
9742 _1mii_ = get_cpu_unit_code ("1b_1mii.");
9743 _1mmi_ = get_cpu_unit_code ("1b_1mmi.");
9744 _1mfi_ = get_cpu_unit_code ("1b_1mfi.");
9745 _1mmf_ = get_cpu_unit_code ("1b_1mmf.");
9746 _1bbb_ = get_cpu_unit_code ("1b_1bbb.");
9747 _1mbb_ = get_cpu_unit_code ("1b_1mbb.");
9748 _1mib_ = get_cpu_unit_code ("1b_1mib.");
9749 _1mmb_ = get_cpu_unit_code ("1b_1mmb.");
9750 _1mfb_ = get_cpu_unit_code ("1b_1mfb.");
9751 _1mlx_ = get_cpu_unit_code ("1b_1mlx.");
9754 if (flag_selective_scheduling2
9755 && !maybe_skip_selective_scheduling ())
9756 run_selective_scheduling ();
9757 else
9758 schedule_ebbs ();
9760 /* Redo alignment computation, as it might gone wrong. */
9761 compute_alignments ();
9763 /* We cannot reuse this one because it has been corrupted by the
9764 evil glat. */
9765 finish_bundle_states ();
9766 free (stops_p);
9767 stops_p = NULL;
9768 emit_insn_group_barriers (dump_file);
9770 ia64_final_schedule = 0;
9771 timevar_pop (TV_SCHED2);
9773 else
9774 emit_all_insn_group_barriers (dump_file);
9776 df_analyze ();
9778 /* A call must not be the last instruction in a function, so that the
9779 return address is still within the function, so that unwinding works
9780 properly. Note that IA-64 differs from dwarf2 on this point. */
9781 if (ia64_except_unwind_info (&global_options) == UI_TARGET)
9783 rtx_insn *insn;
9784 int saw_stop = 0;
9786 insn = get_last_insn ();
9787 if (! INSN_P (insn))
9788 insn = prev_active_insn (insn);
9789 if (insn)
9791 /* Skip over insns that expand to nothing. */
9792 while (NONJUMP_INSN_P (insn)
9793 && get_attr_empty (insn) == EMPTY_YES)
9795 if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
9796 && XINT (PATTERN (insn), 1) == UNSPECV_INSN_GROUP_BARRIER)
9797 saw_stop = 1;
9798 insn = prev_active_insn (insn);
9800 if (CALL_P (insn))
9802 if (! saw_stop)
9803 emit_insn (gen_insn_group_barrier (GEN_INT (3)));
9804 emit_insn (gen_break_f ());
9805 emit_insn (gen_insn_group_barrier (GEN_INT (3)));
9810 emit_predicate_relation_info ();
9812 if (flag_var_tracking)
9814 timevar_push (TV_VAR_TRACKING);
9815 variable_tracking_main ();
9816 timevar_pop (TV_VAR_TRACKING);
9818 df_finish_pass (false);
9821 /* Return true if REGNO is used by the epilogue. */
9824 ia64_epilogue_uses (int regno)
9826 switch (regno)
9828 case R_GR (1):
9829 /* With a call to a function in another module, we will write a new
9830 value to "gp". After returning from such a call, we need to make
9831 sure the function restores the original gp-value, even if the
9832 function itself does not use the gp anymore. */
9833 return !(TARGET_AUTO_PIC || TARGET_NO_PIC);
9835 case IN_REG (0): case IN_REG (1): case IN_REG (2): case IN_REG (3):
9836 case IN_REG (4): case IN_REG (5): case IN_REG (6): case IN_REG (7):
9837 /* For functions defined with the syscall_linkage attribute, all
9838 input registers are marked as live at all function exits. This
9839 prevents the register allocator from using the input registers,
9840 which in turn makes it possible to restart a system call after
9841 an interrupt without having to save/restore the input registers.
9842 This also prevents kernel data from leaking to application code. */
9843 return lookup_attribute ("syscall_linkage",
9844 TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))) != NULL;
9846 case R_BR (0):
9847 /* Conditional return patterns can't represent the use of `b0' as
9848 the return address, so we force the value live this way. */
9849 return 1;
9851 case AR_PFS_REGNUM:
9852 /* Likewise for ar.pfs, which is used by br.ret. */
9853 return 1;
9855 default:
9856 return 0;
9860 /* Return true if REGNO is used by the frame unwinder. */
9863 ia64_eh_uses (int regno)
9865 unsigned int r;
9867 if (! reload_completed)
9868 return 0;
9870 if (regno == 0)
9871 return 0;
9873 for (r = reg_save_b0; r <= reg_save_ar_lc; r++)
9874 if (regno == current_frame_info.r[r]
9875 || regno == emitted_frame_related_regs[r])
9876 return 1;
9878 return 0;
9881 /* Return true if this goes in small data/bss. */
9883 /* ??? We could also support own long data here. Generating movl/add/ld8
9884 instead of addl,ld8/ld8. This makes the code bigger, but should make the
9885 code faster because there is one less load. This also includes incomplete
9886 types which can't go in sdata/sbss. */
9888 static bool
9889 ia64_in_small_data_p (const_tree exp)
9891 if (TARGET_NO_SDATA)
9892 return false;
9894 /* We want to merge strings, so we never consider them small data. */
9895 if (TREE_CODE (exp) == STRING_CST)
9896 return false;
9898 /* Functions are never small data. */
9899 if (TREE_CODE (exp) == FUNCTION_DECL)
9900 return false;
9902 if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
9904 const char *section = DECL_SECTION_NAME (exp);
9906 if (strcmp (section, ".sdata") == 0
9907 || strncmp (section, ".sdata.", 7) == 0
9908 || strncmp (section, ".gnu.linkonce.s.", 16) == 0
9909 || strcmp (section, ".sbss") == 0
9910 || strncmp (section, ".sbss.", 6) == 0
9911 || strncmp (section, ".gnu.linkonce.sb.", 17) == 0)
9912 return true;
9914 else
9916 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
9918 /* If this is an incomplete type with size 0, then we can't put it
9919 in sdata because it might be too big when completed. */
9920 if (size > 0 && size <= ia64_section_threshold)
9921 return true;
9924 return false;
9927 /* Output assembly directives for prologue regions. */
9929 /* The current basic block number. */
9931 static bool last_block;
9933 /* True if we need a copy_state command at the start of the next block. */
9935 static bool need_copy_state;
9937 #ifndef MAX_ARTIFICIAL_LABEL_BYTES
9938 # define MAX_ARTIFICIAL_LABEL_BYTES 30
9939 #endif
9941 /* The function emits unwind directives for the start of an epilogue. */
9943 static void
9944 process_epilogue (FILE *asm_out_file, rtx insn ATTRIBUTE_UNUSED,
9945 bool unwind, bool frame ATTRIBUTE_UNUSED)
9947 /* If this isn't the last block of the function, then we need to label the
9948 current state, and copy it back in at the start of the next block. */
9950 if (!last_block)
9952 if (unwind)
9953 fprintf (asm_out_file, "\t.label_state %d\n",
9954 ++cfun->machine->state_num);
9955 need_copy_state = true;
9958 if (unwind)
9959 fprintf (asm_out_file, "\t.restore sp\n");
9962 /* This function processes a SET pattern for REG_CFA_ADJUST_CFA. */
9964 static void
9965 process_cfa_adjust_cfa (FILE *asm_out_file, rtx pat, rtx insn,
9966 bool unwind, bool frame)
9968 rtx dest = SET_DEST (pat);
9969 rtx src = SET_SRC (pat);
9971 if (dest == stack_pointer_rtx)
9973 if (GET_CODE (src) == PLUS)
9975 rtx op0 = XEXP (src, 0);
9976 rtx op1 = XEXP (src, 1);
9978 gcc_assert (op0 == dest && GET_CODE (op1) == CONST_INT);
9980 if (INTVAL (op1) < 0)
9982 gcc_assert (!frame_pointer_needed);
9983 if (unwind)
9984 fprintf (asm_out_file,
9985 "\t.fframe "HOST_WIDE_INT_PRINT_DEC"\n",
9986 -INTVAL (op1));
9988 else
9989 process_epilogue (asm_out_file, insn, unwind, frame);
9991 else
9993 gcc_assert (src == hard_frame_pointer_rtx);
9994 process_epilogue (asm_out_file, insn, unwind, frame);
9997 else if (dest == hard_frame_pointer_rtx)
9999 gcc_assert (src == stack_pointer_rtx);
10000 gcc_assert (frame_pointer_needed);
10002 if (unwind)
10003 fprintf (asm_out_file, "\t.vframe r%d\n",
10004 ia64_dbx_register_number (REGNO (dest)));
10006 else
10007 gcc_unreachable ();
10010 /* This function processes a SET pattern for REG_CFA_REGISTER. */
10012 static void
10013 process_cfa_register (FILE *asm_out_file, rtx pat, bool unwind)
10015 rtx dest = SET_DEST (pat);
10016 rtx src = SET_SRC (pat);
10017 int dest_regno = REGNO (dest);
10018 int src_regno;
10020 if (src == pc_rtx)
10022 /* Saving return address pointer. */
10023 if (unwind)
10024 fprintf (asm_out_file, "\t.save rp, r%d\n",
10025 ia64_dbx_register_number (dest_regno));
10026 return;
10029 src_regno = REGNO (src);
10031 switch (src_regno)
10033 case PR_REG (0):
10034 gcc_assert (dest_regno == current_frame_info.r[reg_save_pr]);
10035 if (unwind)
10036 fprintf (asm_out_file, "\t.save pr, r%d\n",
10037 ia64_dbx_register_number (dest_regno));
10038 break;
10040 case AR_UNAT_REGNUM:
10041 gcc_assert (dest_regno == current_frame_info.r[reg_save_ar_unat]);
10042 if (unwind)
10043 fprintf (asm_out_file, "\t.save ar.unat, r%d\n",
10044 ia64_dbx_register_number (dest_regno));
10045 break;
10047 case AR_LC_REGNUM:
10048 gcc_assert (dest_regno == current_frame_info.r[reg_save_ar_lc]);
10049 if (unwind)
10050 fprintf (asm_out_file, "\t.save ar.lc, r%d\n",
10051 ia64_dbx_register_number (dest_regno));
10052 break;
10054 default:
10055 /* Everything else should indicate being stored to memory. */
10056 gcc_unreachable ();
10060 /* This function processes a SET pattern for REG_CFA_OFFSET. */
10062 static void
10063 process_cfa_offset (FILE *asm_out_file, rtx pat, bool unwind)
10065 rtx dest = SET_DEST (pat);
10066 rtx src = SET_SRC (pat);
10067 int src_regno = REGNO (src);
10068 const char *saveop;
10069 HOST_WIDE_INT off;
10070 rtx base;
10072 gcc_assert (MEM_P (dest));
10073 if (GET_CODE (XEXP (dest, 0)) == REG)
10075 base = XEXP (dest, 0);
10076 off = 0;
10078 else
10080 gcc_assert (GET_CODE (XEXP (dest, 0)) == PLUS
10081 && GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT);
10082 base = XEXP (XEXP (dest, 0), 0);
10083 off = INTVAL (XEXP (XEXP (dest, 0), 1));
10086 if (base == hard_frame_pointer_rtx)
10088 saveop = ".savepsp";
10089 off = - off;
10091 else
10093 gcc_assert (base == stack_pointer_rtx);
10094 saveop = ".savesp";
10097 src_regno = REGNO (src);
10098 switch (src_regno)
10100 case BR_REG (0):
10101 gcc_assert (!current_frame_info.r[reg_save_b0]);
10102 if (unwind)
10103 fprintf (asm_out_file, "\t%s rp, " HOST_WIDE_INT_PRINT_DEC "\n",
10104 saveop, off);
10105 break;
10107 case PR_REG (0):
10108 gcc_assert (!current_frame_info.r[reg_save_pr]);
10109 if (unwind)
10110 fprintf (asm_out_file, "\t%s pr, " HOST_WIDE_INT_PRINT_DEC "\n",
10111 saveop, off);
10112 break;
10114 case AR_LC_REGNUM:
10115 gcc_assert (!current_frame_info.r[reg_save_ar_lc]);
10116 if (unwind)
10117 fprintf (asm_out_file, "\t%s ar.lc, " HOST_WIDE_INT_PRINT_DEC "\n",
10118 saveop, off);
10119 break;
10121 case AR_PFS_REGNUM:
10122 gcc_assert (!current_frame_info.r[reg_save_ar_pfs]);
10123 if (unwind)
10124 fprintf (asm_out_file, "\t%s ar.pfs, " HOST_WIDE_INT_PRINT_DEC "\n",
10125 saveop, off);
10126 break;
10128 case AR_UNAT_REGNUM:
10129 gcc_assert (!current_frame_info.r[reg_save_ar_unat]);
10130 if (unwind)
10131 fprintf (asm_out_file, "\t%s ar.unat, " HOST_WIDE_INT_PRINT_DEC "\n",
10132 saveop, off);
10133 break;
10135 case GR_REG (4):
10136 case GR_REG (5):
10137 case GR_REG (6):
10138 case GR_REG (7):
10139 if (unwind)
10140 fprintf (asm_out_file, "\t.save.g 0x%x\n",
10141 1 << (src_regno - GR_REG (4)));
10142 break;
10144 case BR_REG (1):
10145 case BR_REG (2):
10146 case BR_REG (3):
10147 case BR_REG (4):
10148 case BR_REG (5):
10149 if (unwind)
10150 fprintf (asm_out_file, "\t.save.b 0x%x\n",
10151 1 << (src_regno - BR_REG (1)));
10152 break;
10154 case FR_REG (2):
10155 case FR_REG (3):
10156 case FR_REG (4):
10157 case FR_REG (5):
10158 if (unwind)
10159 fprintf (asm_out_file, "\t.save.f 0x%x\n",
10160 1 << (src_regno - FR_REG (2)));
10161 break;
10163 case FR_REG (16): case FR_REG (17): case FR_REG (18): case FR_REG (19):
10164 case FR_REG (20): case FR_REG (21): case FR_REG (22): case FR_REG (23):
10165 case FR_REG (24): case FR_REG (25): case FR_REG (26): case FR_REG (27):
10166 case FR_REG (28): case FR_REG (29): case FR_REG (30): case FR_REG (31):
10167 if (unwind)
10168 fprintf (asm_out_file, "\t.save.gf 0x0, 0x%x\n",
10169 1 << (src_regno - FR_REG (12)));
10170 break;
10172 default:
10173 /* ??? For some reason we mark other general registers, even those
10174 we can't represent in the unwind info. Ignore them. */
10175 break;
10179 /* This function looks at a single insn and emits any directives
10180 required to unwind this insn. */
10182 static void
10183 ia64_asm_unwind_emit (FILE *asm_out_file, rtx_insn *insn)
10185 bool unwind = ia64_except_unwind_info (&global_options) == UI_TARGET;
10186 bool frame = dwarf2out_do_frame ();
10187 rtx note, pat;
10188 bool handled_one;
10190 if (!unwind && !frame)
10191 return;
10193 if (NOTE_INSN_BASIC_BLOCK_P (insn))
10195 last_block = NOTE_BASIC_BLOCK (insn)->next_bb
10196 == EXIT_BLOCK_PTR_FOR_FN (cfun);
10198 /* Restore unwind state from immediately before the epilogue. */
10199 if (need_copy_state)
10201 if (unwind)
10203 fprintf (asm_out_file, "\t.body\n");
10204 fprintf (asm_out_file, "\t.copy_state %d\n",
10205 cfun->machine->state_num);
10207 need_copy_state = false;
10211 if (NOTE_P (insn) || ! RTX_FRAME_RELATED_P (insn))
10212 return;
10214 /* Look for the ALLOC insn. */
10215 if (INSN_CODE (insn) == CODE_FOR_alloc)
10217 rtx dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
10218 int dest_regno = REGNO (dest);
10220 /* If this is the final destination for ar.pfs, then this must
10221 be the alloc in the prologue. */
10222 if (dest_regno == current_frame_info.r[reg_save_ar_pfs])
10224 if (unwind)
10225 fprintf (asm_out_file, "\t.save ar.pfs, r%d\n",
10226 ia64_dbx_register_number (dest_regno));
10228 else
10230 /* This must be an alloc before a sibcall. We must drop the
10231 old frame info. The easiest way to drop the old frame
10232 info is to ensure we had a ".restore sp" directive
10233 followed by a new prologue. If the procedure doesn't
10234 have a memory-stack frame, we'll issue a dummy ".restore
10235 sp" now. */
10236 if (current_frame_info.total_size == 0 && !frame_pointer_needed)
10237 /* if haven't done process_epilogue() yet, do it now */
10238 process_epilogue (asm_out_file, insn, unwind, frame);
10239 if (unwind)
10240 fprintf (asm_out_file, "\t.prologue\n");
10242 return;
10245 handled_one = false;
10246 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
10247 switch (REG_NOTE_KIND (note))
10249 case REG_CFA_ADJUST_CFA:
10250 pat = XEXP (note, 0);
10251 if (pat == NULL)
10252 pat = PATTERN (insn);
10253 process_cfa_adjust_cfa (asm_out_file, pat, insn, unwind, frame);
10254 handled_one = true;
10255 break;
10257 case REG_CFA_OFFSET:
10258 pat = XEXP (note, 0);
10259 if (pat == NULL)
10260 pat = PATTERN (insn);
10261 process_cfa_offset (asm_out_file, pat, unwind);
10262 handled_one = true;
10263 break;
10265 case REG_CFA_REGISTER:
10266 pat = XEXP (note, 0);
10267 if (pat == NULL)
10268 pat = PATTERN (insn);
10269 process_cfa_register (asm_out_file, pat, unwind);
10270 handled_one = true;
10271 break;
10273 case REG_FRAME_RELATED_EXPR:
10274 case REG_CFA_DEF_CFA:
10275 case REG_CFA_EXPRESSION:
10276 case REG_CFA_RESTORE:
10277 case REG_CFA_SET_VDRAP:
10278 /* Not used in the ia64 port. */
10279 gcc_unreachable ();
10281 default:
10282 /* Not a frame-related note. */
10283 break;
10286 /* All REG_FRAME_RELATED_P insns, besides ALLOC, are marked with the
10287 explicit action to take. No guessing required. */
10288 gcc_assert (handled_one);
10291 /* Implement TARGET_ASM_EMIT_EXCEPT_PERSONALITY. */
10293 static void
10294 ia64_asm_emit_except_personality (rtx personality)
10296 fputs ("\t.personality\t", asm_out_file);
10297 output_addr_const (asm_out_file, personality);
10298 fputc ('\n', asm_out_file);
10301 /* Implement TARGET_ASM_INITIALIZE_SECTIONS. */
10303 static void
10304 ia64_asm_init_sections (void)
10306 exception_section = get_unnamed_section (0, output_section_asm_op,
10307 "\t.handlerdata");
10310 /* Implement TARGET_DEBUG_UNWIND_INFO. */
10312 static enum unwind_info_type
10313 ia64_debug_unwind_info (void)
10315 return UI_TARGET;
10318 enum ia64_builtins
10320 IA64_BUILTIN_BSP,
10321 IA64_BUILTIN_COPYSIGNQ,
10322 IA64_BUILTIN_FABSQ,
10323 IA64_BUILTIN_FLUSHRS,
10324 IA64_BUILTIN_INFQ,
10325 IA64_BUILTIN_HUGE_VALQ,
10326 IA64_BUILTIN_max
10329 static GTY(()) tree ia64_builtins[(int) IA64_BUILTIN_max];
10331 void
10332 ia64_init_builtins (void)
10334 tree fpreg_type;
10335 tree float80_type;
10336 tree decl;
10338 /* The __fpreg type. */
10339 fpreg_type = make_node (REAL_TYPE);
10340 TYPE_PRECISION (fpreg_type) = 82;
10341 layout_type (fpreg_type);
10342 (*lang_hooks.types.register_builtin_type) (fpreg_type, "__fpreg");
10344 /* The __float80 type. */
10345 float80_type = make_node (REAL_TYPE);
10346 TYPE_PRECISION (float80_type) = 80;
10347 layout_type (float80_type);
10348 (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
10350 /* The __float128 type. */
10351 if (!TARGET_HPUX)
10353 tree ftype;
10354 tree float128_type = make_node (REAL_TYPE);
10356 TYPE_PRECISION (float128_type) = 128;
10357 layout_type (float128_type);
10358 (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
10360 /* TFmode support builtins. */
10361 ftype = build_function_type_list (float128_type, NULL_TREE);
10362 decl = add_builtin_function ("__builtin_infq", ftype,
10363 IA64_BUILTIN_INFQ, BUILT_IN_MD,
10364 NULL, NULL_TREE);
10365 ia64_builtins[IA64_BUILTIN_INFQ] = decl;
10367 decl = add_builtin_function ("__builtin_huge_valq", ftype,
10368 IA64_BUILTIN_HUGE_VALQ, BUILT_IN_MD,
10369 NULL, NULL_TREE);
10370 ia64_builtins[IA64_BUILTIN_HUGE_VALQ] = decl;
10372 ftype = build_function_type_list (float128_type,
10373 float128_type,
10374 NULL_TREE);
10375 decl = add_builtin_function ("__builtin_fabsq", ftype,
10376 IA64_BUILTIN_FABSQ, BUILT_IN_MD,
10377 "__fabstf2", NULL_TREE);
10378 TREE_READONLY (decl) = 1;
10379 ia64_builtins[IA64_BUILTIN_FABSQ] = decl;
10381 ftype = build_function_type_list (float128_type,
10382 float128_type,
10383 float128_type,
10384 NULL_TREE);
10385 decl = add_builtin_function ("__builtin_copysignq", ftype,
10386 IA64_BUILTIN_COPYSIGNQ, BUILT_IN_MD,
10387 "__copysigntf3", NULL_TREE);
10388 TREE_READONLY (decl) = 1;
10389 ia64_builtins[IA64_BUILTIN_COPYSIGNQ] = decl;
10391 else
10392 /* Under HPUX, this is a synonym for "long double". */
10393 (*lang_hooks.types.register_builtin_type) (long_double_type_node,
10394 "__float128");
10396 /* Fwrite on VMS is non-standard. */
10397 #if TARGET_ABI_OPEN_VMS
10398 vms_patch_builtins ();
10399 #endif
10401 #define def_builtin(name, type, code) \
10402 add_builtin_function ((name), (type), (code), BUILT_IN_MD, \
10403 NULL, NULL_TREE)
10405 decl = def_builtin ("__builtin_ia64_bsp",
10406 build_function_type_list (ptr_type_node, NULL_TREE),
10407 IA64_BUILTIN_BSP);
10408 ia64_builtins[IA64_BUILTIN_BSP] = decl;
10410 decl = def_builtin ("__builtin_ia64_flushrs",
10411 build_function_type_list (void_type_node, NULL_TREE),
10412 IA64_BUILTIN_FLUSHRS);
10413 ia64_builtins[IA64_BUILTIN_FLUSHRS] = decl;
10415 #undef def_builtin
10417 if (TARGET_HPUX)
10419 if ((decl = builtin_decl_explicit (BUILT_IN_FINITE)) != NULL_TREE)
10420 set_user_assembler_name (decl, "_Isfinite");
10421 if ((decl = builtin_decl_explicit (BUILT_IN_FINITEF)) != NULL_TREE)
10422 set_user_assembler_name (decl, "_Isfinitef");
10423 if ((decl = builtin_decl_explicit (BUILT_IN_FINITEL)) != NULL_TREE)
10424 set_user_assembler_name (decl, "_Isfinitef128");
10429 ia64_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
10430 enum machine_mode mode ATTRIBUTE_UNUSED,
10431 int ignore ATTRIBUTE_UNUSED)
10433 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10434 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10436 switch (fcode)
10438 case IA64_BUILTIN_BSP:
10439 if (! target || ! register_operand (target, DImode))
10440 target = gen_reg_rtx (DImode);
10441 emit_insn (gen_bsp_value (target));
10442 #ifdef POINTERS_EXTEND_UNSIGNED
10443 target = convert_memory_address (ptr_mode, target);
10444 #endif
10445 return target;
10447 case IA64_BUILTIN_FLUSHRS:
10448 emit_insn (gen_flushrs ());
10449 return const0_rtx;
10451 case IA64_BUILTIN_INFQ:
10452 case IA64_BUILTIN_HUGE_VALQ:
10454 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
10455 REAL_VALUE_TYPE inf;
10456 rtx tmp;
10458 real_inf (&inf);
10459 tmp = CONST_DOUBLE_FROM_REAL_VALUE (inf, target_mode);
10461 tmp = validize_mem (force_const_mem (target_mode, tmp));
10463 if (target == 0)
10464 target = gen_reg_rtx (target_mode);
10466 emit_move_insn (target, tmp);
10467 return target;
10470 case IA64_BUILTIN_FABSQ:
10471 case IA64_BUILTIN_COPYSIGNQ:
10472 return expand_call (exp, target, ignore);
10474 default:
10475 gcc_unreachable ();
10478 return NULL_RTX;
10481 /* Return the ia64 builtin for CODE. */
10483 static tree
10484 ia64_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
10486 if (code >= IA64_BUILTIN_max)
10487 return error_mark_node;
10489 return ia64_builtins[code];
10492 /* For the HP-UX IA64 aggregate parameters are passed stored in the
10493 most significant bits of the stack slot. */
10495 enum direction
10496 ia64_hpux_function_arg_padding (enum machine_mode mode, const_tree type)
10498 /* Exception to normal case for structures/unions/etc. */
10500 if (type && AGGREGATE_TYPE_P (type)
10501 && int_size_in_bytes (type) < UNITS_PER_WORD)
10502 return upward;
10504 /* Fall back to the default. */
10505 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
10508 /* Emit text to declare externally defined variables and functions, because
10509 the Intel assembler does not support undefined externals. */
10511 void
10512 ia64_asm_output_external (FILE *file, tree decl, const char *name)
10514 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
10515 set in order to avoid putting out names that are never really
10516 used. */
10517 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
10519 /* maybe_assemble_visibility will return 1 if the assembler
10520 visibility directive is output. */
10521 int need_visibility = ((*targetm.binds_local_p) (decl)
10522 && maybe_assemble_visibility (decl));
10524 /* GNU as does not need anything here, but the HP linker does
10525 need something for external functions. */
10526 if ((TARGET_HPUX_LD || !TARGET_GNU_AS)
10527 && TREE_CODE (decl) == FUNCTION_DECL)
10528 (*targetm.asm_out.globalize_decl_name) (file, decl);
10529 else if (need_visibility && !TARGET_GNU_AS)
10530 (*targetm.asm_out.globalize_label) (file, name);
10534 /* Set SImode div/mod functions, init_integral_libfuncs only initializes
10535 modes of word_mode and larger. Rename the TFmode libfuncs using the
10536 HPUX conventions. __divtf3 is used for XFmode. We need to keep it for
10537 backward compatibility. */
10539 static void
10540 ia64_init_libfuncs (void)
10542 set_optab_libfunc (sdiv_optab, SImode, "__divsi3");
10543 set_optab_libfunc (udiv_optab, SImode, "__udivsi3");
10544 set_optab_libfunc (smod_optab, SImode, "__modsi3");
10545 set_optab_libfunc (umod_optab, SImode, "__umodsi3");
10547 set_optab_libfunc (add_optab, TFmode, "_U_Qfadd");
10548 set_optab_libfunc (sub_optab, TFmode, "_U_Qfsub");
10549 set_optab_libfunc (smul_optab, TFmode, "_U_Qfmpy");
10550 set_optab_libfunc (sdiv_optab, TFmode, "_U_Qfdiv");
10551 set_optab_libfunc (neg_optab, TFmode, "_U_Qfneg");
10553 set_conv_libfunc (sext_optab, TFmode, SFmode, "_U_Qfcnvff_sgl_to_quad");
10554 set_conv_libfunc (sext_optab, TFmode, DFmode, "_U_Qfcnvff_dbl_to_quad");
10555 set_conv_libfunc (sext_optab, TFmode, XFmode, "_U_Qfcnvff_f80_to_quad");
10556 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_U_Qfcnvff_quad_to_sgl");
10557 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_U_Qfcnvff_quad_to_dbl");
10558 set_conv_libfunc (trunc_optab, XFmode, TFmode, "_U_Qfcnvff_quad_to_f80");
10560 set_conv_libfunc (sfix_optab, SImode, TFmode, "_U_Qfcnvfxt_quad_to_sgl");
10561 set_conv_libfunc (sfix_optab, DImode, TFmode, "_U_Qfcnvfxt_quad_to_dbl");
10562 set_conv_libfunc (sfix_optab, TImode, TFmode, "_U_Qfcnvfxt_quad_to_quad");
10563 set_conv_libfunc (ufix_optab, SImode, TFmode, "_U_Qfcnvfxut_quad_to_sgl");
10564 set_conv_libfunc (ufix_optab, DImode, TFmode, "_U_Qfcnvfxut_quad_to_dbl");
10566 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_U_Qfcnvxf_sgl_to_quad");
10567 set_conv_libfunc (sfloat_optab, TFmode, DImode, "_U_Qfcnvxf_dbl_to_quad");
10568 set_conv_libfunc (sfloat_optab, TFmode, TImode, "_U_Qfcnvxf_quad_to_quad");
10569 /* HP-UX 11.23 libc does not have a function for unsigned
10570 SImode-to-TFmode conversion. */
10571 set_conv_libfunc (ufloat_optab, TFmode, DImode, "_U_Qfcnvxuf_dbl_to_quad");
10574 /* Rename all the TFmode libfuncs using the HPUX conventions. */
10576 static void
10577 ia64_hpux_init_libfuncs (void)
10579 ia64_init_libfuncs ();
10581 /* The HP SI millicode division and mod functions expect DI arguments.
10582 By turning them off completely we avoid using both libgcc and the
10583 non-standard millicode routines and use the HP DI millicode routines
10584 instead. */
10586 set_optab_libfunc (sdiv_optab, SImode, 0);
10587 set_optab_libfunc (udiv_optab, SImode, 0);
10588 set_optab_libfunc (smod_optab, SImode, 0);
10589 set_optab_libfunc (umod_optab, SImode, 0);
10591 set_optab_libfunc (sdiv_optab, DImode, "__milli_divI");
10592 set_optab_libfunc (udiv_optab, DImode, "__milli_divU");
10593 set_optab_libfunc (smod_optab, DImode, "__milli_remI");
10594 set_optab_libfunc (umod_optab, DImode, "__milli_remU");
10596 /* HP-UX libc has TF min/max/abs routines in it. */
10597 set_optab_libfunc (smin_optab, TFmode, "_U_Qfmin");
10598 set_optab_libfunc (smax_optab, TFmode, "_U_Qfmax");
10599 set_optab_libfunc (abs_optab, TFmode, "_U_Qfabs");
10601 /* ia64_expand_compare uses this. */
10602 cmptf_libfunc = init_one_libfunc ("_U_Qfcmp");
10604 /* These should never be used. */
10605 set_optab_libfunc (eq_optab, TFmode, 0);
10606 set_optab_libfunc (ne_optab, TFmode, 0);
10607 set_optab_libfunc (gt_optab, TFmode, 0);
10608 set_optab_libfunc (ge_optab, TFmode, 0);
10609 set_optab_libfunc (lt_optab, TFmode, 0);
10610 set_optab_libfunc (le_optab, TFmode, 0);
10613 /* Rename the division and modulus functions in VMS. */
10615 static void
10616 ia64_vms_init_libfuncs (void)
10618 set_optab_libfunc (sdiv_optab, SImode, "OTS$DIV_I");
10619 set_optab_libfunc (sdiv_optab, DImode, "OTS$DIV_L");
10620 set_optab_libfunc (udiv_optab, SImode, "OTS$DIV_UI");
10621 set_optab_libfunc (udiv_optab, DImode, "OTS$DIV_UL");
10622 set_optab_libfunc (smod_optab, SImode, "OTS$REM_I");
10623 set_optab_libfunc (smod_optab, DImode, "OTS$REM_L");
10624 set_optab_libfunc (umod_optab, SImode, "OTS$REM_UI");
10625 set_optab_libfunc (umod_optab, DImode, "OTS$REM_UL");
10626 abort_libfunc = init_one_libfunc ("decc$abort");
10627 memcmp_libfunc = init_one_libfunc ("decc$memcmp");
10628 #ifdef MEM_LIBFUNCS_INIT
10629 MEM_LIBFUNCS_INIT;
10630 #endif
10633 /* Rename the TFmode libfuncs available from soft-fp in glibc using
10634 the HPUX conventions. */
10636 static void
10637 ia64_sysv4_init_libfuncs (void)
10639 ia64_init_libfuncs ();
10641 /* These functions are not part of the HPUX TFmode interface. We
10642 use them instead of _U_Qfcmp, which doesn't work the way we
10643 expect. */
10644 set_optab_libfunc (eq_optab, TFmode, "_U_Qfeq");
10645 set_optab_libfunc (ne_optab, TFmode, "_U_Qfne");
10646 set_optab_libfunc (gt_optab, TFmode, "_U_Qfgt");
10647 set_optab_libfunc (ge_optab, TFmode, "_U_Qfge");
10648 set_optab_libfunc (lt_optab, TFmode, "_U_Qflt");
10649 set_optab_libfunc (le_optab, TFmode, "_U_Qfle");
10651 /* We leave out _U_Qfmin, _U_Qfmax and _U_Qfabs since soft-fp in
10652 glibc doesn't have them. */
10655 /* Use soft-fp. */
10657 static void
10658 ia64_soft_fp_init_libfuncs (void)
10662 static bool
10663 ia64_vms_valid_pointer_mode (enum machine_mode mode)
10665 return (mode == SImode || mode == DImode);
10668 /* For HPUX, it is illegal to have relocations in shared segments. */
10670 static int
10671 ia64_hpux_reloc_rw_mask (void)
10673 return 3;
10676 /* For others, relax this so that relocations to local data goes in
10677 read-only segments, but we still cannot allow global relocations
10678 in read-only segments. */
10680 static int
10681 ia64_reloc_rw_mask (void)
10683 return flag_pic ? 3 : 2;
10686 /* Return the section to use for X. The only special thing we do here
10687 is to honor small data. */
10689 static section *
10690 ia64_select_rtx_section (enum machine_mode mode, rtx x,
10691 unsigned HOST_WIDE_INT align)
10693 if (GET_MODE_SIZE (mode) > 0
10694 && GET_MODE_SIZE (mode) <= ia64_section_threshold
10695 && !TARGET_NO_SDATA)
10696 return sdata_section;
10697 else
10698 return default_elf_select_rtx_section (mode, x, align);
10701 static unsigned int
10702 ia64_section_type_flags (tree decl, const char *name, int reloc)
10704 unsigned int flags = 0;
10706 if (strcmp (name, ".sdata") == 0
10707 || strncmp (name, ".sdata.", 7) == 0
10708 || strncmp (name, ".gnu.linkonce.s.", 16) == 0
10709 || strncmp (name, ".sdata2.", 8) == 0
10710 || strncmp (name, ".gnu.linkonce.s2.", 17) == 0
10711 || strcmp (name, ".sbss") == 0
10712 || strncmp (name, ".sbss.", 6) == 0
10713 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
10714 flags = SECTION_SMALL;
10716 flags |= default_section_type_flags (decl, name, reloc);
10717 return flags;
10720 /* Returns true if FNTYPE (a FUNCTION_TYPE or a METHOD_TYPE) returns a
10721 structure type and that the address of that type should be passed
10722 in out0, rather than in r8. */
10724 static bool
10725 ia64_struct_retval_addr_is_first_parm_p (tree fntype)
10727 tree ret_type = TREE_TYPE (fntype);
10729 /* The Itanium C++ ABI requires that out0, rather than r8, be used
10730 as the structure return address parameter, if the return value
10731 type has a non-trivial copy constructor or destructor. It is not
10732 clear if this same convention should be used for other
10733 programming languages. Until G++ 3.4, we incorrectly used r8 for
10734 these return values. */
10735 return (abi_version_at_least (2)
10736 && ret_type
10737 && TYPE_MODE (ret_type) == BLKmode
10738 && TREE_ADDRESSABLE (ret_type)
10739 && strcmp (lang_hooks.name, "GNU C++") == 0);
10742 /* Output the assembler code for a thunk function. THUNK_DECL is the
10743 declaration for the thunk function itself, FUNCTION is the decl for
10744 the target function. DELTA is an immediate constant offset to be
10745 added to THIS. If VCALL_OFFSET is nonzero, the word at
10746 *(*this + vcall_offset) should be added to THIS. */
10748 static void
10749 ia64_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
10750 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
10751 tree function)
10753 rtx this_rtx, funexp;
10754 rtx_insn *insn;
10755 unsigned int this_parmno;
10756 unsigned int this_regno;
10757 rtx delta_rtx;
10759 reload_completed = 1;
10760 epilogue_completed = 1;
10762 /* Set things up as ia64_expand_prologue might. */
10763 last_scratch_gr_reg = 15;
10765 memset (&current_frame_info, 0, sizeof (current_frame_info));
10766 current_frame_info.spill_cfa_off = -16;
10767 current_frame_info.n_input_regs = 1;
10768 current_frame_info.need_regstk = (TARGET_REG_NAMES != 0);
10770 /* Mark the end of the (empty) prologue. */
10771 emit_note (NOTE_INSN_PROLOGUE_END);
10773 /* Figure out whether "this" will be the first parameter (the
10774 typical case) or the second parameter (as happens when the
10775 virtual function returns certain class objects). */
10776 this_parmno
10777 = (ia64_struct_retval_addr_is_first_parm_p (TREE_TYPE (thunk))
10778 ? 1 : 0);
10779 this_regno = IN_REG (this_parmno);
10780 if (!TARGET_REG_NAMES)
10781 reg_names[this_regno] = ia64_reg_numbers[this_parmno];
10783 this_rtx = gen_rtx_REG (Pmode, this_regno);
10785 /* Apply the constant offset, if required. */
10786 delta_rtx = GEN_INT (delta);
10787 if (TARGET_ILP32)
10789 rtx tmp = gen_rtx_REG (ptr_mode, this_regno);
10790 REG_POINTER (tmp) = 1;
10791 if (delta && satisfies_constraint_I (delta_rtx))
10793 emit_insn (gen_ptr_extend_plus_imm (this_rtx, tmp, delta_rtx));
10794 delta = 0;
10796 else
10797 emit_insn (gen_ptr_extend (this_rtx, tmp));
10799 if (delta)
10801 if (!satisfies_constraint_I (delta_rtx))
10803 rtx tmp = gen_rtx_REG (Pmode, 2);
10804 emit_move_insn (tmp, delta_rtx);
10805 delta_rtx = tmp;
10807 emit_insn (gen_adddi3 (this_rtx, this_rtx, delta_rtx));
10810 /* Apply the offset from the vtable, if required. */
10811 if (vcall_offset)
10813 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
10814 rtx tmp = gen_rtx_REG (Pmode, 2);
10816 if (TARGET_ILP32)
10818 rtx t = gen_rtx_REG (ptr_mode, 2);
10819 REG_POINTER (t) = 1;
10820 emit_move_insn (t, gen_rtx_MEM (ptr_mode, this_rtx));
10821 if (satisfies_constraint_I (vcall_offset_rtx))
10823 emit_insn (gen_ptr_extend_plus_imm (tmp, t, vcall_offset_rtx));
10824 vcall_offset = 0;
10826 else
10827 emit_insn (gen_ptr_extend (tmp, t));
10829 else
10830 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
10832 if (vcall_offset)
10834 if (!satisfies_constraint_J (vcall_offset_rtx))
10836 rtx tmp2 = gen_rtx_REG (Pmode, next_scratch_gr_reg ());
10837 emit_move_insn (tmp2, vcall_offset_rtx);
10838 vcall_offset_rtx = tmp2;
10840 emit_insn (gen_adddi3 (tmp, tmp, vcall_offset_rtx));
10843 if (TARGET_ILP32)
10844 emit_insn (gen_zero_extendsidi2 (tmp, gen_rtx_MEM (ptr_mode, tmp)));
10845 else
10846 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
10848 emit_insn (gen_adddi3 (this_rtx, this_rtx, tmp));
10851 /* Generate a tail call to the target function. */
10852 if (! TREE_USED (function))
10854 assemble_external (function);
10855 TREE_USED (function) = 1;
10857 funexp = XEXP (DECL_RTL (function), 0);
10858 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
10859 ia64_expand_call (NULL_RTX, funexp, NULL_RTX, 1);
10860 insn = get_last_insn ();
10861 SIBLING_CALL_P (insn) = 1;
10863 /* Code generation for calls relies on splitting. */
10864 reload_completed = 1;
10865 epilogue_completed = 1;
10866 try_split (PATTERN (insn), insn, 0);
10868 emit_barrier ();
10870 /* Run just enough of rest_of_compilation to get the insns emitted.
10871 There's not really enough bulk here to make other passes such as
10872 instruction scheduling worth while. Note that use_thunk calls
10873 assemble_start_function and assemble_end_function. */
10875 emit_all_insn_group_barriers (NULL);
10876 insn = get_insns ();
10877 shorten_branches (insn);
10878 final_start_function (insn, file, 1);
10879 final (insn, file, 1);
10880 final_end_function ();
10882 reload_completed = 0;
10883 epilogue_completed = 0;
10886 /* Worker function for TARGET_STRUCT_VALUE_RTX. */
10888 static rtx
10889 ia64_struct_value_rtx (tree fntype,
10890 int incoming ATTRIBUTE_UNUSED)
10892 if (TARGET_ABI_OPEN_VMS ||
10893 (fntype && ia64_struct_retval_addr_is_first_parm_p (fntype)))
10894 return NULL_RTX;
10895 return gen_rtx_REG (Pmode, GR_REG (8));
10898 static bool
10899 ia64_scalar_mode_supported_p (enum machine_mode mode)
10901 switch (mode)
10903 case QImode:
10904 case HImode:
10905 case SImode:
10906 case DImode:
10907 case TImode:
10908 return true;
10910 case SFmode:
10911 case DFmode:
10912 case XFmode:
10913 case RFmode:
10914 return true;
10916 case TFmode:
10917 return true;
10919 default:
10920 return false;
10924 static bool
10925 ia64_vector_mode_supported_p (enum machine_mode mode)
10927 switch (mode)
10929 case V8QImode:
10930 case V4HImode:
10931 case V2SImode:
10932 return true;
10934 case V2SFmode:
10935 return true;
10937 default:
10938 return false;
10942 /* Implement the FUNCTION_PROFILER macro. */
10944 void
10945 ia64_output_function_profiler (FILE *file, int labelno)
10947 bool indirect_call;
10949 /* If the function needs a static chain and the static chain
10950 register is r15, we use an indirect call so as to bypass
10951 the PLT stub in case the executable is dynamically linked,
10952 because the stub clobbers r15 as per 5.3.6 of the psABI.
10953 We don't need to do that in non canonical PIC mode. */
10955 if (cfun->static_chain_decl && !TARGET_NO_PIC && !TARGET_AUTO_PIC)
10957 gcc_assert (STATIC_CHAIN_REGNUM == 15);
10958 indirect_call = true;
10960 else
10961 indirect_call = false;
10963 if (TARGET_GNU_AS)
10964 fputs ("\t.prologue 4, r40\n", file);
10965 else
10966 fputs ("\t.prologue\n\t.save ar.pfs, r40\n", file);
10967 fputs ("\talloc out0 = ar.pfs, 8, 0, 4, 0\n", file);
10969 if (NO_PROFILE_COUNTERS)
10970 fputs ("\tmov out3 = r0\n", file);
10971 else
10973 char buf[20];
10974 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
10976 if (TARGET_AUTO_PIC)
10977 fputs ("\tmovl out3 = @gprel(", file);
10978 else
10979 fputs ("\taddl out3 = @ltoff(", file);
10980 assemble_name (file, buf);
10981 if (TARGET_AUTO_PIC)
10982 fputs (")\n", file);
10983 else
10984 fputs ("), r1\n", file);
10987 if (indirect_call)
10988 fputs ("\taddl r14 = @ltoff(@fptr(_mcount)), r1\n", file);
10989 fputs ("\t;;\n", file);
10991 fputs ("\t.save rp, r42\n", file);
10992 fputs ("\tmov out2 = b0\n", file);
10993 if (indirect_call)
10994 fputs ("\tld8 r14 = [r14]\n\t;;\n", file);
10995 fputs ("\t.body\n", file);
10996 fputs ("\tmov out1 = r1\n", file);
10997 if (indirect_call)
10999 fputs ("\tld8 r16 = [r14], 8\n\t;;\n", file);
11000 fputs ("\tmov b6 = r16\n", file);
11001 fputs ("\tld8 r1 = [r14]\n", file);
11002 fputs ("\tbr.call.sptk.many b0 = b6\n\t;;\n", file);
11004 else
11005 fputs ("\tbr.call.sptk.many b0 = _mcount\n\t;;\n", file);
11008 static GTY(()) rtx mcount_func_rtx;
11009 static rtx
11010 gen_mcount_func_rtx (void)
11012 if (!mcount_func_rtx)
11013 mcount_func_rtx = init_one_libfunc ("_mcount");
11014 return mcount_func_rtx;
11017 void
11018 ia64_profile_hook (int labelno)
11020 rtx label, ip;
11022 if (NO_PROFILE_COUNTERS)
11023 label = const0_rtx;
11024 else
11026 char buf[30];
11027 const char *label_name;
11028 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
11029 label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
11030 label = gen_rtx_SYMBOL_REF (Pmode, label_name);
11031 SYMBOL_REF_FLAGS (label) = SYMBOL_FLAG_LOCAL;
11033 ip = gen_reg_rtx (Pmode);
11034 emit_insn (gen_ip_value (ip));
11035 emit_library_call (gen_mcount_func_rtx (), LCT_NORMAL,
11036 VOIDmode, 3,
11037 gen_rtx_REG (Pmode, BR_REG (0)), Pmode,
11038 ip, Pmode,
11039 label, Pmode);
11042 /* Return the mangling of TYPE if it is an extended fundamental type. */
11044 static const char *
11045 ia64_mangle_type (const_tree type)
11047 type = TYPE_MAIN_VARIANT (type);
11049 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
11050 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
11051 return NULL;
11053 /* On HP-UX, "long double" is mangled as "e" so __float128 is
11054 mangled as "e". */
11055 if (!TARGET_HPUX && TYPE_MODE (type) == TFmode)
11056 return "g";
11057 /* On HP-UX, "e" is not available as a mangling of __float80 so use
11058 an extended mangling. Elsewhere, "e" is available since long
11059 double is 80 bits. */
11060 if (TYPE_MODE (type) == XFmode)
11061 return TARGET_HPUX ? "u9__float80" : "e";
11062 if (TYPE_MODE (type) == RFmode)
11063 return "u7__fpreg";
11064 return NULL;
11067 /* Return the diagnostic message string if conversion from FROMTYPE to
11068 TOTYPE is not allowed, NULL otherwise. */
11069 static const char *
11070 ia64_invalid_conversion (const_tree fromtype, const_tree totype)
11072 /* Reject nontrivial conversion to or from __fpreg. */
11073 if (TYPE_MODE (fromtype) == RFmode
11074 && TYPE_MODE (totype) != RFmode
11075 && TYPE_MODE (totype) != VOIDmode)
11076 return N_("invalid conversion from %<__fpreg%>");
11077 if (TYPE_MODE (totype) == RFmode
11078 && TYPE_MODE (fromtype) != RFmode)
11079 return N_("invalid conversion to %<__fpreg%>");
11080 return NULL;
11083 /* Return the diagnostic message string if the unary operation OP is
11084 not permitted on TYPE, NULL otherwise. */
11085 static const char *
11086 ia64_invalid_unary_op (int op, const_tree type)
11088 /* Reject operations on __fpreg other than unary + or &. */
11089 if (TYPE_MODE (type) == RFmode
11090 && op != CONVERT_EXPR
11091 && op != ADDR_EXPR)
11092 return N_("invalid operation on %<__fpreg%>");
11093 return NULL;
11096 /* Return the diagnostic message string if the binary operation OP is
11097 not permitted on TYPE1 and TYPE2, NULL otherwise. */
11098 static const char *
11099 ia64_invalid_binary_op (int op ATTRIBUTE_UNUSED, const_tree type1, const_tree type2)
11101 /* Reject operations on __fpreg. */
11102 if (TYPE_MODE (type1) == RFmode || TYPE_MODE (type2) == RFmode)
11103 return N_("invalid operation on %<__fpreg%>");
11104 return NULL;
11107 /* HP-UX version_id attribute.
11108 For object foo, if the version_id is set to 1234 put out an alias
11109 of '.alias foo "foo{1234}" We can't use "foo{1234}" in anything
11110 other than an alias statement because it is an illegal symbol name. */
11112 static tree
11113 ia64_handle_version_id_attribute (tree *node ATTRIBUTE_UNUSED,
11114 tree name ATTRIBUTE_UNUSED,
11115 tree args,
11116 int flags ATTRIBUTE_UNUSED,
11117 bool *no_add_attrs)
11119 tree arg = TREE_VALUE (args);
11121 if (TREE_CODE (arg) != STRING_CST)
11123 error("version attribute is not a string");
11124 *no_add_attrs = true;
11125 return NULL_TREE;
11127 return NULL_TREE;
11130 /* Target hook for c_mode_for_suffix. */
11132 static enum machine_mode
11133 ia64_c_mode_for_suffix (char suffix)
11135 if (suffix == 'q')
11136 return TFmode;
11137 if (suffix == 'w')
11138 return XFmode;
11140 return VOIDmode;
11143 static GTY(()) rtx ia64_dconst_0_5_rtx;
11146 ia64_dconst_0_5 (void)
11148 if (! ia64_dconst_0_5_rtx)
11150 REAL_VALUE_TYPE rv;
11151 real_from_string (&rv, "0.5");
11152 ia64_dconst_0_5_rtx = const_double_from_real_value (rv, DFmode);
11154 return ia64_dconst_0_5_rtx;
11157 static GTY(()) rtx ia64_dconst_0_375_rtx;
11160 ia64_dconst_0_375 (void)
11162 if (! ia64_dconst_0_375_rtx)
11164 REAL_VALUE_TYPE rv;
11165 real_from_string (&rv, "0.375");
11166 ia64_dconst_0_375_rtx = const_double_from_real_value (rv, DFmode);
11168 return ia64_dconst_0_375_rtx;
11171 static enum machine_mode
11172 ia64_get_reg_raw_mode (int regno)
11174 if (FR_REGNO_P (regno))
11175 return XFmode;
11176 return default_get_reg_raw_mode(regno);
11179 /* Implement TARGET_MEMBER_TYPE_FORCES_BLK. ??? Might not be needed
11180 anymore. */
11182 bool
11183 ia64_member_type_forces_blk (const_tree, enum machine_mode mode)
11185 return TARGET_HPUX && mode == TFmode;
11188 /* Always default to .text section until HP-UX linker is fixed. */
11190 ATTRIBUTE_UNUSED static section *
11191 ia64_hpux_function_section (tree decl ATTRIBUTE_UNUSED,
11192 enum node_frequency freq ATTRIBUTE_UNUSED,
11193 bool startup ATTRIBUTE_UNUSED,
11194 bool exit ATTRIBUTE_UNUSED)
11196 return NULL;
11199 /* Construct (set target (vec_select op0 (parallel perm))) and
11200 return true if that's a valid instruction in the active ISA. */
11202 static bool
11203 expand_vselect (rtx target, rtx op0, const unsigned char *perm, unsigned nelt)
11205 rtx rperm[MAX_VECT_LEN], x;
11206 unsigned i;
11208 for (i = 0; i < nelt; ++i)
11209 rperm[i] = GEN_INT (perm[i]);
11211 x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
11212 x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
11213 x = gen_rtx_SET (VOIDmode, target, x);
11215 rtx_insn *insn = emit_insn (x);
11216 if (recog_memoized (insn) < 0)
11218 remove_insn (insn);
11219 return false;
11221 return true;
11224 /* Similar, but generate a vec_concat from op0 and op1 as well. */
11226 static bool
11227 expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
11228 const unsigned char *perm, unsigned nelt)
11230 enum machine_mode v2mode;
11231 rtx x;
11233 v2mode = GET_MODE_2XWIDER_MODE (GET_MODE (op0));
11234 x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
11235 return expand_vselect (target, x, perm, nelt);
11238 /* Try to expand a no-op permutation. */
11240 static bool
11241 expand_vec_perm_identity (struct expand_vec_perm_d *d)
11243 unsigned i, nelt = d->nelt;
11245 for (i = 0; i < nelt; ++i)
11246 if (d->perm[i] != i)
11247 return false;
11249 if (!d->testing_p)
11250 emit_move_insn (d->target, d->op0);
11252 return true;
11255 /* Try to expand D via a shrp instruction. */
11257 static bool
11258 expand_vec_perm_shrp (struct expand_vec_perm_d *d)
11260 unsigned i, nelt = d->nelt, shift, mask;
11261 rtx tmp, hi, lo;
11263 /* ??? Don't force V2SFmode into the integer registers. */
11264 if (d->vmode == V2SFmode)
11265 return false;
11267 mask = (d->one_operand_p ? nelt - 1 : 2 * nelt - 1);
11269 shift = d->perm[0];
11270 if (BYTES_BIG_ENDIAN && shift > nelt)
11271 return false;
11273 for (i = 1; i < nelt; ++i)
11274 if (d->perm[i] != ((shift + i) & mask))
11275 return false;
11277 if (d->testing_p)
11278 return true;
11280 hi = shift < nelt ? d->op1 : d->op0;
11281 lo = shift < nelt ? d->op0 : d->op1;
11283 shift %= nelt;
11285 shift *= GET_MODE_UNIT_SIZE (d->vmode) * BITS_PER_UNIT;
11287 /* We've eliminated the shift 0 case via expand_vec_perm_identity. */
11288 gcc_assert (IN_RANGE (shift, 1, 63));
11290 /* Recall that big-endian elements are numbered starting at the top of
11291 the register. Ideally we'd have a shift-left-pair. But since we
11292 don't, convert to a shift the other direction. */
11293 if (BYTES_BIG_ENDIAN)
11294 shift = 64 - shift;
11296 tmp = gen_reg_rtx (DImode);
11297 hi = gen_lowpart (DImode, hi);
11298 lo = gen_lowpart (DImode, lo);
11299 emit_insn (gen_shrp (tmp, hi, lo, GEN_INT (shift)));
11301 emit_move_insn (d->target, gen_lowpart (d->vmode, tmp));
11302 return true;
11305 /* Try to instantiate D in a single instruction. */
11307 static bool
11308 expand_vec_perm_1 (struct expand_vec_perm_d *d)
11310 unsigned i, nelt = d->nelt;
11311 unsigned char perm2[MAX_VECT_LEN];
11313 /* Try single-operand selections. */
11314 if (d->one_operand_p)
11316 if (expand_vec_perm_identity (d))
11317 return true;
11318 if (expand_vselect (d->target, d->op0, d->perm, nelt))
11319 return true;
11322 /* Try two operand selections. */
11323 if (expand_vselect_vconcat (d->target, d->op0, d->op1, d->perm, nelt))
11324 return true;
11326 /* Recognize interleave style patterns with reversed operands. */
11327 if (!d->one_operand_p)
11329 for (i = 0; i < nelt; ++i)
11331 unsigned e = d->perm[i];
11332 if (e >= nelt)
11333 e -= nelt;
11334 else
11335 e += nelt;
11336 perm2[i] = e;
11339 if (expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
11340 return true;
11343 if (expand_vec_perm_shrp (d))
11344 return true;
11346 /* ??? Look for deposit-like permutations where most of the result
11347 comes from one vector unchanged and the rest comes from a
11348 sequential hunk of the other vector. */
11350 return false;
11353 /* Pattern match broadcast permutations. */
11355 static bool
11356 expand_vec_perm_broadcast (struct expand_vec_perm_d *d)
11358 unsigned i, elt, nelt = d->nelt;
11359 unsigned char perm2[2];
11360 rtx temp;
11361 bool ok;
11363 if (!d->one_operand_p)
11364 return false;
11366 elt = d->perm[0];
11367 for (i = 1; i < nelt; ++i)
11368 if (d->perm[i] != elt)
11369 return false;
11371 switch (d->vmode)
11373 case V2SImode:
11374 case V2SFmode:
11375 /* Implementable by interleave. */
11376 perm2[0] = elt;
11377 perm2[1] = elt + 2;
11378 ok = expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, 2);
11379 gcc_assert (ok);
11380 break;
11382 case V8QImode:
11383 /* Implementable by extract + broadcast. */
11384 if (BYTES_BIG_ENDIAN)
11385 elt = 7 - elt;
11386 elt *= BITS_PER_UNIT;
11387 temp = gen_reg_rtx (DImode);
11388 emit_insn (gen_extzv (temp, gen_lowpart (DImode, d->op0),
11389 GEN_INT (8), GEN_INT (elt)));
11390 emit_insn (gen_mux1_brcst_qi (d->target, gen_lowpart (QImode, temp)));
11391 break;
11393 case V4HImode:
11394 /* Should have been matched directly by vec_select. */
11395 default:
11396 gcc_unreachable ();
11399 return true;
11402 /* A subroutine of ia64_expand_vec_perm_const_1. Try to simplify a
11403 two vector permutation into a single vector permutation by using
11404 an interleave operation to merge the vectors. */
11406 static bool
11407 expand_vec_perm_interleave_2 (struct expand_vec_perm_d *d)
11409 struct expand_vec_perm_d dremap, dfinal;
11410 unsigned char remap[2 * MAX_VECT_LEN];
11411 unsigned contents, i, nelt, nelt2;
11412 unsigned h0, h1, h2, h3;
11413 rtx_insn *seq;
11414 bool ok;
11416 if (d->one_operand_p)
11417 return false;
11419 nelt = d->nelt;
11420 nelt2 = nelt / 2;
11422 /* Examine from whence the elements come. */
11423 contents = 0;
11424 for (i = 0; i < nelt; ++i)
11425 contents |= 1u << d->perm[i];
11427 memset (remap, 0xff, sizeof (remap));
11428 dremap = *d;
11430 h0 = (1u << nelt2) - 1;
11431 h1 = h0 << nelt2;
11432 h2 = h0 << nelt;
11433 h3 = h0 << (nelt + nelt2);
11435 if ((contents & (h0 | h2)) == contents) /* punpck even halves */
11437 for (i = 0; i < nelt; ++i)
11439 unsigned which = i / 2 + (i & 1 ? nelt : 0);
11440 remap[which] = i;
11441 dremap.perm[i] = which;
11444 else if ((contents & (h1 | h3)) == contents) /* punpck odd halves */
11446 for (i = 0; i < nelt; ++i)
11448 unsigned which = i / 2 + nelt2 + (i & 1 ? nelt : 0);
11449 remap[which] = i;
11450 dremap.perm[i] = which;
11453 else if ((contents & 0x5555) == contents) /* mix even elements */
11455 for (i = 0; i < nelt; ++i)
11457 unsigned which = (i & ~1) + (i & 1 ? nelt : 0);
11458 remap[which] = i;
11459 dremap.perm[i] = which;
11462 else if ((contents & 0xaaaa) == contents) /* mix odd elements */
11464 for (i = 0; i < nelt; ++i)
11466 unsigned which = (i | 1) + (i & 1 ? nelt : 0);
11467 remap[which] = i;
11468 dremap.perm[i] = which;
11471 else if (floor_log2 (contents) - ctz_hwi (contents) < (int)nelt) /* shrp */
11473 unsigned shift = ctz_hwi (contents);
11474 for (i = 0; i < nelt; ++i)
11476 unsigned which = (i + shift) & (2 * nelt - 1);
11477 remap[which] = i;
11478 dremap.perm[i] = which;
11481 else
11482 return false;
11484 /* Use the remapping array set up above to move the elements from their
11485 swizzled locations into their final destinations. */
11486 dfinal = *d;
11487 for (i = 0; i < nelt; ++i)
11489 unsigned e = remap[d->perm[i]];
11490 gcc_assert (e < nelt);
11491 dfinal.perm[i] = e;
11493 dfinal.op0 = gen_reg_rtx (dfinal.vmode);
11494 dfinal.op1 = dfinal.op0;
11495 dfinal.one_operand_p = true;
11496 dremap.target = dfinal.op0;
11498 /* Test if the final remap can be done with a single insn. For V4HImode
11499 this *will* succeed. For V8QImode or V2SImode it may not. */
11500 start_sequence ();
11501 ok = expand_vec_perm_1 (&dfinal);
11502 seq = get_insns ();
11503 end_sequence ();
11504 if (!ok)
11505 return false;
11506 if (d->testing_p)
11507 return true;
11509 ok = expand_vec_perm_1 (&dremap);
11510 gcc_assert (ok);
11512 emit_insn (seq);
11513 return true;
11516 /* A subroutine of ia64_expand_vec_perm_const_1. Emit a full V4HImode
11517 constant permutation via two mux2 and a merge. */
11519 static bool
11520 expand_vec_perm_v4hi_5 (struct expand_vec_perm_d *d)
11522 unsigned char perm2[4];
11523 rtx rmask[4];
11524 unsigned i;
11525 rtx t0, t1, mask, x;
11526 bool ok;
11528 if (d->vmode != V4HImode || d->one_operand_p)
11529 return false;
11530 if (d->testing_p)
11531 return true;
11533 for (i = 0; i < 4; ++i)
11535 perm2[i] = d->perm[i] & 3;
11536 rmask[i] = (d->perm[i] & 4 ? const0_rtx : constm1_rtx);
11538 mask = gen_rtx_CONST_VECTOR (V4HImode, gen_rtvec_v (4, rmask));
11539 mask = force_reg (V4HImode, mask);
11541 t0 = gen_reg_rtx (V4HImode);
11542 t1 = gen_reg_rtx (V4HImode);
11544 ok = expand_vselect (t0, d->op0, perm2, 4);
11545 gcc_assert (ok);
11546 ok = expand_vselect (t1, d->op1, perm2, 4);
11547 gcc_assert (ok);
11549 x = gen_rtx_AND (V4HImode, mask, t0);
11550 emit_insn (gen_rtx_SET (VOIDmode, t0, x));
11552 x = gen_rtx_NOT (V4HImode, mask);
11553 x = gen_rtx_AND (V4HImode, x, t1);
11554 emit_insn (gen_rtx_SET (VOIDmode, t1, x));
11556 x = gen_rtx_IOR (V4HImode, t0, t1);
11557 emit_insn (gen_rtx_SET (VOIDmode, d->target, x));
11559 return true;
11562 /* The guts of ia64_expand_vec_perm_const, also used by the ok hook.
11563 With all of the interface bits taken care of, perform the expansion
11564 in D and return true on success. */
11566 static bool
11567 ia64_expand_vec_perm_const_1 (struct expand_vec_perm_d *d)
11569 if (expand_vec_perm_1 (d))
11570 return true;
11571 if (expand_vec_perm_broadcast (d))
11572 return true;
11573 if (expand_vec_perm_interleave_2 (d))
11574 return true;
11575 if (expand_vec_perm_v4hi_5 (d))
11576 return true;
11577 return false;
11580 bool
11581 ia64_expand_vec_perm_const (rtx operands[4])
11583 struct expand_vec_perm_d d;
11584 unsigned char perm[MAX_VECT_LEN];
11585 int i, nelt, which;
11586 rtx sel;
11588 d.target = operands[0];
11589 d.op0 = operands[1];
11590 d.op1 = operands[2];
11591 sel = operands[3];
11593 d.vmode = GET_MODE (d.target);
11594 gcc_assert (VECTOR_MODE_P (d.vmode));
11595 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
11596 d.testing_p = false;
11598 gcc_assert (GET_CODE (sel) == CONST_VECTOR);
11599 gcc_assert (XVECLEN (sel, 0) == nelt);
11600 gcc_checking_assert (sizeof (d.perm) == sizeof (perm));
11602 for (i = which = 0; i < nelt; ++i)
11604 rtx e = XVECEXP (sel, 0, i);
11605 int ei = INTVAL (e) & (2 * nelt - 1);
11607 which |= (ei < nelt ? 1 : 2);
11608 d.perm[i] = ei;
11609 perm[i] = ei;
11612 switch (which)
11614 default:
11615 gcc_unreachable();
11617 case 3:
11618 if (!rtx_equal_p (d.op0, d.op1))
11620 d.one_operand_p = false;
11621 break;
11624 /* The elements of PERM do not suggest that only the first operand
11625 is used, but both operands are identical. Allow easier matching
11626 of the permutation by folding the permutation into the single
11627 input vector. */
11628 for (i = 0; i < nelt; ++i)
11629 if (d.perm[i] >= nelt)
11630 d.perm[i] -= nelt;
11631 /* FALLTHRU */
11633 case 1:
11634 d.op1 = d.op0;
11635 d.one_operand_p = true;
11636 break;
11638 case 2:
11639 for (i = 0; i < nelt; ++i)
11640 d.perm[i] -= nelt;
11641 d.op0 = d.op1;
11642 d.one_operand_p = true;
11643 break;
11646 if (ia64_expand_vec_perm_const_1 (&d))
11647 return true;
11649 /* If the mask says both arguments are needed, but they are the same,
11650 the above tried to expand with one_operand_p true. If that didn't
11651 work, retry with one_operand_p false, as that's what we used in _ok. */
11652 if (which == 3 && d.one_operand_p)
11654 memcpy (d.perm, perm, sizeof (perm));
11655 d.one_operand_p = false;
11656 return ia64_expand_vec_perm_const_1 (&d);
11659 return false;
11662 /* Implement targetm.vectorize.vec_perm_const_ok. */
11664 static bool
11665 ia64_vectorize_vec_perm_const_ok (enum machine_mode vmode,
11666 const unsigned char *sel)
11668 struct expand_vec_perm_d d;
11669 unsigned int i, nelt, which;
11670 bool ret;
11672 d.vmode = vmode;
11673 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
11674 d.testing_p = true;
11676 /* Extract the values from the vector CST into the permutation
11677 array in D. */
11678 memcpy (d.perm, sel, nelt);
11679 for (i = which = 0; i < nelt; ++i)
11681 unsigned char e = d.perm[i];
11682 gcc_assert (e < 2 * nelt);
11683 which |= (e < nelt ? 1 : 2);
11686 /* For all elements from second vector, fold the elements to first. */
11687 if (which == 2)
11688 for (i = 0; i < nelt; ++i)
11689 d.perm[i] -= nelt;
11691 /* Check whether the mask can be applied to the vector type. */
11692 d.one_operand_p = (which != 3);
11694 /* Otherwise we have to go through the motions and see if we can
11695 figure out how to generate the requested permutation. */
11696 d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
11697 d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
11698 if (!d.one_operand_p)
11699 d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
11701 start_sequence ();
11702 ret = ia64_expand_vec_perm_const_1 (&d);
11703 end_sequence ();
11705 return ret;
11708 void
11709 ia64_expand_vec_setv2sf (rtx operands[3])
11711 struct expand_vec_perm_d d;
11712 unsigned int which;
11713 bool ok;
11715 d.target = operands[0];
11716 d.op0 = operands[0];
11717 d.op1 = gen_reg_rtx (V2SFmode);
11718 d.vmode = V2SFmode;
11719 d.nelt = 2;
11720 d.one_operand_p = false;
11721 d.testing_p = false;
11723 which = INTVAL (operands[2]);
11724 gcc_assert (which <= 1);
11725 d.perm[0] = 1 - which;
11726 d.perm[1] = which + 2;
11728 emit_insn (gen_fpack (d.op1, operands[1], CONST0_RTX (SFmode)));
11730 ok = ia64_expand_vec_perm_const_1 (&d);
11731 gcc_assert (ok);
11734 void
11735 ia64_expand_vec_perm_even_odd (rtx target, rtx op0, rtx op1, int odd)
11737 struct expand_vec_perm_d d;
11738 enum machine_mode vmode = GET_MODE (target);
11739 unsigned int i, nelt = GET_MODE_NUNITS (vmode);
11740 bool ok;
11742 d.target = target;
11743 d.op0 = op0;
11744 d.op1 = op1;
11745 d.vmode = vmode;
11746 d.nelt = nelt;
11747 d.one_operand_p = false;
11748 d.testing_p = false;
11750 for (i = 0; i < nelt; ++i)
11751 d.perm[i] = i * 2 + odd;
11753 ok = ia64_expand_vec_perm_const_1 (&d);
11754 gcc_assert (ok);
11757 #include "gt-ia64.h"