OSDN Git Service

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