OSDN Git Service

0c2d20eaf6e0e8574efaaead8b18d22b2ef06f46
[pf3gnuchains/gcc-fork.git] / gcc / config / ia64 / ia64.c
1 /* Definitions of target machine for GNU compiler.
2    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3    2009, 2010
4    Free Software Foundation, Inc.
5    Contributed by James E. Wilson <wilson@cygnus.com> and
6                   David Mosberger <davidm@hpl.hp.com>.
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
14
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
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 "insn-config.h"
33 #include "conditions.h"
34 #include "output.h"
35 #include "insn-attr.h"
36 #include "flags.h"
37 #include "recog.h"
38 #include "expr.h"
39 #include "optabs.h"
40 #include "except.h"
41 #include "function.h"
42 #include "ggc.h"
43 #include "basic-block.h"
44 #include "libfuncs.h"
45 #include "diagnostic-core.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"
54 #include "gimple.h"
55 #include "intl.h"
56 #include "df.h"
57 #include "debug.h"
58 #include "params.h"
59 #include "dbgcnt.h"
60 #include "tm-constrs.h"
61 #include "sel-sched.h"
62 #include "reload.h"
63
64 /* This is used for communication between ASM_OUTPUT_LABEL and
65    ASM_OUTPUT_LABELREF.  */
66 int ia64_asm_output_label = 0;
67
68 /* Register names for ia64_expand_prologue.  */
69 static const char * const ia64_reg_numbers[96] =
70 { "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
71   "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
72   "r48", "r49", "r50", "r51", "r52", "r53", "r54", "r55",
73   "r56", "r57", "r58", "r59", "r60", "r61", "r62", "r63",
74   "r64", "r65", "r66", "r67", "r68", "r69", "r70", "r71",
75   "r72", "r73", "r74", "r75", "r76", "r77", "r78", "r79",
76   "r80", "r81", "r82", "r83", "r84", "r85", "r86", "r87",
77   "r88", "r89", "r90", "r91", "r92", "r93", "r94", "r95",
78   "r96", "r97", "r98", "r99", "r100","r101","r102","r103",
79   "r104","r105","r106","r107","r108","r109","r110","r111",
80   "r112","r113","r114","r115","r116","r117","r118","r119",
81   "r120","r121","r122","r123","r124","r125","r126","r127"};
82
83 /* ??? These strings could be shared with REGISTER_NAMES.  */
84 static const char * const ia64_input_reg_names[8] =
85 { "in0",  "in1",  "in2",  "in3",  "in4",  "in5",  "in6",  "in7" };
86
87 /* ??? These strings could be shared with REGISTER_NAMES.  */
88 static const char * const ia64_local_reg_names[80] =
89 { "loc0", "loc1", "loc2", "loc3", "loc4", "loc5", "loc6", "loc7",
90   "loc8", "loc9", "loc10","loc11","loc12","loc13","loc14","loc15",
91   "loc16","loc17","loc18","loc19","loc20","loc21","loc22","loc23",
92   "loc24","loc25","loc26","loc27","loc28","loc29","loc30","loc31",
93   "loc32","loc33","loc34","loc35","loc36","loc37","loc38","loc39",
94   "loc40","loc41","loc42","loc43","loc44","loc45","loc46","loc47",
95   "loc48","loc49","loc50","loc51","loc52","loc53","loc54","loc55",
96   "loc56","loc57","loc58","loc59","loc60","loc61","loc62","loc63",
97   "loc64","loc65","loc66","loc67","loc68","loc69","loc70","loc71",
98   "loc72","loc73","loc74","loc75","loc76","loc77","loc78","loc79" };
99
100 /* ??? These strings could be shared with REGISTER_NAMES.  */
101 static const char * const ia64_output_reg_names[8] =
102 { "out0", "out1", "out2", "out3", "out4", "out5", "out6", "out7" };
103
104 /* Which cpu are we scheduling for.  */
105 enum processor_type ia64_tune = PROCESSOR_ITANIUM2;
106
107 /* Determines whether we run our final scheduling pass or not.  We always
108    avoid the normal second scheduling pass.  */
109 static int ia64_flag_schedule_insns2;
110
111 /* Determines whether we run variable tracking in machine dependent
112    reorganization.  */
113 static int ia64_flag_var_tracking;
114
115 /* Variables which are this size or smaller are put in the sdata/sbss
116    sections.  */
117
118 unsigned int ia64_section_threshold;
119
120 /* The following variable is used by the DFA insn scheduler.  The value is
121    TRUE if we do insn bundling instead of insn scheduling.  */
122 int bundling_p = 0;
123
124 enum ia64_frame_regs
125 {
126    reg_fp,
127    reg_save_b0,
128    reg_save_pr,
129    reg_save_ar_pfs,
130    reg_save_ar_unat,
131    reg_save_ar_lc,
132    reg_save_gp,
133    number_of_ia64_frame_regs
134 };
135
136 /* Structure to be filled in by ia64_compute_frame_size with register
137    save masks and offsets for the current function.  */
138
139 struct ia64_frame_info
140 {
141   HOST_WIDE_INT total_size;     /* size of the stack frame, not including
142                                    the caller's scratch area.  */
143   HOST_WIDE_INT spill_cfa_off;  /* top of the reg spill area from the cfa.  */
144   HOST_WIDE_INT spill_size;     /* size of the gr/br/fr spill area.  */
145   HOST_WIDE_INT extra_spill_size;  /* size of spill area for others.  */
146   HARD_REG_SET mask;            /* mask of saved registers.  */
147   unsigned int gr_used_mask;    /* mask of registers in use as gr spill
148                                    registers or long-term scratches.  */
149   int n_spilled;                /* number of spilled registers.  */
150   int r[number_of_ia64_frame_regs];  /* Frame related registers.  */
151   int n_input_regs;             /* number of input registers used.  */
152   int n_local_regs;             /* number of local registers used.  */
153   int n_output_regs;            /* number of output registers used.  */
154   int n_rotate_regs;            /* number of rotating registers used.  */
155
156   char need_regstk;             /* true if a .regstk directive needed.  */
157   char initialized;             /* true if the data is finalized.  */
158 };
159
160 /* Current frame information calculated by ia64_compute_frame_size.  */
161 static struct ia64_frame_info current_frame_info;
162 /* The actual registers that are emitted.  */
163 static int emitted_frame_related_regs[number_of_ia64_frame_regs];
164 \f
165 static int ia64_first_cycle_multipass_dfa_lookahead (void);
166 static void ia64_dependencies_evaluation_hook (rtx, rtx);
167 static void ia64_init_dfa_pre_cycle_insn (void);
168 static rtx ia64_dfa_pre_cycle_insn (void);
169 static int ia64_first_cycle_multipass_dfa_lookahead_guard (rtx);
170 static bool ia64_first_cycle_multipass_dfa_lookahead_guard_spec (const_rtx);
171 static int ia64_dfa_new_cycle (FILE *, int, rtx, int, int, int *);
172 static void ia64_h_i_d_extended (void);
173 static void * ia64_alloc_sched_context (void);
174 static void ia64_init_sched_context (void *, bool);
175 static void ia64_set_sched_context (void *);
176 static void ia64_clear_sched_context (void *);
177 static void ia64_free_sched_context (void *);
178 static int ia64_mode_to_int (enum machine_mode);
179 static void ia64_set_sched_flags (spec_info_t);
180 static ds_t ia64_get_insn_spec_ds (rtx);
181 static ds_t ia64_get_insn_checked_ds (rtx);
182 static bool ia64_skip_rtx_p (const_rtx);
183 static int ia64_speculate_insn (rtx, ds_t, rtx *);
184 static bool ia64_needs_block_p (int);
185 static rtx ia64_gen_spec_check (rtx, rtx, ds_t);
186 static int ia64_spec_check_p (rtx);
187 static int ia64_spec_check_src_p (rtx);
188 static rtx gen_tls_get_addr (void);
189 static rtx gen_thread_pointer (void);
190 static int find_gr_spill (enum ia64_frame_regs, int);
191 static int next_scratch_gr_reg (void);
192 static void mark_reg_gr_used_mask (rtx, void *);
193 static void ia64_compute_frame_size (HOST_WIDE_INT);
194 static void setup_spill_pointers (int, rtx, HOST_WIDE_INT);
195 static void finish_spill_pointers (void);
196 static rtx spill_restore_mem (rtx, HOST_WIDE_INT);
197 static void do_spill (rtx (*)(rtx, rtx, rtx), rtx, HOST_WIDE_INT, rtx);
198 static void do_restore (rtx (*)(rtx, rtx, rtx), rtx, HOST_WIDE_INT);
199 static rtx gen_movdi_x (rtx, rtx, rtx);
200 static rtx gen_fr_spill_x (rtx, rtx, rtx);
201 static rtx gen_fr_restore_x (rtx, rtx, rtx);
202
203 static void ia64_option_override (void);
204 static void ia64_option_default_params (void);
205 static bool ia64_can_eliminate (const int, const int);
206 static enum machine_mode hfa_element_mode (const_tree, bool);
207 static void ia64_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
208                                          tree, int *, int);
209 static int ia64_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
210                                    tree, bool);
211 static rtx ia64_function_arg_1 (const CUMULATIVE_ARGS *, enum machine_mode,
212                                 const_tree, bool, bool);
213 static rtx ia64_function_arg (CUMULATIVE_ARGS *, enum machine_mode,
214                               const_tree, bool);
215 static rtx ia64_function_incoming_arg (CUMULATIVE_ARGS *,
216                                        enum machine_mode, const_tree, bool);
217 static void ia64_function_arg_advance (CUMULATIVE_ARGS *, enum machine_mode,
218                                        const_tree, bool);
219 static unsigned int ia64_function_arg_boundary (enum machine_mode,
220                                                 const_tree);
221 static bool ia64_function_ok_for_sibcall (tree, tree);
222 static bool ia64_return_in_memory (const_tree, const_tree);
223 static rtx ia64_function_value (const_tree, const_tree, bool);
224 static rtx ia64_libcall_value (enum machine_mode, const_rtx);
225 static bool ia64_function_value_regno_p (const unsigned int);
226 static int ia64_register_move_cost (enum machine_mode, reg_class_t,
227                                     reg_class_t);
228 static int ia64_memory_move_cost (enum machine_mode mode, reg_class_t,
229                                   bool);
230 static bool ia64_rtx_costs (rtx, int, int, int *, bool);
231 static int ia64_unspec_may_trap_p (const_rtx, unsigned);
232 static void fix_range (const char *);
233 static bool ia64_handle_option (size_t, const char *, int);
234 static struct machine_function * ia64_init_machine_status (void);
235 static void emit_insn_group_barriers (FILE *);
236 static void emit_all_insn_group_barriers (FILE *);
237 static void final_emit_insn_group_barriers (FILE *);
238 static void emit_predicate_relation_info (void);
239 static void ia64_reorg (void);
240 static bool ia64_in_small_data_p (const_tree);
241 static void process_epilogue (FILE *, rtx, bool, bool);
242
243 static bool ia64_assemble_integer (rtx, unsigned int, int);
244 static void ia64_output_function_prologue (FILE *, HOST_WIDE_INT);
245 static void ia64_output_function_epilogue (FILE *, HOST_WIDE_INT);
246 static void ia64_output_function_end_prologue (FILE *);
247
248 static int ia64_issue_rate (void);
249 static int ia64_adjust_cost_2 (rtx, int, rtx, int, dw_t);
250 static void ia64_sched_init (FILE *, int, int);
251 static void ia64_sched_init_global (FILE *, int, int);
252 static void ia64_sched_finish_global (FILE *, int);
253 static void ia64_sched_finish (FILE *, int);
254 static int ia64_dfa_sched_reorder (FILE *, int, rtx *, int *, int, int);
255 static int ia64_sched_reorder (FILE *, int, rtx *, int *, int);
256 static int ia64_sched_reorder2 (FILE *, int, rtx *, int *, int);
257 static int ia64_variable_issue (FILE *, int, rtx, int);
258
259 static void ia64_asm_unwind_emit (FILE *, rtx);
260 static void ia64_asm_emit_except_personality (rtx);
261 static void ia64_asm_init_sections (void);
262
263 static enum unwind_info_type ia64_debug_unwind_info (void);
264 static enum unwind_info_type ia64_except_unwind_info (struct gcc_options *);
265
266 static struct bundle_state *get_free_bundle_state (void);
267 static void free_bundle_state (struct bundle_state *);
268 static void initiate_bundle_states (void);
269 static void finish_bundle_states (void);
270 static unsigned bundle_state_hash (const void *);
271 static int bundle_state_eq_p (const void *, const void *);
272 static int insert_bundle_state (struct bundle_state *);
273 static void initiate_bundle_state_table (void);
274 static void finish_bundle_state_table (void);
275 static int try_issue_nops (struct bundle_state *, int);
276 static int try_issue_insn (struct bundle_state *, rtx);
277 static void issue_nops_and_insn (struct bundle_state *, int, rtx, int, int);
278 static int get_max_pos (state_t);
279 static int get_template (state_t, int);
280
281 static rtx get_next_important_insn (rtx, rtx);
282 static bool important_for_bundling_p (rtx);
283 static void bundling (FILE *, int, rtx, rtx);
284
285 static void ia64_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
286                                   HOST_WIDE_INT, tree);
287 static void ia64_file_start (void);
288 static void ia64_globalize_decl_name (FILE *, tree);
289
290 static int ia64_hpux_reloc_rw_mask (void) ATTRIBUTE_UNUSED;
291 static int ia64_reloc_rw_mask (void) ATTRIBUTE_UNUSED;
292 static section *ia64_select_rtx_section (enum machine_mode, rtx,
293                                          unsigned HOST_WIDE_INT);
294 static void ia64_output_dwarf_dtprel (FILE *, int, rtx)
295      ATTRIBUTE_UNUSED;
296 static unsigned int ia64_section_type_flags (tree, const char *, int);
297 static void ia64_init_libfuncs (void)
298      ATTRIBUTE_UNUSED;
299 static void ia64_hpux_init_libfuncs (void)
300      ATTRIBUTE_UNUSED;
301 static void ia64_sysv4_init_libfuncs (void)
302      ATTRIBUTE_UNUSED;
303 static void ia64_vms_init_libfuncs (void)
304      ATTRIBUTE_UNUSED;
305 static void ia64_soft_fp_init_libfuncs (void)
306      ATTRIBUTE_UNUSED;
307 static bool ia64_vms_valid_pointer_mode (enum machine_mode mode)
308      ATTRIBUTE_UNUSED;
309 static tree ia64_vms_common_object_attribute (tree *, tree, tree, int, bool *)
310      ATTRIBUTE_UNUSED;
311
312 static tree ia64_handle_model_attribute (tree *, tree, tree, int, bool *);
313 static tree ia64_handle_version_id_attribute (tree *, tree, tree, int, bool *);
314 static void ia64_encode_section_info (tree, rtx, int);
315 static rtx ia64_struct_value_rtx (tree, int);
316 static tree ia64_gimplify_va_arg (tree, tree, gimple_seq *, gimple_seq *);
317 static bool ia64_scalar_mode_supported_p (enum machine_mode mode);
318 static bool ia64_vector_mode_supported_p (enum machine_mode mode);
319 static bool ia64_cannot_force_const_mem (rtx);
320 static const char *ia64_mangle_type (const_tree);
321 static const char *ia64_invalid_conversion (const_tree, const_tree);
322 static const char *ia64_invalid_unary_op (int, const_tree);
323 static const char *ia64_invalid_binary_op (int, const_tree, const_tree);
324 static enum machine_mode ia64_c_mode_for_suffix (char);
325 static enum machine_mode ia64_promote_function_mode (const_tree,
326                                                      enum machine_mode,
327                                                      int *,
328                                                      const_tree,
329                                                      int);
330 static void ia64_trampoline_init (rtx, tree, rtx);
331 static void ia64_override_options_after_change (void);
332
333 static void ia64_dwarf_handle_frame_unspec (const char *, rtx, int);
334 static tree ia64_builtin_decl (unsigned, bool);
335
336 static reg_class_t ia64_preferred_reload_class (rtx, reg_class_t);
337 static enum machine_mode ia64_get_reg_raw_mode (int regno);
338 static section * ia64_hpux_function_section (tree, enum node_frequency,
339                                              bool, bool);
340 \f
341 /* Table of valid machine attributes.  */
342 static const struct attribute_spec ia64_attribute_table[] =
343 {
344   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
345   { "syscall_linkage", 0, 0, false, true,  true,  NULL },
346   { "model",           1, 1, true, false, false, ia64_handle_model_attribute },
347 #if TARGET_ABI_OPEN_VMS
348   { "common_object",   1, 1, true, false, false, ia64_vms_common_object_attribute},
349 #endif
350   { "version_id",      1, 1, true, false, false,
351     ia64_handle_version_id_attribute },
352   { NULL,              0, 0, false, false, false, NULL }
353 };
354
355 /* Implement overriding of the optimization options.  */
356 static const struct default_options ia64_option_optimization_table[] =
357   {
358     { OPT_LEVELS_1_PLUS, OPT_fomit_frame_pointer, NULL, 1 },
359 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
360     SUBTARGET_OPTIMIZATION_OPTIONS,
361 #endif
362     { OPT_LEVELS_NONE, 0, NULL, 0 }
363   };
364
365 /* Initialize the GCC target structure.  */
366 #undef TARGET_ATTRIBUTE_TABLE
367 #define TARGET_ATTRIBUTE_TABLE ia64_attribute_table
368
369 #undef TARGET_INIT_BUILTINS
370 #define TARGET_INIT_BUILTINS ia64_init_builtins
371
372 #undef TARGET_EXPAND_BUILTIN
373 #define TARGET_EXPAND_BUILTIN ia64_expand_builtin
374
375 #undef TARGET_BUILTIN_DECL
376 #define TARGET_BUILTIN_DECL ia64_builtin_decl
377
378 #undef TARGET_ASM_BYTE_OP
379 #define TARGET_ASM_BYTE_OP "\tdata1\t"
380 #undef TARGET_ASM_ALIGNED_HI_OP
381 #define TARGET_ASM_ALIGNED_HI_OP "\tdata2\t"
382 #undef TARGET_ASM_ALIGNED_SI_OP
383 #define TARGET_ASM_ALIGNED_SI_OP "\tdata4\t"
384 #undef TARGET_ASM_ALIGNED_DI_OP
385 #define TARGET_ASM_ALIGNED_DI_OP "\tdata8\t"
386 #undef TARGET_ASM_UNALIGNED_HI_OP
387 #define TARGET_ASM_UNALIGNED_HI_OP "\tdata2.ua\t"
388 #undef TARGET_ASM_UNALIGNED_SI_OP
389 #define TARGET_ASM_UNALIGNED_SI_OP "\tdata4.ua\t"
390 #undef TARGET_ASM_UNALIGNED_DI_OP
391 #define TARGET_ASM_UNALIGNED_DI_OP "\tdata8.ua\t"
392 #undef TARGET_ASM_INTEGER
393 #define TARGET_ASM_INTEGER ia64_assemble_integer
394
395 #undef TARGET_OPTION_OVERRIDE
396 #define TARGET_OPTION_OVERRIDE ia64_option_override
397 #undef TARGET_OPTION_OPTIMIZATION_TABLE
398 #define TARGET_OPTION_OPTIMIZATION_TABLE ia64_option_optimization_table
399 #undef TARGET_OPTION_DEFAULT_PARAMS
400 #define TARGET_OPTION_DEFAULT_PARAMS ia64_option_default_params
401
402 #undef TARGET_ASM_FUNCTION_PROLOGUE
403 #define TARGET_ASM_FUNCTION_PROLOGUE ia64_output_function_prologue
404 #undef TARGET_ASM_FUNCTION_END_PROLOGUE
405 #define TARGET_ASM_FUNCTION_END_PROLOGUE ia64_output_function_end_prologue
406 #undef TARGET_ASM_FUNCTION_EPILOGUE
407 #define TARGET_ASM_FUNCTION_EPILOGUE ia64_output_function_epilogue
408
409 #undef TARGET_IN_SMALL_DATA_P
410 #define TARGET_IN_SMALL_DATA_P  ia64_in_small_data_p
411
412 #undef TARGET_SCHED_ADJUST_COST_2
413 #define TARGET_SCHED_ADJUST_COST_2 ia64_adjust_cost_2
414 #undef TARGET_SCHED_ISSUE_RATE
415 #define TARGET_SCHED_ISSUE_RATE ia64_issue_rate
416 #undef TARGET_SCHED_VARIABLE_ISSUE
417 #define TARGET_SCHED_VARIABLE_ISSUE ia64_variable_issue
418 #undef TARGET_SCHED_INIT
419 #define TARGET_SCHED_INIT ia64_sched_init
420 #undef TARGET_SCHED_FINISH
421 #define TARGET_SCHED_FINISH ia64_sched_finish
422 #undef TARGET_SCHED_INIT_GLOBAL
423 #define TARGET_SCHED_INIT_GLOBAL ia64_sched_init_global
424 #undef TARGET_SCHED_FINISH_GLOBAL
425 #define TARGET_SCHED_FINISH_GLOBAL ia64_sched_finish_global
426 #undef TARGET_SCHED_REORDER
427 #define TARGET_SCHED_REORDER ia64_sched_reorder
428 #undef TARGET_SCHED_REORDER2
429 #define TARGET_SCHED_REORDER2 ia64_sched_reorder2
430
431 #undef TARGET_SCHED_DEPENDENCIES_EVALUATION_HOOK
432 #define TARGET_SCHED_DEPENDENCIES_EVALUATION_HOOK ia64_dependencies_evaluation_hook
433
434 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
435 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD ia64_first_cycle_multipass_dfa_lookahead
436
437 #undef TARGET_SCHED_INIT_DFA_PRE_CYCLE_INSN
438 #define TARGET_SCHED_INIT_DFA_PRE_CYCLE_INSN ia64_init_dfa_pre_cycle_insn
439 #undef TARGET_SCHED_DFA_PRE_CYCLE_INSN
440 #define TARGET_SCHED_DFA_PRE_CYCLE_INSN ia64_dfa_pre_cycle_insn
441
442 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
443 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD\
444   ia64_first_cycle_multipass_dfa_lookahead_guard
445
446 #undef TARGET_SCHED_DFA_NEW_CYCLE
447 #define TARGET_SCHED_DFA_NEW_CYCLE ia64_dfa_new_cycle
448
449 #undef TARGET_SCHED_H_I_D_EXTENDED
450 #define TARGET_SCHED_H_I_D_EXTENDED ia64_h_i_d_extended
451
452 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
453 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT ia64_alloc_sched_context
454
455 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
456 #define TARGET_SCHED_INIT_SCHED_CONTEXT ia64_init_sched_context
457
458 #undef TARGET_SCHED_SET_SCHED_CONTEXT
459 #define TARGET_SCHED_SET_SCHED_CONTEXT ia64_set_sched_context
460
461 #undef TARGET_SCHED_CLEAR_SCHED_CONTEXT
462 #define TARGET_SCHED_CLEAR_SCHED_CONTEXT ia64_clear_sched_context
463
464 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
465 #define TARGET_SCHED_FREE_SCHED_CONTEXT ia64_free_sched_context
466
467 #undef TARGET_SCHED_SET_SCHED_FLAGS
468 #define TARGET_SCHED_SET_SCHED_FLAGS ia64_set_sched_flags
469
470 #undef TARGET_SCHED_GET_INSN_SPEC_DS
471 #define TARGET_SCHED_GET_INSN_SPEC_DS ia64_get_insn_spec_ds
472
473 #undef TARGET_SCHED_GET_INSN_CHECKED_DS
474 #define TARGET_SCHED_GET_INSN_CHECKED_DS ia64_get_insn_checked_ds
475
476 #undef TARGET_SCHED_SPECULATE_INSN
477 #define TARGET_SCHED_SPECULATE_INSN ia64_speculate_insn
478
479 #undef TARGET_SCHED_NEEDS_BLOCK_P
480 #define TARGET_SCHED_NEEDS_BLOCK_P ia64_needs_block_p
481
482 #undef TARGET_SCHED_GEN_SPEC_CHECK
483 #define TARGET_SCHED_GEN_SPEC_CHECK ia64_gen_spec_check
484
485 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD_SPEC
486 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD_SPEC\
487   ia64_first_cycle_multipass_dfa_lookahead_guard_spec
488
489 #undef TARGET_SCHED_SKIP_RTX_P
490 #define TARGET_SCHED_SKIP_RTX_P ia64_skip_rtx_p
491
492 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
493 #define TARGET_FUNCTION_OK_FOR_SIBCALL ia64_function_ok_for_sibcall
494 #undef TARGET_ARG_PARTIAL_BYTES
495 #define TARGET_ARG_PARTIAL_BYTES ia64_arg_partial_bytes
496 #undef TARGET_FUNCTION_ARG
497 #define TARGET_FUNCTION_ARG ia64_function_arg
498 #undef TARGET_FUNCTION_INCOMING_ARG
499 #define TARGET_FUNCTION_INCOMING_ARG ia64_function_incoming_arg
500 #undef TARGET_FUNCTION_ARG_ADVANCE
501 #define TARGET_FUNCTION_ARG_ADVANCE ia64_function_arg_advance
502 #undef TARGET_FUNCTION_ARG_BOUNDARY
503 #define TARGET_FUNCTION_ARG_BOUNDARY ia64_function_arg_boundary
504
505 #undef TARGET_ASM_OUTPUT_MI_THUNK
506 #define TARGET_ASM_OUTPUT_MI_THUNK ia64_output_mi_thunk
507 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
508 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
509
510 #undef TARGET_ASM_FILE_START
511 #define TARGET_ASM_FILE_START ia64_file_start
512
513 #undef TARGET_ASM_GLOBALIZE_DECL_NAME
514 #define TARGET_ASM_GLOBALIZE_DECL_NAME ia64_globalize_decl_name
515
516 #undef TARGET_REGISTER_MOVE_COST
517 #define TARGET_REGISTER_MOVE_COST ia64_register_move_cost
518 #undef TARGET_MEMORY_MOVE_COST
519 #define TARGET_MEMORY_MOVE_COST ia64_memory_move_cost
520 #undef TARGET_RTX_COSTS
521 #define TARGET_RTX_COSTS ia64_rtx_costs
522 #undef TARGET_ADDRESS_COST
523 #define TARGET_ADDRESS_COST hook_int_rtx_bool_0
524
525 #undef TARGET_UNSPEC_MAY_TRAP_P
526 #define TARGET_UNSPEC_MAY_TRAP_P ia64_unspec_may_trap_p
527
528 #undef TARGET_MACHINE_DEPENDENT_REORG
529 #define TARGET_MACHINE_DEPENDENT_REORG ia64_reorg
530
531 #undef TARGET_ENCODE_SECTION_INFO
532 #define TARGET_ENCODE_SECTION_INFO ia64_encode_section_info
533
534 #undef  TARGET_SECTION_TYPE_FLAGS
535 #define TARGET_SECTION_TYPE_FLAGS  ia64_section_type_flags
536
537 #ifdef HAVE_AS_TLS
538 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
539 #define TARGET_ASM_OUTPUT_DWARF_DTPREL ia64_output_dwarf_dtprel
540 #endif
541
542 #undef TARGET_PROMOTE_FUNCTION_MODE
543 #define TARGET_PROMOTE_FUNCTION_MODE ia64_promote_function_mode
544
545 /* ??? Investigate.  */
546 #if 0
547 #undef TARGET_PROMOTE_PROTOTYPES
548 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
549 #endif
550
551 #undef TARGET_FUNCTION_VALUE
552 #define TARGET_FUNCTION_VALUE ia64_function_value
553 #undef TARGET_LIBCALL_VALUE
554 #define TARGET_LIBCALL_VALUE ia64_libcall_value
555 #undef TARGET_FUNCTION_VALUE_REGNO_P
556 #define TARGET_FUNCTION_VALUE_REGNO_P ia64_function_value_regno_p
557
558 #undef TARGET_STRUCT_VALUE_RTX
559 #define TARGET_STRUCT_VALUE_RTX ia64_struct_value_rtx
560 #undef TARGET_RETURN_IN_MEMORY
561 #define TARGET_RETURN_IN_MEMORY ia64_return_in_memory
562 #undef TARGET_SETUP_INCOMING_VARARGS
563 #define TARGET_SETUP_INCOMING_VARARGS ia64_setup_incoming_varargs
564 #undef TARGET_STRICT_ARGUMENT_NAMING
565 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
566 #undef TARGET_MUST_PASS_IN_STACK
567 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
568 #undef TARGET_GET_RAW_RESULT_MODE
569 #define TARGET_GET_RAW_RESULT_MODE ia64_get_reg_raw_mode
570 #undef TARGET_GET_RAW_ARG_MODE
571 #define TARGET_GET_RAW_ARG_MODE ia64_get_reg_raw_mode
572
573 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
574 #define TARGET_GIMPLIFY_VA_ARG_EXPR ia64_gimplify_va_arg
575
576 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
577 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC  ia64_dwarf_handle_frame_unspec
578 #undef TARGET_ASM_UNWIND_EMIT
579 #define TARGET_ASM_UNWIND_EMIT ia64_asm_unwind_emit
580 #undef TARGET_ASM_EMIT_EXCEPT_PERSONALITY
581 #define TARGET_ASM_EMIT_EXCEPT_PERSONALITY  ia64_asm_emit_except_personality
582 #undef TARGET_ASM_INIT_SECTIONS
583 #define TARGET_ASM_INIT_SECTIONS  ia64_asm_init_sections
584
585 #undef TARGET_DEBUG_UNWIND_INFO
586 #define TARGET_DEBUG_UNWIND_INFO  ia64_debug_unwind_info
587 #undef TARGET_EXCEPT_UNWIND_INFO
588 #define TARGET_EXCEPT_UNWIND_INFO  ia64_except_unwind_info
589
590 #undef TARGET_SCALAR_MODE_SUPPORTED_P
591 #define TARGET_SCALAR_MODE_SUPPORTED_P ia64_scalar_mode_supported_p
592 #undef TARGET_VECTOR_MODE_SUPPORTED_P
593 #define TARGET_VECTOR_MODE_SUPPORTED_P ia64_vector_mode_supported_p
594
595 /* ia64 architecture manual 4.4.7: ... reads, writes, and flushes may occur
596    in an order different from the specified program order.  */
597 #undef TARGET_RELAXED_ORDERING
598 #define TARGET_RELAXED_ORDERING true
599
600 #undef TARGET_DEFAULT_TARGET_FLAGS
601 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | TARGET_CPU_DEFAULT)
602 #undef TARGET_HANDLE_OPTION
603 #define TARGET_HANDLE_OPTION ia64_handle_option
604
605 #undef TARGET_CANNOT_FORCE_CONST_MEM
606 #define TARGET_CANNOT_FORCE_CONST_MEM ia64_cannot_force_const_mem
607
608 #undef TARGET_MANGLE_TYPE
609 #define TARGET_MANGLE_TYPE ia64_mangle_type
610
611 #undef TARGET_INVALID_CONVERSION
612 #define TARGET_INVALID_CONVERSION ia64_invalid_conversion
613 #undef TARGET_INVALID_UNARY_OP
614 #define TARGET_INVALID_UNARY_OP ia64_invalid_unary_op
615 #undef TARGET_INVALID_BINARY_OP
616 #define TARGET_INVALID_BINARY_OP ia64_invalid_binary_op
617
618 #undef TARGET_C_MODE_FOR_SUFFIX
619 #define TARGET_C_MODE_FOR_SUFFIX ia64_c_mode_for_suffix
620
621 #undef TARGET_CAN_ELIMINATE
622 #define TARGET_CAN_ELIMINATE ia64_can_eliminate
623
624 #undef TARGET_TRAMPOLINE_INIT
625 #define TARGET_TRAMPOLINE_INIT ia64_trampoline_init
626
627 #undef TARGET_INVALID_WITHIN_DOLOOP
628 #define TARGET_INVALID_WITHIN_DOLOOP hook_constcharptr_const_rtx_null
629
630 #undef TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE
631 #define TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE ia64_override_options_after_change
632
633 #undef TARGET_PREFERRED_RELOAD_CLASS
634 #define TARGET_PREFERRED_RELOAD_CLASS ia64_preferred_reload_class
635
636 struct gcc_target targetm = TARGET_INITIALIZER;
637 \f
638 typedef enum
639   {
640     ADDR_AREA_NORMAL,   /* normal address area */
641     ADDR_AREA_SMALL     /* addressable by "addl" (-2MB < addr < 2MB) */
642   }
643 ia64_addr_area;
644
645 static GTY(()) tree small_ident1;
646 static GTY(()) tree small_ident2;
647
648 static void
649 init_idents (void)
650 {
651   if (small_ident1 == 0)
652     {
653       small_ident1 = get_identifier ("small");
654       small_ident2 = get_identifier ("__small__");
655     }
656 }
657
658 /* Retrieve the address area that has been chosen for the given decl.  */
659
660 static ia64_addr_area
661 ia64_get_addr_area (tree decl)
662 {
663   tree model_attr;
664
665   model_attr = lookup_attribute ("model", DECL_ATTRIBUTES (decl));
666   if (model_attr)
667     {
668       tree id;
669
670       init_idents ();
671       id = TREE_VALUE (TREE_VALUE (model_attr));
672       if (id == small_ident1 || id == small_ident2)
673         return ADDR_AREA_SMALL;
674     }
675   return ADDR_AREA_NORMAL;
676 }
677
678 static tree
679 ia64_handle_model_attribute (tree *node, tree name, tree args,
680                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
681 {
682   ia64_addr_area addr_area = ADDR_AREA_NORMAL;
683   ia64_addr_area area;
684   tree arg, decl = *node;
685
686   init_idents ();
687   arg = TREE_VALUE (args);
688   if (arg == small_ident1 || arg == small_ident2)
689     {
690       addr_area = ADDR_AREA_SMALL;
691     }
692   else
693     {
694       warning (OPT_Wattributes, "invalid argument of %qE attribute",
695                name);
696       *no_add_attrs = true;
697     }
698
699   switch (TREE_CODE (decl))
700     {
701     case VAR_DECL:
702       if ((DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl))
703            == FUNCTION_DECL)
704           && !TREE_STATIC (decl))
705         {
706           error_at (DECL_SOURCE_LOCATION (decl),
707                     "an address area attribute cannot be specified for "
708                     "local variables");
709           *no_add_attrs = true;
710         }
711       area = ia64_get_addr_area (decl);
712       if (area != ADDR_AREA_NORMAL && addr_area != area)
713         {
714           error ("address area of %q+D conflicts with previous "
715                  "declaration", decl);
716           *no_add_attrs = true;
717         }
718       break;
719
720     case FUNCTION_DECL:
721       error_at (DECL_SOURCE_LOCATION (decl),
722                 "address area attribute cannot be specified for "
723                 "functions");
724       *no_add_attrs = true;
725       break;
726
727     default:
728       warning (OPT_Wattributes, "%qE attribute ignored",
729                name);
730       *no_add_attrs = true;
731       break;
732     }
733
734   return NULL_TREE;
735 }
736
737 /* The section must have global and overlaid attributes.  */
738 #define SECTION_VMS_OVERLAY SECTION_MACH_DEP
739
740 /* Part of the low level implementation of DEC Ada pragma Common_Object which
741    enables the shared use of variables stored in overlaid linker areas
742    corresponding to the use of Fortran COMMON.  */
743
744 static tree
745 ia64_vms_common_object_attribute (tree *node, tree name, tree args,
746                                   int flags ATTRIBUTE_UNUSED,
747                                   bool *no_add_attrs)
748 {
749     tree decl = *node;
750     tree id, val;
751     if (! DECL_P (decl))
752       abort ();
753   
754     DECL_COMMON (decl) = 1;
755     id = TREE_VALUE (args);
756     if (TREE_CODE (id) == IDENTIFIER_NODE)
757       val = build_string (IDENTIFIER_LENGTH (id), IDENTIFIER_POINTER (id));
758     else if (TREE_CODE (id) == STRING_CST)
759       val = id;
760     else
761       {
762         warning (OPT_Wattributes,
763                  "%qE attribute requires a string constant argument", name);
764         *no_add_attrs = true;
765         return NULL_TREE;
766       }
767     DECL_SECTION_NAME (decl) = val;
768     return NULL_TREE;
769 }
770
771 /* Part of the low level implementation of DEC Ada pragma Common_Object.  */
772
773 void
774 ia64_vms_output_aligned_decl_common (FILE *file, tree decl, const char *name,
775                                      unsigned HOST_WIDE_INT size,
776                                      unsigned int align)
777 {
778   tree attr = DECL_ATTRIBUTES (decl);
779
780   /* As common_object attribute set DECL_SECTION_NAME check it before
781      looking up the attribute.  */
782   if (DECL_SECTION_NAME (decl) && attr)
783     attr = lookup_attribute ("common_object", attr);
784   else
785     attr = NULL_TREE;
786
787   if (!attr)
788     {
789       /*  Code from elfos.h.  */
790       fprintf (file, "%s", COMMON_ASM_OP);
791       assemble_name (file, name);
792       fprintf (file, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
793                size, align / BITS_PER_UNIT);
794     }
795   else
796     {
797       ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
798       ASM_OUTPUT_LABEL (file, name);
799       ASM_OUTPUT_SKIP (file, size ? size : 1);
800     }
801 }
802
803 /* Definition of TARGET_ASM_NAMED_SECTION for VMS.  */
804
805 void
806 ia64_vms_elf_asm_named_section (const char *name, unsigned int flags,
807                                 tree decl)
808 {
809   if (!(flags & SECTION_VMS_OVERLAY))
810     {
811       default_elf_asm_named_section (name, flags, decl);
812       return;
813     }
814   if (flags != (SECTION_VMS_OVERLAY | SECTION_WRITE))
815     abort ();
816
817   if (flags & SECTION_DECLARED)
818     {
819       fprintf (asm_out_file, "\t.section\t%s\n", name);
820       return;
821     }
822
823   fprintf (asm_out_file, "\t.section\t%s,\"awgO\"\n", name);
824 }
825
826 static void
827 ia64_encode_addr_area (tree decl, rtx symbol)
828 {
829   int flags;
830
831   flags = SYMBOL_REF_FLAGS (symbol);
832   switch (ia64_get_addr_area (decl))
833     {
834     case ADDR_AREA_NORMAL: break;
835     case ADDR_AREA_SMALL: flags |= SYMBOL_FLAG_SMALL_ADDR; break;
836     default: gcc_unreachable ();
837     }
838   SYMBOL_REF_FLAGS (symbol) = flags;
839 }
840
841 static void
842 ia64_encode_section_info (tree decl, rtx rtl, int first)
843 {
844   default_encode_section_info (decl, rtl, first);
845
846   /* Careful not to prod global register variables.  */
847   if (TREE_CODE (decl) == VAR_DECL
848       && GET_CODE (DECL_RTL (decl)) == MEM
849       && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF
850       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
851     ia64_encode_addr_area (decl, XEXP (rtl, 0));
852 }
853 \f
854 /* Return 1 if the operands of a move are ok.  */
855
856 int
857 ia64_move_ok (rtx dst, rtx src)
858 {
859   /* If we're under init_recog_no_volatile, we'll not be able to use
860      memory_operand.  So check the code directly and don't worry about
861      the validity of the underlying address, which should have been
862      checked elsewhere anyway.  */
863   if (GET_CODE (dst) != MEM)
864     return 1;
865   if (GET_CODE (src) == MEM)
866     return 0;
867   if (register_operand (src, VOIDmode))
868     return 1;
869
870   /* Otherwise, this must be a constant, and that either 0 or 0.0 or 1.0.  */
871   if (INTEGRAL_MODE_P (GET_MODE (dst)))
872     return src == const0_rtx;
873   else
874     return satisfies_constraint_G (src);
875 }
876
877 /* Return 1 if the operands are ok for a floating point load pair.  */
878
879 int
880 ia64_load_pair_ok (rtx dst, rtx src)
881 {
882   if (GET_CODE (dst) != REG || !FP_REGNO_P (REGNO (dst)))
883     return 0;
884   if (GET_CODE (src) != MEM || MEM_VOLATILE_P (src))
885     return 0;
886   switch (GET_CODE (XEXP (src, 0)))
887     {
888     case REG:
889     case POST_INC:
890       break;
891     case POST_DEC:
892       return 0;
893     case POST_MODIFY:
894       {
895         rtx adjust = XEXP (XEXP (XEXP (src, 0), 1), 1);
896
897         if (GET_CODE (adjust) != CONST_INT
898             || INTVAL (adjust) != GET_MODE_SIZE (GET_MODE (src)))
899           return 0;
900       }
901       break;
902     default:
903       abort ();
904     }
905   return 1;
906 }
907
908 int
909 addp4_optimize_ok (rtx op1, rtx op2)
910 {
911   return (basereg_operand (op1, GET_MODE(op1)) !=
912           basereg_operand (op2, GET_MODE(op2)));
913 }
914
915 /* Check if OP is a mask suitable for use with SHIFT in a dep.z instruction.
916    Return the length of the field, or <= 0 on failure.  */
917
918 int
919 ia64_depz_field_mask (rtx rop, rtx rshift)
920 {
921   unsigned HOST_WIDE_INT op = INTVAL (rop);
922   unsigned HOST_WIDE_INT shift = INTVAL (rshift);
923
924   /* Get rid of the zero bits we're shifting in.  */
925   op >>= shift;
926
927   /* We must now have a solid block of 1's at bit 0.  */
928   return exact_log2 (op + 1);
929 }
930
931 /* Return the TLS model to use for ADDR.  */
932
933 static enum tls_model
934 tls_symbolic_operand_type (rtx addr)
935 {
936   enum tls_model tls_kind = TLS_MODEL_NONE;
937
938   if (GET_CODE (addr) == CONST)
939     {
940       if (GET_CODE (XEXP (addr, 0)) == PLUS
941           && GET_CODE (XEXP (XEXP (addr, 0), 0)) == SYMBOL_REF)
942         tls_kind = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (addr, 0), 0));
943     }
944   else if (GET_CODE (addr) == SYMBOL_REF)
945     tls_kind = SYMBOL_REF_TLS_MODEL (addr);
946
947   return tls_kind;
948 }
949
950 /* Return true if X is a constant that is valid for some immediate
951    field in an instruction.  */
952
953 bool
954 ia64_legitimate_constant_p (rtx x)
955 {
956   switch (GET_CODE (x))
957     {
958     case CONST_INT:
959     case LABEL_REF:
960       return true;
961
962     case CONST_DOUBLE:
963       if (GET_MODE (x) == VOIDmode || GET_MODE (x) == SFmode
964           || GET_MODE (x) == DFmode)
965         return true;
966       return satisfies_constraint_G (x);
967
968     case CONST:
969     case SYMBOL_REF:
970       /* ??? Short term workaround for PR 28490.  We must make the code here
971          match the code in ia64_expand_move and move_operand, even though they
972          are both technically wrong.  */
973       if (tls_symbolic_operand_type (x) == 0)
974         {
975           HOST_WIDE_INT addend = 0;
976           rtx op = x;
977
978           if (GET_CODE (op) == CONST
979               && GET_CODE (XEXP (op, 0)) == PLUS
980               && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT)
981             {
982               addend = INTVAL (XEXP (XEXP (op, 0), 1));
983               op = XEXP (XEXP (op, 0), 0);
984             }
985
986           if (any_offset_symbol_operand (op, GET_MODE (op))
987               || function_operand (op, GET_MODE (op)))
988             return true;
989           if (aligned_offset_symbol_operand (op, GET_MODE (op)))
990             return (addend & 0x3fff) == 0;
991           return false;
992         }
993       return false;
994
995     case CONST_VECTOR:
996       {
997         enum machine_mode mode = GET_MODE (x);
998
999         if (mode == V2SFmode)
1000           return satisfies_constraint_Y (x);
1001
1002         return (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
1003                 && GET_MODE_SIZE (mode) <= 8);
1004       }
1005
1006     default:
1007       return false;
1008     }
1009 }
1010
1011 /* Don't allow TLS addresses to get spilled to memory.  */
1012
1013 static bool
1014 ia64_cannot_force_const_mem (rtx x)
1015 {
1016   if (GET_MODE (x) == RFmode)
1017     return true;
1018   return tls_symbolic_operand_type (x) != 0;
1019 }
1020
1021 /* Expand a symbolic constant load.  */
1022
1023 bool
1024 ia64_expand_load_address (rtx dest, rtx src)
1025 {
1026   gcc_assert (GET_CODE (dest) == REG);
1027
1028   /* ILP32 mode still loads 64-bits of data from the GOT.  This avoids
1029      having to pointer-extend the value afterward.  Other forms of address
1030      computation below are also more natural to compute as 64-bit quantities.
1031      If we've been given an SImode destination register, change it.  */
1032   if (GET_MODE (dest) != Pmode)
1033     dest = gen_rtx_REG_offset (dest, Pmode, REGNO (dest),
1034                                byte_lowpart_offset (Pmode, GET_MODE (dest)));
1035
1036   if (TARGET_NO_PIC)
1037     return false;
1038   if (small_addr_symbolic_operand (src, VOIDmode))
1039     return false;
1040
1041   if (TARGET_AUTO_PIC)
1042     emit_insn (gen_load_gprel64 (dest, src));
1043   else if (GET_CODE (src) == SYMBOL_REF && SYMBOL_REF_FUNCTION_P (src))
1044     emit_insn (gen_load_fptr (dest, src));
1045   else if (sdata_symbolic_operand (src, VOIDmode))
1046     emit_insn (gen_load_gprel (dest, src));
1047   else
1048     {
1049       HOST_WIDE_INT addend = 0;
1050       rtx tmp;
1051
1052       /* We did split constant offsets in ia64_expand_move, and we did try
1053          to keep them split in move_operand, but we also allowed reload to
1054          rematerialize arbitrary constants rather than spill the value to
1055          the stack and reload it.  So we have to be prepared here to split
1056          them apart again.  */
1057       if (GET_CODE (src) == CONST)
1058         {
1059           HOST_WIDE_INT hi, lo;
1060
1061           hi = INTVAL (XEXP (XEXP (src, 0), 1));
1062           lo = ((hi & 0x3fff) ^ 0x2000) - 0x2000;
1063           hi = hi - lo;
1064
1065           if (lo != 0)
1066             {
1067               addend = lo;
1068               src = plus_constant (XEXP (XEXP (src, 0), 0), hi);
1069             }
1070         }
1071
1072       tmp = gen_rtx_HIGH (Pmode, src);
1073       tmp = gen_rtx_PLUS (Pmode, tmp, pic_offset_table_rtx);
1074       emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
1075
1076       tmp = gen_rtx_LO_SUM (Pmode, dest, src);
1077       emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
1078
1079       if (addend)
1080         {
1081           tmp = gen_rtx_PLUS (Pmode, dest, GEN_INT (addend));
1082           emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
1083         }
1084     }
1085
1086   return true;
1087 }
1088
1089 static GTY(()) rtx gen_tls_tga;
1090 static rtx
1091 gen_tls_get_addr (void)
1092 {
1093   if (!gen_tls_tga)
1094     gen_tls_tga = init_one_libfunc ("__tls_get_addr");
1095   return gen_tls_tga;
1096 }
1097
1098 static GTY(()) rtx thread_pointer_rtx;
1099 static rtx
1100 gen_thread_pointer (void)
1101 {
1102   if (!thread_pointer_rtx)
1103     thread_pointer_rtx = gen_rtx_REG (Pmode, 13);
1104   return thread_pointer_rtx;
1105 }
1106
1107 static rtx
1108 ia64_expand_tls_address (enum tls_model tls_kind, rtx op0, rtx op1,
1109                          rtx orig_op1, HOST_WIDE_INT addend)
1110 {
1111   rtx tga_op1, tga_op2, tga_ret, tga_eqv, tmp, insns;
1112   rtx orig_op0 = op0;
1113   HOST_WIDE_INT addend_lo, addend_hi;
1114
1115   switch (tls_kind)
1116     {
1117     case TLS_MODEL_GLOBAL_DYNAMIC:
1118       start_sequence ();
1119
1120       tga_op1 = gen_reg_rtx (Pmode);
1121       emit_insn (gen_load_dtpmod (tga_op1, op1));
1122
1123       tga_op2 = gen_reg_rtx (Pmode);
1124       emit_insn (gen_load_dtprel (tga_op2, op1));
1125
1126       tga_ret = emit_library_call_value (gen_tls_get_addr (), NULL_RTX,
1127                                          LCT_CONST, Pmode, 2, tga_op1,
1128                                          Pmode, tga_op2, Pmode);
1129
1130       insns = get_insns ();
1131       end_sequence ();
1132
1133       if (GET_MODE (op0) != Pmode)
1134         op0 = tga_ret;
1135       emit_libcall_block (insns, op0, tga_ret, op1);
1136       break;
1137
1138     case TLS_MODEL_LOCAL_DYNAMIC:
1139       /* ??? This isn't the completely proper way to do local-dynamic
1140          If the call to __tls_get_addr is used only by a single symbol,
1141          then we should (somehow) move the dtprel to the second arg
1142          to avoid the extra add.  */
1143       start_sequence ();
1144
1145       tga_op1 = gen_reg_rtx (Pmode);
1146       emit_insn (gen_load_dtpmod (tga_op1, op1));
1147
1148       tga_op2 = const0_rtx;
1149
1150       tga_ret = emit_library_call_value (gen_tls_get_addr (), NULL_RTX,
1151                                          LCT_CONST, Pmode, 2, tga_op1,
1152                                          Pmode, tga_op2, Pmode);
1153
1154       insns = get_insns ();
1155       end_sequence ();
1156
1157       tga_eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
1158                                 UNSPEC_LD_BASE);
1159       tmp = gen_reg_rtx (Pmode);
1160       emit_libcall_block (insns, tmp, tga_ret, tga_eqv);
1161
1162       if (!register_operand (op0, Pmode))
1163         op0 = gen_reg_rtx (Pmode);
1164       if (TARGET_TLS64)
1165         {
1166           emit_insn (gen_load_dtprel (op0, op1));
1167           emit_insn (gen_adddi3 (op0, tmp, op0));
1168         }
1169       else
1170         emit_insn (gen_add_dtprel (op0, op1, tmp));
1171       break;
1172
1173     case TLS_MODEL_INITIAL_EXEC:
1174       addend_lo = ((addend & 0x3fff) ^ 0x2000) - 0x2000;
1175       addend_hi = addend - addend_lo;
1176
1177       op1 = plus_constant (op1, addend_hi);
1178       addend = addend_lo;
1179
1180       tmp = gen_reg_rtx (Pmode);
1181       emit_insn (gen_load_tprel (tmp, op1));
1182
1183       if (!register_operand (op0, Pmode))
1184         op0 = gen_reg_rtx (Pmode);
1185       emit_insn (gen_adddi3 (op0, tmp, gen_thread_pointer ()));
1186       break;
1187
1188     case TLS_MODEL_LOCAL_EXEC:
1189       if (!register_operand (op0, Pmode))
1190         op0 = gen_reg_rtx (Pmode);
1191
1192       op1 = orig_op1;
1193       addend = 0;
1194       if (TARGET_TLS64)
1195         {
1196           emit_insn (gen_load_tprel (op0, op1));
1197           emit_insn (gen_adddi3 (op0, op0, gen_thread_pointer ()));
1198         }
1199       else
1200         emit_insn (gen_add_tprel (op0, op1, gen_thread_pointer ()));
1201       break;
1202
1203     default:
1204       gcc_unreachable ();
1205     }
1206
1207   if (addend)
1208     op0 = expand_simple_binop (Pmode, PLUS, op0, GEN_INT (addend),
1209                                orig_op0, 1, OPTAB_DIRECT);
1210   if (orig_op0 == op0)
1211     return NULL_RTX;
1212   if (GET_MODE (orig_op0) == Pmode)
1213     return op0;
1214   return gen_lowpart (GET_MODE (orig_op0), op0);
1215 }
1216
1217 rtx
1218 ia64_expand_move (rtx op0, rtx op1)
1219 {
1220   enum machine_mode mode = GET_MODE (op0);
1221
1222   if (!reload_in_progress && !reload_completed && !ia64_move_ok (op0, op1))
1223     op1 = force_reg (mode, op1);
1224
1225   if ((mode == Pmode || mode == ptr_mode) && symbolic_operand (op1, VOIDmode))
1226     {
1227       HOST_WIDE_INT addend = 0;
1228       enum tls_model tls_kind;
1229       rtx sym = op1;
1230
1231       if (GET_CODE (op1) == CONST
1232           && GET_CODE (XEXP (op1, 0)) == PLUS
1233           && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT)
1234         {
1235           addend = INTVAL (XEXP (XEXP (op1, 0), 1));
1236           sym = XEXP (XEXP (op1, 0), 0);
1237         }
1238
1239       tls_kind = tls_symbolic_operand_type (sym);
1240       if (tls_kind)
1241         return ia64_expand_tls_address (tls_kind, op0, sym, op1, addend);
1242
1243       if (any_offset_symbol_operand (sym, mode))
1244         addend = 0;
1245       else if (aligned_offset_symbol_operand (sym, mode))
1246         {
1247           HOST_WIDE_INT addend_lo, addend_hi;
1248               
1249           addend_lo = ((addend & 0x3fff) ^ 0x2000) - 0x2000;
1250           addend_hi = addend - addend_lo;
1251
1252           if (addend_lo != 0)
1253             {
1254               op1 = plus_constant (sym, addend_hi);
1255               addend = addend_lo;
1256             }
1257           else
1258             addend = 0;
1259         }
1260       else
1261         op1 = sym;
1262
1263       if (reload_completed)
1264         {
1265           /* We really should have taken care of this offset earlier.  */
1266           gcc_assert (addend == 0);
1267           if (ia64_expand_load_address (op0, op1))
1268             return NULL_RTX;
1269         }
1270
1271       if (addend)
1272         {
1273           rtx subtarget = !can_create_pseudo_p () ? op0 : gen_reg_rtx (mode);
1274
1275           emit_insn (gen_rtx_SET (VOIDmode, subtarget, op1));
1276
1277           op1 = expand_simple_binop (mode, PLUS, subtarget,
1278                                      GEN_INT (addend), op0, 1, OPTAB_DIRECT);
1279           if (op0 == op1)
1280             return NULL_RTX;
1281         }
1282     }
1283
1284   return op1;
1285 }
1286
1287 /* Split a move from OP1 to OP0 conditional on COND.  */
1288
1289 void
1290 ia64_emit_cond_move (rtx op0, rtx op1, rtx cond)
1291 {
1292   rtx insn, first = get_last_insn ();
1293
1294   emit_move_insn (op0, op1);
1295
1296   for (insn = get_last_insn (); insn != first; insn = PREV_INSN (insn))
1297     if (INSN_P (insn))
1298       PATTERN (insn) = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond),
1299                                           PATTERN (insn));
1300 }
1301
1302 /* Split a post-reload TImode or TFmode reference into two DImode
1303    components.  This is made extra difficult by the fact that we do
1304    not get any scratch registers to work with, because reload cannot
1305    be prevented from giving us a scratch that overlaps the register
1306    pair involved.  So instead, when addressing memory, we tweak the
1307    pointer register up and back down with POST_INCs.  Or up and not
1308    back down when we can get away with it.
1309
1310    REVERSED is true when the loads must be done in reversed order
1311    (high word first) for correctness.  DEAD is true when the pointer
1312    dies with the second insn we generate and therefore the second
1313    address must not carry a postmodify.
1314
1315    May return an insn which is to be emitted after the moves.  */
1316
1317 static rtx
1318 ia64_split_tmode (rtx out[2], rtx in, bool reversed, bool dead)
1319 {
1320   rtx fixup = 0;
1321
1322   switch (GET_CODE (in))
1323     {
1324     case REG:
1325       out[reversed] = gen_rtx_REG (DImode, REGNO (in));
1326       out[!reversed] = gen_rtx_REG (DImode, REGNO (in) + 1);
1327       break;
1328
1329     case CONST_INT:
1330     case CONST_DOUBLE:
1331       /* Cannot occur reversed.  */
1332       gcc_assert (!reversed);
1333       
1334       if (GET_MODE (in) != TFmode)
1335         split_double (in, &out[0], &out[1]);
1336       else
1337         /* split_double does not understand how to split a TFmode
1338            quantity into a pair of DImode constants.  */
1339         {
1340           REAL_VALUE_TYPE r;
1341           unsigned HOST_WIDE_INT p[2];
1342           long l[4];  /* TFmode is 128 bits */
1343
1344           REAL_VALUE_FROM_CONST_DOUBLE (r, in);
1345           real_to_target (l, &r, TFmode);
1346
1347           if (FLOAT_WORDS_BIG_ENDIAN)
1348             {
1349               p[0] = (((unsigned HOST_WIDE_INT) l[0]) << 32) + l[1];
1350               p[1] = (((unsigned HOST_WIDE_INT) l[2]) << 32) + l[3];
1351             }
1352           else
1353             {
1354               p[0] = (((unsigned HOST_WIDE_INT) l[1]) << 32) + l[0];
1355               p[1] = (((unsigned HOST_WIDE_INT) l[3]) << 32) + l[2];
1356             }
1357           out[0] = GEN_INT (p[0]);
1358           out[1] = GEN_INT (p[1]);
1359         }
1360       break;
1361
1362     case MEM:
1363       {
1364         rtx base = XEXP (in, 0);
1365         rtx offset;
1366
1367         switch (GET_CODE (base))
1368           {
1369           case REG:
1370             if (!reversed)
1371               {
1372                 out[0] = adjust_automodify_address
1373                   (in, DImode, gen_rtx_POST_INC (Pmode, base), 0);
1374                 out[1] = adjust_automodify_address
1375                   (in, DImode, dead ? 0 : gen_rtx_POST_DEC (Pmode, base), 8);
1376               }
1377             else
1378               {
1379                 /* Reversal requires a pre-increment, which can only
1380                    be done as a separate insn.  */
1381                 emit_insn (gen_adddi3 (base, base, GEN_INT (8)));
1382                 out[0] = adjust_automodify_address
1383                   (in, DImode, gen_rtx_POST_DEC (Pmode, base), 8);
1384                 out[1] = adjust_address (in, DImode, 0);
1385               }
1386             break;
1387
1388           case POST_INC:
1389             gcc_assert (!reversed && !dead);
1390             
1391             /* Just do the increment in two steps.  */
1392             out[0] = adjust_automodify_address (in, DImode, 0, 0);
1393             out[1] = adjust_automodify_address (in, DImode, 0, 8);
1394             break;
1395
1396           case POST_DEC:
1397             gcc_assert (!reversed && !dead);
1398             
1399             /* Add 8, subtract 24.  */
1400             base = XEXP (base, 0);
1401             out[0] = adjust_automodify_address
1402               (in, DImode, gen_rtx_POST_INC (Pmode, base), 0);
1403             out[1] = adjust_automodify_address
1404               (in, DImode,
1405                gen_rtx_POST_MODIFY (Pmode, base, plus_constant (base, -24)),
1406                8);
1407             break;
1408
1409           case POST_MODIFY:
1410             gcc_assert (!reversed && !dead);
1411
1412             /* Extract and adjust the modification.  This case is
1413                trickier than the others, because we might have an
1414                index register, or we might have a combined offset that
1415                doesn't fit a signed 9-bit displacement field.  We can
1416                assume the incoming expression is already legitimate.  */
1417             offset = XEXP (base, 1);
1418             base = XEXP (base, 0);
1419
1420             out[0] = adjust_automodify_address
1421               (in, DImode, gen_rtx_POST_INC (Pmode, base), 0);
1422
1423             if (GET_CODE (XEXP (offset, 1)) == REG)
1424               {
1425                 /* Can't adjust the postmodify to match.  Emit the
1426                    original, then a separate addition insn.  */
1427                 out[1] = adjust_automodify_address (in, DImode, 0, 8);
1428                 fixup = gen_adddi3 (base, base, GEN_INT (-8));
1429               }
1430             else
1431               {
1432                 gcc_assert (GET_CODE (XEXP (offset, 1)) == CONST_INT);
1433                 if (INTVAL (XEXP (offset, 1)) < -256 + 8)
1434                   {
1435                     /* Again the postmodify cannot be made to match,
1436                        but in this case it's more efficient to get rid
1437                        of the postmodify entirely and fix up with an
1438                        add insn.  */
1439                     out[1] = adjust_automodify_address (in, DImode, base, 8);
1440                     fixup = gen_adddi3
1441                       (base, base, GEN_INT (INTVAL (XEXP (offset, 1)) - 8));
1442                   }
1443                 else
1444                   {
1445                     /* Combined offset still fits in the displacement field.
1446                        (We cannot overflow it at the high end.)  */
1447                     out[1] = adjust_automodify_address
1448                       (in, DImode, gen_rtx_POST_MODIFY
1449                        (Pmode, base, gen_rtx_PLUS
1450                         (Pmode, base,
1451                          GEN_INT (INTVAL (XEXP (offset, 1)) - 8))),
1452                        8);
1453                   }
1454               }
1455             break;
1456
1457           default:
1458             gcc_unreachable ();
1459           }
1460         break;
1461       }
1462
1463     default:
1464       gcc_unreachable ();
1465     }
1466
1467   return fixup;
1468 }
1469
1470 /* Split a TImode or TFmode move instruction after reload.
1471    This is used by *movtf_internal and *movti_internal.  */
1472 void
1473 ia64_split_tmode_move (rtx operands[])
1474 {
1475   rtx in[2], out[2], insn;
1476   rtx fixup[2];
1477   bool dead = false;
1478   bool reversed = false;
1479
1480   /* It is possible for reload to decide to overwrite a pointer with
1481      the value it points to.  In that case we have to do the loads in
1482      the appropriate order so that the pointer is not destroyed too
1483      early.  Also we must not generate a postmodify for that second
1484      load, or rws_access_regno will die.  */
1485   if (GET_CODE (operands[1]) == MEM
1486       && reg_overlap_mentioned_p (operands[0], operands[1]))
1487     {
1488       rtx base = XEXP (operands[1], 0);
1489       while (GET_CODE (base) != REG)
1490         base = XEXP (base, 0);
1491
1492       if (REGNO (base) == REGNO (operands[0]))
1493         reversed = true;
1494       dead = true;
1495     }
1496   /* Another reason to do the moves in reversed order is if the first
1497      element of the target register pair is also the second element of
1498      the source register pair.  */
1499   if (GET_CODE (operands[0]) == REG && GET_CODE (operands[1]) == REG
1500       && REGNO (operands[0]) == REGNO (operands[1]) + 1)
1501     reversed = true;
1502
1503   fixup[0] = ia64_split_tmode (in, operands[1], reversed, dead);
1504   fixup[1] = ia64_split_tmode (out, operands[0], reversed, dead);
1505
1506 #define MAYBE_ADD_REG_INC_NOTE(INSN, EXP)                               \
1507   if (GET_CODE (EXP) == MEM                                             \
1508       && (GET_CODE (XEXP (EXP, 0)) == POST_MODIFY                       \
1509           || GET_CODE (XEXP (EXP, 0)) == POST_INC                       \
1510           || GET_CODE (XEXP (EXP, 0)) == POST_DEC))                     \
1511     add_reg_note (insn, REG_INC, XEXP (XEXP (EXP, 0), 0))
1512
1513   insn = emit_insn (gen_rtx_SET (VOIDmode, out[0], in[0]));
1514   MAYBE_ADD_REG_INC_NOTE (insn, in[0]);
1515   MAYBE_ADD_REG_INC_NOTE (insn, out[0]);
1516
1517   insn = emit_insn (gen_rtx_SET (VOIDmode, out[1], in[1]));
1518   MAYBE_ADD_REG_INC_NOTE (insn, in[1]);
1519   MAYBE_ADD_REG_INC_NOTE (insn, out[1]);
1520
1521   if (fixup[0])
1522     emit_insn (fixup[0]);
1523   if (fixup[1])
1524     emit_insn (fixup[1]);
1525
1526 #undef MAYBE_ADD_REG_INC_NOTE
1527 }
1528
1529 /* ??? Fixing GR->FR XFmode moves during reload is hard.  You need to go
1530    through memory plus an extra GR scratch register.  Except that you can
1531    either get the first from SECONDARY_MEMORY_NEEDED or the second from
1532    SECONDARY_RELOAD_CLASS, but not both.
1533
1534    We got into problems in the first place by allowing a construct like
1535    (subreg:XF (reg:TI)), which we got from a union containing a long double.
1536    This solution attempts to prevent this situation from occurring.  When
1537    we see something like the above, we spill the inner register to memory.  */
1538
1539 static rtx
1540 spill_xfmode_rfmode_operand (rtx in, int force, enum machine_mode mode)
1541 {
1542   if (GET_CODE (in) == SUBREG
1543       && GET_MODE (SUBREG_REG (in)) == TImode
1544       && GET_CODE (SUBREG_REG (in)) == REG)
1545     {
1546       rtx memt = assign_stack_temp (TImode, 16, 0);
1547       emit_move_insn (memt, SUBREG_REG (in));
1548       return adjust_address (memt, mode, 0);
1549     }
1550   else if (force && GET_CODE (in) == REG)
1551     {
1552       rtx memx = assign_stack_temp (mode, 16, 0);
1553       emit_move_insn (memx, in);
1554       return memx;
1555     }
1556   else
1557     return in;
1558 }
1559
1560 /* Expand the movxf or movrf pattern (MODE says which) with the given
1561    OPERANDS, returning true if the pattern should then invoke
1562    DONE.  */
1563
1564 bool
1565 ia64_expand_movxf_movrf (enum machine_mode mode, rtx operands[])
1566 {
1567   rtx op0 = operands[0];
1568
1569   if (GET_CODE (op0) == SUBREG)
1570     op0 = SUBREG_REG (op0);
1571
1572   /* We must support XFmode loads into general registers for stdarg/vararg,
1573      unprototyped calls, and a rare case where a long double is passed as
1574      an argument after a float HFA fills the FP registers.  We split them into
1575      DImode loads for convenience.  We also need to support XFmode stores
1576      for the last case.  This case does not happen for stdarg/vararg routines,
1577      because we do a block store to memory of unnamed arguments.  */
1578
1579   if (GET_CODE (op0) == REG && GR_REGNO_P (REGNO (op0)))
1580     {
1581       rtx out[2];
1582
1583       /* We're hoping to transform everything that deals with XFmode
1584          quantities and GR registers early in the compiler.  */
1585       gcc_assert (can_create_pseudo_p ());
1586
1587       /* Struct to register can just use TImode instead.  */
1588       if ((GET_CODE (operands[1]) == SUBREG
1589            && GET_MODE (SUBREG_REG (operands[1])) == TImode)
1590           || (GET_CODE (operands[1]) == REG
1591               && GR_REGNO_P (REGNO (operands[1]))))
1592         {
1593           rtx op1 = operands[1];
1594
1595           if (GET_CODE (op1) == SUBREG)
1596             op1 = SUBREG_REG (op1);
1597           else
1598             op1 = gen_rtx_REG (TImode, REGNO (op1));
1599
1600           emit_move_insn (gen_rtx_REG (TImode, REGNO (op0)), op1);
1601           return true;
1602         }
1603
1604       if (GET_CODE (operands[1]) == CONST_DOUBLE)
1605         {
1606           /* Don't word-swap when reading in the constant.  */
1607           emit_move_insn (gen_rtx_REG (DImode, REGNO (op0)),
1608                           operand_subword (operands[1], WORDS_BIG_ENDIAN,
1609                                            0, mode));
1610           emit_move_insn (gen_rtx_REG (DImode, REGNO (op0) + 1),
1611                           operand_subword (operands[1], !WORDS_BIG_ENDIAN,
1612                                            0, mode));
1613           return true;
1614         }
1615
1616       /* If the quantity is in a register not known to be GR, spill it.  */
1617       if (register_operand (operands[1], mode))
1618         operands[1] = spill_xfmode_rfmode_operand (operands[1], 1, mode);
1619
1620       gcc_assert (GET_CODE (operands[1]) == MEM);
1621
1622       /* Don't word-swap when reading in the value.  */
1623       out[0] = gen_rtx_REG (DImode, REGNO (op0));
1624       out[1] = gen_rtx_REG (DImode, REGNO (op0) + 1);
1625
1626       emit_move_insn (out[0], adjust_address (operands[1], DImode, 0));
1627       emit_move_insn (out[1], adjust_address (operands[1], DImode, 8));
1628       return true;
1629     }
1630
1631   if (GET_CODE (operands[1]) == REG && GR_REGNO_P (REGNO (operands[1])))
1632     {
1633       /* We're hoping to transform everything that deals with XFmode
1634          quantities and GR registers early in the compiler.  */
1635       gcc_assert (can_create_pseudo_p ());
1636
1637       /* Op0 can't be a GR_REG here, as that case is handled above.
1638          If op0 is a register, then we spill op1, so that we now have a
1639          MEM operand.  This requires creating an XFmode subreg of a TImode reg
1640          to force the spill.  */
1641       if (register_operand (operands[0], mode))
1642         {
1643           rtx op1 = gen_rtx_REG (TImode, REGNO (operands[1]));
1644           op1 = gen_rtx_SUBREG (mode, op1, 0);
1645           operands[1] = spill_xfmode_rfmode_operand (op1, 0, mode);
1646         }
1647
1648       else
1649         {
1650           rtx in[2];
1651
1652           gcc_assert (GET_CODE (operands[0]) == MEM);
1653
1654           /* Don't word-swap when writing out the value.  */
1655           in[0] = gen_rtx_REG (DImode, REGNO (operands[1]));
1656           in[1] = gen_rtx_REG (DImode, REGNO (operands[1]) + 1);
1657
1658           emit_move_insn (adjust_address (operands[0], DImode, 0), in[0]);
1659           emit_move_insn (adjust_address (operands[0], DImode, 8), in[1]);
1660           return true;
1661         }
1662     }
1663
1664   if (!reload_in_progress && !reload_completed)
1665     {
1666       operands[1] = spill_xfmode_rfmode_operand (operands[1], 0, mode);
1667
1668       if (GET_MODE (op0) == TImode && GET_CODE (op0) == REG)
1669         {
1670           rtx memt, memx, in = operands[1];
1671           if (CONSTANT_P (in))
1672             in = validize_mem (force_const_mem (mode, in));
1673           if (GET_CODE (in) == MEM)
1674             memt = adjust_address (in, TImode, 0);
1675           else
1676             {
1677               memt = assign_stack_temp (TImode, 16, 0);
1678               memx = adjust_address (memt, mode, 0);
1679               emit_move_insn (memx, in);
1680             }
1681           emit_move_insn (op0, memt);
1682           return true;
1683         }
1684
1685       if (!ia64_move_ok (operands[0], operands[1]))
1686         operands[1] = force_reg (mode, operands[1]);
1687     }
1688
1689   return false;
1690 }
1691
1692 /* Emit comparison instruction if necessary, replacing *EXPR, *OP0, *OP1
1693    with the expression that holds the compare result (in VOIDmode).  */
1694
1695 static GTY(()) rtx cmptf_libfunc;
1696
1697 void
1698 ia64_expand_compare (rtx *expr, rtx *op0, rtx *op1)
1699 {
1700   enum rtx_code code = GET_CODE (*expr);
1701   rtx cmp;
1702
1703   /* If we have a BImode input, then we already have a compare result, and
1704      do not need to emit another comparison.  */
1705   if (GET_MODE (*op0) == BImode)
1706     {
1707       gcc_assert ((code == NE || code == EQ) && *op1 == const0_rtx);
1708       cmp = *op0;
1709     }
1710   /* HPUX TFmode compare requires a library call to _U_Qfcmp, which takes a
1711      magic number as its third argument, that indicates what to do.
1712      The return value is an integer to be compared against zero.  */
1713   else if (TARGET_HPUX && GET_MODE (*op0) == TFmode)
1714     {
1715       enum qfcmp_magic {
1716         QCMP_INV = 1,   /* Raise FP_INVALID on SNaN as a side effect.  */
1717         QCMP_UNORD = 2,
1718         QCMP_EQ = 4,
1719         QCMP_LT = 8,
1720         QCMP_GT = 16
1721       };
1722       int magic;
1723       enum rtx_code ncode;
1724       rtx ret, insns;
1725       
1726       gcc_assert (cmptf_libfunc && GET_MODE (*op1) == TFmode);
1727       switch (code)
1728         {
1729           /* 1 = equal, 0 = not equal.  Equality operators do
1730              not raise FP_INVALID when given an SNaN operand.  */
1731         case EQ:        magic = QCMP_EQ;                  ncode = NE; break;
1732         case NE:        magic = QCMP_EQ;                  ncode = EQ; break;
1733           /* isunordered() from C99.  */
1734         case UNORDERED: magic = QCMP_UNORD;               ncode = NE; break;
1735         case ORDERED:   magic = QCMP_UNORD;               ncode = EQ; break;
1736           /* Relational operators raise FP_INVALID when given
1737              an SNaN operand.  */
1738         case LT:        magic = QCMP_LT        |QCMP_INV; ncode = NE; break;
1739         case LE:        magic = QCMP_LT|QCMP_EQ|QCMP_INV; ncode = NE; break;
1740         case GT:        magic = QCMP_GT        |QCMP_INV; ncode = NE; break;
1741         case GE:        magic = QCMP_GT|QCMP_EQ|QCMP_INV; ncode = NE; break;
1742           /* FUTURE: Implement UNEQ, UNLT, UNLE, UNGT, UNGE, LTGT.
1743              Expanders for buneq etc. weuld have to be added to ia64.md
1744              for this to be useful.  */
1745         default: gcc_unreachable ();
1746         }
1747
1748       start_sequence ();
1749
1750       ret = emit_library_call_value (cmptf_libfunc, 0, LCT_CONST, DImode, 3,
1751                                      *op0, TFmode, *op1, TFmode,
1752                                      GEN_INT (magic), DImode);
1753       cmp = gen_reg_rtx (BImode);
1754       emit_insn (gen_rtx_SET (VOIDmode, cmp,
1755                               gen_rtx_fmt_ee (ncode, BImode,
1756                                               ret, const0_rtx)));
1757
1758       insns = get_insns ();
1759       end_sequence ();
1760
1761       emit_libcall_block (insns, cmp, cmp,
1762                           gen_rtx_fmt_ee (code, BImode, *op0, *op1));
1763       code = NE;
1764     }
1765   else
1766     {
1767       cmp = gen_reg_rtx (BImode);
1768       emit_insn (gen_rtx_SET (VOIDmode, cmp,
1769                               gen_rtx_fmt_ee (code, BImode, *op0, *op1)));
1770       code = NE;
1771     }
1772
1773   *expr = gen_rtx_fmt_ee (code, VOIDmode, cmp, const0_rtx);
1774   *op0 = cmp;
1775   *op1 = const0_rtx;
1776 }
1777
1778 /* Generate an integral vector comparison.  Return true if the condition has
1779    been reversed, and so the sense of the comparison should be inverted.  */
1780
1781 static bool
1782 ia64_expand_vecint_compare (enum rtx_code code, enum machine_mode mode,
1783                             rtx dest, rtx op0, rtx op1)
1784 {
1785   bool negate = false;
1786   rtx x;
1787
1788   /* Canonicalize the comparison to EQ, GT, GTU.  */
1789   switch (code)
1790     {
1791     case EQ:
1792     case GT:
1793     case GTU:
1794       break;
1795
1796     case NE:
1797     case LE:
1798     case LEU:
1799       code = reverse_condition (code);
1800       negate = true;
1801       break;
1802
1803     case GE:
1804     case GEU:
1805       code = reverse_condition (code);
1806       negate = true;
1807       /* FALLTHRU */
1808
1809     case LT:
1810     case LTU:
1811       code = swap_condition (code);
1812       x = op0, op0 = op1, op1 = x;
1813       break;
1814
1815     default:
1816       gcc_unreachable ();
1817     }
1818
1819   /* Unsigned parallel compare is not supported by the hardware.  Play some
1820      tricks to turn this into a signed comparison against 0.  */
1821   if (code == GTU)
1822     {
1823       switch (mode)
1824         {
1825         case V2SImode:
1826           {
1827             rtx t1, t2, mask;
1828
1829             /* Subtract (-(INT MAX) - 1) from both operands to make
1830                them signed.  */
1831             mask = GEN_INT (0x80000000);
1832             mask = gen_rtx_CONST_VECTOR (V2SImode, gen_rtvec (2, mask, mask));
1833             mask = force_reg (mode, mask);
1834             t1 = gen_reg_rtx (mode);
1835             emit_insn (gen_subv2si3 (t1, op0, mask));
1836             t2 = gen_reg_rtx (mode);
1837             emit_insn (gen_subv2si3 (t2, op1, mask));
1838             op0 = t1;
1839             op1 = t2;
1840             code = GT;
1841           }
1842           break;
1843
1844         case V8QImode:
1845         case V4HImode:
1846           /* Perform a parallel unsigned saturating subtraction.  */
1847           x = gen_reg_rtx (mode);
1848           emit_insn (gen_rtx_SET (VOIDmode, x,
1849                                   gen_rtx_US_MINUS (mode, op0, op1)));
1850
1851           code = EQ;
1852           op0 = x;
1853           op1 = CONST0_RTX (mode);
1854           negate = !negate;
1855           break;
1856
1857         default:
1858           gcc_unreachable ();
1859         }
1860     }
1861
1862   x = gen_rtx_fmt_ee (code, mode, op0, op1);
1863   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
1864
1865   return negate;
1866 }
1867
1868 /* Emit an integral vector conditional move.  */
1869
1870 void
1871 ia64_expand_vecint_cmov (rtx operands[])
1872 {
1873   enum machine_mode mode = GET_MODE (operands[0]);
1874   enum rtx_code code = GET_CODE (operands[3]);
1875   bool negate;
1876   rtx cmp, x, ot, of;
1877
1878   cmp = gen_reg_rtx (mode);
1879   negate = ia64_expand_vecint_compare (code, mode, cmp,
1880                                        operands[4], operands[5]);
1881
1882   ot = operands[1+negate];
1883   of = operands[2-negate];
1884
1885   if (ot == CONST0_RTX (mode))
1886     {
1887       if (of == CONST0_RTX (mode))
1888         {
1889           emit_move_insn (operands[0], ot);
1890           return;
1891         }
1892
1893       x = gen_rtx_NOT (mode, cmp);
1894       x = gen_rtx_AND (mode, x, of);
1895       emit_insn (gen_rtx_SET (VOIDmode, operands[0], x));
1896     }
1897   else if (of == CONST0_RTX (mode))
1898     {
1899       x = gen_rtx_AND (mode, cmp, ot);
1900       emit_insn (gen_rtx_SET (VOIDmode, operands[0], x));
1901     }
1902   else
1903     {
1904       rtx t, f;
1905
1906       t = gen_reg_rtx (mode);
1907       x = gen_rtx_AND (mode, cmp, operands[1+negate]);
1908       emit_insn (gen_rtx_SET (VOIDmode, t, x));
1909
1910       f = gen_reg_rtx (mode);
1911       x = gen_rtx_NOT (mode, cmp);
1912       x = gen_rtx_AND (mode, x, operands[2-negate]);
1913       emit_insn (gen_rtx_SET (VOIDmode, f, x));
1914
1915       x = gen_rtx_IOR (mode, t, f);
1916       emit_insn (gen_rtx_SET (VOIDmode, operands[0], x));
1917     }
1918 }
1919
1920 /* Emit an integral vector min or max operation.  Return true if all done.  */
1921
1922 bool
1923 ia64_expand_vecint_minmax (enum rtx_code code, enum machine_mode mode,
1924                            rtx operands[])
1925 {
1926   rtx xops[6];
1927
1928   /* These four combinations are supported directly.  */
1929   if (mode == V8QImode && (code == UMIN || code == UMAX))
1930     return false;
1931   if (mode == V4HImode && (code == SMIN || code == SMAX))
1932     return false;
1933
1934   /* This combination can be implemented with only saturating subtraction.  */
1935   if (mode == V4HImode && code == UMAX)
1936     {
1937       rtx x, tmp = gen_reg_rtx (mode);
1938
1939       x = gen_rtx_US_MINUS (mode, operands[1], operands[2]);
1940       emit_insn (gen_rtx_SET (VOIDmode, tmp, x));
1941
1942       emit_insn (gen_addv4hi3 (operands[0], tmp, operands[2]));
1943       return true;
1944     }
1945
1946   /* Everything else implemented via vector comparisons.  */
1947   xops[0] = operands[0];
1948   xops[4] = xops[1] = operands[1];
1949   xops[5] = xops[2] = operands[2];
1950
1951   switch (code)
1952     {
1953     case UMIN:
1954       code = LTU;
1955       break;
1956     case UMAX:
1957       code = GTU;
1958       break;
1959     case SMIN:
1960       code = LT;
1961       break;
1962     case SMAX:
1963       code = GT;
1964       break;
1965     default:
1966       gcc_unreachable ();
1967     }
1968   xops[3] = gen_rtx_fmt_ee (code, VOIDmode, operands[1], operands[2]);
1969
1970   ia64_expand_vecint_cmov (xops);
1971   return true;
1972 }
1973
1974 /* Emit an integral vector unpack operation.  */
1975
1976 void
1977 ia64_expand_unpack (rtx operands[3], bool unsignedp, bool highp)
1978 {
1979   enum machine_mode mode = GET_MODE (operands[1]);
1980   rtx (*gen) (rtx, rtx, rtx);
1981   rtx x;
1982
1983   switch (mode)
1984     {
1985     case V8QImode:
1986       gen = highp ? gen_vec_interleave_highv8qi : gen_vec_interleave_lowv8qi;
1987       break;
1988     case V4HImode:
1989       gen = highp ? gen_vec_interleave_highv4hi : gen_vec_interleave_lowv4hi;
1990       break;
1991     default:
1992       gcc_unreachable ();
1993     }
1994
1995   /* Fill in x with the sign extension of each element in op1.  */
1996   if (unsignedp)
1997     x = CONST0_RTX (mode);
1998   else
1999     {
2000       bool neg;
2001
2002       x = gen_reg_rtx (mode);
2003
2004       neg = ia64_expand_vecint_compare (LT, mode, x, operands[1],
2005                                         CONST0_RTX (mode));
2006       gcc_assert (!neg);
2007     }
2008
2009   emit_insn (gen (gen_lowpart (mode, operands[0]), operands[1], x));
2010 }
2011
2012 /* Emit an integral vector widening sum operations.  */
2013
2014 void
2015 ia64_expand_widen_sum (rtx operands[3], bool unsignedp)
2016 {
2017   rtx l, h, x, s;
2018   enum machine_mode wmode, mode;
2019   rtx (*unpack_l) (rtx, rtx, rtx);
2020   rtx (*unpack_h) (rtx, rtx, rtx);
2021   rtx (*plus) (rtx, rtx, rtx);
2022
2023   wmode = GET_MODE (operands[0]);
2024   mode = GET_MODE (operands[1]);
2025
2026   switch (mode)
2027     {
2028     case V8QImode:
2029       unpack_l = gen_vec_interleave_lowv8qi;
2030       unpack_h = gen_vec_interleave_highv8qi;
2031       plus = gen_addv4hi3;
2032       break;
2033     case V4HImode:
2034       unpack_l = gen_vec_interleave_lowv4hi;
2035       unpack_h = gen_vec_interleave_highv4hi;
2036       plus = gen_addv2si3;
2037       break;
2038     default:
2039       gcc_unreachable ();
2040     }
2041
2042   /* Fill in x with the sign extension of each element in op1.  */
2043   if (unsignedp)
2044     x = CONST0_RTX (mode);
2045   else
2046     {
2047       bool neg;
2048
2049       x = gen_reg_rtx (mode);
2050
2051       neg = ia64_expand_vecint_compare (LT, mode, x, operands[1],
2052                                         CONST0_RTX (mode));
2053       gcc_assert (!neg);
2054     }
2055
2056   l = gen_reg_rtx (wmode);
2057   h = gen_reg_rtx (wmode);
2058   s = gen_reg_rtx (wmode);
2059
2060   emit_insn (unpack_l (gen_lowpart (mode, l), operands[1], x));
2061   emit_insn (unpack_h (gen_lowpart (mode, h), operands[1], x));
2062   emit_insn (plus (s, l, operands[2]));
2063   emit_insn (plus (operands[0], h, s));
2064 }
2065
2066 void
2067 ia64_expand_widen_mul_v4hi (rtx operands[3], bool unsignedp, bool highp)
2068 {
2069   rtx l = gen_reg_rtx (V4HImode);
2070   rtx h = gen_reg_rtx (V4HImode);
2071   rtx (*mulhigh)(rtx, rtx, rtx, rtx);
2072   rtx (*interl)(rtx, rtx, rtx);
2073
2074   emit_insn (gen_mulv4hi3 (l, operands[1], operands[2]));
2075
2076   /* For signed, pmpy2.r would appear to more closely match this operation.
2077      However, the vectorizer is more likely to use the LO and HI patterns
2078      in pairs. At which point, with this formulation, the first two insns
2079      of each can be CSEd.  */
2080   mulhigh = unsignedp ? gen_pmpyshr2_u : gen_pmpyshr2;
2081   emit_insn (mulhigh (h, operands[1], operands[2], GEN_INT (16)));
2082
2083   interl = highp ? gen_vec_interleave_highv4hi : gen_vec_interleave_lowv4hi;
2084   emit_insn (interl (gen_lowpart (V4HImode, operands[0]), l, h));
2085 }
2086
2087 /* Emit a signed or unsigned V8QI dot product operation.  */
2088
2089 void
2090 ia64_expand_dot_prod_v8qi (rtx operands[4], bool unsignedp)
2091 {
2092   rtx l1, l2, h1, h2, x1, x2, p1, p2, p3, p4, s1, s2, s3;
2093
2094   /* Fill in x1 and x2 with the sign extension of each element.  */
2095   if (unsignedp)
2096     x1 = x2 = CONST0_RTX (V8QImode);
2097   else
2098     {
2099       bool neg;
2100
2101       x1 = gen_reg_rtx (V8QImode);
2102       x2 = gen_reg_rtx (V8QImode);
2103
2104       neg = ia64_expand_vecint_compare (LT, V8QImode, x1, operands[1],
2105                                         CONST0_RTX (V8QImode));
2106       gcc_assert (!neg);
2107       neg = ia64_expand_vecint_compare (LT, V8QImode, x2, operands[2],
2108                                         CONST0_RTX (V8QImode));
2109       gcc_assert (!neg);
2110     }
2111
2112   l1 = gen_reg_rtx (V4HImode);
2113   l2 = gen_reg_rtx (V4HImode);
2114   h1 = gen_reg_rtx (V4HImode);
2115   h2 = gen_reg_rtx (V4HImode);
2116
2117   emit_insn (gen_vec_interleave_lowv8qi
2118              (gen_lowpart (V8QImode, l1), operands[1], x1));
2119   emit_insn (gen_vec_interleave_lowv8qi
2120              (gen_lowpart (V8QImode, l2), operands[2], x2));
2121   emit_insn (gen_vec_interleave_highv8qi
2122              (gen_lowpart (V8QImode, h1), operands[1], x1));
2123   emit_insn (gen_vec_interleave_highv8qi
2124              (gen_lowpart (V8QImode, h2), operands[2], x2));
2125
2126   p1 = gen_reg_rtx (V2SImode);
2127   p2 = gen_reg_rtx (V2SImode);
2128   p3 = gen_reg_rtx (V2SImode);
2129   p4 = gen_reg_rtx (V2SImode);
2130   emit_insn (gen_pmpy2_r (p1, l1, l2));
2131   emit_insn (gen_pmpy2_l (p2, l1, l2));
2132   emit_insn (gen_pmpy2_r (p3, h1, h2));
2133   emit_insn (gen_pmpy2_l (p4, h1, h2));
2134
2135   s1 = gen_reg_rtx (V2SImode);
2136   s2 = gen_reg_rtx (V2SImode);
2137   s3 = gen_reg_rtx (V2SImode);
2138   emit_insn (gen_addv2si3 (s1, p1, p2));
2139   emit_insn (gen_addv2si3 (s2, p3, p4));
2140   emit_insn (gen_addv2si3 (s3, s1, operands[3]));
2141   emit_insn (gen_addv2si3 (operands[0], s2, s3));
2142 }
2143
2144 /* Emit the appropriate sequence for a call.  */
2145
2146 void
2147 ia64_expand_call (rtx retval, rtx addr, rtx nextarg ATTRIBUTE_UNUSED,
2148                   int sibcall_p)
2149 {
2150   rtx insn, b0;
2151
2152   addr = XEXP (addr, 0);
2153   addr = convert_memory_address (DImode, addr);
2154   b0 = gen_rtx_REG (DImode, R_BR (0));
2155
2156   /* ??? Should do this for functions known to bind local too.  */
2157   if (TARGET_NO_PIC || TARGET_AUTO_PIC)
2158     {
2159       if (sibcall_p)
2160         insn = gen_sibcall_nogp (addr);
2161       else if (! retval)
2162         insn = gen_call_nogp (addr, b0);
2163       else
2164         insn = gen_call_value_nogp (retval, addr, b0);
2165       insn = emit_call_insn (insn);
2166     }
2167   else
2168     {
2169       if (sibcall_p)
2170         insn = gen_sibcall_gp (addr);
2171       else if (! retval)
2172         insn = gen_call_gp (addr, b0);
2173       else
2174         insn = gen_call_value_gp (retval, addr, b0);
2175       insn = emit_call_insn (insn);
2176
2177       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
2178     }
2179
2180   if (sibcall_p)
2181     use_reg (&CALL_INSN_FUNCTION_USAGE (insn), b0);
2182
2183   if (TARGET_ABI_OPEN_VMS)
2184     use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
2185              gen_rtx_REG (DImode, GR_REG (25)));
2186 }
2187
2188 static void
2189 reg_emitted (enum ia64_frame_regs r)
2190 {
2191   if (emitted_frame_related_regs[r] == 0)
2192     emitted_frame_related_regs[r] = current_frame_info.r[r];
2193   else
2194     gcc_assert (emitted_frame_related_regs[r] == current_frame_info.r[r]);
2195 }
2196
2197 static int
2198 get_reg (enum ia64_frame_regs r)
2199 {
2200   reg_emitted (r);
2201   return current_frame_info.r[r];
2202 }
2203
2204 static bool
2205 is_emitted (int regno)
2206 {
2207   unsigned int r;
2208
2209   for (r = reg_fp; r < number_of_ia64_frame_regs; r++)
2210     if (emitted_frame_related_regs[r] == regno)
2211       return true;
2212   return false;
2213 }
2214
2215 void
2216 ia64_reload_gp (void)
2217 {
2218   rtx tmp;
2219
2220   if (current_frame_info.r[reg_save_gp])
2221     {
2222       tmp = gen_rtx_REG (DImode, get_reg (reg_save_gp));
2223     }
2224   else
2225     {
2226       HOST_WIDE_INT offset;
2227       rtx offset_r;
2228
2229       offset = (current_frame_info.spill_cfa_off
2230                 + current_frame_info.spill_size);
2231       if (frame_pointer_needed)
2232         {
2233           tmp = hard_frame_pointer_rtx;
2234           offset = -offset;
2235         }
2236       else
2237         {
2238           tmp = stack_pointer_rtx;
2239           offset = current_frame_info.total_size - offset;
2240         }
2241
2242       offset_r = GEN_INT (offset);
2243       if (satisfies_constraint_I (offset_r))
2244         emit_insn (gen_adddi3 (pic_offset_table_rtx, tmp, offset_r));
2245       else
2246         {
2247           emit_move_insn (pic_offset_table_rtx, offset_r);
2248           emit_insn (gen_adddi3 (pic_offset_table_rtx,
2249                                  pic_offset_table_rtx, tmp));
2250         }
2251
2252       tmp = gen_rtx_MEM (DImode, pic_offset_table_rtx);
2253     }
2254
2255   emit_move_insn (pic_offset_table_rtx, tmp);
2256 }
2257
2258 void
2259 ia64_split_call (rtx retval, rtx addr, rtx retaddr, rtx scratch_r,
2260                  rtx scratch_b, int noreturn_p, int sibcall_p)
2261 {
2262   rtx insn;
2263   bool is_desc = false;
2264
2265   /* If we find we're calling through a register, then we're actually
2266      calling through a descriptor, so load up the values.  */
2267   if (REG_P (addr) && GR_REGNO_P (REGNO (addr)))
2268     {
2269       rtx tmp;
2270       bool addr_dead_p;
2271
2272       /* ??? We are currently constrained to *not* use peep2, because
2273          we can legitimately change the global lifetime of the GP
2274          (in the form of killing where previously live).  This is
2275          because a call through a descriptor doesn't use the previous
2276          value of the GP, while a direct call does, and we do not
2277          commit to either form until the split here.
2278
2279          That said, this means that we lack precise life info for
2280          whether ADDR is dead after this call.  This is not terribly
2281          important, since we can fix things up essentially for free
2282          with the POST_DEC below, but it's nice to not use it when we
2283          can immediately tell it's not necessary.  */
2284       addr_dead_p = ((noreturn_p || sibcall_p
2285                       || TEST_HARD_REG_BIT (regs_invalidated_by_call,
2286                                             REGNO (addr)))
2287                      && !FUNCTION_ARG_REGNO_P (REGNO (addr)));
2288
2289       /* Load the code address into scratch_b.  */
2290       tmp = gen_rtx_POST_INC (Pmode, addr);
2291       tmp = gen_rtx_MEM (Pmode, tmp);
2292       emit_move_insn (scratch_r, tmp);
2293       emit_move_insn (scratch_b, scratch_r);
2294
2295       /* Load the GP address.  If ADDR is not dead here, then we must
2296          revert the change made above via the POST_INCREMENT.  */
2297       if (!addr_dead_p)
2298         tmp = gen_rtx_POST_DEC (Pmode, addr);
2299       else
2300         tmp = addr;
2301       tmp = gen_rtx_MEM (Pmode, tmp);
2302       emit_move_insn (pic_offset_table_rtx, tmp);
2303
2304       is_desc = true;
2305       addr = scratch_b;
2306     }
2307
2308   if (sibcall_p)
2309     insn = gen_sibcall_nogp (addr);
2310   else if (retval)
2311     insn = gen_call_value_nogp (retval, addr, retaddr);
2312   else
2313     insn = gen_call_nogp (addr, retaddr);
2314   emit_call_insn (insn);
2315
2316   if ((!TARGET_CONST_GP || is_desc) && !noreturn_p && !sibcall_p)
2317     ia64_reload_gp ();
2318 }
2319
2320 /* Expand an atomic operation.  We want to perform MEM <CODE>= VAL atomically.
2321
2322    This differs from the generic code in that we know about the zero-extending
2323    properties of cmpxchg, and the zero-extending requirements of ar.ccv.  We
2324    also know that ld.acq+cmpxchg.rel equals a full barrier.
2325
2326    The loop we want to generate looks like
2327
2328         cmp_reg = mem;
2329       label:
2330         old_reg = cmp_reg;
2331         new_reg = cmp_reg op val;
2332         cmp_reg = compare-and-swap(mem, old_reg, new_reg)
2333         if (cmp_reg != old_reg)
2334           goto label;
2335
2336    Note that we only do the plain load from memory once.  Subsequent
2337    iterations use the value loaded by the compare-and-swap pattern.  */
2338
2339 void
2340 ia64_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
2341                        rtx old_dst, rtx new_dst)
2342 {
2343   enum machine_mode mode = GET_MODE (mem);
2344   rtx old_reg, new_reg, cmp_reg, ar_ccv, label;
2345   enum insn_code icode;
2346
2347   /* Special case for using fetchadd.  */
2348   if ((mode == SImode || mode == DImode)
2349       && (code == PLUS || code == MINUS)
2350       && fetchadd_operand (val, mode))
2351     {
2352       if (code == MINUS)
2353         val = GEN_INT (-INTVAL (val));
2354
2355       if (!old_dst)
2356         old_dst = gen_reg_rtx (mode);
2357
2358       emit_insn (gen_memory_barrier ());
2359
2360       if (mode == SImode)
2361         icode = CODE_FOR_fetchadd_acq_si;
2362       else
2363         icode = CODE_FOR_fetchadd_acq_di;
2364       emit_insn (GEN_FCN (icode) (old_dst, mem, val));
2365
2366       if (new_dst)
2367         {
2368           new_reg = expand_simple_binop (mode, PLUS, old_dst, val, new_dst,
2369                                          true, OPTAB_WIDEN);
2370           if (new_reg != new_dst)
2371             emit_move_insn (new_dst, new_reg);
2372         }
2373       return;
2374     }
2375
2376   /* Because of the volatile mem read, we get an ld.acq, which is the
2377      front half of the full barrier.  The end half is the cmpxchg.rel.  */
2378   gcc_assert (MEM_VOLATILE_P (mem));
2379
2380   old_reg = gen_reg_rtx (DImode);
2381   cmp_reg = gen_reg_rtx (DImode);
2382   label = gen_label_rtx ();
2383
2384   if (mode != DImode)
2385     {
2386       val = simplify_gen_subreg (DImode, val, mode, 0);
2387       emit_insn (gen_extend_insn (cmp_reg, mem, DImode, mode, 1));
2388     }
2389   else
2390     emit_move_insn (cmp_reg, mem);
2391
2392   emit_label (label);
2393
2394   ar_ccv = gen_rtx_REG (DImode, AR_CCV_REGNUM);
2395   emit_move_insn (old_reg, cmp_reg);
2396   emit_move_insn (ar_ccv, cmp_reg);
2397
2398   if (old_dst)
2399     emit_move_insn (old_dst, gen_lowpart (mode, cmp_reg));
2400
2401   new_reg = cmp_reg;
2402   if (code == NOT)
2403     {
2404       new_reg = expand_simple_binop (DImode, AND, new_reg, val, NULL_RTX,
2405                                      true, OPTAB_DIRECT);
2406       new_reg = expand_simple_unop (DImode, code, new_reg, NULL_RTX, true);
2407     }
2408   else
2409     new_reg = expand_simple_binop (DImode, code, new_reg, val, NULL_RTX,
2410                                    true, OPTAB_DIRECT);
2411
2412   if (mode != DImode)
2413     new_reg = gen_lowpart (mode, new_reg);
2414   if (new_dst)
2415     emit_move_insn (new_dst, new_reg);
2416
2417   switch (mode)
2418     {
2419     case QImode:  icode = CODE_FOR_cmpxchg_rel_qi;  break;
2420     case HImode:  icode = CODE_FOR_cmpxchg_rel_hi;  break;
2421     case SImode:  icode = CODE_FOR_cmpxchg_rel_si;  break;
2422     case DImode:  icode = CODE_FOR_cmpxchg_rel_di;  break;
2423     default:
2424       gcc_unreachable ();
2425     }
2426
2427   emit_insn (GEN_FCN (icode) (cmp_reg, mem, ar_ccv, new_reg));
2428
2429   emit_cmp_and_jump_insns (cmp_reg, old_reg, NE, NULL, DImode, true, label);
2430 }
2431 \f
2432 /* Begin the assembly file.  */
2433
2434 static void
2435 ia64_file_start (void)
2436 {
2437   /* Variable tracking should be run after all optimizations which change order
2438      of insns.  It also needs a valid CFG.  This can't be done in
2439      ia64_option_override, because flag_var_tracking is finalized after
2440      that.  */
2441   ia64_flag_var_tracking = flag_var_tracking;
2442   flag_var_tracking = 0;
2443
2444   default_file_start ();
2445   emit_safe_across_calls ();
2446 }
2447
2448 void
2449 emit_safe_across_calls (void)
2450 {
2451   unsigned int rs, re;
2452   int out_state;
2453
2454   rs = 1;
2455   out_state = 0;
2456   while (1)
2457     {
2458       while (rs < 64 && call_used_regs[PR_REG (rs)])
2459         rs++;
2460       if (rs >= 64)
2461         break;
2462       for (re = rs + 1; re < 64 && ! call_used_regs[PR_REG (re)]; re++)
2463         continue;
2464       if (out_state == 0)
2465         {
2466           fputs ("\t.pred.safe_across_calls ", asm_out_file);
2467           out_state = 1;
2468         }
2469       else
2470         fputc (',', asm_out_file);
2471       if (re == rs + 1)
2472         fprintf (asm_out_file, "p%u", rs);
2473       else
2474         fprintf (asm_out_file, "p%u-p%u", rs, re - 1);
2475       rs = re + 1;
2476     }
2477   if (out_state)
2478     fputc ('\n', asm_out_file);
2479 }
2480
2481 /* Globalize a declaration.  */
2482
2483 static void
2484 ia64_globalize_decl_name (FILE * stream, tree decl)
2485 {
2486   const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2487   tree version_attr = lookup_attribute ("version_id", DECL_ATTRIBUTES (decl));
2488   if (version_attr)
2489     {
2490       tree v = TREE_VALUE (TREE_VALUE (version_attr));
2491       const char *p = TREE_STRING_POINTER (v);
2492       fprintf (stream, "\t.alias %s#, \"%s{%s}\"\n", name, name, p);
2493     }
2494   targetm.asm_out.globalize_label (stream, name);
2495   if (TREE_CODE (decl) == FUNCTION_DECL)
2496     ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "function");
2497 }
2498
2499 /* Helper function for ia64_compute_frame_size: find an appropriate general
2500    register to spill some special register to.  SPECIAL_SPILL_MASK contains
2501    bits in GR0 to GR31 that have already been allocated by this routine.
2502    TRY_LOCALS is true if we should attempt to locate a local regnum.  */
2503
2504 static int
2505 find_gr_spill (enum ia64_frame_regs r, int try_locals)
2506 {
2507   int regno;
2508
2509   if (emitted_frame_related_regs[r] != 0)
2510     {
2511       regno = emitted_frame_related_regs[r];
2512       if (regno >= LOC_REG (0) && regno < LOC_REG (80 - frame_pointer_needed)
2513           && current_frame_info.n_local_regs < regno - LOC_REG (0) + 1)
2514         current_frame_info.n_local_regs = regno - LOC_REG (0) + 1;
2515       else if (current_function_is_leaf 
2516                && regno >= GR_REG (1) && regno <= GR_REG (31))
2517         current_frame_info.gr_used_mask |= 1 << regno;
2518
2519       return regno;
2520     }
2521
2522   /* If this is a leaf function, first try an otherwise unused
2523      call-clobbered register.  */
2524   if (current_function_is_leaf)
2525     {
2526       for (regno = GR_REG (1); regno <= GR_REG (31); regno++)
2527         if (! df_regs_ever_live_p (regno)
2528             && call_used_regs[regno]
2529             && ! fixed_regs[regno]
2530             && ! global_regs[regno]
2531             && ((current_frame_info.gr_used_mask >> regno) & 1) == 0
2532             && ! is_emitted (regno))
2533           {
2534             current_frame_info.gr_used_mask |= 1 << regno;
2535             return regno;
2536           }
2537     }
2538
2539   if (try_locals)
2540     {
2541       regno = current_frame_info.n_local_regs;
2542       /* If there is a frame pointer, then we can't use loc79, because
2543          that is HARD_FRAME_POINTER_REGNUM.  In particular, see the
2544          reg_name switching code in ia64_expand_prologue.  */
2545       while (regno < (80 - frame_pointer_needed))
2546         if (! is_emitted (LOC_REG (regno++)))
2547           {
2548             current_frame_info.n_local_regs = regno;
2549             return LOC_REG (regno - 1);
2550           }
2551     }
2552
2553   /* Failed to find a general register to spill to.  Must use stack.  */
2554   return 0;
2555 }
2556
2557 /* In order to make for nice schedules, we try to allocate every temporary
2558    to a different register.  We must of course stay away from call-saved,
2559    fixed, and global registers.  We must also stay away from registers
2560    allocated in current_frame_info.gr_used_mask, since those include regs
2561    used all through the prologue.
2562
2563    Any register allocated here must be used immediately.  The idea is to
2564    aid scheduling, not to solve data flow problems.  */
2565
2566 static int last_scratch_gr_reg;
2567
2568 static int
2569 next_scratch_gr_reg (void)
2570 {
2571   int i, regno;
2572
2573   for (i = 0; i < 32; ++i)
2574     {
2575       regno = (last_scratch_gr_reg + i + 1) & 31;
2576       if (call_used_regs[regno]
2577           && ! fixed_regs[regno]
2578           && ! global_regs[regno]
2579           && ((current_frame_info.gr_used_mask >> regno) & 1) == 0)
2580         {
2581           last_scratch_gr_reg = regno;
2582           return regno;
2583         }
2584     }
2585
2586   /* There must be _something_ available.  */
2587   gcc_unreachable ();
2588 }
2589
2590 /* Helper function for ia64_compute_frame_size, called through
2591    diddle_return_value.  Mark REG in current_frame_info.gr_used_mask.  */
2592
2593 static void
2594 mark_reg_gr_used_mask (rtx reg, void *data ATTRIBUTE_UNUSED)
2595 {
2596   unsigned int regno = REGNO (reg);
2597   if (regno < 32)
2598     {
2599       unsigned int i, n = hard_regno_nregs[regno][GET_MODE (reg)];
2600       for (i = 0; i < n; ++i)
2601         current_frame_info.gr_used_mask |= 1 << (regno + i);
2602     }
2603 }
2604
2605
2606 /* Returns the number of bytes offset between the frame pointer and the stack
2607    pointer for the current function.  SIZE is the number of bytes of space
2608    needed for local variables.  */
2609
2610 static void
2611 ia64_compute_frame_size (HOST_WIDE_INT size)
2612 {
2613   HOST_WIDE_INT total_size;
2614   HOST_WIDE_INT spill_size = 0;
2615   HOST_WIDE_INT extra_spill_size = 0;
2616   HOST_WIDE_INT pretend_args_size;
2617   HARD_REG_SET mask;
2618   int n_spilled = 0;
2619   int spilled_gr_p = 0;
2620   int spilled_fr_p = 0;
2621   unsigned int regno;
2622   int min_regno;
2623   int max_regno;
2624   int i;
2625
2626   if (current_frame_info.initialized)
2627     return;
2628
2629   memset (&current_frame_info, 0, sizeof current_frame_info);
2630   CLEAR_HARD_REG_SET (mask);
2631
2632   /* Don't allocate scratches to the return register.  */
2633   diddle_return_value (mark_reg_gr_used_mask, NULL);
2634
2635   /* Don't allocate scratches to the EH scratch registers.  */
2636   if (cfun->machine->ia64_eh_epilogue_sp)
2637     mark_reg_gr_used_mask (cfun->machine->ia64_eh_epilogue_sp, NULL);
2638   if (cfun->machine->ia64_eh_epilogue_bsp)
2639     mark_reg_gr_used_mask (cfun->machine->ia64_eh_epilogue_bsp, NULL);
2640
2641   /* Find the size of the register stack frame.  We have only 80 local
2642      registers, because we reserve 8 for the inputs and 8 for the
2643      outputs.  */
2644
2645   /* Skip HARD_FRAME_POINTER_REGNUM (loc79) when frame_pointer_needed,
2646      since we'll be adjusting that down later.  */
2647   regno = LOC_REG (78) + ! frame_pointer_needed;
2648   for (; regno >= LOC_REG (0); regno--)
2649     if (df_regs_ever_live_p (regno) && !is_emitted (regno))
2650       break;
2651   current_frame_info.n_local_regs = regno - LOC_REG (0) + 1;
2652
2653   /* For functions marked with the syscall_linkage attribute, we must mark
2654      all eight input registers as in use, so that locals aren't visible to
2655      the caller.  */
2656
2657   if (cfun->machine->n_varargs > 0
2658       || lookup_attribute ("syscall_linkage",
2659                            TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
2660     current_frame_info.n_input_regs = 8;
2661   else
2662     {
2663       for (regno = IN_REG (7); regno >= IN_REG (0); regno--)
2664         if (df_regs_ever_live_p (regno))
2665           break;
2666       current_frame_info.n_input_regs = regno - IN_REG (0) + 1;
2667     }
2668
2669   for (regno = OUT_REG (7); regno >= OUT_REG (0); regno--)
2670     if (df_regs_ever_live_p (regno))
2671       break;
2672   i = regno - OUT_REG (0) + 1;
2673
2674 #ifndef PROFILE_HOOK
2675   /* When -p profiling, we need one output register for the mcount argument.
2676      Likewise for -a profiling for the bb_init_func argument.  For -ax
2677      profiling, we need two output registers for the two bb_init_trace_func
2678      arguments.  */
2679   if (crtl->profile)
2680     i = MAX (i, 1);
2681 #endif
2682   current_frame_info.n_output_regs = i;
2683
2684   /* ??? No rotating register support yet.  */
2685   current_frame_info.n_rotate_regs = 0;
2686
2687   /* Discover which registers need spilling, and how much room that
2688      will take.  Begin with floating point and general registers,
2689      which will always wind up on the stack.  */
2690
2691   for (regno = FR_REG (2); regno <= FR_REG (127); regno++)
2692     if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2693       {
2694         SET_HARD_REG_BIT (mask, regno);
2695         spill_size += 16;
2696         n_spilled += 1;
2697         spilled_fr_p = 1;
2698       }
2699
2700   for (regno = GR_REG (1); regno <= GR_REG (31); regno++)
2701     if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2702       {
2703         SET_HARD_REG_BIT (mask, regno);
2704         spill_size += 8;
2705         n_spilled += 1;
2706         spilled_gr_p = 1;
2707       }
2708
2709   for (regno = BR_REG (1); regno <= BR_REG (7); regno++)
2710     if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2711       {
2712         SET_HARD_REG_BIT (mask, regno);
2713         spill_size += 8;
2714         n_spilled += 1;
2715       }
2716
2717   /* Now come all special registers that might get saved in other
2718      general registers.  */
2719
2720   if (frame_pointer_needed)
2721     {
2722       current_frame_info.r[reg_fp] = find_gr_spill (reg_fp, 1);
2723       /* If we did not get a register, then we take LOC79.  This is guaranteed
2724          to be free, even if regs_ever_live is already set, because this is
2725          HARD_FRAME_POINTER_REGNUM.  This requires incrementing n_local_regs,
2726          as we don't count loc79 above.  */
2727       if (current_frame_info.r[reg_fp] == 0)
2728         {
2729           current_frame_info.r[reg_fp] = LOC_REG (79);
2730           current_frame_info.n_local_regs = LOC_REG (79) - LOC_REG (0) + 1;
2731         }
2732     }
2733
2734   if (! current_function_is_leaf)
2735     {
2736       /* Emit a save of BR0 if we call other functions.  Do this even
2737          if this function doesn't return, as EH depends on this to be
2738          able to unwind the stack.  */
2739       SET_HARD_REG_BIT (mask, BR_REG (0));
2740
2741       current_frame_info.r[reg_save_b0] = find_gr_spill (reg_save_b0, 1);
2742       if (current_frame_info.r[reg_save_b0] == 0)
2743         {
2744           extra_spill_size += 8;
2745           n_spilled += 1;
2746         }
2747
2748       /* Similarly for ar.pfs.  */
2749       SET_HARD_REG_BIT (mask, AR_PFS_REGNUM);
2750       current_frame_info.r[reg_save_ar_pfs] = find_gr_spill (reg_save_ar_pfs, 1);
2751       if (current_frame_info.r[reg_save_ar_pfs] == 0)
2752         {
2753           extra_spill_size += 8;
2754           n_spilled += 1;
2755         }
2756
2757       /* Similarly for gp.  Note that if we're calling setjmp, the stacked
2758          registers are clobbered, so we fall back to the stack.  */
2759       current_frame_info.r[reg_save_gp]
2760         = (cfun->calls_setjmp ? 0 : find_gr_spill (reg_save_gp, 1));
2761       if (current_frame_info.r[reg_save_gp] == 0)
2762         {
2763           SET_HARD_REG_BIT (mask, GR_REG (1));
2764           spill_size += 8;
2765           n_spilled += 1;
2766         }
2767     }
2768   else
2769     {
2770       if (df_regs_ever_live_p (BR_REG (0)) && ! call_used_regs[BR_REG (0)])
2771         {
2772           SET_HARD_REG_BIT (mask, BR_REG (0));
2773           extra_spill_size += 8;
2774           n_spilled += 1;
2775         }
2776
2777       if (df_regs_ever_live_p (AR_PFS_REGNUM))
2778         {
2779           SET_HARD_REG_BIT (mask, AR_PFS_REGNUM);
2780           current_frame_info.r[reg_save_ar_pfs] 
2781             = find_gr_spill (reg_save_ar_pfs, 1);
2782           if (current_frame_info.r[reg_save_ar_pfs] == 0)
2783             {
2784               extra_spill_size += 8;
2785               n_spilled += 1;
2786             }
2787         }
2788     }
2789
2790   /* Unwind descriptor hackery: things are most efficient if we allocate
2791      consecutive GR save registers for RP, PFS, FP in that order. However,
2792      it is absolutely critical that FP get the only hard register that's
2793      guaranteed to be free, so we allocated it first.  If all three did
2794      happen to be allocated hard regs, and are consecutive, rearrange them
2795      into the preferred order now.  
2796      
2797      If we have already emitted code for any of those registers,
2798      then it's already too late to change.  */
2799   min_regno = MIN (current_frame_info.r[reg_fp],
2800                    MIN (current_frame_info.r[reg_save_b0],
2801                         current_frame_info.r[reg_save_ar_pfs]));
2802   max_regno = MAX (current_frame_info.r[reg_fp],
2803                    MAX (current_frame_info.r[reg_save_b0],
2804                         current_frame_info.r[reg_save_ar_pfs]));
2805   if (min_regno > 0
2806       && min_regno + 2 == max_regno
2807       && (current_frame_info.r[reg_fp] == min_regno + 1
2808           || current_frame_info.r[reg_save_b0] == min_regno + 1
2809           || current_frame_info.r[reg_save_ar_pfs] == min_regno + 1)
2810       && (emitted_frame_related_regs[reg_save_b0] == 0
2811           || emitted_frame_related_regs[reg_save_b0] == min_regno)
2812       && (emitted_frame_related_regs[reg_save_ar_pfs] == 0
2813           || emitted_frame_related_regs[reg_save_ar_pfs] == min_regno + 1)
2814       && (emitted_frame_related_regs[reg_fp] == 0
2815           || emitted_frame_related_regs[reg_fp] == min_regno + 2))
2816     {
2817       current_frame_info.r[reg_save_b0] = min_regno;
2818       current_frame_info.r[reg_save_ar_pfs] = min_regno + 1;
2819       current_frame_info.r[reg_fp] = min_regno + 2;
2820     }
2821
2822   /* See if we need to store the predicate register block.  */
2823   for (regno = PR_REG (0); regno <= PR_REG (63); regno++)
2824     if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2825       break;
2826   if (regno <= PR_REG (63))
2827     {
2828       SET_HARD_REG_BIT (mask, PR_REG (0));
2829       current_frame_info.r[reg_save_pr] = find_gr_spill (reg_save_pr, 1);
2830       if (current_frame_info.r[reg_save_pr] == 0)
2831         {
2832           extra_spill_size += 8;
2833           n_spilled += 1;
2834         }
2835
2836       /* ??? Mark them all as used so that register renaming and such
2837          are free to use them.  */
2838       for (regno = PR_REG (0); regno <= PR_REG (63); regno++)
2839         df_set_regs_ever_live (regno, true);
2840     }
2841
2842   /* If we're forced to use st8.spill, we're forced to save and restore
2843      ar.unat as well.  The check for existing liveness allows inline asm
2844      to touch ar.unat.  */
2845   if (spilled_gr_p || cfun->machine->n_varargs
2846       || df_regs_ever_live_p (AR_UNAT_REGNUM))
2847     {
2848       df_set_regs_ever_live (AR_UNAT_REGNUM, true);
2849       SET_HARD_REG_BIT (mask, AR_UNAT_REGNUM);
2850       current_frame_info.r[reg_save_ar_unat] 
2851         = find_gr_spill (reg_save_ar_unat, spill_size == 0);
2852       if (current_frame_info.r[reg_save_ar_unat] == 0)
2853         {
2854           extra_spill_size += 8;
2855           n_spilled += 1;
2856         }
2857     }
2858
2859   if (df_regs_ever_live_p (AR_LC_REGNUM))
2860     {
2861       SET_HARD_REG_BIT (mask, AR_LC_REGNUM);
2862       current_frame_info.r[reg_save_ar_lc] 
2863         = find_gr_spill (reg_save_ar_lc, spill_size == 0);
2864       if (current_frame_info.r[reg_save_ar_lc] == 0)
2865         {
2866           extra_spill_size += 8;
2867           n_spilled += 1;
2868         }
2869     }
2870
2871   /* If we have an odd number of words of pretend arguments written to
2872      the stack, then the FR save area will be unaligned.  We round the
2873      size of this area up to keep things 16 byte aligned.  */
2874   if (spilled_fr_p)
2875     pretend_args_size = IA64_STACK_ALIGN (crtl->args.pretend_args_size);
2876   else
2877     pretend_args_size = crtl->args.pretend_args_size;
2878
2879   total_size = (spill_size + extra_spill_size + size + pretend_args_size
2880                 + crtl->outgoing_args_size);
2881   total_size = IA64_STACK_ALIGN (total_size);
2882
2883   /* We always use the 16-byte scratch area provided by the caller, but
2884      if we are a leaf function, there's no one to which we need to provide
2885      a scratch area.  */
2886   if (current_function_is_leaf)
2887     total_size = MAX (0, total_size - 16);
2888
2889   current_frame_info.total_size = total_size;
2890   current_frame_info.spill_cfa_off = pretend_args_size - 16;
2891   current_frame_info.spill_size = spill_size;
2892   current_frame_info.extra_spill_size = extra_spill_size;
2893   COPY_HARD_REG_SET (current_frame_info.mask, mask);
2894   current_frame_info.n_spilled = n_spilled;
2895   current_frame_info.initialized = reload_completed;
2896 }
2897
2898 /* Worker function for TARGET_CAN_ELIMINATE.  */
2899
2900 bool
2901 ia64_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
2902 {
2903   return (to == BR_REG (0) ? current_function_is_leaf : true);
2904 }
2905
2906 /* Compute the initial difference between the specified pair of registers.  */
2907
2908 HOST_WIDE_INT
2909 ia64_initial_elimination_offset (int from, int to)
2910 {
2911   HOST_WIDE_INT offset;
2912
2913   ia64_compute_frame_size (get_frame_size ());
2914   switch (from)
2915     {
2916     case FRAME_POINTER_REGNUM:
2917       switch (to)
2918         {
2919         case HARD_FRAME_POINTER_REGNUM:
2920           if (current_function_is_leaf)
2921             offset = -current_frame_info.total_size;
2922           else
2923             offset = -(current_frame_info.total_size
2924                        - crtl->outgoing_args_size - 16);
2925           break;
2926
2927         case STACK_POINTER_REGNUM:
2928           if (current_function_is_leaf)
2929             offset = 0;
2930           else
2931             offset = 16 + crtl->outgoing_args_size;
2932           break;
2933
2934         default:
2935           gcc_unreachable ();
2936         }
2937       break;
2938
2939     case ARG_POINTER_REGNUM:
2940       /* Arguments start above the 16 byte save area, unless stdarg
2941          in which case we store through the 16 byte save area.  */
2942       switch (to)
2943         {
2944         case HARD_FRAME_POINTER_REGNUM:
2945           offset = 16 - crtl->args.pretend_args_size;
2946           break;
2947
2948         case STACK_POINTER_REGNUM:
2949           offset = (current_frame_info.total_size
2950                     + 16 - crtl->args.pretend_args_size);
2951           break;
2952
2953         default:
2954           gcc_unreachable ();
2955         }
2956       break;
2957
2958     default:
2959       gcc_unreachable ();
2960     }
2961
2962   return offset;
2963 }
2964
2965 /* If there are more than a trivial number of register spills, we use
2966    two interleaved iterators so that we can get two memory references
2967    per insn group.
2968
2969    In order to simplify things in the prologue and epilogue expanders,
2970    we use helper functions to fix up the memory references after the
2971    fact with the appropriate offsets to a POST_MODIFY memory mode.
2972    The following data structure tracks the state of the two iterators
2973    while insns are being emitted.  */
2974
2975 struct spill_fill_data
2976 {
2977   rtx init_after;               /* point at which to emit initializations */
2978   rtx init_reg[2];              /* initial base register */
2979   rtx iter_reg[2];              /* the iterator registers */
2980   rtx *prev_addr[2];            /* address of last memory use */
2981   rtx prev_insn[2];             /* the insn corresponding to prev_addr */
2982   HOST_WIDE_INT prev_off[2];    /* last offset */
2983   int n_iter;                   /* number of iterators in use */
2984   int next_iter;                /* next iterator to use */
2985   unsigned int save_gr_used_mask;
2986 };
2987
2988 static struct spill_fill_data spill_fill_data;
2989
2990 static void
2991 setup_spill_pointers (int n_spills, rtx init_reg, HOST_WIDE_INT cfa_off)
2992 {
2993   int i;
2994
2995   spill_fill_data.init_after = get_last_insn ();
2996   spill_fill_data.init_reg[0] = init_reg;
2997   spill_fill_data.init_reg[1] = init_reg;
2998   spill_fill_data.prev_addr[0] = NULL;
2999   spill_fill_data.prev_addr[1] = NULL;
3000   spill_fill_data.prev_insn[0] = NULL;
3001   spill_fill_data.prev_insn[1] = NULL;
3002   spill_fill_data.prev_off[0] = cfa_off;
3003   spill_fill_data.prev_off[1] = cfa_off;
3004   spill_fill_data.next_iter = 0;
3005   spill_fill_data.save_gr_used_mask = current_frame_info.gr_used_mask;
3006
3007   spill_fill_data.n_iter = 1 + (n_spills > 2);
3008   for (i = 0; i < spill_fill_data.n_iter; ++i)
3009     {
3010       int regno = next_scratch_gr_reg ();
3011       spill_fill_data.iter_reg[i] = gen_rtx_REG (DImode, regno);
3012       current_frame_info.gr_used_mask |= 1 << regno;
3013     }
3014 }
3015
3016 static void
3017 finish_spill_pointers (void)
3018 {
3019   current_frame_info.gr_used_mask = spill_fill_data.save_gr_used_mask;
3020 }
3021
3022 static rtx
3023 spill_restore_mem (rtx reg, HOST_WIDE_INT cfa_off)
3024 {
3025   int iter = spill_fill_data.next_iter;
3026   HOST_WIDE_INT disp = spill_fill_data.prev_off[iter] - cfa_off;
3027   rtx disp_rtx = GEN_INT (disp);
3028   rtx mem;
3029
3030   if (spill_fill_data.prev_addr[iter])
3031     {
3032       if (satisfies_constraint_N (disp_rtx))
3033         {
3034           *spill_fill_data.prev_addr[iter]
3035             = gen_rtx_POST_MODIFY (DImode, spill_fill_data.iter_reg[iter],
3036                                    gen_rtx_PLUS (DImode,
3037                                                  spill_fill_data.iter_reg[iter],
3038                                                  disp_rtx));
3039           add_reg_note (spill_fill_data.prev_insn[iter],
3040                         REG_INC, spill_fill_data.iter_reg[iter]);
3041         }
3042       else
3043         {
3044           /* ??? Could use register post_modify for loads.  */
3045           if (!satisfies_constraint_I (disp_rtx))
3046             {
3047               rtx tmp = gen_rtx_REG (DImode, next_scratch_gr_reg ());
3048               emit_move_insn (tmp, disp_rtx);
3049               disp_rtx = tmp;
3050             }
3051           emit_insn (gen_adddi3 (spill_fill_data.iter_reg[iter],
3052                                  spill_fill_data.iter_reg[iter], disp_rtx));
3053         }
3054     }
3055   /* Micro-optimization: if we've created a frame pointer, it's at
3056      CFA 0, which may allow the real iterator to be initialized lower,
3057      slightly increasing parallelism.  Also, if there are few saves
3058      it may eliminate the iterator entirely.  */
3059   else if (disp == 0
3060            && spill_fill_data.init_reg[iter] == stack_pointer_rtx
3061            && frame_pointer_needed)
3062     {
3063       mem = gen_rtx_MEM (GET_MODE (reg), hard_frame_pointer_rtx);
3064       set_mem_alias_set (mem, get_varargs_alias_set ());
3065       return mem;
3066     }
3067   else
3068     {
3069       rtx seq, insn;
3070
3071       if (disp == 0)
3072         seq = gen_movdi (spill_fill_data.iter_reg[iter],
3073                          spill_fill_data.init_reg[iter]);
3074       else
3075         {
3076           start_sequence ();
3077
3078           if (!satisfies_constraint_I (disp_rtx))
3079             {
3080               rtx tmp = gen_rtx_REG (DImode, next_scratch_gr_reg ());
3081               emit_move_insn (tmp, disp_rtx);
3082               disp_rtx = tmp;
3083             }
3084
3085           emit_insn (gen_adddi3 (spill_fill_data.iter_reg[iter],
3086                                  spill_fill_data.init_reg[iter],
3087                                  disp_rtx));
3088
3089           seq = get_insns ();
3090           end_sequence ();
3091         }
3092
3093       /* Careful for being the first insn in a sequence.  */
3094       if (spill_fill_data.init_after)
3095         insn = emit_insn_after (seq, spill_fill_data.init_after);
3096       else
3097         {
3098           rtx first = get_insns ();
3099           if (first)
3100             insn = emit_insn_before (seq, first);
3101           else
3102             insn = emit_insn (seq);
3103         }
3104       spill_fill_data.init_after = insn;
3105     }
3106
3107   mem = gen_rtx_MEM (GET_MODE (reg), spill_fill_data.iter_reg[iter]);
3108
3109   /* ??? Not all of the spills are for varargs, but some of them are.
3110      The rest of the spills belong in an alias set of their own.  But
3111      it doesn't actually hurt to include them here.  */
3112   set_mem_alias_set (mem, get_varargs_alias_set ());
3113
3114   spill_fill_data.prev_addr[iter] = &XEXP (mem, 0);
3115   spill_fill_data.prev_off[iter] = cfa_off;
3116
3117   if (++iter >= spill_fill_data.n_iter)
3118     iter = 0;
3119   spill_fill_data.next_iter = iter;
3120
3121   return mem;
3122 }
3123
3124 static void
3125 do_spill (rtx (*move_fn) (rtx, rtx, rtx), rtx reg, HOST_WIDE_INT cfa_off,
3126           rtx frame_reg)
3127 {
3128   int iter = spill_fill_data.next_iter;
3129   rtx mem, insn;
3130
3131   mem = spill_restore_mem (reg, cfa_off);
3132   insn = emit_insn ((*move_fn) (mem, reg, GEN_INT (cfa_off)));
3133   spill_fill_data.prev_insn[iter] = insn;
3134
3135   if (frame_reg)
3136     {
3137       rtx base;
3138       HOST_WIDE_INT off;
3139
3140       RTX_FRAME_RELATED_P (insn) = 1;
3141
3142       /* Don't even pretend that the unwind code can intuit its way
3143          through a pair of interleaved post_modify iterators.  Just
3144          provide the correct answer.  */
3145
3146       if (frame_pointer_needed)
3147         {
3148           base = hard_frame_pointer_rtx;
3149           off = - cfa_off;
3150         }
3151       else
3152         {
3153           base = stack_pointer_rtx;
3154           off = current_frame_info.total_size - cfa_off;
3155         }
3156
3157       add_reg_note (insn, REG_CFA_OFFSET,
3158                     gen_rtx_SET (VOIDmode,
3159                                  gen_rtx_MEM (GET_MODE (reg),
3160                                               plus_constant (base, off)),
3161                                  frame_reg));
3162     }
3163 }
3164
3165 static void
3166 do_restore (rtx (*move_fn) (rtx, rtx, rtx), rtx reg, HOST_WIDE_INT cfa_off)
3167 {
3168   int iter = spill_fill_data.next_iter;
3169   rtx insn;
3170
3171   insn = emit_insn ((*move_fn) (reg, spill_restore_mem (reg, cfa_off),
3172                                 GEN_INT (cfa_off)));
3173   spill_fill_data.prev_insn[iter] = insn;
3174 }
3175
3176 /* Wrapper functions that discards the CONST_INT spill offset.  These
3177    exist so that we can give gr_spill/gr_fill the offset they need and
3178    use a consistent function interface.  */
3179
3180 static rtx
3181 gen_movdi_x (rtx dest, rtx src, rtx offset ATTRIBUTE_UNUSED)
3182 {
3183   return gen_movdi (dest, src);
3184 }
3185
3186 static rtx
3187 gen_fr_spill_x (rtx dest, rtx src, rtx offset ATTRIBUTE_UNUSED)
3188 {
3189   return gen_fr_spill (dest, src);
3190 }
3191
3192 static rtx
3193 gen_fr_restore_x (rtx dest, rtx src, rtx offset ATTRIBUTE_UNUSED)
3194 {
3195   return gen_fr_restore (dest, src);
3196 }
3197
3198 /* Called after register allocation to add any instructions needed for the
3199    prologue.  Using a prologue insn is favored compared to putting all of the
3200    instructions in output_function_prologue(), since it allows the scheduler
3201    to intermix instructions with the saves of the caller saved registers.  In
3202    some cases, it might be necessary to emit a barrier instruction as the last
3203    insn to prevent such scheduling.
3204
3205    Also any insns generated here should have RTX_FRAME_RELATED_P(insn) = 1
3206    so that the debug info generation code can handle them properly.
3207
3208    The register save area is layed out like so:
3209    cfa+16
3210         [ varargs spill area ]
3211         [ fr register spill area ]
3212         [ br register spill area ]
3213         [ ar register spill area ]
3214         [ pr register spill area ]
3215         [ gr register spill area ] */
3216
3217 /* ??? Get inefficient code when the frame size is larger than can fit in an
3218    adds instruction.  */
3219
3220 void
3221 ia64_expand_prologue (void)
3222 {
3223   rtx insn, ar_pfs_save_reg, ar_unat_save_reg;
3224   int i, epilogue_p, regno, alt_regno, cfa_off, n_varargs;
3225   rtx reg, alt_reg;
3226
3227   ia64_compute_frame_size (get_frame_size ());
3228   last_scratch_gr_reg = 15;
3229
3230   if (flag_stack_usage)
3231     current_function_static_stack_size = current_frame_info.total_size;
3232
3233   if (dump_file) 
3234     {
3235       fprintf (dump_file, "ia64 frame related registers "
3236                "recorded in current_frame_info.r[]:\n");
3237 #define PRINTREG(a) if (current_frame_info.r[a]) \
3238         fprintf(dump_file, "%s = %d\n", #a, current_frame_info.r[a])
3239       PRINTREG(reg_fp);
3240       PRINTREG(reg_save_b0);
3241       PRINTREG(reg_save_pr);
3242       PRINTREG(reg_save_ar_pfs);
3243       PRINTREG(reg_save_ar_unat);
3244       PRINTREG(reg_save_ar_lc);
3245       PRINTREG(reg_save_gp);
3246 #undef PRINTREG
3247     }
3248
3249   /* If there is no epilogue, then we don't need some prologue insns.
3250      We need to avoid emitting the dead prologue insns, because flow
3251      will complain about them.  */
3252   if (optimize)
3253     {
3254       edge e;
3255       edge_iterator ei;
3256
3257       FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
3258         if ((e->flags & EDGE_FAKE) == 0
3259             && (e->flags & EDGE_FALLTHRU) != 0)
3260           break;
3261       epilogue_p = (e != NULL);
3262     }
3263   else
3264     epilogue_p = 1;
3265
3266   /* Set the local, input, and output register names.  We need to do this
3267      for GNU libc, which creates crti.S/crtn.S by splitting initfini.c in
3268      half.  If we use in/loc/out register names, then we get assembler errors
3269      in crtn.S because there is no alloc insn or regstk directive in there.  */
3270   if (! TARGET_REG_NAMES)
3271     {
3272       int inputs = current_frame_info.n_input_regs;
3273       int locals = current_frame_info.n_local_regs;
3274       int outputs = current_frame_info.n_output_regs;
3275
3276       for (i = 0; i < inputs; i++)
3277         reg_names[IN_REG (i)] = ia64_reg_numbers[i];
3278       for (i = 0; i < locals; i++)
3279         reg_names[LOC_REG (i)] = ia64_reg_numbers[inputs + i];
3280       for (i = 0; i < outputs; i++)
3281         reg_names[OUT_REG (i)] = ia64_reg_numbers[inputs + locals + i];
3282     }
3283
3284   /* Set the frame pointer register name.  The regnum is logically loc79,
3285      but of course we'll not have allocated that many locals.  Rather than
3286      worrying about renumbering the existing rtxs, we adjust the name.  */
3287   /* ??? This code means that we can never use one local register when
3288      there is a frame pointer.  loc79 gets wasted in this case, as it is
3289      renamed to a register that will never be used.  See also the try_locals
3290      code in find_gr_spill.  */
3291   if (current_frame_info.r[reg_fp])
3292     {
3293       const char *tmp = reg_names[HARD_FRAME_POINTER_REGNUM];
3294       reg_names[HARD_FRAME_POINTER_REGNUM]
3295         = reg_names[current_frame_info.r[reg_fp]];
3296       reg_names[current_frame_info.r[reg_fp]] = tmp;
3297     }
3298
3299   /* We don't need an alloc instruction if we've used no outputs or locals.  */
3300   if (current_frame_info.n_local_regs == 0
3301       && current_frame_info.n_output_regs == 0
3302       && current_frame_info.n_input_regs <= crtl->args.info.int_regs
3303       && !TEST_HARD_REG_BIT (current_frame_info.mask, AR_PFS_REGNUM))
3304     {
3305       /* If there is no alloc, but there are input registers used, then we
3306          need a .regstk directive.  */
3307       current_frame_info.need_regstk = (TARGET_REG_NAMES != 0);
3308       ar_pfs_save_reg = NULL_RTX;
3309     }
3310   else
3311     {
3312       current_frame_info.need_regstk = 0;
3313
3314       if (current_frame_info.r[reg_save_ar_pfs])
3315         {
3316           regno = current_frame_info.r[reg_save_ar_pfs];
3317           reg_emitted (reg_save_ar_pfs);
3318         }
3319       else
3320         regno = next_scratch_gr_reg ();
3321       ar_pfs_save_reg = gen_rtx_REG (DImode, regno);
3322
3323       insn = emit_insn (gen_alloc (ar_pfs_save_reg,
3324                                    GEN_INT (current_frame_info.n_input_regs),
3325                                    GEN_INT (current_frame_info.n_local_regs),
3326                                    GEN_INT (current_frame_info.n_output_regs),
3327                                    GEN_INT (current_frame_info.n_rotate_regs)));
3328       RTX_FRAME_RELATED_P (insn) = (current_frame_info.r[reg_save_ar_pfs] != 0);
3329     }
3330
3331   /* Set up frame pointer, stack pointer, and spill iterators.  */
3332
3333   n_varargs = cfun->machine->n_varargs;
3334   setup_spill_pointers (current_frame_info.n_spilled + n_varargs,
3335                         stack_pointer_rtx, 0);
3336
3337   if (frame_pointer_needed)
3338     {
3339       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
3340       RTX_FRAME_RELATED_P (insn) = 1;
3341
3342       /* Force the unwind info to recognize this as defining a new CFA,
3343          rather than some temp register setup.  */
3344       add_reg_note (insn, REG_CFA_ADJUST_CFA, NULL_RTX);
3345     }
3346
3347   if (current_frame_info.total_size != 0)
3348     {
3349       rtx frame_size_rtx = GEN_INT (- current_frame_info.total_size);
3350       rtx offset;
3351
3352       if (satisfies_constraint_I (frame_size_rtx))
3353         offset = frame_size_rtx;
3354       else
3355         {
3356           regno = next_scratch_gr_reg ();
3357           offset = gen_rtx_REG (DImode, regno);
3358           emit_move_insn (offset, frame_size_rtx);
3359         }
3360
3361       insn = emit_insn (gen_adddi3 (stack_pointer_rtx,
3362                                     stack_pointer_rtx, offset));
3363
3364       if (! frame_pointer_needed)
3365         {
3366           RTX_FRAME_RELATED_P (insn) = 1;
3367           add_reg_note (insn, REG_CFA_ADJUST_CFA,
3368                         gen_rtx_SET (VOIDmode,
3369                                      stack_pointer_rtx,
3370                                      gen_rtx_PLUS (DImode,
3371                                                    stack_pointer_rtx,
3372                                                    frame_size_rtx)));
3373         }
3374
3375       /* ??? At this point we must generate a magic insn that appears to
3376          modify the stack pointer, the frame pointer, and all spill
3377          iterators.  This would allow the most scheduling freedom.  For
3378          now, just hard stop.  */
3379       emit_insn (gen_blockage ());
3380     }
3381
3382   /* Must copy out ar.unat before doing any integer spills.  */
3383   if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
3384     {
3385       if (current_frame_info.r[reg_save_ar_unat])
3386         {
3387           ar_unat_save_reg
3388             = gen_rtx_REG (DImode, current_frame_info.r[reg_save_ar_unat]);
3389           reg_emitted (reg_save_ar_unat);
3390         }
3391       else
3392         {
3393           alt_regno = next_scratch_gr_reg ();
3394           ar_unat_save_reg = gen_rtx_REG (DImode, alt_regno);
3395           current_frame_info.gr_used_mask |= 1 << alt_regno;
3396         }
3397
3398       reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
3399       insn = emit_move_insn (ar_unat_save_reg, reg);
3400       if (current_frame_info.r[reg_save_ar_unat])
3401         {
3402           RTX_FRAME_RELATED_P (insn) = 1;
3403           add_reg_note (insn, REG_CFA_REGISTER, NULL_RTX);
3404         }
3405
3406       /* Even if we're not going to generate an epilogue, we still
3407          need to save the register so that EH works.  */
3408       if (! epilogue_p && current_frame_info.r[reg_save_ar_unat])
3409         emit_insn (gen_prologue_use (ar_unat_save_reg));
3410     }
3411   else
3412     ar_unat_save_reg = NULL_RTX;
3413
3414   /* Spill all varargs registers.  Do this before spilling any GR registers,
3415      since we want the UNAT bits for the GR registers to override the UNAT
3416      bits from varargs, which we don't care about.  */
3417
3418   cfa_off = -16;
3419   for (regno = GR_ARG_FIRST + 7; n_varargs > 0; --n_varargs, --regno)
3420     {
3421       reg = gen_rtx_REG (DImode, regno);
3422       do_spill (gen_gr_spill, reg, cfa_off += 8, NULL_RTX);
3423     }
3424
3425   /* Locate the bottom of the register save area.  */
3426   cfa_off = (current_frame_info.spill_cfa_off
3427              + current_frame_info.spill_size
3428              + current_frame_info.extra_spill_size);
3429
3430   /* Save the predicate register block either in a register or in memory.  */
3431   if (TEST_HARD_REG_BIT (current_frame_info.mask, PR_REG (0)))
3432     {
3433       reg = gen_rtx_REG (DImode, PR_REG (0));
3434       if (current_frame_info.r[reg_save_pr] != 0)
3435         {
3436           alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_pr]);
3437           reg_emitted (reg_save_pr);
3438           insn = emit_move_insn (alt_reg, reg);
3439
3440           /* ??? Denote pr spill/fill by a DImode move that modifies all
3441              64 hard registers.  */
3442           RTX_FRAME_RELATED_P (insn) = 1;
3443           add_reg_note (insn, REG_CFA_REGISTER, NULL_RTX);
3444
3445           /* Even if we're not going to generate an epilogue, we still
3446              need to save the register so that EH works.  */
3447           if (! epilogue_p)
3448             emit_insn (gen_prologue_use (alt_reg));
3449         }
3450       else
3451         {
3452           alt_regno = next_scratch_gr_reg ();
3453           alt_reg = gen_rtx_REG (DImode, alt_regno);
3454           insn = emit_move_insn (alt_reg, reg);
3455           do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
3456           cfa_off -= 8;
3457         }
3458     }
3459
3460   /* Handle AR regs in numerical order.  All of them get special handling.  */
3461   if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM)
3462       && current_frame_info.r[reg_save_ar_unat] == 0)
3463     {
3464       reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
3465       do_spill (gen_movdi_x, ar_unat_save_reg, cfa_off, reg);
3466       cfa_off -= 8;
3467     }
3468
3469   /* The alloc insn already copied ar.pfs into a general register.  The
3470      only thing we have to do now is copy that register to a stack slot
3471      if we'd not allocated a local register for the job.  */
3472   if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_PFS_REGNUM)
3473       && current_frame_info.r[reg_save_ar_pfs] == 0)
3474     {
3475       reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
3476       do_spill (gen_movdi_x, ar_pfs_save_reg, cfa_off, reg);
3477       cfa_off -= 8;
3478     }
3479
3480   if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_LC_REGNUM))
3481     {
3482       reg = gen_rtx_REG (DImode, AR_LC_REGNUM);
3483       if (current_frame_info.r[reg_save_ar_lc] != 0)
3484         {
3485           alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_ar_lc]);
3486           reg_emitted (reg_save_ar_lc);
3487           insn = emit_move_insn (alt_reg, reg);
3488           RTX_FRAME_RELATED_P (insn) = 1;
3489           add_reg_note (insn, REG_CFA_REGISTER, NULL_RTX);
3490
3491           /* Even if we're not going to generate an epilogue, we still
3492              need to save the register so that EH works.  */
3493           if (! epilogue_p)
3494             emit_insn (gen_prologue_use (alt_reg));
3495         }
3496       else
3497         {
3498           alt_regno = next_scratch_gr_reg ();
3499           alt_reg = gen_rtx_REG (DImode, alt_regno);
3500           emit_move_insn (alt_reg, reg);
3501           do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
3502           cfa_off -= 8;
3503         }
3504     }
3505
3506   /* Save the return pointer.  */
3507   if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
3508     {
3509       reg = gen_rtx_REG (DImode, BR_REG (0));
3510       if (current_frame_info.r[reg_save_b0] != 0)
3511         {
3512           alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_b0]);
3513           reg_emitted (reg_save_b0);
3514           insn = emit_move_insn (alt_reg, reg);
3515           RTX_FRAME_RELATED_P (insn) = 1;
3516           add_reg_note (insn, REG_CFA_REGISTER, NULL_RTX);
3517
3518           /* Even if we're not going to generate an epilogue, we still
3519              need to save the register so that EH works.  */
3520           if (! epilogue_p)
3521             emit_insn (gen_prologue_use (alt_reg));
3522         }
3523       else
3524         {
3525           alt_regno = next_scratch_gr_reg ();
3526           alt_reg = gen_rtx_REG (DImode, alt_regno);
3527           emit_move_insn (alt_reg, reg);
3528           do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
3529           cfa_off -= 8;
3530         }
3531     }
3532
3533   if (current_frame_info.r[reg_save_gp])
3534     {
3535       reg_emitted (reg_save_gp);
3536       insn = emit_move_insn (gen_rtx_REG (DImode,
3537                                           current_frame_info.r[reg_save_gp]),
3538                              pic_offset_table_rtx);
3539     }
3540
3541   /* We should now be at the base of the gr/br/fr spill area.  */
3542   gcc_assert (cfa_off == (current_frame_info.spill_cfa_off
3543                           + current_frame_info.spill_size));
3544
3545   /* Spill all general registers.  */
3546   for (regno = GR_REG (1); regno <= GR_REG (31); ++regno)
3547     if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3548       {
3549         reg = gen_rtx_REG (DImode, regno);
3550         do_spill (gen_gr_spill, reg, cfa_off, reg);
3551         cfa_off -= 8;
3552       }
3553
3554   /* Spill the rest of the BR registers.  */
3555   for (regno = BR_REG (1); regno <= BR_REG (7); ++regno)
3556     if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3557       {
3558         alt_regno = next_scratch_gr_reg ();
3559         alt_reg = gen_rtx_REG (DImode, alt_regno);
3560         reg = gen_rtx_REG (DImode, regno);
3561         emit_move_insn (alt_reg, reg);
3562         do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
3563         cfa_off -= 8;
3564       }
3565
3566   /* Align the frame and spill all FR registers.  */
3567   for (regno = FR_REG (2); regno <= FR_REG (127); ++regno)
3568     if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3569       {
3570         gcc_assert (!(cfa_off & 15));
3571         reg = gen_rtx_REG (XFmode, regno);
3572         do_spill (gen_fr_spill_x, reg, cfa_off, reg);
3573         cfa_off -= 16;
3574       }
3575
3576   gcc_assert (cfa_off == current_frame_info.spill_cfa_off);
3577
3578   finish_spill_pointers ();
3579 }
3580
3581 /* Output the textual info surrounding the prologue.  */
3582
3583 void
3584 ia64_start_function (FILE *file, const char *fnname,
3585                      tree decl ATTRIBUTE_UNUSED)
3586 {
3587 #if VMS_DEBUGGING_INFO
3588   if (vms_debug_main
3589       && strncmp (vms_debug_main, fnname, strlen (vms_debug_main)) == 0)
3590     {
3591       targetm.asm_out.globalize_label (asm_out_file, VMS_DEBUG_MAIN_POINTER);
3592       ASM_OUTPUT_DEF (asm_out_file, VMS_DEBUG_MAIN_POINTER, fnname);
3593       dwarf2out_vms_debug_main_pointer ();
3594       vms_debug_main = 0;
3595     }
3596 #endif
3597
3598   fputs ("\t.proc ", file);
3599   assemble_name (file, fnname);
3600   fputc ('\n', file);
3601   ASM_OUTPUT_LABEL (file, fnname);
3602 }
3603
3604 /* Called after register allocation to add any instructions needed for the
3605    epilogue.  Using an epilogue insn is favored compared to putting all of the
3606    instructions in output_function_prologue(), since it allows the scheduler
3607    to intermix instructions with the saves of the caller saved registers.  In
3608    some cases, it might be necessary to emit a barrier instruction as the last
3609    insn to prevent such scheduling.  */
3610
3611 void
3612 ia64_expand_epilogue (int sibcall_p)
3613 {
3614   rtx insn, reg, alt_reg, ar_unat_save_reg;
3615   int regno, alt_regno, cfa_off;
3616
3617   ia64_compute_frame_size (get_frame_size ());
3618
3619   /* If there is a frame pointer, then we use it instead of the stack
3620      pointer, so that the stack pointer does not need to be valid when
3621      the epilogue starts.  See EXIT_IGNORE_STACK.  */
3622   if (frame_pointer_needed)
3623     setup_spill_pointers (current_frame_info.n_spilled,
3624                           hard_frame_pointer_rtx, 0);
3625   else
3626     setup_spill_pointers (current_frame_info.n_spilled, stack_pointer_rtx,
3627                           current_frame_info.total_size);
3628
3629   if (current_frame_info.total_size != 0)
3630     {
3631       /* ??? At this point we must generate a magic insn that appears to
3632          modify the spill iterators and the frame pointer.  This would
3633          allow the most scheduling freedom.  For now, just hard stop.  */
3634       emit_insn (gen_blockage ());
3635     }
3636
3637   /* Locate the bottom of the register save area.  */
3638   cfa_off = (current_frame_info.spill_cfa_off
3639              + current_frame_info.spill_size
3640              + current_frame_info.extra_spill_size);
3641
3642   /* Restore the predicate registers.  */
3643   if (TEST_HARD_REG_BIT (current_frame_info.mask, PR_REG (0)))
3644     {
3645       if (current_frame_info.r[reg_save_pr] != 0)
3646         {
3647           alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_pr]);
3648           reg_emitted (reg_save_pr);
3649         }
3650       else
3651         {
3652           alt_regno = next_scratch_gr_reg ();
3653           alt_reg = gen_rtx_REG (DImode, alt_regno);
3654           do_restore (gen_movdi_x, alt_reg, cfa_off);
3655           cfa_off -= 8;
3656         }
3657       reg = gen_rtx_REG (DImode, PR_REG (0));
3658       emit_move_insn (reg, alt_reg);
3659     }
3660
3661   /* Restore the application registers.  */
3662
3663   /* Load the saved unat from the stack, but do not restore it until
3664      after the GRs have been restored.  */
3665   if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
3666     {
3667       if (current_frame_info.r[reg_save_ar_unat] != 0)
3668         {
3669           ar_unat_save_reg
3670             = gen_rtx_REG (DImode, current_frame_info.r[reg_save_ar_unat]);
3671           reg_emitted (reg_save_ar_unat);
3672         }
3673       else
3674         {
3675           alt_regno = next_scratch_gr_reg ();
3676           ar_unat_save_reg = gen_rtx_REG (DImode, alt_regno);
3677           current_frame_info.gr_used_mask |= 1 << alt_regno;
3678           do_restore (gen_movdi_x, ar_unat_save_reg, cfa_off);
3679           cfa_off -= 8;
3680         }
3681     }
3682   else
3683     ar_unat_save_reg = NULL_RTX;
3684
3685   if (current_frame_info.r[reg_save_ar_pfs] != 0)
3686     {
3687       reg_emitted (reg_save_ar_pfs);
3688       alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_ar_pfs]);
3689       reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
3690       emit_move_insn (reg, alt_reg);
3691     }
3692   else if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_PFS_REGNUM))
3693     {
3694       alt_regno = next_scratch_gr_reg ();
3695       alt_reg = gen_rtx_REG (DImode, alt_regno);
3696       do_restore (gen_movdi_x, alt_reg, cfa_off);
3697       cfa_off -= 8;
3698       reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
3699       emit_move_insn (reg, alt_reg);
3700     }
3701
3702   if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_LC_REGNUM))
3703     {
3704       if (current_frame_info.r[reg_save_ar_lc] != 0)
3705         {
3706           alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_ar_lc]);
3707           reg_emitted (reg_save_ar_lc);
3708         }
3709       else
3710         {
3711           alt_regno = next_scratch_gr_reg ();
3712           alt_reg = gen_rtx_REG (DImode, alt_regno);
3713           do_restore (gen_movdi_x, alt_reg, cfa_off);
3714           cfa_off -= 8;
3715         }
3716       reg = gen_rtx_REG (DImode, AR_LC_REGNUM);
3717       emit_move_insn (reg, alt_reg);
3718     }
3719
3720   /* Restore the return pointer.  */
3721   if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
3722     {
3723       if (current_frame_info.r[reg_save_b0] != 0)
3724         {
3725          alt_reg = gen_rtx_REG (DImode, current_frame_info.r[reg_save_b0]);
3726          reg_emitted (reg_save_b0);
3727         }
3728       else
3729         {
3730           alt_regno = next_scratch_gr_reg ();
3731           alt_reg = gen_rtx_REG (DImode, alt_regno);
3732           do_restore (gen_movdi_x, alt_reg, cfa_off);
3733           cfa_off -= 8;
3734         }
3735       reg = gen_rtx_REG (DImode, BR_REG (0));
3736       emit_move_insn (reg, alt_reg);
3737     }
3738
3739   /* We should now be at the base of the gr/br/fr spill area.  */
3740   gcc_assert (cfa_off == (current_frame_info.spill_cfa_off
3741                           + current_frame_info.spill_size));
3742
3743   /* The GP may be stored on the stack in the prologue, but it's
3744      never restored in the epilogue.  Skip the stack slot.  */
3745   if (TEST_HARD_REG_BIT (current_frame_info.mask, GR_REG (1)))
3746     cfa_off -= 8;
3747
3748   /* Restore all general registers.  */
3749   for (regno = GR_REG (2); regno <= GR_REG (31); ++regno)
3750     if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3751       {
3752         reg = gen_rtx_REG (DImode, regno);
3753         do_restore (gen_gr_restore, reg, cfa_off);
3754         cfa_off -= 8;
3755       }
3756
3757   /* Restore the branch registers.  */
3758   for (regno = BR_REG (1); regno <= BR_REG (7); ++regno)
3759     if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3760       {
3761         alt_regno = next_scratch_gr_reg ();
3762         alt_reg = gen_rtx_REG (DImode, alt_regno);
3763         do_restore (gen_movdi_x, alt_reg, cfa_off);
3764         cfa_off -= 8;
3765         reg = gen_rtx_REG (DImode, regno);
3766         emit_move_insn (reg, alt_reg);
3767       }
3768
3769   /* Restore floating point registers.  */
3770   for (regno = FR_REG (2); regno <= FR_REG (127); ++regno)
3771     if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3772       {
3773         gcc_assert (!(cfa_off & 15));
3774         reg = gen_rtx_REG (XFmode, regno);
3775         do_restore (gen_fr_restore_x, reg, cfa_off);
3776         cfa_off -= 16;
3777       }
3778
3779   /* Restore ar.unat for real.  */
3780   if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
3781     {
3782       reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
3783       emit_move_insn (reg, ar_unat_save_reg);
3784     }
3785
3786   gcc_assert (cfa_off == current_frame_info.spill_cfa_off);
3787
3788   finish_spill_pointers ();
3789
3790   if (current_frame_info.total_size
3791       || cfun->machine->ia64_eh_epilogue_sp
3792       || frame_pointer_needed)
3793     {
3794       /* ??? At this point we must generate a magic insn that appears to
3795          modify the spill iterators, the stack pointer, and the frame
3796          pointer.  This would allow the most scheduling freedom.  For now,
3797          just hard stop.  */
3798       emit_insn (gen_blockage ());
3799     }
3800
3801   if (cfun->machine->ia64_eh_epilogue_sp)
3802     emit_move_insn (stack_pointer_rtx, cfun->machine->ia64_eh_epilogue_sp);
3803   else if (frame_pointer_needed)
3804     {
3805       insn = emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
3806       RTX_FRAME_RELATED_P (insn) = 1;
3807       add_reg_note (insn, REG_CFA_ADJUST_CFA, NULL);
3808     }
3809   else if (current_frame_info.total_size)
3810     {
3811       rtx offset, frame_size_rtx;
3812
3813       frame_size_rtx = GEN_INT (current_frame_info.total_size);
3814       if (satisfies_constraint_I (frame_size_rtx))
3815         offset = frame_size_rtx;
3816       else
3817         {
3818           regno = next_scratch_gr_reg ();
3819           offset = gen_rtx_REG (DImode, regno);
3820           emit_move_insn (offset, frame_size_rtx);
3821         }
3822
3823       insn = emit_insn (gen_adddi3 (stack_pointer_rtx, stack_pointer_rtx,
3824                                     offset));
3825
3826       RTX_FRAME_RELATED_P (insn) = 1;
3827       add_reg_note (insn, REG_CFA_ADJUST_CFA,
3828                     gen_rtx_SET (VOIDmode,
3829                                  stack_pointer_rtx,
3830                                  gen_rtx_PLUS (DImode,
3831                                                stack_pointer_rtx,
3832                                                frame_size_rtx)));
3833     }
3834
3835   if (cfun->machine->ia64_eh_epilogue_bsp)
3836     emit_insn (gen_set_bsp (cfun->machine->ia64_eh_epilogue_bsp));
3837
3838   if (! sibcall_p)
3839     emit_jump_insn (gen_return_internal (gen_rtx_REG (DImode, BR_REG (0))));
3840   else
3841     {
3842       int fp = GR_REG (2);
3843       /* We need a throw away register here, r0 and r1 are reserved,
3844          so r2 is the first available call clobbered register.  If
3845          there was a frame_pointer register, we may have swapped the
3846          names of r2 and HARD_FRAME_POINTER_REGNUM, so we have to make
3847          sure we're using the string "r2" when emitting the register
3848          name for the assembler.  */
3849       if (current_frame_info.r[reg_fp] 
3850           && current_frame_info.r[reg_fp] == GR_REG (2))
3851         fp = HARD_FRAME_POINTER_REGNUM;
3852
3853       /* We must emit an alloc to force the input registers to become output
3854          registers.  Otherwise, if the callee tries to pass its parameters
3855          through to another call without an intervening alloc, then these
3856          values get lost.  */
3857       /* ??? We don't need to preserve all input registers.  We only need to
3858          preserve those input registers used as arguments to the sibling call.
3859          It is unclear how to compute that number here.  */
3860       if (current_frame_info.n_input_regs != 0)
3861         {
3862           rtx n_inputs = GEN_INT (current_frame_info.n_input_regs);
3863           insn = emit_insn (gen_alloc (gen_rtx_REG (DImode, fp),
3864                                 const0_rtx, const0_rtx,
3865                                 n_inputs, const0_rtx));
3866           RTX_FRAME_RELATED_P (insn) = 1;
3867         }
3868     }
3869 }
3870
3871 /* Return 1 if br.ret can do all the work required to return from a
3872    function.  */
3873
3874 int
3875 ia64_direct_return (void)
3876 {
3877   if (reload_completed && ! frame_pointer_needed)
3878     {
3879       ia64_compute_frame_size (get_frame_size ());
3880
3881       return (current_frame_info.total_size == 0
3882               && current_frame_info.n_spilled == 0
3883               && current_frame_info.r[reg_save_b0] == 0
3884               && current_frame_info.r[reg_save_pr] == 0
3885               && current_frame_info.r[reg_save_ar_pfs] == 0
3886               && current_frame_info.r[reg_save_ar_unat] == 0
3887               && current_frame_info.r[reg_save_ar_lc] == 0);
3888     }
3889   return 0;
3890 }
3891
3892 /* Return the magic cookie that we use to hold the return address
3893    during early compilation.  */
3894
3895 rtx
3896 ia64_return_addr_rtx (HOST_WIDE_INT count, rtx frame ATTRIBUTE_UNUSED)
3897 {
3898   if (count != 0)
3899     return NULL;
3900   return gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_RET_ADDR);
3901 }
3902
3903 /* Split this value after reload, now that we know where the return
3904    address is saved.  */
3905
3906 void
3907 ia64_split_return_addr_rtx (rtx dest)
3908 {
3909   rtx src;
3910
3911   if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
3912     {
3913       if (current_frame_info.r[reg_save_b0] != 0)
3914         {
3915           src = gen_rtx_REG (DImode, current_frame_info.r[reg_save_b0]);
3916           reg_emitted (reg_save_b0);
3917         }
3918       else
3919         {
3920           HOST_WIDE_INT off;
3921           unsigned int regno;
3922           rtx off_r;
3923
3924           /* Compute offset from CFA for BR0.  */
3925           /* ??? Must be kept in sync with ia64_expand_prologue.  */
3926           off = (current_frame_info.spill_cfa_off
3927                  + current_frame_info.spill_size);
3928           for (regno = GR_REG (1); regno <= GR_REG (31); ++regno)
3929             if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3930               off -= 8;
3931
3932           /* Convert CFA offset to a register based offset.  */
3933           if (frame_pointer_needed)
3934             src = hard_frame_pointer_rtx;
3935           else
3936             {
3937               src = stack_pointer_rtx;
3938               off += current_frame_info.total_size;
3939             }
3940
3941           /* Load address into scratch register.  */
3942           off_r = GEN_INT (off);
3943           if (satisfies_constraint_I (off_r))
3944             emit_insn (gen_adddi3 (dest, src, off_r));
3945           else
3946             {
3947               emit_move_insn (dest, off_r);
3948               emit_insn (gen_adddi3 (dest, src, dest));
3949             }
3950
3951           src = gen_rtx_MEM (Pmode, dest);
3952         }
3953     }
3954   else
3955     src = gen_rtx_REG (DImode, BR_REG (0));
3956
3957   emit_move_insn (dest, src);
3958 }
3959
3960 int
3961 ia64_hard_regno_rename_ok (int from, int to)
3962 {
3963   /* Don't clobber any of the registers we reserved for the prologue.  */
3964   unsigned int r;
3965
3966   for (r = reg_fp; r <= reg_save_ar_lc; r++)
3967     if (to == current_frame_info.r[r] 
3968         || from == current_frame_info.r[r]
3969         || to == emitted_frame_related_regs[r]
3970         || from == emitted_frame_related_regs[r])
3971       return 0;
3972
3973   /* Don't use output registers outside the register frame.  */
3974   if (OUT_REGNO_P (to) && to >= OUT_REG (current_frame_info.n_output_regs))
3975     return 0;
3976
3977   /* Retain even/oddness on predicate register pairs.  */
3978   if (PR_REGNO_P (from) && PR_REGNO_P (to))
3979     return (from & 1) == (to & 1);
3980
3981   return 1;
3982 }
3983
3984 /* Target hook for assembling integer objects.  Handle word-sized
3985    aligned objects and detect the cases when @fptr is needed.  */
3986
3987 static bool
3988 ia64_assemble_integer (rtx x, unsigned int size, int aligned_p)
3989 {
3990   if (size == POINTER_SIZE / BITS_PER_UNIT
3991       && !(TARGET_NO_PIC || TARGET_AUTO_PIC)
3992       && GET_CODE (x) == SYMBOL_REF
3993       && SYMBOL_REF_FUNCTION_P (x))
3994     {
3995       static const char * const directive[2][2] = {
3996           /* 64-bit pointer */  /* 32-bit pointer */
3997         { "\tdata8.ua\t@fptr(", "\tdata4.ua\t@fptr("},  /* unaligned */
3998         { "\tdata8\t@fptr(",    "\tdata4\t@fptr("}      /* aligned */
3999       };
4000       fputs (directive[(aligned_p != 0)][POINTER_SIZE == 32], asm_out_file);
4001       output_addr_const (asm_out_file, x);
4002       fputs (")\n", asm_out_file);
4003       return true;
4004     }
4005   return default_assemble_integer (x, size, aligned_p);
4006 }
4007
4008 /* Emit the function prologue.  */
4009
4010 static void
4011 ia64_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
4012 {
4013   int mask, grsave, grsave_prev;
4014
4015   if (current_frame_info.need_regstk)
4016     fprintf (file, "\t.regstk %d, %d, %d, %d\n",
4017              current_frame_info.n_input_regs,
4018              current_frame_info.n_local_regs,
4019              current_frame_info.n_output_regs,
4020              current_frame_info.n_rotate_regs);
4021
4022   if (ia64_except_unwind_info (&global_options) != UI_TARGET)
4023     return;
4024
4025   /* Emit the .prologue directive.  */
4026
4027   mask = 0;
4028   grsave = grsave_prev = 0;
4029   if (current_frame_info.r[reg_save_b0] != 0)
4030     {
4031       mask |= 8;
4032       grsave = grsave_prev = current_frame_info.r[reg_save_b0];
4033     }
4034   if (current_frame_info.r[reg_save_ar_pfs] != 0
4035       && (grsave_prev == 0
4036           || current_frame_info.r[reg_save_ar_pfs] == grsave_prev + 1))
4037     {
4038       mask |= 4;
4039       if (grsave_prev == 0)
4040         grsave = current_frame_info.r[reg_save_ar_pfs];
4041       grsave_prev = current_frame_info.r[reg_save_ar_pfs];
4042     }
4043   if (current_frame_info.r[reg_fp] != 0
4044       && (grsave_prev == 0
4045           || current_frame_info.r[reg_fp] == grsave_prev + 1))
4046     {
4047       mask |= 2;
4048       if (grsave_prev == 0)
4049         grsave = HARD_FRAME_POINTER_REGNUM;
4050       grsave_prev = current_frame_info.r[reg_fp];
4051     }
4052   if (current_frame_info.r[reg_save_pr] != 0
4053       && (grsave_prev == 0
4054           || current_frame_info.r[reg_save_pr] == grsave_prev + 1))
4055     {
4056       mask |= 1;
4057       if (grsave_prev == 0)
4058         grsave = current_frame_info.r[reg_save_pr];
4059     }
4060
4061   if (mask && TARGET_GNU_AS)
4062     fprintf (file, "\t.prologue %d, %d\n", mask,
4063              ia64_dbx_register_number (grsave));
4064   else
4065     fputs ("\t.prologue\n", file);
4066
4067   /* Emit a .spill directive, if necessary, to relocate the base of
4068      the register spill area.  */
4069   if (current_frame_info.spill_cfa_off != -16)
4070     fprintf (file, "\t.spill %ld\n",
4071              (long) (current_frame_info.spill_cfa_off
4072                      + current_frame_info.spill_size));
4073 }
4074
4075 /* Emit the .body directive at the scheduled end of the prologue.  */
4076
4077 static void
4078 ia64_output_function_end_prologue (FILE *file)
4079 {
4080   if (ia64_except_unwind_info (&global_options) != UI_TARGET)
4081     return;
4082
4083   fputs ("\t.body\n", file);
4084 }
4085
4086 /* Emit the function epilogue.  */
4087
4088 static void
4089 ia64_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
4090                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
4091 {
4092   int i;
4093
4094   if (current_frame_info.r[reg_fp])
4095     {
4096       const char *tmp = reg_names[HARD_FRAME_POINTER_REGNUM];
4097       reg_names[HARD_FRAME_POINTER_REGNUM]
4098         = reg_names[current_frame_info.r[reg_fp]];
4099       reg_names[current_frame_info.r[reg_fp]] = tmp;
4100       reg_emitted (reg_fp);
4101     }
4102   if (! TARGET_REG_NAMES)
4103     {
4104       for (i = 0; i < current_frame_info.n_input_regs; i++)
4105         reg_names[IN_REG (i)] = ia64_input_reg_names[i];
4106       for (i = 0; i < current_frame_info.n_local_regs; i++)
4107         reg_names[LOC_REG (i)] = ia64_local_reg_names[i];
4108       for (i = 0; i < current_frame_info.n_output_regs; i++)
4109         reg_names[OUT_REG (i)] = ia64_output_reg_names[i];
4110     }
4111
4112   current_frame_info.initialized = 0;
4113 }
4114
4115 int
4116 ia64_dbx_register_number (int regno)
4117 {
4118   /* In ia64_expand_prologue we quite literally renamed the frame pointer
4119      from its home at loc79 to something inside the register frame.  We
4120      must perform the same renumbering here for the debug info.  */
4121   if (current_frame_info.r[reg_fp])
4122     {
4123       if (regno == HARD_FRAME_POINTER_REGNUM)
4124         regno = current_frame_info.r[reg_fp];
4125       else if (regno == current_frame_info.r[reg_fp])
4126         regno = HARD_FRAME_POINTER_REGNUM;
4127     }
4128
4129   if (IN_REGNO_P (regno))
4130     return 32 + regno - IN_REG (0);
4131   else if (LOC_REGNO_P (regno))
4132     return 32 + current_frame_info.n_input_regs + regno - LOC_REG (0);
4133   else if (OUT_REGNO_P (regno))
4134     return (32 + current_frame_info.n_input_regs
4135             + current_frame_info.n_local_regs + regno - OUT_REG (0));
4136   else
4137     return regno;
4138 }
4139
4140 /* Implement TARGET_TRAMPOLINE_INIT.
4141
4142    The trampoline should set the static chain pointer to value placed
4143    into the trampoline and should branch to the specified routine.
4144    To make the normal indirect-subroutine calling convention work,
4145    the trampoline must look like a function descriptor; the first
4146    word being the target address and the second being the target's
4147    global pointer.
4148
4149    We abuse the concept of a global pointer by arranging for it
4150    to point to the data we need to load.  The complete trampoline
4151    has the following form:
4152
4153                 +-------------------+ \
4154         TRAMP:  | __ia64_trampoline | |
4155                 +-------------------+  > fake function descriptor
4156                 | TRAMP+16          | |
4157                 +-------------------+ /
4158                 | target descriptor |
4159                 +-------------------+
4160                 | static link       |
4161                 +-------------------+
4162 */
4163
4164 static void
4165 ia64_trampoline_init (rtx m_tramp, tree fndecl, rtx static_chain)
4166 {
4167   rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
4168   rtx addr, addr_reg, tramp, eight = GEN_INT (8);
4169
4170   /* The Intel assembler requires that the global __ia64_trampoline symbol
4171      be declared explicitly */
4172   if (!TARGET_GNU_AS)
4173     {
4174       static bool declared_ia64_trampoline = false;
4175
4176       if (!declared_ia64_trampoline)
4177         {
4178           declared_ia64_trampoline = true;
4179           (*targetm.asm_out.globalize_label) (asm_out_file,
4180                                               "__ia64_trampoline");
4181         }
4182     }
4183
4184   /* Make sure addresses are Pmode even if we are in ILP32 mode. */
4185   addr = convert_memory_address (Pmode, XEXP (m_tramp, 0));
4186   fnaddr = convert_memory_address (Pmode, fnaddr);
4187   static_chain = convert_memory_address (Pmode, static_chain);
4188
4189   /* Load up our iterator.  */
4190   addr_reg = copy_to_reg (addr);
4191   m_tramp = adjust_automodify_address (m_tramp, Pmode, addr_reg, 0);
4192
4193   /* The first two words are the fake descriptor:
4194      __ia64_trampoline, ADDR+16.  */
4195   tramp = gen_rtx_SYMBOL_REF (Pmode, "__ia64_trampoline");
4196   if (TARGET_ABI_OPEN_VMS)
4197     {
4198       /* HP decided to break the ELF ABI on VMS (to deal with an ambiguity
4199          in the Macro-32 compiler) and changed the semantics of the LTOFF22
4200          relocation against function symbols to make it identical to the
4201          LTOFF_FPTR22 relocation.  Emit the latter directly to stay within
4202          strict ELF and dereference to get the bare code address.  */
4203       rtx reg = gen_reg_rtx (Pmode);
4204       SYMBOL_REF_FLAGS (tramp) |= SYMBOL_FLAG_FUNCTION;
4205       emit_move_insn (reg, tramp);
4206       emit_move_insn (reg, gen_rtx_MEM (Pmode, reg));
4207       tramp = reg;
4208    }
4209   emit_move_insn (m_tramp, tramp);
4210   emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
4211   m_tramp = adjust_automodify_address (m_tramp, VOIDmode, NULL, 8);
4212
4213   emit_move_insn (m_tramp, force_reg (Pmode, plus_constant (addr, 16)));
4214   emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
4215   m_tramp = adjust_automodify_address (m_tramp, VOIDmode, NULL, 8);
4216
4217   /* The third word is the target descriptor.  */
4218   emit_move_insn (m_tramp, force_reg (Pmode, fnaddr));
4219   emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
4220   m_tramp = adjust_automodify_address (m_tramp, VOIDmode, NULL, 8);
4221
4222   /* The fourth word is the static chain.  */
4223   emit_move_insn (m_tramp, static_chain);
4224 }
4225 \f
4226 /* Do any needed setup for a variadic function.  CUM has not been updated
4227    for the last named argument which has type TYPE and mode MODE.
4228
4229    We generate the actual spill instructions during prologue generation.  */
4230
4231 static void
4232 ia64_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4233                              tree type, int * pretend_size,
4234                              int second_time ATTRIBUTE_UNUSED)
4235 {
4236   CUMULATIVE_ARGS next_cum = *cum;
4237
4238   /* Skip the current argument.  */
4239   ia64_function_arg_advance (&next_cum, mode, type, 1);
4240
4241   if (next_cum.words < MAX_ARGUMENT_SLOTS)
4242     {
4243       int n = MAX_ARGUMENT_SLOTS - next_cum.words;
4244       *pretend_size = n * UNITS_PER_WORD;
4245       cfun->machine->n_varargs = n;
4246     }
4247 }
4248
4249 /* Check whether TYPE is a homogeneous floating point aggregate.  If
4250    it is, return the mode of the floating point type that appears
4251    in all leafs.  If it is not, return VOIDmode.
4252
4253    An aggregate is a homogeneous floating point aggregate is if all
4254    fields/elements in it have the same floating point type (e.g,
4255    SFmode).  128-bit quad-precision floats are excluded.
4256
4257    Variable sized aggregates should never arrive here, since we should
4258    have already decided to pass them by reference.  Top-level zero-sized
4259    aggregates are excluded because our parallels crash the middle-end.  */
4260
4261 static enum machine_mode
4262 hfa_element_mode (const_tree type, bool nested)
4263 {
4264   enum machine_mode element_mode = VOIDmode;
4265   enum machine_mode mode;
4266   enum tree_code code = TREE_CODE (type);
4267   int know_element_mode = 0;
4268   tree t;
4269
4270   if (!nested && (!TYPE_SIZE (type) || integer_zerop (TYPE_SIZE (type))))
4271     return VOIDmode;
4272
4273   switch (code)
4274     {
4275     case VOID_TYPE:     case INTEGER_TYPE:      case ENUMERAL_TYPE:
4276     case BOOLEAN_TYPE:  case POINTER_TYPE:
4277     case OFFSET_TYPE:   case REFERENCE_TYPE:    case METHOD_TYPE:
4278     case LANG_TYPE:             case FUNCTION_TYPE:
4279       return VOIDmode;
4280
4281       /* Fortran complex types are supposed to be HFAs, so we need to handle
4282          gcc's COMPLEX_TYPEs as HFAs.  We need to exclude the integral complex
4283          types though.  */
4284     case COMPLEX_TYPE:
4285       if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_COMPLEX_FLOAT
4286           && TYPE_MODE (type) != TCmode)
4287         return GET_MODE_INNER (TYPE_MODE (type));
4288       else
4289         return VOIDmode;
4290
4291     case REAL_TYPE:
4292       /* We want to return VOIDmode for raw REAL_TYPEs, but the actual
4293          mode if this is contained within an aggregate.  */
4294       if (nested && TYPE_MODE (type) != TFmode)
4295         return TYPE_MODE (type);
4296       else
4297         return VOIDmode;
4298
4299     case ARRAY_TYPE:
4300       return hfa_element_mode (TREE_TYPE (type), 1);
4301
4302     case RECORD_TYPE:
4303     case UNION_TYPE:
4304     case QUAL_UNION_TYPE:
4305       for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
4306         {
4307           if (TREE_CODE (t) != FIELD_DECL)
4308             continue;
4309
4310           mode = hfa_element_mode (TREE_TYPE (t), 1);
4311           if (know_element_mode)
4312             {
4313               if (mode != element_mode)
4314                 return VOIDmode;
4315             }
4316           else if (GET_MODE_CLASS (mode) != MODE_FLOAT)
4317             return VOIDmode;
4318           else
4319             {
4320               know_element_mode = 1;
4321               element_mode = mode;
4322             }
4323         }
4324       return element_mode;
4325
4326     default:
4327       /* If we reach here, we probably have some front-end specific type
4328          that the backend doesn't know about.  This can happen via the
4329          aggregate_value_p call in init_function_start.  All we can do is
4330          ignore unknown tree types.  */
4331       return VOIDmode;
4332     }
4333
4334   return VOIDmode;
4335 }
4336
4337 /* Return the number of words required to hold a quantity of TYPE and MODE
4338    when passed as an argument.  */
4339 static int
4340 ia64_function_arg_words (const_tree type, enum machine_mode mode)
4341 {
4342   int words;
4343
4344   if (mode == BLKmode)
4345     words = int_size_in_bytes (type);
4346   else
4347     words = GET_MODE_SIZE (mode);
4348
4349   return (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;  /* round up */
4350 }
4351
4352 /* Return the number of registers that should be skipped so the current
4353    argument (described by TYPE and WORDS) will be properly aligned.
4354
4355    Integer and float arguments larger than 8 bytes start at the next
4356    even boundary.  Aggregates larger than 8 bytes start at the next
4357    even boundary if the aggregate has 16 byte alignment.  Note that
4358    in the 32-bit ABI, TImode and TFmode have only 8-byte alignment
4359    but are still to be aligned in registers.
4360
4361    ??? The ABI does not specify how to handle aggregates with
4362    alignment from 9 to 15 bytes, or greater than 16.  We handle them
4363    all as if they had 16 byte alignment.  Such aggregates can occur
4364    only if gcc extensions are used.  */
4365 static int
4366 ia64_function_arg_offset (const CUMULATIVE_ARGS *cum,
4367                           const_tree type, int words)
4368 {
4369   /* No registers are skipped on VMS.  */
4370   if (TARGET_ABI_OPEN_VMS || (cum->words & 1) == 0)
4371     return 0;
4372
4373   if (type
4374       && TREE_CODE (type) != INTEGER_TYPE
4375       && TREE_CODE (type) != REAL_TYPE)
4376     return TYPE_ALIGN (type) > 8 * BITS_PER_UNIT;
4377   else
4378     return words > 1;
4379 }
4380
4381 /* Return rtx for register where argument is passed, or zero if it is passed
4382    on the stack.  */
4383 /* ??? 128-bit quad-precision floats are always passed in general
4384    registers.  */
4385
4386 static rtx
4387 ia64_function_arg_1 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
4388                      const_tree type, bool named, bool incoming)
4389 {
4390   int basereg = (incoming ? GR_ARG_FIRST : AR_ARG_FIRST);
4391   int words = ia64_function_arg_words (type, mode);
4392   int offset = ia64_function_arg_offset (cum, type, words);
4393   enum machine_mode hfa_mode = VOIDmode;
4394
4395   /* For OPEN VMS, emit the instruction setting up the argument register here,
4396      when we know this will be together with the other arguments setup related
4397      insns.  This is not the conceptually best place to do this, but this is
4398      the easiest as we have convenient access to cumulative args info.  */
4399
4400   if (TARGET_ABI_OPEN_VMS && mode == VOIDmode && type == void_type_node
4401       && named == 1)
4402     {
4403       unsigned HOST_WIDE_INT regval = cum->words;
4404       int i;
4405
4406       for (i = 0; i < 8; i++)
4407         regval |= ((int) cum->atypes[i]) << (i * 3 + 8);
4408
4409       emit_move_insn (gen_rtx_REG (DImode, GR_REG (25)),
4410                       GEN_INT (regval));
4411     }
4412
4413   /* If all argument slots are used, then it must go on the stack.  */
4414   if (cum->words + offset >= MAX_ARGUMENT_SLOTS)
4415     return 0;
4416
4417   /* Check for and handle homogeneous FP aggregates.  */
4418   if (type)
4419     hfa_mode = hfa_element_mode (type, 0);
4420
4421   /* Unnamed prototyped hfas are passed as usual.  Named prototyped hfas
4422      and unprototyped hfas are passed specially.  */
4423   if (hfa_mode != VOIDmode && (! cum->prototype || named))
4424     {
4425       rtx loc[16];
4426       int i = 0;
4427       int fp_regs = cum->fp_regs;
4428       int int_regs = cum->words + offset;
4429       int hfa_size = GET_MODE_SIZE (hfa_mode);
4430       int byte_size;
4431       int args_byte_size;
4432
4433       /* If prototyped, pass it in FR regs then GR regs.
4434          If not prototyped, pass it in both FR and GR regs.
4435
4436          If this is an SFmode aggregate, then it is possible to run out of
4437          FR regs while GR regs are still left.  In that case, we pass the
4438          remaining part in the GR regs.  */
4439
4440       /* Fill the FP regs.  We do this always.  We stop if we reach the end
4441          of the argument, the last FP register, or the last argument slot.  */
4442
4443       byte_size = ((mode == BLKmode)
4444                    ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
4445       args_byte_size = int_regs * UNITS_PER_WORD;
4446       offset = 0;
4447       for (; (offset < byte_size && fp_regs < MAX_ARGUMENT_SLOTS
4448               && args_byte_size < (MAX_ARGUMENT_SLOTS * UNITS_PER_WORD)); i++)
4449         {
4450           loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
4451                                       gen_rtx_REG (hfa_mode, (FR_ARG_FIRST
4452                                                               + fp_regs)),
4453                                       GEN_INT (offset));
4454           offset += hfa_size;
4455           args_byte_size += hfa_size;
4456           fp_regs++;
4457         }
4458
4459       /* If no prototype, then the whole thing must go in GR regs.  */
4460       if (! cum->prototype)
4461         offset = 0;
4462       /* If this is an SFmode aggregate, then we might have some left over
4463          that needs to go in GR regs.  */
4464       else if (byte_size != offset)
4465         int_regs += offset / UNITS_PER_WORD;
4466
4467       /* Fill in the GR regs.  We must use DImode here, not the hfa mode.  */
4468
4469       for (; offset < byte_size && int_regs < MAX_ARGUMENT_SLOTS; i++)
4470         {
4471           enum machine_mode gr_mode = DImode;
4472           unsigned int gr_size;
4473
4474           /* If we have an odd 4 byte hunk because we ran out of FR regs,
4475              then this goes in a GR reg left adjusted/little endian, right
4476              adjusted/big endian.  */
4477           /* ??? Currently this is handled wrong, because 4-byte hunks are
4478              always right adjusted/little endian.  */
4479           if (offset & 0x4)
4480             gr_mode = SImode;
4481           /* If we have an even 4 byte hunk because the aggregate is a
4482              multiple of 4 bytes in size, then this goes in a GR reg right
4483              adjusted/little endian.  */
4484           else if (byte_size - offset == 4)
4485             gr_mode = SImode;
4486
4487           loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
4488                                       gen_rtx_REG (gr_mode, (basereg
4489                                                              + int_regs)),
4490                                       GEN_INT (offset));
4491
4492           gr_size = GET_MODE_SIZE (gr_mode);
4493           offset += gr_size;
4494           if (gr_size == UNITS_PER_WORD
4495               || (gr_size < UNITS_PER_WORD && offset % UNITS_PER_WORD == 0))
4496             int_regs++;
4497           else if (gr_size > UNITS_PER_WORD)
4498             int_regs += gr_size / UNITS_PER_WORD;
4499         }
4500       return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
4501     }
4502   
4503   /* On OpenVMS variable argument is either in Rn or Fn.  */
4504   else if (TARGET_ABI_OPEN_VMS && named == 0)
4505     {
4506       if (FLOAT_MODE_P (mode))
4507         return gen_rtx_REG (mode, FR_ARG_FIRST + cum->words);
4508       else
4509         return gen_rtx_REG (mode, basereg + cum->words);
4510     }
4511
4512   /* Integral and aggregates go in general registers.  If we have run out of
4513      FR registers, then FP values must also go in general registers.  This can
4514      happen when we have a SFmode HFA.  */
4515   else if (mode == TFmode || mode == TCmode
4516            || (! FLOAT_MODE_P (mode) || cum->fp_regs == MAX_ARGUMENT_SLOTS))
4517     {
4518       int byte_size = ((mode == BLKmode)
4519                        ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
4520       if (BYTES_BIG_ENDIAN
4521         && (mode == BLKmode || (type && AGGREGATE_TYPE_P (type)))
4522         && byte_size < UNITS_PER_WORD
4523         && byte_size > 0)
4524         {
4525           rtx gr_reg = gen_rtx_EXPR_LIST (VOIDmode,
4526                                           gen_rtx_REG (DImode,
4527                                                        (basereg + cum->words
4528                                                         + offset)),
4529                                           const0_rtx);
4530           return gen_rtx_PARALLEL (mode, gen_rtvec (1, gr_reg));
4531         }
4532       else
4533         return gen_rtx_REG (mode, basereg + cum->words + offset);
4534
4535     }
4536
4537   /* If there is a prototype, then FP values go in a FR register when
4538      named, and in a GR register when unnamed.  */
4539   else if (cum->prototype)
4540     {
4541       if (named)
4542         return gen_rtx_REG (mode, FR_ARG_FIRST + cum->fp_regs);
4543       /* In big-endian mode, an anonymous SFmode value must be represented
4544          as (parallel:SF [(expr_list (reg:DI n) (const_int 0))]) to force
4545          the value into the high half of the general register.  */
4546       else if (BYTES_BIG_ENDIAN && mode == SFmode)
4547         return gen_rtx_PARALLEL (mode,
4548                  gen_rtvec (1,
4549                    gen_rtx_EXPR_LIST (VOIDmode,
4550                      gen_rtx_REG (DImode, basereg + cum->words + offset),
4551                                       const0_rtx)));
4552       else
4553         return gen_rtx_REG (mode, basereg + cum->words + offset);
4554     }
4555   /* If there is no prototype, then FP values go in both FR and GR
4556      registers.  */
4557   else
4558     {
4559       /* See comment above.  */
4560       enum machine_mode inner_mode =
4561         (BYTES_BIG_ENDIAN && mode == SFmode) ? DImode : mode;
4562
4563       rtx fp_reg = gen_rtx_EXPR_LIST (VOIDmode,
4564                                       gen_rtx_REG (mode, (FR_ARG_FIRST
4565                                                           + cum->fp_regs)),
4566                                       const0_rtx);
4567       rtx gr_reg = gen_rtx_EXPR_LIST (VOIDmode,
4568                                       gen_rtx_REG (inner_mode,
4569                                                    (basereg + cum->words
4570                                                     + offset)),
4571                                       const0_rtx);
4572
4573       return gen_rtx_PARALLEL (mode, gen_rtvec (2, fp_reg, gr_reg));
4574     }
4575 }
4576
4577 /* Implement TARGET_FUNCION_ARG target hook.  */
4578
4579 static rtx
4580 ia64_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4581                    const_tree type, bool named)
4582 {
4583   return ia64_function_arg_1 (cum, mode, type, named, false);
4584 }
4585
4586 /* Implement TARGET_FUNCION_INCOMING_ARG target hook.  */
4587
4588 static rtx
4589 ia64_function_incoming_arg (CUMULATIVE_ARGS *cum,
4590                             enum machine_mode mode,
4591                             const_tree type, bool named)
4592 {
4593   return ia64_function_arg_1 (cum, mode, type, named, true);
4594 }
4595
4596 /* Return number of bytes, at the beginning of the argument, that must be
4597    put in registers.  0 is the argument is entirely in registers or entirely
4598    in memory.  */
4599
4600 static int
4601 ia64_arg_partial_bytes (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4602                         tree type, bool named ATTRIBUTE_UNUSED)
4603 {
4604   int words = ia64_function_arg_words (type, mode);
4605   int offset = ia64_function_arg_offset (cum, type, words);
4606
4607   /* If all argument slots are used, then it must go on the stack.  */
4608   if (cum->words + offset >= MAX_ARGUMENT_SLOTS)
4609     return 0;
4610
4611   /* It doesn't matter whether the argument goes in FR or GR regs.  If
4612      it fits within the 8 argument slots, then it goes entirely in
4613      registers.  If it extends past the last argument slot, then the rest
4614      goes on the stack.  */
4615
4616   if (words + cum->words + offset <= MAX_ARGUMENT_SLOTS)
4617     return 0;
4618
4619   return (MAX_ARGUMENT_SLOTS - cum->words - offset) * UNITS_PER_WORD;
4620 }
4621
4622 /* Return ivms_arg_type based on machine_mode.  */
4623
4624 static enum ivms_arg_type
4625 ia64_arg_type (enum machine_mode mode)
4626 {
4627   switch (mode)
4628     {
4629     case SFmode:
4630       return FS;
4631     case DFmode:
4632       return FT;
4633     default:
4634       return I64;
4635     }
4636 }
4637
4638 /* Update CUM to point after this argument.  This is patterned after
4639    ia64_function_arg.  */
4640
4641 static void
4642 ia64_function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4643                            const_tree type, bool named)
4644 {
4645   int words = ia64_function_arg_words (type, mode);
4646   int offset = ia64_function_arg_offset (cum, type, words);
4647   enum machine_mode hfa_mode = VOIDmode;
4648
4649   /* If all arg slots are already full, then there is nothing to do.  */
4650   if (cum->words >= MAX_ARGUMENT_SLOTS)
4651     {
4652       cum->words += words + offset;
4653       return;
4654     }
4655
4656   cum->atypes[cum->words] = ia64_arg_type (mode);
4657   cum->words += words + offset;
4658
4659   /* Check for and handle homogeneous FP aggregates.  */
4660   if (type)
4661     hfa_mode = hfa_element_mode (type, 0);
4662
4663   /* Unnamed prototyped hfas are passed as usual.  Named prototyped hfas
4664      and unprototyped hfas are passed specially.  */
4665   if (hfa_mode != VOIDmode && (! cum->prototype || named))
4666     {
4667       int fp_regs = cum->fp_regs;
4668       /* This is the original value of cum->words + offset.  */
4669       int int_regs = cum->words - words;
4670       int hfa_size = GET_MODE_SIZE (hfa_mode);
4671       int byte_size;
4672       int args_byte_size;
4673
4674       /* If prototyped, pass it in FR regs then GR regs.
4675          If not prototyped, pass it in both FR and GR regs.
4676
4677          If this is an SFmode aggregate, then it is possible to run out of
4678          FR regs while GR regs are still left.  In that case, we pass the
4679          remaining part in the GR regs.  */
4680
4681       /* Fill the FP regs.  We do this always.  We stop if we reach the end
4682          of the argument, the last FP register, or the last argument slot.  */
4683
4684       byte_size = ((mode == BLKmode)
4685                    ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
4686       args_byte_size = int_regs * UNITS_PER_WORD;
4687       offset = 0;
4688       for (; (offset < byte_size && fp_regs < MAX_ARGUMENT_SLOTS
4689               && args_byte_size < (MAX_ARGUMENT_SLOTS * UNITS_PER_WORD));)
4690         {
4691           offset += hfa_size;
4692           args_byte_size += hfa_size;
4693           fp_regs++;
4694         }
4695
4696       cum->fp_regs = fp_regs;
4697     }
4698
4699   /* On OpenVMS variable argument is either in Rn or Fn.  */
4700   else if (TARGET_ABI_OPEN_VMS && named == 0)
4701     {
4702       cum->int_regs = cum->words;
4703       cum->fp_regs = cum->words;
4704     }
4705
4706   /* Integral and aggregates go in general registers.  So do TFmode FP values.
4707      If we have run out of FR registers, then other FP values must also go in
4708      general registers.  This can happen when we have a SFmode HFA.  */
4709   else if (mode == TFmode || mode == TCmode
4710            || (! FLOAT_MODE_P (mode) || cum->fp_regs == MAX_ARGUMENT_SLOTS))
4711     cum->int_regs = cum->words;
4712
4713   /* If there is a prototype, then FP values go in a FR register when
4714      named, and in a GR register when unnamed.  */
4715   else if (cum->prototype)
4716     {
4717       if (! named)
4718         cum->int_regs = cum->words;
4719       else
4720         /* ??? Complex types should not reach here.  */
4721         cum->fp_regs += (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT ? 2 : 1);
4722     }
4723   /* If there is no prototype, then FP values go in both FR and GR
4724      registers.  */
4725   else
4726     {
4727       /* ??? Complex types should not reach here.  */
4728       cum->fp_regs += (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT ? 2 : 1);
4729       cum->int_regs = cum->words;
4730     }
4731 }
4732
4733 /* Arguments with alignment larger than 8 bytes start at the next even
4734    boundary.  On ILP32 HPUX, TFmode arguments start on next even boundary
4735    even though their normal alignment is 8 bytes.  See ia64_function_arg.  */
4736
4737 static unsigned int
4738 ia64_function_arg_boundary (enum machine_mode mode, const_tree type)
4739 {
4740   if (mode == TFmode && TARGET_HPUX && TARGET_ILP32)
4741     return PARM_BOUNDARY * 2;
4742
4743   if (type)
4744     {
4745       if (TYPE_ALIGN (type) > PARM_BOUNDARY)
4746         return PARM_BOUNDARY * 2;
4747       else
4748         return PARM_BOUNDARY;
4749     }
4750
4751   if (GET_MODE_BITSIZE (mode) > PARM_BOUNDARY)
4752     return PARM_BOUNDARY * 2;
4753   else
4754     return PARM_BOUNDARY;
4755 }
4756
4757 /* True if it is OK to do sibling call optimization for the specified
4758    call expression EXP.  DECL will be the called function, or NULL if
4759    this is an indirect call.  */
4760 static bool
4761 ia64_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
4762 {
4763   /* We can't perform a sibcall if the current function has the syscall_linkage
4764      attribute.  */
4765   if (lookup_attribute ("syscall_linkage",
4766                         TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
4767     return false;
4768
4769   /* We must always return with our current GP.  This means we can
4770      only sibcall to functions defined in the current module unless
4771      TARGET_CONST_GP is set to true.  */
4772   return (decl && (*targetm.binds_local_p) (decl)) || TARGET_CONST_GP;
4773 }
4774 \f
4775
4776 /* Implement va_arg.  */
4777
4778 static tree
4779 ia64_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
4780                       gimple_seq *post_p)
4781 {
4782   /* Variable sized types are passed by reference.  */
4783   if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
4784     {
4785       tree ptrtype = build_pointer_type (type);
4786       tree addr = std_gimplify_va_arg_expr (valist, ptrtype, pre_p, post_p);
4787       return build_va_arg_indirect_ref (addr);
4788     }
4789
4790   /* Aggregate arguments with alignment larger than 8 bytes start at
4791      the next even boundary.  Integer and floating point arguments
4792      do so if they are larger than 8 bytes, whether or not they are
4793      also aligned larger than 8 bytes.  */
4794   if ((TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == INTEGER_TYPE)
4795       ? int_size_in_bytes (type) > 8 : TYPE_ALIGN (type) > 8 * BITS_PER_UNIT)
4796     {
4797       tree t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist), valist,
4798                        size_int (2 * UNITS_PER_WORD - 1));
4799       t = fold_convert (sizetype, t);
4800       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
4801                   size_int (-2 * UNITS_PER_WORD));
4802       t = fold_convert (TREE_TYPE (valist), t);
4803       gimplify_assign (unshare_expr (valist), t, pre_p);
4804     }
4805
4806   return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
4807 }
4808 \f
4809 /* Return 1 if function return value returned in memory.  Return 0 if it is
4810    in a register.  */
4811
4812 static bool
4813 ia64_return_in_memory (const_tree valtype, const_tree fntype ATTRIBUTE_UNUSED)
4814 {
4815   enum machine_mode mode;
4816   enum machine_mode hfa_mode;
4817   HOST_WIDE_INT byte_size;
4818
4819   mode = TYPE_MODE (valtype);
4820   byte_size = GET_MODE_SIZE (mode);
4821   if (mode == BLKmode)
4822     {
4823       byte_size = int_size_in_bytes (valtype);
4824       if (byte_size < 0)
4825         return true;
4826     }
4827
4828   /* Hfa's with up to 8 elements are returned in the FP argument registers.  */
4829
4830   hfa_mode = hfa_element_mode (valtype, 0);
4831   if (hfa_mode != VOIDmode)
4832     {
4833       int hfa_size = GET_MODE_SIZE (hfa_mode);
4834
4835       if (byte_size / hfa_size > MAX_ARGUMENT_SLOTS)
4836         return true;
4837       else
4838         return false;
4839     }
4840   else if (byte_size > UNITS_PER_WORD * MAX_INT_RETURN_SLOTS)
4841     return true;
4842   else
4843     return false;
4844 }
4845
4846 /* Return rtx for register that holds the function return value.  */
4847
4848 static rtx
4849 ia64_function_value (const_tree valtype,
4850                      const_tree fn_decl_or_type,
4851                      bool outgoing ATTRIBUTE_UNUSED)
4852 {
4853   enum machine_mode mode;
4854   enum machine_mode hfa_mode;
4855   int unsignedp;
4856   const_tree func = fn_decl_or_type;
4857
4858   if (fn_decl_or_type
4859       && !DECL_P (fn_decl_or_type))
4860     func = NULL;
4861   
4862   mode = TYPE_MODE (valtype);
4863   hfa_mode = hfa_element_mode (valtype, 0);
4864
4865   if (hfa_mode != VOIDmode)
4866     {
4867       rtx loc[8];
4868       int i;
4869       int hfa_size;
4870       int byte_size;
4871       int offset;
4872
4873       hfa_size = GET_MODE_SIZE (hfa_mode);
4874       byte_size = ((mode == BLKmode)
4875                    ? int_size_in_bytes (valtype) : GET_MODE_SIZE (mode));
4876       offset = 0;
4877       for (i = 0; offset < byte_size; i++)
4878         {
4879           loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
4880                                       gen_rtx_REG (hfa_mode, FR_ARG_FIRST + i),
4881                                       GEN_INT (offset));
4882           offset += hfa_size;
4883         }
4884       return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
4885     }
4886   else if (FLOAT_TYPE_P (valtype) && mode != TFmode && mode != TCmode)
4887     return gen_rtx_REG (mode, FR_ARG_FIRST);
4888   else
4889     {
4890       bool need_parallel = false;
4891
4892       /* In big-endian mode, we need to manage the layout of aggregates
4893          in the registers so that we get the bits properly aligned in
4894          the highpart of the registers.  */
4895       if (BYTES_BIG_ENDIAN
4896           && (mode == BLKmode || (valtype && AGGREGATE_TYPE_P (valtype))))
4897         need_parallel = true;
4898
4899       /* Something like struct S { long double x; char a[0] } is not an
4900          HFA structure, and therefore doesn't go in fp registers.  But
4901          the middle-end will give it XFmode anyway, and XFmode values
4902          don't normally fit in integer registers.  So we need to smuggle
4903          the value inside a parallel.  */
4904       else if (mode == XFmode || mode == XCmode || mode == RFmode)
4905         need_parallel = true;
4906
4907       if (need_parallel)
4908         {
4909           rtx loc[8];
4910           int offset;
4911           int bytesize;
4912           int i;
4913
4914           offset = 0;
4915           bytesize = int_size_in_bytes (valtype);
4916           /* An empty PARALLEL is invalid here, but the return value
4917              doesn't matter for empty structs.  */
4918           if (bytesize == 0)
4919             return gen_rtx_REG (mode, GR_RET_FIRST);
4920           for (i = 0; offset < bytesize; i++)
4921             {
4922               loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
4923                                           gen_rtx_REG (DImode,
4924                                                        GR_RET_FIRST + i),
4925                                           GEN_INT (offset));
4926               offset += UNITS_PER_WORD;
4927             }
4928           return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
4929         }
4930
4931       mode = ia64_promote_function_mode (valtype, mode, &unsignedp,
4932                                          func ? TREE_TYPE (func) : NULL_TREE,
4933                                          true);
4934
4935       return gen_rtx_REG (mode, GR_RET_FIRST);
4936     }
4937 }
4938
4939 /* Worker function for TARGET_LIBCALL_VALUE.  */
4940
4941 static rtx
4942 ia64_libcall_value (enum machine_mode mode,
4943                     const_rtx fun ATTRIBUTE_UNUSED)
4944 {
4945   return gen_rtx_REG (mode,
4946                       (((GET_MODE_CLASS (mode) == MODE_FLOAT
4947                          || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
4948                         && (mode) != TFmode)
4949                        ? FR_RET_FIRST : GR_RET_FIRST));
4950 }
4951
4952 /* Worker function for FUNCTION_VALUE_REGNO_P.  */
4953
4954 static bool
4955 ia64_function_value_regno_p (const unsigned int regno)
4956 {
4957   return ((regno >= GR_RET_FIRST && regno <= GR_RET_LAST)
4958           || (regno >= FR_RET_FIRST && regno <= FR_RET_LAST));
4959 }
4960
4961 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
4962    We need to emit DTP-relative relocations.  */
4963
4964 static void
4965 ia64_output_dwarf_dtprel (FILE *file, int size, rtx x)
4966 {
4967   gcc_assert (size == 4 || size == 8);
4968   if (size == 4)
4969     fputs ("\tdata4.ua\t@dtprel(", file);
4970   else
4971     fputs ("\tdata8.ua\t@dtprel(", file);
4972   output_addr_const (file, x);
4973   fputs (")", file);
4974 }
4975
4976 /* Print a memory address as an operand to reference that memory location.  */
4977
4978 /* ??? Do we need this?  It gets used only for 'a' operands.  We could perhaps
4979    also call this from ia64_print_operand for memory addresses.  */
4980
4981 void
4982 ia64_print_operand_address (FILE * stream ATTRIBUTE_UNUSED,
4983                             rtx address ATTRIBUTE_UNUSED)
4984 {
4985 }
4986
4987 /* Print an operand to an assembler instruction.
4988    C    Swap and print a comparison operator.
4989    D    Print an FP comparison operator.
4990    E    Print 32 - constant, for SImode shifts as extract.
4991    e    Print 64 - constant, for DImode rotates.
4992    F    A floating point constant 0.0 emitted as f0, or 1.0 emitted as f1, or
4993         a floating point register emitted normally.
4994    G    A floating point constant.
4995    I    Invert a predicate register by adding 1.
4996    J    Select the proper predicate register for a condition.
4997    j    Select the inverse predicate register for a condition.
4998    O    Append .acq for volatile load.
4999    P    Postincrement of a MEM.
5000    Q    Append .rel for volatile store.
5001    R    Print .s .d or nothing for a single, double or no truncation.
5002    S    Shift amount for shladd instruction.
5003    T    Print an 8-bit sign extended number (K) as a 32-bit unsigned number
5004         for Intel assembler.
5005    U    Print an 8-bit sign extended number (K) as a 64-bit unsigned number
5006         for Intel assembler.
5007    X    A pair of floating point registers.
5008    r    Print register name, or constant 0 as r0.  HP compatibility for
5009         Linux kernel.
5010    v    Print vector constant value as an 8-byte integer value.  */
5011
5012 void
5013 ia64_print_operand (FILE * file, rtx x, int code)
5014 {
5015   const char *str;
5016
5017   switch (code)
5018     {
5019     case 0:
5020       /* Handled below.  */
5021       break;
5022
5023     case 'C':
5024       {
5025         enum rtx_code c = swap_condition (GET_CODE (x));
5026         fputs (GET_RTX_NAME (c), file);
5027         return;
5028       }
5029
5030     case 'D':
5031       switch (GET_CODE (x))
5032         {
5033         case NE:
5034           str = "neq";
5035           break;
5036         case UNORDERED:
5037           str = "unord";
5038           break;
5039         case ORDERED:
5040           str = "ord";
5041           break;
5042         case UNLT:
5043           str = "nge";
5044           break;
5045         case UNLE:
5046           str = "ngt";
5047           break;
5048         case UNGT:
5049           str = "nle";
5050           break;
5051         case UNGE:
5052           str = "nlt";
5053           break;
5054         default:
5055           str = GET_RTX_NAME (GET_CODE (x));
5056           break;
5057         }
5058       fputs (str, file);
5059       return;
5060
5061     case 'E':
5062       fprintf (file, HOST_WIDE_INT_PRINT_DEC, 32 - INTVAL (x));
5063       return;
5064
5065     case 'e':
5066       fprintf (file, HOST_WIDE_INT_PRINT_DEC, 64 - INTVAL (x));
5067       return;
5068
5069     case 'F':
5070       if (x == CONST0_RTX (GET_MODE (x)))
5071         str = reg_names [FR_REG (0)];
5072       else if (x == CONST1_RTX (GET_MODE (x)))
5073         str = reg_names [FR_REG (1)];
5074       else
5075         {
5076           gcc_assert (GET_CODE (x) == REG);
5077           str = reg_names [REGNO (x)];
5078         }
5079       fputs (str, file);
5080       return;
5081
5082     case 'G':
5083       {
5084         long val[4];
5085         REAL_VALUE_TYPE rv;
5086         REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
5087         real_to_target (val, &rv, GET_MODE (x));
5088         if (GET_MODE (x) == SFmode)
5089           fprintf (file, "0x%08lx", val[0] & 0xffffffff);
5090         else if (GET_MODE (x) == DFmode)
5091           fprintf (file, "0x%08lx%08lx", (WORDS_BIG_ENDIAN ? val[0] : val[1])
5092                                           & 0xffffffff,
5093                                          (WORDS_BIG_ENDIAN ? val[1] : val[0])
5094                                           & 0xffffffff);
5095         else
5096           output_operand_lossage ("invalid %%G mode");
5097       }
5098       return;
5099
5100     case 'I':
5101       fputs (reg_names [REGNO (x) + 1], file);
5102       return;
5103
5104     case 'J':
5105     case 'j':
5106       {
5107         unsigned int regno = REGNO (XEXP (x, 0));
5108         if (GET_CODE (x) == EQ)
5109           regno += 1;
5110         if (code == 'j')
5111           regno ^= 1;
5112         fputs (reg_names [regno], file);
5113       }
5114       return;
5115
5116     case 'O':
5117       if (MEM_VOLATILE_P (x))
5118         fputs(".acq", file);
5119       return;
5120
5121     case 'P':
5122       {
5123         HOST_WIDE_INT value;
5124
5125         switch (GET_CODE (XEXP (x, 0)))
5126           {
5127           default:
5128             return;
5129
5130           case POST_MODIFY:
5131             x = XEXP (XEXP (XEXP (x, 0), 1), 1);
5132             if (GET_CODE (x) == CONST_INT)
5133               value = INTVAL (x);
5134             else
5135               {
5136                 gcc_assert (GET_CODE (x) == REG);
5137                 fprintf (file, ", %s", reg_names[REGNO (x)]);
5138                 return;
5139               }
5140             break;
5141
5142           case POST_INC:
5143             value = GET_MODE_SIZE (GET_MODE (x));
5144             break;
5145
5146           case POST_DEC:
5147             value = - (HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (x));
5148             break;
5149           }
5150
5151         fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC, value);
5152         return;
5153       }
5154
5155     case 'Q':
5156       if (MEM_VOLATILE_P (x))
5157         fputs(".rel", file);
5158       return;
5159
5160     case 'R':
5161       if (x == CONST0_RTX (GET_MODE (x)))
5162         fputs(".s", file);
5163       else if (x == CONST1_RTX (GET_MODE (x)))
5164         fputs(".d", file);
5165       else if (x == CONST2_RTX (GET_MODE (x)))
5166         ;
5167       else
5168         output_operand_lossage ("invalid %%R value");
5169       return;
5170
5171     case 'S':
5172       fprintf (file, "%d", exact_log2 (INTVAL (x)));
5173       return;
5174
5175     case 'T':
5176       if (! TARGET_GNU_AS && GET_CODE (x) == CONST_INT)
5177         {
5178           fprintf (file, "0x%x", (int) INTVAL (x) & 0xffffffff);
5179           return;
5180         }
5181       break;
5182
5183     case 'U':
5184       if (! TARGET_GNU_AS && GET_CODE (x) == CONST_INT)
5185         {
5186           const char *prefix = "0x";
5187           if (INTVAL (x) & 0x80000000)
5188             {
5189               fprintf (file, "0xffffffff");
5190               prefix = "";
5191             }
5192           fprintf (file, "%s%x", prefix, (int) INTVAL (x) & 0xffffffff);
5193           return;
5194         }
5195       break;
5196
5197     case 'X':
5198       {
5199         unsigned int regno = REGNO (x);
5200         fprintf (file, "%s, %s", reg_names [regno], reg_names [regno + 1]);
5201       }
5202       return;
5203
5204     case 'r':
5205       /* If this operand is the constant zero, write it as register zero.
5206          Any register, zero, or CONST_INT value is OK here.  */
5207       if (GET_CODE (x) == REG)
5208         fputs (reg_names[REGNO (x)], file);
5209       else if (x == CONST0_RTX (GET_MODE (x)))
5210         fputs ("r0", file);
5211       else if (GET_CODE (x) == CONST_INT)
5212         output_addr_const (file, x);
5213       else
5214         output_operand_lossage ("invalid %%r value");
5215       return;
5216
5217     case 'v':
5218       gcc_assert (GET_CODE (x) == CONST_VECTOR);
5219       x = simplify_subreg (DImode, x, GET_MODE (x), 0);
5220       break;
5221
5222     case '+':
5223       {
5224         const char *which;
5225
5226         /* For conditional branches, returns or calls, substitute
5227            sptk, dptk, dpnt, or spnt for %s.  */
5228         x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
5229         if (x)
5230           {
5231             int pred_val = INTVAL (XEXP (x, 0));
5232
5233             /* Guess top and bottom 10% statically predicted.  */
5234             if (pred_val < REG_BR_PROB_BASE / 50
5235                 && br_prob_note_reliable_p (x))
5236               which = ".spnt";
5237             else if (pred_val < REG_BR_PROB_BASE / 2)
5238               which = ".dpnt";
5239             else if (pred_val < REG_BR_PROB_BASE / 100 * 98
5240                      || !br_prob_note_reliable_p (x))
5241               which = ".dptk";
5242             else
5243               which = ".sptk";
5244           }
5245         else if (GET_CODE (current_output_insn) == CALL_INSN)
5246           which = ".sptk";
5247         else
5248           which = ".dptk";
5249
5250         fputs (which, file);
5251         return;
5252       }
5253
5254     case ',':
5255       x = current_insn_predicate;
5256       if (x)
5257         {
5258           unsigned int regno = REGNO (XEXP (x, 0));
5259           if (GET_CODE (x) == EQ)
5260             regno += 1;
5261           fprintf (file, "(%s) ", reg_names [regno]);
5262         }
5263       return;
5264
5265     default:
5266       output_operand_lossage ("ia64_print_operand: unknown code");
5267       return;
5268     }
5269
5270   switch (GET_CODE (x))
5271     {
5272       /* This happens for the spill/restore instructions.  */
5273     case POST_INC:
5274     case POST_DEC:
5275     case POST_MODIFY:
5276       x = XEXP (x, 0);
5277       /* ... fall through ...  */
5278
5279     case REG:
5280       fputs (reg_names [REGNO (x)], file);
5281       break;
5282
5283     case MEM:
5284       {
5285         rtx addr = XEXP (x, 0);
5286         if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
5287           addr = XEXP (addr, 0);
5288         fprintf (file, "[%s]", reg_names [REGNO (addr)]);
5289         break;
5290       }
5291
5292     default:
5293       output_addr_const (file, x);
5294       break;
5295     }
5296
5297   return;
5298 }
5299 \f
5300 /* Compute a (partial) cost for rtx X.  Return true if the complete
5301    cost has been computed, and false if subexpressions should be
5302    scanned.  In either case, *TOTAL contains the cost result.  */
5303 /* ??? This is incomplete.  */
5304
5305 static bool
5306 ia64_rtx_costs (rtx x, int code, int outer_code, int *total,
5307                 bool speed ATTRIBUTE_UNUSED)
5308 {
5309   switch (code)
5310     {
5311     case CONST_INT:
5312       switch (outer_code)
5313         {
5314         case SET:
5315           *total = satisfies_constraint_J (x) ? 0 : COSTS_N_INSNS (1);
5316           return true;
5317         case PLUS:
5318           if (satisfies_constraint_I (x))
5319             *total = 0;
5320           else if (satisfies_constraint_J (x))
5321             *total = 1;
5322           else
5323             *total = COSTS_N_INSNS (1);
5324           return true;
5325         default:
5326           if (satisfies_constraint_K (x) || satisfies_constraint_L (x))
5327             *total = 0;
5328           else
5329             *total = COSTS_N_INSNS (1);
5330           return true;
5331         }
5332
5333     case CONST_DOUBLE:
5334       *total = COSTS_N_INSNS (1);
5335       return true;
5336
5337     case CONST:
5338     case SYMBOL_REF:
5339     case LABEL_REF:
5340       *total = COSTS_N_INSNS (3);
5341       return true;
5342
5343     case FMA:
5344       *total = COSTS_N_INSNS (4);
5345       return true;
5346
5347     case MULT:
5348       /* For multiplies wider than HImode, we have to go to the FPU,
5349          which normally involves copies.  Plus there's the latency
5350          of the multiply itself, and the latency of the instructions to
5351          transfer integer regs to FP regs.  */
5352       if (FLOAT_MODE_P (GET_MODE (x)))
5353         *total = COSTS_N_INSNS (4);
5354       else if (GET_MODE_SIZE (GET_MODE (x)) > 2)
5355         *total = COSTS_N_INSNS (10);
5356       else
5357         *total = COSTS_N_INSNS (2);
5358       return true;
5359
5360     case PLUS:
5361     case MINUS:
5362       if (FLOAT_MODE_P (GET_MODE (x)))
5363         {
5364           *total = COSTS_N_INSNS (4);
5365           return true;
5366         }
5367       /* FALLTHRU */
5368
5369     case ASHIFT:
5370     case ASHIFTRT:
5371     case LSHIFTRT:
5372       *total = COSTS_N_INSNS (1);
5373       return true;
5374
5375     case DIV:
5376     case UDIV:
5377     case MOD:
5378     case UMOD:
5379       /* We make divide expensive, so that divide-by-constant will be
5380          optimized to a multiply.  */
5381       *total = COSTS_N_INSNS (60);
5382       return true;
5383
5384     default:
5385       return false;
5386     }
5387 }
5388
5389 /* Calculate the cost of moving data from a register in class FROM to
5390    one in class TO, using MODE.  */
5391
5392 static int
5393 ia64_register_move_cost (enum machine_mode mode, reg_class_t from_i,
5394                          reg_class_t to_i)
5395 {
5396   enum reg_class from = (enum reg_class) from_i;
5397   enum reg_class to = (enum reg_class) to_i;
5398
5399   /* ADDL_REGS is the same as GR_REGS for movement purposes.  */
5400   if (to == ADDL_REGS)
5401     to = GR_REGS;
5402   if (from == ADDL_REGS)
5403     from = GR_REGS;
5404
5405   /* All costs are symmetric, so reduce cases by putting the
5406      lower number class as the destination.  */
5407   if (from < to)
5408     {
5409       enum reg_class tmp = to;
5410       to = from, from = tmp;
5411     }
5412
5413   /* Moving from FR<->GR in XFmode must be more expensive than 2,
5414      so that we get secondary memory reloads.  Between FR_REGS,
5415      we have to make this at least as expensive as memory_move_cost
5416      to avoid spectacularly poor register class preferencing.  */
5417   if (mode == XFmode || mode == RFmode)
5418     {
5419       if (to != GR_REGS || from != GR_REGS)
5420         return memory_move_cost (mode, to, false);
5421       else
5422         return 3;
5423     }
5424
5425   switch (to)
5426     {
5427     case PR_REGS:
5428       /* Moving between PR registers takes two insns.  */
5429       if (from == PR_REGS)
5430         return 3;
5431       /* Moving between PR and anything but GR is impossible.  */
5432       if (from != GR_REGS)
5433         return memory_move_cost (mode, to, false);
5434       break;
5435
5436     case BR_REGS:
5437       /* Moving between BR and anything but GR is impossible.  */
5438       if (from != GR_REGS && from != GR_AND_BR_REGS)
5439         return memory_move_cost (mode, to, false);
5440       break;
5441
5442     case AR_I_REGS:
5443     case AR_M_REGS:
5444       /* Moving between AR and anything but GR is impossible.  */
5445       if (from != GR_REGS)
5446         return memory_move_cost (mode, to, false);
5447       break;
5448
5449     case GR_REGS:
5450     case FR_REGS:
5451     case FP_REGS:
5452     case GR_AND_FR_REGS:
5453     case GR_AND_BR_REGS:
5454     case ALL_REGS:
5455       break;
5456
5457     default:
5458       gcc_unreachable ();
5459     }
5460
5461   return 2;
5462 }
5463
5464 /* Calculate the cost of moving data of MODE from a register to or from
5465    memory.  */
5466
5467 static int
5468 ia64_memory_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
5469                        reg_class_t rclass,
5470                        bool in ATTRIBUTE_UNUSED)
5471 {
5472   if (rclass == GENERAL_REGS
5473       || rclass == FR_REGS
5474       || rclass == FP_REGS
5475       || rclass == GR_AND_FR_REGS)
5476     return 4;
5477   else
5478     return 10;
5479 }
5480
5481 /* Implement TARGET_PREFERRED_RELOAD_CLASS.  Place additional restrictions
5482    on RCLASS to use when copying X into that class.  */
5483
5484 static reg_class_t
5485 ia64_preferred_reload_class (rtx x, reg_class_t rclass)
5486 {
5487   switch (rclass)
5488     {
5489     case FR_REGS:
5490     case FP_REGS:
5491       /* Don't allow volatile mem reloads into floating point registers.
5492          This is defined to force reload to choose the r/m case instead
5493          of the f/f case when reloading (set (reg fX) (mem/v)).  */
5494       if (MEM_P (x) && MEM_VOLATILE_P (x))
5495         return NO_REGS;
5496       
5497       /* Force all unrecognized constants into the constant pool.  */
5498       if (CONSTANT_P (x))
5499         return NO_REGS;
5500       break;
5501
5502     case AR_M_REGS:
5503     case AR_I_REGS:
5504       if (!OBJECT_P (x))
5505         return NO_REGS;
5506       break;
5507
5508     default:
5509       break;
5510     }
5511
5512   return rclass;
5513 }
5514
5515 /* This function returns the register class required for a secondary
5516    register when copying between one of the registers in RCLASS, and X,
5517    using MODE.  A return value of NO_REGS means that no secondary register
5518    is required.  */
5519
5520 enum reg_class
5521 ia64_secondary_reload_class (enum reg_class rclass,
5522                              enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
5523 {
5524   int regno = -1;
5525
5526   if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
5527     regno = true_regnum (x);
5528
5529   switch (rclass)
5530     {
5531     case BR_REGS:
5532     case AR_M_REGS:
5533     case AR_I_REGS:
5534       /* ??? BR<->BR register copies can happen due to a bad gcse/cse/global
5535          interaction.  We end up with two pseudos with overlapping lifetimes
5536          both of which are equiv to the same constant, and both which need
5537          to be in BR_REGS.  This seems to be a cse bug.  cse_basic_block_end
5538          changes depending on the path length, which means the qty_first_reg
5539          check in make_regs_eqv can give different answers at different times.
5540          At some point I'll probably need a reload_indi pattern to handle
5541          this.
5542
5543          We can also get GR_AND_FR_REGS to BR_REGS/AR_REGS copies, where we
5544          wound up with a FP register from GR_AND_FR_REGS.  Extend that to all
5545          non-general registers for good measure.  */
5546       if (regno >= 0 && ! GENERAL_REGNO_P (regno))
5547         return GR_REGS;
5548
5549       /* This is needed if a pseudo used as a call_operand gets spilled to a
5550          stack slot.  */
5551       if (GET_CODE (x) == MEM)
5552         return GR_REGS;
5553       break;
5554
5555     case FR_REGS:
5556     case FP_REGS:
5557       /* Need to go through general registers to get to other class regs.  */
5558       if (regno >= 0 && ! (FR_REGNO_P (regno) || GENERAL_REGNO_P (regno)))
5559         return GR_REGS;
5560
5561       /* This can happen when a paradoxical subreg is an operand to the
5562          muldi3 pattern.  */
5563       /* ??? This shouldn't be necessary after instruction scheduling is
5564          enabled, because paradoxical subregs are not accepted by
5565          register_operand when INSN_SCHEDULING is defined.  Or alternatively,
5566          stop the paradoxical subreg stupidity in the *_operand functions
5567          in recog.c.  */
5568       if (GET_CODE (x) == MEM
5569           && (GET_MODE (x) == SImode || GET_MODE (x) == HImode
5570               || GET_MODE (x) == QImode))
5571         return GR_REGS;
5572
5573       /* This can happen because of the ior/and/etc patterns that accept FP
5574          registers as operands.  If the third operand is a constant, then it
5575          needs to be reloaded into a FP register.  */
5576       if (GET_CODE (x) == CONST_INT)
5577         return GR_REGS;
5578
5579       /* This can happen because of register elimination in a muldi3 insn.
5580          E.g. `26107 * (unsigned long)&u'.  */
5581       if (GET_CODE (x) == PLUS)
5582         return GR_REGS;
5583       break;
5584
5585     case PR_REGS:
5586       /* ??? This happens if we cse/gcse a BImode value across a call,
5587          and the function has a nonlocal goto.  This is because global
5588          does not allocate call crossing pseudos to hard registers when
5589          crtl->has_nonlocal_goto is true.  This is relatively
5590          common for C++ programs that use exceptions.  To reproduce,
5591          return NO_REGS and compile libstdc++.  */
5592       if (GET_CODE (x) == MEM)
5593         return GR_REGS;
5594
5595       /* This can happen when we take a BImode subreg of a DImode value,
5596          and that DImode value winds up in some non-GR register.  */
5597       if (regno >= 0 && ! GENERAL_REGNO_P (regno) && ! PR_REGNO_P (regno))
5598         return GR_REGS;
5599       break;
5600
5601     default:
5602       break;
5603     }
5604
5605   return NO_REGS;
5606 }
5607
5608 \f
5609 /* Implement targetm.unspec_may_trap_p hook.  */
5610 static int
5611 ia64_unspec_may_trap_p (const_rtx x, unsigned flags)
5612 {
5613   if (GET_CODE (x) == UNSPEC)
5614     {
5615       switch (XINT (x, 1))
5616         {
5617         case UNSPEC_LDA:
5618         case UNSPEC_LDS:
5619         case UNSPEC_LDSA:
5620         case UNSPEC_LDCCLR:
5621         case UNSPEC_CHKACLR:
5622         case UNSPEC_CHKS:
5623           /* These unspecs are just wrappers.  */
5624           return may_trap_p_1 (XVECEXP (x, 0, 0), flags);
5625         }
5626     }
5627
5628   return default_unspec_may_trap_p (x, flags);
5629 }
5630
5631 \f
5632 /* Parse the -mfixed-range= option string.  */
5633
5634 static void
5635 fix_range (const char *const_str)
5636 {
5637   int i, first, last;
5638   char *str, *dash, *comma;
5639
5640   /* str must be of the form REG1'-'REG2{,REG1'-'REG} where REG1 and
5641      REG2 are either register names or register numbers.  The effect
5642      of this option is to mark the registers in the range from REG1 to
5643      REG2 as ``fixed'' so they won't be used by the compiler.  This is
5644      used, e.g., to ensure that kernel mode code doesn't use f32-f127.  */
5645
5646   i = strlen (const_str);
5647   str = (char *) alloca (i + 1);
5648   memcpy (str, const_str, i + 1);
5649
5650   while (1)
5651     {
5652       dash = strchr (str, '-');
5653       if (!dash)
5654         {
5655           warning (0, "value of -mfixed-range must have form REG1-REG2");
5656           return;
5657         }
5658       *dash = '\0';
5659
5660       comma = strchr (dash + 1, ',');
5661       if (comma)
5662         *comma = '\0';
5663
5664       first = decode_reg_name (str);
5665       if (first < 0)
5666         {
5667           warning (0, "unknown register name: %s", str);
5668           return;
5669         }
5670
5671       last = decode_reg_name (dash + 1);
5672       if (last < 0)
5673         {
5674           warning (0, "unknown register name: %s", dash + 1);
5675           return;
5676         }
5677
5678       *dash = '-';
5679
5680       if (first > last)
5681         {
5682           warning (0, "%s-%s is an empty range", str, dash + 1);
5683           return;
5684         }
5685
5686       for (i = first; i <= last; ++i)
5687         fixed_regs[i] = call_used_regs[i] = 1;
5688
5689       if (!comma)
5690         break;
5691
5692       *comma = ',';
5693       str = comma + 1;
5694     }
5695 }
5696
5697 /* Implement TARGET_HANDLE_OPTION.  */
5698
5699 static bool
5700 ia64_handle_option (size_t code, const char *arg, int value)
5701 {
5702   switch (code)
5703     {
5704     case OPT_mfixed_range_:
5705       fix_range (arg);
5706       return true;
5707
5708     case OPT_mtls_size_:
5709       if (value != 14 && value != 22 && value != 64)
5710         error ("bad value %<%s%> for -mtls-size= switch", arg);
5711       return true;
5712
5713     case OPT_mtune_:
5714       {
5715         static struct pta
5716           {
5717             const char *name;           /* processor name or nickname.  */
5718             enum processor_type processor;
5719           }
5720         const processor_alias_table[] =
5721           {
5722             {"itanium2", PROCESSOR_ITANIUM2},
5723             {"mckinley", PROCESSOR_ITANIUM2},
5724           };
5725         int const pta_size = ARRAY_SIZE (processor_alias_table);
5726         int i;
5727
5728         for (i = 0; i < pta_size; i++)
5729           if (!strcmp (arg, processor_alias_table[i].name))
5730             {
5731               ia64_tune = processor_alias_table[i].processor;
5732               break;
5733             }
5734         if (i == pta_size)
5735           error ("bad value %<%s%> for -mtune= switch", arg);
5736         return true;
5737       }
5738
5739     default:
5740       return true;
5741     }
5742 }
5743
5744 /* Implement TARGET_OPTION_OVERRIDE.  */
5745
5746 static void
5747 ia64_option_override (void)
5748 {
5749   if (TARGET_AUTO_PIC)
5750     target_flags |= MASK_CONST_GP;
5751
5752   /* Numerous experiment shows that IRA based loop pressure
5753      calculation works better for RTL loop invariant motion on targets
5754      with enough (>= 32) registers.  It is an expensive optimization.
5755      So it is on only for peak performance.  */
5756   if (optimize >= 3)
5757     flag_ira_loop_pressure = 1;
5758
5759
5760   ia64_section_threshold = (global_options_set.x_g_switch_value
5761                             ? g_switch_value
5762                             : IA64_DEFAULT_GVALUE);
5763
5764   init_machine_status = ia64_init_machine_status;
5765
5766   if (align_functions <= 0)
5767     align_functions = 64;
5768   if (align_loops <= 0)
5769     align_loops = 32;
5770   if (TARGET_ABI_OPEN_VMS)
5771     flag_no_common = 1;
5772
5773   ia64_override_options_after_change();
5774 }
5775
5776 /* Implement targetm.override_options_after_change.  */
5777
5778 static void
5779 ia64_override_options_after_change (void)
5780 {
5781   ia64_flag_schedule_insns2 = flag_schedule_insns_after_reload;
5782   flag_schedule_insns_after_reload = 0;
5783
5784   if (optimize >= 3
5785       && !global_options_set.x_flag_selective_scheduling
5786       && !global_options_set.x_flag_selective_scheduling2)
5787     {
5788       flag_selective_scheduling2 = 1;
5789       flag_sel_sched_pipelining = 1;
5790     }
5791   if (mflag_sched_control_spec == 2)
5792     {
5793       /* Control speculation is on by default for the selective scheduler,
5794          but not for the Haifa scheduler.  */
5795       mflag_sched_control_spec = flag_selective_scheduling2 ? 1 : 0;
5796     }
5797   if (flag_sel_sched_pipelining && flag_auto_inc_dec)
5798     {
5799       /* FIXME: remove this when we'd implement breaking autoinsns as
5800          a transformation.  */
5801       flag_auto_inc_dec = 0;
5802     }
5803 }
5804
5805 /* Initialize the record of emitted frame related registers.  */
5806
5807 void ia64_init_expanders (void)
5808 {
5809   memset (&emitted_frame_related_regs, 0, sizeof (emitted_frame_related_regs));
5810 }
5811
5812 static struct machine_function *
5813 ia64_init_machine_status (void)
5814 {
5815   return ggc_alloc_cleared_machine_function ();
5816 }
5817 \f
5818 static enum attr_itanium_class ia64_safe_itanium_class (rtx);
5819 static enum attr_type ia64_safe_type (rtx);
5820
5821 static enum attr_itanium_class
5822 ia64_safe_itanium_class (rtx insn)
5823 {
5824   if (recog_memoized (insn) >= 0)
5825     return get_attr_itanium_class (insn);
5826   else if (DEBUG_INSN_P (insn))
5827     return ITANIUM_CLASS_IGNORE;
5828   else
5829     return ITANIUM_CLASS_UNKNOWN;
5830 }
5831
5832 static enum attr_type
5833 ia64_safe_type (rtx insn)
5834 {
5835   if (recog_memoized (insn) >= 0)
5836     return get_attr_type (insn);
5837   else
5838     return TYPE_UNKNOWN;
5839 }
5840 \f
5841 /* The following collection of routines emit instruction group stop bits as
5842    necessary to avoid dependencies.  */
5843
5844 /* Need to track some additional registers as far as serialization is
5845    concerned so we can properly handle br.call and br.ret.  We could
5846    make these registers visible to gcc, but since these registers are
5847    never explicitly used in gcc generated code, it seems wasteful to
5848    do so (plus it would make the call and return patterns needlessly
5849    complex).  */
5850 #define REG_RP          (BR_REG (0))
5851 #define REG_AR_CFM      (FIRST_PSEUDO_REGISTER + 1)
5852 /* This is used for volatile asms which may require a stop bit immediately
5853    before and after them.  */
5854 #define REG_VOLATILE    (FIRST_PSEUDO_REGISTER + 2)
5855 #define AR_UNAT_BIT_0   (FIRST_PSEUDO_REGISTER + 3)
5856 #define NUM_REGS        (AR_UNAT_BIT_0 + 64)
5857
5858 /* For each register, we keep track of how it has been written in the
5859    current instruction group.
5860
5861    If a register is written unconditionally (no qualifying predicate),
5862    WRITE_COUNT is set to 2 and FIRST_PRED is ignored.
5863
5864    If a register is written if its qualifying predicate P is true, we
5865    set WRITE_COUNT to 1 and FIRST_PRED to P.  Later on, the same register
5866    may be written again by the complement of P (P^1) and when this happens,
5867    WRITE_COUNT gets set to 2.
5868
5869    The result of this is that whenever an insn attempts to write a register
5870    whose WRITE_COUNT is two, we need to issue an insn group barrier first.
5871
5872    If a predicate register is written by a floating-point insn, we set
5873    WRITTEN_BY_FP to true.
5874
5875    If a predicate register is written by an AND.ORCM we set WRITTEN_BY_AND
5876    to true; if it was written by an OR.ANDCM we set WRITTEN_BY_OR to true.  */
5877
5878 #if GCC_VERSION >= 4000
5879 #define RWS_FIELD_TYPE __extension__ unsigned short
5880 #else
5881 #define RWS_FIELD_TYPE unsigned int
5882 #endif
5883 struct reg_write_state
5884 {
5885   RWS_FIELD_TYPE write_count : 2;
5886   RWS_FIELD_TYPE first_pred : 10;
5887   RWS_FIELD_TYPE written_by_fp : 1;
5888   RWS_FIELD_TYPE written_by_and : 1;
5889   RWS_FIELD_TYPE written_by_or : 1;
5890 };
5891
5892 /* Cumulative info for the current instruction group.  */
5893 struct reg_write_state rws_sum[NUM_REGS];
5894 #ifdef ENABLE_CHECKING
5895 /* Bitmap whether a register has been written in the current insn.  */
5896 HARD_REG_ELT_TYPE rws_insn[(NUM_REGS + HOST_BITS_PER_WIDEST_FAST_INT - 1)
5897                            / HOST_BITS_PER_WIDEST_FAST_INT];
5898
5899 static inline void
5900 rws_insn_set (int regno)
5901 {
5902   gcc_assert (!TEST_HARD_REG_BIT (rws_insn, regno));
5903   SET_HARD_REG_BIT (rws_insn, regno);
5904 }
5905
5906 static inline int
5907 rws_insn_test (int regno)
5908 {
5909   return TEST_HARD_REG_BIT (rws_insn, regno);
5910 }
5911 #else
5912 /* When not checking, track just REG_AR_CFM and REG_VOLATILE.  */
5913 unsigned char rws_insn[2];
5914
5915 static inline void
5916 rws_insn_set (int regno)
5917 {
5918   if (regno == REG_AR_CFM)
5919     rws_insn[0] = 1;
5920   else if (regno == REG_VOLATILE)
5921     rws_insn[1] = 1;
5922 }
5923
5924 static inline int
5925 rws_insn_test (int regno)
5926 {
5927   if (regno == REG_AR_CFM)
5928     return rws_insn[0];
5929   if (regno == REG_VOLATILE)
5930     return rws_insn[1];
5931   return 0;
5932 }
5933 #endif
5934
5935 /* Indicates whether this is the first instruction after a stop bit,
5936    in which case we don't need another stop bit.  Without this,
5937    ia64_variable_issue will die when scheduling an alloc.  */
5938 static int first_instruction;
5939
5940 /* Misc flags needed to compute RAW/WAW dependencies while we are traversing
5941    RTL for one instruction.  */
5942 struct reg_flags
5943 {
5944   unsigned int is_write : 1;    /* Is register being written?  */
5945   unsigned int is_fp : 1;       /* Is register used as part of an fp op?  */
5946   unsigned int is_branch : 1;   /* Is register used as part of a branch?  */
5947   unsigned int is_and : 1;      /* Is register used as part of and.orcm?  */
5948   unsigned int is_or : 1;       /* Is register used as part of or.andcm?  */
5949   unsigned int is_sibcall : 1;  /* Is this a sibling or normal call?  */
5950 };
5951
5952 static void rws_update (int, struct reg_flags, int);
5953 static int rws_access_regno (int, struct reg_flags, int);
5954 static int rws_access_reg (rtx, struct reg_flags, int);
5955 static void update_set_flags (rtx, struct reg_flags *);
5956 static int set_src_needs_barrier (rtx, struct reg_flags, int);
5957 static int rtx_needs_barrier (rtx, struct reg_flags, int);
5958 static void init_insn_group_barriers (void);
5959 static int group_barrier_needed (rtx);
5960 static int safe_group_barrier_needed (rtx);
5961 static int in_safe_group_barrier;
5962
5963 /* Update *RWS for REGNO, which is being written by the current instruction,
5964    with predicate PRED, and associated register flags in FLAGS.  */
5965
5966 static void
5967 rws_update (int regno, struct reg_flags flags, int pred)
5968 {
5969   if (pred)
5970     rws_sum[regno].write_count++;
5971   else
5972     rws_sum[regno].write_count = 2;
5973   rws_sum[regno].written_by_fp |= flags.is_fp;
5974   /* ??? Not tracking and/or across differing predicates.  */
5975   rws_sum[regno].written_by_and = flags.is_and;
5976   rws_sum[regno].written_by_or = flags.is_or;
5977   rws_sum[regno].first_pred = pred;
5978 }
5979
5980 /* Handle an access to register REGNO of type FLAGS using predicate register
5981    PRED.  Update rws_sum array.  Return 1 if this access creates
5982    a dependency with an earlier instruction in the same group.  */
5983
5984 static int
5985 rws_access_regno (int regno, struct reg_flags flags, int pred)
5986 {
5987   int need_barrier = 0;
5988
5989   gcc_assert (regno < NUM_REGS);
5990
5991   if (! PR_REGNO_P (regno))
5992     flags.is_and = flags.is_or = 0;
5993
5994   if (flags.is_write)
5995     {
5996       int write_count;
5997
5998       rws_insn_set (regno);
5999       write_count = rws_sum[regno].write_count;
6000
6001       switch (write_count)
6002         {
6003         case 0:
6004           /* The register has not been written yet.  */
6005           if (!in_safe_group_barrier)
6006             rws_update (regno, flags, pred);
6007           break;
6008
6009         case 1:
6010           /* The register has been written via a predicate.  Treat
6011              it like a unconditional write and do not try to check
6012              for complementary pred reg in earlier write.  */
6013           if (flags.is_and && rws_sum[regno].written_by_and)
6014             ;
6015           else if (flags.is_or && rws_sum[regno].written_by_or)
6016             ;
6017           else
6018             need_barrier = 1;
6019           if (!in_safe_group_barrier)
6020             rws_update (regno, flags, pred);
6021           break;
6022
6023         case 2:
6024           /* The register has been unconditionally written already.  We
6025              need a barrier.  */
6026           if (flags.is_and && rws_sum[regno].written_by_and)
6027             ;
6028           else if (flags.is_or && rws_sum[regno].written_by_or)
6029             ;
6030           else
6031             need_barrier = 1;
6032           if (!in_safe_group_barrier)
6033             {
6034               rws_sum[regno].written_by_and = flags.is_and;
6035               rws_sum[regno].written_by_or = flags.is_or;
6036             }
6037           break;
6038
6039         default:
6040           gcc_unreachable ();
6041         }
6042     }
6043   else
6044     {
6045       if (flags.is_branch)
6046         {
6047           /* Branches have several RAW exceptions that allow to avoid
6048              barriers.  */
6049
6050           if (REGNO_REG_CLASS (regno) == BR_REGS || regno == AR_PFS_REGNUM)
6051             /* RAW dependencies on branch regs are permissible as long
6052                as the writer is a non-branch instruction.  Since we
6053                never generate code that uses a branch register written
6054                by a branch instruction, handling this case is
6055                easy.  */
6056             return 0;
6057
6058           if (REGNO_REG_CLASS (regno) == PR_REGS
6059               && ! rws_sum[regno].written_by_fp)
6060             /* The predicates of a branch are available within the
6061                same insn group as long as the predicate was written by
6062                something other than a floating-point instruction.  */
6063             return 0;
6064         }
6065
6066       if (flags.is_and && rws_sum[regno].written_by_and)
6067         return 0;
6068       if (flags.is_or && rws_sum[regno].written_by_or)
6069         return 0;
6070
6071       switch (rws_sum[regno].write_count)
6072         {
6073         case 0:
6074           /* The register has not been written yet.  */
6075           break;
6076
6077         case 1:
6078           /* The register has been written via a predicate, assume we
6079              need a barrier (don't check for complementary regs).  */
6080           need_barrier = 1;
6081           break;
6082
6083         case 2:
6084           /* The register has been unconditionally written already.  We
6085              need a barrier.  */
6086           need_barrier = 1;
6087           break;
6088
6089         default:
6090           gcc_unreachable ();
6091         }
6092     }
6093
6094   return need_barrier;
6095 }
6096
6097 static int
6098 rws_access_reg (rtx reg, struct reg_flags flags, int pred)
6099 {
6100   int regno = REGNO (reg);
6101   int n = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
6102
6103   if (n == 1)
6104     return rws_access_regno (regno, flags, pred);
6105   else
6106     {
6107       int need_barrier = 0;
6108       while (--n >= 0)
6109         need_barrier |= rws_access_regno (regno + n, flags, pred);
6110       return need_barrier;
6111     }
6112 }
6113
6114 /* Examine X, which is a SET rtx, and update the flags, the predicate, and
6115    the condition, stored in *PFLAGS, *PPRED and *PCOND.  */
6116
6117 static void
6118 update_set_flags (rtx x, struct reg_flags *pflags)
6119 {
6120   rtx src = SET_SRC (x);
6121
6122   switch (GET_CODE (src))
6123     {
6124     case CALL:
6125       return;
6126
6127     case IF_THEN_ELSE:
6128       /* There are four cases here:
6129          (1) The destination is (pc), in which case this is a branch,
6130          nothing here applies.
6131          (2) The destination is ar.lc, in which case this is a
6132          doloop_end_internal,
6133          (3) The destination is an fp register, in which case this is
6134          an fselect instruction.
6135          (4) The condition has (unspec [(reg)] UNSPEC_LDC), in which case 
6136          this is a check load.
6137          In all cases, nothing we do in this function applies.  */
6138       return;
6139
6140     default:
6141       if (COMPARISON_P (src)
6142           && SCALAR_FLOAT_MODE_P (GET_MODE (XEXP (src, 0))))
6143         /* Set pflags->is_fp to 1 so that we know we're dealing
6144            with a floating point comparison when processing the
6145            destination of the SET.  */
6146         pflags->is_fp = 1;
6147
6148       /* Discover if this is a parallel comparison.  We only handle
6149          and.orcm and or.andcm at present, since we must retain a
6150          strict inverse on the predicate pair.  */
6151       else if (GET_CODE (src) == AND)
6152         pflags->is_and = 1;
6153       else if (GET_CODE (src) == IOR)
6154         pflags->is_or = 1;
6155
6156       break;
6157     }
6158 }
6159
6160 /* Subroutine of rtx_needs_barrier; this function determines whether the
6161    source of a given SET rtx found in X needs a barrier.  FLAGS and PRED
6162    are as in rtx_needs_barrier.  COND is an rtx that holds the condition
6163    for this insn.  */
6164
6165 static int
6166 set_src_needs_barrier (rtx x, struct reg_flags flags, int pred)
6167 {
6168   int need_barrier = 0;
6169   rtx dst;
6170   rtx src = SET_SRC (x);
6171
6172   if (GET_CODE (src) == CALL)
6173     /* We don't need to worry about the result registers that
6174        get written by subroutine call.  */
6175     return rtx_needs_barrier (src, flags, pred);
6176   else if (SET_DEST (x) == pc_rtx)
6177     {
6178       /* X is a conditional branch.  */
6179       /* ??? This seems redundant, as the caller sets this bit for
6180          all JUMP_INSNs.  */
6181       if (!ia64_spec_check_src_p (src))
6182         flags.is_branch = 1;
6183       return rtx_needs_barrier (src, flags, pred);
6184     }
6185
6186   if (ia64_spec_check_src_p (src))
6187     /* Avoid checking one register twice (in condition 
6188        and in 'then' section) for ldc pattern.  */
6189     {
6190       gcc_assert (REG_P (XEXP (src, 2)));
6191       need_barrier = rtx_needs_barrier (XEXP (src, 2), flags, pred);
6192                   
6193       /* We process MEM below.  */
6194       src = XEXP (src, 1);
6195     }
6196
6197   need_barrier |= rtx_needs_barrier (src, flags, pred);
6198
6199   dst = SET_DEST (x);
6200   if (GET_CODE (dst) == ZERO_EXTRACT)
6201     {
6202       need_barrier |= rtx_needs_barrier (XEXP (dst, 1), flags, pred);
6203       need_barrier |= rtx_needs_barrier (XEXP (dst, 2), flags, pred);
6204     }
6205   return need_barrier;
6206 }
6207
6208 /* Handle an access to rtx X of type FLAGS using predicate register
6209    PRED.  Return 1 if this access creates a dependency with an earlier
6210    instruction in the same group.  */
6211
6212 static int
6213 rtx_needs_barrier (rtx x, struct reg_flags flags, int pred)
6214 {
6215   int i, j;
6216   int is_complemented = 0;
6217   int need_barrier = 0;
6218   const char *format_ptr;
6219   struct reg_flags new_flags;
6220   rtx cond;
6221
6222   if (! x)
6223     return 0;
6224
6225   new_flags = flags;
6226
6227   switch (GET_CODE (x))
6228     {
6229     case SET:
6230       update_set_flags (x, &new_flags);
6231       need_barrier = set_src_needs_barrier (x, new_flags, pred);
6232       if (GET_CODE (SET_SRC (x)) != CALL)
6233         {
6234           new_flags.is_write = 1;
6235           need_barrier |= rtx_needs_barrier (SET_DEST (x), new_flags, pred);
6236         }
6237       break;
6238
6239     case CALL:
6240       new_flags.is_write = 0;
6241       need_barrier |= rws_access_regno (AR_EC_REGNUM, new_flags, pred);
6242
6243       /* Avoid multiple register writes, in case this is a pattern with
6244          multiple CALL rtx.  This avoids a failure in rws_access_reg.  */
6245       if (! flags.is_sibcall && ! rws_insn_test (REG_AR_CFM))
6246         {
6247           new_flags.is_write = 1;
6248           need_barrier |= rws_access_regno (REG_RP, new_flags, pred);
6249           need_barrier |= rws_access_regno (AR_PFS_REGNUM, new_flags, pred);
6250           need_barrier |= rws_access_regno (REG_AR_CFM, new_flags, pred);
6251         }
6252       break;
6253
6254     case COND_EXEC:
6255       /* X is a predicated instruction.  */
6256
6257       cond = COND_EXEC_TEST (x);
6258       gcc_assert (!pred);
6259       need_barrier = rtx_needs_barrier (cond, flags, 0);
6260
6261       if (GET_CODE (cond) == EQ)
6262         is_complemented = 1;
6263       cond = XEXP (cond, 0);
6264       gcc_assert (GET_CODE (cond) == REG
6265                   && REGNO_REG_CLASS (REGNO (cond)) == PR_REGS);
6266       pred = REGNO (cond);
6267       if (is_complemented)
6268         ++pred;
6269
6270       need_barrier |= rtx_needs_barrier (COND_EXEC_CODE (x), flags, pred);
6271       return need_barrier;
6272
6273     case CLOBBER:
6274     case USE:
6275       /* Clobber & use are for earlier compiler-phases only.  */
6276       break;
6277
6278     case ASM_OPERANDS:
6279     case ASM_INPUT:
6280       /* We always emit stop bits for traditional asms.  We emit stop bits
6281          for volatile extended asms if TARGET_VOL_ASM_STOP is true.  */
6282       if (GET_CODE (x) != ASM_OPERANDS
6283           || (MEM_VOLATILE_P (x) && TARGET_VOL_ASM_STOP))
6284         {
6285           /* Avoid writing the register multiple times if we have multiple
6286              asm outputs.  This avoids a failure in rws_access_reg.  */
6287           if (! rws_insn_test (REG_VOLATILE))
6288             {
6289               new_flags.is_write = 1;
6290               rws_access_regno (REG_VOLATILE, new_flags, pred);
6291             }
6292           return 1;
6293         }
6294
6295       /* For all ASM_OPERANDS, we must traverse the vector of input operands.
6296          We cannot just fall through here since then we would be confused
6297          by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
6298          traditional asms unlike their normal usage.  */
6299
6300       for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; --i)
6301         if (rtx_needs_barrier (ASM_OPERANDS_INPUT (x, i), flags, pred))
6302           need_barrier = 1;
6303       break;
6304
6305     case PARALLEL:
6306       for (i = XVECLEN (x, 0) - 1; i >= 0; --i)
6307         {
6308           rtx pat = XVECEXP (x, 0, i);
6309           switch (GET_CODE (pat))
6310             {
6311             case SET:
6312               update_set_flags (pat, &new_flags);
6313               need_barrier |= set_src_needs_barrier (pat, new_flags, pred);
6314               break;
6315
6316             case USE:
6317             case CALL:
6318             case ASM_OPERANDS:
6319               need_barrier |= rtx_needs_barrier (pat, flags, pred);
6320               break;
6321
6322             case CLOBBER:
6323             case RETURN:
6324               break;
6325
6326             default:
6327               gcc_unreachable ();
6328             }
6329         }
6330       for (i = XVECLEN (x, 0) - 1; i >= 0; --i)
6331         {
6332           rtx pat = XVECEXP (x, 0, i);
6333           if (GET_CODE (pat) == SET)
6334             {
6335               if (GET_CODE (SET_SRC (pat)) != CALL)
6336                 {
6337                   new_flags.is_write = 1;
6338                   need_barrier |= rtx_needs_barrier (SET_DEST (pat), new_flags,
6339                                                      pred);
6340                 }
6341             }
6342           else if (GET_CODE (pat) == CLOBBER || GET_CODE (pat) == RETURN)
6343             need_barrier |= rtx_needs_barrier (pat, flags, pred);
6344         }
6345       break;
6346
6347     case SUBREG:
6348       need_barrier |= rtx_needs_barrier (SUBREG_REG (x), flags, pred);
6349       break;
6350     case REG:
6351       if (REGNO (x) == AR_UNAT_REGNUM)
6352         {
6353           for (i = 0; i < 64; ++i)
6354             need_barrier |= rws_access_regno (AR_UNAT_BIT_0 + i, flags, pred);
6355         }
6356       else
6357         need_barrier = rws_access_reg (x, flags, pred);
6358       break;
6359
6360     case MEM:
6361       /* Find the regs used in memory address computation.  */
6362       new_flags.is_write = 0;
6363       need_barrier = rtx_needs_barrier (XEXP (x, 0), new_flags, pred);
6364       break;
6365
6366     case CONST_INT:   case CONST_DOUBLE:  case CONST_VECTOR:
6367     case SYMBOL_REF:  case LABEL_REF:     case CONST:
6368       break;
6369
6370       /* Operators with side-effects.  */
6371     case POST_INC:    case POST_DEC:
6372       gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
6373
6374       new_flags.is_write = 0;
6375       need_barrier  = rws_access_reg (XEXP (x, 0), new_flags, pred);
6376       new_flags.is_write = 1;
6377       need_barrier |= rws_access_reg (XEXP (x, 0), new_flags, pred);
6378       break;
6379
6380     case POST_MODIFY:
6381       gcc_assert (GET_CODE (XEXP (x, 0)) == REG);
6382
6383       new_flags.is_write = 0;
6384       need_barrier  = rws_access_reg (XEXP (x, 0), new_flags, pred);
6385       need_barrier |= rtx_needs_barrier (XEXP (x, 1), new_flags, pred);
6386       new_flags.is_write = 1;
6387       need_barrier |= rws_access_reg (XEXP (x, 0), new_flags, pred);
6388       break;
6389
6390       /* Handle common unary and binary ops for efficiency.  */
6391     case COMPARE:  case PLUS:    case MINUS:   case MULT:      case DIV:
6392     case MOD:      case UDIV:    case UMOD:    case AND:       case IOR:
6393     case XOR:      case ASHIFT:  case ROTATE:  case ASHIFTRT:  case LSHIFTRT:
6394     case ROTATERT: case SMIN:    case SMAX:    case UMIN:      case UMAX:
6395     case NE:       case EQ:      case GE:      case GT:        case LE:
6396     case LT:       case GEU:     case GTU:     case LEU:       case LTU:
6397       need_barrier = rtx_needs_barrier (XEXP (x, 0), new_flags, pred);
6398       need_barrier |= rtx_needs_barrier (XEXP (x, 1), new_flags, pred);
6399       break;
6400
6401     case NEG:      case NOT:            case SIGN_EXTEND:     case ZERO_EXTEND:
6402     case TRUNCATE: case FLOAT_EXTEND:   case FLOAT_TRUNCATE:  case FLOAT:
6403     case FIX:      case UNSIGNED_FLOAT: case UNSIGNED_FIX:    case ABS:
6404     case SQRT:     case FFS:            case POPCOUNT:
6405       need_barrier = rtx_needs_barrier (XEXP (x, 0), flags, pred);
6406       break;
6407
6408     case VEC_SELECT:
6409       /* VEC_SELECT's second argument is a PARALLEL with integers that
6410          describe the elements selected.  On ia64, those integers are
6411          always constants.  Avoid walking the PARALLEL so that we don't
6412          get confused with "normal" parallels and then die.  */
6413       need_barrier = rtx_needs_barrier (XEXP (x, 0), flags, pred);
6414       break;
6415
6416     case UNSPEC:
6417       switch (XINT (x, 1))
6418         {
6419         case UNSPEC_LTOFF_DTPMOD:
6420         case UNSPEC_LTOFF_DTPREL:
6421         case UNSPEC_DTPREL:
6422         case UNSPEC_LTOFF_TPREL:
6423         case UNSPEC_TPREL:
6424         case UNSPEC_PRED_REL_MUTEX:
6425         case UNSPEC_PIC_CALL:
6426         case UNSPEC_MF:
6427         case UNSPEC_FETCHADD_ACQ:
6428         case UNSPEC_BSP_VALUE:
6429         case UNSPEC_FLUSHRS:
6430         case UNSPEC_BUNDLE_SELECTOR:
6431           break;
6432
6433         case UNSPEC_GR_SPILL:
6434         case UNSPEC_GR_RESTORE:
6435           {
6436             HOST_WIDE_INT offset = INTVAL (XVECEXP (x, 0, 1));
6437             HOST_WIDE_INT bit = (offset >> 3) & 63;
6438
6439             need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
6440             new_flags.is_write = (XINT (x, 1) == UNSPEC_GR_SPILL);
6441             need_barrier |= rws_access_regno (AR_UNAT_BIT_0 + bit,
6442                                               new_flags, pred);
6443             break;
6444           }
6445
6446         case UNSPEC_FR_SPILL:
6447         case UNSPEC_FR_RESTORE:
6448         case UNSPEC_GETF_EXP:
6449         case UNSPEC_SETF_EXP:
6450         case UNSPEC_ADDP4:
6451         case UNSPEC_FR_SQRT_RECIP_APPROX:
6452         case UNSPEC_FR_SQRT_RECIP_APPROX_RES:
6453         case UNSPEC_LDA:
6454         case UNSPEC_LDS:
6455         case UNSPEC_LDS_A:
6456         case UNSPEC_LDSA:
6457         case UNSPEC_CHKACLR:
6458         case UNSPEC_CHKS:
6459           need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
6460           break;
6461
6462         case UNSPEC_FR_RECIP_APPROX:
6463         case UNSPEC_SHRP:
6464         case UNSPEC_COPYSIGN:
6465         case UNSPEC_FR_RECIP_APPROX_RES:
6466           need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
6467           need_barrier |= rtx_needs_barrier (XVECEXP (x, 0, 1), flags, pred);
6468           break;
6469
6470         case UNSPEC_CMPXCHG_ACQ:
6471           need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 1), flags, pred);
6472           need_barrier |= rtx_needs_barrier (XVECEXP (x, 0, 2), flags, pred);
6473           break;
6474
6475         default:
6476           gcc_unreachable ();
6477         }
6478       break;
6479
6480     case UNSPEC_VOLATILE:
6481       switch (XINT (x, 1))
6482         {
6483         case UNSPECV_ALLOC:
6484           /* Alloc must always be the first instruction of a group.
6485              We force this by always returning true.  */
6486           /* ??? We might get better scheduling if we explicitly check for
6487              input/local/output register dependencies, and modify the
6488              scheduler so that alloc is always reordered to the start of
6489              the current group.  We could then eliminate all of the
6490              first_instruction code.  */
6491           rws_access_regno (AR_PFS_REGNUM, flags, pred);
6492
6493           new_flags.is_write = 1;
6494           rws_access_regno (REG_AR_CFM, new_flags, pred);
6495           return 1;
6496
6497         case UNSPECV_SET_BSP:
6498           need_barrier = 1;
6499           break;
6500
6501         case UNSPECV_BLOCKAGE:
6502         case UNSPECV_INSN_GROUP_BARRIER:
6503         case UNSPECV_BREAK:
6504         case UNSPECV_PSAC_ALL:
6505         case UNSPECV_PSAC_NORMAL:
6506           return 0;
6507
6508         default:
6509           gcc_unreachable ();
6510         }
6511       break;
6512
6513     case RETURN:
6514       new_flags.is_write = 0;
6515       need_barrier  = rws_access_regno (REG_RP, flags, pred);
6516       need_barrier |= rws_access_regno (AR_PFS_REGNUM, flags, pred);
6517
6518       new_flags.is_write = 1;
6519       need_barrier |= rws_access_regno (AR_EC_REGNUM, new_flags, pred);
6520       need_barrier |= rws_access_regno (REG_AR_CFM, new_flags, pred);
6521       break;
6522
6523     default:
6524       format_ptr = GET_RTX_FORMAT (GET_CODE (x));
6525       for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6526         switch (format_ptr[i])
6527           {
6528           case '0':     /* unused field */
6529           case 'i':     /* integer */
6530           case 'n':     /* note */
6531           case 'w':     /* wide integer */
6532           case 's':     /* pointer to string */
6533           case 'S':     /* optional pointer to string */
6534             break;
6535
6536           case 'e':
6537             if (rtx_needs_barrier (XEXP (x, i), flags, pred))
6538               need_barrier = 1;
6539             break;
6540
6541           case 'E':
6542             for (j = XVECLEN (x, i) - 1; j >= 0; --j)
6543               if (rtx_needs_barrier (XVECEXP (x, i, j), flags, pred))
6544                 need_barrier = 1;
6545             break;
6546
6547           default:
6548             gcc_unreachable ();
6549           }
6550       break;
6551     }
6552   return need_barrier;
6553 }
6554
6555 /* Clear out the state for group_barrier_needed at the start of a
6556    sequence of insns.  */
6557
6558 static void
6559 init_insn_group_barriers (void)
6560 {
6561   memset (rws_sum, 0, sizeof (rws_sum));
6562   first_instruction = 1;
6563 }
6564
6565 /* Given the current state, determine whether a group barrier (a stop bit) is
6566    necessary before INSN.  Return nonzero if so.  This modifies the state to
6567    include the effects of INSN as a side-effect.  */
6568
6569 static int
6570 group_barrier_needed (rtx insn)
6571 {
6572   rtx pat;
6573   int need_barrier = 0;
6574   struct reg_flags flags;
6575
6576   memset (&flags, 0, sizeof (flags));
6577   switch (GET_CODE (insn))
6578     {
6579     case NOTE:
6580     case DEBUG_INSN:
6581       break;
6582
6583     case BARRIER:
6584       /* A barrier doesn't imply an instruction group boundary.  */
6585       break;
6586
6587     case CODE_LABEL:
6588       memset (rws_insn, 0, sizeof (rws_insn));
6589       return 1;
6590
6591     case CALL_INSN:
6592       flags.is_branch = 1;
6593       flags.is_sibcall = SIBLING_CALL_P (insn);
6594       memset (rws_insn, 0, sizeof (rws_insn));
6595
6596       /* Don't bundle a call following another call.  */
6597       if ((pat = prev_active_insn (insn))
6598           && GET_CODE (pat) == CALL_INSN)
6599         {
6600           need_barrier = 1;
6601           break;
6602         }
6603
6604       need_barrier = rtx_needs_barrier (PATTERN (insn), flags, 0);
6605       break;
6606
6607     case JUMP_INSN:
6608       if (!ia64_spec_check_p (insn))
6609         flags.is_branch = 1;
6610
6611       /* Don't bundle a jump following a call.  */
6612       if ((pat = prev_active_insn (insn))
6613           && GET_CODE (pat) == CALL_INSN)
6614         {
6615           need_barrier = 1;
6616           break;
6617         }
6618       /* FALLTHRU */
6619
6620     case INSN:
6621       if (GET_CODE (PATTERN (insn)) == USE
6622           || GET_CODE (PATTERN (insn)) == CLOBBER)
6623         /* Don't care about USE and CLOBBER "insns"---those are used to
6624            indicate to the optimizer that it shouldn't get rid of
6625            certain operations.  */
6626         break;
6627
6628       pat = PATTERN (insn);
6629
6630       /* Ug.  Hack hacks hacked elsewhere.  */
6631       switch (recog_memoized (insn))
6632         {
6633           /* We play dependency tricks with the epilogue in order
6634              to get proper schedules.  Undo this for dv analysis.  */
6635         case CODE_FOR_epilogue_deallocate_stack:
6636         case CODE_FOR_prologue_allocate_stack:
6637           pat = XVECEXP (pat, 0, 0);
6638           break;
6639
6640           /* The pattern we use for br.cloop confuses the code above.
6641              The second element of the vector is representative.  */
6642         case CODE_FOR_doloop_end_internal:
6643           pat = XVECEXP (pat, 0, 1);
6644           break;
6645
6646           /* Doesn't generate code.  */
6647         case CODE_FOR_pred_rel_mutex:
6648         case CODE_FOR_prologue_use:
6649           return 0;
6650
6651         default:
6652           break;
6653         }
6654
6655       memset (rws_insn, 0, sizeof (rws_insn));
6656       need_barrier = rtx_needs_barrier (pat, flags, 0);
6657
6658       /* Check to see if the previous instruction was a volatile
6659          asm.  */
6660       if (! need_barrier)
6661         need_barrier = rws_access_regno (REG_VOLATILE, flags, 0);
6662
6663       break;
6664
6665     default:
6666       gcc_unreachable ();
6667     }
6668
6669   if (first_instruction && INSN_P (insn)
6670       && ia64_safe_itanium_class (insn) != ITANIUM_CLASS_IGNORE
6671       && GET_CODE (PATTERN (insn)) != USE
6672       && GET_CODE (PATTERN (insn)) != CLOBBER)
6673     {
6674       need_barrier = 0;
6675       first_instruction = 0;
6676     }
6677
6678   return need_barrier;
6679 }
6680
6681 /* Like group_barrier_needed, but do not clobber the current state.  */
6682
6683 static int
6684 safe_group_barrier_needed (rtx insn)
6685 {
6686   int saved_first_instruction;
6687   int t;
6688
6689   saved_first_instruction = first_instruction;
6690   in_safe_group_barrier = 1;
6691
6692   t = group_barrier_needed (insn);
6693
6694   first_instruction = saved_first_instruction;
6695   in_safe_group_barrier = 0;
6696
6697   return t;
6698 }
6699
6700 /* Scan the current function and insert stop bits as necessary to
6701    eliminate dependencies.  This function assumes that a final
6702    instruction scheduling pass has been run which has already
6703    inserted most of the necessary stop bits.  This function only
6704    inserts new ones at basic block boundaries, since these are
6705    invisible to the scheduler.  */
6706
6707 static void
6708 emit_insn_group_barriers (FILE *dump)
6709 {
6710   rtx insn;
6711   rtx last_label = 0;
6712   int insns_since_last_label = 0;
6713
6714   init_insn_group_barriers ();
6715
6716   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6717     {
6718       if (GET_CODE (insn) == CODE_LABEL)
6719         {
6720           if (insns_since_last_label)
6721             last_label = insn;
6722           insns_since_last_label = 0;
6723         }
6724       else if (GET_CODE (insn) == NOTE
6725                && NOTE_KIND (insn) == NOTE_INSN_BASIC_BLOCK)
6726         {
6727           if (insns_since_last_label)
6728             last_label = insn;
6729           insns_since_last_label = 0;
6730         }
6731       else if (GET_CODE (insn) == INSN
6732                && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
6733                && XINT (PATTERN (insn), 1) == UNSPECV_INSN_GROUP_BARRIER)
6734         {
6735           init_insn_group_barriers ();
6736           last_label = 0;
6737         }
6738       else if (NONDEBUG_INSN_P (insn))
6739         {
6740           insns_since_last_label = 1;
6741
6742           if (group_barrier_needed (insn))
6743             {
6744               if (last_label)
6745                 {
6746                   if (dump)
6747                     fprintf (dump, "Emitting stop before label %d\n",
6748                              INSN_UID (last_label));
6749                   emit_insn_before (gen_insn_group_barrier (GEN_INT (3)), last_label);
6750                   insn = last_label;
6751
6752                   init_insn_group_barriers ();
6753                   last_label = 0;
6754                 }
6755             }
6756         }
6757     }
6758 }
6759
6760 /* Like emit_insn_group_barriers, but run if no final scheduling pass was run.
6761    This function has to emit all necessary group barriers.  */
6762
6763 static void
6764 emit_all_insn_group_barriers (FILE *dump ATTRIBUTE_UNUSED)
6765 {
6766   rtx insn;
6767
6768   init_insn_group_barriers ();
6769
6770   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6771     {
6772       if (GET_CODE (insn) == BARRIER)
6773         {
6774           rtx last = prev_active_insn (insn);
6775
6776           if (! last)
6777             continue;
6778           if (GET_CODE (last) == JUMP_INSN
6779               && GET_CODE (PATTERN (last)) == ADDR_DIFF_VEC)
6780             last = prev_active_insn (last);
6781           if (recog_memoized (last) != CODE_FOR_insn_group_barrier)
6782             emit_insn_after (gen_insn_group_barrier (GEN_INT (3)), last);
6783
6784           init_insn_group_barriers ();
6785         }
6786       else if (NONDEBUG_INSN_P (insn))
6787         {
6788           if (recog_memoized (insn) == CODE_FOR_insn_group_barrier)
6789             init_insn_group_barriers ();
6790           else if (group_barrier_needed (insn))
6791             {
6792               emit_insn_before (gen_insn_group_barrier (GEN_INT (3)), insn);
6793               init_insn_group_barriers ();
6794               group_barrier_needed (insn);
6795             }
6796         }
6797     }
6798 }
6799
6800 \f
6801
6802 /* Instruction scheduling support.  */
6803
6804 #define NR_BUNDLES 10
6805
6806 /* A list of names of all available bundles.  */
6807
6808 static const char *bundle_name [NR_BUNDLES] =
6809 {
6810   ".mii",
6811   ".mmi",
6812   ".mfi",
6813   ".mmf",
6814 #if NR_BUNDLES == 10
6815   ".bbb",
6816   ".mbb",
6817 #endif
6818   ".mib",
6819   ".mmb",
6820   ".mfb",
6821   ".mlx"
6822 };
6823
6824 /* Nonzero if we should insert stop bits into the schedule.  */
6825
6826 int ia64_final_schedule = 0;
6827
6828 /* Codes of the corresponding queried units: */
6829
6830 static int _0mii_, _0mmi_, _0mfi_, _0mmf_;
6831 static int _0bbb_, _0mbb_, _0mib_, _0mmb_, _0mfb_, _0mlx_;
6832
6833 static int _1mii_, _1mmi_, _1mfi_, _1mmf_;
6834 static int _1bbb_, _1mbb_, _1mib_, _1mmb_, _1mfb_, _1mlx_;
6835
6836 static int pos_1, pos_2, pos_3, pos_4, pos_5, pos_6;
6837
6838 /* The following variable value is an insn group barrier.  */
6839
6840 static rtx dfa_stop_insn;
6841
6842 /* The following variable value is the last issued insn.  */
6843
6844 static rtx last_scheduled_insn;
6845
6846 /* The following variable value is pointer to a DFA state used as
6847    temporary variable.  */
6848
6849 static state_t temp_dfa_state = NULL;
6850
6851 /* The following variable value is DFA state after issuing the last
6852    insn.  */
6853
6854 static state_t prev_cycle_state = NULL;
6855
6856 /* The following array element values are TRUE if the corresponding
6857    insn requires to add stop bits before it.  */
6858
6859 static char *stops_p = NULL;
6860
6861 /* The following variable is used to set up the mentioned above array.  */
6862
6863 static int stop_before_p = 0;
6864
6865 /* The following variable value is length of the arrays `clocks' and
6866    `add_cycles'. */
6867
6868 static int clocks_length;
6869
6870 /* The following variable value is number of data speculations in progress.  */
6871 static int pending_data_specs = 0;
6872
6873 /* Number of memory references on current and three future processor cycles.  */
6874 static char mem_ops_in_group[4];
6875
6876 /* Number of current processor cycle (from scheduler's point of view).  */
6877 static int current_cycle;
6878
6879 static rtx ia64_single_set (rtx);
6880 static void ia64_emit_insn_before (rtx, rtx);
6881
6882 /* Map a bundle number to its pseudo-op.  */
6883
6884 const char *
6885 get_bundle_name (int b)
6886 {
6887   return bundle_name[b];
6888 }
6889
6890
6891 /* Return the maximum number of instructions a cpu can issue.  */
6892
6893 static int
6894 ia64_issue_rate (void)
6895 {
6896   return 6;
6897 }
6898
6899 /* Helper function - like single_set, but look inside COND_EXEC.  */
6900
6901 static rtx
6902 ia64_single_set (rtx insn)
6903 {
6904   rtx x = PATTERN (insn), ret;
6905   if (GET_CODE (x) == COND_EXEC)
6906     x = COND_EXEC_CODE (x);
6907   if (GET_CODE (x) == SET)
6908     return x;
6909
6910   /* Special case here prologue_allocate_stack and epilogue_deallocate_stack.
6911      Although they are not classical single set, the second set is there just
6912      to protect it from moving past FP-relative stack accesses.  */
6913   switch (recog_memoized (insn))
6914     {
6915     case CODE_FOR_prologue_allocate_stack:
6916     case CODE_FOR_epilogue_deallocate_stack:
6917       ret = XVECEXP (x, 0, 0);
6918       break;
6919
6920     default:
6921       ret = single_set_2 (insn, x);
6922       break;
6923     }
6924
6925   return ret;
6926 }
6927
6928 /* Adjust the cost of a scheduling dependency.
6929    Return the new cost of a dependency of type DEP_TYPE or INSN on DEP_INSN.
6930    COST is the current cost, DW is dependency weakness.  */
6931 static int
6932 ia64_adjust_cost_2 (rtx insn, int dep_type1, rtx dep_insn, int cost, dw_t dw)
6933 {
6934   enum reg_note dep_type = (enum reg_note) dep_type1;
6935   enum attr_itanium_class dep_class;
6936   enum attr_itanium_class insn_class;
6937
6938   insn_class = ia64_safe_itanium_class (insn);
6939   dep_class = ia64_safe_itanium_class (dep_insn);
6940
6941   /* Treat true memory dependencies separately.  Ignore apparent true
6942      dependence between store and call (call has a MEM inside a SYMBOL_REF).  */
6943   if (dep_type == REG_DEP_TRUE
6944       && (dep_class == ITANIUM_CLASS_ST || dep_class == ITANIUM_CLASS_STF)
6945       && (insn_class == ITANIUM_CLASS_BR || insn_class == ITANIUM_CLASS_SCALL))
6946     return 0;
6947
6948   if (dw == MIN_DEP_WEAK)
6949     /* Store and load are likely to alias, use higher cost to avoid stall.  */
6950     return PARAM_VALUE (PARAM_SCHED_MEM_TRUE_DEP_COST);
6951   else if (dw > MIN_DEP_WEAK)
6952     {
6953       /* Store and load are less likely to alias.  */
6954       if (mflag_sched_fp_mem_deps_zero_cost && dep_class == ITANIUM_CLASS_STF)
6955         /* Assume there will be no cache conflict for floating-point data.
6956            For integer data, L1 conflict penalty is huge (17 cycles), so we
6957            never assume it will not cause a conflict.  */
6958         return 0;
6959       else
6960         return cost;
6961     }
6962
6963   if (dep_type != REG_DEP_OUTPUT)
6964     return cost;
6965
6966   if (dep_class == ITANIUM_CLASS_ST || dep_class == ITANIUM_CLASS_STF
6967       || insn_class == ITANIUM_CLASS_ST || insn_class == ITANIUM_CLASS_STF)
6968     return 0;
6969
6970   return cost;
6971 }
6972
6973 /* Like emit_insn_before, but skip cycle_display notes.
6974    ??? When cycle display notes are implemented, update this.  */
6975
6976 static void
6977 ia64_emit_insn_before (rtx insn, rtx before)
6978 {
6979   emit_insn_before (insn, before);
6980 }
6981
6982 /* The following function marks insns who produce addresses for load
6983    and store insns.  Such insns will be placed into M slots because it
6984    decrease latency time for Itanium1 (see function
6985    `ia64_produce_address_p' and the DFA descriptions).  */
6986
6987 static void
6988 ia64_dependencies_evaluation_hook (rtx head, rtx tail)
6989 {
6990   rtx insn, next, next_tail;
6991
6992   /* Before reload, which_alternative is not set, which means that
6993      ia64_safe_itanium_class will produce wrong results for (at least)
6994      move instructions.  */
6995   if (!reload_completed)
6996     return;
6997
6998   next_tail = NEXT_INSN (tail);
6999   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
7000     if (INSN_P (insn))
7001       insn->call = 0;
7002   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
7003     if (INSN_P (insn)
7004         && ia64_safe_itanium_class (insn) == ITANIUM_CLASS_IALU)
7005       {
7006         sd_iterator_def sd_it;
7007         dep_t dep;
7008         bool has_mem_op_consumer_p = false;
7009
7010         FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
7011           {
7012             enum attr_itanium_class c;
7013
7014             if (DEP_TYPE (dep) != REG_DEP_TRUE)
7015               continue;
7016
7017             next = DEP_CON (dep);
7018             c = ia64_safe_itanium_class (next);
7019             if ((c == ITANIUM_CLASS_ST
7020                  || c == ITANIUM_CLASS_STF)
7021                 && ia64_st_address_bypass_p (insn, next))
7022               {
7023                 has_mem_op_consumer_p = true;
7024                 break;
7025               }
7026             else if ((c == ITANIUM_CLASS_LD
7027                       || c == ITANIUM_CLASS_FLD
7028                       || c == ITANIUM_CLASS_FLDP)
7029                      && ia64_ld_address_bypass_p (insn, next))
7030               {
7031                 has_mem_op_consumer_p = true;
7032                 break;
7033               }
7034           }
7035
7036         insn->call = has_mem_op_consumer_p;
7037       }
7038 }
7039
7040 /* We're beginning a new block.  Initialize data structures as necessary.  */
7041
7042 static void
7043 ia64_sched_init (FILE *dump ATTRIBUTE_UNUSED,
7044                  int sched_verbose ATTRIBUTE_UNUSED,
7045                  int max_ready ATTRIBUTE_UNUSED)
7046 {
7047 #ifdef ENABLE_CHECKING
7048   rtx insn;
7049
7050   if (!sel_sched_p () && reload_completed)
7051     for (insn = NEXT_INSN (current_sched_info->prev_head);
7052          insn != current_sched_info->next_tail;
7053          insn = NEXT_INSN (insn))
7054       gcc_assert (!SCHED_GROUP_P (insn));
7055 #endif
7056   last_scheduled_insn = NULL_RTX;
7057   init_insn_group_barriers ();
7058
7059   current_cycle = 0;
7060   memset (mem_ops_in_group, 0, sizeof (mem_ops_in_group));
7061 }
7062
7063 /* We're beginning a scheduling pass.  Check assertion.  */
7064
7065 static void
7066 ia64_sched_init_global (FILE *dump ATTRIBUTE_UNUSED,
7067                         int sched_verbose ATTRIBUTE_UNUSED,
7068                         int max_ready ATTRIBUTE_UNUSED)
7069 {  
7070   gcc_assert (pending_data_specs == 0);
7071 }
7072
7073 /* Scheduling pass is now finished.  Free/reset static variable.  */
7074 static void
7075 ia64_sched_finish_global (FILE *dump ATTRIBUTE_UNUSED,
7076                           int sched_verbose ATTRIBUTE_UNUSED)
7077 {
7078   gcc_assert (pending_data_specs == 0);
7079 }
7080
7081 /* Return TRUE if INSN is a load (either normal or speculative, but not a
7082    speculation check), FALSE otherwise.  */
7083 static bool
7084 is_load_p (rtx insn)
7085 {
7086   enum attr_itanium_class insn_class = ia64_safe_itanium_class (insn);
7087
7088   return
7089    ((insn_class == ITANIUM_CLASS_LD || insn_class == ITANIUM_CLASS_FLD)
7090     && get_attr_check_load (insn) == CHECK_LOAD_NO);
7091 }
7092
7093 /* If INSN is a memory reference, memoize it in MEM_OPS_IN_GROUP global array
7094    (taking account for 3-cycle cache reference postponing for stores: Intel
7095    Itanium 2 Reference Manual for Software Development and Optimization,
7096    6.7.3.1).  */
7097 static void
7098 record_memory_reference (rtx insn)
7099 {
7100   enum attr_itanium_class insn_class = ia64_safe_itanium_class (insn);
7101
7102   switch (insn_class) {
7103     case ITANIUM_CLASS_FLD:
7104     case ITANIUM_CLASS_LD:
7105       mem_ops_in_group[current_cycle % 4]++;
7106       break;
7107     case ITANIUM_CLASS_STF:
7108     case ITANIUM_CLASS_ST:
7109       mem_ops_in_group[(current_cycle + 3) % 4]++;
7110       break;
7111     default:;
7112   }
7113 }
7114
7115 /* We are about to being issuing insns for this clock cycle.
7116    Override the default sort algorithm to better slot instructions.  */
7117
7118 static int
7119 ia64_dfa_sched_reorder (FILE *dump, int sched_verbose, rtx *ready,
7120                         int *pn_ready, int clock_var,
7121                         int reorder_type)
7122 {
7123   int n_asms;
7124   int n_ready = *pn_ready;
7125   rtx *e_ready = ready + n_ready;
7126   rtx *insnp;
7127
7128   if (sched_verbose)
7129     fprintf (dump, "// ia64_dfa_sched_reorder (type %d):\n", reorder_type);
7130
7131   if (reorder_type == 0)
7132     {
7133       /* First, move all USEs, CLOBBERs and other crud out of the way.  */
7134       n_asms = 0;
7135       for (insnp = ready; insnp < e_ready; insnp++)
7136         if (insnp < e_ready)
7137           {
7138             rtx insn = *insnp;
7139             enum attr_type t = ia64_safe_type (insn);
7140             if (t == TYPE_UNKNOWN)
7141               {
7142                 if (GET_CODE (PATTERN (insn)) == ASM_INPUT
7143                     || asm_noperands (PATTERN (insn)) >= 0)
7144                   {
7145                     rtx lowest = ready[n_asms];
7146                     ready[n_asms] = insn;
7147                     *insnp = lowest;
7148                     n_asms++;
7149                   }
7150                 else
7151                   {
7152                     rtx highest = ready[n_ready - 1];
7153                     ready[n_ready - 1] = insn;
7154                     *insnp = highest;
7155                     return 1;
7156                   }
7157               }
7158           }
7159
7160       if (n_asms < n_ready)
7161         {
7162           /* Some normal insns to process.  Skip the asms.  */
7163           ready += n_asms;
7164           n_ready -= n_asms;
7165         }
7166       else if (n_ready > 0)
7167         return 1;
7168     }
7169
7170   if (ia64_final_schedule)
7171     {
7172       int deleted = 0;
7173       int nr_need_stop = 0;
7174
7175       for (insnp = ready; insnp < e_ready; insnp++)
7176         if (safe_group_barrier_needed (*insnp))
7177           nr_need_stop++;
7178
7179       if (reorder_type == 1 && n_ready == nr_need_stop)
7180         return 0;
7181       if (reorder_type == 0)
7182         return 1;
7183       insnp = e_ready;
7184       /* Move down everything that needs a stop bit, preserving
7185          relative order.  */
7186       while (insnp-- > ready + deleted)
7187         while (insnp >= ready + deleted)
7188           {
7189             rtx insn = *insnp;
7190             if (! safe_group_barrier_needed (insn))
7191               break;
7192             memmove (ready + 1, ready, (insnp - ready) * sizeof (rtx));
7193             *ready = insn;
7194             deleted++;
7195           }
7196       n_ready -= deleted;
7197       ready += deleted;
7198     }
7199
7200   current_cycle = clock_var;
7201   if (reload_completed && mem_ops_in_group[clock_var % 4] >= ia64_max_memory_insns)
7202     {
7203       int moved = 0;
7204
7205       insnp = e_ready;
7206       /* Move down loads/stores, preserving relative order.  */
7207       while (insnp-- > ready + moved)
7208         while (insnp >= ready + moved)
7209           {
7210             rtx insn = *insnp;
7211             if (! is_load_p (insn))
7212               break;
7213             memmove (ready + 1, ready, (insnp - ready) * sizeof (rtx));
7214             *ready = insn;
7215             moved++;
7216           }
7217       n_ready -= moved;
7218       ready += moved;
7219     }
7220
7221   return 1;
7222 }
7223
7224 /* We are about to being issuing insns for this clock cycle.  Override
7225    the default sort algorithm to better slot instructions.  */
7226
7227 static int
7228 ia64_sched_reorder (FILE *dump, int sched_verbose, rtx *ready, int *pn_ready,
7229                     int clock_var)
7230 {
7231   return ia64_dfa_sched_reorder (dump, sched_verbose, ready,
7232                                  pn_ready, clock_var, 0);
7233 }
7234
7235 /* Like ia64_sched_reorder, but called after issuing each insn.
7236    Override the default sort algorithm to better slot instructions.  */
7237
7238 static int
7239 ia64_sched_reorder2 (FILE *dump ATTRIBUTE_UNUSED,
7240                      int sched_verbose ATTRIBUTE_UNUSED, rtx *ready,
7241                      int *pn_ready, int clock_var)
7242 {
7243   return ia64_dfa_sched_reorder (dump, sched_verbose, ready, pn_ready,
7244                                  clock_var, 1);
7245 }
7246
7247 /* We are about to issue INSN.  Return the number of insns left on the
7248    ready queue that can be issued this cycle.  */
7249
7250 static int
7251 ia64_variable_issue (FILE *dump ATTRIBUTE_UNUSED,
7252                      int sched_verbose ATTRIBUTE_UNUSED,
7253                      rtx insn ATTRIBUTE_UNUSED,
7254                      int can_issue_more ATTRIBUTE_UNUSED)
7255 {
7256   if (sched_deps_info->generate_spec_deps && !sel_sched_p ())
7257     /* Modulo scheduling does not extend h_i_d when emitting
7258        new instructions.  Don't use h_i_d, if we don't have to.  */
7259     {
7260       if (DONE_SPEC (insn) & BEGIN_DATA)
7261         pending_data_specs++;
7262       if (CHECK_SPEC (insn) & BEGIN_DATA)
7263         pending_data_specs--;
7264     }
7265
7266   if (DEBUG_INSN_P (insn))
7267     return 1;
7268
7269   last_scheduled_insn = insn;
7270   memcpy (prev_cycle_state, curr_state, dfa_state_size);
7271   if (reload_completed)
7272     {
7273       int needed = group_barrier_needed (insn);
7274       
7275       gcc_assert (!needed);
7276       if (GET_CODE (insn) == CALL_INSN)
7277         init_insn_group_barriers ();
7278       stops_p [INSN_UID (insn)] = stop_before_p;
7279       stop_before_p = 0;
7280
7281       record_memory_reference (insn);
7282     }
7283   return 1;
7284 }
7285
7286 /* We are choosing insn from the ready queue.  Return nonzero if INSN
7287    can be chosen.  */
7288
7289 static int
7290 ia64_first_cycle_multipass_dfa_lookahead_guard (rtx insn)
7291 {
7292   gcc_assert (insn && INSN_P (insn));
7293   return ((!reload_completed
7294            || !safe_group_barrier_needed (insn))
7295           && ia64_first_cycle_multipass_dfa_lookahead_guard_spec (insn)
7296           && (!mflag_sched_mem_insns_hard_limit
7297               || !is_load_p (insn)
7298               || mem_ops_in_group[current_cycle % 4] < ia64_max_memory_insns));
7299 }
7300
7301 /* We are choosing insn from the ready queue.  Return nonzero if INSN
7302    can be chosen.  */
7303
7304 static bool
7305 ia64_first_cycle_multipass_dfa_lookahead_guard_spec (const_rtx insn)
7306 {
7307   gcc_assert (insn  && INSN_P (insn));
7308   /* Size of ALAT is 32.  As far as we perform conservative data speculation,
7309      we keep ALAT half-empty.  */
7310   return (pending_data_specs < 16
7311           || !(TODO_SPEC (insn) & BEGIN_DATA));
7312 }
7313
7314 /* The following variable value is pseudo-insn used by the DFA insn
7315    scheduler to change the DFA state when the simulated clock is
7316    increased.  */
7317
7318 static rtx dfa_pre_cycle_insn;
7319
7320 /* Returns 1 when a meaningful insn was scheduled between the last group
7321    barrier and LAST.  */
7322 static int
7323 scheduled_good_insn (rtx last)
7324 {
7325   if (last && recog_memoized (last) >= 0)
7326     return 1;
7327
7328   for ( ;
7329        last != NULL && !NOTE_INSN_BASIC_BLOCK_P (last)
7330        && !stops_p[INSN_UID (last)];
7331        last = PREV_INSN (last))
7332     /* We could hit a NOTE_INSN_DELETED here which is actually outside
7333        the ebb we're scheduling.  */
7334     if (INSN_P (last) && recog_memoized (last) >= 0)
7335       return 1;
7336
7337   return 0;
7338 }
7339
7340 /* We are about to being issuing INSN.  Return nonzero if we cannot
7341    issue it on given cycle CLOCK and return zero if we should not sort
7342    the ready queue on the next clock start.  */
7343
7344 static int
7345 ia64_dfa_new_cycle (FILE *dump, int verbose, rtx insn, int last_clock,
7346                     int clock, int *sort_p)
7347 {
7348   gcc_assert (insn && INSN_P (insn));
7349
7350   if (DEBUG_INSN_P (insn))
7351     return 0;
7352
7353   /* When a group barrier is needed for insn, last_scheduled_insn
7354      should be set.  */
7355   gcc_assert (!(reload_completed && safe_group_barrier_needed (insn))
7356               || last_scheduled_insn);
7357
7358   if ((reload_completed
7359        && (safe_group_barrier_needed (insn)
7360            || (mflag_sched_stop_bits_after_every_cycle
7361                && last_clock != clock
7362                && last_scheduled_insn
7363                && scheduled_good_insn (last_scheduled_insn))))
7364       || (last_scheduled_insn
7365           && (GET_CODE (last_scheduled_insn) == CALL_INSN
7366               || GET_CODE (PATTERN (last_scheduled_insn)) == ASM_INPUT
7367               || asm_noperands (PATTERN (last_scheduled_insn)) >= 0)))
7368     {
7369       init_insn_group_barriers ();
7370
7371       if (verbose && dump)
7372         fprintf (dump, "//    Stop should be before %d%s\n", INSN_UID (insn),
7373                  last_clock == clock ? " + cycle advance" : "");
7374
7375       stop_before_p = 1;
7376       current_cycle = clock;
7377       mem_ops_in_group[current_cycle % 4] = 0;
7378
7379       if (last_clock == clock)
7380         {
7381           state_transition (curr_state, dfa_stop_insn);
7382           if (TARGET_EARLY_STOP_BITS)
7383             *sort_p = (last_scheduled_insn == NULL_RTX
7384                        || GET_CODE (last_scheduled_insn) != CALL_INSN);
7385           else
7386             *sort_p = 0;
7387           return 1;
7388         }
7389
7390       if (last_scheduled_insn)
7391         {
7392           if (GET_CODE (PATTERN (last_scheduled_insn)) == ASM_INPUT
7393               || asm_noperands (PATTERN (last_scheduled_insn)) >= 0)
7394             state_reset (curr_state);
7395           else
7396             {
7397               memcpy (curr_state, prev_cycle_state, dfa_state_size);
7398               state_transition (curr_state, dfa_stop_insn);
7399               state_transition (curr_state, dfa_pre_cycle_insn);
7400               state_transition (curr_state, NULL);
7401             }
7402         }
7403     }
7404   return 0;
7405 }
7406
7407 /* Implement targetm.sched.h_i_d_extended hook.
7408    Extend internal data structures.  */
7409 static void
7410 ia64_h_i_d_extended (void)
7411 {
7412   if (stops_p != NULL) 
7413     {
7414       int new_clocks_length = get_max_uid () * 3 / 2;
7415       stops_p = (char *) xrecalloc (stops_p, new_clocks_length, clocks_length, 1);
7416       clocks_length = new_clocks_length;
7417     }
7418 }
7419 \f
7420
7421 /* This structure describes the data used by the backend to guide scheduling.
7422    When the current scheduling point is switched, this data should be saved
7423    and restored later, if the scheduler returns to this point.  */
7424 struct _ia64_sched_context
7425 {
7426   state_t prev_cycle_state;
7427   rtx last_scheduled_insn;
7428   struct reg_write_state rws_sum[NUM_REGS];
7429   struct reg_write_state rws_insn[NUM_REGS];
7430   int first_instruction;
7431   int pending_data_specs;
7432   int current_cycle;
7433   char mem_ops_in_group[4];
7434 };
7435 typedef struct _ia64_sched_context *ia64_sched_context_t;
7436
7437 /* Allocates a scheduling context.  */
7438 static void *
7439 ia64_alloc_sched_context (void)
7440 {
7441   return xmalloc (sizeof (struct _ia64_sched_context));
7442 }
7443
7444 /* Initializes the _SC context with clean data, if CLEAN_P, and from
7445    the global context otherwise.  */
7446 static void
7447 ia64_init_sched_context (void *_sc, bool clean_p)
7448 {
7449   ia64_sched_context_t sc = (ia64_sched_context_t) _sc;
7450
7451   sc->prev_cycle_state = xmalloc (dfa_state_size);
7452   if (clean_p)
7453     {
7454       state_reset (sc->prev_cycle_state);
7455       sc->last_scheduled_insn = NULL_RTX;
7456       memset (sc->rws_sum, 0, sizeof (rws_sum));
7457       memset (sc->rws_insn, 0, sizeof (rws_insn));
7458       sc->first_instruction = 1;
7459       sc->pending_data_specs = 0;
7460       sc->current_cycle = 0;
7461       memset (sc->mem_ops_in_group, 0, sizeof (mem_ops_in_group));
7462     }
7463   else
7464     {
7465       memcpy (sc->prev_cycle_state, prev_cycle_state, dfa_state_size);
7466       sc->last_scheduled_insn = last_scheduled_insn;
7467       memcpy (sc->rws_sum, rws_sum, sizeof (rws_sum));
7468       memcpy (sc->rws_insn, rws_insn, sizeof (rws_insn));
7469       sc->first_instruction = first_instruction;
7470       sc->pending_data_specs = pending_data_specs;
7471       sc->current_cycle = current_cycle;
7472       memcpy (sc->mem_ops_in_group, mem_ops_in_group, sizeof (mem_ops_in_group));
7473     }
7474 }
7475
7476 /* Sets the global scheduling context to the one pointed to by _SC.  */
7477 static void
7478 ia64_set_sched_context (void *_sc)
7479 {
7480   ia64_sched_context_t sc = (ia64_sched_context_t) _sc;
7481
7482   gcc_assert (sc != NULL);
7483
7484   memcpy (prev_cycle_state, sc->prev_cycle_state, dfa_state_size);
7485   last_scheduled_insn = sc->last_scheduled_insn;
7486   memcpy (rws_sum, sc->rws_sum, sizeof (rws_sum));
7487   memcpy (rws_insn, sc->rws_insn, sizeof (rws_insn));
7488   first_instruction = sc->first_instruction;
7489   pending_data_specs = sc->pending_data_specs;
7490   current_cycle = sc->current_cycle;
7491   memcpy (mem_ops_in_group, sc->mem_ops_in_group, sizeof (mem_ops_in_group));
7492 }
7493
7494 /* Clears the data in the _SC scheduling context.  */
7495 static void
7496 ia64_clear_sched_context (void *_sc)
7497 {
7498   ia64_sched_context_t sc = (ia64_sched_context_t) _sc;
7499   
7500   free (sc->prev_cycle_state);
7501   sc->prev_cycle_state = NULL;
7502 }
7503
7504 /* Frees the _SC scheduling context.  */
7505 static void
7506 ia64_free_sched_context (void *_sc)
7507 {
7508   gcc_assert (_sc != NULL);
7509
7510   free (_sc);
7511 }
7512
7513 typedef rtx (* gen_func_t) (rtx, rtx);
7514
7515 /* Return a function that will generate a load of mode MODE_NO
7516    with speculation types TS.  */
7517 static gen_func_t
7518 get_spec_load_gen_function (ds_t ts, int mode_no)
7519 {
7520   static gen_func_t gen_ld_[] = {
7521     gen_movbi,
7522     gen_movqi_internal,
7523     gen_movhi_internal,
7524     gen_movsi_internal,
7525     gen_movdi_internal,
7526     gen_movsf_internal,
7527     gen_movdf_internal,
7528     gen_movxf_internal,
7529     gen_movti_internal,
7530     gen_zero_extendqidi2,
7531     gen_zero_extendhidi2,
7532     gen_zero_extendsidi2,
7533   };
7534
7535   static gen_func_t gen_ld_a[] = {
7536     gen_movbi_advanced,
7537     gen_movqi_advanced,
7538     gen_movhi_advanced,
7539     gen_movsi_advanced,
7540     gen_movdi_advanced,
7541     gen_movsf_advanced,
7542     gen_movdf_advanced,
7543     gen_movxf_advanced,
7544     gen_movti_advanced,
7545     gen_zero_extendqidi2_advanced,
7546     gen_zero_extendhidi2_advanced,
7547     gen_zero_extendsidi2_advanced,
7548   };
7549   static gen_func_t gen_ld_s[] = {
7550     gen_movbi_speculative,
7551     gen_movqi_speculative,
7552     gen_movhi_speculative,
7553     gen_movsi_speculative,
7554     gen_movdi_speculative,
7555     gen_movsf_speculative,
7556     gen_movdf_speculative,
7557     gen_movxf_speculative,
7558     gen_movti_speculative,
7559     gen_zero_extendqidi2_speculative,
7560     gen_zero_extendhidi2_speculative,
7561     gen_zero_extendsidi2_speculative,
7562   };
7563   static gen_func_t gen_ld_sa[] = {
7564     gen_movbi_speculative_advanced,
7565     gen_movqi_speculative_advanced,
7566     gen_movhi_speculative_advanced,
7567     gen_movsi_speculative_advanced,
7568     gen_movdi_speculative_advanced,
7569     gen_movsf_speculative_advanced,
7570     gen_movdf_speculative_advanced,
7571     gen_movxf_speculative_advanced,
7572     gen_movti_speculative_advanced,
7573     gen_zero_extendqidi2_speculative_advanced,
7574     gen_zero_extendhidi2_speculative_advanced,
7575     gen_zero_extendsidi2_speculative_advanced,
7576   };
7577   static gen_func_t gen_ld_s_a[] = {
7578     gen_movbi_speculative_a,
7579     gen_movqi_speculative_a,
7580     gen_movhi_speculative_a,
7581     gen_movsi_speculative_a,
7582     gen_movdi_speculative_a,
7583     gen_movsf_speculative_a,
7584     gen_movdf_speculative_a,
7585     gen_movxf_speculative_a,
7586     gen_movti_speculative_a,
7587     gen_zero_extendqidi2_speculative_a,
7588     gen_zero_extendhidi2_speculative_a,
7589     gen_zero_extendsidi2_speculative_a,
7590   };
7591
7592   gen_func_t *gen_ld;
7593
7594   if (ts & BEGIN_DATA)
7595     {
7596       if (ts & BEGIN_CONTROL)
7597         gen_ld = gen_ld_sa;
7598       else
7599         gen_ld = gen_ld_a;
7600     }
7601   else if (ts & BEGIN_CONTROL)
7602     {
7603       if ((spec_info->flags & SEL_SCHED_SPEC_DONT_CHECK_CONTROL)
7604           || ia64_needs_block_p (ts))
7605         gen_ld = gen_ld_s;
7606       else
7607         gen_ld = gen_ld_s_a;
7608     }
7609   else if (ts == 0)
7610     gen_ld = gen_ld_;
7611   else
7612     gcc_unreachable ();
7613
7614   return gen_ld[mode_no];
7615 }
7616
7617 /* Constants that help mapping 'enum machine_mode' to int.  */
7618 enum SPEC_MODES
7619   {
7620     SPEC_MODE_INVALID = -1,
7621     SPEC_MODE_FIRST = 0,
7622     SPEC_MODE_FOR_EXTEND_FIRST = 1,
7623     SPEC_MODE_FOR_EXTEND_LAST = 3,
7624     SPEC_MODE_LAST = 8
7625   };
7626
7627 enum
7628   {
7629     /* Offset to reach ZERO_EXTEND patterns.  */
7630     SPEC_GEN_EXTEND_OFFSET = SPEC_MODE_LAST - SPEC_MODE_FOR_EXTEND_FIRST + 1
7631   };
7632
7633 /* Return index of the MODE.  */
7634 static int
7635 ia64_mode_to_int (enum machine_mode mode)
7636 {
7637   switch (mode)
7638     {
7639     case BImode: return 0; /* SPEC_MODE_FIRST  */
7640     case QImode: return 1; /* SPEC_MODE_FOR_EXTEND_FIRST  */
7641     case HImode: return 2;
7642     case SImode: return 3; /* SPEC_MODE_FOR_EXTEND_LAST  */
7643     case DImode: return 4;
7644     case SFmode: return 5;
7645     case DFmode: return 6;
7646     case XFmode: return 7;
7647     case TImode:
7648       /* ??? This mode needs testing.  Bypasses for ldfp8 instruction are not
7649          mentioned in itanium[12].md.  Predicate fp_register_operand also
7650          needs to be defined.  Bottom line: better disable for now.  */
7651       return SPEC_MODE_INVALID;
7652     default:     return SPEC_MODE_INVALID;
7653     }
7654 }
7655
7656 /* Provide information about speculation capabilities.  */
7657 static void
7658 ia64_set_sched_flags (spec_info_t spec_info)
7659 {
7660   unsigned int *flags = &(current_sched_info->flags);
7661
7662   if (*flags & SCHED_RGN
7663       || *flags & SCHED_EBB
7664       || *flags & SEL_SCHED)
7665     {
7666       int mask = 0;
7667
7668       if ((mflag_sched_br_data_spec && !reload_completed && optimize > 0)
7669           || (mflag_sched_ar_data_spec && reload_completed))
7670         {
7671           mask |= BEGIN_DATA;
7672
7673           if (!sel_sched_p ()
7674               && ((mflag_sched_br_in_data_spec && !reload_completed)
7675                   || (mflag_sched_ar_in_data_spec && reload_completed)))
7676             mask |= BE_IN_DATA;
7677         }
7678       
7679       if (mflag_sched_control_spec
7680           && (!sel_sched_p ()
7681               || reload_completed))
7682         {
7683           mask |= BEGIN_CONTROL;
7684           
7685           if (!sel_sched_p () && mflag_sched_in_control_spec)
7686             mask |= BE_IN_CONTROL;
7687         }
7688
7689       spec_info->mask = mask;
7690
7691       if (mask)
7692         {
7693           *flags |= USE_DEPS_LIST | DO_SPECULATION;
7694
7695           if (mask & BE_IN_SPEC)
7696             *flags |= NEW_BBS;
7697           
7698           spec_info->flags = 0;
7699       
7700           if ((mask & DATA_SPEC) && mflag_sched_prefer_non_data_spec_insns)
7701             spec_info->flags |= PREFER_NON_DATA_SPEC;
7702
7703           if (mask & CONTROL_SPEC)
7704             {
7705               if (mflag_sched_prefer_non_control_spec_insns)
7706                 spec_info->flags |= PREFER_NON_CONTROL_SPEC;
7707
7708               if (sel_sched_p () && mflag_sel_sched_dont_check_control_spec)
7709                 spec_info->flags |= SEL_SCHED_SPEC_DONT_CHECK_CONTROL;
7710             }
7711
7712           if (sched_verbose >= 1)
7713             spec_info->dump = sched_dump;
7714           else
7715             spec_info->dump = 0;
7716           
7717           if (mflag_sched_count_spec_in_critical_path)
7718             spec_info->flags |= COUNT_SPEC_IN_CRITICAL_PATH;
7719         }
7720     }
7721   else
7722     spec_info->mask = 0;
7723 }
7724
7725 /* If INSN is an appropriate load return its mode.
7726    Return -1 otherwise.  */
7727 static int
7728 get_mode_no_for_insn (rtx insn)
7729 {
7730   rtx reg, mem, mode_rtx;
7731   int mode_no;
7732   bool extend_p;
7733
7734   extract_insn_cached (insn);
7735
7736   /* We use WHICH_ALTERNATIVE only after reload.  This will
7737      guarantee that reload won't touch a speculative insn.  */
7738
7739   if (recog_data.n_operands != 2)
7740     return -1;
7741
7742   reg = recog_data.operand[0];
7743   mem = recog_data.operand[1];
7744
7745   /* We should use MEM's mode since REG's mode in presence of
7746      ZERO_EXTEND will always be DImode.  */
7747   if (get_attr_speculable1 (insn) == SPECULABLE1_YES)
7748     /* Process non-speculative ld.  */
7749     {
7750       if (!reload_completed)
7751         {
7752           /* Do not speculate into regs like ar.lc.  */
7753           if (!REG_P (reg) || AR_REGNO_P (REGNO (reg)))
7754             return -1;
7755
7756           if (!MEM_P (mem))
7757             return -1;
7758
7759           {
7760             rtx mem_reg = XEXP (mem, 0);
7761
7762             if (!REG_P (mem_reg))
7763               return -1;
7764           }
7765
7766           mode_rtx = mem;
7767         }
7768       else if (get_attr_speculable2 (insn) == SPECULABLE2_YES)
7769         {
7770           gcc_assert (REG_P (reg) && MEM_P (mem));
7771           mode_rtx = mem;
7772         }
7773       else
7774         return -1;
7775     }
7776   else if (get_attr_data_speculative (insn) == DATA_SPECULATIVE_YES
7777            || get_attr_control_speculative (insn) == CONTROL_SPECULATIVE_YES
7778            || get_attr_check_load (insn) == CHECK_LOAD_YES)
7779     /* Process speculative ld or ld.c.  */
7780     {
7781       gcc_assert (REG_P (reg) && MEM_P (mem));
7782       mode_rtx = mem;
7783     }
7784   else
7785     {
7786       enum attr_itanium_class attr_class = get_attr_itanium_class (insn);
7787
7788       if (attr_class == ITANIUM_CLASS_CHK_A
7789           || attr_class == ITANIUM_CLASS_CHK_S_I
7790           || attr_class == ITANIUM_CLASS_CHK_S_F)
7791         /* Process chk.  */
7792         mode_rtx = reg;
7793       else
7794         return -1;
7795     }
7796
7797   mode_no = ia64_mode_to_int (GET_MODE (mode_rtx));
7798
7799   if (mode_no == SPEC_MODE_INVALID)
7800     return -1;
7801
7802   extend_p = (GET_MODE (reg) != GET_MODE (mode_rtx));
7803
7804   if (extend_p)
7805     {
7806       if (!(SPEC_MODE_FOR_EXTEND_FIRST <= mode_no
7807             && mode_no <= SPEC_MODE_FOR_EXTEND_LAST))
7808         return -1;
7809
7810       mode_no += SPEC_GEN_EXTEND_OFFSET;
7811     }
7812
7813   return mode_no;
7814 }
7815
7816 /* If X is an unspec part of a speculative load, return its code.
7817    Return -1 otherwise.  */
7818 static int
7819 get_spec_unspec_code (const_rtx x)
7820 {
7821   if (GET_CODE (x) != UNSPEC)
7822     return -1;
7823
7824   {
7825     int code;
7826
7827     code = XINT (x, 1);
7828
7829     switch (code)
7830       {
7831       case UNSPEC_LDA:
7832       case UNSPEC_LDS:
7833       case UNSPEC_LDS_A:
7834       case UNSPEC_LDSA:
7835         return code;
7836
7837       default:
7838         return -1;
7839       }
7840   }
7841 }
7842
7843 /* Implement skip_rtx_p hook.  */
7844 static bool
7845 ia64_skip_rtx_p (const_rtx x)
7846 {
7847   return get_spec_unspec_code (x) != -1;
7848 }
7849
7850 /* If INSN is a speculative load, return its UNSPEC code.
7851    Return -1 otherwise.  */
7852 static int
7853 get_insn_spec_code (const_rtx insn)
7854 {
7855   rtx pat, reg, mem;
7856
7857   pat = PATTERN (insn);
7858
7859   if (GET_CODE (pat) == COND_EXEC)
7860     pat = COND_EXEC_CODE (pat);
7861
7862   if (GET_CODE (pat) != SET)
7863     return -1;
7864
7865   reg = SET_DEST (pat);
7866   if (!REG_P (reg))
7867     return -1;
7868
7869   mem = SET_SRC (pat);
7870   if (GET_CODE (mem) == ZERO_EXTEND)
7871     mem = XEXP (mem, 0);
7872
7873   return get_spec_unspec_code (mem);
7874 }
7875
7876 /* If INSN is a speculative load, return a ds with the speculation types.
7877    Otherwise [if INSN is a normal instruction] return 0.  */
7878 static ds_t
7879 ia64_get_insn_spec_ds (rtx insn)
7880 {
7881   int code = get_insn_spec_code (insn);
7882
7883   switch (code)
7884     {
7885     case UNSPEC_LDA:
7886       return BEGIN_DATA;
7887
7888     case UNSPEC_LDS:
7889     case UNSPEC_LDS_A:
7890       return BEGIN_CONTROL;
7891
7892     case UNSPEC_LDSA:
7893       return BEGIN_DATA | BEGIN_CONTROL;
7894
7895     default:
7896       return 0;
7897     }
7898 }
7899
7900 /* If INSN is a speculative load return a ds with the speculation types that
7901    will be checked.
7902    Otherwise [if INSN is a normal instruction] return 0.  */
7903 static ds_t
7904 ia64_get_insn_checked_ds (rtx insn)
7905 {
7906   int code = get_insn_spec_code (insn);
7907
7908   switch (code)
7909     {
7910     case UNSPEC_LDA:
7911       return BEGIN_DATA | BEGIN_CONTROL;
7912
7913     case UNSPEC_LDS:
7914       return BEGIN_CONTROL;
7915
7916     case UNSPEC_LDS_A:
7917     case UNSPEC_LDSA:
7918       return BEGIN_DATA | BEGIN_CONTROL;
7919
7920     default:
7921       return 0;
7922     }
7923 }
7924
7925 /* If GEN_P is true, calculate the index of needed speculation check and return
7926    speculative pattern for INSN with speculative mode TS, machine mode
7927    MODE_NO and with ZERO_EXTEND (if EXTEND_P is true).
7928    If GEN_P is false, just calculate the index of needed speculation check.  */
7929 static rtx
7930 ia64_gen_spec_load (rtx insn, ds_t ts, int mode_no)
7931 {
7932   rtx pat, new_pat;
7933   gen_func_t gen_load;
7934
7935   gen_load = get_spec_load_gen_function (ts, mode_no);
7936
7937   new_pat = gen_load (copy_rtx (recog_data.operand[0]),
7938                       copy_rtx (recog_data.operand[1]));
7939
7940   pat = PATTERN (insn);
7941   if (GET_CODE (pat) == COND_EXEC)
7942     new_pat = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (COND_EXEC_TEST (pat)),
7943                                  new_pat);
7944
7945   return new_pat;
7946 }
7947
7948 static bool
7949 insn_can_be_in_speculative_p (rtx insn ATTRIBUTE_UNUSED,
7950                               ds_t ds ATTRIBUTE_UNUSED)
7951 {
7952   return false;
7953 }
7954
7955 /* Implement targetm.sched.speculate_insn hook.
7956    Check if the INSN can be TS speculative.
7957    If 'no' - return -1.
7958    If 'yes' - generate speculative pattern in the NEW_PAT and return 1.
7959    If current pattern of the INSN already provides TS speculation,
7960    return 0.  */
7961 static int
7962 ia64_speculate_insn (rtx insn, ds_t ts, rtx *new_pat)
7963 {  
7964   int mode_no;
7965   int res;
7966   
7967   gcc_assert (!(ts & ~SPECULATIVE));
7968
7969   if (ia64_spec_check_p (insn))
7970     return -1;
7971
7972   if ((ts & BE_IN_SPEC)
7973       && !insn_can_be_in_speculative_p (insn, ts))
7974     return -1;
7975
7976   mode_no = get_mode_no_for_insn (insn);
7977
7978   if (mode_no != SPEC_MODE_INVALID)
7979     {
7980       if (ia64_get_insn_spec_ds (insn) == ds_get_speculation_types (ts))
7981         res = 0;
7982       else
7983         {
7984           res = 1;
7985           *new_pat = ia64_gen_spec_load (insn, ts, mode_no);
7986         }
7987     }
7988   else
7989     res = -1;
7990
7991   return res;
7992 }
7993
7994 /* Return a function that will generate a check for speculation TS with mode
7995    MODE_NO.
7996    If simple check is needed, pass true for SIMPLE_CHECK_P.
7997    If clearing check is needed, pass true for CLEARING_CHECK_P.  */
7998 static gen_func_t
7999 get_spec_check_gen_function (ds_t ts, int mode_no,
8000                              bool simple_check_p, bool clearing_check_p)
8001 {
8002   static gen_func_t gen_ld_c_clr[] = {
8003     gen_movbi_clr,
8004     gen_movqi_clr,
8005     gen_movhi_clr,
8006     gen_movsi_clr,
8007     gen_movdi_clr,
8008     gen_movsf_clr,
8009     gen_movdf_clr,
8010     gen_movxf_clr,
8011     gen_movti_clr,
8012     gen_zero_extendqidi2_clr,
8013     gen_zero_extendhidi2_clr,
8014     gen_zero_extendsidi2_clr,
8015   };
8016   static gen_func_t gen_ld_c_nc[] = {
8017     gen_movbi_nc,
8018     gen_movqi_nc,
8019     gen_movhi_nc,
8020     gen_movsi_nc,
8021     gen_movdi_nc,
8022     gen_movsf_nc,
8023     gen_movdf_nc,
8024     gen_movxf_nc,
8025     gen_movti_nc,
8026     gen_zero_extendqidi2_nc,
8027     gen_zero_extendhidi2_nc,
8028     gen_zero_extendsidi2_nc,
8029   };
8030   static gen_func_t gen_chk_a_clr[] = {
8031     gen_advanced_load_check_clr_bi,
8032     gen_advanced_load_check_clr_qi,
8033     gen_advanced_load_check_clr_hi,
8034     gen_advanced_load_check_clr_si,
8035     gen_advanced_load_check_clr_di,
8036     gen_advanced_load_check_clr_sf,
8037     gen_advanced_load_check_clr_df,
8038     gen_advanced_load_check_clr_xf,
8039     gen_advanced_load_check_clr_ti,
8040     gen_advanced_load_check_clr_di,
8041     gen_advanced_load_check_clr_di,
8042     gen_advanced_load_check_clr_di,
8043   };
8044   static gen_func_t gen_chk_a_nc[] = {
8045     gen_advanced_load_check_nc_bi,
8046     gen_advanced_load_check_nc_qi,
8047     gen_advanced_load_check_nc_hi,
8048     gen_advanced_load_check_nc_si,
8049     gen_advanced_load_check_nc_di,
8050     gen_advanced_load_check_nc_sf,
8051     gen_advanced_load_check_nc_df,
8052     gen_advanced_load_check_nc_xf,
8053     gen_advanced_load_check_nc_ti,
8054     gen_advanced_load_check_nc_di,
8055     gen_advanced_load_check_nc_di,
8056     gen_advanced_load_check_nc_di,
8057   };
8058   static gen_func_t gen_chk_s[] = {
8059     gen_speculation_check_bi,
8060     gen_speculation_check_qi,
8061     gen_speculation_check_hi,
8062     gen_speculation_check_si,
8063     gen_speculation_check_di,
8064     gen_speculation_check_sf,
8065     gen_speculation_check_df,
8066     gen_speculation_check_xf,
8067     gen_speculation_check_ti,
8068     gen_speculation_check_di,
8069     gen_speculation_check_di,
8070     gen_speculation_check_di,
8071   };
8072
8073   gen_func_t *gen_check;
8074
8075   if (ts & BEGIN_DATA)
8076     {
8077       /* We don't need recovery because even if this is ld.sa
8078          ALAT entry will be allocated only if NAT bit is set to zero.
8079          So it is enough to use ld.c here.  */
8080
8081       if (simple_check_p)
8082         {
8083           gcc_assert (mflag_sched_spec_ldc);
8084
8085           if (clearing_check_p)
8086             gen_check = gen_ld_c_clr;
8087           else
8088             gen_check = gen_ld_c_nc;
8089         }
8090       else
8091         {
8092           if (clearing_check_p)
8093             gen_check = gen_chk_a_clr;
8094           else
8095             gen_check = gen_chk_a_nc;
8096         }
8097     }
8098   else if (ts & BEGIN_CONTROL)
8099     {
8100       if (simple_check_p)
8101         /* We might want to use ld.sa -> ld.c instead of
8102            ld.s -> chk.s.  */
8103         {
8104           gcc_assert (!ia64_needs_block_p (ts));
8105
8106           if (clearing_check_p)
8107             gen_check = gen_ld_c_clr;
8108           else
8109             gen_check = gen_ld_c_nc;
8110         }
8111       else
8112         {
8113           gen_check = gen_chk_s;
8114         }
8115     }
8116   else
8117     gcc_unreachable ();
8118
8119   gcc_assert (mode_no >= 0);
8120   return gen_check[mode_no];
8121 }
8122
8123 /* Return nonzero, if INSN needs branchy recovery check.  */
8124 static bool
8125 ia64_needs_block_p (ds_t ts)
8126 {
8127   if (ts & BEGIN_DATA)
8128     return !mflag_sched_spec_ldc;
8129
8130   gcc_assert ((ts & BEGIN_CONTROL) != 0);
8131
8132   return !(mflag_sched_spec_control_ldc && mflag_sched_spec_ldc);
8133 }
8134
8135 /* Generate (or regenerate, if (MUTATE_P)) recovery check for INSN.
8136    If (LABEL != 0 || MUTATE_P), generate branchy recovery check.
8137    Otherwise, generate a simple check.  */
8138 static rtx
8139 ia64_gen_spec_check (rtx insn, rtx label, ds_t ds)
8140 {
8141   rtx op1, pat, check_pat;
8142   gen_func_t gen_check;
8143   int mode_no;
8144
8145   mode_no = get_mode_no_for_insn (insn);
8146   gcc_assert (mode_no >= 0);
8147
8148   if (label)
8149     op1 = label;
8150   else
8151     {
8152       gcc_assert (!ia64_needs_block_p (ds));
8153       op1 = copy_rtx (recog_data.operand[1]);
8154     }
8155       
8156   gen_check = get_spec_check_gen_function (ds, mode_no, label == NULL_RTX,
8157                                            true);
8158
8159   check_pat = gen_check (copy_rtx (recog_data.operand[0]), op1);
8160     
8161   pat = PATTERN (insn);
8162   if (GET_CODE (pat) == COND_EXEC)
8163     check_pat = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (COND_EXEC_TEST (pat)),
8164                                    check_pat);
8165
8166   return check_pat;
8167 }
8168
8169 /* Return nonzero, if X is branchy recovery check.  */
8170 static int
8171 ia64_spec_check_p (rtx x)
8172 {
8173   x = PATTERN (x);
8174   if (GET_CODE (x) == COND_EXEC)
8175     x = COND_EXEC_CODE (x);
8176   if (GET_CODE (x) == SET)
8177     return ia64_spec_check_src_p (SET_SRC (x));
8178   return 0;
8179 }
8180
8181 /* Return nonzero, if SRC belongs to recovery check.  */
8182 static int
8183 ia64_spec_check_src_p (rtx src)
8184 {
8185   if (GET_CODE (src) == IF_THEN_ELSE)
8186     {
8187       rtx t;
8188
8189       t = XEXP (src, 0);
8190       if (GET_CODE (t) == NE)
8191         {
8192           t = XEXP (t, 0);          
8193
8194           if (GET_CODE (t) == UNSPEC)
8195             {
8196               int code;
8197               
8198               code = XINT (t, 1);
8199              
8200               if (code == UNSPEC_LDCCLR
8201                   || code == UNSPEC_LDCNC
8202                   || code == UNSPEC_CHKACLR
8203                   || code == UNSPEC_CHKANC
8204                   || code == UNSPEC_CHKS)
8205                 {
8206                   gcc_assert (code != 0);
8207                   return code;
8208                 }
8209             }
8210         }
8211     }
8212   return 0;
8213 }
8214 \f
8215
8216 /* The following page contains abstract data `bundle states' which are
8217    used for bundling insns (inserting nops and template generation).  */
8218
8219 /* The following describes state of insn bundling.  */
8220
8221 struct bundle_state
8222 {
8223   /* Unique bundle state number to identify them in the debugging
8224      output  */
8225   int unique_num;
8226   rtx insn;     /* corresponding insn, NULL for the 1st and the last state  */
8227   /* number nops before and after the insn  */
8228   short before_nops_num, after_nops_num;
8229   int insn_num; /* insn number (0 - for initial state, 1 - for the 1st
8230                    insn */
8231   int cost;     /* cost of the state in cycles */
8232   int accumulated_insns_num; /* number of all previous insns including
8233                                 nops.  L is considered as 2 insns */
8234   int branch_deviation; /* deviation of previous branches from 3rd slots  */
8235   int middle_bundle_stops; /* number of stop bits in the middle of bundles */
8236   struct bundle_state *next;  /* next state with the same insn_num  */
8237   struct bundle_state *originator; /* originator (previous insn state)  */
8238   /* All bundle states are in the following chain.  */
8239   struct bundle_state *allocated_states_chain;
8240   /* The DFA State after issuing the insn and the nops.  */
8241   state_t dfa_state;
8242 };
8243
8244 /* The following is map insn number to the corresponding bundle state.  */
8245
8246 static struct bundle_state **index_to_bundle_states;
8247
8248 /* The unique number of next bundle state.  */
8249
8250 static int bundle_states_num;
8251
8252 /* All allocated bundle states are in the following chain.  */
8253
8254 static struct bundle_state *allocated_bundle_states_chain;
8255
8256 /* All allocated but not used bundle states are in the following
8257    chain.  */
8258
8259 static struct bundle_state *free_bundle_state_chain;
8260
8261
8262 /* The following function returns a free bundle state.  */
8263
8264 static struct bundle_state *
8265 get_free_bundle_state (void)
8266 {
8267   struct bundle_state *result;
8268
8269   if (free_bundle_state_chain != NULL)
8270     {
8271       result = free_bundle_state_chain;
8272       free_bundle_state_chain = result->next;
8273     }
8274   else
8275     {
8276       result = XNEW (struct bundle_state);
8277       result->dfa_state = xmalloc (dfa_state_size);
8278       result->allocated_states_chain = allocated_bundle_states_chain;
8279       allocated_bundle_states_chain = result;
8280     }
8281   result->unique_num = bundle_states_num++;
8282   return result;
8283
8284 }
8285
8286 /* The following function frees given bundle state.  */
8287
8288 static void
8289 free_bundle_state (struct bundle_state *state)
8290 {
8291   state->next = free_bundle_state_chain;
8292   free_bundle_state_chain = state;
8293 }
8294
8295 /* Start work with abstract data `bundle states'.  */
8296
8297 static void
8298 initiate_bundle_states (void)
8299 {
8300   bundle_states_num = 0;
8301   free_bundle_state_chain = NULL;
8302   allocated_bundle_states_chain = NULL;
8303 }
8304
8305 /* Finish work with abstract data `bundle states'.  */
8306
8307 static void
8308 finish_bundle_states (void)
8309 {
8310   struct bundle_state *curr_state, *next_state;
8311
8312   for (curr_state = allocated_bundle_states_chain;
8313        curr_state != NULL;
8314        curr_state = next_state)
8315     {
8316       next_state = curr_state->allocated_states_chain;
8317       free (curr_state->dfa_state);
8318       free (curr_state);
8319     }
8320 }
8321
8322 /* Hash table of the bundle states.  The key is dfa_state and insn_num
8323    of the bundle states.  */
8324
8325 static htab_t bundle_state_table;
8326
8327 /* The function returns hash of BUNDLE_STATE.  */
8328
8329 static unsigned
8330 bundle_state_hash (const void *bundle_state)
8331 {
8332   const struct bundle_state *const state
8333     = (const struct bundle_state *) bundle_state;
8334   unsigned result, i;
8335
8336   for (result = i = 0; i < dfa_state_size; i++)
8337     result += (((unsigned char *) state->dfa_state) [i]
8338                << ((i % CHAR_BIT) * 3 + CHAR_BIT));
8339   return result + state->insn_num;
8340 }
8341
8342 /* The function returns nonzero if the bundle state keys are equal.  */
8343
8344 static int
8345 bundle_state_eq_p (const void *bundle_state_1, const void *bundle_state_2)
8346 {
8347   const struct bundle_state *const state1
8348     = (const struct bundle_state *) bundle_state_1;
8349   const struct bundle_state *const state2
8350     = (const struct bundle_state *) bundle_state_2;
8351
8352   return (state1->insn_num == state2->insn_num
8353           && memcmp (state1->dfa_state, state2->dfa_state,
8354                      dfa_state_size) == 0);
8355 }
8356
8357 /* The function inserts the BUNDLE_STATE into the hash table.  The
8358    function returns nonzero if the bundle has been inserted into the
8359    table.  The table contains the best bundle state with given key.  */
8360
8361 static int
8362 insert_bundle_state (struct bundle_state *bundle_state)
8363 {
8364   void **entry_ptr;
8365
8366   entry_ptr = htab_find_slot (bundle_state_table, bundle_state, INSERT);
8367   if (*entry_ptr == NULL)
8368     {
8369       bundle_state->next = index_to_bundle_states [bundle_state->insn_num];
8370       index_to_bundle_states [bundle_state->insn_num] = bundle_state;
8371       *entry_ptr = (void *) bundle_state;
8372       return TRUE;
8373     }
8374   else if (bundle_state->cost < ((struct bundle_state *) *entry_ptr)->cost
8375            || (bundle_state->cost == ((struct bundle_state *) *entry_ptr)->cost
8376                && (((struct bundle_state *)*entry_ptr)->accumulated_insns_num
8377                    > bundle_state->accumulated_insns_num
8378                    || (((struct bundle_state *)
8379                         *entry_ptr)->accumulated_insns_num
8380                        == bundle_state->accumulated_insns_num
8381                        && (((struct bundle_state *)
8382                             *entry_ptr)->branch_deviation
8383                            > bundle_state->branch_deviation
8384                            || (((struct bundle_state *)
8385                                 *entry_ptr)->branch_deviation
8386                                == bundle_state->branch_deviation
8387                                && ((struct bundle_state *)
8388                                    *entry_ptr)->middle_bundle_stops
8389                                > bundle_state->middle_bundle_stops))))))
8390
8391     {
8392       struct bundle_state temp;
8393
8394       temp = *(struct bundle_state *) *entry_ptr;
8395       *(struct bundle_state *) *entry_ptr = *bundle_state;
8396       ((struct bundle_state *) *entry_ptr)->next = temp.next;
8397       *bundle_state = temp;
8398     }
8399   return FALSE;
8400 }
8401
8402 /* Start work with the hash table.  */
8403
8404 static void
8405 initiate_bundle_state_table (void)
8406 {
8407   bundle_state_table = htab_create (50, bundle_state_hash, bundle_state_eq_p,
8408                                     (htab_del) 0);
8409 }
8410
8411 /* Finish work with the hash table.  */
8412
8413 static void
8414 finish_bundle_state_table (void)
8415 {
8416   htab_delete (bundle_state_table);
8417 }
8418
8419 \f
8420
8421 /* The following variable is a insn `nop' used to check bundle states
8422    with different number of inserted nops.  */
8423
8424 static rtx ia64_nop;
8425
8426 /* The following function tries to issue NOPS_NUM nops for the current
8427    state without advancing processor cycle.  If it failed, the
8428    function returns FALSE and frees the current state.  */
8429
8430 static int
8431 try_issue_nops (struct bundle_state *curr_state, int nops_num)
8432 {
8433   int i;
8434
8435   for (i = 0; i < nops_num; i++)
8436     if (state_transition (curr_state->dfa_state, ia64_nop) >= 0)
8437       {
8438         free_bundle_state (curr_state);
8439         return FALSE;
8440       }
8441   return TRUE;
8442 }
8443
8444 /* The following function tries to issue INSN for the current
8445    state without advancing processor cycle.  If it failed, the
8446    function returns FALSE and frees the current state.  */
8447
8448 static int
8449 try_issue_insn (struct bundle_state *curr_state, rtx insn)
8450 {
8451   if (insn && state_transition (curr_state->dfa_state, insn) >= 0)
8452     {
8453       free_bundle_state (curr_state);
8454       return FALSE;
8455     }
8456   return TRUE;
8457 }
8458
8459 /* The following function tries to issue BEFORE_NOPS_NUM nops and INSN
8460    starting with ORIGINATOR without advancing processor cycle.  If
8461    TRY_BUNDLE_END_P is TRUE, the function also/only (if
8462    ONLY_BUNDLE_END_P is TRUE) tries to issue nops to fill all bundle.
8463    If it was successful, the function creates new bundle state and
8464    insert into the hash table and into `index_to_bundle_states'.  */
8465
8466 static void
8467 issue_nops_and_insn (struct bundle_state *originator, int before_nops_num,
8468                      rtx insn, int try_bundle_end_p, int only_bundle_end_p)
8469 {
8470   struct bundle_state *curr_state;
8471
8472   curr_state = get_free_bundle_state ();
8473   memcpy (curr_state->dfa_state, originator->dfa_state, dfa_state_size);
8474   curr_state->insn = insn;
8475   curr_state->insn_num = originator->insn_num + 1;
8476   curr_state->cost = originator->cost;
8477   curr_state->originator = originator;
8478   curr_state->before_nops_num = before_nops_num;
8479   curr_state->after_nops_num = 0;
8480   curr_state->accumulated_insns_num
8481     = originator->accumulated_insns_num + before_nops_num;
8482   curr_state->branch_deviation = originator->branch_deviation;
8483   curr_state->middle_bundle_stops = originator->middle_bundle_stops;
8484   gcc_assert (insn);
8485   if (INSN_CODE (insn) == CODE_FOR_insn_group_barrier)
8486     {
8487       gcc_assert (GET_MODE (insn) != TImode);
8488       if (!try_issue_nops (curr_state, before_nops_num))
8489         return;
8490       if (!try_issue_insn (curr_state, insn))
8491         return;
8492       memcpy (temp_dfa_state, curr_state->dfa_state, dfa_state_size);
8493       if (curr_state->accumulated_insns_num % 3 != 0)
8494         curr_state->middle_bundle_stops++;
8495       if (state_transition (temp_dfa_state, dfa_pre_cycle_insn) >= 0
8496           && curr_state->accumulated_insns_num % 3 != 0)
8497         {
8498           free_bundle_state (curr_state);
8499           return;
8500         }
8501     }
8502   else if (GET_MODE (insn) != TImode)
8503     {
8504       if (!try_issue_nops (curr_state, before_nops_num))
8505         return;
8506       if (!try_issue_insn (curr_state, insn))
8507         return;
8508       curr_state->accumulated_insns_num++;
8509       gcc_assert (GET_CODE (PATTERN (insn)) != ASM_INPUT
8510                   && asm_noperands (PATTERN (insn)) < 0);
8511
8512       if (ia64_safe_type (insn) == TYPE_L)
8513         curr_state->accumulated_insns_num++;
8514     }
8515   else
8516     {
8517       /* If this is an insn that must be first in a group, then don't allow
8518          nops to be emitted before it.  Currently, alloc is the only such
8519          supported instruction.  */
8520       /* ??? The bundling automatons should handle this for us, but they do
8521          not yet have support for the first_insn attribute.  */
8522       if (before_nops_num > 0 && get_attr_first_insn (insn) == FIRST_INSN_YES)
8523         {
8524           free_bundle_state (curr_state);
8525           return;
8526         }
8527
8528       state_transition (curr_state->dfa_state, dfa_pre_cycle_insn);
8529       state_transition (curr_state->dfa_state, NULL);
8530       curr_state->cost++;
8531       if (!try_issue_nops (curr_state, before_nops_num))
8532         return;
8533       if (!try_issue_insn (curr_state, insn))
8534         return;
8535       curr_state->accumulated_insns_num++;
8536       if (GET_CODE (PATTERN (insn)) == ASM_INPUT
8537           || asm_noperands (PATTERN (insn)) >= 0)
8538         {
8539           /* Finish bundle containing asm insn.  */
8540           curr_state->after_nops_num
8541             = 3 - curr_state->accumulated_insns_num % 3;
8542           curr_state->accumulated_insns_num
8543             += 3 - curr_state->accumulated_insns_num % 3;
8544         }
8545       else if (ia64_safe_type (insn) == TYPE_L)
8546         curr_state->accumulated_insns_num++;
8547     }
8548   if (ia64_safe_type (insn) == TYPE_B)
8549     curr_state->branch_deviation
8550       += 2 - (curr_state->accumulated_insns_num - 1) % 3;
8551   if (try_bundle_end_p && curr_state->accumulated_insns_num % 3 != 0)
8552     {
8553       if (!only_bundle_end_p && insert_bundle_state (curr_state))
8554         {
8555           state_t dfa_state;
8556           struct bundle_state *curr_state1;
8557           struct bundle_state *allocated_states_chain;
8558
8559           curr_state1 = get_free_bundle_state ();
8560           dfa_state = curr_state1->dfa_state;
8561           allocated_states_chain = curr_state1->allocated_states_chain;
8562           *curr_state1 = *curr_state;
8563           curr_state1->dfa_state = dfa_state;
8564           curr_state1->allocated_states_chain = allocated_states_chain;
8565           memcpy (curr_state1->dfa_state, curr_state->dfa_state,
8566                   dfa_state_size);
8567           curr_state = curr_state1;
8568         }
8569       if (!try_issue_nops (curr_state,
8570                            3 - curr_state->accumulated_insns_num % 3))
8571         return;
8572       curr_state->after_nops_num
8573         = 3 - curr_state->accumulated_insns_num % 3;
8574       curr_state->accumulated_insns_num
8575         += 3 - curr_state->accumulated_insns_num % 3;
8576     }
8577   if (!insert_bundle_state (curr_state))
8578     free_bundle_state (curr_state);
8579   return;
8580 }
8581
8582 /* The following function returns position in the two window bundle
8583    for given STATE.  */
8584
8585 static int
8586 get_max_pos (state_t state)
8587 {
8588   if (cpu_unit_reservation_p (state, pos_6))
8589     return 6;
8590   else if (cpu_unit_reservation_p (state, pos_5))
8591     return 5;
8592   else if (cpu_unit_reservation_p (state, pos_4))
8593     return 4;
8594   else if (cpu_unit_reservation_p (state, pos_3))
8595     return 3;
8596   else if (cpu_unit_reservation_p (state, pos_2))
8597     return 2;
8598   else if (cpu_unit_reservation_p (state, pos_1))
8599     return 1;
8600   else
8601     return 0;
8602 }
8603
8604 /* The function returns code of a possible template for given position
8605    and state.  The function should be called only with 2 values of
8606    position equal to 3 or 6.  We avoid generating F NOPs by putting
8607    templates containing F insns at the end of the template search
8608    because undocumented anomaly in McKinley derived cores which can
8609    cause stalls if an F-unit insn (including a NOP) is issued within a
8610    six-cycle window after reading certain application registers (such
8611    as ar.bsp).  Furthermore, power-considerations also argue against
8612    the use of F-unit instructions unless they're really needed.  */
8613
8614 static int
8615 get_template (state_t state, int pos)
8616 {
8617   switch (pos)
8618     {
8619     case 3:
8620       if (cpu_unit_reservation_p (state, _0mmi_))
8621         return 1;
8622       else if (cpu_unit_reservation_p (state, _0mii_))
8623         return 0;
8624       else if (cpu_unit_reservation_p (state, _0mmb_))
8625         return 7;
8626       else if (cpu_unit_reservation_p (state, _0mib_))
8627         return 6;
8628       else if (cpu_unit_reservation_p (state, _0mbb_))
8629         return 5;
8630       else if (cpu_unit_reservation_p (state, _0bbb_))
8631         return 4;
8632       else if (cpu_unit_reservation_p (state, _0mmf_))
8633         return 3;
8634       else if (cpu_unit_reservation_p (state, _0mfi_))
8635         return 2;
8636       else if (cpu_unit_reservation_p (state, _0mfb_))
8637         return 8;
8638       else if (cpu_unit_reservation_p (state, _0mlx_))
8639         return 9;
8640       else
8641         gcc_unreachable ();
8642     case 6:
8643       if (cpu_unit_reservation_p (state, _1mmi_))
8644         return 1;
8645       else if (cpu_unit_reservation_p (state, _1mii_))
8646         return 0;
8647       else if (cpu_unit_reservation_p (state, _1mmb_))
8648         return 7;
8649       else if (cpu_unit_reservation_p (state, _1mib_))
8650         return 6;
8651       else if (cpu_unit_reservation_p (state, _1mbb_))
8652         return 5;
8653       else if (cpu_unit_reservation_p (state, _1bbb_))
8654         return 4;
8655       else if (_1mmf_ >= 0 && cpu_unit_reservation_p (state, _1mmf_))
8656         return 3;
8657       else if (cpu_unit_reservation_p (state, _1mfi_))
8658         return 2;
8659       else if (cpu_unit_reservation_p (state, _1mfb_))
8660         return 8;
8661       else if (cpu_unit_reservation_p (state, _1mlx_))
8662         return 9;
8663       else
8664         gcc_unreachable ();
8665     default:
8666       gcc_unreachable ();
8667     }
8668 }
8669
8670 /* True when INSN is important for bundling.  */
8671 static bool
8672 important_for_bundling_p (rtx insn)
8673 {
8674   return (INSN_P (insn)
8675           && ia64_safe_itanium_class (insn) != ITANIUM_CLASS_IGNORE
8676           && GET_CODE (PATTERN (insn)) != USE
8677           && GET_CODE (PATTERN (insn)) != CLOBBER);
8678 }
8679
8680 /* The following function returns an insn important for insn bundling
8681    followed by INSN and before TAIL.  */
8682
8683 static rtx
8684 get_next_important_insn (rtx insn, rtx tail)
8685 {
8686   for (; insn && insn != tail; insn = NEXT_INSN (insn))
8687     if (important_for_bundling_p (insn))
8688       return insn;
8689   return NULL_RTX;
8690 }
8691
8692 /* Add a bundle selector TEMPLATE0 before INSN.  */
8693
8694 static void
8695 ia64_add_bundle_selector_before (int template0, rtx insn)
8696 {
8697   rtx b = gen_bundle_selector (GEN_INT (template0));
8698
8699   ia64_emit_insn_before (b, insn);
8700 #if NR_BUNDLES == 10
8701   if ((template0 == 4 || template0 == 5)
8702       && ia64_except_unwind_info (&global_options) == UI_TARGET)
8703     {
8704       int i;
8705       rtx note = NULL_RTX;
8706
8707       /* In .mbb and .bbb bundles, check if CALL_INSN isn't in the
8708          first or second slot.  If it is and has REG_EH_NOTE set, copy it
8709          to following nops, as br.call sets rp to the address of following
8710          bundle and therefore an EH region end must be on a bundle
8711          boundary.  */
8712       insn = PREV_INSN (insn);
8713       for (i = 0; i < 3; i++)
8714         {
8715           do
8716             insn = next_active_insn (insn);
8717           while (GET_CODE (insn) == INSN
8718                  && get_attr_empty (insn) == EMPTY_YES);
8719           if (GET_CODE (insn) == CALL_INSN)
8720             note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
8721           else if (note)
8722             {
8723               int code;
8724
8725               gcc_assert ((code = recog_memoized (insn)) == CODE_FOR_nop
8726                           || code == CODE_FOR_nop_b);
8727               if (find_reg_note (insn, REG_EH_REGION, NULL_RTX))
8728                 note = NULL_RTX;
8729               else
8730                 add_reg_note (insn, REG_EH_REGION, XEXP (note, 0));
8731             }
8732         }
8733     }
8734 #endif
8735 }
8736
8737 /* The following function does insn bundling.  Bundling means
8738    inserting templates and nop insns to fit insn groups into permitted
8739    templates.  Instruction scheduling uses NDFA (non-deterministic
8740    finite automata) encoding informations about the templates and the
8741    inserted nops.  Nondeterminism of the automata permits follows
8742    all possible insn sequences very fast.
8743
8744    Unfortunately it is not possible to get information about inserting
8745    nop insns and used templates from the automata states.  The
8746    automata only says that we can issue an insn possibly inserting
8747    some nops before it and using some template.  Therefore insn
8748    bundling in this function is implemented by using DFA
8749    (deterministic finite automata).  We follow all possible insn
8750    sequences by inserting 0-2 nops (that is what the NDFA describe for
8751    insn scheduling) before/after each insn being bundled.  We know the
8752    start of simulated processor cycle from insn scheduling (insn
8753    starting a new cycle has TImode).
8754
8755    Simple implementation of insn bundling would create enormous
8756    number of possible insn sequences satisfying information about new
8757    cycle ticks taken from the insn scheduling.  To make the algorithm
8758    practical we use dynamic programming.  Each decision (about
8759    inserting nops and implicitly about previous decisions) is described
8760    by structure bundle_state (see above).  If we generate the same
8761    bundle state (key is automaton state after issuing the insns and
8762    nops for it), we reuse already generated one.  As consequence we
8763    reject some decisions which cannot improve the solution and
8764    reduce memory for the algorithm.
8765
8766    When we reach the end of EBB (extended basic block), we choose the
8767    best sequence and then, moving back in EBB, insert templates for
8768    the best alternative.  The templates are taken from querying
8769    automaton state for each insn in chosen bundle states.
8770
8771    So the algorithm makes two (forward and backward) passes through
8772    EBB.  */
8773
8774 static void
8775 bundling (FILE *dump, int verbose, rtx prev_head_insn, rtx tail)
8776 {
8777   struct bundle_state *curr_state, *next_state, *best_state;
8778   rtx insn, next_insn;
8779   int insn_num;
8780   int i, bundle_end_p, only_bundle_end_p, asm_p;
8781   int pos = 0, max_pos, template0, template1;
8782   rtx b;
8783   rtx nop;
8784   enum attr_type type;
8785
8786   insn_num = 0;
8787   /* Count insns in the EBB.  */
8788   for (insn = NEXT_INSN (prev_head_insn);
8789        insn && insn != tail;
8790        insn = NEXT_INSN (insn))
8791     if (INSN_P (insn))
8792       insn_num++;
8793   if (insn_num == 0)
8794     return;
8795   bundling_p = 1;
8796   dfa_clean_insn_cache ();
8797   initiate_bundle_state_table ();
8798   index_to_bundle_states = XNEWVEC (struct bundle_state *, insn_num + 2);
8799   /* First (forward) pass -- generation of bundle states.  */
8800   curr_state = get_free_bundle_state ();
8801   curr_state->insn = NULL;
8802   curr_state->before_nops_num = 0;
8803   curr_state->after_nops_num = 0;
8804   curr_state->insn_num = 0;
8805   curr_state->cost = 0;
8806   curr_state->accumulated_insns_num = 0;
8807   curr_state->branch_deviation = 0;
8808   curr_state->middle_bundle_stops = 0;
8809   curr_state->next = NULL;
8810   curr_state->originator = NULL;
8811   state_reset (curr_state->dfa_state);
8812   index_to_bundle_states [0] = curr_state;
8813   insn_num = 0;
8814   /* Shift cycle mark if it is put on insn which could be ignored.  */
8815   for (insn = NEXT_INSN (prev_head_insn);
8816        insn != tail;
8817        insn = NEXT_INSN (insn))
8818     if (INSN_P (insn)
8819         && (ia64_safe_itanium_class (insn) == ITANIUM_CLASS_IGNORE
8820             || GET_CODE (PATTERN (insn)) == USE
8821             || GET_CODE (PATTERN (insn)) == CLOBBER)
8822         && GET_MODE (insn) == TImode)
8823       {
8824         PUT_MODE (insn, VOIDmode);
8825         for (next_insn = NEXT_INSN (insn);
8826              next_insn != tail;
8827              next_insn = NEXT_INSN (next_insn))
8828           if (INSN_P (next_insn)
8829               && ia64_safe_itanium_class (next_insn) != ITANIUM_CLASS_IGNORE
8830               && GET_CODE (PATTERN (next_insn)) != USE
8831               && GET_CODE (PATTERN (next_insn)) != CLOBBER
8832               && INSN_CODE (next_insn) != CODE_FOR_insn_group_barrier)
8833             {
8834               PUT_MODE (next_insn, TImode);
8835               break;
8836             }
8837       }
8838   /* Forward pass: generation of bundle states.  */
8839   for (insn = get_next_important_insn (NEXT_INSN (prev_head_insn), tail);
8840        insn != NULL_RTX;
8841        insn = next_insn)
8842     {
8843       gcc_assert (INSN_P (insn)
8844                   && ia64_safe_itanium_class (insn) != ITANIUM_CLASS_IGNORE
8845                   && GET_CODE (PATTERN (insn)) != USE
8846                   && GET_CODE (PATTERN (insn)) != CLOBBER);
8847       type = ia64_safe_type (insn);
8848       next_insn = get_next_important_insn (NEXT_INSN (insn), tail);
8849       insn_num++;
8850       index_to_bundle_states [insn_num] = NULL;
8851       for (curr_state = index_to_bundle_states [insn_num - 1];
8852            curr_state != NULL;
8853            curr_state = next_state)
8854         {
8855           pos = curr_state->accumulated_insns_num % 3;
8856           next_state = curr_state->next;
8857           /* We must fill up the current bundle in order to start a
8858              subsequent asm insn in a new bundle.  Asm insn is always
8859              placed in a separate bundle.  */
8860           only_bundle_end_p
8861             = (next_insn != NULL_RTX
8862                && INSN_CODE (insn) == CODE_FOR_insn_group_barrier
8863                && ia64_safe_type (next_insn) == TYPE_UNKNOWN);
8864           /* We may fill up the current bundle if it is the cycle end
8865              without a group barrier.  */
8866           bundle_end_p
8867             = (only_bundle_end_p || next_insn == NULL_RTX
8868                || (GET_MODE (next_insn) == TImode
8869                    && INSN_CODE (insn) != CODE_FOR_insn_group_barrier));
8870           if (type == TYPE_F || type == TYPE_B || type == TYPE_L
8871               || type == TYPE_S)
8872             issue_nops_and_insn (curr_state, 2, insn, bundle_end_p,
8873                                  only_bundle_end_p);
8874           issue_nops_and_insn (curr_state, 1, insn, bundle_end_p,
8875                                only_bundle_end_p);
8876           issue_nops_and_insn (curr_state, 0, insn, bundle_end_p,
8877                                only_bundle_end_p);
8878         }
8879       gcc_assert (index_to_bundle_states [insn_num]);
8880       for (curr_state = index_to_bundle_states [insn_num];
8881            curr_state != NULL;
8882            curr_state = curr_state->next)
8883         if (verbose >= 2 && dump)
8884           {
8885             /* This structure is taken from generated code of the
8886                pipeline hazard recognizer (see file insn-attrtab.c).
8887                Please don't forget to change the structure if a new
8888                automaton is added to .md file.  */
8889             struct DFA_chip
8890             {
8891               unsigned short one_automaton_state;
8892               unsigned short oneb_automaton_state;
8893               unsigned short two_automaton_state;
8894               unsigned short twob_automaton_state;
8895             };
8896
8897             fprintf
8898               (dump,
8899                "//    Bundle state %d (orig %d, cost %d, nops %d/%d, insns %d, branch %d, mid.stops %d state %d) for %d\n",
8900                curr_state->unique_num,
8901                (curr_state->originator == NULL
8902                 ? -1 : curr_state->originator->unique_num),
8903                curr_state->cost,
8904                curr_state->before_nops_num, curr_state->after_nops_num,
8905                curr_state->accumulated_insns_num, curr_state->branch_deviation,
8906                curr_state->middle_bundle_stops,
8907                ((struct DFA_chip *) curr_state->dfa_state)->twob_automaton_state,
8908                INSN_UID (insn));
8909           }
8910     }
8911   
8912   /* We should find a solution because the 2nd insn scheduling has
8913      found one.  */
8914   gcc_assert (index_to_bundle_states [insn_num]);
8915   /* Find a state corresponding to the best insn sequence.  */
8916   best_state = NULL;
8917   for (curr_state = index_to_bundle_states [insn_num];
8918        curr_state != NULL;
8919        curr_state = curr_state->next)
8920     /* We are just looking at the states with fully filled up last
8921        bundle.  The first we prefer insn sequences with minimal cost
8922        then with minimal inserted nops and finally with branch insns
8923        placed in the 3rd slots.  */
8924     if (curr_state->accumulated_insns_num % 3 == 0
8925         && (best_state == NULL || best_state->cost > curr_state->cost
8926             || (best_state->cost == curr_state->cost
8927                 && (curr_state->accumulated_insns_num
8928                     < best_state->accumulated_insns_num
8929                     || (curr_state->accumulated_insns_num
8930                         == best_state->accumulated_insns_num
8931                         && (curr_state->branch_deviation
8932                             < best_state->branch_deviation
8933                             || (curr_state->branch_deviation
8934                                 == best_state->branch_deviation
8935                                 && curr_state->middle_bundle_stops
8936                                 < best_state->middle_bundle_stops)))))))
8937       best_state = curr_state;
8938   /* Second (backward) pass: adding nops and templates.  */
8939   gcc_assert (best_state);
8940   insn_num = best_state->before_nops_num;
8941   template0 = template1 = -1;
8942   for (curr_state = best_state;
8943        curr_state->originator != NULL;
8944        curr_state = curr_state->originator)
8945     {
8946       insn = curr_state->insn;
8947       asm_p = (GET_CODE (PATTERN (insn)) == ASM_INPUT
8948                || asm_noperands (PATTERN (insn)) >= 0);
8949       insn_num++;
8950       if (verbose >= 2 && dump)
8951         {
8952           struct DFA_chip
8953           {
8954             unsigned short one_automaton_state;
8955             unsigned short oneb_automaton_state;
8956             unsigned short two_automaton_state;
8957             unsigned short twob_automaton_state;
8958           };
8959
8960           fprintf
8961             (dump,
8962              "//    Best %d (orig %d, cost %d, nops %d/%d, insns %d, branch %d, mid.stops %d, state %d) for %d\n",
8963              curr_state->unique_num,
8964              (curr_state->originator == NULL
8965               ? -1 : curr_state->originator->unique_num),
8966              curr_state->cost,
8967              curr_state->before_nops_num, curr_state->after_nops_num,
8968              curr_state->accumulated_insns_num, curr_state->branch_deviation,
8969              curr_state->middle_bundle_stops,
8970              ((struct DFA_chip *) curr_state->dfa_state)->twob_automaton_state,
8971              INSN_UID (insn));
8972         }
8973       /* Find the position in the current bundle window.  The window can
8974          contain at most two bundles.  Two bundle window means that
8975          the processor will make two bundle rotation.  */
8976       max_pos = get_max_pos (curr_state->dfa_state);
8977       if (max_pos == 6
8978           /* The following (negative template number) means that the
8979              processor did one bundle rotation.  */
8980           || (max_pos == 3 && template0 < 0))
8981         {
8982           /* We are at the end of the window -- find template(s) for
8983              its bundle(s).  */
8984           pos = max_pos;
8985           if (max_pos == 3)
8986             template0 = get_template (curr_state->dfa_state, 3);
8987           else
8988             {
8989               template1 = get_template (curr_state->dfa_state, 3);
8990               template0 = get_template (curr_state->dfa_state, 6);
8991             }
8992         }
8993       if (max_pos > 3 && template1 < 0)
8994         /* It may happen when we have the stop inside a bundle.  */
8995         {
8996           gcc_assert (pos <= 3);
8997           template1 = get_template (curr_state->dfa_state, 3);
8998           pos += 3;
8999         }
9000       if (!asm_p)
9001         /* Emit nops after the current insn.  */
9002         for (i = 0; i < curr_state->after_nops_num; i++)
9003           {
9004             nop = gen_nop ();
9005             emit_insn_after (nop, insn);
9006             pos--;
9007             gcc_assert (pos >= 0);
9008             if (pos % 3 == 0)
9009               {
9010                 /* We are at the start of a bundle: emit the template
9011                    (it should be defined).  */
9012                 gcc_assert (template0 >= 0);
9013                 ia64_add_bundle_selector_before (template0, nop);
9014                 /* If we have two bundle window, we make one bundle
9015                    rotation.  Otherwise template0 will be undefined
9016                    (negative value).  */
9017                 template0 = template1;
9018                 template1 = -1;
9019               }
9020           }
9021       /* Move the position backward in the window.  Group barrier has
9022          no slot.  Asm insn takes all bundle.  */
9023       if (INSN_CODE (insn) != CODE_FOR_insn_group_barrier
9024           && GET_CODE (PATTERN (insn)) != ASM_INPUT
9025           && asm_noperands (PATTERN (insn)) < 0)
9026         pos--;
9027       /* Long insn takes 2 slots.  */
9028       if (ia64_safe_type (insn) == TYPE_L)
9029         pos--;
9030       gcc_assert (pos >= 0);
9031       if (pos % 3 == 0
9032           && INSN_CODE (insn) != CODE_FOR_insn_group_barrier
9033           && GET_CODE (PATTERN (insn)) != ASM_INPUT
9034           && asm_noperands (PATTERN (insn)) < 0)
9035         {
9036           /* The current insn is at the bundle start: emit the
9037              template.  */
9038           gcc_assert (template0 >= 0);
9039           ia64_add_bundle_selector_before (template0, insn);
9040           b = PREV_INSN (insn);
9041           insn = b;
9042           /* See comment above in analogous place for emitting nops
9043              after the insn.  */
9044           template0 = template1;
9045           template1 = -1;
9046         }
9047       /* Emit nops after the current insn.  */
9048       for (i = 0; i < curr_state->before_nops_num; i++)
9049         {
9050           nop = gen_nop ();
9051           ia64_emit_insn_before (nop, insn);
9052           nop = PREV_INSN (insn);
9053           insn = nop;
9054           pos--;
9055           gcc_assert (pos >= 0);
9056           if (pos % 3 == 0)
9057             {
9058               /* See comment above in analogous place for emitting nops
9059                  after the insn.  */
9060               gcc_assert (template0 >= 0);
9061               ia64_add_bundle_selector_before (template0, insn);
9062               b = PREV_INSN (insn);
9063               insn = b;
9064               template0 = template1;
9065               template1 = -1;
9066             }
9067         }
9068     }
9069
9070 #ifdef ENABLE_CHECKING
9071   {
9072     /* Assert right calculation of middle_bundle_stops.  */
9073     int num = best_state->middle_bundle_stops;
9074     bool start_bundle = true, end_bundle = false;
9075
9076     for (insn = NEXT_INSN (prev_head_insn);
9077          insn && insn != tail;
9078          insn = NEXT_INSN (insn))
9079       {
9080         if (!INSN_P (insn))
9081           continue;
9082         if (recog_memoized (insn) == CODE_FOR_bundle_selector)
9083           start_bundle = true;
9084         else
9085           {
9086             rtx next_insn;
9087
9088             for (next_insn = NEXT_INSN (insn);
9089                  next_insn && next_insn != tail;
9090                  next_insn = NEXT_INSN (next_insn))
9091               if (INSN_P (next_insn)
9092                   && (ia64_safe_itanium_class (next_insn)
9093                       != ITANIUM_CLASS_IGNORE
9094                       || recog_memoized (next_insn)
9095                       == CODE_FOR_bundle_selector)
9096                   && GET_CODE (PATTERN (next_insn)) != USE
9097                   && GET_CODE (PATTERN (next_insn)) != CLOBBER)
9098                 break;
9099
9100             end_bundle = next_insn == NULL_RTX
9101              || next_insn == tail
9102              || (INSN_P (next_insn)
9103                  && recog_memoized (next_insn)
9104                  == CODE_FOR_bundle_selector);
9105             if (recog_memoized (insn) == CODE_FOR_insn_group_barrier
9106                 && !start_bundle && !end_bundle
9107                 && next_insn
9108                 && GET_CODE (PATTERN (next_insn)) != ASM_INPUT
9109                 && asm_noperands (PATTERN (next_insn)) < 0)
9110               num--;
9111
9112             start_bundle = false;
9113           }
9114       }
9115
9116     gcc_assert (num == 0);
9117   }
9118 #endif
9119
9120   free (index_to_bundle_states);
9121   finish_bundle_state_table ();
9122   bundling_p = 0;
9123   dfa_clean_insn_cache ();
9124 }
9125
9126 /* The following function is called at the end of scheduling BB or
9127    EBB.  After reload, it inserts stop bits and does insn bundling.  */
9128
9129 static void
9130 ia64_sched_finish (FILE *dump, int sched_verbose)
9131 {
9132   if (sched_verbose)
9133     fprintf (dump, "// Finishing schedule.\n");
9134   if (!reload_completed)
9135     return;
9136   if (reload_completed)
9137     {
9138       final_emit_insn_group_barriers (dump);
9139       bundling (dump, sched_verbose, current_sched_info->prev_head,
9140                 current_sched_info->next_tail);
9141       if (sched_verbose && dump)
9142         fprintf (dump, "//    finishing %d-%d\n",
9143                  INSN_UID (NEXT_INSN (current_sched_info->prev_head)),
9144                  INSN_UID (PREV_INSN (current_sched_info->next_tail)));
9145
9146       return;
9147     }
9148 }
9149
9150 /* The following function inserts stop bits in scheduled BB or EBB.  */
9151
9152 static void
9153 final_emit_insn_group_barriers (FILE *dump ATTRIBUTE_UNUSED)
9154 {
9155   rtx insn;
9156   int need_barrier_p = 0;
9157   int seen_good_insn = 0;
9158
9159   init_insn_group_barriers ();
9160
9161   for (insn = NEXT_INSN (current_sched_info->prev_head);
9162        insn != current_sched_info->next_tail;
9163        insn = NEXT_INSN (insn))
9164     {
9165       if (GET_CODE (insn) == BARRIER)
9166         {
9167           rtx last = prev_active_insn (insn);
9168
9169           if (! last)
9170             continue;
9171           if (GET_CODE (last) == JUMP_INSN
9172               && GET_CODE (PATTERN (last)) == ADDR_DIFF_VEC)
9173             last = prev_active_insn (last);
9174           if (recog_memoized (last) != CODE_FOR_insn_group_barrier)
9175             emit_insn_after (gen_insn_group_barrier (GEN_INT (3)), last);
9176
9177           init_insn_group_barriers ();
9178           seen_good_insn = 0;
9179           need_barrier_p = 0;
9180         }
9181       else if (NONDEBUG_INSN_P (insn))
9182         {
9183           if (recog_memoized (insn) == CODE_FOR_insn_group_barrier)
9184             {
9185               init_insn_group_barriers ();
9186               seen_good_insn = 0;
9187               need_barrier_p = 0;
9188             }
9189           else if (need_barrier_p || group_barrier_needed (insn)
9190                    || (mflag_sched_stop_bits_after_every_cycle
9191                        && GET_MODE (insn) == TImode
9192                        && seen_good_insn))
9193             {
9194               if (TARGET_EARLY_STOP_BITS)
9195                 {
9196                   rtx last;
9197
9198                   for (last = insn;
9199                        last != current_sched_info->prev_head;
9200                        last = PREV_INSN (last))
9201                     if (INSN_P (last) && GET_MODE (last) == TImode
9202                         && stops_p [INSN_UID (last)])
9203                       break;
9204                   if (last == current_sched_info->prev_head)
9205                     last = insn;
9206                   last = prev_active_insn (last);
9207                   if (last
9208                       && recog_memoized (last) != CODE_FOR_insn_group_barrier)
9209                     emit_insn_after (gen_insn_group_barrier (GEN_INT (3)),
9210                                      last);
9211                   init_insn_group_barriers ();
9212                   for (last = NEXT_INSN (last);
9213                        last != insn;
9214                        last = NEXT_INSN (last))
9215                     if (INSN_P (last))
9216                       {
9217                         group_barrier_needed (last);
9218                         if (recog_memoized (last) >= 0
9219                             && important_for_bundling_p (last))
9220                           seen_good_insn = 1;
9221                       }
9222                 }
9223               else
9224                 {
9225                   emit_insn_before (gen_insn_group_barrier (GEN_INT (3)),
9226                                     insn);
9227                   init_insn_group_barriers ();
9228                   seen_good_insn = 0;
9229                 }
9230               group_barrier_needed (insn);
9231               if (recog_memoized (insn) >= 0
9232                   && important_for_bundling_p (insn))
9233                 seen_good_insn = 1;
9234             }
9235           else if (recog_memoized (insn) >= 0
9236                    && important_for_bundling_p (insn))
9237             seen_good_insn = 1;
9238           need_barrier_p = (GET_CODE (insn) == CALL_INSN
9239                             || GET_CODE (PATTERN (insn)) == ASM_INPUT
9240                             || asm_noperands (PATTERN (insn)) >= 0);
9241         }
9242     }
9243 }
9244
9245 \f
9246
9247 /* If the following function returns TRUE, we will use the DFA
9248    insn scheduler.  */
9249
9250 static int
9251 ia64_first_cycle_multipass_dfa_lookahead (void)
9252 {
9253   return (reload_completed ? 6 : 4);
9254 }
9255
9256 /* The following function initiates variable `dfa_pre_cycle_insn'.  */
9257
9258 static void
9259 ia64_init_dfa_pre_cycle_insn (void)
9260 {
9261   if (temp_dfa_state == NULL)
9262     {
9263       dfa_state_size = state_size ();
9264       temp_dfa_state = xmalloc (dfa_state_size);
9265       prev_cycle_state = xmalloc (dfa_state_size);
9266     }
9267   dfa_pre_cycle_insn = make_insn_raw (gen_pre_cycle ());
9268   PREV_INSN (dfa_pre_cycle_insn) = NEXT_INSN (dfa_pre_cycle_insn) = NULL_RTX;
9269   recog_memoized (dfa_pre_cycle_insn);
9270   dfa_stop_insn = make_insn_raw (gen_insn_group_barrier (GEN_INT (3)));
9271   PREV_INSN (dfa_stop_insn) = NEXT_INSN (dfa_stop_insn) = NULL_RTX;
9272   recog_memoized (dfa_stop_insn);
9273 }
9274
9275 /* The following function returns the pseudo insn DFA_PRE_CYCLE_INSN
9276    used by the DFA insn scheduler.  */
9277
9278 static rtx
9279 ia64_dfa_pre_cycle_insn (void)
9280 {
9281   return dfa_pre_cycle_insn;
9282 }
9283
9284 /* The following function returns TRUE if PRODUCER (of type ilog or
9285    ld) produces address for CONSUMER (of type st or stf). */
9286
9287 int
9288 ia64_st_address_bypass_p (rtx producer, rtx consumer)
9289 {
9290   rtx dest, reg, mem;
9291
9292   gcc_assert (producer && consumer);
9293   dest = ia64_single_set (producer);
9294   gcc_assert (dest);
9295   reg = SET_DEST (dest);
9296   gcc_assert (reg);
9297   if (GET_CODE (reg) == SUBREG)
9298     reg = SUBREG_REG (reg);
9299   gcc_assert (GET_CODE (reg) == REG);
9300   
9301   dest = ia64_single_set (consumer);
9302   gcc_assert (dest);
9303   mem = SET_DEST (dest);
9304   gcc_assert (mem && GET_CODE (mem) == MEM);
9305   return reg_mentioned_p (reg, mem);
9306 }
9307
9308 /* The following function returns TRUE if PRODUCER (of type ilog or
9309    ld) produces address for CONSUMER (of type ld or fld). */
9310
9311 int
9312 ia64_ld_address_bypass_p (rtx producer, rtx consumer)
9313 {
9314   rtx dest, src, reg, mem;
9315
9316   gcc_assert (producer && consumer);
9317   dest = ia64_single_set (producer);
9318   gcc_assert (dest);
9319   reg = SET_DEST (dest);
9320   gcc_assert (reg);
9321   if (GET_CODE (reg) == SUBREG)
9322     reg = SUBREG_REG (reg);
9323   gcc_assert (GET_CODE (reg) == REG);
9324   
9325   src = ia64_single_set (consumer);
9326   gcc_assert (src);
9327   mem = SET_SRC (src);
9328   gcc_assert (mem);
9329  
9330   if (GET_CODE (mem) == UNSPEC && XVECLEN (mem, 0) > 0)
9331     mem = XVECEXP (mem, 0, 0);
9332   else if (GET_CODE (mem) == IF_THEN_ELSE)
9333     /* ??? Is this bypass necessary for ld.c?  */
9334     {
9335       gcc_assert (XINT (XEXP (XEXP (mem, 0), 0), 1) == UNSPEC_LDCCLR);
9336       mem = XEXP (mem, 1);
9337     }
9338      
9339   while (GET_CODE (mem) == SUBREG || GET_CODE (mem) == ZERO_EXTEND)
9340     mem = XEXP (mem, 0);
9341
9342   if (GET_CODE (mem) == UNSPEC)
9343     {
9344       int c = XINT (mem, 1);
9345
9346       gcc_assert (c == UNSPEC_LDA || c == UNSPEC_LDS || c == UNSPEC_LDS_A
9347                   || c == UNSPEC_LDSA);
9348       mem = XVECEXP (mem, 0, 0);
9349     }
9350
9351   /* Note that LO_SUM is used for GOT loads.  */
9352   gcc_assert (GET_CODE (mem) == LO_SUM || GET_CODE (mem) == MEM);
9353
9354   return reg_mentioned_p (reg, mem);
9355 }
9356
9357 /* The following function returns TRUE if INSN produces address for a
9358    load/store insn.  We will place such insns into M slot because it
9359    decreases its latency time.  */
9360
9361 int
9362 ia64_produce_address_p (rtx insn)
9363 {
9364   return insn->call;
9365 }
9366
9367 \f
9368 /* Emit pseudo-ops for the assembler to describe predicate relations.
9369    At present this assumes that we only consider predicate pairs to
9370    be mutex, and that the assembler can deduce proper values from
9371    straight-line code.  */
9372
9373 static void
9374 emit_predicate_relation_info (void)
9375 {
9376   basic_block bb;
9377
9378   FOR_EACH_BB_REVERSE (bb)
9379     {
9380       int r;
9381       rtx head = BB_HEAD (bb);
9382
9383       /* We only need such notes at code labels.  */
9384       if (GET_CODE (head) != CODE_LABEL)
9385         continue;
9386       if (NOTE_INSN_BASIC_BLOCK_P (NEXT_INSN (head)))
9387         head = NEXT_INSN (head);
9388
9389       /* Skip p0, which may be thought to be live due to (reg:DI p0)
9390          grabbing the entire block of predicate registers.  */
9391       for (r = PR_REG (2); r < PR_REG (64); r += 2)
9392         if (REGNO_REG_SET_P (df_get_live_in (bb), r))
9393           {
9394             rtx p = gen_rtx_REG (BImode, r);
9395             rtx n = emit_insn_after (gen_pred_rel_mutex (p), head);
9396             if (head == BB_END (bb))
9397               BB_END (bb) = n;
9398             head = n;
9399           }
9400     }
9401
9402   /* Look for conditional calls that do not return, and protect predicate
9403      relations around them.  Otherwise the assembler will assume the call
9404      returns, and complain about uses of call-clobbered predicates after
9405      the call.  */
9406   FOR_EACH_BB_REVERSE (bb)
9407     {
9408       rtx insn = BB_HEAD (bb);
9409
9410       while (1)
9411         {
9412           if (GET_CODE (insn) == CALL_INSN
9413               && GET_CODE (PATTERN (insn)) == COND_EXEC
9414               && find_reg_note (insn, REG_NORETURN, NULL_RTX))
9415             {
9416               rtx b = emit_insn_before (gen_safe_across_calls_all (), insn);
9417               rtx a = emit_insn_after (gen_safe_across_calls_normal (), insn);
9418               if (BB_HEAD (bb) == insn)
9419                 BB_HEAD (bb) = b;
9420               if (BB_END (bb) == insn)
9421                 BB_END (bb) = a;
9422             }
9423
9424           if (insn == BB_END (bb))
9425             break;
9426           insn = NEXT_INSN (insn);
9427         }
9428     }
9429 }
9430
9431 /* Perform machine dependent operations on the rtl chain INSNS.  */
9432
9433 static void
9434 ia64_reorg (void)
9435 {
9436   /* We are freeing block_for_insn in the toplev to keep compatibility
9437      with old MDEP_REORGS that are not CFG based.  Recompute it now.  */
9438   compute_bb_for_insn ();
9439
9440   /* If optimizing, we'll have split before scheduling.  */
9441   if (optimize == 0)
9442     split_all_insns ();
9443
9444   if (optimize && ia64_flag_schedule_insns2
9445       && dbg_cnt (ia64_sched2))
9446     {
9447       timevar_push (TV_SCHED2);
9448       ia64_final_schedule = 1;
9449
9450       initiate_bundle_states ();
9451       ia64_nop = make_insn_raw (gen_nop ());
9452       PREV_INSN (ia64_nop) = NEXT_INSN (ia64_nop) = NULL_RTX;
9453       recog_memoized (ia64_nop);
9454       clocks_length = get_max_uid () + 1;
9455       stops_p = XCNEWVEC (char, clocks_length);
9456
9457       if (ia64_tune == PROCESSOR_ITANIUM2)
9458         {
9459           pos_1 = get_cpu_unit_code ("2_1");
9460           pos_2 = get_cpu_unit_code ("2_2");
9461           pos_3 = get_cpu_unit_code ("2_3");
9462           pos_4 = get_cpu_unit_code ("2_4");
9463           pos_5 = get_cpu_unit_code ("2_5");
9464           pos_6 = get_cpu_unit_code ("2_6");
9465           _0mii_ = get_cpu_unit_code ("2b_0mii.");
9466           _0mmi_ = get_cpu_unit_code ("2b_0mmi.");
9467           _0mfi_ = get_cpu_unit_code ("2b_0mfi.");
9468           _0mmf_ = get_cpu_unit_code ("2b_0mmf.");
9469           _0bbb_ = get_cpu_unit_code ("2b_0bbb.");
9470           _0mbb_ = get_cpu_unit_code ("2b_0mbb.");
9471           _0mib_ = get_cpu_unit_code ("2b_0mib.");
9472           _0mmb_ = get_cpu_unit_code ("2b_0mmb.");
9473           _0mfb_ = get_cpu_unit_code ("2b_0mfb.");
9474           _0mlx_ = get_cpu_unit_code ("2b_0mlx.");
9475           _1mii_ = get_cpu_unit_code ("2b_1mii.");
9476           _1mmi_ = get_cpu_unit_code ("2b_1mmi.");
9477           _1mfi_ = get_cpu_unit_code ("2b_1mfi.");
9478           _1mmf_ = get_cpu_unit_code ("2b_1mmf.");
9479           _1bbb_ = get_cpu_unit_code ("2b_1bbb.");
9480           _1mbb_ = get_cpu_unit_code ("2b_1mbb.");
9481           _1mib_ = get_cpu_unit_code ("2b_1mib.");
9482           _1mmb_ = get_cpu_unit_code ("2b_1mmb.");
9483           _1mfb_ = get_cpu_unit_code ("2b_1mfb.");
9484           _1mlx_ = get_cpu_unit_code ("2b_1mlx.");
9485         }
9486       else
9487         {
9488           pos_1 = get_cpu_unit_code ("1_1");
9489           pos_2 = get_cpu_unit_code ("1_2");
9490           pos_3 = get_cpu_unit_code ("1_3");
9491           pos_4 = get_cpu_unit_code ("1_4");
9492           pos_5 = get_cpu_unit_code ("1_5");
9493           pos_6 = get_cpu_unit_code ("1_6");
9494           _0mii_ = get_cpu_unit_code ("1b_0mii.");
9495           _0mmi_ = get_cpu_unit_code ("1b_0mmi.");
9496           _0mfi_ = get_cpu_unit_code ("1b_0mfi.");
9497           _0mmf_ = get_cpu_unit_code ("1b_0mmf.");
9498           _0bbb_ = get_cpu_unit_code ("1b_0bbb.");
9499           _0mbb_ = get_cpu_unit_code ("1b_0mbb.");
9500           _0mib_ = get_cpu_unit_code ("1b_0mib.");
9501           _0mmb_ = get_cpu_unit_code ("1b_0mmb.");
9502           _0mfb_ = get_cpu_unit_code ("1b_0mfb.");
9503           _0mlx_ = get_cpu_unit_code ("1b_0mlx.");
9504           _1mii_ = get_cpu_unit_code ("1b_1mii.");
9505           _1mmi_ = get_cpu_unit_code ("1b_1mmi.");
9506           _1mfi_ = get_cpu_unit_code ("1b_1mfi.");
9507           _1mmf_ = get_cpu_unit_code ("1b_1mmf.");
9508           _1bbb_ = get_cpu_unit_code ("1b_1bbb.");
9509           _1mbb_ = get_cpu_unit_code ("1b_1mbb.");
9510           _1mib_ = get_cpu_unit_code ("1b_1mib.");
9511           _1mmb_ = get_cpu_unit_code ("1b_1mmb.");
9512           _1mfb_ = get_cpu_unit_code ("1b_1mfb.");
9513           _1mlx_ = get_cpu_unit_code ("1b_1mlx.");
9514         }
9515
9516       if (flag_selective_scheduling2
9517           && !maybe_skip_selective_scheduling ())
9518         run_selective_scheduling ();
9519       else
9520         schedule_ebbs ();
9521
9522       /* Redo alignment computation, as it might gone wrong.  */
9523       compute_alignments ();
9524
9525       /* We cannot reuse this one because it has been corrupted by the
9526          evil glat.  */
9527       finish_bundle_states ();
9528       free (stops_p);
9529       stops_p = NULL;
9530       emit_insn_group_barriers (dump_file);
9531
9532       ia64_final_schedule = 0;
9533       timevar_pop (TV_SCHED2);
9534     }
9535   else
9536     emit_all_insn_group_barriers (dump_file);
9537
9538   df_analyze ();
9539  
9540   /* A call must not be the last instruction in a function, so that the
9541      return address is still within the function, so that unwinding works
9542      properly.  Note that IA-64 differs from dwarf2 on this point.  */
9543   if (ia64_except_unwind_info (&global_options) == UI_TARGET)
9544     {
9545       rtx insn;
9546       int saw_stop = 0;
9547
9548       insn = get_last_insn ();
9549       if (! INSN_P (insn))
9550         insn = prev_active_insn (insn);
9551       if (insn)
9552         {
9553           /* Skip over insns that expand to nothing.  */
9554           while (GET_CODE (insn) == INSN
9555                  && get_attr_empty (insn) == EMPTY_YES)
9556             {
9557               if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
9558                   && XINT (PATTERN (insn), 1) == UNSPECV_INSN_GROUP_BARRIER)
9559                 saw_stop = 1;
9560               insn = prev_active_insn (insn);
9561             }
9562           if (GET_CODE (insn) == CALL_INSN)
9563             {
9564               if (! saw_stop)
9565                 emit_insn (gen_insn_group_barrier (GEN_INT (3)));
9566               emit_insn (gen_break_f ());
9567               emit_insn (gen_insn_group_barrier (GEN_INT (3)));
9568             }
9569         }
9570     }
9571
9572   emit_predicate_relation_info ();
9573
9574   if (ia64_flag_var_tracking)
9575     {
9576       timevar_push (TV_VAR_TRACKING);
9577       variable_tracking_main ();
9578       timevar_pop (TV_VAR_TRACKING);
9579     }
9580   df_finish_pass (false);
9581 }
9582 \f
9583 /* Return true if REGNO is used by the epilogue.  */
9584
9585 int
9586 ia64_epilogue_uses (int regno)
9587 {
9588   switch (regno)
9589     {
9590     case R_GR (1):
9591       /* With a call to a function in another module, we will write a new
9592          value to "gp".  After returning from such a call, we need to make
9593          sure the function restores the original gp-value, even if the
9594          function itself does not use the gp anymore.  */
9595       return !(TARGET_AUTO_PIC || TARGET_NO_PIC);
9596
9597     case IN_REG (0): case IN_REG (1): case IN_REG (2): case IN_REG (3):
9598     case IN_REG (4): case IN_REG (5): case IN_REG (6): case IN_REG (7):
9599       /* For functions defined with the syscall_linkage attribute, all
9600          input registers are marked as live at all function exits.  This
9601          prevents the register allocator from using the input registers,
9602          which in turn makes it possible to restart a system call after
9603          an interrupt without having to save/restore the input registers.
9604          This also prevents kernel data from leaking to application code.  */
9605       return lookup_attribute ("syscall_linkage",
9606            TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))) != NULL;
9607
9608     case R_BR (0):
9609       /* Conditional return patterns can't represent the use of `b0' as
9610          the return address, so we force the value live this way.  */
9611       return 1;
9612
9613     case AR_PFS_REGNUM:
9614       /* Likewise for ar.pfs, which is used by br.ret.  */
9615       return 1;
9616
9617     default:
9618       return 0;
9619     }
9620 }
9621
9622 /* Return true if REGNO is used by the frame unwinder.  */
9623
9624 int
9625 ia64_eh_uses (int regno)
9626 {
9627   unsigned int r;
9628
9629   if (! reload_completed)
9630     return 0;
9631
9632   if (regno == 0)
9633     return 0;
9634
9635   for (r = reg_save_b0; r <= reg_save_ar_lc; r++)
9636     if (regno == current_frame_info.r[r]
9637        || regno == emitted_frame_related_regs[r])
9638       return 1;
9639
9640   return 0;
9641 }
9642 \f
9643 /* Return true if this goes in small data/bss.  */
9644
9645 /* ??? We could also support own long data here.  Generating movl/add/ld8
9646    instead of addl,ld8/ld8.  This makes the code bigger, but should make the
9647    code faster because there is one less load.  This also includes incomplete
9648    types which can't go in sdata/sbss.  */
9649
9650 static bool
9651 ia64_in_small_data_p (const_tree exp)
9652 {
9653   if (TARGET_NO_SDATA)
9654     return false;
9655
9656   /* We want to merge strings, so we never consider them small data.  */
9657   if (TREE_CODE (exp) == STRING_CST)
9658     return false;
9659
9660   /* Functions are never small data.  */
9661   if (TREE_CODE (exp) == FUNCTION_DECL)
9662     return false;
9663
9664   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
9665     {
9666       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
9667
9668       if (strcmp (section, ".sdata") == 0
9669           || strncmp (section, ".sdata.", 7) == 0
9670           || strncmp (section, ".gnu.linkonce.s.", 16) == 0
9671           || strcmp (section, ".sbss") == 0
9672           || strncmp (section, ".sbss.", 6) == 0
9673           || strncmp (section, ".gnu.linkonce.sb.", 17) == 0)
9674         return true;
9675     }
9676   else
9677     {
9678       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
9679
9680       /* If this is an incomplete type with size 0, then we can't put it
9681          in sdata because it might be too big when completed.  */
9682       if (size > 0 && size <= ia64_section_threshold)
9683         return true;
9684     }
9685
9686   return false;
9687 }
9688 \f
9689 /* Output assembly directives for prologue regions.  */
9690
9691 /* The current basic block number.  */
9692
9693 static bool last_block;
9694
9695 /* True if we need a copy_state command at the start of the next block.  */
9696
9697 static bool need_copy_state;
9698
9699 #ifndef MAX_ARTIFICIAL_LABEL_BYTES
9700 # define MAX_ARTIFICIAL_LABEL_BYTES 30
9701 #endif
9702
9703 /* Emit a debugging label after a call-frame-related insn.  We'd
9704    rather output the label right away, but we'd have to output it
9705    after, not before, the instruction, and the instruction has not
9706    been output yet.  So we emit the label after the insn, delete it to
9707    avoid introducing basic blocks, and mark it as preserved, such that
9708    it is still output, given that it is referenced in debug info.  */
9709
9710 static const char *
9711 ia64_emit_deleted_label_after_insn (rtx insn)
9712 {
9713   char label[MAX_ARTIFICIAL_LABEL_BYTES];
9714   rtx lb = gen_label_rtx ();
9715   rtx label_insn = emit_label_after (lb, insn);
9716
9717   LABEL_PRESERVE_P (lb) = 1;
9718
9719   delete_insn (label_insn);
9720
9721   ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (label_insn));
9722
9723   return xstrdup (label);
9724 }
9725
9726 /* Define the CFA after INSN with the steady-state definition.  */
9727
9728 static void
9729 ia64_dwarf2out_def_steady_cfa (rtx insn, bool frame)
9730 {
9731   rtx fp = frame_pointer_needed
9732     ? hard_frame_pointer_rtx
9733     : stack_pointer_rtx;
9734   const char *label = ia64_emit_deleted_label_after_insn (insn);
9735
9736   if (!frame)
9737     return;
9738
9739   dwarf2out_def_cfa
9740     (label, REGNO (fp),
9741      ia64_initial_elimination_offset
9742      (REGNO (arg_pointer_rtx), REGNO (fp))
9743      + ARG_POINTER_CFA_OFFSET (current_function_decl));
9744 }
9745
9746 /* All we need to do here is avoid a crash in the generic dwarf2
9747    processing.  The real CFA definition is set up above.  */
9748
9749 static void
9750 ia64_dwarf_handle_frame_unspec (const char * ARG_UNUSED (label),
9751                                 rtx ARG_UNUSED (pattern),
9752                                 int index)
9753 {
9754   gcc_assert (index == UNSPECV_ALLOC);
9755 }
9756
9757 /* The generic dwarf2 frame debug info generator does not define a
9758    separate region for the very end of the epilogue, so refrain from
9759    doing so in the IA64-specific code as well.  */
9760
9761 #define IA64_CHANGE_CFA_IN_EPILOGUE 0
9762
9763 /* The function emits unwind directives for the start of an epilogue.  */
9764
9765 static void
9766 process_epilogue (FILE *asm_out_file, rtx insn, bool unwind, bool frame)
9767 {
9768   /* If this isn't the last block of the function, then we need to label the
9769      current state, and copy it back in at the start of the next block.  */
9770
9771   if (!last_block)
9772     {
9773       if (unwind)
9774         fprintf (asm_out_file, "\t.label_state %d\n",
9775                  ++cfun->machine->state_num);
9776       need_copy_state = true;
9777     }
9778
9779   if (unwind)
9780     fprintf (asm_out_file, "\t.restore sp\n");
9781   if (IA64_CHANGE_CFA_IN_EPILOGUE && frame)
9782     dwarf2out_def_cfa (ia64_emit_deleted_label_after_insn (insn),
9783                        STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
9784 }
9785
9786 /* This function processes a SET pattern for REG_CFA_ADJUST_CFA.  */
9787
9788 static void
9789 process_cfa_adjust_cfa (FILE *asm_out_file, rtx pat, rtx insn,
9790                         bool unwind, bool frame)
9791 {
9792   rtx dest = SET_DEST (pat);
9793   rtx src = SET_SRC (pat);
9794
9795   if (dest == stack_pointer_rtx)
9796     {
9797       if (GET_CODE (src) == PLUS)
9798         {
9799           rtx op0 = XEXP (src, 0);
9800           rtx op1 = XEXP (src, 1);
9801           
9802           gcc_assert (op0 == dest && GET_CODE (op1) == CONST_INT);
9803           
9804           if (INTVAL (op1) < 0)
9805             {
9806               gcc_assert (!frame_pointer_needed);
9807               if (unwind)
9808                 fprintf (asm_out_file,
9809                          "\t.fframe "HOST_WIDE_INT_PRINT_DEC"\n",
9810                          -INTVAL (op1));
9811               ia64_dwarf2out_def_steady_cfa (insn, frame);
9812             }
9813           else
9814             process_epilogue (asm_out_file, insn, unwind, frame);
9815         }
9816       else
9817         {
9818           gcc_assert (src == hard_frame_pointer_rtx);
9819           process_epilogue (asm_out_file, insn, unwind, frame);
9820         }
9821     }
9822   else if (dest == hard_frame_pointer_rtx)
9823     {
9824       gcc_assert (src == stack_pointer_rtx);
9825       gcc_assert (frame_pointer_needed);
9826
9827       if (unwind)
9828         fprintf (asm_out_file, "\t.vframe r%d\n",
9829                  ia64_dbx_register_number (REGNO (dest)));
9830       ia64_dwarf2out_def_steady_cfa (insn, frame);
9831     }
9832   else
9833     gcc_unreachable ();
9834 }
9835
9836 /* This function processes a SET pattern for REG_CFA_REGISTER.  */
9837
9838 static void
9839 process_cfa_register (FILE *asm_out_file, rtx pat, bool unwind)
9840 {
9841   rtx dest = SET_DEST (pat);
9842   rtx src = SET_SRC (pat);
9843
9844   int dest_regno = REGNO (dest);
9845   int src_regno = REGNO (src);
9846
9847   switch (src_regno)
9848     {
9849     case BR_REG (0):
9850       /* Saving return address pointer.  */
9851       gcc_assert (dest_regno == current_frame_info.r[reg_save_b0]);
9852       if (unwind)
9853         fprintf (asm_out_file, "\t.save rp, r%d\n",
9854                  ia64_dbx_register_number (dest_regno));
9855       break;
9856
9857     case PR_REG (0):
9858       gcc_assert (dest_regno == current_frame_info.r[reg_save_pr]);
9859       if (unwind)
9860         fprintf (asm_out_file, "\t.save pr, r%d\n",
9861                  ia64_dbx_register_number (dest_regno));
9862       break;
9863
9864     case AR_UNAT_REGNUM:
9865       gcc_assert (dest_regno == current_frame_info.r[reg_save_ar_unat]);
9866       if (unwind)
9867         fprintf (asm_out_file, "\t.save ar.unat, r%d\n",
9868                  ia64_dbx_register_number (dest_regno));
9869       break;
9870
9871     case AR_LC_REGNUM:
9872       gcc_assert (dest_regno == current_frame_info.r[reg_save_ar_lc]);
9873       if (unwind)
9874         fprintf (asm_out_file, "\t.save ar.lc, r%d\n",
9875                  ia64_dbx_register_number (dest_regno));
9876       break;
9877
9878     default:
9879       /* Everything else should indicate being stored to memory.  */
9880       gcc_unreachable ();
9881     }
9882 }
9883
9884 /* This function processes a SET pattern for REG_CFA_OFFSET.  */
9885
9886 static void
9887 process_cfa_offset (FILE *asm_out_file, rtx pat, bool unwind)
9888 {
9889   rtx dest = SET_DEST (pat);
9890   rtx src = SET_SRC (pat);
9891   int src_regno = REGNO (src);
9892   const char *saveop;
9893   HOST_WIDE_INT off;
9894   rtx base;
9895
9896   gcc_assert (MEM_P (dest));
9897   if (GET_CODE (XEXP (dest, 0)) == REG)
9898     {
9899       base = XEXP (dest, 0);
9900       off = 0;
9901     }
9902   else
9903     {
9904       gcc_assert (GET_CODE (XEXP (dest, 0)) == PLUS
9905                   && GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT);
9906       base = XEXP (XEXP (dest, 0), 0);
9907       off = INTVAL (XEXP (XEXP (dest, 0), 1));
9908     }
9909
9910   if (base == hard_frame_pointer_rtx)
9911     {
9912       saveop = ".savepsp";
9913       off = - off;
9914     }
9915   else
9916     {
9917       gcc_assert (base == stack_pointer_rtx);
9918       saveop = ".savesp";
9919     }
9920
9921   src_regno = REGNO (src);
9922   switch (src_regno)
9923     {
9924     case BR_REG (0):
9925       gcc_assert (!current_frame_info.r[reg_save_b0]);
9926       if (unwind)
9927         fprintf (asm_out_file, "\t%s rp, " HOST_WIDE_INT_PRINT_DEC "\n",
9928                  saveop, off);
9929       break;
9930
9931     case PR_REG (0):
9932       gcc_assert (!current_frame_info.r[reg_save_pr]);
9933       if (unwind)
9934         fprintf (asm_out_file, "\t%s pr, " HOST_WIDE_INT_PRINT_DEC "\n",
9935                  saveop, off);
9936       break;
9937
9938     case AR_LC_REGNUM:
9939       gcc_assert (!current_frame_info.r[reg_save_ar_lc]);
9940       if (unwind)
9941         fprintf (asm_out_file, "\t%s ar.lc, " HOST_WIDE_INT_PRINT_DEC "\n",
9942                  saveop, off);
9943       break;
9944
9945     case AR_PFS_REGNUM:
9946       gcc_assert (!current_frame_info.r[reg_save_ar_pfs]);
9947       if (unwind)
9948         fprintf (asm_out_file, "\t%s ar.pfs, " HOST_WIDE_INT_PRINT_DEC "\n",
9949                  saveop, off);
9950       break;
9951
9952     case AR_UNAT_REGNUM:
9953       gcc_assert (!current_frame_info.r[reg_save_ar_unat]);
9954       if (unwind)
9955         fprintf (asm_out_file, "\t%s ar.unat, " HOST_WIDE_INT_PRINT_DEC "\n",
9956                  saveop, off);
9957       break;
9958
9959     case GR_REG (4):
9960     case GR_REG (5):
9961     case GR_REG (6):
9962     case GR_REG (7):
9963       if (unwind)
9964         fprintf (asm_out_file, "\t.save.g 0x%x\n",
9965                  1 << (src_regno - GR_REG (4)));
9966       break;
9967
9968     case BR_REG (1):
9969     case BR_REG (2):
9970     case BR_REG (3):
9971     case BR_REG (4):
9972     case BR_REG (5):
9973       if (unwind)
9974         fprintf (asm_out_file, "\t.save.b 0x%x\n",
9975                  1 << (src_regno - BR_REG (1)));
9976       break;
9977
9978     case FR_REG (2):
9979     case FR_REG (3):
9980     case FR_REG (4):
9981     case FR_REG (5):
9982       if (unwind)
9983         fprintf (asm_out_file, "\t.save.f 0x%x\n",
9984                  1 << (src_regno - FR_REG (2)));
9985       break;
9986
9987     case FR_REG (16): case FR_REG (17): case FR_REG (18): case FR_REG (19):
9988     case FR_REG (20): case FR_REG (21): case FR_REG (22): case FR_REG (23):
9989     case FR_REG (24): case FR_REG (25): case FR_REG (26): case FR_REG (27):
9990     case FR_REG (28): case FR_REG (29): case FR_REG (30): case FR_REG (31):
9991       if (unwind)
9992         fprintf (asm_out_file, "\t.save.gf 0x0, 0x%x\n",
9993                  1 << (src_regno - FR_REG (12)));
9994       break;
9995
9996     default:
9997       /* ??? For some reason we mark other general registers, even those
9998          we can't represent in the unwind info.  Ignore them.  */
9999       break;
10000     }
10001 }
10002
10003 /* This function looks at a single insn and emits any directives
10004    required to unwind this insn.  */
10005
10006 static void
10007 ia64_asm_unwind_emit (FILE *asm_out_file, rtx insn)
10008 {
10009   bool unwind = ia64_except_unwind_info (&global_options) == UI_TARGET;
10010   bool frame = dwarf2out_do_frame ();
10011   rtx note, pat;
10012   bool handled_one;
10013
10014   if (!unwind && !frame)
10015     return;
10016
10017   if (NOTE_INSN_BASIC_BLOCK_P (insn))
10018     {
10019       last_block = NOTE_BASIC_BLOCK (insn)->next_bb == EXIT_BLOCK_PTR;
10020
10021       /* Restore unwind state from immediately before the epilogue.  */
10022       if (need_copy_state)
10023         {
10024           if (unwind)
10025             {
10026               fprintf (asm_out_file, "\t.body\n");
10027               fprintf (asm_out_file, "\t.copy_state %d\n",
10028                        cfun->machine->state_num);
10029             }
10030           if (IA64_CHANGE_CFA_IN_EPILOGUE)
10031             ia64_dwarf2out_def_steady_cfa (insn, frame);
10032           need_copy_state = false;
10033         }
10034     }
10035
10036   if (GET_CODE (insn) == NOTE || ! RTX_FRAME_RELATED_P (insn))
10037     return;
10038
10039   /* Look for the ALLOC insn.  */
10040   if (INSN_CODE (insn) == CODE_FOR_alloc)
10041     {
10042       rtx dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
10043       int dest_regno = REGNO (dest);
10044
10045       /* If this is the final destination for ar.pfs, then this must
10046          be the alloc in the prologue.  */
10047       if (dest_regno == current_frame_info.r[reg_save_ar_pfs])
10048         {
10049           if (unwind)
10050             fprintf (asm_out_file, "\t.save ar.pfs, r%d\n",
10051                      ia64_dbx_register_number (dest_regno));
10052         }
10053       else
10054         {
10055           /* This must be an alloc before a sibcall.  We must drop the
10056              old frame info.  The easiest way to drop the old frame
10057              info is to ensure we had a ".restore sp" directive
10058              followed by a new prologue.  If the procedure doesn't
10059              have a memory-stack frame, we'll issue a dummy ".restore
10060              sp" now.  */
10061           if (current_frame_info.total_size == 0 && !frame_pointer_needed)
10062             /* if haven't done process_epilogue() yet, do it now */
10063             process_epilogue (asm_out_file, insn, unwind, frame);
10064           if (unwind)
10065             fprintf (asm_out_file, "\t.prologue\n");
10066         }
10067       return;
10068     }
10069
10070   handled_one = false;
10071   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
10072     switch (REG_NOTE_KIND (note))
10073       {
10074       case REG_CFA_ADJUST_CFA:
10075         pat = XEXP (note, 0);
10076         if (pat == NULL)
10077           pat = PATTERN (insn);
10078         process_cfa_adjust_cfa (asm_out_file, pat, insn, unwind, frame);
10079         handled_one = true;
10080         break;
10081
10082       case REG_CFA_OFFSET:
10083         pat = XEXP (note, 0);
10084         if (pat == NULL)
10085           pat = PATTERN (insn);
10086         process_cfa_offset (asm_out_file, pat, unwind);
10087         handled_one = true;
10088         break;
10089
10090       case REG_CFA_REGISTER:
10091         pat = XEXP (note, 0);
10092         if (pat == NULL)
10093           pat = PATTERN (insn);
10094         process_cfa_register (asm_out_file, pat, unwind);
10095         handled_one = true;
10096         break;
10097
10098       case REG_FRAME_RELATED_EXPR:
10099       case REG_CFA_DEF_CFA:
10100       case REG_CFA_EXPRESSION:
10101       case REG_CFA_RESTORE:
10102       case REG_CFA_SET_VDRAP:
10103         /* Not used in the ia64 port.  */
10104         gcc_unreachable ();
10105
10106       default:
10107         /* Not a frame-related note.  */
10108         break;
10109       }
10110
10111   /* All REG_FRAME_RELATED_P insns, besides ALLOC, are marked with the
10112      explicit action to take.  No guessing required.  */
10113   gcc_assert (handled_one);
10114 }
10115
10116 /* Implement TARGET_ASM_EMIT_EXCEPT_PERSONALITY.  */
10117
10118 static void
10119 ia64_asm_emit_except_personality (rtx personality)
10120 {
10121   fputs ("\t.personality\t", asm_out_file);
10122   output_addr_const (asm_out_file, personality);
10123   fputc ('\n', asm_out_file);
10124 }
10125
10126 /* Implement TARGET_ASM_INITIALIZE_SECTIONS.  */
10127
10128 static void
10129 ia64_asm_init_sections (void)
10130 {
10131   exception_section = get_unnamed_section (0, output_section_asm_op,
10132                                            "\t.handlerdata");
10133 }
10134
10135 /* Implement TARGET_DEBUG_UNWIND_INFO.  */
10136
10137 static enum unwind_info_type
10138 ia64_debug_unwind_info (void)
10139 {
10140   return UI_TARGET;
10141 }
10142
10143 /* Implement TARGET_EXCEPT_UNWIND_INFO.  */
10144
10145 static enum unwind_info_type
10146 ia64_except_unwind_info (struct gcc_options *opts)
10147 {
10148   /* Honor the --enable-sjlj-exceptions configure switch.  */
10149 #ifdef CONFIG_UNWIND_EXCEPTIONS
10150   if (CONFIG_UNWIND_EXCEPTIONS)
10151     return UI_SJLJ;
10152 #endif
10153
10154   /* For simplicity elsewhere in this file, indicate that all unwind
10155      info is disabled if we're not emitting unwind tables.  */
10156   if (!opts->x_flag_exceptions && !opts->x_flag_unwind_tables)
10157     return UI_NONE;
10158
10159   return UI_TARGET;
10160 }
10161 \f
10162 enum ia64_builtins
10163 {
10164   IA64_BUILTIN_BSP,
10165   IA64_BUILTIN_COPYSIGNQ,
10166   IA64_BUILTIN_FABSQ,
10167   IA64_BUILTIN_FLUSHRS,
10168   IA64_BUILTIN_INFQ,
10169   IA64_BUILTIN_HUGE_VALQ,
10170   IA64_BUILTIN_max
10171 };
10172
10173 static GTY(()) tree ia64_builtins[(int) IA64_BUILTIN_max];
10174
10175 void
10176 ia64_init_builtins (void)
10177 {
10178   tree fpreg_type;
10179   tree float80_type;
10180   tree decl;
10181
10182   /* The __fpreg type.  */
10183   fpreg_type = make_node (REAL_TYPE);
10184   TYPE_PRECISION (fpreg_type) = 82;
10185   layout_type (fpreg_type);
10186   (*lang_hooks.types.register_builtin_type) (fpreg_type, "__fpreg");
10187
10188   /* The __float80 type.  */
10189   float80_type = make_node (REAL_TYPE);
10190   TYPE_PRECISION (float80_type) = 80;
10191   layout_type (float80_type);
10192   (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
10193
10194   /* The __float128 type.  */
10195   if (!TARGET_HPUX)
10196     {
10197       tree ftype;
10198       tree float128_type = make_node (REAL_TYPE);
10199
10200       TYPE_PRECISION (float128_type) = 128;
10201       layout_type (float128_type);
10202       (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
10203
10204       /* TFmode support builtins.  */
10205       ftype = build_function_type (float128_type, void_list_node);
10206       decl = add_builtin_function ("__builtin_infq", ftype,
10207                                    IA64_BUILTIN_INFQ, BUILT_IN_MD,
10208                                    NULL, NULL_TREE);
10209       ia64_builtins[IA64_BUILTIN_INFQ] = decl;
10210
10211       decl = add_builtin_function ("__builtin_huge_valq", ftype,
10212                                    IA64_BUILTIN_HUGE_VALQ, BUILT_IN_MD,
10213                                    NULL, NULL_TREE);
10214       ia64_builtins[IA64_BUILTIN_HUGE_VALQ] = decl;
10215
10216       ftype = build_function_type_list (float128_type,
10217                                         float128_type,
10218                                         NULL_TREE);
10219       decl = add_builtin_function ("__builtin_fabsq", ftype,
10220                                    IA64_BUILTIN_FABSQ, BUILT_IN_MD,
10221                                    "__fabstf2", NULL_TREE);
10222       TREE_READONLY (decl) = 1;
10223       ia64_builtins[IA64_BUILTIN_FABSQ] = decl;
10224
10225       ftype = build_function_type_list (float128_type,
10226                                         float128_type,
10227                                         float128_type,
10228                                         NULL_TREE);
10229       decl = add_builtin_function ("__builtin_copysignq", ftype,
10230                                    IA64_BUILTIN_COPYSIGNQ, BUILT_IN_MD,
10231                                    "__copysigntf3", NULL_TREE);
10232       TREE_READONLY (decl) = 1;
10233       ia64_builtins[IA64_BUILTIN_COPYSIGNQ] = decl;
10234     }
10235   else
10236     /* Under HPUX, this is a synonym for "long double".  */
10237     (*lang_hooks.types.register_builtin_type) (long_double_type_node,
10238                                                "__float128");
10239
10240   /* Fwrite on VMS is non-standard.  */
10241   if (TARGET_ABI_OPEN_VMS)
10242     {
10243       implicit_built_in_decls[(int) BUILT_IN_FWRITE] = NULL_TREE;
10244       implicit_built_in_decls[(int) BUILT_IN_FWRITE_UNLOCKED] = NULL_TREE;
10245     }
10246
10247 #define def_builtin(name, type, code)                                   \
10248   add_builtin_function ((name), (type), (code), BUILT_IN_MD,    \
10249                        NULL, NULL_TREE)
10250
10251   decl = def_builtin ("__builtin_ia64_bsp",
10252                build_function_type (ptr_type_node, void_list_node),
10253                IA64_BUILTIN_BSP);
10254   ia64_builtins[IA64_BUILTIN_BSP] = decl;
10255
10256   decl = def_builtin ("__builtin_ia64_flushrs",
10257                build_function_type (void_type_node, void_list_node),
10258                IA64_BUILTIN_FLUSHRS);
10259   ia64_builtins[IA64_BUILTIN_FLUSHRS] = decl;
10260
10261 #undef def_builtin
10262
10263   if (TARGET_HPUX)
10264     {
10265       if (built_in_decls [BUILT_IN_FINITE])
10266         set_user_assembler_name (built_in_decls [BUILT_IN_FINITE],
10267           "_Isfinite");
10268       if (built_in_decls [BUILT_IN_FINITEF])
10269         set_user_assembler_name (built_in_decls [BUILT_IN_FINITEF],
10270           "_Isfinitef");
10271       if (built_in_decls [BUILT_IN_FINITEL])
10272         set_user_assembler_name (built_in_decls [BUILT_IN_FINITEL],
10273           "_Isfinitef128");
10274     }
10275 }
10276
10277 rtx
10278 ia64_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
10279                      enum machine_mode mode ATTRIBUTE_UNUSED,
10280                      int ignore ATTRIBUTE_UNUSED)
10281 {
10282   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10283   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10284
10285   switch (fcode)
10286     {
10287     case IA64_BUILTIN_BSP:
10288       if (! target || ! register_operand (target, DImode))
10289         target = gen_reg_rtx (DImode);
10290       emit_insn (gen_bsp_value (target));
10291 #ifdef POINTERS_EXTEND_UNSIGNED
10292       target = convert_memory_address (ptr_mode, target);
10293 #endif
10294       return target;
10295
10296     case IA64_BUILTIN_FLUSHRS:
10297       emit_insn (gen_flushrs ());
10298       return const0_rtx;
10299
10300     case IA64_BUILTIN_INFQ:
10301     case IA64_BUILTIN_HUGE_VALQ:
10302       {
10303         enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
10304         REAL_VALUE_TYPE inf;
10305         rtx tmp;
10306
10307         real_inf (&inf);
10308         tmp = CONST_DOUBLE_FROM_REAL_VALUE (inf, target_mode);
10309
10310         tmp = validize_mem (force_const_mem (target_mode, tmp));
10311
10312         if (target == 0)
10313           target = gen_reg_rtx (target_mode);
10314
10315         emit_move_insn (target, tmp);
10316         return target;
10317       }
10318
10319     case IA64_BUILTIN_FABSQ:
10320     case IA64_BUILTIN_COPYSIGNQ:
10321       return expand_call (exp, target, ignore);
10322
10323     default:
10324       gcc_unreachable ();
10325     }
10326
10327   return NULL_RTX;
10328 }
10329
10330 /* Return the ia64 builtin for CODE.  */
10331
10332 static tree
10333 ia64_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
10334 {
10335   if (code >= IA64_BUILTIN_max)
10336     return error_mark_node;
10337
10338   return ia64_builtins[code];
10339 }
10340
10341 /* For the HP-UX IA64 aggregate parameters are passed stored in the
10342    most significant bits of the stack slot.  */
10343
10344 enum direction
10345 ia64_hpux_function_arg_padding (enum machine_mode mode, const_tree type)
10346 {
10347    /* Exception to normal case for structures/unions/etc.  */
10348
10349    if (type && AGGREGATE_TYPE_P (type)
10350        && int_size_in_bytes (type) < UNITS_PER_WORD)
10351      return upward;
10352
10353    /* Fall back to the default.  */
10354    return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
10355 }
10356
10357 /* Emit text to declare externally defined variables and functions, because
10358    the Intel assembler does not support undefined externals.  */
10359
10360 void
10361 ia64_asm_output_external (FILE *file, tree decl, const char *name)
10362 {
10363   /* We output the name if and only if TREE_SYMBOL_REFERENCED is
10364      set in order to avoid putting out names that are never really
10365      used. */
10366   if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
10367     {
10368       /* maybe_assemble_visibility will return 1 if the assembler
10369          visibility directive is output.  */
10370       int need_visibility = ((*targetm.binds_local_p) (decl)
10371                              && maybe_assemble_visibility (decl));
10372
10373 #ifdef DO_CRTL_NAMES
10374       DO_CRTL_NAMES;
10375 #endif
10376
10377       /* GNU as does not need anything here, but the HP linker does
10378          need something for external functions.  */
10379       if ((TARGET_HPUX_LD || !TARGET_GNU_AS)
10380           && TREE_CODE (decl) == FUNCTION_DECL)
10381           (*targetm.asm_out.globalize_decl_name) (file, decl);
10382       else if (need_visibility && !TARGET_GNU_AS)
10383         (*targetm.asm_out.globalize_label) (file, name);
10384     }
10385 }
10386
10387 /* Set SImode div/mod functions, init_integral_libfuncs only initializes
10388    modes of word_mode and larger.  Rename the TFmode libfuncs using the
10389    HPUX conventions. __divtf3 is used for XFmode. We need to keep it for
10390    backward compatibility. */
10391
10392 static void
10393 ia64_init_libfuncs (void)
10394 {
10395   set_optab_libfunc (sdiv_optab, SImode, "__divsi3");
10396   set_optab_libfunc (udiv_optab, SImode, "__udivsi3");
10397   set_optab_libfunc (smod_optab, SImode, "__modsi3");
10398   set_optab_libfunc (umod_optab, SImode, "__umodsi3");
10399
10400   set_optab_libfunc (add_optab, TFmode, "_U_Qfadd");
10401   set_optab_libfunc (sub_optab, TFmode, "_U_Qfsub");
10402   set_optab_libfunc (smul_optab, TFmode, "_U_Qfmpy");
10403   set_optab_libfunc (sdiv_optab, TFmode, "_U_Qfdiv");
10404   set_optab_libfunc (neg_optab, TFmode, "_U_Qfneg");
10405
10406   set_conv_libfunc (sext_optab, TFmode, SFmode, "_U_Qfcnvff_sgl_to_quad");
10407   set_conv_libfunc (sext_optab, TFmode, DFmode, "_U_Qfcnvff_dbl_to_quad");
10408   set_conv_libfunc (sext_optab, TFmode, XFmode, "_U_Qfcnvff_f80_to_quad");
10409   set_conv_libfunc (trunc_optab, SFmode, TFmode, "_U_Qfcnvff_quad_to_sgl");
10410   set_conv_libfunc (trunc_optab, DFmode, TFmode, "_U_Qfcnvff_quad_to_dbl");
10411   set_conv_libfunc (trunc_optab, XFmode, TFmode, "_U_Qfcnvff_quad_to_f80");
10412
10413   set_conv_libfunc (sfix_optab, SImode, TFmode, "_U_Qfcnvfxt_quad_to_sgl");
10414   set_conv_libfunc (sfix_optab, DImode, TFmode, "_U_Qfcnvfxt_quad_to_dbl");
10415   set_conv_libfunc (sfix_optab, TImode, TFmode, "_U_Qfcnvfxt_quad_to_quad");
10416   set_conv_libfunc (ufix_optab, SImode, TFmode, "_U_Qfcnvfxut_quad_to_sgl");
10417   set_conv_libfunc (ufix_optab, DImode, TFmode, "_U_Qfcnvfxut_quad_to_dbl");
10418
10419   set_conv_libfunc (sfloat_optab, TFmode, SImode, "_U_Qfcnvxf_sgl_to_quad");
10420   set_conv_libfunc (sfloat_optab, TFmode, DImode, "_U_Qfcnvxf_dbl_to_quad");
10421   set_conv_libfunc (sfloat_optab, TFmode, TImode, "_U_Qfcnvxf_quad_to_quad");
10422   /* HP-UX 11.23 libc does not have a function for unsigned
10423      SImode-to-TFmode conversion.  */
10424   set_conv_libfunc (ufloat_optab, TFmode, DImode, "_U_Qfcnvxuf_dbl_to_quad");
10425 }
10426
10427 /* Rename all the TFmode libfuncs using the HPUX conventions.  */
10428
10429 static void
10430 ia64_hpux_init_libfuncs (void)
10431 {
10432   ia64_init_libfuncs ();
10433
10434   /* The HP SI millicode division and mod functions expect DI arguments.
10435      By turning them off completely we avoid using both libgcc and the
10436      non-standard millicode routines and use the HP DI millicode routines
10437      instead.  */
10438
10439   set_optab_libfunc (sdiv_optab, SImode, 0);
10440   set_optab_libfunc (udiv_optab, SImode, 0);
10441   set_optab_libfunc (smod_optab, SImode, 0);
10442   set_optab_libfunc (umod_optab, SImode, 0);
10443
10444   set_optab_libfunc (sdiv_optab, DImode, "__milli_divI");
10445   set_optab_libfunc (udiv_optab, DImode, "__milli_divU");
10446   set_optab_libfunc (smod_optab, DImode, "__milli_remI");
10447   set_optab_libfunc (umod_optab, DImode, "__milli_remU");
10448
10449   /* HP-UX libc has TF min/max/abs routines in it.  */
10450   set_optab_libfunc (smin_optab, TFmode, "_U_Qfmin");
10451   set_optab_libfunc (smax_optab, TFmode, "_U_Qfmax");
10452   set_optab_libfunc (abs_optab, TFmode, "_U_Qfabs");
10453
10454   /* ia64_expand_compare uses this.  */
10455   cmptf_libfunc = init_one_libfunc ("_U_Qfcmp");
10456
10457   /* These should never be used.  */
10458   set_optab_libfunc (eq_optab, TFmode, 0);
10459   set_optab_libfunc (ne_optab, TFmode, 0);
10460   set_optab_libfunc (gt_optab, TFmode, 0);
10461   set_optab_libfunc (ge_optab, TFmode, 0);
10462   set_optab_libfunc (lt_optab, TFmode, 0);
10463   set_optab_libfunc (le_optab, TFmode, 0);
10464 }
10465
10466 /* Rename the division and modulus functions in VMS.  */
10467
10468 static void
10469 ia64_vms_init_libfuncs (void)
10470 {
10471   set_optab_libfunc (sdiv_optab, SImode, "OTS$DIV_I");
10472   set_optab_libfunc (sdiv_optab, DImode, "OTS$DIV_L");
10473   set_optab_libfunc (udiv_optab, SImode, "OTS$DIV_UI");
10474   set_optab_libfunc (udiv_optab, DImode, "OTS$DIV_UL");
10475   set_optab_libfunc (smod_optab, SImode, "OTS$REM_I");
10476   set_optab_libfunc (smod_optab, DImode, "OTS$REM_L");
10477   set_optab_libfunc (umod_optab, SImode, "OTS$REM_UI");
10478   set_optab_libfunc (umod_optab, DImode, "OTS$REM_UL");
10479   abort_libfunc = init_one_libfunc ("decc$abort");
10480   memcmp_libfunc = init_one_libfunc ("decc$memcmp");
10481 #ifdef MEM_LIBFUNCS_INIT
10482   MEM_LIBFUNCS_INIT;
10483 #endif
10484 }
10485
10486 /* Rename the TFmode libfuncs available from soft-fp in glibc using
10487    the HPUX conventions.  */
10488
10489 static void
10490 ia64_sysv4_init_libfuncs (void)
10491 {
10492   ia64_init_libfuncs ();
10493
10494   /* These functions are not part of the HPUX TFmode interface.  We
10495      use them instead of _U_Qfcmp, which doesn't work the way we
10496      expect.  */
10497   set_optab_libfunc (eq_optab, TFmode, "_U_Qfeq");
10498   set_optab_libfunc (ne_optab, TFmode, "_U_Qfne");
10499   set_optab_libfunc (gt_optab, TFmode, "_U_Qfgt");
10500   set_optab_libfunc (ge_optab, TFmode, "_U_Qfge");
10501   set_optab_libfunc (lt_optab, TFmode, "_U_Qflt");
10502   set_optab_libfunc (le_optab, TFmode, "_U_Qfle");
10503
10504   /* We leave out _U_Qfmin, _U_Qfmax and _U_Qfabs since soft-fp in
10505      glibc doesn't have them.  */
10506 }
10507
10508 /* Use soft-fp.  */
10509
10510 static void
10511 ia64_soft_fp_init_libfuncs (void)
10512 {
10513 }
10514
10515 static bool
10516 ia64_vms_valid_pointer_mode (enum machine_mode mode)
10517 {
10518   return (mode == SImode || mode == DImode);
10519 }
10520 \f
10521 /* For HPUX, it is illegal to have relocations in shared segments.  */
10522
10523 static int
10524 ia64_hpux_reloc_rw_mask (void)
10525 {
10526   return 3;
10527 }
10528
10529 /* For others, relax this so that relocations to local data goes in
10530    read-only segments, but we still cannot allow global relocations
10531    in read-only segments.  */
10532
10533 static int
10534 ia64_reloc_rw_mask (void)
10535 {
10536   return flag_pic ? 3 : 2;
10537 }
10538
10539 /* Return the section to use for X.  The only special thing we do here
10540    is to honor small data.  */
10541
10542 static section *
10543 ia64_select_rtx_section (enum machine_mode mode, rtx x,
10544                          unsigned HOST_WIDE_INT align)
10545 {
10546   if (GET_MODE_SIZE (mode) > 0
10547       && GET_MODE_SIZE (mode) <= ia64_section_threshold
10548       && !TARGET_NO_SDATA)
10549     return sdata_section;
10550   else
10551     return default_elf_select_rtx_section (mode, x, align);
10552 }
10553
10554 static unsigned int
10555 ia64_section_type_flags (tree decl, const char *name, int reloc)
10556 {
10557   unsigned int flags = 0;
10558
10559   if (strcmp (name, ".sdata") == 0
10560       || strncmp (name, ".sdata.", 7) == 0
10561       || strncmp (name, ".gnu.linkonce.s.", 16) == 0
10562       || strncmp (name, ".sdata2.", 8) == 0
10563       || strncmp (name, ".gnu.linkonce.s2.", 17) == 0
10564       || strcmp (name, ".sbss") == 0
10565       || strncmp (name, ".sbss.", 6) == 0
10566       || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
10567     flags = SECTION_SMALL;
10568
10569 #if TARGET_ABI_OPEN_VMS
10570   if (decl && DECL_ATTRIBUTES (decl)
10571       && lookup_attribute ("common_object", DECL_ATTRIBUTES (decl)))
10572     flags |= SECTION_VMS_OVERLAY;
10573 #endif
10574
10575   flags |= default_section_type_flags (decl, name, reloc);
10576   return flags;
10577 }
10578
10579 /* Returns true if FNTYPE (a FUNCTION_TYPE or a METHOD_TYPE) returns a
10580    structure type and that the address of that type should be passed
10581    in out0, rather than in r8.  */
10582
10583 static bool
10584 ia64_struct_retval_addr_is_first_parm_p (tree fntype)
10585 {
10586   tree ret_type = TREE_TYPE (fntype);
10587
10588   /* The Itanium C++ ABI requires that out0, rather than r8, be used
10589      as the structure return address parameter, if the return value
10590      type has a non-trivial copy constructor or destructor.  It is not
10591      clear if this same convention should be used for other
10592      programming languages.  Until G++ 3.4, we incorrectly used r8 for
10593      these return values.  */
10594   return (abi_version_at_least (2)
10595           && ret_type
10596           && TYPE_MODE (ret_type) == BLKmode 
10597           && TREE_ADDRESSABLE (ret_type)
10598           && strcmp (lang_hooks.name, "GNU C++") == 0);
10599 }
10600
10601 /* Output the assembler code for a thunk function.  THUNK_DECL is the
10602    declaration for the thunk function itself, FUNCTION is the decl for
10603    the target function.  DELTA is an immediate constant offset to be
10604    added to THIS.  If VCALL_OFFSET is nonzero, the word at
10605    *(*this + vcall_offset) should be added to THIS.  */
10606
10607 static void
10608 ia64_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
10609                       HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
10610                       tree function)
10611 {
10612   rtx this_rtx, insn, funexp;
10613   unsigned int this_parmno;
10614   unsigned int this_regno;
10615   rtx delta_rtx;
10616
10617   reload_completed = 1;
10618   epilogue_completed = 1;
10619
10620   /* Set things up as ia64_expand_prologue might.  */
10621   last_scratch_gr_reg = 15;
10622
10623   memset (&current_frame_info, 0, sizeof (current_frame_info));
10624   current_frame_info.spill_cfa_off = -16;
10625   current_frame_info.n_input_regs = 1;
10626   current_frame_info.need_regstk = (TARGET_REG_NAMES != 0);
10627
10628   /* Mark the end of the (empty) prologue.  */
10629   emit_note (NOTE_INSN_PROLOGUE_END);
10630
10631   /* Figure out whether "this" will be the first parameter (the
10632      typical case) or the second parameter (as happens when the
10633      virtual function returns certain class objects).  */
10634   this_parmno
10635     = (ia64_struct_retval_addr_is_first_parm_p (TREE_TYPE (thunk))
10636        ? 1 : 0);
10637   this_regno = IN_REG (this_parmno);
10638   if (!TARGET_REG_NAMES)
10639     reg_names[this_regno] = ia64_reg_numbers[this_parmno];
10640
10641   this_rtx = gen_rtx_REG (Pmode, this_regno);
10642
10643   /* Apply the constant offset, if required.  */
10644   delta_rtx = GEN_INT (delta);
10645   if (TARGET_ILP32)
10646     {
10647       rtx tmp = gen_rtx_REG (ptr_mode, this_regno);
10648       REG_POINTER (tmp) = 1;
10649       if (delta && satisfies_constraint_I (delta_rtx))
10650         {
10651           emit_insn (gen_ptr_extend_plus_imm (this_rtx, tmp, delta_rtx));
10652           delta = 0;
10653         }
10654       else
10655         emit_insn (gen_ptr_extend (this_rtx, tmp));
10656     }
10657   if (delta)
10658     {
10659       if (!satisfies_constraint_I (delta_rtx))
10660         {
10661           rtx tmp = gen_rtx_REG (Pmode, 2);
10662           emit_move_insn (tmp, delta_rtx);
10663           delta_rtx = tmp;
10664         }
10665       emit_insn (gen_adddi3 (this_rtx, this_rtx, delta_rtx));
10666     }
10667
10668   /* Apply the offset from the vtable, if required.  */
10669   if (vcall_offset)
10670     {
10671       rtx vcall_offset_rtx = GEN_INT (vcall_offset);
10672       rtx tmp = gen_rtx_REG (Pmode, 2);
10673
10674       if (TARGET_ILP32)
10675         {
10676           rtx t = gen_rtx_REG (ptr_mode, 2);
10677           REG_POINTER (t) = 1;
10678           emit_move_insn (t, gen_rtx_MEM (ptr_mode, this_rtx));
10679           if (satisfies_constraint_I (vcall_offset_rtx))
10680             {
10681               emit_insn (gen_ptr_extend_plus_imm (tmp, t, vcall_offset_rtx));
10682               vcall_offset = 0;
10683             }
10684           else
10685             emit_insn (gen_ptr_extend (tmp, t));
10686         }
10687       else
10688         emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
10689
10690       if (vcall_offset)
10691         {
10692           if (!satisfies_constraint_J (vcall_offset_rtx))
10693             {
10694               rtx tmp2 = gen_rtx_REG (Pmode, next_scratch_gr_reg ());
10695               emit_move_insn (tmp2, vcall_offset_rtx);
10696               vcall_offset_rtx = tmp2;
10697             }
10698           emit_insn (gen_adddi3 (tmp, tmp, vcall_offset_rtx));
10699         }
10700
10701       if (TARGET_ILP32)
10702         emit_insn (gen_zero_extendsidi2 (tmp, gen_rtx_MEM (ptr_mode, tmp)));
10703       else
10704         emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
10705
10706       emit_insn (gen_adddi3 (this_rtx, this_rtx, tmp));
10707     }
10708
10709   /* Generate a tail call to the target function.  */
10710   if (! TREE_USED (function))
10711     {
10712       assemble_external (function);
10713       TREE_USED (function) = 1;
10714     }
10715   funexp = XEXP (DECL_RTL (function), 0);
10716   funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
10717   ia64_expand_call (NULL_RTX, funexp, NULL_RTX, 1);
10718   insn = get_last_insn ();
10719   SIBLING_CALL_P (insn) = 1;
10720
10721   /* Code generation for calls relies on splitting.  */
10722   reload_completed = 1;
10723   epilogue_completed = 1;
10724   try_split (PATTERN (insn), insn, 0);
10725
10726   emit_barrier ();
10727
10728   /* Run just enough of rest_of_compilation to get the insns emitted.
10729      There's not really enough bulk here to make other passes such as
10730      instruction scheduling worth while.  Note that use_thunk calls
10731      assemble_start_function and assemble_end_function.  */
10732
10733   insn_locators_alloc ();
10734   emit_all_insn_group_barriers (NULL);
10735   insn = get_insns ();
10736   shorten_branches (insn);
10737   final_start_function (insn, file, 1);
10738   final (insn, file, 1);
10739   final_end_function ();
10740
10741   reload_completed = 0;
10742   epilogue_completed = 0;
10743 }
10744
10745 /* Worker function for TARGET_STRUCT_VALUE_RTX.  */
10746
10747 static rtx
10748 ia64_struct_value_rtx (tree fntype,
10749                        int incoming ATTRIBUTE_UNUSED)
10750 {
10751   if (TARGET_ABI_OPEN_VMS ||
10752       (fntype && ia64_struct_retval_addr_is_first_parm_p (fntype)))
10753     return NULL_RTX;
10754   return gen_rtx_REG (Pmode, GR_REG (8));
10755 }
10756
10757 static bool
10758 ia64_scalar_mode_supported_p (enum machine_mode mode)
10759 {
10760   switch (mode)
10761     {
10762     case QImode:
10763     case HImode:
10764     case SImode:
10765     case DImode:
10766     case TImode:
10767       return true;
10768
10769     case SFmode:
10770     case DFmode:
10771     case XFmode:
10772     case RFmode:
10773       return true;
10774
10775     case TFmode:
10776       return true;
10777
10778     default:
10779       return false;
10780     }
10781 }
10782
10783 static bool
10784 ia64_vector_mode_supported_p (enum machine_mode mode)
10785 {
10786   switch (mode)
10787     {
10788     case V8QImode:
10789     case V4HImode:
10790     case V2SImode:
10791       return true;
10792
10793     case V2SFmode:
10794       return true;
10795
10796     default:
10797       return false;
10798     }
10799 }
10800
10801 /* Implement the FUNCTION_PROFILER macro.  */
10802
10803 void
10804 ia64_output_function_profiler (FILE *file, int labelno)
10805 {
10806   bool indirect_call;
10807
10808   /* If the function needs a static chain and the static chain
10809      register is r15, we use an indirect call so as to bypass
10810      the PLT stub in case the executable is dynamically linked,
10811      because the stub clobbers r15 as per 5.3.6 of the psABI.
10812      We don't need to do that in non canonical PIC mode.  */
10813
10814   if (cfun->static_chain_decl && !TARGET_NO_PIC && !TARGET_AUTO_PIC)
10815     {
10816       gcc_assert (STATIC_CHAIN_REGNUM == 15);
10817       indirect_call = true;
10818     }
10819   else
10820     indirect_call = false;
10821
10822   if (TARGET_GNU_AS)
10823     fputs ("\t.prologue 4, r40\n", file);
10824   else
10825     fputs ("\t.prologue\n\t.save ar.pfs, r40\n", file);
10826   fputs ("\talloc out0 = ar.pfs, 8, 0, 4, 0\n", file);
10827
10828   if (NO_PROFILE_COUNTERS)
10829     fputs ("\tmov out3 = r0\n", file);
10830   else
10831     {
10832       char buf[20];
10833       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
10834
10835       if (TARGET_AUTO_PIC)
10836         fputs ("\tmovl out3 = @gprel(", file);
10837       else
10838         fputs ("\taddl out3 = @ltoff(", file);
10839       assemble_name (file, buf);
10840       if (TARGET_AUTO_PIC)
10841         fputs (")\n", file);
10842       else
10843         fputs ("), r1\n", file);
10844     }
10845
10846   if (indirect_call)
10847     fputs ("\taddl r14 = @ltoff(@fptr(_mcount)), r1\n", file);
10848   fputs ("\t;;\n", file);
10849
10850   fputs ("\t.save rp, r42\n", file);
10851   fputs ("\tmov out2 = b0\n", file);
10852   if (indirect_call)
10853     fputs ("\tld8 r14 = [r14]\n\t;;\n", file);
10854   fputs ("\t.body\n", file);
10855   fputs ("\tmov out1 = r1\n", file);
10856   if (indirect_call)
10857     {
10858       fputs ("\tld8 r16 = [r14], 8\n\t;;\n", file);
10859       fputs ("\tmov b6 = r16\n", file);
10860       fputs ("\tld8 r1 = [r14]\n", file);
10861       fputs ("\tbr.call.sptk.many b0 = b6\n\t;;\n", file);
10862     }
10863   else
10864     fputs ("\tbr.call.sptk.many b0 = _mcount\n\t;;\n", file);
10865 }
10866
10867 static GTY(()) rtx mcount_func_rtx;
10868 static rtx
10869 gen_mcount_func_rtx (void)
10870 {
10871   if (!mcount_func_rtx)
10872     mcount_func_rtx = init_one_libfunc ("_mcount");
10873   return mcount_func_rtx;
10874 }
10875
10876 void
10877 ia64_profile_hook (int labelno)
10878 {
10879   rtx label, ip;
10880
10881   if (NO_PROFILE_COUNTERS)
10882     label = const0_rtx;
10883   else
10884     {
10885       char buf[30];
10886       const char *label_name;
10887       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
10888       label_name = (*targetm.strip_name_encoding) (ggc_strdup (buf));
10889       label = gen_rtx_SYMBOL_REF (Pmode, label_name);
10890       SYMBOL_REF_FLAGS (label) = SYMBOL_FLAG_LOCAL;
10891     }
10892   ip = gen_reg_rtx (Pmode);
10893   emit_insn (gen_ip_value (ip));
10894   emit_library_call (gen_mcount_func_rtx (), LCT_NORMAL,
10895                      VOIDmode, 3,
10896                      gen_rtx_REG (Pmode, BR_REG (0)), Pmode,
10897                      ip, Pmode,
10898                      label, Pmode);
10899 }
10900
10901 /* Return the mangling of TYPE if it is an extended fundamental type.  */
10902
10903 static const char *
10904 ia64_mangle_type (const_tree type)
10905 {
10906   type = TYPE_MAIN_VARIANT (type);
10907
10908   if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
10909       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10910     return NULL;
10911
10912   /* On HP-UX, "long double" is mangled as "e" so __float128 is
10913      mangled as "e".  */
10914   if (!TARGET_HPUX && TYPE_MODE (type) == TFmode)
10915     return "g";
10916   /* On HP-UX, "e" is not available as a mangling of __float80 so use
10917      an extended mangling.  Elsewhere, "e" is available since long
10918      double is 80 bits.  */
10919   if (TYPE_MODE (type) == XFmode)
10920     return TARGET_HPUX ? "u9__float80" : "e";
10921   if (TYPE_MODE (type) == RFmode)
10922     return "u7__fpreg";
10923   return NULL;
10924 }
10925
10926 /* Return the diagnostic message string if conversion from FROMTYPE to
10927    TOTYPE is not allowed, NULL otherwise.  */
10928 static const char *
10929 ia64_invalid_conversion (const_tree fromtype, const_tree totype)
10930 {
10931   /* Reject nontrivial conversion to or from __fpreg.  */
10932   if (TYPE_MODE (fromtype) == RFmode
10933       && TYPE_MODE (totype) != RFmode
10934       && TYPE_MODE (totype) != VOIDmode)
10935     return N_("invalid conversion from %<__fpreg%>");
10936   if (TYPE_MODE (totype) == RFmode
10937       && TYPE_MODE (fromtype) != RFmode)
10938     return N_("invalid conversion to %<__fpreg%>");
10939   return NULL;
10940 }
10941
10942 /* Return the diagnostic message string if the unary operation OP is
10943    not permitted on TYPE, NULL otherwise.  */
10944 static const char *
10945 ia64_invalid_unary_op (int op, const_tree type)
10946 {
10947   /* Reject operations on __fpreg other than unary + or &.  */
10948   if (TYPE_MODE (type) == RFmode
10949       && op != CONVERT_EXPR
10950       && op != ADDR_EXPR)
10951     return N_("invalid operation on %<__fpreg%>");
10952   return NULL;
10953 }
10954
10955 /* Return the diagnostic message string if the binary operation OP is
10956    not permitted on TYPE1 and TYPE2, NULL otherwise.  */
10957 static const char *
10958 ia64_invalid_binary_op (int op ATTRIBUTE_UNUSED, const_tree type1, const_tree type2)
10959 {
10960   /* Reject operations on __fpreg.  */
10961   if (TYPE_MODE (type1) == RFmode || TYPE_MODE (type2) == RFmode)
10962     return N_("invalid operation on %<__fpreg%>");
10963   return NULL;
10964 }
10965
10966 /* Implement TARGET_OPTION_DEFAULT_PARAMS.  */
10967 static void
10968 ia64_option_default_params (void)
10969 {
10970   /* Let the scheduler form additional regions.  */
10971   set_default_param_value (PARAM_MAX_SCHED_EXTEND_REGIONS_ITERS, 2);
10972
10973   /* Set the default values for cache-related parameters.  */
10974   set_default_param_value (PARAM_SIMULTANEOUS_PREFETCHES, 6);
10975   set_default_param_value (PARAM_L1_CACHE_LINE_SIZE, 32);
10976
10977   set_default_param_value (PARAM_SCHED_MEM_TRUE_DEP_COST, 4);
10978 }
10979
10980 /* HP-UX version_id attribute.
10981    For object foo, if the version_id is set to 1234 put out an alias
10982    of '.alias foo "foo{1234}"  We can't use "foo{1234}" in anything
10983    other than an alias statement because it is an illegal symbol name.  */
10984
10985 static tree
10986 ia64_handle_version_id_attribute (tree *node ATTRIBUTE_UNUSED,
10987                                  tree name ATTRIBUTE_UNUSED,
10988                                  tree args,
10989                                  int flags ATTRIBUTE_UNUSED,
10990                                  bool *no_add_attrs)
10991 {
10992   tree arg = TREE_VALUE (args);
10993
10994   if (TREE_CODE (arg) != STRING_CST)
10995     {
10996       error("version attribute is not a string");
10997       *no_add_attrs = true;
10998       return NULL_TREE;
10999     }
11000   return NULL_TREE;
11001 }
11002
11003 /* Target hook for c_mode_for_suffix.  */
11004
11005 static enum machine_mode
11006 ia64_c_mode_for_suffix (char suffix)
11007 {
11008   if (suffix == 'q')
11009     return TFmode;
11010   if (suffix == 'w')
11011     return XFmode;
11012
11013   return VOIDmode;
11014 }
11015
11016 static enum machine_mode
11017 ia64_promote_function_mode (const_tree type,
11018                             enum machine_mode mode,
11019                             int *punsignedp,
11020                             const_tree funtype,
11021                             int for_return)
11022 {
11023   /* Special processing required for OpenVMS ...  */
11024
11025   if (!TARGET_ABI_OPEN_VMS)
11026     return default_promote_function_mode(type, mode, punsignedp, funtype,
11027                                          for_return);
11028
11029   /* HP OpenVMS Calling Standard dated June, 2004, that describes
11030      HP OpenVMS I64 Version 8.2EFT,
11031      chapter 4 "OpenVMS I64 Conventions"
11032      section 4.7 "Procedure Linkage"
11033      subsection 4.7.5.2, "Normal Register Parameters"
11034
11035      "Unsigned integral (except unsigned 32-bit), set, and VAX floating-point
11036      values passed in registers are zero-filled; signed integral values as
11037      well as unsigned 32-bit integral values are sign-extended to 64 bits.
11038      For all other types passed in the general registers, unused bits are
11039      undefined."  */
11040
11041   if (!AGGREGATE_TYPE_P (type)
11042       && GET_MODE_CLASS (mode) == MODE_INT
11043       && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
11044     {
11045       if (mode == SImode)
11046         *punsignedp = 0;
11047       return DImode;
11048     }
11049   else
11050     return promote_mode (type, mode, punsignedp);
11051 }
11052    
11053 static GTY(()) rtx ia64_dconst_0_5_rtx;
11054
11055 rtx
11056 ia64_dconst_0_5 (void)
11057 {
11058   if (! ia64_dconst_0_5_rtx)
11059     {
11060       REAL_VALUE_TYPE rv;
11061       real_from_string (&rv, "0.5");
11062       ia64_dconst_0_5_rtx = const_double_from_real_value (rv, DFmode);
11063     }
11064   return ia64_dconst_0_5_rtx;
11065 }
11066
11067 static GTY(()) rtx ia64_dconst_0_375_rtx;
11068
11069 rtx
11070 ia64_dconst_0_375 (void)
11071 {
11072   if (! ia64_dconst_0_375_rtx)
11073     {
11074       REAL_VALUE_TYPE rv;
11075       real_from_string (&rv, "0.375");
11076       ia64_dconst_0_375_rtx = const_double_from_real_value (rv, DFmode);
11077     }
11078   return ia64_dconst_0_375_rtx;
11079 }
11080
11081 static enum machine_mode
11082 ia64_get_reg_raw_mode (int regno)
11083 {
11084   if (FR_REGNO_P (regno))
11085     return XFmode;
11086   return default_get_reg_raw_mode(regno);
11087 }
11088
11089 /* Always default to .text section until HP-UX linker is fixed.  */
11090
11091 ATTRIBUTE_UNUSED static section *
11092 ia64_hpux_function_section (tree decl ATTRIBUTE_UNUSED,
11093                             enum node_frequency freq ATTRIBUTE_UNUSED,
11094                             bool startup ATTRIBUTE_UNUSED,
11095                             bool exit ATTRIBUTE_UNUSED)
11096 {
11097   return NULL;
11098 }
11099
11100 #include "gt-ia64.h"