OSDN Git Service

* rtl.def (ADDRESSOF): Remove.
[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
3    Free Software Foundation, Inc.
4    Contributed by James E. Wilson <wilson@cygnus.com> and
5                   David Mosberger <davidm@hpl.hp.com>.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING.  If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.  */
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 "real.h"
33 #include "insn-config.h"
34 #include "conditions.h"
35 #include "output.h"
36 #include "insn-attr.h"
37 #include "flags.h"
38 #include "recog.h"
39 #include "expr.h"
40 #include "optabs.h"
41 #include "except.h"
42 #include "function.h"
43 #include "ggc.h"
44 #include "basic-block.h"
45 #include "toplev.h"
46 #include "sched-int.h"
47 #include "timevar.h"
48 #include "target.h"
49 #include "target-def.h"
50 #include "tm_p.h"
51 #include "hashtab.h"
52 #include "langhooks.h"
53 #include "cfglayout.h"
54 #include "tree-gimple.h"
55
56 /* This is used for communication between ASM_OUTPUT_LABEL and
57    ASM_OUTPUT_LABELREF.  */
58 int ia64_asm_output_label = 0;
59
60 /* Define the information needed to generate branch and scc insns.  This is
61    stored from the compare operation.  */
62 struct rtx_def * ia64_compare_op0;
63 struct rtx_def * ia64_compare_op1;
64
65 /* Register names for ia64_expand_prologue.  */
66 static const char * const ia64_reg_numbers[96] =
67 { "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
68   "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
69   "r48", "r49", "r50", "r51", "r52", "r53", "r54", "r55",
70   "r56", "r57", "r58", "r59", "r60", "r61", "r62", "r63",
71   "r64", "r65", "r66", "r67", "r68", "r69", "r70", "r71",
72   "r72", "r73", "r74", "r75", "r76", "r77", "r78", "r79",
73   "r80", "r81", "r82", "r83", "r84", "r85", "r86", "r87",
74   "r88", "r89", "r90", "r91", "r92", "r93", "r94", "r95",
75   "r96", "r97", "r98", "r99", "r100","r101","r102","r103",
76   "r104","r105","r106","r107","r108","r109","r110","r111",
77   "r112","r113","r114","r115","r116","r117","r118","r119",
78   "r120","r121","r122","r123","r124","r125","r126","r127"};
79
80 /* ??? These strings could be shared with REGISTER_NAMES.  */
81 static const char * const ia64_input_reg_names[8] =
82 { "in0",  "in1",  "in2",  "in3",  "in4",  "in5",  "in6",  "in7" };
83
84 /* ??? These strings could be shared with REGISTER_NAMES.  */
85 static const char * const ia64_local_reg_names[80] =
86 { "loc0", "loc1", "loc2", "loc3", "loc4", "loc5", "loc6", "loc7",
87   "loc8", "loc9", "loc10","loc11","loc12","loc13","loc14","loc15",
88   "loc16","loc17","loc18","loc19","loc20","loc21","loc22","loc23",
89   "loc24","loc25","loc26","loc27","loc28","loc29","loc30","loc31",
90   "loc32","loc33","loc34","loc35","loc36","loc37","loc38","loc39",
91   "loc40","loc41","loc42","loc43","loc44","loc45","loc46","loc47",
92   "loc48","loc49","loc50","loc51","loc52","loc53","loc54","loc55",
93   "loc56","loc57","loc58","loc59","loc60","loc61","loc62","loc63",
94   "loc64","loc65","loc66","loc67","loc68","loc69","loc70","loc71",
95   "loc72","loc73","loc74","loc75","loc76","loc77","loc78","loc79" };
96
97 /* ??? These strings could be shared with REGISTER_NAMES.  */
98 static const char * const ia64_output_reg_names[8] =
99 { "out0", "out1", "out2", "out3", "out4", "out5", "out6", "out7" };
100
101 /* String used with the -mfixed-range= option.  */
102 const char *ia64_fixed_range_string;
103
104 /* Determines whether we use adds, addl, or movl to generate our
105    TLS immediate offsets.  */
106 int ia64_tls_size = 22;
107
108 /* String used with the -mtls-size= option.  */
109 const char *ia64_tls_size_string;
110
111 /* Which cpu are we scheduling for.  */
112 enum processor_type ia64_tune;
113
114 /* String used with the -tune= option.  */
115 const char *ia64_tune_string;
116
117 /* Determines whether we run our final scheduling pass or not.  We always
118    avoid the normal second scheduling pass.  */
119 static int ia64_flag_schedule_insns2;
120
121 /* Determines whether we run variable tracking in machine dependent
122    reorganization.  */
123 static int ia64_flag_var_tracking;
124
125 /* Variables which are this size or smaller are put in the sdata/sbss
126    sections.  */
127
128 unsigned int ia64_section_threshold;
129
130 /* The following variable is used by the DFA insn scheduler.  The value is
131    TRUE if we do insn bundling instead of insn scheduling.  */
132 int bundling_p = 0;
133
134 /* Structure to be filled in by ia64_compute_frame_size with register
135    save masks and offsets for the current function.  */
136
137 struct ia64_frame_info
138 {
139   HOST_WIDE_INT total_size;     /* size of the stack frame, not including
140                                    the caller's scratch area.  */
141   HOST_WIDE_INT spill_cfa_off;  /* top of the reg spill area from the cfa.  */
142   HOST_WIDE_INT spill_size;     /* size of the gr/br/fr spill area.  */
143   HOST_WIDE_INT extra_spill_size;  /* size of spill area for others.  */
144   HARD_REG_SET mask;            /* mask of saved registers.  */
145   unsigned int gr_used_mask;    /* mask of registers in use as gr spill
146                                    registers or long-term scratches.  */
147   int n_spilled;                /* number of spilled registers.  */
148   int reg_fp;                   /* register for fp.  */
149   int reg_save_b0;              /* save register for b0.  */
150   int reg_save_pr;              /* save register for prs.  */
151   int reg_save_ar_pfs;          /* save register for ar.pfs.  */
152   int reg_save_ar_unat;         /* save register for ar.unat.  */
153   int reg_save_ar_lc;           /* save register for ar.lc.  */
154   int reg_save_gp;              /* save register for gp.  */
155   int n_input_regs;             /* number of input registers used.  */
156   int n_local_regs;             /* number of local registers used.  */
157   int n_output_regs;            /* number of output registers used.  */
158   int n_rotate_regs;            /* number of rotating registers used.  */
159
160   char need_regstk;             /* true if a .regstk directive needed.  */
161   char initialized;             /* true if the data is finalized.  */
162 };
163
164 /* Current frame information calculated by ia64_compute_frame_size.  */
165 static struct ia64_frame_info current_frame_info;
166 \f
167 static int ia64_use_dfa_pipeline_interface (void);
168 static int ia64_first_cycle_multipass_dfa_lookahead (void);
169 static void ia64_dependencies_evaluation_hook (rtx, rtx);
170 static void ia64_init_dfa_pre_cycle_insn (void);
171 static rtx ia64_dfa_pre_cycle_insn (void);
172 static int ia64_first_cycle_multipass_dfa_lookahead_guard (rtx);
173 static int ia64_dfa_new_cycle (FILE *, int, rtx, int, int, int *);
174 static rtx gen_tls_get_addr (void);
175 static rtx gen_thread_pointer (void);
176 static rtx ia64_expand_tls_address (enum tls_model, rtx, rtx);
177 static int find_gr_spill (int);
178 static int next_scratch_gr_reg (void);
179 static void mark_reg_gr_used_mask (rtx, void *);
180 static void ia64_compute_frame_size (HOST_WIDE_INT);
181 static void setup_spill_pointers (int, rtx, HOST_WIDE_INT);
182 static void finish_spill_pointers (void);
183 static rtx spill_restore_mem (rtx, HOST_WIDE_INT);
184 static void do_spill (rtx (*)(rtx, rtx, rtx), rtx, HOST_WIDE_INT, rtx);
185 static void do_restore (rtx (*)(rtx, rtx, rtx), rtx, HOST_WIDE_INT);
186 static rtx gen_movdi_x (rtx, rtx, rtx);
187 static rtx gen_fr_spill_x (rtx, rtx, rtx);
188 static rtx gen_fr_restore_x (rtx, rtx, rtx);
189
190 static enum machine_mode hfa_element_mode (tree, int);
191 static void ia64_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
192                                          tree, int *, int);
193 static bool ia64_function_ok_for_sibcall (tree, tree);
194 static bool ia64_return_in_memory (tree, tree);
195 static bool ia64_rtx_costs (rtx, int, int, int *);
196 static void fix_range (const char *);
197 static struct machine_function * ia64_init_machine_status (void);
198 static void emit_insn_group_barriers (FILE *);
199 static void emit_all_insn_group_barriers (FILE *);
200 static void final_emit_insn_group_barriers (FILE *);
201 static void emit_predicate_relation_info (void);
202 static void ia64_reorg (void);
203 static bool ia64_in_small_data_p (tree);
204 static void process_epilogue (void);
205 static int process_set (FILE *, rtx);
206
207 static rtx ia64_expand_fetch_and_op (optab, enum machine_mode, tree, rtx);
208 static rtx ia64_expand_op_and_fetch (optab, enum machine_mode, tree, rtx);
209 static rtx ia64_expand_compare_and_swap (enum machine_mode, enum machine_mode,
210                                          int, tree, rtx);
211 static rtx ia64_expand_lock_test_and_set (enum machine_mode, tree, rtx);
212 static rtx ia64_expand_lock_release (enum machine_mode, tree, rtx);
213 static bool ia64_assemble_integer (rtx, unsigned int, int);
214 static void ia64_output_function_prologue (FILE *, HOST_WIDE_INT);
215 static void ia64_output_function_epilogue (FILE *, HOST_WIDE_INT);
216 static void ia64_output_function_end_prologue (FILE *);
217
218 static int ia64_issue_rate (void);
219 static int ia64_adjust_cost (rtx, rtx, rtx, int);
220 static void ia64_sched_init (FILE *, int, int);
221 static void ia64_sched_finish (FILE *, int);
222 static int ia64_dfa_sched_reorder (FILE *, int, rtx *, int *, int, int);
223 static int ia64_sched_reorder (FILE *, int, rtx *, int *, int);
224 static int ia64_sched_reorder2 (FILE *, int, rtx *, int *, int);
225 static int ia64_variable_issue (FILE *, int, rtx, int);
226
227 static struct bundle_state *get_free_bundle_state (void);
228 static void free_bundle_state (struct bundle_state *);
229 static void initiate_bundle_states (void);
230 static void finish_bundle_states (void);
231 static unsigned bundle_state_hash (const void *);
232 static int bundle_state_eq_p (const void *, const void *);
233 static int insert_bundle_state (struct bundle_state *);
234 static void initiate_bundle_state_table (void);
235 static void finish_bundle_state_table (void);
236 static int try_issue_nops (struct bundle_state *, int);
237 static int try_issue_insn (struct bundle_state *, rtx);
238 static void issue_nops_and_insn (struct bundle_state *, int, rtx, int, int);
239 static int get_max_pos (state_t);
240 static int get_template (state_t, int);
241
242 static rtx get_next_important_insn (rtx, rtx);
243 static void bundling (FILE *, int, rtx, rtx);
244
245 static void ia64_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
246                                   HOST_WIDE_INT, tree);
247 static void ia64_file_start (void);
248
249 static void ia64_select_rtx_section (enum machine_mode, rtx,
250                                      unsigned HOST_WIDE_INT);
251 static void ia64_rwreloc_select_section (tree, int, unsigned HOST_WIDE_INT)
252      ATTRIBUTE_UNUSED;
253 static void ia64_rwreloc_unique_section (tree, int)
254      ATTRIBUTE_UNUSED;
255 static void ia64_rwreloc_select_rtx_section (enum machine_mode, rtx,
256                                              unsigned HOST_WIDE_INT)
257      ATTRIBUTE_UNUSED;
258 static unsigned int ia64_rwreloc_section_type_flags (tree, const char *, int)
259      ATTRIBUTE_UNUSED;
260
261 static void ia64_hpux_add_extern_decl (tree decl)
262      ATTRIBUTE_UNUSED;
263 static void ia64_hpux_file_end (void)
264      ATTRIBUTE_UNUSED;
265 static void ia64_init_libfuncs (void)
266      ATTRIBUTE_UNUSED;
267 static void ia64_hpux_init_libfuncs (void)
268      ATTRIBUTE_UNUSED;
269 static void ia64_sysv4_init_libfuncs (void)
270      ATTRIBUTE_UNUSED;
271 static void ia64_vms_init_libfuncs (void)
272      ATTRIBUTE_UNUSED;
273
274 static tree ia64_handle_model_attribute (tree *, tree, tree, int, bool *);
275 static void ia64_encode_section_info (tree, rtx, int);
276 static rtx ia64_struct_value_rtx (tree, int);
277 static tree ia64_gimplify_va_arg (tree, tree, tree *, tree *);
278
279 \f
280 /* Table of valid machine attributes.  */
281 static const struct attribute_spec ia64_attribute_table[] =
282 {
283   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
284   { "syscall_linkage", 0, 0, false, true,  true,  NULL },
285   { "model",           1, 1, true, false, false, ia64_handle_model_attribute },
286   { NULL,              0, 0, false, false, false, NULL }
287 };
288
289 /* Initialize the GCC target structure.  */
290 #undef TARGET_ATTRIBUTE_TABLE
291 #define TARGET_ATTRIBUTE_TABLE ia64_attribute_table
292
293 #undef TARGET_INIT_BUILTINS
294 #define TARGET_INIT_BUILTINS ia64_init_builtins
295
296 #undef TARGET_EXPAND_BUILTIN
297 #define TARGET_EXPAND_BUILTIN ia64_expand_builtin
298
299 #undef TARGET_ASM_BYTE_OP
300 #define TARGET_ASM_BYTE_OP "\tdata1\t"
301 #undef TARGET_ASM_ALIGNED_HI_OP
302 #define TARGET_ASM_ALIGNED_HI_OP "\tdata2\t"
303 #undef TARGET_ASM_ALIGNED_SI_OP
304 #define TARGET_ASM_ALIGNED_SI_OP "\tdata4\t"
305 #undef TARGET_ASM_ALIGNED_DI_OP
306 #define TARGET_ASM_ALIGNED_DI_OP "\tdata8\t"
307 #undef TARGET_ASM_UNALIGNED_HI_OP
308 #define TARGET_ASM_UNALIGNED_HI_OP "\tdata2.ua\t"
309 #undef TARGET_ASM_UNALIGNED_SI_OP
310 #define TARGET_ASM_UNALIGNED_SI_OP "\tdata4.ua\t"
311 #undef TARGET_ASM_UNALIGNED_DI_OP
312 #define TARGET_ASM_UNALIGNED_DI_OP "\tdata8.ua\t"
313 #undef TARGET_ASM_INTEGER
314 #define TARGET_ASM_INTEGER ia64_assemble_integer
315
316 #undef TARGET_ASM_FUNCTION_PROLOGUE
317 #define TARGET_ASM_FUNCTION_PROLOGUE ia64_output_function_prologue
318 #undef TARGET_ASM_FUNCTION_END_PROLOGUE
319 #define TARGET_ASM_FUNCTION_END_PROLOGUE ia64_output_function_end_prologue
320 #undef TARGET_ASM_FUNCTION_EPILOGUE
321 #define TARGET_ASM_FUNCTION_EPILOGUE ia64_output_function_epilogue
322
323 #undef TARGET_IN_SMALL_DATA_P
324 #define TARGET_IN_SMALL_DATA_P  ia64_in_small_data_p
325
326 #undef TARGET_SCHED_ADJUST_COST
327 #define TARGET_SCHED_ADJUST_COST ia64_adjust_cost
328 #undef TARGET_SCHED_ISSUE_RATE
329 #define TARGET_SCHED_ISSUE_RATE ia64_issue_rate
330 #undef TARGET_SCHED_VARIABLE_ISSUE
331 #define TARGET_SCHED_VARIABLE_ISSUE ia64_variable_issue
332 #undef TARGET_SCHED_INIT
333 #define TARGET_SCHED_INIT ia64_sched_init
334 #undef TARGET_SCHED_FINISH
335 #define TARGET_SCHED_FINISH ia64_sched_finish
336 #undef TARGET_SCHED_REORDER
337 #define TARGET_SCHED_REORDER ia64_sched_reorder
338 #undef TARGET_SCHED_REORDER2
339 #define TARGET_SCHED_REORDER2 ia64_sched_reorder2
340
341 #undef TARGET_SCHED_DEPENDENCIES_EVALUATION_HOOK
342 #define TARGET_SCHED_DEPENDENCIES_EVALUATION_HOOK ia64_dependencies_evaluation_hook
343
344 #undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE
345 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE ia64_use_dfa_pipeline_interface
346
347 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
348 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD ia64_first_cycle_multipass_dfa_lookahead
349
350 #undef TARGET_SCHED_INIT_DFA_PRE_CYCLE_INSN
351 #define TARGET_SCHED_INIT_DFA_PRE_CYCLE_INSN ia64_init_dfa_pre_cycle_insn
352 #undef TARGET_SCHED_DFA_PRE_CYCLE_INSN
353 #define TARGET_SCHED_DFA_PRE_CYCLE_INSN ia64_dfa_pre_cycle_insn
354
355 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
356 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD\
357   ia64_first_cycle_multipass_dfa_lookahead_guard
358
359 #undef TARGET_SCHED_DFA_NEW_CYCLE
360 #define TARGET_SCHED_DFA_NEW_CYCLE ia64_dfa_new_cycle
361
362 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
363 #define TARGET_FUNCTION_OK_FOR_SIBCALL ia64_function_ok_for_sibcall
364
365 #undef TARGET_ASM_OUTPUT_MI_THUNK
366 #define TARGET_ASM_OUTPUT_MI_THUNK ia64_output_mi_thunk
367 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
368 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
369
370 #undef TARGET_ASM_FILE_START
371 #define TARGET_ASM_FILE_START ia64_file_start
372
373 #undef TARGET_RTX_COSTS
374 #define TARGET_RTX_COSTS ia64_rtx_costs
375 #undef TARGET_ADDRESS_COST
376 #define TARGET_ADDRESS_COST hook_int_rtx_0
377
378 #undef TARGET_MACHINE_DEPENDENT_REORG
379 #define TARGET_MACHINE_DEPENDENT_REORG ia64_reorg
380
381 #undef TARGET_ENCODE_SECTION_INFO
382 #define TARGET_ENCODE_SECTION_INFO ia64_encode_section_info
383
384 /* ??? ABI doesn't allow us to define this.  */
385 #if 0
386 #undef TARGET_PROMOTE_FUNCTION_ARGS
387 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
388 #endif
389
390 /* ??? ABI doesn't allow us to define this.  */
391 #if 0
392 #undef TARGET_PROMOTE_FUNCTION_RETURN
393 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
394 #endif
395
396 /* ??? Investigate.  */
397 #if 0
398 #undef TARGET_PROMOTE_PROTOTYPES
399 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
400 #endif
401
402 #undef TARGET_STRUCT_VALUE_RTX
403 #define TARGET_STRUCT_VALUE_RTX ia64_struct_value_rtx
404 #undef TARGET_RETURN_IN_MEMORY
405 #define TARGET_RETURN_IN_MEMORY ia64_return_in_memory
406
407 #undef TARGET_SETUP_INCOMING_VARARGS
408 #define TARGET_SETUP_INCOMING_VARARGS ia64_setup_incoming_varargs
409 #undef TARGET_STRICT_ARGUMENT_NAMING
410 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
411
412 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
413 #define TARGET_GIMPLIFY_VA_ARG_EXPR ia64_gimplify_va_arg
414
415 struct gcc_target targetm = TARGET_INITIALIZER;
416 \f
417 /* Return 1 if OP is a valid operand for the MEM of a CALL insn.  */
418
419 int
420 call_operand (rtx op, enum machine_mode mode)
421 {
422   if (mode != GET_MODE (op) && mode != VOIDmode)
423     return 0;
424
425   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == REG
426           || (GET_CODE (op) == SUBREG && GET_CODE (XEXP (op, 0)) == REG));
427 }
428
429 /* Return 1 if OP refers to a symbol in the sdata section.  */
430
431 int
432 sdata_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
433 {
434   switch (GET_CODE (op))
435     {
436     case CONST:
437       if (GET_CODE (XEXP (op, 0)) != PLUS
438           || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF)
439         break;
440       op = XEXP (XEXP (op, 0), 0);
441       /* FALLTHRU */
442
443     case SYMBOL_REF:
444       if (CONSTANT_POOL_ADDRESS_P (op))
445         return GET_MODE_SIZE (get_pool_mode (op)) <= ia64_section_threshold;
446       else
447         return SYMBOL_REF_LOCAL_P (op) && SYMBOL_REF_SMALL_P (op);
448
449     default:
450       break;
451     }
452
453   return 0;
454 }
455
456 int
457 small_addr_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
458 {
459   return SYMBOL_REF_SMALL_ADDR_P (op);
460 }
461
462 /* Return 1 if OP refers to a symbol, and is appropriate for a GOT load.  */
463
464 int
465 got_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
466 {
467   switch (GET_CODE (op))
468     {
469     case CONST:
470       op = XEXP (op, 0);
471       if (GET_CODE (op) != PLUS)
472         return 0;
473       if (GET_CODE (XEXP (op, 0)) != SYMBOL_REF)
474         return 0;
475       op = XEXP (op, 1);
476       if (GET_CODE (op) != CONST_INT)
477         return 0;
478
479         return 1;
480
481       /* Ok if we're not using GOT entries at all.  */
482       if (TARGET_NO_PIC || TARGET_AUTO_PIC)
483         return 1;
484
485       /* "Ok" while emitting rtl, since otherwise we won't be provided
486          with the entire offset during emission, which makes it very
487          hard to split the offset into high and low parts.  */
488       if (rtx_equal_function_value_matters)
489         return 1;
490
491       /* Force the low 14 bits of the constant to zero so that we do not
492          use up so many GOT entries.  */
493       return (INTVAL (op) & 0x3fff) == 0;
494
495     case SYMBOL_REF:
496       if (SYMBOL_REF_SMALL_ADDR_P (op))
497         return 0;
498     case LABEL_REF:
499       return 1;
500
501     default:
502       break;
503     }
504   return 0;
505 }
506
507 /* Return 1 if OP refers to a symbol.  */
508
509 int
510 symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
511 {
512   switch (GET_CODE (op))
513     {
514     case CONST:
515     case SYMBOL_REF:
516     case LABEL_REF:
517       return 1;
518
519     default:
520       break;
521     }
522   return 0;
523 }
524
525 /* Return tls_model if OP refers to a TLS symbol.  */
526
527 int
528 tls_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
529 {
530   if (GET_CODE (op) != SYMBOL_REF)
531     return 0;
532   return SYMBOL_REF_TLS_MODEL (op);
533 }
534
535
536 /* Return 1 if OP refers to a function.  */
537
538 int
539 function_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
540 {
541   if (GET_CODE (op) == SYMBOL_REF && SYMBOL_REF_FUNCTION_P (op))
542     return 1;
543   else
544     return 0;
545 }
546
547 /* Return 1 if OP is setjmp or a similar function.  */
548
549 /* ??? This is an unsatisfying solution.  Should rethink.  */
550
551 int
552 setjmp_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
553 {
554   const char *name;
555   int retval = 0;
556
557   if (GET_CODE (op) != SYMBOL_REF)
558     return 0;
559
560   name = XSTR (op, 0);
561
562   /* The following code is borrowed from special_function_p in calls.c.  */
563
564   /* Disregard prefix _, __ or __x.  */
565   if (name[0] == '_')
566     {
567       if (name[1] == '_' && name[2] == 'x')
568         name += 3;
569       else if (name[1] == '_')
570         name += 2;
571       else
572         name += 1;
573     }
574
575   if (name[0] == 's')
576     {
577       retval
578         = ((name[1] == 'e'
579             && (! strcmp (name, "setjmp")
580                 || ! strcmp (name, "setjmp_syscall")))
581            || (name[1] == 'i'
582                && ! strcmp (name, "sigsetjmp"))
583            || (name[1] == 'a'
584                && ! strcmp (name, "savectx")));
585     }
586   else if ((name[0] == 'q' && name[1] == 's'
587             && ! strcmp (name, "qsetjmp"))
588            || (name[0] == 'v' && name[1] == 'f'
589                && ! strcmp (name, "vfork")))
590     retval = 1;
591
592   return retval;
593 }
594
595 /* Return 1 if OP is a general operand, excluding tls symbolic operands.  */
596
597 int
598 move_operand (rtx op, enum machine_mode mode)
599 {
600   return general_operand (op, mode) && !tls_symbolic_operand (op, mode);
601 }
602
603 /* Return 1 if OP is a register operand that is (or could be) a GR reg.  */
604
605 int
606 gr_register_operand (rtx op, enum machine_mode mode)
607 {
608   if (! register_operand (op, mode))
609     return 0;
610   if (GET_CODE (op) == SUBREG)
611     op = SUBREG_REG (op);
612   if (GET_CODE (op) == REG)
613     {
614       unsigned int regno = REGNO (op);
615       if (regno < FIRST_PSEUDO_REGISTER)
616         return GENERAL_REGNO_P (regno);
617     }
618   return 1;
619 }
620
621 /* Return 1 if OP is a register operand that is (or could be) an FR reg.  */
622
623 int
624 fr_register_operand (rtx op, enum machine_mode mode)
625 {
626   if (! register_operand (op, mode))
627     return 0;
628   if (GET_CODE (op) == SUBREG)
629     op = SUBREG_REG (op);
630   if (GET_CODE (op) == REG)
631     {
632       unsigned int regno = REGNO (op);
633       if (regno < FIRST_PSEUDO_REGISTER)
634         return FR_REGNO_P (regno);
635     }
636   return 1;
637 }
638
639 /* Return 1 if OP is a register operand that is (or could be) a GR/FR reg.  */
640
641 int
642 grfr_register_operand (rtx op, enum machine_mode mode)
643 {
644   if (! register_operand (op, mode))
645     return 0;
646   if (GET_CODE (op) == SUBREG)
647     op = SUBREG_REG (op);
648   if (GET_CODE (op) == REG)
649     {
650       unsigned int regno = REGNO (op);
651       if (regno < FIRST_PSEUDO_REGISTER)
652         return GENERAL_REGNO_P (regno) || FR_REGNO_P (regno);
653     }
654   return 1;
655 }
656
657 /* Return 1 if OP is a nonimmediate operand that is (or could be) a GR reg.  */
658
659 int
660 gr_nonimmediate_operand (rtx op, enum machine_mode mode)
661 {
662   if (! nonimmediate_operand (op, mode))
663     return 0;
664   if (GET_CODE (op) == SUBREG)
665     op = SUBREG_REG (op);
666   if (GET_CODE (op) == REG)
667     {
668       unsigned int regno = REGNO (op);
669       if (regno < FIRST_PSEUDO_REGISTER)
670         return GENERAL_REGNO_P (regno);
671     }
672   return 1;
673 }
674
675 /* Return 1 if OP is a nonimmediate operand that is (or could be) a FR reg.  */
676
677 int
678 fr_nonimmediate_operand (rtx op, enum machine_mode mode)
679 {
680   if (! nonimmediate_operand (op, mode))
681     return 0;
682   if (GET_CODE (op) == SUBREG)
683     op = SUBREG_REG (op);
684   if (GET_CODE (op) == REG)
685     {
686       unsigned int regno = REGNO (op);
687       if (regno < FIRST_PSEUDO_REGISTER)
688         return FR_REGNO_P (regno);
689     }
690   return 1;
691 }
692
693 /* Return 1 if OP is a nonimmediate operand that is a GR/FR reg.  */
694
695 int
696 grfr_nonimmediate_operand (rtx op, enum machine_mode mode)
697 {
698   if (! nonimmediate_operand (op, mode))
699     return 0;
700   if (GET_CODE (op) == SUBREG)
701     op = SUBREG_REG (op);
702   if (GET_CODE (op) == REG)
703     {
704       unsigned int regno = REGNO (op);
705       if (regno < FIRST_PSEUDO_REGISTER)
706         return GENERAL_REGNO_P (regno) || FR_REGNO_P (regno);
707     }
708   return 1;
709 }
710
711 /* Return 1 if OP is a GR register operand, or zero.  */
712
713 int
714 gr_reg_or_0_operand (rtx op, enum machine_mode mode)
715 {
716   return (op == const0_rtx || gr_register_operand (op, mode));
717 }
718
719 /* Return 1 if OP is a GR register operand, or a 5 bit immediate operand.  */
720
721 int
722 gr_reg_or_5bit_operand (rtx op, enum machine_mode mode)
723 {
724   return ((GET_CODE (op) == CONST_INT && INTVAL (op) >= 0 && INTVAL (op) < 32)
725           || gr_register_operand (op, mode));
726 }
727
728 /* Return 1 if OP is a GR register operand, or a 6 bit immediate operand.  */
729
730 int
731 gr_reg_or_6bit_operand (rtx op, enum machine_mode mode)
732 {
733   return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_M (INTVAL (op)))
734           || gr_register_operand (op, mode));
735 }
736
737 /* Return 1 if OP is a GR register operand, or an 8 bit immediate operand.  */
738
739 int
740 gr_reg_or_8bit_operand (rtx op, enum machine_mode mode)
741 {
742   return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_K (INTVAL (op)))
743           || gr_register_operand (op, mode));
744 }
745
746 /* Return 1 if OP is a GR/FR register operand, or an 8 bit immediate.  */
747
748 int
749 grfr_reg_or_8bit_operand (rtx op, enum machine_mode mode)
750 {
751   return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_K (INTVAL (op)))
752           || grfr_register_operand (op, mode));
753 }
754
755 /* Return 1 if OP is a register operand, or an 8 bit adjusted immediate
756    operand.  */
757
758 int
759 gr_reg_or_8bit_adjusted_operand (rtx op, enum machine_mode mode)
760 {
761   return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_L (INTVAL (op)))
762           || gr_register_operand (op, mode));
763 }
764
765 /* Return 1 if OP is a register operand, or is valid for both an 8 bit
766    immediate and an 8 bit adjusted immediate operand.  This is necessary
767    because when we emit a compare, we don't know what the condition will be,
768    so we need the union of the immediates accepted by GT and LT.  */
769
770 int
771 gr_reg_or_8bit_and_adjusted_operand (rtx op, enum machine_mode mode)
772 {
773   return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_K (INTVAL (op))
774            && CONST_OK_FOR_L (INTVAL (op)))
775           || gr_register_operand (op, mode));
776 }
777
778 /* Return 1 if OP is a register operand, or a 14 bit immediate operand.  */
779
780 int
781 gr_reg_or_14bit_operand (rtx op, enum machine_mode mode)
782 {
783   return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_I (INTVAL (op)))
784           || gr_register_operand (op, mode));
785 }
786
787 /* Return 1 if OP is a register operand, or a 22 bit immediate operand.  */
788
789 int
790 gr_reg_or_22bit_operand (rtx op, enum machine_mode mode)
791 {
792   return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_J (INTVAL (op)))
793           || gr_register_operand (op, mode));
794 }
795
796 /* Return 1 if OP is a 6 bit immediate operand.  */
797
798 int
799 shift_count_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
800 {
801   return (GET_CODE (op) == CONST_INT && CONST_OK_FOR_M (INTVAL (op)));
802 }
803
804 /* Return 1 if OP is a 5 bit immediate operand.  */
805
806 int
807 shift_32bit_count_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
808 {
809   return (GET_CODE (op) == CONST_INT
810            && (INTVAL (op) >= 0 && INTVAL (op) < 32));
811 }
812
813 /* Return 1 if OP is a 2, 4, 8, or 16 immediate operand.  */
814
815 int
816 shladd_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
817 {
818   return (GET_CODE (op) == CONST_INT
819           && (INTVAL (op) == 2 || INTVAL (op) == 4
820               || INTVAL (op) == 8 || INTVAL (op) == 16));
821 }
822
823 /* Return 1 if OP is a -16, -8, -4, -1, 1, 4, 8, or 16 immediate operand.  */
824
825 int
826 fetchadd_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
827 {
828   return (GET_CODE (op) == CONST_INT
829           && (INTVAL (op) == -16 || INTVAL (op) == -8 ||
830               INTVAL (op) == -4  || INTVAL (op) == -1 ||
831               INTVAL (op) == 1   || INTVAL (op) == 4  ||
832               INTVAL (op) == 8   || INTVAL (op) == 16));
833 }
834
835 /* Return 1 if OP is a floating-point constant zero, one, or a register.  */
836
837 int
838 fr_reg_or_fp01_operand (rtx op, enum machine_mode mode)
839 {
840   return ((GET_CODE (op) == CONST_DOUBLE && CONST_DOUBLE_OK_FOR_G (op))
841           || fr_register_operand (op, mode));
842 }
843
844 /* Like nonimmediate_operand, but don't allow MEMs that try to use a
845    POST_MODIFY with a REG as displacement.  */
846
847 int
848 destination_operand (rtx op, enum machine_mode mode)
849 {
850   if (! nonimmediate_operand (op, mode))
851     return 0;
852   if (GET_CODE (op) == MEM
853       && GET_CODE (XEXP (op, 0)) == POST_MODIFY
854       && GET_CODE (XEXP (XEXP (XEXP (op, 0), 1), 1)) == REG)
855     return 0;
856   return 1;
857 }
858
859 /* Like memory_operand, but don't allow post-increments.  */
860
861 int
862 not_postinc_memory_operand (rtx op, enum machine_mode mode)
863 {
864   return (memory_operand (op, mode)
865           && GET_RTX_CLASS (GET_CODE (XEXP (op, 0))) != RTX_AUTOINC);
866 }
867
868 /* Return 1 if this is a comparison operator, which accepts a normal 8-bit
869    signed immediate operand.  */
870
871 int
872 normal_comparison_operator (register rtx op, enum machine_mode mode)
873 {
874   enum rtx_code code = GET_CODE (op);
875   return ((mode == VOIDmode || GET_MODE (op) == mode)
876           && (code == EQ || code == NE
877               || code == GT || code == LE || code == GTU || code == LEU));
878 }
879
880 /* Return 1 if this is a comparison operator, which accepts an adjusted 8-bit
881    signed immediate operand.  */
882
883 int
884 adjusted_comparison_operator (register rtx op, enum machine_mode mode)
885 {
886   enum rtx_code code = GET_CODE (op);
887   return ((mode == VOIDmode || GET_MODE (op) == mode)
888           && (code == LT || code == GE || code == LTU || code == GEU));
889 }
890
891 /* Return 1 if this is a signed inequality operator.  */
892
893 int
894 signed_inequality_operator (register rtx op, enum machine_mode mode)
895 {
896   enum rtx_code code = GET_CODE (op);
897   return ((mode == VOIDmode || GET_MODE (op) == mode)
898           && (code == GE || code == GT
899               || code == LE || code == LT));
900 }
901
902 /* Return 1 if this operator is valid for predication.  */
903
904 int
905 predicate_operator (register rtx op, enum machine_mode mode)
906 {
907   enum rtx_code code = GET_CODE (op);
908   return ((GET_MODE (op) == mode || mode == VOIDmode)
909           && (code == EQ || code == NE));
910 }
911
912 /* Return 1 if this operator can be used in a conditional operation.  */
913
914 int
915 condop_operator (register rtx op, enum machine_mode mode)
916 {
917   enum rtx_code code = GET_CODE (op);
918   return ((GET_MODE (op) == mode || mode == VOIDmode)
919           && (code == PLUS || code == MINUS || code == AND
920               || code == IOR || code == XOR));
921 }
922
923 /* Return 1 if this is the ar.lc register.  */
924
925 int
926 ar_lc_reg_operand (register rtx op, enum machine_mode mode)
927 {
928   return (GET_MODE (op) == DImode
929           && (mode == DImode || mode == VOIDmode)
930           && GET_CODE (op) == REG
931           && REGNO (op) == AR_LC_REGNUM);
932 }
933
934 /* Return 1 if this is the ar.ccv register.  */
935
936 int
937 ar_ccv_reg_operand (register rtx op, enum machine_mode mode)
938 {
939   return ((GET_MODE (op) == mode || mode == VOIDmode)
940           && GET_CODE (op) == REG
941           && REGNO (op) == AR_CCV_REGNUM);
942 }
943
944 /* Return 1 if this is the ar.pfs register.  */
945
946 int
947 ar_pfs_reg_operand (register rtx op, enum machine_mode mode)
948 {
949   return ((GET_MODE (op) == mode || mode == VOIDmode)
950           && GET_CODE (op) == REG
951           && REGNO (op) == AR_PFS_REGNUM);
952 }
953
954 /* Like general_operand, but don't allow (mem (addressof)).  */
955
956 int
957 general_xfmode_operand (rtx op, enum machine_mode mode)
958 {
959   if (! general_operand (op, mode))
960     return 0;
961   return 1;
962 }
963
964 /* Similarly.  */
965
966 int
967 destination_xfmode_operand (rtx op, enum machine_mode mode)
968 {
969   if (! destination_operand (op, mode))
970     return 0;
971   return 1;
972 }
973
974 /* Similarly.  */
975
976 int
977 xfreg_or_fp01_operand (rtx op, enum machine_mode mode)
978 {
979   if (GET_CODE (op) == SUBREG)
980     return 0;
981   return fr_reg_or_fp01_operand (op, mode);
982 }
983
984 /* Return 1 if OP is valid as a base register in a reg + offset address.  */
985
986 int
987 basereg_operand (rtx op, enum machine_mode mode)
988 {
989   /* ??? Should I copy the flag_omit_frame_pointer and cse_not_expected
990      checks from pa.c basereg_operand as well?  Seems to be OK without them
991      in test runs.  */
992
993   return (register_operand (op, mode) &&
994           REG_POINTER ((GET_CODE (op) == SUBREG) ? SUBREG_REG (op) : op));
995 }
996 \f
997 typedef enum
998   {
999     ADDR_AREA_NORMAL,   /* normal address area */
1000     ADDR_AREA_SMALL     /* addressable by "addl" (-2MB < addr < 2MB) */
1001   }
1002 ia64_addr_area;
1003
1004 static GTY(()) tree small_ident1;
1005 static GTY(()) tree small_ident2;
1006
1007 static void
1008 init_idents (void)
1009 {
1010   if (small_ident1 == 0)
1011     {
1012       small_ident1 = get_identifier ("small");
1013       small_ident2 = get_identifier ("__small__");
1014     }
1015 }
1016
1017 /* Retrieve the address area that has been chosen for the given decl.  */
1018
1019 static ia64_addr_area
1020 ia64_get_addr_area (tree decl)
1021 {
1022   tree model_attr;
1023
1024   model_attr = lookup_attribute ("model", DECL_ATTRIBUTES (decl));
1025   if (model_attr)
1026     {
1027       tree id;
1028
1029       init_idents ();
1030       id = TREE_VALUE (TREE_VALUE (model_attr));
1031       if (id == small_ident1 || id == small_ident2)
1032         return ADDR_AREA_SMALL;
1033     }
1034   return ADDR_AREA_NORMAL;
1035 }
1036
1037 static tree
1038 ia64_handle_model_attribute (tree *node, tree name, tree args, int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1039 {
1040   ia64_addr_area addr_area = ADDR_AREA_NORMAL;
1041   ia64_addr_area area;
1042   tree arg, decl = *node;
1043
1044   init_idents ();
1045   arg = TREE_VALUE (args);
1046   if (arg == small_ident1 || arg == small_ident2)
1047     {
1048       addr_area = ADDR_AREA_SMALL;
1049     }
1050   else
1051     {
1052       warning ("invalid argument of `%s' attribute",
1053                IDENTIFIER_POINTER (name));
1054       *no_add_attrs = true;
1055     }
1056
1057   switch (TREE_CODE (decl))
1058     {
1059     case VAR_DECL:
1060       if ((DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl))
1061            == FUNCTION_DECL)
1062           && !TREE_STATIC (decl))
1063         {
1064           error ("%Jan address area attribute cannot be specified for "
1065                  "local variables", decl, decl);
1066           *no_add_attrs = true;
1067         }
1068       area = ia64_get_addr_area (decl);
1069       if (area != ADDR_AREA_NORMAL && addr_area != area)
1070         {
1071           error ("%Jaddress area of '%s' conflicts with previous "
1072                  "declaration", decl, decl);
1073           *no_add_attrs = true;
1074         }
1075       break;
1076
1077     case FUNCTION_DECL:
1078       error ("%Jaddress area attribute cannot be specified for functions",
1079              decl, decl);
1080       *no_add_attrs = true;
1081       break;
1082
1083     default:
1084       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1085       *no_add_attrs = true;
1086       break;
1087     }
1088
1089   return NULL_TREE;
1090 }
1091
1092 static void
1093 ia64_encode_addr_area (tree decl, rtx symbol)
1094 {
1095   int flags;
1096
1097   flags = SYMBOL_REF_FLAGS (symbol);
1098   switch (ia64_get_addr_area (decl))
1099     {
1100     case ADDR_AREA_NORMAL: break;
1101     case ADDR_AREA_SMALL: flags |= SYMBOL_FLAG_SMALL_ADDR; break;
1102     default: abort ();
1103     }
1104   SYMBOL_REF_FLAGS (symbol) = flags;
1105 }
1106
1107 static void
1108 ia64_encode_section_info (tree decl, rtx rtl, int first)
1109 {
1110   default_encode_section_info (decl, rtl, first);
1111
1112   /* Careful not to prod global register variables.  */
1113   if (TREE_CODE (decl) == VAR_DECL
1114       && GET_CODE (DECL_RTL (decl)) == MEM
1115       && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF
1116       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1117     ia64_encode_addr_area (decl, XEXP (rtl, 0));
1118 }
1119 \f
1120 /* Return 1 if the operands of a move are ok.  */
1121
1122 int
1123 ia64_move_ok (rtx dst, rtx src)
1124 {
1125   /* If we're under init_recog_no_volatile, we'll not be able to use
1126      memory_operand.  So check the code directly and don't worry about
1127      the validity of the underlying address, which should have been
1128      checked elsewhere anyway.  */
1129   if (GET_CODE (dst) != MEM)
1130     return 1;
1131   if (GET_CODE (src) == MEM)
1132     return 0;
1133   if (register_operand (src, VOIDmode))
1134     return 1;
1135
1136   /* Otherwise, this must be a constant, and that either 0 or 0.0 or 1.0.  */
1137   if (INTEGRAL_MODE_P (GET_MODE (dst)))
1138     return src == const0_rtx;
1139   else
1140     return GET_CODE (src) == CONST_DOUBLE && CONST_DOUBLE_OK_FOR_G (src);
1141 }
1142
1143 int
1144 addp4_optimize_ok (rtx op1, rtx op2)
1145 {
1146   return (basereg_operand (op1, GET_MODE(op1)) !=
1147           basereg_operand (op2, GET_MODE(op2)));
1148 }
1149
1150 /* Check if OP is a mask suitable for use with SHIFT in a dep.z instruction.
1151    Return the length of the field, or <= 0 on failure.  */
1152
1153 int
1154 ia64_depz_field_mask (rtx rop, rtx rshift)
1155 {
1156   unsigned HOST_WIDE_INT op = INTVAL (rop);
1157   unsigned HOST_WIDE_INT shift = INTVAL (rshift);
1158
1159   /* Get rid of the zero bits we're shifting in.  */
1160   op >>= shift;
1161
1162   /* We must now have a solid block of 1's at bit 0.  */
1163   return exact_log2 (op + 1);
1164 }
1165
1166 /* Expand a symbolic constant load.  */
1167
1168 void
1169 ia64_expand_load_address (rtx dest, rtx src)
1170 {
1171   if (tls_symbolic_operand (src, VOIDmode))
1172     abort ();
1173   if (GET_CODE (dest) != REG)
1174     abort ();
1175
1176   /* ILP32 mode still loads 64-bits of data from the GOT.  This avoids
1177      having to pointer-extend the value afterward.  Other forms of address
1178      computation below are also more natural to compute as 64-bit quantities.
1179      If we've been given an SImode destination register, change it.  */
1180   if (GET_MODE (dest) != Pmode)
1181     dest = gen_rtx_REG (Pmode, REGNO (dest));
1182
1183   if (GET_CODE (src) == SYMBOL_REF && SYMBOL_REF_SMALL_ADDR_P (src))
1184     {
1185       emit_insn (gen_rtx_SET (VOIDmode, dest, src));
1186       return;
1187     }
1188   else if (TARGET_AUTO_PIC)
1189     {
1190       emit_insn (gen_load_gprel64 (dest, src));
1191       return;
1192     }
1193   else if (GET_CODE (src) == SYMBOL_REF && SYMBOL_REF_FUNCTION_P (src))
1194     {
1195       emit_insn (gen_load_fptr (dest, src));
1196       return;
1197     }
1198   else if (sdata_symbolic_operand (src, VOIDmode))
1199     {
1200       emit_insn (gen_load_gprel (dest, src));
1201       return;
1202     }
1203
1204   if (GET_CODE (src) == CONST
1205       && GET_CODE (XEXP (src, 0)) == PLUS
1206       && GET_CODE (XEXP (XEXP (src, 0), 1)) == CONST_INT
1207       && (INTVAL (XEXP (XEXP (src, 0), 1)) & 0x1fff) != 0)
1208     {
1209       rtx sym = XEXP (XEXP (src, 0), 0);
1210       HOST_WIDE_INT ofs, hi, lo;
1211
1212       /* Split the offset into a sign extended 14-bit low part
1213          and a complementary high part.  */
1214       ofs = INTVAL (XEXP (XEXP (src, 0), 1));
1215       lo = ((ofs & 0x3fff) ^ 0x2000) - 0x2000;
1216       hi = ofs - lo;
1217
1218       ia64_expand_load_address (dest, plus_constant (sym, hi));
1219       emit_insn (gen_adddi3 (dest, dest, GEN_INT (lo)));
1220     }
1221   else
1222     {
1223       rtx tmp;
1224
1225       tmp = gen_rtx_HIGH (Pmode, src);
1226       tmp = gen_rtx_PLUS (Pmode, tmp, pic_offset_table_rtx);
1227       emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
1228
1229       tmp = gen_rtx_LO_SUM (GET_MODE (dest), dest, src);
1230       emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
1231     }
1232 }
1233
1234 static GTY(()) rtx gen_tls_tga;
1235 static rtx
1236 gen_tls_get_addr (void)
1237 {
1238   if (!gen_tls_tga)
1239     gen_tls_tga = init_one_libfunc ("__tls_get_addr");
1240   return gen_tls_tga;
1241 }
1242
1243 static GTY(()) rtx thread_pointer_rtx;
1244 static rtx
1245 gen_thread_pointer (void)
1246 {
1247   if (!thread_pointer_rtx)
1248     {
1249       thread_pointer_rtx = gen_rtx_REG (Pmode, 13);
1250       RTX_UNCHANGING_P (thread_pointer_rtx) = 1;
1251     }
1252   return thread_pointer_rtx;
1253 }
1254
1255 static rtx
1256 ia64_expand_tls_address (enum tls_model tls_kind, rtx op0, rtx op1)
1257 {
1258   rtx tga_op1, tga_op2, tga_ret, tga_eqv, tmp, insns;
1259   rtx orig_op0 = op0;
1260
1261   switch (tls_kind)
1262     {
1263     case TLS_MODEL_GLOBAL_DYNAMIC:
1264       start_sequence ();
1265
1266       tga_op1 = gen_reg_rtx (Pmode);
1267       emit_insn (gen_load_ltoff_dtpmod (tga_op1, op1));
1268       tga_op1 = gen_rtx_MEM (Pmode, tga_op1);
1269       RTX_UNCHANGING_P (tga_op1) = 1;
1270
1271       tga_op2 = gen_reg_rtx (Pmode);
1272       emit_insn (gen_load_ltoff_dtprel (tga_op2, op1));
1273       tga_op2 = gen_rtx_MEM (Pmode, tga_op2);
1274       RTX_UNCHANGING_P (tga_op2) = 1;
1275
1276       tga_ret = emit_library_call_value (gen_tls_get_addr (), NULL_RTX,
1277                                          LCT_CONST, Pmode, 2, tga_op1,
1278                                          Pmode, tga_op2, Pmode);
1279
1280       insns = get_insns ();
1281       end_sequence ();
1282
1283       if (GET_MODE (op0) != Pmode)
1284         op0 = tga_ret;
1285       emit_libcall_block (insns, op0, tga_ret, op1);
1286       break;
1287
1288     case TLS_MODEL_LOCAL_DYNAMIC:
1289       /* ??? This isn't the completely proper way to do local-dynamic
1290          If the call to __tls_get_addr is used only by a single symbol,
1291          then we should (somehow) move the dtprel to the second arg
1292          to avoid the extra add.  */
1293       start_sequence ();
1294
1295       tga_op1 = gen_reg_rtx (Pmode);
1296       emit_insn (gen_load_ltoff_dtpmod (tga_op1, op1));
1297       tga_op1 = gen_rtx_MEM (Pmode, tga_op1);
1298       RTX_UNCHANGING_P (tga_op1) = 1;
1299
1300       tga_op2 = const0_rtx;
1301
1302       tga_ret = emit_library_call_value (gen_tls_get_addr (), NULL_RTX,
1303                                          LCT_CONST, Pmode, 2, tga_op1,
1304                                          Pmode, tga_op2, Pmode);
1305
1306       insns = get_insns ();
1307       end_sequence ();
1308
1309       tga_eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
1310                                 UNSPEC_LD_BASE);
1311       tmp = gen_reg_rtx (Pmode);
1312       emit_libcall_block (insns, tmp, tga_ret, tga_eqv);
1313
1314       if (!register_operand (op0, Pmode))
1315         op0 = gen_reg_rtx (Pmode);
1316       if (TARGET_TLS64)
1317         {
1318           emit_insn (gen_load_dtprel (op0, op1));
1319           emit_insn (gen_adddi3 (op0, tmp, op0));
1320         }
1321       else
1322         emit_insn (gen_add_dtprel (op0, tmp, op1));
1323       break;
1324
1325     case TLS_MODEL_INITIAL_EXEC:
1326       tmp = gen_reg_rtx (Pmode);
1327       emit_insn (gen_load_ltoff_tprel (tmp, op1));
1328       tmp = gen_rtx_MEM (Pmode, tmp);
1329       RTX_UNCHANGING_P (tmp) = 1;
1330       tmp = force_reg (Pmode, tmp);
1331
1332       if (!register_operand (op0, Pmode))
1333         op0 = gen_reg_rtx (Pmode);
1334       emit_insn (gen_adddi3 (op0, tmp, gen_thread_pointer ()));
1335       break;
1336
1337     case TLS_MODEL_LOCAL_EXEC:
1338       if (!register_operand (op0, Pmode))
1339         op0 = gen_reg_rtx (Pmode);
1340       if (TARGET_TLS64)
1341         {
1342           emit_insn (gen_load_tprel (op0, op1));
1343           emit_insn (gen_adddi3 (op0, gen_thread_pointer (), op0));
1344         }
1345       else
1346         emit_insn (gen_add_tprel (op0, gen_thread_pointer (), op1));
1347       break;
1348
1349     default:
1350       abort ();
1351     }
1352
1353   if (orig_op0 == op0)
1354     return NULL_RTX;
1355   if (GET_MODE (orig_op0) == Pmode)
1356     return op0;
1357   return gen_lowpart (GET_MODE (orig_op0), op0);
1358 }
1359
1360 rtx
1361 ia64_expand_move (rtx op0, rtx op1)
1362 {
1363   enum machine_mode mode = GET_MODE (op0);
1364
1365   if (!reload_in_progress && !reload_completed && !ia64_move_ok (op0, op1))
1366     op1 = force_reg (mode, op1);
1367
1368   if ((mode == Pmode || mode == ptr_mode) && symbolic_operand (op1, VOIDmode))
1369     {
1370       enum tls_model tls_kind;
1371       if ((tls_kind = tls_symbolic_operand (op1, VOIDmode)))
1372         return ia64_expand_tls_address (tls_kind, op0, op1);
1373
1374       if (!TARGET_NO_PIC && reload_completed)
1375         {
1376           ia64_expand_load_address (op0, op1);
1377           return NULL_RTX;
1378         }
1379     }
1380
1381   return op1;
1382 }
1383
1384 /* Split a move from OP1 to OP0 conditional on COND.  */
1385
1386 void
1387 ia64_emit_cond_move (rtx op0, rtx op1, rtx cond)
1388 {
1389   rtx insn, first = get_last_insn ();
1390
1391   emit_move_insn (op0, op1);
1392
1393   for (insn = get_last_insn (); insn != first; insn = PREV_INSN (insn))
1394     if (INSN_P (insn))
1395       PATTERN (insn) = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond),
1396                                           PATTERN (insn));
1397 }
1398
1399 /* Split a post-reload TImode or TFmode reference into two DImode
1400    components.  This is made extra difficult by the fact that we do
1401    not get any scratch registers to work with, because reload cannot
1402    be prevented from giving us a scratch that overlaps the register
1403    pair involved.  So instead, when addressing memory, we tweak the
1404    pointer register up and back down with POST_INCs.  Or up and not
1405    back down when we can get away with it.
1406
1407    REVERSED is true when the loads must be done in reversed order
1408    (high word first) for correctness.  DEAD is true when the pointer
1409    dies with the second insn we generate and therefore the second
1410    address must not carry a postmodify.
1411
1412    May return an insn which is to be emitted after the moves.  */
1413
1414 static rtx
1415 ia64_split_tmode (rtx out[2], rtx in, bool reversed, bool dead)
1416 {
1417   rtx fixup = 0;
1418
1419   switch (GET_CODE (in))
1420     {
1421     case REG:
1422       out[reversed] = gen_rtx_REG (DImode, REGNO (in));
1423       out[!reversed] = gen_rtx_REG (DImode, REGNO (in) + 1);
1424       break;
1425
1426     case CONST_INT:
1427     case CONST_DOUBLE:
1428       /* Cannot occur reversed.  */
1429       if (reversed) abort ();
1430       
1431       if (GET_MODE (in) != TFmode)
1432         split_double (in, &out[0], &out[1]);
1433       else
1434         /* split_double does not understand how to split a TFmode
1435            quantity into a pair of DImode constants.  */
1436         {
1437           REAL_VALUE_TYPE r;
1438           unsigned HOST_WIDE_INT p[2];
1439           long l[4];  /* TFmode is 128 bits */
1440
1441           REAL_VALUE_FROM_CONST_DOUBLE (r, in);
1442           real_to_target (l, &r, TFmode);
1443
1444           if (FLOAT_WORDS_BIG_ENDIAN)
1445             {
1446               p[0] = (((unsigned HOST_WIDE_INT) l[0]) << 32) + l[1];
1447               p[1] = (((unsigned HOST_WIDE_INT) l[2]) << 32) + l[3];
1448             }
1449           else
1450             {
1451               p[0] = (((unsigned HOST_WIDE_INT) l[3]) << 32) + l[2];
1452               p[1] = (((unsigned HOST_WIDE_INT) l[1]) << 32) + l[0];
1453             }
1454           out[0] = GEN_INT (p[0]);
1455           out[1] = GEN_INT (p[1]);
1456         }
1457       break;
1458
1459     case MEM:
1460       {
1461         rtx base = XEXP (in, 0);
1462         rtx offset;
1463
1464         switch (GET_CODE (base))
1465           {
1466           case REG:
1467             if (!reversed)
1468               {
1469                 out[0] = adjust_automodify_address
1470                   (in, DImode, gen_rtx_POST_INC (Pmode, base), 0);
1471                 out[1] = adjust_automodify_address
1472                   (in, DImode, dead ? 0 : gen_rtx_POST_DEC (Pmode, base), 8);
1473               }
1474             else
1475               {
1476                 /* Reversal requires a pre-increment, which can only
1477                    be done as a separate insn.  */
1478                 emit_insn (gen_adddi3 (base, base, GEN_INT (8)));
1479                 out[0] = adjust_automodify_address
1480                   (in, DImode, gen_rtx_POST_DEC (Pmode, base), 8);
1481                 out[1] = adjust_address (in, DImode, 0);
1482               }
1483             break;
1484
1485           case POST_INC:
1486             if (reversed || dead) abort ();
1487             /* Just do the increment in two steps.  */
1488             out[0] = adjust_automodify_address (in, DImode, 0, 0);
1489             out[1] = adjust_automodify_address (in, DImode, 0, 8);
1490             break;
1491
1492           case POST_DEC:
1493             if (reversed || dead) abort ();
1494             /* Add 8, subtract 24.  */
1495             base = XEXP (base, 0);
1496             out[0] = adjust_automodify_address
1497               (in, DImode, gen_rtx_POST_INC (Pmode, base), 0);
1498             out[1] = adjust_automodify_address
1499               (in, DImode,
1500                gen_rtx_POST_MODIFY (Pmode, base, plus_constant (base, -24)),
1501                8);
1502             break;
1503
1504           case POST_MODIFY:
1505             if (reversed || dead) abort ();
1506             /* Extract and adjust the modification.  This case is
1507                trickier than the others, because we might have an
1508                index register, or we might have a combined offset that
1509                doesn't fit a signed 9-bit displacement field.  We can
1510                assume the incoming expression is already legitimate.  */
1511             offset = XEXP (base, 1);
1512             base = XEXP (base, 0);
1513
1514             out[0] = adjust_automodify_address
1515               (in, DImode, gen_rtx_POST_INC (Pmode, base), 0);
1516
1517             if (GET_CODE (XEXP (offset, 1)) == REG)
1518               {
1519                 /* Can't adjust the postmodify to match.  Emit the
1520                    original, then a separate addition insn.  */
1521                 out[1] = adjust_automodify_address (in, DImode, 0, 8);
1522                 fixup = gen_adddi3 (base, base, GEN_INT (-8));
1523               }
1524             else if (GET_CODE (XEXP (offset, 1)) != CONST_INT)
1525               abort ();
1526             else if (INTVAL (XEXP (offset, 1)) < -256 + 8)
1527               {
1528                 /* Again the postmodify cannot be made to match, but
1529                    in this case it's more efficient to get rid of the
1530                    postmodify entirely and fix up with an add insn.  */
1531                 out[1] = adjust_automodify_address (in, DImode, base, 8);
1532                 fixup = gen_adddi3 (base, base,
1533                                     GEN_INT (INTVAL (XEXP (offset, 1)) - 8));
1534               }
1535             else
1536               {
1537                 /* Combined offset still fits in the displacement field.
1538                    (We cannot overflow it at the high end.)  */
1539                 out[1] = adjust_automodify_address
1540                   (in, DImode,
1541                    gen_rtx_POST_MODIFY (Pmode, base,
1542                      gen_rtx_PLUS (Pmode, base,
1543                                    GEN_INT (INTVAL (XEXP (offset, 1)) - 8))),
1544                    8);
1545               }
1546             break;
1547
1548           default:
1549             abort ();
1550           }
1551         break;
1552       }
1553
1554     default:
1555       abort ();
1556     }
1557
1558   return fixup;
1559 }
1560
1561 /* Split a TImode or TFmode move instruction after reload.
1562    This is used by *movtf_internal and *movti_internal.  */
1563 void
1564 ia64_split_tmode_move (rtx operands[])
1565 {
1566   rtx in[2], out[2], insn;
1567   rtx fixup[2];
1568   bool dead = false;
1569   bool reversed = false;
1570
1571   /* It is possible for reload to decide to overwrite a pointer with
1572      the value it points to.  In that case we have to do the loads in
1573      the appropriate order so that the pointer is not destroyed too
1574      early.  Also we must not generate a postmodify for that second
1575      load, or rws_access_regno will abort.  */
1576   if (GET_CODE (operands[1]) == MEM
1577       && reg_overlap_mentioned_p (operands[0], operands[1]))
1578     {
1579       rtx base = XEXP (operands[1], 0);
1580       while (GET_CODE (base) != REG)
1581         base = XEXP (base, 0);
1582
1583       if (REGNO (base) == REGNO (operands[0]))
1584         reversed = true;
1585       dead = true;
1586     }
1587   /* Another reason to do the moves in reversed order is if the first
1588      element of the target register pair is also the second element of
1589      the source register pair.  */
1590   if (GET_CODE (operands[0]) == REG && GET_CODE (operands[1]) == REG
1591       && REGNO (operands[0]) == REGNO (operands[1]) + 1)
1592     reversed = true;
1593
1594   fixup[0] = ia64_split_tmode (in, operands[1], reversed, dead);
1595   fixup[1] = ia64_split_tmode (out, operands[0], reversed, dead);
1596
1597 #define MAYBE_ADD_REG_INC_NOTE(INSN, EXP)                               \
1598   if (GET_CODE (EXP) == MEM                                             \
1599       && (GET_CODE (XEXP (EXP, 0)) == POST_MODIFY                       \
1600           || GET_CODE (XEXP (EXP, 0)) == POST_INC                       \
1601           || GET_CODE (XEXP (EXP, 0)) == POST_DEC))                     \
1602     REG_NOTES (INSN) = gen_rtx_EXPR_LIST (REG_INC,                      \
1603                                           XEXP (XEXP (EXP, 0), 0),      \
1604                                           REG_NOTES (INSN))
1605
1606   insn = emit_insn (gen_rtx_SET (VOIDmode, out[0], in[0]));
1607   MAYBE_ADD_REG_INC_NOTE (insn, in[0]);
1608   MAYBE_ADD_REG_INC_NOTE (insn, out[0]);
1609
1610   insn = emit_insn (gen_rtx_SET (VOIDmode, out[1], in[1]));
1611   MAYBE_ADD_REG_INC_NOTE (insn, in[1]);
1612   MAYBE_ADD_REG_INC_NOTE (insn, out[1]);
1613
1614   if (fixup[0])
1615     emit_insn (fixup[0]);
1616   if (fixup[1])
1617     emit_insn (fixup[1]);
1618
1619 #undef MAYBE_ADD_REG_INC_NOTE
1620 }
1621
1622 /* ??? Fixing GR->FR XFmode moves during reload is hard.  You need to go
1623    through memory plus an extra GR scratch register.  Except that you can
1624    either get the first from SECONDARY_MEMORY_NEEDED or the second from
1625    SECONDARY_RELOAD_CLASS, but not both.
1626
1627    We got into problems in the first place by allowing a construct like
1628    (subreg:XF (reg:TI)), which we got from a union containing a long double.
1629    This solution attempts to prevent this situation from occurring.  When
1630    we see something like the above, we spill the inner register to memory.  */
1631
1632 rtx
1633 spill_xfmode_operand (rtx in, int force)
1634 {
1635   if (GET_CODE (in) == SUBREG
1636       && GET_MODE (SUBREG_REG (in)) == TImode
1637       && GET_CODE (SUBREG_REG (in)) == REG)
1638     {
1639       rtx memt = assign_stack_temp (TImode, 16, 0);
1640       emit_move_insn (memt, SUBREG_REG (in));
1641       return adjust_address (memt, XFmode, 0);
1642     }
1643   else if (force && GET_CODE (in) == REG)
1644     {
1645       rtx memx = assign_stack_temp (XFmode, 16, 0);
1646       emit_move_insn (memx, in);
1647       return memx;
1648     }
1649   else
1650     return in;
1651 }
1652
1653 /* Emit comparison instruction if necessary, returning the expression
1654    that holds the compare result in the proper mode.  */
1655
1656 static GTY(()) rtx cmptf_libfunc;
1657
1658 rtx
1659 ia64_expand_compare (enum rtx_code code, enum machine_mode mode)
1660 {
1661   rtx op0 = ia64_compare_op0, op1 = ia64_compare_op1;
1662   rtx cmp;
1663
1664   /* If we have a BImode input, then we already have a compare result, and
1665      do not need to emit another comparison.  */
1666   if (GET_MODE (op0) == BImode)
1667     {
1668       if ((code == NE || code == EQ) && op1 == const0_rtx)
1669         cmp = op0;
1670       else
1671         abort ();
1672     }
1673   /* HPUX TFmode compare requires a library call to _U_Qfcmp, which takes a
1674      magic number as its third argument, that indicates what to do.
1675      The return value is an integer to be compared against zero.  */
1676   else if (GET_MODE (op0) == TFmode)
1677     {
1678       enum qfcmp_magic {
1679         QCMP_INV = 1,   /* Raise FP_INVALID on SNaN as a side effect.  */
1680         QCMP_UNORD = 2,
1681         QCMP_EQ = 4,
1682         QCMP_LT = 8,
1683         QCMP_GT = 16
1684       } magic;
1685       enum rtx_code ncode;
1686       rtx ret, insns;
1687       if (!cmptf_libfunc || GET_MODE (op1) != TFmode)
1688         abort ();
1689       switch (code)
1690         {
1691           /* 1 = equal, 0 = not equal.  Equality operators do
1692              not raise FP_INVALID when given an SNaN operand.  */
1693         case EQ:        magic = QCMP_EQ;                  ncode = NE; break;
1694         case NE:        magic = QCMP_EQ;                  ncode = EQ; break;
1695           /* isunordered() from C99.  */
1696         case UNORDERED: magic = QCMP_UNORD;               ncode = NE; break;
1697           /* Relational operators raise FP_INVALID when given
1698              an SNaN operand.  */
1699         case LT:        magic = QCMP_LT        |QCMP_INV; ncode = NE; break;
1700         case LE:        magic = QCMP_LT|QCMP_EQ|QCMP_INV; ncode = NE; break;
1701         case GT:        magic = QCMP_GT        |QCMP_INV; ncode = NE; break;
1702         case GE:        magic = QCMP_GT|QCMP_EQ|QCMP_INV; ncode = NE; break;
1703           /* FUTURE: Implement UNEQ, UNLT, UNLE, UNGT, UNGE, LTGT.
1704              Expanders for buneq etc. weuld have to be added to ia64.md
1705              for this to be useful.  */
1706         default: abort ();
1707         }
1708
1709       start_sequence ();
1710
1711       ret = emit_library_call_value (cmptf_libfunc, 0, LCT_CONST, DImode, 3,
1712                                      op0, TFmode, op1, TFmode,
1713                                      GEN_INT (magic), DImode);
1714       cmp = gen_reg_rtx (BImode);
1715       emit_insn (gen_rtx_SET (VOIDmode, cmp,
1716                               gen_rtx_fmt_ee (ncode, BImode,
1717                                               ret, const0_rtx)));
1718
1719       insns = get_insns ();
1720       end_sequence ();
1721
1722       emit_libcall_block (insns, cmp, cmp,
1723                           gen_rtx_fmt_ee (code, BImode, op0, op1));
1724       code = NE;
1725     }
1726   else
1727     {
1728       cmp = gen_reg_rtx (BImode);
1729       emit_insn (gen_rtx_SET (VOIDmode, cmp,
1730                               gen_rtx_fmt_ee (code, BImode, op0, op1)));
1731       code = NE;
1732     }
1733
1734   return gen_rtx_fmt_ee (code, mode, cmp, const0_rtx);
1735 }
1736
1737 /* Emit the appropriate sequence for a call.  */
1738
1739 void
1740 ia64_expand_call (rtx retval, rtx addr, rtx nextarg ATTRIBUTE_UNUSED,
1741                   int sibcall_p)
1742 {
1743   rtx insn, b0;
1744
1745   addr = XEXP (addr, 0);
1746   addr = convert_memory_address (DImode, addr);
1747   b0 = gen_rtx_REG (DImode, R_BR (0));
1748
1749   /* ??? Should do this for functions known to bind local too.  */
1750   if (TARGET_NO_PIC || TARGET_AUTO_PIC)
1751     {
1752       if (sibcall_p)
1753         insn = gen_sibcall_nogp (addr);
1754       else if (! retval)
1755         insn = gen_call_nogp (addr, b0);
1756       else
1757         insn = gen_call_value_nogp (retval, addr, b0);
1758       insn = emit_call_insn (insn);
1759     }
1760   else
1761     {
1762       if (sibcall_p)
1763         insn = gen_sibcall_gp (addr);
1764       else if (! retval)
1765         insn = gen_call_gp (addr, b0);
1766       else
1767         insn = gen_call_value_gp (retval, addr, b0);
1768       insn = emit_call_insn (insn);
1769
1770       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
1771     }
1772
1773   if (sibcall_p)
1774     use_reg (&CALL_INSN_FUNCTION_USAGE (insn), b0);
1775 }
1776
1777 void
1778 ia64_reload_gp (void)
1779 {
1780   rtx tmp;
1781
1782   if (current_frame_info.reg_save_gp)
1783     tmp = gen_rtx_REG (DImode, current_frame_info.reg_save_gp);
1784   else
1785     {
1786       HOST_WIDE_INT offset;
1787
1788       offset = (current_frame_info.spill_cfa_off
1789                 + current_frame_info.spill_size);
1790       if (frame_pointer_needed)
1791         {
1792           tmp = hard_frame_pointer_rtx;
1793           offset = -offset;
1794         }
1795       else
1796         {
1797           tmp = stack_pointer_rtx;
1798           offset = current_frame_info.total_size - offset;
1799         }
1800
1801       if (CONST_OK_FOR_I (offset))
1802         emit_insn (gen_adddi3 (pic_offset_table_rtx,
1803                                tmp, GEN_INT (offset)));
1804       else
1805         {
1806           emit_move_insn (pic_offset_table_rtx, GEN_INT (offset));
1807           emit_insn (gen_adddi3 (pic_offset_table_rtx,
1808                                  pic_offset_table_rtx, tmp));
1809         }
1810
1811       tmp = gen_rtx_MEM (DImode, pic_offset_table_rtx);
1812     }
1813
1814   emit_move_insn (pic_offset_table_rtx, tmp);
1815 }
1816
1817 void
1818 ia64_split_call (rtx retval, rtx addr, rtx retaddr, rtx scratch_r,
1819                  rtx scratch_b, int noreturn_p, int sibcall_p)
1820 {
1821   rtx insn;
1822   bool is_desc = false;
1823
1824   /* If we find we're calling through a register, then we're actually
1825      calling through a descriptor, so load up the values.  */
1826   if (REG_P (addr) && GR_REGNO_P (REGNO (addr)))
1827     {
1828       rtx tmp;
1829       bool addr_dead_p;
1830
1831       /* ??? We are currently constrained to *not* use peep2, because
1832          we can legitimately change the global lifetime of the GP
1833          (in the form of killing where previously live).  This is
1834          because a call through a descriptor doesn't use the previous
1835          value of the GP, while a direct call does, and we do not
1836          commit to either form until the split here.
1837
1838          That said, this means that we lack precise life info for
1839          whether ADDR is dead after this call.  This is not terribly
1840          important, since we can fix things up essentially for free
1841          with the POST_DEC below, but it's nice to not use it when we
1842          can immediately tell it's not necessary.  */
1843       addr_dead_p = ((noreturn_p || sibcall_p
1844                       || TEST_HARD_REG_BIT (regs_invalidated_by_call,
1845                                             REGNO (addr)))
1846                      && !FUNCTION_ARG_REGNO_P (REGNO (addr)));
1847
1848       /* Load the code address into scratch_b.  */
1849       tmp = gen_rtx_POST_INC (Pmode, addr);
1850       tmp = gen_rtx_MEM (Pmode, tmp);
1851       emit_move_insn (scratch_r, tmp);
1852       emit_move_insn (scratch_b, scratch_r);
1853
1854       /* Load the GP address.  If ADDR is not dead here, then we must
1855          revert the change made above via the POST_INCREMENT.  */
1856       if (!addr_dead_p)
1857         tmp = gen_rtx_POST_DEC (Pmode, addr);
1858       else
1859         tmp = addr;
1860       tmp = gen_rtx_MEM (Pmode, tmp);
1861       emit_move_insn (pic_offset_table_rtx, tmp);
1862
1863       is_desc = true;
1864       addr = scratch_b;
1865     }
1866
1867   if (sibcall_p)
1868     insn = gen_sibcall_nogp (addr);
1869   else if (retval)
1870     insn = gen_call_value_nogp (retval, addr, retaddr);
1871   else
1872     insn = gen_call_nogp (addr, retaddr);
1873   emit_call_insn (insn);
1874
1875   if ((!TARGET_CONST_GP || is_desc) && !noreturn_p && !sibcall_p)
1876     ia64_reload_gp ();
1877 }
1878 \f
1879 /* Begin the assembly file.  */
1880
1881 static void
1882 ia64_file_start (void)
1883 {
1884   default_file_start ();
1885   emit_safe_across_calls ();
1886 }
1887
1888 void
1889 emit_safe_across_calls (void)
1890 {
1891   unsigned int rs, re;
1892   int out_state;
1893
1894   rs = 1;
1895   out_state = 0;
1896   while (1)
1897     {
1898       while (rs < 64 && call_used_regs[PR_REG (rs)])
1899         rs++;
1900       if (rs >= 64)
1901         break;
1902       for (re = rs + 1; re < 64 && ! call_used_regs[PR_REG (re)]; re++)
1903         continue;
1904       if (out_state == 0)
1905         {
1906           fputs ("\t.pred.safe_across_calls ", asm_out_file);
1907           out_state = 1;
1908         }
1909       else
1910         fputc (',', asm_out_file);
1911       if (re == rs + 1)
1912         fprintf (asm_out_file, "p%u", rs);
1913       else
1914         fprintf (asm_out_file, "p%u-p%u", rs, re - 1);
1915       rs = re + 1;
1916     }
1917   if (out_state)
1918     fputc ('\n', asm_out_file);
1919 }
1920
1921 /* Helper function for ia64_compute_frame_size: find an appropriate general
1922    register to spill some special register to.  SPECIAL_SPILL_MASK contains
1923    bits in GR0 to GR31 that have already been allocated by this routine.
1924    TRY_LOCALS is true if we should attempt to locate a local regnum.  */
1925
1926 static int
1927 find_gr_spill (int try_locals)
1928 {
1929   int regno;
1930
1931   /* If this is a leaf function, first try an otherwise unused
1932      call-clobbered register.  */
1933   if (current_function_is_leaf)
1934     {
1935       for (regno = GR_REG (1); regno <= GR_REG (31); regno++)
1936         if (! regs_ever_live[regno]
1937             && call_used_regs[regno]
1938             && ! fixed_regs[regno]
1939             && ! global_regs[regno]
1940             && ((current_frame_info.gr_used_mask >> regno) & 1) == 0)
1941           {
1942             current_frame_info.gr_used_mask |= 1 << regno;
1943             return regno;
1944           }
1945     }
1946
1947   if (try_locals)
1948     {
1949       regno = current_frame_info.n_local_regs;
1950       /* If there is a frame pointer, then we can't use loc79, because
1951          that is HARD_FRAME_POINTER_REGNUM.  In particular, see the
1952          reg_name switching code in ia64_expand_prologue.  */
1953       if (regno < (80 - frame_pointer_needed))
1954         {
1955           current_frame_info.n_local_regs = regno + 1;
1956           return LOC_REG (0) + regno;
1957         }
1958     }
1959
1960   /* Failed to find a general register to spill to.  Must use stack.  */
1961   return 0;
1962 }
1963
1964 /* In order to make for nice schedules, we try to allocate every temporary
1965    to a different register.  We must of course stay away from call-saved,
1966    fixed, and global registers.  We must also stay away from registers
1967    allocated in current_frame_info.gr_used_mask, since those include regs
1968    used all through the prologue.
1969
1970    Any register allocated here must be used immediately.  The idea is to
1971    aid scheduling, not to solve data flow problems.  */
1972
1973 static int last_scratch_gr_reg;
1974
1975 static int
1976 next_scratch_gr_reg (void)
1977 {
1978   int i, regno;
1979
1980   for (i = 0; i < 32; ++i)
1981     {
1982       regno = (last_scratch_gr_reg + i + 1) & 31;
1983       if (call_used_regs[regno]
1984           && ! fixed_regs[regno]
1985           && ! global_regs[regno]
1986           && ((current_frame_info.gr_used_mask >> regno) & 1) == 0)
1987         {
1988           last_scratch_gr_reg = regno;
1989           return regno;
1990         }
1991     }
1992
1993   /* There must be _something_ available.  */
1994   abort ();
1995 }
1996
1997 /* Helper function for ia64_compute_frame_size, called through
1998    diddle_return_value.  Mark REG in current_frame_info.gr_used_mask.  */
1999
2000 static void
2001 mark_reg_gr_used_mask (rtx reg, void *data ATTRIBUTE_UNUSED)
2002 {
2003   unsigned int regno = REGNO (reg);
2004   if (regno < 32)
2005     {
2006       unsigned int i, n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
2007       for (i = 0; i < n; ++i)
2008         current_frame_info.gr_used_mask |= 1 << (regno + i);
2009     }
2010 }
2011
2012 /* Returns the number of bytes offset between the frame pointer and the stack
2013    pointer for the current function.  SIZE is the number of bytes of space
2014    needed for local variables.  */
2015
2016 static void
2017 ia64_compute_frame_size (HOST_WIDE_INT size)
2018 {
2019   HOST_WIDE_INT total_size;
2020   HOST_WIDE_INT spill_size = 0;
2021   HOST_WIDE_INT extra_spill_size = 0;
2022   HOST_WIDE_INT pretend_args_size;
2023   HARD_REG_SET mask;
2024   int n_spilled = 0;
2025   int spilled_gr_p = 0;
2026   int spilled_fr_p = 0;
2027   unsigned int regno;
2028   int i;
2029
2030   if (current_frame_info.initialized)
2031     return;
2032
2033   memset (&current_frame_info, 0, sizeof current_frame_info);
2034   CLEAR_HARD_REG_SET (mask);
2035
2036   /* Don't allocate scratches to the return register.  */
2037   diddle_return_value (mark_reg_gr_used_mask, NULL);
2038
2039   /* Don't allocate scratches to the EH scratch registers.  */
2040   if (cfun->machine->ia64_eh_epilogue_sp)
2041     mark_reg_gr_used_mask (cfun->machine->ia64_eh_epilogue_sp, NULL);
2042   if (cfun->machine->ia64_eh_epilogue_bsp)
2043     mark_reg_gr_used_mask (cfun->machine->ia64_eh_epilogue_bsp, NULL);
2044
2045   /* Find the size of the register stack frame.  We have only 80 local
2046      registers, because we reserve 8 for the inputs and 8 for the
2047      outputs.  */
2048
2049   /* Skip HARD_FRAME_POINTER_REGNUM (loc79) when frame_pointer_needed,
2050      since we'll be adjusting that down later.  */
2051   regno = LOC_REG (78) + ! frame_pointer_needed;
2052   for (; regno >= LOC_REG (0); regno--)
2053     if (regs_ever_live[regno])
2054       break;
2055   current_frame_info.n_local_regs = regno - LOC_REG (0) + 1;
2056
2057   /* For functions marked with the syscall_linkage attribute, we must mark
2058      all eight input registers as in use, so that locals aren't visible to
2059      the caller.  */
2060
2061   if (cfun->machine->n_varargs > 0
2062       || lookup_attribute ("syscall_linkage",
2063                            TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
2064     current_frame_info.n_input_regs = 8;
2065   else
2066     {
2067       for (regno = IN_REG (7); regno >= IN_REG (0); regno--)
2068         if (regs_ever_live[regno])
2069           break;
2070       current_frame_info.n_input_regs = regno - IN_REG (0) + 1;
2071     }
2072
2073   for (regno = OUT_REG (7); regno >= OUT_REG (0); regno--)
2074     if (regs_ever_live[regno])
2075       break;
2076   i = regno - OUT_REG (0) + 1;
2077
2078   /* When -p profiling, we need one output register for the mcount argument.
2079      Likewise for -a profiling for the bb_init_func argument.  For -ax
2080      profiling, we need two output registers for the two bb_init_trace_func
2081      arguments.  */
2082   if (current_function_profile)
2083     i = MAX (i, 1);
2084   current_frame_info.n_output_regs = i;
2085
2086   /* ??? No rotating register support yet.  */
2087   current_frame_info.n_rotate_regs = 0;
2088
2089   /* Discover which registers need spilling, and how much room that
2090      will take.  Begin with floating point and general registers,
2091      which will always wind up on the stack.  */
2092
2093   for (regno = FR_REG (2); regno <= FR_REG (127); regno++)
2094     if (regs_ever_live[regno] && ! call_used_regs[regno])
2095       {
2096         SET_HARD_REG_BIT (mask, regno);
2097         spill_size += 16;
2098         n_spilled += 1;
2099         spilled_fr_p = 1;
2100       }
2101
2102   for (regno = GR_REG (1); regno <= GR_REG (31); regno++)
2103     if (regs_ever_live[regno] && ! call_used_regs[regno])
2104       {
2105         SET_HARD_REG_BIT (mask, regno);
2106         spill_size += 8;
2107         n_spilled += 1;
2108         spilled_gr_p = 1;
2109       }
2110
2111   for (regno = BR_REG (1); regno <= BR_REG (7); regno++)
2112     if (regs_ever_live[regno] && ! call_used_regs[regno])
2113       {
2114         SET_HARD_REG_BIT (mask, regno);
2115         spill_size += 8;
2116         n_spilled += 1;
2117       }
2118
2119   /* Now come all special registers that might get saved in other
2120      general registers.  */
2121
2122   if (frame_pointer_needed)
2123     {
2124       current_frame_info.reg_fp = find_gr_spill (1);
2125       /* If we did not get a register, then we take LOC79.  This is guaranteed
2126          to be free, even if regs_ever_live is already set, because this is
2127          HARD_FRAME_POINTER_REGNUM.  This requires incrementing n_local_regs,
2128          as we don't count loc79 above.  */
2129       if (current_frame_info.reg_fp == 0)
2130         {
2131           current_frame_info.reg_fp = LOC_REG (79);
2132           current_frame_info.n_local_regs++;
2133         }
2134     }
2135
2136   if (! current_function_is_leaf)
2137     {
2138       /* Emit a save of BR0 if we call other functions.  Do this even
2139          if this function doesn't return, as EH depends on this to be
2140          able to unwind the stack.  */
2141       SET_HARD_REG_BIT (mask, BR_REG (0));
2142
2143       current_frame_info.reg_save_b0 = find_gr_spill (1);
2144       if (current_frame_info.reg_save_b0 == 0)
2145         {
2146           spill_size += 8;
2147           n_spilled += 1;
2148         }
2149
2150       /* Similarly for ar.pfs.  */
2151       SET_HARD_REG_BIT (mask, AR_PFS_REGNUM);
2152       current_frame_info.reg_save_ar_pfs = find_gr_spill (1);
2153       if (current_frame_info.reg_save_ar_pfs == 0)
2154         {
2155           extra_spill_size += 8;
2156           n_spilled += 1;
2157         }
2158
2159       /* Similarly for gp.  Note that if we're calling setjmp, the stacked
2160          registers are clobbered, so we fall back to the stack.  */
2161       current_frame_info.reg_save_gp
2162         = (current_function_calls_setjmp ? 0 : find_gr_spill (1));
2163       if (current_frame_info.reg_save_gp == 0)
2164         {
2165           SET_HARD_REG_BIT (mask, GR_REG (1));
2166           spill_size += 8;
2167           n_spilled += 1;
2168         }
2169     }
2170   else
2171     {
2172       if (regs_ever_live[BR_REG (0)] && ! call_used_regs[BR_REG (0)])
2173         {
2174           SET_HARD_REG_BIT (mask, BR_REG (0));
2175           spill_size += 8;
2176           n_spilled += 1;
2177         }
2178
2179       if (regs_ever_live[AR_PFS_REGNUM])
2180         {
2181           SET_HARD_REG_BIT (mask, AR_PFS_REGNUM);
2182           current_frame_info.reg_save_ar_pfs = find_gr_spill (1);
2183           if (current_frame_info.reg_save_ar_pfs == 0)
2184             {
2185               extra_spill_size += 8;
2186               n_spilled += 1;
2187             }
2188         }
2189     }
2190
2191   /* Unwind descriptor hackery: things are most efficient if we allocate
2192      consecutive GR save registers for RP, PFS, FP in that order. However,
2193      it is absolutely critical that FP get the only hard register that's
2194      guaranteed to be free, so we allocated it first.  If all three did
2195      happen to be allocated hard regs, and are consecutive, rearrange them
2196      into the preferred order now.  */
2197   if (current_frame_info.reg_fp != 0
2198       && current_frame_info.reg_save_b0 == current_frame_info.reg_fp + 1
2199       && current_frame_info.reg_save_ar_pfs == current_frame_info.reg_fp + 2)
2200     {
2201       current_frame_info.reg_save_b0 = current_frame_info.reg_fp;
2202       current_frame_info.reg_save_ar_pfs = current_frame_info.reg_fp + 1;
2203       current_frame_info.reg_fp = current_frame_info.reg_fp + 2;
2204     }
2205
2206   /* See if we need to store the predicate register block.  */
2207   for (regno = PR_REG (0); regno <= PR_REG (63); regno++)
2208     if (regs_ever_live[regno] && ! call_used_regs[regno])
2209       break;
2210   if (regno <= PR_REG (63))
2211     {
2212       SET_HARD_REG_BIT (mask, PR_REG (0));
2213       current_frame_info.reg_save_pr = find_gr_spill (1);
2214       if (current_frame_info.reg_save_pr == 0)
2215         {
2216           extra_spill_size += 8;
2217           n_spilled += 1;
2218         }
2219
2220       /* ??? Mark them all as used so that register renaming and such
2221          are free to use them.  */
2222       for (regno = PR_REG (0); regno <= PR_REG (63); regno++)
2223         regs_ever_live[regno] = 1;
2224     }
2225
2226   /* If we're forced to use st8.spill, we're forced to save and restore
2227      ar.unat as well.  The check for existing liveness allows inline asm
2228      to touch ar.unat.  */
2229   if (spilled_gr_p || cfun->machine->n_varargs
2230       || regs_ever_live[AR_UNAT_REGNUM])
2231     {
2232       regs_ever_live[AR_UNAT_REGNUM] = 1;
2233       SET_HARD_REG_BIT (mask, AR_UNAT_REGNUM);
2234       current_frame_info.reg_save_ar_unat = find_gr_spill (spill_size == 0);
2235       if (current_frame_info.reg_save_ar_unat == 0)
2236         {
2237           extra_spill_size += 8;
2238           n_spilled += 1;
2239         }
2240     }
2241
2242   if (regs_ever_live[AR_LC_REGNUM])
2243     {
2244       SET_HARD_REG_BIT (mask, AR_LC_REGNUM);
2245       current_frame_info.reg_save_ar_lc = find_gr_spill (spill_size == 0);
2246       if (current_frame_info.reg_save_ar_lc == 0)
2247         {
2248           extra_spill_size += 8;
2249           n_spilled += 1;
2250         }
2251     }
2252
2253   /* If we have an odd number of words of pretend arguments written to
2254      the stack, then the FR save area will be unaligned.  We round the
2255      size of this area up to keep things 16 byte aligned.  */
2256   if (spilled_fr_p)
2257     pretend_args_size = IA64_STACK_ALIGN (current_function_pretend_args_size);
2258   else
2259     pretend_args_size = current_function_pretend_args_size;
2260
2261   total_size = (spill_size + extra_spill_size + size + pretend_args_size
2262                 + current_function_outgoing_args_size);
2263   total_size = IA64_STACK_ALIGN (total_size);
2264
2265   /* We always use the 16-byte scratch area provided by the caller, but
2266      if we are a leaf function, there's no one to which we need to provide
2267      a scratch area.  */
2268   if (current_function_is_leaf)
2269     total_size = MAX (0, total_size - 16);
2270
2271   current_frame_info.total_size = total_size;
2272   current_frame_info.spill_cfa_off = pretend_args_size - 16;
2273   current_frame_info.spill_size = spill_size;
2274   current_frame_info.extra_spill_size = extra_spill_size;
2275   COPY_HARD_REG_SET (current_frame_info.mask, mask);
2276   current_frame_info.n_spilled = n_spilled;
2277   current_frame_info.initialized = reload_completed;
2278 }
2279
2280 /* Compute the initial difference between the specified pair of registers.  */
2281
2282 HOST_WIDE_INT
2283 ia64_initial_elimination_offset (int from, int to)
2284 {
2285   HOST_WIDE_INT offset;
2286
2287   ia64_compute_frame_size (get_frame_size ());
2288   switch (from)
2289     {
2290     case FRAME_POINTER_REGNUM:
2291       if (to == HARD_FRAME_POINTER_REGNUM)
2292         {
2293           if (current_function_is_leaf)
2294             offset = -current_frame_info.total_size;
2295           else
2296             offset = -(current_frame_info.total_size
2297                        - current_function_outgoing_args_size - 16);
2298         }
2299       else if (to == STACK_POINTER_REGNUM)
2300         {
2301           if (current_function_is_leaf)
2302             offset = 0;
2303           else
2304             offset = 16 + current_function_outgoing_args_size;
2305         }
2306       else
2307         abort ();
2308       break;
2309
2310     case ARG_POINTER_REGNUM:
2311       /* Arguments start above the 16 byte save area, unless stdarg
2312          in which case we store through the 16 byte save area.  */
2313       if (to == HARD_FRAME_POINTER_REGNUM)
2314         offset = 16 - current_function_pretend_args_size;
2315       else if (to == STACK_POINTER_REGNUM)
2316         offset = (current_frame_info.total_size
2317                   + 16 - current_function_pretend_args_size);
2318       else
2319         abort ();
2320       break;
2321
2322     default:
2323       abort ();
2324     }
2325
2326   return offset;
2327 }
2328
2329 /* If there are more than a trivial number of register spills, we use
2330    two interleaved iterators so that we can get two memory references
2331    per insn group.
2332
2333    In order to simplify things in the prologue and epilogue expanders,
2334    we use helper functions to fix up the memory references after the
2335    fact with the appropriate offsets to a POST_MODIFY memory mode.
2336    The following data structure tracks the state of the two iterators
2337    while insns are being emitted.  */
2338
2339 struct spill_fill_data
2340 {
2341   rtx init_after;               /* point at which to emit initializations */
2342   rtx init_reg[2];              /* initial base register */
2343   rtx iter_reg[2];              /* the iterator registers */
2344   rtx *prev_addr[2];            /* address of last memory use */
2345   rtx prev_insn[2];             /* the insn corresponding to prev_addr */
2346   HOST_WIDE_INT prev_off[2];    /* last offset */
2347   int n_iter;                   /* number of iterators in use */
2348   int next_iter;                /* next iterator to use */
2349   unsigned int save_gr_used_mask;
2350 };
2351
2352 static struct spill_fill_data spill_fill_data;
2353
2354 static void
2355 setup_spill_pointers (int n_spills, rtx init_reg, HOST_WIDE_INT cfa_off)
2356 {
2357   int i;
2358
2359   spill_fill_data.init_after = get_last_insn ();
2360   spill_fill_data.init_reg[0] = init_reg;
2361   spill_fill_data.init_reg[1] = init_reg;
2362   spill_fill_data.prev_addr[0] = NULL;
2363   spill_fill_data.prev_addr[1] = NULL;
2364   spill_fill_data.prev_insn[0] = NULL;
2365   spill_fill_data.prev_insn[1] = NULL;
2366   spill_fill_data.prev_off[0] = cfa_off;
2367   spill_fill_data.prev_off[1] = cfa_off;
2368   spill_fill_data.next_iter = 0;
2369   spill_fill_data.save_gr_used_mask = current_frame_info.gr_used_mask;
2370
2371   spill_fill_data.n_iter = 1 + (n_spills > 2);
2372   for (i = 0; i < spill_fill_data.n_iter; ++i)
2373     {
2374       int regno = next_scratch_gr_reg ();
2375       spill_fill_data.iter_reg[i] = gen_rtx_REG (DImode, regno);
2376       current_frame_info.gr_used_mask |= 1 << regno;
2377     }
2378 }
2379
2380 static void
2381 finish_spill_pointers (void)
2382 {
2383   current_frame_info.gr_used_mask = spill_fill_data.save_gr_used_mask;
2384 }
2385
2386 static rtx
2387 spill_restore_mem (rtx reg, HOST_WIDE_INT cfa_off)
2388 {
2389   int iter = spill_fill_data.next_iter;
2390   HOST_WIDE_INT disp = spill_fill_data.prev_off[iter] - cfa_off;
2391   rtx disp_rtx = GEN_INT (disp);
2392   rtx mem;
2393
2394   if (spill_fill_data.prev_addr[iter])
2395     {
2396       if (CONST_OK_FOR_N (disp))
2397         {
2398           *spill_fill_data.prev_addr[iter]
2399             = gen_rtx_POST_MODIFY (DImode, spill_fill_data.iter_reg[iter],
2400                                    gen_rtx_PLUS (DImode,
2401                                                  spill_fill_data.iter_reg[iter],
2402                                                  disp_rtx));
2403           REG_NOTES (spill_fill_data.prev_insn[iter])
2404             = gen_rtx_EXPR_LIST (REG_INC, spill_fill_data.iter_reg[iter],
2405                                  REG_NOTES (spill_fill_data.prev_insn[iter]));
2406         }
2407       else
2408         {
2409           /* ??? Could use register post_modify for loads.  */
2410           if (! CONST_OK_FOR_I (disp))
2411             {
2412               rtx tmp = gen_rtx_REG (DImode, next_scratch_gr_reg ());
2413               emit_move_insn (tmp, disp_rtx);
2414               disp_rtx = tmp;
2415             }
2416           emit_insn (gen_adddi3 (spill_fill_data.iter_reg[iter],
2417                                  spill_fill_data.iter_reg[iter], disp_rtx));
2418         }
2419     }
2420   /* Micro-optimization: if we've created a frame pointer, it's at
2421      CFA 0, which may allow the real iterator to be initialized lower,
2422      slightly increasing parallelism.  Also, if there are few saves
2423      it may eliminate the iterator entirely.  */
2424   else if (disp == 0
2425            && spill_fill_data.init_reg[iter] == stack_pointer_rtx
2426            && frame_pointer_needed)
2427     {
2428       mem = gen_rtx_MEM (GET_MODE (reg), hard_frame_pointer_rtx);
2429       set_mem_alias_set (mem, get_varargs_alias_set ());
2430       return mem;
2431     }
2432   else
2433     {
2434       rtx seq, insn;
2435
2436       if (disp == 0)
2437         seq = gen_movdi (spill_fill_data.iter_reg[iter],
2438                          spill_fill_data.init_reg[iter]);
2439       else
2440         {
2441           start_sequence ();
2442
2443           if (! CONST_OK_FOR_I (disp))
2444             {
2445               rtx tmp = gen_rtx_REG (DImode, next_scratch_gr_reg ());
2446               emit_move_insn (tmp, disp_rtx);
2447               disp_rtx = tmp;
2448             }
2449
2450           emit_insn (gen_adddi3 (spill_fill_data.iter_reg[iter],
2451                                  spill_fill_data.init_reg[iter],
2452                                  disp_rtx));
2453
2454           seq = get_insns ();
2455           end_sequence ();
2456         }
2457
2458       /* Careful for being the first insn in a sequence.  */
2459       if (spill_fill_data.init_after)
2460         insn = emit_insn_after (seq, spill_fill_data.init_after);
2461       else
2462         {
2463           rtx first = get_insns ();
2464           if (first)
2465             insn = emit_insn_before (seq, first);
2466           else
2467             insn = emit_insn (seq);
2468         }
2469       spill_fill_data.init_after = insn;
2470
2471       /* If DISP is 0, we may or may not have a further adjustment
2472          afterward.  If we do, then the load/store insn may be modified
2473          to be a post-modify.  If we don't, then this copy may be
2474          eliminated by copyprop_hardreg_forward, which makes this
2475          insn garbage, which runs afoul of the sanity check in
2476          propagate_one_insn.  So mark this insn as legal to delete.  */
2477       if (disp == 0)
2478         REG_NOTES(insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx,
2479                                              REG_NOTES (insn));
2480     }
2481
2482   mem = gen_rtx_MEM (GET_MODE (reg), spill_fill_data.iter_reg[iter]);
2483
2484   /* ??? Not all of the spills are for varargs, but some of them are.
2485      The rest of the spills belong in an alias set of their own.  But
2486      it doesn't actually hurt to include them here.  */
2487   set_mem_alias_set (mem, get_varargs_alias_set ());
2488
2489   spill_fill_data.prev_addr[iter] = &XEXP (mem, 0);
2490   spill_fill_data.prev_off[iter] = cfa_off;
2491
2492   if (++iter >= spill_fill_data.n_iter)
2493     iter = 0;
2494   spill_fill_data.next_iter = iter;
2495
2496   return mem;
2497 }
2498
2499 static void
2500 do_spill (rtx (*move_fn) (rtx, rtx, rtx), rtx reg, HOST_WIDE_INT cfa_off,
2501           rtx frame_reg)
2502 {
2503   int iter = spill_fill_data.next_iter;
2504   rtx mem, insn;
2505
2506   mem = spill_restore_mem (reg, cfa_off);
2507   insn = emit_insn ((*move_fn) (mem, reg, GEN_INT (cfa_off)));
2508   spill_fill_data.prev_insn[iter] = insn;
2509
2510   if (frame_reg)
2511     {
2512       rtx base;
2513       HOST_WIDE_INT off;
2514
2515       RTX_FRAME_RELATED_P (insn) = 1;
2516
2517       /* Don't even pretend that the unwind code can intuit its way
2518          through a pair of interleaved post_modify iterators.  Just
2519          provide the correct answer.  */
2520
2521       if (frame_pointer_needed)
2522         {
2523           base = hard_frame_pointer_rtx;
2524           off = - cfa_off;
2525         }
2526       else
2527         {
2528           base = stack_pointer_rtx;
2529           off = current_frame_info.total_size - cfa_off;
2530         }
2531
2532       REG_NOTES (insn)
2533         = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
2534                 gen_rtx_SET (VOIDmode,
2535                              gen_rtx_MEM (GET_MODE (reg),
2536                                           plus_constant (base, off)),
2537                              frame_reg),
2538                 REG_NOTES (insn));
2539     }
2540 }
2541
2542 static void
2543 do_restore (rtx (*move_fn) (rtx, rtx, rtx), rtx reg, HOST_WIDE_INT cfa_off)
2544 {
2545   int iter = spill_fill_data.next_iter;
2546   rtx insn;
2547
2548   insn = emit_insn ((*move_fn) (reg, spill_restore_mem (reg, cfa_off),
2549                                 GEN_INT (cfa_off)));
2550   spill_fill_data.prev_insn[iter] = insn;
2551 }
2552
2553 /* Wrapper functions that discards the CONST_INT spill offset.  These
2554    exist so that we can give gr_spill/gr_fill the offset they need and
2555    use a consistent function interface.  */
2556
2557 static rtx
2558 gen_movdi_x (rtx dest, rtx src, rtx offset ATTRIBUTE_UNUSED)
2559 {
2560   return gen_movdi (dest, src);
2561 }
2562
2563 static rtx
2564 gen_fr_spill_x (rtx dest, rtx src, rtx offset ATTRIBUTE_UNUSED)
2565 {
2566   return gen_fr_spill (dest, src);
2567 }
2568
2569 static rtx
2570 gen_fr_restore_x (rtx dest, rtx src, rtx offset ATTRIBUTE_UNUSED)
2571 {
2572   return gen_fr_restore (dest, src);
2573 }
2574
2575 /* Called after register allocation to add any instructions needed for the
2576    prologue.  Using a prologue insn is favored compared to putting all of the
2577    instructions in output_function_prologue(), since it allows the scheduler
2578    to intermix instructions with the saves of the caller saved registers.  In
2579    some cases, it might be necessary to emit a barrier instruction as the last
2580    insn to prevent such scheduling.
2581
2582    Also any insns generated here should have RTX_FRAME_RELATED_P(insn) = 1
2583    so that the debug info generation code can handle them properly.
2584
2585    The register save area is layed out like so:
2586    cfa+16
2587         [ varargs spill area ]
2588         [ fr register spill area ]
2589         [ br register spill area ]
2590         [ ar register spill area ]
2591         [ pr register spill area ]
2592         [ gr register spill area ] */
2593
2594 /* ??? Get inefficient code when the frame size is larger than can fit in an
2595    adds instruction.  */
2596
2597 void
2598 ia64_expand_prologue (void)
2599 {
2600   rtx insn, ar_pfs_save_reg, ar_unat_save_reg;
2601   int i, epilogue_p, regno, alt_regno, cfa_off, n_varargs;
2602   rtx reg, alt_reg;
2603
2604   ia64_compute_frame_size (get_frame_size ());
2605   last_scratch_gr_reg = 15;
2606
2607   /* If there is no epilogue, then we don't need some prologue insns.
2608      We need to avoid emitting the dead prologue insns, because flow
2609      will complain about them.  */
2610   if (optimize)
2611     {
2612       edge e;
2613
2614       for (e = EXIT_BLOCK_PTR->pred; e ; e = e->pred_next)
2615         if ((e->flags & EDGE_FAKE) == 0
2616             && (e->flags & EDGE_FALLTHRU) != 0)
2617           break;
2618       epilogue_p = (e != NULL);
2619     }
2620   else
2621     epilogue_p = 1;
2622
2623   /* Set the local, input, and output register names.  We need to do this
2624      for GNU libc, which creates crti.S/crtn.S by splitting initfini.c in
2625      half.  If we use in/loc/out register names, then we get assembler errors
2626      in crtn.S because there is no alloc insn or regstk directive in there.  */
2627   if (! TARGET_REG_NAMES)
2628     {
2629       int inputs = current_frame_info.n_input_regs;
2630       int locals = current_frame_info.n_local_regs;
2631       int outputs = current_frame_info.n_output_regs;
2632
2633       for (i = 0; i < inputs; i++)
2634         reg_names[IN_REG (i)] = ia64_reg_numbers[i];
2635       for (i = 0; i < locals; i++)
2636         reg_names[LOC_REG (i)] = ia64_reg_numbers[inputs + i];
2637       for (i = 0; i < outputs; i++)
2638         reg_names[OUT_REG (i)] = ia64_reg_numbers[inputs + locals + i];
2639     }
2640
2641   /* Set the frame pointer register name.  The regnum is logically loc79,
2642      but of course we'll not have allocated that many locals.  Rather than
2643      worrying about renumbering the existing rtxs, we adjust the name.  */
2644   /* ??? This code means that we can never use one local register when
2645      there is a frame pointer.  loc79 gets wasted in this case, as it is
2646      renamed to a register that will never be used.  See also the try_locals
2647      code in find_gr_spill.  */
2648   if (current_frame_info.reg_fp)
2649     {
2650       const char *tmp = reg_names[HARD_FRAME_POINTER_REGNUM];
2651       reg_names[HARD_FRAME_POINTER_REGNUM]
2652         = reg_names[current_frame_info.reg_fp];
2653       reg_names[current_frame_info.reg_fp] = tmp;
2654     }
2655
2656   /* We don't need an alloc instruction if we've used no outputs or locals.  */
2657   if (current_frame_info.n_local_regs == 0
2658       && current_frame_info.n_output_regs == 0
2659       && current_frame_info.n_input_regs <= current_function_args_info.int_regs
2660       && !TEST_HARD_REG_BIT (current_frame_info.mask, AR_PFS_REGNUM))
2661     {
2662       /* If there is no alloc, but there are input registers used, then we
2663          need a .regstk directive.  */
2664       current_frame_info.need_regstk = (TARGET_REG_NAMES != 0);
2665       ar_pfs_save_reg = NULL_RTX;
2666     }
2667   else
2668     {
2669       current_frame_info.need_regstk = 0;
2670
2671       if (current_frame_info.reg_save_ar_pfs)
2672         regno = current_frame_info.reg_save_ar_pfs;
2673       else
2674         regno = next_scratch_gr_reg ();
2675       ar_pfs_save_reg = gen_rtx_REG (DImode, regno);
2676
2677       insn = emit_insn (gen_alloc (ar_pfs_save_reg,
2678                                    GEN_INT (current_frame_info.n_input_regs),
2679                                    GEN_INT (current_frame_info.n_local_regs),
2680                                    GEN_INT (current_frame_info.n_output_regs),
2681                                    GEN_INT (current_frame_info.n_rotate_regs)));
2682       RTX_FRAME_RELATED_P (insn) = (current_frame_info.reg_save_ar_pfs != 0);
2683     }
2684
2685   /* Set up frame pointer, stack pointer, and spill iterators.  */
2686
2687   n_varargs = cfun->machine->n_varargs;
2688   setup_spill_pointers (current_frame_info.n_spilled + n_varargs,
2689                         stack_pointer_rtx, 0);
2690
2691   if (frame_pointer_needed)
2692     {
2693       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
2694       RTX_FRAME_RELATED_P (insn) = 1;
2695     }
2696
2697   if (current_frame_info.total_size != 0)
2698     {
2699       rtx frame_size_rtx = GEN_INT (- current_frame_info.total_size);
2700       rtx offset;
2701
2702       if (CONST_OK_FOR_I (- current_frame_info.total_size))
2703         offset = frame_size_rtx;
2704       else
2705         {
2706           regno = next_scratch_gr_reg ();
2707           offset = gen_rtx_REG (DImode, regno);
2708           emit_move_insn (offset, frame_size_rtx);
2709         }
2710
2711       insn = emit_insn (gen_adddi3 (stack_pointer_rtx,
2712                                     stack_pointer_rtx, offset));
2713
2714       if (! frame_pointer_needed)
2715         {
2716           RTX_FRAME_RELATED_P (insn) = 1;
2717           if (GET_CODE (offset) != CONST_INT)
2718             {
2719               REG_NOTES (insn)
2720                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
2721                         gen_rtx_SET (VOIDmode,
2722                                      stack_pointer_rtx,
2723                                      gen_rtx_PLUS (DImode,
2724                                                    stack_pointer_rtx,
2725                                                    frame_size_rtx)),
2726                         REG_NOTES (insn));
2727             }
2728         }
2729
2730       /* ??? At this point we must generate a magic insn that appears to
2731          modify the stack pointer, the frame pointer, and all spill
2732          iterators.  This would allow the most scheduling freedom.  For
2733          now, just hard stop.  */
2734       emit_insn (gen_blockage ());
2735     }
2736
2737   /* Must copy out ar.unat before doing any integer spills.  */
2738   if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
2739     {
2740       if (current_frame_info.reg_save_ar_unat)
2741         ar_unat_save_reg
2742           = gen_rtx_REG (DImode, current_frame_info.reg_save_ar_unat);
2743       else
2744         {
2745           alt_regno = next_scratch_gr_reg ();
2746           ar_unat_save_reg = gen_rtx_REG (DImode, alt_regno);
2747           current_frame_info.gr_used_mask |= 1 << alt_regno;
2748         }
2749
2750       reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
2751       insn = emit_move_insn (ar_unat_save_reg, reg);
2752       RTX_FRAME_RELATED_P (insn) = (current_frame_info.reg_save_ar_unat != 0);
2753
2754       /* Even if we're not going to generate an epilogue, we still
2755          need to save the register so that EH works.  */
2756       if (! epilogue_p && current_frame_info.reg_save_ar_unat)
2757         emit_insn (gen_prologue_use (ar_unat_save_reg));
2758     }
2759   else
2760     ar_unat_save_reg = NULL_RTX;
2761
2762   /* Spill all varargs registers.  Do this before spilling any GR registers,
2763      since we want the UNAT bits for the GR registers to override the UNAT
2764      bits from varargs, which we don't care about.  */
2765
2766   cfa_off = -16;
2767   for (regno = GR_ARG_FIRST + 7; n_varargs > 0; --n_varargs, --regno)
2768     {
2769       reg = gen_rtx_REG (DImode, regno);
2770       do_spill (gen_gr_spill, reg, cfa_off += 8, NULL_RTX);
2771     }
2772
2773   /* Locate the bottom of the register save area.  */
2774   cfa_off = (current_frame_info.spill_cfa_off
2775              + current_frame_info.spill_size
2776              + current_frame_info.extra_spill_size);
2777
2778   /* Save the predicate register block either in a register or in memory.  */
2779   if (TEST_HARD_REG_BIT (current_frame_info.mask, PR_REG (0)))
2780     {
2781       reg = gen_rtx_REG (DImode, PR_REG (0));
2782       if (current_frame_info.reg_save_pr != 0)
2783         {
2784           alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_pr);
2785           insn = emit_move_insn (alt_reg, reg);
2786
2787           /* ??? Denote pr spill/fill by a DImode move that modifies all
2788              64 hard registers.  */
2789           RTX_FRAME_RELATED_P (insn) = 1;
2790           REG_NOTES (insn)
2791             = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
2792                         gen_rtx_SET (VOIDmode, alt_reg, reg),
2793                         REG_NOTES (insn));
2794
2795           /* Even if we're not going to generate an epilogue, we still
2796              need to save the register so that EH works.  */
2797           if (! epilogue_p)
2798             emit_insn (gen_prologue_use (alt_reg));
2799         }
2800       else
2801         {
2802           alt_regno = next_scratch_gr_reg ();
2803           alt_reg = gen_rtx_REG (DImode, alt_regno);
2804           insn = emit_move_insn (alt_reg, reg);
2805           do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
2806           cfa_off -= 8;
2807         }
2808     }
2809
2810   /* Handle AR regs in numerical order.  All of them get special handling.  */
2811   if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM)
2812       && current_frame_info.reg_save_ar_unat == 0)
2813     {
2814       reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
2815       do_spill (gen_movdi_x, ar_unat_save_reg, cfa_off, reg);
2816       cfa_off -= 8;
2817     }
2818
2819   /* The alloc insn already copied ar.pfs into a general register.  The
2820      only thing we have to do now is copy that register to a stack slot
2821      if we'd not allocated a local register for the job.  */
2822   if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_PFS_REGNUM)
2823       && current_frame_info.reg_save_ar_pfs == 0)
2824     {
2825       reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
2826       do_spill (gen_movdi_x, ar_pfs_save_reg, cfa_off, reg);
2827       cfa_off -= 8;
2828     }
2829
2830   if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_LC_REGNUM))
2831     {
2832       reg = gen_rtx_REG (DImode, AR_LC_REGNUM);
2833       if (current_frame_info.reg_save_ar_lc != 0)
2834         {
2835           alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_ar_lc);
2836           insn = emit_move_insn (alt_reg, reg);
2837           RTX_FRAME_RELATED_P (insn) = 1;
2838
2839           /* Even if we're not going to generate an epilogue, we still
2840              need to save the register so that EH works.  */
2841           if (! epilogue_p)
2842             emit_insn (gen_prologue_use (alt_reg));
2843         }
2844       else
2845         {
2846           alt_regno = next_scratch_gr_reg ();
2847           alt_reg = gen_rtx_REG (DImode, alt_regno);
2848           emit_move_insn (alt_reg, reg);
2849           do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
2850           cfa_off -= 8;
2851         }
2852     }
2853
2854   if (current_frame_info.reg_save_gp)
2855     {
2856       insn = emit_move_insn (gen_rtx_REG (DImode,
2857                                           current_frame_info.reg_save_gp),
2858                              pic_offset_table_rtx);
2859       /* We don't know for sure yet if this is actually needed, since
2860          we've not split the PIC call patterns.  If all of the calls
2861          are indirect, and not followed by any uses of the gp, then
2862          this save is dead.  Allow it to go away.  */
2863       REG_NOTES (insn)
2864         = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, REG_NOTES (insn));
2865     }
2866
2867   /* We should now be at the base of the gr/br/fr spill area.  */
2868   if (cfa_off != (current_frame_info.spill_cfa_off
2869                   + current_frame_info.spill_size))
2870     abort ();
2871
2872   /* Spill all general registers.  */
2873   for (regno = GR_REG (1); regno <= GR_REG (31); ++regno)
2874     if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
2875       {
2876         reg = gen_rtx_REG (DImode, regno);
2877         do_spill (gen_gr_spill, reg, cfa_off, reg);
2878         cfa_off -= 8;
2879       }
2880
2881   /* Handle BR0 specially -- it may be getting stored permanently in
2882      some GR register.  */
2883   if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
2884     {
2885       reg = gen_rtx_REG (DImode, BR_REG (0));
2886       if (current_frame_info.reg_save_b0 != 0)
2887         {
2888           alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_b0);
2889           insn = emit_move_insn (alt_reg, reg);
2890           RTX_FRAME_RELATED_P (insn) = 1;
2891
2892           /* Even if we're not going to generate an epilogue, we still
2893              need to save the register so that EH works.  */
2894           if (! epilogue_p)
2895             emit_insn (gen_prologue_use (alt_reg));
2896         }
2897       else
2898         {
2899           alt_regno = next_scratch_gr_reg ();
2900           alt_reg = gen_rtx_REG (DImode, alt_regno);
2901           emit_move_insn (alt_reg, reg);
2902           do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
2903           cfa_off -= 8;
2904         }
2905     }
2906
2907   /* Spill the rest of the BR registers.  */
2908   for (regno = BR_REG (1); regno <= BR_REG (7); ++regno)
2909     if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
2910       {
2911         alt_regno = next_scratch_gr_reg ();
2912         alt_reg = gen_rtx_REG (DImode, alt_regno);
2913         reg = gen_rtx_REG (DImode, regno);
2914         emit_move_insn (alt_reg, reg);
2915         do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
2916         cfa_off -= 8;
2917       }
2918
2919   /* Align the frame and spill all FR registers.  */
2920   for (regno = FR_REG (2); regno <= FR_REG (127); ++regno)
2921     if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
2922       {
2923         if (cfa_off & 15)
2924           abort ();
2925         reg = gen_rtx_REG (XFmode, regno);
2926         do_spill (gen_fr_spill_x, reg, cfa_off, reg);
2927         cfa_off -= 16;
2928       }
2929
2930   if (cfa_off != current_frame_info.spill_cfa_off)
2931     abort ();
2932
2933   finish_spill_pointers ();
2934 }
2935
2936 /* Called after register allocation to add any instructions needed for the
2937    epilogue.  Using an epilogue insn is favored compared to putting all of the
2938    instructions in output_function_prologue(), since it allows the scheduler
2939    to intermix instructions with the saves of the caller saved registers.  In
2940    some cases, it might be necessary to emit a barrier instruction as the last
2941    insn to prevent such scheduling.  */
2942
2943 void
2944 ia64_expand_epilogue (int sibcall_p)
2945 {
2946   rtx insn, reg, alt_reg, ar_unat_save_reg;
2947   int regno, alt_regno, cfa_off;
2948
2949   ia64_compute_frame_size (get_frame_size ());
2950
2951   /* If there is a frame pointer, then we use it instead of the stack
2952      pointer, so that the stack pointer does not need to be valid when
2953      the epilogue starts.  See EXIT_IGNORE_STACK.  */
2954   if (frame_pointer_needed)
2955     setup_spill_pointers (current_frame_info.n_spilled,
2956                           hard_frame_pointer_rtx, 0);
2957   else
2958     setup_spill_pointers (current_frame_info.n_spilled, stack_pointer_rtx,
2959                           current_frame_info.total_size);
2960
2961   if (current_frame_info.total_size != 0)
2962     {
2963       /* ??? At this point we must generate a magic insn that appears to
2964          modify the spill iterators and the frame pointer.  This would
2965          allow the most scheduling freedom.  For now, just hard stop.  */
2966       emit_insn (gen_blockage ());
2967     }
2968
2969   /* Locate the bottom of the register save area.  */
2970   cfa_off = (current_frame_info.spill_cfa_off
2971              + current_frame_info.spill_size
2972              + current_frame_info.extra_spill_size);
2973
2974   /* Restore the predicate registers.  */
2975   if (TEST_HARD_REG_BIT (current_frame_info.mask, PR_REG (0)))
2976     {
2977       if (current_frame_info.reg_save_pr != 0)
2978         alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_pr);
2979       else
2980         {
2981           alt_regno = next_scratch_gr_reg ();
2982           alt_reg = gen_rtx_REG (DImode, alt_regno);
2983           do_restore (gen_movdi_x, alt_reg, cfa_off);
2984           cfa_off -= 8;
2985         }
2986       reg = gen_rtx_REG (DImode, PR_REG (0));
2987       emit_move_insn (reg, alt_reg);
2988     }
2989
2990   /* Restore the application registers.  */
2991
2992   /* Load the saved unat from the stack, but do not restore it until
2993      after the GRs have been restored.  */
2994   if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
2995     {
2996       if (current_frame_info.reg_save_ar_unat != 0)
2997         ar_unat_save_reg
2998           = gen_rtx_REG (DImode, current_frame_info.reg_save_ar_unat);
2999       else
3000         {
3001           alt_regno = next_scratch_gr_reg ();
3002           ar_unat_save_reg = gen_rtx_REG (DImode, alt_regno);
3003           current_frame_info.gr_used_mask |= 1 << alt_regno;
3004           do_restore (gen_movdi_x, ar_unat_save_reg, cfa_off);
3005           cfa_off -= 8;
3006         }
3007     }
3008   else
3009     ar_unat_save_reg = NULL_RTX;
3010
3011   if (current_frame_info.reg_save_ar_pfs != 0)
3012     {
3013       alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_ar_pfs);
3014       reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
3015       emit_move_insn (reg, alt_reg);
3016     }
3017   else if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_PFS_REGNUM))
3018     {
3019       alt_regno = next_scratch_gr_reg ();
3020       alt_reg = gen_rtx_REG (DImode, alt_regno);
3021       do_restore (gen_movdi_x, alt_reg, cfa_off);
3022       cfa_off -= 8;
3023       reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
3024       emit_move_insn (reg, alt_reg);
3025     }
3026
3027   if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_LC_REGNUM))
3028     {
3029       if (current_frame_info.reg_save_ar_lc != 0)
3030         alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_ar_lc);
3031       else
3032         {
3033           alt_regno = next_scratch_gr_reg ();
3034           alt_reg = gen_rtx_REG (DImode, alt_regno);
3035           do_restore (gen_movdi_x, alt_reg, cfa_off);
3036           cfa_off -= 8;
3037         }
3038       reg = gen_rtx_REG (DImode, AR_LC_REGNUM);
3039       emit_move_insn (reg, alt_reg);
3040     }
3041
3042   /* We should now be at the base of the gr/br/fr spill area.  */
3043   if (cfa_off != (current_frame_info.spill_cfa_off
3044                   + current_frame_info.spill_size))
3045     abort ();
3046
3047   /* The GP may be stored on the stack in the prologue, but it's
3048      never restored in the epilogue.  Skip the stack slot.  */
3049   if (TEST_HARD_REG_BIT (current_frame_info.mask, GR_REG (1)))
3050     cfa_off -= 8;
3051
3052   /* Restore all general registers.  */
3053   for (regno = GR_REG (2); regno <= GR_REG (31); ++regno)
3054     if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3055       {
3056         reg = gen_rtx_REG (DImode, regno);
3057         do_restore (gen_gr_restore, reg, cfa_off);
3058         cfa_off -= 8;
3059       }
3060
3061   /* Restore the branch registers.  Handle B0 specially, as it may
3062      have gotten stored in some GR register.  */
3063   if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
3064     {
3065       if (current_frame_info.reg_save_b0 != 0)
3066         alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_b0);
3067       else
3068         {
3069           alt_regno = next_scratch_gr_reg ();
3070           alt_reg = gen_rtx_REG (DImode, alt_regno);
3071           do_restore (gen_movdi_x, alt_reg, cfa_off);
3072           cfa_off -= 8;
3073         }
3074       reg = gen_rtx_REG (DImode, BR_REG (0));
3075       emit_move_insn (reg, alt_reg);
3076     }
3077
3078   for (regno = BR_REG (1); regno <= BR_REG (7); ++regno)
3079     if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3080       {
3081         alt_regno = next_scratch_gr_reg ();
3082         alt_reg = gen_rtx_REG (DImode, alt_regno);
3083         do_restore (gen_movdi_x, alt_reg, cfa_off);
3084         cfa_off -= 8;
3085         reg = gen_rtx_REG (DImode, regno);
3086         emit_move_insn (reg, alt_reg);
3087       }
3088
3089   /* Restore floating point registers.  */
3090   for (regno = FR_REG (2); regno <= FR_REG (127); ++regno)
3091     if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3092       {
3093         if (cfa_off & 15)
3094           abort ();
3095         reg = gen_rtx_REG (XFmode, regno);
3096         do_restore (gen_fr_restore_x, reg, cfa_off);
3097         cfa_off -= 16;
3098       }
3099
3100   /* Restore ar.unat for real.  */
3101   if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
3102     {
3103       reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
3104       emit_move_insn (reg, ar_unat_save_reg);
3105     }
3106
3107   if (cfa_off != current_frame_info.spill_cfa_off)
3108     abort ();
3109
3110   finish_spill_pointers ();
3111
3112   if (current_frame_info.total_size || cfun->machine->ia64_eh_epilogue_sp)
3113     {
3114       /* ??? At this point we must generate a magic insn that appears to
3115          modify the spill iterators, the stack pointer, and the frame
3116          pointer.  This would allow the most scheduling freedom.  For now,
3117          just hard stop.  */
3118       emit_insn (gen_blockage ());
3119     }
3120
3121   if (cfun->machine->ia64_eh_epilogue_sp)
3122     emit_move_insn (stack_pointer_rtx, cfun->machine->ia64_eh_epilogue_sp);
3123   else if (frame_pointer_needed)
3124     {
3125       insn = emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
3126       RTX_FRAME_RELATED_P (insn) = 1;
3127     }
3128   else if (current_frame_info.total_size)
3129     {
3130       rtx offset, frame_size_rtx;
3131
3132       frame_size_rtx = GEN_INT (current_frame_info.total_size);
3133       if (CONST_OK_FOR_I (current_frame_info.total_size))
3134         offset = frame_size_rtx;
3135       else
3136         {
3137           regno = next_scratch_gr_reg ();
3138           offset = gen_rtx_REG (DImode, regno);
3139           emit_move_insn (offset, frame_size_rtx);
3140         }
3141
3142       insn = emit_insn (gen_adddi3 (stack_pointer_rtx, stack_pointer_rtx,
3143                                     offset));
3144
3145       RTX_FRAME_RELATED_P (insn) = 1;
3146       if (GET_CODE (offset) != CONST_INT)
3147         {
3148           REG_NOTES (insn)
3149             = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
3150                         gen_rtx_SET (VOIDmode,
3151                                      stack_pointer_rtx,
3152                                      gen_rtx_PLUS (DImode,
3153                                                    stack_pointer_rtx,
3154                                                    frame_size_rtx)),
3155                         REG_NOTES (insn));
3156         }
3157     }
3158
3159   if (cfun->machine->ia64_eh_epilogue_bsp)
3160     emit_insn (gen_set_bsp (cfun->machine->ia64_eh_epilogue_bsp));
3161
3162   if (! sibcall_p)
3163     emit_jump_insn (gen_return_internal (gen_rtx_REG (DImode, BR_REG (0))));
3164   else
3165     {
3166       int fp = GR_REG (2);
3167       /* We need a throw away register here, r0 and r1 are reserved, so r2 is the
3168          first available call clobbered register.  If there was a frame_pointer
3169          register, we may have swapped the names of r2 and HARD_FRAME_POINTER_REGNUM,
3170          so we have to make sure we're using the string "r2" when emitting
3171          the register name for the assembler.  */
3172       if (current_frame_info.reg_fp && current_frame_info.reg_fp == GR_REG (2))
3173         fp = HARD_FRAME_POINTER_REGNUM;
3174
3175       /* We must emit an alloc to force the input registers to become output
3176          registers.  Otherwise, if the callee tries to pass its parameters
3177          through to another call without an intervening alloc, then these
3178          values get lost.  */
3179       /* ??? We don't need to preserve all input registers.  We only need to
3180          preserve those input registers used as arguments to the sibling call.
3181          It is unclear how to compute that number here.  */
3182       if (current_frame_info.n_input_regs != 0)
3183         emit_insn (gen_alloc (gen_rtx_REG (DImode, fp),
3184                               const0_rtx, const0_rtx,
3185                               GEN_INT (current_frame_info.n_input_regs),
3186                               const0_rtx));
3187     }
3188 }
3189
3190 /* Return 1 if br.ret can do all the work required to return from a
3191    function.  */
3192
3193 int
3194 ia64_direct_return (void)
3195 {
3196   if (reload_completed && ! frame_pointer_needed)
3197     {
3198       ia64_compute_frame_size (get_frame_size ());
3199
3200       return (current_frame_info.total_size == 0
3201               && current_frame_info.n_spilled == 0
3202               && current_frame_info.reg_save_b0 == 0
3203               && current_frame_info.reg_save_pr == 0
3204               && current_frame_info.reg_save_ar_pfs == 0
3205               && current_frame_info.reg_save_ar_unat == 0
3206               && current_frame_info.reg_save_ar_lc == 0);
3207     }
3208   return 0;
3209 }
3210
3211 /* Return the magic cookie that we use to hold the return address
3212    during early compilation.  */
3213
3214 rtx
3215 ia64_return_addr_rtx (HOST_WIDE_INT count, rtx frame ATTRIBUTE_UNUSED)
3216 {
3217   if (count != 0)
3218     return NULL;
3219   return gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_RET_ADDR);
3220 }
3221
3222 /* Split this value after reload, now that we know where the return
3223    address is saved.  */
3224
3225 void
3226 ia64_split_return_addr_rtx (rtx dest)
3227 {
3228   rtx src;
3229
3230   if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
3231     {
3232       if (current_frame_info.reg_save_b0 != 0)
3233         src = gen_rtx_REG (DImode, current_frame_info.reg_save_b0);
3234       else
3235         {
3236           HOST_WIDE_INT off;
3237           unsigned int regno;
3238
3239           /* Compute offset from CFA for BR0.  */
3240           /* ??? Must be kept in sync with ia64_expand_prologue.  */
3241           off = (current_frame_info.spill_cfa_off
3242                  + current_frame_info.spill_size);
3243           for (regno = GR_REG (1); regno <= GR_REG (31); ++regno)
3244             if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
3245               off -= 8;
3246
3247           /* Convert CFA offset to a register based offset.  */
3248           if (frame_pointer_needed)
3249             src = hard_frame_pointer_rtx;
3250           else
3251             {
3252               src = stack_pointer_rtx;
3253               off += current_frame_info.total_size;
3254             }
3255
3256           /* Load address into scratch register.  */
3257           if (CONST_OK_FOR_I (off))
3258             emit_insn (gen_adddi3 (dest, src, GEN_INT (off)));
3259           else
3260             {
3261               emit_move_insn (dest, GEN_INT (off));
3262               emit_insn (gen_adddi3 (dest, src, dest));
3263             }
3264
3265           src = gen_rtx_MEM (Pmode, dest);
3266         }
3267     }
3268   else
3269     src = gen_rtx_REG (DImode, BR_REG (0));
3270
3271   emit_move_insn (dest, src);
3272 }
3273
3274 int
3275 ia64_hard_regno_rename_ok (int from, int to)
3276 {
3277   /* Don't clobber any of the registers we reserved for the prologue.  */
3278   if (to == current_frame_info.reg_fp
3279       || to == current_frame_info.reg_save_b0
3280       || to == current_frame_info.reg_save_pr
3281       || to == current_frame_info.reg_save_ar_pfs
3282       || to == current_frame_info.reg_save_ar_unat
3283       || to == current_frame_info.reg_save_ar_lc)
3284     return 0;
3285
3286   if (from == current_frame_info.reg_fp
3287       || from == current_frame_info.reg_save_b0
3288       || from == current_frame_info.reg_save_pr
3289       || from == current_frame_info.reg_save_ar_pfs
3290       || from == current_frame_info.reg_save_ar_unat
3291       || from == current_frame_info.reg_save_ar_lc)
3292     return 0;
3293
3294   /* Don't use output registers outside the register frame.  */
3295   if (OUT_REGNO_P (to) && to >= OUT_REG (current_frame_info.n_output_regs))
3296     return 0;
3297
3298   /* Retain even/oddness on predicate register pairs.  */
3299   if (PR_REGNO_P (from) && PR_REGNO_P (to))
3300     return (from & 1) == (to & 1);
3301
3302   return 1;
3303 }
3304
3305 /* Target hook for assembling integer objects.  Handle word-sized
3306    aligned objects and detect the cases when @fptr is needed.  */
3307
3308 static bool
3309 ia64_assemble_integer (rtx x, unsigned int size, int aligned_p)
3310 {
3311   if (size == POINTER_SIZE / BITS_PER_UNIT
3312       && aligned_p
3313       && !(TARGET_NO_PIC || TARGET_AUTO_PIC)
3314       && GET_CODE (x) == SYMBOL_REF
3315       && SYMBOL_REF_FUNCTION_P (x))
3316     {
3317       if (POINTER_SIZE == 32)
3318         fputs ("\tdata4\t@fptr(", asm_out_file);
3319       else
3320         fputs ("\tdata8\t@fptr(", asm_out_file);
3321       output_addr_const (asm_out_file, x);
3322       fputs (")\n", asm_out_file);
3323       return true;
3324     }
3325   return default_assemble_integer (x, size, aligned_p);
3326 }
3327
3328 /* Emit the function prologue.  */
3329
3330 static void
3331 ia64_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
3332 {
3333   int mask, grsave, grsave_prev;
3334
3335   if (current_frame_info.need_regstk)
3336     fprintf (file, "\t.regstk %d, %d, %d, %d\n",
3337              current_frame_info.n_input_regs,
3338              current_frame_info.n_local_regs,
3339              current_frame_info.n_output_regs,
3340              current_frame_info.n_rotate_regs);
3341
3342   if (!flag_unwind_tables && (!flag_exceptions || USING_SJLJ_EXCEPTIONS))
3343     return;
3344
3345   /* Emit the .prologue directive.  */
3346
3347   mask = 0;
3348   grsave = grsave_prev = 0;
3349   if (current_frame_info.reg_save_b0 != 0)
3350     {
3351       mask |= 8;
3352       grsave = grsave_prev = current_frame_info.reg_save_b0;
3353     }
3354   if (current_frame_info.reg_save_ar_pfs != 0
3355       && (grsave_prev == 0
3356           || current_frame_info.reg_save_ar_pfs == grsave_prev + 1))
3357     {
3358       mask |= 4;
3359       if (grsave_prev == 0)
3360         grsave = current_frame_info.reg_save_ar_pfs;
3361       grsave_prev = current_frame_info.reg_save_ar_pfs;
3362     }
3363   if (current_frame_info.reg_fp != 0
3364       && (grsave_prev == 0
3365           || current_frame_info.reg_fp == grsave_prev + 1))
3366     {
3367       mask |= 2;
3368       if (grsave_prev == 0)
3369         grsave = HARD_FRAME_POINTER_REGNUM;
3370       grsave_prev = current_frame_info.reg_fp;
3371     }
3372   if (current_frame_info.reg_save_pr != 0
3373       && (grsave_prev == 0
3374           || current_frame_info.reg_save_pr == grsave_prev + 1))
3375     {
3376       mask |= 1;
3377       if (grsave_prev == 0)
3378         grsave = current_frame_info.reg_save_pr;
3379     }
3380
3381   if (mask && TARGET_GNU_AS)
3382     fprintf (file, "\t.prologue %d, %d\n", mask,
3383              ia64_dbx_register_number (grsave));
3384   else
3385     fputs ("\t.prologue\n", file);
3386
3387   /* Emit a .spill directive, if necessary, to relocate the base of
3388      the register spill area.  */
3389   if (current_frame_info.spill_cfa_off != -16)
3390     fprintf (file, "\t.spill %ld\n",
3391              (long) (current_frame_info.spill_cfa_off
3392                      + current_frame_info.spill_size));
3393 }
3394
3395 /* Emit the .body directive at the scheduled end of the prologue.  */
3396
3397 static void
3398 ia64_output_function_end_prologue (FILE *file)
3399 {
3400   if (!flag_unwind_tables && (!flag_exceptions || USING_SJLJ_EXCEPTIONS))
3401     return;
3402
3403   fputs ("\t.body\n", file);
3404 }
3405
3406 /* Emit the function epilogue.  */
3407
3408 static void
3409 ia64_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
3410                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
3411 {
3412   int i;
3413
3414   if (current_frame_info.reg_fp)
3415     {
3416       const char *tmp = reg_names[HARD_FRAME_POINTER_REGNUM];
3417       reg_names[HARD_FRAME_POINTER_REGNUM]
3418         = reg_names[current_frame_info.reg_fp];
3419       reg_names[current_frame_info.reg_fp] = tmp;
3420     }
3421   if (! TARGET_REG_NAMES)
3422     {
3423       for (i = 0; i < current_frame_info.n_input_regs; i++)
3424         reg_names[IN_REG (i)] = ia64_input_reg_names[i];
3425       for (i = 0; i < current_frame_info.n_local_regs; i++)
3426         reg_names[LOC_REG (i)] = ia64_local_reg_names[i];
3427       for (i = 0; i < current_frame_info.n_output_regs; i++)
3428         reg_names[OUT_REG (i)] = ia64_output_reg_names[i];
3429     }
3430
3431   current_frame_info.initialized = 0;
3432 }
3433
3434 int
3435 ia64_dbx_register_number (int regno)
3436 {
3437   /* In ia64_expand_prologue we quite literally renamed the frame pointer
3438      from its home at loc79 to something inside the register frame.  We
3439      must perform the same renumbering here for the debug info.  */
3440   if (current_frame_info.reg_fp)
3441     {
3442       if (regno == HARD_FRAME_POINTER_REGNUM)
3443         regno = current_frame_info.reg_fp;
3444       else if (regno == current_frame_info.reg_fp)
3445         regno = HARD_FRAME_POINTER_REGNUM;
3446     }
3447
3448   if (IN_REGNO_P (regno))
3449     return 32 + regno - IN_REG (0);
3450   else if (LOC_REGNO_P (regno))
3451     return 32 + current_frame_info.n_input_regs + regno - LOC_REG (0);
3452   else if (OUT_REGNO_P (regno))
3453     return (32 + current_frame_info.n_input_regs
3454             + current_frame_info.n_local_regs + regno - OUT_REG (0));
3455   else
3456     return regno;
3457 }
3458
3459 void
3460 ia64_initialize_trampoline (rtx addr, rtx fnaddr, rtx static_chain)
3461 {
3462   rtx addr_reg, eight = GEN_INT (8);
3463
3464   /* The Intel assembler requires that the global __ia64_trampoline symbol
3465      be declared explicitly */
3466   if (!TARGET_GNU_AS)
3467     {
3468       static bool declared_ia64_trampoline = false;
3469
3470       if (!declared_ia64_trampoline)
3471         {
3472           declared_ia64_trampoline = true;
3473           (*targetm.asm_out.globalize_label) (asm_out_file,
3474                                               "__ia64_trampoline");
3475         }
3476     }
3477
3478   /* Load up our iterator.  */
3479   addr_reg = gen_reg_rtx (Pmode);
3480   emit_move_insn (addr_reg, addr);
3481
3482   /* The first two words are the fake descriptor:
3483      __ia64_trampoline, ADDR+16.  */
3484   emit_move_insn (gen_rtx_MEM (Pmode, addr_reg),
3485                   gen_rtx_SYMBOL_REF (Pmode, "__ia64_trampoline"));
3486   emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
3487
3488   emit_move_insn (gen_rtx_MEM (Pmode, addr_reg),
3489                   copy_to_reg (plus_constant (addr, 16)));
3490   emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
3491
3492   /* The third word is the target descriptor.  */
3493   emit_move_insn (gen_rtx_MEM (Pmode, addr_reg), fnaddr);
3494   emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
3495
3496   /* The fourth word is the static chain.  */
3497   emit_move_insn (gen_rtx_MEM (Pmode, addr_reg), static_chain);
3498 }
3499 \f
3500 /* Do any needed setup for a variadic function.  CUM has not been updated
3501    for the last named argument which has type TYPE and mode MODE.
3502
3503    We generate the actual spill instructions during prologue generation.  */
3504
3505 static void
3506 ia64_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3507                              tree type, int * pretend_size,
3508                              int second_time ATTRIBUTE_UNUSED)
3509 {
3510   CUMULATIVE_ARGS next_cum = *cum;
3511
3512   /* Skip the current argument.  */
3513   ia64_function_arg_advance (&next_cum, mode, type, 1);
3514
3515   if (next_cum.words < MAX_ARGUMENT_SLOTS)
3516     {
3517       int n = MAX_ARGUMENT_SLOTS - next_cum.words;
3518       *pretend_size = n * UNITS_PER_WORD;
3519       cfun->machine->n_varargs = n;
3520     }
3521 }
3522
3523 /* Check whether TYPE is a homogeneous floating point aggregate.  If
3524    it is, return the mode of the floating point type that appears
3525    in all leafs.  If it is not, return VOIDmode.
3526
3527    An aggregate is a homogeneous floating point aggregate is if all
3528    fields/elements in it have the same floating point type (e.g,
3529    SFmode).  128-bit quad-precision floats are excluded.  */
3530
3531 static enum machine_mode
3532 hfa_element_mode (tree type, int nested)
3533 {
3534   enum machine_mode element_mode = VOIDmode;
3535   enum machine_mode mode;
3536   enum tree_code code = TREE_CODE (type);
3537   int know_element_mode = 0;
3538   tree t;
3539
3540   switch (code)
3541     {
3542     case VOID_TYPE:     case INTEGER_TYPE:      case ENUMERAL_TYPE:
3543     case BOOLEAN_TYPE:  case CHAR_TYPE:         case POINTER_TYPE:
3544     case OFFSET_TYPE:   case REFERENCE_TYPE:    case METHOD_TYPE:
3545     case FILE_TYPE:     case SET_TYPE:          case LANG_TYPE:
3546     case FUNCTION_TYPE:
3547       return VOIDmode;
3548
3549       /* Fortran complex types are supposed to be HFAs, so we need to handle
3550          gcc's COMPLEX_TYPEs as HFAs.  We need to exclude the integral complex
3551          types though.  */
3552     case COMPLEX_TYPE:
3553       if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_COMPLEX_FLOAT
3554           && TYPE_MODE (type) != TCmode)
3555         return GET_MODE_INNER (TYPE_MODE (type));
3556       else
3557         return VOIDmode;
3558
3559     case REAL_TYPE:
3560       /* We want to return VOIDmode for raw REAL_TYPEs, but the actual
3561          mode if this is contained within an aggregate.  */
3562       if (nested && TYPE_MODE (type) != TFmode)
3563         return TYPE_MODE (type);
3564       else
3565         return VOIDmode;
3566
3567     case ARRAY_TYPE:
3568       return hfa_element_mode (TREE_TYPE (type), 1);
3569
3570     case RECORD_TYPE:
3571     case UNION_TYPE:
3572     case QUAL_UNION_TYPE:
3573       for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
3574         {
3575           if (TREE_CODE (t) != FIELD_DECL)
3576             continue;
3577
3578           mode = hfa_element_mode (TREE_TYPE (t), 1);
3579           if (know_element_mode)
3580             {
3581               if (mode != element_mode)
3582                 return VOIDmode;
3583             }
3584           else if (GET_MODE_CLASS (mode) != MODE_FLOAT)
3585             return VOIDmode;
3586           else
3587             {
3588               know_element_mode = 1;
3589               element_mode = mode;
3590             }
3591         }
3592       return element_mode;
3593
3594     default:
3595       /* If we reach here, we probably have some front-end specific type
3596          that the backend doesn't know about.  This can happen via the
3597          aggregate_value_p call in init_function_start.  All we can do is
3598          ignore unknown tree types.  */
3599       return VOIDmode;
3600     }
3601
3602   return VOIDmode;
3603 }
3604
3605 /* Return the number of words required to hold a quantity of TYPE and MODE
3606    when passed as an argument.  */
3607 static int
3608 ia64_function_arg_words (tree type, enum machine_mode mode)
3609 {
3610   int words;
3611
3612   if (mode == BLKmode)
3613     words = int_size_in_bytes (type);
3614   else
3615     words = GET_MODE_SIZE (mode);
3616
3617   return (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;  /* round up */
3618 }
3619
3620 /* Return the number of registers that should be skipped so the current
3621    argument (described by TYPE and WORDS) will be properly aligned.
3622
3623    Integer and float arguments larger than 8 bytes start at the next
3624    even boundary.  Aggregates larger than 8 bytes start at the next
3625    even boundary if the aggregate has 16 byte alignment.  Note that
3626    in the 32-bit ABI, TImode and TFmode have only 8-byte alignment
3627    but are still to be aligned in registers.
3628
3629    ??? The ABI does not specify how to handle aggregates with
3630    alignment from 9 to 15 bytes, or greater than 16.  We handle them
3631    all as if they had 16 byte alignment.  Such aggregates can occur
3632    only if gcc extensions are used.  */
3633 static int
3634 ia64_function_arg_offset (CUMULATIVE_ARGS *cum, tree type, int words)
3635 {
3636   if ((cum->words & 1) == 0)
3637     return 0;
3638
3639   if (type
3640       && TREE_CODE (type) != INTEGER_TYPE
3641       && TREE_CODE (type) != REAL_TYPE)
3642     return TYPE_ALIGN (type) > 8 * BITS_PER_UNIT;
3643   else
3644     return words > 1;
3645 }
3646
3647 /* Return rtx for register where argument is passed, or zero if it is passed
3648    on the stack.  */
3649 /* ??? 128-bit quad-precision floats are always passed in general
3650    registers.  */
3651
3652 rtx
3653 ia64_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, tree type,
3654                    int named, int incoming)
3655 {
3656   int basereg = (incoming ? GR_ARG_FIRST : AR_ARG_FIRST);
3657   int words = ia64_function_arg_words (type, mode);
3658   int offset = ia64_function_arg_offset (cum, type, words);
3659   enum machine_mode hfa_mode = VOIDmode;
3660
3661   /* If all argument slots are used, then it must go on the stack.  */
3662   if (cum->words + offset >= MAX_ARGUMENT_SLOTS)
3663     return 0;
3664
3665   /* Check for and handle homogeneous FP aggregates.  */
3666   if (type)
3667     hfa_mode = hfa_element_mode (type, 0);
3668
3669   /* Unnamed prototyped hfas are passed as usual.  Named prototyped hfas
3670      and unprototyped hfas are passed specially.  */
3671   if (hfa_mode != VOIDmode && (! cum->prototype || named))
3672     {
3673       rtx loc[16];
3674       int i = 0;
3675       int fp_regs = cum->fp_regs;
3676       int int_regs = cum->words + offset;
3677       int hfa_size = GET_MODE_SIZE (hfa_mode);
3678       int byte_size;
3679       int args_byte_size;
3680
3681       /* If prototyped, pass it in FR regs then GR regs.
3682          If not prototyped, pass it in both FR and GR regs.
3683
3684          If this is an SFmode aggregate, then it is possible to run out of
3685          FR regs while GR regs are still left.  In that case, we pass the
3686          remaining part in the GR regs.  */
3687
3688       /* Fill the FP regs.  We do this always.  We stop if we reach the end
3689          of the argument, the last FP register, or the last argument slot.  */
3690
3691       byte_size = ((mode == BLKmode)
3692                    ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
3693       args_byte_size = int_regs * UNITS_PER_WORD;
3694       offset = 0;
3695       for (; (offset < byte_size && fp_regs < MAX_ARGUMENT_SLOTS
3696               && args_byte_size < (MAX_ARGUMENT_SLOTS * UNITS_PER_WORD)); i++)
3697         {
3698           loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
3699                                       gen_rtx_REG (hfa_mode, (FR_ARG_FIRST
3700                                                               + fp_regs)),
3701                                       GEN_INT (offset));
3702           offset += hfa_size;
3703           args_byte_size += hfa_size;
3704           fp_regs++;
3705         }
3706
3707       /* If no prototype, then the whole thing must go in GR regs.  */
3708       if (! cum->prototype)
3709         offset = 0;
3710       /* If this is an SFmode aggregate, then we might have some left over
3711          that needs to go in GR regs.  */
3712       else if (byte_size != offset)
3713         int_regs += offset / UNITS_PER_WORD;
3714
3715       /* Fill in the GR regs.  We must use DImode here, not the hfa mode.  */
3716
3717       for (; offset < byte_size && int_regs < MAX_ARGUMENT_SLOTS; i++)
3718         {
3719           enum machine_mode gr_mode = DImode;
3720           unsigned int gr_size;
3721
3722           /* If we have an odd 4 byte hunk because we ran out of FR regs,
3723              then this goes in a GR reg left adjusted/little endian, right
3724              adjusted/big endian.  */
3725           /* ??? Currently this is handled wrong, because 4-byte hunks are
3726              always right adjusted/little endian.  */
3727           if (offset & 0x4)
3728             gr_mode = SImode;
3729           /* If we have an even 4 byte hunk because the aggregate is a
3730              multiple of 4 bytes in size, then this goes in a GR reg right
3731              adjusted/little endian.  */
3732           else if (byte_size - offset == 4)
3733             gr_mode = SImode;
3734
3735           loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
3736                                       gen_rtx_REG (gr_mode, (basereg
3737                                                              + int_regs)),
3738                                       GEN_INT (offset));
3739
3740           gr_size = GET_MODE_SIZE (gr_mode);
3741           offset += gr_size;
3742           if (gr_size == UNITS_PER_WORD
3743               || (gr_size < UNITS_PER_WORD && offset % UNITS_PER_WORD == 0))
3744             int_regs++;
3745           else if (gr_size > UNITS_PER_WORD)
3746             int_regs += gr_size / UNITS_PER_WORD;
3747         }
3748
3749       /* If we ended up using just one location, just return that one loc, but
3750          change the mode back to the argument mode.  */
3751       if (i == 1)
3752         return gen_rtx_REG (mode, REGNO (XEXP (loc[0], 0)));
3753       else
3754         return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
3755     }
3756
3757   /* Integral and aggregates go in general registers.  If we have run out of
3758      FR registers, then FP values must also go in general registers.  This can
3759      happen when we have a SFmode HFA.  */
3760   else if (mode == TFmode || mode == TCmode
3761            || (! FLOAT_MODE_P (mode) || cum->fp_regs == MAX_ARGUMENT_SLOTS))
3762     {
3763       int byte_size = ((mode == BLKmode)
3764                        ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
3765       if (BYTES_BIG_ENDIAN
3766         && (mode == BLKmode || (type && AGGREGATE_TYPE_P (type)))
3767         && byte_size < UNITS_PER_WORD
3768         && byte_size > 0)
3769         {
3770           rtx gr_reg = gen_rtx_EXPR_LIST (VOIDmode,
3771                                           gen_rtx_REG (DImode,
3772                                                        (basereg + cum->words
3773                                                         + offset)),
3774                                           const0_rtx);
3775           return gen_rtx_PARALLEL (mode, gen_rtvec (1, gr_reg));
3776         }
3777       else
3778         return gen_rtx_REG (mode, basereg + cum->words + offset);
3779
3780     }
3781
3782   /* If there is a prototype, then FP values go in a FR register when
3783      named, and in a GR register when unnamed.  */
3784   else if (cum->prototype)
3785     {
3786       if (named)
3787         return gen_rtx_REG (mode, FR_ARG_FIRST + cum->fp_regs);
3788       /* In big-endian mode, an anonymous SFmode value must be represented
3789          as (parallel:SF [(expr_list (reg:DI n) (const_int 0))]) to force
3790          the value into the high half of the general register.  */
3791       else if (BYTES_BIG_ENDIAN && mode == SFmode)
3792         return gen_rtx_PARALLEL (mode,
3793                  gen_rtvec (1,
3794                    gen_rtx_EXPR_LIST (VOIDmode,
3795                      gen_rtx_REG (DImode, basereg + cum->words + offset),
3796                                       const0_rtx)));
3797       else
3798         return gen_rtx_REG (mode, basereg + cum->words + offset);
3799     }
3800   /* If there is no prototype, then FP values go in both FR and GR
3801      registers.  */
3802   else
3803     {
3804       /* See comment above.  */
3805       enum machine_mode inner_mode =
3806         (BYTES_BIG_ENDIAN && mode == SFmode) ? DImode : mode;
3807
3808       rtx fp_reg = gen_rtx_EXPR_LIST (VOIDmode,
3809                                       gen_rtx_REG (mode, (FR_ARG_FIRST
3810                                                           + cum->fp_regs)),
3811                                       const0_rtx);
3812       rtx gr_reg = gen_rtx_EXPR_LIST (VOIDmode,
3813                                       gen_rtx_REG (inner_mode,
3814                                                    (basereg + cum->words
3815                                                     + offset)),
3816                                       const0_rtx);
3817
3818       return gen_rtx_PARALLEL (mode, gen_rtvec (2, fp_reg, gr_reg));
3819     }
3820 }
3821
3822 /* Return number of words, at the beginning of the argument, that must be
3823    put in registers.  0 is the argument is entirely in registers or entirely
3824    in memory.  */
3825
3826 int
3827 ia64_function_arg_partial_nregs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3828                                  tree type, int named ATTRIBUTE_UNUSED)
3829 {
3830   int words = ia64_function_arg_words (type, mode);
3831   int offset = ia64_function_arg_offset (cum, type, words);
3832
3833   /* If all argument slots are used, then it must go on the stack.  */
3834   if (cum->words + offset >= MAX_ARGUMENT_SLOTS)
3835     return 0;
3836
3837   /* It doesn't matter whether the argument goes in FR or GR regs.  If
3838      it fits within the 8 argument slots, then it goes entirely in
3839      registers.  If it extends past the last argument slot, then the rest
3840      goes on the stack.  */
3841
3842   if (words + cum->words + offset <= MAX_ARGUMENT_SLOTS)
3843     return 0;
3844
3845   return MAX_ARGUMENT_SLOTS - cum->words - offset;
3846 }
3847
3848 /* Update CUM to point after this argument.  This is patterned after
3849    ia64_function_arg.  */
3850
3851 void
3852 ia64_function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3853                            tree type, int named)
3854 {
3855   int words = ia64_function_arg_words (type, mode);
3856   int offset = ia64_function_arg_offset (cum, type, words);
3857   enum machine_mode hfa_mode = VOIDmode;
3858
3859   /* If all arg slots are already full, then there is nothing to do.  */
3860   if (cum->words >= MAX_ARGUMENT_SLOTS)
3861     return;
3862
3863   cum->words += words + offset;
3864
3865   /* Check for and handle homogeneous FP aggregates.  */
3866   if (type)
3867     hfa_mode = hfa_element_mode (type, 0);
3868
3869   /* Unnamed prototyped hfas are passed as usual.  Named prototyped hfas
3870      and unprototyped hfas are passed specially.  */
3871   if (hfa_mode != VOIDmode && (! cum->prototype || named))
3872     {
3873       int fp_regs = cum->fp_regs;
3874       /* This is the original value of cum->words + offset.  */
3875       int int_regs = cum->words - words;
3876       int hfa_size = GET_MODE_SIZE (hfa_mode);
3877       int byte_size;
3878       int args_byte_size;
3879
3880       /* If prototyped, pass it in FR regs then GR regs.
3881          If not prototyped, pass it in both FR and GR regs.
3882
3883          If this is an SFmode aggregate, then it is possible to run out of
3884          FR regs while GR regs are still left.  In that case, we pass the
3885          remaining part in the GR regs.  */
3886
3887       /* Fill the FP regs.  We do this always.  We stop if we reach the end
3888          of the argument, the last FP register, or the last argument slot.  */
3889
3890       byte_size = ((mode == BLKmode)
3891                    ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
3892       args_byte_size = int_regs * UNITS_PER_WORD;
3893       offset = 0;
3894       for (; (offset < byte_size && fp_regs < MAX_ARGUMENT_SLOTS
3895               && args_byte_size < (MAX_ARGUMENT_SLOTS * UNITS_PER_WORD));)
3896         {
3897           offset += hfa_size;
3898           args_byte_size += hfa_size;
3899           fp_regs++;
3900         }
3901
3902       cum->fp_regs = fp_regs;
3903     }
3904
3905   /* Integral and aggregates go in general registers.  If we have run out of
3906      FR registers, then FP values must also go in general registers.  This can
3907      happen when we have a SFmode HFA.  */
3908   else if (! FLOAT_MODE_P (mode) || cum->fp_regs == MAX_ARGUMENT_SLOTS)
3909     cum->int_regs = cum->words;
3910
3911   /* If there is a prototype, then FP values go in a FR register when
3912      named, and in a GR register when unnamed.  */
3913   else if (cum->prototype)
3914     {
3915       if (! named)
3916         cum->int_regs = cum->words;
3917       else
3918         /* ??? Complex types should not reach here.  */
3919         cum->fp_regs += (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT ? 2 : 1);
3920     }
3921   /* If there is no prototype, then FP values go in both FR and GR
3922      registers.  */
3923   else
3924     {
3925       /* ??? Complex types should not reach here.  */
3926       cum->fp_regs += (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT ? 2 : 1);
3927       cum->int_regs = cum->words;
3928     }
3929 }
3930
3931 /* Variable sized types are passed by reference.  */
3932 /* ??? At present this is a GCC extension to the IA-64 ABI.  */
3933
3934 int
3935 ia64_function_arg_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3936                                      enum machine_mode mode ATTRIBUTE_UNUSED,
3937                                      tree type, int named ATTRIBUTE_UNUSED)
3938 {
3939   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
3940 }
3941
3942 /* True if it is OK to do sibling call optimization for the specified
3943    call expression EXP.  DECL will be the called function, or NULL if
3944    this is an indirect call.  */
3945 static bool
3946 ia64_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3947 {
3948   /* We must always return with our current GP.  This means we can
3949      only sibcall to functions defined in the current module.  */
3950   return decl && (*targetm.binds_local_p) (decl);
3951 }
3952 \f
3953
3954 /* Implement va_arg.  */
3955
3956 static tree
3957 ia64_gimplify_va_arg (tree valist, tree type, tree *pre_p, tree *post_p)
3958 {
3959   /* Variable sized types are passed by reference.  */
3960   if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
3961     {
3962       tree ptrtype = build_pointer_type (type);
3963       tree addr = std_gimplify_va_arg_expr (valist, ptrtype, pre_p, post_p);
3964       return build_fold_indirect_ref (addr);
3965     }
3966
3967   /* Aggregate arguments with alignment larger than 8 bytes start at
3968      the next even boundary.  Integer and floating point arguments
3969      do so if they are larger than 8 bytes, whether or not they are
3970      also aligned larger than 8 bytes.  */
3971   if ((TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == INTEGER_TYPE)
3972       ? int_size_in_bytes (type) > 8 : TYPE_ALIGN (type) > 8 * BITS_PER_UNIT)
3973     {
3974       tree t = build (PLUS_EXPR, TREE_TYPE (valist), valist,
3975                       build_int_2 (2 * UNITS_PER_WORD - 1, 0));
3976       t = build (BIT_AND_EXPR, TREE_TYPE (t), t,
3977                  build_int_2 (-2 * UNITS_PER_WORD, -1));
3978       t = build (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
3979       gimplify_and_add (t, pre_p);
3980     }
3981
3982   return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
3983 }
3984 \f
3985 /* Return 1 if function return value returned in memory.  Return 0 if it is
3986    in a register.  */
3987
3988 static bool
3989 ia64_return_in_memory (tree valtype, tree fntype ATTRIBUTE_UNUSED)
3990 {
3991   enum machine_mode mode;
3992   enum machine_mode hfa_mode;
3993   HOST_WIDE_INT byte_size;
3994
3995   mode = TYPE_MODE (valtype);
3996   byte_size = GET_MODE_SIZE (mode);
3997   if (mode == BLKmode)
3998     {
3999       byte_size = int_size_in_bytes (valtype);
4000       if (byte_size < 0)
4001         return true;
4002     }
4003
4004   /* Hfa's with up to 8 elements are returned in the FP argument registers.  */
4005
4006   hfa_mode = hfa_element_mode (valtype, 0);
4007   if (hfa_mode != VOIDmode)
4008     {
4009       int hfa_size = GET_MODE_SIZE (hfa_mode);
4010
4011       if (byte_size / hfa_size > MAX_ARGUMENT_SLOTS)
4012         return true;
4013       else
4014         return false;
4015     }
4016   else if (byte_size > UNITS_PER_WORD * MAX_INT_RETURN_SLOTS)
4017     return true;
4018   else
4019     return false;
4020 }
4021
4022 /* Return rtx for register that holds the function return value.  */
4023
4024 rtx
4025 ia64_function_value (tree valtype, tree func ATTRIBUTE_UNUSED)
4026 {
4027   enum machine_mode mode;
4028   enum machine_mode hfa_mode;
4029
4030   mode = TYPE_MODE (valtype);
4031   hfa_mode = hfa_element_mode (valtype, 0);
4032
4033   if (hfa_mode != VOIDmode)
4034     {
4035       rtx loc[8];
4036       int i;
4037       int hfa_size;
4038       int byte_size;
4039       int offset;
4040
4041       hfa_size = GET_MODE_SIZE (hfa_mode);
4042       byte_size = ((mode == BLKmode)
4043                    ? int_size_in_bytes (valtype) : GET_MODE_SIZE (mode));
4044       offset = 0;
4045       for (i = 0; offset < byte_size; i++)
4046         {
4047           loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
4048                                       gen_rtx_REG (hfa_mode, FR_ARG_FIRST + i),
4049                                       GEN_INT (offset));
4050           offset += hfa_size;
4051         }
4052
4053       if (i == 1)
4054         return XEXP (loc[0], 0);
4055       else
4056         return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
4057     }
4058   else if (FLOAT_TYPE_P (valtype) && mode != TFmode && mode != TCmode)
4059     return gen_rtx_REG (mode, FR_ARG_FIRST);
4060   else
4061     {
4062       if (BYTES_BIG_ENDIAN
4063           && (mode == BLKmode || (valtype && AGGREGATE_TYPE_P (valtype))))
4064         {
4065           rtx loc[8];
4066           int offset;
4067           int bytesize;
4068           int i;
4069
4070           offset = 0;
4071           bytesize = int_size_in_bytes (valtype);
4072           for (i = 0; offset < bytesize; i++)
4073             {
4074               loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
4075                                           gen_rtx_REG (DImode,
4076                                                        GR_RET_FIRST + i),
4077                                           GEN_INT (offset));
4078               offset += UNITS_PER_WORD;
4079             }
4080           return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
4081         }
4082       else
4083         return gen_rtx_REG (mode, GR_RET_FIRST);
4084     }
4085 }
4086
4087 /* This is called from dwarf2out.c via ASM_OUTPUT_DWARF_DTPREL.
4088    We need to emit DTP-relative relocations.  */
4089
4090 void
4091 ia64_output_dwarf_dtprel (FILE *file, int size, rtx x)
4092 {
4093   if (size != 8)
4094     abort ();
4095   fputs ("\tdata8.ua\t@dtprel(", file);
4096   output_addr_const (file, x);
4097   fputs (")", file);
4098 }
4099
4100 /* Print a memory address as an operand to reference that memory location.  */
4101
4102 /* ??? Do we need this?  It gets used only for 'a' operands.  We could perhaps
4103    also call this from ia64_print_operand for memory addresses.  */
4104
4105 void
4106 ia64_print_operand_address (FILE * stream ATTRIBUTE_UNUSED,
4107                             rtx address ATTRIBUTE_UNUSED)
4108 {
4109 }
4110
4111 /* Print an operand to an assembler instruction.
4112    C    Swap and print a comparison operator.
4113    D    Print an FP comparison operator.
4114    E    Print 32 - constant, for SImode shifts as extract.
4115    e    Print 64 - constant, for DImode rotates.
4116    F    A floating point constant 0.0 emitted as f0, or 1.0 emitted as f1, or
4117         a floating point register emitted normally.
4118    I    Invert a predicate register by adding 1.
4119    J    Select the proper predicate register for a condition.
4120    j    Select the inverse predicate register for a condition.
4121    O    Append .acq for volatile load.
4122    P    Postincrement of a MEM.
4123    Q    Append .rel for volatile store.
4124    S    Shift amount for shladd instruction.
4125    T    Print an 8-bit sign extended number (K) as a 32-bit unsigned number
4126         for Intel assembler.
4127    U    Print an 8-bit sign extended number (K) as a 64-bit unsigned number
4128         for Intel assembler.
4129    r    Print register name, or constant 0 as r0.  HP compatibility for
4130         Linux kernel.  */
4131 void
4132 ia64_print_operand (FILE * file, rtx x, int code)
4133 {
4134   const char *str;
4135
4136   switch (code)
4137     {
4138     case 0:
4139       /* Handled below.  */
4140       break;
4141
4142     case 'C':
4143       {
4144         enum rtx_code c = swap_condition (GET_CODE (x));
4145         fputs (GET_RTX_NAME (c), file);
4146         return;
4147       }
4148
4149     case 'D':
4150       switch (GET_CODE (x))
4151         {
4152         case NE:
4153           str = "neq";
4154           break;
4155         case UNORDERED:
4156           str = "unord";
4157           break;
4158         case ORDERED:
4159           str = "ord";
4160           break;
4161         default:
4162           str = GET_RTX_NAME (GET_CODE (x));
4163           break;
4164         }
4165       fputs (str, file);
4166       return;
4167
4168     case 'E':
4169       fprintf (file, HOST_WIDE_INT_PRINT_DEC, 32 - INTVAL (x));
4170       return;
4171
4172     case 'e':
4173       fprintf (file, HOST_WIDE_INT_PRINT_DEC, 64 - INTVAL (x));
4174       return;
4175
4176     case 'F':
4177       if (x == CONST0_RTX (GET_MODE (x)))
4178         str = reg_names [FR_REG (0)];
4179       else if (x == CONST1_RTX (GET_MODE (x)))
4180         str = reg_names [FR_REG (1)];
4181       else if (GET_CODE (x) == REG)
4182         str = reg_names [REGNO (x)];
4183       else
4184         abort ();
4185       fputs (str, file);
4186       return;
4187
4188     case 'I':
4189       fputs (reg_names [REGNO (x) + 1], file);
4190       return;
4191
4192     case 'J':
4193     case 'j':
4194       {
4195         unsigned int regno = REGNO (XEXP (x, 0));
4196         if (GET_CODE (x) == EQ)
4197           regno += 1;
4198         if (code == 'j')
4199           regno ^= 1;
4200         fputs (reg_names [regno], file);
4201       }
4202       return;
4203
4204     case 'O':
4205       if (MEM_VOLATILE_P (x))
4206         fputs(".acq", file);
4207       return;
4208
4209     case 'P':
4210       {
4211         HOST_WIDE_INT value;
4212
4213         switch (GET_CODE (XEXP (x, 0)))
4214           {
4215           default:
4216             return;
4217
4218           case POST_MODIFY:
4219             x = XEXP (XEXP (XEXP (x, 0), 1), 1);
4220             if (GET_CODE (x) == CONST_INT)
4221               value = INTVAL (x);
4222             else if (GET_CODE (x) == REG)
4223               {
4224                 fprintf (file, ", %s", reg_names[REGNO (x)]);
4225                 return;
4226               }
4227             else
4228               abort ();
4229             break;
4230
4231           case POST_INC:
4232             value = GET_MODE_SIZE (GET_MODE (x));
4233             break;
4234
4235           case POST_DEC:
4236             value = - (HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (x));
4237             break;
4238           }
4239
4240         fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC, value);
4241         return;
4242       }
4243
4244     case 'Q':
4245       if (MEM_VOLATILE_P (x))
4246         fputs(".rel", file);
4247       return;
4248
4249     case 'S':
4250       fprintf (file, "%d", exact_log2 (INTVAL (x)));
4251       return;
4252
4253     case 'T':
4254       if (! TARGET_GNU_AS && GET_CODE (x) == CONST_INT)
4255         {
4256           fprintf (file, "0x%x", (int) INTVAL (x) & 0xffffffff);
4257           return;
4258         }
4259       break;
4260
4261     case 'U':
4262       if (! TARGET_GNU_AS && GET_CODE (x) == CONST_INT)
4263         {
4264           const char *prefix = "0x";
4265           if (INTVAL (x) & 0x80000000)
4266             {
4267               fprintf (file, "0xffffffff");
4268               prefix = "";
4269             }
4270           fprintf (file, "%s%x", prefix, (int) INTVAL (x) & 0xffffffff);
4271           return;
4272         }
4273       break;
4274
4275     case 'r':
4276       /* If this operand is the constant zero, write it as register zero.
4277          Any register, zero, or CONST_INT value is OK here.  */
4278       if (GET_CODE (x) == REG)
4279         fputs (reg_names[REGNO (x)], file);
4280       else if (x == CONST0_RTX (GET_MODE (x)))
4281         fputs ("r0", file);
4282       else if (GET_CODE (x) == CONST_INT)
4283         output_addr_const (file, x);
4284       else
4285         output_operand_lossage ("invalid %%r value");
4286       return;
4287
4288     case '+':
4289       {
4290         const char *which;
4291
4292         /* For conditional branches, returns or calls, substitute
4293            sptk, dptk, dpnt, or spnt for %s.  */
4294         x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
4295         if (x)
4296           {
4297             int pred_val = INTVAL (XEXP (x, 0));
4298
4299             /* Guess top and bottom 10% statically predicted.  */
4300             if (pred_val < REG_BR_PROB_BASE / 50)
4301               which = ".spnt";
4302             else if (pred_val < REG_BR_PROB_BASE / 2)
4303               which = ".dpnt";
4304             else if (pred_val < REG_BR_PROB_BASE / 100 * 98)
4305               which = ".dptk";
4306             else
4307               which = ".sptk";
4308           }
4309         else if (GET_CODE (current_output_insn) == CALL_INSN)
4310           which = ".sptk";
4311         else
4312           which = ".dptk";
4313
4314         fputs (which, file);
4315         return;
4316       }
4317
4318     case ',':
4319       x = current_insn_predicate;
4320       if (x)
4321         {
4322           unsigned int regno = REGNO (XEXP (x, 0));
4323           if (GET_CODE (x) == EQ)
4324             regno += 1;
4325           fprintf (file, "(%s) ", reg_names [regno]);
4326         }
4327       return;
4328
4329     default:
4330       output_operand_lossage ("ia64_print_operand: unknown code");
4331       return;
4332     }
4333
4334   switch (GET_CODE (x))
4335     {
4336       /* This happens for the spill/restore instructions.  */
4337     case POST_INC:
4338     case POST_DEC:
4339     case POST_MODIFY:
4340       x = XEXP (x, 0);
4341       /* ... fall through ...  */
4342
4343     case REG:
4344       fputs (reg_names [REGNO (x)], file);
4345       break;
4346
4347     case MEM:
4348       {
4349         rtx addr = XEXP (x, 0);
4350         if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4351           addr = XEXP (addr, 0);
4352         fprintf (file, "[%s]", reg_names [REGNO (addr)]);
4353         break;
4354       }
4355
4356     default:
4357       output_addr_const (file, x);
4358       break;
4359     }
4360
4361   return;
4362 }
4363 \f
4364 /* Compute a (partial) cost for rtx X.  Return true if the complete
4365    cost has been computed, and false if subexpressions should be
4366    scanned.  In either case, *TOTAL contains the cost result.  */
4367 /* ??? This is incomplete.  */
4368
4369 static bool
4370 ia64_rtx_costs (rtx x, int code, int outer_code, int *total)
4371 {
4372   switch (code)
4373     {
4374     case CONST_INT:
4375       switch (outer_code)
4376         {
4377         case SET:
4378           *total = CONST_OK_FOR_J (INTVAL (x)) ? 0 : COSTS_N_INSNS (1);
4379           return true;
4380         case PLUS:
4381           if (CONST_OK_FOR_I (INTVAL (x)))
4382             *total = 0;
4383           else if (CONST_OK_FOR_J (INTVAL (x)))
4384             *total = 1;
4385           else
4386             *total = COSTS_N_INSNS (1);
4387           return true;
4388         default:
4389           if (CONST_OK_FOR_K (INTVAL (x)) || CONST_OK_FOR_L (INTVAL (x)))
4390             *total = 0;
4391           else
4392             *total = COSTS_N_INSNS (1);
4393           return true;
4394         }
4395
4396     case CONST_DOUBLE:
4397       *total = COSTS_N_INSNS (1);
4398       return true;
4399
4400     case CONST:
4401     case SYMBOL_REF:
4402     case LABEL_REF:
4403       *total = COSTS_N_INSNS (3);
4404       return true;
4405
4406     case MULT:
4407       /* For multiplies wider than HImode, we have to go to the FPU,
4408          which normally involves copies.  Plus there's the latency
4409          of the multiply itself, and the latency of the instructions to
4410          transfer integer regs to FP regs.  */
4411       /* ??? Check for FP mode.  */
4412       if (GET_MODE_SIZE (GET_MODE (x)) > 2)
4413         *total = COSTS_N_INSNS (10);
4414       else
4415         *total = COSTS_N_INSNS (2);
4416       return true;
4417
4418     case PLUS:
4419     case MINUS:
4420     case ASHIFT:
4421     case ASHIFTRT:
4422     case LSHIFTRT:
4423       *total = COSTS_N_INSNS (1);
4424       return true;
4425
4426     case DIV:
4427     case UDIV:
4428     case MOD:
4429     case UMOD:
4430       /* We make divide expensive, so that divide-by-constant will be
4431          optimized to a multiply.  */
4432       *total = COSTS_N_INSNS (60);
4433       return true;
4434
4435     default:
4436       return false;
4437     }
4438 }
4439
4440 /* Calculate the cost of moving data from a register in class FROM to
4441    one in class TO, using MODE.  */
4442
4443 int
4444 ia64_register_move_cost (enum machine_mode mode, enum reg_class from,
4445                          enum reg_class to)
4446 {
4447   /* ADDL_REGS is the same as GR_REGS for movement purposes.  */
4448   if (to == ADDL_REGS)
4449     to = GR_REGS;
4450   if (from == ADDL_REGS)
4451     from = GR_REGS;
4452
4453   /* All costs are symmetric, so reduce cases by putting the
4454      lower number class as the destination.  */
4455   if (from < to)
4456     {
4457       enum reg_class tmp = to;
4458       to = from, from = tmp;
4459     }
4460
4461   /* Moving from FR<->GR in XFmode must be more expensive than 2,
4462      so that we get secondary memory reloads.  Between FR_REGS,
4463      we have to make this at least as expensive as MEMORY_MOVE_COST
4464      to avoid spectacularly poor register class preferencing.  */
4465   if (mode == XFmode)
4466     {
4467       if (to != GR_REGS || from != GR_REGS)
4468         return MEMORY_MOVE_COST (mode, to, 0);
4469       else
4470         return 3;
4471     }
4472
4473   switch (to)
4474     {
4475     case PR_REGS:
4476       /* Moving between PR registers takes two insns.  */
4477       if (from == PR_REGS)
4478         return 3;
4479       /* Moving between PR and anything but GR is impossible.  */
4480       if (from != GR_REGS)
4481         return MEMORY_MOVE_COST (mode, to, 0);
4482       break;
4483
4484     case BR_REGS:
4485       /* Moving between BR and anything but GR is impossible.  */
4486       if (from != GR_REGS && from != GR_AND_BR_REGS)
4487         return MEMORY_MOVE_COST (mode, to, 0);
4488       break;
4489
4490     case AR_I_REGS:
4491     case AR_M_REGS:
4492       /* Moving between AR and anything but GR is impossible.  */
4493       if (from != GR_REGS)
4494         return MEMORY_MOVE_COST (mode, to, 0);
4495       break;
4496
4497     case GR_REGS:
4498     case FR_REGS:
4499     case GR_AND_FR_REGS:
4500     case GR_AND_BR_REGS:
4501     case ALL_REGS:
4502       break;
4503
4504     default:
4505       abort ();
4506     }
4507
4508   return 2;
4509 }
4510
4511 /* This function returns the register class required for a secondary
4512    register when copying between one of the registers in CLASS, and X,
4513    using MODE.  A return value of NO_REGS means that no secondary register
4514    is required.  */
4515
4516 enum reg_class
4517 ia64_secondary_reload_class (enum reg_class class,
4518                              enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
4519 {
4520   int regno = -1;
4521
4522   if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
4523     regno = true_regnum (x);
4524
4525   switch (class)
4526     {
4527     case BR_REGS:
4528     case AR_M_REGS:
4529     case AR_I_REGS:
4530       /* ??? BR<->BR register copies can happen due to a bad gcse/cse/global
4531          interaction.  We end up with two pseudos with overlapping lifetimes
4532          both of which are equiv to the same constant, and both which need
4533          to be in BR_REGS.  This seems to be a cse bug.  cse_basic_block_end
4534          changes depending on the path length, which means the qty_first_reg
4535          check in make_regs_eqv can give different answers at different times.
4536          At some point I'll probably need a reload_indi pattern to handle
4537          this.
4538
4539          We can also get GR_AND_FR_REGS to BR_REGS/AR_REGS copies, where we
4540          wound up with a FP register from GR_AND_FR_REGS.  Extend that to all
4541          non-general registers for good measure.  */
4542       if (regno >= 0 && ! GENERAL_REGNO_P (regno))
4543         return GR_REGS;
4544
4545       /* This is needed if a pseudo used as a call_operand gets spilled to a
4546          stack slot.  */
4547       if (GET_CODE (x) == MEM)
4548         return GR_REGS;
4549       break;
4550
4551     case FR_REGS:
4552       /* Need to go through general registers to get to other class regs.  */
4553       if (regno >= 0 && ! (FR_REGNO_P (regno) || GENERAL_REGNO_P (regno)))
4554         return GR_REGS;
4555
4556       /* This can happen when a paradoxical subreg is an operand to the
4557          muldi3 pattern.  */
4558       /* ??? This shouldn't be necessary after instruction scheduling is
4559          enabled, because paradoxical subregs are not accepted by
4560          register_operand when INSN_SCHEDULING is defined.  Or alternatively,
4561          stop the paradoxical subreg stupidity in the *_operand functions
4562          in recog.c.  */
4563       if (GET_CODE (x) == MEM
4564           && (GET_MODE (x) == SImode || GET_MODE (x) == HImode
4565               || GET_MODE (x) == QImode))
4566         return GR_REGS;
4567
4568       /* This can happen because of the ior/and/etc patterns that accept FP
4569          registers as operands.  If the third operand is a constant, then it
4570          needs to be reloaded into a FP register.  */
4571       if (GET_CODE (x) == CONST_INT)
4572         return GR_REGS;
4573
4574       /* This can happen because of register elimination in a muldi3 insn.
4575          E.g. `26107 * (unsigned long)&u'.  */
4576       if (GET_CODE (x) == PLUS)
4577         return GR_REGS;
4578       break;
4579
4580     case PR_REGS:
4581       /* ??? This happens if we cse/gcse a BImode value across a call,
4582          and the function has a nonlocal goto.  This is because global
4583          does not allocate call crossing pseudos to hard registers when
4584          current_function_has_nonlocal_goto is true.  This is relatively
4585          common for C++ programs that use exceptions.  To reproduce,
4586          return NO_REGS and compile libstdc++.  */
4587       if (GET_CODE (x) == MEM)
4588         return GR_REGS;
4589
4590       /* This can happen when we take a BImode subreg of a DImode value,
4591          and that DImode value winds up in some non-GR register.  */
4592       if (regno >= 0 && ! GENERAL_REGNO_P (regno) && ! PR_REGNO_P (regno))
4593         return GR_REGS;
4594       break;
4595
4596     default:
4597       break;
4598     }
4599
4600   return NO_REGS;
4601 }
4602
4603 \f
4604 /* Emit text to declare externally defined variables and functions, because
4605    the Intel assembler does not support undefined externals.  */
4606
4607 void
4608 ia64_asm_output_external (FILE *file, tree decl, const char *name)
4609 {
4610   int save_referenced;
4611
4612   /* GNU as does not need anything here, but the HP linker does need
4613      something for external functions.  */
4614
4615   if (TARGET_GNU_AS
4616       && (!TARGET_HPUX_LD
4617           || TREE_CODE (decl) != FUNCTION_DECL
4618           || strstr (name, "__builtin_") == name))
4619     return;
4620
4621   /* ??? The Intel assembler creates a reference that needs to be satisfied by
4622      the linker when we do this, so we need to be careful not to do this for
4623      builtin functions which have no library equivalent.  Unfortunately, we
4624      can't tell here whether or not a function will actually be called by
4625      expand_expr, so we pull in library functions even if we may not need
4626      them later.  */
4627   if (! strcmp (name, "__builtin_next_arg")
4628       || ! strcmp (name, "alloca")
4629       || ! strcmp (name, "__builtin_constant_p")
4630       || ! strcmp (name, "__builtin_args_info"))
4631     return;
4632
4633   if (TARGET_HPUX_LD)
4634     ia64_hpux_add_extern_decl (decl);
4635   else
4636     {
4637       /* assemble_name will set TREE_SYMBOL_REFERENCED, so we must save and
4638          restore it.  */
4639       save_referenced = TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl));
4640       if (TREE_CODE (decl) == FUNCTION_DECL)
4641         ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
4642       (*targetm.asm_out.globalize_label) (file, name);
4643       TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = save_referenced;
4644     }
4645 }
4646 \f
4647 /* Parse the -mfixed-range= option string.  */
4648
4649 static void
4650 fix_range (const char *const_str)
4651 {
4652   int i, first, last;
4653   char *str, *dash, *comma;
4654
4655   /* str must be of the form REG1'-'REG2{,REG1'-'REG} where REG1 and
4656      REG2 are either register names or register numbers.  The effect
4657      of this option is to mark the registers in the range from REG1 to
4658      REG2 as ``fixed'' so they won't be used by the compiler.  This is
4659      used, e.g., to ensure that kernel mode code doesn't use f32-f127.  */
4660
4661   i = strlen (const_str);
4662   str = (char *) alloca (i + 1);
4663   memcpy (str, const_str, i + 1);
4664
4665   while (1)
4666     {
4667       dash = strchr (str, '-');
4668       if (!dash)
4669         {
4670           warning ("value of -mfixed-range must have form REG1-REG2");
4671           return;
4672         }
4673       *dash = '\0';
4674
4675       comma = strchr (dash + 1, ',');
4676       if (comma)
4677         *comma = '\0';
4678
4679       first = decode_reg_name (str);
4680       if (first < 0)
4681         {
4682           warning ("unknown register name: %s", str);
4683           return;
4684         }
4685
4686       last = decode_reg_name (dash + 1);
4687       if (last < 0)
4688         {
4689           warning ("unknown register name: %s", dash + 1);
4690           return;
4691         }
4692
4693       *dash = '-';
4694
4695       if (first > last)
4696         {
4697           warning ("%s-%s is an empty range", str, dash + 1);
4698           return;
4699         }
4700
4701       for (i = first; i <= last; ++i)
4702         fixed_regs[i] = call_used_regs[i] = 1;
4703
4704       if (!comma)
4705         break;
4706
4707       *comma = ',';
4708       str = comma + 1;
4709     }
4710 }
4711
4712 static struct machine_function *
4713 ia64_init_machine_status (void)
4714 {
4715   return ggc_alloc_cleared (sizeof (struct machine_function));
4716 }
4717
4718 /* Handle TARGET_OPTIONS switches.  */
4719
4720 void
4721 ia64_override_options (void)
4722 {
4723   static struct pta
4724     {
4725       const char *const name;           /* processor name or nickname.  */
4726       const enum processor_type processor;
4727     }
4728   const processor_alias_table[] =
4729     {
4730       {"itanium", PROCESSOR_ITANIUM},
4731       {"itanium1", PROCESSOR_ITANIUM},
4732       {"merced", PROCESSOR_ITANIUM},
4733       {"itanium2", PROCESSOR_ITANIUM2},
4734       {"mckinley", PROCESSOR_ITANIUM2},
4735     };
4736
4737   int const pta_size = ARRAY_SIZE (processor_alias_table);
4738   int i;
4739
4740   if (TARGET_AUTO_PIC)
4741     target_flags |= MASK_CONST_GP;
4742
4743   if (TARGET_INLINE_FLOAT_DIV_LAT && TARGET_INLINE_FLOAT_DIV_THR)
4744     {
4745       if ((target_flags_explicit & MASK_INLINE_FLOAT_DIV_LAT)
4746            && (target_flags_explicit & MASK_INLINE_FLOAT_DIV_THR))
4747         {
4748           warning ("cannot optimize floating point division for both latency and throughput");
4749           target_flags &= ~MASK_INLINE_FLOAT_DIV_THR;
4750         }
4751       else 
4752         {
4753           if (target_flags_explicit & MASK_INLINE_FLOAT_DIV_THR)
4754             target_flags &= ~MASK_INLINE_FLOAT_DIV_LAT;
4755           else
4756             target_flags &= ~MASK_INLINE_FLOAT_DIV_THR;
4757         }
4758     }
4759
4760   if (TARGET_INLINE_INT_DIV_LAT && TARGET_INLINE_INT_DIV_THR)
4761     {
4762       if ((target_flags_explicit & MASK_INLINE_INT_DIV_LAT)
4763            && (target_flags_explicit & MASK_INLINE_INT_DIV_THR))
4764         {
4765           warning ("cannot optimize integer division for both latency and throughput");
4766           target_flags &= ~MASK_INLINE_INT_DIV_THR;
4767         }
4768       else 
4769         {
4770           if (target_flags_explicit & MASK_INLINE_INT_DIV_THR)
4771             target_flags &= ~MASK_INLINE_INT_DIV_LAT;
4772           else
4773             target_flags &= ~MASK_INLINE_INT_DIV_THR;
4774         }
4775     }
4776
4777   if (TARGET_INLINE_SQRT_LAT && TARGET_INLINE_SQRT_THR)
4778     {
4779       if ((target_flags_explicit & MASK_INLINE_SQRT_LAT)
4780            && (target_flags_explicit & MASK_INLINE_SQRT_THR))
4781         {
4782           warning ("cannot optimize square root for both latency and throughput");
4783           target_flags &= ~MASK_INLINE_SQRT_THR;
4784         }
4785       else 
4786         {
4787           if (target_flags_explicit & MASK_INLINE_SQRT_THR)
4788             target_flags &= ~MASK_INLINE_SQRT_LAT;
4789           else
4790             target_flags &= ~MASK_INLINE_SQRT_THR;
4791         }
4792     }
4793
4794   if (TARGET_INLINE_SQRT_LAT)
4795     {
4796       warning ("not yet implemented: latency-optimized inline square root");
4797       target_flags &= ~MASK_INLINE_SQRT_LAT;
4798     }
4799
4800   if (ia64_fixed_range_string)
4801     fix_range (ia64_fixed_range_string);
4802
4803   if (ia64_tls_size_string)
4804     {
4805       char *end;
4806       unsigned long tmp = strtoul (ia64_tls_size_string, &end, 10);
4807       if (*end || (tmp != 14 && tmp != 22 && tmp != 64))
4808         error ("bad value (%s) for -mtls-size= switch", ia64_tls_size_string);
4809       else
4810         ia64_tls_size = tmp;
4811     }
4812
4813   if (!ia64_tune_string)
4814     ia64_tune_string = "itanium2";
4815
4816   for (i = 0; i < pta_size; i++)
4817     if (! strcmp (ia64_tune_string, processor_alias_table[i].name))
4818       {
4819         ia64_tune = processor_alias_table[i].processor;
4820         break;
4821       }
4822
4823   if (i == pta_size)
4824     error ("bad value (%s) for -tune= switch", ia64_tune_string);
4825
4826   ia64_flag_schedule_insns2 = flag_schedule_insns_after_reload;
4827   flag_schedule_insns_after_reload = 0;
4828
4829   /* Variable tracking should be run after all optimizations which change order
4830      of insns.  It also needs a valid CFG.  */
4831   ia64_flag_var_tracking = flag_var_tracking;
4832   flag_var_tracking = 0;
4833
4834   ia64_section_threshold = g_switch_set ? g_switch_value : IA64_DEFAULT_GVALUE;
4835
4836   init_machine_status = ia64_init_machine_status;
4837 }
4838 \f
4839 static enum attr_itanium_class ia64_safe_itanium_class (rtx);
4840 static enum attr_type ia64_safe_type (rtx);
4841
4842 static enum attr_itanium_class
4843 ia64_safe_itanium_class (rtx insn)
4844 {
4845   if (recog_memoized (insn) >= 0)
4846     return get_attr_itanium_class (insn);
4847   else
4848     return ITANIUM_CLASS_UNKNOWN;
4849 }
4850
4851 static enum attr_type
4852 ia64_safe_type (rtx insn)
4853 {
4854   if (recog_memoized (insn) >= 0)
4855     return get_attr_type (insn);
4856   else
4857     return TYPE_UNKNOWN;
4858 }
4859 \f
4860 /* The following collection of routines emit instruction group stop bits as
4861    necessary to avoid dependencies.  */
4862
4863 /* Need to track some additional registers as far as serialization is
4864    concerned so we can properly handle br.call and br.ret.  We could
4865    make these registers visible to gcc, but since these registers are
4866    never explicitly used in gcc generated code, it seems wasteful to
4867    do so (plus it would make the call and return patterns needlessly
4868    complex).  */
4869 #define REG_RP          (BR_REG (0))
4870 #define REG_AR_CFM      (FIRST_PSEUDO_REGISTER + 1)
4871 /* This is used for volatile asms which may require a stop bit immediately
4872    before and after them.  */
4873 #define REG_VOLATILE    (FIRST_PSEUDO_REGISTER + 2)
4874 #define AR_UNAT_BIT_0   (FIRST_PSEUDO_REGISTER + 3)
4875 #define NUM_REGS        (AR_UNAT_BIT_0 + 64)
4876
4877 /* For each register, we keep track of how it has been written in the
4878    current instruction group.
4879
4880    If a register is written unconditionally (no qualifying predicate),
4881    WRITE_COUNT is set to 2 and FIRST_PRED is ignored.
4882
4883    If a register is written if its qualifying predicate P is true, we
4884    set WRITE_COUNT to 1 and FIRST_PRED to P.  Later on, the same register
4885    may be written again by the complement of P (P^1) and when this happens,
4886    WRITE_COUNT gets set to 2.
4887
4888    The result of this is that whenever an insn attempts to write a register
4889    whose WRITE_COUNT is two, we need to issue an insn group barrier first.
4890
4891    If a predicate register is written by a floating-point insn, we set
4892    WRITTEN_BY_FP to true.
4893
4894    If a predicate register is written by an AND.ORCM we set WRITTEN_BY_AND
4895    to true; if it was written by an OR.ANDCM we set WRITTEN_BY_OR to true.  */
4896
4897 struct reg_write_state
4898 {
4899   unsigned int write_count : 2;
4900   unsigned int first_pred : 16;
4901   unsigned int written_by_fp : 1;
4902   unsigned int written_by_and : 1;
4903   unsigned int written_by_or : 1;
4904 };
4905
4906 /* Cumulative info for the current instruction group.  */
4907 struct reg_write_state rws_sum[NUM_REGS];
4908 /* Info for the current instruction.  This gets copied to rws_sum after a
4909    stop bit is emitted.  */
4910 struct reg_write_state rws_insn[NUM_REGS];
4911
4912 /* Indicates whether this is the first instruction after a stop bit,
4913    in which case we don't need another stop bit.  Without this, we hit
4914    the abort in ia64_variable_issue when scheduling an alloc.  */
4915 static int first_instruction;
4916
4917 /* Misc flags needed to compute RAW/WAW dependencies while we are traversing
4918    RTL for one instruction.  */
4919 struct reg_flags
4920 {
4921   unsigned int is_write : 1;    /* Is register being written?  */
4922   unsigned int is_fp : 1;       /* Is register used as part of an fp op?  */
4923   unsigned int is_branch : 1;   /* Is register used as part of a branch?  */
4924   unsigned int is_and : 1;      /* Is register used as part of and.orcm?  */
4925   unsigned int is_or : 1;       /* Is register used as part of or.andcm?  */
4926   unsigned int is_sibcall : 1;  /* Is this a sibling or normal call?  */
4927 };
4928
4929 static void rws_update (struct reg_write_state *, int, struct reg_flags, int);
4930 static int rws_access_regno (int, struct reg_flags, int);
4931 static int rws_access_reg (rtx, struct reg_flags, int);
4932 static void update_set_flags (rtx, struct reg_flags *, int *, rtx *);
4933 static int set_src_needs_barrier (rtx, struct reg_flags, int, rtx);
4934 static int rtx_needs_barrier (rtx, struct reg_flags, int);
4935 static void init_insn_group_barriers (void);
4936 static int group_barrier_needed_p (rtx);
4937 static int safe_group_barrier_needed_p (rtx);
4938
4939 /* Update *RWS for REGNO, which is being written by the current instruction,
4940    with predicate PRED, and associated register flags in FLAGS.  */
4941
4942 static void
4943 rws_update (struct reg_write_state *rws, int regno, struct reg_flags flags, int pred)
4944 {
4945   if (pred)
4946     rws[regno].write_count++;
4947   else
4948     rws[regno].write_count = 2;
4949   rws[regno].written_by_fp |= flags.is_fp;
4950   /* ??? Not tracking and/or across differing predicates.  */
4951   rws[regno].written_by_and = flags.is_and;
4952   rws[regno].written_by_or = flags.is_or;
4953   rws[regno].first_pred = pred;
4954 }
4955
4956 /* Handle an access to register REGNO of type FLAGS using predicate register
4957    PRED.  Update rws_insn and rws_sum arrays.  Return 1 if this access creates
4958    a dependency with an earlier instruction in the same group.  */
4959
4960 static int
4961 rws_access_regno (int regno, struct reg_flags flags, int pred)
4962 {
4963   int need_barrier = 0;
4964
4965   if (regno >= NUM_REGS)
4966     abort ();
4967
4968   if (! PR_REGNO_P (regno))
4969     flags.is_and = flags.is_or = 0;
4970
4971   if (flags.is_write)
4972     {
4973       int write_count;
4974
4975       /* One insn writes same reg multiple times?  */
4976       if (rws_insn[regno].write_count > 0)
4977         abort ();
4978
4979       /* Update info for current instruction.  */
4980       rws_update (rws_insn, regno, flags, pred);
4981       write_count = rws_sum[regno].write_count;
4982
4983       switch (write_count)
4984         {
4985         case 0:
4986           /* The register has not been written yet.  */
4987           rws_update (rws_sum, regno, flags, pred);
4988           break;
4989
4990         case 1:
4991           /* The register has been written via a predicate.  If this is
4992              not a complementary predicate, then we need a barrier.  */
4993           /* ??? This assumes that P and P+1 are always complementary
4994              predicates for P even.  */
4995           if (flags.is_and && rws_sum[regno].written_by_and)
4996             ;
4997           else if (flags.is_or && rws_sum[regno].written_by_or)
4998             ;
4999           else if ((rws_sum[regno].first_pred ^ 1) != pred)
5000             need_barrier = 1;
5001           rws_update (rws_sum, regno, flags, pred);
5002           break;
5003
5004         case 2:
5005           /* The register has been unconditionally written already.  We
5006              need a barrier.  */
5007           if (flags.is_and && rws_sum[regno].written_by_and)
5008             ;
5009           else if (flags.is_or && rws_sum[regno].written_by_or)
5010             ;
5011           else
5012             need_barrier = 1;
5013           rws_sum[regno].written_by_and = flags.is_and;
5014           rws_sum[regno].written_by_or = flags.is_or;
5015           break;
5016
5017         default:
5018           abort ();
5019         }
5020     }
5021   else
5022     {
5023       if (flags.is_branch)
5024         {
5025           /* Branches have several RAW exceptions that allow to avoid
5026              barriers.  */
5027
5028           if (REGNO_REG_CLASS (regno) == BR_REGS || regno == AR_PFS_REGNUM)
5029             /* RAW dependencies on branch regs are permissible as long
5030                as the writer is a non-branch instruction.  Since we
5031                never generate code that uses a branch register written
5032                by a branch instruction, handling this case is
5033                easy.  */
5034             return 0;
5035
5036           if (REGNO_REG_CLASS (regno) == PR_REGS
5037               && ! rws_sum[regno].written_by_fp)
5038             /* The predicates of a branch are available within the
5039                same insn group as long as the predicate was written by
5040                something other than a floating-point instruction.  */
5041             return 0;
5042         }
5043
5044       if (flags.is_and && rws_sum[regno].written_by_and)
5045         return 0;
5046       if (flags.is_or && rws_sum[regno].written_by_or)
5047         return 0;
5048
5049       switch (rws_sum[regno].write_count)
5050         {
5051         case 0:
5052           /* The register has not been written yet.  */
5053           break;
5054
5055         case 1:
5056           /* The register has been written via a predicate.  If this is
5057              not a complementary predicate, then we need a barrier.  */
5058           /* ??? This assumes that P and P+1 are always complementary
5059              predicates for P even.  */
5060           if ((rws_sum[regno].first_pred ^ 1) != pred)
5061             need_barrier = 1;
5062           break;
5063
5064         case 2:
5065           /* The register has been unconditionally written already.  We
5066              need a barrier.  */
5067           need_barrier = 1;
5068           break;
5069
5070         default:
5071           abort ();
5072         }
5073     }
5074
5075   return need_barrier;
5076 }
5077
5078 static int
5079 rws_access_reg (rtx reg, struct reg_flags flags, int pred)
5080 {
5081   int regno = REGNO (reg);
5082   int n = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
5083
5084   if (n == 1)
5085     return rws_access_regno (regno, flags, pred);
5086   else
5087     {
5088       int need_barrier = 0;
5089       while (--n >= 0)
5090         need_barrier |= rws_access_regno (regno + n, flags, pred);
5091       return need_barrier;
5092     }
5093 }
5094
5095 /* Examine X, which is a SET rtx, and update the flags, the predicate, and
5096    the condition, stored in *PFLAGS, *PPRED and *PCOND.  */
5097
5098 static void
5099 update_set_flags (rtx x, struct reg_flags *pflags, int *ppred, rtx *pcond)
5100 {
5101   rtx src = SET_SRC (x);
5102
5103   *pcond = 0;
5104
5105   switch (GET_CODE (src))
5106     {
5107     case CALL:
5108       return;
5109
5110     case IF_THEN_ELSE:
5111       if (SET_DEST (x) == pc_rtx)
5112         /* X is a conditional branch.  */
5113         return;
5114       else
5115         {
5116           int is_complemented = 0;
5117
5118           /* X is a conditional move.  */
5119           rtx cond = XEXP (src, 0);
5120           if (GET_CODE (cond) == EQ)
5121             is_complemented = 1;
5122           cond = XEXP (cond, 0);
5123           if (GET_CODE (cond) != REG
5124               && REGNO_REG_CLASS (REGNO (cond)) != PR_REGS)
5125             abort ();
5126           *pcond = cond;
5127           if (XEXP (src, 1) == SET_DEST (x)
5128               || XEXP (src, 2) == SET_DEST (x))
5129             {
5130               /* X is a conditional move that conditionally writes the
5131                  destination.  */
5132
5133               /* We need another complement in this case.  */
5134               if (XEXP (src, 1) == SET_DEST (x))
5135                 is_complemented = ! is_complemented;
5136
5137               *ppred = REGNO (cond);
5138               if (is_complemented)
5139                 ++*ppred;
5140             }
5141
5142           /* ??? If this is a conditional write to the dest, then this
5143              instruction does not actually read one source.  This probably
5144              doesn't matter, because that source is also the dest.  */
5145           /* ??? Multiple writes to predicate registers are allowed
5146              if they are all AND type compares, or if they are all OR
5147              type compares.  We do not generate such instructions
5148              currently.  */
5149         }
5150       /* ... fall through ...  */
5151
5152     default:
5153       if (COMPARISON_P (src)
5154           && GET_MODE_CLASS (GET_MODE (XEXP (src, 0))) == MODE_FLOAT)
5155         /* Set pflags->is_fp to 1 so that we know we're dealing
5156            with a floating point comparison when processing the
5157            destination of the SET.  */
5158         pflags->is_fp = 1;
5159
5160       /* Discover if this is a parallel comparison.  We only handle
5161          and.orcm and or.andcm at present, since we must retain a
5162          strict inverse on the predicate pair.  */
5163       else if (GET_CODE (src) == AND)
5164         pflags->is_and = 1;
5165       else if (GET_CODE (src) == IOR)
5166         pflags->is_or = 1;
5167
5168       break;
5169     }
5170 }
5171
5172 /* Subroutine of rtx_needs_barrier; this function determines whether the
5173    source of a given SET rtx found in X needs a barrier.  FLAGS and PRED
5174    are as in rtx_needs_barrier.  COND is an rtx that holds the condition
5175    for this insn.  */
5176
5177 static int
5178 set_src_needs_barrier (rtx x, struct reg_flags flags, int pred, rtx cond)
5179 {
5180   int need_barrier = 0;
5181   rtx dst;
5182   rtx src = SET_SRC (x);
5183
5184   if (GET_CODE (src) == CALL)
5185     /* We don't need to worry about the result registers that
5186        get written by subroutine call.  */
5187     return rtx_needs_barrier (src, flags, pred);
5188   else if (SET_DEST (x) == pc_rtx)
5189     {
5190       /* X is a conditional branch.  */
5191       /* ??? This seems redundant, as the caller sets this bit for
5192          all JUMP_INSNs.  */
5193       flags.is_branch = 1;
5194       return rtx_needs_barrier (src, flags, pred);
5195     }
5196
5197   need_barrier = rtx_needs_barrier (src, flags, pred);
5198
5199   /* This instruction unconditionally uses a predicate register.  */
5200   if (cond)
5201     need_barrier |= rws_access_reg (cond, flags, 0);
5202
5203   dst = SET_DEST (x);
5204   if (GET_CODE (dst) == ZERO_EXTRACT)
5205     {
5206       need_barrier |= rtx_needs_barrier (XEXP (dst, 1), flags, pred);
5207       need_barrier |= rtx_needs_barrier (XEXP (dst, 2), flags, pred);
5208       dst = XEXP (dst, 0);
5209     }
5210   return need_barrier;
5211 }
5212
5213 /* Handle an access to rtx X of type FLAGS using predicate register
5214    PRED.  Return 1 if this access creates a dependency with an earlier
5215    instruction in the same group.  */
5216
5217 static int
5218 rtx_needs_barrier (rtx x, struct reg_flags flags, int pred)
5219 {
5220   int i, j;
5221   int is_complemented = 0;
5222   int need_barrier = 0;
5223   const char *format_ptr;
5224   struct reg_flags new_flags;
5225   rtx cond = 0;
5226
5227   if (! x)
5228     return 0;
5229
5230   new_flags = flags;
5231
5232   switch (GET_CODE (x))
5233     {
5234     case SET:
5235       update_set_flags (x, &new_flags, &pred, &cond);
5236       need_barrier = set_src_needs_barrier (x, new_flags, pred, cond);
5237       if (GET_CODE (SET_SRC (x)) != CALL)
5238         {
5239           new_flags.is_write = 1;
5240           need_barrier |= rtx_needs_barrier (SET_DEST (x), new_flags, pred);
5241         }
5242       break;
5243
5244     case CALL:
5245       new_flags.is_write = 0;
5246       need_barrier |= rws_access_regno (AR_EC_REGNUM, new_flags, pred);
5247
5248       /* Avoid multiple register writes, in case this is a pattern with
5249          multiple CALL rtx.  This avoids an abort in rws_access_reg.  */
5250       if (! flags.is_sibcall && ! rws_insn[REG_AR_CFM].write_count)
5251         {
5252           new_flags.is_write = 1;
5253           need_barrier |= rws_access_regno (REG_RP, new_flags, pred);
5254           need_barrier |= rws_access_regno (AR_PFS_REGNUM, new_flags, pred);
5255           need_barrier |= rws_access_regno (REG_AR_CFM, new_flags, pred);
5256         }
5257       break;
5258
5259     case COND_EXEC:
5260       /* X is a predicated instruction.  */
5261
5262       cond = COND_EXEC_TEST (x);
5263       if (pred)
5264         abort ();
5265       need_barrier = rtx_needs_barrier (cond, flags, 0);
5266
5267       if (GET_CODE (cond) == EQ)
5268         is_complemented = 1;
5269       cond = XEXP (cond, 0);
5270       if (GET_CODE (cond) != REG
5271           && REGNO_REG_CLASS (REGNO (cond)) != PR_REGS)
5272         abort ();
5273       pred = REGNO (cond);
5274       if (is_complemented)
5275         ++pred;
5276
5277       need_barrier |= rtx_needs_barrier (COND_EXEC_CODE (x), flags, pred);
5278       return need_barrier;
5279
5280     case CLOBBER:
5281     case USE:
5282       /* Clobber & use are for earlier compiler-phases only.  */
5283       break;
5284
5285     case ASM_OPERANDS:
5286     case ASM_INPUT:
5287       /* We always emit stop bits for traditional asms.  We emit stop bits
5288          for volatile extended asms if TARGET_VOL_ASM_STOP is true.  */
5289       if (GET_CODE (x) != ASM_OPERANDS
5290           || (MEM_VOLATILE_P (x) && TARGET_VOL_ASM_STOP))
5291         {
5292           /* Avoid writing the register multiple times if we have multiple
5293              asm outputs.  This avoids an abort in rws_access_reg.  */
5294           if (! rws_insn[REG_VOLATILE].write_count)
5295             {
5296               new_flags.is_write = 1;
5297               rws_access_regno (REG_VOLATILE, new_flags, pred);
5298             }
5299           return 1;
5300         }
5301
5302       /* For all ASM_OPERANDS, we must traverse the vector of input operands.
5303          We can not just fall through here since then we would be confused
5304          by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
5305          traditional asms unlike their normal usage.  */
5306
5307       for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; --i)
5308         if (rtx_needs_barrier (ASM_OPERANDS_INPUT (x, i), flags, pred))
5309           need_barrier = 1;
5310       break;
5311
5312     case PARALLEL:
5313       for (i = XVECLEN (x, 0) - 1; i >= 0; --i)
5314         {
5315           rtx pat = XVECEXP (x, 0, i);
5316           if (GET_CODE (pat) == SET)
5317             {
5318               update_set_flags (pat, &new_flags, &pred, &cond);
5319               need_barrier |= set_src_needs_barrier (pat, new_flags, pred, cond);
5320             }
5321           else if (GET_CODE (pat) == USE
5322                    || GET_CODE (pat) == CALL
5323                    || GET_CODE (pat) == ASM_OPERANDS)
5324             need_barrier |= rtx_needs_barrier (pat, flags, pred);
5325           else if (GET_CODE (pat) != CLOBBER && GET_CODE (pat) != RETURN)
5326             abort ();
5327         }
5328       for (i = XVECLEN (x, 0) - 1; i >= 0; --i)
5329         {
5330           rtx pat = XVECEXP (x, 0, i);
5331           if (GET_CODE (pat) == SET)
5332             {
5333               if (GET_CODE (SET_SRC (pat)) != CALL)
5334                 {
5335                   new_flags.is_write = 1;
5336                   need_barrier |= rtx_needs_barrier (SET_DEST (pat), new_flags,
5337                                                      pred);
5338                 }
5339             }
5340           else if (GET_CODE (pat) == CLOBBER || GET_CODE (pat) == RETURN)
5341             need_barrier |= rtx_needs_barrier (pat, flags, pred);
5342         }
5343       break;
5344
5345     case SUBREG:
5346       x = SUBREG_REG (x);
5347       /* FALLTHRU */
5348     case REG:
5349       if (REGNO (x) == AR_UNAT_REGNUM)
5350         {
5351           for (i = 0; i < 64; ++i)
5352             need_barrier |= rws_access_regno (AR_UNAT_BIT_0 + i, flags, pred);
5353         }
5354       else
5355         need_barrier = rws_access_reg (x, flags, pred);
5356       break;
5357
5358     case MEM:
5359       /* Find the regs used in memory address computation.  */
5360       new_flags.is_write = 0;
5361       need_barrier = rtx_needs_barrier (XEXP (x, 0), new_flags, pred);
5362       break;
5363
5364     case CONST_INT:   case CONST_DOUBLE:
5365     case SYMBOL_REF:  case LABEL_REF:     case CONST:
5366       break;
5367
5368       /* Operators with side-effects.  */
5369     case POST_INC:    case POST_DEC:
5370       if (GET_CODE (XEXP (x, 0)) != REG)
5371         abort ();
5372
5373       new_flags.is_write = 0;
5374       need_barrier  = rws_access_reg (XEXP (x, 0), new_flags, pred);
5375       new_flags.is_write = 1;
5376       need_barrier |= rws_access_reg (XEXP (x, 0), new_flags, pred);
5377       break;
5378
5379     case POST_MODIFY:
5380       if (GET_CODE (XEXP (x, 0)) != REG)
5381         abort ();
5382
5383       new_flags.is_write = 0;
5384       need_barrier  = rws_access_reg (XEXP (x, 0), new_flags, pred);
5385       need_barrier |= rtx_needs_barrier (XEXP (x, 1), new_flags, pred);
5386       new_flags.is_write = 1;
5387       need_barrier |= rws_access_reg (XEXP (x, 0), new_flags, pred);
5388       break;
5389
5390       /* Handle common unary and binary ops for efficiency.  */
5391     case COMPARE:  case PLUS:    case MINUS:   case MULT:      case DIV:
5392     case MOD:      case UDIV:    case UMOD:    case AND:       case IOR:
5393     case XOR:      case ASHIFT:  case ROTATE:  case ASHIFTRT:  case LSHIFTRT:
5394     case ROTATERT: case SMIN:    case SMAX:    case UMIN:      case UMAX:
5395     case NE:       case EQ:      case GE:      case GT:        case LE:
5396     case LT:       case GEU:     case GTU:     case LEU:       case LTU:
5397       need_barrier = rtx_needs_barrier (XEXP (x, 0), new_flags, pred);
5398       need_barrier |= rtx_needs_barrier (XEXP (x, 1), new_flags, pred);
5399       break;
5400
5401     case NEG:      case NOT:            case SIGN_EXTEND:     case ZERO_EXTEND:
5402     case TRUNCATE: case FLOAT_EXTEND:   case FLOAT_TRUNCATE:  case FLOAT:
5403     case FIX:      case UNSIGNED_FLOAT: case UNSIGNED_FIX:    case ABS:
5404     case SQRT:     case FFS:            case POPCOUNT:
5405       need_barrier = rtx_needs_barrier (XEXP (x, 0), flags, pred);
5406       break;
5407
5408     case UNSPEC:
5409       switch (XINT (x, 1))
5410         {
5411         case UNSPEC_LTOFF_DTPMOD:
5412         case UNSPEC_LTOFF_DTPREL:
5413         case UNSPEC_DTPREL:
5414         case UNSPEC_LTOFF_TPREL:
5415         case UNSPEC_TPREL:
5416         case UNSPEC_PRED_REL_MUTEX:
5417         case UNSPEC_PIC_CALL:
5418         case UNSPEC_MF:
5419         case UNSPEC_FETCHADD_ACQ:
5420         case UNSPEC_BSP_VALUE:
5421         case UNSPEC_FLUSHRS:
5422         case UNSPEC_BUNDLE_SELECTOR:
5423           break;
5424
5425         case UNSPEC_GR_SPILL:
5426         case UNSPEC_GR_RESTORE:
5427           {
5428             HOST_WIDE_INT offset = INTVAL (XVECEXP (x, 0, 1));
5429             HOST_WIDE_INT bit = (offset >> 3) & 63;
5430
5431             need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
5432             new_flags.is_write = (XINT (x, 1) == 1);
5433             need_barrier |= rws_access_regno (AR_UNAT_BIT_0 + bit,
5434                                               new_flags, pred);
5435             break;
5436           }
5437
5438         case UNSPEC_FR_SPILL:
5439         case UNSPEC_FR_RESTORE:
5440         case UNSPEC_GETF_EXP:
5441         case UNSPEC_SETF_EXP:
5442         case UNSPEC_ADDP4:
5443         case UNSPEC_FR_SQRT_RECIP_APPROX:
5444           need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
5445           break;
5446
5447         case UNSPEC_FR_RECIP_APPROX:
5448           need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
5449           need_barrier |= rtx_needs_barrier (XVECEXP (x, 0, 1), flags, pred);
5450           break;
5451
5452         case UNSPEC_CMPXCHG_ACQ:
5453           need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 1), flags, pred);
5454           need_barrier |= rtx_needs_barrier (XVECEXP (x, 0, 2), flags, pred);
5455           break;
5456
5457         default:
5458           abort ();
5459         }
5460       break;
5461
5462     case UNSPEC_VOLATILE:
5463       switch (XINT (x, 1))
5464         {
5465         case UNSPECV_ALLOC:
5466           /* Alloc must always be the first instruction of a group.
5467              We force this by always returning true.  */
5468           /* ??? We might get better scheduling if we explicitly check for
5469              input/local/output register dependencies, and modify the
5470              scheduler so that alloc is always reordered to the start of
5471              the current group.  We could then eliminate all of the
5472              first_instruction code.  */
5473           rws_access_regno (AR_PFS_REGNUM, flags, pred);
5474
5475           new_flags.is_write = 1;
5476           rws_access_regno (REG_AR_CFM, new_flags, pred);
5477           return 1;
5478
5479         case UNSPECV_SET_BSP:
5480           need_barrier = 1;
5481           break;
5482
5483         case UNSPECV_BLOCKAGE:
5484         case UNSPECV_INSN_GROUP_BARRIER:
5485         case UNSPECV_BREAK:
5486         case UNSPECV_PSAC_ALL:
5487         case UNSPECV_PSAC_NORMAL:
5488           return 0;
5489
5490         default:
5491           abort ();
5492         }
5493       break;
5494
5495     case RETURN:
5496       new_flags.is_write = 0;
5497       need_barrier  = rws_access_regno (REG_RP, flags, pred);
5498       need_barrier |= rws_access_regno (AR_PFS_REGNUM, flags, pred);
5499
5500       new_flags.is_write = 1;
5501       need_barrier |= rws_access_regno (AR_EC_REGNUM, new_flags, pred);
5502       need_barrier |= rws_access_regno (REG_AR_CFM, new_flags, pred);
5503       break;
5504
5505     default:
5506       format_ptr = GET_RTX_FORMAT (GET_CODE (x));
5507       for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5508         switch (format_ptr[i])
5509           {
5510           case '0':     /* unused field */
5511           case 'i':     /* integer */
5512           case 'n':     /* note */
5513           case 'w':     /* wide integer */
5514           case 's':     /* pointer to string */
5515           case 'S':     /* optional pointer to string */
5516             break;
5517
5518           case 'e':
5519             if (rtx_needs_barrier (XEXP (x, i), flags, pred))
5520               need_barrier = 1;
5521             break;
5522
5523           case 'E':
5524             for (j = XVECLEN (x, i) - 1; j >= 0; --j)
5525               if (rtx_needs_barrier (XVECEXP (x, i, j), flags, pred))
5526                 need_barrier = 1;
5527             break;
5528
5529           default:
5530             abort ();
5531           }
5532       break;
5533     }
5534   return need_barrier;
5535 }
5536
5537 /* Clear out the state for group_barrier_needed_p at the start of a
5538    sequence of insns.  */
5539
5540 static void
5541 init_insn_group_barriers (void)
5542 {
5543   memset (rws_sum, 0, sizeof (rws_sum));
5544   first_instruction = 1;
5545 }
5546
5547 /* Given the current state, recorded by previous calls to this function,
5548    determine whether a group barrier (a stop bit) is necessary before INSN.
5549    Return nonzero if so.  */
5550
5551 static int
5552 group_barrier_needed_p (rtx insn)
5553 {
5554   rtx pat;
5555   int need_barrier = 0;
5556   struct reg_flags flags;
5557
5558   memset (&flags, 0, sizeof (flags));
5559   switch (GET_CODE (insn))
5560     {
5561     case NOTE:
5562       break;
5563
5564     case BARRIER:
5565       /* A barrier doesn't imply an instruction group boundary.  */
5566       break;
5567
5568     case CODE_LABEL:
5569       memset (rws_insn, 0, sizeof (rws_insn));
5570       return 1;
5571
5572     case CALL_INSN:
5573       flags.is_branch = 1;
5574       flags.is_sibcall = SIBLING_CALL_P (insn);
5575       memset (rws_insn, 0, sizeof (rws_insn));
5576
5577       /* Don't bundle a call following another call.  */
5578       if ((pat = prev_active_insn (insn))
5579           && GET_CODE (pat) == CALL_INSN)
5580         {
5581           need_barrier = 1;
5582           break;
5583         }
5584
5585       need_barrier = rtx_needs_barrier (PATTERN (insn), flags, 0);
5586       break;
5587
5588     case JUMP_INSN:
5589       flags.is_branch = 1;
5590
5591       /* Don't bundle a jump following a call.  */
5592       if ((pat = prev_active_insn (insn))
5593           && GET_CODE (pat) == CALL_INSN)
5594         {
5595           need_barrier = 1;
5596           break;
5597         }
5598       /* FALLTHRU */
5599
5600     case INSN:
5601       if (GET_CODE (PATTERN (insn)) == USE
5602           || GET_CODE (PATTERN (insn)) == CLOBBER)
5603         /* Don't care about USE and CLOBBER "insns"---those are used to
5604            indicate to the optimizer that it shouldn't get rid of
5605            certain operations.  */
5606         break;
5607
5608       pat = PATTERN (insn);
5609
5610       /* Ug.  Hack hacks hacked elsewhere.  */
5611       switch (recog_memoized (insn))
5612         {
5613           /* We play dependency tricks with the epilogue in order
5614              to get proper schedules.  Undo this for dv analysis.  */
5615         case CODE_FOR_epilogue_deallocate_stack:
5616         case CODE_FOR_prologue_allocate_stack:
5617           pat = XVECEXP (pat, 0, 0);
5618           break;
5619
5620           /* The pattern we use for br.cloop confuses the code above.
5621              The second element of the vector is representative.  */
5622         case CODE_FOR_doloop_end_internal:
5623           pat = XVECEXP (pat, 0, 1);
5624           break;
5625
5626           /* Doesn't generate code.  */
5627         case CODE_FOR_pred_rel_mutex:
5628         case CODE_FOR_prologue_use:
5629           return 0;
5630
5631         default:
5632           break;
5633         }
5634
5635       memset (rws_insn, 0, sizeof (rws_insn));
5636       need_barrier = rtx_needs_barrier (pat, flags, 0);
5637
5638       /* Check to see if the previous instruction was a volatile
5639          asm.  */
5640       if (! need_barrier)
5641         need_barrier = rws_access_regno (REG_VOLATILE, flags, 0);
5642       break;
5643
5644     default:
5645       abort ();
5646     }
5647
5648   if (first_instruction && INSN_P (insn)
5649       && ia64_safe_itanium_class (insn) != ITANIUM_CLASS_IGNORE
5650       && GET_CODE (PATTERN (insn)) != USE
5651       && GET_CODE (PATTERN (insn)) != CLOBBER)
5652     {
5653       need_barrier = 0;
5654       first_instruction = 0;
5655     }
5656
5657   return need_barrier;
5658 }
5659
5660 /* Like group_barrier_needed_p, but do not clobber the current state.  */
5661
5662 static int
5663 safe_group_barrier_needed_p (rtx insn)
5664 {
5665   struct reg_write_state rws_saved[NUM_REGS];
5666   int saved_first_instruction;
5667   int t;
5668
5669   memcpy (rws_saved, rws_sum, NUM_REGS * sizeof *rws_saved);
5670   saved_first_instruction = first_instruction;
5671
5672   t = group_barrier_needed_p (insn);
5673
5674   memcpy (rws_sum, rws_saved, NUM_REGS * sizeof *rws_saved);
5675   first_instruction = saved_first_instruction;
5676
5677   return t;
5678 }
5679
5680 /* Scan the current function and insert stop bits as necessary to
5681    eliminate dependencies.  This function assumes that a final
5682    instruction scheduling pass has been run which has already
5683    inserted most of the necessary stop bits.  This function only
5684    inserts new ones at basic block boundaries, since these are
5685    invisible to the scheduler.  */
5686
5687 static void
5688 emit_insn_group_barriers (FILE *dump)
5689 {
5690   rtx insn;
5691   rtx last_label = 0;
5692   int insns_since_last_label = 0;
5693
5694   init_insn_group_barriers ();
5695
5696   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5697     {
5698       if (GET_CODE (insn) == CODE_LABEL)
5699         {
5700           if (insns_since_last_label)
5701             last_label = insn;
5702           insns_since_last_label = 0;
5703         }
5704       else if (GET_CODE (insn) == NOTE
5705                && NOTE_LINE_NUMBER (insn) == NOTE_INSN_BASIC_BLOCK)
5706         {
5707           if (insns_since_last_label)
5708             last_label = insn;
5709           insns_since_last_label = 0;
5710         }
5711       else if (GET_CODE (insn) == INSN
5712                && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
5713                && XINT (PATTERN (insn), 1) == UNSPECV_INSN_GROUP_BARRIER)
5714         {
5715           init_insn_group_barriers ();
5716           last_label = 0;
5717         }
5718       else if (INSN_P (insn))
5719         {
5720           insns_since_last_label = 1;
5721
5722           if (group_barrier_needed_p (insn))
5723             {
5724               if (last_label)
5725                 {
5726                   if (dump)
5727                     fprintf (dump, "Emitting stop before label %d\n",
5728                              INSN_UID (last_label));
5729                   emit_insn_before (gen_insn_group_barrier (GEN_INT (3)), last_label);
5730                   insn = last_label;
5731
5732                   init_insn_group_barriers ();
5733                   last_label = 0;
5734                 }
5735             }
5736         }
5737     }
5738 }
5739
5740 /* Like emit_insn_group_barriers, but run if no final scheduling pass was run.
5741    This function has to emit all necessary group barriers.  */
5742
5743 static void
5744 emit_all_insn_group_barriers (FILE *dump ATTRIBUTE_UNUSED)
5745 {
5746   rtx insn;
5747
5748   init_insn_group_barriers ();
5749
5750   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5751     {
5752       if (GET_CODE (insn) == BARRIER)
5753         {
5754           rtx last = prev_active_insn (insn);
5755
5756           if (! last)
5757             continue;
5758           if (GET_CODE (last) == JUMP_INSN
5759               && GET_CODE (PATTERN (last)) == ADDR_DIFF_VEC)
5760             last = prev_active_insn (last);
5761           if (recog_memoized (last) != CODE_FOR_insn_group_barrier)
5762             emit_insn_after (gen_insn_group_barrier (GEN_INT (3)), last);
5763
5764           init_insn_group_barriers ();
5765         }
5766       else if (INSN_P (insn))
5767         {
5768           if (recog_memoized (insn) == CODE_FOR_insn_group_barrier)
5769             init_insn_group_barriers ();
5770           else if (group_barrier_needed_p (insn))
5771             {
5772               emit_insn_before (gen_insn_group_barrier (GEN_INT (3)), insn);
5773               init_insn_group_barriers ();
5774               group_barrier_needed_p (insn);
5775             }
5776         }
5777     }
5778 }
5779
5780 \f
5781 static int errata_find_address_regs (rtx *, void *);
5782 static void errata_emit_nops (rtx);
5783 static void fixup_errata (void);
5784
5785 /* This structure is used to track some details about the previous insns
5786    groups so we can determine if it may be necessary to insert NOPs to
5787    workaround hardware errata.  */
5788 static struct group
5789 {
5790   HARD_REG_SET p_reg_set;
5791   HARD_REG_SET gr_reg_conditionally_set;
5792 } last_group[2];
5793
5794 /* Index into the last_group array.  */
5795 static int group_idx;
5796
5797 /* Called through for_each_rtx; determines if a hard register that was
5798    conditionally set in the previous group is used as an address register.
5799    It ensures that for_each_rtx returns 1 in that case.  */
5800 static int
5801 errata_find_address_regs (rtx *xp, void *data ATTRIBUTE_UNUSED)
5802 {
5803   rtx x = *xp;
5804   if (GET_CODE (x) != MEM)
5805     return 0;
5806   x = XEXP (x, 0);
5807   if (GET_CODE (x) == POST_MODIFY)
5808     x = XEXP (x, 0);
5809   if (GET_CODE (x) == REG)
5810     {
5811       struct group *prev_group = last_group + (group_idx ^ 1);
5812       if (TEST_HARD_REG_BIT (prev_group->gr_reg_conditionally_set,
5813                              REGNO (x)))
5814         return 1;
5815       return -1;
5816     }
5817   return 0;
5818 }
5819
5820 /* Called for each insn; this function keeps track of the state in
5821    last_group and emits additional NOPs if necessary to work around
5822    an Itanium A/B step erratum.  */
5823 static void
5824 errata_emit_nops (rtx insn)
5825 {
5826   struct group *this_group = last_group + group_idx;
5827   struct group *prev_group = last_group + (group_idx ^ 1);
5828   rtx pat = PATTERN (insn);
5829   rtx cond = GET_CODE (pat) == COND_EXEC ? COND_EXEC_TEST (pat) : 0;
5830   rtx real_pat = cond ? COND_EXEC_CODE (pat) : pat;
5831   enum attr_type type;
5832   rtx set = real_pat;
5833
5834   if (GET_CODE (real_pat) == USE
5835       || GET_CODE (real_pat) == CLOBBER
5836       || GET_CODE (real_pat) == ASM_INPUT
5837       || GET_CODE (real_pat) == ADDR_VEC
5838       || GET_CODE (real_pat) == ADDR_DIFF_VEC
5839       || asm_noperands (PATTERN (insn)) >= 0)
5840     return;
5841
5842   /* single_set doesn't work for COND_EXEC insns, so we have to duplicate
5843      parts of it.  */
5844
5845   if (GET_CODE (set) == PARALLEL)
5846     {
5847       int i;
5848       set = XVECEXP (real_pat, 0, 0);
5849       for (i = 1; i < XVECLEN (real_pat, 0); i++)
5850         if (GET_CODE (XVECEXP (real_pat, 0, i)) != USE
5851             && GET_CODE (XVECEXP (real_pat, 0, i)) != CLOBBER)
5852           {
5853             set = 0;
5854             break;
5855           }
5856     }
5857
5858   if (set && GET_CODE (set) != SET)
5859     set = 0;
5860
5861   type  = get_attr_type (insn);
5862
5863   if (type == TYPE_F
5864       && set && REG_P (SET_DEST (set)) && PR_REGNO_P (REGNO (SET_DEST (set))))
5865     SET_HARD_REG_BIT (this_group->p_reg_set, REGNO (SET_DEST (set)));
5866
5867   if ((type == TYPE_M || type == TYPE_A) && cond && set
5868       && REG_P (SET_DEST (set))
5869       && GET_CODE (SET_SRC (set)) != PLUS
5870       && GET_CODE (SET_SRC (set)) != MINUS
5871       && (GET_CODE (SET_SRC (set)) != ASHIFT
5872           || !shladd_operand (XEXP (SET_SRC (set), 1), VOIDmode))
5873       && (GET_CODE (SET_SRC (set)) != MEM
5874           || GET_CODE (XEXP (SET_SRC (set), 0)) != POST_MODIFY)
5875       && GENERAL_REGNO_P (REGNO (SET_DEST (set))))
5876     {
5877       if (!COMPARISON_P (cond)
5878           || !REG_P (XEXP (cond, 0)))
5879         abort ();
5880
5881       if (TEST_HARD_REG_BIT (prev_group->p_reg_set, REGNO (XEXP (cond, 0))))
5882         SET_HARD_REG_BIT (this_group->gr_reg_conditionally_set, REGNO (SET_DEST (set)));
5883     }
5884   if (for_each_rtx (&real_pat, errata_find_address_regs, NULL))
5885     {
5886       emit_insn_before (gen_insn_group_barrier (GEN_INT (3)), insn);
5887       emit_insn_before (gen_nop (), insn);
5888       emit_insn_before (gen_insn_group_barrier (GEN_INT (3)), insn);
5889       group_idx = 0;
5890       memset (last_group, 0, sizeof last_group);
5891     }
5892 }
5893
5894 /* Emit extra nops if they are required to work around hardware errata.  */
5895
5896 static void
5897 fixup_errata (void)
5898 {
5899   rtx insn;
5900
5901   if (! TARGET_B_STEP)
5902     return;
5903
5904   group_idx = 0;
5905   memset (last_group, 0, sizeof last_group);
5906
5907   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5908     {
5909       if (!INSN_P (insn))
5910         continue;
5911
5912       if (ia64_safe_type (insn) == TYPE_S)
5913         {
5914           group_idx ^= 1;
5915           memset (last_group + group_idx, 0, sizeof last_group[group_idx]);
5916         }
5917       else
5918         errata_emit_nops (insn);
5919     }
5920 }
5921 \f
5922
5923 /* Instruction scheduling support.  */
5924
5925 #define NR_BUNDLES 10
5926
5927 /* A list of names of all available bundles.  */
5928
5929 static const char *bundle_name [NR_BUNDLES] =
5930 {
5931   ".mii",
5932   ".mmi",
5933   ".mfi",
5934   ".mmf",
5935 #if NR_BUNDLES == 10
5936   ".bbb",
5937   ".mbb",
5938 #endif
5939   ".mib",
5940   ".mmb",
5941   ".mfb",
5942   ".mlx"
5943 };
5944
5945 /* Nonzero if we should insert stop bits into the schedule.  */
5946
5947 int ia64_final_schedule = 0;
5948
5949 /* Codes of the corresponding quieryied units: */
5950
5951 static int _0mii_, _0mmi_, _0mfi_, _0mmf_;
5952 static int _0bbb_, _0mbb_, _0mib_, _0mmb_, _0mfb_, _0mlx_;
5953
5954 static int _1mii_, _1mmi_, _1mfi_, _1mmf_;
5955 static int _1bbb_, _1mbb_, _1mib_, _1mmb_, _1mfb_, _1mlx_;
5956
5957 static int pos_1, pos_2, pos_3, pos_4, pos_5, pos_6;
5958
5959 /* The following variable value is an insn group barrier.  */
5960
5961 static rtx dfa_stop_insn;
5962
5963 /* The following variable value is the last issued insn.  */
5964
5965 static rtx last_scheduled_insn;
5966
5967 /* The following variable value is size of the DFA state.  */
5968
5969 static size_t dfa_state_size;
5970
5971 /* The following variable value is pointer to a DFA state used as
5972    temporary variable.  */
5973
5974 static state_t temp_dfa_state = NULL;
5975
5976 /* The following variable value is DFA state after issuing the last
5977    insn.  */
5978
5979 static state_t prev_cycle_state = NULL;
5980
5981 /* The following array element values are TRUE if the corresponding
5982    insn requires to add stop bits before it.  */
5983
5984 static char *stops_p;
5985
5986 /* The following variable is used to set up the mentioned above array.  */
5987
5988 static int stop_before_p = 0;
5989
5990 /* The following variable value is length of the arrays `clocks' and
5991    `add_cycles'. */
5992
5993 static int clocks_length;
5994
5995 /* The following array element values are cycles on which the
5996    corresponding insn will be issued.  The array is used only for
5997    Itanium1.  */
5998
5999 static int *clocks;
6000
6001 /* The following array element values are numbers of cycles should be
6002    added to improve insn scheduling for MM_insns for Itanium1.  */
6003
6004 static int *add_cycles;
6005
6006 static rtx ia64_single_set (rtx);
6007 static void ia64_emit_insn_before (rtx, rtx);
6008
6009 /* Map a bundle number to its pseudo-op.  */
6010
6011 const char *
6012 get_bundle_name (int b)
6013 {
6014   return bundle_name[b];
6015 }
6016
6017
6018 /* Return the maximum number of instructions a cpu can issue.  */
6019
6020 static int
6021 ia64_issue_rate (void)
6022 {
6023   return 6;
6024 }
6025
6026 /* Helper function - like single_set, but look inside COND_EXEC.  */
6027
6028 static rtx
6029 ia64_single_set (rtx insn)
6030 {
6031   rtx x = PATTERN (insn), ret;
6032   if (GET_CODE (x) == COND_EXEC)
6033     x = COND_EXEC_CODE (x);
6034   if (GET_CODE (x) == SET)
6035     return x;
6036
6037   /* Special case here prologue_allocate_stack and epilogue_deallocate_stack.
6038      Although they are not classical single set, the second set is there just
6039      to protect it from moving past FP-relative stack accesses.  */
6040   switch (recog_memoized (insn))
6041     {
6042     case CODE_FOR_prologue_allocate_stack:
6043     case CODE_FOR_epilogue_deallocate_stack:
6044       ret = XVECEXP (x, 0, 0);
6045       break;
6046
6047     default:
6048       ret = single_set_2 (insn, x);
6049       break;
6050     }
6051
6052   return ret;
6053 }
6054
6055 /* Adjust the cost of a scheduling dependency.  Return the new cost of
6056    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
6057
6058 static int
6059 ia64_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
6060 {
6061   enum attr_itanium_class dep_class;
6062   enum attr_itanium_class insn_class;
6063
6064   if (REG_NOTE_KIND (link) != REG_DEP_OUTPUT)
6065     return cost;
6066
6067   insn_class = ia64_safe_itanium_class (insn);
6068   dep_class = ia64_safe_itanium_class (dep_insn);
6069   if (dep_class == ITANIUM_CLASS_ST || dep_class == ITANIUM_CLASS_STF
6070       || insn_class == ITANIUM_CLASS_ST || insn_class == ITANIUM_CLASS_STF)
6071     return 0;
6072
6073   return cost;
6074 }
6075
6076 /* Like emit_insn_before, but skip cycle_display notes.
6077    ??? When cycle display notes are implemented, update this.  */
6078
6079 static void
6080 ia64_emit_insn_before (rtx insn, rtx before)
6081 {
6082   emit_insn_before (insn, before);
6083 }
6084
6085 /* The following function marks insns who produce addresses for load
6086    and store insns.  Such insns will be placed into M slots because it
6087    decrease latency time for Itanium1 (see function
6088    `ia64_produce_address_p' and the DFA descriptions).  */
6089
6090 static void
6091 ia64_dependencies_evaluation_hook (rtx head, rtx tail)
6092 {
6093   rtx insn, link, next, next_tail;
6094
6095   next_tail = NEXT_INSN (tail);
6096   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
6097     if (INSN_P (insn))
6098       insn->call = 0;
6099   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
6100     if (INSN_P (insn)
6101         && ia64_safe_itanium_class (insn) == ITANIUM_CLASS_IALU)
6102       {
6103         for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
6104           {
6105             next = XEXP (link, 0);
6106             if ((ia64_safe_itanium_class (next) == ITANIUM_CLASS_ST
6107                  || ia64_safe_itanium_class (next) == ITANIUM_CLASS_STF)
6108                 && ia64_st_address_bypass_p (insn, next))
6109               break;
6110             else if ((ia64_safe_itanium_class (next) == ITANIUM_CLASS_LD
6111                       || ia64_safe_itanium_class (next)
6112                       == ITANIUM_CLASS_FLD)
6113                      && ia64_ld_address_bypass_p (insn, next))
6114               break;
6115           }
6116         insn->call = link != 0;
6117       }
6118 }
6119
6120 /* We're beginning a new block.  Initialize data structures as necessary.  */
6121
6122 static void
6123 ia64_sched_init (FILE *dump ATTRIBUTE_UNUSED,
6124                  int sched_verbose ATTRIBUTE_UNUSED,
6125                  int max_ready ATTRIBUTE_UNUSED)
6126 {
6127 #ifdef ENABLE_CHECKING
6128   rtx insn;
6129
6130   if (reload_completed)
6131     for (insn = NEXT_INSN (current_sched_info->prev_head);
6132          insn != current_sched_info->next_tail;
6133          insn = NEXT_INSN (insn))
6134       if (SCHED_GROUP_P (insn))
6135         abort ();
6136 #endif
6137   last_scheduled_insn = NULL_RTX;
6138   init_insn_group_barriers ();
6139 }
6140
6141 /* We are about to being issuing insns for this clock cycle.
6142    Override the default sort algorithm to better slot instructions.  */
6143
6144 static int
6145 ia64_dfa_sched_reorder (FILE *dump, int sched_verbose, rtx *ready,
6146                         int *pn_ready, int clock_var ATTRIBUTE_UNUSED,
6147                         int reorder_type)
6148 {
6149   int n_asms;
6150   int n_ready = *pn_ready;
6151   rtx *e_ready = ready + n_ready;
6152   rtx *insnp;
6153
6154   if (sched_verbose)
6155     fprintf (dump, "// ia64_dfa_sched_reorder (type %d):\n", reorder_type);
6156
6157   if (reorder_type == 0)
6158     {
6159       /* First, move all USEs, CLOBBERs and other crud out of the way.  */
6160       n_asms = 0;
6161       for (insnp = ready; insnp < e_ready; insnp++)
6162         if (insnp < e_ready)
6163           {
6164             rtx insn = *insnp;
6165             enum attr_type t = ia64_safe_type (insn);
6166             if (t == TYPE_UNKNOWN)
6167               {
6168                 if (GET_CODE (PATTERN (insn)) == ASM_INPUT
6169                     || asm_noperands (PATTERN (insn)) >= 0)
6170                   {
6171                     rtx lowest = ready[n_asms];
6172                     ready[n_asms] = insn;
6173                     *insnp = lowest;
6174                     n_asms++;
6175                   }
6176                 else
6177                   {
6178                     rtx highest = ready[n_ready - 1];
6179                     ready[n_ready - 1] = insn;
6180                     *insnp = highest;
6181                     return 1;
6182                   }
6183               }
6184           }
6185
6186       if (n_asms < n_ready)
6187         {
6188           /* Some normal insns to process.  Skip the asms.  */
6189           ready += n_asms;
6190           n_ready -= n_asms;
6191         }
6192       else if (n_ready > 0)
6193         return 1;
6194     }
6195
6196   if (ia64_final_schedule)
6197     {
6198       int deleted = 0;
6199       int nr_need_stop = 0;
6200
6201       for (insnp = ready; insnp < e_ready; insnp++)
6202         if (safe_group_barrier_needed_p (*insnp))
6203           nr_need_stop++;
6204
6205       if (reorder_type == 1 && n_ready == nr_need_stop)
6206         return 0;
6207       if (reorder_type == 0)
6208         return 1;
6209       insnp = e_ready;
6210       /* Move down everything that needs a stop bit, preserving
6211          relative order.  */
6212       while (insnp-- > ready + deleted)
6213         while (insnp >= ready + deleted)
6214           {
6215             rtx insn = *insnp;
6216             if (! safe_group_barrier_needed_p (insn))
6217               break;
6218             memmove (ready + 1, ready, (insnp - ready) * sizeof (rtx));
6219             *ready = insn;
6220             deleted++;
6221           }
6222       n_ready -= deleted;
6223       ready += deleted;
6224     }
6225
6226   return 1;
6227 }
6228
6229 /* We are about to being issuing insns for this clock cycle.  Override
6230    the default sort algorithm to better slot instructions.  */
6231
6232 static int
6233 ia64_sched_reorder (FILE *dump, int sched_verbose, rtx *ready, int *pn_ready,
6234                     int clock_var)
6235 {
6236   return ia64_dfa_sched_reorder (dump, sched_verbose, ready,
6237                                  pn_ready, clock_var, 0);
6238 }
6239
6240 /* Like ia64_sched_reorder, but called after issuing each insn.
6241    Override the default sort algorithm to better slot instructions.  */
6242
6243 static int
6244 ia64_sched_reorder2 (FILE *dump ATTRIBUTE_UNUSED,
6245                      int sched_verbose ATTRIBUTE_UNUSED, rtx *ready,
6246                      int *pn_ready, int clock_var)
6247 {
6248   if (ia64_tune == PROCESSOR_ITANIUM && reload_completed && last_scheduled_insn)
6249     clocks [INSN_UID (last_scheduled_insn)] = clock_var;
6250   return ia64_dfa_sched_reorder (dump, sched_verbose, ready, pn_ready,
6251                                  clock_var, 1);
6252 }
6253
6254 /* We are about to issue INSN.  Return the number of insns left on the
6255    ready queue that can be issued this cycle.  */
6256
6257 static int
6258 ia64_variable_issue (FILE *dump ATTRIBUTE_UNUSED,
6259                      int sched_verbose ATTRIBUTE_UNUSED,
6260                      rtx insn ATTRIBUTE_UNUSED,
6261                      int can_issue_more ATTRIBUTE_UNUSED)
6262 {
6263   last_scheduled_insn = insn;
6264   memcpy (prev_cycle_state, curr_state, dfa_state_size);
6265   if (reload_completed)
6266     {
6267       if (group_barrier_needed_p (insn))
6268         abort ();
6269       if (GET_CODE (insn) == CALL_INSN)
6270         init_insn_group_barriers ();
6271       stops_p [INSN_UID (insn)] = stop_before_p;
6272       stop_before_p = 0;
6273     }
6274   return 1;
6275 }
6276
6277 /* We are choosing insn from the ready queue.  Return nonzero if INSN
6278    can be chosen.  */
6279
6280 static int
6281 ia64_first_cycle_multipass_dfa_lookahead_guard (rtx insn)
6282 {
6283   if (insn == NULL_RTX || !INSN_P (insn))
6284     abort ();
6285   return (!reload_completed
6286           || !safe_group_barrier_needed_p (insn));
6287 }
6288
6289 /* The following variable value is pseudo-insn used by the DFA insn
6290    scheduler to change the DFA state when the simulated clock is
6291    increased.  */
6292
6293 static rtx dfa_pre_cycle_insn;
6294
6295 /* We are about to being issuing INSN.  Return nonzero if we can not
6296    issue it on given cycle CLOCK and return zero if we should not sort
6297    the ready queue on the next clock start.  */
6298
6299 static int
6300 ia64_dfa_new_cycle (FILE *dump, int verbose, rtx insn, int last_clock,
6301                     int clock, int *sort_p)
6302 {
6303   int setup_clocks_p = FALSE;
6304
6305   if (insn == NULL_RTX || !INSN_P (insn))
6306     abort ();
6307   if ((reload_completed && safe_group_barrier_needed_p (insn))
6308       || (last_scheduled_insn
6309           && (GET_CODE (last_scheduled_insn) == CALL_INSN
6310               || GET_CODE (PATTERN (last_scheduled_insn)) == ASM_INPUT
6311               || asm_noperands (PATTERN (last_scheduled_insn)) >= 0)))
6312     {
6313       init_insn_group_barriers ();
6314       if (verbose && dump)
6315         fprintf (dump, "//    Stop should be before %d%s\n", INSN_UID (insn),
6316                  last_clock == clock ? " + cycle advance" : "");
6317       stop_before_p = 1;
6318       if (last_clock == clock)
6319         {
6320           state_transition (curr_state, dfa_stop_insn);
6321           if (TARGET_EARLY_STOP_BITS)
6322             *sort_p = (last_scheduled_insn == NULL_RTX
6323                        || GET_CODE (last_scheduled_insn) != CALL_INSN);
6324           else
6325             *sort_p = 0;
6326           return 1;
6327         }
6328       else if (reload_completed)
6329         setup_clocks_p = TRUE;
6330       memcpy (curr_state, prev_cycle_state, dfa_state_size);
6331       state_transition (curr_state, dfa_stop_insn);
6332       state_transition (curr_state, dfa_pre_cycle_insn);
6333       state_transition (curr_state, NULL);
6334     }
6335   else if (reload_completed)
6336     setup_clocks_p = TRUE;
6337   if (setup_clocks_p && ia64_tune == PROCESSOR_ITANIUM
6338       && GET_CODE (PATTERN (insn)) != ASM_INPUT
6339       && asm_noperands (PATTERN (insn)) == 0)
6340     {
6341       enum attr_itanium_class c = ia64_safe_itanium_class (insn);
6342
6343       if (c != ITANIUM_CLASS_MMMUL && c != ITANIUM_CLASS_MMSHF)
6344         {
6345           rtx link;
6346           int d = -1;
6347
6348           for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
6349             if (REG_NOTE_KIND (link) == 0)
6350               {
6351                 enum attr_itanium_class dep_class;
6352                 rtx dep_insn = XEXP (link, 0);
6353
6354                 dep_class = ia64_safe_itanium_class (dep_insn);
6355                 if ((dep_class == ITANIUM_CLASS_MMMUL
6356                      || dep_class == ITANIUM_CLASS_MMSHF)
6357                     && last_clock - clocks [INSN_UID (dep_insn)] < 4
6358                     && (d < 0
6359                         || last_clock - clocks [INSN_UID (dep_insn)] < d))
6360                   d = last_clock - clocks [INSN_UID (dep_insn)];
6361               }
6362           if (d >= 0)
6363             add_cycles [INSN_UID (insn)] = 3 - d;
6364         }
6365     }
6366   return 0;
6367 }
6368
6369 \f
6370
6371 /* The following page contains abstract data `bundle states' which are
6372    used for bundling insns (inserting nops and template generation).  */
6373
6374 /* The following describes state of insn bundling.  */
6375
6376 struct bundle_state
6377 {
6378   /* Unique bundle state number to identify them in the debugging
6379      output  */
6380   int unique_num;
6381   rtx insn;     /* corresponding insn, NULL for the 1st and the last state  */
6382   /* number nops before and after the insn  */
6383   short before_nops_num, after_nops_num;
6384   int insn_num; /* insn number (0 - for initial state, 1 - for the 1st
6385                    insn */
6386   int cost;     /* cost of the state in cycles */
6387   int accumulated_insns_num; /* number of all previous insns including
6388                                 nops.  L is considered as 2 insns */
6389   int branch_deviation; /* deviation of previous branches from 3rd slots  */
6390   struct bundle_state *next;  /* next state with the same insn_num  */
6391   struct bundle_state *originator; /* originator (previous insn state)  */
6392   /* All bundle states are in the following chain.  */
6393   struct bundle_state *allocated_states_chain;
6394   /* The DFA State after issuing the insn and the nops.  */
6395   state_t dfa_state;
6396 };
6397
6398 /* The following is map insn number to the corresponding bundle state.  */
6399
6400 static struct bundle_state **index_to_bundle_states;
6401
6402 /* The unique number of next bundle state.  */
6403
6404 static int bundle_states_num;
6405
6406 /* All allocated bundle states are in the following chain.  */
6407
6408 static struct bundle_state *allocated_bundle_states_chain;
6409
6410 /* All allocated but not used bundle states are in the following
6411    chain.  */
6412
6413 static struct bundle_state *free_bundle_state_chain;
6414
6415
6416 /* The following function returns a free bundle state.  */
6417
6418 static struct bundle_state *
6419 get_free_bundle_state (void)
6420 {
6421   struct bundle_state *result;
6422
6423   if (free_bundle_state_chain != NULL)
6424     {
6425       result = free_bundle_state_chain;
6426       free_bundle_state_chain = result->next;
6427     }
6428   else
6429     {
6430       result = xmalloc (sizeof (struct bundle_state));
6431       result->dfa_state = xmalloc (dfa_state_size);
6432       result->allocated_states_chain = allocated_bundle_states_chain;
6433       allocated_bundle_states_chain = result;
6434     }
6435   result->unique_num = bundle_states_num++;
6436   return result;
6437
6438 }
6439
6440 /* The following function frees given bundle state.  */
6441
6442 static void
6443 free_bundle_state (struct bundle_state *state)
6444 {
6445   state->next = free_bundle_state_chain;
6446   free_bundle_state_chain = state;
6447 }
6448
6449 /* Start work with abstract data `bundle states'.  */
6450
6451 static void
6452 initiate_bundle_states (void)
6453 {
6454   bundle_states_num = 0;
6455   free_bundle_state_chain = NULL;
6456   allocated_bundle_states_chain = NULL;
6457 }
6458
6459 /* Finish work with abstract data `bundle states'.  */
6460
6461 static void
6462 finish_bundle_states (void)
6463 {
6464   struct bundle_state *curr_state, *next_state;
6465
6466   for (curr_state = allocated_bundle_states_chain;
6467        curr_state != NULL;
6468        curr_state = next_state)
6469     {
6470       next_state = curr_state->allocated_states_chain;
6471       free (curr_state->dfa_state);
6472       free (curr_state);
6473     }
6474 }
6475
6476 /* Hash table of the bundle states.  The key is dfa_state and insn_num
6477    of the bundle states.  */
6478
6479 static htab_t bundle_state_table;
6480
6481 /* The function returns hash of BUNDLE_STATE.  */
6482
6483 static unsigned
6484 bundle_state_hash (const void *bundle_state)
6485 {
6486   const struct bundle_state *state = (struct bundle_state *) bundle_state;
6487   unsigned result, i;
6488
6489   for (result = i = 0; i < dfa_state_size; i++)
6490     result += (((unsigned char *) state->dfa_state) [i]
6491                << ((i % CHAR_BIT) * 3 + CHAR_BIT));
6492   return result + state->insn_num;
6493 }
6494
6495 /* The function returns nonzero if the bundle state keys are equal.  */
6496
6497 static int
6498 bundle_state_eq_p (const void *bundle_state_1, const void *bundle_state_2)
6499 {
6500   const struct bundle_state * state1 = (struct bundle_state *) bundle_state_1;
6501   const struct bundle_state * state2 = (struct bundle_state *) bundle_state_2;
6502
6503   return (state1->insn_num == state2->insn_num
6504           && memcmp (state1->dfa_state, state2->dfa_state,
6505                      dfa_state_size) == 0);
6506 }
6507
6508 /* The function inserts the BUNDLE_STATE into the hash table.  The
6509    function returns nonzero if the bundle has been inserted into the
6510    table.  The table contains the best bundle state with given key.  */
6511
6512 static int
6513 insert_bundle_state (struct bundle_state *bundle_state)
6514 {
6515   void **entry_ptr;
6516
6517   entry_ptr = htab_find_slot (bundle_state_table, bundle_state, 1);
6518   if (*entry_ptr == NULL)
6519     {
6520       bundle_state->next = index_to_bundle_states [bundle_state->insn_num];
6521       index_to_bundle_states [bundle_state->insn_num] = bundle_state;
6522       *entry_ptr = (void *) bundle_state;
6523       return TRUE;
6524     }
6525   else if (bundle_state->cost < ((struct bundle_state *) *entry_ptr)->cost
6526            || (bundle_state->cost == ((struct bundle_state *) *entry_ptr)->cost
6527                && (((struct bundle_state *)*entry_ptr)->accumulated_insns_num
6528                    > bundle_state->accumulated_insns_num
6529                    || (((struct bundle_state *)
6530                         *entry_ptr)->accumulated_insns_num
6531                        == bundle_state->accumulated_insns_num
6532                        && ((struct bundle_state *)
6533                            *entry_ptr)->branch_deviation
6534                        > bundle_state->branch_deviation))))
6535
6536     {
6537       struct bundle_state temp;
6538
6539       temp = *(struct bundle_state *) *entry_ptr;
6540       *(struct bundle_state *) *entry_ptr = *bundle_state;
6541       ((struct bundle_state *) *entry_ptr)->next = temp.next;
6542       *bundle_state = temp;
6543     }
6544   return FALSE;
6545 }
6546
6547 /* Start work with the hash table.  */
6548
6549 static void
6550 initiate_bundle_state_table (void)
6551 {
6552   bundle_state_table = htab_create (50, bundle_state_hash, bundle_state_eq_p,
6553                                     (htab_del) 0);
6554 }
6555
6556 /* Finish work with the hash table.  */
6557
6558 static void
6559 finish_bundle_state_table (void)
6560 {
6561   htab_delete (bundle_state_table);
6562 }
6563
6564 \f
6565
6566 /* The following variable is a insn `nop' used to check bundle states
6567    with different number of inserted nops.  */
6568
6569 static rtx ia64_nop;
6570
6571 /* The following function tries to issue NOPS_NUM nops for the current
6572    state without advancing processor cycle.  If it failed, the
6573    function returns FALSE and frees the current state.  */
6574
6575 static int
6576 try_issue_nops (struct bundle_state *curr_state, int nops_num)
6577 {
6578   int i;
6579
6580   for (i = 0; i < nops_num; i++)
6581     if (state_transition (curr_state->dfa_state, ia64_nop) >= 0)
6582       {
6583         free_bundle_state (curr_state);
6584         return FALSE;
6585       }
6586   return TRUE;
6587 }
6588
6589 /* The following function tries to issue INSN for the current
6590    state without advancing processor cycle.  If it failed, the
6591    function returns FALSE and frees the current state.  */
6592
6593 static int
6594 try_issue_insn (struct bundle_state *curr_state, rtx insn)
6595 {
6596   if (insn && state_transition (curr_state->dfa_state, insn) >= 0)
6597     {
6598       free_bundle_state (curr_state);
6599       return FALSE;
6600     }
6601   return TRUE;
6602 }
6603
6604 /* The following function tries to issue BEFORE_NOPS_NUM nops and INSN
6605    starting with ORIGINATOR without advancing processor cycle.  If
6606    TRY_BUNDLE_END_P is TRUE, the function also/only (if
6607    ONLY_BUNDLE_END_P is TRUE) tries to issue nops to fill all bundle.
6608    If it was successful, the function creates new bundle state and
6609    insert into the hash table and into `index_to_bundle_states'.  */
6610
6611 static void
6612 issue_nops_and_insn (struct bundle_state *originator, int before_nops_num,
6613                      rtx insn, int try_bundle_end_p, int only_bundle_end_p)
6614 {
6615   struct bundle_state *curr_state;
6616
6617   curr_state = get_free_bundle_state ();
6618   memcpy (curr_state->dfa_state, originator->dfa_state, dfa_state_size);
6619   curr_state->insn = insn;
6620   curr_state->insn_num = originator->insn_num + 1;
6621   curr_state->cost = originator->cost;
6622   curr_state->originator = originator;
6623   curr_state->before_nops_num = before_nops_num;
6624   curr_state->after_nops_num = 0;
6625   curr_state->accumulated_insns_num
6626     = originator->accumulated_insns_num + before_nops_num;
6627   curr_state->branch_deviation = originator->branch_deviation;
6628   if (insn == NULL_RTX)
6629     abort ();
6630   else if (INSN_CODE (insn) == CODE_FOR_insn_group_barrier)
6631     {
6632       if (GET_MODE (insn) == TImode)
6633         abort ();
6634       if (!try_issue_nops (curr_state, before_nops_num))
6635         return;
6636       if (!try_issue_insn (curr_state, insn))
6637         return;
6638       memcpy (temp_dfa_state, curr_state->dfa_state, dfa_state_size);
6639       if (state_transition (temp_dfa_state, dfa_pre_cycle_insn) >= 0
6640           && curr_state->accumulated_insns_num % 3 != 0)
6641         {
6642           free_bundle_state (curr_state);
6643           return;
6644         }
6645     }
6646   else if (GET_MODE (insn) != TImode)
6647     {
6648       if (!try_issue_nops (curr_state, before_nops_num))
6649         return;
6650       if (!try_issue_insn (curr_state, insn))
6651         return;
6652       curr_state->accumulated_insns_num++;
6653       if (GET_CODE (PATTERN (insn)) == ASM_INPUT
6654           || asm_noperands (PATTERN (insn)) >= 0)
6655         abort ();
6656       if (ia64_safe_type (insn) == TYPE_L)
6657         curr_state->accumulated_insns_num++;
6658     }
6659   else
6660     {
6661       state_transition (curr_state->dfa_state, dfa_pre_cycle_insn);
6662       state_transition (curr_state->dfa_state, NULL);
6663       curr_state->cost++;
6664       if (!try_issue_nops (curr_state, before_nops_num))
6665         return;
6666       if (!try_issue_insn (curr_state, insn))
6667         return;
6668       curr_state->accumulated_insns_num++;
6669       if (GET_CODE (PATTERN (insn)) == ASM_INPUT
6670           || asm_noperands (PATTERN (insn)) >= 0)
6671         {
6672           /* Finish bundle containing asm insn.  */
6673           curr_state->after_nops_num
6674             = 3 - curr_state->accumulated_insns_num % 3;
6675           curr_state->accumulated_insns_num
6676             += 3 - curr_state->accumulated_insns_num % 3;
6677         }
6678       else if (ia64_safe_type (insn) == TYPE_L)
6679         curr_state->accumulated_insns_num++;
6680     }
6681   if (ia64_safe_type (insn) == TYPE_B)
6682     curr_state->branch_deviation
6683       += 2 - (curr_state->accumulated_insns_num - 1) % 3;
6684   if (try_bundle_end_p && curr_state->accumulated_insns_num % 3 != 0)
6685     {
6686       if (!only_bundle_end_p && insert_bundle_state (curr_state))
6687         {
6688           state_t dfa_state;
6689           struct bundle_state *curr_state1;
6690           struct bundle_state *allocated_states_chain;
6691
6692           curr_state1 = get_free_bundle_state ();
6693           dfa_state = curr_state1->dfa_state;
6694           allocated_states_chain = curr_state1->allocated_states_chain;
6695           *curr_state1 = *curr_state;
6696           curr_state1->dfa_state = dfa_state;
6697           curr_state1->allocated_states_chain = allocated_states_chain;
6698           memcpy (curr_state1->dfa_state, curr_state->dfa_state,
6699                   dfa_state_size);
6700           curr_state = curr_state1;
6701         }
6702       if (!try_issue_nops (curr_state,
6703                            3 - curr_state->accumulated_insns_num % 3))
6704         return;
6705       curr_state->after_nops_num
6706         = 3 - curr_state->accumulated_insns_num % 3;
6707       curr_state->accumulated_insns_num
6708         += 3 - curr_state->accumulated_insns_num % 3;
6709     }
6710   if (!insert_bundle_state (curr_state))
6711     free_bundle_state (curr_state);
6712   return;
6713 }
6714
6715 /* The following function returns position in the two window bundle
6716    for given STATE.  */
6717
6718 static int
6719 get_max_pos (state_t state)
6720 {
6721   if (cpu_unit_reservation_p (state, pos_6))
6722     return 6;
6723   else if (cpu_unit_reservation_p (state, pos_5))
6724     return 5;
6725   else if (cpu_unit_reservation_p (state, pos_4))
6726     return 4;
6727   else if (cpu_unit_reservation_p (state, pos_3))
6728     return 3;
6729   else if (cpu_unit_reservation_p (state, pos_2))
6730     return 2;
6731   else if (cpu_unit_reservation_p (state, pos_1))
6732     return 1;
6733   else
6734     return 0;
6735 }
6736
6737 /* The function returns code of a possible template for given position
6738    and state.  The function should be called only with 2 values of
6739    position equal to 3 or 6.  */
6740
6741 static int
6742 get_template (state_t state, int pos)
6743 {
6744   switch (pos)
6745     {
6746     case 3:
6747       if (cpu_unit_reservation_p (state, _0mii_))
6748         return 0;
6749       else if (cpu_unit_reservation_p (state, _0mmi_))
6750         return 1;
6751       else if (cpu_unit_reservation_p (state, _0mfi_))
6752         return 2;
6753       else if (cpu_unit_reservation_p (state, _0mmf_))
6754         return 3;
6755       else if (cpu_unit_reservation_p (state, _0bbb_))
6756         return 4;
6757       else if (cpu_unit_reservation_p (state, _0mbb_))
6758         return 5;
6759       else if (cpu_unit_reservation_p (state, _0mib_))
6760         return 6;
6761       else if (cpu_unit_reservation_p (state, _0mmb_))
6762         return 7;
6763       else if (cpu_unit_reservation_p (state, _0mfb_))
6764         return 8;
6765       else if (cpu_unit_reservation_p (state, _0mlx_))
6766         return 9;
6767       else
6768         abort ();
6769     case 6:
6770       if (cpu_unit_reservation_p (state, _1mii_))
6771         return 0;
6772       else if (cpu_unit_reservation_p (state, _1mmi_))
6773         return 1;
6774       else if (cpu_unit_reservation_p (state, _1mfi_))
6775         return 2;
6776       else if (_1mmf_ >= 0 && cpu_unit_reservation_p (state, _1mmf_))
6777         return 3;
6778       else if (cpu_unit_reservation_p (state, _1bbb_))
6779         return 4;
6780       else if (cpu_unit_reservation_p (state, _1mbb_))
6781         return 5;
6782       else if (cpu_unit_reservation_p (state, _1mib_))
6783         return 6;
6784       else if (cpu_unit_reservation_p (state, _1mmb_))
6785         return 7;
6786       else if (cpu_unit_reservation_p (state, _1mfb_))
6787         return 8;
6788       else if (cpu_unit_reservation_p (state, _1mlx_))
6789         return 9;
6790       else
6791         abort ();
6792     default:
6793       abort ();
6794     }
6795 }
6796
6797 /* The following function returns an insn important for insn bundling
6798    followed by INSN and before TAIL.  */
6799
6800 static rtx
6801 get_next_important_insn (rtx insn, rtx tail)
6802 {
6803   for (; insn && insn != tail; insn = NEXT_INSN (insn))
6804     if (INSN_P (insn)
6805         && ia64_safe_itanium_class (insn) != ITANIUM_CLASS_IGNORE
6806         && GET_CODE (PATTERN (insn)) != USE
6807         && GET_CODE (PATTERN (insn)) != CLOBBER)
6808       return insn;
6809   return NULL_RTX;
6810 }
6811
6812 /* The following function does insn bundling.  Bundling means
6813    inserting templates and nop insns to fit insn groups into permitted
6814    templates.  Instruction scheduling uses NDFA (non-deterministic
6815    finite automata) encoding informations about the templates and the
6816    inserted nops.  Nondeterminism of the automata permits follows
6817    all possible insn sequences very fast.
6818
6819    Unfortunately it is not possible to get information about inserting
6820    nop insns and used templates from the automata states.  The
6821    automata only says that we can issue an insn possibly inserting
6822    some nops before it and using some template.  Therefore insn
6823    bundling in this function is implemented by using DFA
6824    (deterministic finite automata).  We follows all possible insn
6825    sequences by inserting 0-2 nops (that is what the NDFA describe for
6826    insn scheduling) before/after each insn being bundled.  We know the
6827    start of simulated processor cycle from insn scheduling (insn
6828    starting a new cycle has TImode).
6829
6830    Simple implementation of insn bundling would create enormous
6831    number of possible insn sequences satisfying information about new
6832    cycle ticks taken from the insn scheduling.  To make the algorithm
6833    practical we use dynamic programming.  Each decision (about
6834    inserting nops and implicitly about previous decisions) is described
6835    by structure bundle_state (see above).  If we generate the same
6836    bundle state (key is automaton state after issuing the insns and
6837    nops for it), we reuse already generated one.  As consequence we
6838    reject some decisions which can not improve the solution and
6839    reduce memory for the algorithm.
6840
6841    When we reach the end of EBB (extended basic block), we choose the
6842    best sequence and then, moving back in EBB, insert templates for
6843    the best alternative.  The templates are taken from querying
6844    automaton state for each insn in chosen bundle states.
6845
6846    So the algorithm makes two (forward and backward) passes through
6847    EBB.  There is an additional forward pass through EBB for Itanium1
6848    processor.  This pass inserts more nops to make dependency between
6849    a producer insn and MMMUL/MMSHF at least 4 cycles long.  */
6850
6851 static void
6852 bundling (FILE *dump, int verbose, rtx prev_head_insn, rtx tail)
6853 {
6854   struct bundle_state *curr_state, *next_state, *best_state;
6855   rtx insn, next_insn;
6856   int insn_num;
6857   int i, bundle_end_p, only_bundle_end_p, asm_p;
6858   int pos = 0, max_pos, template0, template1;
6859   rtx b;
6860   rtx nop;
6861   enum attr_type type;
6862
6863   insn_num = 0;
6864   /* Count insns in the EBB.  */
6865   for (insn = NEXT_INSN (prev_head_insn);
6866        insn && insn != tail;
6867        insn = NEXT_INSN (insn))
6868     if (INSN_P (insn))
6869       insn_num++;
6870   if (insn_num == 0)
6871     return;
6872   bundling_p = 1;
6873   dfa_clean_insn_cache ();
6874   initiate_bundle_state_table ();
6875   index_to_bundle_states = xmalloc ((insn_num + 2)
6876                                     * sizeof (struct bundle_state *));
6877   /* First (forward) pass -- generation of bundle states.  */
6878   curr_state = get_free_bundle_state ();
6879   curr_state->insn = NULL;
6880   curr_state->before_nops_num = 0;
6881   curr_state->after_nops_num = 0;
6882   curr_state->insn_num = 0;
6883   curr_state->cost = 0;
6884   curr_state->accumulated_insns_num = 0;
6885   curr_state->branch_deviation = 0;
6886   curr_state->next = NULL;
6887   curr_state->originator = NULL;
6888   state_reset (curr_state->dfa_state);
6889   index_to_bundle_states [0] = curr_state;
6890   insn_num = 0;
6891   /* Shift cycle mark if it is put on insn which could be ignored.  */
6892   for (insn = NEXT_INSN (prev_head_insn);
6893        insn != tail;
6894        insn = NEXT_INSN (insn))
6895     if (INSN_P (insn)
6896         && (ia64_safe_itanium_class (insn) == ITANIUM_CLASS_IGNORE
6897             || GET_CODE (PATTERN (insn)) == USE
6898             || GET_CODE (PATTERN (insn)) == CLOBBER)
6899         && GET_MODE (insn) == TImode)
6900       {
6901         PUT_MODE (insn, VOIDmode);
6902         for (next_insn = NEXT_INSN (insn);
6903              next_insn != tail;
6904              next_insn = NEXT_INSN (next_insn))
6905           if (INSN_P (next_insn)
6906               && ia64_safe_itanium_class (next_insn) != ITANIUM_CLASS_IGNORE
6907               && GET_CODE (PATTERN (next_insn)) != USE
6908               && GET_CODE (PATTERN (next_insn)) != CLOBBER)
6909             {
6910               PUT_MODE (next_insn, TImode);
6911               break;
6912             }
6913       }
6914   /* Froward pass: generation of bundle states.  */
6915   for (insn = get_next_important_insn (NEXT_INSN (prev_head_insn), tail);
6916        insn != NULL_RTX;
6917        insn = next_insn)
6918     {
6919       if (!INSN_P (insn)
6920           || ia64_safe_itanium_class (insn) == ITANIUM_CLASS_IGNORE
6921           || GET_CODE (PATTERN (insn)) == USE
6922           || GET_CODE (PATTERN (insn)) == CLOBBER)
6923         abort ();
6924       type = ia64_safe_type (insn);
6925       next_insn = get_next_important_insn (NEXT_INSN (insn), tail);
6926       insn_num++;
6927       index_to_bundle_states [insn_num] = NULL;
6928       for (curr_state = index_to_bundle_states [insn_num - 1];
6929            curr_state != NULL;
6930            curr_state = next_state)
6931         {
6932           pos = curr_state->accumulated_insns_num % 3;
6933           next_state = curr_state->next;
6934           /* We must fill up the current bundle in order to start a
6935              subsequent asm insn in a new bundle.  Asm insn is always
6936              placed in a separate bundle.  */
6937           only_bundle_end_p
6938             = (next_insn != NULL_RTX
6939                && INSN_CODE (insn) == CODE_FOR_insn_group_barrier
6940                && ia64_safe_type (next_insn) == TYPE_UNKNOWN);
6941           /* We may fill up the current bundle if it is the cycle end
6942              without a group barrier.  */
6943           bundle_end_p
6944             = (only_bundle_end_p || next_insn == NULL_RTX
6945                || (GET_MODE (next_insn) == TImode
6946                    && INSN_CODE (insn) != CODE_FOR_insn_group_barrier));
6947           if (type == TYPE_F || type == TYPE_B || type == TYPE_L
6948               || type == TYPE_S
6949               /* We need to insert 2 nops for cases like M_MII.  To
6950                  guarantee issuing all insns on the same cycle for
6951                  Itanium 1, we need to issue 2 nops after the first M
6952                  insn (MnnMII where n is a nop insn).  */
6953               || ((type == TYPE_M || type == TYPE_A)
6954                   && ia64_tune == PROCESSOR_ITANIUM
6955                   && !bundle_end_p && pos == 1))
6956             issue_nops_and_insn (curr_state, 2, insn, bundle_end_p,
6957                                  only_bundle_end_p);
6958           issue_nops_and_insn (curr_state, 1, insn, bundle_end_p,
6959                                only_bundle_end_p);
6960           issue_nops_and_insn (curr_state, 0, insn, bundle_end_p,
6961                                only_bundle_end_p);
6962         }
6963       if (index_to_bundle_states [insn_num] == NULL)
6964         abort ();
6965       for (curr_state = index_to_bundle_states [insn_num];
6966            curr_state != NULL;
6967            curr_state = curr_state->next)
6968         if (verbose >= 2 && dump)
6969           {
6970             /* This structure is taken from generated code of the
6971                pipeline hazard recognizer (see file insn-attrtab.c).
6972                Please don't forget to change the structure if a new
6973                automaton is added to .md file.  */
6974             struct DFA_chip
6975             {
6976               unsigned short one_automaton_state;
6977               unsigned short oneb_automaton_state;
6978               unsigned short two_automaton_state;
6979               unsigned short twob_automaton_state;
6980             };
6981
6982             fprintf
6983               (dump,
6984                "//    Bundle state %d (orig %d, cost %d, nops %d/%d, insns %d, branch %d, state %d) for %d\n",
6985                curr_state->unique_num,
6986                (curr_state->originator == NULL
6987                 ? -1 : curr_state->originator->unique_num),
6988                curr_state->cost,
6989                curr_state->before_nops_num, curr_state->after_nops_num,
6990                curr_state->accumulated_insns_num, curr_state->branch_deviation,
6991                (ia64_tune == PROCESSOR_ITANIUM
6992                 ? ((struct DFA_chip *) curr_state->dfa_state)->oneb_automaton_state
6993                 : ((struct DFA_chip *) curr_state->dfa_state)->twob_automaton_state),
6994                INSN_UID (insn));
6995           }
6996     }
6997   if (index_to_bundle_states [insn_num] == NULL)
6998     /* We should find a solution because the 2nd insn scheduling has
6999        found one.  */
7000     abort ();
7001   /* Find a state corresponding to the best insn sequence.  */
7002   best_state = NULL;
7003   for (curr_state = index_to_bundle_states [insn_num];
7004        curr_state != NULL;
7005        curr_state = curr_state->next)
7006     /* We are just looking at the states with fully filled up last
7007        bundle.  The first we prefer insn sequences with minimal cost
7008        then with minimal inserted nops and finally with branch insns
7009        placed in the 3rd slots.  */
7010     if (curr_state->accumulated_insns_num % 3 == 0
7011         && (best_state == NULL || best_state->cost > curr_state->cost
7012             || (best_state->cost == curr_state->cost
7013                 && (curr_state->accumulated_insns_num
7014                     < best_state->accumulated_insns_num
7015                     || (curr_state->accumulated_insns_num
7016                         == best_state->accumulated_insns_num
7017                         && curr_state->branch_deviation
7018                         < best_state->branch_deviation)))))
7019       best_state = curr_state;
7020   /* Second (backward) pass: adding nops and templates.  */
7021   insn_num = best_state->before_nops_num;
7022   template0 = template1 = -1;
7023   for (curr_state = best_state;
7024        curr_state->originator != NULL;
7025        curr_state = curr_state->originator)
7026     {
7027       insn = curr_state->insn;
7028       asm_p = (GET_CODE (PATTERN (insn)) == ASM_INPUT
7029                || asm_noperands (PATTERN (insn)) >= 0);
7030       insn_num++;
7031       if (verbose >= 2 && dump)
7032         {
7033           struct DFA_chip
7034           {
7035             unsigned short one_automaton_state;
7036             unsigned short oneb_automaton_state;
7037             unsigned short two_automaton_state;
7038             unsigned short twob_automaton_state;
7039           };
7040
7041           fprintf
7042             (dump,
7043              "//    Best %d (orig %d, cost %d, nops %d/%d, insns %d, branch %d, state %d) for %d\n",
7044              curr_state->unique_num,
7045              (curr_state->originator == NULL
7046               ? -1 : curr_state->originator->unique_num),
7047              curr_state->cost,
7048              curr_state->before_nops_num, curr_state->after_nops_num,
7049              curr_state->accumulated_insns_num, curr_state->branch_deviation,
7050              (ia64_tune == PROCESSOR_ITANIUM
7051               ? ((struct DFA_chip *) curr_state->dfa_state)->oneb_automaton_state
7052               : ((struct DFA_chip *) curr_state->dfa_state)->twob_automaton_state),
7053              INSN_UID (insn));
7054         }
7055       /* Find the position in the current bundle window.  The window can
7056          contain at most two bundles.  Two bundle window means that
7057          the processor will make two bundle rotation.  */
7058       max_pos = get_max_pos (curr_state->dfa_state);
7059       if (max_pos == 6
7060           /* The following (negative template number) means that the
7061              processor did one bundle rotation.  */
7062           || (max_pos == 3 && template0 < 0))
7063         {
7064           /* We are at the end of the window -- find template(s) for
7065              its bundle(s).  */
7066           pos = max_pos;
7067           if (max_pos == 3)
7068             template0 = get_template (curr_state->dfa_state, 3);
7069           else
7070             {
7071               template1 = get_template (curr_state->dfa_state, 3);
7072               template0 = get_template (curr_state->dfa_state, 6);
7073             }
7074         }
7075       if (max_pos > 3 && template1 < 0)
7076         /* It may happen when we have the stop inside a bundle.  */
7077         {
7078           if (pos > 3)
7079             abort ();
7080           template1 = get_template (curr_state->dfa_state, 3);
7081           pos += 3;
7082         }
7083       if (!asm_p)
7084         /* Emit nops after the current insn.  */
7085         for (i = 0; i < curr_state->after_nops_num; i++)
7086           {
7087             nop = gen_nop ();
7088             emit_insn_after (nop, insn);
7089             pos--;
7090             if (pos < 0)
7091               abort ();
7092             if (pos % 3 == 0)
7093               {
7094                 /* We are at the start of a bundle: emit the template
7095                    (it should be defined).  */
7096                 if (template0 < 0)
7097                   abort ();
7098                 b = gen_bundle_selector (GEN_INT (template0));
7099                 ia64_emit_insn_before (b, nop);
7100                 /* If we have two bundle window, we make one bundle
7101                    rotation.  Otherwise template0 will be undefined
7102                    (negative value).  */
7103                 template0 = template1;
7104                 template1 = -1;
7105               }
7106           }
7107       /* Move the position backward in the window.  Group barrier has
7108          no slot.  Asm insn takes all bundle.  */
7109       if (INSN_CODE (insn) != CODE_FOR_insn_group_barrier
7110           && GET_CODE (PATTERN (insn)) != ASM_INPUT
7111           && asm_noperands (PATTERN (insn)) < 0)
7112         pos--;
7113       /* Long insn takes 2 slots.  */
7114       if (ia64_safe_type (insn) == TYPE_L)
7115         pos--;
7116       if (pos < 0)
7117         abort ();
7118       if (pos % 3 == 0
7119           && INSN_CODE (insn) != CODE_FOR_insn_group_barrier
7120           && GET_CODE (PATTERN (insn)) != ASM_INPUT
7121           && asm_noperands (PATTERN (insn)) < 0)
7122         {
7123           /* The current insn is at the bundle start: emit the
7124              template.  */
7125           if (template0 < 0)
7126             abort ();
7127           b = gen_bundle_selector (GEN_INT (template0));
7128           ia64_emit_insn_before (b, insn);
7129           b = PREV_INSN (insn);
7130           insn = b;
7131           /* See comment above in analogous place for emitting nops
7132              after the insn.  */
7133           template0 = template1;
7134           template1 = -1;
7135         }
7136       /* Emit nops after the current insn.  */
7137       for (i = 0; i < curr_state->before_nops_num; i++)
7138         {
7139           nop = gen_nop ();
7140           ia64_emit_insn_before (nop, insn);
7141           nop = PREV_INSN (insn);
7142           insn = nop;
7143           pos--;
7144           if (pos < 0)
7145             abort ();
7146           if (pos % 3 == 0)
7147             {
7148               /* See comment above in analogous place for emitting nops
7149                  after the insn.  */
7150               if (template0 < 0)
7151                 abort ();
7152               b = gen_bundle_selector (GEN_INT (template0));
7153               ia64_emit_insn_before (b, insn);
7154               b = PREV_INSN (insn);
7155               insn = b;
7156               template0 = template1;
7157               template1 = -1;
7158             }
7159         }
7160     }
7161   if (ia64_tune == PROCESSOR_ITANIUM)
7162     /* Insert additional cycles for MM-insns (MMMUL and MMSHF).
7163        Itanium1 has a strange design, if the distance between an insn
7164        and dependent MM-insn is less 4 then we have a 6 additional
7165        cycles stall.  So we make the distance equal to 4 cycles if it
7166        is less.  */
7167     for (insn = get_next_important_insn (NEXT_INSN (prev_head_insn), tail);
7168          insn != NULL_RTX;
7169          insn = next_insn)
7170       {
7171         if (!INSN_P (insn)
7172             || ia64_safe_itanium_class (insn) == ITANIUM_CLASS_IGNORE
7173             || GET_CODE (PATTERN (insn)) == USE
7174             || GET_CODE (PATTERN (insn)) == CLOBBER)
7175           abort ();
7176         next_insn = get_next_important_insn (NEXT_INSN (insn), tail);
7177         if (INSN_UID (insn) < clocks_length && add_cycles [INSN_UID (insn)])
7178           /* We found a MM-insn which needs additional cycles.  */
7179           {
7180             rtx last;
7181             int i, j, n;
7182             int pred_stop_p;
7183
7184             /* Now we are searching for a template of the bundle in
7185                which the MM-insn is placed and the position of the
7186                insn in the bundle (0, 1, 2).  Also we are searching
7187                for that there is a stop before the insn.  */
7188             last = prev_active_insn (insn);
7189             pred_stop_p = recog_memoized (last) == CODE_FOR_insn_group_barrier;
7190             if (pred_stop_p)
7191               last = prev_active_insn (last);
7192             n = 0;
7193             for (;; last = prev_active_insn (last))
7194               if (recog_memoized (last) == CODE_FOR_bundle_selector)
7195                 {
7196                   template0 = XINT (XVECEXP (PATTERN (last), 0, 0), 0);
7197                   if (template0 == 9)
7198                     /* The insn is in MLX bundle.  Change the template
7199                        onto MFI because we will add nops before the
7200                        insn.  It simplifies subsequent code a lot.  */
7201                     PATTERN (last)
7202                       = gen_bundle_selector (const2_rtx); /* -> MFI */
7203                   break;
7204                 }
7205               else if (recog_memoized (last) != CODE_FOR_insn_group_barrier)
7206                 n++;
7207             /* Some check of correctness: the stop is not at the
7208                bundle start, there are no more 3 insns in the bundle,
7209                and the MM-insn is not at the start of bundle with
7210                template MLX.  */
7211             if ((pred_stop_p && n == 0) || n > 2
7212                 || (template0 == 9 && n != 0))
7213               abort ();
7214             /* Put nops after the insn in the bundle.  */
7215             for (j = 3 - n; j > 0; j --)
7216               ia64_emit_insn_before (gen_nop (), insn);
7217             /* It takes into account that we will add more N nops
7218                before the insn lately -- please see code below.  */
7219             add_cycles [INSN_UID (insn)]--;
7220             if (!pred_stop_p || add_cycles [INSN_UID (insn)])
7221               ia64_emit_insn_before (gen_insn_group_barrier (GEN_INT (3)),
7222                                      insn);
7223             if (pred_stop_p)
7224               add_cycles [INSN_UID (insn)]--;
7225             for (i = add_cycles [INSN_UID (insn)]; i > 0; i--)
7226               {
7227                 /* Insert "MII;" template.  */
7228                 ia64_emit_insn_before (gen_bundle_selector (const0_rtx),
7229                                        insn);
7230                 ia64_emit_insn_before (gen_nop (), insn);
7231                 ia64_emit_insn_before (gen_nop (), insn);
7232                 if (i > 1)
7233                   {
7234                     /* To decrease code size, we use "MI;I;"
7235                        template.  */
7236                     ia64_emit_insn_before
7237                       (gen_insn_group_barrier (GEN_INT (3)), insn);
7238                     i--;
7239                   }
7240                 ia64_emit_insn_before (gen_nop (), insn);
7241                 ia64_emit_insn_before (gen_insn_group_barrier (GEN_INT (3)),
7242                                        insn);
7243               }
7244             /* Put the MM-insn in the same slot of a bundle with the
7245                same template as the original one.  */
7246             ia64_emit_insn_before (gen_bundle_selector (GEN_INT (template0)),
7247                                    insn);
7248             /* To put the insn in the same slot, add necessary number
7249                of nops.  */
7250             for (j = n; j > 0; j --)
7251               ia64_emit_insn_before (gen_nop (), insn);
7252             /* Put the stop if the original bundle had it.  */
7253             if (pred_stop_p)
7254               ia64_emit_insn_before (gen_insn_group_barrier (GEN_INT (3)),
7255                                      insn);
7256           }
7257       }
7258   free (index_to_bundle_states);
7259   finish_bundle_state_table ();
7260   bundling_p = 0;
7261   dfa_clean_insn_cache ();
7262 }
7263
7264 /* The following function is called at the end of scheduling BB or
7265    EBB.  After reload, it inserts stop bits and does insn bundling.  */
7266
7267 static void
7268 ia64_sched_finish (FILE *dump, int sched_verbose)
7269 {
7270   if (sched_verbose)
7271     fprintf (dump, "// Finishing schedule.\n");
7272   if (!reload_completed)
7273     return;
7274   if (reload_completed)
7275     {
7276       final_emit_insn_group_barriers (dump);
7277       bundling (dump, sched_verbose, current_sched_info->prev_head,
7278                 current_sched_info->next_tail);
7279       if (sched_verbose && dump)
7280         fprintf (dump, "//    finishing %d-%d\n",
7281                  INSN_UID (NEXT_INSN (current_sched_info->prev_head)),
7282                  INSN_UID (PREV_INSN (current_sched_info->next_tail)));
7283
7284       return;
7285     }
7286 }
7287
7288 /* The following function inserts stop bits in scheduled BB or EBB.  */
7289
7290 static void
7291 final_emit_insn_group_barriers (FILE *dump ATTRIBUTE_UNUSED)
7292 {
7293   rtx insn;
7294   int need_barrier_p = 0;
7295   rtx prev_insn = NULL_RTX;
7296
7297   init_insn_group_barriers ();
7298
7299   for (insn = NEXT_INSN (current_sched_info->prev_head);
7300        insn != current_sched_info->next_tail;
7301        insn = NEXT_INSN (insn))
7302     {
7303       if (GET_CODE (insn) == BARRIER)
7304         {
7305           rtx last = prev_active_insn (insn);
7306
7307           if (! last)
7308             continue;
7309           if (GET_CODE (last) == JUMP_INSN
7310               && GET_CODE (PATTERN (last)) == ADDR_DIFF_VEC)
7311             last = prev_active_insn (last);
7312           if (recog_memoized (last) != CODE_FOR_insn_group_barrier)
7313             emit_insn_after (gen_insn_group_barrier (GEN_INT (3)), last);
7314
7315           init_insn_group_barriers ();
7316           need_barrier_p = 0;
7317           prev_insn = NULL_RTX;
7318         }
7319       else if (INSN_P (insn))
7320         {
7321           if (recog_memoized (insn) == CODE_FOR_insn_group_barrier)
7322             {
7323               init_insn_group_barriers ();
7324               need_barrier_p = 0;
7325               prev_insn = NULL_RTX;
7326             }
7327           else if (need_barrier_p || group_barrier_needed_p (insn))
7328             {
7329               if (TARGET_EARLY_STOP_BITS)
7330                 {
7331                   rtx last;
7332
7333                   for (last = insn;
7334                        last != current_sched_info->prev_head;
7335                        last = PREV_INSN (last))
7336                     if (INSN_P (last) && GET_MODE (last) == TImode
7337                         && stops_p [INSN_UID (last)])
7338                       break;
7339                   if (last == current_sched_info->prev_head)
7340                     last = insn;
7341                   last = prev_active_insn (last);
7342                   if (last
7343                       && recog_memoized (last) != CODE_FOR_insn_group_barrier)
7344                     emit_insn_after (gen_insn_group_barrier (GEN_INT (3)),
7345                                      last);
7346                   init_insn_group_barriers ();
7347                   for (last = NEXT_INSN (last);
7348                        last != insn;
7349                        last = NEXT_INSN (last))
7350                     if (INSN_P (last))
7351                       group_barrier_needed_p (last);
7352                 }
7353               else
7354                 {
7355                   emit_insn_before (gen_insn_group_barrier (GEN_INT (3)),
7356                                     insn);
7357                   init_insn_group_barriers ();
7358                 }
7359               group_barrier_needed_p (insn);
7360               prev_insn = NULL_RTX;
7361             }
7362           else if (recog_memoized (insn) >= 0)
7363             prev_insn = insn;
7364           need_barrier_p = (GET_CODE (insn) == CALL_INSN
7365                             || GET_CODE (PATTERN (insn)) == ASM_INPUT
7366                             || asm_noperands (PATTERN (insn)) >= 0);
7367         }
7368     }
7369 }
7370
7371 \f
7372
7373 /* If the following function returns TRUE, we will use the the DFA
7374    insn scheduler.  */
7375
7376 static int
7377 ia64_use_dfa_pipeline_interface (void)
7378 {
7379   return 1;
7380 }
7381
7382 /* If the following function returns TRUE, we will use the the DFA
7383    insn scheduler.  */
7384
7385 static int
7386 ia64_first_cycle_multipass_dfa_lookahead (void)
7387 {
7388   return (reload_completed ? 6 : 4);
7389 }
7390
7391 /* The following function initiates variable `dfa_pre_cycle_insn'.  */
7392
7393 static void
7394 ia64_init_dfa_pre_cycle_insn (void)
7395 {
7396   if (temp_dfa_state == NULL)
7397     {
7398       dfa_state_size = state_size ();
7399       temp_dfa_state = xmalloc (dfa_state_size);
7400       prev_cycle_state = xmalloc (dfa_state_size);
7401     }
7402   dfa_pre_cycle_insn = make_insn_raw (gen_pre_cycle ());
7403   PREV_INSN (dfa_pre_cycle_insn) = NEXT_INSN (dfa_pre_cycle_insn) = NULL_RTX;
7404   recog_memoized (dfa_pre_cycle_insn);
7405   dfa_stop_insn = make_insn_raw (gen_insn_group_barrier (GEN_INT (3)));
7406   PREV_INSN (dfa_stop_insn) = NEXT_INSN (dfa_stop_insn) = NULL_RTX;
7407   recog_memoized (dfa_stop_insn);
7408 }
7409
7410 /* The following function returns the pseudo insn DFA_PRE_CYCLE_INSN
7411    used by the DFA insn scheduler.  */
7412
7413 static rtx
7414 ia64_dfa_pre_cycle_insn (void)
7415 {
7416   return dfa_pre_cycle_insn;
7417 }
7418
7419 /* The following function returns TRUE if PRODUCER (of type ilog or
7420    ld) produces address for CONSUMER (of type st or stf). */
7421
7422 int
7423 ia64_st_address_bypass_p (rtx producer, rtx consumer)
7424 {
7425   rtx dest, reg, mem;
7426
7427   if (producer == NULL_RTX || consumer == NULL_RTX)
7428     abort ();
7429   dest = ia64_single_set (producer);
7430   if (dest == NULL_RTX || (reg = SET_DEST (dest)) == NULL_RTX
7431       || (GET_CODE (reg) != REG && GET_CODE (reg) != SUBREG))
7432     abort ();
7433   if (GET_CODE (reg) == SUBREG)
7434     reg = SUBREG_REG (reg);
7435   dest = ia64_single_set (consumer);
7436   if (dest == NULL_RTX || (mem = SET_DEST (dest)) == NULL_RTX
7437       || GET_CODE (mem) != MEM)
7438     abort ();
7439   return reg_mentioned_p (reg, mem);
7440 }
7441
7442 /* The following function returns TRUE if PRODUCER (of type ilog or
7443    ld) produces address for CONSUMER (of type ld or fld). */
7444
7445 int
7446 ia64_ld_address_bypass_p (rtx producer, rtx consumer)
7447 {
7448   rtx dest, src, reg, mem;
7449
7450   if (producer == NULL_RTX || consumer == NULL_RTX)
7451     abort ();
7452   dest = ia64_single_set (producer);
7453   if (dest == NULL_RTX || (reg = SET_DEST (dest)) == NULL_RTX
7454       || (GET_CODE (reg) != REG && GET_CODE (reg) != SUBREG))
7455     abort ();
7456   if (GET_CODE (reg) == SUBREG)
7457     reg = SUBREG_REG (reg);
7458   src = ia64_single_set (consumer);
7459   if (src == NULL_RTX || (mem = SET_SRC (src)) == NULL_RTX)
7460     abort ();
7461   if (GET_CODE (mem) == UNSPEC && XVECLEN (mem, 0) > 0)
7462     mem = XVECEXP (mem, 0, 0);
7463   while (GET_CODE (mem) == SUBREG || GET_CODE (mem) == ZERO_EXTEND)
7464     mem = XEXP (mem, 0);
7465
7466   /* Note that LO_SUM is used for GOT loads.  */
7467   if (GET_CODE (mem) != LO_SUM && GET_CODE (mem) != MEM)
7468     abort ();
7469
7470   return reg_mentioned_p (reg, mem);
7471 }
7472
7473 /* The following function returns TRUE if INSN produces address for a
7474    load/store insn.  We will place such insns into M slot because it
7475    decreases its latency time.  */
7476
7477 int
7478 ia64_produce_address_p (rtx insn)
7479 {
7480   return insn->call;
7481 }
7482
7483 \f
7484 /* Emit pseudo-ops for the assembler to describe predicate relations.
7485    At present this assumes that we only consider predicate pairs to
7486    be mutex, and that the assembler can deduce proper values from
7487    straight-line code.  */
7488
7489 static void
7490 emit_predicate_relation_info (void)
7491 {
7492   basic_block bb;
7493
7494   FOR_EACH_BB_REVERSE (bb)
7495     {
7496       int r;
7497       rtx head = BB_HEAD (bb);
7498
7499       /* We only need such notes at code labels.  */
7500       if (GET_CODE (head) != CODE_LABEL)
7501         continue;
7502       if (GET_CODE (NEXT_INSN (head)) == NOTE
7503           && NOTE_LINE_NUMBER (NEXT_INSN (head)) == NOTE_INSN_BASIC_BLOCK)
7504         head = NEXT_INSN (head);
7505
7506       for (r = PR_REG (0); r < PR_REG (64); r += 2)
7507         if (REGNO_REG_SET_P (bb->global_live_at_start, r))
7508           {
7509             rtx p = gen_rtx_REG (BImode, r);
7510             rtx n = emit_insn_after (gen_pred_rel_mutex (p), head);
7511             if (head == BB_END (bb))
7512               BB_END (bb) = n;
7513             head = n;
7514           }
7515     }
7516
7517   /* Look for conditional calls that do not return, and protect predicate
7518      relations around them.  Otherwise the assembler will assume the call
7519      returns, and complain about uses of call-clobbered predicates after
7520      the call.  */
7521   FOR_EACH_BB_REVERSE (bb)
7522     {
7523       rtx insn = BB_HEAD (bb);
7524
7525       while (1)
7526         {
7527           if (GET_CODE (insn) == CALL_INSN
7528               && GET_CODE (PATTERN (insn)) == COND_EXEC
7529               && find_reg_note (insn, REG_NORETURN, NULL_RTX))
7530             {
7531               rtx b = emit_insn_before (gen_safe_across_calls_all (), insn);
7532               rtx a = emit_insn_after (gen_safe_across_calls_normal (), insn);
7533               if (BB_HEAD (bb) == insn)
7534                 BB_HEAD (bb) = b;
7535               if (BB_END (bb) == insn)
7536                 BB_END (bb) = a;
7537             }
7538
7539           if (insn == BB_END (bb))
7540             break;
7541           insn = NEXT_INSN (insn);
7542         }
7543     }
7544 }
7545
7546 /* Perform machine dependent operations on the rtl chain INSNS.  */
7547
7548 static void
7549 ia64_reorg (void)
7550 {
7551   /* We are freeing block_for_insn in the toplev to keep compatibility
7552      with old MDEP_REORGS that are not CFG based.  Recompute it now.  */
7553   compute_bb_for_insn ();
7554
7555   /* If optimizing, we'll have split before scheduling.  */
7556   if (optimize == 0)
7557     split_all_insns (0);
7558
7559   /* ??? update_life_info_in_dirty_blocks fails to terminate during
7560      non-optimizing bootstrap.  */
7561   update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES, PROP_DEATH_NOTES);
7562
7563   if (ia64_flag_schedule_insns2)
7564     {
7565       timevar_push (TV_SCHED2);
7566       ia64_final_schedule = 1;
7567
7568       initiate_bundle_states ();
7569       ia64_nop = make_insn_raw (gen_nop ());
7570       PREV_INSN (ia64_nop) = NEXT_INSN (ia64_nop) = NULL_RTX;
7571       recog_memoized (ia64_nop);
7572       clocks_length = get_max_uid () + 1;
7573       stops_p = xcalloc (1, clocks_length);
7574       if (ia64_tune == PROCESSOR_ITANIUM)
7575         {
7576           clocks = xcalloc (clocks_length, sizeof (int));
7577           add_cycles = xcalloc (clocks_length, sizeof (int));
7578         }
7579       if (ia64_tune == PROCESSOR_ITANIUM2)
7580         {
7581           pos_1 = get_cpu_unit_code ("2_1");
7582           pos_2 = get_cpu_unit_code ("2_2");
7583           pos_3 = get_cpu_unit_code ("2_3");
7584           pos_4 = get_cpu_unit_code ("2_4");
7585           pos_5 = get_cpu_unit_code ("2_5");
7586           pos_6 = get_cpu_unit_code ("2_6");
7587           _0mii_ = get_cpu_unit_code ("2b_0mii.");
7588           _0mmi_ = get_cpu_unit_code ("2b_0mmi.");
7589           _0mfi_ = get_cpu_unit_code ("2b_0mfi.");
7590           _0mmf_ = get_cpu_unit_code ("2b_0mmf.");
7591           _0bbb_ = get_cpu_unit_code ("2b_0bbb.");
7592           _0mbb_ = get_cpu_unit_code ("2b_0mbb.");
7593           _0mib_ = get_cpu_unit_code ("2b_0mib.");
7594           _0mmb_ = get_cpu_unit_code ("2b_0mmb.");
7595           _0mfb_ = get_cpu_unit_code ("2b_0mfb.");
7596           _0mlx_ = get_cpu_unit_code ("2b_0mlx.");
7597           _1mii_ = get_cpu_unit_code ("2b_1mii.");
7598           _1mmi_ = get_cpu_unit_code ("2b_1mmi.");
7599           _1mfi_ = get_cpu_unit_code ("2b_1mfi.");
7600           _1mmf_ = get_cpu_unit_code ("2b_1mmf.");
7601           _1bbb_ = get_cpu_unit_code ("2b_1bbb.");
7602           _1mbb_ = get_cpu_unit_code ("2b_1mbb.");
7603           _1mib_ = get_cpu_unit_code ("2b_1mib.");
7604           _1mmb_ = get_cpu_unit_code ("2b_1mmb.");
7605           _1mfb_ = get_cpu_unit_code ("2b_1mfb.");
7606           _1mlx_ = get_cpu_unit_code ("2b_1mlx.");
7607         }
7608       else
7609         {
7610           pos_1 = get_cpu_unit_code ("1_1");
7611           pos_2 = get_cpu_unit_code ("1_2");
7612           pos_3 = get_cpu_unit_code ("1_3");
7613           pos_4 = get_cpu_unit_code ("1_4");
7614           pos_5 = get_cpu_unit_code ("1_5");
7615           pos_6 = get_cpu_unit_code ("1_6");
7616           _0mii_ = get_cpu_unit_code ("1b_0mii.");
7617           _0mmi_ = get_cpu_unit_code ("1b_0mmi.");
7618           _0mfi_ = get_cpu_unit_code ("1b_0mfi.");
7619           _0mmf_ = get_cpu_unit_code ("1b_0mmf.");
7620           _0bbb_ = get_cpu_unit_code ("1b_0bbb.");
7621           _0mbb_ = get_cpu_unit_code ("1b_0mbb.");
7622           _0mib_ = get_cpu_unit_code ("1b_0mib.");
7623           _0mmb_ = get_cpu_unit_code ("1b_0mmb.");
7624           _0mfb_ = get_cpu_unit_code ("1b_0mfb.");
7625           _0mlx_ = get_cpu_unit_code ("1b_0mlx.");
7626           _1mii_ = get_cpu_unit_code ("1b_1mii.");
7627           _1mmi_ = get_cpu_unit_code ("1b_1mmi.");
7628           _1mfi_ = get_cpu_unit_code ("1b_1mfi.");
7629           _1mmf_ = get_cpu_unit_code ("1b_1mmf.");
7630           _1bbb_ = get_cpu_unit_code ("1b_1bbb.");
7631           _1mbb_ = get_cpu_unit_code ("1b_1mbb.");
7632           _1mib_ = get_cpu_unit_code ("1b_1mib.");
7633           _1mmb_ = get_cpu_unit_code ("1b_1mmb.");
7634           _1mfb_ = get_cpu_unit_code ("1b_1mfb.");
7635           _1mlx_ = get_cpu_unit_code ("1b_1mlx.");
7636         }
7637       schedule_ebbs (dump_file);
7638       finish_bundle_states ();
7639       if (ia64_tune == PROCESSOR_ITANIUM)
7640         {
7641           free (add_cycles);
7642           free (clocks);
7643         }
7644       free (stops_p);
7645       emit_insn_group_barriers (dump_file);
7646
7647       ia64_final_schedule = 0;
7648       timevar_pop (TV_SCHED2);
7649     }
7650   else
7651     emit_all_insn_group_barriers (dump_file);
7652
7653   /* A call must not be the last instruction in a function, so that the
7654      return address is still within the function, so that unwinding works
7655      properly.  Note that IA-64 differs from dwarf2 on this point.  */
7656   if (flag_unwind_tables || (flag_exceptions && !USING_SJLJ_EXCEPTIONS))
7657     {
7658       rtx insn;
7659       int saw_stop = 0;
7660
7661       insn = get_last_insn ();
7662       if (! INSN_P (insn))
7663         insn = prev_active_insn (insn);
7664       /* Skip over insns that expand to nothing.  */
7665       while (GET_CODE (insn) == INSN && get_attr_empty (insn) == EMPTY_YES)
7666         {
7667           if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
7668               && XINT (PATTERN (insn), 1) == UNSPECV_INSN_GROUP_BARRIER)
7669             saw_stop = 1;
7670           insn = prev_active_insn (insn);
7671         }
7672       if (GET_CODE (insn) == CALL_INSN)
7673         {
7674           if (! saw_stop)
7675             emit_insn (gen_insn_group_barrier (GEN_INT (3)));
7676           emit_insn (gen_break_f ());
7677           emit_insn (gen_insn_group_barrier (GEN_INT (3)));
7678         }
7679     }
7680
7681   fixup_errata ();
7682   emit_predicate_relation_info ();
7683
7684   if (ia64_flag_var_tracking)
7685     {
7686       timevar_push (TV_VAR_TRACKING);
7687       variable_tracking_main ();
7688       timevar_pop (TV_VAR_TRACKING);
7689     }
7690 }
7691 \f
7692 /* Return true if REGNO is used by the epilogue.  */
7693
7694 int
7695 ia64_epilogue_uses (int regno)
7696 {
7697   switch (regno)
7698     {
7699     case R_GR (1):
7700       /* With a call to a function in another module, we will write a new
7701          value to "gp".  After returning from such a call, we need to make
7702          sure the function restores the original gp-value, even if the
7703          function itself does not use the gp anymore.  */
7704       return !(TARGET_AUTO_PIC || TARGET_NO_PIC);
7705
7706     case IN_REG (0): case IN_REG (1): case IN_REG (2): case IN_REG (3):
7707     case IN_REG (4): case IN_REG (5): case IN_REG (6): case IN_REG (7):
7708       /* For functions defined with the syscall_linkage attribute, all
7709          input registers are marked as live at all function exits.  This
7710          prevents the register allocator from using the input registers,
7711          which in turn makes it possible to restart a system call after
7712          an interrupt without having to save/restore the input registers.
7713          This also prevents kernel data from leaking to application code.  */
7714       return lookup_attribute ("syscall_linkage",
7715            TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))) != NULL;
7716
7717     case R_BR (0):
7718       /* Conditional return patterns can't represent the use of `b0' as
7719          the return address, so we force the value live this way.  */
7720       return 1;
7721
7722     case AR_PFS_REGNUM:
7723       /* Likewise for ar.pfs, which is used by br.ret.  */
7724       return 1;
7725
7726     default:
7727       return 0;
7728     }
7729 }
7730
7731 /* Return true if REGNO is used by the frame unwinder.  */
7732
7733 int
7734 ia64_eh_uses (int regno)
7735 {
7736   if (! reload_completed)
7737     return 0;
7738
7739   if (current_frame_info.reg_save_b0
7740       && regno == current_frame_info.reg_save_b0)
7741     return 1;
7742   if (current_frame_info.reg_save_pr
7743       && regno == current_frame_info.reg_save_pr)
7744     return 1;
7745   if (current_frame_info.reg_save_ar_pfs
7746       && regno == current_frame_info.reg_save_ar_pfs)
7747     return 1;
7748   if (current_frame_info.reg_save_ar_unat
7749       && regno == current_frame_info.reg_save_ar_unat)
7750     return 1;
7751   if (current_frame_info.reg_save_ar_lc
7752       && regno == current_frame_info.reg_save_ar_lc)
7753     return 1;
7754
7755   return 0;
7756 }
7757 \f
7758 /* Return true if this goes in small data/bss.  */
7759
7760 /* ??? We could also support own long data here.  Generating movl/add/ld8
7761    instead of addl,ld8/ld8.  This makes the code bigger, but should make the
7762    code faster because there is one less load.  This also includes incomplete
7763    types which can't go in sdata/sbss.  */
7764
7765 static bool
7766 ia64_in_small_data_p (tree exp)
7767 {
7768   if (TARGET_NO_SDATA)
7769     return false;
7770
7771   /* We want to merge strings, so we never consider them small data.  */
7772   if (TREE_CODE (exp) == STRING_CST)
7773     return false;
7774
7775   /* Functions are never small data.  */
7776   if (TREE_CODE (exp) == FUNCTION_DECL)
7777     return false;
7778
7779   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
7780     {
7781       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
7782       if (strcmp (section, ".sdata") == 0
7783           || strcmp (section, ".sbss") == 0)
7784         return true;
7785     }
7786   else
7787     {
7788       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
7789
7790       /* If this is an incomplete type with size 0, then we can't put it
7791          in sdata because it might be too big when completed.  */
7792       if (size > 0 && size <= ia64_section_threshold)
7793         return true;
7794     }
7795
7796   return false;
7797 }
7798 \f
7799 /* Output assembly directives for prologue regions.  */
7800
7801 /* The current basic block number.  */
7802
7803 static bool last_block;
7804
7805 /* True if we need a copy_state command at the start of the next block.  */
7806
7807 static bool need_copy_state;
7808
7809 /* The function emits unwind directives for the start of an epilogue.  */
7810
7811 static void
7812 process_epilogue (void)
7813 {
7814   /* If this isn't the last block of the function, then we need to label the
7815      current state, and copy it back in at the start of the next block.  */
7816
7817   if (!last_block)
7818     {
7819       fprintf (asm_out_file, "\t.label_state 1\n");
7820       need_copy_state = true;
7821     }
7822
7823   fprintf (asm_out_file, "\t.restore sp\n");
7824 }
7825
7826 /* This function processes a SET pattern looking for specific patterns
7827    which result in emitting an assembly directive required for unwinding.  */
7828
7829 static int
7830 process_set (FILE *asm_out_file, rtx pat)
7831 {
7832   rtx src = SET_SRC (pat);
7833   rtx dest = SET_DEST (pat);
7834   int src_regno, dest_regno;
7835
7836   /* Look for the ALLOC insn.  */
7837   if (GET_CODE (src) == UNSPEC_VOLATILE
7838       && XINT (src, 1) == UNSPECV_ALLOC
7839       && GET_CODE (dest) == REG)
7840     {
7841       dest_regno = REGNO (dest);
7842
7843       /* If this isn't the final destination for ar.pfs, the alloc
7844          shouldn't have been marked frame related.  */
7845       if (dest_regno != current_frame_info.reg_save_ar_pfs)
7846         abort ();
7847
7848       fprintf (asm_out_file, "\t.save ar.pfs, r%d\n",
7849                ia64_dbx_register_number (dest_regno));
7850       return 1;
7851     }
7852
7853   /* Look for SP = ....  */
7854   if (GET_CODE (dest) == REG && REGNO (dest) == STACK_POINTER_REGNUM)
7855     {
7856       if (GET_CODE (src) == PLUS)
7857         {
7858           rtx op0 = XEXP (src, 0);
7859           rtx op1 = XEXP (src, 1);
7860           if (op0 == dest && GET_CODE (op1) == CONST_INT)
7861             {
7862               if (INTVAL (op1) < 0)
7863                 fprintf (asm_out_file, "\t.fframe "HOST_WIDE_INT_PRINT_DEC"\n",
7864                          -INTVAL (op1));
7865               else
7866                 process_epilogue ();
7867             }
7868           else
7869             abort ();
7870         }
7871       else if (GET_CODE (src) == REG
7872                && REGNO (src) == HARD_FRAME_POINTER_REGNUM)
7873         process_epilogue ();
7874       else
7875         abort ();
7876
7877       return 1;
7878     }
7879
7880   /* Register move we need to look at.  */
7881   if (GET_CODE (dest) == REG && GET_CODE (src) == REG)
7882     {
7883       src_regno = REGNO (src);
7884       dest_regno = REGNO (dest);
7885
7886       switch (src_regno)
7887         {
7888         case BR_REG (0):
7889           /* Saving return address pointer.  */
7890           if (dest_regno != current_frame_info.reg_save_b0)
7891             abort ();
7892           fprintf (asm_out_file, "\t.save rp, r%d\n",
7893                    ia64_dbx_register_number (dest_regno));
7894           return 1;
7895
7896         case PR_REG (0):
7897           if (dest_regno != current_frame_info.reg_save_pr)
7898             abort ();
7899           fprintf (asm_out_file, "\t.save pr, r%d\n",
7900                    ia64_dbx_register_number (dest_regno));
7901           return 1;
7902
7903         case AR_UNAT_REGNUM:
7904           if (dest_regno != current_frame_info.reg_save_ar_unat)
7905             abort ();
7906           fprintf (asm_out_file, "\t.save ar.unat, r%d\n",
7907                    ia64_dbx_register_number (dest_regno));
7908           return 1;
7909
7910         case AR_LC_REGNUM:
7911           if (dest_regno != current_frame_info.reg_save_ar_lc)
7912             abort ();
7913           fprintf (asm_out_file, "\t.save ar.lc, r%d\n",
7914                    ia64_dbx_register_number (dest_regno));
7915           return 1;
7916
7917         case STACK_POINTER_REGNUM:
7918           if (dest_regno != HARD_FRAME_POINTER_REGNUM
7919               || ! frame_pointer_needed)
7920             abort ();
7921           fprintf (asm_out_file, "\t.vframe r%d\n",
7922                    ia64_dbx_register_number (dest_regno));
7923           return 1;
7924
7925         default:
7926           /* Everything else should indicate being stored to memory.  */
7927           abort ();
7928         }
7929     }
7930
7931   /* Memory store we need to look at.  */
7932   if (GET_CODE (dest) == MEM && GET_CODE (src) == REG)
7933     {
7934       long off;
7935       rtx base;
7936       const char *saveop;
7937
7938       if (GET_CODE (XEXP (dest, 0)) == REG)
7939         {
7940           base = XEXP (dest, 0);
7941           off = 0;
7942         }
7943       else if (GET_CODE (XEXP (dest, 0)) == PLUS
7944                && GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT)
7945         {
7946           base = XEXP (XEXP (dest, 0), 0);
7947           off = INTVAL (XEXP (XEXP (dest, 0), 1));
7948         }
7949       else
7950         abort ();
7951
7952       if (base == hard_frame_pointer_rtx)
7953         {
7954           saveop = ".savepsp";
7955           off = - off;
7956         }
7957       else if (base == stack_pointer_rtx)
7958         saveop = ".savesp";
7959       else
7960         abort ();
7961
7962       src_regno = REGNO (src);
7963       switch (src_regno)
7964         {
7965         case BR_REG (0):
7966           if (current_frame_info.reg_save_b0 != 0)
7967             abort ();
7968           fprintf (asm_out_file, "\t%s rp, %ld\n", saveop, off);
7969           return 1;
7970
7971         case PR_REG (0):
7972           if (current_frame_info.reg_save_pr != 0)
7973             abort ();
7974           fprintf (asm_out_file, "\t%s pr, %ld\n", saveop, off);
7975           return 1;
7976
7977         case AR_LC_REGNUM:
7978           if (current_frame_info.reg_save_ar_lc != 0)
7979             abort ();
7980           fprintf (asm_out_file, "\t%s ar.lc, %ld\n", saveop, off);
7981           return 1;
7982
7983         case AR_PFS_REGNUM:
7984           if (current_frame_info.reg_save_ar_pfs != 0)
7985             abort ();
7986           fprintf (asm_out_file, "\t%s ar.pfs, %ld\n", saveop, off);
7987           return 1;
7988
7989         case AR_UNAT_REGNUM:
7990           if (current_frame_info.reg_save_ar_unat != 0)
7991             abort ();
7992           fprintf (asm_out_file, "\t%s ar.unat, %ld\n", saveop, off);
7993           return 1;
7994
7995         case GR_REG (4):
7996         case GR_REG (5):
7997         case GR_REG (6):
7998         case GR_REG (7):
7999           fprintf (asm_out_file, "\t.save.g 0x%x\n",
8000                    1 << (src_regno - GR_REG (4)));
8001           return 1;
8002
8003         case BR_REG (1):
8004         case BR_REG (2):
8005         case BR_REG (3):
8006         case BR_REG (4):
8007         case BR_REG (5):
8008           fprintf (asm_out_file, "\t.save.b 0x%x\n",
8009                    1 << (src_regno - BR_REG (1)));
8010           return 1;
8011
8012         case FR_REG (2):
8013         case FR_REG (3):
8014         case FR_REG (4):
8015         case FR_REG (5):
8016           fprintf (asm_out_file, "\t.save.f 0x%x\n",
8017                    1 << (src_regno - FR_REG (2)));
8018           return 1;
8019
8020         case FR_REG (16): case FR_REG (17): case FR_REG (18): case FR_REG (19):
8021         case FR_REG (20): case FR_REG (21): case FR_REG (22): case FR_REG (23):
8022         case FR_REG (24): case FR_REG (25): case FR_REG (26): case FR_REG (27):
8023         case FR_REG (28): case FR_REG (29): case FR_REG (30): case FR_REG (31):
8024           fprintf (asm_out_file, "\t.save.gf 0x0, 0x%x\n",
8025                    1 << (src_regno - FR_REG (12)));
8026           return 1;
8027
8028         default:
8029           return 0;
8030         }
8031     }
8032
8033   return 0;
8034 }
8035
8036
8037 /* This function looks at a single insn and emits any directives
8038    required to unwind this insn.  */
8039 void
8040 process_for_unwind_directive (FILE *asm_out_file, rtx insn)
8041 {
8042   if (flag_unwind_tables
8043       || (flag_exceptions && !USING_SJLJ_EXCEPTIONS))
8044     {
8045       rtx pat;
8046
8047       if (GET_CODE (insn) == NOTE
8048           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_BASIC_BLOCK)
8049         {
8050           last_block = NOTE_BASIC_BLOCK (insn)->next_bb == EXIT_BLOCK_PTR;
8051
8052           /* Restore unwind state from immediately before the epilogue.  */
8053           if (need_copy_state)
8054             {
8055               fprintf (asm_out_file, "\t.body\n");
8056               fprintf (asm_out_file, "\t.copy_state 1\n");
8057               need_copy_state = false;
8058             }
8059         }
8060
8061       if (GET_CODE (insn) == NOTE || ! RTX_FRAME_RELATED_P (insn))
8062         return;
8063
8064       pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
8065       if (pat)
8066         pat = XEXP (pat, 0);
8067       else
8068         pat = PATTERN (insn);
8069
8070       switch (GET_CODE (pat))
8071         {
8072         case SET:
8073           process_set (asm_out_file, pat);
8074           break;
8075
8076         case PARALLEL:
8077           {
8078             int par_index;
8079             int limit = XVECLEN (pat, 0);
8080             for (par_index = 0; par_index < limit; par_index++)
8081               {
8082                 rtx x = XVECEXP (pat, 0, par_index);
8083                 if (GET_CODE (x) == SET)
8084                   process_set (asm_out_file, x);
8085               }
8086             break;
8087           }
8088
8089         default:
8090           abort ();
8091         }
8092     }
8093 }
8094
8095 \f
8096 void
8097 ia64_init_builtins (void)
8098 {
8099   tree psi_type_node = build_pointer_type (integer_type_node);
8100   tree pdi_type_node = build_pointer_type (long_integer_type_node);
8101
8102   /* __sync_val_compare_and_swap_si, __sync_bool_compare_and_swap_si */
8103   tree si_ftype_psi_si_si
8104     = build_function_type_list (integer_type_node,
8105                                 psi_type_node, integer_type_node,
8106                                 integer_type_node, NULL_TREE);
8107
8108   /* __sync_val_compare_and_swap_di */
8109   tree di_ftype_pdi_di_di
8110     = build_function_type_list (long_integer_type_node,
8111                                 pdi_type_node, long_integer_type_node,
8112                                 long_integer_type_node, NULL_TREE);
8113   /* __sync_bool_compare_and_swap_di */
8114   tree si_ftype_pdi_di_di
8115     = build_function_type_list (integer_type_node,
8116                                 pdi_type_node, long_integer_type_node,
8117                                 long_integer_type_node, NULL_TREE);
8118   /* __sync_synchronize */
8119   tree void_ftype_void
8120     = build_function_type (void_type_node, void_list_node);
8121
8122   /* __sync_lock_test_and_set_si */
8123   tree si_ftype_psi_si
8124     = build_function_type_list (integer_type_node,
8125                                 psi_type_node, integer_type_node, NULL_TREE);
8126
8127   /* __sync_lock_test_and_set_di */
8128   tree di_ftype_pdi_di
8129     = build_function_type_list (long_integer_type_node,
8130                                 pdi_type_node, long_integer_type_node,
8131                                 NULL_TREE);
8132
8133   /* __sync_lock_release_si */
8134   tree void_ftype_psi
8135     = build_function_type_list (void_type_node, psi_type_node, NULL_TREE);
8136
8137   /* __sync_lock_release_di */
8138   tree void_ftype_pdi
8139     = build_function_type_list (void_type_node, pdi_type_node, NULL_TREE);
8140
8141   tree fpreg_type;
8142   tree float80_type;
8143
8144   /* The __fpreg type.  */
8145   fpreg_type = make_node (REAL_TYPE);
8146   /* ??? The back end should know to load/save __fpreg variables using
8147      the ldf.fill and stf.spill instructions.  */
8148   TYPE_PRECISION (fpreg_type) = 96;
8149   layout_type (fpreg_type);
8150   (*lang_hooks.types.register_builtin_type) (fpreg_type, "__fpreg");
8151
8152   /* The __float80 type.  */
8153   float80_type = make_node (REAL_TYPE);
8154   TYPE_PRECISION (float80_type) = 96;
8155   layout_type (float80_type);
8156   (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
8157
8158   /* The __float128 type.  */
8159   if (!TARGET_HPUX)
8160     {
8161       tree float128_type = make_node (REAL_TYPE);
8162       TYPE_PRECISION (float128_type) = 128;
8163       layout_type (float128_type);
8164       (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
8165     }
8166   else
8167     /* Under HPUX, this is a synonym for "long double".  */
8168     (*lang_hooks.types.register_builtin_type) (long_double_type_node,
8169                                                "__float128");
8170
8171 #define def_builtin(name, type, code) \
8172   builtin_function ((name), (type), (code), BUILT_IN_MD, NULL, NULL_TREE)
8173
8174   def_builtin ("__sync_val_compare_and_swap_si", si_ftype_psi_si_si,
8175                IA64_BUILTIN_VAL_COMPARE_AND_SWAP_SI);
8176   def_builtin ("__sync_val_compare_and_swap_di", di_ftype_pdi_di_di,
8177                IA64_BUILTIN_VAL_COMPARE_AND_SWAP_DI);
8178   def_builtin ("__sync_bool_compare_and_swap_si", si_ftype_psi_si_si,
8179                IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_SI);
8180   def_builtin ("__sync_bool_compare_and_swap_di", si_ftype_pdi_di_di,
8181                IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_DI);
8182
8183   def_builtin ("__sync_synchronize", void_ftype_void,
8184                IA64_BUILTIN_SYNCHRONIZE);
8185
8186   def_builtin ("__sync_lock_test_and_set_si", si_ftype_psi_si,
8187                IA64_BUILTIN_LOCK_TEST_AND_SET_SI);
8188   def_builtin ("__sync_lock_test_and_set_di", di_ftype_pdi_di,
8189                IA64_BUILTIN_LOCK_TEST_AND_SET_DI);
8190   def_builtin ("__sync_lock_release_si", void_ftype_psi,
8191                IA64_BUILTIN_LOCK_RELEASE_SI);
8192   def_builtin ("__sync_lock_release_di", void_ftype_pdi,
8193                IA64_BUILTIN_LOCK_RELEASE_DI);
8194
8195   def_builtin ("__builtin_ia64_bsp",
8196                build_function_type (ptr_type_node, void_list_node),
8197                IA64_BUILTIN_BSP);
8198
8199   def_builtin ("__builtin_ia64_flushrs",
8200                build_function_type (void_type_node, void_list_node),
8201                IA64_BUILTIN_FLUSHRS);
8202
8203   def_builtin ("__sync_fetch_and_add_si", si_ftype_psi_si,
8204                IA64_BUILTIN_FETCH_AND_ADD_SI);
8205   def_builtin ("__sync_fetch_and_sub_si", si_ftype_psi_si,
8206                IA64_BUILTIN_FETCH_AND_SUB_SI);
8207   def_builtin ("__sync_fetch_and_or_si", si_ftype_psi_si,
8208                IA64_BUILTIN_FETCH_AND_OR_SI);
8209   def_builtin ("__sync_fetch_and_and_si", si_ftype_psi_si,
8210                IA64_BUILTIN_FETCH_AND_AND_SI);
8211   def_builtin ("__sync_fetch_and_xor_si", si_ftype_psi_si,
8212                IA64_BUILTIN_FETCH_AND_XOR_SI);
8213   def_builtin ("__sync_fetch_and_nand_si", si_ftype_psi_si,
8214                IA64_BUILTIN_FETCH_AND_NAND_SI);
8215
8216   def_builtin ("__sync_add_and_fetch_si", si_ftype_psi_si,
8217                IA64_BUILTIN_ADD_AND_FETCH_SI);
8218   def_builtin ("__sync_sub_and_fetch_si", si_ftype_psi_si,
8219                IA64_BUILTIN_SUB_AND_FETCH_SI);
8220   def_builtin ("__sync_or_and_fetch_si", si_ftype_psi_si,
8221                IA64_BUILTIN_OR_AND_FETCH_SI);
8222   def_builtin ("__sync_and_and_fetch_si", si_ftype_psi_si,
8223                IA64_BUILTIN_AND_AND_FETCH_SI);
8224   def_builtin ("__sync_xor_and_fetch_si", si_ftype_psi_si,
8225                IA64_BUILTIN_XOR_AND_FETCH_SI);
8226   def_builtin ("__sync_nand_and_fetch_si", si_ftype_psi_si,
8227                IA64_BUILTIN_NAND_AND_FETCH_SI);
8228
8229   def_builtin ("__sync_fetch_and_add_di", di_ftype_pdi_di,
8230                IA64_BUILTIN_FETCH_AND_ADD_DI);
8231   def_builtin ("__sync_fetch_and_sub_di", di_ftype_pdi_di,
8232                IA64_BUILTIN_FETCH_AND_SUB_DI);
8233   def_builtin ("__sync_fetch_and_or_di", di_ftype_pdi_di,
8234                IA64_BUILTIN_FETCH_AND_OR_DI);
8235   def_builtin ("__sync_fetch_and_and_di", di_ftype_pdi_di,
8236                IA64_BUILTIN_FETCH_AND_AND_DI);
8237   def_builtin ("__sync_fetch_and_xor_di", di_ftype_pdi_di,
8238                IA64_BUILTIN_FETCH_AND_XOR_DI);
8239   def_builtin ("__sync_fetch_and_nand_di", di_ftype_pdi_di,
8240                IA64_BUILTIN_FETCH_AND_NAND_DI);
8241
8242   def_builtin ("__sync_add_and_fetch_di", di_ftype_pdi_di,
8243                IA64_BUILTIN_ADD_AND_FETCH_DI);
8244   def_builtin ("__sync_sub_and_fetch_di", di_ftype_pdi_di,
8245                IA64_BUILTIN_SUB_AND_FETCH_DI);
8246   def_builtin ("__sync_or_and_fetch_di", di_ftype_pdi_di,
8247                IA64_BUILTIN_OR_AND_FETCH_DI);
8248   def_builtin ("__sync_and_and_fetch_di", di_ftype_pdi_di,
8249                IA64_BUILTIN_AND_AND_FETCH_DI);
8250   def_builtin ("__sync_xor_and_fetch_di", di_ftype_pdi_di,
8251                IA64_BUILTIN_XOR_AND_FETCH_DI);
8252   def_builtin ("__sync_nand_and_fetch_di", di_ftype_pdi_di,
8253                IA64_BUILTIN_NAND_AND_FETCH_DI);
8254
8255 #undef def_builtin
8256 }
8257
8258 /* Expand fetch_and_op intrinsics.  The basic code sequence is:
8259
8260      mf
8261      tmp = [ptr];
8262      do {
8263        ret = tmp;
8264        ar.ccv = tmp;
8265        tmp <op>= value;
8266        cmpxchgsz.acq tmp = [ptr], tmp
8267      } while (tmp != ret)
8268 */
8269
8270 static rtx
8271 ia64_expand_fetch_and_op (optab binoptab, enum machine_mode mode,
8272                           tree arglist, rtx target)
8273 {
8274   rtx ret, label, tmp, ccv, insn, mem, value;
8275   tree arg0, arg1;
8276
8277   arg0 = TREE_VALUE (arglist);
8278   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8279   mem = expand_expr (arg0, NULL_RTX, Pmode, 0);
8280 #ifdef POINTERS_EXTEND_UNSIGNED
8281   if (GET_MODE(mem) != Pmode)
8282     mem = convert_memory_address (Pmode, mem);
8283 #endif
8284   value = expand_expr (arg1, NULL_RTX, mode, 0);
8285
8286   mem = gen_rtx_MEM (mode, force_reg (Pmode, mem));
8287   MEM_VOLATILE_P (mem) = 1;
8288
8289   if (target && register_operand (target, mode))
8290     ret = target;
8291   else
8292     ret = gen_reg_rtx (mode);
8293
8294   emit_insn (gen_mf ());
8295
8296   /* Special case for fetchadd instructions.  */
8297   if (binoptab == add_optab && fetchadd_operand (value, VOIDmode))
8298     {
8299       if (mode == SImode)
8300         insn = gen_fetchadd_acq_si (ret, mem, value);
8301       else
8302         insn = gen_fetchadd_acq_di (ret, mem, value);
8303       emit_insn (insn);
8304       return ret;
8305     }
8306
8307   tmp = gen_reg_rtx (mode);
8308   /* ar.ccv must always be loaded with a zero-extended DImode value.  */
8309   ccv = gen_rtx_REG (DImode, AR_CCV_REGNUM);
8310   emit_move_insn (tmp, mem);
8311
8312   label = gen_label_rtx ();
8313   emit_label (label);
8314   emit_move_insn (ret, tmp);
8315   convert_move (ccv, tmp, /*unsignedp=*/1);
8316
8317   /* Perform the specific operation.  Special case NAND by noticing
8318      one_cmpl_optab instead.  */
8319   if (binoptab == one_cmpl_optab)
8320     {
8321       tmp = expand_unop (mode, binoptab, tmp, NULL, OPTAB_WIDEN);
8322       binoptab = and_optab;
8323     }
8324   tmp = expand_binop (mode, binoptab, tmp, value, tmp, 1, OPTAB_WIDEN);
8325
8326   if (mode == SImode)
8327     insn = gen_cmpxchg_acq_si (tmp, mem, tmp, ccv);
8328   else
8329     insn = gen_cmpxchg_acq_di (tmp, mem, tmp, ccv);
8330   emit_insn (insn);
8331
8332   emit_cmp_and_jump_insns (tmp, ret, NE, 0, mode, 1, label);
8333
8334   return ret;
8335 }
8336
8337 /* Expand op_and_fetch intrinsics.  The basic code sequence is:
8338
8339      mf
8340      tmp = [ptr];
8341      do {
8342        old = tmp;
8343        ar.ccv = tmp;
8344        ret = tmp <op> value;
8345        cmpxchgsz.acq tmp = [ptr], ret
8346      } while (tmp != old)
8347 */
8348
8349 static rtx
8350 ia64_expand_op_and_fetch (optab binoptab, enum machine_mode mode,
8351                           tree arglist, rtx target)
8352 {
8353   rtx old, label, tmp, ret, ccv, insn, mem, value;
8354   tree arg0, arg1;
8355
8356   arg0 = TREE_VALUE (arglist);
8357   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8358   mem = expand_expr (arg0, NULL_RTX, Pmode, 0);
8359 #ifdef POINTERS_EXTEND_UNSIGNED
8360   if (GET_MODE(mem) != Pmode)
8361     mem = convert_memory_address (Pmode, mem);
8362 #endif
8363
8364   value = expand_expr (arg1, NULL_RTX, mode, 0);
8365
8366   mem = gen_rtx_MEM (mode, force_reg (Pmode, mem));
8367   MEM_VOLATILE_P (mem) = 1;
8368
8369   if (target && ! register_operand (target, mode))
8370     target = NULL_RTX;
8371
8372   emit_insn (gen_mf ());
8373   tmp = gen_reg_rtx (mode);
8374   old = gen_reg_rtx (mode);
8375   /* ar.ccv must always be loaded with a zero-extended DImode value.  */
8376   ccv = gen_rtx_REG (DImode, AR_CCV_REGNUM);
8377
8378   emit_move_insn (tmp, mem);
8379
8380   label = gen_label_rtx ();
8381   emit_label (label);
8382   emit_move_insn (old, tmp);
8383   convert_move (ccv, tmp, /*unsignedp=*/1);
8384
8385   /* Perform the specific operation.  Special case NAND by noticing
8386      one_cmpl_optab instead.  */
8387   if (binoptab == one_cmpl_optab)
8388     {
8389       tmp = expand_unop (mode, binoptab, tmp, NULL, OPTAB_WIDEN);
8390       binoptab = and_optab;
8391     }
8392   ret = expand_binop (mode, binoptab, tmp, value, target, 1, OPTAB_WIDEN);
8393
8394   if (mode == SImode)
8395     insn = gen_cmpxchg_acq_si (tmp, mem, ret, ccv);
8396   else
8397     insn = gen_cmpxchg_acq_di (tmp, mem, ret, ccv);
8398   emit_insn (insn);
8399
8400   emit_cmp_and_jump_insns (tmp, old, NE, 0, mode, 1, label);
8401
8402   return ret;
8403 }
8404
8405 /* Expand val_ and bool_compare_and_swap.  For val_ we want:
8406
8407      ar.ccv = oldval
8408      mf
8409      cmpxchgsz.acq ret = [ptr], newval, ar.ccv
8410      return ret
8411
8412    For bool_ it's the same except return ret == oldval.
8413 */
8414
8415 static rtx
8416 ia64_expand_compare_and_swap (enum machine_mode rmode, enum machine_mode mode,
8417                               int boolp, tree arglist, rtx target)
8418 {
8419   tree arg0, arg1, arg2;
8420   rtx mem, old, new, ccv, tmp, insn;
8421
8422   arg0 = TREE_VALUE (arglist);
8423   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8424   arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8425   mem = expand_expr (arg0, NULL_RTX, ptr_mode, 0);
8426   old = expand_expr (arg1, NULL_RTX, mode, 0);
8427   new = expand_expr (arg2, NULL_RTX, mode, 0);
8428
8429   mem = gen_rtx_MEM (mode, force_reg (ptr_mode, mem));
8430   MEM_VOLATILE_P (mem) = 1;
8431
8432   if (GET_MODE (old) != mode)
8433     old = convert_to_mode (mode, old, /*unsignedp=*/1);
8434   if (GET_MODE (new) != mode)
8435     new = convert_to_mode (mode, new, /*unsignedp=*/1);
8436
8437   if (! register_operand (old, mode))
8438     old = copy_to_mode_reg (mode, old);
8439   if (! register_operand (new, mode))
8440     new = copy_to_mode_reg (mode, new);
8441
8442   if (! boolp && target && register_operand (target, mode))
8443     tmp = target;
8444   else
8445     tmp = gen_reg_rtx (mode);
8446
8447   ccv = gen_rtx_REG (DImode, AR_CCV_REGNUM);
8448   convert_move (ccv, old, /*unsignedp=*/1);
8449   emit_insn (gen_mf ());
8450   if (mode == SImode)
8451     insn = gen_cmpxchg_acq_si (tmp, mem, new, ccv);
8452   else
8453     insn = gen_cmpxchg_acq_di (tmp, mem, new, ccv);
8454   emit_insn (insn);
8455
8456   if (boolp)
8457     {
8458       if (! target)
8459         target = gen_reg_rtx (rmode);
8460       return emit_store_flag_force (target, EQ, tmp, old, mode, 1, 1);
8461     }
8462   else
8463     return tmp;
8464 }
8465
8466 /* Expand lock_test_and_set.  I.e. `xchgsz ret = [ptr], new'.  */
8467
8468 static rtx
8469 ia64_expand_lock_test_and_set (enum machine_mode mode, tree arglist,
8470                                rtx target)
8471 {
8472   tree arg0, arg1;
8473   rtx mem, new, ret, insn;
8474
8475   arg0 = TREE_VALUE (arglist);
8476   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8477   mem = expand_expr (arg0, NULL_RTX, ptr_mode, 0);
8478   new = expand_expr (arg1, NULL_RTX, mode, 0);
8479
8480   mem = gen_rtx_MEM (mode, force_reg (ptr_mode, mem));
8481   MEM_VOLATILE_P (mem) = 1;
8482   if (! register_operand (new, mode))
8483     new = copy_to_mode_reg (mode, new);
8484
8485   if (target && register_operand (target, mode))
8486     ret = target;
8487   else
8488     ret = gen_reg_rtx (mode);
8489
8490   if (mode == SImode)
8491     insn = gen_xchgsi (ret, mem, new);
8492   else
8493     insn = gen_xchgdi (ret, mem, new);
8494   emit_insn (insn);
8495
8496   return ret;
8497 }
8498
8499 /* Expand lock_release.  I.e. `stsz.rel [ptr] = r0'.  */
8500
8501 static rtx
8502 ia64_expand_lock_release (enum machine_mode mode, tree arglist,
8503                           rtx target ATTRIBUTE_UNUSED)
8504 {
8505   tree arg0;
8506   rtx mem;
8507
8508   arg0 = TREE_VALUE (arglist);
8509   mem = expand_expr (arg0, NULL_RTX, ptr_mode, 0);
8510
8511   mem = gen_rtx_MEM (mode, force_reg (ptr_mode, mem));
8512   MEM_VOLATILE_P (mem) = 1;
8513
8514   emit_move_insn (mem, const0_rtx);
8515
8516   return const0_rtx;
8517 }
8518
8519 rtx
8520 ia64_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
8521                      enum machine_mode mode ATTRIBUTE_UNUSED,
8522                      int ignore ATTRIBUTE_UNUSED)
8523 {
8524   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
8525   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
8526   tree arglist = TREE_OPERAND (exp, 1);
8527   enum machine_mode rmode = VOIDmode;
8528
8529   switch (fcode)
8530     {
8531     case IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_SI:
8532     case IA64_BUILTIN_VAL_COMPARE_AND_SWAP_SI:
8533       mode = SImode;
8534       rmode = SImode;
8535       break;
8536
8537     case IA64_BUILTIN_LOCK_TEST_AND_SET_SI:
8538     case IA64_BUILTIN_LOCK_RELEASE_SI:
8539     case IA64_BUILTIN_FETCH_AND_ADD_SI:
8540     case IA64_BUILTIN_FETCH_AND_SUB_SI:
8541     case IA64_BUILTIN_FETCH_AND_OR_SI:
8542     case IA64_BUILTIN_FETCH_AND_AND_SI:
8543     case IA64_BUILTIN_FETCH_AND_XOR_SI:
8544     case IA64_BUILTIN_FETCH_AND_NAND_SI:
8545     case IA64_BUILTIN_ADD_AND_FETCH_SI:
8546     case IA64_BUILTIN_SUB_AND_FETCH_SI:
8547     case IA64_BUILTIN_OR_AND_FETCH_SI:
8548     case IA64_BUILTIN_AND_AND_FETCH_SI:
8549     case IA64_BUILTIN_XOR_AND_FETCH_SI:
8550     case IA64_BUILTIN_NAND_AND_FETCH_SI:
8551       mode = SImode;
8552       break;
8553
8554     case IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_DI:
8555       mode = DImode;
8556       rmode = SImode;
8557       break;
8558
8559     case IA64_BUILTIN_VAL_COMPARE_AND_SWAP_DI:
8560       mode = DImode;
8561       rmode = DImode;
8562       break;
8563
8564     case IA64_BUILTIN_LOCK_TEST_AND_SET_DI:
8565     case IA64_BUILTIN_LOCK_RELEASE_DI:
8566     case IA64_BUILTIN_FETCH_AND_ADD_DI:
8567     case IA64_BUILTIN_FETCH_AND_SUB_DI:
8568     case IA64_BUILTIN_FETCH_AND_OR_DI:
8569     case IA64_BUILTIN_FETCH_AND_AND_DI:
8570     case IA64_BUILTIN_FETCH_AND_XOR_DI:
8571     case IA64_BUILTIN_FETCH_AND_NAND_DI:
8572     case IA64_BUILTIN_ADD_AND_FETCH_DI:
8573     case IA64_BUILTIN_SUB_AND_FETCH_DI:
8574     case IA64_BUILTIN_OR_AND_FETCH_DI:
8575     case IA64_BUILTIN_AND_AND_FETCH_DI:
8576     case IA64_BUILTIN_XOR_AND_FETCH_DI:
8577     case IA64_BUILTIN_NAND_AND_FETCH_DI:
8578       mode = DImode;
8579       break;
8580
8581     default:
8582       break;
8583     }
8584
8585   switch (fcode)
8586     {
8587     case IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_SI:
8588     case IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_DI:
8589       return ia64_expand_compare_and_swap (rmode, mode, 1, arglist,
8590                                            target);
8591
8592     case IA64_BUILTIN_VAL_COMPARE_AND_SWAP_SI:
8593     case IA64_BUILTIN_VAL_COMPARE_AND_SWAP_DI:
8594       return ia64_expand_compare_and_swap (rmode, mode, 0, arglist,
8595                                            target);
8596
8597     case IA64_BUILTIN_SYNCHRONIZE:
8598       emit_insn (gen_mf ());
8599       return const0_rtx;
8600
8601     case IA64_BUILTIN_LOCK_TEST_AND_SET_SI:
8602     case IA64_BUILTIN_LOCK_TEST_AND_SET_DI:
8603       return ia64_expand_lock_test_and_set (mode, arglist, target);
8604
8605     case IA64_BUILTIN_LOCK_RELEASE_SI:
8606     case IA64_BUILTIN_LOCK_RELEASE_DI:
8607       return ia64_expand_lock_release (mode, arglist, target);
8608
8609     case IA64_BUILTIN_BSP:
8610       if (! target || ! register_operand (target, DImode))
8611         target = gen_reg_rtx (DImode);
8612       emit_insn (gen_bsp_value (target));
8613 #ifdef POINTERS_EXTEND_UNSIGNED
8614       target = convert_memory_address (ptr_mode, target);
8615 #endif
8616       return target;
8617
8618     case IA64_BUILTIN_FLUSHRS:
8619       emit_insn (gen_flushrs ());
8620       return const0_rtx;
8621
8622     case IA64_BUILTIN_FETCH_AND_ADD_SI:
8623     case IA64_BUILTIN_FETCH_AND_ADD_DI:
8624       return ia64_expand_fetch_and_op (add_optab, mode, arglist, target);
8625
8626     case IA64_BUILTIN_FETCH_AND_SUB_SI:
8627     case IA64_BUILTIN_FETCH_AND_SUB_DI:
8628       return ia64_expand_fetch_and_op (sub_optab, mode, arglist, target);
8629
8630     case IA64_BUILTIN_FETCH_AND_OR_SI:
8631     case IA64_BUILTIN_FETCH_AND_OR_DI:
8632       return ia64_expand_fetch_and_op (ior_optab, mode, arglist, target);
8633
8634     case IA64_BUILTIN_FETCH_AND_AND_SI:
8635     case IA64_BUILTIN_FETCH_AND_AND_DI:
8636       return ia64_expand_fetch_and_op (and_optab, mode, arglist, target);
8637
8638     case IA64_BUILTIN_FETCH_AND_XOR_SI:
8639     case IA64_BUILTIN_FETCH_AND_XOR_DI:
8640       return ia64_expand_fetch_and_op (xor_optab, mode, arglist, target);
8641
8642     case IA64_BUILTIN_FETCH_AND_NAND_SI:
8643     case IA64_BUILTIN_FETCH_AND_NAND_DI:
8644       return ia64_expand_fetch_and_op (one_cmpl_optab, mode, arglist, target);
8645
8646     case IA64_BUILTIN_ADD_AND_FETCH_SI:
8647     case IA64_BUILTIN_ADD_AND_FETCH_DI:
8648       return ia64_expand_op_and_fetch (add_optab, mode, arglist, target);
8649
8650     case IA64_BUILTIN_SUB_AND_FETCH_SI:
8651     case IA64_BUILTIN_SUB_AND_FETCH_DI:
8652       return ia64_expand_op_and_fetch (sub_optab, mode, arglist, target);
8653
8654     case IA64_BUILTIN_OR_AND_FETCH_SI:
8655     case IA64_BUILTIN_OR_AND_FETCH_DI:
8656       return ia64_expand_op_and_fetch (ior_optab, mode, arglist, target);
8657
8658     case IA64_BUILTIN_AND_AND_FETCH_SI:
8659     case IA64_BUILTIN_AND_AND_FETCH_DI:
8660       return ia64_expand_op_and_fetch (and_optab, mode, arglist, target);
8661
8662     case IA64_BUILTIN_XOR_AND_FETCH_SI:
8663     case IA64_BUILTIN_XOR_AND_FETCH_DI:
8664       return ia64_expand_op_and_fetch (xor_optab, mode, arglist, target);
8665
8666     case IA64_BUILTIN_NAND_AND_FETCH_SI:
8667     case IA64_BUILTIN_NAND_AND_FETCH_DI:
8668       return ia64_expand_op_and_fetch (one_cmpl_optab, mode, arglist, target);
8669
8670     default:
8671       break;
8672     }
8673
8674   return NULL_RTX;
8675 }
8676
8677 /* For the HP-UX IA64 aggregate parameters are passed stored in the
8678    most significant bits of the stack slot.  */
8679
8680 enum direction
8681 ia64_hpux_function_arg_padding (enum machine_mode mode, tree type)
8682 {
8683    /* Exception to normal case for structures/unions/etc.  */
8684
8685    if (type && AGGREGATE_TYPE_P (type)
8686        && int_size_in_bytes (type) < UNITS_PER_WORD)
8687      return upward;
8688
8689    /* Fall back to the default.  */
8690    return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
8691 }
8692
8693 /* Linked list of all external functions that are to be emitted by GCC.
8694    We output the name if and only if TREE_SYMBOL_REFERENCED is set in
8695    order to avoid putting out names that are never really used.  */
8696
8697 struct extern_func_list GTY(())
8698 {
8699   struct extern_func_list *next;
8700   tree decl;
8701 };
8702
8703 static GTY(()) struct extern_func_list *extern_func_head;
8704
8705 static void
8706 ia64_hpux_add_extern_decl (tree decl)
8707 {
8708   struct extern_func_list *p = ggc_alloc (sizeof (struct extern_func_list));
8709
8710   p->decl = decl;
8711   p->next = extern_func_head;
8712   extern_func_head = p;
8713 }
8714
8715 /* Print out the list of used global functions.  */
8716
8717 static void
8718 ia64_hpux_file_end (void)
8719 {
8720   struct extern_func_list *p;
8721
8722   for (p = extern_func_head; p; p = p->next)
8723     {
8724       tree decl = p->decl;
8725       tree id = DECL_ASSEMBLER_NAME (decl);
8726
8727       if (!id)
8728         abort ();
8729
8730       if (!TREE_ASM_WRITTEN (decl) && TREE_SYMBOL_REFERENCED (id))
8731         {
8732           const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
8733
8734           TREE_ASM_WRITTEN (decl) = 1;
8735           (*targetm.asm_out.globalize_label) (asm_out_file, name);
8736           fputs (TYPE_ASM_OP, asm_out_file);
8737           assemble_name (asm_out_file, name);
8738           fprintf (asm_out_file, "," TYPE_OPERAND_FMT "\n", "function");
8739         }
8740     }
8741
8742   extern_func_head = 0;
8743 }
8744
8745 /* Set SImode div/mod functions, init_integral_libfuncs only initializes
8746    modes of word_mode and larger.  Rename the TFmode libfuncs using the
8747    HPUX conventions. __divtf3 is used for XFmode. We need to keep it for
8748    backward compatibility. */
8749
8750 static void
8751 ia64_init_libfuncs (void)
8752 {
8753   set_optab_libfunc (sdiv_optab, SImode, "__divsi3");
8754   set_optab_libfunc (udiv_optab, SImode, "__udivsi3");
8755   set_optab_libfunc (smod_optab, SImode, "__modsi3");
8756   set_optab_libfunc (umod_optab, SImode, "__umodsi3");
8757
8758   set_optab_libfunc (add_optab, TFmode, "_U_Qfadd");
8759   set_optab_libfunc (sub_optab, TFmode, "_U_Qfsub");
8760   set_optab_libfunc (smul_optab, TFmode, "_U_Qfmpy");
8761   set_optab_libfunc (sdiv_optab, TFmode, "_U_Qfdiv");
8762   set_optab_libfunc (neg_optab, TFmode, "_U_Qfneg");
8763
8764   set_conv_libfunc (sext_optab, TFmode, SFmode, "_U_Qfcnvff_sgl_to_quad");
8765   set_conv_libfunc (sext_optab, TFmode, DFmode, "_U_Qfcnvff_dbl_to_quad");
8766   set_conv_libfunc (sext_optab, TFmode, XFmode, "_U_Qfcnvff_f80_to_quad");
8767   set_conv_libfunc (trunc_optab, SFmode, TFmode, "_U_Qfcnvff_quad_to_sgl");
8768   set_conv_libfunc (trunc_optab, DFmode, TFmode, "_U_Qfcnvff_quad_to_dbl");
8769   set_conv_libfunc (trunc_optab, XFmode, TFmode, "_U_Qfcnvff_quad_to_f80");
8770
8771   set_conv_libfunc (sfix_optab, SImode, TFmode, "_U_Qfcnvfxt_quad_to_sgl");
8772   set_conv_libfunc (sfix_optab, DImode, TFmode, "_U_Qfcnvfxt_quad_to_dbl");
8773   set_conv_libfunc (ufix_optab, SImode, TFmode, "_U_Qfcnvfxut_quad_to_sgl");
8774   set_conv_libfunc (ufix_optab, DImode, TFmode, "_U_Qfcnvfxut_quad_to_dbl");
8775
8776   set_conv_libfunc (sfloat_optab, TFmode, SImode, "_U_Qfcnvxf_sgl_to_quad");
8777   set_conv_libfunc (sfloat_optab, TFmode, DImode, "_U_Qfcnvxf_dbl_to_quad");
8778 }
8779
8780 /* Rename all the TFmode libfuncs using the HPUX conventions.  */
8781
8782 static void
8783 ia64_hpux_init_libfuncs (void)
8784 {
8785   ia64_init_libfuncs ();
8786
8787   set_optab_libfunc (smin_optab, TFmode, "_U_Qfmin");
8788   set_optab_libfunc (smax_optab, TFmode, "_U_Qfmax");
8789   set_optab_libfunc (abs_optab, TFmode, "_U_Qfabs");
8790
8791   /* ia64_expand_compare uses this.  */
8792   cmptf_libfunc = init_one_libfunc ("_U_Qfcmp");
8793
8794   /* These should never be used.  */
8795   set_optab_libfunc (eq_optab, TFmode, 0);
8796   set_optab_libfunc (ne_optab, TFmode, 0);
8797   set_optab_libfunc (gt_optab, TFmode, 0);
8798   set_optab_libfunc (ge_optab, TFmode, 0);
8799   set_optab_libfunc (lt_optab, TFmode, 0);
8800   set_optab_libfunc (le_optab, TFmode, 0);
8801 }
8802
8803 /* Rename the division and modulus functions in VMS.  */
8804
8805 static void
8806 ia64_vms_init_libfuncs (void)
8807 {
8808   set_optab_libfunc (sdiv_optab, SImode, "OTS$DIV_I");
8809   set_optab_libfunc (sdiv_optab, DImode, "OTS$DIV_L");
8810   set_optab_libfunc (udiv_optab, SImode, "OTS$DIV_UI");
8811   set_optab_libfunc (udiv_optab, DImode, "OTS$DIV_UL");
8812   set_optab_libfunc (smod_optab, SImode, "OTS$REM_I");
8813   set_optab_libfunc (smod_optab, DImode, "OTS$REM_L");
8814   set_optab_libfunc (umod_optab, SImode, "OTS$REM_UI");
8815   set_optab_libfunc (umod_optab, DImode, "OTS$REM_UL");
8816 }
8817
8818 /* Rename the TFmode libfuncs available from soft-fp in glibc using
8819    the HPUX conventions.  */
8820
8821 static void
8822 ia64_sysv4_init_libfuncs (void)
8823 {
8824   ia64_init_libfuncs ();
8825
8826   /* These functions are not part of the HPUX TFmode interface.  We
8827      use them instead of _U_Qfcmp, which doesn't work the way we
8828      expect.  */
8829   set_optab_libfunc (eq_optab, TFmode, "_U_Qfeq");
8830   set_optab_libfunc (ne_optab, TFmode, "_U_Qfne");
8831   set_optab_libfunc (gt_optab, TFmode, "_U_Qfgt");
8832   set_optab_libfunc (ge_optab, TFmode, "_U_Qfge");
8833   set_optab_libfunc (lt_optab, TFmode, "_U_Qflt");
8834   set_optab_libfunc (le_optab, TFmode, "_U_Qfle");
8835
8836   /* We leave out _U_Qfmin, _U_Qfmax and _U_Qfabs since soft-fp in
8837      glibc doesn't have them.  */
8838 }
8839 \f
8840 /* Switch to the section to which we should output X.  The only thing
8841    special we do here is to honor small data.  */
8842
8843 static void
8844 ia64_select_rtx_section (enum machine_mode mode, rtx x,
8845                          unsigned HOST_WIDE_INT align)
8846 {
8847   if (GET_MODE_SIZE (mode) > 0
8848       && GET_MODE_SIZE (mode) <= ia64_section_threshold)
8849     sdata_section ();
8850   else
8851     default_elf_select_rtx_section (mode, x, align);
8852 }
8853
8854 /* It is illegal to have relocations in shared segments on AIX and HPUX.
8855    Pretend flag_pic is always set.  */
8856
8857 static void
8858 ia64_rwreloc_select_section (tree exp, int reloc, unsigned HOST_WIDE_INT align)
8859 {
8860   default_elf_select_section_1 (exp, reloc, align, true);
8861 }
8862
8863 static void
8864 ia64_rwreloc_unique_section (tree decl, int reloc)
8865 {
8866   default_unique_section_1 (decl, reloc, true);
8867 }
8868
8869 static void
8870 ia64_rwreloc_select_rtx_section (enum machine_mode mode, rtx x,
8871                                  unsigned HOST_WIDE_INT align)
8872 {
8873   int save_pic = flag_pic;
8874   flag_pic = 1;
8875   ia64_select_rtx_section (mode, x, align);
8876   flag_pic = save_pic;
8877 }
8878
8879 static unsigned int
8880 ia64_rwreloc_section_type_flags (tree decl, const char *name, int reloc)
8881 {
8882   return default_section_type_flags_1 (decl, name, reloc, true);
8883 }
8884
8885 /* Returns true if FNTYPE (a FUNCTION_TYPE or a METHOD_TYPE) returns a
8886    structure type and that the address of that type should be passed
8887    in out0, rather than in r8.  */
8888
8889 static bool
8890 ia64_struct_retval_addr_is_first_parm_p (tree fntype)
8891 {
8892   tree ret_type = TREE_TYPE (fntype);
8893
8894   /* The Itanium C++ ABI requires that out0, rather than r8, be used
8895      as the structure return address parameter, if the return value
8896      type has a non-trivial copy constructor or destructor.  It is not
8897      clear if this same convention should be used for other
8898      programming languages.  Until G++ 3.4, we incorrectly used r8 for
8899      these return values.  */
8900   return (abi_version_at_least (2)
8901           && ret_type
8902           && TYPE_MODE (ret_type) == BLKmode 
8903           && TREE_ADDRESSABLE (ret_type)
8904           && strcmp (lang_hooks.name, "GNU C++") == 0);
8905 }
8906
8907 /* Output the assembler code for a thunk function.  THUNK_DECL is the
8908    declaration for the thunk function itself, FUNCTION is the decl for
8909    the target function.  DELTA is an immediate constant offset to be
8910    added to THIS.  If VCALL_OFFSET is nonzero, the word at
8911    *(*this + vcall_offset) should be added to THIS.  */
8912
8913 static void
8914 ia64_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
8915                       HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
8916                       tree function)
8917 {
8918   rtx this, insn, funexp;
8919   unsigned int this_parmno;
8920   unsigned int this_regno;
8921
8922   reload_completed = 1;
8923   epilogue_completed = 1;
8924   no_new_pseudos = 1;
8925   reset_block_changes ();
8926
8927   /* Set things up as ia64_expand_prologue might.  */
8928   last_scratch_gr_reg = 15;
8929
8930   memset (&current_frame_info, 0, sizeof (current_frame_info));
8931   current_frame_info.spill_cfa_off = -16;
8932   current_frame_info.n_input_regs = 1;
8933   current_frame_info.need_regstk = (TARGET_REG_NAMES != 0);
8934
8935   /* Mark the end of the (empty) prologue.  */
8936   emit_note (NOTE_INSN_PROLOGUE_END);
8937
8938   /* Figure out whether "this" will be the first parameter (the
8939      typical case) or the second parameter (as happens when the
8940      virtual function returns certain class objects).  */
8941   this_parmno
8942     = (ia64_struct_retval_addr_is_first_parm_p (TREE_TYPE (thunk))
8943        ? 1 : 0);
8944   this_regno = IN_REG (this_parmno);
8945   if (!TARGET_REG_NAMES)
8946     reg_names[this_regno] = ia64_reg_numbers[this_parmno];
8947
8948   this = gen_rtx_REG (Pmode, this_regno);
8949   if (TARGET_ILP32)
8950     {
8951       rtx tmp = gen_rtx_REG (ptr_mode, this_regno);
8952       REG_POINTER (tmp) = 1;
8953       if (delta && CONST_OK_FOR_I (delta))
8954         {
8955           emit_insn (gen_ptr_extend_plus_imm (this, tmp, GEN_INT (delta)));
8956           delta = 0;
8957         }
8958       else
8959         emit_insn (gen_ptr_extend (this, tmp));
8960     }
8961
8962   /* Apply the constant offset, if required.  */
8963   if (delta)
8964     {
8965       rtx delta_rtx = GEN_INT (delta);
8966
8967       if (!CONST_OK_FOR_I (delta))
8968         {
8969           rtx tmp = gen_rtx_REG (Pmode, 2);
8970           emit_move_insn (tmp, delta_rtx);
8971           delta_rtx = tmp;
8972         }
8973       emit_insn (gen_adddi3 (this, this, delta_rtx));
8974     }
8975
8976   /* Apply the offset from the vtable, if required.  */
8977   if (vcall_offset)
8978     {
8979       rtx vcall_offset_rtx = GEN_INT (vcall_offset);
8980       rtx tmp = gen_rtx_REG (Pmode, 2);
8981
8982       if (TARGET_ILP32)
8983         {
8984           rtx t = gen_rtx_REG (ptr_mode, 2);
8985           REG_POINTER (t) = 1;
8986           emit_move_insn (t, gen_rtx_MEM (ptr_mode, this));
8987           if (CONST_OK_FOR_I (vcall_offset))
8988             {
8989               emit_insn (gen_ptr_extend_plus_imm (tmp, t, 
8990                                                   vcall_offset_rtx));
8991               vcall_offset = 0;
8992             }
8993           else
8994             emit_insn (gen_ptr_extend (tmp, t));
8995         }
8996       else
8997         emit_move_insn (tmp, gen_rtx_MEM (Pmode, this));
8998
8999       if (vcall_offset)
9000         {
9001           if (!CONST_OK_FOR_J (vcall_offset))
9002             {
9003               rtx tmp2 = gen_rtx_REG (Pmode, next_scratch_gr_reg ());
9004               emit_move_insn (tmp2, vcall_offset_rtx);
9005               vcall_offset_rtx = tmp2;
9006             }
9007           emit_insn (gen_adddi3 (tmp, tmp, vcall_offset_rtx));
9008         }
9009
9010       if (TARGET_ILP32)
9011         emit_move_insn (gen_rtx_REG (ptr_mode, 2), 
9012                         gen_rtx_MEM (ptr_mode, tmp));
9013       else
9014         emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
9015
9016       emit_insn (gen_adddi3 (this, this, tmp));
9017     }
9018
9019   /* Generate a tail call to the target function.  */
9020   if (! TREE_USED (function))
9021     {
9022       assemble_external (function);
9023       TREE_USED (function) = 1;
9024     }
9025   funexp = XEXP (DECL_RTL (function), 0);
9026   funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
9027   ia64_expand_call (NULL_RTX, funexp, NULL_RTX, 1);
9028   insn = get_last_insn ();
9029   SIBLING_CALL_P (insn) = 1;
9030
9031   /* Code generation for calls relies on splitting.  */
9032   reload_completed = 1;
9033   epilogue_completed = 1;
9034   try_split (PATTERN (insn), insn, 0);
9035
9036   emit_barrier ();
9037
9038   /* Run just enough of rest_of_compilation to get the insns emitted.
9039      There's not really enough bulk here to make other passes such as
9040      instruction scheduling worth while.  Note that use_thunk calls
9041      assemble_start_function and assemble_end_function.  */
9042
9043   insn_locators_initialize ();
9044   emit_all_insn_group_barriers (NULL);
9045   insn = get_insns ();
9046   shorten_branches (insn);
9047   final_start_function (insn, file, 1);
9048   final (insn, file, 1, 0);
9049   final_end_function ();
9050
9051   reload_completed = 0;
9052   epilogue_completed = 0;
9053   no_new_pseudos = 0;
9054 }
9055
9056 /* Worker function for TARGET_STRUCT_VALUE_RTX.  */
9057
9058 static rtx
9059 ia64_struct_value_rtx (tree fntype,
9060                        int incoming ATTRIBUTE_UNUSED)
9061 {
9062   if (fntype && ia64_struct_retval_addr_is_first_parm_p (fntype))
9063     return NULL_RTX;
9064   return gen_rtx_REG (Pmode, GR_REG (8));
9065 }
9066
9067 #include "gt-ia64.h"