OSDN Git Service

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