OSDN Git Service

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