OSDN Git Service

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