2013-11-12 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / config / ia64 / ia64.c
blob2b331873e2444da72b5f34106710f08bb568f693
1 /* Definitions of target machine for GNU compiler.
2 Copyright (C) 1999-2013 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 "regs.h"
29 #include "hard-reg-set.h"
30 #include "insn-config.h"
31 #include "conditions.h"
32 #include "output.h"
33 #include "insn-attr.h"
34 #include "flags.h"
35 #include "recog.h"
36 #include "expr.h"
37 #include "optabs.h"
38 #include "except.h"
39 #include "function.h"
40 #include "ggc.h"
41 #include "basic-block.h"
42 #include "libfuncs.h"
43 #include "diagnostic-core.h"
44 #include "sched-int.h"
45 #include "timevar.h"
46 #include "target.h"
47 #include "target-def.h"
48 #include "common/common-target.h"
49 #include "tm_p.h"
50 #include "hash-table.h"
51 #include "langhooks.h"
52 #include "gimplify.h"
53 #include "intl.h"
54 #include "df.h"
55 #include "debug.h"
56 #include "params.h"
57 #include "dbgcnt.h"
58 #include "tm-constrs.h"
59 #include "sel-sched.h"
60 #include "reload.h"
61 #include "opts.h"
62 #include "dumpfile.h"
64 /* This is used for communication between ASM_OUTPUT_LABEL and
65 ASM_OUTPUT_LABELREF. */
66 int ia64_asm_output_label = 0;
68 /* Register names for ia64_expand_prologue. */
69 static const char * const ia64_reg_numbers[96] =
70 { "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
71 "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
72 "r48", "r49", "r50", "r51", "r52", "r53", "r54", "r55",
73 "r56", "r57", "r58", "r59", "r60", "r61", "r62", "r63",
74 "r64", "r65", "r66", "r67", "r68", "r69", "r70", "r71",
75 "r72", "r73", "r74", "r75", "r76", "r77", "r78", "r79",
76 "r80", "r81", "r82", "r83", "r84", "r85", "r86", "r87",
77 "r88", "r89", "r90", "r91", "r92", "r93", "r94", "r95",
78 "r96", "r97", "r98", "r99", "r100","r101","r102","r103",
79 "r104","r105","r106","r107","r108","r109","r110","r111",
80 "r112","r113","r114","r115","r116","r117","r118","r119",
81 "r120","r121","r122","r123","r124","r125","r126","r127"};
83 /* ??? These strings could be shared with REGISTER_NAMES. */
84 static const char * const ia64_input_reg_names[8] =
85 { "in0", "in1", "in2", "in3", "in4", "in5", "in6", "in7" };
87 /* ??? These strings could be shared with REGISTER_NAMES. */
88 static const char * const ia64_local_reg_names[80] =
89 { "loc0", "loc1", "loc2", "loc3", "loc4", "loc5", "loc6", "loc7",
90 "loc8", "loc9", "loc10","loc11","loc12","loc13","loc14","loc15",
91 "loc16","loc17","loc18","loc19","loc20","loc21","loc22","loc23",
92 "loc24","loc25","loc26","loc27","loc28","loc29","loc30","loc31",
93 "loc32","loc33","loc34","loc35","loc36","loc37","loc38","loc39",
94 "loc40","loc41","loc42","loc43","loc44","loc45","loc46","loc47",
95 "loc48","loc49","loc50","loc51","loc52","loc53","loc54","loc55",
96 "loc56","loc57","loc58","loc59","loc60","loc61","loc62","loc63",
97 "loc64","loc65","loc66","loc67","loc68","loc69","loc70","loc71",
98 "loc72","loc73","loc74","loc75","loc76","loc77","loc78","loc79" };
100 /* ??? These strings could be shared with REGISTER_NAMES. */
101 static const char * const ia64_output_reg_names[8] =
102 { "out0", "out1", "out2", "out3", "out4", "out5", "out6", "out7" };
104 /* Variables which are this size or smaller are put in the sdata/sbss
105 sections. */
107 unsigned int ia64_section_threshold;
109 /* The following variable is used by the DFA insn scheduler. The value is
110 TRUE if we do insn bundling instead of insn scheduling. */
111 int bundling_p = 0;
113 enum ia64_frame_regs
115 reg_fp,
116 reg_save_b0,
117 reg_save_pr,
118 reg_save_ar_pfs,
119 reg_save_ar_unat,
120 reg_save_ar_lc,
121 reg_save_gp,
122 number_of_ia64_frame_regs
125 /* Structure to be filled in by ia64_compute_frame_size with register
126 save masks and offsets for the current function. */
128 struct ia64_frame_info
130 HOST_WIDE_INT total_size; /* size of the stack frame, not including
131 the caller's scratch area. */
132 HOST_WIDE_INT spill_cfa_off; /* top of the reg spill area from the cfa. */
133 HOST_WIDE_INT spill_size; /* size of the gr/br/fr spill area. */
134 HOST_WIDE_INT extra_spill_size; /* size of spill area for others. */
135 HARD_REG_SET mask; /* mask of saved registers. */
136 unsigned int gr_used_mask; /* mask of registers in use as gr spill
137 registers or long-term scratches. */
138 int n_spilled; /* number of spilled registers. */
139 int r[number_of_ia64_frame_regs]; /* Frame related registers. */
140 int n_input_regs; /* number of input registers used. */
141 int n_local_regs; /* number of local registers used. */
142 int n_output_regs; /* number of output registers used. */
143 int n_rotate_regs; /* number of rotating registers used. */
145 char need_regstk; /* true if a .regstk directive needed. */
146 char initialized; /* true if the data is finalized. */
149 /* Current frame information calculated by ia64_compute_frame_size. */
150 static struct ia64_frame_info current_frame_info;
151 /* The actual registers that are emitted. */
152 static int emitted_frame_related_regs[number_of_ia64_frame_regs];
154 static int ia64_first_cycle_multipass_dfa_lookahead (void);
155 static void ia64_dependencies_evaluation_hook (rtx, rtx);
156 static void ia64_init_dfa_pre_cycle_insn (void);
157 static rtx ia64_dfa_pre_cycle_insn (void);
158 static int ia64_first_cycle_multipass_dfa_lookahead_guard (rtx);
159 static bool ia64_first_cycle_multipass_dfa_lookahead_guard_spec (const_rtx);
160 static int ia64_dfa_new_cycle (FILE *, int, rtx, int, int, int *);
161 static void ia64_h_i_d_extended (void);
162 static void * ia64_alloc_sched_context (void);
163 static void ia64_init_sched_context (void *, bool);
164 static void ia64_set_sched_context (void *);
165 static void ia64_clear_sched_context (void *);
166 static void ia64_free_sched_context (void *);
167 static int ia64_mode_to_int (enum machine_mode);
168 static void ia64_set_sched_flags (spec_info_t);
169 static ds_t ia64_get_insn_spec_ds (rtx);
170 static ds_t ia64_get_insn_checked_ds (rtx);
171 static bool ia64_skip_rtx_p (const_rtx);
172 static int ia64_speculate_insn (rtx, ds_t, rtx *);
173 static bool ia64_needs_block_p (ds_t);
174 static rtx ia64_gen_spec_check (rtx, rtx, ds_t);
175 static int ia64_spec_check_p (rtx);
176 static int ia64_spec_check_src_p (rtx);
177 static rtx gen_tls_get_addr (void);
178 static rtx gen_thread_pointer (void);
179 static int find_gr_spill (enum ia64_frame_regs, int);
180 static int next_scratch_gr_reg (void);
181 static void mark_reg_gr_used_mask (rtx, void *);
182 static void ia64_compute_frame_size (HOST_WIDE_INT);
183 static void setup_spill_pointers (int, rtx, HOST_WIDE_INT);
184 static void finish_spill_pointers (void);
185 static rtx spill_restore_mem (rtx, HOST_WIDE_INT);
186 static void do_spill (rtx (*)(rtx, rtx, rtx), rtx, HOST_WIDE_INT, rtx);
187 static void do_restore (rtx (*)(rtx, rtx, rtx), rtx, HOST_WIDE_INT);
188 static rtx gen_movdi_x (rtx, rtx, rtx);
189 static rtx gen_fr_spill_x (rtx, rtx, rtx);
190 static rtx gen_fr_restore_x (rtx, rtx, rtx);
192 static void ia64_option_override (void);
193 static bool ia64_can_eliminate (const int, const int);
194 static enum machine_mode hfa_element_mode (const_tree, bool);
195 static void ia64_setup_incoming_varargs (cumulative_args_t, enum machine_mode,
196 tree, int *, int);
197 static int ia64_arg_partial_bytes (cumulative_args_t, enum machine_mode,
198 tree, bool);
199 static rtx ia64_function_arg_1 (cumulative_args_t, enum machine_mode,
200 const_tree, bool, bool);
201 static rtx ia64_function_arg (cumulative_args_t, enum machine_mode,
202 const_tree, bool);
203 static rtx ia64_function_incoming_arg (cumulative_args_t,
204 enum machine_mode, const_tree, bool);
205 static void ia64_function_arg_advance (cumulative_args_t, enum machine_mode,
206 const_tree, bool);
207 static unsigned int ia64_function_arg_boundary (enum machine_mode,
208 const_tree);
209 static bool ia64_function_ok_for_sibcall (tree, tree);
210 static bool ia64_return_in_memory (const_tree, const_tree);
211 static rtx ia64_function_value (const_tree, const_tree, bool);
212 static rtx ia64_libcall_value (enum machine_mode, const_rtx);
213 static bool ia64_function_value_regno_p (const unsigned int);
214 static int ia64_register_move_cost (enum machine_mode, reg_class_t,
215 reg_class_t);
216 static int ia64_memory_move_cost (enum machine_mode mode, reg_class_t,
217 bool);
218 static bool ia64_rtx_costs (rtx, int, int, int, int *, bool);
219 static int ia64_unspec_may_trap_p (const_rtx, unsigned);
220 static void fix_range (const char *);
221 static struct machine_function * ia64_init_machine_status (void);
222 static void emit_insn_group_barriers (FILE *);
223 static void emit_all_insn_group_barriers (FILE *);
224 static void final_emit_insn_group_barriers (FILE *);
225 static void emit_predicate_relation_info (void);
226 static void ia64_reorg (void);
227 static bool ia64_in_small_data_p (const_tree);
228 static void process_epilogue (FILE *, rtx, bool, bool);
230 static bool ia64_assemble_integer (rtx, unsigned int, int);
231 static void ia64_output_function_prologue (FILE *, HOST_WIDE_INT);
232 static void ia64_output_function_epilogue (FILE *, HOST_WIDE_INT);
233 static void ia64_output_function_end_prologue (FILE *);
235 static void ia64_print_operand (FILE *, rtx, int);
236 static void ia64_print_operand_address (FILE *, rtx);
237 static bool ia64_print_operand_punct_valid_p (unsigned char code);
239 static int ia64_issue_rate (void);
240 static int ia64_adjust_cost_2 (rtx, int, rtx, int, dw_t);
241 static void ia64_sched_init (FILE *, int, int);
242 static void ia64_sched_init_global (FILE *, int, int);
243 static void ia64_sched_finish_global (FILE *, int);
244 static void ia64_sched_finish (FILE *, int);
245 static int ia64_dfa_sched_reorder (FILE *, int, rtx *, int *, int, int);
246 static int ia64_sched_reorder (FILE *, int, rtx *, int *, int);
247 static int ia64_sched_reorder2 (FILE *, int, rtx *, int *, int);
248 static int ia64_variable_issue (FILE *, int, rtx, int);
250 static void ia64_asm_unwind_emit (FILE *, rtx);
251 static void ia64_asm_emit_except_personality (rtx);
252 static void ia64_asm_init_sections (void);
254 static enum unwind_info_type ia64_debug_unwind_info (void);
256 static struct bundle_state *get_free_bundle_state (void);
257 static void free_bundle_state (struct bundle_state *);
258 static void initiate_bundle_states (void);
259 static void finish_bundle_states (void);
260 static int insert_bundle_state (struct bundle_state *);
261 static void initiate_bundle_state_table (void);
262 static void finish_bundle_state_table (void);
263 static int try_issue_nops (struct bundle_state *, int);
264 static int try_issue_insn (struct bundle_state *, rtx);
265 static void issue_nops_and_insn (struct bundle_state *, int, rtx, int, int);
266 static int get_max_pos (state_t);
267 static int get_template (state_t, int);
269 static rtx get_next_important_insn (rtx, rtx);
270 static bool important_for_bundling_p (rtx);
271 static bool unknown_for_bundling_p (rtx);
272 static void bundling (FILE *, int, rtx, rtx);
274 static void ia64_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
275 HOST_WIDE_INT, tree);
276 static void ia64_file_start (void);
277 static void ia64_globalize_decl_name (FILE *, tree);
279 static int ia64_hpux_reloc_rw_mask (void) ATTRIBUTE_UNUSED;
280 static int ia64_reloc_rw_mask (void) ATTRIBUTE_UNUSED;
281 static section *ia64_select_rtx_section (enum machine_mode, rtx,
282 unsigned HOST_WIDE_INT);
283 static void ia64_output_dwarf_dtprel (FILE *, int, rtx)
284 ATTRIBUTE_UNUSED;
285 static unsigned int ia64_section_type_flags (tree, const char *, int);
286 static void ia64_init_libfuncs (void)
287 ATTRIBUTE_UNUSED;
288 static void ia64_hpux_init_libfuncs (void)
289 ATTRIBUTE_UNUSED;
290 static void ia64_sysv4_init_libfuncs (void)
291 ATTRIBUTE_UNUSED;
292 static void ia64_vms_init_libfuncs (void)
293 ATTRIBUTE_UNUSED;
294 static void ia64_soft_fp_init_libfuncs (void)
295 ATTRIBUTE_UNUSED;
296 static bool ia64_vms_valid_pointer_mode (enum machine_mode mode)
297 ATTRIBUTE_UNUSED;
298 static tree ia64_vms_common_object_attribute (tree *, tree, tree, int, bool *)
299 ATTRIBUTE_UNUSED;
301 static tree ia64_handle_model_attribute (tree *, tree, tree, int, bool *);
302 static tree ia64_handle_version_id_attribute (tree *, tree, tree, int, bool *);
303 static void ia64_encode_section_info (tree, rtx, int);
304 static rtx ia64_struct_value_rtx (tree, int);
305 static tree ia64_gimplify_va_arg (tree, tree, gimple_seq *, gimple_seq *);
306 static bool ia64_scalar_mode_supported_p (enum machine_mode mode);
307 static bool ia64_vector_mode_supported_p (enum machine_mode mode);
308 static bool ia64_legitimate_constant_p (enum machine_mode, rtx);
309 static bool ia64_legitimate_address_p (enum machine_mode, rtx, bool);
310 static bool ia64_cannot_force_const_mem (enum machine_mode, rtx);
311 static const char *ia64_mangle_type (const_tree);
312 static const char *ia64_invalid_conversion (const_tree, const_tree);
313 static const char *ia64_invalid_unary_op (int, const_tree);
314 static const char *ia64_invalid_binary_op (int, const_tree, const_tree);
315 static enum machine_mode ia64_c_mode_for_suffix (char);
316 static void ia64_trampoline_init (rtx, tree, rtx);
317 static void ia64_override_options_after_change (void);
318 static bool ia64_member_type_forces_blk (const_tree, enum machine_mode);
320 static tree ia64_builtin_decl (unsigned, bool);
322 static reg_class_t ia64_preferred_reload_class (rtx, reg_class_t);
323 static enum machine_mode ia64_get_reg_raw_mode (int regno);
324 static section * ia64_hpux_function_section (tree, enum node_frequency,
325 bool, bool);
327 static bool ia64_vectorize_vec_perm_const_ok (enum machine_mode vmode,
328 const unsigned char *sel);
330 #define MAX_VECT_LEN 8
332 struct expand_vec_perm_d
334 rtx target, op0, op1;
335 unsigned char perm[MAX_VECT_LEN];
336 enum machine_mode vmode;
337 unsigned char nelt;
338 bool one_operand_p;
339 bool testing_p;
342 static bool ia64_expand_vec_perm_const_1 (struct expand_vec_perm_d *d);
345 /* Table of valid machine attributes. */
346 static const struct attribute_spec ia64_attribute_table[] =
348 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
349 affects_type_identity } */
350 { "syscall_linkage", 0, 0, false, true, true, NULL, false },
351 { "model", 1, 1, true, false, false, ia64_handle_model_attribute,
352 false },
353 #if TARGET_ABI_OPEN_VMS
354 { "common_object", 1, 1, true, false, false,
355 ia64_vms_common_object_attribute, false },
356 #endif
357 { "version_id", 1, 1, true, false, false,
358 ia64_handle_version_id_attribute, false },
359 { NULL, 0, 0, false, false, false, NULL, false }
362 /* Initialize the GCC target structure. */
363 #undef TARGET_ATTRIBUTE_TABLE
364 #define TARGET_ATTRIBUTE_TABLE ia64_attribute_table
366 #undef TARGET_INIT_BUILTINS
367 #define TARGET_INIT_BUILTINS ia64_init_builtins
369 #undef TARGET_EXPAND_BUILTIN
370 #define TARGET_EXPAND_BUILTIN ia64_expand_builtin
372 #undef TARGET_BUILTIN_DECL
373 #define TARGET_BUILTIN_DECL ia64_builtin_decl
375 #undef TARGET_ASM_BYTE_OP
376 #define TARGET_ASM_BYTE_OP "\tdata1\t"
377 #undef TARGET_ASM_ALIGNED_HI_OP
378 #define TARGET_ASM_ALIGNED_HI_OP "\tdata2\t"
379 #undef TARGET_ASM_ALIGNED_SI_OP
380 #define TARGET_ASM_ALIGNED_SI_OP "\tdata4\t"
381 #undef TARGET_ASM_ALIGNED_DI_OP
382 #define TARGET_ASM_ALIGNED_DI_OP "\tdata8\t"
383 #undef TARGET_ASM_UNALIGNED_HI_OP
384 #define TARGET_ASM_UNALIGNED_HI_OP "\tdata2.ua\t"
385 #undef TARGET_ASM_UNALIGNED_SI_OP
386 #define TARGET_ASM_UNALIGNED_SI_OP "\tdata4.ua\t"
387 #undef TARGET_ASM_UNALIGNED_DI_OP
388 #define TARGET_ASM_UNALIGNED_DI_OP "\tdata8.ua\t"
389 #undef TARGET_ASM_INTEGER
390 #define TARGET_ASM_INTEGER ia64_assemble_integer
392 #undef TARGET_OPTION_OVERRIDE
393 #define TARGET_OPTION_OVERRIDE ia64_option_override
395 #undef TARGET_ASM_FUNCTION_PROLOGUE
396 #define TARGET_ASM_FUNCTION_PROLOGUE ia64_output_function_prologue
397 #undef TARGET_ASM_FUNCTION_END_PROLOGUE
398 #define TARGET_ASM_FUNCTION_END_PROLOGUE ia64_output_function_end_prologue
399 #undef TARGET_ASM_FUNCTION_EPILOGUE
400 #define TARGET_ASM_FUNCTION_EPILOGUE ia64_output_function_epilogue
402 #undef TARGET_PRINT_OPERAND
403 #define TARGET_PRINT_OPERAND ia64_print_operand
404 #undef TARGET_PRINT_OPERAND_ADDRESS
405 #define TARGET_PRINT_OPERAND_ADDRESS ia64_print_operand_address
406 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
407 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P ia64_print_operand_punct_valid_p
409 #undef TARGET_IN_SMALL_DATA_P
410 #define TARGET_IN_SMALL_DATA_P ia64_in_small_data_p
412 #undef TARGET_SCHED_ADJUST_COST_2
413 #define TARGET_SCHED_ADJUST_COST_2 ia64_adjust_cost_2
414 #undef TARGET_SCHED_ISSUE_RATE
415 #define TARGET_SCHED_ISSUE_RATE ia64_issue_rate
416 #undef TARGET_SCHED_VARIABLE_ISSUE
417 #define TARGET_SCHED_VARIABLE_ISSUE ia64_variable_issue
418 #undef TARGET_SCHED_INIT
419 #define TARGET_SCHED_INIT ia64_sched_init
420 #undef TARGET_SCHED_FINISH
421 #define TARGET_SCHED_FINISH ia64_sched_finish
422 #undef TARGET_SCHED_INIT_GLOBAL
423 #define TARGET_SCHED_INIT_GLOBAL ia64_sched_init_global
424 #undef TARGET_SCHED_FINISH_GLOBAL
425 #define TARGET_SCHED_FINISH_GLOBAL ia64_sched_finish_global
426 #undef TARGET_SCHED_REORDER
427 #define TARGET_SCHED_REORDER ia64_sched_reorder
428 #undef TARGET_SCHED_REORDER2
429 #define TARGET_SCHED_REORDER2 ia64_sched_reorder2
431 #undef TARGET_SCHED_DEPENDENCIES_EVALUATION_HOOK
432 #define TARGET_SCHED_DEPENDENCIES_EVALUATION_HOOK ia64_dependencies_evaluation_hook
434 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
435 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD ia64_first_cycle_multipass_dfa_lookahead
437 #undef TARGET_SCHED_INIT_DFA_PRE_CYCLE_INSN
438 #define TARGET_SCHED_INIT_DFA_PRE_CYCLE_INSN ia64_init_dfa_pre_cycle_insn
439 #undef TARGET_SCHED_DFA_PRE_CYCLE_INSN
440 #define TARGET_SCHED_DFA_PRE_CYCLE_INSN ia64_dfa_pre_cycle_insn
442 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
443 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD\
444 ia64_first_cycle_multipass_dfa_lookahead_guard
446 #undef TARGET_SCHED_DFA_NEW_CYCLE
447 #define TARGET_SCHED_DFA_NEW_CYCLE ia64_dfa_new_cycle
449 #undef TARGET_SCHED_H_I_D_EXTENDED
450 #define TARGET_SCHED_H_I_D_EXTENDED ia64_h_i_d_extended
452 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
453 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT ia64_alloc_sched_context
455 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
456 #define TARGET_SCHED_INIT_SCHED_CONTEXT ia64_init_sched_context
458 #undef TARGET_SCHED_SET_SCHED_CONTEXT
459 #define TARGET_SCHED_SET_SCHED_CONTEXT ia64_set_sched_context
461 #undef TARGET_SCHED_CLEAR_SCHED_CONTEXT
462 #define TARGET_SCHED_CLEAR_SCHED_CONTEXT ia64_clear_sched_context
464 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
465 #define TARGET_SCHED_FREE_SCHED_CONTEXT ia64_free_sched_context
467 #undef TARGET_SCHED_SET_SCHED_FLAGS
468 #define TARGET_SCHED_SET_SCHED_FLAGS ia64_set_sched_flags
470 #undef TARGET_SCHED_GET_INSN_SPEC_DS
471 #define TARGET_SCHED_GET_INSN_SPEC_DS ia64_get_insn_spec_ds
473 #undef TARGET_SCHED_GET_INSN_CHECKED_DS
474 #define TARGET_SCHED_GET_INSN_CHECKED_DS ia64_get_insn_checked_ds
476 #undef TARGET_SCHED_SPECULATE_INSN
477 #define TARGET_SCHED_SPECULATE_INSN ia64_speculate_insn
479 #undef TARGET_SCHED_NEEDS_BLOCK_P
480 #define TARGET_SCHED_NEEDS_BLOCK_P ia64_needs_block_p
482 #undef TARGET_SCHED_GEN_SPEC_CHECK
483 #define TARGET_SCHED_GEN_SPEC_CHECK ia64_gen_spec_check
485 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD_SPEC
486 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD_SPEC\
487 ia64_first_cycle_multipass_dfa_lookahead_guard_spec
489 #undef TARGET_SCHED_SKIP_RTX_P
490 #define TARGET_SCHED_SKIP_RTX_P ia64_skip_rtx_p
492 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
493 #define TARGET_FUNCTION_OK_FOR_SIBCALL ia64_function_ok_for_sibcall
494 #undef TARGET_ARG_PARTIAL_BYTES
495 #define TARGET_ARG_PARTIAL_BYTES ia64_arg_partial_bytes
496 #undef TARGET_FUNCTION_ARG
497 #define TARGET_FUNCTION_ARG ia64_function_arg
498 #undef TARGET_FUNCTION_INCOMING_ARG
499 #define TARGET_FUNCTION_INCOMING_ARG ia64_function_incoming_arg
500 #undef TARGET_FUNCTION_ARG_ADVANCE
501 #define TARGET_FUNCTION_ARG_ADVANCE ia64_function_arg_advance
502 #undef TARGET_FUNCTION_ARG_BOUNDARY
503 #define TARGET_FUNCTION_ARG_BOUNDARY ia64_function_arg_boundary
505 #undef TARGET_ASM_OUTPUT_MI_THUNK
506 #define TARGET_ASM_OUTPUT_MI_THUNK ia64_output_mi_thunk
507 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
508 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
510 #undef TARGET_ASM_FILE_START
511 #define TARGET_ASM_FILE_START ia64_file_start
513 #undef TARGET_ASM_GLOBALIZE_DECL_NAME
514 #define TARGET_ASM_GLOBALIZE_DECL_NAME ia64_globalize_decl_name
516 #undef TARGET_REGISTER_MOVE_COST
517 #define TARGET_REGISTER_MOVE_COST ia64_register_move_cost
518 #undef TARGET_MEMORY_MOVE_COST
519 #define TARGET_MEMORY_MOVE_COST ia64_memory_move_cost
520 #undef TARGET_RTX_COSTS
521 #define TARGET_RTX_COSTS ia64_rtx_costs
522 #undef TARGET_ADDRESS_COST
523 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
525 #undef TARGET_UNSPEC_MAY_TRAP_P
526 #define TARGET_UNSPEC_MAY_TRAP_P ia64_unspec_may_trap_p
528 #undef TARGET_MACHINE_DEPENDENT_REORG
529 #define TARGET_MACHINE_DEPENDENT_REORG ia64_reorg
531 #undef TARGET_ENCODE_SECTION_INFO
532 #define TARGET_ENCODE_SECTION_INFO ia64_encode_section_info
534 #undef TARGET_SECTION_TYPE_FLAGS
535 #define TARGET_SECTION_TYPE_FLAGS ia64_section_type_flags
537 #ifdef HAVE_AS_TLS
538 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
539 #define TARGET_ASM_OUTPUT_DWARF_DTPREL ia64_output_dwarf_dtprel
540 #endif
542 /* ??? Investigate. */
543 #if 0
544 #undef TARGET_PROMOTE_PROTOTYPES
545 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
546 #endif
548 #undef TARGET_FUNCTION_VALUE
549 #define TARGET_FUNCTION_VALUE ia64_function_value
550 #undef TARGET_LIBCALL_VALUE
551 #define TARGET_LIBCALL_VALUE ia64_libcall_value
552 #undef TARGET_FUNCTION_VALUE_REGNO_P
553 #define TARGET_FUNCTION_VALUE_REGNO_P ia64_function_value_regno_p
555 #undef TARGET_STRUCT_VALUE_RTX
556 #define TARGET_STRUCT_VALUE_RTX ia64_struct_value_rtx
557 #undef TARGET_RETURN_IN_MEMORY
558 #define TARGET_RETURN_IN_MEMORY ia64_return_in_memory
559 #undef TARGET_SETUP_INCOMING_VARARGS
560 #define TARGET_SETUP_INCOMING_VARARGS ia64_setup_incoming_varargs
561 #undef TARGET_STRICT_ARGUMENT_NAMING
562 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
563 #undef TARGET_MUST_PASS_IN_STACK
564 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
565 #undef TARGET_GET_RAW_RESULT_MODE
566 #define TARGET_GET_RAW_RESULT_MODE ia64_get_reg_raw_mode
567 #undef TARGET_GET_RAW_ARG_MODE
568 #define TARGET_GET_RAW_ARG_MODE ia64_get_reg_raw_mode
570 #undef TARGET_MEMBER_TYPE_FORCES_BLK
571 #define TARGET_MEMBER_TYPE_FORCES_BLK ia64_member_type_forces_blk
573 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
574 #define TARGET_GIMPLIFY_VA_ARG_EXPR ia64_gimplify_va_arg
576 #undef TARGET_ASM_UNWIND_EMIT
577 #define TARGET_ASM_UNWIND_EMIT ia64_asm_unwind_emit
578 #undef TARGET_ASM_EMIT_EXCEPT_PERSONALITY
579 #define TARGET_ASM_EMIT_EXCEPT_PERSONALITY ia64_asm_emit_except_personality
580 #undef TARGET_ASM_INIT_SECTIONS
581 #define TARGET_ASM_INIT_SECTIONS ia64_asm_init_sections
583 #undef TARGET_DEBUG_UNWIND_INFO
584 #define TARGET_DEBUG_UNWIND_INFO ia64_debug_unwind_info
586 #undef TARGET_SCALAR_MODE_SUPPORTED_P
587 #define TARGET_SCALAR_MODE_SUPPORTED_P ia64_scalar_mode_supported_p
588 #undef TARGET_VECTOR_MODE_SUPPORTED_P
589 #define TARGET_VECTOR_MODE_SUPPORTED_P ia64_vector_mode_supported_p
591 /* ia64 architecture manual 4.4.7: ... reads, writes, and flushes may occur
592 in an order different from the specified program order. */
593 #undef TARGET_RELAXED_ORDERING
594 #define TARGET_RELAXED_ORDERING true
596 #undef TARGET_LEGITIMATE_CONSTANT_P
597 #define TARGET_LEGITIMATE_CONSTANT_P ia64_legitimate_constant_p
598 #undef TARGET_LEGITIMATE_ADDRESS_P
599 #define TARGET_LEGITIMATE_ADDRESS_P ia64_legitimate_address_p
601 #undef TARGET_CANNOT_FORCE_CONST_MEM
602 #define TARGET_CANNOT_FORCE_CONST_MEM ia64_cannot_force_const_mem
604 #undef TARGET_MANGLE_TYPE
605 #define TARGET_MANGLE_TYPE ia64_mangle_type
607 #undef TARGET_INVALID_CONVERSION
608 #define TARGET_INVALID_CONVERSION ia64_invalid_conversion
609 #undef TARGET_INVALID_UNARY_OP
610 #define TARGET_INVALID_UNARY_OP ia64_invalid_unary_op
611 #undef TARGET_INVALID_BINARY_OP
612 #define TARGET_INVALID_BINARY_OP ia64_invalid_binary_op
614 #undef TARGET_C_MODE_FOR_SUFFIX
615 #define TARGET_C_MODE_FOR_SUFFIX ia64_c_mode_for_suffix
617 #undef TARGET_CAN_ELIMINATE
618 #define TARGET_CAN_ELIMINATE ia64_can_eliminate
620 #undef TARGET_TRAMPOLINE_INIT
621 #define TARGET_TRAMPOLINE_INIT ia64_trampoline_init
623 #undef TARGET_CAN_USE_DOLOOP_P
624 #define TARGET_CAN_USE_DOLOOP_P can_use_doloop_if_innermost
625 #undef TARGET_INVALID_WITHIN_DOLOOP
626 #define TARGET_INVALID_WITHIN_DOLOOP hook_constcharptr_const_rtx_null
628 #undef TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE
629 #define TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE ia64_override_options_after_change
631 #undef TARGET_PREFERRED_RELOAD_CLASS
632 #define TARGET_PREFERRED_RELOAD_CLASS ia64_preferred_reload_class
634 #undef TARGET_DELAY_SCHED2
635 #define TARGET_DELAY_SCHED2 true
637 /* Variable tracking should be run after all optimizations which
638 change order of insns. It also needs a valid CFG. */
639 #undef TARGET_DELAY_VARTRACK
640 #define TARGET_DELAY_VARTRACK true
642 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
643 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK ia64_vectorize_vec_perm_const_ok
645 struct gcc_target targetm = TARGET_INITIALIZER;
647 typedef enum
649 ADDR_AREA_NORMAL, /* normal address area */
650 ADDR_AREA_SMALL /* addressable by "addl" (-2MB < addr < 2MB) */
652 ia64_addr_area;
654 static GTY(()) tree small_ident1;
655 static GTY(()) tree small_ident2;
657 static void
658 init_idents (void)
660 if (small_ident1 == 0)
662 small_ident1 = get_identifier ("small");
663 small_ident2 = get_identifier ("__small__");
667 /* Retrieve the address area that has been chosen for the given decl. */
669 static ia64_addr_area
670 ia64_get_addr_area (tree decl)
672 tree model_attr;
674 model_attr = lookup_attribute ("model", DECL_ATTRIBUTES (decl));
675 if (model_attr)
677 tree id;
679 init_idents ();
680 id = TREE_VALUE (TREE_VALUE (model_attr));
681 if (id == small_ident1 || id == small_ident2)
682 return ADDR_AREA_SMALL;
684 return ADDR_AREA_NORMAL;
687 static tree
688 ia64_handle_model_attribute (tree *node, tree name, tree args,
689 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
691 ia64_addr_area addr_area = ADDR_AREA_NORMAL;
692 ia64_addr_area area;
693 tree arg, decl = *node;
695 init_idents ();
696 arg = TREE_VALUE (args);
697 if (arg == small_ident1 || arg == small_ident2)
699 addr_area = ADDR_AREA_SMALL;
701 else
703 warning (OPT_Wattributes, "invalid argument of %qE attribute",
704 name);
705 *no_add_attrs = true;
708 switch (TREE_CODE (decl))
710 case VAR_DECL:
711 if ((DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl))
712 == FUNCTION_DECL)
713 && !TREE_STATIC (decl))
715 error_at (DECL_SOURCE_LOCATION (decl),
716 "an address area attribute cannot be specified for "
717 "local variables");
718 *no_add_attrs = true;
720 area = ia64_get_addr_area (decl);
721 if (area != ADDR_AREA_NORMAL && addr_area != area)
723 error ("address area of %q+D conflicts with previous "
724 "declaration", decl);
725 *no_add_attrs = true;
727 break;
729 case FUNCTION_DECL:
730 error_at (DECL_SOURCE_LOCATION (decl),
731 "address area attribute cannot be specified for "
732 "functions");
733 *no_add_attrs = true;
734 break;
736 default:
737 warning (OPT_Wattributes, "%qE attribute ignored",
738 name);
739 *no_add_attrs = true;
740 break;
743 return NULL_TREE;
746 /* Part of the low level implementation of DEC Ada pragma Common_Object which
747 enables the shared use of variables stored in overlaid linker areas
748 corresponding to the use of Fortran COMMON. */
750 static tree
751 ia64_vms_common_object_attribute (tree *node, tree name, tree args,
752 int flags ATTRIBUTE_UNUSED,
753 bool *no_add_attrs)
755 tree decl = *node;
756 tree id;
758 gcc_assert (DECL_P (decl));
760 DECL_COMMON (decl) = 1;
761 id = TREE_VALUE (args);
762 if (TREE_CODE (id) != IDENTIFIER_NODE && TREE_CODE (id) != STRING_CST)
764 error ("%qE attribute requires a string constant argument", name);
765 *no_add_attrs = true;
766 return NULL_TREE;
768 return NULL_TREE;
771 /* Part of the low level implementation of DEC Ada pragma Common_Object. */
773 void
774 ia64_vms_output_aligned_decl_common (FILE *file, tree decl, const char *name,
775 unsigned HOST_WIDE_INT size,
776 unsigned int align)
778 tree attr = DECL_ATTRIBUTES (decl);
780 if (attr)
781 attr = lookup_attribute ("common_object", attr);
782 if (attr)
784 tree id = TREE_VALUE (TREE_VALUE (attr));
785 const char *name;
787 if (TREE_CODE (id) == IDENTIFIER_NODE)
788 name = IDENTIFIER_POINTER (id);
789 else if (TREE_CODE (id) == STRING_CST)
790 name = TREE_STRING_POINTER (id);
791 else
792 abort ();
794 fprintf (file, "\t.vms_common\t\"%s\",", name);
796 else
797 fprintf (file, "%s", COMMON_ASM_OP);
799 /* Code from elfos.h. */
800 assemble_name (file, name);
801 fprintf (file, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u",
802 size, align / BITS_PER_UNIT);
804 fputc ('\n', file);
807 static void
808 ia64_encode_addr_area (tree decl, rtx symbol)
810 int flags;
812 flags = SYMBOL_REF_FLAGS (symbol);
813 switch (ia64_get_addr_area (decl))
815 case ADDR_AREA_NORMAL: break;
816 case ADDR_AREA_SMALL: flags |= SYMBOL_FLAG_SMALL_ADDR; break;
817 default: gcc_unreachable ();
819 SYMBOL_REF_FLAGS (symbol) = flags;
822 static void
823 ia64_encode_section_info (tree decl, rtx rtl, int first)
825 default_encode_section_info (decl, rtl, first);
827 /* Careful not to prod global register variables. */
828 if (TREE_CODE (decl) == VAR_DECL
829 && GET_CODE (DECL_RTL (decl)) == MEM
830 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF
831 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
832 ia64_encode_addr_area (decl, XEXP (rtl, 0));
835 /* Return 1 if the operands of a move are ok. */
838 ia64_move_ok (rtx dst, rtx src)
840 /* If we're under init_recog_no_volatile, we'll not be able to use
841 memory_operand. So check the code directly and don't worry about
842 the validity of the underlying address, which should have been
843 checked elsewhere anyway. */
844 if (GET_CODE (dst) != MEM)
845 return 1;
846 if (GET_CODE (src) == MEM)
847 return 0;
848 if (register_operand (src, VOIDmode))
849 return 1;
851 /* Otherwise, this must be a constant, and that either 0 or 0.0 or 1.0. */
852 if (INTEGRAL_MODE_P (GET_MODE (dst)))
853 return src == const0_rtx;
854 else
855 return satisfies_constraint_G (src);
858 /* Return 1 if the operands are ok for a floating point load pair. */
861 ia64_load_pair_ok (rtx dst, rtx src)
863 /* ??? There is a thinko in the implementation of the "x" constraint and the
864 FP_REGS class. The constraint will also reject (reg f30:TI) so we must
865 also return false for it. */
866 if (GET_CODE (dst) != REG
867 || !(FP_REGNO_P (REGNO (dst)) && FP_REGNO_P (REGNO (dst) + 1)))
868 return 0;
869 if (GET_CODE (src) != MEM || MEM_VOLATILE_P (src))
870 return 0;
871 switch (GET_CODE (XEXP (src, 0)))
873 case REG:
874 case POST_INC:
875 break;
876 case POST_DEC:
877 return 0;
878 case POST_MODIFY:
880 rtx adjust = XEXP (XEXP (XEXP (src, 0), 1), 1);
882 if (GET_CODE (adjust) != CONST_INT
883 || INTVAL (adjust) != GET_MODE_SIZE (GET_MODE (src)))
884 return 0;
886 break;
887 default:
888 abort ();
890 return 1;
894 addp4_optimize_ok (rtx op1, rtx op2)
896 return (basereg_operand (op1, GET_MODE(op1)) !=
897 basereg_operand (op2, GET_MODE(op2)));
900 /* Check if OP is a mask suitable for use with SHIFT in a dep.z instruction.
901 Return the length of the field, or <= 0 on failure. */
904 ia64_depz_field_mask (rtx rop, rtx rshift)
906 unsigned HOST_WIDE_INT op = INTVAL (rop);
907 unsigned HOST_WIDE_INT shift = INTVAL (rshift);
909 /* Get rid of the zero bits we're shifting in. */
910 op >>= shift;
912 /* We must now have a solid block of 1's at bit 0. */
913 return exact_log2 (op + 1);
916 /* Return the TLS model to use for ADDR. */
918 static enum tls_model
919 tls_symbolic_operand_type (rtx addr)
921 enum tls_model tls_kind = TLS_MODEL_NONE;
923 if (GET_CODE (addr) == CONST)
925 if (GET_CODE (XEXP (addr, 0)) == PLUS
926 && GET_CODE (XEXP (XEXP (addr, 0), 0)) == SYMBOL_REF)
927 tls_kind = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (addr, 0), 0));
929 else if (GET_CODE (addr) == SYMBOL_REF)
930 tls_kind = SYMBOL_REF_TLS_MODEL (addr);
932 return tls_kind;
935 /* Returns true if REG (assumed to be a `reg' RTX) is valid for use
936 as a base register. */
938 static inline bool
939 ia64_reg_ok_for_base_p (const_rtx reg, bool strict)
941 if (strict
942 && REGNO_OK_FOR_BASE_P (REGNO (reg)))
943 return true;
944 else if (!strict
945 && (GENERAL_REGNO_P (REGNO (reg))
946 || !HARD_REGISTER_P (reg)))
947 return true;
948 else
949 return false;
952 static bool
953 ia64_legitimate_address_reg (const_rtx reg, bool strict)
955 if ((REG_P (reg) && ia64_reg_ok_for_base_p (reg, strict))
956 || (GET_CODE (reg) == SUBREG && REG_P (XEXP (reg, 0))
957 && ia64_reg_ok_for_base_p (XEXP (reg, 0), strict)))
958 return true;
960 return false;
963 static bool
964 ia64_legitimate_address_disp (const_rtx reg, const_rtx disp, bool strict)
966 if (GET_CODE (disp) == PLUS
967 && rtx_equal_p (reg, XEXP (disp, 0))
968 && (ia64_legitimate_address_reg (XEXP (disp, 1), strict)
969 || (CONST_INT_P (XEXP (disp, 1))
970 && IN_RANGE (INTVAL (XEXP (disp, 1)), -256, 255))))
971 return true;
973 return false;
976 /* Implement TARGET_LEGITIMATE_ADDRESS_P. */
978 static bool
979 ia64_legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
980 rtx x, bool strict)
982 if (ia64_legitimate_address_reg (x, strict))
983 return true;
984 else if ((GET_CODE (x) == POST_INC || GET_CODE (x) == POST_DEC)
985 && ia64_legitimate_address_reg (XEXP (x, 0), strict)
986 && XEXP (x, 0) != arg_pointer_rtx)
987 return true;
988 else if (GET_CODE (x) == POST_MODIFY
989 && ia64_legitimate_address_reg (XEXP (x, 0), strict)
990 && XEXP (x, 0) != arg_pointer_rtx
991 && ia64_legitimate_address_disp (XEXP (x, 0), XEXP (x, 1), strict))
992 return true;
993 else
994 return false;
997 /* Return true if X is a constant that is valid for some immediate
998 field in an instruction. */
1000 static bool
1001 ia64_legitimate_constant_p (enum machine_mode mode, rtx x)
1003 switch (GET_CODE (x))
1005 case CONST_INT:
1006 case LABEL_REF:
1007 return true;
1009 case CONST_DOUBLE:
1010 if (GET_MODE (x) == VOIDmode || mode == SFmode || mode == DFmode)
1011 return true;
1012 return satisfies_constraint_G (x);
1014 case CONST:
1015 case SYMBOL_REF:
1016 /* ??? Short term workaround for PR 28490. We must make the code here
1017 match the code in ia64_expand_move and move_operand, even though they
1018 are both technically wrong. */
1019 if (tls_symbolic_operand_type (x) == 0)
1021 HOST_WIDE_INT addend = 0;
1022 rtx op = x;
1024 if (GET_CODE (op) == CONST
1025 && GET_CODE (XEXP (op, 0)) == PLUS
1026 && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT)
1028 addend = INTVAL (XEXP (XEXP (op, 0), 1));
1029 op = XEXP (XEXP (op, 0), 0);
1032 if (any_offset_symbol_operand (op, mode)
1033 || function_operand (op, mode))
1034 return true;
1035 if (aligned_offset_symbol_operand (op, mode))
1036 return (addend & 0x3fff) == 0;
1037 return false;
1039 return false;
1041 case CONST_VECTOR:
1042 if (mode == V2SFmode)
1043 return satisfies_constraint_Y (x);
1045 return (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
1046 && GET_MODE_SIZE (mode) <= 8);
1048 default:
1049 return false;
1053 /* Don't allow TLS addresses to get spilled to memory. */
1055 static bool
1056 ia64_cannot_force_const_mem (enum machine_mode mode, rtx x)
1058 if (mode == RFmode)
1059 return true;
1060 return tls_symbolic_operand_type (x) != 0;
1063 /* Expand a symbolic constant load. */
1065 bool
1066 ia64_expand_load_address (rtx dest, rtx src)
1068 gcc_assert (GET_CODE (dest) == REG);
1070 /* ILP32 mode still loads 64-bits of data from the GOT. This avoids
1071 having to pointer-extend the value afterward. Other forms of address
1072 computation below are also more natural to compute as 64-bit quantities.
1073 If we've been given an SImode destination register, change it. */
1074 if (GET_MODE (dest) != Pmode)
1075 dest = gen_rtx_REG_offset (dest, Pmode, REGNO (dest),
1076 byte_lowpart_offset (Pmode, GET_MODE (dest)));
1078 if (TARGET_NO_PIC)
1079 return false;
1080 if (small_addr_symbolic_operand (src, VOIDmode))
1081 return false;
1083 if (TARGET_AUTO_PIC)
1084 emit_insn (gen_load_gprel64 (dest, src));
1085 else if (GET_CODE (src) == SYMBOL_REF && SYMBOL_REF_FUNCTION_P (src))
1086 emit_insn (gen_load_fptr (dest, src));
1087 else if (sdata_symbolic_operand (src, VOIDmode))
1088 emit_insn (gen_load_gprel (dest, src));
1089 else
1091 HOST_WIDE_INT addend = 0;
1092 rtx tmp;
1094 /* We did split constant offsets in ia64_expand_move, and we did try
1095 to keep them split in move_operand, but we also allowed reload to
1096 rematerialize arbitrary constants rather than spill the value to
1097 the stack and reload it. So we have to be prepared here to split
1098 them apart again. */
1099 if (GET_CODE (src) == CONST)
1101 HOST_WIDE_INT hi, lo;
1103 hi = INTVAL (XEXP (XEXP (src, 0), 1));
1104 lo = ((hi & 0x3fff) ^ 0x2000) - 0x2000;
1105 hi = hi - lo;
1107 if (lo != 0)
1109 addend = lo;
1110 src = plus_constant (Pmode, XEXP (XEXP (src, 0), 0), hi);
1114 tmp = gen_rtx_HIGH (Pmode, src);
1115 tmp = gen_rtx_PLUS (Pmode, tmp, pic_offset_table_rtx);
1116 emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
1118 tmp = gen_rtx_LO_SUM (Pmode, gen_const_mem (Pmode, dest), src);
1119 emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
1121 if (addend)
1123 tmp = gen_rtx_PLUS (Pmode, dest, GEN_INT (addend));
1124 emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
1128 return true;
1131 static GTY(()) rtx gen_tls_tga;
1132 static rtx
1133 gen_tls_get_addr (void)
1135 if (!gen_tls_tga)
1136 gen_tls_tga = init_one_libfunc ("__tls_get_addr");
1137 return gen_tls_tga;
1140 static GTY(()) rtx thread_pointer_rtx;
1141 static rtx
1142 gen_thread_pointer (void)
1144 if (!thread_pointer_rtx)
1145 thread_pointer_rtx = gen_rtx_REG (Pmode, 13);
1146 return thread_pointer_rtx;
1149 static rtx
1150 ia64_expand_tls_address (enum tls_model tls_kind, rtx op0, rtx op1,
1151 rtx orig_op1, HOST_WIDE_INT addend)
1153 rtx tga_op1, tga_op2, tga_ret, tga_eqv, tmp, insns;
1154 rtx orig_op0 = op0;
1155 HOST_WIDE_INT addend_lo, addend_hi;
1157 switch (tls_kind)
1159 case TLS_MODEL_GLOBAL_DYNAMIC:
1160 start_sequence ();
1162 tga_op1 = gen_reg_rtx (Pmode);
1163 emit_insn (gen_load_dtpmod (tga_op1, op1));
1165 tga_op2 = gen_reg_rtx (Pmode);
1166 emit_insn (gen_load_dtprel (tga_op2, op1));
1168 tga_ret = emit_library_call_value (gen_tls_get_addr (), NULL_RTX,
1169 LCT_CONST, Pmode, 2, tga_op1,
1170 Pmode, tga_op2, Pmode);
1172 insns = get_insns ();
1173 end_sequence ();
1175 if (GET_MODE (op0) != Pmode)
1176 op0 = tga_ret;
1177 emit_libcall_block (insns, op0, tga_ret, op1);
1178 break;
1180 case TLS_MODEL_LOCAL_DYNAMIC:
1181 /* ??? This isn't the completely proper way to do local-dynamic
1182 If the call to __tls_get_addr is used only by a single symbol,
1183 then we should (somehow) move the dtprel to the second arg
1184 to avoid the extra add. */
1185 start_sequence ();
1187 tga_op1 = gen_reg_rtx (Pmode);
1188 emit_insn (gen_load_dtpmod (tga_op1, op1));
1190 tga_op2 = const0_rtx;
1192 tga_ret = emit_library_call_value (gen_tls_get_addr (), NULL_RTX,
1193 LCT_CONST, Pmode, 2, tga_op1,
1194 Pmode, tga_op2, Pmode);
1196 insns = get_insns ();
1197 end_sequence ();
1199 tga_eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
1200 UNSPEC_LD_BASE);
1201 tmp = gen_reg_rtx (Pmode);
1202 emit_libcall_block (insns, tmp, tga_ret, tga_eqv);
1204 if (!register_operand (op0, Pmode))
1205 op0 = gen_reg_rtx (Pmode);
1206 if (TARGET_TLS64)
1208 emit_insn (gen_load_dtprel (op0, op1));
1209 emit_insn (gen_adddi3 (op0, tmp, op0));
1211 else
1212 emit_insn (gen_add_dtprel (op0, op1, tmp));
1213 break;
1215 case TLS_MODEL_INITIAL_EXEC:
1216 addend_lo = ((addend & 0x3fff) ^ 0x2000) - 0x2000;
1217 addend_hi = addend - addend_lo;
1219 op1 = plus_constant (Pmode, op1, addend_hi);
1220 addend = addend_lo;
1222 tmp = gen_reg_rtx (Pmode);
1223 emit_insn (gen_load_tprel (tmp, op1));
1225 if (!register_operand (op0, Pmode))
1226 op0 = gen_reg_rtx (Pmode);
1227 emit_insn (gen_adddi3 (op0, tmp, gen_thread_pointer ()));
1228 break;
1230 case TLS_MODEL_LOCAL_EXEC:
1231 if (!register_operand (op0, Pmode))
1232 op0 = gen_reg_rtx (Pmode);
1234 op1 = orig_op1;
1235 addend = 0;
1236 if (TARGET_TLS64)
1238 emit_insn (gen_load_tprel (op0, op1));
1239 emit_insn (gen_adddi3 (op0, op0, gen_thread_pointer ()));
1241 else
1242 emit_insn (gen_add_tprel (op0, op1, gen_thread_pointer ()));
1243 break;
1245 default:
1246 gcc_unreachable ();
1249 if (addend)
1250 op0 = expand_simple_binop (Pmode, PLUS, op0, GEN_INT (addend),
1251 orig_op0, 1, OPTAB_DIRECT);
1252 if (orig_op0 == op0)
1253 return NULL_RTX;
1254 if (GET_MODE (orig_op0) == Pmode)
1255 return op0;
1256 return gen_lowpart (GET_MODE (orig_op0), op0);
1260 ia64_expand_move (rtx op0, rtx op1)
1262 enum machine_mode mode = GET_MODE (op0);
1264 if (!reload_in_progress && !reload_completed && !ia64_move_ok (op0, op1))
1265 op1 = force_reg (mode, op1);
1267 if ((mode == Pmode || mode == ptr_mode) && symbolic_operand (op1, VOIDmode))
1269 HOST_WIDE_INT addend = 0;
1270 enum tls_model tls_kind;
1271 rtx sym = op1;
1273 if (GET_CODE (op1) == CONST
1274 && GET_CODE (XEXP (op1, 0)) == PLUS
1275 && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT)
1277 addend = INTVAL (XEXP (XEXP (op1, 0), 1));
1278 sym = XEXP (XEXP (op1, 0), 0);
1281 tls_kind = tls_symbolic_operand_type (sym);
1282 if (tls_kind)
1283 return ia64_expand_tls_address (tls_kind, op0, sym, op1, addend);
1285 if (any_offset_symbol_operand (sym, mode))
1286 addend = 0;
1287 else if (aligned_offset_symbol_operand (sym, mode))
1289 HOST_WIDE_INT addend_lo, addend_hi;
1291 addend_lo = ((addend & 0x3fff) ^ 0x2000) - 0x2000;
1292 addend_hi = addend - addend_lo;
1294 if (addend_lo != 0)
1296 op1 = plus_constant (mode, sym, addend_hi);
1297 addend = addend_lo;
1299 else
1300 addend = 0;
1302 else
1303 op1 = sym;
1305 if (reload_completed)
1307 /* We really should have taken care of this offset earlier. */
1308 gcc_assert (addend == 0);
1309 if (ia64_expand_load_address (op0, op1))
1310 return NULL_RTX;
1313 if (addend)
1315 rtx subtarget = !can_create_pseudo_p () ? op0 : gen_reg_rtx (mode);
1317 emit_insn (gen_rtx_SET (VOIDmode, subtarget, op1));
1319 op1 = expand_simple_binop (mode, PLUS, subtarget,
1320 GEN_INT (addend), op0, 1, OPTAB_DIRECT);
1321 if (op0 == op1)
1322 return NULL_RTX;
1326 return op1;
1329 /* Split a move from OP1 to OP0 conditional on COND. */
1331 void
1332 ia64_emit_cond_move (rtx op0, rtx op1, rtx cond)
1334 rtx insn, first = get_last_insn ();
1336 emit_move_insn (op0, op1);
1338 for (insn = get_last_insn (); insn != first; insn = PREV_INSN (insn))
1339 if (INSN_P (insn))
1340 PATTERN (insn) = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond),
1341 PATTERN (insn));
1344 /* Split a post-reload TImode or TFmode reference into two DImode
1345 components. This is made extra difficult by the fact that we do
1346 not get any scratch registers to work with, because reload cannot
1347 be prevented from giving us a scratch that overlaps the register
1348 pair involved. So instead, when addressing memory, we tweak the
1349 pointer register up and back down with POST_INCs. Or up and not
1350 back down when we can get away with it.
1352 REVERSED is true when the loads must be done in reversed order
1353 (high word first) for correctness. DEAD is true when the pointer
1354 dies with the second insn we generate and therefore the second
1355 address must not carry a postmodify.
1357 May return an insn which is to be emitted after the moves. */
1359 static rtx
1360 ia64_split_tmode (rtx out[2], rtx in, bool reversed, bool dead)
1362 rtx fixup = 0;
1364 switch (GET_CODE (in))
1366 case REG:
1367 out[reversed] = gen_rtx_REG (DImode, REGNO (in));
1368 out[!reversed] = gen_rtx_REG (DImode, REGNO (in) + 1);
1369 break;
1371 case CONST_INT:
1372 case CONST_DOUBLE:
1373 /* Cannot occur reversed. */
1374 gcc_assert (!reversed);
1376 if (GET_MODE (in) != TFmode)
1377 split_double (in, &out[0], &out[1]);
1378 else
1379 /* split_double does not understand how to split a TFmode
1380 quantity into a pair of DImode constants. */
1382 REAL_VALUE_TYPE r;
1383 unsigned HOST_WIDE_INT p[2];
1384 long l[4]; /* TFmode is 128 bits */
1386 REAL_VALUE_FROM_CONST_DOUBLE (r, in);
1387 real_to_target (l, &r, TFmode);
1389 if (FLOAT_WORDS_BIG_ENDIAN)
1391 p[0] = (((unsigned HOST_WIDE_INT) l[0]) << 32) + l[1];
1392 p[1] = (((unsigned HOST_WIDE_INT) l[2]) << 32) + l[3];
1394 else
1396 p[0] = (((unsigned HOST_WIDE_INT) l[1]) << 32) + l[0];
1397 p[1] = (((unsigned HOST_WIDE_INT) l[3]) << 32) + l[2];
1399 out[0] = GEN_INT (p[0]);
1400 out[1] = GEN_INT (p[1]);
1402 break;
1404 case MEM:
1406 rtx base = XEXP (in, 0);
1407 rtx offset;
1409 switch (GET_CODE (base))
1411 case REG:
1412 if (!reversed)
1414 out[0] = adjust_automodify_address
1415 (in, DImode, gen_rtx_POST_INC (Pmode, base), 0);
1416 out[1] = adjust_automodify_address
1417 (in, DImode, dead ? 0 : gen_rtx_POST_DEC (Pmode, base), 8);
1419 else
1421 /* Reversal requires a pre-increment, which can only
1422 be done as a separate insn. */
1423 emit_insn (gen_adddi3 (base, base, GEN_INT (8)));
1424 out[0] = adjust_automodify_address
1425 (in, DImode, gen_rtx_POST_DEC (Pmode, base), 8);
1426 out[1] = adjust_address (in, DImode, 0);
1428 break;
1430 case POST_INC:
1431 gcc_assert (!reversed && !dead);
1433 /* Just do the increment in two steps. */
1434 out[0] = adjust_automodify_address (in, DImode, 0, 0);
1435 out[1] = adjust_automodify_address (in, DImode, 0, 8);
1436 break;
1438 case POST_DEC:
1439 gcc_assert (!reversed && !dead);
1441 /* Add 8, subtract 24. */
1442 base = XEXP (base, 0);
1443 out[0] = adjust_automodify_address
1444 (in, DImode, gen_rtx_POST_INC (Pmode, base), 0);
1445 out[1] = adjust_automodify_address
1446 (in, DImode,
1447 gen_rtx_POST_MODIFY (Pmode, base,
1448 plus_constant (Pmode, base, -24)),
1450 break;
1452 case POST_MODIFY:
1453 gcc_assert (!reversed && !dead);
1455 /* Extract and adjust the modification. This case is
1456 trickier than the others, because we might have an
1457 index register, or we might have a combined offset that
1458 doesn't fit a signed 9-bit displacement field. We can
1459 assume the incoming expression is already legitimate. */
1460 offset = XEXP (base, 1);
1461 base = XEXP (base, 0);
1463 out[0] = adjust_automodify_address
1464 (in, DImode, gen_rtx_POST_INC (Pmode, base), 0);
1466 if (GET_CODE (XEXP (offset, 1)) == REG)
1468 /* Can't adjust the postmodify to match. Emit the
1469 original, then a separate addition insn. */
1470 out[1] = adjust_automodify_address (in, DImode, 0, 8);
1471 fixup = gen_adddi3 (base, base, GEN_INT (-8));
1473 else
1475 gcc_assert (GET_CODE (XEXP (offset, 1)) == CONST_INT);
1476 if (INTVAL (XEXP (offset, 1)) < -256 + 8)
1478 /* Again the postmodify cannot be made to match,
1479 but in this case it's more efficient to get rid
1480 of the postmodify entirely and fix up with an
1481 add insn. */
1482 out[1] = adjust_automodify_address (in, DImode, base, 8);
1483 fixup = gen_adddi3
1484 (base, base, GEN_INT (INTVAL (XEXP (offset, 1)) - 8));
1486 else
1488 /* Combined offset still fits in the displacement field.
1489 (We cannot overflow it at the high end.) */
1490 out[1] = adjust_automodify_address
1491 (in, DImode, gen_rtx_POST_MODIFY
1492 (Pmode, base, gen_rtx_PLUS
1493 (Pmode, base,
1494 GEN_INT (INTVAL (XEXP (offset, 1)) - 8))),
1498 break;
1500 default:
1501 gcc_unreachable ();
1503 break;
1506 default:
1507 gcc_unreachable ();
1510 return fixup;
1513 /* Split a TImode or TFmode move instruction after reload.
1514 This is used by *movtf_internal and *movti_internal. */
1515 void
1516 ia64_split_tmode_move (rtx operands[])
1518 rtx in[2], out[2], insn;
1519 rtx fixup[2];
1520 bool dead = false;
1521 bool reversed = false;
1523 /* It is possible for reload to decide to overwrite a pointer with
1524 the value it points to. In that case we have to do the loads in
1525 the appropriate order so that the pointer is not destroyed too
1526 early. Also we must not generate a postmodify for that second
1527 load, or rws_access_regno will die. */
1528 if (GET_CODE (operands[1]) == MEM
1529 && reg_overlap_mentioned_p (operands[0], operands[1]))
1531 rtx base = XEXP (operands[1], 0);
1532 while (GET_CODE (base) != REG)
1533 base = XEXP (base, 0);
1535 if (REGNO (base) == REGNO (operands[0]))
1536 reversed = true;
1537 dead = true;
1539 /* Another reason to do the moves in reversed order is if the first
1540 element of the target register pair is also the second element of
1541 the source register pair. */
1542 if (GET_CODE (operands[0]) == REG && GET_CODE (operands[1]) == REG
1543 && REGNO (operands[0]) == REGNO (operands[1]) + 1)
1544 reversed = true;
1546 fixup[0] = ia64_split_tmode (in, operands[1], reversed, dead);
1547 fixup[1] = ia64_split_tmode (out, operands[0], reversed, dead);
1549 #define MAYBE_ADD_REG_INC_NOTE(INSN, EXP) \
1550 if (GET_CODE (EXP) == MEM \
1551 && (GET_CODE (XEXP (EXP, 0)) == POST_MODIFY \
1552 || GET_CODE (XEXP (EXP, 0)) == POST_INC \
1553 || GET_CODE (XEXP (EXP, 0)) == POST_DEC)) \
1554 add_reg_note (insn, REG_INC, XEXP (XEXP (EXP, 0), 0))
1556 insn = emit_insn (gen_rtx_SET (VOIDmode, out[0], in[0]));
1557 MAYBE_ADD_REG_INC_NOTE (insn, in[0]);
1558 MAYBE_ADD_REG_INC_NOTE (insn, out[0]);
1560 insn = emit_insn (gen_rtx_SET (VOIDmode, out[1], in[1]));
1561 MAYBE_ADD_REG_INC_NOTE (insn, in[1]);
1562 MAYBE_ADD_REG_INC_NOTE (insn, out[1]);
1564 if (fixup[0])
1565 emit_insn (fixup[0]);
1566 if (fixup[1])
1567 emit_insn (fixup[1]);
1569 #undef MAYBE_ADD_REG_INC_NOTE
1572 /* ??? Fixing GR->FR XFmode moves during reload is hard. You need to go
1573 through memory plus an extra GR scratch register. Except that you can
1574 either get the first from SECONDARY_MEMORY_NEEDED or the second from
1575 SECONDARY_RELOAD_CLASS, but not both.
1577 We got into problems in the first place by allowing a construct like
1578 (subreg:XF (reg:TI)), which we got from a union containing a long double.
1579 This solution attempts to prevent this situation from occurring. When
1580 we see something like the above, we spill the inner register to memory. */
1582 static rtx
1583 spill_xfmode_rfmode_operand (rtx in, int force, enum machine_mode mode)
1585 if (GET_CODE (in) == SUBREG
1586 && GET_MODE (SUBREG_REG (in)) == TImode
1587 && GET_CODE (SUBREG_REG (in)) == REG)
1589 rtx memt = assign_stack_temp (TImode, 16);
1590 emit_move_insn (memt, SUBREG_REG (in));
1591 return adjust_address (memt, mode, 0);
1593 else if (force && GET_CODE (in) == REG)
1595 rtx memx = assign_stack_temp (mode, 16);
1596 emit_move_insn (memx, in);
1597 return memx;
1599 else
1600 return in;
1603 /* Expand the movxf or movrf pattern (MODE says which) with the given
1604 OPERANDS, returning true if the pattern should then invoke
1605 DONE. */
1607 bool
1608 ia64_expand_movxf_movrf (enum machine_mode mode, rtx operands[])
1610 rtx op0 = operands[0];
1612 if (GET_CODE (op0) == SUBREG)
1613 op0 = SUBREG_REG (op0);
1615 /* We must support XFmode loads into general registers for stdarg/vararg,
1616 unprototyped calls, and a rare case where a long double is passed as
1617 an argument after a float HFA fills the FP registers. We split them into
1618 DImode loads for convenience. We also need to support XFmode stores
1619 for the last case. This case does not happen for stdarg/vararg routines,
1620 because we do a block store to memory of unnamed arguments. */
1622 if (GET_CODE (op0) == REG && GR_REGNO_P (REGNO (op0)))
1624 rtx out[2];
1626 /* We're hoping to transform everything that deals with XFmode
1627 quantities and GR registers early in the compiler. */
1628 gcc_assert (can_create_pseudo_p ());
1630 /* Struct to register can just use TImode instead. */
1631 if ((GET_CODE (operands[1]) == SUBREG
1632 && GET_MODE (SUBREG_REG (operands[1])) == TImode)
1633 || (GET_CODE (operands[1]) == REG
1634 && GR_REGNO_P (REGNO (operands[1]))))
1636 rtx op1 = operands[1];
1638 if (GET_CODE (op1) == SUBREG)
1639 op1 = SUBREG_REG (op1);
1640 else
1641 op1 = gen_rtx_REG (TImode, REGNO (op1));
1643 emit_move_insn (gen_rtx_REG (TImode, REGNO (op0)), op1);
1644 return true;
1647 if (GET_CODE (operands[1]) == CONST_DOUBLE)
1649 /* Don't word-swap when reading in the constant. */
1650 emit_move_insn (gen_rtx_REG (DImode, REGNO (op0)),
1651 operand_subword (operands[1], WORDS_BIG_ENDIAN,
1652 0, mode));
1653 emit_move_insn (gen_rtx_REG (DImode, REGNO (op0) + 1),
1654 operand_subword (operands[1], !WORDS_BIG_ENDIAN,
1655 0, mode));
1656 return true;
1659 /* If the quantity is in a register not known to be GR, spill it. */
1660 if (register_operand (operands[1], mode))
1661 operands[1] = spill_xfmode_rfmode_operand (operands[1], 1, mode);
1663 gcc_assert (GET_CODE (operands[1]) == MEM);
1665 /* Don't word-swap when reading in the value. */
1666 out[0] = gen_rtx_REG (DImode, REGNO (op0));
1667 out[1] = gen_rtx_REG (DImode, REGNO (op0) + 1);
1669 emit_move_insn (out[0], adjust_address (operands[1], DImode, 0));
1670 emit_move_insn (out[1], adjust_address (operands[1], DImode, 8));
1671 return true;
1674 if (GET_CODE (operands[1]) == REG && GR_REGNO_P (REGNO (operands[1])))
1676 /* We're hoping to transform everything that deals with XFmode
1677 quantities and GR registers early in the compiler. */
1678 gcc_assert (can_create_pseudo_p ());
1680 /* Op0 can't be a GR_REG here, as that case is handled above.
1681 If op0 is a register, then we spill op1, so that we now have a
1682 MEM operand. This requires creating an XFmode subreg of a TImode reg
1683 to force the spill. */
1684 if (register_operand (operands[0], mode))
1686 rtx op1 = gen_rtx_REG (TImode, REGNO (operands[1]));
1687 op1 = gen_rtx_SUBREG (mode, op1, 0);
1688 operands[1] = spill_xfmode_rfmode_operand (op1, 0, mode);
1691 else
1693 rtx in[2];
1695 gcc_assert (GET_CODE (operands[0]) == MEM);
1697 /* Don't word-swap when writing out the value. */
1698 in[0] = gen_rtx_REG (DImode, REGNO (operands[1]));
1699 in[1] = gen_rtx_REG (DImode, REGNO (operands[1]) + 1);
1701 emit_move_insn (adjust_address (operands[0], DImode, 0), in[0]);
1702 emit_move_insn (adjust_address (operands[0], DImode, 8), in[1]);
1703 return true;
1707 if (!reload_in_progress && !reload_completed)
1709 operands[1] = spill_xfmode_rfmode_operand (operands[1], 0, mode);
1711 if (GET_MODE (op0) == TImode && GET_CODE (op0) == REG)
1713 rtx memt, memx, in = operands[1];
1714 if (CONSTANT_P (in))
1715 in = validize_mem (force_const_mem (mode, in));
1716 if (GET_CODE (in) == MEM)
1717 memt = adjust_address (in, TImode, 0);
1718 else
1720 memt = assign_stack_temp (TImode, 16);
1721 memx = adjust_address (memt, mode, 0);
1722 emit_move_insn (memx, in);
1724 emit_move_insn (op0, memt);
1725 return true;
1728 if (!ia64_move_ok (operands[0], operands[1]))
1729 operands[1] = force_reg (mode, operands[1]);
1732 return false;
1735 /* Emit comparison instruction if necessary, replacing *EXPR, *OP0, *OP1
1736 with the expression that holds the compare result (in VOIDmode). */
1738 static GTY(()) rtx cmptf_libfunc;
1740 void
1741 ia64_expand_compare (rtx *expr, rtx *op0, rtx *op1)
1743 enum rtx_code code = GET_CODE (*expr);
1744 rtx cmp;
1746 /* If we have a BImode input, then we already have a compare result, and
1747 do not need to emit another comparison. */
1748 if (GET_MODE (*op0) == BImode)
1750 gcc_assert ((code == NE || code == EQ) && *op1 == const0_rtx);
1751 cmp = *op0;
1753 /* HPUX TFmode compare requires a library call to _U_Qfcmp, which takes a
1754 magic number as its third argument, that indicates what to do.
1755 The return value is an integer to be compared against zero. */
1756 else if (TARGET_HPUX && GET_MODE (*op0) == TFmode)
1758 enum qfcmp_magic {
1759 QCMP_INV = 1, /* Raise FP_INVALID on NaNs as a side effect. */
1760 QCMP_UNORD = 2,
1761 QCMP_EQ = 4,
1762 QCMP_LT = 8,
1763 QCMP_GT = 16
1765 int magic;
1766 enum rtx_code ncode;
1767 rtx ret, insns;
1769 gcc_assert (cmptf_libfunc && GET_MODE (*op1) == TFmode);
1770 switch (code)
1772 /* 1 = equal, 0 = not equal. Equality operators do
1773 not raise FP_INVALID when given a NaN operand. */
1774 case EQ: magic = QCMP_EQ; ncode = NE; break;
1775 case NE: magic = QCMP_EQ; ncode = EQ; break;
1776 /* isunordered() from C99. */
1777 case UNORDERED: magic = QCMP_UNORD; ncode = NE; break;
1778 case ORDERED: magic = QCMP_UNORD; ncode = EQ; break;
1779 /* Relational operators raise FP_INVALID when given
1780 a NaN operand. */
1781 case LT: magic = QCMP_LT |QCMP_INV; ncode = NE; break;
1782 case LE: magic = QCMP_LT|QCMP_EQ|QCMP_INV; ncode = NE; break;
1783 case GT: magic = QCMP_GT |QCMP_INV; ncode = NE; break;
1784 case GE: magic = QCMP_GT|QCMP_EQ|QCMP_INV; ncode = NE; break;
1785 /* Unordered relational operators do not raise FP_INVALID
1786 when given a NaN operand. */
1787 case UNLT: magic = QCMP_LT |QCMP_UNORD; ncode = NE; break;
1788 case UNLE: magic = QCMP_LT|QCMP_EQ|QCMP_UNORD; ncode = NE; break;
1789 case UNGT: magic = QCMP_GT |QCMP_UNORD; ncode = NE; break;
1790 case UNGE: magic = QCMP_GT|QCMP_EQ|QCMP_UNORD; ncode = NE; break;
1791 /* Not supported. */
1792 case UNEQ:
1793 case LTGT:
1794 default: gcc_unreachable ();
1797 start_sequence ();
1799 ret = emit_library_call_value (cmptf_libfunc, 0, LCT_CONST, DImode, 3,
1800 *op0, TFmode, *op1, TFmode,
1801 GEN_INT (magic), DImode);
1802 cmp = gen_reg_rtx (BImode);
1803 emit_insn (gen_rtx_SET (VOIDmode, cmp,
1804 gen_rtx_fmt_ee (ncode, BImode,
1805 ret, const0_rtx)));
1807 insns = get_insns ();
1808 end_sequence ();
1810 emit_libcall_block (insns, cmp, cmp,
1811 gen_rtx_fmt_ee (code, BImode, *op0, *op1));
1812 code = NE;
1814 else
1816 cmp = gen_reg_rtx (BImode);
1817 emit_insn (gen_rtx_SET (VOIDmode, cmp,
1818 gen_rtx_fmt_ee (code, BImode, *op0, *op1)));
1819 code = NE;
1822 *expr = gen_rtx_fmt_ee (code, VOIDmode, cmp, const0_rtx);
1823 *op0 = cmp;
1824 *op1 = const0_rtx;
1827 /* Generate an integral vector comparison. Return true if the condition has
1828 been reversed, and so the sense of the comparison should be inverted. */
1830 static bool
1831 ia64_expand_vecint_compare (enum rtx_code code, enum machine_mode mode,
1832 rtx dest, rtx op0, rtx op1)
1834 bool negate = false;
1835 rtx x;
1837 /* Canonicalize the comparison to EQ, GT, GTU. */
1838 switch (code)
1840 case EQ:
1841 case GT:
1842 case GTU:
1843 break;
1845 case NE:
1846 case LE:
1847 case LEU:
1848 code = reverse_condition (code);
1849 negate = true;
1850 break;
1852 case GE:
1853 case GEU:
1854 code = reverse_condition (code);
1855 negate = true;
1856 /* FALLTHRU */
1858 case LT:
1859 case LTU:
1860 code = swap_condition (code);
1861 x = op0, op0 = op1, op1 = x;
1862 break;
1864 default:
1865 gcc_unreachable ();
1868 /* Unsigned parallel compare is not supported by the hardware. Play some
1869 tricks to turn this into a signed comparison against 0. */
1870 if (code == GTU)
1872 switch (mode)
1874 case V2SImode:
1876 rtx t1, t2, mask;
1878 /* Subtract (-(INT MAX) - 1) from both operands to make
1879 them signed. */
1880 mask = GEN_INT (0x80000000);
1881 mask = gen_rtx_CONST_VECTOR (V2SImode, gen_rtvec (2, mask, mask));
1882 mask = force_reg (mode, mask);
1883 t1 = gen_reg_rtx (mode);
1884 emit_insn (gen_subv2si3 (t1, op0, mask));
1885 t2 = gen_reg_rtx (mode);
1886 emit_insn (gen_subv2si3 (t2, op1, mask));
1887 op0 = t1;
1888 op1 = t2;
1889 code = GT;
1891 break;
1893 case V8QImode:
1894 case V4HImode:
1895 /* Perform a parallel unsigned saturating subtraction. */
1896 x = gen_reg_rtx (mode);
1897 emit_insn (gen_rtx_SET (VOIDmode, x,
1898 gen_rtx_US_MINUS (mode, op0, op1)));
1900 code = EQ;
1901 op0 = x;
1902 op1 = CONST0_RTX (mode);
1903 negate = !negate;
1904 break;
1906 default:
1907 gcc_unreachable ();
1911 x = gen_rtx_fmt_ee (code, mode, op0, op1);
1912 emit_insn (gen_rtx_SET (VOIDmode, dest, x));
1914 return negate;
1917 /* Emit an integral vector conditional move. */
1919 void
1920 ia64_expand_vecint_cmov (rtx operands[])
1922 enum machine_mode mode = GET_MODE (operands[0]);
1923 enum rtx_code code = GET_CODE (operands[3]);
1924 bool negate;
1925 rtx cmp, x, ot, of;
1927 cmp = gen_reg_rtx (mode);
1928 negate = ia64_expand_vecint_compare (code, mode, cmp,
1929 operands[4], operands[5]);
1931 ot = operands[1+negate];
1932 of = operands[2-negate];
1934 if (ot == CONST0_RTX (mode))
1936 if (of == CONST0_RTX (mode))
1938 emit_move_insn (operands[0], ot);
1939 return;
1942 x = gen_rtx_NOT (mode, cmp);
1943 x = gen_rtx_AND (mode, x, of);
1944 emit_insn (gen_rtx_SET (VOIDmode, operands[0], x));
1946 else if (of == CONST0_RTX (mode))
1948 x = gen_rtx_AND (mode, cmp, ot);
1949 emit_insn (gen_rtx_SET (VOIDmode, operands[0], x));
1951 else
1953 rtx t, f;
1955 t = gen_reg_rtx (mode);
1956 x = gen_rtx_AND (mode, cmp, operands[1+negate]);
1957 emit_insn (gen_rtx_SET (VOIDmode, t, x));
1959 f = gen_reg_rtx (mode);
1960 x = gen_rtx_NOT (mode, cmp);
1961 x = gen_rtx_AND (mode, x, operands[2-negate]);
1962 emit_insn (gen_rtx_SET (VOIDmode, f, x));
1964 x = gen_rtx_IOR (mode, t, f);
1965 emit_insn (gen_rtx_SET (VOIDmode, operands[0], x));
1969 /* Emit an integral vector min or max operation. Return true if all done. */
1971 bool
1972 ia64_expand_vecint_minmax (enum rtx_code code, enum machine_mode mode,
1973 rtx operands[])
1975 rtx xops[6];
1977 /* These four combinations are supported directly. */
1978 if (mode == V8QImode && (code == UMIN || code == UMAX))
1979 return false;
1980 if (mode == V4HImode && (code == SMIN || code == SMAX))
1981 return false;
1983 /* This combination can be implemented with only saturating subtraction. */
1984 if (mode == V4HImode && code == UMAX)
1986 rtx x, tmp = gen_reg_rtx (mode);
1988 x = gen_rtx_US_MINUS (mode, operands[1], operands[2]);
1989 emit_insn (gen_rtx_SET (VOIDmode, tmp, x));
1991 emit_insn (gen_addv4hi3 (operands[0], tmp, operands[2]));
1992 return true;
1995 /* Everything else implemented via vector comparisons. */
1996 xops[0] = operands[0];
1997 xops[4] = xops[1] = operands[1];
1998 xops[5] = xops[2] = operands[2];
2000 switch (code)
2002 case UMIN:
2003 code = LTU;
2004 break;
2005 case UMAX:
2006 code = GTU;
2007 break;
2008 case SMIN:
2009 code = LT;
2010 break;
2011 case SMAX:
2012 code = GT;
2013 break;
2014 default:
2015 gcc_unreachable ();
2017 xops[3] = gen_rtx_fmt_ee (code, VOIDmode, operands[1], operands[2]);
2019 ia64_expand_vecint_cmov (xops);
2020 return true;
2023 /* The vectors LO and HI each contain N halves of a double-wide vector.
2024 Reassemble either the first N/2 or the second N/2 elements. */
2026 void
2027 ia64_unpack_assemble (rtx out, rtx lo, rtx hi, bool highp)
2029 enum machine_mode vmode = GET_MODE (lo);
2030 unsigned int i, high, nelt = GET_MODE_NUNITS (vmode);
2031 struct expand_vec_perm_d d;
2032 bool ok;
2034 d.target = gen_lowpart (vmode, out);
2035 d.op0 = (TARGET_BIG_ENDIAN ? hi : lo);
2036 d.op1 = (TARGET_BIG_ENDIAN ? lo : hi);
2037 d.vmode = vmode;
2038 d.nelt = nelt;
2039 d.one_operand_p = false;
2040 d.testing_p = false;
2042 high = (highp ? nelt / 2 : 0);
2043 for (i = 0; i < nelt / 2; ++i)
2045 d.perm[i * 2] = i + high;
2046 d.perm[i * 2 + 1] = i + high + nelt;
2049 ok = ia64_expand_vec_perm_const_1 (&d);
2050 gcc_assert (ok);
2053 /* Return a vector of the sign-extension of VEC. */
2055 static rtx
2056 ia64_unpack_sign (rtx vec, bool unsignedp)
2058 enum machine_mode mode = GET_MODE (vec);
2059 rtx zero = CONST0_RTX (mode);
2061 if (unsignedp)
2062 return zero;
2063 else
2065 rtx sign = gen_reg_rtx (mode);
2066 bool neg;
2068 neg = ia64_expand_vecint_compare (LT, mode, sign, vec, zero);
2069 gcc_assert (!neg);
2071 return sign;
2075 /* Emit an integral vector unpack operation. */
2077 void
2078 ia64_expand_unpack (rtx operands[3], bool unsignedp, bool highp)
2080 rtx sign = ia64_unpack_sign (operands[1], unsignedp);
2081 ia64_unpack_assemble (operands[0], operands[1], sign, highp);
2084 /* Emit an integral vector widening sum operations. */
2086 void
2087 ia64_expand_widen_sum (rtx operands[3], bool unsignedp)
2089 enum machine_mode wmode;
2090 rtx l, h, t, sign;
2092 sign = ia64_unpack_sign (operands[1], unsignedp);
2094 wmode = GET_MODE (operands[0]);
2095 l = gen_reg_rtx (wmode);
2096 h = gen_reg_rtx (wmode);
2098 ia64_unpack_assemble (l, operands[1], sign, false);
2099 ia64_unpack_assemble (h, operands[1], sign, true);
2101 t = expand_binop (wmode, add_optab, l, operands[2], NULL, 0, OPTAB_DIRECT);
2102 t = expand_binop (wmode, add_optab, h, t, operands[0], 0, OPTAB_DIRECT);
2103 if (t != operands[0])
2104 emit_move_insn (operands[0], t);
2107 /* Emit the appropriate sequence for a call. */
2109 void
2110 ia64_expand_call (rtx retval, rtx addr, rtx nextarg ATTRIBUTE_UNUSED,
2111 int sibcall_p)
2113 rtx insn, b0;
2115 addr = XEXP (addr, 0);
2116 addr = convert_memory_address (DImode, addr);
2117 b0 = gen_rtx_REG (DImode, R_BR (0));
2119 /* ??? Should do this for functions known to bind local too. */
2120 if (TARGET_NO_PIC || TARGET_AUTO_PIC)
2122 if (sibcall_p)
2123 insn = gen_sibcall_nogp (addr);
2124 else if (! retval)
2125 insn = gen_call_nogp (addr, b0);
2126 else
2127 insn = gen_call_value_nogp (retval, addr, b0);
2128 insn = emit_call_insn (insn);
2130 else
2132 if (sibcall_p)
2133 insn = gen_sibcall_gp (addr);
2134 else if (! retval)
2135 insn = gen_call_gp (addr, b0);
2136 else
2137 insn = gen_call_value_gp (retval, addr, b0);
2138 insn = emit_call_insn (insn);
2140 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
2143 if (sibcall_p)
2144 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), b0);
2146 if (TARGET_ABI_OPEN_VMS)
2147 use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
2148 gen_rtx_REG (DImode, GR_REG (25)));
2151 static void
2152 reg_emitted (enum ia64_frame_regs r)
2154 if (emitted_frame_related_regs[r] == 0)
2155 emitted_frame_related_regs[r] = current_frame_info.r[r];
2156 else
2157 gcc_assert (emitted_frame_related_regs[r] == current_frame_info.r[r]);
2160 static int
2161 get_reg (enum ia64_frame_regs r)
2163 reg_emitted (r);
2164 return current_frame_info.r[r];
2167 static bool
2168 is_emitted (int regno)
2170 unsigned int r;
2172 for (r = reg_fp; r < number_of_ia64_frame_regs; r++)
2173 if (emitted_frame_related_regs[r] == regno)
2174 return true;
2175 return false;
2178 void
2179 ia64_reload_gp (void)
2181 rtx tmp;
2183 if (current_frame_info.r[reg_save_gp])
2185 tmp = gen_rtx_REG (DImode, get_reg (reg_save_gp));
2187 else
2189 HOST_WIDE_INT offset;
2190 rtx offset_r;
2192 offset = (current_frame_info.spill_cfa_off
2193 + current_frame_info.spill_size);
2194 if (frame_pointer_needed)
2196 tmp = hard_frame_pointer_rtx;
2197 offset = -offset;
2199 else
2201 tmp = stack_pointer_rtx;
2202 offset = current_frame_info.total_size - offset;
2205 offset_r = GEN_INT (offset);
2206 if (satisfies_constraint_I (offset_r))
2207 emit_insn (gen_adddi3 (pic_offset_table_rtx, tmp, offset_r));
2208 else
2210 emit_move_insn (pic_offset_table_rtx, offset_r);
2211 emit_insn (gen_adddi3 (pic_offset_table_rtx,
2212 pic_offset_table_rtx, tmp));
2215 tmp = gen_rtx_MEM (DImode, pic_offset_table_rtx);
2218 emit_move_insn (pic_offset_table_rtx, tmp);
2221 void
2222 ia64_split_call (rtx retval, rtx addr, rtx retaddr, rtx scratch_r,
2223 rtx scratch_b, int noreturn_p, int sibcall_p)
2225 rtx insn;
2226 bool is_desc = false;
2228 /* If we find we're calling through a register, then we're actually
2229 calling through a descriptor, so load up the values. */
2230 if (REG_P (addr) && GR_REGNO_P (REGNO (addr)))
2232 rtx tmp;
2233 bool addr_dead_p;
2235 /* ??? We are currently constrained to *not* use peep2, because
2236 we can legitimately change the global lifetime of the GP
2237 (in the form of killing where previously live). This is
2238 because a call through a descriptor doesn't use the previous
2239 value of the GP, while a direct call does, and we do not
2240 commit to either form until the split here.
2242 That said, this means that we lack precise life info for
2243 whether ADDR is dead after this call. This is not terribly
2244 important, since we can fix things up essentially for free
2245 with the POST_DEC below, but it's nice to not use it when we
2246 can immediately tell it's not necessary. */
2247 addr_dead_p = ((noreturn_p || sibcall_p
2248 || TEST_HARD_REG_BIT (regs_invalidated_by_call,
2249 REGNO (addr)))
2250 && !FUNCTION_ARG_REGNO_P (REGNO (addr)));
2252 /* Load the code address into scratch_b. */
2253 tmp = gen_rtx_POST_INC (Pmode, addr);
2254 tmp = gen_rtx_MEM (Pmode, tmp);
2255 emit_move_insn (scratch_r, tmp);
2256 emit_move_insn (scratch_b, scratch_r);
2258 /* Load the GP address. If ADDR is not dead here, then we must
2259 revert the change made above via the POST_INCREMENT. */
2260 if (!addr_dead_p)
2261 tmp = gen_rtx_POST_DEC (Pmode, addr);
2262 else
2263 tmp = addr;
2264 tmp = gen_rtx_MEM (Pmode, tmp);
2265 emit_move_insn (pic_offset_table_rtx, tmp);
2267 is_desc = true;
2268 addr = scratch_b;
2271 if (sibcall_p)
2272 insn = gen_sibcall_nogp (addr);
2273 else if (retval)
2274 insn = gen_call_value_nogp (retval, addr, retaddr);
2275 else
2276 insn = gen_call_nogp (addr, retaddr);
2277 emit_call_insn (insn);
2279 if ((!TARGET_CONST_GP || is_desc) && !noreturn_p && !sibcall_p)
2280 ia64_reload_gp ();
2283 /* Expand an atomic operation. We want to perform MEM <CODE>= VAL atomically.
2285 This differs from the generic code in that we know about the zero-extending
2286 properties of cmpxchg, and the zero-extending requirements of ar.ccv. We
2287 also know that ld.acq+cmpxchg.rel equals a full barrier.
2289 The loop we want to generate looks like
2291 cmp_reg = mem;
2292 label:
2293 old_reg = cmp_reg;
2294 new_reg = cmp_reg op val;
2295 cmp_reg = compare-and-swap(mem, old_reg, new_reg)
2296 if (cmp_reg != old_reg)
2297 goto label;
2299 Note that we only do the plain load from memory once. Subsequent
2300 iterations use the value loaded by the compare-and-swap pattern. */
2302 void
2303 ia64_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
2304 rtx old_dst, rtx new_dst, enum memmodel model)
2306 enum machine_mode mode = GET_MODE (mem);
2307 rtx old_reg, new_reg, cmp_reg, ar_ccv, label;
2308 enum insn_code icode;
2310 /* Special case for using fetchadd. */
2311 if ((mode == SImode || mode == DImode)
2312 && (code == PLUS || code == MINUS)
2313 && fetchadd_operand (val, mode))
2315 if (code == MINUS)
2316 val = GEN_INT (-INTVAL (val));
2318 if (!old_dst)
2319 old_dst = gen_reg_rtx (mode);
2321 switch (model)
2323 case MEMMODEL_ACQ_REL:
2324 case MEMMODEL_SEQ_CST:
2325 emit_insn (gen_memory_barrier ());
2326 /* FALLTHRU */
2327 case MEMMODEL_RELAXED:
2328 case MEMMODEL_ACQUIRE:
2329 case MEMMODEL_CONSUME:
2330 if (mode == SImode)
2331 icode = CODE_FOR_fetchadd_acq_si;
2332 else
2333 icode = CODE_FOR_fetchadd_acq_di;
2334 break;
2335 case MEMMODEL_RELEASE:
2336 if (mode == SImode)
2337 icode = CODE_FOR_fetchadd_rel_si;
2338 else
2339 icode = CODE_FOR_fetchadd_rel_di;
2340 break;
2342 default:
2343 gcc_unreachable ();
2346 emit_insn (GEN_FCN (icode) (old_dst, mem, val));
2348 if (new_dst)
2350 new_reg = expand_simple_binop (mode, PLUS, old_dst, val, new_dst,
2351 true, OPTAB_WIDEN);
2352 if (new_reg != new_dst)
2353 emit_move_insn (new_dst, new_reg);
2355 return;
2358 /* Because of the volatile mem read, we get an ld.acq, which is the
2359 front half of the full barrier. The end half is the cmpxchg.rel.
2360 For relaxed and release memory models, we don't need this. But we
2361 also don't bother trying to prevent it either. */
2362 gcc_assert (model == MEMMODEL_RELAXED
2363 || model == MEMMODEL_RELEASE
2364 || MEM_VOLATILE_P (mem));
2366 old_reg = gen_reg_rtx (DImode);
2367 cmp_reg = gen_reg_rtx (DImode);
2368 label = gen_label_rtx ();
2370 if (mode != DImode)
2372 val = simplify_gen_subreg (DImode, val, mode, 0);
2373 emit_insn (gen_extend_insn (cmp_reg, mem, DImode, mode, 1));
2375 else
2376 emit_move_insn (cmp_reg, mem);
2378 emit_label (label);
2380 ar_ccv = gen_rtx_REG (DImode, AR_CCV_REGNUM);
2381 emit_move_insn (old_reg, cmp_reg);
2382 emit_move_insn (ar_ccv, cmp_reg);
2384 if (old_dst)
2385 emit_move_insn (old_dst, gen_lowpart (mode, cmp_reg));
2387 new_reg = cmp_reg;
2388 if (code == NOT)
2390 new_reg = expand_simple_binop (DImode, AND, new_reg, val, NULL_RTX,
2391 true, OPTAB_DIRECT);
2392 new_reg = expand_simple_unop (DImode, code, new_reg, NULL_RTX, true);
2394 else
2395 new_reg = expand_simple_binop (DImode, code, new_reg, val, NULL_RTX,
2396 true, OPTAB_DIRECT);
2398 if (mode != DImode)
2399 new_reg = gen_lowpart (mode, new_reg);
2400 if (new_dst)
2401 emit_move_insn (new_dst, new_reg);
2403 switch (model)
2405 case MEMMODEL_RELAXED:
2406 case MEMMODEL_ACQUIRE:
2407 case MEMMODEL_CONSUME:
2408 switch (mode)
2410 case QImode: icode = CODE_FOR_cmpxchg_acq_qi; break;
2411 case HImode: icode = CODE_FOR_cmpxchg_acq_hi; break;
2412 case SImode: icode = CODE_FOR_cmpxchg_acq_si; break;
2413 case DImode: icode = CODE_FOR_cmpxchg_acq_di; break;
2414 default:
2415 gcc_unreachable ();
2417 break;
2419 case MEMMODEL_RELEASE:
2420 case MEMMODEL_ACQ_REL:
2421 case MEMMODEL_SEQ_CST:
2422 switch (mode)
2424 case QImode: icode = CODE_FOR_cmpxchg_rel_qi; break;
2425 case HImode: icode = CODE_FOR_cmpxchg_rel_hi; break;
2426 case SImode: icode = CODE_FOR_cmpxchg_rel_si; break;
2427 case DImode: icode = CODE_FOR_cmpxchg_rel_di; break;
2428 default:
2429 gcc_unreachable ();
2431 break;
2433 default:
2434 gcc_unreachable ();
2437 emit_insn (GEN_FCN (icode) (cmp_reg, mem, ar_ccv, new_reg));
2439 emit_cmp_and_jump_insns (cmp_reg, old_reg, NE, NULL, DImode, true, label);
2442 /* Begin the assembly file. */
2444 static void
2445 ia64_file_start (void)
2447 default_file_start ();
2448 emit_safe_across_calls ();
2451 void
2452 emit_safe_across_calls (void)
2454 unsigned int rs, re;
2455 int out_state;
2457 rs = 1;
2458 out_state = 0;
2459 while (1)
2461 while (rs < 64 && call_used_regs[PR_REG (rs)])
2462 rs++;
2463 if (rs >= 64)
2464 break;
2465 for (re = rs + 1; re < 64 && ! call_used_regs[PR_REG (re)]; re++)
2466 continue;
2467 if (out_state == 0)
2469 fputs ("\t.pred.safe_across_calls ", asm_out_file);
2470 out_state = 1;
2472 else
2473 fputc (',', asm_out_file);
2474 if (re == rs + 1)
2475 fprintf (asm_out_file, "p%u", rs);
2476 else
2477 fprintf (asm_out_file, "p%u-p%u", rs, re - 1);
2478 rs = re + 1;
2480 if (out_state)
2481 fputc ('\n', asm_out_file);
2484 /* Globalize a declaration. */
2486 static void
2487 ia64_globalize_decl_name (FILE * stream, tree decl)
2489 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2490 tree version_attr = lookup_attribute ("version_id", DECL_ATTRIBUTES (decl));
2491 if (version_attr)
2493 tree v = TREE_VALUE (TREE_VALUE (version_attr));
2494 const char *p = TREE_STRING_POINTER (v);
2495 fprintf (stream, "\t.alias %s#, \"%s{%s}\"\n", name, name, p);
2497 targetm.asm_out.globalize_label (stream, name);
2498 if (TREE_CODE (decl) == FUNCTION_DECL)
2499 ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "function");
2502 /* Helper function for ia64_compute_frame_size: find an appropriate general
2503 register to spill some special register to. SPECIAL_SPILL_MASK contains
2504 bits in GR0 to GR31 that have already been allocated by this routine.
2505 TRY_LOCALS is true if we should attempt to locate a local regnum. */
2507 static int
2508 find_gr_spill (enum ia64_frame_regs r, int try_locals)
2510 int regno;
2512 if (emitted_frame_related_regs[r] != 0)
2514 regno = emitted_frame_related_regs[r];
2515 if (regno >= LOC_REG (0) && regno < LOC_REG (80 - frame_pointer_needed)
2516 && current_frame_info.n_local_regs < regno - LOC_REG (0) + 1)
2517 current_frame_info.n_local_regs = regno - LOC_REG (0) + 1;
2518 else if (crtl->is_leaf
2519 && regno >= GR_REG (1) && regno <= GR_REG (31))
2520 current_frame_info.gr_used_mask |= 1 << regno;
2522 return regno;
2525 /* If this is a leaf function, first try an otherwise unused
2526 call-clobbered register. */
2527 if (crtl->is_leaf)
2529 for (regno = GR_REG (1); regno <= GR_REG (31); regno++)
2530 if (! df_regs_ever_live_p (regno)
2531 && call_used_regs[regno]
2532 && ! fixed_regs[regno]
2533 && ! global_regs[regno]
2534 && ((current_frame_info.gr_used_mask >> regno) & 1) == 0
2535 && ! is_emitted (regno))
2537 current_frame_info.gr_used_mask |= 1 << regno;
2538 return regno;
2542 if (try_locals)
2544 regno = current_frame_info.n_local_regs;
2545 /* If there is a frame pointer, then we can't use loc79, because
2546 that is HARD_FRAME_POINTER_REGNUM. In particular, see the
2547 reg_name switching code in ia64_expand_prologue. */
2548 while (regno < (80 - frame_pointer_needed))
2549 if (! is_emitted (LOC_REG (regno++)))
2551 current_frame_info.n_local_regs = regno;
2552 return LOC_REG (regno - 1);
2556 /* Failed to find a general register to spill to. Must use stack. */
2557 return 0;
2560 /* In order to make for nice schedules, we try to allocate every temporary
2561 to a different register. We must of course stay away from call-saved,
2562 fixed, and global registers. We must also stay away from registers
2563 allocated in current_frame_info.gr_used_mask, since those include regs
2564 used all through the prologue.
2566 Any register allocated here must be used immediately. The idea is to
2567 aid scheduling, not to solve data flow problems. */
2569 static int last_scratch_gr_reg;
2571 static int
2572 next_scratch_gr_reg (void)
2574 int i, regno;
2576 for (i = 0; i < 32; ++i)
2578 regno = (last_scratch_gr_reg + i + 1) & 31;
2579 if (call_used_regs[regno]
2580 && ! fixed_regs[regno]
2581 && ! global_regs[regno]
2582 && ((current_frame_info.gr_used_mask >> regno) & 1) == 0)
2584 last_scratch_gr_reg = regno;
2585 return regno;
2589 /* There must be _something_ available. */
2590 gcc_unreachable ();
2593 /* Helper function for ia64_compute_frame_size, called through
2594 diddle_return_value. Mark REG in current_frame_info.gr_used_mask. */
2596 static void
2597 mark_reg_gr_used_mask (rtx reg, void *data ATTRIBUTE_UNUSED)
2599 unsigned int regno = REGNO (reg);
2600 if (regno < 32)
2602 unsigned int i, n = hard_regno_nregs[regno][GET_MODE (reg)];
2603 for (i = 0; i < n; ++i)
2604 current_frame_info.gr_used_mask |= 1 << (regno + i);
2609 /* Returns the number of bytes offset between the frame pointer and the stack
2610 pointer for the current function. SIZE is the number of bytes of space
2611 needed for local variables. */
2613 static void
2614 ia64_compute_frame_size (HOST_WIDE_INT size)
2616 HOST_WIDE_INT total_size;
2617 HOST_WIDE_INT spill_size = 0;
2618 HOST_WIDE_INT extra_spill_size = 0;
2619 HOST_WIDE_INT pretend_args_size;
2620 HARD_REG_SET mask;
2621 int n_spilled = 0;
2622 int spilled_gr_p = 0;
2623 int spilled_fr_p = 0;
2624 unsigned int regno;
2625 int min_regno;
2626 int max_regno;
2627 int i;
2629 if (current_frame_info.initialized)
2630 return;
2632 memset (&current_frame_info, 0, sizeof current_frame_info);
2633 CLEAR_HARD_REG_SET (mask);
2635 /* Don't allocate scratches to the return register. */
2636 diddle_return_value (mark_reg_gr_used_mask, NULL);
2638 /* Don't allocate scratches to the EH scratch registers. */
2639 if (cfun->machine->ia64_eh_epilogue_sp)
2640 mark_reg_gr_used_mask (cfun->machine->ia64_eh_epilogue_sp, NULL);
2641 if (cfun->machine->ia64_eh_epilogue_bsp)
2642 mark_reg_gr_used_mask (cfun->machine->ia64_eh_epilogue_bsp, NULL);
2644 /* Static stack checking uses r2 and r3. */
2645 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
2646 current_frame_info.gr_used_mask |= 0xc;
2648 /* Find the size of the register stack frame. We have only 80 local
2649 registers, because we reserve 8 for the inputs and 8 for the
2650 outputs. */
2652 /* Skip HARD_FRAME_POINTER_REGNUM (loc79) when frame_pointer_needed,
2653 since we'll be adjusting that down later. */
2654 regno = LOC_REG (78) + ! frame_pointer_needed;
2655 for (; regno >= LOC_REG (0); regno--)
2656 if (df_regs_ever_live_p (regno) && !is_emitted (regno))
2657 break;
2658 current_frame_info.n_local_regs = regno - LOC_REG (0) + 1;
2660 /* For functions marked with the syscall_linkage attribute, we must mark
2661 all eight input registers as in use, so that locals aren't visible to
2662 the caller. */
2664 if (cfun->machine->n_varargs > 0
2665 || lookup_attribute ("syscall_linkage",
2666 TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
2667 current_frame_info.n_input_regs = 8;
2668 else
2670 for (regno = IN_REG (7); regno >= IN_REG (0); regno--)
2671 if (df_regs_ever_live_p (regno))
2672 break;
2673 current_frame_info.n_input_regs = regno - IN_REG (0) + 1;
2676 for (regno = OUT_REG (7); regno >= OUT_REG (0); regno--)
2677 if (df_regs_ever_live_p (regno))
2678 break;
2679 i = regno - OUT_REG (0) + 1;
2681 #ifndef PROFILE_HOOK
2682 /* When -p profiling, we need one output register for the mcount argument.
2683 Likewise for -a profiling for the bb_init_func argument. For -ax
2684 profiling, we need two output registers for the two bb_init_trace_func
2685 arguments. */
2686 if (crtl->profile)
2687 i = MAX (i, 1);
2688 #endif
2689 current_frame_info.n_output_regs = i;
2691 /* ??? No rotating register support yet. */
2692 current_frame_info.n_rotate_regs = 0;
2694 /* Discover which registers need spilling, and how much room that
2695 will take. Begin with floating point and general registers,
2696 which will always wind up on the stack. */
2698 for (regno = FR_REG (2); regno <= FR_REG (127); regno++)
2699 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2701 SET_HARD_REG_BIT (mask, regno);
2702 spill_size += 16;
2703 n_spilled += 1;
2704 spilled_fr_p = 1;
2707 for (regno = GR_REG (1); regno <= GR_REG (31); regno++)
2708 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2710 SET_HARD_REG_BIT (mask, regno);
2711 spill_size += 8;
2712 n_spilled += 1;
2713 spilled_gr_p = 1;
2716 for (regno = BR_REG (1); regno <= BR_REG (7); regno++)
2717 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2719 SET_HARD_REG_BIT (mask, regno);
2720 spill_size += 8;
2721 n_spilled += 1;
2724 /* Now come all special registers that might get saved in other
2725 general registers. */
2727 if (frame_pointer_needed)
2729 current_frame_info.r[reg_fp] = find_gr_spill (reg_fp, 1);
2730 /* If we did not get a register, then we take LOC79. This is guaranteed
2731 to be free, even if regs_ever_live is already set, because this is
2732 HARD_FRAME_POINTER_REGNUM. This requires incrementing n_local_regs,
2733 as we don't count loc79 above. */
2734 if (current_frame_info.r[reg_fp] == 0)
2736 current_frame_info.r[reg_fp] = LOC_REG (79);
2737 current_frame_info.n_local_regs = LOC_REG (79) - LOC_REG (0) + 1;
2741 if (! crtl->is_leaf)
2743 /* Emit a save of BR0 if we call other functions. Do this even
2744 if this function doesn't return, as EH depends on this to be
2745 able to unwind the stack. */
2746 SET_HARD_REG_BIT (mask, BR_REG (0));
2748 current_frame_info.r[reg_save_b0] = find_gr_spill (reg_save_b0, 1);
2749 if (current_frame_info.r[reg_save_b0] == 0)
2751 extra_spill_size += 8;
2752 n_spilled += 1;
2755 /* Similarly for ar.pfs. */
2756 SET_HARD_REG_BIT (mask, AR_PFS_REGNUM);
2757 current_frame_info.r[reg_save_ar_pfs] = find_gr_spill (reg_save_ar_pfs, 1);
2758 if (current_frame_info.r[reg_save_ar_pfs] == 0)
2760 extra_spill_size += 8;
2761 n_spilled += 1;
2764 /* Similarly for gp. Note that if we're calling setjmp, the stacked
2765 registers are clobbered, so we fall back to the stack. */
2766 current_frame_info.r[reg_save_gp]
2767 = (cfun->calls_setjmp ? 0 : find_gr_spill (reg_save_gp, 1));
2768 if (current_frame_info.r[reg_save_gp] == 0)
2770 SET_HARD_REG_BIT (mask, GR_REG (1));
2771 spill_size += 8;
2772 n_spilled += 1;
2775 else
2777 if (df_regs_ever_live_p (BR_REG (0)) && ! call_used_regs[BR_REG (0)])
2779 SET_HARD_REG_BIT (mask, BR_REG (0));
2780 extra_spill_size += 8;
2781 n_spilled += 1;
2784 if (df_regs_ever_live_p (AR_PFS_REGNUM))
2786 SET_HARD_REG_BIT (mask, AR_PFS_REGNUM);
2787 current_frame_info.r[reg_save_ar_pfs]
2788 = find_gr_spill (reg_save_ar_pfs, 1);
2789 if (current_frame_info.r[reg_save_ar_pfs] == 0)
2791 extra_spill_size += 8;
2792 n_spilled += 1;
2797 /* Unwind descriptor hackery: things are most efficient if we allocate
2798 consecutive GR save registers for RP, PFS, FP in that order. However,
2799 it is absolutely critical that FP get the only hard register that's
2800 guaranteed to be free, so we allocated it first. If all three did
2801 happen to be allocated hard regs, and are consecutive, rearrange them
2802 into the preferred order now.
2804 If we have already emitted code for any of those registers,
2805 then it's already too late to change. */
2806 min_regno = MIN (current_frame_info.r[reg_fp],
2807 MIN (current_frame_info.r[reg_save_b0],
2808 current_frame_info.r[reg_save_ar_pfs]));
2809 max_regno = MAX (current_frame_info.r[reg_fp],
2810 MAX (current_frame_info.r[reg_save_b0],
2811 current_frame_info.r[reg_save_ar_pfs]));
2812 if (min_regno > 0
2813 && min_regno + 2 == max_regno
2814 && (current_frame_info.r[reg_fp] == min_regno + 1
2815 || current_frame_info.r[reg_save_b0] == min_regno + 1
2816 || current_frame_info.r[reg_save_ar_pfs] == min_regno + 1)
2817 && (emitted_frame_related_regs[reg_save_b0] == 0
2818 || emitted_frame_related_regs[reg_save_b0] == min_regno)
2819 && (emitted_frame_related_regs[reg_save_ar_pfs] == 0
2820 || emitted_frame_related_regs[reg_save_ar_pfs] == min_regno + 1)
2821 && (emitted_frame_related_regs[reg_fp] == 0
2822 || emitted_frame_related_regs[reg_fp] == min_regno + 2))
2824 current_frame_info.r[reg_save_b0] = min_regno;
2825 current_frame_info.r[reg_save_ar_pfs] = min_regno + 1;
2826 current_frame_info.r[reg_fp] = min_regno + 2;
2829 /* See if we need to store the predicate register block. */
2830 for (regno = PR_REG (0); regno <= PR_REG (63); regno++)
2831 if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2832 break;
2833 if (regno <= PR_REG (63))
2835 SET_HARD_REG_BIT (mask, PR_REG (0));
2836 current_frame_info.r[reg_save_pr] = find_gr_spill (reg_save_pr, 1);
2837 if (current_frame_info.r[reg_save_pr] == 0)
2839 extra_spill_size += 8;
2840 n_spilled += 1;
2843 /* ??? Mark them all as used so that register renaming and such
2844 are free to use them. */
2845 for (regno = PR_REG (0); regno <= PR_REG (63); regno++)
2846 df_set_regs_ever_live (regno, true);
2849 /* If we're forced to use st8.spill, we're forced to save and restore
2850 ar.unat as well. The check for existing liveness allows inline asm
2851 to touch ar.unat. */
2852 if (spilled_gr_p || cfun->machine->n_varargs
2853 || df_regs_ever_live_p (AR_UNAT_REGNUM))
2855 df_set_regs_ever_live (AR_UNAT_REGNUM, true);
2856 SET_HARD_REG_BIT (mask, AR_UNAT_REGNUM);
2857 current_frame_info.r[reg_save_ar_unat]
2858 = find_gr_spill (reg_save_ar_unat, spill_size == 0);
2859 if (current_frame_info.r[reg_save_ar_unat] == 0)
2861 extra_spill_size += 8;
2862 n_spilled += 1;
2866 if (df_regs_ever_live_p (AR_LC_REGNUM))
2868 SET_HARD_REG_BIT (mask, AR_LC_REGNUM);
2869 current_frame_info.r[reg_save_ar_lc]
2870 = find_gr_spill (reg_save_ar_lc, spill_size == 0);
2871 if (current_frame_info.r[reg_save_ar_lc] == 0)
2873 extra_spill_size += 8;
2874 n_spilled += 1;
2878 /* If we have an odd number of words of pretend arguments written to
2879 the stack, then the FR save area will be unaligned. We round the
2880 size of this area up to keep things 16 byte aligned. */
2881 if (spilled_fr_p)
2882 pretend_args_size = IA64_STACK_ALIGN (crtl->args.pretend_args_size);
2883 else
2884 pretend_args_size = crtl->args.pretend_args_size;
2886 total_size = (spill_size + extra_spill_size + size + pretend_args_size
2887 + crtl->outgoing_args_size);
2888 total_size = IA64_STACK_ALIGN (total_size);
2890 /* We always use the 16-byte scratch area provided by the caller, but
2891 if we are a leaf function, there's no one to which we need to provide
2892 a scratch area. However, if the function allocates dynamic stack space,
2893 the dynamic offset is computed early and contains STACK_POINTER_OFFSET,
2894 so we need to cope. */
2895 if (crtl->is_leaf && !cfun->calls_alloca)
2896 total_size = MAX (0, total_size - 16);
2898 current_frame_info.total_size = total_size;
2899 current_frame_info.spill_cfa_off = pretend_args_size - 16;
2900 current_frame_info.spill_size = spill_size;
2901 current_frame_info.extra_spill_size = extra_spill_size;
2902 COPY_HARD_REG_SET (current_frame_info.mask, mask);
2903 current_frame_info.n_spilled = n_spilled;
2904 current_frame_info.initialized = reload_completed;
2907 /* Worker function for TARGET_CAN_ELIMINATE. */
2909 bool
2910 ia64_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
2912 return (to == BR_REG (0) ? crtl->is_leaf : true);
2915 /* Compute the initial difference between the specified pair of registers. */
2917 HOST_WIDE_INT
2918 ia64_initial_elimination_offset (int from, int to)
2920 HOST_WIDE_INT offset;
2922 ia64_compute_frame_size (get_frame_size ());
2923 switch (from)
2925 case FRAME_POINTER_REGNUM:
2926 switch (to)
2928 case HARD_FRAME_POINTER_REGNUM:
2929 offset = -current_frame_info.total_size;
2930 if (!crtl->is_leaf || cfun->calls_alloca)
2931 offset += 16 + crtl->outgoing_args_size;
2932 break;
2934 case STACK_POINTER_REGNUM:
2935 offset = 0;
2936 if (!crtl->is_leaf || cfun->calls_alloca)
2937 offset += 16 + crtl->outgoing_args_size;
2938 break;
2940 default:
2941 gcc_unreachable ();
2943 break;
2945 case ARG_POINTER_REGNUM:
2946 /* Arguments start above the 16 byte save area, unless stdarg
2947 in which case we store through the 16 byte save area. */
2948 switch (to)
2950 case HARD_FRAME_POINTER_REGNUM:
2951 offset = 16 - crtl->args.pretend_args_size;
2952 break;
2954 case STACK_POINTER_REGNUM:
2955 offset = (current_frame_info.total_size
2956 + 16 - crtl->args.pretend_args_size);
2957 break;
2959 default:
2960 gcc_unreachable ();
2962 break;
2964 default:
2965 gcc_unreachable ();
2968 return offset;
2971 /* If there are more than a trivial number of register spills, we use
2972 two interleaved iterators so that we can get two memory references
2973 per insn group.
2975 In order to simplify things in the prologue and epilogue expanders,
2976 we use helper functions to fix up the memory references after the
2977 fact with the appropriate offsets to a POST_MODIFY memory mode.
2978 The following data structure tracks the state of the two iterators
2979 while insns are being emitted. */
2981 struct spill_fill_data
2983 rtx init_after; /* point at which to emit initializations */
2984 rtx init_reg[2]; /* initial base register */
2985 rtx iter_reg[2]; /* the iterator registers */
2986 rtx *prev_addr[2]; /* address of last memory use */
2987 rtx prev_insn[2]; /* the insn corresponding to prev_addr */
2988 HOST_WIDE_INT prev_off[2]; /* last offset */
2989 int n_iter; /* number of iterators in use */
2990 int next_iter; /* next iterator to use */
2991 unsigned int save_gr_used_mask;
2994 static struct spill_fill_data spill_fill_data;
2996 static void
2997 setup_spill_pointers (int n_spills, rtx init_reg, HOST_WIDE_INT cfa_off)
2999 int i;
3001 spill_fill_data.init_after = get_last_insn ();
3002 spill_fill_data.init_reg[0] = init_reg;
3003 spill_fill_data.init_reg[1] = init_reg;
3004 spill_fill_data.prev_addr[0] = NULL;
3005 spill_fill_data.prev_addr[1] = NULL;
3006 spill_fill_data.prev_insn[0] = NULL;
3007 spill_fill_data.prev_insn[1] = NULL;
3008 spill_fill_data.prev_off[0] = cfa_off;
3009 spill_fill_data.prev_off[1] = cfa_off;
3010 spill_fill_data.next_iter = 0;
3011 spill_fill_data.save_gr_used_mask = current_frame_info.gr_used_mask;
3013 spill_fill_data.n_iter = 1 + (n_spills > 2);
3014 for (i = 0; i < spill_fill_data.n_iter; ++i)
3016 int regno = next_scratch_gr_reg ();
3017 spill_fill_data.iter_reg[i] = gen_rtx_REG (DImode, regno);
3018 current_frame_info.gr_used_mask |= 1 << regno;
3022 static void
3023 finish_spill_pointers (void)
3025 current_frame_info.gr_used_mask = spill_fill_data.save_gr_used_mask;
3028 static rtx
3029 spill_restore_mem (rtx reg, HOST_WIDE_INT cfa_off)
3031 int iter = spill_fill_data.next_iter;
3032 HOST_WIDE_INT disp = spill_fill_data.prev_off[iter] - cfa_off;
3033 rtx disp_rtx = GEN_INT (disp);
3034 rtx mem;
3036 if (spill_fill_data.prev_addr[iter])
3038 if (satisfies_constraint_N (disp_rtx))
3040 *spill_fill_data.prev_addr[iter]
3041 = gen_rtx_POST_MODIFY (DImode, spill_fill_data.iter_reg[iter],
3042 gen_rtx_PLUS (DImode,
3043 spill_fill_data.iter_reg[iter],
3044 disp_rtx));
3045 add_reg_note (spill_fill_data.prev_insn[iter],
3046 REG_INC, spill_fill_data.iter_reg[iter]);
3048 else
3050 /* ??? Could use register post_modify for loads. */
3051 if (!satisfies_constraint_I (disp_rtx))
3053 rtx tmp = gen_rtx_REG (DImode, next_scratch_gr_reg ());
3054 emit_move_insn (tmp, disp_rtx);
3055 disp_rtx = tmp;
3057 emit_insn (gen_adddi3 (spill_fill_data.iter_reg[iter],
3058 spill_fill_data.iter_reg[iter], disp_rtx));
3061 /* Micro-optimization: if we've created a frame pointer, it's at
3062 CFA 0, which may allow the real iterator to be initialized lower,
3063 slightly increasing parallelism. Also, if there are few saves
3064 it may eliminate the iterator entirely. */
3065 else if (disp == 0
3066 && spill_fill_data.init_reg[iter] == stack_pointer_rtx
3067 && frame_pointer_needed)
3069 mem = gen_rtx_MEM (GET_MODE (reg), hard_frame_pointer_rtx);
3070 set_mem_alias_set (mem, get_varargs_alias_set ());
3071 return mem;
3073 else
3075 rtx seq, insn;
3077 if (disp == 0)
3078 seq = gen_movdi (spill_fill_data.iter_reg[iter],
3079 spill_fill_data.init_reg[iter]);
3080 else
3082 start_sequence ();
3084 if (!satisfies_constraint_I (disp_rtx))
3086 rtx tmp = gen_rtx_REG (DImode, next_scratch_gr_reg ());
3087 emit_move_insn (tmp, disp_rtx);
3088 disp_rtx = tmp;
3091 emit_insn (gen_adddi3 (spill_fill_data.iter_reg[iter],
3092 spill_fill_data.init_reg[iter],
3093 disp_rtx));
3095 seq = get_insns ();
3096 end_sequence ();
3099 /* Careful for being the first insn in a sequence. */
3100 if (spill_fill_data.init_after)
3101 insn = emit_insn_after (seq, spill_fill_data.init_after);
3102 else
3104 rtx first = get_insns ();
3105 if (first)
3106 insn = emit_insn_before (seq, first);
3107 else
3108 insn = emit_insn (seq);
3110 spill_fill_data.init_after = insn;
3113 mem = gen_rtx_MEM (GET_MODE (reg), spill_fill_data.iter_reg[iter]);
3115 /* ??? Not all of the spills are for varargs, but some of them are.
3116 The rest of the spills belong in an alias set of their own. But
3117 it doesn't actually hurt to include them here. */
3118 set_mem_alias_set (mem, get_varargs_alias_set ());
3120 spill_fill_data.prev_addr[iter] = &XEXP (mem, 0);
3121 spill_fill_data.prev_off[iter] = cfa_off;
3123 if (++iter >= spill_fill_data.n_iter)
3124 iter = 0;
3125 spill_fill_data.next_iter = iter;
3127 return mem;
3130 static void
3131 do_spill (rtx (*move_fn) (rtx, rtx, rtx), rtx reg, HOST_WIDE_INT cfa_off,
3132 rtx frame_reg)
3134 int iter = spill_fill_data.next_iter;
3135 rtx mem, insn;
3137 mem = spill_restore_mem (reg, cfa_off);
3138 insn = emit_insn ((*move_fn) (mem, reg, GEN_INT (cfa_off)));
3139 spill_fill_data.prev_insn[iter] = insn;
3141 if (frame_reg)
3143 rtx base;
3144 HOST_WIDE_INT off;
3146 RTX_FRAME_RELATED_P (insn) = 1;
3148 /* Don't even pretend that the unwind code can intuit its way
3149 through a pair of interleaved post_modify iterators. Just
3150 provide the correct answer. */
3152 if (frame_pointer_needed)
3154 base = hard_frame_pointer_rtx;
3155 off = - cfa_off;
3157 else
3159 base = stack_pointer_rtx;
3160 off = current_frame_info.total_size - cfa_off;
3163 add_reg_note (insn, REG_CFA_OFFSET,
3164 gen_rtx_SET (VOIDmode,
3165 gen_rtx_MEM (GET_MODE (reg),
3166 plus_constant (Pmode,
3167 base, off)),
3168 frame_reg));
3172 static void
3173 do_restore (rtx (*move_fn) (rtx, rtx, rtx), rtx reg, HOST_WIDE_INT cfa_off)
3175 int iter = spill_fill_data.next_iter;
3176 rtx insn;
3178 insn = emit_insn ((*move_fn) (reg, spill_restore_mem (reg, cfa_off),
3179 GEN_INT (cfa_off)));
3180 spill_fill_data.prev_insn[iter] = insn;
3183 /* Wrapper functions that discards the CONST_INT spill offset. These
3184 exist so that we can give gr_spill/gr_fill the offset they need and
3185 use a consistent function interface. */
3187 static rtx
3188 gen_movdi_x (rtx dest, rtx src, rtx offset ATTRIBUTE_UNUSED)
3190 return gen_movdi (dest, src);
3193 static rtx
3194 gen_fr_spill_x (rtx dest, rtx src, rtx offset ATTRIBUTE_UNUSED)
3196 return gen_fr_spill (dest, src);
3199 static rtx
3200 gen_fr_restore_x (rtx dest, rtx src, rtx offset ATTRIBUTE_UNUSED)
3202 return gen_fr_restore (dest, src);
3205 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
3207 /* See Table 6.2 of the IA-64 Software Developer Manual, Volume 2. */
3208 #define BACKING_STORE_SIZE(N) ((N) > 0 ? ((N) + (N)/63 + 1) * 8 : 0)
3210 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
3211 inclusive. These are offsets from the current stack pointer. BS_SIZE
3212 is the size of the backing store. ??? This clobbers r2 and r3. */
3214 static void
3215 ia64_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size,
3216 int bs_size)
3218 rtx r2 = gen_rtx_REG (Pmode, GR_REG (2));
3219 rtx r3 = gen_rtx_REG (Pmode, GR_REG (3));
3220 rtx p6 = gen_rtx_REG (BImode, PR_REG (6));
3222 /* On the IA-64 there is a second stack in memory, namely the Backing Store
3223 of the Register Stack Engine. We also need to probe it after checking
3224 that the 2 stacks don't overlap. */
3225 emit_insn (gen_bsp_value (r3));
3226 emit_move_insn (r2, GEN_INT (-(first + size)));
3228 /* Compare current value of BSP and SP registers. */
3229 emit_insn (gen_rtx_SET (VOIDmode, p6,
3230 gen_rtx_fmt_ee (LTU, BImode,
3231 r3, stack_pointer_rtx)));
3233 /* Compute the address of the probe for the Backing Store (which grows
3234 towards higher addresses). We probe only at the first offset of
3235 the next page because some OS (eg Linux/ia64) only extend the
3236 backing store when this specific address is hit (but generate a SEGV
3237 on other address). Page size is the worst case (4KB). The reserve
3238 size is at least 4096 - (96 + 2) * 8 = 3312 bytes, which is enough.
3239 Also compute the address of the last probe for the memory stack
3240 (which grows towards lower addresses). */
3241 emit_insn (gen_rtx_SET (VOIDmode, r3, plus_constant (Pmode, r3, 4095)));
3242 emit_insn (gen_rtx_SET (VOIDmode, r2,
3243 gen_rtx_PLUS (Pmode, stack_pointer_rtx, r2)));
3245 /* Compare them and raise SEGV if the former has topped the latter. */
3246 emit_insn (gen_rtx_COND_EXEC (VOIDmode,
3247 gen_rtx_fmt_ee (NE, VOIDmode, p6, const0_rtx),
3248 gen_rtx_SET (VOIDmode, p6,
3249 gen_rtx_fmt_ee (GEU, BImode,
3250 r3, r2))));
3251 emit_insn (gen_rtx_SET (VOIDmode,
3252 gen_rtx_ZERO_EXTRACT (DImode, r3, GEN_INT (12),
3253 const0_rtx),
3254 const0_rtx));
3255 emit_insn (gen_rtx_COND_EXEC (VOIDmode,
3256 gen_rtx_fmt_ee (NE, VOIDmode, p6, const0_rtx),
3257 gen_rtx_TRAP_IF (VOIDmode, const1_rtx,
3258 GEN_INT (11))));
3260 /* Probe the Backing Store if necessary. */
3261 if (bs_size > 0)
3262 emit_stack_probe (r3);
3264 /* Probe the memory stack if necessary. */
3265 if (size == 0)
3268 /* See if we have a constant small number of probes to generate. If so,
3269 that's the easy case. */
3270 else if (size <= PROBE_INTERVAL)
3271 emit_stack_probe (r2);
3273 /* The run-time loop is made up of 8 insns in the generic case while this
3274 compile-time loop is made up of 5+2*(n-2) insns for n # of intervals. */
3275 else if (size <= 4 * PROBE_INTERVAL)
3277 HOST_WIDE_INT i;
3279 emit_move_insn (r2, GEN_INT (-(first + PROBE_INTERVAL)));
3280 emit_insn (gen_rtx_SET (VOIDmode, r2,
3281 gen_rtx_PLUS (Pmode, stack_pointer_rtx, r2)));
3282 emit_stack_probe (r2);
3284 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 2 until
3285 it exceeds SIZE. If only two probes are needed, this will not
3286 generate any code. Then probe at FIRST + SIZE. */
3287 for (i = 2 * PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
3289 emit_insn (gen_rtx_SET (VOIDmode, r2,
3290 plus_constant (Pmode, r2, -PROBE_INTERVAL)));
3291 emit_stack_probe (r2);
3294 emit_insn (gen_rtx_SET (VOIDmode, r2,
3295 plus_constant (Pmode, r2,
3296 (i - PROBE_INTERVAL) - size)));
3297 emit_stack_probe (r2);
3300 /* Otherwise, do the same as above, but in a loop. Note that we must be
3301 extra careful with variables wrapping around because we might be at
3302 the very top (or the very bottom) of the address space and we have
3303 to be able to handle this case properly; in particular, we use an
3304 equality test for the loop condition. */
3305 else
3307 HOST_WIDE_INT rounded_size;
3309 emit_move_insn (r2, GEN_INT (-first));
3312 /* Step 1: round SIZE to the previous multiple of the interval. */
3314 rounded_size = size & -PROBE_INTERVAL;
3317 /* Step 2: compute initial and final value of the loop counter. */
3319 /* TEST_ADDR = SP + FIRST. */
3320 emit_insn (gen_rtx_SET (VOIDmode, r2,
3321 gen_rtx_PLUS (Pmode, stack_pointer_rtx, r2)));
3323 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
3324 if (rounded_size > (1 << 21))
3326 emit_move_insn (r3, GEN_INT (-rounded_size));
3327 emit_insn (gen_rtx_SET (VOIDmode, r3, gen_rtx_PLUS (Pmode, r2, r3)));
3329 else
3330 emit_insn (gen_rtx_SET (VOIDmode, r3,
3331 gen_rtx_PLUS (Pmode, r2,
3332 GEN_INT (-rounded_size))));
3335 /* Step 3: the loop
3337 while (TEST_ADDR != LAST_ADDR)
3339 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
3340 probe at TEST_ADDR
3343 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
3344 until it is equal to ROUNDED_SIZE. */
3346 emit_insn (gen_probe_stack_range (r2, r2, r3));
3349 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
3350 that SIZE is equal to ROUNDED_SIZE. */
3352 /* TEMP = SIZE - ROUNDED_SIZE. */
3353 if (size != rounded_size)
3355 emit_insn (gen_rtx_SET (VOIDmode, r2,
3356 plus_constant (Pmode, r2,
3357 rounded_size - size)));
3358 emit_stack_probe (r2);
3362 /* Make sure nothing is scheduled before we are done. */
3363 emit_insn (gen_blockage ());
3366 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
3367 absolute addresses. */
3369 const char *
3370 output_probe_stack_range (rtx reg1, rtx reg2)
3372 static int labelno = 0;
3373 char loop_lab[32], end_lab[32];
3374 rtx xops[3];
3376 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
3377 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
3379 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
3381 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
3382 xops[0] = reg1;
3383 xops[1] = reg2;
3384 xops[2] = gen_rtx_REG (BImode, PR_REG (6));
3385 output_asm_insn ("cmp.eq %2, %I2 = %0, %1", xops);
3386 fprintf (asm_out_file, "\t(%s) br.cond.dpnt ", reg_names [REGNO (xops[2])]);
3387 assemble_name_raw (asm_out_file, end_lab);
3388 fputc ('\n', asm_out_file);
3390 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
3391 xops[1] = GEN_INT (-PROBE_INTERVAL);
3392 output_asm_insn ("addl %0 = %1, %0", xops);
3393 fputs ("\t;;\n", asm_out_file);
3395 /* Probe at TEST_ADDR and branch. */
3396 output_asm_insn ("probe.w.fault %0, 0", xops);
3397 fprintf (asm_out_file, "\tbr ");
3398 assemble_name_raw (asm_out_file, loop_lab);
3399 fputc ('\n', asm_out_file);
3401 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
3403 return "";
3406 /* Called after register allocation to add any instructions needed for the
3407 prologue. Using a prologue insn is favored compared to putting all of the
3408 instructions in output_function_prologue(), since it allows the scheduler
3409 to intermix instructions with the saves of the caller saved registers. In
3410 some cases, it might be necessary to emit a barrier instruction as the last
3411 insn to prevent such scheduling.
3413 Also any insns generated here should have RTX_FRAME_RELATED_P(insn) = 1
3414 so that the debug info generation code can handle them properly.
3416 The register save area is laid out like so:
3417 cfa+16
3418 [ varargs spill area ]
3419 [ fr register spill area ]
3420 [ br register spill area ]
3421 [ ar register spill area ]
3422 [ pr register spill area ]
3423 [ gr register spill area ] */
3425 /* ??? Get inefficient code when the frame size is larger than can fit in an
3426 adds instruction. */
3428 void
3429 ia64_expand_prologue (void)
3431 rtx insn, ar_pfs_save_reg, ar_unat_save_reg;
3432 int i, epilogue_p, regno, alt_regno, cfa_off, n_varargs;
3433 rtx reg, alt_reg;
3435 ia64_compute_frame_size (get_frame_size ());
3436 last_scratch_gr_reg = 15;
3438 if (flag_stack_usage_info)
3439 current_function_static_stack_size = current_frame_info.total_size;
3441 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
3443 HOST_WIDE_INT size = current_frame_info.total_size;
3444 int bs_size = BACKING_STORE_SIZE (current_frame_info.n_input_regs
3445 + current_frame_info.n_local_regs);
3447 if (crtl->is_leaf && !cfun->calls_alloca)
3449 if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
3450 ia64_emit_probe_stack_range (STACK_CHECK_PROTECT,
3451 size - STACK_CHECK_PROTECT,
3452 bs_size);
3453 else if (size + bs_size > STACK_CHECK_PROTECT)
3454 ia64_emit_probe_stack_range (STACK_CHECK_PROTECT, 0, bs_size);
3456 else if (size + bs_size > 0)
3457 ia64_emit_probe_stack_range (STACK_CHECK_PROTECT, size, bs_size);
3460 if (dump_file)
3462 fprintf (dump_file, "ia64 frame related registers "
3463 "recorded in current_frame_info.r[]:\n");
3464 #define PRINTREG(a) if (current_frame_info.r[a]) \
3465 fprintf(dump_file, "%s = %d\n", #a, current_frame_info.r[a])
3466 PRINTREG(reg_fp);
3467 PRINTREG(reg_save_b0);
3468 PRINTREG(reg_save_pr);
3469 PRINTREG(reg_save_ar_pfs);
3470 PRINTREG(reg_save_ar_unat);
3471 PRINTREG(reg_save_ar_lc);
3472 PRINTREG(reg_save_gp);
3473 #undef PRINTREG
3476 /* If there is no epilogue, then we don't need some prologue insns.
3477 We need to avoid emitting the dead prologue insns, because flow
3478 will complain about them. */
3479 if (optimize)
3481 edge e;
3482 edge_iterator ei;
3484 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
3485 if ((e->flags & EDGE_FAKE) == 0
3486 && (e->flags & EDGE_FALLTHRU) != 0)
3487 break;
3488 epilogue_p = (e != NULL);
3490 else
3491 epilogue_p = 1;
3493 /* Set the local, input, and output register names. We need to do this
3494 for GNU libc, which creates crti.S/crtn.S by splitting initfini.c in
3495 half. If we use in/loc/out register names, then we get assembler errors
3496 in crtn.S because there is no alloc insn or regstk directive in there. */
3497 if (! TARGET_REG_NAMES)
3499 int inputs = current_frame_info.n_input_regs;
3500 int locals = current_frame_info.n_local_regs;
3501 int outputs = current_frame_info.n_output_regs;
3503 for (i = 0; i < inputs; i++)
3504 reg_names[IN_REG (i)] = ia64_reg_numbers[i];
3505 for (i = 0; i < locals; i++)
3506 reg_names[LOC_REG (i)] = ia64_reg_numbers[inputs + i];
3507 for (i = 0; i < outputs; i++)
3508 reg_names[OUT_REG (i)] = ia64_reg_numbers[inputs + locals + i];
3511 /* Set the frame pointer register name. The regnum is logically loc79,
3512 but of course we'll not have allocated that many locals. Rather than
3513 worrying about renumbering the existing rtxs, we adjust the name. */
3514 /* ??? This code means that we can never use one local register when
3515 there is a frame pointer. loc79 gets wasted in this case, as it is
3516 renamed to a register that will never be used. See also the try_locals
3517 code in find_gr_spill. */
3518 if (current_frame_info.r[reg_fp])
3520 const char *tmp = reg_names[HARD_FRAME_POINTER_REGNUM];
3521 reg_names[HARD_FRAME_POINTER_REGNUM]
3522 = reg_names[current_frame_info.r[reg_fp]];
3523 reg_names[current_frame_info.r[reg_fp]] = tmp;
3526 /* We don't need an alloc instruction if we've used no outputs or locals. */
3527 if (current_frame_info.n_local_regs == 0
3528 && current_frame_info.n_output_regs == 0
3529 && current_frame_info.n_input_regs <= crtl->args.info.int_regs
3530 && !TEST_HARD_REG_BIT (current_frame_info.mask, AR_PFS_REGNUM))
3532 /* If there is no alloc, but there are input registers used, then we
3533 need a .regstk directive. */
3534 current_frame_info.need_regstk = (TARGET_REG_NAMES != 0);
3535 ar_pfs_save_reg = NULL_RTX;
3537 else
3539 current_frame_info.need_regstk = 0;
3541 if (current_frame_info.r[reg_save_ar_pfs])
3543 regno = current_frame_info.r[reg_save_ar_pfs];
3544 reg_emitted (reg_save_ar_pfs);
3546 else
3547 regno = next_scratch_gr_reg ();
3548 ar_pfs_save_reg = gen_rtx_REG (DImode, regno);
3550 insn = emit_insn (gen_alloc (ar_pfs_save_reg,
3551 GEN_INT (current_frame_info.n_input_regs),
3552 GEN_INT (current_frame_info.n_local_regs),
3553 GEN_INT (current_frame_info.n_output_regs),
3554 GEN_INT (current_frame_info.n_rotate_regs)));
3555 if (current_frame_info.r[reg_save_ar_pfs])
3557 RTX_FRAME_RELATED_P (insn) = 1;
3558 add_reg_note (insn, REG_CFA_REGISTER,
3559 gen_rtx_SET (VOIDmode,
3560 ar_pfs_save_reg,
3561 gen_rtx_REG (DImode, AR_PFS_REGNUM)));
3565 /* Set up frame pointer, stack pointer, and spill iterators. */
3567 n_varargs = cfun->machine->n_varargs;
3568 setup_spill_pointers (current_frame_info.n_spilled + n_varargs,
3569 stack_pointer_rtx, 0);
3571 if (frame_pointer_needed)
3573 insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
3574 RTX_FRAME_RELATED_P (insn) = 1;
3576 /* Force the unwind info to recognize this as defining a new CFA,
3577 rather than some temp register setup. */
3578 add_reg_note (insn, REG_CFA_ADJUST_CFA, NULL_RTX);
3581 if (current_frame_info.total_size != 0)
3583 rtx frame_size_rtx = GEN_INT (- current_frame_info.total_size);
3584 rtx offset;
3586 if (satisfies_constraint_I (frame_size_rtx))
3587 offset = frame_size_rtx;
3588 else
3590 regno = next_scratch_gr_reg ();
3591 offset = gen_rtx_REG (DImode, regno);
3592 emit_move_insn (offset, frame_size_rtx);
3595 insn = emit_insn (gen_adddi3 (stack_pointer_rtx,
3596 stack_pointer_rtx, offset));
3598 if (! frame_pointer_needed)
3600 RTX_FRAME_RELATED_P (insn) = 1;
3601 add_reg_note (insn, REG_CFA_ADJUST_CFA,
3602 gen_rtx_SET (VOIDmode,
3603 stack_pointer_rtx,
3604 gen_rtx_PLUS (DImode,
3605 stack_pointer_rtx,
3606 frame_size_rtx)));
3609 /* ??? At this point we must generate a magic insn that appears to
3610 modify the stack pointer, the frame pointer, and all spill
3611 iterators. This would allow the most scheduling freedom. For
3612 now, just hard stop. */
3613 emit_insn (gen_blockage ());
3616 /* Must copy out ar.unat before doing any integer spills. */
3617 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
3619 if (current_frame_info.r[reg_save_ar_unat])
3621 ar_unat_save_reg
3622 = gen_rtx_REG (DImode, current_frame_info.r[reg_save_ar_unat]);
3623 reg_emitted (reg_save_ar_unat);
3625 else
3627 alt_regno = next_scratch_gr_reg ();
3628 ar_unat_save_reg = gen_rtx_REG (DImode, alt_regno);
3629 current_frame_info.gr_used_mask |= 1 << alt_regno;
3632 reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
3633 insn = emit_move_insn (ar_unat_save_reg, reg);
3634 if (current_frame_info.r[reg_save_ar_unat])
3636 RTX_FRAME_RELATED_P (insn) = 1;
3637 add_reg_note (insn, REG_CFA_REGISTER, NULL_RTX);
3640 /* Even if we're not going to generate an epilogue, we still
3641 need to save the register so that EH works. */
3642 if (! epilogue_p && current_frame_info.r[reg_save_ar_unat])
3643 emit_insn (gen_prologue_use (ar_unat_save_reg));
3645 else
3646 ar_unat_save_reg = NULL_RTX;
3648 /* Spill all varargs registers. Do this before spilling any GR registers,
3649 since we want the UNAT bits for the GR registers to override the UNAT
3650 bits from varargs, which we don't care about. */
3652 cfa_off = -16;
3653 for (regno = GR_ARG_FIRST + 7; n_varargs > 0; --n_varargs, --regno)
3655 reg = gen_rtx_REG (DImode, regno);
3656 do_spill (gen_gr_spill, reg, cfa_off += 8, NULL_RTX);
3659 /* Locate the bottom of the register save area. */
3660 cfa_off = (current_frame_info.spill_cfa_off
3661 + current_frame_info.spill_size
3662 + current_frame_info.extra_spill_size);
3664 /* Save the predicate register block either in a register or in memory. */
3665 if (TEST_HARD_REG_BIT (current_frame_info.mask, PR_REG (0)))
3667 reg = gen_rtx_REG (DImode, PR_REG (0));
3668 if (current_frame_info.r[reg_save_pr] != 0)
3670 alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_pr]);
3671 reg_emitted (reg_save_pr);
3672 insn = emit_move_insn (alt_reg, reg);
3674 /* ??? Denote pr spill/fill by a DImode move that modifies all
3675 64 hard registers. */
3676 RTX_FRAME_RELATED_P (insn) = 1;
3677 add_reg_note (insn, REG_CFA_REGISTER, NULL_RTX);
3679 /* Even if we're not going to generate an epilogue, we still
3680 need to save the register so that EH works. */
3681 if (! epilogue_p)
3682 emit_insn (gen_prologue_use (alt_reg));
3684 else
3686 alt_regno = next_scratch_gr_reg ();
3687 alt_reg = gen_rtx_REG (DImode, alt_regno);
3688 insn = emit_move_insn (alt_reg, reg);
3689 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
3690 cfa_off -= 8;
3694 /* Handle AR regs in numerical order. All of them get special handling. */
3695 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM)
3696 && current_frame_info.r[reg_save_ar_unat] == 0)
3698 reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
3699 do_spill (gen_movdi_x, ar_unat_save_reg, cfa_off, reg);
3700 cfa_off -= 8;
3703 /* The alloc insn already copied ar.pfs into a general register. The
3704 only thing we have to do now is copy that register to a stack slot
3705 if we'd not allocated a local register for the job. */
3706 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_PFS_REGNUM)
3707 && current_frame_info.r[reg_save_ar_pfs] == 0)
3709 reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
3710 do_spill (gen_movdi_x, ar_pfs_save_reg, cfa_off, reg);
3711 cfa_off -= 8;
3714 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_LC_REGNUM))
3716 reg = gen_rtx_REG (DImode, AR_LC_REGNUM);
3717 if (current_frame_info.r[reg_save_ar_lc] != 0)
3719 alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_ar_lc]);
3720 reg_emitted (reg_save_ar_lc);
3721 insn = emit_move_insn (alt_reg, reg);
3722 RTX_FRAME_RELATED_P (insn) = 1;
3723 add_reg_note (insn, REG_CFA_REGISTER, NULL_RTX);
3725 /* Even if we're not going to generate an epilogue, we still
3726 need to save the register so that EH works. */
3727 if (! epilogue_p)
3728 emit_insn (gen_prologue_use (alt_reg));
3730 else
3732 alt_regno = next_scratch_gr_reg ();
3733 alt_reg = gen_rtx_REG (DImode, alt_regno);
3734 emit_move_insn (alt_reg, reg);
3735 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
3736 cfa_off -= 8;
3740 /* Save the return pointer. */
3741 if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
3743 reg = gen_rtx_REG (DImode, BR_REG (0));
3744 if (current_frame_info.r[reg_save_b0] != 0)
3746 alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_b0]);
3747 reg_emitted (reg_save_b0);
3748 insn = emit_move_insn (alt_reg, reg);
3749 RTX_FRAME_RELATED_P (insn) = 1;
3750 add_reg_note (insn, REG_CFA_REGISTER,
3751 gen_rtx_SET (VOIDmode, alt_reg, pc_rtx));
3753 /* Even if we're not going to generate an epilogue, we still
3754 need to save the register so that EH works. */
3755 if (! epilogue_p)
3756 emit_insn (gen_prologue_use (alt_reg));
3758 else
3760 alt_regno = next_scratch_gr_reg ();
3761 alt_reg = gen_rtx_REG (DImode, alt_regno);
3762 emit_move_insn (alt_reg, reg);
3763 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
3764 cfa_off -= 8;
3768 if (current_frame_info.r[reg_save_gp])
3770 reg_emitted (reg_save_gp);
3771 insn = emit_move_insn (gen_rtx_REG (DImode,
3772 current_frame_info.r[reg_save_gp]),
3773 pic_offset_table_rtx);
3776 /* We should now be at the base of the gr/br/fr spill area. */
3777 gcc_assert (cfa_off == (current_frame_info.spill_cfa_off
3778 + current_frame_info.spill_size));
3780 /* Spill all general registers. */
3781 for (regno = GR_REG (1); regno <= GR_REG (31); ++regno)
3782 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3784 reg = gen_rtx_REG (DImode, regno);
3785 do_spill (gen_gr_spill, reg, cfa_off, reg);
3786 cfa_off -= 8;
3789 /* Spill the rest of the BR registers. */
3790 for (regno = BR_REG (1); regno <= BR_REG (7); ++regno)
3791 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3793 alt_regno = next_scratch_gr_reg ();
3794 alt_reg = gen_rtx_REG (DImode, alt_regno);
3795 reg = gen_rtx_REG (DImode, regno);
3796 emit_move_insn (alt_reg, reg);
3797 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
3798 cfa_off -= 8;
3801 /* Align the frame and spill all FR registers. */
3802 for (regno = FR_REG (2); regno <= FR_REG (127); ++regno)
3803 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3805 gcc_assert (!(cfa_off & 15));
3806 reg = gen_rtx_REG (XFmode, regno);
3807 do_spill (gen_fr_spill_x, reg, cfa_off, reg);
3808 cfa_off -= 16;
3811 gcc_assert (cfa_off == current_frame_info.spill_cfa_off);
3813 finish_spill_pointers ();
3816 /* Output the textual info surrounding the prologue. */
3818 void
3819 ia64_start_function (FILE *file, const char *fnname,
3820 tree decl ATTRIBUTE_UNUSED)
3822 #if TARGET_ABI_OPEN_VMS
3823 vms_start_function (fnname);
3824 #endif
3826 fputs ("\t.proc ", file);
3827 assemble_name (file, fnname);
3828 fputc ('\n', file);
3829 ASM_OUTPUT_LABEL (file, fnname);
3832 /* Called after register allocation to add any instructions needed for the
3833 epilogue. Using an epilogue insn is favored compared to putting all of the
3834 instructions in output_function_prologue(), since it allows the scheduler
3835 to intermix instructions with the saves of the caller saved registers. In
3836 some cases, it might be necessary to emit a barrier instruction as the last
3837 insn to prevent such scheduling. */
3839 void
3840 ia64_expand_epilogue (int sibcall_p)
3842 rtx insn, reg, alt_reg, ar_unat_save_reg;
3843 int regno, alt_regno, cfa_off;
3845 ia64_compute_frame_size (get_frame_size ());
3847 /* If there is a frame pointer, then we use it instead of the stack
3848 pointer, so that the stack pointer does not need to be valid when
3849 the epilogue starts. See EXIT_IGNORE_STACK. */
3850 if (frame_pointer_needed)
3851 setup_spill_pointers (current_frame_info.n_spilled,
3852 hard_frame_pointer_rtx, 0);
3853 else
3854 setup_spill_pointers (current_frame_info.n_spilled, stack_pointer_rtx,
3855 current_frame_info.total_size);
3857 if (current_frame_info.total_size != 0)
3859 /* ??? At this point we must generate a magic insn that appears to
3860 modify the spill iterators and the frame pointer. This would
3861 allow the most scheduling freedom. For now, just hard stop. */
3862 emit_insn (gen_blockage ());
3865 /* Locate the bottom of the register save area. */
3866 cfa_off = (current_frame_info.spill_cfa_off
3867 + current_frame_info.spill_size
3868 + current_frame_info.extra_spill_size);
3870 /* Restore the predicate registers. */
3871 if (TEST_HARD_REG_BIT (current_frame_info.mask, PR_REG (0)))
3873 if (current_frame_info.r[reg_save_pr] != 0)
3875 alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_pr]);
3876 reg_emitted (reg_save_pr);
3878 else
3880 alt_regno = next_scratch_gr_reg ();
3881 alt_reg = gen_rtx_REG (DImode, alt_regno);
3882 do_restore (gen_movdi_x, alt_reg, cfa_off);
3883 cfa_off -= 8;
3885 reg = gen_rtx_REG (DImode, PR_REG (0));
3886 emit_move_insn (reg, alt_reg);
3889 /* Restore the application registers. */
3891 /* Load the saved unat from the stack, but do not restore it until
3892 after the GRs have been restored. */
3893 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
3895 if (current_frame_info.r[reg_save_ar_unat] != 0)
3897 ar_unat_save_reg
3898 = gen_rtx_REG (DImode, current_frame_info.r[reg_save_ar_unat]);
3899 reg_emitted (reg_save_ar_unat);
3901 else
3903 alt_regno = next_scratch_gr_reg ();
3904 ar_unat_save_reg = gen_rtx_REG (DImode, alt_regno);
3905 current_frame_info.gr_used_mask |= 1 << alt_regno;
3906 do_restore (gen_movdi_x, ar_unat_save_reg, cfa_off);
3907 cfa_off -= 8;
3910 else
3911 ar_unat_save_reg = NULL_RTX;
3913 if (current_frame_info.r[reg_save_ar_pfs] != 0)
3915 reg_emitted (reg_save_ar_pfs);
3916 alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_ar_pfs]);
3917 reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
3918 emit_move_insn (reg, alt_reg);
3920 else if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_PFS_REGNUM))
3922 alt_regno = next_scratch_gr_reg ();
3923 alt_reg = gen_rtx_REG (DImode, alt_regno);
3924 do_restore (gen_movdi_x, alt_reg, cfa_off);
3925 cfa_off -= 8;
3926 reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
3927 emit_move_insn (reg, alt_reg);
3930 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_LC_REGNUM))
3932 if (current_frame_info.r[reg_save_ar_lc] != 0)
3934 alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_ar_lc]);
3935 reg_emitted (reg_save_ar_lc);
3937 else
3939 alt_regno = next_scratch_gr_reg ();
3940 alt_reg = gen_rtx_REG (DImode, alt_regno);
3941 do_restore (gen_movdi_x, alt_reg, cfa_off);
3942 cfa_off -= 8;
3944 reg = gen_rtx_REG (DImode, AR_LC_REGNUM);
3945 emit_move_insn (reg, alt_reg);
3948 /* Restore the return pointer. */
3949 if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
3951 if (current_frame_info.r[reg_save_b0] != 0)
3953 alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_b0]);
3954 reg_emitted (reg_save_b0);
3956 else
3958 alt_regno = next_scratch_gr_reg ();
3959 alt_reg = gen_rtx_REG (DImode, alt_regno);
3960 do_restore (gen_movdi_x, alt_reg, cfa_off);
3961 cfa_off -= 8;
3963 reg = gen_rtx_REG (DImode, BR_REG (0));
3964 emit_move_insn (reg, alt_reg);
3967 /* We should now be at the base of the gr/br/fr spill area. */
3968 gcc_assert (cfa_off == (current_frame_info.spill_cfa_off
3969 + current_frame_info.spill_size));
3971 /* The GP may be stored on the stack in the prologue, but it's
3972 never restored in the epilogue. Skip the stack slot. */
3973 if (TEST_HARD_REG_BIT (current_frame_info.mask, GR_REG (1)))
3974 cfa_off -= 8;
3976 /* Restore all general registers. */
3977 for (regno = GR_REG (2); regno <= GR_REG (31); ++regno)
3978 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3980 reg = gen_rtx_REG (DImode, regno);
3981 do_restore (gen_gr_restore, reg, cfa_off);
3982 cfa_off -= 8;
3985 /* Restore the branch registers. */
3986 for (regno = BR_REG (1); regno <= BR_REG (7); ++regno)
3987 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3989 alt_regno = next_scratch_gr_reg ();
3990 alt_reg = gen_rtx_REG (DImode, alt_regno);
3991 do_restore (gen_movdi_x, alt_reg, cfa_off);
3992 cfa_off -= 8;
3993 reg = gen_rtx_REG (DImode, regno);
3994 emit_move_insn (reg, alt_reg);
3997 /* Restore floating point registers. */
3998 for (regno = FR_REG (2); regno <= FR_REG (127); ++regno)
3999 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
4001 gcc_assert (!(cfa_off & 15));
4002 reg = gen_rtx_REG (XFmode, regno);
4003 do_restore (gen_fr_restore_x, reg, cfa_off);
4004 cfa_off -= 16;
4007 /* Restore ar.unat for real. */
4008 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
4010 reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
4011 emit_move_insn (reg, ar_unat_save_reg);
4014 gcc_assert (cfa_off == current_frame_info.spill_cfa_off);
4016 finish_spill_pointers ();
4018 if (current_frame_info.total_size
4019 || cfun->machine->ia64_eh_epilogue_sp
4020 || frame_pointer_needed)
4022 /* ??? At this point we must generate a magic insn that appears to
4023 modify the spill iterators, the stack pointer, and the frame
4024 pointer. This would allow the most scheduling freedom. For now,
4025 just hard stop. */
4026 emit_insn (gen_blockage ());
4029 if (cfun->machine->ia64_eh_epilogue_sp)
4030 emit_move_insn (stack_pointer_rtx, cfun->machine->ia64_eh_epilogue_sp);
4031 else if (frame_pointer_needed)
4033 insn = emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
4034 RTX_FRAME_RELATED_P (insn) = 1;
4035 add_reg_note (insn, REG_CFA_ADJUST_CFA, NULL);
4037 else if (current_frame_info.total_size)
4039 rtx offset, frame_size_rtx;
4041 frame_size_rtx = GEN_INT (current_frame_info.total_size);
4042 if (satisfies_constraint_I (frame_size_rtx))
4043 offset = frame_size_rtx;
4044 else
4046 regno = next_scratch_gr_reg ();
4047 offset = gen_rtx_REG (DImode, regno);
4048 emit_move_insn (offset, frame_size_rtx);
4051 insn = emit_insn (gen_adddi3 (stack_pointer_rtx, stack_pointer_rtx,
4052 offset));
4054 RTX_FRAME_RELATED_P (insn) = 1;
4055 add_reg_note (insn, REG_CFA_ADJUST_CFA,
4056 gen_rtx_SET (VOIDmode,
4057 stack_pointer_rtx,
4058 gen_rtx_PLUS (DImode,
4059 stack_pointer_rtx,
4060 frame_size_rtx)));
4063 if (cfun->machine->ia64_eh_epilogue_bsp)
4064 emit_insn (gen_set_bsp (cfun->machine->ia64_eh_epilogue_bsp));
4066 if (! sibcall_p)
4067 emit_jump_insn (gen_return_internal (gen_rtx_REG (DImode, BR_REG (0))));
4068 else
4070 int fp = GR_REG (2);
4071 /* We need a throw away register here, r0 and r1 are reserved,
4072 so r2 is the first available call clobbered register. If
4073 there was a frame_pointer register, we may have swapped the
4074 names of r2 and HARD_FRAME_POINTER_REGNUM, so we have to make
4075 sure we're using the string "r2" when emitting the register
4076 name for the assembler. */
4077 if (current_frame_info.r[reg_fp]
4078 && current_frame_info.r[reg_fp] == GR_REG (2))
4079 fp = HARD_FRAME_POINTER_REGNUM;
4081 /* We must emit an alloc to force the input registers to become output
4082 registers. Otherwise, if the callee tries to pass its parameters
4083 through to another call without an intervening alloc, then these
4084 values get lost. */
4085 /* ??? We don't need to preserve all input registers. We only need to
4086 preserve those input registers used as arguments to the sibling call.
4087 It is unclear how to compute that number here. */
4088 if (current_frame_info.n_input_regs != 0)
4090 rtx n_inputs = GEN_INT (current_frame_info.n_input_regs);
4092 insn = emit_insn (gen_alloc (gen_rtx_REG (DImode, fp),
4093 const0_rtx, const0_rtx,
4094 n_inputs, const0_rtx));
4095 RTX_FRAME_RELATED_P (insn) = 1;
4097 /* ??? We need to mark the alloc as frame-related so that it gets
4098 passed into ia64_asm_unwind_emit for ia64-specific unwinding.
4099 But there's nothing dwarf2 related to be done wrt the register
4100 windows. If we do nothing, dwarf2out will abort on the UNSPEC;
4101 the empty parallel means dwarf2out will not see anything. */
4102 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
4103 gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (0)));
4108 /* Return 1 if br.ret can do all the work required to return from a
4109 function. */
4112 ia64_direct_return (void)
4114 if (reload_completed && ! frame_pointer_needed)
4116 ia64_compute_frame_size (get_frame_size ());
4118 return (current_frame_info.total_size == 0
4119 && current_frame_info.n_spilled == 0
4120 && current_frame_info.r[reg_save_b0] == 0
4121 && current_frame_info.r[reg_save_pr] == 0
4122 && current_frame_info.r[reg_save_ar_pfs] == 0
4123 && current_frame_info.r[reg_save_ar_unat] == 0
4124 && current_frame_info.r[reg_save_ar_lc] == 0);
4126 return 0;
4129 /* Return the magic cookie that we use to hold the return address
4130 during early compilation. */
4133 ia64_return_addr_rtx (HOST_WIDE_INT count, rtx frame ATTRIBUTE_UNUSED)
4135 if (count != 0)
4136 return NULL;
4137 return gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_RET_ADDR);
4140 /* Split this value after reload, now that we know where the return
4141 address is saved. */
4143 void
4144 ia64_split_return_addr_rtx (rtx dest)
4146 rtx src;
4148 if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
4150 if (current_frame_info.r[reg_save_b0] != 0)
4152 src = gen_rtx_REG (DImode, current_frame_info.r[reg_save_b0]);
4153 reg_emitted (reg_save_b0);
4155 else
4157 HOST_WIDE_INT off;
4158 unsigned int regno;
4159 rtx off_r;
4161 /* Compute offset from CFA for BR0. */
4162 /* ??? Must be kept in sync with ia64_expand_prologue. */
4163 off = (current_frame_info.spill_cfa_off
4164 + current_frame_info.spill_size);
4165 for (regno = GR_REG (1); regno <= GR_REG (31); ++regno)
4166 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
4167 off -= 8;
4169 /* Convert CFA offset to a register based offset. */
4170 if (frame_pointer_needed)
4171 src = hard_frame_pointer_rtx;
4172 else
4174 src = stack_pointer_rtx;
4175 off += current_frame_info.total_size;
4178 /* Load address into scratch register. */
4179 off_r = GEN_INT (off);
4180 if (satisfies_constraint_I (off_r))
4181 emit_insn (gen_adddi3 (dest, src, off_r));
4182 else
4184 emit_move_insn (dest, off_r);
4185 emit_insn (gen_adddi3 (dest, src, dest));
4188 src = gen_rtx_MEM (Pmode, dest);
4191 else
4192 src = gen_rtx_REG (DImode, BR_REG (0));
4194 emit_move_insn (dest, src);
4198 ia64_hard_regno_rename_ok (int from, int to)
4200 /* Don't clobber any of the registers we reserved for the prologue. */
4201 unsigned int r;
4203 for (r = reg_fp; r <= reg_save_ar_lc; r++)
4204 if (to == current_frame_info.r[r]
4205 || from == current_frame_info.r[r]
4206 || to == emitted_frame_related_regs[r]
4207 || from == emitted_frame_related_regs[r])
4208 return 0;
4210 /* Don't use output registers outside the register frame. */
4211 if (OUT_REGNO_P (to) && to >= OUT_REG (current_frame_info.n_output_regs))
4212 return 0;
4214 /* Retain even/oddness on predicate register pairs. */
4215 if (PR_REGNO_P (from) && PR_REGNO_P (to))
4216 return (from & 1) == (to & 1);
4218 return 1;
4221 /* Target hook for assembling integer objects. Handle word-sized
4222 aligned objects and detect the cases when @fptr is needed. */
4224 static bool
4225 ia64_assemble_integer (rtx x, unsigned int size, int aligned_p)
4227 if (size == POINTER_SIZE / BITS_PER_UNIT
4228 && !(TARGET_NO_PIC || TARGET_AUTO_PIC)
4229 && GET_CODE (x) == SYMBOL_REF
4230 && SYMBOL_REF_FUNCTION_P (x))
4232 static const char * const directive[2][2] = {
4233 /* 64-bit pointer */ /* 32-bit pointer */
4234 { "\tdata8.ua\t@fptr(", "\tdata4.ua\t@fptr("}, /* unaligned */
4235 { "\tdata8\t@fptr(", "\tdata4\t@fptr("} /* aligned */
4237 fputs (directive[(aligned_p != 0)][POINTER_SIZE == 32], asm_out_file);
4238 output_addr_const (asm_out_file, x);
4239 fputs (")\n", asm_out_file);
4240 return true;
4242 return default_assemble_integer (x, size, aligned_p);
4245 /* Emit the function prologue. */
4247 static void
4248 ia64_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
4250 int mask, grsave, grsave_prev;
4252 if (current_frame_info.need_regstk)
4253 fprintf (file, "\t.regstk %d, %d, %d, %d\n",
4254 current_frame_info.n_input_regs,
4255 current_frame_info.n_local_regs,
4256 current_frame_info.n_output_regs,
4257 current_frame_info.n_rotate_regs);
4259 if (ia64_except_unwind_info (&global_options) != UI_TARGET)
4260 return;
4262 /* Emit the .prologue directive. */
4264 mask = 0;
4265 grsave = grsave_prev = 0;
4266 if (current_frame_info.r[reg_save_b0] != 0)
4268 mask |= 8;
4269 grsave = grsave_prev = current_frame_info.r[reg_save_b0];
4271 if (current_frame_info.r[reg_save_ar_pfs] != 0
4272 && (grsave_prev == 0
4273 || current_frame_info.r[reg_save_ar_pfs] == grsave_prev + 1))
4275 mask |= 4;
4276 if (grsave_prev == 0)
4277 grsave = current_frame_info.r[reg_save_ar_pfs];
4278 grsave_prev = current_frame_info.r[reg_save_ar_pfs];
4280 if (current_frame_info.r[reg_fp] != 0
4281 && (grsave_prev == 0
4282 || current_frame_info.r[reg_fp] == grsave_prev + 1))
4284 mask |= 2;
4285 if (grsave_prev == 0)
4286 grsave = HARD_FRAME_POINTER_REGNUM;
4287 grsave_prev = current_frame_info.r[reg_fp];
4289 if (current_frame_info.r[reg_save_pr] != 0
4290 && (grsave_prev == 0
4291 || current_frame_info.r[reg_save_pr] == grsave_prev + 1))
4293 mask |= 1;
4294 if (grsave_prev == 0)
4295 grsave = current_frame_info.r[reg_save_pr];
4298 if (mask && TARGET_GNU_AS)
4299 fprintf (file, "\t.prologue %d, %d\n", mask,
4300 ia64_dbx_register_number (grsave));
4301 else
4302 fputs ("\t.prologue\n", file);
4304 /* Emit a .spill directive, if necessary, to relocate the base of
4305 the register spill area. */
4306 if (current_frame_info.spill_cfa_off != -16)
4307 fprintf (file, "\t.spill %ld\n",
4308 (long) (current_frame_info.spill_cfa_off
4309 + current_frame_info.spill_size));
4312 /* Emit the .body directive at the scheduled end of the prologue. */
4314 static void
4315 ia64_output_function_end_prologue (FILE *file)
4317 if (ia64_except_unwind_info (&global_options) != UI_TARGET)
4318 return;
4320 fputs ("\t.body\n", file);
4323 /* Emit the function epilogue. */
4325 static void
4326 ia64_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
4327 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
4329 int i;
4331 if (current_frame_info.r[reg_fp])
4333 const char *tmp = reg_names[HARD_FRAME_POINTER_REGNUM];
4334 reg_names[HARD_FRAME_POINTER_REGNUM]
4335 = reg_names[current_frame_info.r[reg_fp]];
4336 reg_names[current_frame_info.r[reg_fp]] = tmp;
4337 reg_emitted (reg_fp);
4339 if (! TARGET_REG_NAMES)
4341 for (i = 0; i < current_frame_info.n_input_regs; i++)
4342 reg_names[IN_REG (i)] = ia64_input_reg_names[i];
4343 for (i = 0; i < current_frame_info.n_local_regs; i++)
4344 reg_names[LOC_REG (i)] = ia64_local_reg_names[i];
4345 for (i = 0; i < current_frame_info.n_output_regs; i++)
4346 reg_names[OUT_REG (i)] = ia64_output_reg_names[i];
4349 current_frame_info.initialized = 0;
4353 ia64_dbx_register_number (int regno)
4355 /* In ia64_expand_prologue we quite literally renamed the frame pointer
4356 from its home at loc79 to something inside the register frame. We
4357 must perform the same renumbering here for the debug info. */
4358 if (current_frame_info.r[reg_fp])
4360 if (regno == HARD_FRAME_POINTER_REGNUM)
4361 regno = current_frame_info.r[reg_fp];
4362 else if (regno == current_frame_info.r[reg_fp])
4363 regno = HARD_FRAME_POINTER_REGNUM;
4366 if (IN_REGNO_P (regno))
4367 return 32 + regno - IN_REG (0);
4368 else if (LOC_REGNO_P (regno))
4369 return 32 + current_frame_info.n_input_regs + regno - LOC_REG (0);
4370 else if (OUT_REGNO_P (regno))
4371 return (32 + current_frame_info.n_input_regs
4372 + current_frame_info.n_local_regs + regno - OUT_REG (0));
4373 else
4374 return regno;
4377 /* Implement TARGET_TRAMPOLINE_INIT.
4379 The trampoline should set the static chain pointer to value placed
4380 into the trampoline and should branch to the specified routine.
4381 To make the normal indirect-subroutine calling convention work,
4382 the trampoline must look like a function descriptor; the first
4383 word being the target address and the second being the target's
4384 global pointer.
4386 We abuse the concept of a global pointer by arranging for it
4387 to point to the data we need to load. The complete trampoline
4388 has the following form:
4390 +-------------------+ \
4391 TRAMP: | __ia64_trampoline | |
4392 +-------------------+ > fake function descriptor
4393 | TRAMP+16 | |
4394 +-------------------+ /
4395 | target descriptor |
4396 +-------------------+
4397 | static link |
4398 +-------------------+
4401 static void
4402 ia64_trampoline_init (rtx m_tramp, tree fndecl, rtx static_chain)
4404 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
4405 rtx addr, addr_reg, tramp, eight = GEN_INT (8);
4407 /* The Intel assembler requires that the global __ia64_trampoline symbol
4408 be declared explicitly */
4409 if (!TARGET_GNU_AS)
4411 static bool declared_ia64_trampoline = false;
4413 if (!declared_ia64_trampoline)
4415 declared_ia64_trampoline = true;
4416 (*targetm.asm_out.globalize_label) (asm_out_file,
4417 "__ia64_trampoline");
4421 /* Make sure addresses are Pmode even if we are in ILP32 mode. */
4422 addr = convert_memory_address (Pmode, XEXP (m_tramp, 0));
4423 fnaddr = convert_memory_address (Pmode, fnaddr);
4424 static_chain = convert_memory_address (Pmode, static_chain);
4426 /* Load up our iterator. */
4427 addr_reg = copy_to_reg (addr);
4428 m_tramp = adjust_automodify_address (m_tramp, Pmode, addr_reg, 0);
4430 /* The first two words are the fake descriptor:
4431 __ia64_trampoline, ADDR+16. */
4432 tramp = gen_rtx_SYMBOL_REF (Pmode, "__ia64_trampoline");
4433 if (TARGET_ABI_OPEN_VMS)
4435 /* HP decided to break the ELF ABI on VMS (to deal with an ambiguity
4436 in the Macro-32 compiler) and changed the semantics of the LTOFF22
4437 relocation against function symbols to make it identical to the
4438 LTOFF_FPTR22 relocation. Emit the latter directly to stay within
4439 strict ELF and dereference to get the bare code address. */
4440 rtx reg = gen_reg_rtx (Pmode);
4441 SYMBOL_REF_FLAGS (tramp) |= SYMBOL_FLAG_FUNCTION;
4442 emit_move_insn (reg, tramp);
4443 emit_move_insn (reg, gen_rtx_MEM (Pmode, reg));
4444 tramp = reg;
4446 emit_move_insn (m_tramp, tramp);
4447 emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
4448 m_tramp = adjust_automodify_address (m_tramp, VOIDmode, NULL, 8);
4450 emit_move_insn (m_tramp, force_reg (Pmode, plus_constant (Pmode, addr, 16)));
4451 emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
4452 m_tramp = adjust_automodify_address (m_tramp, VOIDmode, NULL, 8);
4454 /* The third word is the target descriptor. */
4455 emit_move_insn (m_tramp, force_reg (Pmode, fnaddr));
4456 emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
4457 m_tramp = adjust_automodify_address (m_tramp, VOIDmode, NULL, 8);
4459 /* The fourth word is the static chain. */
4460 emit_move_insn (m_tramp, static_chain);
4463 /* Do any needed setup for a variadic function. CUM has not been updated
4464 for the last named argument which has type TYPE and mode MODE.
4466 We generate the actual spill instructions during prologue generation. */
4468 static void
4469 ia64_setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
4470 tree type, int * pretend_size,
4471 int second_time ATTRIBUTE_UNUSED)
4473 CUMULATIVE_ARGS next_cum = *get_cumulative_args (cum);
4475 /* Skip the current argument. */
4476 ia64_function_arg_advance (pack_cumulative_args (&next_cum), mode, type, 1);
4478 if (next_cum.words < MAX_ARGUMENT_SLOTS)
4480 int n = MAX_ARGUMENT_SLOTS - next_cum.words;
4481 *pretend_size = n * UNITS_PER_WORD;
4482 cfun->machine->n_varargs = n;
4486 /* Check whether TYPE is a homogeneous floating point aggregate. If
4487 it is, return the mode of the floating point type that appears
4488 in all leafs. If it is not, return VOIDmode.
4490 An aggregate is a homogeneous floating point aggregate is if all
4491 fields/elements in it have the same floating point type (e.g,
4492 SFmode). 128-bit quad-precision floats are excluded.
4494 Variable sized aggregates should never arrive here, since we should
4495 have already decided to pass them by reference. Top-level zero-sized
4496 aggregates are excluded because our parallels crash the middle-end. */
4498 static enum machine_mode
4499 hfa_element_mode (const_tree type, bool nested)
4501 enum machine_mode element_mode = VOIDmode;
4502 enum machine_mode mode;
4503 enum tree_code code = TREE_CODE (type);
4504 int know_element_mode = 0;
4505 tree t;
4507 if (!nested && (!TYPE_SIZE (type) || integer_zerop (TYPE_SIZE (type))))
4508 return VOIDmode;
4510 switch (code)
4512 case VOID_TYPE: case INTEGER_TYPE: case ENUMERAL_TYPE:
4513 case BOOLEAN_TYPE: case POINTER_TYPE:
4514 case OFFSET_TYPE: case REFERENCE_TYPE: case METHOD_TYPE:
4515 case LANG_TYPE: case FUNCTION_TYPE:
4516 return VOIDmode;
4518 /* Fortran complex types are supposed to be HFAs, so we need to handle
4519 gcc's COMPLEX_TYPEs as HFAs. We need to exclude the integral complex
4520 types though. */
4521 case COMPLEX_TYPE:
4522 if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_COMPLEX_FLOAT
4523 && TYPE_MODE (type) != TCmode)
4524 return GET_MODE_INNER (TYPE_MODE (type));
4525 else
4526 return VOIDmode;
4528 case REAL_TYPE:
4529 /* We want to return VOIDmode for raw REAL_TYPEs, but the actual
4530 mode if this is contained within an aggregate. */
4531 if (nested && TYPE_MODE (type) != TFmode)
4532 return TYPE_MODE (type);
4533 else
4534 return VOIDmode;
4536 case ARRAY_TYPE:
4537 return hfa_element_mode (TREE_TYPE (type), 1);
4539 case RECORD_TYPE:
4540 case UNION_TYPE:
4541 case QUAL_UNION_TYPE:
4542 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
4544 if (TREE_CODE (t) != FIELD_DECL)
4545 continue;
4547 mode = hfa_element_mode (TREE_TYPE (t), 1);
4548 if (know_element_mode)
4550 if (mode != element_mode)
4551 return VOIDmode;
4553 else if (GET_MODE_CLASS (mode) != MODE_FLOAT)
4554 return VOIDmode;
4555 else
4557 know_element_mode = 1;
4558 element_mode = mode;
4561 return element_mode;
4563 default:
4564 /* If we reach here, we probably have some front-end specific type
4565 that the backend doesn't know about. This can happen via the
4566 aggregate_value_p call in init_function_start. All we can do is
4567 ignore unknown tree types. */
4568 return VOIDmode;
4571 return VOIDmode;
4574 /* Return the number of words required to hold a quantity of TYPE and MODE
4575 when passed as an argument. */
4576 static int
4577 ia64_function_arg_words (const_tree type, enum machine_mode mode)
4579 int words;
4581 if (mode == BLKmode)
4582 words = int_size_in_bytes (type);
4583 else
4584 words = GET_MODE_SIZE (mode);
4586 return (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD; /* round up */
4589 /* Return the number of registers that should be skipped so the current
4590 argument (described by TYPE and WORDS) will be properly aligned.
4592 Integer and float arguments larger than 8 bytes start at the next
4593 even boundary. Aggregates larger than 8 bytes start at the next
4594 even boundary if the aggregate has 16 byte alignment. Note that
4595 in the 32-bit ABI, TImode and TFmode have only 8-byte alignment
4596 but are still to be aligned in registers.
4598 ??? The ABI does not specify how to handle aggregates with
4599 alignment from 9 to 15 bytes, or greater than 16. We handle them
4600 all as if they had 16 byte alignment. Such aggregates can occur
4601 only if gcc extensions are used. */
4602 static int
4603 ia64_function_arg_offset (const CUMULATIVE_ARGS *cum,
4604 const_tree type, int words)
4606 /* No registers are skipped on VMS. */
4607 if (TARGET_ABI_OPEN_VMS || (cum->words & 1) == 0)
4608 return 0;
4610 if (type
4611 && TREE_CODE (type) != INTEGER_TYPE
4612 && TREE_CODE (type) != REAL_TYPE)
4613 return TYPE_ALIGN (type) > 8 * BITS_PER_UNIT;
4614 else
4615 return words > 1;
4618 /* Return rtx for register where argument is passed, or zero if it is passed
4619 on the stack. */
4620 /* ??? 128-bit quad-precision floats are always passed in general
4621 registers. */
4623 static rtx
4624 ia64_function_arg_1 (cumulative_args_t cum_v, enum machine_mode mode,
4625 const_tree type, bool named, bool incoming)
4627 const CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
4629 int basereg = (incoming ? GR_ARG_FIRST : AR_ARG_FIRST);
4630 int words = ia64_function_arg_words (type, mode);
4631 int offset = ia64_function_arg_offset (cum, type, words);
4632 enum machine_mode hfa_mode = VOIDmode;
4634 /* For OPEN VMS, emit the instruction setting up the argument register here,
4635 when we know this will be together with the other arguments setup related
4636 insns. This is not the conceptually best place to do this, but this is
4637 the easiest as we have convenient access to cumulative args info. */
4639 if (TARGET_ABI_OPEN_VMS && mode == VOIDmode && type == void_type_node
4640 && named == 1)
4642 unsigned HOST_WIDE_INT regval = cum->words;
4643 int i;
4645 for (i = 0; i < 8; i++)
4646 regval |= ((int) cum->atypes[i]) << (i * 3 + 8);
4648 emit_move_insn (gen_rtx_REG (DImode, GR_REG (25)),
4649 GEN_INT (regval));
4652 /* If all argument slots are used, then it must go on the stack. */
4653 if (cum->words + offset >= MAX_ARGUMENT_SLOTS)
4654 return 0;
4656 /* On OpenVMS argument is either in Rn or Fn. */
4657 if (TARGET_ABI_OPEN_VMS)
4659 if (FLOAT_MODE_P (mode))
4660 return gen_rtx_REG (mode, FR_ARG_FIRST + cum->words);
4661 else
4662 return gen_rtx_REG (mode, basereg + cum->words);
4665 /* Check for and handle homogeneous FP aggregates. */
4666 if (type)
4667 hfa_mode = hfa_element_mode (type, 0);
4669 /* Unnamed prototyped hfas are passed as usual. Named prototyped hfas
4670 and unprototyped hfas are passed specially. */
4671 if (hfa_mode != VOIDmode && (! cum->prototype || named))
4673 rtx loc[16];
4674 int i = 0;
4675 int fp_regs = cum->fp_regs;
4676 int int_regs = cum->words + offset;
4677 int hfa_size = GET_MODE_SIZE (hfa_mode);
4678 int byte_size;
4679 int args_byte_size;
4681 /* If prototyped, pass it in FR regs then GR regs.
4682 If not prototyped, pass it in both FR and GR regs.
4684 If this is an SFmode aggregate, then it is possible to run out of
4685 FR regs while GR regs are still left. In that case, we pass the
4686 remaining part in the GR regs. */
4688 /* Fill the FP regs. We do this always. We stop if we reach the end
4689 of the argument, the last FP register, or the last argument slot. */
4691 byte_size = ((mode == BLKmode)
4692 ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
4693 args_byte_size = int_regs * UNITS_PER_WORD;
4694 offset = 0;
4695 for (; (offset < byte_size && fp_regs < MAX_ARGUMENT_SLOTS
4696 && args_byte_size < (MAX_ARGUMENT_SLOTS * UNITS_PER_WORD)); i++)
4698 loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
4699 gen_rtx_REG (hfa_mode, (FR_ARG_FIRST
4700 + fp_regs)),
4701 GEN_INT (offset));
4702 offset += hfa_size;
4703 args_byte_size += hfa_size;
4704 fp_regs++;
4707 /* If no prototype, then the whole thing must go in GR regs. */
4708 if (! cum->prototype)
4709 offset = 0;
4710 /* If this is an SFmode aggregate, then we might have some left over
4711 that needs to go in GR regs. */
4712 else if (byte_size != offset)
4713 int_regs += offset / UNITS_PER_WORD;
4715 /* Fill in the GR regs. We must use DImode here, not the hfa mode. */
4717 for (; offset < byte_size && int_regs < MAX_ARGUMENT_SLOTS; i++)
4719 enum machine_mode gr_mode = DImode;
4720 unsigned int gr_size;
4722 /* If we have an odd 4 byte hunk because we ran out of FR regs,
4723 then this goes in a GR reg left adjusted/little endian, right
4724 adjusted/big endian. */
4725 /* ??? Currently this is handled wrong, because 4-byte hunks are
4726 always right adjusted/little endian. */
4727 if (offset & 0x4)
4728 gr_mode = SImode;
4729 /* If we have an even 4 byte hunk because the aggregate is a
4730 multiple of 4 bytes in size, then this goes in a GR reg right
4731 adjusted/little endian. */
4732 else if (byte_size - offset == 4)
4733 gr_mode = SImode;
4735 loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
4736 gen_rtx_REG (gr_mode, (basereg
4737 + int_regs)),
4738 GEN_INT (offset));
4740 gr_size = GET_MODE_SIZE (gr_mode);
4741 offset += gr_size;
4742 if (gr_size == UNITS_PER_WORD
4743 || (gr_size < UNITS_PER_WORD && offset % UNITS_PER_WORD == 0))
4744 int_regs++;
4745 else if (gr_size > UNITS_PER_WORD)
4746 int_regs += gr_size / UNITS_PER_WORD;
4748 return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
4751 /* Integral and aggregates go in general registers. If we have run out of
4752 FR registers, then FP values must also go in general registers. This can
4753 happen when we have a SFmode HFA. */
4754 else if (mode == TFmode || mode == TCmode
4755 || (! FLOAT_MODE_P (mode) || cum->fp_regs == MAX_ARGUMENT_SLOTS))
4757 int byte_size = ((mode == BLKmode)
4758 ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
4759 if (BYTES_BIG_ENDIAN
4760 && (mode == BLKmode || (type && AGGREGATE_TYPE_P (type)))
4761 && byte_size < UNITS_PER_WORD
4762 && byte_size > 0)
4764 rtx gr_reg = gen_rtx_EXPR_LIST (VOIDmode,
4765 gen_rtx_REG (DImode,
4766 (basereg + cum->words
4767 + offset)),
4768 const0_rtx);
4769 return gen_rtx_PARALLEL (mode, gen_rtvec (1, gr_reg));
4771 else
4772 return gen_rtx_REG (mode, basereg + cum->words + offset);
4776 /* If there is a prototype, then FP values go in a FR register when
4777 named, and in a GR register when unnamed. */
4778 else if (cum->prototype)
4780 if (named)
4781 return gen_rtx_REG (mode, FR_ARG_FIRST + cum->fp_regs);
4782 /* In big-endian mode, an anonymous SFmode value must be represented
4783 as (parallel:SF [(expr_list (reg:DI n) (const_int 0))]) to force
4784 the value into the high half of the general register. */
4785 else if (BYTES_BIG_ENDIAN && mode == SFmode)
4786 return gen_rtx_PARALLEL (mode,
4787 gen_rtvec (1,
4788 gen_rtx_EXPR_LIST (VOIDmode,
4789 gen_rtx_REG (DImode, basereg + cum->words + offset),
4790 const0_rtx)));
4791 else
4792 return gen_rtx_REG (mode, basereg + cum->words + offset);
4794 /* If there is no prototype, then FP values go in both FR and GR
4795 registers. */
4796 else
4798 /* See comment above. */
4799 enum machine_mode inner_mode =
4800 (BYTES_BIG_ENDIAN && mode == SFmode) ? DImode : mode;
4802 rtx fp_reg = gen_rtx_EXPR_LIST (VOIDmode,
4803 gen_rtx_REG (mode, (FR_ARG_FIRST
4804 + cum->fp_regs)),
4805 const0_rtx);
4806 rtx gr_reg = gen_rtx_EXPR_LIST (VOIDmode,
4807 gen_rtx_REG (inner_mode,
4808 (basereg + cum->words
4809 + offset)),
4810 const0_rtx);
4812 return gen_rtx_PARALLEL (mode, gen_rtvec (2, fp_reg, gr_reg));
4816 /* Implement TARGET_FUNCION_ARG target hook. */
4818 static rtx
4819 ia64_function_arg (cumulative_args_t cum, enum machine_mode mode,
4820 const_tree type, bool named)
4822 return ia64_function_arg_1 (cum, mode, type, named, false);
4825 /* Implement TARGET_FUNCION_INCOMING_ARG target hook. */
4827 static rtx
4828 ia64_function_incoming_arg (cumulative_args_t cum,
4829 enum machine_mode mode,
4830 const_tree type, bool named)
4832 return ia64_function_arg_1 (cum, mode, type, named, true);
4835 /* Return number of bytes, at the beginning of the argument, that must be
4836 put in registers. 0 is the argument is entirely in registers or entirely
4837 in memory. */
4839 static int
4840 ia64_arg_partial_bytes (cumulative_args_t cum_v, enum machine_mode mode,
4841 tree type, bool named ATTRIBUTE_UNUSED)
4843 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
4845 int words = ia64_function_arg_words (type, mode);
4846 int offset = ia64_function_arg_offset (cum, type, words);
4848 /* If all argument slots are used, then it must go on the stack. */
4849 if (cum->words + offset >= MAX_ARGUMENT_SLOTS)
4850 return 0;
4852 /* It doesn't matter whether the argument goes in FR or GR regs. If
4853 it fits within the 8 argument slots, then it goes entirely in
4854 registers. If it extends past the last argument slot, then the rest
4855 goes on the stack. */
4857 if (words + cum->words + offset <= MAX_ARGUMENT_SLOTS)
4858 return 0;
4860 return (MAX_ARGUMENT_SLOTS - cum->words - offset) * UNITS_PER_WORD;
4863 /* Return ivms_arg_type based on machine_mode. */
4865 static enum ivms_arg_type
4866 ia64_arg_type (enum machine_mode mode)
4868 switch (mode)
4870 case SFmode:
4871 return FS;
4872 case DFmode:
4873 return FT;
4874 default:
4875 return I64;
4879 /* Update CUM to point after this argument. This is patterned after
4880 ia64_function_arg. */
4882 static void
4883 ia64_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
4884 const_tree type, bool named)
4886 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
4887 int words = ia64_function_arg_words (type, mode);
4888 int offset = ia64_function_arg_offset (cum, type, words);
4889 enum machine_mode hfa_mode = VOIDmode;
4891 /* If all arg slots are already full, then there is nothing to do. */
4892 if (cum->words >= MAX_ARGUMENT_SLOTS)
4894 cum->words += words + offset;
4895 return;
4898 cum->atypes[cum->words] = ia64_arg_type (mode);
4899 cum->words += words + offset;
4901 /* On OpenVMS argument is either in Rn or Fn. */
4902 if (TARGET_ABI_OPEN_VMS)
4904 cum->int_regs = cum->words;
4905 cum->fp_regs = cum->words;
4906 return;
4909 /* Check for and handle homogeneous FP aggregates. */
4910 if (type)
4911 hfa_mode = hfa_element_mode (type, 0);
4913 /* Unnamed prototyped hfas are passed as usual. Named prototyped hfas
4914 and unprototyped hfas are passed specially. */
4915 if (hfa_mode != VOIDmode && (! cum->prototype || named))
4917 int fp_regs = cum->fp_regs;
4918 /* This is the original value of cum->words + offset. */
4919 int int_regs = cum->words - words;
4920 int hfa_size = GET_MODE_SIZE (hfa_mode);
4921 int byte_size;
4922 int args_byte_size;
4924 /* If prototyped, pass it in FR regs then GR regs.
4925 If not prototyped, pass it in both FR and GR regs.
4927 If this is an SFmode aggregate, then it is possible to run out of
4928 FR regs while GR regs are still left. In that case, we pass the
4929 remaining part in the GR regs. */
4931 /* Fill the FP regs. We do this always. We stop if we reach the end
4932 of the argument, the last FP register, or the last argument slot. */
4934 byte_size = ((mode == BLKmode)
4935 ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
4936 args_byte_size = int_regs * UNITS_PER_WORD;
4937 offset = 0;
4938 for (; (offset < byte_size && fp_regs < MAX_ARGUMENT_SLOTS
4939 && args_byte_size < (MAX_ARGUMENT_SLOTS * UNITS_PER_WORD));)
4941 offset += hfa_size;
4942 args_byte_size += hfa_size;
4943 fp_regs++;
4946 cum->fp_regs = fp_regs;
4949 /* Integral and aggregates go in general registers. So do TFmode FP values.
4950 If we have run out of FR registers, then other FP values must also go in
4951 general registers. This can happen when we have a SFmode HFA. */
4952 else if (mode == TFmode || mode == TCmode
4953 || (! FLOAT_MODE_P (mode) || cum->fp_regs == MAX_ARGUMENT_SLOTS))
4954 cum->int_regs = cum->words;
4956 /* If there is a prototype, then FP values go in a FR register when
4957 named, and in a GR register when unnamed. */
4958 else if (cum->prototype)
4960 if (! named)
4961 cum->int_regs = cum->words;
4962 else
4963 /* ??? Complex types should not reach here. */
4964 cum->fp_regs += (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT ? 2 : 1);
4966 /* If there is no prototype, then FP values go in both FR and GR
4967 registers. */
4968 else
4970 /* ??? Complex types should not reach here. */
4971 cum->fp_regs += (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT ? 2 : 1);
4972 cum->int_regs = cum->words;
4976 /* Arguments with alignment larger than 8 bytes start at the next even
4977 boundary. On ILP32 HPUX, TFmode arguments start on next even boundary
4978 even though their normal alignment is 8 bytes. See ia64_function_arg. */
4980 static unsigned int
4981 ia64_function_arg_boundary (enum machine_mode mode, const_tree type)
4983 if (mode == TFmode && TARGET_HPUX && TARGET_ILP32)
4984 return PARM_BOUNDARY * 2;
4986 if (type)
4988 if (TYPE_ALIGN (type) > PARM_BOUNDARY)
4989 return PARM_BOUNDARY * 2;
4990 else
4991 return PARM_BOUNDARY;
4994 if (GET_MODE_BITSIZE (mode) > PARM_BOUNDARY)
4995 return PARM_BOUNDARY * 2;
4996 else
4997 return PARM_BOUNDARY;
5000 /* True if it is OK to do sibling call optimization for the specified
5001 call expression EXP. DECL will be the called function, or NULL if
5002 this is an indirect call. */
5003 static bool
5004 ia64_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
5006 /* We can't perform a sibcall if the current function has the syscall_linkage
5007 attribute. */
5008 if (lookup_attribute ("syscall_linkage",
5009 TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
5010 return false;
5012 /* We must always return with our current GP. This means we can
5013 only sibcall to functions defined in the current module unless
5014 TARGET_CONST_GP is set to true. */
5015 return (decl && (*targetm.binds_local_p) (decl)) || TARGET_CONST_GP;
5019 /* Implement va_arg. */
5021 static tree
5022 ia64_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
5023 gimple_seq *post_p)
5025 /* Variable sized types are passed by reference. */
5026 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
5028 tree ptrtype = build_pointer_type (type);
5029 tree addr = std_gimplify_va_arg_expr (valist, ptrtype, pre_p, post_p);
5030 return build_va_arg_indirect_ref (addr);
5033 /* Aggregate arguments with alignment larger than 8 bytes start at
5034 the next even boundary. Integer and floating point arguments
5035 do so if they are larger than 8 bytes, whether or not they are
5036 also aligned larger than 8 bytes. */
5037 if ((TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == INTEGER_TYPE)
5038 ? int_size_in_bytes (type) > 8 : TYPE_ALIGN (type) > 8 * BITS_PER_UNIT)
5040 tree t = fold_build_pointer_plus_hwi (valist, 2 * UNITS_PER_WORD - 1);
5041 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
5042 build_int_cst (TREE_TYPE (t), -2 * UNITS_PER_WORD));
5043 gimplify_assign (unshare_expr (valist), t, pre_p);
5046 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
5049 /* Return 1 if function return value returned in memory. Return 0 if it is
5050 in a register. */
5052 static bool
5053 ia64_return_in_memory (const_tree valtype, const_tree fntype ATTRIBUTE_UNUSED)
5055 enum machine_mode mode;
5056 enum machine_mode hfa_mode;
5057 HOST_WIDE_INT byte_size;
5059 mode = TYPE_MODE (valtype);
5060 byte_size = GET_MODE_SIZE (mode);
5061 if (mode == BLKmode)
5063 byte_size = int_size_in_bytes (valtype);
5064 if (byte_size < 0)
5065 return true;
5068 /* Hfa's with up to 8 elements are returned in the FP argument registers. */
5070 hfa_mode = hfa_element_mode (valtype, 0);
5071 if (hfa_mode != VOIDmode)
5073 int hfa_size = GET_MODE_SIZE (hfa_mode);
5075 if (byte_size / hfa_size > MAX_ARGUMENT_SLOTS)
5076 return true;
5077 else
5078 return false;
5080 else if (byte_size > UNITS_PER_WORD * MAX_INT_RETURN_SLOTS)
5081 return true;
5082 else
5083 return false;
5086 /* Return rtx for register that holds the function return value. */
5088 static rtx
5089 ia64_function_value (const_tree valtype,
5090 const_tree fn_decl_or_type,
5091 bool outgoing ATTRIBUTE_UNUSED)
5093 enum machine_mode mode;
5094 enum machine_mode hfa_mode;
5095 int unsignedp;
5096 const_tree func = fn_decl_or_type;
5098 if (fn_decl_or_type
5099 && !DECL_P (fn_decl_or_type))
5100 func = NULL;
5102 mode = TYPE_MODE (valtype);
5103 hfa_mode = hfa_element_mode (valtype, 0);
5105 if (hfa_mode != VOIDmode)
5107 rtx loc[8];
5108 int i;
5109 int hfa_size;
5110 int byte_size;
5111 int offset;
5113 hfa_size = GET_MODE_SIZE (hfa_mode);
5114 byte_size = ((mode == BLKmode)
5115 ? int_size_in_bytes (valtype) : GET_MODE_SIZE (mode));
5116 offset = 0;
5117 for (i = 0; offset < byte_size; i++)
5119 loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
5120 gen_rtx_REG (hfa_mode, FR_ARG_FIRST + i),
5121 GEN_INT (offset));
5122 offset += hfa_size;
5124 return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
5126 else if (FLOAT_TYPE_P (valtype) && mode != TFmode && mode != TCmode)
5127 return gen_rtx_REG (mode, FR_ARG_FIRST);
5128 else
5130 bool need_parallel = false;
5132 /* In big-endian mode, we need to manage the layout of aggregates
5133 in the registers so that we get the bits properly aligned in
5134 the highpart of the registers. */
5135 if (BYTES_BIG_ENDIAN
5136 && (mode == BLKmode || (valtype && AGGREGATE_TYPE_P (valtype))))
5137 need_parallel = true;
5139 /* Something like struct S { long double x; char a[0] } is not an
5140 HFA structure, and therefore doesn't go in fp registers. But
5141 the middle-end will give it XFmode anyway, and XFmode values
5142 don't normally fit in integer registers. So we need to smuggle
5143 the value inside a parallel. */
5144 else if (mode == XFmode || mode == XCmode || mode == RFmode)
5145 need_parallel = true;
5147 if (need_parallel)
5149 rtx loc[8];
5150 int offset;
5151 int bytesize;
5152 int i;
5154 offset = 0;
5155 bytesize = int_size_in_bytes (valtype);
5156 /* An empty PARALLEL is invalid here, but the return value
5157 doesn't matter for empty structs. */
5158 if (bytesize == 0)
5159 return gen_rtx_REG (mode, GR_RET_FIRST);
5160 for (i = 0; offset < bytesize; i++)
5162 loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
5163 gen_rtx_REG (DImode,
5164 GR_RET_FIRST + i),
5165 GEN_INT (offset));
5166 offset += UNITS_PER_WORD;
5168 return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
5171 mode = promote_function_mode (valtype, mode, &unsignedp,
5172 func ? TREE_TYPE (func) : NULL_TREE,
5173 true);
5175 return gen_rtx_REG (mode, GR_RET_FIRST);
5179 /* Worker function for TARGET_LIBCALL_VALUE. */
5181 static rtx
5182 ia64_libcall_value (enum machine_mode mode,
5183 const_rtx fun ATTRIBUTE_UNUSED)
5185 return gen_rtx_REG (mode,
5186 (((GET_MODE_CLASS (mode) == MODE_FLOAT
5187 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5188 && (mode) != TFmode)
5189 ? FR_RET_FIRST : GR_RET_FIRST));
5192 /* Worker function for FUNCTION_VALUE_REGNO_P. */
5194 static bool
5195 ia64_function_value_regno_p (const unsigned int regno)
5197 return ((regno >= GR_RET_FIRST && regno <= GR_RET_LAST)
5198 || (regno >= FR_RET_FIRST && regno <= FR_RET_LAST));
5201 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
5202 We need to emit DTP-relative relocations. */
5204 static void
5205 ia64_output_dwarf_dtprel (FILE *file, int size, rtx x)
5207 gcc_assert (size == 4 || size == 8);
5208 if (size == 4)
5209 fputs ("\tdata4.ua\t@dtprel(", file);
5210 else
5211 fputs ("\tdata8.ua\t@dtprel(", file);
5212 output_addr_const (file, x);
5213 fputs (")", file);
5216 /* Print a memory address as an operand to reference that memory location. */
5218 /* ??? Do we need this? It gets used only for 'a' operands. We could perhaps
5219 also call this from ia64_print_operand for memory addresses. */
5221 static void
5222 ia64_print_operand_address (FILE * stream ATTRIBUTE_UNUSED,
5223 rtx address ATTRIBUTE_UNUSED)
5227 /* Print an operand to an assembler instruction.
5228 C Swap and print a comparison operator.
5229 D Print an FP comparison operator.
5230 E Print 32 - constant, for SImode shifts as extract.
5231 e Print 64 - constant, for DImode rotates.
5232 F A floating point constant 0.0 emitted as f0, or 1.0 emitted as f1, or
5233 a floating point register emitted normally.
5234 G A floating point constant.
5235 I Invert a predicate register by adding 1.
5236 J Select the proper predicate register for a condition.
5237 j Select the inverse predicate register for a condition.
5238 O Append .acq for volatile load.
5239 P Postincrement of a MEM.
5240 Q Append .rel for volatile store.
5241 R Print .s .d or nothing for a single, double or no truncation.
5242 S Shift amount for shladd instruction.
5243 T Print an 8-bit sign extended number (K) as a 32-bit unsigned number
5244 for Intel assembler.
5245 U Print an 8-bit sign extended number (K) as a 64-bit unsigned number
5246 for Intel assembler.
5247 X A pair of floating point registers.
5248 r Print register name, or constant 0 as r0. HP compatibility for
5249 Linux kernel.
5250 v Print vector constant value as an 8-byte integer value. */
5252 static void
5253 ia64_print_operand (FILE * file, rtx x, int code)
5255 const char *str;
5257 switch (code)
5259 case 0:
5260 /* Handled below. */
5261 break;
5263 case 'C':
5265 enum rtx_code c = swap_condition (GET_CODE (x));
5266 fputs (GET_RTX_NAME (c), file);
5267 return;
5270 case 'D':
5271 switch (GET_CODE (x))
5273 case NE:
5274 str = "neq";
5275 break;
5276 case UNORDERED:
5277 str = "unord";
5278 break;
5279 case ORDERED:
5280 str = "ord";
5281 break;
5282 case UNLT:
5283 str = "nge";
5284 break;
5285 case UNLE:
5286 str = "ngt";
5287 break;
5288 case UNGT:
5289 str = "nle";
5290 break;
5291 case UNGE:
5292 str = "nlt";
5293 break;
5294 case UNEQ:
5295 case LTGT:
5296 gcc_unreachable ();
5297 default:
5298 str = GET_RTX_NAME (GET_CODE (x));
5299 break;
5301 fputs (str, file);
5302 return;
5304 case 'E':
5305 fprintf (file, HOST_WIDE_INT_PRINT_DEC, 32 - INTVAL (x));
5306 return;
5308 case 'e':
5309 fprintf (file, HOST_WIDE_INT_PRINT_DEC, 64 - INTVAL (x));
5310 return;
5312 case 'F':
5313 if (x == CONST0_RTX (GET_MODE (x)))
5314 str = reg_names [FR_REG (0)];
5315 else if (x == CONST1_RTX (GET_MODE (x)))
5316 str = reg_names [FR_REG (1)];
5317 else
5319 gcc_assert (GET_CODE (x) == REG);
5320 str = reg_names [REGNO (x)];
5322 fputs (str, file);
5323 return;
5325 case 'G':
5327 long val[4];
5328 REAL_VALUE_TYPE rv;
5329 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
5330 real_to_target (val, &rv, GET_MODE (x));
5331 if (GET_MODE (x) == SFmode)
5332 fprintf (file, "0x%08lx", val[0] & 0xffffffff);
5333 else if (GET_MODE (x) == DFmode)
5334 fprintf (file, "0x%08lx%08lx", (WORDS_BIG_ENDIAN ? val[0] : val[1])
5335 & 0xffffffff,
5336 (WORDS_BIG_ENDIAN ? val[1] : val[0])
5337 & 0xffffffff);
5338 else
5339 output_operand_lossage ("invalid %%G mode");
5341 return;
5343 case 'I':
5344 fputs (reg_names [REGNO (x) + 1], file);
5345 return;
5347 case 'J':
5348 case 'j':
5350 unsigned int regno = REGNO (XEXP (x, 0));
5351 if (GET_CODE (x) == EQ)
5352 regno += 1;
5353 if (code == 'j')
5354 regno ^= 1;
5355 fputs (reg_names [regno], file);
5357 return;
5359 case 'O':
5360 if (MEM_VOLATILE_P (x))
5361 fputs(".acq", file);
5362 return;
5364 case 'P':
5366 HOST_WIDE_INT value;
5368 switch (GET_CODE (XEXP (x, 0)))
5370 default:
5371 return;
5373 case POST_MODIFY:
5374 x = XEXP (XEXP (XEXP (x, 0), 1), 1);
5375 if (GET_CODE (x) == CONST_INT)
5376 value = INTVAL (x);
5377 else
5379 gcc_assert (GET_CODE (x) == REG);
5380 fprintf (file, ", %s", reg_names[REGNO (x)]);
5381 return;
5383 break;
5385 case POST_INC:
5386 value = GET_MODE_SIZE (GET_MODE (x));
5387 break;
5389 case POST_DEC:
5390 value = - (HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (x));
5391 break;
5394 fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC, value);
5395 return;
5398 case 'Q':
5399 if (MEM_VOLATILE_P (x))
5400 fputs(".rel", file);
5401 return;
5403 case 'R':
5404 if (x == CONST0_RTX (GET_MODE (x)))
5405 fputs(".s", file);
5406 else if (x == CONST1_RTX (GET_MODE (x)))
5407 fputs(".d", file);
5408 else if (x == CONST2_RTX (GET_MODE (x)))
5410 else
5411 output_operand_lossage ("invalid %%R value");
5412 return;
5414 case 'S':
5415 fprintf (file, "%d", exact_log2 (INTVAL (x)));
5416 return;
5418 case 'T':
5419 if (! TARGET_GNU_AS && GET_CODE (x) == CONST_INT)
5421 fprintf (file, "0x%x", (int) INTVAL (x) & 0xffffffff);
5422 return;
5424 break;
5426 case 'U':
5427 if (! TARGET_GNU_AS && GET_CODE (x) == CONST_INT)
5429 const char *prefix = "0x";
5430 if (INTVAL (x) & 0x80000000)
5432 fprintf (file, "0xffffffff");
5433 prefix = "";
5435 fprintf (file, "%s%x", prefix, (int) INTVAL (x) & 0xffffffff);
5436 return;
5438 break;
5440 case 'X':
5442 unsigned int regno = REGNO (x);
5443 fprintf (file, "%s, %s", reg_names [regno], reg_names [regno + 1]);
5445 return;
5447 case 'r':
5448 /* If this operand is the constant zero, write it as register zero.
5449 Any register, zero, or CONST_INT value is OK here. */
5450 if (GET_CODE (x) == REG)
5451 fputs (reg_names[REGNO (x)], file);
5452 else if (x == CONST0_RTX (GET_MODE (x)))
5453 fputs ("r0", file);
5454 else if (GET_CODE (x) == CONST_INT)
5455 output_addr_const (file, x);
5456 else
5457 output_operand_lossage ("invalid %%r value");
5458 return;
5460 case 'v':
5461 gcc_assert (GET_CODE (x) == CONST_VECTOR);
5462 x = simplify_subreg (DImode, x, GET_MODE (x), 0);
5463 break;
5465 case '+':
5467 const char *which;
5469 /* For conditional branches, returns or calls, substitute
5470 sptk, dptk, dpnt, or spnt for %s. */
5471 x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
5472 if (x)
5474 int pred_val = XINT (x, 0);
5476 /* Guess top and bottom 10% statically predicted. */
5477 if (pred_val < REG_BR_PROB_BASE / 50
5478 && br_prob_note_reliable_p (x))
5479 which = ".spnt";
5480 else if (pred_val < REG_BR_PROB_BASE / 2)
5481 which = ".dpnt";
5482 else if (pred_val < REG_BR_PROB_BASE / 100 * 98
5483 || !br_prob_note_reliable_p (x))
5484 which = ".dptk";
5485 else
5486 which = ".sptk";
5488 else if (CALL_P (current_output_insn))
5489 which = ".sptk";
5490 else
5491 which = ".dptk";
5493 fputs (which, file);
5494 return;
5497 case ',':
5498 x = current_insn_predicate;
5499 if (x)
5501 unsigned int regno = REGNO (XEXP (x, 0));
5502 if (GET_CODE (x) == EQ)
5503 regno += 1;
5504 fprintf (file, "(%s) ", reg_names [regno]);
5506 return;
5508 default:
5509 output_operand_lossage ("ia64_print_operand: unknown code");
5510 return;
5513 switch (GET_CODE (x))
5515 /* This happens for the spill/restore instructions. */
5516 case POST_INC:
5517 case POST_DEC:
5518 case POST_MODIFY:
5519 x = XEXP (x, 0);
5520 /* ... fall through ... */
5522 case REG:
5523 fputs (reg_names [REGNO (x)], file);
5524 break;
5526 case MEM:
5528 rtx addr = XEXP (x, 0);
5529 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
5530 addr = XEXP (addr, 0);
5531 fprintf (file, "[%s]", reg_names [REGNO (addr)]);
5532 break;
5535 default:
5536 output_addr_const (file, x);
5537 break;
5540 return;
5543 /* Worker function for TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
5545 static bool
5546 ia64_print_operand_punct_valid_p (unsigned char code)
5548 return (code == '+' || code == ',');
5551 /* Compute a (partial) cost for rtx X. Return true if the complete
5552 cost has been computed, and false if subexpressions should be
5553 scanned. In either case, *TOTAL contains the cost result. */
5554 /* ??? This is incomplete. */
5556 static bool
5557 ia64_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
5558 int *total, bool speed ATTRIBUTE_UNUSED)
5560 switch (code)
5562 case CONST_INT:
5563 switch (outer_code)
5565 case SET:
5566 *total = satisfies_constraint_J (x) ? 0 : COSTS_N_INSNS (1);
5567 return true;
5568 case PLUS:
5569 if (satisfies_constraint_I (x))
5570 *total = 0;
5571 else if (satisfies_constraint_J (x))
5572 *total = 1;
5573 else
5574 *total = COSTS_N_INSNS (1);
5575 return true;
5576 default:
5577 if (satisfies_constraint_K (x) || satisfies_constraint_L (x))
5578 *total = 0;
5579 else
5580 *total = COSTS_N_INSNS (1);
5581 return true;
5584 case CONST_DOUBLE:
5585 *total = COSTS_N_INSNS (1);
5586 return true;
5588 case CONST:
5589 case SYMBOL_REF:
5590 case LABEL_REF:
5591 *total = COSTS_N_INSNS (3);
5592 return true;
5594 case FMA:
5595 *total = COSTS_N_INSNS (4);
5596 return true;
5598 case MULT:
5599 /* For multiplies wider than HImode, we have to go to the FPU,
5600 which normally involves copies. Plus there's the latency
5601 of the multiply itself, and the latency of the instructions to
5602 transfer integer regs to FP regs. */
5603 if (FLOAT_MODE_P (GET_MODE (x)))
5604 *total = COSTS_N_INSNS (4);
5605 else if (GET_MODE_SIZE (GET_MODE (x)) > 2)
5606 *total = COSTS_N_INSNS (10);
5607 else
5608 *total = COSTS_N_INSNS (2);
5609 return true;
5611 case PLUS:
5612 case MINUS:
5613 if (FLOAT_MODE_P (GET_MODE (x)))
5615 *total = COSTS_N_INSNS (4);
5616 return true;
5618 /* FALLTHRU */
5620 case ASHIFT:
5621 case ASHIFTRT:
5622 case LSHIFTRT:
5623 *total = COSTS_N_INSNS (1);
5624 return true;
5626 case DIV:
5627 case UDIV:
5628 case MOD:
5629 case UMOD:
5630 /* We make divide expensive, so that divide-by-constant will be
5631 optimized to a multiply. */
5632 *total = COSTS_N_INSNS (60);
5633 return true;
5635 default:
5636 return false;
5640 /* Calculate the cost of moving data from a register in class FROM to
5641 one in class TO, using MODE. */
5643 static int
5644 ia64_register_move_cost (enum machine_mode mode, reg_class_t from,
5645 reg_class_t to)
5647 /* ADDL_REGS is the same as GR_REGS for movement purposes. */
5648 if (to == ADDL_REGS)
5649 to = GR_REGS;
5650 if (from == ADDL_REGS)
5651 from = GR_REGS;
5653 /* All costs are symmetric, so reduce cases by putting the
5654 lower number class as the destination. */
5655 if (from < to)
5657 reg_class_t tmp = to;
5658 to = from, from = tmp;
5661 /* Moving from FR<->GR in XFmode must be more expensive than 2,
5662 so that we get secondary memory reloads. Between FR_REGS,
5663 we have to make this at least as expensive as memory_move_cost
5664 to avoid spectacularly poor register class preferencing. */
5665 if (mode == XFmode || mode == RFmode)
5667 if (to != GR_REGS || from != GR_REGS)
5668 return memory_move_cost (mode, to, false);
5669 else
5670 return 3;
5673 switch (to)
5675 case PR_REGS:
5676 /* Moving between PR registers takes two insns. */
5677 if (from == PR_REGS)
5678 return 3;
5679 /* Moving between PR and anything but GR is impossible. */
5680 if (from != GR_REGS)
5681 return memory_move_cost (mode, to, false);
5682 break;
5684 case BR_REGS:
5685 /* Moving between BR and anything but GR is impossible. */
5686 if (from != GR_REGS && from != GR_AND_BR_REGS)
5687 return memory_move_cost (mode, to, false);
5688 break;
5690 case AR_I_REGS:
5691 case AR_M_REGS:
5692 /* Moving between AR and anything but GR is impossible. */
5693 if (from != GR_REGS)
5694 return memory_move_cost (mode, to, false);
5695 break;
5697 case GR_REGS:
5698 case FR_REGS:
5699 case FP_REGS:
5700 case GR_AND_FR_REGS:
5701 case GR_AND_BR_REGS:
5702 case ALL_REGS:
5703 break;
5705 default:
5706 gcc_unreachable ();
5709 return 2;
5712 /* Calculate the cost of moving data of MODE from a register to or from
5713 memory. */
5715 static int
5716 ia64_memory_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
5717 reg_class_t rclass,
5718 bool in ATTRIBUTE_UNUSED)
5720 if (rclass == GENERAL_REGS
5721 || rclass == FR_REGS
5722 || rclass == FP_REGS
5723 || rclass == GR_AND_FR_REGS)
5724 return 4;
5725 else
5726 return 10;
5729 /* Implement TARGET_PREFERRED_RELOAD_CLASS. Place additional restrictions
5730 on RCLASS to use when copying X into that class. */
5732 static reg_class_t
5733 ia64_preferred_reload_class (rtx x, reg_class_t rclass)
5735 switch (rclass)
5737 case FR_REGS:
5738 case FP_REGS:
5739 /* Don't allow volatile mem reloads into floating point registers.
5740 This is defined to force reload to choose the r/m case instead
5741 of the f/f case when reloading (set (reg fX) (mem/v)). */
5742 if (MEM_P (x) && MEM_VOLATILE_P (x))
5743 return NO_REGS;
5745 /* Force all unrecognized constants into the constant pool. */
5746 if (CONSTANT_P (x))
5747 return NO_REGS;
5748 break;
5750 case AR_M_REGS:
5751 case AR_I_REGS:
5752 if (!OBJECT_P (x))
5753 return NO_REGS;
5754 break;
5756 default:
5757 break;
5760 return rclass;
5763 /* This function returns the register class required for a secondary
5764 register when copying between one of the registers in RCLASS, and X,
5765 using MODE. A return value of NO_REGS means that no secondary register
5766 is required. */
5768 enum reg_class
5769 ia64_secondary_reload_class (enum reg_class rclass,
5770 enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
5772 int regno = -1;
5774 if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
5775 regno = true_regnum (x);
5777 switch (rclass)
5779 case BR_REGS:
5780 case AR_M_REGS:
5781 case AR_I_REGS:
5782 /* ??? BR<->BR register copies can happen due to a bad gcse/cse/global
5783 interaction. We end up with two pseudos with overlapping lifetimes
5784 both of which are equiv to the same constant, and both which need
5785 to be in BR_REGS. This seems to be a cse bug. cse_basic_block_end
5786 changes depending on the path length, which means the qty_first_reg
5787 check in make_regs_eqv can give different answers at different times.
5788 At some point I'll probably need a reload_indi pattern to handle
5789 this.
5791 We can also get GR_AND_FR_REGS to BR_REGS/AR_REGS copies, where we
5792 wound up with a FP register from GR_AND_FR_REGS. Extend that to all
5793 non-general registers for good measure. */
5794 if (regno >= 0 && ! GENERAL_REGNO_P (regno))
5795 return GR_REGS;
5797 /* This is needed if a pseudo used as a call_operand gets spilled to a
5798 stack slot. */
5799 if (GET_CODE (x) == MEM)
5800 return GR_REGS;
5801 break;
5803 case FR_REGS:
5804 case FP_REGS:
5805 /* Need to go through general registers to get to other class regs. */
5806 if (regno >= 0 && ! (FR_REGNO_P (regno) || GENERAL_REGNO_P (regno)))
5807 return GR_REGS;
5809 /* This can happen when a paradoxical subreg is an operand to the
5810 muldi3 pattern. */
5811 /* ??? This shouldn't be necessary after instruction scheduling is
5812 enabled, because paradoxical subregs are not accepted by
5813 register_operand when INSN_SCHEDULING is defined. Or alternatively,
5814 stop the paradoxical subreg stupidity in the *_operand functions
5815 in recog.c. */
5816 if (GET_CODE (x) == MEM
5817 && (GET_MODE (x) == SImode || GET_MODE (x) == HImode
5818 || GET_MODE (x) == QImode))
5819 return GR_REGS;
5821 /* This can happen because of the ior/and/etc patterns that accept FP
5822 registers as operands. If the third operand is a constant, then it
5823 needs to be reloaded into a FP register. */
5824 if (GET_CODE (x) == CONST_INT)
5825 return GR_REGS;
5827 /* This can happen because of register elimination in a muldi3 insn.
5828 E.g. `26107 * (unsigned long)&u'. */
5829 if (GET_CODE (x) == PLUS)
5830 return GR_REGS;
5831 break;
5833 case PR_REGS:
5834 /* ??? This happens if we cse/gcse a BImode value across a call,
5835 and the function has a nonlocal goto. This is because global
5836 does not allocate call crossing pseudos to hard registers when
5837 crtl->has_nonlocal_goto is true. This is relatively
5838 common for C++ programs that use exceptions. To reproduce,
5839 return NO_REGS and compile libstdc++. */
5840 if (GET_CODE (x) == MEM)
5841 return GR_REGS;
5843 /* This can happen when we take a BImode subreg of a DImode value,
5844 and that DImode value winds up in some non-GR register. */
5845 if (regno >= 0 && ! GENERAL_REGNO_P (regno) && ! PR_REGNO_P (regno))
5846 return GR_REGS;
5847 break;
5849 default:
5850 break;
5853 return NO_REGS;
5857 /* Implement targetm.unspec_may_trap_p hook. */
5858 static int
5859 ia64_unspec_may_trap_p (const_rtx x, unsigned flags)
5861 switch (XINT (x, 1))
5863 case UNSPEC_LDA:
5864 case UNSPEC_LDS:
5865 case UNSPEC_LDSA:
5866 case UNSPEC_LDCCLR:
5867 case UNSPEC_CHKACLR:
5868 case UNSPEC_CHKS:
5869 /* These unspecs are just wrappers. */
5870 return may_trap_p_1 (XVECEXP (x, 0, 0), flags);
5873 return default_unspec_may_trap_p (x, flags);
5877 /* Parse the -mfixed-range= option string. */
5879 static void
5880 fix_range (const char *const_str)
5882 int i, first, last;
5883 char *str, *dash, *comma;
5885 /* str must be of the form REG1'-'REG2{,REG1'-'REG} where REG1 and
5886 REG2 are either register names or register numbers. The effect
5887 of this option is to mark the registers in the range from REG1 to
5888 REG2 as ``fixed'' so they won't be used by the compiler. This is
5889 used, e.g., to ensure that kernel mode code doesn't use f32-f127. */
5891 i = strlen (const_str);
5892 str = (char *) alloca (i + 1);
5893 memcpy (str, const_str, i + 1);
5895 while (1)
5897 dash = strchr (str, '-');
5898 if (!dash)
5900 warning (0, "value of -mfixed-range must have form REG1-REG2");
5901 return;
5903 *dash = '\0';
5905 comma = strchr (dash + 1, ',');
5906 if (comma)
5907 *comma = '\0';
5909 first = decode_reg_name (str);
5910 if (first < 0)
5912 warning (0, "unknown register name: %s", str);
5913 return;
5916 last = decode_reg_name (dash + 1);
5917 if (last < 0)
5919 warning (0, "unknown register name: %s", dash + 1);
5920 return;
5923 *dash = '-';
5925 if (first > last)
5927 warning (0, "%s-%s is an empty range", str, dash + 1);
5928 return;
5931 for (i = first; i <= last; ++i)
5932 fixed_regs[i] = call_used_regs[i] = 1;
5934 if (!comma)
5935 break;
5937 *comma = ',';
5938 str = comma + 1;
5942 /* Implement TARGET_OPTION_OVERRIDE. */
5944 static void
5945 ia64_option_override (void)
5947 unsigned int i;
5948 cl_deferred_option *opt;
5949 vec<cl_deferred_option> *v
5950 = (vec<cl_deferred_option> *) ia64_deferred_options;
5952 if (v)
5953 FOR_EACH_VEC_ELT (*v, i, opt)
5955 switch (opt->opt_index)
5957 case OPT_mfixed_range_:
5958 fix_range (opt->arg);
5959 break;
5961 default:
5962 gcc_unreachable ();
5966 if (TARGET_AUTO_PIC)
5967 target_flags |= MASK_CONST_GP;
5969 /* Numerous experiment shows that IRA based loop pressure
5970 calculation works better for RTL loop invariant motion on targets
5971 with enough (>= 32) registers. It is an expensive optimization.
5972 So it is on only for peak performance. */
5973 if (optimize >= 3)
5974 flag_ira_loop_pressure = 1;
5977 ia64_section_threshold = (global_options_set.x_g_switch_value
5978 ? g_switch_value
5979 : IA64_DEFAULT_GVALUE);
5981 init_machine_status = ia64_init_machine_status;
5983 if (align_functions <= 0)
5984 align_functions = 64;
5985 if (align_loops <= 0)
5986 align_loops = 32;
5987 if (TARGET_ABI_OPEN_VMS)
5988 flag_no_common = 1;
5990 ia64_override_options_after_change();
5993 /* Implement targetm.override_options_after_change. */
5995 static void
5996 ia64_override_options_after_change (void)
5998 if (optimize >= 3
5999 && !global_options_set.x_flag_selective_scheduling
6000 && !global_options_set.x_flag_selective_scheduling2)
6002 flag_selective_scheduling2 = 1;
6003 flag_sel_sched_pipelining = 1;
6005 if (mflag_sched_control_spec == 2)
6007 /* Control speculation is on by default for the selective scheduler,
6008 but not for the Haifa scheduler. */
6009 mflag_sched_control_spec = flag_selective_scheduling2 ? 1 : 0;
6011 if (flag_sel_sched_pipelining && flag_auto_inc_dec)
6013 /* FIXME: remove this when we'd implement breaking autoinsns as
6014 a transformation. */
6015 flag_auto_inc_dec = 0;
6019 /* Initialize the record of emitted frame related registers. */
6021 void ia64_init_expanders (void)
6023 memset (&emitted_frame_related_regs, 0, sizeof (emitted_frame_related_regs));
6026 static struct machine_function *
6027 ia64_init_machine_status (void)
6029 return ggc_alloc_cleared_machine_function ();
6032 static enum attr_itanium_class ia64_safe_itanium_class (rtx);
6033 static enum attr_type ia64_safe_type (rtx);
6035 static enum attr_itanium_class
6036 ia64_safe_itanium_class (rtx insn)
6038 if (recog_memoized (insn) >= 0)
6039 return get_attr_itanium_class (insn);
6040 else if (DEBUG_INSN_P (insn))
6041 return ITANIUM_CLASS_IGNORE;
6042 else
6043 return ITANIUM_CLASS_UNKNOWN;
6046 static enum attr_type
6047 ia64_safe_type (rtx insn)
6049 if (recog_memoized (insn) >= 0)
6050 return get_attr_type (insn);
6051 else
6052 return TYPE_UNKNOWN;
6055 /* The following collection of routines emit instruction group stop bits as
6056 necessary to avoid dependencies. */
6058 /* Need to track some additional registers as far as serialization is
6059 concerned so we can properly handle br.call and br.ret. We could
6060 make these registers visible to gcc, but since these registers are
6061 never explicitly used in gcc generated code, it seems wasteful to
6062 do so (plus it would make the call and return patterns needlessly
6063 complex). */
6064 #define REG_RP (BR_REG (0))
6065 #define REG_AR_CFM (FIRST_PSEUDO_REGISTER + 1)
6066 /* This is used for volatile asms which may require a stop bit immediately
6067 before and after them. */
6068 #define REG_VOLATILE (FIRST_PSEUDO_REGISTER + 2)
6069 #define AR_UNAT_BIT_0 (FIRST_PSEUDO_REGISTER + 3)
6070 #define NUM_REGS (AR_UNAT_BIT_0 + 64)
6072 /* For each register, we keep track of how it has been written in the
6073 current instruction group.
6075 If a register is written unconditionally (no qualifying predicate),
6076 WRITE_COUNT is set to 2 and FIRST_PRED is ignored.
6078 If a register is written if its qualifying predicate P is true, we
6079 set WRITE_COUNT to 1 and FIRST_PRED to P. Later on, the same register
6080 may be written again by the complement of P (P^1) and when this happens,
6081 WRITE_COUNT gets set to 2.
6083 The result of this is that whenever an insn attempts to write a register
6084 whose WRITE_COUNT is two, we need to issue an insn group barrier first.
6086 If a predicate register is written by a floating-point insn, we set
6087 WRITTEN_BY_FP to true.
6089 If a predicate register is written by an AND.ORCM we set WRITTEN_BY_AND
6090 to true; if it was written by an OR.ANDCM we set WRITTEN_BY_OR to true. */
6092 #if GCC_VERSION >= 4000
6093 #define RWS_FIELD_TYPE __extension__ unsigned short
6094 #else
6095 #define RWS_FIELD_TYPE unsigned int
6096 #endif
6097 struct reg_write_state
6099 RWS_FIELD_TYPE write_count : 2;
6100 RWS_FIELD_TYPE first_pred : 10;
6101 RWS_FIELD_TYPE written_by_fp : 1;
6102 RWS_FIELD_TYPE written_by_and : 1;
6103 RWS_FIELD_TYPE written_by_or : 1;
6106 /* Cumulative info for the current instruction group. */
6107 struct reg_write_state rws_sum[NUM_REGS];
6108 #ifdef ENABLE_CHECKING
6109 /* Bitmap whether a register has been written in the current insn. */
6110 HARD_REG_ELT_TYPE rws_insn[(NUM_REGS + HOST_BITS_PER_WIDEST_FAST_INT - 1)
6111 / HOST_BITS_PER_WIDEST_FAST_INT];
6113 static inline void
6114 rws_insn_set (int regno)
6116 gcc_assert (!TEST_HARD_REG_BIT (rws_insn, regno));
6117 SET_HARD_REG_BIT (rws_insn, regno);
6120 static inline int
6121 rws_insn_test (int regno)
6123 return TEST_HARD_REG_BIT (rws_insn, regno);
6125 #else
6126 /* When not checking, track just REG_AR_CFM and REG_VOLATILE. */
6127 unsigned char rws_insn[2];
6129 static inline void
6130 rws_insn_set (int regno)
6132 if (regno == REG_AR_CFM)
6133 rws_insn[0] = 1;
6134 else if (regno == REG_VOLATILE)
6135 rws_insn[1] = 1;
6138 static inline int
6139 rws_insn_test (int regno)
6141 if (regno == REG_AR_CFM)
6142 return rws_insn[0];
6143 if (regno == REG_VOLATILE)
6144 return rws_insn[1];
6145 return 0;
6147 #endif
6149 /* Indicates whether this is the first instruction after a stop bit,
6150 in which case we don't need another stop bit. Without this,
6151 ia64_variable_issue will die when scheduling an alloc. */
6152 static int first_instruction;
6154 /* Misc flags needed to compute RAW/WAW dependencies while we are traversing
6155 RTL for one instruction. */
6156 struct reg_flags
6158 unsigned int is_write : 1; /* Is register being written? */
6159 unsigned int is_fp : 1; /* Is register used as part of an fp op? */
6160 unsigned int is_branch : 1; /* Is register used as part of a branch? */
6161 unsigned int is_and : 1; /* Is register used as part of and.orcm? */
6162 unsigned int is_or : 1; /* Is register used as part of or.andcm? */
6163 unsigned int is_sibcall : 1; /* Is this a sibling or normal call? */
6166 static void rws_update (int, struct reg_flags, int);
6167 static int rws_access_regno (int, struct reg_flags, int);
6168 static int rws_access_reg (rtx, struct reg_flags, int);
6169 static void update_set_flags (rtx, struct reg_flags *);
6170 static int set_src_needs_barrier (rtx, struct reg_flags, int);
6171 static int rtx_needs_barrier (rtx, struct reg_flags, int);
6172 static void init_insn_group_barriers (void);
6173 static int group_barrier_needed (rtx);
6174 static int safe_group_barrier_needed (rtx);
6175 static int in_safe_group_barrier;
6177 /* Update *RWS for REGNO, which is being written by the current instruction,
6178 with predicate PRED, and associated register flags in FLAGS. */
6180 static void
6181 rws_update (int regno, struct reg_flags flags, int pred)
6183 if (pred)
6184 rws_sum[regno].write_count++;
6185 else
6186 rws_sum[regno].write_count = 2;
6187 rws_sum[regno].written_by_fp |= flags.is_fp;
6188 /* ??? Not tracking and/or across differing predicates. */
6189 rws_sum[regno].written_by_and = flags.is_and;
6190 rws_sum[regno].written_by_or = flags.is_or;
6191 rws_sum[regno].first_pred = pred;
6194 /* Handle an access to register REGNO of type FLAGS using predicate register
6195 PRED. Update rws_sum array. Return 1 if this access creates
6196 a dependency with an earlier instruction in the same group. */
6198 static int
6199 rws_access_regno (int regno, struct reg_flags flags, int pred)
6201 int need_barrier = 0;
6203 gcc_assert (regno < NUM_REGS);
6205 if (! PR_REGNO_P (regno))
6206 flags.is_and = flags.is_or = 0;
6208 if (flags.is_write)
6210 int write_count;
6212 rws_insn_set (regno);
6213 write_count = rws_sum[regno].write_count;
6215 switch (write_count)
6217 case 0:
6218 /* The register has not been written yet. */
6219 if (!in_safe_group_barrier)
6220 rws_update (regno, flags, pred);
6221 break;
6223 case 1:
6224 /* The register has been written via a predicate. Treat
6225 it like a unconditional write and do not try to check
6226 for complementary pred reg in earlier write. */
6227 if (flags.is_and && rws_sum[regno].written_by_and)
6229 else if (flags.is_or && rws_sum[regno].written_by_or)
6231 else
6232 need_barrier = 1;
6233 if (!in_safe_group_barrier)
6234 rws_update (regno, flags, pred);
6235 break;
6237 case 2:
6238 /* The register has been unconditionally written already. We
6239 need a barrier. */
6240 if (flags.is_and && rws_sum[regno].written_by_and)
6242 else if (flags.is_or && rws_sum[regno].written_by_or)
6244 else
6245 need_barrier = 1;
6246 if (!in_safe_group_barrier)
6248 rws_sum[regno].written_by_and = flags.is_and;
6249 rws_sum[regno].written_by_or = flags.is_or;
6251 break;
6253 default:
6254 gcc_unreachable ();
6257 else
6259 if (flags.is_branch)
6261 /* Branches have several RAW exceptions that allow to avoid
6262 barriers. */
6264 if (REGNO_REG_CLASS (regno) == BR_REGS || regno == AR_PFS_REGNUM)
6265 /* RAW dependencies on branch regs are permissible as long
6266 as the writer is a non-branch instruction. Since we
6267 never generate code that uses a branch register written
6268 by a branch instruction, handling this case is
6269 easy. */
6270 return 0;
6272 if (REGNO_REG_CLASS (regno) == PR_REGS
6273 && ! rws_sum[regno].written_by_fp)
6274 /* The predicates of a branch are available within the
6275 same insn group as long as the predicate was written by
6276 something other than a floating-point instruction. */
6277 return 0;
6280 if (flags.is_and && rws_sum[regno].written_by_and)
6281 return 0;
6282 if (flags.is_or && rws_sum[regno].written_by_or)
6283 return 0;
6285 switch (rws_sum[regno].write_count)
6287 case 0:
6288 /* The register has not been written yet. */
6289 break;
6291 case 1:
6292 /* The register has been written via a predicate, assume we
6293 need a barrier (don't check for complementary regs). */
6294 need_barrier = 1;
6295 break;
6297 case 2:
6298 /* The register has been unconditionally written already. We
6299 need a barrier. */
6300 need_barrier = 1;
6301 break;
6303 default:
6304 gcc_unreachable ();
6308 return need_barrier;
6311 static int
6312 rws_access_reg (rtx reg, struct reg_flags flags, int pred)
6314 int regno = REGNO (reg);
6315 int n = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
6317 if (n == 1)
6318 return rws_access_regno (regno, flags, pred);
6319 else
6321 int need_barrier = 0;
6322 while (--n >= 0)
6323 need_barrier |= rws_access_regno (regno + n, flags, pred);
6324 return need_barrier;
6328 /* Examine X, which is a SET rtx, and update the flags, the predicate, and
6329 the condition, stored in *PFLAGS, *PPRED and *PCOND. */
6331 static void
6332 update_set_flags (rtx x, struct reg_flags *pflags)
6334 rtx src = SET_SRC (x);
6336 switch (GET_CODE (src))
6338 case CALL:
6339 return;
6341 case IF_THEN_ELSE:
6342 /* There are four cases here:
6343 (1) The destination is (pc), in which case this is a branch,
6344 nothing here applies.
6345 (2) The destination is ar.lc, in which case this is a
6346 doloop_end_internal,
6347 (3) The destination is an fp register, in which case this is
6348 an fselect instruction.
6349 (4) The condition has (unspec [(reg)] UNSPEC_LDC), in which case
6350 this is a check load.
6351 In all cases, nothing we do in this function applies. */
6352 return;
6354 default:
6355 if (COMPARISON_P (src)
6356 && SCALAR_FLOAT_MODE_P (GET_MODE (XEXP (src, 0))))
6357 /* Set pflags->is_fp to 1 so that we know we're dealing
6358 with a floating point comparison when processing the
6359 destination of the SET. */
6360 pflags->is_fp = 1;
6362 /* Discover if this is a parallel comparison. We only handle
6363 and.orcm and or.andcm at present, since we must retain a
6364 strict inverse on the predicate pair. */
6365 else if (GET_CODE (src) == AND)
6366 pflags->is_and = 1;
6367 else if (GET_CODE (src) == IOR)
6368 pflags->is_or = 1;
6370 break;
6374 /* Subroutine of rtx_needs_barrier; this function determines whether the
6375 source of a given SET rtx found in X needs a barrier. FLAGS and PRED
6376 are as in rtx_needs_barrier. COND is an rtx that holds the condition
6377 for this insn. */
6379 static int
6380 set_src_needs_barrier (rtx x, struct reg_flags flags, int pred)
6382 int need_barrier = 0;
6383 rtx dst;
6384 rtx src = SET_SRC (x);
6386 if (GET_CODE (src) == CALL)
6387 /* We don't need to worry about the result registers that
6388 get written by subroutine call. */
6389 return rtx_needs_barrier (src, flags, pred);
6390 else if (SET_DEST (x) == pc_rtx)
6392 /* X is a conditional branch. */
6393 /* ??? This seems redundant, as the caller sets this bit for
6394 all JUMP_INSNs. */
6395 if (!ia64_spec_check_src_p (src))
6396 flags.is_branch = 1;
6397 return rtx_needs_barrier (src, flags, pred);
6400 if (ia64_spec_check_src_p (src))
6401 /* Avoid checking one register twice (in condition
6402 and in 'then' section) for ldc pattern. */
6404 gcc_assert (REG_P (XEXP (src, 2)));
6405 need_barrier = rtx_needs_barrier (XEXP (src, 2), flags, pred);
6407 /* We process MEM below. */
6408 src = XEXP (src, 1);
6411 need_barrier |= rtx_needs_barrier (src, flags, pred);
6413 dst = SET_DEST (x);
6414 if (GET_CODE (dst) == ZERO_EXTRACT)
6416 need_barrier |= rtx_needs_barrier (XEXP (dst, 1), flags, pred);
6417 need_barrier |= rtx_needs_barrier (XEXP (dst, 2), flags, pred);
6419 return need_barrier;
6422 /* Handle an access to rtx X of type FLAGS using predicate register
6423 PRED. Return 1 if this access creates a dependency with an earlier
6424 instruction in the same group. */
6426 static int
6427 rtx_needs_barrier (rtx x, struct reg_flags flags, int pred)
6429 int i, j;
6430 int is_complemented = 0;
6431 int need_barrier = 0;
6432 const char *format_ptr;
6433 struct reg_flags new_flags;
6434 rtx cond;
6436 if (! x)
6437 return 0;
6439 new_flags = flags;
6441 switch (GET_CODE (x))
6443 case SET:
6444 update_set_flags (x, &new_flags);
6445 need_barrier = set_src_needs_barrier (x, new_flags, pred);
6446 if (GET_CODE (SET_SRC (x)) != CALL)
6448 new_flags.is_write = 1;
6449 need_barrier |= rtx_needs_barrier (SET_DEST (x), new_flags, pred);
6451 break;
6453 case CALL:
6454 new_flags.is_write = 0;
6455 need_barrier |= rws_access_regno (AR_EC_REGNUM, new_flags, pred);
6457 /* Avoid multiple register writes, in case this is a pattern with
6458 multiple CALL rtx. This avoids a failure in rws_access_reg. */
6459 if (! flags.is_sibcall && ! rws_insn_test (REG_AR_CFM))
6461 new_flags.is_write = 1;
6462 need_barrier |= rws_access_regno (REG_RP, new_flags, pred);
6463 need_barrier |= rws_access_regno (AR_PFS_REGNUM, new_flags, pred);
6464 need_barrier |= rws_access_regno (REG_AR_CFM, new_flags, pred);
6466 break;
6468 case COND_EXEC:
6469 /* X is a predicated instruction. */
6471 cond = COND_EXEC_TEST (x);
6472 gcc_assert (!pred);
6473 need_barrier = rtx_needs_barrier (cond, flags, 0);
6475 if (GET_CODE (cond) == EQ)
6476 is_complemented = 1;
6477 cond = XEXP (cond, 0);
6478 gcc_assert (GET_CODE (cond) == REG
6479 && REGNO_REG_CLASS (REGNO (cond)) == PR_REGS);
6480 pred = REGNO (cond);
6481 if (is_complemented)
6482 ++pred;
6484 need_barrier |= rtx_needs_barrier (COND_EXEC_CODE (x), flags, pred);
6485 return need_barrier;
6487 case CLOBBER:
6488 case USE:
6489 /* Clobber & use are for earlier compiler-phases only. */
6490 break;
6492 case ASM_OPERANDS:
6493 case ASM_INPUT:
6494 /* We always emit stop bits for traditional asms. We emit stop bits
6495 for volatile extended asms if TARGET_VOL_ASM_STOP is true. */
6496 if (GET_CODE (x) != ASM_OPERANDS
6497 || (MEM_VOLATILE_P (x) && TARGET_VOL_ASM_STOP))
6499 /* Avoid writing the register multiple times if we have multiple
6500 asm outputs. This avoids a failure in rws_access_reg. */
6501 if (! rws_insn_test (REG_VOLATILE))
6503 new_flags.is_write = 1;
6504 rws_access_regno (REG_VOLATILE, new_flags, pred);
6506 return 1;
6509 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
6510 We cannot just fall through here since then we would be confused
6511 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
6512 traditional asms unlike their normal usage. */
6514 for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; --i)
6515 if (rtx_needs_barrier (ASM_OPERANDS_INPUT (x, i), flags, pred))
6516 need_barrier = 1;
6517 break;
6519 case PARALLEL:
6520 for (i = XVECLEN (x, 0) - 1; i >= 0; --i)
6522 rtx pat = XVECEXP (x, 0, i);
6523 switch (GET_CODE (pat))
6525 case SET:
6526 update_set_flags (pat, &new_flags);
6527 need_barrier |= set_src_needs_barrier (pat, new_flags, pred);
6528 break;
6530 case USE:
6531 case CALL:
6532 case ASM_OPERANDS:
6533 need_barrier |= rtx_needs_barrier (pat, flags, pred);
6534 break;
6536 case CLOBBER:
6537 if (REG_P (XEXP (pat, 0))
6538 && extract_asm_operands (x) != NULL_RTX
6539 && REGNO (XEXP (pat, 0)) != AR_UNAT_REGNUM)
6541 new_flags.is_write = 1;
6542 need_barrier |= rtx_needs_barrier (XEXP (pat, 0),
6543 new_flags, pred);
6544 new_flags = flags;
6546 break;
6548 case RETURN:
6549 break;
6551 default:
6552 gcc_unreachable ();
6555 for (i = XVECLEN (x, 0) - 1; i >= 0; --i)
6557 rtx pat = XVECEXP (x, 0, i);
6558 if (GET_CODE (pat) == SET)
6560 if (GET_CODE (SET_SRC (pat)) != CALL)
6562 new_flags.is_write = 1;
6563 need_barrier |= rtx_needs_barrier (SET_DEST (pat), new_flags,
6564 pred);
6567 else if (GET_CODE (pat) == CLOBBER || GET_CODE (pat) == RETURN)
6568 need_barrier |= rtx_needs_barrier (pat, flags, pred);
6570 break;
6572 case SUBREG:
6573 need_barrier |= rtx_needs_barrier (SUBREG_REG (x), flags, pred);
6574 break;
6575 case REG:
6576 if (REGNO (x) == AR_UNAT_REGNUM)
6578 for (i = 0; i < 64; ++i)
6579 need_barrier |= rws_access_regno (AR_UNAT_BIT_0 + i, flags, pred);
6581 else
6582 need_barrier = rws_access_reg (x, flags, pred);
6583 break;
6585 case MEM:
6586 /* Find the regs used in memory address computation. */
6587 new_flags.is_write = 0;
6588 need_barrier = rtx_needs_barrier (XEXP (x, 0), new_flags, pred);
6589 break;
6591 case CONST_INT: case CONST_DOUBLE: case CONST_VECTOR:
6592 case SYMBOL_REF: case LABEL_REF: case CONST:
6593 break;
6595 /* Operators with side-effects. */
6596 case POST_INC: case POST_DEC:
6597 gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
6599 new_flags.is_write = 0;
6600 need_barrier = rws_access_reg (XEXP (x, 0), new_flags, pred);
6601 new_flags.is_write = 1;
6602 need_barrier |= rws_access_reg (XEXP (x, 0), new_flags, pred);
6603 break;
6605 case POST_MODIFY:
6606 gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
6608 new_flags.is_write = 0;
6609 need_barrier = rws_access_reg (XEXP (x, 0), new_flags, pred);
6610 need_barrier |= rtx_needs_barrier (XEXP (x, 1), new_flags, pred);
6611 new_flags.is_write = 1;
6612 need_barrier |= rws_access_reg (XEXP (x, 0), new_flags, pred);
6613 break;
6615 /* Handle common unary and binary ops for efficiency. */
6616 case COMPARE: case PLUS: case MINUS: case MULT: case DIV:
6617 case MOD: case UDIV: case UMOD: case AND: case IOR:
6618 case XOR: case ASHIFT: case ROTATE: case ASHIFTRT: case LSHIFTRT:
6619 case ROTATERT: case SMIN: case SMAX: case UMIN: case UMAX:
6620 case NE: case EQ: case GE: case GT: case LE:
6621 case LT: case GEU: case GTU: case LEU: case LTU:
6622 need_barrier = rtx_needs_barrier (XEXP (x, 0), new_flags, pred);
6623 need_barrier |= rtx_needs_barrier (XEXP (x, 1), new_flags, pred);
6624 break;
6626 case NEG: case NOT: case SIGN_EXTEND: case ZERO_EXTEND:
6627 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE: case FLOAT:
6628 case FIX: case UNSIGNED_FLOAT: case UNSIGNED_FIX: case ABS:
6629 case SQRT: case FFS: case POPCOUNT:
6630 need_barrier = rtx_needs_barrier (XEXP (x, 0), flags, pred);
6631 break;
6633 case VEC_SELECT:
6634 /* VEC_SELECT's second argument is a PARALLEL with integers that
6635 describe the elements selected. On ia64, those integers are
6636 always constants. Avoid walking the PARALLEL so that we don't
6637 get confused with "normal" parallels and then die. */
6638 need_barrier = rtx_needs_barrier (XEXP (x, 0), flags, pred);
6639 break;
6641 case UNSPEC:
6642 switch (XINT (x, 1))
6644 case UNSPEC_LTOFF_DTPMOD:
6645 case UNSPEC_LTOFF_DTPREL:
6646 case UNSPEC_DTPREL:
6647 case UNSPEC_LTOFF_TPREL:
6648 case UNSPEC_TPREL:
6649 case UNSPEC_PRED_REL_MUTEX:
6650 case UNSPEC_PIC_CALL:
6651 case UNSPEC_MF:
6652 case UNSPEC_FETCHADD_ACQ:
6653 case UNSPEC_FETCHADD_REL:
6654 case UNSPEC_BSP_VALUE:
6655 case UNSPEC_FLUSHRS:
6656 case UNSPEC_BUNDLE_SELECTOR:
6657 break;
6659 case UNSPEC_GR_SPILL:
6660 case UNSPEC_GR_RESTORE:
6662 HOST_WIDE_INT offset = INTVAL (XVECEXP (x, 0, 1));
6663 HOST_WIDE_INT bit = (offset >> 3) & 63;
6665 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
6666 new_flags.is_write = (XINT (x, 1) == UNSPEC_GR_SPILL);
6667 need_barrier |= rws_access_regno (AR_UNAT_BIT_0 + bit,
6668 new_flags, pred);
6669 break;
6672 case UNSPEC_FR_SPILL:
6673 case UNSPEC_FR_RESTORE:
6674 case UNSPEC_GETF_EXP:
6675 case UNSPEC_SETF_EXP:
6676 case UNSPEC_ADDP4:
6677 case UNSPEC_FR_SQRT_RECIP_APPROX:
6678 case UNSPEC_FR_SQRT_RECIP_APPROX_RES:
6679 case UNSPEC_LDA:
6680 case UNSPEC_LDS:
6681 case UNSPEC_LDS_A:
6682 case UNSPEC_LDSA:
6683 case UNSPEC_CHKACLR:
6684 case UNSPEC_CHKS:
6685 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
6686 break;
6688 case UNSPEC_FR_RECIP_APPROX:
6689 case UNSPEC_SHRP:
6690 case UNSPEC_COPYSIGN:
6691 case UNSPEC_FR_RECIP_APPROX_RES:
6692 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
6693 need_barrier |= rtx_needs_barrier (XVECEXP (x, 0, 1), flags, pred);
6694 break;
6696 case UNSPEC_CMPXCHG_ACQ:
6697 case UNSPEC_CMPXCHG_REL:
6698 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 1), flags, pred);
6699 need_barrier |= rtx_needs_barrier (XVECEXP (x, 0, 2), flags, pred);
6700 break;
6702 default:
6703 gcc_unreachable ();
6705 break;
6707 case UNSPEC_VOLATILE:
6708 switch (XINT (x, 1))
6710 case UNSPECV_ALLOC:
6711 /* Alloc must always be the first instruction of a group.
6712 We force this by always returning true. */
6713 /* ??? We might get better scheduling if we explicitly check for
6714 input/local/output register dependencies, and modify the
6715 scheduler so that alloc is always reordered to the start of
6716 the current group. We could then eliminate all of the
6717 first_instruction code. */
6718 rws_access_regno (AR_PFS_REGNUM, flags, pred);
6720 new_flags.is_write = 1;
6721 rws_access_regno (REG_AR_CFM, new_flags, pred);
6722 return 1;
6724 case UNSPECV_SET_BSP:
6725 case UNSPECV_PROBE_STACK_RANGE:
6726 need_barrier = 1;
6727 break;
6729 case UNSPECV_BLOCKAGE:
6730 case UNSPECV_INSN_GROUP_BARRIER:
6731 case UNSPECV_BREAK:
6732 case UNSPECV_PSAC_ALL:
6733 case UNSPECV_PSAC_NORMAL:
6734 return 0;
6736 case UNSPECV_PROBE_STACK_ADDRESS:
6737 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
6738 break;
6740 default:
6741 gcc_unreachable ();
6743 break;
6745 case RETURN:
6746 new_flags.is_write = 0;
6747 need_barrier = rws_access_regno (REG_RP, flags, pred);
6748 need_barrier |= rws_access_regno (AR_PFS_REGNUM, flags, pred);
6750 new_flags.is_write = 1;
6751 need_barrier |= rws_access_regno (AR_EC_REGNUM, new_flags, pred);
6752 need_barrier |= rws_access_regno (REG_AR_CFM, new_flags, pred);
6753 break;
6755 default:
6756 format_ptr = GET_RTX_FORMAT (GET_CODE (x));
6757 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6758 switch (format_ptr[i])
6760 case '0': /* unused field */
6761 case 'i': /* integer */
6762 case 'n': /* note */
6763 case 'w': /* wide integer */
6764 case 's': /* pointer to string */
6765 case 'S': /* optional pointer to string */
6766 break;
6768 case 'e':
6769 if (rtx_needs_barrier (XEXP (x, i), flags, pred))
6770 need_barrier = 1;
6771 break;
6773 case 'E':
6774 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
6775 if (rtx_needs_barrier (XVECEXP (x, i, j), flags, pred))
6776 need_barrier = 1;
6777 break;
6779 default:
6780 gcc_unreachable ();
6782 break;
6784 return need_barrier;
6787 /* Clear out the state for group_barrier_needed at the start of a
6788 sequence of insns. */
6790 static void
6791 init_insn_group_barriers (void)
6793 memset (rws_sum, 0, sizeof (rws_sum));
6794 first_instruction = 1;
6797 /* Given the current state, determine whether a group barrier (a stop bit) is
6798 necessary before INSN. Return nonzero if so. This modifies the state to
6799 include the effects of INSN as a side-effect. */
6801 static int
6802 group_barrier_needed (rtx insn)
6804 rtx pat;
6805 int need_barrier = 0;
6806 struct reg_flags flags;
6808 memset (&flags, 0, sizeof (flags));
6809 switch (GET_CODE (insn))
6811 case NOTE:
6812 case DEBUG_INSN:
6813 break;
6815 case BARRIER:
6816 /* A barrier doesn't imply an instruction group boundary. */
6817 break;
6819 case CODE_LABEL:
6820 memset (rws_insn, 0, sizeof (rws_insn));
6821 return 1;
6823 case CALL_INSN:
6824 flags.is_branch = 1;
6825 flags.is_sibcall = SIBLING_CALL_P (insn);
6826 memset (rws_insn, 0, sizeof (rws_insn));
6828 /* Don't bundle a call following another call. */
6829 if ((pat = prev_active_insn (insn)) && CALL_P (pat))
6831 need_barrier = 1;
6832 break;
6835 need_barrier = rtx_needs_barrier (PATTERN (insn), flags, 0);
6836 break;
6838 case JUMP_INSN:
6839 if (!ia64_spec_check_p (insn))
6840 flags.is_branch = 1;
6842 /* Don't bundle a jump following a call. */
6843 if ((pat = prev_active_insn (insn)) && CALL_P (pat))
6845 need_barrier = 1;
6846 break;
6848 /* FALLTHRU */
6850 case INSN:
6851 if (GET_CODE (PATTERN (insn)) == USE
6852 || GET_CODE (PATTERN (insn)) == CLOBBER)
6853 /* Don't care about USE and CLOBBER "insns"---those are used to
6854 indicate to the optimizer that it shouldn't get rid of
6855 certain operations. */
6856 break;
6858 pat = PATTERN (insn);
6860 /* Ug. Hack hacks hacked elsewhere. */
6861 switch (recog_memoized (insn))
6863 /* We play dependency tricks with the epilogue in order
6864 to get proper schedules. Undo this for dv analysis. */
6865 case CODE_FOR_epilogue_deallocate_stack:
6866 case CODE_FOR_prologue_allocate_stack:
6867 pat = XVECEXP (pat, 0, 0);
6868 break;
6870 /* The pattern we use for br.cloop confuses the code above.
6871 The second element of the vector is representative. */
6872 case CODE_FOR_doloop_end_internal:
6873 pat = XVECEXP (pat, 0, 1);
6874 break;
6876 /* Doesn't generate code. */
6877 case CODE_FOR_pred_rel_mutex:
6878 case CODE_FOR_prologue_use:
6879 return 0;
6881 default:
6882 break;
6885 memset (rws_insn, 0, sizeof (rws_insn));
6886 need_barrier = rtx_needs_barrier (pat, flags, 0);
6888 /* Check to see if the previous instruction was a volatile
6889 asm. */
6890 if (! need_barrier)
6891 need_barrier = rws_access_regno (REG_VOLATILE, flags, 0);
6893 break;
6895 default:
6896 gcc_unreachable ();
6899 if (first_instruction && important_for_bundling_p (insn))
6901 need_barrier = 0;
6902 first_instruction = 0;
6905 return need_barrier;
6908 /* Like group_barrier_needed, but do not clobber the current state. */
6910 static int
6911 safe_group_barrier_needed (rtx insn)
6913 int saved_first_instruction;
6914 int t;
6916 saved_first_instruction = first_instruction;
6917 in_safe_group_barrier = 1;
6919 t = group_barrier_needed (insn);
6921 first_instruction = saved_first_instruction;
6922 in_safe_group_barrier = 0;
6924 return t;
6927 /* Scan the current function and insert stop bits as necessary to
6928 eliminate dependencies. This function assumes that a final
6929 instruction scheduling pass has been run which has already
6930 inserted most of the necessary stop bits. This function only
6931 inserts new ones at basic block boundaries, since these are
6932 invisible to the scheduler. */
6934 static void
6935 emit_insn_group_barriers (FILE *dump)
6937 rtx insn;
6938 rtx last_label = 0;
6939 int insns_since_last_label = 0;
6941 init_insn_group_barriers ();
6943 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6945 if (LABEL_P (insn))
6947 if (insns_since_last_label)
6948 last_label = insn;
6949 insns_since_last_label = 0;
6951 else if (NOTE_P (insn)
6952 && NOTE_KIND (insn) == NOTE_INSN_BASIC_BLOCK)
6954 if (insns_since_last_label)
6955 last_label = insn;
6956 insns_since_last_label = 0;
6958 else if (NONJUMP_INSN_P (insn)
6959 && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
6960 && XINT (PATTERN (insn), 1) == UNSPECV_INSN_GROUP_BARRIER)
6962 init_insn_group_barriers ();
6963 last_label = 0;
6965 else if (NONDEBUG_INSN_P (insn))
6967 insns_since_last_label = 1;
6969 if (group_barrier_needed (insn))
6971 if (last_label)
6973 if (dump)
6974 fprintf (dump, "Emitting stop before label %d\n",
6975 INSN_UID (last_label));
6976 emit_insn_before (gen_insn_group_barrier (GEN_INT (3)), last_label);
6977 insn = last_label;
6979 init_insn_group_barriers ();
6980 last_label = 0;
6987 /* Like emit_insn_group_barriers, but run if no final scheduling pass was run.
6988 This function has to emit all necessary group barriers. */
6990 static void
6991 emit_all_insn_group_barriers (FILE *dump ATTRIBUTE_UNUSED)
6993 rtx insn;
6995 init_insn_group_barriers ();
6997 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6999 if (BARRIER_P (insn))
7001 rtx last = prev_active_insn (insn);
7003 if (! last)
7004 continue;
7005 if (JUMP_TABLE_DATA_P (last))
7006 last = prev_active_insn (last);
7007 if (recog_memoized (last) != CODE_FOR_insn_group_barrier)
7008 emit_insn_after (gen_insn_group_barrier (GEN_INT (3)), last);
7010 init_insn_group_barriers ();
7012 else if (NONDEBUG_INSN_P (insn))
7014 if (recog_memoized (insn) == CODE_FOR_insn_group_barrier)
7015 init_insn_group_barriers ();
7016 else if (group_barrier_needed (insn))
7018 emit_insn_before (gen_insn_group_barrier (GEN_INT (3)), insn);
7019 init_insn_group_barriers ();
7020 group_barrier_needed (insn);
7028 /* Instruction scheduling support. */
7030 #define NR_BUNDLES 10
7032 /* A list of names of all available bundles. */
7034 static const char *bundle_name [NR_BUNDLES] =
7036 ".mii",
7037 ".mmi",
7038 ".mfi",
7039 ".mmf",
7040 #if NR_BUNDLES == 10
7041 ".bbb",
7042 ".mbb",
7043 #endif
7044 ".mib",
7045 ".mmb",
7046 ".mfb",
7047 ".mlx"
7050 /* Nonzero if we should insert stop bits into the schedule. */
7052 int ia64_final_schedule = 0;
7054 /* Codes of the corresponding queried units: */
7056 static int _0mii_, _0mmi_, _0mfi_, _0mmf_;
7057 static int _0bbb_, _0mbb_, _0mib_, _0mmb_, _0mfb_, _0mlx_;
7059 static int _1mii_, _1mmi_, _1mfi_, _1mmf_;
7060 static int _1bbb_, _1mbb_, _1mib_, _1mmb_, _1mfb_, _1mlx_;
7062 static int pos_1, pos_2, pos_3, pos_4, pos_5, pos_6;
7064 /* The following variable value is an insn group barrier. */
7066 static rtx dfa_stop_insn;
7068 /* The following variable value is the last issued insn. */
7070 static rtx last_scheduled_insn;
7072 /* The following variable value is pointer to a DFA state used as
7073 temporary variable. */
7075 static state_t temp_dfa_state = NULL;
7077 /* The following variable value is DFA state after issuing the last
7078 insn. */
7080 static state_t prev_cycle_state = NULL;
7082 /* The following array element values are TRUE if the corresponding
7083 insn requires to add stop bits before it. */
7085 static char *stops_p = NULL;
7087 /* The following variable is used to set up the mentioned above array. */
7089 static int stop_before_p = 0;
7091 /* The following variable value is length of the arrays `clocks' and
7092 `add_cycles'. */
7094 static int clocks_length;
7096 /* The following variable value is number of data speculations in progress. */
7097 static int pending_data_specs = 0;
7099 /* Number of memory references on current and three future processor cycles. */
7100 static char mem_ops_in_group[4];
7102 /* Number of current processor cycle (from scheduler's point of view). */
7103 static int current_cycle;
7105 static rtx ia64_single_set (rtx);
7106 static void ia64_emit_insn_before (rtx, rtx);
7108 /* Map a bundle number to its pseudo-op. */
7110 const char *
7111 get_bundle_name (int b)
7113 return bundle_name[b];
7117 /* Return the maximum number of instructions a cpu can issue. */
7119 static int
7120 ia64_issue_rate (void)
7122 return 6;
7125 /* Helper function - like single_set, but look inside COND_EXEC. */
7127 static rtx
7128 ia64_single_set (rtx insn)
7130 rtx x = PATTERN (insn), ret;
7131 if (GET_CODE (x) == COND_EXEC)
7132 x = COND_EXEC_CODE (x);
7133 if (GET_CODE (x) == SET)
7134 return x;
7136 /* Special case here prologue_allocate_stack and epilogue_deallocate_stack.
7137 Although they are not classical single set, the second set is there just
7138 to protect it from moving past FP-relative stack accesses. */
7139 switch (recog_memoized (insn))
7141 case CODE_FOR_prologue_allocate_stack:
7142 case CODE_FOR_epilogue_deallocate_stack:
7143 ret = XVECEXP (x, 0, 0);
7144 break;
7146 default:
7147 ret = single_set_2 (insn, x);
7148 break;
7151 return ret;
7154 /* Adjust the cost of a scheduling dependency.
7155 Return the new cost of a dependency of type DEP_TYPE or INSN on DEP_INSN.
7156 COST is the current cost, DW is dependency weakness. */
7157 static int
7158 ia64_adjust_cost_2 (rtx insn, int dep_type1, rtx dep_insn, int cost, dw_t dw)
7160 enum reg_note dep_type = (enum reg_note) dep_type1;
7161 enum attr_itanium_class dep_class;
7162 enum attr_itanium_class insn_class;
7164 insn_class = ia64_safe_itanium_class (insn);
7165 dep_class = ia64_safe_itanium_class (dep_insn);
7167 /* Treat true memory dependencies separately. Ignore apparent true
7168 dependence between store and call (call has a MEM inside a SYMBOL_REF). */
7169 if (dep_type == REG_DEP_TRUE
7170 && (dep_class == ITANIUM_CLASS_ST || dep_class == ITANIUM_CLASS_STF)
7171 && (insn_class == ITANIUM_CLASS_BR || insn_class == ITANIUM_CLASS_SCALL))
7172 return 0;
7174 if (dw == MIN_DEP_WEAK)
7175 /* Store and load are likely to alias, use higher cost to avoid stall. */
7176 return PARAM_VALUE (PARAM_SCHED_MEM_TRUE_DEP_COST);
7177 else if (dw > MIN_DEP_WEAK)
7179 /* Store and load are less likely to alias. */
7180 if (mflag_sched_fp_mem_deps_zero_cost && dep_class == ITANIUM_CLASS_STF)
7181 /* Assume there will be no cache conflict for floating-point data.
7182 For integer data, L1 conflict penalty is huge (17 cycles), so we
7183 never assume it will not cause a conflict. */
7184 return 0;
7185 else
7186 return cost;
7189 if (dep_type != REG_DEP_OUTPUT)
7190 return cost;
7192 if (dep_class == ITANIUM_CLASS_ST || dep_class == ITANIUM_CLASS_STF
7193 || insn_class == ITANIUM_CLASS_ST || insn_class == ITANIUM_CLASS_STF)
7194 return 0;
7196 return cost;
7199 /* Like emit_insn_before, but skip cycle_display notes.
7200 ??? When cycle display notes are implemented, update this. */
7202 static void
7203 ia64_emit_insn_before (rtx insn, rtx before)
7205 emit_insn_before (insn, before);
7208 /* The following function marks insns who produce addresses for load
7209 and store insns. Such insns will be placed into M slots because it
7210 decrease latency time for Itanium1 (see function
7211 `ia64_produce_address_p' and the DFA descriptions). */
7213 static void
7214 ia64_dependencies_evaluation_hook (rtx head, rtx tail)
7216 rtx insn, next, next_tail;
7218 /* Before reload, which_alternative is not set, which means that
7219 ia64_safe_itanium_class will produce wrong results for (at least)
7220 move instructions. */
7221 if (!reload_completed)
7222 return;
7224 next_tail = NEXT_INSN (tail);
7225 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
7226 if (INSN_P (insn))
7227 insn->call = 0;
7228 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
7229 if (INSN_P (insn)
7230 && ia64_safe_itanium_class (insn) == ITANIUM_CLASS_IALU)
7232 sd_iterator_def sd_it;
7233 dep_t dep;
7234 bool has_mem_op_consumer_p = false;
7236 FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
7238 enum attr_itanium_class c;
7240 if (DEP_TYPE (dep) != REG_DEP_TRUE)
7241 continue;
7243 next = DEP_CON (dep);
7244 c = ia64_safe_itanium_class (next);
7245 if ((c == ITANIUM_CLASS_ST
7246 || c == ITANIUM_CLASS_STF)
7247 && ia64_st_address_bypass_p (insn, next))
7249 has_mem_op_consumer_p = true;
7250 break;
7252 else if ((c == ITANIUM_CLASS_LD
7253 || c == ITANIUM_CLASS_FLD
7254 || c == ITANIUM_CLASS_FLDP)
7255 && ia64_ld_address_bypass_p (insn, next))
7257 has_mem_op_consumer_p = true;
7258 break;
7262 insn->call = has_mem_op_consumer_p;
7266 /* We're beginning a new block. Initialize data structures as necessary. */
7268 static void
7269 ia64_sched_init (FILE *dump ATTRIBUTE_UNUSED,
7270 int sched_verbose ATTRIBUTE_UNUSED,
7271 int max_ready ATTRIBUTE_UNUSED)
7273 #ifdef ENABLE_CHECKING
7274 rtx insn;
7276 if (!sel_sched_p () && reload_completed)
7277 for (insn = NEXT_INSN (current_sched_info->prev_head);
7278 insn != current_sched_info->next_tail;
7279 insn = NEXT_INSN (insn))
7280 gcc_assert (!SCHED_GROUP_P (insn));
7281 #endif
7282 last_scheduled_insn = NULL_RTX;
7283 init_insn_group_barriers ();
7285 current_cycle = 0;
7286 memset (mem_ops_in_group, 0, sizeof (mem_ops_in_group));
7289 /* We're beginning a scheduling pass. Check assertion. */
7291 static void
7292 ia64_sched_init_global (FILE *dump ATTRIBUTE_UNUSED,
7293 int sched_verbose ATTRIBUTE_UNUSED,
7294 int max_ready ATTRIBUTE_UNUSED)
7296 gcc_assert (pending_data_specs == 0);
7299 /* Scheduling pass is now finished. Free/reset static variable. */
7300 static void
7301 ia64_sched_finish_global (FILE *dump ATTRIBUTE_UNUSED,
7302 int sched_verbose ATTRIBUTE_UNUSED)
7304 gcc_assert (pending_data_specs == 0);
7307 /* Return TRUE if INSN is a load (either normal or speculative, but not a
7308 speculation check), FALSE otherwise. */
7309 static bool
7310 is_load_p (rtx insn)
7312 enum attr_itanium_class insn_class = ia64_safe_itanium_class (insn);
7314 return
7315 ((insn_class == ITANIUM_CLASS_LD || insn_class == ITANIUM_CLASS_FLD)
7316 && get_attr_check_load (insn) == CHECK_LOAD_NO);
7319 /* If INSN is a memory reference, memoize it in MEM_OPS_IN_GROUP global array
7320 (taking account for 3-cycle cache reference postponing for stores: Intel
7321 Itanium 2 Reference Manual for Software Development and Optimization,
7322 6.7.3.1). */
7323 static void
7324 record_memory_reference (rtx insn)
7326 enum attr_itanium_class insn_class = ia64_safe_itanium_class (insn);
7328 switch (insn_class) {
7329 case ITANIUM_CLASS_FLD:
7330 case ITANIUM_CLASS_LD:
7331 mem_ops_in_group[current_cycle % 4]++;
7332 break;
7333 case ITANIUM_CLASS_STF:
7334 case ITANIUM_CLASS_ST:
7335 mem_ops_in_group[(current_cycle + 3) % 4]++;
7336 break;
7337 default:;
7341 /* We are about to being issuing insns for this clock cycle.
7342 Override the default sort algorithm to better slot instructions. */
7344 static int
7345 ia64_dfa_sched_reorder (FILE *dump, int sched_verbose, rtx *ready,
7346 int *pn_ready, int clock_var,
7347 int reorder_type)
7349 int n_asms;
7350 int n_ready = *pn_ready;
7351 rtx *e_ready = ready + n_ready;
7352 rtx *insnp;
7354 if (sched_verbose)
7355 fprintf (dump, "// ia64_dfa_sched_reorder (type %d):\n", reorder_type);
7357 if (reorder_type == 0)
7359 /* First, move all USEs, CLOBBERs and other crud out of the way. */
7360 n_asms = 0;
7361 for (insnp = ready; insnp < e_ready; insnp++)
7362 if (insnp < e_ready)
7364 rtx insn = *insnp;
7365 enum attr_type t = ia64_safe_type (insn);
7366 if (t == TYPE_UNKNOWN)
7368 if (GET_CODE (PATTERN (insn)) == ASM_INPUT
7369 || asm_noperands (PATTERN (insn)) >= 0)
7371 rtx lowest = ready[n_asms];
7372 ready[n_asms] = insn;
7373 *insnp = lowest;
7374 n_asms++;
7376 else
7378 rtx highest = ready[n_ready - 1];
7379 ready[n_ready - 1] = insn;
7380 *insnp = highest;
7381 return 1;
7386 if (n_asms < n_ready)
7388 /* Some normal insns to process. Skip the asms. */
7389 ready += n_asms;
7390 n_ready -= n_asms;
7392 else if (n_ready > 0)
7393 return 1;
7396 if (ia64_final_schedule)
7398 int deleted = 0;
7399 int nr_need_stop = 0;
7401 for (insnp = ready; insnp < e_ready; insnp++)
7402 if (safe_group_barrier_needed (*insnp))
7403 nr_need_stop++;
7405 if (reorder_type == 1 && n_ready == nr_need_stop)
7406 return 0;
7407 if (reorder_type == 0)
7408 return 1;
7409 insnp = e_ready;
7410 /* Move down everything that needs a stop bit, preserving
7411 relative order. */
7412 while (insnp-- > ready + deleted)
7413 while (insnp >= ready + deleted)
7415 rtx insn = *insnp;
7416 if (! safe_group_barrier_needed (insn))
7417 break;
7418 memmove (ready + 1, ready, (insnp - ready) * sizeof (rtx));
7419 *ready = insn;
7420 deleted++;
7422 n_ready -= deleted;
7423 ready += deleted;
7426 current_cycle = clock_var;
7427 if (reload_completed && mem_ops_in_group[clock_var % 4] >= ia64_max_memory_insns)
7429 int moved = 0;
7431 insnp = e_ready;
7432 /* Move down loads/stores, preserving relative order. */
7433 while (insnp-- > ready + moved)
7434 while (insnp >= ready + moved)
7436 rtx insn = *insnp;
7437 if (! is_load_p (insn))
7438 break;
7439 memmove (ready + 1, ready, (insnp - ready) * sizeof (rtx));
7440 *ready = insn;
7441 moved++;
7443 n_ready -= moved;
7444 ready += moved;
7447 return 1;
7450 /* We are about to being issuing insns for this clock cycle. Override
7451 the default sort algorithm to better slot instructions. */
7453 static int
7454 ia64_sched_reorder (FILE *dump, int sched_verbose, rtx *ready, int *pn_ready,
7455 int clock_var)
7457 return ia64_dfa_sched_reorder (dump, sched_verbose, ready,
7458 pn_ready, clock_var, 0);
7461 /* Like ia64_sched_reorder, but called after issuing each insn.
7462 Override the default sort algorithm to better slot instructions. */
7464 static int
7465 ia64_sched_reorder2 (FILE *dump ATTRIBUTE_UNUSED,
7466 int sched_verbose ATTRIBUTE_UNUSED, rtx *ready,
7467 int *pn_ready, int clock_var)
7469 return ia64_dfa_sched_reorder (dump, sched_verbose, ready, pn_ready,
7470 clock_var, 1);
7473 /* We are about to issue INSN. Return the number of insns left on the
7474 ready queue that can be issued this cycle. */
7476 static int
7477 ia64_variable_issue (FILE *dump ATTRIBUTE_UNUSED,
7478 int sched_verbose ATTRIBUTE_UNUSED,
7479 rtx insn ATTRIBUTE_UNUSED,
7480 int can_issue_more ATTRIBUTE_UNUSED)
7482 if (sched_deps_info->generate_spec_deps && !sel_sched_p ())
7483 /* Modulo scheduling does not extend h_i_d when emitting
7484 new instructions. Don't use h_i_d, if we don't have to. */
7486 if (DONE_SPEC (insn) & BEGIN_DATA)
7487 pending_data_specs++;
7488 if (CHECK_SPEC (insn) & BEGIN_DATA)
7489 pending_data_specs--;
7492 if (DEBUG_INSN_P (insn))
7493 return 1;
7495 last_scheduled_insn = insn;
7496 memcpy (prev_cycle_state, curr_state, dfa_state_size);
7497 if (reload_completed)
7499 int needed = group_barrier_needed (insn);
7501 gcc_assert (!needed);
7502 if (CALL_P (insn))
7503 init_insn_group_barriers ();
7504 stops_p [INSN_UID (insn)] = stop_before_p;
7505 stop_before_p = 0;
7507 record_memory_reference (insn);
7509 return 1;
7512 /* We are choosing insn from the ready queue. Return nonzero if INSN
7513 can be chosen. */
7515 static int
7516 ia64_first_cycle_multipass_dfa_lookahead_guard (rtx insn)
7518 gcc_assert (insn && INSN_P (insn));
7519 return ((!reload_completed
7520 || !safe_group_barrier_needed (insn))
7521 && ia64_first_cycle_multipass_dfa_lookahead_guard_spec (insn)
7522 && (!mflag_sched_mem_insns_hard_limit
7523 || !is_load_p (insn)
7524 || mem_ops_in_group[current_cycle % 4] < ia64_max_memory_insns));
7527 /* We are choosing insn from the ready queue. Return nonzero if INSN
7528 can be chosen. */
7530 static bool
7531 ia64_first_cycle_multipass_dfa_lookahead_guard_spec (const_rtx insn)
7533 gcc_assert (insn && INSN_P (insn));
7534 /* Size of ALAT is 32. As far as we perform conservative data speculation,
7535 we keep ALAT half-empty. */
7536 return (pending_data_specs < 16
7537 || !(TODO_SPEC (insn) & BEGIN_DATA));
7540 /* The following variable value is pseudo-insn used by the DFA insn
7541 scheduler to change the DFA state when the simulated clock is
7542 increased. */
7544 static rtx dfa_pre_cycle_insn;
7546 /* Returns 1 when a meaningful insn was scheduled between the last group
7547 barrier and LAST. */
7548 static int
7549 scheduled_good_insn (rtx last)
7551 if (last && recog_memoized (last) >= 0)
7552 return 1;
7554 for ( ;
7555 last != NULL && !NOTE_INSN_BASIC_BLOCK_P (last)
7556 && !stops_p[INSN_UID (last)];
7557 last = PREV_INSN (last))
7558 /* We could hit a NOTE_INSN_DELETED here which is actually outside
7559 the ebb we're scheduling. */
7560 if (INSN_P (last) && recog_memoized (last) >= 0)
7561 return 1;
7563 return 0;
7566 /* We are about to being issuing INSN. Return nonzero if we cannot
7567 issue it on given cycle CLOCK and return zero if we should not sort
7568 the ready queue on the next clock start. */
7570 static int
7571 ia64_dfa_new_cycle (FILE *dump, int verbose, rtx insn, int last_clock,
7572 int clock, int *sort_p)
7574 gcc_assert (insn && INSN_P (insn));
7576 if (DEBUG_INSN_P (insn))
7577 return 0;
7579 /* When a group barrier is needed for insn, last_scheduled_insn
7580 should be set. */
7581 gcc_assert (!(reload_completed && safe_group_barrier_needed (insn))
7582 || last_scheduled_insn);
7584 if ((reload_completed
7585 && (safe_group_barrier_needed (insn)
7586 || (mflag_sched_stop_bits_after_every_cycle
7587 && last_clock != clock
7588 && last_scheduled_insn
7589 && scheduled_good_insn (last_scheduled_insn))))
7590 || (last_scheduled_insn
7591 && (CALL_P (last_scheduled_insn)
7592 || unknown_for_bundling_p (last_scheduled_insn))))
7594 init_insn_group_barriers ();
7596 if (verbose && dump)
7597 fprintf (dump, "// Stop should be before %d%s\n", INSN_UID (insn),
7598 last_clock == clock ? " + cycle advance" : "");
7600 stop_before_p = 1;
7601 current_cycle = clock;
7602 mem_ops_in_group[current_cycle % 4] = 0;
7604 if (last_clock == clock)
7606 state_transition (curr_state, dfa_stop_insn);
7607 if (TARGET_EARLY_STOP_BITS)
7608 *sort_p = (last_scheduled_insn == NULL_RTX
7609 || ! CALL_P (last_scheduled_insn));
7610 else
7611 *sort_p = 0;
7612 return 1;
7615 if (last_scheduled_insn)
7617 if (unknown_for_bundling_p (last_scheduled_insn))
7618 state_reset (curr_state);
7619 else
7621 memcpy (curr_state, prev_cycle_state, dfa_state_size);
7622 state_transition (curr_state, dfa_stop_insn);
7623 state_transition (curr_state, dfa_pre_cycle_insn);
7624 state_transition (curr_state, NULL);
7628 return 0;
7631 /* Implement targetm.sched.h_i_d_extended hook.
7632 Extend internal data structures. */
7633 static void
7634 ia64_h_i_d_extended (void)
7636 if (stops_p != NULL)
7638 int new_clocks_length = get_max_uid () * 3 / 2;
7639 stops_p = (char *) xrecalloc (stops_p, new_clocks_length, clocks_length, 1);
7640 clocks_length = new_clocks_length;
7645 /* This structure describes the data used by the backend to guide scheduling.
7646 When the current scheduling point is switched, this data should be saved
7647 and restored later, if the scheduler returns to this point. */
7648 struct _ia64_sched_context
7650 state_t prev_cycle_state;
7651 rtx last_scheduled_insn;
7652 struct reg_write_state rws_sum[NUM_REGS];
7653 struct reg_write_state rws_insn[NUM_REGS];
7654 int first_instruction;
7655 int pending_data_specs;
7656 int current_cycle;
7657 char mem_ops_in_group[4];
7659 typedef struct _ia64_sched_context *ia64_sched_context_t;
7661 /* Allocates a scheduling context. */
7662 static void *
7663 ia64_alloc_sched_context (void)
7665 return xmalloc (sizeof (struct _ia64_sched_context));
7668 /* Initializes the _SC context with clean data, if CLEAN_P, and from
7669 the global context otherwise. */
7670 static void
7671 ia64_init_sched_context (void *_sc, bool clean_p)
7673 ia64_sched_context_t sc = (ia64_sched_context_t) _sc;
7675 sc->prev_cycle_state = xmalloc (dfa_state_size);
7676 if (clean_p)
7678 state_reset (sc->prev_cycle_state);
7679 sc->last_scheduled_insn = NULL_RTX;
7680 memset (sc->rws_sum, 0, sizeof (rws_sum));
7681 memset (sc->rws_insn, 0, sizeof (rws_insn));
7682 sc->first_instruction = 1;
7683 sc->pending_data_specs = 0;
7684 sc->current_cycle = 0;
7685 memset (sc->mem_ops_in_group, 0, sizeof (mem_ops_in_group));
7687 else
7689 memcpy (sc->prev_cycle_state, prev_cycle_state, dfa_state_size);
7690 sc->last_scheduled_insn = last_scheduled_insn;
7691 memcpy (sc->rws_sum, rws_sum, sizeof (rws_sum));
7692 memcpy (sc->rws_insn, rws_insn, sizeof (rws_insn));
7693 sc->first_instruction = first_instruction;
7694 sc->pending_data_specs = pending_data_specs;
7695 sc->current_cycle = current_cycle;
7696 memcpy (sc->mem_ops_in_group, mem_ops_in_group, sizeof (mem_ops_in_group));
7700 /* Sets the global scheduling context to the one pointed to by _SC. */
7701 static void
7702 ia64_set_sched_context (void *_sc)
7704 ia64_sched_context_t sc = (ia64_sched_context_t) _sc;
7706 gcc_assert (sc != NULL);
7708 memcpy (prev_cycle_state, sc->prev_cycle_state, dfa_state_size);
7709 last_scheduled_insn = sc->last_scheduled_insn;
7710 memcpy (rws_sum, sc->rws_sum, sizeof (rws_sum));
7711 memcpy (rws_insn, sc->rws_insn, sizeof (rws_insn));
7712 first_instruction = sc->first_instruction;
7713 pending_data_specs = sc->pending_data_specs;
7714 current_cycle = sc->current_cycle;
7715 memcpy (mem_ops_in_group, sc->mem_ops_in_group, sizeof (mem_ops_in_group));
7718 /* Clears the data in the _SC scheduling context. */
7719 static void
7720 ia64_clear_sched_context (void *_sc)
7722 ia64_sched_context_t sc = (ia64_sched_context_t) _sc;
7724 free (sc->prev_cycle_state);
7725 sc->prev_cycle_state = NULL;
7728 /* Frees the _SC scheduling context. */
7729 static void
7730 ia64_free_sched_context (void *_sc)
7732 gcc_assert (_sc != NULL);
7734 free (_sc);
7737 typedef rtx (* gen_func_t) (rtx, rtx);
7739 /* Return a function that will generate a load of mode MODE_NO
7740 with speculation types TS. */
7741 static gen_func_t
7742 get_spec_load_gen_function (ds_t ts, int mode_no)
7744 static gen_func_t gen_ld_[] = {
7745 gen_movbi,
7746 gen_movqi_internal,
7747 gen_movhi_internal,
7748 gen_movsi_internal,
7749 gen_movdi_internal,
7750 gen_movsf_internal,
7751 gen_movdf_internal,
7752 gen_movxf_internal,
7753 gen_movti_internal,
7754 gen_zero_extendqidi2,
7755 gen_zero_extendhidi2,
7756 gen_zero_extendsidi2,
7759 static gen_func_t gen_ld_a[] = {
7760 gen_movbi_advanced,
7761 gen_movqi_advanced,
7762 gen_movhi_advanced,
7763 gen_movsi_advanced,
7764 gen_movdi_advanced,
7765 gen_movsf_advanced,
7766 gen_movdf_advanced,
7767 gen_movxf_advanced,
7768 gen_movti_advanced,
7769 gen_zero_extendqidi2_advanced,
7770 gen_zero_extendhidi2_advanced,
7771 gen_zero_extendsidi2_advanced,
7773 static gen_func_t gen_ld_s[] = {
7774 gen_movbi_speculative,
7775 gen_movqi_speculative,
7776 gen_movhi_speculative,
7777 gen_movsi_speculative,
7778 gen_movdi_speculative,
7779 gen_movsf_speculative,
7780 gen_movdf_speculative,
7781 gen_movxf_speculative,
7782 gen_movti_speculative,
7783 gen_zero_extendqidi2_speculative,
7784 gen_zero_extendhidi2_speculative,
7785 gen_zero_extendsidi2_speculative,
7787 static gen_func_t gen_ld_sa[] = {
7788 gen_movbi_speculative_advanced,
7789 gen_movqi_speculative_advanced,
7790 gen_movhi_speculative_advanced,
7791 gen_movsi_speculative_advanced,
7792 gen_movdi_speculative_advanced,
7793 gen_movsf_speculative_advanced,
7794 gen_movdf_speculative_advanced,
7795 gen_movxf_speculative_advanced,
7796 gen_movti_speculative_advanced,
7797 gen_zero_extendqidi2_speculative_advanced,
7798 gen_zero_extendhidi2_speculative_advanced,
7799 gen_zero_extendsidi2_speculative_advanced,
7801 static gen_func_t gen_ld_s_a[] = {
7802 gen_movbi_speculative_a,
7803 gen_movqi_speculative_a,
7804 gen_movhi_speculative_a,
7805 gen_movsi_speculative_a,
7806 gen_movdi_speculative_a,
7807 gen_movsf_speculative_a,
7808 gen_movdf_speculative_a,
7809 gen_movxf_speculative_a,
7810 gen_movti_speculative_a,
7811 gen_zero_extendqidi2_speculative_a,
7812 gen_zero_extendhidi2_speculative_a,
7813 gen_zero_extendsidi2_speculative_a,
7816 gen_func_t *gen_ld;
7818 if (ts & BEGIN_DATA)
7820 if (ts & BEGIN_CONTROL)
7821 gen_ld = gen_ld_sa;
7822 else
7823 gen_ld = gen_ld_a;
7825 else if (ts & BEGIN_CONTROL)
7827 if ((spec_info->flags & SEL_SCHED_SPEC_DONT_CHECK_CONTROL)
7828 || ia64_needs_block_p (ts))
7829 gen_ld = gen_ld_s;
7830 else
7831 gen_ld = gen_ld_s_a;
7833 else if (ts == 0)
7834 gen_ld = gen_ld_;
7835 else
7836 gcc_unreachable ();
7838 return gen_ld[mode_no];
7841 /* Constants that help mapping 'enum machine_mode' to int. */
7842 enum SPEC_MODES
7844 SPEC_MODE_INVALID = -1,
7845 SPEC_MODE_FIRST = 0,
7846 SPEC_MODE_FOR_EXTEND_FIRST = 1,
7847 SPEC_MODE_FOR_EXTEND_LAST = 3,
7848 SPEC_MODE_LAST = 8
7851 enum
7853 /* Offset to reach ZERO_EXTEND patterns. */
7854 SPEC_GEN_EXTEND_OFFSET = SPEC_MODE_LAST - SPEC_MODE_FOR_EXTEND_FIRST + 1
7857 /* Return index of the MODE. */
7858 static int
7859 ia64_mode_to_int (enum machine_mode mode)
7861 switch (mode)
7863 case BImode: return 0; /* SPEC_MODE_FIRST */
7864 case QImode: return 1; /* SPEC_MODE_FOR_EXTEND_FIRST */
7865 case HImode: return 2;
7866 case SImode: return 3; /* SPEC_MODE_FOR_EXTEND_LAST */
7867 case DImode: return 4;
7868 case SFmode: return 5;
7869 case DFmode: return 6;
7870 case XFmode: return 7;
7871 case TImode:
7872 /* ??? This mode needs testing. Bypasses for ldfp8 instruction are not
7873 mentioned in itanium[12].md. Predicate fp_register_operand also
7874 needs to be defined. Bottom line: better disable for now. */
7875 return SPEC_MODE_INVALID;
7876 default: return SPEC_MODE_INVALID;
7880 /* Provide information about speculation capabilities. */
7881 static void
7882 ia64_set_sched_flags (spec_info_t spec_info)
7884 unsigned int *flags = &(current_sched_info->flags);
7886 if (*flags & SCHED_RGN
7887 || *flags & SCHED_EBB
7888 || *flags & SEL_SCHED)
7890 int mask = 0;
7892 if ((mflag_sched_br_data_spec && !reload_completed && optimize > 0)
7893 || (mflag_sched_ar_data_spec && reload_completed))
7895 mask |= BEGIN_DATA;
7897 if (!sel_sched_p ()
7898 && ((mflag_sched_br_in_data_spec && !reload_completed)
7899 || (mflag_sched_ar_in_data_spec && reload_completed)))
7900 mask |= BE_IN_DATA;
7903 if (mflag_sched_control_spec
7904 && (!sel_sched_p ()
7905 || reload_completed))
7907 mask |= BEGIN_CONTROL;
7909 if (!sel_sched_p () && mflag_sched_in_control_spec)
7910 mask |= BE_IN_CONTROL;
7913 spec_info->mask = mask;
7915 if (mask)
7917 *flags |= USE_DEPS_LIST | DO_SPECULATION;
7919 if (mask & BE_IN_SPEC)
7920 *flags |= NEW_BBS;
7922 spec_info->flags = 0;
7924 if ((mask & DATA_SPEC) && mflag_sched_prefer_non_data_spec_insns)
7925 spec_info->flags |= PREFER_NON_DATA_SPEC;
7927 if (mask & CONTROL_SPEC)
7929 if (mflag_sched_prefer_non_control_spec_insns)
7930 spec_info->flags |= PREFER_NON_CONTROL_SPEC;
7932 if (sel_sched_p () && mflag_sel_sched_dont_check_control_spec)
7933 spec_info->flags |= SEL_SCHED_SPEC_DONT_CHECK_CONTROL;
7936 if (sched_verbose >= 1)
7937 spec_info->dump = sched_dump;
7938 else
7939 spec_info->dump = 0;
7941 if (mflag_sched_count_spec_in_critical_path)
7942 spec_info->flags |= COUNT_SPEC_IN_CRITICAL_PATH;
7945 else
7946 spec_info->mask = 0;
7949 /* If INSN is an appropriate load return its mode.
7950 Return -1 otherwise. */
7951 static int
7952 get_mode_no_for_insn (rtx insn)
7954 rtx reg, mem, mode_rtx;
7955 int mode_no;
7956 bool extend_p;
7958 extract_insn_cached (insn);
7960 /* We use WHICH_ALTERNATIVE only after reload. This will
7961 guarantee that reload won't touch a speculative insn. */
7963 if (recog_data.n_operands != 2)
7964 return -1;
7966 reg = recog_data.operand[0];
7967 mem = recog_data.operand[1];
7969 /* We should use MEM's mode since REG's mode in presence of
7970 ZERO_EXTEND will always be DImode. */
7971 if (get_attr_speculable1 (insn) == SPECULABLE1_YES)
7972 /* Process non-speculative ld. */
7974 if (!reload_completed)
7976 /* Do not speculate into regs like ar.lc. */
7977 if (!REG_P (reg) || AR_REGNO_P (REGNO (reg)))
7978 return -1;
7980 if (!MEM_P (mem))
7981 return -1;
7984 rtx mem_reg = XEXP (mem, 0);
7986 if (!REG_P (mem_reg))
7987 return -1;
7990 mode_rtx = mem;
7992 else if (get_attr_speculable2 (insn) == SPECULABLE2_YES)
7994 gcc_assert (REG_P (reg) && MEM_P (mem));
7995 mode_rtx = mem;
7997 else
7998 return -1;
8000 else if (get_attr_data_speculative (insn) == DATA_SPECULATIVE_YES
8001 || get_attr_control_speculative (insn) == CONTROL_SPECULATIVE_YES
8002 || get_attr_check_load (insn) == CHECK_LOAD_YES)
8003 /* Process speculative ld or ld.c. */
8005 gcc_assert (REG_P (reg) && MEM_P (mem));
8006 mode_rtx = mem;
8008 else
8010 enum attr_itanium_class attr_class = get_attr_itanium_class (insn);
8012 if (attr_class == ITANIUM_CLASS_CHK_A
8013 || attr_class == ITANIUM_CLASS_CHK_S_I
8014 || attr_class == ITANIUM_CLASS_CHK_S_F)
8015 /* Process chk. */
8016 mode_rtx = reg;
8017 else
8018 return -1;
8021 mode_no = ia64_mode_to_int (GET_MODE (mode_rtx));
8023 if (mode_no == SPEC_MODE_INVALID)
8024 return -1;
8026 extend_p = (GET_MODE (reg) != GET_MODE (mode_rtx));
8028 if (extend_p)
8030 if (!(SPEC_MODE_FOR_EXTEND_FIRST <= mode_no
8031 && mode_no <= SPEC_MODE_FOR_EXTEND_LAST))
8032 return -1;
8034 mode_no += SPEC_GEN_EXTEND_OFFSET;
8037 return mode_no;
8040 /* If X is an unspec part of a speculative load, return its code.
8041 Return -1 otherwise. */
8042 static int
8043 get_spec_unspec_code (const_rtx x)
8045 if (GET_CODE (x) != UNSPEC)
8046 return -1;
8049 int code;
8051 code = XINT (x, 1);
8053 switch (code)
8055 case UNSPEC_LDA:
8056 case UNSPEC_LDS:
8057 case UNSPEC_LDS_A:
8058 case UNSPEC_LDSA:
8059 return code;
8061 default:
8062 return -1;
8067 /* Implement skip_rtx_p hook. */
8068 static bool
8069 ia64_skip_rtx_p (const_rtx x)
8071 return get_spec_unspec_code (x) != -1;
8074 /* If INSN is a speculative load, return its UNSPEC code.
8075 Return -1 otherwise. */
8076 static int
8077 get_insn_spec_code (const_rtx insn)
8079 rtx pat, reg, mem;
8081 pat = PATTERN (insn);
8083 if (GET_CODE (pat) == COND_EXEC)
8084 pat = COND_EXEC_CODE (pat);
8086 if (GET_CODE (pat) != SET)
8087 return -1;
8089 reg = SET_DEST (pat);
8090 if (!REG_P (reg))
8091 return -1;
8093 mem = SET_SRC (pat);
8094 if (GET_CODE (mem) == ZERO_EXTEND)
8095 mem = XEXP (mem, 0);
8097 return get_spec_unspec_code (mem);
8100 /* If INSN is a speculative load, return a ds with the speculation types.
8101 Otherwise [if INSN is a normal instruction] return 0. */
8102 static ds_t
8103 ia64_get_insn_spec_ds (rtx insn)
8105 int code = get_insn_spec_code (insn);
8107 switch (code)
8109 case UNSPEC_LDA:
8110 return BEGIN_DATA;
8112 case UNSPEC_LDS:
8113 case UNSPEC_LDS_A:
8114 return BEGIN_CONTROL;
8116 case UNSPEC_LDSA:
8117 return BEGIN_DATA | BEGIN_CONTROL;
8119 default:
8120 return 0;
8124 /* If INSN is a speculative load return a ds with the speculation types that
8125 will be checked.
8126 Otherwise [if INSN is a normal instruction] return 0. */
8127 static ds_t
8128 ia64_get_insn_checked_ds (rtx insn)
8130 int code = get_insn_spec_code (insn);
8132 switch (code)
8134 case UNSPEC_LDA:
8135 return BEGIN_DATA | BEGIN_CONTROL;
8137 case UNSPEC_LDS:
8138 return BEGIN_CONTROL;
8140 case UNSPEC_LDS_A:
8141 case UNSPEC_LDSA:
8142 return BEGIN_DATA | BEGIN_CONTROL;
8144 default:
8145 return 0;
8149 /* If GEN_P is true, calculate the index of needed speculation check and return
8150 speculative pattern for INSN with speculative mode TS, machine mode
8151 MODE_NO and with ZERO_EXTEND (if EXTEND_P is true).
8152 If GEN_P is false, just calculate the index of needed speculation check. */
8153 static rtx
8154 ia64_gen_spec_load (rtx insn, ds_t ts, int mode_no)
8156 rtx pat, new_pat;
8157 gen_func_t gen_load;
8159 gen_load = get_spec_load_gen_function (ts, mode_no);
8161 new_pat = gen_load (copy_rtx (recog_data.operand[0]),
8162 copy_rtx (recog_data.operand[1]));
8164 pat = PATTERN (insn);
8165 if (GET_CODE (pat) == COND_EXEC)
8166 new_pat = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (COND_EXEC_TEST (pat)),
8167 new_pat);
8169 return new_pat;
8172 static bool
8173 insn_can_be_in_speculative_p (rtx insn ATTRIBUTE_UNUSED,
8174 ds_t ds ATTRIBUTE_UNUSED)
8176 return false;
8179 /* Implement targetm.sched.speculate_insn hook.
8180 Check if the INSN can be TS speculative.
8181 If 'no' - return -1.
8182 If 'yes' - generate speculative pattern in the NEW_PAT and return 1.
8183 If current pattern of the INSN already provides TS speculation,
8184 return 0. */
8185 static int
8186 ia64_speculate_insn (rtx insn, ds_t ts, rtx *new_pat)
8188 int mode_no;
8189 int res;
8191 gcc_assert (!(ts & ~SPECULATIVE));
8193 if (ia64_spec_check_p (insn))
8194 return -1;
8196 if ((ts & BE_IN_SPEC)
8197 && !insn_can_be_in_speculative_p (insn, ts))
8198 return -1;
8200 mode_no = get_mode_no_for_insn (insn);
8202 if (mode_no != SPEC_MODE_INVALID)
8204 if (ia64_get_insn_spec_ds (insn) == ds_get_speculation_types (ts))
8205 res = 0;
8206 else
8208 res = 1;
8209 *new_pat = ia64_gen_spec_load (insn, ts, mode_no);
8212 else
8213 res = -1;
8215 return res;
8218 /* Return a function that will generate a check for speculation TS with mode
8219 MODE_NO.
8220 If simple check is needed, pass true for SIMPLE_CHECK_P.
8221 If clearing check is needed, pass true for CLEARING_CHECK_P. */
8222 static gen_func_t
8223 get_spec_check_gen_function (ds_t ts, int mode_no,
8224 bool simple_check_p, bool clearing_check_p)
8226 static gen_func_t gen_ld_c_clr[] = {
8227 gen_movbi_clr,
8228 gen_movqi_clr,
8229 gen_movhi_clr,
8230 gen_movsi_clr,
8231 gen_movdi_clr,
8232 gen_movsf_clr,
8233 gen_movdf_clr,
8234 gen_movxf_clr,
8235 gen_movti_clr,
8236 gen_zero_extendqidi2_clr,
8237 gen_zero_extendhidi2_clr,
8238 gen_zero_extendsidi2_clr,
8240 static gen_func_t gen_ld_c_nc[] = {
8241 gen_movbi_nc,
8242 gen_movqi_nc,
8243 gen_movhi_nc,
8244 gen_movsi_nc,
8245 gen_movdi_nc,
8246 gen_movsf_nc,
8247 gen_movdf_nc,
8248 gen_movxf_nc,
8249 gen_movti_nc,
8250 gen_zero_extendqidi2_nc,
8251 gen_zero_extendhidi2_nc,
8252 gen_zero_extendsidi2_nc,
8254 static gen_func_t gen_chk_a_clr[] = {
8255 gen_advanced_load_check_clr_bi,
8256 gen_advanced_load_check_clr_qi,
8257 gen_advanced_load_check_clr_hi,
8258 gen_advanced_load_check_clr_si,
8259 gen_advanced_load_check_clr_di,
8260 gen_advanced_load_check_clr_sf,
8261 gen_advanced_load_check_clr_df,
8262 gen_advanced_load_check_clr_xf,
8263 gen_advanced_load_check_clr_ti,
8264 gen_advanced_load_check_clr_di,
8265 gen_advanced_load_check_clr_di,
8266 gen_advanced_load_check_clr_di,
8268 static gen_func_t gen_chk_a_nc[] = {
8269 gen_advanced_load_check_nc_bi,
8270 gen_advanced_load_check_nc_qi,
8271 gen_advanced_load_check_nc_hi,
8272 gen_advanced_load_check_nc_si,
8273 gen_advanced_load_check_nc_di,
8274 gen_advanced_load_check_nc_sf,
8275 gen_advanced_load_check_nc_df,
8276 gen_advanced_load_check_nc_xf,
8277 gen_advanced_load_check_nc_ti,
8278 gen_advanced_load_check_nc_di,
8279 gen_advanced_load_check_nc_di,
8280 gen_advanced_load_check_nc_di,
8282 static gen_func_t gen_chk_s[] = {
8283 gen_speculation_check_bi,
8284 gen_speculation_check_qi,
8285 gen_speculation_check_hi,
8286 gen_speculation_check_si,
8287 gen_speculation_check_di,
8288 gen_speculation_check_sf,
8289 gen_speculation_check_df,
8290 gen_speculation_check_xf,
8291 gen_speculation_check_ti,
8292 gen_speculation_check_di,
8293 gen_speculation_check_di,
8294 gen_speculation_check_di,
8297 gen_func_t *gen_check;
8299 if (ts & BEGIN_DATA)
8301 /* We don't need recovery because even if this is ld.sa
8302 ALAT entry will be allocated only if NAT bit is set to zero.
8303 So it is enough to use ld.c here. */
8305 if (simple_check_p)
8307 gcc_assert (mflag_sched_spec_ldc);
8309 if (clearing_check_p)
8310 gen_check = gen_ld_c_clr;
8311 else
8312 gen_check = gen_ld_c_nc;
8314 else
8316 if (clearing_check_p)
8317 gen_check = gen_chk_a_clr;
8318 else
8319 gen_check = gen_chk_a_nc;
8322 else if (ts & BEGIN_CONTROL)
8324 if (simple_check_p)
8325 /* We might want to use ld.sa -> ld.c instead of
8326 ld.s -> chk.s. */
8328 gcc_assert (!ia64_needs_block_p (ts));
8330 if (clearing_check_p)
8331 gen_check = gen_ld_c_clr;
8332 else
8333 gen_check = gen_ld_c_nc;
8335 else
8337 gen_check = gen_chk_s;
8340 else
8341 gcc_unreachable ();
8343 gcc_assert (mode_no >= 0);
8344 return gen_check[mode_no];
8347 /* Return nonzero, if INSN needs branchy recovery check. */
8348 static bool
8349 ia64_needs_block_p (ds_t ts)
8351 if (ts & BEGIN_DATA)
8352 return !mflag_sched_spec_ldc;
8354 gcc_assert ((ts & BEGIN_CONTROL) != 0);
8356 return !(mflag_sched_spec_control_ldc && mflag_sched_spec_ldc);
8359 /* Generate (or regenerate) a recovery check for INSN. */
8360 static rtx
8361 ia64_gen_spec_check (rtx insn, rtx label, ds_t ds)
8363 rtx op1, pat, check_pat;
8364 gen_func_t gen_check;
8365 int mode_no;
8367 mode_no = get_mode_no_for_insn (insn);
8368 gcc_assert (mode_no >= 0);
8370 if (label)
8371 op1 = label;
8372 else
8374 gcc_assert (!ia64_needs_block_p (ds));
8375 op1 = copy_rtx (recog_data.operand[1]);
8378 gen_check = get_spec_check_gen_function (ds, mode_no, label == NULL_RTX,
8379 true);
8381 check_pat = gen_check (copy_rtx (recog_data.operand[0]), op1);
8383 pat = PATTERN (insn);
8384 if (GET_CODE (pat) == COND_EXEC)
8385 check_pat = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (COND_EXEC_TEST (pat)),
8386 check_pat);
8388 return check_pat;
8391 /* Return nonzero, if X is branchy recovery check. */
8392 static int
8393 ia64_spec_check_p (rtx x)
8395 x = PATTERN (x);
8396 if (GET_CODE (x) == COND_EXEC)
8397 x = COND_EXEC_CODE (x);
8398 if (GET_CODE (x) == SET)
8399 return ia64_spec_check_src_p (SET_SRC (x));
8400 return 0;
8403 /* Return nonzero, if SRC belongs to recovery check. */
8404 static int
8405 ia64_spec_check_src_p (rtx src)
8407 if (GET_CODE (src) == IF_THEN_ELSE)
8409 rtx t;
8411 t = XEXP (src, 0);
8412 if (GET_CODE (t) == NE)
8414 t = XEXP (t, 0);
8416 if (GET_CODE (t) == UNSPEC)
8418 int code;
8420 code = XINT (t, 1);
8422 if (code == UNSPEC_LDCCLR
8423 || code == UNSPEC_LDCNC
8424 || code == UNSPEC_CHKACLR
8425 || code == UNSPEC_CHKANC
8426 || code == UNSPEC_CHKS)
8428 gcc_assert (code != 0);
8429 return code;
8434 return 0;
8438 /* The following page contains abstract data `bundle states' which are
8439 used for bundling insns (inserting nops and template generation). */
8441 /* The following describes state of insn bundling. */
8443 struct bundle_state
8445 /* Unique bundle state number to identify them in the debugging
8446 output */
8447 int unique_num;
8448 rtx insn; /* corresponding insn, NULL for the 1st and the last state */
8449 /* number nops before and after the insn */
8450 short before_nops_num, after_nops_num;
8451 int insn_num; /* insn number (0 - for initial state, 1 - for the 1st
8452 insn */
8453 int cost; /* cost of the state in cycles */
8454 int accumulated_insns_num; /* number of all previous insns including
8455 nops. L is considered as 2 insns */
8456 int branch_deviation; /* deviation of previous branches from 3rd slots */
8457 int middle_bundle_stops; /* number of stop bits in the middle of bundles */
8458 struct bundle_state *next; /* next state with the same insn_num */
8459 struct bundle_state *originator; /* originator (previous insn state) */
8460 /* All bundle states are in the following chain. */
8461 struct bundle_state *allocated_states_chain;
8462 /* The DFA State after issuing the insn and the nops. */
8463 state_t dfa_state;
8466 /* The following is map insn number to the corresponding bundle state. */
8468 static struct bundle_state **index_to_bundle_states;
8470 /* The unique number of next bundle state. */
8472 static int bundle_states_num;
8474 /* All allocated bundle states are in the following chain. */
8476 static struct bundle_state *allocated_bundle_states_chain;
8478 /* All allocated but not used bundle states are in the following
8479 chain. */
8481 static struct bundle_state *free_bundle_state_chain;
8484 /* The following function returns a free bundle state. */
8486 static struct bundle_state *
8487 get_free_bundle_state (void)
8489 struct bundle_state *result;
8491 if (free_bundle_state_chain != NULL)
8493 result = free_bundle_state_chain;
8494 free_bundle_state_chain = result->next;
8496 else
8498 result = XNEW (struct bundle_state);
8499 result->dfa_state = xmalloc (dfa_state_size);
8500 result->allocated_states_chain = allocated_bundle_states_chain;
8501 allocated_bundle_states_chain = result;
8503 result->unique_num = bundle_states_num++;
8504 return result;
8508 /* The following function frees given bundle state. */
8510 static void
8511 free_bundle_state (struct bundle_state *state)
8513 state->next = free_bundle_state_chain;
8514 free_bundle_state_chain = state;
8517 /* Start work with abstract data `bundle states'. */
8519 static void
8520 initiate_bundle_states (void)
8522 bundle_states_num = 0;
8523 free_bundle_state_chain = NULL;
8524 allocated_bundle_states_chain = NULL;
8527 /* Finish work with abstract data `bundle states'. */
8529 static void
8530 finish_bundle_states (void)
8532 struct bundle_state *curr_state, *next_state;
8534 for (curr_state = allocated_bundle_states_chain;
8535 curr_state != NULL;
8536 curr_state = next_state)
8538 next_state = curr_state->allocated_states_chain;
8539 free (curr_state->dfa_state);
8540 free (curr_state);
8544 /* Hashtable helpers. */
8546 struct bundle_state_hasher : typed_noop_remove <bundle_state>
8548 typedef bundle_state value_type;
8549 typedef bundle_state compare_type;
8550 static inline hashval_t hash (const value_type *);
8551 static inline bool equal (const value_type *, const compare_type *);
8554 /* The function returns hash of BUNDLE_STATE. */
8556 inline hashval_t
8557 bundle_state_hasher::hash (const value_type *state)
8559 unsigned result, i;
8561 for (result = i = 0; i < dfa_state_size; i++)
8562 result += (((unsigned char *) state->dfa_state) [i]
8563 << ((i % CHAR_BIT) * 3 + CHAR_BIT));
8564 return result + state->insn_num;
8567 /* The function returns nonzero if the bundle state keys are equal. */
8569 inline bool
8570 bundle_state_hasher::equal (const value_type *state1,
8571 const compare_type *state2)
8573 return (state1->insn_num == state2->insn_num
8574 && memcmp (state1->dfa_state, state2->dfa_state,
8575 dfa_state_size) == 0);
8578 /* Hash table of the bundle states. The key is dfa_state and insn_num
8579 of the bundle states. */
8581 static hash_table <bundle_state_hasher> bundle_state_table;
8583 /* The function inserts the BUNDLE_STATE into the hash table. The
8584 function returns nonzero if the bundle has been inserted into the
8585 table. The table contains the best bundle state with given key. */
8587 static int
8588 insert_bundle_state (struct bundle_state *bundle_state)
8590 struct bundle_state **entry_ptr;
8592 entry_ptr = bundle_state_table.find_slot (bundle_state, INSERT);
8593 if (*entry_ptr == NULL)
8595 bundle_state->next = index_to_bundle_states [bundle_state->insn_num];
8596 index_to_bundle_states [bundle_state->insn_num] = bundle_state;
8597 *entry_ptr = bundle_state;
8598 return TRUE;
8600 else if (bundle_state->cost < (*entry_ptr)->cost
8601 || (bundle_state->cost == (*entry_ptr)->cost
8602 && ((*entry_ptr)->accumulated_insns_num
8603 > bundle_state->accumulated_insns_num
8604 || ((*entry_ptr)->accumulated_insns_num
8605 == bundle_state->accumulated_insns_num
8606 && ((*entry_ptr)->branch_deviation
8607 > bundle_state->branch_deviation
8608 || ((*entry_ptr)->branch_deviation
8609 == bundle_state->branch_deviation
8610 && (*entry_ptr)->middle_bundle_stops
8611 > bundle_state->middle_bundle_stops))))))
8614 struct bundle_state temp;
8616 temp = **entry_ptr;
8617 **entry_ptr = *bundle_state;
8618 (*entry_ptr)->next = temp.next;
8619 *bundle_state = temp;
8621 return FALSE;
8624 /* Start work with the hash table. */
8626 static void
8627 initiate_bundle_state_table (void)
8629 bundle_state_table.create (50);
8632 /* Finish work with the hash table. */
8634 static void
8635 finish_bundle_state_table (void)
8637 bundle_state_table.dispose ();
8642 /* The following variable is a insn `nop' used to check bundle states
8643 with different number of inserted nops. */
8645 static rtx ia64_nop;
8647 /* The following function tries to issue NOPS_NUM nops for the current
8648 state without advancing processor cycle. If it failed, the
8649 function returns FALSE and frees the current state. */
8651 static int
8652 try_issue_nops (struct bundle_state *curr_state, int nops_num)
8654 int i;
8656 for (i = 0; i < nops_num; i++)
8657 if (state_transition (curr_state->dfa_state, ia64_nop) >= 0)
8659 free_bundle_state (curr_state);
8660 return FALSE;
8662 return TRUE;
8665 /* The following function tries to issue INSN for the current
8666 state without advancing processor cycle. If it failed, the
8667 function returns FALSE and frees the current state. */
8669 static int
8670 try_issue_insn (struct bundle_state *curr_state, rtx insn)
8672 if (insn && state_transition (curr_state->dfa_state, insn) >= 0)
8674 free_bundle_state (curr_state);
8675 return FALSE;
8677 return TRUE;
8680 /* The following function tries to issue BEFORE_NOPS_NUM nops and INSN
8681 starting with ORIGINATOR without advancing processor cycle. If
8682 TRY_BUNDLE_END_P is TRUE, the function also/only (if
8683 ONLY_BUNDLE_END_P is TRUE) tries to issue nops to fill all bundle.
8684 If it was successful, the function creates new bundle state and
8685 insert into the hash table and into `index_to_bundle_states'. */
8687 static void
8688 issue_nops_and_insn (struct bundle_state *originator, int before_nops_num,
8689 rtx insn, int try_bundle_end_p, int only_bundle_end_p)
8691 struct bundle_state *curr_state;
8693 curr_state = get_free_bundle_state ();
8694 memcpy (curr_state->dfa_state, originator->dfa_state, dfa_state_size);
8695 curr_state->insn = insn;
8696 curr_state->insn_num = originator->insn_num + 1;
8697 curr_state->cost = originator->cost;
8698 curr_state->originator = originator;
8699 curr_state->before_nops_num = before_nops_num;
8700 curr_state->after_nops_num = 0;
8701 curr_state->accumulated_insns_num
8702 = originator->accumulated_insns_num + before_nops_num;
8703 curr_state->branch_deviation = originator->branch_deviation;
8704 curr_state->middle_bundle_stops = originator->middle_bundle_stops;
8705 gcc_assert (insn);
8706 if (INSN_CODE (insn) == CODE_FOR_insn_group_barrier)
8708 gcc_assert (GET_MODE (insn) != TImode);
8709 if (!try_issue_nops (curr_state, before_nops_num))
8710 return;
8711 if (!try_issue_insn (curr_state, insn))
8712 return;
8713 memcpy (temp_dfa_state, curr_state->dfa_state, dfa_state_size);
8714 if (curr_state->accumulated_insns_num % 3 != 0)
8715 curr_state->middle_bundle_stops++;
8716 if (state_transition (temp_dfa_state, dfa_pre_cycle_insn) >= 0
8717 && curr_state->accumulated_insns_num % 3 != 0)
8719 free_bundle_state (curr_state);
8720 return;
8723 else if (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 curr_state->accumulated_insns_num++;
8730 gcc_assert (!unknown_for_bundling_p (insn));
8732 if (ia64_safe_type (insn) == TYPE_L)
8733 curr_state->accumulated_insns_num++;
8735 else
8737 /* If this is an insn that must be first in a group, then don't allow
8738 nops to be emitted before it. Currently, alloc is the only such
8739 supported instruction. */
8740 /* ??? The bundling automatons should handle this for us, but they do
8741 not yet have support for the first_insn attribute. */
8742 if (before_nops_num > 0 && get_attr_first_insn (insn) == FIRST_INSN_YES)
8744 free_bundle_state (curr_state);
8745 return;
8748 state_transition (curr_state->dfa_state, dfa_pre_cycle_insn);
8749 state_transition (curr_state->dfa_state, NULL);
8750 curr_state->cost++;
8751 if (!try_issue_nops (curr_state, before_nops_num))
8752 return;
8753 if (!try_issue_insn (curr_state, insn))
8754 return;
8755 curr_state->accumulated_insns_num++;
8756 if (unknown_for_bundling_p (insn))
8758 /* Finish bundle containing asm insn. */
8759 curr_state->after_nops_num
8760 = 3 - curr_state->accumulated_insns_num % 3;
8761 curr_state->accumulated_insns_num
8762 += 3 - curr_state->accumulated_insns_num % 3;
8764 else if (ia64_safe_type (insn) == TYPE_L)
8765 curr_state->accumulated_insns_num++;
8767 if (ia64_safe_type (insn) == TYPE_B)
8768 curr_state->branch_deviation
8769 += 2 - (curr_state->accumulated_insns_num - 1) % 3;
8770 if (try_bundle_end_p && curr_state->accumulated_insns_num % 3 != 0)
8772 if (!only_bundle_end_p && insert_bundle_state (curr_state))
8774 state_t dfa_state;
8775 struct bundle_state *curr_state1;
8776 struct bundle_state *allocated_states_chain;
8778 curr_state1 = get_free_bundle_state ();
8779 dfa_state = curr_state1->dfa_state;
8780 allocated_states_chain = curr_state1->allocated_states_chain;
8781 *curr_state1 = *curr_state;
8782 curr_state1->dfa_state = dfa_state;
8783 curr_state1->allocated_states_chain = allocated_states_chain;
8784 memcpy (curr_state1->dfa_state, curr_state->dfa_state,
8785 dfa_state_size);
8786 curr_state = curr_state1;
8788 if (!try_issue_nops (curr_state,
8789 3 - curr_state->accumulated_insns_num % 3))
8790 return;
8791 curr_state->after_nops_num
8792 = 3 - curr_state->accumulated_insns_num % 3;
8793 curr_state->accumulated_insns_num
8794 += 3 - curr_state->accumulated_insns_num % 3;
8796 if (!insert_bundle_state (curr_state))
8797 free_bundle_state (curr_state);
8798 return;
8801 /* The following function returns position in the two window bundle
8802 for given STATE. */
8804 static int
8805 get_max_pos (state_t state)
8807 if (cpu_unit_reservation_p (state, pos_6))
8808 return 6;
8809 else if (cpu_unit_reservation_p (state, pos_5))
8810 return 5;
8811 else if (cpu_unit_reservation_p (state, pos_4))
8812 return 4;
8813 else if (cpu_unit_reservation_p (state, pos_3))
8814 return 3;
8815 else if (cpu_unit_reservation_p (state, pos_2))
8816 return 2;
8817 else if (cpu_unit_reservation_p (state, pos_1))
8818 return 1;
8819 else
8820 return 0;
8823 /* The function returns code of a possible template for given position
8824 and state. The function should be called only with 2 values of
8825 position equal to 3 or 6. We avoid generating F NOPs by putting
8826 templates containing F insns at the end of the template search
8827 because undocumented anomaly in McKinley derived cores which can
8828 cause stalls if an F-unit insn (including a NOP) is issued within a
8829 six-cycle window after reading certain application registers (such
8830 as ar.bsp). Furthermore, power-considerations also argue against
8831 the use of F-unit instructions unless they're really needed. */
8833 static int
8834 get_template (state_t state, int pos)
8836 switch (pos)
8838 case 3:
8839 if (cpu_unit_reservation_p (state, _0mmi_))
8840 return 1;
8841 else if (cpu_unit_reservation_p (state, _0mii_))
8842 return 0;
8843 else if (cpu_unit_reservation_p (state, _0mmb_))
8844 return 7;
8845 else if (cpu_unit_reservation_p (state, _0mib_))
8846 return 6;
8847 else if (cpu_unit_reservation_p (state, _0mbb_))
8848 return 5;
8849 else if (cpu_unit_reservation_p (state, _0bbb_))
8850 return 4;
8851 else if (cpu_unit_reservation_p (state, _0mmf_))
8852 return 3;
8853 else if (cpu_unit_reservation_p (state, _0mfi_))
8854 return 2;
8855 else if (cpu_unit_reservation_p (state, _0mfb_))
8856 return 8;
8857 else if (cpu_unit_reservation_p (state, _0mlx_))
8858 return 9;
8859 else
8860 gcc_unreachable ();
8861 case 6:
8862 if (cpu_unit_reservation_p (state, _1mmi_))
8863 return 1;
8864 else if (cpu_unit_reservation_p (state, _1mii_))
8865 return 0;
8866 else if (cpu_unit_reservation_p (state, _1mmb_))
8867 return 7;
8868 else if (cpu_unit_reservation_p (state, _1mib_))
8869 return 6;
8870 else if (cpu_unit_reservation_p (state, _1mbb_))
8871 return 5;
8872 else if (cpu_unit_reservation_p (state, _1bbb_))
8873 return 4;
8874 else if (_1mmf_ >= 0 && cpu_unit_reservation_p (state, _1mmf_))
8875 return 3;
8876 else if (cpu_unit_reservation_p (state, _1mfi_))
8877 return 2;
8878 else if (cpu_unit_reservation_p (state, _1mfb_))
8879 return 8;
8880 else if (cpu_unit_reservation_p (state, _1mlx_))
8881 return 9;
8882 else
8883 gcc_unreachable ();
8884 default:
8885 gcc_unreachable ();
8889 /* True when INSN is important for bundling. */
8891 static bool
8892 important_for_bundling_p (rtx insn)
8894 return (INSN_P (insn)
8895 && ia64_safe_itanium_class (insn) != ITANIUM_CLASS_IGNORE
8896 && GET_CODE (PATTERN (insn)) != USE
8897 && GET_CODE (PATTERN (insn)) != CLOBBER);
8900 /* The following function returns an insn important for insn bundling
8901 followed by INSN and before TAIL. */
8903 static rtx
8904 get_next_important_insn (rtx insn, rtx tail)
8906 for (; insn && insn != tail; insn = NEXT_INSN (insn))
8907 if (important_for_bundling_p (insn))
8908 return insn;
8909 return NULL_RTX;
8912 /* True when INSN is unknown, but important, for bundling. */
8914 static bool
8915 unknown_for_bundling_p (rtx insn)
8917 return (INSN_P (insn)
8918 && ia64_safe_itanium_class (insn) == ITANIUM_CLASS_UNKNOWN
8919 && GET_CODE (PATTERN (insn)) != USE
8920 && GET_CODE (PATTERN (insn)) != CLOBBER);
8923 /* Add a bundle selector TEMPLATE0 before INSN. */
8925 static void
8926 ia64_add_bundle_selector_before (int template0, rtx insn)
8928 rtx b = gen_bundle_selector (GEN_INT (template0));
8930 ia64_emit_insn_before (b, insn);
8931 #if NR_BUNDLES == 10
8932 if ((template0 == 4 || template0 == 5)
8933 && ia64_except_unwind_info (&global_options) == UI_TARGET)
8935 int i;
8936 rtx note = NULL_RTX;
8938 /* In .mbb and .bbb bundles, check if CALL_INSN isn't in the
8939 first or second slot. If it is and has REG_EH_NOTE set, copy it
8940 to following nops, as br.call sets rp to the address of following
8941 bundle and therefore an EH region end must be on a bundle
8942 boundary. */
8943 insn = PREV_INSN (insn);
8944 for (i = 0; i < 3; i++)
8947 insn = next_active_insn (insn);
8948 while (NONJUMP_INSN_P (insn)
8949 && get_attr_empty (insn) == EMPTY_YES);
8950 if (CALL_P (insn))
8951 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
8952 else if (note)
8954 int code;
8956 gcc_assert ((code = recog_memoized (insn)) == CODE_FOR_nop
8957 || code == CODE_FOR_nop_b);
8958 if (find_reg_note (insn, REG_EH_REGION, NULL_RTX))
8959 note = NULL_RTX;
8960 else
8961 add_reg_note (insn, REG_EH_REGION, XEXP (note, 0));
8965 #endif
8968 /* The following function does insn bundling. Bundling means
8969 inserting templates and nop insns to fit insn groups into permitted
8970 templates. Instruction scheduling uses NDFA (non-deterministic
8971 finite automata) encoding informations about the templates and the
8972 inserted nops. Nondeterminism of the automata permits follows
8973 all possible insn sequences very fast.
8975 Unfortunately it is not possible to get information about inserting
8976 nop insns and used templates from the automata states. The
8977 automata only says that we can issue an insn possibly inserting
8978 some nops before it and using some template. Therefore insn
8979 bundling in this function is implemented by using DFA
8980 (deterministic finite automata). We follow all possible insn
8981 sequences by inserting 0-2 nops (that is what the NDFA describe for
8982 insn scheduling) before/after each insn being bundled. We know the
8983 start of simulated processor cycle from insn scheduling (insn
8984 starting a new cycle has TImode).
8986 Simple implementation of insn bundling would create enormous
8987 number of possible insn sequences satisfying information about new
8988 cycle ticks taken from the insn scheduling. To make the algorithm
8989 practical we use dynamic programming. Each decision (about
8990 inserting nops and implicitly about previous decisions) is described
8991 by structure bundle_state (see above). If we generate the same
8992 bundle state (key is automaton state after issuing the insns and
8993 nops for it), we reuse already generated one. As consequence we
8994 reject some decisions which cannot improve the solution and
8995 reduce memory for the algorithm.
8997 When we reach the end of EBB (extended basic block), we choose the
8998 best sequence and then, moving back in EBB, insert templates for
8999 the best alternative. The templates are taken from querying
9000 automaton state for each insn in chosen bundle states.
9002 So the algorithm makes two (forward and backward) passes through
9003 EBB. */
9005 static void
9006 bundling (FILE *dump, int verbose, rtx prev_head_insn, rtx tail)
9008 struct bundle_state *curr_state, *next_state, *best_state;
9009 rtx insn, next_insn;
9010 int insn_num;
9011 int i, bundle_end_p, only_bundle_end_p, asm_p;
9012 int pos = 0, max_pos, template0, template1;
9013 rtx b;
9014 rtx nop;
9015 enum attr_type type;
9017 insn_num = 0;
9018 /* Count insns in the EBB. */
9019 for (insn = NEXT_INSN (prev_head_insn);
9020 insn && insn != tail;
9021 insn = NEXT_INSN (insn))
9022 if (INSN_P (insn))
9023 insn_num++;
9024 if (insn_num == 0)
9025 return;
9026 bundling_p = 1;
9027 dfa_clean_insn_cache ();
9028 initiate_bundle_state_table ();
9029 index_to_bundle_states = XNEWVEC (struct bundle_state *, insn_num + 2);
9030 /* First (forward) pass -- generation of bundle states. */
9031 curr_state = get_free_bundle_state ();
9032 curr_state->insn = NULL;
9033 curr_state->before_nops_num = 0;
9034 curr_state->after_nops_num = 0;
9035 curr_state->insn_num = 0;
9036 curr_state->cost = 0;
9037 curr_state->accumulated_insns_num = 0;
9038 curr_state->branch_deviation = 0;
9039 curr_state->middle_bundle_stops = 0;
9040 curr_state->next = NULL;
9041 curr_state->originator = NULL;
9042 state_reset (curr_state->dfa_state);
9043 index_to_bundle_states [0] = curr_state;
9044 insn_num = 0;
9045 /* Shift cycle mark if it is put on insn which could be ignored. */
9046 for (insn = NEXT_INSN (prev_head_insn);
9047 insn != tail;
9048 insn = NEXT_INSN (insn))
9049 if (INSN_P (insn)
9050 && !important_for_bundling_p (insn)
9051 && GET_MODE (insn) == TImode)
9053 PUT_MODE (insn, VOIDmode);
9054 for (next_insn = NEXT_INSN (insn);
9055 next_insn != tail;
9056 next_insn = NEXT_INSN (next_insn))
9057 if (important_for_bundling_p (next_insn)
9058 && INSN_CODE (next_insn) != CODE_FOR_insn_group_barrier)
9060 PUT_MODE (next_insn, TImode);
9061 break;
9064 /* Forward pass: generation of bundle states. */
9065 for (insn = get_next_important_insn (NEXT_INSN (prev_head_insn), tail);
9066 insn != NULL_RTX;
9067 insn = next_insn)
9069 gcc_assert (important_for_bundling_p (insn));
9070 type = ia64_safe_type (insn);
9071 next_insn = get_next_important_insn (NEXT_INSN (insn), tail);
9072 insn_num++;
9073 index_to_bundle_states [insn_num] = NULL;
9074 for (curr_state = index_to_bundle_states [insn_num - 1];
9075 curr_state != NULL;
9076 curr_state = next_state)
9078 pos = curr_state->accumulated_insns_num % 3;
9079 next_state = curr_state->next;
9080 /* We must fill up the current bundle in order to start a
9081 subsequent asm insn in a new bundle. Asm insn is always
9082 placed in a separate bundle. */
9083 only_bundle_end_p
9084 = (next_insn != NULL_RTX
9085 && INSN_CODE (insn) == CODE_FOR_insn_group_barrier
9086 && unknown_for_bundling_p (next_insn));
9087 /* We may fill up the current bundle if it is the cycle end
9088 without a group barrier. */
9089 bundle_end_p
9090 = (only_bundle_end_p || next_insn == NULL_RTX
9091 || (GET_MODE (next_insn) == TImode
9092 && INSN_CODE (insn) != CODE_FOR_insn_group_barrier));
9093 if (type == TYPE_F || type == TYPE_B || type == TYPE_L
9094 || type == TYPE_S)
9095 issue_nops_and_insn (curr_state, 2, insn, bundle_end_p,
9096 only_bundle_end_p);
9097 issue_nops_and_insn (curr_state, 1, insn, bundle_end_p,
9098 only_bundle_end_p);
9099 issue_nops_and_insn (curr_state, 0, insn, bundle_end_p,
9100 only_bundle_end_p);
9102 gcc_assert (index_to_bundle_states [insn_num]);
9103 for (curr_state = index_to_bundle_states [insn_num];
9104 curr_state != NULL;
9105 curr_state = curr_state->next)
9106 if (verbose >= 2 && dump)
9108 /* This structure is taken from generated code of the
9109 pipeline hazard recognizer (see file insn-attrtab.c).
9110 Please don't forget to change the structure if a new
9111 automaton is added to .md file. */
9112 struct DFA_chip
9114 unsigned short one_automaton_state;
9115 unsigned short oneb_automaton_state;
9116 unsigned short two_automaton_state;
9117 unsigned short twob_automaton_state;
9120 fprintf
9121 (dump,
9122 "// Bundle state %d (orig %d, cost %d, nops %d/%d, insns %d, branch %d, mid.stops %d state %d) for %d\n",
9123 curr_state->unique_num,
9124 (curr_state->originator == NULL
9125 ? -1 : curr_state->originator->unique_num),
9126 curr_state->cost,
9127 curr_state->before_nops_num, curr_state->after_nops_num,
9128 curr_state->accumulated_insns_num, curr_state->branch_deviation,
9129 curr_state->middle_bundle_stops,
9130 ((struct DFA_chip *) curr_state->dfa_state)->twob_automaton_state,
9131 INSN_UID (insn));
9135 /* We should find a solution because the 2nd insn scheduling has
9136 found one. */
9137 gcc_assert (index_to_bundle_states [insn_num]);
9138 /* Find a state corresponding to the best insn sequence. */
9139 best_state = NULL;
9140 for (curr_state = index_to_bundle_states [insn_num];
9141 curr_state != NULL;
9142 curr_state = curr_state->next)
9143 /* We are just looking at the states with fully filled up last
9144 bundle. The first we prefer insn sequences with minimal cost
9145 then with minimal inserted nops and finally with branch insns
9146 placed in the 3rd slots. */
9147 if (curr_state->accumulated_insns_num % 3 == 0
9148 && (best_state == NULL || best_state->cost > curr_state->cost
9149 || (best_state->cost == curr_state->cost
9150 && (curr_state->accumulated_insns_num
9151 < best_state->accumulated_insns_num
9152 || (curr_state->accumulated_insns_num
9153 == best_state->accumulated_insns_num
9154 && (curr_state->branch_deviation
9155 < best_state->branch_deviation
9156 || (curr_state->branch_deviation
9157 == best_state->branch_deviation
9158 && curr_state->middle_bundle_stops
9159 < best_state->middle_bundle_stops)))))))
9160 best_state = curr_state;
9161 /* Second (backward) pass: adding nops and templates. */
9162 gcc_assert (best_state);
9163 insn_num = best_state->before_nops_num;
9164 template0 = template1 = -1;
9165 for (curr_state = best_state;
9166 curr_state->originator != NULL;
9167 curr_state = curr_state->originator)
9169 insn = curr_state->insn;
9170 asm_p = unknown_for_bundling_p (insn);
9171 insn_num++;
9172 if (verbose >= 2 && dump)
9174 struct DFA_chip
9176 unsigned short one_automaton_state;
9177 unsigned short oneb_automaton_state;
9178 unsigned short two_automaton_state;
9179 unsigned short twob_automaton_state;
9182 fprintf
9183 (dump,
9184 "// Best %d (orig %d, cost %d, nops %d/%d, insns %d, branch %d, mid.stops %d, state %d) for %d\n",
9185 curr_state->unique_num,
9186 (curr_state->originator == NULL
9187 ? -1 : curr_state->originator->unique_num),
9188 curr_state->cost,
9189 curr_state->before_nops_num, curr_state->after_nops_num,
9190 curr_state->accumulated_insns_num, curr_state->branch_deviation,
9191 curr_state->middle_bundle_stops,
9192 ((struct DFA_chip *) curr_state->dfa_state)->twob_automaton_state,
9193 INSN_UID (insn));
9195 /* Find the position in the current bundle window. The window can
9196 contain at most two bundles. Two bundle window means that
9197 the processor will make two bundle rotation. */
9198 max_pos = get_max_pos (curr_state->dfa_state);
9199 if (max_pos == 6
9200 /* The following (negative template number) means that the
9201 processor did one bundle rotation. */
9202 || (max_pos == 3 && template0 < 0))
9204 /* We are at the end of the window -- find template(s) for
9205 its bundle(s). */
9206 pos = max_pos;
9207 if (max_pos == 3)
9208 template0 = get_template (curr_state->dfa_state, 3);
9209 else
9211 template1 = get_template (curr_state->dfa_state, 3);
9212 template0 = get_template (curr_state->dfa_state, 6);
9215 if (max_pos > 3 && template1 < 0)
9216 /* It may happen when we have the stop inside a bundle. */
9218 gcc_assert (pos <= 3);
9219 template1 = get_template (curr_state->dfa_state, 3);
9220 pos += 3;
9222 if (!asm_p)
9223 /* Emit nops after the current insn. */
9224 for (i = 0; i < curr_state->after_nops_num; i++)
9226 nop = gen_nop ();
9227 emit_insn_after (nop, insn);
9228 pos--;
9229 gcc_assert (pos >= 0);
9230 if (pos % 3 == 0)
9232 /* We are at the start of a bundle: emit the template
9233 (it should be defined). */
9234 gcc_assert (template0 >= 0);
9235 ia64_add_bundle_selector_before (template0, nop);
9236 /* If we have two bundle window, we make one bundle
9237 rotation. Otherwise template0 will be undefined
9238 (negative value). */
9239 template0 = template1;
9240 template1 = -1;
9243 /* Move the position backward in the window. Group barrier has
9244 no slot. Asm insn takes all bundle. */
9245 if (INSN_CODE (insn) != CODE_FOR_insn_group_barrier
9246 && !unknown_for_bundling_p (insn))
9247 pos--;
9248 /* Long insn takes 2 slots. */
9249 if (ia64_safe_type (insn) == TYPE_L)
9250 pos--;
9251 gcc_assert (pos >= 0);
9252 if (pos % 3 == 0
9253 && INSN_CODE (insn) != CODE_FOR_insn_group_barrier
9254 && !unknown_for_bundling_p (insn))
9256 /* The current insn is at the bundle start: emit the
9257 template. */
9258 gcc_assert (template0 >= 0);
9259 ia64_add_bundle_selector_before (template0, insn);
9260 b = PREV_INSN (insn);
9261 insn = b;
9262 /* See comment above in analogous place for emitting nops
9263 after the insn. */
9264 template0 = template1;
9265 template1 = -1;
9267 /* Emit nops after the current insn. */
9268 for (i = 0; i < curr_state->before_nops_num; i++)
9270 nop = gen_nop ();
9271 ia64_emit_insn_before (nop, insn);
9272 nop = PREV_INSN (insn);
9273 insn = nop;
9274 pos--;
9275 gcc_assert (pos >= 0);
9276 if (pos % 3 == 0)
9278 /* See comment above in analogous place for emitting nops
9279 after the insn. */
9280 gcc_assert (template0 >= 0);
9281 ia64_add_bundle_selector_before (template0, insn);
9282 b = PREV_INSN (insn);
9283 insn = b;
9284 template0 = template1;
9285 template1 = -1;
9290 #ifdef ENABLE_CHECKING
9292 /* Assert right calculation of middle_bundle_stops. */
9293 int num = best_state->middle_bundle_stops;
9294 bool start_bundle = true, end_bundle = false;
9296 for (insn = NEXT_INSN (prev_head_insn);
9297 insn && insn != tail;
9298 insn = NEXT_INSN (insn))
9300 if (!INSN_P (insn))
9301 continue;
9302 if (recog_memoized (insn) == CODE_FOR_bundle_selector)
9303 start_bundle = true;
9304 else
9306 rtx next_insn;
9308 for (next_insn = NEXT_INSN (insn);
9309 next_insn && next_insn != tail;
9310 next_insn = NEXT_INSN (next_insn))
9311 if (INSN_P (next_insn)
9312 && (ia64_safe_itanium_class (next_insn)
9313 != ITANIUM_CLASS_IGNORE
9314 || recog_memoized (next_insn)
9315 == CODE_FOR_bundle_selector)
9316 && GET_CODE (PATTERN (next_insn)) != USE
9317 && GET_CODE (PATTERN (next_insn)) != CLOBBER)
9318 break;
9320 end_bundle = next_insn == NULL_RTX
9321 || next_insn == tail
9322 || (INSN_P (next_insn)
9323 && recog_memoized (next_insn)
9324 == CODE_FOR_bundle_selector);
9325 if (recog_memoized (insn) == CODE_FOR_insn_group_barrier
9326 && !start_bundle && !end_bundle
9327 && next_insn
9328 && !unknown_for_bundling_p (next_insn))
9329 num--;
9331 start_bundle = false;
9335 gcc_assert (num == 0);
9337 #endif
9339 free (index_to_bundle_states);
9340 finish_bundle_state_table ();
9341 bundling_p = 0;
9342 dfa_clean_insn_cache ();
9345 /* The following function is called at the end of scheduling BB or
9346 EBB. After reload, it inserts stop bits and does insn bundling. */
9348 static void
9349 ia64_sched_finish (FILE *dump, int sched_verbose)
9351 if (sched_verbose)
9352 fprintf (dump, "// Finishing schedule.\n");
9353 if (!reload_completed)
9354 return;
9355 if (reload_completed)
9357 final_emit_insn_group_barriers (dump);
9358 bundling (dump, sched_verbose, current_sched_info->prev_head,
9359 current_sched_info->next_tail);
9360 if (sched_verbose && dump)
9361 fprintf (dump, "// finishing %d-%d\n",
9362 INSN_UID (NEXT_INSN (current_sched_info->prev_head)),
9363 INSN_UID (PREV_INSN (current_sched_info->next_tail)));
9365 return;
9369 /* The following function inserts stop bits in scheduled BB or EBB. */
9371 static void
9372 final_emit_insn_group_barriers (FILE *dump ATTRIBUTE_UNUSED)
9374 rtx insn;
9375 int need_barrier_p = 0;
9376 int seen_good_insn = 0;
9378 init_insn_group_barriers ();
9380 for (insn = NEXT_INSN (current_sched_info->prev_head);
9381 insn != current_sched_info->next_tail;
9382 insn = NEXT_INSN (insn))
9384 if (BARRIER_P (insn))
9386 rtx last = prev_active_insn (insn);
9388 if (! last)
9389 continue;
9390 if (JUMP_TABLE_DATA_P (last))
9391 last = prev_active_insn (last);
9392 if (recog_memoized (last) != CODE_FOR_insn_group_barrier)
9393 emit_insn_after (gen_insn_group_barrier (GEN_INT (3)), last);
9395 init_insn_group_barriers ();
9396 seen_good_insn = 0;
9397 need_barrier_p = 0;
9399 else if (NONDEBUG_INSN_P (insn))
9401 if (recog_memoized (insn) == CODE_FOR_insn_group_barrier)
9403 init_insn_group_barriers ();
9404 seen_good_insn = 0;
9405 need_barrier_p = 0;
9407 else if (need_barrier_p || group_barrier_needed (insn)
9408 || (mflag_sched_stop_bits_after_every_cycle
9409 && GET_MODE (insn) == TImode
9410 && seen_good_insn))
9412 if (TARGET_EARLY_STOP_BITS)
9414 rtx last;
9416 for (last = insn;
9417 last != current_sched_info->prev_head;
9418 last = PREV_INSN (last))
9419 if (INSN_P (last) && GET_MODE (last) == TImode
9420 && stops_p [INSN_UID (last)])
9421 break;
9422 if (last == current_sched_info->prev_head)
9423 last = insn;
9424 last = prev_active_insn (last);
9425 if (last
9426 && recog_memoized (last) != CODE_FOR_insn_group_barrier)
9427 emit_insn_after (gen_insn_group_barrier (GEN_INT (3)),
9428 last);
9429 init_insn_group_barriers ();
9430 for (last = NEXT_INSN (last);
9431 last != insn;
9432 last = NEXT_INSN (last))
9433 if (INSN_P (last))
9435 group_barrier_needed (last);
9436 if (recog_memoized (last) >= 0
9437 && important_for_bundling_p (last))
9438 seen_good_insn = 1;
9441 else
9443 emit_insn_before (gen_insn_group_barrier (GEN_INT (3)),
9444 insn);
9445 init_insn_group_barriers ();
9446 seen_good_insn = 0;
9448 group_barrier_needed (insn);
9449 if (recog_memoized (insn) >= 0
9450 && important_for_bundling_p (insn))
9451 seen_good_insn = 1;
9453 else if (recog_memoized (insn) >= 0
9454 && important_for_bundling_p (insn))
9455 seen_good_insn = 1;
9456 need_barrier_p = (CALL_P (insn) || unknown_for_bundling_p (insn));
9463 /* If the following function returns TRUE, we will use the DFA
9464 insn scheduler. */
9466 static int
9467 ia64_first_cycle_multipass_dfa_lookahead (void)
9469 return (reload_completed ? 6 : 4);
9472 /* The following function initiates variable `dfa_pre_cycle_insn'. */
9474 static void
9475 ia64_init_dfa_pre_cycle_insn (void)
9477 if (temp_dfa_state == NULL)
9479 dfa_state_size = state_size ();
9480 temp_dfa_state = xmalloc (dfa_state_size);
9481 prev_cycle_state = xmalloc (dfa_state_size);
9483 dfa_pre_cycle_insn = make_insn_raw (gen_pre_cycle ());
9484 PREV_INSN (dfa_pre_cycle_insn) = NEXT_INSN (dfa_pre_cycle_insn) = NULL_RTX;
9485 recog_memoized (dfa_pre_cycle_insn);
9486 dfa_stop_insn = make_insn_raw (gen_insn_group_barrier (GEN_INT (3)));
9487 PREV_INSN (dfa_stop_insn) = NEXT_INSN (dfa_stop_insn) = NULL_RTX;
9488 recog_memoized (dfa_stop_insn);
9491 /* The following function returns the pseudo insn DFA_PRE_CYCLE_INSN
9492 used by the DFA insn scheduler. */
9494 static rtx
9495 ia64_dfa_pre_cycle_insn (void)
9497 return dfa_pre_cycle_insn;
9500 /* The following function returns TRUE if PRODUCER (of type ilog or
9501 ld) produces address for CONSUMER (of type st or stf). */
9504 ia64_st_address_bypass_p (rtx producer, rtx consumer)
9506 rtx dest, reg, mem;
9508 gcc_assert (producer && consumer);
9509 dest = ia64_single_set (producer);
9510 gcc_assert (dest);
9511 reg = SET_DEST (dest);
9512 gcc_assert (reg);
9513 if (GET_CODE (reg) == SUBREG)
9514 reg = SUBREG_REG (reg);
9515 gcc_assert (GET_CODE (reg) == REG);
9517 dest = ia64_single_set (consumer);
9518 gcc_assert (dest);
9519 mem = SET_DEST (dest);
9520 gcc_assert (mem && GET_CODE (mem) == MEM);
9521 return reg_mentioned_p (reg, mem);
9524 /* The following function returns TRUE if PRODUCER (of type ilog or
9525 ld) produces address for CONSUMER (of type ld or fld). */
9528 ia64_ld_address_bypass_p (rtx producer, rtx consumer)
9530 rtx dest, src, reg, mem;
9532 gcc_assert (producer && consumer);
9533 dest = ia64_single_set (producer);
9534 gcc_assert (dest);
9535 reg = SET_DEST (dest);
9536 gcc_assert (reg);
9537 if (GET_CODE (reg) == SUBREG)
9538 reg = SUBREG_REG (reg);
9539 gcc_assert (GET_CODE (reg) == REG);
9541 src = ia64_single_set (consumer);
9542 gcc_assert (src);
9543 mem = SET_SRC (src);
9544 gcc_assert (mem);
9546 if (GET_CODE (mem) == UNSPEC && XVECLEN (mem, 0) > 0)
9547 mem = XVECEXP (mem, 0, 0);
9548 else if (GET_CODE (mem) == IF_THEN_ELSE)
9549 /* ??? Is this bypass necessary for ld.c? */
9551 gcc_assert (XINT (XEXP (XEXP (mem, 0), 0), 1) == UNSPEC_LDCCLR);
9552 mem = XEXP (mem, 1);
9555 while (GET_CODE (mem) == SUBREG || GET_CODE (mem) == ZERO_EXTEND)
9556 mem = XEXP (mem, 0);
9558 if (GET_CODE (mem) == UNSPEC)
9560 int c = XINT (mem, 1);
9562 gcc_assert (c == UNSPEC_LDA || c == UNSPEC_LDS || c == UNSPEC_LDS_A
9563 || c == UNSPEC_LDSA);
9564 mem = XVECEXP (mem, 0, 0);
9567 /* Note that LO_SUM is used for GOT loads. */
9568 gcc_assert (GET_CODE (mem) == LO_SUM || GET_CODE (mem) == MEM);
9570 return reg_mentioned_p (reg, mem);
9573 /* The following function returns TRUE if INSN produces address for a
9574 load/store insn. We will place such insns into M slot because it
9575 decreases its latency time. */
9578 ia64_produce_address_p (rtx insn)
9580 return insn->call;
9584 /* Emit pseudo-ops for the assembler to describe predicate relations.
9585 At present this assumes that we only consider predicate pairs to
9586 be mutex, and that the assembler can deduce proper values from
9587 straight-line code. */
9589 static void
9590 emit_predicate_relation_info (void)
9592 basic_block bb;
9594 FOR_EACH_BB_REVERSE (bb)
9596 int r;
9597 rtx head = BB_HEAD (bb);
9599 /* We only need such notes at code labels. */
9600 if (! LABEL_P (head))
9601 continue;
9602 if (NOTE_INSN_BASIC_BLOCK_P (NEXT_INSN (head)))
9603 head = NEXT_INSN (head);
9605 /* Skip p0, which may be thought to be live due to (reg:DI p0)
9606 grabbing the entire block of predicate registers. */
9607 for (r = PR_REG (2); r < PR_REG (64); r += 2)
9608 if (REGNO_REG_SET_P (df_get_live_in (bb), r))
9610 rtx p = gen_rtx_REG (BImode, r);
9611 rtx n = emit_insn_after (gen_pred_rel_mutex (p), head);
9612 if (head == BB_END (bb))
9613 BB_END (bb) = n;
9614 head = n;
9618 /* Look for conditional calls that do not return, and protect predicate
9619 relations around them. Otherwise the assembler will assume the call
9620 returns, and complain about uses of call-clobbered predicates after
9621 the call. */
9622 FOR_EACH_BB_REVERSE (bb)
9624 rtx insn = BB_HEAD (bb);
9626 while (1)
9628 if (CALL_P (insn)
9629 && GET_CODE (PATTERN (insn)) == COND_EXEC
9630 && find_reg_note (insn, REG_NORETURN, NULL_RTX))
9632 rtx b = emit_insn_before (gen_safe_across_calls_all (), insn);
9633 rtx a = emit_insn_after (gen_safe_across_calls_normal (), insn);
9634 if (BB_HEAD (bb) == insn)
9635 BB_HEAD (bb) = b;
9636 if (BB_END (bb) == insn)
9637 BB_END (bb) = a;
9640 if (insn == BB_END (bb))
9641 break;
9642 insn = NEXT_INSN (insn);
9647 /* Perform machine dependent operations on the rtl chain INSNS. */
9649 static void
9650 ia64_reorg (void)
9652 /* We are freeing block_for_insn in the toplev to keep compatibility
9653 with old MDEP_REORGS that are not CFG based. Recompute it now. */
9654 compute_bb_for_insn ();
9656 /* If optimizing, we'll have split before scheduling. */
9657 if (optimize == 0)
9658 split_all_insns ();
9660 if (optimize && flag_schedule_insns_after_reload
9661 && dbg_cnt (ia64_sched2))
9663 basic_block bb;
9664 timevar_push (TV_SCHED2);
9665 ia64_final_schedule = 1;
9667 /* We can't let modulo-sched prevent us from scheduling any bbs,
9668 since we need the final schedule to produce bundle information. */
9669 FOR_EACH_BB (bb)
9670 bb->flags &= ~BB_DISABLE_SCHEDULE;
9672 initiate_bundle_states ();
9673 ia64_nop = make_insn_raw (gen_nop ());
9674 PREV_INSN (ia64_nop) = NEXT_INSN (ia64_nop) = NULL_RTX;
9675 recog_memoized (ia64_nop);
9676 clocks_length = get_max_uid () + 1;
9677 stops_p = XCNEWVEC (char, clocks_length);
9679 if (ia64_tune == PROCESSOR_ITANIUM2)
9681 pos_1 = get_cpu_unit_code ("2_1");
9682 pos_2 = get_cpu_unit_code ("2_2");
9683 pos_3 = get_cpu_unit_code ("2_3");
9684 pos_4 = get_cpu_unit_code ("2_4");
9685 pos_5 = get_cpu_unit_code ("2_5");
9686 pos_6 = get_cpu_unit_code ("2_6");
9687 _0mii_ = get_cpu_unit_code ("2b_0mii.");
9688 _0mmi_ = get_cpu_unit_code ("2b_0mmi.");
9689 _0mfi_ = get_cpu_unit_code ("2b_0mfi.");
9690 _0mmf_ = get_cpu_unit_code ("2b_0mmf.");
9691 _0bbb_ = get_cpu_unit_code ("2b_0bbb.");
9692 _0mbb_ = get_cpu_unit_code ("2b_0mbb.");
9693 _0mib_ = get_cpu_unit_code ("2b_0mib.");
9694 _0mmb_ = get_cpu_unit_code ("2b_0mmb.");
9695 _0mfb_ = get_cpu_unit_code ("2b_0mfb.");
9696 _0mlx_ = get_cpu_unit_code ("2b_0mlx.");
9697 _1mii_ = get_cpu_unit_code ("2b_1mii.");
9698 _1mmi_ = get_cpu_unit_code ("2b_1mmi.");
9699 _1mfi_ = get_cpu_unit_code ("2b_1mfi.");
9700 _1mmf_ = get_cpu_unit_code ("2b_1mmf.");
9701 _1bbb_ = get_cpu_unit_code ("2b_1bbb.");
9702 _1mbb_ = get_cpu_unit_code ("2b_1mbb.");
9703 _1mib_ = get_cpu_unit_code ("2b_1mib.");
9704 _1mmb_ = get_cpu_unit_code ("2b_1mmb.");
9705 _1mfb_ = get_cpu_unit_code ("2b_1mfb.");
9706 _1mlx_ = get_cpu_unit_code ("2b_1mlx.");
9708 else
9710 pos_1 = get_cpu_unit_code ("1_1");
9711 pos_2 = get_cpu_unit_code ("1_2");
9712 pos_3 = get_cpu_unit_code ("1_3");
9713 pos_4 = get_cpu_unit_code ("1_4");
9714 pos_5 = get_cpu_unit_code ("1_5");
9715 pos_6 = get_cpu_unit_code ("1_6");
9716 _0mii_ = get_cpu_unit_code ("1b_0mii.");
9717 _0mmi_ = get_cpu_unit_code ("1b_0mmi.");
9718 _0mfi_ = get_cpu_unit_code ("1b_0mfi.");
9719 _0mmf_ = get_cpu_unit_code ("1b_0mmf.");
9720 _0bbb_ = get_cpu_unit_code ("1b_0bbb.");
9721 _0mbb_ = get_cpu_unit_code ("1b_0mbb.");
9722 _0mib_ = get_cpu_unit_code ("1b_0mib.");
9723 _0mmb_ = get_cpu_unit_code ("1b_0mmb.");
9724 _0mfb_ = get_cpu_unit_code ("1b_0mfb.");
9725 _0mlx_ = get_cpu_unit_code ("1b_0mlx.");
9726 _1mii_ = get_cpu_unit_code ("1b_1mii.");
9727 _1mmi_ = get_cpu_unit_code ("1b_1mmi.");
9728 _1mfi_ = get_cpu_unit_code ("1b_1mfi.");
9729 _1mmf_ = get_cpu_unit_code ("1b_1mmf.");
9730 _1bbb_ = get_cpu_unit_code ("1b_1bbb.");
9731 _1mbb_ = get_cpu_unit_code ("1b_1mbb.");
9732 _1mib_ = get_cpu_unit_code ("1b_1mib.");
9733 _1mmb_ = get_cpu_unit_code ("1b_1mmb.");
9734 _1mfb_ = get_cpu_unit_code ("1b_1mfb.");
9735 _1mlx_ = get_cpu_unit_code ("1b_1mlx.");
9738 if (flag_selective_scheduling2
9739 && !maybe_skip_selective_scheduling ())
9740 run_selective_scheduling ();
9741 else
9742 schedule_ebbs ();
9744 /* Redo alignment computation, as it might gone wrong. */
9745 compute_alignments ();
9747 /* We cannot reuse this one because it has been corrupted by the
9748 evil glat. */
9749 finish_bundle_states ();
9750 free (stops_p);
9751 stops_p = NULL;
9752 emit_insn_group_barriers (dump_file);
9754 ia64_final_schedule = 0;
9755 timevar_pop (TV_SCHED2);
9757 else
9758 emit_all_insn_group_barriers (dump_file);
9760 df_analyze ();
9762 /* A call must not be the last instruction in a function, so that the
9763 return address is still within the function, so that unwinding works
9764 properly. Note that IA-64 differs from dwarf2 on this point. */
9765 if (ia64_except_unwind_info (&global_options) == UI_TARGET)
9767 rtx insn;
9768 int saw_stop = 0;
9770 insn = get_last_insn ();
9771 if (! INSN_P (insn))
9772 insn = prev_active_insn (insn);
9773 if (insn)
9775 /* Skip over insns that expand to nothing. */
9776 while (NONJUMP_INSN_P (insn)
9777 && get_attr_empty (insn) == EMPTY_YES)
9779 if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
9780 && XINT (PATTERN (insn), 1) == UNSPECV_INSN_GROUP_BARRIER)
9781 saw_stop = 1;
9782 insn = prev_active_insn (insn);
9784 if (CALL_P (insn))
9786 if (! saw_stop)
9787 emit_insn (gen_insn_group_barrier (GEN_INT (3)));
9788 emit_insn (gen_break_f ());
9789 emit_insn (gen_insn_group_barrier (GEN_INT (3)));
9794 emit_predicate_relation_info ();
9796 if (flag_var_tracking)
9798 timevar_push (TV_VAR_TRACKING);
9799 variable_tracking_main ();
9800 timevar_pop (TV_VAR_TRACKING);
9802 df_finish_pass (false);
9805 /* Return true if REGNO is used by the epilogue. */
9808 ia64_epilogue_uses (int regno)
9810 switch (regno)
9812 case R_GR (1):
9813 /* With a call to a function in another module, we will write a new
9814 value to "gp". After returning from such a call, we need to make
9815 sure the function restores the original gp-value, even if the
9816 function itself does not use the gp anymore. */
9817 return !(TARGET_AUTO_PIC || TARGET_NO_PIC);
9819 case IN_REG (0): case IN_REG (1): case IN_REG (2): case IN_REG (3):
9820 case IN_REG (4): case IN_REG (5): case IN_REG (6): case IN_REG (7):
9821 /* For functions defined with the syscall_linkage attribute, all
9822 input registers are marked as live at all function exits. This
9823 prevents the register allocator from using the input registers,
9824 which in turn makes it possible to restart a system call after
9825 an interrupt without having to save/restore the input registers.
9826 This also prevents kernel data from leaking to application code. */
9827 return lookup_attribute ("syscall_linkage",
9828 TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))) != NULL;
9830 case R_BR (0):
9831 /* Conditional return patterns can't represent the use of `b0' as
9832 the return address, so we force the value live this way. */
9833 return 1;
9835 case AR_PFS_REGNUM:
9836 /* Likewise for ar.pfs, which is used by br.ret. */
9837 return 1;
9839 default:
9840 return 0;
9844 /* Return true if REGNO is used by the frame unwinder. */
9847 ia64_eh_uses (int regno)
9849 unsigned int r;
9851 if (! reload_completed)
9852 return 0;
9854 if (regno == 0)
9855 return 0;
9857 for (r = reg_save_b0; r <= reg_save_ar_lc; r++)
9858 if (regno == current_frame_info.r[r]
9859 || regno == emitted_frame_related_regs[r])
9860 return 1;
9862 return 0;
9865 /* Return true if this goes in small data/bss. */
9867 /* ??? We could also support own long data here. Generating movl/add/ld8
9868 instead of addl,ld8/ld8. This makes the code bigger, but should make the
9869 code faster because there is one less load. This also includes incomplete
9870 types which can't go in sdata/sbss. */
9872 static bool
9873 ia64_in_small_data_p (const_tree exp)
9875 if (TARGET_NO_SDATA)
9876 return false;
9878 /* We want to merge strings, so we never consider them small data. */
9879 if (TREE_CODE (exp) == STRING_CST)
9880 return false;
9882 /* Functions are never small data. */
9883 if (TREE_CODE (exp) == FUNCTION_DECL)
9884 return false;
9886 if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
9888 const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
9890 if (strcmp (section, ".sdata") == 0
9891 || strncmp (section, ".sdata.", 7) == 0
9892 || strncmp (section, ".gnu.linkonce.s.", 16) == 0
9893 || strcmp (section, ".sbss") == 0
9894 || strncmp (section, ".sbss.", 6) == 0
9895 || strncmp (section, ".gnu.linkonce.sb.", 17) == 0)
9896 return true;
9898 else
9900 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
9902 /* If this is an incomplete type with size 0, then we can't put it
9903 in sdata because it might be too big when completed. */
9904 if (size > 0 && size <= ia64_section_threshold)
9905 return true;
9908 return false;
9911 /* Output assembly directives for prologue regions. */
9913 /* The current basic block number. */
9915 static bool last_block;
9917 /* True if we need a copy_state command at the start of the next block. */
9919 static bool need_copy_state;
9921 #ifndef MAX_ARTIFICIAL_LABEL_BYTES
9922 # define MAX_ARTIFICIAL_LABEL_BYTES 30
9923 #endif
9925 /* The function emits unwind directives for the start of an epilogue. */
9927 static void
9928 process_epilogue (FILE *asm_out_file, rtx insn ATTRIBUTE_UNUSED,
9929 bool unwind, bool frame ATTRIBUTE_UNUSED)
9931 /* If this isn't the last block of the function, then we need to label the
9932 current state, and copy it back in at the start of the next block. */
9934 if (!last_block)
9936 if (unwind)
9937 fprintf (asm_out_file, "\t.label_state %d\n",
9938 ++cfun->machine->state_num);
9939 need_copy_state = true;
9942 if (unwind)
9943 fprintf (asm_out_file, "\t.restore sp\n");
9946 /* This function processes a SET pattern for REG_CFA_ADJUST_CFA. */
9948 static void
9949 process_cfa_adjust_cfa (FILE *asm_out_file, rtx pat, rtx insn,
9950 bool unwind, bool frame)
9952 rtx dest = SET_DEST (pat);
9953 rtx src = SET_SRC (pat);
9955 if (dest == stack_pointer_rtx)
9957 if (GET_CODE (src) == PLUS)
9959 rtx op0 = XEXP (src, 0);
9960 rtx op1 = XEXP (src, 1);
9962 gcc_assert (op0 == dest && GET_CODE (op1) == CONST_INT);
9964 if (INTVAL (op1) < 0)
9966 gcc_assert (!frame_pointer_needed);
9967 if (unwind)
9968 fprintf (asm_out_file,
9969 "\t.fframe "HOST_WIDE_INT_PRINT_DEC"\n",
9970 -INTVAL (op1));
9972 else
9973 process_epilogue (asm_out_file, insn, unwind, frame);
9975 else
9977 gcc_assert (src == hard_frame_pointer_rtx);
9978 process_epilogue (asm_out_file, insn, unwind, frame);
9981 else if (dest == hard_frame_pointer_rtx)
9983 gcc_assert (src == stack_pointer_rtx);
9984 gcc_assert (frame_pointer_needed);
9986 if (unwind)
9987 fprintf (asm_out_file, "\t.vframe r%d\n",
9988 ia64_dbx_register_number (REGNO (dest)));
9990 else
9991 gcc_unreachable ();
9994 /* This function processes a SET pattern for REG_CFA_REGISTER. */
9996 static void
9997 process_cfa_register (FILE *asm_out_file, rtx pat, bool unwind)
9999 rtx dest = SET_DEST (pat);
10000 rtx src = SET_SRC (pat);
10001 int dest_regno = REGNO (dest);
10002 int src_regno;
10004 if (src == pc_rtx)
10006 /* Saving return address pointer. */
10007 if (unwind)
10008 fprintf (asm_out_file, "\t.save rp, r%d\n",
10009 ia64_dbx_register_number (dest_regno));
10010 return;
10013 src_regno = REGNO (src);
10015 switch (src_regno)
10017 case PR_REG (0):
10018 gcc_assert (dest_regno == current_frame_info.r[reg_save_pr]);
10019 if (unwind)
10020 fprintf (asm_out_file, "\t.save pr, r%d\n",
10021 ia64_dbx_register_number (dest_regno));
10022 break;
10024 case AR_UNAT_REGNUM:
10025 gcc_assert (dest_regno == current_frame_info.r[reg_save_ar_unat]);
10026 if (unwind)
10027 fprintf (asm_out_file, "\t.save ar.unat, r%d\n",
10028 ia64_dbx_register_number (dest_regno));
10029 break;
10031 case AR_LC_REGNUM:
10032 gcc_assert (dest_regno == current_frame_info.r[reg_save_ar_lc]);
10033 if (unwind)
10034 fprintf (asm_out_file, "\t.save ar.lc, r%d\n",
10035 ia64_dbx_register_number (dest_regno));
10036 break;
10038 default:
10039 /* Everything else should indicate being stored to memory. */
10040 gcc_unreachable ();
10044 /* This function processes a SET pattern for REG_CFA_OFFSET. */
10046 static void
10047 process_cfa_offset (FILE *asm_out_file, rtx pat, bool unwind)
10049 rtx dest = SET_DEST (pat);
10050 rtx src = SET_SRC (pat);
10051 int src_regno = REGNO (src);
10052 const char *saveop;
10053 HOST_WIDE_INT off;
10054 rtx base;
10056 gcc_assert (MEM_P (dest));
10057 if (GET_CODE (XEXP (dest, 0)) == REG)
10059 base = XEXP (dest, 0);
10060 off = 0;
10062 else
10064 gcc_assert (GET_CODE (XEXP (dest, 0)) == PLUS
10065 && GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT);
10066 base = XEXP (XEXP (dest, 0), 0);
10067 off = INTVAL (XEXP (XEXP (dest, 0), 1));
10070 if (base == hard_frame_pointer_rtx)
10072 saveop = ".savepsp";
10073 off = - off;
10075 else
10077 gcc_assert (base == stack_pointer_rtx);
10078 saveop = ".savesp";
10081 src_regno = REGNO (src);
10082 switch (src_regno)
10084 case BR_REG (0):
10085 gcc_assert (!current_frame_info.r[reg_save_b0]);
10086 if (unwind)
10087 fprintf (asm_out_file, "\t%s rp, " HOST_WIDE_INT_PRINT_DEC "\n",
10088 saveop, off);
10089 break;
10091 case PR_REG (0):
10092 gcc_assert (!current_frame_info.r[reg_save_pr]);
10093 if (unwind)
10094 fprintf (asm_out_file, "\t%s pr, " HOST_WIDE_INT_PRINT_DEC "\n",
10095 saveop, off);
10096 break;
10098 case AR_LC_REGNUM:
10099 gcc_assert (!current_frame_info.r[reg_save_ar_lc]);
10100 if (unwind)
10101 fprintf (asm_out_file, "\t%s ar.lc, " HOST_WIDE_INT_PRINT_DEC "\n",
10102 saveop, off);
10103 break;
10105 case AR_PFS_REGNUM:
10106 gcc_assert (!current_frame_info.r[reg_save_ar_pfs]);
10107 if (unwind)
10108 fprintf (asm_out_file, "\t%s ar.pfs, " HOST_WIDE_INT_PRINT_DEC "\n",
10109 saveop, off);
10110 break;
10112 case AR_UNAT_REGNUM:
10113 gcc_assert (!current_frame_info.r[reg_save_ar_unat]);
10114 if (unwind)
10115 fprintf (asm_out_file, "\t%s ar.unat, " HOST_WIDE_INT_PRINT_DEC "\n",
10116 saveop, off);
10117 break;
10119 case GR_REG (4):
10120 case GR_REG (5):
10121 case GR_REG (6):
10122 case GR_REG (7):
10123 if (unwind)
10124 fprintf (asm_out_file, "\t.save.g 0x%x\n",
10125 1 << (src_regno - GR_REG (4)));
10126 break;
10128 case BR_REG (1):
10129 case BR_REG (2):
10130 case BR_REG (3):
10131 case BR_REG (4):
10132 case BR_REG (5):
10133 if (unwind)
10134 fprintf (asm_out_file, "\t.save.b 0x%x\n",
10135 1 << (src_regno - BR_REG (1)));
10136 break;
10138 case FR_REG (2):
10139 case FR_REG (3):
10140 case FR_REG (4):
10141 case FR_REG (5):
10142 if (unwind)
10143 fprintf (asm_out_file, "\t.save.f 0x%x\n",
10144 1 << (src_regno - FR_REG (2)));
10145 break;
10147 case FR_REG (16): case FR_REG (17): case FR_REG (18): case FR_REG (19):
10148 case FR_REG (20): case FR_REG (21): case FR_REG (22): case FR_REG (23):
10149 case FR_REG (24): case FR_REG (25): case FR_REG (26): case FR_REG (27):
10150 case FR_REG (28): case FR_REG (29): case FR_REG (30): case FR_REG (31):
10151 if (unwind)
10152 fprintf (asm_out_file, "\t.save.gf 0x0, 0x%x\n",
10153 1 << (src_regno - FR_REG (12)));
10154 break;
10156 default:
10157 /* ??? For some reason we mark other general registers, even those
10158 we can't represent in the unwind info. Ignore them. */
10159 break;
10163 /* This function looks at a single insn and emits any directives
10164 required to unwind this insn. */
10166 static void
10167 ia64_asm_unwind_emit (FILE *asm_out_file, rtx insn)
10169 bool unwind = ia64_except_unwind_info (&global_options) == UI_TARGET;
10170 bool frame = dwarf2out_do_frame ();
10171 rtx note, pat;
10172 bool handled_one;
10174 if (!unwind && !frame)
10175 return;
10177 if (NOTE_INSN_BASIC_BLOCK_P (insn))
10179 last_block = NOTE_BASIC_BLOCK (insn)->next_bb == EXIT_BLOCK_PTR;
10181 /* Restore unwind state from immediately before the epilogue. */
10182 if (need_copy_state)
10184 if (unwind)
10186 fprintf (asm_out_file, "\t.body\n");
10187 fprintf (asm_out_file, "\t.copy_state %d\n",
10188 cfun->machine->state_num);
10190 need_copy_state = false;
10194 if (NOTE_P (insn) || ! RTX_FRAME_RELATED_P (insn))
10195 return;
10197 /* Look for the ALLOC insn. */
10198 if (INSN_CODE (insn) == CODE_FOR_alloc)
10200 rtx dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
10201 int dest_regno = REGNO (dest);
10203 /* If this is the final destination for ar.pfs, then this must
10204 be the alloc in the prologue. */
10205 if (dest_regno == current_frame_info.r[reg_save_ar_pfs])
10207 if (unwind)
10208 fprintf (asm_out_file, "\t.save ar.pfs, r%d\n",
10209 ia64_dbx_register_number (dest_regno));
10211 else
10213 /* This must be an alloc before a sibcall. We must drop the
10214 old frame info. The easiest way to drop the old frame
10215 info is to ensure we had a ".restore sp" directive
10216 followed by a new prologue. If the procedure doesn't
10217 have a memory-stack frame, we'll issue a dummy ".restore
10218 sp" now. */
10219 if (current_frame_info.total_size == 0 && !frame_pointer_needed)
10220 /* if haven't done process_epilogue() yet, do it now */
10221 process_epilogue (asm_out_file, insn, unwind, frame);
10222 if (unwind)
10223 fprintf (asm_out_file, "\t.prologue\n");
10225 return;
10228 handled_one = false;
10229 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
10230 switch (REG_NOTE_KIND (note))
10232 case REG_CFA_ADJUST_CFA:
10233 pat = XEXP (note, 0);
10234 if (pat == NULL)
10235 pat = PATTERN (insn);
10236 process_cfa_adjust_cfa (asm_out_file, pat, insn, unwind, frame);
10237 handled_one = true;
10238 break;
10240 case REG_CFA_OFFSET:
10241 pat = XEXP (note, 0);
10242 if (pat == NULL)
10243 pat = PATTERN (insn);
10244 process_cfa_offset (asm_out_file, pat, unwind);
10245 handled_one = true;
10246 break;
10248 case REG_CFA_REGISTER:
10249 pat = XEXP (note, 0);
10250 if (pat == NULL)
10251 pat = PATTERN (insn);
10252 process_cfa_register (asm_out_file, pat, unwind);
10253 handled_one = true;
10254 break;
10256 case REG_FRAME_RELATED_EXPR:
10257 case REG_CFA_DEF_CFA:
10258 case REG_CFA_EXPRESSION:
10259 case REG_CFA_RESTORE:
10260 case REG_CFA_SET_VDRAP:
10261 /* Not used in the ia64 port. */
10262 gcc_unreachable ();
10264 default:
10265 /* Not a frame-related note. */
10266 break;
10269 /* All REG_FRAME_RELATED_P insns, besides ALLOC, are marked with the
10270 explicit action to take. No guessing required. */
10271 gcc_assert (handled_one);
10274 /* Implement TARGET_ASM_EMIT_EXCEPT_PERSONALITY. */
10276 static void
10277 ia64_asm_emit_except_personality (rtx personality)
10279 fputs ("\t.personality\t", asm_out_file);
10280 output_addr_const (asm_out_file, personality);
10281 fputc ('\n', asm_out_file);
10284 /* Implement TARGET_ASM_INITIALIZE_SECTIONS. */
10286 static void
10287 ia64_asm_init_sections (void)
10289 exception_section = get_unnamed_section (0, output_section_asm_op,
10290 "\t.handlerdata");
10293 /* Implement TARGET_DEBUG_UNWIND_INFO. */
10295 static enum unwind_info_type
10296 ia64_debug_unwind_info (void)
10298 return UI_TARGET;
10301 enum ia64_builtins
10303 IA64_BUILTIN_BSP,
10304 IA64_BUILTIN_COPYSIGNQ,
10305 IA64_BUILTIN_FABSQ,
10306 IA64_BUILTIN_FLUSHRS,
10307 IA64_BUILTIN_INFQ,
10308 IA64_BUILTIN_HUGE_VALQ,
10309 IA64_BUILTIN_max
10312 static GTY(()) tree ia64_builtins[(int) IA64_BUILTIN_max];
10314 void
10315 ia64_init_builtins (void)
10317 tree fpreg_type;
10318 tree float80_type;
10319 tree decl;
10321 /* The __fpreg type. */
10322 fpreg_type = make_node (REAL_TYPE);
10323 TYPE_PRECISION (fpreg_type) = 82;
10324 layout_type (fpreg_type);
10325 (*lang_hooks.types.register_builtin_type) (fpreg_type, "__fpreg");
10327 /* The __float80 type. */
10328 float80_type = make_node (REAL_TYPE);
10329 TYPE_PRECISION (float80_type) = 80;
10330 layout_type (float80_type);
10331 (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
10333 /* The __float128 type. */
10334 if (!TARGET_HPUX)
10336 tree ftype;
10337 tree float128_type = make_node (REAL_TYPE);
10339 TYPE_PRECISION (float128_type) = 128;
10340 layout_type (float128_type);
10341 (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
10343 /* TFmode support builtins. */
10344 ftype = build_function_type_list (float128_type, NULL_TREE);
10345 decl = add_builtin_function ("__builtin_infq", ftype,
10346 IA64_BUILTIN_INFQ, BUILT_IN_MD,
10347 NULL, NULL_TREE);
10348 ia64_builtins[IA64_BUILTIN_INFQ] = decl;
10350 decl = add_builtin_function ("__builtin_huge_valq", ftype,
10351 IA64_BUILTIN_HUGE_VALQ, BUILT_IN_MD,
10352 NULL, NULL_TREE);
10353 ia64_builtins[IA64_BUILTIN_HUGE_VALQ] = decl;
10355 ftype = build_function_type_list (float128_type,
10356 float128_type,
10357 NULL_TREE);
10358 decl = add_builtin_function ("__builtin_fabsq", ftype,
10359 IA64_BUILTIN_FABSQ, BUILT_IN_MD,
10360 "__fabstf2", NULL_TREE);
10361 TREE_READONLY (decl) = 1;
10362 ia64_builtins[IA64_BUILTIN_FABSQ] = decl;
10364 ftype = build_function_type_list (float128_type,
10365 float128_type,
10366 float128_type,
10367 NULL_TREE);
10368 decl = add_builtin_function ("__builtin_copysignq", ftype,
10369 IA64_BUILTIN_COPYSIGNQ, BUILT_IN_MD,
10370 "__copysigntf3", NULL_TREE);
10371 TREE_READONLY (decl) = 1;
10372 ia64_builtins[IA64_BUILTIN_COPYSIGNQ] = decl;
10374 else
10375 /* Under HPUX, this is a synonym for "long double". */
10376 (*lang_hooks.types.register_builtin_type) (long_double_type_node,
10377 "__float128");
10379 /* Fwrite on VMS is non-standard. */
10380 #if TARGET_ABI_OPEN_VMS
10381 vms_patch_builtins ();
10382 #endif
10384 #define def_builtin(name, type, code) \
10385 add_builtin_function ((name), (type), (code), BUILT_IN_MD, \
10386 NULL, NULL_TREE)
10388 decl = def_builtin ("__builtin_ia64_bsp",
10389 build_function_type_list (ptr_type_node, NULL_TREE),
10390 IA64_BUILTIN_BSP);
10391 ia64_builtins[IA64_BUILTIN_BSP] = decl;
10393 decl = def_builtin ("__builtin_ia64_flushrs",
10394 build_function_type_list (void_type_node, NULL_TREE),
10395 IA64_BUILTIN_FLUSHRS);
10396 ia64_builtins[IA64_BUILTIN_FLUSHRS] = decl;
10398 #undef def_builtin
10400 if (TARGET_HPUX)
10402 if ((decl = builtin_decl_explicit (BUILT_IN_FINITE)) != NULL_TREE)
10403 set_user_assembler_name (decl, "_Isfinite");
10404 if ((decl = builtin_decl_explicit (BUILT_IN_FINITEF)) != NULL_TREE)
10405 set_user_assembler_name (decl, "_Isfinitef");
10406 if ((decl = builtin_decl_explicit (BUILT_IN_FINITEL)) != NULL_TREE)
10407 set_user_assembler_name (decl, "_Isfinitef128");
10412 ia64_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
10413 enum machine_mode mode ATTRIBUTE_UNUSED,
10414 int ignore ATTRIBUTE_UNUSED)
10416 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10417 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10419 switch (fcode)
10421 case IA64_BUILTIN_BSP:
10422 if (! target || ! register_operand (target, DImode))
10423 target = gen_reg_rtx (DImode);
10424 emit_insn (gen_bsp_value (target));
10425 #ifdef POINTERS_EXTEND_UNSIGNED
10426 target = convert_memory_address (ptr_mode, target);
10427 #endif
10428 return target;
10430 case IA64_BUILTIN_FLUSHRS:
10431 emit_insn (gen_flushrs ());
10432 return const0_rtx;
10434 case IA64_BUILTIN_INFQ:
10435 case IA64_BUILTIN_HUGE_VALQ:
10437 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
10438 REAL_VALUE_TYPE inf;
10439 rtx tmp;
10441 real_inf (&inf);
10442 tmp = CONST_DOUBLE_FROM_REAL_VALUE (inf, target_mode);
10444 tmp = validize_mem (force_const_mem (target_mode, tmp));
10446 if (target == 0)
10447 target = gen_reg_rtx (target_mode);
10449 emit_move_insn (target, tmp);
10450 return target;
10453 case IA64_BUILTIN_FABSQ:
10454 case IA64_BUILTIN_COPYSIGNQ:
10455 return expand_call (exp, target, ignore);
10457 default:
10458 gcc_unreachable ();
10461 return NULL_RTX;
10464 /* Return the ia64 builtin for CODE. */
10466 static tree
10467 ia64_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
10469 if (code >= IA64_BUILTIN_max)
10470 return error_mark_node;
10472 return ia64_builtins[code];
10475 /* For the HP-UX IA64 aggregate parameters are passed stored in the
10476 most significant bits of the stack slot. */
10478 enum direction
10479 ia64_hpux_function_arg_padding (enum machine_mode mode, const_tree type)
10481 /* Exception to normal case for structures/unions/etc. */
10483 if (type && AGGREGATE_TYPE_P (type)
10484 && int_size_in_bytes (type) < UNITS_PER_WORD)
10485 return upward;
10487 /* Fall back to the default. */
10488 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
10491 /* Emit text to declare externally defined variables and functions, because
10492 the Intel assembler does not support undefined externals. */
10494 void
10495 ia64_asm_output_external (FILE *file, tree decl, const char *name)
10497 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
10498 set in order to avoid putting out names that are never really
10499 used. */
10500 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
10502 /* maybe_assemble_visibility will return 1 if the assembler
10503 visibility directive is output. */
10504 int need_visibility = ((*targetm.binds_local_p) (decl)
10505 && maybe_assemble_visibility (decl));
10507 /* GNU as does not need anything here, but the HP linker does
10508 need something for external functions. */
10509 if ((TARGET_HPUX_LD || !TARGET_GNU_AS)
10510 && TREE_CODE (decl) == FUNCTION_DECL)
10511 (*targetm.asm_out.globalize_decl_name) (file, decl);
10512 else if (need_visibility && !TARGET_GNU_AS)
10513 (*targetm.asm_out.globalize_label) (file, name);
10517 /* Set SImode div/mod functions, init_integral_libfuncs only initializes
10518 modes of word_mode and larger. Rename the TFmode libfuncs using the
10519 HPUX conventions. __divtf3 is used for XFmode. We need to keep it for
10520 backward compatibility. */
10522 static void
10523 ia64_init_libfuncs (void)
10525 set_optab_libfunc (sdiv_optab, SImode, "__divsi3");
10526 set_optab_libfunc (udiv_optab, SImode, "__udivsi3");
10527 set_optab_libfunc (smod_optab, SImode, "__modsi3");
10528 set_optab_libfunc (umod_optab, SImode, "__umodsi3");
10530 set_optab_libfunc (add_optab, TFmode, "_U_Qfadd");
10531 set_optab_libfunc (sub_optab, TFmode, "_U_Qfsub");
10532 set_optab_libfunc (smul_optab, TFmode, "_U_Qfmpy");
10533 set_optab_libfunc (sdiv_optab, TFmode, "_U_Qfdiv");
10534 set_optab_libfunc (neg_optab, TFmode, "_U_Qfneg");
10536 set_conv_libfunc (sext_optab, TFmode, SFmode, "_U_Qfcnvff_sgl_to_quad");
10537 set_conv_libfunc (sext_optab, TFmode, DFmode, "_U_Qfcnvff_dbl_to_quad");
10538 set_conv_libfunc (sext_optab, TFmode, XFmode, "_U_Qfcnvff_f80_to_quad");
10539 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_U_Qfcnvff_quad_to_sgl");
10540 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_U_Qfcnvff_quad_to_dbl");
10541 set_conv_libfunc (trunc_optab, XFmode, TFmode, "_U_Qfcnvff_quad_to_f80");
10543 set_conv_libfunc (sfix_optab, SImode, TFmode, "_U_Qfcnvfxt_quad_to_sgl");
10544 set_conv_libfunc (sfix_optab, DImode, TFmode, "_U_Qfcnvfxt_quad_to_dbl");
10545 set_conv_libfunc (sfix_optab, TImode, TFmode, "_U_Qfcnvfxt_quad_to_quad");
10546 set_conv_libfunc (ufix_optab, SImode, TFmode, "_U_Qfcnvfxut_quad_to_sgl");
10547 set_conv_libfunc (ufix_optab, DImode, TFmode, "_U_Qfcnvfxut_quad_to_dbl");
10549 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_U_Qfcnvxf_sgl_to_quad");
10550 set_conv_libfunc (sfloat_optab, TFmode, DImode, "_U_Qfcnvxf_dbl_to_quad");
10551 set_conv_libfunc (sfloat_optab, TFmode, TImode, "_U_Qfcnvxf_quad_to_quad");
10552 /* HP-UX 11.23 libc does not have a function for unsigned
10553 SImode-to-TFmode conversion. */
10554 set_conv_libfunc (ufloat_optab, TFmode, DImode, "_U_Qfcnvxuf_dbl_to_quad");
10557 /* Rename all the TFmode libfuncs using the HPUX conventions. */
10559 static void
10560 ia64_hpux_init_libfuncs (void)
10562 ia64_init_libfuncs ();
10564 /* The HP SI millicode division and mod functions expect DI arguments.
10565 By turning them off completely we avoid using both libgcc and the
10566 non-standard millicode routines and use the HP DI millicode routines
10567 instead. */
10569 set_optab_libfunc (sdiv_optab, SImode, 0);
10570 set_optab_libfunc (udiv_optab, SImode, 0);
10571 set_optab_libfunc (smod_optab, SImode, 0);
10572 set_optab_libfunc (umod_optab, SImode, 0);
10574 set_optab_libfunc (sdiv_optab, DImode, "__milli_divI");
10575 set_optab_libfunc (udiv_optab, DImode, "__milli_divU");
10576 set_optab_libfunc (smod_optab, DImode, "__milli_remI");
10577 set_optab_libfunc (umod_optab, DImode, "__milli_remU");
10579 /* HP-UX libc has TF min/max/abs routines in it. */
10580 set_optab_libfunc (smin_optab, TFmode, "_U_Qfmin");
10581 set_optab_libfunc (smax_optab, TFmode, "_U_Qfmax");
10582 set_optab_libfunc (abs_optab, TFmode, "_U_Qfabs");
10584 /* ia64_expand_compare uses this. */
10585 cmptf_libfunc = init_one_libfunc ("_U_Qfcmp");
10587 /* These should never be used. */
10588 set_optab_libfunc (eq_optab, TFmode, 0);
10589 set_optab_libfunc (ne_optab, TFmode, 0);
10590 set_optab_libfunc (gt_optab, TFmode, 0);
10591 set_optab_libfunc (ge_optab, TFmode, 0);
10592 set_optab_libfunc (lt_optab, TFmode, 0);
10593 set_optab_libfunc (le_optab, TFmode, 0);
10596 /* Rename the division and modulus functions in VMS. */
10598 static void
10599 ia64_vms_init_libfuncs (void)
10601 set_optab_libfunc (sdiv_optab, SImode, "OTS$DIV_I");
10602 set_optab_libfunc (sdiv_optab, DImode, "OTS$DIV_L");
10603 set_optab_libfunc (udiv_optab, SImode, "OTS$DIV_UI");
10604 set_optab_libfunc (udiv_optab, DImode, "OTS$DIV_UL");
10605 set_optab_libfunc (smod_optab, SImode, "OTS$REM_I");
10606 set_optab_libfunc (smod_optab, DImode, "OTS$REM_L");
10607 set_optab_libfunc (umod_optab, SImode, "OTS$REM_UI");
10608 set_optab_libfunc (umod_optab, DImode, "OTS$REM_UL");
10609 abort_libfunc = init_one_libfunc ("decc$abort");
10610 memcmp_libfunc = init_one_libfunc ("decc$memcmp");
10611 #ifdef MEM_LIBFUNCS_INIT
10612 MEM_LIBFUNCS_INIT;
10613 #endif
10616 /* Rename the TFmode libfuncs available from soft-fp in glibc using
10617 the HPUX conventions. */
10619 static void
10620 ia64_sysv4_init_libfuncs (void)
10622 ia64_init_libfuncs ();
10624 /* These functions are not part of the HPUX TFmode interface. We
10625 use them instead of _U_Qfcmp, which doesn't work the way we
10626 expect. */
10627 set_optab_libfunc (eq_optab, TFmode, "_U_Qfeq");
10628 set_optab_libfunc (ne_optab, TFmode, "_U_Qfne");
10629 set_optab_libfunc (gt_optab, TFmode, "_U_Qfgt");
10630 set_optab_libfunc (ge_optab, TFmode, "_U_Qfge");
10631 set_optab_libfunc (lt_optab, TFmode, "_U_Qflt");
10632 set_optab_libfunc (le_optab, TFmode, "_U_Qfle");
10634 /* We leave out _U_Qfmin, _U_Qfmax and _U_Qfabs since soft-fp in
10635 glibc doesn't have them. */
10638 /* Use soft-fp. */
10640 static void
10641 ia64_soft_fp_init_libfuncs (void)
10645 static bool
10646 ia64_vms_valid_pointer_mode (enum machine_mode mode)
10648 return (mode == SImode || mode == DImode);
10651 /* For HPUX, it is illegal to have relocations in shared segments. */
10653 static int
10654 ia64_hpux_reloc_rw_mask (void)
10656 return 3;
10659 /* For others, relax this so that relocations to local data goes in
10660 read-only segments, but we still cannot allow global relocations
10661 in read-only segments. */
10663 static int
10664 ia64_reloc_rw_mask (void)
10666 return flag_pic ? 3 : 2;
10669 /* Return the section to use for X. The only special thing we do here
10670 is to honor small data. */
10672 static section *
10673 ia64_select_rtx_section (enum machine_mode mode, rtx x,
10674 unsigned HOST_WIDE_INT align)
10676 if (GET_MODE_SIZE (mode) > 0
10677 && GET_MODE_SIZE (mode) <= ia64_section_threshold
10678 && !TARGET_NO_SDATA)
10679 return sdata_section;
10680 else
10681 return default_elf_select_rtx_section (mode, x, align);
10684 static unsigned int
10685 ia64_section_type_flags (tree decl, const char *name, int reloc)
10687 unsigned int flags = 0;
10689 if (strcmp (name, ".sdata") == 0
10690 || strncmp (name, ".sdata.", 7) == 0
10691 || strncmp (name, ".gnu.linkonce.s.", 16) == 0
10692 || strncmp (name, ".sdata2.", 8) == 0
10693 || strncmp (name, ".gnu.linkonce.s2.", 17) == 0
10694 || strcmp (name, ".sbss") == 0
10695 || strncmp (name, ".sbss.", 6) == 0
10696 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
10697 flags = SECTION_SMALL;
10699 flags |= default_section_type_flags (decl, name, reloc);
10700 return flags;
10703 /* Returns true if FNTYPE (a FUNCTION_TYPE or a METHOD_TYPE) returns a
10704 structure type and that the address of that type should be passed
10705 in out0, rather than in r8. */
10707 static bool
10708 ia64_struct_retval_addr_is_first_parm_p (tree fntype)
10710 tree ret_type = TREE_TYPE (fntype);
10712 /* The Itanium C++ ABI requires that out0, rather than r8, be used
10713 as the structure return address parameter, if the return value
10714 type has a non-trivial copy constructor or destructor. It is not
10715 clear if this same convention should be used for other
10716 programming languages. Until G++ 3.4, we incorrectly used r8 for
10717 these return values. */
10718 return (abi_version_at_least (2)
10719 && ret_type
10720 && TYPE_MODE (ret_type) == BLKmode
10721 && TREE_ADDRESSABLE (ret_type)
10722 && strcmp (lang_hooks.name, "GNU C++") == 0);
10725 /* Output the assembler code for a thunk function. THUNK_DECL is the
10726 declaration for the thunk function itself, FUNCTION is the decl for
10727 the target function. DELTA is an immediate constant offset to be
10728 added to THIS. If VCALL_OFFSET is nonzero, the word at
10729 *(*this + vcall_offset) should be added to THIS. */
10731 static void
10732 ia64_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
10733 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
10734 tree function)
10736 rtx this_rtx, insn, funexp;
10737 unsigned int this_parmno;
10738 unsigned int this_regno;
10739 rtx delta_rtx;
10741 reload_completed = 1;
10742 epilogue_completed = 1;
10744 /* Set things up as ia64_expand_prologue might. */
10745 last_scratch_gr_reg = 15;
10747 memset (&current_frame_info, 0, sizeof (current_frame_info));
10748 current_frame_info.spill_cfa_off = -16;
10749 current_frame_info.n_input_regs = 1;
10750 current_frame_info.need_regstk = (TARGET_REG_NAMES != 0);
10752 /* Mark the end of the (empty) prologue. */
10753 emit_note (NOTE_INSN_PROLOGUE_END);
10755 /* Figure out whether "this" will be the first parameter (the
10756 typical case) or the second parameter (as happens when the
10757 virtual function returns certain class objects). */
10758 this_parmno
10759 = (ia64_struct_retval_addr_is_first_parm_p (TREE_TYPE (thunk))
10760 ? 1 : 0);
10761 this_regno = IN_REG (this_parmno);
10762 if (!TARGET_REG_NAMES)
10763 reg_names[this_regno] = ia64_reg_numbers[this_parmno];
10765 this_rtx = gen_rtx_REG (Pmode, this_regno);
10767 /* Apply the constant offset, if required. */
10768 delta_rtx = GEN_INT (delta);
10769 if (TARGET_ILP32)
10771 rtx tmp = gen_rtx_REG (ptr_mode, this_regno);
10772 REG_POINTER (tmp) = 1;
10773 if (delta && satisfies_constraint_I (delta_rtx))
10775 emit_insn (gen_ptr_extend_plus_imm (this_rtx, tmp, delta_rtx));
10776 delta = 0;
10778 else
10779 emit_insn (gen_ptr_extend (this_rtx, tmp));
10781 if (delta)
10783 if (!satisfies_constraint_I (delta_rtx))
10785 rtx tmp = gen_rtx_REG (Pmode, 2);
10786 emit_move_insn (tmp, delta_rtx);
10787 delta_rtx = tmp;
10789 emit_insn (gen_adddi3 (this_rtx, this_rtx, delta_rtx));
10792 /* Apply the offset from the vtable, if required. */
10793 if (vcall_offset)
10795 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
10796 rtx tmp = gen_rtx_REG (Pmode, 2);
10798 if (TARGET_ILP32)
10800 rtx t = gen_rtx_REG (ptr_mode, 2);
10801 REG_POINTER (t) = 1;
10802 emit_move_insn (t, gen_rtx_MEM (ptr_mode, this_rtx));
10803 if (satisfies_constraint_I (vcall_offset_rtx))
10805 emit_insn (gen_ptr_extend_plus_imm (tmp, t, vcall_offset_rtx));
10806 vcall_offset = 0;
10808 else
10809 emit_insn (gen_ptr_extend (tmp, t));
10811 else
10812 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
10814 if (vcall_offset)
10816 if (!satisfies_constraint_J (vcall_offset_rtx))
10818 rtx tmp2 = gen_rtx_REG (Pmode, next_scratch_gr_reg ());
10819 emit_move_insn (tmp2, vcall_offset_rtx);
10820 vcall_offset_rtx = tmp2;
10822 emit_insn (gen_adddi3 (tmp, tmp, vcall_offset_rtx));
10825 if (TARGET_ILP32)
10826 emit_insn (gen_zero_extendsidi2 (tmp, gen_rtx_MEM (ptr_mode, tmp)));
10827 else
10828 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
10830 emit_insn (gen_adddi3 (this_rtx, this_rtx, tmp));
10833 /* Generate a tail call to the target function. */
10834 if (! TREE_USED (function))
10836 assemble_external (function);
10837 TREE_USED (function) = 1;
10839 funexp = XEXP (DECL_RTL (function), 0);
10840 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
10841 ia64_expand_call (NULL_RTX, funexp, NULL_RTX, 1);
10842 insn = get_last_insn ();
10843 SIBLING_CALL_P (insn) = 1;
10845 /* Code generation for calls relies on splitting. */
10846 reload_completed = 1;
10847 epilogue_completed = 1;
10848 try_split (PATTERN (insn), insn, 0);
10850 emit_barrier ();
10852 /* Run just enough of rest_of_compilation to get the insns emitted.
10853 There's not really enough bulk here to make other passes such as
10854 instruction scheduling worth while. Note that use_thunk calls
10855 assemble_start_function and assemble_end_function. */
10857 emit_all_insn_group_barriers (NULL);
10858 insn = get_insns ();
10859 shorten_branches (insn);
10860 final_start_function (insn, file, 1);
10861 final (insn, file, 1);
10862 final_end_function ();
10864 reload_completed = 0;
10865 epilogue_completed = 0;
10868 /* Worker function for TARGET_STRUCT_VALUE_RTX. */
10870 static rtx
10871 ia64_struct_value_rtx (tree fntype,
10872 int incoming ATTRIBUTE_UNUSED)
10874 if (TARGET_ABI_OPEN_VMS ||
10875 (fntype && ia64_struct_retval_addr_is_first_parm_p (fntype)))
10876 return NULL_RTX;
10877 return gen_rtx_REG (Pmode, GR_REG (8));
10880 static bool
10881 ia64_scalar_mode_supported_p (enum machine_mode mode)
10883 switch (mode)
10885 case QImode:
10886 case HImode:
10887 case SImode:
10888 case DImode:
10889 case TImode:
10890 return true;
10892 case SFmode:
10893 case DFmode:
10894 case XFmode:
10895 case RFmode:
10896 return true;
10898 case TFmode:
10899 return true;
10901 default:
10902 return false;
10906 static bool
10907 ia64_vector_mode_supported_p (enum machine_mode mode)
10909 switch (mode)
10911 case V8QImode:
10912 case V4HImode:
10913 case V2SImode:
10914 return true;
10916 case V2SFmode:
10917 return true;
10919 default:
10920 return false;
10924 /* Implement the FUNCTION_PROFILER macro. */
10926 void
10927 ia64_output_function_profiler (FILE *file, int labelno)
10929 bool indirect_call;
10931 /* If the function needs a static chain and the static chain
10932 register is r15, we use an indirect call so as to bypass
10933 the PLT stub in case the executable is dynamically linked,
10934 because the stub clobbers r15 as per 5.3.6 of the psABI.
10935 We don't need to do that in non canonical PIC mode. */
10937 if (cfun->static_chain_decl && !TARGET_NO_PIC && !TARGET_AUTO_PIC)
10939 gcc_assert (STATIC_CHAIN_REGNUM == 15);
10940 indirect_call = true;
10942 else
10943 indirect_call = false;
10945 if (TARGET_GNU_AS)
10946 fputs ("\t.prologue 4, r40\n", file);
10947 else
10948 fputs ("\t.prologue\n\t.save ar.pfs, r40\n", file);
10949 fputs ("\talloc out0 = ar.pfs, 8, 0, 4, 0\n", file);
10951 if (NO_PROFILE_COUNTERS)
10952 fputs ("\tmov out3 = r0\n", file);
10953 else
10955 char buf[20];
10956 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
10958 if (TARGET_AUTO_PIC)
10959 fputs ("\tmovl out3 = @gprel(", file);
10960 else
10961 fputs ("\taddl out3 = @ltoff(", file);
10962 assemble_name (file, buf);
10963 if (TARGET_AUTO_PIC)
10964 fputs (")\n", file);
10965 else
10966 fputs ("), r1\n", file);
10969 if (indirect_call)
10970 fputs ("\taddl r14 = @ltoff(@fptr(_mcount)), r1\n", file);
10971 fputs ("\t;;\n", file);
10973 fputs ("\t.save rp, r42\n", file);
10974 fputs ("\tmov out2 = b0\n", file);
10975 if (indirect_call)
10976 fputs ("\tld8 r14 = [r14]\n\t;;\n", file);
10977 fputs ("\t.body\n", file);
10978 fputs ("\tmov out1 = r1\n", file);
10979 if (indirect_call)
10981 fputs ("\tld8 r16 = [r14], 8\n\t;;\n", file);
10982 fputs ("\tmov b6 = r16\n", file);
10983 fputs ("\tld8 r1 = [r14]\n", file);
10984 fputs ("\tbr.call.sptk.many b0 = b6\n\t;;\n", file);
10986 else
10987 fputs ("\tbr.call.sptk.many b0 = _mcount\n\t;;\n", file);
10990 static GTY(()) rtx mcount_func_rtx;
10991 static rtx
10992 gen_mcount_func_rtx (void)
10994 if (!mcount_func_rtx)
10995 mcount_func_rtx = init_one_libfunc ("_mcount");
10996 return mcount_func_rtx;
10999 void
11000 ia64_profile_hook (int labelno)
11002 rtx label, ip;
11004 if (NO_PROFILE_COUNTERS)
11005 label = const0_rtx;
11006 else
11008 char buf[30];
11009 const char *label_name;
11010 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
11011 label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
11012 label = gen_rtx_SYMBOL_REF (Pmode, label_name);
11013 SYMBOL_REF_FLAGS (label) = SYMBOL_FLAG_LOCAL;
11015 ip = gen_reg_rtx (Pmode);
11016 emit_insn (gen_ip_value (ip));
11017 emit_library_call (gen_mcount_func_rtx (), LCT_NORMAL,
11018 VOIDmode, 3,
11019 gen_rtx_REG (Pmode, BR_REG (0)), Pmode,
11020 ip, Pmode,
11021 label, Pmode);
11024 /* Return the mangling of TYPE if it is an extended fundamental type. */
11026 static const char *
11027 ia64_mangle_type (const_tree type)
11029 type = TYPE_MAIN_VARIANT (type);
11031 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
11032 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
11033 return NULL;
11035 /* On HP-UX, "long double" is mangled as "e" so __float128 is
11036 mangled as "e". */
11037 if (!TARGET_HPUX && TYPE_MODE (type) == TFmode)
11038 return "g";
11039 /* On HP-UX, "e" is not available as a mangling of __float80 so use
11040 an extended mangling. Elsewhere, "e" is available since long
11041 double is 80 bits. */
11042 if (TYPE_MODE (type) == XFmode)
11043 return TARGET_HPUX ? "u9__float80" : "e";
11044 if (TYPE_MODE (type) == RFmode)
11045 return "u7__fpreg";
11046 return NULL;
11049 /* Return the diagnostic message string if conversion from FROMTYPE to
11050 TOTYPE is not allowed, NULL otherwise. */
11051 static const char *
11052 ia64_invalid_conversion (const_tree fromtype, const_tree totype)
11054 /* Reject nontrivial conversion to or from __fpreg. */
11055 if (TYPE_MODE (fromtype) == RFmode
11056 && TYPE_MODE (totype) != RFmode
11057 && TYPE_MODE (totype) != VOIDmode)
11058 return N_("invalid conversion from %<__fpreg%>");
11059 if (TYPE_MODE (totype) == RFmode
11060 && TYPE_MODE (fromtype) != RFmode)
11061 return N_("invalid conversion to %<__fpreg%>");
11062 return NULL;
11065 /* Return the diagnostic message string if the unary operation OP is
11066 not permitted on TYPE, NULL otherwise. */
11067 static const char *
11068 ia64_invalid_unary_op (int op, const_tree type)
11070 /* Reject operations on __fpreg other than unary + or &. */
11071 if (TYPE_MODE (type) == RFmode
11072 && op != CONVERT_EXPR
11073 && op != ADDR_EXPR)
11074 return N_("invalid operation on %<__fpreg%>");
11075 return NULL;
11078 /* Return the diagnostic message string if the binary operation OP is
11079 not permitted on TYPE1 and TYPE2, NULL otherwise. */
11080 static const char *
11081 ia64_invalid_binary_op (int op ATTRIBUTE_UNUSED, const_tree type1, const_tree type2)
11083 /* Reject operations on __fpreg. */
11084 if (TYPE_MODE (type1) == RFmode || TYPE_MODE (type2) == RFmode)
11085 return N_("invalid operation on %<__fpreg%>");
11086 return NULL;
11089 /* HP-UX version_id attribute.
11090 For object foo, if the version_id is set to 1234 put out an alias
11091 of '.alias foo "foo{1234}" We can't use "foo{1234}" in anything
11092 other than an alias statement because it is an illegal symbol name. */
11094 static tree
11095 ia64_handle_version_id_attribute (tree *node ATTRIBUTE_UNUSED,
11096 tree name ATTRIBUTE_UNUSED,
11097 tree args,
11098 int flags ATTRIBUTE_UNUSED,
11099 bool *no_add_attrs)
11101 tree arg = TREE_VALUE (args);
11103 if (TREE_CODE (arg) != STRING_CST)
11105 error("version attribute is not a string");
11106 *no_add_attrs = true;
11107 return NULL_TREE;
11109 return NULL_TREE;
11112 /* Target hook for c_mode_for_suffix. */
11114 static enum machine_mode
11115 ia64_c_mode_for_suffix (char suffix)
11117 if (suffix == 'q')
11118 return TFmode;
11119 if (suffix == 'w')
11120 return XFmode;
11122 return VOIDmode;
11125 static GTY(()) rtx ia64_dconst_0_5_rtx;
11128 ia64_dconst_0_5 (void)
11130 if (! ia64_dconst_0_5_rtx)
11132 REAL_VALUE_TYPE rv;
11133 real_from_string (&rv, "0.5");
11134 ia64_dconst_0_5_rtx = const_double_from_real_value (rv, DFmode);
11136 return ia64_dconst_0_5_rtx;
11139 static GTY(()) rtx ia64_dconst_0_375_rtx;
11142 ia64_dconst_0_375 (void)
11144 if (! ia64_dconst_0_375_rtx)
11146 REAL_VALUE_TYPE rv;
11147 real_from_string (&rv, "0.375");
11148 ia64_dconst_0_375_rtx = const_double_from_real_value (rv, DFmode);
11150 return ia64_dconst_0_375_rtx;
11153 static enum machine_mode
11154 ia64_get_reg_raw_mode (int regno)
11156 if (FR_REGNO_P (regno))
11157 return XFmode;
11158 return default_get_reg_raw_mode(regno);
11161 /* Implement TARGET_MEMBER_TYPE_FORCES_BLK. ??? Might not be needed
11162 anymore. */
11164 bool
11165 ia64_member_type_forces_blk (const_tree, enum machine_mode mode)
11167 return TARGET_HPUX && mode == TFmode;
11170 /* Always default to .text section until HP-UX linker is fixed. */
11172 ATTRIBUTE_UNUSED static section *
11173 ia64_hpux_function_section (tree decl ATTRIBUTE_UNUSED,
11174 enum node_frequency freq ATTRIBUTE_UNUSED,
11175 bool startup ATTRIBUTE_UNUSED,
11176 bool exit ATTRIBUTE_UNUSED)
11178 return NULL;
11181 /* Construct (set target (vec_select op0 (parallel perm))) and
11182 return true if that's a valid instruction in the active ISA. */
11184 static bool
11185 expand_vselect (rtx target, rtx op0, const unsigned char *perm, unsigned nelt)
11187 rtx rperm[MAX_VECT_LEN], x;
11188 unsigned i;
11190 for (i = 0; i < nelt; ++i)
11191 rperm[i] = GEN_INT (perm[i]);
11193 x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
11194 x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
11195 x = gen_rtx_SET (VOIDmode, target, x);
11197 x = emit_insn (x);
11198 if (recog_memoized (x) < 0)
11200 remove_insn (x);
11201 return false;
11203 return true;
11206 /* Similar, but generate a vec_concat from op0 and op1 as well. */
11208 static bool
11209 expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
11210 const unsigned char *perm, unsigned nelt)
11212 enum machine_mode v2mode;
11213 rtx x;
11215 v2mode = GET_MODE_2XWIDER_MODE (GET_MODE (op0));
11216 x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
11217 return expand_vselect (target, x, perm, nelt);
11220 /* Try to expand a no-op permutation. */
11222 static bool
11223 expand_vec_perm_identity (struct expand_vec_perm_d *d)
11225 unsigned i, nelt = d->nelt;
11227 for (i = 0; i < nelt; ++i)
11228 if (d->perm[i] != i)
11229 return false;
11231 if (!d->testing_p)
11232 emit_move_insn (d->target, d->op0);
11234 return true;
11237 /* Try to expand D via a shrp instruction. */
11239 static bool
11240 expand_vec_perm_shrp (struct expand_vec_perm_d *d)
11242 unsigned i, nelt = d->nelt, shift, mask;
11243 rtx tmp, hi, lo;
11245 /* ??? Don't force V2SFmode into the integer registers. */
11246 if (d->vmode == V2SFmode)
11247 return false;
11249 mask = (d->one_operand_p ? nelt - 1 : 2 * nelt - 1);
11251 shift = d->perm[0];
11252 if (BYTES_BIG_ENDIAN && shift > nelt)
11253 return false;
11255 for (i = 1; i < nelt; ++i)
11256 if (d->perm[i] != ((shift + i) & mask))
11257 return false;
11259 if (d->testing_p)
11260 return true;
11262 hi = shift < nelt ? d->op1 : d->op0;
11263 lo = shift < nelt ? d->op0 : d->op1;
11265 shift %= nelt;
11267 shift *= GET_MODE_UNIT_SIZE (d->vmode) * BITS_PER_UNIT;
11269 /* We've eliminated the shift 0 case via expand_vec_perm_identity. */
11270 gcc_assert (IN_RANGE (shift, 1, 63));
11272 /* Recall that big-endian elements are numbered starting at the top of
11273 the register. Ideally we'd have a shift-left-pair. But since we
11274 don't, convert to a shift the other direction. */
11275 if (BYTES_BIG_ENDIAN)
11276 shift = 64 - shift;
11278 tmp = gen_reg_rtx (DImode);
11279 hi = gen_lowpart (DImode, hi);
11280 lo = gen_lowpart (DImode, lo);
11281 emit_insn (gen_shrp (tmp, hi, lo, GEN_INT (shift)));
11283 emit_move_insn (d->target, gen_lowpart (d->vmode, tmp));
11284 return true;
11287 /* Try to instantiate D in a single instruction. */
11289 static bool
11290 expand_vec_perm_1 (struct expand_vec_perm_d *d)
11292 unsigned i, nelt = d->nelt;
11293 unsigned char perm2[MAX_VECT_LEN];
11295 /* Try single-operand selections. */
11296 if (d->one_operand_p)
11298 if (expand_vec_perm_identity (d))
11299 return true;
11300 if (expand_vselect (d->target, d->op0, d->perm, nelt))
11301 return true;
11304 /* Try two operand selections. */
11305 if (expand_vselect_vconcat (d->target, d->op0, d->op1, d->perm, nelt))
11306 return true;
11308 /* Recognize interleave style patterns with reversed operands. */
11309 if (!d->one_operand_p)
11311 for (i = 0; i < nelt; ++i)
11313 unsigned e = d->perm[i];
11314 if (e >= nelt)
11315 e -= nelt;
11316 else
11317 e += nelt;
11318 perm2[i] = e;
11321 if (expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
11322 return true;
11325 if (expand_vec_perm_shrp (d))
11326 return true;
11328 /* ??? Look for deposit-like permutations where most of the result
11329 comes from one vector unchanged and the rest comes from a
11330 sequential hunk of the other vector. */
11332 return false;
11335 /* Pattern match broadcast permutations. */
11337 static bool
11338 expand_vec_perm_broadcast (struct expand_vec_perm_d *d)
11340 unsigned i, elt, nelt = d->nelt;
11341 unsigned char perm2[2];
11342 rtx temp;
11343 bool ok;
11345 if (!d->one_operand_p)
11346 return false;
11348 elt = d->perm[0];
11349 for (i = 1; i < nelt; ++i)
11350 if (d->perm[i] != elt)
11351 return false;
11353 switch (d->vmode)
11355 case V2SImode:
11356 case V2SFmode:
11357 /* Implementable by interleave. */
11358 perm2[0] = elt;
11359 perm2[1] = elt + 2;
11360 ok = expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, 2);
11361 gcc_assert (ok);
11362 break;
11364 case V8QImode:
11365 /* Implementable by extract + broadcast. */
11366 if (BYTES_BIG_ENDIAN)
11367 elt = 7 - elt;
11368 elt *= BITS_PER_UNIT;
11369 temp = gen_reg_rtx (DImode);
11370 emit_insn (gen_extzv (temp, gen_lowpart (DImode, d->op0),
11371 GEN_INT (8), GEN_INT (elt)));
11372 emit_insn (gen_mux1_brcst_qi (d->target, gen_lowpart (QImode, temp)));
11373 break;
11375 case V4HImode:
11376 /* Should have been matched directly by vec_select. */
11377 default:
11378 gcc_unreachable ();
11381 return true;
11384 /* A subroutine of ia64_expand_vec_perm_const_1. Try to simplify a
11385 two vector permutation into a single vector permutation by using
11386 an interleave operation to merge the vectors. */
11388 static bool
11389 expand_vec_perm_interleave_2 (struct expand_vec_perm_d *d)
11391 struct expand_vec_perm_d dremap, dfinal;
11392 unsigned char remap[2 * MAX_VECT_LEN];
11393 unsigned contents, i, nelt, nelt2;
11394 unsigned h0, h1, h2, h3;
11395 rtx seq;
11396 bool ok;
11398 if (d->one_operand_p)
11399 return false;
11401 nelt = d->nelt;
11402 nelt2 = nelt / 2;
11404 /* Examine from whence the elements come. */
11405 contents = 0;
11406 for (i = 0; i < nelt; ++i)
11407 contents |= 1u << d->perm[i];
11409 memset (remap, 0xff, sizeof (remap));
11410 dremap = *d;
11412 h0 = (1u << nelt2) - 1;
11413 h1 = h0 << nelt2;
11414 h2 = h0 << nelt;
11415 h3 = h0 << (nelt + nelt2);
11417 if ((contents & (h0 | h2)) == contents) /* punpck even halves */
11419 for (i = 0; i < nelt; ++i)
11421 unsigned which = i / 2 + (i & 1 ? nelt : 0);
11422 remap[which] = i;
11423 dremap.perm[i] = which;
11426 else if ((contents & (h1 | h3)) == contents) /* punpck odd halves */
11428 for (i = 0; i < nelt; ++i)
11430 unsigned which = i / 2 + nelt2 + (i & 1 ? nelt : 0);
11431 remap[which] = i;
11432 dremap.perm[i] = which;
11435 else if ((contents & 0x5555) == contents) /* mix even elements */
11437 for (i = 0; i < nelt; ++i)
11439 unsigned which = (i & ~1) + (i & 1 ? nelt : 0);
11440 remap[which] = i;
11441 dremap.perm[i] = which;
11444 else if ((contents & 0xaaaa) == contents) /* mix odd elements */
11446 for (i = 0; i < nelt; ++i)
11448 unsigned which = (i | 1) + (i & 1 ? nelt : 0);
11449 remap[which] = i;
11450 dremap.perm[i] = which;
11453 else if (floor_log2 (contents) - ctz_hwi (contents) < (int)nelt) /* shrp */
11455 unsigned shift = ctz_hwi (contents);
11456 for (i = 0; i < nelt; ++i)
11458 unsigned which = (i + shift) & (2 * nelt - 1);
11459 remap[which] = i;
11460 dremap.perm[i] = which;
11463 else
11464 return false;
11466 /* Use the remapping array set up above to move the elements from their
11467 swizzled locations into their final destinations. */
11468 dfinal = *d;
11469 for (i = 0; i < nelt; ++i)
11471 unsigned e = remap[d->perm[i]];
11472 gcc_assert (e < nelt);
11473 dfinal.perm[i] = e;
11475 dfinal.op0 = gen_reg_rtx (dfinal.vmode);
11476 dfinal.op1 = dfinal.op0;
11477 dfinal.one_operand_p = true;
11478 dremap.target = dfinal.op0;
11480 /* Test if the final remap can be done with a single insn. For V4HImode
11481 this *will* succeed. For V8QImode or V2SImode it may not. */
11482 start_sequence ();
11483 ok = expand_vec_perm_1 (&dfinal);
11484 seq = get_insns ();
11485 end_sequence ();
11486 if (!ok)
11487 return false;
11488 if (d->testing_p)
11489 return true;
11491 ok = expand_vec_perm_1 (&dremap);
11492 gcc_assert (ok);
11494 emit_insn (seq);
11495 return true;
11498 /* A subroutine of ia64_expand_vec_perm_const_1. Emit a full V4HImode
11499 constant permutation via two mux2 and a merge. */
11501 static bool
11502 expand_vec_perm_v4hi_5 (struct expand_vec_perm_d *d)
11504 unsigned char perm2[4];
11505 rtx rmask[4];
11506 unsigned i;
11507 rtx t0, t1, mask, x;
11508 bool ok;
11510 if (d->vmode != V4HImode || d->one_operand_p)
11511 return false;
11512 if (d->testing_p)
11513 return true;
11515 for (i = 0; i < 4; ++i)
11517 perm2[i] = d->perm[i] & 3;
11518 rmask[i] = (d->perm[i] & 4 ? const0_rtx : constm1_rtx);
11520 mask = gen_rtx_CONST_VECTOR (V4HImode, gen_rtvec_v (4, rmask));
11521 mask = force_reg (V4HImode, mask);
11523 t0 = gen_reg_rtx (V4HImode);
11524 t1 = gen_reg_rtx (V4HImode);
11526 ok = expand_vselect (t0, d->op0, perm2, 4);
11527 gcc_assert (ok);
11528 ok = expand_vselect (t1, d->op1, perm2, 4);
11529 gcc_assert (ok);
11531 x = gen_rtx_AND (V4HImode, mask, t0);
11532 emit_insn (gen_rtx_SET (VOIDmode, t0, x));
11534 x = gen_rtx_NOT (V4HImode, mask);
11535 x = gen_rtx_AND (V4HImode, x, t1);
11536 emit_insn (gen_rtx_SET (VOIDmode, t1, x));
11538 x = gen_rtx_IOR (V4HImode, t0, t1);
11539 emit_insn (gen_rtx_SET (VOIDmode, d->target, x));
11541 return true;
11544 /* The guts of ia64_expand_vec_perm_const, also used by the ok hook.
11545 With all of the interface bits taken care of, perform the expansion
11546 in D and return true on success. */
11548 static bool
11549 ia64_expand_vec_perm_const_1 (struct expand_vec_perm_d *d)
11551 if (expand_vec_perm_1 (d))
11552 return true;
11553 if (expand_vec_perm_broadcast (d))
11554 return true;
11555 if (expand_vec_perm_interleave_2 (d))
11556 return true;
11557 if (expand_vec_perm_v4hi_5 (d))
11558 return true;
11559 return false;
11562 bool
11563 ia64_expand_vec_perm_const (rtx operands[4])
11565 struct expand_vec_perm_d d;
11566 unsigned char perm[MAX_VECT_LEN];
11567 int i, nelt, which;
11568 rtx sel;
11570 d.target = operands[0];
11571 d.op0 = operands[1];
11572 d.op1 = operands[2];
11573 sel = operands[3];
11575 d.vmode = GET_MODE (d.target);
11576 gcc_assert (VECTOR_MODE_P (d.vmode));
11577 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
11578 d.testing_p = false;
11580 gcc_assert (GET_CODE (sel) == CONST_VECTOR);
11581 gcc_assert (XVECLEN (sel, 0) == nelt);
11582 gcc_checking_assert (sizeof (d.perm) == sizeof (perm));
11584 for (i = which = 0; i < nelt; ++i)
11586 rtx e = XVECEXP (sel, 0, i);
11587 int ei = INTVAL (e) & (2 * nelt - 1);
11589 which |= (ei < nelt ? 1 : 2);
11590 d.perm[i] = ei;
11591 perm[i] = ei;
11594 switch (which)
11596 default:
11597 gcc_unreachable();
11599 case 3:
11600 if (!rtx_equal_p (d.op0, d.op1))
11602 d.one_operand_p = false;
11603 break;
11606 /* The elements of PERM do not suggest that only the first operand
11607 is used, but both operands are identical. Allow easier matching
11608 of the permutation by folding the permutation into the single
11609 input vector. */
11610 for (i = 0; i < nelt; ++i)
11611 if (d.perm[i] >= nelt)
11612 d.perm[i] -= nelt;
11613 /* FALLTHRU */
11615 case 1:
11616 d.op1 = d.op0;
11617 d.one_operand_p = true;
11618 break;
11620 case 2:
11621 for (i = 0; i < nelt; ++i)
11622 d.perm[i] -= nelt;
11623 d.op0 = d.op1;
11624 d.one_operand_p = true;
11625 break;
11628 if (ia64_expand_vec_perm_const_1 (&d))
11629 return true;
11631 /* If the mask says both arguments are needed, but they are the same,
11632 the above tried to expand with one_operand_p true. If that didn't
11633 work, retry with one_operand_p false, as that's what we used in _ok. */
11634 if (which == 3 && d.one_operand_p)
11636 memcpy (d.perm, perm, sizeof (perm));
11637 d.one_operand_p = false;
11638 return ia64_expand_vec_perm_const_1 (&d);
11641 return false;
11644 /* Implement targetm.vectorize.vec_perm_const_ok. */
11646 static bool
11647 ia64_vectorize_vec_perm_const_ok (enum machine_mode vmode,
11648 const unsigned char *sel)
11650 struct expand_vec_perm_d d;
11651 unsigned int i, nelt, which;
11652 bool ret;
11654 d.vmode = vmode;
11655 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
11656 d.testing_p = true;
11658 /* Extract the values from the vector CST into the permutation
11659 array in D. */
11660 memcpy (d.perm, sel, nelt);
11661 for (i = which = 0; i < nelt; ++i)
11663 unsigned char e = d.perm[i];
11664 gcc_assert (e < 2 * nelt);
11665 which |= (e < nelt ? 1 : 2);
11668 /* For all elements from second vector, fold the elements to first. */
11669 if (which == 2)
11670 for (i = 0; i < nelt; ++i)
11671 d.perm[i] -= nelt;
11673 /* Check whether the mask can be applied to the vector type. */
11674 d.one_operand_p = (which != 3);
11676 /* Otherwise we have to go through the motions and see if we can
11677 figure out how to generate the requested permutation. */
11678 d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
11679 d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
11680 if (!d.one_operand_p)
11681 d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
11683 start_sequence ();
11684 ret = ia64_expand_vec_perm_const_1 (&d);
11685 end_sequence ();
11687 return ret;
11690 void
11691 ia64_expand_vec_setv2sf (rtx operands[3])
11693 struct expand_vec_perm_d d;
11694 unsigned int which;
11695 bool ok;
11697 d.target = operands[0];
11698 d.op0 = operands[0];
11699 d.op1 = gen_reg_rtx (V2SFmode);
11700 d.vmode = V2SFmode;
11701 d.nelt = 2;
11702 d.one_operand_p = false;
11703 d.testing_p = false;
11705 which = INTVAL (operands[2]);
11706 gcc_assert (which <= 1);
11707 d.perm[0] = 1 - which;
11708 d.perm[1] = which + 2;
11710 emit_insn (gen_fpack (d.op1, operands[1], CONST0_RTX (SFmode)));
11712 ok = ia64_expand_vec_perm_const_1 (&d);
11713 gcc_assert (ok);
11716 void
11717 ia64_expand_vec_perm_even_odd (rtx target, rtx op0, rtx op1, int odd)
11719 struct expand_vec_perm_d d;
11720 enum machine_mode vmode = GET_MODE (target);
11721 unsigned int i, nelt = GET_MODE_NUNITS (vmode);
11722 bool ok;
11724 d.target = target;
11725 d.op0 = op0;
11726 d.op1 = op1;
11727 d.vmode = vmode;
11728 d.nelt = nelt;
11729 d.one_operand_p = false;
11730 d.testing_p = false;
11732 for (i = 0; i < nelt; ++i)
11733 d.perm[i] = i * 2 + odd;
11735 ok = ia64_expand_vec_perm_const_1 (&d);
11736 gcc_assert (ok);
11739 #include "gt-ia64.h"