OSDN Git Service

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