OSDN Git Service

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