This commit was manufactured by cvs2svn to create branch
[official-gcc.git] / gcc / config / ia64 / ia64.c
blob13a19a87449980a12f8e56b9626d2af54142a247
1 /* Definitions of target machine for GNU compiler.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Contributed by James E. Wilson <wilson@cygnus.com> and
5 David Mosberger <davidm@hpl.hp.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "real.h"
33 #include "insn-config.h"
34 #include "conditions.h"
35 #include "output.h"
36 #include "insn-attr.h"
37 #include "flags.h"
38 #include "recog.h"
39 #include "expr.h"
40 #include "optabs.h"
41 #include "except.h"
42 #include "function.h"
43 #include "ggc.h"
44 #include "basic-block.h"
45 #include "toplev.h"
46 #include "sched-int.h"
47 #include "timevar.h"
48 #include "target.h"
49 #include "target-def.h"
50 #include "tm_p.h"
51 #include "hashtab.h"
52 #include "langhooks.h"
53 #include "cfglayout.h"
55 /* This is used for communication between ASM_OUTPUT_LABEL and
56 ASM_OUTPUT_LABELREF. */
57 int ia64_asm_output_label = 0;
59 /* Define the information needed to generate branch and scc insns. This is
60 stored from the compare operation. */
61 struct rtx_def * ia64_compare_op0;
62 struct rtx_def * ia64_compare_op1;
64 /* Register names for ia64_expand_prologue. */
65 static const char * const ia64_reg_numbers[96] =
66 { "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
67 "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
68 "r48", "r49", "r50", "r51", "r52", "r53", "r54", "r55",
69 "r56", "r57", "r58", "r59", "r60", "r61", "r62", "r63",
70 "r64", "r65", "r66", "r67", "r68", "r69", "r70", "r71",
71 "r72", "r73", "r74", "r75", "r76", "r77", "r78", "r79",
72 "r80", "r81", "r82", "r83", "r84", "r85", "r86", "r87",
73 "r88", "r89", "r90", "r91", "r92", "r93", "r94", "r95",
74 "r96", "r97", "r98", "r99", "r100","r101","r102","r103",
75 "r104","r105","r106","r107","r108","r109","r110","r111",
76 "r112","r113","r114","r115","r116","r117","r118","r119",
77 "r120","r121","r122","r123","r124","r125","r126","r127"};
79 /* ??? These strings could be shared with REGISTER_NAMES. */
80 static const char * const ia64_input_reg_names[8] =
81 { "in0", "in1", "in2", "in3", "in4", "in5", "in6", "in7" };
83 /* ??? These strings could be shared with REGISTER_NAMES. */
84 static const char * const ia64_local_reg_names[80] =
85 { "loc0", "loc1", "loc2", "loc3", "loc4", "loc5", "loc6", "loc7",
86 "loc8", "loc9", "loc10","loc11","loc12","loc13","loc14","loc15",
87 "loc16","loc17","loc18","loc19","loc20","loc21","loc22","loc23",
88 "loc24","loc25","loc26","loc27","loc28","loc29","loc30","loc31",
89 "loc32","loc33","loc34","loc35","loc36","loc37","loc38","loc39",
90 "loc40","loc41","loc42","loc43","loc44","loc45","loc46","loc47",
91 "loc48","loc49","loc50","loc51","loc52","loc53","loc54","loc55",
92 "loc56","loc57","loc58","loc59","loc60","loc61","loc62","loc63",
93 "loc64","loc65","loc66","loc67","loc68","loc69","loc70","loc71",
94 "loc72","loc73","loc74","loc75","loc76","loc77","loc78","loc79" };
96 /* ??? These strings could be shared with REGISTER_NAMES. */
97 static const char * const ia64_output_reg_names[8] =
98 { "out0", "out1", "out2", "out3", "out4", "out5", "out6", "out7" };
100 /* String used with the -mfixed-range= option. */
101 const char *ia64_fixed_range_string;
103 /* Determines whether we use adds, addl, or movl to generate our
104 TLS immediate offsets. */
105 int ia64_tls_size = 22;
107 /* String used with the -mtls-size= option. */
108 const char *ia64_tls_size_string;
110 /* Which cpu are we scheduling for. */
111 enum processor_type ia64_tune;
113 /* String used with the -tune= option. */
114 const char *ia64_tune_string;
116 /* Determines whether we run our final scheduling pass or not. We always
117 avoid the normal second scheduling pass. */
118 static int ia64_flag_schedule_insns2;
120 /* Variables which are this size or smaller are put in the sdata/sbss
121 sections. */
123 unsigned int ia64_section_threshold;
125 /* The following variable is used by the DFA insn scheduler. The value is
126 TRUE if we do insn bundling instead of insn scheduling. */
127 int bundling_p = 0;
129 /* Structure to be filled in by ia64_compute_frame_size with register
130 save masks and offsets for the current function. */
132 struct ia64_frame_info
134 HOST_WIDE_INT total_size; /* size of the stack frame, not including
135 the caller's scratch area. */
136 HOST_WIDE_INT spill_cfa_off; /* top of the reg spill area from the cfa. */
137 HOST_WIDE_INT spill_size; /* size of the gr/br/fr spill area. */
138 HOST_WIDE_INT extra_spill_size; /* size of spill area for others. */
139 HARD_REG_SET mask; /* mask of saved registers. */
140 unsigned int gr_used_mask; /* mask of registers in use as gr spill
141 registers or long-term scratches. */
142 int n_spilled; /* number of spilled registers. */
143 int reg_fp; /* register for fp. */
144 int reg_save_b0; /* save register for b0. */
145 int reg_save_pr; /* save register for prs. */
146 int reg_save_ar_pfs; /* save register for ar.pfs. */
147 int reg_save_ar_unat; /* save register for ar.unat. */
148 int reg_save_ar_lc; /* save register for ar.lc. */
149 int reg_save_gp; /* save register for gp. */
150 int n_input_regs; /* number of input registers used. */
151 int n_local_regs; /* number of local registers used. */
152 int n_output_regs; /* number of output registers used. */
153 int n_rotate_regs; /* number of rotating registers used. */
155 char need_regstk; /* true if a .regstk directive needed. */
156 char initialized; /* true if the data is finalized. */
159 /* Current frame information calculated by ia64_compute_frame_size. */
160 static struct ia64_frame_info current_frame_info;
162 static int ia64_use_dfa_pipeline_interface (void);
163 static int ia64_first_cycle_multipass_dfa_lookahead (void);
164 static void ia64_dependencies_evaluation_hook (rtx, rtx);
165 static void ia64_init_dfa_pre_cycle_insn (void);
166 static rtx ia64_dfa_pre_cycle_insn (void);
167 static int ia64_first_cycle_multipass_dfa_lookahead_guard (rtx);
168 static int ia64_dfa_new_cycle (FILE *, int, rtx, int, int, int *);
169 static rtx gen_tls_get_addr (void);
170 static rtx gen_thread_pointer (void);
171 static rtx ia64_expand_tls_address (enum tls_model, rtx, rtx);
172 static int find_gr_spill (int);
173 static int next_scratch_gr_reg (void);
174 static void mark_reg_gr_used_mask (rtx, void *);
175 static void ia64_compute_frame_size (HOST_WIDE_INT);
176 static void setup_spill_pointers (int, rtx, HOST_WIDE_INT);
177 static void finish_spill_pointers (void);
178 static rtx spill_restore_mem (rtx, HOST_WIDE_INT);
179 static void do_spill (rtx (*)(rtx, rtx, rtx), rtx, HOST_WIDE_INT, rtx);
180 static void do_restore (rtx (*)(rtx, rtx, rtx), rtx, HOST_WIDE_INT);
181 static rtx gen_movdi_x (rtx, rtx, rtx);
182 static rtx gen_fr_spill_x (rtx, rtx, rtx);
183 static rtx gen_fr_restore_x (rtx, rtx, rtx);
185 static enum machine_mode hfa_element_mode (tree, int);
186 static bool ia64_function_ok_for_sibcall (tree, tree);
187 static bool ia64_rtx_costs (rtx, int, int, int *);
188 static void fix_range (const char *);
189 static struct machine_function * ia64_init_machine_status (void);
190 static void emit_insn_group_barriers (FILE *);
191 static void emit_all_insn_group_barriers (FILE *);
192 static void final_emit_insn_group_barriers (FILE *);
193 static void emit_predicate_relation_info (void);
194 static void ia64_reorg (void);
195 static bool ia64_in_small_data_p (tree);
196 static void process_epilogue (void);
197 static int process_set (FILE *, rtx);
199 static rtx ia64_expand_fetch_and_op (optab, enum machine_mode, tree, rtx);
200 static rtx ia64_expand_op_and_fetch (optab, enum machine_mode, tree, rtx);
201 static rtx ia64_expand_compare_and_swap (enum machine_mode, enum machine_mode,
202 int, tree, rtx);
203 static rtx ia64_expand_lock_test_and_set (enum machine_mode, tree, rtx);
204 static rtx ia64_expand_lock_release (enum machine_mode, tree, rtx);
205 static bool ia64_assemble_integer (rtx, unsigned int, int);
206 static void ia64_output_function_prologue (FILE *, HOST_WIDE_INT);
207 static void ia64_output_function_epilogue (FILE *, HOST_WIDE_INT);
208 static void ia64_output_function_end_prologue (FILE *);
210 static int ia64_issue_rate (void);
211 static int ia64_adjust_cost (rtx, rtx, rtx, int);
212 static void ia64_sched_init (FILE *, int, int);
213 static void ia64_sched_finish (FILE *, int);
214 static int ia64_dfa_sched_reorder (FILE *, int, rtx *, int *, int, int);
215 static int ia64_sched_reorder (FILE *, int, rtx *, int *, int);
216 static int ia64_sched_reorder2 (FILE *, int, rtx *, int *, int);
217 static int ia64_variable_issue (FILE *, int, rtx, int);
219 static struct bundle_state *get_free_bundle_state (void);
220 static void free_bundle_state (struct bundle_state *);
221 static void initiate_bundle_states (void);
222 static void finish_bundle_states (void);
223 static unsigned bundle_state_hash (const void *);
224 static int bundle_state_eq_p (const void *, const void *);
225 static int insert_bundle_state (struct bundle_state *);
226 static void initiate_bundle_state_table (void);
227 static void finish_bundle_state_table (void);
228 static int try_issue_nops (struct bundle_state *, int);
229 static int try_issue_insn (struct bundle_state *, rtx);
230 static void issue_nops_and_insn (struct bundle_state *, int, rtx, int, int);
231 static int get_max_pos (state_t);
232 static int get_template (state_t, int);
234 static rtx get_next_important_insn (rtx, rtx);
235 static void bundling (FILE *, int, rtx, rtx);
237 static void ia64_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
238 HOST_WIDE_INT, tree);
239 static void ia64_file_start (void);
241 static void ia64_select_rtx_section (enum machine_mode, rtx,
242 unsigned HOST_WIDE_INT);
243 static void ia64_rwreloc_select_section (tree, int, unsigned HOST_WIDE_INT)
244 ATTRIBUTE_UNUSED;
245 static void ia64_rwreloc_unique_section (tree, int)
246 ATTRIBUTE_UNUSED;
247 static void ia64_rwreloc_select_rtx_section (enum machine_mode, rtx,
248 unsigned HOST_WIDE_INT)
249 ATTRIBUTE_UNUSED;
250 static unsigned int ia64_rwreloc_section_type_flags (tree, const char *, int)
251 ATTRIBUTE_UNUSED;
253 static void ia64_hpux_add_extern_decl (tree decl)
254 ATTRIBUTE_UNUSED;
255 static void ia64_hpux_file_end (void)
256 ATTRIBUTE_UNUSED;
257 static void ia64_hpux_init_libfuncs (void)
258 ATTRIBUTE_UNUSED;
259 static void ia64_vms_init_libfuncs (void)
260 ATTRIBUTE_UNUSED;
262 static tree ia64_handle_model_attribute (tree *, tree, tree, int, bool *);
263 static void ia64_encode_section_info (tree, rtx, int);
264 static rtx ia64_struct_value_rtx (tree, int);
267 /* Table of valid machine attributes. */
268 static const struct attribute_spec ia64_attribute_table[] =
270 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
271 { "syscall_linkage", 0, 0, false, true, true, NULL },
272 { "model", 1, 1, true, false, false, ia64_handle_model_attribute },
273 { NULL, 0, 0, false, false, false, NULL }
276 /* Initialize the GCC target structure. */
277 #undef TARGET_ATTRIBUTE_TABLE
278 #define TARGET_ATTRIBUTE_TABLE ia64_attribute_table
280 #undef TARGET_INIT_BUILTINS
281 #define TARGET_INIT_BUILTINS ia64_init_builtins
283 #undef TARGET_EXPAND_BUILTIN
284 #define TARGET_EXPAND_BUILTIN ia64_expand_builtin
286 #undef TARGET_ASM_BYTE_OP
287 #define TARGET_ASM_BYTE_OP "\tdata1\t"
288 #undef TARGET_ASM_ALIGNED_HI_OP
289 #define TARGET_ASM_ALIGNED_HI_OP "\tdata2\t"
290 #undef TARGET_ASM_ALIGNED_SI_OP
291 #define TARGET_ASM_ALIGNED_SI_OP "\tdata4\t"
292 #undef TARGET_ASM_ALIGNED_DI_OP
293 #define TARGET_ASM_ALIGNED_DI_OP "\tdata8\t"
294 #undef TARGET_ASM_UNALIGNED_HI_OP
295 #define TARGET_ASM_UNALIGNED_HI_OP "\tdata2.ua\t"
296 #undef TARGET_ASM_UNALIGNED_SI_OP
297 #define TARGET_ASM_UNALIGNED_SI_OP "\tdata4.ua\t"
298 #undef TARGET_ASM_UNALIGNED_DI_OP
299 #define TARGET_ASM_UNALIGNED_DI_OP "\tdata8.ua\t"
300 #undef TARGET_ASM_INTEGER
301 #define TARGET_ASM_INTEGER ia64_assemble_integer
303 #undef TARGET_ASM_FUNCTION_PROLOGUE
304 #define TARGET_ASM_FUNCTION_PROLOGUE ia64_output_function_prologue
305 #undef TARGET_ASM_FUNCTION_END_PROLOGUE
306 #define TARGET_ASM_FUNCTION_END_PROLOGUE ia64_output_function_end_prologue
307 #undef TARGET_ASM_FUNCTION_EPILOGUE
308 #define TARGET_ASM_FUNCTION_EPILOGUE ia64_output_function_epilogue
310 #undef TARGET_IN_SMALL_DATA_P
311 #define TARGET_IN_SMALL_DATA_P ia64_in_small_data_p
313 #undef TARGET_SCHED_ADJUST_COST
314 #define TARGET_SCHED_ADJUST_COST ia64_adjust_cost
315 #undef TARGET_SCHED_ISSUE_RATE
316 #define TARGET_SCHED_ISSUE_RATE ia64_issue_rate
317 #undef TARGET_SCHED_VARIABLE_ISSUE
318 #define TARGET_SCHED_VARIABLE_ISSUE ia64_variable_issue
319 #undef TARGET_SCHED_INIT
320 #define TARGET_SCHED_INIT ia64_sched_init
321 #undef TARGET_SCHED_FINISH
322 #define TARGET_SCHED_FINISH ia64_sched_finish
323 #undef TARGET_SCHED_REORDER
324 #define TARGET_SCHED_REORDER ia64_sched_reorder
325 #undef TARGET_SCHED_REORDER2
326 #define TARGET_SCHED_REORDER2 ia64_sched_reorder2
328 #undef TARGET_SCHED_DEPENDENCIES_EVALUATION_HOOK
329 #define TARGET_SCHED_DEPENDENCIES_EVALUATION_HOOK ia64_dependencies_evaluation_hook
331 #undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE
332 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE ia64_use_dfa_pipeline_interface
334 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
335 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD ia64_first_cycle_multipass_dfa_lookahead
337 #undef TARGET_SCHED_INIT_DFA_PRE_CYCLE_INSN
338 #define TARGET_SCHED_INIT_DFA_PRE_CYCLE_INSN ia64_init_dfa_pre_cycle_insn
339 #undef TARGET_SCHED_DFA_PRE_CYCLE_INSN
340 #define TARGET_SCHED_DFA_PRE_CYCLE_INSN ia64_dfa_pre_cycle_insn
342 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
343 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD\
344 ia64_first_cycle_multipass_dfa_lookahead_guard
346 #undef TARGET_SCHED_DFA_NEW_CYCLE
347 #define TARGET_SCHED_DFA_NEW_CYCLE ia64_dfa_new_cycle
349 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
350 #define TARGET_FUNCTION_OK_FOR_SIBCALL ia64_function_ok_for_sibcall
352 #undef TARGET_ASM_OUTPUT_MI_THUNK
353 #define TARGET_ASM_OUTPUT_MI_THUNK ia64_output_mi_thunk
354 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
355 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
357 #undef TARGET_ASM_FILE_START
358 #define TARGET_ASM_FILE_START ia64_file_start
360 #undef TARGET_RTX_COSTS
361 #define TARGET_RTX_COSTS ia64_rtx_costs
362 #undef TARGET_ADDRESS_COST
363 #define TARGET_ADDRESS_COST hook_int_rtx_0
365 #undef TARGET_MACHINE_DEPENDENT_REORG
366 #define TARGET_MACHINE_DEPENDENT_REORG ia64_reorg
368 #undef TARGET_ENCODE_SECTION_INFO
369 #define TARGET_ENCODE_SECTION_INFO ia64_encode_section_info
371 #undef TARGET_STRUCT_VALUE_RTX
372 #define TARGET_STRUCT_VALUE_RTX ia64_struct_value_rtx
374 struct gcc_target targetm = TARGET_INITIALIZER;
376 /* Return 1 if OP is a valid operand for the MEM of a CALL insn. */
379 call_operand (rtx op, enum machine_mode mode)
381 if (mode != GET_MODE (op) && mode != VOIDmode)
382 return 0;
384 return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == REG
385 || (GET_CODE (op) == SUBREG && GET_CODE (XEXP (op, 0)) == REG));
388 /* Return 1 if OP refers to a symbol in the sdata section. */
391 sdata_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
393 switch (GET_CODE (op))
395 case CONST:
396 if (GET_CODE (XEXP (op, 0)) != PLUS
397 || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF)
398 break;
399 op = XEXP (XEXP (op, 0), 0);
400 /* FALLTHRU */
402 case SYMBOL_REF:
403 if (CONSTANT_POOL_ADDRESS_P (op))
404 return GET_MODE_SIZE (get_pool_mode (op)) <= ia64_section_threshold;
405 else
406 return SYMBOL_REF_LOCAL_P (op) && SYMBOL_REF_SMALL_P (op);
408 default:
409 break;
412 return 0;
416 small_addr_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
418 return SYMBOL_REF_SMALL_ADDR_P (op);
421 /* Return 1 if OP refers to a symbol, and is appropriate for a GOT load. */
424 got_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
426 switch (GET_CODE (op))
428 case CONST:
429 op = XEXP (op, 0);
430 if (GET_CODE (op) != PLUS)
431 return 0;
432 if (GET_CODE (XEXP (op, 0)) != SYMBOL_REF)
433 return 0;
434 op = XEXP (op, 1);
435 if (GET_CODE (op) != CONST_INT)
436 return 0;
438 return 1;
440 /* Ok if we're not using GOT entries at all. */
441 if (TARGET_NO_PIC || TARGET_AUTO_PIC)
442 return 1;
444 /* "Ok" while emitting rtl, since otherwise we won't be provided
445 with the entire offset during emission, which makes it very
446 hard to split the offset into high and low parts. */
447 if (rtx_equal_function_value_matters)
448 return 1;
450 /* Force the low 14 bits of the constant to zero so that we do not
451 use up so many GOT entries. */
452 return (INTVAL (op) & 0x3fff) == 0;
454 case SYMBOL_REF:
455 if (SYMBOL_REF_SMALL_ADDR_P (op))
456 return 0;
457 case LABEL_REF:
458 return 1;
460 default:
461 break;
463 return 0;
466 /* Return 1 if OP refers to a symbol. */
469 symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
471 switch (GET_CODE (op))
473 case CONST:
474 case SYMBOL_REF:
475 case LABEL_REF:
476 return 1;
478 default:
479 break;
481 return 0;
484 /* Return tls_model if OP refers to a TLS symbol. */
487 tls_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
489 if (GET_CODE (op) != SYMBOL_REF)
490 return 0;
491 return SYMBOL_REF_TLS_MODEL (op);
495 /* Return 1 if OP refers to a function. */
498 function_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
500 if (GET_CODE (op) == SYMBOL_REF && SYMBOL_REF_FUNCTION_P (op))
501 return 1;
502 else
503 return 0;
506 /* Return 1 if OP is setjmp or a similar function. */
508 /* ??? This is an unsatisfying solution. Should rethink. */
511 setjmp_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
513 const char *name;
514 int retval = 0;
516 if (GET_CODE (op) != SYMBOL_REF)
517 return 0;
519 name = XSTR (op, 0);
521 /* The following code is borrowed from special_function_p in calls.c. */
523 /* Disregard prefix _, __ or __x. */
524 if (name[0] == '_')
526 if (name[1] == '_' && name[2] == 'x')
527 name += 3;
528 else if (name[1] == '_')
529 name += 2;
530 else
531 name += 1;
534 if (name[0] == 's')
536 retval
537 = ((name[1] == 'e'
538 && (! strcmp (name, "setjmp")
539 || ! strcmp (name, "setjmp_syscall")))
540 || (name[1] == 'i'
541 && ! strcmp (name, "sigsetjmp"))
542 || (name[1] == 'a'
543 && ! strcmp (name, "savectx")));
545 else if ((name[0] == 'q' && name[1] == 's'
546 && ! strcmp (name, "qsetjmp"))
547 || (name[0] == 'v' && name[1] == 'f'
548 && ! strcmp (name, "vfork")))
549 retval = 1;
551 return retval;
554 /* Return 1 if OP is a general operand, excluding tls symbolic operands. */
557 move_operand (rtx op, enum machine_mode mode)
559 return general_operand (op, mode) && !tls_symbolic_operand (op, mode);
562 /* Return 1 if OP is a register operand that is (or could be) a GR reg. */
565 gr_register_operand (rtx op, enum machine_mode mode)
567 if (! register_operand (op, mode))
568 return 0;
569 if (GET_CODE (op) == SUBREG)
570 op = SUBREG_REG (op);
571 if (GET_CODE (op) == REG)
573 unsigned int regno = REGNO (op);
574 if (regno < FIRST_PSEUDO_REGISTER)
575 return GENERAL_REGNO_P (regno);
577 return 1;
580 /* Return 1 if OP is a register operand that is (or could be) an FR reg. */
583 fr_register_operand (rtx op, enum machine_mode mode)
585 if (! register_operand (op, mode))
586 return 0;
587 if (GET_CODE (op) == SUBREG)
588 op = SUBREG_REG (op);
589 if (GET_CODE (op) == REG)
591 unsigned int regno = REGNO (op);
592 if (regno < FIRST_PSEUDO_REGISTER)
593 return FR_REGNO_P (regno);
595 return 1;
598 /* Return 1 if OP is a register operand that is (or could be) a GR/FR reg. */
601 grfr_register_operand (rtx op, enum machine_mode mode)
603 if (! register_operand (op, mode))
604 return 0;
605 if (GET_CODE (op) == SUBREG)
606 op = SUBREG_REG (op);
607 if (GET_CODE (op) == REG)
609 unsigned int regno = REGNO (op);
610 if (regno < FIRST_PSEUDO_REGISTER)
611 return GENERAL_REGNO_P (regno) || FR_REGNO_P (regno);
613 return 1;
616 /* Return 1 if OP is a nonimmediate operand that is (or could be) a GR reg. */
619 gr_nonimmediate_operand (rtx op, enum machine_mode mode)
621 if (! nonimmediate_operand (op, mode))
622 return 0;
623 if (GET_CODE (op) == SUBREG)
624 op = SUBREG_REG (op);
625 if (GET_CODE (op) == REG)
627 unsigned int regno = REGNO (op);
628 if (regno < FIRST_PSEUDO_REGISTER)
629 return GENERAL_REGNO_P (regno);
631 return 1;
634 /* Return 1 if OP is a nonimmediate operand that is (or could be) a FR reg. */
637 fr_nonimmediate_operand (rtx op, enum machine_mode mode)
639 if (! nonimmediate_operand (op, mode))
640 return 0;
641 if (GET_CODE (op) == SUBREG)
642 op = SUBREG_REG (op);
643 if (GET_CODE (op) == REG)
645 unsigned int regno = REGNO (op);
646 if (regno < FIRST_PSEUDO_REGISTER)
647 return FR_REGNO_P (regno);
649 return 1;
652 /* Return 1 if OP is a nonimmediate operand that is a GR/FR reg. */
655 grfr_nonimmediate_operand (rtx op, enum machine_mode mode)
657 if (! nonimmediate_operand (op, mode))
658 return 0;
659 if (GET_CODE (op) == SUBREG)
660 op = SUBREG_REG (op);
661 if (GET_CODE (op) == REG)
663 unsigned int regno = REGNO (op);
664 if (regno < FIRST_PSEUDO_REGISTER)
665 return GENERAL_REGNO_P (regno) || FR_REGNO_P (regno);
667 return 1;
670 /* Return 1 if OP is a GR register operand, or zero. */
673 gr_reg_or_0_operand (rtx op, enum machine_mode mode)
675 return (op == const0_rtx || gr_register_operand (op, mode));
678 /* Return 1 if OP is a GR register operand, or a 5 bit immediate operand. */
681 gr_reg_or_5bit_operand (rtx op, enum machine_mode mode)
683 return ((GET_CODE (op) == CONST_INT && INTVAL (op) >= 0 && INTVAL (op) < 32)
684 || GET_CODE (op) == CONSTANT_P_RTX
685 || gr_register_operand (op, mode));
688 /* Return 1 if OP is a GR register operand, or a 6 bit immediate operand. */
691 gr_reg_or_6bit_operand (rtx op, enum machine_mode mode)
693 return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_M (INTVAL (op)))
694 || GET_CODE (op) == CONSTANT_P_RTX
695 || gr_register_operand (op, mode));
698 /* Return 1 if OP is a GR register operand, or an 8 bit immediate operand. */
701 gr_reg_or_8bit_operand (rtx op, enum machine_mode mode)
703 return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_K (INTVAL (op)))
704 || GET_CODE (op) == CONSTANT_P_RTX
705 || gr_register_operand (op, mode));
708 /* Return 1 if OP is a GR/FR register operand, or an 8 bit immediate. */
711 grfr_reg_or_8bit_operand (rtx op, enum machine_mode mode)
713 return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_K (INTVAL (op)))
714 || GET_CODE (op) == CONSTANT_P_RTX
715 || grfr_register_operand (op, mode));
718 /* Return 1 if OP is a register operand, or an 8 bit adjusted immediate
719 operand. */
722 gr_reg_or_8bit_adjusted_operand (rtx op, enum machine_mode mode)
724 return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_L (INTVAL (op)))
725 || GET_CODE (op) == CONSTANT_P_RTX
726 || gr_register_operand (op, mode));
729 /* Return 1 if OP is a register operand, or is valid for both an 8 bit
730 immediate and an 8 bit adjusted immediate operand. This is necessary
731 because when we emit a compare, we don't know what the condition will be,
732 so we need the union of the immediates accepted by GT and LT. */
735 gr_reg_or_8bit_and_adjusted_operand (rtx op, enum machine_mode mode)
737 return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_K (INTVAL (op))
738 && CONST_OK_FOR_L (INTVAL (op)))
739 || GET_CODE (op) == CONSTANT_P_RTX
740 || gr_register_operand (op, mode));
743 /* Return 1 if OP is a register operand, or a 14 bit immediate operand. */
746 gr_reg_or_14bit_operand (rtx op, enum machine_mode mode)
748 return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_I (INTVAL (op)))
749 || GET_CODE (op) == CONSTANT_P_RTX
750 || gr_register_operand (op, mode));
753 /* Return 1 if OP is a register operand, or a 22 bit immediate operand. */
756 gr_reg_or_22bit_operand (rtx op, enum machine_mode mode)
758 return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_J (INTVAL (op)))
759 || GET_CODE (op) == CONSTANT_P_RTX
760 || gr_register_operand (op, mode));
763 /* Return 1 if OP is a 6 bit immediate operand. */
766 shift_count_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
768 return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_M (INTVAL (op)))
769 || GET_CODE (op) == CONSTANT_P_RTX);
772 /* Return 1 if OP is a 5 bit immediate operand. */
775 shift_32bit_count_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
777 return ((GET_CODE (op) == CONST_INT
778 && (INTVAL (op) >= 0 && INTVAL (op) < 32))
779 || GET_CODE (op) == CONSTANT_P_RTX);
782 /* Return 1 if OP is a 2, 4, 8, or 16 immediate operand. */
785 shladd_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
787 return (GET_CODE (op) == CONST_INT
788 && (INTVAL (op) == 2 || INTVAL (op) == 4
789 || INTVAL (op) == 8 || INTVAL (op) == 16));
792 /* Return 1 if OP is a -16, -8, -4, -1, 1, 4, 8, or 16 immediate operand. */
795 fetchadd_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
797 return (GET_CODE (op) == CONST_INT
798 && (INTVAL (op) == -16 || INTVAL (op) == -8 ||
799 INTVAL (op) == -4 || INTVAL (op) == -1 ||
800 INTVAL (op) == 1 || INTVAL (op) == 4 ||
801 INTVAL (op) == 8 || INTVAL (op) == 16));
804 /* Return 1 if OP is a floating-point constant zero, one, or a register. */
807 fr_reg_or_fp01_operand (rtx op, enum machine_mode mode)
809 return ((GET_CODE (op) == CONST_DOUBLE && CONST_DOUBLE_OK_FOR_G (op))
810 || fr_register_operand (op, mode));
813 /* Like nonimmediate_operand, but don't allow MEMs that try to use a
814 POST_MODIFY with a REG as displacement. */
817 destination_operand (rtx op, enum machine_mode mode)
819 if (! nonimmediate_operand (op, mode))
820 return 0;
821 if (GET_CODE (op) == MEM
822 && GET_CODE (XEXP (op, 0)) == POST_MODIFY
823 && GET_CODE (XEXP (XEXP (XEXP (op, 0), 1), 1)) == REG)
824 return 0;
825 return 1;
828 /* Like memory_operand, but don't allow post-increments. */
831 not_postinc_memory_operand (rtx op, enum machine_mode mode)
833 return (memory_operand (op, mode)
834 && GET_RTX_CLASS (GET_CODE (XEXP (op, 0))) != 'a');
837 /* Return 1 if this is a comparison operator, which accepts a normal 8-bit
838 signed immediate operand. */
841 normal_comparison_operator (register rtx op, enum machine_mode mode)
843 enum rtx_code code = GET_CODE (op);
844 return ((mode == VOIDmode || GET_MODE (op) == mode)
845 && (code == EQ || code == NE
846 || code == GT || code == LE || code == GTU || code == LEU));
849 /* Return 1 if this is a comparison operator, which accepts an adjusted 8-bit
850 signed immediate operand. */
853 adjusted_comparison_operator (register rtx op, enum machine_mode mode)
855 enum rtx_code code = GET_CODE (op);
856 return ((mode == VOIDmode || GET_MODE (op) == mode)
857 && (code == LT || code == GE || code == LTU || code == GEU));
860 /* Return 1 if this is a signed inequality operator. */
863 signed_inequality_operator (register rtx op, enum machine_mode mode)
865 enum rtx_code code = GET_CODE (op);
866 return ((mode == VOIDmode || GET_MODE (op) == mode)
867 && (code == GE || code == GT
868 || code == LE || code == LT));
871 /* Return 1 if this operator is valid for predication. */
874 predicate_operator (register rtx op, enum machine_mode mode)
876 enum rtx_code code = GET_CODE (op);
877 return ((GET_MODE (op) == mode || mode == VOIDmode)
878 && (code == EQ || code == NE));
881 /* Return 1 if this operator can be used in a conditional operation. */
884 condop_operator (register rtx op, enum machine_mode mode)
886 enum rtx_code code = GET_CODE (op);
887 return ((GET_MODE (op) == mode || mode == VOIDmode)
888 && (code == PLUS || code == MINUS || code == AND
889 || code == IOR || code == XOR));
892 /* Return 1 if this is the ar.lc register. */
895 ar_lc_reg_operand (register rtx op, enum machine_mode mode)
897 return (GET_MODE (op) == DImode
898 && (mode == DImode || mode == VOIDmode)
899 && GET_CODE (op) == REG
900 && REGNO (op) == AR_LC_REGNUM);
903 /* Return 1 if this is the ar.ccv register. */
906 ar_ccv_reg_operand (register rtx op, enum machine_mode mode)
908 return ((GET_MODE (op) == mode || mode == VOIDmode)
909 && GET_CODE (op) == REG
910 && REGNO (op) == AR_CCV_REGNUM);
913 /* Return 1 if this is the ar.pfs register. */
916 ar_pfs_reg_operand (register rtx op, enum machine_mode mode)
918 return ((GET_MODE (op) == mode || mode == VOIDmode)
919 && GET_CODE (op) == REG
920 && REGNO (op) == AR_PFS_REGNUM);
923 /* Like general_operand, but don't allow (mem (addressof)). */
926 general_xfmode_operand (rtx op, enum machine_mode mode)
928 if (! general_operand (op, mode))
929 return 0;
930 if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == ADDRESSOF)
931 return 0;
932 return 1;
935 /* Similarly. */
938 destination_xfmode_operand (rtx op, enum machine_mode mode)
940 if (! destination_operand (op, mode))
941 return 0;
942 if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == ADDRESSOF)
943 return 0;
944 return 1;
947 /* Similarly. */
950 xfreg_or_fp01_operand (rtx op, enum machine_mode mode)
952 if (GET_CODE (op) == SUBREG)
953 return 0;
954 return fr_reg_or_fp01_operand (op, mode);
957 /* Return 1 if OP is valid as a base register in a reg + offset address. */
960 basereg_operand (rtx op, enum machine_mode mode)
962 /* ??? Should I copy the flag_omit_frame_pointer and cse_not_expected
963 checks from pa.c basereg_operand as well? Seems to be OK without them
964 in test runs. */
966 return (register_operand (op, mode) &&
967 REG_POINTER ((GET_CODE (op) == SUBREG) ? SUBREG_REG (op) : op));
970 typedef enum
972 ADDR_AREA_NORMAL, /* normal address area */
973 ADDR_AREA_SMALL /* addressable by "addl" (-2MB < addr < 2MB) */
975 ia64_addr_area;
977 static GTY(()) tree small_ident1;
978 static GTY(()) tree small_ident2;
980 static void
981 init_idents (void)
983 if (small_ident1 == 0)
985 small_ident1 = get_identifier ("small");
986 small_ident2 = get_identifier ("__small__");
990 /* Retrieve the address area that has been chosen for the given decl. */
992 static ia64_addr_area
993 ia64_get_addr_area (tree decl)
995 tree model_attr;
997 model_attr = lookup_attribute ("model", DECL_ATTRIBUTES (decl));
998 if (model_attr)
1000 tree id;
1002 init_idents ();
1003 id = TREE_VALUE (TREE_VALUE (model_attr));
1004 if (id == small_ident1 || id == small_ident2)
1005 return ADDR_AREA_SMALL;
1007 return ADDR_AREA_NORMAL;
1010 static tree
1011 ia64_handle_model_attribute (tree *node, tree name, tree args, int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1013 ia64_addr_area addr_area = ADDR_AREA_NORMAL;
1014 ia64_addr_area area;
1015 tree arg, decl = *node;
1017 init_idents ();
1018 arg = TREE_VALUE (args);
1019 if (arg == small_ident1 || arg == small_ident2)
1021 addr_area = ADDR_AREA_SMALL;
1023 else
1025 warning ("invalid argument of `%s' attribute",
1026 IDENTIFIER_POINTER (name));
1027 *no_add_attrs = true;
1030 switch (TREE_CODE (decl))
1032 case VAR_DECL:
1033 if ((DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl))
1034 == FUNCTION_DECL)
1035 && !TREE_STATIC (decl))
1037 error ("%Jan address area attribute cannot be specified for "
1038 "local variables", decl, decl);
1039 *no_add_attrs = true;
1041 area = ia64_get_addr_area (decl);
1042 if (area != ADDR_AREA_NORMAL && addr_area != area)
1044 error ("%Jaddress area of '%s' conflicts with previous "
1045 "declaration", decl, decl);
1046 *no_add_attrs = true;
1048 break;
1050 case FUNCTION_DECL:
1051 error ("%Jaddress area attribute cannot be specified for functions",
1052 decl, decl);
1053 *no_add_attrs = true;
1054 break;
1056 default:
1057 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1058 *no_add_attrs = true;
1059 break;
1062 return NULL_TREE;
1065 static void
1066 ia64_encode_addr_area (tree decl, rtx symbol)
1068 int flags;
1070 flags = SYMBOL_REF_FLAGS (symbol);
1071 switch (ia64_get_addr_area (decl))
1073 case ADDR_AREA_NORMAL: break;
1074 case ADDR_AREA_SMALL: flags |= SYMBOL_FLAG_SMALL_ADDR; break;
1075 default: abort ();
1077 SYMBOL_REF_FLAGS (symbol) = flags;
1080 static void
1081 ia64_encode_section_info (tree decl, rtx rtl, int first)
1083 default_encode_section_info (decl, rtl, first);
1085 if (TREE_CODE (decl) == VAR_DECL
1086 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1087 ia64_encode_addr_area (decl, XEXP (rtl, 0));
1090 /* Return 1 if the operands of a move are ok. */
1093 ia64_move_ok (rtx dst, rtx src)
1095 /* If we're under init_recog_no_volatile, we'll not be able to use
1096 memory_operand. So check the code directly and don't worry about
1097 the validity of the underlying address, which should have been
1098 checked elsewhere anyway. */
1099 if (GET_CODE (dst) != MEM)
1100 return 1;
1101 if (GET_CODE (src) == MEM)
1102 return 0;
1103 if (register_operand (src, VOIDmode))
1104 return 1;
1106 /* Otherwise, this must be a constant, and that either 0 or 0.0 or 1.0. */
1107 if (INTEGRAL_MODE_P (GET_MODE (dst)))
1108 return src == const0_rtx;
1109 else
1110 return GET_CODE (src) == CONST_DOUBLE && CONST_DOUBLE_OK_FOR_G (src);
1114 addp4_optimize_ok (rtx op1, rtx op2)
1116 return (basereg_operand (op1, GET_MODE(op1)) !=
1117 basereg_operand (op2, GET_MODE(op2)));
1120 /* Check if OP is a mask suitable for use with SHIFT in a dep.z instruction.
1121 Return the length of the field, or <= 0 on failure. */
1124 ia64_depz_field_mask (rtx rop, rtx rshift)
1126 unsigned HOST_WIDE_INT op = INTVAL (rop);
1127 unsigned HOST_WIDE_INT shift = INTVAL (rshift);
1129 /* Get rid of the zero bits we're shifting in. */
1130 op >>= shift;
1132 /* We must now have a solid block of 1's at bit 0. */
1133 return exact_log2 (op + 1);
1136 /* Expand a symbolic constant load. */
1138 void
1139 ia64_expand_load_address (rtx dest, rtx src)
1141 if (tls_symbolic_operand (src, VOIDmode))
1142 abort ();
1143 if (GET_CODE (dest) != REG)
1144 abort ();
1146 /* ILP32 mode still loads 64-bits of data from the GOT. This avoids
1147 having to pointer-extend the value afterward. Other forms of address
1148 computation below are also more natural to compute as 64-bit quantities.
1149 If we've been given an SImode destination register, change it. */
1150 if (GET_MODE (dest) != Pmode)
1151 dest = gen_rtx_REG (Pmode, REGNO (dest));
1153 if (GET_CODE (src) == SYMBOL_REF && SYMBOL_REF_SMALL_ADDR_P (src))
1155 emit_insn (gen_rtx_SET (VOIDmode, dest, src));
1156 return;
1158 else if (TARGET_AUTO_PIC)
1160 emit_insn (gen_load_gprel64 (dest, src));
1161 return;
1163 else if (GET_CODE (src) == SYMBOL_REF && SYMBOL_REF_FUNCTION_P (src))
1165 emit_insn (gen_load_fptr (dest, src));
1166 return;
1168 else if (sdata_symbolic_operand (src, VOIDmode))
1170 emit_insn (gen_load_gprel (dest, src));
1171 return;
1174 if (GET_CODE (src) == CONST
1175 && GET_CODE (XEXP (src, 0)) == PLUS
1176 && GET_CODE (XEXP (XEXP (src, 0), 1)) == CONST_INT
1177 && (INTVAL (XEXP (XEXP (src, 0), 1)) & 0x1fff) != 0)
1179 rtx sym = XEXP (XEXP (src, 0), 0);
1180 HOST_WIDE_INT ofs, hi, lo;
1182 /* Split the offset into a sign extended 14-bit low part
1183 and a complementary high part. */
1184 ofs = INTVAL (XEXP (XEXP (src, 0), 1));
1185 lo = ((ofs & 0x3fff) ^ 0x2000) - 0x2000;
1186 hi = ofs - lo;
1188 ia64_expand_load_address (dest, plus_constant (sym, hi));
1189 emit_insn (gen_adddi3 (dest, dest, GEN_INT (lo)));
1191 else
1193 rtx tmp;
1195 tmp = gen_rtx_HIGH (Pmode, src);
1196 tmp = gen_rtx_PLUS (Pmode, tmp, pic_offset_table_rtx);
1197 emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
1199 tmp = gen_rtx_LO_SUM (GET_MODE (dest), dest, src);
1200 emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
1204 static GTY(()) rtx gen_tls_tga;
1205 static rtx
1206 gen_tls_get_addr (void)
1208 if (!gen_tls_tga)
1209 gen_tls_tga = init_one_libfunc ("__tls_get_addr");
1210 return gen_tls_tga;
1213 static GTY(()) rtx thread_pointer_rtx;
1214 static rtx
1215 gen_thread_pointer (void)
1217 if (!thread_pointer_rtx)
1219 thread_pointer_rtx = gen_rtx_REG (Pmode, 13);
1220 RTX_UNCHANGING_P (thread_pointer_rtx) = 1;
1222 return thread_pointer_rtx;
1225 static rtx
1226 ia64_expand_tls_address (enum tls_model tls_kind, rtx op0, rtx op1)
1228 rtx tga_op1, tga_op2, tga_ret, tga_eqv, tmp, insns;
1229 rtx orig_op0 = op0;
1231 switch (tls_kind)
1233 case TLS_MODEL_GLOBAL_DYNAMIC:
1234 start_sequence ();
1236 tga_op1 = gen_reg_rtx (Pmode);
1237 emit_insn (gen_load_ltoff_dtpmod (tga_op1, op1));
1238 tga_op1 = gen_rtx_MEM (Pmode, tga_op1);
1239 RTX_UNCHANGING_P (tga_op1) = 1;
1241 tga_op2 = gen_reg_rtx (Pmode);
1242 emit_insn (gen_load_ltoff_dtprel (tga_op2, op1));
1243 tga_op2 = gen_rtx_MEM (Pmode, tga_op2);
1244 RTX_UNCHANGING_P (tga_op2) = 1;
1246 tga_ret = emit_library_call_value (gen_tls_get_addr (), NULL_RTX,
1247 LCT_CONST, Pmode, 2, tga_op1,
1248 Pmode, tga_op2, Pmode);
1250 insns = get_insns ();
1251 end_sequence ();
1253 if (GET_MODE (op0) != Pmode)
1254 op0 = tga_ret;
1255 emit_libcall_block (insns, op0, tga_ret, op1);
1256 break;
1258 case TLS_MODEL_LOCAL_DYNAMIC:
1259 /* ??? This isn't the completely proper way to do local-dynamic
1260 If the call to __tls_get_addr is used only by a single symbol,
1261 then we should (somehow) move the dtprel to the second arg
1262 to avoid the extra add. */
1263 start_sequence ();
1265 tga_op1 = gen_reg_rtx (Pmode);
1266 emit_insn (gen_load_ltoff_dtpmod (tga_op1, op1));
1267 tga_op1 = gen_rtx_MEM (Pmode, tga_op1);
1268 RTX_UNCHANGING_P (tga_op1) = 1;
1270 tga_op2 = const0_rtx;
1272 tga_ret = emit_library_call_value (gen_tls_get_addr (), NULL_RTX,
1273 LCT_CONST, Pmode, 2, tga_op1,
1274 Pmode, tga_op2, Pmode);
1276 insns = get_insns ();
1277 end_sequence ();
1279 tga_eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
1280 UNSPEC_LD_BASE);
1281 tmp = gen_reg_rtx (Pmode);
1282 emit_libcall_block (insns, tmp, tga_ret, tga_eqv);
1284 if (!register_operand (op0, Pmode))
1285 op0 = gen_reg_rtx (Pmode);
1286 if (TARGET_TLS64)
1288 emit_insn (gen_load_dtprel (op0, op1));
1289 emit_insn (gen_adddi3 (op0, tmp, op0));
1291 else
1292 emit_insn (gen_add_dtprel (op0, tmp, op1));
1293 break;
1295 case TLS_MODEL_INITIAL_EXEC:
1296 tmp = gen_reg_rtx (Pmode);
1297 emit_insn (gen_load_ltoff_tprel (tmp, op1));
1298 tmp = gen_rtx_MEM (Pmode, tmp);
1299 RTX_UNCHANGING_P (tmp) = 1;
1300 tmp = force_reg (Pmode, tmp);
1302 if (!register_operand (op0, Pmode))
1303 op0 = gen_reg_rtx (Pmode);
1304 emit_insn (gen_adddi3 (op0, tmp, gen_thread_pointer ()));
1305 break;
1307 case TLS_MODEL_LOCAL_EXEC:
1308 if (!register_operand (op0, Pmode))
1309 op0 = gen_reg_rtx (Pmode);
1310 if (TARGET_TLS64)
1312 emit_insn (gen_load_tprel (op0, op1));
1313 emit_insn (gen_adddi3 (op0, gen_thread_pointer (), op0));
1315 else
1316 emit_insn (gen_add_tprel (op0, gen_thread_pointer (), op1));
1317 break;
1319 default:
1320 abort ();
1323 if (orig_op0 == op0)
1324 return NULL_RTX;
1325 if (GET_MODE (orig_op0) == Pmode)
1326 return op0;
1327 return gen_lowpart (GET_MODE (orig_op0), op0);
1331 ia64_expand_move (rtx op0, rtx op1)
1333 enum machine_mode mode = GET_MODE (op0);
1335 if (!reload_in_progress && !reload_completed && !ia64_move_ok (op0, op1))
1336 op1 = force_reg (mode, op1);
1338 if ((mode == Pmode || mode == ptr_mode) && symbolic_operand (op1, VOIDmode))
1340 enum tls_model tls_kind;
1341 if ((tls_kind = tls_symbolic_operand (op1, VOIDmode)))
1342 return ia64_expand_tls_address (tls_kind, op0, op1);
1344 if (!TARGET_NO_PIC && reload_completed)
1346 ia64_expand_load_address (op0, op1);
1347 return NULL_RTX;
1351 return op1;
1354 /* Split a move from OP1 to OP0 conditional on COND. */
1356 void
1357 ia64_emit_cond_move (rtx op0, rtx op1, rtx cond)
1359 rtx insn, first = get_last_insn ();
1361 emit_move_insn (op0, op1);
1363 for (insn = get_last_insn (); insn != first; insn = PREV_INSN (insn))
1364 if (INSN_P (insn))
1365 PATTERN (insn) = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond),
1366 PATTERN (insn));
1369 /* Split a post-reload TImode or TFmode reference into two DImode
1370 components. This is made extra difficult by the fact that we do
1371 not get any scratch registers to work with, because reload cannot
1372 be prevented from giving us a scratch that overlaps the register
1373 pair involved. So instead, when addressing memory, we tweak the
1374 pointer register up and back down with POST_INCs. Or up and not
1375 back down when we can get away with it.
1377 REVERSED is true when the loads must be done in reversed order
1378 (high word first) for correctness. DEAD is true when the pointer
1379 dies with the second insn we generate and therefore the second
1380 address must not carry a postmodify.
1382 May return an insn which is to be emitted after the moves. */
1384 static rtx
1385 ia64_split_tmode (rtx out[2], rtx in, bool reversed, bool dead)
1387 rtx fixup = 0;
1389 switch (GET_CODE (in))
1391 case REG:
1392 out[reversed] = gen_rtx_REG (DImode, REGNO (in));
1393 out[!reversed] = gen_rtx_REG (DImode, REGNO (in) + 1);
1394 break;
1396 case CONST_INT:
1397 case CONST_DOUBLE:
1398 /* Cannot occur reversed. */
1399 if (reversed) abort ();
1401 if (GET_MODE (in) != TFmode)
1402 split_double (in, &out[0], &out[1]);
1403 else
1404 /* split_double does not understand how to split a TFmode
1405 quantity into a pair of DImode constants. */
1407 REAL_VALUE_TYPE r;
1408 unsigned HOST_WIDE_INT p[2];
1409 long l[4]; /* TFmode is 128 bits */
1411 REAL_VALUE_FROM_CONST_DOUBLE (r, in);
1412 real_to_target (l, &r, TFmode);
1414 if (FLOAT_WORDS_BIG_ENDIAN)
1416 p[0] = (((unsigned HOST_WIDE_INT) l[0]) << 32) + l[1];
1417 p[1] = (((unsigned HOST_WIDE_INT) l[2]) << 32) + l[3];
1419 else
1421 p[0] = (((unsigned HOST_WIDE_INT) l[3]) << 32) + l[2];
1422 p[1] = (((unsigned HOST_WIDE_INT) l[1]) << 32) + l[0];
1424 out[0] = GEN_INT (p[0]);
1425 out[1] = GEN_INT (p[1]);
1427 break;
1429 case MEM:
1431 rtx base = XEXP (in, 0);
1432 rtx offset;
1434 switch (GET_CODE (base))
1436 case REG:
1437 if (!reversed)
1439 out[0] = adjust_automodify_address
1440 (in, DImode, gen_rtx_POST_INC (Pmode, base), 0);
1441 out[1] = adjust_automodify_address
1442 (in, DImode, dead ? 0 : gen_rtx_POST_DEC (Pmode, base), 8);
1444 else
1446 /* Reversal requires a pre-increment, which can only
1447 be done as a separate insn. */
1448 emit_insn (gen_adddi3 (base, base, GEN_INT (8)));
1449 out[0] = adjust_automodify_address
1450 (in, DImode, gen_rtx_POST_DEC (Pmode, base), 8);
1451 out[1] = adjust_address (in, DImode, 0);
1453 break;
1455 case POST_INC:
1456 if (reversed || dead) abort ();
1457 /* Just do the increment in two steps. */
1458 out[0] = adjust_automodify_address (in, DImode, 0, 0);
1459 out[1] = adjust_automodify_address (in, DImode, 0, 8);
1460 break;
1462 case POST_DEC:
1463 if (reversed || dead) abort ();
1464 /* Add 8, subtract 24. */
1465 base = XEXP (base, 0);
1466 out[0] = adjust_automodify_address
1467 (in, DImode, gen_rtx_POST_INC (Pmode, base), 0);
1468 out[1] = adjust_automodify_address
1469 (in, DImode,
1470 gen_rtx_POST_MODIFY (Pmode, base, plus_constant (base, -24)),
1472 break;
1474 case POST_MODIFY:
1475 if (reversed || dead) abort ();
1476 /* Extract and adjust the modification. This case is
1477 trickier than the others, because we might have an
1478 index register, or we might have a combined offset that
1479 doesn't fit a signed 9-bit displacement field. We can
1480 assume the incoming expression is already legitimate. */
1481 offset = XEXP (base, 1);
1482 base = XEXP (base, 0);
1484 out[0] = adjust_automodify_address
1485 (in, DImode, gen_rtx_POST_INC (Pmode, base), 0);
1487 if (GET_CODE (XEXP (offset, 1)) == REG)
1489 /* Can't adjust the postmodify to match. Emit the
1490 original, then a separate addition insn. */
1491 out[1] = adjust_automodify_address (in, DImode, 0, 8);
1492 fixup = gen_adddi3 (base, base, GEN_INT (-8));
1494 else if (GET_CODE (XEXP (offset, 1)) != CONST_INT)
1495 abort ();
1496 else if (INTVAL (XEXP (offset, 1)) < -256 + 8)
1498 /* Again the postmodify cannot be made to match, but
1499 in this case it's more efficient to get rid of the
1500 postmodify entirely and fix up with an add insn. */
1501 out[1] = adjust_automodify_address (in, DImode, base, 8);
1502 fixup = gen_adddi3 (base, base,
1503 GEN_INT (INTVAL (XEXP (offset, 1)) - 8));
1505 else
1507 /* Combined offset still fits in the displacement field.
1508 (We cannot overflow it at the high end.) */
1509 out[1] = adjust_automodify_address
1510 (in, DImode,
1511 gen_rtx_POST_MODIFY (Pmode, base,
1512 gen_rtx_PLUS (Pmode, base,
1513 GEN_INT (INTVAL (XEXP (offset, 1)) - 8))),
1516 break;
1518 default:
1519 abort ();
1521 break;
1524 default:
1525 abort ();
1528 return fixup;
1531 /* Split a TImode or TFmode move instruction after reload.
1532 This is used by *movtf_internal and *movti_internal. */
1533 void
1534 ia64_split_tmode_move (rtx operands[])
1536 rtx in[2], out[2], insn;
1537 rtx fixup[2];
1538 bool dead = false;
1539 bool reversed = false;
1541 /* It is possible for reload to decide to overwrite a pointer with
1542 the value it points to. In that case we have to do the loads in
1543 the appropriate order so that the pointer is not destroyed too
1544 early. Also we must not generate a postmodify for that second
1545 load, or rws_access_regno will abort. */
1546 if (GET_CODE (operands[1]) == MEM
1547 && reg_overlap_mentioned_p (operands[0], operands[1]))
1549 rtx base = XEXP (operands[1], 0);
1550 while (GET_CODE (base) != REG)
1551 base = XEXP (base, 0);
1553 if (REGNO (base) == REGNO (operands[0]))
1554 reversed = true;
1555 dead = true;
1557 /* Another reason to do the moves in reversed order is if the first
1558 element of the target register pair is also the second element of
1559 the source register pair. */
1560 if (GET_CODE (operands[0]) == REG && GET_CODE (operands[1]) == REG
1561 && REGNO (operands[0]) == REGNO (operands[1]) + 1)
1562 reversed = true;
1564 fixup[0] = ia64_split_tmode (in, operands[1], reversed, dead);
1565 fixup[1] = ia64_split_tmode (out, operands[0], reversed, dead);
1567 #define MAYBE_ADD_REG_INC_NOTE(INSN, EXP) \
1568 if (GET_CODE (EXP) == MEM \
1569 && (GET_CODE (XEXP (EXP, 0)) == POST_MODIFY \
1570 || GET_CODE (XEXP (EXP, 0)) == POST_INC \
1571 || GET_CODE (XEXP (EXP, 0)) == POST_DEC)) \
1572 REG_NOTES (INSN) = gen_rtx_EXPR_LIST (REG_INC, \
1573 XEXP (XEXP (EXP, 0), 0), \
1574 REG_NOTES (INSN))
1576 insn = emit_insn (gen_rtx_SET (VOIDmode, out[0], in[0]));
1577 MAYBE_ADD_REG_INC_NOTE (insn, in[0]);
1578 MAYBE_ADD_REG_INC_NOTE (insn, out[0]);
1580 insn = emit_insn (gen_rtx_SET (VOIDmode, out[1], in[1]));
1581 MAYBE_ADD_REG_INC_NOTE (insn, in[1]);
1582 MAYBE_ADD_REG_INC_NOTE (insn, out[1]);
1584 if (fixup[0])
1585 emit_insn (fixup[0]);
1586 if (fixup[1])
1587 emit_insn (fixup[1]);
1589 #undef MAYBE_ADD_REG_INC_NOTE
1592 /* ??? Fixing GR->FR XFmode moves during reload is hard. You need to go
1593 through memory plus an extra GR scratch register. Except that you can
1594 either get the first from SECONDARY_MEMORY_NEEDED or the second from
1595 SECONDARY_RELOAD_CLASS, but not both.
1597 We got into problems in the first place by allowing a construct like
1598 (subreg:XF (reg:TI)), which we got from a union containing a long double.
1599 This solution attempts to prevent this situation from occurring. When
1600 we see something like the above, we spill the inner register to memory. */
1603 spill_xfmode_operand (rtx in, int force)
1605 if (GET_CODE (in) == SUBREG
1606 && GET_MODE (SUBREG_REG (in)) == TImode
1607 && GET_CODE (SUBREG_REG (in)) == REG)
1609 rtx mem = gen_mem_addressof (SUBREG_REG (in), NULL_TREE, /*rescan=*/true);
1610 return gen_rtx_MEM (XFmode, copy_to_reg (XEXP (mem, 0)));
1612 else if (force && GET_CODE (in) == REG)
1614 rtx mem = gen_mem_addressof (in, NULL_TREE, /*rescan=*/true);
1615 return gen_rtx_MEM (XFmode, copy_to_reg (XEXP (mem, 0)));
1617 else if (GET_CODE (in) == MEM
1618 && GET_CODE (XEXP (in, 0)) == ADDRESSOF)
1619 return change_address (in, XFmode, copy_to_reg (XEXP (in, 0)));
1620 else
1621 return in;
1624 /* Emit comparison instruction if necessary, returning the expression
1625 that holds the compare result in the proper mode. */
1627 static GTY(()) rtx cmptf_libfunc;
1630 ia64_expand_compare (enum rtx_code code, enum machine_mode mode)
1632 rtx op0 = ia64_compare_op0, op1 = ia64_compare_op1;
1633 rtx cmp;
1635 /* If we have a BImode input, then we already have a compare result, and
1636 do not need to emit another comparison. */
1637 if (GET_MODE (op0) == BImode)
1639 if ((code == NE || code == EQ) && op1 == const0_rtx)
1640 cmp = op0;
1641 else
1642 abort ();
1644 /* HPUX TFmode compare requires a library call to _U_Qfcmp, which takes a
1645 magic number as its third argument, that indicates what to do.
1646 The return value is an integer to be compared against zero. */
1647 else if (TARGET_HPUX && GET_MODE (op0) == TFmode)
1649 enum qfcmp_magic {
1650 QCMP_INV = 1, /* Raise FP_INVALID on SNaN as a side effect. */
1651 QCMP_UNORD = 2,
1652 QCMP_EQ = 4,
1653 QCMP_LT = 8,
1654 QCMP_GT = 16
1655 } magic;
1656 enum rtx_code ncode;
1657 rtx ret, insns;
1658 if (GET_MODE (op1) != TFmode)
1659 abort ();
1660 switch (code)
1662 /* 1 = equal, 0 = not equal. Equality operators do
1663 not raise FP_INVALID when given an SNaN operand. */
1664 case EQ: magic = QCMP_EQ; ncode = NE; break;
1665 case NE: magic = QCMP_EQ; ncode = EQ; break;
1666 /* isunordered() from C99. */
1667 case UNORDERED: magic = QCMP_UNORD; ncode = NE; break;
1668 /* Relational operators raise FP_INVALID when given
1669 an SNaN operand. */
1670 case LT: magic = QCMP_LT |QCMP_INV; ncode = NE; break;
1671 case LE: magic = QCMP_LT|QCMP_EQ|QCMP_INV; ncode = NE; break;
1672 case GT: magic = QCMP_GT |QCMP_INV; ncode = NE; break;
1673 case GE: magic = QCMP_GT|QCMP_EQ|QCMP_INV; ncode = NE; break;
1674 /* FUTURE: Implement UNEQ, UNLT, UNLE, UNGT, UNGE, LTGT.
1675 Expanders for buneq etc. weuld have to be added to ia64.md
1676 for this to be useful. */
1677 default: abort ();
1680 start_sequence ();
1682 ret = emit_library_call_value (cmptf_libfunc, 0, LCT_CONST, DImode, 3,
1683 op0, TFmode, op1, TFmode,
1684 GEN_INT (magic), DImode);
1685 cmp = gen_reg_rtx (BImode);
1686 emit_insn (gen_rtx_SET (VOIDmode, cmp,
1687 gen_rtx_fmt_ee (ncode, BImode,
1688 ret, const0_rtx)));
1690 insns = get_insns ();
1691 end_sequence ();
1693 emit_libcall_block (insns, cmp, cmp,
1694 gen_rtx_fmt_ee (code, BImode, op0, op1));
1695 code = NE;
1697 else
1699 cmp = gen_reg_rtx (BImode);
1700 emit_insn (gen_rtx_SET (VOIDmode, cmp,
1701 gen_rtx_fmt_ee (code, BImode, op0, op1)));
1702 code = NE;
1705 return gen_rtx_fmt_ee (code, mode, cmp, const0_rtx);
1708 /* Emit the appropriate sequence for a call. */
1710 void
1711 ia64_expand_call (rtx retval, rtx addr, rtx nextarg ATTRIBUTE_UNUSED,
1712 int sibcall_p)
1714 rtx insn, b0;
1716 addr = XEXP (addr, 0);
1717 addr = convert_memory_address (DImode, addr);
1718 b0 = gen_rtx_REG (DImode, R_BR (0));
1720 /* ??? Should do this for functions known to bind local too. */
1721 if (TARGET_NO_PIC || TARGET_AUTO_PIC)
1723 if (sibcall_p)
1724 insn = gen_sibcall_nogp (addr);
1725 else if (! retval)
1726 insn = gen_call_nogp (addr, b0);
1727 else
1728 insn = gen_call_value_nogp (retval, addr, b0);
1729 insn = emit_call_insn (insn);
1731 else
1733 if (sibcall_p)
1734 insn = gen_sibcall_gp (addr);
1735 else if (! retval)
1736 insn = gen_call_gp (addr, b0);
1737 else
1738 insn = gen_call_value_gp (retval, addr, b0);
1739 insn = emit_call_insn (insn);
1741 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
1744 if (sibcall_p)
1745 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), b0);
1748 void
1749 ia64_reload_gp (void)
1751 rtx tmp;
1753 if (current_frame_info.reg_save_gp)
1754 tmp = gen_rtx_REG (DImode, current_frame_info.reg_save_gp);
1755 else
1757 HOST_WIDE_INT offset;
1759 offset = (current_frame_info.spill_cfa_off
1760 + current_frame_info.spill_size);
1761 if (frame_pointer_needed)
1763 tmp = hard_frame_pointer_rtx;
1764 offset = -offset;
1766 else
1768 tmp = stack_pointer_rtx;
1769 offset = current_frame_info.total_size - offset;
1772 if (CONST_OK_FOR_I (offset))
1773 emit_insn (gen_adddi3 (pic_offset_table_rtx,
1774 tmp, GEN_INT (offset)));
1775 else
1777 emit_move_insn (pic_offset_table_rtx, GEN_INT (offset));
1778 emit_insn (gen_adddi3 (pic_offset_table_rtx,
1779 pic_offset_table_rtx, tmp));
1782 tmp = gen_rtx_MEM (DImode, pic_offset_table_rtx);
1785 emit_move_insn (pic_offset_table_rtx, tmp);
1788 void
1789 ia64_split_call (rtx retval, rtx addr, rtx retaddr, rtx scratch_r,
1790 rtx scratch_b, int noreturn_p, int sibcall_p)
1792 rtx insn;
1793 bool is_desc = false;
1795 /* If we find we're calling through a register, then we're actually
1796 calling through a descriptor, so load up the values. */
1797 if (REG_P (addr) && GR_REGNO_P (REGNO (addr)))
1799 rtx tmp;
1800 bool addr_dead_p;
1802 /* ??? We are currently constrained to *not* use peep2, because
1803 we can legitimately change the global lifetime of the GP
1804 (in the form of killing where previously live). This is
1805 because a call through a descriptor doesn't use the previous
1806 value of the GP, while a direct call does, and we do not
1807 commit to either form until the split here.
1809 That said, this means that we lack precise life info for
1810 whether ADDR is dead after this call. This is not terribly
1811 important, since we can fix things up essentially for free
1812 with the POST_DEC below, but it's nice to not use it when we
1813 can immediately tell it's not necessary. */
1814 addr_dead_p = ((noreturn_p || sibcall_p
1815 || TEST_HARD_REG_BIT (regs_invalidated_by_call,
1816 REGNO (addr)))
1817 && !FUNCTION_ARG_REGNO_P (REGNO (addr)));
1819 /* Load the code address into scratch_b. */
1820 tmp = gen_rtx_POST_INC (Pmode, addr);
1821 tmp = gen_rtx_MEM (Pmode, tmp);
1822 emit_move_insn (scratch_r, tmp);
1823 emit_move_insn (scratch_b, scratch_r);
1825 /* Load the GP address. If ADDR is not dead here, then we must
1826 revert the change made above via the POST_INCREMENT. */
1827 if (!addr_dead_p)
1828 tmp = gen_rtx_POST_DEC (Pmode, addr);
1829 else
1830 tmp = addr;
1831 tmp = gen_rtx_MEM (Pmode, tmp);
1832 emit_move_insn (pic_offset_table_rtx, tmp);
1834 is_desc = true;
1835 addr = scratch_b;
1838 if (sibcall_p)
1839 insn = gen_sibcall_nogp (addr);
1840 else if (retval)
1841 insn = gen_call_value_nogp (retval, addr, retaddr);
1842 else
1843 insn = gen_call_nogp (addr, retaddr);
1844 emit_call_insn (insn);
1846 if ((!TARGET_CONST_GP || is_desc) && !noreturn_p && !sibcall_p)
1847 ia64_reload_gp ();
1850 /* Begin the assembly file. */
1852 static void
1853 ia64_file_start (void)
1855 default_file_start ();
1856 emit_safe_across_calls ();
1859 void
1860 emit_safe_across_calls (void)
1862 unsigned int rs, re;
1863 int out_state;
1865 rs = 1;
1866 out_state = 0;
1867 while (1)
1869 while (rs < 64 && call_used_regs[PR_REG (rs)])
1870 rs++;
1871 if (rs >= 64)
1872 break;
1873 for (re = rs + 1; re < 64 && ! call_used_regs[PR_REG (re)]; re++)
1874 continue;
1875 if (out_state == 0)
1877 fputs ("\t.pred.safe_across_calls ", asm_out_file);
1878 out_state = 1;
1880 else
1881 fputc (',', asm_out_file);
1882 if (re == rs + 1)
1883 fprintf (asm_out_file, "p%u", rs);
1884 else
1885 fprintf (asm_out_file, "p%u-p%u", rs, re - 1);
1886 rs = re + 1;
1888 if (out_state)
1889 fputc ('\n', asm_out_file);
1892 /* Helper function for ia64_compute_frame_size: find an appropriate general
1893 register to spill some special register to. SPECIAL_SPILL_MASK contains
1894 bits in GR0 to GR31 that have already been allocated by this routine.
1895 TRY_LOCALS is true if we should attempt to locate a local regnum. */
1897 static int
1898 find_gr_spill (int try_locals)
1900 int regno;
1902 /* If this is a leaf function, first try an otherwise unused
1903 call-clobbered register. */
1904 if (current_function_is_leaf)
1906 for (regno = GR_REG (1); regno <= GR_REG (31); regno++)
1907 if (! regs_ever_live[regno]
1908 && call_used_regs[regno]
1909 && ! fixed_regs[regno]
1910 && ! global_regs[regno]
1911 && ((current_frame_info.gr_used_mask >> regno) & 1) == 0)
1913 current_frame_info.gr_used_mask |= 1 << regno;
1914 return regno;
1918 if (try_locals)
1920 regno = current_frame_info.n_local_regs;
1921 /* If there is a frame pointer, then we can't use loc79, because
1922 that is HARD_FRAME_POINTER_REGNUM. In particular, see the
1923 reg_name switching code in ia64_expand_prologue. */
1924 if (regno < (80 - frame_pointer_needed))
1926 current_frame_info.n_local_regs = regno + 1;
1927 return LOC_REG (0) + regno;
1931 /* Failed to find a general register to spill to. Must use stack. */
1932 return 0;
1935 /* In order to make for nice schedules, we try to allocate every temporary
1936 to a different register. We must of course stay away from call-saved,
1937 fixed, and global registers. We must also stay away from registers
1938 allocated in current_frame_info.gr_used_mask, since those include regs
1939 used all through the prologue.
1941 Any register allocated here must be used immediately. The idea is to
1942 aid scheduling, not to solve data flow problems. */
1944 static int last_scratch_gr_reg;
1946 static int
1947 next_scratch_gr_reg (void)
1949 int i, regno;
1951 for (i = 0; i < 32; ++i)
1953 regno = (last_scratch_gr_reg + i + 1) & 31;
1954 if (call_used_regs[regno]
1955 && ! fixed_regs[regno]
1956 && ! global_regs[regno]
1957 && ((current_frame_info.gr_used_mask >> regno) & 1) == 0)
1959 last_scratch_gr_reg = regno;
1960 return regno;
1964 /* There must be _something_ available. */
1965 abort ();
1968 /* Helper function for ia64_compute_frame_size, called through
1969 diddle_return_value. Mark REG in current_frame_info.gr_used_mask. */
1971 static void
1972 mark_reg_gr_used_mask (rtx reg, void *data ATTRIBUTE_UNUSED)
1974 unsigned int regno = REGNO (reg);
1975 if (regno < 32)
1977 unsigned int i, n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
1978 for (i = 0; i < n; ++i)
1979 current_frame_info.gr_used_mask |= 1 << (regno + i);
1983 /* Returns the number of bytes offset between the frame pointer and the stack
1984 pointer for the current function. SIZE is the number of bytes of space
1985 needed for local variables. */
1987 static void
1988 ia64_compute_frame_size (HOST_WIDE_INT size)
1990 HOST_WIDE_INT total_size;
1991 HOST_WIDE_INT spill_size = 0;
1992 HOST_WIDE_INT extra_spill_size = 0;
1993 HOST_WIDE_INT pretend_args_size;
1994 HARD_REG_SET mask;
1995 int n_spilled = 0;
1996 int spilled_gr_p = 0;
1997 int spilled_fr_p = 0;
1998 unsigned int regno;
1999 int i;
2001 if (current_frame_info.initialized)
2002 return;
2004 memset (&current_frame_info, 0, sizeof current_frame_info);
2005 CLEAR_HARD_REG_SET (mask);
2007 /* Don't allocate scratches to the return register. */
2008 diddle_return_value (mark_reg_gr_used_mask, NULL);
2010 /* Don't allocate scratches to the EH scratch registers. */
2011 if (cfun->machine->ia64_eh_epilogue_sp)
2012 mark_reg_gr_used_mask (cfun->machine->ia64_eh_epilogue_sp, NULL);
2013 if (cfun->machine->ia64_eh_epilogue_bsp)
2014 mark_reg_gr_used_mask (cfun->machine->ia64_eh_epilogue_bsp, NULL);
2016 /* Find the size of the register stack frame. We have only 80 local
2017 registers, because we reserve 8 for the inputs and 8 for the
2018 outputs. */
2020 /* Skip HARD_FRAME_POINTER_REGNUM (loc79) when frame_pointer_needed,
2021 since we'll be adjusting that down later. */
2022 regno = LOC_REG (78) + ! frame_pointer_needed;
2023 for (; regno >= LOC_REG (0); regno--)
2024 if (regs_ever_live[regno])
2025 break;
2026 current_frame_info.n_local_regs = regno - LOC_REG (0) + 1;
2028 /* For functions marked with the syscall_linkage attribute, we must mark
2029 all eight input registers as in use, so that locals aren't visible to
2030 the caller. */
2032 if (cfun->machine->n_varargs > 0
2033 || lookup_attribute ("syscall_linkage",
2034 TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
2035 current_frame_info.n_input_regs = 8;
2036 else
2038 for (regno = IN_REG (7); regno >= IN_REG (0); regno--)
2039 if (regs_ever_live[regno])
2040 break;
2041 current_frame_info.n_input_regs = regno - IN_REG (0) + 1;
2044 for (regno = OUT_REG (7); regno >= OUT_REG (0); regno--)
2045 if (regs_ever_live[regno])
2046 break;
2047 i = regno - OUT_REG (0) + 1;
2049 /* When -p profiling, we need one output register for the mcount argument.
2050 Likewise for -a profiling for the bb_init_func argument. For -ax
2051 profiling, we need two output registers for the two bb_init_trace_func
2052 arguments. */
2053 if (current_function_profile)
2054 i = MAX (i, 1);
2055 current_frame_info.n_output_regs = i;
2057 /* ??? No rotating register support yet. */
2058 current_frame_info.n_rotate_regs = 0;
2060 /* Discover which registers need spilling, and how much room that
2061 will take. Begin with floating point and general registers,
2062 which will always wind up on the stack. */
2064 for (regno = FR_REG (2); regno <= FR_REG (127); regno++)
2065 if (regs_ever_live[regno] && ! call_used_regs[regno])
2067 SET_HARD_REG_BIT (mask, regno);
2068 spill_size += 16;
2069 n_spilled += 1;
2070 spilled_fr_p = 1;
2073 for (regno = GR_REG (1); regno <= GR_REG (31); regno++)
2074 if (regs_ever_live[regno] && ! call_used_regs[regno])
2076 SET_HARD_REG_BIT (mask, regno);
2077 spill_size += 8;
2078 n_spilled += 1;
2079 spilled_gr_p = 1;
2082 for (regno = BR_REG (1); regno <= BR_REG (7); regno++)
2083 if (regs_ever_live[regno] && ! call_used_regs[regno])
2085 SET_HARD_REG_BIT (mask, regno);
2086 spill_size += 8;
2087 n_spilled += 1;
2090 /* Now come all special registers that might get saved in other
2091 general registers. */
2093 if (frame_pointer_needed)
2095 current_frame_info.reg_fp = find_gr_spill (1);
2096 /* If we did not get a register, then we take LOC79. This is guaranteed
2097 to be free, even if regs_ever_live is already set, because this is
2098 HARD_FRAME_POINTER_REGNUM. This requires incrementing n_local_regs,
2099 as we don't count loc79 above. */
2100 if (current_frame_info.reg_fp == 0)
2102 current_frame_info.reg_fp = LOC_REG (79);
2103 current_frame_info.n_local_regs++;
2107 if (! current_function_is_leaf)
2109 /* Emit a save of BR0 if we call other functions. Do this even
2110 if this function doesn't return, as EH depends on this to be
2111 able to unwind the stack. */
2112 SET_HARD_REG_BIT (mask, BR_REG (0));
2114 current_frame_info.reg_save_b0 = find_gr_spill (1);
2115 if (current_frame_info.reg_save_b0 == 0)
2117 spill_size += 8;
2118 n_spilled += 1;
2121 /* Similarly for ar.pfs. */
2122 SET_HARD_REG_BIT (mask, AR_PFS_REGNUM);
2123 current_frame_info.reg_save_ar_pfs = find_gr_spill (1);
2124 if (current_frame_info.reg_save_ar_pfs == 0)
2126 extra_spill_size += 8;
2127 n_spilled += 1;
2130 /* Similarly for gp. Note that if we're calling setjmp, the stacked
2131 registers are clobbered, so we fall back to the stack. */
2132 current_frame_info.reg_save_gp
2133 = (current_function_calls_setjmp ? 0 : find_gr_spill (1));
2134 if (current_frame_info.reg_save_gp == 0)
2136 SET_HARD_REG_BIT (mask, GR_REG (1));
2137 spill_size += 8;
2138 n_spilled += 1;
2141 else
2143 if (regs_ever_live[BR_REG (0)] && ! call_used_regs[BR_REG (0)])
2145 SET_HARD_REG_BIT (mask, BR_REG (0));
2146 spill_size += 8;
2147 n_spilled += 1;
2150 if (regs_ever_live[AR_PFS_REGNUM])
2152 SET_HARD_REG_BIT (mask, AR_PFS_REGNUM);
2153 current_frame_info.reg_save_ar_pfs = find_gr_spill (1);
2154 if (current_frame_info.reg_save_ar_pfs == 0)
2156 extra_spill_size += 8;
2157 n_spilled += 1;
2162 /* Unwind descriptor hackery: things are most efficient if we allocate
2163 consecutive GR save registers for RP, PFS, FP in that order. However,
2164 it is absolutely critical that FP get the only hard register that's
2165 guaranteed to be free, so we allocated it first. If all three did
2166 happen to be allocated hard regs, and are consecutive, rearrange them
2167 into the preferred order now. */
2168 if (current_frame_info.reg_fp != 0
2169 && current_frame_info.reg_save_b0 == current_frame_info.reg_fp + 1
2170 && current_frame_info.reg_save_ar_pfs == current_frame_info.reg_fp + 2)
2172 current_frame_info.reg_save_b0 = current_frame_info.reg_fp;
2173 current_frame_info.reg_save_ar_pfs = current_frame_info.reg_fp + 1;
2174 current_frame_info.reg_fp = current_frame_info.reg_fp + 2;
2177 /* See if we need to store the predicate register block. */
2178 for (regno = PR_REG (0); regno <= PR_REG (63); regno++)
2179 if (regs_ever_live[regno] && ! call_used_regs[regno])
2180 break;
2181 if (regno <= PR_REG (63))
2183 SET_HARD_REG_BIT (mask, PR_REG (0));
2184 current_frame_info.reg_save_pr = find_gr_spill (1);
2185 if (current_frame_info.reg_save_pr == 0)
2187 extra_spill_size += 8;
2188 n_spilled += 1;
2191 /* ??? Mark them all as used so that register renaming and such
2192 are free to use them. */
2193 for (regno = PR_REG (0); regno <= PR_REG (63); regno++)
2194 regs_ever_live[regno] = 1;
2197 /* If we're forced to use st8.spill, we're forced to save and restore
2198 ar.unat as well. The check for existing liveness allows inline asm
2199 to touch ar.unat. */
2200 if (spilled_gr_p || cfun->machine->n_varargs
2201 || regs_ever_live[AR_UNAT_REGNUM])
2203 regs_ever_live[AR_UNAT_REGNUM] = 1;
2204 SET_HARD_REG_BIT (mask, AR_UNAT_REGNUM);
2205 current_frame_info.reg_save_ar_unat = find_gr_spill (spill_size == 0);
2206 if (current_frame_info.reg_save_ar_unat == 0)
2208 extra_spill_size += 8;
2209 n_spilled += 1;
2213 if (regs_ever_live[AR_LC_REGNUM])
2215 SET_HARD_REG_BIT (mask, AR_LC_REGNUM);
2216 current_frame_info.reg_save_ar_lc = find_gr_spill (spill_size == 0);
2217 if (current_frame_info.reg_save_ar_lc == 0)
2219 extra_spill_size += 8;
2220 n_spilled += 1;
2224 /* If we have an odd number of words of pretend arguments written to
2225 the stack, then the FR save area will be unaligned. We round the
2226 size of this area up to keep things 16 byte aligned. */
2227 if (spilled_fr_p)
2228 pretend_args_size = IA64_STACK_ALIGN (current_function_pretend_args_size);
2229 else
2230 pretend_args_size = current_function_pretend_args_size;
2232 total_size = (spill_size + extra_spill_size + size + pretend_args_size
2233 + current_function_outgoing_args_size);
2234 total_size = IA64_STACK_ALIGN (total_size);
2236 /* We always use the 16-byte scratch area provided by the caller, but
2237 if we are a leaf function, there's no one to which we need to provide
2238 a scratch area. */
2239 if (current_function_is_leaf)
2240 total_size = MAX (0, total_size - 16);
2242 current_frame_info.total_size = total_size;
2243 current_frame_info.spill_cfa_off = pretend_args_size - 16;
2244 current_frame_info.spill_size = spill_size;
2245 current_frame_info.extra_spill_size = extra_spill_size;
2246 COPY_HARD_REG_SET (current_frame_info.mask, mask);
2247 current_frame_info.n_spilled = n_spilled;
2248 current_frame_info.initialized = reload_completed;
2251 /* Compute the initial difference between the specified pair of registers. */
2253 HOST_WIDE_INT
2254 ia64_initial_elimination_offset (int from, int to)
2256 HOST_WIDE_INT offset;
2258 ia64_compute_frame_size (get_frame_size ());
2259 switch (from)
2261 case FRAME_POINTER_REGNUM:
2262 if (to == HARD_FRAME_POINTER_REGNUM)
2264 if (current_function_is_leaf)
2265 offset = -current_frame_info.total_size;
2266 else
2267 offset = -(current_frame_info.total_size
2268 - current_function_outgoing_args_size - 16);
2270 else if (to == STACK_POINTER_REGNUM)
2272 if (current_function_is_leaf)
2273 offset = 0;
2274 else
2275 offset = 16 + current_function_outgoing_args_size;
2277 else
2278 abort ();
2279 break;
2281 case ARG_POINTER_REGNUM:
2282 /* Arguments start above the 16 byte save area, unless stdarg
2283 in which case we store through the 16 byte save area. */
2284 if (to == HARD_FRAME_POINTER_REGNUM)
2285 offset = 16 - current_function_pretend_args_size;
2286 else if (to == STACK_POINTER_REGNUM)
2287 offset = (current_frame_info.total_size
2288 + 16 - current_function_pretend_args_size);
2289 else
2290 abort ();
2291 break;
2293 default:
2294 abort ();
2297 return offset;
2300 /* If there are more than a trivial number of register spills, we use
2301 two interleaved iterators so that we can get two memory references
2302 per insn group.
2304 In order to simplify things in the prologue and epilogue expanders,
2305 we use helper functions to fix up the memory references after the
2306 fact with the appropriate offsets to a POST_MODIFY memory mode.
2307 The following data structure tracks the state of the two iterators
2308 while insns are being emitted. */
2310 struct spill_fill_data
2312 rtx init_after; /* point at which to emit initializations */
2313 rtx init_reg[2]; /* initial base register */
2314 rtx iter_reg[2]; /* the iterator registers */
2315 rtx *prev_addr[2]; /* address of last memory use */
2316 rtx prev_insn[2]; /* the insn corresponding to prev_addr */
2317 HOST_WIDE_INT prev_off[2]; /* last offset */
2318 int n_iter; /* number of iterators in use */
2319 int next_iter; /* next iterator to use */
2320 unsigned int save_gr_used_mask;
2323 static struct spill_fill_data spill_fill_data;
2325 static void
2326 setup_spill_pointers (int n_spills, rtx init_reg, HOST_WIDE_INT cfa_off)
2328 int i;
2330 spill_fill_data.init_after = get_last_insn ();
2331 spill_fill_data.init_reg[0] = init_reg;
2332 spill_fill_data.init_reg[1] = init_reg;
2333 spill_fill_data.prev_addr[0] = NULL;
2334 spill_fill_data.prev_addr[1] = NULL;
2335 spill_fill_data.prev_insn[0] = NULL;
2336 spill_fill_data.prev_insn[1] = NULL;
2337 spill_fill_data.prev_off[0] = cfa_off;
2338 spill_fill_data.prev_off[1] = cfa_off;
2339 spill_fill_data.next_iter = 0;
2340 spill_fill_data.save_gr_used_mask = current_frame_info.gr_used_mask;
2342 spill_fill_data.n_iter = 1 + (n_spills > 2);
2343 for (i = 0; i < spill_fill_data.n_iter; ++i)
2345 int regno = next_scratch_gr_reg ();
2346 spill_fill_data.iter_reg[i] = gen_rtx_REG (DImode, regno);
2347 current_frame_info.gr_used_mask |= 1 << regno;
2351 static void
2352 finish_spill_pointers (void)
2354 current_frame_info.gr_used_mask = spill_fill_data.save_gr_used_mask;
2357 static rtx
2358 spill_restore_mem (rtx reg, HOST_WIDE_INT cfa_off)
2360 int iter = spill_fill_data.next_iter;
2361 HOST_WIDE_INT disp = spill_fill_data.prev_off[iter] - cfa_off;
2362 rtx disp_rtx = GEN_INT (disp);
2363 rtx mem;
2365 if (spill_fill_data.prev_addr[iter])
2367 if (CONST_OK_FOR_N (disp))
2369 *spill_fill_data.prev_addr[iter]
2370 = gen_rtx_POST_MODIFY (DImode, spill_fill_data.iter_reg[iter],
2371 gen_rtx_PLUS (DImode,
2372 spill_fill_data.iter_reg[iter],
2373 disp_rtx));
2374 REG_NOTES (spill_fill_data.prev_insn[iter])
2375 = gen_rtx_EXPR_LIST (REG_INC, spill_fill_data.iter_reg[iter],
2376 REG_NOTES (spill_fill_data.prev_insn[iter]));
2378 else
2380 /* ??? Could use register post_modify for loads. */
2381 if (! CONST_OK_FOR_I (disp))
2383 rtx tmp = gen_rtx_REG (DImode, next_scratch_gr_reg ());
2384 emit_move_insn (tmp, disp_rtx);
2385 disp_rtx = tmp;
2387 emit_insn (gen_adddi3 (spill_fill_data.iter_reg[iter],
2388 spill_fill_data.iter_reg[iter], disp_rtx));
2391 /* Micro-optimization: if we've created a frame pointer, it's at
2392 CFA 0, which may allow the real iterator to be initialized lower,
2393 slightly increasing parallelism. Also, if there are few saves
2394 it may eliminate the iterator entirely. */
2395 else if (disp == 0
2396 && spill_fill_data.init_reg[iter] == stack_pointer_rtx
2397 && frame_pointer_needed)
2399 mem = gen_rtx_MEM (GET_MODE (reg), hard_frame_pointer_rtx);
2400 set_mem_alias_set (mem, get_varargs_alias_set ());
2401 return mem;
2403 else
2405 rtx seq, insn;
2407 if (disp == 0)
2408 seq = gen_movdi (spill_fill_data.iter_reg[iter],
2409 spill_fill_data.init_reg[iter]);
2410 else
2412 start_sequence ();
2414 if (! CONST_OK_FOR_I (disp))
2416 rtx tmp = gen_rtx_REG (DImode, next_scratch_gr_reg ());
2417 emit_move_insn (tmp, disp_rtx);
2418 disp_rtx = tmp;
2421 emit_insn (gen_adddi3 (spill_fill_data.iter_reg[iter],
2422 spill_fill_data.init_reg[iter],
2423 disp_rtx));
2425 seq = get_insns ();
2426 end_sequence ();
2429 /* Careful for being the first insn in a sequence. */
2430 if (spill_fill_data.init_after)
2431 insn = emit_insn_after (seq, spill_fill_data.init_after);
2432 else
2434 rtx first = get_insns ();
2435 if (first)
2436 insn = emit_insn_before (seq, first);
2437 else
2438 insn = emit_insn (seq);
2440 spill_fill_data.init_after = insn;
2442 /* If DISP is 0, we may or may not have a further adjustment
2443 afterward. If we do, then the load/store insn may be modified
2444 to be a post-modify. If we don't, then this copy may be
2445 eliminated by copyprop_hardreg_forward, which makes this
2446 insn garbage, which runs afoul of the sanity check in
2447 propagate_one_insn. So mark this insn as legal to delete. */
2448 if (disp == 0)
2449 REG_NOTES(insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx,
2450 REG_NOTES (insn));
2453 mem = gen_rtx_MEM (GET_MODE (reg), spill_fill_data.iter_reg[iter]);
2455 /* ??? Not all of the spills are for varargs, but some of them are.
2456 The rest of the spills belong in an alias set of their own. But
2457 it doesn't actually hurt to include them here. */
2458 set_mem_alias_set (mem, get_varargs_alias_set ());
2460 spill_fill_data.prev_addr[iter] = &XEXP (mem, 0);
2461 spill_fill_data.prev_off[iter] = cfa_off;
2463 if (++iter >= spill_fill_data.n_iter)
2464 iter = 0;
2465 spill_fill_data.next_iter = iter;
2467 return mem;
2470 static void
2471 do_spill (rtx (*move_fn) (rtx, rtx, rtx), rtx reg, HOST_WIDE_INT cfa_off,
2472 rtx frame_reg)
2474 int iter = spill_fill_data.next_iter;
2475 rtx mem, insn;
2477 mem = spill_restore_mem (reg, cfa_off);
2478 insn = emit_insn ((*move_fn) (mem, reg, GEN_INT (cfa_off)));
2479 spill_fill_data.prev_insn[iter] = insn;
2481 if (frame_reg)
2483 rtx base;
2484 HOST_WIDE_INT off;
2486 RTX_FRAME_RELATED_P (insn) = 1;
2488 /* Don't even pretend that the unwind code can intuit its way
2489 through a pair of interleaved post_modify iterators. Just
2490 provide the correct answer. */
2492 if (frame_pointer_needed)
2494 base = hard_frame_pointer_rtx;
2495 off = - cfa_off;
2497 else
2499 base = stack_pointer_rtx;
2500 off = current_frame_info.total_size - cfa_off;
2503 REG_NOTES (insn)
2504 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
2505 gen_rtx_SET (VOIDmode,
2506 gen_rtx_MEM (GET_MODE (reg),
2507 plus_constant (base, off)),
2508 frame_reg),
2509 REG_NOTES (insn));
2513 static void
2514 do_restore (rtx (*move_fn) (rtx, rtx, rtx), rtx reg, HOST_WIDE_INT cfa_off)
2516 int iter = spill_fill_data.next_iter;
2517 rtx insn;
2519 insn = emit_insn ((*move_fn) (reg, spill_restore_mem (reg, cfa_off),
2520 GEN_INT (cfa_off)));
2521 spill_fill_data.prev_insn[iter] = insn;
2524 /* Wrapper functions that discards the CONST_INT spill offset. These
2525 exist so that we can give gr_spill/gr_fill the offset they need and
2526 use a consistent function interface. */
2528 static rtx
2529 gen_movdi_x (rtx dest, rtx src, rtx offset ATTRIBUTE_UNUSED)
2531 return gen_movdi (dest, src);
2534 static rtx
2535 gen_fr_spill_x (rtx dest, rtx src, rtx offset ATTRIBUTE_UNUSED)
2537 return gen_fr_spill (dest, src);
2540 static rtx
2541 gen_fr_restore_x (rtx dest, rtx src, rtx offset ATTRIBUTE_UNUSED)
2543 return gen_fr_restore (dest, src);
2546 /* Called after register allocation to add any instructions needed for the
2547 prologue. Using a prologue insn is favored compared to putting all of the
2548 instructions in output_function_prologue(), since it allows the scheduler
2549 to intermix instructions with the saves of the caller saved registers. In
2550 some cases, it might be necessary to emit a barrier instruction as the last
2551 insn to prevent such scheduling.
2553 Also any insns generated here should have RTX_FRAME_RELATED_P(insn) = 1
2554 so that the debug info generation code can handle them properly.
2556 The register save area is layed out like so:
2557 cfa+16
2558 [ varargs spill area ]
2559 [ fr register spill area ]
2560 [ br register spill area ]
2561 [ ar register spill area ]
2562 [ pr register spill area ]
2563 [ gr register spill area ] */
2565 /* ??? Get inefficient code when the frame size is larger than can fit in an
2566 adds instruction. */
2568 void
2569 ia64_expand_prologue (void)
2571 rtx insn, ar_pfs_save_reg, ar_unat_save_reg;
2572 int i, epilogue_p, regno, alt_regno, cfa_off, n_varargs;
2573 rtx reg, alt_reg;
2575 ia64_compute_frame_size (get_frame_size ());
2576 last_scratch_gr_reg = 15;
2578 /* If there is no epilogue, then we don't need some prologue insns.
2579 We need to avoid emitting the dead prologue insns, because flow
2580 will complain about them. */
2581 if (optimize)
2583 edge e;
2585 for (e = EXIT_BLOCK_PTR->pred; e ; e = e->pred_next)
2586 if ((e->flags & EDGE_FAKE) == 0
2587 && (e->flags & EDGE_FALLTHRU) != 0)
2588 break;
2589 epilogue_p = (e != NULL);
2591 else
2592 epilogue_p = 1;
2594 /* Set the local, input, and output register names. We need to do this
2595 for GNU libc, which creates crti.S/crtn.S by splitting initfini.c in
2596 half. If we use in/loc/out register names, then we get assembler errors
2597 in crtn.S because there is no alloc insn or regstk directive in there. */
2598 if (! TARGET_REG_NAMES)
2600 int inputs = current_frame_info.n_input_regs;
2601 int locals = current_frame_info.n_local_regs;
2602 int outputs = current_frame_info.n_output_regs;
2604 for (i = 0; i < inputs; i++)
2605 reg_names[IN_REG (i)] = ia64_reg_numbers[i];
2606 for (i = 0; i < locals; i++)
2607 reg_names[LOC_REG (i)] = ia64_reg_numbers[inputs + i];
2608 for (i = 0; i < outputs; i++)
2609 reg_names[OUT_REG (i)] = ia64_reg_numbers[inputs + locals + i];
2612 /* Set the frame pointer register name. The regnum is logically loc79,
2613 but of course we'll not have allocated that many locals. Rather than
2614 worrying about renumbering the existing rtxs, we adjust the name. */
2615 /* ??? This code means that we can never use one local register when
2616 there is a frame pointer. loc79 gets wasted in this case, as it is
2617 renamed to a register that will never be used. See also the try_locals
2618 code in find_gr_spill. */
2619 if (current_frame_info.reg_fp)
2621 const char *tmp = reg_names[HARD_FRAME_POINTER_REGNUM];
2622 reg_names[HARD_FRAME_POINTER_REGNUM]
2623 = reg_names[current_frame_info.reg_fp];
2624 reg_names[current_frame_info.reg_fp] = tmp;
2627 /* We don't need an alloc instruction if we've used no outputs or locals. */
2628 if (current_frame_info.n_local_regs == 0
2629 && current_frame_info.n_output_regs == 0
2630 && current_frame_info.n_input_regs <= current_function_args_info.int_regs
2631 && !TEST_HARD_REG_BIT (current_frame_info.mask, AR_PFS_REGNUM))
2633 /* If there is no alloc, but there are input registers used, then we
2634 need a .regstk directive. */
2635 current_frame_info.need_regstk = (TARGET_REG_NAMES != 0);
2636 ar_pfs_save_reg = NULL_RTX;
2638 else
2640 current_frame_info.need_regstk = 0;
2642 if (current_frame_info.reg_save_ar_pfs)
2643 regno = current_frame_info.reg_save_ar_pfs;
2644 else
2645 regno = next_scratch_gr_reg ();
2646 ar_pfs_save_reg = gen_rtx_REG (DImode, regno);
2648 insn = emit_insn (gen_alloc (ar_pfs_save_reg,
2649 GEN_INT (current_frame_info.n_input_regs),
2650 GEN_INT (current_frame_info.n_local_regs),
2651 GEN_INT (current_frame_info.n_output_regs),
2652 GEN_INT (current_frame_info.n_rotate_regs)));
2653 RTX_FRAME_RELATED_P (insn) = (current_frame_info.reg_save_ar_pfs != 0);
2656 /* Set up frame pointer, stack pointer, and spill iterators. */
2658 n_varargs = cfun->machine->n_varargs;
2659 setup_spill_pointers (current_frame_info.n_spilled + n_varargs,
2660 stack_pointer_rtx, 0);
2662 if (frame_pointer_needed)
2664 insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
2665 RTX_FRAME_RELATED_P (insn) = 1;
2668 if (current_frame_info.total_size != 0)
2670 rtx frame_size_rtx = GEN_INT (- current_frame_info.total_size);
2671 rtx offset;
2673 if (CONST_OK_FOR_I (- current_frame_info.total_size))
2674 offset = frame_size_rtx;
2675 else
2677 regno = next_scratch_gr_reg ();
2678 offset = gen_rtx_REG (DImode, regno);
2679 emit_move_insn (offset, frame_size_rtx);
2682 insn = emit_insn (gen_adddi3 (stack_pointer_rtx,
2683 stack_pointer_rtx, offset));
2685 if (! frame_pointer_needed)
2687 RTX_FRAME_RELATED_P (insn) = 1;
2688 if (GET_CODE (offset) != CONST_INT)
2690 REG_NOTES (insn)
2691 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
2692 gen_rtx_SET (VOIDmode,
2693 stack_pointer_rtx,
2694 gen_rtx_PLUS (DImode,
2695 stack_pointer_rtx,
2696 frame_size_rtx)),
2697 REG_NOTES (insn));
2701 /* ??? At this point we must generate a magic insn that appears to
2702 modify the stack pointer, the frame pointer, and all spill
2703 iterators. This would allow the most scheduling freedom. For
2704 now, just hard stop. */
2705 emit_insn (gen_blockage ());
2708 /* Must copy out ar.unat before doing any integer spills. */
2709 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
2711 if (current_frame_info.reg_save_ar_unat)
2712 ar_unat_save_reg
2713 = gen_rtx_REG (DImode, current_frame_info.reg_save_ar_unat);
2714 else
2716 alt_regno = next_scratch_gr_reg ();
2717 ar_unat_save_reg = gen_rtx_REG (DImode, alt_regno);
2718 current_frame_info.gr_used_mask |= 1 << alt_regno;
2721 reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
2722 insn = emit_move_insn (ar_unat_save_reg, reg);
2723 RTX_FRAME_RELATED_P (insn) = (current_frame_info.reg_save_ar_unat != 0);
2725 /* Even if we're not going to generate an epilogue, we still
2726 need to save the register so that EH works. */
2727 if (! epilogue_p && current_frame_info.reg_save_ar_unat)
2728 emit_insn (gen_prologue_use (ar_unat_save_reg));
2730 else
2731 ar_unat_save_reg = NULL_RTX;
2733 /* Spill all varargs registers. Do this before spilling any GR registers,
2734 since we want the UNAT bits for the GR registers to override the UNAT
2735 bits from varargs, which we don't care about. */
2737 cfa_off = -16;
2738 for (regno = GR_ARG_FIRST + 7; n_varargs > 0; --n_varargs, --regno)
2740 reg = gen_rtx_REG (DImode, regno);
2741 do_spill (gen_gr_spill, reg, cfa_off += 8, NULL_RTX);
2744 /* Locate the bottom of the register save area. */
2745 cfa_off = (current_frame_info.spill_cfa_off
2746 + current_frame_info.spill_size
2747 + current_frame_info.extra_spill_size);
2749 /* Save the predicate register block either in a register or in memory. */
2750 if (TEST_HARD_REG_BIT (current_frame_info.mask, PR_REG (0)))
2752 reg = gen_rtx_REG (DImode, PR_REG (0));
2753 if (current_frame_info.reg_save_pr != 0)
2755 alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_pr);
2756 insn = emit_move_insn (alt_reg, reg);
2758 /* ??? Denote pr spill/fill by a DImode move that modifies all
2759 64 hard registers. */
2760 RTX_FRAME_RELATED_P (insn) = 1;
2761 REG_NOTES (insn)
2762 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
2763 gen_rtx_SET (VOIDmode, alt_reg, reg),
2764 REG_NOTES (insn));
2766 /* Even if we're not going to generate an epilogue, we still
2767 need to save the register so that EH works. */
2768 if (! epilogue_p)
2769 emit_insn (gen_prologue_use (alt_reg));
2771 else
2773 alt_regno = next_scratch_gr_reg ();
2774 alt_reg = gen_rtx_REG (DImode, alt_regno);
2775 insn = emit_move_insn (alt_reg, reg);
2776 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
2777 cfa_off -= 8;
2781 /* Handle AR regs in numerical order. All of them get special handling. */
2782 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM)
2783 && current_frame_info.reg_save_ar_unat == 0)
2785 reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
2786 do_spill (gen_movdi_x, ar_unat_save_reg, cfa_off, reg);
2787 cfa_off -= 8;
2790 /* The alloc insn already copied ar.pfs into a general register. The
2791 only thing we have to do now is copy that register to a stack slot
2792 if we'd not allocated a local register for the job. */
2793 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_PFS_REGNUM)
2794 && current_frame_info.reg_save_ar_pfs == 0)
2796 reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
2797 do_spill (gen_movdi_x, ar_pfs_save_reg, cfa_off, reg);
2798 cfa_off -= 8;
2801 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_LC_REGNUM))
2803 reg = gen_rtx_REG (DImode, AR_LC_REGNUM);
2804 if (current_frame_info.reg_save_ar_lc != 0)
2806 alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_ar_lc);
2807 insn = emit_move_insn (alt_reg, reg);
2808 RTX_FRAME_RELATED_P (insn) = 1;
2810 /* Even if we're not going to generate an epilogue, we still
2811 need to save the register so that EH works. */
2812 if (! epilogue_p)
2813 emit_insn (gen_prologue_use (alt_reg));
2815 else
2817 alt_regno = next_scratch_gr_reg ();
2818 alt_reg = gen_rtx_REG (DImode, alt_regno);
2819 emit_move_insn (alt_reg, reg);
2820 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
2821 cfa_off -= 8;
2825 if (current_frame_info.reg_save_gp)
2827 insn = emit_move_insn (gen_rtx_REG (DImode,
2828 current_frame_info.reg_save_gp),
2829 pic_offset_table_rtx);
2830 /* We don't know for sure yet if this is actually needed, since
2831 we've not split the PIC call patterns. If all of the calls
2832 are indirect, and not followed by any uses of the gp, then
2833 this save is dead. Allow it to go away. */
2834 REG_NOTES (insn)
2835 = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, REG_NOTES (insn));
2838 /* We should now be at the base of the gr/br/fr spill area. */
2839 if (cfa_off != (current_frame_info.spill_cfa_off
2840 + current_frame_info.spill_size))
2841 abort ();
2843 /* Spill all general registers. */
2844 for (regno = GR_REG (1); regno <= GR_REG (31); ++regno)
2845 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
2847 reg = gen_rtx_REG (DImode, regno);
2848 do_spill (gen_gr_spill, reg, cfa_off, reg);
2849 cfa_off -= 8;
2852 /* Handle BR0 specially -- it may be getting stored permanently in
2853 some GR register. */
2854 if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
2856 reg = gen_rtx_REG (DImode, BR_REG (0));
2857 if (current_frame_info.reg_save_b0 != 0)
2859 alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_b0);
2860 insn = emit_move_insn (alt_reg, reg);
2861 RTX_FRAME_RELATED_P (insn) = 1;
2863 /* Even if we're not going to generate an epilogue, we still
2864 need to save the register so that EH works. */
2865 if (! epilogue_p)
2866 emit_insn (gen_prologue_use (alt_reg));
2868 else
2870 alt_regno = next_scratch_gr_reg ();
2871 alt_reg = gen_rtx_REG (DImode, alt_regno);
2872 emit_move_insn (alt_reg, reg);
2873 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
2874 cfa_off -= 8;
2878 /* Spill the rest of the BR registers. */
2879 for (regno = BR_REG (1); regno <= BR_REG (7); ++regno)
2880 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
2882 alt_regno = next_scratch_gr_reg ();
2883 alt_reg = gen_rtx_REG (DImode, alt_regno);
2884 reg = gen_rtx_REG (DImode, regno);
2885 emit_move_insn (alt_reg, reg);
2886 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
2887 cfa_off -= 8;
2890 /* Align the frame and spill all FR registers. */
2891 for (regno = FR_REG (2); regno <= FR_REG (127); ++regno)
2892 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
2894 if (cfa_off & 15)
2895 abort ();
2896 reg = gen_rtx_REG (XFmode, regno);
2897 do_spill (gen_fr_spill_x, reg, cfa_off, reg);
2898 cfa_off -= 16;
2901 if (cfa_off != current_frame_info.spill_cfa_off)
2902 abort ();
2904 finish_spill_pointers ();
2907 /* Called after register allocation to add any instructions needed for the
2908 epilogue. Using an epilogue insn is favored compared to putting all of the
2909 instructions in output_function_prologue(), since it allows the scheduler
2910 to intermix instructions with the saves of the caller saved registers. In
2911 some cases, it might be necessary to emit a barrier instruction as the last
2912 insn to prevent such scheduling. */
2914 void
2915 ia64_expand_epilogue (int sibcall_p)
2917 rtx insn, reg, alt_reg, ar_unat_save_reg;
2918 int regno, alt_regno, cfa_off;
2920 ia64_compute_frame_size (get_frame_size ());
2922 /* If there is a frame pointer, then we use it instead of the stack
2923 pointer, so that the stack pointer does not need to be valid when
2924 the epilogue starts. See EXIT_IGNORE_STACK. */
2925 if (frame_pointer_needed)
2926 setup_spill_pointers (current_frame_info.n_spilled,
2927 hard_frame_pointer_rtx, 0);
2928 else
2929 setup_spill_pointers (current_frame_info.n_spilled, stack_pointer_rtx,
2930 current_frame_info.total_size);
2932 if (current_frame_info.total_size != 0)
2934 /* ??? At this point we must generate a magic insn that appears to
2935 modify the spill iterators and the frame pointer. This would
2936 allow the most scheduling freedom. For now, just hard stop. */
2937 emit_insn (gen_blockage ());
2940 /* Locate the bottom of the register save area. */
2941 cfa_off = (current_frame_info.spill_cfa_off
2942 + current_frame_info.spill_size
2943 + current_frame_info.extra_spill_size);
2945 /* Restore the predicate registers. */
2946 if (TEST_HARD_REG_BIT (current_frame_info.mask, PR_REG (0)))
2948 if (current_frame_info.reg_save_pr != 0)
2949 alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_pr);
2950 else
2952 alt_regno = next_scratch_gr_reg ();
2953 alt_reg = gen_rtx_REG (DImode, alt_regno);
2954 do_restore (gen_movdi_x, alt_reg, cfa_off);
2955 cfa_off -= 8;
2957 reg = gen_rtx_REG (DImode, PR_REG (0));
2958 emit_move_insn (reg, alt_reg);
2961 /* Restore the application registers. */
2963 /* Load the saved unat from the stack, but do not restore it until
2964 after the GRs have been restored. */
2965 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
2967 if (current_frame_info.reg_save_ar_unat != 0)
2968 ar_unat_save_reg
2969 = gen_rtx_REG (DImode, current_frame_info.reg_save_ar_unat);
2970 else
2972 alt_regno = next_scratch_gr_reg ();
2973 ar_unat_save_reg = gen_rtx_REG (DImode, alt_regno);
2974 current_frame_info.gr_used_mask |= 1 << alt_regno;
2975 do_restore (gen_movdi_x, ar_unat_save_reg, cfa_off);
2976 cfa_off -= 8;
2979 else
2980 ar_unat_save_reg = NULL_RTX;
2982 if (current_frame_info.reg_save_ar_pfs != 0)
2984 alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_ar_pfs);
2985 reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
2986 emit_move_insn (reg, alt_reg);
2988 else if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_PFS_REGNUM))
2990 alt_regno = next_scratch_gr_reg ();
2991 alt_reg = gen_rtx_REG (DImode, alt_regno);
2992 do_restore (gen_movdi_x, alt_reg, cfa_off);
2993 cfa_off -= 8;
2994 reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
2995 emit_move_insn (reg, alt_reg);
2998 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_LC_REGNUM))
3000 if (current_frame_info.reg_save_ar_lc != 0)
3001 alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_ar_lc);
3002 else
3004 alt_regno = next_scratch_gr_reg ();
3005 alt_reg = gen_rtx_REG (DImode, alt_regno);
3006 do_restore (gen_movdi_x, alt_reg, cfa_off);
3007 cfa_off -= 8;
3009 reg = gen_rtx_REG (DImode, AR_LC_REGNUM);
3010 emit_move_insn (reg, alt_reg);
3013 /* We should now be at the base of the gr/br/fr spill area. */
3014 if (cfa_off != (current_frame_info.spill_cfa_off
3015 + current_frame_info.spill_size))
3016 abort ();
3018 /* The GP may be stored on the stack in the prologue, but it's
3019 never restored in the epilogue. Skip the stack slot. */
3020 if (TEST_HARD_REG_BIT (current_frame_info.mask, GR_REG (1)))
3021 cfa_off -= 8;
3023 /* Restore all general registers. */
3024 for (regno = GR_REG (2); regno <= GR_REG (31); ++regno)
3025 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3027 reg = gen_rtx_REG (DImode, regno);
3028 do_restore (gen_gr_restore, reg, cfa_off);
3029 cfa_off -= 8;
3032 /* Restore the branch registers. Handle B0 specially, as it may
3033 have gotten stored in some GR register. */
3034 if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
3036 if (current_frame_info.reg_save_b0 != 0)
3037 alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_b0);
3038 else
3040 alt_regno = next_scratch_gr_reg ();
3041 alt_reg = gen_rtx_REG (DImode, alt_regno);
3042 do_restore (gen_movdi_x, alt_reg, cfa_off);
3043 cfa_off -= 8;
3045 reg = gen_rtx_REG (DImode, BR_REG (0));
3046 emit_move_insn (reg, alt_reg);
3049 for (regno = BR_REG (1); regno <= BR_REG (7); ++regno)
3050 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3052 alt_regno = next_scratch_gr_reg ();
3053 alt_reg = gen_rtx_REG (DImode, alt_regno);
3054 do_restore (gen_movdi_x, alt_reg, cfa_off);
3055 cfa_off -= 8;
3056 reg = gen_rtx_REG (DImode, regno);
3057 emit_move_insn (reg, alt_reg);
3060 /* Restore floating point registers. */
3061 for (regno = FR_REG (2); regno <= FR_REG (127); ++regno)
3062 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3064 if (cfa_off & 15)
3065 abort ();
3066 reg = gen_rtx_REG (XFmode, regno);
3067 do_restore (gen_fr_restore_x, reg, cfa_off);
3068 cfa_off -= 16;
3071 /* Restore ar.unat for real. */
3072 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
3074 reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
3075 emit_move_insn (reg, ar_unat_save_reg);
3078 if (cfa_off != current_frame_info.spill_cfa_off)
3079 abort ();
3081 finish_spill_pointers ();
3083 if (current_frame_info.total_size || cfun->machine->ia64_eh_epilogue_sp)
3085 /* ??? At this point we must generate a magic insn that appears to
3086 modify the spill iterators, the stack pointer, and the frame
3087 pointer. This would allow the most scheduling freedom. For now,
3088 just hard stop. */
3089 emit_insn (gen_blockage ());
3092 if (cfun->machine->ia64_eh_epilogue_sp)
3093 emit_move_insn (stack_pointer_rtx, cfun->machine->ia64_eh_epilogue_sp);
3094 else if (frame_pointer_needed)
3096 insn = emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
3097 RTX_FRAME_RELATED_P (insn) = 1;
3099 else if (current_frame_info.total_size)
3101 rtx offset, frame_size_rtx;
3103 frame_size_rtx = GEN_INT (current_frame_info.total_size);
3104 if (CONST_OK_FOR_I (current_frame_info.total_size))
3105 offset = frame_size_rtx;
3106 else
3108 regno = next_scratch_gr_reg ();
3109 offset = gen_rtx_REG (DImode, regno);
3110 emit_move_insn (offset, frame_size_rtx);
3113 insn = emit_insn (gen_adddi3 (stack_pointer_rtx, stack_pointer_rtx,
3114 offset));
3116 RTX_FRAME_RELATED_P (insn) = 1;
3117 if (GET_CODE (offset) != CONST_INT)
3119 REG_NOTES (insn)
3120 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
3121 gen_rtx_SET (VOIDmode,
3122 stack_pointer_rtx,
3123 gen_rtx_PLUS (DImode,
3124 stack_pointer_rtx,
3125 frame_size_rtx)),
3126 REG_NOTES (insn));
3130 if (cfun->machine->ia64_eh_epilogue_bsp)
3131 emit_insn (gen_set_bsp (cfun->machine->ia64_eh_epilogue_bsp));
3133 if (! sibcall_p)
3134 emit_jump_insn (gen_return_internal (gen_rtx_REG (DImode, BR_REG (0))));
3135 else
3137 int fp = GR_REG (2);
3138 /* We need a throw away register here, r0 and r1 are reserved, so r2 is the
3139 first available call clobbered register. If there was a frame_pointer
3140 register, we may have swapped the names of r2 and HARD_FRAME_POINTER_REGNUM,
3141 so we have to make sure we're using the string "r2" when emitting
3142 the register name for the assembler. */
3143 if (current_frame_info.reg_fp && current_frame_info.reg_fp == GR_REG (2))
3144 fp = HARD_FRAME_POINTER_REGNUM;
3146 /* We must emit an alloc to force the input registers to become output
3147 registers. Otherwise, if the callee tries to pass its parameters
3148 through to another call without an intervening alloc, then these
3149 values get lost. */
3150 /* ??? We don't need to preserve all input registers. We only need to
3151 preserve those input registers used as arguments to the sibling call.
3152 It is unclear how to compute that number here. */
3153 if (current_frame_info.n_input_regs != 0)
3154 emit_insn (gen_alloc (gen_rtx_REG (DImode, fp),
3155 GEN_INT (0), GEN_INT (0),
3156 GEN_INT (current_frame_info.n_input_regs),
3157 GEN_INT (0)));
3161 /* Return 1 if br.ret can do all the work required to return from a
3162 function. */
3165 ia64_direct_return (void)
3167 if (reload_completed && ! frame_pointer_needed)
3169 ia64_compute_frame_size (get_frame_size ());
3171 return (current_frame_info.total_size == 0
3172 && current_frame_info.n_spilled == 0
3173 && current_frame_info.reg_save_b0 == 0
3174 && current_frame_info.reg_save_pr == 0
3175 && current_frame_info.reg_save_ar_pfs == 0
3176 && current_frame_info.reg_save_ar_unat == 0
3177 && current_frame_info.reg_save_ar_lc == 0);
3179 return 0;
3182 /* Return the magic cookie that we use to hold the return address
3183 during early compilation. */
3186 ia64_return_addr_rtx (HOST_WIDE_INT count, rtx frame ATTRIBUTE_UNUSED)
3188 if (count != 0)
3189 return NULL;
3190 return gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_RET_ADDR);
3193 /* Split this value after reload, now that we know where the return
3194 address is saved. */
3196 void
3197 ia64_split_return_addr_rtx (rtx dest)
3199 rtx src;
3201 if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
3203 if (current_frame_info.reg_save_b0 != 0)
3204 src = gen_rtx_REG (DImode, current_frame_info.reg_save_b0);
3205 else
3207 HOST_WIDE_INT off;
3208 unsigned int regno;
3210 /* Compute offset from CFA for BR0. */
3211 /* ??? Must be kept in sync with ia64_expand_prologue. */
3212 off = (current_frame_info.spill_cfa_off
3213 + current_frame_info.spill_size);
3214 for (regno = GR_REG (1); regno <= GR_REG (31); ++regno)
3215 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3216 off -= 8;
3218 /* Convert CFA offset to a register based offset. */
3219 if (frame_pointer_needed)
3220 src = hard_frame_pointer_rtx;
3221 else
3223 src = stack_pointer_rtx;
3224 off += current_frame_info.total_size;
3227 /* Load address into scratch register. */
3228 if (CONST_OK_FOR_I (off))
3229 emit_insn (gen_adddi3 (dest, src, GEN_INT (off)));
3230 else
3232 emit_move_insn (dest, GEN_INT (off));
3233 emit_insn (gen_adddi3 (dest, src, dest));
3236 src = gen_rtx_MEM (Pmode, dest);
3239 else
3240 src = gen_rtx_REG (DImode, BR_REG (0));
3242 emit_move_insn (dest, src);
3246 ia64_hard_regno_rename_ok (int from, int to)
3248 /* Don't clobber any of the registers we reserved for the prologue. */
3249 if (to == current_frame_info.reg_fp
3250 || to == current_frame_info.reg_save_b0
3251 || to == current_frame_info.reg_save_pr
3252 || to == current_frame_info.reg_save_ar_pfs
3253 || to == current_frame_info.reg_save_ar_unat
3254 || to == current_frame_info.reg_save_ar_lc)
3255 return 0;
3257 if (from == current_frame_info.reg_fp
3258 || from == current_frame_info.reg_save_b0
3259 || from == current_frame_info.reg_save_pr
3260 || from == current_frame_info.reg_save_ar_pfs
3261 || from == current_frame_info.reg_save_ar_unat
3262 || from == current_frame_info.reg_save_ar_lc)
3263 return 0;
3265 /* Don't use output registers outside the register frame. */
3266 if (OUT_REGNO_P (to) && to >= OUT_REG (current_frame_info.n_output_regs))
3267 return 0;
3269 /* Retain even/oddness on predicate register pairs. */
3270 if (PR_REGNO_P (from) && PR_REGNO_P (to))
3271 return (from & 1) == (to & 1);
3273 return 1;
3276 /* Target hook for assembling integer objects. Handle word-sized
3277 aligned objects and detect the cases when @fptr is needed. */
3279 static bool
3280 ia64_assemble_integer (rtx x, unsigned int size, int aligned_p)
3282 if (size == POINTER_SIZE / BITS_PER_UNIT
3283 && aligned_p
3284 && !(TARGET_NO_PIC || TARGET_AUTO_PIC)
3285 && GET_CODE (x) == SYMBOL_REF
3286 && SYMBOL_REF_FUNCTION_P (x))
3288 if (POINTER_SIZE == 32)
3289 fputs ("\tdata4\t@fptr(", asm_out_file);
3290 else
3291 fputs ("\tdata8\t@fptr(", asm_out_file);
3292 output_addr_const (asm_out_file, x);
3293 fputs (")\n", asm_out_file);
3294 return true;
3296 return default_assemble_integer (x, size, aligned_p);
3299 /* Emit the function prologue. */
3301 static void
3302 ia64_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
3304 int mask, grsave, grsave_prev;
3306 if (current_frame_info.need_regstk)
3307 fprintf (file, "\t.regstk %d, %d, %d, %d\n",
3308 current_frame_info.n_input_regs,
3309 current_frame_info.n_local_regs,
3310 current_frame_info.n_output_regs,
3311 current_frame_info.n_rotate_regs);
3313 if (!flag_unwind_tables && (!flag_exceptions || USING_SJLJ_EXCEPTIONS))
3314 return;
3316 /* Emit the .prologue directive. */
3318 mask = 0;
3319 grsave = grsave_prev = 0;
3320 if (current_frame_info.reg_save_b0 != 0)
3322 mask |= 8;
3323 grsave = grsave_prev = current_frame_info.reg_save_b0;
3325 if (current_frame_info.reg_save_ar_pfs != 0
3326 && (grsave_prev == 0
3327 || current_frame_info.reg_save_ar_pfs == grsave_prev + 1))
3329 mask |= 4;
3330 if (grsave_prev == 0)
3331 grsave = current_frame_info.reg_save_ar_pfs;
3332 grsave_prev = current_frame_info.reg_save_ar_pfs;
3334 if (current_frame_info.reg_fp != 0
3335 && (grsave_prev == 0
3336 || current_frame_info.reg_fp == grsave_prev + 1))
3338 mask |= 2;
3339 if (grsave_prev == 0)
3340 grsave = HARD_FRAME_POINTER_REGNUM;
3341 grsave_prev = current_frame_info.reg_fp;
3343 if (current_frame_info.reg_save_pr != 0
3344 && (grsave_prev == 0
3345 || current_frame_info.reg_save_pr == grsave_prev + 1))
3347 mask |= 1;
3348 if (grsave_prev == 0)
3349 grsave = current_frame_info.reg_save_pr;
3352 if (mask && TARGET_GNU_AS)
3353 fprintf (file, "\t.prologue %d, %d\n", mask,
3354 ia64_dbx_register_number (grsave));
3355 else
3356 fputs ("\t.prologue\n", file);
3358 /* Emit a .spill directive, if necessary, to relocate the base of
3359 the register spill area. */
3360 if (current_frame_info.spill_cfa_off != -16)
3361 fprintf (file, "\t.spill %ld\n",
3362 (long) (current_frame_info.spill_cfa_off
3363 + current_frame_info.spill_size));
3366 /* Emit the .body directive at the scheduled end of the prologue. */
3368 static void
3369 ia64_output_function_end_prologue (FILE *file)
3371 if (!flag_unwind_tables && (!flag_exceptions || USING_SJLJ_EXCEPTIONS))
3372 return;
3374 fputs ("\t.body\n", file);
3377 /* Emit the function epilogue. */
3379 static void
3380 ia64_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
3381 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
3383 int i;
3385 if (current_frame_info.reg_fp)
3387 const char *tmp = reg_names[HARD_FRAME_POINTER_REGNUM];
3388 reg_names[HARD_FRAME_POINTER_REGNUM]
3389 = reg_names[current_frame_info.reg_fp];
3390 reg_names[current_frame_info.reg_fp] = tmp;
3392 if (! TARGET_REG_NAMES)
3394 for (i = 0; i < current_frame_info.n_input_regs; i++)
3395 reg_names[IN_REG (i)] = ia64_input_reg_names[i];
3396 for (i = 0; i < current_frame_info.n_local_regs; i++)
3397 reg_names[LOC_REG (i)] = ia64_local_reg_names[i];
3398 for (i = 0; i < current_frame_info.n_output_regs; i++)
3399 reg_names[OUT_REG (i)] = ia64_output_reg_names[i];
3402 current_frame_info.initialized = 0;
3406 ia64_dbx_register_number (int regno)
3408 /* In ia64_expand_prologue we quite literally renamed the frame pointer
3409 from its home at loc79 to something inside the register frame. We
3410 must perform the same renumbering here for the debug info. */
3411 if (current_frame_info.reg_fp)
3413 if (regno == HARD_FRAME_POINTER_REGNUM)
3414 regno = current_frame_info.reg_fp;
3415 else if (regno == current_frame_info.reg_fp)
3416 regno = HARD_FRAME_POINTER_REGNUM;
3419 if (IN_REGNO_P (regno))
3420 return 32 + regno - IN_REG (0);
3421 else if (LOC_REGNO_P (regno))
3422 return 32 + current_frame_info.n_input_regs + regno - LOC_REG (0);
3423 else if (OUT_REGNO_P (regno))
3424 return (32 + current_frame_info.n_input_regs
3425 + current_frame_info.n_local_regs + regno - OUT_REG (0));
3426 else
3427 return regno;
3430 void
3431 ia64_initialize_trampoline (rtx addr, rtx fnaddr, rtx static_chain)
3433 rtx addr_reg, eight = GEN_INT (8);
3435 /* The Intel assembler requires that the global __ia64_trampoline symbol
3436 be declared explicitly */
3437 if (!TARGET_GNU_AS)
3439 static bool declared_ia64_trampoline = false;
3441 if (!declared_ia64_trampoline)
3443 declared_ia64_trampoline = true;
3444 (*targetm.asm_out.globalize_label) (asm_out_file,
3445 "__ia64_trampoline");
3449 /* Load up our iterator. */
3450 addr_reg = gen_reg_rtx (Pmode);
3451 emit_move_insn (addr_reg, addr);
3453 /* The first two words are the fake descriptor:
3454 __ia64_trampoline, ADDR+16. */
3455 emit_move_insn (gen_rtx_MEM (Pmode, addr_reg),
3456 gen_rtx_SYMBOL_REF (Pmode, "__ia64_trampoline"));
3457 emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
3459 emit_move_insn (gen_rtx_MEM (Pmode, addr_reg),
3460 copy_to_reg (plus_constant (addr, 16)));
3461 emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
3463 /* The third word is the target descriptor. */
3464 emit_move_insn (gen_rtx_MEM (Pmode, addr_reg), fnaddr);
3465 emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
3467 /* The fourth word is the static chain. */
3468 emit_move_insn (gen_rtx_MEM (Pmode, addr_reg), static_chain);
3471 /* Do any needed setup for a variadic function. CUM has not been updated
3472 for the last named argument which has type TYPE and mode MODE.
3474 We generate the actual spill instructions during prologue generation. */
3476 void
3477 ia64_setup_incoming_varargs (CUMULATIVE_ARGS cum, int int_mode, tree type,
3478 int * pretend_size,
3479 int second_time ATTRIBUTE_UNUSED)
3481 /* Skip the current argument. */
3482 ia64_function_arg_advance (&cum, int_mode, type, 1);
3484 if (cum.words < MAX_ARGUMENT_SLOTS)
3486 int n = MAX_ARGUMENT_SLOTS - cum.words;
3487 *pretend_size = n * UNITS_PER_WORD;
3488 cfun->machine->n_varargs = n;
3492 /* Check whether TYPE is a homogeneous floating point aggregate. If
3493 it is, return the mode of the floating point type that appears
3494 in all leafs. If it is not, return VOIDmode.
3496 An aggregate is a homogeneous floating point aggregate is if all
3497 fields/elements in it have the same floating point type (e.g,
3498 SFmode). 128-bit quad-precision floats are excluded. */
3500 static enum machine_mode
3501 hfa_element_mode (tree type, int nested)
3503 enum machine_mode element_mode = VOIDmode;
3504 enum machine_mode mode;
3505 enum tree_code code = TREE_CODE (type);
3506 int know_element_mode = 0;
3507 tree t;
3509 switch (code)
3511 case VOID_TYPE: case INTEGER_TYPE: case ENUMERAL_TYPE:
3512 case BOOLEAN_TYPE: case CHAR_TYPE: case POINTER_TYPE:
3513 case OFFSET_TYPE: case REFERENCE_TYPE: case METHOD_TYPE:
3514 case FILE_TYPE: case SET_TYPE: case LANG_TYPE:
3515 case FUNCTION_TYPE:
3516 return VOIDmode;
3518 /* Fortran complex types are supposed to be HFAs, so we need to handle
3519 gcc's COMPLEX_TYPEs as HFAs. We need to exclude the integral complex
3520 types though. */
3521 case COMPLEX_TYPE:
3522 if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_COMPLEX_FLOAT
3523 && TYPE_MODE (type) != TCmode)
3524 return GET_MODE_INNER (TYPE_MODE (type));
3525 else
3526 return VOIDmode;
3528 case REAL_TYPE:
3529 /* We want to return VOIDmode for raw REAL_TYPEs, but the actual
3530 mode if this is contained within an aggregate. */
3531 if (nested && TYPE_MODE (type) != TFmode)
3532 return TYPE_MODE (type);
3533 else
3534 return VOIDmode;
3536 case ARRAY_TYPE:
3537 return hfa_element_mode (TREE_TYPE (type), 1);
3539 case RECORD_TYPE:
3540 case UNION_TYPE:
3541 case QUAL_UNION_TYPE:
3542 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
3544 if (TREE_CODE (t) != FIELD_DECL)
3545 continue;
3547 mode = hfa_element_mode (TREE_TYPE (t), 1);
3548 if (know_element_mode)
3550 if (mode != element_mode)
3551 return VOIDmode;
3553 else if (GET_MODE_CLASS (mode) != MODE_FLOAT)
3554 return VOIDmode;
3555 else
3557 know_element_mode = 1;
3558 element_mode = mode;
3561 return element_mode;
3563 default:
3564 /* If we reach here, we probably have some front-end specific type
3565 that the backend doesn't know about. This can happen via the
3566 aggregate_value_p call in init_function_start. All we can do is
3567 ignore unknown tree types. */
3568 return VOIDmode;
3571 return VOIDmode;
3574 /* Return the number of words required to hold a quantity of TYPE and MODE
3575 when passed as an argument. */
3576 static int
3577 ia64_function_arg_words (tree type, enum machine_mode mode)
3579 int words;
3581 if (mode == BLKmode)
3582 words = int_size_in_bytes (type);
3583 else
3584 words = GET_MODE_SIZE (mode);
3586 return (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD; /* round up */
3589 /* Return the number of registers that should be skipped so the current
3590 argument (described by TYPE and WORDS) will be properly aligned.
3592 Integer and float arguments larger than 8 bytes start at the next
3593 even boundary. Aggregates larger than 8 bytes start at the next
3594 even boundary if the aggregate has 16 byte alignment. Note that
3595 in the 32-bit ABI, TImode and TFmode have only 8-byte alignment
3596 but are still to be aligned in registers.
3598 ??? The ABI does not specify how to handle aggregates with
3599 alignment from 9 to 15 bytes, or greater than 16. We handle them
3600 all as if they had 16 byte alignment. Such aggregates can occur
3601 only if gcc extensions are used. */
3602 static int
3603 ia64_function_arg_offset (CUMULATIVE_ARGS *cum, tree type, int words)
3605 if ((cum->words & 1) == 0)
3606 return 0;
3608 if (type
3609 && TREE_CODE (type) != INTEGER_TYPE
3610 && TREE_CODE (type) != REAL_TYPE)
3611 return TYPE_ALIGN (type) > 8 * BITS_PER_UNIT;
3612 else
3613 return words > 1;
3616 /* Return rtx for register where argument is passed, or zero if it is passed
3617 on the stack. */
3618 /* ??? 128-bit quad-precision floats are always passed in general
3619 registers. */
3622 ia64_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, tree type,
3623 int named, int incoming)
3625 int basereg = (incoming ? GR_ARG_FIRST : AR_ARG_FIRST);
3626 int words = ia64_function_arg_words (type, mode);
3627 int offset = ia64_function_arg_offset (cum, type, words);
3628 enum machine_mode hfa_mode = VOIDmode;
3630 /* If all argument slots are used, then it must go on the stack. */
3631 if (cum->words + offset >= MAX_ARGUMENT_SLOTS)
3632 return 0;
3634 /* Check for and handle homogeneous FP aggregates. */
3635 if (type)
3636 hfa_mode = hfa_element_mode (type, 0);
3638 /* Unnamed prototyped hfas are passed as usual. Named prototyped hfas
3639 and unprototyped hfas are passed specially. */
3640 if (hfa_mode != VOIDmode && (! cum->prototype || named))
3642 rtx loc[16];
3643 int i = 0;
3644 int fp_regs = cum->fp_regs;
3645 int int_regs = cum->words + offset;
3646 int hfa_size = GET_MODE_SIZE (hfa_mode);
3647 int byte_size;
3648 int args_byte_size;
3650 /* If prototyped, pass it in FR regs then GR regs.
3651 If not prototyped, pass it in both FR and GR regs.
3653 If this is an SFmode aggregate, then it is possible to run out of
3654 FR regs while GR regs are still left. In that case, we pass the
3655 remaining part in the GR regs. */
3657 /* Fill the FP regs. We do this always. We stop if we reach the end
3658 of the argument, the last FP register, or the last argument slot. */
3660 byte_size = ((mode == BLKmode)
3661 ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
3662 args_byte_size = int_regs * UNITS_PER_WORD;
3663 offset = 0;
3664 for (; (offset < byte_size && fp_regs < MAX_ARGUMENT_SLOTS
3665 && args_byte_size < (MAX_ARGUMENT_SLOTS * UNITS_PER_WORD)); i++)
3667 loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
3668 gen_rtx_REG (hfa_mode, (FR_ARG_FIRST
3669 + fp_regs)),
3670 GEN_INT (offset));
3671 offset += hfa_size;
3672 args_byte_size += hfa_size;
3673 fp_regs++;
3676 /* If no prototype, then the whole thing must go in GR regs. */
3677 if (! cum->prototype)
3678 offset = 0;
3679 /* If this is an SFmode aggregate, then we might have some left over
3680 that needs to go in GR regs. */
3681 else if (byte_size != offset)
3682 int_regs += offset / UNITS_PER_WORD;
3684 /* Fill in the GR regs. We must use DImode here, not the hfa mode. */
3686 for (; offset < byte_size && int_regs < MAX_ARGUMENT_SLOTS; i++)
3688 enum machine_mode gr_mode = DImode;
3689 unsigned int gr_size;
3691 /* If we have an odd 4 byte hunk because we ran out of FR regs,
3692 then this goes in a GR reg left adjusted/little endian, right
3693 adjusted/big endian. */
3694 /* ??? Currently this is handled wrong, because 4-byte hunks are
3695 always right adjusted/little endian. */
3696 if (offset & 0x4)
3697 gr_mode = SImode;
3698 /* If we have an even 4 byte hunk because the aggregate is a
3699 multiple of 4 bytes in size, then this goes in a GR reg right
3700 adjusted/little endian. */
3701 else if (byte_size - offset == 4)
3702 gr_mode = SImode;
3704 loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
3705 gen_rtx_REG (gr_mode, (basereg
3706 + int_regs)),
3707 GEN_INT (offset));
3709 gr_size = GET_MODE_SIZE (gr_mode);
3710 offset += gr_size;
3711 if (gr_size == UNITS_PER_WORD
3712 || (gr_size < UNITS_PER_WORD && offset % UNITS_PER_WORD == 0))
3713 int_regs++;
3714 else if (gr_size > UNITS_PER_WORD)
3715 int_regs += gr_size / UNITS_PER_WORD;
3718 /* If we ended up using just one location, just return that one loc, but
3719 change the mode back to the argument mode. */
3720 if (i == 1)
3721 return gen_rtx_REG (mode, REGNO (XEXP (loc[0], 0)));
3722 else
3723 return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
3726 /* Integral and aggregates go in general registers. If we have run out of
3727 FR registers, then FP values must also go in general registers. This can
3728 happen when we have a SFmode HFA. */
3729 else if (mode == TFmode || mode == TCmode
3730 || (! FLOAT_MODE_P (mode) || cum->fp_regs == MAX_ARGUMENT_SLOTS))
3732 int byte_size = ((mode == BLKmode)
3733 ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
3734 if (BYTES_BIG_ENDIAN
3735 && (mode == BLKmode || (type && AGGREGATE_TYPE_P (type)))
3736 && byte_size < UNITS_PER_WORD
3737 && byte_size > 0)
3739 rtx gr_reg = gen_rtx_EXPR_LIST (VOIDmode,
3740 gen_rtx_REG (DImode,
3741 (basereg + cum->words
3742 + offset)),
3743 const0_rtx);
3744 return gen_rtx_PARALLEL (mode, gen_rtvec (1, gr_reg));
3746 else
3747 return gen_rtx_REG (mode, basereg + cum->words + offset);
3751 /* If there is a prototype, then FP values go in a FR register when
3752 named, and in a GR register when unnamed. */
3753 else if (cum->prototype)
3755 if (named)
3756 return gen_rtx_REG (mode, FR_ARG_FIRST + cum->fp_regs);
3757 /* In big-endian mode, an anonymous SFmode value must be represented
3758 as (parallel:SF [(expr_list (reg:DI n) (const_int 0))]) to force
3759 the value into the high half of the general register. */
3760 else if (BYTES_BIG_ENDIAN && mode == SFmode)
3761 return gen_rtx_PARALLEL (mode,
3762 gen_rtvec (1,
3763 gen_rtx_EXPR_LIST (VOIDmode,
3764 gen_rtx_REG (DImode, basereg + cum->words + offset),
3765 const0_rtx)));
3766 else
3767 return gen_rtx_REG (mode, basereg + cum->words + offset);
3769 /* If there is no prototype, then FP values go in both FR and GR
3770 registers. */
3771 else
3773 /* See comment above. */
3774 enum machine_mode inner_mode =
3775 (BYTES_BIG_ENDIAN && mode == SFmode) ? DImode : mode;
3777 rtx fp_reg = gen_rtx_EXPR_LIST (VOIDmode,
3778 gen_rtx_REG (mode, (FR_ARG_FIRST
3779 + cum->fp_regs)),
3780 const0_rtx);
3781 rtx gr_reg = gen_rtx_EXPR_LIST (VOIDmode,
3782 gen_rtx_REG (inner_mode,
3783 (basereg + cum->words
3784 + offset)),
3785 const0_rtx);
3787 return gen_rtx_PARALLEL (mode, gen_rtvec (2, fp_reg, gr_reg));
3791 /* Return number of words, at the beginning of the argument, that must be
3792 put in registers. 0 is the argument is entirely in registers or entirely
3793 in memory. */
3796 ia64_function_arg_partial_nregs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3797 tree type, int named ATTRIBUTE_UNUSED)
3799 int words = ia64_function_arg_words (type, mode);
3800 int offset = ia64_function_arg_offset (cum, type, words);
3802 /* If all argument slots are used, then it must go on the stack. */
3803 if (cum->words + offset >= MAX_ARGUMENT_SLOTS)
3804 return 0;
3806 /* It doesn't matter whether the argument goes in FR or GR regs. If
3807 it fits within the 8 argument slots, then it goes entirely in
3808 registers. If it extends past the last argument slot, then the rest
3809 goes on the stack. */
3811 if (words + cum->words + offset <= MAX_ARGUMENT_SLOTS)
3812 return 0;
3814 return MAX_ARGUMENT_SLOTS - cum->words - offset;
3817 /* Update CUM to point after this argument. This is patterned after
3818 ia64_function_arg. */
3820 void
3821 ia64_function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3822 tree type, int named)
3824 int words = ia64_function_arg_words (type, mode);
3825 int offset = ia64_function_arg_offset (cum, type, words);
3826 enum machine_mode hfa_mode = VOIDmode;
3828 /* If all arg slots are already full, then there is nothing to do. */
3829 if (cum->words >= MAX_ARGUMENT_SLOTS)
3830 return;
3832 cum->words += words + offset;
3834 /* Check for and handle homogeneous FP aggregates. */
3835 if (type)
3836 hfa_mode = hfa_element_mode (type, 0);
3838 /* Unnamed prototyped hfas are passed as usual. Named prototyped hfas
3839 and unprototyped hfas are passed specially. */
3840 if (hfa_mode != VOIDmode && (! cum->prototype || named))
3842 int fp_regs = cum->fp_regs;
3843 /* This is the original value of cum->words + offset. */
3844 int int_regs = cum->words - words;
3845 int hfa_size = GET_MODE_SIZE (hfa_mode);
3846 int byte_size;
3847 int args_byte_size;
3849 /* If prototyped, pass it in FR regs then GR regs.
3850 If not prototyped, pass it in both FR and GR regs.
3852 If this is an SFmode aggregate, then it is possible to run out of
3853 FR regs while GR regs are still left. In that case, we pass the
3854 remaining part in the GR regs. */
3856 /* Fill the FP regs. We do this always. We stop if we reach the end
3857 of the argument, the last FP register, or the last argument slot. */
3859 byte_size = ((mode == BLKmode)
3860 ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
3861 args_byte_size = int_regs * UNITS_PER_WORD;
3862 offset = 0;
3863 for (; (offset < byte_size && fp_regs < MAX_ARGUMENT_SLOTS
3864 && args_byte_size < (MAX_ARGUMENT_SLOTS * UNITS_PER_WORD));)
3866 offset += hfa_size;
3867 args_byte_size += hfa_size;
3868 fp_regs++;
3871 cum->fp_regs = fp_regs;
3874 /* Integral and aggregates go in general registers. If we have run out of
3875 FR registers, then FP values must also go in general registers. This can
3876 happen when we have a SFmode HFA. */
3877 else if (! FLOAT_MODE_P (mode) || cum->fp_regs == MAX_ARGUMENT_SLOTS)
3878 cum->int_regs = cum->words;
3880 /* If there is a prototype, then FP values go in a FR register when
3881 named, and in a GR register when unnamed. */
3882 else if (cum->prototype)
3884 if (! named)
3885 cum->int_regs = cum->words;
3886 else
3887 /* ??? Complex types should not reach here. */
3888 cum->fp_regs += (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT ? 2 : 1);
3890 /* If there is no prototype, then FP values go in both FR and GR
3891 registers. */
3892 else
3894 /* ??? Complex types should not reach here. */
3895 cum->fp_regs += (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT ? 2 : 1);
3896 cum->int_regs = cum->words;
3900 /* Variable sized types are passed by reference. */
3901 /* ??? At present this is a GCC extension to the IA-64 ABI. */
3904 ia64_function_arg_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3905 enum machine_mode mode ATTRIBUTE_UNUSED,
3906 tree type, int named ATTRIBUTE_UNUSED)
3908 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
3911 /* True if it is OK to do sibling call optimization for the specified
3912 call expression EXP. DECL will be the called function, or NULL if
3913 this is an indirect call. */
3914 static bool
3915 ia64_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3917 /* We must always return with our current GP. This means we can
3918 only sibcall to functions defined in the current module. */
3919 return decl && (*targetm.binds_local_p) (decl);
3923 /* Implement va_arg. */
3926 ia64_va_arg (tree valist, tree type)
3928 tree t;
3930 /* Variable sized types are passed by reference. */
3931 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
3933 rtx addr = force_reg (ptr_mode,
3934 std_expand_builtin_va_arg (valist, build_pointer_type (type)));
3935 #ifdef POINTERS_EXTEND_UNSIGNED
3936 addr = convert_memory_address (Pmode, addr);
3937 #endif
3938 return gen_rtx_MEM (ptr_mode, addr);
3941 /* Aggregate arguments with alignment larger than 8 bytes start at
3942 the next even boundary. Integer and floating point arguments
3943 do so if they are larger than 8 bytes, whether or not they are
3944 also aligned larger than 8 bytes. */
3945 if ((TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == INTEGER_TYPE)
3946 ? int_size_in_bytes (type) > 8 : TYPE_ALIGN (type) > 8 * BITS_PER_UNIT)
3948 t = build (PLUS_EXPR, TREE_TYPE (valist), valist,
3949 build_int_2 (2 * UNITS_PER_WORD - 1, 0));
3950 t = build (BIT_AND_EXPR, TREE_TYPE (t), t,
3951 build_int_2 (-2 * UNITS_PER_WORD, -1));
3952 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
3953 TREE_SIDE_EFFECTS (t) = 1;
3954 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3957 return std_expand_builtin_va_arg (valist, type);
3960 /* Return 1 if function return value returned in memory. Return 0 if it is
3961 in a register. */
3964 ia64_return_in_memory (tree valtype)
3966 enum machine_mode mode;
3967 enum machine_mode hfa_mode;
3968 HOST_WIDE_INT byte_size;
3970 mode = TYPE_MODE (valtype);
3971 byte_size = GET_MODE_SIZE (mode);
3972 if (mode == BLKmode)
3974 byte_size = int_size_in_bytes (valtype);
3975 if (byte_size < 0)
3976 return 1;
3979 /* Hfa's with up to 8 elements are returned in the FP argument registers. */
3981 hfa_mode = hfa_element_mode (valtype, 0);
3982 if (hfa_mode != VOIDmode)
3984 int hfa_size = GET_MODE_SIZE (hfa_mode);
3986 if (byte_size / hfa_size > MAX_ARGUMENT_SLOTS)
3987 return 1;
3988 else
3989 return 0;
3991 else if (byte_size > UNITS_PER_WORD * MAX_INT_RETURN_SLOTS)
3992 return 1;
3993 else
3994 return 0;
3997 /* Return rtx for register that holds the function return value. */
4000 ia64_function_value (tree valtype, tree func ATTRIBUTE_UNUSED)
4002 enum machine_mode mode;
4003 enum machine_mode hfa_mode;
4005 mode = TYPE_MODE (valtype);
4006 hfa_mode = hfa_element_mode (valtype, 0);
4008 if (hfa_mode != VOIDmode)
4010 rtx loc[8];
4011 int i;
4012 int hfa_size;
4013 int byte_size;
4014 int offset;
4016 hfa_size = GET_MODE_SIZE (hfa_mode);
4017 byte_size = ((mode == BLKmode)
4018 ? int_size_in_bytes (valtype) : GET_MODE_SIZE (mode));
4019 offset = 0;
4020 for (i = 0; offset < byte_size; i++)
4022 loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
4023 gen_rtx_REG (hfa_mode, FR_ARG_FIRST + i),
4024 GEN_INT (offset));
4025 offset += hfa_size;
4028 if (i == 1)
4029 return XEXP (loc[0], 0);
4030 else
4031 return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
4033 else if (FLOAT_TYPE_P (valtype) && mode != TFmode && mode != TCmode)
4034 return gen_rtx_REG (mode, FR_ARG_FIRST);
4035 else
4037 if (BYTES_BIG_ENDIAN
4038 && (mode == BLKmode || (valtype && AGGREGATE_TYPE_P (valtype))))
4040 rtx loc[8];
4041 int offset;
4042 int bytesize;
4043 int i;
4045 offset = 0;
4046 bytesize = int_size_in_bytes (valtype);
4047 for (i = 0; offset < bytesize; i++)
4049 loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
4050 gen_rtx_REG (DImode,
4051 GR_RET_FIRST + i),
4052 GEN_INT (offset));
4053 offset += UNITS_PER_WORD;
4055 return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
4057 else
4058 return gen_rtx_REG (mode, GR_RET_FIRST);
4062 /* This is called from dwarf2out.c via ASM_OUTPUT_DWARF_DTPREL.
4063 We need to emit DTP-relative relocations. */
4065 void
4066 ia64_output_dwarf_dtprel (FILE *file, int size, rtx x)
4068 if (size != 8)
4069 abort ();
4070 fputs ("\tdata8.ua\t@dtprel(", file);
4071 output_addr_const (file, x);
4072 fputs (")", file);
4075 /* Print a memory address as an operand to reference that memory location. */
4077 /* ??? Do we need this? It gets used only for 'a' operands. We could perhaps
4078 also call this from ia64_print_operand for memory addresses. */
4080 void
4081 ia64_print_operand_address (FILE * stream ATTRIBUTE_UNUSED,
4082 rtx address ATTRIBUTE_UNUSED)
4086 /* Print an operand to an assembler instruction.
4087 C Swap and print a comparison operator.
4088 D Print an FP comparison operator.
4089 E Print 32 - constant, for SImode shifts as extract.
4090 e Print 64 - constant, for DImode rotates.
4091 F A floating point constant 0.0 emitted as f0, or 1.0 emitted as f1, or
4092 a floating point register emitted normally.
4093 I Invert a predicate register by adding 1.
4094 J Select the proper predicate register for a condition.
4095 j Select the inverse predicate register for a condition.
4096 O Append .acq for volatile load.
4097 P Postincrement of a MEM.
4098 Q Append .rel for volatile store.
4099 S Shift amount for shladd instruction.
4100 T Print an 8-bit sign extended number (K) as a 32-bit unsigned number
4101 for Intel assembler.
4102 U Print an 8-bit sign extended number (K) as a 64-bit unsigned number
4103 for Intel assembler.
4104 r Print register name, or constant 0 as r0. HP compatibility for
4105 Linux kernel. */
4106 void
4107 ia64_print_operand (FILE * file, rtx x, int code)
4109 const char *str;
4111 switch (code)
4113 case 0:
4114 /* Handled below. */
4115 break;
4117 case 'C':
4119 enum rtx_code c = swap_condition (GET_CODE (x));
4120 fputs (GET_RTX_NAME (c), file);
4121 return;
4124 case 'D':
4125 switch (GET_CODE (x))
4127 case NE:
4128 str = "neq";
4129 break;
4130 case UNORDERED:
4131 str = "unord";
4132 break;
4133 case ORDERED:
4134 str = "ord";
4135 break;
4136 default:
4137 str = GET_RTX_NAME (GET_CODE (x));
4138 break;
4140 fputs (str, file);
4141 return;
4143 case 'E':
4144 fprintf (file, HOST_WIDE_INT_PRINT_DEC, 32 - INTVAL (x));
4145 return;
4147 case 'e':
4148 fprintf (file, HOST_WIDE_INT_PRINT_DEC, 64 - INTVAL (x));
4149 return;
4151 case 'F':
4152 if (x == CONST0_RTX (GET_MODE (x)))
4153 str = reg_names [FR_REG (0)];
4154 else if (x == CONST1_RTX (GET_MODE (x)))
4155 str = reg_names [FR_REG (1)];
4156 else if (GET_CODE (x) == REG)
4157 str = reg_names [REGNO (x)];
4158 else
4159 abort ();
4160 fputs (str, file);
4161 return;
4163 case 'I':
4164 fputs (reg_names [REGNO (x) + 1], file);
4165 return;
4167 case 'J':
4168 case 'j':
4170 unsigned int regno = REGNO (XEXP (x, 0));
4171 if (GET_CODE (x) == EQ)
4172 regno += 1;
4173 if (code == 'j')
4174 regno ^= 1;
4175 fputs (reg_names [regno], file);
4177 return;
4179 case 'O':
4180 if (MEM_VOLATILE_P (x))
4181 fputs(".acq", file);
4182 return;
4184 case 'P':
4186 HOST_WIDE_INT value;
4188 switch (GET_CODE (XEXP (x, 0)))
4190 default:
4191 return;
4193 case POST_MODIFY:
4194 x = XEXP (XEXP (XEXP (x, 0), 1), 1);
4195 if (GET_CODE (x) == CONST_INT)
4196 value = INTVAL (x);
4197 else if (GET_CODE (x) == REG)
4199 fprintf (file, ", %s", reg_names[REGNO (x)]);
4200 return;
4202 else
4203 abort ();
4204 break;
4206 case POST_INC:
4207 value = GET_MODE_SIZE (GET_MODE (x));
4208 break;
4210 case POST_DEC:
4211 value = - (HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (x));
4212 break;
4215 fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC, value);
4216 return;
4219 case 'Q':
4220 if (MEM_VOLATILE_P (x))
4221 fputs(".rel", file);
4222 return;
4224 case 'S':
4225 fprintf (file, "%d", exact_log2 (INTVAL (x)));
4226 return;
4228 case 'T':
4229 if (! TARGET_GNU_AS && GET_CODE (x) == CONST_INT)
4231 fprintf (file, "0x%x", (int) INTVAL (x) & 0xffffffff);
4232 return;
4234 break;
4236 case 'U':
4237 if (! TARGET_GNU_AS && GET_CODE (x) == CONST_INT)
4239 const char *prefix = "0x";
4240 if (INTVAL (x) & 0x80000000)
4242 fprintf (file, "0xffffffff");
4243 prefix = "";
4245 fprintf (file, "%s%x", prefix, (int) INTVAL (x) & 0xffffffff);
4246 return;
4248 break;
4250 case 'r':
4251 /* If this operand is the constant zero, write it as register zero.
4252 Any register, zero, or CONST_INT value is OK here. */
4253 if (GET_CODE (x) == REG)
4254 fputs (reg_names[REGNO (x)], file);
4255 else if (x == CONST0_RTX (GET_MODE (x)))
4256 fputs ("r0", file);
4257 else if (GET_CODE (x) == CONST_INT)
4258 output_addr_const (file, x);
4259 else
4260 output_operand_lossage ("invalid %%r value");
4261 return;
4263 case '+':
4265 const char *which;
4267 /* For conditional branches, returns or calls, substitute
4268 sptk, dptk, dpnt, or spnt for %s. */
4269 x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
4270 if (x)
4272 int pred_val = INTVAL (XEXP (x, 0));
4274 /* Guess top and bottom 10% statically predicted. */
4275 if (pred_val < REG_BR_PROB_BASE / 50)
4276 which = ".spnt";
4277 else if (pred_val < REG_BR_PROB_BASE / 2)
4278 which = ".dpnt";
4279 else if (pred_val < REG_BR_PROB_BASE / 100 * 98)
4280 which = ".dptk";
4281 else
4282 which = ".sptk";
4284 else if (GET_CODE (current_output_insn) == CALL_INSN)
4285 which = ".sptk";
4286 else
4287 which = ".dptk";
4289 fputs (which, file);
4290 return;
4293 case ',':
4294 x = current_insn_predicate;
4295 if (x)
4297 unsigned int regno = REGNO (XEXP (x, 0));
4298 if (GET_CODE (x) == EQ)
4299 regno += 1;
4300 fprintf (file, "(%s) ", reg_names [regno]);
4302 return;
4304 default:
4305 output_operand_lossage ("ia64_print_operand: unknown code");
4306 return;
4309 switch (GET_CODE (x))
4311 /* This happens for the spill/restore instructions. */
4312 case POST_INC:
4313 case POST_DEC:
4314 case POST_MODIFY:
4315 x = XEXP (x, 0);
4316 /* ... fall through ... */
4318 case REG:
4319 fputs (reg_names [REGNO (x)], file);
4320 break;
4322 case MEM:
4324 rtx addr = XEXP (x, 0);
4325 if (GET_RTX_CLASS (GET_CODE (addr)) == 'a')
4326 addr = XEXP (addr, 0);
4327 fprintf (file, "[%s]", reg_names [REGNO (addr)]);
4328 break;
4331 default:
4332 output_addr_const (file, x);
4333 break;
4336 return;
4339 /* Compute a (partial) cost for rtx X. Return true if the complete
4340 cost has been computed, and false if subexpressions should be
4341 scanned. In either case, *TOTAL contains the cost result. */
4342 /* ??? This is incomplete. */
4344 static bool
4345 ia64_rtx_costs (rtx x, int code, int outer_code, int *total)
4347 switch (code)
4349 case CONST_INT:
4350 switch (outer_code)
4352 case SET:
4353 *total = CONST_OK_FOR_J (INTVAL (x)) ? 0 : COSTS_N_INSNS (1);
4354 return true;
4355 case PLUS:
4356 if (CONST_OK_FOR_I (INTVAL (x)))
4357 *total = 0;
4358 else if (CONST_OK_FOR_J (INTVAL (x)))
4359 *total = 1;
4360 else
4361 *total = COSTS_N_INSNS (1);
4362 return true;
4363 default:
4364 if (CONST_OK_FOR_K (INTVAL (x)) || CONST_OK_FOR_L (INTVAL (x)))
4365 *total = 0;
4366 else
4367 *total = COSTS_N_INSNS (1);
4368 return true;
4371 case CONST_DOUBLE:
4372 *total = COSTS_N_INSNS (1);
4373 return true;
4375 case CONST:
4376 case SYMBOL_REF:
4377 case LABEL_REF:
4378 *total = COSTS_N_INSNS (3);
4379 return true;
4381 case MULT:
4382 /* For multiplies wider than HImode, we have to go to the FPU,
4383 which normally involves copies. Plus there's the latency
4384 of the multiply itself, and the latency of the instructions to
4385 transfer integer regs to FP regs. */
4386 /* ??? Check for FP mode. */
4387 if (GET_MODE_SIZE (GET_MODE (x)) > 2)
4388 *total = COSTS_N_INSNS (10);
4389 else
4390 *total = COSTS_N_INSNS (2);
4391 return true;
4393 case PLUS:
4394 case MINUS:
4395 case ASHIFT:
4396 case ASHIFTRT:
4397 case LSHIFTRT:
4398 *total = COSTS_N_INSNS (1);
4399 return true;
4401 case DIV:
4402 case UDIV:
4403 case MOD:
4404 case UMOD:
4405 /* We make divide expensive, so that divide-by-constant will be
4406 optimized to a multiply. */
4407 *total = COSTS_N_INSNS (60);
4408 return true;
4410 default:
4411 return false;
4415 /* Calculate the cost of moving data from a register in class FROM to
4416 one in class TO, using MODE. */
4419 ia64_register_move_cost (enum machine_mode mode, enum reg_class from,
4420 enum reg_class to)
4422 /* ADDL_REGS is the same as GR_REGS for movement purposes. */
4423 if (to == ADDL_REGS)
4424 to = GR_REGS;
4425 if (from == ADDL_REGS)
4426 from = GR_REGS;
4428 /* All costs are symmetric, so reduce cases by putting the
4429 lower number class as the destination. */
4430 if (from < to)
4432 enum reg_class tmp = to;
4433 to = from, from = tmp;
4436 /* Moving from FR<->GR in XFmode must be more expensive than 2,
4437 so that we get secondary memory reloads. Between FR_REGS,
4438 we have to make this at least as expensive as MEMORY_MOVE_COST
4439 to avoid spectacularly poor register class preferencing. */
4440 if (mode == XFmode)
4442 if (to != GR_REGS || from != GR_REGS)
4443 return MEMORY_MOVE_COST (mode, to, 0);
4444 else
4445 return 3;
4448 switch (to)
4450 case PR_REGS:
4451 /* Moving between PR registers takes two insns. */
4452 if (from == PR_REGS)
4453 return 3;
4454 /* Moving between PR and anything but GR is impossible. */
4455 if (from != GR_REGS)
4456 return MEMORY_MOVE_COST (mode, to, 0);
4457 break;
4459 case BR_REGS:
4460 /* Moving between BR and anything but GR is impossible. */
4461 if (from != GR_REGS && from != GR_AND_BR_REGS)
4462 return MEMORY_MOVE_COST (mode, to, 0);
4463 break;
4465 case AR_I_REGS:
4466 case AR_M_REGS:
4467 /* Moving between AR and anything but GR is impossible. */
4468 if (from != GR_REGS)
4469 return MEMORY_MOVE_COST (mode, to, 0);
4470 break;
4472 case GR_REGS:
4473 case FR_REGS:
4474 case GR_AND_FR_REGS:
4475 case GR_AND_BR_REGS:
4476 case ALL_REGS:
4477 break;
4479 default:
4480 abort ();
4483 return 2;
4486 /* This function returns the register class required for a secondary
4487 register when copying between one of the registers in CLASS, and X,
4488 using MODE. A return value of NO_REGS means that no secondary register
4489 is required. */
4491 enum reg_class
4492 ia64_secondary_reload_class (enum reg_class class,
4493 enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
4495 int regno = -1;
4497 if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
4498 regno = true_regnum (x);
4500 switch (class)
4502 case BR_REGS:
4503 case AR_M_REGS:
4504 case AR_I_REGS:
4505 /* ??? BR<->BR register copies can happen due to a bad gcse/cse/global
4506 interaction. We end up with two pseudos with overlapping lifetimes
4507 both of which are equiv to the same constant, and both which need
4508 to be in BR_REGS. This seems to be a cse bug. cse_basic_block_end
4509 changes depending on the path length, which means the qty_first_reg
4510 check in make_regs_eqv can give different answers at different times.
4511 At some point I'll probably need a reload_indi pattern to handle
4512 this.
4514 We can also get GR_AND_FR_REGS to BR_REGS/AR_REGS copies, where we
4515 wound up with a FP register from GR_AND_FR_REGS. Extend that to all
4516 non-general registers for good measure. */
4517 if (regno >= 0 && ! GENERAL_REGNO_P (regno))
4518 return GR_REGS;
4520 /* This is needed if a pseudo used as a call_operand gets spilled to a
4521 stack slot. */
4522 if (GET_CODE (x) == MEM)
4523 return GR_REGS;
4524 break;
4526 case FR_REGS:
4527 /* Need to go through general registers to get to other class regs. */
4528 if (regno >= 0 && ! (FR_REGNO_P (regno) || GENERAL_REGNO_P (regno)))
4529 return GR_REGS;
4531 /* This can happen when a paradoxical subreg is an operand to the
4532 muldi3 pattern. */
4533 /* ??? This shouldn't be necessary after instruction scheduling is
4534 enabled, because paradoxical subregs are not accepted by
4535 register_operand when INSN_SCHEDULING is defined. Or alternatively,
4536 stop the paradoxical subreg stupidity in the *_operand functions
4537 in recog.c. */
4538 if (GET_CODE (x) == MEM
4539 && (GET_MODE (x) == SImode || GET_MODE (x) == HImode
4540 || GET_MODE (x) == QImode))
4541 return GR_REGS;
4543 /* This can happen because of the ior/and/etc patterns that accept FP
4544 registers as operands. If the third operand is a constant, then it
4545 needs to be reloaded into a FP register. */
4546 if (GET_CODE (x) == CONST_INT)
4547 return GR_REGS;
4549 /* This can happen because of register elimination in a muldi3 insn.
4550 E.g. `26107 * (unsigned long)&u'. */
4551 if (GET_CODE (x) == PLUS)
4552 return GR_REGS;
4553 break;
4555 case PR_REGS:
4556 /* ??? This happens if we cse/gcse a BImode value across a call,
4557 and the function has a nonlocal goto. This is because global
4558 does not allocate call crossing pseudos to hard registers when
4559 current_function_has_nonlocal_goto is true. This is relatively
4560 common for C++ programs that use exceptions. To reproduce,
4561 return NO_REGS and compile libstdc++. */
4562 if (GET_CODE (x) == MEM)
4563 return GR_REGS;
4565 /* This can happen when we take a BImode subreg of a DImode value,
4566 and that DImode value winds up in some non-GR register. */
4567 if (regno >= 0 && ! GENERAL_REGNO_P (regno) && ! PR_REGNO_P (regno))
4568 return GR_REGS;
4569 break;
4571 default:
4572 break;
4575 return NO_REGS;
4579 /* Emit text to declare externally defined variables and functions, because
4580 the Intel assembler does not support undefined externals. */
4582 void
4583 ia64_asm_output_external (FILE *file, tree decl, const char *name)
4585 int save_referenced;
4587 /* GNU as does not need anything here, but the HP linker does need
4588 something for external functions. */
4590 if (TARGET_GNU_AS
4591 && (!TARGET_HPUX_LD
4592 || TREE_CODE (decl) != FUNCTION_DECL
4593 || strstr (name, "__builtin_") == name))
4594 return;
4596 /* ??? The Intel assembler creates a reference that needs to be satisfied by
4597 the linker when we do this, so we need to be careful not to do this for
4598 builtin functions which have no library equivalent. Unfortunately, we
4599 can't tell here whether or not a function will actually be called by
4600 expand_expr, so we pull in library functions even if we may not need
4601 them later. */
4602 if (! strcmp (name, "__builtin_next_arg")
4603 || ! strcmp (name, "alloca")
4604 || ! strcmp (name, "__builtin_constant_p")
4605 || ! strcmp (name, "__builtin_args_info"))
4606 return;
4608 if (TARGET_HPUX_LD)
4609 ia64_hpux_add_extern_decl (decl);
4610 else
4612 /* assemble_name will set TREE_SYMBOL_REFERENCED, so we must save and
4613 restore it. */
4614 save_referenced = TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl));
4615 if (TREE_CODE (decl) == FUNCTION_DECL)
4616 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
4617 (*targetm.asm_out.globalize_label) (file, name);
4618 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = save_referenced;
4622 /* Parse the -mfixed-range= option string. */
4624 static void
4625 fix_range (const char *const_str)
4627 int i, first, last;
4628 char *str, *dash, *comma;
4630 /* str must be of the form REG1'-'REG2{,REG1'-'REG} where REG1 and
4631 REG2 are either register names or register numbers. The effect
4632 of this option is to mark the registers in the range from REG1 to
4633 REG2 as ``fixed'' so they won't be used by the compiler. This is
4634 used, e.g., to ensure that kernel mode code doesn't use f32-f127. */
4636 i = strlen (const_str);
4637 str = (char *) alloca (i + 1);
4638 memcpy (str, const_str, i + 1);
4640 while (1)
4642 dash = strchr (str, '-');
4643 if (!dash)
4645 warning ("value of -mfixed-range must have form REG1-REG2");
4646 return;
4648 *dash = '\0';
4650 comma = strchr (dash + 1, ',');
4651 if (comma)
4652 *comma = '\0';
4654 first = decode_reg_name (str);
4655 if (first < 0)
4657 warning ("unknown register name: %s", str);
4658 return;
4661 last = decode_reg_name (dash + 1);
4662 if (last < 0)
4664 warning ("unknown register name: %s", dash + 1);
4665 return;
4668 *dash = '-';
4670 if (first > last)
4672 warning ("%s-%s is an empty range", str, dash + 1);
4673 return;
4676 for (i = first; i <= last; ++i)
4677 fixed_regs[i] = call_used_regs[i] = 1;
4679 if (!comma)
4680 break;
4682 *comma = ',';
4683 str = comma + 1;
4687 static struct machine_function *
4688 ia64_init_machine_status (void)
4690 return ggc_alloc_cleared (sizeof (struct machine_function));
4693 /* Handle TARGET_OPTIONS switches. */
4695 void
4696 ia64_override_options (void)
4698 static struct pta
4700 const char *const name; /* processor name or nickname. */
4701 const enum processor_type processor;
4703 const processor_alias_table[] =
4705 {"itanium", PROCESSOR_ITANIUM},
4706 {"itanium1", PROCESSOR_ITANIUM},
4707 {"merced", PROCESSOR_ITANIUM},
4708 {"itanium2", PROCESSOR_ITANIUM2},
4709 {"mckinley", PROCESSOR_ITANIUM2},
4712 int const pta_size = ARRAY_SIZE (processor_alias_table);
4713 int i;
4715 if (TARGET_AUTO_PIC)
4716 target_flags |= MASK_CONST_GP;
4718 if (TARGET_INLINE_FLOAT_DIV_LAT && TARGET_INLINE_FLOAT_DIV_THR)
4720 warning ("cannot optimize floating point division for both latency and throughput");
4721 target_flags &= ~MASK_INLINE_FLOAT_DIV_THR;
4724 if (TARGET_INLINE_INT_DIV_LAT && TARGET_INLINE_INT_DIV_THR)
4726 warning ("cannot optimize integer division for both latency and throughput");
4727 target_flags &= ~MASK_INLINE_INT_DIV_THR;
4730 if (TARGET_INLINE_SQRT_LAT && TARGET_INLINE_SQRT_THR)
4732 warning ("cannot optimize square root for both latency and throughput");
4733 target_flags &= ~MASK_INLINE_SQRT_THR;
4736 if (TARGET_INLINE_SQRT_LAT)
4738 warning ("not yet implemented: latency-optimized inline square root");
4739 target_flags &= ~MASK_INLINE_SQRT_LAT;
4742 if (ia64_fixed_range_string)
4743 fix_range (ia64_fixed_range_string);
4745 if (ia64_tls_size_string)
4747 char *end;
4748 unsigned long tmp = strtoul (ia64_tls_size_string, &end, 10);
4749 if (*end || (tmp != 14 && tmp != 22 && tmp != 64))
4750 error ("bad value (%s) for -mtls-size= switch", ia64_tls_size_string);
4751 else
4752 ia64_tls_size = tmp;
4755 if (!ia64_tune_string)
4756 ia64_tune_string = "itanium2";
4758 for (i = 0; i < pta_size; i++)
4759 if (! strcmp (ia64_tune_string, processor_alias_table[i].name))
4761 ia64_tune = processor_alias_table[i].processor;
4762 break;
4765 if (i == pta_size)
4766 error ("bad value (%s) for -tune= switch", ia64_tune_string);
4768 ia64_flag_schedule_insns2 = flag_schedule_insns_after_reload;
4769 flag_schedule_insns_after_reload = 0;
4771 ia64_section_threshold = g_switch_set ? g_switch_value : IA64_DEFAULT_GVALUE;
4773 init_machine_status = ia64_init_machine_status;
4776 static enum attr_itanium_class ia64_safe_itanium_class (rtx);
4777 static enum attr_type ia64_safe_type (rtx);
4779 static enum attr_itanium_class
4780 ia64_safe_itanium_class (rtx insn)
4782 if (recog_memoized (insn) >= 0)
4783 return get_attr_itanium_class (insn);
4784 else
4785 return ITANIUM_CLASS_UNKNOWN;
4788 static enum attr_type
4789 ia64_safe_type (rtx insn)
4791 if (recog_memoized (insn) >= 0)
4792 return get_attr_type (insn);
4793 else
4794 return TYPE_UNKNOWN;
4797 /* The following collection of routines emit instruction group stop bits as
4798 necessary to avoid dependencies. */
4800 /* Need to track some additional registers as far as serialization is
4801 concerned so we can properly handle br.call and br.ret. We could
4802 make these registers visible to gcc, but since these registers are
4803 never explicitly used in gcc generated code, it seems wasteful to
4804 do so (plus it would make the call and return patterns needlessly
4805 complex). */
4806 #define REG_GP (GR_REG (1))
4807 #define REG_RP (BR_REG (0))
4808 #define REG_AR_CFM (FIRST_PSEUDO_REGISTER + 1)
4809 /* This is used for volatile asms which may require a stop bit immediately
4810 before and after them. */
4811 #define REG_VOLATILE (FIRST_PSEUDO_REGISTER + 2)
4812 #define AR_UNAT_BIT_0 (FIRST_PSEUDO_REGISTER + 3)
4813 #define NUM_REGS (AR_UNAT_BIT_0 + 64)
4815 /* For each register, we keep track of how it has been written in the
4816 current instruction group.
4818 If a register is written unconditionally (no qualifying predicate),
4819 WRITE_COUNT is set to 2 and FIRST_PRED is ignored.
4821 If a register is written if its qualifying predicate P is true, we
4822 set WRITE_COUNT to 1 and FIRST_PRED to P. Later on, the same register
4823 may be written again by the complement of P (P^1) and when this happens,
4824 WRITE_COUNT gets set to 2.
4826 The result of this is that whenever an insn attempts to write a register
4827 whose WRITE_COUNT is two, we need to issue an insn group barrier first.
4829 If a predicate register is written by a floating-point insn, we set
4830 WRITTEN_BY_FP to true.
4832 If a predicate register is written by an AND.ORCM we set WRITTEN_BY_AND
4833 to true; if it was written by an OR.ANDCM we set WRITTEN_BY_OR to true. */
4835 struct reg_write_state
4837 unsigned int write_count : 2;
4838 unsigned int first_pred : 16;
4839 unsigned int written_by_fp : 1;
4840 unsigned int written_by_and : 1;
4841 unsigned int written_by_or : 1;
4844 /* Cumulative info for the current instruction group. */
4845 struct reg_write_state rws_sum[NUM_REGS];
4846 /* Info for the current instruction. This gets copied to rws_sum after a
4847 stop bit is emitted. */
4848 struct reg_write_state rws_insn[NUM_REGS];
4850 /* Indicates whether this is the first instruction after a stop bit,
4851 in which case we don't need another stop bit. Without this, we hit
4852 the abort in ia64_variable_issue when scheduling an alloc. */
4853 static int first_instruction;
4855 /* Misc flags needed to compute RAW/WAW dependencies while we are traversing
4856 RTL for one instruction. */
4857 struct reg_flags
4859 unsigned int is_write : 1; /* Is register being written? */
4860 unsigned int is_fp : 1; /* Is register used as part of an fp op? */
4861 unsigned int is_branch : 1; /* Is register used as part of a branch? */
4862 unsigned int is_and : 1; /* Is register used as part of and.orcm? */
4863 unsigned int is_or : 1; /* Is register used as part of or.andcm? */
4864 unsigned int is_sibcall : 1; /* Is this a sibling or normal call? */
4867 static void rws_update (struct reg_write_state *, int, struct reg_flags, int);
4868 static int rws_access_regno (int, struct reg_flags, int);
4869 static int rws_access_reg (rtx, struct reg_flags, int);
4870 static void update_set_flags (rtx, struct reg_flags *, int *, rtx *);
4871 static int set_src_needs_barrier (rtx, struct reg_flags, int, rtx);
4872 static int rtx_needs_barrier (rtx, struct reg_flags, int);
4873 static void init_insn_group_barriers (void);
4874 static int group_barrier_needed_p (rtx);
4875 static int safe_group_barrier_needed_p (rtx);
4877 /* Update *RWS for REGNO, which is being written by the current instruction,
4878 with predicate PRED, and associated register flags in FLAGS. */
4880 static void
4881 rws_update (struct reg_write_state *rws, int regno, struct reg_flags flags, int pred)
4883 if (pred)
4884 rws[regno].write_count++;
4885 else
4886 rws[regno].write_count = 2;
4887 rws[regno].written_by_fp |= flags.is_fp;
4888 /* ??? Not tracking and/or across differing predicates. */
4889 rws[regno].written_by_and = flags.is_and;
4890 rws[regno].written_by_or = flags.is_or;
4891 rws[regno].first_pred = pred;
4894 /* Handle an access to register REGNO of type FLAGS using predicate register
4895 PRED. Update rws_insn and rws_sum arrays. Return 1 if this access creates
4896 a dependency with an earlier instruction in the same group. */
4898 static int
4899 rws_access_regno (int regno, struct reg_flags flags, int pred)
4901 int need_barrier = 0;
4903 if (regno >= NUM_REGS)
4904 abort ();
4906 if (! PR_REGNO_P (regno))
4907 flags.is_and = flags.is_or = 0;
4909 if (flags.is_write)
4911 int write_count;
4913 /* One insn writes same reg multiple times? */
4914 if (rws_insn[regno].write_count > 0)
4915 abort ();
4917 /* Update info for current instruction. */
4918 rws_update (rws_insn, regno, flags, pred);
4919 write_count = rws_sum[regno].write_count;
4921 switch (write_count)
4923 case 0:
4924 /* The register has not been written yet. */
4925 rws_update (rws_sum, regno, flags, pred);
4926 break;
4928 case 1:
4929 /* The register has been written via a predicate. If this is
4930 not a complementary predicate, then we need a barrier. */
4931 /* ??? This assumes that P and P+1 are always complementary
4932 predicates for P even. */
4933 if (flags.is_and && rws_sum[regno].written_by_and)
4935 else if (flags.is_or && rws_sum[regno].written_by_or)
4937 else if ((rws_sum[regno].first_pred ^ 1) != pred)
4938 need_barrier = 1;
4939 rws_update (rws_sum, regno, flags, pred);
4940 break;
4942 case 2:
4943 /* The register has been unconditionally written already. We
4944 need a barrier. */
4945 if (flags.is_and && rws_sum[regno].written_by_and)
4947 else if (flags.is_or && rws_sum[regno].written_by_or)
4949 else
4950 need_barrier = 1;
4951 rws_sum[regno].written_by_and = flags.is_and;
4952 rws_sum[regno].written_by_or = flags.is_or;
4953 break;
4955 default:
4956 abort ();
4959 else
4961 if (flags.is_branch)
4963 /* Branches have several RAW exceptions that allow to avoid
4964 barriers. */
4966 if (REGNO_REG_CLASS (regno) == BR_REGS || regno == AR_PFS_REGNUM)
4967 /* RAW dependencies on branch regs are permissible as long
4968 as the writer is a non-branch instruction. Since we
4969 never generate code that uses a branch register written
4970 by a branch instruction, handling this case is
4971 easy. */
4972 return 0;
4974 if (REGNO_REG_CLASS (regno) == PR_REGS
4975 && ! rws_sum[regno].written_by_fp)
4976 /* The predicates of a branch are available within the
4977 same insn group as long as the predicate was written by
4978 something other than a floating-point instruction. */
4979 return 0;
4982 if (flags.is_and && rws_sum[regno].written_by_and)
4983 return 0;
4984 if (flags.is_or && rws_sum[regno].written_by_or)
4985 return 0;
4987 switch (rws_sum[regno].write_count)
4989 case 0:
4990 /* The register has not been written yet. */
4991 break;
4993 case 1:
4994 /* The register has been written via a predicate. If this is
4995 not a complementary predicate, then we need a barrier. */
4996 /* ??? This assumes that P and P+1 are always complementary
4997 predicates for P even. */
4998 if ((rws_sum[regno].first_pred ^ 1) != pred)
4999 need_barrier = 1;
5000 break;
5002 case 2:
5003 /* The register has been unconditionally written already. We
5004 need a barrier. */
5005 need_barrier = 1;
5006 break;
5008 default:
5009 abort ();
5013 return need_barrier;
5016 static int
5017 rws_access_reg (rtx reg, struct reg_flags flags, int pred)
5019 int regno = REGNO (reg);
5020 int n = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
5022 if (n == 1)
5023 return rws_access_regno (regno, flags, pred);
5024 else
5026 int need_barrier = 0;
5027 while (--n >= 0)
5028 need_barrier |= rws_access_regno (regno + n, flags, pred);
5029 return need_barrier;
5033 /* Examine X, which is a SET rtx, and update the flags, the predicate, and
5034 the condition, stored in *PFLAGS, *PPRED and *PCOND. */
5036 static void
5037 update_set_flags (rtx x, struct reg_flags *pflags, int *ppred, rtx *pcond)
5039 rtx src = SET_SRC (x);
5041 *pcond = 0;
5043 switch (GET_CODE (src))
5045 case CALL:
5046 return;
5048 case IF_THEN_ELSE:
5049 if (SET_DEST (x) == pc_rtx)
5050 /* X is a conditional branch. */
5051 return;
5052 else
5054 int is_complemented = 0;
5056 /* X is a conditional move. */
5057 rtx cond = XEXP (src, 0);
5058 if (GET_CODE (cond) == EQ)
5059 is_complemented = 1;
5060 cond = XEXP (cond, 0);
5061 if (GET_CODE (cond) != REG
5062 && REGNO_REG_CLASS (REGNO (cond)) != PR_REGS)
5063 abort ();
5064 *pcond = cond;
5065 if (XEXP (src, 1) == SET_DEST (x)
5066 || XEXP (src, 2) == SET_DEST (x))
5068 /* X is a conditional move that conditionally writes the
5069 destination. */
5071 /* We need another complement in this case. */
5072 if (XEXP (src, 1) == SET_DEST (x))
5073 is_complemented = ! is_complemented;
5075 *ppred = REGNO (cond);
5076 if (is_complemented)
5077 ++*ppred;
5080 /* ??? If this is a conditional write to the dest, then this
5081 instruction does not actually read one source. This probably
5082 doesn't matter, because that source is also the dest. */
5083 /* ??? Multiple writes to predicate registers are allowed
5084 if they are all AND type compares, or if they are all OR
5085 type compares. We do not generate such instructions
5086 currently. */
5088 /* ... fall through ... */
5090 default:
5091 if (GET_RTX_CLASS (GET_CODE (src)) == '<'
5092 && GET_MODE_CLASS (GET_MODE (XEXP (src, 0))) == MODE_FLOAT)
5093 /* Set pflags->is_fp to 1 so that we know we're dealing
5094 with a floating point comparison when processing the
5095 destination of the SET. */
5096 pflags->is_fp = 1;
5098 /* Discover if this is a parallel comparison. We only handle
5099 and.orcm and or.andcm at present, since we must retain a
5100 strict inverse on the predicate pair. */
5101 else if (GET_CODE (src) == AND)
5102 pflags->is_and = 1;
5103 else if (GET_CODE (src) == IOR)
5104 pflags->is_or = 1;
5106 break;
5110 /* Subroutine of rtx_needs_barrier; this function determines whether the
5111 source of a given SET rtx found in X needs a barrier. FLAGS and PRED
5112 are as in rtx_needs_barrier. COND is an rtx that holds the condition
5113 for this insn. */
5115 static int
5116 set_src_needs_barrier (rtx x, struct reg_flags flags, int pred, rtx cond)
5118 int need_barrier = 0;
5119 rtx dst;
5120 rtx src = SET_SRC (x);
5122 if (GET_CODE (src) == CALL)
5123 /* We don't need to worry about the result registers that
5124 get written by subroutine call. */
5125 return rtx_needs_barrier (src, flags, pred);
5126 else if (SET_DEST (x) == pc_rtx)
5128 /* X is a conditional branch. */
5129 /* ??? This seems redundant, as the caller sets this bit for
5130 all JUMP_INSNs. */
5131 flags.is_branch = 1;
5132 return rtx_needs_barrier (src, flags, pred);
5135 need_barrier = rtx_needs_barrier (src, flags, pred);
5137 /* This instruction unconditionally uses a predicate register. */
5138 if (cond)
5139 need_barrier |= rws_access_reg (cond, flags, 0);
5141 dst = SET_DEST (x);
5142 if (GET_CODE (dst) == ZERO_EXTRACT)
5144 need_barrier |= rtx_needs_barrier (XEXP (dst, 1), flags, pred);
5145 need_barrier |= rtx_needs_barrier (XEXP (dst, 2), flags, pred);
5146 dst = XEXP (dst, 0);
5148 return need_barrier;
5151 /* Handle an access to rtx X of type FLAGS using predicate register
5152 PRED. Return 1 if this access creates a dependency with an earlier
5153 instruction in the same group. */
5155 static int
5156 rtx_needs_barrier (rtx x, struct reg_flags flags, int pred)
5158 int i, j;
5159 int is_complemented = 0;
5160 int need_barrier = 0;
5161 const char *format_ptr;
5162 struct reg_flags new_flags;
5163 rtx cond = 0;
5165 if (! x)
5166 return 0;
5168 new_flags = flags;
5170 switch (GET_CODE (x))
5172 case SET:
5173 update_set_flags (x, &new_flags, &pred, &cond);
5174 need_barrier = set_src_needs_barrier (x, new_flags, pred, cond);
5175 if (GET_CODE (SET_SRC (x)) != CALL)
5177 new_flags.is_write = 1;
5178 need_barrier |= rtx_needs_barrier (SET_DEST (x), new_flags, pred);
5180 break;
5182 case CALL:
5183 new_flags.is_write = 0;
5184 need_barrier |= rws_access_regno (AR_EC_REGNUM, new_flags, pred);
5186 /* Avoid multiple register writes, in case this is a pattern with
5187 multiple CALL rtx. This avoids an abort in rws_access_reg. */
5188 if (! flags.is_sibcall && ! rws_insn[REG_AR_CFM].write_count)
5190 new_flags.is_write = 1;
5191 need_barrier |= rws_access_regno (REG_RP, new_flags, pred);
5192 need_barrier |= rws_access_regno (AR_PFS_REGNUM, new_flags, pred);
5193 need_barrier |= rws_access_regno (REG_AR_CFM, new_flags, pred);
5195 break;
5197 case COND_EXEC:
5198 /* X is a predicated instruction. */
5200 cond = COND_EXEC_TEST (x);
5201 if (pred)
5202 abort ();
5203 need_barrier = rtx_needs_barrier (cond, flags, 0);
5205 if (GET_CODE (cond) == EQ)
5206 is_complemented = 1;
5207 cond = XEXP (cond, 0);
5208 if (GET_CODE (cond) != REG
5209 && REGNO_REG_CLASS (REGNO (cond)) != PR_REGS)
5210 abort ();
5211 pred = REGNO (cond);
5212 if (is_complemented)
5213 ++pred;
5215 need_barrier |= rtx_needs_barrier (COND_EXEC_CODE (x), flags, pred);
5216 return need_barrier;
5218 case CLOBBER:
5219 case USE:
5220 /* Clobber & use are for earlier compiler-phases only. */
5221 break;
5223 case ASM_OPERANDS:
5224 case ASM_INPUT:
5225 /* We always emit stop bits for traditional asms. We emit stop bits
5226 for volatile extended asms if TARGET_VOL_ASM_STOP is true. */
5227 if (GET_CODE (x) != ASM_OPERANDS
5228 || (MEM_VOLATILE_P (x) && TARGET_VOL_ASM_STOP))
5230 /* Avoid writing the register multiple times if we have multiple
5231 asm outputs. This avoids an abort in rws_access_reg. */
5232 if (! rws_insn[REG_VOLATILE].write_count)
5234 new_flags.is_write = 1;
5235 rws_access_regno (REG_VOLATILE, new_flags, pred);
5237 return 1;
5240 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
5241 We can not just fall through here since then we would be confused
5242 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
5243 traditional asms unlike their normal usage. */
5245 for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; --i)
5246 if (rtx_needs_barrier (ASM_OPERANDS_INPUT (x, i), flags, pred))
5247 need_barrier = 1;
5248 break;
5250 case PARALLEL:
5251 for (i = XVECLEN (x, 0) - 1; i >= 0; --i)
5253 rtx pat = XVECEXP (x, 0, i);
5254 if (GET_CODE (pat) == SET)
5256 update_set_flags (pat, &new_flags, &pred, &cond);
5257 need_barrier |= set_src_needs_barrier (pat, new_flags, pred, cond);
5259 else if (GET_CODE (pat) == USE
5260 || GET_CODE (pat) == CALL
5261 || GET_CODE (pat) == ASM_OPERANDS)
5262 need_barrier |= rtx_needs_barrier (pat, flags, pred);
5263 else if (GET_CODE (pat) != CLOBBER && GET_CODE (pat) != RETURN)
5264 abort ();
5266 for (i = XVECLEN (x, 0) - 1; i >= 0; --i)
5268 rtx pat = XVECEXP (x, 0, i);
5269 if (GET_CODE (pat) == SET)
5271 if (GET_CODE (SET_SRC (pat)) != CALL)
5273 new_flags.is_write = 1;
5274 need_barrier |= rtx_needs_barrier (SET_DEST (pat), new_flags,
5275 pred);
5278 else if (GET_CODE (pat) == CLOBBER || GET_CODE (pat) == RETURN)
5279 need_barrier |= rtx_needs_barrier (pat, flags, pred);
5281 break;
5283 case SUBREG:
5284 x = SUBREG_REG (x);
5285 /* FALLTHRU */
5286 case REG:
5287 if (REGNO (x) == AR_UNAT_REGNUM)
5289 for (i = 0; i < 64; ++i)
5290 need_barrier |= rws_access_regno (AR_UNAT_BIT_0 + i, flags, pred);
5292 else
5293 need_barrier = rws_access_reg (x, flags, pred);
5294 break;
5296 case MEM:
5297 /* Find the regs used in memory address computation. */
5298 new_flags.is_write = 0;
5299 need_barrier = rtx_needs_barrier (XEXP (x, 0), new_flags, pred);
5300 break;
5302 case CONST_INT: case CONST_DOUBLE:
5303 case SYMBOL_REF: case LABEL_REF: case CONST:
5304 break;
5306 /* Operators with side-effects. */
5307 case POST_INC: case POST_DEC:
5308 if (GET_CODE (XEXP (x, 0)) != REG)
5309 abort ();
5311 new_flags.is_write = 0;
5312 need_barrier = rws_access_reg (XEXP (x, 0), new_flags, pred);
5313 new_flags.is_write = 1;
5314 need_barrier |= rws_access_reg (XEXP (x, 0), new_flags, pred);
5315 break;
5317 case POST_MODIFY:
5318 if (GET_CODE (XEXP (x, 0)) != REG)
5319 abort ();
5321 new_flags.is_write = 0;
5322 need_barrier = rws_access_reg (XEXP (x, 0), new_flags, pred);
5323 need_barrier |= rtx_needs_barrier (XEXP (x, 1), new_flags, pred);
5324 new_flags.is_write = 1;
5325 need_barrier |= rws_access_reg (XEXP (x, 0), new_flags, pred);
5326 break;
5328 /* Handle common unary and binary ops for efficiency. */
5329 case COMPARE: case PLUS: case MINUS: case MULT: case DIV:
5330 case MOD: case UDIV: case UMOD: case AND: case IOR:
5331 case XOR: case ASHIFT: case ROTATE: case ASHIFTRT: case LSHIFTRT:
5332 case ROTATERT: case SMIN: case SMAX: case UMIN: case UMAX:
5333 case NE: case EQ: case GE: case GT: case LE:
5334 case LT: case GEU: case GTU: case LEU: case LTU:
5335 need_barrier = rtx_needs_barrier (XEXP (x, 0), new_flags, pred);
5336 need_barrier |= rtx_needs_barrier (XEXP (x, 1), new_flags, pred);
5337 break;
5339 case NEG: case NOT: case SIGN_EXTEND: case ZERO_EXTEND:
5340 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE: case FLOAT:
5341 case FIX: case UNSIGNED_FLOAT: case UNSIGNED_FIX: case ABS:
5342 case SQRT: case FFS: case POPCOUNT:
5343 need_barrier = rtx_needs_barrier (XEXP (x, 0), flags, pred);
5344 break;
5346 case UNSPEC:
5347 switch (XINT (x, 1))
5349 case UNSPEC_LTOFF_DTPMOD:
5350 case UNSPEC_LTOFF_DTPREL:
5351 case UNSPEC_DTPREL:
5352 case UNSPEC_LTOFF_TPREL:
5353 case UNSPEC_TPREL:
5354 case UNSPEC_PRED_REL_MUTEX:
5355 case UNSPEC_PIC_CALL:
5356 case UNSPEC_MF:
5357 case UNSPEC_FETCHADD_ACQ:
5358 case UNSPEC_BSP_VALUE:
5359 case UNSPEC_FLUSHRS:
5360 case UNSPEC_BUNDLE_SELECTOR:
5361 break;
5363 case UNSPEC_GR_SPILL:
5364 case UNSPEC_GR_RESTORE:
5366 HOST_WIDE_INT offset = INTVAL (XVECEXP (x, 0, 1));
5367 HOST_WIDE_INT bit = (offset >> 3) & 63;
5369 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
5370 new_flags.is_write = (XINT (x, 1) == 1);
5371 need_barrier |= rws_access_regno (AR_UNAT_BIT_0 + bit,
5372 new_flags, pred);
5373 break;
5376 case UNSPEC_FR_SPILL:
5377 case UNSPEC_FR_RESTORE:
5378 case UNSPEC_GETF_EXP:
5379 case UNSPEC_SETF_EXP:
5380 case UNSPEC_ADDP4:
5381 case UNSPEC_FR_SQRT_RECIP_APPROX:
5382 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
5383 break;
5385 case UNSPEC_FR_RECIP_APPROX:
5386 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
5387 need_barrier |= rtx_needs_barrier (XVECEXP (x, 0, 1), flags, pred);
5388 break;
5390 case UNSPEC_CMPXCHG_ACQ:
5391 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 1), flags, pred);
5392 need_barrier |= rtx_needs_barrier (XVECEXP (x, 0, 2), flags, pred);
5393 break;
5395 default:
5396 abort ();
5398 break;
5400 case UNSPEC_VOLATILE:
5401 switch (XINT (x, 1))
5403 case UNSPECV_ALLOC:
5404 /* Alloc must always be the first instruction of a group.
5405 We force this by always returning true. */
5406 /* ??? We might get better scheduling if we explicitly check for
5407 input/local/output register dependencies, and modify the
5408 scheduler so that alloc is always reordered to the start of
5409 the current group. We could then eliminate all of the
5410 first_instruction code. */
5411 rws_access_regno (AR_PFS_REGNUM, flags, pred);
5413 new_flags.is_write = 1;
5414 rws_access_regno (REG_AR_CFM, new_flags, pred);
5415 return 1;
5417 case UNSPECV_SET_BSP:
5418 need_barrier = 1;
5419 break;
5421 case UNSPECV_BLOCKAGE:
5422 case UNSPECV_INSN_GROUP_BARRIER:
5423 case UNSPECV_BREAK:
5424 case UNSPECV_PSAC_ALL:
5425 case UNSPECV_PSAC_NORMAL:
5426 return 0;
5428 default:
5429 abort ();
5431 break;
5433 case RETURN:
5434 new_flags.is_write = 0;
5435 need_barrier = rws_access_regno (REG_RP, flags, pred);
5436 need_barrier |= rws_access_regno (AR_PFS_REGNUM, flags, pred);
5438 new_flags.is_write = 1;
5439 need_barrier |= rws_access_regno (AR_EC_REGNUM, new_flags, pred);
5440 need_barrier |= rws_access_regno (REG_AR_CFM, new_flags, pred);
5441 break;
5443 default:
5444 format_ptr = GET_RTX_FORMAT (GET_CODE (x));
5445 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5446 switch (format_ptr[i])
5448 case '0': /* unused field */
5449 case 'i': /* integer */
5450 case 'n': /* note */
5451 case 'w': /* wide integer */
5452 case 's': /* pointer to string */
5453 case 'S': /* optional pointer to string */
5454 break;
5456 case 'e':
5457 if (rtx_needs_barrier (XEXP (x, i), flags, pred))
5458 need_barrier = 1;
5459 break;
5461 case 'E':
5462 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
5463 if (rtx_needs_barrier (XVECEXP (x, i, j), flags, pred))
5464 need_barrier = 1;
5465 break;
5467 default:
5468 abort ();
5470 break;
5472 return need_barrier;
5475 /* Clear out the state for group_barrier_needed_p at the start of a
5476 sequence of insns. */
5478 static void
5479 init_insn_group_barriers (void)
5481 memset (rws_sum, 0, sizeof (rws_sum));
5482 first_instruction = 1;
5485 /* Given the current state, recorded by previous calls to this function,
5486 determine whether a group barrier (a stop bit) is necessary before INSN.
5487 Return nonzero if so. */
5489 static int
5490 group_barrier_needed_p (rtx insn)
5492 rtx pat;
5493 int need_barrier = 0;
5494 struct reg_flags flags;
5496 memset (&flags, 0, sizeof (flags));
5497 switch (GET_CODE (insn))
5499 case NOTE:
5500 break;
5502 case BARRIER:
5503 /* A barrier doesn't imply an instruction group boundary. */
5504 break;
5506 case CODE_LABEL:
5507 memset (rws_insn, 0, sizeof (rws_insn));
5508 return 1;
5510 case CALL_INSN:
5511 flags.is_branch = 1;
5512 flags.is_sibcall = SIBLING_CALL_P (insn);
5513 memset (rws_insn, 0, sizeof (rws_insn));
5515 /* Don't bundle a call following another call. */
5516 if ((pat = prev_active_insn (insn))
5517 && GET_CODE (pat) == CALL_INSN)
5519 need_barrier = 1;
5520 break;
5523 need_barrier = rtx_needs_barrier (PATTERN (insn), flags, 0);
5524 break;
5526 case JUMP_INSN:
5527 flags.is_branch = 1;
5529 /* Don't bundle a jump following a call. */
5530 if ((pat = prev_active_insn (insn))
5531 && GET_CODE (pat) == CALL_INSN)
5533 need_barrier = 1;
5534 break;
5536 /* FALLTHRU */
5538 case INSN:
5539 if (GET_CODE (PATTERN (insn)) == USE
5540 || GET_CODE (PATTERN (insn)) == CLOBBER)
5541 /* Don't care about USE and CLOBBER "insns"---those are used to
5542 indicate to the optimizer that it shouldn't get rid of
5543 certain operations. */
5544 break;
5546 pat = PATTERN (insn);
5548 /* Ug. Hack hacks hacked elsewhere. */
5549 switch (recog_memoized (insn))
5551 /* We play dependency tricks with the epilogue in order
5552 to get proper schedules. Undo this for dv analysis. */
5553 case CODE_FOR_epilogue_deallocate_stack:
5554 case CODE_FOR_prologue_allocate_stack:
5555 pat = XVECEXP (pat, 0, 0);
5556 break;
5558 /* The pattern we use for br.cloop confuses the code above.
5559 The second element of the vector is representative. */
5560 case CODE_FOR_doloop_end_internal:
5561 pat = XVECEXP (pat, 0, 1);
5562 break;
5564 /* Doesn't generate code. */
5565 case CODE_FOR_pred_rel_mutex:
5566 case CODE_FOR_prologue_use:
5567 return 0;
5569 default:
5570 break;
5573 memset (rws_insn, 0, sizeof (rws_insn));
5574 need_barrier = rtx_needs_barrier (pat, flags, 0);
5576 /* Check to see if the previous instruction was a volatile
5577 asm. */
5578 if (! need_barrier)
5579 need_barrier = rws_access_regno (REG_VOLATILE, flags, 0);
5580 break;
5582 default:
5583 abort ();
5586 if (first_instruction && INSN_P (insn)
5587 && ia64_safe_itanium_class (insn) != ITANIUM_CLASS_IGNORE
5588 && GET_CODE (PATTERN (insn)) != USE
5589 && GET_CODE (PATTERN (insn)) != CLOBBER)
5591 need_barrier = 0;
5592 first_instruction = 0;
5595 return need_barrier;
5598 /* Like group_barrier_needed_p, but do not clobber the current state. */
5600 static int
5601 safe_group_barrier_needed_p (rtx insn)
5603 struct reg_write_state rws_saved[NUM_REGS];
5604 int saved_first_instruction;
5605 int t;
5607 memcpy (rws_saved, rws_sum, NUM_REGS * sizeof *rws_saved);
5608 saved_first_instruction = first_instruction;
5610 t = group_barrier_needed_p (insn);
5612 memcpy (rws_sum, rws_saved, NUM_REGS * sizeof *rws_saved);
5613 first_instruction = saved_first_instruction;
5615 return t;
5618 /* Scan the current function and insert stop bits as necessary to
5619 eliminate dependencies. This function assumes that a final
5620 instruction scheduling pass has been run which has already
5621 inserted most of the necessary stop bits. This function only
5622 inserts new ones at basic block boundaries, since these are
5623 invisible to the scheduler. */
5625 static void
5626 emit_insn_group_barriers (FILE *dump)
5628 rtx insn;
5629 rtx last_label = 0;
5630 int insns_since_last_label = 0;
5632 init_insn_group_barriers ();
5634 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5636 if (GET_CODE (insn) == CODE_LABEL)
5638 if (insns_since_last_label)
5639 last_label = insn;
5640 insns_since_last_label = 0;
5642 else if (GET_CODE (insn) == NOTE
5643 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_BASIC_BLOCK)
5645 if (insns_since_last_label)
5646 last_label = insn;
5647 insns_since_last_label = 0;
5649 else if (GET_CODE (insn) == INSN
5650 && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
5651 && XINT (PATTERN (insn), 1) == UNSPECV_INSN_GROUP_BARRIER)
5653 init_insn_group_barriers ();
5654 last_label = 0;
5656 else if (INSN_P (insn))
5658 insns_since_last_label = 1;
5660 if (group_barrier_needed_p (insn))
5662 if (last_label)
5664 if (dump)
5665 fprintf (dump, "Emitting stop before label %d\n",
5666 INSN_UID (last_label));
5667 emit_insn_before (gen_insn_group_barrier (GEN_INT (3)), last_label);
5668 insn = last_label;
5670 init_insn_group_barriers ();
5671 last_label = 0;
5678 /* Like emit_insn_group_barriers, but run if no final scheduling pass was run.
5679 This function has to emit all necessary group barriers. */
5681 static void
5682 emit_all_insn_group_barriers (FILE *dump ATTRIBUTE_UNUSED)
5684 rtx insn;
5686 init_insn_group_barriers ();
5688 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5690 if (GET_CODE (insn) == BARRIER)
5692 rtx last = prev_active_insn (insn);
5694 if (! last)
5695 continue;
5696 if (GET_CODE (last) == JUMP_INSN
5697 && GET_CODE (PATTERN (last)) == ADDR_DIFF_VEC)
5698 last = prev_active_insn (last);
5699 if (recog_memoized (last) != CODE_FOR_insn_group_barrier)
5700 emit_insn_after (gen_insn_group_barrier (GEN_INT (3)), last);
5702 init_insn_group_barriers ();
5704 else if (INSN_P (insn))
5706 if (recog_memoized (insn) == CODE_FOR_insn_group_barrier)
5707 init_insn_group_barriers ();
5708 else if (group_barrier_needed_p (insn))
5710 emit_insn_before (gen_insn_group_barrier (GEN_INT (3)), insn);
5711 init_insn_group_barriers ();
5712 group_barrier_needed_p (insn);
5719 static int errata_find_address_regs (rtx *, void *);
5720 static void errata_emit_nops (rtx);
5721 static void fixup_errata (void);
5723 /* This structure is used to track some details about the previous insns
5724 groups so we can determine if it may be necessary to insert NOPs to
5725 workaround hardware errata. */
5726 static struct group
5728 HARD_REG_SET p_reg_set;
5729 HARD_REG_SET gr_reg_conditionally_set;
5730 } last_group[2];
5732 /* Index into the last_group array. */
5733 static int group_idx;
5735 /* Called through for_each_rtx; determines if a hard register that was
5736 conditionally set in the previous group is used as an address register.
5737 It ensures that for_each_rtx returns 1 in that case. */
5738 static int
5739 errata_find_address_regs (rtx *xp, void *data ATTRIBUTE_UNUSED)
5741 rtx x = *xp;
5742 if (GET_CODE (x) != MEM)
5743 return 0;
5744 x = XEXP (x, 0);
5745 if (GET_CODE (x) == POST_MODIFY)
5746 x = XEXP (x, 0);
5747 if (GET_CODE (x) == REG)
5749 struct group *prev_group = last_group + (group_idx ^ 1);
5750 if (TEST_HARD_REG_BIT (prev_group->gr_reg_conditionally_set,
5751 REGNO (x)))
5752 return 1;
5753 return -1;
5755 return 0;
5758 /* Called for each insn; this function keeps track of the state in
5759 last_group and emits additional NOPs if necessary to work around
5760 an Itanium A/B step erratum. */
5761 static void
5762 errata_emit_nops (rtx insn)
5764 struct group *this_group = last_group + group_idx;
5765 struct group *prev_group = last_group + (group_idx ^ 1);
5766 rtx pat = PATTERN (insn);
5767 rtx cond = GET_CODE (pat) == COND_EXEC ? COND_EXEC_TEST (pat) : 0;
5768 rtx real_pat = cond ? COND_EXEC_CODE (pat) : pat;
5769 enum attr_type type;
5770 rtx set = real_pat;
5772 if (GET_CODE (real_pat) == USE
5773 || GET_CODE (real_pat) == CLOBBER
5774 || GET_CODE (real_pat) == ASM_INPUT
5775 || GET_CODE (real_pat) == ADDR_VEC
5776 || GET_CODE (real_pat) == ADDR_DIFF_VEC
5777 || asm_noperands (PATTERN (insn)) >= 0)
5778 return;
5780 /* single_set doesn't work for COND_EXEC insns, so we have to duplicate
5781 parts of it. */
5783 if (GET_CODE (set) == PARALLEL)
5785 int i;
5786 set = XVECEXP (real_pat, 0, 0);
5787 for (i = 1; i < XVECLEN (real_pat, 0); i++)
5788 if (GET_CODE (XVECEXP (real_pat, 0, i)) != USE
5789 && GET_CODE (XVECEXP (real_pat, 0, i)) != CLOBBER)
5791 set = 0;
5792 break;
5796 if (set && GET_CODE (set) != SET)
5797 set = 0;
5799 type = get_attr_type (insn);
5801 if (type == TYPE_F
5802 && set && REG_P (SET_DEST (set)) && PR_REGNO_P (REGNO (SET_DEST (set))))
5803 SET_HARD_REG_BIT (this_group->p_reg_set, REGNO (SET_DEST (set)));
5805 if ((type == TYPE_M || type == TYPE_A) && cond && set
5806 && REG_P (SET_DEST (set))
5807 && GET_CODE (SET_SRC (set)) != PLUS
5808 && GET_CODE (SET_SRC (set)) != MINUS
5809 && (GET_CODE (SET_SRC (set)) != ASHIFT
5810 || !shladd_operand (XEXP (SET_SRC (set), 1), VOIDmode))
5811 && (GET_CODE (SET_SRC (set)) != MEM
5812 || GET_CODE (XEXP (SET_SRC (set), 0)) != POST_MODIFY)
5813 && GENERAL_REGNO_P (REGNO (SET_DEST (set))))
5815 if (GET_RTX_CLASS (GET_CODE (cond)) != '<'
5816 || ! REG_P (XEXP (cond, 0)))
5817 abort ();
5819 if (TEST_HARD_REG_BIT (prev_group->p_reg_set, REGNO (XEXP (cond, 0))))
5820 SET_HARD_REG_BIT (this_group->gr_reg_conditionally_set, REGNO (SET_DEST (set)));
5822 if (for_each_rtx (&real_pat, errata_find_address_regs, NULL))
5824 emit_insn_before (gen_insn_group_barrier (GEN_INT (3)), insn);
5825 emit_insn_before (gen_nop (), insn);
5826 emit_insn_before (gen_insn_group_barrier (GEN_INT (3)), insn);
5827 group_idx = 0;
5828 memset (last_group, 0, sizeof last_group);
5832 /* Emit extra nops if they are required to work around hardware errata. */
5834 static void
5835 fixup_errata (void)
5837 rtx insn;
5839 if (! TARGET_B_STEP)
5840 return;
5842 group_idx = 0;
5843 memset (last_group, 0, sizeof last_group);
5845 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5847 if (!INSN_P (insn))
5848 continue;
5850 if (ia64_safe_type (insn) == TYPE_S)
5852 group_idx ^= 1;
5853 memset (last_group + group_idx, 0, sizeof last_group[group_idx]);
5855 else
5856 errata_emit_nops (insn);
5861 /* Instruction scheduling support. */
5863 #define NR_BUNDLES 10
5865 /* A list of names of all available bundles. */
5867 static const char *bundle_name [NR_BUNDLES] =
5869 ".mii",
5870 ".mmi",
5871 ".mfi",
5872 ".mmf",
5873 #if NR_BUNDLES == 10
5874 ".bbb",
5875 ".mbb",
5876 #endif
5877 ".mib",
5878 ".mmb",
5879 ".mfb",
5880 ".mlx"
5883 /* Nonzero if we should insert stop bits into the schedule. */
5885 int ia64_final_schedule = 0;
5887 /* Codes of the corresponding quieryied units: */
5889 static int _0mii_, _0mmi_, _0mfi_, _0mmf_;
5890 static int _0bbb_, _0mbb_, _0mib_, _0mmb_, _0mfb_, _0mlx_;
5892 static int _1mii_, _1mmi_, _1mfi_, _1mmf_;
5893 static int _1bbb_, _1mbb_, _1mib_, _1mmb_, _1mfb_, _1mlx_;
5895 static int pos_1, pos_2, pos_3, pos_4, pos_5, pos_6;
5897 /* The following variable value is an insn group barrier. */
5899 static rtx dfa_stop_insn;
5901 /* The following variable value is the last issued insn. */
5903 static rtx last_scheduled_insn;
5905 /* The following variable value is size of the DFA state. */
5907 static size_t dfa_state_size;
5909 /* The following variable value is pointer to a DFA state used as
5910 temporary variable. */
5912 static state_t temp_dfa_state = NULL;
5914 /* The following variable value is DFA state after issuing the last
5915 insn. */
5917 static state_t prev_cycle_state = NULL;
5919 /* The following array element values are TRUE if the corresponding
5920 insn requires to add stop bits before it. */
5922 static char *stops_p;
5924 /* The following variable is used to set up the mentioned above array. */
5926 static int stop_before_p = 0;
5928 /* The following variable value is length of the arrays `clocks' and
5929 `add_cycles'. */
5931 static int clocks_length;
5933 /* The following array element values are cycles on which the
5934 corresponding insn will be issued. The array is used only for
5935 Itanium1. */
5937 static int *clocks;
5939 /* The following array element values are numbers of cycles should be
5940 added to improve insn scheduling for MM_insns for Itanium1. */
5942 static int *add_cycles;
5944 static rtx ia64_single_set (rtx);
5945 static void ia64_emit_insn_before (rtx, rtx);
5947 /* Map a bundle number to its pseudo-op. */
5949 const char *
5950 get_bundle_name (int b)
5952 return bundle_name[b];
5956 /* Return the maximum number of instructions a cpu can issue. */
5958 static int
5959 ia64_issue_rate (void)
5961 return 6;
5964 /* Helper function - like single_set, but look inside COND_EXEC. */
5966 static rtx
5967 ia64_single_set (rtx insn)
5969 rtx x = PATTERN (insn), ret;
5970 if (GET_CODE (x) == COND_EXEC)
5971 x = COND_EXEC_CODE (x);
5972 if (GET_CODE (x) == SET)
5973 return x;
5975 /* Special case here prologue_allocate_stack and epilogue_deallocate_stack.
5976 Although they are not classical single set, the second set is there just
5977 to protect it from moving past FP-relative stack accesses. */
5978 switch (recog_memoized (insn))
5980 case CODE_FOR_prologue_allocate_stack:
5981 case CODE_FOR_epilogue_deallocate_stack:
5982 ret = XVECEXP (x, 0, 0);
5983 break;
5985 default:
5986 ret = single_set_2 (insn, x);
5987 break;
5990 return ret;
5993 /* Adjust the cost of a scheduling dependency. Return the new cost of
5994 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
5996 static int
5997 ia64_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
5999 enum attr_itanium_class dep_class;
6000 enum attr_itanium_class insn_class;
6002 if (REG_NOTE_KIND (link) != REG_DEP_OUTPUT)
6003 return cost;
6005 insn_class = ia64_safe_itanium_class (insn);
6006 dep_class = ia64_safe_itanium_class (dep_insn);
6007 if (dep_class == ITANIUM_CLASS_ST || dep_class == ITANIUM_CLASS_STF
6008 || insn_class == ITANIUM_CLASS_ST || insn_class == ITANIUM_CLASS_STF)
6009 return 0;
6011 return cost;
6014 /* Like emit_insn_before, but skip cycle_display notes.
6015 ??? When cycle display notes are implemented, update this. */
6017 static void
6018 ia64_emit_insn_before (rtx insn, rtx before)
6020 emit_insn_before (insn, before);
6023 /* The following function marks insns who produce addresses for load
6024 and store insns. Such insns will be placed into M slots because it
6025 decrease latency time for Itanium1 (see function
6026 `ia64_produce_address_p' and the DFA descriptions). */
6028 static void
6029 ia64_dependencies_evaluation_hook (rtx head, rtx tail)
6031 rtx insn, link, next, next_tail;
6033 next_tail = NEXT_INSN (tail);
6034 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
6035 if (INSN_P (insn))
6036 insn->call = 0;
6037 for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
6038 if (INSN_P (insn)
6039 && ia64_safe_itanium_class (insn) == ITANIUM_CLASS_IALU)
6041 for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
6043 next = XEXP (link, 0);
6044 if ((ia64_safe_itanium_class (next) == ITANIUM_CLASS_ST
6045 || ia64_safe_itanium_class (next) == ITANIUM_CLASS_STF)
6046 && ia64_st_address_bypass_p (insn, next))
6047 break;
6048 else if ((ia64_safe_itanium_class (next) == ITANIUM_CLASS_LD
6049 || ia64_safe_itanium_class (next)
6050 == ITANIUM_CLASS_FLD)
6051 && ia64_ld_address_bypass_p (insn, next))
6052 break;
6054 insn->call = link != 0;
6058 /* We're beginning a new block. Initialize data structures as necessary. */
6060 static void
6061 ia64_sched_init (FILE *dump ATTRIBUTE_UNUSED,
6062 int sched_verbose ATTRIBUTE_UNUSED,
6063 int max_ready ATTRIBUTE_UNUSED)
6065 #ifdef ENABLE_CHECKING
6066 rtx insn;
6068 if (reload_completed)
6069 for (insn = NEXT_INSN (current_sched_info->prev_head);
6070 insn != current_sched_info->next_tail;
6071 insn = NEXT_INSN (insn))
6072 if (SCHED_GROUP_P (insn))
6073 abort ();
6074 #endif
6075 last_scheduled_insn = NULL_RTX;
6076 init_insn_group_barriers ();
6079 /* We are about to being issuing insns for this clock cycle.
6080 Override the default sort algorithm to better slot instructions. */
6082 static int
6083 ia64_dfa_sched_reorder (FILE *dump, int sched_verbose, rtx *ready,
6084 int *pn_ready, int clock_var ATTRIBUTE_UNUSED,
6085 int reorder_type)
6087 int n_asms;
6088 int n_ready = *pn_ready;
6089 rtx *e_ready = ready + n_ready;
6090 rtx *insnp;
6092 if (sched_verbose)
6093 fprintf (dump, "// ia64_dfa_sched_reorder (type %d):\n", reorder_type);
6095 if (reorder_type == 0)
6097 /* First, move all USEs, CLOBBERs and other crud out of the way. */
6098 n_asms = 0;
6099 for (insnp = ready; insnp < e_ready; insnp++)
6100 if (insnp < e_ready)
6102 rtx insn = *insnp;
6103 enum attr_type t = ia64_safe_type (insn);
6104 if (t == TYPE_UNKNOWN)
6106 if (GET_CODE (PATTERN (insn)) == ASM_INPUT
6107 || asm_noperands (PATTERN (insn)) >= 0)
6109 rtx lowest = ready[n_asms];
6110 ready[n_asms] = insn;
6111 *insnp = lowest;
6112 n_asms++;
6114 else
6116 rtx highest = ready[n_ready - 1];
6117 ready[n_ready - 1] = insn;
6118 *insnp = highest;
6119 return 1;
6124 if (n_asms < n_ready)
6126 /* Some normal insns to process. Skip the asms. */
6127 ready += n_asms;
6128 n_ready -= n_asms;
6130 else if (n_ready > 0)
6131 return 1;
6134 if (ia64_final_schedule)
6136 int deleted = 0;
6137 int nr_need_stop = 0;
6139 for (insnp = ready; insnp < e_ready; insnp++)
6140 if (safe_group_barrier_needed_p (*insnp))
6141 nr_need_stop++;
6143 if (reorder_type == 1 && n_ready == nr_need_stop)
6144 return 0;
6145 if (reorder_type == 0)
6146 return 1;
6147 insnp = e_ready;
6148 /* Move down everything that needs a stop bit, preserving
6149 relative order. */
6150 while (insnp-- > ready + deleted)
6151 while (insnp >= ready + deleted)
6153 rtx insn = *insnp;
6154 if (! safe_group_barrier_needed_p (insn))
6155 break;
6156 memmove (ready + 1, ready, (insnp - ready) * sizeof (rtx));
6157 *ready = insn;
6158 deleted++;
6160 n_ready -= deleted;
6161 ready += deleted;
6164 return 1;
6167 /* We are about to being issuing insns for this clock cycle. Override
6168 the default sort algorithm to better slot instructions. */
6170 static int
6171 ia64_sched_reorder (FILE *dump, int sched_verbose, rtx *ready, int *pn_ready,
6172 int clock_var)
6174 return ia64_dfa_sched_reorder (dump, sched_verbose, ready,
6175 pn_ready, clock_var, 0);
6178 /* Like ia64_sched_reorder, but called after issuing each insn.
6179 Override the default sort algorithm to better slot instructions. */
6181 static int
6182 ia64_sched_reorder2 (FILE *dump ATTRIBUTE_UNUSED,
6183 int sched_verbose ATTRIBUTE_UNUSED, rtx *ready,
6184 int *pn_ready, int clock_var)
6186 if (ia64_tune == PROCESSOR_ITANIUM && reload_completed && last_scheduled_insn)
6187 clocks [INSN_UID (last_scheduled_insn)] = clock_var;
6188 return ia64_dfa_sched_reorder (dump, sched_verbose, ready, pn_ready,
6189 clock_var, 1);
6192 /* We are about to issue INSN. Return the number of insns left on the
6193 ready queue that can be issued this cycle. */
6195 static int
6196 ia64_variable_issue (FILE *dump ATTRIBUTE_UNUSED,
6197 int sched_verbose ATTRIBUTE_UNUSED,
6198 rtx insn ATTRIBUTE_UNUSED,
6199 int can_issue_more ATTRIBUTE_UNUSED)
6201 last_scheduled_insn = insn;
6202 memcpy (prev_cycle_state, curr_state, dfa_state_size);
6203 if (reload_completed)
6205 if (group_barrier_needed_p (insn))
6206 abort ();
6207 if (GET_CODE (insn) == CALL_INSN)
6208 init_insn_group_barriers ();
6209 stops_p [INSN_UID (insn)] = stop_before_p;
6210 stop_before_p = 0;
6212 return 1;
6215 /* We are choosing insn from the ready queue. Return nonzero if INSN
6216 can be chosen. */
6218 static int
6219 ia64_first_cycle_multipass_dfa_lookahead_guard (rtx insn)
6221 if (insn == NULL_RTX || !INSN_P (insn))
6222 abort ();
6223 return (!reload_completed
6224 || !safe_group_barrier_needed_p (insn));
6227 /* The following variable value is pseudo-insn used by the DFA insn
6228 scheduler to change the DFA state when the simulated clock is
6229 increased. */
6231 static rtx dfa_pre_cycle_insn;
6233 /* We are about to being issuing INSN. Return nonzero if we can not
6234 issue it on given cycle CLOCK and return zero if we should not sort
6235 the ready queue on the next clock start. */
6237 static int
6238 ia64_dfa_new_cycle (FILE *dump, int verbose, rtx insn, int last_clock,
6239 int clock, int *sort_p)
6241 int setup_clocks_p = FALSE;
6243 if (insn == NULL_RTX || !INSN_P (insn))
6244 abort ();
6245 if ((reload_completed && safe_group_barrier_needed_p (insn))
6246 || (last_scheduled_insn
6247 && (GET_CODE (last_scheduled_insn) == CALL_INSN
6248 || GET_CODE (PATTERN (last_scheduled_insn)) == ASM_INPUT
6249 || asm_noperands (PATTERN (last_scheduled_insn)) >= 0)))
6251 init_insn_group_barriers ();
6252 if (verbose && dump)
6253 fprintf (dump, "// Stop should be before %d%s\n", INSN_UID (insn),
6254 last_clock == clock ? " + cycle advance" : "");
6255 stop_before_p = 1;
6256 if (last_clock == clock)
6258 state_transition (curr_state, dfa_stop_insn);
6259 if (TARGET_EARLY_STOP_BITS)
6260 *sort_p = (last_scheduled_insn == NULL_RTX
6261 || GET_CODE (last_scheduled_insn) != CALL_INSN);
6262 else
6263 *sort_p = 0;
6264 return 1;
6266 else if (reload_completed)
6267 setup_clocks_p = TRUE;
6268 memcpy (curr_state, prev_cycle_state, dfa_state_size);
6269 state_transition (curr_state, dfa_stop_insn);
6270 state_transition (curr_state, dfa_pre_cycle_insn);
6271 state_transition (curr_state, NULL);
6273 else if (reload_completed)
6274 setup_clocks_p = TRUE;
6275 if (setup_clocks_p && ia64_tune == PROCESSOR_ITANIUM)
6277 enum attr_itanium_class c = ia64_safe_itanium_class (insn);
6279 if (c != ITANIUM_CLASS_MMMUL && c != ITANIUM_CLASS_MMSHF)
6281 rtx link;
6282 int d = -1;
6284 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
6285 if (REG_NOTE_KIND (link) == 0)
6287 enum attr_itanium_class dep_class;
6288 rtx dep_insn = XEXP (link, 0);
6290 dep_class = ia64_safe_itanium_class (dep_insn);
6291 if ((dep_class == ITANIUM_CLASS_MMMUL
6292 || dep_class == ITANIUM_CLASS_MMSHF)
6293 && last_clock - clocks [INSN_UID (dep_insn)] < 4
6294 && (d < 0
6295 || last_clock - clocks [INSN_UID (dep_insn)] < d))
6296 d = last_clock - clocks [INSN_UID (dep_insn)];
6298 if (d >= 0)
6299 add_cycles [INSN_UID (insn)] = 3 - d;
6302 return 0;
6307 /* The following page contains abstract data `bundle states' which are
6308 used for bundling insns (inserting nops and template generation). */
6310 /* The following describes state of insn bundling. */
6312 struct bundle_state
6314 /* Unique bundle state number to identify them in the debugging
6315 output */
6316 int unique_num;
6317 rtx insn; /* corresponding insn, NULL for the 1st and the last state */
6318 /* number nops before and after the insn */
6319 short before_nops_num, after_nops_num;
6320 int insn_num; /* insn number (0 - for initial state, 1 - for the 1st
6321 insn */
6322 int cost; /* cost of the state in cycles */
6323 int accumulated_insns_num; /* number of all previous insns including
6324 nops. L is considered as 2 insns */
6325 int branch_deviation; /* deviation of previous branches from 3rd slots */
6326 struct bundle_state *next; /* next state with the same insn_num */
6327 struct bundle_state *originator; /* originator (previous insn state) */
6328 /* All bundle states are in the following chain. */
6329 struct bundle_state *allocated_states_chain;
6330 /* The DFA State after issuing the insn and the nops. */
6331 state_t dfa_state;
6334 /* The following is map insn number to the corresponding bundle state. */
6336 static struct bundle_state **index_to_bundle_states;
6338 /* The unique number of next bundle state. */
6340 static int bundle_states_num;
6342 /* All allocated bundle states are in the following chain. */
6344 static struct bundle_state *allocated_bundle_states_chain;
6346 /* All allocated but not used bundle states are in the following
6347 chain. */
6349 static struct bundle_state *free_bundle_state_chain;
6352 /* The following function returns a free bundle state. */
6354 static struct bundle_state *
6355 get_free_bundle_state (void)
6357 struct bundle_state *result;
6359 if (free_bundle_state_chain != NULL)
6361 result = free_bundle_state_chain;
6362 free_bundle_state_chain = result->next;
6364 else
6366 result = xmalloc (sizeof (struct bundle_state));
6367 result->dfa_state = xmalloc (dfa_state_size);
6368 result->allocated_states_chain = allocated_bundle_states_chain;
6369 allocated_bundle_states_chain = result;
6371 result->unique_num = bundle_states_num++;
6372 return result;
6376 /* The following function frees given bundle state. */
6378 static void
6379 free_bundle_state (struct bundle_state *state)
6381 state->next = free_bundle_state_chain;
6382 free_bundle_state_chain = state;
6385 /* Start work with abstract data `bundle states'. */
6387 static void
6388 initiate_bundle_states (void)
6390 bundle_states_num = 0;
6391 free_bundle_state_chain = NULL;
6392 allocated_bundle_states_chain = NULL;
6395 /* Finish work with abstract data `bundle states'. */
6397 static void
6398 finish_bundle_states (void)
6400 struct bundle_state *curr_state, *next_state;
6402 for (curr_state = allocated_bundle_states_chain;
6403 curr_state != NULL;
6404 curr_state = next_state)
6406 next_state = curr_state->allocated_states_chain;
6407 free (curr_state->dfa_state);
6408 free (curr_state);
6412 /* Hash table of the bundle states. The key is dfa_state and insn_num
6413 of the bundle states. */
6415 static htab_t bundle_state_table;
6417 /* The function returns hash of BUNDLE_STATE. */
6419 static unsigned
6420 bundle_state_hash (const void *bundle_state)
6422 const struct bundle_state *state = (struct bundle_state *) bundle_state;
6423 unsigned result, i;
6425 for (result = i = 0; i < dfa_state_size; i++)
6426 result += (((unsigned char *) state->dfa_state) [i]
6427 << ((i % CHAR_BIT) * 3 + CHAR_BIT));
6428 return result + state->insn_num;
6431 /* The function returns nonzero if the bundle state keys are equal. */
6433 static int
6434 bundle_state_eq_p (const void *bundle_state_1, const void *bundle_state_2)
6436 const struct bundle_state * state1 = (struct bundle_state *) bundle_state_1;
6437 const struct bundle_state * state2 = (struct bundle_state *) bundle_state_2;
6439 return (state1->insn_num == state2->insn_num
6440 && memcmp (state1->dfa_state, state2->dfa_state,
6441 dfa_state_size) == 0);
6444 /* The function inserts the BUNDLE_STATE into the hash table. The
6445 function returns nonzero if the bundle has been inserted into the
6446 table. The table contains the best bundle state with given key. */
6448 static int
6449 insert_bundle_state (struct bundle_state *bundle_state)
6451 void **entry_ptr;
6453 entry_ptr = htab_find_slot (bundle_state_table, bundle_state, 1);
6454 if (*entry_ptr == NULL)
6456 bundle_state->next = index_to_bundle_states [bundle_state->insn_num];
6457 index_to_bundle_states [bundle_state->insn_num] = bundle_state;
6458 *entry_ptr = (void *) bundle_state;
6459 return TRUE;
6461 else if (bundle_state->cost < ((struct bundle_state *) *entry_ptr)->cost
6462 || (bundle_state->cost == ((struct bundle_state *) *entry_ptr)->cost
6463 && (((struct bundle_state *)*entry_ptr)->accumulated_insns_num
6464 > bundle_state->accumulated_insns_num
6465 || (((struct bundle_state *)
6466 *entry_ptr)->accumulated_insns_num
6467 == bundle_state->accumulated_insns_num
6468 && ((struct bundle_state *)
6469 *entry_ptr)->branch_deviation
6470 > bundle_state->branch_deviation))))
6473 struct bundle_state temp;
6475 temp = *(struct bundle_state *) *entry_ptr;
6476 *(struct bundle_state *) *entry_ptr = *bundle_state;
6477 ((struct bundle_state *) *entry_ptr)->next = temp.next;
6478 *bundle_state = temp;
6480 return FALSE;
6483 /* Start work with the hash table. */
6485 static void
6486 initiate_bundle_state_table (void)
6488 bundle_state_table = htab_create (50, bundle_state_hash, bundle_state_eq_p,
6489 (htab_del) 0);
6492 /* Finish work with the hash table. */
6494 static void
6495 finish_bundle_state_table (void)
6497 htab_delete (bundle_state_table);
6502 /* The following variable is a insn `nop' used to check bundle states
6503 with different number of inserted nops. */
6505 static rtx ia64_nop;
6507 /* The following function tries to issue NOPS_NUM nops for the current
6508 state without advancing processor cycle. If it failed, the
6509 function returns FALSE and frees the current state. */
6511 static int
6512 try_issue_nops (struct bundle_state *curr_state, int nops_num)
6514 int i;
6516 for (i = 0; i < nops_num; i++)
6517 if (state_transition (curr_state->dfa_state, ia64_nop) >= 0)
6519 free_bundle_state (curr_state);
6520 return FALSE;
6522 return TRUE;
6525 /* The following function tries to issue INSN for the current
6526 state without advancing processor cycle. If it failed, the
6527 function returns FALSE and frees the current state. */
6529 static int
6530 try_issue_insn (struct bundle_state *curr_state, rtx insn)
6532 if (insn && state_transition (curr_state->dfa_state, insn) >= 0)
6534 free_bundle_state (curr_state);
6535 return FALSE;
6537 return TRUE;
6540 /* The following function tries to issue BEFORE_NOPS_NUM nops and INSN
6541 starting with ORIGINATOR without advancing processor cycle. If
6542 TRY_BUNDLE_END_P is TRUE, the function also/only (if
6543 ONLY_BUNDLE_END_P is TRUE) tries to issue nops to fill all bundle.
6544 If it was successful, the function creates new bundle state and
6545 insert into the hash table and into `index_to_bundle_states'. */
6547 static void
6548 issue_nops_and_insn (struct bundle_state *originator, int before_nops_num,
6549 rtx insn, int try_bundle_end_p, int only_bundle_end_p)
6551 struct bundle_state *curr_state;
6553 curr_state = get_free_bundle_state ();
6554 memcpy (curr_state->dfa_state, originator->dfa_state, dfa_state_size);
6555 curr_state->insn = insn;
6556 curr_state->insn_num = originator->insn_num + 1;
6557 curr_state->cost = originator->cost;
6558 curr_state->originator = originator;
6559 curr_state->before_nops_num = before_nops_num;
6560 curr_state->after_nops_num = 0;
6561 curr_state->accumulated_insns_num
6562 = originator->accumulated_insns_num + before_nops_num;
6563 curr_state->branch_deviation = originator->branch_deviation;
6564 if (insn == NULL_RTX)
6565 abort ();
6566 else if (INSN_CODE (insn) == CODE_FOR_insn_group_barrier)
6568 if (GET_MODE (insn) == TImode)
6569 abort ();
6570 if (!try_issue_nops (curr_state, before_nops_num))
6571 return;
6572 if (!try_issue_insn (curr_state, insn))
6573 return;
6574 memcpy (temp_dfa_state, curr_state->dfa_state, dfa_state_size);
6575 if (state_transition (temp_dfa_state, dfa_pre_cycle_insn) >= 0
6576 && curr_state->accumulated_insns_num % 3 != 0)
6578 free_bundle_state (curr_state);
6579 return;
6582 else if (GET_MODE (insn) != TImode)
6584 if (!try_issue_nops (curr_state, before_nops_num))
6585 return;
6586 if (!try_issue_insn (curr_state, insn))
6587 return;
6588 curr_state->accumulated_insns_num++;
6589 if (GET_CODE (PATTERN (insn)) == ASM_INPUT
6590 || asm_noperands (PATTERN (insn)) >= 0)
6591 abort ();
6592 if (ia64_safe_type (insn) == TYPE_L)
6593 curr_state->accumulated_insns_num++;
6595 else
6597 state_transition (curr_state->dfa_state, dfa_pre_cycle_insn);
6598 state_transition (curr_state->dfa_state, NULL);
6599 curr_state->cost++;
6600 if (!try_issue_nops (curr_state, before_nops_num))
6601 return;
6602 if (!try_issue_insn (curr_state, insn))
6603 return;
6604 curr_state->accumulated_insns_num++;
6605 if (GET_CODE (PATTERN (insn)) == ASM_INPUT
6606 || asm_noperands (PATTERN (insn)) >= 0)
6608 /* Finish bundle containing asm insn. */
6609 curr_state->after_nops_num
6610 = 3 - curr_state->accumulated_insns_num % 3;
6611 curr_state->accumulated_insns_num
6612 += 3 - curr_state->accumulated_insns_num % 3;
6614 else if (ia64_safe_type (insn) == TYPE_L)
6615 curr_state->accumulated_insns_num++;
6617 if (ia64_safe_type (insn) == TYPE_B)
6618 curr_state->branch_deviation
6619 += 2 - (curr_state->accumulated_insns_num - 1) % 3;
6620 if (try_bundle_end_p && curr_state->accumulated_insns_num % 3 != 0)
6622 if (!only_bundle_end_p && insert_bundle_state (curr_state))
6624 state_t dfa_state;
6625 struct bundle_state *curr_state1;
6626 struct bundle_state *allocated_states_chain;
6628 curr_state1 = get_free_bundle_state ();
6629 dfa_state = curr_state1->dfa_state;
6630 allocated_states_chain = curr_state1->allocated_states_chain;
6631 *curr_state1 = *curr_state;
6632 curr_state1->dfa_state = dfa_state;
6633 curr_state1->allocated_states_chain = allocated_states_chain;
6634 memcpy (curr_state1->dfa_state, curr_state->dfa_state,
6635 dfa_state_size);
6636 curr_state = curr_state1;
6638 if (!try_issue_nops (curr_state,
6639 3 - curr_state->accumulated_insns_num % 3))
6640 return;
6641 curr_state->after_nops_num
6642 = 3 - curr_state->accumulated_insns_num % 3;
6643 curr_state->accumulated_insns_num
6644 += 3 - curr_state->accumulated_insns_num % 3;
6646 if (!insert_bundle_state (curr_state))
6647 free_bundle_state (curr_state);
6648 return;
6651 /* The following function returns position in the two window bundle
6652 for given STATE. */
6654 static int
6655 get_max_pos (state_t state)
6657 if (cpu_unit_reservation_p (state, pos_6))
6658 return 6;
6659 else if (cpu_unit_reservation_p (state, pos_5))
6660 return 5;
6661 else if (cpu_unit_reservation_p (state, pos_4))
6662 return 4;
6663 else if (cpu_unit_reservation_p (state, pos_3))
6664 return 3;
6665 else if (cpu_unit_reservation_p (state, pos_2))
6666 return 2;
6667 else if (cpu_unit_reservation_p (state, pos_1))
6668 return 1;
6669 else
6670 return 0;
6673 /* The function returns code of a possible template for given position
6674 and state. The function should be called only with 2 values of
6675 position equal to 3 or 6. */
6677 static int
6678 get_template (state_t state, int pos)
6680 switch (pos)
6682 case 3:
6683 if (cpu_unit_reservation_p (state, _0mii_))
6684 return 0;
6685 else if (cpu_unit_reservation_p (state, _0mmi_))
6686 return 1;
6687 else if (cpu_unit_reservation_p (state, _0mfi_))
6688 return 2;
6689 else if (cpu_unit_reservation_p (state, _0mmf_))
6690 return 3;
6691 else if (cpu_unit_reservation_p (state, _0bbb_))
6692 return 4;
6693 else if (cpu_unit_reservation_p (state, _0mbb_))
6694 return 5;
6695 else if (cpu_unit_reservation_p (state, _0mib_))
6696 return 6;
6697 else if (cpu_unit_reservation_p (state, _0mmb_))
6698 return 7;
6699 else if (cpu_unit_reservation_p (state, _0mfb_))
6700 return 8;
6701 else if (cpu_unit_reservation_p (state, _0mlx_))
6702 return 9;
6703 else
6704 abort ();
6705 case 6:
6706 if (cpu_unit_reservation_p (state, _1mii_))
6707 return 0;
6708 else if (cpu_unit_reservation_p (state, _1mmi_))
6709 return 1;
6710 else if (cpu_unit_reservation_p (state, _1mfi_))
6711 return 2;
6712 else if (_1mmf_ >= 0 && cpu_unit_reservation_p (state, _1mmf_))
6713 return 3;
6714 else if (cpu_unit_reservation_p (state, _1bbb_))
6715 return 4;
6716 else if (cpu_unit_reservation_p (state, _1mbb_))
6717 return 5;
6718 else if (cpu_unit_reservation_p (state, _1mib_))
6719 return 6;
6720 else if (cpu_unit_reservation_p (state, _1mmb_))
6721 return 7;
6722 else if (cpu_unit_reservation_p (state, _1mfb_))
6723 return 8;
6724 else if (cpu_unit_reservation_p (state, _1mlx_))
6725 return 9;
6726 else
6727 abort ();
6728 default:
6729 abort ();
6733 /* The following function returns an insn important for insn bundling
6734 followed by INSN and before TAIL. */
6736 static rtx
6737 get_next_important_insn (rtx insn, rtx tail)
6739 for (; insn && insn != tail; insn = NEXT_INSN (insn))
6740 if (INSN_P (insn)
6741 && ia64_safe_itanium_class (insn) != ITANIUM_CLASS_IGNORE
6742 && GET_CODE (PATTERN (insn)) != USE
6743 && GET_CODE (PATTERN (insn)) != CLOBBER)
6744 return insn;
6745 return NULL_RTX;
6748 /* The following function does insn bundling. Bundling means
6749 inserting templates and nop insns to fit insn groups into permitted
6750 templates. Instruction scheduling uses NDFA (non-deterministic
6751 finite automata) encoding informations about the templates and the
6752 inserted nops. Nondeterminism of the automata permits follows
6753 all possible insn sequences very fast.
6755 Unfortunately it is not possible to get information about inserting
6756 nop insns and used templates from the automata states. The
6757 automata only says that we can issue an insn possibly inserting
6758 some nops before it and using some template. Therefore insn
6759 bundling in this function is implemented by using DFA
6760 (deterministic finite automata). We follows all possible insn
6761 sequences by inserting 0-2 nops (that is what the NDFA describe for
6762 insn scheduling) before/after each insn being bundled. We know the
6763 start of simulated processor cycle from insn scheduling (insn
6764 starting a new cycle has TImode).
6766 Simple implementation of insn bundling would create enormous
6767 number of possible insn sequences satisfying information about new
6768 cycle ticks taken from the insn scheduling. To make the algorithm
6769 practical we use dynamic programming. Each decision (about
6770 inserting nops and implicitly about previous decisions) is described
6771 by structure bundle_state (see above). If we generate the same
6772 bundle state (key is automaton state after issuing the insns and
6773 nops for it), we reuse already generated one. As consequence we
6774 reject some decisions which can not improve the solution and
6775 reduce memory for the algorithm.
6777 When we reach the end of EBB (extended basic block), we choose the
6778 best sequence and then, moving back in EBB, insert templates for
6779 the best alternative. The templates are taken from querying
6780 automaton state for each insn in chosen bundle states.
6782 So the algorithm makes two (forward and backward) passes through
6783 EBB. There is an additional forward pass through EBB for Itanium1
6784 processor. This pass inserts more nops to make dependency between
6785 a producer insn and MMMUL/MMSHF at least 4 cycles long. */
6787 static void
6788 bundling (FILE *dump, int verbose, rtx prev_head_insn, rtx tail)
6790 struct bundle_state *curr_state, *next_state, *best_state;
6791 rtx insn, next_insn;
6792 int insn_num;
6793 int i, bundle_end_p, only_bundle_end_p, asm_p;
6794 int pos = 0, max_pos, template0, template1;
6795 rtx b;
6796 rtx nop;
6797 enum attr_type type;
6799 insn_num = 0;
6800 /* Count insns in the EBB. */
6801 for (insn = NEXT_INSN (prev_head_insn);
6802 insn && insn != tail;
6803 insn = NEXT_INSN (insn))
6804 if (INSN_P (insn))
6805 insn_num++;
6806 if (insn_num == 0)
6807 return;
6808 bundling_p = 1;
6809 dfa_clean_insn_cache ();
6810 initiate_bundle_state_table ();
6811 index_to_bundle_states = xmalloc ((insn_num + 2)
6812 * sizeof (struct bundle_state *));
6813 /* First (forward) pass -- generation of bundle states. */
6814 curr_state = get_free_bundle_state ();
6815 curr_state->insn = NULL;
6816 curr_state->before_nops_num = 0;
6817 curr_state->after_nops_num = 0;
6818 curr_state->insn_num = 0;
6819 curr_state->cost = 0;
6820 curr_state->accumulated_insns_num = 0;
6821 curr_state->branch_deviation = 0;
6822 curr_state->next = NULL;
6823 curr_state->originator = NULL;
6824 state_reset (curr_state->dfa_state);
6825 index_to_bundle_states [0] = curr_state;
6826 insn_num = 0;
6827 /* Shift cycle mark if it is put on insn which could be ignored. */
6828 for (insn = NEXT_INSN (prev_head_insn);
6829 insn != tail;
6830 insn = NEXT_INSN (insn))
6831 if (INSN_P (insn)
6832 && (ia64_safe_itanium_class (insn) == ITANIUM_CLASS_IGNORE
6833 || GET_CODE (PATTERN (insn)) == USE
6834 || GET_CODE (PATTERN (insn)) == CLOBBER)
6835 && GET_MODE (insn) == TImode)
6837 PUT_MODE (insn, VOIDmode);
6838 for (next_insn = NEXT_INSN (insn);
6839 next_insn != tail;
6840 next_insn = NEXT_INSN (next_insn))
6841 if (INSN_P (next_insn)
6842 && ia64_safe_itanium_class (next_insn) != ITANIUM_CLASS_IGNORE
6843 && GET_CODE (PATTERN (next_insn)) != USE
6844 && GET_CODE (PATTERN (next_insn)) != CLOBBER)
6846 PUT_MODE (next_insn, TImode);
6847 break;
6850 /* Froward pass: generation of bundle states. */
6851 for (insn = get_next_important_insn (NEXT_INSN (prev_head_insn), tail);
6852 insn != NULL_RTX;
6853 insn = next_insn)
6855 if (!INSN_P (insn)
6856 || ia64_safe_itanium_class (insn) == ITANIUM_CLASS_IGNORE
6857 || GET_CODE (PATTERN (insn)) == USE
6858 || GET_CODE (PATTERN (insn)) == CLOBBER)
6859 abort ();
6860 type = ia64_safe_type (insn);
6861 next_insn = get_next_important_insn (NEXT_INSN (insn), tail);
6862 insn_num++;
6863 index_to_bundle_states [insn_num] = NULL;
6864 for (curr_state = index_to_bundle_states [insn_num - 1];
6865 curr_state != NULL;
6866 curr_state = next_state)
6868 pos = curr_state->accumulated_insns_num % 3;
6869 next_state = curr_state->next;
6870 /* We must fill up the current bundle in order to start a
6871 subsequent asm insn in a new bundle. Asm insn is always
6872 placed in a separate bundle. */
6873 only_bundle_end_p
6874 = (next_insn != NULL_RTX
6875 && INSN_CODE (insn) == CODE_FOR_insn_group_barrier
6876 && ia64_safe_type (next_insn) == TYPE_UNKNOWN);
6877 /* We may fill up the current bundle if it is the cycle end
6878 without a group barrier. */
6879 bundle_end_p
6880 = (only_bundle_end_p || next_insn == NULL_RTX
6881 || (GET_MODE (next_insn) == TImode
6882 && INSN_CODE (insn) != CODE_FOR_insn_group_barrier));
6883 if (type == TYPE_F || type == TYPE_B || type == TYPE_L
6884 || type == TYPE_S
6885 /* We need to insert 2 nops for cases like M_MII. To
6886 guarantee issuing all insns on the same cycle for
6887 Itanium 1, we need to issue 2 nops after the first M
6888 insn (MnnMII where n is a nop insn). */
6889 || (type == TYPE_M && ia64_tune == PROCESSOR_ITANIUM
6890 && !bundle_end_p && pos == 1))
6891 issue_nops_and_insn (curr_state, 2, insn, bundle_end_p,
6892 only_bundle_end_p);
6893 issue_nops_and_insn (curr_state, 1, insn, bundle_end_p,
6894 only_bundle_end_p);
6895 issue_nops_and_insn (curr_state, 0, insn, bundle_end_p,
6896 only_bundle_end_p);
6898 if (index_to_bundle_states [insn_num] == NULL)
6899 abort ();
6900 for (curr_state = index_to_bundle_states [insn_num];
6901 curr_state != NULL;
6902 curr_state = curr_state->next)
6903 if (verbose >= 2 && dump)
6905 /* This structure is taken from generated code of the
6906 pipeline hazard recognizer (see file insn-attrtab.c).
6907 Please don't forget to change the structure if a new
6908 automaton is added to .md file. */
6909 struct DFA_chip
6911 unsigned short one_automaton_state;
6912 unsigned short oneb_automaton_state;
6913 unsigned short two_automaton_state;
6914 unsigned short twob_automaton_state;
6917 fprintf
6918 (dump,
6919 "// Bundle state %d (orig %d, cost %d, nops %d/%d, insns %d, branch %d, state %d) for %d\n",
6920 curr_state->unique_num,
6921 (curr_state->originator == NULL
6922 ? -1 : curr_state->originator->unique_num),
6923 curr_state->cost,
6924 curr_state->before_nops_num, curr_state->after_nops_num,
6925 curr_state->accumulated_insns_num, curr_state->branch_deviation,
6926 (ia64_tune == PROCESSOR_ITANIUM
6927 ? ((struct DFA_chip *) curr_state->dfa_state)->oneb_automaton_state
6928 : ((struct DFA_chip *) curr_state->dfa_state)->twob_automaton_state),
6929 INSN_UID (insn));
6932 if (index_to_bundle_states [insn_num] == NULL)
6933 /* We should find a solution because the 2nd insn scheduling has
6934 found one. */
6935 abort ();
6936 /* Find a state corresponding to the best insn sequence. */
6937 best_state = NULL;
6938 for (curr_state = index_to_bundle_states [insn_num];
6939 curr_state != NULL;
6940 curr_state = curr_state->next)
6941 /* We are just looking at the states with fully filled up last
6942 bundle. The first we prefer insn sequences with minimal cost
6943 then with minimal inserted nops and finally with branch insns
6944 placed in the 3rd slots. */
6945 if (curr_state->accumulated_insns_num % 3 == 0
6946 && (best_state == NULL || best_state->cost > curr_state->cost
6947 || (best_state->cost == curr_state->cost
6948 && (curr_state->accumulated_insns_num
6949 < best_state->accumulated_insns_num
6950 || (curr_state->accumulated_insns_num
6951 == best_state->accumulated_insns_num
6952 && curr_state->branch_deviation
6953 < best_state->branch_deviation)))))
6954 best_state = curr_state;
6955 /* Second (backward) pass: adding nops and templates. */
6956 insn_num = best_state->before_nops_num;
6957 template0 = template1 = -1;
6958 for (curr_state = best_state;
6959 curr_state->originator != NULL;
6960 curr_state = curr_state->originator)
6962 insn = curr_state->insn;
6963 asm_p = (GET_CODE (PATTERN (insn)) == ASM_INPUT
6964 || asm_noperands (PATTERN (insn)) >= 0);
6965 insn_num++;
6966 if (verbose >= 2 && dump)
6968 struct DFA_chip
6970 unsigned short one_automaton_state;
6971 unsigned short oneb_automaton_state;
6972 unsigned short two_automaton_state;
6973 unsigned short twob_automaton_state;
6976 fprintf
6977 (dump,
6978 "// Best %d (orig %d, cost %d, nops %d/%d, insns %d, branch %d, state %d) for %d\n",
6979 curr_state->unique_num,
6980 (curr_state->originator == NULL
6981 ? -1 : curr_state->originator->unique_num),
6982 curr_state->cost,
6983 curr_state->before_nops_num, curr_state->after_nops_num,
6984 curr_state->accumulated_insns_num, curr_state->branch_deviation,
6985 (ia64_tune == PROCESSOR_ITANIUM
6986 ? ((struct DFA_chip *) curr_state->dfa_state)->oneb_automaton_state
6987 : ((struct DFA_chip *) curr_state->dfa_state)->twob_automaton_state),
6988 INSN_UID (insn));
6990 /* Find the position in the current bundle window. The window can
6991 contain at most two bundles. Two bundle window means that
6992 the processor will make two bundle rotation. */
6993 max_pos = get_max_pos (curr_state->dfa_state);
6994 if (max_pos == 6
6995 /* The following (negative template number) means that the
6996 processor did one bundle rotation. */
6997 || (max_pos == 3 && template0 < 0))
6999 /* We are at the end of the window -- find template(s) for
7000 its bundle(s). */
7001 pos = max_pos;
7002 if (max_pos == 3)
7003 template0 = get_template (curr_state->dfa_state, 3);
7004 else
7006 template1 = get_template (curr_state->dfa_state, 3);
7007 template0 = get_template (curr_state->dfa_state, 6);
7010 if (max_pos > 3 && template1 < 0)
7011 /* It may happen when we have the stop inside a bundle. */
7013 if (pos > 3)
7014 abort ();
7015 template1 = get_template (curr_state->dfa_state, 3);
7016 pos += 3;
7018 if (!asm_p)
7019 /* Emit nops after the current insn. */
7020 for (i = 0; i < curr_state->after_nops_num; i++)
7022 nop = gen_nop ();
7023 emit_insn_after (nop, insn);
7024 pos--;
7025 if (pos < 0)
7026 abort ();
7027 if (pos % 3 == 0)
7029 /* We are at the start of a bundle: emit the template
7030 (it should be defined). */
7031 if (template0 < 0)
7032 abort ();
7033 b = gen_bundle_selector (GEN_INT (template0));
7034 ia64_emit_insn_before (b, nop);
7035 /* If we have two bundle window, we make one bundle
7036 rotation. Otherwise template0 will be undefined
7037 (negative value). */
7038 template0 = template1;
7039 template1 = -1;
7042 /* Move the position backward in the window. Group barrier has
7043 no slot. Asm insn takes all bundle. */
7044 if (INSN_CODE (insn) != CODE_FOR_insn_group_barrier
7045 && GET_CODE (PATTERN (insn)) != ASM_INPUT
7046 && asm_noperands (PATTERN (insn)) < 0)
7047 pos--;
7048 /* Long insn takes 2 slots. */
7049 if (ia64_safe_type (insn) == TYPE_L)
7050 pos--;
7051 if (pos < 0)
7052 abort ();
7053 if (pos % 3 == 0
7054 && INSN_CODE (insn) != CODE_FOR_insn_group_barrier
7055 && GET_CODE (PATTERN (insn)) != ASM_INPUT
7056 && asm_noperands (PATTERN (insn)) < 0)
7058 /* The current insn is at the bundle start: emit the
7059 template. */
7060 if (template0 < 0)
7061 abort ();
7062 b = gen_bundle_selector (GEN_INT (template0));
7063 ia64_emit_insn_before (b, insn);
7064 b = PREV_INSN (insn);
7065 insn = b;
7066 /* See comment above in analogous place for emiting nops
7067 after the insn. */
7068 template0 = template1;
7069 template1 = -1;
7071 /* Emit nops after the current insn. */
7072 for (i = 0; i < curr_state->before_nops_num; i++)
7074 nop = gen_nop ();
7075 ia64_emit_insn_before (nop, insn);
7076 nop = PREV_INSN (insn);
7077 insn = nop;
7078 pos--;
7079 if (pos < 0)
7080 abort ();
7081 if (pos % 3 == 0)
7083 /* See comment above in analogous place for emiting nops
7084 after the insn. */
7085 if (template0 < 0)
7086 abort ();
7087 b = gen_bundle_selector (GEN_INT (template0));
7088 ia64_emit_insn_before (b, insn);
7089 b = PREV_INSN (insn);
7090 insn = b;
7091 template0 = template1;
7092 template1 = -1;
7096 if (ia64_tune == PROCESSOR_ITANIUM)
7097 /* Insert additional cycles for MM-insns (MMMUL and MMSHF).
7098 Itanium1 has a strange design, if the distance between an insn
7099 and dependent MM-insn is less 4 then we have a 6 additional
7100 cycles stall. So we make the distance equal to 4 cycles if it
7101 is less. */
7102 for (insn = get_next_important_insn (NEXT_INSN (prev_head_insn), tail);
7103 insn != NULL_RTX;
7104 insn = next_insn)
7106 if (!INSN_P (insn)
7107 || ia64_safe_itanium_class (insn) == ITANIUM_CLASS_IGNORE
7108 || GET_CODE (PATTERN (insn)) == USE
7109 || GET_CODE (PATTERN (insn)) == CLOBBER)
7110 abort ();
7111 next_insn = get_next_important_insn (NEXT_INSN (insn), tail);
7112 if (INSN_UID (insn) < clocks_length && add_cycles [INSN_UID (insn)])
7113 /* We found a MM-insn which needs additional cycles. */
7115 rtx last;
7116 int i, j, n;
7117 int pred_stop_p;
7119 /* Now we are searching for a template of the bundle in
7120 which the MM-insn is placed and the position of the
7121 insn in the bundle (0, 1, 2). Also we are searching
7122 for that there is a stop before the insn. */
7123 last = prev_active_insn (insn);
7124 pred_stop_p = recog_memoized (last) == CODE_FOR_insn_group_barrier;
7125 if (pred_stop_p)
7126 last = prev_active_insn (last);
7127 n = 0;
7128 for (;; last = prev_active_insn (last))
7129 if (recog_memoized (last) == CODE_FOR_bundle_selector)
7131 template0 = XINT (XVECEXP (PATTERN (last), 0, 0), 0);
7132 if (template0 == 9)
7133 /* The insn is in MLX bundle. Change the template
7134 onto MFI because we will add nops before the
7135 insn. It simplifies subsequent code a lot. */
7136 PATTERN (last)
7137 = gen_bundle_selector (GEN_INT (2)); /* -> MFI */
7138 break;
7140 else if (recog_memoized (last) != CODE_FOR_insn_group_barrier)
7141 n++;
7142 /* Some check of correctness: the stop is not at the
7143 bundle start, there are no more 3 insns in the bundle,
7144 and the MM-insn is not at the start of bundle with
7145 template MLX. */
7146 if ((pred_stop_p && n == 0) || n > 2
7147 || (template0 == 9 && n != 0))
7148 abort ();
7149 /* Put nops after the insn in the bundle. */
7150 for (j = 3 - n; j > 0; j --)
7151 ia64_emit_insn_before (gen_nop (), insn);
7152 /* It takes into account that we will add more N nops
7153 before the insn lately -- please see code below. */
7154 add_cycles [INSN_UID (insn)]--;
7155 if (!pred_stop_p || add_cycles [INSN_UID (insn)])
7156 ia64_emit_insn_before (gen_insn_group_barrier (GEN_INT (3)),
7157 insn);
7158 if (pred_stop_p)
7159 add_cycles [INSN_UID (insn)]--;
7160 for (i = add_cycles [INSN_UID (insn)]; i > 0; i--)
7162 /* Insert "MII;" template. */
7163 ia64_emit_insn_before (gen_bundle_selector (GEN_INT (0)),
7164 insn);
7165 ia64_emit_insn_before (gen_nop (), insn);
7166 ia64_emit_insn_before (gen_nop (), insn);
7167 if (i > 1)
7169 /* To decrease code size, we use "MI;I;"
7170 template. */
7171 ia64_emit_insn_before
7172 (gen_insn_group_barrier (GEN_INT (3)), insn);
7173 i--;
7175 ia64_emit_insn_before (gen_nop (), insn);
7176 ia64_emit_insn_before (gen_insn_group_barrier (GEN_INT (3)),
7177 insn);
7179 /* Put the MM-insn in the same slot of a bundle with the
7180 same template as the original one. */
7181 ia64_emit_insn_before (gen_bundle_selector (GEN_INT (template0)),
7182 insn);
7183 /* To put the insn in the same slot, add necessary number
7184 of nops. */
7185 for (j = n; j > 0; j --)
7186 ia64_emit_insn_before (gen_nop (), insn);
7187 /* Put the stop if the original bundle had it. */
7188 if (pred_stop_p)
7189 ia64_emit_insn_before (gen_insn_group_barrier (GEN_INT (3)),
7190 insn);
7193 free (index_to_bundle_states);
7194 finish_bundle_state_table ();
7195 bundling_p = 0;
7196 dfa_clean_insn_cache ();
7199 /* The following function is called at the end of scheduling BB or
7200 EBB. After reload, it inserts stop bits and does insn bundling. */
7202 static void
7203 ia64_sched_finish (FILE *dump, int sched_verbose)
7205 if (sched_verbose)
7206 fprintf (dump, "// Finishing schedule.\n");
7207 if (!reload_completed)
7208 return;
7209 if (reload_completed)
7211 final_emit_insn_group_barriers (dump);
7212 bundling (dump, sched_verbose, current_sched_info->prev_head,
7213 current_sched_info->next_tail);
7214 if (sched_verbose && dump)
7215 fprintf (dump, "// finishing %d-%d\n",
7216 INSN_UID (NEXT_INSN (current_sched_info->prev_head)),
7217 INSN_UID (PREV_INSN (current_sched_info->next_tail)));
7219 return;
7223 /* The following function inserts stop bits in scheduled BB or EBB. */
7225 static void
7226 final_emit_insn_group_barriers (FILE *dump ATTRIBUTE_UNUSED)
7228 rtx insn;
7229 int need_barrier_p = 0;
7230 rtx prev_insn = NULL_RTX;
7232 init_insn_group_barriers ();
7234 for (insn = NEXT_INSN (current_sched_info->prev_head);
7235 insn != current_sched_info->next_tail;
7236 insn = NEXT_INSN (insn))
7238 if (GET_CODE (insn) == BARRIER)
7240 rtx last = prev_active_insn (insn);
7242 if (! last)
7243 continue;
7244 if (GET_CODE (last) == JUMP_INSN
7245 && GET_CODE (PATTERN (last)) == ADDR_DIFF_VEC)
7246 last = prev_active_insn (last);
7247 if (recog_memoized (last) != CODE_FOR_insn_group_barrier)
7248 emit_insn_after (gen_insn_group_barrier (GEN_INT (3)), last);
7250 init_insn_group_barriers ();
7251 need_barrier_p = 0;
7252 prev_insn = NULL_RTX;
7254 else if (INSN_P (insn))
7256 if (recog_memoized (insn) == CODE_FOR_insn_group_barrier)
7258 init_insn_group_barriers ();
7259 need_barrier_p = 0;
7260 prev_insn = NULL_RTX;
7262 else if (need_barrier_p || group_barrier_needed_p (insn))
7264 if (TARGET_EARLY_STOP_BITS)
7266 rtx last;
7268 for (last = insn;
7269 last != current_sched_info->prev_head;
7270 last = PREV_INSN (last))
7271 if (INSN_P (last) && GET_MODE (last) == TImode
7272 && stops_p [INSN_UID (last)])
7273 break;
7274 if (last == current_sched_info->prev_head)
7275 last = insn;
7276 last = prev_active_insn (last);
7277 if (last
7278 && recog_memoized (last) != CODE_FOR_insn_group_barrier)
7279 emit_insn_after (gen_insn_group_barrier (GEN_INT (3)),
7280 last);
7281 init_insn_group_barriers ();
7282 for (last = NEXT_INSN (last);
7283 last != insn;
7284 last = NEXT_INSN (last))
7285 if (INSN_P (last))
7286 group_barrier_needed_p (last);
7288 else
7290 emit_insn_before (gen_insn_group_barrier (GEN_INT (3)),
7291 insn);
7292 init_insn_group_barriers ();
7294 group_barrier_needed_p (insn);
7295 prev_insn = NULL_RTX;
7297 else if (recog_memoized (insn) >= 0)
7298 prev_insn = insn;
7299 need_barrier_p = (GET_CODE (insn) == CALL_INSN
7300 || GET_CODE (PATTERN (insn)) == ASM_INPUT
7301 || asm_noperands (PATTERN (insn)) >= 0);
7308 /* If the following function returns TRUE, we will use the the DFA
7309 insn scheduler. */
7311 static int
7312 ia64_use_dfa_pipeline_interface (void)
7314 return 1;
7317 /* If the following function returns TRUE, we will use the the DFA
7318 insn scheduler. */
7320 static int
7321 ia64_first_cycle_multipass_dfa_lookahead (void)
7323 return (reload_completed ? 6 : 4);
7326 /* The following function initiates variable `dfa_pre_cycle_insn'. */
7328 static void
7329 ia64_init_dfa_pre_cycle_insn (void)
7331 if (temp_dfa_state == NULL)
7333 dfa_state_size = state_size ();
7334 temp_dfa_state = xmalloc (dfa_state_size);
7335 prev_cycle_state = xmalloc (dfa_state_size);
7337 dfa_pre_cycle_insn = make_insn_raw (gen_pre_cycle ());
7338 PREV_INSN (dfa_pre_cycle_insn) = NEXT_INSN (dfa_pre_cycle_insn) = NULL_RTX;
7339 recog_memoized (dfa_pre_cycle_insn);
7340 dfa_stop_insn = make_insn_raw (gen_insn_group_barrier (GEN_INT (3)));
7341 PREV_INSN (dfa_stop_insn) = NEXT_INSN (dfa_stop_insn) = NULL_RTX;
7342 recog_memoized (dfa_stop_insn);
7345 /* The following function returns the pseudo insn DFA_PRE_CYCLE_INSN
7346 used by the DFA insn scheduler. */
7348 static rtx
7349 ia64_dfa_pre_cycle_insn (void)
7351 return dfa_pre_cycle_insn;
7354 /* The following function returns TRUE if PRODUCER (of type ilog or
7355 ld) produces address for CONSUMER (of type st or stf). */
7358 ia64_st_address_bypass_p (rtx producer, rtx consumer)
7360 rtx dest, reg, mem;
7362 if (producer == NULL_RTX || consumer == NULL_RTX)
7363 abort ();
7364 dest = ia64_single_set (producer);
7365 if (dest == NULL_RTX || (reg = SET_DEST (dest)) == NULL_RTX
7366 || (GET_CODE (reg) != REG && GET_CODE (reg) != SUBREG))
7367 abort ();
7368 if (GET_CODE (reg) == SUBREG)
7369 reg = SUBREG_REG (reg);
7370 dest = ia64_single_set (consumer);
7371 if (dest == NULL_RTX || (mem = SET_DEST (dest)) == NULL_RTX
7372 || GET_CODE (mem) != MEM)
7373 abort ();
7374 return reg_mentioned_p (reg, mem);
7377 /* The following function returns TRUE if PRODUCER (of type ilog or
7378 ld) produces address for CONSUMER (of type ld or fld). */
7381 ia64_ld_address_bypass_p (rtx producer, rtx consumer)
7383 rtx dest, src, reg, mem;
7385 if (producer == NULL_RTX || consumer == NULL_RTX)
7386 abort ();
7387 dest = ia64_single_set (producer);
7388 if (dest == NULL_RTX || (reg = SET_DEST (dest)) == NULL_RTX
7389 || (GET_CODE (reg) != REG && GET_CODE (reg) != SUBREG))
7390 abort ();
7391 if (GET_CODE (reg) == SUBREG)
7392 reg = SUBREG_REG (reg);
7393 src = ia64_single_set (consumer);
7394 if (src == NULL_RTX || (mem = SET_SRC (src)) == NULL_RTX)
7395 abort ();
7396 if (GET_CODE (mem) == UNSPEC && XVECLEN (mem, 0) > 0)
7397 mem = XVECEXP (mem, 0, 0);
7398 while (GET_CODE (mem) == SUBREG || GET_CODE (mem) == ZERO_EXTEND)
7399 mem = XEXP (mem, 0);
7401 /* Note that LO_SUM is used for GOT loads. */
7402 if (GET_CODE (mem) != LO_SUM && GET_CODE (mem) != MEM)
7403 abort ();
7405 return reg_mentioned_p (reg, mem);
7408 /* The following function returns TRUE if INSN produces address for a
7409 load/store insn. We will place such insns into M slot because it
7410 decreases its latency time. */
7413 ia64_produce_address_p (rtx insn)
7415 return insn->call;
7419 /* Emit pseudo-ops for the assembler to describe predicate relations.
7420 At present this assumes that we only consider predicate pairs to
7421 be mutex, and that the assembler can deduce proper values from
7422 straight-line code. */
7424 static void
7425 emit_predicate_relation_info (void)
7427 basic_block bb;
7429 FOR_EACH_BB_REVERSE (bb)
7431 int r;
7432 rtx head = BB_HEAD (bb);
7434 /* We only need such notes at code labels. */
7435 if (GET_CODE (head) != CODE_LABEL)
7436 continue;
7437 if (GET_CODE (NEXT_INSN (head)) == NOTE
7438 && NOTE_LINE_NUMBER (NEXT_INSN (head)) == NOTE_INSN_BASIC_BLOCK)
7439 head = NEXT_INSN (head);
7441 for (r = PR_REG (0); r < PR_REG (64); r += 2)
7442 if (REGNO_REG_SET_P (bb->global_live_at_start, r))
7444 rtx p = gen_rtx_REG (BImode, r);
7445 rtx n = emit_insn_after (gen_pred_rel_mutex (p), head);
7446 if (head == BB_END (bb))
7447 BB_END (bb) = n;
7448 head = n;
7452 /* Look for conditional calls that do not return, and protect predicate
7453 relations around them. Otherwise the assembler will assume the call
7454 returns, and complain about uses of call-clobbered predicates after
7455 the call. */
7456 FOR_EACH_BB_REVERSE (bb)
7458 rtx insn = BB_HEAD (bb);
7460 while (1)
7462 if (GET_CODE (insn) == CALL_INSN
7463 && GET_CODE (PATTERN (insn)) == COND_EXEC
7464 && find_reg_note (insn, REG_NORETURN, NULL_RTX))
7466 rtx b = emit_insn_before (gen_safe_across_calls_all (), insn);
7467 rtx a = emit_insn_after (gen_safe_across_calls_normal (), insn);
7468 if (BB_HEAD (bb) == insn)
7469 BB_HEAD (bb) = b;
7470 if (BB_END (bb) == insn)
7471 BB_END (bb) = a;
7474 if (insn == BB_END (bb))
7475 break;
7476 insn = NEXT_INSN (insn);
7481 /* Perform machine dependent operations on the rtl chain INSNS. */
7483 static void
7484 ia64_reorg (void)
7486 /* We are freeing block_for_insn in the toplev to keep compatibility
7487 with old MDEP_REORGS that are not CFG based. Recompute it now. */
7488 compute_bb_for_insn ();
7490 /* If optimizing, we'll have split before scheduling. */
7491 if (optimize == 0)
7492 split_all_insns (0);
7494 /* ??? update_life_info_in_dirty_blocks fails to terminate during
7495 non-optimizing bootstrap. */
7496 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES, PROP_DEATH_NOTES);
7498 if (ia64_flag_schedule_insns2)
7500 timevar_push (TV_SCHED2);
7501 ia64_final_schedule = 1;
7503 initiate_bundle_states ();
7504 ia64_nop = make_insn_raw (gen_nop ());
7505 PREV_INSN (ia64_nop) = NEXT_INSN (ia64_nop) = NULL_RTX;
7506 recog_memoized (ia64_nop);
7507 clocks_length = get_max_uid () + 1;
7508 stops_p = xcalloc (1, clocks_length);
7509 if (ia64_tune == PROCESSOR_ITANIUM)
7511 clocks = xcalloc (clocks_length, sizeof (int));
7512 add_cycles = xcalloc (clocks_length, sizeof (int));
7514 if (ia64_tune == PROCESSOR_ITANIUM2)
7516 pos_1 = get_cpu_unit_code ("2_1");
7517 pos_2 = get_cpu_unit_code ("2_2");
7518 pos_3 = get_cpu_unit_code ("2_3");
7519 pos_4 = get_cpu_unit_code ("2_4");
7520 pos_5 = get_cpu_unit_code ("2_5");
7521 pos_6 = get_cpu_unit_code ("2_6");
7522 _0mii_ = get_cpu_unit_code ("2b_0mii.");
7523 _0mmi_ = get_cpu_unit_code ("2b_0mmi.");
7524 _0mfi_ = get_cpu_unit_code ("2b_0mfi.");
7525 _0mmf_ = get_cpu_unit_code ("2b_0mmf.");
7526 _0bbb_ = get_cpu_unit_code ("2b_0bbb.");
7527 _0mbb_ = get_cpu_unit_code ("2b_0mbb.");
7528 _0mib_ = get_cpu_unit_code ("2b_0mib.");
7529 _0mmb_ = get_cpu_unit_code ("2b_0mmb.");
7530 _0mfb_ = get_cpu_unit_code ("2b_0mfb.");
7531 _0mlx_ = get_cpu_unit_code ("2b_0mlx.");
7532 _1mii_ = get_cpu_unit_code ("2b_1mii.");
7533 _1mmi_ = get_cpu_unit_code ("2b_1mmi.");
7534 _1mfi_ = get_cpu_unit_code ("2b_1mfi.");
7535 _1mmf_ = get_cpu_unit_code ("2b_1mmf.");
7536 _1bbb_ = get_cpu_unit_code ("2b_1bbb.");
7537 _1mbb_ = get_cpu_unit_code ("2b_1mbb.");
7538 _1mib_ = get_cpu_unit_code ("2b_1mib.");
7539 _1mmb_ = get_cpu_unit_code ("2b_1mmb.");
7540 _1mfb_ = get_cpu_unit_code ("2b_1mfb.");
7541 _1mlx_ = get_cpu_unit_code ("2b_1mlx.");
7543 else
7545 pos_1 = get_cpu_unit_code ("1_1");
7546 pos_2 = get_cpu_unit_code ("1_2");
7547 pos_3 = get_cpu_unit_code ("1_3");
7548 pos_4 = get_cpu_unit_code ("1_4");
7549 pos_5 = get_cpu_unit_code ("1_5");
7550 pos_6 = get_cpu_unit_code ("1_6");
7551 _0mii_ = get_cpu_unit_code ("1b_0mii.");
7552 _0mmi_ = get_cpu_unit_code ("1b_0mmi.");
7553 _0mfi_ = get_cpu_unit_code ("1b_0mfi.");
7554 _0mmf_ = get_cpu_unit_code ("1b_0mmf.");
7555 _0bbb_ = get_cpu_unit_code ("1b_0bbb.");
7556 _0mbb_ = get_cpu_unit_code ("1b_0mbb.");
7557 _0mib_ = get_cpu_unit_code ("1b_0mib.");
7558 _0mmb_ = get_cpu_unit_code ("1b_0mmb.");
7559 _0mfb_ = get_cpu_unit_code ("1b_0mfb.");
7560 _0mlx_ = get_cpu_unit_code ("1b_0mlx.");
7561 _1mii_ = get_cpu_unit_code ("1b_1mii.");
7562 _1mmi_ = get_cpu_unit_code ("1b_1mmi.");
7563 _1mfi_ = get_cpu_unit_code ("1b_1mfi.");
7564 _1mmf_ = get_cpu_unit_code ("1b_1mmf.");
7565 _1bbb_ = get_cpu_unit_code ("1b_1bbb.");
7566 _1mbb_ = get_cpu_unit_code ("1b_1mbb.");
7567 _1mib_ = get_cpu_unit_code ("1b_1mib.");
7568 _1mmb_ = get_cpu_unit_code ("1b_1mmb.");
7569 _1mfb_ = get_cpu_unit_code ("1b_1mfb.");
7570 _1mlx_ = get_cpu_unit_code ("1b_1mlx.");
7572 schedule_ebbs (rtl_dump_file);
7573 finish_bundle_states ();
7574 if (ia64_tune == PROCESSOR_ITANIUM)
7576 free (add_cycles);
7577 free (clocks);
7579 free (stops_p);
7580 emit_insn_group_barriers (rtl_dump_file);
7582 ia64_final_schedule = 0;
7583 timevar_pop (TV_SCHED2);
7585 else
7586 emit_all_insn_group_barriers (rtl_dump_file);
7588 /* A call must not be the last instruction in a function, so that the
7589 return address is still within the function, so that unwinding works
7590 properly. Note that IA-64 differs from dwarf2 on this point. */
7591 if (flag_unwind_tables || (flag_exceptions && !USING_SJLJ_EXCEPTIONS))
7593 rtx insn;
7594 int saw_stop = 0;
7596 insn = get_last_insn ();
7597 if (! INSN_P (insn))
7598 insn = prev_active_insn (insn);
7599 if (GET_CODE (insn) == INSN
7600 && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
7601 && XINT (PATTERN (insn), 1) == UNSPECV_INSN_GROUP_BARRIER)
7603 saw_stop = 1;
7604 insn = prev_active_insn (insn);
7606 if (GET_CODE (insn) == CALL_INSN)
7608 if (! saw_stop)
7609 emit_insn (gen_insn_group_barrier (GEN_INT (3)));
7610 emit_insn (gen_break_f ());
7611 emit_insn (gen_insn_group_barrier (GEN_INT (3)));
7615 fixup_errata ();
7616 emit_predicate_relation_info ();
7619 /* Return true if REGNO is used by the epilogue. */
7622 ia64_epilogue_uses (int regno)
7624 switch (regno)
7626 case R_GR (1):
7627 /* With a call to a function in another module, we will write a new
7628 value to "gp". After returning from such a call, we need to make
7629 sure the function restores the original gp-value, even if the
7630 function itself does not use the gp anymore. */
7631 return !(TARGET_AUTO_PIC || TARGET_NO_PIC);
7633 case IN_REG (0): case IN_REG (1): case IN_REG (2): case IN_REG (3):
7634 case IN_REG (4): case IN_REG (5): case IN_REG (6): case IN_REG (7):
7635 /* For functions defined with the syscall_linkage attribute, all
7636 input registers are marked as live at all function exits. This
7637 prevents the register allocator from using the input registers,
7638 which in turn makes it possible to restart a system call after
7639 an interrupt without having to save/restore the input registers.
7640 This also prevents kernel data from leaking to application code. */
7641 return lookup_attribute ("syscall_linkage",
7642 TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))) != NULL;
7644 case R_BR (0):
7645 /* Conditional return patterns can't represent the use of `b0' as
7646 the return address, so we force the value live this way. */
7647 return 1;
7649 case AR_PFS_REGNUM:
7650 /* Likewise for ar.pfs, which is used by br.ret. */
7651 return 1;
7653 default:
7654 return 0;
7658 /* Return true if REGNO is used by the frame unwinder. */
7661 ia64_eh_uses (int regno)
7663 if (! reload_completed)
7664 return 0;
7666 if (current_frame_info.reg_save_b0
7667 && regno == current_frame_info.reg_save_b0)
7668 return 1;
7669 if (current_frame_info.reg_save_pr
7670 && regno == current_frame_info.reg_save_pr)
7671 return 1;
7672 if (current_frame_info.reg_save_ar_pfs
7673 && regno == current_frame_info.reg_save_ar_pfs)
7674 return 1;
7675 if (current_frame_info.reg_save_ar_unat
7676 && regno == current_frame_info.reg_save_ar_unat)
7677 return 1;
7678 if (current_frame_info.reg_save_ar_lc
7679 && regno == current_frame_info.reg_save_ar_lc)
7680 return 1;
7682 return 0;
7685 /* Return true if this goes in small data/bss. */
7687 /* ??? We could also support own long data here. Generating movl/add/ld8
7688 instead of addl,ld8/ld8. This makes the code bigger, but should make the
7689 code faster because there is one less load. This also includes incomplete
7690 types which can't go in sdata/sbss. */
7692 static bool
7693 ia64_in_small_data_p (tree exp)
7695 if (TARGET_NO_SDATA)
7696 return false;
7698 /* We want to merge strings, so we never consider them small data. */
7699 if (TREE_CODE (exp) == STRING_CST)
7700 return false;
7702 /* Functions are never small data. */
7703 if (TREE_CODE (exp) == FUNCTION_DECL)
7704 return false;
7706 if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
7708 const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
7709 if (strcmp (section, ".sdata") == 0
7710 || strcmp (section, ".sbss") == 0)
7711 return true;
7713 else
7715 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
7717 /* If this is an incomplete type with size 0, then we can't put it
7718 in sdata because it might be too big when completed. */
7719 if (size > 0 && size <= ia64_section_threshold)
7720 return true;
7723 return false;
7726 /* Output assembly directives for prologue regions. */
7728 /* The current basic block number. */
7730 static bool last_block;
7732 /* True if we need a copy_state command at the start of the next block. */
7734 static bool need_copy_state;
7736 /* The function emits unwind directives for the start of an epilogue. */
7738 static void
7739 process_epilogue (void)
7741 /* If this isn't the last block of the function, then we need to label the
7742 current state, and copy it back in at the start of the next block. */
7744 if (!last_block)
7746 fprintf (asm_out_file, "\t.label_state 1\n");
7747 need_copy_state = true;
7750 fprintf (asm_out_file, "\t.restore sp\n");
7753 /* This function processes a SET pattern looking for specific patterns
7754 which result in emitting an assembly directive required for unwinding. */
7756 static int
7757 process_set (FILE *asm_out_file, rtx pat)
7759 rtx src = SET_SRC (pat);
7760 rtx dest = SET_DEST (pat);
7761 int src_regno, dest_regno;
7763 /* Look for the ALLOC insn. */
7764 if (GET_CODE (src) == UNSPEC_VOLATILE
7765 && XINT (src, 1) == UNSPECV_ALLOC
7766 && GET_CODE (dest) == REG)
7768 dest_regno = REGNO (dest);
7770 /* If this isn't the final destination for ar.pfs, the alloc
7771 shouldn't have been marked frame related. */
7772 if (dest_regno != current_frame_info.reg_save_ar_pfs)
7773 abort ();
7775 fprintf (asm_out_file, "\t.save ar.pfs, r%d\n",
7776 ia64_dbx_register_number (dest_regno));
7777 return 1;
7780 /* Look for SP = .... */
7781 if (GET_CODE (dest) == REG && REGNO (dest) == STACK_POINTER_REGNUM)
7783 if (GET_CODE (src) == PLUS)
7785 rtx op0 = XEXP (src, 0);
7786 rtx op1 = XEXP (src, 1);
7787 if (op0 == dest && GET_CODE (op1) == CONST_INT)
7789 if (INTVAL (op1) < 0)
7790 fprintf (asm_out_file, "\t.fframe "HOST_WIDE_INT_PRINT_DEC"\n",
7791 -INTVAL (op1));
7792 else
7793 process_epilogue ();
7795 else
7796 abort ();
7798 else if (GET_CODE (src) == REG
7799 && REGNO (src) == HARD_FRAME_POINTER_REGNUM)
7800 process_epilogue ();
7801 else
7802 abort ();
7804 return 1;
7807 /* Register move we need to look at. */
7808 if (GET_CODE (dest) == REG && GET_CODE (src) == REG)
7810 src_regno = REGNO (src);
7811 dest_regno = REGNO (dest);
7813 switch (src_regno)
7815 case BR_REG (0):
7816 /* Saving return address pointer. */
7817 if (dest_regno != current_frame_info.reg_save_b0)
7818 abort ();
7819 fprintf (asm_out_file, "\t.save rp, r%d\n",
7820 ia64_dbx_register_number (dest_regno));
7821 return 1;
7823 case PR_REG (0):
7824 if (dest_regno != current_frame_info.reg_save_pr)
7825 abort ();
7826 fprintf (asm_out_file, "\t.save pr, r%d\n",
7827 ia64_dbx_register_number (dest_regno));
7828 return 1;
7830 case AR_UNAT_REGNUM:
7831 if (dest_regno != current_frame_info.reg_save_ar_unat)
7832 abort ();
7833 fprintf (asm_out_file, "\t.save ar.unat, r%d\n",
7834 ia64_dbx_register_number (dest_regno));
7835 return 1;
7837 case AR_LC_REGNUM:
7838 if (dest_regno != current_frame_info.reg_save_ar_lc)
7839 abort ();
7840 fprintf (asm_out_file, "\t.save ar.lc, r%d\n",
7841 ia64_dbx_register_number (dest_regno));
7842 return 1;
7844 case STACK_POINTER_REGNUM:
7845 if (dest_regno != HARD_FRAME_POINTER_REGNUM
7846 || ! frame_pointer_needed)
7847 abort ();
7848 fprintf (asm_out_file, "\t.vframe r%d\n",
7849 ia64_dbx_register_number (dest_regno));
7850 return 1;
7852 default:
7853 /* Everything else should indicate being stored to memory. */
7854 abort ();
7858 /* Memory store we need to look at. */
7859 if (GET_CODE (dest) == MEM && GET_CODE (src) == REG)
7861 long off;
7862 rtx base;
7863 const char *saveop;
7865 if (GET_CODE (XEXP (dest, 0)) == REG)
7867 base = XEXP (dest, 0);
7868 off = 0;
7870 else if (GET_CODE (XEXP (dest, 0)) == PLUS
7871 && GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT)
7873 base = XEXP (XEXP (dest, 0), 0);
7874 off = INTVAL (XEXP (XEXP (dest, 0), 1));
7876 else
7877 abort ();
7879 if (base == hard_frame_pointer_rtx)
7881 saveop = ".savepsp";
7882 off = - off;
7884 else if (base == stack_pointer_rtx)
7885 saveop = ".savesp";
7886 else
7887 abort ();
7889 src_regno = REGNO (src);
7890 switch (src_regno)
7892 case BR_REG (0):
7893 if (current_frame_info.reg_save_b0 != 0)
7894 abort ();
7895 fprintf (asm_out_file, "\t%s rp, %ld\n", saveop, off);
7896 return 1;
7898 case PR_REG (0):
7899 if (current_frame_info.reg_save_pr != 0)
7900 abort ();
7901 fprintf (asm_out_file, "\t%s pr, %ld\n", saveop, off);
7902 return 1;
7904 case AR_LC_REGNUM:
7905 if (current_frame_info.reg_save_ar_lc != 0)
7906 abort ();
7907 fprintf (asm_out_file, "\t%s ar.lc, %ld\n", saveop, off);
7908 return 1;
7910 case AR_PFS_REGNUM:
7911 if (current_frame_info.reg_save_ar_pfs != 0)
7912 abort ();
7913 fprintf (asm_out_file, "\t%s ar.pfs, %ld\n", saveop, off);
7914 return 1;
7916 case AR_UNAT_REGNUM:
7917 if (current_frame_info.reg_save_ar_unat != 0)
7918 abort ();
7919 fprintf (asm_out_file, "\t%s ar.unat, %ld\n", saveop, off);
7920 return 1;
7922 case GR_REG (4):
7923 case GR_REG (5):
7924 case GR_REG (6):
7925 case GR_REG (7):
7926 fprintf (asm_out_file, "\t.save.g 0x%x\n",
7927 1 << (src_regno - GR_REG (4)));
7928 return 1;
7930 case BR_REG (1):
7931 case BR_REG (2):
7932 case BR_REG (3):
7933 case BR_REG (4):
7934 case BR_REG (5):
7935 fprintf (asm_out_file, "\t.save.b 0x%x\n",
7936 1 << (src_regno - BR_REG (1)));
7937 return 1;
7939 case FR_REG (2):
7940 case FR_REG (3):
7941 case FR_REG (4):
7942 case FR_REG (5):
7943 fprintf (asm_out_file, "\t.save.f 0x%x\n",
7944 1 << (src_regno - FR_REG (2)));
7945 return 1;
7947 case FR_REG (16): case FR_REG (17): case FR_REG (18): case FR_REG (19):
7948 case FR_REG (20): case FR_REG (21): case FR_REG (22): case FR_REG (23):
7949 case FR_REG (24): case FR_REG (25): case FR_REG (26): case FR_REG (27):
7950 case FR_REG (28): case FR_REG (29): case FR_REG (30): case FR_REG (31):
7951 fprintf (asm_out_file, "\t.save.gf 0x0, 0x%x\n",
7952 1 << (src_regno - FR_REG (12)));
7953 return 1;
7955 default:
7956 return 0;
7960 return 0;
7964 /* This function looks at a single insn and emits any directives
7965 required to unwind this insn. */
7966 void
7967 process_for_unwind_directive (FILE *asm_out_file, rtx insn)
7969 if (flag_unwind_tables
7970 || (flag_exceptions && !USING_SJLJ_EXCEPTIONS))
7972 rtx pat;
7974 if (GET_CODE (insn) == NOTE
7975 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_BASIC_BLOCK)
7977 last_block = NOTE_BASIC_BLOCK (insn)->next_bb == EXIT_BLOCK_PTR;
7979 /* Restore unwind state from immediately before the epilogue. */
7980 if (need_copy_state)
7982 fprintf (asm_out_file, "\t.body\n");
7983 fprintf (asm_out_file, "\t.copy_state 1\n");
7984 need_copy_state = false;
7988 if (GET_CODE (insn) == NOTE || ! RTX_FRAME_RELATED_P (insn))
7989 return;
7991 pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
7992 if (pat)
7993 pat = XEXP (pat, 0);
7994 else
7995 pat = PATTERN (insn);
7997 switch (GET_CODE (pat))
7999 case SET:
8000 process_set (asm_out_file, pat);
8001 break;
8003 case PARALLEL:
8005 int par_index;
8006 int limit = XVECLEN (pat, 0);
8007 for (par_index = 0; par_index < limit; par_index++)
8009 rtx x = XVECEXP (pat, 0, par_index);
8010 if (GET_CODE (x) == SET)
8011 process_set (asm_out_file, x);
8013 break;
8016 default:
8017 abort ();
8023 void
8024 ia64_init_builtins (void)
8026 tree psi_type_node = build_pointer_type (integer_type_node);
8027 tree pdi_type_node = build_pointer_type (long_integer_type_node);
8029 /* __sync_val_compare_and_swap_si, __sync_bool_compare_and_swap_si */
8030 tree si_ftype_psi_si_si
8031 = build_function_type_list (integer_type_node,
8032 psi_type_node, integer_type_node,
8033 integer_type_node, NULL_TREE);
8035 /* __sync_val_compare_and_swap_di */
8036 tree di_ftype_pdi_di_di
8037 = build_function_type_list (long_integer_type_node,
8038 pdi_type_node, long_integer_type_node,
8039 long_integer_type_node, NULL_TREE);
8040 /* __sync_bool_compare_and_swap_di */
8041 tree si_ftype_pdi_di_di
8042 = build_function_type_list (integer_type_node,
8043 pdi_type_node, long_integer_type_node,
8044 long_integer_type_node, NULL_TREE);
8045 /* __sync_synchronize */
8046 tree void_ftype_void
8047 = build_function_type (void_type_node, void_list_node);
8049 /* __sync_lock_test_and_set_si */
8050 tree si_ftype_psi_si
8051 = build_function_type_list (integer_type_node,
8052 psi_type_node, integer_type_node, NULL_TREE);
8054 /* __sync_lock_test_and_set_di */
8055 tree di_ftype_pdi_di
8056 = build_function_type_list (long_integer_type_node,
8057 pdi_type_node, long_integer_type_node,
8058 NULL_TREE);
8060 /* __sync_lock_release_si */
8061 tree void_ftype_psi
8062 = build_function_type_list (void_type_node, psi_type_node, NULL_TREE);
8064 /* __sync_lock_release_di */
8065 tree void_ftype_pdi
8066 = build_function_type_list (void_type_node, pdi_type_node, NULL_TREE);
8068 tree fpreg_type;
8069 tree float80_type;
8071 /* The __fpreg type. */
8072 fpreg_type = make_node (REAL_TYPE);
8073 /* ??? The back end should know to load/save __fpreg variables using
8074 the ldf.fill and stf.spill instructions. */
8075 TYPE_PRECISION (fpreg_type) = 96;
8076 layout_type (fpreg_type);
8077 (*lang_hooks.types.register_builtin_type) (fpreg_type, "__fpreg");
8079 /* The __float80 type. */
8080 float80_type = make_node (REAL_TYPE);
8081 TYPE_PRECISION (float80_type) = 96;
8082 layout_type (float80_type);
8083 (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
8085 /* The __float128 type. */
8086 if (!TARGET_HPUX)
8088 tree float128_type = make_node (REAL_TYPE);
8089 TYPE_PRECISION (float128_type) = 128;
8090 layout_type (float128_type);
8091 (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
8093 else
8094 /* Under HPUX, this is a synonym for "long double". */
8095 (*lang_hooks.types.register_builtin_type) (long_double_type_node,
8096 "__float128");
8098 #define def_builtin(name, type, code) \
8099 builtin_function ((name), (type), (code), BUILT_IN_MD, NULL, NULL_TREE)
8101 def_builtin ("__sync_val_compare_and_swap_si", si_ftype_psi_si_si,
8102 IA64_BUILTIN_VAL_COMPARE_AND_SWAP_SI);
8103 def_builtin ("__sync_val_compare_and_swap_di", di_ftype_pdi_di_di,
8104 IA64_BUILTIN_VAL_COMPARE_AND_SWAP_DI);
8105 def_builtin ("__sync_bool_compare_and_swap_si", si_ftype_psi_si_si,
8106 IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_SI);
8107 def_builtin ("__sync_bool_compare_and_swap_di", si_ftype_pdi_di_di,
8108 IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_DI);
8110 def_builtin ("__sync_synchronize", void_ftype_void,
8111 IA64_BUILTIN_SYNCHRONIZE);
8113 def_builtin ("__sync_lock_test_and_set_si", si_ftype_psi_si,
8114 IA64_BUILTIN_LOCK_TEST_AND_SET_SI);
8115 def_builtin ("__sync_lock_test_and_set_di", di_ftype_pdi_di,
8116 IA64_BUILTIN_LOCK_TEST_AND_SET_DI);
8117 def_builtin ("__sync_lock_release_si", void_ftype_psi,
8118 IA64_BUILTIN_LOCK_RELEASE_SI);
8119 def_builtin ("__sync_lock_release_di", void_ftype_pdi,
8120 IA64_BUILTIN_LOCK_RELEASE_DI);
8122 def_builtin ("__builtin_ia64_bsp",
8123 build_function_type (ptr_type_node, void_list_node),
8124 IA64_BUILTIN_BSP);
8126 def_builtin ("__builtin_ia64_flushrs",
8127 build_function_type (void_type_node, void_list_node),
8128 IA64_BUILTIN_FLUSHRS);
8130 def_builtin ("__sync_fetch_and_add_si", si_ftype_psi_si,
8131 IA64_BUILTIN_FETCH_AND_ADD_SI);
8132 def_builtin ("__sync_fetch_and_sub_si", si_ftype_psi_si,
8133 IA64_BUILTIN_FETCH_AND_SUB_SI);
8134 def_builtin ("__sync_fetch_and_or_si", si_ftype_psi_si,
8135 IA64_BUILTIN_FETCH_AND_OR_SI);
8136 def_builtin ("__sync_fetch_and_and_si", si_ftype_psi_si,
8137 IA64_BUILTIN_FETCH_AND_AND_SI);
8138 def_builtin ("__sync_fetch_and_xor_si", si_ftype_psi_si,
8139 IA64_BUILTIN_FETCH_AND_XOR_SI);
8140 def_builtin ("__sync_fetch_and_nand_si", si_ftype_psi_si,
8141 IA64_BUILTIN_FETCH_AND_NAND_SI);
8143 def_builtin ("__sync_add_and_fetch_si", si_ftype_psi_si,
8144 IA64_BUILTIN_ADD_AND_FETCH_SI);
8145 def_builtin ("__sync_sub_and_fetch_si", si_ftype_psi_si,
8146 IA64_BUILTIN_SUB_AND_FETCH_SI);
8147 def_builtin ("__sync_or_and_fetch_si", si_ftype_psi_si,
8148 IA64_BUILTIN_OR_AND_FETCH_SI);
8149 def_builtin ("__sync_and_and_fetch_si", si_ftype_psi_si,
8150 IA64_BUILTIN_AND_AND_FETCH_SI);
8151 def_builtin ("__sync_xor_and_fetch_si", si_ftype_psi_si,
8152 IA64_BUILTIN_XOR_AND_FETCH_SI);
8153 def_builtin ("__sync_nand_and_fetch_si", si_ftype_psi_si,
8154 IA64_BUILTIN_NAND_AND_FETCH_SI);
8156 def_builtin ("__sync_fetch_and_add_di", di_ftype_pdi_di,
8157 IA64_BUILTIN_FETCH_AND_ADD_DI);
8158 def_builtin ("__sync_fetch_and_sub_di", di_ftype_pdi_di,
8159 IA64_BUILTIN_FETCH_AND_SUB_DI);
8160 def_builtin ("__sync_fetch_and_or_di", di_ftype_pdi_di,
8161 IA64_BUILTIN_FETCH_AND_OR_DI);
8162 def_builtin ("__sync_fetch_and_and_di", di_ftype_pdi_di,
8163 IA64_BUILTIN_FETCH_AND_AND_DI);
8164 def_builtin ("__sync_fetch_and_xor_di", di_ftype_pdi_di,
8165 IA64_BUILTIN_FETCH_AND_XOR_DI);
8166 def_builtin ("__sync_fetch_and_nand_di", di_ftype_pdi_di,
8167 IA64_BUILTIN_FETCH_AND_NAND_DI);
8169 def_builtin ("__sync_add_and_fetch_di", di_ftype_pdi_di,
8170 IA64_BUILTIN_ADD_AND_FETCH_DI);
8171 def_builtin ("__sync_sub_and_fetch_di", di_ftype_pdi_di,
8172 IA64_BUILTIN_SUB_AND_FETCH_DI);
8173 def_builtin ("__sync_or_and_fetch_di", di_ftype_pdi_di,
8174 IA64_BUILTIN_OR_AND_FETCH_DI);
8175 def_builtin ("__sync_and_and_fetch_di", di_ftype_pdi_di,
8176 IA64_BUILTIN_AND_AND_FETCH_DI);
8177 def_builtin ("__sync_xor_and_fetch_di", di_ftype_pdi_di,
8178 IA64_BUILTIN_XOR_AND_FETCH_DI);
8179 def_builtin ("__sync_nand_and_fetch_di", di_ftype_pdi_di,
8180 IA64_BUILTIN_NAND_AND_FETCH_DI);
8182 #undef def_builtin
8185 /* Expand fetch_and_op intrinsics. The basic code sequence is:
8188 tmp = [ptr];
8189 do {
8190 ret = tmp;
8191 ar.ccv = tmp;
8192 tmp <op>= value;
8193 cmpxchgsz.acq tmp = [ptr], tmp
8194 } while (tmp != ret)
8197 static rtx
8198 ia64_expand_fetch_and_op (optab binoptab, enum machine_mode mode,
8199 tree arglist, rtx target)
8201 rtx ret, label, tmp, ccv, insn, mem, value;
8202 tree arg0, arg1;
8204 arg0 = TREE_VALUE (arglist);
8205 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8206 mem = expand_expr (arg0, NULL_RTX, Pmode, 0);
8207 #ifdef POINTERS_EXTEND_UNSIGNED
8208 if (GET_MODE(mem) != Pmode)
8209 mem = convert_memory_address (Pmode, mem);
8210 #endif
8211 value = expand_expr (arg1, NULL_RTX, mode, 0);
8213 mem = gen_rtx_MEM (mode, force_reg (Pmode, mem));
8214 MEM_VOLATILE_P (mem) = 1;
8216 if (target && register_operand (target, mode))
8217 ret = target;
8218 else
8219 ret = gen_reg_rtx (mode);
8221 emit_insn (gen_mf ());
8223 /* Special case for fetchadd instructions. */
8224 if (binoptab == add_optab && fetchadd_operand (value, VOIDmode))
8226 if (mode == SImode)
8227 insn = gen_fetchadd_acq_si (ret, mem, value);
8228 else
8229 insn = gen_fetchadd_acq_di (ret, mem, value);
8230 emit_insn (insn);
8231 return ret;
8234 tmp = gen_reg_rtx (mode);
8235 /* ar.ccv must always be loaded with a zero-extended DImode value. */
8236 ccv = gen_rtx_REG (DImode, AR_CCV_REGNUM);
8237 emit_move_insn (tmp, mem);
8239 label = gen_label_rtx ();
8240 emit_label (label);
8241 emit_move_insn (ret, tmp);
8242 convert_move (ccv, tmp, /*unsignedp=*/1);
8244 /* Perform the specific operation. Special case NAND by noticing
8245 one_cmpl_optab instead. */
8246 if (binoptab == one_cmpl_optab)
8248 tmp = expand_unop (mode, binoptab, tmp, NULL, OPTAB_WIDEN);
8249 binoptab = and_optab;
8251 tmp = expand_binop (mode, binoptab, tmp, value, tmp, 1, OPTAB_WIDEN);
8253 if (mode == SImode)
8254 insn = gen_cmpxchg_acq_si (tmp, mem, tmp, ccv);
8255 else
8256 insn = gen_cmpxchg_acq_di (tmp, mem, tmp, ccv);
8257 emit_insn (insn);
8259 emit_cmp_and_jump_insns (tmp, ret, NE, 0, mode, 1, label);
8261 return ret;
8264 /* Expand op_and_fetch intrinsics. The basic code sequence is:
8267 tmp = [ptr];
8268 do {
8269 old = tmp;
8270 ar.ccv = tmp;
8271 ret = tmp <op> value;
8272 cmpxchgsz.acq tmp = [ptr], ret
8273 } while (tmp != old)
8276 static rtx
8277 ia64_expand_op_and_fetch (optab binoptab, enum machine_mode mode,
8278 tree arglist, rtx target)
8280 rtx old, label, tmp, ret, ccv, insn, mem, value;
8281 tree arg0, arg1;
8283 arg0 = TREE_VALUE (arglist);
8284 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8285 mem = expand_expr (arg0, NULL_RTX, Pmode, 0);
8286 #ifdef POINTERS_EXTEND_UNSIGNED
8287 if (GET_MODE(mem) != Pmode)
8288 mem = convert_memory_address (Pmode, mem);
8289 #endif
8291 value = expand_expr (arg1, NULL_RTX, mode, 0);
8293 mem = gen_rtx_MEM (mode, force_reg (Pmode, mem));
8294 MEM_VOLATILE_P (mem) = 1;
8296 if (target && ! register_operand (target, mode))
8297 target = NULL_RTX;
8299 emit_insn (gen_mf ());
8300 tmp = gen_reg_rtx (mode);
8301 old = gen_reg_rtx (mode);
8302 /* ar.ccv must always be loaded with a zero-extended DImode value. */
8303 ccv = gen_rtx_REG (DImode, AR_CCV_REGNUM);
8305 emit_move_insn (tmp, mem);
8307 label = gen_label_rtx ();
8308 emit_label (label);
8309 emit_move_insn (old, tmp);
8310 convert_move (ccv, tmp, /*unsignedp=*/1);
8312 /* Perform the specific operation. Special case NAND by noticing
8313 one_cmpl_optab instead. */
8314 if (binoptab == one_cmpl_optab)
8316 tmp = expand_unop (mode, binoptab, tmp, NULL, OPTAB_WIDEN);
8317 binoptab = and_optab;
8319 ret = expand_binop (mode, binoptab, tmp, value, target, 1, OPTAB_WIDEN);
8321 if (mode == SImode)
8322 insn = gen_cmpxchg_acq_si (tmp, mem, ret, ccv);
8323 else
8324 insn = gen_cmpxchg_acq_di (tmp, mem, ret, ccv);
8325 emit_insn (insn);
8327 emit_cmp_and_jump_insns (tmp, old, NE, 0, mode, 1, label);
8329 return ret;
8332 /* Expand val_ and bool_compare_and_swap. For val_ we want:
8334 ar.ccv = oldval
8336 cmpxchgsz.acq ret = [ptr], newval, ar.ccv
8337 return ret
8339 For bool_ it's the same except return ret == oldval.
8342 static rtx
8343 ia64_expand_compare_and_swap (enum machine_mode rmode, enum machine_mode mode,
8344 int boolp, tree arglist, rtx target)
8346 tree arg0, arg1, arg2;
8347 rtx mem, old, new, ccv, tmp, insn;
8349 arg0 = TREE_VALUE (arglist);
8350 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8351 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8352 mem = expand_expr (arg0, NULL_RTX, ptr_mode, 0);
8353 old = expand_expr (arg1, NULL_RTX, mode, 0);
8354 new = expand_expr (arg2, NULL_RTX, mode, 0);
8356 mem = gen_rtx_MEM (mode, force_reg (ptr_mode, mem));
8357 MEM_VOLATILE_P (mem) = 1;
8359 if (GET_MODE (old) != mode)
8360 old = convert_to_mode (mode, old, /*unsignedp=*/1);
8361 if (GET_MODE (new) != mode)
8362 new = convert_to_mode (mode, new, /*unsignedp=*/1);
8364 if (! register_operand (old, mode))
8365 old = copy_to_mode_reg (mode, old);
8366 if (! register_operand (new, mode))
8367 new = copy_to_mode_reg (mode, new);
8369 if (! boolp && target && register_operand (target, mode))
8370 tmp = target;
8371 else
8372 tmp = gen_reg_rtx (mode);
8374 ccv = gen_rtx_REG (DImode, AR_CCV_REGNUM);
8375 convert_move (ccv, old, /*unsignedp=*/1);
8376 emit_insn (gen_mf ());
8377 if (mode == SImode)
8378 insn = gen_cmpxchg_acq_si (tmp, mem, new, ccv);
8379 else
8380 insn = gen_cmpxchg_acq_di (tmp, mem, new, ccv);
8381 emit_insn (insn);
8383 if (boolp)
8385 if (! target)
8386 target = gen_reg_rtx (rmode);
8387 return emit_store_flag_force (target, EQ, tmp, old, mode, 1, 1);
8389 else
8390 return tmp;
8393 /* Expand lock_test_and_set. I.e. `xchgsz ret = [ptr], new'. */
8395 static rtx
8396 ia64_expand_lock_test_and_set (enum machine_mode mode, tree arglist,
8397 rtx target)
8399 tree arg0, arg1;
8400 rtx mem, new, ret, insn;
8402 arg0 = TREE_VALUE (arglist);
8403 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8404 mem = expand_expr (arg0, NULL_RTX, ptr_mode, 0);
8405 new = expand_expr (arg1, NULL_RTX, mode, 0);
8407 mem = gen_rtx_MEM (mode, force_reg (ptr_mode, mem));
8408 MEM_VOLATILE_P (mem) = 1;
8409 if (! register_operand (new, mode))
8410 new = copy_to_mode_reg (mode, new);
8412 if (target && register_operand (target, mode))
8413 ret = target;
8414 else
8415 ret = gen_reg_rtx (mode);
8417 if (mode == SImode)
8418 insn = gen_xchgsi (ret, mem, new);
8419 else
8420 insn = gen_xchgdi (ret, mem, new);
8421 emit_insn (insn);
8423 return ret;
8426 /* Expand lock_release. I.e. `stsz.rel [ptr] = r0'. */
8428 static rtx
8429 ia64_expand_lock_release (enum machine_mode mode, tree arglist,
8430 rtx target ATTRIBUTE_UNUSED)
8432 tree arg0;
8433 rtx mem;
8435 arg0 = TREE_VALUE (arglist);
8436 mem = expand_expr (arg0, NULL_RTX, ptr_mode, 0);
8438 mem = gen_rtx_MEM (mode, force_reg (ptr_mode, mem));
8439 MEM_VOLATILE_P (mem) = 1;
8441 emit_move_insn (mem, const0_rtx);
8443 return const0_rtx;
8447 ia64_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
8448 enum machine_mode mode ATTRIBUTE_UNUSED,
8449 int ignore ATTRIBUTE_UNUSED)
8451 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
8452 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
8453 tree arglist = TREE_OPERAND (exp, 1);
8454 enum machine_mode rmode = VOIDmode;
8456 switch (fcode)
8458 case IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_SI:
8459 case IA64_BUILTIN_VAL_COMPARE_AND_SWAP_SI:
8460 mode = SImode;
8461 rmode = SImode;
8462 break;
8464 case IA64_BUILTIN_LOCK_TEST_AND_SET_SI:
8465 case IA64_BUILTIN_LOCK_RELEASE_SI:
8466 case IA64_BUILTIN_FETCH_AND_ADD_SI:
8467 case IA64_BUILTIN_FETCH_AND_SUB_SI:
8468 case IA64_BUILTIN_FETCH_AND_OR_SI:
8469 case IA64_BUILTIN_FETCH_AND_AND_SI:
8470 case IA64_BUILTIN_FETCH_AND_XOR_SI:
8471 case IA64_BUILTIN_FETCH_AND_NAND_SI:
8472 case IA64_BUILTIN_ADD_AND_FETCH_SI:
8473 case IA64_BUILTIN_SUB_AND_FETCH_SI:
8474 case IA64_BUILTIN_OR_AND_FETCH_SI:
8475 case IA64_BUILTIN_AND_AND_FETCH_SI:
8476 case IA64_BUILTIN_XOR_AND_FETCH_SI:
8477 case IA64_BUILTIN_NAND_AND_FETCH_SI:
8478 mode = SImode;
8479 break;
8481 case IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_DI:
8482 mode = DImode;
8483 rmode = SImode;
8484 break;
8486 case IA64_BUILTIN_VAL_COMPARE_AND_SWAP_DI:
8487 mode = DImode;
8488 rmode = DImode;
8489 break;
8491 case IA64_BUILTIN_LOCK_TEST_AND_SET_DI:
8492 case IA64_BUILTIN_LOCK_RELEASE_DI:
8493 case IA64_BUILTIN_FETCH_AND_ADD_DI:
8494 case IA64_BUILTIN_FETCH_AND_SUB_DI:
8495 case IA64_BUILTIN_FETCH_AND_OR_DI:
8496 case IA64_BUILTIN_FETCH_AND_AND_DI:
8497 case IA64_BUILTIN_FETCH_AND_XOR_DI:
8498 case IA64_BUILTIN_FETCH_AND_NAND_DI:
8499 case IA64_BUILTIN_ADD_AND_FETCH_DI:
8500 case IA64_BUILTIN_SUB_AND_FETCH_DI:
8501 case IA64_BUILTIN_OR_AND_FETCH_DI:
8502 case IA64_BUILTIN_AND_AND_FETCH_DI:
8503 case IA64_BUILTIN_XOR_AND_FETCH_DI:
8504 case IA64_BUILTIN_NAND_AND_FETCH_DI:
8505 mode = DImode;
8506 break;
8508 default:
8509 break;
8512 switch (fcode)
8514 case IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_SI:
8515 case IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_DI:
8516 return ia64_expand_compare_and_swap (rmode, mode, 1, arglist,
8517 target);
8519 case IA64_BUILTIN_VAL_COMPARE_AND_SWAP_SI:
8520 case IA64_BUILTIN_VAL_COMPARE_AND_SWAP_DI:
8521 return ia64_expand_compare_and_swap (rmode, mode, 0, arglist,
8522 target);
8524 case IA64_BUILTIN_SYNCHRONIZE:
8525 emit_insn (gen_mf ());
8526 return const0_rtx;
8528 case IA64_BUILTIN_LOCK_TEST_AND_SET_SI:
8529 case IA64_BUILTIN_LOCK_TEST_AND_SET_DI:
8530 return ia64_expand_lock_test_and_set (mode, arglist, target);
8532 case IA64_BUILTIN_LOCK_RELEASE_SI:
8533 case IA64_BUILTIN_LOCK_RELEASE_DI:
8534 return ia64_expand_lock_release (mode, arglist, target);
8536 case IA64_BUILTIN_BSP:
8537 if (! target || ! register_operand (target, DImode))
8538 target = gen_reg_rtx (DImode);
8539 emit_insn (gen_bsp_value (target));
8540 #ifdef POINTERS_EXTEND_UNSIGNED
8541 target = convert_memory_address (ptr_mode, target);
8542 #endif
8543 return target;
8545 case IA64_BUILTIN_FLUSHRS:
8546 emit_insn (gen_flushrs ());
8547 return const0_rtx;
8549 case IA64_BUILTIN_FETCH_AND_ADD_SI:
8550 case IA64_BUILTIN_FETCH_AND_ADD_DI:
8551 return ia64_expand_fetch_and_op (add_optab, mode, arglist, target);
8553 case IA64_BUILTIN_FETCH_AND_SUB_SI:
8554 case IA64_BUILTIN_FETCH_AND_SUB_DI:
8555 return ia64_expand_fetch_and_op (sub_optab, mode, arglist, target);
8557 case IA64_BUILTIN_FETCH_AND_OR_SI:
8558 case IA64_BUILTIN_FETCH_AND_OR_DI:
8559 return ia64_expand_fetch_and_op (ior_optab, mode, arglist, target);
8561 case IA64_BUILTIN_FETCH_AND_AND_SI:
8562 case IA64_BUILTIN_FETCH_AND_AND_DI:
8563 return ia64_expand_fetch_and_op (and_optab, mode, arglist, target);
8565 case IA64_BUILTIN_FETCH_AND_XOR_SI:
8566 case IA64_BUILTIN_FETCH_AND_XOR_DI:
8567 return ia64_expand_fetch_and_op (xor_optab, mode, arglist, target);
8569 case IA64_BUILTIN_FETCH_AND_NAND_SI:
8570 case IA64_BUILTIN_FETCH_AND_NAND_DI:
8571 return ia64_expand_fetch_and_op (one_cmpl_optab, mode, arglist, target);
8573 case IA64_BUILTIN_ADD_AND_FETCH_SI:
8574 case IA64_BUILTIN_ADD_AND_FETCH_DI:
8575 return ia64_expand_op_and_fetch (add_optab, mode, arglist, target);
8577 case IA64_BUILTIN_SUB_AND_FETCH_SI:
8578 case IA64_BUILTIN_SUB_AND_FETCH_DI:
8579 return ia64_expand_op_and_fetch (sub_optab, mode, arglist, target);
8581 case IA64_BUILTIN_OR_AND_FETCH_SI:
8582 case IA64_BUILTIN_OR_AND_FETCH_DI:
8583 return ia64_expand_op_and_fetch (ior_optab, mode, arglist, target);
8585 case IA64_BUILTIN_AND_AND_FETCH_SI:
8586 case IA64_BUILTIN_AND_AND_FETCH_DI:
8587 return ia64_expand_op_and_fetch (and_optab, mode, arglist, target);
8589 case IA64_BUILTIN_XOR_AND_FETCH_SI:
8590 case IA64_BUILTIN_XOR_AND_FETCH_DI:
8591 return ia64_expand_op_and_fetch (xor_optab, mode, arglist, target);
8593 case IA64_BUILTIN_NAND_AND_FETCH_SI:
8594 case IA64_BUILTIN_NAND_AND_FETCH_DI:
8595 return ia64_expand_op_and_fetch (one_cmpl_optab, mode, arglist, target);
8597 default:
8598 break;
8601 return NULL_RTX;
8604 /* For the HP-UX IA64 aggregate parameters are passed stored in the
8605 most significant bits of the stack slot. */
8607 enum direction
8608 ia64_hpux_function_arg_padding (enum machine_mode mode, tree type)
8610 /* Exception to normal case for structures/unions/etc. */
8612 if (type && AGGREGATE_TYPE_P (type)
8613 && int_size_in_bytes (type) < UNITS_PER_WORD)
8614 return upward;
8616 /* Fall back to the default. */
8617 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
8620 /* Linked list of all external functions that are to be emitted by GCC.
8621 We output the name if and only if TREE_SYMBOL_REFERENCED is set in
8622 order to avoid putting out names that are never really used. */
8624 struct extern_func_list GTY(())
8626 struct extern_func_list *next;
8627 tree decl;
8630 static GTY(()) struct extern_func_list *extern_func_head;
8632 static void
8633 ia64_hpux_add_extern_decl (tree decl)
8635 struct extern_func_list *p = ggc_alloc (sizeof (struct extern_func_list));
8637 p->decl = decl;
8638 p->next = extern_func_head;
8639 extern_func_head = p;
8642 /* Print out the list of used global functions. */
8644 static void
8645 ia64_hpux_file_end (void)
8647 struct extern_func_list *p;
8649 for (p = extern_func_head; p; p = p->next)
8651 tree decl = p->decl;
8652 tree id = DECL_ASSEMBLER_NAME (decl);
8654 if (!id)
8655 abort ();
8657 if (!TREE_ASM_WRITTEN (decl) && TREE_SYMBOL_REFERENCED (id))
8659 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
8661 TREE_ASM_WRITTEN (decl) = 1;
8662 (*targetm.asm_out.globalize_label) (asm_out_file, name);
8663 fputs (TYPE_ASM_OP, asm_out_file);
8664 assemble_name (asm_out_file, name);
8665 fprintf (asm_out_file, "," TYPE_OPERAND_FMT "\n", "function");
8669 extern_func_head = 0;
8672 /* Rename all the TFmode libfuncs using the HPUX conventions. */
8674 static void
8675 ia64_hpux_init_libfuncs (void)
8677 set_optab_libfunc (add_optab, TFmode, "_U_Qfadd");
8678 set_optab_libfunc (sub_optab, TFmode, "_U_Qfsub");
8679 set_optab_libfunc (smul_optab, TFmode, "_U_Qfmpy");
8680 set_optab_libfunc (sdiv_optab, TFmode, "_U_Qfdiv");
8681 set_optab_libfunc (smin_optab, TFmode, "_U_Qfmin");
8682 set_optab_libfunc (smax_optab, TFmode, "_U_Qfmax");
8683 set_optab_libfunc (abs_optab, TFmode, "_U_Qfabs");
8684 set_optab_libfunc (neg_optab, TFmode, "_U_Qfneg");
8686 /* ia64_expand_compare uses this. */
8687 cmptf_libfunc = init_one_libfunc ("_U_Qfcmp");
8689 /* These should never be used. */
8690 set_optab_libfunc (eq_optab, TFmode, 0);
8691 set_optab_libfunc (ne_optab, TFmode, 0);
8692 set_optab_libfunc (gt_optab, TFmode, 0);
8693 set_optab_libfunc (ge_optab, TFmode, 0);
8694 set_optab_libfunc (lt_optab, TFmode, 0);
8695 set_optab_libfunc (le_optab, TFmode, 0);
8697 set_conv_libfunc (sext_optab, TFmode, SFmode, "_U_Qfcnvff_sgl_to_quad");
8698 set_conv_libfunc (sext_optab, TFmode, DFmode, "_U_Qfcnvff_dbl_to_quad");
8699 set_conv_libfunc (sext_optab, TFmode, XFmode, "_U_Qfcnvff_f80_to_quad");
8700 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_U_Qfcnvff_quad_to_sgl");
8701 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_U_Qfcnvff_quad_to_dbl");
8702 set_conv_libfunc (trunc_optab, XFmode, TFmode, "_U_Qfcnvff_quad_to_f80");
8704 set_conv_libfunc (sfix_optab, SImode, TFmode, "_U_Qfcnvfxt_quad_to_sgl");
8705 set_conv_libfunc (sfix_optab, DImode, TFmode, "_U_Qfcnvfxt_quad_to_dbl");
8706 set_conv_libfunc (ufix_optab, SImode, TFmode, "_U_Qfcnvfxut_quad_to_sgl");
8707 set_conv_libfunc (ufix_optab, DImode, TFmode, "_U_Qfcnvfxut_quad_to_dbl");
8709 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_U_Qfcnvxf_sgl_to_quad");
8710 set_conv_libfunc (sfloat_optab, TFmode, DImode, "_U_Qfcnvxf_dbl_to_quad");
8713 /* Rename the division and modulus functions in VMS. */
8715 static void
8716 ia64_vms_init_libfuncs (void)
8718 set_optab_libfunc (sdiv_optab, SImode, "OTS$DIV_I");
8719 set_optab_libfunc (sdiv_optab, DImode, "OTS$DIV_L");
8720 set_optab_libfunc (udiv_optab, SImode, "OTS$DIV_UI");
8721 set_optab_libfunc (udiv_optab, DImode, "OTS$DIV_UL");
8722 set_optab_libfunc (smod_optab, SImode, "OTS$REM_I");
8723 set_optab_libfunc (smod_optab, DImode, "OTS$REM_L");
8724 set_optab_libfunc (umod_optab, SImode, "OTS$REM_UI");
8725 set_optab_libfunc (umod_optab, DImode, "OTS$REM_UL");
8728 /* Switch to the section to which we should output X. The only thing
8729 special we do here is to honor small data. */
8731 static void
8732 ia64_select_rtx_section (enum machine_mode mode, rtx x,
8733 unsigned HOST_WIDE_INT align)
8735 if (GET_MODE_SIZE (mode) > 0
8736 && GET_MODE_SIZE (mode) <= ia64_section_threshold)
8737 sdata_section ();
8738 else
8739 default_elf_select_rtx_section (mode, x, align);
8742 /* It is illegal to have relocations in shared segments on AIX and HPUX.
8743 Pretend flag_pic is always set. */
8745 static void
8746 ia64_rwreloc_select_section (tree exp, int reloc, unsigned HOST_WIDE_INT align)
8748 default_elf_select_section_1 (exp, reloc, align, true);
8751 static void
8752 ia64_rwreloc_unique_section (tree decl, int reloc)
8754 default_unique_section_1 (decl, reloc, true);
8757 static void
8758 ia64_rwreloc_select_rtx_section (enum machine_mode mode, rtx x,
8759 unsigned HOST_WIDE_INT align)
8761 int save_pic = flag_pic;
8762 flag_pic = 1;
8763 ia64_select_rtx_section (mode, x, align);
8764 flag_pic = save_pic;
8767 static unsigned int
8768 ia64_rwreloc_section_type_flags (tree decl, const char *name, int reloc)
8770 return default_section_type_flags_1 (decl, name, reloc, true);
8773 /* Returns true if FNTYPE (a FUNCTION_TYPE or a METHOD_TYPE) returns a
8774 structure type and that the address of that type should be passed
8775 in out0, rather than in r8. */
8777 static bool
8778 ia64_struct_retval_addr_is_first_parm_p (tree fntype)
8780 tree ret_type = TREE_TYPE (fntype);
8782 /* The Itanium C++ ABI requires that out0, rather than r8, be used
8783 as the structure return address parameter, if the return value
8784 type has a non-trivial copy constructor or destructor. It is not
8785 clear if this same convention should be used for other
8786 programming languages. Until G++ 3.4, we incorrectly used r8 for
8787 these return values. */
8788 return (abi_version_at_least (2)
8789 && ret_type
8790 && TYPE_MODE (ret_type) == BLKmode
8791 && TREE_ADDRESSABLE (ret_type)
8792 && strcmp (lang_hooks.name, "GNU C++") == 0);
8795 /* Output the assembler code for a thunk function. THUNK_DECL is the
8796 declaration for the thunk function itself, FUNCTION is the decl for
8797 the target function. DELTA is an immediate constant offset to be
8798 added to THIS. If VCALL_OFFSET is nonzero, the word at
8799 *(*this + vcall_offset) should be added to THIS. */
8801 static void
8802 ia64_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
8803 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
8804 tree function)
8806 rtx this, insn, funexp;
8807 unsigned int this_parmno;
8808 unsigned int this_regno;
8810 reload_completed = 1;
8811 epilogue_completed = 1;
8812 no_new_pseudos = 1;
8814 /* Set things up as ia64_expand_prologue might. */
8815 last_scratch_gr_reg = 15;
8817 memset (&current_frame_info, 0, sizeof (current_frame_info));
8818 current_frame_info.spill_cfa_off = -16;
8819 current_frame_info.n_input_regs = 1;
8820 current_frame_info.need_regstk = (TARGET_REG_NAMES != 0);
8822 /* Mark the end of the (empty) prologue. */
8823 emit_note (NOTE_INSN_PROLOGUE_END);
8825 /* Figure out whether "this" will be the first parameter (the
8826 typical case) or the second parameter (as happens when the
8827 virtual function returns certain class objects). */
8828 this_parmno
8829 = (ia64_struct_retval_addr_is_first_parm_p (TREE_TYPE (thunk))
8830 ? 1 : 0);
8831 this_regno = IN_REG (this_parmno);
8832 if (!TARGET_REG_NAMES)
8833 reg_names[this_regno] = ia64_reg_numbers[this_parmno];
8835 this = gen_rtx_REG (Pmode, this_regno);
8836 if (TARGET_ILP32)
8838 rtx tmp = gen_rtx_REG (ptr_mode, this_regno);
8839 REG_POINTER (tmp) = 1;
8840 if (delta && CONST_OK_FOR_I (delta))
8842 emit_insn (gen_ptr_extend_plus_imm (this, tmp, GEN_INT (delta)));
8843 delta = 0;
8845 else
8846 emit_insn (gen_ptr_extend (this, tmp));
8849 /* Apply the constant offset, if required. */
8850 if (delta)
8852 rtx delta_rtx = GEN_INT (delta);
8854 if (!CONST_OK_FOR_I (delta))
8856 rtx tmp = gen_rtx_REG (Pmode, 2);
8857 emit_move_insn (tmp, delta_rtx);
8858 delta_rtx = tmp;
8860 emit_insn (gen_adddi3 (this, this, delta_rtx));
8863 /* Apply the offset from the vtable, if required. */
8864 if (vcall_offset)
8866 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
8867 rtx tmp = gen_rtx_REG (Pmode, 2);
8869 if (TARGET_ILP32)
8871 rtx t = gen_rtx_REG (ptr_mode, 2);
8872 REG_POINTER (t) = 1;
8873 emit_move_insn (t, gen_rtx_MEM (ptr_mode, this));
8874 if (CONST_OK_FOR_I (vcall_offset))
8876 emit_insn (gen_ptr_extend_plus_imm (tmp, t,
8877 vcall_offset_rtx));
8878 vcall_offset = 0;
8880 else
8881 emit_insn (gen_ptr_extend (tmp, t));
8883 else
8884 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this));
8886 if (vcall_offset)
8888 if (!CONST_OK_FOR_J (vcall_offset))
8890 rtx tmp2 = gen_rtx_REG (Pmode, next_scratch_gr_reg ());
8891 emit_move_insn (tmp2, vcall_offset_rtx);
8892 vcall_offset_rtx = tmp2;
8894 emit_insn (gen_adddi3 (tmp, tmp, vcall_offset_rtx));
8897 if (TARGET_ILP32)
8898 emit_move_insn (gen_rtx_REG (ptr_mode, 2),
8899 gen_rtx_MEM (ptr_mode, tmp));
8900 else
8901 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
8903 emit_insn (gen_adddi3 (this, this, tmp));
8906 /* Generate a tail call to the target function. */
8907 if (! TREE_USED (function))
8909 assemble_external (function);
8910 TREE_USED (function) = 1;
8912 funexp = XEXP (DECL_RTL (function), 0);
8913 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
8914 ia64_expand_call (NULL_RTX, funexp, NULL_RTX, 1);
8915 insn = get_last_insn ();
8916 SIBLING_CALL_P (insn) = 1;
8918 /* Code generation for calls relies on splitting. */
8919 reload_completed = 1;
8920 epilogue_completed = 1;
8921 try_split (PATTERN (insn), insn, 0);
8923 emit_barrier ();
8925 /* Run just enough of rest_of_compilation to get the insns emitted.
8926 There's not really enough bulk here to make other passes such as
8927 instruction scheduling worth while. Note that use_thunk calls
8928 assemble_start_function and assemble_end_function. */
8930 insn_locators_initialize ();
8931 emit_all_insn_group_barriers (NULL);
8932 insn = get_insns ();
8933 shorten_branches (insn);
8934 final_start_function (insn, file, 1);
8935 final (insn, file, 1, 0);
8936 final_end_function ();
8938 reload_completed = 0;
8939 epilogue_completed = 0;
8940 no_new_pseudos = 0;
8943 /* Worker function for TARGET_STRUCT_VALUE_RTX. */
8945 static rtx
8946 ia64_struct_value_rtx (tree fntype,
8947 int incoming ATTRIBUTE_UNUSED)
8949 if (fntype && ia64_struct_retval_addr_is_first_parm_p (fntype))
8950 return NULL_RTX;
8951 return gen_rtx_REG (Pmode, GR_REG (8));
8954 #include "gt-ia64.h"