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