OSDN Git Service

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