s390.c (s390_emit_epilogue): Recognize more cases where register 14 will be saved.
[gcc.git] / gcc / config / s390 / s390.c
1 /* Subroutines used for code generation on IBM S/390 and zSeries
2 Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Hartmut Penner (hpenner@de.ibm.com) and
4 Ulrich Weigand (uweigand@de.ibm.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "tm_p.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "real.h"
33 #include "insn-config.h"
34 #include "conditions.h"
35 #include "output.h"
36 #include "insn-attr.h"
37 #include "flags.h"
38 #include "except.h"
39 #include "function.h"
40 #include "recog.h"
41 #include "expr.h"
42 #include "reload.h"
43 #include "toplev.h"
44 #include "basic-block.h"
45 #include "integrate.h"
46 #include "ggc.h"
47 #include "target.h"
48 #include "target-def.h"
49 #include "debug.h"
50 #include "langhooks.h"
51 #include "optabs.h"
52
53 /* Machine-specific symbol_ref flags. */
54 #define SYMBOL_FLAG_ALIGN1 (SYMBOL_FLAG_MACH_DEP << 0)
55
56
57 static bool s390_assemble_integer (rtx, unsigned int, int);
58 static void s390_select_rtx_section (enum machine_mode, rtx,
59 unsigned HOST_WIDE_INT);
60 static void s390_encode_section_info (tree, rtx, int);
61 static bool s390_cannot_force_const_mem (rtx);
62 static rtx s390_delegitimize_address (rtx);
63 static bool s390_return_in_memory (tree, tree);
64 static void s390_init_builtins (void);
65 static rtx s390_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
66 static void s390_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
67 HOST_WIDE_INT, tree);
68 static enum attr_type s390_safe_attr_type (rtx);
69
70 static int s390_adjust_cost (rtx, rtx, rtx, int);
71 static int s390_adjust_priority (rtx, int);
72 static int s390_issue_rate (void);
73 static int s390_use_dfa_pipeline_interface (void);
74 static int s390_first_cycle_multipass_dfa_lookahead (void);
75 static int s390_sched_reorder2 (FILE *, int, rtx *, int *, int);
76 static bool s390_rtx_costs (rtx, int, int, int *);
77 static int s390_address_cost (rtx);
78 static void s390_reorg (void);
79 static bool s390_valid_pointer_mode (enum machine_mode);
80 static tree s390_build_builtin_va_list (void);
81
82 #undef TARGET_ASM_ALIGNED_HI_OP
83 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
84 #undef TARGET_ASM_ALIGNED_DI_OP
85 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
86 #undef TARGET_ASM_INTEGER
87 #define TARGET_ASM_INTEGER s390_assemble_integer
88
89 #undef TARGET_ASM_OPEN_PAREN
90 #define TARGET_ASM_OPEN_PAREN ""
91
92 #undef TARGET_ASM_CLOSE_PAREN
93 #define TARGET_ASM_CLOSE_PAREN ""
94
95 #undef TARGET_ASM_SELECT_RTX_SECTION
96 #define TARGET_ASM_SELECT_RTX_SECTION s390_select_rtx_section
97
98 #undef TARGET_ENCODE_SECTION_INFO
99 #define TARGET_ENCODE_SECTION_INFO s390_encode_section_info
100
101 #ifdef HAVE_AS_TLS
102 #undef TARGET_HAVE_TLS
103 #define TARGET_HAVE_TLS true
104 #endif
105 #undef TARGET_CANNOT_FORCE_CONST_MEM
106 #define TARGET_CANNOT_FORCE_CONST_MEM s390_cannot_force_const_mem
107
108 #undef TARGET_DELEGITIMIZE_ADDRESS
109 #define TARGET_DELEGITIMIZE_ADDRESS s390_delegitimize_address
110
111 #undef TARGET_RETURN_IN_MEMORY
112 #define TARGET_RETURN_IN_MEMORY s390_return_in_memory
113
114 #undef TARGET_INIT_BUILTINS
115 #define TARGET_INIT_BUILTINS s390_init_builtins
116 #undef TARGET_EXPAND_BUILTIN
117 #define TARGET_EXPAND_BUILTIN s390_expand_builtin
118
119 #undef TARGET_ASM_OUTPUT_MI_THUNK
120 #define TARGET_ASM_OUTPUT_MI_THUNK s390_output_mi_thunk
121 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
122 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
123
124 #undef TARGET_SCHED_ADJUST_COST
125 #define TARGET_SCHED_ADJUST_COST s390_adjust_cost
126 #undef TARGET_SCHED_ADJUST_PRIORITY
127 #define TARGET_SCHED_ADJUST_PRIORITY s390_adjust_priority
128 #undef TARGET_SCHED_ISSUE_RATE
129 #define TARGET_SCHED_ISSUE_RATE s390_issue_rate
130 #undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE
131 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE s390_use_dfa_pipeline_interface
132 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
133 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD s390_first_cycle_multipass_dfa_lookahead
134 #undef TARGET_SCHED_REORDER2
135 #define TARGET_SCHED_REORDER2 s390_sched_reorder2
136
137 #undef TARGET_RTX_COSTS
138 #define TARGET_RTX_COSTS s390_rtx_costs
139 #undef TARGET_ADDRESS_COST
140 #define TARGET_ADDRESS_COST s390_address_cost
141 #undef TARGET_DIRECT_POOL_LOAD_P
142 #define TARGET_DIRECT_POOL_LOAD_P hook_bool_machine_mode_true
143
144 #undef TARGET_MACHINE_DEPENDENT_REORG
145 #define TARGET_MACHINE_DEPENDENT_REORG s390_reorg
146
147 #undef TARGET_VALID_POINTER_MODE
148 #define TARGET_VALID_POINTER_MODE s390_valid_pointer_mode
149
150 #undef TARGET_BUILD_BUILTIN_VA_LIST
151 #define TARGET_BUILD_BUILTIN_VA_LIST s390_build_builtin_va_list
152
153 struct gcc_target targetm = TARGET_INITIALIZER;
154
155 extern int reload_completed;
156
157 /* The alias set for prologue/epilogue register save/restore. */
158 static int s390_sr_alias_set = 0;
159
160 /* Save information from a "cmpxx" operation until the branch or scc is
161 emitted. */
162 rtx s390_compare_op0, s390_compare_op1;
163
164 /* Structure used to hold the components of a S/390 memory
165 address. A legitimate address on S/390 is of the general
166 form
167 base + index + displacement
168 where any of the components is optional.
169
170 base and index are registers of the class ADDR_REGS,
171 displacement is an unsigned 12-bit immediate constant. */
172
173 struct s390_address
174 {
175 rtx base;
176 rtx indx;
177 rtx disp;
178 int pointer;
179 };
180
181 /* Which cpu are we tuning for. */
182 enum processor_type s390_tune;
183 enum processor_flags s390_tune_flags;
184 /* Which instruction set architecture to use. */
185 enum processor_type s390_arch;
186 enum processor_flags s390_arch_flags;
187
188 /* Strings to hold which cpu and instruction set architecture to use. */
189 const char *s390_tune_string; /* for -mtune=<xxx> */
190 const char *s390_arch_string; /* for -march=<xxx> */
191
192 /* Define the structure for the machine field in struct function. */
193
194 struct machine_function GTY(())
195 {
196 /* Set, if some of the fprs 8-15 need to be saved (64 bit abi). */
197 int save_fprs_p;
198
199 /* Set if return address needs to be saved. */
200 bool save_return_addr_p;
201
202 /* Number of first and last gpr to be saved, restored. */
203 int first_save_gpr;
204 int first_restore_gpr;
205 int last_save_gpr;
206
207 /* Size of stack frame. */
208 HOST_WIDE_INT frame_size;
209
210 /* Some local-dynamic TLS symbol name. */
211 const char *some_ld_name;
212 };
213
214 static int s390_match_ccmode_set (rtx, enum machine_mode);
215 static int s390_branch_condition_mask (rtx);
216 static const char *s390_branch_condition_mnemonic (rtx, int);
217 static int check_mode (rtx, enum machine_mode *);
218 static int general_s_operand (rtx, enum machine_mode, int);
219 static int s390_short_displacement (rtx);
220 static int s390_decompose_address (rtx, struct s390_address *);
221 static rtx get_thread_pointer (void);
222 static rtx legitimize_tls_address (rtx, rtx);
223 static void print_shift_count_operand (FILE *, rtx);
224 static const char *get_some_local_dynamic_name (void);
225 static int get_some_local_dynamic_name_1 (rtx *, void *);
226 static int reg_used_in_mem_p (int, rtx);
227 static int addr_generation_dependency_p (rtx, rtx);
228 static int s390_split_branches (void);
229 static void find_constant_pool_ref (rtx, rtx *);
230 static void replace_constant_pool_ref (rtx *, rtx, rtx);
231 static rtx find_ltrel_base (rtx);
232 static void replace_ltrel_base (rtx *, rtx);
233 static void s390_optimize_prolog (bool);
234 static int find_unused_clobbered_reg (void);
235 static void s390_frame_info (void);
236 static rtx save_fpr (rtx, int, int);
237 static rtx restore_fpr (rtx, int, int);
238 static rtx save_gprs (rtx, int, int, int);
239 static rtx restore_gprs (rtx, int, int, int);
240 static int s390_function_arg_size (enum machine_mode, tree);
241 static bool s390_function_arg_float (enum machine_mode, tree);
242 static struct machine_function * s390_init_machine_status (void);
243
244 /* Check whether integer displacement is in range. */
245 #define DISP_IN_RANGE(d) \
246 (TARGET_LONG_DISPLACEMENT? ((d) >= -524288 && (d) <= 524287) \
247 : ((d) >= 0 && (d) <= 4095))
248
249 /* Return true if SET either doesn't set the CC register, or else
250 the source and destination have matching CC modes and that
251 CC mode is at least as constrained as REQ_MODE. */
252
253 static int
254 s390_match_ccmode_set (rtx set, enum machine_mode req_mode)
255 {
256 enum machine_mode set_mode;
257
258 if (GET_CODE (set) != SET)
259 abort ();
260
261 if (GET_CODE (SET_DEST (set)) != REG || !CC_REGNO_P (REGNO (SET_DEST (set))))
262 return 1;
263
264 set_mode = GET_MODE (SET_DEST (set));
265 switch (set_mode)
266 {
267 case CCSmode:
268 case CCSRmode:
269 case CCUmode:
270 case CCURmode:
271 case CCLmode:
272 case CCL1mode:
273 case CCL2mode:
274 case CCT1mode:
275 case CCT2mode:
276 case CCT3mode:
277 if (req_mode != set_mode)
278 return 0;
279 break;
280
281 case CCZmode:
282 if (req_mode != CCSmode && req_mode != CCUmode && req_mode != CCTmode
283 && req_mode != CCSRmode && req_mode != CCURmode)
284 return 0;
285 break;
286
287 case CCAPmode:
288 case CCANmode:
289 if (req_mode != CCAmode)
290 return 0;
291 break;
292
293 default:
294 abort ();
295 }
296
297 return (GET_MODE (SET_SRC (set)) == set_mode);
298 }
299
300 /* Return true if every SET in INSN that sets the CC register
301 has source and destination with matching CC modes and that
302 CC mode is at least as constrained as REQ_MODE.
303 If REQ_MODE is VOIDmode, always return false. */
304
305 int
306 s390_match_ccmode (rtx insn, enum machine_mode req_mode)
307 {
308 int i;
309
310 /* s390_tm_ccmode returns VOIDmode to indicate failure. */
311 if (req_mode == VOIDmode)
312 return 0;
313
314 if (GET_CODE (PATTERN (insn)) == SET)
315 return s390_match_ccmode_set (PATTERN (insn), req_mode);
316
317 if (GET_CODE (PATTERN (insn)) == PARALLEL)
318 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
319 {
320 rtx set = XVECEXP (PATTERN (insn), 0, i);
321 if (GET_CODE (set) == SET)
322 if (!s390_match_ccmode_set (set, req_mode))
323 return 0;
324 }
325
326 return 1;
327 }
328
329 /* If a test-under-mask instruction can be used to implement
330 (compare (and ... OP1) OP2), return the CC mode required
331 to do that. Otherwise, return VOIDmode.
332 MIXED is true if the instruction can distinguish between
333 CC1 and CC2 for mixed selected bits (TMxx), it is false
334 if the instruction cannot (TM). */
335
336 enum machine_mode
337 s390_tm_ccmode (rtx op1, rtx op2, int mixed)
338 {
339 int bit0, bit1;
340
341 /* ??? Fixme: should work on CONST_DOUBLE as well. */
342 if (GET_CODE (op1) != CONST_INT || GET_CODE (op2) != CONST_INT)
343 return VOIDmode;
344
345 /* Selected bits all zero: CC0. */
346 if (INTVAL (op2) == 0)
347 return CCTmode;
348
349 /* Selected bits all one: CC3. */
350 if (INTVAL (op2) == INTVAL (op1))
351 return CCT3mode;
352
353 /* Exactly two bits selected, mixed zeroes and ones: CC1 or CC2. */
354 if (mixed)
355 {
356 bit1 = exact_log2 (INTVAL (op2));
357 bit0 = exact_log2 (INTVAL (op1) ^ INTVAL (op2));
358 if (bit0 != -1 && bit1 != -1)
359 return bit0 > bit1 ? CCT1mode : CCT2mode;
360 }
361
362 return VOIDmode;
363 }
364
365 /* Given a comparison code OP (EQ, NE, etc.) and the operands
366 OP0 and OP1 of a COMPARE, return the mode to be used for the
367 comparison. */
368
369 enum machine_mode
370 s390_select_ccmode (enum rtx_code code, rtx op0, rtx op1)
371 {
372 switch (code)
373 {
374 case EQ:
375 case NE:
376 if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
377 && CONST_OK_FOR_LETTER_P (INTVAL (XEXP (op0, 1)), 'K'))
378 return CCAPmode;
379 if ((GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
380 || GET_CODE (op1) == NEG)
381 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
382 return CCLmode;
383
384 if (GET_CODE (op0) == AND)
385 {
386 /* Check whether we can potentially do it via TM. */
387 enum machine_mode ccmode;
388 ccmode = s390_tm_ccmode (XEXP (op0, 1), op1, 1);
389 if (ccmode != VOIDmode)
390 {
391 /* Relax CCTmode to CCZmode to allow fall-back to AND
392 if that turns out to be beneficial. */
393 return ccmode == CCTmode ? CCZmode : ccmode;
394 }
395 }
396
397 if (register_operand (op0, HImode)
398 && GET_CODE (op1) == CONST_INT
399 && (INTVAL (op1) == -1 || INTVAL (op1) == 65535))
400 return CCT3mode;
401 if (register_operand (op0, QImode)
402 && GET_CODE (op1) == CONST_INT
403 && (INTVAL (op1) == -1 || INTVAL (op1) == 255))
404 return CCT3mode;
405
406 return CCZmode;
407
408 case LE:
409 case LT:
410 case GE:
411 case GT:
412 if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
413 && CONST_OK_FOR_LETTER_P (INTVAL (XEXP (op0, 1)), 'K'))
414 {
415 if (INTVAL (XEXP((op0), 1)) < 0)
416 return CCANmode;
417 else
418 return CCAPmode;
419 }
420 case UNORDERED:
421 case ORDERED:
422 case UNEQ:
423 case UNLE:
424 case UNLT:
425 case UNGE:
426 case UNGT:
427 case LTGT:
428 if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
429 && GET_CODE (op1) != CONST_INT)
430 return CCSRmode;
431 return CCSmode;
432
433 case LTU:
434 case GEU:
435 if (GET_CODE (op0) == PLUS
436 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
437 return CCL1mode;
438
439 if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
440 && GET_CODE (op1) != CONST_INT)
441 return CCURmode;
442 return CCUmode;
443
444 case LEU:
445 case GTU:
446 if (GET_CODE (op0) == MINUS
447 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
448 return CCL2mode;
449
450 if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
451 && GET_CODE (op1) != CONST_INT)
452 return CCURmode;
453 return CCUmode;
454
455 default:
456 abort ();
457 }
458 }
459
460 /* Return nonzero if OP is a valid comparison operator
461 for an ALC condition in mode MODE. */
462
463 int
464 s390_alc_comparison (rtx op, enum machine_mode mode)
465 {
466 if (mode != VOIDmode && mode != GET_MODE (op))
467 return 0;
468
469 if (GET_RTX_CLASS (GET_CODE (op)) != '<')
470 return 0;
471
472 if (GET_CODE (XEXP (op, 0)) != REG
473 || REGNO (XEXP (op, 0)) != CC_REGNUM
474 || XEXP (op, 1) != const0_rtx)
475 return 0;
476
477 switch (GET_MODE (XEXP (op, 0)))
478 {
479 case CCL1mode:
480 return GET_CODE (op) == LTU;
481
482 case CCL2mode:
483 return GET_CODE (op) == LEU;
484
485 case CCUmode:
486 return GET_CODE (op) == GTU;
487
488 case CCURmode:
489 return GET_CODE (op) == LTU;
490
491 case CCSmode:
492 return GET_CODE (op) == UNGT;
493
494 case CCSRmode:
495 return GET_CODE (op) == UNLT;
496
497 default:
498 return 0;
499 }
500 }
501
502 /* Return nonzero if OP is a valid comparison operator
503 for an SLB condition in mode MODE. */
504
505 int
506 s390_slb_comparison (rtx op, enum machine_mode mode)
507 {
508 if (mode != VOIDmode && mode != GET_MODE (op))
509 return 0;
510
511 if (GET_RTX_CLASS (GET_CODE (op)) != '<')
512 return 0;
513
514 if (GET_CODE (XEXP (op, 0)) != REG
515 || REGNO (XEXP (op, 0)) != CC_REGNUM
516 || XEXP (op, 1) != const0_rtx)
517 return 0;
518
519 switch (GET_MODE (XEXP (op, 0)))
520 {
521 case CCL1mode:
522 return GET_CODE (op) == GEU;
523
524 case CCL2mode:
525 return GET_CODE (op) == GTU;
526
527 case CCUmode:
528 return GET_CODE (op) == LEU;
529
530 case CCURmode:
531 return GET_CODE (op) == GEU;
532
533 case CCSmode:
534 return GET_CODE (op) == LE;
535
536 case CCSRmode:
537 return GET_CODE (op) == GE;
538
539 default:
540 return 0;
541 }
542 }
543
544 /* Return branch condition mask to implement a branch
545 specified by CODE. */
546
547 static int
548 s390_branch_condition_mask (rtx code)
549 {
550 const int CC0 = 1 << 3;
551 const int CC1 = 1 << 2;
552 const int CC2 = 1 << 1;
553 const int CC3 = 1 << 0;
554
555 if (GET_CODE (XEXP (code, 0)) != REG
556 || REGNO (XEXP (code, 0)) != CC_REGNUM
557 || XEXP (code, 1) != const0_rtx)
558 abort ();
559
560 switch (GET_MODE (XEXP (code, 0)))
561 {
562 case CCZmode:
563 switch (GET_CODE (code))
564 {
565 case EQ: return CC0;
566 case NE: return CC1 | CC2 | CC3;
567 default:
568 abort ();
569 }
570 break;
571
572 case CCT1mode:
573 switch (GET_CODE (code))
574 {
575 case EQ: return CC1;
576 case NE: return CC0 | CC2 | CC3;
577 default:
578 abort ();
579 }
580 break;
581
582 case CCT2mode:
583 switch (GET_CODE (code))
584 {
585 case EQ: return CC2;
586 case NE: return CC0 | CC1 | CC3;
587 default:
588 abort ();
589 }
590 break;
591
592 case CCT3mode:
593 switch (GET_CODE (code))
594 {
595 case EQ: return CC3;
596 case NE: return CC0 | CC1 | CC2;
597 default:
598 abort ();
599 }
600 break;
601
602 case CCLmode:
603 switch (GET_CODE (code))
604 {
605 case EQ: return CC0 | CC2;
606 case NE: return CC1 | CC3;
607 default:
608 abort ();
609 }
610 break;
611
612 case CCL1mode:
613 switch (GET_CODE (code))
614 {
615 case LTU: return CC2 | CC3; /* carry */
616 case GEU: return CC0 | CC1; /* no carry */
617 default:
618 abort ();
619 }
620 break;
621
622 case CCL2mode:
623 switch (GET_CODE (code))
624 {
625 case GTU: return CC0 | CC1; /* borrow */
626 case LEU: return CC2 | CC3; /* no borrow */
627 default:
628 abort ();
629 }
630 break;
631
632 case CCUmode:
633 switch (GET_CODE (code))
634 {
635 case EQ: return CC0;
636 case NE: return CC1 | CC2 | CC3;
637 case LTU: return CC1;
638 case GTU: return CC2;
639 case LEU: return CC0 | CC1;
640 case GEU: return CC0 | CC2;
641 default:
642 abort ();
643 }
644 break;
645
646 case CCURmode:
647 switch (GET_CODE (code))
648 {
649 case EQ: return CC0;
650 case NE: return CC2 | CC1 | CC3;
651 case LTU: return CC2;
652 case GTU: return CC1;
653 case LEU: return CC0 | CC2;
654 case GEU: return CC0 | CC1;
655 default:
656 abort ();
657 }
658 break;
659
660 case CCAPmode:
661 switch (GET_CODE (code))
662 {
663 case EQ: return CC0;
664 case NE: return CC1 | CC2 | CC3;
665 case LT: return CC1 | CC3;
666 case GT: return CC2;
667 case LE: return CC0 | CC1 | CC3;
668 case GE: return CC0 | CC2;
669 default:
670 abort ();
671 }
672 break;
673
674 case CCANmode:
675 switch (GET_CODE (code))
676 {
677 case EQ: return CC0;
678 case NE: return CC1 | CC2 | CC3;
679 case LT: return CC1;
680 case GT: return CC2 | CC3;
681 case LE: return CC0 | CC1;
682 case GE: return CC0 | CC2 | CC3;
683 default:
684 abort ();
685 }
686 break;
687
688 case CCSmode:
689 switch (GET_CODE (code))
690 {
691 case EQ: return CC0;
692 case NE: return CC1 | CC2 | CC3;
693 case LT: return CC1;
694 case GT: return CC2;
695 case LE: return CC0 | CC1;
696 case GE: return CC0 | CC2;
697 case UNORDERED: return CC3;
698 case ORDERED: return CC0 | CC1 | CC2;
699 case UNEQ: return CC0 | CC3;
700 case UNLT: return CC1 | CC3;
701 case UNGT: return CC2 | CC3;
702 case UNLE: return CC0 | CC1 | CC3;
703 case UNGE: return CC0 | CC2 | CC3;
704 case LTGT: return CC1 | CC2;
705 default:
706 abort ();
707 }
708 break;
709
710 case CCSRmode:
711 switch (GET_CODE (code))
712 {
713 case EQ: return CC0;
714 case NE: return CC2 | CC1 | CC3;
715 case LT: return CC2;
716 case GT: return CC1;
717 case LE: return CC0 | CC2;
718 case GE: return CC0 | CC1;
719 case UNORDERED: return CC3;
720 case ORDERED: return CC0 | CC2 | CC1;
721 case UNEQ: return CC0 | CC3;
722 case UNLT: return CC2 | CC3;
723 case UNGT: return CC1 | CC3;
724 case UNLE: return CC0 | CC2 | CC3;
725 case UNGE: return CC0 | CC1 | CC3;
726 case LTGT: return CC2 | CC1;
727 default:
728 abort ();
729 }
730 break;
731
732 default:
733 abort ();
734 }
735 }
736
737 /* If INV is false, return assembler mnemonic string to implement
738 a branch specified by CODE. If INV is true, return mnemonic
739 for the corresponding inverted branch. */
740
741 static const char *
742 s390_branch_condition_mnemonic (rtx code, int inv)
743 {
744 static const char *const mnemonic[16] =
745 {
746 NULL, "o", "h", "nle",
747 "l", "nhe", "lh", "ne",
748 "e", "nlh", "he", "nl",
749 "le", "nh", "no", NULL
750 };
751
752 int mask = s390_branch_condition_mask (code);
753
754 if (inv)
755 mask ^= 15;
756
757 if (mask < 1 || mask > 14)
758 abort ();
759
760 return mnemonic[mask];
761 }
762
763 /* If OP is an integer constant of mode MODE with exactly one
764 HImode subpart unequal to DEF, return the number of that
765 subpart. As a special case, all HImode subparts of OP are
766 equal to DEF, return zero. Otherwise, return -1. */
767
768 int
769 s390_single_hi (rtx op, enum machine_mode mode, int def)
770 {
771 if (GET_CODE (op) == CONST_INT)
772 {
773 unsigned HOST_WIDE_INT value = 0;
774 int n_parts = GET_MODE_SIZE (mode) / 2;
775 int i, part = -1;
776
777 for (i = 0; i < n_parts; i++)
778 {
779 if (i == 0)
780 value = (unsigned HOST_WIDE_INT) INTVAL (op);
781 else
782 value >>= 16;
783
784 if ((value & 0xffff) != (unsigned)(def & 0xffff))
785 {
786 if (part != -1)
787 return -1;
788 else
789 part = i;
790 }
791 }
792
793 return part == -1 ? 0 : (n_parts - 1 - part);
794 }
795
796 else if (GET_CODE (op) == CONST_DOUBLE
797 && GET_MODE (op) == VOIDmode)
798 {
799 unsigned HOST_WIDE_INT value = 0;
800 int n_parts = GET_MODE_SIZE (mode) / 2;
801 int i, part = -1;
802
803 for (i = 0; i < n_parts; i++)
804 {
805 if (i == 0)
806 value = (unsigned HOST_WIDE_INT) CONST_DOUBLE_LOW (op);
807 else if (i == HOST_BITS_PER_WIDE_INT / 16)
808 value = (unsigned HOST_WIDE_INT) CONST_DOUBLE_HIGH (op);
809 else
810 value >>= 16;
811
812 if ((value & 0xffff) != (unsigned)(def & 0xffff))
813 {
814 if (part != -1)
815 return -1;
816 else
817 part = i;
818 }
819 }
820
821 return part == -1 ? 0 : (n_parts - 1 - part);
822 }
823
824 return -1;
825 }
826
827 /* Extract the HImode part number PART from integer
828 constant OP of mode MODE. */
829
830 int
831 s390_extract_hi (rtx op, enum machine_mode mode, int part)
832 {
833 int n_parts = GET_MODE_SIZE (mode) / 2;
834 if (part < 0 || part >= n_parts)
835 abort();
836 else
837 part = n_parts - 1 - part;
838
839 if (GET_CODE (op) == CONST_INT)
840 {
841 unsigned HOST_WIDE_INT value = (unsigned HOST_WIDE_INT) INTVAL (op);
842 return ((value >> (16 * part)) & 0xffff);
843 }
844 else if (GET_CODE (op) == CONST_DOUBLE
845 && GET_MODE (op) == VOIDmode)
846 {
847 unsigned HOST_WIDE_INT value;
848 if (part < HOST_BITS_PER_WIDE_INT / 16)
849 value = (unsigned HOST_WIDE_INT) CONST_DOUBLE_LOW (op);
850 else
851 value = (unsigned HOST_WIDE_INT) CONST_DOUBLE_HIGH (op),
852 part -= HOST_BITS_PER_WIDE_INT / 16;
853
854 return ((value >> (16 * part)) & 0xffff);
855 }
856
857 abort ();
858 }
859
860 /* If OP is an integer constant of mode MODE with exactly one
861 QImode subpart unequal to DEF, return the number of that
862 subpart. As a special case, all QImode subparts of OP are
863 equal to DEF, return zero. Otherwise, return -1. */
864
865 int
866 s390_single_qi (rtx op, enum machine_mode mode, int def)
867 {
868 if (GET_CODE (op) == CONST_INT)
869 {
870 unsigned HOST_WIDE_INT value = 0;
871 int n_parts = GET_MODE_SIZE (mode);
872 int i, part = -1;
873
874 for (i = 0; i < n_parts; i++)
875 {
876 if (i == 0)
877 value = (unsigned HOST_WIDE_INT) INTVAL (op);
878 else
879 value >>= 8;
880
881 if ((value & 0xff) != (unsigned)(def & 0xff))
882 {
883 if (part != -1)
884 return -1;
885 else
886 part = i;
887 }
888 }
889
890 return part == -1 ? 0 : (n_parts - 1 - part);
891 }
892
893 else if (GET_CODE (op) == CONST_DOUBLE
894 && GET_MODE (op) == VOIDmode)
895 {
896 unsigned HOST_WIDE_INT value = 0;
897 int n_parts = GET_MODE_SIZE (mode);
898 int i, part = -1;
899
900 for (i = 0; i < n_parts; i++)
901 {
902 if (i == 0)
903 value = (unsigned HOST_WIDE_INT) CONST_DOUBLE_LOW (op);
904 else if (i == HOST_BITS_PER_WIDE_INT / 8)
905 value = (unsigned HOST_WIDE_INT) CONST_DOUBLE_HIGH (op);
906 else
907 value >>= 8;
908
909 if ((value & 0xff) != (unsigned)(def & 0xff))
910 {
911 if (part != -1)
912 return -1;
913 else
914 part = i;
915 }
916 }
917
918 return part == -1 ? 0 : (n_parts - 1 - part);
919 }
920
921 return -1;
922 }
923
924 /* Extract the QImode part number PART from integer
925 constant OP of mode MODE. */
926
927 int
928 s390_extract_qi (rtx op, enum machine_mode mode, int part)
929 {
930 int n_parts = GET_MODE_SIZE (mode);
931 if (part < 0 || part >= n_parts)
932 abort();
933 else
934 part = n_parts - 1 - part;
935
936 if (GET_CODE (op) == CONST_INT)
937 {
938 unsigned HOST_WIDE_INT value = (unsigned HOST_WIDE_INT) INTVAL (op);
939 return ((value >> (8 * part)) & 0xff);
940 }
941 else if (GET_CODE (op) == CONST_DOUBLE
942 && GET_MODE (op) == VOIDmode)
943 {
944 unsigned HOST_WIDE_INT value;
945 if (part < HOST_BITS_PER_WIDE_INT / 8)
946 value = (unsigned HOST_WIDE_INT) CONST_DOUBLE_LOW (op);
947 else
948 value = (unsigned HOST_WIDE_INT) CONST_DOUBLE_HIGH (op),
949 part -= HOST_BITS_PER_WIDE_INT / 8;
950
951 return ((value >> (8 * part)) & 0xff);
952 }
953
954 abort ();
955 }
956
957 /* Check whether we can (and want to) split a double-word
958 move in mode MODE from SRC to DST into two single-word
959 moves, moving the subword FIRST_SUBWORD first. */
960
961 bool
962 s390_split_ok_p (rtx dst, rtx src, enum machine_mode mode, int first_subword)
963 {
964 /* Floating point registers cannot be split. */
965 if (FP_REG_P (src) || FP_REG_P (dst))
966 return false;
967
968 /* We don't need to split if operands are directly accessible. */
969 if (s_operand (src, mode) || s_operand (dst, mode))
970 return false;
971
972 /* Non-offsettable memory references cannot be split. */
973 if ((GET_CODE (src) == MEM && !offsettable_memref_p (src))
974 || (GET_CODE (dst) == MEM && !offsettable_memref_p (dst)))
975 return false;
976
977 /* Moving the first subword must not clobber a register
978 needed to move the second subword. */
979 if (register_operand (dst, mode))
980 {
981 rtx subreg = operand_subword (dst, first_subword, 0, mode);
982 if (reg_overlap_mentioned_p (subreg, src))
983 return false;
984 }
985
986 return true;
987 }
988
989
990 /* Change optimizations to be performed, depending on the
991 optimization level.
992
993 LEVEL is the optimization level specified; 2 if `-O2' is
994 specified, 1 if `-O' is specified, and 0 if neither is specified.
995
996 SIZE is nonzero if `-Os' is specified and zero otherwise. */
997
998 void
999 optimization_options (int level ATTRIBUTE_UNUSED, int size ATTRIBUTE_UNUSED)
1000 {
1001 /* ??? There are apparently still problems with -fcaller-saves. */
1002 flag_caller_saves = 0;
1003
1004 /* By default, always emit DWARF-2 unwind info. This allows debugging
1005 without maintaining a stack frame back-chain. */
1006 flag_asynchronous_unwind_tables = 1;
1007 }
1008
1009 void
1010 override_options (void)
1011 {
1012 int i;
1013 static struct pta
1014 {
1015 const char *const name; /* processor name or nickname. */
1016 const enum processor_type processor;
1017 const enum processor_flags flags;
1018 }
1019 const processor_alias_table[] =
1020 {
1021 {"g5", PROCESSOR_9672_G5, PF_IEEE_FLOAT},
1022 {"g6", PROCESSOR_9672_G6, PF_IEEE_FLOAT},
1023 {"z900", PROCESSOR_2064_Z900, PF_IEEE_FLOAT | PF_ZARCH},
1024 {"z990", PROCESSOR_2084_Z990, PF_IEEE_FLOAT | PF_ZARCH
1025 | PF_LONG_DISPLACEMENT},
1026 };
1027
1028 int const pta_size = ARRAY_SIZE (processor_alias_table);
1029
1030 /* Acquire a unique set number for our register saves and restores. */
1031 s390_sr_alias_set = new_alias_set ();
1032
1033 /* Set up function hooks. */
1034 init_machine_status = s390_init_machine_status;
1035
1036 /* Architecture mode defaults according to ABI. */
1037 if (!(target_flags_explicit & MASK_ZARCH))
1038 {
1039 if (TARGET_64BIT)
1040 target_flags |= MASK_ZARCH;
1041 else
1042 target_flags &= ~MASK_ZARCH;
1043 }
1044
1045 /* Determine processor architectural level. */
1046 if (!s390_arch_string)
1047 s390_arch_string = TARGET_ZARCH? "z900" : "g5";
1048
1049 for (i = 0; i < pta_size; i++)
1050 if (! strcmp (s390_arch_string, processor_alias_table[i].name))
1051 {
1052 s390_arch = processor_alias_table[i].processor;
1053 s390_arch_flags = processor_alias_table[i].flags;
1054 break;
1055 }
1056 if (i == pta_size)
1057 error ("Unknown cpu used in -march=%s.", s390_arch_string);
1058
1059 /* Determine processor to tune for. */
1060 if (!s390_tune_string)
1061 {
1062 s390_tune = s390_arch;
1063 s390_tune_flags = s390_arch_flags;
1064 s390_tune_string = s390_arch_string;
1065 }
1066 else
1067 {
1068 for (i = 0; i < pta_size; i++)
1069 if (! strcmp (s390_tune_string, processor_alias_table[i].name))
1070 {
1071 s390_tune = processor_alias_table[i].processor;
1072 s390_tune_flags = processor_alias_table[i].flags;
1073 break;
1074 }
1075 if (i == pta_size)
1076 error ("Unknown cpu used in -mtune=%s.", s390_tune_string);
1077 }
1078
1079 /* Sanity checks. */
1080 if (TARGET_ZARCH && !(s390_arch_flags & PF_ZARCH))
1081 error ("z/Architecture mode not supported on %s.", s390_arch_string);
1082 if (TARGET_64BIT && !TARGET_ZARCH)
1083 error ("64-bit ABI not supported in ESA/390 mode.");
1084 }
1085
1086 /* Map for smallest class containing reg regno. */
1087
1088 const enum reg_class regclass_map[FIRST_PSEUDO_REGISTER] =
1089 { GENERAL_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
1090 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
1091 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
1092 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
1093 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
1094 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
1095 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
1096 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
1097 ADDR_REGS, NO_REGS, ADDR_REGS
1098 };
1099
1100 /* Return attribute type of insn. */
1101
1102 static enum attr_type
1103 s390_safe_attr_type (rtx insn)
1104 {
1105 if (recog_memoized (insn) >= 0)
1106 return get_attr_type (insn);
1107 else
1108 return TYPE_NONE;
1109 }
1110
1111 /* Return true if OP a (const_int 0) operand.
1112 OP is the current operation.
1113 MODE is the current operation mode. */
1114
1115 int
1116 const0_operand (register rtx op, enum machine_mode mode)
1117 {
1118 return op == CONST0_RTX (mode);
1119 }
1120
1121 /* Return true if OP is constant.
1122 OP is the current operation.
1123 MODE is the current operation mode. */
1124
1125 int
1126 consttable_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1127 {
1128 return CONSTANT_P (op);
1129 }
1130
1131 /* Return true if the mode of operand OP matches MODE.
1132 If MODE is set to VOIDmode, set it to the mode of OP. */
1133
1134 static int
1135 check_mode (register rtx op, enum machine_mode *mode)
1136 {
1137 if (*mode == VOIDmode)
1138 *mode = GET_MODE (op);
1139 else
1140 {
1141 if (GET_MODE (op) != VOIDmode && GET_MODE (op) != *mode)
1142 return 0;
1143 }
1144 return 1;
1145 }
1146
1147 /* Return true if OP a valid operand for the LARL instruction.
1148 OP is the current operation.
1149 MODE is the current operation mode. */
1150
1151 int
1152 larl_operand (register rtx op, enum machine_mode mode)
1153 {
1154 if (! check_mode (op, &mode))
1155 return 0;
1156
1157 /* Allow labels and local symbols. */
1158 if (GET_CODE (op) == LABEL_REF)
1159 return 1;
1160 if (GET_CODE (op) == SYMBOL_REF)
1161 return ((SYMBOL_REF_FLAGS (op) & SYMBOL_FLAG_ALIGN1) == 0
1162 && SYMBOL_REF_TLS_MODEL (op) == 0
1163 && (!flag_pic || SYMBOL_REF_LOCAL_P (op)));
1164
1165 /* Everything else must have a CONST, so strip it. */
1166 if (GET_CODE (op) != CONST)
1167 return 0;
1168 op = XEXP (op, 0);
1169
1170 /* Allow adding *even* in-range constants. */
1171 if (GET_CODE (op) == PLUS)
1172 {
1173 if (GET_CODE (XEXP (op, 1)) != CONST_INT
1174 || (INTVAL (XEXP (op, 1)) & 1) != 0)
1175 return 0;
1176 #if HOST_BITS_PER_WIDE_INT > 32
1177 if (INTVAL (XEXP (op, 1)) >= (HOST_WIDE_INT)1 << 32
1178 || INTVAL (XEXP (op, 1)) < -((HOST_WIDE_INT)1 << 32))
1179 return 0;
1180 #endif
1181 op = XEXP (op, 0);
1182 }
1183
1184 /* Labels and local symbols allowed here as well. */
1185 if (GET_CODE (op) == LABEL_REF)
1186 return 1;
1187 if (GET_CODE (op) == SYMBOL_REF)
1188 return ((SYMBOL_REF_FLAGS (op) & SYMBOL_FLAG_ALIGN1) == 0
1189 && SYMBOL_REF_TLS_MODEL (op) == 0
1190 && (!flag_pic || SYMBOL_REF_LOCAL_P (op)));
1191
1192 /* Now we must have a @GOTENT offset or @PLT stub
1193 or an @INDNTPOFF TLS offset. */
1194 if (GET_CODE (op) == UNSPEC
1195 && XINT (op, 1) == UNSPEC_GOTENT)
1196 return 1;
1197 if (GET_CODE (op) == UNSPEC
1198 && XINT (op, 1) == UNSPEC_PLT)
1199 return 1;
1200 if (GET_CODE (op) == UNSPEC
1201 && XINT (op, 1) == UNSPEC_INDNTPOFF)
1202 return 1;
1203
1204 return 0;
1205 }
1206
1207 /* Helper routine to implement s_operand and s_imm_operand.
1208 OP is the current operation.
1209 MODE is the current operation mode.
1210 ALLOW_IMMEDIATE specifies whether immediate operands should
1211 be accepted or not. */
1212
1213 static int
1214 general_s_operand (register rtx op, enum machine_mode mode,
1215 int allow_immediate)
1216 {
1217 struct s390_address addr;
1218
1219 /* Call general_operand first, so that we don't have to
1220 check for many special cases. */
1221 if (!general_operand (op, mode))
1222 return 0;
1223
1224 /* Just like memory_operand, allow (subreg (mem ...))
1225 after reload. */
1226 if (reload_completed
1227 && GET_CODE (op) == SUBREG
1228 && GET_CODE (SUBREG_REG (op)) == MEM)
1229 op = SUBREG_REG (op);
1230
1231 switch (GET_CODE (op))
1232 {
1233 /* Constants are OK as s-operand if ALLOW_IMMEDIATE
1234 is true and we are still before reload. */
1235 case CONST_INT:
1236 case CONST_DOUBLE:
1237 if (!allow_immediate || reload_completed)
1238 return 0;
1239 return 1;
1240
1241 /* Memory operands are OK unless they already use an
1242 index register. */
1243 case MEM:
1244 if (GET_CODE (XEXP (op, 0)) == ADDRESSOF)
1245 return 1;
1246 if (!s390_decompose_address (XEXP (op, 0), &addr))
1247 return 0;
1248 if (addr.indx)
1249 return 0;
1250 /* Do not allow literal pool references unless ALLOW_IMMEDIATE
1251 is true. This prevents compares between two literal pool
1252 entries from being accepted. */
1253 if (!allow_immediate
1254 && addr.base && REGNO (addr.base) == BASE_REGISTER)
1255 return 0;
1256 return 1;
1257
1258 default:
1259 break;
1260 }
1261
1262 return 0;
1263 }
1264
1265 /* Return true if OP is a valid S-type operand.
1266 OP is the current operation.
1267 MODE is the current operation mode. */
1268
1269 int
1270 s_operand (register rtx op, enum machine_mode mode)
1271 {
1272 return general_s_operand (op, mode, 0);
1273 }
1274
1275 /* Return true if OP is a valid S-type operand or an immediate
1276 operand that can be addressed as S-type operand by forcing
1277 it into the literal pool.
1278 OP is the current operation.
1279 MODE is the current operation mode. */
1280
1281 int
1282 s_imm_operand (register rtx op, enum machine_mode mode)
1283 {
1284 return general_s_operand (op, mode, 1);
1285 }
1286
1287 /* Return true if OP a valid shift count operand.
1288 OP is the current operation.
1289 MODE is the current operation mode. */
1290
1291 int
1292 shift_count_operand (rtx op, enum machine_mode mode)
1293 {
1294 HOST_WIDE_INT offset = 0;
1295
1296 if (! check_mode (op, &mode))
1297 return 0;
1298
1299 /* We can have an integer constant, an address register,
1300 or a sum of the two. Note that reload already checks
1301 that any register present is an address register, so
1302 we just check for any register here. */
1303 if (GET_CODE (op) == CONST_INT)
1304 {
1305 offset = INTVAL (op);
1306 op = NULL_RTX;
1307 }
1308 if (op && GET_CODE (op) == PLUS && GET_CODE (XEXP (op, 1)) == CONST_INT)
1309 {
1310 offset = INTVAL (XEXP (op, 1));
1311 op = XEXP (op, 0);
1312 }
1313 while (op && GET_CODE (op) == SUBREG)
1314 op = SUBREG_REG (op);
1315 if (op && GET_CODE (op) != REG)
1316 return 0;
1317
1318 /* Unfortunately we have to reject constants that are invalid
1319 for an address, or else reload will get confused. */
1320 if (!DISP_IN_RANGE (offset))
1321 return 0;
1322
1323 return 1;
1324 }
1325
1326 /* Return true if DISP is a valid short displacement. */
1327
1328 static int
1329 s390_short_displacement (rtx disp)
1330 {
1331 /* No displacement is OK. */
1332 if (!disp)
1333 return 1;
1334
1335 /* Integer displacement in range. */
1336 if (GET_CODE (disp) == CONST_INT)
1337 return INTVAL (disp) >= 0 && INTVAL (disp) < 4096;
1338
1339 /* GOT offset is not OK, the GOT can be large. */
1340 if (GET_CODE (disp) == CONST
1341 && GET_CODE (XEXP (disp, 0)) == UNSPEC
1342 && XINT (XEXP (disp, 0), 1) == UNSPEC_GOT)
1343 return 0;
1344
1345 /* All other symbolic constants are literal pool references,
1346 which are OK as the literal pool must be small. */
1347 if (GET_CODE (disp) == CONST)
1348 return 1;
1349
1350 return 0;
1351 }
1352
1353 /* Return true if OP is a valid operand for a C constraint. */
1354
1355 int
1356 s390_extra_constraint (rtx op, int c)
1357 {
1358 struct s390_address addr;
1359
1360 switch (c)
1361 {
1362 case 'Q':
1363 if (GET_CODE (op) != MEM)
1364 return 0;
1365 if (!s390_decompose_address (XEXP (op, 0), &addr))
1366 return 0;
1367 if (addr.indx)
1368 return 0;
1369
1370 if (TARGET_LONG_DISPLACEMENT)
1371 {
1372 if (!s390_short_displacement (addr.disp))
1373 return 0;
1374 }
1375 break;
1376
1377 case 'R':
1378 if (GET_CODE (op) != MEM)
1379 return 0;
1380
1381 if (TARGET_LONG_DISPLACEMENT)
1382 {
1383 if (!s390_decompose_address (XEXP (op, 0), &addr))
1384 return 0;
1385 if (!s390_short_displacement (addr.disp))
1386 return 0;
1387 }
1388 break;
1389
1390 case 'S':
1391 if (!TARGET_LONG_DISPLACEMENT)
1392 return 0;
1393 if (GET_CODE (op) != MEM)
1394 return 0;
1395 if (!s390_decompose_address (XEXP (op, 0), &addr))
1396 return 0;
1397 if (addr.indx)
1398 return 0;
1399 if (s390_short_displacement (addr.disp))
1400 return 0;
1401 break;
1402
1403 case 'T':
1404 if (!TARGET_LONG_DISPLACEMENT)
1405 return 0;
1406 if (GET_CODE (op) != MEM)
1407 return 0;
1408 /* Any invalid address here will be fixed up by reload,
1409 so accept it for the most generic constraint. */
1410 if (s390_decompose_address (XEXP (op, 0), &addr)
1411 && s390_short_displacement (addr.disp))
1412 return 0;
1413 break;
1414
1415 case 'U':
1416 if (TARGET_LONG_DISPLACEMENT)
1417 {
1418 if (!s390_decompose_address (op, &addr))
1419 return 0;
1420 if (!s390_short_displacement (addr.disp))
1421 return 0;
1422 }
1423 break;
1424
1425 case 'W':
1426 if (!TARGET_LONG_DISPLACEMENT)
1427 return 0;
1428 /* Any invalid address here will be fixed up by reload,
1429 so accept it for the most generic constraint. */
1430 if (s390_decompose_address (op, &addr)
1431 && s390_short_displacement (addr.disp))
1432 return 0;
1433 break;
1434
1435 case 'Y':
1436 return shift_count_operand (op, VOIDmode);
1437
1438 default:
1439 return 0;
1440 }
1441
1442 return 1;
1443 }
1444
1445 /* Compute a (partial) cost for rtx X. Return true if the complete
1446 cost has been computed, and false if subexpressions should be
1447 scanned. In either case, *TOTAL contains the cost result. */
1448
1449 static bool
1450 s390_rtx_costs (rtx x, int code, int outer_code, int *total)
1451 {
1452 switch (code)
1453 {
1454 case CONST:
1455 if (GET_CODE (XEXP (x, 0)) == MINUS
1456 && GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
1457 *total = 1000;
1458 else
1459 *total = 0;
1460 return true;
1461
1462 case CONST_INT:
1463 /* Force_const_mem does not work out of reload, because the
1464 saveable_obstack is set to reload_obstack, which does not
1465 live long enough. Because of this we cannot use force_const_mem
1466 in addsi3. This leads to problems with gen_add2_insn with a
1467 constant greater than a short. Because of that we give an
1468 addition of greater constants a cost of 3 (reload1.c 10096). */
1469 /* ??? saveable_obstack no longer exists. */
1470 if (outer_code == PLUS
1471 && (INTVAL (x) > 32767 || INTVAL (x) < -32768))
1472 *total = COSTS_N_INSNS (3);
1473 else
1474 *total = 0;
1475 return true;
1476
1477 case LABEL_REF:
1478 case SYMBOL_REF:
1479 case CONST_DOUBLE:
1480 *total = 0;
1481 return true;
1482
1483 case ASHIFT:
1484 case ASHIFTRT:
1485 case LSHIFTRT:
1486 case PLUS:
1487 case AND:
1488 case IOR:
1489 case XOR:
1490 case MINUS:
1491 case NEG:
1492 case NOT:
1493 *total = COSTS_N_INSNS (1);
1494 return true;
1495
1496 case MULT:
1497 if (GET_MODE (XEXP (x, 0)) == DImode)
1498 *total = COSTS_N_INSNS (40);
1499 else
1500 *total = COSTS_N_INSNS (7);
1501 return true;
1502
1503 case DIV:
1504 case UDIV:
1505 case MOD:
1506 case UMOD:
1507 *total = COSTS_N_INSNS (33);
1508 return true;
1509
1510 default:
1511 return false;
1512 }
1513 }
1514
1515 /* Return the cost of an address rtx ADDR. */
1516
1517 static int
1518 s390_address_cost (rtx addr)
1519 {
1520 struct s390_address ad;
1521 if (!s390_decompose_address (addr, &ad))
1522 return 1000;
1523
1524 return ad.indx? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (1);
1525 }
1526
1527 /* Return true if OP is a valid operand for the BRAS instruction.
1528 OP is the current operation.
1529 MODE is the current operation mode. */
1530
1531 int
1532 bras_sym_operand (register rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1533 {
1534 register enum rtx_code code = GET_CODE (op);
1535
1536 /* Allow SYMBOL_REFs. */
1537 if (code == SYMBOL_REF)
1538 return 1;
1539
1540 /* Allow @PLT stubs. */
1541 if (code == CONST
1542 && GET_CODE (XEXP (op, 0)) == UNSPEC
1543 && XINT (XEXP (op, 0), 1) == UNSPEC_PLT)
1544 return 1;
1545 return 0;
1546 }
1547
1548 /* If OP is a SYMBOL_REF of a thread-local symbol, return its TLS mode,
1549 otherwise return 0. */
1550
1551 int
1552 tls_symbolic_operand (register rtx op)
1553 {
1554 if (GET_CODE (op) != SYMBOL_REF)
1555 return 0;
1556 return SYMBOL_REF_TLS_MODEL (op);
1557 }
1558 \f
1559 /* Return true if OP is a load multiple operation. It is known to be a
1560 PARALLEL and the first section will be tested.
1561 OP is the current operation.
1562 MODE is the current operation mode. */
1563
1564 int
1565 load_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1566 {
1567 enum machine_mode elt_mode;
1568 int count = XVECLEN (op, 0);
1569 unsigned int dest_regno;
1570 rtx src_addr;
1571 int i, off;
1572
1573
1574 /* Perform a quick check so we don't blow up below. */
1575 if (count <= 1
1576 || GET_CODE (XVECEXP (op, 0, 0)) != SET
1577 || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
1578 || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
1579 return 0;
1580
1581 dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
1582 src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
1583 elt_mode = GET_MODE (SET_DEST (XVECEXP (op, 0, 0)));
1584
1585 /* Check, is base, or base + displacement. */
1586
1587 if (GET_CODE (src_addr) == REG)
1588 off = 0;
1589 else if (GET_CODE (src_addr) == PLUS
1590 && GET_CODE (XEXP (src_addr, 0)) == REG
1591 && GET_CODE (XEXP (src_addr, 1)) == CONST_INT)
1592 {
1593 off = INTVAL (XEXP (src_addr, 1));
1594 src_addr = XEXP (src_addr, 0);
1595 }
1596 else
1597 return 0;
1598
1599 if (src_addr == frame_pointer_rtx || src_addr == arg_pointer_rtx)
1600 return 0;
1601
1602 for (i = 1; i < count; i++)
1603 {
1604 rtx elt = XVECEXP (op, 0, i);
1605
1606 if (GET_CODE (elt) != SET
1607 || GET_CODE (SET_DEST (elt)) != REG
1608 || GET_MODE (SET_DEST (elt)) != elt_mode
1609 || REGNO (SET_DEST (elt)) != dest_regno + i
1610 || GET_CODE (SET_SRC (elt)) != MEM
1611 || GET_MODE (SET_SRC (elt)) != elt_mode
1612 || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
1613 || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
1614 || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
1615 || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1))
1616 != off + i * GET_MODE_SIZE (elt_mode))
1617 return 0;
1618 }
1619
1620 return 1;
1621 }
1622
1623 /* Return true if OP is a store multiple operation. It is known to be a
1624 PARALLEL and the first section will be tested.
1625 OP is the current operation.
1626 MODE is the current operation mode. */
1627
1628 int
1629 store_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1630 {
1631 enum machine_mode elt_mode;
1632 int count = XVECLEN (op, 0);
1633 unsigned int src_regno;
1634 rtx dest_addr;
1635 int i, off;
1636
1637 /* Perform a quick check so we don't blow up below. */
1638 if (count <= 1
1639 || GET_CODE (XVECEXP (op, 0, 0)) != SET
1640 || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
1641 || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
1642 return 0;
1643
1644 src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
1645 dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
1646 elt_mode = GET_MODE (SET_SRC (XVECEXP (op, 0, 0)));
1647
1648 /* Check, is base, or base + displacement. */
1649
1650 if (GET_CODE (dest_addr) == REG)
1651 off = 0;
1652 else if (GET_CODE (dest_addr) == PLUS
1653 && GET_CODE (XEXP (dest_addr, 0)) == REG
1654 && GET_CODE (XEXP (dest_addr, 1)) == CONST_INT)
1655 {
1656 off = INTVAL (XEXP (dest_addr, 1));
1657 dest_addr = XEXP (dest_addr, 0);
1658 }
1659 else
1660 return 0;
1661
1662 if (dest_addr == frame_pointer_rtx || dest_addr == arg_pointer_rtx)
1663 return 0;
1664
1665 for (i = 1; i < count; i++)
1666 {
1667 rtx elt = XVECEXP (op, 0, i);
1668
1669 if (GET_CODE (elt) != SET
1670 || GET_CODE (SET_SRC (elt)) != REG
1671 || GET_MODE (SET_SRC (elt)) != elt_mode
1672 || REGNO (SET_SRC (elt)) != src_regno + i
1673 || GET_CODE (SET_DEST (elt)) != MEM
1674 || GET_MODE (SET_DEST (elt)) != elt_mode
1675 || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
1676 || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
1677 || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
1678 || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1))
1679 != off + i * GET_MODE_SIZE (elt_mode))
1680 return 0;
1681 }
1682 return 1;
1683 }
1684
1685
1686 /* Return true if OP contains a symbol reference */
1687
1688 int
1689 symbolic_reference_mentioned_p (rtx op)
1690 {
1691 register const char *fmt;
1692 register int i;
1693
1694 if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
1695 return 1;
1696
1697 fmt = GET_RTX_FORMAT (GET_CODE (op));
1698 for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
1699 {
1700 if (fmt[i] == 'E')
1701 {
1702 register int j;
1703
1704 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
1705 if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
1706 return 1;
1707 }
1708
1709 else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
1710 return 1;
1711 }
1712
1713 return 0;
1714 }
1715
1716 /* Return true if OP contains a reference to a thread-local symbol. */
1717
1718 int
1719 tls_symbolic_reference_mentioned_p (rtx op)
1720 {
1721 register const char *fmt;
1722 register int i;
1723
1724 if (GET_CODE (op) == SYMBOL_REF)
1725 return tls_symbolic_operand (op);
1726
1727 fmt = GET_RTX_FORMAT (GET_CODE (op));
1728 for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
1729 {
1730 if (fmt[i] == 'E')
1731 {
1732 register int j;
1733
1734 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
1735 if (tls_symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
1736 return 1;
1737 }
1738
1739 else if (fmt[i] == 'e' && tls_symbolic_reference_mentioned_p (XEXP (op, i)))
1740 return 1;
1741 }
1742
1743 return 0;
1744 }
1745
1746
1747 /* Return true if OP is a legitimate general operand when
1748 generating PIC code. It is given that flag_pic is on
1749 and that OP satisfies CONSTANT_P or is a CONST_DOUBLE. */
1750
1751 int
1752 legitimate_pic_operand_p (register rtx op)
1753 {
1754 /* Accept all non-symbolic constants. */
1755 if (!SYMBOLIC_CONST (op))
1756 return 1;
1757
1758 /* Reject everything else; must be handled
1759 via emit_symbolic_move. */
1760 return 0;
1761 }
1762
1763 /* Returns true if the constant value OP is a legitimate general operand.
1764 It is given that OP satisfies CONSTANT_P or is a CONST_DOUBLE. */
1765
1766 int
1767 legitimate_constant_p (register rtx op)
1768 {
1769 /* Accept all non-symbolic constants. */
1770 if (!SYMBOLIC_CONST (op))
1771 return 1;
1772
1773 /* Accept immediate LARL operands. */
1774 if (TARGET_CPU_ZARCH && larl_operand (op, VOIDmode))
1775 return 1;
1776
1777 /* Thread-local symbols are never legal constants. This is
1778 so that emit_call knows that computing such addresses
1779 might require a function call. */
1780 if (TLS_SYMBOLIC_CONST (op))
1781 return 0;
1782
1783 /* In the PIC case, symbolic constants must *not* be
1784 forced into the literal pool. We accept them here,
1785 so that they will be handled by emit_symbolic_move. */
1786 if (flag_pic)
1787 return 1;
1788
1789 /* All remaining non-PIC symbolic constants are
1790 forced into the literal pool. */
1791 return 0;
1792 }
1793
1794 /* Determine if it's legal to put X into the constant pool. This
1795 is not possible if X contains the address of a symbol that is
1796 not constant (TLS) or not known at final link time (PIC). */
1797
1798 static bool
1799 s390_cannot_force_const_mem (rtx x)
1800 {
1801 switch (GET_CODE (x))
1802 {
1803 case CONST_INT:
1804 case CONST_DOUBLE:
1805 /* Accept all non-symbolic constants. */
1806 return false;
1807
1808 case LABEL_REF:
1809 /* Labels are OK iff we are non-PIC. */
1810 return flag_pic != 0;
1811
1812 case SYMBOL_REF:
1813 /* 'Naked' TLS symbol references are never OK,
1814 non-TLS symbols are OK iff we are non-PIC. */
1815 if (tls_symbolic_operand (x))
1816 return true;
1817 else
1818 return flag_pic != 0;
1819
1820 case CONST:
1821 return s390_cannot_force_const_mem (XEXP (x, 0));
1822 case PLUS:
1823 case MINUS:
1824 return s390_cannot_force_const_mem (XEXP (x, 0))
1825 || s390_cannot_force_const_mem (XEXP (x, 1));
1826
1827 case UNSPEC:
1828 switch (XINT (x, 1))
1829 {
1830 /* Only lt-relative or GOT-relative UNSPECs are OK. */
1831 case UNSPEC_LTREL_OFFSET:
1832 case UNSPEC_GOT:
1833 case UNSPEC_GOTOFF:
1834 case UNSPEC_PLTOFF:
1835 case UNSPEC_TLSGD:
1836 case UNSPEC_TLSLDM:
1837 case UNSPEC_NTPOFF:
1838 case UNSPEC_DTPOFF:
1839 case UNSPEC_GOTNTPOFF:
1840 case UNSPEC_INDNTPOFF:
1841 return false;
1842
1843 default:
1844 return true;
1845 }
1846 break;
1847
1848 default:
1849 abort ();
1850 }
1851 }
1852
1853 /* Returns true if the constant value OP is a legitimate general
1854 operand during and after reload. The difference to
1855 legitimate_constant_p is that this function will not accept
1856 a constant that would need to be forced to the literal pool
1857 before it can be used as operand. */
1858
1859 int
1860 legitimate_reload_constant_p (register rtx op)
1861 {
1862 /* Accept la(y) operands. */
1863 if (GET_CODE (op) == CONST_INT
1864 && DISP_IN_RANGE (INTVAL (op)))
1865 return 1;
1866
1867 /* Accept l(g)hi operands. */
1868 if (GET_CODE (op) == CONST_INT
1869 && CONST_OK_FOR_LETTER_P (INTVAL (op), 'K'))
1870 return 1;
1871
1872 /* Accept lliXX operands. */
1873 if (TARGET_ZARCH
1874 && s390_single_hi (op, DImode, 0) >= 0)
1875 return 1;
1876
1877 /* Accept larl operands. */
1878 if (TARGET_CPU_ZARCH
1879 && larl_operand (op, VOIDmode))
1880 return 1;
1881
1882 /* Everything else cannot be handled without reload. */
1883 return 0;
1884 }
1885
1886 /* Given an rtx OP being reloaded into a reg required to be in class CLASS,
1887 return the class of reg to actually use. */
1888
1889 enum reg_class
1890 s390_preferred_reload_class (rtx op, enum reg_class class)
1891 {
1892 /* This can happen if a floating point constant is being
1893 reloaded into an integer register. Leave well alone. */
1894 if (GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT
1895 && class != FP_REGS)
1896 return class;
1897
1898 switch (GET_CODE (op))
1899 {
1900 /* Constants we cannot reload must be forced into the
1901 literal pool. */
1902
1903 case CONST_DOUBLE:
1904 case CONST_INT:
1905 if (legitimate_reload_constant_p (op))
1906 return class;
1907 else
1908 return NO_REGS;
1909
1910 /* If a symbolic constant or a PLUS is reloaded,
1911 it is most likely being used as an address, so
1912 prefer ADDR_REGS. If 'class' is not a superset
1913 of ADDR_REGS, e.g. FP_REGS, reject this reload. */
1914 case PLUS:
1915 case LABEL_REF:
1916 case SYMBOL_REF:
1917 case CONST:
1918 if (reg_class_subset_p (ADDR_REGS, class))
1919 return ADDR_REGS;
1920 else
1921 return NO_REGS;
1922
1923 default:
1924 break;
1925 }
1926
1927 return class;
1928 }
1929
1930 /* Return the register class of a scratch register needed to
1931 load IN into a register of class CLASS in MODE.
1932
1933 We need a temporary when loading a PLUS expression which
1934 is not a legitimate operand of the LOAD ADDRESS instruction. */
1935
1936 enum reg_class
1937 s390_secondary_input_reload_class (enum reg_class class ATTRIBUTE_UNUSED,
1938 enum machine_mode mode, rtx in)
1939 {
1940 if (s390_plus_operand (in, mode))
1941 return ADDR_REGS;
1942
1943 return NO_REGS;
1944 }
1945
1946 /* Return the register class of a scratch register needed to
1947 store a register of class CLASS in MODE into OUT:
1948
1949 We need a temporary when storing a double-word to a
1950 non-offsettable memory address. */
1951
1952 enum reg_class
1953 s390_secondary_output_reload_class (enum reg_class class,
1954 enum machine_mode mode, rtx out)
1955 {
1956 if ((TARGET_64BIT ? mode == TImode
1957 : (mode == DImode || mode == DFmode))
1958 && reg_classes_intersect_p (GENERAL_REGS, class)
1959 && GET_CODE (out) == MEM
1960 && !offsettable_memref_p (out)
1961 && !s_operand (out, VOIDmode))
1962 return ADDR_REGS;
1963
1964 return NO_REGS;
1965 }
1966
1967 /* Return true if OP is a PLUS that is not a legitimate
1968 operand for the LA instruction.
1969 OP is the current operation.
1970 MODE is the current operation mode. */
1971
1972 int
1973 s390_plus_operand (register rtx op, enum machine_mode mode)
1974 {
1975 if (!check_mode (op, &mode) || mode != Pmode)
1976 return FALSE;
1977
1978 if (GET_CODE (op) != PLUS)
1979 return FALSE;
1980
1981 if (legitimate_la_operand_p (op))
1982 return FALSE;
1983
1984 return TRUE;
1985 }
1986
1987 /* Generate code to load SRC, which is PLUS that is not a
1988 legitimate operand for the LA instruction, into TARGET.
1989 SCRATCH may be used as scratch register. */
1990
1991 void
1992 s390_expand_plus_operand (register rtx target, register rtx src,
1993 register rtx scratch)
1994 {
1995 rtx sum1, sum2;
1996 struct s390_address ad;
1997
1998 /* src must be a PLUS; get its two operands. */
1999 if (GET_CODE (src) != PLUS || GET_MODE (src) != Pmode)
2000 abort ();
2001
2002 /* Check if any of the two operands is already scheduled
2003 for replacement by reload. This can happen e.g. when
2004 float registers occur in an address. */
2005 sum1 = find_replacement (&XEXP (src, 0));
2006 sum2 = find_replacement (&XEXP (src, 1));
2007 src = gen_rtx_PLUS (Pmode, sum1, sum2);
2008
2009 /* If the address is already strictly valid, there's nothing to do. */
2010 if (!s390_decompose_address (src, &ad)
2011 || (ad.base && !REG_OK_FOR_BASE_STRICT_P (ad.base))
2012 || (ad.indx && !REG_OK_FOR_INDEX_STRICT_P (ad.indx)))
2013 {
2014 /* Otherwise, one of the operands cannot be an address register;
2015 we reload its value into the scratch register. */
2016 if (true_regnum (sum1) < 1 || true_regnum (sum1) > 15)
2017 {
2018 emit_move_insn (scratch, sum1);
2019 sum1 = scratch;
2020 }
2021 if (true_regnum (sum2) < 1 || true_regnum (sum2) > 15)
2022 {
2023 emit_move_insn (scratch, sum2);
2024 sum2 = scratch;
2025 }
2026
2027 /* According to the way these invalid addresses are generated
2028 in reload.c, it should never happen (at least on s390) that
2029 *neither* of the PLUS components, after find_replacements
2030 was applied, is an address register. */
2031 if (sum1 == scratch && sum2 == scratch)
2032 {
2033 debug_rtx (src);
2034 abort ();
2035 }
2036
2037 src = gen_rtx_PLUS (Pmode, sum1, sum2);
2038 }
2039
2040 /* Emit the LOAD ADDRESS pattern. Note that reload of PLUS
2041 is only ever performed on addresses, so we can mark the
2042 sum as legitimate for LA in any case. */
2043 s390_load_address (target, src);
2044 }
2045
2046
2047 /* Decompose a RTL expression ADDR for a memory address into
2048 its components, returned in OUT.
2049
2050 Returns 0 if ADDR is not a valid memory address, nonzero
2051 otherwise. If OUT is NULL, don't return the components,
2052 but check for validity only.
2053
2054 Note: Only addresses in canonical form are recognized.
2055 LEGITIMIZE_ADDRESS should convert non-canonical forms to the
2056 canonical form so that they will be recognized. */
2057
2058 static int
2059 s390_decompose_address (register rtx addr, struct s390_address *out)
2060 {
2061 rtx base = NULL_RTX;
2062 rtx indx = NULL_RTX;
2063 rtx disp = NULL_RTX;
2064 int pointer = FALSE;
2065 int base_ptr = FALSE;
2066 int indx_ptr = FALSE;
2067
2068 /* Decompose address into base + index + displacement. */
2069
2070 if (GET_CODE (addr) == REG || GET_CODE (addr) == UNSPEC)
2071 base = addr;
2072
2073 else if (GET_CODE (addr) == PLUS)
2074 {
2075 rtx op0 = XEXP (addr, 0);
2076 rtx op1 = XEXP (addr, 1);
2077 enum rtx_code code0 = GET_CODE (op0);
2078 enum rtx_code code1 = GET_CODE (op1);
2079
2080 if (code0 == REG || code0 == UNSPEC)
2081 {
2082 if (code1 == REG || code1 == UNSPEC)
2083 {
2084 indx = op0; /* index + base */
2085 base = op1;
2086 }
2087
2088 else
2089 {
2090 base = op0; /* base + displacement */
2091 disp = op1;
2092 }
2093 }
2094
2095 else if (code0 == PLUS)
2096 {
2097 indx = XEXP (op0, 0); /* index + base + disp */
2098 base = XEXP (op0, 1);
2099 disp = op1;
2100 }
2101
2102 else
2103 {
2104 return FALSE;
2105 }
2106 }
2107
2108 else
2109 disp = addr; /* displacement */
2110
2111
2112 /* Validate base register. */
2113 if (base)
2114 {
2115 if (GET_CODE (base) == UNSPEC)
2116 {
2117 if (XVECLEN (base, 0) != 1 || XINT (base, 1) != UNSPEC_LTREL_BASE)
2118 return FALSE;
2119 base = gen_rtx_REG (Pmode, BASE_REGISTER);
2120 }
2121
2122 if (GET_CODE (base) != REG || GET_MODE (base) != Pmode)
2123 return FALSE;
2124
2125 if (REGNO (base) == BASE_REGISTER
2126 || REGNO (base) == STACK_POINTER_REGNUM
2127 || REGNO (base) == FRAME_POINTER_REGNUM
2128 || ((reload_completed || reload_in_progress)
2129 && frame_pointer_needed
2130 && REGNO (base) == HARD_FRAME_POINTER_REGNUM)
2131 || REGNO (base) == ARG_POINTER_REGNUM
2132 || (REGNO (base) >= FIRST_VIRTUAL_REGISTER
2133 && REGNO (base) <= LAST_VIRTUAL_REGISTER)
2134 || (flag_pic
2135 && REGNO (base) == PIC_OFFSET_TABLE_REGNUM))
2136 pointer = base_ptr = TRUE;
2137 }
2138
2139 /* Validate index register. */
2140 if (indx)
2141 {
2142 if (GET_CODE (indx) == UNSPEC)
2143 {
2144 if (XVECLEN (indx, 0) != 1 || XINT (indx, 1) != UNSPEC_LTREL_BASE)
2145 return FALSE;
2146 indx = gen_rtx_REG (Pmode, BASE_REGISTER);
2147 }
2148
2149 if (GET_CODE (indx) != REG || GET_MODE (indx) != Pmode)
2150 return FALSE;
2151
2152 if (REGNO (indx) == BASE_REGISTER
2153 || REGNO (indx) == STACK_POINTER_REGNUM
2154 || REGNO (indx) == FRAME_POINTER_REGNUM
2155 || ((reload_completed || reload_in_progress)
2156 && frame_pointer_needed
2157 && REGNO (indx) == HARD_FRAME_POINTER_REGNUM)
2158 || REGNO (indx) == ARG_POINTER_REGNUM
2159 || (REGNO (indx) >= FIRST_VIRTUAL_REGISTER
2160 && REGNO (indx) <= LAST_VIRTUAL_REGISTER)
2161 || (flag_pic
2162 && REGNO (indx) == PIC_OFFSET_TABLE_REGNUM))
2163 pointer = indx_ptr = TRUE;
2164 }
2165
2166 /* Prefer to use pointer as base, not index. */
2167 if (base && indx && !base_ptr
2168 && (indx_ptr || (!REG_POINTER (base) && REG_POINTER (indx))))
2169 {
2170 rtx tmp = base;
2171 base = indx;
2172 indx = tmp;
2173 }
2174
2175 /* Validate displacement. */
2176 if (disp)
2177 {
2178 /* Allow integer constant in range. */
2179 if (GET_CODE (disp) == CONST_INT)
2180 {
2181 /* If the argument pointer is involved, the displacement will change
2182 later anyway as the argument pointer gets eliminated. This could
2183 make a valid displacement invalid, but it is more likely to make
2184 an invalid displacement valid, because we sometimes access the
2185 register save area via negative offsets to the arg pointer.
2186 Thus we don't check the displacement for validity here. If after
2187 elimination the displacement turns out to be invalid after all,
2188 this is fixed up by reload in any case. */
2189 if (base != arg_pointer_rtx && indx != arg_pointer_rtx)
2190 {
2191 if (!DISP_IN_RANGE (INTVAL (disp)))
2192 return FALSE;
2193 }
2194 }
2195
2196 /* In the small-PIC case, the linker converts @GOT
2197 and @GOTNTPOFF offsets to possible displacements. */
2198 else if (GET_CODE (disp) == CONST
2199 && GET_CODE (XEXP (disp, 0)) == UNSPEC
2200 && (XINT (XEXP (disp, 0), 1) == UNSPEC_GOT
2201 || XINT (XEXP (disp, 0), 1) == UNSPEC_GOTNTPOFF))
2202 {
2203 if (flag_pic != 1)
2204 return FALSE;
2205
2206 pointer = TRUE;
2207 }
2208
2209 /* Accept chunkfied literal pool symbol references. */
2210 else if (GET_CODE (disp) == CONST
2211 && GET_CODE (XEXP (disp, 0)) == MINUS
2212 && GET_CODE (XEXP (XEXP (disp, 0), 0)) == LABEL_REF
2213 && GET_CODE (XEXP (XEXP (disp, 0), 1)) == LABEL_REF)
2214 {
2215 pointer = TRUE;
2216 }
2217
2218 /* Likewise if a constant offset is present. */
2219 else if (GET_CODE (disp) == CONST
2220 && GET_CODE (XEXP (disp, 0)) == PLUS
2221 && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT
2222 && GET_CODE (XEXP (XEXP (disp, 0), 0)) == MINUS
2223 && GET_CODE (XEXP (XEXP (XEXP (disp, 0), 0), 0)) == LABEL_REF
2224 && GET_CODE (XEXP (XEXP (XEXP (disp, 0), 0), 1)) == LABEL_REF)
2225 {
2226 pointer = TRUE;
2227 }
2228
2229 /* We can convert literal pool addresses to
2230 displacements by basing them off the base register. */
2231 else
2232 {
2233 /* In some cases, we can accept an additional
2234 small constant offset. Split these off here. */
2235
2236 unsigned int offset = 0;
2237
2238 if (GET_CODE (disp) == CONST
2239 && GET_CODE (XEXP (disp, 0)) == PLUS
2240 && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
2241 {
2242 offset = INTVAL (XEXP (XEXP (disp, 0), 1));
2243 disp = XEXP (XEXP (disp, 0), 0);
2244 }
2245
2246 /* Now we must have a literal pool address. */
2247 if (GET_CODE (disp) != SYMBOL_REF
2248 || !CONSTANT_POOL_ADDRESS_P (disp))
2249 return FALSE;
2250
2251 /* If we have an offset, make sure it does not
2252 exceed the size of the constant pool entry. */
2253 if (offset && offset >= GET_MODE_SIZE (get_pool_mode (disp)))
2254 return FALSE;
2255
2256 /* Either base or index must be free to
2257 hold the base register. */
2258 if (base && indx)
2259 return FALSE;
2260
2261 /* Convert the address. */
2262 if (base)
2263 indx = gen_rtx_REG (Pmode, BASE_REGISTER);
2264 else
2265 base = gen_rtx_REG (Pmode, BASE_REGISTER);
2266
2267 disp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, disp),
2268 UNSPEC_LTREL_OFFSET);
2269 disp = gen_rtx_CONST (Pmode, disp);
2270
2271 if (offset)
2272 disp = plus_constant (disp, offset);
2273
2274 pointer = TRUE;
2275 }
2276 }
2277
2278 if (!base && !indx)
2279 pointer = TRUE;
2280
2281 if (out)
2282 {
2283 out->base = base;
2284 out->indx = indx;
2285 out->disp = disp;
2286 out->pointer = pointer;
2287 }
2288
2289 return TRUE;
2290 }
2291
2292 /* Return nonzero if ADDR is a valid memory address.
2293 STRICT specifies whether strict register checking applies. */
2294
2295 int
2296 legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2297 register rtx addr, int strict)
2298 {
2299 struct s390_address ad;
2300 if (!s390_decompose_address (addr, &ad))
2301 return FALSE;
2302
2303 if (strict)
2304 {
2305 if (ad.base && !REG_OK_FOR_BASE_STRICT_P (ad.base))
2306 return FALSE;
2307 if (ad.indx && !REG_OK_FOR_INDEX_STRICT_P (ad.indx))
2308 return FALSE;
2309 }
2310 else
2311 {
2312 if (ad.base && !REG_OK_FOR_BASE_NONSTRICT_P (ad.base))
2313 return FALSE;
2314 if (ad.indx && !REG_OK_FOR_INDEX_NONSTRICT_P (ad.indx))
2315 return FALSE;
2316 }
2317
2318 return TRUE;
2319 }
2320
2321 /* Return 1 if OP is a valid operand for the LA instruction.
2322 In 31-bit, we need to prove that the result is used as an
2323 address, as LA performs only a 31-bit addition. */
2324
2325 int
2326 legitimate_la_operand_p (register rtx op)
2327 {
2328 struct s390_address addr;
2329 if (!s390_decompose_address (op, &addr))
2330 return FALSE;
2331
2332 if (TARGET_64BIT || addr.pointer)
2333 return TRUE;
2334
2335 return FALSE;
2336 }
2337
2338 /* Return 1 if OP is a valid operand for the LA instruction,
2339 and we prefer to use LA over addition to compute it. */
2340
2341 int
2342 preferred_la_operand_p (register rtx op)
2343 {
2344 struct s390_address addr;
2345 if (!s390_decompose_address (op, &addr))
2346 return FALSE;
2347
2348 if (!TARGET_64BIT && !addr.pointer)
2349 return FALSE;
2350
2351 if (addr.pointer)
2352 return TRUE;
2353
2354 if ((addr.base && REG_P (addr.base) && REG_POINTER (addr.base))
2355 || (addr.indx && REG_P (addr.indx) && REG_POINTER (addr.indx)))
2356 return TRUE;
2357
2358 return FALSE;
2359 }
2360
2361 /* Emit a forced load-address operation to load SRC into DST.
2362 This will use the LOAD ADDRESS instruction even in situations
2363 where legitimate_la_operand_p (SRC) returns false. */
2364
2365 void
2366 s390_load_address (rtx dst, rtx src)
2367 {
2368 if (TARGET_64BIT)
2369 emit_move_insn (dst, src);
2370 else
2371 emit_insn (gen_force_la_31 (dst, src));
2372 }
2373
2374 /* Return a legitimate reference for ORIG (an address) using the
2375 register REG. If REG is 0, a new pseudo is generated.
2376
2377 There are two types of references that must be handled:
2378
2379 1. Global data references must load the address from the GOT, via
2380 the PIC reg. An insn is emitted to do this load, and the reg is
2381 returned.
2382
2383 2. Static data references, constant pool addresses, and code labels
2384 compute the address as an offset from the GOT, whose base is in
2385 the PIC reg. Static data objects have SYMBOL_FLAG_LOCAL set to
2386 differentiate them from global data objects. The returned
2387 address is the PIC reg + an unspec constant.
2388
2389 GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
2390 reg also appears in the address. */
2391
2392 rtx
2393 legitimize_pic_address (rtx orig, rtx reg)
2394 {
2395 rtx addr = orig;
2396 rtx new = orig;
2397 rtx base;
2398
2399 if (GET_CODE (addr) == LABEL_REF
2400 || (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (addr)))
2401 {
2402 /* This is a local symbol. */
2403 if (TARGET_CPU_ZARCH && larl_operand (addr, VOIDmode))
2404 {
2405 /* Access local symbols PC-relative via LARL.
2406 This is the same as in the non-PIC case, so it is
2407 handled automatically ... */
2408 }
2409 else
2410 {
2411 /* Access local symbols relative to the GOT. */
2412
2413 rtx temp = reg? reg : gen_reg_rtx (Pmode);
2414
2415 if (reload_in_progress || reload_completed)
2416 regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2417
2418 addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
2419 addr = gen_rtx_CONST (Pmode, addr);
2420 addr = force_const_mem (Pmode, addr);
2421 emit_move_insn (temp, addr);
2422
2423 new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
2424 if (reg != 0)
2425 {
2426 emit_move_insn (reg, new);
2427 new = reg;
2428 }
2429 }
2430 }
2431 else if (GET_CODE (addr) == SYMBOL_REF)
2432 {
2433 if (reg == 0)
2434 reg = gen_reg_rtx (Pmode);
2435
2436 if (flag_pic == 1)
2437 {
2438 /* Assume GOT offset < 4k. This is handled the same way
2439 in both 31- and 64-bit code (@GOT). */
2440
2441 if (reload_in_progress || reload_completed)
2442 regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2443
2444 new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
2445 new = gen_rtx_CONST (Pmode, new);
2446 new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
2447 new = gen_rtx_MEM (Pmode, new);
2448 RTX_UNCHANGING_P (new) = 1;
2449 emit_move_insn (reg, new);
2450 new = reg;
2451 }
2452 else if (TARGET_CPU_ZARCH)
2453 {
2454 /* If the GOT offset might be >= 4k, we determine the position
2455 of the GOT entry via a PC-relative LARL (@GOTENT). */
2456
2457 rtx temp = gen_reg_rtx (Pmode);
2458
2459 new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTENT);
2460 new = gen_rtx_CONST (Pmode, new);
2461 emit_move_insn (temp, new);
2462
2463 new = gen_rtx_MEM (Pmode, temp);
2464 RTX_UNCHANGING_P (new) = 1;
2465 emit_move_insn (reg, new);
2466 new = reg;
2467 }
2468 else
2469 {
2470 /* If the GOT offset might be >= 4k, we have to load it
2471 from the literal pool (@GOT). */
2472
2473 rtx temp = gen_reg_rtx (Pmode);
2474
2475 if (reload_in_progress || reload_completed)
2476 regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2477
2478 addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
2479 addr = gen_rtx_CONST (Pmode, addr);
2480 addr = force_const_mem (Pmode, addr);
2481 emit_move_insn (temp, addr);
2482
2483 new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
2484 new = gen_rtx_MEM (Pmode, new);
2485 RTX_UNCHANGING_P (new) = 1;
2486 emit_move_insn (reg, new);
2487 new = reg;
2488 }
2489 }
2490 else
2491 {
2492 if (GET_CODE (addr) == CONST)
2493 {
2494 addr = XEXP (addr, 0);
2495 if (GET_CODE (addr) == UNSPEC)
2496 {
2497 if (XVECLEN (addr, 0) != 1)
2498 abort ();
2499 switch (XINT (addr, 1))
2500 {
2501 /* If someone moved a GOT-relative UNSPEC
2502 out of the literal pool, force them back in. */
2503 case UNSPEC_GOTOFF:
2504 case UNSPEC_PLTOFF:
2505 new = force_const_mem (Pmode, orig);
2506 break;
2507
2508 /* @GOT is OK as is if small. */
2509 case UNSPEC_GOT:
2510 if (flag_pic == 2)
2511 new = force_const_mem (Pmode, orig);
2512 break;
2513
2514 /* @GOTENT is OK as is. */
2515 case UNSPEC_GOTENT:
2516 break;
2517
2518 /* @PLT is OK as is on 64-bit, must be converted to
2519 GOT-relative @PLTOFF on 31-bit. */
2520 case UNSPEC_PLT:
2521 if (!TARGET_CPU_ZARCH)
2522 {
2523 rtx temp = reg? reg : gen_reg_rtx (Pmode);
2524
2525 if (reload_in_progress || reload_completed)
2526 regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2527
2528 addr = XVECEXP (addr, 0, 0);
2529 addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr),
2530 UNSPEC_PLTOFF);
2531 addr = gen_rtx_CONST (Pmode, addr);
2532 addr = force_const_mem (Pmode, addr);
2533 emit_move_insn (temp, addr);
2534
2535 new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
2536 if (reg != 0)
2537 {
2538 emit_move_insn (reg, new);
2539 new = reg;
2540 }
2541 }
2542 break;
2543
2544 /* Everything else cannot happen. */
2545 default:
2546 abort ();
2547 }
2548 }
2549 else if (GET_CODE (addr) != PLUS)
2550 abort ();
2551 }
2552 if (GET_CODE (addr) == PLUS)
2553 {
2554 rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
2555 /* Check first to see if this is a constant offset
2556 from a local symbol reference. */
2557 if ((GET_CODE (op0) == LABEL_REF
2558 || (GET_CODE (op0) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (op0)))
2559 && GET_CODE (op1) == CONST_INT)
2560 {
2561 if (TARGET_CPU_ZARCH && larl_operand (op0, VOIDmode))
2562 {
2563 if (INTVAL (op1) & 1)
2564 {
2565 /* LARL can't handle odd offsets, so emit a
2566 pair of LARL and LA. */
2567 rtx temp = reg? reg : gen_reg_rtx (Pmode);
2568
2569 if (!DISP_IN_RANGE (INTVAL (op1)))
2570 {
2571 int even = INTVAL (op1) - 1;
2572 op0 = gen_rtx_PLUS (Pmode, op0, GEN_INT (even));
2573 op0 = gen_rtx_CONST (Pmode, op0);
2574 op1 = GEN_INT (1);
2575 }
2576
2577 emit_move_insn (temp, op0);
2578 new = gen_rtx_PLUS (Pmode, temp, op1);
2579
2580 if (reg != 0)
2581 {
2582 emit_move_insn (reg, new);
2583 new = reg;
2584 }
2585 }
2586 else
2587 {
2588 /* If the offset is even, we can just use LARL.
2589 This will happen automatically. */
2590 }
2591 }
2592 else
2593 {
2594 /* Access local symbols relative to the GOT. */
2595
2596 rtx temp = reg? reg : gen_reg_rtx (Pmode);
2597
2598 if (reload_in_progress || reload_completed)
2599 regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2600
2601 addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
2602 UNSPEC_GOTOFF);
2603 addr = gen_rtx_PLUS (Pmode, addr, op1);
2604 addr = gen_rtx_CONST (Pmode, addr);
2605 addr = force_const_mem (Pmode, addr);
2606 emit_move_insn (temp, addr);
2607
2608 new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
2609 if (reg != 0)
2610 {
2611 emit_move_insn (reg, new);
2612 new = reg;
2613 }
2614 }
2615 }
2616
2617 /* Now, check whether it is a GOT relative symbol plus offset
2618 that was pulled out of the literal pool. Force it back in. */
2619
2620 else if (GET_CODE (op0) == UNSPEC
2621 && GET_CODE (op1) == CONST_INT)
2622 {
2623 if (XVECLEN (op0, 0) != 1)
2624 abort ();
2625 if (XINT (op0, 1) != UNSPEC_GOTOFF)
2626 abort ();
2627
2628 new = force_const_mem (Pmode, orig);
2629 }
2630
2631 /* Otherwise, compute the sum. */
2632 else
2633 {
2634 base = legitimize_pic_address (XEXP (addr, 0), reg);
2635 new = legitimize_pic_address (XEXP (addr, 1),
2636 base == reg ? NULL_RTX : reg);
2637 if (GET_CODE (new) == CONST_INT)
2638 new = plus_constant (base, INTVAL (new));
2639 else
2640 {
2641 if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
2642 {
2643 base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
2644 new = XEXP (new, 1);
2645 }
2646 new = gen_rtx_PLUS (Pmode, base, new);
2647 }
2648
2649 if (GET_CODE (new) == CONST)
2650 new = XEXP (new, 0);
2651 new = force_operand (new, 0);
2652 }
2653 }
2654 }
2655 return new;
2656 }
2657
2658 /* Load the thread pointer into a register. */
2659
2660 static rtx
2661 get_thread_pointer (void)
2662 {
2663 rtx tp;
2664
2665 tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
2666 tp = force_reg (Pmode, tp);
2667 mark_reg_pointer (tp, BITS_PER_WORD);
2668
2669 return tp;
2670 }
2671
2672 /* Construct the SYMBOL_REF for the tls_get_offset function. */
2673
2674 static GTY(()) rtx s390_tls_symbol;
2675 rtx
2676 s390_tls_get_offset (void)
2677 {
2678 if (!s390_tls_symbol)
2679 s390_tls_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tls_get_offset");
2680
2681 return s390_tls_symbol;
2682 }
2683
2684 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
2685 this (thread-local) address. REG may be used as temporary. */
2686
2687 static rtx
2688 legitimize_tls_address (rtx addr, rtx reg)
2689 {
2690 rtx new, tls_call, temp, base, r2, insn;
2691
2692 if (GET_CODE (addr) == SYMBOL_REF)
2693 switch (tls_symbolic_operand (addr))
2694 {
2695 case TLS_MODEL_GLOBAL_DYNAMIC:
2696 start_sequence ();
2697 r2 = gen_rtx_REG (Pmode, 2);
2698 tls_call = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_TLSGD);
2699 new = gen_rtx_CONST (Pmode, tls_call);
2700 new = force_const_mem (Pmode, new);
2701 emit_move_insn (r2, new);
2702 emit_call_insn (gen_call_value_tls (r2, tls_call));
2703 insn = get_insns ();
2704 end_sequence ();
2705
2706 new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_NTPOFF);
2707 temp = gen_reg_rtx (Pmode);
2708 emit_libcall_block (insn, temp, r2, new);
2709
2710 new = gen_rtx_PLUS (Pmode, get_thread_pointer (), temp);
2711 if (reg != 0)
2712 {
2713 s390_load_address (reg, new);
2714 new = reg;
2715 }
2716 break;
2717
2718 case TLS_MODEL_LOCAL_DYNAMIC:
2719 start_sequence ();
2720 r2 = gen_rtx_REG (Pmode, 2);
2721 tls_call = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TLSLDM);
2722 new = gen_rtx_CONST (Pmode, tls_call);
2723 new = force_const_mem (Pmode, new);
2724 emit_move_insn (r2, new);
2725 emit_call_insn (gen_call_value_tls (r2, tls_call));
2726 insn = get_insns ();
2727 end_sequence ();
2728
2729 new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TLSLDM_NTPOFF);
2730 temp = gen_reg_rtx (Pmode);
2731 emit_libcall_block (insn, temp, r2, new);
2732
2733 new = gen_rtx_PLUS (Pmode, get_thread_pointer (), temp);
2734 base = gen_reg_rtx (Pmode);
2735 s390_load_address (base, new);
2736
2737 new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_DTPOFF);
2738 new = gen_rtx_CONST (Pmode, new);
2739 new = force_const_mem (Pmode, new);
2740 temp = gen_reg_rtx (Pmode);
2741 emit_move_insn (temp, new);
2742
2743 new = gen_rtx_PLUS (Pmode, base, temp);
2744 if (reg != 0)
2745 {
2746 s390_load_address (reg, new);
2747 new = reg;
2748 }
2749 break;
2750
2751 case TLS_MODEL_INITIAL_EXEC:
2752 if (flag_pic == 1)
2753 {
2754 /* Assume GOT offset < 4k. This is handled the same way
2755 in both 31- and 64-bit code. */
2756
2757 if (reload_in_progress || reload_completed)
2758 regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2759
2760 new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTNTPOFF);
2761 new = gen_rtx_CONST (Pmode, new);
2762 new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
2763 new = gen_rtx_MEM (Pmode, new);
2764 RTX_UNCHANGING_P (new) = 1;
2765 temp = gen_reg_rtx (Pmode);
2766 emit_move_insn (temp, new);
2767 }
2768 else if (TARGET_CPU_ZARCH)
2769 {
2770 /* If the GOT offset might be >= 4k, we determine the position
2771 of the GOT entry via a PC-relative LARL. */
2772
2773 new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_INDNTPOFF);
2774 new = gen_rtx_CONST (Pmode, new);
2775 temp = gen_reg_rtx (Pmode);
2776 emit_move_insn (temp, new);
2777
2778 new = gen_rtx_MEM (Pmode, temp);
2779 RTX_UNCHANGING_P (new) = 1;
2780 temp = gen_reg_rtx (Pmode);
2781 emit_move_insn (temp, new);
2782 }
2783 else if (flag_pic)
2784 {
2785 /* If the GOT offset might be >= 4k, we have to load it
2786 from the literal pool. */
2787
2788 if (reload_in_progress || reload_completed)
2789 regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2790
2791 new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTNTPOFF);
2792 new = gen_rtx_CONST (Pmode, new);
2793 new = force_const_mem (Pmode, new);
2794 temp = gen_reg_rtx (Pmode);
2795 emit_move_insn (temp, new);
2796
2797 new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
2798 new = gen_rtx_MEM (Pmode, new);
2799 RTX_UNCHANGING_P (new) = 1;
2800
2801 new = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, new, addr), UNSPEC_TLS_LOAD);
2802 temp = gen_reg_rtx (Pmode);
2803 emit_insn (gen_rtx_SET (Pmode, temp, new));
2804 }
2805 else
2806 {
2807 /* In position-dependent code, load the absolute address of
2808 the GOT entry from the literal pool. */
2809
2810 new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_INDNTPOFF);
2811 new = gen_rtx_CONST (Pmode, new);
2812 new = force_const_mem (Pmode, new);
2813 temp = gen_reg_rtx (Pmode);
2814 emit_move_insn (temp, new);
2815
2816 new = temp;
2817 new = gen_rtx_MEM (Pmode, new);
2818 RTX_UNCHANGING_P (new) = 1;
2819
2820 new = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, new, addr), UNSPEC_TLS_LOAD);
2821 temp = gen_reg_rtx (Pmode);
2822 emit_insn (gen_rtx_SET (Pmode, temp, new));
2823 }
2824
2825 new = gen_rtx_PLUS (Pmode, get_thread_pointer (), temp);
2826 if (reg != 0)
2827 {
2828 s390_load_address (reg, new);
2829 new = reg;
2830 }
2831 break;
2832
2833 case TLS_MODEL_LOCAL_EXEC:
2834 new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_NTPOFF);
2835 new = gen_rtx_CONST (Pmode, new);
2836 new = force_const_mem (Pmode, new);
2837 temp = gen_reg_rtx (Pmode);
2838 emit_move_insn (temp, new);
2839
2840 new = gen_rtx_PLUS (Pmode, get_thread_pointer (), temp);
2841 if (reg != 0)
2842 {
2843 s390_load_address (reg, new);
2844 new = reg;
2845 }
2846 break;
2847
2848 default:
2849 abort ();
2850 }
2851
2852 else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == UNSPEC)
2853 {
2854 switch (XINT (XEXP (addr, 0), 1))
2855 {
2856 case UNSPEC_INDNTPOFF:
2857 if (TARGET_CPU_ZARCH)
2858 new = addr;
2859 else
2860 abort ();
2861 break;
2862
2863 default:
2864 abort ();
2865 }
2866 }
2867
2868 else
2869 abort (); /* for now ... */
2870
2871 return new;
2872 }
2873
2874 /* Emit insns to move operands[1] into operands[0]. */
2875
2876 void
2877 emit_symbolic_move (rtx *operands)
2878 {
2879 rtx temp = no_new_pseudos ? operands[0] : gen_reg_rtx (Pmode);
2880
2881 if (GET_CODE (operands[0]) == MEM)
2882 operands[1] = force_reg (Pmode, operands[1]);
2883 else if (TLS_SYMBOLIC_CONST (operands[1]))
2884 operands[1] = legitimize_tls_address (operands[1], temp);
2885 else if (flag_pic)
2886 operands[1] = legitimize_pic_address (operands[1], temp);
2887 }
2888
2889 /* Try machine-dependent ways of modifying an illegitimate address X
2890 to be legitimate. If we find one, return the new, valid address.
2891
2892 OLDX is the address as it was before break_out_memory_refs was called.
2893 In some cases it is useful to look at this to decide what needs to be done.
2894
2895 MODE is the mode of the operand pointed to by X.
2896
2897 When -fpic is used, special handling is needed for symbolic references.
2898 See comments by legitimize_pic_address for details. */
2899
2900 rtx
2901 legitimize_address (register rtx x, register rtx oldx ATTRIBUTE_UNUSED,
2902 enum machine_mode mode ATTRIBUTE_UNUSED)
2903 {
2904 rtx constant_term = const0_rtx;
2905
2906 if (TLS_SYMBOLIC_CONST (x))
2907 {
2908 x = legitimize_tls_address (x, 0);
2909
2910 if (legitimate_address_p (mode, x, FALSE))
2911 return x;
2912 }
2913 else if (flag_pic)
2914 {
2915 if (SYMBOLIC_CONST (x)
2916 || (GET_CODE (x) == PLUS
2917 && (SYMBOLIC_CONST (XEXP (x, 0))
2918 || SYMBOLIC_CONST (XEXP (x, 1)))))
2919 x = legitimize_pic_address (x, 0);
2920
2921 if (legitimate_address_p (mode, x, FALSE))
2922 return x;
2923 }
2924
2925 x = eliminate_constant_term (x, &constant_term);
2926
2927 /* Optimize loading of large displacements by splitting them
2928 into the multiple of 4K and the rest; this allows the
2929 former to be CSE'd if possible.
2930
2931 Don't do this if the displacement is added to a register
2932 pointing into the stack frame, as the offsets will
2933 change later anyway. */
2934
2935 if (GET_CODE (constant_term) == CONST_INT
2936 && !TARGET_LONG_DISPLACEMENT
2937 && !DISP_IN_RANGE (INTVAL (constant_term))
2938 && !(REG_P (x) && REGNO_PTR_FRAME_P (REGNO (x))))
2939 {
2940 HOST_WIDE_INT lower = INTVAL (constant_term) & 0xfff;
2941 HOST_WIDE_INT upper = INTVAL (constant_term) ^ lower;
2942
2943 rtx temp = gen_reg_rtx (Pmode);
2944 rtx val = force_operand (GEN_INT (upper), temp);
2945 if (val != temp)
2946 emit_move_insn (temp, val);
2947
2948 x = gen_rtx_PLUS (Pmode, x, temp);
2949 constant_term = GEN_INT (lower);
2950 }
2951
2952 if (GET_CODE (x) == PLUS)
2953 {
2954 if (GET_CODE (XEXP (x, 0)) == REG)
2955 {
2956 register rtx temp = gen_reg_rtx (Pmode);
2957 register rtx val = force_operand (XEXP (x, 1), temp);
2958 if (val != temp)
2959 emit_move_insn (temp, val);
2960
2961 x = gen_rtx_PLUS (Pmode, XEXP (x, 0), temp);
2962 }
2963
2964 else if (GET_CODE (XEXP (x, 1)) == REG)
2965 {
2966 register rtx temp = gen_reg_rtx (Pmode);
2967 register rtx val = force_operand (XEXP (x, 0), temp);
2968 if (val != temp)
2969 emit_move_insn (temp, val);
2970
2971 x = gen_rtx_PLUS (Pmode, temp, XEXP (x, 1));
2972 }
2973 }
2974
2975 if (constant_term != const0_rtx)
2976 x = gen_rtx_PLUS (Pmode, x, constant_term);
2977
2978 return x;
2979 }
2980
2981 /* Emit code to move LEN bytes from DST to SRC. */
2982
2983 void
2984 s390_expand_movstr (rtx dst, rtx src, rtx len)
2985 {
2986 if (GET_CODE (len) == CONST_INT && INTVAL (len) >= 0 && INTVAL (len) <= 256)
2987 {
2988 if (INTVAL (len) > 0)
2989 emit_insn (gen_movstr_short (dst, src, GEN_INT (INTVAL (len) - 1)));
2990 }
2991
2992 else if (TARGET_MVCLE)
2993 {
2994 emit_insn (gen_movstr_long (dst, src, convert_to_mode (Pmode, len, 1)));
2995 }
2996
2997 else
2998 {
2999 rtx dst_addr, src_addr, count, blocks, temp;
3000 rtx end_label = gen_label_rtx ();
3001 enum machine_mode mode;
3002 tree type;
3003
3004 mode = GET_MODE (len);
3005 if (mode == VOIDmode)
3006 mode = Pmode;
3007
3008 type = lang_hooks.types.type_for_mode (mode, 1);
3009 if (!type)
3010 abort ();
3011
3012 dst_addr = gen_reg_rtx (Pmode);
3013 src_addr = gen_reg_rtx (Pmode);
3014 count = gen_reg_rtx (mode);
3015 blocks = gen_reg_rtx (mode);
3016
3017 convert_move (count, len, 1);
3018 emit_cmp_and_jump_insns (count, const0_rtx,
3019 EQ, NULL_RTX, mode, 1, end_label);
3020
3021 emit_move_insn (dst_addr, force_operand (XEXP (dst, 0), NULL_RTX));
3022 emit_move_insn (src_addr, force_operand (XEXP (src, 0), NULL_RTX));
3023 dst = change_address (dst, VOIDmode, dst_addr);
3024 src = change_address (src, VOIDmode, src_addr);
3025
3026 temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1, 0);
3027 if (temp != count)
3028 emit_move_insn (count, temp);
3029
3030 temp = expand_binop (mode, ashr_optab, count, GEN_INT (8), blocks, 1, 0);
3031 if (temp != blocks)
3032 emit_move_insn (blocks, temp);
3033
3034 expand_start_loop (1);
3035 expand_exit_loop_top_cond (0, build (NE_EXPR, type,
3036 make_tree (type, blocks),
3037 make_tree (type, const0_rtx)));
3038
3039 emit_insn (gen_movstr_short (dst, src, GEN_INT (255)));
3040 s390_load_address (dst_addr,
3041 gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (256)));
3042 s390_load_address (src_addr,
3043 gen_rtx_PLUS (Pmode, src_addr, GEN_INT (256)));
3044
3045 temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1, 0);
3046 if (temp != blocks)
3047 emit_move_insn (blocks, temp);
3048
3049 expand_end_loop ();
3050
3051 emit_insn (gen_movstr_short (dst, src,
3052 convert_to_mode (Pmode, count, 1)));
3053 emit_label (end_label);
3054 }
3055 }
3056
3057 /* Emit code to clear LEN bytes at DST. */
3058
3059 void
3060 s390_expand_clrstr (rtx dst, rtx len)
3061 {
3062 if (GET_CODE (len) == CONST_INT && INTVAL (len) >= 0 && INTVAL (len) <= 256)
3063 {
3064 if (INTVAL (len) > 0)
3065 emit_insn (gen_clrstr_short (dst, GEN_INT (INTVAL (len) - 1)));
3066 }
3067
3068 else if (TARGET_MVCLE)
3069 {
3070 emit_insn (gen_clrstr_long (dst, convert_to_mode (Pmode, len, 1)));
3071 }
3072
3073 else
3074 {
3075 rtx dst_addr, src_addr, count, blocks, temp;
3076 rtx end_label = gen_label_rtx ();
3077 enum machine_mode mode;
3078 tree type;
3079
3080 mode = GET_MODE (len);
3081 if (mode == VOIDmode)
3082 mode = Pmode;
3083
3084 type = lang_hooks.types.type_for_mode (mode, 1);
3085 if (!type)
3086 abort ();
3087
3088 dst_addr = gen_reg_rtx (Pmode);
3089 src_addr = gen_reg_rtx (Pmode);
3090 count = gen_reg_rtx (mode);
3091 blocks = gen_reg_rtx (mode);
3092
3093 convert_move (count, len, 1);
3094 emit_cmp_and_jump_insns (count, const0_rtx,
3095 EQ, NULL_RTX, mode, 1, end_label);
3096
3097 emit_move_insn (dst_addr, force_operand (XEXP (dst, 0), NULL_RTX));
3098 dst = change_address (dst, VOIDmode, dst_addr);
3099
3100 temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1, 0);
3101 if (temp != count)
3102 emit_move_insn (count, temp);
3103
3104 temp = expand_binop (mode, ashr_optab, count, GEN_INT (8), blocks, 1, 0);
3105 if (temp != blocks)
3106 emit_move_insn (blocks, temp);
3107
3108 expand_start_loop (1);
3109 expand_exit_loop_top_cond (0, build (NE_EXPR, type,
3110 make_tree (type, blocks),
3111 make_tree (type, const0_rtx)));
3112
3113 emit_insn (gen_clrstr_short (dst, GEN_INT (255)));
3114 s390_load_address (dst_addr,
3115 gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (256)));
3116
3117 temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1, 0);
3118 if (temp != blocks)
3119 emit_move_insn (blocks, temp);
3120
3121 expand_end_loop ();
3122
3123 emit_insn (gen_clrstr_short (dst, convert_to_mode (Pmode, count, 1)));
3124 emit_label (end_label);
3125 }
3126 }
3127
3128 /* Emit code to compare LEN bytes at OP0 with those at OP1,
3129 and return the result in TARGET. */
3130
3131 void
3132 s390_expand_cmpmem (rtx target, rtx op0, rtx op1, rtx len)
3133 {
3134 rtx (*gen_result) (rtx) =
3135 GET_MODE (target) == DImode ? gen_cmpint_di : gen_cmpint_si;
3136
3137 op0 = protect_from_queue (op0, 0);
3138 op1 = protect_from_queue (op1, 0);
3139 len = protect_from_queue (len, 0);
3140
3141 if (GET_CODE (len) == CONST_INT && INTVAL (len) >= 0 && INTVAL (len) <= 256)
3142 {
3143 if (INTVAL (len) > 0)
3144 {
3145 emit_insn (gen_cmpmem_short (op0, op1, GEN_INT (INTVAL (len) - 1)));
3146 emit_insn (gen_result (target));
3147 }
3148 else
3149 emit_move_insn (target, const0_rtx);
3150 }
3151
3152 else /* if (TARGET_MVCLE) */
3153 {
3154 emit_insn (gen_cmpmem_long (op0, op1, convert_to_mode (Pmode, len, 1)));
3155 emit_insn (gen_result (target));
3156 }
3157
3158 #if 0
3159 /* Deactivate for now as profile code cannot cope with
3160 CC being live across basic block boundaries. */
3161 else
3162 {
3163 rtx addr0, addr1, count, blocks, temp;
3164 rtx end_label = gen_label_rtx ();
3165 enum machine_mode mode;
3166 tree type;
3167
3168 mode = GET_MODE (len);
3169 if (mode == VOIDmode)
3170 mode = Pmode;
3171
3172 type = lang_hooks.types.type_for_mode (mode, 1);
3173 if (!type)
3174 abort ();
3175
3176 addr0 = gen_reg_rtx (Pmode);
3177 addr1 = gen_reg_rtx (Pmode);
3178 count = gen_reg_rtx (mode);
3179 blocks = gen_reg_rtx (mode);
3180
3181 convert_move (count, len, 1);
3182 emit_cmp_and_jump_insns (count, const0_rtx,
3183 EQ, NULL_RTX, mode, 1, end_label);
3184
3185 emit_move_insn (addr0, force_operand (XEXP (op0, 0), NULL_RTX));
3186 emit_move_insn (addr1, force_operand (XEXP (op1, 0), NULL_RTX));
3187 op0 = change_address (op0, VOIDmode, addr0);
3188 op1 = change_address (op1, VOIDmode, addr1);
3189
3190 temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1, 0);
3191 if (temp != count)
3192 emit_move_insn (count, temp);
3193
3194 temp = expand_binop (mode, ashr_optab, count, GEN_INT (8), blocks, 1, 0);
3195 if (temp != blocks)
3196 emit_move_insn (blocks, temp);
3197
3198 expand_start_loop (1);
3199 expand_exit_loop_top_cond (0, build (NE_EXPR, type,
3200 make_tree (type, blocks),
3201 make_tree (type, const0_rtx)));
3202
3203 emit_insn (gen_cmpmem_short (op0, op1, GEN_INT (255)));
3204 temp = gen_rtx_NE (VOIDmode, gen_rtx_REG (CCSmode, 33), const0_rtx);
3205 temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
3206 gen_rtx_LABEL_REF (VOIDmode, end_label), pc_rtx);
3207 temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
3208 emit_jump_insn (temp);
3209
3210 s390_load_address (addr0,
3211 gen_rtx_PLUS (Pmode, addr0, GEN_INT (256)));
3212 s390_load_address (addr1,
3213 gen_rtx_PLUS (Pmode, addr1, GEN_INT (256)));
3214
3215 temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1, 0);
3216 if (temp != blocks)
3217 emit_move_insn (blocks, temp);
3218
3219 expand_end_loop ();
3220
3221 emit_insn (gen_cmpmem_short (op0, op1,
3222 convert_to_mode (Pmode, count, 1)));
3223 emit_label (end_label);
3224
3225 emit_insn (gen_result (target));
3226 }
3227 #endif
3228 }
3229
3230 /* This is called from dwarf2out.c via ASM_OUTPUT_DWARF_DTPREL.
3231 We need to emit DTP-relative relocations. */
3232
3233 void
3234 s390_output_dwarf_dtprel (FILE *file, int size, rtx x)
3235 {
3236 switch (size)
3237 {
3238 case 4:
3239 fputs ("\t.long\t", file);
3240 break;
3241 case 8:
3242 fputs ("\t.quad\t", file);
3243 break;
3244 default:
3245 abort ();
3246 }
3247 output_addr_const (file, x);
3248 fputs ("@DTPOFF", file);
3249 }
3250
3251 /* In the name of slightly smaller debug output, and to cater to
3252 general assembler losage, recognize various UNSPEC sequences
3253 and turn them back into a direct symbol reference. */
3254
3255 static rtx
3256 s390_delegitimize_address (rtx orig_x)
3257 {
3258 rtx x = orig_x, y;
3259
3260 if (GET_CODE (x) != MEM)
3261 return orig_x;
3262
3263 x = XEXP (x, 0);
3264 if (GET_CODE (x) == PLUS
3265 && GET_CODE (XEXP (x, 1)) == CONST
3266 && GET_CODE (XEXP (x, 0)) == REG
3267 && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
3268 {
3269 y = XEXP (XEXP (x, 1), 0);
3270 if (GET_CODE (y) == UNSPEC
3271 && XINT (y, 1) == UNSPEC_GOT)
3272 return XVECEXP (y, 0, 0);
3273 return orig_x;
3274 }
3275
3276 if (GET_CODE (x) == CONST)
3277 {
3278 y = XEXP (x, 0);
3279 if (GET_CODE (y) == UNSPEC
3280 && XINT (y, 1) == UNSPEC_GOTENT)
3281 return XVECEXP (y, 0, 0);
3282 return orig_x;
3283 }
3284
3285 return orig_x;
3286 }
3287
3288 /* Output shift count operand OP to stdio stream FILE. */
3289
3290 static void
3291 print_shift_count_operand (FILE *file, rtx op)
3292 {
3293 HOST_WIDE_INT offset = 0;
3294
3295 /* We can have an integer constant, an address register,
3296 or a sum of the two. */
3297 if (GET_CODE (op) == CONST_INT)
3298 {
3299 offset = INTVAL (op);
3300 op = NULL_RTX;
3301 }
3302 if (op && GET_CODE (op) == PLUS && GET_CODE (XEXP (op, 1)) == CONST_INT)
3303 {
3304 offset = INTVAL (XEXP (op, 1));
3305 op = XEXP (op, 0);
3306 }
3307 while (op && GET_CODE (op) == SUBREG)
3308 op = SUBREG_REG (op);
3309
3310 /* Sanity check. */
3311 if (op && (GET_CODE (op) != REG
3312 || REGNO (op) >= FIRST_PSEUDO_REGISTER
3313 || REGNO_REG_CLASS (REGNO (op)) != ADDR_REGS))
3314 abort ();
3315
3316 /* Shift counts are truncated to the low six bits anyway. */
3317 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset & 63);
3318 if (op)
3319 fprintf (file, "(%s)", reg_names[REGNO (op)]);
3320 }
3321
3322 /* Locate some local-dynamic symbol still in use by this function
3323 so that we can print its name in local-dynamic base patterns. */
3324
3325 static const char *
3326 get_some_local_dynamic_name (void)
3327 {
3328 rtx insn;
3329
3330 if (cfun->machine->some_ld_name)
3331 return cfun->machine->some_ld_name;
3332
3333 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
3334 if (INSN_P (insn)
3335 && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
3336 return cfun->machine->some_ld_name;
3337
3338 abort ();
3339 }
3340
3341 static int
3342 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
3343 {
3344 rtx x = *px;
3345
3346 if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3347 {
3348 x = get_pool_constant (x);
3349 return for_each_rtx (&x, get_some_local_dynamic_name_1, 0);
3350 }
3351
3352 if (GET_CODE (x) == SYMBOL_REF
3353 && tls_symbolic_operand (x) == TLS_MODEL_LOCAL_DYNAMIC)
3354 {
3355 cfun->machine->some_ld_name = XSTR (x, 0);
3356 return 1;
3357 }
3358
3359 return 0;
3360 }
3361
3362 /* Output symbolic constant X in assembler syntax to
3363 stdio stream FILE. */
3364
3365 void
3366 s390_output_symbolic_const (FILE *file, rtx x)
3367 {
3368 switch (GET_CODE (x))
3369 {
3370 case CONST:
3371 case ZERO_EXTEND:
3372 case SIGN_EXTEND:
3373 s390_output_symbolic_const (file, XEXP (x, 0));
3374 break;
3375
3376 case PLUS:
3377 s390_output_symbolic_const (file, XEXP (x, 0));
3378 fprintf (file, "+");
3379 s390_output_symbolic_const (file, XEXP (x, 1));
3380 break;
3381
3382 case MINUS:
3383 s390_output_symbolic_const (file, XEXP (x, 0));
3384 fprintf (file, "-");
3385 s390_output_symbolic_const (file, XEXP (x, 1));
3386 break;
3387
3388 case CONST_INT:
3389 case LABEL_REF:
3390 case CODE_LABEL:
3391 case SYMBOL_REF:
3392 output_addr_const (file, x);
3393 break;
3394
3395 case UNSPEC:
3396 if (XVECLEN (x, 0) != 1)
3397 output_operand_lossage ("invalid UNSPEC as operand (1)");
3398 switch (XINT (x, 1))
3399 {
3400 case UNSPEC_GOTENT:
3401 s390_output_symbolic_const (file, XVECEXP (x, 0, 0));
3402 fprintf (file, "@GOTENT");
3403 break;
3404 case UNSPEC_GOT:
3405 s390_output_symbolic_const (file, XVECEXP (x, 0, 0));
3406 fprintf (file, "@GOT");
3407 break;
3408 case UNSPEC_GOTOFF:
3409 s390_output_symbolic_const (file, XVECEXP (x, 0, 0));
3410 fprintf (file, "@GOTOFF");
3411 break;
3412 case UNSPEC_PLT:
3413 s390_output_symbolic_const (file, XVECEXP (x, 0, 0));
3414 fprintf (file, "@PLT");
3415 break;
3416 case UNSPEC_PLTOFF:
3417 s390_output_symbolic_const (file, XVECEXP (x, 0, 0));
3418 fprintf (file, "@PLTOFF");
3419 break;
3420 case UNSPEC_TLSGD:
3421 s390_output_symbolic_const (file, XVECEXP (x, 0, 0));
3422 fprintf (file, "@TLSGD");
3423 break;
3424 case UNSPEC_TLSLDM:
3425 assemble_name (file, get_some_local_dynamic_name ());
3426 fprintf (file, "@TLSLDM");
3427 break;
3428 case UNSPEC_DTPOFF:
3429 s390_output_symbolic_const (file, XVECEXP (x, 0, 0));
3430 fprintf (file, "@DTPOFF");
3431 break;
3432 case UNSPEC_NTPOFF:
3433 s390_output_symbolic_const (file, XVECEXP (x, 0, 0));
3434 fprintf (file, "@NTPOFF");
3435 break;
3436 case UNSPEC_GOTNTPOFF:
3437 s390_output_symbolic_const (file, XVECEXP (x, 0, 0));
3438 fprintf (file, "@GOTNTPOFF");
3439 break;
3440 case UNSPEC_INDNTPOFF:
3441 s390_output_symbolic_const (file, XVECEXP (x, 0, 0));
3442 fprintf (file, "@INDNTPOFF");
3443 break;
3444 default:
3445 output_operand_lossage ("invalid UNSPEC as operand (2)");
3446 break;
3447 }
3448 break;
3449
3450 default:
3451 fatal_insn ("UNKNOWN in s390_output_symbolic_const !?", x);
3452 break;
3453 }
3454 }
3455
3456 /* Output address operand ADDR in assembler syntax to
3457 stdio stream FILE. */
3458
3459 void
3460 print_operand_address (FILE *file, rtx addr)
3461 {
3462 struct s390_address ad;
3463
3464 if (!s390_decompose_address (addr, &ad)
3465 || (ad.base && !REG_OK_FOR_BASE_STRICT_P (ad.base))
3466 || (ad.indx && !REG_OK_FOR_INDEX_STRICT_P (ad.indx)))
3467 output_operand_lossage ("Cannot decompose address.");
3468
3469 if (ad.disp)
3470 s390_output_symbolic_const (file, ad.disp);
3471 else
3472 fprintf (file, "0");
3473
3474 if (ad.base && ad.indx)
3475 fprintf (file, "(%s,%s)", reg_names[REGNO (ad.indx)],
3476 reg_names[REGNO (ad.base)]);
3477 else if (ad.base)
3478 fprintf (file, "(%s)", reg_names[REGNO (ad.base)]);
3479 }
3480
3481 /* Output operand X in assembler syntax to stdio stream FILE.
3482 CODE specified the format flag. The following format flags
3483 are recognized:
3484
3485 'C': print opcode suffix for branch condition.
3486 'D': print opcode suffix for inverse branch condition.
3487 'J': print tls_load/tls_gdcall/tls_ldcall suffix
3488 'O': print only the displacement of a memory reference.
3489 'R': print only the base register of a memory reference.
3490 'N': print the second word of a DImode operand.
3491 'M': print the second word of a TImode operand.
3492 'Y': print shift count operand.
3493
3494 'b': print integer X as if it's an unsigned byte.
3495 'x': print integer X as if it's an unsigned word.
3496 'h': print integer X as if it's a signed word. */
3497
3498 void
3499 print_operand (FILE *file, rtx x, int code)
3500 {
3501 switch (code)
3502 {
3503 case 'C':
3504 fprintf (file, s390_branch_condition_mnemonic (x, FALSE));
3505 return;
3506
3507 case 'D':
3508 fprintf (file, s390_branch_condition_mnemonic (x, TRUE));
3509 return;
3510
3511 case 'J':
3512 if (GET_CODE (x) == SYMBOL_REF)
3513 {
3514 fprintf (file, "%s", ":tls_load:");
3515 output_addr_const (file, x);
3516 }
3517 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSGD)
3518 {
3519 fprintf (file, "%s", ":tls_gdcall:");
3520 output_addr_const (file, XVECEXP (x, 0, 0));
3521 }
3522 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSLDM)
3523 {
3524 fprintf (file, "%s", ":tls_ldcall:");
3525 assemble_name (file, get_some_local_dynamic_name ());
3526 }
3527 else
3528 abort ();
3529 return;
3530
3531 case 'O':
3532 {
3533 struct s390_address ad;
3534
3535 if (GET_CODE (x) != MEM
3536 || !s390_decompose_address (XEXP (x, 0), &ad)
3537 || (ad.base && !REG_OK_FOR_BASE_STRICT_P (ad.base))
3538 || ad.indx)
3539 abort ();
3540
3541 if (ad.disp)
3542 s390_output_symbolic_const (file, ad.disp);
3543 else
3544 fprintf (file, "0");
3545 }
3546 return;
3547
3548 case 'R':
3549 {
3550 struct s390_address ad;
3551
3552 if (GET_CODE (x) != MEM
3553 || !s390_decompose_address (XEXP (x, 0), &ad)
3554 || (ad.base && !REG_OK_FOR_BASE_STRICT_P (ad.base))
3555 || ad.indx)
3556 abort ();
3557
3558 if (ad.base)
3559 fprintf (file, "%s", reg_names[REGNO (ad.base)]);
3560 else
3561 fprintf (file, "0");
3562 }
3563 return;
3564
3565 case 'N':
3566 if (GET_CODE (x) == REG)
3567 x = gen_rtx_REG (GET_MODE (x), REGNO (x) + 1);
3568 else if (GET_CODE (x) == MEM)
3569 x = change_address (x, VOIDmode, plus_constant (XEXP (x, 0), 4));
3570 else
3571 abort ();
3572 break;
3573
3574 case 'M':
3575 if (GET_CODE (x) == REG)
3576 x = gen_rtx_REG (GET_MODE (x), REGNO (x) + 1);
3577 else if (GET_CODE (x) == MEM)
3578 x = change_address (x, VOIDmode, plus_constant (XEXP (x, 0), 8));
3579 else
3580 abort ();
3581 break;
3582
3583 case 'Y':
3584 print_shift_count_operand (file, x);
3585 return;
3586 }
3587
3588 switch (GET_CODE (x))
3589 {
3590 case REG:
3591 fprintf (file, "%s", reg_names[REGNO (x)]);
3592 break;
3593
3594 case MEM:
3595 output_address (XEXP (x, 0));
3596 break;
3597
3598 case CONST:
3599 case CODE_LABEL:
3600 case LABEL_REF:
3601 case SYMBOL_REF:
3602 s390_output_symbolic_const (file, x);
3603 break;
3604
3605 case CONST_INT:
3606 if (code == 'b')
3607 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0xff);
3608 else if (code == 'x')
3609 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0xffff);
3610 else if (code == 'h')
3611 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ((INTVAL (x) & 0xffff) ^ 0x8000) - 0x8000);
3612 else
3613 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
3614 break;
3615
3616 case CONST_DOUBLE:
3617 if (GET_MODE (x) != VOIDmode)
3618 abort ();
3619 if (code == 'b')
3620 fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x) & 0xff);
3621 else if (code == 'x')
3622 fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x) & 0xffff);
3623 else if (code == 'h')
3624 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ((CONST_DOUBLE_LOW (x) & 0xffff) ^ 0x8000) - 0x8000);
3625 else
3626 abort ();
3627 break;
3628
3629 default:
3630 fatal_insn ("UNKNOWN in print_operand !?", x);
3631 break;
3632 }
3633 }
3634
3635 /* Target hook for assembling integer objects. We need to define it
3636 here to work a round a bug in some versions of GAS, which couldn't
3637 handle values smaller than INT_MIN when printed in decimal. */
3638
3639 static bool
3640 s390_assemble_integer (rtx x, unsigned int size, int aligned_p)
3641 {
3642 if (size == 8 && aligned_p
3643 && GET_CODE (x) == CONST_INT && INTVAL (x) < INT_MIN)
3644 {
3645 fprintf (asm_out_file, "\t.quad\t" HOST_WIDE_INT_PRINT_HEX "\n",
3646 INTVAL (x));
3647 return true;
3648 }
3649 return default_assemble_integer (x, size, aligned_p);
3650 }
3651
3652 /* Returns true if register REGNO is used for forming
3653 a memory address in expression X. */
3654
3655 static int
3656 reg_used_in_mem_p (int regno, rtx x)
3657 {
3658 enum rtx_code code = GET_CODE (x);
3659 int i, j;
3660 const char *fmt;
3661
3662 if (code == MEM)
3663 {
3664 if (refers_to_regno_p (regno, regno+1,
3665 XEXP (x, 0), 0))
3666 return 1;
3667 }
3668 else if (code == SET
3669 && GET_CODE (SET_DEST (x)) == PC)
3670 {
3671 if (refers_to_regno_p (regno, regno+1,
3672 SET_SRC (x), 0))
3673 return 1;
3674 }
3675
3676 fmt = GET_RTX_FORMAT (code);
3677 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3678 {
3679 if (fmt[i] == 'e'
3680 && reg_used_in_mem_p (regno, XEXP (x, i)))
3681 return 1;
3682
3683 else if (fmt[i] == 'E')
3684 for (j = 0; j < XVECLEN (x, i); j++)
3685 if (reg_used_in_mem_p (regno, XVECEXP (x, i, j)))
3686 return 1;
3687 }
3688 return 0;
3689 }
3690
3691 /* Returns true if expression DEP_RTX sets an address register
3692 used by instruction INSN to address memory. */
3693
3694 static int
3695 addr_generation_dependency_p (rtx dep_rtx, rtx insn)
3696 {
3697 rtx target, pat;
3698
3699 if (GET_CODE (dep_rtx) == INSN)
3700 dep_rtx = PATTERN (dep_rtx);
3701
3702 if (GET_CODE (dep_rtx) == SET)
3703 {
3704 target = SET_DEST (dep_rtx);
3705 if (GET_CODE (target) == STRICT_LOW_PART)
3706 target = XEXP (target, 0);
3707 while (GET_CODE (target) == SUBREG)
3708 target = SUBREG_REG (target);
3709
3710 if (GET_CODE (target) == REG)
3711 {
3712 int regno = REGNO (target);
3713
3714 if (s390_safe_attr_type (insn) == TYPE_LA)
3715 {
3716 pat = PATTERN (insn);
3717 if (GET_CODE (pat) == PARALLEL)
3718 {
3719 if (XVECLEN (pat, 0) != 2)
3720 abort();
3721 pat = XVECEXP (pat, 0, 0);
3722 }
3723 if (GET_CODE (pat) == SET)
3724 return refers_to_regno_p (regno, regno+1, SET_SRC (pat), 0);
3725 else
3726 abort();
3727 }
3728 else if (get_attr_atype (insn) == ATYPE_AGEN)
3729 return reg_used_in_mem_p (regno, PATTERN (insn));
3730 }
3731 }
3732 return 0;
3733 }
3734
3735 /* Return 1, if dep_insn sets register used in insn in the agen unit. */
3736
3737 int
3738 s390_agen_dep_p (rtx dep_insn, rtx insn)
3739 {
3740 rtx dep_rtx = PATTERN (dep_insn);
3741 int i;
3742
3743 if (GET_CODE (dep_rtx) == SET
3744 && addr_generation_dependency_p (dep_rtx, insn))
3745 return 1;
3746 else if (GET_CODE (dep_rtx) == PARALLEL)
3747 {
3748 for (i = 0; i < XVECLEN (dep_rtx, 0); i++)
3749 {
3750 if (addr_generation_dependency_p (XVECEXP (dep_rtx, 0, i), insn))
3751 return 1;
3752 }
3753 }
3754 return 0;
3755 }
3756
3757 /* Return the modified cost of the dependency of instruction INSN
3758 on instruction DEP_INSN through the link LINK. COST is the
3759 default cost of that dependency.
3760
3761 Data dependencies are all handled without delay. However, if a
3762 register is modified and subsequently used as base or index
3763 register of a memory reference, at least 4 cycles need to pass
3764 between setting and using the register to avoid pipeline stalls.
3765 An exception is the LA instruction. An address generated by LA can
3766 be used by introducing only a one cycle stall on the pipeline. */
3767
3768 static int
3769 s390_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
3770 {
3771 rtx dep_rtx;
3772 int i;
3773
3774 /* If the dependence is an anti-dependence, there is no cost. For an
3775 output dependence, there is sometimes a cost, but it doesn't seem
3776 worth handling those few cases. */
3777
3778 if (REG_NOTE_KIND (link) != 0)
3779 return 0;
3780
3781 /* If we can't recognize the insns, we can't really do anything. */
3782 if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
3783 return cost;
3784
3785 /* DFA based scheduling checks address dependency in md file. */
3786 if (s390_use_dfa_pipeline_interface ())
3787 {
3788 /* Operand forward in case of lr, load and la. */
3789 if (s390_tune == PROCESSOR_2084_Z990
3790 && cost == 1
3791 && (s390_safe_attr_type (dep_insn) == TYPE_LA
3792 || s390_safe_attr_type (dep_insn) == TYPE_LR
3793 || s390_safe_attr_type (dep_insn) == TYPE_LOAD))
3794 return 0;
3795 return cost;
3796 }
3797
3798 dep_rtx = PATTERN (dep_insn);
3799
3800 if (GET_CODE (dep_rtx) == SET
3801 && addr_generation_dependency_p (dep_rtx, insn))
3802 cost += (s390_safe_attr_type (dep_insn) == TYPE_LA) ? 1 : 4;
3803 else if (GET_CODE (dep_rtx) == PARALLEL)
3804 {
3805 for (i = 0; i < XVECLEN (dep_rtx, 0); i++)
3806 {
3807 if (addr_generation_dependency_p (XVECEXP (dep_rtx, 0, i), insn))
3808 cost += (s390_safe_attr_type (dep_insn) == TYPE_LA) ? 1 : 4;
3809 }
3810 }
3811
3812 return cost;
3813 }
3814 /* A C statement (sans semicolon) to update the integer scheduling priority
3815 INSN_PRIORITY (INSN). Increase the priority to execute the INSN earlier,
3816 reduce the priority to execute INSN later. Do not define this macro if
3817 you do not need to adjust the scheduling priorities of insns.
3818
3819 A STD instruction should be scheduled earlier,
3820 in order to use the bypass. */
3821
3822 static int
3823 s390_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
3824 {
3825 if (! INSN_P (insn))
3826 return priority;
3827
3828 if (s390_tune != PROCESSOR_2084_Z990)
3829 return priority;
3830
3831 switch (s390_safe_attr_type (insn))
3832 {
3833 case TYPE_FSTORED:
3834 case TYPE_FSTORES:
3835 priority = priority << 3;
3836 break;
3837 case TYPE_STORE:
3838 priority = priority << 1;
3839 break;
3840 default:
3841 break;
3842 }
3843 return priority;
3844 }
3845
3846 /* The number of instructions that can be issued per cycle. */
3847
3848 static int
3849 s390_issue_rate (void)
3850 {
3851 if (s390_tune == PROCESSOR_2084_Z990)
3852 return 3;
3853 return 1;
3854 }
3855
3856 /* If the following function returns TRUE, we will use the the DFA
3857 insn scheduler. */
3858
3859 static int
3860 s390_use_dfa_pipeline_interface (void)
3861 {
3862 if (s390_tune == PROCESSOR_2064_Z900
3863 || s390_tune == PROCESSOR_2084_Z990)
3864 return 1;
3865
3866 return 0;
3867 }
3868
3869 static int
3870 s390_first_cycle_multipass_dfa_lookahead (void)
3871 {
3872 return s390_use_dfa_pipeline_interface () ? 4 : 0;
3873 }
3874
3875 /* Called after issuing each insn.
3876 Triggers default sort algorithm to better slot instructions. */
3877
3878 static int
3879 s390_sched_reorder2 (FILE *dump ATTRIBUTE_UNUSED,
3880 int sched_verbose ATTRIBUTE_UNUSED,
3881 rtx *ready ATTRIBUTE_UNUSED,
3882 int *pn_ready ATTRIBUTE_UNUSED,
3883 int clock_var ATTRIBUTE_UNUSED)
3884 {
3885 return s390_issue_rate();
3886 }
3887
3888
3889 /* Split all branches that exceed the maximum distance.
3890 Returns true if this created a new literal pool entry. */
3891
3892 static int
3893 s390_split_branches (void)
3894 {
3895 rtx temp_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
3896 int new_literal = 0;
3897 rtx insn, pat, tmp, target;
3898 rtx *label;
3899
3900 /* We need correct insn addresses. */
3901
3902 shorten_branches (get_insns ());
3903
3904 /* Find all branches that exceed 64KB, and split them. */
3905
3906 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3907 {
3908 if (GET_CODE (insn) != JUMP_INSN)
3909 continue;
3910
3911 pat = PATTERN (insn);
3912 if (GET_CODE (pat) == PARALLEL && XVECLEN (pat, 0) > 2)
3913 pat = XVECEXP (pat, 0, 0);
3914 if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
3915 continue;
3916
3917 if (GET_CODE (SET_SRC (pat)) == LABEL_REF)
3918 {
3919 label = &SET_SRC (pat);
3920 }
3921 else if (GET_CODE (SET_SRC (pat)) == IF_THEN_ELSE)
3922 {
3923 if (GET_CODE (XEXP (SET_SRC (pat), 1)) == LABEL_REF)
3924 label = &XEXP (SET_SRC (pat), 1);
3925 else if (GET_CODE (XEXP (SET_SRC (pat), 2)) == LABEL_REF)
3926 label = &XEXP (SET_SRC (pat), 2);
3927 else
3928 continue;
3929 }
3930 else
3931 continue;
3932
3933 if (get_attr_length (insn) <= 4)
3934 continue;
3935
3936 /* We are going to use the return register as scratch register,
3937 make sure it will be saved/restored by the prologue/epilogue. */
3938 cfun->machine->save_return_addr_p = 1;
3939
3940 if (!flag_pic)
3941 {
3942 new_literal = 1;
3943 tmp = force_const_mem (Pmode, *label);
3944 tmp = emit_insn_before (gen_rtx_SET (Pmode, temp_reg, tmp), insn);
3945 INSN_ADDRESSES_NEW (tmp, -1);
3946
3947 target = temp_reg;
3948 }
3949 else
3950 {
3951 new_literal = 1;
3952 target = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, *label),
3953 UNSPEC_LTREL_OFFSET);
3954 target = gen_rtx_CONST (Pmode, target);
3955 target = force_const_mem (Pmode, target);
3956 tmp = emit_insn_before (gen_rtx_SET (Pmode, temp_reg, target), insn);
3957 INSN_ADDRESSES_NEW (tmp, -1);
3958
3959 target = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (target, 0)),
3960 UNSPEC_LTREL_BASE);
3961 target = gen_rtx_PLUS (Pmode, temp_reg, target);
3962 }
3963
3964 if (!validate_change (insn, label, target, 0))
3965 abort ();
3966 }
3967
3968 return new_literal;
3969 }
3970
3971
3972 /* Find a literal pool symbol referenced in RTX X, and store
3973 it at REF. Will abort if X contains references to more than
3974 one such pool symbol; multiple references to the same symbol
3975 are allowed, however.
3976
3977 The rtx pointed to by REF must be initialized to NULL_RTX
3978 by the caller before calling this routine. */
3979
3980 static void
3981 find_constant_pool_ref (rtx x, rtx *ref)
3982 {
3983 int i, j;
3984 const char *fmt;
3985
3986 /* Ignore LTREL_BASE references. */
3987 if (GET_CODE (x) == UNSPEC
3988 && XINT (x, 1) == UNSPEC_LTREL_BASE)
3989 return;
3990 /* Likewise POOL_ENTRY insns. */
3991 if (GET_CODE (x) == UNSPEC_VOLATILE
3992 && XINT (x, 1) == UNSPECV_POOL_ENTRY)
3993 return;
3994
3995 if (GET_CODE (x) == SYMBOL_REF
3996 && CONSTANT_POOL_ADDRESS_P (x))
3997 {
3998 if (*ref == NULL_RTX)
3999 *ref = x;
4000 else if (*ref != x)
4001 abort();
4002 }
4003
4004 fmt = GET_RTX_FORMAT (GET_CODE (x));
4005 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
4006 {
4007 if (fmt[i] == 'e')
4008 {
4009 find_constant_pool_ref (XEXP (x, i), ref);
4010 }
4011 else if (fmt[i] == 'E')
4012 {
4013 for (j = 0; j < XVECLEN (x, i); j++)
4014 find_constant_pool_ref (XVECEXP (x, i, j), ref);
4015 }
4016 }
4017 }
4018
4019 /* Replace every reference to the literal pool symbol REF
4020 in X by the address ADDR. Fix up MEMs as required. */
4021
4022 static void
4023 replace_constant_pool_ref (rtx *x, rtx ref, rtx addr)
4024 {
4025 int i, j;
4026 const char *fmt;
4027
4028 if (*x == ref)
4029 abort ();
4030
4031 /* Literal pool references can only occur inside a MEM ... */
4032 if (GET_CODE (*x) == MEM)
4033 {
4034 rtx memref = XEXP (*x, 0);
4035
4036 if (memref == ref)
4037 {
4038 *x = replace_equiv_address (*x, addr);
4039 return;
4040 }
4041
4042 if (GET_CODE (memref) == CONST
4043 && GET_CODE (XEXP (memref, 0)) == PLUS
4044 && GET_CODE (XEXP (XEXP (memref, 0), 1)) == CONST_INT
4045 && XEXP (XEXP (memref, 0), 0) == ref)
4046 {
4047 HOST_WIDE_INT off = INTVAL (XEXP (XEXP (memref, 0), 1));
4048 *x = replace_equiv_address (*x, plus_constant (addr, off));
4049 return;
4050 }
4051 }
4052
4053 /* ... or a load-address type pattern. */
4054 if (GET_CODE (*x) == SET)
4055 {
4056 rtx addrref = SET_SRC (*x);
4057
4058 if (addrref == ref)
4059 {
4060 SET_SRC (*x) = addr;
4061 return;
4062 }
4063
4064 if (GET_CODE (addrref) == CONST
4065 && GET_CODE (XEXP (addrref, 0)) == PLUS
4066 && GET_CODE (XEXP (XEXP (addrref, 0), 1)) == CONST_INT
4067 && XEXP (XEXP (addrref, 0), 0) == ref)
4068 {
4069 HOST_WIDE_INT off = INTVAL (XEXP (XEXP (addrref, 0), 1));
4070 SET_SRC (*x) = plus_constant (addr, off);
4071 return;
4072 }
4073 }
4074
4075 fmt = GET_RTX_FORMAT (GET_CODE (*x));
4076 for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
4077 {
4078 if (fmt[i] == 'e')
4079 {
4080 replace_constant_pool_ref (&XEXP (*x, i), ref, addr);
4081 }
4082 else if (fmt[i] == 'E')
4083 {
4084 for (j = 0; j < XVECLEN (*x, i); j++)
4085 replace_constant_pool_ref (&XVECEXP (*x, i, j), ref, addr);
4086 }
4087 }
4088 }
4089
4090 /* Check whether X contains an UNSPEC_LTREL_BASE.
4091 Return its constant pool symbol if found, NULL_RTX otherwise. */
4092
4093 static rtx
4094 find_ltrel_base (rtx x)
4095 {
4096 int i, j;
4097 const char *fmt;
4098
4099 if (GET_CODE (x) == UNSPEC
4100 && XINT (x, 1) == UNSPEC_LTREL_BASE)
4101 return XVECEXP (x, 0, 0);
4102
4103 fmt = GET_RTX_FORMAT (GET_CODE (x));
4104 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
4105 {
4106 if (fmt[i] == 'e')
4107 {
4108 rtx fnd = find_ltrel_base (XEXP (x, i));
4109 if (fnd)
4110 return fnd;
4111 }
4112 else if (fmt[i] == 'E')
4113 {
4114 for (j = 0; j < XVECLEN (x, i); j++)
4115 {
4116 rtx fnd = find_ltrel_base (XVECEXP (x, i, j));
4117 if (fnd)
4118 return fnd;
4119 }
4120 }
4121 }
4122
4123 return NULL_RTX;
4124 }
4125
4126 /* Replace any occurrence of UNSPEC_LTREL_BASE in X with BASE. */
4127
4128 static void
4129 replace_ltrel_base (rtx *x, rtx base)
4130 {
4131 int i, j;
4132 const char *fmt;
4133
4134 if (GET_CODE (*x) == UNSPEC
4135 && XINT (*x, 1) == UNSPEC_LTREL_BASE)
4136 {
4137 *x = base;
4138 return;
4139 }
4140
4141 fmt = GET_RTX_FORMAT (GET_CODE (*x));
4142 for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
4143 {
4144 if (fmt[i] == 'e')
4145 {
4146 replace_ltrel_base (&XEXP (*x, i), base);
4147 }
4148 else if (fmt[i] == 'E')
4149 {
4150 for (j = 0; j < XVECLEN (*x, i); j++)
4151 replace_ltrel_base (&XVECEXP (*x, i, j), base);
4152 }
4153 }
4154 }
4155
4156
4157 /* We keep a list of constants which we have to add to internal
4158 constant tables in the middle of large functions. */
4159
4160 #define NR_C_MODES 7
4161 enum machine_mode constant_modes[NR_C_MODES] =
4162 {
4163 TImode,
4164 DFmode, DImode,
4165 SFmode, SImode,
4166 HImode,
4167 QImode
4168 };
4169
4170 struct constant
4171 {
4172 struct constant *next;
4173 rtx value;
4174 rtx label;
4175 };
4176
4177 struct constant_pool
4178 {
4179 struct constant_pool *next;
4180 rtx first_insn;
4181 rtx pool_insn;
4182 bitmap insns;
4183
4184 struct constant *constants[NR_C_MODES];
4185 rtx label;
4186 int size;
4187 };
4188
4189 static struct constant_pool * s390_mainpool_start (void);
4190 static void s390_mainpool_finish (struct constant_pool *, rtx base_reg);
4191 static void s390_mainpool_cancel (struct constant_pool *);
4192
4193 static struct constant_pool * s390_chunkify_start (rtx base_reg);
4194 static void s390_chunkify_finish (struct constant_pool *, rtx base_reg);
4195 static void s390_chunkify_cancel (struct constant_pool *);
4196
4197 static struct constant_pool *s390_start_pool (struct constant_pool **, rtx);
4198 static void s390_end_pool (struct constant_pool *, rtx);
4199 static void s390_add_pool_insn (struct constant_pool *, rtx);
4200 static struct constant_pool *s390_find_pool (struct constant_pool *, rtx);
4201 static void s390_add_constant (struct constant_pool *, rtx, enum machine_mode);
4202 static rtx s390_find_constant (struct constant_pool *, rtx, enum machine_mode);
4203 static rtx s390_dump_pool (struct constant_pool *, bool);
4204 static struct constant_pool *s390_alloc_pool (void);
4205 static void s390_free_pool (struct constant_pool *);
4206
4207 /* Create new constant pool covering instructions starting at INSN
4208 and chain it to the end of POOL_LIST. */
4209
4210 static struct constant_pool *
4211 s390_start_pool (struct constant_pool **pool_list, rtx insn)
4212 {
4213 struct constant_pool *pool, **prev;
4214
4215 pool = s390_alloc_pool ();
4216 pool->first_insn = insn;
4217
4218 for (prev = pool_list; *prev; prev = &(*prev)->next)
4219 ;
4220 *prev = pool;
4221
4222 return pool;
4223 }
4224
4225 /* End range of instructions covered by POOL at INSN and emit
4226 placeholder insn representing the pool. */
4227
4228 static void
4229 s390_end_pool (struct constant_pool *pool, rtx insn)
4230 {
4231 rtx pool_size = GEN_INT (pool->size + 8 /* alignment slop */);
4232
4233 if (!insn)
4234 insn = get_last_insn ();
4235
4236 pool->pool_insn = emit_insn_after (gen_pool (pool_size), insn);
4237 INSN_ADDRESSES_NEW (pool->pool_insn, -1);
4238 }
4239
4240 /* Add INSN to the list of insns covered by POOL. */
4241
4242 static void
4243 s390_add_pool_insn (struct constant_pool *pool, rtx insn)
4244 {
4245 bitmap_set_bit (pool->insns, INSN_UID (insn));
4246 }
4247
4248 /* Return pool out of POOL_LIST that covers INSN. */
4249
4250 static struct constant_pool *
4251 s390_find_pool (struct constant_pool *pool_list, rtx insn)
4252 {
4253 struct constant_pool *pool;
4254
4255 for (pool = pool_list; pool; pool = pool->next)
4256 if (bitmap_bit_p (pool->insns, INSN_UID (insn)))
4257 break;
4258
4259 return pool;
4260 }
4261
4262 /* Add constant VAL of mode MODE to the constant pool POOL. */
4263
4264 static void
4265 s390_add_constant (struct constant_pool *pool, rtx val, enum machine_mode mode)
4266 {
4267 struct constant *c;
4268 int i;
4269
4270 for (i = 0; i < NR_C_MODES; i++)
4271 if (constant_modes[i] == mode)
4272 break;
4273 if (i == NR_C_MODES)
4274 abort ();
4275
4276 for (c = pool->constants[i]; c != NULL; c = c->next)
4277 if (rtx_equal_p (val, c->value))
4278 break;
4279
4280 if (c == NULL)
4281 {
4282 c = (struct constant *) xmalloc (sizeof *c);
4283 c->value = val;
4284 c->label = gen_label_rtx ();
4285 c->next = pool->constants[i];
4286 pool->constants[i] = c;
4287 pool->size += GET_MODE_SIZE (mode);
4288 }
4289 }
4290
4291 /* Find constant VAL of mode MODE in the constant pool POOL.
4292 Return an RTX describing the distance from the start of
4293 the pool to the location of the new constant. */
4294
4295 static rtx
4296 s390_find_constant (struct constant_pool *pool, rtx val,
4297 enum machine_mode mode)
4298 {
4299 struct constant *c;
4300 rtx offset;
4301 int i;
4302
4303 for (i = 0; i < NR_C_MODES; i++)
4304 if (constant_modes[i] == mode)
4305 break;
4306 if (i == NR_C_MODES)
4307 abort ();
4308
4309 for (c = pool->constants[i]; c != NULL; c = c->next)
4310 if (rtx_equal_p (val, c->value))
4311 break;
4312
4313 if (c == NULL)
4314 abort ();
4315
4316 offset = gen_rtx_MINUS (Pmode, gen_rtx_LABEL_REF (Pmode, c->label),
4317 gen_rtx_LABEL_REF (Pmode, pool->label));
4318 offset = gen_rtx_CONST (Pmode, offset);
4319 return offset;
4320 }
4321
4322 /* Dump out the constants in POOL. If REMOTE_LABEL is true,
4323 do not emit the pool base label. */
4324
4325 static rtx
4326 s390_dump_pool (struct constant_pool *pool, bool remote_label)
4327 {
4328 struct constant *c;
4329 rtx insn;
4330 int i;
4331
4332 /* Pool start insn switches to proper section
4333 and guarantees necessary alignment. */
4334 if (TARGET_CPU_ZARCH)
4335 insn = emit_insn_after (gen_pool_start_64 (), pool->pool_insn);
4336 else
4337 insn = emit_insn_after (gen_pool_start_31 (), pool->pool_insn);
4338 INSN_ADDRESSES_NEW (insn, -1);
4339
4340 if (!remote_label)
4341 {
4342 insn = emit_label_after (pool->label, insn);
4343 INSN_ADDRESSES_NEW (insn, -1);
4344 }
4345
4346 /* Dump constants in descending alignment requirement order,
4347 ensuring proper alignment for every constant. */
4348 for (i = 0; i < NR_C_MODES; i++)
4349 for (c = pool->constants[i]; c; c = c->next)
4350 {
4351 /* Convert UNSPEC_LTREL_OFFSET unspecs to pool-relative references. */
4352 rtx value = c->value;
4353 if (GET_CODE (value) == CONST
4354 && GET_CODE (XEXP (value, 0)) == UNSPEC
4355 && XINT (XEXP (value, 0), 1) == UNSPEC_LTREL_OFFSET
4356 && XVECLEN (XEXP (value, 0), 0) == 1)
4357 {
4358 value = gen_rtx_MINUS (Pmode, XVECEXP (XEXP (value, 0), 0, 0),
4359 gen_rtx_LABEL_REF (VOIDmode, pool->label));
4360 value = gen_rtx_CONST (VOIDmode, value);
4361 }
4362
4363 insn = emit_label_after (c->label, insn);
4364 INSN_ADDRESSES_NEW (insn, -1);
4365
4366 value = gen_rtx_UNSPEC_VOLATILE (constant_modes[i],
4367 gen_rtvec (1, value),
4368 UNSPECV_POOL_ENTRY);
4369 insn = emit_insn_after (value, insn);
4370 INSN_ADDRESSES_NEW (insn, -1);
4371 }
4372
4373 /* Pool end insn switches back to previous section
4374 and guarantees necessary alignment. */
4375 if (TARGET_CPU_ZARCH)
4376 insn = emit_insn_after (gen_pool_end_64 (), insn);
4377 else
4378 insn = emit_insn_after (gen_pool_end_31 (), insn);
4379 INSN_ADDRESSES_NEW (insn, -1);
4380
4381 insn = emit_barrier_after (insn);
4382 INSN_ADDRESSES_NEW (insn, -1);
4383
4384 /* Remove placeholder insn. */
4385 remove_insn (pool->pool_insn);
4386
4387 return insn;
4388 }
4389
4390 /* Allocate new constant_pool structure. */
4391
4392 static struct constant_pool *
4393 s390_alloc_pool (void)
4394 {
4395 struct constant_pool *pool;
4396 int i;
4397
4398 pool = (struct constant_pool *) xmalloc (sizeof *pool);
4399 pool->next = NULL;
4400 for (i = 0; i < NR_C_MODES; i++)
4401 pool->constants[i] = NULL;
4402
4403 pool->label = gen_label_rtx ();
4404 pool->first_insn = NULL_RTX;
4405 pool->pool_insn = NULL_RTX;
4406 pool->insns = BITMAP_XMALLOC ();
4407 pool->size = 0;
4408
4409 return pool;
4410 }
4411
4412 /* Free all memory used by POOL. */
4413
4414 static void
4415 s390_free_pool (struct constant_pool *pool)
4416 {
4417 int i;
4418
4419 for (i = 0; i < NR_C_MODES; i++)
4420 {
4421 struct constant *c = pool->constants[i];
4422 while (c != NULL)
4423 {
4424 struct constant *next = c->next;
4425 free (c);
4426 c = next;
4427 }
4428 }
4429
4430 BITMAP_XFREE (pool->insns);
4431 free (pool);
4432 }
4433
4434
4435 /* Collect main literal pool. Return NULL on overflow. */
4436
4437 static struct constant_pool *
4438 s390_mainpool_start (void)
4439 {
4440 struct constant_pool *pool;
4441 rtx insn;
4442
4443 pool = s390_alloc_pool ();
4444
4445 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4446 {
4447 if (GET_CODE (insn) == INSN
4448 && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
4449 && XINT (PATTERN (insn), 1) == UNSPECV_MAIN_POOL)
4450 {
4451 if (pool->pool_insn)
4452 abort ();
4453 pool->pool_insn = insn;
4454 }
4455
4456 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
4457 {
4458 rtx pool_ref = NULL_RTX;
4459 find_constant_pool_ref (PATTERN (insn), &pool_ref);
4460 if (pool_ref)
4461 {
4462 rtx constant = get_pool_constant (pool_ref);
4463 enum machine_mode mode = get_pool_mode (pool_ref);
4464 s390_add_constant (pool, constant, mode);
4465 }
4466 }
4467 }
4468
4469 if (!pool->pool_insn)
4470 abort ();
4471
4472 if (pool->size >= 4096)
4473 {
4474 s390_free_pool (pool);
4475 pool = NULL;
4476 }
4477
4478 return pool;
4479 }
4480
4481 /* POOL holds the main literal pool as collected by s390_mainpool_start.
4482 Modify the current function to output the pool constants as well as
4483 the pool register setup instruction. BASE_REG is the register to
4484 be used as pool base register. */
4485
4486 static void
4487 s390_mainpool_finish (struct constant_pool *pool, rtx base_reg)
4488 {
4489 rtx insn;
4490
4491 /* If the pool is empty, we're done. */
4492 if (pool->size == 0)
4493 {
4494 remove_insn (pool->pool_insn);
4495 s390_free_pool (pool);
4496 return;
4497 }
4498
4499 /* We need correct insn addresses. */
4500 shorten_branches (get_insns ());
4501
4502 /* On zSeries, we use a LARL to load the pool register. The pool is
4503 located in the .rodata section, so we emit it after the function. */
4504 if (TARGET_CPU_ZARCH)
4505 {
4506 insn = gen_main_base_64 (base_reg, pool->label);
4507 insn = emit_insn_after (insn, pool->pool_insn);
4508 INSN_ADDRESSES_NEW (insn, -1);
4509 remove_insn (pool->pool_insn);
4510
4511 insn = get_last_insn ();
4512 pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
4513 INSN_ADDRESSES_NEW (pool->pool_insn, -1);
4514
4515 s390_dump_pool (pool, 0);
4516 }
4517
4518 /* On S/390, if the total size of the function's code plus literal pool
4519 does not exceed 4096 bytes, we use BASR to set up a function base
4520 pointer, and emit the literal pool at the end of the function. */
4521 else if (INSN_ADDRESSES (INSN_UID (get_last_insn ()))
4522 + pool->size + 8 /* alignment slop */ < 4096)
4523 {
4524 insn = gen_main_base_31_small (base_reg, pool->label);
4525 insn = emit_insn_after (insn, pool->pool_insn);
4526 INSN_ADDRESSES_NEW (insn, -1);
4527 remove_insn (pool->pool_insn);
4528
4529 insn = emit_label_after (pool->label, insn);
4530 INSN_ADDRESSES_NEW (insn, -1);
4531
4532 insn = get_last_insn ();
4533 pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
4534 INSN_ADDRESSES_NEW (pool->pool_insn, -1);
4535
4536 s390_dump_pool (pool, 1);
4537 }
4538
4539 /* Otherwise, we emit an inline literal pool and use BASR to branch
4540 over it, setting up the pool register at the same time. */
4541 else
4542 {
4543 rtx pool_end = gen_label_rtx ();
4544
4545 insn = gen_main_base_31_large (base_reg, pool->label, pool_end);
4546 insn = emit_insn_after (insn, pool->pool_insn);
4547 INSN_ADDRESSES_NEW (insn, -1);
4548 remove_insn (pool->pool_insn);
4549
4550 insn = emit_label_after (pool->label, insn);
4551 INSN_ADDRESSES_NEW (insn, -1);
4552
4553 pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
4554 INSN_ADDRESSES_NEW (pool->pool_insn, -1);
4555
4556 insn = emit_label_after (pool_end, pool->pool_insn);
4557 INSN_ADDRESSES_NEW (insn, -1);
4558
4559 s390_dump_pool (pool, 1);
4560 }
4561
4562
4563 /* Replace all literal pool references. */
4564
4565 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4566 {
4567 if (INSN_P (insn))
4568 replace_ltrel_base (&PATTERN (insn), base_reg);
4569
4570 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
4571 {
4572 rtx addr, pool_ref = NULL_RTX;
4573 find_constant_pool_ref (PATTERN (insn), &pool_ref);
4574 if (pool_ref)
4575 {
4576 addr = s390_find_constant (pool, get_pool_constant (pool_ref),
4577 get_pool_mode (pool_ref));
4578 addr = gen_rtx_PLUS (Pmode, base_reg, addr);
4579 replace_constant_pool_ref (&PATTERN (insn), pool_ref, addr);
4580 INSN_CODE (insn) = -1;
4581 }
4582 }
4583 }
4584
4585
4586 /* Free the pool. */
4587 s390_free_pool (pool);
4588 }
4589
4590 /* POOL holds the main literal pool as collected by s390_mainpool_start.
4591 We have decided we cannot use this pool, so revert all changes
4592 to the current function that were done by s390_mainpool_start. */
4593 static void
4594 s390_mainpool_cancel (struct constant_pool *pool)
4595 {
4596 /* We didn't actually change the instruction stream, so simply
4597 free the pool memory. */
4598 s390_free_pool (pool);
4599 }
4600
4601
4602 /* Chunkify the literal pool. BASE_REG is to be used as pool
4603 register. */
4604
4605 #define S390_POOL_CHUNK_MIN 0xc00
4606 #define S390_POOL_CHUNK_MAX 0xe00
4607
4608 static struct constant_pool *
4609 s390_chunkify_start (rtx base_reg)
4610 {
4611 struct constant_pool *curr_pool = NULL, *pool_list = NULL;
4612 int extra_size = 0;
4613 bitmap far_labels;
4614 rtx pending_ltrel = NULL_RTX;
4615 rtx insn;
4616
4617 rtx (*gen_reload_base) (rtx, rtx) =
4618 TARGET_CPU_ZARCH? gen_reload_base_64 : gen_reload_base_31;
4619
4620
4621 /* We need correct insn addresses. */
4622
4623 shorten_branches (get_insns ());
4624
4625 /* Scan all insns and move literals to pool chunks. */
4626
4627 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4628 {
4629 /* Check for pending LTREL_BASE. */
4630 if (INSN_P (insn))
4631 {
4632 rtx ltrel_base = find_ltrel_base (PATTERN (insn));
4633 if (ltrel_base)
4634 {
4635 if (ltrel_base == pending_ltrel)
4636 pending_ltrel = NULL_RTX;
4637 else
4638 abort ();
4639 }
4640 }
4641
4642 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
4643 {
4644 rtx pool_ref = NULL_RTX;
4645 find_constant_pool_ref (PATTERN (insn), &pool_ref);
4646 if (pool_ref)
4647 {
4648 rtx constant = get_pool_constant (pool_ref);
4649 enum machine_mode mode = get_pool_mode (pool_ref);
4650
4651 if (!curr_pool)
4652 curr_pool = s390_start_pool (&pool_list, insn);
4653
4654 s390_add_constant (curr_pool, constant, mode);
4655 s390_add_pool_insn (curr_pool, insn);
4656
4657 /* Don't split the pool chunk between a LTREL_OFFSET load
4658 and the corresponding LTREL_BASE. */
4659 if (GET_CODE (constant) == CONST
4660 && GET_CODE (XEXP (constant, 0)) == UNSPEC
4661 && XINT (XEXP (constant, 0), 1) == UNSPEC_LTREL_OFFSET)
4662 {
4663 if (pending_ltrel)
4664 abort ();
4665 pending_ltrel = pool_ref;
4666 }
4667 }
4668 }
4669
4670 if (GET_CODE (insn) == JUMP_INSN || GET_CODE (insn) == CODE_LABEL)
4671 {
4672 if (curr_pool)
4673 s390_add_pool_insn (curr_pool, insn);
4674 /* An LTREL_BASE must follow within the same basic block. */
4675 if (pending_ltrel)
4676 abort ();
4677 }
4678
4679 if (!curr_pool
4680 || INSN_ADDRESSES_SIZE () <= (size_t) INSN_UID (insn)
4681 || INSN_ADDRESSES (INSN_UID (insn)) == -1)
4682 continue;
4683
4684 if (TARGET_CPU_ZARCH)
4685 {
4686 if (curr_pool->size < S390_POOL_CHUNK_MAX)
4687 continue;
4688
4689 s390_end_pool (curr_pool, NULL_RTX);
4690 curr_pool = NULL;
4691 }
4692 else
4693 {
4694 int chunk_size = INSN_ADDRESSES (INSN_UID (insn))
4695 - INSN_ADDRESSES (INSN_UID (curr_pool->first_insn))
4696 + extra_size;
4697
4698 /* We will later have to insert base register reload insns.
4699 Those will have an effect on code size, which we need to
4700 consider here. This calculation makes rather pessimistic
4701 worst-case assumptions. */
4702 if (GET_CODE (insn) == CODE_LABEL)
4703 extra_size += 6;
4704
4705 if (chunk_size < S390_POOL_CHUNK_MIN
4706 && curr_pool->size < S390_POOL_CHUNK_MIN)
4707 continue;
4708
4709 /* Pool chunks can only be inserted after BARRIERs ... */
4710 if (GET_CODE (insn) == BARRIER)
4711 {
4712 s390_end_pool (curr_pool, insn);
4713 curr_pool = NULL;
4714 extra_size = 0;
4715 }
4716
4717 /* ... so if we don't find one in time, create one. */
4718 else if ((chunk_size > S390_POOL_CHUNK_MAX
4719 || curr_pool->size > S390_POOL_CHUNK_MAX))
4720 {
4721 rtx label, jump, barrier;
4722
4723 /* We can insert the barrier only after a 'real' insn. */
4724 if (GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
4725 continue;
4726 if (get_attr_length (insn) == 0)
4727 continue;
4728
4729 /* Don't separate LTREL_BASE from the corresponding
4730 LTREL_OFFSET load. */
4731 if (pending_ltrel)
4732 continue;
4733
4734 label = gen_label_rtx ();
4735 jump = emit_jump_insn_after (gen_jump (label), insn);
4736 barrier = emit_barrier_after (jump);
4737 insn = emit_label_after (label, barrier);
4738 JUMP_LABEL (jump) = label;
4739 LABEL_NUSES (label) = 1;
4740
4741 INSN_ADDRESSES_NEW (jump, -1);
4742 INSN_ADDRESSES_NEW (barrier, -1);
4743 INSN_ADDRESSES_NEW (insn, -1);
4744
4745 s390_end_pool (curr_pool, barrier);
4746 curr_pool = NULL;
4747 extra_size = 0;
4748 }
4749 }
4750 }
4751
4752 if (curr_pool)
4753 s390_end_pool (curr_pool, NULL_RTX);
4754 if (pending_ltrel)
4755 abort ();
4756
4757
4758 /* Find all labels that are branched into
4759 from an insn belonging to a different chunk. */
4760
4761 far_labels = BITMAP_XMALLOC ();
4762
4763 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4764 {
4765 /* Labels marked with LABEL_PRESERVE_P can be target
4766 of non-local jumps, so we have to mark them.
4767 The same holds for named labels.
4768
4769 Don't do that, however, if it is the label before
4770 a jump table. */
4771
4772 if (GET_CODE (insn) == CODE_LABEL
4773 && (LABEL_PRESERVE_P (insn) || LABEL_NAME (insn)))
4774 {
4775 rtx vec_insn = next_real_insn (insn);
4776 rtx vec_pat = vec_insn && GET_CODE (vec_insn) == JUMP_INSN ?
4777 PATTERN (vec_insn) : NULL_RTX;
4778 if (!vec_pat
4779 || !(GET_CODE (vec_pat) == ADDR_VEC
4780 || GET_CODE (vec_pat) == ADDR_DIFF_VEC))
4781 bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (insn));
4782 }
4783
4784 /* If we have a direct jump (conditional or unconditional)
4785 or a casesi jump, check all potential targets. */
4786 else if (GET_CODE (insn) == JUMP_INSN)
4787 {
4788 rtx pat = PATTERN (insn);
4789 if (GET_CODE (pat) == PARALLEL && XVECLEN (pat, 0) > 2)
4790 pat = XVECEXP (pat, 0, 0);
4791
4792 if (GET_CODE (pat) == SET)
4793 {
4794 rtx label = JUMP_LABEL (insn);
4795 if (label)
4796 {
4797 if (s390_find_pool (pool_list, label)
4798 != s390_find_pool (pool_list, insn))
4799 bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (label));
4800 }
4801 }
4802 else if (GET_CODE (pat) == PARALLEL
4803 && XVECLEN (pat, 0) == 2
4804 && GET_CODE (XVECEXP (pat, 0, 0)) == SET
4805 && GET_CODE (XVECEXP (pat, 0, 1)) == USE
4806 && GET_CODE (XEXP (XVECEXP (pat, 0, 1), 0)) == LABEL_REF)
4807 {
4808 /* Find the jump table used by this casesi jump. */
4809 rtx vec_label = XEXP (XEXP (XVECEXP (pat, 0, 1), 0), 0);
4810 rtx vec_insn = next_real_insn (vec_label);
4811 rtx vec_pat = vec_insn && GET_CODE (vec_insn) == JUMP_INSN ?
4812 PATTERN (vec_insn) : NULL_RTX;
4813 if (vec_pat
4814 && (GET_CODE (vec_pat) == ADDR_VEC
4815 || GET_CODE (vec_pat) == ADDR_DIFF_VEC))
4816 {
4817 int i, diff_p = GET_CODE (vec_pat) == ADDR_DIFF_VEC;
4818
4819 for (i = 0; i < XVECLEN (vec_pat, diff_p); i++)
4820 {
4821 rtx label = XEXP (XVECEXP (vec_pat, diff_p, i), 0);
4822
4823 if (s390_find_pool (pool_list, label)
4824 != s390_find_pool (pool_list, insn))
4825 bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (label));
4826 }
4827 }
4828 }
4829 }
4830 }
4831
4832 /* Insert base register reload insns before every pool. */
4833
4834 for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
4835 {
4836 rtx new_insn = gen_reload_base (base_reg, curr_pool->label);
4837 rtx insn = curr_pool->first_insn;
4838 INSN_ADDRESSES_NEW (emit_insn_before (new_insn, insn), -1);
4839 }
4840
4841 /* Insert base register reload insns at every far label. */
4842
4843 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4844 if (GET_CODE (insn) == CODE_LABEL
4845 && bitmap_bit_p (far_labels, CODE_LABEL_NUMBER (insn)))
4846 {
4847 struct constant_pool *pool = s390_find_pool (pool_list, insn);
4848 if (pool)
4849 {
4850 rtx new_insn = gen_reload_base (base_reg, pool->label);
4851 INSN_ADDRESSES_NEW (emit_insn_after (new_insn, insn), -1);
4852 }
4853 }
4854
4855
4856 BITMAP_XFREE (far_labels);
4857
4858
4859 /* Recompute insn addresses. */
4860
4861 init_insn_lengths ();
4862 shorten_branches (get_insns ());
4863
4864 return pool_list;
4865 }
4866
4867 /* POOL_LIST is a chunk list as prepared by s390_chunkify_start.
4868 After we have decided to use this list, finish implementing
4869 all changes to the current function as required. BASE_REG is
4870 to be used as pool base register. */
4871
4872 static void
4873 s390_chunkify_finish (struct constant_pool *pool_list, rtx base_reg)
4874 {
4875 struct constant_pool *curr_pool = NULL;
4876 rtx insn;
4877
4878
4879 /* Replace all literal pool references. */
4880
4881 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4882 {
4883 if (INSN_P (insn))
4884 replace_ltrel_base (&PATTERN (insn), base_reg);
4885
4886 curr_pool = s390_find_pool (pool_list, insn);
4887 if (!curr_pool)
4888 continue;
4889
4890 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
4891 {
4892 rtx addr, pool_ref = NULL_RTX;
4893 find_constant_pool_ref (PATTERN (insn), &pool_ref);
4894 if (pool_ref)
4895 {
4896 addr = s390_find_constant (curr_pool, get_pool_constant (pool_ref),
4897 get_pool_mode (pool_ref));
4898 addr = gen_rtx_PLUS (Pmode, base_reg, addr);
4899 replace_constant_pool_ref (&PATTERN (insn), pool_ref, addr);
4900 INSN_CODE (insn) = -1;
4901 }
4902 }
4903 }
4904
4905 /* Dump out all literal pools. */
4906
4907 for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
4908 s390_dump_pool (curr_pool, 0);
4909
4910 /* Free pool list. */
4911
4912 while (pool_list)
4913 {
4914 struct constant_pool *next = pool_list->next;
4915 s390_free_pool (pool_list);
4916 pool_list = next;
4917 }
4918 }
4919
4920 /* POOL_LIST is a chunk list as prepared by s390_chunkify_start.
4921 We have decided we cannot use this list, so revert all changes
4922 to the current function that were done by s390_chunkify_start. */
4923
4924 static void
4925 s390_chunkify_cancel (struct constant_pool *pool_list)
4926 {
4927 struct constant_pool *curr_pool = NULL;
4928 rtx insn;
4929
4930 /* Remove all pool placeholder insns. */
4931
4932 for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
4933 {
4934 /* Did we insert an extra barrier? Remove it. */
4935 rtx barrier = PREV_INSN (curr_pool->pool_insn);
4936 rtx jump = barrier? PREV_INSN (barrier) : NULL_RTX;
4937 rtx label = NEXT_INSN (curr_pool->pool_insn);
4938
4939 if (jump && GET_CODE (jump) == JUMP_INSN
4940 && barrier && GET_CODE (barrier) == BARRIER
4941 && label && GET_CODE (label) == CODE_LABEL
4942 && GET_CODE (PATTERN (jump)) == SET
4943 && SET_DEST (PATTERN (jump)) == pc_rtx
4944 && GET_CODE (SET_SRC (PATTERN (jump))) == LABEL_REF
4945 && XEXP (SET_SRC (PATTERN (jump)), 0) == label)
4946 {
4947 remove_insn (jump);
4948 remove_insn (barrier);
4949 remove_insn (label);
4950 }
4951
4952 remove_insn (curr_pool->pool_insn);
4953 }
4954
4955 /* Remove all base register reload insns. */
4956
4957 for (insn = get_insns (); insn; )
4958 {
4959 rtx next_insn = NEXT_INSN (insn);
4960
4961 if (GET_CODE (insn) == INSN
4962 && GET_CODE (PATTERN (insn)) == SET
4963 && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC
4964 && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPEC_RELOAD_BASE)
4965 remove_insn (insn);
4966
4967 insn = next_insn;
4968 }
4969
4970 /* Free pool list. */
4971
4972 while (pool_list)
4973 {
4974 struct constant_pool *next = pool_list->next;
4975 s390_free_pool (pool_list);
4976 pool_list = next;
4977 }
4978 }
4979
4980
4981 /* Output to FILE the constant pool entry EXP in mode MODE
4982 with alignment ALIGN. */
4983
4984 void
4985 s390_output_pool_entry (FILE *file, rtx exp, enum machine_mode mode,
4986 unsigned int align)
4987 {
4988 REAL_VALUE_TYPE r;
4989
4990 switch (GET_MODE_CLASS (mode))
4991 {
4992 case MODE_FLOAT:
4993 if (GET_CODE (exp) != CONST_DOUBLE)
4994 abort ();
4995
4996 REAL_VALUE_FROM_CONST_DOUBLE (r, exp);
4997 assemble_real (r, mode, align);
4998 break;
4999
5000 case MODE_INT:
5001 if (GET_CODE (exp) == CONST
5002 || GET_CODE (exp) == SYMBOL_REF
5003 || GET_CODE (exp) == LABEL_REF)
5004 {
5005 fputs (integer_asm_op (GET_MODE_SIZE (mode), TRUE), file);
5006 s390_output_symbolic_const (file, exp);
5007 fputc ('\n', file);
5008 }
5009 else
5010 {
5011 assemble_integer (exp, GET_MODE_SIZE (mode), align, 1);
5012 }
5013 break;
5014
5015 default:
5016 abort ();
5017 }
5018 }
5019
5020
5021 /* Rework the prolog/epilog to avoid saving/restoring
5022 registers unnecessarily. BASE_USED specifies whether
5023 the literal pool base register needs to be saved. */
5024
5025 static void
5026 s390_optimize_prolog (bool base_used)
5027 {
5028 int save_first, save_last, restore_first, restore_last;
5029 int i, j;
5030 rtx insn, new_insn, next_insn;
5031
5032 /* Recompute regs_ever_live data for special registers. */
5033 regs_ever_live[BASE_REGISTER] = base_used;
5034 regs_ever_live[RETURN_REGNUM] = cfun->machine->save_return_addr_p;
5035 regs_ever_live[STACK_POINTER_REGNUM] = cfun->machine->frame_size > 0;
5036
5037
5038 /* Find first and last gpr to be saved. */
5039
5040 for (i = 6; i < 16; i++)
5041 if (regs_ever_live[i])
5042 if (!global_regs[i]
5043 || i == STACK_POINTER_REGNUM
5044 || i == RETURN_REGNUM
5045 || i == BASE_REGISTER
5046 || (flag_pic && i == (int)PIC_OFFSET_TABLE_REGNUM))
5047 break;
5048
5049 for (j = 15; j > i; j--)
5050 if (regs_ever_live[j])
5051 if (!global_regs[j]
5052 || j == STACK_POINTER_REGNUM
5053 || j == RETURN_REGNUM
5054 || j == BASE_REGISTER
5055 || (flag_pic && j == (int)PIC_OFFSET_TABLE_REGNUM))
5056 break;
5057
5058 if (i == 16)
5059 {
5060 /* Nothing to save/restore. */
5061 save_first = restore_first = -1;
5062 save_last = restore_last = -1;
5063 }
5064 else
5065 {
5066 /* Save/restore from i to j. */
5067 save_first = restore_first = i;
5068 save_last = restore_last = j;
5069 }
5070
5071 /* Varargs functions need to save gprs 2 to 6. */
5072 if (current_function_stdarg)
5073 {
5074 save_first = 2;
5075 if (save_last < 6)
5076 save_last = 6;
5077 }
5078
5079
5080 /* If all special registers are in fact used, there's nothing we
5081 can do, so no point in walking the insn list. */
5082 if (i <= BASE_REGISTER && j >= BASE_REGISTER
5083 && (TARGET_CPU_ZARCH || (i <= RETURN_REGNUM && j >= RETURN_REGNUM)))
5084 return;
5085
5086
5087 /* Search for prolog/epilog insns and replace them. */
5088
5089 for (insn = get_insns (); insn; insn = next_insn)
5090 {
5091 int first, last, off;
5092 rtx set, base, offset;
5093
5094 next_insn = NEXT_INSN (insn);
5095
5096 if (GET_CODE (insn) != INSN)
5097 continue;
5098
5099 if (GET_CODE (PATTERN (insn)) == PARALLEL
5100 && store_multiple_operation (PATTERN (insn), VOIDmode))
5101 {
5102 set = XVECEXP (PATTERN (insn), 0, 0);
5103 first = REGNO (SET_SRC (set));
5104 last = first + XVECLEN (PATTERN (insn), 0) - 1;
5105 offset = const0_rtx;
5106 base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
5107 off = INTVAL (offset) - first * UNITS_PER_WORD;
5108
5109 if (GET_CODE (base) != REG || off < 0)
5110 continue;
5111 if (first > BASE_REGISTER || last < BASE_REGISTER)
5112 continue;
5113
5114 if (save_first != -1)
5115 {
5116 new_insn = save_gprs (base, off, save_first, save_last);
5117 new_insn = emit_insn_before (new_insn, insn);
5118 INSN_ADDRESSES_NEW (new_insn, -1);
5119 }
5120
5121 remove_insn (insn);
5122 continue;
5123 }
5124
5125 if (GET_CODE (PATTERN (insn)) == SET
5126 && GET_CODE (SET_SRC (PATTERN (insn))) == REG
5127 && REGNO (SET_SRC (PATTERN (insn))) == BASE_REGISTER
5128 && GET_CODE (SET_DEST (PATTERN (insn))) == MEM)
5129 {
5130 set = PATTERN (insn);
5131 offset = const0_rtx;
5132 base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
5133 off = INTVAL (offset) - BASE_REGISTER * UNITS_PER_WORD;
5134
5135 if (GET_CODE (base) != REG || off < 0)
5136 continue;
5137
5138 if (save_first != -1)
5139 {
5140 new_insn = save_gprs (base, off, save_first, save_last);
5141 new_insn = emit_insn_before (new_insn, insn);
5142 INSN_ADDRESSES_NEW (new_insn, -1);
5143 }
5144
5145 remove_insn (insn);
5146 continue;
5147 }
5148
5149 if (GET_CODE (PATTERN (insn)) == PARALLEL
5150 && load_multiple_operation (PATTERN (insn), VOIDmode))
5151 {
5152 set = XVECEXP (PATTERN (insn), 0, 0);
5153 first = REGNO (SET_DEST (set));
5154 last = first + XVECLEN (PATTERN (insn), 0) - 1;
5155 offset = const0_rtx;
5156 base = eliminate_constant_term (XEXP (SET_SRC (set), 0), &offset);
5157 off = INTVAL (offset) - first * UNITS_PER_WORD;
5158
5159 if (GET_CODE (base) != REG || off < 0)
5160 continue;
5161 if (first > BASE_REGISTER || last < BASE_REGISTER)
5162 continue;
5163
5164 if (restore_first != -1)
5165 {
5166 new_insn = restore_gprs (base, off, restore_first, restore_last);
5167 new_insn = emit_insn_before (new_insn, insn);
5168 INSN_ADDRESSES_NEW (new_insn, -1);
5169 }
5170
5171 remove_insn (insn);
5172 continue;
5173 }
5174
5175 if (GET_CODE (PATTERN (insn)) == SET
5176 && GET_CODE (SET_DEST (PATTERN (insn))) == REG
5177 && REGNO (SET_DEST (PATTERN (insn))) == BASE_REGISTER
5178 && GET_CODE (SET_SRC (PATTERN (insn))) == MEM)
5179 {
5180 set = PATTERN (insn);
5181 offset = const0_rtx;
5182 base = eliminate_constant_term (XEXP (SET_SRC (set), 0), &offset);
5183 off = INTVAL (offset) - BASE_REGISTER * UNITS_PER_WORD;
5184
5185 if (GET_CODE (base) != REG || off < 0)
5186 continue;
5187
5188 if (restore_first != -1)
5189 {
5190 new_insn = restore_gprs (base, off, restore_first, restore_last);
5191 new_insn = emit_insn_before (new_insn, insn);
5192 INSN_ADDRESSES_NEW (new_insn, -1);
5193 }
5194
5195 remove_insn (insn);
5196 continue;
5197 }
5198 }
5199 }
5200
5201 /* Perform machine-dependent processing. */
5202
5203 static void
5204 s390_reorg (void)
5205 {
5206 rtx base_reg = gen_rtx_REG (Pmode, BASE_REGISTER);
5207 bool base_used = false;
5208 bool pool_overflow = false;
5209
5210 /* Make sure all splits have been performed; splits after
5211 machine_dependent_reorg might confuse insn length counts. */
5212 split_all_insns_noflow ();
5213
5214
5215 /* In small leaf functions, try to use an unused call-clobbered
5216 register as base register to avoid save/restore overhead. */
5217 if (current_function_is_leaf && !regs_ever_live[5])
5218 base_reg = gen_rtx_REG (Pmode, 5);
5219
5220
5221 /* Install the main literal pool and the associated base
5222 register load insns.
5223
5224 In addition, there are two problematic situations we need
5225 to correct:
5226
5227 - the literal pool might be > 4096 bytes in size, so that
5228 some of its elements cannot be directly accessed
5229
5230 - a branch target might be > 64K away from the branch, so that
5231 it is not possible to use a PC-relative instruction.
5232
5233 To fix those, we split the single literal pool into multiple
5234 pool chunks, reloading the pool base register at various
5235 points throughout the function to ensure it always points to
5236 the pool chunk the following code expects, and / or replace
5237 PC-relative branches by absolute branches.
5238
5239 However, the two problems are interdependent: splitting the
5240 literal pool can move a branch further away from its target,
5241 causing the 64K limit to overflow, and on the other hand,
5242 replacing a PC-relative branch by an absolute branch means
5243 we need to put the branch target address into the literal
5244 pool, possibly causing it to overflow.
5245
5246 So, we loop trying to fix up both problems until we manage
5247 to satisfy both conditions at the same time. Note that the
5248 loop is guaranteed to terminate as every pass of the loop
5249 strictly decreases the total number of PC-relative branches
5250 in the function. (This is not completely true as there
5251 might be branch-over-pool insns introduced by chunkify_start.
5252 Those never need to be split however.) */
5253
5254 for (;;)
5255 {
5256 struct constant_pool *pool = NULL;
5257
5258 /* Collect the literal pool. */
5259 if (!pool_overflow)
5260 {
5261 pool = s390_mainpool_start ();
5262 if (!pool)
5263 pool_overflow = true;
5264 }
5265
5266 /* If literal pool overflowed, start to chunkify it. */
5267 if (pool_overflow)
5268 pool = s390_chunkify_start (base_reg);
5269
5270 /* Split out-of-range branches. If this has created new
5271 literal pool entries, cancel current chunk list and
5272 recompute it. zSeries machines have large branch
5273 instructions, so we never need to split a branch. */
5274 if (!TARGET_CPU_ZARCH && s390_split_branches ())
5275 {
5276 if (pool_overflow)
5277 s390_chunkify_cancel (pool);
5278 else
5279 s390_mainpool_cancel (pool);
5280
5281 continue;
5282 }
5283
5284 /* If we made it up to here, both conditions are satisfied.
5285 Finish up literal pool related changes. */
5286 if ((pool_overflow || pool->size > 0)
5287 && REGNO (base_reg) == BASE_REGISTER)
5288 base_used = true;
5289
5290 if (pool_overflow)
5291 s390_chunkify_finish (pool, base_reg);
5292 else
5293 s390_mainpool_finish (pool, base_reg);
5294
5295 break;
5296 }
5297
5298 s390_optimize_prolog (base_used);
5299 }
5300
5301
5302 /* Return an RTL expression representing the value of the return address
5303 for the frame COUNT steps up from the current frame. FRAME is the
5304 frame pointer of that frame. */
5305
5306 rtx
5307 s390_return_addr_rtx (int count, rtx frame)
5308 {
5309 rtx addr;
5310
5311 /* For the current frame, we need to make sure the initial
5312 value of RETURN_REGNUM is actually saved. */
5313
5314 if (count == 0)
5315 cfun->machine->save_return_addr_p = true;
5316
5317 /* To retrieve the return address we read the stack slot where the
5318 corresponding RETURN_REGNUM value was saved. */
5319
5320 addr = plus_constant (frame, RETURN_REGNUM * UNITS_PER_WORD);
5321 addr = memory_address (Pmode, addr);
5322 return gen_rtx_MEM (Pmode, addr);
5323 }
5324
5325 /* Find first call clobbered register unsused in a function.
5326 This could be used as base register in a leaf function
5327 or for holding the return address before epilogue. */
5328
5329 static int
5330 find_unused_clobbered_reg (void)
5331 {
5332 int i;
5333 for (i = 0; i < 6; i++)
5334 if (!regs_ever_live[i])
5335 return i;
5336 return 0;
5337 }
5338
5339 /* Fill FRAME with info about frame of current function. */
5340
5341 static void
5342 s390_frame_info (void)
5343 {
5344 int i, j;
5345 HOST_WIDE_INT fsize = get_frame_size ();
5346
5347 if (fsize > 0x7fff0000)
5348 fatal_error ("Total size of local variables exceeds architecture limit.");
5349
5350 /* fprs 8 - 15 are caller saved for 64 Bit ABI. */
5351 cfun->machine->save_fprs_p = 0;
5352 if (TARGET_64BIT)
5353 for (i = 24; i < 32; i++)
5354 if (regs_ever_live[i] && !global_regs[i])
5355 {
5356 cfun->machine->save_fprs_p = 1;
5357 break;
5358 }
5359
5360 cfun->machine->frame_size = fsize + cfun->machine->save_fprs_p * 64;
5361
5362 /* Does function need to setup frame and save area. */
5363
5364 if (! current_function_is_leaf
5365 || cfun->machine->frame_size > 0
5366 || current_function_calls_alloca
5367 || current_function_stdarg)
5368 cfun->machine->frame_size += STARTING_FRAME_OFFSET;
5369
5370 /* If we use the return register, we'll need to make sure
5371 it is going to be saved/restored. */
5372
5373 if (!current_function_is_leaf
5374 || regs_ever_live[RETURN_REGNUM])
5375 cfun->machine->save_return_addr_p = 1;
5376
5377 /* Find first and last gpr to be saved. Note that at this point,
5378 we assume the base register and -on S/390- the return register
5379 always need to be saved. This is done because the usage of these
5380 register might change even after the prolog was emitted.
5381 If it turns out later that we really don't need them, the
5382 prolog/epilog code is modified again. */
5383
5384 regs_ever_live[BASE_REGISTER] = 1;
5385 if (!TARGET_CPU_ZARCH || cfun->machine->save_return_addr_p)
5386 regs_ever_live[RETURN_REGNUM] = 1;
5387 regs_ever_live[STACK_POINTER_REGNUM] = cfun->machine->frame_size > 0;
5388
5389 for (i = 6; i < 16; i++)
5390 if (regs_ever_live[i])
5391 if (!global_regs[i]
5392 || i == STACK_POINTER_REGNUM
5393 || i == RETURN_REGNUM
5394 || i == BASE_REGISTER
5395 || (flag_pic && i == (int)PIC_OFFSET_TABLE_REGNUM))
5396 break;
5397
5398 for (j = 15; j > i; j--)
5399 if (regs_ever_live[j])
5400 if (!global_regs[j]
5401 || j == STACK_POINTER_REGNUM
5402 || j == RETURN_REGNUM
5403 || j == BASE_REGISTER
5404 || (flag_pic && j == (int)PIC_OFFSET_TABLE_REGNUM))
5405 break;
5406
5407 /* Save / Restore from gpr i to j. */
5408 cfun->machine->first_save_gpr = i;
5409 cfun->machine->first_restore_gpr = i;
5410 cfun->machine->last_save_gpr = j;
5411
5412 /* Varargs functions need to save gprs 2 to 6. */
5413 if (current_function_stdarg)
5414 cfun->machine->first_save_gpr = 2;
5415 }
5416
5417 /* Return offset between argument pointer and frame pointer
5418 initially after prologue. */
5419
5420 int
5421 s390_arg_frame_offset (void)
5422 {
5423 HOST_WIDE_INT fsize = get_frame_size ();
5424 int save_fprs_p, i;
5425
5426 /* fprs 8 - 15 are caller saved for 64 Bit ABI. */
5427 save_fprs_p = 0;
5428 if (TARGET_64BIT)
5429 for (i = 24; i < 32; i++)
5430 if (regs_ever_live[i] && !global_regs[i])
5431 {
5432 save_fprs_p = 1;
5433 break;
5434 }
5435
5436 fsize = fsize + save_fprs_p * 64;
5437
5438 /* Does function need to setup frame and save area. */
5439
5440 if (! current_function_is_leaf
5441 || fsize > 0
5442 || current_function_calls_alloca
5443 || current_function_stdarg)
5444 fsize += STARTING_FRAME_OFFSET;
5445 return fsize + STACK_POINTER_OFFSET;
5446 }
5447
5448 /* Emit insn to save fpr REGNUM at offset OFFSET relative
5449 to register BASE. Return generated insn. */
5450
5451 static rtx
5452 save_fpr (rtx base, int offset, int regnum)
5453 {
5454 rtx addr;
5455 addr = gen_rtx_MEM (DFmode, plus_constant (base, offset));
5456 set_mem_alias_set (addr, s390_sr_alias_set);
5457
5458 return emit_move_insn (addr, gen_rtx_REG (DFmode, regnum));
5459 }
5460
5461 /* Emit insn to restore fpr REGNUM from offset OFFSET relative
5462 to register BASE. Return generated insn. */
5463
5464 static rtx
5465 restore_fpr (rtx base, int offset, int regnum)
5466 {
5467 rtx addr;
5468 addr = gen_rtx_MEM (DFmode, plus_constant (base, offset));
5469 set_mem_alias_set (addr, s390_sr_alias_set);
5470
5471 return emit_move_insn (gen_rtx_REG (DFmode, regnum), addr);
5472 }
5473
5474 /* Generate insn to save registers FIRST to LAST into
5475 the register save area located at offset OFFSET
5476 relative to register BASE. */
5477
5478 static rtx
5479 save_gprs (rtx base, int offset, int first, int last)
5480 {
5481 rtx addr, insn, note;
5482 int i;
5483
5484 addr = plus_constant (base, offset + first * UNITS_PER_WORD);
5485 addr = gen_rtx_MEM (Pmode, addr);
5486 set_mem_alias_set (addr, s390_sr_alias_set);
5487
5488 /* Special-case single register. */
5489 if (first == last)
5490 {
5491 if (TARGET_64BIT)
5492 insn = gen_movdi (addr, gen_rtx_REG (Pmode, first));
5493 else
5494 insn = gen_movsi (addr, gen_rtx_REG (Pmode, first));
5495
5496 RTX_FRAME_RELATED_P (insn) = 1;
5497 return insn;
5498 }
5499
5500
5501 insn = gen_store_multiple (addr,
5502 gen_rtx_REG (Pmode, first),
5503 GEN_INT (last - first + 1));
5504
5505
5506 /* We need to set the FRAME_RELATED flag on all SETs
5507 inside the store-multiple pattern.
5508
5509 However, we must not emit DWARF records for registers 2..5
5510 if they are stored for use by variable arguments ...
5511
5512 ??? Unfortunately, it is not enough to simply not the the
5513 FRAME_RELATED flags for those SETs, because the first SET
5514 of the PARALLEL is always treated as if it had the flag
5515 set, even if it does not. Therefore we emit a new pattern
5516 without those registers as REG_FRAME_RELATED_EXPR note. */
5517
5518 if (first >= 6)
5519 {
5520 rtx pat = PATTERN (insn);
5521
5522 for (i = 0; i < XVECLEN (pat, 0); i++)
5523 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
5524 RTX_FRAME_RELATED_P (XVECEXP (pat, 0, i)) = 1;
5525
5526 RTX_FRAME_RELATED_P (insn) = 1;
5527 }
5528 else if (last >= 6)
5529 {
5530 addr = plus_constant (base, offset + 6 * UNITS_PER_WORD);
5531 note = gen_store_multiple (gen_rtx_MEM (Pmode, addr),
5532 gen_rtx_REG (Pmode, 6),
5533 GEN_INT (last - 6 + 1));
5534 note = PATTERN (note);
5535
5536 REG_NOTES (insn) =
5537 gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
5538 note, REG_NOTES (insn));
5539
5540 for (i = 0; i < XVECLEN (note, 0); i++)
5541 if (GET_CODE (XVECEXP (note, 0, i)) == SET)
5542 RTX_FRAME_RELATED_P (XVECEXP (note, 0, i)) = 1;
5543
5544 RTX_FRAME_RELATED_P (insn) = 1;
5545 }
5546
5547 return insn;
5548 }
5549
5550 /* Generate insn to restore registers FIRST to LAST from
5551 the register save area located at offset OFFSET
5552 relative to register BASE. */
5553
5554 static rtx
5555 restore_gprs (rtx base, int offset, int first, int last)
5556 {
5557 rtx addr, insn;
5558
5559 addr = plus_constant (base, offset + first * UNITS_PER_WORD);
5560 addr = gen_rtx_MEM (Pmode, addr);
5561 set_mem_alias_set (addr, s390_sr_alias_set);
5562
5563 /* Special-case single register. */
5564 if (first == last)
5565 {
5566 if (TARGET_64BIT)
5567 insn = gen_movdi (gen_rtx_REG (Pmode, first), addr);
5568 else
5569 insn = gen_movsi (gen_rtx_REG (Pmode, first), addr);
5570
5571 return insn;
5572 }
5573
5574 insn = gen_load_multiple (gen_rtx_REG (Pmode, first),
5575 addr,
5576 GEN_INT (last - first + 1));
5577 return insn;
5578 }
5579
5580 /* Emit code to load the GOT register. If MAYBE_DEAD is true,
5581 annotate generated insns with REG_MAYBE_DEAD notes. */
5582
5583 static GTY(()) rtx got_symbol;
5584 void
5585 s390_load_got (int maybe_dead)
5586 {
5587 if (!got_symbol)
5588 {
5589 got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
5590 SYMBOL_REF_FLAGS (got_symbol) = SYMBOL_FLAG_LOCAL;
5591 }
5592
5593 if (TARGET_CPU_ZARCH)
5594 {
5595 rtx insn = emit_move_insn (pic_offset_table_rtx, got_symbol);
5596 if (maybe_dead)
5597 REG_NOTES(insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, NULL_RTX,
5598 REG_NOTES (insn));
5599 }
5600 else
5601 {
5602 rtx offset, insn;
5603
5604 offset = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, got_symbol),
5605 UNSPEC_LTREL_OFFSET);
5606 offset = gen_rtx_CONST (Pmode, offset);
5607 offset = force_const_mem (Pmode, offset);
5608
5609 insn = emit_move_insn (pic_offset_table_rtx, offset);
5610 if (maybe_dead)
5611 REG_NOTES(insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, NULL_RTX,
5612 REG_NOTES (insn));
5613
5614 offset = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (offset, 0)),
5615 UNSPEC_LTREL_BASE);
5616 offset = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, offset);
5617
5618 insn = emit_move_insn (pic_offset_table_rtx, offset);
5619 if (maybe_dead)
5620 REG_NOTES(insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, NULL_RTX,
5621 REG_NOTES (insn));
5622 }
5623 }
5624
5625 /* Expand the prologue into a bunch of separate insns. */
5626
5627 void
5628 s390_emit_prologue (void)
5629 {
5630 rtx insn, addr;
5631 rtx temp_reg;
5632 int i;
5633
5634 /* Compute frame_info. */
5635
5636 s390_frame_info ();
5637
5638 /* Choose best register to use for temp use within prologue.
5639 See below for why TPF must use the register 1. */
5640
5641 if (!current_function_is_leaf
5642 && !TARGET_TPF)
5643 temp_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
5644 else
5645 temp_reg = gen_rtx_REG (Pmode, 1);
5646
5647 /* Save call saved gprs. */
5648
5649 insn = save_gprs (stack_pointer_rtx, 0,
5650 cfun->machine->first_save_gpr, cfun->machine->last_save_gpr);
5651 emit_insn (insn);
5652
5653 /* Dummy insn to mark literal pool slot. */
5654
5655 emit_insn (gen_main_pool ());
5656
5657 /* Save fprs for variable args. */
5658
5659 if (current_function_stdarg)
5660 for (i = 16; i < (TARGET_64BIT ? 20 : 18); i++)
5661 save_fpr (stack_pointer_rtx, 16*UNITS_PER_WORD + 8*(i-16), i);
5662
5663 /* Save fprs 4 and 6 if used (31 bit ABI). */
5664
5665 if (!TARGET_64BIT)
5666 for (i = 18; i < 20; i++)
5667 if (regs_ever_live[i] && !global_regs[i])
5668 {
5669 insn = save_fpr (stack_pointer_rtx, 16*UNITS_PER_WORD + 8*(i-16), i);
5670 RTX_FRAME_RELATED_P (insn) = 1;
5671 }
5672
5673 /* Decrement stack pointer. */
5674
5675 if (cfun->machine->frame_size > 0)
5676 {
5677 rtx frame_off = GEN_INT (-cfun->machine->frame_size);
5678
5679 /* Save incoming stack pointer into temp reg. */
5680
5681 if (TARGET_BACKCHAIN || cfun->machine->save_fprs_p)
5682 {
5683 insn = emit_insn (gen_move_insn (temp_reg, stack_pointer_rtx));
5684 }
5685
5686 /* Subtract frame size from stack pointer. */
5687
5688 if (DISP_IN_RANGE (INTVAL (frame_off)))
5689 {
5690 insn = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
5691 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
5692 frame_off));
5693 insn = emit_insn (insn);
5694 }
5695 else
5696 {
5697 if (!CONST_OK_FOR_LETTER_P (INTVAL (frame_off), 'K'))
5698 frame_off = force_const_mem (Pmode, frame_off);
5699
5700 insn = emit_insn (gen_add2_insn (stack_pointer_rtx, frame_off));
5701 }
5702
5703 RTX_FRAME_RELATED_P (insn) = 1;
5704 REG_NOTES (insn) =
5705 gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
5706 gen_rtx_SET (VOIDmode, stack_pointer_rtx,
5707 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
5708 GEN_INT (-cfun->machine->frame_size))),
5709 REG_NOTES (insn));
5710
5711 /* Set backchain. */
5712
5713 if (TARGET_BACKCHAIN)
5714 {
5715 addr = gen_rtx_MEM (Pmode, stack_pointer_rtx);
5716 set_mem_alias_set (addr, s390_sr_alias_set);
5717 insn = emit_insn (gen_move_insn (addr, temp_reg));
5718 }
5719
5720 /* If we support asynchronous exceptions (e.g. for Java),
5721 we need to make sure the backchain pointer is set up
5722 before any possibly trapping memory access. */
5723
5724 if (TARGET_BACKCHAIN && flag_non_call_exceptions)
5725 {
5726 addr = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
5727 emit_insn (gen_rtx_CLOBBER (VOIDmode, addr));
5728 }
5729 }
5730
5731 /* Save fprs 8 - 15 (64 bit ABI). */
5732
5733 if (cfun->machine->save_fprs_p)
5734 {
5735 insn = emit_insn (gen_add2_insn (temp_reg, GEN_INT(-64)));
5736
5737 for (i = 24; i < 32; i++)
5738 if (regs_ever_live[i] && !global_regs[i])
5739 {
5740 rtx addr = plus_constant (stack_pointer_rtx,
5741 cfun->machine->frame_size - 64 + (i-24)*8);
5742
5743 insn = save_fpr (temp_reg, (i-24)*8, i);
5744 RTX_FRAME_RELATED_P (insn) = 1;
5745 REG_NOTES (insn) =
5746 gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
5747 gen_rtx_SET (VOIDmode,
5748 gen_rtx_MEM (DFmode, addr),
5749 gen_rtx_REG (DFmode, i)),
5750 REG_NOTES (insn));
5751 }
5752 }
5753
5754 /* Set frame pointer, if needed. */
5755
5756 if (frame_pointer_needed)
5757 {
5758 insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
5759 RTX_FRAME_RELATED_P (insn) = 1;
5760 }
5761
5762 /* Set up got pointer, if needed. */
5763
5764 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
5765 s390_load_got(true);
5766
5767 if (TARGET_TPF)
5768 {
5769 /* Generate a BAS instruction to serve as a function
5770 entry intercept to facilitate the use of tracing
5771 algorithms located at the branch target.
5772
5773 This must use register 1. */
5774 rtx addr;
5775 rtx unkn;
5776 rtx link;
5777
5778 addr = GEN_INT (0xfe0);
5779 unkn = CONST0_RTX (SImode);
5780 link = gen_rtx_REG (Pmode, 1);
5781
5782 emit_call_insn (gen_call_exp (gen_rtx_MEM (QImode, addr), unkn, link));
5783
5784 /* Emit a blockage here so that all code
5785 lies between the profiling mechanisms. */
5786 emit_insn (gen_blockage ());
5787 }
5788 }
5789
5790 /* Expand the epilogue into a bunch of separate insns. */
5791
5792 void
5793 s390_emit_epilogue (void)
5794 {
5795 rtx frame_pointer, return_reg;
5796 int area_bottom, area_top, offset = 0;
5797 rtvec p;
5798 int i;
5799
5800 if (TARGET_TPF)
5801 {
5802
5803 /* Generate a BAS instruction to serve as a function
5804 entry intercept to facilitate the use of tracing
5805 algorithms located at the branch target.
5806
5807 This must use register 1. */
5808
5809 rtx addr;
5810 rtx unkn;
5811 rtx link;
5812
5813 addr = GEN_INT (0xfe6);
5814 unkn = CONST0_RTX (SImode);
5815 link = gen_rtx_REG (Pmode, 1);
5816
5817 /* Emit a blockage here so that all code
5818 lies between the profiling mechanisms. */
5819 emit_insn (gen_blockage ());
5820
5821 emit_call_insn (gen_call_exp (gen_rtx_MEM (QImode, addr), unkn, link));
5822 }
5823
5824 /* Check whether to use frame or stack pointer for restore. */
5825
5826 frame_pointer = frame_pointer_needed ?
5827 hard_frame_pointer_rtx : stack_pointer_rtx;
5828
5829 /* Compute which parts of the save area we need to access. */
5830
5831 if (cfun->machine->first_restore_gpr != -1)
5832 {
5833 area_bottom = cfun->machine->first_restore_gpr * UNITS_PER_WORD;
5834 area_top = (cfun->machine->last_save_gpr + 1) * UNITS_PER_WORD;
5835 }
5836 else
5837 {
5838 area_bottom = INT_MAX;
5839 area_top = INT_MIN;
5840 }
5841
5842 if (TARGET_64BIT)
5843 {
5844 if (cfun->machine->save_fprs_p)
5845 {
5846 if (area_bottom > -64)
5847 area_bottom = -64;
5848 if (area_top < 0)
5849 area_top = 0;
5850 }
5851 }
5852 else
5853 {
5854 for (i = 18; i < 20; i++)
5855 if (regs_ever_live[i] && !global_regs[i])
5856 {
5857 if (area_bottom > 16*UNITS_PER_WORD + 8*(i-16))
5858 area_bottom = 16*UNITS_PER_WORD + 8*(i-16);
5859 if (area_top < 16*UNITS_PER_WORD + 8*(i-16) + 8)
5860 area_top = 16*UNITS_PER_WORD + 8*(i-16) + 8;
5861 }
5862 }
5863
5864 /* Check whether we can access the register save area.
5865 If not, increment the frame pointer as required. */
5866
5867 if (area_top <= area_bottom)
5868 {
5869 /* Nothing to restore. */
5870 }
5871 else if (DISP_IN_RANGE (cfun->machine->frame_size + area_bottom)
5872 && DISP_IN_RANGE (cfun->machine->frame_size + area_top-1))
5873 {
5874 /* Area is in range. */
5875 offset = cfun->machine->frame_size;
5876 }
5877 else
5878 {
5879 rtx insn, frame_off;
5880
5881 offset = area_bottom < 0 ? -area_bottom : 0;
5882 frame_off = GEN_INT (cfun->machine->frame_size - offset);
5883
5884 if (DISP_IN_RANGE (INTVAL (frame_off)))
5885 {
5886 insn = gen_rtx_SET (VOIDmode, frame_pointer,
5887 gen_rtx_PLUS (Pmode, frame_pointer, frame_off));
5888 insn = emit_insn (insn);
5889 }
5890 else
5891 {
5892 if (!CONST_OK_FOR_LETTER_P (INTVAL (frame_off), 'K'))
5893 frame_off = force_const_mem (Pmode, frame_off);
5894
5895 insn = emit_insn (gen_add2_insn (frame_pointer, frame_off));
5896 }
5897 }
5898
5899 /* Restore call saved fprs. */
5900
5901 if (TARGET_64BIT)
5902 {
5903 if (cfun->machine->save_fprs_p)
5904 for (i = 24; i < 32; i++)
5905 if (regs_ever_live[i] && !global_regs[i])
5906 restore_fpr (frame_pointer,
5907 offset - 64 + (i-24) * 8, i);
5908 }
5909 else
5910 {
5911 for (i = 18; i < 20; i++)
5912 if (regs_ever_live[i] && !global_regs[i])
5913 restore_fpr (frame_pointer,
5914 offset + 16*UNITS_PER_WORD + 8*(i-16), i);
5915 }
5916
5917 /* Return register. */
5918
5919 return_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
5920
5921 /* Restore call saved gprs. */
5922
5923 if (cfun->machine->first_restore_gpr != -1)
5924 {
5925 rtx insn, addr;
5926 int i;
5927
5928 /* Check for global register and save them
5929 to stack location from where they get restored. */
5930
5931 for (i = cfun->machine->first_restore_gpr;
5932 i <= cfun->machine->last_save_gpr;
5933 i++)
5934 {
5935 /* These registers are special and need to be
5936 restored in any case. */
5937 if (i == STACK_POINTER_REGNUM
5938 || i == RETURN_REGNUM
5939 || i == BASE_REGISTER
5940 || (flag_pic && i == (int)PIC_OFFSET_TABLE_REGNUM))
5941 continue;
5942
5943 if (global_regs[i])
5944 {
5945 addr = plus_constant (frame_pointer,
5946 offset + i * UNITS_PER_WORD);
5947 addr = gen_rtx_MEM (Pmode, addr);
5948 set_mem_alias_set (addr, s390_sr_alias_set);
5949 emit_move_insn (addr, gen_rtx_REG (Pmode, i));
5950 }
5951 }
5952
5953 /* Fetch return address from stack before load multiple,
5954 this will do good for scheduling. */
5955
5956 if (cfun->machine->save_return_addr_p
5957 || (cfun->machine->first_restore_gpr < BASE_REGISTER
5958 && cfun->machine->last_save_gpr > RETURN_REGNUM))
5959 {
5960 int return_regnum = find_unused_clobbered_reg();
5961 if (!return_regnum)
5962 return_regnum = 4;
5963 return_reg = gen_rtx_REG (Pmode, return_regnum);
5964
5965 addr = plus_constant (frame_pointer,
5966 offset + RETURN_REGNUM * UNITS_PER_WORD);
5967 addr = gen_rtx_MEM (Pmode, addr);
5968 set_mem_alias_set (addr, s390_sr_alias_set);
5969 emit_move_insn (return_reg, addr);
5970 }
5971
5972 /* ??? As references to the base register are not made
5973 explicit in insn RTX code, we have to add a barrier here
5974 to prevent incorrect scheduling. */
5975
5976 emit_insn (gen_blockage());
5977
5978 insn = restore_gprs (frame_pointer, offset,
5979 cfun->machine->first_restore_gpr,
5980 cfun->machine->last_save_gpr);
5981 emit_insn (insn);
5982 }
5983
5984 /* Return to caller. */
5985
5986 p = rtvec_alloc (2);
5987
5988 RTVEC_ELT (p, 0) = gen_rtx_RETURN (VOIDmode);
5989 RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, return_reg);
5990 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
5991 }
5992
5993
5994 /* Return the size in bytes of a function argument of
5995 type TYPE and/or mode MODE. At least one of TYPE or
5996 MODE must be specified. */
5997
5998 static int
5999 s390_function_arg_size (enum machine_mode mode, tree type)
6000 {
6001 if (type)
6002 return int_size_in_bytes (type);
6003
6004 /* No type info available for some library calls ... */
6005 if (mode != BLKmode)
6006 return GET_MODE_SIZE (mode);
6007
6008 /* If we have neither type nor mode, abort */
6009 abort ();
6010 }
6011
6012 /* Return true if a function argument of type TYPE and mode MODE
6013 is to be passed in a floating-point register, if available. */
6014
6015 static bool
6016 s390_function_arg_float (enum machine_mode mode, tree type)
6017 {
6018 int size = s390_function_arg_size (mode, type);
6019 if (size > 8)
6020 return false;
6021
6022 /* Soft-float changes the ABI: no floating-point registers are used. */
6023 if (TARGET_SOFT_FLOAT)
6024 return false;
6025
6026 /* No type info available for some library calls ... */
6027 if (!type)
6028 return mode == SFmode || mode == DFmode;
6029
6030 /* The ABI says that record types with a single member are treated
6031 just like that member would be. */
6032 while (TREE_CODE (type) == RECORD_TYPE)
6033 {
6034 tree field, single = NULL_TREE;
6035
6036 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6037 {
6038 if (TREE_CODE (field) != FIELD_DECL)
6039 continue;
6040
6041 if (single == NULL_TREE)
6042 single = TREE_TYPE (field);
6043 else
6044 return false;
6045 }
6046
6047 if (single == NULL_TREE)
6048 return false;
6049 else
6050 type = single;
6051 }
6052
6053 return TREE_CODE (type) == REAL_TYPE;
6054 }
6055
6056 /* Return true if a function argument of type TYPE and mode MODE
6057 is to be passed in an integer register, or a pair of integer
6058 registers, if available. */
6059
6060 static bool
6061 s390_function_arg_integer (enum machine_mode mode, tree type)
6062 {
6063 int size = s390_function_arg_size (mode, type);
6064 if (size > 8)
6065 return false;
6066
6067 /* No type info available for some library calls ... */
6068 if (!type)
6069 return GET_MODE_CLASS (mode) == MODE_INT
6070 || (TARGET_SOFT_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT);
6071
6072 /* We accept small integral (and similar) types. */
6073 if (INTEGRAL_TYPE_P (type)
6074 || POINTER_TYPE_P (type)
6075 || TREE_CODE (type) == OFFSET_TYPE
6076 || (TARGET_SOFT_FLOAT && TREE_CODE (type) == REAL_TYPE))
6077 return true;
6078
6079 /* We also accept structs of size 1, 2, 4, 8 that are not
6080 passed in floating-point registers. */
6081 if (AGGREGATE_TYPE_P (type)
6082 && exact_log2 (size) >= 0
6083 && !s390_function_arg_float (mode, type))
6084 return true;
6085
6086 return false;
6087 }
6088
6089 /* Return 1 if a function argument of type TYPE and mode MODE
6090 is to be passed by reference. The ABI specifies that only
6091 structures of size 1, 2, 4, or 8 bytes are passed by value,
6092 all other structures (and complex numbers) are passed by
6093 reference. */
6094
6095 int
6096 s390_function_arg_pass_by_reference (enum machine_mode mode, tree type)
6097 {
6098 int size = s390_function_arg_size (mode, type);
6099 if (size > 8)
6100 return true;
6101
6102 if (type)
6103 {
6104 if (AGGREGATE_TYPE_P (type) && exact_log2 (size) < 0)
6105 return 1;
6106
6107 if (TREE_CODE (type) == COMPLEX_TYPE
6108 || TREE_CODE (type) == VECTOR_TYPE)
6109 return 1;
6110 }
6111
6112 return 0;
6113 }
6114
6115 /* Update the data in CUM to advance over an argument of mode MODE and
6116 data type TYPE. (TYPE is null for libcalls where that information
6117 may not be available.). The boolean NAMED specifies whether the
6118 argument is a named argument (as opposed to an unnamed argument
6119 matching an ellipsis). */
6120
6121 void
6122 s390_function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6123 tree type, int named ATTRIBUTE_UNUSED)
6124 {
6125 if (s390_function_arg_pass_by_reference (mode, type))
6126 {
6127 cum->gprs += 1;
6128 }
6129 else if (s390_function_arg_float (mode, type))
6130 {
6131 cum->fprs += 1;
6132 }
6133 else if (s390_function_arg_integer (mode, type))
6134 {
6135 int size = s390_function_arg_size (mode, type);
6136 cum->gprs += ((size + UNITS_PER_WORD-1) / UNITS_PER_WORD);
6137 }
6138 else
6139 abort ();
6140 }
6141
6142 /* Define where to put the arguments to a function.
6143 Value is zero to push the argument on the stack,
6144 or a hard register in which to store the argument.
6145
6146 MODE is the argument's machine mode.
6147 TYPE is the data type of the argument (as a tree).
6148 This is null for libcalls where that information may
6149 not be available.
6150 CUM is a variable of type CUMULATIVE_ARGS which gives info about
6151 the preceding args and about the function being called.
6152 NAMED is nonzero if this argument is a named parameter
6153 (otherwise it is an extra parameter matching an ellipsis).
6154
6155 On S/390, we use general purpose registers 2 through 6 to
6156 pass integer, pointer, and certain structure arguments, and
6157 floating point registers 0 and 2 (0, 2, 4, and 6 on 64-bit)
6158 to pass floating point arguments. All remaining arguments
6159 are pushed to the stack. */
6160
6161 rtx
6162 s390_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, tree type,
6163 int named ATTRIBUTE_UNUSED)
6164 {
6165 if (s390_function_arg_pass_by_reference (mode, type))
6166 return 0;
6167
6168 if (s390_function_arg_float (mode, type))
6169 {
6170 if (cum->fprs + 1 > (TARGET_64BIT? 4 : 2))
6171 return 0;
6172 else
6173 return gen_rtx (REG, mode, cum->fprs + 16);
6174 }
6175 else if (s390_function_arg_integer (mode, type))
6176 {
6177 int size = s390_function_arg_size (mode, type);
6178 int n_gprs = (size + UNITS_PER_WORD-1) / UNITS_PER_WORD;
6179
6180 if (cum->gprs + n_gprs > 5)
6181 return 0;
6182 else
6183 return gen_rtx (REG, mode, cum->gprs + 2);
6184 }
6185
6186 /* After the real arguments, expand_call calls us once again
6187 with a void_type_node type. Whatever we return here is
6188 passed as operand 2 to the call expanders.
6189
6190 We don't need this feature ... */
6191 else if (type == void_type_node)
6192 return const0_rtx;
6193
6194 abort ();
6195 }
6196
6197 /* Return true if return values of type TYPE should be returned
6198 in a memory buffer whose address is passed by the caller as
6199 hidden first argument. */
6200
6201 static bool
6202 s390_return_in_memory (tree type, tree fundecl ATTRIBUTE_UNUSED)
6203 {
6204 /* We accept small integral (and similar) types. */
6205 if (INTEGRAL_TYPE_P (type)
6206 || POINTER_TYPE_P (type)
6207 || TREE_CODE (type) == OFFSET_TYPE
6208 || TREE_CODE (type) == REAL_TYPE)
6209 return int_size_in_bytes (type) > 8;
6210
6211 /* Aggregates and similar constructs are always returned
6212 in memory. */
6213 if (AGGREGATE_TYPE_P (type)
6214 || TREE_CODE (type) == COMPLEX_TYPE
6215 || TREE_CODE (type) == VECTOR_TYPE)
6216 return true;
6217
6218 /* ??? We get called on all sorts of random stuff from
6219 aggregate_value_p. We can't abort, but it's not clear
6220 what's safe to return. Pretend it's a struct I guess. */
6221 return true;
6222 }
6223
6224 /* Define where to return a (scalar) value of type TYPE.
6225 If TYPE is null, define where to return a (scalar)
6226 value of mode MODE from a libcall. */
6227
6228 rtx
6229 s390_function_value (tree type, enum machine_mode mode)
6230 {
6231 if (type)
6232 {
6233 int unsignedp = TREE_UNSIGNED (type);
6234 mode = promote_mode (type, TYPE_MODE (type), &unsignedp, 1);
6235 }
6236
6237 if (GET_MODE_CLASS (mode) != MODE_INT
6238 && GET_MODE_CLASS (mode) != MODE_FLOAT)
6239 abort ();
6240 if (GET_MODE_SIZE (mode) > 8)
6241 abort ();
6242
6243 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
6244 return gen_rtx_REG (mode, 16);
6245 else
6246 return gen_rtx_REG (mode, 2);
6247 }
6248
6249
6250 /* Create and return the va_list datatype.
6251
6252 On S/390, va_list is an array type equivalent to
6253
6254 typedef struct __va_list_tag
6255 {
6256 long __gpr;
6257 long __fpr;
6258 void *__overflow_arg_area;
6259 void *__reg_save_area;
6260 } va_list[1];
6261
6262 where __gpr and __fpr hold the number of general purpose
6263 or floating point arguments used up to now, respectively,
6264 __overflow_arg_area points to the stack location of the
6265 next argument passed on the stack, and __reg_save_area
6266 always points to the start of the register area in the
6267 call frame of the current function. The function prologue
6268 saves all registers used for argument passing into this
6269 area if the function uses variable arguments. */
6270
6271 static tree
6272 s390_build_builtin_va_list (void)
6273 {
6274 tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
6275
6276 record = lang_hooks.types.make_type (RECORD_TYPE);
6277
6278 type_decl =
6279 build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
6280
6281 f_gpr = build_decl (FIELD_DECL, get_identifier ("__gpr"),
6282 long_integer_type_node);
6283 f_fpr = build_decl (FIELD_DECL, get_identifier ("__fpr"),
6284 long_integer_type_node);
6285 f_ovf = build_decl (FIELD_DECL, get_identifier ("__overflow_arg_area"),
6286 ptr_type_node);
6287 f_sav = build_decl (FIELD_DECL, get_identifier ("__reg_save_area"),
6288 ptr_type_node);
6289
6290 DECL_FIELD_CONTEXT (f_gpr) = record;
6291 DECL_FIELD_CONTEXT (f_fpr) = record;
6292 DECL_FIELD_CONTEXT (f_ovf) = record;
6293 DECL_FIELD_CONTEXT (f_sav) = record;
6294
6295 TREE_CHAIN (record) = type_decl;
6296 TYPE_NAME (record) = type_decl;
6297 TYPE_FIELDS (record) = f_gpr;
6298 TREE_CHAIN (f_gpr) = f_fpr;
6299 TREE_CHAIN (f_fpr) = f_ovf;
6300 TREE_CHAIN (f_ovf) = f_sav;
6301
6302 layout_type (record);
6303
6304 /* The correct type is an array type of one element. */
6305 return build_array_type (record, build_index_type (size_zero_node));
6306 }
6307
6308 /* Implement va_start by filling the va_list structure VALIST.
6309 STDARG_P is always true, and ignored.
6310 NEXTARG points to the first anonymous stack argument.
6311
6312 The following global variables are used to initialize
6313 the va_list structure:
6314
6315 current_function_args_info:
6316 holds number of gprs and fprs used for named arguments.
6317 current_function_arg_offset_rtx:
6318 holds the offset of the first anonymous stack argument
6319 (relative to the virtual arg pointer). */
6320
6321 void
6322 s390_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
6323 {
6324 HOST_WIDE_INT n_gpr, n_fpr;
6325 int off;
6326 tree f_gpr, f_fpr, f_ovf, f_sav;
6327 tree gpr, fpr, ovf, sav, t;
6328
6329 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
6330 f_fpr = TREE_CHAIN (f_gpr);
6331 f_ovf = TREE_CHAIN (f_fpr);
6332 f_sav = TREE_CHAIN (f_ovf);
6333
6334 valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
6335 gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
6336 fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
6337 ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
6338 sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
6339
6340 /* Count number of gp and fp argument registers used. */
6341
6342 n_gpr = current_function_args_info.gprs;
6343 n_fpr = current_function_args_info.fprs;
6344
6345 t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, build_int_2 (n_gpr, 0));
6346 TREE_SIDE_EFFECTS (t) = 1;
6347 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6348
6349 t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr, build_int_2 (n_fpr, 0));
6350 TREE_SIDE_EFFECTS (t) = 1;
6351 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6352
6353 /* Find the overflow area. */
6354 t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
6355
6356 off = INTVAL (current_function_arg_offset_rtx);
6357 off = off < 0 ? 0 : off;
6358 if (TARGET_DEBUG_ARG)
6359 fprintf (stderr, "va_start: n_gpr = %d, n_fpr = %d off %d\n",
6360 (int)n_gpr, (int)n_fpr, off);
6361
6362 t = build (PLUS_EXPR, TREE_TYPE (ovf), t, build_int_2 (off, 0));
6363
6364 t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
6365 TREE_SIDE_EFFECTS (t) = 1;
6366 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6367
6368 /* Find the register save area. */
6369 t = make_tree (TREE_TYPE (sav), virtual_incoming_args_rtx);
6370 t = build (PLUS_EXPR, TREE_TYPE (sav), t,
6371 build_int_2 (-STACK_POINTER_OFFSET, -1));
6372 t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
6373 TREE_SIDE_EFFECTS (t) = 1;
6374 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6375 }
6376
6377 /* Implement va_arg by updating the va_list structure
6378 VALIST as required to retrieve an argument of type
6379 TYPE, and returning that argument.
6380
6381 Generates code equivalent to:
6382
6383 if (integral value) {
6384 if (size <= 4 && args.gpr < 5 ||
6385 size > 4 && args.gpr < 4 )
6386 ret = args.reg_save_area[args.gpr+8]
6387 else
6388 ret = *args.overflow_arg_area++;
6389 } else if (float value) {
6390 if (args.fgpr < 2)
6391 ret = args.reg_save_area[args.fpr+64]
6392 else
6393 ret = *args.overflow_arg_area++;
6394 } else if (aggregate value) {
6395 if (args.gpr < 5)
6396 ret = *args.reg_save_area[args.gpr]
6397 else
6398 ret = **args.overflow_arg_area++;
6399 } */
6400
6401 rtx
6402 s390_va_arg (tree valist, tree type)
6403 {
6404 tree f_gpr, f_fpr, f_ovf, f_sav;
6405 tree gpr, fpr, ovf, sav, reg, t, u;
6406 int indirect_p, size, n_reg, sav_ofs, sav_scale, max_reg;
6407 rtx lab_false, lab_over, addr_rtx, r;
6408
6409 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
6410 f_fpr = TREE_CHAIN (f_gpr);
6411 f_ovf = TREE_CHAIN (f_fpr);
6412 f_sav = TREE_CHAIN (f_ovf);
6413
6414 valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
6415 gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
6416 fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
6417 ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
6418 sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
6419
6420 size = int_size_in_bytes (type);
6421
6422 if (s390_function_arg_pass_by_reference (TYPE_MODE (type), type))
6423 {
6424 if (TARGET_DEBUG_ARG)
6425 {
6426 fprintf (stderr, "va_arg: aggregate type");
6427 debug_tree (type);
6428 }
6429
6430 /* Aggregates are passed by reference. */
6431 indirect_p = 1;
6432 reg = gpr;
6433 n_reg = 1;
6434 sav_ofs = 2 * UNITS_PER_WORD;
6435 sav_scale = UNITS_PER_WORD;
6436 size = UNITS_PER_WORD;
6437 max_reg = 4;
6438 }
6439 else if (s390_function_arg_float (TYPE_MODE (type), type))
6440 {
6441 if (TARGET_DEBUG_ARG)
6442 {
6443 fprintf (stderr, "va_arg: float type");
6444 debug_tree (type);
6445 }
6446
6447 /* FP args go in FP registers, if present. */
6448 indirect_p = 0;
6449 reg = fpr;
6450 n_reg = 1;
6451 sav_ofs = 16 * UNITS_PER_WORD;
6452 sav_scale = 8;
6453 /* TARGET_64BIT has up to 4 parameter in fprs */
6454 max_reg = TARGET_64BIT ? 3 : 1;
6455 }
6456 else
6457 {
6458 if (TARGET_DEBUG_ARG)
6459 {
6460 fprintf (stderr, "va_arg: other type");
6461 debug_tree (type);
6462 }
6463
6464 /* Otherwise into GP registers. */
6465 indirect_p = 0;
6466 reg = gpr;
6467 n_reg = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6468 sav_ofs = 2 * UNITS_PER_WORD;
6469
6470 if (size < UNITS_PER_WORD)
6471 sav_ofs += UNITS_PER_WORD - size;
6472
6473 sav_scale = UNITS_PER_WORD;
6474 if (n_reg > 1)
6475 max_reg = 3;
6476 else
6477 max_reg = 4;
6478 }
6479
6480 /* Pull the value out of the saved registers ... */
6481
6482 lab_false = gen_label_rtx ();
6483 lab_over = gen_label_rtx ();
6484 addr_rtx = gen_reg_rtx (Pmode);
6485
6486 emit_cmp_and_jump_insns (expand_expr (reg, NULL_RTX, Pmode, EXPAND_NORMAL),
6487 GEN_INT (max_reg),
6488 GT, const1_rtx, Pmode, 0, lab_false);
6489
6490 if (sav_ofs)
6491 t = build (PLUS_EXPR, ptr_type_node, sav, build_int_2 (sav_ofs, 0));
6492 else
6493 t = sav;
6494
6495 u = build (MULT_EXPR, long_integer_type_node,
6496 reg, build_int_2 (sav_scale, 0));
6497 TREE_SIDE_EFFECTS (u) = 1;
6498
6499 t = build (PLUS_EXPR, ptr_type_node, t, u);
6500 TREE_SIDE_EFFECTS (t) = 1;
6501
6502 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
6503 if (r != addr_rtx)
6504 emit_move_insn (addr_rtx, r);
6505
6506
6507 emit_jump_insn (gen_jump (lab_over));
6508 emit_barrier ();
6509 emit_label (lab_false);
6510
6511 /* ... Otherwise out of the overflow area. */
6512
6513 t = save_expr (ovf);
6514
6515
6516 /* In 64 BIT for each argument on stack, a full 64 bit slot is allocated. */
6517 if (size < UNITS_PER_WORD)
6518 {
6519 t = build (PLUS_EXPR, TREE_TYPE (t), t, build_int_2 (UNITS_PER_WORD-size, 0));
6520 t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
6521 TREE_SIDE_EFFECTS (t) = 1;
6522 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6523
6524 t = save_expr (ovf);
6525 }
6526
6527 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
6528 if (r != addr_rtx)
6529 emit_move_insn (addr_rtx, r);
6530
6531 t = build (PLUS_EXPR, TREE_TYPE (t), t, build_int_2 (size, 0));
6532 t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
6533 TREE_SIDE_EFFECTS (t) = 1;
6534 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6535
6536 emit_label (lab_over);
6537
6538 /* If less than max_regs a registers are retrieved out
6539 of register save area, increment. */
6540
6541 u = build (PREINCREMENT_EXPR, TREE_TYPE (reg), reg,
6542 build_int_2 (n_reg, 0));
6543 TREE_SIDE_EFFECTS (u) = 1;
6544 expand_expr (u, const0_rtx, VOIDmode, EXPAND_NORMAL);
6545
6546 if (indirect_p)
6547 {
6548 r = gen_rtx_MEM (Pmode, addr_rtx);
6549 set_mem_alias_set (r, get_varargs_alias_set ());
6550 emit_move_insn (addr_rtx, r);
6551 }
6552
6553
6554 return addr_rtx;
6555 }
6556
6557
6558 /* Builtins. */
6559
6560 enum s390_builtin
6561 {
6562 S390_BUILTIN_THREAD_POINTER,
6563 S390_BUILTIN_SET_THREAD_POINTER,
6564
6565 S390_BUILTIN_max
6566 };
6567
6568 static unsigned int const code_for_builtin_64[S390_BUILTIN_max] = {
6569 CODE_FOR_get_tp_64,
6570 CODE_FOR_set_tp_64
6571 };
6572
6573 static unsigned int const code_for_builtin_31[S390_BUILTIN_max] = {
6574 CODE_FOR_get_tp_31,
6575 CODE_FOR_set_tp_31
6576 };
6577
6578 static void
6579 s390_init_builtins (void)
6580 {
6581 tree ftype;
6582
6583 ftype = build_function_type (ptr_type_node, void_list_node);
6584 builtin_function ("__builtin_thread_pointer", ftype,
6585 S390_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
6586 NULL, NULL_TREE);
6587
6588 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
6589 builtin_function ("__builtin_set_thread_pointer", ftype,
6590 S390_BUILTIN_SET_THREAD_POINTER, BUILT_IN_MD,
6591 NULL, NULL_TREE);
6592 }
6593
6594 /* Expand an expression EXP that calls a built-in function,
6595 with result going to TARGET if that's convenient
6596 (and in mode MODE if that's convenient).
6597 SUBTARGET may be used as the target for computing one of EXP's operands.
6598 IGNORE is nonzero if the value is to be ignored. */
6599
6600 static rtx
6601 s390_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
6602 enum machine_mode mode ATTRIBUTE_UNUSED,
6603 int ignore ATTRIBUTE_UNUSED)
6604 {
6605 #define MAX_ARGS 2
6606
6607 unsigned int const *code_for_builtin =
6608 TARGET_64BIT ? code_for_builtin_64 : code_for_builtin_31;
6609
6610 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6611 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
6612 tree arglist = TREE_OPERAND (exp, 1);
6613 enum insn_code icode;
6614 rtx op[MAX_ARGS], pat;
6615 int arity;
6616 bool nonvoid;
6617
6618 if (fcode >= S390_BUILTIN_max)
6619 internal_error ("bad builtin fcode");
6620 icode = code_for_builtin[fcode];
6621 if (icode == 0)
6622 internal_error ("bad builtin fcode");
6623
6624 nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
6625
6626 for (arglist = TREE_OPERAND (exp, 1), arity = 0;
6627 arglist;
6628 arglist = TREE_CHAIN (arglist), arity++)
6629 {
6630 const struct insn_operand_data *insn_op;
6631
6632 tree arg = TREE_VALUE (arglist);
6633 if (arg == error_mark_node)
6634 return NULL_RTX;
6635 if (arity > MAX_ARGS)
6636 return NULL_RTX;
6637
6638 insn_op = &insn_data[icode].operand[arity + nonvoid];
6639
6640 op[arity] = expand_expr (arg, NULL_RTX, insn_op->mode, 0);
6641
6642 if (!(*insn_op->predicate) (op[arity], insn_op->mode))
6643 op[arity] = copy_to_mode_reg (insn_op->mode, op[arity]);
6644 }
6645
6646 if (nonvoid)
6647 {
6648 enum machine_mode tmode = insn_data[icode].operand[0].mode;
6649 if (!target
6650 || GET_MODE (target) != tmode
6651 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
6652 target = gen_reg_rtx (tmode);
6653 }
6654
6655 switch (arity)
6656 {
6657 case 0:
6658 pat = GEN_FCN (icode) (target);
6659 break;
6660 case 1:
6661 if (nonvoid)
6662 pat = GEN_FCN (icode) (target, op[0]);
6663 else
6664 pat = GEN_FCN (icode) (op[0]);
6665 break;
6666 case 2:
6667 pat = GEN_FCN (icode) (target, op[0], op[1]);
6668 break;
6669 default:
6670 abort ();
6671 }
6672 if (!pat)
6673 return NULL_RTX;
6674 emit_insn (pat);
6675
6676 if (nonvoid)
6677 return target;
6678 else
6679 return const0_rtx;
6680 }
6681
6682
6683 /* Output assembly code for the trampoline template to
6684 stdio stream FILE.
6685
6686 On S/390, we use gpr 1 internally in the trampoline code;
6687 gpr 0 is used to hold the static chain. */
6688
6689 void
6690 s390_trampoline_template (FILE *file)
6691 {
6692 if (TARGET_64BIT)
6693 {
6694 fprintf (file, "larl\t%s,0f\n", reg_names[1]);
6695 fprintf (file, "lg\t%s,0(%s)\n", reg_names[0], reg_names[1]);
6696 fprintf (file, "lg\t%s,8(%s)\n", reg_names[1], reg_names[1]);
6697 fprintf (file, "br\t%s\n", reg_names[1]);
6698 fprintf (file, "0:\t.quad\t0\n");
6699 fprintf (file, ".quad\t0\n");
6700 }
6701 else
6702 {
6703 fprintf (file, "basr\t%s,0\n", reg_names[1]);
6704 fprintf (file, "l\t%s,10(%s)\n", reg_names[0], reg_names[1]);
6705 fprintf (file, "l\t%s,14(%s)\n", reg_names[1], reg_names[1]);
6706 fprintf (file, "br\t%s\n", reg_names[1]);
6707 fprintf (file, ".long\t0\n");
6708 fprintf (file, ".long\t0\n");
6709 }
6710 }
6711
6712 /* Emit RTL insns to initialize the variable parts of a trampoline.
6713 FNADDR is an RTX for the address of the function's pure code.
6714 CXT is an RTX for the static chain value for the function. */
6715
6716 void
6717 s390_initialize_trampoline (rtx addr, rtx fnaddr, rtx cxt)
6718 {
6719 emit_move_insn (gen_rtx
6720 (MEM, Pmode,
6721 memory_address (Pmode,
6722 plus_constant (addr, (TARGET_64BIT ? 20 : 12) ))), cxt);
6723 emit_move_insn (gen_rtx
6724 (MEM, Pmode,
6725 memory_address (Pmode,
6726 plus_constant (addr, (TARGET_64BIT ? 28 : 16) ))), fnaddr);
6727 }
6728
6729 /* Return rtx for 64-bit constant formed from the 32-bit subwords
6730 LOW and HIGH, independent of the host word size. */
6731
6732 rtx
6733 s390_gen_rtx_const_DI (int high, int low)
6734 {
6735 #if HOST_BITS_PER_WIDE_INT >= 64
6736 HOST_WIDE_INT val;
6737 val = (HOST_WIDE_INT)high;
6738 val <<= 32;
6739 val |= (HOST_WIDE_INT)low;
6740
6741 return GEN_INT (val);
6742 #else
6743 #if HOST_BITS_PER_WIDE_INT >= 32
6744 return immed_double_const ((HOST_WIDE_INT)low, (HOST_WIDE_INT)high, DImode);
6745 #else
6746 abort ();
6747 #endif
6748 #endif
6749 }
6750
6751 /* Output assembler code to FILE to increment profiler label # LABELNO
6752 for profiling a function entry. */
6753
6754 void
6755 s390_function_profiler (FILE *file, int labelno)
6756 {
6757 rtx op[7];
6758
6759 char label[128];
6760 ASM_GENERATE_INTERNAL_LABEL (label, "LP", labelno);
6761
6762 fprintf (file, "# function profiler \n");
6763
6764 op[0] = gen_rtx_REG (Pmode, RETURN_REGNUM);
6765 op[1] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
6766 op[1] = gen_rtx_MEM (Pmode, plus_constant (op[1], UNITS_PER_WORD));
6767
6768 op[2] = gen_rtx_REG (Pmode, 1);
6769 op[3] = gen_rtx_SYMBOL_REF (Pmode, label);
6770 SYMBOL_REF_FLAGS (op[3]) = SYMBOL_FLAG_LOCAL;
6771
6772 op[4] = gen_rtx_SYMBOL_REF (Pmode, "_mcount");
6773 if (flag_pic)
6774 {
6775 op[4] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op[4]), UNSPEC_PLT);
6776 op[4] = gen_rtx_CONST (Pmode, op[4]);
6777 }
6778
6779 if (TARGET_64BIT)
6780 {
6781 output_asm_insn ("stg\t%0,%1", op);
6782 output_asm_insn ("larl\t%2,%3", op);
6783 output_asm_insn ("brasl\t%0,%4", op);
6784 output_asm_insn ("lg\t%0,%1", op);
6785 }
6786 else if (!flag_pic)
6787 {
6788 op[6] = gen_label_rtx ();
6789
6790 output_asm_insn ("st\t%0,%1", op);
6791 output_asm_insn ("bras\t%2,%l6", op);
6792 output_asm_insn (".long\t%4", op);
6793 output_asm_insn (".long\t%3", op);
6794 targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[6]));
6795 output_asm_insn ("l\t%0,0(%2)", op);
6796 output_asm_insn ("l\t%2,4(%2)", op);
6797 output_asm_insn ("basr\t%0,%0", op);
6798 output_asm_insn ("l\t%0,%1", op);
6799 }
6800 else
6801 {
6802 op[5] = gen_label_rtx ();
6803 op[6] = gen_label_rtx ();
6804
6805 output_asm_insn ("st\t%0,%1", op);
6806 output_asm_insn ("bras\t%2,%l6", op);
6807 targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[5]));
6808 output_asm_insn (".long\t%4-%l5", op);
6809 output_asm_insn (".long\t%3-%l5", op);
6810 targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[6]));
6811 output_asm_insn ("lr\t%0,%2", op);
6812 output_asm_insn ("a\t%0,0(%2)", op);
6813 output_asm_insn ("a\t%2,4(%2)", op);
6814 output_asm_insn ("basr\t%0,%0", op);
6815 output_asm_insn ("l\t%0,%1", op);
6816 }
6817 }
6818
6819 /* Select section for constant in constant pool. In 32-bit mode,
6820 constants go in the function section; in 64-bit mode in .rodata. */
6821
6822 static void
6823 s390_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6824 rtx x ATTRIBUTE_UNUSED,
6825 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6826 {
6827 if (TARGET_CPU_ZARCH)
6828 readonly_data_section ();
6829 else
6830 function_section (current_function_decl);
6831 }
6832
6833 /* Encode symbol attributes (local vs. global, tls model) of a SYMBOL_REF
6834 into its SYMBOL_REF_FLAGS. */
6835
6836 static void
6837 s390_encode_section_info (tree decl, rtx rtl, int first)
6838 {
6839 default_encode_section_info (decl, rtl, first);
6840
6841 /* If a variable has a forced alignment to < 2 bytes, mark it with
6842 SYMBOL_FLAG_ALIGN1 to prevent it from being used as LARL operand. */
6843 if (TREE_CODE (decl) == VAR_DECL
6844 && DECL_USER_ALIGN (decl) && DECL_ALIGN (decl) < 16)
6845 SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_ALIGN1;
6846 }
6847
6848 /* Output thunk to FILE that implements a C++ virtual function call (with
6849 multiple inheritance) to FUNCTION. The thunk adjusts the this pointer
6850 by DELTA, and unless VCALL_OFFSET is zero, applies an additional adjustment
6851 stored at VCALL_OFFSET in the vtable whose address is located at offset 0
6852 relative to the resulting this pointer. */
6853
6854 static void
6855 s390_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
6856 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
6857 tree function)
6858 {
6859 rtx op[10];
6860 int nonlocal = 0;
6861
6862 /* Operand 0 is the target function. */
6863 op[0] = XEXP (DECL_RTL (function), 0);
6864 if (flag_pic && !SYMBOL_REF_LOCAL_P (op[0]))
6865 {
6866 nonlocal = 1;
6867 op[0] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op[0]),
6868 TARGET_64BIT ? UNSPEC_PLT : UNSPEC_GOT);
6869 op[0] = gen_rtx_CONST (Pmode, op[0]);
6870 }
6871
6872 /* Operand 1 is the 'this' pointer. */
6873 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
6874 op[1] = gen_rtx_REG (Pmode, 3);
6875 else
6876 op[1] = gen_rtx_REG (Pmode, 2);
6877
6878 /* Operand 2 is the delta. */
6879 op[2] = GEN_INT (delta);
6880
6881 /* Operand 3 is the vcall_offset. */
6882 op[3] = GEN_INT (vcall_offset);
6883
6884 /* Operand 4 is the temporary register. */
6885 op[4] = gen_rtx_REG (Pmode, 1);
6886
6887 /* Operands 5 to 8 can be used as labels. */
6888 op[5] = NULL_RTX;
6889 op[6] = NULL_RTX;
6890 op[7] = NULL_RTX;
6891 op[8] = NULL_RTX;
6892
6893 /* Operand 9 can be used for temporary register. */
6894 op[9] = NULL_RTX;
6895
6896 /* Generate code. */
6897 if (TARGET_64BIT)
6898 {
6899 /* Setup literal pool pointer if required. */
6900 if ((!DISP_IN_RANGE (delta)
6901 && !CONST_OK_FOR_LETTER_P (delta, 'K'))
6902 || (!DISP_IN_RANGE (vcall_offset)
6903 && !CONST_OK_FOR_LETTER_P (vcall_offset, 'K')))
6904 {
6905 op[5] = gen_label_rtx ();
6906 output_asm_insn ("larl\t%4,%5", op);
6907 }
6908
6909 /* Add DELTA to this pointer. */
6910 if (delta)
6911 {
6912 if (CONST_OK_FOR_LETTER_P (delta, 'J'))
6913 output_asm_insn ("la\t%1,%2(%1)", op);
6914 else if (DISP_IN_RANGE (delta))
6915 output_asm_insn ("lay\t%1,%2(%1)", op);
6916 else if (CONST_OK_FOR_LETTER_P (delta, 'K'))
6917 output_asm_insn ("aghi\t%1,%2", op);
6918 else
6919 {
6920 op[6] = gen_label_rtx ();
6921 output_asm_insn ("agf\t%1,%6-%5(%4)", op);
6922 }
6923 }
6924
6925 /* Perform vcall adjustment. */
6926 if (vcall_offset)
6927 {
6928 if (DISP_IN_RANGE (vcall_offset))
6929 {
6930 output_asm_insn ("lg\t%4,0(%1)", op);
6931 output_asm_insn ("ag\t%1,%3(%4)", op);
6932 }
6933 else if (CONST_OK_FOR_LETTER_P (vcall_offset, 'K'))
6934 {
6935 output_asm_insn ("lghi\t%4,%3", op);
6936 output_asm_insn ("ag\t%4,0(%1)", op);
6937 output_asm_insn ("ag\t%1,0(%4)", op);
6938 }
6939 else
6940 {
6941 op[7] = gen_label_rtx ();
6942 output_asm_insn ("llgf\t%4,%7-%5(%4)", op);
6943 output_asm_insn ("ag\t%4,0(%1)", op);
6944 output_asm_insn ("ag\t%1,0(%4)", op);
6945 }
6946 }
6947
6948 /* Jump to target. */
6949 output_asm_insn ("jg\t%0", op);
6950
6951 /* Output literal pool if required. */
6952 if (op[5])
6953 {
6954 output_asm_insn (".align\t4", op);
6955 targetm.asm_out.internal_label (file, "L",
6956 CODE_LABEL_NUMBER (op[5]));
6957 }
6958 if (op[6])
6959 {
6960 targetm.asm_out.internal_label (file, "L",
6961 CODE_LABEL_NUMBER (op[6]));
6962 output_asm_insn (".long\t%2", op);
6963 }
6964 if (op[7])
6965 {
6966 targetm.asm_out.internal_label (file, "L",
6967 CODE_LABEL_NUMBER (op[7]));
6968 output_asm_insn (".long\t%3", op);
6969 }
6970 }
6971 else
6972 {
6973 /* Setup base pointer if required. */
6974 if (!vcall_offset
6975 || (!DISP_IN_RANGE (delta)
6976 && !CONST_OK_FOR_LETTER_P (delta, 'K'))
6977 || (!DISP_IN_RANGE (delta)
6978 && !CONST_OK_FOR_LETTER_P (vcall_offset, 'K')))
6979 {
6980 op[5] = gen_label_rtx ();
6981 output_asm_insn ("basr\t%4,0", op);
6982 targetm.asm_out.internal_label (file, "L",
6983 CODE_LABEL_NUMBER (op[5]));
6984 }
6985
6986 /* Add DELTA to this pointer. */
6987 if (delta)
6988 {
6989 if (CONST_OK_FOR_LETTER_P (delta, 'J'))
6990 output_asm_insn ("la\t%1,%2(%1)", op);
6991 else if (DISP_IN_RANGE (delta))
6992 output_asm_insn ("lay\t%1,%2(%1)", op);
6993 else if (CONST_OK_FOR_LETTER_P (delta, 'K'))
6994 output_asm_insn ("ahi\t%1,%2", op);
6995 else
6996 {
6997 op[6] = gen_label_rtx ();
6998 output_asm_insn ("a\t%1,%6-%5(%4)", op);
6999 }
7000 }
7001
7002 /* Perform vcall adjustment. */
7003 if (vcall_offset)
7004 {
7005 if (CONST_OK_FOR_LETTER_P (vcall_offset, 'J'))
7006 {
7007 output_asm_insn ("lg\t%4,0(%1)", op);
7008 output_asm_insn ("a\t%1,%3(%4)", op);
7009 }
7010 else if (DISP_IN_RANGE (vcall_offset))
7011 {
7012 output_asm_insn ("lg\t%4,0(%1)", op);
7013 output_asm_insn ("ay\t%1,%3(%4)", op);
7014 }
7015 else if (CONST_OK_FOR_LETTER_P (vcall_offset, 'K'))
7016 {
7017 output_asm_insn ("lhi\t%4,%3", op);
7018 output_asm_insn ("a\t%4,0(%1)", op);
7019 output_asm_insn ("a\t%1,0(%4)", op);
7020 }
7021 else
7022 {
7023 op[7] = gen_label_rtx ();
7024 output_asm_insn ("l\t%4,%7-%5(%4)", op);
7025 output_asm_insn ("a\t%4,0(%1)", op);
7026 output_asm_insn ("a\t%1,0(%4)", op);
7027 }
7028
7029 /* We had to clobber the base pointer register.
7030 Re-setup the base pointer (with a different base). */
7031 op[5] = gen_label_rtx ();
7032 output_asm_insn ("basr\t%4,0", op);
7033 targetm.asm_out.internal_label (file, "L",
7034 CODE_LABEL_NUMBER (op[5]));
7035 }
7036
7037 /* Jump to target. */
7038 op[8] = gen_label_rtx ();
7039
7040 if (!flag_pic)
7041 output_asm_insn ("l\t%4,%8-%5(%4)", op);
7042 else if (!nonlocal)
7043 output_asm_insn ("a\t%4,%8-%5(%4)", op);
7044 /* We cannot call through .plt, since .plt requires %r12 loaded. */
7045 else if (flag_pic == 1)
7046 {
7047 output_asm_insn ("a\t%4,%8-%5(%4)", op);
7048 output_asm_insn ("l\t%4,%0(%4)", op);
7049 }
7050 else if (flag_pic == 2)
7051 {
7052 op[9] = gen_rtx_REG (Pmode, 0);
7053 output_asm_insn ("l\t%9,%8-4-%5(%4)", op);
7054 output_asm_insn ("a\t%4,%8-%5(%4)", op);
7055 output_asm_insn ("ar\t%4,%9", op);
7056 output_asm_insn ("l\t%4,0(%4)", op);
7057 }
7058
7059 output_asm_insn ("br\t%4", op);
7060
7061 /* Output literal pool. */
7062 output_asm_insn (".align\t4", op);
7063
7064 if (nonlocal && flag_pic == 2)
7065 output_asm_insn (".long\t%0", op);
7066 if (nonlocal)
7067 {
7068 op[0] = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
7069 SYMBOL_REF_FLAGS (op[0]) = SYMBOL_FLAG_LOCAL;
7070 }
7071
7072 targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[8]));
7073 if (!flag_pic)
7074 output_asm_insn (".long\t%0", op);
7075 else
7076 output_asm_insn (".long\t%0-%5", op);
7077
7078 if (op[6])
7079 {
7080 targetm.asm_out.internal_label (file, "L",
7081 CODE_LABEL_NUMBER (op[6]));
7082 output_asm_insn (".long\t%2", op);
7083 }
7084 if (op[7])
7085 {
7086 targetm.asm_out.internal_label (file, "L",
7087 CODE_LABEL_NUMBER (op[7]));
7088 output_asm_insn (".long\t%3", op);
7089 }
7090 }
7091 }
7092
7093 bool
7094 s390_valid_pointer_mode (enum machine_mode mode)
7095 {
7096 return (mode == SImode || (TARGET_64BIT && mode == DImode));
7097 }
7098
7099 /* How to allocate a 'struct machine_function'. */
7100
7101 static struct machine_function *
7102 s390_init_machine_status (void)
7103 {
7104 return ggc_alloc_cleared (sizeof (struct machine_function));
7105 }
7106
7107 #include "gt-s390.h"