OSDN Git Service

2010-04-08 Wolfgang Gellerich <gellerich@de.ibm.com>
[pf3gnuchains/gcc-fork.git] / gcc / config / s390 / s390.c
1 /* Subroutines used for code generation on IBM S/390 and zSeries
2    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3    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 "real.h"
34 #include "insn-config.h"
35 #include "conditions.h"
36 #include "output.h"
37 #include "insn-attr.h"
38 #include "flags.h"
39 #include "except.h"
40 #include "function.h"
41 #include "recog.h"
42 #include "expr.h"
43 #include "reload.h"
44 #include "toplev.h"
45 #include "basic-block.h"
46 #include "integrate.h"
47 #include "ggc.h"
48 #include "target.h"
49 #include "target-def.h"
50 #include "debug.h"
51 #include "langhooks.h"
52 #include "optabs.h"
53 #include "gimple.h"
54 #include "df.h"
55 #include "params.h"
56
57
58 /* Define the specific costs for a given cpu.  */
59
60 struct processor_costs
61 {
62   /* multiplication */
63   const int m;        /* cost of an M instruction.  */
64   const int mghi;     /* cost of an MGHI instruction.  */
65   const int mh;       /* cost of an MH instruction.  */
66   const int mhi;      /* cost of an MHI instruction.  */
67   const int ml;       /* cost of an ML instruction.  */
68   const int mr;       /* cost of an MR instruction.  */
69   const int ms;       /* cost of an MS instruction.  */
70   const int msg;      /* cost of an MSG instruction.  */
71   const int msgf;     /* cost of an MSGF instruction.  */
72   const int msgfr;    /* cost of an MSGFR instruction.  */
73   const int msgr;     /* cost of an MSGR instruction.  */
74   const int msr;      /* cost of an MSR instruction.  */
75   const int mult_df;  /* cost of multiplication in DFmode.  */
76   const int mxbr;
77   /* square root */
78   const int sqxbr;    /* cost of square root in TFmode.  */
79   const int sqdbr;    /* cost of square root in DFmode.  */
80   const int sqebr;    /* cost of square root in SFmode.  */
81   /* multiply and add */
82   const int madbr;    /* cost of multiply and add in DFmode.  */
83   const int maebr;    /* cost of multiply and add in SFmode.  */
84   /* division */
85   const int dxbr;
86   const int ddbr;
87   const int debr;
88   const int dlgr;
89   const int dlr;
90   const int dr;
91   const int dsgfr;
92   const int dsgr;
93 };
94
95 const struct processor_costs *s390_cost;
96
97 static const
98 struct processor_costs z900_cost =
99 {
100   COSTS_N_INSNS (5),     /* M     */
101   COSTS_N_INSNS (10),    /* MGHI  */
102   COSTS_N_INSNS (5),     /* MH    */
103   COSTS_N_INSNS (4),     /* MHI   */
104   COSTS_N_INSNS (5),     /* ML    */
105   COSTS_N_INSNS (5),     /* MR    */
106   COSTS_N_INSNS (4),     /* MS    */
107   COSTS_N_INSNS (15),    /* MSG   */
108   COSTS_N_INSNS (7),     /* MSGF  */
109   COSTS_N_INSNS (7),     /* MSGFR */
110   COSTS_N_INSNS (10),    /* MSGR  */
111   COSTS_N_INSNS (4),     /* MSR   */
112   COSTS_N_INSNS (7),     /* multiplication in DFmode */
113   COSTS_N_INSNS (13),    /* MXBR */
114   COSTS_N_INSNS (136),   /* SQXBR */
115   COSTS_N_INSNS (44),    /* SQDBR */
116   COSTS_N_INSNS (35),    /* SQEBR */
117   COSTS_N_INSNS (18),    /* MADBR */
118   COSTS_N_INSNS (13),    /* MAEBR */
119   COSTS_N_INSNS (134),   /* DXBR */
120   COSTS_N_INSNS (30),    /* DDBR */
121   COSTS_N_INSNS (27),    /* DEBR */
122   COSTS_N_INSNS (220),   /* DLGR */
123   COSTS_N_INSNS (34),    /* DLR */
124   COSTS_N_INSNS (34),    /* DR */
125   COSTS_N_INSNS (32),    /* DSGFR */
126   COSTS_N_INSNS (32),    /* DSGR */
127 };
128
129 static const
130 struct processor_costs z990_cost =
131 {
132   COSTS_N_INSNS (4),     /* M     */
133   COSTS_N_INSNS (2),     /* MGHI  */
134   COSTS_N_INSNS (2),     /* MH    */
135   COSTS_N_INSNS (2),     /* MHI   */
136   COSTS_N_INSNS (4),     /* ML    */
137   COSTS_N_INSNS (4),     /* MR    */
138   COSTS_N_INSNS (5),     /* MS    */
139   COSTS_N_INSNS (6),     /* MSG   */
140   COSTS_N_INSNS (4),     /* MSGF  */
141   COSTS_N_INSNS (4),     /* MSGFR */
142   COSTS_N_INSNS (4),     /* MSGR  */
143   COSTS_N_INSNS (4),     /* MSR   */
144   COSTS_N_INSNS (1),     /* multiplication in DFmode */
145   COSTS_N_INSNS (28),    /* MXBR */
146   COSTS_N_INSNS (130),   /* SQXBR */
147   COSTS_N_INSNS (66),    /* SQDBR */
148   COSTS_N_INSNS (38),    /* SQEBR */
149   COSTS_N_INSNS (1),     /* MADBR */
150   COSTS_N_INSNS (1),     /* MAEBR */
151   COSTS_N_INSNS (60),    /* DXBR */
152   COSTS_N_INSNS (40),    /* DDBR */
153   COSTS_N_INSNS (26),    /* DEBR */
154   COSTS_N_INSNS (176),   /* DLGR */
155   COSTS_N_INSNS (31),    /* DLR */
156   COSTS_N_INSNS (31),    /* DR */
157   COSTS_N_INSNS (31),    /* DSGFR */
158   COSTS_N_INSNS (31),    /* DSGR */
159 };
160
161 static const
162 struct processor_costs z9_109_cost =
163 {
164   COSTS_N_INSNS (4),     /* M     */
165   COSTS_N_INSNS (2),     /* MGHI  */
166   COSTS_N_INSNS (2),     /* MH    */
167   COSTS_N_INSNS (2),     /* MHI   */
168   COSTS_N_INSNS (4),     /* ML    */
169   COSTS_N_INSNS (4),     /* MR    */
170   COSTS_N_INSNS (5),     /* MS    */
171   COSTS_N_INSNS (6),     /* MSG   */
172   COSTS_N_INSNS (4),     /* MSGF  */
173   COSTS_N_INSNS (4),     /* MSGFR */
174   COSTS_N_INSNS (4),     /* MSGR  */
175   COSTS_N_INSNS (4),     /* MSR   */
176   COSTS_N_INSNS (1),     /* multiplication in DFmode */
177   COSTS_N_INSNS (28),    /* MXBR */
178   COSTS_N_INSNS (130),   /* SQXBR */
179   COSTS_N_INSNS (66),    /* SQDBR */
180   COSTS_N_INSNS (38),    /* SQEBR */
181   COSTS_N_INSNS (1),     /* MADBR */
182   COSTS_N_INSNS (1),     /* MAEBR */
183   COSTS_N_INSNS (60),    /* DXBR */
184   COSTS_N_INSNS (40),    /* DDBR */
185   COSTS_N_INSNS (26),    /* DEBR */
186   COSTS_N_INSNS (30),    /* DLGR */
187   COSTS_N_INSNS (23),    /* DLR */
188   COSTS_N_INSNS (23),    /* DR */
189   COSTS_N_INSNS (24),    /* DSGFR */
190   COSTS_N_INSNS (24),    /* DSGR */
191 };
192
193 static const
194 struct processor_costs z10_cost =
195 {
196   COSTS_N_INSNS (10),    /* M     */
197   COSTS_N_INSNS (10),    /* MGHI  */
198   COSTS_N_INSNS (10),    /* MH    */
199   COSTS_N_INSNS (10),    /* MHI   */
200   COSTS_N_INSNS (10),    /* ML    */
201   COSTS_N_INSNS (10),    /* MR    */
202   COSTS_N_INSNS (10),    /* MS    */
203   COSTS_N_INSNS (10),    /* MSG   */
204   COSTS_N_INSNS (10),    /* MSGF  */
205   COSTS_N_INSNS (10),    /* MSGFR */
206   COSTS_N_INSNS (10),    /* MSGR  */
207   COSTS_N_INSNS (10),    /* MSR   */
208   COSTS_N_INSNS (1) ,    /* multiplication in DFmode */
209   COSTS_N_INSNS (50),    /* MXBR */
210   COSTS_N_INSNS (120),   /* SQXBR */
211   COSTS_N_INSNS (52),    /* SQDBR */
212   COSTS_N_INSNS (38),    /* SQEBR */
213   COSTS_N_INSNS (1),     /* MADBR */
214   COSTS_N_INSNS (1),     /* MAEBR */
215   COSTS_N_INSNS (111),   /* DXBR */
216   COSTS_N_INSNS (39),    /* DDBR */
217   COSTS_N_INSNS (32),    /* DEBR */
218   COSTS_N_INSNS (160),   /* DLGR */
219   COSTS_N_INSNS (71),    /* DLR */
220   COSTS_N_INSNS (71),    /* DR */
221   COSTS_N_INSNS (71),    /* DSGFR */
222   COSTS_N_INSNS (71),    /* DSGR */
223 };
224
225 extern int reload_completed;
226
227 /* Kept up to date using the SCHED_VARIABLE_ISSUE hook.  */
228 static rtx last_scheduled_insn;
229
230 /* Structure used to hold the components of a S/390 memory
231    address.  A legitimate address on S/390 is of the general
232    form
233           base + index + displacement
234    where any of the components is optional.
235
236    base and index are registers of the class ADDR_REGS,
237    displacement is an unsigned 12-bit immediate constant.  */
238
239 struct s390_address
240 {
241   rtx base;
242   rtx indx;
243   rtx disp;
244   bool pointer;
245   bool literal_pool;
246 };
247
248 /* Which cpu are we tuning for.  */
249 enum processor_type s390_tune = PROCESSOR_max;
250 int s390_tune_flags;
251 /* Which instruction set architecture to use.  */
252 enum processor_type s390_arch;
253 int s390_arch_flags;
254
255 HOST_WIDE_INT s390_warn_framesize = 0;
256 HOST_WIDE_INT s390_stack_size = 0;
257 HOST_WIDE_INT s390_stack_guard = 0;
258
259 /* The following structure is embedded in the machine
260    specific part of struct function.  */
261
262 struct GTY (()) s390_frame_layout
263 {
264   /* Offset within stack frame.  */
265   HOST_WIDE_INT gprs_offset;
266   HOST_WIDE_INT f0_offset;
267   HOST_WIDE_INT f4_offset;
268   HOST_WIDE_INT f8_offset;
269   HOST_WIDE_INT backchain_offset;
270
271   /* Number of first and last gpr where slots in the register
272      save area are reserved for.  */
273   int first_save_gpr_slot;
274   int last_save_gpr_slot;
275
276   /* Number of first and last gpr to be saved, restored.  */
277   int first_save_gpr;
278   int first_restore_gpr;
279   int last_save_gpr;
280   int last_restore_gpr;
281
282   /* Bits standing for floating point registers. Set, if the
283      respective register has to be saved. Starting with reg 16 (f0)
284      at the rightmost bit.
285      Bit 15 -  8  7  6  5  4  3  2  1  0
286      fpr 15 -  8  7  5  3  1  6  4  2  0
287      reg 31 - 24 23 22 21 20 19 18 17 16  */
288   unsigned int fpr_bitmap;
289
290   /* Number of floating point registers f8-f15 which must be saved.  */
291   int high_fprs;
292
293   /* Set if return address needs to be saved.
294      This flag is set by s390_return_addr_rtx if it could not use
295      the initial value of r14 and therefore depends on r14 saved
296      to the stack.  */
297   bool save_return_addr_p;
298
299   /* Size of stack frame.  */
300   HOST_WIDE_INT frame_size;
301 };
302
303 /* Define the structure for the machine field in struct function.  */
304
305 struct GTY(()) machine_function
306 {
307   struct s390_frame_layout frame_layout;
308
309   /* Literal pool base register.  */
310   rtx base_reg;
311
312   /* True if we may need to perform branch splitting.  */
313   bool split_branches_pending_p;
314
315   /* Some local-dynamic TLS symbol name.  */
316   const char *some_ld_name;
317
318   bool has_landing_pad_p;
319 };
320
321 /* Few accessor macros for struct cfun->machine->s390_frame_layout.  */
322
323 #define cfun_frame_layout (cfun->machine->frame_layout)
324 #define cfun_save_high_fprs_p (!!cfun_frame_layout.high_fprs)
325 #define cfun_gprs_save_area_size ((cfun_frame_layout.last_save_gpr_slot -           \
326   cfun_frame_layout.first_save_gpr_slot + 1) * UNITS_PER_WORD)
327 #define cfun_set_fpr_bit(BITNUM) (cfun->machine->frame_layout.fpr_bitmap |=    \
328   (1 << (BITNUM)))
329 #define cfun_fpr_bit_p(BITNUM) (!!(cfun->machine->frame_layout.fpr_bitmap &    \
330   (1 << (BITNUM))))
331
332 /* Number of GPRs and FPRs used for argument passing.  */
333 #define GP_ARG_NUM_REG 5
334 #define FP_ARG_NUM_REG (TARGET_64BIT? 4 : 2)
335
336 /* A couple of shortcuts.  */
337 #define CONST_OK_FOR_J(x) \
338         CONST_OK_FOR_CONSTRAINT_P((x), 'J', "J")
339 #define CONST_OK_FOR_K(x) \
340         CONST_OK_FOR_CONSTRAINT_P((x), 'K', "K")
341 #define CONST_OK_FOR_Os(x) \
342         CONST_OK_FOR_CONSTRAINT_P((x), 'O', "Os")
343 #define CONST_OK_FOR_Op(x) \
344         CONST_OK_FOR_CONSTRAINT_P((x), 'O', "Op")
345 #define CONST_OK_FOR_On(x) \
346         CONST_OK_FOR_CONSTRAINT_P((x), 'O', "On")
347
348 #define REGNO_PAIR_OK(REGNO, MODE)                               \
349   (HARD_REGNO_NREGS ((REGNO), (MODE)) == 1 || !((REGNO) & 1))
350
351 /* That's the read ahead of the dynamic branch prediction unit in
352    bytes on a z10 CPU.  */
353 #define Z10_PREDICT_DISTANCE 384
354
355 static enum machine_mode
356 s390_libgcc_cmp_return_mode (void)
357 {
358   return TARGET_64BIT ? DImode : SImode;
359 }
360
361 static enum machine_mode
362 s390_libgcc_shift_count_mode (void)
363 {
364   return TARGET_64BIT ? DImode : SImode;
365 }
366
367 /* Return true if the back end supports mode MODE.  */
368 static bool
369 s390_scalar_mode_supported_p (enum machine_mode mode)
370 {
371   if (DECIMAL_FLOAT_MODE_P (mode))
372     return default_decimal_float_supported_p ();
373   else
374     return default_scalar_mode_supported_p (mode);
375 }
376
377 /* Set the has_landing_pad_p flag in struct machine_function to VALUE.  */
378
379 void
380 s390_set_has_landing_pad_p (bool value)
381 {
382   cfun->machine->has_landing_pad_p = value;
383 }
384
385 /* If two condition code modes are compatible, return a condition code
386    mode which is compatible with both.  Otherwise, return
387    VOIDmode.  */
388
389 static enum machine_mode
390 s390_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
391 {
392   if (m1 == m2)
393     return m1;
394
395   switch (m1)
396     {
397     case CCZmode:
398       if (m2 == CCUmode || m2 == CCTmode || m2 == CCZ1mode
399           || m2 == CCSmode || m2 == CCSRmode || m2 == CCURmode)
400         return m2;
401       return VOIDmode;
402
403     case CCSmode:
404     case CCUmode:
405     case CCTmode:
406     case CCSRmode:
407     case CCURmode:
408     case CCZ1mode:
409       if (m2 == CCZmode)
410         return m1;
411
412       return VOIDmode;
413
414     default:
415       return VOIDmode;
416     }
417   return VOIDmode;
418 }
419
420 /* Return true if SET either doesn't set the CC register, or else
421    the source and destination have matching CC modes and that
422    CC mode is at least as constrained as REQ_MODE.  */
423
424 static bool
425 s390_match_ccmode_set (rtx set, enum machine_mode req_mode)
426 {
427   enum machine_mode set_mode;
428
429   gcc_assert (GET_CODE (set) == SET);
430
431   if (GET_CODE (SET_DEST (set)) != REG || !CC_REGNO_P (REGNO (SET_DEST (set))))
432     return 1;
433
434   set_mode = GET_MODE (SET_DEST (set));
435   switch (set_mode)
436     {
437     case CCSmode:
438     case CCSRmode:
439     case CCUmode:
440     case CCURmode:
441     case CCLmode:
442     case CCL1mode:
443     case CCL2mode:
444     case CCL3mode:
445     case CCT1mode:
446     case CCT2mode:
447     case CCT3mode:
448       if (req_mode != set_mode)
449         return 0;
450       break;
451
452     case CCZmode:
453       if (req_mode != CCSmode && req_mode != CCUmode && req_mode != CCTmode
454           && req_mode != CCSRmode && req_mode != CCURmode)
455         return 0;
456       break;
457
458     case CCAPmode:
459     case CCANmode:
460       if (req_mode != CCAmode)
461         return 0;
462       break;
463
464     default:
465       gcc_unreachable ();
466     }
467
468   return (GET_MODE (SET_SRC (set)) == set_mode);
469 }
470
471 /* Return true if every SET in INSN that sets the CC register
472    has source and destination with matching CC modes and that
473    CC mode is at least as constrained as REQ_MODE.
474    If REQ_MODE is VOIDmode, always return false.  */
475
476 bool
477 s390_match_ccmode (rtx insn, enum machine_mode req_mode)
478 {
479   int i;
480
481   /* s390_tm_ccmode returns VOIDmode to indicate failure.  */
482   if (req_mode == VOIDmode)
483     return false;
484
485   if (GET_CODE (PATTERN (insn)) == SET)
486     return s390_match_ccmode_set (PATTERN (insn), req_mode);
487
488   if (GET_CODE (PATTERN (insn)) == PARALLEL)
489       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
490         {
491           rtx set = XVECEXP (PATTERN (insn), 0, i);
492           if (GET_CODE (set) == SET)
493             if (!s390_match_ccmode_set (set, req_mode))
494               return false;
495         }
496
497   return true;
498 }
499
500 /* If a test-under-mask instruction can be used to implement
501    (compare (and ... OP1) OP2), return the CC mode required
502    to do that.  Otherwise, return VOIDmode.
503    MIXED is true if the instruction can distinguish between
504    CC1 and CC2 for mixed selected bits (TMxx), it is false
505    if the instruction cannot (TM).  */
506
507 enum machine_mode
508 s390_tm_ccmode (rtx op1, rtx op2, bool mixed)
509 {
510   int bit0, bit1;
511
512   /* ??? Fixme: should work on CONST_DOUBLE as well.  */
513   if (GET_CODE (op1) != CONST_INT || GET_CODE (op2) != CONST_INT)
514     return VOIDmode;
515
516   /* Selected bits all zero: CC0.
517      e.g.: int a; if ((a & (16 + 128)) == 0) */
518   if (INTVAL (op2) == 0)
519     return CCTmode;
520
521   /* Selected bits all one: CC3.
522      e.g.: int a; if ((a & (16 + 128)) == 16 + 128) */
523   if (INTVAL (op2) == INTVAL (op1))
524     return CCT3mode;
525
526   /* Exactly two bits selected, mixed zeroes and ones: CC1 or CC2. e.g.:
527      int a;
528      if ((a & (16 + 128)) == 16)         -> CCT1
529      if ((a & (16 + 128)) == 128)        -> CCT2  */
530   if (mixed)
531     {
532       bit1 = exact_log2 (INTVAL (op2));
533       bit0 = exact_log2 (INTVAL (op1) ^ INTVAL (op2));
534       if (bit0 != -1 && bit1 != -1)
535         return bit0 > bit1 ? CCT1mode : CCT2mode;
536     }
537
538   return VOIDmode;
539 }
540
541 /* Given a comparison code OP (EQ, NE, etc.) and the operands
542    OP0 and OP1 of a COMPARE, return the mode to be used for the
543    comparison.  */
544
545 enum machine_mode
546 s390_select_ccmode (enum rtx_code code, rtx op0, rtx op1)
547 {
548   switch (code)
549     {
550       case EQ:
551       case NE:
552         if ((GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
553             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
554           return CCAPmode;
555         if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
556             && CONST_OK_FOR_K (INTVAL (XEXP (op0, 1))))
557           return CCAPmode;
558         if ((GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
559              || GET_CODE (op1) == NEG)
560             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
561           return CCLmode;
562
563         if (GET_CODE (op0) == AND)
564           {
565             /* Check whether we can potentially do it via TM.  */
566             enum machine_mode ccmode;
567             ccmode = s390_tm_ccmode (XEXP (op0, 1), op1, 1);
568             if (ccmode != VOIDmode)
569               {
570                 /* Relax CCTmode to CCZmode to allow fall-back to AND
571                    if that turns out to be beneficial.  */
572                 return ccmode == CCTmode ? CCZmode : ccmode;
573               }
574           }
575
576         if (register_operand (op0, HImode)
577             && GET_CODE (op1) == CONST_INT
578             && (INTVAL (op1) == -1 || INTVAL (op1) == 65535))
579           return CCT3mode;
580         if (register_operand (op0, QImode)
581             && GET_CODE (op1) == CONST_INT
582             && (INTVAL (op1) == -1 || INTVAL (op1) == 255))
583           return CCT3mode;
584
585         return CCZmode;
586
587       case LE:
588       case LT:
589       case GE:
590       case GT:
591         /* The only overflow condition of NEG and ABS happens when
592            -INT_MAX is used as parameter, which stays negative. So
593            we have an overflow from a positive value to a negative.
594            Using CCAP mode the resulting cc can be used for comparisons.  */
595         if ((GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
596             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
597           return CCAPmode;
598
599         /* If constants are involved in an add instruction it is possible to use
600            the resulting cc for comparisons with zero. Knowing the sign of the
601            constant the overflow behavior gets predictable. e.g.:
602              int a, b; if ((b = a + c) > 0)
603            with c as a constant value: c < 0 -> CCAN and c >= 0 -> CCAP  */
604         if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
605             && CONST_OK_FOR_K (INTVAL (XEXP (op0, 1))))
606           {
607             if (INTVAL (XEXP((op0), 1)) < 0)
608               return CCANmode;
609             else
610               return CCAPmode;
611           }
612         /* Fall through.  */
613       case UNORDERED:
614       case ORDERED:
615       case UNEQ:
616       case UNLE:
617       case UNLT:
618       case UNGE:
619       case UNGT:
620       case LTGT:
621         if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
622             && GET_CODE (op1) != CONST_INT)
623           return CCSRmode;
624         return CCSmode;
625
626       case LTU:
627       case GEU:
628         if (GET_CODE (op0) == PLUS
629             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
630           return CCL1mode;
631
632         if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
633             && GET_CODE (op1) != CONST_INT)
634           return CCURmode;
635         return CCUmode;
636
637       case LEU:
638       case GTU:
639         if (GET_CODE (op0) == MINUS
640             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
641           return CCL2mode;
642
643         if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
644             && GET_CODE (op1) != CONST_INT)
645           return CCURmode;
646         return CCUmode;
647
648       default:
649         gcc_unreachable ();
650     }
651 }
652
653 /* Replace the comparison OP0 CODE OP1 by a semantically equivalent one
654    that we can implement more efficiently.  */
655
656 void
657 s390_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1)
658 {
659   /* Convert ZERO_EXTRACT back to AND to enable TM patterns.  */
660   if ((*code == EQ || *code == NE)
661       && *op1 == const0_rtx
662       && GET_CODE (*op0) == ZERO_EXTRACT
663       && GET_CODE (XEXP (*op0, 1)) == CONST_INT
664       && GET_CODE (XEXP (*op0, 2)) == CONST_INT
665       && SCALAR_INT_MODE_P (GET_MODE (XEXP (*op0, 0))))
666     {
667       rtx inner = XEXP (*op0, 0);
668       HOST_WIDE_INT modesize = GET_MODE_BITSIZE (GET_MODE (inner));
669       HOST_WIDE_INT len = INTVAL (XEXP (*op0, 1));
670       HOST_WIDE_INT pos = INTVAL (XEXP (*op0, 2));
671
672       if (len > 0 && len < modesize
673           && pos >= 0 && pos + len <= modesize
674           && modesize <= HOST_BITS_PER_WIDE_INT)
675         {
676           unsigned HOST_WIDE_INT block;
677           block = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
678           block <<= modesize - pos - len;
679
680           *op0 = gen_rtx_AND (GET_MODE (inner), inner,
681                               gen_int_mode (block, GET_MODE (inner)));
682         }
683     }
684
685   /* Narrow AND of memory against immediate to enable TM.  */
686   if ((*code == EQ || *code == NE)
687       && *op1 == const0_rtx
688       && GET_CODE (*op0) == AND
689       && GET_CODE (XEXP (*op0, 1)) == CONST_INT
690       && SCALAR_INT_MODE_P (GET_MODE (XEXP (*op0, 0))))
691     {
692       rtx inner = XEXP (*op0, 0);
693       rtx mask = XEXP (*op0, 1);
694
695       /* Ignore paradoxical SUBREGs if all extra bits are masked out.  */
696       if (GET_CODE (inner) == SUBREG
697           && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (inner)))
698           && (GET_MODE_SIZE (GET_MODE (inner))
699               >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner))))
700           && ((INTVAL (mask)
701                & GET_MODE_MASK (GET_MODE (inner))
702                & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (inner))))
703               == 0))
704         inner = SUBREG_REG (inner);
705
706       /* Do not change volatile MEMs.  */
707       if (MEM_P (inner) && !MEM_VOLATILE_P (inner))
708         {
709           int part = s390_single_part (XEXP (*op0, 1),
710                                        GET_MODE (inner), QImode, 0);
711           if (part >= 0)
712             {
713               mask = gen_int_mode (s390_extract_part (mask, QImode, 0), QImode);
714               inner = adjust_address_nv (inner, QImode, part);
715               *op0 = gen_rtx_AND (QImode, inner, mask);
716             }
717         }
718     }
719
720   /* Narrow comparisons against 0xffff to HImode if possible.  */
721   if ((*code == EQ || *code == NE)
722       && GET_CODE (*op1) == CONST_INT
723       && INTVAL (*op1) == 0xffff
724       && SCALAR_INT_MODE_P (GET_MODE (*op0))
725       && (nonzero_bits (*op0, GET_MODE (*op0))
726           & ~(unsigned HOST_WIDE_INT) 0xffff) == 0)
727     {
728       *op0 = gen_lowpart (HImode, *op0);
729       *op1 = constm1_rtx;
730     }
731
732   /* Remove redundant UNSPEC_CCU_TO_INT conversions if possible.  */
733   if (GET_CODE (*op0) == UNSPEC
734       && XINT (*op0, 1) == UNSPEC_CCU_TO_INT
735       && XVECLEN (*op0, 0) == 1
736       && GET_MODE (XVECEXP (*op0, 0, 0)) == CCUmode
737       && GET_CODE (XVECEXP (*op0, 0, 0)) == REG
738       && REGNO (XVECEXP (*op0, 0, 0)) == CC_REGNUM
739       && *op1 == const0_rtx)
740     {
741       enum rtx_code new_code = UNKNOWN;
742       switch (*code)
743         {
744           case EQ: new_code = EQ;  break;
745           case NE: new_code = NE;  break;
746           case LT: new_code = GTU; break;
747           case GT: new_code = LTU; break;
748           case LE: new_code = GEU; break;
749           case GE: new_code = LEU; break;
750           default: break;
751         }
752
753       if (new_code != UNKNOWN)
754         {
755           *op0 = XVECEXP (*op0, 0, 0);
756           *code = new_code;
757         }
758     }
759
760   /* Remove redundant UNSPEC_CCZ_TO_INT conversions if possible.  */
761   if (GET_CODE (*op0) == UNSPEC
762       && XINT (*op0, 1) == UNSPEC_CCZ_TO_INT
763       && XVECLEN (*op0, 0) == 1
764       && GET_MODE (XVECEXP (*op0, 0, 0)) == CCZmode
765       && GET_CODE (XVECEXP (*op0, 0, 0)) == REG
766       && REGNO (XVECEXP (*op0, 0, 0)) == CC_REGNUM
767       && *op1 == const0_rtx)
768     {
769       enum rtx_code new_code = UNKNOWN;
770       switch (*code)
771         {
772           case EQ: new_code = EQ;  break;
773           case NE: new_code = NE;  break;
774           default: break;
775         }
776
777       if (new_code != UNKNOWN)
778         {
779           *op0 = XVECEXP (*op0, 0, 0);
780           *code = new_code;
781         }
782     }
783
784   /* Simplify cascaded EQ, NE with const0_rtx.  */
785   if ((*code == NE || *code == EQ)
786       && (GET_CODE (*op0) == EQ || GET_CODE (*op0) == NE)
787       && GET_MODE (*op0) == SImode
788       && GET_MODE (XEXP (*op0, 0)) == CCZ1mode
789       && REG_P (XEXP (*op0, 0))
790       && XEXP (*op0, 1) == const0_rtx
791       && *op1 == const0_rtx)
792     {
793       if ((*code == EQ && GET_CODE (*op0) == NE)
794           || (*code == NE && GET_CODE (*op0) == EQ))
795         *code = EQ;
796       else
797         *code = NE;
798       *op0 = XEXP (*op0, 0);
799     }
800
801   /* Prefer register over memory as first operand.  */
802   if (MEM_P (*op0) && REG_P (*op1))
803     {
804       rtx tem = *op0; *op0 = *op1; *op1 = tem;
805       *code = swap_condition (*code);
806     }
807 }
808
809 /* Emit a compare instruction suitable to implement the comparison
810    OP0 CODE OP1.  Return the correct condition RTL to be placed in
811    the IF_THEN_ELSE of the conditional branch testing the result.  */
812
813 rtx
814 s390_emit_compare (enum rtx_code code, rtx op0, rtx op1)
815 {
816   enum machine_mode mode = s390_select_ccmode (code, op0, op1);
817   rtx cc;
818
819   /* Do not output a redundant compare instruction if a compare_and_swap
820      pattern already computed the result and the machine modes are compatible.  */
821   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
822     {
823       gcc_assert (s390_cc_modes_compatible (GET_MODE (op0), mode)
824                   == GET_MODE (op0));
825       cc = op0;
826     }
827   else
828     {
829       cc = gen_rtx_REG (mode, CC_REGNUM);
830       emit_insn (gen_rtx_SET (VOIDmode, cc, gen_rtx_COMPARE (mode, op0, op1)));
831     }
832
833   return gen_rtx_fmt_ee (code, VOIDmode, cc, const0_rtx);
834 }
835
836 /* Emit a SImode compare and swap instruction setting MEM to NEW_RTX if OLD
837    matches CMP.
838    Return the correct condition RTL to be placed in the IF_THEN_ELSE of the
839    conditional branch testing the result.  */
840
841 static rtx
842 s390_emit_compare_and_swap (enum rtx_code code, rtx old, rtx mem, rtx cmp, rtx new_rtx)
843 {
844   emit_insn (gen_sync_compare_and_swapsi (old, mem, cmp, new_rtx));
845   return s390_emit_compare (code, gen_rtx_REG (CCZ1mode, CC_REGNUM), const0_rtx);
846 }
847
848 /* Emit a jump instruction to TARGET.  If COND is NULL_RTX, emit an
849    unconditional jump, else a conditional jump under condition COND.  */
850
851 void
852 s390_emit_jump (rtx target, rtx cond)
853 {
854   rtx insn;
855
856   target = gen_rtx_LABEL_REF (VOIDmode, target);
857   if (cond)
858     target = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, target, pc_rtx);
859
860   insn = gen_rtx_SET (VOIDmode, pc_rtx, target);
861   emit_jump_insn (insn);
862 }
863
864 /* Return branch condition mask to implement a branch
865    specified by CODE.  Return -1 for invalid comparisons.  */
866
867 int
868 s390_branch_condition_mask (rtx code)
869 {
870   const int CC0 = 1 << 3;
871   const int CC1 = 1 << 2;
872   const int CC2 = 1 << 1;
873   const int CC3 = 1 << 0;
874
875   gcc_assert (GET_CODE (XEXP (code, 0)) == REG);
876   gcc_assert (REGNO (XEXP (code, 0)) == CC_REGNUM);
877   gcc_assert (XEXP (code, 1) == const0_rtx);
878
879   switch (GET_MODE (XEXP (code, 0)))
880     {
881     case CCZmode:
882     case CCZ1mode:
883       switch (GET_CODE (code))
884         {
885         case EQ:        return CC0;
886         case NE:        return CC1 | CC2 | CC3;
887         default:        return -1;
888         }
889       break;
890
891     case CCT1mode:
892       switch (GET_CODE (code))
893         {
894         case EQ:        return CC1;
895         case NE:        return CC0 | CC2 | CC3;
896         default:        return -1;
897         }
898       break;
899
900     case CCT2mode:
901       switch (GET_CODE (code))
902         {
903         case EQ:        return CC2;
904         case NE:        return CC0 | CC1 | CC3;
905         default:        return -1;
906         }
907       break;
908
909     case CCT3mode:
910       switch (GET_CODE (code))
911         {
912         case EQ:        return CC3;
913         case NE:        return CC0 | CC1 | CC2;
914         default:        return -1;
915         }
916       break;
917
918     case CCLmode:
919       switch (GET_CODE (code))
920         {
921         case EQ:        return CC0 | CC2;
922         case NE:        return CC1 | CC3;
923         default:        return -1;
924         }
925       break;
926
927     case CCL1mode:
928       switch (GET_CODE (code))
929         {
930         case LTU:       return CC2 | CC3;  /* carry */
931         case GEU:       return CC0 | CC1;  /* no carry */
932         default:        return -1;
933         }
934       break;
935
936     case CCL2mode:
937       switch (GET_CODE (code))
938         {
939         case GTU:       return CC0 | CC1;  /* borrow */
940         case LEU:       return CC2 | CC3;  /* no borrow */
941         default:        return -1;
942         }
943       break;
944
945     case CCL3mode:
946       switch (GET_CODE (code))
947         {
948         case EQ:        return CC0 | CC2;
949         case NE:        return CC1 | CC3;
950         case LTU:       return CC1;
951         case GTU:       return CC3;
952         case LEU:       return CC1 | CC2;
953         case GEU:       return CC2 | CC3;
954         default:        return -1;
955         }
956
957     case CCUmode:
958       switch (GET_CODE (code))
959         {
960         case EQ:        return CC0;
961         case NE:        return CC1 | CC2 | CC3;
962         case LTU:       return CC1;
963         case GTU:       return CC2;
964         case LEU:       return CC0 | CC1;
965         case GEU:       return CC0 | CC2;
966         default:        return -1;
967         }
968       break;
969
970     case CCURmode:
971       switch (GET_CODE (code))
972         {
973         case EQ:        return CC0;
974         case NE:        return CC2 | CC1 | CC3;
975         case LTU:       return CC2;
976         case GTU:       return CC1;
977         case LEU:       return CC0 | CC2;
978         case GEU:       return CC0 | CC1;
979         default:        return -1;
980         }
981       break;
982
983     case CCAPmode:
984       switch (GET_CODE (code))
985         {
986         case EQ:        return CC0;
987         case NE:        return CC1 | CC2 | CC3;
988         case LT:        return CC1 | CC3;
989         case GT:        return CC2;
990         case LE:        return CC0 | CC1 | CC3;
991         case GE:        return CC0 | CC2;
992         default:        return -1;
993         }
994       break;
995
996     case CCANmode:
997       switch (GET_CODE (code))
998         {
999         case EQ:        return CC0;
1000         case NE:        return CC1 | CC2 | CC3;
1001         case LT:        return CC1;
1002         case GT:        return CC2 | CC3;
1003         case LE:        return CC0 | CC1;
1004         case GE:        return CC0 | CC2 | CC3;
1005         default:        return -1;
1006         }
1007       break;
1008
1009     case CCSmode:
1010       switch (GET_CODE (code))
1011         {
1012         case EQ:        return CC0;
1013         case NE:        return CC1 | CC2 | CC3;
1014         case LT:        return CC1;
1015         case GT:        return CC2;
1016         case LE:        return CC0 | CC1;
1017         case GE:        return CC0 | CC2;
1018         case UNORDERED: return CC3;
1019         case ORDERED:   return CC0 | CC1 | CC2;
1020         case UNEQ:      return CC0 | CC3;
1021         case UNLT:      return CC1 | CC3;
1022         case UNGT:      return CC2 | CC3;
1023         case UNLE:      return CC0 | CC1 | CC3;
1024         case UNGE:      return CC0 | CC2 | CC3;
1025         case LTGT:      return CC1 | CC2;
1026         default:        return -1;
1027         }
1028       break;
1029
1030     case CCSRmode:
1031       switch (GET_CODE (code))
1032         {
1033         case EQ:        return CC0;
1034         case NE:        return CC2 | CC1 | CC3;
1035         case LT:        return CC2;
1036         case GT:        return CC1;
1037         case LE:        return CC0 | CC2;
1038         case GE:        return CC0 | CC1;
1039         case UNORDERED: return CC3;
1040         case ORDERED:   return CC0 | CC2 | CC1;
1041         case UNEQ:      return CC0 | CC3;
1042         case UNLT:      return CC2 | CC3;
1043         case UNGT:      return CC1 | CC3;
1044         case UNLE:      return CC0 | CC2 | CC3;
1045         case UNGE:      return CC0 | CC1 | CC3;
1046         case LTGT:      return CC2 | CC1;
1047         default:        return -1;
1048         }
1049       break;
1050
1051     default:
1052       return -1;
1053     }
1054 }
1055
1056
1057 /* Return branch condition mask to implement a compare and branch
1058    specified by CODE.  Return -1 for invalid comparisons.  */
1059
1060 int
1061 s390_compare_and_branch_condition_mask (rtx code)
1062 {
1063   const int CC0 = 1 << 3;
1064   const int CC1 = 1 << 2;
1065   const int CC2 = 1 << 1;
1066
1067   switch (GET_CODE (code))
1068     {
1069     case EQ:
1070       return CC0;
1071     case NE:
1072       return CC1 | CC2;
1073     case LT:
1074     case LTU:
1075       return CC1;
1076     case GT:
1077     case GTU:
1078       return CC2;
1079     case LE:
1080     case LEU:
1081       return CC0 | CC1;
1082     case GE:
1083     case GEU:
1084       return CC0 | CC2;
1085     default:
1086       gcc_unreachable ();
1087     }
1088   return -1;
1089 }
1090
1091 /* If INV is false, return assembler mnemonic string to implement
1092    a branch specified by CODE.  If INV is true, return mnemonic
1093    for the corresponding inverted branch.  */
1094
1095 static const char *
1096 s390_branch_condition_mnemonic (rtx code, int inv)
1097 {
1098   int mask;
1099
1100   static const char *const mnemonic[16] =
1101     {
1102       NULL, "o", "h", "nle",
1103       "l", "nhe", "lh", "ne",
1104       "e", "nlh", "he", "nl",
1105       "le", "nh", "no", NULL
1106     };
1107
1108   if (GET_CODE (XEXP (code, 0)) == REG
1109       && REGNO (XEXP (code, 0)) == CC_REGNUM
1110       && XEXP (code, 1) == const0_rtx)
1111     mask = s390_branch_condition_mask (code);
1112   else
1113     mask = s390_compare_and_branch_condition_mask (code);
1114
1115   gcc_assert (mask >= 0);
1116
1117   if (inv)
1118     mask ^= 15;
1119
1120   gcc_assert (mask >= 1 && mask <= 14);
1121
1122   return mnemonic[mask];
1123 }
1124
1125 /* Return the part of op which has a value different from def.
1126    The size of the part is determined by mode.
1127    Use this function only if you already know that op really
1128    contains such a part.  */
1129
1130 unsigned HOST_WIDE_INT
1131 s390_extract_part (rtx op, enum machine_mode mode, int def)
1132 {
1133   unsigned HOST_WIDE_INT value = 0;
1134   int max_parts = HOST_BITS_PER_WIDE_INT / GET_MODE_BITSIZE (mode);
1135   int part_bits = GET_MODE_BITSIZE (mode);
1136   unsigned HOST_WIDE_INT part_mask
1137     = ((unsigned HOST_WIDE_INT)1 << part_bits) - 1;
1138   int i;
1139
1140   for (i = 0; i < max_parts; i++)
1141     {
1142       if (i == 0)
1143         value = (unsigned HOST_WIDE_INT) INTVAL (op);
1144       else
1145         value >>= part_bits;
1146
1147       if ((value & part_mask) != (def & part_mask))
1148         return value & part_mask;
1149     }
1150
1151   gcc_unreachable ();
1152 }
1153
1154 /* If OP is an integer constant of mode MODE with exactly one
1155    part of mode PART_MODE unequal to DEF, return the number of that
1156    part. Otherwise, return -1.  */
1157
1158 int
1159 s390_single_part (rtx op,
1160                   enum machine_mode mode,
1161                   enum machine_mode part_mode,
1162                   int def)
1163 {
1164   unsigned HOST_WIDE_INT value = 0;
1165   int n_parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (part_mode);
1166   unsigned HOST_WIDE_INT part_mask
1167     = ((unsigned HOST_WIDE_INT)1 << GET_MODE_BITSIZE (part_mode)) - 1;
1168   int i, part = -1;
1169
1170   if (GET_CODE (op) != CONST_INT)
1171     return -1;
1172
1173   for (i = 0; i < n_parts; i++)
1174     {
1175       if (i == 0)
1176         value = (unsigned HOST_WIDE_INT) INTVAL (op);
1177       else
1178         value >>= GET_MODE_BITSIZE (part_mode);
1179
1180       if ((value & part_mask) != (def & part_mask))
1181         {
1182           if (part != -1)
1183             return -1;
1184           else
1185             part = i;
1186         }
1187     }
1188   return part == -1 ? -1 : n_parts - 1 - part;
1189 }
1190
1191 /* Return true if IN contains a contiguous bitfield in the lower SIZE
1192    bits and no other bits are set in IN.  POS and LENGTH can be used
1193    to obtain the start position and the length of the bitfield.
1194
1195    POS gives the position of the first bit of the bitfield counting
1196    from the lowest order bit starting with zero.  In order to use this
1197    value for S/390 instructions this has to be converted to "bits big
1198    endian" style.  */
1199
1200 bool
1201 s390_contiguous_bitmask_p (unsigned HOST_WIDE_INT in, int size,
1202                            int *pos, int *length)
1203 {
1204   int tmp_pos = 0;
1205   int tmp_length = 0;
1206   int i;
1207   unsigned HOST_WIDE_INT mask = 1ULL;
1208   bool contiguous = false;
1209
1210   for (i = 0; i < size; mask <<= 1, i++)
1211     {
1212       if (contiguous)
1213         {
1214           if (mask & in)
1215             tmp_length++;
1216           else
1217             break;
1218         }
1219       else
1220         {
1221           if (mask & in)
1222             {
1223               contiguous = true;
1224               tmp_length++;
1225             }
1226           else
1227             tmp_pos++;
1228         }
1229     }
1230
1231   if (!tmp_length)
1232     return false;
1233
1234   /* Calculate a mask for all bits beyond the contiguous bits.  */
1235   mask = (-1LL & ~(((1ULL << (tmp_length + tmp_pos - 1)) << 1) - 1));
1236
1237   if (mask & in)
1238     return false;
1239
1240   if (tmp_length + tmp_pos - 1 > size)
1241     return false;
1242
1243   if (length)
1244     *length = tmp_length;
1245
1246   if (pos)
1247     *pos = tmp_pos;
1248
1249   return true;
1250 }
1251
1252 /* Check whether we can (and want to) split a double-word
1253    move in mode MODE from SRC to DST into two single-word
1254    moves, moving the subword FIRST_SUBWORD first.  */
1255
1256 bool
1257 s390_split_ok_p (rtx dst, rtx src, enum machine_mode mode, int first_subword)
1258 {
1259   /* Floating point registers cannot be split.  */
1260   if (FP_REG_P (src) || FP_REG_P (dst))
1261     return false;
1262
1263   /* We don't need to split if operands are directly accessible.  */
1264   if (s_operand (src, mode) || s_operand (dst, mode))
1265     return false;
1266
1267   /* Non-offsettable memory references cannot be split.  */
1268   if ((GET_CODE (src) == MEM && !offsettable_memref_p (src))
1269       || (GET_CODE (dst) == MEM && !offsettable_memref_p (dst)))
1270     return false;
1271
1272   /* Moving the first subword must not clobber a register
1273      needed to move the second subword.  */
1274   if (register_operand (dst, mode))
1275     {
1276       rtx subreg = operand_subword (dst, first_subword, 0, mode);
1277       if (reg_overlap_mentioned_p (subreg, src))
1278         return false;
1279     }
1280
1281   return true;
1282 }
1283
1284 /* Return true if it can be proven that [MEM1, MEM1 + SIZE]
1285    and [MEM2, MEM2 + SIZE] do overlap and false
1286    otherwise.  */
1287
1288 bool
1289 s390_overlap_p (rtx mem1, rtx mem2, HOST_WIDE_INT size)
1290 {
1291   rtx addr1, addr2, addr_delta;
1292   HOST_WIDE_INT delta;
1293
1294   if (GET_CODE (mem1) != MEM || GET_CODE (mem2) != MEM)
1295     return true;
1296
1297   if (size == 0)
1298     return false;
1299
1300   addr1 = XEXP (mem1, 0);
1301   addr2 = XEXP (mem2, 0);
1302
1303   addr_delta = simplify_binary_operation (MINUS, Pmode, addr2, addr1);
1304
1305   /* This overlapping check is used by peepholes merging memory block operations.
1306      Overlapping operations would otherwise be recognized by the S/390 hardware
1307      and would fall back to a slower implementation. Allowing overlapping
1308      operations would lead to slow code but not to wrong code. Therefore we are
1309      somewhat optimistic if we cannot prove that the memory blocks are
1310      overlapping.
1311      That's why we return false here although this may accept operations on
1312      overlapping memory areas.  */
1313   if (!addr_delta || GET_CODE (addr_delta) != CONST_INT)
1314     return false;
1315
1316   delta = INTVAL (addr_delta);
1317
1318   if (delta == 0
1319       || (delta > 0 && delta < size)
1320       || (delta < 0 && -delta < size))
1321     return true;
1322
1323   return false;
1324 }
1325
1326 /* Check whether the address of memory reference MEM2 equals exactly
1327    the address of memory reference MEM1 plus DELTA.  Return true if
1328    we can prove this to be the case, false otherwise.  */
1329
1330 bool
1331 s390_offset_p (rtx mem1, rtx mem2, rtx delta)
1332 {
1333   rtx addr1, addr2, addr_delta;
1334
1335   if (GET_CODE (mem1) != MEM || GET_CODE (mem2) != MEM)
1336     return false;
1337
1338   addr1 = XEXP (mem1, 0);
1339   addr2 = XEXP (mem2, 0);
1340
1341   addr_delta = simplify_binary_operation (MINUS, Pmode, addr2, addr1);
1342   if (!addr_delta || !rtx_equal_p (addr_delta, delta))
1343     return false;
1344
1345   return true;
1346 }
1347
1348 /* Expand logical operator CODE in mode MODE with operands OPERANDS.  */
1349
1350 void
1351 s390_expand_logical_operator (enum rtx_code code, enum machine_mode mode,
1352                               rtx *operands)
1353 {
1354   enum machine_mode wmode = mode;
1355   rtx dst = operands[0];
1356   rtx src1 = operands[1];
1357   rtx src2 = operands[2];
1358   rtx op, clob, tem;
1359
1360   /* If we cannot handle the operation directly, use a temp register.  */
1361   if (!s390_logical_operator_ok_p (operands))
1362     dst = gen_reg_rtx (mode);
1363
1364   /* QImode and HImode patterns make sense only if we have a destination
1365      in memory.  Otherwise perform the operation in SImode.  */
1366   if ((mode == QImode || mode == HImode) && GET_CODE (dst) != MEM)
1367     wmode = SImode;
1368
1369   /* Widen operands if required.  */
1370   if (mode != wmode)
1371     {
1372       if (GET_CODE (dst) == SUBREG
1373           && (tem = simplify_subreg (wmode, dst, mode, 0)) != 0)
1374         dst = tem;
1375       else if (REG_P (dst))
1376         dst = gen_rtx_SUBREG (wmode, dst, 0);
1377       else
1378         dst = gen_reg_rtx (wmode);
1379
1380       if (GET_CODE (src1) == SUBREG
1381           && (tem = simplify_subreg (wmode, src1, mode, 0)) != 0)
1382         src1 = tem;
1383       else if (GET_MODE (src1) != VOIDmode)
1384         src1 = gen_rtx_SUBREG (wmode, force_reg (mode, src1), 0);
1385
1386       if (GET_CODE (src2) == SUBREG
1387           && (tem = simplify_subreg (wmode, src2, mode, 0)) != 0)
1388         src2 = tem;
1389       else if (GET_MODE (src2) != VOIDmode)
1390         src2 = gen_rtx_SUBREG (wmode, force_reg (mode, src2), 0);
1391     }
1392
1393   /* Emit the instruction.  */
1394   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, wmode, src1, src2));
1395   clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
1396   emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
1397
1398   /* Fix up the destination if needed.  */
1399   if (dst != operands[0])
1400     emit_move_insn (operands[0], gen_lowpart (mode, dst));
1401 }
1402
1403 /* Check whether OPERANDS are OK for a logical operation (AND, IOR, XOR).  */
1404
1405 bool
1406 s390_logical_operator_ok_p (rtx *operands)
1407 {
1408   /* If the destination operand is in memory, it needs to coincide
1409      with one of the source operands.  After reload, it has to be
1410      the first source operand.  */
1411   if (GET_CODE (operands[0]) == MEM)
1412     return rtx_equal_p (operands[0], operands[1])
1413            || (!reload_completed && rtx_equal_p (operands[0], operands[2]));
1414
1415   return true;
1416 }
1417
1418 /* Narrow logical operation CODE of memory operand MEMOP with immediate
1419    operand IMMOP to switch from SS to SI type instructions.  */
1420
1421 void
1422 s390_narrow_logical_operator (enum rtx_code code, rtx *memop, rtx *immop)
1423 {
1424   int def = code == AND ? -1 : 0;
1425   HOST_WIDE_INT mask;
1426   int part;
1427
1428   gcc_assert (GET_CODE (*memop) == MEM);
1429   gcc_assert (!MEM_VOLATILE_P (*memop));
1430
1431   mask = s390_extract_part (*immop, QImode, def);
1432   part = s390_single_part (*immop, GET_MODE (*memop), QImode, def);
1433   gcc_assert (part >= 0);
1434
1435   *memop = adjust_address (*memop, QImode, part);
1436   *immop = gen_int_mode (mask, QImode);
1437 }
1438
1439
1440 /* How to allocate a 'struct machine_function'.  */
1441
1442 static struct machine_function *
1443 s390_init_machine_status (void)
1444 {
1445   return GGC_CNEW (struct machine_function);
1446 }
1447
1448 /* Change optimizations to be performed, depending on the
1449    optimization level.
1450
1451    LEVEL is the optimization level specified; 2 if `-O2' is
1452    specified, 1 if `-O' is specified, and 0 if neither is specified.
1453
1454    SIZE is nonzero if `-Os' is specified and zero otherwise.  */
1455
1456 void
1457 optimization_options (int level ATTRIBUTE_UNUSED, int size ATTRIBUTE_UNUSED)
1458 {
1459   /* ??? There are apparently still problems with -fcaller-saves.  */
1460   flag_caller_saves = 0;
1461
1462   /* By default, always emit DWARF-2 unwind info.  This allows debugging
1463      without maintaining a stack frame back-chain.  */
1464   flag_asynchronous_unwind_tables = 1;
1465
1466   /* Use MVCLE instructions to decrease code size if requested.  */
1467   if (size != 0)
1468     target_flags |= MASK_MVCLE;
1469 }
1470
1471 /* Return true if ARG is the name of a processor.  Set *TYPE and *FLAGS
1472    to the associated processor_type and processor_flags if so.  */
1473
1474 static bool
1475 s390_handle_arch_option (const char *arg,
1476                          enum processor_type *type,
1477                          int *flags)
1478 {
1479   static struct pta
1480     {
1481       const char *const name;           /* processor name or nickname.  */
1482       const enum processor_type processor;
1483       const int flags;                  /* From enum processor_flags. */
1484     }
1485   const processor_alias_table[] =
1486     {
1487       {"g5", PROCESSOR_9672_G5, PF_IEEE_FLOAT},
1488       {"g6", PROCESSOR_9672_G6, PF_IEEE_FLOAT},
1489       {"z900", PROCESSOR_2064_Z900, PF_IEEE_FLOAT | PF_ZARCH},
1490       {"z990", PROCESSOR_2084_Z990, PF_IEEE_FLOAT | PF_ZARCH
1491                                     | PF_LONG_DISPLACEMENT},
1492       {"z9-109", PROCESSOR_2094_Z9_109, PF_IEEE_FLOAT | PF_ZARCH
1493                                        | PF_LONG_DISPLACEMENT | PF_EXTIMM},
1494       {"z9-ec", PROCESSOR_2094_Z9_109, PF_IEEE_FLOAT | PF_ZARCH
1495                              | PF_LONG_DISPLACEMENT | PF_EXTIMM | PF_DFP },
1496       {"z10", PROCESSOR_2097_Z10, PF_IEEE_FLOAT | PF_ZARCH
1497                              | PF_LONG_DISPLACEMENT | PF_EXTIMM | PF_DFP | PF_Z10},
1498     };
1499   size_t i;
1500
1501   for (i = 0; i < ARRAY_SIZE (processor_alias_table); i++)
1502     if (strcmp (arg, processor_alias_table[i].name) == 0)
1503       {
1504         *type = processor_alias_table[i].processor;
1505         *flags = processor_alias_table[i].flags;
1506         return true;
1507       }
1508   return false;
1509 }
1510
1511 /* Implement TARGET_HANDLE_OPTION.  */
1512
1513 static bool
1514 s390_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
1515 {
1516   switch (code)
1517     {
1518     case OPT_march_:
1519       return s390_handle_arch_option (arg, &s390_arch, &s390_arch_flags);
1520
1521     case OPT_mstack_guard_:
1522       if (sscanf (arg, HOST_WIDE_INT_PRINT_DEC, &s390_stack_guard) != 1)
1523         return false;
1524       if (exact_log2 (s390_stack_guard) == -1)
1525         error ("stack guard value must be an exact power of 2");
1526       return true;
1527
1528     case OPT_mstack_size_:
1529       if (sscanf (arg, HOST_WIDE_INT_PRINT_DEC, &s390_stack_size) != 1)
1530         return false;
1531       if (exact_log2 (s390_stack_size) == -1)
1532         error ("stack size must be an exact power of 2");
1533       return true;
1534
1535     case OPT_mtune_:
1536       return s390_handle_arch_option (arg, &s390_tune, &s390_tune_flags);
1537
1538     case OPT_mwarn_framesize_:
1539       return sscanf (arg, HOST_WIDE_INT_PRINT_DEC, &s390_warn_framesize) == 1;
1540
1541     default:
1542       return true;
1543     }
1544 }
1545
1546 void
1547 override_options (void)
1548 {
1549   /* Set up function hooks.  */
1550   init_machine_status = s390_init_machine_status;
1551
1552   /* Architecture mode defaults according to ABI.  */
1553   if (!(target_flags_explicit & MASK_ZARCH))
1554     {
1555       if (TARGET_64BIT)
1556         target_flags |= MASK_ZARCH;
1557       else
1558         target_flags &= ~MASK_ZARCH;
1559     }
1560
1561   /* Determine processor architectural level.  */
1562   if (!s390_arch_string)
1563     {
1564       s390_arch_string = TARGET_ZARCH? "z900" : "g5";
1565       s390_handle_arch_option (s390_arch_string, &s390_arch, &s390_arch_flags);
1566     }
1567
1568   /* Determine processor to tune for.  */
1569   if (s390_tune == PROCESSOR_max)
1570     {
1571       s390_tune = s390_arch;
1572       s390_tune_flags = s390_arch_flags;
1573     }
1574
1575   /* Sanity checks.  */
1576   if (TARGET_ZARCH && !TARGET_CPU_ZARCH)
1577     error ("z/Architecture mode not supported on %s", s390_arch_string);
1578   if (TARGET_64BIT && !TARGET_ZARCH)
1579     error ("64-bit ABI not supported in ESA/390 mode");
1580
1581   if (TARGET_HARD_DFP && !TARGET_DFP)
1582     {
1583       if (target_flags_explicit & MASK_HARD_DFP)
1584         {
1585           if (!TARGET_CPU_DFP)
1586             error ("Hardware decimal floating point instructions"
1587                    " not available on %s", s390_arch_string);
1588           if (!TARGET_ZARCH)
1589             error ("Hardware decimal floating point instructions"
1590                    " not available in ESA/390 mode");
1591         }
1592       else
1593         target_flags &= ~MASK_HARD_DFP;
1594     }
1595
1596   if ((target_flags_explicit & MASK_SOFT_FLOAT) && TARGET_SOFT_FLOAT)
1597     {
1598       if ((target_flags_explicit & MASK_HARD_DFP) && TARGET_HARD_DFP)
1599         error ("-mhard-dfp can't be used in conjunction with -msoft-float");
1600
1601       target_flags &= ~MASK_HARD_DFP;
1602     }
1603
1604   /* Set processor cost function.  */
1605   switch (s390_tune)
1606     {
1607     case PROCESSOR_2084_Z990:
1608       s390_cost = &z990_cost;
1609       break;
1610     case PROCESSOR_2094_Z9_109:
1611       s390_cost = &z9_109_cost;
1612       break;
1613     case PROCESSOR_2097_Z10:
1614       s390_cost = &z10_cost;
1615       break;
1616     default:
1617       s390_cost = &z900_cost;
1618     }
1619
1620   if (TARGET_BACKCHAIN && TARGET_PACKED_STACK && TARGET_HARD_FLOAT)
1621     error ("-mbackchain -mpacked-stack -mhard-float are not supported "
1622            "in combination");
1623
1624   if (s390_stack_size)
1625     {
1626       if (s390_stack_guard >= s390_stack_size)
1627         error ("stack size must be greater than the stack guard value");
1628       else if (s390_stack_size > 1 << 16)
1629         error ("stack size must not be greater than 64k");
1630     }
1631   else if (s390_stack_guard)
1632     error ("-mstack-guard implies use of -mstack-size");
1633
1634 #ifdef TARGET_DEFAULT_LONG_DOUBLE_128
1635   if (!(target_flags_explicit & MASK_LONG_DOUBLE_128))
1636     target_flags |= MASK_LONG_DOUBLE_128;
1637 #endif
1638
1639   if (s390_tune == PROCESSOR_2097_Z10)
1640     {
1641       if (!PARAM_SET_P (PARAM_MAX_UNROLLED_INSNS))
1642         set_param_value ("max-unrolled-insns", 100);
1643       if (!PARAM_SET_P (PARAM_MAX_UNROLL_TIMES))
1644         set_param_value ("max-unroll-times", 32);
1645       if (!PARAM_SET_P (PARAM_MAX_COMPLETELY_PEELED_INSNS))
1646         set_param_value ("max-completely-peeled-insns", 800);
1647       if (!PARAM_SET_P (PARAM_MAX_COMPLETELY_PEEL_TIMES))
1648         set_param_value ("max-completely-peel-times", 64);
1649     }
1650
1651   set_param_value ("max-pending-list-length", 256);
1652 }
1653
1654 /* Map for smallest class containing reg regno.  */
1655
1656 const enum reg_class regclass_map[FIRST_PSEUDO_REGISTER] =
1657 { GENERAL_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
1658   ADDR_REGS,    ADDR_REGS, ADDR_REGS, ADDR_REGS,
1659   ADDR_REGS,    ADDR_REGS, ADDR_REGS, ADDR_REGS,
1660   ADDR_REGS,    ADDR_REGS, ADDR_REGS, ADDR_REGS,
1661   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,
1662   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,
1663   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,
1664   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,
1665   ADDR_REGS,    CC_REGS,   ADDR_REGS, ADDR_REGS,
1666   ACCESS_REGS,  ACCESS_REGS
1667 };
1668
1669 /* Return attribute type of insn.  */
1670
1671 static enum attr_type
1672 s390_safe_attr_type (rtx insn)
1673 {
1674   if (recog_memoized (insn) >= 0)
1675     return get_attr_type (insn);
1676   else
1677     return TYPE_NONE;
1678 }
1679
1680 /* Return true if DISP is a valid short displacement.  */
1681
1682 static bool
1683 s390_short_displacement (rtx disp)
1684 {
1685   /* No displacement is OK.  */
1686   if (!disp)
1687     return true;
1688
1689   /* Without the long displacement facility we don't need to
1690      distingiush between long and short displacement.  */
1691   if (!TARGET_LONG_DISPLACEMENT)
1692     return true;
1693
1694   /* Integer displacement in range.  */
1695   if (GET_CODE (disp) == CONST_INT)
1696     return INTVAL (disp) >= 0 && INTVAL (disp) < 4096;
1697
1698   /* GOT offset is not OK, the GOT can be large.  */
1699   if (GET_CODE (disp) == CONST
1700       && GET_CODE (XEXP (disp, 0)) == UNSPEC
1701       && (XINT (XEXP (disp, 0), 1) == UNSPEC_GOT
1702           || XINT (XEXP (disp, 0), 1) == UNSPEC_GOTNTPOFF))
1703     return false;
1704
1705   /* All other symbolic constants are literal pool references,
1706      which are OK as the literal pool must be small.  */
1707   if (GET_CODE (disp) == CONST)
1708     return true;
1709
1710   return false;
1711 }
1712
1713 /* Decompose a RTL expression ADDR for a memory address into
1714    its components, returned in OUT.
1715
1716    Returns false if ADDR is not a valid memory address, true
1717    otherwise.  If OUT is NULL, don't return the components,
1718    but check for validity only.
1719
1720    Note: Only addresses in canonical form are recognized.
1721    LEGITIMIZE_ADDRESS should convert non-canonical forms to the
1722    canonical form so that they will be recognized.  */
1723
1724 static int
1725 s390_decompose_address (rtx addr, struct s390_address *out)
1726 {
1727   HOST_WIDE_INT offset = 0;
1728   rtx base = NULL_RTX;
1729   rtx indx = NULL_RTX;
1730   rtx disp = NULL_RTX;
1731   rtx orig_disp;
1732   bool pointer = false;
1733   bool base_ptr = false;
1734   bool indx_ptr = false;
1735   bool literal_pool = false;
1736
1737   /* We may need to substitute the literal pool base register into the address
1738      below.  However, at this point we do not know which register is going to
1739      be used as base, so we substitute the arg pointer register.  This is going
1740      to be treated as holding a pointer below -- it shouldn't be used for any
1741      other purpose.  */
1742   rtx fake_pool_base = gen_rtx_REG (Pmode, ARG_POINTER_REGNUM);
1743
1744   /* Decompose address into base + index + displacement.  */
1745
1746   if (GET_CODE (addr) == REG || GET_CODE (addr) == UNSPEC)
1747     base = addr;
1748
1749   else if (GET_CODE (addr) == PLUS)
1750     {
1751       rtx op0 = XEXP (addr, 0);
1752       rtx op1 = XEXP (addr, 1);
1753       enum rtx_code code0 = GET_CODE (op0);
1754       enum rtx_code code1 = GET_CODE (op1);
1755
1756       if (code0 == REG || code0 == UNSPEC)
1757         {
1758           if (code1 == REG || code1 == UNSPEC)
1759             {
1760               indx = op0;       /* index + base */
1761               base = op1;
1762             }
1763
1764           else
1765             {
1766               base = op0;       /* base + displacement */
1767               disp = op1;
1768             }
1769         }
1770
1771       else if (code0 == PLUS)
1772         {
1773           indx = XEXP (op0, 0); /* index + base + disp */
1774           base = XEXP (op0, 1);
1775           disp = op1;
1776         }
1777
1778       else
1779         {
1780           return false;
1781         }
1782     }
1783
1784   else
1785     disp = addr;                /* displacement */
1786
1787   /* Extract integer part of displacement.  */
1788   orig_disp = disp;
1789   if (disp)
1790     {
1791       if (GET_CODE (disp) == CONST_INT)
1792         {
1793           offset = INTVAL (disp);
1794           disp = NULL_RTX;
1795         }
1796       else if (GET_CODE (disp) == CONST
1797                && GET_CODE (XEXP (disp, 0)) == PLUS
1798                && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
1799         {
1800           offset = INTVAL (XEXP (XEXP (disp, 0), 1));
1801           disp = XEXP (XEXP (disp, 0), 0);
1802         }
1803     }
1804
1805   /* Strip off CONST here to avoid special case tests later.  */
1806   if (disp && GET_CODE (disp) == CONST)
1807     disp = XEXP (disp, 0);
1808
1809   /* We can convert literal pool addresses to
1810      displacements by basing them off the base register.  */
1811   if (disp && GET_CODE (disp) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (disp))
1812     {
1813       /* Either base or index must be free to hold the base register.  */
1814       if (!base)
1815         base = fake_pool_base, literal_pool = true;
1816       else if (!indx)
1817         indx = fake_pool_base, literal_pool = true;
1818       else
1819         return false;
1820
1821       /* Mark up the displacement.  */
1822       disp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, disp),
1823                              UNSPEC_LTREL_OFFSET);
1824     }
1825
1826   /* Validate base register.  */
1827   if (base)
1828     {
1829       if (GET_CODE (base) == UNSPEC)
1830         switch (XINT (base, 1))
1831           {
1832           case UNSPEC_LTREF:
1833             if (!disp)
1834               disp = gen_rtx_UNSPEC (Pmode,
1835                                      gen_rtvec (1, XVECEXP (base, 0, 0)),
1836                                      UNSPEC_LTREL_OFFSET);
1837             else
1838               return false;
1839
1840             base = XVECEXP (base, 0, 1);
1841             break;
1842
1843           case UNSPEC_LTREL_BASE:
1844             if (XVECLEN (base, 0) == 1)
1845               base = fake_pool_base, literal_pool = true;
1846             else
1847               base = XVECEXP (base, 0, 1);
1848             break;
1849
1850           default:
1851             return false;
1852           }
1853
1854       if (!REG_P (base)
1855           || (GET_MODE (base) != SImode
1856               && GET_MODE (base) != Pmode))
1857         return false;
1858
1859       if (REGNO (base) == STACK_POINTER_REGNUM
1860           || REGNO (base) == FRAME_POINTER_REGNUM
1861           || ((reload_completed || reload_in_progress)
1862               && frame_pointer_needed
1863               && REGNO (base) == HARD_FRAME_POINTER_REGNUM)
1864           || REGNO (base) == ARG_POINTER_REGNUM
1865           || (flag_pic
1866               && REGNO (base) == PIC_OFFSET_TABLE_REGNUM))
1867         pointer = base_ptr = true;
1868
1869       if ((reload_completed || reload_in_progress)
1870           && base == cfun->machine->base_reg)
1871         pointer = base_ptr = literal_pool = true;
1872     }
1873
1874   /* Validate index register.  */
1875   if (indx)
1876     {
1877       if (GET_CODE (indx) == UNSPEC)
1878         switch (XINT (indx, 1))
1879           {
1880           case UNSPEC_LTREF:
1881             if (!disp)
1882               disp = gen_rtx_UNSPEC (Pmode,
1883                                      gen_rtvec (1, XVECEXP (indx, 0, 0)),
1884                                      UNSPEC_LTREL_OFFSET);
1885             else
1886               return false;
1887
1888             indx = XVECEXP (indx, 0, 1);
1889             break;
1890
1891           case UNSPEC_LTREL_BASE:
1892             if (XVECLEN (indx, 0) == 1)
1893               indx = fake_pool_base, literal_pool = true;
1894             else
1895               indx = XVECEXP (indx, 0, 1);
1896             break;
1897
1898           default:
1899             return false;
1900           }
1901
1902       if (!REG_P (indx)
1903           || (GET_MODE (indx) != SImode
1904               && GET_MODE (indx) != Pmode))
1905         return false;
1906
1907       if (REGNO (indx) == STACK_POINTER_REGNUM
1908           || REGNO (indx) == FRAME_POINTER_REGNUM
1909           || ((reload_completed || reload_in_progress)
1910               && frame_pointer_needed
1911               && REGNO (indx) == HARD_FRAME_POINTER_REGNUM)
1912           || REGNO (indx) == ARG_POINTER_REGNUM
1913           || (flag_pic
1914               && REGNO (indx) == PIC_OFFSET_TABLE_REGNUM))
1915         pointer = indx_ptr = true;
1916
1917       if ((reload_completed || reload_in_progress)
1918           && indx == cfun->machine->base_reg)
1919         pointer = indx_ptr = literal_pool = true;
1920     }
1921
1922   /* Prefer to use pointer as base, not index.  */
1923   if (base && indx && !base_ptr
1924       && (indx_ptr || (!REG_POINTER (base) && REG_POINTER (indx))))
1925     {
1926       rtx tmp = base;
1927       base = indx;
1928       indx = tmp;
1929     }
1930
1931   /* Validate displacement.  */
1932   if (!disp)
1933     {
1934       /* If virtual registers are involved, the displacement will change later
1935          anyway as the virtual registers get eliminated.  This could make a
1936          valid displacement invalid, but it is more likely to make an invalid
1937          displacement valid, because we sometimes access the register save area
1938          via negative offsets to one of those registers.
1939          Thus we don't check the displacement for validity here.  If after
1940          elimination the displacement turns out to be invalid after all,
1941          this is fixed up by reload in any case.  */
1942       if (base != arg_pointer_rtx
1943           && indx != arg_pointer_rtx
1944           && base != return_address_pointer_rtx
1945           && indx != return_address_pointer_rtx
1946           && base != frame_pointer_rtx
1947           && indx != frame_pointer_rtx
1948           && base != virtual_stack_vars_rtx
1949           && indx != virtual_stack_vars_rtx)
1950         if (!DISP_IN_RANGE (offset))
1951           return false;
1952     }
1953   else
1954     {
1955       /* All the special cases are pointers.  */
1956       pointer = true;
1957
1958       /* In the small-PIC case, the linker converts @GOT
1959          and @GOTNTPOFF offsets to possible displacements.  */
1960       if (GET_CODE (disp) == UNSPEC
1961           && (XINT (disp, 1) == UNSPEC_GOT
1962               || XINT (disp, 1) == UNSPEC_GOTNTPOFF)
1963           && flag_pic == 1)
1964         {
1965           ;
1966         }
1967
1968       /* Accept pool label offsets.  */
1969       else if (GET_CODE (disp) == UNSPEC
1970                && XINT (disp, 1) == UNSPEC_POOL_OFFSET)
1971         ;
1972
1973       /* Accept literal pool references.  */
1974       else if (GET_CODE (disp) == UNSPEC
1975                && XINT (disp, 1) == UNSPEC_LTREL_OFFSET)
1976         {
1977           orig_disp = gen_rtx_CONST (Pmode, disp);
1978           if (offset)
1979             {
1980               /* If we have an offset, make sure it does not
1981                  exceed the size of the constant pool entry.  */
1982               rtx sym = XVECEXP (disp, 0, 0);
1983               if (offset >= GET_MODE_SIZE (get_pool_mode (sym)))
1984                 return false;
1985
1986               orig_disp = plus_constant (orig_disp, offset);
1987             }
1988         }
1989
1990       else
1991         return false;
1992     }
1993
1994   if (!base && !indx)
1995     pointer = true;
1996
1997   if (out)
1998     {
1999       out->base = base;
2000       out->indx = indx;
2001       out->disp = orig_disp;
2002       out->pointer = pointer;
2003       out->literal_pool = literal_pool;
2004     }
2005
2006   return true;
2007 }
2008
2009 /* Decompose a RTL expression OP for a shift count into its components,
2010    and return the base register in BASE and the offset in OFFSET.
2011
2012    Return true if OP is a valid shift count, false if not.  */
2013
2014 bool
2015 s390_decompose_shift_count (rtx op, rtx *base, HOST_WIDE_INT *offset)
2016 {
2017   HOST_WIDE_INT off = 0;
2018
2019   /* We can have an integer constant, an address register,
2020      or a sum of the two.  */
2021   if (GET_CODE (op) == CONST_INT)
2022     {
2023       off = INTVAL (op);
2024       op = NULL_RTX;
2025     }
2026   if (op && GET_CODE (op) == PLUS && GET_CODE (XEXP (op, 1)) == CONST_INT)
2027     {
2028       off = INTVAL (XEXP (op, 1));
2029       op = XEXP (op, 0);
2030     }
2031   while (op && GET_CODE (op) == SUBREG)
2032     op = SUBREG_REG (op);
2033
2034   if (op && GET_CODE (op) != REG)
2035     return false;
2036
2037   if (offset)
2038     *offset = off;
2039   if (base)
2040     *base = op;
2041
2042    return true;
2043 }
2044
2045
2046 /* Return true if CODE is a valid address without index.  */
2047
2048 bool
2049 s390_legitimate_address_without_index_p (rtx op)
2050 {
2051   struct s390_address addr;
2052
2053   if (!s390_decompose_address (XEXP (op, 0), &addr))
2054     return false;
2055   if (addr.indx)
2056     return false;
2057
2058   return true;
2059 }
2060
2061
2062 /* Return true if ADDR is of kind symbol_ref or symbol_ref + const_int
2063    and return these parts in SYMREF and ADDEND.  You can pass NULL in
2064    SYMREF and/or ADDEND if you are not interested in these values.  */
2065
2066 static bool
2067 s390_symref_operand_p (rtx addr, rtx *symref, HOST_WIDE_INT *addend)
2068 {
2069   HOST_WIDE_INT tmpaddend = 0;
2070
2071   if (GET_CODE (addr) == CONST)
2072     addr = XEXP (addr, 0);
2073
2074   if (GET_CODE (addr) == PLUS)
2075     {
2076       if (GET_CODE (XEXP (addr, 0)) == SYMBOL_REF
2077           && CONST_INT_P (XEXP (addr, 1)))
2078         {
2079           tmpaddend = INTVAL (XEXP (addr, 1));
2080           addr = XEXP (addr, 0);
2081         }
2082       else
2083         return false;
2084     }
2085   else
2086     if (GET_CODE (addr) != SYMBOL_REF)
2087         return false;
2088
2089   if (symref)
2090     *symref = addr;
2091   if (addend)
2092     *addend = tmpaddend;
2093
2094   return true;
2095 }
2096
2097
2098 /* Return true if the address in OP is valid for constraint letter C
2099    if wrapped in a MEM rtx.  Set LIT_POOL_OK to true if it literal
2100    pool MEMs should be accepted.  Only the Q, R, S, T constraint
2101    letters are allowed for C.  */
2102
2103 static int
2104 s390_check_qrst_address (char c, rtx op, bool lit_pool_ok)
2105 {
2106   struct s390_address addr;
2107   bool decomposed = false;
2108
2109   /* This check makes sure that no symbolic address (except literal
2110      pool references) are accepted by the R or T constraints.  */
2111   if (s390_symref_operand_p (op, NULL, NULL))
2112     {
2113       if (!lit_pool_ok)
2114         return 0;
2115       if (!s390_decompose_address (op, &addr))
2116         return 0;
2117       if (!addr.literal_pool)
2118         return 0;
2119       decomposed = true;
2120     }
2121
2122   switch (c)
2123     {
2124     case 'Q': /* no index short displacement */
2125       if (!decomposed && !s390_decompose_address (op, &addr))
2126         return 0;
2127       if (addr.indx)
2128         return 0;
2129       if (!s390_short_displacement (addr.disp))
2130         return 0;
2131       break;
2132
2133     case 'R': /* with index short displacement */
2134       if (TARGET_LONG_DISPLACEMENT)
2135         {
2136           if (!decomposed && !s390_decompose_address (op, &addr))
2137             return 0;
2138           if (!s390_short_displacement (addr.disp))
2139             return 0;
2140         }
2141       /* Any invalid address here will be fixed up by reload,
2142          so accept it for the most generic constraint.  */
2143       break;
2144
2145     case 'S': /* no index long displacement */
2146       if (!TARGET_LONG_DISPLACEMENT)
2147         return 0;
2148       if (!decomposed && !s390_decompose_address (op, &addr))
2149         return 0;
2150       if (addr.indx)
2151         return 0;
2152       if (s390_short_displacement (addr.disp))
2153         return 0;
2154       break;
2155
2156     case 'T': /* with index long displacement */
2157       if (!TARGET_LONG_DISPLACEMENT)
2158         return 0;
2159       /* Any invalid address here will be fixed up by reload,
2160          so accept it for the most generic constraint.  */
2161       if ((decomposed || s390_decompose_address (op, &addr))
2162           && s390_short_displacement (addr.disp))
2163         return 0;
2164       break;
2165     default:
2166       return 0;
2167     }
2168   return 1;
2169 }
2170
2171
2172 /* Evaluates constraint strings described by the regular expression
2173    ([A|B|Z](Q|R|S|T))|U|W|Y and returns 1 if OP is a valid operand for
2174    the constraint given in STR, or 0 else.  */
2175
2176 int
2177 s390_mem_constraint (const char *str, rtx op)
2178 {
2179   char c = str[0];
2180
2181   switch (c)
2182     {
2183     case 'A':
2184       /* Check for offsettable variants of memory constraints.  */
2185       if (!MEM_P (op) || MEM_VOLATILE_P (op))
2186         return 0;
2187       if ((reload_completed || reload_in_progress)
2188           ? !offsettable_memref_p (op) : !offsettable_nonstrict_memref_p (op))
2189         return 0;
2190       return s390_check_qrst_address (str[1], XEXP (op, 0), true);
2191     case 'B':
2192       /* Check for non-literal-pool variants of memory constraints.  */
2193       if (!MEM_P (op))
2194         return 0;
2195       return s390_check_qrst_address (str[1], XEXP (op, 0), false);
2196     case 'Q':
2197     case 'R':
2198     case 'S':
2199     case 'T':
2200       if (GET_CODE (op) != MEM)
2201         return 0;
2202       return s390_check_qrst_address (c, XEXP (op, 0), true);
2203     case 'U':
2204       return (s390_check_qrst_address ('Q', op, true)
2205               || s390_check_qrst_address ('R', op, true));
2206     case 'W':
2207       return (s390_check_qrst_address ('S', op, true)
2208               || s390_check_qrst_address ('T', op, true));
2209     case 'Y':
2210       /* Simply check for the basic form of a shift count.  Reload will
2211          take care of making sure we have a proper base register.  */
2212       if (!s390_decompose_shift_count (op, NULL, NULL))
2213         return 0;
2214       break;
2215     case 'Z':
2216       return s390_check_qrst_address (str[1], op, true);
2217     default:
2218       return 0;
2219     }
2220   return 1;
2221 }
2222
2223
2224 /* Evaluates constraint strings starting with letter O.  Input
2225    parameter C is the second letter following the "O" in the constraint
2226    string. Returns 1 if VALUE meets the respective constraint and 0
2227    otherwise.  */
2228
2229 int
2230 s390_O_constraint_str (const char c, HOST_WIDE_INT value)
2231 {
2232   if (!TARGET_EXTIMM)
2233     return 0;
2234
2235   switch (c)
2236     {
2237     case 's':
2238       return trunc_int_for_mode (value, SImode) == value;
2239
2240     case 'p':
2241       return value == 0
2242         || s390_single_part (GEN_INT (value), DImode, SImode, 0) == 1;
2243
2244     case 'n':
2245       return s390_single_part (GEN_INT (value - 1), DImode, SImode, -1) == 1;
2246
2247     default:
2248       gcc_unreachable ();
2249     }
2250 }
2251
2252
2253 /* Evaluates constraint strings starting with letter N.  Parameter STR
2254    contains the letters following letter "N" in the constraint string.
2255    Returns true if VALUE matches the constraint.  */
2256
2257 int
2258 s390_N_constraint_str (const char *str, HOST_WIDE_INT value)
2259 {
2260   enum machine_mode mode, part_mode;
2261   int def;
2262   int part, part_goal;
2263
2264
2265   if (str[0] == 'x')
2266     part_goal = -1;
2267   else
2268     part_goal = str[0] - '0';
2269
2270   switch (str[1])
2271     {
2272     case 'Q':
2273       part_mode = QImode;
2274       break;
2275     case 'H':
2276       part_mode = HImode;
2277       break;
2278     case 'S':
2279       part_mode = SImode;
2280       break;
2281     default:
2282       return 0;
2283     }
2284
2285   switch (str[2])
2286     {
2287     case 'H':
2288       mode = HImode;
2289       break;
2290     case 'S':
2291       mode = SImode;
2292       break;
2293     case 'D':
2294       mode = DImode;
2295       break;
2296     default:
2297       return 0;
2298     }
2299
2300   switch (str[3])
2301     {
2302     case '0':
2303       def = 0;
2304       break;
2305     case 'F':
2306       def = -1;
2307       break;
2308     default:
2309       return 0;
2310     }
2311
2312   if (GET_MODE_SIZE (mode) <= GET_MODE_SIZE (part_mode))
2313     return 0;
2314
2315   part = s390_single_part (GEN_INT (value), mode, part_mode, def);
2316   if (part < 0)
2317     return 0;
2318   if (part_goal != -1 && part_goal != part)
2319     return 0;
2320
2321   return 1;
2322 }
2323
2324
2325 /* Returns true if the input parameter VALUE is a float zero.  */
2326
2327 int
2328 s390_float_const_zero_p (rtx value)
2329 {
2330   return (GET_MODE_CLASS (GET_MODE (value)) == MODE_FLOAT
2331           && value == CONST0_RTX (GET_MODE (value)));
2332 }
2333
2334
2335 /* Compute a (partial) cost for rtx X.  Return true if the complete
2336    cost has been computed, and false if subexpressions should be
2337    scanned.  In either case, *TOTAL contains the cost result.
2338    CODE contains GET_CODE (x), OUTER_CODE contains the code
2339    of the superexpression of x.  */
2340
2341 static bool
2342 s390_rtx_costs (rtx x, int code, int outer_code, int *total,
2343                 bool speed ATTRIBUTE_UNUSED)
2344 {
2345   switch (code)
2346     {
2347     case CONST:
2348     case CONST_INT:
2349     case LABEL_REF:
2350     case SYMBOL_REF:
2351     case CONST_DOUBLE:
2352     case MEM:
2353       *total = 0;
2354       return true;
2355
2356     case ASHIFT:
2357     case ASHIFTRT:
2358     case LSHIFTRT:
2359     case ROTATE:
2360     case ROTATERT:
2361     case AND:
2362     case IOR:
2363     case XOR:
2364     case NEG:
2365     case NOT:
2366       *total = COSTS_N_INSNS (1);
2367       return false;
2368
2369     case PLUS:
2370     case MINUS:
2371       /* Check for multiply and add.  */
2372       if ((GET_MODE (x) == DFmode || GET_MODE (x) == SFmode)
2373           && GET_CODE (XEXP (x, 0)) == MULT
2374           && TARGET_HARD_FLOAT && TARGET_FUSED_MADD)
2375         {
2376           /* This is the multiply and add case.  */
2377           if (GET_MODE (x) == DFmode)
2378             *total = s390_cost->madbr;
2379           else
2380             *total = s390_cost->maebr;
2381           *total += (rtx_cost (XEXP (XEXP (x, 0), 0), MULT, speed)
2382                      + rtx_cost (XEXP (XEXP (x, 0), 1), MULT, speed)
2383                      + rtx_cost (XEXP (x, 1), (enum rtx_code) code, speed));
2384           return true;  /* Do not do an additional recursive descent.  */
2385         }
2386       *total = COSTS_N_INSNS (1);
2387       return false;
2388
2389     case MULT:
2390       switch (GET_MODE (x))
2391         {
2392         case SImode:
2393           {
2394             rtx left = XEXP (x, 0);
2395             rtx right = XEXP (x, 1);
2396             if (GET_CODE (right) == CONST_INT
2397                 && CONST_OK_FOR_K (INTVAL (right)))
2398               *total = s390_cost->mhi;
2399             else if (GET_CODE (left) == SIGN_EXTEND)
2400               *total = s390_cost->mh;
2401             else
2402               *total = s390_cost->ms;  /* msr, ms, msy */
2403             break;
2404           }
2405         case DImode:
2406           {
2407             rtx left = XEXP (x, 0);
2408             rtx right = XEXP (x, 1);
2409             if (TARGET_64BIT)
2410               {
2411                 if (GET_CODE (right) == CONST_INT
2412                     && CONST_OK_FOR_K (INTVAL (right)))
2413                   *total = s390_cost->mghi;
2414                 else if (GET_CODE (left) == SIGN_EXTEND)
2415                   *total = s390_cost->msgf;
2416                 else
2417                   *total = s390_cost->msg;  /* msgr, msg */
2418               }
2419             else /* TARGET_31BIT */
2420               {
2421                 if (GET_CODE (left) == SIGN_EXTEND
2422                     && GET_CODE (right) == SIGN_EXTEND)
2423                   /* mulsidi case: mr, m */
2424                   *total = s390_cost->m;
2425                 else if (GET_CODE (left) == ZERO_EXTEND
2426                          && GET_CODE (right) == ZERO_EXTEND
2427                          && TARGET_CPU_ZARCH)
2428                   /* umulsidi case: ml, mlr */
2429                   *total = s390_cost->ml;
2430                 else
2431                   /* Complex calculation is required.  */
2432                   *total = COSTS_N_INSNS (40);
2433               }
2434             break;
2435           }
2436         case SFmode:
2437         case DFmode:
2438           *total = s390_cost->mult_df;
2439           break;
2440         case TFmode:
2441           *total = s390_cost->mxbr;
2442           break;
2443         default:
2444           return false;
2445         }
2446       return false;
2447
2448     case UDIV:
2449     case UMOD:
2450       if (GET_MODE (x) == TImode)              /* 128 bit division */
2451         *total = s390_cost->dlgr;
2452       else if (GET_MODE (x) == DImode)
2453         {
2454           rtx right = XEXP (x, 1);
2455           if (GET_CODE (right) == ZERO_EXTEND) /* 64 by 32 bit division */
2456             *total = s390_cost->dlr;
2457           else                                 /* 64 by 64 bit division */
2458             *total = s390_cost->dlgr;
2459         }
2460       else if (GET_MODE (x) == SImode)         /* 32 bit division */
2461         *total = s390_cost->dlr;
2462       return false;
2463
2464     case DIV:
2465     case MOD:
2466       if (GET_MODE (x) == DImode)
2467         {
2468           rtx right = XEXP (x, 1);
2469           if (GET_CODE (right) == ZERO_EXTEND) /* 64 by 32 bit division */
2470             if (TARGET_64BIT)
2471               *total = s390_cost->dsgfr;
2472             else
2473               *total = s390_cost->dr;
2474           else                                 /* 64 by 64 bit division */
2475             *total = s390_cost->dsgr;
2476         }
2477       else if (GET_MODE (x) == SImode)         /* 32 bit division */
2478         *total = s390_cost->dlr;
2479       else if (GET_MODE (x) == SFmode)
2480         {
2481           *total = s390_cost->debr;
2482         }
2483       else if (GET_MODE (x) == DFmode)
2484         {
2485           *total = s390_cost->ddbr;
2486         }
2487       else if (GET_MODE (x) == TFmode)
2488         {
2489           *total = s390_cost->dxbr;
2490         }
2491       return false;
2492
2493     case SQRT:
2494       if (GET_MODE (x) == SFmode)
2495         *total = s390_cost->sqebr;
2496       else if (GET_MODE (x) == DFmode)
2497         *total = s390_cost->sqdbr;
2498       else /* TFmode */
2499         *total = s390_cost->sqxbr;
2500       return false;
2501
2502     case SIGN_EXTEND:
2503     case ZERO_EXTEND:
2504       if (outer_code == MULT || outer_code == DIV || outer_code == MOD
2505           || outer_code == PLUS || outer_code == MINUS
2506           || outer_code == COMPARE)
2507         *total = 0;
2508       return false;
2509
2510     case COMPARE:
2511       *total = COSTS_N_INSNS (1);
2512       if (GET_CODE (XEXP (x, 0)) == AND
2513           && GET_CODE (XEXP (x, 1)) == CONST_INT
2514           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
2515         {
2516           rtx op0 = XEXP (XEXP (x, 0), 0);
2517           rtx op1 = XEXP (XEXP (x, 0), 1);
2518           rtx op2 = XEXP (x, 1);
2519
2520           if (memory_operand (op0, GET_MODE (op0))
2521               && s390_tm_ccmode (op1, op2, 0) != VOIDmode)
2522             return true;
2523           if (register_operand (op0, GET_MODE (op0))
2524               && s390_tm_ccmode (op1, op2, 1) != VOIDmode)
2525             return true;
2526         }
2527       return false;
2528
2529     default:
2530       return false;
2531     }
2532 }
2533
2534 /* Return the cost of an address rtx ADDR.  */
2535
2536 static int
2537 s390_address_cost (rtx addr, bool speed ATTRIBUTE_UNUSED)
2538 {
2539   struct s390_address ad;
2540   if (!s390_decompose_address (addr, &ad))
2541     return 1000;
2542
2543   return ad.indx? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (1);
2544 }
2545
2546 /* If OP is a SYMBOL_REF of a thread-local symbol, return its TLS mode,
2547    otherwise return 0.  */
2548
2549 int
2550 tls_symbolic_operand (rtx op)
2551 {
2552   if (GET_CODE (op) != SYMBOL_REF)
2553     return 0;
2554   return SYMBOL_REF_TLS_MODEL (op);
2555 }
2556 \f
2557 /* Split DImode access register reference REG (on 64-bit) into its constituent
2558    low and high parts, and store them into LO and HI.  Note that gen_lowpart/
2559    gen_highpart cannot be used as they assume all registers are word-sized,
2560    while our access registers have only half that size.  */
2561
2562 void
2563 s390_split_access_reg (rtx reg, rtx *lo, rtx *hi)
2564 {
2565   gcc_assert (TARGET_64BIT);
2566   gcc_assert (ACCESS_REG_P (reg));
2567   gcc_assert (GET_MODE (reg) == DImode);
2568   gcc_assert (!(REGNO (reg) & 1));
2569
2570   *lo = gen_rtx_REG (SImode, REGNO (reg) + 1);
2571   *hi = gen_rtx_REG (SImode, REGNO (reg));
2572 }
2573
2574 /* Return true if OP contains a symbol reference */
2575
2576 bool
2577 symbolic_reference_mentioned_p (rtx op)
2578 {
2579   const char *fmt;
2580   int i;
2581
2582   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
2583     return 1;
2584
2585   fmt = GET_RTX_FORMAT (GET_CODE (op));
2586   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
2587     {
2588       if (fmt[i] == 'E')
2589         {
2590           int j;
2591
2592           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
2593             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
2594               return 1;
2595         }
2596
2597       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
2598         return 1;
2599     }
2600
2601   return 0;
2602 }
2603
2604 /* Return true if OP contains a reference to a thread-local symbol.  */
2605
2606 bool
2607 tls_symbolic_reference_mentioned_p (rtx op)
2608 {
2609   const char *fmt;
2610   int i;
2611
2612   if (GET_CODE (op) == SYMBOL_REF)
2613     return tls_symbolic_operand (op);
2614
2615   fmt = GET_RTX_FORMAT (GET_CODE (op));
2616   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
2617     {
2618       if (fmt[i] == 'E')
2619         {
2620           int j;
2621
2622           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
2623             if (tls_symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
2624               return true;
2625         }
2626
2627       else if (fmt[i] == 'e' && tls_symbolic_reference_mentioned_p (XEXP (op, i)))
2628         return true;
2629     }
2630
2631   return false;
2632 }
2633
2634
2635 /* Return true if OP is a legitimate general operand when
2636    generating PIC code.  It is given that flag_pic is on
2637    and that OP satisfies CONSTANT_P or is a CONST_DOUBLE.  */
2638
2639 int
2640 legitimate_pic_operand_p (rtx op)
2641 {
2642   /* Accept all non-symbolic constants.  */
2643   if (!SYMBOLIC_CONST (op))
2644     return 1;
2645
2646   /* Reject everything else; must be handled
2647      via emit_symbolic_move.  */
2648   return 0;
2649 }
2650
2651 /* Returns true if the constant value OP is a legitimate general operand.
2652    It is given that OP satisfies CONSTANT_P or is a CONST_DOUBLE.  */
2653
2654 int
2655 legitimate_constant_p (rtx op)
2656 {
2657   /* Accept all non-symbolic constants.  */
2658   if (!SYMBOLIC_CONST (op))
2659     return 1;
2660
2661   /* Accept immediate LARL operands.  */
2662   if (TARGET_CPU_ZARCH && larl_operand (op, VOIDmode))
2663     return 1;
2664
2665   /* Thread-local symbols are never legal constants.  This is
2666      so that emit_call knows that computing such addresses
2667      might require a function call.  */
2668   if (TLS_SYMBOLIC_CONST (op))
2669     return 0;
2670
2671   /* In the PIC case, symbolic constants must *not* be
2672      forced into the literal pool.  We accept them here,
2673      so that they will be handled by emit_symbolic_move.  */
2674   if (flag_pic)
2675     return 1;
2676
2677   /* All remaining non-PIC symbolic constants are
2678      forced into the literal pool.  */
2679   return 0;
2680 }
2681
2682 /* Determine if it's legal to put X into the constant pool.  This
2683    is not possible if X contains the address of a symbol that is
2684    not constant (TLS) or not known at final link time (PIC).  */
2685
2686 static bool
2687 s390_cannot_force_const_mem (rtx x)
2688 {
2689   switch (GET_CODE (x))
2690     {
2691     case CONST_INT:
2692     case CONST_DOUBLE:
2693       /* Accept all non-symbolic constants.  */
2694       return false;
2695
2696     case LABEL_REF:
2697       /* Labels are OK iff we are non-PIC.  */
2698       return flag_pic != 0;
2699
2700     case SYMBOL_REF:
2701       /* 'Naked' TLS symbol references are never OK,
2702          non-TLS symbols are OK iff we are non-PIC.  */
2703       if (tls_symbolic_operand (x))
2704         return true;
2705       else
2706         return flag_pic != 0;
2707
2708     case CONST:
2709       return s390_cannot_force_const_mem (XEXP (x, 0));
2710     case PLUS:
2711     case MINUS:
2712       return s390_cannot_force_const_mem (XEXP (x, 0))
2713              || s390_cannot_force_const_mem (XEXP (x, 1));
2714
2715     case UNSPEC:
2716       switch (XINT (x, 1))
2717         {
2718         /* Only lt-relative or GOT-relative UNSPECs are OK.  */
2719         case UNSPEC_LTREL_OFFSET:
2720         case UNSPEC_GOT:
2721         case UNSPEC_GOTOFF:
2722         case UNSPEC_PLTOFF:
2723         case UNSPEC_TLSGD:
2724         case UNSPEC_TLSLDM:
2725         case UNSPEC_NTPOFF:
2726         case UNSPEC_DTPOFF:
2727         case UNSPEC_GOTNTPOFF:
2728         case UNSPEC_INDNTPOFF:
2729           return false;
2730
2731         /* If the literal pool shares the code section, be put
2732            execute template placeholders into the pool as well.  */
2733         case UNSPEC_INSN:
2734           return TARGET_CPU_ZARCH;
2735
2736         default:
2737           return true;
2738         }
2739       break;
2740
2741     default:
2742       gcc_unreachable ();
2743     }
2744 }
2745
2746 /* Returns true if the constant value OP is a legitimate general
2747    operand during and after reload.  The difference to
2748    legitimate_constant_p is that this function will not accept
2749    a constant that would need to be forced to the literal pool
2750    before it can be used as operand.  */
2751
2752 bool
2753 legitimate_reload_constant_p (rtx op)
2754 {
2755   /* Accept la(y) operands.  */
2756   if (GET_CODE (op) == CONST_INT
2757       && DISP_IN_RANGE (INTVAL (op)))
2758     return true;
2759
2760   /* Accept l(g)hi/l(g)fi operands.  */
2761   if (GET_CODE (op) == CONST_INT
2762       && (CONST_OK_FOR_K (INTVAL (op)) || CONST_OK_FOR_Os (INTVAL (op))))
2763     return true;
2764
2765   /* Accept lliXX operands.  */
2766   if (TARGET_ZARCH
2767       && GET_CODE (op) == CONST_INT
2768       && trunc_int_for_mode (INTVAL (op), word_mode) == INTVAL (op)
2769       && s390_single_part (op, word_mode, HImode, 0) >= 0)
2770   return true;
2771
2772   if (TARGET_EXTIMM
2773       && GET_CODE (op) == CONST_INT
2774       && trunc_int_for_mode (INTVAL (op), word_mode) == INTVAL (op)
2775       && s390_single_part (op, word_mode, SImode, 0) >= 0)
2776     return true;
2777
2778   /* Accept larl operands.  */
2779   if (TARGET_CPU_ZARCH
2780       && larl_operand (op, VOIDmode))
2781     return true;
2782
2783   /* Accept lzXX operands.  */
2784   if (GET_CODE (op) == CONST_DOUBLE
2785       && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, 'G', "G"))
2786     return true;
2787
2788   /* Accept double-word operands that can be split.  */
2789   if (GET_CODE (op) == CONST_INT
2790       && trunc_int_for_mode (INTVAL (op), word_mode) != INTVAL (op))
2791     {
2792       enum machine_mode dword_mode = word_mode == SImode ? DImode : TImode;
2793       rtx hi = operand_subword (op, 0, 0, dword_mode);
2794       rtx lo = operand_subword (op, 1, 0, dword_mode);
2795       return legitimate_reload_constant_p (hi)
2796              && legitimate_reload_constant_p (lo);
2797     }
2798
2799   /* Everything else cannot be handled without reload.  */
2800   return false;
2801 }
2802
2803 /* Given an rtx OP being reloaded into a reg required to be in class RCLASS,
2804    return the class of reg to actually use.  */
2805
2806 enum reg_class
2807 s390_preferred_reload_class (rtx op, enum reg_class rclass)
2808 {
2809   switch (GET_CODE (op))
2810     {
2811       /* Constants we cannot reload must be forced into the
2812          literal pool.  */
2813
2814       case CONST_DOUBLE:
2815       case CONST_INT:
2816         if (legitimate_reload_constant_p (op))
2817           return rclass;
2818         else
2819           return NO_REGS;
2820
2821       /* If a symbolic constant or a PLUS is reloaded,
2822          it is most likely being used as an address, so
2823          prefer ADDR_REGS.  If 'class' is not a superset
2824          of ADDR_REGS, e.g. FP_REGS, reject this reload.  */
2825       case PLUS:
2826       case LABEL_REF:
2827       case SYMBOL_REF:
2828       case CONST:
2829         if (reg_class_subset_p (ADDR_REGS, rclass))
2830           return ADDR_REGS;
2831         else
2832           return NO_REGS;
2833
2834       default:
2835         break;
2836     }
2837
2838   return rclass;
2839 }
2840
2841 /* Return true if ADDR is SYMBOL_REF + addend with addend being a
2842    multiple of ALIGNMENT and the SYMBOL_REF being naturally
2843    aligned.  */
2844
2845 bool
2846 s390_check_symref_alignment (rtx addr, HOST_WIDE_INT alignment)
2847 {
2848   HOST_WIDE_INT addend;
2849   rtx symref;
2850
2851   if (!s390_symref_operand_p (addr, &symref, &addend))
2852     return false;
2853
2854   return (!SYMBOL_REF_NOT_NATURALLY_ALIGNED_P (symref)
2855           && !(addend & (alignment - 1)));
2856 }
2857
2858 /* ADDR is moved into REG using larl.  If ADDR isn't a valid larl
2859    operand SCRATCH is used to reload the even part of the address and
2860    adding one.  */
2861
2862 void
2863 s390_reload_larl_operand (rtx reg, rtx addr, rtx scratch)
2864 {
2865   HOST_WIDE_INT addend;
2866   rtx symref;
2867
2868   if (!s390_symref_operand_p (addr, &symref, &addend))
2869     gcc_unreachable ();
2870
2871   if (!(addend & 1))
2872     /* Easy case.  The addend is even so larl will do fine.  */
2873     emit_move_insn (reg, addr);
2874   else
2875     {
2876       /* We can leave the scratch register untouched if the target
2877          register is a valid base register.  */
2878       if (REGNO (reg) < FIRST_PSEUDO_REGISTER
2879           && REGNO_REG_CLASS (REGNO (reg)) == ADDR_REGS)
2880         scratch = reg;
2881
2882       gcc_assert (REGNO (scratch) < FIRST_PSEUDO_REGISTER);
2883       gcc_assert (REGNO_REG_CLASS (REGNO (scratch)) == ADDR_REGS);
2884
2885       if (addend != 1)
2886         emit_move_insn (scratch,
2887                         gen_rtx_CONST (Pmode,
2888                                        gen_rtx_PLUS (Pmode, symref,
2889                                                      GEN_INT (addend - 1))));
2890       else
2891         emit_move_insn (scratch, symref);
2892
2893       /* Increment the address using la in order to avoid clobbering cc.  */
2894       emit_move_insn (reg, gen_rtx_PLUS (Pmode, scratch, const1_rtx));
2895     }
2896 }
2897
2898 /* Generate what is necessary to move between REG and MEM using
2899    SCRATCH.  The direction is given by TOMEM.  */
2900
2901 void
2902 s390_reload_symref_address (rtx reg, rtx mem, rtx scratch, bool tomem)
2903 {
2904   /* Reload might have pulled a constant out of the literal pool.
2905      Force it back in.  */
2906   if (CONST_INT_P (mem) || GET_CODE (mem) == CONST_DOUBLE
2907       || GET_CODE (mem) == CONST)
2908     mem = force_const_mem (GET_MODE (reg), mem);
2909
2910   gcc_assert (MEM_P (mem));
2911
2912   /* For a load from memory we can leave the scratch register
2913      untouched if the target register is a valid base register.  */
2914   if (!tomem
2915       && REGNO (reg) < FIRST_PSEUDO_REGISTER
2916       && REGNO_REG_CLASS (REGNO (reg)) == ADDR_REGS
2917       && GET_MODE (reg) == GET_MODE (scratch))
2918     scratch = reg;
2919
2920   /* Load address into scratch register.  Since we can't have a
2921      secondary reload for a secondary reload we have to cover the case
2922      where larl would need a secondary reload here as well.  */
2923   s390_reload_larl_operand (scratch, XEXP (mem, 0), scratch);
2924
2925   /* Now we can use a standard load/store to do the move.  */
2926   if (tomem)
2927     emit_move_insn (replace_equiv_address (mem, scratch), reg);
2928   else
2929     emit_move_insn (reg, replace_equiv_address (mem, scratch));
2930 }
2931
2932 /* Inform reload about cases where moving X with a mode MODE to a register in
2933    RCLASS requires an extra scratch or immediate register.  Return the class
2934    needed for the immediate register.  */
2935
2936 static enum reg_class
2937 s390_secondary_reload (bool in_p, rtx x, enum reg_class rclass,
2938                        enum machine_mode mode, secondary_reload_info *sri)
2939 {
2940   /* Intermediate register needed.  */
2941   if (reg_classes_intersect_p (CC_REGS, rclass))
2942     return GENERAL_REGS;
2943
2944   if (TARGET_Z10)
2945     {
2946       /* On z10 several optimizer steps may generate larl operands with
2947          an odd addend.  */
2948       if (in_p
2949           && s390_symref_operand_p (x, NULL, NULL)
2950           && mode == Pmode
2951           && !s390_check_symref_alignment (x, 2))
2952         sri->icode = ((mode == DImode) ? CODE_FOR_reloaddi_larl_odd_addend_z10
2953                       : CODE_FOR_reloadsi_larl_odd_addend_z10);
2954
2955       /* On z10 we need a scratch register when moving QI, TI or floating
2956          point mode values from or to a memory location with a SYMBOL_REF
2957          or if the symref addend of a SI or DI move is not aligned to the
2958          width of the access.  */
2959       if (MEM_P (x)
2960           && s390_symref_operand_p (XEXP (x, 0), NULL, NULL)
2961           && (mode == QImode || mode == TImode || FLOAT_MODE_P (mode)
2962               || (!TARGET_64BIT && mode == DImode)
2963               || ((mode == HImode || mode == SImode || mode == DImode)
2964                   && (!s390_check_symref_alignment (XEXP (x, 0),
2965                                                     GET_MODE_SIZE (mode))))))
2966         {
2967 #define __SECONDARY_RELOAD_CASE(M,m)                                    \
2968           case M##mode:                                                 \
2969             if (TARGET_64BIT)                                           \
2970               sri->icode = in_p ? CODE_FOR_reload##m##di_toreg_z10 :    \
2971                                   CODE_FOR_reload##m##di_tomem_z10;     \
2972             else                                                        \
2973               sri->icode = in_p ? CODE_FOR_reload##m##si_toreg_z10 :    \
2974                                   CODE_FOR_reload##m##si_tomem_z10;     \
2975           break;
2976
2977           switch (GET_MODE (x))
2978             {
2979               __SECONDARY_RELOAD_CASE (QI, qi);
2980               __SECONDARY_RELOAD_CASE (HI, hi);
2981               __SECONDARY_RELOAD_CASE (SI, si);
2982               __SECONDARY_RELOAD_CASE (DI, di);
2983               __SECONDARY_RELOAD_CASE (TI, ti);
2984               __SECONDARY_RELOAD_CASE (SF, sf);
2985               __SECONDARY_RELOAD_CASE (DF, df);
2986               __SECONDARY_RELOAD_CASE (TF, tf);
2987               __SECONDARY_RELOAD_CASE (SD, sd);
2988               __SECONDARY_RELOAD_CASE (DD, dd);
2989               __SECONDARY_RELOAD_CASE (TD, td);
2990
2991             default:
2992               gcc_unreachable ();
2993             }
2994 #undef __SECONDARY_RELOAD_CASE
2995         }
2996     }
2997
2998   /* We need a scratch register when loading a PLUS expression which
2999      is not a legitimate operand of the LOAD ADDRESS instruction.  */
3000   if (in_p && s390_plus_operand (x, mode))
3001     sri->icode = (TARGET_64BIT ?
3002                   CODE_FOR_reloaddi_plus : CODE_FOR_reloadsi_plus);
3003
3004   /* Performing a multiword move from or to memory we have to make sure the
3005      second chunk in memory is addressable without causing a displacement
3006      overflow.  If that would be the case we calculate the address in
3007      a scratch register.  */
3008   if (MEM_P (x)
3009       && GET_CODE (XEXP (x, 0)) == PLUS
3010       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3011       && !DISP_IN_RANGE (INTVAL (XEXP (XEXP (x, 0), 1))
3012                          + GET_MODE_SIZE (mode) - 1))
3013     {
3014       /* For GENERAL_REGS a displacement overflow is no problem if occurring
3015          in a s_operand address since we may fallback to lm/stm.  So we only
3016          have to care about overflows in the b+i+d case.  */
3017       if ((reg_classes_intersect_p (GENERAL_REGS, rclass)
3018            && s390_class_max_nregs (GENERAL_REGS, mode) > 1
3019            && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS)
3020           /* For FP_REGS no lm/stm is available so this check is triggered
3021              for displacement overflows in b+i+d and b+d like addresses.  */
3022           || (reg_classes_intersect_p (FP_REGS, rclass)
3023               && s390_class_max_nregs (FP_REGS, mode) > 1))
3024         {
3025           if (in_p)
3026             sri->icode = (TARGET_64BIT ?
3027                           CODE_FOR_reloaddi_nonoffmem_in :
3028                           CODE_FOR_reloadsi_nonoffmem_in);
3029           else
3030             sri->icode = (TARGET_64BIT ?
3031                           CODE_FOR_reloaddi_nonoffmem_out :
3032                           CODE_FOR_reloadsi_nonoffmem_out);
3033         }
3034     }
3035
3036   /* A scratch address register is needed when a symbolic constant is
3037      copied to r0 compiling with -fPIC.  In other cases the target
3038      register might be used as temporary (see legitimize_pic_address).  */
3039   if (in_p && SYMBOLIC_CONST (x) && flag_pic == 2 && rclass != ADDR_REGS)
3040     sri->icode = (TARGET_64BIT ?
3041                   CODE_FOR_reloaddi_PIC_addr :
3042                   CODE_FOR_reloadsi_PIC_addr);
3043
3044   /* Either scratch or no register needed.  */
3045   return NO_REGS;
3046 }
3047
3048 /* Generate code to load SRC, which is PLUS that is not a
3049    legitimate operand for the LA instruction, into TARGET.
3050    SCRATCH may be used as scratch register.  */
3051
3052 void
3053 s390_expand_plus_operand (rtx target, rtx src,
3054                           rtx scratch)
3055 {
3056   rtx sum1, sum2;
3057   struct s390_address ad;
3058
3059   /* src must be a PLUS; get its two operands.  */
3060   gcc_assert (GET_CODE (src) == PLUS);
3061   gcc_assert (GET_MODE (src) == Pmode);
3062
3063   /* Check if any of the two operands is already scheduled
3064      for replacement by reload.  This can happen e.g. when
3065      float registers occur in an address.  */
3066   sum1 = find_replacement (&XEXP (src, 0));
3067   sum2 = find_replacement (&XEXP (src, 1));
3068   src = gen_rtx_PLUS (Pmode, sum1, sum2);
3069
3070   /* If the address is already strictly valid, there's nothing to do.  */
3071   if (!s390_decompose_address (src, &ad)
3072       || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
3073       || (ad.indx && !REGNO_OK_FOR_INDEX_P (REGNO (ad.indx))))
3074     {
3075       /* Otherwise, one of the operands cannot be an address register;
3076          we reload its value into the scratch register.  */
3077       if (true_regnum (sum1) < 1 || true_regnum (sum1) > 15)
3078         {
3079           emit_move_insn (scratch, sum1);
3080           sum1 = scratch;
3081         }
3082       if (true_regnum (sum2) < 1 || true_regnum (sum2) > 15)
3083         {
3084           emit_move_insn (scratch, sum2);
3085           sum2 = scratch;
3086         }
3087
3088       /* According to the way these invalid addresses are generated
3089          in reload.c, it should never happen (at least on s390) that
3090          *neither* of the PLUS components, after find_replacements
3091          was applied, is an address register.  */
3092       if (sum1 == scratch && sum2 == scratch)
3093         {
3094           debug_rtx (src);
3095           gcc_unreachable ();
3096         }
3097
3098       src = gen_rtx_PLUS (Pmode, sum1, sum2);
3099     }
3100
3101   /* Emit the LOAD ADDRESS pattern.  Note that reload of PLUS
3102      is only ever performed on addresses, so we can mark the
3103      sum as legitimate for LA in any case.  */
3104   s390_load_address (target, src);
3105 }
3106
3107
3108 /* Return true if ADDR is a valid memory address.
3109    STRICT specifies whether strict register checking applies.  */
3110
3111 static bool
3112 s390_legitimate_address_p (enum machine_mode mode, rtx addr, bool strict)
3113 {
3114   struct s390_address ad;
3115
3116   if (TARGET_Z10
3117       && larl_operand (addr, VOIDmode)
3118       && (mode == VOIDmode
3119           || s390_check_symref_alignment (addr, GET_MODE_SIZE (mode))))
3120     return true;
3121
3122   if (!s390_decompose_address (addr, &ad))
3123     return false;
3124
3125   if (strict)
3126     {
3127       if (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
3128         return false;
3129
3130       if (ad.indx && !REGNO_OK_FOR_INDEX_P (REGNO (ad.indx)))
3131         return false;
3132     }
3133   else
3134     {
3135       if (ad.base
3136           && !(REGNO (ad.base) >= FIRST_PSEUDO_REGISTER
3137                || REGNO_REG_CLASS (REGNO (ad.base)) == ADDR_REGS))
3138         return false;
3139
3140       if (ad.indx
3141           && !(REGNO (ad.indx) >= FIRST_PSEUDO_REGISTER
3142                || REGNO_REG_CLASS (REGNO (ad.indx)) == ADDR_REGS))
3143           return false;
3144     }
3145   return true;
3146 }
3147
3148 /* Return true if OP is a valid operand for the LA instruction.
3149    In 31-bit, we need to prove that the result is used as an
3150    address, as LA performs only a 31-bit addition.  */
3151
3152 bool
3153 legitimate_la_operand_p (rtx op)
3154 {
3155   struct s390_address addr;
3156   if (!s390_decompose_address (op, &addr))
3157     return false;
3158
3159   return (TARGET_64BIT || addr.pointer);
3160 }
3161
3162 /* Return true if it is valid *and* preferable to use LA to
3163    compute the sum of OP1 and OP2.  */
3164
3165 bool
3166 preferred_la_operand_p (rtx op1, rtx op2)
3167 {
3168   struct s390_address addr;
3169
3170   if (op2 != const0_rtx)
3171     op1 = gen_rtx_PLUS (Pmode, op1, op2);
3172
3173   if (!s390_decompose_address (op1, &addr))
3174     return false;
3175   if (addr.base && !REGNO_OK_FOR_BASE_P (REGNO (addr.base)))
3176     return false;
3177   if (addr.indx && !REGNO_OK_FOR_INDEX_P (REGNO (addr.indx)))
3178     return false;
3179
3180   if (!TARGET_64BIT && !addr.pointer)
3181     return false;
3182
3183   if (addr.pointer)
3184     return true;
3185
3186   if ((addr.base && REG_P (addr.base) && REG_POINTER (addr.base))
3187       || (addr.indx && REG_P (addr.indx) && REG_POINTER (addr.indx)))
3188     return true;
3189
3190   return false;
3191 }
3192
3193 /* Emit a forced load-address operation to load SRC into DST.
3194    This will use the LOAD ADDRESS instruction even in situations
3195    where legitimate_la_operand_p (SRC) returns false.  */
3196
3197 void
3198 s390_load_address (rtx dst, rtx src)
3199 {
3200   if (TARGET_64BIT)
3201     emit_move_insn (dst, src);
3202   else
3203     emit_insn (gen_force_la_31 (dst, src));
3204 }
3205
3206 /* Return a legitimate reference for ORIG (an address) using the
3207    register REG.  If REG is 0, a new pseudo is generated.
3208
3209    There are two types of references that must be handled:
3210
3211    1. Global data references must load the address from the GOT, via
3212       the PIC reg.  An insn is emitted to do this load, and the reg is
3213       returned.
3214
3215    2. Static data references, constant pool addresses, and code labels
3216       compute the address as an offset from the GOT, whose base is in
3217       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
3218       differentiate them from global data objects.  The returned
3219       address is the PIC reg + an unspec constant.
3220
3221    TARGET_LEGITIMIZE_ADDRESS_P rejects symbolic references unless the PIC
3222    reg also appears in the address.  */
3223
3224 rtx
3225 legitimize_pic_address (rtx orig, rtx reg)
3226 {
3227   rtx addr = orig;
3228   rtx new_rtx = orig;
3229   rtx base;
3230
3231   gcc_assert (!TLS_SYMBOLIC_CONST (addr));
3232
3233   if (GET_CODE (addr) == LABEL_REF
3234       || (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (addr)))
3235     {
3236       /* This is a local symbol.  */
3237       if (TARGET_CPU_ZARCH && larl_operand (addr, VOIDmode))
3238         {
3239           /* Access local symbols PC-relative via LARL.
3240              This is the same as in the non-PIC case, so it is
3241              handled automatically ...  */
3242         }
3243       else
3244         {
3245           /* Access local symbols relative to the GOT.  */
3246
3247           rtx temp = reg? reg : gen_reg_rtx (Pmode);
3248
3249           if (reload_in_progress || reload_completed)
3250             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
3251
3252           addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
3253           addr = gen_rtx_CONST (Pmode, addr);
3254           addr = force_const_mem (Pmode, addr);
3255           emit_move_insn (temp, addr);
3256
3257           new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
3258           if (reg != 0)
3259             {
3260               s390_load_address (reg, new_rtx);
3261               new_rtx = reg;
3262             }
3263         }
3264     }
3265   else if (GET_CODE (addr) == SYMBOL_REF)
3266     {
3267       if (reg == 0)
3268         reg = gen_reg_rtx (Pmode);
3269
3270       if (flag_pic == 1)
3271         {
3272           /* Assume GOT offset < 4k.  This is handled the same way
3273              in both 31- and 64-bit code (@GOT).  */
3274
3275           if (reload_in_progress || reload_completed)
3276             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
3277
3278           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
3279           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
3280           new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
3281           new_rtx = gen_const_mem (Pmode, new_rtx);
3282           emit_move_insn (reg, new_rtx);
3283           new_rtx = reg;
3284         }
3285       else if (TARGET_CPU_ZARCH)
3286         {
3287           /* If the GOT offset might be >= 4k, we determine the position
3288              of the GOT entry via a PC-relative LARL (@GOTENT).  */
3289
3290           rtx temp = reg ? reg : gen_reg_rtx (Pmode);
3291
3292           gcc_assert (REGNO (temp) >= FIRST_PSEUDO_REGISTER
3293                       || REGNO_REG_CLASS (REGNO (temp)) == ADDR_REGS);
3294
3295           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTENT);
3296           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
3297           emit_move_insn (temp, new_rtx);
3298
3299           new_rtx = gen_const_mem (Pmode, temp);
3300           emit_move_insn (reg, new_rtx);
3301           new_rtx = reg;
3302         }
3303       else
3304         {
3305           /* If the GOT offset might be >= 4k, we have to load it
3306              from the literal pool (@GOT).  */
3307
3308           rtx temp = reg ? reg : gen_reg_rtx (Pmode);
3309
3310           gcc_assert (REGNO (temp) >= FIRST_PSEUDO_REGISTER
3311                       || REGNO_REG_CLASS (REGNO (temp)) == ADDR_REGS);
3312
3313           if (reload_in_progress || reload_completed)
3314             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
3315
3316           addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
3317           addr = gen_rtx_CONST (Pmode, addr);
3318           addr = force_const_mem (Pmode, addr);
3319           emit_move_insn (temp, addr);
3320
3321           new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
3322           new_rtx = gen_const_mem (Pmode, new_rtx);
3323           emit_move_insn (reg, new_rtx);
3324           new_rtx = reg;
3325         }
3326     }
3327   else
3328     {
3329       if (GET_CODE (addr) == CONST)
3330         {
3331           addr = XEXP (addr, 0);
3332           if (GET_CODE (addr) == UNSPEC)
3333             {
3334               gcc_assert (XVECLEN (addr, 0) == 1);
3335               switch (XINT (addr, 1))
3336                 {
3337                   /* If someone moved a GOT-relative UNSPEC
3338                      out of the literal pool, force them back in.  */
3339                   case UNSPEC_GOTOFF:
3340                   case UNSPEC_PLTOFF:
3341                     new_rtx = force_const_mem (Pmode, orig);
3342                     break;
3343
3344                   /* @GOT is OK as is if small.  */
3345                   case UNSPEC_GOT:
3346                     if (flag_pic == 2)
3347                       new_rtx = force_const_mem (Pmode, orig);
3348                     break;
3349
3350                   /* @GOTENT is OK as is.  */
3351                   case UNSPEC_GOTENT:
3352                     break;
3353
3354                   /* @PLT is OK as is on 64-bit, must be converted to
3355                      GOT-relative @PLTOFF on 31-bit.  */
3356                   case UNSPEC_PLT:
3357                     if (!TARGET_CPU_ZARCH)
3358                       {
3359                         rtx temp = reg? reg : gen_reg_rtx (Pmode);
3360
3361                         if (reload_in_progress || reload_completed)
3362                           df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
3363
3364                         addr = XVECEXP (addr, 0, 0);
3365                         addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr),
3366                                                UNSPEC_PLTOFF);
3367                         addr = gen_rtx_CONST (Pmode, addr);
3368                         addr = force_const_mem (Pmode, addr);
3369                         emit_move_insn (temp, addr);
3370
3371                         new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
3372                         if (reg != 0)
3373                           {
3374                             s390_load_address (reg, new_rtx);
3375                             new_rtx = reg;
3376                           }
3377                       }
3378                     break;
3379
3380                   /* Everything else cannot happen.  */
3381                   default:
3382                     gcc_unreachable ();
3383                 }
3384             }
3385           else
3386             gcc_assert (GET_CODE (addr) == PLUS);
3387         }
3388       if (GET_CODE (addr) == PLUS)
3389         {
3390           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
3391
3392           gcc_assert (!TLS_SYMBOLIC_CONST (op0));
3393           gcc_assert (!TLS_SYMBOLIC_CONST (op1));
3394
3395           /* Check first to see if this is a constant offset
3396              from a local symbol reference.  */
3397           if ((GET_CODE (op0) == LABEL_REF
3398                 || (GET_CODE (op0) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (op0)))
3399               && GET_CODE (op1) == CONST_INT)
3400             {
3401               if (TARGET_CPU_ZARCH
3402                   && larl_operand (op0, VOIDmode)
3403                   && INTVAL (op1) < (HOST_WIDE_INT)1 << 31
3404                   && INTVAL (op1) >= -((HOST_WIDE_INT)1 << 31))
3405                 {
3406                   if (INTVAL (op1) & 1)
3407                     {
3408                       /* LARL can't handle odd offsets, so emit a
3409                          pair of LARL and LA.  */
3410                       rtx temp = reg? reg : gen_reg_rtx (Pmode);
3411
3412                       if (!DISP_IN_RANGE (INTVAL (op1)))
3413                         {
3414                           HOST_WIDE_INT even = INTVAL (op1) - 1;
3415                           op0 = gen_rtx_PLUS (Pmode, op0, GEN_INT (even));
3416                           op0 = gen_rtx_CONST (Pmode, op0);
3417                           op1 = const1_rtx;
3418                         }
3419
3420                       emit_move_insn (temp, op0);
3421                       new_rtx = gen_rtx_PLUS (Pmode, temp, op1);
3422
3423                       if (reg != 0)
3424                         {
3425                           s390_load_address (reg, new_rtx);
3426                           new_rtx = reg;
3427                         }
3428                     }
3429                   else
3430                     {
3431                       /* If the offset is even, we can just use LARL.
3432                          This will happen automatically.  */
3433                     }
3434                 }
3435               else
3436                 {
3437                   /* Access local symbols relative to the GOT.  */
3438
3439                   rtx temp = reg? reg : gen_reg_rtx (Pmode);
3440
3441                   if (reload_in_progress || reload_completed)
3442                     df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
3443
3444                   addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
3445                                          UNSPEC_GOTOFF);
3446                   addr = gen_rtx_PLUS (Pmode, addr, op1);
3447                   addr = gen_rtx_CONST (Pmode, addr);
3448                   addr = force_const_mem (Pmode, addr);
3449                   emit_move_insn (temp, addr);
3450
3451                   new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
3452                   if (reg != 0)
3453                     {
3454                       s390_load_address (reg, new_rtx);
3455                       new_rtx = reg;
3456                     }
3457                 }
3458             }
3459
3460           /* Now, check whether it is a GOT relative symbol plus offset
3461              that was pulled out of the literal pool.  Force it back in.  */
3462
3463           else if (GET_CODE (op0) == UNSPEC
3464                    && GET_CODE (op1) == CONST_INT
3465                    && XINT (op0, 1) == UNSPEC_GOTOFF)
3466             {
3467               gcc_assert (XVECLEN (op0, 0) == 1);
3468
3469               new_rtx = force_const_mem (Pmode, orig);
3470             }
3471
3472           /* Otherwise, compute the sum.  */
3473           else
3474             {
3475               base = legitimize_pic_address (XEXP (addr, 0), reg);
3476               new_rtx  = legitimize_pic_address (XEXP (addr, 1),
3477                                              base == reg ? NULL_RTX : reg);
3478               if (GET_CODE (new_rtx) == CONST_INT)