OSDN Git Service

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