]> gcc.gnu.org Git - gcc.git/blame - gcc/config/ia64/ia64.c
Fix bfd/archive.c miscompilation.
[gcc.git] / gcc / config / ia64 / ia64.c
CommitLineData
c65ebc55 1/* Definitions of target machine for GNU compiler.
e65271be 2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
c65ebc55
JW
3 Contributed by James E. Wilson <wilson@cygnus.com> and
4 David Mosberger <davidm@hpl.hp.com>.
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
22
c65ebc55 23#include "config.h"
ed9ccd8a 24#include "system.h"
c65ebc55
JW
25#include "rtl.h"
26#include "tree.h"
27#include "tm_p.h"
28#include "regs.h"
29#include "hard-reg-set.h"
30#include "real.h"
31#include "insn-config.h"
32#include "conditions.h"
33#include "insn-flags.h"
34#include "output.h"
35#include "insn-attr.h"
36#include "flags.h"
37#include "recog.h"
38#include "expr.h"
39#include "obstack.h"
40#include "except.h"
41#include "function.h"
42#include "ggc.h"
43#include "basic-block.h"
809d4ef1 44#include "toplev.h"
c65ebc55
JW
45
46/* This is used for communication between ASM_OUTPUT_LABEL and
47 ASM_OUTPUT_LABELREF. */
48int ia64_asm_output_label = 0;
49
50/* Define the information needed to generate branch and scc insns. This is
51 stored from the compare operation. */
52struct rtx_def * ia64_compare_op0;
53struct rtx_def * ia64_compare_op1;
54
c65ebc55 55/* Register names for ia64_expand_prologue. */
3b572406 56static const char * const ia64_reg_numbers[96] =
c65ebc55
JW
57{ "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
58 "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
59 "r48", "r49", "r50", "r51", "r52", "r53", "r54", "r55",
60 "r56", "r57", "r58", "r59", "r60", "r61", "r62", "r63",
61 "r64", "r65", "r66", "r67", "r68", "r69", "r70", "r71",
62 "r72", "r73", "r74", "r75", "r76", "r77", "r78", "r79",
63 "r80", "r81", "r82", "r83", "r84", "r85", "r86", "r87",
64 "r88", "r89", "r90", "r91", "r92", "r93", "r94", "r95",
65 "r96", "r97", "r98", "r99", "r100","r101","r102","r103",
66 "r104","r105","r106","r107","r108","r109","r110","r111",
67 "r112","r113","r114","r115","r116","r117","r118","r119",
68 "r120","r121","r122","r123","r124","r125","r126","r127"};
69
70/* ??? These strings could be shared with REGISTER_NAMES. */
3b572406 71static const char * const ia64_input_reg_names[8] =
c65ebc55
JW
72{ "in0", "in1", "in2", "in3", "in4", "in5", "in6", "in7" };
73
74/* ??? These strings could be shared with REGISTER_NAMES. */
3b572406 75static const char * const ia64_local_reg_names[80] =
c65ebc55
JW
76{ "loc0", "loc1", "loc2", "loc3", "loc4", "loc5", "loc6", "loc7",
77 "loc8", "loc9", "loc10","loc11","loc12","loc13","loc14","loc15",
78 "loc16","loc17","loc18","loc19","loc20","loc21","loc22","loc23",
79 "loc24","loc25","loc26","loc27","loc28","loc29","loc30","loc31",
80 "loc32","loc33","loc34","loc35","loc36","loc37","loc38","loc39",
81 "loc40","loc41","loc42","loc43","loc44","loc45","loc46","loc47",
82 "loc48","loc49","loc50","loc51","loc52","loc53","loc54","loc55",
83 "loc56","loc57","loc58","loc59","loc60","loc61","loc62","loc63",
84 "loc64","loc65","loc66","loc67","loc68","loc69","loc70","loc71",
85 "loc72","loc73","loc74","loc75","loc76","loc77","loc78","loc79" };
86
87/* ??? These strings could be shared with REGISTER_NAMES. */
3b572406 88static const char * const ia64_output_reg_names[8] =
c65ebc55
JW
89{ "out0", "out1", "out2", "out3", "out4", "out5", "out6", "out7" };
90
91/* String used with the -mfixed-range= option. */
92const char *ia64_fixed_range_string;
93
94/* Variables which are this size or smaller are put in the sdata/sbss
95 sections. */
96
3b572406
RH
97unsigned int ia64_section_threshold;
98\f
97e242b0
RH
99static int find_gr_spill PARAMS ((int));
100static int next_scratch_gr_reg PARAMS ((void));
101static void mark_reg_gr_used_mask PARAMS ((rtx, void *));
102static void ia64_compute_frame_size PARAMS ((HOST_WIDE_INT));
103static void setup_spill_pointers PARAMS ((int, rtx, HOST_WIDE_INT));
104static void finish_spill_pointers PARAMS ((void));
105static rtx spill_restore_mem PARAMS ((rtx, HOST_WIDE_INT));
870f9ec0
RH
106static void do_spill PARAMS ((rtx (*)(rtx, rtx, rtx), rtx, HOST_WIDE_INT, rtx));
107static void do_restore PARAMS ((rtx (*)(rtx, rtx, rtx), rtx, HOST_WIDE_INT));
97e242b0 108
3b572406
RH
109static enum machine_mode hfa_element_mode PARAMS ((tree, int));
110static void fix_range PARAMS ((const char *));
111static void ia64_add_gc_roots PARAMS ((void));
112static void ia64_init_machine_status PARAMS ((struct function *));
113static void ia64_mark_machine_status PARAMS ((struct function *));
114static void emit_insn_group_barriers PARAMS ((rtx));
115static void emit_predicate_relation_info PARAMS ((rtx));
116static int process_set PARAMS ((FILE *, rtx));
117static rtx ia64_expand_compare_and_swap PARAMS ((enum insn_code, tree,
118 rtx, int));
119static rtx ia64_expand_binop_builtin PARAMS ((enum insn_code, tree, rtx));
120\f
c65ebc55
JW
121/* Return 1 if OP is a valid operand for the MEM of a CALL insn. */
122
123int
124call_operand (op, mode)
125 rtx op;
126 enum machine_mode mode;
127{
128 if (mode != GET_MODE (op))
129 return 0;
130
131 return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == REG
132 || (GET_CODE (op) == SUBREG && GET_CODE (XEXP (op, 0)) == REG));
133}
134
135/* Return 1 if OP refers to a symbol in the sdata section. */
136
137int
138sdata_symbolic_operand (op, mode)
139 rtx op;
fd7c34b0 140 enum machine_mode mode ATTRIBUTE_UNUSED;
c65ebc55
JW
141{
142 switch (GET_CODE (op))
143 {
ac9cd70f
RH
144 case CONST:
145 if (GET_CODE (XEXP (op, 0)) != PLUS
146 || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF)
147 break;
148 op = XEXP (XEXP (op, 0), 0);
149 /* FALLTHRU */
150
c65ebc55 151 case SYMBOL_REF:
ac9cd70f
RH
152 if (CONSTANT_POOL_ADDRESS_P (op))
153 return GET_MODE_SIZE (get_pool_mode (op)) <= ia64_section_threshold;
154 else
155 return XSTR (op, 0)[0] == SDATA_NAME_FLAG_CHAR;
c65ebc55 156
c65ebc55
JW
157 default:
158 break;
159 }
160
161 return 0;
162}
163
ec039e3c 164/* Return 1 if OP refers to a symbol, and is appropriate for a GOT load. */
c65ebc55
JW
165
166int
ec039e3c 167got_symbolic_operand (op, mode)
c65ebc55 168 rtx op;
fd7c34b0 169 enum machine_mode mode ATTRIBUTE_UNUSED;
c65ebc55
JW
170{
171 switch (GET_CODE (op))
172 {
173 case CONST:
dee4095a
RH
174 op = XEXP (op, 0);
175 if (GET_CODE (op) != PLUS)
176 return 0;
177 if (GET_CODE (XEXP (op, 0)) != SYMBOL_REF)
178 return 0;
179 op = XEXP (op, 1);
180 if (GET_CODE (op) != CONST_INT)
181 return 0;
ec039e3c
RH
182
183 return 1;
184
185 /* Ok if we're not using GOT entries at all. */
186 if (TARGET_NO_PIC || TARGET_AUTO_PIC)
187 return 1;
188
189 /* "Ok" while emitting rtl, since otherwise we won't be provided
190 with the entire offset during emission, which makes it very
191 hard to split the offset into high and low parts. */
192 if (rtx_equal_function_value_matters)
193 return 1;
194
195 /* Force the low 14 bits of the constant to zero so that we do not
dee4095a 196 use up so many GOT entries. */
ec039e3c
RH
197 return (INTVAL (op) & 0x3fff) == 0;
198
199 case SYMBOL_REF:
200 case LABEL_REF:
dee4095a
RH
201 return 1;
202
ec039e3c
RH
203 default:
204 break;
205 }
206 return 0;
207}
208
209/* Return 1 if OP refers to a symbol. */
210
211int
212symbolic_operand (op, mode)
213 rtx op;
214 enum machine_mode mode ATTRIBUTE_UNUSED;
215{
216 switch (GET_CODE (op))
217 {
218 case CONST:
c65ebc55
JW
219 case SYMBOL_REF:
220 case LABEL_REF:
221 return 1;
222
223 default:
224 break;
225 }
226 return 0;
227}
228
229/* Return 1 if OP refers to a function. */
230
231int
232function_operand (op, mode)
233 rtx op;
fd7c34b0 234 enum machine_mode mode ATTRIBUTE_UNUSED;
c65ebc55
JW
235{
236 if (GET_CODE (op) == SYMBOL_REF && SYMBOL_REF_FLAG (op))
237 return 1;
238 else
239 return 0;
240}
241
242/* Return 1 if OP is setjmp or a similar function. */
243
244/* ??? This is an unsatisfying solution. Should rethink. */
245
246int
247setjmp_operand (op, mode)
248 rtx op;
fd7c34b0 249 enum machine_mode mode ATTRIBUTE_UNUSED;
c65ebc55 250{
809d4ef1 251 const char *name;
c65ebc55
JW
252 int retval = 0;
253
254 if (GET_CODE (op) != SYMBOL_REF)
255 return 0;
256
257 name = XSTR (op, 0);
258
259 /* The following code is borrowed from special_function_p in calls.c. */
260
261 /* Disregard prefix _, __ or __x. */
262 if (name[0] == '_')
263 {
264 if (name[1] == '_' && name[2] == 'x')
265 name += 3;
266 else if (name[1] == '_')
267 name += 2;
268 else
269 name += 1;
270 }
271
272 if (name[0] == 's')
273 {
274 retval
275 = ((name[1] == 'e'
276 && (! strcmp (name, "setjmp")
277 || ! strcmp (name, "setjmp_syscall")))
278 || (name[1] == 'i'
279 && ! strcmp (name, "sigsetjmp"))
280 || (name[1] == 'a'
281 && ! strcmp (name, "savectx")));
282 }
283 else if ((name[0] == 'q' && name[1] == 's'
284 && ! strcmp (name, "qsetjmp"))
285 || (name[0] == 'v' && name[1] == 'f'
286 && ! strcmp (name, "vfork")))
287 retval = 1;
288
289 return retval;
290}
291
292/* Return 1 if OP is a general operand, but when pic exclude symbolic
293 operands. */
294
295/* ??? If we drop no-pic support, can delete SYMBOL_REF, CONST, and LABEL_REF
296 from PREDICATE_CODES. */
297
298int
299move_operand (op, mode)
300 rtx op;
301 enum machine_mode mode;
302{
ec039e3c 303 if (! TARGET_NO_PIC && symbolic_operand (op, mode))
c65ebc55
JW
304 return 0;
305
306 return general_operand (op, mode);
307}
308
309/* Return 1 if OP is a register operand, or zero. */
310
311int
312reg_or_0_operand (op, mode)
313 rtx op;
314 enum machine_mode mode;
315{
316 return (op == const0_rtx || register_operand (op, mode));
317}
318
041f25e6
RH
319/* Return 1 if OP is a register operand, or a 5 bit immediate operand. */
320
321int
322reg_or_5bit_operand (op, mode)
323 rtx op;
324 enum machine_mode mode;
325{
326 return ((GET_CODE (op) == CONST_INT && INTVAL (op) >= 0 && INTVAL (op) < 32)
327 || GET_CODE (op) == CONSTANT_P_RTX
328 || register_operand (op, mode));
329}
330
c65ebc55
JW
331/* Return 1 if OP is a register operand, or a 6 bit immediate operand. */
332
333int
334reg_or_6bit_operand (op, mode)
335 rtx op;
336 enum machine_mode mode;
337{
338 return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_M (INTVAL (op)))
339 || GET_CODE (op) == CONSTANT_P_RTX
340 || register_operand (op, mode));
341}
342
343/* Return 1 if OP is a register operand, or an 8 bit immediate operand. */
344
345int
346reg_or_8bit_operand (op, mode)
347 rtx op;
348 enum machine_mode mode;
349{
350 return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_K (INTVAL (op)))
351 || GET_CODE (op) == CONSTANT_P_RTX
352 || register_operand (op, mode));
353}
354
97e242b0 355
c65ebc55
JW
356/* Return 1 if OP is a register operand, or an 8 bit adjusted immediate
357 operand. */
358
359int
360reg_or_8bit_adjusted_operand (op, mode)
361 rtx op;
362 enum machine_mode mode;
363{
364 return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_L (INTVAL (op)))
365 || GET_CODE (op) == CONSTANT_P_RTX
366 || register_operand (op, mode));
367}
368
369/* Return 1 if OP is a register operand, or is valid for both an 8 bit
370 immediate and an 8 bit adjusted immediate operand. This is necessary
371 because when we emit a compare, we don't know what the condition will be,
372 so we need the union of the immediates accepted by GT and LT. */
373
374int
375reg_or_8bit_and_adjusted_operand (op, mode)
376 rtx op;
377 enum machine_mode mode;
378{
379 return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_K (INTVAL (op))
380 && CONST_OK_FOR_L (INTVAL (op)))
381 || GET_CODE (op) == CONSTANT_P_RTX
382 || register_operand (op, mode));
383}
384
385/* Return 1 if OP is a register operand, or a 14 bit immediate operand. */
386
387int
388reg_or_14bit_operand (op, mode)
389 rtx op;
390 enum machine_mode mode;
391{
392 return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_I (INTVAL (op)))
393 || GET_CODE (op) == CONSTANT_P_RTX
394 || register_operand (op, mode));
395}
396
397/* Return 1 if OP is a register operand, or a 22 bit immediate operand. */
398
399int
400reg_or_22bit_operand (op, mode)
401 rtx op;
402 enum machine_mode mode;
403{
404 return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_J (INTVAL (op)))
405 || GET_CODE (op) == CONSTANT_P_RTX
406 || register_operand (op, mode));
407}
408
409/* Return 1 if OP is a 6 bit immediate operand. */
410
411int
412shift_count_operand (op, mode)
413 rtx op;
fd7c34b0 414 enum machine_mode mode ATTRIBUTE_UNUSED;
c65ebc55
JW
415{
416 return ((GET_CODE (op) == CONST_INT && CONST_OK_FOR_M (INTVAL (op)))
417 || GET_CODE (op) == CONSTANT_P_RTX);
418}
419
420/* Return 1 if OP is a 5 bit immediate operand. */
421
422int
423shift_32bit_count_operand (op, mode)
424 rtx op;
fd7c34b0 425 enum machine_mode mode ATTRIBUTE_UNUSED;
c65ebc55
JW
426{
427 return ((GET_CODE (op) == CONST_INT
428 && (INTVAL (op) >= 0 && INTVAL (op) < 32))
429 || GET_CODE (op) == CONSTANT_P_RTX);
430}
431
432/* Return 1 if OP is a 2, 4, 8, or 16 immediate operand. */
433
434int
435shladd_operand (op, mode)
436 rtx op;
fd7c34b0 437 enum machine_mode mode ATTRIBUTE_UNUSED;
c65ebc55
JW
438{
439 return (GET_CODE (op) == CONST_INT
440 && (INTVAL (op) == 2 || INTVAL (op) == 4
441 || INTVAL (op) == 8 || INTVAL (op) == 16));
442}
443
444/* Return 1 if OP is a -16, -8, -4, -1, 1, 4, 8, or 16 immediate operand. */
445
446int
447fetchadd_operand (op, mode)
448 rtx op;
fd7c34b0 449 enum machine_mode mode ATTRIBUTE_UNUSED;
c65ebc55
JW
450{
451 return (GET_CODE (op) == CONST_INT
452 && (INTVAL (op) == -16 || INTVAL (op) == -8 ||
453 INTVAL (op) == -4 || INTVAL (op) == -1 ||
454 INTVAL (op) == 1 || INTVAL (op) == 4 ||
455 INTVAL (op) == 8 || INTVAL (op) == 16));
456}
457
458/* Return 1 if OP is a floating-point constant zero, one, or a register. */
459
460int
461reg_or_fp01_operand (op, mode)
462 rtx op;
463 enum machine_mode mode;
464{
465 return ((GET_CODE (op) == CONST_DOUBLE && CONST_DOUBLE_OK_FOR_G (op))
466 || GET_CODE (op) == CONSTANT_P_RTX
467 || register_operand (op, mode));
468}
469
4b983fdc
RH
470/* Like nonimmediate_operand, but don't allow MEMs that try to use a
471 POST_MODIFY with a REG as displacement. */
472
473int
474destination_operand (op, mode)
475 rtx op;
476 enum machine_mode mode;
477{
478 if (! nonimmediate_operand (op, mode))
479 return 0;
480 if (GET_CODE (op) == MEM
481 && GET_CODE (XEXP (op, 0)) == POST_MODIFY
482 && GET_CODE (XEXP (XEXP (XEXP (op, 0), 1), 1)) == REG)
483 return 0;
484 return 1;
485}
486
c65ebc55
JW
487/* Return 1 if this is a comparison operator, which accepts an normal 8-bit
488 signed immediate operand. */
489
490int
491normal_comparison_operator (op, mode)
492 register rtx op;
493 enum machine_mode mode;
494{
495 enum rtx_code code = GET_CODE (op);
496 return ((mode == VOIDmode || GET_MODE (op) == mode)
809d4ef1 497 && (code == EQ || code == NE
c65ebc55
JW
498 || code == GT || code == LE || code == GTU || code == LEU));
499}
500
501/* Return 1 if this is a comparison operator, which accepts an adjusted 8-bit
502 signed immediate operand. */
503
504int
505adjusted_comparison_operator (op, mode)
506 register rtx op;
507 enum machine_mode mode;
508{
509 enum rtx_code code = GET_CODE (op);
510 return ((mode == VOIDmode || GET_MODE (op) == mode)
511 && (code == LT || code == GE || code == LTU || code == GEU));
512}
513
514/* Return 1 if OP is a call returning an HFA. It is known to be a PARALLEL
515 and the first section has already been tested. */
516
517int
518call_multiple_values_operation (op, mode)
519 rtx op;
520 enum machine_mode mode ATTRIBUTE_UNUSED;
521{
522 int count = XVECLEN (op, 0) - 2;
523 int i;
fd7c34b0 524 unsigned int dest_regno;
c65ebc55
JW
525
526 /* Perform a quick check so we don't block up below. */
527 if (count <= 1
528 || GET_CODE (XVECEXP (op, 0, 0)) != SET
529 || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
530 || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != CALL)
531 return 0;
532
533 dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
534
535 for (i = 1; i < count; i++)
536 {
537 rtx elt = XVECEXP (op, 0, i + 2);
538
539 if (GET_CODE (elt) != SET
540 || GET_CODE (SET_SRC (elt)) != CALL
541 || GET_CODE (SET_DEST (elt)) != REG
542 || REGNO (SET_DEST (elt)) != dest_regno + i)
543 return 0;
544 }
545
546 return 1;
547}
548
e5bde68a
RH
549/* Return 1 if this operator is valid for predication. */
550
551int
552predicate_operator (op, mode)
553 register rtx op;
554 enum machine_mode mode;
555{
556 enum rtx_code code = GET_CODE (op);
557 return ((GET_MODE (op) == mode || mode == VOIDmode)
558 && (code == EQ || code == NE));
559}
5527bf14
RH
560
561/* Return 1 if this is the ar.lc register. */
562
563int
564ar_lc_reg_operand (op, mode)
565 register rtx op;
566 enum machine_mode mode;
567{
568 return (GET_MODE (op) == DImode
569 && (mode == DImode || mode == VOIDmode)
570 && GET_CODE (op) == REG
571 && REGNO (op) == AR_LC_REGNUM);
572}
97e242b0
RH
573
574/* Return 1 if this is the ar.ccv register. */
575
576int
577ar_ccv_reg_operand (op, mode)
578 register rtx op;
579 enum machine_mode mode;
580{
581 return ((GET_MODE (op) == mode || mode == VOIDmode)
582 && GET_CODE (op) == REG
583 && REGNO (op) == AR_CCV_REGNUM);
584}
3f622353
RH
585
586/* Like general_operand, but don't allow (mem (addressof)). */
587
588int
589general_tfmode_operand (op, mode)
590 rtx op;
591 enum machine_mode mode;
592{
593 if (! general_operand (op, mode))
594 return 0;
595 if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == ADDRESSOF)
596 return 0;
597 return 1;
598}
599
600/* Similarly. */
601
602int
603destination_tfmode_operand (op, mode)
604 rtx op;
605 enum machine_mode mode;
606{
607 if (! destination_operand (op, mode))
608 return 0;
609 if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == ADDRESSOF)
610 return 0;
611 return 1;
612}
613
614/* Similarly. */
615
616int
617tfreg_or_fp01_operand (op, mode)
618 rtx op;
619 enum machine_mode mode;
620{
621 if (GET_CODE (op) == SUBREG)
622 return 0;
623 return reg_or_fp01_operand (op, mode);
624}
9b7bf67d 625\f
557b9df5
RH
626/* Return 1 if the operands of a move are ok. */
627
628int
629ia64_move_ok (dst, src)
630 rtx dst, src;
631{
632 /* If we're under init_recog_no_volatile, we'll not be able to use
633 memory_operand. So check the code directly and don't worry about
634 the validity of the underlying address, which should have been
635 checked elsewhere anyway. */
636 if (GET_CODE (dst) != MEM)
637 return 1;
638 if (GET_CODE (src) == MEM)
639 return 0;
640 if (register_operand (src, VOIDmode))
641 return 1;
642
643 /* Otherwise, this must be a constant, and that either 0 or 0.0 or 1.0. */
644 if (INTEGRAL_MODE_P (GET_MODE (dst)))
645 return src == const0_rtx;
646 else
647 return GET_CODE (src) == CONST_DOUBLE && CONST_DOUBLE_OK_FOR_G (src);
648}
9b7bf67d 649
041f25e6
RH
650/* Check if OP is a mask suitible for use with SHIFT in a dep.z instruction.
651 Return the length of the field, or <= 0 on failure. */
652
653int
654ia64_depz_field_mask (rop, rshift)
655 rtx rop, rshift;
656{
657 unsigned HOST_WIDE_INT op = INTVAL (rop);
658 unsigned HOST_WIDE_INT shift = INTVAL (rshift);
659
660 /* Get rid of the zero bits we're shifting in. */
661 op >>= shift;
662
663 /* We must now have a solid block of 1's at bit 0. */
664 return exact_log2 (op + 1);
665}
666
9b7bf67d
RH
667/* Expand a symbolic constant load. */
668/* ??? Should generalize this, so that we can also support 32 bit pointers. */
669
670void
671ia64_expand_load_address (dest, src)
672 rtx dest, src;
673{
674 rtx temp;
675
676 /* The destination could be a MEM during initial rtl generation,
677 which isn't a valid destination for the PIC load address patterns. */
678 if (! register_operand (dest, DImode))
679 temp = gen_reg_rtx (DImode);
680 else
681 temp = dest;
682
683 if (TARGET_AUTO_PIC)
684 emit_insn (gen_load_gprel64 (temp, src));
685 else if (GET_CODE (src) == SYMBOL_REF && SYMBOL_REF_FLAG (src))
686 emit_insn (gen_load_fptr (temp, src));
687 else if (sdata_symbolic_operand (src, DImode))
688 emit_insn (gen_load_gprel (temp, src));
689 else if (GET_CODE (src) == CONST
690 && GET_CODE (XEXP (src, 0)) == PLUS
691 && GET_CODE (XEXP (XEXP (src, 0), 1)) == CONST_INT
692 && (INTVAL (XEXP (XEXP (src, 0), 1)) & 0x1fff) != 0)
693 {
694 rtx subtarget = no_new_pseudos ? temp : gen_reg_rtx (DImode);
695 rtx sym = XEXP (XEXP (src, 0), 0);
696 HOST_WIDE_INT ofs, hi, lo;
697
698 /* Split the offset into a sign extended 14-bit low part
699 and a complementary high part. */
700 ofs = INTVAL (XEXP (XEXP (src, 0), 1));
701 lo = ((ofs & 0x3fff) ^ 0x2000) - 0x2000;
702 hi = ofs - lo;
703
704 emit_insn (gen_load_symptr (subtarget, plus_constant (sym, hi)));
705 emit_insn (gen_adddi3 (temp, subtarget, GEN_INT (lo)));
706 }
707 else
708 emit_insn (gen_load_symptr (temp, src));
709
710 if (temp != dest)
711 emit_move_insn (dest, temp);
712}
97e242b0
RH
713
714rtx
715ia64_gp_save_reg (setjmp_p)
716 int setjmp_p;
717{
718 rtx save = cfun->machine->ia64_gp_save;
719
720 if (save != NULL)
721 {
722 /* We can't save GP in a pseudo if we are calling setjmp, because
723 pseudos won't be restored by longjmp. For now, we save it in r4. */
724 /* ??? It would be more efficient to save this directly into a stack
725 slot. Unfortunately, the stack slot address gets cse'd across
726 the setjmp call because the NOTE_INSN_SETJMP note is in the wrong
727 place. */
728
729 /* ??? Get the barf bag, Virginia. We've got to replace this thing
730 in place, since this rtx is used in exception handling receivers.
731 Moreover, we must get this rtx out of regno_reg_rtx or reload
732 will do the wrong thing. */
733 unsigned int old_regno = REGNO (save);
734 if (setjmp_p && old_regno != GR_REG (4))
735 {
736 REGNO (save) = GR_REG (4);
737 regno_reg_rtx[old_regno] = gen_rtx_raw_REG (DImode, old_regno);
738 }
739 }
740 else
741 {
742 if (setjmp_p)
743 save = gen_rtx_REG (DImode, GR_REG (4));
744 else if (! optimize)
745 save = gen_rtx_REG (DImode, LOC_REG (0));
746 else
747 save = gen_reg_rtx (DImode);
748 cfun->machine->ia64_gp_save = save;
749 }
750
751 return save;
752}
3f622353
RH
753
754/* Split a post-reload TImode reference into two DImode components. */
755
756rtx
757ia64_split_timode (out, in, scratch)
758 rtx out[2];
759 rtx in, scratch;
760{
761 switch (GET_CODE (in))
762 {
763 case REG:
764 out[0] = gen_rtx_REG (DImode, REGNO (in));
765 out[1] = gen_rtx_REG (DImode, REGNO (in) + 1);
766 return NULL_RTX;
767
768 case MEM:
769 {
770 HOST_WIDE_INT offset;
771 rtx base = XEXP (in, 0);
772 rtx offset_rtx;
773
774 switch (GET_CODE (base))
775 {
776 case REG:
777 out[0] = change_address (in, DImode, NULL_RTX);
778 break;
779 case POST_MODIFY:
780 base = XEXP (base, 0);
781 out[0] = change_address (in, DImode, NULL_RTX);
782 break;
783
784 /* Since we're changing the mode, we need to change to POST_MODIFY
785 as well to preserve the size of the increment. Either that or
786 do the update in two steps, but we've already got this scratch
787 register handy so let's use it. */
788 case POST_INC:
789 base = XEXP (base, 0);
790 out[0] = change_address (in, DImode,
791 gen_rtx_POST_MODIFY (Pmode, base,plus_constant (base, 16)));
792 break;
793 case POST_DEC:
794 base = XEXP (base, 0);
795 out[0] = change_address (in, DImode,
796 gen_rtx_POST_MODIFY (Pmode, base,plus_constant (base, -16)));
797 break;
798 default:
799 abort ();
800 }
801
802 if (scratch == NULL_RTX)
803 abort ();
804 out[1] = change_address (in, DImode, scratch);
805 return gen_adddi3 (scratch, base, GEN_INT (8));
806 }
807
808 case CONST_INT:
809 case CONST_DOUBLE:
810 split_double (in, &out[0], &out[1]);
811 return NULL_RTX;
812
813 default:
814 abort ();
815 }
816}
817
818/* ??? Fixing GR->FR TFmode moves during reload is hard. You need to go
819 through memory plus an extra GR scratch register. Except that you can
820 either get the first from SECONDARY_MEMORY_NEEDED or the second from
821 SECONDARY_RELOAD_CLASS, but not both.
822
823 We got into problems in the first place by allowing a construct like
824 (subreg:TF (reg:TI)), which we got from a union containing a long double.
825 This solution attempts to prevent this situation from ocurring. When
826 we see something like the above, we spill the inner register to memory. */
827
828rtx
829spill_tfmode_operand (in, force)
830 rtx in;
831 int force;
832{
833 if (GET_CODE (in) == SUBREG
834 && GET_MODE (SUBREG_REG (in)) == TImode
835 && GET_CODE (SUBREG_REG (in)) == REG)
836 {
837 rtx mem = gen_mem_addressof (SUBREG_REG (in), NULL_TREE);
838 return gen_rtx_MEM (TFmode, copy_to_reg (XEXP (mem, 0)));
839 }
840 else if (force && GET_CODE (in) == REG)
841 {
842 rtx mem = gen_mem_addressof (in, NULL_TREE);
843 return gen_rtx_MEM (TFmode, copy_to_reg (XEXP (mem, 0)));
844 }
845 else if (GET_CODE (in) == MEM
846 && GET_CODE (XEXP (in, 0)) == ADDRESSOF)
847 {
848 return change_address (in, TFmode, copy_to_reg (XEXP (in, 0)));
849 }
850 else
851 return in;
852}
809d4ef1 853\f
3b572406
RH
854/* Begin the assembly file. */
855
856void
857ia64_file_start (f)
858 FILE *f;
859{
860 unsigned int rs, re;
861 int out_state;
862
863 rs = 1;
864 out_state = 0;
865 while (1)
866 {
867 while (rs < 64 && call_used_regs[PR_REG (rs)])
868 rs++;
869 if (rs >= 64)
870 break;
871 for (re = rs + 1; re < 64 && ! call_used_regs[PR_REG (re)]; re++)
872 continue;
873 if (out_state == 0)
874 {
875 fputs ("\t.pred.safe_across_calls ", f);
876 out_state = 1;
877 }
878 else
879 fputc (',', f);
880 if (re == rs + 1)
881 fprintf (f, "p%u", rs);
882 else
883 fprintf (f, "p%u-p%u", rs, re - 1);
884 rs = re + 1;
885 }
886 if (out_state)
887 fputc ('\n', f);
888}
889
97e242b0 890
c65ebc55
JW
891/* Structure to be filled in by ia64_compute_frame_size with register
892 save masks and offsets for the current function. */
893
894struct ia64_frame_info
895{
97e242b0
RH
896 HOST_WIDE_INT total_size; /* size of the stack frame, not including
897 the caller's scratch area. */
898 HOST_WIDE_INT spill_cfa_off; /* top of the reg spill area from the cfa. */
899 HOST_WIDE_INT spill_size; /* size of the gr/br/fr spill area. */
900 HOST_WIDE_INT extra_spill_size; /* size of spill area for others. */
c65ebc55 901 HARD_REG_SET mask; /* mask of saved registers. */
97e242b0
RH
902 unsigned int gr_used_mask; /* mask of registers in use as gr spill
903 registers or long-term scratches. */
904 int n_spilled; /* number of spilled registers. */
905 int reg_fp; /* register for fp. */
906 int reg_save_b0; /* save register for b0. */
907 int reg_save_pr; /* save register for prs. */
908 int reg_save_ar_pfs; /* save register for ar.pfs. */
909 int reg_save_ar_unat; /* save register for ar.unat. */
910 int reg_save_ar_lc; /* save register for ar.lc. */
911 int n_input_regs; /* number of input registers used. */
912 int n_local_regs; /* number of local registers used. */
913 int n_output_regs; /* number of output registers used. */
914 int n_rotate_regs; /* number of rotating registers used. */
915
916 char need_regstk; /* true if a .regstk directive needed. */
917 char initialized; /* true if the data is finalized. */
c65ebc55
JW
918};
919
97e242b0
RH
920/* Current frame information calculated by ia64_compute_frame_size. */
921static struct ia64_frame_info current_frame_info;
c65ebc55 922
97e242b0
RH
923/* Helper function for ia64_compute_frame_size: find an appropriate general
924 register to spill some special register to. SPECIAL_SPILL_MASK contains
925 bits in GR0 to GR31 that have already been allocated by this routine.
926 TRY_LOCALS is true if we should attempt to locate a local regnum. */
c65ebc55 927
97e242b0
RH
928static int
929find_gr_spill (try_locals)
930 int try_locals;
931{
932 int regno;
933
934 /* If this is a leaf function, first try an otherwise unused
935 call-clobbered register. */
936 if (current_function_is_leaf)
937 {
938 for (regno = GR_REG (1); regno <= GR_REG (31); regno++)
939 if (! regs_ever_live[regno]
940 && call_used_regs[regno]
941 && ! fixed_regs[regno]
942 && ! global_regs[regno]
943 && ((current_frame_info.gr_used_mask >> regno) & 1) == 0)
944 {
945 current_frame_info.gr_used_mask |= 1 << regno;
946 return regno;
947 }
948 }
949
950 if (try_locals)
951 {
952 regno = current_frame_info.n_local_regs;
953 if (regno < 80)
954 {
955 current_frame_info.n_local_regs = regno + 1;
956 return LOC_REG (0) + regno;
957 }
958 }
959
960 /* Failed to find a general register to spill to. Must use stack. */
961 return 0;
962}
963
964/* In order to make for nice schedules, we try to allocate every temporary
965 to a different register. We must of course stay away from call-saved,
966 fixed, and global registers. We must also stay away from registers
967 allocated in current_frame_info.gr_used_mask, since those include regs
968 used all through the prologue.
969
970 Any register allocated here must be used immediately. The idea is to
971 aid scheduling, not to solve data flow problems. */
972
973static int last_scratch_gr_reg;
974
975static int
976next_scratch_gr_reg ()
977{
978 int i, regno;
979
980 for (i = 0; i < 32; ++i)
981 {
982 regno = (last_scratch_gr_reg + i + 1) & 31;
983 if (call_used_regs[regno]
984 && ! fixed_regs[regno]
985 && ! global_regs[regno]
986 && ((current_frame_info.gr_used_mask >> regno) & 1) == 0)
987 {
988 last_scratch_gr_reg = regno;
989 return regno;
990 }
991 }
992
993 /* There must be _something_ available. */
994 abort ();
995}
996
997/* Helper function for ia64_compute_frame_size, called through
998 diddle_return_value. Mark REG in current_frame_info.gr_used_mask. */
999
1000static void
1001mark_reg_gr_used_mask (reg, data)
1002 rtx reg;
1003 void *data ATTRIBUTE_UNUSED;
c65ebc55 1004{
97e242b0
RH
1005 unsigned int regno = REGNO (reg);
1006 if (regno < 32)
1007 current_frame_info.gr_used_mask |= 1 << regno;
c65ebc55
JW
1008}
1009
1010/* Returns the number of bytes offset between the frame pointer and the stack
1011 pointer for the current function. SIZE is the number of bytes of space
1012 needed for local variables. */
97e242b0
RH
1013
1014static void
c65ebc55 1015ia64_compute_frame_size (size)
97e242b0 1016 HOST_WIDE_INT size;
c65ebc55 1017{
97e242b0
RH
1018 HOST_WIDE_INT total_size;
1019 HOST_WIDE_INT spill_size = 0;
1020 HOST_WIDE_INT extra_spill_size = 0;
1021 HOST_WIDE_INT pretend_args_size;
c65ebc55 1022 HARD_REG_SET mask;
97e242b0
RH
1023 int n_spilled = 0;
1024 int spilled_gr_p = 0;
1025 int spilled_fr_p = 0;
1026 unsigned int regno;
1027 int i;
c65ebc55 1028
97e242b0
RH
1029 if (current_frame_info.initialized)
1030 return;
294dac80 1031
97e242b0 1032 memset (&current_frame_info, 0, sizeof current_frame_info);
c65ebc55
JW
1033 CLEAR_HARD_REG_SET (mask);
1034
97e242b0
RH
1035 /* Don't allocate scratches to the return register. */
1036 diddle_return_value (mark_reg_gr_used_mask, NULL);
1037
1038 /* Don't allocate scratches to the EH scratch registers. */
1039 if (cfun->machine->ia64_eh_epilogue_sp)
1040 mark_reg_gr_used_mask (cfun->machine->ia64_eh_epilogue_sp, NULL);
1041 if (cfun->machine->ia64_eh_epilogue_bsp)
1042 mark_reg_gr_used_mask (cfun->machine->ia64_eh_epilogue_bsp, NULL);
c65ebc55 1043
97e242b0
RH
1044 /* Find the size of the register stack frame. We have only 80 local
1045 registers, because we reserve 8 for the inputs and 8 for the
1046 outputs. */
1047
1048 /* Skip HARD_FRAME_POINTER_REGNUM (loc79) when frame_pointer_needed,
1049 since we'll be adjusting that down later. */
1050 regno = LOC_REG (78) + ! frame_pointer_needed;
1051 for (; regno >= LOC_REG (0); regno--)
1052 if (regs_ever_live[regno])
1053 break;
1054 current_frame_info.n_local_regs = regno - LOC_REG (0) + 1;
c65ebc55 1055
97e242b0
RH
1056 if (current_function_varargs || current_function_stdarg)
1057 current_frame_info.n_input_regs = 8;
1058 else
1059 {
1060 for (regno = IN_REG (7); regno >= IN_REG (0); regno--)
1061 if (regs_ever_live[regno])
1062 break;
1063 current_frame_info.n_input_regs = regno - IN_REG (0) + 1;
1064 }
1065
1066 for (regno = OUT_REG (7); regno >= OUT_REG (0); regno--)
1067 if (regs_ever_live[regno])
1068 break;
1069 i = regno - OUT_REG (0) + 1;
1070
1071 /* When -p profiling, we need one output register for the mcount argument.
1072 Likwise for -a profiling for the bb_init_func argument. For -ax
1073 profiling, we need two output registers for the two bb_init_trace_func
1074 arguments. */
1075 if (profile_flag || profile_block_flag == 1)
1076 i = MAX (i, 1);
1077 else if (profile_block_flag == 2)
1078 i = MAX (i, 2);
1079 current_frame_info.n_output_regs = i;
1080
1081 /* ??? No rotating register support yet. */
1082 current_frame_info.n_rotate_regs = 0;
1083
1084 /* Discover which registers need spilling, and how much room that
1085 will take. Begin with floating point and general registers,
1086 which will always wind up on the stack. */
1087
1088 for (regno = FR_REG (2); regno <= FR_REG (127); regno++)
c65ebc55
JW
1089 if (regs_ever_live[regno] && ! call_used_regs[regno])
1090 {
1091 SET_HARD_REG_BIT (mask, regno);
97e242b0
RH
1092 spill_size += 16;
1093 n_spilled += 1;
1094 spilled_fr_p = 1;
c65ebc55
JW
1095 }
1096
97e242b0 1097 for (regno = GR_REG (1); regno <= GR_REG (31); regno++)
c65ebc55
JW
1098 if (regs_ever_live[regno] && ! call_used_regs[regno])
1099 {
1100 SET_HARD_REG_BIT (mask, regno);
97e242b0
RH
1101 spill_size += 8;
1102 n_spilled += 1;
1103 spilled_gr_p = 1;
c65ebc55
JW
1104 }
1105
97e242b0 1106 for (regno = BR_REG (1); regno <= BR_REG (7); regno++)
c65ebc55
JW
1107 if (regs_ever_live[regno] && ! call_used_regs[regno])
1108 {
1109 SET_HARD_REG_BIT (mask, regno);
97e242b0
RH
1110 spill_size += 8;
1111 n_spilled += 1;
c65ebc55
JW
1112 }
1113
97e242b0
RH
1114 /* Now come all special registers that might get saved in other
1115 general registers. */
1116
1117 if (frame_pointer_needed)
1118 {
1119 current_frame_info.reg_fp = find_gr_spill (1);
1120 /* We should have gotten at least LOC79, since that's what
1121 HARD_FRAME_POINTER_REGNUM is. */
1122 if (current_frame_info.reg_fp == 0)
1123 abort ();
1124 }
1125
1126 if (! current_function_is_leaf)
c65ebc55 1127 {
97e242b0
RH
1128 /* Emit a save of BR0 if we call other functions. Do this even
1129 if this function doesn't return, as EH depends on this to be
1130 able to unwind the stack. */
1131 SET_HARD_REG_BIT (mask, BR_REG (0));
1132
1133 current_frame_info.reg_save_b0 = find_gr_spill (1);
1134 if (current_frame_info.reg_save_b0 == 0)
1135 {
1136 spill_size += 8;
1137 n_spilled += 1;
1138 }
1139
1140 /* Similarly for ar.pfs. */
1141 SET_HARD_REG_BIT (mask, AR_PFS_REGNUM);
1142 current_frame_info.reg_save_ar_pfs = find_gr_spill (1);
1143 if (current_frame_info.reg_save_ar_pfs == 0)
1144 {
1145 extra_spill_size += 8;
1146 n_spilled += 1;
1147 }
c65ebc55
JW
1148 }
1149 else
97e242b0
RH
1150 {
1151 if (regs_ever_live[BR_REG (0)] && ! call_used_regs[BR_REG (0)])
1152 {
1153 SET_HARD_REG_BIT (mask, BR_REG (0));
1154 spill_size += 8;
1155 n_spilled += 1;
1156 }
1157 }
c65ebc55 1158
97e242b0
RH
1159 /* Unwind descriptor hackery: things are most efficient if we allocate
1160 consecutive GR save registers for RP, PFS, FP in that order. However,
1161 it is absolutely critical that FP get the only hard register that's
1162 guaranteed to be free, so we allocated it first. If all three did
1163 happen to be allocated hard regs, and are consecutive, rearrange them
1164 into the preferred order now. */
1165 if (current_frame_info.reg_fp != 0
1166 && current_frame_info.reg_save_b0 == current_frame_info.reg_fp + 1
1167 && current_frame_info.reg_save_ar_pfs == current_frame_info.reg_fp + 2)
5527bf14 1168 {
97e242b0
RH
1169 current_frame_info.reg_save_b0 = current_frame_info.reg_fp;
1170 current_frame_info.reg_save_ar_pfs = current_frame_info.reg_fp + 1;
1171 current_frame_info.reg_fp = current_frame_info.reg_fp + 2;
5527bf14
RH
1172 }
1173
97e242b0
RH
1174 /* See if we need to store the predicate register block. */
1175 for (regno = PR_REG (0); regno <= PR_REG (63); regno++)
1176 if (regs_ever_live[regno] && ! call_used_regs[regno])
1177 break;
1178 if (regno <= PR_REG (63))
c65ebc55 1179 {
97e242b0
RH
1180 SET_HARD_REG_BIT (mask, PR_REG (0));
1181 current_frame_info.reg_save_pr = find_gr_spill (1);
1182 if (current_frame_info.reg_save_pr == 0)
1183 {
1184 extra_spill_size += 8;
1185 n_spilled += 1;
1186 }
1187
1188 /* ??? Mark them all as used so that register renaming and such
1189 are free to use them. */
1190 for (regno = PR_REG (0); regno <= PR_REG (63); regno++)
1191 regs_ever_live[regno] = 1;
c65ebc55
JW
1192 }
1193
97e242b0
RH
1194 /* If we're forced to use st8.spill, we're forced to save and restore
1195 ar.unat as well. */
1196 if (spilled_gr_p || current_function_varargs || current_function_stdarg)
1197 {
1198 SET_HARD_REG_BIT (mask, AR_UNAT_REGNUM);
1199 current_frame_info.reg_save_ar_unat = find_gr_spill (spill_size == 0);
1200 if (current_frame_info.reg_save_ar_unat == 0)
1201 {
1202 extra_spill_size += 8;
1203 n_spilled += 1;
1204 }
1205 }
1206
1207 if (regs_ever_live[AR_LC_REGNUM])
1208 {
1209 SET_HARD_REG_BIT (mask, AR_LC_REGNUM);
1210 current_frame_info.reg_save_ar_lc = find_gr_spill (spill_size == 0);
1211 if (current_frame_info.reg_save_ar_lc == 0)
1212 {
1213 extra_spill_size += 8;
1214 n_spilled += 1;
1215 }
1216 }
1217
1218 /* If we have an odd number of words of pretend arguments written to
1219 the stack, then the FR save area will be unaligned. We round the
1220 size of this area up to keep things 16 byte aligned. */
1221 if (spilled_fr_p)
1222 pretend_args_size = IA64_STACK_ALIGN (current_function_pretend_args_size);
1223 else
1224 pretend_args_size = current_function_pretend_args_size;
1225
1226 total_size = (spill_size + extra_spill_size + size + pretend_args_size
1227 + current_function_outgoing_args_size);
1228 total_size = IA64_STACK_ALIGN (total_size);
1229
1230 /* We always use the 16-byte scratch area provided by the caller, but
1231 if we are a leaf function, there's no one to which we need to provide
1232 a scratch area. */
1233 if (current_function_is_leaf)
1234 total_size = MAX (0, total_size - 16);
1235
c65ebc55 1236 current_frame_info.total_size = total_size;
97e242b0
RH
1237 current_frame_info.spill_cfa_off = pretend_args_size - 16;
1238 current_frame_info.spill_size = spill_size;
1239 current_frame_info.extra_spill_size = extra_spill_size;
c65ebc55 1240 COPY_HARD_REG_SET (current_frame_info.mask, mask);
97e242b0 1241 current_frame_info.n_spilled = n_spilled;
c65ebc55 1242 current_frame_info.initialized = reload_completed;
97e242b0
RH
1243}
1244
1245/* Compute the initial difference between the specified pair of registers. */
1246
1247HOST_WIDE_INT
1248ia64_initial_elimination_offset (from, to)
1249 int from, to;
1250{
1251 HOST_WIDE_INT offset;
1252
1253 ia64_compute_frame_size (get_frame_size ());
1254 switch (from)
1255 {
1256 case FRAME_POINTER_REGNUM:
1257 if (to == HARD_FRAME_POINTER_REGNUM)
1258 {
1259 if (current_function_is_leaf)
1260 offset = -current_frame_info.total_size;
1261 else
1262 offset = -(current_frame_info.total_size
1263 - current_function_outgoing_args_size - 16);
1264 }
1265 else if (to == STACK_POINTER_REGNUM)
1266 {
1267 if (current_function_is_leaf)
1268 offset = 0;
1269 else
1270 offset = 16 + current_function_outgoing_args_size;
1271 }
1272 else
1273 abort ();
1274 break;
c65ebc55 1275
97e242b0
RH
1276 case ARG_POINTER_REGNUM:
1277 /* Arguments start above the 16 byte save area, unless stdarg
1278 in which case we store through the 16 byte save area. */
1279 if (to == HARD_FRAME_POINTER_REGNUM)
1280 offset = 16 - current_function_pretend_args_size;
1281 else if (to == STACK_POINTER_REGNUM)
1282 offset = (current_frame_info.total_size
1283 + 16 - current_function_pretend_args_size);
1284 else
1285 abort ();
1286 break;
1287
1288 case RETURN_ADDRESS_POINTER_REGNUM:
1289 offset = 0;
1290 break;
1291
1292 default:
1293 abort ();
1294 }
1295
1296 return offset;
c65ebc55
JW
1297}
1298
97e242b0
RH
1299/* If there are more than a trivial number of register spills, we use
1300 two interleaved iterators so that we can get two memory references
1301 per insn group.
1302
1303 In order to simplify things in the prologue and epilogue expanders,
1304 we use helper functions to fix up the memory references after the
1305 fact with the appropriate offsets to a POST_MODIFY memory mode.
1306 The following data structure tracks the state of the two iterators
1307 while insns are being emitted. */
1308
1309struct spill_fill_data
c65ebc55 1310{
97e242b0
RH
1311 rtx init_after; /* point at which to emit intializations */
1312 rtx init_reg[2]; /* initial base register */
1313 rtx iter_reg[2]; /* the iterator registers */
1314 rtx *prev_addr[2]; /* address of last memory use */
1315 HOST_WIDE_INT prev_off[2]; /* last offset */
1316 int n_iter; /* number of iterators in use */
1317 int next_iter; /* next iterator to use */
1318 unsigned int save_gr_used_mask;
1319};
1320
1321static struct spill_fill_data spill_fill_data;
c65ebc55 1322
97e242b0
RH
1323static void
1324setup_spill_pointers (n_spills, init_reg, cfa_off)
1325 int n_spills;
1326 rtx init_reg;
1327 HOST_WIDE_INT cfa_off;
1328{
1329 int i;
1330
1331 spill_fill_data.init_after = get_last_insn ();
1332 spill_fill_data.init_reg[0] = init_reg;
1333 spill_fill_data.init_reg[1] = init_reg;
1334 spill_fill_data.prev_addr[0] = NULL;
1335 spill_fill_data.prev_addr[1] = NULL;
1336 spill_fill_data.prev_off[0] = cfa_off;
1337 spill_fill_data.prev_off[1] = cfa_off;
1338 spill_fill_data.next_iter = 0;
1339 spill_fill_data.save_gr_used_mask = current_frame_info.gr_used_mask;
1340
1341 spill_fill_data.n_iter = 1 + (n_spills > 2);
1342 for (i = 0; i < spill_fill_data.n_iter; ++i)
c65ebc55 1343 {
97e242b0
RH
1344 int regno = next_scratch_gr_reg ();
1345 spill_fill_data.iter_reg[i] = gen_rtx_REG (DImode, regno);
1346 current_frame_info.gr_used_mask |= 1 << regno;
1347 }
1348}
1349
1350static void
1351finish_spill_pointers ()
1352{
1353 current_frame_info.gr_used_mask = spill_fill_data.save_gr_used_mask;
1354}
c65ebc55 1355
97e242b0
RH
1356static rtx
1357spill_restore_mem (reg, cfa_off)
1358 rtx reg;
1359 HOST_WIDE_INT cfa_off;
1360{
1361 int iter = spill_fill_data.next_iter;
1362 HOST_WIDE_INT disp = spill_fill_data.prev_off[iter] - cfa_off;
1363 rtx disp_rtx = GEN_INT (disp);
1364 rtx mem;
1365
1366 if (spill_fill_data.prev_addr[iter])
1367 {
1368 if (CONST_OK_FOR_N (disp))
1369 *spill_fill_data.prev_addr[iter]
1370 = gen_rtx_POST_MODIFY (DImode, spill_fill_data.iter_reg[iter],
1371 gen_rtx_PLUS (DImode,
1372 spill_fill_data.iter_reg[iter],
1373 disp_rtx));
c65ebc55
JW
1374 else
1375 {
97e242b0
RH
1376 /* ??? Could use register post_modify for loads. */
1377 if (! CONST_OK_FOR_I (disp))
1378 {
1379 rtx tmp = gen_rtx_REG (DImode, next_scratch_gr_reg ());
1380 emit_move_insn (tmp, disp_rtx);
1381 disp_rtx = tmp;
1382 }
1383 emit_insn (gen_adddi3 (spill_fill_data.iter_reg[iter],
1384 spill_fill_data.iter_reg[iter], disp_rtx));
c65ebc55 1385 }
97e242b0
RH
1386 }
1387 /* Micro-optimization: if we've created a frame pointer, it's at
1388 CFA 0, which may allow the real iterator to be initialized lower,
1389 slightly increasing parallelism. Also, if there are few saves
1390 it may eliminate the iterator entirely. */
1391 else if (disp == 0
1392 && spill_fill_data.init_reg[iter] == stack_pointer_rtx
1393 && frame_pointer_needed)
1394 {
1395 mem = gen_rtx_MEM (GET_MODE (reg), hard_frame_pointer_rtx);
1396 MEM_ALIAS_SET (mem) = get_varargs_alias_set ();
1397 return mem;
1398 }
1399 else
1400 {
1401 rtx seq;
809d4ef1 1402
97e242b0
RH
1403 if (disp == 0)
1404 seq = gen_movdi (spill_fill_data.iter_reg[iter],
1405 spill_fill_data.init_reg[iter]);
1406 else
c65ebc55 1407 {
97e242b0
RH
1408 start_sequence ();
1409
1410 if (! CONST_OK_FOR_I (disp))
c65ebc55 1411 {
97e242b0
RH
1412 rtx tmp = gen_rtx_REG (DImode, next_scratch_gr_reg ());
1413 emit_move_insn (tmp, disp_rtx);
1414 disp_rtx = tmp;
c65ebc55 1415 }
97e242b0
RH
1416
1417 emit_insn (gen_adddi3 (spill_fill_data.iter_reg[iter],
1418 spill_fill_data.init_reg[iter],
1419 disp_rtx));
1420
1421 seq = gen_sequence ();
1422 end_sequence ();
c65ebc55 1423 }
809d4ef1 1424
97e242b0
RH
1425 /* Careful for being the first insn in a sequence. */
1426 if (spill_fill_data.init_after)
1427 spill_fill_data.init_after
1428 = emit_insn_after (seq, spill_fill_data.init_after);
1429 else
bc08aefe
RH
1430 {
1431 rtx first = get_insns ();
1432 if (first)
1433 spill_fill_data.init_after
1434 = emit_insn_before (seq, first);
1435 else
1436 spill_fill_data.init_after = emit_insn (seq);
1437 }
97e242b0 1438 }
c65ebc55 1439
97e242b0 1440 mem = gen_rtx_MEM (GET_MODE (reg), spill_fill_data.iter_reg[iter]);
c65ebc55 1441
97e242b0
RH
1442 /* ??? Not all of the spills are for varargs, but some of them are.
1443 The rest of the spills belong in an alias set of their own. But
1444 it doesn't actually hurt to include them here. */
1445 MEM_ALIAS_SET (mem) = get_varargs_alias_set ();
809d4ef1 1446
97e242b0
RH
1447 spill_fill_data.prev_addr[iter] = &XEXP (mem, 0);
1448 spill_fill_data.prev_off[iter] = cfa_off;
c65ebc55 1449
97e242b0
RH
1450 if (++iter >= spill_fill_data.n_iter)
1451 iter = 0;
1452 spill_fill_data.next_iter = iter;
c65ebc55 1453
97e242b0
RH
1454 return mem;
1455}
5527bf14 1456
97e242b0
RH
1457static void
1458do_spill (move_fn, reg, cfa_off, frame_reg)
870f9ec0 1459 rtx (*move_fn) PARAMS ((rtx, rtx, rtx));
97e242b0
RH
1460 rtx reg, frame_reg;
1461 HOST_WIDE_INT cfa_off;
1462{
1463 rtx mem, insn;
5527bf14 1464
97e242b0 1465 mem = spill_restore_mem (reg, cfa_off);
870f9ec0 1466 insn = emit_insn ((*move_fn) (mem, reg, GEN_INT (cfa_off)));
5527bf14 1467
97e242b0
RH
1468 if (frame_reg)
1469 {
1470 rtx base;
1471 HOST_WIDE_INT off;
1472
1473 RTX_FRAME_RELATED_P (insn) = 1;
1474
1475 /* Don't even pretend that the unwind code can intuit its way
1476 through a pair of interleaved post_modify iterators. Just
1477 provide the correct answer. */
1478
1479 if (frame_pointer_needed)
1480 {
1481 base = hard_frame_pointer_rtx;
1482 off = - cfa_off;
5527bf14 1483 }
97e242b0
RH
1484 else
1485 {
1486 base = stack_pointer_rtx;
1487 off = current_frame_info.total_size - cfa_off;
1488 }
1489
1490 REG_NOTES (insn)
1491 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
1492 gen_rtx_SET (VOIDmode,
1493 gen_rtx_MEM (GET_MODE (reg),
1494 plus_constant (base, off)),
1495 frame_reg),
1496 REG_NOTES (insn));
c65ebc55
JW
1497 }
1498}
1499
97e242b0
RH
1500static void
1501do_restore (move_fn, reg, cfa_off)
870f9ec0 1502 rtx (*move_fn) PARAMS ((rtx, rtx, rtx));
97e242b0
RH
1503 rtx reg;
1504 HOST_WIDE_INT cfa_off;
1505{
870f9ec0
RH
1506 emit_insn ((*move_fn) (reg, spill_restore_mem (reg, cfa_off),
1507 GEN_INT (cfa_off)));
97e242b0
RH
1508}
1509
870f9ec0
RH
1510/* Wrapper functions that discards the CONST_INT spill offset. These
1511 exist so that we can give gr_spill/gr_fill the offset they need and
1512 use a consistant function interface. */
1513
1514static rtx
1515gen_movdi_x (dest, src, offset)
1516 rtx dest, src;
1517 rtx offset ATTRIBUTE_UNUSED;
1518{
1519 return gen_movdi (dest, src);
1520}
1521
1522static rtx
1523gen_fr_spill_x (dest, src, offset)
1524 rtx dest, src;
1525 rtx offset ATTRIBUTE_UNUSED;
1526{
1527 return gen_fr_spill (dest, src);
1528}
1529
1530static rtx
1531gen_fr_restore_x (dest, src, offset)
1532 rtx dest, src;
1533 rtx offset ATTRIBUTE_UNUSED;
1534{
1535 return gen_fr_restore (dest, src);
1536}
c65ebc55
JW
1537
1538/* Called after register allocation to add any instructions needed for the
1539 prologue. Using a prologue insn is favored compared to putting all of the
1540 instructions in the FUNCTION_PROLOGUE macro, since it allows the scheduler
1541 to intermix instructions with the saves of the caller saved registers. In
1542 some cases, it might be necessary to emit a barrier instruction as the last
1543 insn to prevent such scheduling.
1544
1545 Also any insns generated here should have RTX_FRAME_RELATED_P(insn) = 1
97e242b0
RH
1546 so that the debug info generation code can handle them properly.
1547
1548 The register save area is layed out like so:
1549 cfa+16
1550 [ varargs spill area ]
1551 [ fr register spill area ]
1552 [ br register spill area ]
1553 [ ar register spill area ]
1554 [ pr register spill area ]
1555 [ gr register spill area ] */
c65ebc55
JW
1556
1557/* ??? Get inefficient code when the frame size is larger than can fit in an
1558 adds instruction. */
1559
c65ebc55
JW
1560void
1561ia64_expand_prologue ()
1562{
97e242b0
RH
1563 rtx insn, ar_pfs_save_reg, ar_unat_save_reg;
1564 int i, epilogue_p, regno, alt_regno, cfa_off, n_varargs;
1565 rtx reg, alt_reg;
1566
1567 ia64_compute_frame_size (get_frame_size ());
1568 last_scratch_gr_reg = 15;
1569
1570 /* If there is no epilogue, then we don't need some prologue insns.
1571 We need to avoid emitting the dead prologue insns, because flow
1572 will complain about them. */
c65ebc55
JW
1573 if (optimize)
1574 {
97e242b0
RH
1575 edge e;
1576
c65ebc55
JW
1577 for (e = EXIT_BLOCK_PTR->pred; e ; e = e->pred_next)
1578 if ((e->flags & EDGE_FAKE) == 0
1579 && (e->flags & EDGE_FALLTHRU) != 0)
1580 break;
1581 epilogue_p = (e != NULL);
1582 }
1583 else
1584 epilogue_p = 1;
1585
97e242b0
RH
1586 /* Set the local, input, and output register names. We need to do this
1587 for GNU libc, which creates crti.S/crtn.S by splitting initfini.c in
1588 half. If we use in/loc/out register names, then we get assembler errors
1589 in crtn.S because there is no alloc insn or regstk directive in there. */
1590 if (! TARGET_REG_NAMES)
1591 {
1592 int inputs = current_frame_info.n_input_regs;
1593 int locals = current_frame_info.n_local_regs;
1594 int outputs = current_frame_info.n_output_regs;
1595
1596 for (i = 0; i < inputs; i++)
1597 reg_names[IN_REG (i)] = ia64_reg_numbers[i];
1598 for (i = 0; i < locals; i++)
1599 reg_names[LOC_REG (i)] = ia64_reg_numbers[inputs + i];
1600 for (i = 0; i < outputs; i++)
1601 reg_names[OUT_REG (i)] = ia64_reg_numbers[inputs + locals + i];
1602 }
c65ebc55 1603
97e242b0
RH
1604 /* Set the frame pointer register name. The regnum is logically loc79,
1605 but of course we'll not have allocated that many locals. Rather than
1606 worrying about renumbering the existing rtxs, we adjust the name. */
1607 if (current_frame_info.reg_fp)
1608 {
1609 const char *tmp = reg_names[HARD_FRAME_POINTER_REGNUM];
1610 reg_names[HARD_FRAME_POINTER_REGNUM]
1611 = reg_names[current_frame_info.reg_fp];
1612 reg_names[current_frame_info.reg_fp] = tmp;
1613 }
c65ebc55 1614
97e242b0
RH
1615 /* Fix up the return address placeholder. */
1616 /* ??? We can fail if __builtin_return_address is used, and we didn't
1617 allocate a register in which to save b0. I can't think of a way to
1618 eliminate RETURN_ADDRESS_POINTER_REGNUM to a local register and
1619 then be sure that I got the right one. Further, reload doesn't seem
1620 to care if an eliminable register isn't used, and "eliminates" it
1621 anyway. */
1622 if (regs_ever_live[RETURN_ADDRESS_POINTER_REGNUM]
1623 && current_frame_info.reg_save_b0 != 0)
1624 XINT (return_address_pointer_rtx, 0) = current_frame_info.reg_save_b0;
1625
1626 /* We don't need an alloc instruction if we've used no outputs or locals. */
1627 if (current_frame_info.n_local_regs == 0
1628 && current_frame_info.n_output_regs == 0)
1629 {
1630 /* If there is no alloc, but there are input registers used, then we
1631 need a .regstk directive. */
1632 current_frame_info.need_regstk = (TARGET_REG_NAMES != 0);
1633 ar_pfs_save_reg = NULL_RTX;
1634 }
1635 else
1636 {
1637 current_frame_info.need_regstk = 0;
c65ebc55 1638
97e242b0
RH
1639 if (current_frame_info.reg_save_ar_pfs)
1640 regno = current_frame_info.reg_save_ar_pfs;
1641 else
1642 regno = next_scratch_gr_reg ();
1643 ar_pfs_save_reg = gen_rtx_REG (DImode, regno);
1644
1645 insn = emit_insn (gen_alloc (ar_pfs_save_reg,
1646 GEN_INT (current_frame_info.n_input_regs),
1647 GEN_INT (current_frame_info.n_local_regs),
1648 GEN_INT (current_frame_info.n_output_regs),
1649 GEN_INT (current_frame_info.n_rotate_regs)));
1650 RTX_FRAME_RELATED_P (insn) = (current_frame_info.reg_save_ar_pfs != 0);
1651 }
c65ebc55 1652
97e242b0 1653 /* Set up frame pointer, stack pointer, and spill iterators. */
c65ebc55 1654
97e242b0
RH
1655 n_varargs = current_function_pretend_args_size / UNITS_PER_WORD;
1656 setup_spill_pointers (current_frame_info.n_spilled + n_varargs,
1657 stack_pointer_rtx, 0);
c65ebc55 1658
97e242b0
RH
1659 if (frame_pointer_needed)
1660 {
1661 insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
1662 RTX_FRAME_RELATED_P (insn) = 1;
1663 }
c65ebc55 1664
97e242b0
RH
1665 if (current_frame_info.total_size != 0)
1666 {
1667 rtx frame_size_rtx = GEN_INT (- current_frame_info.total_size);
1668 rtx offset;
c65ebc55 1669
97e242b0
RH
1670 if (CONST_OK_FOR_I (- current_frame_info.total_size))
1671 offset = frame_size_rtx;
1672 else
1673 {
1674 regno = next_scratch_gr_reg ();
1675 offset = gen_rtx_REG (DImode, regno);
1676 emit_move_insn (offset, frame_size_rtx);
1677 }
c65ebc55 1678
97e242b0
RH
1679 insn = emit_insn (gen_adddi3 (stack_pointer_rtx,
1680 stack_pointer_rtx, offset));
c65ebc55 1681
97e242b0
RH
1682 if (! frame_pointer_needed)
1683 {
1684 RTX_FRAME_RELATED_P (insn) = 1;
1685 if (GET_CODE (offset) != CONST_INT)
1686 {
1687 REG_NOTES (insn)
1688 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
1689 gen_rtx_SET (VOIDmode,
1690 stack_pointer_rtx,
1691 gen_rtx_PLUS (DImode,
1692 stack_pointer_rtx,
1693 frame_size_rtx)),
1694 REG_NOTES (insn));
1695 }
1696 }
c65ebc55 1697
97e242b0
RH
1698 /* ??? At this point we must generate a magic insn that appears to
1699 modify the stack pointer, the frame pointer, and all spill
1700 iterators. This would allow the most scheduling freedom. For
1701 now, just hard stop. */
1702 emit_insn (gen_blockage ());
1703 }
c65ebc55 1704
97e242b0
RH
1705 /* Must copy out ar.unat before doing any integer spills. */
1706 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
c65ebc55 1707 {
97e242b0
RH
1708 if (current_frame_info.reg_save_ar_unat)
1709 ar_unat_save_reg
1710 = gen_rtx_REG (DImode, current_frame_info.reg_save_ar_unat);
1711 else
c65ebc55 1712 {
97e242b0
RH
1713 alt_regno = next_scratch_gr_reg ();
1714 ar_unat_save_reg = gen_rtx_REG (DImode, alt_regno);
1715 current_frame_info.gr_used_mask |= 1 << alt_regno;
c65ebc55 1716 }
c65ebc55 1717
97e242b0
RH
1718 reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
1719 insn = emit_move_insn (ar_unat_save_reg, reg);
1720 RTX_FRAME_RELATED_P (insn) = (current_frame_info.reg_save_ar_unat != 0);
1721
1722 /* Even if we're not going to generate an epilogue, we still
1723 need to save the register so that EH works. */
1724 if (! epilogue_p && current_frame_info.reg_save_ar_unat)
1725 emit_insn (gen_rtx_USE (VOIDmode, ar_unat_save_reg));
c65ebc55
JW
1726 }
1727 else
97e242b0
RH
1728 ar_unat_save_reg = NULL_RTX;
1729
1730 /* Spill all varargs registers. Do this before spilling any GR registers,
1731 since we want the UNAT bits for the GR registers to override the UNAT
1732 bits from varargs, which we don't care about. */
c65ebc55 1733
97e242b0
RH
1734 cfa_off = -16;
1735 for (regno = GR_ARG_FIRST + 7; n_varargs > 0; --n_varargs, --regno)
c65ebc55 1736 {
97e242b0 1737 reg = gen_rtx_REG (DImode, regno);
870f9ec0 1738 do_spill (gen_gr_spill, reg, cfa_off += 8, NULL_RTX);
c65ebc55 1739 }
c65ebc55 1740
97e242b0
RH
1741 /* Locate the bottom of the register save area. */
1742 cfa_off = (current_frame_info.spill_cfa_off
1743 + current_frame_info.spill_size
1744 + current_frame_info.extra_spill_size);
c65ebc55 1745
97e242b0
RH
1746 /* Save the predicate register block either in a register or in memory. */
1747 if (TEST_HARD_REG_BIT (current_frame_info.mask, PR_REG (0)))
1748 {
1749 reg = gen_rtx_REG (DImode, PR_REG (0));
1750 if (current_frame_info.reg_save_pr != 0)
1ff5b671 1751 {
97e242b0
RH
1752 alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_pr);
1753 insn = emit_move_insn (alt_reg, reg);
1ff5b671 1754
97e242b0
RH
1755 /* ??? Denote pr spill/fill by a DImode move that modifies all
1756 64 hard registers. */
1ff5b671 1757 RTX_FRAME_RELATED_P (insn) = 1;
97e242b0
RH
1758 REG_NOTES (insn)
1759 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
1760 gen_rtx_SET (VOIDmode, alt_reg, reg),
1761 REG_NOTES (insn));
46327bc5 1762
97e242b0
RH
1763 /* Even if we're not going to generate an epilogue, we still
1764 need to save the register so that EH works. */
1765 if (! epilogue_p)
1766 emit_insn (gen_rtx_USE (VOIDmode, alt_reg));
1ff5b671
JW
1767 }
1768 else
97e242b0
RH
1769 {
1770 alt_regno = next_scratch_gr_reg ();
1771 alt_reg = gen_rtx_REG (DImode, alt_regno);
1772 insn = emit_move_insn (alt_reg, reg);
870f9ec0 1773 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
97e242b0
RH
1774 cfa_off -= 8;
1775 }
c65ebc55
JW
1776 }
1777
97e242b0
RH
1778 /* Handle AR regs in numerical order. All of them get special handling. */
1779 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM)
1780 && current_frame_info.reg_save_ar_unat == 0)
c65ebc55 1781 {
97e242b0 1782 reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
870f9ec0 1783 do_spill (gen_movdi_x, ar_unat_save_reg, cfa_off, reg);
97e242b0 1784 cfa_off -= 8;
c65ebc55 1785 }
97e242b0
RH
1786
1787 /* The alloc insn already copied ar.pfs into a general register. The
1788 only thing we have to do now is copy that register to a stack slot
1789 if we'd not allocated a local register for the job. */
1790 if (current_frame_info.reg_save_ar_pfs == 0
1791 && ! current_function_is_leaf)
c65ebc55 1792 {
97e242b0 1793 reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
870f9ec0 1794 do_spill (gen_movdi_x, ar_pfs_save_reg, cfa_off, reg);
97e242b0
RH
1795 cfa_off -= 8;
1796 }
1797
1798 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_LC_REGNUM))
1799 {
1800 reg = gen_rtx_REG (DImode, AR_LC_REGNUM);
1801 if (current_frame_info.reg_save_ar_lc != 0)
1802 {
1803 alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_ar_lc);
1804 insn = emit_move_insn (alt_reg, reg);
1805 RTX_FRAME_RELATED_P (insn) = 1;
1806
1807 /* Even if we're not going to generate an epilogue, we still
1808 need to save the register so that EH works. */
1809 if (! epilogue_p)
1810 emit_insn (gen_rtx_USE (VOIDmode, alt_reg));
1811 }
c65ebc55
JW
1812 else
1813 {
97e242b0
RH
1814 alt_regno = next_scratch_gr_reg ();
1815 alt_reg = gen_rtx_REG (DImode, alt_regno);
1816 emit_move_insn (alt_reg, reg);
870f9ec0 1817 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
97e242b0
RH
1818 cfa_off -= 8;
1819 }
1820 }
1821
1822 /* We should now be at the base of the gr/br/fr spill area. */
1823 if (cfa_off != (current_frame_info.spill_cfa_off
1824 + current_frame_info.spill_size))
1825 abort ();
1826
1827 /* Spill all general registers. */
1828 for (regno = GR_REG (1); regno <= GR_REG (31); ++regno)
1829 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
1830 {
1831 reg = gen_rtx_REG (DImode, regno);
1832 do_spill (gen_gr_spill, reg, cfa_off, reg);
1833 cfa_off -= 8;
1834 }
1835
1836 /* Handle BR0 specially -- it may be getting stored permanently in
1837 some GR register. */
1838 if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
1839 {
1840 reg = gen_rtx_REG (DImode, BR_REG (0));
1841 if (current_frame_info.reg_save_b0 != 0)
1842 {
1843 alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_b0);
1844 insn = emit_move_insn (alt_reg, reg);
c65ebc55 1845 RTX_FRAME_RELATED_P (insn) = 1;
97e242b0
RH
1846
1847 /* Even if we're not going to generate an epilogue, we still
1848 need to save the register so that EH works. */
1849 if (! epilogue_p)
1850 emit_insn (gen_rtx_USE (VOIDmode, alt_reg));
c65ebc55 1851 }
c65ebc55 1852 else
97e242b0
RH
1853 {
1854 alt_regno = next_scratch_gr_reg ();
1855 alt_reg = gen_rtx_REG (DImode, alt_regno);
1856 emit_move_insn (alt_reg, reg);
870f9ec0 1857 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
97e242b0
RH
1858 cfa_off -= 8;
1859 }
c65ebc55
JW
1860 }
1861
97e242b0
RH
1862 /* Spill the rest of the BR registers. */
1863 for (regno = BR_REG (1); regno <= BR_REG (7); ++regno)
1864 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
1865 {
1866 alt_regno = next_scratch_gr_reg ();
1867 alt_reg = gen_rtx_REG (DImode, alt_regno);
1868 reg = gen_rtx_REG (DImode, regno);
1869 emit_move_insn (alt_reg, reg);
870f9ec0 1870 do_spill (gen_movdi_x, alt_reg, cfa_off, reg);
97e242b0
RH
1871 cfa_off -= 8;
1872 }
1873
1874 /* Align the frame and spill all FR registers. */
1875 for (regno = FR_REG (2); regno <= FR_REG (127); ++regno)
1876 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
1877 {
1878 if (cfa_off & 15)
1879 abort ();
3f622353 1880 reg = gen_rtx_REG (TFmode, regno);
870f9ec0 1881 do_spill (gen_fr_spill_x, reg, cfa_off, reg);
97e242b0
RH
1882 cfa_off -= 16;
1883 }
1884
1885 if (cfa_off != current_frame_info.spill_cfa_off)
1886 abort ();
1887
1888 finish_spill_pointers ();
c65ebc55
JW
1889}
1890
1891/* Called after register allocation to add any instructions needed for the
1892 epilogue. Using a epilogue insn is favored compared to putting all of the
1893 instructions in the FUNCTION_PROLOGUE macro, since it allows the scheduler
1894 to intermix instructions with the saves of the caller saved registers. In
1895 some cases, it might be necessary to emit a barrier instruction as the last
1896 insn to prevent such scheduling. */
1897
1898void
1899ia64_expand_epilogue ()
1900{
97e242b0
RH
1901 rtx insn, reg, alt_reg, ar_unat_save_reg;
1902 int regno, alt_regno, cfa_off;
1903
1904 ia64_compute_frame_size (get_frame_size ());
1905
1906 /* If there is a frame pointer, then we use it instead of the stack
1907 pointer, so that the stack pointer does not need to be valid when
1908 the epilogue starts. See EXIT_IGNORE_STACK. */
1909 if (frame_pointer_needed)
1910 setup_spill_pointers (current_frame_info.n_spilled,
1911 hard_frame_pointer_rtx, 0);
1912 else
1913 setup_spill_pointers (current_frame_info.n_spilled, stack_pointer_rtx,
1914 current_frame_info.total_size);
1915
1916 if (current_frame_info.total_size != 0)
1917 {
1918 /* ??? At this point we must generate a magic insn that appears to
1919 modify the spill iterators and the frame pointer. This would
1920 allow the most scheduling freedom. For now, just hard stop. */
1921 emit_insn (gen_blockage ());
1922 }
1923
1924 /* Locate the bottom of the register save area. */
1925 cfa_off = (current_frame_info.spill_cfa_off
1926 + current_frame_info.spill_size
1927 + current_frame_info.extra_spill_size);
1928
1929 /* Restore the predicate registers. */
1930 if (TEST_HARD_REG_BIT (current_frame_info.mask, PR_REG (0)))
1931 {
1932 if (current_frame_info.reg_save_pr != 0)
1933 alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_pr);
1934 else
1935 {
1936 alt_regno = next_scratch_gr_reg ();
1937 alt_reg = gen_rtx_REG (DImode, alt_regno);
870f9ec0 1938 do_restore (gen_movdi_x, alt_reg, cfa_off);
97e242b0
RH
1939 cfa_off -= 8;
1940 }
1941 reg = gen_rtx_REG (DImode, PR_REG (0));
1942 emit_move_insn (reg, alt_reg);
1943 }
1944
1945 /* Restore the application registers. */
1946
1947 /* Load the saved unat from the stack, but do not restore it until
1948 after the GRs have been restored. */
1949 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
1950 {
1951 if (current_frame_info.reg_save_ar_unat != 0)
1952 ar_unat_save_reg
1953 = gen_rtx_REG (DImode, current_frame_info.reg_save_ar_unat);
1954 else
1955 {
1956 alt_regno = next_scratch_gr_reg ();
1957 ar_unat_save_reg = gen_rtx_REG (DImode, alt_regno);
1958 current_frame_info.gr_used_mask |= 1 << alt_regno;
870f9ec0 1959 do_restore (gen_movdi_x, ar_unat_save_reg, cfa_off);
97e242b0
RH
1960 cfa_off -= 8;
1961 }
1962 }
1963 else
1964 ar_unat_save_reg = NULL_RTX;
1965
1966 if (current_frame_info.reg_save_ar_pfs != 0)
1967 {
1968 alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_ar_pfs);
1969 reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
1970 emit_move_insn (reg, alt_reg);
1971 }
1972 else if (! current_function_is_leaf)
c65ebc55 1973 {
97e242b0
RH
1974 alt_regno = next_scratch_gr_reg ();
1975 alt_reg = gen_rtx_REG (DImode, alt_regno);
870f9ec0 1976 do_restore (gen_movdi_x, alt_reg, cfa_off);
97e242b0
RH
1977 cfa_off -= 8;
1978 reg = gen_rtx_REG (DImode, AR_PFS_REGNUM);
1979 emit_move_insn (reg, alt_reg);
1980 }
1981
1982 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_LC_REGNUM))
1983 {
1984 if (current_frame_info.reg_save_ar_lc != 0)
1985 alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_ar_lc);
1986 else
1987 {
1988 alt_regno = next_scratch_gr_reg ();
1989 alt_reg = gen_rtx_REG (DImode, alt_regno);
870f9ec0 1990 do_restore (gen_movdi_x, alt_reg, cfa_off);
97e242b0
RH
1991 cfa_off -= 8;
1992 }
1993 reg = gen_rtx_REG (DImode, AR_LC_REGNUM);
1994 emit_move_insn (reg, alt_reg);
1995 }
1996
1997 /* We should now be at the base of the gr/br/fr spill area. */
1998 if (cfa_off != (current_frame_info.spill_cfa_off
1999 + current_frame_info.spill_size))
2000 abort ();
2001
2002 /* Restore all general registers. */
2003 for (regno = GR_REG (1); regno <= GR_REG (31); ++regno)
2004 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
0c96007e 2005 {
97e242b0
RH
2006 reg = gen_rtx_REG (DImode, regno);
2007 do_restore (gen_gr_restore, reg, cfa_off);
2008 cfa_off -= 8;
0c96007e 2009 }
97e242b0
RH
2010
2011 /* Restore the branch registers. Handle B0 specially, as it may
2012 have gotten stored in some GR register. */
2013 if (TEST_HARD_REG_BIT (current_frame_info.mask, BR_REG (0)))
2014 {
2015 if (current_frame_info.reg_save_b0 != 0)
2016 alt_reg = gen_rtx_REG (DImode, current_frame_info.reg_save_b0);
2017 else
2018 {
2019 alt_regno = next_scratch_gr_reg ();
2020 alt_reg = gen_rtx_REG (DImode, alt_regno);
870f9ec0 2021 do_restore (gen_movdi_x, alt_reg, cfa_off);
97e242b0
RH
2022 cfa_off -= 8;
2023 }
2024 reg = gen_rtx_REG (DImode, BR_REG (0));
2025 emit_move_insn (reg, alt_reg);
2026 }
2027
2028 for (regno = BR_REG (1); regno <= BR_REG (7); ++regno)
2029 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
0c96007e 2030 {
97e242b0
RH
2031 alt_regno = next_scratch_gr_reg ();
2032 alt_reg = gen_rtx_REG (DImode, alt_regno);
870f9ec0 2033 do_restore (gen_movdi_x, alt_reg, cfa_off);
97e242b0
RH
2034 cfa_off -= 8;
2035 reg = gen_rtx_REG (DImode, regno);
2036 emit_move_insn (reg, alt_reg);
2037 }
c65ebc55 2038
97e242b0
RH
2039 /* Restore floating point registers. */
2040 for (regno = FR_REG (2); regno <= FR_REG (127); ++regno)
2041 if (TEST_HARD_REG_BIT (current_frame_info.mask, regno))
2042 {
2043 if (cfa_off & 15)
2044 abort ();
3f622353 2045 reg = gen_rtx_REG (TFmode, regno);
870f9ec0 2046 do_restore (gen_fr_restore_x, reg, cfa_off);
97e242b0 2047 cfa_off -= 16;
0c96007e 2048 }
97e242b0
RH
2049
2050 /* Restore ar.unat for real. */
2051 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM))
2052 {
2053 reg = gen_rtx_REG (DImode, AR_UNAT_REGNUM);
2054 emit_move_insn (reg, ar_unat_save_reg);
c65ebc55
JW
2055 }
2056
97e242b0
RH
2057 if (cfa_off != current_frame_info.spill_cfa_off)
2058 abort ();
2059
2060 finish_spill_pointers ();
c65ebc55 2061
97e242b0
RH
2062 if (current_frame_info.total_size || cfun->machine->ia64_eh_epilogue_sp)
2063 {
2064 /* ??? At this point we must generate a magic insn that appears to
2065 modify the spill iterators, the stack pointer, and the frame
2066 pointer. This would allow the most scheduling freedom. For now,
2067 just hard stop. */
2068 emit_insn (gen_blockage ());
2069 }
c65ebc55 2070
97e242b0
RH
2071 if (cfun->machine->ia64_eh_epilogue_sp)
2072 emit_move_insn (stack_pointer_rtx, cfun->machine->ia64_eh_epilogue_sp);
2073 else if (frame_pointer_needed)
2074 {
2075 insn = emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
2076 RTX_FRAME_RELATED_P (insn) = 1;
2077 }
2078 else if (current_frame_info.total_size)
0c96007e 2079 {
97e242b0
RH
2080 rtx offset, frame_size_rtx;
2081
2082 frame_size_rtx = GEN_INT (current_frame_info.total_size);
2083 if (CONST_OK_FOR_I (current_frame_info.total_size))
2084 offset = frame_size_rtx;
2085 else
2086 {
2087 regno = next_scratch_gr_reg ();
2088 offset = gen_rtx_REG (DImode, regno);
2089 emit_move_insn (offset, frame_size_rtx);
2090 }
2091
2092 insn = emit_insn (gen_adddi3 (stack_pointer_rtx, stack_pointer_rtx,
2093 offset));
2094
2095 RTX_FRAME_RELATED_P (insn) = 1;
2096 if (GET_CODE (offset) != CONST_INT)
2097 {
2098 REG_NOTES (insn)
2099 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
2100 gen_rtx_SET (VOIDmode,
2101 stack_pointer_rtx,
2102 gen_rtx_PLUS (DImode,
2103 stack_pointer_rtx,
2104 frame_size_rtx)),
2105 REG_NOTES (insn));
2106 }
0c96007e 2107 }
97e242b0
RH
2108
2109 if (cfun->machine->ia64_eh_epilogue_bsp)
2110 emit_insn (gen_set_bsp (cfun->machine->ia64_eh_epilogue_bsp));
2111
c65ebc55
JW
2112 emit_jump_insn (gen_return_internal (gen_rtx_REG (DImode, BR_REG (0))));
2113}
2114
97e242b0
RH
2115/* Return 1 if br.ret can do all the work required to return from a
2116 function. */
2117
2118int
2119ia64_direct_return ()
2120{
2121 if (reload_completed && ! frame_pointer_needed)
2122 {
2123 ia64_compute_frame_size (get_frame_size ());
2124
2125 return (current_frame_info.total_size == 0
2126 && current_frame_info.n_spilled == 0
2127 && current_frame_info.reg_save_b0 == 0
2128 && current_frame_info.reg_save_pr == 0
2129 && current_frame_info.reg_save_ar_pfs == 0
2130 && current_frame_info.reg_save_ar_unat == 0
2131 && current_frame_info.reg_save_ar_lc == 0);
2132 }
2133 return 0;
2134}
2135
c65ebc55
JW
2136/* Emit the function prologue. */
2137
2138void
2139ia64_function_prologue (file, size)
2140 FILE *file;
fd7c34b0 2141 int size ATTRIBUTE_UNUSED;
c65ebc55 2142{
97e242b0
RH
2143 int mask, grsave, grsave_prev;
2144
2145 if (current_frame_info.need_regstk)
2146 fprintf (file, "\t.regstk %d, %d, %d, %d\n",
2147 current_frame_info.n_input_regs,
2148 current_frame_info.n_local_regs,
2149 current_frame_info.n_output_regs,
2150 current_frame_info.n_rotate_regs);
c65ebc55 2151
0c96007e
AM
2152 if (!flag_unwind_tables && (!flag_exceptions || exceptions_via_longjmp))
2153 return;
2154
97e242b0 2155 /* Emit the .prologue directive. */
809d4ef1 2156
97e242b0
RH
2157 mask = 0;
2158 grsave = grsave_prev = 0;
2159 if (current_frame_info.reg_save_b0 != 0)
0c96007e 2160 {
97e242b0
RH
2161 mask |= 8;
2162 grsave = grsave_prev = current_frame_info.reg_save_b0;
2163 }
2164 if (current_frame_info.reg_save_ar_pfs != 0
2165 && (grsave_prev == 0
2166 || current_frame_info.reg_save_ar_pfs == grsave_prev + 1))
2167 {
2168 mask |= 4;
2169 if (grsave_prev == 0)
2170 grsave = current_frame_info.reg_save_ar_pfs;
2171 grsave_prev = current_frame_info.reg_save_ar_pfs;
0c96007e 2172 }
97e242b0
RH
2173 if (current_frame_info.reg_fp != 0
2174 && (grsave_prev == 0
2175 || current_frame_info.reg_fp == grsave_prev + 1))
2176 {
2177 mask |= 2;
2178 if (grsave_prev == 0)
2179 grsave = HARD_FRAME_POINTER_REGNUM;
2180 grsave_prev = current_frame_info.reg_fp;
2181 }
2182 if (current_frame_info.reg_save_pr != 0
2183 && (grsave_prev == 0
2184 || current_frame_info.reg_save_pr == grsave_prev + 1))
2185 {
2186 mask |= 1;
2187 if (grsave_prev == 0)
2188 grsave = current_frame_info.reg_save_pr;
2189 }
2190
2191 if (mask)
2192 fprintf (file, "\t.prologue %d, %d\n", mask,
2193 ia64_dbx_register_number (grsave));
2194 else
2195 fputs ("\t.prologue\n", file);
2196
2197 /* Emit a .spill directive, if necessary, to relocate the base of
2198 the register spill area. */
2199 if (current_frame_info.spill_cfa_off != -16)
2200 fprintf (file, "\t.spill %ld\n",
2201 (long) (current_frame_info.spill_cfa_off
2202 + current_frame_info.spill_size));
c65ebc55
JW
2203}
2204
0186257f
JW
2205/* Emit the .body directive at the scheduled end of the prologue. */
2206
2207void
2208ia64_output_end_prologue (file)
2209 FILE *file;
2210{
2211 if (!flag_unwind_tables && (!flag_exceptions || exceptions_via_longjmp))
2212 return;
2213
2214 fputs ("\t.body\n", file);
2215}
2216
c65ebc55
JW
2217/* Emit the function epilogue. */
2218
2219void
2220ia64_function_epilogue (file, size)
fd7c34b0
RH
2221 FILE *file ATTRIBUTE_UNUSED;
2222 int size ATTRIBUTE_UNUSED;
c65ebc55 2223{
97e242b0
RH
2224 /* Reset from the function's potential modifications. */
2225 XINT (return_address_pointer_rtx, 0) = RETURN_ADDRESS_POINTER_REGNUM;
c65ebc55 2226
97e242b0
RH
2227 if (current_frame_info.reg_fp)
2228 {
2229 const char *tmp = reg_names[HARD_FRAME_POINTER_REGNUM];
2230 reg_names[HARD_FRAME_POINTER_REGNUM]
2231 = reg_names[current_frame_info.reg_fp];
2232 reg_names[current_frame_info.reg_fp] = tmp;
2233 }
2234 if (! TARGET_REG_NAMES)
2235 {
2236 int i;
2237
2238 for (i = 0; i < current_frame_info.n_input_regs; i++)
2239 reg_names[IN_REG (i)] = ia64_input_reg_names[i];
2240 for (i = 0; i < current_frame_info.n_local_regs; i++)
2241 reg_names[LOC_REG (i)] = ia64_local_reg_names[i];
2242 for (i = 0; i < current_frame_info.n_output_regs; i++)
2243 reg_names[OUT_REG (i)] = ia64_output_reg_names[i];
2244 }
2245 current_frame_info.initialized = 0;
2246}
c65ebc55
JW
2247
2248int
97e242b0
RH
2249ia64_dbx_register_number (regno)
2250 int regno;
c65ebc55 2251{
97e242b0
RH
2252 /* In ia64_expand_prologue we quite literally renamed the frame pointer
2253 from its home at loc79 to something inside the register frame. We
2254 must perform the same renumbering here for the debug info. */
2255 if (current_frame_info.reg_fp)
2256 {
2257 if (regno == HARD_FRAME_POINTER_REGNUM)
2258 regno = current_frame_info.reg_fp;
2259 else if (regno == current_frame_info.reg_fp)
2260 regno = HARD_FRAME_POINTER_REGNUM;
2261 }
2262
2263 if (IN_REGNO_P (regno))
2264 return 32 + regno - IN_REG (0);
2265 else if (LOC_REGNO_P (regno))
2266 return 32 + current_frame_info.n_input_regs + regno - LOC_REG (0);
2267 else if (OUT_REGNO_P (regno))
2268 return (32 + current_frame_info.n_input_regs
2269 + current_frame_info.n_local_regs + regno - OUT_REG (0));
2270 else
2271 return regno;
c65ebc55
JW
2272}
2273
97e242b0
RH
2274void
2275ia64_initialize_trampoline (addr, fnaddr, static_chain)
2276 rtx addr, fnaddr, static_chain;
2277{
2278 rtx addr_reg, eight = GEN_INT (8);
2279
2280 /* Load up our iterator. */
2281 addr_reg = gen_reg_rtx (Pmode);
2282 emit_move_insn (addr_reg, addr);
2283
2284 /* The first two words are the fake descriptor:
2285 __ia64_trampoline, ADDR+16. */
2286 emit_move_insn (gen_rtx_MEM (Pmode, addr_reg),
2287 gen_rtx_SYMBOL_REF (Pmode, "__ia64_trampoline"));
2288 emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
2289
2290 emit_move_insn (gen_rtx_MEM (Pmode, addr_reg),
2291 copy_to_reg (plus_constant (addr, 16)));
2292 emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
2293
2294 /* The third word is the target descriptor. */
2295 emit_move_insn (gen_rtx_MEM (Pmode, addr_reg), fnaddr);
2296 emit_insn (gen_adddi3 (addr_reg, addr_reg, eight));
2297
2298 /* The fourth word is the static chain. */
2299 emit_move_insn (gen_rtx_MEM (Pmode, addr_reg), static_chain);
2300}
c65ebc55
JW
2301\f
2302/* Do any needed setup for a variadic function. CUM has not been updated
97e242b0
RH
2303 for the last named argument which has type TYPE and mode MODE.
2304
2305 We generate the actual spill instructions during prologue generation. */
2306
c65ebc55
JW
2307void
2308ia64_setup_incoming_varargs (cum, int_mode, type, pretend_size, second_time)
2309 CUMULATIVE_ARGS cum;
fd7c34b0
RH
2310 int int_mode ATTRIBUTE_UNUSED;
2311 tree type ATTRIBUTE_UNUSED;
c65ebc55 2312 int * pretend_size;
97e242b0 2313 int second_time ATTRIBUTE_UNUSED;
c65ebc55
JW
2314{
2315 /* If this is a stdarg function, then don't save the current argument. */
2316 int offset = ! current_function_varargs;
2317
2318 if (cum.words < MAX_ARGUMENT_SLOTS)
97e242b0
RH
2319 *pretend_size = ((MAX_ARGUMENT_SLOTS - cum.words - offset)
2320 * UNITS_PER_WORD);
c65ebc55
JW
2321}
2322
2323/* Check whether TYPE is a homogeneous floating point aggregate. If
2324 it is, return the mode of the floating point type that appears
2325 in all leafs. If it is not, return VOIDmode.
2326
2327 An aggregate is a homogeneous floating point aggregate is if all
2328 fields/elements in it have the same floating point type (e.g,
2329 SFmode). 128-bit quad-precision floats are excluded. */
2330
2331static enum machine_mode
2332hfa_element_mode (type, nested)
2333 tree type;
2334 int nested;
2335{
2336 enum machine_mode element_mode = VOIDmode;
2337 enum machine_mode mode;
2338 enum tree_code code = TREE_CODE (type);
2339 int know_element_mode = 0;
2340 tree t;
2341
2342 switch (code)
2343 {
2344 case VOID_TYPE: case INTEGER_TYPE: case ENUMERAL_TYPE:
2345 case BOOLEAN_TYPE: case CHAR_TYPE: case POINTER_TYPE:
2346 case OFFSET_TYPE: case REFERENCE_TYPE: case METHOD_TYPE:
2347 case FILE_TYPE: case SET_TYPE: case LANG_TYPE:
2348 case FUNCTION_TYPE:
2349 return VOIDmode;
2350
2351 /* Fortran complex types are supposed to be HFAs, so we need to handle
2352 gcc's COMPLEX_TYPEs as HFAs. We need to exclude the integral complex
2353 types though. */
2354 case COMPLEX_TYPE:
2355 if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_COMPLEX_FLOAT)
2356 return mode_for_size (GET_MODE_UNIT_SIZE (TYPE_MODE (type))
2357 * BITS_PER_UNIT, MODE_FLOAT, 0);
2358 else
2359 return VOIDmode;
2360
2361 case REAL_TYPE:
2362 /* We want to return VOIDmode for raw REAL_TYPEs, but the actual
2363 mode if this is contained within an aggregate. */
2364 if (nested)
2365 return TYPE_MODE (type);
2366 else
2367 return VOIDmode;
2368
2369 case ARRAY_TYPE:
2370 return TYPE_MODE (TREE_TYPE (type));
2371
2372 case RECORD_TYPE:
2373 case UNION_TYPE:
2374 case QUAL_UNION_TYPE:
2375 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
2376 {
2377 if (TREE_CODE (t) != FIELD_DECL)
2378 continue;
2379
2380 mode = hfa_element_mode (TREE_TYPE (t), 1);
2381 if (know_element_mode)
2382 {
2383 if (mode != element_mode)
2384 return VOIDmode;
2385 }
2386 else if (GET_MODE_CLASS (mode) != MODE_FLOAT)
2387 return VOIDmode;
2388 else
2389 {
2390 know_element_mode = 1;
2391 element_mode = mode;
2392 }
2393 }
2394 return element_mode;
2395
2396 default:
2397 /* If we reach here, we probably have some front-end specific type
2398 that the backend doesn't know about. This can happen via the
2399 aggregate_value_p call in init_function_start. All we can do is
2400 ignore unknown tree types. */
2401 return VOIDmode;
2402 }
2403
2404 return VOIDmode;
2405}
2406
2407/* Return rtx for register where argument is passed, or zero if it is passed
2408 on the stack. */
2409
2410/* ??? 128-bit quad-precision floats are always passed in general
2411 registers. */
2412
2413rtx
2414ia64_function_arg (cum, mode, type, named, incoming)
2415 CUMULATIVE_ARGS *cum;
2416 enum machine_mode mode;
2417 tree type;
2418 int named;
2419 int incoming;
2420{
2421 int basereg = (incoming ? GR_ARG_FIRST : AR_ARG_FIRST);
2422 int words = (((mode == BLKmode ? int_size_in_bytes (type)
2423 : GET_MODE_SIZE (mode)) + UNITS_PER_WORD - 1)
2424 / UNITS_PER_WORD);
2425 int offset = 0;
2426 enum machine_mode hfa_mode = VOIDmode;
2427
f9f45ccb
JW
2428 /* Integer and float arguments larger than 8 bytes start at the next even
2429 boundary. Aggregates larger than 8 bytes start at the next even boundary
7d17b34d
JW
2430 if the aggregate has 16 byte alignment. Net effect is that types with
2431 alignment greater than 8 start at the next even boundary. */
f9f45ccb
JW
2432 /* ??? The ABI does not specify how to handle aggregates with alignment from
2433 9 to 15 bytes, or greater than 16. We handle them all as if they had
2434 16 byte alignment. Such aggregates can occur only if gcc extensions are
2435 used. */
7d17b34d
JW
2436 if ((type ? (TYPE_ALIGN (type) > 8 * BITS_PER_UNIT)
2437 : (words > 1))
2438 && (cum->words & 1))
c65ebc55
JW
2439 offset = 1;
2440
2441 /* If all argument slots are used, then it must go on the stack. */
2442 if (cum->words + offset >= MAX_ARGUMENT_SLOTS)
2443 return 0;
2444
2445 /* Check for and handle homogeneous FP aggregates. */
2446 if (type)
2447 hfa_mode = hfa_element_mode (type, 0);
2448
2449 /* Unnamed prototyped hfas are passed as usual. Named prototyped hfas
2450 and unprototyped hfas are passed specially. */
2451 if (hfa_mode != VOIDmode && (! cum->prototype || named))
2452 {
2453 rtx loc[16];
2454 int i = 0;
2455 int fp_regs = cum->fp_regs;
2456 int int_regs = cum->words + offset;
2457 int hfa_size = GET_MODE_SIZE (hfa_mode);
2458 int byte_size;
2459 int args_byte_size;
2460
2461 /* If prototyped, pass it in FR regs then GR regs.
2462 If not prototyped, pass it in both FR and GR regs.
2463
2464 If this is an SFmode aggregate, then it is possible to run out of
2465 FR regs while GR regs are still left. In that case, we pass the
2466 remaining part in the GR regs. */
2467
2468 /* Fill the FP regs. We do this always. We stop if we reach the end
2469 of the argument, the last FP register, or the last argument slot. */
2470
2471 byte_size = ((mode == BLKmode)
2472 ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
2473 args_byte_size = int_regs * UNITS_PER_WORD;
2474 offset = 0;
2475 for (; (offset < byte_size && fp_regs < MAX_ARGUMENT_SLOTS
2476 && args_byte_size < (MAX_ARGUMENT_SLOTS * UNITS_PER_WORD)); i++)
2477 {
2478 loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
2479 gen_rtx_REG (hfa_mode, (FR_ARG_FIRST
2480 + fp_regs)),
2481 GEN_INT (offset));
c65ebc55
JW
2482 offset += hfa_size;
2483 args_byte_size += hfa_size;
2484 fp_regs++;
2485 }
2486
2487 /* If no prototype, then the whole thing must go in GR regs. */
2488 if (! cum->prototype)
2489 offset = 0;
2490 /* If this is an SFmode aggregate, then we might have some left over
2491 that needs to go in GR regs. */
2492 else if (byte_size != offset)
2493 int_regs += offset / UNITS_PER_WORD;
2494
2495 /* Fill in the GR regs. We must use DImode here, not the hfa mode. */
2496
2497 for (; offset < byte_size && int_regs < MAX_ARGUMENT_SLOTS; i++)
2498 {
2499 enum machine_mode gr_mode = DImode;
2500
2501 /* If we have an odd 4 byte hunk because we ran out of FR regs,
2502 then this goes in a GR reg left adjusted/little endian, right
2503 adjusted/big endian. */
2504 /* ??? Currently this is handled wrong, because 4-byte hunks are
2505 always right adjusted/little endian. */
2506 if (offset & 0x4)
2507 gr_mode = SImode;
2508 /* If we have an even 4 byte hunk because the aggregate is a
2509 multiple of 4 bytes in size, then this goes in a GR reg right
2510 adjusted/little endian. */
2511 else if (byte_size - offset == 4)
2512 gr_mode = SImode;
2513
2514 loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
2515 gen_rtx_REG (gr_mode, (basereg
2516 + int_regs)),
2517 GEN_INT (offset));
2518 offset += GET_MODE_SIZE (gr_mode);
2519 int_regs++;
2520 }
2521
2522 /* If we ended up using just one location, just return that one loc. */
2523 if (i == 1)
2524 return XEXP (loc[0], 0);
2525 else
2526 return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
2527 }
2528
2529 /* Integral and aggregates go in general registers. If we have run out of
2530 FR registers, then FP values must also go in general registers. This can
2531 happen when we have a SFmode HFA. */
2532 else if (! FLOAT_MODE_P (mode) || cum->fp_regs == MAX_ARGUMENT_SLOTS)
2533 return gen_rtx_REG (mode, basereg + cum->words + offset);
2534
2535 /* If there is a prototype, then FP values go in a FR register when
2536 named, and in a GR registeer when unnamed. */
2537 else if (cum->prototype)
2538 {
2539 if (! named)
2540 return gen_rtx_REG (mode, basereg + cum->words + offset);
2541 else
2542 return gen_rtx_REG (mode, FR_ARG_FIRST + cum->fp_regs);
2543 }
2544 /* If there is no prototype, then FP values go in both FR and GR
2545 registers. */
2546 else
2547 {
2548 rtx fp_reg = gen_rtx_EXPR_LIST (VOIDmode,
2549 gen_rtx_REG (mode, (FR_ARG_FIRST
2550 + cum->fp_regs)),
2551 const0_rtx);
2552 rtx gr_reg = gen_rtx_EXPR_LIST (VOIDmode,
2553 gen_rtx_REG (mode,
2554 (basereg + cum->words
2555 + offset)),
2556 const0_rtx);
809d4ef1 2557
c65ebc55
JW
2558 return gen_rtx_PARALLEL (mode, gen_rtvec (2, fp_reg, gr_reg));
2559 }
2560}
2561
2562/* Return number of words, at the beginning of the argument, that must be
2563 put in registers. 0 is the argument is entirely in registers or entirely
2564 in memory. */
2565
2566int
2567ia64_function_arg_partial_nregs (cum, mode, type, named)
2568 CUMULATIVE_ARGS *cum;
2569 enum machine_mode mode;
2570 tree type;
fd7c34b0 2571 int named ATTRIBUTE_UNUSED;
c65ebc55
JW
2572{
2573 int words = (((mode == BLKmode ? int_size_in_bytes (type)
2574 : GET_MODE_SIZE (mode)) + UNITS_PER_WORD - 1)
2575 / UNITS_PER_WORD);
2576 int offset = 0;
2577
7d17b34d
JW
2578 /* Arguments with alignment larger than 8 bytes start at the next even
2579 boundary. */
2580 if ((type ? (TYPE_ALIGN (type) > 8 * BITS_PER_UNIT)
2581 : (words > 1))
2582 && (cum->words & 1))
c65ebc55
JW
2583 offset = 1;
2584
2585 /* If all argument slots are used, then it must go on the stack. */
2586 if (cum->words + offset >= MAX_ARGUMENT_SLOTS)
2587 return 0;
2588
2589 /* It doesn't matter whether the argument goes in FR or GR regs. If
2590 it fits within the 8 argument slots, then it goes entirely in
2591 registers. If it extends past the last argument slot, then the rest
2592 goes on the stack. */
2593
2594 if (words + cum->words + offset <= MAX_ARGUMENT_SLOTS)
2595 return 0;
2596
2597 return MAX_ARGUMENT_SLOTS - cum->words - offset;
2598}
2599
2600/* Update CUM to point after this argument. This is patterned after
2601 ia64_function_arg. */
2602
2603void
2604ia64_function_arg_advance (cum, mode, type, named)
2605 CUMULATIVE_ARGS *cum;
2606 enum machine_mode mode;
2607 tree type;
2608 int named;
2609{
2610 int words = (((mode == BLKmode ? int_size_in_bytes (type)
2611 : GET_MODE_SIZE (mode)) + UNITS_PER_WORD - 1)
2612 / UNITS_PER_WORD);
2613 int offset = 0;
2614 enum machine_mode hfa_mode = VOIDmode;
2615
2616 /* If all arg slots are already full, then there is nothing to do. */
2617 if (cum->words >= MAX_ARGUMENT_SLOTS)
2618 return;
2619
7d17b34d
JW
2620 /* Arguments with alignment larger than 8 bytes start at the next even
2621 boundary. */
2622 if ((type ? (TYPE_ALIGN (type) > 8 * BITS_PER_UNIT)
2623 : (words > 1))
2624 && (cum->words & 1))
c65ebc55
JW
2625 offset = 1;
2626
2627 cum->words += words + offset;
2628
2629 /* Check for and handle homogeneous FP aggregates. */
2630 if (type)
2631 hfa_mode = hfa_element_mode (type, 0);
2632
2633 /* Unnamed prototyped hfas are passed as usual. Named prototyped hfas
2634 and unprototyped hfas are passed specially. */
2635 if (hfa_mode != VOIDmode && (! cum->prototype || named))
2636 {
2637 int fp_regs = cum->fp_regs;
2638 /* This is the original value of cum->words + offset. */
2639 int int_regs = cum->words - words;
2640 int hfa_size = GET_MODE_SIZE (hfa_mode);
2641 int byte_size;
2642 int args_byte_size;
2643
2644 /* If prototyped, pass it in FR regs then GR regs.
2645 If not prototyped, pass it in both FR and GR regs.
2646
2647 If this is an SFmode aggregate, then it is possible to run out of
2648 FR regs while GR regs are still left. In that case, we pass the
2649 remaining part in the GR regs. */
2650
2651 /* Fill the FP regs. We do this always. We stop if we reach the end
2652 of the argument, the last FP register, or the last argument slot. */
2653
2654 byte_size = ((mode == BLKmode)
2655 ? int_size_in_bytes (type) : GET_MODE_SIZE (mode));
2656 args_byte_size = int_regs * UNITS_PER_WORD;
2657 offset = 0;
2658 for (; (offset < byte_size && fp_regs < MAX_ARGUMENT_SLOTS
2659 && args_byte_size < (MAX_ARGUMENT_SLOTS * UNITS_PER_WORD));)
2660 {
c65ebc55
JW
2661 offset += hfa_size;
2662 args_byte_size += hfa_size;
2663 fp_regs++;
2664 }
2665
2666 cum->fp_regs = fp_regs;
2667 }
2668
2669 /* Integral and aggregates go in general registers. If we have run out of
2670 FR registers, then FP values must also go in general registers. This can
2671 happen when we have a SFmode HFA. */
2672 else if (! FLOAT_MODE_P (mode) || cum->fp_regs == MAX_ARGUMENT_SLOTS)
2673 return;
2674
2675 /* If there is a prototype, then FP values go in a FR register when
2676 named, and in a GR registeer when unnamed. */
2677 else if (cum->prototype)
2678 {
2679 if (! named)
2680 return;
2681 else
2682 /* ??? Complex types should not reach here. */
2683 cum->fp_regs += (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT ? 2 : 1);
2684 }
2685 /* If there is no prototype, then FP values go in both FR and GR
2686 registers. */
2687 else
2688 /* ??? Complex types should not reach here. */
2689 cum->fp_regs += (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT ? 2 : 1);
2690
2691 return;
2692}
2693\f
2694/* Implement va_start. */
2695
2696void
2697ia64_va_start (stdarg_p, valist, nextarg)
2698 int stdarg_p;
2699 tree valist;
2700 rtx nextarg;
2701{
2702 int arg_words;
2703 int ofs;
2704
2705 arg_words = current_function_args_info.words;
2706
2707 if (stdarg_p)
2708 ofs = 0;
2709 else
2710 ofs = (arg_words >= MAX_ARGUMENT_SLOTS ? -UNITS_PER_WORD : 0);
2711
2712 nextarg = plus_constant (nextarg, ofs);
2713 std_expand_builtin_va_start (1, valist, nextarg);
2714}
2715
2716/* Implement va_arg. */
2717
2718rtx
2719ia64_va_arg (valist, type)
2720 tree valist, type;
2721{
c65ebc55
JW
2722 tree t;
2723
7d17b34d
JW
2724 /* Arguments with alignment larger than 8 bytes start at the next even
2725 boundary. */
2726 if (TYPE_ALIGN (type) > 8 * BITS_PER_UNIT)
c65ebc55
JW
2727 {
2728 t = build (PLUS_EXPR, TREE_TYPE (valist), valist,
2729 build_int_2 (2 * UNITS_PER_WORD - 1, 0));
809d4ef1 2730 t = build (BIT_AND_EXPR, TREE_TYPE (t), t,
c65ebc55
JW
2731 build_int_2 (-2 * UNITS_PER_WORD, -1));
2732 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
2733 TREE_SIDE_EFFECTS (t) = 1;
2734 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2735 }
2736
2737 return std_expand_builtin_va_arg (valist, type);
2738}
2739\f
2740/* Return 1 if function return value returned in memory. Return 0 if it is
2741 in a register. */
2742
2743int
2744ia64_return_in_memory (valtype)
2745 tree valtype;
2746{
2747 enum machine_mode mode;
2748 enum machine_mode hfa_mode;
2749 int byte_size;
2750
2751 mode = TYPE_MODE (valtype);
2752 byte_size = ((mode == BLKmode)
2753 ? int_size_in_bytes (valtype) : GET_MODE_SIZE (mode));
2754
2755 /* Hfa's with up to 8 elements are returned in the FP argument registers. */
2756
2757 hfa_mode = hfa_element_mode (valtype, 0);
2758 if (hfa_mode != VOIDmode)
2759 {
2760 int hfa_size = GET_MODE_SIZE (hfa_mode);
2761
c65ebc55
JW
2762 if (byte_size / hfa_size > MAX_ARGUMENT_SLOTS)
2763 return 1;
2764 else
2765 return 0;
2766 }
2767
2768 else if (byte_size > UNITS_PER_WORD * MAX_INT_RETURN_SLOTS)
2769 return 1;
2770 else
2771 return 0;
2772}
2773
2774/* Return rtx for register that holds the function return value. */
2775
2776rtx
2777ia64_function_value (valtype, func)
2778 tree valtype;
fd7c34b0 2779 tree func ATTRIBUTE_UNUSED;
c65ebc55
JW
2780{
2781 enum machine_mode mode;
2782 enum machine_mode hfa_mode;
2783
2784 mode = TYPE_MODE (valtype);
2785 hfa_mode = hfa_element_mode (valtype, 0);
2786
2787 if (hfa_mode != VOIDmode)
2788 {
2789 rtx loc[8];
2790 int i;
2791 int hfa_size;
2792 int byte_size;
2793 int offset;
2794
2795 hfa_size = GET_MODE_SIZE (hfa_mode);
2796 byte_size = ((mode == BLKmode)
2797 ? int_size_in_bytes (valtype) : GET_MODE_SIZE (mode));
2798 offset = 0;
2799 for (i = 0; offset < byte_size; i++)
2800 {
2801 loc[i] = gen_rtx_EXPR_LIST (VOIDmode,
2802 gen_rtx_REG (hfa_mode, FR_ARG_FIRST + i),
2803 GEN_INT (offset));
c65ebc55
JW
2804 offset += hfa_size;
2805 }
2806
2807 if (i == 1)
2808 return XEXP (loc[0], 0);
2809 else
2810 return gen_rtx_PARALLEL (mode, gen_rtvec_v (i, loc));
2811 }
2812 else if (FLOAT_TYPE_P (valtype))
2813 return gen_rtx_REG (mode, FR_ARG_FIRST);
2814 else
2815 return gen_rtx_REG (mode, GR_RET_FIRST);
2816}
2817
2818/* Print a memory address as an operand to reference that memory location. */
2819
2820/* ??? Do we need this? It gets used only for 'a' operands. We could perhaps
2821 also call this from ia64_print_operand for memory addresses. */
2822
2823void
2824ia64_print_operand_address (stream, address)
fd7c34b0
RH
2825 FILE * stream ATTRIBUTE_UNUSED;
2826 rtx address ATTRIBUTE_UNUSED;
c65ebc55
JW
2827{
2828}
2829
2830/* Print an operand to a assembler instruction.
2831 B Work arounds for hardware bugs.
2832 C Swap and print a comparison operator.
2833 D Print an FP comparison operator.
2834 E Print 32 - constant, for SImode shifts as extract.
2835 F A floating point constant 0.0 emitted as f0, or 1.0 emitted as f1, or
2836 a floating point register emitted normally.
2837 I Invert a predicate register by adding 1.
e5bde68a 2838 J Select the proper predicate register for a condition.
6b6c1201 2839 j Select the inverse predicate register for a condition.
c65ebc55
JW
2840 O Append .acq for volatile load.
2841 P Postincrement of a MEM.
2842 Q Append .rel for volatile store.
2843 S Shift amount for shladd instruction.
2844 T Print an 8-bit sign extended number (K) as a 32-bit unsigned number
2845 for Intel assembler.
2846 U Print an 8-bit sign extended number (K) as a 64-bit unsigned number
2847 for Intel assembler.
2848 r Print register name, or constant 0 as r0. HP compatibility for
2849 Linux kernel. */
2850void
2851ia64_print_operand (file, x, code)
2852 FILE * file;
2853 rtx x;
2854 int code;
2855{
e57b9d65
RH
2856 const char *str;
2857
c65ebc55
JW
2858 switch (code)
2859 {
c65ebc55
JW
2860 case 0:
2861 /* Handled below. */
2862 break;
809d4ef1 2863
c65ebc55
JW
2864 case 'B':
2865 if (TARGET_A_STEP)
2866 fputs (" ;; nop 0 ;; nop 0 ;;", file);
2867 return;
2868
2869 case 'C':
2870 {
2871 enum rtx_code c = swap_condition (GET_CODE (x));
2872 fputs (GET_RTX_NAME (c), file);
2873 return;
2874 }
2875
2876 case 'D':
e57b9d65
RH
2877 switch (GET_CODE (x))
2878 {
2879 case NE:
2880 str = "neq";
2881 break;
2882 case UNORDERED:
2883 str = "unord";
2884 break;
2885 case ORDERED:
2886 str = "ord";
2887 break;
2888 default:
2889 str = GET_RTX_NAME (GET_CODE (x));
2890 break;
2891 }
2892 fputs (str, file);
c65ebc55
JW
2893 return;
2894
2895 case 'E':
2896 fprintf (file, HOST_WIDE_INT_PRINT_DEC, 32 - INTVAL (x));
2897 return;
2898
2899 case 'F':
2900 if (x == CONST0_RTX (GET_MODE (x)))
e57b9d65 2901 str = reg_names [FR_REG (0)];
c65ebc55 2902 else if (x == CONST1_RTX (GET_MODE (x)))
e57b9d65 2903 str = reg_names [FR_REG (1)];
c65ebc55 2904 else if (GET_CODE (x) == REG)
e57b9d65 2905 str = reg_names [REGNO (x)];
c65ebc55
JW
2906 else
2907 abort ();
e57b9d65 2908 fputs (str, file);
c65ebc55
JW
2909 return;
2910
2911 case 'I':
2912 fputs (reg_names [REGNO (x) + 1], file);
2913 return;
2914
e5bde68a 2915 case 'J':
6b6c1201
RH
2916 case 'j':
2917 {
2918 unsigned int regno = REGNO (XEXP (x, 0));
2919 if (GET_CODE (x) == EQ)
2920 regno += 1;
2921 if (code == 'j')
2922 regno ^= 1;
2923 fputs (reg_names [regno], file);
2924 }
e5bde68a
RH
2925 return;
2926
c65ebc55
JW
2927 case 'O':
2928 if (MEM_VOLATILE_P (x))
2929 fputs(".acq", file);
2930 return;
2931
2932 case 'P':
2933 {
4b983fdc 2934 HOST_WIDE_INT value;
c65ebc55 2935
4b983fdc
RH
2936 switch (GET_CODE (XEXP (x, 0)))
2937 {
2938 default:
2939 return;
2940
2941 case POST_MODIFY:
2942 x = XEXP (XEXP (XEXP (x, 0), 1), 1);
2943 if (GET_CODE (x) == CONST_INT)
08012cda 2944 value = INTVAL (x);
4b983fdc
RH
2945 else if (GET_CODE (x) == REG)
2946 {
08012cda 2947 fprintf (file, ", %s", reg_names[REGNO (x)]);
4b983fdc
RH
2948 return;
2949 }
2950 else
2951 abort ();
2952 break;
c65ebc55 2953
4b983fdc
RH
2954 case POST_INC:
2955 value = GET_MODE_SIZE (GET_MODE (x));
4b983fdc 2956 break;
c65ebc55 2957
4b983fdc 2958 case POST_DEC:
08012cda 2959 value = - (HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (x));
4b983fdc
RH
2960 break;
2961 }
809d4ef1 2962
4b983fdc
RH
2963 putc (',', file);
2964 putc (' ', file);
2965 fprintf (file, HOST_WIDE_INT_PRINT_DEC, value);
c65ebc55
JW
2966 return;
2967 }
2968
2969 case 'Q':
2970 if (MEM_VOLATILE_P (x))
2971 fputs(".rel", file);
2972 return;
2973
2974 case 'S':
809d4ef1 2975 fprintf (file, "%d", exact_log2 (INTVAL (x)));
c65ebc55
JW
2976 return;
2977
2978 case 'T':
2979 if (! TARGET_GNU_AS && GET_CODE (x) == CONST_INT)
2980 {
809d4ef1 2981 fprintf (file, "0x%x", (int) INTVAL (x) & 0xffffffff);
c65ebc55
JW
2982 return;
2983 }
2984 break;
2985
2986 case 'U':
2987 if (! TARGET_GNU_AS && GET_CODE (x) == CONST_INT)
2988 {
3b572406 2989 const char *prefix = "0x";
c65ebc55
JW
2990 if (INTVAL (x) & 0x80000000)
2991 {
2992 fprintf (file, "0xffffffff");
2993 prefix = "";
2994 }
809d4ef1 2995 fprintf (file, "%s%x", prefix, (int) INTVAL (x) & 0xffffffff);
c65ebc55
JW
2996 return;
2997 }
2998 break;
809d4ef1 2999
c65ebc55
JW
3000 case 'r':
3001 /* If this operand is the constant zero, write it as zero. */
3002 if (GET_CODE (x) == REG)
3003 fputs (reg_names[REGNO (x)], file);
3004 else if (x == CONST0_RTX (GET_MODE (x)))
3005 fputs ("r0", file);
3006 else
3007 output_operand_lossage ("invalid %%r value");
3008 return;
3009
85548039
RH
3010 case '+':
3011 {
3012 const char *which;
3013
3014 /* For conditional branches, returns or calls, substitute
3015 sptk, dptk, dpnt, or spnt for %s. */
3016 x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
3017 if (x)
3018 {
3019 int pred_val = INTVAL (XEXP (x, 0));
3020
3021 /* Guess top and bottom 10% statically predicted. */
3022 if (pred_val < REG_BR_PROB_BASE / 10)
3023 which = ".spnt";
3024 else if (pred_val < REG_BR_PROB_BASE / 2)
3025 which = ".dpnt";
3026 else if (pred_val < REG_BR_PROB_BASE * 9 / 10)
3027 which = ".dptk";
3028 else
3029 which = ".sptk";
3030 }
3031 else if (GET_CODE (current_output_insn) == CALL_INSN)
3032 which = ".sptk";
3033 else
3034 which = ".dptk";
3035
3036 fputs (which, file);
3037 return;
3038 }
3039
6f8aa100
RH
3040 case ',':
3041 x = current_insn_predicate;
3042 if (x)
3043 {
3044 unsigned int regno = REGNO (XEXP (x, 0));
3045 if (GET_CODE (x) == EQ)
3046 regno += 1;
6f8aa100
RH
3047 fprintf (file, "(%s) ", reg_names [regno]);
3048 }
3049 return;
3050
c65ebc55
JW
3051 default:
3052 output_operand_lossage ("ia64_print_operand: unknown code");
3053 return;
3054 }
3055
3056 switch (GET_CODE (x))
3057 {
3058 /* This happens for the spill/restore instructions. */
3059 case POST_INC:
4b983fdc
RH
3060 case POST_DEC:
3061 case POST_MODIFY:
c65ebc55
JW
3062 x = XEXP (x, 0);
3063 /* ... fall through ... */
3064
3065 case REG:
3066 fputs (reg_names [REGNO (x)], file);
3067 break;
3068
3069 case MEM:
3070 {
3071 rtx addr = XEXP (x, 0);
4b983fdc 3072 if (GET_RTX_CLASS (GET_CODE (addr)) == 'a')
c65ebc55
JW
3073 addr = XEXP (addr, 0);
3074 fprintf (file, "[%s]", reg_names [REGNO (addr)]);
3075 break;
3076 }
809d4ef1 3077
c65ebc55
JW
3078 default:
3079 output_addr_const (file, x);
3080 break;
3081 }
3082
3083 return;
3084}
c65ebc55 3085\f
5527bf14
RH
3086/* Calulate the cost of moving data from a register in class FROM to
3087 one in class TO. */
3088
3089int
3090ia64_register_move_cost (from, to)
3091 enum reg_class from, to;
3092{
3093 int from_hard, to_hard;
3094 int from_gr, to_gr;
3f622353 3095 int from_fr, to_fr;
5527bf14
RH
3096
3097 from_hard = (from == BR_REGS || from == AR_M_REGS || from == AR_I_REGS);
3098 to_hard = (to == BR_REGS || to == AR_M_REGS || to == AR_I_REGS);
3099 from_gr = (from == GENERAL_REGS);
3100 to_gr = (to == GENERAL_REGS);
3f622353
RH
3101 from_fr = (from == FR_REGS);
3102 to_fr = (to == FR_REGS);
5527bf14
RH
3103
3104 if (from_hard && to_hard)
3105 return 8;
3106 else if ((from_hard && !to_gr) || (!from_gr && to_hard))
3107 return 6;
3108
3f622353
RH
3109 /* ??? Moving from FR<->GR must be more expensive than 2, so that we get
3110 secondary memory reloads for TFmode moves. Unfortunately, we don't
3111 have the mode here, so we can't check that. */
3112 /* Moreover, we have to make this at least as high as MEMORY_MOVE_COST
3113 to avoid spectacularly poor register class preferencing for TFmode. */
3114 else if (from_fr != to_fr)
3115 return 5;
3116
5527bf14
RH
3117 return 2;
3118}
c65ebc55
JW
3119
3120/* This function returns the register class required for a secondary
3121 register when copying between one of the registers in CLASS, and X,
3122 using MODE. A return value of NO_REGS means that no secondary register
3123 is required. */
3124
3125enum reg_class
3126ia64_secondary_reload_class (class, mode, x)
3127 enum reg_class class;
fd7c34b0 3128 enum machine_mode mode ATTRIBUTE_UNUSED;
c65ebc55
JW
3129 rtx x;
3130{
3131 int regno = -1;
3132
3133 if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
3134 regno = true_regnum (x);
3135
97e242b0
RH
3136 switch (class)
3137 {
3138 case BR_REGS:
3139 /* ??? This is required because of a bad gcse/cse/global interaction.
3140 We end up with two pseudos with overlapping lifetimes both of which
3141 are equiv to the same constant, and both which need to be in BR_REGS.
3142 This results in a BR_REGS to BR_REGS copy which doesn't exist. To
3143 reproduce, return NO_REGS here, and compile divdi3 in libgcc2.c.
3144 This seems to be a cse bug. cse_basic_block_end changes depending
3145 on the path length, which means the qty_first_reg check in
3146 make_regs_eqv can give different answers at different times. */
3147 /* ??? At some point I'll probably need a reload_indi pattern to handle
3148 this. */
3149 if (BR_REGNO_P (regno))
3150 return GR_REGS;
3151
3152 /* This is needed if a pseudo used as a call_operand gets spilled to a
3153 stack slot. */
3154 if (GET_CODE (x) == MEM)
3155 return GR_REGS;
3156 break;
3157
3158 case FR_REGS:
3159 /* This can happen when a paradoxical subreg is an operand to the
3160 muldi3 pattern. */
3161 /* ??? This shouldn't be necessary after instruction scheduling is
3162 enabled, because paradoxical subregs are not accepted by
3163 register_operand when INSN_SCHEDULING is defined. Or alternatively,
3164 stop the paradoxical subreg stupidity in the *_operand functions
3165 in recog.c. */
3166 if (GET_CODE (x) == MEM
3167 && (GET_MODE (x) == SImode || GET_MODE (x) == HImode
3168 || GET_MODE (x) == QImode))
3169 return GR_REGS;
3170
3171 /* This can happen because of the ior/and/etc patterns that accept FP
3172 registers as operands. If the third operand is a constant, then it
3173 needs to be reloaded into a FP register. */
3174 if (GET_CODE (x) == CONST_INT)
3175 return GR_REGS;
3176
3177 /* This can happen because of register elimination in a muldi3 insn.
3178 E.g. `26107 * (unsigned long)&u'. */
3179 if (GET_CODE (x) == PLUS)
3180 return GR_REGS;
3181 break;
3182
3183 case PR_REGS:
3184 /* ??? This happens if we cse/gcse a CCmode value across a call,
3185 and the function has a nonlocal goto. This is because global
3186 does not allocate call crossing pseudos to hard registers when
3187 current_function_has_nonlocal_goto is true. This is relatively
3188 common for C++ programs that use exceptions. To reproduce,
3189 return NO_REGS and compile libstdc++. */
3190 if (GET_CODE (x) == MEM)
3191 return GR_REGS;
3192 break;
3193
3f622353
RH
3194 case GR_REGS:
3195 /* Since we have no offsettable memory addresses, we need a temporary
3196 to hold the address of the second word. */
3197 if (mode == TImode)
3198 return GR_REGS;
3199 break;
3200
97e242b0
RH
3201 default:
3202 break;
3203 }
c65ebc55
JW
3204
3205 return NO_REGS;
3206}
3207
3208\f
3209/* Emit text to declare externally defined variables and functions, because
3210 the Intel assembler does not support undefined externals. */
3211
3212void
3213ia64_asm_output_external (file, decl, name)
3214 FILE *file;
3215 tree decl;
809d4ef1 3216 const char *name;
c65ebc55
JW
3217{
3218 int save_referenced;
3219
3220 /* GNU as does not need anything here. */
3221 if (TARGET_GNU_AS)
3222 return;
3223
3224 /* ??? The Intel assembler creates a reference that needs to be satisfied by
3225 the linker when we do this, so we need to be careful not to do this for
3226 builtin functions which have no library equivalent. Unfortunately, we
3227 can't tell here whether or not a function will actually be called by
3228 expand_expr, so we pull in library functions even if we may not need
3229 them later. */
3230 if (! strcmp (name, "__builtin_next_arg")
3231 || ! strcmp (name, "alloca")
3232 || ! strcmp (name, "__builtin_constant_p")
3233 || ! strcmp (name, "__builtin_args_info"))
3234 return;
3235
3236 /* assemble_name will set TREE_SYMBOL_REFERENCED, so we must save and
3237 restore it. */
3238 save_referenced = TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl));
3239 if (TREE_CODE (decl) == FUNCTION_DECL)
3240 {
3241 fprintf (file, "\t%s\t ", TYPE_ASM_OP);
3242 assemble_name (file, name);
3243 putc (',', file);
3244 fprintf (file, TYPE_OPERAND_FMT, "function");
3245 putc ('\n', file);
3246 }
3247 ASM_GLOBALIZE_LABEL (file, name);
3248 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = save_referenced;
3249}
3250\f
3251/* Parse the -mfixed-range= option string. */
3252
3253static void
3b572406
RH
3254fix_range (const_str)
3255 const char *const_str;
c65ebc55
JW
3256{
3257 int i, first, last;
3b572406 3258 char *str, *dash, *comma;
c65ebc55
JW
3259
3260 /* str must be of the form REG1'-'REG2{,REG1'-'REG} where REG1 and
3261 REG2 are either register names or register numbers. The effect
3262 of this option is to mark the registers in the range from REG1 to
3263 REG2 as ``fixed'' so they won't be used by the compiler. This is
3264 used, e.g., to ensure that kernel mode code doesn't use f32-f127. */
3265
3b572406
RH
3266 i = strlen (const_str);
3267 str = (char *) alloca (i + 1);
3268 memcpy (str, const_str, i + 1);
3269
c65ebc55
JW
3270 while (1)
3271 {
3272 dash = strchr (str, '-');
3273 if (!dash)
3274 {
3275 warning ("value of -mfixed-range must have form REG1-REG2");
3276 return;
3277 }
3278 *dash = '\0';
3279
3280 comma = strchr (dash + 1, ',');
3281 if (comma)
3282 *comma = '\0';
3283
3284 first = decode_reg_name (str);
3285 if (first < 0)
3286 {
3287 warning ("unknown register name: %s", str);
3288 return;
3289 }
3290
3291 last = decode_reg_name (dash + 1);
3292 if (last < 0)
3293 {
3294 warning ("unknown register name: %s", dash + 1);
3295 return;
3296 }
3297
3298 *dash = '-';
3299
3300 if (first > last)
3301 {
3302 warning ("%s-%s is an empty range", str, dash + 1);
3303 return;
3304 }
3305
3306 for (i = first; i <= last; ++i)
3307 fixed_regs[i] = call_used_regs[i] = 1;
3308
3309 if (!comma)
3310 break;
3311
3312 *comma = ',';
3313 str = comma + 1;
3314 }
3315}
3316
3317/* Called to register all of our global variables with the garbage
3318 collector. */
3319
3320static void
3321ia64_add_gc_roots ()
3322{
3323 ggc_add_rtx_root (&ia64_compare_op0, 1);
3324 ggc_add_rtx_root (&ia64_compare_op1, 1);
3325}
3326
0c96007e
AM
3327static void
3328ia64_init_machine_status (p)
3329 struct function *p;
3330{
3331 p->machine =
3332 (struct machine_function *) xcalloc (1, sizeof (struct machine_function));
3333}
3334
3335static void
3336ia64_mark_machine_status (p)
3337 struct function *p;
3338{
3339 ggc_mark_rtx (p->machine->ia64_eh_epilogue_sp);
3340 ggc_mark_rtx (p->machine->ia64_eh_epilogue_bsp);
97e242b0 3341 ggc_mark_rtx (p->machine->ia64_gp_save);
0c96007e
AM
3342}
3343
3344
c65ebc55
JW
3345/* Handle TARGET_OPTIONS switches. */
3346
3347void
3348ia64_override_options ()
3349{
59da9a7d
JW
3350 if (TARGET_AUTO_PIC)
3351 target_flags |= MASK_CONST_GP;
3352
c65ebc55
JW
3353 if (ia64_fixed_range_string)
3354 fix_range (ia64_fixed_range_string);
3355
3356 ia64_section_threshold = g_switch_set ? g_switch_value : IA64_DEFAULT_GVALUE;
3357
0c96007e
AM
3358 init_machine_status = ia64_init_machine_status;
3359 mark_machine_status = ia64_mark_machine_status;
3360
c65ebc55
JW
3361 ia64_add_gc_roots ();
3362}
3363\f
3364/* The following collection of routines emit instruction group stop bits as
3365 necessary to avoid dependencies. */
3366
3367/* Need to track some additional registers as far as serialization is
3368 concerned so we can properly handle br.call and br.ret. We could
3369 make these registers visible to gcc, but since these registers are
3370 never explicitly used in gcc generated code, it seems wasteful to
3371 do so (plus it would make the call and return patterns needlessly
3372 complex). */
3373#define REG_GP (GR_REG (1))
3374#define REG_RP (BR_REG (0))
c65ebc55 3375#define REG_AR_CFM (FIRST_PSEUDO_REGISTER + 1)
c65ebc55
JW
3376/* This is used for volatile asms which may require a stop bit immediately
3377 before and after them. */
5527bf14 3378#define REG_VOLATILE (FIRST_PSEUDO_REGISTER + 2)
870f9ec0
RH
3379#define AR_UNAT_BIT_0 (FIRST_PSEUDO_REGISTER + 3)
3380#define NUM_REGS (AR_UNAT_BIT_0 + 64)
c65ebc55
JW
3381
3382/* For each register, we keep track of how many times it has been
3383 written in the current instruction group. If a register is written
3384 unconditionally (no qualifying predicate), WRITE_COUNT is set to 2
3385 and FIRST_PRED is ignored. If a register is written if its
3386 qualifying predicate P is true, we set WRITE_COUNT to 1 and
3387 FIRST_PRED to P. Later on, the same register may be written again
3388 by the complement of P (P+1 if P is even, P-1, otherwise) and when
3389 this happens, WRITE_COUNT gets set to 2. The result of this is
3390 that whenever an insn attempts to write a register whose
3391 WRITE_COUNT is two, we need to issue a insn group barrier first. */
3392struct reg_write_state
3393{
3394 char write_count;
3395 char written_by_fp; /* Was register written by a floating-point insn? */
3396 short first_pred; /* 0 means ``no predicate'' */
3397};
3398
3399/* Cumulative info for the current instruction group. */
3400struct reg_write_state rws_sum[NUM_REGS];
3401/* Info for the current instruction. This gets copied to rws_sum after a
3402 stop bit is emitted. */
3403struct reg_write_state rws_insn[NUM_REGS];
3404
3405/* Misc flags needed to compute RAW/WAW dependencies while we are traversing
3406 RTL for one instruction. */
3407struct reg_flags
3408{
3409 unsigned int is_write : 1; /* Is register being written? */
3410 unsigned int is_fp : 1; /* Is register used as part of an fp op? */
3411 unsigned int is_branch : 1; /* Is register used as part of a branch? */
3412};
3413
3b572406
RH
3414static void rws_update PARAMS ((struct reg_write_state *, int,
3415 struct reg_flags, int));
97e242b0
RH
3416static int rws_access_regno PARAMS ((int, struct reg_flags, int));
3417static int rws_access_reg PARAMS ((rtx, struct reg_flags, int));
3b572406
RH
3418static int rtx_needs_barrier PARAMS ((rtx, struct reg_flags, int));
3419
c65ebc55
JW
3420/* Update *RWS for REGNO, which is being written by the current instruction,
3421 with predicate PRED, and associated register flags in FLAGS. */
3422
3423static void
3424rws_update (rws, regno, flags, pred)
3425 struct reg_write_state *rws;
3426 int regno;
3427 struct reg_flags flags;
3428 int pred;
3429{
3430 rws[regno].write_count += pred ? 1 : 2;
3431 rws[regno].written_by_fp |= flags.is_fp;
3432 rws[regno].first_pred = pred;
3433}
3434
3435/* Handle an access to register REGNO of type FLAGS using predicate register
3436 PRED. Update rws_insn and rws_sum arrays. Return 1 if this access creates
3437 a dependency with an earlier instruction in the same group. */
3438
3439static int
97e242b0 3440rws_access_regno (regno, flags, pred)
c65ebc55
JW
3441 int regno;
3442 struct reg_flags flags;
3443 int pred;
3444{
3445 int need_barrier = 0;
c65ebc55
JW
3446
3447 if (regno >= NUM_REGS)
3448 abort ();
3449
3450 if (flags.is_write)
3451 {
12c2c7aa
JW
3452 int write_count;
3453
c65ebc55
JW
3454 /* One insn writes same reg multiple times? */
3455 if (rws_insn[regno].write_count > 0)
3456 abort ();
3457
3458 /* Update info for current instruction. */
3459 rws_update (rws_insn, regno, flags, pred);
12c2c7aa 3460 write_count = rws_sum[regno].write_count;
12c2c7aa
JW
3461
3462 switch (write_count)
c65ebc55
JW
3463 {
3464 case 0:
3465 /* The register has not been written yet. */
3466 rws_update (rws_sum, regno, flags, pred);
c65ebc55
JW
3467 break;
3468
3469 case 1:
3470 /* The register has been written via a predicate. If this is
3471 not a complementary predicate, then we need a barrier. */
3472 /* ??? This assumes that P and P+1 are always complementary
3473 predicates for P even. */
3474 if ((rws_sum[regno].first_pred ^ 1) != pred)
3475 need_barrier = 1;
3476 rws_update (rws_sum, regno, flags, pred);
c65ebc55
JW
3477 break;
3478
3479 case 2:
3480 /* The register has been unconditionally written already. We
3481 need a barrier. */
3482 need_barrier = 1;
3483 break;
3484
3485 default:
3486 abort ();
3487 }
3488 }
3489 else
3490 {
3491 if (flags.is_branch)
3492 {
3493 /* Branches have several RAW exceptions that allow to avoid
3494 barriers. */
3495
5527bf14 3496 if (REGNO_REG_CLASS (regno) == BR_REGS || regno == AR_PFS_REGNUM)
c65ebc55
JW
3497 /* RAW dependencies on branch regs are permissible as long
3498 as the writer is a non-branch instruction. Since we
3499 never generate code that uses a branch register written
3500 by a branch instruction, handling this case is
3501 easy. */
5527bf14 3502 return 0;
c65ebc55
JW
3503
3504 if (REGNO_REG_CLASS (regno) == PR_REGS
3505 && ! rws_sum[regno].written_by_fp)
3506 /* The predicates of a branch are available within the
3507 same insn group as long as the predicate was written by
3508 something other than a floating-point instruction. */
3509 return 0;
3510 }
3511
3512 switch (rws_sum[regno].write_count)
3513 {
3514 case 0:
3515 /* The register has not been written yet. */
3516 break;
3517
3518 case 1:
3519 /* The register has been written via a predicate. If this is
3520 not a complementary predicate, then we need a barrier. */
3521 /* ??? This assumes that P and P+1 are always complementary
3522 predicates for P even. */
3523 if ((rws_sum[regno].first_pred ^ 1) != pred)
3524 need_barrier = 1;
3525 break;
3526
3527 case 2:
3528 /* The register has been unconditionally written already. We
3529 need a barrier. */
3530 need_barrier = 1;
3531 break;
3532
3533 default:
3534 abort ();
3535 }
3536 }
3537
3538 return need_barrier;
3539}
3540
97e242b0
RH
3541static int
3542rws_access_reg (reg, flags, pred)
3543 rtx reg;
3544 struct reg_flags flags;
3545 int pred;
3546{
3547 int regno = REGNO (reg);
3548 int n = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
3549
3550 if (n == 1)
3551 return rws_access_regno (regno, flags, pred);
3552 else
3553 {
3554 int need_barrier = 0;
3555 while (--n >= 0)
3556 need_barrier |= rws_access_regno (regno + n, flags, pred);
3557 return need_barrier;
3558 }
3559}
3560
c65ebc55
JW
3561/* Handle an access to rtx X of type FLAGS using predicate register PRED.
3562 Return 1 is this access creates a dependency with an earlier instruction
3563 in the same group. */
3564
3565static int
3566rtx_needs_barrier (x, flags, pred)
3567 rtx x;
3568 struct reg_flags flags;
3569 int pred;
3570{
3571 int i, j;
3572 int is_complemented = 0;
3573 int need_barrier = 0;
3574 const char *format_ptr;
3575 struct reg_flags new_flags;
3576 rtx src, dst;
3577 rtx cond = 0;
3578
3579 if (! x)
3580 return 0;
3581
3582 new_flags = flags;
3583
3584 switch (GET_CODE (x))
3585 {
3586 case SET:
3587 src = SET_SRC (x);
3588 switch (GET_CODE (src))
3589 {
3590 case CALL:
3591 /* We don't need to worry about the result registers that
3592 get written by subroutine call. */
3593 need_barrier = rtx_needs_barrier (src, flags, pred);
3594 return need_barrier;
3595
3596 case IF_THEN_ELSE:
3597 if (SET_DEST (x) == pc_rtx)
3598 {
3599 /* X is a conditional branch. */
3600 /* ??? This seems redundant, as the caller sets this bit for
3601 all JUMP_INSNs. */
3602 new_flags.is_branch = 1;
3603 need_barrier = rtx_needs_barrier (src, new_flags, pred);
3604 return need_barrier;
3605 }
3606 else
3607 {
3608 /* X is a conditional move. */
3609 cond = XEXP (src, 0);
3610 if (GET_CODE (cond) == EQ)
3611 is_complemented = 1;
3612 cond = XEXP (cond, 0);
3613 if (GET_CODE (cond) != REG
3614 && REGNO_REG_CLASS (REGNO (cond)) != PR_REGS)
3615 abort ();
3616
3617 if (XEXP (src, 1) == SET_DEST (x)
3618 || XEXP (src, 2) == SET_DEST (x))
3619 {
3620 /* X is a conditional move that conditionally writes the
3621 destination. */
3622
3623 /* We need another complement in this case. */
3624 if (XEXP (src, 1) == SET_DEST (x))
3625 is_complemented = ! is_complemented;
3626
3627 pred = REGNO (cond);
3628 if (is_complemented)
3629 ++pred;
3630 }
3631
3632 /* ??? If this is a conditional write to the dest, then this
3633 instruction does not actually read one source. This probably
3634 doesn't matter, because that source is also the dest. */
3635 /* ??? Multiple writes to predicate registers are allowed
3636 if they are all AND type compares, or if they are all OR
3637 type compares. We do not generate such instructions
3638 currently. */
3639 }
3640 /* ... fall through ... */
3641
3642 default:
3643 if (GET_RTX_CLASS (GET_CODE (src)) == '<'
3644 && GET_MODE_CLASS (GET_MODE (XEXP (src, 0))) == MODE_FLOAT)
3645 /* Set new_flags.is_fp to 1 so that we know we're dealing
3646 with a floating point comparison when processing the
3647 destination of the SET. */
3648 new_flags.is_fp = 1;
3649 break;
3650 }
3651 need_barrier = rtx_needs_barrier (src, flags, pred);
97e242b0 3652
c65ebc55
JW
3653 /* This instruction unconditionally uses a predicate register. */
3654 if (cond)
97e242b0 3655 need_barrier |= rws_access_reg (cond, flags, 0);
c65ebc55
JW
3656
3657 dst = SET_DEST (x);
3658 if (GET_CODE (dst) == ZERO_EXTRACT)
3659 {
3660 need_barrier |= rtx_needs_barrier (XEXP (dst, 1), flags, pred);
3661 need_barrier |= rtx_needs_barrier (XEXP (dst, 2), flags, pred);
3662 dst = XEXP (dst, 0);
3663 }
3664 new_flags.is_write = 1;
3665 need_barrier |= rtx_needs_barrier (dst, new_flags, pred);
3666 break;
3667
3668 case CALL:
3669 new_flags.is_write = 0;
97e242b0 3670 need_barrier |= rws_access_regno (AR_EC_REGNUM, new_flags, pred);
c65ebc55
JW
3671
3672 /* Avoid multiple register writes, in case this is a pattern with
3673 multiple CALL rtx. This avoids an abort in rws_access_reg. */
3674 /* ??? This assumes that no rtx other than CALL/RETURN sets REG_AR_CFM,
3675 and that we don't have predicated calls/returns. */
3676 if (! rws_insn[REG_AR_CFM].write_count)
3677 {
3678 new_flags.is_write = 1;
97e242b0
RH
3679 need_barrier |= rws_access_regno (REG_RP, new_flags, pred);
3680 need_barrier |= rws_access_regno (AR_PFS_REGNUM, new_flags, pred);
3681 need_barrier |= rws_access_regno (REG_AR_CFM, new_flags, pred);
c65ebc55
JW
3682 }
3683 break;
3684
e5bde68a
RH
3685 case COND_EXEC:
3686 /* X is a predicated instruction. */
3687
3688 cond = COND_EXEC_TEST (x);
3689 if (pred)
3690 abort ();
3691 need_barrier = rtx_needs_barrier (cond, flags, 0);
3692
3693 if (GET_CODE (cond) == EQ)
3694 is_complemented = 1;
3695 cond = XEXP (cond, 0);
3696 if (GET_CODE (cond) != REG
3697 && REGNO_REG_CLASS (REGNO (cond)) != PR_REGS)
3698 abort ();
3699 pred = REGNO (cond);
3700 if (is_complemented)
3701 ++pred;
3702
3703 need_barrier |= rtx_needs_barrier (COND_EXEC_CODE (x), flags, pred);
3704 return need_barrier;
3705
c65ebc55
JW
3706 case CLOBBER:
3707#if 0
3708 case USE:
3709 /* We must handle USE here in case it occurs within a PARALLEL.
3710 For instance, the mov ar.pfs= instruction has a USE which requires
3711 a barrier between it and an immediately preceeding alloc. */
3712#endif
3713 /* Clobber & use are for earlier compiler-phases only. */
3714 break;
3715
3716 case ASM_OPERANDS:
3717 case ASM_INPUT:
3718 /* We always emit stop bits for traditional asms. We emit stop bits
3719 for volatile extended asms if TARGET_VOL_ASM_STOP is true. */
3720 if (GET_CODE (x) != ASM_OPERANDS
3721 || (MEM_VOLATILE_P (x) && TARGET_VOL_ASM_STOP))
3722 {
3723 /* Avoid writing the register multiple times if we have multiple
3724 asm outputs. This avoids an abort in rws_access_reg. */
3725 if (! rws_insn[REG_VOLATILE].write_count)
3726 {
3727 new_flags.is_write = 1;
97e242b0 3728 rws_access_regno (REG_VOLATILE, new_flags, pred);
c65ebc55
JW
3729 }
3730 return 1;
3731 }
3732
3733 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
3734 We can not just fall through here since then we would be confused
3735 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
3736 traditional asms unlike their normal usage. */
3737
3738 for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; --i)
3739 if (rtx_needs_barrier (ASM_OPERANDS_INPUT (x, i), flags, pred))
3740 need_barrier = 1;
3741 break;
3742
3743 case PARALLEL:
3744 for (i = XVECLEN (x, 0) - 1; i >= 0; --i)
3745 if (rtx_needs_barrier (XVECEXP (x, 0, i), flags, pred))
3746 need_barrier = 1;
3747 break;
3748
3749 case SUBREG:
3750 x = SUBREG_REG (x);
3751 /* FALLTHRU */
3752 case REG:
870f9ec0
RH
3753 if (REGNO (x) == AR_UNAT_REGNUM)
3754 {
3755 for (i = 0; i < 64; ++i)
3756 need_barrier |= rws_access_regno (AR_UNAT_BIT_0 + i, flags, pred);
3757 }
3758 else
3759 need_barrier = rws_access_reg (x, flags, pred);
c65ebc55
JW
3760 break;
3761
3762 case MEM:
3763 /* Find the regs used in memory address computation. */
3764 new_flags.is_write = 0;
3765 need_barrier = rtx_needs_barrier (XEXP (x, 0), new_flags, pred);
3766 break;
3767
3768 case CONST_INT: case CONST_DOUBLE:
3769 case SYMBOL_REF: case LABEL_REF: case CONST:
3770 break;
3771
3772 /* Operators with side-effects. */
3773 case POST_INC: case POST_DEC:
3774 if (GET_CODE (XEXP (x, 0)) != REG)
3775 abort ();
3776
3777 new_flags.is_write = 0;
97e242b0 3778 need_barrier = rws_access_reg (XEXP (x, 0), new_flags, pred);
c65ebc55 3779 new_flags.is_write = 1;
97e242b0 3780 need_barrier |= rws_access_reg (XEXP (x, 0), new_flags, pred);
4b983fdc
RH
3781 break;
3782
3783 case POST_MODIFY:
3784 if (GET_CODE (XEXP (x, 0)) != REG)
3785 abort ();
3786
3787 new_flags.is_write = 0;
97e242b0 3788 need_barrier = rws_access_reg (XEXP (x, 0), new_flags, pred);
4b983fdc
RH
3789 need_barrier |= rtx_needs_barrier (XEXP (x, 1), new_flags, pred);
3790 new_flags.is_write = 1;
97e242b0 3791 need_barrier |= rws_access_reg (XEXP (x, 0), new_flags, pred);
c65ebc55
JW
3792 break;
3793
3794 /* Handle common unary and binary ops for efficiency. */
3795 case COMPARE: case PLUS: case MINUS: case MULT: case DIV:
3796 case MOD: case UDIV: case UMOD: case AND: case IOR:
3797 case XOR: case ASHIFT: case ROTATE: case ASHIFTRT: case LSHIFTRT:
3798 case ROTATERT: case SMIN: case SMAX: case UMIN: case UMAX:
3799 case NE: case EQ: case GE: case GT: case LE:
3800 case LT: case GEU: case GTU: case LEU: case LTU:
3801 need_barrier = rtx_needs_barrier (XEXP (x, 0), new_flags, pred);
3802 need_barrier |= rtx_needs_barrier (XEXP (x, 1), new_flags, pred);
3803 break;
3804
3805 case NEG: case NOT: case SIGN_EXTEND: case ZERO_EXTEND:
3806 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE: case FLOAT:
3807 case FIX: case UNSIGNED_FLOAT: case UNSIGNED_FIX: case ABS:
3808 case SQRT: case FFS:
3809 need_barrier = rtx_needs_barrier (XEXP (x, 0), flags, pred);
3810 break;
3811
3812 case UNSPEC:
3813 switch (XINT (x, 1))
3814 {
c65ebc55
JW
3815 case 1: /* st8.spill */
3816 case 2: /* ld8.fill */
870f9ec0
RH
3817 {
3818 HOST_WIDE_INT offset = INTVAL (XVECEXP (x, 0, 1));
3819 HOST_WIDE_INT bit = (offset >> 3) & 63;
3820
3821 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
3822 new_flags.is_write = (XINT (x, 1) == 1);
3823 need_barrier |= rws_access_regno (AR_UNAT_BIT_0 + bit,
3824 new_flags, pred);
3825 break;
3826 }
3827
c65ebc55
JW
3828 case 3: /* stf.spill */
3829 case 4: /* ldf.spill */
3830 case 8: /* popcnt */
3831 need_barrier = rtx_needs_barrier (XVECEXP (x, 0, 0), flags, pred);
3832 break;
3833
c65ebc55 3834 case 12: /* mf */
c65ebc55 3835 case 13: /* cmpxchg_acq */
c65ebc55 3836 case 19: /* fetchadd_acq */
0c96007e 3837 case 20: /* mov = ar.bsp */
ce152ef8
AM
3838 case 21: /* flushrs */
3839 break;
0c96007e 3840
c65ebc55
JW
3841 default:
3842 abort ();
3843 }
3844 break;
3845
3846 case UNSPEC_VOLATILE:
3847 switch (XINT (x, 1))
3848 {
3849 case 0: /* alloc */
3850 /* Alloc must always be the first instruction. Currently, we
3851 only emit it at the function start, so we don't need to worry
3852 about emitting a stop bit before it. */
97e242b0 3853 need_barrier = rws_access_regno (AR_PFS_REGNUM, flags, pred);
c65ebc55
JW
3854
3855 new_flags.is_write = 1;
97e242b0 3856 need_barrier |= rws_access_regno (REG_AR_CFM, new_flags, pred);
c65ebc55
JW
3857 return need_barrier;
3858
3859 case 1: /* blockage */
3860 case 2: /* insn group barrier */
3861 return 0;
3862
3b572406
RH
3863 case 5: /* set_bsp */
3864 need_barrier = 1;
3865 break;
3866
3b572406
RH
3867 case 7: /* pred.rel.mutex */
3868 return 0;
0c96007e 3869
c65ebc55
JW
3870 default:
3871 abort ();
3872 }
3873 break;
3874
3875 case RETURN:
3876 new_flags.is_write = 0;
97e242b0
RH
3877 need_barrier = rws_access_regno (REG_RP, flags, pred);
3878 need_barrier |= rws_access_regno (AR_PFS_REGNUM, flags, pred);
c65ebc55
JW
3879
3880 new_flags.is_write = 1;
97e242b0
RH
3881 need_barrier |= rws_access_regno (AR_EC_REGNUM, new_flags, pred);
3882 need_barrier |= rws_access_regno (REG_AR_CFM, new_flags, pred);
c65ebc55
JW
3883 break;
3884
3885 default:
3886 format_ptr = GET_RTX_FORMAT (GET_CODE (x));
3887 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
3888 switch (format_ptr[i])
3889 {
3890 case '0': /* unused field */
3891 case 'i': /* integer */
3892 case 'n': /* note */
3893 case 'w': /* wide integer */
3894 case 's': /* pointer to string */
3895 case 'S': /* optional pointer to string */
3896 break;
3897
3898 case 'e':
3899 if (rtx_needs_barrier (XEXP (x, i), flags, pred))
3900 need_barrier = 1;
3901 break;
3902
3903 case 'E':
3904 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
3905 if (rtx_needs_barrier (XVECEXP (x, i, j), flags, pred))
3906 need_barrier = 1;
3907 break;
3908
3909 default:
3910 abort ();
3911 }
3912 }
3913 return need_barrier;
3914}
3915
3916/* INSNS is an chain of instructions. Scan the chain, and insert stop bits
3917 as necessary to eliminate dependendencies. */
3918
3919static void
3920emit_insn_group_barriers (insns)
3921 rtx insns;
3922{
c65ebc55
JW
3923 rtx insn, prev_insn;
3924
3925 memset (rws_sum, 0, sizeof (rws_sum));
3926
3927 prev_insn = 0;
3928 for (insn = insns; insn; insn = NEXT_INSN (insn))
3929 {
6b6c1201
RH
3930 int need_barrier = 0;
3931 struct reg_flags flags;
3932
c65ebc55
JW
3933 memset (&flags, 0, sizeof (flags));
3934 switch (GET_CODE (insn))
3935 {
3936 case NOTE:
9c668921
RH
3937 /* For very small loops we can wind up with extra stop bits
3938 inside the loop because of not putting a stop after the
3939 assignment to ar.lc before the loop label. */
3940 /* ??? Ideally we'd do this for any register used in the first
3941 insn group that's been written recently. */
3942 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
3943 {
3944 need_barrier = rws_access_regno (AR_LC_REGNUM, flags, 0);
3945 if (need_barrier)
3946 {
3947 emit_insn_after (gen_insn_group_barrier (), insn);
3948 memset (rws_sum, 0, sizeof(rws_sum));
3949 prev_insn = NULL_RTX;
3950 }
3951 }
6b6c1201
RH
3952 break;
3953
3954 case CALL_INSN:
3955 flags.is_branch = 1;
3956 memset (rws_insn, 0, sizeof (rws_insn));
3957 need_barrier = rtx_needs_barrier (PATTERN (insn), flags, 0);
c65ebc55 3958
6b6c1201
RH
3959 if (need_barrier)
3960 {
3961 /* PREV_INSN null can happen if the very first insn is a
3962 volatile asm. */
3963 if (prev_insn)
3964 emit_insn_after (gen_insn_group_barrier (), prev_insn);
3965 memcpy (rws_sum, rws_insn, sizeof (rws_sum));
3966 }
c65ebc55 3967
6b6c1201
RH
3968 /* A call must end a group, otherwise the assembler might pack
3969 it in with a following branch and then the function return
3970 goes to the wrong place. Do this unconditionally for
3971 unconditional calls, simply because it (1) looks nicer and
3972 (2) keeps the data structures more accurate for the insns
3973 following the call. */
c65ebc55 3974
6b6c1201
RH
3975 need_barrier = 1;
3976 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
3977 {
3978 rtx next_insn = insn;
3979 do
3980 next_insn = next_nonnote_insn (next_insn);
3981 while (next_insn
3982 && GET_CODE (next_insn) == INSN
3983 && (GET_CODE (PATTERN (next_insn)) == USE
3984 || GET_CODE (PATTERN (next_insn)) == CLOBBER));
3985 if (next_insn && GET_CODE (next_insn) != JUMP_INSN)
3986 need_barrier = 0;
3987 }
3988 if (need_barrier)
3989 {
3990 emit_insn_after (gen_insn_group_barrier (), insn);
3991 memset (rws_sum, 0, sizeof (rws_sum));
3992 prev_insn = NULL_RTX;
c65ebc55
JW
3993 }
3994 break;
6b6c1201 3995
c65ebc55 3996 case JUMP_INSN:
c65ebc55 3997 flags.is_branch = 1;
6b6c1201
RH
3998 /* FALLTHRU */
3999
c65ebc55
JW
4000 case INSN:
4001 if (GET_CODE (PATTERN (insn)) == USE)
4002 /* Don't care about USE "insns"---those are used to
4003 indicate to the optimizer that it shouldn't get rid of
4004 certain operations. */
4005 break;
4006 else
4007 {
e57b9d65
RH
4008 rtx pat = PATTERN (insn);
4009
870f9ec0
RH
4010 /* Ug. Hack hacks hacked elsewhere. */
4011 switch (INSN_CODE (insn))
4012 {
4013 /* We play dependency tricks with the epilogue in order
4014 to get proper schedules. Undo this for dv analysis. */
4015 case CODE_FOR_epilogue_deallocate_stack:
4016 pat = XVECEXP (pat, 0, 0);
4017 break;
4018
4019 /* The pattern we use for br.cloop confuses the code above.
4020 The second element of the vector is representative. */
4021 case CODE_FOR_doloop_end_internal:
4022 pat = XVECEXP (pat, 0, 1);
4023 break;
4024
4025 /* We include ar.unat in the rtl pattern so that sched2
4026 does not move the ar.unat save/restore after/before
4027 a gr spill/fill. However, we special case these
4028 insns based on their unspec number so as to model
4029 their precise ar.unat bit operations. If we pass on
4030 the use/clobber of the whole ar.unat register we'll
4031 waste this effort. */
4032 case CODE_FOR_gr_spill_internal:
4033 case CODE_FOR_gr_restore_internal:
4034 pat = XVECEXP (pat, 0, 0);
4035 break;
4036
4037 default:
4038 break;
4039 }
5527bf14 4040
c65ebc55 4041 memset (rws_insn, 0, sizeof (rws_insn));
e57b9d65 4042 need_barrier |= rtx_needs_barrier (pat, flags, 0);
c65ebc55
JW
4043
4044 /* Check to see if the previous instruction was a volatile
4045 asm. */
4046 if (! need_barrier)
97e242b0 4047 need_barrier = rws_access_regno (REG_VOLATILE, flags, 0);
c65ebc55
JW
4048
4049 if (need_barrier)
4050 {
4051 /* PREV_INSN null can happen if the very first insn is a
4052 volatile asm. */
4053 if (prev_insn)
4054 emit_insn_after (gen_insn_group_barrier (), prev_insn);
4055 memcpy (rws_sum, rws_insn, sizeof (rws_sum));
4056 }
c65ebc55
JW
4057 prev_insn = insn;
4058 }
4059 break;
4060
4061 case BARRIER:
4062 /* A barrier doesn't imply an instruction group boundary. */
4063 break;
4064
4065 case CODE_LABEL:
4066 /* Leave prev_insn alone so the barrier gets generated in front
4067 of the label, if one is needed. */
4068 break;
4069
4070 default:
4071 abort ();
4072 }
4073 }
4074}
4075
3b572406
RH
4076/* Emit pseudo-ops for the assembler to describe predicate relations.
4077 At present this assumes that we only consider predicate pairs to
4078 be mutex, and that the assembler can deduce proper values from
4079 straight-line code. */
4080
4081static void
4082emit_predicate_relation_info (insns)
4083 rtx insns;
4084{
4085 int i;
4086
4087 /* Make sure the CFG and global_live_at_start are correct. */
4088 find_basic_blocks (insns, max_reg_num (), NULL);
4089 life_analysis (insns, NULL, 0);
4090
4091 for (i = n_basic_blocks - 1; i >= 0; --i)
4092 {
4093 basic_block bb = BASIC_BLOCK (i);
4094 int r;
4095 rtx head = bb->head;
4096
4097 /* We only need such notes at code labels. */
4098 if (GET_CODE (head) != CODE_LABEL)
4099 continue;
4100 if (GET_CODE (NEXT_INSN (head)) == NOTE
4101 && NOTE_LINE_NUMBER (NEXT_INSN (head)) == NOTE_INSN_BASIC_BLOCK)
4102 head = NEXT_INSN (head);
4103
4104 for (r = PR_REG (0); r < PR_REG (64); r += 2)
4105 if (REGNO_REG_SET_P (bb->global_live_at_start, r))
4106 {
054451ea
RH
4107 rtx p = gen_rtx_REG (CCmode, r);
4108 rtx n = emit_insn_after (gen_pred_rel_mutex (p), head);
3b572406
RH
4109 if (head == bb->end)
4110 bb->end = n;
4111 head = n;
4112 }
4113 }
4114}
4115
c65ebc55
JW
4116/* Perform machine dependent operations on the rtl chain INSNS. */
4117
4118void
4119ia64_reorg (insns)
4120 rtx insns;
4121{
9b7bf67d
RH
4122 /* If optimizing, we'll have split before scheduling. */
4123 if (optimize == 0)
4124 split_all_insns (0);
4125
3b572406 4126 emit_predicate_relation_info (insns);
c65ebc55
JW
4127 emit_insn_group_barriers (insns);
4128}
4129\f
4130/* Return true if REGNO is used by the epilogue. */
4131
4132int
4133ia64_epilogue_uses (regno)
4134 int regno;
4135{
59da9a7d
JW
4136 /* When a function makes a call through a function descriptor, we
4137 will write a (potentially) new value to "gp". After returning
4138 from such a call, we need to make sure the function restores the
4139 original gp-value, even if the function itself does not use the
4140 gp anymore. */
6b6c1201
RH
4141 if (regno == R_GR (1)
4142 && TARGET_CONST_GP
4143 && !(TARGET_AUTO_PIC || TARGET_NO_PIC))
59da9a7d
JW
4144 return 1;
4145
c65ebc55
JW
4146 /* For functions defined with the syscall_linkage attribute, all input
4147 registers are marked as live at all function exits. This prevents the
4148 register allocator from using the input registers, which in turn makes it
4149 possible to restart a system call after an interrupt without having to
4150 save/restore the input registers. */
4151
4152 if (IN_REGNO_P (regno)
4153 && (regno < IN_REG (current_function_args_info.words))
4154 && lookup_attribute ("syscall_linkage",
4155 TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
4156 return 1;
4157
6b6c1201
RH
4158 /* Conditional return patterns can't represent the use of `b0' as
4159 the return address, so we force the value live this way. */
4160 if (regno == R_BR (0))
4161 return 1;
4162
97e242b0
RH
4163 if (regs_ever_live[AR_LC_REGNUM] && regno == AR_LC_REGNUM)
4164 return 1;
4165 if (! current_function_is_leaf && regno == AR_PFS_REGNUM)
4166 return 1;
4167 if (TEST_HARD_REG_BIT (current_frame_info.mask, AR_UNAT_REGNUM)
4168 && regno == AR_UNAT_REGNUM)
5527bf14
RH
4169 return 1;
4170
c65ebc55
JW
4171 return 0;
4172}
4173
4174/* Return true if IDENTIFIER is a valid attribute for TYPE. */
4175
4176int
4177ia64_valid_type_attribute (type, attributes, identifier, args)
4178 tree type;
4179 tree attributes ATTRIBUTE_UNUSED;
4180 tree identifier;
4181 tree args;
4182{
4183 /* We only support an attribute for function calls. */
4184
4185 if (TREE_CODE (type) != FUNCTION_TYPE
4186 && TREE_CODE (type) != METHOD_TYPE)
4187 return 0;
4188
4189 /* The "syscall_linkage" attribute says the callee is a system call entry
4190 point. This affects ia64_epilogue_uses. */
4191
4192 if (is_attribute_p ("syscall_linkage", identifier))
4193 return args == NULL_TREE;
4194
4195 return 0;
4196}
4197\f
4198/* For ia64, SYMBOL_REF_FLAG set means that it is a function.
4199
4200 We add @ to the name if this goes in small data/bss. We can only put
4201 a variable in small data/bss if it is defined in this module or a module
4202 that we are statically linked with. We can't check the second condition,
4203 but TREE_STATIC gives us the first one. */
4204
4205/* ??? If we had IPA, we could check the second condition. We could support
4206 programmer added section attributes if the variable is not defined in this
4207 module. */
4208
4209/* ??? See the v850 port for a cleaner way to do this. */
4210
4211/* ??? We could also support own long data here. Generating movl/add/ld8
4212 instead of addl,ld8/ld8. This makes the code bigger, but should make the
4213 code faster because there is one less load. This also includes incomplete
4214 types which can't go in sdata/sbss. */
4215
4216/* ??? See select_section. We must put short own readonly variables in
4217 sdata/sbss instead of the more natural rodata, because we can't perform
4218 the DECL_READONLY_SECTION test here. */
4219
4220extern struct obstack * saveable_obstack;
4221
4222void
4223ia64_encode_section_info (decl)
4224 tree decl;
4225{
549f0725
RH
4226 const char *symbol_str;
4227
c65ebc55 4228 if (TREE_CODE (decl) == FUNCTION_DECL)
549f0725
RH
4229 {
4230 SYMBOL_REF_FLAG (XEXP (DECL_RTL (decl), 0)) = 1;
4231 return;
4232 }
4233
4234 /* Careful not to prod global register variables. */
4235 if (TREE_CODE (decl) != VAR_DECL
3b572406
RH
4236 || GET_CODE (DECL_RTL (decl)) != MEM
4237 || GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
549f0725
RH
4238 return;
4239
4240 symbol_str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
4241
c65ebc55
JW
4242 /* We assume that -fpic is used only to create a shared library (dso).
4243 With -fpic, no global data can ever be sdata.
4244 Without -fpic, global common uninitialized data can never be sdata, since
4245 it can unify with a real definition in a dso. */
4246 /* ??? Actually, we can put globals in sdata, as long as we don't use gprel
4247 to access them. The linker may then be able to do linker relaxation to
4248 optimize references to them. Currently sdata implies use of gprel. */
549f0725
RH
4249 if (! TARGET_NO_SDATA
4250 && TREE_STATIC (decl)
4251 && ! (DECL_ONE_ONLY (decl) || DECL_WEAK (decl))
4252 && ! (TREE_PUBLIC (decl)
4253 && (flag_pic
4254 || (DECL_COMMON (decl)
4255 && (DECL_INITIAL (decl) == 0
4256 || DECL_INITIAL (decl) == error_mark_node))))
4257 /* Either the variable must be declared without a section attribute,
4258 or the section must be sdata or sbss. */
4259 && (DECL_SECTION_NAME (decl) == 0
4260 || ! strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
4261 ".sdata")
4262 || ! strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
4263 ".sbss")))
c65ebc55 4264 {
97e242b0 4265 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
c65ebc55 4266
59da9a7d
JW
4267 /* If the variable has already been defined in the output file, then it
4268 is too late to put it in sdata if it wasn't put there in the first
4269 place. The test is here rather than above, because if it is already
4270 in sdata, then it can stay there. */
809d4ef1 4271
549f0725 4272 if (TREE_ASM_WRITTEN (decl))
59da9a7d
JW
4273 ;
4274
c65ebc55
JW
4275 /* If this is an incomplete type with size 0, then we can't put it in
4276 sdata because it might be too big when completed. */
97e242b0
RH
4277 else if (size > 0
4278 && size <= (HOST_WIDE_INT) ia64_section_threshold
549f0725 4279 && symbol_str[0] != SDATA_NAME_FLAG_CHAR)
c65ebc55 4280 {
97e242b0 4281 size_t len = strlen (symbol_str);
549f0725
RH
4282 char *newstr;
4283
4284 if (ggc_p)
4285 newstr = ggc_alloc_string (NULL, len + 1);
4286 else
4287 newstr = obstack_alloc (saveable_obstack, len + 2);
c65ebc55 4288
c65ebc55 4289 *newstr = SDATA_NAME_FLAG_CHAR;
549f0725
RH
4290 memcpy (newstr + 1, symbol_str, len + 1);
4291
c65ebc55
JW
4292 XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
4293 }
809d4ef1 4294 }
32adf8e6
AH
4295 /* This decl is marked as being in small data/bss but it shouldn't
4296 be; one likely explanation for this is that the decl has been
4297 moved into a different section from the one it was in when
4298 ENCODE_SECTION_INFO was first called. Remove the '@'.*/
549f0725 4299 else if (symbol_str[0] == SDATA_NAME_FLAG_CHAR)
32adf8e6 4300 {
549f0725
RH
4301 if (ggc_p)
4302 XSTR (XEXP (DECL_RTL (decl), 0), 0)
4303 = ggc_alloc_string (symbol_str + 1, -1);
4304 else
4305 XSTR (XEXP (DECL_RTL (decl), 0), 0) = symbol_str + 1;
c65ebc55
JW
4306 }
4307}
0c96007e
AM
4308\f
4309/* Output assmebly directives for prologue regions. */
4310
0c96007e
AM
4311/* This function processes a SET pattern looking for specific patterns
4312 which result in emitting an assembly directive required for unwinding. */
97e242b0 4313
0c96007e
AM
4314static int
4315process_set (asm_out_file, pat)
4316 FILE *asm_out_file;
4317 rtx pat;
4318{
4319 rtx src = SET_SRC (pat);
4320 rtx dest = SET_DEST (pat);
97e242b0 4321 int src_regno, dest_regno;
0c96007e 4322
97e242b0
RH
4323 /* Look for the ALLOC insn. */
4324 if (GET_CODE (src) == UNSPEC_VOLATILE
4325 && XINT (src, 1) == 0
4326 && GET_CODE (dest) == REG)
0c96007e 4327 {
97e242b0
RH
4328 dest_regno = REGNO (dest);
4329
4330 /* If this isn't the final destination for ar.pfs, the alloc
4331 shouldn't have been marked frame related. */
4332 if (dest_regno != current_frame_info.reg_save_ar_pfs)
4333 abort ();
4334
809d4ef1 4335 fprintf (asm_out_file, "\t.save ar.pfs, r%d\n",
97e242b0 4336 ia64_dbx_register_number (dest_regno));
0c96007e
AM
4337 return 1;
4338 }
4339
97e242b0 4340 /* Look for SP = .... */
0c96007e
AM
4341 if (GET_CODE (dest) == REG && REGNO (dest) == STACK_POINTER_REGNUM)
4342 {
4343 if (GET_CODE (src) == PLUS)
4344 {
4345 rtx op0 = XEXP (src, 0);
4346 rtx op1 = XEXP (src, 1);
4347 if (op0 == dest && GET_CODE (op1) == CONST_INT)
4348 {
0186257f
JW
4349 if (INTVAL (op1) < 0)
4350 {
4351 fputs ("\t.fframe ", asm_out_file);
4352 fprintf (asm_out_file, HOST_WIDE_INT_PRINT_DEC,
4353 -INTVAL (op1));
4354 fputc ('\n', asm_out_file);
0186257f
JW
4355 }
4356 else
4357 fprintf (asm_out_file, "\t.restore sp\n");
0c96007e 4358 }
0186257f
JW
4359 else
4360 abort ();
0c96007e 4361 }
97e242b0
RH
4362 else if (GET_CODE (src) == REG
4363 && REGNO (src) == HARD_FRAME_POINTER_REGNUM)
0186257f
JW
4364 fprintf (asm_out_file, "\t.restore sp\n");
4365 else
4366 abort ();
4367
4368 return 1;
0c96007e 4369 }
0c96007e
AM
4370
4371 /* Register move we need to look at. */
4372 if (GET_CODE (dest) == REG && GET_CODE (src) == REG)
4373 {
97e242b0
RH
4374 src_regno = REGNO (src);
4375 dest_regno = REGNO (dest);
4376
4377 switch (src_regno)
4378 {
4379 case BR_REG (0):
0c96007e 4380 /* Saving return address pointer. */
97e242b0
RH
4381 if (dest_regno != current_frame_info.reg_save_b0)
4382 abort ();
4383 fprintf (asm_out_file, "\t.save rp, r%d\n",
4384 ia64_dbx_register_number (dest_regno));
4385 return 1;
4386
4387 case PR_REG (0):
4388 if (dest_regno != current_frame_info.reg_save_pr)
4389 abort ();
4390 fprintf (asm_out_file, "\t.save pr, r%d\n",
4391 ia64_dbx_register_number (dest_regno));
4392 return 1;
4393
4394 case AR_UNAT_REGNUM:
4395 if (dest_regno != current_frame_info.reg_save_ar_unat)
4396 abort ();
4397 fprintf (asm_out_file, "\t.save ar.unat, r%d\n",
4398 ia64_dbx_register_number (dest_regno));
4399 return 1;
4400
4401 case AR_LC_REGNUM:
4402 if (dest_regno != current_frame_info.reg_save_ar_lc)
4403 abort ();
4404 fprintf (asm_out_file, "\t.save ar.lc, r%d\n",
4405 ia64_dbx_register_number (dest_regno));
4406 return 1;
4407
4408 case STACK_POINTER_REGNUM:
4409 if (dest_regno != HARD_FRAME_POINTER_REGNUM
4410 || ! frame_pointer_needed)
4411 abort ();
4412 fprintf (asm_out_file, "\t.vframe r%d\n",
4413 ia64_dbx_register_number (dest_regno));
4414 return 1;
4415
4416 default:
4417 /* Everything else should indicate being stored to memory. */
4418 abort ();
0c96007e
AM
4419 }
4420 }
97e242b0
RH
4421
4422 /* Memory store we need to look at. */
4423 if (GET_CODE (dest) == MEM && GET_CODE (src) == REG)
0c96007e 4424 {
97e242b0
RH
4425 long off;
4426 rtx base;
4427 const char *saveop;
4428
4429 if (GET_CODE (XEXP (dest, 0)) == REG)
0c96007e 4430 {
97e242b0
RH
4431 base = XEXP (dest, 0);
4432 off = 0;
0c96007e 4433 }
97e242b0
RH
4434 else if (GET_CODE (XEXP (dest, 0)) == PLUS
4435 && GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT)
0c96007e 4436 {
97e242b0
RH
4437 base = XEXP (XEXP (dest, 0), 0);
4438 off = INTVAL (XEXP (XEXP (dest, 0), 1));
0c96007e 4439 }
97e242b0
RH
4440 else
4441 abort ();
0c96007e 4442
97e242b0
RH
4443 if (base == hard_frame_pointer_rtx)
4444 {
4445 saveop = ".savepsp";
4446 off = - off;
4447 }
4448 else if (base == stack_pointer_rtx)
4449 saveop = ".savesp";
4450 else
4451 abort ();
4452
4453 src_regno = REGNO (src);
4454 switch (src_regno)
4455 {
4456 case BR_REG (0):
4457 if (current_frame_info.reg_save_b0 != 0)
4458 abort ();
4459 fprintf (asm_out_file, "\t%s rp, %ld\n", saveop, off);
4460 return 1;
4461
4462 case PR_REG (0):
4463 if (current_frame_info.reg_save_pr != 0)
4464 abort ();
4465 fprintf (asm_out_file, "\t%s pr, %ld\n", saveop, off);
4466 return 1;
4467
4468 case AR_LC_REGNUM:
4469 if (current_frame_info.reg_save_ar_lc != 0)
4470 abort ();
4471 fprintf (asm_out_file, "\t%s ar.lc, %ld\n", saveop, off);
4472 return 1;
4473
4474 case AR_PFS_REGNUM:
4475 if (current_frame_info.reg_save_ar_pfs != 0)
4476 abort ();
4477 fprintf (asm_out_file, "\t%s ar.pfs, %ld\n", saveop, off);
4478 return 1;
4479
4480 case AR_UNAT_REGNUM:
4481 if (current_frame_info.reg_save_ar_unat != 0)
4482 abort ();
4483 fprintf (asm_out_file, "\t%s ar.unat, %ld\n", saveop, off);
4484 return 1;
4485
4486 case GR_REG (4):
4487 case GR_REG (5):
4488 case GR_REG (6):
4489 case GR_REG (7):
4490 fprintf (asm_out_file, "\t.save.g 0x%x\n",
4491 1 << (src_regno - GR_REG (4)));
97e242b0
RH
4492 return 1;
4493
4494 case BR_REG (1):
4495 case BR_REG (2):
4496 case BR_REG (3):
4497 case BR_REG (4):
4498 case BR_REG (5):
4499 fprintf (asm_out_file, "\t.save.b 0x%x\n",
4500 1 << (src_regno - BR_REG (1)));
0c96007e 4501 return 1;
97e242b0
RH
4502
4503 case FR_REG (2):
4504 case FR_REG (3):
4505 case FR_REG (4):
4506 case FR_REG (5):
4507 fprintf (asm_out_file, "\t.save.f 0x%x\n",
4508 1 << (src_regno - FR_REG (2)));
4509 return 1;
4510
4511 case FR_REG (16): case FR_REG (17): case FR_REG (18): case FR_REG (19):
4512 case FR_REG (20): case FR_REG (21): case FR_REG (22): case FR_REG (23):
4513 case FR_REG (24): case FR_REG (25): case FR_REG (26): case FR_REG (27):
4514 case FR_REG (28): case FR_REG (29): case FR_REG (30): case FR_REG (31):
4515 fprintf (asm_out_file, "\t.save.gf 0x0, 0x%x\n",
4516 1 << (src_regno - FR_REG (12)));
4517 return 1;
4518
4519 default:
4520 return 0;
0c96007e
AM
4521 }
4522 }
97e242b0 4523
0c96007e
AM
4524 return 0;
4525}
4526
4527
4528/* This function looks at a single insn and emits any directives
4529 required to unwind this insn. */
4530void
4531process_for_unwind_directive (asm_out_file, insn)
4532 FILE *asm_out_file;
4533 rtx insn;
4534{
809d4ef1 4535 if ((flag_unwind_tables
0c96007e
AM
4536 || (flag_exceptions && !exceptions_via_longjmp))
4537 && RTX_FRAME_RELATED_P (insn))
4538 {
97e242b0
RH
4539 rtx pat;
4540
4541 pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
4542 if (pat)
4543 pat = XEXP (pat, 0);
4544 else
4545 pat = PATTERN (insn);
0c96007e
AM
4546
4547 switch (GET_CODE (pat))
4548 {
809d4ef1
RH
4549 case SET:
4550 process_set (asm_out_file, pat);
4551 break;
4552
4553 case PARALLEL:
4554 {
4555 int par_index;
4556 int limit = XVECLEN (pat, 0);
4557 for (par_index = 0; par_index < limit; par_index++)
4558 {
4559 rtx x = XVECEXP (pat, 0, par_index);
4560 if (GET_CODE (x) == SET)
4561 process_set (asm_out_file, x);
4562 }
4563 break;
4564 }
4565
4566 default:
4567 abort ();
0c96007e
AM
4568 }
4569 }
4570}
c65ebc55
JW
4571
4572#define def_builtin(name, type, code) \
4573 builtin_function ((name), (type), (code), BUILT_IN_MD, NULL_PTR)
4574
4575struct builtin_description
4576{
4577 enum insn_code icode;
4578 const char *name;
4579 enum ia64_builtins code;
4580 enum rtx_code comparison;
4581 unsigned int flag;
4582};
4583
4584/* All 32 bit intrinsics that take 2 arguments. */
4585static struct builtin_description bdesc_2argsi[] =
4586{
3b572406
RH
4587 { CODE_FOR_fetch_and_add_si, "__sync_fetch_and_add_si",
4588 IA64_BUILTIN_FETCH_AND_ADD_SI, 0, 0 },
4589 { CODE_FOR_fetch_and_sub_si, "__sync_fetch_and_sub_si",
4590 IA64_BUILTIN_FETCH_AND_SUB_SI, 0, 0 },
4591 { CODE_FOR_fetch_and_or_si, "__sync_fetch_and_or_si",
4592 IA64_BUILTIN_FETCH_AND_OR_SI, 0, 0 },
4593 { CODE_FOR_fetch_and_and_si, "__sync_fetch_and_and_si",
4594 IA64_BUILTIN_FETCH_AND_AND_SI, 0, 0 },
4595 { CODE_FOR_fetch_and_xor_si, "__sync_fetch_and_xor_si",
4596 IA64_BUILTIN_FETCH_AND_XOR_SI, 0, 0 },
4597 { CODE_FOR_fetch_and_nand_si, "__sync_fetch_and_nand_si",
4598 IA64_BUILTIN_FETCH_AND_NAND_SI, 0, 0 },
4599 { CODE_FOR_add_and_fetch_si, "__sync_add_and_fetch_si",
4600 IA64_BUILTIN_ADD_AND_FETCH_SI, 0, 0 },
4601 { CODE_FOR_sub_and_fetch_si, "__sync_sub_and_fetch_si",
4602 IA64_BUILTIN_SUB_AND_FETCH_SI, 0, 0 },
4603 { CODE_FOR_or_and_fetch_si, "__sync_or_and_fetch_si",
4604 IA64_BUILTIN_OR_AND_FETCH_SI, 0, 0 },
4605 { CODE_FOR_and_and_fetch_si, "__sync_and_and_fetch_si",
4606 IA64_BUILTIN_AND_AND_FETCH_SI, 0, 0 },
4607 { CODE_FOR_xor_and_fetch_si, "__sync_xor_and_fetch_si",
4608 IA64_BUILTIN_XOR_AND_FETCH_SI, 0, 0 },
4609 { CODE_FOR_nand_and_fetch_si, "__sync_nand_and_fetch_si",
4610 IA64_BUILTIN_NAND_AND_FETCH_SI, 0, 0 }
c65ebc55
JW
4611};
4612
4613/* All 64 bit intrinsics that take 2 arguments. */
4614static struct builtin_description bdesc_2argdi[] =
4615{
3b572406
RH
4616 { CODE_FOR_fetch_and_add_di, "__sync_fetch_and_add_di",
4617 IA64_BUILTIN_FETCH_AND_ADD_DI, 0, 0 },
4618 { CODE_FOR_fetch_and_sub_di, "__sync_fetch_and_sub_di",
4619 IA64_BUILTIN_FETCH_AND_SUB_DI, 0, 0 },
4620 { CODE_FOR_fetch_and_or_di, "__sync_fetch_and_or_di",
4621 IA64_BUILTIN_FETCH_AND_OR_DI, 0, 0 },
4622 { CODE_FOR_fetch_and_and_di, "__sync_fetch_and_and_di",
4623 IA64_BUILTIN_FETCH_AND_AND_DI, 0, 0 },
4624 { CODE_FOR_fetch_and_xor_di, "__sync_fetch_and_xor_di",
4625 IA64_BUILTIN_FETCH_AND_XOR_DI, 0, 0 },
4626 { CODE_FOR_fetch_and_nand_di, "__sync_fetch_and_nand_di",
4627 IA64_BUILTIN_FETCH_AND_NAND_DI, 0, 0 },
4628 { CODE_FOR_add_and_fetch_di, "__sync_add_and_fetch_di",
4629 IA64_BUILTIN_ADD_AND_FETCH_DI, 0, 0 },
4630 { CODE_FOR_sub_and_fetch_di, "__sync_sub_and_fetch_di",
4631 IA64_BUILTIN_SUB_AND_FETCH_DI, 0, 0 },
4632 { CODE_FOR_or_and_fetch_di, "__sync_or_and_fetch_di",
4633 IA64_BUILTIN_OR_AND_FETCH_DI, 0, 0 },
4634 { CODE_FOR_and_and_fetch_di, "__sync_and_and_fetch_di",
4635 IA64_BUILTIN_AND_AND_FETCH_DI, 0, 0 },
4636 { CODE_FOR_xor_and_fetch_di, "__sync_xor_and_fetch_di",
4637 IA64_BUILTIN_XOR_AND_FETCH_DI, 0, 0 },
4638 { CODE_FOR_nand_and_fetch_di, "__sync_nand_and_fetch_di",
4639 IA64_BUILTIN_NAND_AND_FETCH_DI, 0, 0 }
c65ebc55
JW
4640};
4641
4642void
4643ia64_init_builtins ()
4644{
3b572406 4645 size_t i;
c65ebc55
JW
4646
4647 tree psi_type_node = build_pointer_type (integer_type_node);
4648 tree pdi_type_node = build_pointer_type (long_integer_type_node);
4649 tree endlink = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
4650
c65ebc55
JW
4651 /* __sync_val_compare_and_swap_si, __sync_bool_compare_and_swap_si */
4652 tree si_ftype_psi_si_si
4653 = build_function_type (integer_type_node,
4654 tree_cons (NULL_TREE, psi_type_node,
4655 tree_cons (NULL_TREE, integer_type_node,
3b572406
RH
4656 tree_cons (NULL_TREE,
4657 integer_type_node,
c65ebc55
JW
4658 endlink))));
4659
4660 /* __sync_val_compare_and_swap_di, __sync_bool_compare_and_swap_di */
4661 tree di_ftype_pdi_di_di
4662 = build_function_type (long_integer_type_node,
4663 tree_cons (NULL_TREE, pdi_type_node,
3b572406
RH
4664 tree_cons (NULL_TREE,
4665 long_integer_type_node,
4666 tree_cons (NULL_TREE,
4667 long_integer_type_node,
c65ebc55
JW
4668 endlink))));
4669 /* __sync_synchronize */
4670 tree void_ftype_void
4671 = build_function_type (void_type_node, endlink);
4672
4673 /* __sync_lock_test_and_set_si */
4674 tree si_ftype_psi_si
4675 = build_function_type (integer_type_node,
4676 tree_cons (NULL_TREE, psi_type_node,
4677 tree_cons (NULL_TREE, integer_type_node, endlink)));
4678
4679 /* __sync_lock_test_and_set_di */
4680 tree di_ftype_pdi_di
809d4ef1 4681 = build_function_type (long_integer_type_node,
c65ebc55 4682 tree_cons (NULL_TREE, pdi_type_node,
3b572406
RH
4683 tree_cons (NULL_TREE, long_integer_type_node,
4684 endlink)));
c65ebc55
JW
4685
4686 /* __sync_lock_release_si */
4687 tree void_ftype_psi
3b572406
RH
4688 = build_function_type (void_type_node, tree_cons (NULL_TREE, psi_type_node,
4689 endlink));
c65ebc55
JW
4690
4691 /* __sync_lock_release_di */
4692 tree void_ftype_pdi
3b572406
RH
4693 = build_function_type (void_type_node, tree_cons (NULL_TREE, pdi_type_node,
4694 endlink));
c65ebc55 4695
3b572406
RH
4696 def_builtin ("__sync_val_compare_and_swap_si", si_ftype_psi_si_si,
4697 IA64_BUILTIN_VAL_COMPARE_AND_SWAP_SI);
c65ebc55 4698
3b572406
RH
4699 def_builtin ("__sync_val_compare_and_swap_di", di_ftype_pdi_di_di,
4700 IA64_BUILTIN_VAL_COMPARE_AND_SWAP_DI);
c65ebc55 4701
3b572406
RH
4702 def_builtin ("__sync_bool_compare_and_swap_si", si_ftype_psi_si_si,
4703 IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_SI);
c65ebc55 4704
3b572406
RH
4705 def_builtin ("__sync_bool_compare_and_swap_di", di_ftype_pdi_di_di,
4706 IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_DI);
c65ebc55 4707
3b572406
RH
4708 def_builtin ("__sync_synchronize", void_ftype_void,
4709 IA64_BUILTIN_SYNCHRONIZE);
c65ebc55 4710
3b572406
RH
4711 def_builtin ("__sync_lock_test_and_set_si", si_ftype_psi_si,
4712 IA64_BUILTIN_LOCK_TEST_AND_SET_SI);
c65ebc55 4713
3b572406
RH
4714 def_builtin ("__sync_lock_test_and_set_di", di_ftype_pdi_di,
4715 IA64_BUILTIN_LOCK_TEST_AND_SET_DI);
c65ebc55 4716
3b572406
RH
4717 def_builtin ("__sync_lock_release_si", void_ftype_psi,
4718 IA64_BUILTIN_LOCK_RELEASE_SI);
c65ebc55 4719
3b572406
RH
4720 def_builtin ("__sync_lock_release_di", void_ftype_pdi,
4721 IA64_BUILTIN_LOCK_RELEASE_DI);
c65ebc55 4722
3b572406
RH
4723 def_builtin ("__builtin_ia64_bsp",
4724 build_function_type (ptr_type_node, endlink),
4725 IA64_BUILTIN_BSP);
ce152ef8
AM
4726
4727 def_builtin ("__builtin_ia64_flushrs",
4728 build_function_type (void_type_node, endlink),
4729 IA64_BUILTIN_FLUSHRS);
4730
c65ebc55 4731 /* Add all builtins that are operations on two args. */
3b572406
RH
4732 for (i = 0; i < sizeof(bdesc_2argsi) / sizeof *bdesc_2argsi; i++)
4733 def_builtin (bdesc_2argsi[i].name, si_ftype_psi_si, bdesc_2argsi[i].code);
4734 for (i = 0; i < sizeof(bdesc_2argdi) / sizeof *bdesc_2argdi; i++)
4735 def_builtin (bdesc_2argdi[i].name, si_ftype_psi_si, bdesc_2argdi[i].code);
c65ebc55
JW
4736}
4737
4738/* Expand fetch_and_op intrinsics. The basic code sequence is:
4739
4740 mf
4741 ldsz return = [ptr];
4742 tmp = return;
4743 do {
4744 oldval = tmp;
4745 ar.ccv = tmp;
4746 tmp <op>= value;
4747 cmpxchgsz.acq tmp = [ptr], tmp
4748 cmpxchgsz.acq tmp = [ptr], tmp
4749 } while (tmp != oldval)
4750*/
4751void
4752ia64_expand_fetch_and_op (code, mode, operands)
4753 enum fetchop_code code;
4754 enum machine_mode mode;
4755 rtx operands[];
4756{
97e242b0
RH
4757 rtx mfreg = gen_rtx_MEM (BLKmode, gen_rtx_REG (mode, GR_REG (1)));
4758 rtx oldval, newlabel, tmp_reg, ccv;
4759
c65ebc55
JW
4760 emit_insn (gen_mf (mfreg));
4761 tmp_reg = gen_reg_rtx (mode);
4762 oldval = gen_reg_rtx (mode);
97e242b0 4763 ccv = gen_rtx_REG (mode, AR_CCV_REGNUM);
c65ebc55 4764
97e242b0
RH
4765 emit_move_insn (operands[0], operands[1]);
4766 emit_move_insn (tmp_reg, operands[0]);
c65ebc55
JW
4767
4768 newlabel = gen_label_rtx ();
4769 emit_label (newlabel);
97e242b0
RH
4770 emit_move_insn (oldval, tmp_reg);
4771 emit_move_insn (ccv, tmp_reg);
c65ebc55
JW
4772
4773 /* Perform the specific operation. */
4774 switch (code)
4775 {
4776 case IA64_ADD_OP:
4777 {
4778 rtx reg;
4779 if (GET_CODE (operands[2]) == CONST_INT)
4780 reg = gen_reg_rtx (mode);
809d4ef1 4781 else
c65ebc55
JW
4782 reg = operands[2];
4783 if (mode == SImode)
4784 {
4785 if (reg != operands[2])
4786 emit_insn (gen_movsi (reg, operands[2]));
4787 emit_insn (gen_addsi3 (tmp_reg, tmp_reg, reg));
4788 }
4789 else
4790 {
4791 if (reg != operands[2])
4792 emit_insn (gen_movdi (reg, operands[2]));
4793 emit_insn (gen_adddi3 (tmp_reg, tmp_reg, reg));
4794 }
4795 break;
4796 }
4797
4798 case IA64_SUB_OP:
4799 if (mode == SImode)
4800 emit_insn (gen_subsi3 (tmp_reg, tmp_reg, operands[2]));
4801 else
4802 emit_insn (gen_subdi3 (tmp_reg, tmp_reg, operands[2]));
4803 break;
4804
4805 case IA64_OR_OP:
4806 emit_insn (gen_iordi3 (tmp_reg, tmp_reg, operands[2]));
4807 break;
4808
4809 case IA64_AND_OP:
4810 emit_insn (gen_anddi3 (tmp_reg, tmp_reg, operands[2]));
4811 break;
4812
4813 case IA64_XOR_OP:
4814 emit_insn (gen_xordi3 (tmp_reg, tmp_reg, operands[2]));
4815 break;
4816
4817 case IA64_NAND_OP:
4818 emit_insn (gen_anddi3 (tmp_reg, tmp_reg, operands[2]));
4819 if (mode == SImode)
4820 emit_insn (gen_one_cmplsi2 (tmp_reg, operands[0]));
4821 else
4822 emit_insn (gen_one_cmpldi2 (tmp_reg, operands[0]));
4823 break;
4824
4825 default:
4826 break;
4827 }
809d4ef1
RH
4828
4829 if (mode == SImode)
97e242b0 4830 emit_insn (gen_cmpxchg_acq_si (tmp_reg, operands[1], tmp_reg, ccv));
c65ebc55 4831 else
97e242b0 4832 emit_insn (gen_cmpxchg_acq_di (tmp_reg, operands[1], tmp_reg, ccv));
c65ebc55
JW
4833
4834 emit_cmp_and_jump_insns (tmp_reg, oldval, NE, 0, mode, 1, 0, newlabel);
4835}
4836
4837/* Expand op_and_fetch intrinsics. The basic code sequence is:
4838
4839 mf
4840 ldsz return = [ptr];
4841 do {
4842 oldval = tmp;
4843 ar.ccv = tmp;
4844 return = tmp + value;
4845 cmpxchgsz.acq tmp = [ptr], return
4846 } while (tmp != oldval)
4847*/
4848void
4849ia64_expand_op_and_fetch (code, mode, operands)
4850 enum fetchop_code code;
4851 enum machine_mode mode;
4852 rtx operands[];
4853{
97e242b0
RH
4854 rtx mfreg = gen_rtx_MEM (BLKmode, gen_rtx_REG (mode, GR_REG (1)));
4855 rtx oldval, newlabel, tmp_reg, ccv;
c65ebc55
JW
4856
4857 emit_insn (gen_mf (mfreg));
4858 tmp_reg = gen_reg_rtx (mode);
97e242b0
RH
4859 ccv = gen_rtx_REG (mode, AR_CCV_REGNUM);
4860
4861 emit_move_insn (tmp_reg, operands[1]);
c65ebc55
JW
4862
4863 newlabel = gen_label_rtx ();
4864 emit_label (newlabel);
4865 oldval = gen_reg_rtx (mode);
97e242b0
RH
4866 emit_move_insn (oldval, tmp_reg);
4867 emit_move_insn (ccv, tmp_reg);
c65ebc55
JW
4868
4869 /* Perform the specific operation. */
4870 switch (code)
4871 {
4872 case IA64_ADD_OP:
4873 if (mode == SImode)
4874 emit_insn (gen_addsi3 (operands[0], tmp_reg, operands[2]));
4875 else
4876 emit_insn (gen_adddi3 (operands[0], tmp_reg, operands[2]));
4877 break;
4878
4879 case IA64_SUB_OP:
4880 if (mode == SImode)
4881 emit_insn (gen_subsi3 (operands[0], tmp_reg, operands[2]));
4882 else
4883 emit_insn (gen_subdi3 (operands[0], tmp_reg, operands[2]));
4884 break;
4885
4886 case IA64_OR_OP:
4887 emit_insn (gen_iordi3 (operands[0], tmp_reg, operands[2]));
4888 break;
4889
4890 case IA64_AND_OP:
4891 emit_insn (gen_anddi3 (operands[0], tmp_reg, operands[2]));
4892 break;
4893
4894 case IA64_XOR_OP:
4895 emit_insn (gen_xordi3 (operands[0], tmp_reg, operands[2]));
4896 break;
4897
4898 case IA64_NAND_OP:
4899 emit_insn (gen_anddi3 (operands[0], tmp_reg, operands[2]));
4900 if (mode == SImode)
4901 emit_insn (gen_one_cmplsi2 (operands[0], operands[0]));
4902 else
4903 emit_insn (gen_one_cmpldi2 (operands[0], operands[0]));
4904 break;
4905
4906 default:
4907 break;
4908 }
809d4ef1
RH
4909
4910 if (mode == SImode)
97e242b0 4911 emit_insn (gen_cmpxchg_acq_si (tmp_reg, operands[1], operands[0], ccv));
c65ebc55 4912 else
97e242b0 4913 emit_insn (gen_cmpxchg_acq_di (tmp_reg, operands[1], operands[0], ccv));
c65ebc55
JW
4914
4915 emit_cmp_and_jump_insns (tmp_reg, oldval, NE, 0, mode, 1, 0, newlabel);
4916}
4917
4918/* Expand val_ and bool_compare_and_swap. For val_ we want:
4919
4920 ar.ccv = oldval
4921 mf
4922 cmpxchgsz.acq ret = [ptr], newval, ar.ccv
4923 return ret
4924
4925 For bool_ it's the same except return ret == oldval.
4926*/
4927static rtx
4928ia64_expand_compare_and_swap (icode, arglist, target, boolcode)
4929 enum insn_code icode;
4930 tree arglist;
4931 rtx target;
4932 int boolcode;
4933{
4934 tree arg0, arg1, arg2;
809d4ef1 4935 rtx op0, op1, op2, pat;
c65ebc55 4936 enum machine_mode tmode, mode0, mode1, mode2;
809d4ef1 4937
c65ebc55
JW
4938 arg0 = TREE_VALUE (arglist);
4939 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4940 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4941 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4942 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4943 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
4944 tmode = insn_data[icode].operand[0].mode;
4945 mode0 = insn_data[icode].operand[1].mode;
4946 mode1 = insn_data[icode].operand[2].mode;
4947 mode2 = insn_data[icode].operand[3].mode;
4948
4949 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
c65ebc55
JW
4950 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
4951 op1 = copy_to_mode_reg (mode1, op1);
4952 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
4953 op2 = copy_to_mode_reg (mode2, op2);
4954 if (target == 0
4955 || GET_MODE (target) != tmode
4956 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4957 target = gen_reg_rtx (tmode);
4958
4959 pat = GEN_FCN (icode) (target, op0, op1, op2);
4960 if (! pat)
4961 return 0;
4962 emit_insn (pat);
4963 if (boolcode)
4964 {
4965 if (tmode == SImode)
4966 {
4967 emit_insn (gen_cmpsi (target, op1));
4968 emit_insn (gen_seq (gen_lowpart (DImode, target)));
4969 }
4970 else
4971 {
4972 emit_insn (gen_cmpdi (target, op1));
4973 emit_insn (gen_seq (target));
4974 }
4975 }
4976 return target;
4977}
4978
4979/* Expand all intrinsics that take 2 arguments. */
4980static rtx
4981ia64_expand_binop_builtin (icode, arglist, target)
4982 enum insn_code icode;
4983 tree arglist;
4984 rtx target;
4985{
4986 rtx pat;
4987 tree arg0 = TREE_VALUE (arglist);
4988 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4989 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4990 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4991 enum machine_mode tmode = insn_data[icode].operand[0].mode;
4992 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
4993 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
4994
4995 if (! target
4996 || GET_MODE (target) != tmode
4997 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4998 target = gen_reg_rtx (tmode);
4999
5000 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
5001 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5002 op1 = copy_to_mode_reg (mode1, op1);
5003
5004 pat = GEN_FCN (icode) (target, op0, op1);
5005 if (! pat)
5006 return 0;
5007 emit_insn (pat);
5008 return target;
5009}
5010
5011rtx
5012ia64_expand_builtin (exp, target, subtarget, mode, ignore)
5013 tree exp;
5014 rtx target;
fd7c34b0
RH
5015 rtx subtarget ATTRIBUTE_UNUSED;
5016 enum machine_mode mode ATTRIBUTE_UNUSED;
5017 int ignore ATTRIBUTE_UNUSED;
c65ebc55 5018{
809d4ef1 5019 rtx op0, op1, pat;
c65ebc55 5020 rtx tmp_reg;
809d4ef1 5021 tree arg0, arg1;
c65ebc55
JW
5022 tree arglist = TREE_OPERAND (exp, 1);
5023 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
97e242b0 5024 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
809d4ef1 5025 enum machine_mode tmode, mode0, mode1;
c65ebc55 5026 enum insn_code icode;
3b572406 5027 size_t i;
c65ebc55
JW
5028 struct builtin_description *d;
5029
5030 switch (fcode)
5031 {
5032 case IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_SI:
809d4ef1
RH
5033 return ia64_expand_compare_and_swap (CODE_FOR_val_compare_and_swap_si,
5034 arglist, target, 1);
5035
c65ebc55 5036 case IA64_BUILTIN_VAL_COMPARE_AND_SWAP_SI:
809d4ef1
RH
5037 return ia64_expand_compare_and_swap (CODE_FOR_val_compare_and_swap_si,
5038 arglist, target, 0);
5039
c65ebc55 5040 case IA64_BUILTIN_BOOL_COMPARE_AND_SWAP_DI:
809d4ef1
RH
5041 return ia64_expand_compare_and_swap (CODE_FOR_val_compare_and_swap_di,
5042 arglist, target, 1);
5043
c65ebc55 5044 case IA64_BUILTIN_VAL_COMPARE_AND_SWAP_DI:
809d4ef1
RH
5045 return ia64_expand_compare_and_swap (CODE_FOR_val_compare_and_swap_di,
5046 arglist, target, 0);
5047
c65ebc55
JW
5048 case IA64_BUILTIN_SYNCHRONIZE:
5049 /* Pass a volatile memory operand. */
5050 tmp_reg = gen_rtx_REG (DImode, GR_REG(0));
5051 target = gen_rtx_MEM (BLKmode, tmp_reg);
5052 emit_insn (gen_mf (target));
3b572406 5053 return const0_rtx;
c65ebc55
JW
5054
5055 case IA64_BUILTIN_LOCK_TEST_AND_SET_SI:
5056 icode = CODE_FOR_lock_test_and_set_si;
5057 arg0 = TREE_VALUE (arglist);
5058 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5059 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5060 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5061 tmode = insn_data[icode].operand[0].mode;
5062 mode0 = insn_data[icode].operand[1].mode;
5063 mode1 = insn_data[icode].operand[2].mode;
5064 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
c65ebc55
JW
5065 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5066 op1 = copy_to_mode_reg (mode1, op1);
5067 if (target == 0
5068 || GET_MODE (target) != tmode
5069 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5070 target = gen_reg_rtx (tmode);
5071 pat = GEN_FCN (icode) (target, op0, op1);
5072 if (! pat)
5073 return 0;
5074 emit_insn (pat);
5075 return target;
5076
5077 case IA64_BUILTIN_LOCK_TEST_AND_SET_DI:
5078 icode = CODE_FOR_lock_test_and_set_di;
5079 arg0 = TREE_VALUE (arglist);
5080 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5081 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5082 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5083 tmode = insn_data[icode].operand[0].mode;
5084 mode0 = insn_data[icode].operand[1].mode;
5085 mode1 = insn_data[icode].operand[2].mode;
5086 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
c65ebc55
JW
5087 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5088 op1 = copy_to_mode_reg (mode1, op1);
5089 if (target == 0
5090 || GET_MODE (target) != tmode
5091 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5092 target = gen_reg_rtx (tmode);
5093 pat = GEN_FCN (icode) (target, op0, op1);
5094 if (! pat)
5095 return 0;
5096 emit_insn (pat);
5097 return target;
5098
5099 case IA64_BUILTIN_LOCK_RELEASE_SI:
5100 arg0 = TREE_VALUE (arglist);
5101 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5102 op0 = gen_rtx_MEM (SImode, copy_to_mode_reg (Pmode, op0));
5103 MEM_VOLATILE_P (op0) = 1;
5104 emit_insn (gen_movsi (op0, GEN_INT(0)));
3b572406 5105 return const0_rtx;
c65ebc55
JW
5106
5107 case IA64_BUILTIN_LOCK_RELEASE_DI:
5108 arg0 = TREE_VALUE (arglist);
5109 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5110 op0 = gen_rtx_MEM (DImode, copy_to_mode_reg (Pmode, op0));
5111 MEM_VOLATILE_P (op0) = 1;
5112 emit_insn (gen_movdi (op0, GEN_INT(0)));
3b572406 5113 return const0_rtx;
c65ebc55 5114
ce152ef8
AM
5115 case IA64_BUILTIN_BSP:
5116 {
5117 rtx reg = gen_reg_rtx (DImode);
5118 emit_insn (gen_bsp_value (reg));
5119 return reg;
5120 }
5121
5122 case IA64_BUILTIN_FLUSHRS:
3b572406
RH
5123 emit_insn (gen_flushrs ());
5124 return const0_rtx;
ce152ef8 5125
c65ebc55
JW
5126 default:
5127 break;
5128 }
5129
5130 /* Expand all 32 bit intrinsics that take 2 arguments. */
5131 for (i=0, d = bdesc_2argsi; i < sizeof (bdesc_2argsi) / sizeof *d; i++, d++)
5132 if (d->code == fcode)
5133 return ia64_expand_binop_builtin (d->icode, arglist, target);
5134
5135 /* Expand all 64 bit intrinsics that take 2 arguments. */
5136 for (i=0, d = bdesc_2argdi; i < sizeof (bdesc_2argdi) / sizeof *d; i++, d++)
5137 if (d->code == fcode)
5138 return ia64_expand_binop_builtin (d->icode, arglist, target);
5139
809d4ef1 5140 return 0;
c65ebc55 5141}
This page took 0.716728 seconds and 5 git commands to generate.