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