OSDN Git Service

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