Update copyright years.
[platform/upstream/gcc.git] / gcc / config / s390 / s390.c
1 /* Subroutines used for code generation on IBM S/390 and zSeries
2    Copyright (C) 1999-2017 Free Software Foundation, Inc.
3    Contributed by Hartmut Penner (hpenner@de.ibm.com) and
4                   Ulrich Weigand (uweigand@de.ibm.com) and
5                   Andreas Krebbel (Andreas.Krebbel@de.ibm.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "target.h"
28 #include "target-globals.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "gimple.h"
32 #include "cfghooks.h"
33 #include "cfgloop.h"
34 #include "df.h"
35 #include "memmodel.h"
36 #include "tm_p.h"
37 #include "stringpool.h"
38 #include "expmed.h"
39 #include "optabs.h"
40 #include "regs.h"
41 #include "emit-rtl.h"
42 #include "recog.h"
43 #include "cgraph.h"
44 #include "diagnostic-core.h"
45 #include "diagnostic.h"
46 #include "alias.h"
47 #include "fold-const.h"
48 #include "print-tree.h"
49 #include "stor-layout.h"
50 #include "varasm.h"
51 #include "calls.h"
52 #include "conditions.h"
53 #include "output.h"
54 #include "insn-attr.h"
55 #include "flags.h"
56 #include "except.h"
57 #include "dojump.h"
58 #include "explow.h"
59 #include "stmt.h"
60 #include "expr.h"
61 #include "reload.h"
62 #include "cfgrtl.h"
63 #include "cfganal.h"
64 #include "lcm.h"
65 #include "cfgbuild.h"
66 #include "cfgcleanup.h"
67 #include "debug.h"
68 #include "langhooks.h"
69 #include "internal-fn.h"
70 #include "gimple-fold.h"
71 #include "tree-eh.h"
72 #include "gimplify.h"
73 #include "params.h"
74 #include "opts.h"
75 #include "tree-pass.h"
76 #include "context.h"
77 #include "builtins.h"
78 #include "rtl-iter.h"
79 #include "intl.h"
80 #include "tm-constrs.h"
81
82 /* This file should be included last.  */
83 #include "target-def.h"
84
85 /* Remember the last target of s390_set_current_function.  */
86 static GTY(()) tree s390_previous_fndecl;
87
88 /* Define the specific costs for a given cpu.  */
89
90 struct processor_costs
91 {
92   /* multiplication */
93   const int m;        /* cost of an M instruction.  */
94   const int mghi;     /* cost of an MGHI instruction.  */
95   const int mh;       /* cost of an MH instruction.  */
96   const int mhi;      /* cost of an MHI instruction.  */
97   const int ml;       /* cost of an ML instruction.  */
98   const int mr;       /* cost of an MR instruction.  */
99   const int ms;       /* cost of an MS instruction.  */
100   const int msg;      /* cost of an MSG instruction.  */
101   const int msgf;     /* cost of an MSGF instruction.  */
102   const int msgfr;    /* cost of an MSGFR instruction.  */
103   const int msgr;     /* cost of an MSGR instruction.  */
104   const int msr;      /* cost of an MSR instruction.  */
105   const int mult_df;  /* cost of multiplication in DFmode.  */
106   const int mxbr;
107   /* square root */
108   const int sqxbr;    /* cost of square root in TFmode.  */
109   const int sqdbr;    /* cost of square root in DFmode.  */
110   const int sqebr;    /* cost of square root in SFmode.  */
111   /* multiply and add */
112   const int madbr;    /* cost of multiply and add in DFmode.  */
113   const int maebr;    /* cost of multiply and add in SFmode.  */
114   /* division */
115   const int dxbr;
116   const int ddbr;
117   const int debr;
118   const int dlgr;
119   const int dlr;
120   const int dr;
121   const int dsgfr;
122   const int dsgr;
123 };
124
125 #define s390_cost ((const struct processor_costs *)(s390_cost_pointer))
126
127 static const
128 struct processor_costs z900_cost =
129 {
130   COSTS_N_INSNS (5),     /* M     */
131   COSTS_N_INSNS (10),    /* MGHI  */
132   COSTS_N_INSNS (5),     /* MH    */
133   COSTS_N_INSNS (4),     /* MHI   */
134   COSTS_N_INSNS (5),     /* ML    */
135   COSTS_N_INSNS (5),     /* MR    */
136   COSTS_N_INSNS (4),     /* MS    */
137   COSTS_N_INSNS (15),    /* MSG   */
138   COSTS_N_INSNS (7),     /* MSGF  */
139   COSTS_N_INSNS (7),     /* MSGFR */
140   COSTS_N_INSNS (10),    /* MSGR  */
141   COSTS_N_INSNS (4),     /* MSR   */
142   COSTS_N_INSNS (7),     /* multiplication in DFmode */
143   COSTS_N_INSNS (13),    /* MXBR */
144   COSTS_N_INSNS (136),   /* SQXBR */
145   COSTS_N_INSNS (44),    /* SQDBR */
146   COSTS_N_INSNS (35),    /* SQEBR */
147   COSTS_N_INSNS (18),    /* MADBR */
148   COSTS_N_INSNS (13),    /* MAEBR */
149   COSTS_N_INSNS (134),   /* DXBR */
150   COSTS_N_INSNS (30),    /* DDBR */
151   COSTS_N_INSNS (27),    /* DEBR */
152   COSTS_N_INSNS (220),   /* DLGR */
153   COSTS_N_INSNS (34),    /* DLR */
154   COSTS_N_INSNS (34),    /* DR */
155   COSTS_N_INSNS (32),    /* DSGFR */
156   COSTS_N_INSNS (32),    /* DSGR */
157 };
158
159 static const
160 struct processor_costs z990_cost =
161 {
162   COSTS_N_INSNS (4),     /* M     */
163   COSTS_N_INSNS (2),     /* MGHI  */
164   COSTS_N_INSNS (2),     /* MH    */
165   COSTS_N_INSNS (2),     /* MHI   */
166   COSTS_N_INSNS (4),     /* ML    */
167   COSTS_N_INSNS (4),     /* MR    */
168   COSTS_N_INSNS (5),     /* MS    */
169   COSTS_N_INSNS (6),     /* MSG   */
170   COSTS_N_INSNS (4),     /* MSGF  */
171   COSTS_N_INSNS (4),     /* MSGFR */
172   COSTS_N_INSNS (4),     /* MSGR  */
173   COSTS_N_INSNS (4),     /* MSR   */
174   COSTS_N_INSNS (1),     /* multiplication in DFmode */
175   COSTS_N_INSNS (28),    /* MXBR */
176   COSTS_N_INSNS (130),   /* SQXBR */
177   COSTS_N_INSNS (66),    /* SQDBR */
178   COSTS_N_INSNS (38),    /* SQEBR */
179   COSTS_N_INSNS (1),     /* MADBR */
180   COSTS_N_INSNS (1),     /* MAEBR */
181   COSTS_N_INSNS (60),    /* DXBR */
182   COSTS_N_INSNS (40),    /* DDBR */
183   COSTS_N_INSNS (26),    /* DEBR */
184   COSTS_N_INSNS (176),   /* DLGR */
185   COSTS_N_INSNS (31),    /* DLR */
186   COSTS_N_INSNS (31),    /* DR */
187   COSTS_N_INSNS (31),    /* DSGFR */
188   COSTS_N_INSNS (31),    /* DSGR */
189 };
190
191 static const
192 struct processor_costs z9_109_cost =
193 {
194   COSTS_N_INSNS (4),     /* M     */
195   COSTS_N_INSNS (2),     /* MGHI  */
196   COSTS_N_INSNS (2),     /* MH    */
197   COSTS_N_INSNS (2),     /* MHI   */
198   COSTS_N_INSNS (4),     /* ML    */
199   COSTS_N_INSNS (4),     /* MR    */
200   COSTS_N_INSNS (5),     /* MS    */
201   COSTS_N_INSNS (6),     /* MSG   */
202   COSTS_N_INSNS (4),     /* MSGF  */
203   COSTS_N_INSNS (4),     /* MSGFR */
204   COSTS_N_INSNS (4),     /* MSGR  */
205   COSTS_N_INSNS (4),     /* MSR   */
206   COSTS_N_INSNS (1),     /* multiplication in DFmode */
207   COSTS_N_INSNS (28),    /* MXBR */
208   COSTS_N_INSNS (130),   /* SQXBR */
209   COSTS_N_INSNS (66),    /* SQDBR */
210   COSTS_N_INSNS (38),    /* SQEBR */
211   COSTS_N_INSNS (1),     /* MADBR */
212   COSTS_N_INSNS (1),     /* MAEBR */
213   COSTS_N_INSNS (60),    /* DXBR */
214   COSTS_N_INSNS (40),    /* DDBR */
215   COSTS_N_INSNS (26),    /* DEBR */
216   COSTS_N_INSNS (30),    /* DLGR */
217   COSTS_N_INSNS (23),    /* DLR */
218   COSTS_N_INSNS (23),    /* DR */
219   COSTS_N_INSNS (24),    /* DSGFR */
220   COSTS_N_INSNS (24),    /* DSGR */
221 };
222
223 static const
224 struct processor_costs z10_cost =
225 {
226   COSTS_N_INSNS (10),    /* M     */
227   COSTS_N_INSNS (10),    /* MGHI  */
228   COSTS_N_INSNS (10),    /* MH    */
229   COSTS_N_INSNS (10),    /* MHI   */
230   COSTS_N_INSNS (10),    /* ML    */
231   COSTS_N_INSNS (10),    /* MR    */
232   COSTS_N_INSNS (10),    /* MS    */
233   COSTS_N_INSNS (10),    /* MSG   */
234   COSTS_N_INSNS (10),    /* MSGF  */
235   COSTS_N_INSNS (10),    /* MSGFR */
236   COSTS_N_INSNS (10),    /* MSGR  */
237   COSTS_N_INSNS (10),    /* MSR   */
238   COSTS_N_INSNS (1) ,    /* multiplication in DFmode */
239   COSTS_N_INSNS (50),    /* MXBR */
240   COSTS_N_INSNS (120),   /* SQXBR */
241   COSTS_N_INSNS (52),    /* SQDBR */
242   COSTS_N_INSNS (38),    /* SQEBR */
243   COSTS_N_INSNS (1),     /* MADBR */
244   COSTS_N_INSNS (1),     /* MAEBR */
245   COSTS_N_INSNS (111),   /* DXBR */
246   COSTS_N_INSNS (39),    /* DDBR */
247   COSTS_N_INSNS (32),    /* DEBR */
248   COSTS_N_INSNS (160),   /* DLGR */
249   COSTS_N_INSNS (71),    /* DLR */
250   COSTS_N_INSNS (71),    /* DR */
251   COSTS_N_INSNS (71),    /* DSGFR */
252   COSTS_N_INSNS (71),    /* DSGR */
253 };
254
255 static const
256 struct processor_costs z196_cost =
257 {
258   COSTS_N_INSNS (7),     /* M     */
259   COSTS_N_INSNS (5),     /* MGHI  */
260   COSTS_N_INSNS (5),     /* MH    */
261   COSTS_N_INSNS (5),     /* MHI   */
262   COSTS_N_INSNS (7),     /* ML    */
263   COSTS_N_INSNS (7),     /* MR    */
264   COSTS_N_INSNS (6),     /* MS    */
265   COSTS_N_INSNS (8),     /* MSG   */
266   COSTS_N_INSNS (6),     /* MSGF  */
267   COSTS_N_INSNS (6),     /* MSGFR */
268   COSTS_N_INSNS (8),     /* MSGR  */
269   COSTS_N_INSNS (6),     /* MSR   */
270   COSTS_N_INSNS (1) ,    /* multiplication in DFmode */
271   COSTS_N_INSNS (40),    /* MXBR B+40 */
272   COSTS_N_INSNS (100),   /* SQXBR B+100 */
273   COSTS_N_INSNS (42),    /* SQDBR B+42 */
274   COSTS_N_INSNS (28),    /* SQEBR B+28 */
275   COSTS_N_INSNS (1),     /* MADBR B */
276   COSTS_N_INSNS (1),     /* MAEBR B */
277   COSTS_N_INSNS (101),   /* DXBR B+101 */
278   COSTS_N_INSNS (29),    /* DDBR */
279   COSTS_N_INSNS (22),    /* DEBR */
280   COSTS_N_INSNS (160),   /* DLGR cracked */
281   COSTS_N_INSNS (160),   /* DLR cracked */
282   COSTS_N_INSNS (160),   /* DR expanded */
283   COSTS_N_INSNS (160),   /* DSGFR cracked */
284   COSTS_N_INSNS (160),   /* DSGR cracked */
285 };
286
287 static const
288 struct processor_costs zEC12_cost =
289 {
290   COSTS_N_INSNS (7),     /* M     */
291   COSTS_N_INSNS (5),     /* MGHI  */
292   COSTS_N_INSNS (5),     /* MH    */
293   COSTS_N_INSNS (5),     /* MHI   */
294   COSTS_N_INSNS (7),     /* ML    */
295   COSTS_N_INSNS (7),     /* MR    */
296   COSTS_N_INSNS (6),     /* MS    */
297   COSTS_N_INSNS (8),     /* MSG   */
298   COSTS_N_INSNS (6),     /* MSGF  */
299   COSTS_N_INSNS (6),     /* MSGFR */
300   COSTS_N_INSNS (8),     /* MSGR  */
301   COSTS_N_INSNS (6),     /* MSR   */
302   COSTS_N_INSNS (1) ,    /* multiplication in DFmode */
303   COSTS_N_INSNS (40),    /* MXBR B+40 */
304   COSTS_N_INSNS (100),   /* SQXBR B+100 */
305   COSTS_N_INSNS (42),    /* SQDBR B+42 */
306   COSTS_N_INSNS (28),    /* SQEBR B+28 */
307   COSTS_N_INSNS (1),     /* MADBR B */
308   COSTS_N_INSNS (1),     /* MAEBR B */
309   COSTS_N_INSNS (131),   /* DXBR B+131 */
310   COSTS_N_INSNS (29),    /* DDBR */
311   COSTS_N_INSNS (22),    /* DEBR */
312   COSTS_N_INSNS (160),   /* DLGR cracked */
313   COSTS_N_INSNS (160),   /* DLR cracked */
314   COSTS_N_INSNS (160),   /* DR expanded */
315   COSTS_N_INSNS (160),   /* DSGFR cracked */
316   COSTS_N_INSNS (160),   /* DSGR cracked */
317 };
318
319 static struct
320 {
321   const char *const name;
322   const enum processor_type processor;
323   const struct processor_costs *cost;
324 }
325 const processor_table[] =
326 {
327   { "g5",     PROCESSOR_9672_G5,     &z900_cost },
328   { "g6",     PROCESSOR_9672_G6,     &z900_cost },
329   { "z900",   PROCESSOR_2064_Z900,   &z900_cost },
330   { "z990",   PROCESSOR_2084_Z990,   &z990_cost },
331   { "z9-109", PROCESSOR_2094_Z9_109, &z9_109_cost },
332   { "z9-ec",  PROCESSOR_2094_Z9_EC,  &z9_109_cost },
333   { "z10",    PROCESSOR_2097_Z10,    &z10_cost },
334   { "z196",   PROCESSOR_2817_Z196,   &z196_cost },
335   { "zEC12",  PROCESSOR_2827_ZEC12,  &zEC12_cost },
336   { "z13",    PROCESSOR_2964_Z13,    &zEC12_cost },
337   { "native", PROCESSOR_NATIVE,      NULL }
338 };
339
340 extern int reload_completed;
341
342 /* Kept up to date using the SCHED_VARIABLE_ISSUE hook.  */
343 static rtx_insn *last_scheduled_insn;
344 #define MAX_SCHED_UNITS 3
345 static int last_scheduled_unit_distance[MAX_SCHED_UNITS];
346
347 /* The maximum score added for an instruction whose unit hasn't been
348    in use for MAX_SCHED_MIX_DISTANCE steps.  Increase this value to
349    give instruction mix scheduling more priority over instruction
350    grouping.  */
351 #define MAX_SCHED_MIX_SCORE      8
352
353 /* The maximum distance up to which individual scores will be
354    calculated.  Everything beyond this gives MAX_SCHED_MIX_SCORE.
355    Increase this with the OOO windows size of the machine.  */
356 #define MAX_SCHED_MIX_DISTANCE 100
357
358 /* Structure used to hold the components of a S/390 memory
359    address.  A legitimate address on S/390 is of the general
360    form
361           base + index + displacement
362    where any of the components is optional.
363
364    base and index are registers of the class ADDR_REGS,
365    displacement is an unsigned 12-bit immediate constant.  */
366
367 struct s390_address
368 {
369   rtx base;
370   rtx indx;
371   rtx disp;
372   bool pointer;
373   bool literal_pool;
374 };
375
376 /* The following structure is embedded in the machine
377    specific part of struct function.  */
378
379 struct GTY (()) s390_frame_layout
380 {
381   /* Offset within stack frame.  */
382   HOST_WIDE_INT gprs_offset;
383   HOST_WIDE_INT f0_offset;
384   HOST_WIDE_INT f4_offset;
385   HOST_WIDE_INT f8_offset;
386   HOST_WIDE_INT backchain_offset;
387
388   /* Number of first and last gpr where slots in the register
389      save area are reserved for.  */
390   int first_save_gpr_slot;
391   int last_save_gpr_slot;
392
393   /* Location (FP register number) where GPRs (r0-r15) should
394      be saved to.
395       0 - does not need to be saved at all
396      -1 - stack slot  */
397 #define SAVE_SLOT_NONE   0
398 #define SAVE_SLOT_STACK -1
399   signed char gpr_save_slots[16];
400
401   /* Number of first and last gpr to be saved, restored.  */
402   int first_save_gpr;
403   int first_restore_gpr;
404   int last_save_gpr;
405   int last_restore_gpr;
406
407   /* Bits standing for floating point registers. Set, if the
408      respective register has to be saved. Starting with reg 16 (f0)
409      at the rightmost bit.
410      Bit 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
411      fpr 15 13 11  9 14 12 10  8  7  5  3  1  6  4  2  0
412      reg 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16  */
413   unsigned int fpr_bitmap;
414
415   /* Number of floating point registers f8-f15 which must be saved.  */
416   int high_fprs;
417
418   /* Set if return address needs to be saved.
419      This flag is set by s390_return_addr_rtx if it could not use
420      the initial value of r14 and therefore depends on r14 saved
421      to the stack.  */
422   bool save_return_addr_p;
423
424   /* Size of stack frame.  */
425   HOST_WIDE_INT frame_size;
426 };
427
428 /* Define the structure for the machine field in struct function.  */
429
430 struct GTY(()) machine_function
431 {
432   struct s390_frame_layout frame_layout;
433
434   /* Literal pool base register.  */
435   rtx base_reg;
436
437   /* True if we may need to perform branch splitting.  */
438   bool split_branches_pending_p;
439
440   bool has_landing_pad_p;
441
442   /* True if the current function may contain a tbegin clobbering
443      FPRs.  */
444   bool tbegin_p;
445
446   /* For -fsplit-stack support: A stack local which holds a pointer to
447      the stack arguments for a function with a variable number of
448      arguments.  This is set at the start of the function and is used
449      to initialize the overflow_arg_area field of the va_list
450      structure.  */
451   rtx split_stack_varargs_pointer;
452 };
453
454 /* Few accessor macros for struct cfun->machine->s390_frame_layout.  */
455
456 #define cfun_frame_layout (cfun->machine->frame_layout)
457 #define cfun_save_high_fprs_p (!!cfun_frame_layout.high_fprs)
458 #define cfun_save_arg_fprs_p (!!(TARGET_64BIT                           \
459                                  ? cfun_frame_layout.fpr_bitmap & 0x0f  \
460                                  : cfun_frame_layout.fpr_bitmap & 0x03))
461 #define cfun_gprs_save_area_size ((cfun_frame_layout.last_save_gpr_slot - \
462   cfun_frame_layout.first_save_gpr_slot + 1) * UNITS_PER_LONG)
463 #define cfun_set_fpr_save(REGNO) (cfun->machine->frame_layout.fpr_bitmap |=    \
464   (1 << (REGNO - FPR0_REGNUM)))
465 #define cfun_fpr_save_p(REGNO) (!!(cfun->machine->frame_layout.fpr_bitmap &    \
466   (1 << (REGNO - FPR0_REGNUM))))
467 #define cfun_gpr_save_slot(REGNO) \
468   cfun->machine->frame_layout.gpr_save_slots[REGNO]
469
470 /* Number of GPRs and FPRs used for argument passing.  */
471 #define GP_ARG_NUM_REG 5
472 #define FP_ARG_NUM_REG (TARGET_64BIT? 4 : 2)
473 #define VEC_ARG_NUM_REG 8
474
475 /* A couple of shortcuts.  */
476 #define CONST_OK_FOR_J(x) \
477         CONST_OK_FOR_CONSTRAINT_P((x), 'J', "J")
478 #define CONST_OK_FOR_K(x) \
479         CONST_OK_FOR_CONSTRAINT_P((x), 'K', "K")
480 #define CONST_OK_FOR_Os(x) \
481         CONST_OK_FOR_CONSTRAINT_P((x), 'O', "Os")
482 #define CONST_OK_FOR_Op(x) \
483         CONST_OK_FOR_CONSTRAINT_P((x), 'O', "Op")
484 #define CONST_OK_FOR_On(x) \
485         CONST_OK_FOR_CONSTRAINT_P((x), 'O', "On")
486
487 #define REGNO_PAIR_OK(REGNO, MODE)                               \
488   (HARD_REGNO_NREGS ((REGNO), (MODE)) == 1 || !((REGNO) & 1))
489
490 /* That's the read ahead of the dynamic branch prediction unit in
491    bytes on a z10 (or higher) CPU.  */
492 #define PREDICT_DISTANCE (TARGET_Z10 ? 384 : 2048)
493
494
495 /* Indicate which ABI has been used for passing vector args.
496    0 - no vector type arguments have been passed where the ABI is relevant
497    1 - the old ABI has been used
498    2 - a vector type argument has been passed either in a vector register
499        or on the stack by value  */
500 static int s390_vector_abi = 0;
501
502 /* Set the vector ABI marker if TYPE is subject to the vector ABI
503    switch.  The vector ABI affects only vector data types.  There are
504    two aspects of the vector ABI relevant here:
505
506    1. vectors >= 16 bytes have an alignment of 8 bytes with the new
507    ABI and natural alignment with the old.
508
509    2. vector <= 16 bytes are passed in VRs or by value on the stack
510    with the new ABI but by reference on the stack with the old.
511
512    If ARG_P is true TYPE is used for a function argument or return
513    value.  The ABI marker then is set for all vector data types.  If
514    ARG_P is false only type 1 vectors are being checked.  */
515
516 static void
517 s390_check_type_for_vector_abi (const_tree type, bool arg_p, bool in_struct_p)
518 {
519   static hash_set<const_tree> visited_types_hash;
520
521   if (s390_vector_abi)
522     return;
523
524   if (type == NULL_TREE || TREE_CODE (type) == ERROR_MARK)
525     return;
526
527   if (visited_types_hash.contains (type))
528     return;
529
530   visited_types_hash.add (type);
531
532   if (VECTOR_TYPE_P (type))
533     {
534       int type_size = int_size_in_bytes (type);
535
536       /* Outside arguments only the alignment is changing and this
537          only happens for vector types >= 16 bytes.  */
538       if (!arg_p && type_size < 16)
539         return;
540
541       /* In arguments vector types > 16 are passed as before (GCC
542          never enforced the bigger alignment for arguments which was
543          required by the old vector ABI).  However, it might still be
544          ABI relevant due to the changed alignment if it is a struct
545          member.  */
546       if (arg_p && type_size > 16 && !in_struct_p)
547         return;
548
549       s390_vector_abi = TARGET_VX_ABI ? 2 : 1;
550     }
551   else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
552     {
553       /* ARRAY_TYPE: Since with neither of the ABIs we have more than
554          natural alignment there will never be ABI dependent padding
555          in an array type.  That's why we do not set in_struct_p to
556          true here.  */
557       s390_check_type_for_vector_abi (TREE_TYPE (type), arg_p, in_struct_p);
558     }
559   else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
560     {
561       tree arg_chain;
562
563       /* Check the return type.  */
564       s390_check_type_for_vector_abi (TREE_TYPE (type), true, false);
565
566       for (arg_chain = TYPE_ARG_TYPES (type);
567            arg_chain;
568            arg_chain = TREE_CHAIN (arg_chain))
569         s390_check_type_for_vector_abi (TREE_VALUE (arg_chain), true, false);
570     }
571   else if (RECORD_OR_UNION_TYPE_P (type))
572     {
573       tree field;
574
575       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
576         {
577           if (TREE_CODE (field) != FIELD_DECL)
578             continue;
579
580           s390_check_type_for_vector_abi (TREE_TYPE (field), arg_p, true);
581         }
582     }
583 }
584
585
586 /* System z builtins.  */
587
588 #include "s390-builtins.h"
589
590 const unsigned int bflags_builtin[S390_BUILTIN_MAX + 1] =
591   {
592 #undef B_DEF
593 #undef OB_DEF
594 #undef OB_DEF_VAR
595 #define B_DEF(NAME, PATTERN, ATTRS, BFLAGS, ...) BFLAGS,
596 #define OB_DEF(...)
597 #define OB_DEF_VAR(...)
598 #include "s390-builtins.def"
599     0
600   };
601
602 const unsigned int opflags_builtin[S390_BUILTIN_MAX + 1] =
603   {
604 #undef B_DEF
605 #undef OB_DEF
606 #undef OB_DEF_VAR
607 #define B_DEF(NAME, PATTERN, ATTRS, BFLAGS, OPFLAGS, ...) OPFLAGS,
608 #define OB_DEF(...)
609 #define OB_DEF_VAR(...)
610 #include "s390-builtins.def"
611     0
612   };
613
614 const unsigned int bflags_overloaded_builtin[S390_OVERLOADED_BUILTIN_MAX + 1] =
615   {
616 #undef B_DEF
617 #undef OB_DEF
618 #undef OB_DEF_VAR
619 #define B_DEF(...)
620 #define OB_DEF(NAME, FIRST_VAR_NAME, LAST_VAR_NAME, BFLAGS, ...) BFLAGS,
621 #define OB_DEF_VAR(...)
622 #include "s390-builtins.def"
623     0
624   };
625
626 const unsigned int
627 opflags_overloaded_builtin_var[S390_OVERLOADED_BUILTIN_VAR_MAX + 1] =
628   {
629 #undef B_DEF
630 #undef OB_DEF
631 #undef OB_DEF_VAR
632 #define B_DEF(...)
633 #define OB_DEF(...)
634 #define OB_DEF_VAR(NAME, PATTERN, FLAGS, FNTYPE) FLAGS,
635 #include "s390-builtins.def"
636     0
637   };
638
639 tree s390_builtin_types[BT_MAX];
640 tree s390_builtin_fn_types[BT_FN_MAX];
641 tree s390_builtin_decls[S390_BUILTIN_MAX +
642                         S390_OVERLOADED_BUILTIN_MAX +
643                         S390_OVERLOADED_BUILTIN_VAR_MAX];
644
645 static enum insn_code const code_for_builtin[S390_BUILTIN_MAX + 1] = {
646 #undef B_DEF
647 #undef OB_DEF
648 #undef OB_DEF_VAR
649 #define B_DEF(NAME, PATTERN, ...) CODE_FOR_##PATTERN,
650 #define OB_DEF(...)
651 #define OB_DEF_VAR(...)
652
653 #include "s390-builtins.def"
654   CODE_FOR_nothing
655 };
656
657 static void
658 s390_init_builtins (void)
659 {
660   /* These definitions are being used in s390-builtins.def.  */
661   tree returns_twice_attr = tree_cons (get_identifier ("returns_twice"),
662                                        NULL, NULL);
663   tree noreturn_attr = tree_cons (get_identifier ("noreturn"), NULL, NULL);
664   tree c_uint64_type_node;
665
666   /* The uint64_type_node from tree.c is not compatible to the C99
667      uint64_t data type.  What we want is c_uint64_type_node from
668      c-common.c.  But since backend code is not supposed to interface
669      with the frontend we recreate it here.  */
670   if (TARGET_64BIT)
671     c_uint64_type_node = long_unsigned_type_node;
672   else
673     c_uint64_type_node = long_long_unsigned_type_node;
674
675 #undef DEF_TYPE
676 #define DEF_TYPE(INDEX, BFLAGS, NODE, CONST_P)          \
677   if (s390_builtin_types[INDEX] == NULL)                \
678     s390_builtin_types[INDEX] = (!CONST_P) ?            \
679       (NODE) : build_type_variant ((NODE), 1, 0);
680
681 #undef DEF_POINTER_TYPE
682 #define DEF_POINTER_TYPE(INDEX, BFLAGS, INDEX_BASE)                     \
683   if (s390_builtin_types[INDEX] == NULL)                                \
684     s390_builtin_types[INDEX] =                                         \
685       build_pointer_type (s390_builtin_types[INDEX_BASE]);
686
687 #undef DEF_DISTINCT_TYPE
688 #define DEF_DISTINCT_TYPE(INDEX, BFLAGS, INDEX_BASE)                    \
689   if (s390_builtin_types[INDEX] == NULL)                                \
690     s390_builtin_types[INDEX] =                                         \
691       build_distinct_type_copy (s390_builtin_types[INDEX_BASE]);
692
693 #undef DEF_VECTOR_TYPE
694 #define DEF_VECTOR_TYPE(INDEX, BFLAGS, INDEX_BASE, ELEMENTS)            \
695   if (s390_builtin_types[INDEX] == NULL)                                \
696     s390_builtin_types[INDEX] =                                         \
697       build_vector_type (s390_builtin_types[INDEX_BASE], ELEMENTS);
698
699 #undef DEF_OPAQUE_VECTOR_TYPE
700 #define DEF_OPAQUE_VECTOR_TYPE(INDEX, BFLAGS, INDEX_BASE, ELEMENTS)     \
701   if (s390_builtin_types[INDEX] == NULL)                                \
702     s390_builtin_types[INDEX] =                                         \
703       build_opaque_vector_type (s390_builtin_types[INDEX_BASE], ELEMENTS);
704
705 #undef DEF_FN_TYPE
706 #define DEF_FN_TYPE(INDEX, BFLAGS, args...)                     \
707   if (s390_builtin_fn_types[INDEX] == NULL)                     \
708     s390_builtin_fn_types[INDEX] =                              \
709       build_function_type_list (args, NULL_TREE);
710 #undef DEF_OV_TYPE
711 #define DEF_OV_TYPE(...)
712 #include "s390-builtin-types.def"
713
714 #undef B_DEF
715 #define B_DEF(NAME, PATTERN, ATTRS, BFLAGS, OPFLAGS, FNTYPE)            \
716   if (s390_builtin_decls[S390_BUILTIN_##NAME] == NULL)                  \
717     s390_builtin_decls[S390_BUILTIN_##NAME] =                           \
718       add_builtin_function ("__builtin_" #NAME,                         \
719                             s390_builtin_fn_types[FNTYPE],              \
720                             S390_BUILTIN_##NAME,                        \
721                             BUILT_IN_MD,                                \
722                             NULL,                                       \
723                             ATTRS);
724 #undef OB_DEF
725 #define OB_DEF(NAME, FIRST_VAR_NAME, LAST_VAR_NAME, BFLAGS, FNTYPE)     \
726   if (s390_builtin_decls[S390_OVERLOADED_BUILTIN_##NAME + S390_BUILTIN_MAX] \
727       == NULL)                                                          \
728     s390_builtin_decls[S390_OVERLOADED_BUILTIN_##NAME + S390_BUILTIN_MAX] = \
729       add_builtin_function ("__builtin_" #NAME,                         \
730                             s390_builtin_fn_types[FNTYPE],              \
731                             S390_OVERLOADED_BUILTIN_##NAME + S390_BUILTIN_MAX, \
732                             BUILT_IN_MD,                                \
733                             NULL,                                       \
734                             0);
735 #undef OB_DEF_VAR
736 #define OB_DEF_VAR(...)
737 #include "s390-builtins.def"
738
739 }
740
741 /* Return true if ARG is appropriate as argument number ARGNUM of
742    builtin DECL.  The operand flags from s390-builtins.def have to
743    passed as OP_FLAGS.  */
744 bool
745 s390_const_operand_ok (tree arg, int argnum, int op_flags, tree decl)
746 {
747   if (O_UIMM_P (op_flags))
748     {
749       int bitwidths[] = { 1, 2, 3, 4, 5, 8, 12, 16, 32 };
750       int bitwidth = bitwidths[op_flags - O_U1];
751
752       if (!tree_fits_uhwi_p (arg)
753           || tree_to_uhwi (arg) > ((unsigned HOST_WIDE_INT)1 << bitwidth) - 1)
754         {
755           error("constant argument %d for builtin %qF is out of range (0.."
756                 HOST_WIDE_INT_PRINT_UNSIGNED ")",
757                 argnum, decl,
758                 ((unsigned HOST_WIDE_INT)1 << bitwidth) - 1);
759           return false;
760         }
761     }
762
763   if (O_SIMM_P (op_flags))
764     {
765       int bitwidths[] = { 2, 3, 4, 5, 8, 12, 16, 32 };
766       int bitwidth = bitwidths[op_flags - O_S2];
767
768       if (!tree_fits_shwi_p (arg)
769           || tree_to_shwi (arg) < -((HOST_WIDE_INT)1 << (bitwidth - 1))
770           || tree_to_shwi (arg) > (((HOST_WIDE_INT)1 << (bitwidth - 1)) - 1))
771         {
772           error("constant argument %d for builtin %qF is out of range ("
773                 HOST_WIDE_INT_PRINT_DEC ".."
774                 HOST_WIDE_INT_PRINT_DEC ")",
775                 argnum, decl,
776                 -((HOST_WIDE_INT)1 << (bitwidth - 1)),
777                 ((HOST_WIDE_INT)1 << (bitwidth - 1)) - 1);
778           return false;
779         }
780     }
781   return true;
782 }
783
784 /* Expand an expression EXP that calls a built-in function,
785    with result going to TARGET if that's convenient
786    (and in mode MODE if that's convenient).
787    SUBTARGET may be used as the target for computing one of EXP's operands.
788    IGNORE is nonzero if the value is to be ignored.  */
789
790 static rtx
791 s390_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
792                      machine_mode mode ATTRIBUTE_UNUSED,
793                      int ignore ATTRIBUTE_UNUSED)
794 {
795 #define MAX_ARGS 6
796
797   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
798   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
799   enum insn_code icode;
800   rtx op[MAX_ARGS], pat;
801   int arity;
802   bool nonvoid;
803   tree arg;
804   call_expr_arg_iterator iter;
805   unsigned int all_op_flags = opflags_for_builtin (fcode);
806   machine_mode last_vec_mode = VOIDmode;
807
808   if (TARGET_DEBUG_ARG)
809     {
810       fprintf (stderr,
811                "s390_expand_builtin, code = %4d, %s, bflags = 0x%x\n",
812                (int)fcode, IDENTIFIER_POINTER (DECL_NAME (fndecl)),
813                bflags_for_builtin (fcode));
814     }
815
816   if (S390_USE_TARGET_ATTRIBUTE)
817     {
818       unsigned int bflags;
819
820       bflags = bflags_for_builtin (fcode);
821       if ((bflags & B_HTM) && !TARGET_HTM)
822         {
823           error ("Builtin %qF is not supported without -mhtm "
824                  "(default with -march=zEC12 and higher).", fndecl);
825           return const0_rtx;
826         }
827       if ((bflags & B_VX) && !TARGET_VX)
828         {
829           error ("Builtin %qF is not supported without -mvx "
830                  "(default with -march=z13 and higher).", fndecl);
831           return const0_rtx;
832         }
833     }
834   if (fcode >= S390_OVERLOADED_BUILTIN_VAR_OFFSET
835       && fcode < S390_ALL_BUILTIN_MAX)
836     {
837       gcc_unreachable ();
838     }
839   else if (fcode < S390_OVERLOADED_BUILTIN_OFFSET)
840     {
841       icode = code_for_builtin[fcode];
842       /* Set a flag in the machine specific cfun part in order to support
843          saving/restoring of FPRs.  */
844       if (fcode == S390_BUILTIN_tbegin || fcode == S390_BUILTIN_tbegin_retry)
845         cfun->machine->tbegin_p = true;
846     }
847   else if (fcode < S390_OVERLOADED_BUILTIN_VAR_OFFSET)
848     {
849       error ("Unresolved overloaded builtin");
850       return const0_rtx;
851     }
852   else
853     internal_error ("bad builtin fcode");
854
855   if (icode == 0)
856     internal_error ("bad builtin icode");
857
858   nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
859
860   if (nonvoid)
861     {
862       machine_mode tmode = insn_data[icode].operand[0].mode;
863       if (!target
864           || GET_MODE (target) != tmode
865           || !(*insn_data[icode].operand[0].predicate) (target, tmode))
866         target = gen_reg_rtx (tmode);
867
868       /* There are builtins (e.g. vec_promote) with no vector
869          arguments but an element selector.  So we have to also look
870          at the vector return type when emitting the modulo
871          operation.  */
872       if (VECTOR_MODE_P (insn_data[icode].operand[0].mode))
873         last_vec_mode = insn_data[icode].operand[0].mode;
874     }
875
876   arity = 0;
877   FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
878     {
879       rtx tmp_rtx;
880       const struct insn_operand_data *insn_op;
881       unsigned int op_flags = all_op_flags & ((1 << O_SHIFT) - 1);
882
883       all_op_flags = all_op_flags >> O_SHIFT;
884
885       if (arg == error_mark_node)
886         return NULL_RTX;
887       if (arity >= MAX_ARGS)
888         return NULL_RTX;
889
890       if (O_IMM_P (op_flags)
891           && TREE_CODE (arg) != INTEGER_CST)
892         {
893           error ("constant value required for builtin %qF argument %d",
894                  fndecl, arity + 1);
895           return const0_rtx;
896         }
897
898       if (!s390_const_operand_ok (arg, arity + 1, op_flags, fndecl))
899         return const0_rtx;
900
901       insn_op = &insn_data[icode].operand[arity + nonvoid];
902       op[arity] = expand_expr (arg, NULL_RTX, insn_op->mode, EXPAND_NORMAL);
903
904       /* expand_expr truncates constants to the target mode only if it
905          is "convenient".  However, our checks below rely on this
906          being done.  */
907       if (CONST_INT_P (op[arity])
908           && SCALAR_INT_MODE_P (insn_op->mode)
909           && GET_MODE (op[arity]) != insn_op->mode)
910         op[arity] = GEN_INT (trunc_int_for_mode (INTVAL (op[arity]),
911                                                  insn_op->mode));
912
913       /* Wrap the expanded RTX for pointer types into a MEM expr with
914          the proper mode.  This allows us to use e.g. (match_operand
915          "memory_operand"..) in the insn patterns instead of (mem
916          (match_operand "address_operand)).  This is helpful for
917          patterns not just accepting MEMs.  */
918       if (POINTER_TYPE_P (TREE_TYPE (arg))
919           && insn_op->predicate != address_operand)
920         op[arity] = gen_rtx_MEM (insn_op->mode, op[arity]);
921
922       /* Expand the module operation required on element selectors.  */
923       if (op_flags == O_ELEM)
924         {
925           gcc_assert (last_vec_mode != VOIDmode);
926           op[arity] = simplify_expand_binop (SImode, code_to_optab (AND),
927                                              op[arity],
928                                              GEN_INT (GET_MODE_NUNITS (last_vec_mode) - 1),
929                                              NULL_RTX, 1, OPTAB_DIRECT);
930         }
931
932       /* Record the vector mode used for an element selector.  This assumes:
933          1. There is no builtin with two different vector modes and an element selector
934          2. The element selector comes after the vector type it is referring to.
935          This currently the true for all the builtins but FIXME we
936          should better check for that.  */
937       if (VECTOR_MODE_P (insn_op->mode))
938         last_vec_mode = insn_op->mode;
939
940       if (insn_op->predicate (op[arity], insn_op->mode))
941         {
942           arity++;
943           continue;
944         }
945
946       if (MEM_P (op[arity])
947           && insn_op->predicate == memory_operand
948           && (GET_MODE (XEXP (op[arity], 0)) == Pmode
949               || GET_MODE (XEXP (op[arity], 0)) == VOIDmode))
950         {
951           op[arity] = replace_equiv_address (op[arity],
952                                              copy_to_mode_reg (Pmode,
953                                                XEXP (op[arity], 0)));
954         }
955       /* Some of the builtins require different modes/types than the
956          pattern in order to implement a specific API.  Instead of
957          adding many expanders which do the mode change we do it here.
958          E.g. s390_vec_add_u128 required to have vector unsigned char
959          arguments is mapped to addti3.  */
960       else if (insn_op->mode != VOIDmode
961                && GET_MODE (op[arity]) != VOIDmode
962                && GET_MODE (op[arity]) != insn_op->mode
963                && ((tmp_rtx = simplify_gen_subreg (insn_op->mode, op[arity],
964                                                    GET_MODE (op[arity]), 0))
965                    != NULL_RTX))
966         {
967           op[arity] = tmp_rtx;
968         }
969       else if (GET_MODE (op[arity]) == insn_op->mode
970                || GET_MODE (op[arity]) == VOIDmode
971                || (insn_op->predicate == address_operand
972                    && GET_MODE (op[arity]) == Pmode))
973         {
974           /* An address_operand usually has VOIDmode in the expander
975              so we cannot use this.  */
976           machine_mode target_mode =
977             (insn_op->predicate == address_operand
978              ? Pmode : insn_op->mode);
979           op[arity] = copy_to_mode_reg (target_mode, op[arity]);
980         }
981
982       if (!insn_op->predicate (op[arity], insn_op->mode))
983         {
984           error ("Invalid argument %d for builtin %qF", arity + 1, fndecl);
985           return const0_rtx;
986         }
987       arity++;
988     }
989
990   switch (arity)
991     {
992     case 0:
993       pat = GEN_FCN (icode) (target);
994       break;
995     case 1:
996       if (nonvoid)
997         pat = GEN_FCN (icode) (target, op[0]);
998       else
999         pat = GEN_FCN (icode) (op[0]);
1000       break;
1001     case 2:
1002       if (nonvoid)
1003         pat = GEN_FCN (icode) (target, op[0], op[1]);
1004       else
1005         pat = GEN_FCN (icode) (op[0], op[1]);
1006       break;
1007     case 3:
1008       if (nonvoid)
1009         pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
1010       else
1011         pat = GEN_FCN (icode) (op[0], op[1], op[2]);
1012       break;
1013     case 4:
1014       if (nonvoid)
1015         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
1016       else
1017         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
1018       break;
1019     case 5:
1020       if (nonvoid)
1021         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
1022       else
1023         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
1024       break;
1025     case 6:
1026       if (nonvoid)
1027         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4], op[5]);
1028       else
1029         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4], op[5]);
1030       break;
1031     default:
1032       gcc_unreachable ();
1033     }
1034   if (!pat)
1035     return NULL_RTX;
1036   emit_insn (pat);
1037
1038   if (nonvoid)
1039     return target;
1040   else
1041     return const0_rtx;
1042 }
1043
1044
1045 static const int s390_hotpatch_hw_max = 1000000;
1046 static int s390_hotpatch_hw_before_label = 0;
1047 static int s390_hotpatch_hw_after_label = 0;
1048
1049 /* Check whether the hotpatch attribute is applied to a function and, if it has
1050    an argument, the argument is valid.  */
1051
1052 static tree
1053 s390_handle_hotpatch_attribute (tree *node, tree name, tree args,
1054                                 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1055 {
1056   tree expr;
1057   tree expr2;
1058   int err;
1059
1060   if (TREE_CODE (*node) != FUNCTION_DECL)
1061     {
1062       warning (OPT_Wattributes, "%qE attribute only applies to functions",
1063                name);
1064       *no_add_attrs = true;
1065     }
1066   if (args != NULL && TREE_CHAIN (args) != NULL)
1067     {
1068       expr = TREE_VALUE (args);
1069       expr2 = TREE_VALUE (TREE_CHAIN (args));
1070     }
1071   if (args == NULL || TREE_CHAIN (args) == NULL)
1072     err = 1;
1073   else if (TREE_CODE (expr) != INTEGER_CST
1074            || !INTEGRAL_TYPE_P (TREE_TYPE (expr))
1075            || wi::gtu_p (expr, s390_hotpatch_hw_max))
1076     err = 1;
1077   else if (TREE_CODE (expr2) != INTEGER_CST
1078            || !INTEGRAL_TYPE_P (TREE_TYPE (expr2))
1079            || wi::gtu_p (expr2, s390_hotpatch_hw_max))
1080     err = 1;
1081   else
1082     err = 0;
1083   if (err)
1084     {
1085       error ("requested %qE attribute is not a comma separated pair of"
1086              " non-negative integer constants or too large (max. %d)", name,
1087              s390_hotpatch_hw_max);
1088       *no_add_attrs = true;
1089     }
1090
1091   return NULL_TREE;
1092 }
1093
1094 /* Expand the s390_vector_bool type attribute.  */
1095
1096 static tree
1097 s390_handle_vectorbool_attribute (tree *node, tree name ATTRIBUTE_UNUSED,
1098                                   tree args ATTRIBUTE_UNUSED,
1099                                   int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1100 {
1101   tree type = *node, result = NULL_TREE;
1102   machine_mode mode;
1103
1104   while (POINTER_TYPE_P (type)
1105          || TREE_CODE (type) == FUNCTION_TYPE
1106          || TREE_CODE (type) == METHOD_TYPE
1107          || TREE_CODE (type) == ARRAY_TYPE)
1108     type = TREE_TYPE (type);
1109
1110   mode = TYPE_MODE (type);
1111   switch (mode)
1112     {
1113     case DImode: case V2DImode: result = s390_builtin_types[BT_BV2DI]; break;
1114     case SImode: case V4SImode: result = s390_builtin_types[BT_BV4SI]; break;
1115     case HImode: case V8HImode: result = s390_builtin_types[BT_BV8HI]; break;
1116     case QImode: case V16QImode: result = s390_builtin_types[BT_BV16QI];
1117     default: break;
1118     }
1119
1120   *no_add_attrs = true;  /* No need to hang on to the attribute.  */
1121
1122   if (result)
1123     *node = lang_hooks.types.reconstruct_complex_type (*node, result);
1124
1125   return NULL_TREE;
1126 }
1127
1128 static const struct attribute_spec s390_attribute_table[] = {
1129   { "hotpatch", 2, 2, true, false, false, s390_handle_hotpatch_attribute, false },
1130   { "s390_vector_bool", 0, 0, false, true, false, s390_handle_vectorbool_attribute, true },
1131   /* End element.  */
1132   { NULL,        0, 0, false, false, false, NULL, false }
1133 };
1134
1135 /* Return the alignment for LABEL.  We default to the -falign-labels
1136    value except for the literal pool base label.  */
1137 int
1138 s390_label_align (rtx_insn *label)
1139 {
1140   rtx_insn *prev_insn = prev_active_insn (label);
1141   rtx set, src;
1142
1143   if (prev_insn == NULL_RTX)
1144     goto old;
1145
1146   set = single_set (prev_insn);
1147
1148   if (set == NULL_RTX)
1149     goto old;
1150
1151   src = SET_SRC (set);
1152
1153   /* Don't align literal pool base labels.  */
1154   if (GET_CODE (src) == UNSPEC
1155       && XINT (src, 1) == UNSPEC_MAIN_BASE)
1156     return 0;
1157
1158  old:
1159   return align_labels_log;
1160 }
1161
1162 static machine_mode
1163 s390_libgcc_cmp_return_mode (void)
1164 {
1165   return TARGET_64BIT ? DImode : SImode;
1166 }
1167
1168 static machine_mode
1169 s390_libgcc_shift_count_mode (void)
1170 {
1171   return TARGET_64BIT ? DImode : SImode;
1172 }
1173
1174 static machine_mode
1175 s390_unwind_word_mode (void)
1176 {
1177   return TARGET_64BIT ? DImode : SImode;
1178 }
1179
1180 /* Return true if the back end supports mode MODE.  */
1181 static bool
1182 s390_scalar_mode_supported_p (machine_mode mode)
1183 {
1184   /* In contrast to the default implementation reject TImode constants on 31bit
1185      TARGET_ZARCH for ABI compliance.  */
1186   if (!TARGET_64BIT && TARGET_ZARCH && mode == TImode)
1187     return false;
1188
1189   if (DECIMAL_FLOAT_MODE_P (mode))
1190     return default_decimal_float_supported_p ();
1191
1192   return default_scalar_mode_supported_p (mode);
1193 }
1194
1195 /* Return true if the back end supports vector mode MODE.  */
1196 static bool
1197 s390_vector_mode_supported_p (machine_mode mode)
1198 {
1199   machine_mode inner;
1200
1201   if (!VECTOR_MODE_P (mode)
1202       || !TARGET_VX
1203       || GET_MODE_SIZE (mode) > 16)
1204     return false;
1205
1206   inner = GET_MODE_INNER (mode);
1207
1208   switch (inner)
1209     {
1210     case QImode:
1211     case HImode:
1212     case SImode:
1213     case DImode:
1214     case TImode:
1215     case SFmode:
1216     case DFmode:
1217     case TFmode:
1218       return true;
1219     default:
1220       return false;
1221     }
1222 }
1223
1224 /* Set the has_landing_pad_p flag in struct machine_function to VALUE.  */
1225
1226 void
1227 s390_set_has_landing_pad_p (bool value)
1228 {
1229   cfun->machine->has_landing_pad_p = value;
1230 }
1231
1232 /* If two condition code modes are compatible, return a condition code
1233    mode which is compatible with both.  Otherwise, return
1234    VOIDmode.  */
1235
1236 static machine_mode
1237 s390_cc_modes_compatible (machine_mode m1, machine_mode m2)
1238 {
1239   if (m1 == m2)
1240     return m1;
1241
1242   switch (m1)
1243     {
1244     case CCZmode:
1245       if (m2 == CCUmode || m2 == CCTmode || m2 == CCZ1mode
1246           || m2 == CCSmode || m2 == CCSRmode || m2 == CCURmode)
1247         return m2;
1248       return VOIDmode;
1249
1250     case CCSmode:
1251     case CCUmode:
1252     case CCTmode:
1253     case CCSRmode:
1254     case CCURmode:
1255     case CCZ1mode:
1256       if (m2 == CCZmode)
1257         return m1;
1258
1259       return VOIDmode;
1260
1261     default:
1262       return VOIDmode;
1263     }
1264   return VOIDmode;
1265 }
1266
1267 /* Return true if SET either doesn't set the CC register, or else
1268    the source and destination have matching CC modes and that
1269    CC mode is at least as constrained as REQ_MODE.  */
1270
1271 static bool
1272 s390_match_ccmode_set (rtx set, machine_mode req_mode)
1273 {
1274   machine_mode set_mode;
1275
1276   gcc_assert (GET_CODE (set) == SET);
1277
1278   /* These modes are supposed to be used only in CC consumer
1279      patterns.  */
1280   gcc_assert (req_mode != CCVIALLmode && req_mode != CCVIANYmode
1281               && req_mode != CCVFALLmode && req_mode != CCVFANYmode);
1282
1283   if (GET_CODE (SET_DEST (set)) != REG || !CC_REGNO_P (REGNO (SET_DEST (set))))
1284     return 1;
1285
1286   set_mode = GET_MODE (SET_DEST (set));
1287   switch (set_mode)
1288     {
1289     case CCSmode:
1290     case CCSRmode:
1291     case CCUmode:
1292     case CCURmode:
1293     case CCLmode:
1294     case CCL1mode:
1295     case CCL2mode:
1296     case CCL3mode:
1297     case CCT1mode:
1298     case CCT2mode:
1299     case CCT3mode:
1300     case CCVEQmode:
1301     case CCVIHmode:
1302     case CCVIHUmode:
1303     case CCVFHmode:
1304     case CCVFHEmode:
1305       if (req_mode != set_mode)
1306         return 0;
1307       break;
1308
1309     case CCZmode:
1310       if (req_mode != CCSmode && req_mode != CCUmode && req_mode != CCTmode
1311           && req_mode != CCSRmode && req_mode != CCURmode)
1312         return 0;
1313       break;
1314
1315     case CCAPmode:
1316     case CCANmode:
1317       if (req_mode != CCAmode)
1318         return 0;
1319       break;
1320
1321     default:
1322       gcc_unreachable ();
1323     }
1324
1325   return (GET_MODE (SET_SRC (set)) == set_mode);
1326 }
1327
1328 /* Return true if every SET in INSN that sets the CC register
1329    has source and destination with matching CC modes and that
1330    CC mode is at least as constrained as REQ_MODE.
1331    If REQ_MODE is VOIDmode, always return false.  */
1332
1333 bool
1334 s390_match_ccmode (rtx_insn *insn, machine_mode req_mode)
1335 {
1336   int i;
1337
1338   /* s390_tm_ccmode returns VOIDmode to indicate failure.  */
1339   if (req_mode == VOIDmode)
1340     return false;
1341
1342   if (GET_CODE (PATTERN (insn)) == SET)
1343     return s390_match_ccmode_set (PATTERN (insn), req_mode);
1344
1345   if (GET_CODE (PATTERN (insn)) == PARALLEL)
1346       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1347         {
1348           rtx set = XVECEXP (PATTERN (insn), 0, i);
1349           if (GET_CODE (set) == SET)
1350             if (!s390_match_ccmode_set (set, req_mode))
1351               return false;
1352         }
1353
1354   return true;
1355 }
1356
1357 /* If a test-under-mask instruction can be used to implement
1358    (compare (and ... OP1) OP2), return the CC mode required
1359    to do that.  Otherwise, return VOIDmode.
1360    MIXED is true if the instruction can distinguish between
1361    CC1 and CC2 for mixed selected bits (TMxx), it is false
1362    if the instruction cannot (TM).  */
1363
1364 machine_mode
1365 s390_tm_ccmode (rtx op1, rtx op2, bool mixed)
1366 {
1367   int bit0, bit1;
1368
1369   /* ??? Fixme: should work on CONST_WIDE_INT as well.  */
1370   if (GET_CODE (op1) != CONST_INT || GET_CODE (op2) != CONST_INT)
1371     return VOIDmode;
1372
1373   /* Selected bits all zero: CC0.
1374      e.g.: int a; if ((a & (16 + 128)) == 0) */
1375   if (INTVAL (op2) == 0)
1376     return CCTmode;
1377
1378   /* Selected bits all one: CC3.
1379      e.g.: int a; if ((a & (16 + 128)) == 16 + 128) */
1380   if (INTVAL (op2) == INTVAL (op1))
1381     return CCT3mode;
1382
1383   /* Exactly two bits selected, mixed zeroes and ones: CC1 or CC2. e.g.:
1384      int a;
1385      if ((a & (16 + 128)) == 16)         -> CCT1
1386      if ((a & (16 + 128)) == 128)        -> CCT2  */
1387   if (mixed)
1388     {
1389       bit1 = exact_log2 (INTVAL (op2));
1390       bit0 = exact_log2 (INTVAL (op1) ^ INTVAL (op2));
1391       if (bit0 != -1 && bit1 != -1)
1392         return bit0 > bit1 ? CCT1mode : CCT2mode;
1393     }
1394
1395   return VOIDmode;
1396 }
1397
1398 /* Given a comparison code OP (EQ, NE, etc.) and the operands
1399    OP0 and OP1 of a COMPARE, return the mode to be used for the
1400    comparison.  */
1401
1402 machine_mode
1403 s390_select_ccmode (enum rtx_code code, rtx op0, rtx op1)
1404 {
1405   if (TARGET_VX
1406       && register_operand (op0, DFmode)
1407       && register_operand (op1, DFmode))
1408     {
1409       /* LT, LE, UNGT, UNGE require swapping OP0 and OP1.  Either
1410          s390_emit_compare or s390_canonicalize_comparison will take
1411          care of it.  */
1412       switch (code)
1413         {
1414         case EQ:
1415         case NE:
1416           return CCVEQmode;
1417         case GT:
1418         case UNLE:
1419           return CCVFHmode;
1420         case GE:
1421         case UNLT:
1422           return CCVFHEmode;
1423         default:
1424           ;
1425         }
1426     }
1427
1428   switch (code)
1429     {
1430       case EQ:
1431       case NE:
1432         if ((GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
1433             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
1434           return CCAPmode;
1435         if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
1436             && CONST_OK_FOR_K (INTVAL (XEXP (op0, 1))))
1437           return CCAPmode;
1438         if ((GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
1439              || GET_CODE (op1) == NEG)
1440             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
1441           return CCLmode;
1442
1443         if (GET_CODE (op0) == AND)
1444           {
1445             /* Check whether we can potentially do it via TM.  */
1446             machine_mode ccmode;
1447             ccmode = s390_tm_ccmode (XEXP (op0, 1), op1, 1);
1448             if (ccmode != VOIDmode)
1449               {
1450                 /* Relax CCTmode to CCZmode to allow fall-back to AND
1451                    if that turns out to be beneficial.  */
1452                 return ccmode == CCTmode ? CCZmode : ccmode;
1453               }
1454           }
1455
1456         if (register_operand (op0, HImode)
1457             && GET_CODE (op1) == CONST_INT
1458             && (INTVAL (op1) == -1 || INTVAL (op1) == 65535))
1459           return CCT3mode;
1460         if (register_operand (op0, QImode)
1461             && GET_CODE (op1) == CONST_INT
1462             && (INTVAL (op1) == -1 || INTVAL (op1) == 255))
1463           return CCT3mode;
1464
1465         return CCZmode;
1466
1467       case LE:
1468       case LT:
1469       case GE:
1470       case GT:
1471         /* The only overflow condition of NEG and ABS happens when
1472            -INT_MAX is used as parameter, which stays negative. So
1473            we have an overflow from a positive value to a negative.
1474            Using CCAP mode the resulting cc can be used for comparisons.  */
1475         if ((GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
1476             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
1477           return CCAPmode;
1478
1479         /* If constants are involved in an add instruction it is possible to use
1480            the resulting cc for comparisons with zero. Knowing the sign of the
1481            constant the overflow behavior gets predictable. e.g.:
1482              int a, b; if ((b = a + c) > 0)
1483            with c as a constant value: c < 0 -> CCAN and c >= 0 -> CCAP  */
1484         if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
1485             && (CONST_OK_FOR_K (INTVAL (XEXP (op0, 1)))
1486                 || (CONST_OK_FOR_CONSTRAINT_P (INTVAL (XEXP (op0, 1)), 'O', "Os")
1487                     /* Avoid INT32_MIN on 32 bit.  */
1488                     && (!TARGET_ZARCH || INTVAL (XEXP (op0, 1)) != -0x7fffffff - 1))))
1489           {
1490             if (INTVAL (XEXP((op0), 1)) < 0)
1491               return CCANmode;
1492             else
1493               return CCAPmode;
1494           }
1495         /* Fall through.  */
1496       case UNORDERED:
1497       case ORDERED:
1498       case UNEQ:
1499       case UNLE:
1500       case UNLT:
1501       case UNGE:
1502       case UNGT:
1503       case LTGT:
1504         if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
1505             && GET_CODE (op1) != CONST_INT)
1506           return CCSRmode;
1507         return CCSmode;
1508
1509       case LTU:
1510       case GEU:
1511         if (GET_CODE (op0) == PLUS
1512             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
1513           return CCL1mode;
1514
1515         if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
1516             && GET_CODE (op1) != CONST_INT)
1517           return CCURmode;
1518         return CCUmode;
1519
1520       case LEU:
1521       case GTU:
1522         if (GET_CODE (op0) == MINUS
1523             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
1524           return CCL2mode;
1525
1526         if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
1527             && GET_CODE (op1) != CONST_INT)
1528           return CCURmode;
1529         return CCUmode;
1530
1531       default:
1532         gcc_unreachable ();
1533     }
1534 }
1535
1536 /* Replace the comparison OP0 CODE OP1 by a semantically equivalent one
1537    that we can implement more efficiently.  */
1538
1539 static void
1540 s390_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
1541                               bool op0_preserve_value)
1542 {
1543   if (op0_preserve_value)
1544     return;
1545
1546   /* Convert ZERO_EXTRACT back to AND to enable TM patterns.  */
1547   if ((*code == EQ || *code == NE)
1548       && *op1 == const0_rtx
1549       && GET_CODE (*op0) == ZERO_EXTRACT
1550       && GET_CODE (XEXP (*op0, 1)) == CONST_INT
1551       && GET_CODE (XEXP (*op0, 2)) == CONST_INT
1552       && SCALAR_INT_MODE_P (GET_MODE (XEXP (*op0, 0))))
1553     {
1554       rtx inner = XEXP (*op0, 0);
1555       HOST_WIDE_INT modesize = GET_MODE_BITSIZE (GET_MODE (inner));
1556       HOST_WIDE_INT len = INTVAL (XEXP (*op0, 1));
1557       HOST_WIDE_INT pos = INTVAL (XEXP (*op0, 2));
1558
1559       if (len > 0 && len < modesize
1560           && pos >= 0 && pos + len <= modesize
1561           && modesize <= HOST_BITS_PER_WIDE_INT)
1562         {
1563           unsigned HOST_WIDE_INT block;
1564           block = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
1565           block <<= modesize - pos - len;
1566
1567           *op0 = gen_rtx_AND (GET_MODE (inner), inner,
1568                               gen_int_mode (block, GET_MODE (inner)));
1569         }
1570     }
1571
1572   /* Narrow AND of memory against immediate to enable TM.  */
1573   if ((*code == EQ || *code == NE)
1574       && *op1 == const0_rtx
1575       && GET_CODE (*op0) == AND
1576       && GET_CODE (XEXP (*op0, 1)) == CONST_INT
1577       && SCALAR_INT_MODE_P (GET_MODE (XEXP (*op0, 0))))
1578     {
1579       rtx inner = XEXP (*op0, 0);
1580       rtx mask = XEXP (*op0, 1);
1581
1582       /* Ignore paradoxical SUBREGs if all extra bits are masked out.  */
1583       if (GET_CODE (inner) == SUBREG
1584           && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (inner)))
1585           && (GET_MODE_SIZE (GET_MODE (inner))
1586               >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner))))
1587           && ((INTVAL (mask)
1588                & GET_MODE_MASK (GET_MODE (inner))
1589                & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (inner))))
1590               == 0))
1591         inner = SUBREG_REG (inner);
1592
1593       /* Do not change volatile MEMs.  */
1594       if (MEM_P (inner) && !MEM_VOLATILE_P (inner))
1595         {
1596           int part = s390_single_part (XEXP (*op0, 1),
1597                                        GET_MODE (inner), QImode, 0);
1598           if (part >= 0)
1599             {
1600               mask = gen_int_mode (s390_extract_part (mask, QImode, 0), QImode);
1601               inner = adjust_address_nv (inner, QImode, part);
1602               *op0 = gen_rtx_AND (QImode, inner, mask);
1603             }
1604         }
1605     }
1606
1607   /* Narrow comparisons against 0xffff to HImode if possible.  */
1608   if ((*code == EQ || *code == NE)
1609       && GET_CODE (*op1) == CONST_INT
1610       && INTVAL (*op1) == 0xffff
1611       && SCALAR_INT_MODE_P (GET_MODE (*op0))
1612       && (nonzero_bits (*op0, GET_MODE (*op0))
1613           & ~(unsigned HOST_WIDE_INT) 0xffff) == 0)
1614     {
1615       *op0 = gen_lowpart (HImode, *op0);
1616       *op1 = constm1_rtx;
1617     }
1618
1619   /* Remove redundant UNSPEC_STRCMPCC_TO_INT conversions if possible.  */
1620   if (GET_CODE (*op0) == UNSPEC
1621       && XINT (*op0, 1) == UNSPEC_STRCMPCC_TO_INT
1622       && XVECLEN (*op0, 0) == 1
1623       && GET_MODE (XVECEXP (*op0, 0, 0)) == CCUmode
1624       && GET_CODE (XVECEXP (*op0, 0, 0)) == REG
1625       && REGNO (XVECEXP (*op0, 0, 0)) == CC_REGNUM
1626       && *op1 == const0_rtx)
1627     {
1628       enum rtx_code new_code = UNKNOWN;
1629       switch (*code)
1630         {
1631           case EQ: new_code = EQ;  break;
1632           case NE: new_code = NE;  break;
1633           case LT: new_code = GTU; break;
1634           case GT: new_code = LTU; break;
1635           case LE: new_code = GEU; break;
1636           case GE: new_code = LEU; break;
1637           default: break;
1638         }
1639
1640       if (new_code != UNKNOWN)
1641         {
1642           *op0 = XVECEXP (*op0, 0, 0);
1643           *code = new_code;
1644         }
1645     }
1646
1647   /* Remove redundant UNSPEC_CC_TO_INT conversions if possible.  */
1648   if (GET_CODE (*op0) == UNSPEC
1649       && XINT (*op0, 1) == UNSPEC_CC_TO_INT
1650       && XVECLEN (*op0, 0) == 1
1651       && GET_CODE (XVECEXP (*op0, 0, 0)) == REG
1652       && REGNO (XVECEXP (*op0, 0, 0)) == CC_REGNUM
1653       && CONST_INT_P (*op1))
1654     {
1655       enum rtx_code new_code = UNKNOWN;
1656       switch (GET_MODE (XVECEXP (*op0, 0, 0)))
1657         {
1658         case CCZmode:
1659         case CCRAWmode:
1660           switch (*code)
1661             {
1662             case EQ: new_code = EQ;  break;
1663             case NE: new_code = NE;  break;
1664             default: break;
1665             }
1666           break;
1667         default: break;
1668         }
1669
1670       if (new_code != UNKNOWN)
1671         {
1672           /* For CCRAWmode put the required cc mask into the second
1673              operand.  */
1674         if (GET_MODE (XVECEXP (*op0, 0, 0)) == CCRAWmode
1675             && INTVAL (*op1) >= 0 && INTVAL (*op1) <= 3)
1676             *op1 = gen_rtx_CONST_INT (VOIDmode, 1 << (3 - INTVAL (*op1)));
1677           *op0 = XVECEXP (*op0, 0, 0);
1678           *code = new_code;
1679         }
1680     }
1681
1682   /* Simplify cascaded EQ, NE with const0_rtx.  */
1683   if ((*code == NE || *code == EQ)
1684       && (GET_CODE (*op0) == EQ || GET_CODE (*op0) == NE)
1685       && GET_MODE (*op0) == SImode
1686       && GET_MODE (XEXP (*op0, 0)) == CCZ1mode
1687       && REG_P (XEXP (*op0, 0))
1688       && XEXP (*op0, 1) == const0_rtx
1689       && *op1 == const0_rtx)
1690     {
1691       if ((*code == EQ && GET_CODE (*op0) == NE)
1692           || (*code == NE && GET_CODE (*op0) == EQ))
1693         *code = EQ;
1694       else
1695         *code = NE;
1696       *op0 = XEXP (*op0, 0);
1697     }
1698
1699   /* Prefer register over memory as first operand.  */
1700   if (MEM_P (*op0) && REG_P (*op1))
1701     {
1702       rtx tem = *op0; *op0 = *op1; *op1 = tem;
1703       *code = (int)swap_condition ((enum rtx_code)*code);
1704     }
1705
1706   /* Using the scalar variants of vector instructions for 64 bit FP
1707      comparisons might require swapping the operands.  */
1708   if (TARGET_VX
1709       && register_operand (*op0, DFmode)
1710       && register_operand (*op1, DFmode)
1711       && (*code == LT || *code == LE || *code == UNGT || *code == UNGE))
1712     {
1713       rtx tmp;
1714
1715       switch (*code)
1716         {
1717         case LT:   *code = GT; break;
1718         case LE:   *code = GE; break;
1719         case UNGT: *code = UNLE; break;
1720         case UNGE: *code = UNLT; break;
1721         default: ;
1722         }
1723       tmp = *op0; *op0 = *op1; *op1 = tmp;
1724     }
1725
1726   /* A comparison result is compared against zero.  Replace it with
1727      the (perhaps inverted) original comparison.
1728      This probably should be done by simplify_relational_operation.  */
1729   if ((*code == EQ || *code == NE)
1730       && *op1 == const0_rtx
1731       && COMPARISON_P (*op0)
1732       && CC_REG_P (XEXP (*op0, 0)))
1733     {
1734       enum rtx_code new_code;
1735
1736       if (*code == EQ)
1737         new_code = reversed_comparison_code_parts (GET_CODE (*op0),
1738                                                    XEXP (*op0, 0),
1739                                                    XEXP (*op1, 0), NULL);
1740       else
1741         new_code = GET_CODE (*op0);
1742
1743       if (new_code != UNKNOWN)
1744         {
1745           *code = new_code;
1746           *op1 = XEXP (*op0, 1);
1747           *op0 = XEXP (*op0, 0);
1748         }
1749     }
1750 }
1751
1752 /* Helper function for s390_emit_compare.  If possible emit a 64 bit
1753    FP compare using the single element variant of vector instructions.
1754    Replace CODE with the comparison code to be used in the CC reg
1755    compare and return the condition code register RTX in CC.  */
1756
1757 static bool
1758 s390_expand_vec_compare_scalar (enum rtx_code *code, rtx cmp1, rtx cmp2,
1759                                 rtx *cc)
1760 {
1761   machine_mode cmp_mode;
1762   bool swap_p = false;
1763
1764   switch (*code)
1765     {
1766     case EQ:   cmp_mode = CCVEQmode;  break;
1767     case NE:   cmp_mode = CCVEQmode;  break;
1768     case GT:   cmp_mode = CCVFHmode;  break;
1769     case GE:   cmp_mode = CCVFHEmode; break;
1770     case UNLE: cmp_mode = CCVFHmode;  break;
1771     case UNLT: cmp_mode = CCVFHEmode; break;
1772     case LT:   cmp_mode = CCVFHmode;  *code = GT;   swap_p = true; break;
1773     case LE:   cmp_mode = CCVFHEmode; *code = GE;   swap_p = true; break;
1774     case UNGE: cmp_mode = CCVFHmode;  *code = UNLE; swap_p = true; break;
1775     case UNGT: cmp_mode = CCVFHEmode; *code = UNLT; swap_p = true; break;
1776     default: return false;
1777     }
1778
1779   if (swap_p)
1780     {
1781       rtx tmp = cmp2;
1782       cmp2 = cmp1;
1783       cmp1 = tmp;
1784     }
1785
1786   emit_insn (gen_rtx_PARALLEL (VOIDmode,
1787                gen_rtvec (2,
1788                           gen_rtx_SET (gen_rtx_REG (cmp_mode, CC_REGNUM),
1789                                        gen_rtx_COMPARE (cmp_mode, cmp1,
1790                                                         cmp2)),
1791                           gen_rtx_CLOBBER (VOIDmode,
1792                                            gen_rtx_SCRATCH (V2DImode)))));
1793
1794   /* This is the cc reg how it will be used in the cc mode consumer.
1795      It either needs to be CCVFALL or CCVFANY.  However, CC1 will
1796      never be set by the scalar variants.  So it actually doesn't
1797      matter which one we choose here.  */
1798   *cc = gen_rtx_REG (CCVFALLmode, CC_REGNUM);
1799   return true;
1800 }
1801
1802
1803 /* Emit a compare instruction suitable to implement the comparison
1804    OP0 CODE OP1.  Return the correct condition RTL to be placed in
1805    the IF_THEN_ELSE of the conditional branch testing the result.  */
1806
1807 rtx
1808 s390_emit_compare (enum rtx_code code, rtx op0, rtx op1)
1809 {
1810   machine_mode mode = s390_select_ccmode (code, op0, op1);
1811   rtx cc;
1812
1813   if (TARGET_VX
1814       && register_operand (op0, DFmode)
1815       && register_operand (op1, DFmode)
1816       && s390_expand_vec_compare_scalar (&code, op0, op1, &cc))
1817     {
1818       /* Work has been done by s390_expand_vec_compare_scalar already.  */
1819     }
1820   else if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
1821     {
1822       /* Do not output a redundant compare instruction if a
1823          compare_and_swap pattern already computed the result and the
1824          machine modes are compatible.  */
1825       gcc_assert (s390_cc_modes_compatible (GET_MODE (op0), mode)
1826                   == GET_MODE (op0));
1827       cc = op0;
1828     }
1829   else
1830     {
1831       cc = gen_rtx_REG (mode, CC_REGNUM);
1832       emit_insn (gen_rtx_SET (cc, gen_rtx_COMPARE (mode, op0, op1)));
1833     }
1834
1835   return gen_rtx_fmt_ee (code, VOIDmode, cc, const0_rtx);
1836 }
1837
1838 /* Emit a SImode compare and swap instruction setting MEM to NEW_RTX if OLD
1839    matches CMP.
1840    Return the correct condition RTL to be placed in the IF_THEN_ELSE of the
1841    conditional branch testing the result.  */
1842
1843 static rtx
1844 s390_emit_compare_and_swap (enum rtx_code code, rtx old, rtx mem,
1845                             rtx cmp, rtx new_rtx)
1846 {
1847   emit_insn (gen_atomic_compare_and_swapsi_internal (old, mem, cmp, new_rtx));
1848   return s390_emit_compare (code, gen_rtx_REG (CCZ1mode, CC_REGNUM),
1849                             const0_rtx);
1850 }
1851
1852 /* Emit a jump instruction to TARGET and return it.  If COND is
1853    NULL_RTX, emit an unconditional jump, else a conditional jump under
1854    condition COND.  */
1855
1856 rtx_insn *
1857 s390_emit_jump (rtx target, rtx cond)
1858 {
1859   rtx insn;
1860
1861   target = gen_rtx_LABEL_REF (VOIDmode, target);
1862   if (cond)
1863     target = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, target, pc_rtx);
1864
1865   insn = gen_rtx_SET (pc_rtx, target);
1866   return emit_jump_insn (insn);
1867 }
1868
1869 /* Return branch condition mask to implement a branch
1870    specified by CODE.  Return -1 for invalid comparisons.  */
1871
1872 int
1873 s390_branch_condition_mask (rtx code)
1874 {
1875   const int CC0 = 1 << 3;
1876   const int CC1 = 1 << 2;
1877   const int CC2 = 1 << 1;
1878   const int CC3 = 1 << 0;
1879
1880   gcc_assert (GET_CODE (XEXP (code, 0)) == REG);
1881   gcc_assert (REGNO (XEXP (code, 0)) == CC_REGNUM);
1882   gcc_assert (XEXP (code, 1) == const0_rtx
1883               || (GET_MODE (XEXP (code, 0)) == CCRAWmode
1884                   && CONST_INT_P (XEXP (code, 1))));
1885
1886
1887   switch (GET_MODE (XEXP (code, 0)))
1888     {
1889     case CCZmode:
1890     case CCZ1mode:
1891       switch (GET_CODE (code))
1892         {
1893         case EQ:        return CC0;
1894         case NE:        return CC1 | CC2 | CC3;
1895         default:        return -1;
1896         }
1897       break;
1898
1899     case CCT1mode:
1900       switch (GET_CODE (code))
1901         {
1902         case EQ:        return CC1;
1903         case NE:        return CC0 | CC2 | CC3;
1904         default:        return -1;
1905         }
1906       break;
1907
1908     case CCT2mode:
1909       switch (GET_CODE (code))
1910         {
1911         case EQ:        return CC2;
1912         case NE:        return CC0 | CC1 | CC3;
1913         default:        return -1;
1914         }
1915       break;
1916
1917     case CCT3mode:
1918       switch (GET_CODE (code))
1919         {
1920         case EQ:        return CC3;
1921         case NE:        return CC0 | CC1 | CC2;
1922         default:        return -1;
1923         }
1924       break;
1925
1926     case CCLmode:
1927       switch (GET_CODE (code))
1928         {
1929         case EQ:        return CC0 | CC2;
1930         case NE:        return CC1 | CC3;
1931         default:        return -1;
1932         }
1933       break;
1934
1935     case CCL1mode:
1936       switch (GET_CODE (code))
1937         {
1938         case LTU:       return CC2 | CC3;  /* carry */
1939         case GEU:       return CC0 | CC1;  /* no carry */
1940         default:        return -1;
1941         }
1942       break;
1943
1944     case CCL2mode:
1945       switch (GET_CODE (code))
1946         {
1947         case GTU:       return CC0 | CC1;  /* borrow */
1948         case LEU:       return CC2 | CC3;  /* no borrow */
1949         default:        return -1;
1950         }
1951       break;
1952
1953     case CCL3mode:
1954       switch (GET_CODE (code))
1955         {
1956         case EQ:        return CC0 | CC2;
1957         case NE:        return CC1 | CC3;
1958         case LTU:       return CC1;
1959         case GTU:       return CC3;
1960         case LEU:       return CC1 | CC2;
1961         case GEU:       return CC2 | CC3;
1962         default:        return -1;
1963         }
1964
1965     case CCUmode:
1966       switch (GET_CODE (code))
1967         {
1968         case EQ:        return CC0;
1969         case NE:        return CC1 | CC2 | CC3;
1970         case LTU:       return CC1;
1971         case GTU:       return CC2;
1972         case LEU:       return CC0 | CC1;
1973         case GEU:       return CC0 | CC2;
1974         default:        return -1;
1975         }
1976       break;
1977
1978     case CCURmode:
1979       switch (GET_CODE (code))
1980         {
1981         case EQ:        return CC0;
1982         case NE:        return CC2 | CC1 | CC3;
1983         case LTU:       return CC2;
1984         case GTU:       return CC1;
1985         case LEU:       return CC0 | CC2;
1986         case GEU:       return CC0 | CC1;
1987         default:        return -1;
1988         }
1989       break;
1990
1991     case CCAPmode:
1992       switch (GET_CODE (code))
1993         {
1994         case EQ:        return CC0;
1995         case NE:        return CC1 | CC2 | CC3;
1996         case LT:        return CC1 | CC3;
1997         case GT:        return CC2;
1998         case LE:        return CC0 | CC1 | CC3;
1999         case GE:        return CC0 | CC2;
2000         default:        return -1;
2001         }
2002       break;
2003
2004     case CCANmode:
2005       switch (GET_CODE (code))
2006         {
2007         case EQ:        return CC0;
2008         case NE:        return CC1 | CC2 | CC3;
2009         case LT:        return CC1;
2010         case GT:        return CC2 | CC3;
2011         case LE:        return CC0 | CC1;
2012         case GE:        return CC0 | CC2 | CC3;
2013         default:        return -1;
2014         }
2015       break;
2016
2017     case CCSmode:
2018       switch (GET_CODE (code))
2019         {
2020         case EQ:        return CC0;
2021         case NE:        return CC1 | CC2 | CC3;
2022         case LT:        return CC1;
2023         case GT:        return CC2;
2024         case LE:        return CC0 | CC1;
2025         case GE:        return CC0 | CC2;
2026         case UNORDERED: return CC3;
2027         case ORDERED:   return CC0 | CC1 | CC2;
2028         case UNEQ:      return CC0 | CC3;
2029         case UNLT:      return CC1 | CC3;
2030         case UNGT:      return CC2 | CC3;
2031         case UNLE:      return CC0 | CC1 | CC3;
2032         case UNGE:      return CC0 | CC2 | CC3;
2033         case LTGT:      return CC1 | CC2;
2034         default:        return -1;
2035         }
2036       break;
2037
2038     case CCSRmode:
2039       switch (GET_CODE (code))
2040         {
2041         case EQ:        return CC0;
2042         case NE:        return CC2 | CC1 | CC3;
2043         case LT:        return CC2;
2044         case GT:        return CC1;
2045         case LE:        return CC0 | CC2;
2046         case GE:        return CC0 | CC1;
2047         case UNORDERED: return CC3;
2048         case ORDERED:   return CC0 | CC2 | CC1;
2049         case UNEQ:      return CC0 | CC3;
2050         case UNLT:      return CC2 | CC3;
2051         case UNGT:      return CC1 | CC3;
2052         case UNLE:      return CC0 | CC2 | CC3;
2053         case UNGE:      return CC0 | CC1 | CC3;
2054         case LTGT:      return CC2 | CC1;
2055         default:        return -1;
2056         }
2057       break;
2058
2059       /* Vector comparison modes.  */
2060       /* CC2 will never be set.  It however is part of the negated
2061          masks.  */
2062     case CCVIALLmode:
2063       switch (GET_CODE (code))
2064         {
2065         case EQ:
2066         case GTU:
2067         case GT:
2068         case GE:        return CC0;
2069           /* The inverted modes are in fact *any* modes.  */
2070         case NE:
2071         case LEU:
2072         case LE:
2073         case LT:        return CC3 | CC1 | CC2;
2074         default:        return -1;
2075         }
2076
2077     case CCVIANYmode:
2078       switch (GET_CODE (code))
2079         {
2080         case EQ:
2081         case GTU:
2082         case GT:
2083         case GE:        return CC0 | CC1;
2084           /* The inverted modes are in fact *all* modes.  */
2085         case NE:
2086         case LEU:
2087         case LE:
2088         case LT:        return CC3 | CC2;
2089         default:        return -1;
2090         }
2091     case CCVFALLmode:
2092       switch (GET_CODE (code))
2093         {
2094         case EQ:
2095         case GT:
2096         case GE:        return CC0;
2097           /* The inverted modes are in fact *any* modes.  */
2098         case NE:
2099         case UNLE:
2100         case UNLT:      return CC3 | CC1 | CC2;
2101         default:        return -1;
2102         }
2103
2104     case CCVFANYmode:
2105       switch (GET_CODE (code))
2106         {
2107         case EQ:
2108         case GT:
2109         case GE:        return CC0 | CC1;
2110           /* The inverted modes are in fact *all* modes.  */
2111         case NE:
2112         case UNLE:
2113         case UNLT:      return CC3 | CC2;
2114         default:        return -1;
2115         }
2116
2117     case CCRAWmode:
2118       switch (GET_CODE (code))
2119         {
2120         case EQ:
2121           return INTVAL (XEXP (code, 1));
2122         case NE:
2123           return (INTVAL (XEXP (code, 1))) ^ 0xf;
2124         default:
2125           gcc_unreachable ();
2126         }
2127
2128     default:
2129       return -1;
2130     }
2131 }
2132
2133
2134 /* Return branch condition mask to implement a compare and branch
2135    specified by CODE.  Return -1 for invalid comparisons.  */
2136
2137 int
2138 s390_compare_and_branch_condition_mask (rtx code)
2139 {
2140   const int CC0 = 1 << 3;
2141   const int CC1 = 1 << 2;
2142   const int CC2 = 1 << 1;
2143
2144   switch (GET_CODE (code))
2145     {
2146     case EQ:
2147       return CC0;
2148     case NE:
2149       return CC1 | CC2;
2150     case LT:
2151     case LTU:
2152       return CC1;
2153     case GT:
2154     case GTU:
2155       return CC2;
2156     case LE:
2157     case LEU:
2158       return CC0 | CC1;
2159     case GE:
2160     case GEU:
2161       return CC0 | CC2;
2162     default:
2163       gcc_unreachable ();
2164     }
2165   return -1;
2166 }
2167
2168 /* If INV is false, return assembler mnemonic string to implement
2169    a branch specified by CODE.  If INV is true, return mnemonic
2170    for the corresponding inverted branch.  */
2171
2172 static const char *
2173 s390_branch_condition_mnemonic (rtx code, int inv)
2174 {
2175   int mask;
2176
2177   static const char *const mnemonic[16] =
2178     {
2179       NULL, "o", "h", "nle",
2180       "l", "nhe", "lh", "ne",
2181       "e", "nlh", "he", "nl",
2182       "le", "nh", "no", NULL
2183     };
2184
2185   if (GET_CODE (XEXP (code, 0)) == REG
2186       && REGNO (XEXP (code, 0)) == CC_REGNUM
2187       && (XEXP (code, 1) == const0_rtx
2188           || (GET_MODE (XEXP (code, 0)) == CCRAWmode
2189               && CONST_INT_P (XEXP (code, 1)))))
2190     mask = s390_branch_condition_mask (code);
2191   else
2192     mask = s390_compare_and_branch_condition_mask (code);
2193
2194   gcc_assert (mask >= 0);
2195
2196   if (inv)
2197     mask ^= 15;
2198
2199   gcc_assert (mask >= 1 && mask <= 14);
2200
2201   return mnemonic[mask];
2202 }
2203
2204 /* Return the part of op which has a value different from def.
2205    The size of the part is determined by mode.
2206    Use this function only if you already know that op really
2207    contains such a part.  */
2208
2209 unsigned HOST_WIDE_INT
2210 s390_extract_part (rtx op, machine_mode mode, int def)
2211 {
2212   unsigned HOST_WIDE_INT value = 0;
2213   int max_parts = HOST_BITS_PER_WIDE_INT / GET_MODE_BITSIZE (mode);
2214   int part_bits = GET_MODE_BITSIZE (mode);
2215   unsigned HOST_WIDE_INT part_mask
2216     = ((unsigned HOST_WIDE_INT)1 << part_bits) - 1;
2217   int i;
2218
2219   for (i = 0; i < max_parts; i++)
2220     {
2221       if (i == 0)
2222         value = (unsigned HOST_WIDE_INT) INTVAL (op);
2223       else
2224         value >>= part_bits;
2225
2226       if ((value & part_mask) != (def & part_mask))
2227         return value & part_mask;
2228     }
2229
2230   gcc_unreachable ();
2231 }
2232
2233 /* If OP is an integer constant of mode MODE with exactly one
2234    part of mode PART_MODE unequal to DEF, return the number of that
2235    part. Otherwise, return -1.  */
2236
2237 int
2238 s390_single_part (rtx op,
2239                   machine_mode mode,
2240                   machine_mode part_mode,
2241                   int def)
2242 {
2243   unsigned HOST_WIDE_INT value = 0;
2244   int n_parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (part_mode);
2245   unsigned HOST_WIDE_INT part_mask
2246     = ((unsigned HOST_WIDE_INT)1 << GET_MODE_BITSIZE (part_mode)) - 1;
2247   int i, part = -1;
2248
2249   if (GET_CODE (op) != CONST_INT)
2250     return -1;
2251
2252   for (i = 0; i < n_parts; i++)
2253     {
2254       if (i == 0)
2255         value = (unsigned HOST_WIDE_INT) INTVAL (op);
2256       else
2257         value >>= GET_MODE_BITSIZE (part_mode);
2258
2259       if ((value & part_mask) != (def & part_mask))
2260         {
2261           if (part != -1)
2262             return -1;
2263           else
2264             part = i;
2265         }
2266     }
2267   return part == -1 ? -1 : n_parts - 1 - part;
2268 }
2269
2270 /* Return true if IN contains a contiguous bitfield in the lower SIZE
2271    bits and no other bits are set in (the lower SIZE bits of) IN.
2272
2273    PSTART and PEND can be used to obtain the start and end
2274    position (inclusive) of the bitfield relative to 64
2275    bits. *PSTART / *PEND gives the position of the first/last bit
2276    of the bitfield counting from the highest order bit starting
2277    with zero.  */
2278
2279 bool
2280 s390_contiguous_bitmask_nowrap_p (unsigned HOST_WIDE_INT in, int size,
2281                                   int *pstart, int *pend)
2282 {
2283   int start;
2284   int end = -1;
2285   int lowbit = sizeof (HOST_WIDE_INT) * BITS_PER_UNIT - 1;
2286   int highbit = sizeof (HOST_WIDE_INT) * BITS_PER_UNIT - size;
2287   unsigned HOST_WIDE_INT bitmask = 1ULL;
2288
2289   gcc_assert (!!pstart == !!pend);
2290   for (start = lowbit; start >= highbit; bitmask <<= 1, start--)
2291     if (end == -1)
2292       {
2293         /* Look for the rightmost bit of a contiguous range of ones.  */
2294         if (bitmask & in)
2295           /* Found it.  */
2296           end = start;
2297       }
2298     else
2299       {
2300         /* Look for the firt zero bit after the range of ones.  */
2301         if (! (bitmask & in))
2302           /* Found it.  */
2303           break;
2304       }
2305   /* We're one past the last one-bit.  */
2306   start++;
2307
2308   if (end == -1)
2309     /* No one bits found.  */
2310     return false;
2311
2312   if (start > highbit)
2313     {
2314       unsigned HOST_WIDE_INT mask;
2315
2316       /* Calculate a mask for all bits beyond the contiguous bits.  */
2317       mask = ((~(0ULL) >> highbit) & (~(0ULL) << (lowbit - start + 1)));
2318       if (mask & in)
2319         /* There are more bits set beyond the first range of one bits.  */
2320         return false;
2321     }
2322
2323   if (pstart)
2324     {
2325       *pstart = start;
2326       *pend = end;
2327     }
2328
2329   return true;
2330 }
2331
2332 /* Same as s390_contiguous_bitmask_nowrap_p but also returns true
2333    if ~IN contains a contiguous bitfield.  In that case, *END is <
2334    *START.
2335
2336    If WRAP_P is true, a bitmask that wraps around is also tested.
2337    When a wraparoud occurs *START is greater than *END (in
2338    non-null pointers), and the uppermost (64 - SIZE) bits are thus
2339    part of the range.  If WRAP_P is false, no wraparound is
2340    tested.  */
2341
2342 bool
2343 s390_contiguous_bitmask_p (unsigned HOST_WIDE_INT in, bool wrap_p,
2344                            int size, int *start, int *end)
2345 {
2346   int bs = sizeof (HOST_WIDE_INT) * BITS_PER_UNIT;
2347   bool b;
2348
2349   gcc_assert (!!start == !!end);
2350   if ((in & ((~(0ULL)) >> (bs - size))) == 0)
2351     /* This cannot be expressed as a contiguous bitmask.  Exit early because
2352        the second call of s390_contiguous_bitmask_nowrap_p would accept this as
2353        a valid bitmask.  */
2354     return false;
2355   b = s390_contiguous_bitmask_nowrap_p (in, size, start, end);
2356   if (b)
2357     return true;
2358   if (! wrap_p)
2359     return false;
2360   b = s390_contiguous_bitmask_nowrap_p (~in, size, start, end);
2361   if (b && start)
2362     {
2363       int s = *start;
2364       int e = *end;
2365
2366       gcc_assert (s >= 1);
2367       *start = ((e + 1) & (bs - 1));
2368       *end = ((s - 1 + bs) & (bs - 1));
2369     }
2370
2371   return b;
2372 }
2373
2374 /* Return true if OP contains the same contiguous bitfield in *all*
2375    its elements.  START and END can be used to obtain the start and
2376    end position of the bitfield.
2377
2378    START/STOP give the position of the first/last bit of the bitfield
2379    counting from the lowest order bit starting with zero.  In order to
2380    use these values for S/390 instructions this has to be converted to
2381    "bits big endian" style.  */
2382
2383 bool
2384 s390_contiguous_bitmask_vector_p (rtx op, int *start, int *end)
2385 {
2386   unsigned HOST_WIDE_INT mask;
2387   int size;
2388   rtx elt;
2389   bool b;
2390
2391   gcc_assert (!!start == !!end);
2392   if (!const_vec_duplicate_p (op, &elt)
2393       || !CONST_INT_P (elt))
2394     return false;
2395
2396   size = GET_MODE_UNIT_BITSIZE (GET_MODE (op));
2397
2398   /* We cannot deal with V1TI/V1TF. This would require a vgmq.  */
2399   if (size > 64)
2400     return false;
2401
2402   mask = UINTVAL (elt);
2403
2404   b = s390_contiguous_bitmask_p (mask, true, size, start, end);
2405   if (b)
2406     {
2407       if (start)
2408         {
2409           int bs = sizeof (HOST_WIDE_INT) * BITS_PER_UNIT;
2410
2411           *start -= (bs - size);
2412           *end -= (bs - size);
2413         }
2414       return true;
2415     }
2416   else
2417     return false;
2418 }
2419
2420 /* Return true if C consists only of byte chunks being either 0 or
2421    0xff.  If MASK is !=NULL a byte mask is generated which is
2422    appropriate for the vector generate byte mask instruction.  */
2423
2424 bool
2425 s390_bytemask_vector_p (rtx op, unsigned *mask)
2426 {
2427   int i;
2428   unsigned tmp_mask = 0;
2429   int nunit, unit_size;
2430
2431   if (!VECTOR_MODE_P (GET_MODE (op))
2432       || GET_CODE (op) != CONST_VECTOR
2433       || !CONST_INT_P (XVECEXP (op, 0, 0)))
2434     return false;
2435
2436   nunit = GET_MODE_NUNITS (GET_MODE (op));
2437   unit_size = GET_MODE_UNIT_SIZE (GET_MODE (op));
2438
2439   for (i = 0; i < nunit; i++)
2440     {
2441       unsigned HOST_WIDE_INT c;
2442       int j;
2443
2444       if (!CONST_INT_P (XVECEXP (op, 0, i)))
2445         return false;
2446
2447       c = UINTVAL (XVECEXP (op, 0, i));
2448       for (j = 0; j < unit_size; j++)
2449         {
2450           if ((c & 0xff) != 0 && (c & 0xff) != 0xff)
2451             return false;
2452           tmp_mask |= (c & 1) << ((nunit - 1 - i) * unit_size + j);
2453           c = c >> BITS_PER_UNIT;
2454         }
2455     }
2456
2457   if (mask != NULL)
2458     *mask = tmp_mask;
2459
2460   return true;
2461 }
2462
2463 /* Check whether a rotate of ROTL followed by an AND of CONTIG is
2464    equivalent to a shift followed by the AND.  In particular, CONTIG
2465    should not overlap the (rotated) bit 0/bit 63 gap.  Negative values
2466    for ROTL indicate a rotate to the right.  */
2467
2468 bool
2469 s390_extzv_shift_ok (int bitsize, int rotl, unsigned HOST_WIDE_INT contig)
2470 {
2471   int start, end;
2472   bool ok;
2473
2474   ok = s390_contiguous_bitmask_nowrap_p (contig, bitsize, &start, &end);
2475   gcc_assert (ok);
2476
2477   if (rotl >= 0)
2478     return (64 - end >= rotl);
2479   else
2480     {
2481       /* Translate "- rotate right" in BITSIZE mode to "rotate left" in
2482          DIMode.  */
2483       rotl = -rotl + (64 - bitsize);
2484       return (start >= rotl);
2485     }
2486 }
2487
2488 /* Check whether we can (and want to) split a double-word
2489    move in mode MODE from SRC to DST into two single-word
2490    moves, moving the subword FIRST_SUBWORD first.  */
2491
2492 bool
2493 s390_split_ok_p (rtx dst, rtx src, machine_mode mode, int first_subword)
2494 {
2495   /* Floating point and vector registers cannot be split.  */
2496   if (FP_REG_P (src) || FP_REG_P (dst) || VECTOR_REG_P (src) || VECTOR_REG_P (dst))
2497     return false;
2498
2499   /* We don't need to split if operands are directly accessible.  */
2500   if (s_operand (src, mode) || s_operand (dst, mode))
2501     return false;
2502
2503   /* Non-offsettable memory references cannot be split.  */
2504   if ((GET_CODE (src) == MEM && !offsettable_memref_p (src))
2505       || (GET_CODE (dst) == MEM && !offsettable_memref_p (dst)))
2506     return false;
2507
2508   /* Moving the first subword must not clobber a register
2509      needed to move the second subword.  */
2510   if (register_operand (dst, mode))
2511     {
2512       rtx subreg = operand_subword (dst, first_subword, 0, mode);
2513       if (reg_overlap_mentioned_p (subreg, src))
2514         return false;
2515     }
2516
2517   return true;
2518 }
2519
2520 /* Return true if it can be proven that [MEM1, MEM1 + SIZE]
2521    and [MEM2, MEM2 + SIZE] do overlap and false
2522    otherwise.  */
2523
2524 bool
2525 s390_overlap_p (rtx mem1, rtx mem2, HOST_WIDE_INT size)
2526 {
2527   rtx addr1, addr2, addr_delta;
2528   HOST_WIDE_INT delta;
2529
2530   if (GET_CODE (mem1) != MEM || GET_CODE (mem2) != MEM)
2531     return true;
2532
2533   if (size == 0)
2534     return false;
2535
2536   addr1 = XEXP (mem1, 0);
2537   addr2 = XEXP (mem2, 0);
2538
2539   addr_delta = simplify_binary_operation (MINUS, Pmode, addr2, addr1);
2540
2541   /* This overlapping check is used by peepholes merging memory block operations.
2542      Overlapping operations would otherwise be recognized by the S/390 hardware
2543      and would fall back to a slower implementation. Allowing overlapping
2544      operations would lead to slow code but not to wrong code. Therefore we are
2545      somewhat optimistic if we cannot prove that the memory blocks are
2546      overlapping.
2547      That's why we return false here although this may accept operations on
2548      overlapping memory areas.  */
2549   if (!addr_delta || GET_CODE (addr_delta) != CONST_INT)
2550     return false;
2551
2552   delta = INTVAL (addr_delta);
2553
2554   if (delta == 0
2555       || (delta > 0 && delta < size)
2556       || (delta < 0 && -delta < size))
2557     return true;
2558
2559   return false;
2560 }
2561
2562 /* Check whether the address of memory reference MEM2 equals exactly
2563    the address of memory reference MEM1 plus DELTA.  Return true if
2564    we can prove this to be the case, false otherwise.  */
2565
2566 bool
2567 s390_offset_p (rtx mem1, rtx mem2, rtx delta)
2568 {
2569   rtx addr1, addr2, addr_delta;
2570
2571   if (GET_CODE (mem1) != MEM || GET_CODE (mem2) != MEM)
2572     return false;
2573
2574   addr1 = XEXP (mem1, 0);
2575   addr2 = XEXP (mem2, 0);
2576
2577   addr_delta = simplify_binary_operation (MINUS, Pmode, addr2, addr1);
2578   if (!addr_delta || !rtx_equal_p (addr_delta, delta))
2579     return false;
2580
2581   return true;
2582 }
2583
2584 /* Expand logical operator CODE in mode MODE with operands OPERANDS.  */
2585
2586 void
2587 s390_expand_logical_operator (enum rtx_code code, machine_mode mode,
2588                               rtx *operands)
2589 {
2590   machine_mode wmode = mode;
2591   rtx dst = operands[0];
2592   rtx src1 = operands[1];
2593   rtx src2 = operands[2];
2594   rtx op, clob, tem;
2595
2596   /* If we cannot handle the operation directly, use a temp register.  */
2597   if (!s390_logical_operator_ok_p (operands))
2598     dst = gen_reg_rtx (mode);
2599
2600   /* QImode and HImode patterns make sense only if we have a destination
2601      in memory.  Otherwise perform the operation in SImode.  */
2602   if ((mode == QImode || mode == HImode) && GET_CODE (dst) != MEM)
2603     wmode = SImode;
2604
2605   /* Widen operands if required.  */
2606   if (mode != wmode)
2607     {
2608       if (GET_CODE (dst) == SUBREG
2609           && (tem = simplify_subreg (wmode, dst, mode, 0)) != 0)
2610         dst = tem;
2611       else if (REG_P (dst))
2612         dst = gen_rtx_SUBREG (wmode, dst, 0);
2613       else
2614         dst = gen_reg_rtx (wmode);
2615
2616       if (GET_CODE (src1) == SUBREG
2617           && (tem = simplify_subreg (wmode, src1, mode, 0)) != 0)
2618         src1 = tem;
2619       else if (GET_MODE (src1) != VOIDmode)
2620         src1 = gen_rtx_SUBREG (wmode, force_reg (mode, src1), 0);
2621
2622       if (GET_CODE (src2) == SUBREG
2623           && (tem = simplify_subreg (wmode, src2, mode, 0)) != 0)
2624         src2 = tem;
2625       else if (GET_MODE (src2) != VOIDmode)
2626         src2 = gen_rtx_SUBREG (wmode, force_reg (mode, src2), 0);
2627     }
2628
2629   /* Emit the instruction.  */
2630   op = gen_rtx_SET (dst, gen_rtx_fmt_ee (code, wmode, src1, src2));
2631   clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
2632   emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
2633
2634   /* Fix up the destination if needed.  */
2635   if (dst != operands[0])
2636     emit_move_insn (operands[0], gen_lowpart (mode, dst));
2637 }
2638
2639 /* Check whether OPERANDS are OK for a logical operation (AND, IOR, XOR).  */
2640
2641 bool
2642 s390_logical_operator_ok_p (rtx *operands)
2643 {
2644   /* If the destination operand is in memory, it needs to coincide
2645      with one of the source operands.  After reload, it has to be
2646      the first source operand.  */
2647   if (GET_CODE (operands[0]) == MEM)
2648     return rtx_equal_p (operands[0], operands[1])
2649            || (!reload_completed && rtx_equal_p (operands[0], operands[2]));
2650
2651   return true;
2652 }
2653
2654 /* Narrow logical operation CODE of memory operand MEMOP with immediate
2655    operand IMMOP to switch from SS to SI type instructions.  */
2656
2657 void
2658 s390_narrow_logical_operator (enum rtx_code code, rtx *memop, rtx *immop)
2659 {
2660   int def = code == AND ? -1 : 0;
2661   HOST_WIDE_INT mask;
2662   int part;
2663
2664   gcc_assert (GET_CODE (*memop) == MEM);
2665   gcc_assert (!MEM_VOLATILE_P (*memop));
2666
2667   mask = s390_extract_part (*immop, QImode, def);
2668   part = s390_single_part (*immop, GET_MODE (*memop), QImode, def);
2669   gcc_assert (part >= 0);
2670
2671   *memop = adjust_address (*memop, QImode, part);
2672   *immop = gen_int_mode (mask, QImode);
2673 }
2674
2675
2676 /* How to allocate a 'struct machine_function'.  */
2677
2678 static struct machine_function *
2679 s390_init_machine_status (void)
2680 {
2681   return ggc_cleared_alloc<machine_function> ();
2682 }
2683
2684 /* Map for smallest class containing reg regno.  */
2685
2686 const enum reg_class regclass_map[FIRST_PSEUDO_REGISTER] =
2687 { GENERAL_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,  /*  0 */
2688   ADDR_REGS,    ADDR_REGS, ADDR_REGS, ADDR_REGS,  /*  4 */
2689   ADDR_REGS,    ADDR_REGS, ADDR_REGS, ADDR_REGS,  /*  8 */
2690   ADDR_REGS,    ADDR_REGS, ADDR_REGS, ADDR_REGS,  /* 12 */
2691   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,    /* 16 */
2692   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,    /* 20 */
2693   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,    /* 24 */
2694   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,    /* 28 */
2695   ADDR_REGS,    CC_REGS,   ADDR_REGS, ADDR_REGS,  /* 32 */
2696   ACCESS_REGS,  ACCESS_REGS, VEC_REGS, VEC_REGS,  /* 36 */
2697   VEC_REGS, VEC_REGS, VEC_REGS, VEC_REGS,         /* 40 */
2698   VEC_REGS, VEC_REGS, VEC_REGS, VEC_REGS,         /* 44 */
2699   VEC_REGS, VEC_REGS, VEC_REGS, VEC_REGS,         /* 48 */
2700   VEC_REGS, VEC_REGS                              /* 52 */
2701 };
2702
2703 /* Return attribute type of insn.  */
2704
2705 static enum attr_type
2706 s390_safe_attr_type (rtx_insn *insn)
2707 {
2708   if (recog_memoized (insn) >= 0)
2709     return get_attr_type (insn);
2710   else
2711     return TYPE_NONE;
2712 }
2713
2714 /* Return true if DISP is a valid short displacement.  */
2715
2716 static bool
2717 s390_short_displacement (rtx disp)
2718 {
2719   /* No displacement is OK.  */
2720   if (!disp)
2721     return true;
2722
2723   /* Without the long displacement facility we don't need to
2724      distingiush between long and short displacement.  */
2725   if (!TARGET_LONG_DISPLACEMENT)
2726     return true;
2727
2728   /* Integer displacement in range.  */
2729   if (GET_CODE (disp) == CONST_INT)
2730     return INTVAL (disp) >= 0 && INTVAL (disp) < 4096;
2731
2732   /* GOT offset is not OK, the GOT can be large.  */
2733   if (GET_CODE (disp) == CONST
2734       && GET_CODE (XEXP (disp, 0)) == UNSPEC
2735       && (XINT (XEXP (disp, 0), 1) == UNSPEC_GOT
2736           || XINT (XEXP (disp, 0), 1) == UNSPEC_GOTNTPOFF))
2737     return false;
2738
2739   /* All other symbolic constants are literal pool references,
2740      which are OK as the literal pool must be small.  */
2741   if (GET_CODE (disp) == CONST)
2742     return true;
2743
2744   return false;
2745 }
2746
2747 /* Decompose a RTL expression ADDR for a memory address into
2748    its components, returned in OUT.
2749
2750    Returns false if ADDR is not a valid memory address, true
2751    otherwise.  If OUT is NULL, don't return the components,
2752    but check for validity only.
2753
2754    Note: Only addresses in canonical form are recognized.
2755    LEGITIMIZE_ADDRESS should convert non-canonical forms to the
2756    canonical form so that they will be recognized.  */
2757
2758 static int
2759 s390_decompose_address (rtx addr, struct s390_address *out)
2760 {
2761   HOST_WIDE_INT offset = 0;
2762   rtx base = NULL_RTX;
2763   rtx indx = NULL_RTX;
2764   rtx disp = NULL_RTX;
2765   rtx orig_disp;
2766   bool pointer = false;
2767   bool base_ptr = false;
2768   bool indx_ptr = false;
2769   bool literal_pool = false;
2770
2771   /* We may need to substitute the literal pool base register into the address
2772      below.  However, at this point we do not know which register is going to
2773      be used as base, so we substitute the arg pointer register.  This is going
2774      to be treated as holding a pointer below -- it shouldn't be used for any
2775      other purpose.  */
2776   rtx fake_pool_base = gen_rtx_REG (Pmode, ARG_POINTER_REGNUM);
2777
2778   /* Decompose address into base + index + displacement.  */
2779
2780   if (GET_CODE (addr) == REG || GET_CODE (addr) == UNSPEC)
2781     base = addr;
2782
2783   else if (GET_CODE (addr) == PLUS)
2784     {
2785       rtx op0 = XEXP (addr, 0);
2786       rtx op1 = XEXP (addr, 1);
2787       enum rtx_code code0 = GET_CODE (op0);
2788       enum rtx_code code1 = GET_CODE (op1);
2789
2790       if (code0 == REG || code0 == UNSPEC)
2791         {
2792           if (code1 == REG || code1 == UNSPEC)
2793             {
2794               indx = op0;       /* index + base */
2795               base = op1;
2796             }
2797
2798           else
2799             {
2800               base = op0;       /* base + displacement */
2801               disp = op1;
2802             }
2803         }
2804
2805       else if (code0 == PLUS)
2806         {
2807           indx = XEXP (op0, 0); /* index + base + disp */
2808           base = XEXP (op0, 1);
2809           disp = op1;
2810         }
2811
2812       else
2813         {
2814           return false;
2815         }
2816     }
2817
2818   else
2819     disp = addr;                /* displacement */
2820
2821   /* Extract integer part of displacement.  */
2822   orig_disp = disp;
2823   if (disp)
2824     {
2825       if (GET_CODE (disp) == CONST_INT)
2826         {
2827           offset = INTVAL (disp);
2828           disp = NULL_RTX;
2829         }
2830       else if (GET_CODE (disp) == CONST
2831                && GET_CODE (XEXP (disp, 0)) == PLUS
2832                && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
2833         {
2834           offset = INTVAL (XEXP (XEXP (disp, 0), 1));
2835           disp = XEXP (XEXP (disp, 0), 0);
2836         }
2837     }
2838
2839   /* Strip off CONST here to avoid special case tests later.  */
2840   if (disp && GET_CODE (disp) == CONST)
2841     disp = XEXP (disp, 0);
2842
2843   /* We can convert literal pool addresses to
2844      displacements by basing them off the base register.  */
2845   if (disp && GET_CODE (disp) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (disp))
2846     {
2847       /* Either base or index must be free to hold the base register.  */
2848       if (!base)
2849         base = fake_pool_base, literal_pool = true;
2850       else if (!indx)
2851         indx = fake_pool_base, literal_pool = true;
2852       else
2853         return false;
2854
2855       /* Mark up the displacement.  */
2856       disp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, disp),
2857                              UNSPEC_LTREL_OFFSET);
2858     }
2859
2860   /* Validate base register.  */
2861   if (base)
2862     {
2863       if (GET_CODE (base) == UNSPEC)
2864         switch (XINT (base, 1))
2865           {
2866           case UNSPEC_LTREF:
2867             if (!disp)
2868               disp = gen_rtx_UNSPEC (Pmode,
2869                                      gen_rtvec (1, XVECEXP (base, 0, 0)),
2870                                      UNSPEC_LTREL_OFFSET);
2871             else
2872               return false;
2873
2874             base = XVECEXP (base, 0, 1);
2875             break;
2876
2877           case UNSPEC_LTREL_BASE:
2878             if (XVECLEN (base, 0) == 1)
2879               base = fake_pool_base, literal_pool = true;
2880             else
2881               base = XVECEXP (base, 0, 1);
2882             break;
2883
2884           default:
2885             return false;
2886           }
2887
2888       if (!REG_P (base) || GET_MODE (base) != Pmode)
2889         return false;
2890
2891       if (REGNO (base) == STACK_POINTER_REGNUM
2892           || REGNO (base) == FRAME_POINTER_REGNUM
2893           || ((reload_completed || reload_in_progress)
2894               && frame_pointer_needed
2895               && REGNO (base) == HARD_FRAME_POINTER_REGNUM)
2896           || REGNO (base) == ARG_POINTER_REGNUM
2897           || (flag_pic
2898               && REGNO (base) == PIC_OFFSET_TABLE_REGNUM))
2899         pointer = base_ptr = true;
2900
2901       if ((reload_completed || reload_in_progress)
2902           && base == cfun->machine->base_reg)
2903         pointer = base_ptr = literal_pool = true;
2904     }
2905
2906   /* Validate index register.  */
2907   if (indx)
2908     {
2909       if (GET_CODE (indx) == UNSPEC)
2910         switch (XINT (indx, 1))
2911           {
2912           case UNSPEC_LTREF:
2913             if (!disp)
2914               disp = gen_rtx_UNSPEC (Pmode,
2915                                      gen_rtvec (1, XVECEXP (indx, 0, 0)),
2916                                      UNSPEC_LTREL_OFFSET);
2917             else
2918               return false;
2919
2920             indx = XVECEXP (indx, 0, 1);
2921             break;
2922
2923           case UNSPEC_LTREL_BASE:
2924             if (XVECLEN (indx, 0) == 1)
2925               indx = fake_pool_base, literal_pool = true;
2926             else
2927               indx = XVECEXP (indx, 0, 1);
2928             break;
2929
2930           default:
2931             return false;
2932           }
2933
2934       if (!REG_P (indx) || GET_MODE (indx) != Pmode)
2935         return false;
2936
2937       if (REGNO (indx) == STACK_POINTER_REGNUM
2938           || REGNO (indx) == FRAME_POINTER_REGNUM
2939           || ((reload_completed || reload_in_progress)
2940               && frame_pointer_needed
2941               && REGNO (indx) == HARD_FRAME_POINTER_REGNUM)
2942           || REGNO (indx) == ARG_POINTER_REGNUM
2943           || (flag_pic
2944               && REGNO (indx) == PIC_OFFSET_TABLE_REGNUM))
2945         pointer = indx_ptr = true;
2946
2947       if ((reload_completed || reload_in_progress)
2948           && indx == cfun->machine->base_reg)
2949         pointer = indx_ptr = literal_pool = true;
2950     }
2951
2952   /* Prefer to use pointer as base, not index.  */
2953   if (base && indx && !base_ptr
2954       && (indx_ptr || (!REG_POINTER (base) && REG_POINTER (indx))))
2955     {
2956       rtx tmp = base;
2957       base = indx;
2958       indx = tmp;
2959     }
2960
2961   /* Validate displacement.  */
2962   if (!disp)
2963     {
2964       /* If virtual registers are involved, the displacement will change later
2965          anyway as the virtual registers get eliminated.  This could make a
2966          valid displacement invalid, but it is more likely to make an invalid
2967          displacement valid, because we sometimes access the register save area
2968          via negative offsets to one of those registers.
2969          Thus we don't check the displacement for validity here.  If after
2970          elimination the displacement turns out to be invalid after all,
2971          this is fixed up by reload in any case.  */
2972       /* LRA maintains always displacements up to date and we need to
2973          know the displacement is right during all LRA not only at the
2974          final elimination.  */
2975       if (lra_in_progress
2976           || (base != arg_pointer_rtx
2977               && indx != arg_pointer_rtx
2978               && base != return_address_pointer_rtx
2979               && indx != return_address_pointer_rtx
2980               && base != frame_pointer_rtx
2981               && indx != frame_pointer_rtx
2982               && base != virtual_stack_vars_rtx
2983               && indx != virtual_stack_vars_rtx))
2984         if (!DISP_IN_RANGE (offset))
2985           return false;
2986     }
2987   else
2988     {
2989       /* All the special cases are pointers.  */
2990       pointer = true;
2991
2992       /* In the small-PIC case, the linker converts @GOT
2993          and @GOTNTPOFF offsets to possible displacements.  */
2994       if (GET_CODE (disp) == UNSPEC
2995           && (XINT (disp, 1) == UNSPEC_GOT
2996               || XINT (disp, 1) == UNSPEC_GOTNTPOFF)
2997           && flag_pic == 1)
2998         {
2999           ;
3000         }
3001
3002       /* Accept pool label offsets.  */
3003       else if (GET_CODE (disp) == UNSPEC
3004                && XINT (disp, 1) == UNSPEC_POOL_OFFSET)
3005         ;
3006
3007       /* Accept literal pool references.  */
3008       else if (GET_CODE (disp) == UNSPEC
3009                && XINT (disp, 1) == UNSPEC_LTREL_OFFSET)
3010         {
3011           /* In case CSE pulled a non literal pool reference out of
3012              the pool we have to reject the address.  This is
3013              especially important when loading the GOT pointer on non
3014              zarch CPUs.  In this case the literal pool contains an lt
3015              relative offset to the _GLOBAL_OFFSET_TABLE_ label which
3016              will most likely exceed the displacement.  */
3017           if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
3018               || !CONSTANT_POOL_ADDRESS_P (XVECEXP (disp, 0, 0)))
3019             return false;
3020
3021           orig_disp = gen_rtx_CONST (Pmode, disp);
3022           if (offset)
3023             {
3024               /* If we have an offset, make sure it does not
3025                  exceed the size of the constant pool entry.  */
3026               rtx sym = XVECEXP (disp, 0, 0);
3027               if (offset >= GET_MODE_SIZE (get_pool_mode (sym)))
3028                 return false;
3029
3030               orig_disp = plus_constant (Pmode, orig_disp, offset);
3031             }
3032         }
3033
3034       else
3035         return false;
3036     }
3037
3038   if (!base && !indx)
3039     pointer = true;
3040
3041   if (out)
3042     {
3043       out->base = base;
3044       out->indx = indx;
3045       out->disp = orig_disp;
3046       out->pointer = pointer;
3047       out->literal_pool = literal_pool;
3048     }
3049
3050   return true;
3051 }
3052
3053 /* Decompose a RTL expression OP for an address style operand into its
3054    components, and return the base register in BASE and the offset in
3055    OFFSET.  While OP looks like an address it is never supposed to be
3056    used as such.
3057
3058    Return true if OP is a valid address operand, false if not.  */
3059
3060 bool
3061 s390_decompose_addrstyle_without_index (rtx op, rtx *base,
3062                                         HOST_WIDE_INT *offset)
3063 {
3064   rtx off = NULL_RTX;
3065
3066   /* We can have an integer constant, an address register,
3067      or a sum of the two.  */
3068   if (CONST_SCALAR_INT_P (op))
3069     {
3070       off = op;
3071       op = NULL_RTX;
3072     }
3073   if (op && GET_CODE (op) == PLUS && CONST_SCALAR_INT_P (XEXP (op, 1)))
3074     {
3075       off = XEXP (op, 1);
3076       op = XEXP (op, 0);
3077     }
3078   while (op && GET_CODE (op) == SUBREG)
3079     op = SUBREG_REG (op);
3080
3081   if (op && GET_CODE (op) != REG)
3082     return false;
3083
3084   if (offset)
3085     {
3086       if (off == NULL_RTX)
3087         *offset = 0;
3088       else if (CONST_INT_P (off))
3089         *offset = INTVAL (off);
3090       else if (CONST_WIDE_INT_P (off))
3091         /* The offset will anyway be cut down to 12 bits so take just
3092            the lowest order chunk of the wide int.  */
3093         *offset = CONST_WIDE_INT_ELT (off, 0);
3094       else
3095         gcc_unreachable ();
3096     }
3097   if (base)
3098     *base = op;
3099
3100    return true;
3101 }
3102
3103
3104 /* Return true if CODE is a valid address without index.  */
3105
3106 bool
3107 s390_legitimate_address_without_index_p (rtx op)
3108 {
3109   struct s390_address addr;
3110
3111   if (!s390_decompose_address (XEXP (op, 0), &addr))
3112     return false;
3113   if (addr.indx)
3114     return false;
3115
3116   return true;
3117 }
3118
3119
3120 /* Return TRUE if ADDR is an operand valid for a load/store relative
3121    instruction.  Be aware that the alignment of the operand needs to
3122    be checked separately.
3123    Valid addresses are single references or a sum of a reference and a
3124    constant integer. Return these parts in SYMREF and ADDEND.  You can
3125    pass NULL in REF and/or ADDEND if you are not interested in these
3126    values.  Literal pool references are *not* considered symbol
3127    references.  */
3128
3129 static bool
3130 s390_loadrelative_operand_p (rtx addr, rtx *symref, HOST_WIDE_INT *addend)
3131 {
3132   HOST_WIDE_INT tmpaddend = 0;
3133
3134   if (GET_CODE (addr) == CONST)
3135     addr = XEXP (addr, 0);
3136
3137   if (GET_CODE (addr) == PLUS)
3138     {
3139       if (!CONST_INT_P (XEXP (addr, 1)))
3140         return false;
3141
3142       tmpaddend = INTVAL (XEXP (addr, 1));
3143       addr = XEXP (addr, 0);
3144     }
3145
3146   if ((GET_CODE (addr) == SYMBOL_REF && !CONSTANT_POOL_ADDRESS_P (addr))
3147       || (GET_CODE (addr) == UNSPEC
3148           && (XINT (addr, 1) == UNSPEC_GOTENT
3149               || (TARGET_CPU_ZARCH && XINT (addr, 1) == UNSPEC_PLT))))
3150     {
3151       if (symref)
3152         *symref = addr;
3153       if (addend)
3154         *addend = tmpaddend;
3155
3156       return true;
3157     }
3158   return false;
3159 }
3160
3161 /* Return true if the address in OP is valid for constraint letter C
3162    if wrapped in a MEM rtx.  Set LIT_POOL_OK to true if it literal
3163    pool MEMs should be accepted.  Only the Q, R, S, T constraint
3164    letters are allowed for C.  */
3165
3166 static int
3167 s390_check_qrst_address (char c, rtx op, bool lit_pool_ok)
3168 {
3169   struct s390_address addr;
3170   bool decomposed = false;
3171
3172   /* This check makes sure that no symbolic address (except literal
3173      pool references) are accepted by the R or T constraints.  */
3174   if (s390_loadrelative_operand_p (op, NULL, NULL))
3175     return 0;
3176
3177   /* Ensure literal pool references are only accepted if LIT_POOL_OK.  */
3178   if (!lit_pool_ok)
3179     {
3180       if (!s390_decompose_address (op, &addr))
3181         return 0;
3182       if (addr.literal_pool)
3183         return 0;
3184       decomposed = true;
3185     }
3186
3187   /* With reload, we sometimes get intermediate address forms that are
3188      actually invalid as-is, but we need to accept them in the most
3189      generic cases below ('R' or 'T'), since reload will in fact fix
3190      them up.  LRA behaves differently here; we never see such forms,
3191      but on the other hand, we need to strictly reject every invalid
3192      address form.  Perform this check right up front.  */
3193   if (lra_in_progress)
3194     {
3195       if (!decomposed && !s390_decompose_address (op, &addr))
3196         return 0;
3197       decomposed = true;
3198     }
3199
3200   switch (c)
3201     {
3202     case 'Q': /* no index short displacement */
3203       if (!decomposed && !s390_decompose_address (op, &addr))
3204         return 0;
3205       if (addr.indx)
3206         return 0;
3207       if (!s390_short_displacement (addr.disp))
3208         return 0;
3209       break;
3210
3211     case 'R': /* with index short displacement */
3212       if (TARGET_LONG_DISPLACEMENT)
3213         {
3214           if (!decomposed && !s390_decompose_address (op, &addr))
3215             return 0;
3216           if (!s390_short_displacement (addr.disp))
3217             return 0;
3218         }
3219       /* Any invalid address here will be fixed up by reload,
3220          so accept it for the most generic constraint.  */
3221       break;
3222
3223     case 'S': /* no index long displacement */
3224       if (!decomposed && !s390_decompose_address (op, &addr))
3225         return 0;
3226       if (addr.indx)
3227         return 0;
3228       break;
3229
3230     case 'T': /* with index long displacement */
3231       /* Any invalid address here will be fixed up by reload,
3232          so accept it for the most generic constraint.  */
3233       break;
3234
3235     default:
3236       return 0;
3237     }
3238   return 1;
3239 }
3240
3241
3242 /* Evaluates constraint strings described by the regular expression
3243    ([A|B|Z](Q|R|S|T))|Y and returns 1 if OP is a valid operand for
3244    the constraint given in STR, or 0 else.  */
3245
3246 int
3247 s390_mem_constraint (const char *str, rtx op)
3248 {
3249   char c = str[0];
3250
3251   switch (c)
3252     {
3253     case 'A':
3254       /* Check for offsettable variants of memory constraints.  */
3255       if (!MEM_P (op) || MEM_VOLATILE_P (op))
3256         return 0;
3257       if ((reload_completed || reload_in_progress)
3258           ? !offsettable_memref_p (op) : !offsettable_nonstrict_memref_p (op))
3259         return 0;
3260       return s390_check_qrst_address (str[1], XEXP (op, 0), true);
3261     case 'B':
3262       /* Check for non-literal-pool variants of memory constraints.  */
3263       if (!MEM_P (op))
3264         return 0;
3265       return s390_check_qrst_address (str[1], XEXP (op, 0), false);
3266     case 'Q':
3267     case 'R':
3268     case 'S':
3269     case 'T':
3270       if (GET_CODE (op) != MEM)
3271         return 0;
3272       return s390_check_qrst_address (c, XEXP (op, 0), true);
3273     case 'Y':
3274       /* Simply check for the basic form of a shift count.  Reload will
3275          take care of making sure we have a proper base register.  */
3276       if (!s390_decompose_addrstyle_without_index (op, NULL, NULL))
3277         return 0;
3278       break;
3279     case 'Z':
3280       return s390_check_qrst_address (str[1], op, true);
3281     default:
3282       return 0;
3283     }
3284   return 1;
3285 }
3286
3287
3288 /* Evaluates constraint strings starting with letter O.  Input
3289    parameter C is the second letter following the "O" in the constraint
3290    string. Returns 1 if VALUE meets the respective constraint and 0
3291    otherwise.  */
3292
3293 int
3294 s390_O_constraint_str (const char c, HOST_WIDE_INT value)
3295 {
3296   if (!TARGET_EXTIMM)
3297     return 0;
3298
3299   switch (c)
3300     {
3301     case 's':
3302       return trunc_int_for_mode (value, SImode) == value;
3303
3304     case 'p':
3305       return value == 0
3306         || s390_single_part (GEN_INT (value), DImode, SImode, 0) == 1;
3307
3308     case 'n':
3309       return s390_single_part (GEN_INT (value - 1), DImode, SImode, -1) == 1;
3310
3311     default:
3312       gcc_unreachable ();
3313     }
3314 }
3315
3316
3317 /* Evaluates constraint strings starting with letter N.  Parameter STR
3318    contains the letters following letter "N" in the constraint string.
3319    Returns true if VALUE matches the constraint.  */
3320
3321 int
3322 s390_N_constraint_str (const char *str, HOST_WIDE_INT value)
3323 {
3324   machine_mode mode, part_mode;
3325   int def;
3326   int part, part_goal;
3327
3328
3329   if (str[0] == 'x')
3330     part_goal = -1;
3331   else
3332     part_goal = str[0] - '0';
3333
3334   switch (str[1])
3335     {
3336     case 'Q':
3337       part_mode = QImode;
3338       break;
3339     case 'H':
3340       part_mode = HImode;
3341       break;
3342     case 'S':
3343       part_mode = SImode;
3344       break;
3345     default:
3346       return 0;
3347     }
3348
3349   switch (str[2])
3350     {
3351     case 'H':
3352       mode = HImode;
3353       break;
3354     case 'S':
3355       mode = SImode;
3356       break;
3357     case 'D':
3358       mode = DImode;
3359       break;
3360     default:
3361       return 0;
3362     }
3363
3364   switch (str[3])
3365     {
3366     case '0':
3367       def = 0;
3368       break;
3369     case 'F':
3370       def = -1;
3371       break;
3372     default:
3373       return 0;
3374     }
3375
3376   if (GET_MODE_SIZE (mode) <= GET_MODE_SIZE (part_mode))
3377     return 0;
3378
3379   part = s390_single_part (GEN_INT (value), mode, part_mode, def);
3380   if (part < 0)
3381     return 0;
3382   if (part_goal != -1 && part_goal != part)
3383     return 0;
3384
3385   return 1;
3386 }
3387
3388
3389 /* Returns true if the input parameter VALUE is a float zero.  */
3390
3391 int
3392 s390_float_const_zero_p (rtx value)
3393 {
3394   return (GET_MODE_CLASS (GET_MODE (value)) == MODE_FLOAT
3395           && value == CONST0_RTX (GET_MODE (value)));
3396 }
3397
3398 /* Implement TARGET_REGISTER_MOVE_COST.  */
3399
3400 static int
3401 s390_register_move_cost (machine_mode mode,
3402                          reg_class_t from, reg_class_t to)
3403 {
3404   /* On s390, copy between fprs and gprs is expensive.  */
3405
3406   /* It becomes somewhat faster having ldgr/lgdr.  */
3407   if (TARGET_Z10 && GET_MODE_SIZE (mode) == 8)
3408     {
3409       /* ldgr is single cycle. */
3410       if (reg_classes_intersect_p (from, GENERAL_REGS)
3411           && reg_classes_intersect_p (to, FP_REGS))
3412         return 1;
3413       /* lgdr needs 3 cycles. */
3414       if (reg_classes_intersect_p (to, GENERAL_REGS)
3415           && reg_classes_intersect_p (from, FP_REGS))
3416         return 3;
3417     }
3418
3419   /* Otherwise copying is done via memory.  */
3420   if ((reg_classes_intersect_p (from, GENERAL_REGS)
3421        && reg_classes_intersect_p (to, FP_REGS))
3422       || (reg_classes_intersect_p (from, FP_REGS)
3423           && reg_classes_intersect_p (to, GENERAL_REGS)))
3424     return 10;
3425
3426   return 1;
3427 }
3428
3429 /* Implement TARGET_MEMORY_MOVE_COST.  */
3430
3431 static int
3432 s390_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
3433                        reg_class_t rclass ATTRIBUTE_UNUSED,
3434                        bool in ATTRIBUTE_UNUSED)
3435 {
3436   return 2;
3437 }
3438
3439 /* Compute a (partial) cost for rtx X.  Return true if the complete
3440    cost has been computed, and false if subexpressions should be
3441    scanned.  In either case, *TOTAL contains the cost result.  The
3442    initial value of *TOTAL is the default value computed by
3443    rtx_cost.  It may be left unmodified.  OUTER_CODE contains the
3444    code of the superexpression of x.  */
3445
3446 static bool
3447 s390_rtx_costs (rtx x, machine_mode mode, int outer_code,
3448                 int opno ATTRIBUTE_UNUSED,
3449                 int *total, bool speed ATTRIBUTE_UNUSED)
3450 {
3451   int code = GET_CODE (x);
3452   switch (code)
3453     {
3454     case CONST:
3455     case CONST_INT:
3456     case LABEL_REF:
3457     case SYMBOL_REF:
3458     case CONST_DOUBLE:
3459     case CONST_WIDE_INT:
3460     case MEM:
3461       *total = 0;
3462       return true;
3463
3464     case IOR:
3465       /* risbg */
3466       if (GET_CODE (XEXP (x, 0)) == AND
3467           && GET_CODE (XEXP (x, 1)) == ASHIFT
3468           && REG_P (XEXP (XEXP (x, 0), 0))
3469           && REG_P (XEXP (XEXP (x, 1), 0))
3470           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
3471           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
3472           && (UINTVAL (XEXP (XEXP (x, 0), 1)) ==
3473               (1UL << UINTVAL (XEXP (XEXP (x, 1), 1))) - 1))
3474         {
3475           *total = COSTS_N_INSNS (2);
3476           return true;
3477         }
3478       /* fallthrough */
3479     case ASHIFT:
3480     case ASHIFTRT:
3481     case LSHIFTRT:
3482     case ROTATE:
3483     case ROTATERT:
3484     case AND:
3485     case XOR:
3486     case NEG:
3487     case NOT:
3488       *total = COSTS_N_INSNS (1);
3489       return false;
3490
3491     case PLUS:
3492     case MINUS:
3493       *total = COSTS_N_INSNS (1);
3494       return false;
3495
3496     case MULT:
3497       switch (mode)
3498         {
3499         case SImode:
3500           {
3501             rtx left = XEXP (x, 0);
3502             rtx right = XEXP (x, 1);
3503             if (GET_CODE (right) == CONST_INT
3504                 && CONST_OK_FOR_K (INTVAL (right)))
3505               *total = s390_cost->mhi;
3506             else if (GET_CODE (left) == SIGN_EXTEND)
3507               *total = s390_cost->mh;
3508             else
3509               *total = s390_cost->ms;  /* msr, ms, msy */
3510             break;
3511           }
3512         case DImode:
3513           {
3514             rtx left = XEXP (x, 0);
3515             rtx right = XEXP (x, 1);
3516             if (TARGET_ZARCH)
3517               {
3518                 if (GET_CODE (right) == CONST_INT
3519                     && CONST_OK_FOR_K (INTVAL (right)))
3520                   *total = s390_cost->mghi;
3521                 else if (GET_CODE (left) == SIGN_EXTEND)
3522                   *total = s390_cost->msgf;
3523                 else
3524                   *total = s390_cost->msg;  /* msgr, msg */
3525               }
3526             else /* TARGET_31BIT */
3527               {
3528                 if (GET_CODE (left) == SIGN_EXTEND
3529                     && GET_CODE (right) == SIGN_EXTEND)
3530                   /* mulsidi case: mr, m */
3531                   *total = s390_cost->m;
3532                 else if (GET_CODE (left) == ZERO_EXTEND
3533                          && GET_CODE (right) == ZERO_EXTEND
3534                          && TARGET_CPU_ZARCH)
3535                   /* umulsidi case: ml, mlr */
3536                   *total = s390_cost->ml;
3537                 else
3538                   /* Complex calculation is required.  */
3539                   *total = COSTS_N_INSNS (40);
3540               }
3541             break;
3542           }
3543         case SFmode:
3544         case DFmode:
3545           *total = s390_cost->mult_df;
3546           break;
3547         case TFmode:
3548           *total = s390_cost->mxbr;
3549           break;
3550         default:
3551           return false;
3552         }
3553       return false;
3554
3555     case FMA:
3556       switch (mode)
3557         {
3558         case DFmode:
3559           *total = s390_cost->madbr;
3560           break;
3561         case SFmode:
3562           *total = s390_cost->maebr;
3563           break;
3564         default:
3565           return false;
3566         }
3567       /* Negate in the third argument is free: FMSUB.  */
3568       if (GET_CODE (XEXP (x, 2)) == NEG)
3569         {
3570           *total += (rtx_cost (XEXP (x, 0), mode, FMA, 0, speed)
3571                      + rtx_cost (XEXP (x, 1), mode, FMA, 1, speed)
3572                      + rtx_cost (XEXP (XEXP (x, 2), 0), mode, FMA, 2, speed));
3573           return true;
3574         }
3575       return false;
3576
3577     case UDIV:
3578     case UMOD:
3579       if (mode == TImode)              /* 128 bit division */
3580         *total = s390_cost->dlgr;
3581       else if (mode == DImode)
3582         {
3583           rtx right = XEXP (x, 1);
3584           if (GET_CODE (right) == ZERO_EXTEND) /* 64 by 32 bit division */
3585             *total = s390_cost->dlr;
3586           else                                 /* 64 by 64 bit division */
3587             *total = s390_cost->dlgr;
3588         }
3589       else if (mode == SImode)         /* 32 bit division */
3590         *total = s390_cost->dlr;
3591       return false;
3592
3593     case DIV:
3594     case MOD:
3595       if (mode == DImode)
3596         {
3597           rtx right = XEXP (x, 1);
3598           if (GET_CODE (right) == ZERO_EXTEND) /* 64 by 32 bit division */
3599             if (TARGET_ZARCH)
3600               *total = s390_cost->dsgfr;
3601             else
3602               *total = s390_cost->dr;
3603           else                                 /* 64 by 64 bit division */
3604             *total = s390_cost->dsgr;
3605         }
3606       else if (mode == SImode)         /* 32 bit division */
3607         *total = s390_cost->dlr;
3608       else if (mode == SFmode)
3609         {
3610           *total = s390_cost->debr;
3611         }
3612       else if (mode == DFmode)
3613         {
3614           *total = s390_cost->ddbr;
3615         }
3616       else if (mode == TFmode)
3617         {
3618           *total = s390_cost->dxbr;
3619         }
3620       return false;
3621
3622     case SQRT:
3623       if (mode == SFmode)
3624         *total = s390_cost->sqebr;
3625       else if (mode == DFmode)
3626         *total = s390_cost->sqdbr;
3627       else /* TFmode */
3628         *total = s390_cost->sqxbr;
3629       return false;
3630
3631     case SIGN_EXTEND:
3632     case ZERO_EXTEND:
3633       if (outer_code == MULT || outer_code == DIV || outer_code == MOD
3634           || outer_code == PLUS || outer_code == MINUS
3635           || outer_code == COMPARE)
3636         *total = 0;
3637       return false;
3638
3639     case COMPARE:
3640       *total = COSTS_N_INSNS (1);
3641       if (GET_CODE (XEXP (x, 0)) == AND
3642           && GET_CODE (XEXP (x, 1)) == CONST_INT
3643           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
3644         {
3645           rtx op0 = XEXP (XEXP (x, 0), 0);
3646           rtx op1 = XEXP (XEXP (x, 0), 1);
3647           rtx op2 = XEXP (x, 1);
3648
3649           if (memory_operand (op0, GET_MODE (op0))
3650               && s390_tm_ccmode (op1, op2, 0) != VOIDmode)
3651             return true;
3652           if (register_operand (op0, GET_MODE (op0))
3653               && s390_tm_ccmode (op1, op2, 1) != VOIDmode)
3654             return true;
3655         }
3656       return false;
3657
3658     default:
3659       return false;
3660     }
3661 }
3662
3663 /* Return the cost of an address rtx ADDR.  */
3664
3665 static int
3666 s390_address_cost (rtx addr, machine_mode mode ATTRIBUTE_UNUSED,
3667                    addr_space_t as ATTRIBUTE_UNUSED,
3668                    bool speed ATTRIBUTE_UNUSED)
3669 {
3670   struct s390_address ad;
3671   if (!s390_decompose_address (addr, &ad))
3672     return 1000;
3673
3674   return ad.indx? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (1);
3675 }
3676
3677 /* Implement targetm.vectorize.builtin_vectorization_cost.  */
3678 static int
3679 s390_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
3680                                  tree vectype,
3681                                  int misalign ATTRIBUTE_UNUSED)
3682 {
3683   switch (type_of_cost)
3684     {
3685       case scalar_stmt:
3686       case scalar_load:
3687       case scalar_store:
3688       case vector_stmt:
3689       case vector_load:
3690       case vector_store:
3691       case vec_to_scalar:
3692       case scalar_to_vec:
3693       case cond_branch_not_taken:
3694       case vec_perm:
3695       case vec_promote_demote:
3696       case unaligned_load:
3697       case unaligned_store:
3698         return 1;
3699
3700       case cond_branch_taken:
3701         return 3;
3702
3703       case vec_construct:
3704         return TYPE_VECTOR_SUBPARTS (vectype) - 1;
3705
3706       default:
3707         gcc_unreachable ();
3708     }
3709 }
3710
3711 /* If OP is a SYMBOL_REF of a thread-local symbol, return its TLS mode,
3712    otherwise return 0.  */
3713
3714 int
3715 tls_symbolic_operand (rtx op)
3716 {
3717   if (GET_CODE (op) != SYMBOL_REF)
3718     return 0;
3719   return SYMBOL_REF_TLS_MODEL (op);
3720 }
3721 \f
3722 /* Split DImode access register reference REG (on 64-bit) into its constituent
3723    low and high parts, and store them into LO and HI.  Note that gen_lowpart/
3724    gen_highpart cannot be used as they assume all registers are word-sized,
3725    while our access registers have only half that size.  */
3726
3727 void
3728 s390_split_access_reg (rtx reg, rtx *lo, rtx *hi)
3729 {
3730   gcc_assert (TARGET_64BIT);
3731   gcc_assert (ACCESS_REG_P (reg));
3732   gcc_assert (GET_MODE (reg) == DImode);
3733   gcc_assert (!(REGNO (reg) & 1));
3734
3735   *lo = gen_rtx_REG (SImode, REGNO (reg) + 1);
3736   *hi = gen_rtx_REG (SImode, REGNO (reg));
3737 }
3738
3739 /* Return true if OP contains a symbol reference */
3740
3741 bool
3742 symbolic_reference_mentioned_p (rtx op)
3743 {
3744   const char *fmt;
3745   int i;
3746
3747   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
3748     return 1;
3749
3750   fmt = GET_RTX_FORMAT (GET_CODE (op));
3751   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
3752     {
3753       if (fmt[i] == 'E')
3754         {
3755           int j;
3756
3757           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
3758             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
3759               return 1;
3760         }
3761
3762       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
3763         return 1;
3764     }
3765
3766   return 0;
3767 }
3768
3769 /* Return true if OP contains a reference to a thread-local symbol.  */
3770
3771 bool
3772 tls_symbolic_reference_mentioned_p (rtx op)
3773 {
3774   const char *fmt;
3775   int i;
3776
3777   if (GET_CODE (op) == SYMBOL_REF)
3778     return tls_symbolic_operand (op);
3779
3780   fmt = GET_RTX_FORMAT (GET_CODE (op));
3781   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
3782     {
3783       if (fmt[i] == 'E')
3784         {
3785           int j;
3786
3787           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
3788             if (tls_symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
3789               return true;
3790         }
3791
3792       else if (fmt[i] == 'e' && tls_symbolic_reference_mentioned_p (XEXP (op, i)))
3793         return true;
3794     }
3795
3796   return false;
3797 }
3798
3799
3800 /* Return true if OP is a legitimate general operand when
3801    generating PIC code.  It is given that flag_pic is on
3802    and that OP satisfies CONSTANT_P.  */
3803
3804 int
3805 legitimate_pic_operand_p (rtx op)
3806 {
3807   /* Accept all non-symbolic constants.  */
3808   if (!SYMBOLIC_CONST (op))
3809     return 1;
3810
3811   /* Reject everything else; must be handled
3812      via emit_symbolic_move.  */
3813   return 0;
3814 }
3815
3816 /* Returns true if the constant value OP is a legitimate general operand.
3817    It is given that OP satisfies CONSTANT_P.  */
3818
3819 static bool
3820 s390_legitimate_constant_p (machine_mode mode, rtx op)
3821 {
3822   if (TARGET_VX && VECTOR_MODE_P (mode) && GET_CODE (op) == CONST_VECTOR)
3823     {
3824       if (GET_MODE_SIZE (mode) != 16)
3825         return 0;
3826
3827       if (!satisfies_constraint_j00 (op)
3828           && !satisfies_constraint_jm1 (op)
3829           && !satisfies_constraint_jKK (op)
3830           && !satisfies_constraint_jxx (op)
3831           && !satisfies_constraint_jyy (op))
3832         return 0;
3833     }
3834
3835   /* Accept all non-symbolic constants.  */
3836   if (!SYMBOLIC_CONST (op))
3837     return 1;
3838
3839   /* Accept immediate LARL operands.  */
3840   if (TARGET_CPU_ZARCH && larl_operand (op, mode))
3841     return 1;
3842
3843   /* Thread-local symbols are never legal constants.  This is
3844      so that emit_call knows that computing such addresses
3845      might require a function call.  */
3846   if (TLS_SYMBOLIC_CONST (op))
3847     return 0;
3848
3849   /* In the PIC case, symbolic constants must *not* be
3850      forced into the literal pool.  We accept them here,
3851      so that they will be handled by emit_symbolic_move.  */
3852   if (flag_pic)
3853     return 1;
3854
3855   /* All remaining non-PIC symbolic constants are
3856      forced into the literal pool.  */
3857   return 0;
3858 }
3859
3860 /* Determine if it's legal to put X into the constant pool.  This
3861    is not possible if X contains the address of a symbol that is
3862    not constant (TLS) or not known at final link time (PIC).  */
3863
3864 static bool
3865 s390_cannot_force_const_mem (machine_mode mode, rtx x)
3866 {
3867   switch (GET_CODE (x))
3868     {
3869     case CONST_INT:
3870     case CONST_DOUBLE:
3871     case CONST_WIDE_INT:
3872     case CONST_VECTOR:
3873       /* Accept all non-symbolic constants.  */
3874       return false;
3875
3876     case LABEL_REF:
3877       /* Labels are OK iff we are non-PIC.  */
3878       return flag_pic != 0;
3879
3880     case SYMBOL_REF:
3881       /* 'Naked' TLS symbol references are never OK,
3882          non-TLS symbols are OK iff we are non-PIC.  */
3883       if (tls_symbolic_operand (x))
3884         return true;
3885       else
3886         return flag_pic != 0;
3887
3888     case CONST:
3889       return s390_cannot_force_const_mem (mode, XEXP (x, 0));
3890     case PLUS:
3891     case MINUS:
3892       return s390_cannot_force_const_mem (mode, XEXP (x, 0))
3893              || s390_cannot_force_const_mem (mode, XEXP (x, 1));
3894
3895     case UNSPEC:
3896       switch (XINT (x, 1))
3897         {
3898         /* Only lt-relative or GOT-relative UNSPECs are OK.  */
3899         case UNSPEC_LTREL_OFFSET:
3900         case UNSPEC_GOT:
3901         case UNSPEC_GOTOFF:
3902         case UNSPEC_PLTOFF:
3903         case UNSPEC_TLSGD:
3904         case UNSPEC_TLSLDM:
3905         case UNSPEC_NTPOFF:
3906         case UNSPEC_DTPOFF:
3907         case UNSPEC_GOTNTPOFF:
3908         case UNSPEC_INDNTPOFF:
3909           return false;
3910
3911         /* If the literal pool shares the code section, be put
3912            execute template placeholders into the pool as well.  */
3913         case UNSPEC_INSN:
3914           return TARGET_CPU_ZARCH;
3915
3916         default:
3917           return true;
3918         }
3919       break;
3920
3921     default:
3922       gcc_unreachable ();
3923     }
3924 }
3925
3926 /* Returns true if the constant value OP is a legitimate general
3927    operand during and after reload.  The difference to
3928    legitimate_constant_p is that this function will not accept
3929    a constant that would need to be forced to the literal pool
3930    before it can be used as operand.
3931    This function accepts all constants which can be loaded directly
3932    into a GPR.  */
3933
3934 bool
3935 legitimate_reload_constant_p (rtx op)
3936 {
3937   /* Accept la(y) operands.  */
3938   if (GET_CODE (op) == CONST_INT
3939       && DISP_IN_RANGE (INTVAL (op)))
3940     return true;
3941
3942   /* Accept l(g)hi/l(g)fi operands.  */
3943   if (GET_CODE (op) == CONST_INT
3944       && (CONST_OK_FOR_K (INTVAL (op)) || CONST_OK_FOR_Os (INTVAL (op))))
3945     return true;
3946
3947   /* Accept lliXX operands.  */
3948   if (TARGET_ZARCH
3949       && GET_CODE (op) == CONST_INT
3950       && trunc_int_for_mode (INTVAL (op), word_mode) == INTVAL (op)
3951       && s390_single_part (op, word_mode, HImode, 0) >= 0)
3952   return true;
3953
3954   if (TARGET_EXTIMM
3955       && GET_CODE (op) == CONST_INT
3956       && trunc_int_for_mode (INTVAL (op), word_mode) == INTVAL (op)
3957       && s390_single_part (op, word_mode, SImode, 0) >= 0)
3958     return true;
3959
3960   /* Accept larl operands.  */
3961   if (TARGET_CPU_ZARCH
3962       && larl_operand (op, VOIDmode))
3963     return true;
3964
3965   /* Accept floating-point zero operands that fit into a single GPR.  */
3966   if (GET_CODE (op) == CONST_DOUBLE
3967       && s390_float_const_zero_p (op)
3968       && GET_MODE_SIZE (GET_MODE (op)) <= UNITS_PER_WORD)
3969     return true;
3970
3971   /* Accept double-word operands that can be split.  */
3972   if (GET_CODE (op) == CONST_WIDE_INT
3973       || (GET_CODE (op) == CONST_INT
3974           && trunc_int_for_mode (INTVAL (op), word_mode) != INTVAL (op)))
3975     {
3976       machine_mode dword_mode = word_mode == SImode ? DImode : TImode;
3977       rtx hi = operand_subword (op, 0, 0, dword_mode);
3978       rtx lo = operand_subword (op, 1, 0, dword_mode);
3979       return legitimate_reload_constant_p (hi)
3980              && legitimate_reload_constant_p (lo);
3981     }
3982
3983   /* Everything else cannot be handled without reload.  */
3984   return false;
3985 }
3986
3987 /* Returns true if the constant value OP is a legitimate fp operand
3988    during and after reload.
3989    This function accepts all constants which can be loaded directly
3990    into an FPR.  */
3991
3992 static bool
3993 legitimate_reload_fp_constant_p (rtx op)
3994 {
3995   /* Accept floating-point zero operands if the load zero instruction
3996      can be used.  Prior to z196 the load fp zero instruction caused a
3997      performance penalty if the result is used as BFP number.  */
3998   if (TARGET_Z196
3999       && GET_CODE (op) == CONST_DOUBLE
4000       && s390_float_const_zero_p (op))
4001     return true;
4002
4003   return false;
4004 }
4005
4006 /* Returns true if the constant value OP is a legitimate vector operand
4007    during and after reload.
4008    This function accepts all constants which can be loaded directly
4009    into an VR.  */
4010
4011 static bool
4012 legitimate_reload_vector_constant_p (rtx op)
4013 {
4014   if (TARGET_VX && GET_MODE_SIZE (GET_MODE (op)) == 16
4015       && (satisfies_constraint_j00 (op)
4016           || satisfies_constraint_jm1 (op)
4017           || satisfies_constraint_jKK (op)
4018           || satisfies_constraint_jxx (op)
4019           || satisfies_constraint_jyy (op)))
4020     return true;
4021
4022   return false;
4023 }
4024
4025 /* Given an rtx OP being reloaded into a reg required to be in class RCLASS,
4026    return the class of reg to actually use.  */
4027
4028 static reg_class_t
4029 s390_preferred_reload_class (rtx op, reg_class_t rclass)
4030 {
4031   switch (GET_CODE (op))
4032     {
4033       /* Constants we cannot reload into general registers
4034          must be forced into the literal pool.  */
4035       case CONST_VECTOR:
4036       case CONST_DOUBLE:
4037       case CONST_INT:
4038       case CONST_WIDE_INT:
4039         if (reg_class_subset_p (GENERAL_REGS, rclass)
4040             && legitimate_reload_constant_p (op))
4041           return GENERAL_REGS;
4042         else if (reg_class_subset_p (ADDR_REGS, rclass)
4043                  && legitimate_reload_constant_p (op))
4044           return ADDR_REGS;
4045         else if (reg_class_subset_p (FP_REGS, rclass)
4046                  && legitimate_reload_fp_constant_p (op))
4047           return FP_REGS;
4048         else if (reg_class_subset_p (VEC_REGS, rclass)
4049                  && legitimate_reload_vector_constant_p (op))
4050           return VEC_REGS;
4051
4052         return NO_REGS;
4053
4054       /* If a symbolic constant or a PLUS is reloaded,
4055          it is most likely being used as an address, so
4056          prefer ADDR_REGS.  If 'class' is not a superset
4057          of ADDR_REGS, e.g. FP_REGS, reject this reload.  */
4058       case CONST:
4059         /* Symrefs cannot be pushed into the literal pool with -fPIC
4060            so we *MUST NOT* return NO_REGS for these cases
4061            (s390_cannot_force_const_mem will return true).  
4062
4063            On the other hand we MUST return NO_REGS for symrefs with
4064            invalid addend which might have been pushed to the literal
4065            pool (no -fPIC).  Usually we would expect them to be
4066            handled via secondary reload but this does not happen if
4067            they are used as literal pool slot replacement in reload
4068            inheritance (see emit_input_reload_insns).  */
4069         if (TARGET_CPU_ZARCH
4070             && GET_CODE (XEXP (op, 0)) == PLUS
4071             && GET_CODE (XEXP (XEXP(op, 0), 0)) == SYMBOL_REF
4072             && GET_CODE (XEXP (XEXP(op, 0), 1)) == CONST_INT)
4073           {
4074             if (flag_pic && reg_class_subset_p (ADDR_REGS, rclass))
4075               return ADDR_REGS;
4076             else
4077               return NO_REGS;
4078           }
4079         /* fallthrough */
4080       case LABEL_REF:
4081       case SYMBOL_REF:
4082         if (!legitimate_reload_constant_p (op))
4083           return NO_REGS;
4084         /* fallthrough */
4085       case PLUS:
4086         /* load address will be used.  */
4087         if (reg_class_subset_p (ADDR_REGS, rclass))
4088           return ADDR_REGS;
4089         else
4090           return NO_REGS;
4091
4092       default:
4093         break;
4094     }
4095
4096   return rclass;
4097 }
4098
4099 /* Return true if ADDR is SYMBOL_REF + addend with addend being a
4100    multiple of ALIGNMENT and the SYMBOL_REF being naturally
4101    aligned.  */
4102
4103 bool
4104 s390_check_symref_alignment (rtx addr, HOST_WIDE_INT alignment)
4105 {
4106   HOST_WIDE_INT addend;
4107   rtx symref;
4108
4109   /* The "required alignment" might be 0 (e.g. for certain structs
4110      accessed via BLKmode).  Early abort in this case, as well as when
4111      an alignment > 8 is required.  */
4112   if (alignment < 2 || alignment > 8)
4113     return false;
4114
4115   if (!s390_loadrelative_operand_p (addr, &symref, &addend))
4116     return false;
4117
4118   if (addend & (alignment - 1))
4119     return false;
4120
4121   if (GET_CODE (symref) == SYMBOL_REF)
4122     {
4123       /* We have load-relative instructions for 2-byte, 4-byte, and
4124          8-byte alignment so allow only these.  */
4125       switch (alignment)
4126         {
4127         case 8: return !SYMBOL_FLAG_NOTALIGN8_P (symref);
4128         case 4: return !SYMBOL_FLAG_NOTALIGN4_P (symref);
4129         case 2: return !SYMBOL_FLAG_NOTALIGN2_P (symref);
4130         default: return false;
4131         }
4132     }
4133
4134   if (GET_CODE (symref) == UNSPEC
4135       && alignment <= UNITS_PER_LONG)
4136     return true;
4137
4138   return false;
4139 }
4140
4141 /* ADDR is moved into REG using larl.  If ADDR isn't a valid larl
4142    operand SCRATCH is used to reload the even part of the address and
4143    adding one.  */
4144
4145 void
4146 s390_reload_larl_operand (rtx reg, rtx addr, rtx scratch)
4147 {
4148   HOST_WIDE_INT addend;
4149   rtx symref;
4150
4151   if (!s390_loadrelative_operand_p (addr, &symref, &addend))
4152     gcc_unreachable ();
4153
4154   if (!(addend & 1))
4155     /* Easy case.  The addend is even so larl will do fine.  */
4156     emit_move_insn (reg, addr);
4157   else
4158     {
4159       /* We can leave the scratch register untouched if the target
4160          register is a valid base register.  */
4161       if (REGNO (reg) < FIRST_PSEUDO_REGISTER
4162           && REGNO_REG_CLASS (REGNO (reg)) == ADDR_REGS)
4163         scratch = reg;
4164
4165       gcc_assert (REGNO (scratch) < FIRST_PSEUDO_REGISTER);
4166       gcc_assert (REGNO_REG_CLASS (REGNO (scratch)) == ADDR_REGS);
4167
4168       if (addend != 1)
4169         emit_move_insn (scratch,
4170                         gen_rtx_CONST (Pmode,
4171                                        gen_rtx_PLUS (Pmode, symref,
4172                                                      GEN_INT (addend - 1))));
4173       else
4174         emit_move_insn (scratch, symref);
4175
4176       /* Increment the address using la in order to avoid clobbering cc.  */
4177       s390_load_address (reg, gen_rtx_PLUS (Pmode, scratch, const1_rtx));
4178     }
4179 }
4180
4181 /* Generate what is necessary to move between REG and MEM using
4182    SCRATCH.  The direction is given by TOMEM.  */
4183
4184 void
4185 s390_reload_symref_address (rtx reg, rtx mem, rtx scratch, bool tomem)
4186 {
4187   /* Reload might have pulled a constant out of the literal pool.
4188      Force it back in.  */
4189   if (CONST_INT_P (mem) || GET_CODE (mem) == CONST_DOUBLE
4190       || GET_CODE (mem) == CONST_WIDE_INT
4191       || GET_CODE (mem) == CONST_VECTOR
4192       || GET_CODE (mem) == CONST)
4193     mem = force_const_mem (GET_MODE (reg), mem);
4194
4195   gcc_assert (MEM_P (mem));
4196
4197   /* For a load from memory we can leave the scratch register
4198      untouched if the target register is a valid base register.  */
4199   if (!tomem
4200       && REGNO (reg) < FIRST_PSEUDO_REGISTER
4201       && REGNO_REG_CLASS (REGNO (reg)) == ADDR_REGS
4202       && GET_MODE (reg) == GET_MODE (scratch))
4203     scratch = reg;
4204
4205   /* Load address into scratch register.  Since we can't have a
4206      secondary reload for a secondary reload we have to cover the case
4207      where larl would need a secondary reload here as well.  */
4208   s390_reload_larl_operand (scratch, XEXP (mem, 0), scratch);
4209
4210   /* Now we can use a standard load/store to do the move.  */
4211   if (tomem)
4212     emit_move_insn (replace_equiv_address (mem, scratch), reg);
4213   else
4214     emit_move_insn (reg, replace_equiv_address (mem, scratch));
4215 }
4216
4217 /* Inform reload about cases where moving X with a mode MODE to a register in
4218    RCLASS requires an extra scratch or immediate register.  Return the class
4219    needed for the immediate register.  */
4220
4221 static reg_class_t
4222 s390_secondary_reload (bool in_p, rtx x, reg_class_t rclass_i,
4223                        machine_mode mode, secondary_reload_info *sri)
4224 {
4225   enum reg_class rclass = (enum reg_class) rclass_i;
4226
4227   /* Intermediate register needed.  */
4228   if (reg_classes_intersect_p (CC_REGS, rclass))
4229     return GENERAL_REGS;
4230
4231   if (TARGET_VX)
4232     {
4233       /* The vst/vl vector move instructions allow only for short
4234          displacements.  */
4235       if (MEM_P (x)
4236           && GET_CODE (XEXP (x, 0)) == PLUS
4237           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4238           && !SHORT_DISP_IN_RANGE(INTVAL (XEXP (XEXP (x, 0), 1)))
4239           && reg_class_subset_p (rclass, VEC_REGS)
4240           && (!reg_class_subset_p (rclass, FP_REGS)
4241               || (GET_MODE_SIZE (mode) > 8
4242                   && s390_class_max_nregs (FP_REGS, mode) == 1)))
4243         {
4244           if (in_p)
4245             sri->icode = (TARGET_64BIT ?
4246                           CODE_FOR_reloaddi_la_in :
4247                           CODE_FOR_reloadsi_la_in);
4248           else
4249             sri->icode = (TARGET_64BIT ?
4250                           CODE_FOR_reloaddi_la_out :
4251                           CODE_FOR_reloadsi_la_out);
4252         }
4253     }
4254
4255   if (TARGET_Z10)
4256     {
4257       HOST_WIDE_INT offset;
4258       rtx symref;
4259
4260       /* On z10 several optimizer steps may generate larl operands with
4261          an odd addend.  */
4262       if (in_p
4263           && s390_loadrelative_operand_p (x, &symref, &offset)
4264           && mode == Pmode
4265           && !SYMBOL_FLAG_NOTALIGN2_P (symref)
4266           && (offset & 1) == 1)
4267         sri->icode = ((mode == DImode) ? CODE_FOR_reloaddi_larl_odd_addend_z10
4268                       : CODE_FOR_reloadsi_larl_odd_addend_z10);
4269
4270       /* Handle all the (mem (symref)) accesses we cannot use the z10
4271          instructions for.  */
4272       if (MEM_P (x)
4273           && s390_loadrelative_operand_p (XEXP (x, 0), NULL, NULL)
4274           && (mode == QImode
4275               || !reg_class_subset_p (rclass, GENERAL_REGS)
4276               || GET_MODE_SIZE (mode) > UNITS_PER_WORD
4277               || !s390_check_symref_alignment (XEXP (x, 0),
4278                                                GET_MODE_SIZE (mode))))
4279         {
4280 #define __SECONDARY_RELOAD_CASE(M,m)                                    \
4281           case M##mode:                                                 \
4282             if (TARGET_64BIT)                                           \
4283               sri->icode = in_p ? CODE_FOR_reload##m##di_toreg_z10 :    \
4284                                   CODE_FOR_reload##m##di_tomem_z10;     \
4285             else                                                        \
4286               sri->icode = in_p ? CODE_FOR_reload##m##si_toreg_z10 :    \
4287                                   CODE_FOR_reload##m##si_tomem_z10;     \
4288           break;
4289
4290           switch (GET_MODE (x))
4291             {
4292               __SECONDARY_RELOAD_CASE (QI, qi);
4293               __SECONDARY_RELOAD_CASE (HI, hi);
4294               __SECONDARY_RELOAD_CASE (SI, si);
4295               __SECONDARY_RELOAD_CASE (DI, di);
4296               __SECONDARY_RELOAD_CASE (TI, ti);
4297               __SECONDARY_RELOAD_CASE (SF, sf);
4298               __SECONDARY_RELOAD_CASE (DF, df);
4299               __SECONDARY_RELOAD_CASE (TF, tf);
4300               __SECONDARY_RELOAD_CASE (SD, sd);
4301               __SECONDARY_RELOAD_CASE (DD, dd);
4302               __SECONDARY_RELOAD_CASE (TD, td);
4303               __SECONDARY_RELOAD_CASE (V1QI, v1qi);
4304               __SECONDARY_RELOAD_CASE (V2QI, v2qi);
4305               __SECONDARY_RELOAD_CASE (V4QI, v4qi);
4306               __SECONDARY_RELOAD_CASE (V8QI, v8qi);
4307               __SECONDARY_RELOAD_CASE (V16QI, v16qi);
4308               __SECONDARY_RELOAD_CASE (V1HI, v1hi);
4309               __SECONDARY_RELOAD_CASE (V2HI, v2hi);
4310               __SECONDARY_RELOAD_CASE (V4HI, v4hi);
4311               __SECONDARY_RELOAD_CASE (V8HI, v8hi);
4312               __SECONDARY_RELOAD_CASE (V1SI, v1si);
4313               __SECONDARY_RELOAD_CASE (V2SI, v2si);
4314               __SECONDARY_RELOAD_CASE (V4SI, v4si);
4315               __SECONDARY_RELOAD_CASE (V1DI, v1di);
4316               __SECONDARY_RELOAD_CASE (V2DI, v2di);
4317               __SECONDARY_RELOAD_CASE (V1TI, v1ti);
4318               __SECONDARY_RELOAD_CASE (V1SF, v1sf);
4319               __SECONDARY_RELOAD_CASE (V2SF, v2sf);
4320               __SECONDARY_RELOAD_CASE (V4SF, v4sf);
4321               __SECONDARY_RELOAD_CASE (V1DF, v1df);
4322               __SECONDARY_RELOAD_CASE (V2DF, v2df);
4323               __SECONDARY_RELOAD_CASE (V1TF, v1tf);
4324             default:
4325               gcc_unreachable ();
4326             }
4327 #undef __SECONDARY_RELOAD_CASE
4328         }
4329     }
4330
4331   /* We need a scratch register when loading a PLUS expression which
4332      is not a legitimate operand of the LOAD ADDRESS instruction.  */
4333   /* LRA can deal with transformation of plus op very well -- so we
4334      don't need to prompt LRA in this case.  */
4335   if (! lra_in_progress && in_p && s390_plus_operand (x, mode))
4336     sri->icode = (TARGET_64BIT ?
4337                   CODE_FOR_reloaddi_plus : CODE_FOR_reloadsi_plus);
4338
4339   /* Performing a multiword move from or to memory we have to make sure the
4340      second chunk in memory is addressable without causing a displacement
4341      overflow.  If that would be the case we calculate the address in
4342      a scratch register.  */
4343   if (MEM_P (x)
4344       && GET_CODE (XEXP (x, 0)) == PLUS
4345       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4346       && !DISP_IN_RANGE (INTVAL (XEXP (XEXP (x, 0), 1))
4347                          + GET_MODE_SIZE (mode) - 1))
4348     {
4349       /* For GENERAL_REGS a displacement overflow is no problem if occurring
4350          in a s_operand address since we may fallback to lm/stm.  So we only
4351          have to care about overflows in the b+i+d case.  */
4352       if ((reg_classes_intersect_p (GENERAL_REGS, rclass)
4353            && s390_class_max_nregs (GENERAL_REGS, mode) > 1
4354            && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS)
4355           /* For FP_REGS no lm/stm is available so this check is triggered
4356              for displacement overflows in b+i+d and b+d like addresses.  */
4357           || (reg_classes_intersect_p (FP_REGS, rclass)
4358               && s390_class_max_nregs (FP_REGS, mode) > 1))
4359         {
4360           if (in_p)
4361             sri->icode = (TARGET_64BIT ?
4362                           CODE_FOR_reloaddi_la_in :
4363                           CODE_FOR_reloadsi_la_in);
4364           else
4365             sri->icode = (TARGET_64BIT ?
4366                           CODE_FOR_reloaddi_la_out :
4367                           CODE_FOR_reloadsi_la_out);
4368         }
4369     }
4370
4371   /* A scratch address register is needed when a symbolic constant is
4372      copied to r0 compiling with -fPIC.  In other cases the target
4373      register might be used as temporary (see legitimize_pic_address).  */
4374   if (in_p && SYMBOLIC_CONST (x) && flag_pic == 2 && rclass != ADDR_REGS)
4375     sri->icode = (TARGET_64BIT ?
4376                   CODE_FOR_reloaddi_PIC_addr :
4377                   CODE_FOR_reloadsi_PIC_addr);
4378
4379   /* Either scratch or no register needed.  */
4380   return NO_REGS;
4381 }
4382
4383 /* Generate code to load SRC, which is PLUS that is not a
4384    legitimate operand for the LA instruction, into TARGET.
4385    SCRATCH may be used as scratch register.  */
4386
4387 void
4388 s390_expand_plus_operand (rtx target, rtx src,
4389                           rtx scratch)
4390 {
4391   rtx sum1, sum2;
4392   struct s390_address ad;
4393
4394   /* src must be a PLUS; get its two operands.  */
4395   gcc_assert (GET_CODE (src) == PLUS);
4396   gcc_assert (GET_MODE (src) == Pmode);
4397
4398   /* Check if any of the two operands is already scheduled
4399      for replacement by reload.  This can happen e.g. when
4400      float registers occur in an address.  */
4401   sum1 = find_replacement (&XEXP (src, 0));
4402   sum2 = find_replacement (&XEXP (src, 1));
4403   src = gen_rtx_PLUS (Pmode, sum1, sum2);
4404
4405   /* If the address is already strictly valid, there's nothing to do.  */
4406   if (!s390_decompose_address (src, &ad)
4407       || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
4408       || (ad.indx && !REGNO_OK_FOR_INDEX_P (REGNO (ad.indx))))
4409     {
4410       /* Otherwise, one of the operands cannot be an address register;
4411          we reload its value into the scratch register.  */
4412       if (true_regnum (sum1) < 1 || true_regnum (sum1) > 15)
4413         {
4414           emit_move_insn (scratch, sum1);
4415           sum1 = scratch;
4416         }
4417       if (true_regnum (sum2) < 1 || true_regnum (sum2) > 15)
4418         {
4419           emit_move_insn (scratch, sum2);
4420           sum2 = scratch;
4421         }
4422
4423       /* According to the way these invalid addresses are generated
4424          in reload.c, it should never happen (at least on s390) that
4425          *neither* of the PLUS components, after find_replacements
4426          was applied, is an address register.  */
4427       if (sum1 == scratch && sum2 == scratch)
4428         {
4429           debug_rtx (src);
4430           gcc_unreachable ();
4431         }
4432
4433       src = gen_rtx_PLUS (Pmode, sum1, sum2);
4434     }
4435
4436   /* Emit the LOAD ADDRESS pattern.  Note that reload of PLUS
4437      is only ever performed on addresses, so we can mark the
4438      sum as legitimate for LA in any case.  */
4439   s390_load_address (target, src);
4440 }
4441
4442
4443 /* Return true if ADDR is a valid memory address.
4444    STRICT specifies whether strict register checking applies.  */
4445
4446 static bool
4447 s390_legitimate_address_p (machine_mode mode, rtx addr, bool strict)
4448 {
4449   struct s390_address ad;
4450
4451   if (TARGET_Z10
4452       && larl_operand (addr, VOIDmode)
4453       && (mode == VOIDmode
4454           || s390_check_symref_alignment (addr, GET_MODE_SIZE (mode))))
4455     return true;
4456
4457   if (!s390_decompose_address (addr, &ad))
4458     return false;
4459
4460   if (strict)
4461     {
4462       if (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
4463         return false;
4464
4465       if (ad.indx && !REGNO_OK_FOR_INDEX_P (REGNO (ad.indx)))
4466         return false;
4467     }
4468   else
4469     {
4470       if (ad.base
4471           && !(REGNO (ad.base) >= FIRST_PSEUDO_REGISTER
4472                || REGNO_REG_CLASS (REGNO (ad.base)) == ADDR_REGS))
4473         return false;
4474
4475       if (ad.indx
4476           && !(REGNO (ad.indx) >= FIRST_PSEUDO_REGISTER
4477                || REGNO_REG_CLASS (REGNO (ad.indx)) == ADDR_REGS))
4478           return false;
4479     }
4480   return true;
4481 }
4482
4483 /* Return true if OP is a valid operand for the LA instruction.
4484    In 31-bit, we need to prove that the result is used as an
4485    address, as LA performs only a 31-bit addition.  */
4486
4487 bool
4488 legitimate_la_operand_p (rtx op)
4489 {
4490   struct s390_address addr;
4491   if (!s390_decompose_address (op, &addr))
4492     return false;
4493
4494   return (TARGET_64BIT || addr.pointer);
4495 }
4496
4497 /* Return true if it is valid *and* preferable to use LA to
4498    compute the sum of OP1 and OP2.  */
4499
4500 bool
4501 preferred_la_operand_p (rtx op1, rtx op2)
4502 {
4503   struct s390_address addr;
4504
4505   if (op2 != const0_rtx)
4506     op1 = gen_rtx_PLUS (Pmode, op1, op2);
4507
4508   if (!s390_decompose_address (op1, &addr))
4509     return false;
4510   if (addr.base && !REGNO_OK_FOR_BASE_P (REGNO (addr.base)))
4511     return false;
4512   if (addr.indx && !REGNO_OK_FOR_INDEX_P (REGNO (addr.indx)))
4513     return false;
4514
4515   /* Avoid LA instructions with index register on z196; it is
4516      preferable to use regular add instructions when possible.
4517      Starting with zEC12 the la with index register is "uncracked"
4518      again.  */
4519   if (addr.indx && s390_tune == PROCESSOR_2817_Z196)
4520     return false;
4521
4522   if (!TARGET_64BIT && !addr.pointer)
4523     return false;
4524
4525   if (addr.pointer)
4526     return true;
4527
4528   if ((addr.base && REG_P (addr.base) && REG_POINTER (addr.base))
4529       || (addr.indx && REG_P (addr.indx) && REG_POINTER (addr.indx)))
4530     return true;
4531
4532   return false;
4533 }
4534
4535 /* Emit a forced load-address operation to load SRC into DST.
4536    This will use the LOAD ADDRESS instruction even in situations
4537    where legitimate_la_operand_p (SRC) returns false.  */
4538
4539 void
4540 s390_load_address (rtx dst, rtx src)
4541 {
4542   if (TARGET_64BIT)
4543     emit_move_insn (dst, src);
4544   else
4545     emit_insn (gen_force_la_31 (dst, src));
4546 }
4547
4548 /* Return a legitimate reference for ORIG (an address) using the
4549    register REG.  If REG is 0, a new pseudo is generated.
4550
4551    There are two types of references that must be handled:
4552
4553    1. Global data references must load the address from the GOT, via
4554       the PIC reg.  An insn is emitted to do this load, and the reg is
4555       returned.
4556
4557    2. Static data references, constant pool addresses, and code labels
4558       compute the address as an offset from the GOT, whose base is in
4559       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
4560       differentiate them from global data objects.  The returned
4561       address is the PIC reg + an unspec constant.
4562
4563    TARGET_LEGITIMIZE_ADDRESS_P rejects symbolic references unless the PIC
4564    reg also appears in the address.  */
4565
4566 rtx
4567 legitimize_pic_address (rtx orig, rtx reg)
4568 {
4569   rtx addr = orig;
4570   rtx addend = const0_rtx;
4571   rtx new_rtx = orig;
4572
4573   gcc_assert (!TLS_SYMBOLIC_CONST (addr));
4574
4575   if (GET_CODE (addr) == CONST)
4576     addr = XEXP (addr, 0);
4577
4578   if (GET_CODE (addr) == PLUS)
4579     {
4580       addend = XEXP (addr, 1);
4581       addr = XEXP (addr, 0);
4582     }
4583
4584   if ((GET_CODE (addr) == LABEL_REF
4585        || (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (addr))
4586        || (GET_CODE (addr) == UNSPEC &&
4587            (XINT (addr, 1) == UNSPEC_GOTENT
4588             || (TARGET_CPU_ZARCH && XINT (addr, 1) == UNSPEC_PLT))))
4589       && GET_CODE (addend) == CONST_INT)
4590     {
4591       /* This can be locally addressed.  */
4592
4593       /* larl_operand requires UNSPECs to be wrapped in a const rtx.  */
4594       rtx const_addr = (GET_CODE (addr) == UNSPEC ?
4595                         gen_rtx_CONST (Pmode, addr) : addr);
4596
4597       if (TARGET_CPU_ZARCH
4598           && larl_operand (const_addr, VOIDmode)
4599           && INTVAL (addend) < (HOST_WIDE_INT)1 << 31
4600           && INTVAL (addend) >= -((HOST_WIDE_INT)1 << 31))
4601         {
4602           if (INTVAL (addend) & 1)
4603             {
4604               /* LARL can't handle odd offsets, so emit a pair of LARL
4605                  and LA.  */
4606               rtx temp = reg? reg : gen_reg_rtx (Pmode);
4607
4608               if (!DISP_IN_RANGE (INTVAL (addend)))
4609                 {
4610                   HOST_WIDE_INT even = INTVAL (addend) - 1;
4611                   addr = gen_rtx_PLUS (Pmode, addr, GEN_INT (even));
4612                   addr = gen_rtx_CONST (Pmode, addr);
4613                   addend = const1_rtx;
4614                 }
4615
4616               emit_move_insn (temp, addr);
4617               new_rtx = gen_rtx_PLUS (Pmode, temp, addend);
4618
4619               if (reg != 0)
4620                 {
4621                   s390_load_address (reg, new_rtx);
4622                   new_rtx = reg;
4623                 }
4624             }
4625           else
4626             {
4627               /* If the offset is even, we can just use LARL.  This
4628                  will happen automatically.  */
4629             }
4630         }
4631       else
4632         {
4633           /* No larl - Access local symbols relative to the GOT.  */
4634
4635           rtx temp = reg? reg : gen_reg_rtx (Pmode);
4636
4637           if (reload_in_progress || reload_completed)
4638             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
4639
4640           addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
4641           if (addend != const0_rtx)
4642             addr = gen_rtx_PLUS (Pmode, addr, addend);
4643           addr = gen_rtx_CONST (Pmode, addr);
4644           addr = force_const_mem (Pmode, addr);
4645           emit_move_insn (temp, addr);
4646
4647           new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
4648           if (reg != 0)
4649             {
4650               s390_load_address (reg, new_rtx);
4651               new_rtx = reg;
4652             }
4653         }
4654     }
4655   else if (GET_CODE (addr) == SYMBOL_REF && addend == const0_rtx)
4656     {
4657       /* A non-local symbol reference without addend.
4658
4659          The symbol ref is wrapped into an UNSPEC to make sure the
4660          proper operand modifier (@GOT or @GOTENT) will be emitted.
4661          This will tell the linker to put the symbol into the GOT.
4662
4663          Additionally the code dereferencing the GOT slot is emitted here.
4664
4665          An addend to the symref needs to be added afterwards.
4666          legitimize_pic_address calls itself recursively to handle
4667          that case.  So no need to do it here.  */
4668
4669       if (reg == 0)
4670         reg = gen_reg_rtx (Pmode);
4671
4672       if (TARGET_Z10)
4673         {
4674           /* Use load relative if possible.
4675              lgrl <target>, sym@GOTENT  */
4676           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTENT);
4677           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
4678           new_rtx = gen_const_mem (GET_MODE (reg), new_rtx);
4679
4680           emit_move_insn (reg, new_rtx);
4681           new_rtx = reg;
4682         }
4683       else if (flag_pic == 1)
4684         {
4685           /* Assume GOT offset is a valid displacement operand (< 4k
4686              or < 512k with z990).  This is handled the same way in
4687              both 31- and 64-bit code (@GOT).
4688              lg <target>, sym@GOT(r12)  */
4689
4690           if (reload_in_progress || reload_completed)
4691             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
4692
4693           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
4694           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
4695           new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
4696           new_rtx = gen_const_mem (Pmode, new_rtx);
4697           emit_move_insn (reg, new_rtx);
4698           new_rtx = reg;
4699         }
4700       else if (TARGET_CPU_ZARCH)
4701         {
4702           /* If the GOT offset might be >= 4k, we determine the position
4703              of the GOT entry via a PC-relative LARL (@GOTENT).
4704              larl temp, sym@GOTENT
4705              lg   <target>, 0(temp) */
4706
4707           rtx temp = reg ? reg : gen_reg_rtx (Pmode);
4708
4709           gcc_assert (REGNO (temp) >= FIRST_PSEUDO_REGISTER
4710                       || REGNO_REG_CLASS (REGNO (temp)) == ADDR_REGS);
4711
4712           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTENT);
4713           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
4714           emit_move_insn (temp, new_rtx);
4715
4716           new_rtx = gen_const_mem (Pmode, temp);
4717           emit_move_insn (reg, new_rtx);
4718
4719           new_rtx = reg;
4720         }
4721       else
4722         {
4723           /* If the GOT offset might be >= 4k, we have to load it
4724              from the literal pool (@GOT).
4725
4726              lg temp, lit-litbase(r13)
4727              lg <target>, 0(temp)
4728              lit:  .long sym@GOT  */
4729
4730           rtx temp = reg ? reg : gen_reg_rtx (Pmode);
4731
4732           gcc_assert (REGNO (temp) >= FIRST_PSEUDO_REGISTER
4733                       || REGNO_REG_CLASS (REGNO (temp)) == ADDR_REGS);
4734
4735           if (reload_in_progress || reload_completed)
4736             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
4737
4738           addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
4739           addr = gen_rtx_CONST (Pmode, addr);
4740           addr = force_const_mem (Pmode, addr);
4741           emit_move_insn (temp, addr);
4742
4743           new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
4744           new_rtx = gen_const_mem (Pmode, new_rtx);
4745           emit_move_insn (reg, new_rtx);
4746           new_rtx = reg;
4747         }
4748     }
4749   else if (GET_CODE (addr) == UNSPEC && GET_CODE (addend) == CONST_INT)
4750     {
4751       gcc_assert (XVECLEN (addr, 0) == 1);
4752       switch (XINT (addr, 1))
4753         {
4754           /* These address symbols (or PLT slots) relative to the GOT
4755              (not GOT slots!).  In general this will exceed the
4756              displacement range so these value belong into the literal
4757              pool.  */
4758         case UNSPEC_GOTOFF:
4759         case UNSPEC_PLTOFF:
4760           new_rtx = force_const_mem (Pmode, orig);
4761           break;
4762
4763           /* For -fPIC the GOT size might exceed the displacement
4764              range so make sure the value is in the literal pool.  */
4765         case UNSPEC_GOT:
4766           if (flag_pic == 2)
4767             new_rtx = force_const_mem (Pmode, orig);
4768           break;
4769
4770           /* For @GOTENT larl is used.  This is handled like local
4771              symbol refs.  */
4772         case UNSPEC_GOTENT:
4773           gcc_unreachable ();
4774           break;
4775
4776           /* @PLT is OK as is on 64-bit, must be converted to
4777              GOT-relative @PLTOFF on 31-bit.  */
4778         case UNSPEC_PLT:
4779           if (!TARGET_CPU_ZARCH)
4780             {
4781               rtx temp = reg? reg : gen_reg_rtx (Pmode);
4782
4783               if (reload_in_progress || reload_completed)
4784                 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
4785
4786               addr = XVECEXP (addr, 0, 0);
4787               addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr),
4788                                      UNSPEC_PLTOFF);
4789               if (addend != const0_rtx)
4790                 addr = gen_rtx_PLUS (Pmode, addr, addend);
4791               addr = gen_rtx_CONST (Pmode, addr);
4792               addr = force_const_mem (Pmode, addr);
4793               emit_move_insn (temp, addr);
4794
4795               new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
4796               if (reg != 0)
4797                 {
4798                   s390_load_address (reg, new_rtx);
4799                   new_rtx = reg;
4800                 }
4801             }
4802           else
4803             /* On 64 bit larl can be used.  This case is handled like
4804                local symbol refs.  */
4805             gcc_unreachable ();
4806           break;
4807
4808           /* Everything else cannot happen.  */
4809         default:
4810           gcc_unreachable ();
4811         }
4812     }
4813   else if (addend != const0_rtx)
4814     {
4815       /* Otherwise, compute the sum.  */
4816
4817       rtx base = legitimize_pic_address (addr, reg);
4818       new_rtx  = legitimize_pic_address (addend,
4819                                          base == reg ? NULL_RTX : reg);
4820       if (GET_CODE (new_rtx) == CONST_INT)
4821         new_rtx = plus_constant (Pmode, base, INTVAL (new_rtx));
4822       else
4823         {
4824           if (GET_CODE (new_rtx) == PLUS && CONSTANT_P (XEXP (new_rtx, 1)))
4825             {
4826               base = gen_rtx_PLUS (Pmode, base, XEXP (new_rtx, 0));
4827               new_rtx = XEXP (new_rtx, 1);
4828             }
4829           new_rtx = gen_rtx_PLUS (Pmode, base, new_rtx);
4830         }
4831
4832       if (GET_CODE (new_rtx) == CONST)
4833         new_rtx = XEXP (new_rtx, 0);
4834       new_rtx = force_operand (new_rtx, 0);
4835     }
4836
4837   return new_rtx;
4838 }
4839
4840 /* Load the thread pointer into a register.  */
4841
4842 rtx
4843 s390_get_thread_pointer (void)
4844 {
4845   rtx tp = gen_reg_rtx (Pmode);
4846
4847   emit_move_insn (tp, gen_rtx_REG (Pmode, TP_REGNUM));
4848   mark_reg_pointer (tp, BITS_PER_WORD);
4849
4850   return tp;
4851 }
4852
4853 /* Emit a tls call insn. The call target is the SYMBOL_REF stored
4854    in s390_tls_symbol which always refers to __tls_get_offset.
4855    The returned offset is written to RESULT_REG and an USE rtx is
4856    generated for TLS_CALL.  */
4857
4858 static GTY(()) rtx s390_tls_symbol;
4859
4860 static void
4861 s390_emit_tls_call_insn (rtx result_reg, rtx tls_call)
4862 {
4863   rtx insn;
4864
4865   if (!flag_pic)
4866     emit_insn (s390_load_got ());
4867
4868   if (!s390_tls_symbol)
4869     s390_tls_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tls_get_offset");
4870
4871   insn = s390_emit_call (s390_tls_symbol, tls_call, result_reg,
4872                          gen_rtx_REG (Pmode, RETURN_REGNUM));
4873
4874   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), result_reg);
4875   RTL_CONST_CALL_P (insn) = 1;
4876 }
4877
4878 /* ADDR contains a thread-local SYMBOL_REF.  Generate code to compute
4879    this (thread-local) address.  REG may be used as temporary.  */
4880
4881 static rtx
4882 legitimize_tls_address (rtx addr, rtx reg)
4883 {
4884   rtx new_rtx, tls_call, temp, base, r2;
4885   rtx_insn *insn;
4886
4887   if (GET_CODE (addr) == SYMBOL_REF)
4888     switch (tls_symbolic_operand (addr))
4889       {
4890       case TLS_MODEL_GLOBAL_DYNAMIC:
4891         start_sequence ();
4892         r2 = gen_rtx_REG (Pmode, 2);
4893         tls_call = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_TLSGD);
4894         new_rtx = gen_rtx_CONST (Pmode, tls_call);
4895         new_rtx = force_const_mem (Pmode, new_rtx);
4896         emit_move_insn (r2, new_rtx);
4897         s390_emit_tls_call_insn (r2, tls_call);
4898         insn = get_insns ();
4899         end_sequence ();
4900
4901         new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_NTPOFF);
4902         temp = gen_reg_rtx (Pmode);
4903         emit_libcall_block (insn, temp, r2, new_rtx);
4904
4905         new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
4906         if (reg != 0)
4907           {
4908             s390_load_address (reg, new_rtx);
4909             new_rtx = reg;
4910           }
4911         break;
4912
4913       case TLS_MODEL_LOCAL_DYNAMIC:
4914         start_sequence ();
4915         r2 = gen_rtx_REG (Pmode, 2);
4916         tls_call = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TLSLDM);
4917         new_rtx = gen_rtx_CONST (Pmode, tls_call);
4918         new_rtx = force_const_mem (Pmode, new_rtx);
4919         emit_move_insn (r2, new_rtx);
4920         s390_emit_tls_call_insn (r2, tls_call);
4921         insn = get_insns ();
4922         end_sequence ();
4923
4924         new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TLSLDM_NTPOFF);
4925         temp = gen_reg_rtx (Pmode);
4926         emit_libcall_block (insn, temp, r2, new_rtx);
4927
4928         new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
4929         base = gen_reg_rtx (Pmode);
4930         s390_load_address (base, new_rtx);
4931
4932         new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_DTPOFF);
4933         new_rtx = gen_rtx_CONST (Pmode, new_rtx);
4934         new_rtx = force_const_mem (Pmode, new_rtx);
4935         temp = gen_reg_rtx (Pmode);
4936         emit_move_insn (temp, new_rtx);
4937
4938         new_rtx = gen_rtx_PLUS (Pmode, base, temp);
4939         if (reg != 0)
4940           {
4941             s390_load_address (reg, new_rtx);
4942             new_rtx = reg;
4943           }
4944         break;
4945
4946       case TLS_MODEL_INITIAL_EXEC:
4947         if (flag_pic == 1)
4948           {
4949             /* Assume GOT offset < 4k.  This is handled the same way
4950                in both 31- and 64-bit code.  */
4951
4952             if (reload_in_progress || reload_completed)
4953               df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
4954
4955             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTNTPOFF);
4956             new_rtx = gen_rtx_CONST (Pmode, new_rtx);
4957             new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
4958             new_rtx = gen_const_mem (Pmode, new_rtx);
4959             temp = gen_reg_rtx (Pmode);
4960             emit_move_insn (temp, new_rtx);
4961           }
4962         else if (TARGET_CPU_ZARCH)
4963           {
4964             /* If the GOT offset might be >= 4k, we determine the position
4965                of the GOT entry via a PC-relative LARL.  */
4966
4967             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_INDNTPOFF);
4968             new_rtx = gen_rtx_CONST (Pmode, new_rtx);
4969             temp = gen_reg_rtx (Pmode);
4970             emit_move_insn (temp, new_rtx);
4971
4972             new_rtx = gen_const_mem (Pmode, temp);
4973             temp = gen_reg_rtx (Pmode);
4974             emit_move_insn (temp, new_rtx);
4975           }
4976         else if (flag_pic)
4977           {
4978             /* If the GOT offset might be >= 4k, we have to load it
4979                from the literal pool.  */
4980
4981             if (reload_in_progress || reload_completed)
4982               df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
4983
4984             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTNTPOFF);
4985             new_rtx = gen_rtx_CONST (Pmode, new_rtx);
4986             new_rtx = force_const_mem (Pmode, new_rtx);
4987             temp = gen_reg_rtx (Pmode);
4988             emit_move_insn (temp, new_rtx);
4989
4990             new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
4991             new_rtx = gen_const_mem (Pmode, new_rtx);
4992
4993             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, new_rtx, addr), UNSPEC_TLS_LOAD);
4994             temp = gen_reg_rtx (Pmode);
4995             emit_insn (gen_rtx_SET (temp, new_rtx));
4996           }
4997         else
4998           {
4999             /* In position-dependent code, load the absolute address of
5000                the GOT entry from the literal pool.  */
5001
5002             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_INDNTPOFF);
5003             new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5004             new_rtx = force_const_mem (Pmode, new_rtx);
5005             temp = gen_reg_rtx (Pmode);
5006             emit_move_insn (temp, new_rtx);
5007
5008             new_rtx = temp;
5009             new_rtx = gen_const_mem (Pmode, new_rtx);
5010             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, new_rtx, addr), UNSPEC_TLS_LOAD);
5011             temp = gen_reg_rtx (Pmode);
5012             emit_insn (gen_rtx_SET (temp, new_rtx));
5013           }
5014
5015         new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
5016         if (reg != 0)
5017           {
5018             s390_load_address (reg, new_rtx);
5019             new_rtx = reg;
5020           }
5021         break;
5022
5023       case TLS_MODEL_LOCAL_EXEC:
5024         new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_NTPOFF);
5025         new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5026         new_rtx = force_const_mem (Pmode, new_rtx);
5027         temp = gen_reg_rtx (Pmode);
5028         emit_move_insn (temp, new_rtx);
5029
5030         new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
5031         if (reg != 0)
5032           {
5033             s390_load_address (reg, new_rtx);
5034             new_rtx = reg;
5035           }
5036         break;
5037
5038       default:
5039         gcc_unreachable ();
5040       }
5041
5042   else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == UNSPEC)
5043     {
5044       switch (XINT (XEXP (addr, 0), 1))
5045         {
5046         case UNSPEC_INDNTPOFF:
5047           gcc_assert (TARGET_CPU_ZARCH);
5048           new_rtx = addr;
5049           break;
5050
5051         default:
5052           gcc_unreachable ();
5053         }
5054     }
5055
5056   else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS
5057            && GET_CODE (XEXP (XEXP (addr, 0), 1)) == CONST_INT)
5058     {
5059       new_rtx = XEXP (XEXP (addr, 0), 0);
5060       if (GET_CODE (new_rtx) != SYMBOL_REF)
5061         new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5062
5063       new_rtx = legitimize_tls_address (new_rtx, reg);
5064       new_rtx = plus_constant (Pmode, new_rtx,
5065                                INTVAL (XEXP (XEXP (addr, 0), 1)));
5066       new_rtx = force_operand (new_rtx, 0);
5067     }
5068
5069   else
5070     gcc_unreachable ();  /* for now ... */
5071
5072   return new_rtx;
5073 }
5074
5075 /* Emit insns making the address in operands[1] valid for a standard
5076    move to operands[0].  operands[1] is replaced by an address which
5077    should be used instead of the former RTX to emit the move
5078    pattern.  */
5079
5080 void
5081 emit_symbolic_move (rtx *operands)
5082 {
5083   rtx temp = !can_create_pseudo_p () ? operands[0] : gen_reg_rtx (Pmode);
5084
5085   if (GET_CODE (operands[0]) == MEM)
5086     operands[1] = force_reg (Pmode, operands[1]);
5087   else if (TLS_SYMBOLIC_CONST (operands[1]))
5088     operands[1] = legitimize_tls_address (operands[1], temp);
5089   else if (flag_pic)
5090     operands[1] = legitimize_pic_address (operands[1], temp);
5091 }
5092
5093 /* Try machine-dependent ways of modifying an illegitimate address X
5094    to be legitimate.  If we find one, return the new, valid address.
5095
5096    OLDX is the address as it was before break_out_memory_refs was called.
5097    In some cases it is useful to look at this to decide what needs to be done.
5098
5099    MODE is the mode of the operand pointed to by X.
5100
5101    When -fpic is used, special handling is needed for symbolic references.
5102    See comments by legitimize_pic_address for details.  */
5103
5104 static rtx
5105 s390_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
5106                          machine_mode mode ATTRIBUTE_UNUSED)
5107 {
5108   rtx constant_term = const0_rtx;
5109
5110   if (TLS_SYMBOLIC_CONST (x))
5111     {
5112       x = legitimize_tls_address (x, 0);
5113
5114       if (s390_legitimate_address_p (mode, x, FALSE))
5115         return x;
5116     }
5117   else if (GET_CODE (x) == PLUS
5118            && (TLS_SYMBOLIC_CONST (XEXP (x, 0))
5119                || TLS_SYMBOLIC_CONST (XEXP (x, 1))))
5120     {
5121       return x;
5122     }
5123   else if (flag_pic)
5124     {
5125       if (SYMBOLIC_CONST (x)
5126           || (GET_CODE (x) == PLUS
5127               && (SYMBOLIC_CONST (XEXP (x, 0))
5128                   || SYMBOLIC_CONST (XEXP (x, 1)))))
5129           x = legitimize_pic_address (x, 0);
5130
5131       if (s390_legitimate_address_p (mode, x, FALSE))
5132         return x;
5133     }
5134
5135   x = eliminate_constant_term (x, &constant_term);
5136
5137   /* Optimize loading of large displacements by splitting them
5138      into the multiple of 4K and the rest; this allows the
5139      former to be CSE'd if possible.
5140
5141      Don't do this if the displacement is added to a register
5142      pointing into the stack frame, as the offsets will
5143      change later anyway.  */
5144
5145   if (GET_CODE (constant_term) == CONST_INT
5146       && !TARGET_LONG_DISPLACEMENT
5147       && !DISP_IN_RANGE (INTVAL (constant_term))
5148       && !(REG_P (x) && REGNO_PTR_FRAME_P (REGNO (x))))
5149     {
5150       HOST_WIDE_INT lower = INTVAL (constant_term) & 0xfff;
5151       HOST_WIDE_INT upper = INTVAL (constant_term) ^ lower;
5152
5153       rtx temp = gen_reg_rtx (Pmode);
5154       rtx val  = force_operand (GEN_INT (upper), temp);
5155       if (val != temp)
5156         emit_move_insn (temp, val);
5157
5158       x = gen_rtx_PLUS (Pmode, x, temp);
5159       constant_term = GEN_INT (lower);
5160     }
5161
5162   if (GET_CODE (x) == PLUS)
5163     {
5164       if (GET_CODE (XEXP (x, 0)) == REG)
5165         {
5166           rtx temp = gen_reg_rtx (Pmode);
5167           rtx val  = force_operand (XEXP (x, 1), temp);
5168           if (val != temp)
5169             emit_move_insn (temp, val);
5170
5171           x = gen_rtx_PLUS (Pmode, XEXP (x, 0), temp);
5172         }
5173
5174       else if (GET_CODE (XEXP (x, 1)) == REG)
5175         {
5176           rtx temp = gen_reg_rtx (Pmode);
5177           rtx val  = force_operand (XEXP (x, 0), temp);
5178           if (val != temp)
5179             emit_move_insn (temp, val);
5180
5181           x = gen_rtx_PLUS (Pmode, temp, XEXP (x, 1));
5182         }
5183     }
5184
5185   if (constant_term != const0_rtx)
5186     x = gen_rtx_PLUS (Pmode, x, constant_term);
5187
5188   return x;
5189 }
5190
5191 /* Try a machine-dependent way of reloading an illegitimate address AD
5192    operand.  If we find one, push the reload and return the new address.
5193
5194    MODE is the mode of the enclosing MEM.  OPNUM is the operand number
5195    and TYPE is the reload type of the current reload.  */
5196
5197 rtx
5198 legitimize_reload_address (rtx ad, machine_mode mode ATTRIBUTE_UNUSED,
5199                            int opnum, int type)
5200 {
5201   if (!optimize || TARGET_LONG_DISPLACEMENT)
5202     return NULL_RTX;
5203
5204   if (GET_CODE (ad) == PLUS)
5205     {
5206       rtx tem = simplify_binary_operation (PLUS, Pmode,
5207                                            XEXP (ad, 0), XEXP (ad, 1));
5208       if (tem)
5209         ad = tem;
5210     }
5211
5212   if (GET_CODE (ad) == PLUS
5213       && GET_CODE (XEXP (ad, 0)) == REG
5214       && GET_CODE (XEXP (ad, 1)) == CONST_INT
5215       && !DISP_IN_RANGE (INTVAL (XEXP (ad, 1))))
5216     {
5217       HOST_WIDE_INT lower = INTVAL (XEXP (ad, 1)) & 0xfff;
5218       HOST_WIDE_INT upper = INTVAL (XEXP (ad, 1)) ^ lower;
5219       rtx cst, tem, new_rtx;
5220
5221       cst = GEN_INT (upper);
5222       if (!legitimate_reload_constant_p (cst))
5223         cst = force_const_mem (Pmode, cst);
5224
5225       tem = gen_rtx_PLUS (Pmode, XEXP (ad, 0), cst);
5226       new_rtx = gen_rtx_PLUS (Pmode, tem, GEN_INT (lower));
5227
5228       push_reload (XEXP (tem, 1), 0, &XEXP (tem, 1), 0,
5229                    BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
5230                    opnum, (enum reload_type) type);
5231       return new_rtx;
5232     }
5233
5234   return NULL_RTX;
5235 }
5236
5237 /* Emit code to move LEN bytes from DST to SRC.  */
5238
5239 bool
5240 s390_expand_movmem (rtx dst, rtx src, rtx len)
5241 {
5242   /* When tuning for z10 or higher we rely on the Glibc functions to
5243      do the right thing. Only for constant lengths below 64k we will
5244      generate inline code.  */
5245   if (s390_tune >= PROCESSOR_2097_Z10
5246       && (GET_CODE (len) != CONST_INT || INTVAL (len) > (1<<16)))
5247     return false;
5248
5249   if (GET_CODE (len) == CONST_INT && INTVAL (len) >= 0 && INTVAL (len) <= 256)
5250     {
5251       if (INTVAL (len) > 0)
5252         emit_insn (gen_movmem_short (dst, src, GEN_INT (INTVAL (len) - 1)));
5253     }
5254
5255   else if (TARGET_MVCLE)
5256     {
5257       emit_insn (gen_movmem_long (dst, src, convert_to_mode (Pmode, len, 1)));
5258     }
5259
5260   else
5261     {
5262       rtx dst_addr, src_addr, count, blocks, temp;
5263       rtx_code_label *loop_start_label = gen_label_rtx ();
5264       rtx_code_label *loop_end_label = gen_label_rtx ();
5265       rtx_code_label *end_label = gen_label_rtx ();
5266       machine_mode mode;
5267
5268       mode = GET_MODE (len);
5269       if (mode == VOIDmode)
5270         mode = Pmode;
5271
5272       dst_addr = gen_reg_rtx (Pmode);
5273       src_addr = gen_reg_rtx (Pmode);
5274       count = gen_reg_rtx (mode);
5275       blocks = gen_reg_rtx (mode);
5276
5277       convert_move (count, len, 1);
5278       emit_cmp_and_jump_insns (count, const0_rtx,
5279                                EQ, NULL_RTX, mode, 1, end_label);
5280
5281       emit_move_insn (dst_addr, force_operand (XEXP (dst, 0), NULL_RTX));
5282       emit_move_insn (src_addr, force_operand (XEXP (src, 0), NULL_RTX));
5283       dst = change_address (dst, VOIDmode, dst_addr);
5284       src = change_address (src, VOIDmode, src_addr);
5285
5286       temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1,
5287                            OPTAB_DIRECT);
5288       if (temp != count)
5289         emit_move_insn (count, temp);
5290
5291       temp = expand_binop (mode, lshr_optab, count, GEN_INT (8), blocks, 1,
5292                            OPTAB_DIRECT);
5293       if (temp != blocks)
5294         emit_move_insn (blocks, temp);
5295
5296       emit_cmp_and_jump_insns (blocks, const0_rtx,
5297                                EQ, NULL_RTX, mode, 1, loop_end_label);
5298
5299       emit_label (loop_start_label);
5300
5301       if (TARGET_Z10
5302           && (GET_CODE (len) != CONST_INT || INTVAL (len) > 768))
5303         {
5304           rtx prefetch;
5305
5306           /* Issue a read prefetch for the +3 cache line.  */
5307           prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, src_addr, GEN_INT (768)),
5308                                    const0_rtx, const0_rtx);
5309           PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
5310           emit_insn (prefetch);
5311
5312           /* Issue a write prefetch for the +3 cache line.  */
5313           prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (768)),
5314                                    const1_rtx, const0_rtx);
5315           PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
5316           emit_insn (prefetch);
5317         }
5318
5319       emit_insn (gen_movmem_short (dst, src, GEN_INT (255)));
5320       s390_load_address (dst_addr,
5321                          gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (256)));
5322       s390_load_address (src_addr,
5323                          gen_rtx_PLUS (Pmode, src_addr, GEN_INT (256)));
5324
5325       temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1,
5326                            OPTAB_DIRECT);
5327       if (temp != blocks)
5328         emit_move_insn (blocks, temp);
5329
5330       emit_cmp_and_jump_insns (blocks, const0_rtx,
5331                                EQ, NULL_RTX, mode, 1, loop_end_label);
5332
5333       emit_jump (loop_start_label);
5334       emit_label (loop_end_label);
5335
5336       emit_insn (gen_movmem_short (dst, src,
5337                                    convert_to_mode (Pmode, count, 1)));
5338       emit_label (end_label);
5339     }
5340   return true;
5341 }
5342
5343 /* Emit code to set LEN bytes at DST to VAL.
5344    Make use of clrmem if VAL is zero.  */
5345
5346 void
5347 s390_expand_setmem (rtx dst, rtx len, rtx val)
5348 {
5349   if (GET_CODE (len) == CONST_INT && INTVAL (len) == 0)
5350     return;
5351
5352   gcc_assert (GET_CODE (val) == CONST_INT || GET_MODE (val) == QImode);
5353
5354   if (GET_CODE (len) == CONST_INT && INTVAL (len) > 0 && INTVAL (len) <= 257)
5355     {
5356       if (val == const0_rtx && INTVAL (len) <= 256)
5357         emit_insn (gen_clrmem_short (dst, GEN_INT (INTVAL (len) - 1)));
5358       else
5359         {
5360           /* Initialize memory by storing the first byte.  */
5361           emit_move_insn (adjust_address (dst, QImode, 0), val);
5362
5363           if (INTVAL (len) > 1)
5364             {
5365               /* Initiate 1 byte overlap move.
5366                  The first byte of DST is propagated through DSTP1.
5367                  Prepare a movmem for:  DST+1 = DST (length = LEN - 1).
5368                  DST is set to size 1 so the rest of the memory location
5369                  does not count as source operand.  */
5370               rtx dstp1 = adjust_address (dst, VOIDmode, 1);
5371               set_mem_size (dst, 1);
5372
5373               emit_insn (gen_movmem_short (dstp1, dst,
5374                                            GEN_INT (INTVAL (len) - 2)));
5375             }
5376         }
5377     }
5378
5379   else if (TARGET_MVCLE)
5380     {
5381       val = force_not_mem (convert_modes (Pmode, QImode, val, 1));
5382       if (TARGET_64BIT)
5383         emit_insn (gen_setmem_long_di (dst, convert_to_mode (Pmode, len, 1),
5384                                        val));
5385       else
5386         emit_insn (gen_setmem_long_si (dst, convert_to_mode (Pmode, len, 1),
5387                                        val));
5388     }
5389
5390   else
5391     {
5392       rtx dst_addr, count, blocks, temp, dstp1 = NULL_RTX;
5393       rtx_code_label *loop_start_label = gen_label_rtx ();
5394       rtx_code_label *loop_end_label = gen_label_rtx ();
5395       rtx_code_label *end_label = gen_label_rtx ();
5396       machine_mode mode;
5397
5398       mode = GET_MODE (len);
5399       if (mode == VOIDmode)
5400         mode = Pmode;
5401
5402       dst_addr = gen_reg_rtx (Pmode);
5403       count = gen_reg_rtx (mode);
5404       blocks = gen_reg_rtx (mode);
5405
5406       convert_move (count, len, 1);
5407       emit_cmp_and_jump_insns (count, const0_rtx,
5408                                EQ, NULL_RTX, mode, 1, end_label);
5409
5410       emit_move_insn (dst_addr, force_operand (XEXP (dst, 0), NULL_RTX));
5411       dst = change_address (dst, VOIDmode, dst_addr);
5412
5413       if (val == const0_rtx)
5414         temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1,
5415                              OPTAB_DIRECT);
5416       else
5417         {
5418           dstp1 = adjust_address (dst, VOIDmode, 1);
5419           set_mem_size (dst, 1);
5420
5421           /* Initialize memory by storing the first byte.  */
5422           emit_move_insn (adjust_address (dst, QImode, 0), val);
5423
5424           /* If count is 1 we are done.  */
5425           emit_cmp_and_jump_insns (count, const1_rtx,
5426                                    EQ, NULL_RTX, mode, 1, end_label);
5427
5428           temp = expand_binop (mode, add_optab, count, GEN_INT (-2), count, 1,
5429                                OPTAB_DIRECT);
5430         }
5431       if (temp != count)
5432         emit_move_insn (count, temp);
5433
5434       temp = expand_binop (mode, lshr_optab, count, GEN_INT (8), blocks, 1,
5435                            OPTAB_DIRECT);
5436       if (temp != blocks)
5437         emit_move_insn (blocks, temp);
5438
5439       emit_cmp_and_jump_insns (blocks, const0_rtx,
5440                                EQ, NULL_RTX, mode, 1, loop_end_label);
5441
5442       emit_label (loop_start_label);
5443
5444       if (TARGET_Z10
5445           && (GET_CODE (len) != CONST_INT || INTVAL (len) > 1024))
5446         {
5447           /* Issue a write prefetch for the +4 cache line.  */
5448           rtx prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, dst_addr,
5449                                                      GEN_INT (1024)),
5450                                        const1_rtx, const0_rtx);
5451           emit_insn (prefetch);
5452           PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
5453         }
5454
5455       if (val == const0_rtx)
5456         emit_insn (gen_clrmem_short (dst, GEN_INT (255)));
5457       else
5458         emit_insn (gen_movmem_short (dstp1, dst, GEN_INT (255)));
5459       s390_load_address (dst_addr,
5460                          gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (256)));
5461
5462       temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1,
5463                            OPTAB_DIRECT);
5464       if (temp != blocks)
5465         emit_move_insn (blocks, temp);
5466
5467       emit_cmp_and_jump_insns (blocks, const0_rtx,
5468                                EQ, NULL_RTX, mode, 1, loop_end_label);
5469
5470       emit_jump (loop_start_label);
5471       emit_label (loop_end_label);
5472
5473       if (val == const0_rtx)
5474         emit_insn (gen_clrmem_short (dst, convert_to_mode (Pmode, count, 1)));
5475       else
5476         emit_insn (gen_movmem_short (dstp1, dst, convert_to_mode (Pmode, count, 1)));
5477       emit_label (end_label);
5478     }
5479 }
5480
5481 /* Emit code to compare LEN bytes at OP0 with those at OP1,
5482    and return the result in TARGET.  */
5483
5484 bool
5485 s390_expand_cmpmem (rtx target, rtx op0, rtx op1, rtx len)
5486 {
5487   rtx ccreg = gen_rtx_REG (CCUmode, CC_REGNUM);
5488   rtx tmp;
5489
5490   /* When tuning for z10 or higher we rely on the Glibc functions to
5491      do the right thing. Only for constant lengths below 64k we will
5492      generate inline code.  */
5493   if (s390_tune >= PROCESSOR_2097_Z10
5494       && (GET_CODE (len) != CONST_INT || INTVAL (len) > (1<<16)))
5495     return false;
5496
5497   /* As the result of CMPINT is inverted compared to what we need,
5498      we have to swap the operands.  */
5499   tmp = op0; op0 = op1; op1 = tmp;
5500
5501   if (GET_CODE (len) == CONST_INT && INTVAL (len) >= 0 && INTVAL (len) <= 256)
5502     {
5503       if (INTVAL (len) > 0)
5504         {
5505           emit_insn (gen_cmpmem_short (op0, op1, GEN_INT (INTVAL (len) - 1)));
5506           emit_insn (gen_cmpint (target, ccreg));
5507         }
5508       else
5509         emit_move_insn (target, const0_rtx);
5510     }
5511   else if (TARGET_MVCLE)
5512     {
5513       emit_insn (gen_cmpmem_long (op0, op1, convert_to_mode (Pmode, len, 1)));
5514       emit_insn (gen_cmpint (target, ccreg));
5515     }
5516   else
5517     {
5518       rtx addr0, addr1, count, blocks, temp;
5519       rtx_code_label *loop_start_label = gen_label_rtx ();
5520       rtx_code_label *loop_end_label = gen_label_rtx ();
5521       rtx_code_label *end_label = gen_label_rtx ();
5522       machine_mode mode;
5523
5524       mode = GET_MODE (len);
5525       if (mode == VOIDmode)
5526         mode = Pmode;
5527
5528       addr0 = gen_reg_rtx (Pmode);
5529       addr1 = gen_reg_rtx (Pmode);
5530       count = gen_reg_rtx (mode);
5531       blocks = gen_reg_rtx (mode);
5532
5533       convert_move (count, len, 1);
5534       emit_cmp_and_jump_insns (count, const0_rtx,
5535                                EQ, NULL_RTX, mode, 1, end_label);
5536
5537       emit_move_insn (addr0, force_operand (XEXP (op0, 0), NULL_RTX));
5538       emit_move_insn (addr1, force_operand (XEXP (op1, 0), NULL_RTX));
5539       op0 = change_address (op0, VOIDmode, addr0);
5540       op1 = change_address (op1, VOIDmode, addr1);
5541
5542       temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1,
5543                            OPTAB_DIRECT);
5544       if (temp != count)
5545         emit_move_insn (count, temp);
5546
5547       temp = expand_binop (mode, lshr_optab, count, GEN_INT (8), blocks, 1,
5548                            OPTAB_DIRECT);
5549       if (temp != blocks)
5550         emit_move_insn (blocks, temp);
5551
5552       emit_cmp_and_jump_insns (blocks, const0_rtx,
5553                                EQ, NULL_RTX, mode, 1, loop_end_label);
5554
5555       emit_label (loop_start_label);
5556
5557       if (TARGET_Z10
5558           && (GET_CODE (len) != CONST_INT || INTVAL (len) > 512))
5559         {
5560           rtx prefetch;
5561
5562           /* Issue a read prefetch for the +2 cache line of operand 1.  */
5563           prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, addr0, GEN_INT (512)),
5564                                    const0_rtx, const0_rtx);
5565           emit_insn (prefetch);
5566           PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
5567
5568           /* Issue a read prefetch for the +2 cache line of operand 2.  */
5569           prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, addr1, GEN_INT (512)),
5570                                    const0_rtx, const0_rtx);
5571           emit_insn (prefetch);
5572           PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
5573         }
5574
5575       emit_insn (gen_cmpmem_short (op0, op1, GEN_INT (255)));
5576       temp = gen_rtx_NE (VOIDmode, ccreg, const0_rtx);
5577       temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
5578                         gen_rtx_LABEL_REF (VOIDmode, end_label), pc_rtx);
5579       temp = gen_rtx_SET (pc_rtx, temp);
5580       emit_jump_insn (temp);
5581
5582       s390_load_address (addr0,
5583                          gen_rtx_PLUS (Pmode, addr0, GEN_INT (256)));
5584       s390_load_address (addr1,
5585                          gen_rtx_PLUS (Pmode, addr1, GEN_INT (256)));
5586
5587       temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1,
5588                            OPTAB_DIRECT);
5589       if (temp != blocks)
5590         emit_move_insn (blocks, temp);
5591
5592       emit_cmp_and_jump_insns (blocks, const0_rtx,
5593                                EQ, NULL_RTX, mode, 1, loop_end_label);
5594
5595       emit_jump (loop_start_label);
5596       emit_label (loop_end_label);
5597
5598       emit_insn (gen_cmpmem_short (op0, op1,
5599                                    convert_to_mode (Pmode, count, 1)));
5600       emit_label (end_label);
5601
5602       emit_insn (gen_cmpint (target, ccreg));
5603     }
5604   return true;
5605 }
5606
5607 /* Emit a conditional jump to LABEL for condition code mask MASK using
5608    comparsion operator COMPARISON.  Return the emitted jump insn.  */
5609
5610 static rtx_insn *
5611 s390_emit_ccraw_jump (HOST_WIDE_INT mask, enum rtx_code comparison, rtx label)
5612 {
5613   rtx temp;
5614
5615   gcc_assert (comparison == EQ || comparison == NE);
5616   gcc_assert (mask > 0 && mask < 15);
5617
5618   temp = gen_rtx_fmt_ee (comparison, VOIDmode,
5619                          gen_rtx_REG (CCRAWmode, CC_REGNUM), GEN_INT (mask));
5620   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
5621                                gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
5622   temp = gen_rtx_SET (pc_rtx, temp);
5623   return emit_jump_insn (temp);
5624 }
5625
5626 /* Emit the instructions to implement strlen of STRING and store the
5627    result in TARGET.  The string has the known ALIGNMENT.  This
5628    version uses vector instructions and is therefore not appropriate
5629    for targets prior to z13.  */
5630
5631 void
5632 s390_expand_vec_strlen (rtx target, rtx string, rtx alignment)
5633 {
5634   int very_unlikely = REG_BR_PROB_BASE / 100 - 1;
5635   int very_likely = REG_BR_PROB_BASE - 1;
5636   rtx highest_index_to_load_reg = gen_reg_rtx (Pmode);
5637   rtx str_reg = gen_reg_rtx (V16QImode);
5638   rtx str_addr_base_reg = gen_reg_rtx (Pmode);
5639   rtx str_idx_reg = gen_reg_rtx (Pmode);
5640   rtx result_reg = gen_reg_rtx (V16QImode);
5641   rtx is_aligned_label = gen_label_rtx ();
5642   rtx into_loop_label = NULL_RTX;
5643   rtx loop_start_label = gen_label_rtx ();
5644   rtx temp;
5645   rtx len = gen_reg_rtx (QImode);
5646   rtx cond;
5647
5648   s390_load_address (str_addr_base_reg, XEXP (string, 0));
5649   emit_move_insn (str_idx_reg, const0_rtx);
5650
5651   if (INTVAL (alignment) < 16)
5652     {
5653       /* Check whether the address happens to be aligned properly so
5654          jump directly to the aligned loop.  */
5655       emit_cmp_and_jump_insns (gen_rtx_AND (Pmode,
5656                                             str_addr_base_reg, GEN_INT (15)),
5657                                const0_rtx, EQ, NULL_RTX,
5658                                Pmode, 1, is_aligned_label);
5659
5660       temp = gen_reg_rtx (Pmode);
5661       temp = expand_binop (Pmode, and_optab, str_addr_base_reg,
5662                            GEN_INT (15), temp, 1, OPTAB_DIRECT);
5663       gcc_assert (REG_P (temp));
5664       highest_index_to_load_reg =
5665         expand_binop (Pmode, sub_optab, GEN_INT (15), temp,
5666                       highest_index_to_load_reg, 1, OPTAB_DIRECT);
5667       gcc_assert (REG_P (highest_index_to_load_reg));
5668       emit_insn (gen_vllv16qi (str_reg,
5669                    convert_to_mode (SImode, highest_index_to_load_reg, 1),
5670                    gen_rtx_MEM (BLKmode, str_addr_base_reg)));
5671
5672       into_loop_label = gen_label_rtx ();
5673       s390_emit_jump (into_loop_label, NULL_RTX);
5674       emit_barrier ();
5675     }
5676
5677   emit_label (is_aligned_label);
5678   LABEL_NUSES (is_aligned_label) = INTVAL (alignment) < 16 ? 2 : 1;
5679
5680   /* Reaching this point we are only performing 16 bytes aligned
5681      loads.  */
5682   emit_move_insn (highest_index_to_load_reg, GEN_INT (15));
5683
5684   emit_label (loop_start_label);
5685   LABEL_NUSES (loop_start_label) = 1;
5686
5687   /* Load 16 bytes of the string into VR.  */
5688   emit_move_insn (str_reg,
5689                   gen_rtx_MEM (V16QImode,
5690                                gen_rtx_PLUS (Pmode, str_idx_reg,
5691                                              str_addr_base_reg)));
5692   if (into_loop_label != NULL_RTX)
5693     {
5694       emit_label (into_loop_label);
5695       LABEL_NUSES (into_loop_label) = 1;
5696     }
5697
5698   /* Increment string index by 16 bytes.  */
5699   expand_binop (Pmode, add_optab, str_idx_reg, GEN_INT (16),
5700                 str_idx_reg, 1, OPTAB_DIRECT);
5701
5702   emit_insn (gen_vec_vfenesv16qi (result_reg, str_reg, str_reg,
5703                                   GEN_INT (VSTRING_FLAG_ZS | VSTRING_FLAG_CS)));
5704
5705   add_int_reg_note (s390_emit_ccraw_jump (8, NE, loop_start_label),
5706                     REG_BR_PROB, very_likely);
5707   emit_insn (gen_vec_extractv16qi (len, result_reg, GEN_INT (7)));
5708
5709   /* If the string pointer wasn't aligned we have loaded less then 16
5710      bytes and the remaining bytes got filled with zeros (by vll).
5711      Now we have to check whether the resulting index lies within the
5712      bytes actually part of the string.  */
5713
5714   cond = s390_emit_compare (GT, convert_to_mode (Pmode, len, 1),
5715                             highest_index_to_load_reg);
5716   s390_load_address (highest_index_to_load_reg,
5717                      gen_rtx_PLUS (Pmode, highest_index_to_load_reg,
5718                                    const1_rtx));
5719   if (TARGET_64BIT)
5720     emit_insn (gen_movdicc (str_idx_reg, cond,
5721                             highest_index_to_load_reg, str_idx_reg));
5722   else
5723     emit_insn (gen_movsicc (str_idx_reg, cond,
5724                             highest_index_to_load_reg, str_idx_reg));
5725
5726   add_int_reg_note (s390_emit_jump (is_aligned_label, cond), REG_BR_PROB,
5727                     very_unlikely);
5728
5729   expand_binop (Pmode, add_optab, str_idx_reg,
5730                 GEN_INT (-16), str_idx_reg, 1, OPTAB_DIRECT);
5731   /* FIXME: len is already zero extended - so avoid the llgcr emitted
5732      here.  */
5733   temp = expand_binop (Pmode, add_optab, str_idx_reg,
5734                        convert_to_mode (Pmode, len, 1),
5735                        target, 1, OPTAB_DIRECT);
5736   if (temp != target)
5737     emit_move_insn (target, temp);
5738 }
5739
5740 void
5741 s390_expand_vec_movstr (rtx result, rtx dst, rtx src)
5742 {
5743   int very_unlikely = REG_BR_PROB_BASE / 100 - 1;
5744   rtx temp = gen_reg_rtx (Pmode);
5745   rtx src_addr = XEXP (src, 0);
5746   rtx dst_addr = XEXP (dst, 0);
5747   rtx src_addr_reg = gen_reg_rtx (Pmode);
5748   rtx dst_addr_reg = gen_reg_rtx (Pmode);
5749   rtx offset = gen_reg_rtx (Pmode);
5750   rtx vsrc = gen_reg_rtx (V16QImode);
5751   rtx vpos = gen_reg_rtx (V16QImode);
5752   rtx loadlen = gen_reg_rtx (SImode);
5753   rtx gpos_qi = gen_reg_rtx(QImode);
5754   rtx gpos = gen_reg_rtx (SImode);
5755   rtx done_label = gen_label_rtx ();
5756   rtx loop_label = gen_label_rtx ();
5757   rtx exit_label = gen_label_rtx ();
5758   rtx full_label = gen_label_rtx ();
5759
5760   /* Perform a quick check for string ending on the first up to 16
5761      bytes and exit early if successful.  */
5762
5763   emit_insn (gen_vlbb (vsrc, src, GEN_INT (6)));
5764   emit_insn (gen_lcbb (loadlen, src_addr, GEN_INT (6)));
5765   emit_insn (gen_vfenezv16qi (vpos, vsrc, vsrc));
5766   emit_insn (gen_vec_extractv16qi (gpos_qi, vpos, GEN_INT (7)));
5767   emit_move_insn (gpos, gen_rtx_SUBREG (SImode, gpos_qi, 0));
5768   /* gpos is the byte index if a zero was found and 16 otherwise.
5769      So if it is lower than the loaded bytes we have a hit.  */
5770   emit_cmp_and_jump_insns (gpos, loadlen, GE, NULL_RTX, SImode, 1,
5771                            full_label);
5772   emit_insn (gen_vstlv16qi (vsrc, gpos, dst));
5773
5774   force_expand_binop (Pmode, add_optab, dst_addr, gpos, result,
5775                       1, OPTAB_DIRECT);
5776   emit_jump (exit_label);
5777   emit_barrier ();
5778
5779   emit_label (full_label);
5780   LABEL_NUSES (full_label) = 1;
5781
5782   /* Calculate `offset' so that src + offset points to the last byte
5783      before 16 byte alignment.  */
5784
5785   /* temp = src_addr & 0xf */
5786   force_expand_binop (Pmode, and_optab, src_addr, GEN_INT (15), temp,
5787                       1, OPTAB_DIRECT);
5788
5789   /* offset = 0xf - temp */
5790   emit_move_insn (offset, GEN_INT (15));
5791   force_expand_binop (Pmode, sub_optab, offset, temp, offset,
5792                       1, OPTAB_DIRECT);
5793
5794   /* Store `offset' bytes in the dstination string.  The quick check
5795      has loaded at least `offset' bytes into vsrc.  */
5796
5797   emit_insn (gen_vstlv16qi (vsrc, gen_lowpart (SImode, offset), dst));
5798
5799   /* Advance to the next byte to be loaded.  */
5800   force_expand_binop (Pmode, add_optab, offset, const1_rtx, offset,
5801                       1, OPTAB_DIRECT);
5802
5803   /* Make sure the addresses are single regs which can be used as a
5804      base.  */
5805   emit_move_insn (src_addr_reg, src_addr);
5806   emit_move_insn (dst_addr_reg, dst_addr);
5807
5808   /* MAIN LOOP */
5809
5810   emit_label (loop_label);
5811   LABEL_NUSES (loop_label) = 1;
5812
5813   emit_move_insn (vsrc,
5814                   gen_rtx_MEM (V16QImode,
5815                                gen_rtx_PLUS (Pmode, src_addr_reg, offset)));
5816
5817   emit_insn (gen_vec_vfenesv16qi (vpos, vsrc, vsrc,
5818                                   GEN_INT (VSTRING_FLAG_ZS | VSTRING_FLAG_CS)));
5819   add_int_reg_note (s390_emit_ccraw_jump (8, EQ, done_label),
5820                     REG_BR_PROB, very_unlikely);
5821
5822   emit_move_insn (gen_rtx_MEM (V16QImode,
5823                                gen_rtx_PLUS (Pmode, dst_addr_reg, offset)),
5824                   vsrc);
5825   /* offset += 16 */
5826   force_expand_binop (Pmode, add_optab, offset, GEN_INT (16),
5827                       offset,  1, OPTAB_DIRECT);
5828
5829   emit_jump (loop_label);
5830   emit_barrier ();
5831
5832   /* REGULAR EXIT */
5833
5834   /* We are done.  Add the offset of the zero character to the dst_addr
5835      pointer to get the result.  */
5836
5837   emit_label (done_label);
5838   LABEL_NUSES (done_label) = 1;
5839
5840   force_expand_binop (Pmode, add_optab, dst_addr_reg, offset, dst_addr_reg,
5841                       1, OPTAB_DIRECT);
5842
5843   emit_insn (gen_vec_extractv16qi (gpos_qi, vpos, GEN_INT (7)));
5844   emit_move_insn (gpos, gen_rtx_SUBREG (SImode, gpos_qi, 0));
5845
5846   emit_insn (gen_vstlv16qi (vsrc, gpos, gen_rtx_MEM (BLKmode, dst_addr_reg)));
5847
5848   force_expand_binop (Pmode, add_optab, dst_addr_reg, gpos, result,
5849                       1, OPTAB_DIRECT);
5850
5851   /* EARLY EXIT */
5852
5853   emit_label (exit_label);
5854   LABEL_NUSES (exit_label) = 1;
5855 }
5856
5857
5858 /* Expand conditional increment or decrement using alc/slb instructions.
5859    Should generate code setting DST to either SRC or SRC + INCREMENT,
5860    depending on the result of the comparison CMP_OP0 CMP_CODE CMP_OP1.
5861    Returns true if successful, false otherwise.
5862
5863    That makes it possible to implement some if-constructs without jumps e.g.:
5864    (borrow = CC0 | CC1 and carry = CC2 | CC3)
5865    unsigned int a, b, c;
5866    if (a < b)  c++; -> CCU  b > a  -> CC2;    c += carry;
5867    if (a < b)  c--; -> CCL3 a - b  -> borrow; c -= borrow;
5868    if (a <= b) c++; -> CCL3 b - a  -> borrow; c += carry;
5869    if (a <= b) c--; -> CCU  a <= b -> borrow; c -= borrow;
5870
5871    Checks for EQ and NE with a nonzero value need an additional xor e.g.:
5872    if (a == b) c++; -> CCL3 a ^= b; 0 - a  -> borrow;    c += carry;
5873    if (a == b) c--; -> CCU  a ^= b; a <= 0 -> CC0 | CC1; c -= borrow;
5874    if (a != b) c++; -> CCU  a ^= b; a > 0  -> CC2;       c += carry;
5875    if (a != b) c--; -> CCL3 a ^= b; 0 - a  -> borrow;    c -= borrow; */
5876
5877 bool
5878 s390_expand_addcc (enum rtx_code cmp_code, rtx cmp_op0, rtx cmp_op1,
5879                    rtx dst, rtx src, rtx increment)
5880 {
5881   machine_mode cmp_mode;
5882   machine_mode cc_mode;
5883   rtx op_res;
5884   rtx insn;
5885   rtvec p;
5886   int ret;
5887
5888   if ((GET_MODE (cmp_op0) == SImode || GET_MODE (cmp_op0) == VOIDmode)
5889       && (GET_MODE (cmp_op1) == SImode || GET_MODE (cmp_op1) == VOIDmode))
5890     cmp_mode = SImode;
5891   else if ((GET_MODE (cmp_op0) == DImode || GET_MODE (cmp_op0) == VOIDmode)
5892            && (GET_MODE (cmp_op1) == DImode || GET_MODE (cmp_op1) == VOIDmode))
5893     cmp_mode = DImode;
5894   else
5895     return false;
5896
5897   /* Try ADD LOGICAL WITH CARRY.  */
5898   if (increment == const1_rtx)
5899     {
5900       /* Determine CC mode to use.  */
5901       if (cmp_code == EQ || cmp_code == NE)
5902         {
5903           if (cmp_op1 != const0_rtx)
5904             {
5905               cmp_op0 = expand_simple_binop (cmp_mode, XOR, cmp_op0, cmp_op1,
5906                                              NULL_RTX, 0, OPTAB_WIDEN);
5907               cmp_op1 = const0_rtx;
5908             }
5909
5910           cmp_code = cmp_code == EQ ? LEU : GTU;
5911         }
5912
5913       if (cmp_code == LTU || cmp_code == LEU)
5914         {
5915           rtx tem = cmp_op0;
5916           cmp_op0 = cmp_op1;
5917           cmp_op1 = tem;
5918           cmp_code = swap_condition (cmp_code);
5919         }
5920
5921       switch (cmp_code)
5922         {
5923           case GTU:
5924             cc_mode = CCUmode;
5925             break;
5926
5927           case GEU:
5928             cc_mode = CCL3mode;
5929             break;
5930
5931           default:
5932             return false;
5933         }
5934
5935       /* Emit comparison instruction pattern. */
5936       if (!register_operand (cmp_op0, cmp_mode))
5937         cmp_op0 = force_reg (cmp_mode, cmp_op0);
5938
5939       insn = gen_rtx_SET (gen_rtx_REG (cc_mode, CC_REGNUM),
5940                           gen_rtx_COMPARE (cc_mode, cmp_op0, cmp_op1));
5941       /* We use insn_invalid_p here to add clobbers if required.  */
5942       ret = insn_invalid_p (emit_insn (insn), false);
5943       gcc_assert (!ret);
5944
5945       /* Emit ALC instruction pattern.  */
5946       op_res = gen_rtx_fmt_ee (cmp_code, GET_MODE (dst),
5947                                gen_rtx_REG (cc_mode, CC_REGNUM),
5948                                const0_rtx);
5949
5950       if (src != const0_rtx)
5951         {
5952           if (!register_operand (src, GET_MODE (dst)))
5953             src = force_reg (GET_MODE (dst), src);
5954
5955           op_res = gen_rtx_PLUS (GET_MODE (dst), op_res, src);
5956           op_res = gen_rtx_PLUS (GET_MODE (dst), op_res, const0_rtx);
5957         }
5958
5959       p = rtvec_alloc (2);
5960       RTVEC_ELT (p, 0) =
5961         gen_rtx_SET (dst, op_res);
5962       RTVEC_ELT (p, 1) =
5963         gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
5964       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
5965
5966       return true;
5967     }
5968
5969   /* Try SUBTRACT LOGICAL WITH BORROW.  */
5970   if (increment == constm1_rtx)
5971     {
5972       /* Determine CC mode to use.  */
5973       if (cmp_code == EQ || cmp_code == NE)
5974         {
5975           if (cmp_op1 != const0_rtx)
5976             {
5977               cmp_op0 = expand_simple_binop (cmp_mode, XOR, cmp_op0, cmp_op1,
5978                                              NULL_RTX, 0, OPTAB_WIDEN);
5979               cmp_op1 = const0_rtx;
5980             }
5981
5982           cmp_code = cmp_code == EQ ? LEU : GTU;
5983         }
5984
5985       if (cmp_code == GTU || cmp_code == GEU)
5986         {
5987           rtx tem = cmp_op0;
5988           cmp_op0 = cmp_op1;
5989           cmp_op1 = tem;
5990           cmp_code = swap_condition (cmp_code);
5991         }
5992
5993       switch (cmp_code)
5994         {
5995           case LEU:
5996             cc_mode = CCUmode;
5997             break;
5998
5999           case LTU:
6000             cc_mode = CCL3mode;
6001             break;
6002
6003           default:
6004             return false;
6005         }
6006
6007       /* Emit comparison instruction pattern. */
6008       if (!register_operand (cmp_op0, cmp_mode))
6009         cmp_op0 = force_reg (cmp_mode, cmp_op0);
6010
6011       insn = gen_rtx_SET (gen_rtx_REG (cc_mode, CC_REGNUM),
6012                           gen_rtx_COMPARE (cc_mode, cmp_op0, cmp_op1));
6013       /* We use insn_invalid_p here to add clobbers if required.  */
6014       ret = insn_invalid_p (emit_insn (insn), false);
6015       gcc_assert (!ret);
6016
6017       /* Emit SLB instruction pattern.  */
6018       if (!register_operand (src, GET_MODE (dst)))
6019         src = force_reg (GET_MODE (dst), src);
6020
6021       op_res = gen_rtx_MINUS (GET_MODE (dst),
6022                               gen_rtx_MINUS (GET_MODE (dst), src, const0_rtx),
6023                               gen_rtx_fmt_ee (cmp_code, GET_MODE (dst),
6024                                               gen_rtx_REG (cc_mode, CC_REGNUM),
6025                                               const0_rtx));
6026       p = rtvec_alloc (2);
6027       RTVEC_ELT (p, 0) =
6028         gen_rtx_SET (dst, op_res);
6029       RTVEC_ELT (p, 1) =
6030         gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
6031       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
6032
6033       return true;
6034     }
6035
6036   return false;
6037 }
6038
6039 /* Expand code for the insv template. Return true if successful.  */
6040
6041 bool
6042 s390_expand_insv (rtx dest, rtx op1, rtx op2, rtx src)
6043 {
6044   int bitsize = INTVAL (op1);
6045   int bitpos = INTVAL (op2);
6046   machine_mode mode = GET_MODE (dest);
6047   machine_mode smode;
6048   int smode_bsize, mode_bsize;
6049   rtx op, clobber;
6050
6051   if (bitsize + bitpos > GET_MODE_BITSIZE (mode))
6052     return false;
6053
6054   /* Generate INSERT IMMEDIATE (IILL et al).  */
6055   /* (set (ze (reg)) (const_int)).  */
6056   if (TARGET_ZARCH
6057       && register_operand (dest, word_mode)
6058       && (bitpos % 16) == 0
6059       && (bitsize % 16) == 0
6060       && const_int_operand (src, VOIDmode))
6061     {
6062       HOST_WIDE_INT val = INTVAL (src);
6063       int regpos = bitpos + bitsize;
6064
6065       while (regpos > bitpos)
6066         {
6067           machine_mode putmode;
6068           int putsize;
6069
6070           if (TARGET_EXTIMM && (regpos % 32 == 0) && (regpos >= bitpos + 32))
6071             putmode = SImode;
6072           else
6073             putmode = HImode;
6074
6075           putsize = GET_MODE_BITSIZE (putmode);
6076           regpos -= putsize;
6077           emit_move_insn (gen_rtx_ZERO_EXTRACT (word_mode, dest,
6078                                                 GEN_INT (putsize),
6079                                                 GEN_INT (regpos)),
6080                           gen_int_mode (val, putmode));
6081           val >>= putsize;
6082         }
6083       gcc_assert (regpos == bitpos);
6084       return true;
6085     }
6086
6087   smode = smallest_mode_for_size (bitsize, MODE_INT);
6088   smode_bsize = GET_MODE_BITSIZE (smode);
6089   mode_bsize = GET_MODE_BITSIZE (mode);
6090
6091   /* Generate STORE CHARACTERS UNDER MASK (STCM et al).  */
6092   if (bitpos == 0
6093       && (bitsize % BITS_PER_UNIT) == 0
6094       && MEM_P (dest)
6095       && (register_operand (src, word_mode)
6096           || const_int_operand (src, VOIDmode)))
6097     {
6098       /* Emit standard pattern if possible.  */
6099       if (smode_bsize == bitsize)
6100         {
6101           emit_move_insn (adjust_address (dest, smode, 0),
6102                           gen_lowpart (smode, src));
6103           return true;
6104         }
6105
6106       /* (set (ze (mem)) (const_int)).  */
6107       else if (const_int_operand (src, VOIDmode))
6108         {
6109           int size = bitsize / BITS_PER_UNIT;
6110           rtx src_mem = adjust_address (force_const_mem (word_mode, src),
6111                                         BLKmode,
6112                                         UNITS_PER_WORD - size);
6113
6114           dest = adjust_address (dest, BLKmode, 0);
6115           set_mem_size (dest, size);
6116           s390_expand_movmem (dest, src_mem, GEN_INT (size));
6117           return true;
6118         }
6119
6120       /* (set (ze (mem)) (reg)).  */
6121       else if (register_operand (src, word_mode))
6122         {
6123           if (bitsize <= 32)
6124             emit_move_insn (gen_rtx_ZERO_EXTRACT (word_mode, dest, op1,
6125                                                   const0_rtx), src);
6126           else
6127             {
6128               /* Emit st,stcmh sequence.  */
6129               int stcmh_width = bitsize - 32;
6130               int size = stcmh_width / BITS_PER_UNIT;
6131
6132               emit_move_insn (adjust_address (dest, SImode, size),
6133                               gen_lowpart (SImode, src));
6134               set_mem_size (dest, size);
6135               emit_move_insn (gen_rtx_ZERO_EXTRACT (word_mode, dest,
6136                                                     GEN_INT (stcmh_width),
6137                                                     const0_rtx),
6138                               gen_rtx_LSHIFTRT (word_mode, src, GEN_INT (32)));
6139             }
6140           return true;
6141         }
6142     }
6143
6144   /* Generate INSERT CHARACTERS UNDER MASK (IC, ICM et al).  */
6145   if ((bitpos % BITS_PER_UNIT) == 0
6146       && (bitsize % BITS_PER_UNIT) == 0
6147       && (bitpos & 32) == ((bitpos + bitsize - 1) & 32)
6148       && MEM_P (src)
6149       && (mode == DImode || mode == SImode)
6150       && register_operand (dest, mode))
6151     {
6152       /* Emit a strict_low_part pattern if possible.  */
6153       if (smode_bsize == bitsize && bitpos == mode_bsize - smode_bsize)
6154         {
6155           op = gen_rtx_STRICT_LOW_PART (VOIDmode, gen_lowpart (smode, dest));
6156           op = gen_rtx_SET (op, gen_lowpart (smode, src));
6157           clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
6158           emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clobber)));
6159           return true;
6160         }
6161
6162       /* ??? There are more powerful versions of ICM that are not
6163          completely represented in the md file.  */
6164     }
6165
6166   /* For z10, generate ROTATE THEN INSERT SELECTED BITS (RISBG et al).  */
6167   if (TARGET_Z10 && (mode == DImode || mode == SImode))
6168     {
6169       machine_mode mode_s = GET_MODE (src);
6170
6171       if (CONSTANT_P (src))
6172         {
6173           /* For constant zero values the representation with AND
6174              appears to be folded in more situations than the (set
6175              (zero_extract) ...).
6176              We only do this when the start and end of the bitfield
6177              remain in the same SImode chunk.  That way nihf or nilf
6178              can be used.
6179              The AND patterns might still generate a risbg for this.  */
6180           if (src == const0_rtx && bitpos / 32  == (bitpos + bitsize - 1) / 32)
6181             return false;
6182           else
6183             src = force_reg (mode, src);
6184         }
6185       else if (mode_s != mode)
6186         {
6187           gcc_assert (GET_MODE_BITSIZE (mode_s) >= bitsize);
6188           src = force_reg (mode_s, src);
6189           src = gen_lowpart (mode, src);
6190         }
6191
6192       op = gen_rtx_ZERO_EXTRACT (mode, dest, op1, op2),
6193       op = gen_rtx_SET (op, src);
6194
6195       if (!TARGET_ZEC12)
6196         {
6197           clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
6198           op = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clobber));
6199         }
6200       emit_insn (op);
6201
6202       return true;
6203     }
6204
6205   return false;
6206 }
6207
6208 /* A subroutine of s390_expand_cs_hqi and s390_expand_atomic which returns a
6209    register that holds VAL of mode MODE shifted by COUNT bits.  */
6210
6211 static inline rtx
6212 s390_expand_mask_and_shift (rtx val, machine_mode mode, rtx count)
6213 {
6214   val = expand_simple_binop (SImode, AND, val, GEN_INT (GET_MODE_MASK (mode)),
6215                              NULL_RTX, 1, OPTAB_DIRECT);
6216   return expand_simple_binop (SImode, ASHIFT, val, count,
6217                               NULL_RTX, 1, OPTAB_DIRECT);
6218 }
6219
6220 /* Generate a vector comparison COND of CMP_OP1 and CMP_OP2 and store
6221    the result in TARGET.  */
6222
6223 void
6224 s390_expand_vec_compare (rtx target, enum rtx_code cond,
6225                          rtx cmp_op1, rtx cmp_op2)
6226 {
6227   machine_mode mode = GET_MODE (target);
6228   bool neg_p = false, swap_p = false;
6229   rtx tmp;
6230
6231   if (GET_MODE (cmp_op1) == V2DFmode)
6232     {
6233       switch (cond)
6234         {
6235           /* NE a != b -> !(a == b) */
6236         case NE:   cond = EQ; neg_p = true;                break;
6237           /* UNGT a u> b -> !(b >= a) */
6238         case UNGT: cond = GE; neg_p = true; swap_p = true; break;
6239           /* UNGE a u>= b -> !(b > a) */
6240         case UNGE: cond = GT; neg_p = true; swap_p = true; break;
6241           /* LE: a <= b -> b >= a */
6242         case LE:   cond = GE;               swap_p = true; break;
6243           /* UNLE: a u<= b -> !(a > b) */
6244         case UNLE: cond = GT; neg_p = true;                break;
6245           /* LT: a < b -> b > a */
6246         case LT:   cond = GT;               swap_p = true; break;
6247           /* UNLT: a u< b -> !(a >= b) */
6248         case UNLT: cond = GE; neg_p = true;                break;
6249         case UNEQ:
6250           emit_insn (gen_vec_cmpuneqv2df (target, cmp_op1, cmp_op2));
6251           return;
6252         case LTGT:
6253           emit_insn (gen_vec_cmpltgtv2df (target, cmp_op1, cmp_op2));
6254           return;
6255         case ORDERED:
6256           emit_insn (gen_vec_orderedv2df (target, cmp_op1, cmp_op2));
6257           return;
6258         case UNORDERED:
6259           emit_insn (gen_vec_unorderedv2df (target, cmp_op1, cmp_op2));
6260           return;
6261         default: break;
6262         }
6263     }
6264   else
6265     {
6266       switch (cond)
6267         {
6268           /* NE: a != b -> !(a == b) */
6269         case NE:  cond = EQ;  neg_p = true;                break;
6270           /* GE: a >= b -> !(b > a) */
6271         case GE:  cond = GT;  neg_p = true; swap_p = true; break;
6272           /* GEU: a >= b -> !(b > a) */
6273         case GEU: cond = GTU; neg_p = true; swap_p = true; break;
6274           /* LE: a <= b -> !(a > b) */
6275         case LE:  cond = GT;  neg_p = true;                break;
6276           /* LEU: a <= b -> !(a > b) */
6277         case LEU: cond = GTU; neg_p = true;                break;
6278           /* LT: a < b -> b > a */
6279         case LT:  cond = GT;                swap_p = true; break;
6280           /* LTU: a < b -> b > a */
6281         case LTU: cond = GTU;               swap_p = true; break;
6282         default: break;
6283         }
6284     }
6285
6286   if (swap_p)
6287     {
6288       tmp = cmp_op1; cmp_op1 = cmp_op2; cmp_op2 = tmp;
6289     }
6290
6291   emit_insn (gen_rtx_SET (target, gen_rtx_fmt_ee (cond,
6292                                                   mode,
6293                                                   cmp_op1, cmp_op2)));
6294   if (neg_p)
6295     emit_insn (gen_rtx_SET (target, gen_rtx_NOT (mode, target)));
6296 }
6297
6298 /* Expand the comparison CODE of CMP1 and CMP2 and copy 1 or 0 into
6299    TARGET if either all (ALL_P is true) or any (ALL_P is false) of the
6300    elements in CMP1 and CMP2 fulfill the comparison.
6301    This function is only used to emit patterns for the vx builtins and
6302    therefore only handles comparison codes required by the
6303    builtins.  */
6304 void
6305 s390_expand_vec_compare_cc (rtx target, enum rtx_code code,
6306                             rtx cmp1, rtx cmp2, bool all_p)
6307 {
6308   machine_mode cc_producer_mode, cc_consumer_mode, scratch_mode;
6309   rtx tmp_reg = gen_reg_rtx (SImode);
6310   bool swap_p = false;
6311
6312   if (GET_MODE_CLASS (GET_MODE (cmp1)) == MODE_VECTOR_INT)
6313     {
6314       switch (code)
6315         {
6316         case EQ:
6317         case NE:
6318           cc_producer_mode = CCVEQmode;
6319           break;
6320         case GE:
6321         case LT:
6322           code = swap_condition (code);
6323           swap_p = true;
6324           /* fallthrough */
6325         case GT:
6326         case LE:
6327           cc_producer_mode = CCVIHmode;
6328           break;
6329         case GEU:
6330         case LTU:
6331           code = swap_condition (code);
6332           swap_p = true;
6333           /* fallthrough */
6334         case GTU:
6335         case LEU:
6336           cc_producer_mode = CCVIHUmode;
6337           break;
6338         default:
6339           gcc_unreachable ();
6340         }
6341
6342       scratch_mode = GET_MODE (cmp1);
6343       /* These codes represent inverted CC interpretations.  Inverting
6344          an ALL CC mode results in an ANY CC mode and the other way
6345          around.  Invert the all_p flag here to compensate for
6346          that.  */
6347       if (code == NE || code == LE || code == LEU)
6348         all_p = !all_p;
6349
6350       cc_consumer_mode = all_p ? CCVIALLmode : CCVIANYmode;
6351     }
6352   else if (GET_MODE_CLASS (GET_MODE (cmp1)) == MODE_VECTOR_FLOAT)
6353     {
6354       bool inv_p = false;
6355
6356       switch (code)
6357         {
6358         case EQ:   cc_producer_mode = CCVEQmode;  break;
6359         case NE:   cc_producer_mode = CCVEQmode;  inv_p = true; break;
6360         case GT:   cc_producer_mode = CCVFHmode;  break;
6361         case GE:   cc_producer_mode = CCVFHEmode; break;
6362         case UNLE: cc_producer_mode = CCVFHmode;  inv_p = true; break;
6363         case UNLT: cc_producer_mode = CCVFHEmode; inv_p = true; break;
6364         case LT:   cc_producer_mode = CCVFHmode;  code = GT; swap_p = true; break;
6365         case LE:   cc_producer_mode = CCVFHEmode; code = GE; swap_p = true; break;
6366         default: gcc_unreachable ();
6367         }
6368       scratch_mode = mode_for_vector (
6369                        int_mode_for_mode (GET_MODE_INNER (GET_MODE (cmp1))),
6370                        GET_MODE_NUNITS (GET_MODE (cmp1)));
6371       gcc_assert (scratch_mode != BLKmode);
6372
6373       if (inv_p)
6374         all_p = !all_p;
6375
6376       cc_consumer_mode = all_p ? CCVFALLmode : CCVFANYmode;
6377     }
6378   else
6379     gcc_unreachable ();
6380
6381   if (swap_p)
6382     {
6383       rtx tmp = cmp2;
6384       cmp2 = cmp1;
6385       cmp1 = tmp;
6386     }
6387
6388   emit_insn (gen_rtx_PARALLEL (VOIDmode,
6389                gen_rtvec (2, gen_rtx_SET (
6390                                gen_rtx_REG (cc_producer_mode, CC_REGNUM),
6391                                gen_rtx_COMPARE (cc_producer_mode, cmp1, cmp2)),
6392                           gen_rtx_CLOBBER (VOIDmode,
6393                                            gen_rtx_SCRATCH (scratch_mode)))));
6394   emit_move_insn (target, const0_rtx);
6395   emit_move_insn (tmp_reg, const1_rtx);
6396
6397   emit_move_insn (target,
6398                   gen_rtx_IF_THEN_ELSE (SImode,
6399                     gen_rtx_fmt_ee (code, VOIDmode,
6400                                     gen_rtx_REG (cc_consumer_mode, CC_REGNUM),
6401                                     const0_rtx),
6402                                         tmp_reg, target));
6403 }
6404
6405 /* Invert the comparison CODE applied to a CC mode.  This is only safe
6406    if we know whether there result was created by a floating point
6407    compare or not.  For the CCV modes this is encoded as part of the
6408    mode.  */
6409 enum rtx_code
6410 s390_reverse_condition (machine_mode mode, enum rtx_code code)
6411 {
6412   /* Reversal of FP compares takes care -- an ordered compare
6413      becomes an unordered compare and vice versa.  */
6414   if (mode == CCVFALLmode || mode == CCVFANYmode)
6415     return reverse_condition_maybe_unordered (code);
6416   else if (mode == CCVIALLmode || mode == CCVIANYmode)
6417     return reverse_condition (code);
6418   else
6419     gcc_unreachable ();
6420 }
6421
6422 /* Generate a vector comparison expression loading either elements of
6423    THEN or ELS into TARGET depending on the comparison COND of CMP_OP1
6424    and CMP_OP2.  */
6425
6426 void
6427 s390_expand_vcond (rtx target, rtx then, rtx els,
6428                    enum rtx_code cond, rtx cmp_op1, rtx cmp_op2)
6429 {
6430   rtx tmp;
6431   machine_mode result_mode;
6432   rtx result_target;
6433
6434   machine_mode target_mode = GET_MODE (target);
6435   machine_mode cmp_mode = GET_MODE (cmp_op1);
6436   rtx op = (cond == LT) ? els : then;
6437
6438   /* Try to optimize x < 0 ? -1 : 0 into (signed) x >> 31
6439      and x < 0 ? 1 : 0 into (unsigned) x >> 31.  Likewise
6440      for short and byte (x >> 15 and x >> 7 respectively).  */
6441   if ((cond == LT || cond == GE)
6442       && target_mode == cmp_mode
6443       && cmp_op2 == CONST0_RTX (cmp_mode)
6444       && op == CONST0_RTX (target_mode)
6445       && s390_vector_mode_supported_p (target_mode)
6446       && GET_MODE_CLASS (target_mode) == MODE_VECTOR_INT)
6447     {
6448       rtx negop = (cond == LT) ? then : els;
6449
6450       int shift = GET_MODE_BITSIZE (GET_MODE_INNER (target_mode)) - 1;
6451
6452       /* if x < 0 ? 1 : 0 or if x >= 0 ? 0 : 1 */
6453       if (negop == CONST1_RTX (target_mode))
6454         {
6455           rtx res = expand_simple_binop (cmp_mode, LSHIFTRT, cmp_op1,
6456                                          GEN_INT (shift), target,
6457                                          1, OPTAB_DIRECT);
6458           if (res != target)
6459             emit_move_insn (target, res);
6460           return;
6461         }
6462
6463       /* if x < 0 ? -1 : 0 or if x >= 0 ? 0 : -1 */
6464       else if (all_ones_operand (negop, target_mode))
6465         {
6466           rtx res = expand_simple_binop (cmp_mode, ASHIFTRT, cmp_op1,
6467                                          GEN_INT (shift), target,
6468                                          0, OPTAB_DIRECT);
6469           if (res != target)
6470             emit_move_insn (target, res);
6471           return;
6472         }
6473     }
6474
6475   /* We always use an integral type vector to hold the comparison
6476      result.  */
6477   result_mode = cmp_mode == V2DFmode ? V2DImode : cmp_mode;
6478   result_target = gen_reg_rtx (result_mode);
6479
6480   /* We allow vector immediates as comparison operands that
6481      can be handled by the optimization above but not by the
6482      following code.  Hence, force them into registers here.  */
6483   if (!REG_P (cmp_op1))
6484     cmp_op1 = force_reg (GET_MODE (cmp_op1), cmp_op1);
6485
6486   if (!REG_P (cmp_op2))
6487     cmp_op2 = force_reg (GET_MODE (cmp_op2), cmp_op2);
6488
6489   s390_expand_vec_compare (result_target, cond,
6490                            cmp_op1, cmp_op2);
6491
6492   /* If the results are supposed to be either -1 or 0 we are done
6493      since this is what our compare instructions generate anyway.  */
6494   if (all_ones_operand (then, GET_MODE (then))
6495       && const0_operand (els, GET_MODE (els)))
6496     {
6497       emit_move_insn (target, gen_rtx_SUBREG (target_mode,
6498                                               result_target, 0));
6499       return;
6500     }
6501
6502   /* Otherwise we will do a vsel afterwards.  */
6503   /* This gets triggered e.g.
6504      with gcc.c-torture/compile/pr53410-1.c */
6505   if (!REG_P (then))
6506     then = force_reg (target_mode, then);
6507
6508   if (!REG_P (els))
6509     els = force_reg (target_mode, els);
6510
6511   tmp = gen_rtx_fmt_ee (EQ, VOIDmode,
6512                         result_target,
6513                         CONST0_RTX (result_mode));
6514
6515   /* We compared the result against zero above so we have to swap then
6516      and els here.  */
6517   tmp = gen_rtx_IF_THEN_ELSE (target_mode, tmp, els, then);
6518
6519   gcc_assert (target_mode == GET_MODE (then));
6520   emit_insn (gen_rtx_SET (target, tmp));
6521 }
6522
6523 /* Emit the RTX necessary to initialize the vector TARGET with values
6524    in VALS.  */
6525 void
6526 s390_expand_vec_init (rtx target, rtx vals)
6527 {
6528   machine_mode mode = GET_MODE (target);
6529   machine_mode inner_mode = GET_MODE_INNER (mode);
6530   int n_elts = GET_MODE_NUNITS (mode);
6531   bool all_same = true, all_regs = true, all_const_int = true;
6532   rtx x;
6533   int i;
6534
6535   for (i = 0; i < n_elts; ++i)
6536     {
6537       x = XVECEXP (vals, 0, i);
6538
6539       if (!CONST_INT_P (x))
6540         all_const_int = false;
6541
6542       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
6543         all_same = false;
6544
6545       if (!REG_P (x))
6546         all_regs = false;
6547     }
6548
6549   /* Use vector gen mask or vector gen byte mask if possible.  */
6550   if (all_same && all_const_int
6551       && (XVECEXP (vals, 0, 0) == const0_rtx
6552           || s390_contiguous_bitmask_vector_p (XVECEXP (vals, 0, 0),
6553                                                NULL, NULL)
6554           || s390_bytemask_vector_p (XVECEXP (vals, 0, 0), NULL)))
6555     {
6556       emit_insn (gen_rtx_SET (target,
6557                               gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0))));
6558       return;
6559     }
6560
6561   if (all_same)
6562     {
6563       emit_insn (gen_rtx_SET (target,
6564                               gen_rtx_VEC_DUPLICATE (mode,
6565                                                      XVECEXP (vals, 0, 0))));
6566       return;
6567     }
6568
6569   if (all_regs && REG_P (target) && n_elts == 2 && inner_mode == DImode)
6570     {
6571       /* Use vector load pair.  */
6572       emit_insn (gen_rtx_SET (target,
6573                               gen_rtx_VEC_CONCAT (mode,
6574                                                   XVECEXP (vals, 0, 0),
6575                                                   XVECEXP (vals, 0, 1))));
6576       return;
6577     }
6578
6579   /* We are about to set the vector elements one by one.  Zero out the
6580      full register first in order to help the data flow framework to
6581      detect it as full VR set.  */
6582   emit_insn (gen_rtx_SET (target, CONST0_RTX (mode)));
6583
6584   /* Unfortunately the vec_init expander is not allowed to fail.  So
6585      we have to implement the fallback ourselves.  */
6586   for (i = 0; i < n_elts; i++)
6587     {
6588       rtx elem = XVECEXP (vals, 0, i);
6589       if (!general_operand (elem, GET_MODE (elem)))
6590         elem = force_reg (inner_mode, elem);
6591
6592       emit_insn (gen_rtx_SET (target,
6593                               gen_rtx_UNSPEC (mode,
6594                                               gen_rtvec (3, elem,
6595                                                          GEN_INT (i), target),
6596                                               UNSPEC_VEC_SET)));
6597     }
6598 }
6599
6600 /* Structure to hold the initial parameters for a compare_and_swap operation
6601    in HImode and QImode.  */
6602
6603 struct alignment_context
6604 {
6605   rtx memsi;      /* SI aligned memory location.  */
6606   rtx shift;      /* Bit offset with regard to lsb.  */
6607   rtx modemask;   /* Mask of the HQImode shifted by SHIFT bits.  */
6608   rtx modemaski;  /* ~modemask */
6609   bool aligned;   /* True if memory is aligned, false else.  */
6610 };
6611
6612 /* A subroutine of s390_expand_cs_hqi and s390_expand_atomic to initialize
6613    structure AC for transparent simplifying, if the memory alignment is known
6614    to be at least 32bit.  MEM is the memory location for the actual operation
6615    and MODE its mode.  */
6616
6617 static void
6618 init_alignment_context (struct alignment_context *ac, rtx mem,
6619                         machine_mode mode)
6620 {
6621   ac->shift = GEN_INT (GET_MODE_SIZE (SImode) - GET_MODE_SIZE (mode));
6622   ac->aligned = (MEM_ALIGN (mem) >= GET_MODE_BITSIZE (SImode));
6623
6624   if (ac->aligned)
6625     ac->memsi = adjust_address (mem, SImode, 0); /* Memory is aligned.  */
6626   else
6627     {
6628       /* Alignment is unknown.  */
6629       rtx byteoffset, addr, align;
6630
6631       /* Force the address into a register.  */
6632       addr = force_reg (Pmode, XEXP (mem, 0));
6633
6634       /* Align it to SImode.  */
6635       align = expand_simple_binop (Pmode, AND, addr,
6636                                    GEN_INT (-GET_MODE_SIZE (SImode)),
6637                                    NULL_RTX, 1, OPTAB_DIRECT);
6638       /* Generate MEM.  */
6639       ac->memsi = gen_rtx_MEM (SImode, align);
6640       MEM_VOLATILE_P (ac->memsi) = MEM_VOLATILE_P (mem);
6641       set_mem_alias_set (ac->memsi, ALIAS_SET_MEMORY_BARRIER);
6642       set_mem_align (ac->memsi, GET_MODE_BITSIZE (SImode));
6643
6644       /* Calculate shiftcount.  */
6645       byteoffset = expand_simple_binop (Pmode, AND, addr,
6646                                         GEN_INT (GET_MODE_SIZE (SImode) - 1),
6647                                         NULL_RTX, 1, OPTAB_DIRECT);
6648       /* As we already have some offset, evaluate the remaining distance.  */
6649       ac->shift = expand_simple_binop (SImode, MINUS, ac->shift, byteoffset,
6650                                       NULL_RTX, 1, OPTAB_DIRECT);
6651     }
6652
6653   /* Shift is the byte count, but we need the bitcount.  */
6654   ac->shift = expand_simple_binop (SImode, ASHIFT, ac->shift, GEN_INT (3),
6655                                    NULL_RTX, 1, OPTAB_DIRECT);
6656
6657   /* Calculate masks.  */
6658   ac->modemask = expand_simple_binop (SImode, ASHIFT,
6659                                       GEN_INT (GET_MODE_MASK (mode)),
6660                                       ac->shift, NULL_RTX, 1, OPTAB_DIRECT);
6661   ac->modemaski = expand_simple_unop (SImode, NOT, ac->modemask,
6662                                       NULL_RTX, 1);
6663 }
6664
6665 /* A subroutine of s390_expand_cs_hqi.  Insert INS into VAL.  If possible,
6666    use a single insv insn into SEQ2.  Otherwise, put prep insns in SEQ1 and
6667    perform the merge in SEQ2.  */
6668
6669 static rtx
6670 s390_two_part_insv (struct alignment_context *ac, rtx *seq1, rtx *seq2,
6671                     machine_mode mode, rtx val, rtx ins)
6672 {
6673   rtx tmp;
6674
6675   if (ac->aligned)
6676     {
6677       start_sequence ();
6678       tmp = copy_to_mode_reg (SImode, val);
6679       if (s390_expand_insv (tmp, GEN_INT (GET_MODE_BITSIZE (mode)),
6680                             const0_rtx, ins))
6681         {
6682           *seq1 = NULL;
6683           *seq2 = get_insns ();
6684           end_sequence ();
6685           return tmp;
6686         }
6687       end_sequence ();
6688     }
6689
6690   /* Failed to use insv.  Generate a two part shift and mask.  */
6691   start_sequence ();
6692   tmp = s390_expand_mask_and_shift (ins, mode, ac->shift);
6693   *seq1 = get_insns ();
6694   end_sequence ();
6695
6696   start_sequence ();
6697   tmp = expand_simple_binop (SImode, IOR, tmp, val, NULL_RTX, 1, OPTAB_DIRECT);
6698   *seq2 = get_insns ();
6699   end_sequence ();
6700
6701   return tmp;
6702 }
6703
6704 /* Expand an atomic compare and swap operation for HImode and QImode.  MEM is
6705    the memory location, CMP the old value to compare MEM with and NEW_RTX the
6706    value to set if CMP == MEM.  */
6707
6708 void
6709 s390_expand_cs_hqi (machine_mode mode, rtx btarget, rtx vtarget, rtx mem,
6710                     rtx cmp, rtx new_rtx, bool is_weak)
6711 {
6712   struct alignment_context ac;
6713   rtx cmpv, newv, val, cc, seq0, seq1, seq2, seq3;
6714   rtx res = gen_reg_rtx (SImode);
6715   rtx_code_label *csloop = NULL, *csend = NULL;
6716
6717   gcc_assert (MEM_P (mem));
6718
6719   init_alignment_context (&ac, mem, mode);
6720
6721   /* Load full word.  Subsequent loads are performed by CS.  */
6722   val = expand_simple_binop (SImode, AND, ac.memsi, ac.modemaski,
6723                              NULL_RTX, 1, OPTAB_DIRECT);
6724
6725   /* Prepare insertions of cmp and new_rtx into the loaded value.  When
6726      possible, we try to use insv to make this happen efficiently.  If
6727      that fails we'll generate code both inside and outside the loop.  */
6728   cmpv = s390_two_part_insv (&ac, &seq0, &seq2, mode, val, cmp);
6729   newv = s390_two_part_insv (&ac, &seq1, &seq3, mode, val, new_rtx);
6730
6731   if (seq0)
6732     emit_insn (seq0);
6733   if (seq1)
6734     emit_insn (seq1);
6735
6736   /* Start CS loop.  */
6737   if (!is_weak)
6738     {
6739       /* Begin assuming success.  */
6740       emit_move_insn (btarget, const1_rtx);
6741
6742       csloop = gen_label_rtx ();
6743       csend = gen_label_rtx ();
6744       emit_label (csloop);
6745     }
6746
6747   /* val = "<mem>00..0<mem>"
6748    * cmp = "00..0<cmp>00..0"
6749    * new = "00..0<new>00..0"
6750    */
6751
6752   emit_insn (seq2);
6753   emit_insn (seq3);
6754
6755   cc = s390_emit_compare_and_swap (EQ, res, ac.memsi, cmpv, newv);
6756   if (is_weak)
6757     emit_insn (gen_cstorecc4 (btarget, cc, XEXP (cc, 0), XEXP (cc, 1)));
6758   else
6759     {
6760       rtx tmp;
6761
6762       /* Jump to end if we're done (likely?).  */
6763       s390_emit_jump (csend, cc);
6764
6765       /* Check for changes outside mode, and loop internal if so.
6766          Arrange the moves so that the compare is adjacent to the
6767          branch so that we can generate CRJ.  */
6768       tmp = copy_to_reg (val);
6769       force_expand_binop (SImode, and_optab, res, ac.modemaski, val,
6770                           1, OPTAB_DIRECT);
6771       cc = s390_emit_compare (NE, val, tmp);
6772       s390_emit_jump (csloop, cc);
6773
6774       /* Failed.  */
6775       emit_move_insn (btarget, const0_rtx);
6776       emit_label (csend);
6777     }
6778
6779   /* Return the correct part of the bitfield.  */
6780   convert_move (vtarget, expand_simple_binop (SImode, LSHIFTRT, res, ac.shift,
6781                                               NULL_RTX, 1, OPTAB_DIRECT), 1);
6782 }
6783
6784 /* Expand an atomic operation CODE of mode MODE.  MEM is the memory location
6785    and VAL the value to play with.  If AFTER is true then store the value
6786    MEM holds after the operation, if AFTER is false then store the value MEM
6787    holds before the operation.  If TARGET is zero then discard that value, else
6788    store it to TARGET.  */
6789
6790 void
6791 s390_expand_atomic (machine_mode mode, enum rtx_code code,
6792                     rtx target, rtx mem, rtx val, bool after)
6793 {
6794   struct alignment_context ac;
6795   rtx cmp;
6796   rtx new_rtx = gen_reg_rtx (SImode);
6797   rtx orig = gen_reg_rtx (SImode);
6798   rtx_code_label *csloop = gen_label_rtx ();
6799
6800   gcc_assert (!target || register_operand (target, VOIDmode));
6801   gcc_assert (MEM_P (mem));
6802
6803   init_alignment_context (&ac, mem, mode);
6804
6805   /* Shift val to the correct bit positions.
6806      Preserve "icm", but prevent "ex icm".  */
6807   if (!(ac.aligned && code == SET && MEM_P (val)))
6808     val = s390_expand_mask_and_shift (val, mode, ac.shift);
6809
6810   /* Further preparation insns.  */
6811   if (code == PLUS || code == MINUS)
6812     emit_move_insn (orig, val);
6813   else if (code == MULT || code == AND) /* val = "11..1<val>11..1" */
6814     val = expand_simple_binop (SImode, XOR, val, ac.modemaski,
6815                                NULL_RTX, 1, OPTAB_DIRECT);
6816
6817   /* Load full word.  Subsequent loads are performed by CS.  */
6818   cmp = force_reg (SImode, ac.memsi);
6819
6820   /* Start CS loop.  */
6821   emit_label (csloop);
6822   emit_move_insn (new_rtx, cmp);
6823
6824   /* Patch new with val at correct position.  */
6825   switch (code)
6826     {
6827     case PLUS:
6828     case MINUS:
6829       val = expand_simple_binop (SImode, code, new_rtx, orig,
6830                                  NULL_RTX, 1, OPTAB_DIRECT);
6831       val = expand_simple_binop (SImode, AND, val, ac.modemask,
6832                                  NULL_RTX, 1, OPTAB_DIRECT);
6833       /* FALLTHRU */
6834     case SET:
6835       if (ac.aligned && MEM_P (val))
6836         store_bit_field (new_rtx, GET_MODE_BITSIZE (mode), 0,
6837                          0, 0, SImode, val, false);
6838       else
6839         {
6840           new_rtx = expand_simple_binop (SImode, AND, new_rtx, ac.modemaski,
6841                                      NULL_RTX, 1, OPTAB_DIRECT);
6842           new_rtx = expand_simple_binop (SImode, IOR, new_rtx, val,
6843                                      NULL_RTX, 1, OPTAB_DIRECT);
6844         }
6845       break;
6846     case AND:
6847     case IOR:
6848     case XOR:
6849       new_rtx = expand_simple_binop (SImode, code, new_rtx, val,
6850                                  NULL_RTX, 1, OPTAB_DIRECT);
6851       break;
6852     case MULT: /* NAND */
6853       new_rtx = expand_simple_binop (SImode, AND, new_rtx, val,
6854                                  NULL_RTX, 1, OPTAB_DIRECT);
6855       new_rtx = expand_simple_binop (SImode, XOR, new_rtx, ac.modemask,
6856                                  NULL_RTX, 1, OPTAB_DIRECT);
6857       break;
6858     default:
6859       gcc_unreachable ();
6860     }
6861
6862   s390_emit_jump (csloop, s390_emit_compare_and_swap (NE, cmp,
6863                                                       ac.memsi, cmp, new_rtx));
6864
6865   /* Return the correct part of the bitfield.  */
6866   if (target)
6867     convert_move (target, expand_simple_binop (SImode, LSHIFTRT,
6868                                                after ? new_rtx : cmp, ac.shift,
6869                                                NULL_RTX, 1, OPTAB_DIRECT), 1);
6870 }
6871
6872 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
6873    We need to emit DTP-relative relocations.  */
6874
6875 static void s390_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
6876
6877 static void
6878 s390_output_dwarf_dtprel (FILE *file, int size, rtx x)
6879 {
6880   switch (size)
6881     {
6882     case 4:
6883       fputs ("\t.long\t", file);
6884       break;
6885     case 8:
6886       fputs ("\t.quad\t", file);
6887       break;
6888     default:
6889       gcc_unreachable ();
6890     }
6891   output_addr_const (file, x);
6892   fputs ("@DTPOFF", file);
6893 }
6894
6895 /* Return the proper mode for REGNO being represented in the dwarf
6896    unwind table.  */
6897 machine_mode
6898 s390_dwarf_frame_reg_mode (int regno)
6899 {
6900   machine_mode save_mode = default_dwarf_frame_reg_mode (regno);
6901
6902   /* Make sure not to return DImode for any GPR with -m31 -mzarch.  */
6903   if (GENERAL_REGNO_P (regno))
6904     save_mode = Pmode;
6905
6906   /* The rightmost 64 bits of vector registers are call-clobbered.  */
6907   if (GET_MODE_SIZE (save_mode) > 8)
6908     save_mode = DImode;
6909
6910   return save_mode;
6911 }
6912
6913 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
6914 /* Implement TARGET_MANGLE_TYPE.  */
6915
6916 static const char *
6917 s390_mangle_type (const_tree type)
6918 {
6919   type = TYPE_MAIN_VARIANT (type);
6920
6921   if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
6922       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
6923     return NULL;
6924
6925   if (type == s390_builtin_types[BT_BV16QI]) return "U6__boolc";
6926   if (type == s390_builtin_types[BT_BV8HI]) return "U6__bools";
6927   if (type == s390_builtin_types[BT_BV4SI]) return "U6__booli";
6928   if (type == s390_builtin_types[BT_BV2DI]) return "U6__booll";
6929
6930   if (TYPE_MAIN_VARIANT (type) == long_double_type_node
6931       && TARGET_LONG_DOUBLE_128)
6932     return "g";
6933
6934   /* For all other types, use normal C++ mangling.  */
6935   return NULL;
6936 }
6937 #endif
6938
6939 /* In the name of slightly smaller debug output, and to cater to
6940    general assembler lossage, recognize various UNSPEC sequences
6941    and turn them back into a direct symbol reference.  */
6942
6943 static rtx
6944 s390_delegitimize_address (rtx orig_x)
6945 {
6946   rtx x, y;
6947
6948   orig_x = delegitimize_mem_from_attrs (orig_x);
6949   x = orig_x;
6950
6951   /* Extract the symbol ref from:
6952      (plus:SI (reg:SI 12 %r12)
6953               (const:SI (unspec:SI [(symbol_ref/f:SI ("*.LC0"))]
6954                                     UNSPEC_GOTOFF/PLTOFF)))
6955      and
6956      (plus:SI (reg:SI 12 %r12)
6957               (const:SI (plus:SI (unspec:SI [(symbol_ref:SI ("L"))]
6958                                              UNSPEC_GOTOFF/PLTOFF)
6959                                  (const_int 4 [0x4]))))  */
6960   if (GET_CODE (x) == PLUS
6961       && REG_P (XEXP (x, 0))
6962       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM
6963       && GET_CODE (XEXP (x, 1)) == CONST)
6964     {
6965       HOST_WIDE_INT offset = 0;
6966
6967       /* The const operand.  */
6968       y = XEXP (XEXP (x, 1), 0);
6969
6970       if (GET_CODE (y) == PLUS
6971           && GET_CODE (XEXP (y, 1)) == CONST_INT)
6972         {
6973           offset = INTVAL (XEXP (y, 1));
6974           y = XEXP (y, 0);
6975         }
6976
6977       if (GET_CODE (y) == UNSPEC
6978           && (XINT (y, 1) == UNSPEC_GOTOFF
6979               || XINT (y, 1) == UNSPEC_PLTOFF))
6980         return plus_constant (Pmode, XVECEXP (y, 0, 0), offset);
6981     }
6982
6983   if (GET_CODE (x) != MEM)
6984     return orig_x;
6985
6986   x = XEXP (x, 0);
6987   if (GET_CODE (x) == PLUS
6988       && GET_CODE (XEXP (x, 1)) == CONST
6989       && GET_CODE (XEXP (x, 0)) == REG
6990       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
6991     {
6992       y = XEXP (XEXP (x, 1), 0);
6993       if (GET_CODE (y) == UNSPEC
6994           && XINT (y, 1) == UNSPEC_GOT)
6995         y = XVECEXP (y, 0, 0);
6996       else
6997         return orig_x;
6998     }
6999   else if (GET_CODE (x) == CONST)
7000     {
7001       /* Extract the symbol ref from:
7002          (mem:QI (const:DI (unspec:DI [(symbol_ref:DI ("foo"))]
7003                                        UNSPEC_PLT/GOTENT)))  */
7004
7005       y = XEXP (x, 0);
7006       if (GET_CODE (y) == UNSPEC
7007           && (XINT (y, 1) == UNSPEC_GOTENT
7008               || XINT (y, 1) == UNSPEC_PLT))
7009         y = XVECEXP (y, 0, 0);
7010       else
7011         return orig_x;
7012     }
7013   else
7014     return orig_x;
7015
7016   if (GET_MODE (orig_x) != Pmode)
7017     {
7018       if (GET_MODE (orig_x) == BLKmode)
7019         return orig_x;
7020       y = lowpart_subreg (GET_MODE (orig_x), y, Pmode);
7021       if (y == NULL_RTX)
7022         return orig_x;
7023     }
7024   return y;
7025 }
7026
7027 /* Output operand OP to stdio stream FILE.
7028    OP is an address (register + offset) which is not used to address data;
7029    instead the rightmost bits are interpreted as the value.  */
7030
7031 static void
7032 print_addrstyle_operand (FILE *file, rtx op)
7033 {
7034   HOST_WIDE_INT offset;
7035   rtx base;
7036
7037   /* Extract base register and offset.  */
7038   if (!s390_decompose_addrstyle_without_index (op, &base, &offset))
7039     gcc_unreachable ();
7040
7041   /* Sanity check.  */
7042   if (base)
7043     {
7044       gcc_assert (GET_CODE (base) == REG);
7045       gcc_assert (REGNO (base) < FIRST_PSEUDO_REGISTER);
7046       gcc_assert (REGNO_REG_CLASS (REGNO (base)) == ADDR_REGS);
7047     }
7048
7049   /* Offsets are constricted to twelve bits.  */
7050   fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset & ((1 << 12) - 1));
7051   if (base)
7052     fprintf (file, "(%s)", reg_names[REGNO (base)]);
7053 }
7054
7055 /* Assigns the number of NOP halfwords to be emitted before and after the
7056    function label to *HW_BEFORE and *HW_AFTER.  Both pointers must not be NULL.
7057    If hotpatching is disabled for the function, the values are set to zero.
7058 */
7059
7060 static void
7061 s390_function_num_hotpatch_hw (tree decl,
7062                                int *hw_before,
7063                                int *hw_after)
7064 {
7065   tree attr;
7066
7067   attr = lookup_attribute ("hotpatch", DECL_ATTRIBUTES (decl));
7068
7069   /* Handle the arguments of the hotpatch attribute.  The values
7070      specified via attribute might override the cmdline argument
7071      values.  */
7072   if (attr)
7073     {
7074       tree args = TREE_VALUE (attr);
7075
7076       *hw_before = TREE_INT_CST_LOW (TREE_VALUE (args));
7077       *hw_after = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args)));
7078     }
7079   else
7080     {
7081       /* Use the values specified by the cmdline arguments.  */
7082       *hw_before = s390_hotpatch_hw_before_label;
7083       *hw_after = s390_hotpatch_hw_after_label;
7084     }
7085 }
7086
7087 /* Write the current .machine and .machinemode specification to the assembler
7088    file.  */
7089
7090 #ifdef HAVE_AS_MACHINE_MACHINEMODE
7091 static void
7092 s390_asm_output_machine_for_arch (FILE *asm_out_file)
7093 {
7094   fprintf (asm_out_file, "\t.machinemode %s\n",
7095            (TARGET_ZARCH) ? "zarch" : "esa");
7096   fprintf (asm_out_file, "\t.machine \"%s", processor_table[s390_arch].name);
7097   if (S390_USE_ARCHITECTURE_MODIFIERS)
7098     {
7099       int cpu_flags;
7100
7101       cpu_flags = processor_flags_table[(int) s390_arch];
7102       if (TARGET_HTM && !(cpu_flags & PF_TX))
7103         fprintf (asm_out_file, "+htm");
7104       else if (!TARGET_HTM && (cpu_flags & PF_TX))
7105         fprintf (asm_out_file, "+nohtm");
7106       if (TARGET_VX && !(cpu_flags & PF_VX))
7107         fprintf (asm_out_file, "+vx");
7108       else if (!TARGET_VX && (cpu_flags & PF_VX))
7109         fprintf (asm_out_file, "+novx");
7110     }
7111   fprintf (asm_out_file, "\"\n");
7112 }
7113
7114 /* Write an extra function header before the very start of the function.  */
7115
7116 void
7117 s390_asm_output_function_prefix (FILE *asm_out_file,
7118                                  const char *fnname ATTRIBUTE_UNUSED)
7119 {
7120   if (DECL_FUNCTION_SPECIFIC_TARGET (current_function_decl) == NULL)
7121     return;
7122   /* Since only the function specific options are saved but not the indications
7123      which options are set, it's too much work here to figure out which options
7124      have actually changed.  Thus, generate .machine and .machinemode whenever a
7125      function has the target attribute or pragma.  */
7126   fprintf (asm_out_file, "\t.machinemode push\n");
7127   fprintf (asm_out_file, "\t.machine push\n");
7128   s390_asm_output_machine_for_arch (asm_out_file);
7129 }
7130
7131 /* Write an extra function footer after the very end of the function.  */
7132
7133 void
7134 s390_asm_declare_function_size (FILE *asm_out_file,
7135                                 const char *fnname, tree decl)
7136 {
7137   if (!flag_inhibit_size_directive)
7138     ASM_OUTPUT_MEASURED_SIZE (asm_out_file, fnname);
7139   if (DECL_FUNCTION_SPECIFIC_TARGET (decl) == NULL)
7140     return;
7141   fprintf (asm_out_file, "\t.machine pop\n");
7142   fprintf (asm_out_file, "\t.machinemode pop\n");
7143 }
7144 #endif
7145
7146 /* Write the extra assembler code needed to declare a function properly.  */
7147
7148 void
7149 s390_asm_output_function_label (FILE *asm_out_file, const char *fname,
7150                                 tree decl)
7151 {
7152   int hw_before, hw_after;
7153
7154   s390_function_num_hotpatch_hw (decl, &hw_before, &hw_after);
7155   if (hw_before > 0)
7156     {
7157       unsigned int function_alignment;
7158       int i;
7159
7160       /* Add a trampoline code area before the function label and initialize it
7161          with two-byte nop instructions.  This area can be overwritten with code
7162          that jumps to a patched version of the function.  */
7163       asm_fprintf (asm_out_file, "\tnopr\t%%r7"
7164                    "\t# pre-label NOPs for hotpatch (%d halfwords)\n",
7165                    hw_before);
7166       for (i = 1; i < hw_before; i++)
7167         fputs ("\tnopr\t%r7\n", asm_out_file);
7168
7169       /* Note:  The function label must be aligned so that (a) the bytes of the
7170          following nop do not cross a cacheline boundary, and (b) a jump address
7171          (eight bytes for 64 bit targets, 4 bytes for 32 bit targets) can be
7172          stored directly before the label without crossing a cacheline
7173          boundary.  All this is necessary to make sure the trampoline code can
7174          be changed atomically.
7175          This alignment is done automatically using the FOUNCTION_BOUNDARY, but
7176          if there are NOPs before the function label, the alignment is placed
7177          before them.  So it is necessary to duplicate the alignment after the
7178          NOPs.  */
7179       function_alignment = MAX (8, DECL_ALIGN (decl) / BITS_PER_UNIT);
7180       if (! DECL_USER_ALIGN (decl))
7181         function_alignment = MAX (function_alignment,
7182                                   (unsigned int) align_functions);
7183       fputs ("\t# alignment for hotpatch\n", asm_out_file);
7184       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (function_alignment));
7185     }
7186
7187   if (S390_USE_TARGET_ATTRIBUTE && TARGET_DEBUG_ARG)
7188     {
7189       asm_fprintf (asm_out_file, "\t# fn:%s ar%d\n", fname, s390_arch);
7190       asm_fprintf (asm_out_file, "\t# fn:%s tu%d\n", fname, s390_tune);
7191       asm_fprintf (asm_out_file, "\t# fn:%s sg%d\n", fname, s390_stack_guard);
7192       asm_fprintf (asm_out_file, "\t# fn:%s ss%d\n", fname, s390_stack_size);
7193       asm_fprintf (asm_out_file, "\t# fn:%s bc%d\n", fname, s390_branch_cost);
7194       asm_fprintf (asm_out_file, "\t# fn:%s wf%d\n", fname,
7195                    s390_warn_framesize);
7196       asm_fprintf (asm_out_file, "\t# fn:%s ba%d\n", fname, TARGET_BACKCHAIN);
7197       asm_fprintf (asm_out_file, "\t# fn:%s hd%d\n", fname, TARGET_HARD_DFP);
7198       asm_fprintf (asm_out_file, "\t# fn:%s hf%d\n", fname, !TARGET_SOFT_FLOAT);
7199       asm_fprintf (asm_out_file, "\t# fn:%s ht%d\n", fname, TARGET_OPT_HTM);
7200       asm_fprintf (asm_out_file, "\t# fn:%s vx%d\n", fname, TARGET_OPT_VX);
7201       asm_fprintf (asm_out_file, "\t# fn:%s ps%d\n", fname,
7202                    TARGET_PACKED_STACK);
7203       asm_fprintf (asm_out_file, "\t# fn:%s se%d\n", fname, TARGET_SMALL_EXEC);
7204       asm_fprintf (asm_out_file, "\t# fn:%s mv%d\n", fname, TARGET_MVCLE);
7205       asm_fprintf (asm_out_file, "\t# fn:%s zv%d\n", fname, TARGET_ZVECTOR);
7206       asm_fprintf (asm_out_file, "\t# fn:%s wd%d\n", fname,
7207                    s390_warn_dynamicstack_p);
7208     }
7209   ASM_OUTPUT_LABEL (asm_out_file, fname);
7210   if (hw_after > 0)
7211     asm_fprintf (asm_out_file,
7212                  "\t# post-label NOPs for hotpatch (%d halfwords)\n",
7213                  hw_after);
7214 }
7215
7216 /* Output machine-dependent UNSPECs occurring in address constant X
7217    in assembler syntax to stdio stream FILE.  Returns true if the
7218    constant X could be recognized, false otherwise.  */
7219
7220 static bool
7221 s390_output_addr_const_extra (FILE *file, rtx x)
7222 {
7223   if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 1)
7224     switch (XINT (x, 1))
7225       {
7226       case UNSPEC_GOTENT:
7227         output_addr_const (file, XVECEXP (x, 0, 0));
7228         fprintf (file, "@GOTENT");
7229         return true;
7230       case UNSPEC_GOT:
7231         output_addr_const (file, XVECEXP (x, 0, 0));
7232         fprintf (file, "@GOT");
7233         return true;
7234       case UNSPEC_GOTOFF:
7235         output_addr_const (file, XVECEXP (x, 0, 0));
7236         fprintf (file, "@GOTOFF");
7237         return true;
7238       case UNSPEC_PLT:
7239         output_addr_const (file, XVECEXP (x, 0, 0));
7240         fprintf (file, "@PLT");
7241         return true;
7242       case UNSPEC_PLTOFF:
7243         output_addr_const (file, XVECEXP (x, 0, 0));
7244         fprintf (file, "@PLTOFF");
7245         return true;
7246       case UNSPEC_TLSGD:
7247         output_addr_const (file, XVECEXP (x, 0, 0));
7248         fprintf (file, "@TLSGD");
7249         return true;
7250       case UNSPEC_TLSLDM:
7251         assemble_name (file, get_some_local_dynamic_name ());
7252         fprintf (file, "@TLSLDM");
7253         return true;
7254       case UNSPEC_DTPOFF:
7255         output_addr_const (file, XVECEXP (x, 0, 0));
7256         fprintf (file, "@DTPOFF");
7257         return true;
7258       case UNSPEC_NTPOFF:
7259         output_addr_const (file, XVECEXP (x, 0, 0));
7260         fprintf (file, "@NTPOFF");
7261         return true;
7262       case UNSPEC_GOTNTPOFF:
7263         output_addr_const (file, XVECEXP (x, 0, 0));
7264         fprintf (file, "@GOTNTPOFF");
7265         return true;
7266       case UNSPEC_INDNTPOFF:
7267         output_addr_const (file, XVECEXP (x, 0, 0));
7268         fprintf (file, "@INDNTPOFF");
7269         return true;
7270       }
7271
7272   if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 2)
7273     switch (XINT (x, 1))
7274       {
7275       case UNSPEC_POOL_OFFSET:
7276         x = gen_rtx_MINUS (GET_MODE (x), XVECEXP (x, 0, 0), XVECEXP (x, 0, 1));
7277         output_addr_const (file, x);
7278         return true;
7279       }
7280   return false;
7281 }
7282
7283 /* Output address operand ADDR in assembler syntax to
7284    stdio stream FILE.  */
7285
7286 void
7287 print_operand_address (FILE *file, rtx addr)
7288 {
7289   struct s390_address ad;
7290
7291   if (s390_loadrelative_operand_p (addr, NULL, NULL))
7292     {
7293       if (!TARGET_Z10)
7294         {
7295           output_operand_lossage ("symbolic memory references are "
7296                                   "only supported on z10 or later");
7297           return;
7298         }
7299       output_addr_const (file, addr);
7300       return;
7301     }
7302
7303   if (!s390_decompose_address (addr, &ad)
7304       || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
7305       || (ad.indx && !REGNO_OK_FOR_INDEX_P (REGNO (ad.indx))))
7306     output_operand_lossage ("cannot decompose address");
7307
7308   if (ad.disp)
7309     output_addr_const (file, ad.disp);
7310   else
7311     fprintf (file, "0");
7312
7313   if (ad.base && ad.indx)
7314     fprintf (file, "(%s,%s)", reg_names[REGNO (ad.indx)],
7315                               reg_names[REGNO (ad.base)]);
7316   else if (ad.base)
7317     fprintf (file, "(%s)", reg_names[REGNO (ad.base)]);
7318 }
7319
7320 /* Output operand X in assembler syntax to stdio stream FILE.
7321    CODE specified the format flag.  The following format flags
7322    are recognized:
7323
7324     'C': print opcode suffix for branch condition.
7325     'D': print opcode suffix for inverse branch condition.
7326     'E': print opcode suffix for branch on index instruction.
7327     'G': print the size of the operand in bytes.
7328     'J': print tls_load/tls_gdcall/tls_ldcall suffix
7329     'M': print the second word of a TImode operand.
7330     'N': print the second word of a DImode operand.
7331     'O': print only the displacement of a memory reference or address.
7332     'R': print only the base register of a memory reference or address.
7333     'S': print S-type memory reference (base+displacement).
7334     'Y': print address style operand without index (e.g. shift count or setmem
7335          operand).
7336
7337     'b': print integer X as if it's an unsigned byte.
7338     'c': print integer X as if it's an signed byte.
7339     'e': "end" contiguous bitmask X in either DImode or vector inner mode.
7340     'f': "end" contiguous bitmask X in SImode.
7341     'h': print integer X as if it's a signed halfword.
7342     'i': print the first nonzero HImode part of X.
7343     'j': print the first HImode part unequal to -1 of X.
7344     'k': print the first nonzero SImode part of X.
7345     'm': print the first SImode part unequal to -1 of X.
7346     'o': print integer X as if it's an unsigned 32bit word.
7347     's': "start" of contiguous bitmask X in either DImode or vector inner mode.
7348     't': CONST_INT: "start" of contiguous bitmask X in SImode.
7349          CONST_VECTOR: Generate a bitmask for vgbm instruction.
7350     'x': print integer X as if it's an unsigned halfword.
7351     'v': print register number as vector register (v1 instead of f1).
7352 */
7353
7354 void
7355 print_operand (FILE *file, rtx x, int code)
7356 {
7357   HOST_WIDE_INT ival;
7358
7359   switch (code)
7360     {
7361     case 'C':
7362       fprintf (file, s390_branch_condition_mnemonic (x, FALSE));
7363       return;
7364
7365     case 'D':
7366       fprintf (file, s390_branch_condition_mnemonic (x, TRUE));
7367       return;
7368
7369     case 'E':
7370       if (GET_CODE (x) == LE)
7371         fprintf (file, "l");
7372       else if (GET_CODE (x) == GT)
7373         fprintf (file, "h");
7374       else
7375         output_operand_lossage ("invalid comparison operator "
7376                                 "for 'E' output modifier");
7377       return;
7378
7379     case 'J':
7380       if (GET_CODE (x) == SYMBOL_REF)
7381         {
7382           fprintf (file, "%s", ":tls_load:");
7383           output_addr_const (file, x);
7384         }
7385       else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSGD)
7386         {
7387           fprintf (file, "%s", ":tls_gdcall:");
7388           output_addr_const (file, XVECEXP (x, 0, 0));
7389         }
7390       else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSLDM)
7391         {
7392           fprintf (file, "%s", ":tls_ldcall:");
7393           const char *name = get_some_local_dynamic_name ();
7394           gcc_assert (name);
7395           assemble_name (file, name);
7396         }
7397       else
7398         output_operand_lossage ("invalid reference for 'J' output modifier");
7399       return;
7400
7401     case 'G':
7402       fprintf (file, "%u", GET_MODE_SIZE (GET_MODE (x)));
7403       return;
7404
7405     case 'O':
7406       {
7407         struct s390_address ad;
7408         int ret;
7409
7410         ret = s390_decompose_address (MEM_P (x) ? XEXP (x, 0) : x, &ad);
7411
7412         if (!ret
7413             || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
7414             || ad.indx)
7415           {
7416             output_operand_lossage ("invalid address for 'O' output modifier");
7417             return;
7418           }
7419
7420         if (ad.disp)
7421           output_addr_const (file, ad.disp);
7422         else
7423           fprintf (file, "0");
7424       }
7425       return;
7426
7427     case 'R':
7428       {
7429         struct s390_address ad;
7430         int ret;
7431
7432         ret = s390_decompose_address (MEM_P (x) ? XEXP (x, 0) : x, &ad);
7433
7434         if (!ret
7435             || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
7436             || ad.indx)
7437           {
7438             output_operand_lossage ("invalid address for 'R' output modifier");
7439             return;
7440           }
7441
7442         if (ad.base)
7443           fprintf (file, "%s", reg_names[REGNO (ad.base)]);
7444         else
7445           fprintf (file, "0");
7446       }
7447       return;
7448
7449     case 'S':
7450       {
7451         struct s390_address ad;
7452         int ret;
7453
7454         if (!MEM_P (x))
7455           {
7456             output_operand_lossage ("memory reference expected for "
7457                                     "'S' output modifier");
7458             return;
7459           }
7460         ret = s390_decompose_address (XEXP (x, 0), &ad);
7461
7462         if (!ret
7463             || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
7464             || ad.indx)
7465           {
7466             output_operand_lossage ("invalid address for 'S' output modifier");
7467             return;
7468           }
7469
7470         if (ad.disp)
7471           output_addr_const (file, ad.disp);
7472         else
7473           fprintf (file, "0");
7474
7475         if (ad.base)
7476           fprintf (file, "(%s)", reg_names[REGNO (ad.base)]);
7477       }
7478       return;
7479
7480     case 'N':
7481       if (GET_CODE (x) == REG)
7482         x = gen_rtx_REG (GET_MODE (x), REGNO (x) + 1);
7483       else if (GET_CODE (x) == MEM)
7484         x = change_address (x, VOIDmode,
7485                             plus_constant (Pmode, XEXP (x, 0), 4));
7486       else
7487         output_operand_lossage ("register or memory expression expected "
7488                                 "for 'N' output modifier");
7489       break;
7490
7491     case 'M':
7492       if (GET_CODE (x) == REG)
7493         x = gen_rtx_REG (GET_MODE (x), REGNO (x) + 1);
7494       else if (GET_CODE (x) == MEM)
7495         x = change_address (x, VOIDmode,
7496                             plus_constant (Pmode, XEXP (x, 0), 8));
7497       else
7498         output_operand_lossage ("register or memory expression expected "
7499                                 "for 'M' output modifier");
7500       break;
7501
7502     case 'Y':
7503       print_addrstyle_operand (file, x);
7504       return;
7505     }
7506
7507   switch (GET_CODE (x))
7508     {
7509     case REG:
7510       /* Print FP regs as fx instead of vx when they are accessed
7511          through non-vector mode.  */
7512       if (code == 'v'
7513           || VECTOR_NOFP_REG_P (x)
7514           || (FP_REG_P (x) && VECTOR_MODE_P (GET_MODE (x)))
7515           || (VECTOR_REG_P (x)
7516               && (GET_MODE_SIZE (GET_MODE (x)) /
7517                   s390_class_max_nregs (FP_REGS, GET_MODE (x))) > 8))
7518         fprintf (file, "%%v%s", reg_names[REGNO (x)] + 2);
7519       else
7520         fprintf (file, "%s", reg_names[REGNO (x)]);
7521       break;
7522
7523     case MEM:
7524       output_address (GET_MODE (x), XEXP (x, 0));
7525       break;
7526
7527     case CONST:
7528     case CODE_LABEL:
7529     case LABEL_REF:
7530     case SYMBOL_REF:
7531       output_addr_const (file, x);
7532       break;
7533
7534     case CONST_INT:
7535       ival = INTVAL (x);
7536       switch (code)
7537         {
7538         case 0:
7539           break;
7540         case 'b':
7541           ival &= 0xff;
7542           break;
7543         case 'c':
7544           ival = ((ival & 0xff) ^ 0x80) - 0x80;
7545           break;
7546         case 'x':
7547           ival &= 0xffff;
7548           break;
7549         case 'h':
7550           ival = ((ival & 0xffff) ^ 0x8000) - 0x8000;
7551           break;
7552         case 'i':
7553           ival = s390_extract_part (x, HImode, 0);
7554           break;
7555         case 'j':
7556           ival = s390_extract_part (x, HImode, -1);
7557           break;
7558         case 'k':
7559           ival = s390_extract_part (x, SImode, 0);
7560           break;
7561         case 'm':
7562           ival = s390_extract_part (x, SImode, -1);
7563           break;
7564         case 'o':
7565           ival &= 0xffffffff;
7566           break;
7567         case 'e': case 'f':
7568         case 's': case 't':
7569           {
7570             int start, end;
7571             int len;
7572             bool ok;
7573
7574             len = (code == 's' || code == 'e' ? 64 : 32);
7575             ok = s390_contiguous_bitmask_p (ival, true, len, &start, &end);
7576             gcc_assert (ok);
7577             if (code == 's' || code == 't')
7578               ival = start;
7579             else
7580               ival = end;
7581           }
7582           break;
7583         default:
7584           output_operand_lossage ("invalid constant for output modifier '%c'", code);
7585         }
7586       fprintf (file, HOST_WIDE_INT_PRINT_DEC, ival);
7587       break;
7588
7589     case CONST_WIDE_INT:
7590       if (code == 'b')
7591         fprintf (file, HOST_WIDE_INT_PRINT_DEC,
7592                  CONST_WIDE_INT_ELT (x, 0) & 0xff);
7593       else if (code == 'x')
7594         fprintf (file, HOST_WIDE_INT_PRINT_DEC,
7595                  CONST_WIDE_INT_ELT (x, 0) & 0xffff);
7596       else if (code == 'h')
7597         fprintf (file, HOST_WIDE_INT_PRINT_DEC,
7598                  ((CONST_WIDE_INT_ELT (x, 0) & 0xffff) ^ 0x8000) - 0x8000);
7599       else
7600         {
7601           if (code == 0)
7602             output_operand_lossage ("invalid constant - try using "
7603                                     "an output modifier");
7604           else
7605             output_operand_lossage ("invalid constant for output modifier '%c'",
7606                                     code);
7607         }
7608       break;
7609     case CONST_VECTOR:
7610       switch (code)
7611         {
7612         case 'h':
7613           gcc_assert (const_vec_duplicate_p (x));
7614           fprintf (file, HOST_WIDE_INT_PRINT_DEC,
7615                    ((INTVAL (XVECEXP (x, 0, 0)) & 0xffff) ^ 0x8000) - 0x8000);
7616           break;
7617         case 'e':
7618         case 's':
7619           {
7620             int start, end;
7621             bool ok;
7622
7623             ok = s390_contiguous_bitmask_vector_p (x, &start, &end);
7624             gcc_assert (ok);
7625             ival = (code == 's') ? start : end;
7626             fprintf (file, HOST_WIDE_INT_PRINT_DEC, ival);
7627           }
7628           break;
7629         case 't':
7630           {
7631             unsigned mask;
7632             bool ok = s390_bytemask_vector_p (x, &mask);
7633             gcc_assert (ok);
7634             fprintf (file, "%u", mask);
7635           }
7636           break;
7637
7638         default:
7639           output_operand_lossage ("invalid constant vector for output "
7640                                   "modifier '%c'", code);
7641         }
7642       break;
7643
7644     default:
7645       if (code == 0)
7646         output_operand_lossage ("invalid expression - try using "
7647                                 "an output modifier");
7648       else
7649         output_operand_lossage ("invalid expression for output "
7650                                 "modifier '%c'", code);
7651       break;
7652     }
7653 }
7654
7655 /* Target hook for assembling integer objects.  We need to define it
7656    here to work a round a bug in some versions of GAS, which couldn't
7657    handle values smaller than INT_MIN when printed in decimal.  */
7658
7659 static bool
7660 s390_assemble_integer (rtx x, unsigned int size, int aligned_p)
7661 {
7662   if (size == 8 && aligned_p
7663       && GET_CODE (x) == CONST_INT && INTVAL (x) < INT_MIN)
7664     {
7665       fprintf (asm_out_file, "\t.quad\t" HOST_WIDE_INT_PRINT_HEX "\n",
7666                INTVAL (x));
7667       return true;
7668     }
7669   return default_assemble_integer (x, size, aligned_p);
7670 }
7671
7672 /* Returns true if register REGNO is used  for forming
7673    a memory address in expression X.  */
7674
7675 static bool
7676 reg_used_in_mem_p (int regno, rtx x)
7677 {
7678   enum rtx_code code = GET_CODE (x);
7679   int i, j;
7680   const char *fmt;
7681
7682   if (code == MEM)
7683     {
7684       if (refers_to_regno_p (regno, XEXP (x, 0)))
7685         return true;
7686     }
7687   else if (code == SET
7688            && GET_CODE (SET_DEST (x)) == PC)
7689     {
7690       if (refers_to_regno_p (regno, SET_SRC (x)))
7691         return true;
7692     }
7693
7694   fmt = GET_RTX_FORMAT (code);
7695   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7696     {
7697       if (fmt[i] == 'e'
7698           && reg_used_in_mem_p (regno, XEXP (x, i)))
7699         return true;
7700
7701       else if (fmt[i] == 'E')
7702         for (j = 0; j < XVECLEN (x, i); j++)
7703           if (reg_used_in_mem_p (regno, XVECEXP (x, i, j)))
7704             return true;
7705     }
7706   return false;
7707 }
7708
7709 /* Returns true if expression DEP_RTX sets an address register
7710    used by instruction INSN to address memory.  */
7711
7712 static bool
7713 addr_generation_dependency_p (rtx dep_rtx, rtx_insn *insn)
7714 {
7715   rtx target, pat;
7716
7717   if (NONJUMP_INSN_P (dep_rtx))
7718     dep_rtx = PATTERN (dep_rtx);
7719
7720   if (GET_CODE (dep_rtx) == SET)
7721     {
7722       target = SET_DEST (dep_rtx);
7723       if (GET_CODE (target) == STRICT_LOW_PART)
7724         target = XEXP (target, 0);
7725       while (GET_CODE (target) == SUBREG)
7726         target = SUBREG_REG (target);
7727
7728       if (GET_CODE (target) == REG)
7729         {
7730           int regno = REGNO (target);
7731
7732           if (s390_safe_attr_type (insn) == TYPE_LA)
7733             {
7734               pat = PATTERN (insn);
7735               if (GET_CODE (pat) == PARALLEL)
7736                 {
7737                   gcc_assert (XVECLEN (pat, 0) == 2);
7738                   pat = XVECEXP (pat, 0, 0);
7739                 }
7740               gcc_assert (GET_CODE (pat) == SET);
7741               return refers_to_regno_p (regno, SET_SRC (pat));
7742             }
7743           else if (get_attr_atype (insn) == ATYPE_AGEN)
7744             return reg_used_in_mem_p (regno, PATTERN (insn));
7745         }
7746     }
7747   return false;
7748 }
7749
7750 /* Return 1, if dep_insn sets register used in insn in the agen unit.  */
7751
7752 int
7753 s390_agen_dep_p (rtx_insn *dep_insn, rtx_insn *insn)
7754 {
7755   rtx dep_rtx = PATTERN (dep_insn);
7756   int i;
7757
7758   if (GET_CODE (dep_rtx) == SET
7759       && addr_generation_dependency_p (dep_rtx, insn))
7760     return 1;
7761   else if (GET_CODE (dep_rtx) == PARALLEL)
7762     {
7763       for (i = 0; i < XVECLEN (dep_rtx, 0); i++)
7764         {
7765           if (addr_generation_dependency_p (XVECEXP (dep_rtx, 0, i), insn))
7766             return 1;
7767         }
7768     }
7769   return 0;
7770 }
7771
7772
7773 /* A C statement (sans semicolon) to update the integer scheduling priority
7774    INSN_PRIORITY (INSN).  Increase the priority to execute the INSN earlier,
7775    reduce the priority to execute INSN later.  Do not define this macro if
7776    you do not need to adjust the scheduling priorities of insns.
7777
7778    A STD instruction should be scheduled earlier,
7779    in order to use the bypass.  */
7780 static int
7781 s390_adjust_priority (rtx_insn *insn, int priority)
7782 {
7783   if (! INSN_P (insn))
7784     return priority;
7785
7786   if (s390_tune <= PROCESSOR_2064_Z900)
7787     return priority;
7788
7789   switch (s390_safe_attr_type (insn))
7790     {
7791       case TYPE_FSTOREDF:
7792       case TYPE_FSTORESF:
7793         priority = priority << 3;
7794         break;
7795       case TYPE_STORE:
7796       case TYPE_STM:
7797         priority = priority << 1;
7798         break;
7799       default:
7800         break;
7801     }
7802   return priority;
7803 }
7804
7805
7806 /* The number of instructions that can be issued per cycle.  */
7807
7808 static int
7809 s390_issue_rate (void)
7810 {
7811   switch (s390_tune)
7812     {
7813     case PROCESSOR_2084_Z990:
7814     case PROCESSOR_2094_Z9_109:
7815     case PROCESSOR_2094_Z9_EC:
7816     case PROCESSOR_2817_Z196:
7817       return 3;
7818     case PROCESSOR_2097_Z10:
7819       return 2;
7820     case PROCESSOR_9672_G5:
7821     case PROCESSOR_9672_G6:
7822     case PROCESSOR_2064_Z900:
7823       /* Starting with EC12 we use the sched_reorder hook to take care
7824          of instruction dispatch constraints.  The algorithm only
7825          picks the best instruction and assumes only a single
7826          instruction gets issued per cycle.  */
7827     case PROCESSOR_2827_ZEC12:
7828     case PROCESSOR_2964_Z13:
7829     default:
7830       return 1;
7831     }
7832 }
7833
7834 static int
7835 s390_first_cycle_multipass_dfa_lookahead (void)
7836 {
7837   return 4;
7838 }
7839
7840 /* Annotate every literal pool reference in X by an UNSPEC_LTREF expression.
7841    Fix up MEMs as required.  */
7842
7843 static void
7844 annotate_constant_pool_refs (rtx *x)
7845 {
7846   int i, j;
7847   const char *fmt;
7848
7849   gcc_assert (GET_CODE (*x) != SYMBOL_REF
7850               || !CONSTANT_POOL_ADDRESS_P (*x));
7851
7852   /* Literal pool references can only occur inside a MEM ...  */
7853   if (GET_CODE (*x) == MEM)
7854     {
7855       rtx memref = XEXP (*x, 0);
7856
7857       if (GET_CODE (memref) == SYMBOL_REF
7858           && CONSTANT_POOL_ADDRESS_P (memref))
7859         {
7860           rtx base = cfun->machine->base_reg;
7861           rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, memref, base),
7862                                      UNSPEC_LTREF);
7863
7864           *x = replace_equiv_address (*x, addr);
7865           return;
7866         }
7867
7868       if (GET_CODE (memref) == CONST
7869           && GET_CODE (XEXP (memref, 0)) == PLUS
7870           && GET_CODE (XEXP (XEXP (memref, 0), 1)) == CONST_INT
7871           && GET_CODE (XEXP (XEXP (memref, 0), 0)) == SYMBOL_REF
7872           && CONSTANT_POOL_ADDRESS_P (XEXP (XEXP (memref, 0), 0)))
7873         {
7874           HOST_WIDE_INT off = INTVAL (XEXP (XEXP (memref, 0), 1));
7875           rtx sym = XEXP (XEXP (memref, 0), 0);
7876           rtx base = cfun->machine->base_reg;
7877           rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, sym, base),
7878                                      UNSPEC_LTREF);
7879
7880           *x = replace_equiv_address (*x, plus_constant (Pmode, addr, off));
7881           return;
7882         }
7883     }
7884
7885   /* ... or a load-address type pattern.  */
7886   if (GET_CODE (*x) == SET)
7887     {
7888       rtx addrref = SET_SRC (*x);
7889
7890       if (GET_CODE (addrref) == SYMBOL_REF
7891           && CONSTANT_POOL_ADDRESS_P (addrref))
7892         {
7893           rtx base = cfun->machine->base_reg;
7894           rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, addrref, base),
7895                                      UNSPEC_LTREF);
7896
7897           SET_SRC (*x) = addr;
7898           return;
7899         }
7900
7901       if (GET_CODE (addrref) == CONST
7902           && GET_CODE (XEXP (addrref, 0)) == PLUS
7903           && GET_CODE (XEXP (XEXP (addrref, 0), 1)) == CONST_INT
7904           && GET_CODE (XEXP (XEXP (addrref, 0), 0)) == SYMBOL_REF
7905           && CONSTANT_POOL_ADDRESS_P (XEXP (XEXP (addrref, 0), 0)))
7906         {
7907           HOST_WIDE_INT off = INTVAL (XEXP (XEXP (addrref, 0), 1));
7908           rtx sym = XEXP (XEXP (addrref, 0), 0);
7909           rtx base = cfun->machine->base_reg;
7910           rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, sym, base),
7911                                      UNSPEC_LTREF);
7912
7913           SET_SRC (*x) = plus_constant (Pmode, addr, off);
7914           return;
7915         }
7916     }
7917
7918   /* Annotate LTREL_BASE as well.  */
7919   if (GET_CODE (*x) == UNSPEC
7920       && XINT (*x, 1) == UNSPEC_LTREL_BASE)
7921     {
7922       rtx base = cfun->machine->base_reg;
7923       *x = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, XVECEXP (*x, 0, 0), base),
7924                                   UNSPEC_LTREL_BASE);
7925       return;
7926     }
7927
7928   fmt = GET_RTX_FORMAT (GET_CODE (*x));
7929   for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
7930     {
7931       if (fmt[i] == 'e')
7932         {
7933           annotate_constant_pool_refs (&XEXP (*x, i));
7934         }
7935       else if (fmt[i] == 'E')
7936         {
7937           for (j = 0; j < XVECLEN (*x, i); j++)
7938             annotate_constant_pool_refs (&XVECEXP (*x, i, j));
7939         }
7940     }
7941 }
7942
7943 /* Split all branches that exceed the maximum distance.
7944    Returns true if this created a new literal pool entry.  */
7945
7946 static int
7947 s390_split_branches (void)
7948 {
7949   rtx temp_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
7950   int new_literal = 0, ret;
7951   rtx_insn *insn;
7952   rtx pat, target;
7953   rtx *label;
7954
7955   /* We need correct insn addresses.  */
7956
7957   shorten_branches (get_insns ());
7958
7959   /* Find all branches that exceed 64KB, and split them.  */
7960
7961   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
7962     {
7963       if (! JUMP_P (insn) || tablejump_p (insn, NULL, NULL))
7964         continue;
7965
7966       pat = PATTERN (insn);
7967       if (GET_CODE (pat) == PARALLEL)
7968         pat = XVECEXP (pat, 0, 0);
7969       if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
7970         continue;
7971
7972       if (GET_CODE (SET_SRC (pat)) == LABEL_REF)
7973         {
7974           label = &SET_SRC (pat);
7975         }
7976       else if (GET_CODE (SET_SRC (pat)) == IF_THEN_ELSE)
7977         {
7978           if (GET_CODE (XEXP (SET_SRC (pat), 1)) == LABEL_REF)
7979             label = &XEXP (SET_SRC (pat), 1);
7980           else if (GET_CODE (XEXP (SET_SRC (pat), 2)) == LABEL_REF)
7981             label = &XEXP (SET_SRC (pat), 2);
7982           else
7983             continue;
7984         }
7985       else
7986         continue;
7987
7988       if (get_attr_length (insn) <= 4)
7989         continue;
7990
7991       /* We are going to use the return register as scratch register,
7992          make sure it will be saved/restored by the prologue/epilogue.  */
7993       cfun_frame_layout.save_return_addr_p = 1;
7994
7995       if (!flag_pic)
7996         {
7997           new_literal = 1;
7998           rtx mem = force_const_mem (Pmode, *label);
7999           rtx_insn *set_insn = emit_insn_before (gen_rtx_SET (temp_reg, mem),
8000                                                  insn);
8001           INSN_ADDRESSES_NEW (set_insn, -1);
8002           annotate_constant_pool_refs (&PATTERN (set_insn));
8003
8004           target = temp_reg;
8005         }
8006       else
8007         {
8008           new_literal = 1;
8009           target = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, *label),
8010                                    UNSPEC_LTREL_OFFSET);
8011           target = gen_rtx_CONST (Pmode, target);
8012           target = force_const_mem (Pmode, target);
8013           rtx_insn *set_insn = emit_insn_before (gen_rtx_SET (temp_reg, target),
8014                                                  insn);
8015           INSN_ADDRESSES_NEW (set_insn, -1);
8016           annotate_constant_pool_refs (&PATTERN (set_insn));
8017
8018           target = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, XEXP (target, 0),
8019                                                         cfun->machine->base_reg),
8020                                    UNSPEC_LTREL_BASE);
8021           target = gen_rtx_PLUS (Pmode, temp_reg, target);
8022         }
8023
8024       ret = validate_change (insn, label, target, 0);
8025       gcc_assert (ret);
8026     }
8027
8028   return new_literal;
8029 }
8030
8031
8032 /* Find an annotated literal pool symbol referenced in RTX X,
8033    and store it at REF.  Will abort if X contains references to
8034    more than one such pool symbol; multiple references to the same
8035    symbol are allowed, however.
8036
8037    The rtx pointed to by REF must be initialized to NULL_RTX
8038    by the caller before calling this routine.  */
8039
8040 static void
8041 find_constant_pool_ref (rtx x, rtx *ref)
8042 {
8043   int i, j;
8044   const char *fmt;
8045
8046   /* Ignore LTREL_BASE references.  */
8047   if (GET_CODE (x) == UNSPEC
8048       && XINT (x, 1) == UNSPEC_LTREL_BASE)
8049     return;
8050   /* Likewise POOL_ENTRY insns.  */
8051   if (GET_CODE (x) == UNSPEC_VOLATILE
8052       && XINT (x, 1) == UNSPECV_POOL_ENTRY)
8053     return;
8054
8055   gcc_assert (GET_CODE (x) != SYMBOL_REF
8056               || !CONSTANT_POOL_ADDRESS_P (x));
8057
8058   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_LTREF)
8059     {
8060       rtx sym = XVECEXP (x, 0, 0);
8061       gcc_assert (GET_CODE (sym) == SYMBOL_REF
8062                   && CONSTANT_POOL_ADDRESS_P (sym));
8063
8064       if (*ref == NULL_RTX)
8065         *ref = sym;
8066       else
8067         gcc_assert (*ref == sym);
8068
8069       return;
8070     }
8071
8072   fmt = GET_RTX_FORMAT (GET_CODE (x));
8073   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
8074     {
8075       if (fmt[i] == 'e')
8076         {
8077           find_constant_pool_ref (XEXP (x, i), ref);
8078         }
8079       else if (fmt[i] == 'E')
8080         {
8081           for (j = 0; j < XVECLEN (x, i); j++)
8082             find_constant_pool_ref (XVECEXP (x, i, j), ref);
8083         }
8084     }
8085 }
8086
8087 /* Replace every reference to the annotated literal pool
8088    symbol REF in X by its base plus OFFSET.  */
8089
8090 static void
8091 replace_constant_pool_ref (rtx *x, rtx ref, rtx offset)
8092 {
8093   int i, j;
8094   const char *fmt;
8095
8096   gcc_assert (*x != ref);
8097
8098   if (GET_CODE (*x) == UNSPEC
8099       && XINT (*x, 1) == UNSPEC_LTREF
8100       && XVECEXP (*x, 0, 0) == ref)
8101     {
8102       *x = gen_rtx_PLUS (Pmode, XVECEXP (*x, 0, 1), offset);
8103       return;
8104     }
8105
8106   if (GET_CODE (*x) == PLUS
8107       && GET_CODE (XEXP (*x, 1)) == CONST_INT
8108       && GET_CODE (XEXP (*x, 0)) == UNSPEC
8109       && XINT (XEXP (*x, 0), 1) == UNSPEC_LTREF
8110       && XVECEXP (XEXP (*x, 0), 0, 0) == ref)
8111     {
8112       rtx addr = gen_rtx_PLUS (Pmode, XVECEXP (XEXP (*x, 0), 0, 1), offset);
8113       *x = plus_constant (Pmode, addr, INTVAL (XEXP (*x, 1)));
8114       return;
8115     }
8116
8117   fmt = GET_RTX_FORMAT (GET_CODE (*x));
8118   for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
8119     {
8120       if (fmt[i] == 'e')
8121         {
8122           replace_constant_pool_ref (&XEXP (*x, i), ref, offset);
8123         }
8124       else if (fmt[i] == 'E')
8125         {
8126           for (j = 0; j < XVECLEN (*x, i); j++)
8127             replace_constant_pool_ref (&XVECEXP (*x, i, j), ref, offset);
8128         }
8129     }
8130 }
8131
8132 /* Check whether X contains an UNSPEC_LTREL_BASE.
8133    Return its constant pool symbol if found, NULL_RTX otherwise.  */
8134
8135 static rtx
8136 find_ltrel_base (rtx x)
8137 {
8138   int i, j;
8139   const char *fmt;
8140
8141   if (GET_CODE (x) == UNSPEC
8142       && XINT (x, 1) == UNSPEC_LTREL_BASE)
8143     return XVECEXP (x, 0, 0);
8144
8145   fmt = GET_RTX_FORMAT (GET_CODE (x));
8146   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
8147     {
8148       if (fmt[i] == 'e')
8149         {
8150           rtx fnd = find_ltrel_base (XEXP (x, i));
8151           if (fnd)
8152             return fnd;
8153         }
8154       else if (fmt[i] == 'E')
8155         {
8156           for (j = 0; j < XVECLEN (x, i); j++)
8157             {
8158               rtx fnd = find_ltrel_base (XVECEXP (x, i, j));
8159               if (fnd)
8160                 return fnd;
8161             }
8162         }
8163     }
8164
8165   return NULL_RTX;
8166 }
8167
8168 /* Replace any occurrence of UNSPEC_LTREL_BASE in X with its base.  */
8169
8170 static void
8171 replace_ltrel_base (rtx *x)
8172 {
8173   int i, j;
8174   const char *fmt;
8175
8176   if (GET_CODE (*x) == UNSPEC
8177       && XINT (*x, 1) == UNSPEC_LTREL_BASE)
8178     {
8179       *x = XVECEXP (*x, 0, 1);
8180       return;
8181     }
8182
8183   fmt = GET_RTX_FORMAT (GET_CODE (*x));
8184   for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
8185     {
8186       if (fmt[i] == 'e')
8187         {
8188           replace_ltrel_base (&XEXP (*x, i));
8189         }
8190       else if (fmt[i] == 'E')
8191         {
8192           for (j = 0; j < XVECLEN (*x, i); j++)
8193             replace_ltrel_base (&XVECEXP (*x, i, j));
8194         }
8195     }
8196 }
8197
8198
8199 /* We keep a list of constants which we have to add to internal
8200    constant tables in the middle of large functions.  */
8201
8202 #define NR_C_MODES 32
8203 machine_mode constant_modes[NR_C_MODES] =
8204 {
8205   TFmode, TImode, TDmode,
8206   V16QImode, V8HImode, V4SImode, V2DImode, V1TImode,
8207   V4SFmode, V2DFmode, V1TFmode,
8208   DFmode, DImode, DDmode,
8209   V8QImode, V4HImode, V2SImode, V1DImode, V2SFmode, V1DFmode,
8210   SFmode, SImode, SDmode,
8211   V4QImode, V2HImode, V1SImode,  V1SFmode,
8212   HImode,
8213   V2QImode, V1HImode,
8214   QImode,
8215   V1QImode
8216 };
8217
8218 struct constant
8219 {
8220   struct constant *next;
8221   rtx value;
8222   rtx_code_label *label;
8223 };
8224
8225 struct constant_pool
8226 {
8227   struct constant_pool *next;
8228   rtx_insn *first_insn;
8229   rtx_insn *pool_insn;
8230   bitmap insns;
8231   rtx_insn *emit_pool_after;
8232
8233   struct constant *constants[NR_C_MODES];
8234   struct constant *execute;
8235   rtx_code_label *label;
8236   int size;
8237 };
8238
8239 /* Allocate new constant_pool structure.  */
8240
8241 static struct constant_pool *
8242 s390_alloc_pool (void)
8243 {
8244   struct constant_pool *pool;
8245   int i;
8246
8247   pool = (struct constant_pool *) xmalloc (sizeof *pool);
8248   pool->next = NULL;
8249   for (i = 0; i < NR_C_MODES; i++)
8250     pool->constants[i] = NULL;
8251
8252   pool->execute = NULL;
8253   pool->label = gen_label_rtx ();
8254   pool->first_insn = NULL;
8255   pool->pool_insn = NULL;
8256   pool->insns = BITMAP_ALLOC (NULL);
8257   pool->size = 0;
8258   pool->emit_pool_after = NULL;
8259
8260   return pool;
8261 }
8262
8263 /* Create new constant pool covering instructions starting at INSN
8264    and chain it to the end of POOL_LIST.  */
8265
8266 static struct constant_pool *
8267 s390_start_pool (struct constant_pool **pool_list, rtx_insn *insn)
8268 {
8269   struct constant_pool *pool, **prev;
8270
8271   pool = s390_alloc_pool ();
8272   pool->first_insn = insn;
8273
8274   for (prev = pool_list; *prev; prev = &(*prev)->next)
8275     ;
8276   *prev = pool;
8277
8278   return pool;
8279 }
8280
8281 /* End range of instructions covered by POOL at INSN and emit
8282    placeholder insn representing the pool.  */
8283
8284 static void
8285 s390_end_pool (struct constant_pool *pool, rtx_insn *insn)
8286 {
8287   rtx pool_size = GEN_INT (pool->size + 8 /* alignment slop */);
8288
8289   if (!insn)
8290     insn = get_last_insn ();
8291
8292   pool->pool_insn = emit_insn_after (gen_pool (pool_size), insn);
8293   INSN_ADDRESSES_NEW (pool->pool_insn, -1);
8294 }
8295
8296 /* Add INSN to the list of insns covered by POOL.  */
8297
8298 static void
8299 s390_add_pool_insn (struct constant_pool *pool, rtx insn)
8300 {
8301   bitmap_set_bit (pool->insns, INSN_UID (insn));
8302 }
8303
8304 /* Return pool out of POOL_LIST that covers INSN.  */
8305
8306 static struct constant_pool *
8307 s390_find_pool (struct constant_pool *pool_list, rtx insn)
8308 {
8309   struct constant_pool *pool;
8310
8311   for (pool = pool_list; pool; pool = pool->next)
8312     if (bitmap_bit_p (pool->insns, INSN_UID (insn)))
8313       break;
8314
8315   return pool;
8316 }
8317
8318 /* Add constant VAL of mode MODE to the constant pool POOL.  */
8319
8320 static void
8321 s390_add_constant (struct constant_pool *pool, rtx val, machine_mode mode)
8322 {
8323   struct constant *c;
8324   int i;
8325
8326   for (i = 0; i < NR_C_MODES; i++)
8327     if (constant_modes[i] == mode)
8328       break;
8329   gcc_assert (i != NR_C_MODES);
8330
8331   for (c = pool->constants[i]; c != NULL; c = c->next)
8332     if (rtx_equal_p (val, c->value))
8333       break;
8334
8335   if (c == NULL)
8336     {
8337       c = (struct constant *) xmalloc (sizeof *c);
8338       c->value = val;
8339       c->label = gen_label_rtx ();
8340       c->next = pool->constants[i];
8341       pool->constants[i] = c;
8342       pool->size += GET_MODE_SIZE (mode);
8343     }
8344 }
8345
8346 /* Return an rtx that represents the offset of X from the start of
8347    pool POOL.  */
8348
8349 static rtx
8350 s390_pool_offset (struct constant_pool *pool, rtx x)
8351 {
8352   rtx label;
8353
8354   label = gen_rtx_LABEL_REF (GET_MODE (x), pool->label);
8355   x = gen_rtx_UNSPEC (GET_MODE (x), gen_rtvec (2, x, label),
8356                       UNSPEC_POOL_OFFSET);
8357   return gen_rtx_CONST (GET_MODE (x), x);
8358 }
8359
8360 /* Find constant VAL of mode MODE in the constant pool POOL.
8361    Return an RTX describing the distance from the start of
8362    the pool to the location of the new constant.  */
8363
8364 static rtx
8365 s390_find_constant (struct constant_pool *pool, rtx val,
8366                     machine_mode mode)
8367 {
8368   struct constant *c;
8369   int i;
8370
8371   for (i = 0; i < NR_C_MODES; i++)
8372     if (constant_modes[i] == mode)
8373       break;
8374   gcc_assert (i != NR_C_MODES);
8375
8376   for (c = pool->constants[i]; c != NULL; c = c->next)
8377     if (rtx_equal_p (val, c->value))
8378       break;
8379
8380   gcc_assert (c);
8381
8382   return s390_pool_offset (pool, gen_rtx_LABEL_REF (Pmode, c->label));
8383 }
8384
8385 /* Check whether INSN is an execute.  Return the label_ref to its
8386    execute target template if so, NULL_RTX otherwise.  */
8387
8388 static rtx
8389 s390_execute_label (rtx insn)
8390 {
8391   if (NONJUMP_INSN_P (insn)
8392       && GET_CODE (PATTERN (insn)) == PARALLEL
8393       && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == UNSPEC
8394       && XINT (XVECEXP (PATTERN (insn), 0, 0), 1) == UNSPEC_EXECUTE)
8395     return XVECEXP (XVECEXP (PATTERN (insn), 0, 0), 0, 2);
8396
8397   return NULL_RTX;
8398 }
8399
8400 /* Add execute target for INSN to the constant pool POOL.  */
8401
8402 static void
8403 s390_add_execute (struct constant_pool *pool, rtx insn)
8404 {
8405   struct constant *c;
8406
8407   for (c = pool->execute; c != NULL; c = c->next)
8408     if (INSN_UID (insn) == INSN_UID (c->value))
8409       break;
8410
8411   if (c == NULL)
8412     {
8413       c = (struct constant *) xmalloc (sizeof *c);
8414       c->value = insn;
8415       c->label = gen_label_rtx ();
8416       c->next = pool->execute;
8417       pool->execute = c;
8418       pool->size += 6;
8419     }
8420 }
8421
8422 /* Find execute target for INSN in the constant pool POOL.
8423    Return an RTX describing the distance from the start of
8424    the pool to the location of the execute target.  */
8425
8426 static rtx
8427 s390_find_execute (struct constant_pool *pool, rtx insn)
8428 {
8429   struct constant *c;
8430
8431   for (c = pool->execute; c != NULL; c = c->next)
8432     if (INSN_UID (insn) == INSN_UID (c->value))
8433       break;
8434
8435   gcc_assert (c);
8436
8437   return s390_pool_offset (pool, gen_rtx_LABEL_REF (Pmode, c->label));
8438 }
8439
8440 /* For an execute INSN, extract the execute target template.  */
8441
8442 static rtx
8443 s390_execute_target (rtx insn)
8444 {
8445   rtx pattern = PATTERN (insn);
8446   gcc_assert (s390_execute_label (insn));
8447
8448   if (XVECLEN (pattern, 0) == 2)
8449     {
8450       pattern = copy_rtx (XVECEXP (pattern, 0, 1));
8451     }
8452   else
8453     {
8454       rtvec vec = rtvec_alloc (XVECLEN (pattern, 0) - 1);
8455       int i;
8456
8457       for (i = 0; i < XVECLEN (pattern, 0) - 1; i++)
8458         RTVEC_ELT (vec, i) = copy_rtx (XVECEXP (pattern, 0, i + 1));
8459
8460       pattern = gen_rtx_PARALLEL (VOIDmode, vec);
8461     }
8462
8463   return pattern;
8464 }
8465
8466 /* Indicate that INSN cannot be duplicated.  This is the case for
8467    execute insns that carry a unique label.  */
8468
8469 static bool
8470 s390_cannot_copy_insn_p (rtx_insn *insn)
8471 {
8472   rtx label = s390_execute_label (insn);
8473   return label && label != const0_rtx;
8474 }
8475
8476 /* Dump out the constants in POOL.  If REMOTE_LABEL is true,
8477    do not emit the pool base label.  */
8478
8479 static void
8480 s390_dump_pool (struct constant_pool *pool, bool remote_label)
8481 {
8482   struct constant *c;
8483   rtx_insn *insn = pool->pool_insn;
8484   int i;
8485
8486   /* Switch to rodata section.  */
8487   if (TARGET_CPU_ZARCH)
8488     {
8489       insn = emit_insn_after (gen_pool_section_start (), insn);
8490       INSN_ADDRESSES_NEW (insn, -1);
8491     }
8492
8493   /* Ensure minimum pool alignment.  */
8494   if (TARGET_CPU_ZARCH)
8495     insn = emit_insn_after (gen_pool_align (GEN_INT (8)), insn);
8496   else
8497     insn = emit_insn_after (gen_pool_align (GEN_INT (4)), insn);
8498   INSN_ADDRESSES_NEW (insn, -1);
8499
8500   /* Emit pool base label.  */
8501   if (!remote_label)
8502     {
8503       insn = emit_label_after (pool->label, insn);
8504       INSN_ADDRESSES_NEW (insn, -1);
8505     }
8506
8507   /* Dump constants in descending alignment requirement order,
8508      ensuring proper alignment for every constant.  */
8509   for (i = 0; i < NR_C_MODES; i++)
8510     for (c = pool->constants[i]; c; c = c->next)
8511       {
8512         /* Convert UNSPEC_LTREL_OFFSET unspecs to pool-relative references.  */
8513         rtx value = copy_rtx (c->value);
8514         if (GET_CODE (value) == CONST
8515             && GET_CODE (XEXP (value, 0)) == UNSPEC
8516             && XINT (XEXP (value, 0), 1) == UNSPEC_LTREL_OFFSET
8517             && XVECLEN (XEXP (value, 0), 0) == 1)
8518           value = s390_pool_offset (pool, XVECEXP (XEXP (value, 0), 0, 0));
8519
8520         insn = emit_label_after (c->label, insn);
8521         INSN_ADDRESSES_NEW (insn, -1);
8522
8523         value = gen_rtx_UNSPEC_VOLATILE (constant_modes[i],
8524                                          gen_rtvec (1, value),
8525                                          UNSPECV_POOL_ENTRY);
8526         insn = emit_insn_after (value, insn);
8527         INSN_ADDRESSES_NEW (insn, -1);
8528       }
8529
8530   /* Ensure minimum alignment for instructions.  */
8531   insn = emit_insn_after (gen_pool_align (GEN_INT (2)), insn);
8532   INSN_ADDRESSES_NEW (insn, -1);
8533
8534   /* Output in-pool execute template insns.  */
8535   for (c = pool->execute; c; c = c->next)
8536     {
8537       insn = emit_label_after (c->label, insn);
8538       INSN_ADDRESSES_NEW (insn, -1);
8539
8540       insn = emit_insn_after (s390_execute_target (c->value), insn);
8541       INSN_ADDRESSES_NEW (insn, -1);
8542     }
8543
8544   /* Switch back to previous section.  */
8545   if (TARGET_CPU_ZARCH)
8546     {
8547       insn = emit_insn_after (gen_pool_section_end (), insn);
8548       INSN_ADDRESSES_NEW (insn, -1);
8549     }
8550
8551   insn = emit_barrier_after (insn);
8552   INSN_ADDRESSES_NEW (insn, -1);
8553
8554   /* Remove placeholder insn.  */
8555   remove_insn (pool->pool_insn);
8556 }
8557
8558 /* Free all memory used by POOL.  */
8559
8560 static void
8561 s390_free_pool (struct constant_pool *pool)
8562 {
8563   struct constant *c, *next;
8564   int i;
8565
8566   for (i = 0; i < NR_C_MODES; i++)
8567     for (c = pool->constants[i]; c; c = next)
8568       {
8569         next = c->next;
8570         free (c);
8571       }
8572
8573   for (c = pool->execute; c; c = next)
8574     {
8575       next = c->next;
8576       free (c);
8577     }
8578
8579   BITMAP_FREE (pool->insns);
8580   free (pool);
8581 }
8582
8583
8584 /* Collect main literal pool.  Return NULL on overflow.  */
8585
8586 static struct constant_pool *
8587 s390_mainpool_start (void)
8588 {
8589   struct constant_pool *pool;
8590   rtx_insn *insn;
8591
8592   pool = s390_alloc_pool ();
8593
8594   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
8595     {
8596       if (NONJUMP_INSN_P (insn)
8597           && GET_CODE (PATTERN (insn)) == SET
8598           && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC_VOLATILE
8599           && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPECV_MAIN_POOL)
8600         {
8601           /* There might be two main_pool instructions if base_reg
8602              is call-clobbered; one for shrink-wrapped code and one
8603              for the rest.  We want to keep the first.  */
8604           if (pool->pool_insn)
8605             {
8606               insn = PREV_INSN (insn);
8607               delete_insn (NEXT_INSN (insn));
8608               continue;
8609             }
8610           pool->pool_insn = insn;
8611         }
8612
8613       if (!TARGET_CPU_ZARCH && s390_execute_label (insn))
8614         {
8615           s390_add_execute (pool, insn);
8616         }
8617       else if (NONJUMP_INSN_P (insn) || CALL_P (insn))
8618         {
8619           rtx pool_ref = NULL_RTX;
8620           find_constant_pool_ref (PATTERN (insn), &pool_ref);
8621           if (pool_ref)
8622             {
8623               rtx constant = get_pool_constant (pool_ref);
8624               machine_mode mode = get_pool_mode (pool_ref);
8625               s390_add_constant (pool, constant, mode);
8626             }
8627         }
8628
8629       /* If hot/cold partitioning is enabled we have to make sure that
8630          the literal pool is emitted in the same section where the
8631          initialization of the literal pool base pointer takes place.
8632          emit_pool_after is only used in the non-overflow case on non
8633          Z cpus where we can emit the literal pool at the end of the
8634          function body within the text section.  */
8635       if (NOTE_P (insn)
8636           && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS
8637           && !pool->emit_pool_after)
8638         pool->emit_pool_after = PREV_INSN (insn);
8639     }
8640
8641   gcc_assert (pool->pool_insn || pool->size == 0);
8642
8643   if (pool->size >= 4096)
8644     {
8645       /* We're going to chunkify the pool, so remove the main
8646          pool placeholder insn.  */
8647       remove_insn (pool->pool_insn);
8648
8649       s390_free_pool (pool);
8650       pool = NULL;
8651     }
8652
8653   /* If the functions ends with the section where the literal pool
8654      should be emitted set the marker to its end.  */
8655   if (pool && !pool->emit_pool_after)
8656     pool->emit_pool_after = get_last_insn ();
8657
8658   return pool;
8659 }
8660
8661 /* POOL holds the main literal pool as collected by s390_mainpool_start.
8662    Modify the current function to output the pool constants as well as
8663    the pool register setup instruction.  */
8664
8665 static void
8666 s390_mainpool_finish (struct constant_pool *pool)
8667 {
8668   rtx base_reg = cfun->machine->base_reg;
8669
8670   /* If the pool is empty, we're done.  */
8671   if (pool->size == 0)
8672     {
8673       /* We don't actually need a base register after all.  */
8674       cfun->machine->base_reg = NULL_RTX;
8675
8676       if (pool->pool_insn)
8677         remove_insn (pool->pool_insn);
8678       s390_free_pool (pool);
8679       return;
8680     }
8681
8682   /* We need correct insn addresses.  */
8683   shorten_branches (get_insns ());
8684
8685   /* On zSeries, we use a LARL to load the pool register.  The pool is
8686      located in the .rodata section, so we emit it after the function.  */
8687   if (TARGET_CPU_ZARCH)
8688     {
8689       rtx set = gen_main_base_64 (base_reg, pool->label);
8690       rtx_insn *insn = emit_insn_after (set, pool->pool_insn);
8691       INSN_ADDRESSES_NEW (insn, -1);
8692       remove_insn (pool->pool_insn);
8693
8694       insn = get_last_insn ();
8695       pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
8696       INSN_ADDRESSES_NEW (pool->pool_insn, -1);
8697
8698       s390_dump_pool (pool, 0);
8699     }
8700
8701   /* On S/390, if the total size of the function's code plus literal pool
8702      does not exceed 4096 bytes, we use BASR to set up a function base
8703      pointer, and emit the literal pool at the end of the function.  */
8704   else if (INSN_ADDRESSES (INSN_UID (pool->emit_pool_after))
8705            + pool->size + 8 /* alignment slop */ < 4096)
8706     {
8707       rtx set = gen_main_base_31_small (base_reg, pool->label);
8708       rtx_insn *insn = emit_insn_after (set, pool->pool_insn);
8709       INSN_ADDRESSES_NEW (insn, -1);
8710       remove_insn (pool->pool_insn);
8711
8712       insn = emit_label_after (pool->label, insn);
8713       INSN_ADDRESSES_NEW (insn, -1);
8714
8715       /* emit_pool_after will be set by s390_mainpool_start to the
8716          last insn of the section where the literal pool should be
8717          emitted.  */
8718       insn = pool->emit_pool_after;
8719
8720       pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
8721       INSN_ADDRESSES_NEW (pool->pool_insn, -1);
8722
8723       s390_dump_pool (pool, 1);
8724     }
8725
8726   /* Otherwise, we emit an inline literal pool and use BASR to branch
8727      over it, setting up the pool register at the same time.  */
8728   else
8729     {
8730       rtx_code_label *pool_end = gen_label_rtx ();
8731
8732       rtx pat = gen_main_base_31_large (base_reg, pool->label, pool_end);
8733       rtx_insn *insn = emit_jump_insn_after (pat, pool->pool_insn);
8734       JUMP_LABEL (insn) = pool_end;
8735       INSN_ADDRESSES_NEW (insn, -1);
8736       remove_insn (pool->pool_insn);
8737
8738       insn = emit_label_after (pool->label, insn);
8739       INSN_ADDRESSES_NEW (insn, -1);
8740
8741       pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
8742       INSN_ADDRESSES_NEW (pool->pool_insn, -1);
8743
8744       insn = emit_label_after (pool_end, pool->pool_insn);
8745       INSN_ADDRESSES_NEW (insn, -1);
8746
8747       s390_dump_pool (pool, 1);
8748     }
8749
8750
8751   /* Replace all literal pool references.  */
8752
8753   for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
8754     {
8755       if (INSN_P (insn))
8756         replace_ltrel_base (&PATTERN (insn));
8757
8758       if (NONJUMP_INSN_P (insn) || CALL_P (insn))
8759         {
8760           rtx addr, pool_ref = NULL_RTX;
8761           find_constant_pool_ref (PATTERN (insn), &pool_ref);
8762           if (pool_ref)
8763             {
8764               if (s390_execute_label (insn))
8765                 addr = s390_find_execute (pool, insn);
8766               else
8767                 addr = s390_find_constant (pool, get_pool_constant (pool_ref),
8768                                                  get_pool_mode (pool_ref));
8769
8770               replace_constant_pool_ref (&PATTERN (insn), pool_ref, addr);
8771               INSN_CODE (insn) = -1;
8772             }
8773         }
8774     }
8775
8776
8777   /* Free the pool.  */
8778   s390_free_pool (pool);
8779 }
8780
8781 /* POOL holds the main literal pool as collected by s390_mainpool_start.
8782    We have decided we cannot use this pool, so revert all changes
8783    to the current function that were done by s390_mainpool_start.  */
8784 static void
8785 s390_mainpool_cancel (struct constant_pool *pool)
8786 {
8787   /* We didn't actually change the instruction stream, so simply
8788      free the pool memory.  */
8789   s390_free_pool (pool);
8790 }
8791
8792
8793 /* Chunkify the literal pool.  */
8794
8795 #define S390_POOL_CHUNK_MIN     0xc00
8796 #define S390_POOL_CHUNK_MAX     0xe00
8797
8798 static struct constant_pool *
8799 s390_chunkify_start (void)
8800 {
8801   struct constant_pool *curr_pool = NULL, *pool_list = NULL;
8802   int extra_size = 0;
8803   bitmap far_labels;
8804   rtx pending_ltrel = NULL_RTX;
8805   rtx_insn *insn;
8806
8807   rtx (*gen_reload_base) (rtx, rtx) =
8808     TARGET_CPU_ZARCH? gen_reload_base_64 : gen_reload_base_31;
8809
8810
8811   /* We need correct insn addresses.  */
8812
8813   shorten_branches (get_insns ());
8814
8815   /* Scan all insns and move literals to pool chunks.  */
8816
8817   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
8818     {
8819       bool section_switch_p = false;
8820
8821       /* Check for pending LTREL_BASE.  */
8822       if (INSN_P (insn))
8823         {
8824           rtx ltrel_base = find_ltrel_base (PATTERN (insn));
8825           if (ltrel_base)
8826             {
8827               gcc_assert (ltrel_base == pending_ltrel);
8828               pending_ltrel = NULL_RTX;
8829             }
8830         }
8831
8832       if (!TARGET_CPU_ZARCH && s390_execute_label (insn))
8833         {
8834           if (!curr_pool)
8835             curr_pool = s390_start_pool (&pool_list, insn);
8836
8837           s390_add_execute (curr_pool, insn);
8838           s390_add_pool_insn (curr_pool, insn);
8839         }
8840       else if (NONJUMP_INSN_P (insn) || CALL_P (insn))
8841         {
8842           rtx pool_ref = NULL_RTX;
8843           find_constant_pool_ref (PATTERN (insn), &pool_ref);
8844           if (pool_ref)
8845             {
8846               rtx constant = get_pool_constant (pool_ref);
8847               machine_mode mode = get_pool_mode (pool_ref);
8848
8849               if (!curr_pool)
8850                 curr_pool = s390_start_pool (&pool_list, insn);
8851
8852               s390_add_constant (curr_pool, constant, mode);
8853               s390_add_pool_insn (curr_pool, insn);
8854
8855               /* Don't split the pool chunk between a LTREL_OFFSET load
8856                  and the corresponding LTREL_BASE.  */
8857               if (GET_CODE (constant) == CONST
8858                   && GET_CODE (XEXP (constant, 0)) == UNSPEC
8859                   && XINT (XEXP (constant, 0), 1) == UNSPEC_LTREL_OFFSET)
8860                 {
8861                   gcc_assert (!pending_ltrel);
8862                   pending_ltrel = pool_ref;
8863                 }
8864             }
8865         }
8866
8867       if (JUMP_P (insn) || JUMP_TABLE_DATA_P (insn) || LABEL_P (insn))
8868         {
8869           if (curr_pool)
8870             s390_add_pool_insn (curr_pool, insn);
8871           /* An LTREL_BASE must follow within the same basic block.  */
8872           gcc_assert (!pending_ltrel);
8873         }
8874
8875       if (NOTE_P (insn))
8876         switch (NOTE_KIND (insn))
8877           {
8878           case NOTE_INSN_SWITCH_TEXT_SECTIONS:
8879             section_switch_p = true;
8880             break;
8881           case NOTE_INSN_VAR_LOCATION:
8882           case NOTE_INSN_CALL_ARG_LOCATION:
8883             continue;
8884           default:
8885             break;
8886           }
8887
8888       if (!curr_pool
8889           || INSN_ADDRESSES_SIZE () <= (size_t) INSN_UID (insn)
8890           || INSN_ADDRESSES (INSN_UID (insn)) == -1)
8891         continue;
8892
8893       if (TARGET_CPU_ZARCH)
8894         {
8895           if (curr_pool->size < S390_POOL_CHUNK_MAX)
8896             continue;
8897
8898           s390_end_pool (curr_pool, NULL);
8899           curr_pool = NULL;
8900         }
8901       else
8902         {
8903           int chunk_size = INSN_ADDRESSES (INSN_UID (insn))
8904                            - INSN_ADDRESSES (INSN_UID (curr_pool->first_insn))
8905                          + extra_size;
8906
8907           /* We will later have to insert base register reload insns.
8908              Those will have an effect on code size, which we need to
8909              consider here.  This calculation makes rather pessimistic
8910              worst-case assumptions.  */
8911           if (LABEL_P (insn))
8912             extra_size += 6;
8913
8914           if (chunk_size < S390_POOL_CHUNK_MIN
8915               && curr_pool->size < S390_POOL_CHUNK_MIN
8916               && !section_switch_p)
8917             continue;
8918
8919           /* Pool chunks can only be inserted after BARRIERs ...  */
8920           if (BARRIER_P (insn))
8921             {
8922               s390_end_pool (curr_pool, insn);
8923               curr_pool = NULL;
8924               extra_size = 0;
8925             }
8926
8927           /* ... so if we don't find one in time, create one.  */
8928           else if (chunk_size > S390_POOL_CHUNK_MAX
8929                    || curr_pool->size > S390_POOL_CHUNK_MAX
8930                    || section_switch_p)
8931             {
8932               rtx_insn *label, *jump, *barrier, *next, *prev;
8933
8934               if (!section_switch_p)
8935                 {
8936                   /* We can insert the barrier only after a 'real' insn.  */
8937                   if (! NONJUMP_INSN_P (insn) && ! CALL_P (insn))
8938                     continue;
8939                   if (get_attr_length (insn) == 0)
8940                     continue;
8941                   /* Don't separate LTREL_BASE from the corresponding
8942                      LTREL_OFFSET load.  */
8943                   if (pending_ltrel)
8944                     continue;
8945                   next = insn;
8946                   do
8947                     {
8948                       insn = next;
8949                       next = NEXT_INSN (insn);
8950                     }
8951                   while (next
8952                          && NOTE_P (next)
8953                          && (NOTE_KIND (next) == NOTE_INSN_VAR_LOCATION
8954                              || NOTE_KIND (next) == NOTE_INSN_CALL_ARG_LOCATION));
8955                 }
8956               else
8957                 {
8958                   gcc_assert (!pending_ltrel);
8959
8960                   /* The old pool has to end before the section switch
8961                      note in order to make it part of the current
8962                      section.  */
8963                   insn = PREV_INSN (insn);
8964                 }
8965
8966               label = gen_label_rtx ();
8967               prev = insn;
8968               if (prev && NOTE_P (prev))
8969                 prev = prev_nonnote_insn (prev);
8970               if (prev)
8971                 jump = emit_jump_insn_after_setloc (gen_jump (label), insn,
8972                                                     INSN_LOCATION (prev));
8973               else
8974                 jump = emit_jump_insn_after_noloc (gen_jump (label), insn);
8975               barrier = emit_barrier_after (jump);
8976               insn = emit_label_after (label, barrier);
8977               JUMP_LABEL (jump) = label;
8978               LABEL_NUSES (label) = 1;
8979
8980               INSN_ADDRESSES_NEW (jump, -1);
8981               INSN_ADDRESSES_NEW (barrier, -1);
8982               INSN_ADDRESSES_NEW (insn, -1);
8983
8984               s390_end_pool (curr_pool, barrier);
8985               curr_pool = NULL;
8986               extra_size = 0;
8987             }
8988         }
8989     }
8990
8991   if (curr_pool)
8992     s390_end_pool (curr_pool, NULL);
8993   gcc_assert (!pending_ltrel);
8994
8995   /* Find all labels that are branched into
8996      from an insn belonging to a different chunk.  */
8997
8998   far_labels = BITMAP_ALLOC (NULL);
8999
9000   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9001     {
9002       rtx_jump_table_data *table;
9003
9004       /* Labels marked with LABEL_PRESERVE_P can be target
9005          of non-local jumps, so we have to mark them.
9006          The same holds for named labels.
9007
9008          Don't do that, however, if it is the label before
9009          a jump table.  */
9010
9011       if (LABEL_P (insn)
9012           && (LABEL_PRESERVE_P (insn) || LABEL_NAME (insn)))
9013         {
9014           rtx_insn *vec_insn = NEXT_INSN (insn);
9015           if (! vec_insn || ! JUMP_TABLE_DATA_P (vec_insn))
9016             bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (insn));
9017         }
9018       /* Check potential targets in a table jump (casesi_jump).  */
9019       else if (tablejump_p (insn, NULL, &table))
9020         {
9021           rtx vec_pat = PATTERN (table);
9022           int i, diff_p = GET_CODE (vec_pat) == ADDR_DIFF_VEC;
9023
9024           for (i = 0; i < XVECLEN (vec_pat, diff_p); i++)
9025             {
9026               rtx label = XEXP (XVECEXP (vec_pat, diff_p, i), 0);
9027
9028               if (s390_find_pool (pool_list, label)
9029                   != s390_find_pool (pool_list, insn))
9030                 bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (label));
9031             }
9032         }
9033       /* If we have a direct jump (conditional or unconditional),
9034          check all potential targets.  */
9035       else if (JUMP_P (insn))
9036         {
9037           rtx pat = PATTERN (insn);
9038
9039           if (GET_CODE (pat) == PARALLEL)
9040             pat = XVECEXP (pat, 0, 0);
9041
9042           if (GET_CODE (pat) == SET)
9043             {
9044               rtx label = JUMP_LABEL (insn);
9045               if (label && !ANY_RETURN_P (label))
9046                 {
9047                   if (s390_find_pool (pool_list, label)
9048                       != s390_find_pool (pool_list, insn))
9049                     bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (label));
9050                 }
9051             }
9052         }
9053     }
9054
9055   /* Insert base register reload insns before every pool.  */
9056
9057   for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
9058     {
9059       rtx new_insn = gen_reload_base (cfun->machine->base_reg,
9060                                       curr_pool->label);
9061       rtx_insn *insn = curr_pool->first_insn;
9062       INSN_ADDRESSES_NEW (emit_insn_before (new_insn, insn), -1);
9063     }
9064
9065   /* Insert base register reload insns at every far label.  */
9066
9067   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9068     if (LABEL_P (insn)
9069         && bitmap_bit_p (far_labels, CODE_LABEL_NUMBER (insn)))
9070       {
9071         struct constant_pool *pool = s390_find_pool (pool_list, insn);
9072         if (pool)
9073           {
9074             rtx new_insn = gen_reload_base (cfun->machine->base_reg,
9075                                             pool->label);
9076             INSN_ADDRESSES_NEW (emit_insn_after (new_insn, insn), -1);
9077           }
9078       }
9079
9080
9081   BITMAP_FREE (far_labels);
9082
9083
9084   /* Recompute insn addresses.  */
9085
9086   init_insn_lengths ();
9087   shorten_branches (get_insns ());
9088
9089   return pool_list;
9090 }
9091
9092 /* POOL_LIST is a chunk list as prepared by s390_chunkify_start.
9093    After we have decided to use this list, finish implementing
9094    all changes to the current function as required.  */
9095
9096 static void
9097 s390_chunkify_finish (struct constant_pool *pool_list)
9098 {
9099   struct constant_pool *curr_pool = NULL;
9100   rtx_insn *insn;
9101
9102
9103   /* Replace all literal pool references.  */
9104
9105   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9106     {
9107       if (INSN_P (insn))
9108         replace_ltrel_base (&PATTERN (insn));
9109
9110       curr_pool = s390_find_pool (pool_list, insn);
9111       if (!curr_pool)
9112         continue;
9113
9114       if (NONJUMP_INSN_P (insn) || CALL_P (insn))
9115         {
9116           rtx addr, pool_ref = NULL_RTX;
9117           find_constant_pool_ref (PATTERN (insn), &pool_ref);
9118           if (pool_ref)
9119             {
9120               if (s390_execute_label (insn))
9121                 addr = s390_find_execute (curr_pool, insn);
9122               else
9123                 addr = s390_find_constant (curr_pool,
9124                                            get_pool_constant (pool_ref),
9125                                            get_pool_mode (pool_ref));
9126
9127               replace_constant_pool_ref (&PATTERN (insn), pool_ref, addr);
9128               INSN_CODE (insn) = -1;
9129             }
9130         }
9131     }
9132
9133   /* Dump out all literal pools.  */
9134
9135   for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
9136     s390_dump_pool (curr_pool, 0);
9137
9138   /* Free pool list.  */
9139
9140   while (pool_list)
9141     {
9142       struct constant_pool *next = pool_list->next;
9143       s390_free_pool (pool_list);
9144       pool_list = next;
9145     }
9146 }
9147
9148 /* POOL_LIST is a chunk list as prepared by s390_chunkify_start.
9149    We have decided we cannot use this list, so revert all changes
9150    to the current function that were done by s390_chunkify_start.  */
9151
9152 static void
9153 s390_chunkify_cancel (struct constant_pool *pool_list)
9154 {
9155   struct constant_pool *curr_pool = NULL;
9156   rtx_insn *insn;
9157
9158   /* Remove all pool placeholder insns.  */
9159
9160   for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
9161     {
9162       /* Did we insert an extra barrier?  Remove it.  */
9163       rtx_insn *barrier = PREV_INSN (curr_pool->pool_insn);
9164       rtx_insn *jump = barrier? PREV_INSN (barrier) : NULL;
9165       rtx_insn *label = NEXT_INSN (curr_pool->pool_insn);
9166
9167       if (jump && JUMP_P (jump)
9168           && barrier && BARRIER_P (barrier)
9169           && label && LABEL_P (label)
9170           && GET_CODE (PATTERN (jump)) == SET
9171           && SET_DEST (PATTERN (jump)) == pc_rtx
9172           && GET_CODE (SET_SRC (PATTERN (jump))) == LABEL_REF
9173           && XEXP (SET_SRC (PATTERN (jump)), 0) == label)
9174         {
9175           remove_insn (jump);
9176           remove_insn (barrier);
9177           remove_insn (label);
9178         }
9179
9180       remove_insn (curr_pool->pool_insn);
9181     }
9182
9183   /* Remove all base register reload insns.  */
9184
9185   for (insn = get_insns (); insn; )
9186     {
9187       rtx_insn *next_insn = NEXT_INSN (insn);
9188
9189       if (NONJUMP_INSN_P (insn)
9190           && GET_CODE (PATTERN (insn)) == SET
9191           && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC
9192           && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPEC_RELOAD_BASE)
9193         remove_insn (insn);
9194
9195       insn = next_insn;
9196     }
9197
9198   /* Free pool list.  */
9199
9200   while (pool_list)
9201     {
9202       struct constant_pool *next = pool_list->next;
9203       s390_free_pool (pool_list);
9204       pool_list = next;
9205     }
9206 }
9207
9208 /* Output the constant pool entry EXP in mode MODE with alignment ALIGN.  */
9209
9210 void
9211 s390_output_pool_entry (rtx exp, machine_mode mode, unsigned int align)
9212 {
9213   switch (GET_MODE_CLASS (mode))
9214     {
9215     case MODE_FLOAT:
9216     case MODE_DECIMAL_FLOAT:
9217       gcc_assert (GET_CODE (exp) == CONST_DOUBLE);
9218
9219       assemble_real (*CONST_DOUBLE_REAL_VALUE (exp), mode, align);
9220       break;
9221
9222     case MODE_INT:
9223       assemble_integer (exp, GET_MODE_SIZE (mode), align, 1);
9224       mark_symbol_refs_as_used (exp);
9225       break;
9226
9227     case MODE_VECTOR_INT:
9228     case MODE_VECTOR_FLOAT:
9229       {
9230         int i;
9231         machine_mode inner_mode;
9232         gcc_assert (GET_CODE (exp) == CONST_VECTOR);
9233
9234         inner_mode = GET_MODE_INNER (GET_MODE (exp));
9235         for (i = 0; i < XVECLEN (exp, 0); i++)
9236           s390_output_pool_entry (XVECEXP (exp, 0, i),
9237                                   inner_mode,
9238                                   i == 0
9239                                   ? align
9240                                   : GET_MODE_BITSIZE (inner_mode));
9241       }
9242       break;
9243
9244     default:
9245       gcc_unreachable ();
9246     }
9247 }
9248
9249
9250 /* Return an RTL expression representing the value of the return address
9251    for the frame COUNT steps up from the current frame.  FRAME is the
9252    frame pointer of that frame.  */
9253
9254 rtx
9255 s390_return_addr_rtx (int count, rtx frame ATTRIBUTE_UNUSED)
9256 {
9257   int offset;
9258   rtx addr;
9259
9260   /* Without backchain, we fail for all but the current frame.  */
9261
9262   if (!TARGET_BACKCHAIN && count > 0)
9263     return NULL_RTX;
9264
9265   /* For the current frame, we need to make sure the initial
9266      value of RETURN_REGNUM is actually saved.  */
9267
9268   if (count == 0)
9269     {
9270       /* On non-z architectures branch splitting could overwrite r14.  */
9271       if (TARGET_CPU_ZARCH)
9272         return get_hard_reg_initial_val (Pmode, RETURN_REGNUM);
9273       else
9274         {
9275           cfun_frame_layout.save_return_addr_p = true;
9276           return gen_rtx_MEM (Pmode, return_address_pointer_rtx);
9277         }
9278     }
9279
9280   if (TARGET_PACKED_STACK)
9281     offset = -2 * UNITS_PER_LONG;
9282   else
9283     offset = RETURN_REGNUM * UNITS_PER_LONG;
9284
9285   addr = plus_constant (Pmode, frame, offset);
9286   addr = memory_address (Pmode, addr);
9287   return gen_rtx_MEM (Pmode, addr);
9288 }
9289
9290 /* Return an RTL expression representing the back chain stored in
9291    the current stack frame.  */
9292
9293 rtx
9294 s390_back_chain_rtx (void)
9295 {
9296   rtx chain;
9297
9298   gcc_assert (TARGET_BACKCHAIN);
9299
9300   if (TARGET_PACKED_STACK)
9301     chain = plus_constant (Pmode, stack_pointer_rtx,
9302                            STACK_POINTER_OFFSET - UNITS_PER_LONG);
9303   else
9304     chain = stack_pointer_rtx;
9305
9306   chain = gen_rtx_MEM (Pmode, chain);
9307   return chain;
9308 }
9309
9310 /* Find first call clobbered register unused in a function.
9311    This could be used as base register in a leaf function
9312    or for holding the return address before epilogue.  */
9313
9314 static int
9315 find_unused_clobbered_reg (void)
9316 {
9317   int i;
9318   for (i = 0; i < 6; i++)
9319     if (!df_regs_ever_live_p (i))
9320       return i;
9321   return 0;
9322 }
9323
9324
9325 /* Helper function for s390_regs_ever_clobbered.  Sets the fields in DATA for all
9326    clobbered hard regs in SETREG.  */
9327
9328 static void
9329 s390_reg_clobbered_rtx (rtx setreg, const_rtx set_insn ATTRIBUTE_UNUSED, void *data)
9330 {
9331   char *regs_ever_clobbered = (char *)data;
9332   unsigned int i, regno;
9333   machine_mode mode = GET_MODE (setreg);
9334
9335   if (GET_CODE (setreg) == SUBREG)
9336     {
9337       rtx inner = SUBREG_REG (setreg);
9338       if (!GENERAL_REG_P (inner) && !FP_REG_P (inner))
9339         return;
9340       regno = subreg_regno (setreg);
9341     }
9342   else if (GENERAL_REG_P (setreg) || FP_REG_P (setreg))
9343     regno = REGNO (setreg);
9344   else
9345     return;
9346
9347   for (i = regno;
9348        i < regno + HARD_REGNO_NREGS (regno, mode);
9349        i++)
9350     regs_ever_clobbered[i] = 1;
9351 }
9352
9353 /* Walks through all basic blocks of the current function looking
9354    for clobbered hard regs using s390_reg_clobbered_rtx.  The fields
9355    of the passed integer array REGS_EVER_CLOBBERED are set to one for
9356    each of those regs.  */
9357
9358 static void
9359 s390_regs_ever_clobbered (char regs_ever_clobbered[])
9360 {
9361   basic_block cur_bb;
9362   rtx_insn *cur_insn;
9363   unsigned int i;
9364
9365   memset (regs_ever_clobbered, 0, 32);
9366
9367   /* For non-leaf functions we have to consider all call clobbered regs to be
9368      clobbered.  */
9369   if (!crtl->is_leaf)
9370     {
9371       for (i = 0; i < 32; i++)
9372         regs_ever_clobbered[i] = call_really_used_regs[i];
9373     }
9374
9375   /* Make the "magic" eh_return registers live if necessary.  For regs_ever_live
9376      this work is done by liveness analysis (mark_regs_live_at_end).
9377      Special care is needed for functions containing landing pads.  Landing pads
9378      may use the eh registers, but the code which sets these registers is not
9379      contained in that function.  Hence s390_regs_ever_clobbered is not able to
9380      deal with this automatically.  */
9381   if (crtl->calls_eh_return || cfun->machine->has_landing_pad_p)
9382     for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM ; i++)
9383       if (crtl->calls_eh_return
9384           || (cfun->machine->has_landing_pad_p
9385               && df_regs_ever_live_p (EH_RETURN_DATA_REGNO (i))))
9386         regs_ever_clobbered[EH_RETURN_DATA_REGNO (i)] = 1;
9387
9388   /* For nonlocal gotos all call-saved registers have to be saved.
9389      This flag is also set for the unwinding code in libgcc.
9390      See expand_builtin_unwind_init.  For regs_ever_live this is done by
9391      reload.  */
9392   if (crtl->saves_all_registers)
9393     for (i = 0; i < 32; i++)
9394       if (!call_really_used_regs[i])
9395         regs_ever_clobbered[i] = 1;
9396
9397   FOR_EACH_BB_FN (cur_bb, cfun)
9398     {
9399       FOR_BB_INSNS (cur_bb, cur_insn)
9400         {
9401           rtx pat;
9402
9403           if (!INSN_P (cur_insn))
9404             continue;
9405
9406           pat = PATTERN (cur_insn);
9407
9408           /* Ignore GPR restore insns.  */
9409           if (epilogue_completed && RTX_FRAME_RELATED_P (cur_insn))
9410             {
9411               if (GET_CODE (pat) == SET
9412                   && GENERAL_REG_P (SET_DEST (pat)))
9413                 {
9414                   /* lgdr  */
9415                   if (GET_MODE (SET_SRC (pat)) == DImode
9416                       && FP_REG_P (SET_SRC (pat)))
9417                     continue;
9418
9419                   /* l / lg  */
9420                   if (GET_CODE (SET_SRC (pat)) == MEM)
9421                     continue;
9422                 }
9423
9424               /* lm / lmg */
9425               if (GET_CODE (pat) == PARALLEL
9426                   && load_multiple_operation (pat, VOIDmode))
9427                 continue;
9428             }
9429
9430           note_stores (pat,
9431                        s390_reg_clobbered_rtx,
9432                        regs_ever_clobbered);
9433         }
9434     }
9435 }
9436
9437 /* Determine the frame area which actually has to be accessed
9438    in the function epilogue. The values are stored at the
9439    given pointers AREA_BOTTOM (address of the lowest used stack
9440    address) and AREA_TOP (address of the first item which does
9441    not belong to the stack frame).  */
9442
9443 static void
9444 s390_frame_area (int *area_bottom, int *area_top)
9445 {
9446   int b, t;
9447
9448   b = INT_MAX;
9449   t = INT_MIN;
9450
9451   if (cfun_frame_layout.first_restore_gpr != -1)
9452     {
9453       b = (cfun_frame_layout.gprs_offset
9454            + cfun_frame_layout.first_restore_gpr * UNITS_PER_LONG);
9455       t = b + (cfun_frame_layout.last_restore_gpr
9456                - cfun_frame_layout.first_restore_gpr + 1) * UNITS_PER_LONG;
9457     }
9458
9459   if (TARGET_64BIT && cfun_save_high_fprs_p)
9460     {
9461       b = MIN (b, cfun_frame_layout.f8_offset);
9462       t = MAX (t, (cfun_frame_layout.f8_offset
9463                    + cfun_frame_layout.high_fprs * 8));
9464     }
9465
9466   if (!TARGET_64BIT)
9467     {
9468       if (cfun_fpr_save_p (FPR4_REGNUM))
9469         {
9470           b = MIN (b, cfun_frame_layout.f4_offset);
9471           t = MAX (t, cfun_frame_layout.f4_offset + 8);
9472         }
9473       if (cfun_fpr_save_p (FPR6_REGNUM))
9474         {
9475           b = MIN (b, cfun_frame_layout.f4_offset + 8);
9476           t = MAX (t, cfun_frame_layout.f4_offset + 16);
9477         }
9478     }
9479   *area_bottom = b;
9480   *area_top = t;
9481 }
9482 /* Update gpr_save_slots in the frame layout trying to make use of
9483    FPRs as GPR save slots.
9484    This is a helper routine of s390_register_info.  */
9485
9486 static void
9487 s390_register_info_gprtofpr ()
9488 {
9489   int save_reg_slot = FPR0_REGNUM;
9490   int i, j;
9491
9492   if (!TARGET_Z10 || !TARGET_HARD_FLOAT || !crtl->is_leaf)
9493     return;
9494
9495   for (i = 15; i >= 6; i--)
9496     {
9497       if (cfun_gpr_save_slot (i) == SAVE_SLOT_NONE)
9498         continue;
9499
9500       /* Advance to the next FP register which can be used as a
9501          GPR save slot.  */
9502       while ((!call_really_used_regs[save_reg_slot]
9503               || df_regs_ever_live_p (save_reg_slot)
9504               || cfun_fpr_save_p (save_reg_slot))
9505              && FP_REGNO_P (save_reg_slot))
9506         save_reg_slot++;
9507       if (!FP_REGNO_P (save_reg_slot))
9508         {
9509           /* We only want to use ldgr/lgdr if we can get rid of
9510              stm/lm entirely.  So undo the gpr slot allocation in
9511              case we ran out of FPR save slots.  */
9512           for (j = 6; j <= 15; j++)
9513             if (FP_REGNO_P (cfun_gpr_save_slot (j)))
9514               cfun_gpr_save_slot (j) = SAVE_SLOT_STACK;
9515           break;
9516         }
9517       cfun_gpr_save_slot (i) = save_reg_slot++;
9518     }
9519 }
9520
9521 /* Set the bits in fpr_bitmap for FPRs which need to be saved due to
9522    stdarg.
9523    This is a helper routine for s390_register_info.  */
9524
9525 static void
9526 s390_register_info_stdarg_fpr ()
9527 {
9528   int i;
9529   int min_fpr;
9530   int max_fpr;
9531
9532   /* Save the FP argument regs for stdarg. f0, f2 for 31 bit and
9533      f0-f4 for 64 bit.  */
9534   if (!cfun->stdarg
9535       || !TARGET_HARD_FLOAT
9536       || !cfun->va_list_fpr_size
9537       || crtl->args.info.fprs >= FP_ARG_NUM_REG)
9538     return;
9539
9540   min_fpr = crtl->args.info.fprs;
9541   max_fpr = min_fpr + cfun->va_list_fpr_size - 1;
9542   if (max_fpr >= FP_ARG_NUM_REG)
9543     max_fpr = FP_ARG_NUM_REG - 1;
9544
9545   /* FPR argument regs start at f0.  */
9546   min_fpr += FPR0_REGNUM;
9547   max_fpr += FPR0_REGNUM;
9548
9549   for (i = min_fpr; i <= max_fpr; i++)
9550     cfun_set_fpr_save (i);
9551 }
9552
9553 /* Reserve the GPR save slots for GPRs which need to be saved due to
9554    stdarg.
9555    This is a helper routine for s390_register_info.  */
9556
9557 static void
9558 s390_register_info_stdarg_gpr ()
9559 {
9560   int i;
9561   int min_gpr;
9562   int max_gpr;
9563
9564   if (!cfun->stdarg
9565       || !cfun->va_list_gpr_size
9566       || crtl->args.info.gprs >= GP_ARG_NUM_REG)
9567     return;
9568
9569   min_gpr = crtl->args.info.gprs;
9570   max_gpr = min_gpr + cfun->va_list_gpr_size - 1;
9571   if (max_gpr >= GP_ARG_NUM_REG)
9572     max_gpr = GP_ARG_NUM_REG - 1;
9573
9574   /* GPR argument regs start at r2.  */
9575   min_gpr += GPR2_REGNUM;
9576   max_gpr += GPR2_REGNUM;
9577
9578   /* If r6 was supposed to be saved into an FPR and now needs to go to
9579      the stack for vararg we have to adjust the restore range to make
9580      sure that the restore is done from stack as well.  */
9581   if (FP_REGNO_P (cfun_gpr_save_slot (GPR6_REGNUM))
9582       && min_gpr <= GPR6_REGNUM
9583       && max_gpr >= GPR6_REGNUM)
9584     {
9585       if (cfun_frame_layout.first_restore_gpr == -1
9586           || cfun_frame_layout.first_restore_gpr > GPR6_REGNUM)
9587         cfun_frame_layout.first_restore_gpr = GPR6_REGNUM;
9588       if (cfun_frame_layout.last_restore_gpr == -1
9589           || cfun_frame_layout.last_restore_gpr < GPR6_REGNUM)
9590         cfun_frame_layout.last_restore_gpr = GPR6_REGNUM;
9591     }
9592
9593   if (cfun_frame_layout.first_save_gpr == -1
9594       || cfun_frame_layout.first_save_gpr > min_gpr)
9595     cfun_frame_layout.first_save_gpr = min_gpr;
9596
9597   if (cfun_frame_layout.last_save_gpr == -1
9598       || cfun_frame_layout.last_save_gpr < max_gpr)
9599     cfun_frame_layout.last_save_gpr = max_gpr;
9600
9601   for (i = min_gpr; i <= max_gpr; i++)
9602     cfun_gpr_save_slot (i) = SAVE_SLOT_STACK;
9603 }
9604
9605 /* Calculate the save and restore ranges for stm(g) and lm(g) in the
9606    prologue and epilogue.  */
9607
9608 static void
9609 s390_register_info_set_ranges ()
9610 {
9611   int i, j;
9612
9613   /* Find the first and the last save slot supposed to use the stack
9614      to set the restore range.
9615      Vararg regs might be marked as save to stack but only the
9616      call-saved regs really need restoring (i.e. r6).  This code
9617      assumes that the vararg regs have not yet been recorded in
9618      cfun_gpr_save_slot.  */
9619   for (i = 0; i < 16 && cfun_gpr_save_slot (i) != SAVE_SLOT_STACK; i++);
9620   for (j = 15; j > i && cfun_gpr_save_slot (j) != SAVE_SLOT_STACK; j--);
9621   cfun_frame_layout.first_restore_gpr = (i == 16) ? -1 : i;
9622   cfun_frame_layout.last_restore_gpr = (i == 16) ? -1 : j;
9623   cfun_frame_layout.first_save_gpr = (i == 16) ? -1 : i;
9624   cfun_frame_layout.last_save_gpr = (i == 16) ? -1 : j;
9625 }
9626
9627 /* The GPR and FPR save slots in cfun->machine->frame_layout are set
9628    for registers which need to be saved in function prologue.
9629    This function can be used until the insns emitted for save/restore
9630    of the regs are visible in the RTL stream.  */
9631
9632 static void
9633 s390_register_info ()
9634 {
9635   int i;
9636   char clobbered_regs[32];
9637
9638   gcc_assert (!epilogue_completed);
9639
9640   if (reload_completed)
9641     /* After reload we rely on our own routine to determine which
9642        registers need saving.  */
9643     s390_regs_ever_clobbered (clobbered_regs);
9644   else
9645     /* During reload we use regs_ever_live as a base since reload
9646        does changes in there which we otherwise would not be aware
9647        of.  */
9648     for (i = 0; i < 32; i++)
9649       clobbered_regs[i] = df_regs_ever_live_p (i);
9650
9651   for (i = 0; i < 32; i++)
9652     clobbered_regs[i] = clobbered_regs[i] && !global_regs[i];
9653
9654   /* Mark the call-saved FPRs which need to be saved.
9655      This needs to be done before checking the special GPRs since the
9656      stack pointer usage depends on whether high FPRs have to be saved
9657      or not.  */
9658   cfun_frame_layout.fpr_bitmap = 0;
9659   cfun_frame_layout.high_fprs = 0;
9660   for (i = FPR0_REGNUM; i <= FPR15_REGNUM; i++)
9661     if (clobbered_regs[i] && !call_really_used_regs[i])
9662       {
9663         cfun_set_fpr_save (i);
9664         if (i >= FPR8_REGNUM)
9665           cfun_frame_layout.high_fprs++;
9666       }
9667
9668   /* Register 12 is used for GOT address, but also as temp in prologue
9669      for split-stack stdarg functions (unless r14 is available).  */
9670   clobbered_regs[12]
9671     |= ((flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
9672         || (flag_split_stack && cfun->stdarg
9673             && (crtl->is_leaf || TARGET_TPF_PROFILING
9674                 || has_hard_reg_initial_val (Pmode, RETURN_REGNUM))));
9675
9676   clobbered_regs[BASE_REGNUM]
9677     |= (cfun->machine->base_reg
9678         && REGNO (cfun->machine->base_reg) == BASE_REGNUM);
9679
9680   clobbered_regs[HARD_FRAME_POINTER_REGNUM]
9681     |= !!frame_pointer_needed;
9682
9683   /* On pre z900 machines this might take until machine dependent
9684      reorg to decide.
9685      save_return_addr_p will only be set on non-zarch machines so
9686      there is no risk that r14 goes into an FPR instead of a stack
9687      slot.  */
9688   clobbered_regs[RETURN_REGNUM]
9689     |= (!crtl->is_leaf
9690         || TARGET_TPF_PROFILING
9691         || cfun->machine->split_branches_pending_p
9692         || cfun_frame_layout.save_return_addr_p
9693         || crtl->calls_eh_return);
9694
9695   clobbered_regs[STACK_POINTER_REGNUM]
9696     |= (!crtl->is_leaf
9697         || TARGET_TPF_PROFILING
9698         || cfun_save_high_fprs_p
9699         || get_frame_size () > 0
9700         || (reload_completed && cfun_frame_layout.frame_size > 0)
9701         || cfun->calls_alloca);
9702
9703   memset (cfun_frame_layout.gpr_save_slots, SAVE_SLOT_NONE, 16);
9704
9705   for (i = 6; i < 16; i++)
9706     if (clobbered_regs[i])
9707       cfun_gpr_save_slot (i) = SAVE_SLOT_STACK;
9708
9709   s390_register_info_stdarg_fpr ();
9710   s390_register_info_gprtofpr ();
9711   s390_register_info_set_ranges ();
9712   /* stdarg functions might need to save GPRs 2 to 6.  This might
9713      override the GPR->FPR save decision made by
9714      s390_register_info_gprtofpr for r6 since vararg regs must go to
9715      the stack.  */
9716   s390_register_info_stdarg_gpr ();
9717 }
9718
9719 /* This function is called by s390_optimize_prologue in order to get
9720    rid of unnecessary GPR save/restore instructions.  The register info
9721    for the GPRs is re-computed and the ranges are re-calculated.  */
9722
9723 static void
9724 s390_optimize_register_info ()
9725 {
9726   char clobbered_regs[32];
9727   int i;
9728
9729   gcc_assert (epilogue_completed);
9730   gcc_assert (!cfun->machine->split_branches_pending_p);
9731
9732   s390_regs_ever_clobbered (clobbered_regs);
9733
9734   for (i = 0; i < 32; i++)
9735     clobbered_regs[i] = clobbered_regs[i] && !global_regs[i];
9736
9737   /* There is still special treatment needed for cases invisible to
9738      s390_regs_ever_clobbered.  */
9739   clobbered_regs[RETURN_REGNUM]
9740     |= (TARGET_TPF_PROFILING
9741         /* When expanding builtin_return_addr in ESA mode we do not
9742            know whether r14 will later be needed as scratch reg when
9743            doing branch splitting.  So the builtin always accesses the
9744            r14 save slot and we need to stick to the save/restore
9745            decision for r14 even if it turns out that it didn't get
9746            clobbered.  */
9747         || cfun_frame_layout.save_return_addr_p
9748         || crtl->calls_eh_return);
9749
9750   memset (cfun_frame_layout.gpr_save_slots, SAVE_SLOT_NONE, 6);
9751
9752   for (i = 6; i < 16; i++)
9753     if (!clobbered_regs[i])
9754       cfun_gpr_save_slot (i) = SAVE_SLOT_NONE;
9755
9756   s390_register_info_set_ranges ();
9757   s390_register_info_stdarg_gpr ();
9758 }
9759
9760 /* Fill cfun->machine with info about frame of current function.  */
9761
9762 static void
9763 s390_frame_info (void)
9764 {
9765   HOST_WIDE_INT lowest_offset;
9766
9767   cfun_frame_layout.first_save_gpr_slot = cfun_frame_layout.first_save_gpr;
9768   cfun_frame_layout.last_save_gpr_slot = cfun_frame_layout.last_save_gpr;
9769
9770   /* The va_arg builtin uses a constant distance of 16 *
9771      UNITS_PER_LONG (r0-r15) to reach the FPRs from the reg_save_area
9772      pointer.  So even if we are going to save the stack pointer in an
9773      FPR we need the stack space in order to keep the offsets
9774      correct.  */
9775   if (cfun->stdarg && cfun_save_arg_fprs_p)
9776     {
9777       cfun_frame_layout.last_save_gpr_slot = STACK_POINTER_REGNUM;
9778
9779       if (cfun_frame_layout.first_save_gpr_slot == -1)
9780         cfun_frame_layout.first_save_gpr_slot = STACK_POINTER_REGNUM;
9781     }
9782
9783   cfun_frame_layout.frame_size = get_frame_size ();
9784   if (!TARGET_64BIT && cfun_frame_layout.frame_size > 0x7fff0000)
9785     fatal_error (input_location,
9786                  "total size of local variables exceeds architecture limit");
9787
9788   if (!TARGET_PACKED_STACK)
9789     {
9790       /* Fixed stack layout.  */
9791       cfun_frame_layout.backchain_offset = 0;
9792       cfun_frame_layout.f0_offset = 16 * UNITS_PER_LONG;
9793       cfun_frame_layout.f4_offset = cfun_frame_layout.f0_offset + 2 * 8;
9794       cfun_frame_layout.f8_offset = -cfun_frame_layout.high_fprs * 8;
9795       cfun_frame_layout.gprs_offset = (cfun_frame_layout.first_save_gpr_slot
9796                                        * UNITS_PER_LONG);
9797     }
9798   else if (TARGET_BACKCHAIN)
9799     {
9800       /* Kernel stack layout - packed stack, backchain, no float  */
9801       gcc_assert (TARGET_SOFT_FLOAT);
9802       cfun_frame_layout.backchain_offset = (STACK_POINTER_OFFSET
9803                                             - UNITS_PER_LONG);
9804
9805       /* The distance between the backchain and the return address
9806          save slot must not change.  So we always need a slot for the
9807          stack pointer which resides in between.  */
9808       cfun_frame_layout.last_save_gpr_slot = STACK_POINTER_REGNUM;
9809
9810       cfun_frame_layout.gprs_offset
9811         = cfun_frame_layout.backchain_offset - cfun_gprs_save_area_size;
9812
9813       /* FPRs will not be saved.  Nevertheless pick sane values to
9814          keep area calculations valid.  */
9815       cfun_frame_layout.f0_offset =
9816         cfun_frame_layout.f4_offset =
9817         cfun_frame_layout.f8_offset = cfun_frame_layout.gprs_offset;
9818     }
9819   else
9820     {
9821       int num_fprs;
9822
9823       /* Packed stack layout without backchain.  */
9824
9825       /* With stdarg FPRs need their dedicated slots.  */
9826       num_fprs = (TARGET_64BIT && cfun->stdarg ? 2
9827                   : (cfun_fpr_save_p (FPR4_REGNUM) +
9828                      cfun_fpr_save_p (FPR6_REGNUM)));
9829       cfun_frame_layout.f4_offset = STACK_POINTER_OFFSET - 8 * num_fprs;
9830
9831       num_fprs = (cfun->stdarg ? 2
9832                   : (cfun_fpr_save_p (FPR0_REGNUM)
9833                      + cfun_fpr_save_p (FPR2_REGNUM)));
9834       cfun_frame_layout.f0_offset = cfun_frame_layout.f4_offset - 8 * num_fprs;
9835
9836       cfun_frame_layout.gprs_offset
9837         = cfun_frame_layout.f0_offset - cfun_gprs_save_area_size;
9838
9839       cfun_frame_layout.f8_offset = (cfun_frame_layout.gprs_offset
9840                                      - cfun_frame_layout.high_fprs * 8);
9841     }
9842
9843   if (cfun_save_high_fprs_p)
9844     cfun_frame_layout.frame_size += cfun_frame_layout.high_fprs * 8;
9845
9846   if (!crtl->is_leaf)
9847     cfun_frame_layout.frame_size += crtl->outgoing_args_size;
9848
9849   /* In the following cases we have to allocate a STACK_POINTER_OFFSET
9850      sized area at the bottom of the stack.  This is required also for
9851      leaf functions.  When GCC generates a local stack reference it
9852      will always add STACK_POINTER_OFFSET to all these references.  */
9853   if (crtl->is_leaf
9854       && !TARGET_TPF_PROFILING
9855       && cfun_frame_layout.frame_size == 0
9856       && !cfun->calls_alloca)
9857     return;
9858
9859   /* Calculate the number of bytes we have used in our own register
9860      save area.  With the packed stack layout we can re-use the
9861      remaining bytes for normal stack elements.  */
9862
9863   if (TARGET_PACKED_STACK)
9864     lowest_offset = MIN (MIN (cfun_frame_layout.f0_offset,
9865                               cfun_frame_layout.f4_offset),
9866                          cfun_frame_layout.gprs_offset);
9867   else
9868     lowest_offset = 0;
9869
9870   if (TARGET_BACKCHAIN)
9871     lowest_offset = MIN (lowest_offset, cfun_frame_layout.backchain_offset);
9872
9873   cfun_frame_layout.frame_size += STACK_POINTER_OFFSET - lowest_offset;
9874
9875   /* If under 31 bit an odd number of gprs has to be saved we have to
9876      adjust the frame size to sustain 8 byte alignment of stack
9877      frames.  */
9878   cfun_frame_layout.frame_size = ((cfun_frame_layout.frame_size +
9879                                    STACK_BOUNDARY / BITS_PER_UNIT - 1)
9880                                   & ~(STACK_BOUNDARY / BITS_PER_UNIT - 1));
9881 }
9882
9883 /* Generate frame layout.  Fills in register and frame data for the current
9884    function in cfun->machine.  This routine can be called multiple times;
9885    it will re-do the complete frame layout every time.  */
9886
9887 static void
9888 s390_init_frame_layout (void)
9889 {
9890   HOST_WIDE_INT frame_size;
9891   int base_used;
9892
9893   /* After LRA the frame layout is supposed to be read-only and should
9894      not be re-computed.  */
9895   if (reload_completed)
9896     return;
9897
9898   /* On S/390 machines, we may need to perform branch splitting, which
9899      will require both base and return address register.  We have no
9900      choice but to assume we're going to need them until right at the
9901      end of the machine dependent reorg phase.  */
9902   if (!TARGET_CPU_ZARCH)
9903     cfun->machine->split_branches_pending_p = true;
9904
9905   do
9906     {
9907       frame_size = cfun_frame_layout.frame_size;
9908
9909       /* Try to predict whether we'll need the base register.  */
9910       base_used = cfun->machine->split_branches_pending_p
9911                   || crtl->uses_const_pool
9912                   || (!DISP_IN_RANGE (frame_size)
9913                       && !CONST_OK_FOR_K (frame_size));
9914
9915       /* Decide which register to use as literal pool base.  In small
9916          leaf functions, try to use an unused call-clobbered register
9917          as base register to avoid save/restore overhead.  */
9918       if (!base_used)
9919         cfun->machine->base_reg = NULL_RTX;
9920       else
9921         {
9922           int br = 0;
9923
9924           if (crtl->is_leaf)
9925             /* Prefer r5 (most likely to be free).  */
9926             for (br = 5; br >= 2 && df_regs_ever_live_p (br); br--)
9927               ;
9928           cfun->machine->base_reg =
9929             gen_rtx_REG (Pmode, (br >= 2) ? br : BASE_REGNUM);
9930         }
9931
9932       s390_register_info ();
9933       s390_frame_info ();
9934     }
9935   while (frame_size != cfun_frame_layout.frame_size);
9936 }
9937
9938 /* Remove the FPR clobbers from a tbegin insn if it can be proven that
9939    the TX is nonescaping.  A transaction is considered escaping if
9940    there is at least one path from tbegin returning CC0 to the
9941    function exit block without an tend.
9942
9943    The check so far has some limitations:
9944    - only single tbegin/tend BBs are supported
9945    - the first cond jump after tbegin must separate the CC0 path from ~CC0
9946    - when CC is copied to a GPR and the CC0 check is done with the GPR
9947      this is not supported
9948 */
9949
9950 static void
9951 s390_optimize_nonescaping_tx (void)
9952 {
9953   const unsigned int CC0 = 1 << 3;
9954   basic_block tbegin_bb = NULL;
9955   basic_block tend_bb = NULL;
9956   basic_block bb;
9957   rtx_insn *insn;
9958   bool result = true;
9959   int bb_index;
9960   rtx_insn *tbegin_insn = NULL;
9961
9962   if (!cfun->machine->tbegin_p)
9963     return;
9964
9965   for (bb_index = 0; bb_index < n_basic_blocks_for_fn (cfun); bb_index++)
9966     {
9967       bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
9968
9969       if (!bb)
9970         continue;
9971
9972       FOR_BB_INSNS (bb, insn)
9973         {
9974           rtx ite, cc, pat, target;
9975           unsigned HOST_WIDE_INT mask;
9976
9977           if (!INSN_P (insn) || INSN_CODE (insn) <= 0)
9978             continue;
9979
9980           pat = PATTERN (insn);
9981
9982           if (GET_CODE (pat) == PARALLEL)
9983             pat = XVECEXP (pat, 0, 0);
9984
9985           if (GET_CODE (pat) != SET
9986               || GET_CODE (SET_SRC (pat)) != UNSPEC_VOLATILE)
9987             continue;
9988
9989           if (XINT (SET_SRC (pat), 1) == UNSPECV_TBEGIN)
9990             {
9991               rtx_insn *tmp;
9992
9993               tbegin_insn = insn;
9994
9995               /* Just return if the tbegin doesn't have clobbers.  */
9996               if (GET_CODE (PATTERN (insn)) != PARALLEL)
9997                 return;
9998
9999               if (tbegin_bb != NULL)
10000                 return;
10001
10002               /* Find the next conditional jump.  */
10003               for (tmp = NEXT_INSN (insn);
10004                    tmp != NULL_RTX;
10005                    tmp = NEXT_INSN (tmp))
10006                 {
10007                   if (reg_set_p (gen_rtx_REG (CCmode, CC_REGNUM), tmp))
10008                     return;
10009                   if (!JUMP_P (tmp))
10010                     continue;
10011
10012                   ite = SET_SRC (PATTERN (tmp));
10013                   if (GET_CODE (ite) != IF_THEN_ELSE)
10014                     continue;
10015
10016                   cc = XEXP (XEXP (ite, 0), 0);
10017                   if (!REG_P (cc) || !CC_REGNO_P (REGNO (cc))
10018                       || GET_MODE (cc) != CCRAWmode
10019                       || GET_CODE (XEXP (XEXP (ite, 0), 1)) != CONST_INT)
10020                     return;
10021
10022                   if (bb->succs->length () != 2)
10023                     return;
10024
10025                   mask = INTVAL (XEXP (XEXP (ite, 0), 1));
10026                   if (GET_CODE (XEXP (ite, 0)) == NE)
10027                     mask ^= 0xf;
10028
10029                   if (mask == CC0)
10030                     target = XEXP (ite, 1);
10031                   else if (mask == (CC0 ^ 0xf))
10032                     target = XEXP (ite, 2);
10033                   else
10034                     return;
10035
10036                   {
10037                     edge_iterator ei;
10038                     edge e1, e2;
10039
10040                     ei = ei_start (bb->succs);
10041                     e1 = ei_safe_edge (ei);
10042                     ei_next (&ei);
10043                     e2 = ei_safe_edge (ei);
10044
10045                     if (e2->flags & EDGE_FALLTHRU)
10046                       {
10047                         e2 = e1;
10048                         e1 = ei_safe_edge (ei);
10049                       }
10050
10051                     if (!(e1->flags & EDGE_FALLTHRU))
10052                       return;
10053
10054                     tbegin_bb = (target == pc_rtx) ? e1->dest : e2->dest;
10055                   }
10056                   if (tmp == BB_END (bb))
10057                     break;
10058                 }
10059             }
10060
10061           if (XINT (SET_SRC (pat), 1) == UNSPECV_TEND)
10062             {
10063               if (tend_bb != NULL)
10064                 return;
10065               tend_bb = bb;
10066             }
10067         }
10068     }
10069
10070   /* Either we successfully remove the FPR clobbers here or we are not
10071      able to do anything for this TX.  Both cases don't qualify for
10072      another look.  */
10073   cfun->machine->tbegin_p = false;
10074
10075   if (tbegin_bb == NULL || tend_bb == NULL)
10076     return;
10077
10078   calculate_dominance_info (CDI_POST_DOMINATORS);
10079   result = dominated_by_p (CDI_POST_DOMINATORS, tbegin_bb, tend_bb);
10080   free_dominance_info (CDI_POST_DOMINATORS);
10081
10082   if (!result)
10083     return;
10084
10085   PATTERN (tbegin_insn) = gen_rtx_PARALLEL (VOIDmode,
10086                             gen_rtvec (2,
10087                                        XVECEXP (PATTERN (tbegin_insn), 0, 0),
10088                                        XVECEXP (PATTERN (tbegin_insn), 0, 1)));
10089   INSN_CODE (tbegin_insn) = -1;
10090   df_insn_rescan (tbegin_insn);
10091
10092   return;
10093 }
10094
10095 /* Return true if it is legal to put a value with MODE into REGNO.  */
10096
10097 bool
10098 s390_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
10099 {
10100   if (!TARGET_VX && VECTOR_NOFP_REGNO_P (regno))
10101     return false;
10102
10103   switch (REGNO_REG_CLASS (regno))
10104     {
10105     case VEC_REGS:
10106       return ((GET_MODE_CLASS (mode) == MODE_INT
10107                && s390_class_max_nregs (VEC_REGS, mode) == 1)
10108               || mode == DFmode
10109               || s390_vector_mode_supported_p (mode));
10110       break;
10111     case FP_REGS:
10112       if (TARGET_VX
10113           && ((GET_MODE_CLASS (mode) == MODE_INT
10114                && s390_class_max_nregs (FP_REGS, mode) == 1)
10115               || mode == DFmode
10116               || s390_vector_mode_supported_p (mode)))
10117         return true;
10118
10119       if (REGNO_PAIR_OK (regno, mode))
10120         {
10121           if (mode == SImode || mode == DImode)
10122             return true;
10123
10124           if (FLOAT_MODE_P (mode) && GET_MODE_CLASS (mode) != MODE_VECTOR_FLOAT)
10125             return true;
10126         }
10127       break;
10128     case ADDR_REGS:
10129       if (FRAME_REGNO_P (regno) && mode == Pmode)
10130         return true;
10131
10132       /* fallthrough */
10133     case GENERAL_REGS:
10134       if (REGNO_PAIR_OK (regno, mode))
10135         {
10136           if (TARGET_ZARCH
10137               || (mode != TFmode && mode != TCmode && mode != TDmode))
10138             return true;
10139         }
10140       break;
10141     case CC_REGS:
10142       if (GET_MODE_CLASS (mode) == MODE_CC)
10143         return true;
10144       break;
10145     case ACCESS_REGS:
10146       if (REGNO_PAIR_OK (regno, mode))
10147         {
10148           if (mode == SImode || mode == Pmode)
10149             return true;
10150         }
10151       break;
10152     default:
10153       return false;
10154     }
10155
10156   return false;
10157 }
10158
10159 /* Return nonzero if register OLD_REG can be renamed to register NEW_REG.  */
10160
10161 bool
10162 s390_hard_regno_rename_ok (unsigned int old_reg, unsigned int new_reg)
10163 {
10164    /* Once we've decided upon a register to use as base register, it must
10165       no longer be used for any other purpose.  */
10166   if (cfun->machine->base_reg)
10167     if (REGNO (cfun->machine->base_reg) == old_reg
10168         || REGNO (cfun->machine->base_reg) == new_reg)
10169       return false;
10170
10171   /* Prevent regrename from using call-saved regs which haven't
10172      actually been saved.  This is necessary since regrename assumes
10173      the backend save/restore decisions are based on
10174      df_regs_ever_live.  Since we have our own routine we have to tell
10175      regrename manually about it.  */
10176   if (GENERAL_REGNO_P (new_reg)
10177       && !call_really_used_regs[new_reg]
10178       && cfun_gpr_save_slot (new_reg) == SAVE_SLOT_NONE)
10179     return false;
10180
10181   return true;
10182 }
10183
10184 /* Return nonzero if register REGNO can be used as a scratch register
10185    in peephole2.  */
10186
10187 static bool
10188 s390_hard_regno_scratch_ok (unsigned int regno)
10189 {
10190   /* See s390_hard_regno_rename_ok.  */
10191   if (GENERAL_REGNO_P (regno)
10192       && !call_really_used_regs[regno]
10193       && cfun_gpr_save_slot (regno) == SAVE_SLOT_NONE)
10194     return false;
10195
10196   return true;
10197 }
10198
10199 /* Maximum number of registers to represent a value of mode MODE
10200    in a register of class RCLASS.  */
10201
10202 int
10203 s390_class_max_nregs (enum reg_class rclass, machine_mode mode)
10204 {
10205   int reg_size;
10206   bool reg_pair_required_p = false;
10207
10208   switch (rclass)
10209     {
10210     case FP_REGS:
10211     case VEC_REGS:
10212       reg_size = TARGET_VX ? 16 : 8;
10213
10214       /* TF and TD modes would fit into a VR but we put them into a
10215          register pair since we do not have 128bit FP instructions on
10216          full VRs.  */
10217       if (TARGET_VX
10218           && SCALAR_FLOAT_MODE_P (mode)
10219           && GET_MODE_SIZE (mode) >= 16)
10220         reg_pair_required_p = true;
10221
10222       /* Even if complex types would fit into a single FPR/VR we force
10223          them into a register pair to deal with the parts more easily.
10224          (FIXME: What about complex ints?)  */
10225       if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
10226         reg_pair_required_p = true;
10227       break;
10228     case ACCESS_REGS:
10229       reg_size = 4;
10230       break;
10231     default:
10232       reg_size = UNITS_PER_WORD;
10233       break;
10234     }
10235
10236   if (reg_pair_required_p)
10237     return 2 * ((GET_MODE_SIZE (mode) / 2 + reg_size - 1) / reg_size);
10238
10239   return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
10240 }
10241
10242 /* Return TRUE if changing mode from FROM to TO should not be allowed
10243    for register class CLASS.  */
10244
10245 int
10246 s390_cannot_change_mode_class (machine_mode from_mode,
10247                                machine_mode to_mode,
10248                                enum reg_class rclass)
10249 {
10250   machine_mode small_mode;
10251   machine_mode big_mode;
10252
10253   if (GET_MODE_SIZE (from_mode) == GET_MODE_SIZE (to_mode))
10254     return 0;
10255
10256   if (GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
10257     {
10258       small_mode = from_mode;
10259       big_mode = to_mode;
10260     }
10261   else
10262     {
10263       small_mode = to_mode;
10264       big_mode = from_mode;
10265     }
10266
10267   /* Values residing in VRs are little-endian style.  All modes are
10268      placed left-aligned in an VR.  This means that we cannot allow
10269      switching between modes with differing sizes.  Also if the vector
10270      facility is available we still place TFmode values in VR register
10271      pairs, since the only instructions we have operating on TFmodes
10272      only deal with register pairs.  Therefore we have to allow DFmode
10273      subregs of TFmodes to enable the TFmode splitters.  */
10274   if (reg_classes_intersect_p (VEC_REGS, rclass)
10275       && (GET_MODE_SIZE (small_mode) < 8
10276           || s390_class_max_nregs (VEC_REGS, big_mode) == 1))
10277     return 1;
10278
10279   /* Likewise for access registers, since they have only half the
10280      word size on 64-bit.  */
10281   if (reg_classes_intersect_p (ACCESS_REGS, rclass))
10282     return 1;
10283
10284   return 0;
10285 }
10286
10287 /* Return true if we use LRA instead of reload pass.  */
10288 static bool
10289 s390_lra_p (void)
10290 {
10291   return s390_lra_flag;
10292 }
10293
10294 /* Return true if register FROM can be eliminated via register TO.  */
10295
10296 static bool
10297 s390_can_eliminate (const int from, const int to)
10298 {
10299   /* On zSeries machines, we have not marked the base register as fixed.
10300      Instead, we have an elimination rule BASE_REGNUM -> BASE_REGNUM.
10301      If a function requires the base register, we say here that this
10302      elimination cannot be performed.  This will cause reload to free
10303      up the base register (as if it were fixed).  On the other hand,
10304      if the current function does *not* require the base register, we
10305      say here the elimination succeeds, which in turn allows reload
10306      to allocate the base register for any other purpose.  */
10307   if (from == BASE_REGNUM && to == BASE_REGNUM)
10308     {
10309       if (TARGET_CPU_ZARCH)
10310         {
10311           s390_init_frame_layout ();
10312           return cfun->machine->base_reg == NULL_RTX;
10313         }
10314
10315       return false;
10316     }
10317
10318   /* Everything else must point into the stack frame.  */
10319   gcc_assert (to == STACK_POINTER_REGNUM
10320               || to == HARD_FRAME_POINTER_REGNUM);
10321
10322   gcc_assert (from == FRAME_POINTER_REGNUM
10323               || from == ARG_POINTER_REGNUM
10324               || from == RETURN_ADDRESS_POINTER_REGNUM);
10325
10326   /* Make sure we actually saved the return address.  */
10327   if (from == RETURN_ADDRESS_POINTER_REGNUM)
10328     if (!crtl->calls_eh_return
10329         && !cfun->stdarg
10330         && !cfun_frame_layout.save_return_addr_p)
10331       return false;
10332
10333   return true;
10334 }
10335
10336 /* Return offset between register FROM and TO initially after prolog.  */
10337
10338 HOST_WIDE_INT
10339 s390_initial_elimination_offset (int from, int to)
10340 {
10341   HOST_WIDE_INT offset;
10342
10343   /* ??? Why are we called for non-eliminable pairs?  */
10344   if (!s390_can_eliminate (from, to))
10345     return 0;
10346
10347   switch (from)
10348     {
10349     case FRAME_POINTER_REGNUM:
10350       offset = (get_frame_size()
10351                 + STACK_POINTER_OFFSET
10352                 + crtl->outgoing_args_size);
10353       break;
10354
10355     case ARG_POINTER_REGNUM:
10356       s390_init_frame_layout ();
10357       offset = cfun_frame_layout.frame_size + STACK_POINTER_OFFSET;
10358       break;
10359
10360     case RETURN_ADDRESS_POINTER_REGNUM:
10361       s390_init_frame_layout ();
10362
10363       if (cfun_frame_layout.first_save_gpr_slot == -1)
10364         {
10365           /* If it turns out that for stdarg nothing went into the reg
10366              save area we also do not need the return address
10367              pointer.  */
10368           if (cfun->stdarg && !cfun_save_arg_fprs_p)
10369             return 0;
10370
10371           gcc_unreachable ();
10372         }
10373
10374       /* In order to make the following work it is not necessary for
10375          r14 to have a save slot.  It is sufficient if one other GPR
10376          got one.  Since the GPRs are always stored without gaps we
10377          are able to calculate where the r14 save slot would
10378          reside.  */
10379       offset = (cfun_frame_layout.frame_size + cfun_frame_layout.gprs_offset +
10380                 (RETURN_REGNUM - cfun_frame_layout.first_save_gpr_slot) *
10381                 UNITS_PER_LONG);
10382       break;
10383
10384     case BASE_REGNUM:
10385       offset = 0;
10386       break;
10387
10388     default:
10389       gcc_unreachable ();
10390     }
10391
10392   return offset;
10393 }
10394
10395 /* Emit insn to save fpr REGNUM at offset OFFSET relative
10396    to register BASE.  Return generated insn.  */
10397
10398 static rtx
10399 save_fpr (rtx base, int offset, int regnum)
10400 {
10401   rtx addr;
10402   addr = gen_rtx_MEM (DFmode, plus_constant (Pmode, base, offset));
10403
10404   if (regnum >= 16 && regnum <= (16 + FP_ARG_NUM_REG))
10405     set_mem_alias_set (addr, get_varargs_alias_set ());
10406   else
10407     set_mem_alias_set (addr, get_frame_alias_set ());
10408
10409   return emit_move_insn (addr, gen_rtx_REG (DFmode, regnum));
10410 }
10411
10412 /* Emit insn to restore fpr REGNUM from offset OFFSET relative
10413    to register BASE.  Return generated insn.  */
10414
10415 static rtx
10416 restore_fpr (rtx base, int offset, int regnum)
10417 {
10418   rtx addr;
10419   addr = gen_rtx_MEM (DFmode, plus_constant (Pmode, base, offset));
10420   set_mem_alias_set (addr, get_frame_alias_set ());
10421
10422   return emit_move_insn (gen_rtx_REG (DFmode, regnum), addr);
10423 }
10424
10425 /* Return true if REGNO is a global register, but not one
10426    of the special ones that need to be saved/restored in anyway.  */
10427
10428 static inline bool
10429 global_not_special_regno_p (int regno)
10430 {
10431   return (global_regs[regno]
10432           /* These registers are special and need to be
10433              restored in any case.  */
10434           && !(regno == STACK_POINTER_REGNUM
10435                || regno == RETURN_REGNUM
10436                || regno == BASE_REGNUM
10437                || (flag_pic && regno == (int)PIC_OFFSET_TABLE_REGNUM)));
10438 }
10439
10440 /* Generate insn to save registers FIRST to LAST into
10441    the register save area located at offset OFFSET
10442    relative to register BASE.  */
10443
10444 static rtx
10445 save_gprs (rtx base, int offset, int first, int last)
10446 {
10447   rtx addr, insn, note;
10448   int i;
10449
10450   addr = plus_constant (Pmode, base, offset);
10451   addr = gen_rtx_MEM (Pmode, addr);
10452
10453   set_mem_alias_set (addr, get_frame_alias_set ());
10454
10455   /* Special-case single register.  */
10456   if (first == last)
10457     {
10458       if (TARGET_64BIT)
10459         insn = gen_movdi (addr, gen_rtx_REG (Pmode, first));
10460       else
10461         insn = gen_movsi (addr, gen_rtx_REG (Pmode, first));
10462
10463       if (!global_not_special_regno_p (first))
10464         RTX_FRAME_RELATED_P (insn) = 1;
10465       return insn;
10466     }
10467
10468
10469   insn = gen_store_multiple (addr,
10470                              gen_rtx_REG (Pmode, first),
10471                              GEN_INT (last - first + 1));
10472
10473   if (first <= 6 && cfun->stdarg)
10474     for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
10475       {
10476         rtx mem = XEXP (XVECEXP (PATTERN (insn), 0, i), 0);
10477
10478         if (first + i <= 6)
10479           set_mem_alias_set (mem, get_varargs_alias_set ());
10480       }
10481
10482   /* We need to set the FRAME_RELATED flag on all SETs
10483      inside the store-multiple pattern.
10484
10485      However, we must not emit DWARF records for registers 2..5
10486      if they are stored for use by variable arguments ...
10487
10488      ??? Unfortunately, it is not enough to simply not the
10489      FRAME_RELATED flags for those SETs, because the first SET
10490      of the PARALLEL is always treated as if it had the flag
10491      set, even if it does not.  Therefore we emit a new pattern
10492      without those registers as REG_FRAME_RELATED_EXPR note.  */
10493
10494   if (first >= 6 && !global_not_special_regno_p (first))
10495     {
10496       rtx pat = PATTERN (insn);
10497
10498       for (i = 0; i < XVECLEN (pat, 0); i++)
10499         if (GET_CODE (XVECEXP (pat, 0, i)) == SET
10500             && !global_not_special_regno_p (REGNO (SET_SRC (XVECEXP (pat,
10501                                                                      0, i)))))
10502           RTX_FRAME_RELATED_P (XVECEXP (pat, 0, i)) = 1;
10503
10504       RTX_FRAME_RELATED_P (insn) = 1;
10505     }
10506   else if (last >= 6)
10507     {
10508       int start;
10509
10510       for (start = first >= 6 ? first : 6; start <= last; start++)
10511         if (!global_not_special_regno_p (start))
10512           break;
10513
10514       if (start > last)
10515         return insn;
10516
10517       addr = plus_constant (Pmode, base,
10518                             offset + (start - first) * UNITS_PER_LONG);
10519
10520       if (start == last)
10521         {
10522           if (TARGET_64BIT)
10523             note = gen_movdi (gen_rtx_MEM (Pmode, addr),
10524                               gen_rtx_REG (Pmode, start));
10525           else
10526             note = gen_movsi (gen_rtx_MEM (Pmode, addr),
10527                               gen_rtx_REG (Pmode, start));
10528           note = PATTERN (note);
10529
10530           add_reg_note (insn, REG_FRAME_RELATED_EXPR, note);
10531           RTX_FRAME_RELATED_P (insn) = 1;
10532
10533           return insn;
10534         }
10535
10536       note = gen_store_multiple (gen_rtx_MEM (Pmode, addr),
10537                                  gen_rtx_REG (Pmode, start),
10538                                  GEN_INT (last - start + 1));
10539       note = PATTERN (note);
10540
10541       add_reg_note (insn, REG_FRAME_RELATED_EXPR, note);
10542
10543       for (i = 0; i < XVECLEN (note, 0); i++)
10544         if (GET_CODE (XVECEXP (note, 0, i)) == SET
10545             && !global_not_special_regno_p (REGNO (SET_SRC (XVECEXP (note,
10546                                                                      0, i)))))
10547           RTX_FRAME_RELATED_P (XVECEXP (note, 0, i)) = 1;
10548
10549       RTX_FRAME_RELATED_P (insn) = 1;
10550     }
10551
10552   return insn;
10553 }
10554
10555 /* Generate insn to restore registers FIRST to LAST from
10556    the register save area located at offset OFFSET
10557    relative to register BASE.  */
10558
10559 static rtx
10560 restore_gprs (rtx base, int offset, int first, int last)
10561 {
10562   rtx addr, insn;
10563
10564   addr = plus_constant (Pmode, base, offset);
10565   addr = gen_rtx_MEM (Pmode, addr);
10566   set_mem_alias_set (addr, get_frame_alias_set ());
10567
10568   /* Special-case single register.  */
10569   if (first == last)
10570     {
10571       if (TARGET_64BIT)
10572         insn = gen_movdi (gen_rtx_REG (Pmode, first), addr);
10573       else
10574         insn = gen_movsi (gen_rtx_REG (Pmode, first), addr);
10575
10576       RTX_FRAME_RELATED_P (insn) = 1;
10577       return insn;
10578     }
10579
10580   insn = gen_load_multiple (gen_rtx_REG (Pmode, first),
10581                             addr,
10582                             GEN_INT (last - first + 1));
10583   RTX_FRAME_RELATED_P (insn) = 1;
10584   return insn;
10585 }
10586
10587 /* Return insn sequence to load the GOT register.  */
10588
10589 static GTY(()) rtx got_symbol;
10590 rtx_insn *
10591 s390_load_got (void)
10592 {
10593   rtx_insn *insns;
10594
10595   /* We cannot use pic_offset_table_rtx here since we use this
10596      function also for non-pic if __tls_get_offset is called and in
10597      that case PIC_OFFSET_TABLE_REGNUM as well as pic_offset_table_rtx
10598      aren't usable.  */
10599   rtx got_rtx = gen_rtx_REG (Pmode, 12);
10600
10601   if (!got_symbol)
10602     {
10603       got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
10604       SYMBOL_REF_FLAGS (got_symbol) = SYMBOL_FLAG_LOCAL;
10605     }
10606
10607   start_sequence ();
10608
10609   if (TARGET_CPU_ZARCH)
10610     {
10611       emit_move_insn (got_rtx, got_symbol);
10612     }
10613   else
10614     {
10615       rtx offset;
10616
10617       offset = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, got_symbol),
10618                                UNSPEC_LTREL_OFFSET);
10619       offset = gen_rtx_CONST (Pmode, offset);
10620       offset = force_const_mem (Pmode, offset);
10621
10622       emit_move_insn (got_rtx, offset);
10623
10624       offset = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (offset, 0)),
10625                                UNSPEC_LTREL_BASE);
10626       offset = gen_rtx_PLUS (Pmode, got_rtx, offset);
10627
10628       emit_move_insn (got_rtx, offset);
10629     }
10630
10631   insns = get_insns ();
10632   end_sequence ();
10633   return insns;
10634 }
10635
10636 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
10637    and the change to the stack pointer.  */
10638
10639 static void
10640 s390_emit_stack_tie (void)
10641 {
10642   rtx mem = gen_frame_mem (BLKmode,
10643                            gen_rtx_REG (Pmode, STACK_POINTER_REGNUM));
10644
10645   emit_insn (gen_stack_tie (mem));
10646 }
10647
10648 /* Copy GPRS into FPR save slots.  */
10649
10650 static void
10651 s390_save_gprs_to_fprs (void)
10652 {
10653   int i;
10654
10655   if (!TARGET_Z10 || !TARGET_HARD_FLOAT || !crtl->is_leaf)
10656     return;
10657
10658   for (i = 6; i < 16; i++)
10659     {
10660       if (FP_REGNO_P (cfun_gpr_save_slot (i)))
10661         {
10662           rtx_insn *insn =
10663             emit_move_insn (gen_rtx_REG (DImode, cfun_gpr_save_slot (i)),
10664                             gen_rtx_REG (DImode, i));
10665           RTX_FRAME_RELATED_P (insn) = 1;
10666           /* This prevents dwarf2cfi from interpreting the set.  Doing
10667              so it might emit def_cfa_register infos setting an FPR as
10668              new CFA.  */
10669           add_reg_note (insn, REG_CFA_REGISTER, copy_rtx (PATTERN (insn)));
10670         }
10671     }
10672 }
10673
10674 /* Restore GPRs from FPR save slots.  */
10675
10676 static void
10677 s390_restore_gprs_from_fprs (void)
10678 {
10679   int i;
10680
10681   if (!TARGET_Z10 || !TARGET_HARD_FLOAT || !crtl->is_leaf)
10682     return;
10683
10684   for (i = 6; i < 16; i++)
10685     {
10686       rtx_insn *insn;
10687
10688       if (!FP_REGNO_P (cfun_gpr_save_slot (i)))
10689         continue;
10690
10691       rtx fpr = gen_rtx_REG (DImode, cfun_gpr_save_slot (i));
10692
10693       if (i == STACK_POINTER_REGNUM)
10694         insn = emit_insn (gen_stack_restore_from_fpr (fpr));
10695       else
10696         insn = emit_move_insn (gen_rtx_REG (DImode, i), fpr);
10697
10698       df_set_regs_ever_live (i, true);
10699       add_reg_note (insn, REG_CFA_RESTORE, gen_rtx_REG (DImode, i));
10700       if (i == STACK_POINTER_REGNUM)
10701         add_reg_note (insn, REG_CFA_DEF_CFA,
10702                       plus_constant (Pmode, stack_pointer_rtx,
10703                                      STACK_POINTER_OFFSET));
10704       RTX_FRAME_RELATED_P (insn) = 1;
10705     }
10706 }
10707
10708
10709 /* A pass run immediately before shrink-wrapping and prologue and epilogue
10710    generation.  */
10711
10712 namespace {
10713
10714 const pass_data pass_data_s390_early_mach =
10715 {
10716   RTL_PASS, /* type */
10717   "early_mach", /* name */
10718   OPTGROUP_NONE, /* optinfo_flags */
10719   TV_MACH_DEP, /* tv_id */
10720   0, /* properties_required */
10721   0, /* properties_provided */
10722   0, /* properties_destroyed */
10723   0, /* todo_flags_start */
10724   ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
10725 };
10726
10727 class pass_s390_early_mach : public rtl_opt_pass
10728 {
10729 public:
10730   pass_s390_early_mach (gcc::context *ctxt)
10731     : rtl_opt_pass (pass_data_s390_early_mach, ctxt)
10732   {}
10733
10734   /* opt_pass methods: */
10735   virtual unsigned int execute (function *);
10736
10737 }; // class pass_s390_early_mach
10738
10739 unsigned int
10740 pass_s390_early_mach::execute (function *fun)
10741 {
10742   rtx_insn *insn;
10743
10744   /* Try to get rid of the FPR clobbers.  */
10745   s390_optimize_nonescaping_tx ();
10746
10747   /* Re-compute register info.  */
10748   s390_register_info ();
10749
10750   /* If we're using a base register, ensure that it is always valid for
10751      the first non-prologue instruction.  */
10752   if (fun->machine->base_reg)
10753     emit_insn_at_entry (gen_main_pool (fun->machine->base_reg));
10754
10755   /* Annotate all constant pool references to let the scheduler know
10756      they implicitly use the base register.  */
10757   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
10758     if (INSN_P (insn))
10759       {
10760         annotate_constant_pool_refs (&PATTERN (insn));
10761         df_insn_rescan (insn);
10762       }
10763   return 0;
10764 }
10765
10766 } // anon namespace
10767
10768 /* Expand the prologue into a bunch of separate insns.  */
10769
10770 void
10771 s390_emit_prologue (void)
10772 {
10773   rtx insn, addr;
10774   rtx temp_reg;
10775   int i;
10776   int offset;
10777   int next_fpr = 0;
10778
10779   /* Choose best register to use for temp use within prologue.
10780      TPF with profiling must avoid the register 14 - the tracing function
10781      needs the original contents of r14 to be preserved.  */
10782
10783   if (!has_hard_reg_initial_val (Pmode, RETURN_REGNUM)
10784       && !crtl->is_leaf
10785       && !TARGET_TPF_PROFILING)
10786     temp_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
10787   else if (flag_split_stack && cfun->stdarg)
10788     temp_reg = gen_rtx_REG (Pmode, 12);
10789   else
10790     temp_reg = gen_rtx_REG (Pmode, 1);
10791
10792   s390_save_gprs_to_fprs ();
10793
10794   /* Save call saved gprs.  */
10795   if (cfun_frame_layout.first_save_gpr != -1)
10796     {
10797       insn = save_gprs (stack_pointer_rtx,
10798                         cfun_frame_layout.gprs_offset +
10799                         UNITS_PER_LONG * (cfun_frame_layout.first_save_gpr
10800                                           - cfun_frame_layout.first_save_gpr_slot),
10801                         cfun_frame_layout.first_save_gpr,
10802                         cfun_frame_layout.last_save_gpr);
10803       emit_insn (insn);
10804     }
10805
10806   /* Dummy insn to mark literal pool slot.  */
10807
10808   if (cfun->machine->base_reg)
10809     emit_insn (gen_main_pool (cfun->machine->base_reg));
10810
10811   offset = cfun_frame_layout.f0_offset;
10812
10813   /* Save f0 and f2.  */
10814   for (i = FPR0_REGNUM; i <= FPR0_REGNUM + 1; i++)
10815     {
10816       if (cfun_fpr_save_p (i))
10817         {
10818           save_fpr (stack_pointer_rtx, offset, i);
10819           offset += 8;
10820         }
10821       else if (!TARGET_PACKED_STACK || cfun->stdarg)
10822         offset += 8;
10823     }
10824
10825   /* Save f4 and f6.  */
10826   offset = cfun_frame_layout.f4_offset;
10827   for (i = FPR4_REGNUM; i <= FPR4_REGNUM + 1; i++)
10828     {
10829       if (cfun_fpr_save_p (i))
10830         {
10831           insn = save_fpr (stack_pointer_rtx, offset, i);
10832           offset += 8;
10833
10834           /* If f4 and f6 are call clobbered they are saved due to
10835              stdargs and therefore are not frame related.  */
10836           if (!call_really_used_regs[i])
10837             RTX_FRAME_RELATED_P (insn) = 1;
10838         }
10839       else if (!TARGET_PACKED_STACK || call_really_used_regs[i])
10840         offset += 8;
10841     }
10842
10843   if (TARGET_PACKED_STACK
10844       && cfun_save_high_fprs_p
10845       && cfun_frame_layout.f8_offset + cfun_frame_layout.high_fprs * 8 > 0)
10846     {
10847       offset = (cfun_frame_layout.f8_offset
10848                 + (cfun_frame_layout.high_fprs - 1) * 8);
10849
10850       for (i = FPR15_REGNUM; i >= FPR8_REGNUM && offset >= 0; i--)
10851         if (cfun_fpr_save_p (i))
10852           {
10853             insn = save_fpr (stack_pointer_rtx, offset, i);
10854
10855             RTX_FRAME_RELATED_P (insn) = 1;
10856             offset -= 8;
10857           }
10858       if (offset >= cfun_frame_layout.f8_offset)
10859         next_fpr = i;
10860     }
10861
10862   if (!TARGET_PACKED_STACK)
10863     next_fpr = cfun_save_high_fprs_p ? FPR15_REGNUM : 0;
10864
10865   if (flag_stack_usage_info)
10866     current_function_static_stack_size = cfun_frame_layout.frame_size;
10867
10868   /* Decrement stack pointer.  */
10869
10870   if (cfun_frame_layout.frame_size > 0)
10871     {
10872       rtx frame_off = GEN_INT (-cfun_frame_layout.frame_size);
10873       rtx real_frame_off;
10874
10875       if (s390_stack_size)
10876         {
10877           HOST_WIDE_INT stack_guard;
10878
10879           if (s390_stack_guard)
10880             stack_guard = s390_stack_guard;
10881           else
10882             {
10883               /* If no value for stack guard is provided the smallest power of 2
10884                  larger than the current frame size is chosen.  */
10885               stack_guard = 1;
10886               while (stack_guard < cfun_frame_layout.frame_size)
10887                 stack_guard <<= 1;
10888             }
10889
10890           if (cfun_frame_layout.frame_size >= s390_stack_size)
10891             {
10892               warning (0, "frame size of function %qs is %wd"
10893                        " bytes exceeding user provided stack limit of "
10894                        "%d bytes.  "
10895                        "An unconditional trap is added.",
10896                        current_function_name(), cfun_frame_layout.frame_size,
10897                        s390_stack_size);
10898               emit_insn (gen_trap ());
10899               emit_barrier ();
10900             }
10901           else
10902             {
10903               /* stack_guard has to be smaller than s390_stack_size.
10904                  Otherwise we would emit an AND with zero which would
10905                  not match the test under mask pattern.  */
10906               if (stack_guard >= s390_stack_size)
10907                 {
10908                   warning (0, "frame size of function %qs is %wd"
10909                            " bytes which is more than half the stack size. "
10910                            "The dynamic check would not be reliable. "
10911                            "No check emitted for this function.",
10912                            current_function_name(),
10913                            cfun_frame_layout.frame_size);
10914                 }
10915               else
10916                 {
10917                   HOST_WIDE_INT stack_check_mask = ((s390_stack_size - 1)
10918                                                     & ~(stack_guard - 1));
10919
10920                   rtx t = gen_rtx_AND (Pmode, stack_pointer_rtx,
10921                                        GEN_INT (stack_check_mask));
10922                   if (TARGET_64BIT)
10923                     emit_insn (gen_ctrapdi4 (gen_rtx_EQ (VOIDmode,
10924                                                          t, const0_rtx),
10925                                              t, const0_rtx, const0_rtx));
10926                   else
10927                     emit_insn (gen_ctrapsi4 (gen_rtx_EQ (VOIDmode,
10928                                                          t, const0_rtx),
10929                                              t, const0_rtx, const0_rtx));
10930                 }
10931             }
10932         }
10933
10934       if (s390_warn_framesize > 0
10935           && cfun_frame_layout.frame_size >= s390_warn_framesize)
10936         warning (0, "frame size of %qs is %wd bytes",
10937                  current_function_name (), cfun_frame_layout.frame_size);
10938
10939       if (s390_warn_dynamicstack_p && cfun->calls_alloca)
10940         warning (0, "%qs uses dynamic stack allocation", current_function_name ());
10941
10942       /* Save incoming stack pointer into temp reg.  */
10943       if (TARGET_BACKCHAIN || next_fpr)
10944         insn = emit_insn (gen_move_insn (temp_reg, stack_pointer_rtx));
10945
10946       /* Subtract frame size from stack pointer.  */
10947
10948       if (DISP_IN_RANGE (INTVAL (frame_off)))
10949         {
10950           insn = gen_rtx_SET (stack_pointer_rtx,
10951                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
10952                                             frame_off));
10953           insn = emit_insn (insn);
10954         }
10955       else
10956         {
10957           if (!CONST_OK_FOR_K (INTVAL (frame_off)))
10958             frame_off = force_const_mem (Pmode, frame_off);
10959
10960           insn = emit_insn (gen_add2_insn (stack_pointer_rtx, frame_off));
10961           annotate_constant_pool_refs (&PATTERN (insn));
10962         }
10963
10964       RTX_FRAME_RELATED_P (insn) = 1;
10965       real_frame_off = GEN_INT (-cfun_frame_layout.frame_size);
10966       add_reg_note (insn, REG_FRAME_RELATED_EXPR,
10967                     gen_rtx_SET (stack_pointer_rtx,
10968                                  gen_rtx_PLUS (Pmode, stack_pointer_rtx,
10969                                                real_frame_off)));
10970
10971       /* Set backchain.  */
10972
10973       if (TARGET_BACKCHAIN)
10974         {
10975           if (cfun_frame_layout.backchain_offset)
10976             addr = gen_rtx_MEM (Pmode,
10977                                 plus_constant (Pmode, stack_pointer_rtx,
10978                                   cfun_frame_layout.backchain_offset));
10979           else
10980             addr = gen_rtx_MEM (Pmode, stack_pointer_rtx);
10981           set_mem_alias_set (addr, get_frame_alias_set ());
10982           insn = emit_insn (gen_move_insn (addr, temp_reg));
10983         }
10984
10985       /* If we support non-call exceptions (e.g. for Java),
10986          we need to make sure the backchain pointer is set up
10987          before any possibly trapping memory access.  */
10988       if (TARGET_BACKCHAIN && cfun->can_throw_non_call_exceptions)
10989         {
10990           addr = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
10991           emit_clobber (addr);
10992         }
10993     }
10994
10995   /* Save fprs 8 - 15 (64 bit ABI).  */
10996
10997   if (cfun_save_high_fprs_p && next_fpr)
10998     {
10999       /* If the stack might be accessed through a different register
11000          we have to make sure that the stack pointer decrement is not
11001          moved below the use of the stack slots.  */
11002       s390_emit_stack_tie ();
11003
11004       insn = emit_insn (gen_add2_insn (temp_reg,
11005                                        GEN_INT (cfun_frame_layout.f8_offset)));
11006
11007       offset = 0;
11008
11009       for (i = FPR8_REGNUM; i <= next_fpr; i++)
11010         if (cfun_fpr_save_p (i))
11011           {
11012             rtx addr = plus_constant (Pmode, stack_pointer_rtx,
11013                                       cfun_frame_layout.frame_size
11014                                       + cfun_frame_layout.f8_offset
11015                                       + offset);
11016
11017             insn = save_fpr (temp_reg, offset, i);
11018             offset += 8;
11019             RTX_FRAME_RELATED_P (insn) = 1;
11020             add_reg_note (insn, REG_FRAME_RELATED_EXPR,
11021                           gen_rtx_SET (gen_rtx_MEM (DFmode, addr),
11022                                        gen_rtx_REG (DFmode, i)));
11023           }
11024     }
11025
11026   /* Set frame pointer, if needed.  */
11027
11028   if (frame_pointer_needed)
11029     {
11030       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
11031       RTX_FRAME_RELATED_P (insn) = 1;
11032     }
11033
11034   /* Set up got pointer, if needed.  */
11035
11036   if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
11037     {
11038       rtx_insn *insns = s390_load_got ();
11039
11040       for (rtx_insn *insn = insns; insn; insn = NEXT_INSN (insn))
11041         annotate_constant_pool_refs (&PATTERN (insn));
11042
11043       emit_insn (insns);
11044     }
11045
11046   if (TARGET_TPF_PROFILING)
11047     {
11048       /* Generate a BAS instruction to serve as a function
11049          entry intercept to facilitate the use of tracing
11050          algorithms located at the branch target.  */
11051       emit_insn (gen_prologue_tpf ());
11052
11053       /* Emit a blockage here so that all code
11054          lies between the profiling mechanisms.  */
11055       emit_insn (gen_blockage ());
11056     }
11057 }
11058
11059 /* Expand the epilogue into a bunch of separate insns.  */
11060
11061 void
11062 s390_emit_epilogue (bool sibcall)
11063 {
11064   rtx frame_pointer, return_reg, cfa_restores = NULL_RTX;
11065   int area_bottom, area_top, offset = 0;
11066   int next_offset;
11067   rtvec p;
11068   int i;
11069
11070   if (TARGET_TPF_PROFILING)
11071     {
11072
11073       /* Generate a BAS instruction to serve as a function
11074          entry intercept to facilitate the use of tracing
11075          algorithms located at the branch target.  */
11076
11077       /* Emit a blockage here so that all code
11078          lies between the profiling mechanisms.  */
11079       emit_insn (gen_blockage ());
11080
11081       emit_insn (gen_epilogue_tpf ());
11082     }
11083
11084   /* Check whether to use frame or stack pointer for restore.  */
11085
11086   frame_pointer = (frame_pointer_needed
11087                    ? hard_frame_pointer_rtx : stack_pointer_rtx);
11088
11089   s390_frame_area (&area_bottom, &area_top);
11090
11091   /* Check whether we can access the register save area.
11092      If not, increment the frame pointer as required.  */
11093
11094   if (area_top <= area_bottom)
11095     {
11096       /* Nothing to restore.  */
11097     }
11098   else if (DISP_IN_RANGE (cfun_frame_layout.frame_size + area_bottom)
11099            && DISP_IN_RANGE (cfun_frame_layout.frame_size + area_top - 1))
11100     {
11101       /* Area is in range.  */
11102       offset = cfun_frame_layout.frame_size;
11103     }
11104   else
11105     {
11106       rtx insn, frame_off, cfa;
11107
11108       offset = area_bottom < 0 ? -area_bottom : 0;
11109       frame_off = GEN_INT (cfun_frame_layout.frame_size - offset);
11110
11111       cfa = gen_rtx_SET (frame_pointer,
11112                          gen_rtx_PLUS (Pmode, frame_pointer, frame_off));
11113       if (DISP_IN_RANGE (INTVAL (frame_off)))
11114         {
11115           insn = gen_rtx_SET (frame_pointer,
11116                               gen_rtx_PLUS (Pmode, frame_pointer, frame_off));
11117           insn = emit_insn (insn);
11118         }
11119       else
11120         {
11121           if (!CONST_OK_FOR_K (INTVAL (frame_off)))
11122             frame_off = force_const_mem (Pmode, frame_off);
11123
11124           insn = emit_insn (gen_add2_insn (frame_pointer, frame_off));
11125           annotate_constant_pool_refs (&PATTERN (insn));
11126         }
11127       add_reg_note (insn, REG_CFA_ADJUST_CFA, cfa);
11128       RTX_FRAME_RELATED_P (insn) = 1;
11129     }
11130
11131   /* Restore call saved fprs.  */
11132
11133   if (TARGET_64BIT)
11134     {
11135       if (cfun_save_high_fprs_p)
11136         {
11137           next_offset = cfun_frame_layout.f8_offset;
11138           for (i = FPR8_REGNUM; i <= FPR15_REGNUM; i++)
11139             {
11140               if (cfun_fpr_save_p (i))
11141                 {
11142                   restore_fpr (frame_pointer,
11143                                offset + next_offset, i);
11144                   cfa_restores
11145                     = alloc_reg_note (REG_CFA_RESTORE,
11146                                       gen_rtx_REG (DFmode, i), cfa_restores);
11147                   next_offset += 8;
11148                 }
11149             }
11150         }
11151
11152     }
11153   else
11154     {
11155       next_offset = cfun_frame_layout.f4_offset;
11156       /* f4, f6 */
11157       for (i = FPR4_REGNUM; i <= FPR4_REGNUM + 1; i++)
11158         {
11159           if (cfun_fpr_save_p (i))
11160             {
11161               restore_fpr (frame_pointer,
11162                            offset + next_offset, i);
11163               cfa_restores
11164                 = alloc_reg_note (REG_CFA_RESTORE,
11165                                   gen_rtx_REG (DFmode, i), cfa_restores);
11166               next_offset += 8;
11167             }
11168           else if (!TARGET_PACKED_STACK)
11169             next_offset += 8;
11170         }
11171
11172     }
11173
11174   /* Return register.  */
11175
11176   return_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
11177
11178   /* Restore call saved gprs.  */
11179
11180   if (cfun_frame_layout.first_restore_gpr != -1)
11181     {
11182       rtx insn, addr;
11183       int i;
11184
11185       /* Check for global register and save them
11186          to stack location from where they get restored.  */
11187
11188       for (i = cfun_frame_layout.first_restore_gpr;
11189            i <= cfun_frame_layout.last_restore_gpr;
11190            i++)
11191         {
11192           if (global_not_special_regno_p (i))
11193             {
11194               addr = plus_constant (Pmode, frame_pointer,
11195                                     offset + cfun_frame_layout.gprs_offset
11196                                     + (i - cfun_frame_layout.first_save_gpr_slot)
11197                                     * UNITS_PER_LONG);
11198               addr = gen_rtx_MEM (Pmode, addr);
11199               set_mem_alias_set (addr, get_frame_alias_set ());
11200               emit_move_insn (addr, gen_rtx_REG (Pmode, i));
11201             }
11202           else
11203             cfa_restores
11204               = alloc_reg_note (REG_CFA_RESTORE,
11205                                 gen_rtx_REG (Pmode, i), cfa_restores);
11206         }
11207
11208       if (! sibcall)
11209         {
11210           /* Fetch return address from stack before load multiple,
11211              this will do good for scheduling.
11212
11213              Only do this if we already decided that r14 needs to be
11214              saved to a stack slot. (And not just because r14 happens to
11215              be in between two GPRs which need saving.)  Otherwise it
11216              would be difficult to take that decision back in
11217              s390_optimize_prologue.  */
11218           if (cfun_gpr_save_slot (RETURN_REGNUM) == SAVE_SLOT_STACK)
11219             {
11220               int return_regnum = find_unused_clobbered_reg();
11221               if (!return_regnum)
11222                 return_regnum = 4;
11223               return_reg = gen_rtx_REG (Pmode, return_regnum);
11224
11225               addr = plus_constant (Pmode, frame_pointer,
11226                                     offset + cfun_frame_layout.gprs_offset
11227                                     + (RETURN_REGNUM
11228                                        - cfun_frame_layout.first_save_gpr_slot)
11229                                     * UNITS_PER_LONG);
11230               addr = gen_rtx_MEM (Pmode, addr);
11231               set_mem_alias_set (addr, get_frame_alias_set ());
11232               emit_move_insn (return_reg, addr);
11233
11234               /* Once we did that optimization we have to make sure
11235                  s390_optimize_prologue does not try to remove the
11236                  store of r14 since we will not be able to find the
11237                  load issued here.  */
11238               cfun_frame_layout.save_return_addr_p = true;
11239             }
11240         }
11241
11242       insn = restore_gprs (frame_pointer,
11243                            offset + cfun_frame_layout.gprs_offset
11244                            + (cfun_frame_layout.first_restore_gpr
11245                               - cfun_frame_layout.first_save_gpr_slot)
11246                            * UNITS_PER_LONG,
11247                            cfun_frame_layout.first_restore_gpr,
11248                            cfun_frame_layout.last_restore_gpr);
11249       insn = emit_insn (insn);
11250       REG_NOTES (insn) = cfa_restores;
11251       add_reg_note (insn, REG_CFA_DEF_CFA,
11252                     plus_constant (Pmode, stack_pointer_rtx,
11253                                    STACK_POINTER_OFFSET));
11254       RTX_FRAME_RELATED_P (insn) = 1;
11255     }
11256
11257   s390_restore_gprs_from_fprs ();
11258
11259   if (! sibcall)
11260     {
11261
11262       /* Return to caller.  */
11263
11264       p = rtvec_alloc (2);
11265
11266       RTVEC_ELT (p, 0) = ret_rtx;
11267       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, return_reg);
11268       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
11269     }
11270 }
11271
11272 /* Implement TARGET_SET_UP_BY_PROLOGUE.  */
11273
11274 static void
11275 s300_set_up_by_prologue (hard_reg_set_container *regs)
11276 {
11277   if (cfun->machine->base_reg
11278       && !call_really_used_regs[REGNO (cfun->machine->base_reg)])
11279     SET_HARD_REG_BIT (regs->set, REGNO (cfun->machine->base_reg));
11280 }
11281
11282 /* -fsplit-stack support.  */
11283
11284 /* A SYMBOL_REF for __morestack.  */
11285 static GTY(()) rtx morestack_ref;
11286
11287 /* When using -fsplit-stack, the allocation routines set a field in
11288    the TCB to the bottom of the stack plus this much space, measured
11289    in bytes.  */
11290
11291 #define SPLIT_STACK_AVAILABLE 1024
11292
11293 /* Emit -fsplit-stack prologue, which goes before the regular function
11294    prologue.  */
11295
11296 void
11297 s390_expand_split_stack_prologue (void)
11298 {
11299   rtx r1, guard, cc = NULL;
11300   rtx_insn *insn;
11301   /* Offset from thread pointer to __private_ss.  */
11302   int psso = TARGET_64BIT ? 0x38 : 0x20;
11303   /* Pointer size in bytes.  */
11304   /* Frame size and argument size - the two parameters to __morestack.  */
11305   HOST_WIDE_INT frame_size = cfun_frame_layout.frame_size;
11306   /* Align argument size to 8 bytes - simplifies __morestack code.  */
11307   HOST_WIDE_INT args_size = crtl->args.size >= 0
11308                             ? ((crtl->args.size + 7) & ~7)
11309                             : 0;
11310   /* Label to be called by __morestack.  */
11311   rtx_code_label *call_done = NULL;
11312   rtx_code_label *parm_base = NULL;
11313   rtx tmp;
11314
11315   gcc_assert (flag_split_stack && reload_completed);
11316   if (!TARGET_CPU_ZARCH)
11317     {
11318       sorry ("CPUs older than z900 are not supported for -fsplit-stack");
11319       return;
11320     }
11321
11322   r1 = gen_rtx_REG (Pmode, 1);
11323
11324   /* If no stack frame will be allocated, don't do anything.  */
11325   if (!frame_size)
11326     {
11327       if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11328         {
11329           /* If va_start is used, just use r15.  */
11330           emit_move_insn (r1,
11331                          gen_rtx_PLUS (Pmode, stack_pointer_rtx,
11332                                        GEN_INT (STACK_POINTER_OFFSET)));
11333
11334         }
11335       return;
11336     }
11337
11338   if (morestack_ref == NULL_RTX)
11339     {
11340       morestack_ref = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
11341       SYMBOL_REF_FLAGS (morestack_ref) |= (SYMBOL_FLAG_LOCAL
11342                                            | SYMBOL_FLAG_FUNCTION);
11343     }
11344
11345   if (CONST_OK_FOR_K (frame_size) || CONST_OK_FOR_Op (frame_size))
11346     {
11347       /* If frame_size will fit in an add instruction, do a stack space
11348          check, and only call __morestack if there's not enough space.  */
11349
11350       /* Get thread pointer.  r1 is the only register we can always destroy - r0
11351          could contain a static chain (and cannot be used to address memory
11352          anyway), r2-r6 can contain parameters, and r6-r15 are callee-saved.  */
11353       emit_move_insn (r1, gen_rtx_REG (Pmode, TP_REGNUM));
11354       /* Aim at __private_ss.  */
11355       guard = gen_rtx_MEM (Pmode, plus_constant (Pmode, r1, psso));
11356
11357       /* If less that 1kiB used, skip addition and compare directly with
11358          __private_ss.  */
11359       if (frame_size > SPLIT_STACK_AVAILABLE)
11360         {
11361           emit_move_insn (r1, guard);
11362           if (TARGET_64BIT)
11363             emit_insn (gen_adddi3 (r1, r1, GEN_INT (frame_size)));
11364           else
11365             emit_insn (gen_addsi3 (r1, r1, GEN_INT (frame_size)));
11366           guard = r1;
11367         }
11368
11369       /* Compare the (maybe adjusted) guard with the stack pointer.  */
11370       cc = s390_emit_compare (LT, stack_pointer_rtx, guard);
11371     }
11372
11373   call_done = gen_label_rtx ();
11374   parm_base = gen_label_rtx ();
11375
11376   /* Emit the parameter block.  */
11377   tmp = gen_split_stack_data (parm_base, call_done,
11378                               GEN_INT (frame_size),
11379                               GEN_INT (args_size));
11380   insn = emit_insn (tmp);
11381   add_reg_note (insn, REG_LABEL_OPERAND, call_done);
11382   LABEL_NUSES (call_done)++;
11383   add_reg_note (insn, REG_LABEL_OPERAND, parm_base);
11384   LABEL_NUSES (parm_base)++;
11385
11386   /* %r1 = litbase.  */
11387   insn = emit_move_insn (r1, gen_rtx_LABEL_REF (VOIDmode, parm_base));
11388   add_reg_note (insn, REG_LABEL_OPERAND, parm_base);
11389   LABEL_NUSES (parm_base)++;
11390
11391   /* Now, we need to call __morestack.  It has very special calling
11392      conventions: it preserves param/return/static chain registers for
11393      calling main function body, and looks for its own parameters at %r1. */
11394
11395   if (cc != NULL)
11396     {
11397       tmp = gen_split_stack_cond_call (morestack_ref, cc, call_done);
11398
11399       insn = emit_jump_insn (tmp);
11400       JUMP_LABEL (insn) = call_done;
11401       LABEL_NUSES (call_done)++;
11402
11403       /* Mark the jump as very unlikely to be taken.  */
11404       add_int_reg_note (insn, REG_BR_PROB, REG_BR_PROB_BASE / 100);
11405
11406       if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11407         {
11408           /* If va_start is used, and __morestack was not called, just use
11409              r15.  */
11410           emit_move_insn (r1,
11411                          gen_rtx_PLUS (Pmode, stack_pointer_rtx,
11412                                        GEN_INT (STACK_POINTER_OFFSET)));
11413         }
11414     }
11415   else
11416     {
11417       tmp = gen_split_stack_call (morestack_ref, call_done);
11418       insn = emit_jump_insn (tmp);
11419       JUMP_LABEL (insn) = call_done;
11420       LABEL_NUSES (call_done)++;
11421       emit_barrier ();
11422     }
11423
11424   /* __morestack will call us here.  */
11425
11426   emit_label (call_done);
11427 }
11428
11429 /* We may have to tell the dataflow pass that the split stack prologue
11430    is initializing a register.  */
11431
11432 static void
11433 s390_live_on_entry (bitmap regs)
11434 {
11435   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11436     {
11437       gcc_assert (flag_split_stack);
11438       bitmap_set_bit (regs, 1);
11439     }
11440 }
11441
11442 /* Return true if the function can use simple_return to return outside
11443    of a shrink-wrapped region.  At present shrink-wrapping is supported
11444    in all cases.  */
11445
11446 bool
11447 s390_can_use_simple_return_insn (void)
11448 {
11449   return true;
11450 }
11451
11452 /* Return true if the epilogue is guaranteed to contain only a return
11453    instruction and if a direct return can therefore be used instead.
11454    One of the main advantages of using direct return instructions
11455    is that we can then use conditional returns.  */
11456
11457 bool
11458 s390_can_use_return_insn (void)
11459 {
11460   int i;
11461
11462   if (!reload_completed)
11463     return false;
11464
11465   if (crtl->profile)
11466     return false;
11467
11468   if (TARGET_TPF_PROFILING)
11469     return false;
11470
11471   for (i = 0; i < 16; i++)
11472     if (cfun_gpr_save_slot (i) != SAVE_SLOT_NONE)
11473       return false;
11474
11475   /* For 31 bit this is not covered by the frame_size check below
11476      since f4, f6 are saved in the register save area without needing
11477      additional stack space.  */
11478   if (!TARGET_64BIT
11479       && (cfun_fpr_save_p (FPR4_REGNUM) || cfun_fpr_save_p (FPR6_REGNUM)))
11480     return false;
11481
11482   if (cfun->machine->base_reg
11483       && !call_really_used_regs[REGNO (cfun->machine->base_reg)])
11484     return false;
11485
11486   return cfun_frame_layout.frame_size == 0;
11487 }
11488
11489 /* The VX ABI differs for vararg functions.  Therefore we need the
11490    prototype of the callee to be available when passing vector type
11491    values.  */
11492 static const char *
11493 s390_invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
11494 {
11495   return ((TARGET_VX_ABI
11496            && typelist == 0
11497            && VECTOR_TYPE_P (TREE_TYPE (val))
11498            && (funcdecl == NULL_TREE
11499                || (TREE_CODE (funcdecl) == FUNCTION_DECL
11500                    && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
11501           ? N_("Vector argument passed to unprototyped function")
11502           : NULL);
11503 }
11504
11505
11506 /* Return the size in bytes of a function argument of
11507    type TYPE and/or mode MODE.  At least one of TYPE or
11508    MODE must be specified.  */
11509
11510 static int
11511 s390_function_arg_size (machine_mode mode, const_tree type)
11512 {
11513   if (type)
11514     return int_size_in_bytes (type);
11515
11516   /* No type info available for some library calls ...  */
11517   if (mode != BLKmode)
11518     return GET_MODE_SIZE (mode);
11519
11520   /* If we have neither type nor mode, abort */
11521   gcc_unreachable ();
11522 }
11523
11524 /* Return true if a function argument of type TYPE and mode MODE
11525    is to be passed in a vector register, if available.  */
11526
11527 bool
11528 s390_function_arg_vector (machine_mode mode, const_tree type)
11529 {
11530   if (!TARGET_VX_ABI)
11531     return false;
11532
11533   if (s390_function_arg_size (mode, type) > 16)
11534     return false;
11535
11536   /* No type info available for some library calls ...  */
11537   if (!type)
11538     return VECTOR_MODE_P (mode);
11539
11540   /* The ABI says that record types with a single member are treated
11541      just like that member would be.  */
11542   while (TREE_CODE (type) == RECORD_TYPE)
11543     {
11544       tree field, single = NULL_TREE;
11545
11546       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11547         {
11548           if (TREE_CODE (field) != FIELD_DECL)
11549             continue;
11550
11551           if (single == NULL_TREE)
11552             single = TREE_TYPE (field);
11553           else
11554             return false;
11555         }
11556
11557       if (single == NULL_TREE)
11558         return false;
11559       else
11560         {
11561           /* If the field declaration adds extra byte due to
11562              e.g. padding this is not accepted as vector type.  */
11563           if (int_size_in_bytes (single) <= 0
11564               || int_size_in_bytes (single) != int_size_in_bytes (type))
11565             return false;
11566           type = single;
11567         }
11568     }
11569
11570   return VECTOR_TYPE_P (type);
11571 }
11572
11573 /* Return true if a function argument of type TYPE and mode MODE
11574    is to be passed in a floating-point register, if available.  */
11575
11576 static bool
11577 s390_function_arg_float (machine_mode mode, const_tree type)
11578 {
11579   if (s390_function_arg_size (mode, type) > 8)
11580     return false;
11581
11582   /* Soft-float changes the ABI: no floating-point registers are used.  */
11583   if (TARGET_SOFT_FLOAT)
11584     return false;
11585
11586   /* No type info available for some library calls ...  */
11587   if (!type)
11588     return mode == SFmode || mode == DFmode || mode == SDmode || mode == DDmode;
11589
11590   /* The ABI says that record types with a single member are treated
11591      just like that member would be.  */
11592   while (TREE_CODE (type) == RECORD_TYPE)
11593     {
11594       tree field, single = NULL_TREE;
11595
11596       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11597         {
11598           if (TREE_CODE (field) != FIELD_DECL)
11599             continue;
11600
11601           if (single == NULL_TREE)
11602             single = TREE_TYPE (field);
11603           else
11604             return false;
11605         }
11606
11607       if (single == NULL_TREE)
11608         return false;
11609       else
11610         type = single;
11611     }
11612
11613   return TREE_CODE (type) == REAL_TYPE;
11614 }
11615
11616 /* Return true if a function argument of type TYPE and mode MODE
11617    is to be passed in an integer register, or a pair of integer
11618    registers, if available.  */
11619
11620 static bool
11621 s390_function_arg_integer (machine_mode mode, const_tree type)
11622 {
11623   int size = s390_function_arg_size (mode, type);
11624   if (size > 8)
11625     return false;
11626
11627   /* No type info available for some library calls ...  */
11628   if (!type)
11629     return GET_MODE_CLASS (mode) == MODE_INT
11630            || (TARGET_SOFT_FLOAT &&  SCALAR_FLOAT_MODE_P (mode));
11631
11632   /* We accept small integral (and similar) types.  */
11633   if (INTEGRAL_TYPE_P (type)
11634       || POINTER_TYPE_P (type)
11635       || TREE_CODE (type) == NULLPTR_TYPE
11636       || TREE_CODE (type) == OFFSET_TYPE
11637       || (TARGET_SOFT_FLOAT && TREE_CODE (type) == REAL_TYPE))
11638     return true;
11639
11640   /* We also accept structs of size 1, 2, 4, 8 that are not
11641      passed in floating-point registers.  */
11642   if (AGGREGATE_TYPE_P (type)
11643       && exact_log2 (size) >= 0
11644       && !s390_function_arg_float (mode, type))
11645     return true;
11646
11647   return false;
11648 }
11649
11650 /* Return 1 if a function argument of type TYPE and mode MODE
11651    is to be passed by reference.  The ABI specifies that only
11652    structures of size 1, 2, 4, or 8 bytes are passed by value,
11653    all other structures (and complex numbers) are passed by
11654    reference.  */
11655
11656 static bool
11657 s390_pass_by_reference (cumulative_args_t ca ATTRIBUTE_UNUSED,
11658                         machine_mode mode, const_tree type,
11659                         bool named ATTRIBUTE_UNUSED)
11660 {
11661   int size = s390_function_arg_size (mode, type);
11662
11663   if (s390_function_arg_vector (mode, type))
11664     return false;
11665
11666   if (size > 8)
11667     return true;
11668
11669   if (type)
11670     {
11671       if (AGGREGATE_TYPE_P (type) && exact_log2 (size) < 0)
11672         return true;
11673
11674       if (TREE_CODE (type) == COMPLEX_TYPE
11675           || TREE_CODE (type) == VECTOR_TYPE)
11676         return true;
11677     }
11678
11679   return false;
11680 }
11681
11682 /* Update the data in CUM to advance over an argument of mode MODE and
11683    data type TYPE.  (TYPE is null for libcalls where that information
11684    may not be available.).  The boolean NAMED specifies whether the
11685    argument is a named argument (as opposed to an unnamed argument
11686    matching an ellipsis).  */
11687
11688 static void
11689 s390_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
11690                            const_tree type, bool named)
11691 {
11692   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
11693
11694   if (s390_function_arg_vector (mode, type))
11695     {
11696       /* We are called for unnamed vector stdarg arguments which are
11697          passed on the stack.  In this case this hook does not have to
11698          do anything since stack arguments are tracked by common
11699          code.  */
11700       if (!named)
11701         return;
11702       cum->vrs += 1;
11703     }
11704   else if (s390_function_arg_float (mode, type))
11705     {
11706       cum->fprs += 1;
11707     }
11708   else if (s390_function_arg_integer (mode, type))
11709     {
11710       int size = s390_function_arg_size (mode, type);
11711       cum->gprs += ((size + UNITS_PER_LONG - 1) / UNITS_PER_LONG);
11712     }
11713   else
11714     gcc_unreachable ();
11715 }
11716
11717 /* Define where to put the arguments to a function.
11718    Value is zero to push the argument on the stack,
11719    or a hard register in which to store the argument.
11720
11721    MODE is the argument's machine mode.
11722    TYPE is the data type of the argument (as a tree).
11723     This is null for libcalls where that information may
11724     not be available.
11725    CUM is a variable of type CUMULATIVE_ARGS which gives info about
11726     the preceding args and about the function being called.
11727    NAMED is nonzero if this argument is a named parameter
11728     (otherwise it is an extra parameter matching an ellipsis).
11729
11730    On S/390, we use general purpose registers 2 through 6 to
11731    pass integer, pointer, and certain structure arguments, and
11732    floating point registers 0 and 2 (0, 2, 4, and 6 on 64-bit)
11733    to pass floating point arguments.  All remaining arguments
11734    are pushed to the stack.  */
11735
11736 static rtx
11737 s390_function_arg (cumulative_args_t cum_v, machine_mode mode,
11738                    const_tree type, bool named)
11739 {
11740   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
11741
11742   if (!named)
11743     s390_check_type_for_vector_abi (type, true, false);
11744
11745   if (s390_function_arg_vector (mode, type))
11746     {
11747       /* Vector arguments being part of the ellipsis are passed on the
11748          stack.  */
11749       if (!named || (cum->vrs + 1 > VEC_ARG_NUM_REG))
11750         return NULL_RTX;
11751
11752       return gen_rtx_REG (mode, cum->vrs + FIRST_VEC_ARG_REGNO);
11753     }
11754   else if (s390_function_arg_float (mode, type))
11755     {
11756       if (cum->fprs + 1 > FP_ARG_NUM_REG)
11757         return NULL_RTX;
11758       else
11759         return gen_rtx_REG (mode, cum->fprs + 16);
11760     }
11761   else if (s390_function_arg_integer (mode, type))
11762     {
11763       int size = s390_function_arg_size (mode, type);
11764       int n_gprs = (size + UNITS_PER_LONG - 1) / UNITS_PER_LONG;
11765
11766       if (cum->gprs + n_gprs > GP_ARG_NUM_REG)
11767         return NULL_RTX;
11768       else if (n_gprs == 1 || UNITS_PER_WORD == UNITS_PER_LONG)
11769         return gen_rtx_REG (mode, cum->gprs + 2);
11770       else if (n_gprs == 2)
11771         {
11772           rtvec p = rtvec_alloc (2);
11773
11774           RTVEC_ELT (p, 0)
11775             = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, cum->gprs + 2),
11776                                          const0_rtx);
11777           RTVEC_ELT (p, 1)
11778             = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, cum->gprs + 3),
11779                                          GEN_INT (4));
11780
11781           return gen_rtx_PARALLEL (mode, p);
11782         }
11783     }
11784
11785   /* After the real arguments, expand_call calls us once again
11786      with a void_type_node type.  Whatever we return here is
11787      passed as operand 2 to the call expanders.
11788
11789      We don't need this feature ...  */
11790   else if (type == void_type_node)
11791     return const0_rtx;
11792
11793   gcc_unreachable ();
11794 }
11795
11796 /* Return true if return values of type TYPE should be returned
11797    in a memory buffer whose address is passed by the caller as
11798    hidden first argument.  */
11799
11800 static bool
11801 s390_return_in_memory (const_tree type, const_tree fundecl ATTRIBUTE_UNUSED)
11802 {
11803   /* We accept small integral (and similar) types.  */
11804   if (INTEGRAL_TYPE_P (type)
11805       || POINTER_TYPE_P (type)
11806       || TREE_CODE (type) == OFFSET_TYPE
11807       || TREE_CODE (type) == REAL_TYPE)
11808     return int_size_in_bytes (type) > 8;
11809
11810   /* vector types which fit into a VR.  */
11811   if (TARGET_VX_ABI
11812       && VECTOR_TYPE_P (type)
11813       && int_size_in_bytes (type) <= 16)
11814     return false;
11815
11816   /* Aggregates and similar constructs are always returned
11817      in memory.  */
11818   if (AGGREGATE_TYPE_P (type)
11819       || TREE_CODE (type) == COMPLEX_TYPE
11820       || VECTOR_TYPE_P (type))
11821     return true;
11822
11823   /* ??? We get called on all sorts of random stuff from
11824      aggregate_value_p.  We can't abort, but it's not clear
11825      what's safe to return.  Pretend it's a struct I guess.  */
11826   return true;
11827 }
11828
11829 /* Function arguments and return values are promoted to word size.  */
11830
11831 static machine_mode
11832 s390_promote_function_mode (const_tree type, machine_mode mode,
11833                             int *punsignedp,
11834                             const_tree fntype ATTRIBUTE_UNUSED,
11835                             int for_return ATTRIBUTE_UNUSED)
11836 {
11837   if (INTEGRAL_MODE_P (mode)
11838       && GET_MODE_SIZE (mode) < UNITS_PER_LONG)
11839     {
11840       if (type != NULL_TREE && POINTER_TYPE_P (type))
11841         *punsignedp = POINTERS_EXTEND_UNSIGNED;
11842       return Pmode;
11843     }
11844
11845   return mode;
11846 }
11847
11848 /* Define where to return a (scalar) value of type RET_TYPE.
11849    If RET_TYPE is null, define where to return a (scalar)
11850    value of mode MODE from a libcall.  */
11851
11852 static rtx
11853 s390_function_and_libcall_value (machine_mode mode,
11854                                  const_tree ret_type,
11855                                  const_tree fntype_or_decl,
11856                                  bool outgoing ATTRIBUTE_UNUSED)
11857 {
11858   /* For vector return types it is important to use the RET_TYPE
11859      argument whenever available since the middle-end might have
11860      changed the mode to a scalar mode.  */
11861   bool vector_ret_type_p = ((ret_type && VECTOR_TYPE_P (ret_type))
11862                             || (!ret_type && VECTOR_MODE_P (mode)));
11863
11864   /* For normal functions perform the promotion as
11865      promote_function_mode would do.  */
11866   if (ret_type)
11867     {
11868       int unsignedp = TYPE_UNSIGNED (ret_type);
11869       mode = promote_function_mode (ret_type, mode, &unsignedp,
11870                                     fntype_or_decl, 1);
11871     }
11872
11873   gcc_assert (GET_MODE_CLASS (mode) == MODE_INT
11874               || SCALAR_FLOAT_MODE_P (mode)
11875               || (TARGET_VX_ABI && vector_ret_type_p));
11876   gcc_assert (GET_MODE_SIZE (mode) <= (TARGET_VX_ABI ? 16 : 8));
11877
11878   if (TARGET_VX_ABI && vector_ret_type_p)
11879     return gen_rtx_REG (mode, FIRST_VEC_ARG_REGNO);
11880   else if (TARGET_HARD_FLOAT && SCALAR_FLOAT_MODE_P (mode))
11881     return gen_rtx_REG (mode, 16);
11882   else if (GET_MODE_SIZE (mode) <= UNITS_PER_LONG
11883            || UNITS_PER_LONG == UNITS_PER_WORD)
11884     return gen_rtx_REG (mode, 2);
11885   else if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_LONG)
11886     {
11887       /* This case is triggered when returning a 64 bit value with
11888          -m31 -mzarch.  Although the value would fit into a single
11889          register it has to be forced into a 32 bit register pair in
11890          order to match the ABI.  */
11891       rtvec p = rtvec_alloc (2);
11892
11893       RTVEC_ELT (p, 0)
11894         = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, 2), const0_rtx);
11895       RTVEC_ELT (p, 1)
11896         = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, 3), GEN_INT (4));
11897
11898       return gen_rtx_PARALLEL (mode, p);
11899     }
11900
11901   gcc_unreachable ();
11902 }
11903
11904 /* Define where to return a scalar return value of type RET_TYPE.  */
11905
11906 static rtx
11907 s390_function_value (const_tree ret_type, const_tree fn_decl_or_type,
11908                      bool outgoing)
11909 {
11910   return s390_function_and_libcall_value (TYPE_MODE (ret_type), ret_type,
11911                                           fn_decl_or_type, outgoing);
11912 }
11913
11914 /* Define where to return a scalar libcall return value of mode
11915    MODE.  */
11916
11917 static rtx
11918 s390_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
11919 {
11920   return s390_function_and_libcall_value (mode, NULL_TREE,
11921                                           NULL_TREE, true);
11922 }
11923
11924
11925 /* Create and return the va_list datatype.
11926
11927    On S/390, va_list is an array type equivalent to
11928
11929       typedef struct __va_list_tag
11930         {
11931             long __gpr;
11932             long __fpr;
11933             void *__overflow_arg_area;
11934             void *__reg_save_area;
11935         } va_list[1];
11936
11937    where __gpr and __fpr hold the number of general purpose
11938    or floating point arguments used up to now, respectively,
11939    __overflow_arg_area points to the stack location of the
11940    next argument passed on the stack, and __reg_save_area
11941    always points to the start of the register area in the
11942    call frame of the current function.  The function prologue
11943    saves all registers used for argument passing into this
11944    area if the function uses variable arguments.  */
11945
11946 static tree
11947 s390_build_builtin_va_list (void)
11948 {
11949   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
11950
11951   record = lang_hooks.types.make_type (RECORD_TYPE);
11952
11953   type_decl =
11954     build_decl (BUILTINS_LOCATION,
11955                 TYPE_DECL, get_identifier ("__va_list_tag"), record);
11956
11957   f_gpr = build_decl (BUILTINS_LOCATION,
11958                       FIELD_DECL, get_identifier ("__gpr"),
11959                       long_integer_type_node);
11960   f_fpr = build_decl (BUILTINS_LOCATION,
11961                       FIELD_DECL, get_identifier ("__fpr"),
11962                       long_integer_type_node);
11963   f_ovf = build_decl (BUILTINS_LOCATION,
11964                       FIELD_DECL, get_identifier ("__overflow_arg_area"),
11965                       ptr_type_node);
11966   f_sav = build_decl (BUILTINS_LOCATION,
11967                       FIELD_DECL, get_identifier ("__reg_save_area"),
11968                       ptr_type_node);
11969
11970   va_list_gpr_counter_field = f_gpr;
11971   va_list_fpr_counter_field = f_fpr;
11972
11973   DECL_FIELD_CONTEXT (f_gpr) = record;
11974   DECL_FIELD_CONTEXT (f_fpr) = record;
11975   DECL_FIELD_CONTEXT (f_ovf) = record;
11976   DECL_FIELD_CONTEXT (f_sav) = record;
11977
11978   TYPE_STUB_DECL (record) = type_decl;
11979   TYPE_NAME (record) = type_decl;
11980   TYPE_FIELDS (record) = f_gpr;
11981   DECL_CHAIN (f_gpr) = f_fpr;
11982   DECL_CHAIN (f_fpr) = f_ovf;
11983   DECL_CHAIN (f_ovf) = f_sav;
11984
11985   layout_type (record);
11986
11987   /* The correct type is an array type of one element.  */
11988   return build_array_type (record, build_index_type (size_zero_node));
11989 }
11990
11991 /* Implement va_start by filling the va_list structure VALIST.
11992    STDARG_P is always true, and ignored.
11993    NEXTARG points to the first anonymous stack argument.
11994
11995    The following global variables are used to initialize
11996    the va_list structure:
11997
11998      crtl->args.info:
11999        holds number of gprs and fprs used for named arguments.
12000      crtl->args.arg_offset_rtx:
12001        holds the offset of the first anonymous stack argument
12002        (relative to the virtual arg pointer).  */
12003
12004 static void
12005 s390_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
12006 {
12007   HOST_WIDE_INT n_gpr, n_fpr;
12008   int off;
12009   tree f_gpr, f_fpr, f_ovf, f_sav;
12010   tree gpr, fpr, ovf, sav, t;
12011
12012   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
12013   f_fpr = DECL_CHAIN (f_gpr);
12014   f_ovf = DECL_CHAIN (f_fpr);
12015   f_sav = DECL_CHAIN (f_ovf);
12016
12017   valist = build_simple_mem_ref (valist);
12018   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
12019   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
12020   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
12021   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
12022
12023   /* Count number of gp and fp argument registers used.  */
12024
12025   n_gpr = crtl->args.info.gprs;
12026   n_fpr = crtl->args.info.fprs;
12027
12028   if (cfun->va_list_gpr_size)
12029     {
12030       t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
12031                   build_int_cst (NULL_TREE, n_gpr));
12032       TREE_SIDE_EFFECTS (t) = 1;
12033       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
12034     }
12035
12036   if (cfun->va_list_fpr_size)
12037     {
12038       t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
12039                   build_int_cst (NULL_TREE, n_fpr));
12040       TREE_SIDE_EFFECTS (t) = 1;
12041       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
12042     }
12043
12044   if (flag_split_stack
12045      && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
12046          == NULL)
12047      && cfun->machine->split_stack_varargs_pointer == NULL_RTX)
12048     {
12049       rtx reg;
12050       rtx_insn *seq;
12051
12052       reg = gen_reg_rtx (Pmode);
12053       cfun->machine->split_stack_varargs_pointer = reg;
12054
12055       start_sequence ();
12056       emit_move_insn (reg, gen_rtx_REG (Pmode, 1));
12057       seq = get_insns ();
12058       end_sequence ();
12059
12060       push_topmost_sequence ();
12061       emit_insn_after (seq, entry_of_function ());
12062       pop_topmost_sequence ();
12063     }
12064
12065   /* Find the overflow area.
12066      FIXME: This currently is too pessimistic when the vector ABI is
12067      enabled.  In that case we *always* set up the overflow area
12068      pointer.  */
12069   if (n_gpr + cfun->va_list_gpr_size > GP_ARG_NUM_REG
12070       || n_fpr + cfun->va_list_fpr_size > FP_ARG_NUM_REG
12071       || TARGET_VX_ABI)
12072     {
12073       if (cfun->machine->split_stack_varargs_pointer == NULL_RTX)
12074         t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
12075       else
12076         t = make_tree (TREE_TYPE (ovf), cfun->machine->split_stack_varargs_pointer);
12077
12078       off = INTVAL (crtl->args.arg_offset_rtx);
12079       off = off < 0 ? 0 : off;
12080       if (TARGET_DEBUG_ARG)
12081         fprintf (stderr, "va_start: n_gpr = %d, n_fpr = %d off %d\n",
12082                  (int)n_gpr, (int)n_fpr, off);
12083
12084       t = fold_build_pointer_plus_hwi (t, off);
12085
12086       t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
12087       TREE_SIDE_EFFECTS (t) = 1;
12088       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
12089     }
12090
12091   /* Find the register save area.  */
12092   if ((cfun->va_list_gpr_size && n_gpr < GP_ARG_NUM_REG)
12093       || (cfun->va_list_fpr_size && n_fpr < FP_ARG_NUM_REG))
12094     {
12095       t = make_tree (TREE_TYPE (sav), return_address_pointer_rtx);
12096       t = fold_build_pointer_plus_hwi (t, -RETURN_REGNUM * UNITS_PER_LONG);
12097
12098       t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
12099       TREE_SIDE_EFFECTS (t) = 1;
12100       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
12101     }
12102 }
12103
12104 /* Implement va_arg by updating the va_list structure
12105    VALIST as required to retrieve an argument of type
12106    TYPE, and returning that argument.
12107
12108    Generates code equivalent to:
12109
12110    if (integral value) {
12111      if (size  <= 4 && args.gpr < 5 ||
12112          size  > 4 && args.gpr < 4 )
12113        ret = args.reg_save_area[args.gpr+8]
12114      else
12115        ret = *args.overflow_arg_area++;
12116    } else if (vector value) {
12117        ret = *args.overflow_arg_area;
12118        args.overflow_arg_area += size / 8;
12119    } else if (float value) {
12120      if (args.fgpr < 2)
12121        ret = args.reg_save_area[args.fpr+64]
12122      else
12123        ret = *args.overflow_arg_area++;
12124    } else if (aggregate value) {
12125      if (args.gpr < 5)
12126        ret = *args.reg_save_area[args.gpr]
12127      else
12128        ret = **args.overflow_arg_area++;
12129    } */
12130
12131 static tree
12132 s390_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
12133                       gimple_seq *post_p ATTRIBUTE_UNUSED)
12134 {
12135   tree f_gpr, f_fpr, f_ovf, f_sav;
12136   tree gpr, fpr, ovf, sav, reg, t, u;
12137   int indirect_p, size, n_reg, sav_ofs, sav_scale, max_reg;
12138   tree lab_false, lab_over;
12139   tree addr = create_tmp_var (ptr_type_node, "addr");
12140   bool left_align_p; /* How a value < UNITS_PER_LONG is aligned within
12141                         a stack slot.  */
12142
12143   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
12144   f_fpr = DECL_CHAIN (f_gpr);
12145   f_ovf = DECL_CHAIN (f_fpr);
12146   f_sav = DECL_CHAIN (f_ovf);
12147
12148   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
12149   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
12150   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
12151
12152   /* The tree for args* cannot be shared between gpr/fpr and ovf since
12153      both appear on a lhs.  */
12154   valist = unshare_expr (valist);
12155   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
12156
12157   size = int_size_in_bytes (type);
12158
12159   s390_check_type_for_vector_abi (type, true, false);
12160
12161   if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
12162     {
12163       if (TARGET_DEBUG_ARG)
12164         {
12165           fprintf (stderr, "va_arg: aggregate type");
12166           debug_tree (type);
12167         }
12168
12169       /* Aggregates are passed by reference.  */
12170       indirect_p = 1;
12171       reg = gpr;
12172       n_reg = 1;
12173
12174       /* kernel stack layout on 31 bit: It is assumed here that no padding
12175          will be added by s390_frame_info because for va_args always an even
12176          number of gprs has to be saved r15-r2 = 14 regs.  */
12177       sav_ofs = 2 * UNITS_PER_LONG;
12178       sav_scale = UNITS_PER_LONG;
12179       size = UNITS_PER_LONG;
12180       max_reg = GP_ARG_NUM_REG - n_reg;
12181       left_align_p = false;
12182     }
12183   else if (s390_function_arg_vector (TYPE_MODE (type), type))
12184     {
12185       if (TARGET_DEBUG_ARG)
12186         {
12187           fprintf (stderr, "va_arg: vector type");
12188           debug_tree (type);
12189         }
12190
12191       indirect_p = 0;
12192       reg = NULL_TREE;
12193       n_reg = 0;
12194       sav_ofs = 0;
12195       sav_scale = 8;
12196       max_reg = 0;
12197       left_align_p = true;
12198     }
12199   else if (s390_function_arg_float (TYPE_MODE (type), type))
12200     {
12201       if (TARGET_DEBUG_ARG)
12202         {
12203           fprintf (stderr, "va_arg: float type");
12204           debug_tree (type);
12205         }
12206
12207       /* FP args go in FP registers, if present.  */
12208       indirect_p = 0;
12209       reg = fpr;
12210       n_reg = 1;
12211       sav_ofs = 16 * UNITS_PER_LONG;
12212       sav_scale = 8;
12213       max_reg = FP_ARG_NUM_REG - n_reg;
12214       left_align_p = false;
12215     }
12216   else
12217     {
12218       if (TARGET_DEBUG_ARG)
12219         {
12220           fprintf (stderr, "va_arg: other type");
12221           debug_tree (type);
12222         }
12223
12224       /* Otherwise into GP registers.  */
12225       indirect_p = 0;
12226       reg = gpr;
12227       n_reg = (size + UNITS_PER_LONG - 1) / UNITS_PER_LONG;
12228
12229       /* kernel stack layout on 31 bit: It is assumed here that no padding
12230          will be added by s390_frame_info because for va_args always an even
12231          number of gprs has to be saved r15-r2 = 14 regs.  */
12232       sav_ofs = 2 * UNITS_PER_LONG;
12233
12234       if (size < UNITS_PER_LONG)
12235         sav_ofs += UNITS_PER_LONG - size;
12236
12237       sav_scale = UNITS_PER_LONG;
12238       max_reg = GP_ARG_NUM_REG - n_reg;
12239       left_align_p = false;
12240     }
12241
12242   /* Pull the value out of the saved registers ...  */
12243
12244   if (reg != NULL_TREE)
12245     {
12246       /*
12247         if (reg > ((typeof (reg))max_reg))
12248           goto lab_false;
12249
12250         addr = sav + sav_ofs + reg * save_scale;
12251
12252         goto lab_over;
12253
12254         lab_false:
12255       */
12256
12257       lab_false = create_artificial_label (UNKNOWN_LOCATION);
12258       lab_over = create_artificial_label (UNKNOWN_LOCATION);
12259
12260       t = fold_convert (TREE_TYPE (reg), size_int (max_reg));
12261       t = build2 (GT_EXPR, boolean_type_node, reg, t);
12262       u = build1 (GOTO_EXPR, void_type_node, lab_false);
12263       t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
12264       gimplify_and_add (t, pre_p);
12265
12266       t = fold_build_pointer_plus_hwi (sav, sav_ofs);
12267       u = build2 (MULT_EXPR, TREE_TYPE (reg), reg,
12268                   fold_convert (TREE_TYPE (reg), size_int (sav_scale)));
12269       t = fold_build_pointer_plus (t, u);
12270
12271       gimplify_assign (addr, t, pre_p);
12272
12273       gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
12274
12275       gimple_seq_add_stmt (pre_p, gimple_build_label (lab_false));
12276     }
12277
12278   /* ... Otherwise out of the overflow area.  */
12279
12280   t = ovf;
12281   if (size < UNITS_PER_LONG && !left_align_p)
12282     t = fold_build_pointer_plus_hwi (t, UNITS_PER_LONG - size);
12283
12284   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
12285
12286   gimplify_assign (addr, t, pre_p);
12287
12288   if (size < UNITS_PER_LONG && left_align_p)
12289     t = fold_build_pointer_plus_hwi (t, UNITS_PER_LONG);
12290   else
12291     t = fold_build_pointer_plus_hwi (t, size);
12292
12293   gimplify_assign (ovf, t, pre_p);
12294
12295   if (reg != NULL_TREE)
12296     gimple_seq_add_stmt (pre_p, gimple_build_label (lab_over));
12297
12298
12299   /* Increment register save count.  */
12300
12301   if (n_reg > 0)
12302     {
12303       u = build2 (PREINCREMENT_EXPR, TREE_TYPE (reg), reg,
12304                   fold_convert (TREE_TYPE (reg), size_int (n_reg)));
12305       gimplify_and_add (u, pre_p);
12306     }
12307
12308   if (indirect_p)
12309     {
12310       t = build_pointer_type_for_mode (build_pointer_type (type),
12311                                        ptr_mode, true);
12312       addr = fold_convert (t, addr);
12313       addr = build_va_arg_indirect_ref (addr);
12314     }
12315   else
12316     {
12317       t = build_pointer_type_for_mode (type, ptr_mode, true);
12318       addr = fold_convert (t, addr);
12319     }
12320
12321   return build_va_arg_indirect_ref (addr);
12322 }
12323
12324 /* Emit rtl for the tbegin or tbegin_retry (RETRY != NULL_RTX)
12325    expanders.
12326    DEST  - Register location where CC will be stored.
12327    TDB   - Pointer to a 256 byte area where to store the transaction.
12328            diagnostic block. NULL if TDB is not needed.
12329    RETRY - Retry count value.  If non-NULL a retry loop for CC2
12330            is emitted
12331    CLOBBER_FPRS_P - If true clobbers for all FPRs are emitted as part
12332                     of the tbegin instruction pattern.  */
12333
12334 void
12335 s390_expand_tbegin (rtx dest, rtx tdb, rtx retry, bool clobber_fprs_p)
12336 {
12337   rtx retry_plus_two = gen_reg_rtx (SImode);
12338   rtx retry_reg = gen_reg_rtx (SImode);
12339   rtx_code_label *retry_label = NULL;
12340
12341   if (retry != NULL_RTX)
12342     {
12343       emit_move_insn (retry_reg, retry);
12344       emit_insn (gen_addsi3 (retry_plus_two, retry_reg, const2_rtx));
12345       emit_insn (gen_addsi3 (retry_reg, retry_reg, const1_rtx));
12346       retry_label = gen_label_rtx ();
12347       emit_label (retry_label);
12348     }
12349
12350   if (clobber_fprs_p)
12351     {
12352       if (TARGET_VX)
12353         emit_insn (gen_tbegin_1_z13 (gen_rtx_CONST_INT (VOIDmode, TBEGIN_MASK),
12354                                      tdb));
12355       else
12356         emit_insn (gen_tbegin_1 (gen_rtx_CONST_INT (VOIDmode, TBEGIN_MASK),
12357                                  tdb));
12358     }
12359   else
12360     emit_insn (gen_tbegin_nofloat_1 (gen_rtx_CONST_INT (VOIDmode, TBEGIN_MASK),
12361                                      tdb));
12362
12363   emit_move_insn (dest, gen_rtx_UNSPEC (SImode,
12364                                         gen_rtvec (1, gen_rtx_REG (CCRAWmode,
12365                                                                    CC_REGNUM)),
12366                                         UNSPEC_CC_TO_INT));
12367   if (retry != NULL_RTX)
12368     {
12369       const int CC0 = 1 << 3;
12370       const int CC1 = 1 << 2;
12371       const int CC3 = 1 << 0;
12372       rtx jump;
12373       rtx count = gen_reg_rtx (SImode);
12374       rtx_code_label *leave_label = gen_label_rtx ();
12375
12376       /* Exit for success and permanent failures.  */
12377       jump = s390_emit_jump (leave_label,
12378                              gen_rtx_EQ (VOIDmode,
12379                                gen_rtx_REG (CCRAWmode, CC_REGNUM),
12380                                gen_rtx_CONST_INT (VOIDmode, CC0 | CC1 | CC3)));
12381       LABEL_NUSES (leave_label) = 1;
12382
12383       /* CC2 - transient failure. Perform retry with ppa.  */
12384       emit_move_insn (count, retry_plus_two);
12385       emit_insn (gen_subsi3 (count, count, retry_reg));
12386       emit_insn (gen_tx_assist (count));
12387       jump = emit_jump_insn (gen_doloop_si64 (retry_label,
12388                                               retry_reg,
12389                                               retry_reg));
12390       JUMP_LABEL (jump) = retry_label;
12391       LABEL_NUSES (retry_label) = 1;
12392       emit_label (leave_label);
12393     }
12394 }
12395
12396
12397 /* Return the decl for the target specific builtin with the function
12398    code FCODE.  */
12399
12400 static tree
12401 s390_builtin_decl (unsigned fcode, bool initialized_p ATTRIBUTE_UNUSED)
12402 {
12403   if (fcode >= S390_BUILTIN_MAX)
12404     return error_mark_node;
12405
12406   return s390_builtin_decls[fcode];
12407 }
12408
12409 /* We call mcount before the function prologue.  So a profiled leaf
12410    function should stay a leaf function.  */
12411
12412 static bool
12413 s390_keep_leaf_when_profiled ()
12414 {
12415   return true;
12416 }
12417
12418 /* Output assembly code for the trampoline template to
12419    stdio stream FILE.
12420
12421    On S/390, we use gpr 1 internally in the trampoline code;
12422    gpr 0 is used to hold the static chain.  */
12423
12424 static void
12425 s390_asm_trampoline_template (FILE *file)
12426 {
12427   rtx op[2];
12428   op[0] = gen_rtx_REG (Pmode, 0);
12429   op[1] = gen_rtx_REG (Pmode, 1);
12430
12431   if (TARGET_64BIT)
12432     {
12433       output_asm_insn ("basr\t%1,0", op);         /* 2 byte */
12434       output_asm_insn ("lmg\t%0,%1,14(%1)", op);  /* 6 byte */
12435       output_asm_insn ("br\t%1", op);             /* 2 byte */
12436       ASM_OUTPUT_SKIP (file, (HOST_WIDE_INT)(TRAMPOLINE_SIZE - 10));
12437     }
12438   else
12439     {
12440       output_asm_insn ("basr\t%1,0", op);         /* 2 byte */
12441       output_asm_insn ("lm\t%0,%1,6(%1)", op);    /* 4 byte */
12442       output_asm_insn ("br\t%1", op);             /* 2 byte */
12443       ASM_OUTPUT_SKIP (file, (HOST_WIDE_INT)(TRAMPOLINE_SIZE - 8));
12444     }
12445 }
12446
12447 /* Emit RTL insns to initialize the variable parts of a trampoline.
12448    FNADDR is an RTX for the address of the function's pure code.
12449    CXT is an RTX for the static chain value for the function.  */
12450
12451 static void
12452 s390_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
12453 {
12454   rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
12455   rtx mem;
12456
12457   emit_block_move (m_tramp, assemble_trampoline_template (),
12458                    GEN_INT (2 * UNITS_PER_LONG), BLOCK_OP_NORMAL);
12459
12460   mem = adjust_address (m_tramp, Pmode, 2 * UNITS_PER_LONG);
12461   emit_move_insn (mem, cxt);
12462   mem = adjust_address (m_tramp, Pmode, 3 * UNITS_PER_LONG);
12463   emit_move_insn (mem, fnaddr);
12464 }
12465
12466 /* Output assembler code to FILE to increment profiler label # LABELNO
12467    for profiling a function entry.  */
12468
12469 void
12470 s390_function_profiler (FILE *file, int labelno)
12471 {
12472   rtx op[7];
12473
12474   char label[128];
12475   ASM_GENERATE_INTERNAL_LABEL (label, "LP", labelno);
12476
12477   fprintf (file, "# function profiler \n");
12478
12479   op[0] = gen_rtx_REG (Pmode, RETURN_REGNUM);
12480   op[1] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
12481   op[1] = gen_rtx_MEM (Pmode, plus_constant (Pmode, op[1], UNITS_PER_LONG));
12482
12483   op[2] = gen_rtx_REG (Pmode, 1);
12484   op[3] = gen_rtx_SYMBOL_REF (Pmode, label);
12485   SYMBOL_REF_FLAGS (op[3]) = SYMBOL_FLAG_LOCAL;
12486
12487   op[4] = gen_rtx_SYMBOL_REF (Pmode, "_mcount");
12488   if (flag_pic)
12489     {
12490       op[4] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op[4]), UNSPEC_PLT);
12491       op[4] = gen_rtx_CONST (Pmode, op[4]);
12492     }
12493
12494   if (TARGET_64BIT)
12495     {
12496       output_asm_insn ("stg\t%0,%1", op);
12497       output_asm_insn ("larl\t%2,%3", op);
12498       output_asm_insn ("brasl\t%0,%4", op);
12499       output_asm_insn ("lg\t%0,%1", op);
12500     }
12501   else if (TARGET_CPU_ZARCH)
12502     {
12503       output_asm_insn ("st\t%0,%1", op);
12504       output_asm_insn ("larl\t%2,%3", op);
12505       output_asm_insn ("brasl\t%0,%4", op);
12506       output_asm_insn ("l\t%0,%1", op);
12507     }
12508   else if (!flag_pic)
12509     {
12510       op[6] = gen_label_rtx ();
12511
12512       output_asm_insn ("st\t%0,%1", op);
12513       output_asm_insn ("bras\t%2,%l6", op);
12514       output_asm_insn (".long\t%4", op);
12515       output_asm_insn (".long\t%3", op);
12516       targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[6]));
12517       output_asm_insn ("l\t%0,0(%2)", op);
12518       output_asm_insn ("l\t%2,4(%2)", op);
12519       output_asm_insn ("basr\t%0,%0", op);
12520       output_asm_insn ("l\t%0,%1", op);
12521     }
12522   else
12523     {
12524       op[5] = gen_label_rtx ();
12525       op[6] = gen_label_rtx ();
12526
12527       output_asm_insn ("st\t%0,%1", op);
12528       output_asm_insn ("bras\t%2,%l6", op);
12529       targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[5]));
12530       output_asm_insn (".long\t%4-%l5", op);
12531       output_asm_insn (".long\t%3-%l5", op);
12532       targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[6]));
12533       output_asm_insn ("lr\t%0,%2", op);
12534       output_asm_insn ("a\t%0,0(%2)", op);
12535       output_asm_insn ("a\t%2,4(%2)", op);
12536       output_asm_insn ("basr\t%0,%0", op);
12537       output_asm_insn ("l\t%0,%1", op);
12538     }
12539 }
12540
12541 /* Encode symbol attributes (local vs. global, tls model) of a SYMBOL_REF
12542    into its SYMBOL_REF_FLAGS.  */
12543
12544 static void
12545 s390_encode_section_info (tree decl, rtx rtl, int first)
12546 {
12547   default_encode_section_info (decl, rtl, first);
12548
12549   if (TREE_CODE (decl) == VAR_DECL)
12550     {
12551       /* Store the alignment to be able to check if we can use
12552          a larl/load-relative instruction.  We only handle the cases
12553          that can go wrong (i.e. no FUNC_DECLs).  */
12554       if (DECL_ALIGN (decl) == 0 || DECL_ALIGN (decl) % 16)
12555         SYMBOL_FLAG_SET_NOTALIGN2 (XEXP (rtl, 0));
12556       else if (DECL_ALIGN (decl) % 32)
12557         SYMBOL_FLAG_SET_NOTALIGN4 (XEXP (rtl, 0));
12558       else if (DECL_ALIGN (decl) % 64)
12559         SYMBOL_FLAG_SET_NOTALIGN8 (XEXP (rtl, 0));
12560     }
12561
12562   /* Literal pool references don't have a decl so they are handled
12563      differently here.  We rely on the information in the MEM_ALIGN
12564      entry to decide upon the alignment.  */
12565   if (MEM_P (rtl)
12566       && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
12567       && TREE_CONSTANT_POOL_ADDRESS_P (XEXP (rtl, 0)))
12568     {
12569       if (MEM_ALIGN (rtl) == 0 || MEM_ALIGN (rtl) % 16)
12570         SYMBOL_FLAG_SET_NOTALIGN2 (XEXP (rtl, 0));
12571       else if (MEM_ALIGN (rtl) % 32)
12572         SYMBOL_FLAG_SET_NOTALIGN4 (XEXP (rtl, 0));
12573       else if (MEM_ALIGN (rtl) % 64)
12574         SYMBOL_FLAG_SET_NOTALIGN8 (XEXP (rtl, 0));
12575     }
12576 }
12577
12578 /* Output thunk to FILE that implements a C++ virtual function call (with
12579    multiple inheritance) to FUNCTION.  The thunk adjusts the this pointer
12580    by DELTA, and unless VCALL_OFFSET is zero, applies an additional adjustment
12581    stored at VCALL_OFFSET in the vtable whose address is located at offset 0
12582    relative to the resulting this pointer.  */
12583
12584 static void
12585 s390_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
12586                       HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
12587                       tree function)
12588 {
12589   rtx op[10];
12590   int nonlocal = 0;
12591
12592   /* Make sure unwind info is emitted for the thunk if needed.  */
12593   final_start_function (emit_barrier (), file, 1);
12594
12595   /* Operand 0 is the target function.  */
12596   op[0] = XEXP (DECL_RTL (function), 0);
12597   if (flag_pic && !SYMBOL_REF_LOCAL_P (op[0]))
12598     {
12599       nonlocal = 1;
12600       op[0] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op[0]),
12601                               TARGET_64BIT ? UNSPEC_PLT : UNSPEC_GOT);
12602       op[0] = gen_rtx_CONST (Pmode, op[0]);
12603     }
12604
12605   /* Operand 1 is the 'this' pointer.  */
12606   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
12607     op[1] = gen_rtx_REG (Pmode, 3);
12608   else
12609     op[1] = gen_rtx_REG (Pmode, 2);
12610
12611   /* Operand 2 is the delta.  */
12612   op[2] = GEN_INT (delta);
12613
12614   /* Operand 3 is the vcall_offset.  */
12615   op[3] = GEN_INT (vcall_offset);
12616
12617   /* Operand 4 is the temporary register.  */
12618   op[4] = gen_rtx_REG (Pmode, 1);
12619
12620   /* Operands 5 to 8 can be used as labels.  */
12621   op[5] = NULL_RTX;
12622   op[6] = NULL_RTX;
12623   op[7] = NULL_RTX;
12624   op[8] = NULL_RTX;
12625
12626   /* Operand 9 can be used for temporary register.  */
12627   op[9] = NULL_RTX;
12628
12629   /* Generate code.  */
12630   if (TARGET_64BIT)
12631     {
12632       /* Setup literal pool pointer if required.  */
12633       if ((!DISP_IN_RANGE (delta)
12634            && !CONST_OK_FOR_K (delta)
12635            && !CONST_OK_FOR_Os (delta))
12636           || (!DISP_IN_RANGE (vcall_offset)
12637               && !CONST_OK_FOR_K (vcall_offset)
12638               && !CONST_OK_FOR_Os (vcall_offset)))
12639         {
12640           op[5] = gen_label_rtx ();
12641           output_asm_insn ("larl\t%4,%5", op);
12642         }
12643
12644       /* Add DELTA to this pointer.  */
12645       if (delta)
12646         {
12647           if (CONST_OK_FOR_J (delta))
12648             output_asm_insn ("la\t%1,%2(%1)", op);
12649           else if (DISP_IN_RANGE (delta))
12650             output_asm_insn ("lay\t%1,%2(%1)", op);
12651           else if (CONST_OK_FOR_K (delta))
12652             output_asm_insn ("aghi\t%1,%2", op);
12653           else if (CONST_OK_FOR_Os (delta))
12654             output_asm_insn ("agfi\t%1,%2", op);
12655           else
12656             {
12657               op[6] = gen_label_rtx ();
12658               output_asm_insn ("agf\t%1,%6-%5(%4)", op);
12659             }
12660         }
12661
12662       /* Perform vcall adjustment.  */
12663       if (vcall_offset)
12664         {
12665           if (DISP_IN_RANGE (vcall_offset))
12666             {
12667               output_asm_insn ("lg\t%4,0(%1)", op);
12668               output_asm_insn ("ag\t%1,%3(%4)", op);
12669             }
12670           else if (CONST_OK_FOR_K (vcall_offset))
12671             {
12672               output_asm_insn ("lghi\t%4,%3", op);
12673               output_asm_insn ("ag\t%4,0(%1)", op);
12674               output_asm_insn ("ag\t%1,0(%4)", op);
12675             }
12676           else if (CONST_OK_FOR_Os (vcall_offset))
12677             {
12678               output_asm_insn ("lgfi\t%4,%3", op);
12679               output_asm_insn ("ag\t%4,0(%1)", op);
12680               output_asm_insn ("ag\t%1,0(%4)", op);
12681             }
12682           else
12683             {
12684               op[7] = gen_label_rtx ();
12685               output_asm_insn ("llgf\t%4,%7-%5(%4)", op);
12686               output_asm_insn ("ag\t%4,0(%1)", op);
12687               output_asm_insn ("ag\t%1,0(%4)", op);
12688             }
12689         }
12690
12691       /* Jump to target.  */
12692       output_asm_insn ("jg\t%0", op);
12693
12694       /* Output literal pool if required.  */
12695       if (op[5])
12696         {
12697           output_asm_insn (".align\t4", op);
12698           targetm.asm_out.internal_label (file, "L",
12699                                           CODE_LABEL_NUMBER (op[5]));
12700         }
12701       if (op[6])
12702         {
12703           targetm.asm_out.internal_label (file, "L",
12704                                           CODE_LABEL_NUMBER (op[6]));
12705           output_asm_insn (".long\t%2", op);
12706         }
12707       if (op[7])
12708         {
12709           targetm.asm_out.internal_label (file, "L",
12710                                           CODE_LABEL_NUMBER (op[7]));
12711           output_asm_insn (".long\t%3", op);
12712         }
12713     }
12714   else
12715     {
12716       /* Setup base pointer if required.  */
12717       if (!vcall_offset
12718           || (!DISP_IN_RANGE (delta)
12719               && !CONST_OK_FOR_K (delta)
12720               && !CONST_OK_FOR_Os (delta))
12721           || (!DISP_IN_RANGE (delta)
12722               && !CONST_OK_FOR_K (vcall_offset)
12723               && !CONST_OK_FOR_Os (vcall_offset)))
12724         {
12725           op[5] = gen_label_rtx ();
12726           output_asm_insn ("basr\t%4,0", op);
12727           targetm.asm_out.internal_label (file, "L",
12728                                           CODE_LABEL_NUMBER (op[5]));
12729         }
12730
12731       /* Add DELTA to this pointer.  */
12732       if (delta)
12733         {
12734           if (CONST_OK_FOR_J (delta))
12735             output_asm_insn ("la\t%1,%2(%1)", op);
12736           else if (DISP_IN_RANGE (delta))
12737             output_asm_insn ("lay\t%1,%2(%1)", op);
12738           else if (CONST_OK_FOR_K (delta))
12739             output_asm_insn ("ahi\t%1,%2", op);
12740           else if (CONST_OK_FOR_Os (delta))
12741             output_asm_insn ("afi\t%1,%2", op);
12742           else
12743             {
12744               op[6] = gen_label_rtx ();
12745               output_asm_insn ("a\t%1,%6-%5(%4)", op);
12746             }
12747         }
12748
12749       /* Perform vcall adjustment.  */
12750       if (vcall_offset)
12751         {
12752           if (CONST_OK_FOR_J (vcall_offset))
12753             {
12754               output_asm_insn ("l\t%4,0(%1)", op);
12755               output_asm_insn ("a\t%1,%3(%4)", op);
12756             }
12757           else if (DISP_IN_RANGE (vcall_offset))
12758             {
12759               output_asm_insn ("l\t%4,0(%1)", op);
12760               output_asm_insn ("ay\t%1,%3(%4)", op);
12761             }
12762           else if (CONST_OK_FOR_K (vcall_offset))
12763             {
12764               output_asm_insn ("lhi\t%4,%3", op);
12765               output_asm_insn ("a\t%4,0(%1)", op);
12766               output_asm_insn ("a\t%1,0(%4)", op);
12767             }
12768           else if (CONST_OK_FOR_Os (vcall_offset))
12769             {
12770               output_asm_insn ("iilf\t%4,%3", op);
12771               output_asm_insn ("a\t%4,0(%1)", op);
12772               output_asm_insn ("a\t%1,0(%4)", op);
12773             }
12774           else
12775             {
12776               op[7] = gen_label_rtx ();
12777               output_asm_insn ("l\t%4,%7-%5(%4)", op);
12778               output_asm_insn ("a\t%4,0(%1)", op);
12779               output_asm_insn ("a\t%1,0(%4)", op);
12780             }
12781
12782           /* We had to clobber the base pointer register.
12783              Re-setup the base pointer (with a different base).  */
12784           op[5] = gen_label_rtx ();
12785           output_asm_insn ("basr\t%4,0", op);
12786           targetm.asm_out.internal_label (file, "L",
12787                                           CODE_LABEL_NUMBER (op[5]));
12788         }
12789
12790       /* Jump to target.  */
12791       op[8] = gen_label_rtx ();
12792
12793       if (!flag_pic)
12794         output_asm_insn ("l\t%4,%8-%5(%4)", op);
12795       else if (!nonlocal)
12796         output_asm_insn ("a\t%4,%8-%5(%4)", op);
12797       /* We cannot call through .plt, since .plt requires %r12 loaded.  */
12798       else if (flag_pic == 1)
12799         {
12800           output_asm_insn ("a\t%4,%8-%5(%4)", op);
12801           output_asm_insn ("l\t%4,%0(%4)", op);
12802         }
12803       else if (flag_pic == 2)
12804         {
12805           op[9] = gen_rtx_REG (Pmode, 0);
12806           output_asm_insn ("l\t%9,%8-4-%5(%4)", op);
12807           output_asm_insn ("a\t%4,%8-%5(%4)", op);
12808           output_asm_insn ("ar\t%4,%9", op);
12809           output_asm_insn ("l\t%4,0(%4)", op);
12810         }
12811
12812       output_asm_insn ("br\t%4", op);
12813
12814       /* Output literal pool.  */
12815       output_asm_insn (".align\t4", op);
12816
12817       if (nonlocal && flag_pic == 2)
12818         output_asm_insn (".long\t%0", op);
12819       if (nonlocal)
12820         {
12821           op[0] = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
12822           SYMBOL_REF_FLAGS (op[0]) = SYMBOL_FLAG_LOCAL;
12823         }
12824
12825       targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[8]));
12826       if (!flag_pic)
12827         output_asm_insn (".long\t%0", op);
12828       else
12829         output_asm_insn (".long\t%0-%5", op);
12830
12831       if (op[6])
12832         {
12833           targetm.asm_out.internal_label (file, "L",
12834                                           CODE_LABEL_NUMBER (op[6]));
12835           output_asm_insn (".long\t%2", op);
12836         }
12837       if (op[7])
12838         {
12839           targetm.asm_out.internal_label (file, "L",
12840                                           CODE_LABEL_NUMBER (op[7]));
12841           output_asm_insn (".long\t%3", op);
12842         }
12843     }
12844   final_end_function ();
12845 }
12846
12847 static bool
12848 s390_valid_pointer_mode (machine_mode mode)
12849 {
12850   return (mode == SImode || (TARGET_64BIT && mode == DImode));
12851 }
12852
12853 /* Checks whether the given CALL_EXPR would use a caller
12854    saved register.  This is used to decide whether sibling call
12855    optimization could be performed on the respective function
12856    call.  */
12857
12858 static bool
12859 s390_call_saved_register_used (tree call_expr)
12860 {
12861   CUMULATIVE_ARGS cum_v;
12862   cumulative_args_t cum;
12863   tree parameter;
12864   machine_mode mode;
12865   tree type;
12866   rtx parm_rtx;
12867   int reg, i;
12868
12869   INIT_CUMULATIVE_ARGS (cum_v, NULL, NULL, 0, 0);
12870   cum = pack_cumulative_args (&cum_v);
12871
12872   for (i = 0; i < call_expr_nargs (call_expr); i++)
12873     {
12874       parameter = CALL_EXPR_ARG (call_expr, i);
12875       gcc_assert (parameter);
12876
12877       /* For an undeclared variable passed as parameter we will get
12878          an ERROR_MARK node here.  */
12879       if (TREE_CODE (parameter) == ERROR_MARK)
12880         return true;
12881
12882       type = TREE_TYPE (parameter);
12883       gcc_assert (type);
12884
12885       mode = TYPE_MODE (type);
12886       gcc_assert (mode);
12887
12888       /* We assume that in the target function all parameters are
12889          named.  This only has an impact on vector argument register
12890          usage none of which is call-saved.  */
12891       if (pass_by_reference (&cum_v, mode, type, true))
12892         {
12893           mode = Pmode;
12894           type = build_pointer_type (type);
12895         }
12896
12897        parm_rtx = s390_function_arg (cum, mode, type, true);
12898
12899        s390_function_arg_advance (cum, mode, type, true);
12900
12901        if (!parm_rtx)
12902          continue;
12903
12904        if (REG_P (parm_rtx))
12905          {
12906            for (reg = 0;
12907                 reg < HARD_REGNO_NREGS (REGNO (parm_rtx), GET_MODE (parm_rtx));
12908                 reg++)
12909              if (!call_used_regs[reg + REGNO (parm_rtx)])
12910                return true;
12911          }
12912
12913        if (GET_CODE (parm_rtx) == PARALLEL)
12914          {
12915            int i;
12916
12917            for (i = 0; i < XVECLEN (parm_rtx, 0); i++)
12918              {
12919                rtx r = XEXP (XVECEXP (parm_rtx, 0, i), 0);
12920
12921                gcc_assert (REG_P (r));
12922
12923                for (reg = 0;
12924                     reg < HARD_REGNO_NREGS (REGNO (r), GET_MODE (r));
12925                     reg++)
12926                  if (!call_used_regs[reg + REGNO (r)])
12927                    return true;
12928              }
12929          }
12930
12931     }
12932   return false;
12933 }
12934
12935 /* Return true if the given call expression can be
12936    turned into a sibling call.
12937    DECL holds the declaration of the function to be called whereas
12938    EXP is the call expression itself.  */
12939
12940 static bool
12941 s390_function_ok_for_sibcall (tree decl, tree exp)
12942 {
12943   /* The TPF epilogue uses register 1.  */
12944   if (TARGET_TPF_PROFILING)
12945     return false;
12946
12947   /* The 31 bit PLT code uses register 12 (GOT pointer - caller saved)
12948      which would have to be restored before the sibcall.  */
12949   if (!TARGET_64BIT && flag_pic && decl && !targetm.binds_local_p (decl))
12950     return false;
12951
12952   /* Register 6 on s390 is available as an argument register but unfortunately
12953      "caller saved". This makes functions needing this register for arguments
12954      not suitable for sibcalls.  */
12955   return !s390_call_saved_register_used (exp);
12956 }
12957
12958 /* Return the fixed registers used for condition codes.  */
12959
12960 static bool
12961 s390_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
12962 {
12963   *p1 = CC_REGNUM;
12964   *p2 = INVALID_REGNUM;
12965
12966   return true;
12967 }
12968
12969 /* This function is used by the call expanders of the machine description.
12970    It emits the call insn itself together with the necessary operations
12971    to adjust the target address and returns the emitted insn.
12972    ADDR_LOCATION is the target address rtx
12973    TLS_CALL the location of the thread-local symbol
12974    RESULT_REG the register where the result of the call should be stored
12975    RETADDR_REG the register where the return address should be stored
12976                If this parameter is NULL_RTX the call is considered
12977                to be a sibling call.  */
12978
12979 rtx_insn *
12980 s390_emit_call (rtx addr_location, rtx tls_call, rtx result_reg,
12981                 rtx retaddr_reg)
12982 {
12983   bool plt_call = false;
12984   rtx_insn *insn;
12985   rtx call;
12986   rtx clobber;
12987   rtvec vec;
12988
12989   /* Direct function calls need special treatment.  */
12990   if (GET_CODE (addr_location) == SYMBOL_REF)
12991     {
12992       /* When calling a global routine in PIC mode, we must
12993          replace the symbol itself with the PLT stub.  */
12994       if (flag_pic && !SYMBOL_REF_LOCAL_P (addr_location))
12995         {
12996           if (TARGET_64BIT || retaddr_reg != NULL_RTX)
12997             {
12998               addr_location = gen_rtx_UNSPEC (Pmode,
12999                                               gen_rtvec (1, addr_location),
13000                                               UNSPEC_PLT);
13001               addr_location = gen_rtx_CONST (Pmode, addr_location);
13002               plt_call = true;
13003             }
13004           else
13005             /* For -fpic code the PLT entries might use r12 which is
13006                call-saved.  Therefore we cannot do a sibcall when
13007                calling directly using a symbol ref.  When reaching
13008                this point we decided (in s390_function_ok_for_sibcall)
13009                to do a sibcall for a function pointer but one of the
13010                optimizers was able to get rid of the function pointer
13011                by propagating the symbol ref into the call.  This
13012                optimization is illegal for S/390 so we turn the direct
13013                call into a indirect call again.  */
13014             addr_location = force_reg (Pmode, addr_location);
13015         }
13016
13017       /* Unless we can use the bras(l) insn, force the
13018          routine address into a register.  */
13019       if (!TARGET_SMALL_EXEC && !TARGET_CPU_ZARCH)
13020         {
13021           if (flag_pic)
13022             addr_location = legitimize_pic_address (addr_location, 0);
13023           else
13024             addr_location = force_reg (Pmode, addr_location);
13025         }
13026     }
13027
13028   /* If it is already an indirect call or the code above moved the
13029      SYMBOL_REF to somewhere else make sure the address can be found in
13030      register 1.  */
13031   if (retaddr_reg == NULL_RTX
13032       && GET_CODE (addr_location) != SYMBOL_REF
13033       && !plt_call)
13034     {
13035       emit_move_insn (gen_rtx_REG (Pmode, SIBCALL_REGNUM), addr_location);
13036       addr_location = gen_rtx_REG (Pmode, SIBCALL_REGNUM);
13037     }
13038
13039   addr_location = gen_rtx_MEM (QImode, addr_location);
13040   call = gen_rtx_CALL (VOIDmode, addr_location, const0_rtx);
13041
13042   if (result_reg != NULL_RTX)
13043     call = gen_rtx_SET (result_reg, call);
13044
13045   if (retaddr_reg != NULL_RTX)
13046     {
13047       clobber = gen_rtx_CLOBBER (VOIDmode, retaddr_reg);
13048
13049       if (tls_call != NULL_RTX)
13050         vec = gen_rtvec (3, call, clobber,
13051                          gen_rtx_USE (VOIDmode, tls_call));
13052       else
13053         vec = gen_rtvec (2, call, clobber);
13054
13055       call = gen_rtx_PARALLEL (VOIDmode, vec);
13056     }
13057
13058   insn = emit_call_insn (call);
13059
13060   /* 31-bit PLT stubs and tls calls use the GOT register implicitly.  */
13061   if ((!TARGET_64BIT && plt_call) || tls_call != NULL_RTX)
13062     {
13063       /* s390_function_ok_for_sibcall should
13064          have denied sibcalls in this case.  */
13065       gcc_assert (retaddr_reg != NULL_RTX);
13066       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, 12));
13067     }
13068   return insn;
13069 }
13070
13071 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE.  */
13072
13073 static void
13074 s390_conditional_register_usage (void)
13075 {
13076   int i;
13077
13078   if (flag_pic)
13079     {
13080       fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
13081       call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
13082     }
13083   if (TARGET_CPU_ZARCH)
13084     {
13085       fixed_regs[BASE_REGNUM] = 0;
13086       call_used_regs[BASE_REGNUM] = 0;
13087       fixed_regs[RETURN_REGNUM] = 0;
13088       call_used_regs[RETURN_REGNUM] = 0;
13089     }
13090   if (TARGET_64BIT)
13091     {
13092       for (i = FPR8_REGNUM; i <= FPR15_REGNUM; i++)
13093         call_used_regs[i] = call_really_used_regs[i] = 0;
13094     }
13095   else
13096     {
13097       call_used_regs[FPR4_REGNUM] = call_really_used_regs[FPR4_REGNUM] = 0;
13098       call_used_regs[FPR6_REGNUM] = call_really_used_regs[FPR6_REGNUM] = 0;
13099     }
13100
13101   if (TARGET_SOFT_FLOAT)
13102     {
13103       for (i = FPR0_REGNUM; i <= FPR15_REGNUM; i++)
13104         call_used_regs[i] = fixed_regs[i] = 1;
13105     }
13106
13107   /* Disable v16 - v31 for non-vector target.  */
13108   if (!TARGET_VX)
13109     {
13110       for (i = VR16_REGNUM; i <= VR31_REGNUM; i++)
13111         fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
13112     }
13113 }
13114
13115 /* Corresponding function to eh_return expander.  */
13116
13117 static GTY(()) rtx s390_tpf_eh_return_symbol;
13118 void
13119 s390_emit_tpf_eh_return (rtx target)
13120 {
13121   rtx_insn *insn;
13122   rtx reg, orig_ra;
13123
13124   if (!s390_tpf_eh_return_symbol)
13125     s390_tpf_eh_return_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tpf_eh_return");
13126
13127   reg = gen_rtx_REG (Pmode, 2);
13128   orig_ra = gen_rtx_REG (Pmode, 3);
13129
13130   emit_move_insn (reg, target);
13131   emit_move_insn (orig_ra, get_hard_reg_initial_val (Pmode, RETURN_REGNUM));
13132   insn = s390_emit_call (s390_tpf_eh_return_symbol, NULL_RTX, reg,
13133                                      gen_rtx_REG (Pmode, RETURN_REGNUM));
13134   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
13135   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), orig_ra);
13136
13137   emit_move_insn (EH_RETURN_HANDLER_RTX, reg);
13138 }
13139
13140 /* Rework the prologue/epilogue to avoid saving/restoring
13141    registers unnecessarily.  */
13142
13143 static void
13144 s390_optimize_prologue (void)
13145 {
13146   rtx_insn *insn, *new_insn, *next_insn;
13147
13148   /* Do a final recompute of the frame-related data.  */
13149   s390_optimize_register_info ();
13150
13151   /* If all special registers are in fact used, there's nothing we
13152      can do, so no point in walking the insn list.  */
13153
13154   if (cfun_frame_layout.first_save_gpr <= BASE_REGNUM
13155       && cfun_frame_layout.last_save_gpr >= BASE_REGNUM
13156       && (TARGET_CPU_ZARCH
13157           || (cfun_frame_layout.first_save_gpr <= RETURN_REGNUM
13158               && cfun_frame_layout.last_save_gpr >= RETURN_REGNUM)))
13159     return;
13160
13161   /* Search for prologue/epilogue insns and replace them.  */
13162
13163   for (insn = get_insns (); insn; insn = next_insn)
13164     {
13165       int first, last, off;
13166       rtx set, base, offset;
13167       rtx pat;
13168
13169       next_insn = NEXT_INSN (insn);
13170
13171       if (! NONJUMP_INSN_P (insn) || ! RTX_FRAME_RELATED_P (insn))
13172         continue;
13173
13174       pat = PATTERN (insn);
13175
13176       /* Remove ldgr/lgdr instructions used for saving and restore
13177          GPRs if possible.  */
13178       if (TARGET_Z10)
13179         {
13180           rtx tmp_pat = pat;
13181
13182           if (INSN_CODE (insn) == CODE_FOR_stack_restore_from_fpr)
13183             tmp_pat = XVECEXP (pat, 0, 0);
13184
13185           if (GET_CODE (tmp_pat) == SET
13186               && GET_MODE (SET_SRC (tmp_pat)) == DImode
13187               && REG_P (SET_SRC (tmp_pat))
13188               && REG_P (SET_DEST (tmp_pat)))
13189             {
13190               int src_regno = REGNO (SET_SRC (tmp_pat));
13191               int dest_regno = REGNO (SET_DEST (tmp_pat));
13192               int gpr_regno;
13193               int fpr_regno;
13194
13195               if (!((GENERAL_REGNO_P (src_regno)
13196                      && FP_REGNO_P (dest_regno))
13197                     || (FP_REGNO_P (src_regno)
13198                         && GENERAL_REGNO_P (dest_regno))))
13199                 continue;
13200
13201               gpr_regno = GENERAL_REGNO_P (src_regno) ? src_regno : dest_regno;
13202               fpr_regno = FP_REGNO_P (src_regno) ? src_regno : dest_regno;
13203
13204               /* GPR must be call-saved, FPR must be call-clobbered.  */
13205               if (!call_really_used_regs[fpr_regno]
13206                   || call_really_used_regs[gpr_regno])
13207                 continue;
13208
13209               /* It must not happen that what we once saved in an FPR now
13210                  needs a stack slot.  */
13211               gcc_assert (cfun_gpr_save_slot (gpr_regno) != SAVE_SLOT_STACK);
13212
13213               if (cfun_gpr_save_slot (gpr_regno) == SAVE_SLOT_NONE)
13214                 {
13215                   remove_insn (insn);
13216                   continue;
13217                 }
13218             }
13219         }
13220
13221       if (GET_CODE (pat) == PARALLEL
13222           && store_multiple_operation (pat, VOIDmode))
13223         {
13224           set = XVECEXP (pat, 0, 0);
13225           first = REGNO (SET_SRC (set));
13226           last = first + XVECLEN (pat, 0) - 1;
13227           offset = const0_rtx;
13228           base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
13229           off = INTVAL (offset);
13230
13231           if (GET_CODE (base) != REG || off < 0)
13232             continue;
13233           if (cfun_frame_layout.first_save_gpr != -1
13234               && (cfun_frame_layout.first_save_gpr < first
13235                   || cfun_frame_layout.last_save_gpr > last))
13236             continue;
13237           if (REGNO (base) != STACK_POINTER_REGNUM
13238               && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
13239             continue;
13240           if (first > BASE_REGNUM || last < BASE_REGNUM)
13241             continue;
13242
13243           if (cfun_frame_layout.first_save_gpr != -1)
13244             {
13245               rtx s_pat = save_gprs (base,
13246                                      off + (cfun_frame_layout.first_save_gpr
13247                                             - first) * UNITS_PER_LONG,
13248                                      cfun_frame_layout.first_save_gpr,
13249                                      cfun_frame_layout.last_save_gpr);
13250               new_insn = emit_insn_before (s_pat, insn);
13251               INSN_ADDRESSES_NEW (new_insn, -1);
13252             }
13253
13254           remove_insn (insn);
13255           continue;
13256         }
13257
13258       if (cfun_frame_layout.first_save_gpr == -1
13259           && GET_CODE (pat) == SET
13260           && GENERAL_REG_P (SET_SRC (pat))
13261           && GET_CODE (SET_DEST (pat)) == MEM)
13262         {
13263           set = pat;
13264           first = REGNO (SET_SRC (set));
13265           offset = const0_rtx;
13266           base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
13267           off = INTVAL (offset);
13268
13269           if (GET_CODE (base) != REG || off < 0)
13270             continue;
13271           if (REGNO (base) != STACK_POINTER_REGNUM
13272               && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
13273             continue;
13274
13275           remove_insn (insn);
13276           continue;
13277         }
13278
13279       if (GET_CODE (pat) == PARALLEL
13280           && load_multiple_operation (pat, VOIDmode))
13281         {
13282           set = XVECEXP (pat, 0, 0);
13283           first = REGNO (SET_DEST (set));
13284           last = first + XVECLEN (pat, 0) - 1;
13285           offset = const0_rtx;
13286           base = eliminate_constant_term (XEXP (SET_SRC (set), 0), &offset);
13287           off = INTVAL (offset);
13288
13289           if (GET_CODE (base) != REG || off < 0)
13290             continue;
13291
13292           if (cfun_frame_layout.first_restore_gpr != -1
13293               && (cfun_frame_layout.first_restore_gpr < first
13294                   || cfun_frame_layout.last_restore_gpr > last))
13295             continue;
13296           if (REGNO (base) != STACK_POINTER_REGNUM
13297               && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
13298             continue;
13299           if (first > BASE_REGNUM || last < BASE_REGNUM)
13300             continue;
13301
13302           if (cfun_frame_layout.first_restore_gpr != -1)
13303             {
13304               rtx rpat = restore_gprs (base,
13305                                        off + (cfun_frame_layout.first_restore_gpr
13306                                               - first) * UNITS_PER_LONG,
13307                                        cfun_frame_layout.first_restore_gpr,
13308                                        cfun_frame_layout.last_restore_gpr);
13309
13310               /* Remove REG_CFA_RESTOREs for registers that we no
13311                  longer need to save.  */
13312               REG_NOTES (rpat) = REG_NOTES (insn);
13313               for (rtx *ptr = &REG_NOTES (rpat); *ptr; )
13314                 if (REG_NOTE_KIND (*ptr) == REG_CFA_RESTORE
13315                     && ((int) REGNO (XEXP (*ptr, 0))
13316                         < cfun_frame_layout.first_restore_gpr))
13317                   *ptr = XEXP (*ptr, 1);
13318                 else
13319                   ptr = &XEXP (*ptr, 1);
13320               new_insn = emit_insn_before (rpat, insn);
13321               RTX_FRAME_RELATED_P (new_insn) = 1;
13322               INSN_ADDRESSES_NEW (new_insn, -1);
13323             }
13324
13325           remove_insn (insn);
13326           continue;
13327         }
13328
13329       if (cfun_frame_layout.first_restore_gpr == -1
13330           && GET_CODE (pat) == SET
13331           && GENERAL_REG_P (SET_DEST (pat))
13332           && GET_CODE (SET_SRC (pat)) == MEM)
13333         {
13334           set = pat;
13335           first = REGNO (SET_DEST (set));
13336           offset = const0_rtx;
13337           base = eliminate_constant_term (XEXP (SET_SRC (set), 0), &offset);
13338           off = INTVAL (offset);
13339
13340           if (GET_CODE (base) != REG || off < 0)
13341             continue;
13342
13343           if (REGNO (base) != STACK_POINTER_REGNUM
13344               && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
13345             continue;
13346
13347           remove_insn (insn);
13348           continue;
13349         }
13350     }
13351 }
13352
13353 /* On z10 and later the dynamic branch prediction must see the
13354    backward jump within a certain windows.  If not it falls back to
13355    the static prediction.  This function rearranges the loop backward
13356    branch in a way which makes the static prediction always correct.
13357    The function returns true if it added an instruction.  */
13358 static bool
13359 s390_fix_long_loop_prediction (rtx_insn *insn)
13360 {
13361   rtx set = single_set (insn);
13362   rtx code_label, label_ref;
13363   rtx_insn *uncond_jump;
13364   rtx_insn *cur_insn;
13365   rtx tmp;
13366   int distance;
13367
13368   /* This will exclude branch on count and branch on index patterns
13369      since these are correctly statically predicted.  */
13370   if (!set
13371       || SET_DEST (set) != pc_rtx
13372       || GET_CODE (SET_SRC(set)) != IF_THEN_ELSE)
13373     return false;
13374
13375   /* Skip conditional returns.  */
13376   if (ANY_RETURN_P (XEXP (SET_SRC (set), 1))
13377       && XEXP (SET_SRC (set), 2) == pc_rtx)
13378     return false;
13379
13380   label_ref = (GET_CODE (XEXP (SET_SRC (set), 1)) == LABEL_REF ?
13381                XEXP (SET_SRC (set), 1) : XEXP (SET_SRC (set), 2));
13382
13383   gcc_assert (GET_CODE (label_ref) == LABEL_REF);
13384
13385   code_label = XEXP (label_ref, 0);
13386
13387   if (INSN_ADDRESSES (INSN_UID (code_label)) == -1
13388       || INSN_ADDRESSES (INSN_UID (insn)) == -1
13389       || (INSN_ADDRESSES (INSN_UID (insn))
13390           - INSN_ADDRESSES (INSN_UID (code_label)) < PREDICT_DISTANCE))
13391     return false;
13392
13393   for (distance = 0, cur_insn = PREV_INSN (insn);
13394        distance < PREDICT_DISTANCE - 6;
13395        distance += get_attr_length (cur_insn), cur_insn = PREV_INSN (cur_insn))
13396     if (!cur_insn || JUMP_P (cur_insn) || LABEL_P (cur_insn))
13397       return false;
13398
13399   rtx_code_label *new_label = gen_label_rtx ();
13400   uncond_jump = emit_jump_insn_after (
13401                   gen_rtx_SET (pc_rtx,
13402                                gen_rtx_LABEL_REF (VOIDmode, code_label)),
13403                   insn);
13404   emit_label_after (new_label, uncond_jump);
13405
13406   tmp = XEXP (SET_SRC (set), 1);
13407   XEXP (SET_SRC (set), 1) = XEXP (SET_SRC (set), 2);
13408   XEXP (SET_SRC (set), 2) = tmp;
13409   INSN_CODE (insn) = -1;
13410
13411   XEXP (label_ref, 0) = new_label;
13412   JUMP_LABEL (insn) = new_label;
13413   JUMP_LABEL (uncond_jump) = code_label;
13414
13415   return true;
13416 }
13417
13418 /* Returns 1 if INSN reads the value of REG for purposes not related
13419    to addressing of memory, and 0 otherwise.  */
13420 static int
13421 s390_non_addr_reg_read_p (rtx reg, rtx_insn *insn)
13422 {
13423   return reg_referenced_p (reg, PATTERN (insn))
13424     && !reg_used_in_mem_p (REGNO (reg), PATTERN (insn));
13425 }
13426
13427 /* Starting from INSN find_cond_jump looks downwards in the insn
13428    stream for a single jump insn which is the last user of the
13429    condition code set in INSN.  */
13430 static rtx_insn *
13431 find_cond_jump (rtx_insn *insn)
13432 {
13433   for (; insn; insn = NEXT_INSN (insn))
13434     {
13435       rtx ite, cc;
13436
13437       if (LABEL_P (insn))
13438         break;
13439
13440       if (!JUMP_P (insn))
13441         {
13442           if (reg_mentioned_p (gen_rtx_REG (CCmode, CC_REGNUM), insn))
13443             break;
13444           continue;
13445         }
13446
13447       /* This will be triggered by a return.  */
13448       if (GET_CODE (PATTERN (insn)) != SET)
13449         break;
13450
13451       gcc_assert (SET_DEST (PATTERN (insn)) == pc_rtx);
13452       ite = SET_SRC (PATTERN (insn));
13453
13454       if (GET_CODE (ite) != IF_THEN_ELSE)
13455         break;
13456
13457       cc = XEXP (XEXP (ite, 0), 0);
13458       if (!REG_P (cc) || !CC_REGNO_P (REGNO (cc)))
13459         break;
13460
13461       if (find_reg_note (insn, REG_DEAD, cc))
13462         return insn;
13463       break;
13464     }
13465
13466   return NULL;
13467 }
13468
13469 /* Swap the condition in COND and the operands in OP0 and OP1 so that
13470    the semantics does not change.  If NULL_RTX is passed as COND the
13471    function tries to find the conditional jump starting with INSN.  */
13472 static void
13473 s390_swap_cmp (rtx cond, rtx *op0, rtx *op1, rtx_insn *insn)
13474 {
13475   rtx tmp = *op0;
13476
13477   if (cond == NULL_RTX)
13478     {
13479       rtx_insn *jump = find_cond_jump (NEXT_INSN (insn));
13480       rtx set = jump ? single_set (jump) : NULL_RTX;
13481
13482       if (set == NULL_RTX)
13483         return;
13484
13485       cond = XEXP (SET_SRC (set), 0);
13486     }
13487
13488   *op0 = *op1;
13489   *op1 = tmp;
13490   PUT_CODE (cond, swap_condition (GET_CODE (cond)));
13491 }
13492
13493 /* On z10, instructions of the compare-and-branch family have the
13494    property to access the register occurring as second operand with
13495    its bits complemented.  If such a compare is grouped with a second
13496    instruction that accesses the same register non-complemented, and
13497    if that register's value is delivered via a bypass, then the
13498    pipeline recycles, thereby causing significant performance decline.
13499    This function locates such situations and exchanges the two
13500    operands of the compare.  The function return true whenever it
13501    added an insn.  */
13502 static bool
13503 s390_z10_optimize_cmp (rtx_insn *insn)
13504 {
13505   rtx_insn *prev_insn, *next_insn;
13506   bool insn_added_p = false;
13507   rtx cond, *op0, *op1;
13508
13509   if (GET_CODE (PATTERN (insn)) == PARALLEL)
13510     {
13511       /* Handle compare and branch and branch on count
13512          instructions.  */
13513       rtx pattern = single_set (insn);
13514
13515       if (!pattern
13516           || SET_DEST (pattern) != pc_rtx
13517           || GET_CODE (SET_SRC (pattern)) != IF_THEN_ELSE)
13518         return false;
13519
13520       cond = XEXP (SET_SRC (pattern), 0);
13521       op0 = &XEXP (cond, 0);
13522       op1 = &XEXP (cond, 1);
13523     }
13524   else if (GET_CODE (PATTERN (insn)) == SET)
13525     {
13526       rtx src, dest;
13527
13528       /* Handle normal compare instructions.  */
13529       src = SET_SRC (PATTERN (insn));
13530       dest = SET_DEST (PATTERN (insn));
13531
13532       if (!REG_P (dest)
13533           || !CC_REGNO_P (REGNO (dest))
13534           || GET_CODE (src) != COMPARE)
13535         return false;
13536
13537       /* s390_swap_cmp will try to find the conditional
13538          jump when passing NULL_RTX as condition.  */
13539       cond = NULL_RTX;
13540       op0 = &XEXP (src, 0);
13541       op1 = &XEXP (src, 1);
13542     }
13543   else
13544     return false;
13545
13546   if (!REG_P (*op0) || !REG_P (*op1))
13547     return false;
13548
13549   if (GET_MODE_CLASS (GET_MODE (*op0)) != MODE_INT)
13550     return false;
13551
13552   /* Swap the COMPARE arguments and its mask if there is a
13553      conflicting access in the previous insn.  */
13554   prev_insn = prev_active_insn (insn);
13555   if (prev_insn != NULL_RTX && INSN_P (prev_insn)
13556       && reg_referenced_p (*op1, PATTERN (prev_insn)))
13557     s390_swap_cmp (cond, op0, op1, insn);
13558
13559   /* Check if there is a conflict with the next insn. If there
13560      was no conflict with the previous insn, then swap the
13561      COMPARE arguments and its mask.  If we already swapped
13562      the operands, or if swapping them would cause a conflict
13563      with the previous insn, issue a NOP after the COMPARE in
13564      order to separate the two instuctions.  */
13565   next_insn = next_active_insn (insn);
13566   if (next_insn != NULL_RTX && INSN_P (next_insn)
13567       && s390_non_addr_reg_read_p (*op1, next_insn))
13568     {
13569       if (prev_insn != NULL_RTX && INSN_P (prev_insn)
13570           && s390_non_addr_reg_read_p (*op0, prev_insn))
13571         {
13572           if (REGNO (*op1) == 0)
13573             emit_insn_after (gen_nop1 (), insn);
13574           else
13575             emit_insn_after (gen_nop (), insn);
13576           insn_added_p = true;
13577         }
13578       else
13579         s390_swap_cmp (cond, op0, op1, insn);
13580     }
13581   return insn_added_p;
13582 }
13583
13584 /* Number of INSNs to be scanned backward in the last BB of the loop
13585    and forward in the first BB of the loop.  This usually should be a
13586    bit more than the number of INSNs which could go into one
13587    group.  */
13588 #define S390_OSC_SCAN_INSN_NUM 5
13589
13590 /* Scan LOOP for static OSC collisions and return true if a osc_break
13591    should be issued for this loop.  */
13592 static bool
13593 s390_adjust_loop_scan_osc (struct loop* loop)
13594
13595 {
13596   HARD_REG_SET modregs, newregs;
13597   rtx_insn *insn, *store_insn = NULL;
13598   rtx set;
13599   struct s390_address addr_store, addr_load;
13600   subrtx_iterator::array_type array;
13601   int insn_count;
13602
13603   CLEAR_HARD_REG_SET (modregs);
13604
13605   insn_count = 0;
13606   FOR_BB_INSNS_REVERSE (loop->latch, insn)
13607     {
13608       if (!INSN_P (insn) || INSN_CODE (insn) <= 0)
13609         continue;
13610
13611       insn_count++;
13612       if (insn_count > S390_OSC_SCAN_INSN_NUM)
13613         return false;
13614
13615       find_all_hard_reg_sets (insn, &newregs, true);
13616       IOR_HARD_REG_SET (modregs, newregs);
13617
13618       set = single_set (insn);
13619       if (!set)
13620         continue;
13621
13622       if (MEM_P (SET_DEST (set))
13623           && s390_decompose_address (XEXP (SET_DEST (set), 0), &addr_store))
13624         {
13625           store_insn = insn;
13626           break;
13627         }
13628     }
13629
13630   if (store_insn == NULL_RTX)
13631     return false;
13632
13633   insn_count = 0;
13634   FOR_BB_INSNS (loop->header, insn)
13635     {
13636       if (!INSN_P (insn) || INSN_CODE (insn) <= 0)
13637         continue;
13638
13639       if (insn == store_insn)
13640         return false;
13641
13642       insn_count++;
13643       if (insn_count > S390_OSC_SCAN_INSN_NUM)
13644         return false;
13645
13646       find_all_hard_reg_sets (insn, &newregs, true);
13647       IOR_HARD_REG_SET (modregs, newregs);
13648
13649       set = single_set (insn);
13650       if (!set)
13651         continue;
13652
13653       /* An intermediate store disrupts static OSC checking
13654          anyway.  */
13655       if (MEM_P (SET_DEST (set))
13656           && s390_decompose_address (XEXP (SET_DEST (set), 0), NULL))
13657         return false;
13658
13659       FOR_EACH_SUBRTX (iter, array, SET_SRC (set), NONCONST)
13660         if (MEM_P (*iter)
13661             && s390_decompose_address (XEXP (*iter, 0), &addr_load)
13662             && rtx_equal_p (addr_load.base, addr_store.base)
13663             && rtx_equal_p (addr_load.indx, addr_store.indx)
13664             && rtx_equal_p (addr_load.disp, addr_store.disp))
13665           {
13666             if ((addr_load.base != NULL_RTX
13667                  && TEST_HARD_REG_BIT (modregs, REGNO (addr_load.base)))
13668                 || (addr_load.indx != NULL_RTX
13669                     && TEST_HARD_REG_BIT (modregs, REGNO (addr_load.indx))))
13670               return true;
13671           }
13672     }
13673   return false;
13674 }
13675
13676 /* Look for adjustments which can be done on simple innermost
13677    loops.  */
13678 static void
13679 s390_adjust_loops ()
13680 {
13681   struct loop *loop = NULL;
13682
13683   df_analyze ();
13684   compute_bb_for_insn ();
13685
13686   /* Find the loops.  */
13687   loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
13688
13689   FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST)
13690     {
13691       if (dump_file)
13692         {
13693           flow_loop_dump (loop, dump_file, NULL, 0);
13694           fprintf (dump_file, ";;  OSC loop scan Loop: ");
13695         }
13696       if (loop->latch == NULL
13697           || pc_set (BB_END (loop->latch)) == NULL_RTX
13698           || !s390_adjust_loop_scan_osc (loop))
13699         {
13700           if (dump_file)
13701             {
13702               if (loop->latch == NULL)
13703                 fprintf (dump_file, " muliple backward jumps\n");
13704               else
13705                 {
13706                   fprintf (dump_file, " header insn: %d latch insn: %d ",
13707                            INSN_UID (BB_HEAD (loop->header)),
13708                            INSN_UID (BB_END (loop->latch)));
13709                   if (pc_set (BB_END (loop->latch)) == NULL_RTX)
13710                     fprintf (dump_file, " loop does not end with jump\n");
13711                   else
13712                     fprintf (dump_file, " not instrumented\n");
13713                 }
13714             }
13715         }
13716       else
13717         {
13718           rtx_insn *new_insn;
13719
13720           if (dump_file)
13721             fprintf (dump_file, " adding OSC break insn: ");
13722           new_insn = emit_insn_before (gen_osc_break (),
13723                                        BB_END (loop->latch));
13724           INSN_ADDRESSES_NEW (new_insn, -1);
13725         }
13726     }
13727
13728   loop_optimizer_finalize ();
13729
13730   df_finish_pass (false);
13731 }
13732
13733 /* Perform machine-dependent processing.  */
13734
13735 static void
13736 s390_reorg (void)
13737 {
13738   bool pool_overflow = false;
13739   int hw_before, hw_after;
13740
13741   if (s390_tune == PROCESSOR_2964_Z13)
13742     s390_adjust_loops ();
13743
13744   /* Make sure all splits have been performed; splits after
13745      machine_dependent_reorg might confuse insn length counts.  */
13746   split_all_insns_noflow ();
13747
13748   /* Install the main literal pool and the associated base
13749      register load insns.
13750
13751      In addition, there are two problematic situations we need
13752      to correct:
13753
13754      - the literal pool might be > 4096 bytes in size, so that
13755        some of its elements cannot be directly accessed
13756
13757      - a branch target might be > 64K away from the branch, so that
13758        it is not possible to use a PC-relative instruction.
13759
13760      To fix those, we split the single literal pool into multiple
13761      pool chunks, reloading the pool base register at various
13762      points throughout the function to ensure it always points to
13763      the pool chunk the following code expects, and / or replace
13764      PC-relative branches by absolute branches.
13765
13766      However, the two problems are interdependent: splitting the
13767      literal pool can move a branch further away from its target,
13768      causing the 64K limit to overflow, and on the other hand,
13769      replacing a PC-relative branch by an absolute branch means
13770      we need to put the branch target address into the literal
13771      pool, possibly causing it to overflow.
13772
13773      So, we loop trying to fix up both problems until we manage
13774      to satisfy both conditions at the same time.  Note that the
13775      loop is guaranteed to terminate as every pass of the loop
13776      strictly decreases the total number of PC-relative branches
13777      in the function.  (This is not completely true as there
13778      might be branch-over-pool insns introduced by chunkify_start.
13779      Those never need to be split however.)  */
13780
13781   for (;;)
13782     {
13783       struct constant_pool *pool = NULL;
13784
13785       /* Collect the literal pool.  */
13786       if (!pool_overflow)
13787         {
13788           pool = s390_mainpool_start ();
13789           if (!pool)
13790             pool_overflow = true;
13791         }
13792
13793       /* If literal pool overflowed, start to chunkify it.  */
13794       if (pool_overflow)
13795         pool = s390_chunkify_start ();
13796
13797       /* Split out-of-range branches.  If this has created new
13798          literal pool entries, cancel current chunk list and
13799          recompute it.  zSeries machines have large branch
13800          instructions, so we never need to split a branch.  */
13801       if (!TARGET_CPU_ZARCH && s390_split_branches ())
13802         {
13803           if (pool_overflow)
13804             s390_chunkify_cancel (pool);
13805           else
13806             s390_mainpool_cancel (pool);
13807
13808           continue;
13809         }
13810
13811       /* If we made it up to here, both conditions are satisfied.
13812          Finish up literal pool related changes.  */
13813       if (pool_overflow)
13814         s390_chunkify_finish (pool);
13815       else
13816         s390_mainpool_finish (pool);
13817
13818       /* We're done splitting branches.  */
13819       cfun->machine->split_branches_pending_p = false;
13820       break;
13821     }
13822
13823   /* Generate out-of-pool execute target insns.  */
13824   if (TARGET_CPU_ZARCH)
13825     {
13826       rtx_insn *insn, *target;
13827       rtx label;
13828
13829       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
13830         {
13831           label = s390_execute_label (insn);
13832           if (!label)
13833             continue;
13834
13835           gcc_assert (label != const0_rtx);
13836
13837           target = emit_label (XEXP (label, 0));
13838           INSN_ADDRESSES_NEW (target, -1);
13839
13840           target = emit_insn (s390_execute_target (insn));
13841           INSN_ADDRESSES_NEW (target, -1);
13842         }
13843     }
13844
13845   /* Try to optimize prologue and epilogue further.  */
13846   s390_optimize_prologue ();
13847
13848   /* Walk over the insns and do some >=z10 specific changes.  */
13849   if (s390_tune >= PROCESSOR_2097_Z10)
13850     {
13851       rtx_insn *insn;
13852       bool insn_added_p = false;
13853
13854       /* The insn lengths and addresses have to be up to date for the
13855          following manipulations.  */
13856       shorten_branches (get_insns ());
13857
13858       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
13859         {
13860           if (!INSN_P (insn) || INSN_CODE (insn) <= 0)
13861             continue;
13862
13863           if (JUMP_P (insn))
13864             insn_added_p |= s390_fix_long_loop_prediction (insn);
13865
13866           if ((GET_CODE (PATTERN (insn)) == PARALLEL
13867                || GET_CODE (PATTERN (insn)) == SET)
13868               && s390_tune == PROCESSOR_2097_Z10)
13869             insn_added_p |= s390_z10_optimize_cmp (insn);
13870         }
13871
13872       /* Adjust branches if we added new instructions.  */
13873       if (insn_added_p)
13874         shorten_branches (get_insns ());
13875     }
13876
13877   s390_function_num_hotpatch_hw (current_function_decl, &hw_before, &hw_after);
13878   if (hw_after > 0)
13879     {
13880       rtx_insn *insn;
13881
13882       /* Insert NOPs for hotpatching. */
13883       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
13884         /* Emit NOPs
13885             1. inside the area covered by debug information to allow setting
13886                breakpoints at the NOPs,
13887             2. before any insn which results in an asm instruction,
13888             3. before in-function labels to avoid jumping to the NOPs, for
13889                example as part of a loop,
13890             4. before any barrier in case the function is completely empty
13891                (__builtin_unreachable ()) and has neither internal labels nor
13892                active insns.
13893         */
13894         if (active_insn_p (insn) || BARRIER_P (insn) || LABEL_P (insn))
13895           break;
13896       /* Output a series of NOPs before the first active insn.  */
13897       while (insn && hw_after > 0)
13898         {
13899           if (hw_after >= 3 && TARGET_CPU_ZARCH)
13900             {
13901               emit_insn_before (gen_nop_6_byte (), insn);
13902               hw_after -= 3;
13903             }
13904           else if (hw_after >= 2)
13905             {
13906               emit_insn_before (gen_nop_4_byte (), insn);
13907               hw_after -= 2;
13908             }
13909           else
13910             {
13911               emit_insn_before (gen_nop_2_byte (), insn);
13912               hw_after -= 1;
13913             }
13914         }
13915     }
13916 }
13917
13918 /* Return true if INSN is a fp load insn writing register REGNO.  */
13919 static inline bool
13920 s390_fpload_toreg (rtx_insn *insn, unsigned int regno)
13921 {
13922   rtx set;
13923   enum attr_type flag = s390_safe_attr_type (insn);
13924
13925   if (flag != TYPE_FLOADSF && flag != TYPE_FLOADDF)
13926     return false;
13927
13928   set = single_set (insn);
13929
13930   if (set == NULL_RTX)
13931     return false;
13932
13933   if (!REG_P (SET_DEST (set)) || !MEM_P (SET_SRC (set)))
13934     return false;
13935
13936   if (REGNO (SET_DEST (set)) != regno)
13937     return false;
13938
13939   return true;
13940 }
13941
13942 /* This value describes the distance to be avoided between an
13943    aritmetic fp instruction and an fp load writing the same register.
13944    Z10_EARLYLOAD_DISTANCE - 1 as well as Z10_EARLYLOAD_DISTANCE + 1 is
13945    fine but the exact value has to be avoided. Otherwise the FP
13946    pipeline will throw an exception causing a major penalty.  */
13947 #define Z10_EARLYLOAD_DISTANCE 7
13948
13949 /* Rearrange the ready list in order to avoid the situation described
13950    for Z10_EARLYLOAD_DISTANCE.  A problematic load instruction is
13951    moved to the very end of the ready list.  */
13952 static void
13953 s390_z10_prevent_earlyload_conflicts (rtx_insn **ready, int *nready_p)
13954 {
13955   unsigned int regno;
13956   int nready = *nready_p;
13957   rtx_insn *tmp;
13958   int i;
13959   rtx_insn *insn;
13960   rtx set;
13961   enum attr_type flag;
13962   int distance;
13963
13964   /* Skip DISTANCE - 1 active insns.  */
13965   for (insn = last_scheduled_insn, distance = Z10_EARLYLOAD_DISTANCE - 1;
13966        distance > 0 && insn != NULL_RTX;
13967        distance--, insn = prev_active_insn (insn))
13968     if (CALL_P (insn) || JUMP_P (insn))
13969       return;
13970
13971   if (insn == NULL_RTX)
13972     return;
13973
13974   set = single_set (insn);
13975
13976   if (set == NULL_RTX || !REG_P (SET_DEST (set))
13977       || GET_MODE_CLASS (GET_MODE (SET_DEST (set))) != MODE_FLOAT)
13978     return;
13979
13980   flag = s390_safe_attr_type (insn);
13981
13982   if (flag == TYPE_FLOADSF || flag == TYPE_FLOADDF)
13983     return;
13984
13985   regno = REGNO (SET_DEST (set));
13986   i = nready - 1;
13987
13988   while (!s390_fpload_toreg (ready[i], regno) && i > 0)
13989     i--;
13990
13991   if (!i)
13992     return;
13993
13994   tmp = ready[i];
13995   memmove (&ready[1], &ready[0], sizeof (rtx_insn *) * i);
13996   ready[0] = tmp;
13997 }
13998
13999
14000 /* The s390_sched_state variable tracks the state of the current or
14001    the last instruction group.
14002
14003    0,1,2 number of instructions scheduled in the current group
14004    3     the last group is complete - normal insns
14005    4     the last group was a cracked/expanded insn */
14006
14007 static int s390_sched_state;
14008
14009 #define S390_SCHED_STATE_NORMAL  3
14010 #define S390_SCHED_STATE_CRACKED 4
14011
14012 #define S390_SCHED_ATTR_MASK_CRACKED    0x1
14013 #define S390_SCHED_ATTR_MASK_EXPANDED   0x2
14014 #define S390_SCHED_ATTR_MASK_ENDGROUP   0x4
14015 #define S390_SCHED_ATTR_MASK_GROUPALONE 0x8
14016
14017 static unsigned int
14018 s390_get_sched_attrmask (rtx_insn *insn)
14019 {
14020   unsigned int mask = 0;
14021
14022   switch (s390_tune)
14023     {
14024     case PROCESSOR_2827_ZEC12:
14025       if (get_attr_zEC12_cracked (insn))
14026         mask |= S390_SCHED_ATTR_MASK_CRACKED;
14027       if (get_attr_zEC12_expanded (insn))
14028         mask |= S390_SCHED_ATTR_MASK_EXPANDED;
14029       if (get_attr_zEC12_endgroup (insn))
14030         mask |= S390_SCHED_ATTR_MASK_ENDGROUP;
14031       if (get_attr_zEC12_groupalone (insn))
14032         mask |= S390_SCHED_ATTR_MASK_GROUPALONE;
14033       break;
14034     case PROCESSOR_2964_Z13:
14035       if (get_attr_z13_cracked (insn))
14036         mask |= S390_SCHED_ATTR_MASK_CRACKED;
14037       if (get_attr_z13_expanded (insn))
14038         mask |= S390_SCHED_ATTR_MASK_EXPANDED;
14039       if (get_attr_z13_endgroup (insn))
14040         mask |= S390_SCHED_ATTR_MASK_ENDGROUP;
14041       if (get_attr_z13_groupalone (insn))
14042         mask |= S390_SCHED_ATTR_MASK_GROUPALONE;
14043       break;
14044     default:
14045       gcc_unreachable ();
14046     }
14047   return mask;
14048 }
14049
14050 static unsigned int
14051 s390_get_unit_mask (rtx_insn *insn, int *units)
14052 {
14053   unsigned int mask = 0;
14054
14055   switch (s390_tune)
14056     {
14057     case PROCESSOR_2964_Z13:
14058       *units = 3;
14059       if (get_attr_z13_unit_lsu (insn))
14060         mask |= 1 << 0;
14061       if (get_attr_z13_unit_fxu (insn))
14062         mask |= 1 << 1;
14063       if (get_attr_z13_unit_vfu (insn))
14064         mask |= 1 << 2;
14065       break;
14066     default:
14067       gcc_unreachable ();
14068     }
14069   return mask;
14070 }
14071
14072 /* Return the scheduling score for INSN.  The higher the score the
14073    better.  The score is calculated from the OOO scheduling attributes
14074    of INSN and the scheduling state s390_sched_state.  */
14075 static int
14076 s390_sched_score (rtx_insn *insn)
14077 {
14078   unsigned int mask = s390_get_sched_attrmask (insn);
14079   int score = 0;
14080
14081   switch (s390_sched_state)
14082     {
14083     case 0:
14084       /* Try to put insns into the first slot which would otherwise
14085          break a group.  */
14086       if ((mask & S390_SCHED_ATTR_MASK_CRACKED) != 0
14087           || (mask & S390_SCHED_ATTR_MASK_EXPANDED) != 0)
14088         score += 5;
14089       if ((mask & S390_SCHED_ATTR_MASK_GROUPALONE) != 0)
14090         score += 10;
14091       /* fallthrough */
14092     case 1:
14093       /* Prefer not cracked insns while trying to put together a
14094          group.  */
14095       if ((mask & S390_SCHED_ATTR_MASK_CRACKED) == 0
14096           && (mask & S390_SCHED_ATTR_MASK_EXPANDED) == 0
14097           && (mask & S390_SCHED_ATTR_MASK_GROUPALONE) == 0)
14098         score += 10;
14099       if ((mask & S390_SCHED_ATTR_MASK_ENDGROUP) == 0)
14100         score += 5;
14101       break;
14102     case 2:
14103       /* Prefer not cracked insns while trying to put together a
14104          group.  */
14105       if ((mask & S390_SCHED_ATTR_MASK_CRACKED) == 0
14106           && (mask & S390_SCHED_ATTR_MASK_EXPANDED) == 0
14107           && (mask & S390_SCHED_ATTR_MASK_GROUPALONE) == 0)
14108         score += 10;
14109       /* Prefer endgroup insns in the last slot.  */
14110       if ((mask & S390_SCHED_ATTR_MASK_ENDGROUP) != 0)
14111         score += 10;
14112       break;
14113     case S390_SCHED_STATE_NORMAL:
14114       /* Prefer not cracked insns if the last was not cracked.  */
14115       if ((mask & S390_SCHED_ATTR_MASK_CRACKED) == 0
14116           && (mask & S390_SCHED_ATTR_MASK_EXPANDED) == 0)
14117         score += 5;
14118       if ((mask & S390_SCHED_ATTR_MASK_GROUPALONE) != 0)
14119         score += 10;
14120       break;
14121     case S390_SCHED_STATE_CRACKED:
14122       /* Try to keep cracked insns together to prevent them from
14123          interrupting groups.  */
14124       if ((mask & S390_SCHED_ATTR_MASK_CRACKED) != 0
14125           || (mask & S390_SCHED_ATTR_MASK_EXPANDED) != 0)
14126         score += 5;
14127       break;
14128     }
14129
14130   if (s390_tune == PROCESSOR_2964_Z13)
14131     {
14132       int units, i;
14133       unsigned unit_mask, m = 1;
14134
14135       unit_mask = s390_get_unit_mask (insn, &units);
14136       gcc_assert (units <= MAX_SCHED_UNITS);
14137
14138       /* Add a score in range 0..MAX_SCHED_MIX_SCORE depending on how long
14139          ago the last insn of this unit type got scheduled.  This is
14140          supposed to help providing a proper instruction mix to the
14141          CPU.  */
14142       for (i = 0; i < units; i++, m <<= 1)
14143         if (m & unit_mask)
14144           score += (last_scheduled_unit_distance[i] * MAX_SCHED_MIX_SCORE /
14145                     MAX_SCHED_MIX_DISTANCE);
14146     }
14147   return score;
14148 }
14149
14150 /* This function is called via hook TARGET_SCHED_REORDER before
14151    issuing one insn from list READY which contains *NREADYP entries.
14152    For target z10 it reorders load instructions to avoid early load
14153    conflicts in the floating point pipeline  */
14154 static int
14155 s390_sched_reorder (FILE *file, int verbose,
14156                     rtx_insn **ready, int *nreadyp, int clock ATTRIBUTE_UNUSED)
14157 {
14158   if (s390_tune == PROCESSOR_2097_Z10
14159       && reload_completed
14160       && *nreadyp > 1)
14161     s390_z10_prevent_earlyload_conflicts (ready, nreadyp);
14162
14163   if (s390_tune >= PROCESSOR_2827_ZEC12
14164       && reload_completed
14165       && *nreadyp > 1)
14166     {
14167       int i;
14168       int last_index = *nreadyp - 1;
14169       int max_index = -1;
14170       int max_score = -1;
14171       rtx_insn *tmp;
14172
14173       /* Just move the insn with the highest score to the top (the
14174          end) of the list.  A full sort is not needed since a conflict
14175          in the hazard recognition cannot happen.  So the top insn in
14176          the ready list will always be taken.  */
14177       for (i = last_index; i >= 0; i--)
14178         {
14179           int score;
14180
14181           if (recog_memoized (ready[i]) < 0)
14182             continue;
14183
14184           score = s390_sched_score (ready[i]);
14185           if (score > max_score)
14186             {
14187               max_score = score;
14188               max_index = i;
14189             }
14190         }
14191
14192       if (max_index != -1)
14193         {
14194           if (max_index != last_index)
14195             {
14196               tmp = ready[max_index];
14197               ready[max_index] = ready[last_index];
14198               ready[last_index] = tmp;
14199
14200               if (verbose > 5)
14201                 fprintf (file,
14202                          ";;\t\tBACKEND: move insn %d to the top of list\n",
14203                          INSN_UID (ready[last_index]));
14204             }
14205           else if (verbose > 5)
14206             fprintf (file,
14207                      ";;\t\tBACKEND: best insn %d already on top\n",
14208                      INSN_UID (ready[last_index]));
14209         }
14210
14211       if (verbose > 5)
14212         {
14213           fprintf (file, "ready list ooo attributes - sched state: %d\n",
14214                    s390_sched_state);
14215
14216           for (i = last_index; i >= 0; i--)
14217             {
14218               unsigned int sched_mask;
14219               rtx_insn *insn = ready[i];
14220
14221               if (recog_memoized (insn) < 0)
14222                 continue;
14223
14224               sched_mask = s390_get_sched_attrmask (insn);
14225               fprintf (file, ";;\t\tBACKEND: insn %d score: %d: ",
14226                        INSN_UID (insn),
14227                        s390_sched_score (insn));
14228 #define PRINT_SCHED_ATTR(M, ATTR) fprintf (file, "%s ",\
14229                                            ((M) & sched_mask) ? #ATTR : "");
14230               PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_CRACKED, cracked);
14231               PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_EXPANDED, expanded);
14232               PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_ENDGROUP, endgroup);
14233               PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_GROUPALONE, groupalone);
14234 #undef PRINT_SCHED_ATTR
14235               if (s390_tune == PROCESSOR_2964_Z13)
14236                 {
14237                   unsigned int unit_mask, m = 1;
14238                   int units, j;
14239
14240                   unit_mask  = s390_get_unit_mask (insn, &units);
14241                   fprintf (file, "(units:");
14242                   for (j = 0; j < units; j++, m <<= 1)
14243                     if (m & unit_mask)
14244                       fprintf (file, " u%d", j);
14245                   fprintf (file, ")");
14246                 }
14247               fprintf (file, "\n");
14248             }
14249         }
14250     }
14251
14252   return s390_issue_rate ();
14253 }
14254
14255
14256 /* This function is called via hook TARGET_SCHED_VARIABLE_ISSUE after
14257    the scheduler has issued INSN.  It stores the last issued insn into
14258    last_scheduled_insn in order to make it available for
14259    s390_sched_reorder.  */
14260 static int
14261 s390_sched_variable_issue (FILE *file, int verbose, rtx_insn *insn, int more)
14262 {
14263   last_scheduled_insn = insn;
14264
14265   if (s390_tune >= PROCESSOR_2827_ZEC12
14266       && reload_completed
14267       && recog_memoized (insn) >= 0)
14268     {
14269       unsigned int mask = s390_get_sched_attrmask (insn);
14270
14271       if ((mask & S390_SCHED_ATTR_MASK_CRACKED) != 0
14272           || (mask & S390_SCHED_ATTR_MASK_EXPANDED) != 0)
14273         s390_sched_state = S390_SCHED_STATE_CRACKED;
14274       else if ((mask & S390_SCHED_ATTR_MASK_ENDGROUP) != 0
14275                || (mask & S390_SCHED_ATTR_MASK_GROUPALONE) != 0)
14276         s390_sched_state = S390_SCHED_STATE_NORMAL;
14277       else
14278         {
14279           /* Only normal insns are left (mask == 0).  */
14280           switch (s390_sched_state)
14281             {
14282             case 0:
14283             case 1:
14284             case 2:
14285             case S390_SCHED_STATE_NORMAL:
14286               if (s390_sched_state == S390_SCHED_STATE_NORMAL)
14287                 s390_sched_state = 1;
14288               else
14289                 s390_sched_state++;
14290
14291               break;
14292             case S390_SCHED_STATE_CRACKED:
14293               s390_sched_state = S390_SCHED_STATE_NORMAL;
14294               break;
14295             }
14296         }
14297
14298       if (s390_tune == PROCESSOR_2964_Z13)
14299         {
14300           int units, i;
14301           unsigned unit_mask, m = 1;
14302
14303           unit_mask = s390_get_unit_mask (insn, &units);
14304           gcc_assert (units <= MAX_SCHED_UNITS);
14305
14306           for (i = 0; i < units; i++, m <<= 1)
14307             if (m & unit_mask)
14308               last_scheduled_unit_distance[i] = 0;
14309             else if (last_scheduled_unit_distance[i] < MAX_SCHED_MIX_DISTANCE)
14310               last_scheduled_unit_distance[i]++;
14311         }
14312
14313       if (verbose > 5)
14314         {
14315           unsigned int sched_mask;
14316
14317           sched_mask = s390_get_sched_attrmask (insn);
14318
14319           fprintf (file, ";;\t\tBACKEND: insn %d: ", INSN_UID (insn));
14320 #define PRINT_SCHED_ATTR(M, ATTR) fprintf (file, "%s ", ((M) & sched_mask) ? #ATTR : "");
14321           PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_CRACKED, cracked);
14322           PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_EXPANDED, expanded);
14323           PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_ENDGROUP, endgroup);
14324           PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_GROUPALONE, groupalone);
14325 #undef PRINT_SCHED_ATTR
14326
14327           if (s390_tune == PROCESSOR_2964_Z13)
14328             {
14329               unsigned int unit_mask, m = 1;
14330               int units, j;
14331
14332               unit_mask  = s390_get_unit_mask (insn, &units);
14333               fprintf (file, "(units:");
14334               for (j = 0; j < units; j++, m <<= 1)
14335                 if (m & unit_mask)
14336                   fprintf (file, " %d", j);
14337               fprintf (file, ")");
14338             }
14339           fprintf (file, " sched state: %d\n", s390_sched_state);
14340
14341           if (s390_tune == PROCESSOR_2964_Z13)
14342             {
14343               int units, j;
14344
14345               s390_get_unit_mask (insn, &units);
14346
14347               fprintf (file, ";;\t\tBACKEND: units unused for: ");
14348               for (j = 0; j < units; j++)
14349                 fprintf (file, "%d:%d ", j, last_scheduled_unit_distance[j]);
14350               fprintf (file, "\n");
14351             }
14352         }
14353     }
14354
14355   if (GET_CODE (PATTERN (insn)) != USE
14356       && GET_CODE (PATTERN (insn)) != CLOBBER)
14357     return more - 1;
14358   else
14359     return more;
14360 }
14361
14362 static void
14363 s390_sched_init (FILE *file ATTRIBUTE_UNUSED,
14364                  int verbose ATTRIBUTE_UNUSED,
14365                  int max_ready ATTRIBUTE_UNUSED)
14366 {
14367   last_scheduled_insn = NULL;
14368   memset (last_scheduled_unit_distance, 0, MAX_SCHED_UNITS * sizeof (int));
14369   s390_sched_state = 0;
14370 }
14371
14372 /* This target hook implementation for TARGET_LOOP_UNROLL_ADJUST calculates
14373    a new number struct loop *loop should be unrolled if tuned for cpus with
14374    a built-in stride prefetcher.
14375    The loop is analyzed for memory accesses by calling check_dpu for
14376    each rtx of the loop. Depending on the loop_depth and the amount of
14377    memory accesses a new number <=nunroll is returned to improve the
14378    behavior of the hardware prefetch unit.  */
14379 static unsigned
14380 s390_loop_unroll_adjust (unsigned nunroll, struct loop *loop)
14381 {
14382   basic_block *bbs;
14383   rtx_insn *insn;
14384   unsigned i;
14385   unsigned mem_count = 0;
14386
14387   if (s390_tune < PROCESSOR_2097_Z10)
14388     return nunroll;
14389
14390   /* Count the number of memory references within the loop body.  */
14391   bbs = get_loop_body (loop);
14392   subrtx_iterator::array_type array;
14393   for (i = 0; i < loop->num_nodes; i++)
14394     FOR_BB_INSNS (bbs[i], insn)
14395       if (INSN_P (insn) && INSN_CODE (insn) != -1)
14396         FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
14397           if (MEM_P (*iter))
14398             mem_count += 1;
14399   free (bbs);
14400
14401   /* Prevent division by zero, and we do not need to adjust nunroll in this case.  */
14402   if (mem_count == 0)
14403     return nunroll;
14404
14405   switch (loop_depth(loop))
14406     {
14407     case 1:
14408       return MIN (nunroll, 28 / mem_count);
14409     case 2:
14410       return MIN (nunroll, 22 / mem_count);
14411     default:
14412       return MIN (nunroll, 16 / mem_count);
14413     }
14414 }
14415
14416 /* Restore the current options.  This is a hook function and also called
14417    internally.  */
14418
14419 static void
14420 s390_function_specific_restore (struct gcc_options *opts,
14421                                 struct cl_target_option *ptr ATTRIBUTE_UNUSED)
14422 {
14423   opts->x_s390_cost_pointer = (long)processor_table[opts->x_s390_tune].cost;
14424 }
14425
14426 static void
14427 s390_option_override_internal (bool main_args_p,
14428                                struct gcc_options *opts,
14429                                const struct gcc_options *opts_set)
14430 {
14431   const char *prefix;
14432   const char *suffix;
14433
14434   /* Set up prefix/suffix so the error messages refer to either the command
14435      line argument, or the attribute(target).  */
14436   if (main_args_p)
14437     {
14438       prefix = "-m";
14439       suffix = "";
14440     }
14441   else
14442     {
14443       prefix = "option(\"";
14444       suffix = "\")";
14445     }
14446
14447
14448   /* Architecture mode defaults according to ABI.  */
14449   if (!(opts_set->x_target_flags & MASK_ZARCH))
14450     {
14451       if (TARGET_64BIT)
14452         opts->x_target_flags |= MASK_ZARCH;
14453       else
14454         opts->x_target_flags &= ~MASK_ZARCH;
14455     }
14456
14457   /* Set the march default in case it hasn't been specified on cmdline.  */
14458   if (!opts_set->x_s390_arch)
14459     opts->x_s390_arch = PROCESSOR_2064_Z900;
14460   else if (opts->x_s390_arch == PROCESSOR_9672_G5
14461            || opts->x_s390_arch == PROCESSOR_9672_G6)
14462     warning (OPT_Wdeprecated, "%sarch=%s%s is deprecated and will be removed "
14463              "in future releases; use at least %sarch=z900%s",
14464              prefix, opts->x_s390_arch == PROCESSOR_9672_G5 ? "g5" : "g6",
14465              suffix, prefix, suffix);
14466
14467   opts->x_s390_arch_flags = processor_flags_table[(int) opts->x_s390_arch];
14468
14469   /* Determine processor to tune for.  */
14470   if (!opts_set->x_s390_tune)
14471     opts->x_s390_tune = opts->x_s390_arch;
14472   else if (opts->x_s390_tune == PROCESSOR_9672_G5
14473            || opts->x_s390_tune == PROCESSOR_9672_G6)
14474     warning (OPT_Wdeprecated, "%stune=%s%s is deprecated and will be removed "
14475              "in future releases; use at least %stune=z900%s",
14476              prefix, opts->x_s390_tune == PROCESSOR_9672_G5 ? "g5" : "g6",
14477              suffix, prefix, suffix);
14478
14479   opts->x_s390_tune_flags = processor_flags_table[opts->x_s390_tune];
14480
14481   /* Sanity checks.  */
14482   if (opts->x_s390_arch == PROCESSOR_NATIVE
14483       || opts->x_s390_tune == PROCESSOR_NATIVE)
14484     gcc_unreachable ();
14485   if (TARGET_ZARCH_P (opts->x_target_flags) && !TARGET_CPU_ZARCH_P (opts))
14486     error ("z/Architecture mode not supported on %s",
14487            processor_table[(int)opts->x_s390_arch].name);
14488   if (TARGET_64BIT && !TARGET_ZARCH_P (opts->x_target_flags))
14489     error ("64-bit ABI not supported in ESA/390 mode");
14490
14491   /* Enable hardware transactions if available and not explicitly
14492      disabled by user.  E.g. with -m31 -march=zEC12 -mzarch */
14493   if (!TARGET_OPT_HTM_P (opts_set->x_target_flags))
14494     {
14495       if (TARGET_CPU_HTM_P (opts) && TARGET_ZARCH_P (opts->x_target_flags))
14496         opts->x_target_flags |= MASK_OPT_HTM;
14497       else
14498         opts->x_target_flags &= ~MASK_OPT_HTM;
14499     }
14500
14501   if (TARGET_OPT_VX_P (opts_set->x_target_flags))
14502     {
14503       if (TARGET_OPT_VX_P (opts->x_target_flags))
14504         {
14505           if (!TARGET_CPU_VX_P (opts))
14506             error ("hardware vector support not available on %s",
14507                    processor_table[(int)opts->x_s390_arch].name);
14508           if (TARGET_SOFT_FLOAT_P (opts->x_target_flags))
14509             error ("hardware vector support not available with -msoft-float");
14510         }
14511     }
14512   else
14513     {
14514       if (TARGET_CPU_VX_P (opts))
14515         /* Enable vector support if available and not explicitly disabled
14516            by user.  E.g. with -m31 -march=z13 -mzarch */
14517         opts->x_target_flags |= MASK_OPT_VX;
14518       else
14519         opts->x_target_flags &= ~MASK_OPT_VX;
14520     }
14521
14522   /* Use hardware DFP if available and not explicitly disabled by
14523      user. E.g. with -m31 -march=z10 -mzarch   */
14524   if (!TARGET_HARD_DFP_P (opts_set->x_target_flags))
14525     {
14526       if (TARGET_DFP_P (opts))
14527         opts->x_target_flags |= MASK_HARD_DFP;
14528       else
14529         opts->x_target_flags &= ~MASK_HARD_DFP;
14530     }
14531
14532   if (TARGET_HARD_DFP_P (opts->x_target_flags) && !TARGET_DFP_P (opts))
14533     {
14534       if (TARGET_HARD_DFP_P (opts_set->x_target_flags))
14535         {
14536           if (!TARGET_CPU_DFP_P (opts))
14537             error ("hardware decimal floating point instructions"
14538                    " not available on %s",
14539                    processor_table[(int)opts->x_s390_arch].name);
14540           if (!TARGET_ZARCH_P (opts->x_target_flags))
14541             error ("hardware decimal floating point instructions"
14542                    " not available in ESA/390 mode");
14543         }
14544       else
14545         opts->x_target_flags &= ~MASK_HARD_DFP;
14546     }
14547
14548   if (TARGET_SOFT_FLOAT_P (opts_set->x_target_flags)
14549       && TARGET_SOFT_FLOAT_P (opts->x_target_flags))
14550     {
14551       if (TARGET_HARD_DFP_P (opts_set->x_target_flags)
14552           && TARGET_HARD_DFP_P (opts->x_target_flags))
14553         error ("-mhard-dfp can%'t be used in conjunction with -msoft-float");
14554
14555       opts->x_target_flags &= ~MASK_HARD_DFP;
14556     }
14557
14558   if (TARGET_BACKCHAIN_P (opts->x_target_flags)
14559       && TARGET_PACKED_STACK_P (opts->x_target_flags)
14560       && TARGET_HARD_FLOAT_P (opts->x_target_flags))
14561     error ("-mbackchain -mpacked-stack -mhard-float are not supported "
14562            "in combination");
14563
14564   if (opts->x_s390_stack_size)
14565     {
14566       if (opts->x_s390_stack_guard >= opts->x_s390_stack_size)
14567         error ("stack size must be greater than the stack guard value");
14568       else if (opts->x_s390_stack_size > 1 << 16)
14569         error ("stack size must not be greater than 64k");
14570     }
14571   else if (opts->x_s390_stack_guard)
14572     error ("-mstack-guard implies use of -mstack-size");
14573
14574 #ifdef TARGET_DEFAULT_LONG_DOUBLE_128
14575   if (!TARGET_LONG_DOUBLE_128_P (opts_set->x_target_flags))
14576     opts->x_target_flags |= MASK_LONG_DOUBLE_128;
14577 #endif
14578
14579   if (opts->x_s390_tune >= PROCESSOR_2097_Z10)
14580     {
14581       maybe_set_param_value (PARAM_MAX_UNROLLED_INSNS, 100,
14582                              opts->x_param_values,
14583                              opts_set->x_param_values);
14584       maybe_set_param_value (PARAM_MAX_UNROLL_TIMES, 32,
14585                              opts->x_param_values,
14586                              opts_set->x_param_values);
14587       maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 2000,
14588                              opts->x_param_values,
14589                              opts_set->x_param_values);
14590       maybe_set_param_value (PARAM_MAX_COMPLETELY_PEEL_TIMES, 64,
14591                              opts->x_param_values,
14592                              opts_set->x_param_values);
14593     }
14594
14595   maybe_set_param_value (PARAM_MAX_PENDING_LIST_LENGTH, 256,
14596                          opts->x_param_values,
14597                          opts_set->x_param_values);
14598   /* values for loop prefetching */
14599   maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE, 256,
14600                          opts->x_param_values,
14601                          opts_set->x_param_values);
14602   maybe_set_param_value (PARAM_L1_CACHE_SIZE, 128,
14603                          opts->x_param_values,
14604                          opts_set->x_param_values);
14605   /* s390 has more than 2 levels and the size is much larger.  Since
14606      we are always running virtualized assume that we only get a small
14607      part of the caches above l1.  */
14608   maybe_set_param_value (PARAM_L2_CACHE_SIZE, 1500,
14609                          opts->x_param_values,
14610                          opts_set->x_param_values);
14611   maybe_set_param_value (PARAM_PREFETCH_MIN_INSN_TO_MEM_RATIO, 2,
14612                          opts->x_param_values,
14613                          opts_set->x_param_values);
14614   maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES, 6,
14615                          opts->x_param_values,
14616                          opts_set->x_param_values);
14617
14618   /* Use the alternative scheduling-pressure algorithm by default.  */
14619   maybe_set_param_value (PARAM_SCHED_PRESSURE_ALGORITHM, 2,
14620                          opts->x_param_values,
14621                          opts_set->x_param_values);
14622
14623   /* Call target specific restore function to do post-init work.  At the moment,
14624      this just sets opts->x_s390_cost_pointer.  */
14625   s390_function_specific_restore (opts, NULL);
14626 }
14627
14628 static void
14629 s390_option_override (void)
14630 {
14631   unsigned int i;
14632   cl_deferred_option *opt;
14633   vec<cl_deferred_option> *v =
14634     (vec<cl_deferred_option> *) s390_deferred_options;
14635
14636   if (v)
14637     FOR_EACH_VEC_ELT (*v, i, opt)
14638       {
14639         switch (opt->opt_index)
14640           {
14641           case OPT_mhotpatch_:
14642             {
14643               int val1;
14644               int val2;
14645               char s[256];
14646               char *t;
14647
14648               strncpy (s, opt->arg, 256);
14649               s[255] = 0;
14650               t = strchr (s, ',');
14651               if (t != NULL)
14652                 {
14653                   *t = 0;
14654                   t++;
14655                   val1 = integral_argument (s);
14656                   val2 = integral_argument (t);
14657                 }
14658               else
14659                 {
14660                   val1 = -1;
14661                   val2 = -1;
14662                 }
14663               if (val1 == -1 || val2 == -1)
14664                 {
14665                   /* argument is not a plain number */
14666                   error ("arguments to %qs should be non-negative integers",
14667                          "-mhotpatch=n,m");
14668                   break;
14669                 }
14670               else if (val1 > s390_hotpatch_hw_max
14671                        || val2 > s390_hotpatch_hw_max)
14672                 {
14673                   error ("argument to %qs is too large (max. %d)",
14674                          "-mhotpatch=n,m", s390_hotpatch_hw_max);
14675                   break;
14676                 }
14677               s390_hotpatch_hw_before_label = val1;
14678               s390_hotpatch_hw_after_label = val2;
14679               break;
14680             }
14681           default:
14682             gcc_unreachable ();
14683           }
14684       }
14685
14686   /* Set up function hooks.  */
14687   init_machine_status = s390_init_machine_status;
14688
14689   s390_option_override_internal (true, &global_options, &global_options_set);
14690
14691   /* Save the initial options in case the user does function specific
14692      options.  */
14693   target_option_default_node = build_target_option_node (&global_options);
14694   target_option_current_node = target_option_default_node;
14695
14696   /* This cannot reside in s390_option_optimization_table since HAVE_prefetch
14697      requires the arch flags to be evaluated already.  Since prefetching
14698      is beneficial on s390, we enable it if available.  */
14699   if (flag_prefetch_loop_arrays < 0 && HAVE_prefetch && optimize >= 3)
14700     flag_prefetch_loop_arrays = 1;
14701
14702   if (TARGET_TPF)
14703     {
14704       /* Don't emit DWARF3/4 unless specifically selected.  The TPF
14705          debuggers do not yet support DWARF 3/4.  */
14706       if (!global_options_set.x_dwarf_strict) 
14707         dwarf_strict = 1;
14708       if (!global_options_set.x_dwarf_version)
14709         dwarf_version = 2;
14710     }
14711
14712   /* Register a target-specific optimization-and-lowering pass
14713      to run immediately before prologue and epilogue generation.
14714
14715      Registering the pass must be done at start up.  It's
14716      convenient to do it here.  */
14717   opt_pass *new_pass = new pass_s390_early_mach (g);
14718   struct register_pass_info insert_pass_s390_early_mach =
14719     {
14720       new_pass,                 /* pass */
14721       "pro_and_epilogue",       /* reference_pass_name */
14722       1,                        /* ref_pass_instance_number */
14723       PASS_POS_INSERT_BEFORE    /* po_op */
14724     };
14725   register_pass (&insert_pass_s390_early_mach);
14726 }
14727
14728 #if S390_USE_TARGET_ATTRIBUTE
14729 /* Inner function to process the attribute((target(...))), take an argument and
14730    set the current options from the argument. If we have a list, recursively go
14731    over the list.  */
14732
14733 static bool
14734 s390_valid_target_attribute_inner_p (tree args,
14735                                      struct gcc_options *opts,
14736                                      struct gcc_options *new_opts_set,
14737                                      bool force_pragma)
14738 {
14739   char *next_optstr;
14740   bool ret = true;
14741
14742 #define S390_ATTRIB(S,O,A)  { S, sizeof (S)-1, O, A, 0 }
14743 #define S390_PRAGMA(S,O,A)  { S, sizeof (S)-1, O, A, 1 }
14744   static const struct
14745   {
14746     const char *string;
14747     size_t len;
14748     int opt;
14749     int has_arg;
14750     int only_as_pragma;
14751   } attrs[] = {
14752     /* enum options */
14753     S390_ATTRIB ("arch=", OPT_march_, 1),
14754     S390_ATTRIB ("tune=", OPT_mtune_, 1),
14755     /* uinteger options */
14756     S390_ATTRIB ("stack-guard=", OPT_mstack_guard_, 1),
14757     S390_ATTRIB ("stack-size=", OPT_mstack_size_, 1),
14758     S390_ATTRIB ("branch-cost=", OPT_mbranch_cost_, 1),
14759     S390_ATTRIB ("warn-framesize=", OPT_mwarn_framesize_, 1),
14760     /* flag options */
14761     S390_ATTRIB ("backchain", OPT_mbackchain, 0),
14762     S390_ATTRIB ("hard-dfp", OPT_mhard_dfp, 0),
14763     S390_ATTRIB ("hard-float", OPT_mhard_float, 0),
14764     S390_ATTRIB ("htm", OPT_mhtm, 0),
14765     S390_ATTRIB ("vx", OPT_mvx, 0),
14766     S390_ATTRIB ("packed-stack", OPT_mpacked_stack, 0),
14767     S390_ATTRIB ("small-exec", OPT_msmall_exec, 0),
14768     S390_ATTRIB ("soft-float", OPT_msoft_float, 0),
14769     S390_ATTRIB ("mvcle", OPT_mmvcle, 0),
14770     S390_PRAGMA ("zvector", OPT_mzvector, 0),
14771     /* boolean options */
14772     S390_ATTRIB ("warn-dynamicstack", OPT_mwarn_dynamicstack, 0),
14773   };
14774 #undef S390_ATTRIB
14775 #undef S390_PRAGMA
14776
14777   /* If this is a list, recurse to get the options.  */
14778   if (TREE_CODE (args) == TREE_LIST)
14779     {
14780       bool ret = true;
14781       int num_pragma_values;
14782       int i;
14783
14784       /* Note: attribs.c:decl_attributes prepends the values from
14785          current_target_pragma to the list of target attributes.  To determine
14786          whether we're looking at a value of the attribute or the pragma we
14787          assume that the first [list_length (current_target_pragma)] values in
14788          the list are the values from the pragma.  */
14789       num_pragma_values = (!force_pragma && current_target_pragma != NULL)
14790         ? list_length (current_target_pragma) : 0;
14791       for (i = 0; args; args = TREE_CHAIN (args), i++)
14792         {
14793           bool is_pragma;
14794
14795           is_pragma = (force_pragma || i < num_pragma_values);
14796           if (TREE_VALUE (args)
14797               && !s390_valid_target_attribute_inner_p (TREE_VALUE (args),
14798                                                        opts, new_opts_set,
14799                                                        is_pragma))
14800             {
14801               ret = false;
14802             }
14803         }
14804       return ret;
14805     }
14806
14807   else if (TREE_CODE (args) != STRING_CST)
14808     {
14809       error ("attribute %<target%> argument not a string");
14810       return false;
14811     }
14812
14813   /* Handle multiple arguments separated by commas.  */
14814   next_optstr = ASTRDUP (TREE_STRING_POINTER (args));
14815
14816   while (next_optstr && *next_optstr != '\0')
14817     {
14818       char *p = next_optstr;
14819       char *orig_p = p;
14820       char *comma = strchr (next_optstr, ',');
14821       size_t len, opt_len;
14822       int opt;
14823       bool opt_set_p;
14824       char ch;
14825       unsigned i;
14826       int mask = 0;
14827       enum cl_var_type var_type;
14828       bool found;
14829
14830       if (comma)
14831         {
14832           *comma = '\0';
14833           len = comma - next_optstr;
14834           next_optstr = comma + 1;
14835         }
14836       else
14837         {
14838           len = strlen (p);
14839           next_optstr = NULL;
14840         }
14841
14842       /* Recognize no-xxx.  */
14843       if (len > 3 && p[0] == 'n' && p[1] == 'o' && p[2] == '-')
14844         {
14845           opt_set_p = false;
14846           p += 3;
14847           len -= 3;
14848         }
14849       else
14850         opt_set_p = true;
14851
14852       /* Find the option.  */
14853       ch = *p;
14854       found = false;
14855       for (i = 0; i < ARRAY_SIZE (attrs); i++)
14856         {
14857           opt_len = attrs[i].len;
14858           if (ch == attrs[i].string[0]
14859               && ((attrs[i].has_arg) ? len > opt_len : len == opt_len)
14860               && memcmp (p, attrs[i].string, opt_len) == 0)
14861             {
14862               opt = attrs[i].opt;
14863               if (!opt_set_p && cl_options[opt].cl_reject_negative)
14864                 continue;
14865               mask = cl_options[opt].var_value;
14866               var_type = cl_options[opt].var_type;
14867               found = true;
14868               break;
14869             }
14870         }
14871
14872       /* Process the option.  */
14873       if (!found)
14874         {
14875           error ("attribute(target(\"%s\")) is unknown", orig_p);
14876           return false;
14877         }
14878       else if (attrs[i].only_as_pragma && !force_pragma)
14879         {
14880           /* Value is not allowed for the target attribute.  */
14881           error ("Value %qs is not supported by attribute %<target%>",
14882                  attrs[i].string);
14883           return false;
14884         }
14885
14886       else if (var_type == CLVC_BIT_SET || var_type == CLVC_BIT_CLEAR)
14887         {
14888           if (var_type == CLVC_BIT_CLEAR)
14889             opt_set_p = !opt_set_p;
14890
14891           if (opt_set_p)
14892             opts->x_target_flags |= mask;
14893           else
14894             opts->x_target_flags &= ~mask;
14895           new_opts_set->x_target_flags |= mask;
14896         }
14897
14898       else if (cl_options[opt].var_type == CLVC_BOOLEAN)
14899         {
14900           int value;
14901
14902           if (cl_options[opt].cl_uinteger)
14903             {
14904               /* Unsigned integer argument.  Code based on the function
14905                  decode_cmdline_option () in opts-common.c.  */
14906               value = integral_argument (p + opt_len);
14907             }
14908           else
14909             value = (opt_set_p) ? 1 : 0;
14910
14911           if (value != -1)
14912             {
14913               struct cl_decoded_option decoded;
14914
14915               /* Value range check; only implemented for numeric and boolean
14916                  options at the moment.  */
14917               generate_option (opt, NULL, value, CL_TARGET, &decoded);
14918               s390_handle_option (opts, new_opts_set, &decoded, input_location);
14919               set_option (opts, new_opts_set, opt, value,
14920                           p + opt_len, DK_UNSPECIFIED, input_location,
14921                           global_dc);
14922             }
14923           else
14924             {
14925               error ("attribute(target(\"%s\")) is unknown", orig_p);
14926               ret = false;
14927             }
14928         }
14929
14930       else if (cl_options[opt].var_type == CLVC_ENUM)
14931         {
14932           bool arg_ok;
14933           int value;
14934
14935           arg_ok = opt_enum_arg_to_value (opt, p + opt_len, &value, CL_TARGET);
14936           if (arg_ok)
14937             set_option (opts, new_opts_set, opt, value,
14938                         p + opt_len, DK_UNSPECIFIED, input_location,
14939                         global_dc);
14940           else
14941             {
14942               error ("attribute(target(\"%s\")) is unknown", orig_p);
14943               ret = false;
14944             }
14945         }
14946
14947       else
14948         gcc_unreachable ();
14949     }
14950   return ret;
14951 }
14952
14953 /* Return a TARGET_OPTION_NODE tree of the target options listed or NULL.  */
14954
14955 tree
14956 s390_valid_target_attribute_tree (tree args,
14957                                   struct gcc_options *opts,
14958                                   const struct gcc_options *opts_set,
14959                                   bool force_pragma)
14960 {
14961   tree t = NULL_TREE;
14962   struct gcc_options new_opts_set;
14963
14964   memset (&new_opts_set, 0, sizeof (new_opts_set));
14965
14966   /* Process each of the options on the chain.  */
14967   if (! s390_valid_target_attribute_inner_p (args, opts, &new_opts_set,
14968                                              force_pragma))
14969     return error_mark_node;
14970
14971   /* If some option was set (even if it has not changed), rerun
14972      s390_option_override_internal, and then save the options away.  */
14973   if (new_opts_set.x_target_flags
14974       || new_opts_set.x_s390_arch
14975       || new_opts_set.x_s390_tune
14976       || new_opts_set.x_s390_stack_guard
14977       || new_opts_set.x_s390_stack_size
14978       || new_opts_set.x_s390_branch_cost
14979       || new_opts_set.x_s390_warn_framesize
14980       || new_opts_set.x_s390_warn_dynamicstack_p)
14981     {
14982       const unsigned char *src = (const unsigned char *)opts_set;
14983       unsigned char *dest = (unsigned char *)&new_opts_set;
14984       unsigned int i;
14985
14986       /* Merge the original option flags into the new ones.  */
14987       for (i = 0; i < sizeof(*opts_set); i++)
14988         dest[i] |= src[i];
14989
14990       /* Do any overrides, such as arch=xxx, or tune=xxx support.  */
14991       s390_option_override_internal (false, opts, &new_opts_set);
14992       /* Save the current options unless we are validating options for
14993          #pragma.  */
14994       t = build_target_option_node (opts);
14995     }
14996   return t;
14997 }
14998
14999 /* Hook to validate attribute((target("string"))).  */
15000
15001 static bool
15002 s390_valid_target_attribute_p (tree fndecl,
15003                                tree ARG_UNUSED (name),
15004                                tree args,
15005                                int ARG_UNUSED (flags))
15006 {
15007   struct gcc_options func_options;
15008   tree new_target, new_optimize;
15009   bool ret = true;
15010
15011   /* attribute((target("default"))) does nothing, beyond
15012      affecting multi-versioning.  */
15013   if (TREE_VALUE (args)
15014       && TREE_CODE (TREE_VALUE (args)) == STRING_CST
15015       && TREE_CHAIN (args) == NULL_TREE
15016       && strcmp (TREE_STRING_POINTER (TREE_VALUE (args)), "default") == 0)
15017     return true;
15018
15019   tree old_optimize = build_optimization_node (&global_options);
15020
15021   /* Get the optimization options of the current function.  */
15022   tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
15023
15024   if (!func_optimize)
15025     func_optimize = old_optimize;
15026
15027   /* Init func_options.  */
15028   memset (&func_options, 0, sizeof (func_options));
15029   init_options_struct (&func_options, NULL);
15030   lang_hooks.init_options_struct (&func_options);
15031
15032   cl_optimization_restore (&func_options, TREE_OPTIMIZATION (func_optimize));
15033
15034   /* Initialize func_options to the default before its target options can
15035      be set.  */
15036   cl_target_option_restore (&func_options,
15037                             TREE_TARGET_OPTION (target_option_default_node));
15038
15039   new_target = s390_valid_target_attribute_tree (args, &func_options,
15040                                                  &global_options_set,
15041                                                  (args ==
15042                                                   current_target_pragma));
15043   new_optimize = build_optimization_node (&func_options);
15044   if (new_target == error_mark_node)
15045     ret = false;
15046   else if (fndecl && new_target)
15047     {
15048       DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
15049       if (old_optimize != new_optimize)
15050         DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
15051     }
15052   return ret;
15053 }
15054
15055 /* Restore targets globals from NEW_TREE and invalidate s390_previous_fndecl
15056    cache.  */
15057
15058 void
15059 s390_activate_target_options (tree new_tree)
15060 {
15061   cl_target_option_restore (&global_options, TREE_TARGET_OPTION (new_tree));
15062   if (TREE_TARGET_GLOBALS (new_tree))
15063     restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
15064   else if (new_tree == target_option_default_node)
15065     restore_target_globals (&default_target_globals);
15066   else
15067     TREE_TARGET_GLOBALS (new_tree) = save_target_globals_default_opts ();
15068   s390_previous_fndecl = NULL_TREE;
15069 }
15070
15071 /* Establish appropriate back-end context for processing the function
15072    FNDECL.  The argument might be NULL to indicate processing at top
15073    level, outside of any function scope.  */
15074 static void
15075 s390_set_current_function (tree fndecl)
15076 {
15077   /* Only change the context if the function changes.  This hook is called
15078      several times in the course of compiling a function, and we don't want to
15079      slow things down too much or call target_reinit when it isn't safe.  */
15080   if (fndecl == s390_previous_fndecl)
15081     return;
15082
15083   tree old_tree;
15084   if (s390_previous_fndecl == NULL_TREE)
15085     old_tree = target_option_current_node;
15086   else if (DECL_FUNCTION_SPECIFIC_TARGET (s390_previous_fndecl))
15087     old_tree = DECL_FUNCTION_SPECIFIC_TARGET (s390_previous_fndecl);
15088   else
15089     old_tree = target_option_default_node;
15090
15091   if (fndecl == NULL_TREE)
15092     {
15093       if (old_tree != target_option_current_node)
15094         s390_activate_target_options (target_option_current_node);
15095       return;
15096     }
15097
15098   tree new_tree = DECL_FUNCTION_SPECIFIC_TARGET (fndecl);
15099   if (new_tree == NULL_TREE)
15100     new_tree = target_option_default_node;
15101
15102   if (old_tree != new_tree)
15103     s390_activate_target_options (new_tree);
15104   s390_previous_fndecl = fndecl;
15105 }
15106 #endif
15107
15108 /* Implement TARGET_USE_BY_PIECES_INFRASTRUCTURE_P.  */
15109
15110 static bool
15111 s390_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size,
15112                                      unsigned int align ATTRIBUTE_UNUSED,
15113                                      enum by_pieces_operation op ATTRIBUTE_UNUSED,
15114                                      bool speed_p ATTRIBUTE_UNUSED)
15115 {
15116   return (size == 1 || size == 2
15117           || size == 4 || (TARGET_ZARCH && size == 8));
15118 }
15119
15120 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV hook.  */
15121
15122 static void
15123 s390_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
15124 {
15125   tree sfpc = s390_builtin_decls[S390_BUILTIN_s390_sfpc];
15126   tree efpc = s390_builtin_decls[S390_BUILTIN_s390_efpc];
15127   tree call_efpc = build_call_expr (efpc, 0);
15128   tree fenv_var = create_tmp_var_raw (unsigned_type_node);
15129
15130 #define FPC_EXCEPTION_MASK       HOST_WIDE_INT_UC (0xf8000000)
15131 #define FPC_FLAGS_MASK           HOST_WIDE_INT_UC (0x00f80000)
15132 #define FPC_DXC_MASK             HOST_WIDE_INT_UC (0x0000ff00)
15133 #define FPC_EXCEPTION_MASK_SHIFT HOST_WIDE_INT_UC (24)
15134 #define FPC_FLAGS_SHIFT          HOST_WIDE_INT_UC (16)
15135 #define FPC_DXC_SHIFT            HOST_WIDE_INT_UC (8)
15136
15137   /* Generates the equivalent of feholdexcept (&fenv_var)
15138
15139      fenv_var = __builtin_s390_efpc ();
15140      __builtin_s390_sfpc (fenv_var & mask) */
15141   tree old_fpc = build2 (MODIFY_EXPR, unsigned_type_node, fenv_var, call_efpc);
15142   tree new_fpc =
15143     build2 (BIT_AND_EXPR, unsigned_type_node, fenv_var,
15144             build_int_cst (unsigned_type_node,
15145                            ~(FPC_DXC_MASK | FPC_FLAGS_MASK |
15146                              FPC_EXCEPTION_MASK)));
15147   tree set_new_fpc = build_call_expr (sfpc, 1, new_fpc);
15148   *hold = build2 (COMPOUND_EXPR, void_type_node, old_fpc, set_new_fpc);
15149
15150   /* Generates the equivalent of feclearexcept (FE_ALL_EXCEPT)
15151
15152      __builtin_s390_sfpc (__builtin_s390_efpc () & mask) */
15153   new_fpc = build2 (BIT_AND_EXPR, unsigned_type_node, call_efpc,
15154                     build_int_cst (unsigned_type_node,
15155                                    ~(FPC_DXC_MASK | FPC_FLAGS_MASK)));
15156   *clear = build_call_expr (sfpc, 1, new_fpc);
15157
15158   /* Generates the equivalent of feupdateenv (fenv_var)
15159
15160   old_fpc = __builtin_s390_efpc ();
15161   __builtin_s390_sfpc (fenv_var);
15162   __atomic_feraiseexcept ((old_fpc & FPC_FLAGS_MASK) >> FPC_FLAGS_SHIFT);  */
15163
15164   old_fpc = create_tmp_var_raw (unsigned_type_node);
15165   tree store_old_fpc = build2 (MODIFY_EXPR, void_type_node,
15166                                old_fpc, call_efpc);
15167
15168   set_new_fpc = build_call_expr (sfpc, 1, fenv_var);
15169
15170   tree raise_old_except = build2 (BIT_AND_EXPR, unsigned_type_node, old_fpc,
15171                                   build_int_cst (unsigned_type_node,
15172                                                  FPC_FLAGS_MASK));
15173   raise_old_except = build2 (RSHIFT_EXPR, unsigned_type_node, raise_old_except,
15174                              build_int_cst (unsigned_type_node,
15175                                             FPC_FLAGS_SHIFT));
15176   tree atomic_feraiseexcept
15177     = builtin_decl_implicit (BUILT_IN_ATOMIC_FERAISEEXCEPT);
15178   raise_old_except = build_call_expr (atomic_feraiseexcept,
15179                                       1, raise_old_except);
15180
15181   *update = build2 (COMPOUND_EXPR, void_type_node,
15182                     build2 (COMPOUND_EXPR, void_type_node,
15183                             store_old_fpc, set_new_fpc),
15184                     raise_old_except);
15185
15186 #undef FPC_EXCEPTION_MASK
15187 #undef FPC_FLAGS_MASK
15188 #undef FPC_DXC_MASK
15189 #undef FPC_EXCEPTION_MASK_SHIFT
15190 #undef FPC_FLAGS_SHIFT
15191 #undef FPC_DXC_SHIFT
15192 }
15193
15194 /* Return the vector mode to be used for inner mode MODE when doing
15195    vectorization.  */
15196 static machine_mode
15197 s390_preferred_simd_mode (machine_mode mode)
15198 {
15199   if (TARGET_VX)
15200     switch (mode)
15201       {
15202       case DFmode:
15203         return V2DFmode;
15204       case DImode:
15205         return V2DImode;
15206       case SImode:
15207         return V4SImode;
15208       case HImode:
15209         return V8HImode;
15210       case QImode:
15211         return V16QImode;
15212       default:;
15213       }
15214   return word_mode;
15215 }
15216
15217 /* Our hardware does not require vectors to be strictly aligned.  */
15218 static bool
15219 s390_support_vector_misalignment (machine_mode mode ATTRIBUTE_UNUSED,
15220                                   const_tree type ATTRIBUTE_UNUSED,
15221                                   int misalignment ATTRIBUTE_UNUSED,
15222                                   bool is_packed ATTRIBUTE_UNUSED)
15223 {
15224   if (TARGET_VX)
15225     return true;
15226
15227   return default_builtin_support_vector_misalignment (mode, type, misalignment,
15228                                                       is_packed);
15229 }
15230
15231 /* The vector ABI requires vector types to be aligned on an 8 byte
15232    boundary (our stack alignment).  However, we allow this to be
15233    overriden by the user, while this definitely breaks the ABI.  */
15234 static HOST_WIDE_INT
15235 s390_vector_alignment (const_tree type)
15236 {
15237   if (!TARGET_VX_ABI)
15238     return default_vector_alignment (type);
15239
15240   if (TYPE_USER_ALIGN (type))
15241     return TYPE_ALIGN (type);
15242
15243   return MIN (64, tree_to_shwi (TYPE_SIZE (type)));
15244 }
15245
15246 #ifdef HAVE_AS_MACHINE_MACHINEMODE
15247 /* Implement TARGET_ASM_FILE_START.  */
15248 static void
15249 s390_asm_file_start (void)
15250 {
15251   default_file_start ();
15252   s390_asm_output_machine_for_arch (asm_out_file);
15253 }
15254 #endif
15255
15256 /* Implement TARGET_ASM_FILE_END.  */
15257 static void
15258 s390_asm_file_end (void)
15259 {
15260 #ifdef HAVE_AS_GNU_ATTRIBUTE
15261   varpool_node *vnode;
15262   cgraph_node *cnode;
15263
15264   FOR_EACH_VARIABLE (vnode)
15265     if (TREE_PUBLIC (vnode->decl))
15266       s390_check_type_for_vector_abi (TREE_TYPE (vnode->decl), false, false);
15267
15268   FOR_EACH_FUNCTION (cnode)
15269     if (TREE_PUBLIC (cnode->decl))
15270       s390_check_type_for_vector_abi (TREE_TYPE (cnode->decl), false, false);
15271
15272
15273   if (s390_vector_abi != 0)
15274     fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
15275              s390_vector_abi);
15276 #endif
15277   file_end_indicate_exec_stack ();
15278
15279   if (flag_split_stack)
15280     file_end_indicate_split_stack ();
15281 }
15282
15283 /* Return true if TYPE is a vector bool type.  */
15284 static inline bool
15285 s390_vector_bool_type_p (const_tree type)
15286 {
15287   return TYPE_VECTOR_OPAQUE (type);
15288 }
15289
15290 /* Return the diagnostic message string if the binary operation OP is
15291    not permitted on TYPE1 and TYPE2, NULL otherwise.  */
15292 static const char*
15293 s390_invalid_binary_op (int op ATTRIBUTE_UNUSED, const_tree type1, const_tree type2)
15294 {
15295   bool bool1_p, bool2_p;
15296   bool plusminus_p;
15297   bool muldiv_p;
15298   bool compare_p;
15299   machine_mode mode1, mode2;
15300
15301   if (!TARGET_ZVECTOR)
15302     return NULL;
15303
15304   if (!VECTOR_TYPE_P (type1) || !VECTOR_TYPE_P (type2))
15305     return NULL;
15306
15307   bool1_p = s390_vector_bool_type_p (type1);
15308   bool2_p = s390_vector_bool_type_p (type2);
15309
15310   /* Mixing signed and unsigned types is forbidden for all
15311      operators.  */
15312   if (!bool1_p && !bool2_p
15313       && TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2))
15314     return N_("types differ in signess");
15315
15316   plusminus_p = (op == PLUS_EXPR || op == MINUS_EXPR);
15317   muldiv_p = (op == MULT_EXPR || op == RDIV_EXPR || op == TRUNC_DIV_EXPR
15318               || op == CEIL_DIV_EXPR || op == FLOOR_DIV_EXPR
15319               || op == ROUND_DIV_EXPR);
15320   compare_p = (op == LT_EXPR || op == LE_EXPR || op == GT_EXPR || op == GE_EXPR
15321                || op == EQ_EXPR || op == NE_EXPR);
15322
15323   if (bool1_p && bool2_p && (plusminus_p || muldiv_p))
15324     return N_("binary operator does not support two vector bool operands");
15325
15326   if (bool1_p != bool2_p && (muldiv_p || compare_p))
15327     return N_("binary operator does not support vector bool operand");
15328
15329   mode1 = TYPE_MODE (type1);
15330   mode2 = TYPE_MODE (type2);
15331
15332   if (bool1_p != bool2_p && plusminus_p
15333       && (GET_MODE_CLASS (mode1) == MODE_VECTOR_FLOAT
15334           || GET_MODE_CLASS (mode2) == MODE_VECTOR_FLOAT))
15335     return N_("binary operator does not support mixing vector "
15336               "bool with floating point vector operands");
15337
15338   return NULL;
15339 }
15340
15341 /* Implement TARGET_C_EXCESS_PRECISION.
15342
15343    FIXME: For historical reasons, float_t and double_t are typedef'ed to
15344    double on s390, causing operations on float_t to operate in a higher
15345    precision than is necessary.  However, it is not the case that SFmode
15346    operations have implicit excess precision, and we generate more optimal
15347    code if we let the compiler know no implicit extra precision is added.
15348
15349    That means when we are compiling with -fexcess-precision=fast, the value
15350    we set for FLT_EVAL_METHOD will be out of line with the actual precision of
15351    float_t (though they would be correct for -fexcess-precision=standard).
15352
15353    A complete fix would modify glibc to remove the unnecessary typedef
15354    of float_t to double.  */
15355
15356 static enum flt_eval_method
15357 s390_excess_precision (enum excess_precision_type type)
15358 {
15359   switch (type)
15360     {
15361       case EXCESS_PRECISION_TYPE_IMPLICIT:
15362       case EXCESS_PRECISION_TYPE_FAST:
15363         /* The fastest type to promote to will always be the native type,
15364            whether that occurs with implicit excess precision or
15365            otherwise.  */
15366         return FLT_EVAL_METHOD_PROMOTE_TO_FLOAT;
15367       case EXCESS_PRECISION_TYPE_STANDARD:
15368         /* Otherwise, when we are in a standards compliant mode, to
15369            ensure consistency with the implementation in glibc, report that
15370            float is evaluated to the range and precision of double.  */
15371         return FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE;
15372       default:
15373         gcc_unreachable ();
15374     }
15375   return FLT_EVAL_METHOD_UNPREDICTABLE;
15376 }
15377
15378 /* Initialize GCC target structure.  */
15379
15380 #undef  TARGET_ASM_ALIGNED_HI_OP
15381 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
15382 #undef  TARGET_ASM_ALIGNED_DI_OP
15383 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
15384 #undef  TARGET_ASM_INTEGER
15385 #define TARGET_ASM_INTEGER s390_assemble_integer
15386
15387 #undef  TARGET_ASM_OPEN_PAREN
15388 #define TARGET_ASM_OPEN_PAREN ""
15389
15390 #undef  TARGET_ASM_CLOSE_PAREN
15391 #define TARGET_ASM_CLOSE_PAREN ""
15392
15393 #undef TARGET_OPTION_OVERRIDE
15394 #define TARGET_OPTION_OVERRIDE s390_option_override
15395
15396 #ifdef TARGET_THREAD_SSP_OFFSET
15397 #undef TARGET_STACK_PROTECT_GUARD
15398 #define TARGET_STACK_PROTECT_GUARD hook_tree_void_null
15399 #endif
15400
15401 #undef  TARGET_ENCODE_SECTION_INFO
15402 #define TARGET_ENCODE_SECTION_INFO s390_encode_section_info
15403
15404 #undef TARGET_SCALAR_MODE_SUPPORTED_P
15405 #define TARGET_SCALAR_MODE_SUPPORTED_P s390_scalar_mode_supported_p
15406
15407 #ifdef HAVE_AS_TLS
15408 #undef TARGET_HAVE_TLS
15409 #define TARGET_HAVE_TLS true
15410 #endif
15411 #undef TARGET_CANNOT_FORCE_CONST_MEM
15412 #define TARGET_CANNOT_FORCE_CONST_MEM s390_cannot_force_const_mem
15413
15414 #undef TARGET_DELEGITIMIZE_ADDRESS
15415 #define TARGET_DELEGITIMIZE_ADDRESS s390_delegitimize_address
15416
15417 #undef TARGET_LEGITIMIZE_ADDRESS
15418 #define TARGET_LEGITIMIZE_ADDRESS s390_legitimize_address
15419
15420 #undef TARGET_RETURN_IN_MEMORY
15421 #define TARGET_RETURN_IN_MEMORY s390_return_in_memory
15422
15423 #undef  TARGET_INIT_BUILTINS
15424 #define TARGET_INIT_BUILTINS s390_init_builtins
15425 #undef  TARGET_EXPAND_BUILTIN
15426 #define TARGET_EXPAND_BUILTIN s390_expand_builtin
15427 #undef  TARGET_BUILTIN_DECL
15428 #define TARGET_BUILTIN_DECL s390_builtin_decl
15429
15430 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
15431 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA s390_output_addr_const_extra
15432
15433 #undef TARGET_ASM_OUTPUT_MI_THUNK
15434 #define TARGET_ASM_OUTPUT_MI_THUNK s390_output_mi_thunk
15435 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
15436 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
15437
15438 #undef TARGET_C_EXCESS_PRECISION
15439 #define TARGET_C_EXCESS_PRECISION s390_excess_precision
15440
15441 #undef  TARGET_SCHED_ADJUST_PRIORITY
15442 #define TARGET_SCHED_ADJUST_PRIORITY s390_adjust_priority
15443 #undef TARGET_SCHED_ISSUE_RATE
15444 #define TARGET_SCHED_ISSUE_RATE s390_issue_rate
15445 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
15446 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD s390_first_cycle_multipass_dfa_lookahead
15447
15448 #undef TARGET_SCHED_VARIABLE_ISSUE
15449 #define TARGET_SCHED_VARIABLE_ISSUE s390_sched_variable_issue
15450 #undef TARGET_SCHED_REORDER
15451 #define TARGET_SCHED_REORDER s390_sched_reorder
15452 #undef TARGET_SCHED_INIT
15453 #define TARGET_SCHED_INIT s390_sched_init
15454
15455 #undef TARGET_CANNOT_COPY_INSN_P
15456 #define TARGET_CANNOT_COPY_INSN_P s390_cannot_copy_insn_p
15457 #undef TARGET_RTX_COSTS
15458 #define TARGET_RTX_COSTS s390_rtx_costs
15459 #undef TARGET_ADDRESS_COST
15460 #define TARGET_ADDRESS_COST s390_address_cost
15461 #undef TARGET_REGISTER_MOVE_COST
15462 #define TARGET_REGISTER_MOVE_COST s390_register_move_cost
15463 #undef TARGET_MEMORY_MOVE_COST
15464 #define TARGET_MEMORY_MOVE_COST s390_memory_move_cost
15465 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
15466 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
15467   s390_builtin_vectorization_cost
15468
15469 #undef TARGET_MACHINE_DEPENDENT_REORG
15470 #define TARGET_MACHINE_DEPENDENT_REORG s390_reorg
15471
15472 #undef TARGET_VALID_POINTER_MODE
15473 #define TARGET_VALID_POINTER_MODE s390_valid_pointer_mode
15474
15475 #undef TARGET_BUILD_BUILTIN_VA_LIST
15476 #define TARGET_BUILD_BUILTIN_VA_LIST s390_build_builtin_va_list
15477 #undef TARGET_EXPAND_BUILTIN_VA_START
15478 #define TARGET_EXPAND_BUILTIN_VA_START s390_va_start
15479 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
15480 #define TARGET_GIMPLIFY_VA_ARG_EXPR s390_gimplify_va_arg
15481
15482 #undef TARGET_PROMOTE_FUNCTION_MODE
15483 #define TARGET_PROMOTE_FUNCTION_MODE s390_promote_function_mode
15484 #undef TARGET_PASS_BY_REFERENCE
15485 #define TARGET_PASS_BY_REFERENCE s390_pass_by_reference
15486
15487 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
15488 #define TARGET_FUNCTION_OK_FOR_SIBCALL s390_function_ok_for_sibcall
15489 #undef TARGET_FUNCTION_ARG
15490 #define TARGET_FUNCTION_ARG s390_function_arg
15491 #undef TARGET_FUNCTION_ARG_ADVANCE
15492 #define TARGET_FUNCTION_ARG_ADVANCE s390_function_arg_advance
15493 #undef TARGET_FUNCTION_VALUE
15494 #define TARGET_FUNCTION_VALUE s390_function_value
15495 #undef TARGET_LIBCALL_VALUE
15496 #define TARGET_LIBCALL_VALUE s390_libcall_value
15497 #undef TARGET_STRICT_ARGUMENT_NAMING
15498 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
15499
15500 #undef TARGET_KEEP_LEAF_WHEN_PROFILED
15501 #define TARGET_KEEP_LEAF_WHEN_PROFILED s390_keep_leaf_when_profiled
15502
15503 #undef TARGET_FIXED_CONDITION_CODE_REGS
15504 #define TARGET_FIXED_CONDITION_CODE_REGS s390_fixed_condition_code_regs
15505
15506 #undef TARGET_CC_MODES_COMPATIBLE
15507 #define TARGET_CC_MODES_COMPATIBLE s390_cc_modes_compatible
15508
15509 #undef TARGET_INVALID_WITHIN_DOLOOP
15510 #define TARGET_INVALID_WITHIN_DOLOOP hook_constcharptr_const_rtx_insn_null
15511
15512 #ifdef HAVE_AS_TLS
15513 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
15514 #define TARGET_ASM_OUTPUT_DWARF_DTPREL s390_output_dwarf_dtprel
15515 #endif
15516
15517 #undef TARGET_DWARF_FRAME_REG_MODE
15518 #define TARGET_DWARF_FRAME_REG_MODE s390_dwarf_frame_reg_mode
15519
15520 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
15521 #undef TARGET_MANGLE_TYPE
15522 #define TARGET_MANGLE_TYPE s390_mangle_type
15523 #endif
15524
15525 #undef TARGET_SCALAR_MODE_SUPPORTED_P
15526 #define TARGET_SCALAR_MODE_SUPPORTED_P s390_scalar_mode_supported_p
15527
15528 #undef TARGET_VECTOR_MODE_SUPPORTED_P
15529 #define TARGET_VECTOR_MODE_SUPPORTED_P s390_vector_mode_supported_p
15530
15531 #undef  TARGET_PREFERRED_RELOAD_CLASS
15532 #define TARGET_PREFERRED_RELOAD_CLASS s390_preferred_reload_class
15533
15534 #undef TARGET_SECONDARY_RELOAD
15535 #define TARGET_SECONDARY_RELOAD s390_secondary_reload
15536
15537 #undef TARGET_LIBGCC_CMP_RETURN_MODE
15538 #define TARGET_LIBGCC_CMP_RETURN_MODE s390_libgcc_cmp_return_mode
15539
15540 #undef TARGET_LIBGCC_SHIFT_COUNT_MODE
15541 #define TARGET_LIBGCC_SHIFT_COUNT_MODE s390_libgcc_shift_count_mode
15542
15543 #undef TARGET_LEGITIMATE_ADDRESS_P
15544 #define TARGET_LEGITIMATE_ADDRESS_P s390_legitimate_address_p
15545
15546 #undef TARGET_LEGITIMATE_CONSTANT_P
15547 #define TARGET_LEGITIMATE_CONSTANT_P s390_legitimate_constant_p
15548
15549 #undef TARGET_LRA_P
15550 #define TARGET_LRA_P s390_lra_p
15551
15552 #undef TARGET_CAN_ELIMINATE
15553 #define TARGET_CAN_ELIMINATE s390_can_eliminate
15554
15555 #undef TARGET_CONDITIONAL_REGISTER_USAGE
15556 #define TARGET_CONDITIONAL_REGISTER_USAGE s390_conditional_register_usage
15557
15558 #undef TARGET_LOOP_UNROLL_ADJUST
15559 #define TARGET_LOOP_UNROLL_ADJUST s390_loop_unroll_adjust
15560
15561 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
15562 #define TARGET_ASM_TRAMPOLINE_TEMPLATE s390_asm_trampoline_template
15563 #undef TARGET_TRAMPOLINE_INIT
15564 #define TARGET_TRAMPOLINE_INIT s390_trampoline_init
15565
15566 #undef TARGET_UNWIND_WORD_MODE
15567 #define TARGET_UNWIND_WORD_MODE s390_unwind_word_mode
15568
15569 #undef TARGET_CANONICALIZE_COMPARISON
15570 #define TARGET_CANONICALIZE_COMPARISON s390_canonicalize_comparison
15571
15572 #undef TARGET_HARD_REGNO_SCRATCH_OK
15573 #define TARGET_HARD_REGNO_SCRATCH_OK s390_hard_regno_scratch_ok
15574
15575 #undef TARGET_ATTRIBUTE_TABLE
15576 #define TARGET_ATTRIBUTE_TABLE s390_attribute_table
15577
15578 #undef TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P
15579 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P hook_bool_const_tree_true
15580
15581 #undef TARGET_SET_UP_BY_PROLOGUE
15582 #define TARGET_SET_UP_BY_PROLOGUE s300_set_up_by_prologue
15583
15584 #undef TARGET_EXTRA_LIVE_ON_ENTRY
15585 #define TARGET_EXTRA_LIVE_ON_ENTRY s390_live_on_entry
15586
15587 #undef TARGET_USE_BY_PIECES_INFRASTRUCTURE_P
15588 #define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \
15589   s390_use_by_pieces_infrastructure_p
15590
15591 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
15592 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV s390_atomic_assign_expand_fenv
15593
15594 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
15595 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN s390_invalid_arg_for_unprototyped_fn
15596
15597 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
15598 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE s390_preferred_simd_mode
15599
15600 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
15601 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT s390_support_vector_misalignment
15602
15603 #undef TARGET_VECTOR_ALIGNMENT
15604 #define TARGET_VECTOR_ALIGNMENT s390_vector_alignment
15605
15606 #undef TARGET_INVALID_BINARY_OP
15607 #define TARGET_INVALID_BINARY_OP s390_invalid_binary_op
15608
15609 #ifdef HAVE_AS_MACHINE_MACHINEMODE
15610 #undef TARGET_ASM_FILE_START
15611 #define TARGET_ASM_FILE_START s390_asm_file_start
15612 #endif
15613
15614 #undef TARGET_ASM_FILE_END
15615 #define TARGET_ASM_FILE_END s390_asm_file_end
15616
15617 #if S390_USE_TARGET_ATTRIBUTE
15618 #undef TARGET_SET_CURRENT_FUNCTION
15619 #define TARGET_SET_CURRENT_FUNCTION s390_set_current_function
15620
15621 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
15622 #define TARGET_OPTION_VALID_ATTRIBUTE_P s390_valid_target_attribute_p
15623 #endif
15624
15625 #undef TARGET_OPTION_RESTORE
15626 #define TARGET_OPTION_RESTORE s390_function_specific_restore
15627
15628 struct gcc_target targetm = TARGET_INITIALIZER;
15629
15630 #include "gt-s390.h"