OSDN Git Service

2005-10-19 Adrian Straetling <straetling@de.ibm.com>
[pf3gnuchains/gcc-fork.git] / gcc / config / s390 / s390.c
1 /* Subroutines used for code generation on IBM S/390 and zSeries
2    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005
3    Free Software Foundation, Inc.
4    Contributed by Hartmut Penner (hpenner@de.ibm.com) and
5                   Ulrich Weigand (uweigand@de.ibm.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING.  If not, write to the Free
21 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA.  */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "tm_p.h"
31 #include "regs.h"
32 #include "hard-reg-set.h"
33 #include "real.h"
34 #include "insn-config.h"
35 #include "conditions.h"
36 #include "output.h"
37 #include "insn-attr.h"
38 #include "flags.h"
39 #include "except.h"
40 #include "function.h"
41 #include "recog.h"
42 #include "expr.h"
43 #include "reload.h"
44 #include "toplev.h"
45 #include "basic-block.h"
46 #include "integrate.h"
47 #include "ggc.h"
48 #include "target.h"
49 #include "target-def.h"
50 #include "debug.h"
51 #include "langhooks.h"
52 #include "optabs.h"
53 #include "tree-gimple.h"
54
55
56 /* Define the specific costs for a given cpu.  */
57
58 struct processor_costs 
59 {
60   /* multiplication */
61   const int m;        /* cost of an M instruction.  */
62   const int mghi;     /* cost of an MGHI instruction.  */
63   const int mh;       /* cost of an MH instruction.  */
64   const int mhi;      /* cost of an MHI instruction.  */
65   const int ml;       /* cost of an ML instruction.  */
66   const int mr;       /* cost of an MR instruction.  */
67   const int ms;       /* cost of an MS instruction.  */
68   const int msg;      /* cost of an MSG instruction.  */
69   const int msgf;     /* cost of an MSGF instruction.  */
70   const int msgfr;    /* cost of an MSGFR instruction.  */
71   const int msgr;     /* cost of an MSGR instruction.  */
72   const int msr;      /* cost of an MSR instruction.  */
73   const int mult_df;  /* cost of multiplication in DFmode.  */
74   /* square root */
75   const int sqdbr;    /* cost of square root in DFmode.  */
76   const int sqebr;    /* cost of square root in SFmode.  */
77   /* multiply and add */
78   const int madbr;    /* cost of multiply and add in DFmode.  */
79   const int maebr;    /* cost of multiply and add in SFmode.  */
80   /* division */
81   const int ddbr;
82   const int ddr;
83   const int debr;
84   const int der;
85   const int dlgr;
86   const int dlr;
87   const int dr;
88   const int dsgfr;
89   const int dsgr;
90 };
91
92 const struct processor_costs *s390_cost;
93
94 static const
95 struct processor_costs z900_cost = 
96 {
97   COSTS_N_INSNS (5),     /* M     */
98   COSTS_N_INSNS (10),    /* MGHI  */
99   COSTS_N_INSNS (5),     /* MH    */
100   COSTS_N_INSNS (4),     /* MHI   */
101   COSTS_N_INSNS (5),     /* ML    */
102   COSTS_N_INSNS (5),     /* MR    */
103   COSTS_N_INSNS (4),     /* MS    */
104   COSTS_N_INSNS (15),    /* MSG   */
105   COSTS_N_INSNS (7),     /* MSGF  */
106   COSTS_N_INSNS (7),     /* MSGFR */
107   COSTS_N_INSNS (10),    /* MSGR  */
108   COSTS_N_INSNS (4),     /* MSR   */
109   COSTS_N_INSNS (7),     /* multiplication in DFmode */
110   COSTS_N_INSNS (44),    /* SQDBR */
111   COSTS_N_INSNS (35),    /* SQEBR */
112   COSTS_N_INSNS (18),    /* MADBR */
113   COSTS_N_INSNS (13),    /* MAEBR */
114   COSTS_N_INSNS (30),    /* DDBR */
115   COSTS_N_INSNS (30),    /* DDR  */
116   COSTS_N_INSNS (27),    /* DEBR */
117   COSTS_N_INSNS (26),    /* DER  */
118   COSTS_N_INSNS (220),   /* DLGR */
119   COSTS_N_INSNS (34),    /* DLR */
120   COSTS_N_INSNS (34),    /* DR */
121   COSTS_N_INSNS (32),    /* DSGFR */
122   COSTS_N_INSNS (32),    /* DSGR */
123 };
124
125 static const
126 struct processor_costs z990_cost = 
127 {
128   COSTS_N_INSNS (4),     /* M     */
129   COSTS_N_INSNS (2),     /* MGHI  */
130   COSTS_N_INSNS (2),     /* MH    */
131   COSTS_N_INSNS (2),     /* MHI   */
132   COSTS_N_INSNS (4),     /* ML    */
133   COSTS_N_INSNS (4),     /* MR    */
134   COSTS_N_INSNS (5),     /* MS    */
135   COSTS_N_INSNS (6),     /* MSG   */
136   COSTS_N_INSNS (4),     /* MSGF  */
137   COSTS_N_INSNS (4),     /* MSGFR */
138   COSTS_N_INSNS (4),     /* MSGR  */
139   COSTS_N_INSNS (4),     /* MSR   */
140   COSTS_N_INSNS (1),     /* multiplication in DFmode */
141   COSTS_N_INSNS (66),    /* SQDBR */
142   COSTS_N_INSNS (38),    /* SQEBR */
143   COSTS_N_INSNS (1),     /* MADBR */
144   COSTS_N_INSNS (1),     /* MAEBR */
145   COSTS_N_INSNS (40),    /* DDBR */
146   COSTS_N_INSNS (44),    /* DDR  */
147   COSTS_N_INSNS (26),    /* DDBR */
148   COSTS_N_INSNS (28),    /* DER  */
149   COSTS_N_INSNS (176),   /* DLGR */
150   COSTS_N_INSNS (31),    /* DLR */
151   COSTS_N_INSNS (31),    /* DR */
152   COSTS_N_INSNS (31),    /* DSGFR */
153   COSTS_N_INSNS (31),    /* DSGR */
154 };
155
156 static const
157 struct processor_costs z9_109_cost = 
158 {
159   COSTS_N_INSNS (4),     /* M     */
160   COSTS_N_INSNS (2),     /* MGHI  */
161   COSTS_N_INSNS (2),     /* MH    */
162   COSTS_N_INSNS (2),     /* MHI   */
163   COSTS_N_INSNS (4),     /* ML    */
164   COSTS_N_INSNS (4),     /* MR    */
165   COSTS_N_INSNS (5),     /* MS    */
166   COSTS_N_INSNS (6),     /* MSG   */
167   COSTS_N_INSNS (4),     /* MSGF  */
168   COSTS_N_INSNS (4),     /* MSGFR */
169   COSTS_N_INSNS (4),     /* MSGR  */
170   COSTS_N_INSNS (4),     /* MSR   */
171   COSTS_N_INSNS (1),     /* multiplication in DFmode */
172   COSTS_N_INSNS (66),    /* SQDBR */
173   COSTS_N_INSNS (38),    /* SQEBR */
174   COSTS_N_INSNS (1),     /* MADBR */
175   COSTS_N_INSNS (1),     /* MAEBR */
176   COSTS_N_INSNS (40),    /* DDBR */
177   COSTS_N_INSNS (37),    /* DDR  */
178   COSTS_N_INSNS (26),    /* DDBR */
179   COSTS_N_INSNS (28),    /* DER  */
180   COSTS_N_INSNS (30),    /* DLGR */
181   COSTS_N_INSNS (23),    /* DLR */
182   COSTS_N_INSNS (23),    /* DR */
183   COSTS_N_INSNS (24),    /* DSGFR */
184   COSTS_N_INSNS (24),    /* DSGR */
185 };
186
187 extern int reload_completed;
188
189 /* Save information from a "cmpxx" operation until the branch or scc is
190    emitted.  */
191 rtx s390_compare_op0, s390_compare_op1;
192
193 /* Save the result of a compare_and_swap  until the branch or scc is
194    emitted.  */
195 rtx s390_compare_emitted = NULL_RTX;
196
197 /* Structure used to hold the components of a S/390 memory
198    address.  A legitimate address on S/390 is of the general
199    form
200           base + index + displacement
201    where any of the components is optional.
202
203    base and index are registers of the class ADDR_REGS,
204    displacement is an unsigned 12-bit immediate constant.  */
205
206 struct s390_address
207 {
208   rtx base;
209   rtx indx;
210   rtx disp;
211   bool pointer;
212 };
213
214 /* Which cpu are we tuning for.  */
215 enum processor_type s390_tune = PROCESSOR_max;
216 enum processor_flags s390_tune_flags;
217 /* Which instruction set architecture to use.  */
218 enum processor_type s390_arch;
219 enum processor_flags s390_arch_flags;
220
221 HOST_WIDE_INT s390_warn_framesize = 0;
222 HOST_WIDE_INT s390_stack_size = 0;
223 HOST_WIDE_INT s390_stack_guard = 0;
224
225 /* The following structure is embedded in the machine 
226    specific part of struct function.  */
227
228 struct s390_frame_layout GTY (())
229 {
230   /* Offset within stack frame.  */
231   HOST_WIDE_INT gprs_offset;
232   HOST_WIDE_INT f0_offset;
233   HOST_WIDE_INT f4_offset;
234   HOST_WIDE_INT f8_offset;
235   HOST_WIDE_INT backchain_offset;
236   
237   /* Number of first and last gpr to be saved, restored.  */
238   int first_save_gpr;
239   int first_restore_gpr;
240   int last_save_gpr;
241   int last_restore_gpr;
242
243   /* Bits standing for floating point registers. Set, if the 
244      respective register has to be saved. Starting with reg 16 (f0) 
245      at the rightmost bit.
246      Bit 15 -  8  7  6  5  4  3  2  1  0
247      fpr 15 -  8  7  5  3  1  6  4  2  0
248      reg 31 - 24 23 22 21 20 19 18 17 16  */
249   unsigned int fpr_bitmap;
250
251   /* Number of floating point registers f8-f15 which must be saved.  */
252   int high_fprs;
253
254   /* Set if return address needs to be saved.
255      This flag is set by s390_return_addr_rtx if it could not use
256      the initial value of r14 and therefore depends on r14 saved
257      to the stack.  */
258   bool save_return_addr_p;
259
260   /* Size of stack frame.  */
261   HOST_WIDE_INT frame_size;
262 };
263
264 /* Define the structure for the machine field in struct function.  */
265
266 struct machine_function GTY(())
267 {
268   struct s390_frame_layout frame_layout;
269
270   /* Literal pool base register.  */
271   rtx base_reg;
272
273   /* True if we may need to perform branch splitting.  */
274   bool split_branches_pending_p;
275
276   /* Some local-dynamic TLS symbol name.  */
277   const char *some_ld_name;
278
279   bool has_landing_pad_p;
280 };
281
282 /* Few accessor macros for struct cfun->machine->s390_frame_layout.  */
283
284 #define cfun_frame_layout (cfun->machine->frame_layout)
285 #define cfun_save_high_fprs_p (!!cfun_frame_layout.high_fprs)
286 #define cfun_gprs_save_area_size ((cfun_frame_layout.last_save_gpr -           \
287   cfun_frame_layout.first_save_gpr + 1) * UNITS_PER_WORD)
288 #define cfun_set_fpr_bit(BITNUM) (cfun->machine->frame_layout.fpr_bitmap |=    \
289   (1 << (BITNUM)))
290 #define cfun_fpr_bit_p(BITNUM) (!!(cfun->machine->frame_layout.fpr_bitmap &    \
291   (1 << (BITNUM))))
292
293 /* Number of GPRs and FPRs used for argument passing.  */
294 #define GP_ARG_NUM_REG 5
295 #define FP_ARG_NUM_REG (TARGET_64BIT? 4 : 2)
296
297 /* A couple of shortcuts.  */
298 #define CONST_OK_FOR_J(x) \
299         CONST_OK_FOR_CONSTRAINT_P((x), 'J', "J")
300 #define CONST_OK_FOR_K(x) \
301         CONST_OK_FOR_CONSTRAINT_P((x), 'K', "K")
302 #define CONST_OK_FOR_Os(x) \
303         CONST_OK_FOR_CONSTRAINT_P((x), 'O', "Os")
304 #define CONST_OK_FOR_Op(x) \
305         CONST_OK_FOR_CONSTRAINT_P((x), 'O', "Op")
306 #define CONST_OK_FOR_On(x) \
307         CONST_OK_FOR_CONSTRAINT_P((x), 'O', "On")
308
309 /* Set the has_landing_pad_p flag in struct machine_function to VALUE.  */
310
311 void
312 s390_set_has_landing_pad_p (bool value)
313 {
314   cfun->machine->has_landing_pad_p = value;
315 }
316
317 /* If two condition code modes are compatible, return a condition code
318    mode which is compatible with both.  Otherwise, return
319    VOIDmode.  */
320
321 static enum machine_mode
322 s390_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
323 {
324   if (m1 == m2)
325     return m1;
326
327   switch (m1)
328     {
329     case CCZmode:
330       if (m2 == CCUmode || m2 == CCTmode || m2 == CCZ1mode
331           || m2 == CCSmode || m2 == CCSRmode || m2 == CCURmode)
332         return m2;
333       return VOIDmode;
334
335     case CCSmode:
336     case CCUmode:
337     case CCTmode:
338     case CCSRmode:
339     case CCURmode:
340     case CCZ1mode:
341       if (m2 == CCZmode)
342         return m1;
343       
344       return VOIDmode;
345
346     default:
347       return VOIDmode;
348     }
349   return VOIDmode;
350 }
351
352 /* Return true if SET either doesn't set the CC register, or else
353    the source and destination have matching CC modes and that
354    CC mode is at least as constrained as REQ_MODE.  */
355
356 static bool
357 s390_match_ccmode_set (rtx set, enum machine_mode req_mode)
358 {
359   enum machine_mode set_mode;
360
361   gcc_assert (GET_CODE (set) == SET);
362
363   if (GET_CODE (SET_DEST (set)) != REG || !CC_REGNO_P (REGNO (SET_DEST (set))))
364     return 1;
365
366   set_mode = GET_MODE (SET_DEST (set));
367   switch (set_mode)
368     {
369     case CCSmode:
370     case CCSRmode:
371     case CCUmode:
372     case CCURmode:
373     case CCLmode:
374     case CCL1mode:
375     case CCL2mode:
376     case CCL3mode:
377     case CCT1mode:
378     case CCT2mode:
379     case CCT3mode:
380       if (req_mode != set_mode)
381         return 0;
382       break;
383
384     case CCZmode:
385       if (req_mode != CCSmode && req_mode != CCUmode && req_mode != CCTmode
386           && req_mode != CCSRmode && req_mode != CCURmode)
387         return 0;
388       break;
389
390     case CCAPmode:
391     case CCANmode:
392       if (req_mode != CCAmode)
393         return 0;
394       break;
395
396     default:
397       gcc_unreachable ();
398     }
399
400   return (GET_MODE (SET_SRC (set)) == set_mode);
401 }
402
403 /* Return true if every SET in INSN that sets the CC register
404    has source and destination with matching CC modes and that
405    CC mode is at least as constrained as REQ_MODE.
406    If REQ_MODE is VOIDmode, always return false.  */
407
408 bool
409 s390_match_ccmode (rtx insn, enum machine_mode req_mode)
410 {
411   int i;
412
413   /* s390_tm_ccmode returns VOIDmode to indicate failure.  */
414   if (req_mode == VOIDmode)
415     return false;
416
417   if (GET_CODE (PATTERN (insn)) == SET)
418     return s390_match_ccmode_set (PATTERN (insn), req_mode);
419
420   if (GET_CODE (PATTERN (insn)) == PARALLEL)
421       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
422         {
423           rtx set = XVECEXP (PATTERN (insn), 0, i);
424           if (GET_CODE (set) == SET)
425             if (!s390_match_ccmode_set (set, req_mode))
426               return false;
427         }
428
429   return true;
430 }
431
432 /* If a test-under-mask instruction can be used to implement
433    (compare (and ... OP1) OP2), return the CC mode required
434    to do that.  Otherwise, return VOIDmode.
435    MIXED is true if the instruction can distinguish between
436    CC1 and CC2 for mixed selected bits (TMxx), it is false
437    if the instruction cannot (TM).  */
438
439 enum machine_mode
440 s390_tm_ccmode (rtx op1, rtx op2, bool mixed)
441 {
442   int bit0, bit1;
443
444   /* ??? Fixme: should work on CONST_DOUBLE as well.  */
445   if (GET_CODE (op1) != CONST_INT || GET_CODE (op2) != CONST_INT)
446     return VOIDmode;
447
448   /* Selected bits all zero: CC0.
449      e.g.: int a; if ((a & (16 + 128)) == 0) */
450   if (INTVAL (op2) == 0)
451     return CCTmode;
452
453   /* Selected bits all one: CC3. 
454      e.g.: int a; if ((a & (16 + 128)) == 16 + 128) */
455   if (INTVAL (op2) == INTVAL (op1))
456     return CCT3mode;
457
458   /* Exactly two bits selected, mixed zeroes and ones: CC1 or CC2. e.g.:
459      int a;
460      if ((a & (16 + 128)) == 16)         -> CCT1
461      if ((a & (16 + 128)) == 128)        -> CCT2  */
462   if (mixed)
463     {
464       bit1 = exact_log2 (INTVAL (op2));
465       bit0 = exact_log2 (INTVAL (op1) ^ INTVAL (op2));
466       if (bit0 != -1 && bit1 != -1)
467         return bit0 > bit1 ? CCT1mode : CCT2mode;
468     }
469
470   return VOIDmode;
471 }
472
473 /* Given a comparison code OP (EQ, NE, etc.) and the operands
474    OP0 and OP1 of a COMPARE, return the mode to be used for the
475    comparison.  */
476
477 enum machine_mode
478 s390_select_ccmode (enum rtx_code code, rtx op0, rtx op1)
479 {
480   switch (code)
481     {
482       case EQ:
483       case NE:
484         if ((GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
485             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
486           return CCAPmode;
487         if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
488             && CONST_OK_FOR_K (INTVAL (XEXP (op0, 1))))
489           return CCAPmode;
490         if ((GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
491              || GET_CODE (op1) == NEG)
492             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
493           return CCLmode;
494
495         if (GET_CODE (op0) == AND)
496           {
497             /* Check whether we can potentially do it via TM.  */
498             enum machine_mode ccmode;
499             ccmode = s390_tm_ccmode (XEXP (op0, 1), op1, 1);
500             if (ccmode != VOIDmode)
501               {
502                 /* Relax CCTmode to CCZmode to allow fall-back to AND
503                    if that turns out to be beneficial.  */
504                 return ccmode == CCTmode ? CCZmode : ccmode;
505               }
506           }
507
508         if (register_operand (op0, HImode)
509             && GET_CODE (op1) == CONST_INT
510             && (INTVAL (op1) == -1 || INTVAL (op1) == 65535))
511           return CCT3mode;
512         if (register_operand (op0, QImode)
513             && GET_CODE (op1) == CONST_INT
514             && (INTVAL (op1) == -1 || INTVAL (op1) == 255))
515           return CCT3mode;
516
517         return CCZmode;
518
519       case LE:
520       case LT:
521       case GE:
522       case GT:
523         /* The only overflow condition of NEG and ABS happens when
524            -INT_MAX is used as parameter, which stays negative. So
525            we have an overflow from a positive value to a negative. 
526            Using CCAP mode the resulting cc can be used for comparisons.  */
527         if ((GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
528             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
529           return CCAPmode;
530
531         /* If constants are involved in an add instruction it is possible to use
532            the resulting cc for comparisons with zero. Knowing the sign of the
533            constant the overflow behavior gets predictable. e.g.:
534              int a, b; if ((b = a + c) > 0)  
535            with c as a constant value: c < 0 -> CCAN and c >= 0 -> CCAP  */
536         if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
537             && CONST_OK_FOR_K (INTVAL (XEXP (op0, 1))))
538           {
539             if (INTVAL (XEXP((op0), 1)) < 0)
540               return CCANmode;
541             else
542               return CCAPmode;
543           }
544         /* Fall through.  */
545       case UNORDERED:
546       case ORDERED:
547       case UNEQ:
548       case UNLE:
549       case UNLT:
550       case UNGE:
551       case UNGT:
552       case LTGT:
553         if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
554             && GET_CODE (op1) != CONST_INT)
555           return CCSRmode;
556         return CCSmode;
557
558       case LTU:
559       case GEU:
560         if (GET_CODE (op0) == PLUS
561             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
562           return CCL1mode;
563
564         if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
565             && GET_CODE (op1) != CONST_INT)
566           return CCURmode;
567         return CCUmode;
568
569       case LEU:
570       case GTU:
571         if (GET_CODE (op0) == MINUS
572             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
573           return CCL2mode;
574
575         if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
576             && GET_CODE (op1) != CONST_INT)
577           return CCURmode;
578         return CCUmode;
579
580       default:
581         gcc_unreachable ();
582     }
583 }
584
585 /* Replace the comparison OP0 CODE OP1 by a semantically equivalent one
586    that we can implement more efficiently.  */
587
588 void
589 s390_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1)
590 {
591   /* Convert ZERO_EXTRACT back to AND to enable TM patterns.  */
592   if ((*code == EQ || *code == NE)
593       && *op1 == const0_rtx
594       && GET_CODE (*op0) == ZERO_EXTRACT
595       && GET_CODE (XEXP (*op0, 1)) == CONST_INT
596       && GET_CODE (XEXP (*op0, 2)) == CONST_INT
597       && SCALAR_INT_MODE_P (GET_MODE (XEXP (*op0, 0))))
598     {
599       rtx inner = XEXP (*op0, 0);
600       HOST_WIDE_INT modesize = GET_MODE_BITSIZE (GET_MODE (inner));
601       HOST_WIDE_INT len = INTVAL (XEXP (*op0, 1));
602       HOST_WIDE_INT pos = INTVAL (XEXP (*op0, 2));
603
604       if (len > 0 && len < modesize
605           && pos >= 0 && pos + len <= modesize
606           && modesize <= HOST_BITS_PER_WIDE_INT)
607         {
608           unsigned HOST_WIDE_INT block;
609           block = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
610           block <<= modesize - pos - len;
611
612           *op0 = gen_rtx_AND (GET_MODE (inner), inner,
613                               gen_int_mode (block, GET_MODE (inner)));
614         }
615     }
616
617   /* Narrow AND of memory against immediate to enable TM.  */
618   if ((*code == EQ || *code == NE)
619       && *op1 == const0_rtx
620       && GET_CODE (*op0) == AND
621       && GET_CODE (XEXP (*op0, 1)) == CONST_INT
622       && SCALAR_INT_MODE_P (GET_MODE (XEXP (*op0, 0))))
623     {
624       rtx inner = XEXP (*op0, 0);
625       rtx mask = XEXP (*op0, 1);
626
627       /* Ignore paradoxical SUBREGs if all extra bits are masked out.  */
628       if (GET_CODE (inner) == SUBREG
629           && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (inner)))
630           && (GET_MODE_SIZE (GET_MODE (inner))
631               >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner))))
632           && ((INTVAL (mask)
633                & GET_MODE_MASK (GET_MODE (inner))
634                & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (inner))))
635               == 0))
636         inner = SUBREG_REG (inner);
637
638       /* Do not change volatile MEMs.  */
639       if (MEM_P (inner) && !MEM_VOLATILE_P (inner))
640         {
641           int part = s390_single_part (XEXP (*op0, 1),
642                                        GET_MODE (inner), QImode, 0);
643           if (part >= 0)
644             {
645               mask = gen_int_mode (s390_extract_part (mask, QImode, 0), QImode);
646               inner = adjust_address_nv (inner, QImode, part);
647               *op0 = gen_rtx_AND (QImode, inner, mask);
648             }
649         }
650     }
651
652   /* Narrow comparisons against 0xffff to HImode if possible.  */
653   if ((*code == EQ || *code == NE)
654       && GET_CODE (*op1) == CONST_INT
655       && INTVAL (*op1) == 0xffff
656       && SCALAR_INT_MODE_P (GET_MODE (*op0))
657       && (nonzero_bits (*op0, GET_MODE (*op0)) 
658           & ~(unsigned HOST_WIDE_INT) 0xffff) == 0)
659     {
660       *op0 = gen_lowpart (HImode, *op0);
661       *op1 = constm1_rtx;
662     }
663
664
665   /* Remove redundant UNSPEC_CMPINT conversions if possible.  */
666   if (GET_CODE (*op0) == UNSPEC
667       && XINT (*op0, 1) == UNSPEC_CMPINT
668       && XVECLEN (*op0, 0) == 1
669       && GET_MODE (XVECEXP (*op0, 0, 0)) == CCUmode
670       && GET_CODE (XVECEXP (*op0, 0, 0)) == REG
671       && REGNO (XVECEXP (*op0, 0, 0)) == CC_REGNUM
672       && *op1 == const0_rtx)
673     {
674       enum rtx_code new_code = UNKNOWN;
675       switch (*code)
676         {
677           case EQ: new_code = EQ;  break;
678           case NE: new_code = NE;  break;
679           case LT: new_code = GTU; break;
680           case GT: new_code = LTU; break;
681           case LE: new_code = GEU; break;
682           case GE: new_code = LEU; break;
683           default: break;
684         }
685
686       if (new_code != UNKNOWN)
687         {
688           *op0 = XVECEXP (*op0, 0, 0);
689           *code = new_code;
690         }
691     }
692
693   /* Simplify cascaded EQ, NE with const0_rtx.  */
694   if ((*code == NE || *code == EQ)
695       && (GET_CODE (*op0) == EQ || GET_CODE (*op0) == NE)
696       && GET_MODE (*op0) == SImode
697       && GET_MODE (XEXP (*op0, 0)) == CCZ1mode
698       && REG_P (XEXP (*op0, 0))
699       && XEXP (*op0, 1) == const0_rtx
700       && *op1 == const0_rtx)
701     {
702       if ((*code == EQ && GET_CODE (*op0) == NE)
703           || (*code == NE && GET_CODE (*op0) == EQ))
704         *code = EQ;
705       else
706         *code = NE;
707       *op0 = XEXP (*op0, 0);
708     }
709
710   /* Prefer register over memory as first operand.  */
711   if (MEM_P (*op0) && REG_P (*op1))
712     {
713       rtx tem = *op0; *op0 = *op1; *op1 = tem;
714       *code = swap_condition (*code);
715     }
716 }
717
718 /* Emit a compare instruction suitable to implement the comparison
719    OP0 CODE OP1.  Return the correct condition RTL to be placed in
720    the IF_THEN_ELSE of the conditional branch testing the result.  */
721
722 rtx
723 s390_emit_compare (enum rtx_code code, rtx op0, rtx op1)
724 {
725   enum machine_mode mode = s390_select_ccmode (code, op0, op1);
726   rtx ret = NULL_RTX;
727
728   /* Do not output a redundant compare instruction if a compare_and_swap
729      pattern already computed the result and the machine modes are compatible.  */
730   if (s390_compare_emitted 
731       && (s390_cc_modes_compatible (GET_MODE (s390_compare_emitted), mode)
732           == GET_MODE (s390_compare_emitted)))
733     ret = gen_rtx_fmt_ee (code, VOIDmode, s390_compare_emitted, const0_rtx); 
734   else
735     {
736       rtx cc = gen_rtx_REG (mode, CC_REGNUM);
737       
738       emit_insn (gen_rtx_SET (VOIDmode, cc, gen_rtx_COMPARE (mode, op0, op1)));
739       ret = gen_rtx_fmt_ee (code, VOIDmode, cc, const0_rtx); 
740     }
741   s390_compare_emitted = NULL_RTX;
742   return ret;
743 }
744
745 /* Emit a jump instruction to TARGET.  If COND is NULL_RTX, emit an
746    unconditional jump, else a conditional jump under condition COND.  */
747
748 void
749 s390_emit_jump (rtx target, rtx cond)
750 {
751   rtx insn;
752
753   target = gen_rtx_LABEL_REF (VOIDmode, target);
754   if (cond)
755     target = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, target, pc_rtx);
756
757   insn = gen_rtx_SET (VOIDmode, pc_rtx, target);
758   emit_jump_insn (insn);
759 }
760
761 /* Return branch condition mask to implement a branch
762    specified by CODE.  Return -1 for invalid comparisons.  */
763
764 int
765 s390_branch_condition_mask (rtx code)
766 {
767   const int CC0 = 1 << 3;
768   const int CC1 = 1 << 2;
769   const int CC2 = 1 << 1;
770   const int CC3 = 1 << 0;
771
772   gcc_assert (GET_CODE (XEXP (code, 0)) == REG);
773   gcc_assert (REGNO (XEXP (code, 0)) == CC_REGNUM);
774   gcc_assert (XEXP (code, 1) == const0_rtx);
775
776   switch (GET_MODE (XEXP (code, 0)))
777     {
778     case CCZmode:
779     case CCZ1mode:
780       switch (GET_CODE (code))
781         {
782         case EQ:        return CC0;
783         case NE:        return CC1 | CC2 | CC3;
784         default:        return -1;
785         }
786       break;
787
788     case CCT1mode:
789       switch (GET_CODE (code))
790         {
791         case EQ:        return CC1;
792         case NE:        return CC0 | CC2 | CC3;
793         default:        return -1;
794         }
795       break;
796
797     case CCT2mode:
798       switch (GET_CODE (code))
799         {
800         case EQ:        return CC2;
801         case NE:        return CC0 | CC1 | CC3;
802         default:        return -1;
803         }
804       break;
805
806     case CCT3mode:
807       switch (GET_CODE (code))
808         {
809         case EQ:        return CC3;
810         case NE:        return CC0 | CC1 | CC2;
811         default:        return -1;
812         }
813       break;
814
815     case CCLmode:
816       switch (GET_CODE (code))
817         {
818         case EQ:        return CC0 | CC2;
819         case NE:        return CC1 | CC3;
820         default:        return -1;
821         }
822       break;
823
824     case CCL1mode:
825       switch (GET_CODE (code))
826         {
827         case LTU:       return CC2 | CC3;  /* carry */
828         case GEU:       return CC0 | CC1;  /* no carry */
829         default:        return -1;
830         }
831       break;
832
833     case CCL2mode:
834       switch (GET_CODE (code))
835         {
836         case GTU:       return CC0 | CC1;  /* borrow */
837         case LEU:       return CC2 | CC3;  /* no borrow */
838         default:        return -1;
839         }
840       break;
841
842     case CCL3mode:
843       switch (GET_CODE (code))
844         {
845         case EQ:        return CC0 | CC2;
846         case NE:        return CC1 | CC3;
847         case LTU:       return CC1;
848         case GTU:       return CC3;
849         case LEU:       return CC1 | CC2;
850         case GEU:       return CC2 | CC3;
851         default:        return -1;
852         }
853
854     case CCUmode:
855       switch (GET_CODE (code))
856         {
857         case EQ:        return CC0;
858         case NE:        return CC1 | CC2 | CC3;
859         case LTU:       return CC1;
860         case GTU:       return CC2;
861         case LEU:       return CC0 | CC1;
862         case GEU:       return CC0 | CC2;
863         default:        return -1;
864         }
865       break;
866
867     case CCURmode:
868       switch (GET_CODE (code))
869         {
870         case EQ:        return CC0;
871         case NE:        return CC2 | CC1 | CC3;
872         case LTU:       return CC2;
873         case GTU:       return CC1;
874         case LEU:       return CC0 | CC2;
875         case GEU:       return CC0 | CC1;
876         default:        return -1;
877         }
878       break;
879
880     case CCAPmode:
881       switch (GET_CODE (code))
882         {
883         case EQ:        return CC0;
884         case NE:        return CC1 | CC2 | CC3;
885         case LT:        return CC1 | CC3;
886         case GT:        return CC2;
887         case LE:        return CC0 | CC1 | CC3;
888         case GE:        return CC0 | CC2;
889         default:        return -1;
890         }
891       break;
892
893     case CCANmode:
894       switch (GET_CODE (code))
895         {
896         case EQ:        return CC0;
897         case NE:        return CC1 | CC2 | CC3;
898         case LT:        return CC1;
899         case GT:        return CC2 | CC3;
900         case LE:        return CC0 | CC1;
901         case GE:        return CC0 | CC2 | CC3;
902         default:        return -1;
903         }
904       break;
905
906     case CCSmode:
907       switch (GET_CODE (code))
908         {
909         case EQ:        return CC0;
910         case NE:        return CC1 | CC2 | CC3;
911         case LT:        return CC1;
912         case GT:        return CC2;
913         case LE:        return CC0 | CC1;
914         case GE:        return CC0 | CC2;
915         case UNORDERED: return CC3;
916         case ORDERED:   return CC0 | CC1 | CC2;
917         case UNEQ:      return CC0 | CC3;
918         case UNLT:      return CC1 | CC3;
919         case UNGT:      return CC2 | CC3;
920         case UNLE:      return CC0 | CC1 | CC3;
921         case UNGE:      return CC0 | CC2 | CC3;
922         case LTGT:      return CC1 | CC2;
923         default:        return -1;
924         }
925       break;
926
927     case CCSRmode:
928       switch (GET_CODE (code))
929         {
930         case EQ:        return CC0;
931         case NE:        return CC2 | CC1 | CC3;
932         case LT:        return CC2;
933         case GT:        return CC1;
934         case LE:        return CC0 | CC2;
935         case GE:        return CC0 | CC1;
936         case UNORDERED: return CC3;
937         case ORDERED:   return CC0 | CC2 | CC1;
938         case UNEQ:      return CC0 | CC3;
939         case UNLT:      return CC2 | CC3;
940         case UNGT:      return CC1 | CC3;
941         case UNLE:      return CC0 | CC2 | CC3;
942         case UNGE:      return CC0 | CC1 | CC3;
943         case LTGT:      return CC2 | CC1;
944         default:        return -1;
945         }
946       break;
947
948     default:
949       return -1;
950     }
951 }
952
953 /* If INV is false, return assembler mnemonic string to implement
954    a branch specified by CODE.  If INV is true, return mnemonic
955    for the corresponding inverted branch.  */
956
957 static const char *
958 s390_branch_condition_mnemonic (rtx code, int inv)
959 {
960   static const char *const mnemonic[16] =
961     {
962       NULL, "o", "h", "nle",
963       "l", "nhe", "lh", "ne",
964       "e", "nlh", "he", "nl",
965       "le", "nh", "no", NULL
966     };
967
968   int mask = s390_branch_condition_mask (code);
969   gcc_assert (mask >= 0);
970
971   if (inv)
972     mask ^= 15;
973
974   gcc_assert (mask >= 1 && mask <= 14);
975
976   return mnemonic[mask];
977 }
978
979 /* Return the part of op which has a value different from def.
980    The size of the part is determined by mode.
981    Use this function only if you already know that op really
982    contains such a part.  */
983
984 unsigned HOST_WIDE_INT
985 s390_extract_part (rtx op, enum machine_mode mode, int def)
986 {
987   unsigned HOST_WIDE_INT value = 0;
988   int max_parts = HOST_BITS_PER_WIDE_INT / GET_MODE_BITSIZE (mode);
989   int part_bits = GET_MODE_BITSIZE (mode);
990   unsigned HOST_WIDE_INT part_mask
991     = ((unsigned HOST_WIDE_INT)1 << part_bits) - 1;
992   int i;
993
994   for (i = 0; i < max_parts; i++)
995     {
996       if (i == 0)
997         value = (unsigned HOST_WIDE_INT) INTVAL (op);
998       else
999         value >>= part_bits;
1000
1001       if ((value & part_mask) != (def & part_mask))
1002         return value & part_mask;
1003     }
1004
1005   gcc_unreachable ();
1006 }
1007
1008 /* If OP is an integer constant of mode MODE with exactly one
1009    part of mode PART_MODE unequal to DEF, return the number of that
1010    part. Otherwise, return -1.  */
1011
1012 int
1013 s390_single_part (rtx op,
1014                   enum machine_mode mode,
1015                   enum machine_mode part_mode,
1016                   int def)
1017 {
1018   unsigned HOST_WIDE_INT value = 0;
1019   int n_parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (part_mode);
1020   unsigned HOST_WIDE_INT part_mask
1021     = ((unsigned HOST_WIDE_INT)1 << GET_MODE_BITSIZE (part_mode)) - 1;
1022   int i, part = -1;
1023
1024   if (GET_CODE (op) != CONST_INT)
1025     return -1;
1026
1027   for (i = 0; i < n_parts; i++)
1028     {
1029       if (i == 0)
1030         value = (unsigned HOST_WIDE_INT) INTVAL (op);
1031       else
1032         value >>= GET_MODE_BITSIZE (part_mode);
1033
1034       if ((value & part_mask) != (def & part_mask))
1035         {
1036           if (part != -1)
1037             return -1;
1038           else
1039             part = i;
1040         }
1041     }
1042   return part == -1 ? -1 : n_parts - 1 - part;
1043 }
1044
1045 /* Check whether we can (and want to) split a double-word
1046    move in mode MODE from SRC to DST into two single-word
1047    moves, moving the subword FIRST_SUBWORD first.  */
1048
1049 bool
1050 s390_split_ok_p (rtx dst, rtx src, enum machine_mode mode, int first_subword)
1051 {
1052   /* Floating point registers cannot be split.  */
1053   if (FP_REG_P (src) || FP_REG_P (dst))
1054     return false;
1055
1056   /* We don't need to split if operands are directly accessible.  */
1057   if (s_operand (src, mode) || s_operand (dst, mode))
1058     return false;
1059
1060   /* Non-offsettable memory references cannot be split.  */
1061   if ((GET_CODE (src) == MEM && !offsettable_memref_p (src))
1062       || (GET_CODE (dst) == MEM && !offsettable_memref_p (dst)))
1063     return false;
1064
1065   /* Moving the first subword must not clobber a register
1066      needed to move the second subword.  */
1067   if (register_operand (dst, mode))
1068     {
1069       rtx subreg = operand_subword (dst, first_subword, 0, mode);
1070       if (reg_overlap_mentioned_p (subreg, src))
1071         return false;
1072     }
1073
1074   return true;
1075 }
1076
1077 /* Return true if it can be proven that [MEM1, MEM1 + SIZE]
1078    and [MEM2, MEM2 + SIZE] do overlap and false
1079    otherwise.  */
1080
1081 bool
1082 s390_overlap_p (rtx mem1, rtx mem2, HOST_WIDE_INT size)
1083 {
1084   rtx addr1, addr2, addr_delta;
1085   HOST_WIDE_INT delta;
1086
1087   if (GET_CODE (mem1) != MEM || GET_CODE (mem2) != MEM)
1088     return true;
1089
1090   if (size == 0)
1091     return false;
1092
1093   addr1 = XEXP (mem1, 0);
1094   addr2 = XEXP (mem2, 0);
1095
1096   addr_delta = simplify_binary_operation (MINUS, Pmode, addr2, addr1);
1097
1098   /* This overlapping check is used by peepholes merging memory block operations.
1099      Overlapping operations would otherwise be recognized by the S/390 hardware
1100      and would fall back to a slower implementation. Allowing overlapping 
1101      operations would lead to slow code but not to wrong code. Therefore we are
1102      somewhat optimistic if we cannot prove that the memory blocks are 
1103      overlapping.
1104      That's why we return false here although this may accept operations on
1105      overlapping memory areas.  */
1106   if (!addr_delta || GET_CODE (addr_delta) != CONST_INT)
1107     return false;
1108
1109   delta = INTVAL (addr_delta);
1110
1111   if (delta == 0
1112       || (delta > 0 && delta < size)
1113       || (delta < 0 && -delta < size))
1114     return true;
1115
1116   return false;
1117 }
1118
1119 /* Check whether the address of memory reference MEM2 equals exactly
1120    the address of memory reference MEM1 plus DELTA.  Return true if
1121    we can prove this to be the case, false otherwise.  */
1122
1123 bool
1124 s390_offset_p (rtx mem1, rtx mem2, rtx delta)
1125 {
1126   rtx addr1, addr2, addr_delta;
1127
1128   if (GET_CODE (mem1) != MEM || GET_CODE (mem2) != MEM)
1129     return false;
1130
1131   addr1 = XEXP (mem1, 0);
1132   addr2 = XEXP (mem2, 0);
1133
1134   addr_delta = simplify_binary_operation (MINUS, Pmode, addr2, addr1);
1135   if (!addr_delta || !rtx_equal_p (addr_delta, delta))
1136     return false;
1137
1138   return true;
1139 }
1140
1141 /* Expand logical operator CODE in mode MODE with operands OPERANDS.  */
1142
1143 void
1144 s390_expand_logical_operator (enum rtx_code code, enum machine_mode mode,
1145                               rtx *operands)
1146 {
1147   enum machine_mode wmode = mode;
1148   rtx dst = operands[0];
1149   rtx src1 = operands[1];
1150   rtx src2 = operands[2];
1151   rtx op, clob, tem;
1152
1153   /* If we cannot handle the operation directly, use a temp register.  */
1154   if (!s390_logical_operator_ok_p (operands))
1155     dst = gen_reg_rtx (mode);
1156
1157   /* QImode and HImode patterns make sense only if we have a destination
1158      in memory.  Otherwise perform the operation in SImode.  */
1159   if ((mode == QImode || mode == HImode) && GET_CODE (dst) != MEM)
1160     wmode = SImode;
1161
1162   /* Widen operands if required.  */
1163   if (mode != wmode)
1164     {
1165       if (GET_CODE (dst) == SUBREG
1166           && (tem = simplify_subreg (wmode, dst, mode, 0)) != 0)
1167         dst = tem;
1168       else if (REG_P (dst))
1169         dst = gen_rtx_SUBREG (wmode, dst, 0);
1170       else
1171         dst = gen_reg_rtx (wmode);
1172
1173       if (GET_CODE (src1) == SUBREG
1174           && (tem = simplify_subreg (wmode, src1, mode, 0)) != 0)
1175         src1 = tem;
1176       else if (GET_MODE (src1) != VOIDmode)
1177         src1 = gen_rtx_SUBREG (wmode, force_reg (mode, src1), 0);
1178
1179       if (GET_CODE (src2) == SUBREG
1180           && (tem = simplify_subreg (wmode, src2, mode, 0)) != 0)
1181         src2 = tem;
1182       else if (GET_MODE (src2) != VOIDmode)
1183         src2 = gen_rtx_SUBREG (wmode, force_reg (mode, src2), 0);
1184     }
1185
1186   /* Emit the instruction.  */
1187   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, wmode, src1, src2));
1188   clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
1189   emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
1190
1191   /* Fix up the destination if needed.  */
1192   if (dst != operands[0])
1193     emit_move_insn (operands[0], gen_lowpart (mode, dst));
1194 }
1195
1196 /* Check whether OPERANDS are OK for a logical operation (AND, IOR, XOR).  */
1197
1198 bool
1199 s390_logical_operator_ok_p (rtx *operands)
1200 {
1201   /* If the destination operand is in memory, it needs to coincide
1202      with one of the source operands.  After reload, it has to be
1203      the first source operand.  */
1204   if (GET_CODE (operands[0]) == MEM)
1205     return rtx_equal_p (operands[0], operands[1])
1206            || (!reload_completed && rtx_equal_p (operands[0], operands[2]));
1207
1208   return true;
1209 }
1210
1211 /* Narrow logical operation CODE of memory operand MEMOP with immediate
1212    operand IMMOP to switch from SS to SI type instructions.  */
1213
1214 void
1215 s390_narrow_logical_operator (enum rtx_code code, rtx *memop, rtx *immop)
1216 {
1217   int def = code == AND ? -1 : 0;
1218   HOST_WIDE_INT mask;
1219   int part;
1220
1221   gcc_assert (GET_CODE (*memop) == MEM);
1222   gcc_assert (!MEM_VOLATILE_P (*memop));
1223
1224   mask = s390_extract_part (*immop, QImode, def);
1225   part = s390_single_part (*immop, GET_MODE (*memop), QImode, def);
1226   gcc_assert (part >= 0);
1227
1228   *memop = adjust_address (*memop, QImode, part);
1229   *immop = gen_int_mode (mask, QImode);
1230 }
1231
1232
1233 /* How to allocate a 'struct machine_function'.  */
1234
1235 static struct machine_function *
1236 s390_init_machine_status (void)
1237 {
1238   return ggc_alloc_cleared (sizeof (struct machine_function));
1239 }
1240
1241 /* Change optimizations to be performed, depending on the
1242    optimization level.
1243
1244    LEVEL is the optimization level specified; 2 if `-O2' is
1245    specified, 1 if `-O' is specified, and 0 if neither is specified.
1246
1247    SIZE is nonzero if `-Os' is specified and zero otherwise.  */
1248
1249 void
1250 optimization_options (int level ATTRIBUTE_UNUSED, int size ATTRIBUTE_UNUSED)
1251 {
1252   /* ??? There are apparently still problems with -fcaller-saves.  */
1253   flag_caller_saves = 0;
1254
1255   /* By default, always emit DWARF-2 unwind info.  This allows debugging
1256      without maintaining a stack frame back-chain.  */
1257   flag_asynchronous_unwind_tables = 1;
1258
1259   /* Use MVCLE instructions to decrease code size if requested.  */
1260   if (size != 0)
1261     target_flags |= MASK_MVCLE;
1262 }
1263
1264 /* Return true if ARG is the name of a processor.  Set *TYPE and *FLAGS
1265    to the associated processor_type and processor_flags if so.  */
1266
1267 static bool
1268 s390_handle_arch_option (const char *arg,
1269                          enum processor_type *type,
1270                          enum processor_flags *flags)
1271 {
1272   static struct pta
1273     {
1274       const char *const name;           /* processor name or nickname.  */
1275       const enum processor_type processor;
1276       const enum processor_flags flags;
1277     }
1278   const processor_alias_table[] =
1279     {
1280       {"g5", PROCESSOR_9672_G5, PF_IEEE_FLOAT},
1281       {"g6", PROCESSOR_9672_G6, PF_IEEE_FLOAT},
1282       {"z900", PROCESSOR_2064_Z900, PF_IEEE_FLOAT | PF_ZARCH},
1283       {"z990", PROCESSOR_2084_Z990, PF_IEEE_FLOAT | PF_ZARCH
1284                                     | PF_LONG_DISPLACEMENT},
1285       {"z9-109", PROCESSOR_2094_Z9_109, PF_IEEE_FLOAT | PF_ZARCH
1286                                        | PF_LONG_DISPLACEMENT | PF_EXTIMM},
1287     };
1288   size_t i;
1289
1290   for (i = 0; i < ARRAY_SIZE (processor_alias_table); i++)
1291     if (strcmp (arg, processor_alias_table[i].name) == 0)
1292       {
1293         *type = processor_alias_table[i].processor;
1294         *flags = processor_alias_table[i].flags;
1295         return true;
1296       }
1297   return false;
1298 }
1299
1300 /* Implement TARGET_HANDLE_OPTION.  */
1301
1302 static bool
1303 s390_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
1304 {
1305   switch (code)
1306     {
1307     case OPT_march_:
1308       return s390_handle_arch_option (arg, &s390_arch, &s390_arch_flags);
1309
1310     case OPT_mstack_guard_:
1311       if (sscanf (arg, HOST_WIDE_INT_PRINT_DEC, &s390_stack_guard) != 1)
1312         return false;
1313       if (exact_log2 (s390_stack_guard) == -1)
1314         error ("stack guard value must be an exact power of 2");
1315       return true;
1316
1317     case OPT_mstack_size_:
1318       if (sscanf (arg, HOST_WIDE_INT_PRINT_DEC, &s390_stack_size) != 1)
1319         return false;
1320       if (exact_log2 (s390_stack_size) == -1)
1321         error ("stack size must be an exact power of 2");
1322       return true;
1323
1324     case OPT_mtune_:
1325       return s390_handle_arch_option (arg, &s390_tune, &s390_tune_flags);
1326
1327     case OPT_mwarn_framesize_:
1328       return sscanf (arg, HOST_WIDE_INT_PRINT_DEC, &s390_warn_framesize) == 1;
1329
1330     default:
1331       return true;
1332     }
1333 }
1334
1335 void
1336 override_options (void)
1337 {
1338   /* Set up function hooks.  */
1339   init_machine_status = s390_init_machine_status;
1340
1341   /* Architecture mode defaults according to ABI.  */
1342   if (!(target_flags_explicit & MASK_ZARCH))
1343     {
1344       if (TARGET_64BIT)
1345         target_flags |= MASK_ZARCH;
1346       else
1347         target_flags &= ~MASK_ZARCH;
1348     }
1349
1350   /* Determine processor architectural level.  */
1351   if (!s390_arch_string)
1352     {
1353       s390_arch_string = TARGET_ZARCH? "z900" : "g5";
1354       s390_handle_arch_option (s390_arch_string, &s390_arch, &s390_arch_flags);
1355     }
1356
1357   /* Determine processor to tune for.  */
1358   if (s390_tune == PROCESSOR_max)
1359     {
1360       s390_tune = s390_arch;
1361       s390_tune_flags = s390_arch_flags;
1362     }
1363
1364   /* Sanity checks.  */
1365   if (TARGET_ZARCH && !(s390_arch_flags & PF_ZARCH))
1366     error ("z/Architecture mode not supported on %s", s390_arch_string);
1367   if (TARGET_64BIT && !TARGET_ZARCH)
1368     error ("64-bit ABI not supported in ESA/390 mode");
1369
1370   /* Set processor cost function.  */
1371   if (s390_tune == PROCESSOR_2094_Z9_109)
1372     s390_cost = &z9_109_cost;
1373   else if (s390_tune == PROCESSOR_2084_Z990)
1374     s390_cost = &z990_cost;
1375   else
1376     s390_cost = &z900_cost;
1377   
1378   if (TARGET_BACKCHAIN && TARGET_PACKED_STACK && TARGET_HARD_FLOAT)
1379     error ("-mbackchain -mpacked-stack -mhard-float are not supported "
1380            "in combination");
1381
1382   if (s390_stack_size)
1383     {
1384       if (!s390_stack_guard)
1385         error ("-mstack-size implies use of -mstack-guard");
1386       else if (s390_stack_guard >= s390_stack_size)
1387         error ("stack size must be greater than the stack guard value");
1388       else if (s390_stack_size > 1 << 16)
1389         error ("stack size must not be greater than 64k");
1390     }
1391   else if (s390_stack_guard)
1392     error ("-mstack-guard implies use of -mstack-size"); 
1393 }
1394
1395 /* Map for smallest class containing reg regno.  */
1396
1397 const enum reg_class regclass_map[FIRST_PSEUDO_REGISTER] =
1398 { GENERAL_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
1399   ADDR_REGS,    ADDR_REGS, ADDR_REGS, ADDR_REGS,
1400   ADDR_REGS,    ADDR_REGS, ADDR_REGS, ADDR_REGS,
1401   ADDR_REGS,    ADDR_REGS, ADDR_REGS, ADDR_REGS,
1402   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,
1403   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,
1404   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,
1405   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,
1406   ADDR_REGS,    CC_REGS,   ADDR_REGS, ADDR_REGS,
1407   ACCESS_REGS,  ACCESS_REGS
1408 };
1409
1410 /* Return attribute type of insn.  */
1411
1412 static enum attr_type
1413 s390_safe_attr_type (rtx insn)
1414 {
1415   if (recog_memoized (insn) >= 0)
1416     return get_attr_type (insn);
1417   else
1418     return TYPE_NONE;
1419 }
1420
1421 /* Return true if DISP is a valid short displacement.  */
1422
1423 static bool
1424 s390_short_displacement (rtx disp)
1425 {
1426   /* No displacement is OK.  */
1427   if (!disp)
1428     return true;
1429
1430   /* Integer displacement in range.  */
1431   if (GET_CODE (disp) == CONST_INT)
1432     return INTVAL (disp) >= 0 && INTVAL (disp) < 4096;
1433
1434   /* GOT offset is not OK, the GOT can be large.  */
1435   if (GET_CODE (disp) == CONST
1436       && GET_CODE (XEXP (disp, 0)) == UNSPEC
1437       && (XINT (XEXP (disp, 0), 1) == UNSPEC_GOT
1438           || XINT (XEXP (disp, 0), 1) == UNSPEC_GOTNTPOFF))
1439     return false;
1440
1441   /* All other symbolic constants are literal pool references,
1442      which are OK as the literal pool must be small.  */
1443   if (GET_CODE (disp) == CONST)
1444     return true;
1445
1446   return false;
1447 }
1448
1449 /* Decompose a RTL expression ADDR for a memory address into
1450    its components, returned in OUT.
1451
1452    Returns false if ADDR is not a valid memory address, true
1453    otherwise.  If OUT is NULL, don't return the components,
1454    but check for validity only.
1455
1456    Note: Only addresses in canonical form are recognized.
1457    LEGITIMIZE_ADDRESS should convert non-canonical forms to the
1458    canonical form so that they will be recognized.  */
1459
1460 static int
1461 s390_decompose_address (rtx addr, struct s390_address *out)
1462 {
1463   HOST_WIDE_INT offset = 0;
1464   rtx base = NULL_RTX;
1465   rtx indx = NULL_RTX;
1466   rtx disp = NULL_RTX;
1467   rtx orig_disp;
1468   bool pointer = false;
1469   bool base_ptr = false;
1470   bool indx_ptr = false;
1471
1472   /* Decompose address into base + index + displacement.  */
1473
1474   if (GET_CODE (addr) == REG || GET_CODE (addr) == UNSPEC)
1475     base = addr;
1476
1477   else if (GET_CODE (addr) == PLUS)
1478     {
1479       rtx op0 = XEXP (addr, 0);
1480       rtx op1 = XEXP (addr, 1);
1481       enum rtx_code code0 = GET_CODE (op0);
1482       enum rtx_code code1 = GET_CODE (op1);
1483
1484       if (code0 == REG || code0 == UNSPEC)
1485         {
1486           if (code1 == REG || code1 == UNSPEC)
1487             {
1488               indx = op0;       /* index + base */
1489               base = op1;
1490             }
1491
1492           else
1493             {
1494               base = op0;       /* base + displacement */
1495               disp = op1;
1496             }
1497         }
1498
1499       else if (code0 == PLUS)
1500         {
1501           indx = XEXP (op0, 0); /* index + base + disp */
1502           base = XEXP (op0, 1);
1503           disp = op1;
1504         }
1505
1506       else
1507         {
1508           return false;
1509         }
1510     }
1511
1512   else
1513     disp = addr;                /* displacement */
1514
1515   /* Extract integer part of displacement.  */
1516   orig_disp = disp;
1517   if (disp)
1518     {
1519       if (GET_CODE (disp) == CONST_INT)
1520         {
1521           offset = INTVAL (disp);
1522           disp = NULL_RTX;
1523         }
1524       else if (GET_CODE (disp) == CONST
1525                && GET_CODE (XEXP (disp, 0)) == PLUS
1526                && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
1527         {
1528           offset = INTVAL (XEXP (XEXP (disp, 0), 1));
1529           disp = XEXP (XEXP (disp, 0), 0);
1530         }
1531     }
1532
1533   /* Strip off CONST here to avoid special case tests later.  */
1534   if (disp && GET_CODE (disp) == CONST)
1535     disp = XEXP (disp, 0);
1536
1537   /* We can convert literal pool addresses to
1538      displacements by basing them off the base register.  */
1539   if (disp && GET_CODE (disp) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (disp))
1540     {
1541       /* Either base or index must be free to hold the base register.  */
1542       if (!base)
1543         base = gen_rtx_REG (Pmode, BASE_REGNUM);
1544       else if (!indx)
1545         indx = gen_rtx_REG (Pmode, BASE_REGNUM);
1546       else
1547         return false;
1548
1549       /* Mark up the displacement.  */
1550       disp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, disp),
1551                              UNSPEC_LTREL_OFFSET);
1552     }
1553
1554   /* Validate base register.  */
1555   if (base)
1556     {
1557       if (GET_CODE (base) == UNSPEC)
1558         switch (XINT (base, 1))
1559           {
1560           case UNSPEC_LTREF:
1561             if (!disp)
1562               disp = gen_rtx_UNSPEC (Pmode, 
1563                                      gen_rtvec (1, XVECEXP (base, 0, 0)),
1564                                      UNSPEC_LTREL_OFFSET);
1565             else
1566               return false;
1567
1568             base = gen_rtx_REG (Pmode, BASE_REGNUM);
1569             break;
1570
1571           case UNSPEC_LTREL_BASE:
1572             base = gen_rtx_REG (Pmode, BASE_REGNUM);
1573             break;
1574
1575           default:
1576             return false;
1577           }
1578
1579       if (GET_CODE (base) != REG || GET_MODE (base) != Pmode)
1580         return false;
1581
1582       if (REGNO (base) == BASE_REGNUM
1583           || REGNO (base) == STACK_POINTER_REGNUM
1584           || REGNO (base) == FRAME_POINTER_REGNUM
1585           || ((reload_completed || reload_in_progress)
1586               && frame_pointer_needed
1587               && REGNO (base) == HARD_FRAME_POINTER_REGNUM)
1588           || REGNO (base) == ARG_POINTER_REGNUM
1589           || (flag_pic
1590               && REGNO (base) == PIC_OFFSET_TABLE_REGNUM))
1591         pointer = base_ptr = true;
1592     }
1593
1594   /* Validate index register.  */
1595   if (indx)
1596     {
1597       if (GET_CODE (indx) == UNSPEC)
1598         switch (XINT (indx, 1))
1599           {
1600           case UNSPEC_LTREF:
1601             if (!disp)
1602               disp = gen_rtx_UNSPEC (Pmode, 
1603                                      gen_rtvec (1, XVECEXP (indx, 0, 0)),
1604                                      UNSPEC_LTREL_OFFSET);
1605             else
1606               return false;
1607
1608             indx = gen_rtx_REG (Pmode, BASE_REGNUM);
1609             break;
1610
1611           case UNSPEC_LTREL_BASE:
1612             indx = gen_rtx_REG (Pmode, BASE_REGNUM);
1613             break;
1614
1615           default:
1616             return false;
1617           }
1618
1619       if (GET_CODE (indx) != REG || GET_MODE (indx) != Pmode)
1620         return false;
1621
1622       if (REGNO (indx) == BASE_REGNUM
1623           || REGNO (indx) == STACK_POINTER_REGNUM
1624           || REGNO (indx) == FRAME_POINTER_REGNUM
1625           || ((reload_completed || reload_in_progress)
1626               && frame_pointer_needed
1627               && REGNO (indx) == HARD_FRAME_POINTER_REGNUM)
1628           || REGNO (indx) == ARG_POINTER_REGNUM
1629           || (flag_pic
1630               && REGNO (indx) == PIC_OFFSET_TABLE_REGNUM))
1631         pointer = indx_ptr = true;
1632     }
1633
1634   /* Prefer to use pointer as base, not index.  */
1635   if (base && indx && !base_ptr
1636       && (indx_ptr || (!REG_POINTER (base) && REG_POINTER (indx))))
1637     {
1638       rtx tmp = base;
1639       base = indx;
1640       indx = tmp;
1641     }
1642
1643   /* Validate displacement.  */
1644   if (!disp)
1645     {
1646       /* If virtual registers are involved, the displacement will change later 
1647          anyway as the virtual registers get eliminated.  This could make a 
1648          valid displacement invalid, but it is more likely to make an invalid 
1649          displacement valid, because we sometimes access the register save area 
1650          via negative offsets to one of those registers.
1651          Thus we don't check the displacement for validity here.  If after
1652          elimination the displacement turns out to be invalid after all,
1653          this is fixed up by reload in any case.  */
1654       if (base != arg_pointer_rtx 
1655           && indx != arg_pointer_rtx 
1656           && base != return_address_pointer_rtx 
1657           && indx != return_address_pointer_rtx
1658           && base != frame_pointer_rtx 
1659           && indx != frame_pointer_rtx
1660           && base != virtual_stack_vars_rtx 
1661           && indx != virtual_stack_vars_rtx)
1662         if (!DISP_IN_RANGE (offset))
1663           return false;
1664     }
1665   else
1666     {
1667       /* All the special cases are pointers.  */
1668       pointer = true;
1669
1670       /* In the small-PIC case, the linker converts @GOT
1671          and @GOTNTPOFF offsets to possible displacements.  */
1672       if (GET_CODE (disp) == UNSPEC
1673           && (XINT (disp, 1) == UNSPEC_GOT
1674               || XINT (disp, 1) == UNSPEC_GOTNTPOFF)
1675           && offset == 0
1676           && flag_pic == 1)
1677         {
1678           ;
1679         }
1680
1681       /* Accept chunkified literal pool symbol references.  */
1682       else if (GET_CODE (disp) == MINUS
1683                && GET_CODE (XEXP (disp, 0)) == LABEL_REF
1684                && GET_CODE (XEXP (disp, 1)) == LABEL_REF)
1685         {
1686           ;
1687         }
1688
1689       /* Accept literal pool references.  */
1690       else if (GET_CODE (disp) == UNSPEC
1691                && XINT (disp, 1) == UNSPEC_LTREL_OFFSET)
1692         {
1693           orig_disp = gen_rtx_CONST (Pmode, disp);
1694           if (offset)
1695             {
1696               /* If we have an offset, make sure it does not
1697                  exceed the size of the constant pool entry.  */
1698               rtx sym = XVECEXP (disp, 0, 0);
1699               if (offset >= GET_MODE_SIZE (get_pool_mode (sym)))
1700                 return false;
1701
1702               orig_disp = plus_constant (orig_disp, offset);
1703             }
1704         }
1705
1706       else
1707         return false;
1708     }
1709
1710   if (!base && !indx)
1711     pointer = true;
1712
1713   if (out)
1714     {
1715       out->base = base;
1716       out->indx = indx;
1717       out->disp = orig_disp;
1718       out->pointer = pointer;
1719     }
1720
1721   return true;
1722 }
1723
1724 /* Return true if CODE is a valid address without index.  */
1725
1726 bool
1727 s390_legitimate_address_without_index_p (rtx op)
1728 {
1729   struct s390_address addr;
1730
1731   if (!s390_decompose_address (XEXP (op, 0), &addr))
1732     return false;
1733   if (addr.indx)
1734     return false;
1735
1736   return true;
1737 }
1738
1739 /* Return 1 if OP is a valid operand for a C constraint, 0 else.  */
1740
1741 int
1742 s390_extra_constraint_str (rtx op, int c, const char * str)
1743 {
1744   struct s390_address addr;
1745
1746   gcc_assert (c == str[0]);
1747
1748   /* Check for offsettable variants of memory constraints.  */
1749   if (c == 'A')
1750     {
1751       /* Only accept non-volatile MEMs.  */
1752       if (!MEM_P (op) || MEM_VOLATILE_P (op))
1753         return 0;
1754
1755       if ((reload_completed || reload_in_progress)
1756           ? !offsettable_memref_p (op)
1757           : !offsettable_nonstrict_memref_p (op))
1758         return 0;
1759
1760       c = str[1];
1761     }
1762
1763   /* Check for non-literal-pool variants of memory constraints.  */
1764   else if (c == 'B')
1765     {
1766       if (GET_CODE (op) != MEM)
1767         return 0;
1768       if (!s390_decompose_address (XEXP (op, 0), &addr))
1769         return 0;
1770       if (addr.base && REG_P (addr.base) && REGNO (addr.base) == BASE_REGNUM)
1771         return 0;
1772       if (addr.indx && REG_P (addr.indx) && REGNO (addr.indx) == BASE_REGNUM)
1773         return 0;
1774
1775       c = str[1];
1776     }
1777
1778   switch (c)
1779     {
1780     case 'Q':
1781       if (GET_CODE (op) != MEM)
1782         return 0;
1783       if (!s390_decompose_address (XEXP (op, 0), &addr))
1784         return 0;
1785       if (addr.indx)
1786         return 0;
1787
1788       if (TARGET_LONG_DISPLACEMENT)
1789         {
1790           if (!s390_short_displacement (addr.disp))
1791             return 0;
1792         }
1793       break;
1794
1795     case 'R':
1796       if (GET_CODE (op) != MEM)
1797         return 0;
1798
1799       if (TARGET_LONG_DISPLACEMENT)
1800         {
1801           if (!s390_decompose_address (XEXP (op, 0), &addr))
1802             return 0;
1803           if (!s390_short_displacement (addr.disp))
1804             return 0;
1805         }
1806       break;
1807
1808     case 'S':
1809       if (!TARGET_LONG_DISPLACEMENT)
1810         return 0;
1811       if (GET_CODE (op) != MEM)
1812         return 0;
1813       if (!s390_decompose_address (XEXP (op, 0), &addr))
1814         return 0;
1815       if (addr.indx)
1816         return 0;
1817       if (s390_short_displacement (addr.disp))
1818         return 0;
1819       break;
1820
1821     case 'T':
1822       if (!TARGET_LONG_DISPLACEMENT)
1823         return 0;
1824       if (GET_CODE (op) != MEM)
1825         return 0;
1826       /* Any invalid address here will be fixed up by reload,
1827          so accept it for the most generic constraint.  */
1828       if (s390_decompose_address (XEXP (op, 0), &addr)
1829           && s390_short_displacement (addr.disp))
1830         return 0;
1831       break;
1832
1833     case 'U':
1834       if (TARGET_LONG_DISPLACEMENT)
1835         {
1836           if (!s390_decompose_address (op, &addr))
1837             return 0;
1838           if (!s390_short_displacement (addr.disp))
1839             return 0;
1840         }
1841       break;
1842
1843     case 'W':
1844       if (!TARGET_LONG_DISPLACEMENT)
1845         return 0;
1846       /* Any invalid address here will be fixed up by reload,
1847          so accept it for the most generic constraint.  */
1848       if (s390_decompose_address (op, &addr)
1849           && s390_short_displacement (addr.disp))
1850         return 0;
1851       break;
1852
1853     case 'Y':
1854       return shift_count_operand (op, VOIDmode);
1855
1856     default:
1857       return 0;
1858     }
1859
1860   return 1;
1861 }
1862
1863 /* Return true if VALUE matches the constraint STR.  */
1864
1865 int
1866 s390_const_double_ok_for_constraint_p (rtx value,
1867                                        int c,
1868                                        const char * str)
1869 {
1870   gcc_assert (c == str[0]);
1871
1872   switch (str[0])
1873     {
1874     case 'G':
1875       /* The floating point zero constant.  */
1876       return (GET_MODE_CLASS (GET_MODE (value)) == MODE_FLOAT
1877               && value == CONST0_RTX (GET_MODE (value)));
1878       
1879     default:
1880       return 0;
1881     }
1882 }
1883
1884 /* Return true if VALUE matches the constraint STR.  */
1885
1886 int
1887 s390_const_ok_for_constraint_p (HOST_WIDE_INT value,
1888                                 int c,
1889                                 const char * str)
1890 {
1891   enum machine_mode mode, part_mode;
1892   int def;
1893   int part, part_goal;
1894
1895   gcc_assert (c == str[0]);
1896
1897   switch (str[0])
1898     {
1899     case 'I':
1900       return (unsigned int)value < 256;
1901
1902     case 'J':
1903       return (unsigned int)value < 4096;
1904
1905     case 'K':
1906       return value >= -32768 && value < 32768;
1907
1908     case 'L':
1909       return (TARGET_LONG_DISPLACEMENT ?
1910               (value >= -524288 && value <= 524287)
1911               : (value >= 0 && value <= 4095));
1912     case 'M':
1913       return value == 2147483647;
1914
1915     case 'N':
1916       if (str[1] == 'x')
1917         part_goal = -1;
1918       else
1919         part_goal = str[1] - '0';
1920
1921       switch (str[2])
1922         {
1923         case 'Q': part_mode = QImode; break;
1924         case 'H': part_mode = HImode; break;
1925         case 'S': part_mode = SImode; break;
1926         default:  return 0;
1927         }
1928
1929       switch (str[3])
1930         {
1931         case 'H': mode = HImode; break;
1932         case 'S': mode = SImode; break;
1933         case 'D': mode = DImode; break;
1934         default: return 0;
1935         }
1936
1937       switch (str[4])
1938         {
1939         case '0': def = 0;  break;
1940         case 'F': def = -1; break;
1941         default: return 0;
1942         }
1943
1944       if (GET_MODE_SIZE (mode) <= GET_MODE_SIZE (part_mode))
1945         return 0;
1946
1947       part = s390_single_part (GEN_INT (value), mode, part_mode, def);
1948       if (part < 0)
1949         return 0;
1950       if (part_goal != -1 && part_goal != part)
1951         return 0;
1952
1953       break;
1954
1955     case 'O':
1956       if (!TARGET_EXTIMM)
1957         return 0;
1958       
1959       switch (str[1])
1960         {
1961         case 's':
1962           return trunc_int_for_mode (value, SImode) == value;
1963           
1964         case 'p':
1965           return value == 0
1966             || s390_single_part (GEN_INT (value), DImode, SImode, 0) == 1;
1967           
1968         case 'n':
1969           return value == -1
1970             || s390_single_part (GEN_INT (value), DImode, SImode, -1) == 1;
1971           
1972         default:
1973           gcc_unreachable ();
1974         }
1975       break;
1976
1977     case 'P':
1978       return legitimate_reload_constant_p (GEN_INT (value));
1979
1980     default:
1981       return 0;
1982     }
1983
1984   return 1;
1985 }
1986
1987 /* Compute a (partial) cost for rtx X.  Return true if the complete
1988    cost has been computed, and false if subexpressions should be
1989    scanned.  In either case, *TOTAL contains the cost result.  
1990    CODE contains GET_CODE (x), OUTER_CODE contains the code 
1991    of the superexpression of x.  */
1992
1993 static bool
1994 s390_rtx_costs (rtx x, int code, int outer_code, int *total)
1995 {
1996   switch (code)
1997     {
1998     case CONST:
1999     case CONST_INT:
2000     case LABEL_REF:
2001     case SYMBOL_REF:
2002     case CONST_DOUBLE:
2003     case MEM:
2004       *total = 0;
2005       return true;
2006
2007     case ASHIFT:
2008     case ASHIFTRT:
2009     case LSHIFTRT:
2010     case ROTATE:
2011     case ROTATERT:
2012     case AND:
2013     case IOR:
2014     case XOR:
2015     case NEG:
2016     case NOT:
2017       *total = COSTS_N_INSNS (1);
2018       return false;
2019
2020     case PLUS:
2021     case MINUS:
2022       /* Check for multiply and add.  */
2023       if ((GET_MODE (x) == DFmode || GET_MODE (x) == SFmode)
2024           && GET_CODE (XEXP (x, 0)) == MULT
2025           && TARGET_HARD_FLOAT && TARGET_IEEE_FLOAT && TARGET_FUSED_MADD)
2026         {
2027           /* This is the multiply and add case.  */
2028           if (GET_MODE (x) == DFmode)
2029             *total = s390_cost->madbr;
2030           else
2031             *total = s390_cost->maebr;
2032           *total += rtx_cost (XEXP (XEXP (x, 0), 0), MULT) 
2033             + rtx_cost (XEXP (XEXP (x, 0), 1), MULT) 
2034             + rtx_cost (XEXP (x, 1), code);
2035           return true;  /* Do not do an additional recursive descent.  */
2036         }
2037       *total = COSTS_N_INSNS (1);
2038       return false;
2039
2040     case MULT:      
2041       switch (GET_MODE (x))
2042         {
2043         case SImode:
2044           {
2045             rtx left = XEXP (x, 0);
2046             rtx right = XEXP (x, 1);
2047             if (GET_CODE (right) == CONST_INT
2048                 && CONST_OK_FOR_K (INTVAL (right)))
2049               *total = s390_cost->mhi;
2050             else if (GET_CODE (left) == SIGN_EXTEND)
2051               *total = s390_cost->mh;
2052             else
2053               *total = s390_cost->ms;  /* msr, ms, msy */
2054             break;
2055           }
2056         case DImode:
2057           {
2058             rtx left = XEXP (x, 0);
2059             rtx right = XEXP (x, 1);
2060             if (TARGET_64BIT)
2061               {
2062                 if (GET_CODE (right) == CONST_INT
2063                     && CONST_OK_FOR_K (INTVAL (right)))
2064                   *total = s390_cost->mghi;
2065                 else if (GET_CODE (left) == SIGN_EXTEND)
2066                   *total = s390_cost->msgf;
2067                 else
2068                   *total = s390_cost->msg;  /* msgr, msg */
2069               }
2070             else /* TARGET_31BIT */
2071               {
2072                 if (GET_CODE (left) == SIGN_EXTEND
2073                     && GET_CODE (right) == SIGN_EXTEND)
2074                   /* mulsidi case: mr, m */
2075                   *total = s390_cost->m;
2076                 else if (GET_CODE (left) == ZERO_EXTEND
2077                          && GET_CODE (right) == ZERO_EXTEND
2078                          && TARGET_CPU_ZARCH)
2079                   /* umulsidi case: ml, mlr */
2080                   *total = s390_cost->ml;
2081                 else
2082                   /* Complex calculation is required.  */
2083                   *total = COSTS_N_INSNS (40);
2084               }
2085             break;
2086           }
2087         case SFmode:
2088         case DFmode:
2089           *total = s390_cost->mult_df;
2090           break;
2091         default:
2092           return false;
2093         }
2094       return false;
2095
2096     case UDIV:
2097     case UMOD:
2098       if (GET_MODE (x) == TImode)              /* 128 bit division */
2099         *total = s390_cost->dlgr;
2100       else if (GET_MODE (x) == DImode)
2101         {
2102           rtx right = XEXP (x, 1);
2103           if (GET_CODE (right) == ZERO_EXTEND) /* 64 by 32 bit division */
2104             *total = s390_cost->dlr;
2105           else                                 /* 64 by 64 bit division */
2106             *total = s390_cost->dlgr;
2107         }
2108       else if (GET_MODE (x) == SImode)         /* 32 bit division */
2109         *total = s390_cost->dlr;
2110       return false;
2111
2112     case DIV:
2113     case MOD:
2114       if (GET_MODE (x) == DImode)
2115         {
2116           rtx right = XEXP (x, 1);
2117           if (GET_CODE (right) == ZERO_EXTEND) /* 64 by 32 bit division */
2118             if (TARGET_64BIT)
2119               *total = s390_cost->dsgfr;
2120             else
2121               *total = s390_cost->dr;
2122           else                                 /* 64 by 64 bit division */
2123             *total = s390_cost->dsgr;
2124         }
2125       else if (GET_MODE (x) == SImode)         /* 32 bit division */
2126         *total = s390_cost->dlr;
2127       else if (GET_MODE (x) == SFmode)
2128         {
2129           if (TARGET_IEEE_FLOAT)
2130             *total = s390_cost->debr;
2131           else /* TARGET_IBM_FLOAT */
2132             *total = s390_cost->der;
2133         }
2134       else if (GET_MODE (x) == DFmode)
2135         {
2136           if (TARGET_IEEE_FLOAT)
2137             *total = s390_cost->ddbr;
2138           else /* TARGET_IBM_FLOAT */
2139             *total = s390_cost->ddr;
2140         }
2141       return false;
2142
2143     case SQRT:
2144       if (GET_MODE (x) == SFmode)
2145         *total = s390_cost->sqebr;
2146       else /* DFmode */
2147         *total = s390_cost->sqdbr;
2148       return false;
2149
2150     case SIGN_EXTEND:
2151     case ZERO_EXTEND:
2152       if (outer_code == MULT || outer_code == DIV || outer_code == MOD
2153           || outer_code == PLUS || outer_code == MINUS
2154           || outer_code == COMPARE)
2155         *total = 0;
2156       return false;
2157
2158     case COMPARE:
2159       *total = COSTS_N_INSNS (1);
2160       if (GET_CODE (XEXP (x, 0)) == AND
2161           && GET_CODE (XEXP (x, 1)) == CONST_INT
2162           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
2163         {
2164           rtx op0 = XEXP (XEXP (x, 0), 0);
2165           rtx op1 = XEXP (XEXP (x, 0), 1);
2166           rtx op2 = XEXP (x, 1);
2167
2168           if (memory_operand (op0, GET_MODE (op0))
2169               && s390_tm_ccmode (op1, op2, 0) != VOIDmode)
2170             return true;
2171           if (register_operand (op0, GET_MODE (op0))
2172               && s390_tm_ccmode (op1, op2, 1) != VOIDmode)
2173             return true;
2174         }
2175       return false;
2176
2177     default:
2178       return false;
2179     }
2180 }
2181
2182 /* Return the cost of an address rtx ADDR.  */
2183
2184 static int
2185 s390_address_cost (rtx addr)
2186 {
2187   struct s390_address ad;
2188   if (!s390_decompose_address (addr, &ad))
2189     return 1000;
2190
2191   return ad.indx? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (1);
2192 }
2193
2194 /* If OP is a SYMBOL_REF of a thread-local symbol, return its TLS mode,
2195    otherwise return 0.  */
2196
2197 int
2198 tls_symbolic_operand (rtx op)
2199 {
2200   if (GET_CODE (op) != SYMBOL_REF)
2201     return 0;
2202   return SYMBOL_REF_TLS_MODEL (op);
2203 }
2204 \f
2205 /* Split DImode access register reference REG (on 64-bit) into its constituent
2206    low and high parts, and store them into LO and HI.  Note that gen_lowpart/
2207    gen_highpart cannot be used as they assume all registers are word-sized,
2208    while our access registers have only half that size.  */
2209
2210 void
2211 s390_split_access_reg (rtx reg, rtx *lo, rtx *hi)
2212 {
2213   gcc_assert (TARGET_64BIT);
2214   gcc_assert (ACCESS_REG_P (reg));
2215   gcc_assert (GET_MODE (reg) == DImode);
2216   gcc_assert (!(REGNO (reg) & 1));
2217
2218   *lo = gen_rtx_REG (SImode, REGNO (reg) + 1);
2219   *hi = gen_rtx_REG (SImode, REGNO (reg));
2220 }
2221
2222 /* Return true if OP contains a symbol reference */
2223
2224 bool
2225 symbolic_reference_mentioned_p (rtx op)
2226 {
2227   const char *fmt;
2228   int i;
2229
2230   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
2231     return 1;
2232
2233   fmt = GET_RTX_FORMAT (GET_CODE (op));
2234   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
2235     {
2236       if (fmt[i] == 'E')
2237         {
2238           int j;
2239
2240           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
2241             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
2242               return 1;
2243         }
2244
2245       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
2246         return 1;
2247     }
2248
2249   return 0;
2250 }
2251
2252 /* Return true if OP contains a reference to a thread-local symbol.  */
2253
2254 bool
2255 tls_symbolic_reference_mentioned_p (rtx op)
2256 {
2257   const char *fmt;
2258   int i;
2259
2260   if (GET_CODE (op) == SYMBOL_REF)
2261     return tls_symbolic_operand (op);
2262
2263   fmt = GET_RTX_FORMAT (GET_CODE (op));
2264   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
2265     {
2266       if (fmt[i] == 'E')
2267         {
2268           int j;
2269
2270           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
2271             if (tls_symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
2272               return true;
2273         }
2274
2275       else if (fmt[i] == 'e' && tls_symbolic_reference_mentioned_p (XEXP (op, i)))
2276         return true;
2277     }
2278
2279   return false;
2280 }
2281
2282
2283 /* Return true if OP is a legitimate general operand when
2284    generating PIC code.  It is given that flag_pic is on
2285    and that OP satisfies CONSTANT_P or is a CONST_DOUBLE.  */
2286
2287 int
2288 legitimate_pic_operand_p (rtx op)
2289 {
2290   /* Accept all non-symbolic constants.  */
2291   if (!SYMBOLIC_CONST (op))
2292     return 1;
2293
2294   /* Reject everything else; must be handled
2295      via emit_symbolic_move.  */
2296   return 0;
2297 }
2298
2299 /* Returns true if the constant value OP is a legitimate general operand.
2300    It is given that OP satisfies CONSTANT_P or is a CONST_DOUBLE.  */
2301
2302 int
2303 legitimate_constant_p (rtx op)
2304 {
2305   /* Accept all non-symbolic constants.  */
2306   if (!SYMBOLIC_CONST (op))
2307     return 1;
2308
2309   /* Accept immediate LARL operands.  */
2310   if (TARGET_CPU_ZARCH && larl_operand (op, VOIDmode))
2311     return 1;
2312
2313   /* Thread-local symbols are never legal constants.  This is
2314      so that emit_call knows that computing such addresses
2315      might require a function call.  */
2316   if (TLS_SYMBOLIC_CONST (op))
2317     return 0;
2318
2319   /* In the PIC case, symbolic constants must *not* be
2320      forced into the literal pool.  We accept them here,
2321      so that they will be handled by emit_symbolic_move.  */
2322   if (flag_pic)
2323     return 1;
2324
2325   /* All remaining non-PIC symbolic constants are
2326      forced into the literal pool.  */
2327   return 0;
2328 }
2329
2330 /* Determine if it's legal to put X into the constant pool.  This
2331    is not possible if X contains the address of a symbol that is
2332    not constant (TLS) or not known at final link time (PIC).  */
2333
2334 static bool
2335 s390_cannot_force_const_mem (rtx x)
2336 {
2337   switch (GET_CODE (x))
2338     {
2339     case CONST_INT:
2340     case CONST_DOUBLE:
2341       /* Accept all non-symbolic constants.  */
2342       return false;
2343
2344     case LABEL_REF:
2345       /* Labels are OK iff we are non-PIC.  */
2346       return flag_pic != 0;
2347
2348     case SYMBOL_REF:
2349       /* 'Naked' TLS symbol references are never OK,
2350          non-TLS symbols are OK iff we are non-PIC.  */
2351       if (tls_symbolic_operand (x))
2352         return true;
2353       else
2354         return flag_pic != 0;
2355
2356     case CONST:
2357       return s390_cannot_force_const_mem (XEXP (x, 0));
2358     case PLUS:
2359     case MINUS:
2360       return s390_cannot_force_const_mem (XEXP (x, 0))
2361              || s390_cannot_force_const_mem (XEXP (x, 1));
2362
2363     case UNSPEC:
2364       switch (XINT (x, 1))
2365         {
2366         /* Only lt-relative or GOT-relative UNSPECs are OK.  */
2367         case UNSPEC_LTREL_OFFSET:
2368         case UNSPEC_GOT:
2369         case UNSPEC_GOTOFF:
2370         case UNSPEC_PLTOFF:
2371         case UNSPEC_TLSGD:
2372         case UNSPEC_TLSLDM:
2373         case UNSPEC_NTPOFF:
2374         case UNSPEC_DTPOFF:
2375         case UNSPEC_GOTNTPOFF:
2376         case UNSPEC_INDNTPOFF:
2377           return false;
2378
2379         /* If the literal pool shares the code section, be put
2380            execute template placeholders into the pool as well.  */
2381         case UNSPEC_INSN:
2382           return TARGET_CPU_ZARCH;
2383
2384         default:
2385           return true;
2386         }
2387       break;
2388
2389     default:
2390       gcc_unreachable ();
2391     }
2392 }
2393
2394 /* Returns true if the constant value OP is a legitimate general
2395    operand during and after reload.  The difference to
2396    legitimate_constant_p is that this function will not accept
2397    a constant that would need to be forced to the literal pool
2398    before it can be used as operand.  */
2399
2400 bool
2401 legitimate_reload_constant_p (rtx op)
2402 {
2403   /* Accept la(y) operands.  */
2404   if (GET_CODE (op) == CONST_INT
2405       && DISP_IN_RANGE (INTVAL (op)))
2406     return true;
2407
2408   /* Accept l(g)hi/l(g)fi operands.  */
2409   if (GET_CODE (op) == CONST_INT
2410       && (CONST_OK_FOR_K (INTVAL (op)) || CONST_OK_FOR_Os (INTVAL (op))))
2411     return true;
2412
2413   /* Accept lliXX operands.  */
2414   if (TARGET_ZARCH
2415       && GET_CODE (op) == CONST_INT
2416       && trunc_int_for_mode (INTVAL (op), word_mode) == INTVAL (op)
2417       && s390_single_part (op, word_mode, HImode, 0) >= 0)
2418   return true;
2419
2420   if (TARGET_EXTIMM
2421       && GET_CODE (op) == CONST_INT
2422       && trunc_int_for_mode (INTVAL (op), word_mode) == INTVAL (op)
2423       && s390_single_part (op, word_mode, SImode, 0) >= 0)
2424     return true;
2425
2426   /* Accept larl operands.  */
2427   if (TARGET_CPU_ZARCH
2428       && larl_operand (op, VOIDmode))
2429     return true;
2430
2431   /* Accept lzXX operands.  */
2432   if (GET_CODE (op) == CONST_DOUBLE
2433       && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, 'G', "G"))
2434     return true;
2435
2436   /* Accept double-word operands that can be split.  */
2437   if (GET_CODE (op) == CONST_INT
2438       && trunc_int_for_mode (INTVAL (op), word_mode) != INTVAL (op))
2439     {
2440       enum machine_mode dword_mode = word_mode == SImode ? DImode : TImode;
2441       rtx hi = operand_subword (op, 0, 0, dword_mode);
2442       rtx lo = operand_subword (op, 1, 0, dword_mode);
2443       return legitimate_reload_constant_p (hi)
2444              && legitimate_reload_constant_p (lo);
2445     }
2446
2447   /* Everything else cannot be handled without reload.  */
2448   return false;
2449 }
2450
2451 /* Given an rtx OP being reloaded into a reg required to be in class CLASS,
2452    return the class of reg to actually use.  */
2453
2454 enum reg_class
2455 s390_preferred_reload_class (rtx op, enum reg_class class)
2456 {
2457   switch (GET_CODE (op))
2458     {
2459       /* Constants we cannot reload must be forced into the
2460          literal pool.  */
2461
2462       case CONST_DOUBLE:
2463       case CONST_INT:
2464         if (legitimate_reload_constant_p (op))
2465           return class;
2466         else
2467           return NO_REGS;
2468
2469       /* If a symbolic constant or a PLUS is reloaded,
2470          it is most likely being used as an address, so
2471          prefer ADDR_REGS.  If 'class' is not a superset
2472          of ADDR_REGS, e.g. FP_REGS, reject this reload.  */
2473       case PLUS:
2474       case LABEL_REF:
2475       case SYMBOL_REF:
2476       case CONST:
2477         if (reg_class_subset_p (ADDR_REGS, class))
2478           return ADDR_REGS;
2479         else
2480           return NO_REGS;
2481
2482       default:
2483         break;
2484     }
2485
2486   return class;
2487 }
2488
2489 /* Return the register class of a scratch register needed to
2490    load IN into a register of class CLASS in MODE.
2491
2492    We need a temporary when loading a PLUS expression which
2493    is not a legitimate operand of the LOAD ADDRESS instruction.  */
2494
2495 enum reg_class
2496 s390_secondary_input_reload_class (enum reg_class class,
2497                                    enum machine_mode mode, rtx in)
2498 {
2499   if (s390_plus_operand (in, mode))
2500     return ADDR_REGS;
2501
2502   if (reg_classes_intersect_p (CC_REGS, class))
2503     return GENERAL_REGS;
2504
2505   return NO_REGS;
2506 }
2507
2508 /* Return the register class of a scratch register needed to
2509    store a register of class CLASS in MODE into OUT:
2510
2511    We need a temporary when storing a double-word to a
2512    non-offsettable memory address.  */
2513
2514 enum reg_class
2515 s390_secondary_output_reload_class (enum reg_class class,
2516                                     enum machine_mode mode, rtx out)
2517 {
2518   if ((TARGET_64BIT ? mode == TImode
2519                     : (mode == DImode || mode == DFmode))
2520       && reg_classes_intersect_p (GENERAL_REGS, class)
2521       && GET_CODE (out) == MEM
2522       && GET_CODE (XEXP (out, 0)) == PLUS
2523       && GET_CODE (XEXP (XEXP (out, 0), 0)) == PLUS
2524       && GET_CODE (XEXP (XEXP (out, 0), 1)) == CONST_INT
2525       && !DISP_IN_RANGE (INTVAL (XEXP (XEXP (out, 0), 1))
2526                          + GET_MODE_SIZE (mode) - 1))
2527     return ADDR_REGS;
2528
2529   if (reg_classes_intersect_p (CC_REGS, class))
2530     return GENERAL_REGS;
2531
2532   return NO_REGS;
2533 }
2534
2535 /* Generate code to load SRC, which is PLUS that is not a
2536    legitimate operand for the LA instruction, into TARGET.
2537    SCRATCH may be used as scratch register.  */
2538
2539 void
2540 s390_expand_plus_operand (rtx target, rtx src,
2541                           rtx scratch)
2542 {
2543   rtx sum1, sum2;
2544   struct s390_address ad;
2545
2546   /* src must be a PLUS; get its two operands.  */
2547   gcc_assert (GET_CODE (src) == PLUS);
2548   gcc_assert (GET_MODE (src) == Pmode);
2549
2550   /* Check if any of the two operands is already scheduled
2551      for replacement by reload.  This can happen e.g. when
2552      float registers occur in an address.  */
2553   sum1 = find_replacement (&XEXP (src, 0));
2554   sum2 = find_replacement (&XEXP (src, 1));
2555   src = gen_rtx_PLUS (Pmode, sum1, sum2);
2556
2557   /* If the address is already strictly valid, there's nothing to do.  */
2558   if (!s390_decompose_address (src, &ad)
2559       || (ad.base && !REG_OK_FOR_BASE_STRICT_P (ad.base))
2560       || (ad.indx && !REG_OK_FOR_INDEX_STRICT_P (ad.indx)))
2561     {
2562       /* Otherwise, one of the operands cannot be an address register;
2563          we reload its value into the scratch register.  */
2564       if (true_regnum (sum1) < 1 || true_regnum (sum1) > 15)
2565         {
2566           emit_move_insn (scratch, sum1);
2567           sum1 = scratch;
2568         }
2569       if (true_regnum (sum2) < 1 || true_regnum (sum2) > 15)
2570         {
2571           emit_move_insn (scratch, sum2);
2572           sum2 = scratch;
2573         }
2574
2575       /* According to the way these invalid addresses are generated
2576          in reload.c, it should never happen (at least on s390) that
2577          *neither* of the PLUS components, after find_replacements
2578          was applied, is an address register.  */
2579       if (sum1 == scratch && sum2 == scratch)
2580         {
2581           debug_rtx (src);
2582           gcc_unreachable ();
2583         }
2584
2585       src = gen_rtx_PLUS (Pmode, sum1, sum2);
2586     }
2587
2588   /* Emit the LOAD ADDRESS pattern.  Note that reload of PLUS
2589      is only ever performed on addresses, so we can mark the
2590      sum as legitimate for LA in any case.  */
2591   s390_load_address (target, src);
2592 }
2593
2594
2595 /* Return true if ADDR is a valid memory address.
2596    STRICT specifies whether strict register checking applies.  */
2597
2598 bool
2599 legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2600                       rtx addr, int strict)
2601 {
2602   struct s390_address ad;
2603   if (!s390_decompose_address (addr, &ad))
2604     return false;
2605
2606   if (strict)
2607     {
2608       if (ad.base && !REG_OK_FOR_BASE_STRICT_P (ad.base))
2609         return false;
2610       if (ad.indx && !REG_OK_FOR_INDEX_STRICT_P (ad.indx))
2611         return false;
2612     }
2613   else
2614     {
2615       if (ad.base && !REG_OK_FOR_BASE_NONSTRICT_P (ad.base))
2616         return false;
2617       if (ad.indx && !REG_OK_FOR_INDEX_NONSTRICT_P (ad.indx))
2618         return false;
2619     }
2620
2621   return true;
2622 }
2623
2624 /* Return true if OP is a valid operand for the LA instruction.
2625    In 31-bit, we need to prove that the result is used as an
2626    address, as LA performs only a 31-bit addition.  */
2627
2628 bool
2629 legitimate_la_operand_p (rtx op)
2630 {
2631   struct s390_address addr;
2632   if (!s390_decompose_address (op, &addr))
2633     return false;
2634
2635   return (TARGET_64BIT || addr.pointer);
2636 }
2637
2638 /* Return true if it is valid *and* preferable to use LA to
2639    compute the sum of OP1 and OP2.  */
2640
2641 bool
2642 preferred_la_operand_p (rtx op1, rtx op2)
2643 {
2644   struct s390_address addr;
2645
2646   if (op2 != const0_rtx)
2647     op1 = gen_rtx_PLUS (Pmode, op1, op2);
2648
2649   if (!s390_decompose_address (op1, &addr))
2650     return false;
2651   if (addr.base && !REG_OK_FOR_BASE_STRICT_P (addr.base))
2652     return false;
2653   if (addr.indx && !REG_OK_FOR_INDEX_STRICT_P (addr.indx))
2654     return false;
2655
2656   if (!TARGET_64BIT && !addr.pointer)
2657     return false;
2658
2659   if (addr.pointer)
2660     return true;
2661
2662   if ((addr.base && REG_P (addr.base) && REG_POINTER (addr.base))
2663       || (addr.indx && REG_P (addr.indx) && REG_POINTER (addr.indx)))
2664     return true;
2665
2666   return false;
2667 }
2668
2669 /* Emit a forced load-address operation to load SRC into DST.
2670    This will use the LOAD ADDRESS instruction even in situations
2671    where legitimate_la_operand_p (SRC) returns false.  */
2672
2673 void
2674 s390_load_address (rtx dst, rtx src)
2675 {
2676   if (TARGET_64BIT)
2677     emit_move_insn (dst, src);
2678   else
2679     emit_insn (gen_force_la_31 (dst, src));
2680 }
2681
2682 /* Return a legitimate reference for ORIG (an address) using the
2683    register REG.  If REG is 0, a new pseudo is generated.
2684
2685    There are two types of references that must be handled:
2686
2687    1. Global data references must load the address from the GOT, via
2688       the PIC reg.  An insn is emitted to do this load, and the reg is
2689       returned.
2690
2691    2. Static data references, constant pool addresses, and code labels
2692       compute the address as an offset from the GOT, whose base is in
2693       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
2694       differentiate them from global data objects.  The returned
2695       address is the PIC reg + an unspec constant.
2696
2697    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
2698    reg also appears in the address.  */
2699
2700 rtx
2701 legitimize_pic_address (rtx orig, rtx reg)
2702 {
2703   rtx addr = orig;
2704   rtx new = orig;
2705   rtx base;
2706
2707   if (GET_CODE (addr) == LABEL_REF
2708       || (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (addr)))
2709     {
2710       /* This is a local symbol.  */
2711       if (TARGET_CPU_ZARCH && larl_operand (addr, VOIDmode))
2712         {
2713           /* Access local symbols PC-relative via LARL.
2714              This is the same as in the non-PIC case, so it is
2715              handled automatically ...  */
2716         }
2717       else
2718         {
2719           /* Access local symbols relative to the GOT.  */
2720
2721           rtx temp = reg? reg : gen_reg_rtx (Pmode);
2722
2723           if (reload_in_progress || reload_completed)
2724             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2725
2726           addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
2727           addr = gen_rtx_CONST (Pmode, addr);
2728           addr = force_const_mem (Pmode, addr);
2729           emit_move_insn (temp, addr);
2730
2731           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
2732           if (reg != 0)
2733             {
2734               s390_load_address (reg, new);
2735               new = reg;
2736             }
2737         }
2738     }
2739   else if (GET_CODE (addr) == SYMBOL_REF)
2740     {
2741       if (reg == 0)
2742         reg = gen_reg_rtx (Pmode);
2743
2744       if (flag_pic == 1)
2745         {
2746           /* Assume GOT offset < 4k.  This is handled the same way
2747              in both 31- and 64-bit code (@GOT).  */
2748
2749           if (reload_in_progress || reload_completed)
2750             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2751
2752           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
2753           new = gen_rtx_CONST (Pmode, new);
2754           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
2755           new = gen_const_mem (Pmode, new);
2756           emit_move_insn (reg, new);
2757           new = reg;
2758         }
2759       else if (TARGET_CPU_ZARCH)
2760         {
2761           /* If the GOT offset might be >= 4k, we determine the position
2762              of the GOT entry via a PC-relative LARL (@GOTENT).  */
2763
2764           rtx temp = gen_reg_rtx (Pmode);
2765
2766           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTENT);
2767           new = gen_rtx_CONST (Pmode, new);
2768           emit_move_insn (temp, new);
2769
2770           new = gen_const_mem (Pmode, temp);
2771           emit_move_insn (reg, new);
2772           new = reg;
2773         }
2774       else
2775         {
2776           /* If the GOT offset might be >= 4k, we have to load it
2777              from the literal pool (@GOT).  */
2778
2779           rtx temp = gen_reg_rtx (Pmode);
2780
2781           if (reload_in_progress || reload_completed)
2782             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2783
2784           addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
2785           addr = gen_rtx_CONST (Pmode, addr);
2786           addr = force_const_mem (Pmode, addr);
2787           emit_move_insn (temp, addr);
2788
2789           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
2790           new = gen_const_mem (Pmode, new);
2791           emit_move_insn (reg, new);
2792           new = reg;
2793         }
2794     }
2795   else
2796     {
2797       if (GET_CODE (addr) == CONST)
2798         {
2799           addr = XEXP (addr, 0);
2800           if (GET_CODE (addr) == UNSPEC)
2801             {
2802               gcc_assert (XVECLEN (addr, 0) == 1);
2803               switch (XINT (addr, 1))
2804                 {
2805                   /* If someone moved a GOT-relative UNSPEC
2806                      out of the literal pool, force them back in.  */
2807                   case UNSPEC_GOTOFF:
2808                   case UNSPEC_PLTOFF:
2809                     new = force_const_mem (Pmode, orig);
2810                     break;
2811
2812                   /* @GOT is OK as is if small.  */
2813                   case UNSPEC_GOT:
2814                     if (flag_pic == 2)
2815                       new = force_const_mem (Pmode, orig);
2816                     break;
2817
2818                   /* @GOTENT is OK as is.  */
2819                   case UNSPEC_GOTENT:
2820                     break;
2821
2822                   /* @PLT is OK as is on 64-bit, must be converted to
2823                      GOT-relative @PLTOFF on 31-bit.  */
2824                   case UNSPEC_PLT:
2825                     if (!TARGET_CPU_ZARCH)
2826                       {
2827                         rtx temp = reg? reg : gen_reg_rtx (Pmode);
2828
2829                         if (reload_in_progress || reload_completed)
2830                           regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2831
2832                         addr = XVECEXP (addr, 0, 0);
2833                         addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr),
2834                                                UNSPEC_PLTOFF);
2835                         addr = gen_rtx_CONST (Pmode, addr);
2836                         addr = force_const_mem (Pmode, addr);
2837                         emit_move_insn (temp, addr);
2838
2839                         new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
2840                         if (reg != 0)
2841                           {
2842                             s390_load_address (reg, new);
2843                             new = reg;
2844                           }
2845                       }
2846                     break;
2847
2848                   /* Everything else cannot happen.  */
2849                   default:
2850                     gcc_unreachable ();
2851                 }
2852             }
2853           else 
2854             gcc_assert (GET_CODE (addr) == PLUS);
2855         }
2856       if (GET_CODE (addr) == PLUS)
2857         {
2858           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
2859           /* Check first to see if this is a constant offset
2860              from a local symbol reference.  */
2861           if ((GET_CODE (op0) == LABEL_REF
2862                 || (GET_CODE (op0) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (op0)))
2863               && GET_CODE (op1) == CONST_INT)
2864             {
2865               if (TARGET_CPU_ZARCH && larl_operand (op0, VOIDmode))
2866                 {
2867                   if (INTVAL (op1) & 1)
2868                     {
2869                       /* LARL can't handle odd offsets, so emit a
2870                          pair of LARL and LA.  */
2871                       rtx temp = reg? reg : gen_reg_rtx (Pmode);
2872
2873                       if (!DISP_IN_RANGE (INTVAL (op1)))
2874                         {
2875                           int even = INTVAL (op1) - 1;
2876                           op0 = gen_rtx_PLUS (Pmode, op0, GEN_INT (even));
2877                           op0 = gen_rtx_CONST (Pmode, op0);
2878                           op1 = const1_rtx;
2879                         }
2880
2881                       emit_move_insn (temp, op0);
2882                       new = gen_rtx_PLUS (Pmode, temp, op1);
2883
2884                       if (reg != 0)
2885                         {
2886                           s390_load_address (reg, new);
2887                           new = reg;
2888                         }
2889                     }
2890                   else
2891                     {
2892                       /* If the offset is even, we can just use LARL.
2893                          This will happen automatically.  */
2894                     }
2895                 }
2896               else
2897                 {
2898                   /* Access local symbols relative to the GOT.  */
2899
2900                   rtx temp = reg? reg : gen_reg_rtx (Pmode);
2901
2902                   if (reload_in_progress || reload_completed)
2903                     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2904
2905                   addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
2906                                          UNSPEC_GOTOFF);
2907                   addr = gen_rtx_PLUS (Pmode, addr, op1);
2908                   addr = gen_rtx_CONST (Pmode, addr);
2909                   addr = force_const_mem (Pmode, addr);
2910                   emit_move_insn (temp, addr);
2911
2912                   new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
2913                   if (reg != 0)
2914                     {
2915                       s390_load_address (reg, new);
2916                       new = reg;
2917                     }
2918                 }
2919             }
2920
2921           /* Now, check whether it is a GOT relative symbol plus offset
2922              that was pulled out of the literal pool.  Force it back in.  */
2923
2924           else if (GET_CODE (op0) == UNSPEC
2925                    && GET_CODE (op1) == CONST_INT
2926                    && XINT (op0, 1) == UNSPEC_GOTOFF)
2927             {
2928               gcc_assert (XVECLEN (op0, 0) == 1);
2929
2930               new = force_const_mem (Pmode, orig);
2931             }
2932
2933           /* Otherwise, compute the sum.  */
2934           else
2935             {
2936               base = legitimize_pic_address (XEXP (addr, 0), reg);
2937               new  = legitimize_pic_address (XEXP (addr, 1),
2938                                              base == reg ? NULL_RTX : reg);
2939               if (GET_CODE (new) == CONST_INT)
2940                 new = plus_constant (base, INTVAL (new));
2941               else
2942                 {
2943                   if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
2944                     {
2945                       base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
2946                       new = XEXP (new, 1);
2947                     }
2948                   new = gen_rtx_PLUS (Pmode, base, new);
2949                 }
2950
2951               if (GET_CODE (new) == CONST)
2952                 new = XEXP (new, 0);
2953               new = force_operand (new, 0);
2954             }
2955         }
2956     }
2957   return new;
2958 }
2959
2960 /* Load the thread pointer into a register.  */
2961
2962 rtx
2963 s390_get_thread_pointer (void)
2964 {
2965   rtx tp = gen_reg_rtx (Pmode);
2966
2967   emit_move_insn (tp, gen_rtx_REG (Pmode, TP_REGNUM));
2968   mark_reg_pointer (tp, BITS_PER_WORD);
2969
2970   return tp;
2971 }
2972
2973 /* Emit a tls call insn. The call target is the SYMBOL_REF stored
2974    in s390_tls_symbol which always refers to __tls_get_offset.
2975    The returned offset is written to RESULT_REG and an USE rtx is
2976    generated for TLS_CALL.  */
2977
2978 static GTY(()) rtx s390_tls_symbol;
2979
2980 static void
2981 s390_emit_tls_call_insn (rtx result_reg, rtx tls_call)
2982 {
2983   rtx insn;
2984
2985   gcc_assert (flag_pic);
2986
2987   if (!s390_tls_symbol)
2988     s390_tls_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tls_get_offset");
2989
2990   insn = s390_emit_call (s390_tls_symbol, tls_call, result_reg,
2991                          gen_rtx_REG (Pmode, RETURN_REGNUM));
2992
2993   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), result_reg);
2994   CONST_OR_PURE_CALL_P (insn) = 1;
2995 }
2996
2997 /* ADDR contains a thread-local SYMBOL_REF.  Generate code to compute
2998    this (thread-local) address.  REG may be used as temporary.  */
2999
3000 static rtx
3001 legitimize_tls_address (rtx addr, rtx reg)
3002 {
3003   rtx new, tls_call, temp, base, r2, insn;
3004
3005   if (GET_CODE (addr) == SYMBOL_REF)
3006     switch (tls_symbolic_operand (addr))
3007       {
3008       case TLS_MODEL_GLOBAL_DYNAMIC:
3009         start_sequence ();
3010         r2 = gen_rtx_REG (Pmode, 2);
3011         tls_call = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_TLSGD);
3012         new = gen_rtx_CONST (Pmode, tls_call);
3013         new = force_const_mem (Pmode, new);
3014         emit_move_insn (r2, new);
3015         s390_emit_tls_call_insn (r2, tls_call);
3016         insn = get_insns ();
3017         end_sequence ();
3018
3019         new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_NTPOFF);
3020         temp = gen_reg_rtx (Pmode);
3021         emit_libcall_block (insn, temp, r2, new);
3022
3023         new = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
3024         if (reg != 0)
3025           {
3026             s390_load_address (reg, new);
3027             new = reg;
3028           }
3029         break;
3030
3031       case TLS_MODEL_LOCAL_DYNAMIC:
3032         start_sequence ();
3033         r2 = gen_rtx_REG (Pmode, 2);
3034         tls_call = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TLSLDM);
3035         new = gen_rtx_CONST (Pmode, tls_call);
3036         new = force_const_mem (Pmode, new);
3037         emit_move_insn (r2, new);
3038         s390_emit_tls_call_insn (r2, tls_call);
3039         insn = get_insns ();
3040         end_sequence ();
3041
3042         new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TLSLDM_NTPOFF);
3043         temp = gen_reg_rtx (Pmode);
3044         emit_libcall_block (insn, temp, r2, new);
3045
3046         new = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
3047         base = gen_reg_rtx (Pmode);
3048         s390_load_address (base, new);
3049
3050         new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_DTPOFF);
3051         new = gen_rtx_CONST (Pmode, new);
3052         new = force_const_mem (Pmode, new);
3053         temp = gen_reg_rtx (Pmode);
3054         emit_move_insn (temp, new);
3055
3056         new = gen_rtx_PLUS (Pmode, base, temp);
3057         if (reg != 0)
3058           {
3059             s390_load_address (reg, new);
3060             new = reg;
3061           }
3062         break;
3063
3064       case TLS_MODEL_INITIAL_EXEC:
3065         if (flag_pic == 1)
3066           {
3067             /* Assume GOT offset < 4k.  This is handled the same way
3068                in both 31- and 64-bit code.  */
3069
3070             if (reload_in_progress || reload_completed)
3071               regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
3072
3073             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTNTPOFF);
3074             new = gen_rtx_CONST (Pmode, new);
3075             new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
3076             new = gen_const_mem (Pmode, new);
3077             temp = gen_reg_rtx (Pmode);
3078             emit_move_insn (temp, new);
3079           }
3080         else if (TARGET_CPU_ZARCH)
3081           {
3082             /* If the GOT offset might be >= 4k, we determine the position
3083                of the GOT entry via a PC-relative LARL.  */
3084
3085             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_INDNTPOFF);
3086             new = gen_rtx_CONST (Pmode, new);
3087             temp = gen_reg_rtx (Pmode);
3088             emit_move_insn (temp, new);
3089
3090             new = gen_const_mem (Pmode, temp);
3091             temp = gen_reg_rtx (Pmode);
3092             emit_move_insn (temp, new);
3093           }
3094         else if (flag_pic)
3095           {
3096             /* If the GOT offset might be >= 4k, we have to load it
3097                from the literal pool.  */
3098
3099             if (reload_in_progress || reload_completed)
3100               regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
3101
3102             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTNTPOFF);
3103             new = gen_rtx_CONST (Pmode, new);
3104             new = force_const_mem (Pmode, new);
3105             temp = gen_reg_rtx (Pmode);
3106             emit_move_insn (temp, new);
3107
3108             new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
3109             new = gen_const_mem (Pmode, new);
3110
3111             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, new, addr), UNSPEC_TLS_LOAD);
3112             temp = gen_reg_rtx (Pmode);
3113             emit_insn (gen_rtx_SET (Pmode, temp, new));
3114           }
3115         else
3116           {
3117             /* In position-dependent code, load the absolute address of
3118                the GOT entry from the literal pool.  */
3119
3120             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_INDNTPOFF);
3121             new = gen_rtx_CONST (Pmode, new);
3122             new = force_const_mem (Pmode, new);
3123             temp = gen_reg_rtx (Pmode);
3124             emit_move_insn (temp, new);
3125
3126             new = temp;
3127             new = gen_const_mem (Pmode, new);
3128             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, new, addr), UNSPEC_TLS_LOAD);
3129             temp = gen_reg_rtx (Pmode);
3130             emit_insn (gen_rtx_SET (Pmode, temp, new));
3131           }
3132
3133         new = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
3134         if (reg != 0)
3135           {
3136             s390_load_address (reg, new);
3137             new = reg;
3138           }
3139         break;
3140
3141       case TLS_MODEL_LOCAL_EXEC:
3142         new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_NTPOFF);
3143         new = gen_rtx_CONST (Pmode, new);
3144         new = force_const_mem (Pmode, new);
3145         temp = gen_reg_rtx (Pmode);
3146         emit_move_insn (temp, new);
3147
3148         new = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
3149         if (reg != 0)
3150           {
3151             s390_load_address (reg, new);
3152             new = reg;
3153           }
3154         break;
3155
3156       default:
3157         gcc_unreachable ();
3158       }
3159
3160   else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == UNSPEC)
3161     {
3162       switch (XINT (XEXP (addr, 0), 1))
3163         {
3164         case UNSPEC_INDNTPOFF:
3165           gcc_assert (TARGET_CPU_ZARCH);
3166           new = addr;
3167           break;
3168
3169         default:
3170           gcc_unreachable ();
3171         }
3172     }
3173
3174   else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS
3175            && GET_CODE (XEXP (XEXP (addr, 0), 1)) == CONST_INT)
3176     {
3177       new = XEXP (XEXP (addr, 0), 0);
3178       if (GET_CODE (new) != SYMBOL_REF)
3179         new = gen_rtx_CONST (Pmode, new);
3180
3181       new = legitimize_tls_address (new, reg);
3182       new = plus_constant (new, INTVAL (XEXP (XEXP (addr, 0), 1)));
3183       new = force_operand (new, 0);
3184     }
3185
3186   else
3187     gcc_unreachable ();  /* for now ... */
3188
3189   return new;
3190 }
3191
3192 /* Emit insns to move operands[1] into operands[0].  */
3193
3194 void
3195 emit_symbolic_move (rtx *operands)
3196 {
3197   rtx temp = no_new_pseudos ? operands[0] : gen_reg_rtx (Pmode);
3198
3199   if (GET_CODE (operands[0]) == MEM)
3200     operands[1] = force_reg (Pmode, operands[1]);
3201   else if (TLS_SYMBOLIC_CONST (operands[1]))
3202     operands[1] = legitimize_tls_address (operands[1], temp);
3203   else if (flag_pic)
3204     operands[1] = legitimize_pic_address (operands[1], temp);
3205 }
3206
3207 /* Try machine-dependent ways of modifying an illegitimate address X
3208    to be legitimate.  If we find one, return the new, valid address.
3209
3210    OLDX is the address as it was before break_out_memory_refs was called.
3211    In some cases it is useful to look at this to decide what needs to be done.
3212
3213    MODE is the mode of the operand pointed to by X.
3214
3215    When -fpic is used, special handling is needed for symbolic references.
3216    See comments by legitimize_pic_address for details.  */
3217
3218 rtx
3219 legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
3220                     enum machine_mode mode ATTRIBUTE_UNUSED)
3221 {
3222   rtx constant_term = const0_rtx;
3223
3224   if (TLS_SYMBOLIC_CONST (x))
3225     {
3226       x = legitimize_tls_address (x, 0);
3227
3228       if (legitimate_address_p (mode, x, FALSE))
3229         return x;
3230     }
3231   else if (flag_pic)
3232     {
3233       if (SYMBOLIC_CONST (x)
3234           || (GET_CODE (x) == PLUS
3235               && (SYMBOLIC_CONST (XEXP (x, 0))
3236                   || SYMBOLIC_CONST (XEXP (x, 1)))))
3237           x = legitimize_pic_address (x, 0);
3238
3239       if (legitimate_address_p (mode, x, FALSE))
3240         return x;
3241     }
3242
3243   x = eliminate_constant_term (x, &constant_term);
3244
3245   /* Optimize loading of large displacements by splitting them
3246      into the multiple of 4K and the rest; this allows the
3247      former to be CSE'd if possible.
3248
3249      Don't do this if the displacement is added to a register
3250      pointing into the stack frame, as the offsets will
3251      change later anyway.  */
3252
3253   if (GET_CODE (constant_term) == CONST_INT
3254       && !TARGET_LONG_DISPLACEMENT
3255       && !DISP_IN_RANGE (INTVAL (constant_term))
3256       && !(REG_P (x) && REGNO_PTR_FRAME_P (REGNO (x))))
3257     {
3258       HOST_WIDE_INT lower = INTVAL (constant_term) & 0xfff;
3259       HOST_WIDE_INT upper = INTVAL (constant_term) ^ lower;
3260
3261       rtx temp = gen_reg_rtx (Pmode);
3262       rtx val  = force_operand (GEN_INT (upper), temp);
3263       if (val != temp)
3264         emit_move_insn (temp, val);
3265
3266       x = gen_rtx_PLUS (Pmode, x, temp);
3267       constant_term = GEN_INT (lower);
3268     }
3269
3270   if (GET_CODE (x) == PLUS)
3271     {
3272       if (GET_CODE (XEXP (x, 0)) == REG)
3273         {
3274           rtx temp = gen_reg_rtx (Pmode);
3275           rtx val  = force_operand (XEXP (x, 1), temp);
3276           if (val != temp)
3277             emit_move_insn (temp, val);
3278
3279           x = gen_rtx_PLUS (Pmode, XEXP (x, 0), temp);
3280         }
3281
3282       else if (GET_CODE (XEXP (x, 1)) == REG)
3283         {
3284           rtx temp = gen_reg_rtx (Pmode);
3285           rtx val  = force_operand (XEXP (x, 0), temp);
3286           if (val != temp)
3287             emit_move_insn (temp, val);
3288
3289           x = gen_rtx_PLUS (Pmode, temp, XEXP (x, 1));
3290         }
3291     }
3292
3293   if (constant_term != const0_rtx)
3294     x = gen_rtx_PLUS (Pmode, x, constant_term);
3295
3296   return x;
3297 }
3298
3299 /* Try a machine-dependent way of reloading an illegitimate address AD
3300    operand.  If we find one, push the reload and and return the new address.
3301
3302    MODE is the mode of the enclosing MEM.  OPNUM is the operand number
3303    and TYPE is the reload type of the current reload.  */
3304
3305 rtx 
3306 legitimize_reload_address (rtx ad, enum machine_mode mode ATTRIBUTE_UNUSED,
3307                            int opnum, int type)
3308 {
3309   if (!optimize || TARGET_LONG_DISPLACEMENT)
3310     return NULL_RTX;
3311
3312   if (GET_CODE (ad) == PLUS)
3313     {
3314       rtx tem = simplify_binary_operation (PLUS, Pmode,
3315                                            XEXP (ad, 0), XEXP (ad, 1));
3316       if (tem)
3317         ad = tem;
3318     }
3319
3320   if (GET_CODE (ad) == PLUS
3321       && GET_CODE (XEXP (ad, 0)) == REG
3322       && GET_CODE (XEXP (ad, 1)) == CONST_INT
3323       && !DISP_IN_RANGE (INTVAL (XEXP (ad, 1))))
3324     {
3325       HOST_WIDE_INT lower = INTVAL (XEXP (ad, 1)) & 0xfff;
3326       HOST_WIDE_INT upper = INTVAL (XEXP (ad, 1)) ^ lower;
3327       rtx cst, tem, new;
3328
3329       cst = GEN_INT (upper);
3330       if (!legitimate_reload_constant_p (cst))
3331         cst = force_const_mem (Pmode, cst);
3332
3333       tem = gen_rtx_PLUS (Pmode, XEXP (ad, 0), cst);
3334       new = gen_rtx_PLUS (Pmode, tem, GEN_INT (lower));
3335
3336       push_reload (XEXP (tem, 1), 0, &XEXP (tem, 1), 0,
3337                    BASE_REG_CLASS, Pmode, VOIDmode, 0, 0, 
3338                    opnum, (enum reload_type) type);
3339       return new;
3340     }
3341
3342   return NULL_RTX;
3343 }
3344
3345 /* Emit code to move LEN bytes from DST to SRC.  */
3346
3347 void
3348 s390_expand_movmem (rtx dst, rtx src, rtx len)
3349 {
3350   if (GET_CODE (len) == CONST_INT && INTVAL (len) >= 0 && INTVAL (len) <= 256)
3351     {
3352       if (INTVAL (len) > 0)
3353         emit_insn (gen_movmem_short (dst, src, GEN_INT (INTVAL (len) - 1)));
3354     }
3355
3356   else if (TARGET_MVCLE)
3357     {
3358       emit_insn (gen_movmem_long (dst, src, convert_to_mode (Pmode, len, 1)));
3359     }
3360
3361   else
3362     {
3363       rtx dst_addr, src_addr, count, blocks, temp;
3364       rtx loop_start_label = gen_label_rtx ();
3365       rtx loop_end_label = gen_label_rtx ();
3366       rtx end_label = gen_label_rtx ();
3367       enum machine_mode mode;
3368
3369       mode = GET_MODE (len);
3370       if (mode == VOIDmode)
3371         mode = Pmode;
3372
3373       dst_addr = gen_reg_rtx (Pmode);
3374       src_addr = gen_reg_rtx (Pmode);
3375       count = gen_reg_rtx (mode);
3376       blocks = gen_reg_rtx (mode);
3377
3378       convert_move (count, len, 1);
3379       emit_cmp_and_jump_insns (count, const0_rtx,
3380                                EQ, NULL_RTX, mode, 1, end_label);
3381
3382       emit_move_insn (dst_addr, force_operand (XEXP (dst, 0), NULL_RTX));
3383       emit_move_insn (src_addr, force_operand (XEXP (src, 0), NULL_RTX));
3384       dst = change_address (dst, VOIDmode, dst_addr);
3385       src = change_address (src, VOIDmode, src_addr);
3386
3387       temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1, 0);
3388       if (temp != count)
3389         emit_move_insn (count, temp);
3390
3391       temp = expand_binop (mode, ashr_optab, count, GEN_INT (8), blocks, 1, 0);
3392       if (temp != blocks)
3393         emit_move_insn (blocks, temp);
3394
3395       emit_cmp_and_jump_insns (blocks, const0_rtx,
3396                                EQ, NULL_RTX, mode, 1, loop_end_label);
3397
3398       emit_label (loop_start_label);
3399
3400       emit_insn (gen_movmem_short (dst, src, GEN_INT (255)));
3401       s390_load_address (dst_addr,
3402                          gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (256)));
3403       s390_load_address (src_addr,
3404                          gen_rtx_PLUS (Pmode, src_addr, GEN_INT (256)));
3405
3406       temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1, 0);
3407       if (temp != blocks)
3408         emit_move_insn (blocks, temp);
3409
3410       emit_cmp_and_jump_insns (blocks, const0_rtx,
3411                                EQ, NULL_RTX, mode, 1, loop_end_label);
3412
3413       emit_jump (loop_start_label);
3414       emit_label (loop_end_label);
3415
3416       emit_insn (gen_movmem_short (dst, src,
3417                                    convert_to_mode (Pmode, count, 1)));
3418       emit_label (end_label);
3419     }
3420 }
3421
3422 /* Emit code to set LEN bytes at DST to VAL.
3423    Make use of clrmem if VAL is zero.  */
3424
3425 void
3426 s390_expand_setmem (rtx dst, rtx len, rtx val)
3427 {
3428   gcc_assert (GET_CODE (len) != CONST_INT || INTVAL (len) > 0);
3429   gcc_assert (GET_CODE (val) == CONST_INT || GET_MODE (val) == QImode);
3430   
3431   if (GET_CODE (len) == CONST_INT && INTVAL (len) <= 257)
3432     {
3433       if (val == const0_rtx && INTVAL (len) <= 256)
3434         emit_insn (gen_clrmem_short (dst, GEN_INT (INTVAL (len) - 1)));
3435       else
3436         {
3437           /* Initialize memory by storing the first byte.  */
3438           emit_move_insn (adjust_address (dst, QImode, 0), val);
3439           
3440           if (INTVAL (len) > 1)
3441             {
3442               /* Initiate 1 byte overlap move.
3443                  The first byte of DST is propagated through DSTP1.
3444                  Prepare a movmem for:  DST+1 = DST (length = LEN - 1).
3445                  DST is set to size 1 so the rest of the memory location
3446                  does not count as source operand.  */
3447               rtx dstp1 = adjust_address (dst, VOIDmode, 1);
3448               set_mem_size (dst, const1_rtx);
3449
3450               emit_insn (gen_movmem_short (dstp1, dst, 
3451                                            GEN_INT (INTVAL (len) - 2)));
3452             }
3453         }
3454     }
3455
3456   else if (TARGET_MVCLE)
3457     {
3458       val = force_not_mem (convert_modes (Pmode, QImode, val, 1));
3459       emit_insn (gen_setmem_long (dst, convert_to_mode (Pmode, len, 1), val));
3460     }
3461
3462   else
3463     {
3464       rtx dst_addr, src_addr, count, blocks, temp, dstp1 = NULL_RTX;
3465       rtx loop_start_label = gen_label_rtx ();
3466       rtx loop_end_label = gen_label_rtx ();
3467       rtx end_label = gen_label_rtx ();
3468       enum machine_mode mode;
3469
3470       mode = GET_MODE (len);
3471       if (mode == VOIDmode)
3472         mode = Pmode;
3473
3474       dst_addr = gen_reg_rtx (Pmode);
3475       src_addr = gen_reg_rtx (Pmode);
3476       count = gen_reg_rtx (mode);
3477       blocks = gen_reg_rtx (mode);
3478
3479       convert_move (count, len, 1);
3480       emit_cmp_and_jump_insns (count, const0_rtx,
3481                                EQ, NULL_RTX, mode, 1, end_label);
3482
3483       emit_move_insn (dst_addr, force_operand (XEXP (dst, 0), NULL_RTX));
3484       dst = change_address (dst, VOIDmode, dst_addr);
3485
3486       if (val == const0_rtx)
3487         temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1, 0);
3488       else
3489         {
3490           dstp1 = adjust_address (dst, VOIDmode, 1);
3491           set_mem_size (dst, const1_rtx);
3492
3493           /* Initialize memory by storing the first byte.  */
3494           emit_move_insn (adjust_address (dst, QImode, 0), val);
3495           
3496           /* If count is 1 we are done.  */
3497           emit_cmp_and_jump_insns (count, const1_rtx,
3498                                    EQ, NULL_RTX, mode, 1, end_label);
3499
3500           temp = expand_binop (mode, add_optab, count, GEN_INT (-2), count, 1, 0);
3501         }
3502       if (temp != count)
3503         emit_move_insn (count, temp);
3504
3505       temp = expand_binop (mode, ashr_optab, count, GEN_INT (8), blocks, 1, 0);
3506       if (temp != blocks)
3507         emit_move_insn (blocks, temp);
3508
3509       emit_cmp_and_jump_insns (blocks, const0_rtx,
3510                                EQ, NULL_RTX, mode, 1, loop_end_label);
3511
3512       emit_label (loop_start_label);
3513
3514       if (val == const0_rtx)
3515         emit_insn (gen_clrmem_short (dst, GEN_INT (255)));
3516       else
3517         emit_insn (gen_movmem_short (dstp1, dst, GEN_INT (255)));
3518       s390_load_address (dst_addr,
3519                          gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (256)));
3520
3521       temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1, 0);
3522       if (temp != blocks)
3523         emit_move_insn (blocks, temp);
3524
3525       emit_cmp_and_jump_insns (blocks, const0_rtx,
3526                                EQ, NULL_RTX, mode, 1, loop_end_label);
3527
3528       emit_jump (loop_start_label);
3529       emit_label (loop_end_label);
3530
3531       if (val == const0_rtx)
3532         emit_insn (gen_clrmem_short (dst, convert_to_mode (Pmode, count, 1)));
3533       else
3534         emit_insn (gen_movmem_short (dstp1, dst, convert_to_mode (Pmode, count, 1)));
3535       emit_label (end_label);
3536     }
3537 }
3538
3539 /* Emit code to compare LEN bytes at OP0 with those at OP1,
3540    and return the result in TARGET.  */
3541
3542 void
3543 s390_expand_cmpmem (rtx target, rtx op0, rtx op1, rtx len)
3544 {
3545   rtx ccreg = gen_rtx_REG (CCUmode, CC_REGNUM);
3546   rtx tmp;
3547
3548   /* As the result of CMPINT is inverted compared to what we need,
3549      we have to swap the operands.  */
3550   tmp = op0; op0 = op1; op1 = tmp;
3551
3552   if (GET_CODE (len) == CONST_INT && INTVAL (len) >= 0 && INTVAL (len) <= 256)
3553     {
3554       if (INTVAL (len) > 0)
3555         {
3556           emit_insn (gen_cmpmem_short (op0, op1, GEN_INT (INTVAL (len) - 1)));
3557           emit_insn (gen_cmpint (target, ccreg));
3558         }
3559       else
3560         emit_move_insn (target, const0_rtx);
3561     }
3562   else if (TARGET_MVCLE)
3563     {
3564       emit_insn (gen_cmpmem_long (op0, op1, convert_to_mode (Pmode, len, 1)));
3565       emit_insn (gen_cmpint (target, ccreg));
3566     }
3567   else
3568     {
3569       rtx addr0, addr1, count, blocks, temp;
3570       rtx loop_start_label = gen_label_rtx ();
3571       rtx loop_end_label = gen_label_rtx ();
3572       rtx end_label = gen_label_rtx ();
3573       enum machine_mode mode;
3574
3575       mode = GET_MODE (len);
3576       if (mode == VOIDmode)
3577         mode = Pmode;
3578
3579       addr0 = gen_reg_rtx (Pmode);
3580       addr1 = gen_reg_rtx (Pmode);
3581       count = gen_reg_rtx (mode);
3582       blocks = gen_reg_rtx (mode);
3583
3584       convert_move (count, len, 1);
3585       emit_cmp_and_jump_insns (count, const0_rtx,
3586                                EQ, NULL_RTX, mode, 1, end_label);
3587
3588       emit_move_insn (addr0, force_operand (XEXP (op0, 0), NULL_RTX));
3589       emit_move_insn (addr1, force_operand (XEXP (op1, 0), NULL_RTX));
3590       op0 = change_address (op0, VOIDmode, addr0);
3591       op1 = change_address (op1, VOIDmode, addr1);
3592
3593       temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1, 0);
3594       if (temp != count)
3595         emit_move_insn (count, temp);
3596
3597       temp = expand_binop (mode, ashr_optab, count, GEN_INT (8), blocks, 1, 0);
3598       if (temp != blocks)
3599         emit_move_insn (blocks, temp);
3600
3601       emit_cmp_and_jump_insns (blocks, const0_rtx,
3602                                EQ, NULL_RTX, mode, 1, loop_end_label);
3603
3604       emit_label (loop_start_label);
3605
3606       emit_insn (gen_cmpmem_short (op0, op1, GEN_INT (255)));
3607       temp = gen_rtx_NE (VOIDmode, ccreg, const0_rtx);
3608       temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
3609                         gen_rtx_LABEL_REF (VOIDmode, end_label), pc_rtx);
3610       temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
3611       emit_jump_insn (temp);
3612
3613       s390_load_address (addr0,
3614                          gen_rtx_PLUS (Pmode, addr0, GEN_INT (256)));
3615       s390_load_address (addr1,
3616                          gen_rtx_PLUS (Pmode, addr1, GEN_INT (256)));
3617
3618       temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1, 0);
3619       if (temp != blocks)
3620         emit_move_insn (blocks, temp);
3621
3622       emit_cmp_and_jump_insns (blocks, const0_rtx,
3623                                EQ, NULL_RTX, mode, 1, loop_end_label);
3624
3625       emit_jump (loop_start_label);
3626       emit_label (loop_end_label);
3627
3628       emit_insn (gen_cmpmem_short (op0, op1,
3629                                    convert_to_mode (Pmode, count, 1)));
3630       emit_label (end_label);
3631
3632       emit_insn (gen_cmpint (target, ccreg));
3633     }
3634 }
3635
3636
3637 /* Expand conditional increment or decrement using alc/slb instructions.
3638    Should generate code setting DST to either SRC or SRC + INCREMENT,
3639    depending on the result of the comparison CMP_OP0 CMP_CODE CMP_OP1.
3640    Returns true if successful, false otherwise.
3641
3642    That makes it possible to implement some if-constructs without jumps e.g.:
3643    (borrow = CC0 | CC1 and carry = CC2 | CC3)
3644    unsigned int a, b, c;
3645    if (a < b)  c++; -> CCU  b > a  -> CC2;    c += carry;
3646    if (a < b)  c--; -> CCL3 a - b  -> borrow; c -= borrow;
3647    if (a <= b) c++; -> CCL3 b - a  -> borrow; c += carry;
3648    if (a <= b) c--; -> CCU  a <= b -> borrow; c -= borrow;
3649
3650    Checks for EQ and NE with a nonzero value need an additional xor e.g.:
3651    if (a == b) c++; -> CCL3 a ^= b; 0 - a  -> borrow;    c += carry;
3652    if (a == b) c--; -> CCU  a ^= b; a <= 0 -> CC0 | CC1; c -= borrow;
3653    if (a != b) c++; -> CCU  a ^= b; a > 0  -> CC2;       c += carry;
3654    if (a != b) c--; -> CCL3 a ^= b; 0 - a  -> borrow;    c -= borrow; */
3655
3656 bool
3657 s390_expand_addcc (enum rtx_code cmp_code, rtx cmp_op0, rtx cmp_op1,
3658                    rtx dst, rtx src, rtx increment)
3659 {
3660   enum machine_mode cmp_mode;
3661   enum machine_mode cc_mode;
3662   rtx op_res;
3663   rtx insn;
3664   rtvec p;
3665   int ret;
3666
3667   if ((GET_MODE (cmp_op0) == SImode || GET_MODE (cmp_op0) == VOIDmode)
3668       && (GET_MODE (cmp_op1) == SImode || GET_MODE (cmp_op1) == VOIDmode))
3669     cmp_mode = SImode;
3670   else if ((GET_MODE (cmp_op0) == DImode || GET_MODE (cmp_op0) == VOIDmode)
3671            && (GET_MODE (cmp_op1) == DImode || GET_MODE (cmp_op1) == VOIDmode))
3672     cmp_mode = DImode;
3673   else
3674     return false;
3675
3676   /* Try ADD LOGICAL WITH CARRY.  */
3677   if (increment == const1_rtx)
3678     {
3679       /* Determine CC mode to use.  */
3680       if (cmp_code == EQ || cmp_code == NE)
3681         {
3682           if (cmp_op1 != const0_rtx)
3683             {
3684               cmp_op0 = expand_simple_binop (cmp_mode, XOR, cmp_op0, cmp_op1,
3685                                              NULL_RTX, 0, OPTAB_WIDEN);
3686               cmp_op1 = const0_rtx;
3687             }
3688
3689           cmp_code = cmp_code == EQ ? LEU : GTU;
3690         }
3691
3692       if (cmp_code == LTU || cmp_code == LEU)
3693         {
3694           rtx tem = cmp_op0;
3695           cmp_op0 = cmp_op1;
3696           cmp_op1 = tem;
3697           cmp_code = swap_condition (cmp_code);
3698         }
3699
3700       switch (cmp_code)
3701         {
3702           case GTU:
3703             cc_mode = CCUmode;
3704             break;
3705
3706           case GEU:
3707             cc_mode = CCL3mode;
3708             break;
3709
3710           default:
3711             return false;
3712         }
3713
3714       /* Emit comparison instruction pattern. */
3715       if (!register_operand (cmp_op0, cmp_mode))
3716         cmp_op0 = force_reg (cmp_mode, cmp_op0);
3717
3718       insn = gen_rtx_SET (VOIDmode, gen_rtx_REG (cc_mode, CC_REGNUM),
3719                           gen_rtx_COMPARE (cc_mode, cmp_op0, cmp_op1));
3720       /* We use insn_invalid_p here to add clobbers if required.  */
3721       ret = insn_invalid_p (emit_insn (insn));
3722       gcc_assert (!ret);
3723
3724       /* Emit ALC instruction pattern.  */
3725       op_res = gen_rtx_fmt_ee (cmp_code, GET_MODE (dst),
3726                                gen_rtx_REG (cc_mode, CC_REGNUM),
3727                                const0_rtx);
3728
3729       if (src != const0_rtx)
3730         {
3731           if (!register_operand (src, GET_MODE (dst)))
3732             src = force_reg (GET_MODE (dst), src);
3733
3734           src = gen_rtx_PLUS (GET_MODE (dst), src, const0_rtx);
3735           op_res = gen_rtx_PLUS (GET_MODE (dst), src, op_res);
3736         }
3737
3738       p = rtvec_alloc (2);
3739       RTVEC_ELT (p, 0) = 
3740         gen_rtx_SET (VOIDmode, dst, op_res);
3741       RTVEC_ELT (p, 1) = 
3742         gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
3743       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
3744
3745       return true;
3746     }
3747
3748   /* Try SUBTRACT LOGICAL WITH BORROW.  */
3749   if (increment == constm1_rtx)
3750     {
3751       /* Determine CC mode to use.  */
3752       if (cmp_code == EQ || cmp_code == NE)
3753         {
3754           if (cmp_op1 != const0_rtx)
3755             {
3756               cmp_op0 = expand_simple_binop (cmp_mode, XOR, cmp_op0, cmp_op1,
3757                                              NULL_RTX, 0, OPTAB_WIDEN);
3758               cmp_op1 = const0_rtx;
3759             }
3760
3761           cmp_code = cmp_code == EQ ? LEU : GTU;
3762         }
3763
3764       if (cmp_code == GTU || cmp_code == GEU)
3765         {
3766           rtx tem = cmp_op0;
3767           cmp_op0 = cmp_op1;
3768           cmp_op1 = tem;
3769           cmp_code = swap_condition (cmp_code);
3770         }
3771
3772       switch (cmp_code)
3773         {
3774           case LEU:
3775             cc_mode = CCUmode;
3776             break;
3777
3778           case LTU:
3779             cc_mode = CCL3mode;
3780             break;
3781
3782           default:
3783             return false;
3784         }
3785
3786       /* Emit comparison instruction pattern. */
3787       if (!register_operand (cmp_op0, cmp_mode))
3788         cmp_op0 = force_reg (cmp_mode, cmp_op0);
3789
3790       insn = gen_rtx_SET (VOIDmode, gen_rtx_REG (cc_mode, CC_REGNUM),
3791                           gen_rtx_COMPARE (cc_mode, cmp_op0, cmp_op1));
3792       /* We use insn_invalid_p here to add clobbers if required.  */
3793       ret = insn_invalid_p (emit_insn (insn));
3794       gcc_assert (!ret);
3795
3796       /* Emit SLB instruction pattern.  */
3797       if (!register_operand (src, GET_MODE (dst)))
3798         src = force_reg (GET_MODE (dst), src);
3799
3800       op_res = gen_rtx_MINUS (GET_MODE (dst), 
3801                               gen_rtx_MINUS (GET_MODE (dst), src, const0_rtx), 
3802                               gen_rtx_fmt_ee (cmp_code, GET_MODE (dst), 
3803                                               gen_rtx_REG (cc_mode, CC_REGNUM), 
3804                                               const0_rtx));
3805       p = rtvec_alloc (2);
3806       RTVEC_ELT (p, 0) = 
3807         gen_rtx_SET (VOIDmode, dst, op_res);
3808       RTVEC_ELT (p, 1) = 
3809         gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
3810       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
3811
3812       return true;
3813     }
3814
3815   return false;
3816 }
3817
3818 /* Expand code for the insv template. Return true if successful, false else.  */
3819
3820 bool 
3821 s390_expand_insv (rtx dest, rtx op1, rtx op2, rtx src)
3822 {
3823   int bitsize = INTVAL (op1);
3824   int bitpos = INTVAL (op2);
3825
3826   /* We need byte alignement.  */
3827   if (bitsize % BITS_PER_UNIT)
3828     return false;
3829
3830   if (bitpos == 0
3831       && memory_operand (dest, VOIDmode)
3832       && (register_operand (src, word_mode)
3833           || const_int_operand (src, VOIDmode)))
3834     {
3835       /* Emit standard pattern if possible.  */
3836       enum machine_mode mode = smallest_mode_for_size (bitsize, MODE_INT);
3837       if (GET_MODE_BITSIZE (mode) == bitsize)
3838         emit_move_insn (adjust_address (dest, mode, 0), gen_lowpart (mode, src));
3839
3840       /* (set (ze (mem)) (const_int)).  */
3841       else if (const_int_operand (src, VOIDmode))
3842         {
3843           int size = bitsize / BITS_PER_UNIT;
3844           rtx src_mem = adjust_address (force_const_mem (word_mode, src), BLKmode,
3845                                         GET_MODE_SIZE (word_mode) - size);
3846
3847           dest = adjust_address (dest, BLKmode, 0);
3848           set_mem_size (dest, GEN_INT (size));
3849           s390_expand_movmem (dest, src_mem, GEN_INT (size));
3850         }
3851           
3852       /* (set (ze (mem)) (reg)).  */
3853       else if (register_operand (src, word_mode))
3854         {
3855           if (bitsize <= GET_MODE_BITSIZE (SImode))
3856             emit_move_insn (gen_rtx_ZERO_EXTRACT (word_mode, dest, op1,
3857                                                   const0_rtx), src);
3858           else
3859             {
3860               /* Emit st,stcmh sequence.  */
3861               int stcmh_width = bitsize - GET_MODE_BITSIZE (SImode);
3862               int size = stcmh_width / BITS_PER_UNIT;
3863
3864               emit_move_insn (adjust_address (dest, SImode, size), 
3865                               gen_lowpart (SImode, src));
3866               set_mem_size (dest, GEN_INT (size));
3867               emit_move_insn (gen_rtx_ZERO_EXTRACT (word_mode, dest, GEN_INT
3868                                                     (stcmh_width), const0_rtx),
3869                               gen_rtx_LSHIFTRT (word_mode, src, GEN_INT
3870                                                 (GET_MODE_BITSIZE (SImode))));
3871             }
3872         }
3873       else
3874         return false;
3875
3876       return true;
3877     }
3878
3879   /* (set (ze (reg)) (const_int)).  */
3880   if (TARGET_ZARCH
3881       && register_operand (dest, word_mode) 
3882       && (bitpos % 16) == 0
3883       && (bitsize % 16) == 0
3884       && const_int_operand (src, VOIDmode))
3885     {
3886       HOST_WIDE_INT val = INTVAL (src);
3887       int regpos = bitpos + bitsize;
3888
3889       while (regpos > bitpos)
3890         {
3891           enum machine_mode putmode;
3892           int putsize;
3893
3894           if (TARGET_EXTIMM && (regpos % 32 == 0) && (regpos >= bitpos + 32))
3895             putmode = SImode;
3896           else
3897             putmode = HImode;
3898
3899           putsize = GET_MODE_BITSIZE (putmode);
3900           regpos -= putsize;
3901           emit_move_insn (gen_rtx_ZERO_EXTRACT (word_mode, dest, 
3902                                                 GEN_INT (putsize),
3903                                                 GEN_INT (regpos)), 
3904                           gen_int_mode (val, putmode));
3905           val >>= putsize;
3906         }
3907       gcc_assert (regpos == bitpos);
3908       return true;
3909     }
3910
3911   return false;
3912 }
3913
3914 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
3915    We need to emit DTP-relative relocations.  */
3916
3917 static void s390_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
3918
3919 static void
3920 s390_output_dwarf_dtprel (FILE *file, int size, rtx x)
3921 {
3922   switch (size)
3923     {
3924     case 4:
3925       fputs ("\t.long\t", file);
3926       break;
3927     case 8:
3928       fputs ("\t.quad\t", file);
3929       break;
3930     default:
3931       gcc_unreachable ();
3932     }
3933   output_addr_const (file, x);
3934   fputs ("@DTPOFF", file);
3935 }
3936
3937 /* In the name of slightly smaller debug output, and to cater to
3938    general assembler lossage, recognize various UNSPEC sequences
3939    and turn them back into a direct symbol reference.  */
3940
3941 static rtx
3942 s390_delegitimize_address (rtx orig_x)
3943 {
3944   rtx x = orig_x, y;
3945
3946   if (GET_CODE (x) != MEM)
3947     return orig_x;
3948
3949   x = XEXP (x, 0);
3950   if (GET_CODE (x) == PLUS
3951       && GET_CODE (XEXP (x, 1)) == CONST
3952       && GET_CODE (XEXP (x, 0)) == REG
3953       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
3954     {
3955       y = XEXP (XEXP (x, 1), 0);
3956       if (GET_CODE (y) == UNSPEC
3957           && XINT (y, 1) == UNSPEC_GOT)
3958         return XVECEXP (y, 0, 0);
3959       return orig_x;
3960     }
3961
3962   if (GET_CODE (x) == CONST)
3963     {
3964       y = XEXP (x, 0);
3965       if (GET_CODE (y) == UNSPEC
3966           && XINT (y, 1) == UNSPEC_GOTENT)
3967         return XVECEXP (y, 0, 0);
3968       return orig_x;
3969     }
3970
3971   return orig_x;
3972 }
3973
3974 /* Output operand OP to stdio stream FILE.
3975    OP is an address (register + offset) which is not used to address data;
3976    instead the rightmost bits are interpreted as the value.  */
3977
3978 static void
3979 print_shift_count_operand (FILE *file, rtx op)
3980 {
3981   HOST_WIDE_INT offset = 0;
3982
3983   /* Shift count operands are always truncated to the 6 least significant bits and
3984      the setmem padding byte to the least 8 significant bits.  Hence we can drop
3985      pointless ANDs.  */
3986   if (GET_CODE (op) == AND && GET_CODE (XEXP (op, 1)) == CONST_INT)
3987     {
3988       if ((INTVAL (XEXP (op, 1)) & 63) != 63)
3989         gcc_unreachable ();
3990
3991       op = XEXP (op, 0);
3992     }
3993
3994   /* We can have an integer constant, an address register,
3995      or a sum of the two.  */
3996   if (GET_CODE (op) == CONST_INT)
3997     {
3998       offset = INTVAL (op);
3999       op = NULL_RTX;
4000     }
4001   if (op && GET_CODE (op) == PLUS && GET_CODE (XEXP (op, 1)) == CONST_INT)
4002     {
4003       offset = INTVAL (XEXP (op, 1));
4004       op = XEXP (op, 0);
4005     }
4006   while (op && GET_CODE (op) == SUBREG)
4007     op = SUBREG_REG (op);
4008
4009   /* Sanity check.  */
4010   if (op)
4011     {
4012       gcc_assert (GET_CODE (op) == REG);
4013       gcc_assert (REGNO (op) < FIRST_PSEUDO_REGISTER);
4014       gcc_assert (REGNO_REG_CLASS (REGNO (op)) == ADDR_REGS);
4015     }
4016
4017   /* Offsets are constricted to twelve bits.  */
4018   fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset & ((1 << 12) - 1));
4019   if (op)
4020     fprintf (file, "(%s)", reg_names[REGNO (op)]);
4021 }
4022
4023 /* See 'get_some_local_dynamic_name'.  */
4024
4025 static int
4026 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
4027 {
4028   rtx x = *px;
4029
4030   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
4031     {
4032       x = get_pool_constant (x);
4033       return for_each_rtx (&x, get_some_local_dynamic_name_1, 0);
4034     }
4035
4036   if (GET_CODE (x) == SYMBOL_REF
4037       && tls_symbolic_operand (x) == TLS_MODEL_LOCAL_DYNAMIC)
4038     {
4039       cfun->machine->some_ld_name = XSTR (x, 0);
4040       return 1;
4041     }
4042
4043   return 0;
4044 }
4045
4046 /* Locate some local-dynamic symbol still in use by this function
4047    so that we can print its name in local-dynamic base patterns.  */
4048
4049 static const char *
4050 get_some_local_dynamic_name (void)
4051 {
4052   rtx insn;
4053
4054   if (cfun->machine->some_ld_name)
4055     return cfun->machine->some_ld_name;
4056
4057   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
4058     if (INSN_P (insn)
4059         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
4060       return cfun->machine->some_ld_name;
4061
4062   gcc_unreachable ();
4063 }
4064
4065 /* Output machine-dependent UNSPECs occurring in address constant X
4066    in assembler syntax to stdio stream FILE.  Returns true if the
4067    constant X could be recognized, false otherwise.  */
4068
4069 bool
4070 s390_output_addr_const_extra (FILE *file, rtx x)
4071 {
4072   if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 1)
4073     switch (XINT (x, 1))
4074       {
4075       case UNSPEC_GOTENT:
4076         output_addr_const (file, XVECEXP (x, 0, 0));
4077         fprintf (file, "@GOTENT");
4078         return true;
4079       case UNSPEC_GOT:
4080         output_addr_const (file, XVECEXP (x, 0, 0));
4081         fprintf (file, "@GOT");
4082         return true;
4083       case UNSPEC_GOTOFF:
4084         output_addr_const (file, XVECEXP (x, 0, 0));
4085         fprintf (file, "@GOTOFF");
4086         return true;
4087       case UNSPEC_PLT:
4088         output_addr_const (file, XVECEXP (x, 0, 0));
4089         fprintf (file, "@PLT");
4090         return true;
4091       case UNSPEC_PLTOFF:
4092         output_addr_const (file, XVECEXP (x, 0, 0));
4093         fprintf (file, "@PLTOFF");
4094         return true;
4095       case UNSPEC_TLSGD:
4096         output_addr_const (file, XVECEXP (x, 0, 0));
4097         fprintf (file, "@TLSGD");
4098         return true;
4099       case UNSPEC_TLSLDM:
4100         assemble_name (file, get_some_local_dynamic_name ());
4101         fprintf (file, "@TLSLDM");
4102         return true;
4103       case UNSPEC_DTPOFF:
4104         output_addr_const (file, XVECEXP (x, 0, 0));
4105         fprintf (file, "@DTPOFF");
4106         return true;
4107       case UNSPEC_NTPOFF:
4108         output_addr_const (file, XVECEXP (x, 0, 0));
4109         fprintf (file, "@NTPOFF");
4110         return true;
4111       case UNSPEC_GOTNTPOFF:
4112         output_addr_const (file, XVECEXP (x, 0, 0));
4113         fprintf (file, "@GOTNTPOFF");
4114         return true;
4115       case UNSPEC_INDNTPOFF:
4116         output_addr_const (file, XVECEXP (x, 0, 0));
4117         fprintf (file, "@INDNTPOFF");
4118         return true;
4119       }
4120
4121   return false;
4122 }
4123
4124 /* Output address operand ADDR in assembler syntax to
4125    stdio stream FILE.  */
4126
4127 void
4128 print_operand_address (FILE *file, rtx addr)
4129 {
4130   struct s390_address ad;
4131
4132   if (!s390_decompose_address (addr, &ad)
4133       || (ad.base && !REG_OK_FOR_BASE_STRICT_P (ad.base))
4134       || (ad.indx && !REG_OK_FOR_INDEX_STRICT_P (ad.indx)))
4135     output_operand_lossage ("cannot decompose address");
4136
4137   if (ad.disp)
4138     output_addr_const (file, ad.disp);
4139   else
4140     fprintf (file, "0");
4141
4142   if (ad.base && ad.indx)
4143     fprintf (file, "(%s,%s)", reg_names[REGNO (ad.indx)],
4144                               reg_names[REGNO (ad.base)]);
4145   else if (ad.base)
4146     fprintf (file, "(%s)", reg_names[REGNO (ad.base)]);
4147 }
4148
4149 /* Output operand X in assembler syntax to stdio stream FILE.
4150    CODE specified the format flag.  The following format flags
4151    are recognized:
4152
4153     'C': print opcode suffix for branch condition.
4154     'D': print opcode suffix for inverse branch condition.
4155     'J': print tls_load/tls_gdcall/tls_ldcall suffix
4156     'G': print the size of the operand in bytes.
4157     'O': print only the displacement of a memory reference.
4158     'R': print only the base register of a memory reference.
4159     'S': print S-type memory reference (base+displacement).
4160     'N': print the second word of a DImode operand.
4161     'M': print the second word of a TImode operand.
4162     'Y': print shift count operand.
4163
4164     'b': print integer X as if it's an unsigned byte.
4165     'x': print integer X as if it's an unsigned halfword.
4166     'h': print integer X as if it's a signed halfword.
4167     'i': print the first nonzero HImode part of X.
4168     'j': print the first HImode part unequal to -1 of X.
4169     'k': print the first nonzero SImode part of X.
4170     'm': print the first SImode part unequal to -1 of X.
4171     'o': print integer X as if it's an unsigned 32bit word.  */
4172
4173 void
4174 print_operand (FILE *file, rtx x, int code)
4175 {
4176   switch (code)
4177     {
4178     case 'C':
4179       fprintf (file, s390_branch_condition_mnemonic (x, FALSE));
4180       return;
4181
4182     case 'D':
4183       fprintf (file, s390_branch_condition_mnemonic (x, TRUE));
4184       return;
4185
4186     case 'J':
4187       if (GET_CODE (x) == SYMBOL_REF)
4188         {
4189           fprintf (file, "%s", ":tls_load:");
4190           output_addr_const (file, x);
4191         }
4192       else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSGD)
4193         {
4194           fprintf (file, "%s", ":tls_gdcall:");
4195           output_addr_const (file, XVECEXP (x, 0, 0));
4196         }
4197       else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSLDM)
4198         {
4199           fprintf (file, "%s", ":tls_ldcall:");
4200           assemble_name (file, get_some_local_dynamic_name ());
4201         }
4202       else
4203         gcc_unreachable ();
4204       return;
4205
4206     case 'G':
4207       fprintf (file, "%u", GET_MODE_SIZE (GET_MODE (x)));
4208       return;
4209
4210     case 'O':
4211       {
4212         struct s390_address ad;
4213         int ret;
4214
4215         gcc_assert (GET_CODE (x) == MEM);
4216         ret = s390_decompose_address (XEXP (x, 0), &ad);
4217         gcc_assert (ret);
4218         gcc_assert (!ad.base || REG_OK_FOR_BASE_STRICT_P (ad.base));
4219         gcc_assert (!ad.indx);
4220
4221         if (ad.disp)
4222           output_addr_const (file, ad.disp);
4223         else
4224           fprintf (file, "0");
4225       }
4226       return;
4227
4228     case 'R':
4229       {
4230         struct s390_address ad;
4231         int ret;
4232
4233         gcc_assert (GET_CODE (x) == MEM);
4234         ret = s390_decompose_address (XEXP (x, 0), &ad);
4235         gcc_assert (ret);
4236         gcc_assert (!ad.base || REG_OK_FOR_BASE_STRICT_P (ad.base));
4237         gcc_assert (!ad.indx);
4238
4239         if (ad.base)
4240           fprintf (file, "%s", reg_names[REGNO (ad.base)]);
4241         else
4242           fprintf (file, "0");
4243       }
4244       return;
4245
4246     case 'S':
4247       {
4248         struct s390_address ad;
4249         int ret;
4250
4251         gcc_assert (GET_CODE (x) == MEM);
4252         ret = s390_decompose_address (XEXP (x, 0), &ad);
4253         gcc_assert (ret);
4254         gcc_assert (!ad.base || REG_OK_FOR_BASE_STRICT_P (ad.base));
4255         gcc_assert (!ad.indx);
4256
4257         if (ad.disp)
4258           output_addr_const (file, ad.disp);
4259         else
4260           fprintf (file, "0");
4261
4262         if (ad.base)
4263           fprintf (file, "(%s)", reg_names[REGNO (ad.base)]);
4264       }
4265       return;
4266
4267     case 'N':
4268       if (GET_CODE (x) == REG)
4269         x = gen_rtx_REG (GET_MODE (x), REGNO (x) + 1);
4270       else if (GET_CODE (x) == MEM)
4271         x = change_address (x, VOIDmode, plus_constant (XEXP (x, 0), 4));
4272       else
4273         gcc_unreachable ();
4274       break;
4275
4276     case 'M':
4277       if (GET_CODE (x) == REG)
4278         x = gen_rtx_REG (GET_MODE (x), REGNO (x) + 1);
4279       else if (GET_CODE (x) == MEM)
4280         x = change_address (x, VOIDmode, plus_constant (XEXP (x, 0), 8));
4281       else
4282         gcc_unreachable ();
4283       break;
4284
4285     case 'Y':
4286       print_shift_count_operand (file, x);
4287       return;
4288     }
4289
4290   switch (GET_CODE (x))
4291     {
4292     case REG:
4293       fprintf (file, "%s", reg_names[REGNO (x)]);
4294       break;
4295
4296     case MEM:
4297       output_address (XEXP (x, 0));
4298       break;
4299
4300     case CONST:
4301     case CODE_LABEL:
4302     case LABEL_REF:
4303     case SYMBOL_REF:
4304       output_addr_const (file, x);
4305       break;
4306
4307     case CONST_INT:
4308       if (code == 'b')
4309         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0xff);
4310       else if (code == 'x')
4311         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0xffff);
4312       else if (code == 'h')
4313         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ((INTVAL (x) & 0xffff) ^ 0x8000) - 0x8000);
4314       else if (code == 'i')
4315         fprintf (file, HOST_WIDE_INT_PRINT_DEC,
4316                  s390_extract_part (x, HImode, 0));
4317       else if (code == 'j')
4318         fprintf (file, HOST_WIDE_INT_PRINT_DEC,
4319                  s390_extract_part (x, HImode, -1));
4320       else if (code == 'k')
4321         fprintf (file, HOST_WIDE_INT_PRINT_DEC,
4322                  s390_extract_part (x, SImode, 0));
4323       else if (code == 'm')
4324         fprintf (file, HOST_WIDE_INT_PRINT_DEC,
4325                  s390_extract_part (x, SImode, -1));
4326       else if (code == 'o')
4327         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0xffffffff);
4328       else
4329         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
4330       break;
4331
4332     case CONST_DOUBLE:
4333       gcc_assert (GET_MODE (x) == VOIDmode);
4334       if (code == 'b')
4335         fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x) & 0xff);
4336       else if (code == 'x')
4337         fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x) & 0xffff);
4338       else if (code == 'h')
4339         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ((CONST_DOUBLE_LOW (x) & 0xffff) ^ 0x8000) - 0x8000);
4340       else
4341         gcc_unreachable ();
4342       break;
4343
4344     default:
4345       fatal_insn ("UNKNOWN in print_operand !?", x);
4346       break;
4347     }
4348 }
4349
4350 /* Target hook for assembling integer objects.  We need to define it
4351    here to work a round a bug in some versions of GAS, which couldn't
4352    handle values smaller than INT_MIN when printed in decimal.  */
4353
4354 static bool
4355 s390_assemble_integer (rtx x, unsigned int size, int aligned_p)
4356 {
4357   if (size == 8 && aligned_p
4358       && GET_CODE (x) == CONST_INT && INTVAL (x) < INT_MIN)
4359     {
4360       fprintf (asm_out_file, "\t.quad\t" HOST_WIDE_INT_PRINT_HEX "\n",
4361                INTVAL (x));
4362       return true;
4363     }
4364   return default_assemble_integer (x, size, aligned_p);
4365 }
4366
4367 /* Returns true if register REGNO is used  for forming
4368    a memory address in expression X.  */
4369
4370 static bool
4371 reg_used_in_mem_p (int regno, rtx x)
4372 {
4373   enum rtx_code code = GET_CODE (x);
4374   int i, j;
4375   const char *fmt;
4376
4377   if (code == MEM)
4378     {
4379       if (refers_to_regno_p (regno, regno+1,
4380                              XEXP (x, 0), 0))
4381         return true;
4382     }
4383   else if (code == SET
4384            && GET_CODE (SET_DEST (x)) == PC)
4385     {
4386       if (refers_to_regno_p (regno, regno+1,
4387                              SET_SRC (x), 0))
4388         return true;
4389     }
4390
4391   fmt = GET_RTX_FORMAT (code);
4392   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4393     {
4394       if (fmt[i] == 'e'
4395           && reg_used_in_mem_p (regno, XEXP (x, i)))
4396         return true;
4397
4398       else if (fmt[i] == 'E')
4399         for (j = 0; j < XVECLEN (x, i); j++)
4400           if (reg_used_in_mem_p (regno, XVECEXP (x, i, j)))
4401             return true;
4402     }
4403   return false;
4404 }
4405
4406 /* Returns true if expression DEP_RTX sets an address register
4407    used by instruction INSN to address memory.  */
4408
4409 static bool
4410 addr_generation_dependency_p (rtx dep_rtx, rtx insn)
4411 {
4412   rtx target, pat;
4413
4414   if (GET_CODE (dep_rtx) == INSN)
4415       dep_rtx = PATTERN (dep_rtx);
4416
4417   if (GET_CODE (dep_rtx) == SET)
4418     {
4419       target = SET_DEST (dep_rtx);
4420       if (GET_CODE (target) == STRICT_LOW_PART)
4421         target = XEXP (target, 0);
4422       while (GET_CODE (target) == SUBREG)
4423         target = SUBREG_REG (target);
4424
4425       if (GET_CODE (target) == REG)
4426         {
4427           int regno = REGNO (target);
4428
4429           if (s390_safe_attr_type (insn) == TYPE_LA)
4430             {
4431               pat = PATTERN (insn);
4432               if (GET_CODE (pat) == PARALLEL)
4433                 {
4434                   gcc_assert (XVECLEN (pat, 0) == 2);
4435                   pat = XVECEXP (pat, 0, 0);
4436                 }
4437               gcc_assert (GET_CODE (pat) == SET);
4438               return refers_to_regno_p (regno, regno+1, SET_SRC (pat), 0);
4439             }
4440           else if (get_attr_atype (insn) == ATYPE_AGEN)
4441             return reg_used_in_mem_p (regno, PATTERN (insn));
4442         }
4443     }
4444   return false;
4445 }
4446
4447 /* Return 1, if dep_insn sets register used in insn in the agen unit.  */
4448
4449 int
4450 s390_agen_dep_p (rtx dep_insn, rtx insn)
4451 {
4452   rtx dep_rtx = PATTERN (dep_insn);
4453   int i;
4454
4455   if (GET_CODE (dep_rtx) == SET
4456       && addr_generation_dependency_p (dep_rtx, insn))
4457     return 1;
4458   else if (GET_CODE (dep_rtx) == PARALLEL)
4459     {
4460       for (i = 0; i < XVECLEN (dep_rtx, 0); i++)
4461         {
4462           if (addr_generation_dependency_p (XVECEXP (dep_rtx, 0, i), insn))
4463             return 1;
4464         }
4465     }
4466   return 0;
4467 }
4468
4469 /* A C statement (sans semicolon) to update the integer scheduling priority
4470    INSN_PRIORITY (INSN).  Increase the priority to execute the INSN earlier,
4471    reduce the priority to execute INSN later.  Do not define this macro if
4472    you do not need to adjust the scheduling priorities of insns.
4473
4474    A STD instruction should be scheduled earlier,
4475    in order to use the bypass.  */
4476
4477 static int
4478 s390_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
4479 {
4480   if (! INSN_P (insn))
4481     return priority;
4482
4483   if (s390_tune != PROCESSOR_2084_Z990
4484       && s390_tune != PROCESSOR_2094_Z9_109)
4485     return priority;
4486
4487   switch (s390_safe_attr_type (insn))
4488     {
4489       case TYPE_FSTOREDF:
4490       case TYPE_FSTORESF:
4491         priority = priority << 3;
4492         break;
4493       case TYPE_STORE:
4494       case TYPE_STM:
4495         priority = priority << 1;
4496         break;
4497       default:
4498         break;
4499     }
4500   return priority;
4501 }
4502
4503 /* The number of instructions that can be issued per cycle.  */
4504
4505 static int
4506 s390_issue_rate (void)
4507 {
4508   if (s390_tune == PROCESSOR_2084_Z990
4509       || s390_tune == PROCESSOR_2094_Z9_109)
4510     return 3;
4511   return 1;
4512 }
4513
4514 static int
4515 s390_first_cycle_multipass_dfa_lookahead (void)
4516 {
4517   return 4;
4518 }
4519
4520
4521 /* Annotate every literal pool reference in X by an UNSPEC_LTREF expression.
4522    Fix up MEMs as required.  */
4523
4524 static void
4525 annotate_constant_pool_refs (rtx *x)
4526 {
4527   int i, j;
4528   const char *fmt;
4529
4530   gcc_assert (GET_CODE (*x) != SYMBOL_REF
4531               || !CONSTANT_POOL_ADDRESS_P (*x));
4532
4533   /* Literal pool references can only occur inside a MEM ...  */
4534   if (GET_CODE (*x) == MEM)
4535     {
4536       rtx memref = XEXP (*x, 0);
4537
4538       if (GET_CODE (memref) == SYMBOL_REF
4539           && CONSTANT_POOL_ADDRESS_P (memref))
4540         {
4541           rtx base = cfun->machine->base_reg;
4542           rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, memref, base),
4543                                      UNSPEC_LTREF);
4544
4545           *x = replace_equiv_address (*x, addr);
4546           return;
4547         }
4548
4549       if (GET_CODE (memref) == CONST
4550           && GET_CODE (XEXP (memref, 0)) == PLUS
4551           && GET_CODE (XEXP (XEXP (memref, 0), 1)) == CONST_INT
4552           && GET_CODE (XEXP (XEXP (memref, 0), 0)) == SYMBOL_REF
4553           && CONSTANT_POOL_ADDRESS_P (XEXP (XEXP (memref, 0), 0)))
4554         {
4555           HOST_WIDE_INT off = INTVAL (XEXP (XEXP (memref, 0), 1));
4556           rtx sym = XEXP (XEXP (memref, 0), 0);
4557           rtx base = cfun->machine->base_reg;
4558           rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, sym, base),
4559                                      UNSPEC_LTREF);
4560
4561           *x = replace_equiv_address (*x, plus_constant (addr, off));
4562           return;
4563         }
4564     }
4565
4566   /* ... or a load-address type pattern.  */
4567   if (GET_CODE (*x) == SET)
4568     {
4569       rtx addrref = SET_SRC (*x);
4570
4571       if (GET_CODE (addrref) == SYMBOL_REF
4572           && CONSTANT_POOL_ADDRESS_P (addrref))
4573         {
4574           rtx base = cfun->machine->base_reg;
4575           rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, addrref, base),
4576                                      UNSPEC_LTREF);
4577
4578           SET_SRC (*x) = addr;
4579           return;
4580         }
4581
4582       if (GET_CODE (addrref) == CONST
4583           && GET_CODE (XEXP (addrref, 0)) == PLUS
4584           && GET_CODE (XEXP (XEXP (addrref, 0), 1)) == CONST_INT
4585           && GET_CODE (XEXP (XEXP (addrref, 0), 0)) == SYMBOL_REF
4586           && CONSTANT_POOL_ADDRESS_P (XEXP (XEXP (addrref, 0), 0)))
4587         {
4588           HOST_WIDE_INT off = INTVAL (XEXP (XEXP (addrref, 0), 1));
4589           rtx sym = XEXP (XEXP (addrref, 0), 0);
4590           rtx base = cfun->machine->base_reg;
4591           rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, sym, base),
4592                                      UNSPEC_LTREF);
4593
4594           SET_SRC (*x) = plus_constant (addr, off);
4595           return;
4596         }
4597     }
4598
4599   /* Annotate LTREL_BASE as well.  */
4600   if (GET_CODE (*x) == UNSPEC
4601       && XINT (*x, 1) == UNSPEC_LTREL_BASE)
4602     {
4603       rtx base = cfun->machine->base_reg;
4604       *x = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, XVECEXP (*x, 0, 0), base),
4605                                   UNSPEC_LTREL_BASE);
4606       return;
4607     }
4608
4609   fmt = GET_RTX_FORMAT (GET_CODE (*x));
4610   for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
4611     {
4612       if (fmt[i] == 'e')
4613         {
4614           annotate_constant_pool_refs (&XEXP (*x, i));
4615         }
4616       else if (fmt[i] == 'E')
4617         {
4618           for (j = 0; j < XVECLEN (*x, i); j++)
4619             annotate_constant_pool_refs (&XVECEXP (*x, i, j));
4620         }
4621     }
4622 }
4623
4624 /* Split all branches that exceed the maximum distance.
4625    Returns true if this created a new literal pool entry.  */
4626
4627 static int
4628 s390_split_branches (void)
4629 {
4630   rtx temp_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
4631   int new_literal = 0, ret;
4632   rtx insn, pat, tmp, target;
4633   rtx *label;
4634
4635   /* We need correct insn addresses.  */
4636
4637   shorten_branches (get_insns ());
4638
4639   /* Find all branches that exceed 64KB, and split them.  */
4640
4641   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4642     {
4643       if (GET_CODE (insn) != JUMP_INSN)
4644         continue;
4645
4646       pat = PATTERN (insn);
4647       if (GET_CODE (pat) == PARALLEL && XVECLEN (pat, 0) > 2)
4648         pat = XVECEXP (pat, 0, 0);
4649       if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
4650         continue;
4651
4652       if (GET_CODE (SET_SRC (pat)) == LABEL_REF)
4653         {
4654           label = &SET_SRC (pat);
4655         }
4656       else if (GET_CODE (SET_SRC (pat)) == IF_THEN_ELSE)
4657         {
4658           if (GET_CODE (XEXP (SET_SRC (pat), 1)) == LABEL_REF)
4659             label = &XEXP (SET_SRC (pat), 1);
4660           else if (GET_CODE (XEXP (SET_SRC (pat), 2)) == LABEL_REF)
4661             label = &XEXP (SET_SRC (pat), 2);
4662           else
4663             continue;
4664         }
4665       else
4666         continue;
4667
4668       if (get_attr_length (insn) <= 4)
4669         continue;
4670
4671       /* We are going to use the return register as scratch register,
4672          make sure it will be saved/restored by the prologue/epilogue.  */
4673       cfun_frame_layout.save_return_addr_p = 1;
4674
4675       if (!flag_pic)
4676         {
4677           new_literal = 1;
4678           tmp = force_const_mem (Pmode, *label);
4679           tmp = emit_insn_before (gen_rtx_SET (Pmode, temp_reg, tmp), insn);
4680           INSN_ADDRESSES_NEW (tmp, -1);
4681           annotate_constant_pool_refs (&PATTERN (tmp));
4682
4683           target = temp_reg;
4684         }
4685       else
4686         {
4687           new_literal = 1;
4688           target = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, *label),
4689                                    UNSPEC_LTREL_OFFSET);
4690           target = gen_rtx_CONST (Pmode, target);
4691           target = force_const_mem (Pmode, target);
4692           tmp = emit_insn_before (gen_rtx_SET (Pmode, temp_reg, target), insn);
4693           INSN_ADDRESSES_NEW (tmp, -1);
4694           annotate_constant_pool_refs (&PATTERN (tmp));
4695
4696           target = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, XEXP (target, 0),
4697                                                         cfun->machine->base_reg),
4698                                    UNSPEC_LTREL_BASE);
4699           target = gen_rtx_PLUS (Pmode, temp_reg, target);
4700         }
4701
4702       ret = validate_change (insn, label, target, 0);
4703       gcc_assert (ret);
4704     }
4705
4706   return new_literal;
4707 }
4708
4709
4710 /* Find an annotated literal pool symbol referenced in RTX X, 
4711    and store it at REF.  Will abort if X contains references to 
4712    more than one such pool symbol; multiple references to the same
4713    symbol are allowed, however.
4714
4715    The rtx pointed to by REF must be initialized to NULL_RTX
4716    by the caller before calling this routine.  */
4717
4718 static void
4719 find_constant_pool_ref (rtx x, rtx *ref)
4720 {
4721   int i, j;
4722   const char *fmt;
4723
4724   /* Ignore LTREL_BASE references.  */
4725   if (GET_CODE (x) == UNSPEC
4726       && XINT (x, 1) == UNSPEC_LTREL_BASE)
4727     return;
4728   /* Likewise POOL_ENTRY insns.  */
4729   if (GET_CODE (x) == UNSPEC_VOLATILE
4730       && XINT (x, 1) == UNSPECV_POOL_ENTRY)
4731     return;
4732
4733   gcc_assert (GET_CODE (x) != SYMBOL_REF
4734               || !CONSTANT_POOL_ADDRESS_P (x));
4735
4736   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_LTREF)
4737     {
4738       rtx sym = XVECEXP (x, 0, 0);
4739       gcc_assert (GET_CODE (sym) == SYMBOL_REF
4740                   && CONSTANT_POOL_ADDRESS_P (sym));
4741
4742       if (*ref == NULL_RTX)
4743         *ref = sym;
4744       else 
4745         gcc_assert (*ref == sym);
4746
4747       return;
4748     }
4749
4750   fmt = GET_RTX_FORMAT (GET_CODE (x));
4751   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
4752     {
4753       if (fmt[i] == 'e')
4754         {
4755           find_constant_pool_ref (XEXP (x, i), ref);
4756         }
4757       else if (fmt[i] == 'E')
4758         {
4759           for (j = 0; j < XVECLEN (x, i); j++)
4760             find_constant_pool_ref (XVECEXP (x, i, j), ref);
4761         }
4762     }
4763 }
4764
4765 /* Replace every reference to the annotated literal pool 
4766    symbol REF in X by its base plus OFFSET.  */
4767
4768 static void
4769 replace_constant_pool_ref (rtx *x, rtx ref, rtx offset)
4770 {
4771   int i, j;
4772   const char *fmt;
4773
4774   gcc_assert (*x != ref);
4775
4776   if (GET_CODE (*x) == UNSPEC
4777       && XINT (*x, 1) == UNSPEC_LTREF
4778       && XVECEXP (*x, 0, 0) == ref)
4779     {
4780       *x = gen_rtx_PLUS (Pmode, XVECEXP (*x, 0, 1), offset);
4781       return;
4782     }
4783
4784   if (GET_CODE (*x) == PLUS
4785       && GET_CODE (XEXP (*x, 1)) == CONST_INT
4786       && GET_CODE (XEXP (*x, 0)) == UNSPEC
4787       && XINT (XEXP (*x, 0), 1) == UNSPEC_LTREF
4788       && XVECEXP (XEXP (*x, 0), 0, 0) == ref)
4789     {
4790       rtx addr = gen_rtx_PLUS (Pmode, XVECEXP (XEXP (*x, 0), 0, 1), offset);
4791       *x = plus_constant (addr, INTVAL (XEXP (*x, 1)));
4792       return;
4793     }
4794
4795   fmt = GET_RTX_FORMAT (GET_CODE (*x));
4796   for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
4797     {
4798       if (fmt[i] == 'e')
4799         {
4800           replace_constant_pool_ref (&XEXP (*x, i), ref, offset);
4801         }
4802       else if (fmt[i] == 'E')
4803         {
4804           for (j = 0; j < XVECLEN (*x, i); j++)
4805             replace_constant_pool_ref (&XVECEXP (*x, i, j), ref, offset);
4806         }
4807     }
4808 }
4809
4810 /* Check whether X contains an UNSPEC_LTREL_BASE.
4811    Return its constant pool symbol if found, NULL_RTX otherwise.  */
4812
4813 static rtx
4814 find_ltrel_base (rtx x)
4815 {
4816   int i, j;
4817   const char *fmt;
4818
4819   if (GET_CODE (x) == UNSPEC
4820       && XINT (x, 1) == UNSPEC_LTREL_BASE)
4821     return XVECEXP (x, 0, 0);
4822
4823   fmt = GET_RTX_FORMAT (GET_CODE (x));
4824   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
4825     {
4826       if (fmt[i] == 'e')
4827         {
4828           rtx fnd = find_ltrel_base (XEXP (x, i));
4829           if (fnd)
4830             return fnd;
4831         }
4832       else if (fmt[i] == 'E')
4833         {
4834           for (j = 0; j < XVECLEN (x, i); j++)
4835             {
4836               rtx fnd = find_ltrel_base (XVECEXP (x, i, j));
4837               if (fnd)
4838                 return fnd;
4839             }
4840         }
4841     }
4842
4843   return NULL_RTX;
4844 }
4845
4846 /* Replace any occurrence of UNSPEC_LTREL_BASE in X with its base.  */
4847
4848 static void
4849 replace_ltrel_base (rtx *x)
4850 {
4851   int i, j;
4852   const char *fmt;
4853
4854   if (GET_CODE (*x) == UNSPEC
4855       && XINT (*x, 1) == UNSPEC_LTREL_BASE)
4856     {
4857       *x = XVECEXP (*x, 0, 1);
4858       return;
4859     }
4860
4861   fmt = GET_RTX_FORMAT (GET_CODE (*x));
4862   for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
4863     {
4864       if (fmt[i] == 'e')
4865         {
4866           replace_ltrel_base (&XEXP (*x, i));
4867         }
4868       else if (fmt[i] == 'E')
4869         {
4870           for (j = 0; j < XVECLEN (*x, i); j++)
4871             replace_ltrel_base (&XVECEXP (*x, i, j));
4872         }
4873     }
4874 }
4875
4876
4877 /* We keep a list of constants which we have to add to internal
4878    constant tables in the middle of large functions.  */
4879
4880 #define NR_C_MODES 7
4881 enum machine_mode constant_modes[NR_C_MODES] =
4882 {
4883   TImode,
4884   DFmode, DImode,
4885   SFmode, SImode,
4886   HImode,
4887   QImode
4888 };
4889
4890 struct constant
4891 {
4892   struct constant *next;
4893   rtx value;
4894   rtx label;
4895 };
4896
4897 struct constant_pool
4898 {
4899   struct constant_pool *next;
4900   rtx first_insn;
4901   rtx pool_insn;
4902   bitmap insns;
4903
4904   struct constant *constants[NR_C_MODES];
4905   struct constant *execute;
4906   rtx label;
4907   int size;
4908 };
4909
4910 /* Allocate new constant_pool structure.  */
4911
4912 static struct constant_pool *
4913 s390_alloc_pool (void)
4914 {
4915   struct constant_pool *pool;
4916   int i;
4917
4918   pool = (struct constant_pool *) xmalloc (sizeof *pool);
4919   pool->next = NULL;
4920   for (i = 0; i < NR_C_MODES; i++)
4921     pool->constants[i] = NULL;
4922
4923   pool->execute = NULL;
4924   pool->label = gen_label_rtx ();
4925   pool->first_insn = NULL_RTX;
4926   pool->pool_insn = NULL_RTX;
4927   pool->insns = BITMAP_ALLOC (NULL);
4928   pool->size = 0;
4929
4930   return pool;
4931 }
4932
4933 /* Create new constant pool covering instructions starting at INSN
4934    and chain it to the end of POOL_LIST.  */
4935
4936 static struct constant_pool *
4937 s390_start_pool (struct constant_pool **pool_list, rtx insn)
4938 {
4939   struct constant_pool *pool, **prev;
4940
4941   pool = s390_alloc_pool ();
4942   pool->first_insn = insn;
4943
4944   for (prev = pool_list; *prev; prev = &(*prev)->next)
4945     ;
4946   *prev = pool;
4947
4948   return pool;
4949 }
4950
4951 /* End range of instructions covered by POOL at INSN and emit
4952    placeholder insn representing the pool.  */
4953
4954 static void
4955 s390_end_pool (struct constant_pool *pool, rtx insn)
4956 {
4957   rtx pool_size = GEN_INT (pool->size + 8 /* alignment slop */);
4958
4959   if (!insn)
4960     insn = get_last_insn ();
4961
4962   pool->pool_insn = emit_insn_after (gen_pool (pool_size), insn);
4963   INSN_ADDRESSES_NEW (pool->pool_insn, -1);
4964 }
4965
4966 /* Add INSN to the list of insns covered by POOL.  */
4967
4968 static void
4969 s390_add_pool_insn (struct constant_pool *pool, rtx insn)
4970 {
4971   bitmap_set_bit (pool->insns, INSN_UID (insn));
4972 }
4973
4974 /* Return pool out of POOL_LIST that covers INSN.  */
4975
4976 static struct constant_pool *
4977 s390_find_pool (struct constant_pool *pool_list, rtx insn)
4978 {
4979   struct constant_pool *pool;
4980
4981   for (pool = pool_list; pool; pool = pool->next)
4982     if (bitmap_bit_p (pool->insns, INSN_UID (insn)))
4983       break;
4984
4985   return pool;
4986 }
4987
4988 /* Add constant VAL of mode MODE to the constant pool POOL.  */
4989
4990 static void
4991 s390_add_constant (struct constant_pool *pool, rtx val, enum machine_mode mode)
4992 {
4993   struct constant *c;
4994   int i;
4995
4996   for (i = 0; i < NR_C_MODES; i++)
4997     if (constant_modes[i] == mode)
4998       break;
4999   gcc_assert (i != NR_C_MODES);
5000
5001   for (c = pool->constants[i]; c != NULL; c = c->next)
5002     if (rtx_equal_p (val, c->value))
5003       break;
5004
5005   if (c == NULL)
5006     {
5007       c = (struct constant *) xmalloc (sizeof *c);
5008       c->value = val;
5009       c->label = gen_label_rtx ();
5010       c->next = pool->constants[i];
5011       pool->constants[i] = c;
5012       pool->size += GET_MODE_SIZE (mode);
5013     }
5014 }
5015
5016 /* Find constant VAL of mode MODE in the constant pool POOL.
5017    Return an RTX describing the distance from the start of
5018    the pool to the location of the new constant.  */
5019
5020 static rtx
5021 s390_find_constant (struct constant_pool *pool, rtx val,
5022                     enum machine_mode mode)
5023 {
5024   struct constant *c;
5025   rtx offset;
5026   int i;
5027
5028   for (i = 0; i < NR_C_MODES; i++)
5029     if (constant_modes[i] == mode)
5030       break;
5031   gcc_assert (i != NR_C_MODES);
5032
5033   for (c = pool->constants[i]; c != NULL; c = c->next)
5034     if (rtx_equal_p (val, c->value))
5035       break;
5036
5037   gcc_assert (c);
5038
5039   offset = gen_rtx_MINUS (Pmode, gen_rtx_LABEL_REF (Pmode, c->label),
5040                                  gen_rtx_LABEL_REF (Pmode, pool->label));
5041   offset = gen_rtx_CONST (Pmode, offset);
5042   return offset;
5043 }
5044
5045 /* Check whether INSN is an execute.  Return the label_ref to its
5046    execute target template if so, NULL_RTX otherwise.  */
5047
5048 static rtx
5049 s390_execute_label (rtx insn)
5050 {
5051   if (GET_CODE (insn) == INSN
5052       && GET_CODE (PATTERN (insn)) == PARALLEL
5053       && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == UNSPEC
5054       && XINT (XVECEXP (PATTERN (insn), 0, 0), 1) == UNSPEC_EXECUTE)
5055     return XVECEXP (XVECEXP (PATTERN (insn), 0, 0), 0, 2);
5056
5057   return NULL_RTX;
5058 }
5059
5060 /* Add execute target for INSN to the constant pool POOL.  */
5061
5062 static void
5063 s390_add_execute (struct constant_pool *pool, rtx insn)
5064 {
5065   struct constant *c;
5066
5067   for (c = pool->execute; c != NULL; c = c->next)
5068     if (INSN_UID (insn) == INSN_UID (c->value))
5069       break;
5070
5071   if (c == NULL)
5072     {
5073       c = (struct constant *) xmalloc (sizeof *c);
5074       c->value = insn;
5075       c->label = gen_label_rtx ();
5076       c->next = pool->execute;
5077       pool->execute = c;
5078       pool->size += 6;
5079     }
5080 }
5081
5082 /* Find execute target for INSN in the constant pool POOL.
5083    Return an RTX describing the distance from the start of
5084    the pool to the location of the execute target.  */
5085
5086 static rtx
5087 s390_find_execute (struct constant_pool *pool, rtx insn)
5088 {
5089   struct constant *c;
5090   rtx offset;
5091
5092   for (c = pool->execute; c != NULL; c = c->next)
5093     if (INSN_UID (insn) == INSN_UID (c->value))
5094       break;
5095
5096   gcc_assert (c);
5097
5098   offset = gen_rtx_MINUS (Pmode, gen_rtx_LABEL_REF (Pmode, c->label),
5099                                  gen_rtx_LABEL_REF (Pmode, pool->label));
5100   offset = gen_rtx_CONST (Pmode, offset);
5101   return offset;
5102 }
5103
5104 /* For an execute INSN, extract the execute target template.  */
5105
5106 static rtx
5107 s390_execute_target (rtx insn)
5108 {
5109   rtx pattern = PATTERN (insn);
5110   gcc_assert (s390_execute_label (insn));
5111
5112   if (XVECLEN (pattern, 0) == 2)
5113     {
5114       pattern = copy_rtx (XVECEXP (pattern, 0, 1));
5115     }
5116   else
5117     {
5118       rtvec vec = rtvec_alloc (XVECLEN (pattern, 0) - 1);
5119       int i;
5120
5121       for (i = 0; i < XVECLEN (pattern, 0) - 1; i++)
5122         RTVEC_ELT (vec, i) = copy_rtx (XVECEXP (pattern, 0, i + 1));
5123
5124       pattern = gen_rtx_PARALLEL (VOIDmode, vec);
5125     }
5126
5127   return pattern;
5128 }
5129
5130 /* Indicate that INSN cannot be duplicated.  This is the case for
5131    execute insns that carry a unique label.  */
5132
5133 static bool
5134 s390_cannot_copy_insn_p (rtx insn)
5135 {
5136   rtx label = s390_execute_label (insn);
5137   return label && label != const0_rtx;
5138 }
5139
5140 /* Dump out the constants in POOL.  If REMOTE_LABEL is true,
5141    do not emit the pool base label.  */
5142
5143 static void
5144 s390_dump_pool (struct constant_pool *pool, bool remote_label)
5145 {
5146   struct constant *c;
5147   rtx insn = pool->pool_insn;
5148   int i;
5149
5150   /* Switch to rodata section.  */
5151   if (TARGET_CPU_ZARCH)
5152     {
5153       insn = emit_insn_after (gen_pool_section_start (), insn);
5154       INSN_ADDRESSES_NEW (insn, -1);
5155     }
5156
5157   /* Ensure minimum pool alignment.  */
5158   if (TARGET_CPU_ZARCH)
5159     insn = emit_insn_after (gen_pool_align (GEN_INT (8)), insn);
5160   else
5161     insn = emit_insn_after (gen_pool_align (GEN_INT (4)), insn);
5162   INSN_ADDRESSES_NEW (insn, -1);
5163
5164   /* Emit pool base label.  */
5165   if (!remote_label)
5166     {
5167       insn = emit_label_after (pool->label, insn);
5168       INSN_ADDRESSES_NEW (insn, -1);
5169     }
5170
5171   /* Dump constants in descending alignment requirement order,
5172      ensuring proper alignment for every constant.  */
5173   for (i = 0; i < NR_C_MODES; i++)
5174     for (c = pool->constants[i]; c; c = c->next)
5175       {
5176         /* Convert UNSPEC_LTREL_OFFSET unspecs to pool-relative references.  */
5177         rtx value = c->value;
5178         if (GET_CODE (value) == CONST
5179             && GET_CODE (XEXP (value, 0)) == UNSPEC
5180             && XINT (XEXP (value, 0), 1) == UNSPEC_LTREL_OFFSET
5181             && XVECLEN (XEXP (value, 0), 0) == 1)
5182           {
5183             value = gen_rtx_MINUS (Pmode, XVECEXP (XEXP (value, 0), 0, 0),
5184                                    gen_rtx_LABEL_REF (VOIDmode, pool->label));
5185             value = gen_rtx_CONST (VOIDmode, value);
5186           }
5187
5188         insn = emit_label_after (c->label, insn);
5189         INSN_ADDRESSES_NEW (insn, -1);
5190
5191         value = gen_rtx_UNSPEC_VOLATILE (constant_modes[i],
5192                                          gen_rtvec (1, value),
5193                                          UNSPECV_POOL_ENTRY);
5194         insn = emit_insn_after (value, insn);
5195         INSN_ADDRESSES_NEW (insn, -1);
5196       }
5197
5198   /* Ensure minimum alignment for instructions.  */
5199   insn = emit_insn_after (gen_pool_align (GEN_INT (2)), insn);
5200   INSN_ADDRESSES_NEW (insn, -1);
5201
5202   /* Output in-pool execute template insns.  */
5203   for (c = pool->execute; c; c = c->next)
5204     {
5205       insn = emit_label_after (c->label, insn);
5206       INSN_ADDRESSES_NEW (insn, -1);
5207
5208       insn = emit_insn_after (s390_execute_target (c->value), insn);
5209       INSN_ADDRESSES_NEW (insn, -1);
5210     }
5211
5212   /* Switch back to previous section.  */
5213   if (TARGET_CPU_ZARCH)
5214     {
5215       insn = emit_insn_after (gen_pool_section_end (), insn);
5216       INSN_ADDRESSES_NEW (insn, -1);
5217     }
5218
5219   insn = emit_barrier_after (insn);
5220   INSN_ADDRESSES_NEW (insn, -1);
5221
5222   /* Remove placeholder insn.  */
5223   remove_insn (pool->pool_insn);
5224 }
5225
5226 /* Free all memory used by POOL.  */
5227
5228 static void
5229 s390_free_pool (struct constant_pool *pool)
5230 {
5231   struct constant *c, *next;
5232   int i;
5233
5234   for (i = 0; i < NR_C_MODES; i++)
5235     for (c = pool->constants[i]; c; c = next)
5236       {
5237         next = c->next;
5238         free (c);
5239       }
5240
5241   for (c = pool->execute; c; c = next)
5242     {
5243       next = c->next;
5244       free (c);
5245     }
5246
5247   BITMAP_FREE (pool->insns);
5248   free (pool);
5249 }
5250
5251
5252 /* Collect main literal pool.  Return NULL on overflow.  */
5253
5254 static struct constant_pool *
5255 s390_mainpool_start (void)
5256 {
5257   struct constant_pool *pool;
5258   rtx insn;
5259
5260   pool = s390_alloc_pool ();
5261
5262   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5263     {
5264       if (GET_CODE (insn) == INSN
5265           && GET_CODE (PATTERN (insn)) == SET
5266           && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC_VOLATILE
5267           && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPECV_MAIN_POOL)
5268         {
5269           gcc_assert (!pool->pool_insn);
5270           pool->pool_insn = insn;
5271         }
5272
5273       if (!TARGET_CPU_ZARCH && s390_execute_label (insn))
5274         {
5275           s390_add_execute (pool, insn);
5276         }
5277       else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
5278         {
5279           rtx pool_ref = NULL_RTX;
5280           find_constant_pool_ref (PATTERN (insn), &pool_ref);
5281           if (pool_ref)
5282             {
5283               rtx constant = get_pool_constant (pool_ref);
5284               enum machine_mode mode = get_pool_mode (pool_ref);
5285               s390_add_constant (pool, constant, mode);
5286             }
5287         }
5288     }
5289
5290   gcc_assert (pool->pool_insn || pool->size == 0);
5291
5292   if (pool->size >= 4096)
5293     {
5294       /* We're going to chunkify the pool, so remove the main
5295          pool placeholder insn.  */
5296       remove_insn (pool->pool_insn);
5297
5298       s390_free_pool (pool);
5299       pool = NULL;
5300     }
5301
5302   return pool;
5303 }
5304
5305 /* POOL holds the main literal pool as collected by s390_mainpool_start.
5306    Modify the current function to output the pool constants as well as
5307    the pool register setup instruction.  */
5308
5309 static void
5310 s390_mainpool_finish (struct constant_pool *pool)
5311 {
5312   rtx base_reg = cfun->machine->base_reg;
5313   rtx insn;
5314
5315   /* If the pool is empty, we're done.  */
5316   if (pool->size == 0)
5317     {
5318       /* We don't actually need a base register after all.  */
5319       cfun->machine->base_reg = NULL_RTX;
5320
5321       if (pool->pool_insn)
5322         remove_insn (pool->pool_insn);
5323       s390_free_pool (pool);
5324       return;
5325     }
5326
5327   /* We need correct insn addresses.  */
5328   shorten_branches (get_insns ());
5329
5330   /* On zSeries, we use a LARL to load the pool register.  The pool is
5331      located in the .rodata section, so we emit it after the function.  */
5332   if (TARGET_CPU_ZARCH)
5333     {
5334       insn = gen_main_base_64 (base_reg, pool->label);
5335       insn = emit_insn_after (insn, pool->pool_insn);
5336       INSN_ADDRESSES_NEW (insn, -1);
5337       remove_insn (pool->pool_insn);
5338
5339       insn = get_last_insn ();
5340       pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
5341       INSN_ADDRESSES_NEW (pool->pool_insn, -1);
5342
5343       s390_dump_pool (pool, 0);
5344     }
5345
5346   /* On S/390, if the total size of the function's code plus literal pool
5347      does not exceed 4096 bytes, we use BASR to set up a function base
5348      pointer, and emit the literal pool at the end of the function.  */
5349   else if (INSN_ADDRESSES (INSN_UID (get_last_insn ()))
5350            + pool->size + 8 /* alignment slop */ < 4096)
5351     {
5352       insn = gen_main_base_31_small (base_reg, pool->label);
5353       insn = emit_insn_after (insn, pool->pool_insn);
5354       INSN_ADDRESSES_NEW (insn, -1);
5355       remove_insn (pool->pool_insn);
5356
5357       insn = emit_label_after (pool->label, insn);
5358       INSN_ADDRESSES_NEW (insn, -1);
5359
5360       insn = get_last_insn ();
5361       pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
5362       INSN_ADDRESSES_NEW (pool->pool_insn, -1);
5363
5364       s390_dump_pool (pool, 1);
5365     }
5366
5367   /* Otherwise, we emit an inline literal pool and use BASR to branch
5368      over it, setting up the pool register at the same time.  */
5369   else
5370     {
5371       rtx pool_end = gen_label_rtx ();
5372
5373       insn = gen_main_base_31_large (base_reg, pool->label, pool_end);
5374       insn = emit_insn_after (insn, pool->pool_insn);
5375       INSN_ADDRESSES_NEW (insn, -1);
5376       remove_insn (pool->pool_insn);
5377
5378       insn = emit_label_after (pool->label, insn);
5379       INSN_ADDRESSES_NEW (insn, -1);
5380
5381       pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
5382       INSN_ADDRESSES_NEW (pool->pool_insn, -1);
5383
5384       insn = emit_label_after (pool_end, pool->pool_insn);
5385       INSN_ADDRESSES_NEW (insn, -1);
5386
5387       s390_dump_pool (pool, 1);
5388     }
5389
5390
5391   /* Replace all literal pool references.  */
5392
5393   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5394     {
5395       if (INSN_P (insn))
5396         replace_ltrel_base (&PATTERN (insn));
5397
5398       if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
5399         {
5400           rtx addr, pool_ref = NULL_RTX;
5401           find_constant_pool_ref (PATTERN (insn), &pool_ref);
5402           if (pool_ref)
5403             {
5404               if (s390_execute_label (insn))
5405                 addr = s390_find_execute (pool, insn);
5406               else
5407                 addr = s390_find_constant (pool, get_pool_constant (pool_ref),
5408                                                  get_pool_mode (pool_ref));
5409
5410               replace_constant_pool_ref (&PATTERN (insn), pool_ref, addr);
5411               INSN_CODE (insn) = -1;
5412             }
5413         }
5414     }
5415
5416
5417   /* Free the pool.  */
5418   s390_free_pool (pool);
5419 }
5420
5421 /* POOL holds the main literal pool as collected by s390_mainpool_start.
5422    We have decided we cannot use this pool, so revert all changes
5423    to the current function that were done by s390_mainpool_start.  */
5424 static void
5425 s390_mainpool_cancel (struct constant_pool *pool)
5426 {
5427   /* We didn't actually change the instruction stream, so simply
5428      free the pool memory.  */
5429   s390_free_pool (pool);
5430 }
5431
5432
5433 /* Chunkify the literal pool.  */
5434
5435 #define S390_POOL_CHUNK_MIN     0xc00
5436 #define S390_POOL_CHUNK_MAX     0xe00
5437
5438 static struct constant_pool *
5439 s390_chunkify_start (void)
5440 {
5441   struct constant_pool *curr_pool = NULL, *pool_list = NULL;
5442   int extra_size = 0;
5443   bitmap far_labels;
5444   rtx pending_ltrel = NULL_RTX;
5445   rtx insn;
5446
5447   rtx (*gen_reload_base) (rtx, rtx) =
5448     TARGET_CPU_ZARCH? gen_reload_base_64 : gen_reload_base_31;
5449
5450
5451   /* We need correct insn addresses.  */
5452
5453   shorten_branches (get_insns ());
5454
5455   /* Scan all insns and move literals to pool chunks.  */
5456
5457   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5458     {
5459       /* Check for pending LTREL_BASE.  */
5460       if (INSN_P (insn))
5461         {
5462           rtx ltrel_base = find_ltrel_base (PATTERN (insn));
5463           if (ltrel_base)
5464             {
5465               gcc_assert (ltrel_base == pending_ltrel);
5466               pending_ltrel = NULL_RTX;
5467             }
5468         }
5469
5470       if (!TARGET_CPU_ZARCH && s390_execute_label (insn))
5471         {
5472           if (!curr_pool)
5473             curr_pool = s390_start_pool (&pool_list, insn);
5474
5475           s390_add_execute (curr_pool, insn);
5476           s390_add_pool_insn (curr_pool, insn);
5477         }
5478       else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
5479         {
5480           rtx pool_ref = NULL_RTX;
5481           find_constant_pool_ref (PATTERN (insn), &pool_ref);
5482           if (pool_ref)
5483             {
5484               rtx constant = get_pool_constant (pool_ref);
5485               enum machine_mode mode = get_pool_mode (pool_ref);
5486
5487               if (!curr_pool)
5488                 curr_pool = s390_start_pool (&pool_list, insn);
5489
5490               s390_add_constant (curr_pool, constant, mode);
5491               s390_add_pool_insn (curr_pool, insn);
5492
5493               /* Don't split the pool chunk between a LTREL_OFFSET load
5494                  and the corresponding LTREL_BASE.  */
5495               if (GET_CODE (constant) == CONST
5496                   && GET_CODE (XEXP (constant, 0)) == UNSPEC
5497                   && XINT (XEXP (constant, 0), 1) == UNSPEC_LTREL_OFFSET)
5498                 {
5499                   gcc_assert (!pending_ltrel);
5500                   pending_ltrel = pool_ref;
5501                 }
5502             }
5503         }
5504
5505       if (GET_CODE (insn) == JUMP_INSN || GET_CODE (insn) == CODE_LABEL)
5506         {
5507           if (curr_pool)
5508             s390_add_pool_insn (curr_pool, insn);
5509           /* An LTREL_BASE must follow within the same basic block.  */
5510           gcc_assert (!pending_ltrel);
5511         }
5512
5513       if (!curr_pool
5514           || INSN_ADDRESSES_SIZE () <= (size_t) INSN_UID (insn)
5515           || INSN_ADDRESSES (INSN_UID (insn)) == -1)
5516         continue;
5517
5518       if (TARGET_CPU_ZARCH)
5519         {
5520           if (curr_pool->size < S390_POOL_CHUNK_MAX)
5521             continue;
5522
5523           s390_end_pool (curr_pool, NULL_RTX);
5524           curr_pool = NULL;
5525         }
5526       else
5527         {
5528           int chunk_size = INSN_ADDRESSES (INSN_UID (insn))
5529                            - INSN_ADDRESSES (INSN_UID (curr_pool->first_insn))
5530                          + extra_size;
5531
5532           /* We will later have to insert base register reload insns.
5533              Those will have an effect on code size, which we need to
5534              consider here.  This calculation makes rather pessimistic
5535              worst-case assumptions.  */
5536           if (GET_CODE (insn) == CODE_LABEL)
5537             extra_size += 6;
5538
5539           if (chunk_size < S390_POOL_CHUNK_MIN
5540               && curr_pool->size < S390_POOL_CHUNK_MIN)
5541             continue;
5542
5543           /* Pool chunks can only be inserted after BARRIERs ...  */
5544           if (GET_CODE (insn) == BARRIER)
5545             {
5546               s390_end_pool (curr_pool, insn);
5547               curr_pool = NULL;
5548               extra_size = 0;
5549             }
5550
5551           /* ... so if we don't find one in time, create one.  */
5552           else if ((chunk_size > S390_POOL_CHUNK_MAX
5553                    || curr_pool->size > S390_POOL_CHUNK_MAX))
5554             {
5555               rtx label, jump, barrier;
5556
5557               /* We can insert the barrier only after a 'real' insn.  */
5558               if (GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
5559                 continue;
5560               if (get_attr_length (insn) == 0)
5561                 continue;
5562
5563               /* Don't separate LTREL_BASE from the corresponding
5564                  LTREL_OFFSET load.  */
5565               if (pending_ltrel)
5566                 continue;
5567
5568               label = gen_label_rtx ();
5569               jump = emit_jump_insn_after (gen_jump (label), insn);
5570               barrier = emit_barrier_after (jump);
5571               insn = emit_label_after (label, barrier);
5572               JUMP_LABEL (jump) = label;
5573               LABEL_NUSES (label) = 1;
5574
5575               INSN_ADDRESSES_NEW (jump, -1);
5576               INSN_ADDRESSES_NEW (barrier, -1);
5577               INSN_ADDRESSES_NEW (insn, -1);
5578
5579               s390_end_pool (curr_pool, barrier);
5580               curr_pool = NULL;
5581               extra_size = 0;
5582             }
5583         }
5584     }
5585
5586   if (curr_pool)
5587     s390_end_pool (curr_pool, NULL_RTX);
5588   gcc_assert (!pending_ltrel);
5589
5590   /* Find all labels that are branched into
5591      from an insn belonging to a different chunk.  */
5592
5593   far_labels = BITMAP_ALLOC (NULL);
5594
5595   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5596     {
5597       /* Labels marked with LABEL_PRESERVE_P can be target
5598          of non-local jumps, so we have to mark them.
5599          The same holds for named labels.
5600
5601          Don't do that, however, if it is the label before
5602          a jump table.  */
5603
5604       if (GET_CODE (insn) == CODE_LABEL
5605           && (LABEL_PRESERVE_P (insn) || LABEL_NAME (insn)))
5606         {
5607           rtx vec_insn = next_real_insn (insn);
5608           rtx vec_pat = vec_insn && GET_CODE (vec_insn) == JUMP_INSN ?
5609                         PATTERN (vec_insn) : NULL_RTX;
5610           if (!vec_pat
5611               || !(GET_CODE (vec_pat) == ADDR_VEC
5612                    || GET_CODE (vec_pat) == ADDR_DIFF_VEC))
5613             bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (insn));
5614         }
5615
5616       /* If we have a direct jump (conditional or unconditional)
5617          or a casesi jump, check all potential targets.  */
5618       else if (GET_CODE (insn) == JUMP_INSN)
5619         {
5620           rtx pat = PATTERN (insn);
5621           if (GET_CODE (pat) == PARALLEL && XVECLEN (pat, 0) > 2)
5622             pat = XVECEXP (pat, 0, 0);
5623
5624           if (GET_CODE (pat) == SET)
5625             {
5626               rtx label = JUMP_LABEL (insn);
5627               if (label)
5628                 {
5629                   if (s390_find_pool (pool_list, label)
5630                       != s390_find_pool (pool_list, insn))
5631                     bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (label));
5632                 }
5633             }
5634           else if (GET_CODE (pat) == PARALLEL
5635                    && XVECLEN (pat, 0) == 2
5636                    && GET_CODE (XVECEXP (pat, 0, 0)) == SET
5637                    && GET_CODE (XVECEXP (pat, 0, 1)) == USE
5638                    && GET_CODE (XEXP (XVECEXP (pat, 0, 1), 0)) == LABEL_REF)
5639             {
5640               /* Find the jump table used by this casesi jump.  */
5641               rtx vec_label = XEXP (XEXP (XVECEXP (pat, 0, 1), 0), 0);
5642               rtx vec_insn = next_real_insn (vec_label);
5643               rtx vec_pat = vec_insn && GET_CODE (vec_insn) == JUMP_INSN ?
5644                             PATTERN (vec_insn) : NULL_RTX;
5645               if (vec_pat
5646                   && (GET_CODE (vec_pat) == ADDR_VEC
5647                       || GET_CODE (vec_pat) == ADDR_DIFF_VEC))
5648                 {
5649                   int i, diff_p = GET_CODE (vec_pat) == ADDR_DIFF_VEC;
5650
5651                   for (i = 0; i < XVECLEN (vec_pat, diff_p); i++)
5652                     {
5653                       rtx label = XEXP (XVECEXP (vec_pat, diff_p, i), 0);
5654
5655                       if (s390_find_pool (pool_list, label)
5656                           != s390_find_pool (pool_list, insn))
5657                         bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (label));
5658                     }
5659                 }
5660             }
5661         }
5662     }
5663
5664   /* Insert base register reload insns before every pool.  */
5665
5666   for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
5667     {
5668       rtx new_insn = gen_reload_base (cfun->machine->base_reg, 
5669                                       curr_pool->label);
5670       rtx insn = curr_pool->first_insn;
5671       INSN_ADDRESSES_NEW (emit_insn_before (new_insn, insn), -1);
5672     }
5673
5674   /* Insert base register reload insns at every far label.  */
5675
5676   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5677     if (GET_CODE (insn) == CODE_LABEL
5678         && bitmap_bit_p (far_labels, CODE_LABEL_NUMBER (insn)))
5679       {
5680         struct constant_pool *pool = s390_find_pool (pool_list, insn);
5681         if (pool)
5682           {
5683             rtx new_insn = gen_reload_base (cfun->machine->base_reg, 
5684                                             pool->label);
5685             INSN_ADDRESSES_NEW (emit_insn_after (new_insn, insn), -1);
5686           }
5687       }
5688
5689
5690   BITMAP_FREE (far_labels);
5691
5692
5693   /* Recompute insn addresses.  */
5694
5695   init_insn_lengths ();
5696   shorten_branches (get_insns ());
5697
5698   return pool_list;
5699 }
5700
5701 /* POOL_LIST is a chunk list as prepared by s390_chunkify_start.
5702    After we have decided to use this list, finish implementing
5703    all changes to the current function as required.  */
5704
5705 static void
5706 s390_chunkify_finish (struct constant_pool *pool_list)
5707 {
5708   struct constant_pool *curr_pool = NULL;
5709   rtx insn;
5710
5711
5712   /* Replace all literal pool references.  */
5713
5714   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5715     {
5716       if (INSN_P (insn))
5717         replace_ltrel_base (&PATTERN (insn));
5718
5719       curr_pool = s390_find_pool (pool_list, insn);
5720       if (!curr_pool)
5721         continue;
5722
5723       if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
5724         {
5725           rtx addr, pool_ref = NULL_RTX;
5726           find_constant_pool_ref (PATTERN (insn), &pool_ref);
5727           if (pool_ref)
5728             {
5729               if (s390_execute_label (insn))
5730                 addr = s390_find_execute (curr_pool, insn);
5731               else
5732                 addr = s390_find_constant (curr_pool,
5733                                            get_pool_constant (pool_ref),
5734                                            get_pool_mode (pool_ref));
5735
5736               replace_constant_pool_ref (&PATTERN (insn), pool_ref, addr);
5737               INSN_CODE (insn) = -1;
5738             }
5739         }
5740     }
5741
5742   /* Dump out all literal pools.  */
5743
5744   for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
5745     s390_dump_pool (curr_pool, 0);
5746
5747   /* Free pool list.  */
5748
5749   while (pool_list)
5750     {
5751       struct constant_pool *next = pool_list->next;
5752       s390_free_pool (pool_list);
5753       pool_list = next;
5754     }
5755 }
5756
5757 /* POOL_LIST is a chunk list as prepared by s390_chunkify_start.
5758    We have decided we cannot use this list, so revert all changes
5759    to the current function that were done by s390_chunkify_start.  */
5760
5761 static void
5762 s390_chunkify_cancel (struct constant_pool *pool_list)
5763 {
5764   struct constant_pool *curr_pool = NULL;
5765   rtx insn;
5766
5767   /* Remove all pool placeholder insns.  */
5768
5769   for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
5770     {
5771       /* Did we insert an extra barrier?  Remove it.  */
5772       rtx barrier = PREV_INSN (curr_pool->pool_insn);
5773       rtx jump = barrier? PREV_INSN (barrier) : NULL_RTX;
5774       rtx label = NEXT_INSN (curr_pool->pool_insn);
5775
5776       if (jump && GET_CODE (jump) == JUMP_INSN
5777           && barrier && GET_CODE (barrier) == BARRIER
5778           && label && GET_CODE (label) == CODE_LABEL
5779           && GET_CODE (PATTERN (jump)) == SET
5780           && SET_DEST (PATTERN (jump)) == pc_rtx
5781           && GET_CODE (SET_SRC (PATTERN (jump))) == LABEL_REF
5782           && XEXP (SET_SRC (PATTERN (jump)), 0) == label)
5783         {
5784           remove_insn (jump);
5785           remove_insn (barrier);
5786           remove_insn (label);
5787         }
5788
5789       remove_insn (curr_pool->pool_insn);
5790     }
5791
5792   /* Remove all base register reload insns.  */
5793
5794   for (insn = get_insns (); insn; )
5795     {
5796       rtx next_insn = NEXT_INSN (insn);
5797
5798       if (GET_CODE (insn) == INSN
5799           && GET_CODE (PATTERN (insn)) == SET
5800           && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC
5801           && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPEC_RELOAD_BASE)
5802         remove_insn (insn);
5803
5804       insn = next_insn;
5805     }
5806
5807   /* Free pool list.  */
5808
5809   while (pool_list)
5810     {
5811       struct constant_pool *next = pool_list->next;
5812       s390_free_pool (pool_list);
5813       pool_list = next;
5814     }
5815 }
5816
5817
5818 /* Output the constant pool entry EXP in mode MODE with alignment ALIGN.  */
5819
5820 void
5821 s390_output_pool_entry (rtx exp, enum machine_mode mode, unsigned int align)
5822 {
5823   REAL_VALUE_TYPE r;
5824
5825   switch (GET_MODE_CLASS (mode))
5826     {
5827     case MODE_FLOAT:
5828       gcc_assert (GET_CODE (exp) == CONST_DOUBLE);
5829
5830       REAL_VALUE_FROM_CONST_DOUBLE (r, exp);
5831       assemble_real (r, mode, align);
5832       break;
5833
5834     case MODE_INT:
5835       assemble_integer (exp, GET_MODE_SIZE (mode), align, 1);
5836       break;
5837
5838     default:
5839       gcc_unreachable ();
5840     }
5841 }
5842
5843
5844 /* Return an RTL expression representing the value of the return address
5845    for the frame COUNT steps up from the current frame.  FRAME is the
5846    frame pointer of that frame.  */
5847
5848 rtx
5849 s390_return_addr_rtx (int count, rtx frame ATTRIBUTE_UNUSED)
5850 {
5851   int offset;
5852   rtx addr;
5853
5854   /* Without backchain, we fail for all but the current frame.  */
5855
5856   if (!TARGET_BACKCHAIN && count > 0)
5857     return NULL_RTX;
5858
5859   /* For the current frame, we need to make sure the initial
5860      value of RETURN_REGNUM is actually saved.  */
5861
5862   if (count == 0)
5863     {
5864       /* On non-z architectures branch splitting could overwrite r14.  */
5865       if (TARGET_CPU_ZARCH)
5866         return get_hard_reg_initial_val (Pmode, RETURN_REGNUM);
5867       else
5868         {
5869           cfun_frame_layout.save_return_addr_p = true;
5870           return gen_rtx_MEM (Pmode, return_address_pointer_rtx);
5871         }
5872     }
5873
5874   if (TARGET_PACKED_STACK)
5875     offset = -2 * UNITS_PER_WORD;
5876   else
5877     offset = RETURN_REGNUM * UNITS_PER_WORD;
5878
5879   addr = plus_constant (frame, offset);
5880   addr = memory_address (Pmode, addr);
5881   return gen_rtx_MEM (Pmode, addr);
5882 }
5883
5884 /* Return an RTL expression representing the back chain stored in
5885    the current stack frame.  */
5886
5887 rtx
5888 s390_back_chain_rtx (void)
5889 {
5890   rtx chain;
5891
5892   gcc_assert (TARGET_BACKCHAIN);
5893
5894   if (TARGET_PACKED_STACK)
5895     chain = plus_constant (stack_pointer_rtx,
5896                            STACK_POINTER_OFFSET - UNITS_PER_WORD);
5897   else
5898     chain = stack_pointer_rtx;
5899
5900   chain = gen_rtx_MEM (Pmode, chain);
5901   return chain;
5902 }
5903
5904 /* Find first call clobbered register unused in a function.
5905    This could be used as base register in a leaf function
5906    or for holding the return address before epilogue.  */
5907
5908 static int
5909 find_unused_clobbered_reg (void)
5910 {
5911   int i;
5912   for (i = 0; i < 6; i++)
5913     if (!regs_ever_live[i])
5914       return i;
5915   return 0;
5916 }
5917
5918
5919 /* Helper function for s390_regs_ever_clobbered.  Sets the fields in DATA for all 
5920    clobbered hard regs in SETREG.  */
5921
5922 static void
5923 s390_reg_clobbered_rtx (rtx setreg, rtx set_insn ATTRIBUTE_UNUSED, void *data)
5924 {
5925   int *regs_ever_clobbered = (int *)data;
5926   unsigned int i, regno;
5927   enum machine_mode mode = GET_MODE (setreg);
5928
5929   if (GET_CODE (setreg) == SUBREG)
5930     {
5931       rtx inner = SUBREG_REG (setreg);
5932       if (!GENERAL_REG_P (inner))
5933         return;
5934       regno = subreg_regno (setreg);
5935     }
5936   else if (GENERAL_REG_P (setreg))
5937     regno = REGNO (setreg);
5938   else
5939     return;
5940
5941   for (i = regno;
5942        i < regno + HARD_REGNO_NREGS (regno, mode);
5943        i++)
5944     regs_ever_clobbered[i] = 1;
5945 }
5946
5947 /* Walks through all basic blocks of the current function looking
5948    for clobbered hard regs using s390_reg_clobbered_rtx.  The fields
5949    of the passed integer array REGS_EVER_CLOBBERED are set to one for
5950    each of those regs.  */
5951
5952 static void
5953 s390_regs_ever_clobbered (int *regs_ever_clobbered)
5954 {
5955   basic_block cur_bb;
5956   rtx cur_insn;
5957   unsigned int i;
5958
5959   memset (regs_ever_clobbered, 0, 16 * sizeof (int));
5960
5961   /* For non-leaf functions we have to consider all call clobbered regs to be
5962      clobbered.  */
5963   if (!current_function_is_leaf)
5964     {
5965       for (i = 0; i < 16; i++)
5966         regs_ever_clobbered[i] = call_really_used_regs[i];
5967     }
5968
5969   /* Make the "magic" eh_return registers live if necessary.  For regs_ever_live
5970      this work is done by liveness analysis (mark_regs_live_at_end).
5971      Special care is needed for functions containing landing pads.  Landing pads
5972      may use the eh registers, but the code which sets these registers is not
5973      contained in that function.  Hence s390_regs_ever_clobbered is not able to
5974      deal with this automatically.  */
5975   if (current_function_calls_eh_return || cfun->machine->has_landing_pad_p)
5976     for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM ; i++)
5977       regs_ever_clobbered[EH_RETURN_DATA_REGNO (i)] = 1;
5978
5979   /* For nonlocal gotos all call-saved registers have to be saved.
5980      This flag is also set for the unwinding code in libgcc.
5981      See expand_builtin_unwind_init.  For regs_ever_live this is done by
5982      reload.  */
5983   if (current_function_has_nonlocal_label)
5984     for (i = 0; i < 16; i++)
5985       if (!call_really_used_regs[i])
5986         regs_ever_clobbered[i] = 1;
5987
5988   FOR_EACH_BB (cur_bb)
5989     {
5990       FOR_BB_INSNS (cur_bb, cur_insn)
5991         {
5992           if (INSN_P (cur_insn))
5993             note_stores (PATTERN (cur_insn),
5994                          s390_reg_clobbered_rtx, 
5995                          regs_ever_clobbered);
5996         }
5997     }
5998 }
5999
6000 /* Determine the frame area which actually has to be accessed 
6001    in the function epilogue. The values are stored at the 
6002    given pointers AREA_BOTTOM (address of the lowest used stack
6003    address) and AREA_TOP (address of the first item which does 
6004    not belong to the stack frame).  */
6005
6006 static void
6007 s390_frame_area (int *area_bottom, int *area_top)
6008 {
6009   int b, t;
6010   int i;
6011
6012   b = INT_MAX;
6013   t = INT_MIN;
6014
6015   if (cfun_frame_layout.first_restore_gpr != -1)
6016     {
6017       b = (cfun_frame_layout.gprs_offset
6018            + cfun_frame_layout.first_restore_gpr * UNITS_PER_WORD);
6019       t = b + (cfun_frame_layout.last_restore_gpr
6020                - cfun_frame_layout.first_restore_gpr + 1) * UNITS_PER_WORD;
6021     }
6022
6023   if (TARGET_64BIT && cfun_save_high_fprs_p)
6024     {
6025       b = MIN (b, cfun_frame_layout.f8_offset);
6026       t = MAX (t, (cfun_frame_layout.f8_offset
6027                    + cfun_frame_layout.high_fprs * 8));
6028     }
6029
6030   if (!TARGET_64BIT)
6031     for (i = 2; i < 4; i++)
6032       if (cfun_fpr_bit_p (i))
6033         {
6034           b = MIN (b, cfun_frame_layout.f4_offset + (i - 2) * 8);
6035           t = MAX (t, cfun_frame_layout.f4_offset + (i - 1) * 8);
6036         }
6037   
6038   *area_bottom = b;
6039   *area_top = t;
6040 }
6041
6042 /* Fill cfun->machine with info about register usage of current function.
6043    Return in CLOBBERED_REGS which GPRs are currently considered set.  */
6044
6045 static void
6046 s390_register_info (int clobbered_regs[])
6047 {
6048   int i, j;
6049
6050   /* fprs 8 - 15 are call saved for 64 Bit ABI.  */
6051   cfun_frame_layout.fpr_bitmap = 0;
6052   cfun_frame_layout.high_fprs = 0;
6053   if (TARGET_64BIT)
6054     for (i = 24; i < 32; i++)
6055       if (regs_ever_live[i] && !global_regs[i])
6056         {
6057           cfun_set_fpr_bit (i - 16);
6058           cfun_frame_layout.high_fprs++;
6059         }
6060
6061   /* Find first and last gpr to be saved.  We trust regs_ever_live
6062      data, except that we don't save and restore global registers.
6063
6064      Also, all registers with special meaning to the compiler need
6065      to be handled extra.  */
6066
6067   s390_regs_ever_clobbered (clobbered_regs);
6068
6069   for (i = 0; i < 16; i++)
6070     clobbered_regs[i] = clobbered_regs[i] && !global_regs[i] && !fixed_regs[i];
6071
6072   if (frame_pointer_needed)
6073     clobbered_regs[HARD_FRAME_POINTER_REGNUM] = 1;
6074
6075   if (flag_pic)
6076     clobbered_regs[PIC_OFFSET_TABLE_REGNUM] 
6077       |= regs_ever_live[PIC_OFFSET_TABLE_REGNUM];
6078
6079   clobbered_regs[BASE_REGNUM] 
6080     |= (cfun->machine->base_reg
6081         && REGNO (cfun->machine->base_reg) == BASE_REGNUM);
6082
6083   clobbered_regs[RETURN_REGNUM]
6084     |= (!current_function_is_leaf
6085         || TARGET_TPF_PROFILING
6086         || cfun->machine->split_branches_pending_p
6087         || cfun_frame_layout.save_return_addr_p
6088         || current_function_calls_eh_return
6089         || current_function_stdarg);
6090
6091   clobbered_regs[STACK_POINTER_REGNUM]
6092     |= (!current_function_is_leaf
6093         || TARGET_TPF_PROFILING
6094         || cfun_save_high_fprs_p
6095         || get_frame_size () > 0
6096         || current_function_calls_alloca
6097         || current_function_stdarg);
6098
6099   for (i = 6; i < 16; i++)
6100     if (clobbered_regs[i])
6101       break;
6102   for (j = 15; j > i; j--)
6103     if (clobbered_regs[j])
6104       break;
6105
6106   if (i == 16)
6107     {
6108       /* Nothing to save/restore.  */
6109       cfun_frame_layout.first_save_gpr = -1;
6110       cfun_frame_layout.first_restore_gpr = -1;
6111       cfun_frame_layout.last_save_gpr = -1;
6112       cfun_frame_layout.last_restore_gpr = -1;
6113     }
6114   else
6115     {
6116       /* Save / Restore from gpr i to j.  */
6117       cfun_frame_layout.first_save_gpr = i;
6118       cfun_frame_layout.first_restore_gpr = i;
6119       cfun_frame_layout.last_save_gpr = j;
6120       cfun_frame_layout.last_restore_gpr = j;
6121     }
6122
6123   if (current_function_stdarg)
6124     {
6125       /* Varargs functions need to save gprs 2 to 6.  */
6126       if (cfun->va_list_gpr_size
6127           && current_function_args_info.gprs < GP_ARG_NUM_REG)
6128         {
6129           int min_gpr = current_function_args_info.gprs;
6130           int max_gpr = min_gpr + cfun->va_list_gpr_size;
6131           if (max_gpr > GP_ARG_NUM_REG)
6132             max_gpr = GP_ARG_NUM_REG;
6133
6134           if (cfun_frame_layout.first_save_gpr == -1
6135               || cfun_frame_layout.first_save_gpr > 2 + min_gpr)
6136             cfun_frame_layout.first_save_gpr = 2 + min_gpr;
6137
6138           if (cfun_frame_layout.last_save_gpr == -1
6139               || cfun_frame_layout.last_save_gpr < 2 + max_gpr - 1)
6140             cfun_frame_layout.last_save_gpr = 2 + max_gpr - 1;
6141         }
6142
6143       /* Mark f0, f2 for 31 bit and f0-f4 for 64 bit to be saved.  */
6144       if (TARGET_HARD_FLOAT && cfun->va_list_fpr_size
6145           && current_function_args_info.fprs < FP_ARG_NUM_REG)
6146         {
6147           int min_fpr = current_function_args_info.fprs;
6148           int max_fpr = min_fpr + cfun->va_list_fpr_size;
6149           if (max_fpr > FP_ARG_NUM_REG)
6150             max_fpr = FP_ARG_NUM_REG;
6151
6152           /* ??? This is currently required to ensure proper location
6153              of the fpr save slots within the va_list save area.  */
6154           if (TARGET_PACKED_STACK)
6155             min_fpr = 0;
6156
6157           for (i = min_fpr; i < max_fpr; i++)
6158             cfun_set_fpr_bit (i);
6159         }
6160     }
6161
6162   if (!TARGET_64BIT)
6163     for (i = 2; i < 4; i++)
6164       if (regs_ever_live[i + 16] && !global_regs[i + 16])
6165         cfun_set_fpr_bit (i);
6166 }
6167
6168 /* Fill cfun->machine with info about frame of current function.  */
6169
6170 static void
6171 s390_frame_info (void)
6172 {
6173   int i;
6174
6175   cfun_frame_layout.frame_size = get_frame_size ();
6176   if (!TARGET_64BIT && cfun_frame_layout.frame_size > 0x7fff0000)
6177     fatal_error ("total size of local variables exceeds architecture limit");
6178   
6179   if (!TARGET_PACKED_STACK)
6180     {
6181       cfun_frame_layout.backchain_offset = 0;
6182       cfun_frame_layout.f0_offset = 16 * UNITS_PER_WORD;
6183       cfun_frame_layout.f4_offset = cfun_frame_layout.f0_offset + 2 * 8;
6184       cfun_frame_layout.f8_offset = -cfun_frame_layout.high_fprs * 8;
6185       cfun_frame_layout.gprs_offset = (cfun_frame_layout.first_save_gpr
6186                                        * UNITS_PER_WORD);
6187     }
6188   else if (TARGET_BACKCHAIN) /* kernel stack layout */
6189     {
6190       cfun_frame_layout.backchain_offset = (STACK_POINTER_OFFSET
6191                                             - UNITS_PER_WORD);
6192       cfun_frame_layout.gprs_offset 
6193         = (cfun_frame_layout.backchain_offset 
6194            - (STACK_POINTER_REGNUM - cfun_frame_layout.first_save_gpr + 1)
6195            * UNITS_PER_WORD);
6196           
6197       if (TARGET_64BIT)
6198         {
6199           cfun_frame_layout.f4_offset 
6200             = (cfun_frame_layout.gprs_offset
6201                - 8 * (cfun_fpr_bit_p (2) + cfun_fpr_bit_p (3)));
6202           
6203           cfun_frame_layout.f0_offset 
6204             = (cfun_frame_layout.f4_offset 
6205                - 8 * (cfun_fpr_bit_p (0) + cfun_fpr_bit_p (1)));
6206         }
6207       else
6208         {
6209           /* On 31 bit we have to care about alignment of the
6210              floating point regs to provide fastest access.  */
6211           cfun_frame_layout.f0_offset 
6212             = ((cfun_frame_layout.gprs_offset 
6213                 & ~(STACK_BOUNDARY / BITS_PER_UNIT - 1))
6214                - 8 * (cfun_fpr_bit_p (0) + cfun_fpr_bit_p (1)));
6215           
6216           cfun_frame_layout.f4_offset 
6217             = (cfun_frame_layout.f0_offset
6218                - 8 * (cfun_fpr_bit_p (2) + cfun_fpr_bit_p (3)));
6219         }
6220     }
6221   else /* no backchain */
6222     {
6223       cfun_frame_layout.f4_offset 
6224         = (STACK_POINTER_OFFSET
6225            - 8 * (cfun_fpr_bit_p (2) + cfun_fpr_bit_p (3)));
6226       
6227       cfun_frame_layout.f0_offset 
6228         = (cfun_frame_layout.f4_offset
6229            - 8 * (cfun_fpr_bit_p (0) + cfun_fpr_bit_p (1)));
6230       
6231       cfun_frame_layout.gprs_offset 
6232         = cfun_frame_layout.f0_offset - cfun_gprs_save_area_size;
6233     }
6234
6235   if (current_function_is_leaf
6236       && !TARGET_TPF_PROFILING
6237       && cfun_frame_layout.frame_size == 0
6238       && !cfun_save_high_fprs_p
6239       && !current_function_calls_alloca
6240       && !current_function_stdarg)
6241     return;
6242
6243   if (!TARGET_PACKED_STACK)
6244     cfun_frame_layout.frame_size += (STACK_POINTER_OFFSET
6245                                      + current_function_outgoing_args_size
6246                                      + cfun_frame_layout.high_fprs * 8);
6247   else
6248     {
6249       if (TARGET_BACKCHAIN)
6250         cfun_frame_layout.frame_size += UNITS_PER_WORD;
6251
6252       /* No alignment trouble here because f8-f15 are only saved under 
6253          64 bit.  */
6254       cfun_frame_layout.f8_offset = (MIN (MIN (cfun_frame_layout.f0_offset,
6255                                                cfun_frame_layout.f4_offset),
6256                                           cfun_frame_layout.gprs_offset)
6257                                      - cfun_frame_layout.high_fprs * 8);
6258
6259       cfun_frame_layout.frame_size += cfun_frame_layout.high_fprs * 8;
6260
6261       for (i = 0; i < 8; i++)
6262         if (cfun_fpr_bit_p (i))
6263           cfun_frame_layout.frame_size += 8;
6264       
6265       cfun_frame_layout.frame_size += cfun_gprs_save_area_size;
6266       
6267       /* If under 31 bit an odd number of gprs has to be saved we have to adjust
6268          the frame size to sustain 8 byte alignment of stack frames.  */
6269       cfun_frame_layout.frame_size = ((cfun_frame_layout.frame_size +
6270                                        STACK_BOUNDARY / BITS_PER_UNIT - 1)
6271                                       & ~(STACK_BOUNDARY / BITS_PER_UNIT - 1));
6272
6273       cfun_frame_layout.frame_size += current_function_outgoing_args_size;
6274     }
6275 }
6276
6277 /* Generate frame layout.  Fills in register and frame data for the current
6278    function in cfun->machine.  This routine can be called multiple times;
6279    it will re-do the complete frame layout every time.  */
6280
6281 static void
6282 s390_init_frame_layout (void)
6283 {
6284   HOST_WIDE_INT frame_size;
6285   int base_used;
6286   int clobbered_regs[16];
6287
6288   /* On S/390 machines, we may need to perform branch splitting, which
6289      will require both base and return address register.  We have no
6290      choice but to assume we're going to need them until right at the
6291      end of the machine dependent reorg phase.  */
6292   if (!TARGET_CPU_ZARCH)
6293     cfun->machine->split_branches_pending_p = true;
6294
6295   do
6296     {
6297       frame_size = cfun_frame_layout.frame_size;
6298
6299       /* Try to predict whether we'll need the base register.  */
6300       base_used = cfun->machine->split_branches_pending_p
6301                   || current_function_uses_const_pool
6302                   || (!DISP_IN_RANGE (-frame_size)
6303                       && !CONST_OK_FOR_K (-frame_size));
6304
6305       /* Decide which register to use as literal pool base.  In small
6306          leaf functions, try to use an unused call-clobbered register
6307          as base register to avoid save/restore overhead.  */
6308       if (!base_used)
6309         cfun->machine->base_reg = NULL_RTX;
6310       else if (current_function_is_leaf && !regs_ever_live[5])
6311         cfun->machine->base_reg = gen_rtx_REG (Pmode, 5);
6312       else
6313         cfun->machine->base_reg = gen_rtx_REG (Pmode, BASE_REGNUM);
6314
6315       s390_register_info (clobbered_regs);
6316       s390_frame_info ();
6317     }
6318   while (frame_size != cfun_frame_layout.frame_size);
6319 }
6320
6321 /* Update frame layout.  Recompute actual register save data based on
6322    current info and update regs_ever_live for the special registers.
6323    May be called multiple times, but may never cause *more* registers
6324    to be saved than s390_init_frame_layout allocated room for.  */
6325
6326 static void
6327 s390_update_frame_layout (void)
6328 {
6329   int clobbered_regs[16];
6330
6331   s390_register_info (clobbered_regs);
6332
6333   regs_ever_live[BASE_REGNUM] = clobbered_regs[BASE_REGNUM];
6334   regs_ever_live[RETURN_REGNUM] = clobbered_regs[RETURN_REGNUM];
6335   regs_ever_live[STACK_POINTER_REGNUM] = clobbered_regs[STACK_POINTER_REGNUM];
6336
6337   if (cfun->machine->base_reg)
6338     regs_ever_live[REGNO (cfun->machine->base_reg)] = 1;
6339 }
6340
6341 /* Return nonzero if register OLD_REG can be renamed to register NEW_REG.  */
6342
6343 bool
6344 s390_hard_regno_rename_ok (unsigned int old_reg, unsigned int new_reg)
6345 {
6346    /* Once we've decided upon a register to use as base register, it must
6347       no longer be used for any other purpose.  */
6348   if (cfun->machine->base_reg)
6349     if (REGNO (cfun->machine->base_reg) == old_reg
6350         || REGNO (cfun->machine->base_reg) == new_reg)
6351       return false;
6352
6353   return true;
6354 }
6355
6356 /* Return true if register FROM can be eliminated via register TO.  */
6357
6358 bool
6359 s390_can_eliminate (int from, int to)
6360 {
6361   /* On zSeries machines, we have not marked the base register as fixed.
6362      Instead, we have an elimination rule BASE_REGNUM -> BASE_REGNUM.
6363      If a function requires the base register, we say here that this
6364      elimination cannot be performed.  This will cause reload to free
6365      up the base register (as if it were fixed).  On the other hand,
6366      if the current function does *not* require the base register, we
6367      say here the elimination succeeds, which in turn allows reload
6368      to allocate the base register for any other purpose.  */
6369   if (from == BASE_REGNUM && to == BASE_REGNUM)
6370     {
6371       if (TARGET_CPU_ZARCH)
6372         {
6373           s390_init_frame_layout ();
6374           return cfun->machine->base_reg == NULL_RTX;
6375         }
6376
6377       return false;
6378     }
6379
6380   /* Everything else must point into the stack frame.  */
6381   gcc_assert (to == STACK_POINTER_REGNUM
6382               || to == HARD_FRAME_POINTER_REGNUM);
6383
6384   gcc_assert (from == FRAME_POINTER_REGNUM
6385               || from == ARG_POINTER_REGNUM
6386               || from == RETURN_ADDRESS_POINTER_REGNUM);
6387
6388   /* Make sure we actually saved the return address.  */
6389   if (from == RETURN_ADDRESS_POINTER_REGNUM)
6390     if (!current_function_calls_eh_return
6391         && !current_function_stdarg
6392         && !cfun_frame_layout.save_return_addr_p)
6393       return false;
6394
6395   return true;
6396 }
6397
6398 /* Return offset between register FROM and TO initially after prolog.  */
6399
6400 HOST_WIDE_INT
6401 s390_initial_elimination_offset (int from, int to)
6402 {
6403   HOST_WIDE_INT offset;
6404   int index;
6405
6406   /* ??? Why are we called for non-eliminable pairs?  */
6407   if (!s390_can_eliminate (from, to))
6408     return 0;
6409
6410   switch (from)
6411     {
6412     case FRAME_POINTER_REGNUM:
6413       offset = (get_frame_size() 
6414                 + STACK_POINTER_OFFSET
6415                 + current_function_outgoing_args_size);
6416       break;
6417
6418     case ARG_POINTER_REGNUM:
6419       s390_init_frame_layout ();
6420       offset = cfun_frame_layout.frame_size + STACK_POINTER_OFFSET;
6421       break;
6422
6423     case RETURN_ADDRESS_POINTER_REGNUM:
6424       s390_init_frame_layout ();
6425       index = RETURN_REGNUM - cfun_frame_layout.first_save_gpr;
6426       gcc_assert (index >= 0);
6427       offset = cfun_frame_layout.frame_size + cfun_frame_layout.gprs_offset;
6428       offset += index * UNITS_PER_WORD;
6429       break;
6430
6431     case BASE_REGNUM:
6432       offset = 0;
6433       break;
6434
6435     default:
6436       gcc_unreachable ();
6437     }
6438
6439   return offset;
6440 }
6441
6442 /* Emit insn to save fpr REGNUM at offset OFFSET relative
6443    to register BASE.  Return generated insn.  */
6444
6445 static rtx
6446 save_fpr (rtx base, int offset, int regnum)
6447 {
6448   rtx addr;
6449   addr = gen_rtx_MEM (DFmode, plus_constant (base, offset));
6450
6451   if (regnum >= 16 && regnum <= (16 + FP_ARG_NUM_REG))
6452     set_mem_alias_set (addr, get_varargs_alias_set ());
6453   else
6454     set_mem_alias_set (addr, get_frame_alias_set ());
6455
6456   return emit_move_insn (addr, gen_rtx_REG (DFmode, regnum));
6457 }
6458
6459 /* Emit insn to restore fpr REGNUM from offset OFFSET relative
6460    to register BASE.  Return generated insn.  */
6461
6462 static rtx
6463 restore_fpr (rtx base, int offset, int regnum)
6464 {
6465   rtx addr;
6466   addr = gen_rtx_MEM (DFmode, plus_constant (base, offset));
6467   set_mem_alias_set (addr, get_frame_alias_set ());
6468
6469   return emit_move_insn (gen_rtx_REG (DFmode, regnum), addr);
6470 }
6471
6472 /* Generate insn to save registers FIRST to LAST into
6473    the register save area located at offset OFFSET
6474    relative to register BASE.  */
6475
6476 static rtx
6477 save_gprs (rtx base, int offset, int first, int last)
6478 {
6479   rtx addr, insn, note;
6480   int i;
6481
6482   addr = plus_constant (base, offset);
6483   addr = gen_rtx_MEM (Pmode, addr);
6484
6485   set_mem_alias_set (addr, get_frame_alias_set ());
6486
6487   /* Special-case single register.  */
6488   if (first == last)
6489     {
6490       if (TARGET_64BIT)
6491         insn = gen_movdi (addr, gen_rtx_REG (Pmode, first));
6492       else
6493         insn = gen_movsi (addr, gen_rtx_REG (Pmode, first));
6494
6495       RTX_FRAME_RELATED_P (insn) = 1;
6496       return insn;
6497     }
6498
6499
6500   insn = gen_store_multiple (addr,
6501                              gen_rtx_REG (Pmode, first),
6502                              GEN_INT (last - first + 1));
6503
6504   if (first <= 6 && current_function_stdarg)
6505     for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
6506       {
6507         rtx mem = XEXP (XVECEXP (PATTERN (insn), 0, i), 0);
6508         
6509         if (first + i <= 6)
6510           set_mem_alias_set (mem, get_varargs_alias_set ());
6511       }
6512
6513   /* We need to set the FRAME_RELATED flag on all SETs
6514      inside the store-multiple pattern.
6515
6516      However, we must not emit DWARF records for registers 2..5
6517      if they are stored for use by variable arguments ...
6518
6519      ??? Unfortunately, it is not enough to simply not the
6520      FRAME_RELATED flags for those SETs, because the first SET
6521      of the PARALLEL is always treated as if it had the flag
6522      set, even if it does not.  Therefore we emit a new pattern
6523      without those registers as REG_FRAME_RELATED_EXPR note.  */
6524
6525   if (first >= 6)
6526     {
6527       rtx pat = PATTERN (insn);
6528
6529       for (i = 0; i < XVECLEN (pat, 0); i++)
6530         if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
6531           RTX_FRAME_RELATED_P (XVECEXP (pat, 0, i)) = 1;
6532
6533       RTX_FRAME_RELATED_P (insn) = 1;
6534     }
6535   else if (last >= 6)
6536     {
6537       addr = plus_constant (base, offset + (6 - first) * UNITS_PER_WORD);
6538       note = gen_store_multiple (gen_rtx_MEM (Pmode, addr),
6539                                  gen_rtx_REG (Pmode, 6),
6540                                  GEN_INT (last - 6 + 1));
6541       note = PATTERN (note);
6542
6543       REG_NOTES (insn) =
6544         gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
6545                            note, REG_NOTES (insn));
6546
6547       for (i = 0; i < XVECLEN (note, 0); i++)
6548         if (GET_CODE (XVECEXP (note, 0, i)) == SET)
6549           RTX_FRAME_RELATED_P (XVECEXP (note, 0, i)) = 1;
6550
6551       RTX_FRAME_RELATED_P (insn) = 1;
6552     }
6553
6554   return insn;
6555 }
6556
6557 /* Generate insn to restore registers FIRST to LAST from
6558    the register save area located at offset OFFSET
6559    relative to register BASE.  */
6560
6561 static rtx
6562 restore_gprs (rtx base, int offset, int first, int last)
6563 {
6564   rtx addr, insn;
6565
6566   addr = plus_constant (base, offset);
6567   addr = gen_rtx_MEM (Pmode, addr);
6568   set_mem_alias_set (addr, get_frame_alias_set ());
6569
6570   /* Special-case single register.  */
6571   if (first == last)
6572     {
6573       if (TARGET_64BIT)
6574         insn = gen_movdi (gen_rtx_REG (Pmode, first), addr);
6575       else
6576         insn = gen_movsi (gen_rtx_REG (Pmode, first), addr);
6577
6578       return insn;
6579     }
6580
6581   insn = gen_load_multiple (gen_rtx_REG (Pmode, first),
6582                             addr,
6583                             GEN_INT (last - first + 1));
6584   return insn;
6585 }
6586
6587 /* Return insn sequence to load the GOT register.  */
6588
6589 static GTY(()) rtx got_symbol;
6590 rtx
6591 s390_load_got (void)
6592 {
6593   rtx insns;
6594
6595   if (!got_symbol)
6596     {
6597       got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
6598       SYMBOL_REF_FLAGS (got_symbol) = SYMBOL_FLAG_LOCAL;
6599     }
6600
6601   start_sequence ();
6602
6603   if (TARGET_CPU_ZARCH)
6604     {
6605       emit_move_insn (pic_offset_table_rtx, got_symbol);
6606     }
6607   else
6608     {
6609       rtx offset;
6610
6611       offset = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, got_symbol),
6612                                UNSPEC_LTREL_OFFSET);
6613       offset = gen_rtx_CONST (Pmode, offset);
6614       offset = force_const_mem (Pmode, offset);
6615
6616       emit_move_insn (pic_offset_table_rtx, offset);
6617
6618       offset = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (offset, 0)),
6619                                UNSPEC_LTREL_BASE);
6620       offset = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, offset);
6621
6622       emit_move_insn (pic_offset_table_rtx, offset);
6623     }
6624
6625   insns = get_insns ();
6626   end_sequence ();
6627   return insns;
6628 }
6629
6630 /* Expand the prologue into a bunch of separate insns.  */
6631
6632 void
6633 s390_emit_prologue (void)
6634 {
6635   rtx insn, addr;
6636   rtx temp_reg;
6637   int i;
6638   int offset;
6639   int next_fpr = 0;
6640
6641   /* Complete frame layout.  */
6642
6643   s390_update_frame_layout ();
6644
6645   /* Annotate all constant pool references to let the scheduler know
6646      they implicitly use the base register.  */
6647
6648   push_topmost_sequence ();
6649
6650   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6651     if (INSN_P (insn))
6652       annotate_constant_pool_refs (&PATTERN (insn));
6653
6654   pop_topmost_sequence ();
6655
6656   /* Choose best register to use for temp use within prologue.
6657      See below for why TPF must use the register 1.  */
6658
6659   if (!has_hard_reg_initial_val (Pmode, RETURN_REGNUM) 
6660       && !current_function_is_leaf 
6661       && !TARGET_TPF_PROFILING)
6662     temp_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
6663   else
6664     temp_reg = gen_rtx_REG (Pmode, 1);
6665
6666   /* Save call saved gprs.  */
6667   if (cfun_frame_layout.first_save_gpr != -1)
6668     {
6669       insn = save_gprs (stack_pointer_rtx, 
6670                         cfun_frame_layout.gprs_offset,
6671                         cfun_frame_layout.first_save_gpr, 
6672                         cfun_frame_layout.last_save_gpr);
6673       emit_insn (insn);
6674     }
6675
6676   /* Dummy insn to mark literal pool slot.  */
6677
6678   if (cfun->machine->base_reg)
6679     emit_insn (gen_main_pool (cfun->machine->base_reg));
6680
6681   offset = cfun_frame_layout.f0_offset;
6682
6683   /* Save f0 and f2.  */
6684   for (i = 0; i < 2; i++)
6685     {
6686       if (cfun_fpr_bit_p (i))
6687         {
6688           save_fpr (stack_pointer_rtx, offset, i + 16);
6689           offset += 8;
6690         }
6691       else if (!TARGET_PACKED_STACK)
6692           offset += 8;
6693     }
6694
6695   /* Save f4 and f6.  */
6696   offset = cfun_frame_layout.f4_offset;
6697   for (i = 2; i < 4; i++)
6698     {
6699       if (cfun_fpr_bit_p (i))
6700         {
6701           insn = save_fpr (stack_pointer_rtx, offset, i + 16);
6702           offset += 8;
6703
6704           /* If f4 and f6 are call clobbered they are saved due to stdargs and
6705              therefore are not frame related.  */
6706           if (!call_really_used_regs[i + 16])
6707             RTX_FRAME_RELATED_P (insn) = 1;
6708         }
6709       else if (!TARGET_PACKED_STACK)
6710         offset += 8;
6711     }
6712
6713   if (TARGET_PACKED_STACK
6714       && cfun_save_high_fprs_p
6715       && cfun_frame_layout.f8_offset + cfun_frame_layout.high_fprs * 8 > 0)
6716     {
6717       offset = (cfun_frame_layout.f8_offset
6718                 + (cfun_frame_layout.high_fprs - 1) * 8);
6719
6720       for (i = 15; i > 7 && offset >= 0; i--)
6721         if (cfun_fpr_bit_p (i))
6722           {
6723             insn = save_fpr (stack_pointer_rtx, offset, i + 16);
6724                        
6725             RTX_FRAME_RELATED_P (insn) = 1;
6726             offset -= 8;
6727           }
6728       if (offset >= cfun_frame_layout.f8_offset)
6729         next_fpr = i + 16;
6730     }
6731   
6732   if (!TARGET_PACKED_STACK)
6733     next_fpr = cfun_save_high_fprs_p ? 31 : 0;
6734
6735   /* Decrement stack pointer.  */
6736
6737   if (cfun_frame_layout.frame_size > 0)
6738     {
6739       rtx frame_off = GEN_INT (-cfun_frame_layout.frame_size);
6740
6741       if (s390_stack_size)
6742         {
6743           HOST_WIDE_INT stack_check_mask = ((s390_stack_size - 1)
6744                                             & ~(s390_stack_guard - 1));
6745           rtx t = gen_rtx_AND (Pmode, stack_pointer_rtx,
6746                                GEN_INT (stack_check_mask));
6747
6748           if (TARGET_64BIT)
6749             gen_cmpdi (t, const0_rtx);
6750           else
6751             gen_cmpsi (t, const0_rtx);
6752
6753           emit_insn (gen_conditional_trap (gen_rtx_EQ (CCmode, 
6754                                                        gen_rtx_REG (CCmode, 
6755                                                                     CC_REGNUM),
6756                                                        const0_rtx),
6757                                            const0_rtx));
6758         }
6759
6760       if (s390_warn_framesize > 0 
6761           && cfun_frame_layout.frame_size >= s390_warn_framesize)
6762         warning (0, "frame size of %qs is " HOST_WIDE_INT_PRINT_DEC " bytes", 
6763                  current_function_name (), cfun_frame_layout.frame_size);
6764
6765       if (s390_warn_dynamicstack_p && cfun->calls_alloca)
6766         warning (0, "%qs uses dynamic stack allocation", current_function_name ());
6767
6768       /* Save incoming stack pointer into temp reg.  */
6769       if (TARGET_BACKCHAIN || next_fpr)
6770         insn = emit_insn (gen_move_insn (temp_reg, stack_pointer_rtx));
6771
6772       /* Subtract frame size from stack pointer.  */
6773
6774       if (DISP_IN_RANGE (INTVAL (frame_off)))
6775         {
6776           insn = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
6777                               gen_rtx_PLUS (Pmode, stack_pointer_rtx, 
6778                                             frame_off));
6779           insn = emit_insn (insn);
6780         }
6781       else
6782         {
6783           if (!CONST_OK_FOR_K (INTVAL (frame_off)))
6784             frame_off = force_const_mem (Pmode, frame_off);
6785
6786           insn = emit_insn (gen_add2_insn (stack_pointer_rtx, frame_off));
6787           annotate_constant_pool_refs (&PATTERN (insn));
6788         }
6789
6790       RTX_FRAME_RELATED_P (insn) = 1;
6791       REG_NOTES (insn) =
6792         gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
6793                            gen_rtx_SET (VOIDmode, stack_pointer_rtx,
6794                              gen_rtx_PLUS (Pmode, stack_pointer_rtx,
6795                                GEN_INT (-cfun_frame_layout.frame_size))),
6796                            REG_NOTES (insn));
6797
6798       /* Set backchain.  */
6799
6800       if (TARGET_BACKCHAIN)
6801         {
6802           if (cfun_frame_layout.backchain_offset)
6803             addr = gen_rtx_MEM (Pmode, 
6804                                 plus_constant (stack_pointer_rtx, 
6805                                   cfun_frame_layout.backchain_offset));
6806           else
6807             addr = gen_rtx_MEM (Pmode, stack_pointer_rtx);  
6808           set_mem_alias_set (addr, get_frame_alias_set ());
6809           insn = emit_insn (gen_move_insn (addr, temp_reg));
6810         }
6811
6812       /* If we support asynchronous exceptions (e.g. for Java),
6813          we need to make sure the backchain pointer is set up
6814          before any possibly trapping memory access.  */
6815
6816       if (TARGET_BACKCHAIN && flag_non_call_exceptions)
6817         {
6818           addr = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
6819           emit_insn (gen_rtx_CLOBBER (VOIDmode, addr));
6820         }
6821     }
6822
6823   /* Save fprs 8 - 15 (64 bit ABI).  */
6824
6825   if (cfun_save_high_fprs_p && next_fpr)
6826     {
6827       insn = emit_insn (gen_add2_insn (temp_reg, 
6828                                        GEN_INT (cfun_frame_layout.f8_offset)));
6829
6830       offset = 0;
6831
6832       for (i = 24; i <= next_fpr; i++)
6833         if (cfun_fpr_bit_p (i - 16))
6834           {
6835             rtx addr = plus_constant (stack_pointer_rtx,
6836                                       cfun_frame_layout.frame_size
6837                                       + cfun_frame_layout.f8_offset
6838                                       + offset);
6839             
6840             insn = save_fpr (temp_reg, offset, i);
6841             offset += 8;
6842             RTX_FRAME_RELATED_P (insn) = 1;
6843             REG_NOTES (insn) =
6844               gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
6845                                  gen_rtx_SET (VOIDmode,
6846                                               gen_rtx_MEM (DFmode, addr),
6847                                               gen_rtx_REG (DFmode, i)),
6848                                  REG_NOTES (insn));
6849           }
6850     }
6851
6852   /* Set frame pointer, if needed.  */
6853
6854   if (frame_pointer_needed)
6855     {
6856       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
6857       RTX_FRAME_RELATED_P (insn) = 1;
6858     }
6859
6860   /* Set up got pointer, if needed.  */
6861
6862   if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
6863     {
6864       rtx insns = s390_load_got ();
6865
6866       for (insn = insns; insn; insn = NEXT_INSN (insn))
6867         {
6868           annotate_constant_pool_refs (&PATTERN (insn));
6869
6870           REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, NULL_RTX,
6871                                                 REG_NOTES (insn));
6872         }
6873
6874       emit_insn (insns);
6875     }
6876
6877   if (TARGET_TPF_PROFILING)
6878     {
6879       /* Generate a BAS instruction to serve as a function
6880          entry intercept to facilitate the use of tracing
6881          algorithms located at the branch target.  */
6882       emit_insn (gen_prologue_tpf ());
6883
6884       /* Emit a blockage here so that all code
6885          lies between the profiling mechanisms.  */
6886       emit_insn (gen_blockage ());
6887     }
6888 }
6889
6890 /* Expand the epilogue into a bunch of separate insns.  */
6891
6892 void
6893 s390_emit_epilogue (bool sibcall)
6894 {
6895   rtx frame_pointer, return_reg;
6896   int area_bottom, area_top, offset = 0;
6897   int next_offset;
6898   rtvec p;
6899   int i;
6900
6901   if (TARGET_TPF_PROFILING)
6902     {
6903
6904       /* Generate a BAS instruction to serve as a function
6905          entry intercept to facilitate the use of tracing
6906          algorithms located at the branch target.  */
6907
6908       /* Emit a blockage here so that all code
6909          lies between the profiling mechanisms.  */
6910       emit_insn (gen_blockage ());
6911
6912       emit_insn (gen_epilogue_tpf ());
6913     }
6914
6915   /* Check whether to use frame or stack pointer for restore.  */
6916
6917   frame_pointer = (frame_pointer_needed 
6918                    ? hard_frame_pointer_rtx : stack_pointer_rtx);
6919
6920   s390_frame_area (&area_bottom, &area_top);
6921
6922   /* Check whether we can access the register save area.
6923      If not, increment the frame pointer as required.  */
6924
6925   if (area_top <= area_bottom)
6926     {
6927       /* Nothing to restore.  */
6928     }
6929   else if (DISP_IN_RANGE (cfun_frame_layout.frame_size + area_bottom)
6930            && DISP_IN_RANGE (cfun_frame_layout.frame_size + area_top - 1))
6931     {
6932       /* Area is in range.  */
6933       offset = cfun_frame_layout.frame_size;
6934     }
6935   else
6936     {
6937       rtx insn, frame_off;
6938
6939       offset = area_bottom < 0 ? -area_bottom : 0;
6940       frame_off = GEN_INT (cfun_frame_layout.frame_size - offset);
6941
6942       if (DISP_IN_RANGE (INTVAL (frame_off)))
6943         {
6944           insn = gen_rtx_SET (VOIDmode, frame_pointer,
6945                               gen_rtx_PLUS (Pmode, frame_pointer, frame_off));
6946           insn = emit_insn (insn);
6947         }
6948       else
6949         {
6950           if (!CONST_OK_FOR_K (INTVAL (frame_off)))
6951             frame_off = force_const_mem (Pmode, frame_off);
6952
6953           insn = emit_insn (gen_add2_insn (frame_pointer, frame_off));
6954           annotate_constant_pool_refs (&PATTERN (insn));
6955         }
6956     }
6957
6958   /* Restore call saved fprs.  */
6959
6960   if (TARGET_64BIT)
6961     {
6962       if (cfun_save_high_fprs_p)
6963         {
6964           next_offset = cfun_frame_layout.f8_offset;
6965           for (i = 24; i < 32; i++)
6966             {
6967               if (cfun_fpr_bit_p (i - 16))
6968                 {
6969                   restore_fpr (frame_pointer,
6970                                offset + next_offset, i);
6971                   next_offset += 8;
6972                 }
6973             }
6974         }
6975               
6976     }
6977   else
6978     {
6979       next_offset = cfun_frame_layout.f4_offset;
6980       for (i = 18; i < 20; i++)
6981         {
6982           if (cfun_fpr_bit_p (i - 16))
6983             {
6984               restore_fpr (frame_pointer,
6985                            offset + next_offset, i);
6986               next_offset += 8;
6987             }
6988           else if (!TARGET_PACKED_STACK)
6989             next_offset += 8;
6990         }
6991       
6992     }
6993
6994   /* Return register.  */
6995
6996   return_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
6997
6998   /* Restore call saved gprs.  */
6999
7000   if (cfun_frame_layout.first_restore_gpr != -1)
7001     {
7002       rtx insn, addr;
7003       int i;
7004
7005       /* Check for global register and save them
7006          to stack location from where they get restored.  */
7007
7008       for (i = cfun_frame_layout.first_restore_gpr;
7009            i <= cfun_frame_layout.last_restore_gpr;
7010            i++)
7011         {
7012           /* These registers are special and need to be
7013              restored in any case.  */
7014           if (i == STACK_POINTER_REGNUM
7015               || i == RETURN_REGNUM
7016               || i == BASE_REGNUM
7017               || (flag_pic && i == (int)PIC_OFFSET_TABLE_REGNUM))
7018             continue;
7019
7020           if (global_regs[i])
7021             {
7022               addr = plus_constant (frame_pointer,
7023                                     offset + cfun_frame_layout.gprs_offset 
7024                                     + (i - cfun_frame_layout.first_save_gpr)
7025                                     * UNITS_PER_WORD);
7026               addr = gen_rtx_MEM (Pmode, addr);
7027               set_mem_alias_set (addr, get_frame_alias_set ());
7028               emit_move_insn (addr, gen_rtx_REG (Pmode, i));
7029             }
7030         }
7031
7032       if (! sibcall)
7033         {
7034           /* Fetch return address from stack before load multiple,
7035              this will do good for scheduling.  */
7036
7037           if (cfun_frame_layout.save_return_addr_p
7038               || (cfun_frame_layout.first_restore_gpr < BASE_REGNUM
7039                   && cfun_frame_layout.last_restore_gpr > RETURN_REGNUM))
7040             {
7041               int return_regnum = find_unused_clobbered_reg();
7042               if (!return_regnum)
7043                 return_regnum = 4;
7044               return_reg = gen_rtx_REG (Pmode, return_regnum);
7045
7046               addr = plus_constant (frame_pointer,
7047                                     offset + cfun_frame_layout.gprs_offset
7048                                     + (RETURN_REGNUM 
7049                                        - cfun_frame_layout.first_save_gpr)
7050                                     * UNITS_PER_WORD);
7051               addr = gen_rtx_MEM (Pmode, addr);
7052               set_mem_alias_set (addr, get_frame_alias_set ());
7053               emit_move_insn (return_reg, addr);
7054             }
7055         }
7056
7057       insn = restore_gprs (frame_pointer,
7058                            offset + cfun_frame_layout.gprs_offset
7059                            + (cfun_frame_layout.first_restore_gpr 
7060                               - cfun_frame_layout.first_save_gpr)
7061                            * UNITS_PER_WORD,
7062                            cfun_frame_layout.first_restore_gpr,
7063                            cfun_frame_layout.last_restore_gpr);
7064       emit_insn (insn);
7065     }
7066
7067   if (! sibcall)
7068     {
7069
7070       /* Return to caller.  */
7071
7072       p = rtvec_alloc (2);
7073
7074       RTVEC_ELT (p, 0) = gen_rtx_RETURN (VOIDmode);
7075       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, return_reg);
7076       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
7077     }
7078 }
7079
7080
7081 /* Return the size in bytes of a function argument of
7082    type TYPE and/or mode MODE.  At least one of TYPE or
7083    MODE must be specified.  */
7084
7085 static int
7086 s390_function_arg_size (enum machine_mode mode, tree type)
7087 {
7088   if (type)
7089     return int_size_in_bytes (type);
7090
7091   /* No type info available for some library calls ...  */
7092   if (mode != BLKmode)
7093     return GET_MODE_SIZE (mode);
7094
7095   /* If we have neither type nor mode, abort */
7096   gcc_unreachable ();
7097 }
7098
7099 /* Return true if a function argument of type TYPE and mode MODE
7100    is to be passed in a floating-point register, if available.  */
7101
7102 static bool
7103 s390_function_arg_float (enum machine_mode mode, tree type)
7104 {
7105   int size = s390_function_arg_size (mode, type);
7106   if (size > 8)
7107     return false;
7108
7109   /* Soft-float changes the ABI: no floating-point registers are used.  */
7110   if (TARGET_SOFT_FLOAT)
7111     return false;
7112
7113   /* No type info available for some library calls ...  */
7114   if (!type)
7115     return mode == SFmode || mode == DFmode;
7116
7117   /* The ABI says that record types with a single member are treated
7118      just like that member would be.  */
7119   while (TREE_CODE (type) == RECORD_TYPE)
7120     {
7121       tree field, single = NULL_TREE;
7122
7123       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
7124         {
7125           if (TREE_CODE (field) != FIELD_DECL)
7126             continue;
7127
7128           if (single == NULL_TREE)
7129             single = TREE_TYPE (field);
7130           else
7131             return false;
7132         }
7133
7134       if (single == NULL_TREE)
7135         return false;
7136       else
7137         type = single;
7138     }
7139
7140   return TREE_CODE (type) == REAL_TYPE;
7141 }
7142
7143 /* Return true if a function argument of type TYPE and mode MODE
7144    is to be passed in an integer register, or a pair of integer
7145    registers, if available.  */
7146
7147 static bool
7148 s390_function_arg_integer (enum machine_mode mode, tree type)
7149 {
7150   int size = s390_function_arg_size (mode, type);
7151   if (size > 8)
7152     return false;
7153
7154   /* No type info available for some library calls ...  */
7155   if (!type)
7156     return GET_MODE_CLASS (mode) == MODE_INT
7157            || (TARGET_SOFT_FLOAT &&  GET_MODE_CLASS (mode) == MODE_FLOAT);
7158
7159   /* We accept small integral (and similar) types.  */
7160   if (INTEGRAL_TYPE_P (type)
7161       || POINTER_TYPE_P (type)
7162       || TREE_CODE (type) == OFFSET_TYPE
7163       || (TARGET_SOFT_FLOAT && TREE_CODE (type) == REAL_TYPE))
7164     return true;
7165
7166   /* We also accept structs of size 1, 2, 4, 8 that are not
7167      passed in floating-point registers.  */
7168   if (AGGREGATE_TYPE_P (type)
7169       && exact_log2 (size) >= 0
7170       && !s390_function_arg_float (mode, type))
7171     return true;
7172
7173   return false;
7174 }
7175
7176 /* Return 1 if a function argument of type TYPE and mode MODE
7177    is to be passed by reference.  The ABI specifies that only
7178    structures of size 1, 2, 4, or 8 bytes are passed by value,
7179    all other structures (and complex numbers) are passed by
7180    reference.  */
7181
7182 static bool
7183 s390_pass_by_reference (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED,
7184                         enum machine_mode mode, tree type,
7185                         bool named ATTRIBUTE_UNUSED)
7186 {
7187   int size = s390_function_arg_size (mode, type);
7188   if (size > 8)
7189     return true;
7190
7191   if (type)
7192     {
7193       if (AGGREGATE_TYPE_P (type) && exact_log2 (size) < 0)
7194         return 1;
7195
7196       if (TREE_CODE (type) == COMPLEX_TYPE
7197           || TREE_CODE (type) == VECTOR_TYPE)
7198         return 1;
7199     }
7200
7201   return 0;
7202 }
7203
7204 /* Update the data in CUM to advance over an argument of mode MODE and
7205    data type TYPE.  (TYPE is null for libcalls where that information
7206    may not be available.).  The boolean NAMED specifies whether the
7207    argument is a named argument (as opposed to an unnamed argument
7208    matching an ellipsis).  */
7209
7210 void
7211 s390_function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
7212                            tree type, int named ATTRIBUTE_UNUSED)
7213 {
7214   if (s390_function_arg_float (mode, type))
7215     {
7216       cum->fprs += 1;
7217     }
7218   else if (s390_function_arg_integer (mode, type))
7219     {
7220       int size = s390_function_arg_size (mode, type);
7221       cum->gprs += ((size + UNITS_PER_WORD-1) / UNITS_PER_WORD);
7222     }
7223   else
7224     gcc_unreachable ();
7225 }
7226
7227 /* Define where to put the arguments to a function.
7228    Value is zero to push the argument on the stack,
7229    or a hard register in which to store the argument.
7230
7231    MODE is the argument's machine mode.
7232    TYPE is the data type of the argument (as a tree).
7233     This is null for libcalls where that information may
7234     not be available.
7235    CUM is a variable of type CUMULATIVE_ARGS which gives info about
7236     the preceding args and about the function being called.
7237    NAMED is nonzero if this argument is a named parameter
7238     (otherwise it is an extra parameter matching an ellipsis).
7239
7240    On S/390, we use general purpose registers 2 through 6 to
7241    pass integer, pointer, and certain structure arguments, and
7242    floating point registers 0 and 2 (0, 2, 4, and 6 on 64-bit)
7243    to pass floating point arguments.  All remaining arguments
7244    are pushed to the stack.  */
7245
7246 rtx
7247 s390_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, tree type,
7248                    int named ATTRIBUTE_UNUSED)
7249 {
7250   if (s390_function_arg_float (mode, type))
7251     {
7252       if (cum->fprs + 1 > FP_ARG_NUM_REG)
7253         return 0;
7254       else
7255         return gen_rtx_REG (mode, cum->fprs + 16);
7256     }
7257   else if (s390_function_arg_integer (mode, type))
7258     {
7259       int size = s390_function_arg_size (mode, type);
7260       int n_gprs = (size + UNITS_PER_WORD-1) / UNITS_PER_WORD;
7261
7262       if (cum->gprs + n_gprs > GP_ARG_NUM_REG)
7263         return 0;
7264       else
7265         return gen_rtx_REG (mode, cum->gprs + 2);
7266     }
7267
7268   /* After the real arguments, expand_call calls us once again
7269      with a void_type_node type.  Whatever we return here is
7270      passed as operand 2 to the call expanders.
7271
7272      We don't need this feature ...  */
7273   else if (type == void_type_node)
7274     return const0_rtx;
7275
7276   gcc_unreachable ();
7277 }
7278
7279 /* Return true if return values of type TYPE should be returned
7280    in a memory buffer whose address is passed by the caller as
7281    hidden first argument.  */
7282
7283 static bool
7284 s390_return_in_memory (tree type, tree fundecl ATTRIBUTE_UNUSED)
7285 {
7286   /* We accept small integral (and similar) types.  */
7287   if (INTEGRAL_TYPE_P (type)
7288       || POINTER_TYPE_P (type)
7289       || TREE_CODE (type) == OFFSET_TYPE
7290       || TREE_CODE (type) == REAL_TYPE)
7291     return int_size_in_bytes (type) > 8;
7292
7293   /* Aggregates and similar constructs are always returned
7294      in memory.  */
7295   if (AGGREGATE_TYPE_P (type)
7296       || TREE_CODE (type) == COMPLEX_TYPE
7297       || TREE_CODE (type) == VECTOR_TYPE)
7298     return true;
7299
7300   /* ??? We get called on all sorts of random stuff from
7301      aggregate_value_p.  We can't abort, but it's not clear
7302      what's safe to return.  Pretend it's a struct I guess.  */
7303   return true;
7304 }
7305
7306 /* Define where to return a (scalar) value of type TYPE.
7307    If TYPE is null, define where to return a (scalar)
7308    value of mode MODE from a libcall.  */
7309
7310 rtx
7311 s390_function_value (tree type, enum machine_mode mode)
7312 {
7313   if (type)
7314     {
7315       int unsignedp = TYPE_UNSIGNED (type);
7316       mode = promote_mode (type, TYPE_MODE (type), &unsignedp, 1);
7317     }
7318
7319   gcc_assert (GET_MODE_CLASS (mode) == MODE_INT
7320                || GET_MODE_CLASS (mode) == MODE_FLOAT);
7321   gcc_assert (GET_MODE_SIZE (mode) <= 8);
7322
7323   if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
7324     return gen_rtx_REG (mode, 16);
7325   else
7326     return gen_rtx_REG (mode, 2);
7327 }
7328
7329
7330 /* Create and return the va_list datatype.
7331
7332    On S/390, va_list is an array type equivalent to
7333
7334       typedef struct __va_list_tag
7335         {
7336             long __gpr;
7337             long __fpr;
7338             void *__overflow_arg_area;
7339             void *__reg_save_area;
7340         } va_list[1];
7341
7342    where __gpr and __fpr hold the number of general purpose
7343    or floating point arguments used up to now, respectively,
7344    __overflow_arg_area points to the stack location of the
7345    next argument passed on the stack, and __reg_save_area
7346    always points to the start of the register area in the
7347    call frame of the current function.  The function prologue
7348    saves all registers used for argument passing into this
7349    area if the function uses variable arguments.  */
7350
7351 static tree
7352 s390_build_builtin_va_list (void)
7353 {
7354   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
7355
7356   record = lang_hooks.types.make_type (RECORD_TYPE);
7357
7358   type_decl =
7359     build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
7360
7361   f_gpr = build_decl (FIELD_DECL, get_identifier ("__gpr"),
7362                       long_integer_type_node);
7363   f_fpr = build_decl (FIELD_DECL, get_identifier ("__fpr"),
7364                       long_integer_type_node);
7365   f_ovf = build_decl (FIELD_DECL, get_identifier ("__overflow_arg_area"),
7366                       ptr_type_node);
7367   f_sav = build_decl (FIELD_DECL, get_identifier ("__reg_save_area"),
7368                       ptr_type_node);
7369
7370   va_list_gpr_counter_field = f_gpr;
7371   va_list_fpr_counter_field = f_fpr;
7372
7373   DECL_FIELD_CONTEXT (f_gpr) = record;
7374   DECL_FIELD_CONTEXT (f_fpr) = record;
7375   DECL_FIELD_CONTEXT (f_ovf) = record;
7376   DECL_FIELD_CONTEXT (f_sav) = record;
7377
7378   TREE_CHAIN (record) = type_decl;
7379   TYPE_NAME (record) = type_decl;
7380   TYPE_FIELDS (record) = f_gpr;
7381   TREE_CHAIN (f_gpr) = f_fpr;
7382   TREE_CHAIN (f_fpr) = f_ovf;
7383   TREE_CHAIN (f_ovf) = f_sav;
7384
7385   layout_type (record);
7386
7387   /* The correct type is an array type of one element.  */
7388   return build_array_type (record, build_index_type (size_zero_node));
7389 }
7390
7391 /* Implement va_start by filling the va_list structure VALIST.
7392    STDARG_P is always true, and ignored.
7393    NEXTARG points to the first anonymous stack argument.
7394
7395    The following global variables are used to initialize
7396    the va_list structure:
7397
7398      current_function_args_info:
7399        holds number of gprs and fprs used for named arguments.
7400      current_function_arg_offset_rtx:
7401        holds the offset of the first anonymous stack argument
7402        (relative to the virtual arg pointer).  */
7403
7404 void
7405 s390_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
7406 {
7407   HOST_WIDE_INT n_gpr, n_fpr;
7408   int off;
7409   tree f_gpr, f_fpr, f_ovf, f_sav;
7410   tree gpr, fpr, ovf, sav, t;
7411
7412   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
7413   f_fpr = TREE_CHAIN (f_gpr);
7414   f_ovf = TREE_CHAIN (f_fpr);
7415   f_sav = TREE_CHAIN (f_ovf);
7416
7417   valist = build_va_arg_indirect_ref (valist);
7418   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
7419   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
7420   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
7421   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
7422
7423   /* Count number of gp and fp argument registers used.  */
7424
7425   n_gpr = current_function_args_info.gprs;
7426   n_fpr = current_function_args_info.fprs;
7427
7428   if (cfun->va_list_gpr_size)
7429     {
7430       t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
7431                  build_int_cst (NULL_TREE, n_gpr));
7432       TREE_SIDE_EFFECTS (t) = 1;
7433       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7434     }
7435
7436   if (cfun->va_list_fpr_size)
7437     {
7438       t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
7439                  build_int_cst (NULL_TREE, n_fpr));
7440       TREE_SIDE_EFFECTS (t) = 1;
7441       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7442     }
7443
7444   /* Find the overflow area.  */
7445   if (n_gpr + cfun->va_list_gpr_size > GP_ARG_NUM_REG
7446       || n_fpr + cfun->va_list_fpr_size > FP_ARG_NUM_REG)
7447     {
7448       t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
7449
7450       off = INTVAL (current_function_arg_offset_rtx);
7451       off = off < 0 ? 0 : off;
7452       if (TARGET_DEBUG_ARG)
7453         fprintf (stderr, "va_start: n_gpr = %d, n_fpr = %d off %d\n",
7454                  (int)n_gpr, (int)n_fpr, off);
7455
7456       t = build (PLUS_EXPR, TREE_TYPE (ovf), t, build_int_cst (NULL_TREE, off));
7457
7458       t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
7459       TREE_SIDE_EFFECTS (t) = 1;
7460       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7461     }
7462
7463   /* Find the register save area.  */
7464   if ((cfun->va_list_gpr_size && n_gpr < GP_ARG_NUM_REG)
7465       || (cfun->va_list_fpr_size && n_fpr < FP_ARG_NUM_REG))
7466     {
7467       t = make_tree (TREE_TYPE (sav), return_address_pointer_rtx);
7468       t = build (PLUS_EXPR, TREE_TYPE (sav), t,
7469                  build_int_cst (NULL_TREE, -RETURN_REGNUM * UNITS_PER_WORD));
7470   
7471       t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
7472       TREE_SIDE_EFFECTS (t) = 1;
7473       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7474     }
7475 }
7476
7477 /* Implement va_arg by updating the va_list structure
7478    VALIST as required to retrieve an argument of type
7479    TYPE, and returning that argument.
7480
7481    Generates code equivalent to:
7482
7483    if (integral value) {
7484      if (size  <= 4 && args.gpr < 5 ||
7485          size  > 4 && args.gpr < 4 )
7486        ret = args.reg_save_area[args.gpr+8]
7487      else
7488        ret = *args.overflow_arg_area++;
7489    } else if (float value) {
7490      if (args.fgpr < 2)
7491        ret = args.reg_save_area[args.fpr+64]
7492      else
7493        ret = *args.overflow_arg_area++;
7494    } else if (aggregate value) {
7495      if (args.gpr < 5)
7496        ret = *args.reg_save_area[args.gpr]
7497      else
7498        ret = **args.overflow_arg_area++;
7499    } */
7500
7501 static tree
7502 s390_gimplify_va_arg (tree valist, tree type, tree *pre_p, 
7503                       tree *post_p ATTRIBUTE_UNUSED)
7504 {
7505   tree f_gpr, f_fpr, f_ovf, f_sav;
7506   tree gpr, fpr, ovf, sav, reg, t, u;
7507   int indirect_p, size, n_reg, sav_ofs, sav_scale, max_reg;
7508   tree lab_false, lab_over, addr;
7509
7510   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
7511   f_fpr = TREE_CHAIN (f_gpr);
7512   f_ovf = TREE_CHAIN (f_fpr);
7513   f_sav = TREE_CHAIN (f_ovf);
7514
7515   valist = build_va_arg_indirect_ref (valist);
7516   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
7517   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
7518   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
7519   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
7520
7521   size = int_size_in_bytes (type);
7522
7523   if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
7524     {
7525       if (TARGET_DEBUG_ARG)
7526         {
7527           fprintf (stderr, "va_arg: aggregate type");
7528           debug_tree (type);
7529         }
7530
7531       /* Aggregates are passed by reference.  */
7532       indirect_p = 1;
7533       reg = gpr;
7534       n_reg = 1;
7535
7536       /* kernel stack layout on 31 bit: It is assumed here that no padding
7537          will be added by s390_frame_info because for va_args always an even
7538          number of gprs has to be saved r15-r2 = 14 regs.  */
7539       sav_ofs = 2 * UNITS_PER_WORD;
7540       sav_scale = UNITS_PER_WORD;
7541       size = UNITS_PER_WORD;
7542       max_reg = GP_ARG_NUM_REG - n_reg;
7543     }
7544   else if (s390_function_arg_float (TYPE_MODE (type), type))
7545     {
7546       if (TARGET_DEBUG_ARG)
7547         {
7548           fprintf (stderr, "va_arg: float type");
7549           debug_tree (type);
7550         }
7551
7552       /* FP args go in FP registers, if present.  */
7553       indirect_p = 0;
7554       reg = fpr;
7555       n_reg = 1;
7556       sav_ofs = 16 * UNITS_PER_WORD;
7557       sav_scale = 8;
7558       max_reg = FP_ARG_NUM_REG - n_reg;
7559     }
7560   else
7561     {
7562       if (TARGET_DEBUG_ARG)
7563         {
7564           fprintf (stderr, "va_arg: other type");
7565           debug_tree (type);
7566         }
7567
7568       /* Otherwise into GP registers.  */
7569       indirect_p = 0;
7570       reg = gpr;
7571       n_reg = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
7572
7573       /* kernel stack layout on 31 bit: It is assumed here that no padding
7574          will be added by s390_frame_info because for va_args always an even
7575          number of gprs has to be saved r15-r2 = 14 regs.  */
7576       sav_ofs = 2 * UNITS_PER_WORD;
7577
7578       if (size < UNITS_PER_WORD)
7579         sav_ofs += UNITS_PER_WORD - size;
7580
7581       sav_scale = UNITS_PER_WORD;
7582       max_reg = GP_ARG_NUM_REG - n_reg;
7583     }
7584
7585   /* Pull the value out of the saved registers ...  */
7586
7587   lab_false = create_artificial_label ();
7588   lab_over = create_artificial_label ();
7589   addr = create_tmp_var (ptr_type_node, "addr");
7590   DECL_POINTER_ALIAS_SET (addr) = get_varargs_alias_set ();
7591
7592   t = fold_convert (TREE_TYPE (reg), size_int (max_reg));
7593   t = build2 (GT_EXPR, boolean_type_node, reg, t);
7594   u = build1 (GOTO_EXPR, void_type_node, lab_false);
7595   t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
7596   gimplify_and_add (t, pre_p);
7597
7598   t = build2 (PLUS_EXPR, ptr_type_node, sav, 
7599               fold_convert (ptr_type_node, size_int (sav_ofs)));
7600   u = build2 (MULT_EXPR, TREE_TYPE (reg), reg, 
7601               fold_convert (TREE_TYPE (reg), size_int (sav_scale)));
7602   t = build2 (PLUS_EXPR, ptr_type_node, t, fold_convert (ptr_type_node, u));
7603
7604   t = build2 (MODIFY_EXPR, void_type_node, addr, t);
7605   gimplify_and_add (t, pre_p);
7606
7607   t = build1 (GOTO_EXPR, void_type_node, lab_over);
7608   gimplify_and_add (t, pre_p);
7609
7610   t = build1 (LABEL_EXPR, void_type_node, lab_false);
7611   append_to_statement_list (t, pre_p);
7612
7613
7614   /* ... Otherwise out of the overflow area.  */
7615
7616   t = ovf;
7617   if (size < UNITS_PER_WORD)
7618     t = build2 (PLUS_EXPR, ptr_type_node, t, 
7619                 fold_convert (ptr_type_node, size_int (UNITS_PER_WORD - size)));
7620
7621   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
7622
7623   u = build2 (MODIFY_EXPR, void_type_node, addr, t);
7624   gimplify_and_add (u, pre_p);
7625
7626   t = build2 (PLUS_EXPR, ptr_type_node, t, 
7627               fold_convert (ptr_type_node, size_int (size)));
7628   t = build2 (MODIFY_EXPR, ptr_type_node, ovf, t);
7629   gimplify_and_add (t, pre_p);
7630
7631   t = build1 (LABEL_EXPR, void_type_node, lab_over);
7632   append_to_statement_list (t, pre_p);
7633
7634
7635   /* Increment register save count.  */
7636
7637   u = build2 (PREINCREMENT_EXPR, TREE_TYPE (reg), reg,
7638               fold_convert (TREE_TYPE (reg), size_int (n_reg)));
7639   gimplify_and_add (u, pre_p);
7640
7641   if (indirect_p)
7642     {
7643       t = build_pointer_type (build_pointer_type (type));
7644       addr = fold_convert (t, addr);
7645       addr = build_va_arg_indirect_ref (addr);
7646     }
7647   else
7648     {
7649       t = build_pointer_type (type);
7650       addr = fold_convert (t, addr);
7651     }
7652
7653   return build_va_arg_indirect_ref (addr);
7654 }
7655
7656
7657 /* Builtins.  */
7658
7659 enum s390_builtin
7660 {
7661   S390_BUILTIN_THREAD_POINTER,
7662   S390_BUILTIN_SET_THREAD_POINTER,
7663
7664   S390_BUILTIN_max
7665 };
7666
7667 static unsigned int const code_for_builtin_64[S390_BUILTIN_max] = {
7668   CODE_FOR_get_tp_64,
7669   CODE_FOR_set_tp_64
7670 };
7671
7672 static unsigned int const code_for_builtin_31[S390_BUILTIN_max] = {
7673   CODE_FOR_get_tp_31,
7674   CODE_FOR_set_tp_31
7675 };
7676
7677 static void
7678 s390_init_builtins (void)
7679 {
7680   tree ftype;
7681
7682   ftype = build_function_type (ptr_type_node, void_list_node);
7683   lang_hooks.builtin_function ("__builtin_thread_pointer", ftype,
7684                                S390_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
7685                                NULL, NULL_TREE);
7686
7687   ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
7688   lang_hooks.builtin_function ("__builtin_set_thread_pointer", ftype,
7689                                S390_BUILTIN_SET_THREAD_POINTER, BUILT_IN_MD,
7690                                NULL, NULL_TREE);
7691 }
7692
7693 /* Expand an expression EXP that calls a built-in function,
7694    with result going to TARGET if that's convenient
7695    (and in mode MODE if that's convenient).
7696    SUBTARGET may be used as the target for computing one of EXP's operands.
7697    IGNORE is nonzero if the value is to be ignored.  */
7698
7699 static rtx
7700 s390_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
7701                      enum machine_mode mode ATTRIBUTE_UNUSED,
7702                      int ignore ATTRIBUTE_UNUSED)
7703 {
7704 #define MAX_ARGS 2
7705
7706   unsigned int const *code_for_builtin =
7707     TARGET_64BIT ? code_for_builtin_64 : code_for_builtin_31;
7708
7709   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7710   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
7711   tree arglist = TREE_OPERAND (exp, 1);
7712   enum insn_code icode;
7713   rtx op[MAX_ARGS], pat;
7714   int arity;
7715   bool nonvoid;
7716
7717   if (fcode >= S390_BUILTIN_max)
7718     internal_error ("bad builtin fcode");
7719   icode = code_for_builtin[fcode];
7720   if (icode == 0)
7721     internal_error ("bad builtin fcode");
7722
7723   nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
7724
7725   for (arglist = TREE_OPERAND (exp, 1), arity = 0;
7726        arglist;
7727        arglist = TREE_CHAIN (arglist), arity++)
7728     {
7729       const struct insn_operand_data *insn_op;
7730
7731       tree arg = TREE_VALUE (arglist);
7732       if (arg == error_mark_node)
7733         return NULL_RTX;
7734       if (arity > MAX_ARGS)
7735         return NULL_RTX;
7736
7737       insn_op = &insn_data[icode].operand[arity + nonvoid];
7738
7739       op[arity] = expand_expr (arg, NULL_RTX, insn_op->mode, 0);
7740
7741       if (!(*insn_op->predicate) (op[arity], insn_op->mode))
7742         op[arity] = copy_to_mode_reg (insn_op->mode, op[arity]);
7743     }
7744
7745   if (nonvoid)
7746     {
7747       enum machine_mode tmode = insn_data[icode].operand[0].mode;
7748       if (!target
7749           || GET_MODE (target) != tmode
7750           || !(*insn_data[icode].operand[0].predicate) (target, tmode))
7751         target = gen_reg_rtx (tmode);
7752     }
7753
7754   switch (arity)
7755     {
7756     case 0:
7757       pat = GEN_FCN (icode) (target);
7758       break;
7759     case 1:
7760       if (nonvoid)
7761         pat = GEN_FCN (icode) (target, op[0]);
7762       else
7763         pat = GEN_FCN (icode) (op[0]);
7764       break;
7765     case 2:
7766       pat = GEN_FCN (icode) (target, op[0], op[1]);
7767       break;
7768     default:
7769       gcc_unreachable ();
7770     }
7771   if (!pat)
7772     return NULL_RTX;
7773   emit_insn (pat);
7774
7775   if (nonvoid)
7776     return target;
7777   else
7778     return const0_rtx;
7779 }
7780
7781
7782 /* Output assembly code for the trampoline template to
7783    stdio stream FILE.
7784
7785    On S/390, we use gpr 1 internally in the trampoline code;
7786    gpr 0 is used to hold the static chain.  */
7787
7788 void
7789 s390_trampoline_template (FILE *file)
7790 {
7791   rtx op[2];
7792   op[0] = gen_rtx_REG (Pmode, 0);
7793   op[1] = gen_rtx_REG (Pmode, 1);
7794
7795   if (TARGET_64BIT)
7796     {
7797       output_asm_insn ("basr\t%1,0", op);
7798       output_asm_insn ("lmg\t%0,%1,14(%1)", op);
7799       output_asm_insn ("br\t%1", op);
7800       ASM_OUTPUT_SKIP (file, (HOST_WIDE_INT)(TRAMPOLINE_SIZE - 10));
7801     }
7802   else
7803     {
7804       output_asm_insn ("basr\t%1,0", op);
7805       output_asm_insn ("lm\t%0,%1,6(%1)", op);
7806       output_asm_insn ("br\t%1", op);
7807       ASM_OUTPUT_SKIP (file, (HOST_WIDE_INT)(TRAMPOLINE_SIZE - 8));
7808     }
7809 }
7810
7811 /* Emit RTL insns to initialize the variable parts of a trampoline.
7812    FNADDR is an RTX for the address of the function's pure code.
7813    CXT is an RTX for the static chain value for the function.  */
7814
7815 void
7816 s390_initialize_trampoline (rtx addr, rtx fnaddr, rtx cxt)
7817 {
7818   emit_move_insn (gen_rtx_MEM (Pmode,
7819                    memory_address (Pmode,
7820                    plus_constant (addr, (TARGET_64BIT ? 16 : 8)))), cxt);
7821   emit_move_insn (gen_rtx_MEM (Pmode,
7822                    memory_address (Pmode,
7823                    plus_constant (addr, (TARGET_64BIT ? 24 : 12)))), fnaddr);
7824 }
7825
7826 /* Return rtx for 64-bit constant formed from the 32-bit subwords
7827    LOW and HIGH, independent of the host word size.  */
7828
7829 rtx
7830 s390_gen_rtx_const_DI (int high, int low)
7831 {
7832 #if HOST_BITS_PER_WIDE_INT >= 64
7833   HOST_WIDE_INT val;
7834   val = (HOST_WIDE_INT)high;
7835   val <<= 32;
7836   val |= (HOST_WIDE_INT)low;
7837
7838   return GEN_INT (val);
7839 #else
7840 #if HOST_BITS_PER_WIDE_INT >= 32
7841   return immed_double_const ((HOST_WIDE_INT)low, (HOST_WIDE_INT)high, DImode);
7842 #else
7843   gcc_unreachable ();
7844 #endif
7845 #endif
7846 }
7847
7848 /* Output assembler code to FILE to increment profiler label # LABELNO
7849    for profiling a function entry.  */
7850
7851 void
7852 s390_function_profiler (FILE *file, int labelno)
7853 {
7854   rtx op[7];
7855
7856   char label[128];
7857   ASM_GENERATE_INTERNAL_LABEL (label, "LP", labelno);
7858
7859   fprintf (file, "# function profiler \n");
7860
7861   op[0] = gen_rtx_REG (Pmode, RETURN_REGNUM);
7862   op[1] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
7863   op[1] = gen_rtx_MEM (Pmode, plus_constant (op[1], UNITS_PER_WORD));
7864
7865   op[2] = gen_rtx_REG (Pmode, 1);
7866   op[3] = gen_rtx_SYMBOL_REF (Pmode, label);
7867   SYMBOL_REF_FLAGS (op[3]) = SYMBOL_FLAG_LOCAL;
7868
7869   op[4] = gen_rtx_SYMBOL_REF (Pmode, "_mcount");
7870   if (flag_pic)
7871     {
7872       op[4] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op[4]), UNSPEC_PLT);
7873       op[4] = gen_rtx_CONST (Pmode, op[4]);
7874     }
7875
7876   if (TARGET_64BIT)
7877     {
7878       output_asm_insn ("stg\t%0,%1", op);
7879       output_asm_insn ("larl\t%2,%3", op);
7880       output_asm_insn ("brasl\t%0,%4", op);
7881       output_asm_insn ("lg\t%0,%1", op);
7882     }
7883   else if (!flag_pic)
7884     {
7885       op[6] = gen_label_rtx ();
7886
7887       output_asm_insn ("st\t%0,%1", op);
7888       output_asm_insn ("bras\t%2,%l6", op);
7889       output_asm_insn (".long\t%4", op);
7890       output_asm_insn (".long\t%3", op);
7891       targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[6]));
7892       output_asm_insn ("l\t%0,0(%2)", op);
7893       output_asm_insn ("l\t%2,4(%2)", op);
7894       output_asm_insn ("basr\t%0,%0", op);
7895       output_asm_insn ("l\t%0,%1", op);
7896     }
7897   else
7898     {
7899       op[5] = gen_label_rtx ();
7900       op[6] = gen_label_rtx ();
7901
7902       output_asm_insn ("st\t%0,%1", op);
7903       output_asm_insn ("bras\t%2,%l6", op);
7904       targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[5]));
7905       output_asm_insn (".long\t%4-%l5", op);
7906       output_asm_insn (".long\t%3-%l5", op);
7907       targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[6]));
7908       output_asm_insn ("lr\t%0,%2", op);
7909       output_asm_insn ("a\t%0,0(%2)", op);
7910       output_asm_insn ("a\t%2,4(%2)", op);
7911       output_asm_insn ("basr\t%0,%0", op);
7912       output_asm_insn ("l\t%0,%1", op);
7913     }
7914 }
7915
7916 /* Encode symbol attributes (local vs. global, tls model) of a SYMBOL_REF
7917    into its SYMBOL_REF_FLAGS.  */
7918
7919 static void
7920 s390_encode_section_info (tree decl, rtx rtl, int first)
7921 {
7922   default_encode_section_info (decl, rtl, first);
7923
7924   /* If a variable has a forced alignment to < 2 bytes, mark it with
7925      SYMBOL_FLAG_ALIGN1 to prevent it from being used as LARL operand.  */
7926   if (TREE_CODE (decl) == VAR_DECL
7927       && DECL_USER_ALIGN (decl) && DECL_ALIGN (decl) < 16)
7928     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_ALIGN1;
7929 }
7930
7931 /* Output thunk to FILE that implements a C++ virtual function call (with
7932    multiple inheritance) to FUNCTION.  The thunk adjusts the this pointer
7933    by DELTA, and unless VCALL_OFFSET is zero, applies an additional adjustment
7934    stored at VCALL_OFFSET in the vtable whose address is located at offset 0
7935    relative to the resulting this pointer.  */
7936
7937 static void
7938 s390_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
7939                       HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
7940                       tree function)
7941 {
7942   rtx op[10];
7943   int nonlocal = 0;
7944
7945   /* Operand 0 is the target function.  */
7946   op[0] = XEXP (DECL_RTL (function), 0);
7947   if (flag_pic && !SYMBOL_REF_LOCAL_P (op[0]))
7948     {
7949       nonlocal = 1;
7950       op[0] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op[0]),
7951                               TARGET_64BIT ? UNSPEC_PLT : UNSPEC_GOT);
7952       op[0] = gen_rtx_CONST (Pmode, op[0]);
7953     }
7954
7955   /* Operand 1 is the 'this' pointer.  */
7956   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
7957     op[1] = gen_rtx_REG (Pmode, 3);
7958   else
7959     op[1] = gen_rtx_REG (Pmode, 2);
7960
7961   /* Operand 2 is the delta.  */
7962   op[2] = GEN_INT (delta);
7963
7964   /* Operand 3 is the vcall_offset.  */
7965   op[3] = GEN_INT (vcall_offset);
7966
7967   /* Operand 4 is the temporary register.  */
7968   op[4] = gen_rtx_REG (Pmode, 1);
7969
7970   /* Operands 5 to 8 can be used as labels.  */
7971   op[5] = NULL_RTX;
7972   op[6] = NULL_RTX;
7973   op[7] = NULL_RTX;
7974   op[8] = NULL_RTX;
7975
7976   /* Operand 9 can be used for temporary register.  */
7977   op[9] = NULL_RTX;
7978
7979   /* Generate code.  */
7980   if (TARGET_64BIT)
7981     {
7982       /* Setup literal pool pointer if required.  */
7983       if ((!DISP_IN_RANGE (delta)
7984            && !CONST_OK_FOR_K (delta)
7985            && !CONST_OK_FOR_Os (delta))
7986           || (!DISP_IN_RANGE (vcall_offset)
7987               && !CONST_OK_FOR_K (vcall_offset)
7988               && !CONST_OK_FOR_Os (vcall_offset)))
7989         {
7990           op[5] = gen_label_rtx ();
7991           output_asm_insn ("larl\t%4,%5", op);
7992         }
7993
7994       /* Add DELTA to this pointer.  */
7995       if (delta)
7996         {
7997           if (CONST_OK_FOR_J (delta))
7998             output_asm_insn ("la\t%1,%2(%1)", op);
7999           else if (DISP_IN_RANGE (delta))
8000             output_asm_insn ("lay\t%1,%2(%1)", op);
8001           else if (CONST_OK_FOR_K (delta))
8002             output_asm_insn ("aghi\t%1,%2", op);
8003           else if (CONST_OK_FOR_Os (delta))
8004             output_asm_insn ("agfi\t%1,%2", op);
8005           else
8006             {
8007               op[6] = gen_label_rtx ();
8008               output_asm_insn ("agf\t%1,%6-%5(%4)", op);
8009             }
8010         }
8011
8012       /* Perform vcall adjustment.  */
8013       if (vcall_offset)
8014         {
8015           if (DISP_IN_RANGE (vcall_offset))
8016             {
8017               output_asm_insn ("lg\t%4,0(%1)", op);
8018               output_asm_insn ("ag\t%1,%3(%4)", op);
8019             }
8020           else if (CONST_OK_FOR_K (vcall_offset))
8021             {
8022               output_asm_insn ("lghi\t%4,%3", op);
8023               output_asm_insn ("ag\t%4,0(%1)", op);
8024               output_asm_insn ("ag\t%1,0(%4)", op);
8025             }
8026           else if (CONST_OK_FOR_Os (vcall_offset))
8027             {
8028               output_asm_insn ("lgfi\t%4,%3", op);
8029               output_asm_insn ("ag\t%4,0(%1)", op);
8030               output_asm_insn ("ag\t%1,0(%4)", op);
8031             }
8032           else
8033             {
8034               op[7] = gen_label_rtx ();
8035               output_asm_insn ("llgf\t%4,%7-%5(%4)", op);
8036               output_asm_insn ("ag\t%4,0(%1)", op);
8037               output_asm_insn ("ag\t%1,0(%4)", op);
8038             }
8039         }
8040
8041       /* Jump to target.  */
8042       output_asm_insn ("jg\t%0", op);
8043
8044       /* Output literal pool if required.  */
8045       if (op[5])
8046         {
8047           output_asm_insn (".align\t4", op);
8048           targetm.asm_out.internal_label (file, "L",
8049                                           CODE_LABEL_NUMBER (op[5]));
8050         }
8051       if (op[6])
8052         {
8053           targetm.asm_out.internal_label (file, "L",
8054                                           CODE_LABEL_NUMBER (op[6]));
8055           output_asm_insn (".long\t%2", op);
8056         }
8057       if (op[7])
8058         {
8059           targetm.asm_out.internal_label (file, "L",
8060                                           CODE_LABEL_NUMBER (op[7]));
8061           output_asm_insn (".long\t%3", op);
8062         }
8063     }
8064   else
8065     {
8066       /* Setup base pointer if required.  */
8067       if (!vcall_offset
8068           || (!DISP_IN_RANGE (delta)
8069               && !CONST_OK_FOR_K (delta)
8070               && !CONST_OK_FOR_Os (delta))
8071           || (!DISP_IN_RANGE (delta)
8072               && !CONST_OK_FOR_K (vcall_offset)
8073               && !CONST_OK_FOR_Os (vcall_offset)))
8074         {
8075           op[5] = gen_label_rtx ();
8076           output_asm_insn ("basr\t%4,0", op);
8077           targetm.asm_out.internal_label (file, "L",
8078                                           CODE_LABEL_NUMBER (op[5]));
8079         }
8080
8081       /* Add DELTA to this pointer.  */
8082       if (delta)
8083         {
8084           if (CONST_OK_FOR_J (delta))
8085             output_asm_insn ("la\t%1,%2(%1)", op);
8086           else if (DISP_IN_RANGE (delta))
8087             output_asm_insn ("lay\t%1,%2(%1)", op);
8088           else if (CONST_OK_FOR_K (delta))
8089             output_asm_insn ("ahi\t%1,%2", op);
8090           else if (CONST_OK_FOR_Os (delta))
8091             output_asm_insn ("afi\t%1,%2", op);
8092           else
8093             {
8094               op[6] = gen_label_rtx ();
8095               output_asm_insn ("a\t%1,%6-%5(%4)", op);
8096             }
8097         }
8098
8099       /* Perform vcall adjustment.  */
8100       if (vcall_offset)
8101         {
8102           if (CONST_OK_FOR_J (vcall_offset))
8103             {
8104               output_asm_insn ("l\t%4,0(%1)", op);
8105               output_asm_insn ("a\t%1,%3(%4)", op);
8106             }
8107           else if (DISP_IN_RANGE (vcall_offset))
8108             {
8109               output_asm_insn ("l\t%4,0(%1)", op);
8110               output_asm_insn ("ay\t%1,%3(%4)", op);
8111             }
8112           else if (CONST_OK_FOR_K (vcall_offset))
8113             {
8114               output_asm_insn ("lhi\t%4,%3", op);
8115               output_asm_insn ("a\t%4,0(%1)", op);
8116               output_asm_insn ("a\t%1,0(%4)", op);
8117             }
8118           else if (CONST_OK_FOR_Os (vcall_offset))
8119             {
8120               output_asm_insn ("iilf\t%4,%3", op);
8121               output_asm_insn ("a\t%4,0(%1)", op);
8122               output_asm_insn ("a\t%1,0(%4)", op);
8123             }
8124           else
8125             {
8126               op[7] = gen_label_rtx ();
8127               output_asm_insn ("l\t%4,%7-%5(%4)", op);
8128               output_asm_insn ("a\t%4,0(%1)", op);
8129               output_asm_insn ("a\t%1,0(%4)", op);
8130             }
8131
8132           /* We had to clobber the base pointer register.
8133              Re-setup the base pointer (with a different base).  */
8134           op[5] = gen_label_rtx ();
8135           output_asm_insn ("basr\t%4,0", op);
8136           targetm.asm_out.internal_label (file, "L",
8137                                           CODE_LABEL_NUMBER (op[5]));
8138         }
8139
8140       /* Jump to target.  */
8141       op[8] = gen_label_rtx ();
8142
8143       if (!flag_pic)
8144         output_asm_insn ("l\t%4,%8-%5(%4)", op);
8145       else if (!nonlocal)
8146         output_asm_insn ("a\t%4,%8-%5(%4)", op);
8147       /* We cannot call through .plt, since .plt requires %r12 loaded.  */
8148       else if (flag_pic == 1)
8149         {
8150           output_asm_insn ("a\t%4,%8-%5(%4)", op);
8151           output_asm_insn ("l\t%4,%0(%4)", op);
8152         }
8153       else if (flag_pic == 2)
8154         {
8155           op[9] = gen_rtx_REG (Pmode, 0);
8156           output_asm_insn ("l\t%9,%8-4-%5(%4)", op);
8157           output_asm_insn ("a\t%4,%8-%5(%4)", op);
8158           output_asm_insn ("ar\t%4,%9", op);
8159           output_asm_insn ("l\t%4,0(%4)", op);
8160         }
8161
8162       output_asm_insn ("br\t%4", op);
8163
8164       /* Output literal pool.  */
8165       output_asm_insn (".align\t4", op);
8166
8167       if (nonlocal && flag_pic == 2)
8168         output_asm_insn (".long\t%0", op);
8169       if (nonlocal)
8170         {
8171           op[0] = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
8172           SYMBOL_REF_FLAGS (op[0]) = SYMBOL_FLAG_LOCAL;
8173         }
8174
8175       targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[8]));
8176       if (!flag_pic)
8177         output_asm_insn (".long\t%0", op);
8178       else
8179         output_asm_insn (".long\t%0-%5", op);
8180
8181       if (op[6])
8182         {
8183           targetm.asm_out.internal_label (file, "L",
8184                                           CODE_LABEL_NUMBER (op[6]));
8185           output_asm_insn (".long\t%2", op);
8186         }
8187       if (op[7])
8188         {
8189           targetm.asm_out.internal_label (file, "L",
8190                                           CODE_LABEL_NUMBER (op[7]));
8191           output_asm_insn (".long\t%3", op);
8192         }
8193     }
8194 }
8195
8196 static bool
8197 s390_valid_pointer_mode (enum machine_mode mode)
8198 {
8199   return (mode == SImode || (TARGET_64BIT && mode == DImode));
8200 }
8201
8202 /* Checks whether the given ARGUMENT_LIST would use a caller
8203    saved register.  This is used to decide whether sibling call
8204    optimization could be performed on the respective function
8205    call.  */
8206
8207 static bool
8208 s390_call_saved_register_used (tree argument_list)
8209 {
8210   CUMULATIVE_ARGS cum;
8211   tree parameter;
8212   enum machine_mode mode;
8213   tree type;
8214   rtx parm_rtx;
8215   int reg;
8216
8217   INIT_CUMULATIVE_ARGS (cum, NULL, NULL, 0, 0);
8218
8219   while (argument_list)
8220     {
8221       parameter = TREE_VALUE (argument_list);
8222       argument_list = TREE_CHAIN (argument_list);
8223
8224       gcc_assert (parameter);
8225
8226       /* For an undeclared variable passed as parameter we will get
8227          an ERROR_MARK node here.  */
8228       if (TREE_CODE (parameter) == ERROR_MARK)
8229         return true;
8230
8231       type = TREE_TYPE (parameter);
8232       gcc_assert (type);
8233
8234       mode = TYPE_MODE (type);
8235       gcc_assert (mode);
8236
8237       if (pass_by_reference (&cum, mode, type, true))
8238         {
8239           mode = Pmode;
8240           type = build_pointer_type (type);
8241         }
8242
8243        parm_rtx = s390_function_arg (&cum, mode, type, 0);
8244
8245        s390_function_arg_advance (&cum, mode, type, 0);
8246
8247        if (parm_rtx && REG_P (parm_rtx))
8248          {
8249            for (reg = 0;
8250                 reg < HARD_REGNO_NREGS (REGNO (parm_rtx), GET_MODE (parm_rtx));
8251                 reg++)
8252              if (! call_used_regs[reg + REGNO (parm_rtx)])
8253                return true;
8254          }
8255     }
8256   return false;
8257 }
8258
8259 /* Return true if the given call expression can be
8260    turned into a sibling call.
8261    DECL holds the declaration of the function to be called whereas
8262    EXP is the call expression itself.  */
8263
8264 static bool
8265 s390_function_ok_for_sibcall (tree decl, tree exp)
8266 {
8267   /* The TPF epilogue uses register 1.  */
8268   if (TARGET_TPF_PROFILING)
8269     return false;
8270
8271   /* The 31 bit PLT code uses register 12 (GOT pointer - caller saved)
8272      which would have to be restored before the sibcall.  */
8273   if (!TARGET_64BIT && flag_pic && decl && TREE_PUBLIC (decl))
8274     return false;
8275
8276   /* Register 6 on s390 is available as an argument register but unfortunately
8277      "caller saved". This makes functions needing this register for arguments
8278      not suitable for sibcalls.  */
8279   if (TREE_OPERAND (exp, 1)
8280       && s390_call_saved_register_used (TREE_OPERAND (exp, 1)))
8281       return false;
8282
8283   return true;
8284 }
8285
8286 /* Return the fixed registers used for condition codes.  */
8287
8288 static bool
8289 s390_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
8290 {
8291   *p1 = CC_REGNUM;
8292   *p2 = INVALID_REGNUM;
8293  
8294   return true;
8295 }
8296
8297 /* This function is used by the call expanders of the machine description.
8298    It emits the call insn itself together with the necessary operations
8299    to adjust the target address and returns the emitted insn.
8300    ADDR_LOCATION is the target address rtx
8301    TLS_CALL the location of the thread-local symbol
8302    RESULT_REG the register where the result of the call should be stored
8303    RETADDR_REG the register where the return address should be stored
8304                If this parameter is NULL_RTX the call is considered
8305                to be a sibling call.  */
8306
8307 rtx
8308 s390_emit_call (rtx addr_location, rtx tls_call, rtx result_reg,
8309                 rtx retaddr_reg)
8310 {
8311   bool plt_call = false;
8312   rtx insn;
8313   rtx call;
8314   rtx clobber;
8315   rtvec vec;
8316
8317   /* Direct function calls need special treatment.  */
8318   if (GET_CODE (addr_location) == SYMBOL_REF)
8319     {
8320       /* When calling a global routine in PIC mode, we must
8321          replace the symbol itself with the PLT stub.  */
8322       if (flag_pic && !SYMBOL_REF_LOCAL_P (addr_location))
8323         {
8324           addr_location = gen_rtx_UNSPEC (Pmode,
8325                                           gen_rtvec (1, addr_location),
8326                                           UNSPEC_PLT);
8327           addr_location = gen_rtx_CONST (Pmode, addr_location);
8328           plt_call = true;
8329         }
8330
8331       /* Unless we can use the bras(l) insn, force the
8332          routine address into a register.  */
8333       if (!TARGET_SMALL_EXEC && !TARGET_CPU_ZARCH)
8334         {
8335           if (flag_pic)
8336             addr_location = legitimize_pic_address (addr_location, 0);
8337           else
8338             addr_location = force_reg (Pmode, addr_location);
8339         }
8340     }
8341
8342   /* If it is already an indirect call or the code above moved the
8343      SYMBOL_REF to somewhere else make sure the address can be found in
8344      register 1.  */
8345   if (retaddr_reg == NULL_RTX
8346       && GET_CODE (addr_location) != SYMBOL_REF
8347       && !plt_call)
8348     {
8349       emit_move_insn (gen_rtx_REG (Pmode, SIBCALL_REGNUM), addr_location);
8350       addr_location = gen_rtx_REG (Pmode, SIBCALL_REGNUM);
8351     }
8352
8353   addr_location = gen_rtx_MEM (QImode, addr_location);
8354   call = gen_rtx_CALL (VOIDmode, addr_location, const0_rtx);
8355
8356   if (result_reg != NULL_RTX)
8357     call = gen_rtx_SET (VOIDmode, result_reg, call);
8358
8359   if (retaddr_reg != NULL_RTX)
8360     {
8361       clobber = gen_rtx_CLOBBER (VOIDmode, retaddr_reg);
8362
8363       if (tls_call != NULL_RTX)
8364         vec = gen_rtvec (3, call, clobber,
8365                          gen_rtx_USE (VOIDmode, tls_call));
8366       else
8367         vec = gen_rtvec (2, call, clobber);
8368
8369       call = gen_rtx_PARALLEL (VOIDmode, vec);
8370     }
8371
8372   insn = emit_call_insn (call);
8373
8374   /* 31-bit PLT stubs and tls calls use the GOT register implicitly.  */
8375   if ((!TARGET_64BIT && plt_call) || tls_call != NULL_RTX)
8376     {
8377       /* s390_function_ok_for_sibcall should
8378          have denied sibcalls in this case.  */
8379       gcc_assert (retaddr_reg != NULL_RTX);
8380
8381       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
8382     }
8383   return insn;
8384 }
8385
8386 /* Implement CONDITIONAL_REGISTER_USAGE.  */
8387
8388 void
8389 s390_conditional_register_usage (void)
8390 {
8391   int i;
8392
8393   if (flag_pic)
8394     {
8395       fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
8396       call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
8397     }
8398   if (TARGET_CPU_ZARCH)
8399     {
8400       fixed_regs[BASE_REGNUM] = 0;
8401       call_used_regs[BASE_REGNUM] = 0;
8402       fixed_regs[RETURN_REGNUM] = 0;
8403       call_used_regs[RETURN_REGNUM] = 0;
8404     }
8405   if (TARGET_64BIT)
8406     {
8407       for (i = 24; i < 32; i++)
8408         call_used_regs[i] = call_really_used_regs[i] = 0;
8409     }
8410   else
8411     {
8412       for (i = 18; i < 20; i++)
8413         call_used_regs[i] = call_really_used_regs[i] = 0;
8414     }
8415
8416   if (TARGET_SOFT_FLOAT)
8417     {
8418       for (i = 16; i < 32; i++)
8419         call_used_regs[i] = fixed_regs[i] = 1;
8420     }
8421 }
8422
8423 /* Corresponding function to eh_return expander.  */
8424
8425 static GTY(()) rtx s390_tpf_eh_return_symbol;
8426 void
8427 s390_emit_tpf_eh_return (rtx target)
8428 {
8429   rtx insn, reg;
8430
8431   if (!s390_tpf_eh_return_symbol)
8432     s390_tpf_eh_return_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tpf_eh_return");
8433
8434   reg = gen_rtx_REG (Pmode, 2);
8435
8436   emit_move_insn (reg, target);
8437   insn = s390_emit_call (s390_tpf_eh_return_symbol, NULL_RTX, reg,
8438                                      gen_rtx_REG (Pmode, RETURN_REGNUM));
8439   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
8440
8441   emit_move_insn (EH_RETURN_HANDLER_RTX, reg);
8442 }
8443
8444 /* Rework the prologue/epilogue to avoid saving/restoring
8445    registers unnecessarily.  */
8446
8447 static void
8448 s390_optimize_prologue (void)
8449 {
8450   rtx insn, new_insn, next_insn;
8451
8452   /* Do a final recompute of the frame-related data.  */
8453
8454   s390_update_frame_layout ();
8455
8456   /* If all special registers are in fact used, there's nothing we
8457      can do, so no point in walking the insn list.  */
8458
8459   if (cfun_frame_layout.first_save_gpr <= BASE_REGNUM 
8460       && cfun_frame_layout.last_save_gpr >= BASE_REGNUM
8461       && (TARGET_CPU_ZARCH 
8462           || (cfun_frame_layout.first_save_gpr <= RETURN_REGNUM 
8463               && cfun_frame_layout.last_save_gpr >= RETURN_REGNUM)))
8464     return;
8465
8466   /* Search for prologue/epilogue insns and replace them.  */
8467
8468   for (insn = get_insns (); insn; insn = next_insn)
8469     {
8470       int first, last, off;
8471       rtx set, base, offset;
8472
8473       next_insn = NEXT_INSN (insn);
8474
8475       if (GET_CODE (insn) != INSN)
8476         continue;
8477
8478       if (GET_CODE (PATTERN (insn)) == PARALLEL
8479           && store_multiple_operation (PATTERN (insn), VOIDmode))
8480         {
8481           set = XVECEXP (PATTERN (insn), 0, 0);
8482           first = REGNO (SET_SRC (set));
8483           last = first + XVECLEN (PATTERN (insn), 0) - 1;
8484           offset = const0_rtx;
8485           base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
8486           off = INTVAL (offset);
8487
8488           if (GET_CODE (base) != REG || off < 0)
8489             continue;
8490           if (cfun_frame_layout.first_save_gpr != -1
8491               && (cfun_frame_layout.first_save_gpr < first
8492                   || cfun_frame_layout.last_save_gpr > last))
8493             continue;
8494           if (REGNO (base) != STACK_POINTER_REGNUM
8495               && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
8496             continue;
8497           if (first > BASE_REGNUM || last < BASE_REGNUM)
8498             continue;
8499
8500           if (cfun_frame_layout.first_save_gpr != -1)
8501             {
8502               new_insn  = save_gprs (base, 
8503                                      off + (cfun_frame_layout.first_save_gpr
8504                                             - first) * UNITS_PER_WORD, 
8505                                      cfun_frame_layout.first_save_gpr,
8506                                      cfun_frame_layout.last_save_gpr);
8507               new_insn = emit_insn_before (new_insn, insn);
8508               INSN_ADDRESSES_NEW (new_insn, -1);
8509             }
8510
8511           remove_insn (insn);
8512           continue;
8513         }
8514
8515       if (cfun_frame_layout.first_save_gpr == -1
8516           && GET_CODE (PATTERN (insn)) == SET
8517           && GET_CODE (SET_SRC (PATTERN (insn))) == REG
8518           && (REGNO (SET_SRC (PATTERN (insn))) == BASE_REGNUM
8519               || (!TARGET_CPU_ZARCH
8520                   && REGNO (SET_SRC (PATTERN (insn))) == RETURN_REGNUM))
8521           && GET_CODE (SET_DEST (PATTERN (insn))) == MEM)
8522         {
8523           set = PATTERN (insn);
8524           first = REGNO (SET_SRC (set));
8525           offset = const0_rtx;
8526           base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
8527           off = INTVAL (offset);
8528
8529           if (GET_CODE (base) != REG || off < 0)
8530             continue;
8531           if (REGNO (base) != STACK_POINTER_REGNUM
8532               && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
8533             continue;
8534
8535           remove_insn (insn);
8536           continue;
8537         }
8538
8539       if (GET_CODE (PATTERN (insn)) == PARALLEL
8540           && load_multiple_operation (PATTERN (insn), VOIDmode))
8541         {
8542           set = XVECEXP (PATTERN (insn), 0, 0);
8543           first = REGNO (SET_DEST (set));
8544           last = first + XVECLEN (PATTERN (insn), 0) - 1;
8545           offset = const0_rtx;
8546           base = eliminate_constant_term (XEXP (SET_SRC (set), 0), &offset);
8547           off = INTVAL (offset);
8548
8549           if (GET_CODE (base) != REG || off < 0)
8550             continue;
8551           if (cfun_frame_layout.first_restore_gpr != -1
8552               && (cfun_frame_layout.first_restore_gpr < first
8553                   || cfun_frame_layout.last_restore_gpr > last))
8554             continue;
8555           if (REGNO (base) != STACK_POINTER_REGNUM
8556               && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
8557             continue;
8558           if (first > BASE_REGNUM || last < BASE_REGNUM)
8559             continue;
8560
8561           if (cfun_frame_layout.first_restore_gpr != -1)
8562             {
8563               new_insn = restore_gprs (base, 
8564                                        off + (cfun_frame_layout.first_restore_gpr
8565                                               - first) * UNITS_PER_WORD, 
8566                                        cfun_frame_layout.first_restore_gpr,
8567                                        cfun_frame_layout.last_restore_gpr);
8568               new_insn = emit_insn_before (new_insn, insn);
8569               INSN_ADDRESSES_NEW (new_insn, -1);
8570             }
8571
8572           remove_insn (insn);
8573           continue;
8574         }
8575
8576       if (cfun_frame_layout.first_restore_gpr == -1
8577           && GET_CODE (PATTERN (insn)) == SET
8578           && GET_CODE (SET_DEST (PATTERN (insn))) == REG
8579           && (REGNO (SET_DEST (PATTERN (insn))) == BASE_REGNUM
8580               || (!TARGET_CPU_ZARCH
8581                   && REGNO (SET_DEST (PATTERN (insn))) == RETURN_REGNUM))
8582           && GET_CODE (SET_SRC (PATTERN (insn))) == MEM)
8583         {
8584           set = PATTERN (insn);
8585           first = REGNO (SET_DEST (set));
8586           offset = const0_rtx;
8587           base = eliminate_constant_term (XEXP (SET_SRC (set), 0), &offset);
8588           off = INTVAL (offset);
8589
8590           if (GET_CODE (base) != REG || off < 0)
8591             continue;
8592           if (REGNO (base) != STACK_POINTER_REGNUM
8593               && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
8594             continue;
8595
8596           remove_insn (insn);
8597           continue;
8598         }
8599     }
8600 }
8601
8602 /* Perform machine-dependent processing.  */
8603
8604 static void
8605 s390_reorg (void)
8606 {
8607   bool pool_overflow = false;
8608
8609   /* Make sure all splits have been performed; splits after
8610      machine_dependent_reorg might confuse insn length counts.  */
8611   split_all_insns_noflow ();
8612
8613
8614   /* Install the main literal pool and the associated base
8615      register load insns.
8616
8617      In addition, there are two problematic situations we need
8618      to correct:
8619
8620      - the literal pool might be > 4096 bytes in size, so that
8621        some of its elements cannot be directly accessed
8622
8623      - a branch target might be > 64K away from the branch, so that
8624        it is not possible to use a PC-relative instruction.
8625
8626      To fix those, we split the single literal pool into multiple
8627      pool chunks, reloading the pool base register at various
8628      points throughout the function to ensure it always points to
8629      the pool chunk the following code expects, and / or replace
8630      PC-relative branches by absolute branches.
8631
8632      However, the two problems are interdependent: splitting the
8633      literal pool can move a branch further away from its target,
8634      causing the 64K limit to overflow, and on the other hand,
8635      replacing a PC-relative branch by an absolute branch means
8636      we need to put the branch target address into the literal
8637      pool, possibly causing it to overflow.
8638
8639      So, we loop trying to fix up both problems until we manage
8640      to satisfy both conditions at the same time.  Note that the
8641      loop is guaranteed to terminate as every pass of the loop
8642      strictly decreases the total number of PC-relative branches
8643      in the function.  (This is not completely true as there
8644      might be branch-over-pool insns introduced by chunkify_start.
8645      Those never need to be split however.)  */
8646
8647   for (;;)
8648     {
8649       struct constant_pool *pool = NULL;
8650
8651       /* Collect the literal pool.  */
8652       if (!pool_overflow)
8653         {
8654           pool = s390_mainpool_start ();
8655           if (!pool)
8656             pool_overflow = true;
8657         }
8658
8659       /* If literal pool overflowed, start to chunkify it.  */
8660       if (pool_overflow)
8661         pool = s390_chunkify_start ();
8662
8663       /* Split out-of-range branches.  If this has created new
8664          literal pool entries, cancel current chunk list and
8665          recompute it.  zSeries machines have large branch
8666          instructions, so we never need to split a branch.  */
8667       if (!TARGET_CPU_ZARCH && s390_split_branches ())
8668         {
8669           if (pool_overflow)
8670             s390_chunkify_cancel (pool);
8671           else
8672             s390_mainpool_cancel (pool);
8673
8674           continue;
8675         }
8676
8677       /* If we made it up to here, both conditions are satisfied.
8678          Finish up literal pool related changes.  */
8679       if (pool_overflow)
8680         s390_chunkify_finish (pool);
8681       else
8682         s390_mainpool_finish (pool);
8683
8684       /* We're done splitting branches.  */
8685       cfun->machine->split_branches_pending_p = false;
8686       break;
8687     }
8688
8689   /* Generate out-of-pool execute target insns.  */
8690   if (TARGET_CPU_ZARCH)
8691     {
8692       rtx insn, label, target;
8693
8694       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
8695         {
8696           label = s390_execute_label (insn);
8697           if (!label)
8698             continue;
8699
8700           gcc_assert (label != const0_rtx);
8701
8702           target = emit_label (XEXP (label, 0));
8703           INSN_ADDRESSES_NEW (target, -1);
8704
8705           target = emit_insn (s390_execute_target (insn));
8706           INSN_ADDRESSES_NEW (target, -1);
8707         }
8708     }
8709
8710   /* Try to optimize prologue and epilogue further.  */
8711   s390_optimize_prologue ();
8712 }
8713
8714
8715 /* Initialize GCC target structure.  */
8716
8717 #undef  TARGET_ASM_ALIGNED_HI_OP
8718 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
8719 #undef  TARGET_ASM_ALIGNED_DI_OP
8720 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
8721 #undef  TARGET_ASM_INTEGER
8722 #define TARGET_ASM_INTEGER s390_assemble_integer
8723
8724 #undef  TARGET_ASM_OPEN_PAREN
8725 #define TARGET_ASM_OPEN_PAREN ""
8726
8727 #undef  TARGET_ASM_CLOSE_PAREN
8728 #define TARGET_ASM_CLOSE_PAREN ""
8729
8730 #undef TARGET_DEFAULT_TARGET_FLAGS
8731 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_FUSED_MADD)
8732 #undef TARGET_HANDLE_OPTION
8733 #define TARGET_HANDLE_OPTION s390_handle_option
8734
8735 #undef  TARGET_ENCODE_SECTION_INFO
8736 #define TARGET_ENCODE_SECTION_INFO s390_encode_section_info
8737
8738 #ifdef HAVE_AS_TLS
8739 #undef TARGET_HAVE_TLS
8740 #define TARGET_HAVE_TLS true
8741 #endif
8742 #undef TARGET_CANNOT_FORCE_CONST_MEM
8743 #define TARGET_CANNOT_FORCE_CONST_MEM s390_cannot_force_const_mem
8744
8745 #undef TARGET_DELEGITIMIZE_ADDRESS
8746 #define TARGET_DELEGITIMIZE_ADDRESS s390_delegitimize_address
8747
8748 #undef TARGET_RETURN_IN_MEMORY
8749 #define TARGET_RETURN_IN_MEMORY s390_return_in_memory
8750
8751 #undef  TARGET_INIT_BUILTINS
8752 #define TARGET_INIT_BUILTINS s390_init_builtins
8753 #undef  TARGET_EXPAND_BUILTIN
8754 #define TARGET_EXPAND_BUILTIN s390_expand_builtin
8755
8756 #undef TARGET_ASM_OUTPUT_MI_THUNK
8757 #define TARGET_ASM_OUTPUT_MI_THUNK s390_output_mi_thunk
8758 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
8759 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
8760
8761 #undef  TARGET_SCHED_ADJUST_PRIORITY
8762 #define TARGET_SCHED_ADJUST_PRIORITY s390_adjust_priority
8763 #undef TARGET_SCHED_ISSUE_RATE
8764 #define TARGET_SCHED_ISSUE_RATE s390_issue_rate
8765 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
8766 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD s390_first_cycle_multipass_dfa_lookahead
8767
8768 #undef TARGET_CANNOT_COPY_INSN_P
8769 #define TARGET_CANNOT_COPY_INSN_P s390_cannot_copy_insn_p
8770 #undef TARGET_RTX_COSTS
8771 #define TARGET_RTX_COSTS s390_rtx_costs
8772 #undef TARGET_ADDRESS_COST
8773 #define TARGET_ADDRESS_COST s390_address_cost
8774
8775 #undef TARGET_MACHINE_DEPENDENT_REORG
8776 #define TARGET_MACHINE_DEPENDENT_REORG s390_reorg
8777
8778 #undef TARGET_VALID_POINTER_MODE
8779 #define TARGET_VALID_POINTER_MODE s390_valid_pointer_mode
8780
8781 #undef TARGET_BUILD_BUILTIN_VA_LIST
8782 #define TARGET_BUILD_BUILTIN_VA_LIST s390_build_builtin_va_list
8783 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
8784 #define TARGET_GIMPLIFY_VA_ARG_EXPR s390_gimplify_va_arg
8785
8786 #undef TARGET_PROMOTE_FUNCTION_ARGS
8787 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
8788 #undef TARGET_PROMOTE_FUNCTION_RETURN
8789 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
8790 #undef TARGET_PASS_BY_REFERENCE
8791 #define TARGET_PASS_BY_REFERENCE s390_pass_by_reference
8792
8793 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
8794 #define TARGET_FUNCTION_OK_FOR_SIBCALL s390_function_ok_for_sibcall
8795
8796 #undef TARGET_FIXED_CONDITION_CODE_REGS
8797 #define TARGET_FIXED_CONDITION_CODE_REGS s390_fixed_condition_code_regs
8798
8799 #undef TARGET_CC_MODES_COMPATIBLE
8800 #define TARGET_CC_MODES_COMPATIBLE s390_cc_modes_compatible
8801
8802 #undef TARGET_INVALID_WITHIN_DOLOOP
8803 #define TARGET_INVALID_WITHIN_DOLOOP hook_constcharptr_rtx_null
8804
8805 #ifdef HAVE_AS_TLS
8806 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
8807 #define TARGET_ASM_OUTPUT_DWARF_DTPREL s390_output_dwarf_dtprel
8808 #endif
8809
8810 struct gcc_target targetm = TARGET_INITIALIZER;
8811
8812 #include "gt-s390.h"