[18/77] Make int_mode_for_mode return an opt_scalar_int_mode
[official-gcc.git] / gcc / config / s390 / s390.c
blobb5c0bd13f3b30890e802bd6d510cac8843f66df1
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).
7 This file is part of GCC.
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.
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.
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/>. */
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 "attribs.h"
39 #include "expmed.h"
40 #include "optabs.h"
41 #include "regs.h"
42 #include "emit-rtl.h"
43 #include "recog.h"
44 #include "cgraph.h"
45 #include "diagnostic-core.h"
46 #include "diagnostic.h"
47 #include "alias.h"
48 #include "fold-const.h"
49 #include "print-tree.h"
50 #include "stor-layout.h"
51 #include "varasm.h"
52 #include "calls.h"
53 #include "conditions.h"
54 #include "output.h"
55 #include "insn-attr.h"
56 #include "flags.h"
57 #include "except.h"
58 #include "dojump.h"
59 #include "explow.h"
60 #include "stmt.h"
61 #include "expr.h"
62 #include "reload.h"
63 #include "cfgrtl.h"
64 #include "cfganal.h"
65 #include "lcm.h"
66 #include "cfgbuild.h"
67 #include "cfgcleanup.h"
68 #include "debug.h"
69 #include "langhooks.h"
70 #include "internal-fn.h"
71 #include "gimple-fold.h"
72 #include "tree-eh.h"
73 #include "gimplify.h"
74 #include "params.h"
75 #include "opts.h"
76 #include "tree-pass.h"
77 #include "context.h"
78 #include "builtins.h"
79 #include "rtl-iter.h"
80 #include "intl.h"
81 #include "tm-constrs.h"
83 /* This file should be included last. */
84 #include "target-def.h"
86 /* Remember the last target of s390_set_current_function. */
87 static GTY(()) tree s390_previous_fndecl;
89 /* Define the specific costs for a given cpu. */
91 struct processor_costs
93 /* multiplication */
94 const int m; /* cost of an M instruction. */
95 const int mghi; /* cost of an MGHI instruction. */
96 const int mh; /* cost of an MH instruction. */
97 const int mhi; /* cost of an MHI instruction. */
98 const int ml; /* cost of an ML instruction. */
99 const int mr; /* cost of an MR instruction. */
100 const int ms; /* cost of an MS instruction. */
101 const int msg; /* cost of an MSG instruction. */
102 const int msgf; /* cost of an MSGF instruction. */
103 const int msgfr; /* cost of an MSGFR instruction. */
104 const int msgr; /* cost of an MSGR instruction. */
105 const int msr; /* cost of an MSR instruction. */
106 const int mult_df; /* cost of multiplication in DFmode. */
107 const int mxbr;
108 /* square root */
109 const int sqxbr; /* cost of square root in TFmode. */
110 const int sqdbr; /* cost of square root in DFmode. */
111 const int sqebr; /* cost of square root in SFmode. */
112 /* multiply and add */
113 const int madbr; /* cost of multiply and add in DFmode. */
114 const int maebr; /* cost of multiply and add in SFmode. */
115 /* division */
116 const int dxbr;
117 const int ddbr;
118 const int debr;
119 const int dlgr;
120 const int dlr;
121 const int dr;
122 const int dsgfr;
123 const int dsgr;
126 #define s390_cost ((const struct processor_costs *)(s390_cost_pointer))
128 static const
129 struct processor_costs z900_cost =
131 COSTS_N_INSNS (5), /* M */
132 COSTS_N_INSNS (10), /* MGHI */
133 COSTS_N_INSNS (5), /* MH */
134 COSTS_N_INSNS (4), /* MHI */
135 COSTS_N_INSNS (5), /* ML */
136 COSTS_N_INSNS (5), /* MR */
137 COSTS_N_INSNS (4), /* MS */
138 COSTS_N_INSNS (15), /* MSG */
139 COSTS_N_INSNS (7), /* MSGF */
140 COSTS_N_INSNS (7), /* MSGFR */
141 COSTS_N_INSNS (10), /* MSGR */
142 COSTS_N_INSNS (4), /* MSR */
143 COSTS_N_INSNS (7), /* multiplication in DFmode */
144 COSTS_N_INSNS (13), /* MXBR */
145 COSTS_N_INSNS (136), /* SQXBR */
146 COSTS_N_INSNS (44), /* SQDBR */
147 COSTS_N_INSNS (35), /* SQEBR */
148 COSTS_N_INSNS (18), /* MADBR */
149 COSTS_N_INSNS (13), /* MAEBR */
150 COSTS_N_INSNS (134), /* DXBR */
151 COSTS_N_INSNS (30), /* DDBR */
152 COSTS_N_INSNS (27), /* DEBR */
153 COSTS_N_INSNS (220), /* DLGR */
154 COSTS_N_INSNS (34), /* DLR */
155 COSTS_N_INSNS (34), /* DR */
156 COSTS_N_INSNS (32), /* DSGFR */
157 COSTS_N_INSNS (32), /* DSGR */
160 static const
161 struct processor_costs z990_cost =
163 COSTS_N_INSNS (4), /* M */
164 COSTS_N_INSNS (2), /* MGHI */
165 COSTS_N_INSNS (2), /* MH */
166 COSTS_N_INSNS (2), /* MHI */
167 COSTS_N_INSNS (4), /* ML */
168 COSTS_N_INSNS (4), /* MR */
169 COSTS_N_INSNS (5), /* MS */
170 COSTS_N_INSNS (6), /* MSG */
171 COSTS_N_INSNS (4), /* MSGF */
172 COSTS_N_INSNS (4), /* MSGFR */
173 COSTS_N_INSNS (4), /* MSGR */
174 COSTS_N_INSNS (4), /* MSR */
175 COSTS_N_INSNS (1), /* multiplication in DFmode */
176 COSTS_N_INSNS (28), /* MXBR */
177 COSTS_N_INSNS (130), /* SQXBR */
178 COSTS_N_INSNS (66), /* SQDBR */
179 COSTS_N_INSNS (38), /* SQEBR */
180 COSTS_N_INSNS (1), /* MADBR */
181 COSTS_N_INSNS (1), /* MAEBR */
182 COSTS_N_INSNS (60), /* DXBR */
183 COSTS_N_INSNS (40), /* DDBR */
184 COSTS_N_INSNS (26), /* DEBR */
185 COSTS_N_INSNS (176), /* DLGR */
186 COSTS_N_INSNS (31), /* DLR */
187 COSTS_N_INSNS (31), /* DR */
188 COSTS_N_INSNS (31), /* DSGFR */
189 COSTS_N_INSNS (31), /* DSGR */
192 static const
193 struct processor_costs z9_109_cost =
195 COSTS_N_INSNS (4), /* M */
196 COSTS_N_INSNS (2), /* MGHI */
197 COSTS_N_INSNS (2), /* MH */
198 COSTS_N_INSNS (2), /* MHI */
199 COSTS_N_INSNS (4), /* ML */
200 COSTS_N_INSNS (4), /* MR */
201 COSTS_N_INSNS (5), /* MS */
202 COSTS_N_INSNS (6), /* MSG */
203 COSTS_N_INSNS (4), /* MSGF */
204 COSTS_N_INSNS (4), /* MSGFR */
205 COSTS_N_INSNS (4), /* MSGR */
206 COSTS_N_INSNS (4), /* MSR */
207 COSTS_N_INSNS (1), /* multiplication in DFmode */
208 COSTS_N_INSNS (28), /* MXBR */
209 COSTS_N_INSNS (130), /* SQXBR */
210 COSTS_N_INSNS (66), /* SQDBR */
211 COSTS_N_INSNS (38), /* SQEBR */
212 COSTS_N_INSNS (1), /* MADBR */
213 COSTS_N_INSNS (1), /* MAEBR */
214 COSTS_N_INSNS (60), /* DXBR */
215 COSTS_N_INSNS (40), /* DDBR */
216 COSTS_N_INSNS (26), /* DEBR */
217 COSTS_N_INSNS (30), /* DLGR */
218 COSTS_N_INSNS (23), /* DLR */
219 COSTS_N_INSNS (23), /* DR */
220 COSTS_N_INSNS (24), /* DSGFR */
221 COSTS_N_INSNS (24), /* DSGR */
224 static const
225 struct processor_costs z10_cost =
227 COSTS_N_INSNS (10), /* M */
228 COSTS_N_INSNS (10), /* MGHI */
229 COSTS_N_INSNS (10), /* MH */
230 COSTS_N_INSNS (10), /* MHI */
231 COSTS_N_INSNS (10), /* ML */
232 COSTS_N_INSNS (10), /* MR */
233 COSTS_N_INSNS (10), /* MS */
234 COSTS_N_INSNS (10), /* MSG */
235 COSTS_N_INSNS (10), /* MSGF */
236 COSTS_N_INSNS (10), /* MSGFR */
237 COSTS_N_INSNS (10), /* MSGR */
238 COSTS_N_INSNS (10), /* MSR */
239 COSTS_N_INSNS (1) , /* multiplication in DFmode */
240 COSTS_N_INSNS (50), /* MXBR */
241 COSTS_N_INSNS (120), /* SQXBR */
242 COSTS_N_INSNS (52), /* SQDBR */
243 COSTS_N_INSNS (38), /* SQEBR */
244 COSTS_N_INSNS (1), /* MADBR */
245 COSTS_N_INSNS (1), /* MAEBR */
246 COSTS_N_INSNS (111), /* DXBR */
247 COSTS_N_INSNS (39), /* DDBR */
248 COSTS_N_INSNS (32), /* DEBR */
249 COSTS_N_INSNS (160), /* DLGR */
250 COSTS_N_INSNS (71), /* DLR */
251 COSTS_N_INSNS (71), /* DR */
252 COSTS_N_INSNS (71), /* DSGFR */
253 COSTS_N_INSNS (71), /* DSGR */
256 static const
257 struct processor_costs z196_cost =
259 COSTS_N_INSNS (7), /* M */
260 COSTS_N_INSNS (5), /* MGHI */
261 COSTS_N_INSNS (5), /* MH */
262 COSTS_N_INSNS (5), /* MHI */
263 COSTS_N_INSNS (7), /* ML */
264 COSTS_N_INSNS (7), /* MR */
265 COSTS_N_INSNS (6), /* MS */
266 COSTS_N_INSNS (8), /* MSG */
267 COSTS_N_INSNS (6), /* MSGF */
268 COSTS_N_INSNS (6), /* MSGFR */
269 COSTS_N_INSNS (8), /* MSGR */
270 COSTS_N_INSNS (6), /* MSR */
271 COSTS_N_INSNS (1) , /* multiplication in DFmode */
272 COSTS_N_INSNS (40), /* MXBR B+40 */
273 COSTS_N_INSNS (100), /* SQXBR B+100 */
274 COSTS_N_INSNS (42), /* SQDBR B+42 */
275 COSTS_N_INSNS (28), /* SQEBR B+28 */
276 COSTS_N_INSNS (1), /* MADBR B */
277 COSTS_N_INSNS (1), /* MAEBR B */
278 COSTS_N_INSNS (101), /* DXBR B+101 */
279 COSTS_N_INSNS (29), /* DDBR */
280 COSTS_N_INSNS (22), /* DEBR */
281 COSTS_N_INSNS (160), /* DLGR cracked */
282 COSTS_N_INSNS (160), /* DLR cracked */
283 COSTS_N_INSNS (160), /* DR expanded */
284 COSTS_N_INSNS (160), /* DSGFR cracked */
285 COSTS_N_INSNS (160), /* DSGR cracked */
288 static const
289 struct processor_costs zEC12_cost =
291 COSTS_N_INSNS (7), /* M */
292 COSTS_N_INSNS (5), /* MGHI */
293 COSTS_N_INSNS (5), /* MH */
294 COSTS_N_INSNS (5), /* MHI */
295 COSTS_N_INSNS (7), /* ML */
296 COSTS_N_INSNS (7), /* MR */
297 COSTS_N_INSNS (6), /* MS */
298 COSTS_N_INSNS (8), /* MSG */
299 COSTS_N_INSNS (6), /* MSGF */
300 COSTS_N_INSNS (6), /* MSGFR */
301 COSTS_N_INSNS (8), /* MSGR */
302 COSTS_N_INSNS (6), /* MSR */
303 COSTS_N_INSNS (1) , /* multiplication in DFmode */
304 COSTS_N_INSNS (40), /* MXBR B+40 */
305 COSTS_N_INSNS (100), /* SQXBR B+100 */
306 COSTS_N_INSNS (42), /* SQDBR B+42 */
307 COSTS_N_INSNS (28), /* SQEBR B+28 */
308 COSTS_N_INSNS (1), /* MADBR B */
309 COSTS_N_INSNS (1), /* MAEBR B */
310 COSTS_N_INSNS (131), /* DXBR B+131 */
311 COSTS_N_INSNS (29), /* DDBR */
312 COSTS_N_INSNS (22), /* DEBR */
313 COSTS_N_INSNS (160), /* DLGR cracked */
314 COSTS_N_INSNS (160), /* DLR cracked */
315 COSTS_N_INSNS (160), /* DR expanded */
316 COSTS_N_INSNS (160), /* DSGFR cracked */
317 COSTS_N_INSNS (160), /* DSGR cracked */
320 static struct
322 /* The preferred name to be used in user visible output. */
323 const char *const name;
324 /* CPU name as it should be passed to Binutils via .machine */
325 const char *const binutils_name;
326 const enum processor_type processor;
327 const struct processor_costs *cost;
329 const processor_table[] =
331 { "g5", "g5", PROCESSOR_9672_G5, &z900_cost },
332 { "g6", "g6", PROCESSOR_9672_G6, &z900_cost },
333 { "z900", "z900", PROCESSOR_2064_Z900, &z900_cost },
334 { "z990", "z990", PROCESSOR_2084_Z990, &z990_cost },
335 { "z9-109", "z9-109", PROCESSOR_2094_Z9_109, &z9_109_cost },
336 { "z9-ec", "z9-ec", PROCESSOR_2094_Z9_EC, &z9_109_cost },
337 { "z10", "z10", PROCESSOR_2097_Z10, &z10_cost },
338 { "z196", "z196", PROCESSOR_2817_Z196, &z196_cost },
339 { "zEC12", "zEC12", PROCESSOR_2827_ZEC12, &zEC12_cost },
340 { "z13", "z13", PROCESSOR_2964_Z13, &zEC12_cost },
341 { "z14", "arch12", PROCESSOR_3906_Z14, &zEC12_cost },
342 { "native", "", PROCESSOR_NATIVE, NULL }
345 extern int reload_completed;
347 /* Kept up to date using the SCHED_VARIABLE_ISSUE hook. */
348 static rtx_insn *last_scheduled_insn;
349 #define MAX_SCHED_UNITS 3
350 static int last_scheduled_unit_distance[MAX_SCHED_UNITS];
352 /* The maximum score added for an instruction whose unit hasn't been
353 in use for MAX_SCHED_MIX_DISTANCE steps. Increase this value to
354 give instruction mix scheduling more priority over instruction
355 grouping. */
356 #define MAX_SCHED_MIX_SCORE 8
358 /* The maximum distance up to which individual scores will be
359 calculated. Everything beyond this gives MAX_SCHED_MIX_SCORE.
360 Increase this with the OOO windows size of the machine. */
361 #define MAX_SCHED_MIX_DISTANCE 100
363 /* Structure used to hold the components of a S/390 memory
364 address. A legitimate address on S/390 is of the general
365 form
366 base + index + displacement
367 where any of the components is optional.
369 base and index are registers of the class ADDR_REGS,
370 displacement is an unsigned 12-bit immediate constant. */
372 struct s390_address
374 rtx base;
375 rtx indx;
376 rtx disp;
377 bool pointer;
378 bool literal_pool;
381 /* The following structure is embedded in the machine
382 specific part of struct function. */
384 struct GTY (()) s390_frame_layout
386 /* Offset within stack frame. */
387 HOST_WIDE_INT gprs_offset;
388 HOST_WIDE_INT f0_offset;
389 HOST_WIDE_INT f4_offset;
390 HOST_WIDE_INT f8_offset;
391 HOST_WIDE_INT backchain_offset;
393 /* Number of first and last gpr where slots in the register
394 save area are reserved for. */
395 int first_save_gpr_slot;
396 int last_save_gpr_slot;
398 /* Location (FP register number) where GPRs (r0-r15) should
399 be saved to.
400 0 - does not need to be saved at all
401 -1 - stack slot */
402 #define SAVE_SLOT_NONE 0
403 #define SAVE_SLOT_STACK -1
404 signed char gpr_save_slots[16];
406 /* Number of first and last gpr to be saved, restored. */
407 int first_save_gpr;
408 int first_restore_gpr;
409 int last_save_gpr;
410 int last_restore_gpr;
412 /* Bits standing for floating point registers. Set, if the
413 respective register has to be saved. Starting with reg 16 (f0)
414 at the rightmost bit.
415 Bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
416 fpr 15 13 11 9 14 12 10 8 7 5 3 1 6 4 2 0
417 reg 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 */
418 unsigned int fpr_bitmap;
420 /* Number of floating point registers f8-f15 which must be saved. */
421 int high_fprs;
423 /* Set if return address needs to be saved.
424 This flag is set by s390_return_addr_rtx if it could not use
425 the initial value of r14 and therefore depends on r14 saved
426 to the stack. */
427 bool save_return_addr_p;
429 /* Size of stack frame. */
430 HOST_WIDE_INT frame_size;
433 /* Define the structure for the machine field in struct function. */
435 struct GTY(()) machine_function
437 struct s390_frame_layout frame_layout;
439 /* Literal pool base register. */
440 rtx base_reg;
442 /* True if we may need to perform branch splitting. */
443 bool split_branches_pending_p;
445 bool has_landing_pad_p;
447 /* True if the current function may contain a tbegin clobbering
448 FPRs. */
449 bool tbegin_p;
451 /* For -fsplit-stack support: A stack local which holds a pointer to
452 the stack arguments for a function with a variable number of
453 arguments. This is set at the start of the function and is used
454 to initialize the overflow_arg_area field of the va_list
455 structure. */
456 rtx split_stack_varargs_pointer;
459 /* Few accessor macros for struct cfun->machine->s390_frame_layout. */
461 #define cfun_frame_layout (cfun->machine->frame_layout)
462 #define cfun_save_high_fprs_p (!!cfun_frame_layout.high_fprs)
463 #define cfun_save_arg_fprs_p (!!(TARGET_64BIT \
464 ? cfun_frame_layout.fpr_bitmap & 0x0f \
465 : cfun_frame_layout.fpr_bitmap & 0x03))
466 #define cfun_gprs_save_area_size ((cfun_frame_layout.last_save_gpr_slot - \
467 cfun_frame_layout.first_save_gpr_slot + 1) * UNITS_PER_LONG)
468 #define cfun_set_fpr_save(REGNO) (cfun->machine->frame_layout.fpr_bitmap |= \
469 (1 << (REGNO - FPR0_REGNUM)))
470 #define cfun_fpr_save_p(REGNO) (!!(cfun->machine->frame_layout.fpr_bitmap & \
471 (1 << (REGNO - FPR0_REGNUM))))
472 #define cfun_gpr_save_slot(REGNO) \
473 cfun->machine->frame_layout.gpr_save_slots[REGNO]
475 /* Number of GPRs and FPRs used for argument passing. */
476 #define GP_ARG_NUM_REG 5
477 #define FP_ARG_NUM_REG (TARGET_64BIT? 4 : 2)
478 #define VEC_ARG_NUM_REG 8
480 /* A couple of shortcuts. */
481 #define CONST_OK_FOR_J(x) \
482 CONST_OK_FOR_CONSTRAINT_P((x), 'J', "J")
483 #define CONST_OK_FOR_K(x) \
484 CONST_OK_FOR_CONSTRAINT_P((x), 'K', "K")
485 #define CONST_OK_FOR_Os(x) \
486 CONST_OK_FOR_CONSTRAINT_P((x), 'O', "Os")
487 #define CONST_OK_FOR_Op(x) \
488 CONST_OK_FOR_CONSTRAINT_P((x), 'O', "Op")
489 #define CONST_OK_FOR_On(x) \
490 CONST_OK_FOR_CONSTRAINT_P((x), 'O', "On")
492 #define REGNO_PAIR_OK(REGNO, MODE) \
493 (HARD_REGNO_NREGS ((REGNO), (MODE)) == 1 || !((REGNO) & 1))
495 /* That's the read ahead of the dynamic branch prediction unit in
496 bytes on a z10 (or higher) CPU. */
497 #define PREDICT_DISTANCE (TARGET_Z10 ? 384 : 2048)
500 /* Indicate which ABI has been used for passing vector args.
501 0 - no vector type arguments have been passed where the ABI is relevant
502 1 - the old ABI has been used
503 2 - a vector type argument has been passed either in a vector register
504 or on the stack by value */
505 static int s390_vector_abi = 0;
507 /* Set the vector ABI marker if TYPE is subject to the vector ABI
508 switch. The vector ABI affects only vector data types. There are
509 two aspects of the vector ABI relevant here:
511 1. vectors >= 16 bytes have an alignment of 8 bytes with the new
512 ABI and natural alignment with the old.
514 2. vector <= 16 bytes are passed in VRs or by value on the stack
515 with the new ABI but by reference on the stack with the old.
517 If ARG_P is true TYPE is used for a function argument or return
518 value. The ABI marker then is set for all vector data types. If
519 ARG_P is false only type 1 vectors are being checked. */
521 static void
522 s390_check_type_for_vector_abi (const_tree type, bool arg_p, bool in_struct_p)
524 static hash_set<const_tree> visited_types_hash;
526 if (s390_vector_abi)
527 return;
529 if (type == NULL_TREE || TREE_CODE (type) == ERROR_MARK)
530 return;
532 if (visited_types_hash.contains (type))
533 return;
535 visited_types_hash.add (type);
537 if (VECTOR_TYPE_P (type))
539 int type_size = int_size_in_bytes (type);
541 /* Outside arguments only the alignment is changing and this
542 only happens for vector types >= 16 bytes. */
543 if (!arg_p && type_size < 16)
544 return;
546 /* In arguments vector types > 16 are passed as before (GCC
547 never enforced the bigger alignment for arguments which was
548 required by the old vector ABI). However, it might still be
549 ABI relevant due to the changed alignment if it is a struct
550 member. */
551 if (arg_p && type_size > 16 && !in_struct_p)
552 return;
554 s390_vector_abi = TARGET_VX_ABI ? 2 : 1;
556 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
558 /* ARRAY_TYPE: Since with neither of the ABIs we have more than
559 natural alignment there will never be ABI dependent padding
560 in an array type. That's why we do not set in_struct_p to
561 true here. */
562 s390_check_type_for_vector_abi (TREE_TYPE (type), arg_p, in_struct_p);
564 else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
566 tree arg_chain;
568 /* Check the return type. */
569 s390_check_type_for_vector_abi (TREE_TYPE (type), true, false);
571 for (arg_chain = TYPE_ARG_TYPES (type);
572 arg_chain;
573 arg_chain = TREE_CHAIN (arg_chain))
574 s390_check_type_for_vector_abi (TREE_VALUE (arg_chain), true, false);
576 else if (RECORD_OR_UNION_TYPE_P (type))
578 tree field;
580 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
582 if (TREE_CODE (field) != FIELD_DECL)
583 continue;
585 s390_check_type_for_vector_abi (TREE_TYPE (field), arg_p, true);
591 /* System z builtins. */
593 #include "s390-builtins.h"
595 const unsigned int bflags_builtin[S390_BUILTIN_MAX + 1] =
597 #undef B_DEF
598 #undef OB_DEF
599 #undef OB_DEF_VAR
600 #define B_DEF(NAME, PATTERN, ATTRS, BFLAGS, ...) BFLAGS,
601 #define OB_DEF(...)
602 #define OB_DEF_VAR(...)
603 #include "s390-builtins.def"
607 const unsigned int opflags_builtin[S390_BUILTIN_MAX + 1] =
609 #undef B_DEF
610 #undef OB_DEF
611 #undef OB_DEF_VAR
612 #define B_DEF(NAME, PATTERN, ATTRS, BFLAGS, OPFLAGS, ...) OPFLAGS,
613 #define OB_DEF(...)
614 #define OB_DEF_VAR(...)
615 #include "s390-builtins.def"
619 const unsigned int bflags_overloaded_builtin[S390_OVERLOADED_BUILTIN_MAX + 1] =
621 #undef B_DEF
622 #undef OB_DEF
623 #undef OB_DEF_VAR
624 #define B_DEF(...)
625 #define OB_DEF(NAME, FIRST_VAR_NAME, LAST_VAR_NAME, BFLAGS, ...) BFLAGS,
626 #define OB_DEF_VAR(...)
627 #include "s390-builtins.def"
631 const unsigned int
632 bflags_overloaded_builtin_var[S390_OVERLOADED_BUILTIN_VAR_MAX + 1] =
634 #undef B_DEF
635 #undef OB_DEF
636 #undef OB_DEF_VAR
637 #define B_DEF(...)
638 #define OB_DEF(...)
639 #define OB_DEF_VAR(NAME, PATTERN, FLAGS, OPFLAGS, FNTYPE) FLAGS,
640 #include "s390-builtins.def"
644 const unsigned int
645 opflags_overloaded_builtin_var[S390_OVERLOADED_BUILTIN_VAR_MAX + 1] =
647 #undef B_DEF
648 #undef OB_DEF
649 #undef OB_DEF_VAR
650 #define B_DEF(...)
651 #define OB_DEF(...)
652 #define OB_DEF_VAR(NAME, PATTERN, FLAGS, OPFLAGS, FNTYPE) OPFLAGS,
653 #include "s390-builtins.def"
657 tree s390_builtin_types[BT_MAX];
658 tree s390_builtin_fn_types[BT_FN_MAX];
659 tree s390_builtin_decls[S390_BUILTIN_MAX +
660 S390_OVERLOADED_BUILTIN_MAX +
661 S390_OVERLOADED_BUILTIN_VAR_MAX];
663 static enum insn_code const code_for_builtin[S390_BUILTIN_MAX + 1] = {
664 #undef B_DEF
665 #undef OB_DEF
666 #undef OB_DEF_VAR
667 #define B_DEF(NAME, PATTERN, ...) CODE_FOR_##PATTERN,
668 #define OB_DEF(...)
669 #define OB_DEF_VAR(...)
671 #include "s390-builtins.def"
672 CODE_FOR_nothing
675 static void
676 s390_init_builtins (void)
678 /* These definitions are being used in s390-builtins.def. */
679 tree returns_twice_attr = tree_cons (get_identifier ("returns_twice"),
680 NULL, NULL);
681 tree noreturn_attr = tree_cons (get_identifier ("noreturn"), NULL, NULL);
682 tree c_uint64_type_node;
684 /* The uint64_type_node from tree.c is not compatible to the C99
685 uint64_t data type. What we want is c_uint64_type_node from
686 c-common.c. But since backend code is not supposed to interface
687 with the frontend we recreate it here. */
688 if (TARGET_64BIT)
689 c_uint64_type_node = long_unsigned_type_node;
690 else
691 c_uint64_type_node = long_long_unsigned_type_node;
693 #undef DEF_TYPE
694 #define DEF_TYPE(INDEX, NODE, CONST_P) \
695 if (s390_builtin_types[INDEX] == NULL) \
696 s390_builtin_types[INDEX] = (!CONST_P) ? \
697 (NODE) : build_type_variant ((NODE), 1, 0);
699 #undef DEF_POINTER_TYPE
700 #define DEF_POINTER_TYPE(INDEX, INDEX_BASE) \
701 if (s390_builtin_types[INDEX] == NULL) \
702 s390_builtin_types[INDEX] = \
703 build_pointer_type (s390_builtin_types[INDEX_BASE]);
705 #undef DEF_DISTINCT_TYPE
706 #define DEF_DISTINCT_TYPE(INDEX, INDEX_BASE) \
707 if (s390_builtin_types[INDEX] == NULL) \
708 s390_builtin_types[INDEX] = \
709 build_distinct_type_copy (s390_builtin_types[INDEX_BASE]);
711 #undef DEF_VECTOR_TYPE
712 #define DEF_VECTOR_TYPE(INDEX, INDEX_BASE, ELEMENTS) \
713 if (s390_builtin_types[INDEX] == NULL) \
714 s390_builtin_types[INDEX] = \
715 build_vector_type (s390_builtin_types[INDEX_BASE], ELEMENTS);
717 #undef DEF_OPAQUE_VECTOR_TYPE
718 #define DEF_OPAQUE_VECTOR_TYPE(INDEX, INDEX_BASE, ELEMENTS) \
719 if (s390_builtin_types[INDEX] == NULL) \
720 s390_builtin_types[INDEX] = \
721 build_opaque_vector_type (s390_builtin_types[INDEX_BASE], ELEMENTS);
723 #undef DEF_FN_TYPE
724 #define DEF_FN_TYPE(INDEX, args...) \
725 if (s390_builtin_fn_types[INDEX] == NULL) \
726 s390_builtin_fn_types[INDEX] = \
727 build_function_type_list (args, NULL_TREE);
728 #undef DEF_OV_TYPE
729 #define DEF_OV_TYPE(...)
730 #include "s390-builtin-types.def"
732 #undef B_DEF
733 #define B_DEF(NAME, PATTERN, ATTRS, BFLAGS, OPFLAGS, FNTYPE) \
734 if (s390_builtin_decls[S390_BUILTIN_##NAME] == NULL) \
735 s390_builtin_decls[S390_BUILTIN_##NAME] = \
736 add_builtin_function ("__builtin_" #NAME, \
737 s390_builtin_fn_types[FNTYPE], \
738 S390_BUILTIN_##NAME, \
739 BUILT_IN_MD, \
740 NULL, \
741 ATTRS);
742 #undef OB_DEF
743 #define OB_DEF(NAME, FIRST_VAR_NAME, LAST_VAR_NAME, BFLAGS, FNTYPE) \
744 if (s390_builtin_decls[S390_OVERLOADED_BUILTIN_##NAME + S390_BUILTIN_MAX] \
745 == NULL) \
746 s390_builtin_decls[S390_OVERLOADED_BUILTIN_##NAME + S390_BUILTIN_MAX] = \
747 add_builtin_function ("__builtin_" #NAME, \
748 s390_builtin_fn_types[FNTYPE], \
749 S390_OVERLOADED_BUILTIN_##NAME + S390_BUILTIN_MAX, \
750 BUILT_IN_MD, \
751 NULL, \
753 #undef OB_DEF_VAR
754 #define OB_DEF_VAR(...)
755 #include "s390-builtins.def"
759 /* Return true if ARG is appropriate as argument number ARGNUM of
760 builtin DECL. The operand flags from s390-builtins.def have to
761 passed as OP_FLAGS. */
762 bool
763 s390_const_operand_ok (tree arg, int argnum, int op_flags, tree decl)
765 if (O_UIMM_P (op_flags))
767 int bitwidths[] = { 1, 2, 3, 4, 5, 8, 12, 16, 32 };
768 int bitwidth = bitwidths[op_flags - O_U1];
770 if (!tree_fits_uhwi_p (arg)
771 || tree_to_uhwi (arg) > (HOST_WIDE_INT_1U << bitwidth) - 1)
773 error("constant argument %d for builtin %qF is out of range (0.."
774 HOST_WIDE_INT_PRINT_UNSIGNED ")",
775 argnum, decl,
776 (HOST_WIDE_INT_1U << bitwidth) - 1);
777 return false;
781 if (O_SIMM_P (op_flags))
783 int bitwidths[] = { 2, 3, 4, 5, 8, 12, 16, 32 };
784 int bitwidth = bitwidths[op_flags - O_S2];
786 if (!tree_fits_shwi_p (arg)
787 || tree_to_shwi (arg) < -(HOST_WIDE_INT_1 << (bitwidth - 1))
788 || tree_to_shwi (arg) > ((HOST_WIDE_INT_1 << (bitwidth - 1)) - 1))
790 error("constant argument %d for builtin %qF is out of range ("
791 HOST_WIDE_INT_PRINT_DEC ".."
792 HOST_WIDE_INT_PRINT_DEC ")",
793 argnum, decl,
794 -(HOST_WIDE_INT_1 << (bitwidth - 1)),
795 (HOST_WIDE_INT_1 << (bitwidth - 1)) - 1);
796 return false;
799 return true;
802 /* Expand an expression EXP that calls a built-in function,
803 with result going to TARGET if that's convenient
804 (and in mode MODE if that's convenient).
805 SUBTARGET may be used as the target for computing one of EXP's operands.
806 IGNORE is nonzero if the value is to be ignored. */
808 static rtx
809 s390_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
810 machine_mode mode ATTRIBUTE_UNUSED,
811 int ignore ATTRIBUTE_UNUSED)
813 #define MAX_ARGS 6
815 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
816 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
817 enum insn_code icode;
818 rtx op[MAX_ARGS], pat;
819 int arity;
820 bool nonvoid;
821 tree arg;
822 call_expr_arg_iterator iter;
823 unsigned int all_op_flags = opflags_for_builtin (fcode);
824 machine_mode last_vec_mode = VOIDmode;
826 if (TARGET_DEBUG_ARG)
828 fprintf (stderr,
829 "s390_expand_builtin, code = %4d, %s, bflags = 0x%x\n",
830 (int)fcode, IDENTIFIER_POINTER (DECL_NAME (fndecl)),
831 bflags_for_builtin (fcode));
834 if (S390_USE_TARGET_ATTRIBUTE)
836 unsigned int bflags;
838 bflags = bflags_for_builtin (fcode);
839 if ((bflags & B_HTM) && !TARGET_HTM)
841 error ("builtin %qF is not supported without -mhtm "
842 "(default with -march=zEC12 and higher).", fndecl);
843 return const0_rtx;
845 if (((bflags & B_VX) || (bflags & B_VXE)) && !TARGET_VX)
847 error ("builtin %qF requires -mvx "
848 "(default with -march=z13 and higher).", fndecl);
849 return const0_rtx;
852 if ((bflags & B_VXE) && !TARGET_VXE)
854 error ("Builtin %qF requires z14 or higher.", fndecl);
855 return const0_rtx;
858 if (fcode >= S390_OVERLOADED_BUILTIN_VAR_OFFSET
859 && fcode < S390_ALL_BUILTIN_MAX)
861 gcc_unreachable ();
863 else if (fcode < S390_OVERLOADED_BUILTIN_OFFSET)
865 icode = code_for_builtin[fcode];
866 /* Set a flag in the machine specific cfun part in order to support
867 saving/restoring of FPRs. */
868 if (fcode == S390_BUILTIN_tbegin || fcode == S390_BUILTIN_tbegin_retry)
869 cfun->machine->tbegin_p = true;
871 else if (fcode < S390_OVERLOADED_BUILTIN_VAR_OFFSET)
873 error ("unresolved overloaded builtin");
874 return const0_rtx;
876 else
877 internal_error ("bad builtin fcode");
879 if (icode == 0)
880 internal_error ("bad builtin icode");
882 nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
884 if (nonvoid)
886 machine_mode tmode = insn_data[icode].operand[0].mode;
887 if (!target
888 || GET_MODE (target) != tmode
889 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
890 target = gen_reg_rtx (tmode);
892 /* There are builtins (e.g. vec_promote) with no vector
893 arguments but an element selector. So we have to also look
894 at the vector return type when emitting the modulo
895 operation. */
896 if (VECTOR_MODE_P (insn_data[icode].operand[0].mode))
897 last_vec_mode = insn_data[icode].operand[0].mode;
900 arity = 0;
901 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
903 rtx tmp_rtx;
904 const struct insn_operand_data *insn_op;
905 unsigned int op_flags = all_op_flags & ((1 << O_SHIFT) - 1);
907 all_op_flags = all_op_flags >> O_SHIFT;
909 if (arg == error_mark_node)
910 return NULL_RTX;
911 if (arity >= MAX_ARGS)
912 return NULL_RTX;
914 if (O_IMM_P (op_flags)
915 && TREE_CODE (arg) != INTEGER_CST)
917 error ("constant value required for builtin %qF argument %d",
918 fndecl, arity + 1);
919 return const0_rtx;
922 if (!s390_const_operand_ok (arg, arity + 1, op_flags, fndecl))
923 return const0_rtx;
925 insn_op = &insn_data[icode].operand[arity + nonvoid];
926 op[arity] = expand_expr (arg, NULL_RTX, insn_op->mode, EXPAND_NORMAL);
928 /* expand_expr truncates constants to the target mode only if it
929 is "convenient". However, our checks below rely on this
930 being done. */
931 if (CONST_INT_P (op[arity])
932 && SCALAR_INT_MODE_P (insn_op->mode)
933 && GET_MODE (op[arity]) != insn_op->mode)
934 op[arity] = GEN_INT (trunc_int_for_mode (INTVAL (op[arity]),
935 insn_op->mode));
937 /* Wrap the expanded RTX for pointer types into a MEM expr with
938 the proper mode. This allows us to use e.g. (match_operand
939 "memory_operand"..) in the insn patterns instead of (mem
940 (match_operand "address_operand)). This is helpful for
941 patterns not just accepting MEMs. */
942 if (POINTER_TYPE_P (TREE_TYPE (arg))
943 && insn_op->predicate != address_operand)
944 op[arity] = gen_rtx_MEM (insn_op->mode, op[arity]);
946 /* Expand the module operation required on element selectors. */
947 if (op_flags == O_ELEM)
949 gcc_assert (last_vec_mode != VOIDmode);
950 op[arity] = simplify_expand_binop (SImode, code_to_optab (AND),
951 op[arity],
952 GEN_INT (GET_MODE_NUNITS (last_vec_mode) - 1),
953 NULL_RTX, 1, OPTAB_DIRECT);
956 /* Record the vector mode used for an element selector. This assumes:
957 1. There is no builtin with two different vector modes and an element selector
958 2. The element selector comes after the vector type it is referring to.
959 This currently the true for all the builtins but FIXME we
960 should better check for that. */
961 if (VECTOR_MODE_P (insn_op->mode))
962 last_vec_mode = insn_op->mode;
964 if (insn_op->predicate (op[arity], insn_op->mode))
966 arity++;
967 continue;
970 if (MEM_P (op[arity])
971 && insn_op->predicate == memory_operand
972 && (GET_MODE (XEXP (op[arity], 0)) == Pmode
973 || GET_MODE (XEXP (op[arity], 0)) == VOIDmode))
975 op[arity] = replace_equiv_address (op[arity],
976 copy_to_mode_reg (Pmode,
977 XEXP (op[arity], 0)));
979 /* Some of the builtins require different modes/types than the
980 pattern in order to implement a specific API. Instead of
981 adding many expanders which do the mode change we do it here.
982 E.g. s390_vec_add_u128 required to have vector unsigned char
983 arguments is mapped to addti3. */
984 else if (insn_op->mode != VOIDmode
985 && GET_MODE (op[arity]) != VOIDmode
986 && GET_MODE (op[arity]) != insn_op->mode
987 && ((tmp_rtx = simplify_gen_subreg (insn_op->mode, op[arity],
988 GET_MODE (op[arity]), 0))
989 != NULL_RTX))
991 op[arity] = tmp_rtx;
993 else if (GET_MODE (op[arity]) == insn_op->mode
994 || GET_MODE (op[arity]) == VOIDmode
995 || (insn_op->predicate == address_operand
996 && GET_MODE (op[arity]) == Pmode))
998 /* An address_operand usually has VOIDmode in the expander
999 so we cannot use this. */
1000 machine_mode target_mode =
1001 (insn_op->predicate == address_operand
1002 ? (machine_mode) Pmode : insn_op->mode);
1003 op[arity] = copy_to_mode_reg (target_mode, op[arity]);
1006 if (!insn_op->predicate (op[arity], insn_op->mode))
1008 error ("invalid argument %d for builtin %qF", arity + 1, fndecl);
1009 return const0_rtx;
1011 arity++;
1014 switch (arity)
1016 case 0:
1017 pat = GEN_FCN (icode) (target);
1018 break;
1019 case 1:
1020 if (nonvoid)
1021 pat = GEN_FCN (icode) (target, op[0]);
1022 else
1023 pat = GEN_FCN (icode) (op[0]);
1024 break;
1025 case 2:
1026 if (nonvoid)
1027 pat = GEN_FCN (icode) (target, op[0], op[1]);
1028 else
1029 pat = GEN_FCN (icode) (op[0], op[1]);
1030 break;
1031 case 3:
1032 if (nonvoid)
1033 pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
1034 else
1035 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
1036 break;
1037 case 4:
1038 if (nonvoid)
1039 pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
1040 else
1041 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
1042 break;
1043 case 5:
1044 if (nonvoid)
1045 pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
1046 else
1047 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
1048 break;
1049 case 6:
1050 if (nonvoid)
1051 pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4], op[5]);
1052 else
1053 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4], op[5]);
1054 break;
1055 default:
1056 gcc_unreachable ();
1058 if (!pat)
1059 return NULL_RTX;
1060 emit_insn (pat);
1062 if (nonvoid)
1063 return target;
1064 else
1065 return const0_rtx;
1069 static const int s390_hotpatch_hw_max = 1000000;
1070 static int s390_hotpatch_hw_before_label = 0;
1071 static int s390_hotpatch_hw_after_label = 0;
1073 /* Check whether the hotpatch attribute is applied to a function and, if it has
1074 an argument, the argument is valid. */
1076 static tree
1077 s390_handle_hotpatch_attribute (tree *node, tree name, tree args,
1078 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1080 tree expr;
1081 tree expr2;
1082 int err;
1084 if (TREE_CODE (*node) != FUNCTION_DECL)
1086 warning (OPT_Wattributes, "%qE attribute only applies to functions",
1087 name);
1088 *no_add_attrs = true;
1090 if (args != NULL && TREE_CHAIN (args) != NULL)
1092 expr = TREE_VALUE (args);
1093 expr2 = TREE_VALUE (TREE_CHAIN (args));
1095 if (args == NULL || TREE_CHAIN (args) == NULL)
1096 err = 1;
1097 else if (TREE_CODE (expr) != INTEGER_CST
1098 || !INTEGRAL_TYPE_P (TREE_TYPE (expr))
1099 || wi::gtu_p (expr, s390_hotpatch_hw_max))
1100 err = 1;
1101 else if (TREE_CODE (expr2) != INTEGER_CST
1102 || !INTEGRAL_TYPE_P (TREE_TYPE (expr2))
1103 || wi::gtu_p (expr2, s390_hotpatch_hw_max))
1104 err = 1;
1105 else
1106 err = 0;
1107 if (err)
1109 error ("requested %qE attribute is not a comma separated pair of"
1110 " non-negative integer constants or too large (max. %d)", name,
1111 s390_hotpatch_hw_max);
1112 *no_add_attrs = true;
1115 return NULL_TREE;
1118 /* Expand the s390_vector_bool type attribute. */
1120 static tree
1121 s390_handle_vectorbool_attribute (tree *node, tree name ATTRIBUTE_UNUSED,
1122 tree args ATTRIBUTE_UNUSED,
1123 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1125 tree type = *node, result = NULL_TREE;
1126 machine_mode mode;
1128 while (POINTER_TYPE_P (type)
1129 || TREE_CODE (type) == FUNCTION_TYPE
1130 || TREE_CODE (type) == METHOD_TYPE
1131 || TREE_CODE (type) == ARRAY_TYPE)
1132 type = TREE_TYPE (type);
1134 mode = TYPE_MODE (type);
1135 switch (mode)
1137 case E_DImode: case E_V2DImode:
1138 result = s390_builtin_types[BT_BV2DI];
1139 break;
1140 case E_SImode: case E_V4SImode:
1141 result = s390_builtin_types[BT_BV4SI];
1142 break;
1143 case E_HImode: case E_V8HImode:
1144 result = s390_builtin_types[BT_BV8HI];
1145 break;
1146 case E_QImode: case E_V16QImode:
1147 result = s390_builtin_types[BT_BV16QI];
1148 break;
1149 default:
1150 break;
1153 *no_add_attrs = true; /* No need to hang on to the attribute. */
1155 if (result)
1156 *node = lang_hooks.types.reconstruct_complex_type (*node, result);
1158 return NULL_TREE;
1161 static const struct attribute_spec s390_attribute_table[] = {
1162 { "hotpatch", 2, 2, true, false, false, s390_handle_hotpatch_attribute, false },
1163 { "s390_vector_bool", 0, 0, false, true, false, s390_handle_vectorbool_attribute, true },
1164 /* End element. */
1165 { NULL, 0, 0, false, false, false, NULL, false }
1168 /* Return the alignment for LABEL. We default to the -falign-labels
1169 value except for the literal pool base label. */
1171 s390_label_align (rtx_insn *label)
1173 rtx_insn *prev_insn = prev_active_insn (label);
1174 rtx set, src;
1176 if (prev_insn == NULL_RTX)
1177 goto old;
1179 set = single_set (prev_insn);
1181 if (set == NULL_RTX)
1182 goto old;
1184 src = SET_SRC (set);
1186 /* Don't align literal pool base labels. */
1187 if (GET_CODE (src) == UNSPEC
1188 && XINT (src, 1) == UNSPEC_MAIN_BASE)
1189 return 0;
1191 old:
1192 return align_labels_log;
1195 static GTY(()) rtx got_symbol;
1197 /* Return the GOT table symbol. The symbol will be created when the
1198 function is invoked for the first time. */
1200 static rtx
1201 s390_got_symbol (void)
1203 if (!got_symbol)
1205 got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
1206 SYMBOL_REF_FLAGS (got_symbol) = SYMBOL_FLAG_LOCAL;
1209 return got_symbol;
1212 static machine_mode
1213 s390_libgcc_cmp_return_mode (void)
1215 return TARGET_64BIT ? DImode : SImode;
1218 static machine_mode
1219 s390_libgcc_shift_count_mode (void)
1221 return TARGET_64BIT ? DImode : SImode;
1224 static machine_mode
1225 s390_unwind_word_mode (void)
1227 return TARGET_64BIT ? DImode : SImode;
1230 /* Return true if the back end supports mode MODE. */
1231 static bool
1232 s390_scalar_mode_supported_p (machine_mode mode)
1234 /* In contrast to the default implementation reject TImode constants on 31bit
1235 TARGET_ZARCH for ABI compliance. */
1236 if (!TARGET_64BIT && TARGET_ZARCH && mode == TImode)
1237 return false;
1239 if (DECIMAL_FLOAT_MODE_P (mode))
1240 return default_decimal_float_supported_p ();
1242 return default_scalar_mode_supported_p (mode);
1245 /* Return true if the back end supports vector mode MODE. */
1246 static bool
1247 s390_vector_mode_supported_p (machine_mode mode)
1249 machine_mode inner;
1251 if (!VECTOR_MODE_P (mode)
1252 || !TARGET_VX
1253 || GET_MODE_SIZE (mode) > 16)
1254 return false;
1256 inner = GET_MODE_INNER (mode);
1258 switch (inner)
1260 case E_QImode:
1261 case E_HImode:
1262 case E_SImode:
1263 case E_DImode:
1264 case E_TImode:
1265 case E_SFmode:
1266 case E_DFmode:
1267 case E_TFmode:
1268 return true;
1269 default:
1270 return false;
1274 /* Set the has_landing_pad_p flag in struct machine_function to VALUE. */
1276 void
1277 s390_set_has_landing_pad_p (bool value)
1279 cfun->machine->has_landing_pad_p = value;
1282 /* If two condition code modes are compatible, return a condition code
1283 mode which is compatible with both. Otherwise, return
1284 VOIDmode. */
1286 static machine_mode
1287 s390_cc_modes_compatible (machine_mode m1, machine_mode m2)
1289 if (m1 == m2)
1290 return m1;
1292 switch (m1)
1294 case E_CCZmode:
1295 if (m2 == CCUmode || m2 == CCTmode || m2 == CCZ1mode
1296 || m2 == CCSmode || m2 == CCSRmode || m2 == CCURmode)
1297 return m2;
1298 return VOIDmode;
1300 case E_CCSmode:
1301 case E_CCUmode:
1302 case E_CCTmode:
1303 case E_CCSRmode:
1304 case E_CCURmode:
1305 case E_CCZ1mode:
1306 if (m2 == CCZmode)
1307 return m1;
1309 return VOIDmode;
1311 default:
1312 return VOIDmode;
1314 return VOIDmode;
1317 /* Return true if SET either doesn't set the CC register, or else
1318 the source and destination have matching CC modes and that
1319 CC mode is at least as constrained as REQ_MODE. */
1321 static bool
1322 s390_match_ccmode_set (rtx set, machine_mode req_mode)
1324 machine_mode set_mode;
1326 gcc_assert (GET_CODE (set) == SET);
1328 /* These modes are supposed to be used only in CC consumer
1329 patterns. */
1330 gcc_assert (req_mode != CCVIALLmode && req_mode != CCVIANYmode
1331 && req_mode != CCVFALLmode && req_mode != CCVFANYmode);
1333 if (GET_CODE (SET_DEST (set)) != REG || !CC_REGNO_P (REGNO (SET_DEST (set))))
1334 return 1;
1336 set_mode = GET_MODE (SET_DEST (set));
1337 switch (set_mode)
1339 case E_CCZ1mode:
1340 case E_CCSmode:
1341 case E_CCSRmode:
1342 case E_CCUmode:
1343 case E_CCURmode:
1344 case E_CCLmode:
1345 case E_CCL1mode:
1346 case E_CCL2mode:
1347 case E_CCL3mode:
1348 case E_CCT1mode:
1349 case E_CCT2mode:
1350 case E_CCT3mode:
1351 case E_CCVEQmode:
1352 case E_CCVIHmode:
1353 case E_CCVIHUmode:
1354 case E_CCVFHmode:
1355 case E_CCVFHEmode:
1356 if (req_mode != set_mode)
1357 return 0;
1358 break;
1360 case E_CCZmode:
1361 if (req_mode != CCSmode && req_mode != CCUmode && req_mode != CCTmode
1362 && req_mode != CCSRmode && req_mode != CCURmode
1363 && req_mode != CCZ1mode)
1364 return 0;
1365 break;
1367 case E_CCAPmode:
1368 case E_CCANmode:
1369 if (req_mode != CCAmode)
1370 return 0;
1371 break;
1373 default:
1374 gcc_unreachable ();
1377 return (GET_MODE (SET_SRC (set)) == set_mode);
1380 /* Return true if every SET in INSN that sets the CC register
1381 has source and destination with matching CC modes and that
1382 CC mode is at least as constrained as REQ_MODE.
1383 If REQ_MODE is VOIDmode, always return false. */
1385 bool
1386 s390_match_ccmode (rtx_insn *insn, machine_mode req_mode)
1388 int i;
1390 /* s390_tm_ccmode returns VOIDmode to indicate failure. */
1391 if (req_mode == VOIDmode)
1392 return false;
1394 if (GET_CODE (PATTERN (insn)) == SET)
1395 return s390_match_ccmode_set (PATTERN (insn), req_mode);
1397 if (GET_CODE (PATTERN (insn)) == PARALLEL)
1398 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1400 rtx set = XVECEXP (PATTERN (insn), 0, i);
1401 if (GET_CODE (set) == SET)
1402 if (!s390_match_ccmode_set (set, req_mode))
1403 return false;
1406 return true;
1409 /* If a test-under-mask instruction can be used to implement
1410 (compare (and ... OP1) OP2), return the CC mode required
1411 to do that. Otherwise, return VOIDmode.
1412 MIXED is true if the instruction can distinguish between
1413 CC1 and CC2 for mixed selected bits (TMxx), it is false
1414 if the instruction cannot (TM). */
1416 machine_mode
1417 s390_tm_ccmode (rtx op1, rtx op2, bool mixed)
1419 int bit0, bit1;
1421 /* ??? Fixme: should work on CONST_WIDE_INT as well. */
1422 if (GET_CODE (op1) != CONST_INT || GET_CODE (op2) != CONST_INT)
1423 return VOIDmode;
1425 /* Selected bits all zero: CC0.
1426 e.g.: int a; if ((a & (16 + 128)) == 0) */
1427 if (INTVAL (op2) == 0)
1428 return CCTmode;
1430 /* Selected bits all one: CC3.
1431 e.g.: int a; if ((a & (16 + 128)) == 16 + 128) */
1432 if (INTVAL (op2) == INTVAL (op1))
1433 return CCT3mode;
1435 /* Exactly two bits selected, mixed zeroes and ones: CC1 or CC2. e.g.:
1436 int a;
1437 if ((a & (16 + 128)) == 16) -> CCT1
1438 if ((a & (16 + 128)) == 128) -> CCT2 */
1439 if (mixed)
1441 bit1 = exact_log2 (INTVAL (op2));
1442 bit0 = exact_log2 (INTVAL (op1) ^ INTVAL (op2));
1443 if (bit0 != -1 && bit1 != -1)
1444 return bit0 > bit1 ? CCT1mode : CCT2mode;
1447 return VOIDmode;
1450 /* Given a comparison code OP (EQ, NE, etc.) and the operands
1451 OP0 and OP1 of a COMPARE, return the mode to be used for the
1452 comparison. */
1454 machine_mode
1455 s390_select_ccmode (enum rtx_code code, rtx op0, rtx op1)
1457 switch (code)
1459 case EQ:
1460 case NE:
1461 if ((GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
1462 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
1463 return CCAPmode;
1464 if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
1465 && CONST_OK_FOR_K (INTVAL (XEXP (op0, 1))))
1466 return CCAPmode;
1467 if ((GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
1468 || GET_CODE (op1) == NEG)
1469 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
1470 return CCLmode;
1472 if (GET_CODE (op0) == AND)
1474 /* Check whether we can potentially do it via TM. */
1475 machine_mode ccmode;
1476 ccmode = s390_tm_ccmode (XEXP (op0, 1), op1, 1);
1477 if (ccmode != VOIDmode)
1479 /* Relax CCTmode to CCZmode to allow fall-back to AND
1480 if that turns out to be beneficial. */
1481 return ccmode == CCTmode ? CCZmode : ccmode;
1485 if (register_operand (op0, HImode)
1486 && GET_CODE (op1) == CONST_INT
1487 && (INTVAL (op1) == -1 || INTVAL (op1) == 65535))
1488 return CCT3mode;
1489 if (register_operand (op0, QImode)
1490 && GET_CODE (op1) == CONST_INT
1491 && (INTVAL (op1) == -1 || INTVAL (op1) == 255))
1492 return CCT3mode;
1494 return CCZmode;
1496 case LE:
1497 case LT:
1498 case GE:
1499 case GT:
1500 /* The only overflow condition of NEG and ABS happens when
1501 -INT_MAX is used as parameter, which stays negative. So
1502 we have an overflow from a positive value to a negative.
1503 Using CCAP mode the resulting cc can be used for comparisons. */
1504 if ((GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
1505 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
1506 return CCAPmode;
1508 /* If constants are involved in an add instruction it is possible to use
1509 the resulting cc for comparisons with zero. Knowing the sign of the
1510 constant the overflow behavior gets predictable. e.g.:
1511 int a, b; if ((b = a + c) > 0)
1512 with c as a constant value: c < 0 -> CCAN and c >= 0 -> CCAP */
1513 if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
1514 && (CONST_OK_FOR_K (INTVAL (XEXP (op0, 1)))
1515 || (CONST_OK_FOR_CONSTRAINT_P (INTVAL (XEXP (op0, 1)), 'O', "Os")
1516 /* Avoid INT32_MIN on 32 bit. */
1517 && (!TARGET_ZARCH || INTVAL (XEXP (op0, 1)) != -0x7fffffff - 1))))
1519 if (INTVAL (XEXP((op0), 1)) < 0)
1520 return CCANmode;
1521 else
1522 return CCAPmode;
1524 /* Fall through. */
1525 case UNORDERED:
1526 case ORDERED:
1527 case UNEQ:
1528 case UNLE:
1529 case UNLT:
1530 case UNGE:
1531 case UNGT:
1532 case LTGT:
1533 if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
1534 && GET_CODE (op1) != CONST_INT)
1535 return CCSRmode;
1536 return CCSmode;
1538 case LTU:
1539 case GEU:
1540 if (GET_CODE (op0) == PLUS
1541 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
1542 return CCL1mode;
1544 if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
1545 && GET_CODE (op1) != CONST_INT)
1546 return CCURmode;
1547 return CCUmode;
1549 case LEU:
1550 case GTU:
1551 if (GET_CODE (op0) == MINUS
1552 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
1553 return CCL2mode;
1555 if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
1556 && GET_CODE (op1) != CONST_INT)
1557 return CCURmode;
1558 return CCUmode;
1560 default:
1561 gcc_unreachable ();
1565 /* Replace the comparison OP0 CODE OP1 by a semantically equivalent one
1566 that we can implement more efficiently. */
1568 static void
1569 s390_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
1570 bool op0_preserve_value)
1572 if (op0_preserve_value)
1573 return;
1575 /* Convert ZERO_EXTRACT back to AND to enable TM patterns. */
1576 if ((*code == EQ || *code == NE)
1577 && *op1 == const0_rtx
1578 && GET_CODE (*op0) == ZERO_EXTRACT
1579 && GET_CODE (XEXP (*op0, 1)) == CONST_INT
1580 && GET_CODE (XEXP (*op0, 2)) == CONST_INT
1581 && SCALAR_INT_MODE_P (GET_MODE (XEXP (*op0, 0))))
1583 rtx inner = XEXP (*op0, 0);
1584 HOST_WIDE_INT modesize = GET_MODE_BITSIZE (GET_MODE (inner));
1585 HOST_WIDE_INT len = INTVAL (XEXP (*op0, 1));
1586 HOST_WIDE_INT pos = INTVAL (XEXP (*op0, 2));
1588 if (len > 0 && len < modesize
1589 && pos >= 0 && pos + len <= modesize
1590 && modesize <= HOST_BITS_PER_WIDE_INT)
1592 unsigned HOST_WIDE_INT block;
1593 block = (HOST_WIDE_INT_1U << len) - 1;
1594 block <<= modesize - pos - len;
1596 *op0 = gen_rtx_AND (GET_MODE (inner), inner,
1597 gen_int_mode (block, GET_MODE (inner)));
1601 /* Narrow AND of memory against immediate to enable TM. */
1602 if ((*code == EQ || *code == NE)
1603 && *op1 == const0_rtx
1604 && GET_CODE (*op0) == AND
1605 && GET_CODE (XEXP (*op0, 1)) == CONST_INT
1606 && SCALAR_INT_MODE_P (GET_MODE (XEXP (*op0, 0))))
1608 rtx inner = XEXP (*op0, 0);
1609 rtx mask = XEXP (*op0, 1);
1611 /* Ignore paradoxical SUBREGs if all extra bits are masked out. */
1612 if (GET_CODE (inner) == SUBREG
1613 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (inner)))
1614 && (GET_MODE_SIZE (GET_MODE (inner))
1615 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner))))
1616 && ((INTVAL (mask)
1617 & GET_MODE_MASK (GET_MODE (inner))
1618 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (inner))))
1619 == 0))
1620 inner = SUBREG_REG (inner);
1622 /* Do not change volatile MEMs. */
1623 if (MEM_P (inner) && !MEM_VOLATILE_P (inner))
1625 int part = s390_single_part (XEXP (*op0, 1),
1626 GET_MODE (inner), QImode, 0);
1627 if (part >= 0)
1629 mask = gen_int_mode (s390_extract_part (mask, QImode, 0), QImode);
1630 inner = adjust_address_nv (inner, QImode, part);
1631 *op0 = gen_rtx_AND (QImode, inner, mask);
1636 /* Narrow comparisons against 0xffff to HImode if possible. */
1637 if ((*code == EQ || *code == NE)
1638 && GET_CODE (*op1) == CONST_INT
1639 && INTVAL (*op1) == 0xffff
1640 && SCALAR_INT_MODE_P (GET_MODE (*op0))
1641 && (nonzero_bits (*op0, GET_MODE (*op0))
1642 & ~HOST_WIDE_INT_UC (0xffff)) == 0)
1644 *op0 = gen_lowpart (HImode, *op0);
1645 *op1 = constm1_rtx;
1648 /* Remove redundant UNSPEC_STRCMPCC_TO_INT conversions if possible. */
1649 if (GET_CODE (*op0) == UNSPEC
1650 && XINT (*op0, 1) == UNSPEC_STRCMPCC_TO_INT
1651 && XVECLEN (*op0, 0) == 1
1652 && GET_MODE (XVECEXP (*op0, 0, 0)) == CCUmode
1653 && GET_CODE (XVECEXP (*op0, 0, 0)) == REG
1654 && REGNO (XVECEXP (*op0, 0, 0)) == CC_REGNUM
1655 && *op1 == const0_rtx)
1657 enum rtx_code new_code = UNKNOWN;
1658 switch (*code)
1660 case EQ: new_code = EQ; break;
1661 case NE: new_code = NE; break;
1662 case LT: new_code = GTU; break;
1663 case GT: new_code = LTU; break;
1664 case LE: new_code = GEU; break;
1665 case GE: new_code = LEU; break;
1666 default: break;
1669 if (new_code != UNKNOWN)
1671 *op0 = XVECEXP (*op0, 0, 0);
1672 *code = new_code;
1676 /* Remove redundant UNSPEC_CC_TO_INT conversions if possible. */
1677 if (GET_CODE (*op0) == UNSPEC
1678 && XINT (*op0, 1) == UNSPEC_CC_TO_INT
1679 && XVECLEN (*op0, 0) == 1
1680 && GET_CODE (XVECEXP (*op0, 0, 0)) == REG
1681 && REGNO (XVECEXP (*op0, 0, 0)) == CC_REGNUM
1682 && CONST_INT_P (*op1))
1684 enum rtx_code new_code = UNKNOWN;
1685 switch (GET_MODE (XVECEXP (*op0, 0, 0)))
1687 case E_CCZmode:
1688 case E_CCRAWmode:
1689 switch (*code)
1691 case EQ: new_code = EQ; break;
1692 case NE: new_code = NE; break;
1693 default: break;
1695 break;
1696 default: break;
1699 if (new_code != UNKNOWN)
1701 /* For CCRAWmode put the required cc mask into the second
1702 operand. */
1703 if (GET_MODE (XVECEXP (*op0, 0, 0)) == CCRAWmode
1704 && INTVAL (*op1) >= 0 && INTVAL (*op1) <= 3)
1705 *op1 = gen_rtx_CONST_INT (VOIDmode, 1 << (3 - INTVAL (*op1)));
1706 *op0 = XVECEXP (*op0, 0, 0);
1707 *code = new_code;
1711 /* Simplify cascaded EQ, NE with const0_rtx. */
1712 if ((*code == NE || *code == EQ)
1713 && (GET_CODE (*op0) == EQ || GET_CODE (*op0) == NE)
1714 && GET_MODE (*op0) == SImode
1715 && GET_MODE (XEXP (*op0, 0)) == CCZ1mode
1716 && REG_P (XEXP (*op0, 0))
1717 && XEXP (*op0, 1) == const0_rtx
1718 && *op1 == const0_rtx)
1720 if ((*code == EQ && GET_CODE (*op0) == NE)
1721 || (*code == NE && GET_CODE (*op0) == EQ))
1722 *code = EQ;
1723 else
1724 *code = NE;
1725 *op0 = XEXP (*op0, 0);
1728 /* Prefer register over memory as first operand. */
1729 if (MEM_P (*op0) && REG_P (*op1))
1731 rtx tem = *op0; *op0 = *op1; *op1 = tem;
1732 *code = (int)swap_condition ((enum rtx_code)*code);
1735 /* A comparison result is compared against zero. Replace it with
1736 the (perhaps inverted) original comparison.
1737 This probably should be done by simplify_relational_operation. */
1738 if ((*code == EQ || *code == NE)
1739 && *op1 == const0_rtx
1740 && COMPARISON_P (*op0)
1741 && CC_REG_P (XEXP (*op0, 0)))
1743 enum rtx_code new_code;
1745 if (*code == EQ)
1746 new_code = reversed_comparison_code_parts (GET_CODE (*op0),
1747 XEXP (*op0, 0),
1748 XEXP (*op1, 0), NULL);
1749 else
1750 new_code = GET_CODE (*op0);
1752 if (new_code != UNKNOWN)
1754 *code = new_code;
1755 *op1 = XEXP (*op0, 1);
1756 *op0 = XEXP (*op0, 0);
1762 /* Emit a compare instruction suitable to implement the comparison
1763 OP0 CODE OP1. Return the correct condition RTL to be placed in
1764 the IF_THEN_ELSE of the conditional branch testing the result. */
1767 s390_emit_compare (enum rtx_code code, rtx op0, rtx op1)
1769 machine_mode mode = s390_select_ccmode (code, op0, op1);
1770 rtx cc;
1772 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
1774 /* Do not output a redundant compare instruction if a
1775 compare_and_swap pattern already computed the result and the
1776 machine modes are compatible. */
1777 gcc_assert (s390_cc_modes_compatible (GET_MODE (op0), mode)
1778 == GET_MODE (op0));
1779 cc = op0;
1781 else
1783 cc = gen_rtx_REG (mode, CC_REGNUM);
1784 emit_insn (gen_rtx_SET (cc, gen_rtx_COMPARE (mode, op0, op1)));
1787 return gen_rtx_fmt_ee (code, VOIDmode, cc, const0_rtx);
1790 /* Emit a SImode compare and swap instruction setting MEM to NEW_RTX if OLD
1791 matches CMP.
1792 Return the correct condition RTL to be placed in the IF_THEN_ELSE of the
1793 conditional branch testing the result. */
1795 static rtx
1796 s390_emit_compare_and_swap (enum rtx_code code, rtx old, rtx mem,
1797 rtx cmp, rtx new_rtx, machine_mode ccmode)
1799 rtx cc;
1801 cc = gen_rtx_REG (ccmode, CC_REGNUM);
1802 switch (GET_MODE (mem))
1804 case E_SImode:
1805 emit_insn (gen_atomic_compare_and_swapsi_internal (old, mem, cmp,
1806 new_rtx, cc));
1807 break;
1808 case E_DImode:
1809 emit_insn (gen_atomic_compare_and_swapdi_internal (old, mem, cmp,
1810 new_rtx, cc));
1811 break;
1812 case E_TImode:
1813 emit_insn (gen_atomic_compare_and_swapti_internal (old, mem, cmp,
1814 new_rtx, cc));
1815 break;
1816 case E_QImode:
1817 case E_HImode:
1818 default:
1819 gcc_unreachable ();
1821 return s390_emit_compare (code, cc, const0_rtx);
1824 /* Emit a jump instruction to TARGET and return it. If COND is
1825 NULL_RTX, emit an unconditional jump, else a conditional jump under
1826 condition COND. */
1828 rtx_insn *
1829 s390_emit_jump (rtx target, rtx cond)
1831 rtx insn;
1833 target = gen_rtx_LABEL_REF (VOIDmode, target);
1834 if (cond)
1835 target = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, target, pc_rtx);
1837 insn = gen_rtx_SET (pc_rtx, target);
1838 return emit_jump_insn (insn);
1841 /* Return branch condition mask to implement a branch
1842 specified by CODE. Return -1 for invalid comparisons. */
1845 s390_branch_condition_mask (rtx code)
1847 const int CC0 = 1 << 3;
1848 const int CC1 = 1 << 2;
1849 const int CC2 = 1 << 1;
1850 const int CC3 = 1 << 0;
1852 gcc_assert (GET_CODE (XEXP (code, 0)) == REG);
1853 gcc_assert (REGNO (XEXP (code, 0)) == CC_REGNUM);
1854 gcc_assert (XEXP (code, 1) == const0_rtx
1855 || (GET_MODE (XEXP (code, 0)) == CCRAWmode
1856 && CONST_INT_P (XEXP (code, 1))));
1859 switch (GET_MODE (XEXP (code, 0)))
1861 case E_CCZmode:
1862 case E_CCZ1mode:
1863 switch (GET_CODE (code))
1865 case EQ: return CC0;
1866 case NE: return CC1 | CC2 | CC3;
1867 default: return -1;
1869 break;
1871 case E_CCT1mode:
1872 switch (GET_CODE (code))
1874 case EQ: return CC1;
1875 case NE: return CC0 | CC2 | CC3;
1876 default: return -1;
1878 break;
1880 case E_CCT2mode:
1881 switch (GET_CODE (code))
1883 case EQ: return CC2;
1884 case NE: return CC0 | CC1 | CC3;
1885 default: return -1;
1887 break;
1889 case E_CCT3mode:
1890 switch (GET_CODE (code))
1892 case EQ: return CC3;
1893 case NE: return CC0 | CC1 | CC2;
1894 default: return -1;
1896 break;
1898 case E_CCLmode:
1899 switch (GET_CODE (code))
1901 case EQ: return CC0 | CC2;
1902 case NE: return CC1 | CC3;
1903 default: return -1;
1905 break;
1907 case E_CCL1mode:
1908 switch (GET_CODE (code))
1910 case LTU: return CC2 | CC3; /* carry */
1911 case GEU: return CC0 | CC1; /* no carry */
1912 default: return -1;
1914 break;
1916 case E_CCL2mode:
1917 switch (GET_CODE (code))
1919 case GTU: return CC0 | CC1; /* borrow */
1920 case LEU: return CC2 | CC3; /* no borrow */
1921 default: return -1;
1923 break;
1925 case E_CCL3mode:
1926 switch (GET_CODE (code))
1928 case EQ: return CC0 | CC2;
1929 case NE: return CC1 | CC3;
1930 case LTU: return CC1;
1931 case GTU: return CC3;
1932 case LEU: return CC1 | CC2;
1933 case GEU: return CC2 | CC3;
1934 default: return -1;
1937 case E_CCUmode:
1938 switch (GET_CODE (code))
1940 case EQ: return CC0;
1941 case NE: return CC1 | CC2 | CC3;
1942 case LTU: return CC1;
1943 case GTU: return CC2;
1944 case LEU: return CC0 | CC1;
1945 case GEU: return CC0 | CC2;
1946 default: return -1;
1948 break;
1950 case E_CCURmode:
1951 switch (GET_CODE (code))
1953 case EQ: return CC0;
1954 case NE: return CC2 | CC1 | CC3;
1955 case LTU: return CC2;
1956 case GTU: return CC1;
1957 case LEU: return CC0 | CC2;
1958 case GEU: return CC0 | CC1;
1959 default: return -1;
1961 break;
1963 case E_CCAPmode:
1964 switch (GET_CODE (code))
1966 case EQ: return CC0;
1967 case NE: return CC1 | CC2 | CC3;
1968 case LT: return CC1 | CC3;
1969 case GT: return CC2;
1970 case LE: return CC0 | CC1 | CC3;
1971 case GE: return CC0 | CC2;
1972 default: return -1;
1974 break;
1976 case E_CCANmode:
1977 switch (GET_CODE (code))
1979 case EQ: return CC0;
1980 case NE: return CC1 | CC2 | CC3;
1981 case LT: return CC1;
1982 case GT: return CC2 | CC3;
1983 case LE: return CC0 | CC1;
1984 case GE: return CC0 | CC2 | CC3;
1985 default: return -1;
1987 break;
1989 case E_CCSmode:
1990 switch (GET_CODE (code))
1992 case EQ: return CC0;
1993 case NE: return CC1 | CC2 | CC3;
1994 case LT: return CC1;
1995 case GT: return CC2;
1996 case LE: return CC0 | CC1;
1997 case GE: return CC0 | CC2;
1998 case UNORDERED: return CC3;
1999 case ORDERED: return CC0 | CC1 | CC2;
2000 case UNEQ: return CC0 | CC3;
2001 case UNLT: return CC1 | CC3;
2002 case UNGT: return CC2 | CC3;
2003 case UNLE: return CC0 | CC1 | CC3;
2004 case UNGE: return CC0 | CC2 | CC3;
2005 case LTGT: return CC1 | CC2;
2006 default: return -1;
2008 break;
2010 case E_CCSRmode:
2011 switch (GET_CODE (code))
2013 case EQ: return CC0;
2014 case NE: return CC2 | CC1 | CC3;
2015 case LT: return CC2;
2016 case GT: return CC1;
2017 case LE: return CC0 | CC2;
2018 case GE: return CC0 | CC1;
2019 case UNORDERED: return CC3;
2020 case ORDERED: return CC0 | CC2 | CC1;
2021 case UNEQ: return CC0 | CC3;
2022 case UNLT: return CC2 | CC3;
2023 case UNGT: return CC1 | CC3;
2024 case UNLE: return CC0 | CC2 | CC3;
2025 case UNGE: return CC0 | CC1 | CC3;
2026 case LTGT: return CC2 | CC1;
2027 default: return -1;
2029 break;
2031 /* Vector comparison modes. */
2032 /* CC2 will never be set. It however is part of the negated
2033 masks. */
2034 case E_CCVIALLmode:
2035 switch (GET_CODE (code))
2037 case EQ:
2038 case GTU:
2039 case GT:
2040 case GE: return CC0;
2041 /* The inverted modes are in fact *any* modes. */
2042 case NE:
2043 case LEU:
2044 case LE:
2045 case LT: return CC3 | CC1 | CC2;
2046 default: return -1;
2049 case E_CCVIANYmode:
2050 switch (GET_CODE (code))
2052 case EQ:
2053 case GTU:
2054 case GT:
2055 case GE: return CC0 | CC1;
2056 /* The inverted modes are in fact *all* modes. */
2057 case NE:
2058 case LEU:
2059 case LE:
2060 case LT: return CC3 | CC2;
2061 default: return -1;
2063 case E_CCVFALLmode:
2064 switch (GET_CODE (code))
2066 case EQ:
2067 case GT:
2068 case GE: return CC0;
2069 /* The inverted modes are in fact *any* modes. */
2070 case NE:
2071 case UNLE:
2072 case UNLT: return CC3 | CC1 | CC2;
2073 default: return -1;
2076 case E_CCVFANYmode:
2077 switch (GET_CODE (code))
2079 case EQ:
2080 case GT:
2081 case GE: return CC0 | CC1;
2082 /* The inverted modes are in fact *all* modes. */
2083 case NE:
2084 case UNLE:
2085 case UNLT: return CC3 | CC2;
2086 default: return -1;
2089 case E_CCRAWmode:
2090 switch (GET_CODE (code))
2092 case EQ:
2093 return INTVAL (XEXP (code, 1));
2094 case NE:
2095 return (INTVAL (XEXP (code, 1))) ^ 0xf;
2096 default:
2097 gcc_unreachable ();
2100 default:
2101 return -1;
2106 /* Return branch condition mask to implement a compare and branch
2107 specified by CODE. Return -1 for invalid comparisons. */
2110 s390_compare_and_branch_condition_mask (rtx code)
2112 const int CC0 = 1 << 3;
2113 const int CC1 = 1 << 2;
2114 const int CC2 = 1 << 1;
2116 switch (GET_CODE (code))
2118 case EQ:
2119 return CC0;
2120 case NE:
2121 return CC1 | CC2;
2122 case LT:
2123 case LTU:
2124 return CC1;
2125 case GT:
2126 case GTU:
2127 return CC2;
2128 case LE:
2129 case LEU:
2130 return CC0 | CC1;
2131 case GE:
2132 case GEU:
2133 return CC0 | CC2;
2134 default:
2135 gcc_unreachable ();
2137 return -1;
2140 /* If INV is false, return assembler mnemonic string to implement
2141 a branch specified by CODE. If INV is true, return mnemonic
2142 for the corresponding inverted branch. */
2144 static const char *
2145 s390_branch_condition_mnemonic (rtx code, int inv)
2147 int mask;
2149 static const char *const mnemonic[16] =
2151 NULL, "o", "h", "nle",
2152 "l", "nhe", "lh", "ne",
2153 "e", "nlh", "he", "nl",
2154 "le", "nh", "no", NULL
2157 if (GET_CODE (XEXP (code, 0)) == REG
2158 && REGNO (XEXP (code, 0)) == CC_REGNUM
2159 && (XEXP (code, 1) == const0_rtx
2160 || (GET_MODE (XEXP (code, 0)) == CCRAWmode
2161 && CONST_INT_P (XEXP (code, 1)))))
2162 mask = s390_branch_condition_mask (code);
2163 else
2164 mask = s390_compare_and_branch_condition_mask (code);
2166 gcc_assert (mask >= 0);
2168 if (inv)
2169 mask ^= 15;
2171 gcc_assert (mask >= 1 && mask <= 14);
2173 return mnemonic[mask];
2176 /* Return the part of op which has a value different from def.
2177 The size of the part is determined by mode.
2178 Use this function only if you already know that op really
2179 contains such a part. */
2181 unsigned HOST_WIDE_INT
2182 s390_extract_part (rtx op, machine_mode mode, int def)
2184 unsigned HOST_WIDE_INT value = 0;
2185 int max_parts = HOST_BITS_PER_WIDE_INT / GET_MODE_BITSIZE (mode);
2186 int part_bits = GET_MODE_BITSIZE (mode);
2187 unsigned HOST_WIDE_INT part_mask = (HOST_WIDE_INT_1U << part_bits) - 1;
2188 int i;
2190 for (i = 0; i < max_parts; i++)
2192 if (i == 0)
2193 value = UINTVAL (op);
2194 else
2195 value >>= part_bits;
2197 if ((value & part_mask) != (def & part_mask))
2198 return value & part_mask;
2201 gcc_unreachable ();
2204 /* If OP is an integer constant of mode MODE with exactly one
2205 part of mode PART_MODE unequal to DEF, return the number of that
2206 part. Otherwise, return -1. */
2209 s390_single_part (rtx op,
2210 machine_mode mode,
2211 machine_mode part_mode,
2212 int def)
2214 unsigned HOST_WIDE_INT value = 0;
2215 int n_parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (part_mode);
2216 unsigned HOST_WIDE_INT part_mask
2217 = (HOST_WIDE_INT_1U << GET_MODE_BITSIZE (part_mode)) - 1;
2218 int i, part = -1;
2220 if (GET_CODE (op) != CONST_INT)
2221 return -1;
2223 for (i = 0; i < n_parts; i++)
2225 if (i == 0)
2226 value = UINTVAL (op);
2227 else
2228 value >>= GET_MODE_BITSIZE (part_mode);
2230 if ((value & part_mask) != (def & part_mask))
2232 if (part != -1)
2233 return -1;
2234 else
2235 part = i;
2238 return part == -1 ? -1 : n_parts - 1 - part;
2241 /* Return true if IN contains a contiguous bitfield in the lower SIZE
2242 bits and no other bits are set in (the lower SIZE bits of) IN.
2244 PSTART and PEND can be used to obtain the start and end
2245 position (inclusive) of the bitfield relative to 64
2246 bits. *PSTART / *PEND gives the position of the first/last bit
2247 of the bitfield counting from the highest order bit starting
2248 with zero. */
2250 bool
2251 s390_contiguous_bitmask_nowrap_p (unsigned HOST_WIDE_INT in, int size,
2252 int *pstart, int *pend)
2254 int start;
2255 int end = -1;
2256 int lowbit = HOST_BITS_PER_WIDE_INT - 1;
2257 int highbit = HOST_BITS_PER_WIDE_INT - size;
2258 unsigned HOST_WIDE_INT bitmask = HOST_WIDE_INT_1U;
2260 gcc_assert (!!pstart == !!pend);
2261 for (start = lowbit; start >= highbit; bitmask <<= 1, start--)
2262 if (end == -1)
2264 /* Look for the rightmost bit of a contiguous range of ones. */
2265 if (bitmask & in)
2266 /* Found it. */
2267 end = start;
2269 else
2271 /* Look for the firt zero bit after the range of ones. */
2272 if (! (bitmask & in))
2273 /* Found it. */
2274 break;
2276 /* We're one past the last one-bit. */
2277 start++;
2279 if (end == -1)
2280 /* No one bits found. */
2281 return false;
2283 if (start > highbit)
2285 unsigned HOST_WIDE_INT mask;
2287 /* Calculate a mask for all bits beyond the contiguous bits. */
2288 mask = ((~HOST_WIDE_INT_0U >> highbit)
2289 & (~HOST_WIDE_INT_0U << (lowbit - start + 1)));
2290 if (mask & in)
2291 /* There are more bits set beyond the first range of one bits. */
2292 return false;
2295 if (pstart)
2297 *pstart = start;
2298 *pend = end;
2301 return true;
2304 /* Same as s390_contiguous_bitmask_nowrap_p but also returns true
2305 if ~IN contains a contiguous bitfield. In that case, *END is <
2306 *START.
2308 If WRAP_P is true, a bitmask that wraps around is also tested.
2309 When a wraparoud occurs *START is greater than *END (in
2310 non-null pointers), and the uppermost (64 - SIZE) bits are thus
2311 part of the range. If WRAP_P is false, no wraparound is
2312 tested. */
2314 bool
2315 s390_contiguous_bitmask_p (unsigned HOST_WIDE_INT in, bool wrap_p,
2316 int size, int *start, int *end)
2318 int bs = HOST_BITS_PER_WIDE_INT;
2319 bool b;
2321 gcc_assert (!!start == !!end);
2322 if ((in & ((~HOST_WIDE_INT_0U) >> (bs - size))) == 0)
2323 /* This cannot be expressed as a contiguous bitmask. Exit early because
2324 the second call of s390_contiguous_bitmask_nowrap_p would accept this as
2325 a valid bitmask. */
2326 return false;
2327 b = s390_contiguous_bitmask_nowrap_p (in, size, start, end);
2328 if (b)
2329 return true;
2330 if (! wrap_p)
2331 return false;
2332 b = s390_contiguous_bitmask_nowrap_p (~in, size, start, end);
2333 if (b && start)
2335 int s = *start;
2336 int e = *end;
2338 gcc_assert (s >= 1);
2339 *start = ((e + 1) & (bs - 1));
2340 *end = ((s - 1 + bs) & (bs - 1));
2343 return b;
2346 /* Return true if OP contains the same contiguous bitfield in *all*
2347 its elements. START and END can be used to obtain the start and
2348 end position of the bitfield.
2350 START/STOP give the position of the first/last bit of the bitfield
2351 counting from the lowest order bit starting with zero. In order to
2352 use these values for S/390 instructions this has to be converted to
2353 "bits big endian" style. */
2355 bool
2356 s390_contiguous_bitmask_vector_p (rtx op, int *start, int *end)
2358 unsigned HOST_WIDE_INT mask;
2359 int size;
2360 rtx elt;
2361 bool b;
2363 gcc_assert (!!start == !!end);
2364 if (!const_vec_duplicate_p (op, &elt)
2365 || !CONST_INT_P (elt))
2366 return false;
2368 size = GET_MODE_UNIT_BITSIZE (GET_MODE (op));
2370 /* We cannot deal with V1TI/V1TF. This would require a vgmq. */
2371 if (size > 64)
2372 return false;
2374 mask = UINTVAL (elt);
2376 b = s390_contiguous_bitmask_p (mask, true, size, start, end);
2377 if (b)
2379 if (start)
2381 *start -= (HOST_BITS_PER_WIDE_INT - size);
2382 *end -= (HOST_BITS_PER_WIDE_INT - size);
2384 return true;
2386 else
2387 return false;
2390 /* Return true if C consists only of byte chunks being either 0 or
2391 0xff. If MASK is !=NULL a byte mask is generated which is
2392 appropriate for the vector generate byte mask instruction. */
2394 bool
2395 s390_bytemask_vector_p (rtx op, unsigned *mask)
2397 int i;
2398 unsigned tmp_mask = 0;
2399 int nunit, unit_size;
2401 if (!VECTOR_MODE_P (GET_MODE (op))
2402 || GET_CODE (op) != CONST_VECTOR
2403 || !CONST_INT_P (XVECEXP (op, 0, 0)))
2404 return false;
2406 nunit = GET_MODE_NUNITS (GET_MODE (op));
2407 unit_size = GET_MODE_UNIT_SIZE (GET_MODE (op));
2409 for (i = 0; i < nunit; i++)
2411 unsigned HOST_WIDE_INT c;
2412 int j;
2414 if (!CONST_INT_P (XVECEXP (op, 0, i)))
2415 return false;
2417 c = UINTVAL (XVECEXP (op, 0, i));
2418 for (j = 0; j < unit_size; j++)
2420 if ((c & 0xff) != 0 && (c & 0xff) != 0xff)
2421 return false;
2422 tmp_mask |= (c & 1) << ((nunit - 1 - i) * unit_size + j);
2423 c = c >> BITS_PER_UNIT;
2427 if (mask != NULL)
2428 *mask = tmp_mask;
2430 return true;
2433 /* Check whether a rotate of ROTL followed by an AND of CONTIG is
2434 equivalent to a shift followed by the AND. In particular, CONTIG
2435 should not overlap the (rotated) bit 0/bit 63 gap. Negative values
2436 for ROTL indicate a rotate to the right. */
2438 bool
2439 s390_extzv_shift_ok (int bitsize, int rotl, unsigned HOST_WIDE_INT contig)
2441 int start, end;
2442 bool ok;
2444 ok = s390_contiguous_bitmask_nowrap_p (contig, bitsize, &start, &end);
2445 gcc_assert (ok);
2447 if (rotl >= 0)
2448 return (64 - end >= rotl);
2449 else
2451 /* Translate "- rotate right" in BITSIZE mode to "rotate left" in
2452 DIMode. */
2453 rotl = -rotl + (64 - bitsize);
2454 return (start >= rotl);
2458 /* Check whether we can (and want to) split a double-word
2459 move in mode MODE from SRC to DST into two single-word
2460 moves, moving the subword FIRST_SUBWORD first. */
2462 bool
2463 s390_split_ok_p (rtx dst, rtx src, machine_mode mode, int first_subword)
2465 /* Floating point and vector registers cannot be split. */
2466 if (FP_REG_P (src) || FP_REG_P (dst) || VECTOR_REG_P (src) || VECTOR_REG_P (dst))
2467 return false;
2469 /* Non-offsettable memory references cannot be split. */
2470 if ((GET_CODE (src) == MEM && !offsettable_memref_p (src))
2471 || (GET_CODE (dst) == MEM && !offsettable_memref_p (dst)))
2472 return false;
2474 /* Moving the first subword must not clobber a register
2475 needed to move the second subword. */
2476 if (register_operand (dst, mode))
2478 rtx subreg = operand_subword (dst, first_subword, 0, mode);
2479 if (reg_overlap_mentioned_p (subreg, src))
2480 return false;
2483 return true;
2486 /* Return true if it can be proven that [MEM1, MEM1 + SIZE]
2487 and [MEM2, MEM2 + SIZE] do overlap and false
2488 otherwise. */
2490 bool
2491 s390_overlap_p (rtx mem1, rtx mem2, HOST_WIDE_INT size)
2493 rtx addr1, addr2, addr_delta;
2494 HOST_WIDE_INT delta;
2496 if (GET_CODE (mem1) != MEM || GET_CODE (mem2) != MEM)
2497 return true;
2499 if (size == 0)
2500 return false;
2502 addr1 = XEXP (mem1, 0);
2503 addr2 = XEXP (mem2, 0);
2505 addr_delta = simplify_binary_operation (MINUS, Pmode, addr2, addr1);
2507 /* This overlapping check is used by peepholes merging memory block operations.
2508 Overlapping operations would otherwise be recognized by the S/390 hardware
2509 and would fall back to a slower implementation. Allowing overlapping
2510 operations would lead to slow code but not to wrong code. Therefore we are
2511 somewhat optimistic if we cannot prove that the memory blocks are
2512 overlapping.
2513 That's why we return false here although this may accept operations on
2514 overlapping memory areas. */
2515 if (!addr_delta || GET_CODE (addr_delta) != CONST_INT)
2516 return false;
2518 delta = INTVAL (addr_delta);
2520 if (delta == 0
2521 || (delta > 0 && delta < size)
2522 || (delta < 0 && -delta < size))
2523 return true;
2525 return false;
2528 /* Check whether the address of memory reference MEM2 equals exactly
2529 the address of memory reference MEM1 plus DELTA. Return true if
2530 we can prove this to be the case, false otherwise. */
2532 bool
2533 s390_offset_p (rtx mem1, rtx mem2, rtx delta)
2535 rtx addr1, addr2, addr_delta;
2537 if (GET_CODE (mem1) != MEM || GET_CODE (mem2) != MEM)
2538 return false;
2540 addr1 = XEXP (mem1, 0);
2541 addr2 = XEXP (mem2, 0);
2543 addr_delta = simplify_binary_operation (MINUS, Pmode, addr2, addr1);
2544 if (!addr_delta || !rtx_equal_p (addr_delta, delta))
2545 return false;
2547 return true;
2550 /* Expand logical operator CODE in mode MODE with operands OPERANDS. */
2552 void
2553 s390_expand_logical_operator (enum rtx_code code, machine_mode mode,
2554 rtx *operands)
2556 machine_mode wmode = mode;
2557 rtx dst = operands[0];
2558 rtx src1 = operands[1];
2559 rtx src2 = operands[2];
2560 rtx op, clob, tem;
2562 /* If we cannot handle the operation directly, use a temp register. */
2563 if (!s390_logical_operator_ok_p (operands))
2564 dst = gen_reg_rtx (mode);
2566 /* QImode and HImode patterns make sense only if we have a destination
2567 in memory. Otherwise perform the operation in SImode. */
2568 if ((mode == QImode || mode == HImode) && GET_CODE (dst) != MEM)
2569 wmode = SImode;
2571 /* Widen operands if required. */
2572 if (mode != wmode)
2574 if (GET_CODE (dst) == SUBREG
2575 && (tem = simplify_subreg (wmode, dst, mode, 0)) != 0)
2576 dst = tem;
2577 else if (REG_P (dst))
2578 dst = gen_rtx_SUBREG (wmode, dst, 0);
2579 else
2580 dst = gen_reg_rtx (wmode);
2582 if (GET_CODE (src1) == SUBREG
2583 && (tem = simplify_subreg (wmode, src1, mode, 0)) != 0)
2584 src1 = tem;
2585 else if (GET_MODE (src1) != VOIDmode)
2586 src1 = gen_rtx_SUBREG (wmode, force_reg (mode, src1), 0);
2588 if (GET_CODE (src2) == SUBREG
2589 && (tem = simplify_subreg (wmode, src2, mode, 0)) != 0)
2590 src2 = tem;
2591 else if (GET_MODE (src2) != VOIDmode)
2592 src2 = gen_rtx_SUBREG (wmode, force_reg (mode, src2), 0);
2595 /* Emit the instruction. */
2596 op = gen_rtx_SET (dst, gen_rtx_fmt_ee (code, wmode, src1, src2));
2597 clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
2598 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
2600 /* Fix up the destination if needed. */
2601 if (dst != operands[0])
2602 emit_move_insn (operands[0], gen_lowpart (mode, dst));
2605 /* Check whether OPERANDS are OK for a logical operation (AND, IOR, XOR). */
2607 bool
2608 s390_logical_operator_ok_p (rtx *operands)
2610 /* If the destination operand is in memory, it needs to coincide
2611 with one of the source operands. After reload, it has to be
2612 the first source operand. */
2613 if (GET_CODE (operands[0]) == MEM)
2614 return rtx_equal_p (operands[0], operands[1])
2615 || (!reload_completed && rtx_equal_p (operands[0], operands[2]));
2617 return true;
2620 /* Narrow logical operation CODE of memory operand MEMOP with immediate
2621 operand IMMOP to switch from SS to SI type instructions. */
2623 void
2624 s390_narrow_logical_operator (enum rtx_code code, rtx *memop, rtx *immop)
2626 int def = code == AND ? -1 : 0;
2627 HOST_WIDE_INT mask;
2628 int part;
2630 gcc_assert (GET_CODE (*memop) == MEM);
2631 gcc_assert (!MEM_VOLATILE_P (*memop));
2633 mask = s390_extract_part (*immop, QImode, def);
2634 part = s390_single_part (*immop, GET_MODE (*memop), QImode, def);
2635 gcc_assert (part >= 0);
2637 *memop = adjust_address (*memop, QImode, part);
2638 *immop = gen_int_mode (mask, QImode);
2642 /* How to allocate a 'struct machine_function'. */
2644 static struct machine_function *
2645 s390_init_machine_status (void)
2647 return ggc_cleared_alloc<machine_function> ();
2650 /* Map for smallest class containing reg regno. */
2652 const enum reg_class regclass_map[FIRST_PSEUDO_REGISTER] =
2653 { GENERAL_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS, /* 0 */
2654 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS, /* 4 */
2655 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS, /* 8 */
2656 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS, /* 12 */
2657 FP_REGS, FP_REGS, FP_REGS, FP_REGS, /* 16 */
2658 FP_REGS, FP_REGS, FP_REGS, FP_REGS, /* 20 */
2659 FP_REGS, FP_REGS, FP_REGS, FP_REGS, /* 24 */
2660 FP_REGS, FP_REGS, FP_REGS, FP_REGS, /* 28 */
2661 ADDR_REGS, CC_REGS, ADDR_REGS, ADDR_REGS, /* 32 */
2662 ACCESS_REGS, ACCESS_REGS, VEC_REGS, VEC_REGS, /* 36 */
2663 VEC_REGS, VEC_REGS, VEC_REGS, VEC_REGS, /* 40 */
2664 VEC_REGS, VEC_REGS, VEC_REGS, VEC_REGS, /* 44 */
2665 VEC_REGS, VEC_REGS, VEC_REGS, VEC_REGS, /* 48 */
2666 VEC_REGS, VEC_REGS /* 52 */
2669 /* Return attribute type of insn. */
2671 static enum attr_type
2672 s390_safe_attr_type (rtx_insn *insn)
2674 if (recog_memoized (insn) >= 0)
2675 return get_attr_type (insn);
2676 else
2677 return TYPE_NONE;
2680 /* Return true if DISP is a valid short displacement. */
2682 static bool
2683 s390_short_displacement (rtx disp)
2685 /* No displacement is OK. */
2686 if (!disp)
2687 return true;
2689 /* Without the long displacement facility we don't need to
2690 distingiush between long and short displacement. */
2691 if (!TARGET_LONG_DISPLACEMENT)
2692 return true;
2694 /* Integer displacement in range. */
2695 if (GET_CODE (disp) == CONST_INT)
2696 return INTVAL (disp) >= 0 && INTVAL (disp) < 4096;
2698 /* GOT offset is not OK, the GOT can be large. */
2699 if (GET_CODE (disp) == CONST
2700 && GET_CODE (XEXP (disp, 0)) == UNSPEC
2701 && (XINT (XEXP (disp, 0), 1) == UNSPEC_GOT
2702 || XINT (XEXP (disp, 0), 1) == UNSPEC_GOTNTPOFF))
2703 return false;
2705 /* All other symbolic constants are literal pool references,
2706 which are OK as the literal pool must be small. */
2707 if (GET_CODE (disp) == CONST)
2708 return true;
2710 return false;
2713 /* Decompose a RTL expression ADDR for a memory address into
2714 its components, returned in OUT.
2716 Returns false if ADDR is not a valid memory address, true
2717 otherwise. If OUT is NULL, don't return the components,
2718 but check for validity only.
2720 Note: Only addresses in canonical form are recognized.
2721 LEGITIMIZE_ADDRESS should convert non-canonical forms to the
2722 canonical form so that they will be recognized. */
2724 static int
2725 s390_decompose_address (rtx addr, struct s390_address *out)
2727 HOST_WIDE_INT offset = 0;
2728 rtx base = NULL_RTX;
2729 rtx indx = NULL_RTX;
2730 rtx disp = NULL_RTX;
2731 rtx orig_disp;
2732 bool pointer = false;
2733 bool base_ptr = false;
2734 bool indx_ptr = false;
2735 bool literal_pool = false;
2737 /* We may need to substitute the literal pool base register into the address
2738 below. However, at this point we do not know which register is going to
2739 be used as base, so we substitute the arg pointer register. This is going
2740 to be treated as holding a pointer below -- it shouldn't be used for any
2741 other purpose. */
2742 rtx fake_pool_base = gen_rtx_REG (Pmode, ARG_POINTER_REGNUM);
2744 /* Decompose address into base + index + displacement. */
2746 if (GET_CODE (addr) == REG || GET_CODE (addr) == UNSPEC)
2747 base = addr;
2749 else if (GET_CODE (addr) == PLUS)
2751 rtx op0 = XEXP (addr, 0);
2752 rtx op1 = XEXP (addr, 1);
2753 enum rtx_code code0 = GET_CODE (op0);
2754 enum rtx_code code1 = GET_CODE (op1);
2756 if (code0 == REG || code0 == UNSPEC)
2758 if (code1 == REG || code1 == UNSPEC)
2760 indx = op0; /* index + base */
2761 base = op1;
2764 else
2766 base = op0; /* base + displacement */
2767 disp = op1;
2771 else if (code0 == PLUS)
2773 indx = XEXP (op0, 0); /* index + base + disp */
2774 base = XEXP (op0, 1);
2775 disp = op1;
2778 else
2780 return false;
2784 else
2785 disp = addr; /* displacement */
2787 /* Extract integer part of displacement. */
2788 orig_disp = disp;
2789 if (disp)
2791 if (GET_CODE (disp) == CONST_INT)
2793 offset = INTVAL (disp);
2794 disp = NULL_RTX;
2796 else if (GET_CODE (disp) == CONST
2797 && GET_CODE (XEXP (disp, 0)) == PLUS
2798 && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
2800 offset = INTVAL (XEXP (XEXP (disp, 0), 1));
2801 disp = XEXP (XEXP (disp, 0), 0);
2805 /* Strip off CONST here to avoid special case tests later. */
2806 if (disp && GET_CODE (disp) == CONST)
2807 disp = XEXP (disp, 0);
2809 /* We can convert literal pool addresses to
2810 displacements by basing them off the base register. */
2811 if (disp && GET_CODE (disp) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (disp))
2813 if (base || indx)
2814 return false;
2816 base = fake_pool_base, literal_pool = true;
2818 /* Mark up the displacement. */
2819 disp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, disp),
2820 UNSPEC_LTREL_OFFSET);
2823 /* Validate base register. */
2824 if (base)
2826 if (GET_CODE (base) == UNSPEC)
2827 switch (XINT (base, 1))
2829 case UNSPEC_LTREF:
2830 if (!disp)
2831 disp = gen_rtx_UNSPEC (Pmode,
2832 gen_rtvec (1, XVECEXP (base, 0, 0)),
2833 UNSPEC_LTREL_OFFSET);
2834 else
2835 return false;
2837 base = XVECEXP (base, 0, 1);
2838 break;
2840 case UNSPEC_LTREL_BASE:
2841 if (XVECLEN (base, 0) == 1)
2842 base = fake_pool_base, literal_pool = true;
2843 else
2844 base = XVECEXP (base, 0, 1);
2845 break;
2847 default:
2848 return false;
2851 if (!REG_P (base) || GET_MODE (base) != Pmode)
2852 return false;
2854 if (REGNO (base) == STACK_POINTER_REGNUM
2855 || REGNO (base) == FRAME_POINTER_REGNUM
2856 || ((reload_completed || reload_in_progress)
2857 && frame_pointer_needed
2858 && REGNO (base) == HARD_FRAME_POINTER_REGNUM)
2859 || REGNO (base) == ARG_POINTER_REGNUM
2860 || (flag_pic
2861 && REGNO (base) == PIC_OFFSET_TABLE_REGNUM))
2862 pointer = base_ptr = true;
2864 if ((reload_completed || reload_in_progress)
2865 && base == cfun->machine->base_reg)
2866 pointer = base_ptr = literal_pool = true;
2869 /* Validate index register. */
2870 if (indx)
2872 if (GET_CODE (indx) == UNSPEC)
2873 switch (XINT (indx, 1))
2875 case UNSPEC_LTREF:
2876 if (!disp)
2877 disp = gen_rtx_UNSPEC (Pmode,
2878 gen_rtvec (1, XVECEXP (indx, 0, 0)),
2879 UNSPEC_LTREL_OFFSET);
2880 else
2881 return false;
2883 indx = XVECEXP (indx, 0, 1);
2884 break;
2886 case UNSPEC_LTREL_BASE:
2887 if (XVECLEN (indx, 0) == 1)
2888 indx = fake_pool_base, literal_pool = true;
2889 else
2890 indx = XVECEXP (indx, 0, 1);
2891 break;
2893 default:
2894 return false;
2897 if (!REG_P (indx) || GET_MODE (indx) != Pmode)
2898 return false;
2900 if (REGNO (indx) == STACK_POINTER_REGNUM
2901 || REGNO (indx) == FRAME_POINTER_REGNUM
2902 || ((reload_completed || reload_in_progress)
2903 && frame_pointer_needed
2904 && REGNO (indx) == HARD_FRAME_POINTER_REGNUM)
2905 || REGNO (indx) == ARG_POINTER_REGNUM
2906 || (flag_pic
2907 && REGNO (indx) == PIC_OFFSET_TABLE_REGNUM))
2908 pointer = indx_ptr = true;
2910 if ((reload_completed || reload_in_progress)
2911 && indx == cfun->machine->base_reg)
2912 pointer = indx_ptr = literal_pool = true;
2915 /* Prefer to use pointer as base, not index. */
2916 if (base && indx && !base_ptr
2917 && (indx_ptr || (!REG_POINTER (base) && REG_POINTER (indx))))
2919 rtx tmp = base;
2920 base = indx;
2921 indx = tmp;
2924 /* Validate displacement. */
2925 if (!disp)
2927 /* If virtual registers are involved, the displacement will change later
2928 anyway as the virtual registers get eliminated. This could make a
2929 valid displacement invalid, but it is more likely to make an invalid
2930 displacement valid, because we sometimes access the register save area
2931 via negative offsets to one of those registers.
2932 Thus we don't check the displacement for validity here. If after
2933 elimination the displacement turns out to be invalid after all,
2934 this is fixed up by reload in any case. */
2935 /* LRA maintains always displacements up to date and we need to
2936 know the displacement is right during all LRA not only at the
2937 final elimination. */
2938 if (lra_in_progress
2939 || (base != arg_pointer_rtx
2940 && indx != arg_pointer_rtx
2941 && base != return_address_pointer_rtx
2942 && indx != return_address_pointer_rtx
2943 && base != frame_pointer_rtx
2944 && indx != frame_pointer_rtx
2945 && base != virtual_stack_vars_rtx
2946 && indx != virtual_stack_vars_rtx))
2947 if (!DISP_IN_RANGE (offset))
2948 return false;
2950 else
2952 /* All the special cases are pointers. */
2953 pointer = true;
2955 /* In the small-PIC case, the linker converts @GOT
2956 and @GOTNTPOFF offsets to possible displacements. */
2957 if (GET_CODE (disp) == UNSPEC
2958 && (XINT (disp, 1) == UNSPEC_GOT
2959 || XINT (disp, 1) == UNSPEC_GOTNTPOFF)
2960 && flag_pic == 1)
2965 /* Accept pool label offsets. */
2966 else if (GET_CODE (disp) == UNSPEC
2967 && XINT (disp, 1) == UNSPEC_POOL_OFFSET)
2970 /* Accept literal pool references. */
2971 else if (GET_CODE (disp) == UNSPEC
2972 && XINT (disp, 1) == UNSPEC_LTREL_OFFSET)
2974 /* In case CSE pulled a non literal pool reference out of
2975 the pool we have to reject the address. This is
2976 especially important when loading the GOT pointer on non
2977 zarch CPUs. In this case the literal pool contains an lt
2978 relative offset to the _GLOBAL_OFFSET_TABLE_ label which
2979 will most likely exceed the displacement. */
2980 if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
2981 || !CONSTANT_POOL_ADDRESS_P (XVECEXP (disp, 0, 0)))
2982 return false;
2984 orig_disp = gen_rtx_CONST (Pmode, disp);
2985 if (offset)
2987 /* If we have an offset, make sure it does not
2988 exceed the size of the constant pool entry. */
2989 rtx sym = XVECEXP (disp, 0, 0);
2990 if (offset >= GET_MODE_SIZE (get_pool_mode (sym)))
2991 return false;
2993 orig_disp = plus_constant (Pmode, orig_disp, offset);
2997 else
2998 return false;
3001 if (!base && !indx)
3002 pointer = true;
3004 if (out)
3006 out->base = base;
3007 out->indx = indx;
3008 out->disp = orig_disp;
3009 out->pointer = pointer;
3010 out->literal_pool = literal_pool;
3013 return true;
3016 /* Decompose a RTL expression OP for an address style operand into its
3017 components, and return the base register in BASE and the offset in
3018 OFFSET. While OP looks like an address it is never supposed to be
3019 used as such.
3021 Return true if OP is a valid address operand, false if not. */
3023 bool
3024 s390_decompose_addrstyle_without_index (rtx op, rtx *base,
3025 HOST_WIDE_INT *offset)
3027 rtx off = NULL_RTX;
3029 /* We can have an integer constant, an address register,
3030 or a sum of the two. */
3031 if (CONST_SCALAR_INT_P (op))
3033 off = op;
3034 op = NULL_RTX;
3036 if (op && GET_CODE (op) == PLUS && CONST_SCALAR_INT_P (XEXP (op, 1)))
3038 off = XEXP (op, 1);
3039 op = XEXP (op, 0);
3041 while (op && GET_CODE (op) == SUBREG)
3042 op = SUBREG_REG (op);
3044 if (op && GET_CODE (op) != REG)
3045 return false;
3047 if (offset)
3049 if (off == NULL_RTX)
3050 *offset = 0;
3051 else if (CONST_INT_P (off))
3052 *offset = INTVAL (off);
3053 else if (CONST_WIDE_INT_P (off))
3054 /* The offset will anyway be cut down to 12 bits so take just
3055 the lowest order chunk of the wide int. */
3056 *offset = CONST_WIDE_INT_ELT (off, 0);
3057 else
3058 gcc_unreachable ();
3060 if (base)
3061 *base = op;
3063 return true;
3067 /* Return true if CODE is a valid address without index. */
3069 bool
3070 s390_legitimate_address_without_index_p (rtx op)
3072 struct s390_address addr;
3074 if (!s390_decompose_address (XEXP (op, 0), &addr))
3075 return false;
3076 if (addr.indx)
3077 return false;
3079 return true;
3083 /* Return TRUE if ADDR is an operand valid for a load/store relative
3084 instruction. Be aware that the alignment of the operand needs to
3085 be checked separately.
3086 Valid addresses are single references or a sum of a reference and a
3087 constant integer. Return these parts in SYMREF and ADDEND. You can
3088 pass NULL in REF and/or ADDEND if you are not interested in these
3089 values. Literal pool references are *not* considered symbol
3090 references. */
3092 static bool
3093 s390_loadrelative_operand_p (rtx addr, rtx *symref, HOST_WIDE_INT *addend)
3095 HOST_WIDE_INT tmpaddend = 0;
3097 if (GET_CODE (addr) == CONST)
3098 addr = XEXP (addr, 0);
3100 if (GET_CODE (addr) == PLUS)
3102 if (!CONST_INT_P (XEXP (addr, 1)))
3103 return false;
3105 tmpaddend = INTVAL (XEXP (addr, 1));
3106 addr = XEXP (addr, 0);
3109 if ((GET_CODE (addr) == SYMBOL_REF && !CONSTANT_POOL_ADDRESS_P (addr))
3110 || (GET_CODE (addr) == UNSPEC
3111 && (XINT (addr, 1) == UNSPEC_GOTENT
3112 || (TARGET_CPU_ZARCH && XINT (addr, 1) == UNSPEC_PLT))))
3114 if (symref)
3115 *symref = addr;
3116 if (addend)
3117 *addend = tmpaddend;
3119 return true;
3121 return false;
3124 /* Return true if the address in OP is valid for constraint letter C
3125 if wrapped in a MEM rtx. Set LIT_POOL_OK to true if it literal
3126 pool MEMs should be accepted. Only the Q, R, S, T constraint
3127 letters are allowed for C. */
3129 static int
3130 s390_check_qrst_address (char c, rtx op, bool lit_pool_ok)
3132 struct s390_address addr;
3133 bool decomposed = false;
3135 if (!address_operand (op, GET_MODE (op)))
3136 return 0;
3138 /* This check makes sure that no symbolic address (except literal
3139 pool references) are accepted by the R or T constraints. */
3140 if (s390_loadrelative_operand_p (op, NULL, NULL))
3141 return 0;
3143 /* Ensure literal pool references are only accepted if LIT_POOL_OK. */
3144 if (!lit_pool_ok)
3146 if (!s390_decompose_address (op, &addr))
3147 return 0;
3148 if (addr.literal_pool)
3149 return 0;
3150 decomposed = true;
3153 /* With reload, we sometimes get intermediate address forms that are
3154 actually invalid as-is, but we need to accept them in the most
3155 generic cases below ('R' or 'T'), since reload will in fact fix
3156 them up. LRA behaves differently here; we never see such forms,
3157 but on the other hand, we need to strictly reject every invalid
3158 address form. Perform this check right up front. */
3159 if (lra_in_progress)
3161 if (!decomposed && !s390_decompose_address (op, &addr))
3162 return 0;
3163 decomposed = true;
3166 switch (c)
3168 case 'Q': /* no index short displacement */
3169 if (!decomposed && !s390_decompose_address (op, &addr))
3170 return 0;
3171 if (addr.indx)
3172 return 0;
3173 if (!s390_short_displacement (addr.disp))
3174 return 0;
3175 break;
3177 case 'R': /* with index short displacement */
3178 if (TARGET_LONG_DISPLACEMENT)
3180 if (!decomposed && !s390_decompose_address (op, &addr))
3181 return 0;
3182 if (!s390_short_displacement (addr.disp))
3183 return 0;
3185 /* Any invalid address here will be fixed up by reload,
3186 so accept it for the most generic constraint. */
3187 break;
3189 case 'S': /* no index long displacement */
3190 if (!decomposed && !s390_decompose_address (op, &addr))
3191 return 0;
3192 if (addr.indx)
3193 return 0;
3194 break;
3196 case 'T': /* with index long displacement */
3197 /* Any invalid address here will be fixed up by reload,
3198 so accept it for the most generic constraint. */
3199 break;
3201 default:
3202 return 0;
3204 return 1;
3208 /* Evaluates constraint strings described by the regular expression
3209 ([A|B|Z](Q|R|S|T))|Y and returns 1 if OP is a valid operand for
3210 the constraint given in STR, or 0 else. */
3213 s390_mem_constraint (const char *str, rtx op)
3215 char c = str[0];
3217 switch (c)
3219 case 'A':
3220 /* Check for offsettable variants of memory constraints. */
3221 if (!MEM_P (op) || MEM_VOLATILE_P (op))
3222 return 0;
3223 if ((reload_completed || reload_in_progress)
3224 ? !offsettable_memref_p (op) : !offsettable_nonstrict_memref_p (op))
3225 return 0;
3226 return s390_check_qrst_address (str[1], XEXP (op, 0), true);
3227 case 'B':
3228 /* Check for non-literal-pool variants of memory constraints. */
3229 if (!MEM_P (op))
3230 return 0;
3231 return s390_check_qrst_address (str[1], XEXP (op, 0), false);
3232 case 'Q':
3233 case 'R':
3234 case 'S':
3235 case 'T':
3236 if (GET_CODE (op) != MEM)
3237 return 0;
3238 return s390_check_qrst_address (c, XEXP (op, 0), true);
3239 case 'Y':
3240 /* Simply check for the basic form of a shift count. Reload will
3241 take care of making sure we have a proper base register. */
3242 if (!s390_decompose_addrstyle_without_index (op, NULL, NULL))
3243 return 0;
3244 break;
3245 case 'Z':
3246 return s390_check_qrst_address (str[1], op, true);
3247 default:
3248 return 0;
3250 return 1;
3254 /* Evaluates constraint strings starting with letter O. Input
3255 parameter C is the second letter following the "O" in the constraint
3256 string. Returns 1 if VALUE meets the respective constraint and 0
3257 otherwise. */
3260 s390_O_constraint_str (const char c, HOST_WIDE_INT value)
3262 if (!TARGET_EXTIMM)
3263 return 0;
3265 switch (c)
3267 case 's':
3268 return trunc_int_for_mode (value, SImode) == value;
3270 case 'p':
3271 return value == 0
3272 || s390_single_part (GEN_INT (value), DImode, SImode, 0) == 1;
3274 case 'n':
3275 return s390_single_part (GEN_INT (value - 1), DImode, SImode, -1) == 1;
3277 default:
3278 gcc_unreachable ();
3283 /* Evaluates constraint strings starting with letter N. Parameter STR
3284 contains the letters following letter "N" in the constraint string.
3285 Returns true if VALUE matches the constraint. */
3288 s390_N_constraint_str (const char *str, HOST_WIDE_INT value)
3290 machine_mode mode, part_mode;
3291 int def;
3292 int part, part_goal;
3295 if (str[0] == 'x')
3296 part_goal = -1;
3297 else
3298 part_goal = str[0] - '0';
3300 switch (str[1])
3302 case 'Q':
3303 part_mode = QImode;
3304 break;
3305 case 'H':
3306 part_mode = HImode;
3307 break;
3308 case 'S':
3309 part_mode = SImode;
3310 break;
3311 default:
3312 return 0;
3315 switch (str[2])
3317 case 'H':
3318 mode = HImode;
3319 break;
3320 case 'S':
3321 mode = SImode;
3322 break;
3323 case 'D':
3324 mode = DImode;
3325 break;
3326 default:
3327 return 0;
3330 switch (str[3])
3332 case '0':
3333 def = 0;
3334 break;
3335 case 'F':
3336 def = -1;
3337 break;
3338 default:
3339 return 0;
3342 if (GET_MODE_SIZE (mode) <= GET_MODE_SIZE (part_mode))
3343 return 0;
3345 part = s390_single_part (GEN_INT (value), mode, part_mode, def);
3346 if (part < 0)
3347 return 0;
3348 if (part_goal != -1 && part_goal != part)
3349 return 0;
3351 return 1;
3355 /* Returns true if the input parameter VALUE is a float zero. */
3358 s390_float_const_zero_p (rtx value)
3360 return (GET_MODE_CLASS (GET_MODE (value)) == MODE_FLOAT
3361 && value == CONST0_RTX (GET_MODE (value)));
3364 /* Implement TARGET_REGISTER_MOVE_COST. */
3366 static int
3367 s390_register_move_cost (machine_mode mode,
3368 reg_class_t from, reg_class_t to)
3370 /* On s390, copy between fprs and gprs is expensive. */
3372 /* It becomes somewhat faster having ldgr/lgdr. */
3373 if (TARGET_Z10 && GET_MODE_SIZE (mode) == 8)
3375 /* ldgr is single cycle. */
3376 if (reg_classes_intersect_p (from, GENERAL_REGS)
3377 && reg_classes_intersect_p (to, FP_REGS))
3378 return 1;
3379 /* lgdr needs 3 cycles. */
3380 if (reg_classes_intersect_p (to, GENERAL_REGS)
3381 && reg_classes_intersect_p (from, FP_REGS))
3382 return 3;
3385 /* Otherwise copying is done via memory. */
3386 if ((reg_classes_intersect_p (from, GENERAL_REGS)
3387 && reg_classes_intersect_p (to, FP_REGS))
3388 || (reg_classes_intersect_p (from, FP_REGS)
3389 && reg_classes_intersect_p (to, GENERAL_REGS)))
3390 return 10;
3392 return 1;
3395 /* Implement TARGET_MEMORY_MOVE_COST. */
3397 static int
3398 s390_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
3399 reg_class_t rclass ATTRIBUTE_UNUSED,
3400 bool in ATTRIBUTE_UNUSED)
3402 return 2;
3405 /* Compute a (partial) cost for rtx X. Return true if the complete
3406 cost has been computed, and false if subexpressions should be
3407 scanned. In either case, *TOTAL contains the cost result. The
3408 initial value of *TOTAL is the default value computed by
3409 rtx_cost. It may be left unmodified. OUTER_CODE contains the
3410 code of the superexpression of x. */
3412 static bool
3413 s390_rtx_costs (rtx x, machine_mode mode, int outer_code,
3414 int opno ATTRIBUTE_UNUSED,
3415 int *total, bool speed ATTRIBUTE_UNUSED)
3417 int code = GET_CODE (x);
3418 switch (code)
3420 case CONST:
3421 case CONST_INT:
3422 case LABEL_REF:
3423 case SYMBOL_REF:
3424 case CONST_DOUBLE:
3425 case CONST_WIDE_INT:
3426 case MEM:
3427 *total = 0;
3428 return true;
3430 case SET:
3432 /* Without this a conditional move instruction would be
3433 accounted as 3 * COSTS_N_INSNS (set, if_then_else,
3434 comparison operator). That's a bit pessimistic. */
3436 if (!TARGET_Z196 || GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
3437 return false;
3439 rtx cond = XEXP (SET_SRC (x), 0);
3441 if (!CC_REG_P (XEXP (cond, 0)) || !CONST_INT_P (XEXP (cond, 1)))
3442 return false;
3444 /* It is going to be a load/store on condition. Make it
3445 slightly more expensive than a normal load. */
3446 *total = COSTS_N_INSNS (1) + 1;
3448 rtx dst = SET_DEST (x);
3449 rtx then = XEXP (SET_SRC (x), 1);
3450 rtx els = XEXP (SET_SRC (x), 2);
3452 /* It is a real IF-THEN-ELSE. An additional move will be
3453 needed to implement that. */
3454 if (reload_completed
3455 && !rtx_equal_p (dst, then)
3456 && !rtx_equal_p (dst, els))
3457 *total += COSTS_N_INSNS (1) / 2;
3459 /* A minor penalty for constants we cannot directly handle. */
3460 if ((CONST_INT_P (then) || CONST_INT_P (els))
3461 && (!TARGET_Z13 || MEM_P (dst)
3462 || (CONST_INT_P (then) && !satisfies_constraint_K (then))
3463 || (CONST_INT_P (els) && !satisfies_constraint_K (els))))
3464 *total += COSTS_N_INSNS (1) / 2;
3466 /* A store on condition can only handle register src operands. */
3467 if (MEM_P (dst) && (!REG_P (then) || !REG_P (els)))
3468 *total += COSTS_N_INSNS (1) / 2;
3470 return true;
3472 case IOR:
3473 /* risbg */
3474 if (GET_CODE (XEXP (x, 0)) == AND
3475 && GET_CODE (XEXP (x, 1)) == ASHIFT
3476 && REG_P (XEXP (XEXP (x, 0), 0))
3477 && REG_P (XEXP (XEXP (x, 1), 0))
3478 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
3479 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
3480 && (UINTVAL (XEXP (XEXP (x, 0), 1)) ==
3481 (HOST_WIDE_INT_1U << UINTVAL (XEXP (XEXP (x, 1), 1))) - 1))
3483 *total = COSTS_N_INSNS (2);
3484 return true;
3487 /* ~AND on a 128 bit mode. This can be done using a vector
3488 instruction. */
3489 if (TARGET_VXE
3490 && GET_CODE (XEXP (x, 0)) == NOT
3491 && GET_CODE (XEXP (x, 1)) == NOT
3492 && REG_P (XEXP (XEXP (x, 0), 0))
3493 && REG_P (XEXP (XEXP (x, 1), 0))
3494 && GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 0), 0))) == 16
3495 && s390_hard_regno_mode_ok (VR0_REGNUM,
3496 GET_MODE (XEXP (XEXP (x, 0), 0))))
3498 *total = COSTS_N_INSNS (1);
3499 return true;
3501 /* fallthrough */
3502 case ASHIFT:
3503 case ASHIFTRT:
3504 case LSHIFTRT:
3505 case ROTATE:
3506 case ROTATERT:
3507 case AND:
3508 case XOR:
3509 case NEG:
3510 case NOT:
3511 *total = COSTS_N_INSNS (1);
3512 return false;
3514 case PLUS:
3515 case MINUS:
3516 *total = COSTS_N_INSNS (1);
3517 return false;
3519 case MULT:
3520 switch (mode)
3522 case E_SImode:
3524 rtx left = XEXP (x, 0);
3525 rtx right = XEXP (x, 1);
3526 if (GET_CODE (right) == CONST_INT
3527 && CONST_OK_FOR_K (INTVAL (right)))
3528 *total = s390_cost->mhi;
3529 else if (GET_CODE (left) == SIGN_EXTEND)
3530 *total = s390_cost->mh;
3531 else
3532 *total = s390_cost->ms; /* msr, ms, msy */
3533 break;
3535 case E_DImode:
3537 rtx left = XEXP (x, 0);
3538 rtx right = XEXP (x, 1);
3539 if (TARGET_ZARCH)
3541 if (GET_CODE (right) == CONST_INT
3542 && CONST_OK_FOR_K (INTVAL (right)))
3543 *total = s390_cost->mghi;
3544 else if (GET_CODE (left) == SIGN_EXTEND)
3545 *total = s390_cost->msgf;
3546 else
3547 *total = s390_cost->msg; /* msgr, msg */
3549 else /* TARGET_31BIT */
3551 if (GET_CODE (left) == SIGN_EXTEND
3552 && GET_CODE (right) == SIGN_EXTEND)
3553 /* mulsidi case: mr, m */
3554 *total = s390_cost->m;
3555 else if (GET_CODE (left) == ZERO_EXTEND
3556 && GET_CODE (right) == ZERO_EXTEND
3557 && TARGET_CPU_ZARCH)
3558 /* umulsidi case: ml, mlr */
3559 *total = s390_cost->ml;
3560 else
3561 /* Complex calculation is required. */
3562 *total = COSTS_N_INSNS (40);
3564 break;
3566 case E_SFmode:
3567 case E_DFmode:
3568 *total = s390_cost->mult_df;
3569 break;
3570 case E_TFmode:
3571 *total = s390_cost->mxbr;
3572 break;
3573 default:
3574 return false;
3576 return false;
3578 case FMA:
3579 switch (mode)
3581 case E_DFmode:
3582 *total = s390_cost->madbr;
3583 break;
3584 case E_SFmode:
3585 *total = s390_cost->maebr;
3586 break;
3587 default:
3588 return false;
3590 /* Negate in the third argument is free: FMSUB. */
3591 if (GET_CODE (XEXP (x, 2)) == NEG)
3593 *total += (rtx_cost (XEXP (x, 0), mode, FMA, 0, speed)
3594 + rtx_cost (XEXP (x, 1), mode, FMA, 1, speed)
3595 + rtx_cost (XEXP (XEXP (x, 2), 0), mode, FMA, 2, speed));
3596 return true;
3598 return false;
3600 case UDIV:
3601 case UMOD:
3602 if (mode == TImode) /* 128 bit division */
3603 *total = s390_cost->dlgr;
3604 else if (mode == DImode)
3606 rtx right = XEXP (x, 1);
3607 if (GET_CODE (right) == ZERO_EXTEND) /* 64 by 32 bit division */
3608 *total = s390_cost->dlr;
3609 else /* 64 by 64 bit division */
3610 *total = s390_cost->dlgr;
3612 else if (mode == SImode) /* 32 bit division */
3613 *total = s390_cost->dlr;
3614 return false;
3616 case DIV:
3617 case MOD:
3618 if (mode == DImode)
3620 rtx right = XEXP (x, 1);
3621 if (GET_CODE (right) == ZERO_EXTEND) /* 64 by 32 bit division */
3622 if (TARGET_ZARCH)
3623 *total = s390_cost->dsgfr;
3624 else
3625 *total = s390_cost->dr;
3626 else /* 64 by 64 bit division */
3627 *total = s390_cost->dsgr;
3629 else if (mode == SImode) /* 32 bit division */
3630 *total = s390_cost->dlr;
3631 else if (mode == SFmode)
3633 *total = s390_cost->debr;
3635 else if (mode == DFmode)
3637 *total = s390_cost->ddbr;
3639 else if (mode == TFmode)
3641 *total = s390_cost->dxbr;
3643 return false;
3645 case SQRT:
3646 if (mode == SFmode)
3647 *total = s390_cost->sqebr;
3648 else if (mode == DFmode)
3649 *total = s390_cost->sqdbr;
3650 else /* TFmode */
3651 *total = s390_cost->sqxbr;
3652 return false;
3654 case SIGN_EXTEND:
3655 case ZERO_EXTEND:
3656 if (outer_code == MULT || outer_code == DIV || outer_code == MOD
3657 || outer_code == PLUS || outer_code == MINUS
3658 || outer_code == COMPARE)
3659 *total = 0;
3660 return false;
3662 case COMPARE:
3663 *total = COSTS_N_INSNS (1);
3664 if (GET_CODE (XEXP (x, 0)) == AND
3665 && GET_CODE (XEXP (x, 1)) == CONST_INT
3666 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
3668 rtx op0 = XEXP (XEXP (x, 0), 0);
3669 rtx op1 = XEXP (XEXP (x, 0), 1);
3670 rtx op2 = XEXP (x, 1);
3672 if (memory_operand (op0, GET_MODE (op0))
3673 && s390_tm_ccmode (op1, op2, 0) != VOIDmode)
3674 return true;
3675 if (register_operand (op0, GET_MODE (op0))
3676 && s390_tm_ccmode (op1, op2, 1) != VOIDmode)
3677 return true;
3679 return false;
3681 default:
3682 return false;
3686 /* Return the cost of an address rtx ADDR. */
3688 static int
3689 s390_address_cost (rtx addr, machine_mode mode ATTRIBUTE_UNUSED,
3690 addr_space_t as ATTRIBUTE_UNUSED,
3691 bool speed ATTRIBUTE_UNUSED)
3693 struct s390_address ad;
3694 if (!s390_decompose_address (addr, &ad))
3695 return 1000;
3697 return ad.indx? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (1);
3700 /* Implement targetm.vectorize.builtin_vectorization_cost. */
3701 static int
3702 s390_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
3703 tree vectype,
3704 int misalign ATTRIBUTE_UNUSED)
3706 switch (type_of_cost)
3708 case scalar_stmt:
3709 case scalar_load:
3710 case scalar_store:
3711 case vector_stmt:
3712 case vector_load:
3713 case vector_store:
3714 case vec_to_scalar:
3715 case scalar_to_vec:
3716 case cond_branch_not_taken:
3717 case vec_perm:
3718 case vec_promote_demote:
3719 case unaligned_load:
3720 case unaligned_store:
3721 return 1;
3723 case cond_branch_taken:
3724 return 3;
3726 case vec_construct:
3727 return TYPE_VECTOR_SUBPARTS (vectype) - 1;
3729 default:
3730 gcc_unreachable ();
3734 /* If OP is a SYMBOL_REF of a thread-local symbol, return its TLS mode,
3735 otherwise return 0. */
3738 tls_symbolic_operand (rtx op)
3740 if (GET_CODE (op) != SYMBOL_REF)
3741 return 0;
3742 return SYMBOL_REF_TLS_MODEL (op);
3745 /* Split DImode access register reference REG (on 64-bit) into its constituent
3746 low and high parts, and store them into LO and HI. Note that gen_lowpart/
3747 gen_highpart cannot be used as they assume all registers are word-sized,
3748 while our access registers have only half that size. */
3750 void
3751 s390_split_access_reg (rtx reg, rtx *lo, rtx *hi)
3753 gcc_assert (TARGET_64BIT);
3754 gcc_assert (ACCESS_REG_P (reg));
3755 gcc_assert (GET_MODE (reg) == DImode);
3756 gcc_assert (!(REGNO (reg) & 1));
3758 *lo = gen_rtx_REG (SImode, REGNO (reg) + 1);
3759 *hi = gen_rtx_REG (SImode, REGNO (reg));
3762 /* Return true if OP contains a symbol reference */
3764 bool
3765 symbolic_reference_mentioned_p (rtx op)
3767 const char *fmt;
3768 int i;
3770 if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
3771 return 1;
3773 fmt = GET_RTX_FORMAT (GET_CODE (op));
3774 for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
3776 if (fmt[i] == 'E')
3778 int j;
3780 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
3781 if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
3782 return 1;
3785 else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
3786 return 1;
3789 return 0;
3792 /* Return true if OP contains a reference to a thread-local symbol. */
3794 bool
3795 tls_symbolic_reference_mentioned_p (rtx op)
3797 const char *fmt;
3798 int i;
3800 if (GET_CODE (op) == SYMBOL_REF)
3801 return tls_symbolic_operand (op);
3803 fmt = GET_RTX_FORMAT (GET_CODE (op));
3804 for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
3806 if (fmt[i] == 'E')
3808 int j;
3810 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
3811 if (tls_symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
3812 return true;
3815 else if (fmt[i] == 'e' && tls_symbolic_reference_mentioned_p (XEXP (op, i)))
3816 return true;
3819 return false;
3823 /* Return true if OP is a legitimate general operand when
3824 generating PIC code. It is given that flag_pic is on
3825 and that OP satisfies CONSTANT_P. */
3828 legitimate_pic_operand_p (rtx op)
3830 /* Accept all non-symbolic constants. */
3831 if (!SYMBOLIC_CONST (op))
3832 return 1;
3834 /* Reject everything else; must be handled
3835 via emit_symbolic_move. */
3836 return 0;
3839 /* Returns true if the constant value OP is a legitimate general operand.
3840 It is given that OP satisfies CONSTANT_P. */
3842 static bool
3843 s390_legitimate_constant_p (machine_mode mode, rtx op)
3845 if (TARGET_VX && VECTOR_MODE_P (mode) && GET_CODE (op) == CONST_VECTOR)
3847 if (GET_MODE_SIZE (mode) != 16)
3848 return 0;
3850 if (!satisfies_constraint_j00 (op)
3851 && !satisfies_constraint_jm1 (op)
3852 && !satisfies_constraint_jKK (op)
3853 && !satisfies_constraint_jxx (op)
3854 && !satisfies_constraint_jyy (op))
3855 return 0;
3858 /* Accept all non-symbolic constants. */
3859 if (!SYMBOLIC_CONST (op))
3860 return 1;
3862 /* Accept immediate LARL operands. */
3863 if (TARGET_CPU_ZARCH && larl_operand (op, mode))
3864 return 1;
3866 /* Thread-local symbols are never legal constants. This is
3867 so that emit_call knows that computing such addresses
3868 might require a function call. */
3869 if (TLS_SYMBOLIC_CONST (op))
3870 return 0;
3872 /* In the PIC case, symbolic constants must *not* be
3873 forced into the literal pool. We accept them here,
3874 so that they will be handled by emit_symbolic_move. */
3875 if (flag_pic)
3876 return 1;
3878 /* All remaining non-PIC symbolic constants are
3879 forced into the literal pool. */
3880 return 0;
3883 /* Determine if it's legal to put X into the constant pool. This
3884 is not possible if X contains the address of a symbol that is
3885 not constant (TLS) or not known at final link time (PIC). */
3887 static bool
3888 s390_cannot_force_const_mem (machine_mode mode, rtx x)
3890 switch (GET_CODE (x))
3892 case CONST_INT:
3893 case CONST_DOUBLE:
3894 case CONST_WIDE_INT:
3895 case CONST_VECTOR:
3896 /* Accept all non-symbolic constants. */
3897 return false;
3899 case LABEL_REF:
3900 /* Labels are OK iff we are non-PIC. */
3901 return flag_pic != 0;
3903 case SYMBOL_REF:
3904 /* 'Naked' TLS symbol references are never OK,
3905 non-TLS symbols are OK iff we are non-PIC. */
3906 if (tls_symbolic_operand (x))
3907 return true;
3908 else
3909 return flag_pic != 0;
3911 case CONST:
3912 return s390_cannot_force_const_mem (mode, XEXP (x, 0));
3913 case PLUS:
3914 case MINUS:
3915 return s390_cannot_force_const_mem (mode, XEXP (x, 0))
3916 || s390_cannot_force_const_mem (mode, XEXP (x, 1));
3918 case UNSPEC:
3919 switch (XINT (x, 1))
3921 /* Only lt-relative or GOT-relative UNSPECs are OK. */
3922 case UNSPEC_LTREL_OFFSET:
3923 case UNSPEC_GOT:
3924 case UNSPEC_GOTOFF:
3925 case UNSPEC_PLTOFF:
3926 case UNSPEC_TLSGD:
3927 case UNSPEC_TLSLDM:
3928 case UNSPEC_NTPOFF:
3929 case UNSPEC_DTPOFF:
3930 case UNSPEC_GOTNTPOFF:
3931 case UNSPEC_INDNTPOFF:
3932 return false;
3934 /* If the literal pool shares the code section, be put
3935 execute template placeholders into the pool as well. */
3936 case UNSPEC_INSN:
3937 return TARGET_CPU_ZARCH;
3939 default:
3940 return true;
3942 break;
3944 default:
3945 gcc_unreachable ();
3949 /* Returns true if the constant value OP is a legitimate general
3950 operand during and after reload. The difference to
3951 legitimate_constant_p is that this function will not accept
3952 a constant that would need to be forced to the literal pool
3953 before it can be used as operand.
3954 This function accepts all constants which can be loaded directly
3955 into a GPR. */
3957 bool
3958 legitimate_reload_constant_p (rtx op)
3960 /* Accept la(y) operands. */
3961 if (GET_CODE (op) == CONST_INT
3962 && DISP_IN_RANGE (INTVAL (op)))
3963 return true;
3965 /* Accept l(g)hi/l(g)fi operands. */
3966 if (GET_CODE (op) == CONST_INT
3967 && (CONST_OK_FOR_K (INTVAL (op)) || CONST_OK_FOR_Os (INTVAL (op))))
3968 return true;
3970 /* Accept lliXX operands. */
3971 if (TARGET_ZARCH
3972 && GET_CODE (op) == CONST_INT
3973 && trunc_int_for_mode (INTVAL (op), word_mode) == INTVAL (op)
3974 && s390_single_part (op, word_mode, HImode, 0) >= 0)
3975 return true;
3977 if (TARGET_EXTIMM
3978 && GET_CODE (op) == CONST_INT
3979 && trunc_int_for_mode (INTVAL (op), word_mode) == INTVAL (op)
3980 && s390_single_part (op, word_mode, SImode, 0) >= 0)
3981 return true;
3983 /* Accept larl operands. */
3984 if (TARGET_CPU_ZARCH
3985 && larl_operand (op, VOIDmode))
3986 return true;
3988 /* Accept floating-point zero operands that fit into a single GPR. */
3989 if (GET_CODE (op) == CONST_DOUBLE
3990 && s390_float_const_zero_p (op)
3991 && GET_MODE_SIZE (GET_MODE (op)) <= UNITS_PER_WORD)
3992 return true;
3994 /* Accept double-word operands that can be split. */
3995 if (GET_CODE (op) == CONST_WIDE_INT
3996 || (GET_CODE (op) == CONST_INT
3997 && trunc_int_for_mode (INTVAL (op), word_mode) != INTVAL (op)))
3999 machine_mode dword_mode = word_mode == SImode ? DImode : TImode;
4000 rtx hi = operand_subword (op, 0, 0, dword_mode);
4001 rtx lo = operand_subword (op, 1, 0, dword_mode);
4002 return legitimate_reload_constant_p (hi)
4003 && legitimate_reload_constant_p (lo);
4006 /* Everything else cannot be handled without reload. */
4007 return false;
4010 /* Returns true if the constant value OP is a legitimate fp operand
4011 during and after reload.
4012 This function accepts all constants which can be loaded directly
4013 into an FPR. */
4015 static bool
4016 legitimate_reload_fp_constant_p (rtx op)
4018 /* Accept floating-point zero operands if the load zero instruction
4019 can be used. Prior to z196 the load fp zero instruction caused a
4020 performance penalty if the result is used as BFP number. */
4021 if (TARGET_Z196
4022 && GET_CODE (op) == CONST_DOUBLE
4023 && s390_float_const_zero_p (op))
4024 return true;
4026 return false;
4029 /* Returns true if the constant value OP is a legitimate vector operand
4030 during and after reload.
4031 This function accepts all constants which can be loaded directly
4032 into an VR. */
4034 static bool
4035 legitimate_reload_vector_constant_p (rtx op)
4037 if (TARGET_VX && GET_MODE_SIZE (GET_MODE (op)) == 16
4038 && (satisfies_constraint_j00 (op)
4039 || satisfies_constraint_jm1 (op)
4040 || satisfies_constraint_jKK (op)
4041 || satisfies_constraint_jxx (op)
4042 || satisfies_constraint_jyy (op)))
4043 return true;
4045 return false;
4048 /* Given an rtx OP being reloaded into a reg required to be in class RCLASS,
4049 return the class of reg to actually use. */
4051 static reg_class_t
4052 s390_preferred_reload_class (rtx op, reg_class_t rclass)
4054 switch (GET_CODE (op))
4056 /* Constants we cannot reload into general registers
4057 must be forced into the literal pool. */
4058 case CONST_VECTOR:
4059 case CONST_DOUBLE:
4060 case CONST_INT:
4061 case CONST_WIDE_INT:
4062 if (reg_class_subset_p (GENERAL_REGS, rclass)
4063 && legitimate_reload_constant_p (op))
4064 return GENERAL_REGS;
4065 else if (reg_class_subset_p (ADDR_REGS, rclass)
4066 && legitimate_reload_constant_p (op))
4067 return ADDR_REGS;
4068 else if (reg_class_subset_p (FP_REGS, rclass)
4069 && legitimate_reload_fp_constant_p (op))
4070 return FP_REGS;
4071 else if (reg_class_subset_p (VEC_REGS, rclass)
4072 && legitimate_reload_vector_constant_p (op))
4073 return VEC_REGS;
4075 return NO_REGS;
4077 /* If a symbolic constant or a PLUS is reloaded,
4078 it is most likely being used as an address, so
4079 prefer ADDR_REGS. If 'class' is not a superset
4080 of ADDR_REGS, e.g. FP_REGS, reject this reload. */
4081 case CONST:
4082 /* Symrefs cannot be pushed into the literal pool with -fPIC
4083 so we *MUST NOT* return NO_REGS for these cases
4084 (s390_cannot_force_const_mem will return true).
4086 On the other hand we MUST return NO_REGS for symrefs with
4087 invalid addend which might have been pushed to the literal
4088 pool (no -fPIC). Usually we would expect them to be
4089 handled via secondary reload but this does not happen if
4090 they are used as literal pool slot replacement in reload
4091 inheritance (see emit_input_reload_insns). */
4092 if (TARGET_CPU_ZARCH
4093 && GET_CODE (XEXP (op, 0)) == PLUS
4094 && GET_CODE (XEXP (XEXP(op, 0), 0)) == SYMBOL_REF
4095 && GET_CODE (XEXP (XEXP(op, 0), 1)) == CONST_INT)
4097 if (flag_pic && reg_class_subset_p (ADDR_REGS, rclass))
4098 return ADDR_REGS;
4099 else
4100 return NO_REGS;
4102 /* fallthrough */
4103 case LABEL_REF:
4104 case SYMBOL_REF:
4105 if (!legitimate_reload_constant_p (op))
4106 return NO_REGS;
4107 /* fallthrough */
4108 case PLUS:
4109 /* load address will be used. */
4110 if (reg_class_subset_p (ADDR_REGS, rclass))
4111 return ADDR_REGS;
4112 else
4113 return NO_REGS;
4115 default:
4116 break;
4119 return rclass;
4122 /* Return true if ADDR is SYMBOL_REF + addend with addend being a
4123 multiple of ALIGNMENT and the SYMBOL_REF being naturally
4124 aligned. */
4126 bool
4127 s390_check_symref_alignment (rtx addr, HOST_WIDE_INT alignment)
4129 HOST_WIDE_INT addend;
4130 rtx symref;
4132 /* The "required alignment" might be 0 (e.g. for certain structs
4133 accessed via BLKmode). Early abort in this case, as well as when
4134 an alignment > 8 is required. */
4135 if (alignment < 2 || alignment > 8)
4136 return false;
4138 if (!s390_loadrelative_operand_p (addr, &symref, &addend))
4139 return false;
4141 if (addend & (alignment - 1))
4142 return false;
4144 if (GET_CODE (symref) == SYMBOL_REF)
4146 /* We have load-relative instructions for 2-byte, 4-byte, and
4147 8-byte alignment so allow only these. */
4148 switch (alignment)
4150 case 8: return !SYMBOL_FLAG_NOTALIGN8_P (symref);
4151 case 4: return !SYMBOL_FLAG_NOTALIGN4_P (symref);
4152 case 2: return !SYMBOL_FLAG_NOTALIGN2_P (symref);
4153 default: return false;
4157 if (GET_CODE (symref) == UNSPEC
4158 && alignment <= UNITS_PER_LONG)
4159 return true;
4161 return false;
4164 /* ADDR is moved into REG using larl. If ADDR isn't a valid larl
4165 operand SCRATCH is used to reload the even part of the address and
4166 adding one. */
4168 void
4169 s390_reload_larl_operand (rtx reg, rtx addr, rtx scratch)
4171 HOST_WIDE_INT addend;
4172 rtx symref;
4174 if (!s390_loadrelative_operand_p (addr, &symref, &addend))
4175 gcc_unreachable ();
4177 if (!(addend & 1))
4178 /* Easy case. The addend is even so larl will do fine. */
4179 emit_move_insn (reg, addr);
4180 else
4182 /* We can leave the scratch register untouched if the target
4183 register is a valid base register. */
4184 if (REGNO (reg) < FIRST_PSEUDO_REGISTER
4185 && REGNO_REG_CLASS (REGNO (reg)) == ADDR_REGS)
4186 scratch = reg;
4188 gcc_assert (REGNO (scratch) < FIRST_PSEUDO_REGISTER);
4189 gcc_assert (REGNO_REG_CLASS (REGNO (scratch)) == ADDR_REGS);
4191 if (addend != 1)
4192 emit_move_insn (scratch,
4193 gen_rtx_CONST (Pmode,
4194 gen_rtx_PLUS (Pmode, symref,
4195 GEN_INT (addend - 1))));
4196 else
4197 emit_move_insn (scratch, symref);
4199 /* Increment the address using la in order to avoid clobbering cc. */
4200 s390_load_address (reg, gen_rtx_PLUS (Pmode, scratch, const1_rtx));
4204 /* Generate what is necessary to move between REG and MEM using
4205 SCRATCH. The direction is given by TOMEM. */
4207 void
4208 s390_reload_symref_address (rtx reg, rtx mem, rtx scratch, bool tomem)
4210 /* Reload might have pulled a constant out of the literal pool.
4211 Force it back in. */
4212 if (CONST_INT_P (mem) || GET_CODE (mem) == CONST_DOUBLE
4213 || GET_CODE (mem) == CONST_WIDE_INT
4214 || GET_CODE (mem) == CONST_VECTOR
4215 || GET_CODE (mem) == CONST)
4216 mem = force_const_mem (GET_MODE (reg), mem);
4218 gcc_assert (MEM_P (mem));
4220 /* For a load from memory we can leave the scratch register
4221 untouched if the target register is a valid base register. */
4222 if (!tomem
4223 && REGNO (reg) < FIRST_PSEUDO_REGISTER
4224 && REGNO_REG_CLASS (REGNO (reg)) == ADDR_REGS
4225 && GET_MODE (reg) == GET_MODE (scratch))
4226 scratch = reg;
4228 /* Load address into scratch register. Since we can't have a
4229 secondary reload for a secondary reload we have to cover the case
4230 where larl would need a secondary reload here as well. */
4231 s390_reload_larl_operand (scratch, XEXP (mem, 0), scratch);
4233 /* Now we can use a standard load/store to do the move. */
4234 if (tomem)
4235 emit_move_insn (replace_equiv_address (mem, scratch), reg);
4236 else
4237 emit_move_insn (reg, replace_equiv_address (mem, scratch));
4240 /* Inform reload about cases where moving X with a mode MODE to a register in
4241 RCLASS requires an extra scratch or immediate register. Return the class
4242 needed for the immediate register. */
4244 static reg_class_t
4245 s390_secondary_reload (bool in_p, rtx x, reg_class_t rclass_i,
4246 machine_mode mode, secondary_reload_info *sri)
4248 enum reg_class rclass = (enum reg_class) rclass_i;
4250 /* Intermediate register needed. */
4251 if (reg_classes_intersect_p (CC_REGS, rclass))
4252 return GENERAL_REGS;
4254 if (TARGET_VX)
4256 /* The vst/vl vector move instructions allow only for short
4257 displacements. */
4258 if (MEM_P (x)
4259 && GET_CODE (XEXP (x, 0)) == PLUS
4260 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4261 && !SHORT_DISP_IN_RANGE(INTVAL (XEXP (XEXP (x, 0), 1)))
4262 && reg_class_subset_p (rclass, VEC_REGS)
4263 && (!reg_class_subset_p (rclass, FP_REGS)
4264 || (GET_MODE_SIZE (mode) > 8
4265 && s390_class_max_nregs (FP_REGS, mode) == 1)))
4267 if (in_p)
4268 sri->icode = (TARGET_64BIT ?
4269 CODE_FOR_reloaddi_la_in :
4270 CODE_FOR_reloadsi_la_in);
4271 else
4272 sri->icode = (TARGET_64BIT ?
4273 CODE_FOR_reloaddi_la_out :
4274 CODE_FOR_reloadsi_la_out);
4278 if (TARGET_Z10)
4280 HOST_WIDE_INT offset;
4281 rtx symref;
4283 /* On z10 several optimizer steps may generate larl operands with
4284 an odd addend. */
4285 if (in_p
4286 && s390_loadrelative_operand_p (x, &symref, &offset)
4287 && mode == Pmode
4288 && !SYMBOL_FLAG_NOTALIGN2_P (symref)
4289 && (offset & 1) == 1)
4290 sri->icode = ((mode == DImode) ? CODE_FOR_reloaddi_larl_odd_addend_z10
4291 : CODE_FOR_reloadsi_larl_odd_addend_z10);
4293 /* Handle all the (mem (symref)) accesses we cannot use the z10
4294 instructions for. */
4295 if (MEM_P (x)
4296 && s390_loadrelative_operand_p (XEXP (x, 0), NULL, NULL)
4297 && (mode == QImode
4298 || !reg_class_subset_p (rclass, GENERAL_REGS)
4299 || GET_MODE_SIZE (mode) > UNITS_PER_WORD
4300 || !s390_check_symref_alignment (XEXP (x, 0),
4301 GET_MODE_SIZE (mode))))
4303 #define __SECONDARY_RELOAD_CASE(M,m) \
4304 case E_##M##mode: \
4305 if (TARGET_64BIT) \
4306 sri->icode = in_p ? CODE_FOR_reload##m##di_toreg_z10 : \
4307 CODE_FOR_reload##m##di_tomem_z10; \
4308 else \
4309 sri->icode = in_p ? CODE_FOR_reload##m##si_toreg_z10 : \
4310 CODE_FOR_reload##m##si_tomem_z10; \
4311 break;
4313 switch (GET_MODE (x))
4315 __SECONDARY_RELOAD_CASE (QI, qi);
4316 __SECONDARY_RELOAD_CASE (HI, hi);
4317 __SECONDARY_RELOAD_CASE (SI, si);
4318 __SECONDARY_RELOAD_CASE (DI, di);
4319 __SECONDARY_RELOAD_CASE (TI, ti);
4320 __SECONDARY_RELOAD_CASE (SF, sf);
4321 __SECONDARY_RELOAD_CASE (DF, df);
4322 __SECONDARY_RELOAD_CASE (TF, tf);
4323 __SECONDARY_RELOAD_CASE (SD, sd);
4324 __SECONDARY_RELOAD_CASE (DD, dd);
4325 __SECONDARY_RELOAD_CASE (TD, td);
4326 __SECONDARY_RELOAD_CASE (V1QI, v1qi);
4327 __SECONDARY_RELOAD_CASE (V2QI, v2qi);
4328 __SECONDARY_RELOAD_CASE (V4QI, v4qi);
4329 __SECONDARY_RELOAD_CASE (V8QI, v8qi);
4330 __SECONDARY_RELOAD_CASE (V16QI, v16qi);
4331 __SECONDARY_RELOAD_CASE (V1HI, v1hi);
4332 __SECONDARY_RELOAD_CASE (V2HI, v2hi);
4333 __SECONDARY_RELOAD_CASE (V4HI, v4hi);
4334 __SECONDARY_RELOAD_CASE (V8HI, v8hi);
4335 __SECONDARY_RELOAD_CASE (V1SI, v1si);
4336 __SECONDARY_RELOAD_CASE (V2SI, v2si);
4337 __SECONDARY_RELOAD_CASE (V4SI, v4si);
4338 __SECONDARY_RELOAD_CASE (V1DI, v1di);
4339 __SECONDARY_RELOAD_CASE (V2DI, v2di);
4340 __SECONDARY_RELOAD_CASE (V1TI, v1ti);
4341 __SECONDARY_RELOAD_CASE (V1SF, v1sf);
4342 __SECONDARY_RELOAD_CASE (V2SF, v2sf);
4343 __SECONDARY_RELOAD_CASE (V4SF, v4sf);
4344 __SECONDARY_RELOAD_CASE (V1DF, v1df);
4345 __SECONDARY_RELOAD_CASE (V2DF, v2df);
4346 __SECONDARY_RELOAD_CASE (V1TF, v1tf);
4347 default:
4348 gcc_unreachable ();
4350 #undef __SECONDARY_RELOAD_CASE
4354 /* We need a scratch register when loading a PLUS expression which
4355 is not a legitimate operand of the LOAD ADDRESS instruction. */
4356 /* LRA can deal with transformation of plus op very well -- so we
4357 don't need to prompt LRA in this case. */
4358 if (! lra_in_progress && in_p && s390_plus_operand (x, mode))
4359 sri->icode = (TARGET_64BIT ?
4360 CODE_FOR_reloaddi_plus : CODE_FOR_reloadsi_plus);
4362 /* Performing a multiword move from or to memory we have to make sure the
4363 second chunk in memory is addressable without causing a displacement
4364 overflow. If that would be the case we calculate the address in
4365 a scratch register. */
4366 if (MEM_P (x)
4367 && GET_CODE (XEXP (x, 0)) == PLUS
4368 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4369 && !DISP_IN_RANGE (INTVAL (XEXP (XEXP (x, 0), 1))
4370 + GET_MODE_SIZE (mode) - 1))
4372 /* For GENERAL_REGS a displacement overflow is no problem if occurring
4373 in a s_operand address since we may fallback to lm/stm. So we only
4374 have to care about overflows in the b+i+d case. */
4375 if ((reg_classes_intersect_p (GENERAL_REGS, rclass)
4376 && s390_class_max_nregs (GENERAL_REGS, mode) > 1
4377 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS)
4378 /* For FP_REGS no lm/stm is available so this check is triggered
4379 for displacement overflows in b+i+d and b+d like addresses. */
4380 || (reg_classes_intersect_p (FP_REGS, rclass)
4381 && s390_class_max_nregs (FP_REGS, mode) > 1))
4383 if (in_p)
4384 sri->icode = (TARGET_64BIT ?
4385 CODE_FOR_reloaddi_la_in :
4386 CODE_FOR_reloadsi_la_in);
4387 else
4388 sri->icode = (TARGET_64BIT ?
4389 CODE_FOR_reloaddi_la_out :
4390 CODE_FOR_reloadsi_la_out);
4394 /* A scratch address register is needed when a symbolic constant is
4395 copied to r0 compiling with -fPIC. In other cases the target
4396 register might be used as temporary (see legitimize_pic_address). */
4397 if (in_p && SYMBOLIC_CONST (x) && flag_pic == 2 && rclass != ADDR_REGS)
4398 sri->icode = (TARGET_64BIT ?
4399 CODE_FOR_reloaddi_PIC_addr :
4400 CODE_FOR_reloadsi_PIC_addr);
4402 /* Either scratch or no register needed. */
4403 return NO_REGS;
4406 /* Generate code to load SRC, which is PLUS that is not a
4407 legitimate operand for the LA instruction, into TARGET.
4408 SCRATCH may be used as scratch register. */
4410 void
4411 s390_expand_plus_operand (rtx target, rtx src,
4412 rtx scratch)
4414 rtx sum1, sum2;
4415 struct s390_address ad;
4417 /* src must be a PLUS; get its two operands. */
4418 gcc_assert (GET_CODE (src) == PLUS);
4419 gcc_assert (GET_MODE (src) == Pmode);
4421 /* Check if any of the two operands is already scheduled
4422 for replacement by reload. This can happen e.g. when
4423 float registers occur in an address. */
4424 sum1 = find_replacement (&XEXP (src, 0));
4425 sum2 = find_replacement (&XEXP (src, 1));
4426 src = gen_rtx_PLUS (Pmode, sum1, sum2);
4428 /* If the address is already strictly valid, there's nothing to do. */
4429 if (!s390_decompose_address (src, &ad)
4430 || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
4431 || (ad.indx && !REGNO_OK_FOR_INDEX_P (REGNO (ad.indx))))
4433 /* Otherwise, one of the operands cannot be an address register;
4434 we reload its value into the scratch register. */
4435 if (true_regnum (sum1) < 1 || true_regnum (sum1) > 15)
4437 emit_move_insn (scratch, sum1);
4438 sum1 = scratch;
4440 if (true_regnum (sum2) < 1 || true_regnum (sum2) > 15)
4442 emit_move_insn (scratch, sum2);
4443 sum2 = scratch;
4446 /* According to the way these invalid addresses are generated
4447 in reload.c, it should never happen (at least on s390) that
4448 *neither* of the PLUS components, after find_replacements
4449 was applied, is an address register. */
4450 if (sum1 == scratch && sum2 == scratch)
4452 debug_rtx (src);
4453 gcc_unreachable ();
4456 src = gen_rtx_PLUS (Pmode, sum1, sum2);
4459 /* Emit the LOAD ADDRESS pattern. Note that reload of PLUS
4460 is only ever performed on addresses, so we can mark the
4461 sum as legitimate for LA in any case. */
4462 s390_load_address (target, src);
4466 /* Return true if ADDR is a valid memory address.
4467 STRICT specifies whether strict register checking applies. */
4469 static bool
4470 s390_legitimate_address_p (machine_mode mode, rtx addr, bool strict)
4472 struct s390_address ad;
4474 if (TARGET_Z10
4475 && larl_operand (addr, VOIDmode)
4476 && (mode == VOIDmode
4477 || s390_check_symref_alignment (addr, GET_MODE_SIZE (mode))))
4478 return true;
4480 if (!s390_decompose_address (addr, &ad))
4481 return false;
4483 if (strict)
4485 if (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
4486 return false;
4488 if (ad.indx && !REGNO_OK_FOR_INDEX_P (REGNO (ad.indx)))
4489 return false;
4491 else
4493 if (ad.base
4494 && !(REGNO (ad.base) >= FIRST_PSEUDO_REGISTER
4495 || REGNO_REG_CLASS (REGNO (ad.base)) == ADDR_REGS))
4496 return false;
4498 if (ad.indx
4499 && !(REGNO (ad.indx) >= FIRST_PSEUDO_REGISTER
4500 || REGNO_REG_CLASS (REGNO (ad.indx)) == ADDR_REGS))
4501 return false;
4503 return true;
4506 /* Return true if OP is a valid operand for the LA instruction.
4507 In 31-bit, we need to prove that the result is used as an
4508 address, as LA performs only a 31-bit addition. */
4510 bool
4511 legitimate_la_operand_p (rtx op)
4513 struct s390_address addr;
4514 if (!s390_decompose_address (op, &addr))
4515 return false;
4517 return (TARGET_64BIT || addr.pointer);
4520 /* Return true if it is valid *and* preferable to use LA to
4521 compute the sum of OP1 and OP2. */
4523 bool
4524 preferred_la_operand_p (rtx op1, rtx op2)
4526 struct s390_address addr;
4528 if (op2 != const0_rtx)
4529 op1 = gen_rtx_PLUS (Pmode, op1, op2);
4531 if (!s390_decompose_address (op1, &addr))
4532 return false;
4533 if (addr.base && !REGNO_OK_FOR_BASE_P (REGNO (addr.base)))
4534 return false;
4535 if (addr.indx && !REGNO_OK_FOR_INDEX_P (REGNO (addr.indx)))
4536 return false;
4538 /* Avoid LA instructions with index register on z196; it is
4539 preferable to use regular add instructions when possible.
4540 Starting with zEC12 the la with index register is "uncracked"
4541 again. */
4542 if (addr.indx && s390_tune == PROCESSOR_2817_Z196)
4543 return false;
4545 if (!TARGET_64BIT && !addr.pointer)
4546 return false;
4548 if (addr.pointer)
4549 return true;
4551 if ((addr.base && REG_P (addr.base) && REG_POINTER (addr.base))
4552 || (addr.indx && REG_P (addr.indx) && REG_POINTER (addr.indx)))
4553 return true;
4555 return false;
4558 /* Emit a forced load-address operation to load SRC into DST.
4559 This will use the LOAD ADDRESS instruction even in situations
4560 where legitimate_la_operand_p (SRC) returns false. */
4562 void
4563 s390_load_address (rtx dst, rtx src)
4565 if (TARGET_64BIT)
4566 emit_move_insn (dst, src);
4567 else
4568 emit_insn (gen_force_la_31 (dst, src));
4571 /* Return true if it ok to use SYMBOL_REF in a relative address. */
4573 bool
4574 s390_rel_address_ok_p (rtx symbol_ref)
4576 tree decl;
4578 if (symbol_ref == s390_got_symbol () || CONSTANT_POOL_ADDRESS_P (symbol_ref))
4579 return true;
4581 decl = SYMBOL_REF_DECL (symbol_ref);
4583 if (!flag_pic || SYMBOL_REF_LOCAL_P (symbol_ref))
4584 return (s390_pic_data_is_text_relative
4585 || (decl
4586 && TREE_CODE (decl) == FUNCTION_DECL));
4588 return false;
4591 /* Return a legitimate reference for ORIG (an address) using the
4592 register REG. If REG is 0, a new pseudo is generated.
4594 There are two types of references that must be handled:
4596 1. Global data references must load the address from the GOT, via
4597 the PIC reg. An insn is emitted to do this load, and the reg is
4598 returned.
4600 2. Static data references, constant pool addresses, and code labels
4601 compute the address as an offset from the GOT, whose base is in
4602 the PIC reg. Static data objects have SYMBOL_FLAG_LOCAL set to
4603 differentiate them from global data objects. The returned
4604 address is the PIC reg + an unspec constant.
4606 TARGET_LEGITIMIZE_ADDRESS_P rejects symbolic references unless the PIC
4607 reg also appears in the address. */
4610 legitimize_pic_address (rtx orig, rtx reg)
4612 rtx addr = orig;
4613 rtx addend = const0_rtx;
4614 rtx new_rtx = orig;
4616 gcc_assert (!TLS_SYMBOLIC_CONST (addr));
4618 if (GET_CODE (addr) == CONST)
4619 addr = XEXP (addr, 0);
4621 if (GET_CODE (addr) == PLUS)
4623 addend = XEXP (addr, 1);
4624 addr = XEXP (addr, 0);
4627 if ((GET_CODE (addr) == LABEL_REF
4628 || (SYMBOL_REF_P (addr) && s390_rel_address_ok_p (addr))
4629 || (GET_CODE (addr) == UNSPEC &&
4630 (XINT (addr, 1) == UNSPEC_GOTENT
4631 || (TARGET_CPU_ZARCH && XINT (addr, 1) == UNSPEC_PLT))))
4632 && GET_CODE (addend) == CONST_INT)
4634 /* This can be locally addressed. */
4636 /* larl_operand requires UNSPECs to be wrapped in a const rtx. */
4637 rtx const_addr = (GET_CODE (addr) == UNSPEC ?
4638 gen_rtx_CONST (Pmode, addr) : addr);
4640 if (TARGET_CPU_ZARCH
4641 && larl_operand (const_addr, VOIDmode)
4642 && INTVAL (addend) < HOST_WIDE_INT_1 << 31
4643 && INTVAL (addend) >= -(HOST_WIDE_INT_1 << 31))
4645 if (INTVAL (addend) & 1)
4647 /* LARL can't handle odd offsets, so emit a pair of LARL
4648 and LA. */
4649 rtx temp = reg? reg : gen_reg_rtx (Pmode);
4651 if (!DISP_IN_RANGE (INTVAL (addend)))
4653 HOST_WIDE_INT even = INTVAL (addend) - 1;
4654 addr = gen_rtx_PLUS (Pmode, addr, GEN_INT (even));
4655 addr = gen_rtx_CONST (Pmode, addr);
4656 addend = const1_rtx;
4659 emit_move_insn (temp, addr);
4660 new_rtx = gen_rtx_PLUS (Pmode, temp, addend);
4662 if (reg != 0)
4664 s390_load_address (reg, new_rtx);
4665 new_rtx = reg;
4668 else
4670 /* If the offset is even, we can just use LARL. This
4671 will happen automatically. */
4674 else
4676 /* No larl - Access local symbols relative to the GOT. */
4678 rtx temp = reg? reg : gen_reg_rtx (Pmode);
4680 if (reload_in_progress || reload_completed)
4681 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
4683 addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
4684 if (addend != const0_rtx)
4685 addr = gen_rtx_PLUS (Pmode, addr, addend);
4686 addr = gen_rtx_CONST (Pmode, addr);
4687 addr = force_const_mem (Pmode, addr);
4688 emit_move_insn (temp, addr);
4690 new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
4691 if (reg != 0)
4693 s390_load_address (reg, new_rtx);
4694 new_rtx = reg;
4698 else if (GET_CODE (addr) == SYMBOL_REF && addend == const0_rtx)
4700 /* A non-local symbol reference without addend.
4702 The symbol ref is wrapped into an UNSPEC to make sure the
4703 proper operand modifier (@GOT or @GOTENT) will be emitted.
4704 This will tell the linker to put the symbol into the GOT.
4706 Additionally the code dereferencing the GOT slot is emitted here.
4708 An addend to the symref needs to be added afterwards.
4709 legitimize_pic_address calls itself recursively to handle
4710 that case. So no need to do it here. */
4712 if (reg == 0)
4713 reg = gen_reg_rtx (Pmode);
4715 if (TARGET_Z10)
4717 /* Use load relative if possible.
4718 lgrl <target>, sym@GOTENT */
4719 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTENT);
4720 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
4721 new_rtx = gen_const_mem (GET_MODE (reg), new_rtx);
4723 emit_move_insn (reg, new_rtx);
4724 new_rtx = reg;
4726 else if (flag_pic == 1)
4728 /* Assume GOT offset is a valid displacement operand (< 4k
4729 or < 512k with z990). This is handled the same way in
4730 both 31- and 64-bit code (@GOT).
4731 lg <target>, sym@GOT(r12) */
4733 if (reload_in_progress || reload_completed)
4734 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
4736 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
4737 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
4738 new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
4739 new_rtx = gen_const_mem (Pmode, new_rtx);
4740 emit_move_insn (reg, new_rtx);
4741 new_rtx = reg;
4743 else if (TARGET_CPU_ZARCH)
4745 /* If the GOT offset might be >= 4k, we determine the position
4746 of the GOT entry via a PC-relative LARL (@GOTENT).
4747 larl temp, sym@GOTENT
4748 lg <target>, 0(temp) */
4750 rtx temp = reg ? reg : gen_reg_rtx (Pmode);
4752 gcc_assert (REGNO (temp) >= FIRST_PSEUDO_REGISTER
4753 || REGNO_REG_CLASS (REGNO (temp)) == ADDR_REGS);
4755 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTENT);
4756 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
4757 emit_move_insn (temp, new_rtx);
4759 new_rtx = gen_const_mem (Pmode, temp);
4760 emit_move_insn (reg, new_rtx);
4762 new_rtx = reg;
4764 else
4766 /* If the GOT offset might be >= 4k, we have to load it
4767 from the literal pool (@GOT).
4769 lg temp, lit-litbase(r13)
4770 lg <target>, 0(temp)
4771 lit: .long sym@GOT */
4773 rtx temp = reg ? reg : gen_reg_rtx (Pmode);
4775 gcc_assert (REGNO (temp) >= FIRST_PSEUDO_REGISTER
4776 || REGNO_REG_CLASS (REGNO (temp)) == ADDR_REGS);
4778 if (reload_in_progress || reload_completed)
4779 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
4781 addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
4782 addr = gen_rtx_CONST (Pmode, addr);
4783 addr = force_const_mem (Pmode, addr);
4784 emit_move_insn (temp, addr);
4786 new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
4787 new_rtx = gen_const_mem (Pmode, new_rtx);
4788 emit_move_insn (reg, new_rtx);
4789 new_rtx = reg;
4792 else if (GET_CODE (addr) == UNSPEC && GET_CODE (addend) == CONST_INT)
4794 gcc_assert (XVECLEN (addr, 0) == 1);
4795 switch (XINT (addr, 1))
4797 /* These address symbols (or PLT slots) relative to the GOT
4798 (not GOT slots!). In general this will exceed the
4799 displacement range so these value belong into the literal
4800 pool. */
4801 case UNSPEC_GOTOFF:
4802 case UNSPEC_PLTOFF:
4803 new_rtx = force_const_mem (Pmode, orig);
4804 break;
4806 /* For -fPIC the GOT size might exceed the displacement
4807 range so make sure the value is in the literal pool. */
4808 case UNSPEC_GOT:
4809 if (flag_pic == 2)
4810 new_rtx = force_const_mem (Pmode, orig);
4811 break;
4813 /* For @GOTENT larl is used. This is handled like local
4814 symbol refs. */
4815 case UNSPEC_GOTENT:
4816 gcc_unreachable ();
4817 break;
4819 /* @PLT is OK as is on 64-bit, must be converted to
4820 GOT-relative @PLTOFF on 31-bit. */
4821 case UNSPEC_PLT:
4822 if (!TARGET_CPU_ZARCH)
4824 rtx temp = reg? reg : gen_reg_rtx (Pmode);
4826 if (reload_in_progress || reload_completed)
4827 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
4829 addr = XVECEXP (addr, 0, 0);
4830 addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr),
4831 UNSPEC_PLTOFF);
4832 if (addend != const0_rtx)
4833 addr = gen_rtx_PLUS (Pmode, addr, addend);
4834 addr = gen_rtx_CONST (Pmode, addr);
4835 addr = force_const_mem (Pmode, addr);
4836 emit_move_insn (temp, addr);
4838 new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
4839 if (reg != 0)
4841 s390_load_address (reg, new_rtx);
4842 new_rtx = reg;
4845 else
4846 /* On 64 bit larl can be used. This case is handled like
4847 local symbol refs. */
4848 gcc_unreachable ();
4849 break;
4851 /* Everything else cannot happen. */
4852 default:
4853 gcc_unreachable ();
4856 else if (addend != const0_rtx)
4858 /* Otherwise, compute the sum. */
4860 rtx base = legitimize_pic_address (addr, reg);
4861 new_rtx = legitimize_pic_address (addend,
4862 base == reg ? NULL_RTX : reg);
4863 if (GET_CODE (new_rtx) == CONST_INT)
4864 new_rtx = plus_constant (Pmode, base, INTVAL (new_rtx));
4865 else
4867 if (GET_CODE (new_rtx) == PLUS && CONSTANT_P (XEXP (new_rtx, 1)))
4869 base = gen_rtx_PLUS (Pmode, base, XEXP (new_rtx, 0));
4870 new_rtx = XEXP (new_rtx, 1);
4872 new_rtx = gen_rtx_PLUS (Pmode, base, new_rtx);
4875 if (GET_CODE (new_rtx) == CONST)
4876 new_rtx = XEXP (new_rtx, 0);
4877 new_rtx = force_operand (new_rtx, 0);
4880 return new_rtx;
4883 /* Load the thread pointer into a register. */
4886 s390_get_thread_pointer (void)
4888 rtx tp = gen_reg_rtx (Pmode);
4890 emit_move_insn (tp, gen_rtx_REG (Pmode, TP_REGNUM));
4891 mark_reg_pointer (tp, BITS_PER_WORD);
4893 return tp;
4896 /* Emit a tls call insn. The call target is the SYMBOL_REF stored
4897 in s390_tls_symbol which always refers to __tls_get_offset.
4898 The returned offset is written to RESULT_REG and an USE rtx is
4899 generated for TLS_CALL. */
4901 static GTY(()) rtx s390_tls_symbol;
4903 static void
4904 s390_emit_tls_call_insn (rtx result_reg, rtx tls_call)
4906 rtx insn;
4908 if (!flag_pic)
4909 emit_insn (s390_load_got ());
4911 if (!s390_tls_symbol)
4912 s390_tls_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tls_get_offset");
4914 insn = s390_emit_call (s390_tls_symbol, tls_call, result_reg,
4915 gen_rtx_REG (Pmode, RETURN_REGNUM));
4917 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), result_reg);
4918 RTL_CONST_CALL_P (insn) = 1;
4921 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
4922 this (thread-local) address. REG may be used as temporary. */
4924 static rtx
4925 legitimize_tls_address (rtx addr, rtx reg)
4927 rtx new_rtx, tls_call, temp, base, r2;
4928 rtx_insn *insn;
4930 if (GET_CODE (addr) == SYMBOL_REF)
4931 switch (tls_symbolic_operand (addr))
4933 case TLS_MODEL_GLOBAL_DYNAMIC:
4934 start_sequence ();
4935 r2 = gen_rtx_REG (Pmode, 2);
4936 tls_call = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_TLSGD);
4937 new_rtx = gen_rtx_CONST (Pmode, tls_call);
4938 new_rtx = force_const_mem (Pmode, new_rtx);
4939 emit_move_insn (r2, new_rtx);
4940 s390_emit_tls_call_insn (r2, tls_call);
4941 insn = get_insns ();
4942 end_sequence ();
4944 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_NTPOFF);
4945 temp = gen_reg_rtx (Pmode);
4946 emit_libcall_block (insn, temp, r2, new_rtx);
4948 new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
4949 if (reg != 0)
4951 s390_load_address (reg, new_rtx);
4952 new_rtx = reg;
4954 break;
4956 case TLS_MODEL_LOCAL_DYNAMIC:
4957 start_sequence ();
4958 r2 = gen_rtx_REG (Pmode, 2);
4959 tls_call = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TLSLDM);
4960 new_rtx = gen_rtx_CONST (Pmode, tls_call);
4961 new_rtx = force_const_mem (Pmode, new_rtx);
4962 emit_move_insn (r2, new_rtx);
4963 s390_emit_tls_call_insn (r2, tls_call);
4964 insn = get_insns ();
4965 end_sequence ();
4967 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TLSLDM_NTPOFF);
4968 temp = gen_reg_rtx (Pmode);
4969 emit_libcall_block (insn, temp, r2, new_rtx);
4971 new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
4972 base = gen_reg_rtx (Pmode);
4973 s390_load_address (base, new_rtx);
4975 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_DTPOFF);
4976 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
4977 new_rtx = force_const_mem (Pmode, new_rtx);
4978 temp = gen_reg_rtx (Pmode);
4979 emit_move_insn (temp, new_rtx);
4981 new_rtx = gen_rtx_PLUS (Pmode, base, temp);
4982 if (reg != 0)
4984 s390_load_address (reg, new_rtx);
4985 new_rtx = reg;
4987 break;
4989 case TLS_MODEL_INITIAL_EXEC:
4990 if (flag_pic == 1)
4992 /* Assume GOT offset < 4k. This is handled the same way
4993 in both 31- and 64-bit code. */
4995 if (reload_in_progress || reload_completed)
4996 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
4998 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTNTPOFF);
4999 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5000 new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
5001 new_rtx = gen_const_mem (Pmode, new_rtx);
5002 temp = gen_reg_rtx (Pmode);
5003 emit_move_insn (temp, new_rtx);
5005 else if (TARGET_CPU_ZARCH)
5007 /* If the GOT offset might be >= 4k, we determine the position
5008 of the GOT entry via a PC-relative LARL. */
5010 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_INDNTPOFF);
5011 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5012 temp = gen_reg_rtx (Pmode);
5013 emit_move_insn (temp, new_rtx);
5015 new_rtx = gen_const_mem (Pmode, temp);
5016 temp = gen_reg_rtx (Pmode);
5017 emit_move_insn (temp, new_rtx);
5019 else if (flag_pic)
5021 /* If the GOT offset might be >= 4k, we have to load it
5022 from the literal pool. */
5024 if (reload_in_progress || reload_completed)
5025 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
5027 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTNTPOFF);
5028 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5029 new_rtx = force_const_mem (Pmode, new_rtx);
5030 temp = gen_reg_rtx (Pmode);
5031 emit_move_insn (temp, new_rtx);
5033 new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
5034 new_rtx = gen_const_mem (Pmode, new_rtx);
5036 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, new_rtx, addr), UNSPEC_TLS_LOAD);
5037 temp = gen_reg_rtx (Pmode);
5038 emit_insn (gen_rtx_SET (temp, new_rtx));
5040 else
5042 /* In position-dependent code, load the absolute address of
5043 the GOT entry from the literal pool. */
5045 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_INDNTPOFF);
5046 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5047 new_rtx = force_const_mem (Pmode, new_rtx);
5048 temp = gen_reg_rtx (Pmode);
5049 emit_move_insn (temp, new_rtx);
5051 new_rtx = temp;
5052 new_rtx = gen_const_mem (Pmode, new_rtx);
5053 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, new_rtx, addr), UNSPEC_TLS_LOAD);
5054 temp = gen_reg_rtx (Pmode);
5055 emit_insn (gen_rtx_SET (temp, new_rtx));
5058 new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
5059 if (reg != 0)
5061 s390_load_address (reg, new_rtx);
5062 new_rtx = reg;
5064 break;
5066 case TLS_MODEL_LOCAL_EXEC:
5067 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_NTPOFF);
5068 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5069 new_rtx = force_const_mem (Pmode, new_rtx);
5070 temp = gen_reg_rtx (Pmode);
5071 emit_move_insn (temp, new_rtx);
5073 new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
5074 if (reg != 0)
5076 s390_load_address (reg, new_rtx);
5077 new_rtx = reg;
5079 break;
5081 default:
5082 gcc_unreachable ();
5085 else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == UNSPEC)
5087 switch (XINT (XEXP (addr, 0), 1))
5089 case UNSPEC_INDNTPOFF:
5090 gcc_assert (TARGET_CPU_ZARCH);
5091 new_rtx = addr;
5092 break;
5094 default:
5095 gcc_unreachable ();
5099 else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS
5100 && GET_CODE (XEXP (XEXP (addr, 0), 1)) == CONST_INT)
5102 new_rtx = XEXP (XEXP (addr, 0), 0);
5103 if (GET_CODE (new_rtx) != SYMBOL_REF)
5104 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5106 new_rtx = legitimize_tls_address (new_rtx, reg);
5107 new_rtx = plus_constant (Pmode, new_rtx,
5108 INTVAL (XEXP (XEXP (addr, 0), 1)));
5109 new_rtx = force_operand (new_rtx, 0);
5112 else
5113 gcc_unreachable (); /* for now ... */
5115 return new_rtx;
5118 /* Emit insns making the address in operands[1] valid for a standard
5119 move to operands[0]. operands[1] is replaced by an address which
5120 should be used instead of the former RTX to emit the move
5121 pattern. */
5123 void
5124 emit_symbolic_move (rtx *operands)
5126 rtx temp = !can_create_pseudo_p () ? operands[0] : gen_reg_rtx (Pmode);
5128 if (GET_CODE (operands[0]) == MEM)
5129 operands[1] = force_reg (Pmode, operands[1]);
5130 else if (TLS_SYMBOLIC_CONST (operands[1]))
5131 operands[1] = legitimize_tls_address (operands[1], temp);
5132 else if (flag_pic)
5133 operands[1] = legitimize_pic_address (operands[1], temp);
5136 /* Try machine-dependent ways of modifying an illegitimate address X
5137 to be legitimate. If we find one, return the new, valid address.
5139 OLDX is the address as it was before break_out_memory_refs was called.
5140 In some cases it is useful to look at this to decide what needs to be done.
5142 MODE is the mode of the operand pointed to by X.
5144 When -fpic is used, special handling is needed for symbolic references.
5145 See comments by legitimize_pic_address for details. */
5147 static rtx
5148 s390_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
5149 machine_mode mode ATTRIBUTE_UNUSED)
5151 rtx constant_term = const0_rtx;
5153 if (TLS_SYMBOLIC_CONST (x))
5155 x = legitimize_tls_address (x, 0);
5157 if (s390_legitimate_address_p (mode, x, FALSE))
5158 return x;
5160 else if (GET_CODE (x) == PLUS
5161 && (TLS_SYMBOLIC_CONST (XEXP (x, 0))
5162 || TLS_SYMBOLIC_CONST (XEXP (x, 1))))
5164 return x;
5166 else if (flag_pic)
5168 if (SYMBOLIC_CONST (x)
5169 || (GET_CODE (x) == PLUS
5170 && (SYMBOLIC_CONST (XEXP (x, 0))
5171 || SYMBOLIC_CONST (XEXP (x, 1)))))
5172 x = legitimize_pic_address (x, 0);
5174 if (s390_legitimate_address_p (mode, x, FALSE))
5175 return x;
5178 x = eliminate_constant_term (x, &constant_term);
5180 /* Optimize loading of large displacements by splitting them
5181 into the multiple of 4K and the rest; this allows the
5182 former to be CSE'd if possible.
5184 Don't do this if the displacement is added to a register
5185 pointing into the stack frame, as the offsets will
5186 change later anyway. */
5188 if (GET_CODE (constant_term) == CONST_INT
5189 && !TARGET_LONG_DISPLACEMENT
5190 && !DISP_IN_RANGE (INTVAL (constant_term))
5191 && !(REG_P (x) && REGNO_PTR_FRAME_P (REGNO (x))))
5193 HOST_WIDE_INT lower = INTVAL (constant_term) & 0xfff;
5194 HOST_WIDE_INT upper = INTVAL (constant_term) ^ lower;
5196 rtx temp = gen_reg_rtx (Pmode);
5197 rtx val = force_operand (GEN_INT (upper), temp);
5198 if (val != temp)
5199 emit_move_insn (temp, val);
5201 x = gen_rtx_PLUS (Pmode, x, temp);
5202 constant_term = GEN_INT (lower);
5205 if (GET_CODE (x) == PLUS)
5207 if (GET_CODE (XEXP (x, 0)) == REG)
5209 rtx temp = gen_reg_rtx (Pmode);
5210 rtx val = force_operand (XEXP (x, 1), temp);
5211 if (val != temp)
5212 emit_move_insn (temp, val);
5214 x = gen_rtx_PLUS (Pmode, XEXP (x, 0), temp);
5217 else if (GET_CODE (XEXP (x, 1)) == REG)
5219 rtx temp = gen_reg_rtx (Pmode);
5220 rtx val = force_operand (XEXP (x, 0), temp);
5221 if (val != temp)
5222 emit_move_insn (temp, val);
5224 x = gen_rtx_PLUS (Pmode, temp, XEXP (x, 1));
5228 if (constant_term != const0_rtx)
5229 x = gen_rtx_PLUS (Pmode, x, constant_term);
5231 return x;
5234 /* Try a machine-dependent way of reloading an illegitimate address AD
5235 operand. If we find one, push the reload and return the new address.
5237 MODE is the mode of the enclosing MEM. OPNUM is the operand number
5238 and TYPE is the reload type of the current reload. */
5241 legitimize_reload_address (rtx ad, machine_mode mode ATTRIBUTE_UNUSED,
5242 int opnum, int type)
5244 if (!optimize || TARGET_LONG_DISPLACEMENT)
5245 return NULL_RTX;
5247 if (GET_CODE (ad) == PLUS)
5249 rtx tem = simplify_binary_operation (PLUS, Pmode,
5250 XEXP (ad, 0), XEXP (ad, 1));
5251 if (tem)
5252 ad = tem;
5255 if (GET_CODE (ad) == PLUS
5256 && GET_CODE (XEXP (ad, 0)) == REG
5257 && GET_CODE (XEXP (ad, 1)) == CONST_INT
5258 && !DISP_IN_RANGE (INTVAL (XEXP (ad, 1))))
5260 HOST_WIDE_INT lower = INTVAL (XEXP (ad, 1)) & 0xfff;
5261 HOST_WIDE_INT upper = INTVAL (XEXP (ad, 1)) ^ lower;
5262 rtx cst, tem, new_rtx;
5264 cst = GEN_INT (upper);
5265 if (!legitimate_reload_constant_p (cst))
5266 cst = force_const_mem (Pmode, cst);
5268 tem = gen_rtx_PLUS (Pmode, XEXP (ad, 0), cst);
5269 new_rtx = gen_rtx_PLUS (Pmode, tem, GEN_INT (lower));
5271 push_reload (XEXP (tem, 1), 0, &XEXP (tem, 1), 0,
5272 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
5273 opnum, (enum reload_type) type);
5274 return new_rtx;
5277 return NULL_RTX;
5280 /* Emit code to move LEN bytes from DST to SRC. */
5282 bool
5283 s390_expand_movmem (rtx dst, rtx src, rtx len)
5285 /* When tuning for z10 or higher we rely on the Glibc functions to
5286 do the right thing. Only for constant lengths below 64k we will
5287 generate inline code. */
5288 if (s390_tune >= PROCESSOR_2097_Z10
5289 && (GET_CODE (len) != CONST_INT || INTVAL (len) > (1<<16)))
5290 return false;
5292 /* Expand memcpy for constant length operands without a loop if it
5293 is shorter that way.
5295 With a constant length argument a
5296 memcpy loop (without pfd) is 36 bytes -> 6 * mvc */
5297 if (GET_CODE (len) == CONST_INT
5298 && INTVAL (len) >= 0
5299 && INTVAL (len) <= 256 * 6
5300 && (!TARGET_MVCLE || INTVAL (len) <= 256))
5302 HOST_WIDE_INT o, l;
5304 for (l = INTVAL (len), o = 0; l > 0; l -= 256, o += 256)
5306 rtx newdst = adjust_address (dst, BLKmode, o);
5307 rtx newsrc = adjust_address (src, BLKmode, o);
5308 emit_insn (gen_movmem_short (newdst, newsrc,
5309 GEN_INT (l > 256 ? 255 : l - 1)));
5313 else if (TARGET_MVCLE)
5315 emit_insn (gen_movmem_long (dst, src, convert_to_mode (Pmode, len, 1)));
5318 else
5320 rtx dst_addr, src_addr, count, blocks, temp;
5321 rtx_code_label *loop_start_label = gen_label_rtx ();
5322 rtx_code_label *loop_end_label = gen_label_rtx ();
5323 rtx_code_label *end_label = gen_label_rtx ();
5324 machine_mode mode;
5326 mode = GET_MODE (len);
5327 if (mode == VOIDmode)
5328 mode = Pmode;
5330 dst_addr = gen_reg_rtx (Pmode);
5331 src_addr = gen_reg_rtx (Pmode);
5332 count = gen_reg_rtx (mode);
5333 blocks = gen_reg_rtx (mode);
5335 convert_move (count, len, 1);
5336 emit_cmp_and_jump_insns (count, const0_rtx,
5337 EQ, NULL_RTX, mode, 1, end_label);
5339 emit_move_insn (dst_addr, force_operand (XEXP (dst, 0), NULL_RTX));
5340 emit_move_insn (src_addr, force_operand (XEXP (src, 0), NULL_RTX));
5341 dst = change_address (dst, VOIDmode, dst_addr);
5342 src = change_address (src, VOIDmode, src_addr);
5344 temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1,
5345 OPTAB_DIRECT);
5346 if (temp != count)
5347 emit_move_insn (count, temp);
5349 temp = expand_binop (mode, lshr_optab, count, GEN_INT (8), blocks, 1,
5350 OPTAB_DIRECT);
5351 if (temp != blocks)
5352 emit_move_insn (blocks, temp);
5354 emit_cmp_and_jump_insns (blocks, const0_rtx,
5355 EQ, NULL_RTX, mode, 1, loop_end_label);
5357 emit_label (loop_start_label);
5359 if (TARGET_Z10
5360 && (GET_CODE (len) != CONST_INT || INTVAL (len) > 768))
5362 rtx prefetch;
5364 /* Issue a read prefetch for the +3 cache line. */
5365 prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, src_addr, GEN_INT (768)),
5366 const0_rtx, const0_rtx);
5367 PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
5368 emit_insn (prefetch);
5370 /* Issue a write prefetch for the +3 cache line. */
5371 prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (768)),
5372 const1_rtx, const0_rtx);
5373 PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
5374 emit_insn (prefetch);
5377 emit_insn (gen_movmem_short (dst, src, GEN_INT (255)));
5378 s390_load_address (dst_addr,
5379 gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (256)));
5380 s390_load_address (src_addr,
5381 gen_rtx_PLUS (Pmode, src_addr, GEN_INT (256)));
5383 temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1,
5384 OPTAB_DIRECT);
5385 if (temp != blocks)
5386 emit_move_insn (blocks, temp);
5388 emit_cmp_and_jump_insns (blocks, const0_rtx,
5389 EQ, NULL_RTX, mode, 1, loop_end_label);
5391 emit_jump (loop_start_label);
5392 emit_label (loop_end_label);
5394 emit_insn (gen_movmem_short (dst, src,
5395 convert_to_mode (Pmode, count, 1)));
5396 emit_label (end_label);
5398 return true;
5401 /* Emit code to set LEN bytes at DST to VAL.
5402 Make use of clrmem if VAL is zero. */
5404 void
5405 s390_expand_setmem (rtx dst, rtx len, rtx val)
5407 if (GET_CODE (len) == CONST_INT && INTVAL (len) <= 0)
5408 return;
5410 gcc_assert (GET_CODE (val) == CONST_INT || GET_MODE (val) == QImode);
5412 /* Expand setmem/clrmem for a constant length operand without a
5413 loop if it will be shorter that way.
5414 With a constant length and without pfd argument a
5415 clrmem loop is 32 bytes -> 5.3 * xc
5416 setmem loop is 36 bytes -> 3.6 * (mvi/stc + mvc) */
5417 if (GET_CODE (len) == CONST_INT
5418 && ((INTVAL (len) <= 256 * 5 && val == const0_rtx)
5419 || INTVAL (len) <= 257 * 3)
5420 && (!TARGET_MVCLE || INTVAL (len) <= 256))
5422 HOST_WIDE_INT o, l;
5424 if (val == const0_rtx)
5425 /* clrmem: emit 256 byte blockwise XCs. */
5426 for (l = INTVAL (len), o = 0; l > 0; l -= 256, o += 256)
5428 rtx newdst = adjust_address (dst, BLKmode, o);
5429 emit_insn (gen_clrmem_short (newdst,
5430 GEN_INT (l > 256 ? 255 : l - 1)));
5432 else
5433 /* setmem: emit 1(mvi) + 256(mvc) byte blockwise memsets by
5434 setting first byte to val and using a 256 byte mvc with one
5435 byte overlap to propagate the byte. */
5436 for (l = INTVAL (len), o = 0; l > 0; l -= 257, o += 257)
5438 rtx newdst = adjust_address (dst, BLKmode, o);
5439 emit_move_insn (adjust_address (dst, QImode, o), val);
5440 if (l > 1)
5442 rtx newdstp1 = adjust_address (dst, BLKmode, o + 1);
5443 emit_insn (gen_movmem_short (newdstp1, newdst,
5444 GEN_INT (l > 257 ? 255 : l - 2)));
5449 else if (TARGET_MVCLE)
5451 val = force_not_mem (convert_modes (Pmode, QImode, val, 1));
5452 if (TARGET_64BIT)
5453 emit_insn (gen_setmem_long_di (dst, convert_to_mode (Pmode, len, 1),
5454 val));
5455 else
5456 emit_insn (gen_setmem_long_si (dst, convert_to_mode (Pmode, len, 1),
5457 val));
5460 else
5462 rtx dst_addr, count, blocks, temp, dstp1 = NULL_RTX;
5463 rtx_code_label *loop_start_label = gen_label_rtx ();
5464 rtx_code_label *onebyte_end_label = gen_label_rtx ();
5465 rtx_code_label *zerobyte_end_label = gen_label_rtx ();
5466 rtx_code_label *restbyte_end_label = gen_label_rtx ();
5467 machine_mode mode;
5469 mode = GET_MODE (len);
5470 if (mode == VOIDmode)
5471 mode = Pmode;
5473 dst_addr = gen_reg_rtx (Pmode);
5474 count = gen_reg_rtx (mode);
5475 blocks = gen_reg_rtx (mode);
5477 convert_move (count, len, 1);
5478 emit_cmp_and_jump_insns (count, const0_rtx,
5479 EQ, NULL_RTX, mode, 1, zerobyte_end_label,
5480 profile_probability::very_unlikely ());
5482 /* We need to make a copy of the target address since memset is
5483 supposed to return it unmodified. We have to make it here
5484 already since the new reg is used at onebyte_end_label. */
5485 emit_move_insn (dst_addr, force_operand (XEXP (dst, 0), NULL_RTX));
5486 dst = change_address (dst, VOIDmode, dst_addr);
5488 if (val != const0_rtx)
5490 /* When using the overlapping mvc the original target
5491 address is only accessed as single byte entity (even by
5492 the mvc reading this value). */
5493 set_mem_size (dst, 1);
5494 dstp1 = adjust_address (dst, VOIDmode, 1);
5495 emit_cmp_and_jump_insns (count,
5496 const1_rtx, EQ, NULL_RTX, mode, 1,
5497 onebyte_end_label,
5498 profile_probability::very_unlikely ());
5501 /* There is one unconditional (mvi+mvc)/xc after the loop
5502 dealing with the rest of the bytes, subtracting two (mvi+mvc)
5503 or one (xc) here leaves this number of bytes to be handled by
5504 it. */
5505 temp = expand_binop (mode, add_optab, count,
5506 val == const0_rtx ? constm1_rtx : GEN_INT (-2),
5507 count, 1, OPTAB_DIRECT);
5508 if (temp != count)
5509 emit_move_insn (count, temp);
5511 temp = expand_binop (mode, lshr_optab, count, GEN_INT (8), blocks, 1,
5512 OPTAB_DIRECT);
5513 if (temp != blocks)
5514 emit_move_insn (blocks, temp);
5516 emit_cmp_and_jump_insns (blocks, const0_rtx,
5517 EQ, NULL_RTX, mode, 1, restbyte_end_label);
5519 emit_jump (loop_start_label);
5521 if (val != const0_rtx)
5523 /* The 1 byte != 0 special case. Not handled efficiently
5524 since we require two jumps for that. However, this
5525 should be very rare. */
5526 emit_label (onebyte_end_label);
5527 emit_move_insn (adjust_address (dst, QImode, 0), val);
5528 emit_jump (zerobyte_end_label);
5531 emit_label (loop_start_label);
5533 if (TARGET_Z10
5534 && (GET_CODE (len) != CONST_INT || INTVAL (len) > 1024))
5536 /* Issue a write prefetch for the +4 cache line. */
5537 rtx prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, dst_addr,
5538 GEN_INT (1024)),
5539 const1_rtx, const0_rtx);
5540 emit_insn (prefetch);
5541 PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
5544 if (val == const0_rtx)
5545 emit_insn (gen_clrmem_short (dst, GEN_INT (255)));
5546 else
5548 /* Set the first byte in the block to the value and use an
5549 overlapping mvc for the block. */
5550 emit_move_insn (adjust_address (dst, QImode, 0), val);
5551 emit_insn (gen_movmem_short (dstp1, dst, GEN_INT (254)));
5553 s390_load_address (dst_addr,
5554 gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (256)));
5556 temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1,
5557 OPTAB_DIRECT);
5558 if (temp != blocks)
5559 emit_move_insn (blocks, temp);
5561 emit_cmp_and_jump_insns (blocks, const0_rtx,
5562 NE, NULL_RTX, mode, 1, loop_start_label);
5564 emit_label (restbyte_end_label);
5566 if (val == const0_rtx)
5567 emit_insn (gen_clrmem_short (dst, convert_to_mode (Pmode, count, 1)));
5568 else
5570 /* Set the first byte in the block to the value and use an
5571 overlapping mvc for the block. */
5572 emit_move_insn (adjust_address (dst, QImode, 0), val);
5573 /* execute only uses the lowest 8 bits of count that's
5574 exactly what we need here. */
5575 emit_insn (gen_movmem_short (dstp1, dst,
5576 convert_to_mode (Pmode, count, 1)));
5579 emit_label (zerobyte_end_label);
5583 /* Emit code to compare LEN bytes at OP0 with those at OP1,
5584 and return the result in TARGET. */
5586 bool
5587 s390_expand_cmpmem (rtx target, rtx op0, rtx op1, rtx len)
5589 rtx ccreg = gen_rtx_REG (CCUmode, CC_REGNUM);
5590 rtx tmp;
5592 /* When tuning for z10 or higher we rely on the Glibc functions to
5593 do the right thing. Only for constant lengths below 64k we will
5594 generate inline code. */
5595 if (s390_tune >= PROCESSOR_2097_Z10
5596 && (GET_CODE (len) != CONST_INT || INTVAL (len) > (1<<16)))
5597 return false;
5599 /* As the result of CMPINT is inverted compared to what we need,
5600 we have to swap the operands. */
5601 tmp = op0; op0 = op1; op1 = tmp;
5603 if (GET_CODE (len) == CONST_INT && INTVAL (len) >= 0 && INTVAL (len) <= 256)
5605 if (INTVAL (len) > 0)
5607 emit_insn (gen_cmpmem_short (op0, op1, GEN_INT (INTVAL (len) - 1)));
5608 emit_insn (gen_cmpint (target, ccreg));
5610 else
5611 emit_move_insn (target, const0_rtx);
5613 else if (TARGET_MVCLE)
5615 emit_insn (gen_cmpmem_long (op0, op1, convert_to_mode (Pmode, len, 1)));
5616 emit_insn (gen_cmpint (target, ccreg));
5618 else
5620 rtx addr0, addr1, count, blocks, temp;
5621 rtx_code_label *loop_start_label = gen_label_rtx ();
5622 rtx_code_label *loop_end_label = gen_label_rtx ();
5623 rtx_code_label *end_label = gen_label_rtx ();
5624 machine_mode mode;
5626 mode = GET_MODE (len);
5627 if (mode == VOIDmode)
5628 mode = Pmode;
5630 addr0 = gen_reg_rtx (Pmode);
5631 addr1 = gen_reg_rtx (Pmode);
5632 count = gen_reg_rtx (mode);
5633 blocks = gen_reg_rtx (mode);
5635 convert_move (count, len, 1);
5636 emit_cmp_and_jump_insns (count, const0_rtx,
5637 EQ, NULL_RTX, mode, 1, end_label);
5639 emit_move_insn (addr0, force_operand (XEXP (op0, 0), NULL_RTX));
5640 emit_move_insn (addr1, force_operand (XEXP (op1, 0), NULL_RTX));
5641 op0 = change_address (op0, VOIDmode, addr0);
5642 op1 = change_address (op1, VOIDmode, addr1);
5644 temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1,
5645 OPTAB_DIRECT);
5646 if (temp != count)
5647 emit_move_insn (count, temp);
5649 temp = expand_binop (mode, lshr_optab, count, GEN_INT (8), blocks, 1,
5650 OPTAB_DIRECT);
5651 if (temp != blocks)
5652 emit_move_insn (blocks, temp);
5654 emit_cmp_and_jump_insns (blocks, const0_rtx,
5655 EQ, NULL_RTX, mode, 1, loop_end_label);
5657 emit_label (loop_start_label);
5659 if (TARGET_Z10
5660 && (GET_CODE (len) != CONST_INT || INTVAL (len) > 512))
5662 rtx prefetch;
5664 /* Issue a read prefetch for the +2 cache line of operand 1. */
5665 prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, addr0, GEN_INT (512)),
5666 const0_rtx, const0_rtx);
5667 emit_insn (prefetch);
5668 PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
5670 /* Issue a read prefetch for the +2 cache line of operand 2. */
5671 prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, addr1, GEN_INT (512)),
5672 const0_rtx, const0_rtx);
5673 emit_insn (prefetch);
5674 PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
5677 emit_insn (gen_cmpmem_short (op0, op1, GEN_INT (255)));
5678 temp = gen_rtx_NE (VOIDmode, ccreg, const0_rtx);
5679 temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
5680 gen_rtx_LABEL_REF (VOIDmode, end_label), pc_rtx);
5681 temp = gen_rtx_SET (pc_rtx, temp);
5682 emit_jump_insn (temp);
5684 s390_load_address (addr0,
5685 gen_rtx_PLUS (Pmode, addr0, GEN_INT (256)));
5686 s390_load_address (addr1,
5687 gen_rtx_PLUS (Pmode, addr1, GEN_INT (256)));
5689 temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1,
5690 OPTAB_DIRECT);
5691 if (temp != blocks)
5692 emit_move_insn (blocks, temp);
5694 emit_cmp_and_jump_insns (blocks, const0_rtx,
5695 EQ, NULL_RTX, mode, 1, loop_end_label);
5697 emit_jump (loop_start_label);
5698 emit_label (loop_end_label);
5700 emit_insn (gen_cmpmem_short (op0, op1,
5701 convert_to_mode (Pmode, count, 1)));
5702 emit_label (end_label);
5704 emit_insn (gen_cmpint (target, ccreg));
5706 return true;
5709 /* Emit a conditional jump to LABEL for condition code mask MASK using
5710 comparsion operator COMPARISON. Return the emitted jump insn. */
5712 static rtx_insn *
5713 s390_emit_ccraw_jump (HOST_WIDE_INT mask, enum rtx_code comparison, rtx label)
5715 rtx temp;
5717 gcc_assert (comparison == EQ || comparison == NE);
5718 gcc_assert (mask > 0 && mask < 15);
5720 temp = gen_rtx_fmt_ee (comparison, VOIDmode,
5721 gen_rtx_REG (CCRAWmode, CC_REGNUM), GEN_INT (mask));
5722 temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
5723 gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
5724 temp = gen_rtx_SET (pc_rtx, temp);
5725 return emit_jump_insn (temp);
5728 /* Emit the instructions to implement strlen of STRING and store the
5729 result in TARGET. The string has the known ALIGNMENT. This
5730 version uses vector instructions and is therefore not appropriate
5731 for targets prior to z13. */
5733 void
5734 s390_expand_vec_strlen (rtx target, rtx string, rtx alignment)
5736 rtx highest_index_to_load_reg = gen_reg_rtx (Pmode);
5737 rtx str_reg = gen_reg_rtx (V16QImode);
5738 rtx str_addr_base_reg = gen_reg_rtx (Pmode);
5739 rtx str_idx_reg = gen_reg_rtx (Pmode);
5740 rtx result_reg = gen_reg_rtx (V16QImode);
5741 rtx is_aligned_label = gen_label_rtx ();
5742 rtx into_loop_label = NULL_RTX;
5743 rtx loop_start_label = gen_label_rtx ();
5744 rtx temp;
5745 rtx len = gen_reg_rtx (QImode);
5746 rtx cond;
5748 s390_load_address (str_addr_base_reg, XEXP (string, 0));
5749 emit_move_insn (str_idx_reg, const0_rtx);
5751 if (INTVAL (alignment) < 16)
5753 /* Check whether the address happens to be aligned properly so
5754 jump directly to the aligned loop. */
5755 emit_cmp_and_jump_insns (gen_rtx_AND (Pmode,
5756 str_addr_base_reg, GEN_INT (15)),
5757 const0_rtx, EQ, NULL_RTX,
5758 Pmode, 1, is_aligned_label);
5760 temp = gen_reg_rtx (Pmode);
5761 temp = expand_binop (Pmode, and_optab, str_addr_base_reg,
5762 GEN_INT (15), temp, 1, OPTAB_DIRECT);
5763 gcc_assert (REG_P (temp));
5764 highest_index_to_load_reg =
5765 expand_binop (Pmode, sub_optab, GEN_INT (15), temp,
5766 highest_index_to_load_reg, 1, OPTAB_DIRECT);
5767 gcc_assert (REG_P (highest_index_to_load_reg));
5768 emit_insn (gen_vllv16qi (str_reg,
5769 convert_to_mode (SImode, highest_index_to_load_reg, 1),
5770 gen_rtx_MEM (BLKmode, str_addr_base_reg)));
5772 into_loop_label = gen_label_rtx ();
5773 s390_emit_jump (into_loop_label, NULL_RTX);
5774 emit_barrier ();
5777 emit_label (is_aligned_label);
5778 LABEL_NUSES (is_aligned_label) = INTVAL (alignment) < 16 ? 2 : 1;
5780 /* Reaching this point we are only performing 16 bytes aligned
5781 loads. */
5782 emit_move_insn (highest_index_to_load_reg, GEN_INT (15));
5784 emit_label (loop_start_label);
5785 LABEL_NUSES (loop_start_label) = 1;
5787 /* Load 16 bytes of the string into VR. */
5788 emit_move_insn (str_reg,
5789 gen_rtx_MEM (V16QImode,
5790 gen_rtx_PLUS (Pmode, str_idx_reg,
5791 str_addr_base_reg)));
5792 if (into_loop_label != NULL_RTX)
5794 emit_label (into_loop_label);
5795 LABEL_NUSES (into_loop_label) = 1;
5798 /* Increment string index by 16 bytes. */
5799 expand_binop (Pmode, add_optab, str_idx_reg, GEN_INT (16),
5800 str_idx_reg, 1, OPTAB_DIRECT);
5802 emit_insn (gen_vec_vfenesv16qi (result_reg, str_reg, str_reg,
5803 GEN_INT (VSTRING_FLAG_ZS | VSTRING_FLAG_CS)));
5805 add_int_reg_note (s390_emit_ccraw_jump (8, NE, loop_start_label),
5806 REG_BR_PROB,
5807 profile_probability::very_likely ().to_reg_br_prob_note ());
5808 emit_insn (gen_vec_extractv16qiqi (len, result_reg, GEN_INT (7)));
5810 /* If the string pointer wasn't aligned we have loaded less then 16
5811 bytes and the remaining bytes got filled with zeros (by vll).
5812 Now we have to check whether the resulting index lies within the
5813 bytes actually part of the string. */
5815 cond = s390_emit_compare (GT, convert_to_mode (Pmode, len, 1),
5816 highest_index_to_load_reg);
5817 s390_load_address (highest_index_to_load_reg,
5818 gen_rtx_PLUS (Pmode, highest_index_to_load_reg,
5819 const1_rtx));
5820 if (TARGET_64BIT)
5821 emit_insn (gen_movdicc (str_idx_reg, cond,
5822 highest_index_to_load_reg, str_idx_reg));
5823 else
5824 emit_insn (gen_movsicc (str_idx_reg, cond,
5825 highest_index_to_load_reg, str_idx_reg));
5827 add_reg_br_prob_note (s390_emit_jump (is_aligned_label, cond),
5828 profile_probability::very_unlikely ());
5830 expand_binop (Pmode, add_optab, str_idx_reg,
5831 GEN_INT (-16), str_idx_reg, 1, OPTAB_DIRECT);
5832 /* FIXME: len is already zero extended - so avoid the llgcr emitted
5833 here. */
5834 temp = expand_binop (Pmode, add_optab, str_idx_reg,
5835 convert_to_mode (Pmode, len, 1),
5836 target, 1, OPTAB_DIRECT);
5837 if (temp != target)
5838 emit_move_insn (target, temp);
5841 void
5842 s390_expand_vec_movstr (rtx result, rtx dst, rtx src)
5844 rtx temp = gen_reg_rtx (Pmode);
5845 rtx src_addr = XEXP (src, 0);
5846 rtx dst_addr = XEXP (dst, 0);
5847 rtx src_addr_reg = gen_reg_rtx (Pmode);
5848 rtx dst_addr_reg = gen_reg_rtx (Pmode);
5849 rtx offset = gen_reg_rtx (Pmode);
5850 rtx vsrc = gen_reg_rtx (V16QImode);
5851 rtx vpos = gen_reg_rtx (V16QImode);
5852 rtx loadlen = gen_reg_rtx (SImode);
5853 rtx gpos_qi = gen_reg_rtx(QImode);
5854 rtx gpos = gen_reg_rtx (SImode);
5855 rtx done_label = gen_label_rtx ();
5856 rtx loop_label = gen_label_rtx ();
5857 rtx exit_label = gen_label_rtx ();
5858 rtx full_label = gen_label_rtx ();
5860 /* Perform a quick check for string ending on the first up to 16
5861 bytes and exit early if successful. */
5863 emit_insn (gen_vlbb (vsrc, src, GEN_INT (6)));
5864 emit_insn (gen_lcbb (loadlen, src_addr, GEN_INT (6)));
5865 emit_insn (gen_vfenezv16qi (vpos, vsrc, vsrc));
5866 emit_insn (gen_vec_extractv16qiqi (gpos_qi, vpos, GEN_INT (7)));
5867 emit_move_insn (gpos, gen_rtx_SUBREG (SImode, gpos_qi, 0));
5868 /* gpos is the byte index if a zero was found and 16 otherwise.
5869 So if it is lower than the loaded bytes we have a hit. */
5870 emit_cmp_and_jump_insns (gpos, loadlen, GE, NULL_RTX, SImode, 1,
5871 full_label);
5872 emit_insn (gen_vstlv16qi (vsrc, gpos, dst));
5874 force_expand_binop (Pmode, add_optab, dst_addr, gpos, result,
5875 1, OPTAB_DIRECT);
5876 emit_jump (exit_label);
5877 emit_barrier ();
5879 emit_label (full_label);
5880 LABEL_NUSES (full_label) = 1;
5882 /* Calculate `offset' so that src + offset points to the last byte
5883 before 16 byte alignment. */
5885 /* temp = src_addr & 0xf */
5886 force_expand_binop (Pmode, and_optab, src_addr, GEN_INT (15), temp,
5887 1, OPTAB_DIRECT);
5889 /* offset = 0xf - temp */
5890 emit_move_insn (offset, GEN_INT (15));
5891 force_expand_binop (Pmode, sub_optab, offset, temp, offset,
5892 1, OPTAB_DIRECT);
5894 /* Store `offset' bytes in the dstination string. The quick check
5895 has loaded at least `offset' bytes into vsrc. */
5897 emit_insn (gen_vstlv16qi (vsrc, gen_lowpart (SImode, offset), dst));
5899 /* Advance to the next byte to be loaded. */
5900 force_expand_binop (Pmode, add_optab, offset, const1_rtx, offset,
5901 1, OPTAB_DIRECT);
5903 /* Make sure the addresses are single regs which can be used as a
5904 base. */
5905 emit_move_insn (src_addr_reg, src_addr);
5906 emit_move_insn (dst_addr_reg, dst_addr);
5908 /* MAIN LOOP */
5910 emit_label (loop_label);
5911 LABEL_NUSES (loop_label) = 1;
5913 emit_move_insn (vsrc,
5914 gen_rtx_MEM (V16QImode,
5915 gen_rtx_PLUS (Pmode, src_addr_reg, offset)));
5917 emit_insn (gen_vec_vfenesv16qi (vpos, vsrc, vsrc,
5918 GEN_INT (VSTRING_FLAG_ZS | VSTRING_FLAG_CS)));
5919 add_int_reg_note (s390_emit_ccraw_jump (8, EQ, done_label),
5920 REG_BR_PROB, profile_probability::very_unlikely ()
5921 .to_reg_br_prob_note ());
5923 emit_move_insn (gen_rtx_MEM (V16QImode,
5924 gen_rtx_PLUS (Pmode, dst_addr_reg, offset)),
5925 vsrc);
5926 /* offset += 16 */
5927 force_expand_binop (Pmode, add_optab, offset, GEN_INT (16),
5928 offset, 1, OPTAB_DIRECT);
5930 emit_jump (loop_label);
5931 emit_barrier ();
5933 /* REGULAR EXIT */
5935 /* We are done. Add the offset of the zero character to the dst_addr
5936 pointer to get the result. */
5938 emit_label (done_label);
5939 LABEL_NUSES (done_label) = 1;
5941 force_expand_binop (Pmode, add_optab, dst_addr_reg, offset, dst_addr_reg,
5942 1, OPTAB_DIRECT);
5944 emit_insn (gen_vec_extractv16qiqi (gpos_qi, vpos, GEN_INT (7)));
5945 emit_move_insn (gpos, gen_rtx_SUBREG (SImode, gpos_qi, 0));
5947 emit_insn (gen_vstlv16qi (vsrc, gpos, gen_rtx_MEM (BLKmode, dst_addr_reg)));
5949 force_expand_binop (Pmode, add_optab, dst_addr_reg, gpos, result,
5950 1, OPTAB_DIRECT);
5952 /* EARLY EXIT */
5954 emit_label (exit_label);
5955 LABEL_NUSES (exit_label) = 1;
5959 /* Expand conditional increment or decrement using alc/slb instructions.
5960 Should generate code setting DST to either SRC or SRC + INCREMENT,
5961 depending on the result of the comparison CMP_OP0 CMP_CODE CMP_OP1.
5962 Returns true if successful, false otherwise.
5964 That makes it possible to implement some if-constructs without jumps e.g.:
5965 (borrow = CC0 | CC1 and carry = CC2 | CC3)
5966 unsigned int a, b, c;
5967 if (a < b) c++; -> CCU b > a -> CC2; c += carry;
5968 if (a < b) c--; -> CCL3 a - b -> borrow; c -= borrow;
5969 if (a <= b) c++; -> CCL3 b - a -> borrow; c += carry;
5970 if (a <= b) c--; -> CCU a <= b -> borrow; c -= borrow;
5972 Checks for EQ and NE with a nonzero value need an additional xor e.g.:
5973 if (a == b) c++; -> CCL3 a ^= b; 0 - a -> borrow; c += carry;
5974 if (a == b) c--; -> CCU a ^= b; a <= 0 -> CC0 | CC1; c -= borrow;
5975 if (a != b) c++; -> CCU a ^= b; a > 0 -> CC2; c += carry;
5976 if (a != b) c--; -> CCL3 a ^= b; 0 - a -> borrow; c -= borrow; */
5978 bool
5979 s390_expand_addcc (enum rtx_code cmp_code, rtx cmp_op0, rtx cmp_op1,
5980 rtx dst, rtx src, rtx increment)
5982 machine_mode cmp_mode;
5983 machine_mode cc_mode;
5984 rtx op_res;
5985 rtx insn;
5986 rtvec p;
5987 int ret;
5989 if ((GET_MODE (cmp_op0) == SImode || GET_MODE (cmp_op0) == VOIDmode)
5990 && (GET_MODE (cmp_op1) == SImode || GET_MODE (cmp_op1) == VOIDmode))
5991 cmp_mode = SImode;
5992 else if ((GET_MODE (cmp_op0) == DImode || GET_MODE (cmp_op0) == VOIDmode)
5993 && (GET_MODE (cmp_op1) == DImode || GET_MODE (cmp_op1) == VOIDmode))
5994 cmp_mode = DImode;
5995 else
5996 return false;
5998 /* Try ADD LOGICAL WITH CARRY. */
5999 if (increment == const1_rtx)
6001 /* Determine CC mode to use. */
6002 if (cmp_code == EQ || cmp_code == NE)
6004 if (cmp_op1 != const0_rtx)
6006 cmp_op0 = expand_simple_binop (cmp_mode, XOR, cmp_op0, cmp_op1,
6007 NULL_RTX, 0, OPTAB_WIDEN);
6008 cmp_op1 = const0_rtx;
6011 cmp_code = cmp_code == EQ ? LEU : GTU;
6014 if (cmp_code == LTU || cmp_code == LEU)
6016 rtx tem = cmp_op0;
6017 cmp_op0 = cmp_op1;
6018 cmp_op1 = tem;
6019 cmp_code = swap_condition (cmp_code);
6022 switch (cmp_code)
6024 case GTU:
6025 cc_mode = CCUmode;
6026 break;
6028 case GEU:
6029 cc_mode = CCL3mode;
6030 break;
6032 default:
6033 return false;
6036 /* Emit comparison instruction pattern. */
6037 if (!register_operand (cmp_op0, cmp_mode))
6038 cmp_op0 = force_reg (cmp_mode, cmp_op0);
6040 insn = gen_rtx_SET (gen_rtx_REG (cc_mode, CC_REGNUM),
6041 gen_rtx_COMPARE (cc_mode, cmp_op0, cmp_op1));
6042 /* We use insn_invalid_p here to add clobbers if required. */
6043 ret = insn_invalid_p (emit_insn (insn), false);
6044 gcc_assert (!ret);
6046 /* Emit ALC instruction pattern. */
6047 op_res = gen_rtx_fmt_ee (cmp_code, GET_MODE (dst),
6048 gen_rtx_REG (cc_mode, CC_REGNUM),
6049 const0_rtx);
6051 if (src != const0_rtx)
6053 if (!register_operand (src, GET_MODE (dst)))
6054 src = force_reg (GET_MODE (dst), src);
6056 op_res = gen_rtx_PLUS (GET_MODE (dst), op_res, src);
6057 op_res = gen_rtx_PLUS (GET_MODE (dst), op_res, const0_rtx);
6060 p = rtvec_alloc (2);
6061 RTVEC_ELT (p, 0) =
6062 gen_rtx_SET (dst, op_res);
6063 RTVEC_ELT (p, 1) =
6064 gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
6065 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
6067 return true;
6070 /* Try SUBTRACT LOGICAL WITH BORROW. */
6071 if (increment == constm1_rtx)
6073 /* Determine CC mode to use. */
6074 if (cmp_code == EQ || cmp_code == NE)
6076 if (cmp_op1 != const0_rtx)
6078 cmp_op0 = expand_simple_binop (cmp_mode, XOR, cmp_op0, cmp_op1,
6079 NULL_RTX, 0, OPTAB_WIDEN);
6080 cmp_op1 = const0_rtx;
6083 cmp_code = cmp_code == EQ ? LEU : GTU;
6086 if (cmp_code == GTU || cmp_code == GEU)
6088 rtx tem = cmp_op0;
6089 cmp_op0 = cmp_op1;
6090 cmp_op1 = tem;
6091 cmp_code = swap_condition (cmp_code);
6094 switch (cmp_code)
6096 case LEU:
6097 cc_mode = CCUmode;
6098 break;
6100 case LTU:
6101 cc_mode = CCL3mode;
6102 break;
6104 default:
6105 return false;
6108 /* Emit comparison instruction pattern. */
6109 if (!register_operand (cmp_op0, cmp_mode))
6110 cmp_op0 = force_reg (cmp_mode, cmp_op0);
6112 insn = gen_rtx_SET (gen_rtx_REG (cc_mode, CC_REGNUM),
6113 gen_rtx_COMPARE (cc_mode, cmp_op0, cmp_op1));
6114 /* We use insn_invalid_p here to add clobbers if required. */
6115 ret = insn_invalid_p (emit_insn (insn), false);
6116 gcc_assert (!ret);
6118 /* Emit SLB instruction pattern. */
6119 if (!register_operand (src, GET_MODE (dst)))
6120 src = force_reg (GET_MODE (dst), src);
6122 op_res = gen_rtx_MINUS (GET_MODE (dst),
6123 gen_rtx_MINUS (GET_MODE (dst), src, const0_rtx),
6124 gen_rtx_fmt_ee (cmp_code, GET_MODE (dst),
6125 gen_rtx_REG (cc_mode, CC_REGNUM),
6126 const0_rtx));
6127 p = rtvec_alloc (2);
6128 RTVEC_ELT (p, 0) =
6129 gen_rtx_SET (dst, op_res);
6130 RTVEC_ELT (p, 1) =
6131 gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
6132 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
6134 return true;
6137 return false;
6140 /* Expand code for the insv template. Return true if successful. */
6142 bool
6143 s390_expand_insv (rtx dest, rtx op1, rtx op2, rtx src)
6145 int bitsize = INTVAL (op1);
6146 int bitpos = INTVAL (op2);
6147 machine_mode mode = GET_MODE (dest);
6148 machine_mode smode;
6149 int smode_bsize, mode_bsize;
6150 rtx op, clobber;
6152 if (bitsize + bitpos > GET_MODE_BITSIZE (mode))
6153 return false;
6155 /* Generate INSERT IMMEDIATE (IILL et al). */
6156 /* (set (ze (reg)) (const_int)). */
6157 if (TARGET_ZARCH
6158 && register_operand (dest, word_mode)
6159 && (bitpos % 16) == 0
6160 && (bitsize % 16) == 0
6161 && const_int_operand (src, VOIDmode))
6163 HOST_WIDE_INT val = INTVAL (src);
6164 int regpos = bitpos + bitsize;
6166 while (regpos > bitpos)
6168 machine_mode putmode;
6169 int putsize;
6171 if (TARGET_EXTIMM && (regpos % 32 == 0) && (regpos >= bitpos + 32))
6172 putmode = SImode;
6173 else
6174 putmode = HImode;
6176 putsize = GET_MODE_BITSIZE (putmode);
6177 regpos -= putsize;
6178 emit_move_insn (gen_rtx_ZERO_EXTRACT (word_mode, dest,
6179 GEN_INT (putsize),
6180 GEN_INT (regpos)),
6181 gen_int_mode (val, putmode));
6182 val >>= putsize;
6184 gcc_assert (regpos == bitpos);
6185 return true;
6188 smode = smallest_mode_for_size (bitsize, MODE_INT);
6189 smode_bsize = GET_MODE_BITSIZE (smode);
6190 mode_bsize = GET_MODE_BITSIZE (mode);
6192 /* Generate STORE CHARACTERS UNDER MASK (STCM et al). */
6193 if (bitpos == 0
6194 && (bitsize % BITS_PER_UNIT) == 0
6195 && MEM_P (dest)
6196 && (register_operand (src, word_mode)
6197 || const_int_operand (src, VOIDmode)))
6199 /* Emit standard pattern if possible. */
6200 if (smode_bsize == bitsize)
6202 emit_move_insn (adjust_address (dest, smode, 0),
6203 gen_lowpart (smode, src));
6204 return true;
6207 /* (set (ze (mem)) (const_int)). */
6208 else if (const_int_operand (src, VOIDmode))
6210 int size = bitsize / BITS_PER_UNIT;
6211 rtx src_mem = adjust_address (force_const_mem (word_mode, src),
6212 BLKmode,
6213 UNITS_PER_WORD - size);
6215 dest = adjust_address (dest, BLKmode, 0);
6216 set_mem_size (dest, size);
6217 s390_expand_movmem (dest, src_mem, GEN_INT (size));
6218 return true;
6221 /* (set (ze (mem)) (reg)). */
6222 else if (register_operand (src, word_mode))
6224 if (bitsize <= 32)
6225 emit_move_insn (gen_rtx_ZERO_EXTRACT (word_mode, dest, op1,
6226 const0_rtx), src);
6227 else
6229 /* Emit st,stcmh sequence. */
6230 int stcmh_width = bitsize - 32;
6231 int size = stcmh_width / BITS_PER_UNIT;
6233 emit_move_insn (adjust_address (dest, SImode, size),
6234 gen_lowpart (SImode, src));
6235 set_mem_size (dest, size);
6236 emit_move_insn (gen_rtx_ZERO_EXTRACT (word_mode, dest,
6237 GEN_INT (stcmh_width),
6238 const0_rtx),
6239 gen_rtx_LSHIFTRT (word_mode, src, GEN_INT (32)));
6241 return true;
6245 /* Generate INSERT CHARACTERS UNDER MASK (IC, ICM et al). */
6246 if ((bitpos % BITS_PER_UNIT) == 0
6247 && (bitsize % BITS_PER_UNIT) == 0
6248 && (bitpos & 32) == ((bitpos + bitsize - 1) & 32)
6249 && MEM_P (src)
6250 && (mode == DImode || mode == SImode)
6251 && register_operand (dest, mode))
6253 /* Emit a strict_low_part pattern if possible. */
6254 if (smode_bsize == bitsize && bitpos == mode_bsize - smode_bsize)
6256 op = gen_rtx_STRICT_LOW_PART (VOIDmode, gen_lowpart (smode, dest));
6257 op = gen_rtx_SET (op, gen_lowpart (smode, src));
6258 clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
6259 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clobber)));
6260 return true;
6263 /* ??? There are more powerful versions of ICM that are not
6264 completely represented in the md file. */
6267 /* For z10, generate ROTATE THEN INSERT SELECTED BITS (RISBG et al). */
6268 if (TARGET_Z10 && (mode == DImode || mode == SImode))
6270 machine_mode mode_s = GET_MODE (src);
6272 if (CONSTANT_P (src))
6274 /* For constant zero values the representation with AND
6275 appears to be folded in more situations than the (set
6276 (zero_extract) ...).
6277 We only do this when the start and end of the bitfield
6278 remain in the same SImode chunk. That way nihf or nilf
6279 can be used.
6280 The AND patterns might still generate a risbg for this. */
6281 if (src == const0_rtx && bitpos / 32 == (bitpos + bitsize - 1) / 32)
6282 return false;
6283 else
6284 src = force_reg (mode, src);
6286 else if (mode_s != mode)
6288 gcc_assert (GET_MODE_BITSIZE (mode_s) >= bitsize);
6289 src = force_reg (mode_s, src);
6290 src = gen_lowpart (mode, src);
6293 op = gen_rtx_ZERO_EXTRACT (mode, dest, op1, op2),
6294 op = gen_rtx_SET (op, src);
6296 if (!TARGET_ZEC12)
6298 clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
6299 op = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clobber));
6301 emit_insn (op);
6303 return true;
6306 return false;
6309 /* A subroutine of s390_expand_cs_hqi and s390_expand_atomic which returns a
6310 register that holds VAL of mode MODE shifted by COUNT bits. */
6312 static inline rtx
6313 s390_expand_mask_and_shift (rtx val, machine_mode mode, rtx count)
6315 val = expand_simple_binop (SImode, AND, val, GEN_INT (GET_MODE_MASK (mode)),
6316 NULL_RTX, 1, OPTAB_DIRECT);
6317 return expand_simple_binop (SImode, ASHIFT, val, count,
6318 NULL_RTX, 1, OPTAB_DIRECT);
6321 /* Generate a vector comparison COND of CMP_OP1 and CMP_OP2 and store
6322 the result in TARGET. */
6324 void
6325 s390_expand_vec_compare (rtx target, enum rtx_code cond,
6326 rtx cmp_op1, rtx cmp_op2)
6328 machine_mode mode = GET_MODE (target);
6329 bool neg_p = false, swap_p = false;
6330 rtx tmp;
6332 if (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_VECTOR_FLOAT)
6334 switch (cond)
6336 /* NE a != b -> !(a == b) */
6337 case NE: cond = EQ; neg_p = true; break;
6338 /* UNGT a u> b -> !(b >= a) */
6339 case UNGT: cond = GE; neg_p = true; swap_p = true; break;
6340 /* UNGE a u>= b -> !(b > a) */
6341 case UNGE: cond = GT; neg_p = true; swap_p = true; break;
6342 /* LE: a <= b -> b >= a */
6343 case LE: cond = GE; swap_p = true; break;
6344 /* UNLE: a u<= b -> !(a > b) */
6345 case UNLE: cond = GT; neg_p = true; break;
6346 /* LT: a < b -> b > a */
6347 case LT: cond = GT; swap_p = true; break;
6348 /* UNLT: a u< b -> !(a >= b) */
6349 case UNLT: cond = GE; neg_p = true; break;
6350 case UNEQ:
6351 emit_insn (gen_vec_cmpuneqv2df (target, cmp_op1, cmp_op2));
6352 return;
6353 case LTGT:
6354 emit_insn (gen_vec_cmpltgtv2df (target, cmp_op1, cmp_op2));
6355 return;
6356 case ORDERED:
6357 emit_insn (gen_vec_orderedv2df (target, cmp_op1, cmp_op2));
6358 return;
6359 case UNORDERED:
6360 emit_insn (gen_vec_unorderedv2df (target, cmp_op1, cmp_op2));
6361 return;
6362 default: break;
6365 else
6367 switch (cond)
6369 /* NE: a != b -> !(a == b) */
6370 case NE: cond = EQ; neg_p = true; break;
6371 /* GE: a >= b -> !(b > a) */
6372 case GE: cond = GT; neg_p = true; swap_p = true; break;
6373 /* GEU: a >= b -> !(b > a) */
6374 case GEU: cond = GTU; neg_p = true; swap_p = true; break;
6375 /* LE: a <= b -> !(a > b) */
6376 case LE: cond = GT; neg_p = true; break;
6377 /* LEU: a <= b -> !(a > b) */
6378 case LEU: cond = GTU; neg_p = true; break;
6379 /* LT: a < b -> b > a */
6380 case LT: cond = GT; swap_p = true; break;
6381 /* LTU: a < b -> b > a */
6382 case LTU: cond = GTU; swap_p = true; break;
6383 default: break;
6387 if (swap_p)
6389 tmp = cmp_op1; cmp_op1 = cmp_op2; cmp_op2 = tmp;
6392 emit_insn (gen_rtx_SET (target, gen_rtx_fmt_ee (cond,
6393 mode,
6394 cmp_op1, cmp_op2)));
6395 if (neg_p)
6396 emit_insn (gen_rtx_SET (target, gen_rtx_NOT (mode, target)));
6399 /* Expand the comparison CODE of CMP1 and CMP2 and copy 1 or 0 into
6400 TARGET if either all (ALL_P is true) or any (ALL_P is false) of the
6401 elements in CMP1 and CMP2 fulfill the comparison.
6402 This function is only used to emit patterns for the vx builtins and
6403 therefore only handles comparison codes required by the
6404 builtins. */
6405 void
6406 s390_expand_vec_compare_cc (rtx target, enum rtx_code code,
6407 rtx cmp1, rtx cmp2, bool all_p)
6409 machine_mode cc_producer_mode, cc_consumer_mode, scratch_mode;
6410 rtx tmp_reg = gen_reg_rtx (SImode);
6411 bool swap_p = false;
6413 if (GET_MODE_CLASS (GET_MODE (cmp1)) == MODE_VECTOR_INT)
6415 switch (code)
6417 case EQ:
6418 case NE:
6419 cc_producer_mode = CCVEQmode;
6420 break;
6421 case GE:
6422 case LT:
6423 code = swap_condition (code);
6424 swap_p = true;
6425 /* fallthrough */
6426 case GT:
6427 case LE:
6428 cc_producer_mode = CCVIHmode;
6429 break;
6430 case GEU:
6431 case LTU:
6432 code = swap_condition (code);
6433 swap_p = true;
6434 /* fallthrough */
6435 case GTU:
6436 case LEU:
6437 cc_producer_mode = CCVIHUmode;
6438 break;
6439 default:
6440 gcc_unreachable ();
6443 scratch_mode = GET_MODE (cmp1);
6444 /* These codes represent inverted CC interpretations. Inverting
6445 an ALL CC mode results in an ANY CC mode and the other way
6446 around. Invert the all_p flag here to compensate for
6447 that. */
6448 if (code == NE || code == LE || code == LEU)
6449 all_p = !all_p;
6451 cc_consumer_mode = all_p ? CCVIALLmode : CCVIANYmode;
6453 else if (GET_MODE_CLASS (GET_MODE (cmp1)) == MODE_VECTOR_FLOAT)
6455 bool inv_p = false;
6457 switch (code)
6459 case EQ: cc_producer_mode = CCVEQmode; break;
6460 case NE: cc_producer_mode = CCVEQmode; inv_p = true; break;
6461 case GT: cc_producer_mode = CCVFHmode; break;
6462 case GE: cc_producer_mode = CCVFHEmode; break;
6463 case UNLE: cc_producer_mode = CCVFHmode; inv_p = true; break;
6464 case UNLT: cc_producer_mode = CCVFHEmode; inv_p = true; break;
6465 case LT: cc_producer_mode = CCVFHmode; code = GT; swap_p = true; break;
6466 case LE: cc_producer_mode = CCVFHEmode; code = GE; swap_p = true; break;
6467 default: gcc_unreachable ();
6469 scratch_mode = mode_for_vector
6470 (int_mode_for_mode (GET_MODE_INNER (GET_MODE (cmp1))).require (),
6471 GET_MODE_NUNITS (GET_MODE (cmp1)));
6472 gcc_assert (scratch_mode != BLKmode);
6474 if (inv_p)
6475 all_p = !all_p;
6477 cc_consumer_mode = all_p ? CCVFALLmode : CCVFANYmode;
6479 else
6480 gcc_unreachable ();
6482 if (swap_p)
6484 rtx tmp = cmp2;
6485 cmp2 = cmp1;
6486 cmp1 = tmp;
6489 emit_insn (gen_rtx_PARALLEL (VOIDmode,
6490 gen_rtvec (2, gen_rtx_SET (
6491 gen_rtx_REG (cc_producer_mode, CC_REGNUM),
6492 gen_rtx_COMPARE (cc_producer_mode, cmp1, cmp2)),
6493 gen_rtx_CLOBBER (VOIDmode,
6494 gen_rtx_SCRATCH (scratch_mode)))));
6495 emit_move_insn (target, const0_rtx);
6496 emit_move_insn (tmp_reg, const1_rtx);
6498 emit_move_insn (target,
6499 gen_rtx_IF_THEN_ELSE (SImode,
6500 gen_rtx_fmt_ee (code, VOIDmode,
6501 gen_rtx_REG (cc_consumer_mode, CC_REGNUM),
6502 const0_rtx),
6503 tmp_reg, target));
6506 /* Invert the comparison CODE applied to a CC mode. This is only safe
6507 if we know whether there result was created by a floating point
6508 compare or not. For the CCV modes this is encoded as part of the
6509 mode. */
6510 enum rtx_code
6511 s390_reverse_condition (machine_mode mode, enum rtx_code code)
6513 /* Reversal of FP compares takes care -- an ordered compare
6514 becomes an unordered compare and vice versa. */
6515 if (mode == CCVFALLmode || mode == CCVFANYmode)
6516 return reverse_condition_maybe_unordered (code);
6517 else if (mode == CCVIALLmode || mode == CCVIANYmode)
6518 return reverse_condition (code);
6519 else
6520 gcc_unreachable ();
6523 /* Generate a vector comparison expression loading either elements of
6524 THEN or ELS into TARGET depending on the comparison COND of CMP_OP1
6525 and CMP_OP2. */
6527 void
6528 s390_expand_vcond (rtx target, rtx then, rtx els,
6529 enum rtx_code cond, rtx cmp_op1, rtx cmp_op2)
6531 rtx tmp;
6532 machine_mode result_mode;
6533 rtx result_target;
6535 machine_mode target_mode = GET_MODE (target);
6536 machine_mode cmp_mode = GET_MODE (cmp_op1);
6537 rtx op = (cond == LT) ? els : then;
6539 /* Try to optimize x < 0 ? -1 : 0 into (signed) x >> 31
6540 and x < 0 ? 1 : 0 into (unsigned) x >> 31. Likewise
6541 for short and byte (x >> 15 and x >> 7 respectively). */
6542 if ((cond == LT || cond == GE)
6543 && target_mode == cmp_mode
6544 && cmp_op2 == CONST0_RTX (cmp_mode)
6545 && op == CONST0_RTX (target_mode)
6546 && s390_vector_mode_supported_p (target_mode)
6547 && GET_MODE_CLASS (target_mode) == MODE_VECTOR_INT)
6549 rtx negop = (cond == LT) ? then : els;
6551 int shift = GET_MODE_BITSIZE (GET_MODE_INNER (target_mode)) - 1;
6553 /* if x < 0 ? 1 : 0 or if x >= 0 ? 0 : 1 */
6554 if (negop == CONST1_RTX (target_mode))
6556 rtx res = expand_simple_binop (cmp_mode, LSHIFTRT, cmp_op1,
6557 GEN_INT (shift), target,
6558 1, OPTAB_DIRECT);
6559 if (res != target)
6560 emit_move_insn (target, res);
6561 return;
6564 /* if x < 0 ? -1 : 0 or if x >= 0 ? 0 : -1 */
6565 else if (all_ones_operand (negop, target_mode))
6567 rtx res = expand_simple_binop (cmp_mode, ASHIFTRT, cmp_op1,
6568 GEN_INT (shift), target,
6569 0, OPTAB_DIRECT);
6570 if (res != target)
6571 emit_move_insn (target, res);
6572 return;
6576 /* We always use an integral type vector to hold the comparison
6577 result. */
6578 result_mode = mode_for_vector
6579 (int_mode_for_mode (GET_MODE_INNER (cmp_mode)).require (),
6580 GET_MODE_NUNITS (cmp_mode));
6581 result_target = gen_reg_rtx (result_mode);
6583 /* We allow vector immediates as comparison operands that
6584 can be handled by the optimization above but not by the
6585 following code. Hence, force them into registers here. */
6586 if (!REG_P (cmp_op1))
6587 cmp_op1 = force_reg (GET_MODE (cmp_op1), cmp_op1);
6589 if (!REG_P (cmp_op2))
6590 cmp_op2 = force_reg (GET_MODE (cmp_op2), cmp_op2);
6592 s390_expand_vec_compare (result_target, cond,
6593 cmp_op1, cmp_op2);
6595 /* If the results are supposed to be either -1 or 0 we are done
6596 since this is what our compare instructions generate anyway. */
6597 if (all_ones_operand (then, GET_MODE (then))
6598 && const0_operand (els, GET_MODE (els)))
6600 emit_move_insn (target, gen_rtx_SUBREG (target_mode,
6601 result_target, 0));
6602 return;
6605 /* Otherwise we will do a vsel afterwards. */
6606 /* This gets triggered e.g.
6607 with gcc.c-torture/compile/pr53410-1.c */
6608 if (!REG_P (then))
6609 then = force_reg (target_mode, then);
6611 if (!REG_P (els))
6612 els = force_reg (target_mode, els);
6614 tmp = gen_rtx_fmt_ee (EQ, VOIDmode,
6615 result_target,
6616 CONST0_RTX (result_mode));
6618 /* We compared the result against zero above so we have to swap then
6619 and els here. */
6620 tmp = gen_rtx_IF_THEN_ELSE (target_mode, tmp, els, then);
6622 gcc_assert (target_mode == GET_MODE (then));
6623 emit_insn (gen_rtx_SET (target, tmp));
6626 /* Emit the RTX necessary to initialize the vector TARGET with values
6627 in VALS. */
6628 void
6629 s390_expand_vec_init (rtx target, rtx vals)
6631 machine_mode mode = GET_MODE (target);
6632 machine_mode inner_mode = GET_MODE_INNER (mode);
6633 int n_elts = GET_MODE_NUNITS (mode);
6634 bool all_same = true, all_regs = true, all_const_int = true;
6635 rtx x;
6636 int i;
6638 for (i = 0; i < n_elts; ++i)
6640 x = XVECEXP (vals, 0, i);
6642 if (!CONST_INT_P (x))
6643 all_const_int = false;
6645 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
6646 all_same = false;
6648 if (!REG_P (x))
6649 all_regs = false;
6652 /* Use vector gen mask or vector gen byte mask if possible. */
6653 if (all_same && all_const_int
6654 && (XVECEXP (vals, 0, 0) == const0_rtx
6655 || s390_contiguous_bitmask_vector_p (XVECEXP (vals, 0, 0),
6656 NULL, NULL)
6657 || s390_bytemask_vector_p (XVECEXP (vals, 0, 0), NULL)))
6659 emit_insn (gen_rtx_SET (target,
6660 gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0))));
6661 return;
6664 if (all_same)
6666 emit_insn (gen_rtx_SET (target,
6667 gen_rtx_VEC_DUPLICATE (mode,
6668 XVECEXP (vals, 0, 0))));
6669 return;
6672 if (all_regs
6673 && REG_P (target)
6674 && n_elts == 2
6675 && GET_MODE_SIZE (inner_mode) == 8)
6677 /* Use vector load pair. */
6678 emit_insn (gen_rtx_SET (target,
6679 gen_rtx_VEC_CONCAT (mode,
6680 XVECEXP (vals, 0, 0),
6681 XVECEXP (vals, 0, 1))));
6682 return;
6685 /* Use vector load logical element and zero. */
6686 if (TARGET_VXE && (mode == V4SImode || mode == V4SFmode))
6688 bool found = true;
6690 x = XVECEXP (vals, 0, 0);
6691 if (memory_operand (x, inner_mode))
6693 for (i = 1; i < n_elts; ++i)
6694 found = found && XVECEXP (vals, 0, i) == const0_rtx;
6696 if (found)
6698 machine_mode half_mode = (inner_mode == SFmode
6699 ? V2SFmode : V2SImode);
6700 emit_insn (gen_rtx_SET (target,
6701 gen_rtx_VEC_CONCAT (mode,
6702 gen_rtx_VEC_CONCAT (half_mode,
6704 const0_rtx),
6705 gen_rtx_VEC_CONCAT (half_mode,
6706 const0_rtx,
6707 const0_rtx))));
6708 return;
6713 /* We are about to set the vector elements one by one. Zero out the
6714 full register first in order to help the data flow framework to
6715 detect it as full VR set. */
6716 emit_insn (gen_rtx_SET (target, CONST0_RTX (mode)));
6718 /* Unfortunately the vec_init expander is not allowed to fail. So
6719 we have to implement the fallback ourselves. */
6720 for (i = 0; i < n_elts; i++)
6722 rtx elem = XVECEXP (vals, 0, i);
6723 if (!general_operand (elem, GET_MODE (elem)))
6724 elem = force_reg (inner_mode, elem);
6726 emit_insn (gen_rtx_SET (target,
6727 gen_rtx_UNSPEC (mode,
6728 gen_rtvec (3, elem,
6729 GEN_INT (i), target),
6730 UNSPEC_VEC_SET)));
6734 /* Structure to hold the initial parameters for a compare_and_swap operation
6735 in HImode and QImode. */
6737 struct alignment_context
6739 rtx memsi; /* SI aligned memory location. */
6740 rtx shift; /* Bit offset with regard to lsb. */
6741 rtx modemask; /* Mask of the HQImode shifted by SHIFT bits. */
6742 rtx modemaski; /* ~modemask */
6743 bool aligned; /* True if memory is aligned, false else. */
6746 /* A subroutine of s390_expand_cs_hqi and s390_expand_atomic to initialize
6747 structure AC for transparent simplifying, if the memory alignment is known
6748 to be at least 32bit. MEM is the memory location for the actual operation
6749 and MODE its mode. */
6751 static void
6752 init_alignment_context (struct alignment_context *ac, rtx mem,
6753 machine_mode mode)
6755 ac->shift = GEN_INT (GET_MODE_SIZE (SImode) - GET_MODE_SIZE (mode));
6756 ac->aligned = (MEM_ALIGN (mem) >= GET_MODE_BITSIZE (SImode));
6758 if (ac->aligned)
6759 ac->memsi = adjust_address (mem, SImode, 0); /* Memory is aligned. */
6760 else
6762 /* Alignment is unknown. */
6763 rtx byteoffset, addr, align;
6765 /* Force the address into a register. */
6766 addr = force_reg (Pmode, XEXP (mem, 0));
6768 /* Align it to SImode. */
6769 align = expand_simple_binop (Pmode, AND, addr,
6770 GEN_INT (-GET_MODE_SIZE (SImode)),
6771 NULL_RTX, 1, OPTAB_DIRECT);
6772 /* Generate MEM. */
6773 ac->memsi = gen_rtx_MEM (SImode, align);
6774 MEM_VOLATILE_P (ac->memsi) = MEM_VOLATILE_P (mem);
6775 set_mem_alias_set (ac->memsi, ALIAS_SET_MEMORY_BARRIER);
6776 set_mem_align (ac->memsi, GET_MODE_BITSIZE (SImode));
6778 /* Calculate shiftcount. */
6779 byteoffset = expand_simple_binop (Pmode, AND, addr,
6780 GEN_INT (GET_MODE_SIZE (SImode) - 1),
6781 NULL_RTX, 1, OPTAB_DIRECT);
6782 /* As we already have some offset, evaluate the remaining distance. */
6783 ac->shift = expand_simple_binop (SImode, MINUS, ac->shift, byteoffset,
6784 NULL_RTX, 1, OPTAB_DIRECT);
6787 /* Shift is the byte count, but we need the bitcount. */
6788 ac->shift = expand_simple_binop (SImode, ASHIFT, ac->shift, GEN_INT (3),
6789 NULL_RTX, 1, OPTAB_DIRECT);
6791 /* Calculate masks. */
6792 ac->modemask = expand_simple_binop (SImode, ASHIFT,
6793 GEN_INT (GET_MODE_MASK (mode)),
6794 ac->shift, NULL_RTX, 1, OPTAB_DIRECT);
6795 ac->modemaski = expand_simple_unop (SImode, NOT, ac->modemask,
6796 NULL_RTX, 1);
6799 /* A subroutine of s390_expand_cs_hqi. Insert INS into VAL. If possible,
6800 use a single insv insn into SEQ2. Otherwise, put prep insns in SEQ1 and
6801 perform the merge in SEQ2. */
6803 static rtx
6804 s390_two_part_insv (struct alignment_context *ac, rtx *seq1, rtx *seq2,
6805 machine_mode mode, rtx val, rtx ins)
6807 rtx tmp;
6809 if (ac->aligned)
6811 start_sequence ();
6812 tmp = copy_to_mode_reg (SImode, val);
6813 if (s390_expand_insv (tmp, GEN_INT (GET_MODE_BITSIZE (mode)),
6814 const0_rtx, ins))
6816 *seq1 = NULL;
6817 *seq2 = get_insns ();
6818 end_sequence ();
6819 return tmp;
6821 end_sequence ();
6824 /* Failed to use insv. Generate a two part shift and mask. */
6825 start_sequence ();
6826 tmp = s390_expand_mask_and_shift (ins, mode, ac->shift);
6827 *seq1 = get_insns ();
6828 end_sequence ();
6830 start_sequence ();
6831 tmp = expand_simple_binop (SImode, IOR, tmp, val, NULL_RTX, 1, OPTAB_DIRECT);
6832 *seq2 = get_insns ();
6833 end_sequence ();
6835 return tmp;
6838 /* Expand an atomic compare and swap operation for HImode and QImode. MEM is
6839 the memory location, CMP the old value to compare MEM with and NEW_RTX the
6840 value to set if CMP == MEM. */
6842 static void
6843 s390_expand_cs_hqi (machine_mode mode, rtx btarget, rtx vtarget, rtx mem,
6844 rtx cmp, rtx new_rtx, bool is_weak)
6846 struct alignment_context ac;
6847 rtx cmpv, newv, val, cc, seq0, seq1, seq2, seq3;
6848 rtx res = gen_reg_rtx (SImode);
6849 rtx_code_label *csloop = NULL, *csend = NULL;
6851 gcc_assert (MEM_P (mem));
6853 init_alignment_context (&ac, mem, mode);
6855 /* Load full word. Subsequent loads are performed by CS. */
6856 val = expand_simple_binop (SImode, AND, ac.memsi, ac.modemaski,
6857 NULL_RTX, 1, OPTAB_DIRECT);
6859 /* Prepare insertions of cmp and new_rtx into the loaded value. When
6860 possible, we try to use insv to make this happen efficiently. If
6861 that fails we'll generate code both inside and outside the loop. */
6862 cmpv = s390_two_part_insv (&ac, &seq0, &seq2, mode, val, cmp);
6863 newv = s390_two_part_insv (&ac, &seq1, &seq3, mode, val, new_rtx);
6865 if (seq0)
6866 emit_insn (seq0);
6867 if (seq1)
6868 emit_insn (seq1);
6870 /* Start CS loop. */
6871 if (!is_weak)
6873 /* Begin assuming success. */
6874 emit_move_insn (btarget, const1_rtx);
6876 csloop = gen_label_rtx ();
6877 csend = gen_label_rtx ();
6878 emit_label (csloop);
6881 /* val = "<mem>00..0<mem>"
6882 * cmp = "00..0<cmp>00..0"
6883 * new = "00..0<new>00..0"
6886 emit_insn (seq2);
6887 emit_insn (seq3);
6889 cc = s390_emit_compare_and_swap (EQ, res, ac.memsi, cmpv, newv, CCZ1mode);
6890 if (is_weak)
6891 emit_insn (gen_cstorecc4 (btarget, cc, XEXP (cc, 0), XEXP (cc, 1)));
6892 else
6894 rtx tmp;
6896 /* Jump to end if we're done (likely?). */
6897 s390_emit_jump (csend, cc);
6899 /* Check for changes outside mode, and loop internal if so.
6900 Arrange the moves so that the compare is adjacent to the
6901 branch so that we can generate CRJ. */
6902 tmp = copy_to_reg (val);
6903 force_expand_binop (SImode, and_optab, res, ac.modemaski, val,
6904 1, OPTAB_DIRECT);
6905 cc = s390_emit_compare (NE, val, tmp);
6906 s390_emit_jump (csloop, cc);
6908 /* Failed. */
6909 emit_move_insn (btarget, const0_rtx);
6910 emit_label (csend);
6913 /* Return the correct part of the bitfield. */
6914 convert_move (vtarget, expand_simple_binop (SImode, LSHIFTRT, res, ac.shift,
6915 NULL_RTX, 1, OPTAB_DIRECT), 1);
6918 /* Variant of s390_expand_cs for SI, DI and TI modes. */
6919 static void
6920 s390_expand_cs_tdsi (machine_mode mode, rtx btarget, rtx vtarget, rtx mem,
6921 rtx cmp, rtx new_rtx, bool is_weak)
6923 rtx output = vtarget;
6924 rtx_code_label *skip_cs_label = NULL;
6925 bool do_const_opt = false;
6927 if (!register_operand (output, mode))
6928 output = gen_reg_rtx (mode);
6930 /* If IS_WEAK is true and the INPUT value is a constant, compare the memory
6931 with the constant first and skip the compare_and_swap because its very
6932 expensive and likely to fail anyway.
6933 Note 1: This is done only for IS_WEAK. C11 allows optimizations that may
6934 cause spurious in that case.
6935 Note 2: It may be useful to do this also for non-constant INPUT.
6936 Note 3: Currently only targets with "load on condition" are supported
6937 (z196 and newer). */
6939 if (TARGET_Z196
6940 && (mode == SImode || mode == DImode))
6941 do_const_opt = (is_weak && CONST_INT_P (cmp));
6943 if (do_const_opt)
6945 rtx cc = gen_rtx_REG (CCZmode, CC_REGNUM);
6947 skip_cs_label = gen_label_rtx ();
6948 emit_move_insn (btarget, const0_rtx);
6949 if (CONST_INT_P (cmp) && INTVAL (cmp) == 0)
6951 rtvec lt = rtvec_alloc (2);
6953 /* Load-and-test + conditional jump. */
6954 RTVEC_ELT (lt, 0)
6955 = gen_rtx_SET (cc, gen_rtx_COMPARE (CCZmode, mem, cmp));
6956 RTVEC_ELT (lt, 1) = gen_rtx_SET (output, mem);
6957 emit_insn (gen_rtx_PARALLEL (VOIDmode, lt));
6959 else
6961 emit_move_insn (output, mem);
6962 emit_insn (gen_rtx_SET (cc, gen_rtx_COMPARE (CCZmode, output, cmp)));
6964 s390_emit_jump (skip_cs_label, gen_rtx_NE (VOIDmode, cc, const0_rtx));
6965 add_reg_br_prob_note (get_last_insn (),
6966 profile_probability::very_unlikely ());
6967 /* If the jump is not taken, OUTPUT is the expected value. */
6968 cmp = output;
6969 /* Reload newval to a register manually, *after* the compare and jump
6970 above. Otherwise Reload might place it before the jump. */
6972 else
6973 cmp = force_reg (mode, cmp);
6974 new_rtx = force_reg (mode, new_rtx);
6975 s390_emit_compare_and_swap (EQ, output, mem, cmp, new_rtx,
6976 (do_const_opt) ? CCZmode : CCZ1mode);
6977 if (skip_cs_label != NULL)
6978 emit_label (skip_cs_label);
6980 /* We deliberately accept non-register operands in the predicate
6981 to ensure the write back to the output operand happens *before*
6982 the store-flags code below. This makes it easier for combine
6983 to merge the store-flags code with a potential test-and-branch
6984 pattern following (immediately!) afterwards. */
6985 if (output != vtarget)
6986 emit_move_insn (vtarget, output);
6988 if (do_const_opt)
6990 rtx cc, cond, ite;
6992 /* Do not use gen_cstorecc4 here because it writes either 1 or 0, but
6993 btarget has already been initialized with 0 above. */
6994 cc = gen_rtx_REG (CCZmode, CC_REGNUM);
6995 cond = gen_rtx_EQ (VOIDmode, cc, const0_rtx);
6996 ite = gen_rtx_IF_THEN_ELSE (SImode, cond, const1_rtx, btarget);
6997 emit_insn (gen_rtx_SET (btarget, ite));
6999 else
7001 rtx cc, cond;
7003 cc = gen_rtx_REG (CCZ1mode, CC_REGNUM);
7004 cond = gen_rtx_EQ (SImode, cc, const0_rtx);
7005 emit_insn (gen_cstorecc4 (btarget, cond, cc, const0_rtx));
7009 /* Expand an atomic compare and swap operation. MEM is the memory location,
7010 CMP the old value to compare MEM with and NEW_RTX the value to set if
7011 CMP == MEM. */
7013 void
7014 s390_expand_cs (machine_mode mode, rtx btarget, rtx vtarget, rtx mem,
7015 rtx cmp, rtx new_rtx, bool is_weak)
7017 switch (mode)
7019 case E_TImode:
7020 case E_DImode:
7021 case E_SImode:
7022 s390_expand_cs_tdsi (mode, btarget, vtarget, mem, cmp, new_rtx, is_weak);
7023 break;
7024 case E_HImode:
7025 case E_QImode:
7026 s390_expand_cs_hqi (mode, btarget, vtarget, mem, cmp, new_rtx, is_weak);
7027 break;
7028 default:
7029 gcc_unreachable ();
7033 /* Expand an atomic_exchange operation simulated with a compare-and-swap loop.
7034 The memory location MEM is set to INPUT. OUTPUT is set to the previous value
7035 of MEM. */
7037 void
7038 s390_expand_atomic_exchange_tdsi (rtx output, rtx mem, rtx input)
7040 machine_mode mode = GET_MODE (mem);
7041 rtx_code_label *csloop;
7043 if (TARGET_Z196
7044 && (mode == DImode || mode == SImode)
7045 && CONST_INT_P (input) && INTVAL (input) == 0)
7047 emit_move_insn (output, const0_rtx);
7048 if (mode == DImode)
7049 emit_insn (gen_atomic_fetch_anddi (output, mem, const0_rtx, input));
7050 else
7051 emit_insn (gen_atomic_fetch_andsi (output, mem, const0_rtx, input));
7052 return;
7055 input = force_reg (mode, input);
7056 emit_move_insn (output, mem);
7057 csloop = gen_label_rtx ();
7058 emit_label (csloop);
7059 s390_emit_jump (csloop, s390_emit_compare_and_swap (NE, output, mem, output,
7060 input, CCZ1mode));
7063 /* Expand an atomic operation CODE of mode MODE. MEM is the memory location
7064 and VAL the value to play with. If AFTER is true then store the value
7065 MEM holds after the operation, if AFTER is false then store the value MEM
7066 holds before the operation. If TARGET is zero then discard that value, else
7067 store it to TARGET. */
7069 void
7070 s390_expand_atomic (machine_mode mode, enum rtx_code code,
7071 rtx target, rtx mem, rtx val, bool after)
7073 struct alignment_context ac;
7074 rtx cmp;
7075 rtx new_rtx = gen_reg_rtx (SImode);
7076 rtx orig = gen_reg_rtx (SImode);
7077 rtx_code_label *csloop = gen_label_rtx ();
7079 gcc_assert (!target || register_operand (target, VOIDmode));
7080 gcc_assert (MEM_P (mem));
7082 init_alignment_context (&ac, mem, mode);
7084 /* Shift val to the correct bit positions.
7085 Preserve "icm", but prevent "ex icm". */
7086 if (!(ac.aligned && code == SET && MEM_P (val)))
7087 val = s390_expand_mask_and_shift (val, mode, ac.shift);
7089 /* Further preparation insns. */
7090 if (code == PLUS || code == MINUS)
7091 emit_move_insn (orig, val);
7092 else if (code == MULT || code == AND) /* val = "11..1<val>11..1" */
7093 val = expand_simple_binop (SImode, XOR, val, ac.modemaski,
7094 NULL_RTX, 1, OPTAB_DIRECT);
7096 /* Load full word. Subsequent loads are performed by CS. */
7097 cmp = force_reg (SImode, ac.memsi);
7099 /* Start CS loop. */
7100 emit_label (csloop);
7101 emit_move_insn (new_rtx, cmp);
7103 /* Patch new with val at correct position. */
7104 switch (code)
7106 case PLUS:
7107 case MINUS:
7108 val = expand_simple_binop (SImode, code, new_rtx, orig,
7109 NULL_RTX, 1, OPTAB_DIRECT);
7110 val = expand_simple_binop (SImode, AND, val, ac.modemask,
7111 NULL_RTX, 1, OPTAB_DIRECT);
7112 /* FALLTHRU */
7113 case SET:
7114 if (ac.aligned && MEM_P (val))
7115 store_bit_field (new_rtx, GET_MODE_BITSIZE (mode), 0,
7116 0, 0, SImode, val, false);
7117 else
7119 new_rtx = expand_simple_binop (SImode, AND, new_rtx, ac.modemaski,
7120 NULL_RTX, 1, OPTAB_DIRECT);
7121 new_rtx = expand_simple_binop (SImode, IOR, new_rtx, val,
7122 NULL_RTX, 1, OPTAB_DIRECT);
7124 break;
7125 case AND:
7126 case IOR:
7127 case XOR:
7128 new_rtx = expand_simple_binop (SImode, code, new_rtx, val,
7129 NULL_RTX, 1, OPTAB_DIRECT);
7130 break;
7131 case MULT: /* NAND */
7132 new_rtx = expand_simple_binop (SImode, AND, new_rtx, val,
7133 NULL_RTX, 1, OPTAB_DIRECT);
7134 new_rtx = expand_simple_binop (SImode, XOR, new_rtx, ac.modemask,
7135 NULL_RTX, 1, OPTAB_DIRECT);
7136 break;
7137 default:
7138 gcc_unreachable ();
7141 s390_emit_jump (csloop, s390_emit_compare_and_swap (NE, cmp,
7142 ac.memsi, cmp, new_rtx,
7143 CCZ1mode));
7145 /* Return the correct part of the bitfield. */
7146 if (target)
7147 convert_move (target, expand_simple_binop (SImode, LSHIFTRT,
7148 after ? new_rtx : cmp, ac.shift,
7149 NULL_RTX, 1, OPTAB_DIRECT), 1);
7152 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
7153 We need to emit DTP-relative relocations. */
7155 static void s390_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
7157 static void
7158 s390_output_dwarf_dtprel (FILE *file, int size, rtx x)
7160 switch (size)
7162 case 4:
7163 fputs ("\t.long\t", file);
7164 break;
7165 case 8:
7166 fputs ("\t.quad\t", file);
7167 break;
7168 default:
7169 gcc_unreachable ();
7171 output_addr_const (file, x);
7172 fputs ("@DTPOFF", file);
7175 /* Return the proper mode for REGNO being represented in the dwarf
7176 unwind table. */
7177 machine_mode
7178 s390_dwarf_frame_reg_mode (int regno)
7180 machine_mode save_mode = default_dwarf_frame_reg_mode (regno);
7182 /* Make sure not to return DImode for any GPR with -m31 -mzarch. */
7183 if (GENERAL_REGNO_P (regno))
7184 save_mode = Pmode;
7186 /* The rightmost 64 bits of vector registers are call-clobbered. */
7187 if (GET_MODE_SIZE (save_mode) > 8)
7188 save_mode = DImode;
7190 return save_mode;
7193 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
7194 /* Implement TARGET_MANGLE_TYPE. */
7196 static const char *
7197 s390_mangle_type (const_tree type)
7199 type = TYPE_MAIN_VARIANT (type);
7201 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
7202 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
7203 return NULL;
7205 if (type == s390_builtin_types[BT_BV16QI]) return "U6__boolc";
7206 if (type == s390_builtin_types[BT_BV8HI]) return "U6__bools";
7207 if (type == s390_builtin_types[BT_BV4SI]) return "U6__booli";
7208 if (type == s390_builtin_types[BT_BV2DI]) return "U6__booll";
7210 if (TYPE_MAIN_VARIANT (type) == long_double_type_node
7211 && TARGET_LONG_DOUBLE_128)
7212 return "g";
7214 /* For all other types, use normal C++ mangling. */
7215 return NULL;
7217 #endif
7219 /* In the name of slightly smaller debug output, and to cater to
7220 general assembler lossage, recognize various UNSPEC sequences
7221 and turn them back into a direct symbol reference. */
7223 static rtx
7224 s390_delegitimize_address (rtx orig_x)
7226 rtx x, y;
7228 orig_x = delegitimize_mem_from_attrs (orig_x);
7229 x = orig_x;
7231 /* Extract the symbol ref from:
7232 (plus:SI (reg:SI 12 %r12)
7233 (const:SI (unspec:SI [(symbol_ref/f:SI ("*.LC0"))]
7234 UNSPEC_GOTOFF/PLTOFF)))
7236 (plus:SI (reg:SI 12 %r12)
7237 (const:SI (plus:SI (unspec:SI [(symbol_ref:SI ("L"))]
7238 UNSPEC_GOTOFF/PLTOFF)
7239 (const_int 4 [0x4])))) */
7240 if (GET_CODE (x) == PLUS
7241 && REG_P (XEXP (x, 0))
7242 && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM
7243 && GET_CODE (XEXP (x, 1)) == CONST)
7245 HOST_WIDE_INT offset = 0;
7247 /* The const operand. */
7248 y = XEXP (XEXP (x, 1), 0);
7250 if (GET_CODE (y) == PLUS
7251 && GET_CODE (XEXP (y, 1)) == CONST_INT)
7253 offset = INTVAL (XEXP (y, 1));
7254 y = XEXP (y, 0);
7257 if (GET_CODE (y) == UNSPEC
7258 && (XINT (y, 1) == UNSPEC_GOTOFF
7259 || XINT (y, 1) == UNSPEC_PLTOFF))
7260 return plus_constant (Pmode, XVECEXP (y, 0, 0), offset);
7263 if (GET_CODE (x) != MEM)
7264 return orig_x;
7266 x = XEXP (x, 0);
7267 if (GET_CODE (x) == PLUS
7268 && GET_CODE (XEXP (x, 1)) == CONST
7269 && GET_CODE (XEXP (x, 0)) == REG
7270 && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
7272 y = XEXP (XEXP (x, 1), 0);
7273 if (GET_CODE (y) == UNSPEC
7274 && XINT (y, 1) == UNSPEC_GOT)
7275 y = XVECEXP (y, 0, 0);
7276 else
7277 return orig_x;
7279 else if (GET_CODE (x) == CONST)
7281 /* Extract the symbol ref from:
7282 (mem:QI (const:DI (unspec:DI [(symbol_ref:DI ("foo"))]
7283 UNSPEC_PLT/GOTENT))) */
7285 y = XEXP (x, 0);
7286 if (GET_CODE (y) == UNSPEC
7287 && (XINT (y, 1) == UNSPEC_GOTENT
7288 || XINT (y, 1) == UNSPEC_PLT))
7289 y = XVECEXP (y, 0, 0);
7290 else
7291 return orig_x;
7293 else
7294 return orig_x;
7296 if (GET_MODE (orig_x) != Pmode)
7298 if (GET_MODE (orig_x) == BLKmode)
7299 return orig_x;
7300 y = lowpart_subreg (GET_MODE (orig_x), y, Pmode);
7301 if (y == NULL_RTX)
7302 return orig_x;
7304 return y;
7307 /* Output operand OP to stdio stream FILE.
7308 OP is an address (register + offset) which is not used to address data;
7309 instead the rightmost bits are interpreted as the value. */
7311 static void
7312 print_addrstyle_operand (FILE *file, rtx op)
7314 HOST_WIDE_INT offset;
7315 rtx base;
7317 /* Extract base register and offset. */
7318 if (!s390_decompose_addrstyle_without_index (op, &base, &offset))
7319 gcc_unreachable ();
7321 /* Sanity check. */
7322 if (base)
7324 gcc_assert (GET_CODE (base) == REG);
7325 gcc_assert (REGNO (base) < FIRST_PSEUDO_REGISTER);
7326 gcc_assert (REGNO_REG_CLASS (REGNO (base)) == ADDR_REGS);
7329 /* Offsets are constricted to twelve bits. */
7330 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset & ((1 << 12) - 1));
7331 if (base)
7332 fprintf (file, "(%s)", reg_names[REGNO (base)]);
7335 /* Assigns the number of NOP halfwords to be emitted before and after the
7336 function label to *HW_BEFORE and *HW_AFTER. Both pointers must not be NULL.
7337 If hotpatching is disabled for the function, the values are set to zero.
7340 static void
7341 s390_function_num_hotpatch_hw (tree decl,
7342 int *hw_before,
7343 int *hw_after)
7345 tree attr;
7347 attr = lookup_attribute ("hotpatch", DECL_ATTRIBUTES (decl));
7349 /* Handle the arguments of the hotpatch attribute. The values
7350 specified via attribute might override the cmdline argument
7351 values. */
7352 if (attr)
7354 tree args = TREE_VALUE (attr);
7356 *hw_before = TREE_INT_CST_LOW (TREE_VALUE (args));
7357 *hw_after = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args)));
7359 else
7361 /* Use the values specified by the cmdline arguments. */
7362 *hw_before = s390_hotpatch_hw_before_label;
7363 *hw_after = s390_hotpatch_hw_after_label;
7367 /* Write the current .machine and .machinemode specification to the assembler
7368 file. */
7370 #ifdef HAVE_AS_MACHINE_MACHINEMODE
7371 static void
7372 s390_asm_output_machine_for_arch (FILE *asm_out_file)
7374 fprintf (asm_out_file, "\t.machinemode %s\n",
7375 (TARGET_ZARCH) ? "zarch" : "esa");
7376 fprintf (asm_out_file, "\t.machine \"%s",
7377 processor_table[s390_arch].binutils_name);
7378 if (S390_USE_ARCHITECTURE_MODIFIERS)
7380 int cpu_flags;
7382 cpu_flags = processor_flags_table[(int) s390_arch];
7383 if (TARGET_HTM && !(cpu_flags & PF_TX))
7384 fprintf (asm_out_file, "+htm");
7385 else if (!TARGET_HTM && (cpu_flags & PF_TX))
7386 fprintf (asm_out_file, "+nohtm");
7387 if (TARGET_VX && !(cpu_flags & PF_VX))
7388 fprintf (asm_out_file, "+vx");
7389 else if (!TARGET_VX && (cpu_flags & PF_VX))
7390 fprintf (asm_out_file, "+novx");
7392 fprintf (asm_out_file, "\"\n");
7395 /* Write an extra function header before the very start of the function. */
7397 void
7398 s390_asm_output_function_prefix (FILE *asm_out_file,
7399 const char *fnname ATTRIBUTE_UNUSED)
7401 if (DECL_FUNCTION_SPECIFIC_TARGET (current_function_decl) == NULL)
7402 return;
7403 /* Since only the function specific options are saved but not the indications
7404 which options are set, it's too much work here to figure out which options
7405 have actually changed. Thus, generate .machine and .machinemode whenever a
7406 function has the target attribute or pragma. */
7407 fprintf (asm_out_file, "\t.machinemode push\n");
7408 fprintf (asm_out_file, "\t.machine push\n");
7409 s390_asm_output_machine_for_arch (asm_out_file);
7412 /* Write an extra function footer after the very end of the function. */
7414 void
7415 s390_asm_declare_function_size (FILE *asm_out_file,
7416 const char *fnname, tree decl)
7418 if (!flag_inhibit_size_directive)
7419 ASM_OUTPUT_MEASURED_SIZE (asm_out_file, fnname);
7420 if (DECL_FUNCTION_SPECIFIC_TARGET (decl) == NULL)
7421 return;
7422 fprintf (asm_out_file, "\t.machine pop\n");
7423 fprintf (asm_out_file, "\t.machinemode pop\n");
7425 #endif
7427 /* Write the extra assembler code needed to declare a function properly. */
7429 void
7430 s390_asm_output_function_label (FILE *asm_out_file, const char *fname,
7431 tree decl)
7433 int hw_before, hw_after;
7435 s390_function_num_hotpatch_hw (decl, &hw_before, &hw_after);
7436 if (hw_before > 0)
7438 unsigned int function_alignment;
7439 int i;
7441 /* Add a trampoline code area before the function label and initialize it
7442 with two-byte nop instructions. This area can be overwritten with code
7443 that jumps to a patched version of the function. */
7444 asm_fprintf (asm_out_file, "\tnopr\t%%r0"
7445 "\t# pre-label NOPs for hotpatch (%d halfwords)\n",
7446 hw_before);
7447 for (i = 1; i < hw_before; i++)
7448 fputs ("\tnopr\t%r0\n", asm_out_file);
7450 /* Note: The function label must be aligned so that (a) the bytes of the
7451 following nop do not cross a cacheline boundary, and (b) a jump address
7452 (eight bytes for 64 bit targets, 4 bytes for 32 bit targets) can be
7453 stored directly before the label without crossing a cacheline
7454 boundary. All this is necessary to make sure the trampoline code can
7455 be changed atomically.
7456 This alignment is done automatically using the FOUNCTION_BOUNDARY, but
7457 if there are NOPs before the function label, the alignment is placed
7458 before them. So it is necessary to duplicate the alignment after the
7459 NOPs. */
7460 function_alignment = MAX (8, DECL_ALIGN (decl) / BITS_PER_UNIT);
7461 if (! DECL_USER_ALIGN (decl))
7462 function_alignment = MAX (function_alignment,
7463 (unsigned int) align_functions);
7464 fputs ("\t# alignment for hotpatch\n", asm_out_file);
7465 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (function_alignment));
7468 if (S390_USE_TARGET_ATTRIBUTE && TARGET_DEBUG_ARG)
7470 asm_fprintf (asm_out_file, "\t# fn:%s ar%d\n", fname, s390_arch);
7471 asm_fprintf (asm_out_file, "\t# fn:%s tu%d\n", fname, s390_tune);
7472 asm_fprintf (asm_out_file, "\t# fn:%s sg%d\n", fname, s390_stack_guard);
7473 asm_fprintf (asm_out_file, "\t# fn:%s ss%d\n", fname, s390_stack_size);
7474 asm_fprintf (asm_out_file, "\t# fn:%s bc%d\n", fname, s390_branch_cost);
7475 asm_fprintf (asm_out_file, "\t# fn:%s wf%d\n", fname,
7476 s390_warn_framesize);
7477 asm_fprintf (asm_out_file, "\t# fn:%s ba%d\n", fname, TARGET_BACKCHAIN);
7478 asm_fprintf (asm_out_file, "\t# fn:%s hd%d\n", fname, TARGET_HARD_DFP);
7479 asm_fprintf (asm_out_file, "\t# fn:%s hf%d\n", fname, !TARGET_SOFT_FLOAT);
7480 asm_fprintf (asm_out_file, "\t# fn:%s ht%d\n", fname, TARGET_OPT_HTM);
7481 asm_fprintf (asm_out_file, "\t# fn:%s vx%d\n", fname, TARGET_OPT_VX);
7482 asm_fprintf (asm_out_file, "\t# fn:%s ps%d\n", fname,
7483 TARGET_PACKED_STACK);
7484 asm_fprintf (asm_out_file, "\t# fn:%s se%d\n", fname, TARGET_SMALL_EXEC);
7485 asm_fprintf (asm_out_file, "\t# fn:%s mv%d\n", fname, TARGET_MVCLE);
7486 asm_fprintf (asm_out_file, "\t# fn:%s zv%d\n", fname, TARGET_ZVECTOR);
7487 asm_fprintf (asm_out_file, "\t# fn:%s wd%d\n", fname,
7488 s390_warn_dynamicstack_p);
7490 ASM_OUTPUT_LABEL (asm_out_file, fname);
7491 if (hw_after > 0)
7492 asm_fprintf (asm_out_file,
7493 "\t# post-label NOPs for hotpatch (%d halfwords)\n",
7494 hw_after);
7497 /* Output machine-dependent UNSPECs occurring in address constant X
7498 in assembler syntax to stdio stream FILE. Returns true if the
7499 constant X could be recognized, false otherwise. */
7501 static bool
7502 s390_output_addr_const_extra (FILE *file, rtx x)
7504 if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 1)
7505 switch (XINT (x, 1))
7507 case UNSPEC_GOTENT:
7508 output_addr_const (file, XVECEXP (x, 0, 0));
7509 fprintf (file, "@GOTENT");
7510 return true;
7511 case UNSPEC_GOT:
7512 output_addr_const (file, XVECEXP (x, 0, 0));
7513 fprintf (file, "@GOT");
7514 return true;
7515 case UNSPEC_GOTOFF:
7516 output_addr_const (file, XVECEXP (x, 0, 0));
7517 fprintf (file, "@GOTOFF");
7518 return true;
7519 case UNSPEC_PLT:
7520 output_addr_const (file, XVECEXP (x, 0, 0));
7521 fprintf (file, "@PLT");
7522 return true;
7523 case UNSPEC_PLTOFF:
7524 output_addr_const (file, XVECEXP (x, 0, 0));
7525 fprintf (file, "@PLTOFF");
7526 return true;
7527 case UNSPEC_TLSGD:
7528 output_addr_const (file, XVECEXP (x, 0, 0));
7529 fprintf (file, "@TLSGD");
7530 return true;
7531 case UNSPEC_TLSLDM:
7532 assemble_name (file, get_some_local_dynamic_name ());
7533 fprintf (file, "@TLSLDM");
7534 return true;
7535 case UNSPEC_DTPOFF:
7536 output_addr_const (file, XVECEXP (x, 0, 0));
7537 fprintf (file, "@DTPOFF");
7538 return true;
7539 case UNSPEC_NTPOFF:
7540 output_addr_const (file, XVECEXP (x, 0, 0));
7541 fprintf (file, "@NTPOFF");
7542 return true;
7543 case UNSPEC_GOTNTPOFF:
7544 output_addr_const (file, XVECEXP (x, 0, 0));
7545 fprintf (file, "@GOTNTPOFF");
7546 return true;
7547 case UNSPEC_INDNTPOFF:
7548 output_addr_const (file, XVECEXP (x, 0, 0));
7549 fprintf (file, "@INDNTPOFF");
7550 return true;
7553 if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 2)
7554 switch (XINT (x, 1))
7556 case UNSPEC_POOL_OFFSET:
7557 x = gen_rtx_MINUS (GET_MODE (x), XVECEXP (x, 0, 0), XVECEXP (x, 0, 1));
7558 output_addr_const (file, x);
7559 return true;
7561 return false;
7564 /* Output address operand ADDR in assembler syntax to
7565 stdio stream FILE. */
7567 void
7568 print_operand_address (FILE *file, rtx addr)
7570 struct s390_address ad;
7571 memset (&ad, 0, sizeof (s390_address));
7573 if (s390_loadrelative_operand_p (addr, NULL, NULL))
7575 if (!TARGET_Z10)
7577 output_operand_lossage ("symbolic memory references are "
7578 "only supported on z10 or later");
7579 return;
7581 output_addr_const (file, addr);
7582 return;
7585 if (!s390_decompose_address (addr, &ad)
7586 || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
7587 || (ad.indx && !REGNO_OK_FOR_INDEX_P (REGNO (ad.indx))))
7588 output_operand_lossage ("cannot decompose address");
7590 if (ad.disp)
7591 output_addr_const (file, ad.disp);
7592 else
7593 fprintf (file, "0");
7595 if (ad.base && ad.indx)
7596 fprintf (file, "(%s,%s)", reg_names[REGNO (ad.indx)],
7597 reg_names[REGNO (ad.base)]);
7598 else if (ad.base)
7599 fprintf (file, "(%s)", reg_names[REGNO (ad.base)]);
7602 /* Output operand X in assembler syntax to stdio stream FILE.
7603 CODE specified the format flag. The following format flags
7604 are recognized:
7606 'C': print opcode suffix for branch condition.
7607 'D': print opcode suffix for inverse branch condition.
7608 'E': print opcode suffix for branch on index instruction.
7609 'G': print the size of the operand in bytes.
7610 'J': print tls_load/tls_gdcall/tls_ldcall suffix
7611 'M': print the second word of a TImode operand.
7612 'N': print the second word of a DImode operand.
7613 'O': print only the displacement of a memory reference or address.
7614 'R': print only the base register of a memory reference or address.
7615 'S': print S-type memory reference (base+displacement).
7616 'Y': print address style operand without index (e.g. shift count or setmem
7617 operand).
7619 'b': print integer X as if it's an unsigned byte.
7620 'c': print integer X as if it's an signed byte.
7621 'e': "end" contiguous bitmask X in either DImode or vector inner mode.
7622 'f': "end" contiguous bitmask X in SImode.
7623 'h': print integer X as if it's a signed halfword.
7624 'i': print the first nonzero HImode part of X.
7625 'j': print the first HImode part unequal to -1 of X.
7626 'k': print the first nonzero SImode part of X.
7627 'm': print the first SImode part unequal to -1 of X.
7628 'o': print integer X as if it's an unsigned 32bit word.
7629 's': "start" of contiguous bitmask X in either DImode or vector inner mode.
7630 't': CONST_INT: "start" of contiguous bitmask X in SImode.
7631 CONST_VECTOR: Generate a bitmask for vgbm instruction.
7632 'x': print integer X as if it's an unsigned halfword.
7633 'v': print register number as vector register (v1 instead of f1).
7636 void
7637 print_operand (FILE *file, rtx x, int code)
7639 HOST_WIDE_INT ival;
7641 switch (code)
7643 case 'C':
7644 fprintf (file, s390_branch_condition_mnemonic (x, FALSE));
7645 return;
7647 case 'D':
7648 fprintf (file, s390_branch_condition_mnemonic (x, TRUE));
7649 return;
7651 case 'E':
7652 if (GET_CODE (x) == LE)
7653 fprintf (file, "l");
7654 else if (GET_CODE (x) == GT)
7655 fprintf (file, "h");
7656 else
7657 output_operand_lossage ("invalid comparison operator "
7658 "for 'E' output modifier");
7659 return;
7661 case 'J':
7662 if (GET_CODE (x) == SYMBOL_REF)
7664 fprintf (file, "%s", ":tls_load:");
7665 output_addr_const (file, x);
7667 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSGD)
7669 fprintf (file, "%s", ":tls_gdcall:");
7670 output_addr_const (file, XVECEXP (x, 0, 0));
7672 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSLDM)
7674 fprintf (file, "%s", ":tls_ldcall:");
7675 const char *name = get_some_local_dynamic_name ();
7676 gcc_assert (name);
7677 assemble_name (file, name);
7679 else
7680 output_operand_lossage ("invalid reference for 'J' output modifier");
7681 return;
7683 case 'G':
7684 fprintf (file, "%u", GET_MODE_SIZE (GET_MODE (x)));
7685 return;
7687 case 'O':
7689 struct s390_address ad;
7690 int ret;
7692 ret = s390_decompose_address (MEM_P (x) ? XEXP (x, 0) : x, &ad);
7694 if (!ret
7695 || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
7696 || ad.indx)
7698 output_operand_lossage ("invalid address for 'O' output modifier");
7699 return;
7702 if (ad.disp)
7703 output_addr_const (file, ad.disp);
7704 else
7705 fprintf (file, "0");
7707 return;
7709 case 'R':
7711 struct s390_address ad;
7712 int ret;
7714 ret = s390_decompose_address (MEM_P (x) ? XEXP (x, 0) : x, &ad);
7716 if (!ret
7717 || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
7718 || ad.indx)
7720 output_operand_lossage ("invalid address for 'R' output modifier");
7721 return;
7724 if (ad.base)
7725 fprintf (file, "%s", reg_names[REGNO (ad.base)]);
7726 else
7727 fprintf (file, "0");
7729 return;
7731 case 'S':
7733 struct s390_address ad;
7734 int ret;
7736 if (!MEM_P (x))
7738 output_operand_lossage ("memory reference expected for "
7739 "'S' output modifier");
7740 return;
7742 ret = s390_decompose_address (XEXP (x, 0), &ad);
7744 if (!ret
7745 || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
7746 || ad.indx)
7748 output_operand_lossage ("invalid address for 'S' output modifier");
7749 return;
7752 if (ad.disp)
7753 output_addr_const (file, ad.disp);
7754 else
7755 fprintf (file, "0");
7757 if (ad.base)
7758 fprintf (file, "(%s)", reg_names[REGNO (ad.base)]);
7760 return;
7762 case 'N':
7763 if (GET_CODE (x) == REG)
7764 x = gen_rtx_REG (GET_MODE (x), REGNO (x) + 1);
7765 else if (GET_CODE (x) == MEM)
7766 x = change_address (x, VOIDmode,
7767 plus_constant (Pmode, XEXP (x, 0), 4));
7768 else
7769 output_operand_lossage ("register or memory expression expected "
7770 "for 'N' output modifier");
7771 break;
7773 case 'M':
7774 if (GET_CODE (x) == REG)
7775 x = gen_rtx_REG (GET_MODE (x), REGNO (x) + 1);
7776 else if (GET_CODE (x) == MEM)
7777 x = change_address (x, VOIDmode,
7778 plus_constant (Pmode, XEXP (x, 0), 8));
7779 else
7780 output_operand_lossage ("register or memory expression expected "
7781 "for 'M' output modifier");
7782 break;
7784 case 'Y':
7785 print_addrstyle_operand (file, x);
7786 return;
7789 switch (GET_CODE (x))
7791 case REG:
7792 /* Print FP regs as fx instead of vx when they are accessed
7793 through non-vector mode. */
7794 if (code == 'v'
7795 || VECTOR_NOFP_REG_P (x)
7796 || (FP_REG_P (x) && VECTOR_MODE_P (GET_MODE (x)))
7797 || (VECTOR_REG_P (x)
7798 && (GET_MODE_SIZE (GET_MODE (x)) /
7799 s390_class_max_nregs (FP_REGS, GET_MODE (x))) > 8))
7800 fprintf (file, "%%v%s", reg_names[REGNO (x)] + 2);
7801 else
7802 fprintf (file, "%s", reg_names[REGNO (x)]);
7803 break;
7805 case MEM:
7806 output_address (GET_MODE (x), XEXP (x, 0));
7807 break;
7809 case CONST:
7810 case CODE_LABEL:
7811 case LABEL_REF:
7812 case SYMBOL_REF:
7813 output_addr_const (file, x);
7814 break;
7816 case CONST_INT:
7817 ival = INTVAL (x);
7818 switch (code)
7820 case 0:
7821 break;
7822 case 'b':
7823 ival &= 0xff;
7824 break;
7825 case 'c':
7826 ival = ((ival & 0xff) ^ 0x80) - 0x80;
7827 break;
7828 case 'x':
7829 ival &= 0xffff;
7830 break;
7831 case 'h':
7832 ival = ((ival & 0xffff) ^ 0x8000) - 0x8000;
7833 break;
7834 case 'i':
7835 ival = s390_extract_part (x, HImode, 0);
7836 break;
7837 case 'j':
7838 ival = s390_extract_part (x, HImode, -1);
7839 break;
7840 case 'k':
7841 ival = s390_extract_part (x, SImode, 0);
7842 break;
7843 case 'm':
7844 ival = s390_extract_part (x, SImode, -1);
7845 break;
7846 case 'o':
7847 ival &= 0xffffffff;
7848 break;
7849 case 'e': case 'f':
7850 case 's': case 't':
7852 int start, end;
7853 int len;
7854 bool ok;
7856 len = (code == 's' || code == 'e' ? 64 : 32);
7857 ok = s390_contiguous_bitmask_p (ival, true, len, &start, &end);
7858 gcc_assert (ok);
7859 if (code == 's' || code == 't')
7860 ival = start;
7861 else
7862 ival = end;
7864 break;
7865 default:
7866 output_operand_lossage ("invalid constant for output modifier '%c'", code);
7868 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ival);
7869 break;
7871 case CONST_WIDE_INT:
7872 if (code == 'b')
7873 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
7874 CONST_WIDE_INT_ELT (x, 0) & 0xff);
7875 else if (code == 'x')
7876 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
7877 CONST_WIDE_INT_ELT (x, 0) & 0xffff);
7878 else if (code == 'h')
7879 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
7880 ((CONST_WIDE_INT_ELT (x, 0) & 0xffff) ^ 0x8000) - 0x8000);
7881 else
7883 if (code == 0)
7884 output_operand_lossage ("invalid constant - try using "
7885 "an output modifier");
7886 else
7887 output_operand_lossage ("invalid constant for output modifier '%c'",
7888 code);
7890 break;
7891 case CONST_VECTOR:
7892 switch (code)
7894 case 'h':
7895 gcc_assert (const_vec_duplicate_p (x));
7896 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
7897 ((INTVAL (XVECEXP (x, 0, 0)) & 0xffff) ^ 0x8000) - 0x8000);
7898 break;
7899 case 'e':
7900 case 's':
7902 int start, end;
7903 bool ok;
7905 ok = s390_contiguous_bitmask_vector_p (x, &start, &end);
7906 gcc_assert (ok);
7907 ival = (code == 's') ? start : end;
7908 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ival);
7910 break;
7911 case 't':
7913 unsigned mask;
7914 bool ok = s390_bytemask_vector_p (x, &mask);
7915 gcc_assert (ok);
7916 fprintf (file, "%u", mask);
7918 break;
7920 default:
7921 output_operand_lossage ("invalid constant vector for output "
7922 "modifier '%c'", code);
7924 break;
7926 default:
7927 if (code == 0)
7928 output_operand_lossage ("invalid expression - try using "
7929 "an output modifier");
7930 else
7931 output_operand_lossage ("invalid expression for output "
7932 "modifier '%c'", code);
7933 break;
7937 /* Target hook for assembling integer objects. We need to define it
7938 here to work a round a bug in some versions of GAS, which couldn't
7939 handle values smaller than INT_MIN when printed in decimal. */
7941 static bool
7942 s390_assemble_integer (rtx x, unsigned int size, int aligned_p)
7944 if (size == 8 && aligned_p
7945 && GET_CODE (x) == CONST_INT && INTVAL (x) < INT_MIN)
7947 fprintf (asm_out_file, "\t.quad\t" HOST_WIDE_INT_PRINT_HEX "\n",
7948 INTVAL (x));
7949 return true;
7951 return default_assemble_integer (x, size, aligned_p);
7954 /* Returns true if register REGNO is used for forming
7955 a memory address in expression X. */
7957 static bool
7958 reg_used_in_mem_p (int regno, rtx x)
7960 enum rtx_code code = GET_CODE (x);
7961 int i, j;
7962 const char *fmt;
7964 if (code == MEM)
7966 if (refers_to_regno_p (regno, XEXP (x, 0)))
7967 return true;
7969 else if (code == SET
7970 && GET_CODE (SET_DEST (x)) == PC)
7972 if (refers_to_regno_p (regno, SET_SRC (x)))
7973 return true;
7976 fmt = GET_RTX_FORMAT (code);
7977 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7979 if (fmt[i] == 'e'
7980 && reg_used_in_mem_p (regno, XEXP (x, i)))
7981 return true;
7983 else if (fmt[i] == 'E')
7984 for (j = 0; j < XVECLEN (x, i); j++)
7985 if (reg_used_in_mem_p (regno, XVECEXP (x, i, j)))
7986 return true;
7988 return false;
7991 /* Returns true if expression DEP_RTX sets an address register
7992 used by instruction INSN to address memory. */
7994 static bool
7995 addr_generation_dependency_p (rtx dep_rtx, rtx_insn *insn)
7997 rtx target, pat;
7999 if (NONJUMP_INSN_P (dep_rtx))
8000 dep_rtx = PATTERN (dep_rtx);
8002 if (GET_CODE (dep_rtx) == SET)
8004 target = SET_DEST (dep_rtx);
8005 if (GET_CODE (target) == STRICT_LOW_PART)
8006 target = XEXP (target, 0);
8007 while (GET_CODE (target) == SUBREG)
8008 target = SUBREG_REG (target);
8010 if (GET_CODE (target) == REG)
8012 int regno = REGNO (target);
8014 if (s390_safe_attr_type (insn) == TYPE_LA)
8016 pat = PATTERN (insn);
8017 if (GET_CODE (pat) == PARALLEL)
8019 gcc_assert (XVECLEN (pat, 0) == 2);
8020 pat = XVECEXP (pat, 0, 0);
8022 gcc_assert (GET_CODE (pat) == SET);
8023 return refers_to_regno_p (regno, SET_SRC (pat));
8025 else if (get_attr_atype (insn) == ATYPE_AGEN)
8026 return reg_used_in_mem_p (regno, PATTERN (insn));
8029 return false;
8032 /* Return 1, if dep_insn sets register used in insn in the agen unit. */
8035 s390_agen_dep_p (rtx_insn *dep_insn, rtx_insn *insn)
8037 rtx dep_rtx = PATTERN (dep_insn);
8038 int i;
8040 if (GET_CODE (dep_rtx) == SET
8041 && addr_generation_dependency_p (dep_rtx, insn))
8042 return 1;
8043 else if (GET_CODE (dep_rtx) == PARALLEL)
8045 for (i = 0; i < XVECLEN (dep_rtx, 0); i++)
8047 if (addr_generation_dependency_p (XVECEXP (dep_rtx, 0, i), insn))
8048 return 1;
8051 return 0;
8055 /* A C statement (sans semicolon) to update the integer scheduling priority
8056 INSN_PRIORITY (INSN). Increase the priority to execute the INSN earlier,
8057 reduce the priority to execute INSN later. Do not define this macro if
8058 you do not need to adjust the scheduling priorities of insns.
8060 A STD instruction should be scheduled earlier,
8061 in order to use the bypass. */
8062 static int
8063 s390_adjust_priority (rtx_insn *insn, int priority)
8065 if (! INSN_P (insn))
8066 return priority;
8068 if (s390_tune <= PROCESSOR_2064_Z900)
8069 return priority;
8071 switch (s390_safe_attr_type (insn))
8073 case TYPE_FSTOREDF:
8074 case TYPE_FSTORESF:
8075 priority = priority << 3;
8076 break;
8077 case TYPE_STORE:
8078 case TYPE_STM:
8079 priority = priority << 1;
8080 break;
8081 default:
8082 break;
8084 return priority;
8088 /* The number of instructions that can be issued per cycle. */
8090 static int
8091 s390_issue_rate (void)
8093 switch (s390_tune)
8095 case PROCESSOR_2084_Z990:
8096 case PROCESSOR_2094_Z9_109:
8097 case PROCESSOR_2094_Z9_EC:
8098 case PROCESSOR_2817_Z196:
8099 return 3;
8100 case PROCESSOR_2097_Z10:
8101 return 2;
8102 case PROCESSOR_9672_G5:
8103 case PROCESSOR_9672_G6:
8104 case PROCESSOR_2064_Z900:
8105 /* Starting with EC12 we use the sched_reorder hook to take care
8106 of instruction dispatch constraints. The algorithm only
8107 picks the best instruction and assumes only a single
8108 instruction gets issued per cycle. */
8109 case PROCESSOR_2827_ZEC12:
8110 case PROCESSOR_2964_Z13:
8111 case PROCESSOR_3906_Z14:
8112 default:
8113 return 1;
8117 static int
8118 s390_first_cycle_multipass_dfa_lookahead (void)
8120 return 4;
8123 /* Annotate every literal pool reference in X by an UNSPEC_LTREF expression.
8124 Fix up MEMs as required. */
8126 static void
8127 annotate_constant_pool_refs (rtx *x)
8129 int i, j;
8130 const char *fmt;
8132 gcc_assert (GET_CODE (*x) != SYMBOL_REF
8133 || !CONSTANT_POOL_ADDRESS_P (*x));
8135 /* Literal pool references can only occur inside a MEM ... */
8136 if (GET_CODE (*x) == MEM)
8138 rtx memref = XEXP (*x, 0);
8140 if (GET_CODE (memref) == SYMBOL_REF
8141 && CONSTANT_POOL_ADDRESS_P (memref))
8143 rtx base = cfun->machine->base_reg;
8144 rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, memref, base),
8145 UNSPEC_LTREF);
8147 *x = replace_equiv_address (*x, addr);
8148 return;
8151 if (GET_CODE (memref) == CONST
8152 && GET_CODE (XEXP (memref, 0)) == PLUS
8153 && GET_CODE (XEXP (XEXP (memref, 0), 1)) == CONST_INT
8154 && GET_CODE (XEXP (XEXP (memref, 0), 0)) == SYMBOL_REF
8155 && CONSTANT_POOL_ADDRESS_P (XEXP (XEXP (memref, 0), 0)))
8157 HOST_WIDE_INT off = INTVAL (XEXP (XEXP (memref, 0), 1));
8158 rtx sym = XEXP (XEXP (memref, 0), 0);
8159 rtx base = cfun->machine->base_reg;
8160 rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, sym, base),
8161 UNSPEC_LTREF);
8163 *x = replace_equiv_address (*x, plus_constant (Pmode, addr, off));
8164 return;
8168 /* ... or a load-address type pattern. */
8169 if (GET_CODE (*x) == SET)
8171 rtx addrref = SET_SRC (*x);
8173 if (GET_CODE (addrref) == SYMBOL_REF
8174 && CONSTANT_POOL_ADDRESS_P (addrref))
8176 rtx base = cfun->machine->base_reg;
8177 rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, addrref, base),
8178 UNSPEC_LTREF);
8180 SET_SRC (*x) = addr;
8181 return;
8184 if (GET_CODE (addrref) == CONST
8185 && GET_CODE (XEXP (addrref, 0)) == PLUS
8186 && GET_CODE (XEXP (XEXP (addrref, 0), 1)) == CONST_INT
8187 && GET_CODE (XEXP (XEXP (addrref, 0), 0)) == SYMBOL_REF
8188 && CONSTANT_POOL_ADDRESS_P (XEXP (XEXP (addrref, 0), 0)))
8190 HOST_WIDE_INT off = INTVAL (XEXP (XEXP (addrref, 0), 1));
8191 rtx sym = XEXP (XEXP (addrref, 0), 0);
8192 rtx base = cfun->machine->base_reg;
8193 rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, sym, base),
8194 UNSPEC_LTREF);
8196 SET_SRC (*x) = plus_constant (Pmode, addr, off);
8197 return;
8201 /* Annotate LTREL_BASE as well. */
8202 if (GET_CODE (*x) == UNSPEC
8203 && XINT (*x, 1) == UNSPEC_LTREL_BASE)
8205 rtx base = cfun->machine->base_reg;
8206 *x = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, XVECEXP (*x, 0, 0), base),
8207 UNSPEC_LTREL_BASE);
8208 return;
8211 fmt = GET_RTX_FORMAT (GET_CODE (*x));
8212 for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
8214 if (fmt[i] == 'e')
8216 annotate_constant_pool_refs (&XEXP (*x, i));
8218 else if (fmt[i] == 'E')
8220 for (j = 0; j < XVECLEN (*x, i); j++)
8221 annotate_constant_pool_refs (&XVECEXP (*x, i, j));
8226 /* Split all branches that exceed the maximum distance.
8227 Returns true if this created a new literal pool entry. */
8229 static int
8230 s390_split_branches (void)
8232 rtx temp_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
8233 int new_literal = 0, ret;
8234 rtx_insn *insn;
8235 rtx pat, target;
8236 rtx *label;
8238 /* We need correct insn addresses. */
8240 shorten_branches (get_insns ());
8242 /* Find all branches that exceed 64KB, and split them. */
8244 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
8246 if (! JUMP_P (insn) || tablejump_p (insn, NULL, NULL))
8247 continue;
8249 pat = PATTERN (insn);
8250 if (GET_CODE (pat) == PARALLEL)
8251 pat = XVECEXP (pat, 0, 0);
8252 if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
8253 continue;
8255 if (GET_CODE (SET_SRC (pat)) == LABEL_REF)
8257 label = &SET_SRC (pat);
8259 else if (GET_CODE (SET_SRC (pat)) == IF_THEN_ELSE)
8261 if (GET_CODE (XEXP (SET_SRC (pat), 1)) == LABEL_REF)
8262 label = &XEXP (SET_SRC (pat), 1);
8263 else if (GET_CODE (XEXP (SET_SRC (pat), 2)) == LABEL_REF)
8264 label = &XEXP (SET_SRC (pat), 2);
8265 else
8266 continue;
8268 else
8269 continue;
8271 if (get_attr_length (insn) <= 4)
8272 continue;
8274 /* We are going to use the return register as scratch register,
8275 make sure it will be saved/restored by the prologue/epilogue. */
8276 cfun_frame_layout.save_return_addr_p = 1;
8278 if (!flag_pic)
8280 new_literal = 1;
8281 rtx mem = force_const_mem (Pmode, *label);
8282 rtx_insn *set_insn = emit_insn_before (gen_rtx_SET (temp_reg, mem),
8283 insn);
8284 INSN_ADDRESSES_NEW (set_insn, -1);
8285 annotate_constant_pool_refs (&PATTERN (set_insn));
8287 target = temp_reg;
8289 else
8291 new_literal = 1;
8292 target = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, *label),
8293 UNSPEC_LTREL_OFFSET);
8294 target = gen_rtx_CONST (Pmode, target);
8295 target = force_const_mem (Pmode, target);
8296 rtx_insn *set_insn = emit_insn_before (gen_rtx_SET (temp_reg, target),
8297 insn);
8298 INSN_ADDRESSES_NEW (set_insn, -1);
8299 annotate_constant_pool_refs (&PATTERN (set_insn));
8301 target = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, XEXP (target, 0),
8302 cfun->machine->base_reg),
8303 UNSPEC_LTREL_BASE);
8304 target = gen_rtx_PLUS (Pmode, temp_reg, target);
8307 ret = validate_change (insn, label, target, 0);
8308 gcc_assert (ret);
8311 return new_literal;
8315 /* Find an annotated literal pool symbol referenced in RTX X,
8316 and store it at REF. Will abort if X contains references to
8317 more than one such pool symbol; multiple references to the same
8318 symbol are allowed, however.
8320 The rtx pointed to by REF must be initialized to NULL_RTX
8321 by the caller before calling this routine. */
8323 static void
8324 find_constant_pool_ref (rtx x, rtx *ref)
8326 int i, j;
8327 const char *fmt;
8329 /* Ignore LTREL_BASE references. */
8330 if (GET_CODE (x) == UNSPEC
8331 && XINT (x, 1) == UNSPEC_LTREL_BASE)
8332 return;
8333 /* Likewise POOL_ENTRY insns. */
8334 if (GET_CODE (x) == UNSPEC_VOLATILE
8335 && XINT (x, 1) == UNSPECV_POOL_ENTRY)
8336 return;
8338 gcc_assert (GET_CODE (x) != SYMBOL_REF
8339 || !CONSTANT_POOL_ADDRESS_P (x));
8341 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_LTREF)
8343 rtx sym = XVECEXP (x, 0, 0);
8344 gcc_assert (GET_CODE (sym) == SYMBOL_REF
8345 && CONSTANT_POOL_ADDRESS_P (sym));
8347 if (*ref == NULL_RTX)
8348 *ref = sym;
8349 else
8350 gcc_assert (*ref == sym);
8352 return;
8355 fmt = GET_RTX_FORMAT (GET_CODE (x));
8356 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
8358 if (fmt[i] == 'e')
8360 find_constant_pool_ref (XEXP (x, i), ref);
8362 else if (fmt[i] == 'E')
8364 for (j = 0; j < XVECLEN (x, i); j++)
8365 find_constant_pool_ref (XVECEXP (x, i, j), ref);
8370 /* Replace every reference to the annotated literal pool
8371 symbol REF in X by its base plus OFFSET. */
8373 static void
8374 replace_constant_pool_ref (rtx *x, rtx ref, rtx offset)
8376 int i, j;
8377 const char *fmt;
8379 gcc_assert (*x != ref);
8381 if (GET_CODE (*x) == UNSPEC
8382 && XINT (*x, 1) == UNSPEC_LTREF
8383 && XVECEXP (*x, 0, 0) == ref)
8385 *x = gen_rtx_PLUS (Pmode, XVECEXP (*x, 0, 1), offset);
8386 return;
8389 if (GET_CODE (*x) == PLUS
8390 && GET_CODE (XEXP (*x, 1)) == CONST_INT
8391 && GET_CODE (XEXP (*x, 0)) == UNSPEC
8392 && XINT (XEXP (*x, 0), 1) == UNSPEC_LTREF
8393 && XVECEXP (XEXP (*x, 0), 0, 0) == ref)
8395 rtx addr = gen_rtx_PLUS (Pmode, XVECEXP (XEXP (*x, 0), 0, 1), offset);
8396 *x = plus_constant (Pmode, addr, INTVAL (XEXP (*x, 1)));
8397 return;
8400 fmt = GET_RTX_FORMAT (GET_CODE (*x));
8401 for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
8403 if (fmt[i] == 'e')
8405 replace_constant_pool_ref (&XEXP (*x, i), ref, offset);
8407 else if (fmt[i] == 'E')
8409 for (j = 0; j < XVECLEN (*x, i); j++)
8410 replace_constant_pool_ref (&XVECEXP (*x, i, j), ref, offset);
8415 /* Check whether X contains an UNSPEC_LTREL_BASE.
8416 Return its constant pool symbol if found, NULL_RTX otherwise. */
8418 static rtx
8419 find_ltrel_base (rtx x)
8421 int i, j;
8422 const char *fmt;
8424 if (GET_CODE (x) == UNSPEC
8425 && XINT (x, 1) == UNSPEC_LTREL_BASE)
8426 return XVECEXP (x, 0, 0);
8428 fmt = GET_RTX_FORMAT (GET_CODE (x));
8429 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
8431 if (fmt[i] == 'e')
8433 rtx fnd = find_ltrel_base (XEXP (x, i));
8434 if (fnd)
8435 return fnd;
8437 else if (fmt[i] == 'E')
8439 for (j = 0; j < XVECLEN (x, i); j++)
8441 rtx fnd = find_ltrel_base (XVECEXP (x, i, j));
8442 if (fnd)
8443 return fnd;
8448 return NULL_RTX;
8451 /* Replace any occurrence of UNSPEC_LTREL_BASE in X with its base. */
8453 static void
8454 replace_ltrel_base (rtx *x)
8456 int i, j;
8457 const char *fmt;
8459 if (GET_CODE (*x) == UNSPEC
8460 && XINT (*x, 1) == UNSPEC_LTREL_BASE)
8462 *x = XVECEXP (*x, 0, 1);
8463 return;
8466 fmt = GET_RTX_FORMAT (GET_CODE (*x));
8467 for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
8469 if (fmt[i] == 'e')
8471 replace_ltrel_base (&XEXP (*x, i));
8473 else if (fmt[i] == 'E')
8475 for (j = 0; j < XVECLEN (*x, i); j++)
8476 replace_ltrel_base (&XVECEXP (*x, i, j));
8482 /* We keep a list of constants which we have to add to internal
8483 constant tables in the middle of large functions. */
8485 #define NR_C_MODES 32
8486 machine_mode constant_modes[NR_C_MODES] =
8488 TFmode, TImode, TDmode,
8489 V16QImode, V8HImode, V4SImode, V2DImode, V1TImode,
8490 V4SFmode, V2DFmode, V1TFmode,
8491 DFmode, DImode, DDmode,
8492 V8QImode, V4HImode, V2SImode, V1DImode, V2SFmode, V1DFmode,
8493 SFmode, SImode, SDmode,
8494 V4QImode, V2HImode, V1SImode, V1SFmode,
8495 HImode,
8496 V2QImode, V1HImode,
8497 QImode,
8498 V1QImode
8501 struct constant
8503 struct constant *next;
8504 rtx value;
8505 rtx_code_label *label;
8508 struct constant_pool
8510 struct constant_pool *next;
8511 rtx_insn *first_insn;
8512 rtx_insn *pool_insn;
8513 bitmap insns;
8514 rtx_insn *emit_pool_after;
8516 struct constant *constants[NR_C_MODES];
8517 struct constant *execute;
8518 rtx_code_label *label;
8519 int size;
8522 /* Allocate new constant_pool structure. */
8524 static struct constant_pool *
8525 s390_alloc_pool (void)
8527 struct constant_pool *pool;
8528 int i;
8530 pool = (struct constant_pool *) xmalloc (sizeof *pool);
8531 pool->next = NULL;
8532 for (i = 0; i < NR_C_MODES; i++)
8533 pool->constants[i] = NULL;
8535 pool->execute = NULL;
8536 pool->label = gen_label_rtx ();
8537 pool->first_insn = NULL;
8538 pool->pool_insn = NULL;
8539 pool->insns = BITMAP_ALLOC (NULL);
8540 pool->size = 0;
8541 pool->emit_pool_after = NULL;
8543 return pool;
8546 /* Create new constant pool covering instructions starting at INSN
8547 and chain it to the end of POOL_LIST. */
8549 static struct constant_pool *
8550 s390_start_pool (struct constant_pool **pool_list, rtx_insn *insn)
8552 struct constant_pool *pool, **prev;
8554 pool = s390_alloc_pool ();
8555 pool->first_insn = insn;
8557 for (prev = pool_list; *prev; prev = &(*prev)->next)
8559 *prev = pool;
8561 return pool;
8564 /* End range of instructions covered by POOL at INSN and emit
8565 placeholder insn representing the pool. */
8567 static void
8568 s390_end_pool (struct constant_pool *pool, rtx_insn *insn)
8570 rtx pool_size = GEN_INT (pool->size + 8 /* alignment slop */);
8572 if (!insn)
8573 insn = get_last_insn ();
8575 pool->pool_insn = emit_insn_after (gen_pool (pool_size), insn);
8576 INSN_ADDRESSES_NEW (pool->pool_insn, -1);
8579 /* Add INSN to the list of insns covered by POOL. */
8581 static void
8582 s390_add_pool_insn (struct constant_pool *pool, rtx insn)
8584 bitmap_set_bit (pool->insns, INSN_UID (insn));
8587 /* Return pool out of POOL_LIST that covers INSN. */
8589 static struct constant_pool *
8590 s390_find_pool (struct constant_pool *pool_list, rtx insn)
8592 struct constant_pool *pool;
8594 for (pool = pool_list; pool; pool = pool->next)
8595 if (bitmap_bit_p (pool->insns, INSN_UID (insn)))
8596 break;
8598 return pool;
8601 /* Add constant VAL of mode MODE to the constant pool POOL. */
8603 static void
8604 s390_add_constant (struct constant_pool *pool, rtx val, machine_mode mode)
8606 struct constant *c;
8607 int i;
8609 for (i = 0; i < NR_C_MODES; i++)
8610 if (constant_modes[i] == mode)
8611 break;
8612 gcc_assert (i != NR_C_MODES);
8614 for (c = pool->constants[i]; c != NULL; c = c->next)
8615 if (rtx_equal_p (val, c->value))
8616 break;
8618 if (c == NULL)
8620 c = (struct constant *) xmalloc (sizeof *c);
8621 c->value = val;
8622 c->label = gen_label_rtx ();
8623 c->next = pool->constants[i];
8624 pool->constants[i] = c;
8625 pool->size += GET_MODE_SIZE (mode);
8629 /* Return an rtx that represents the offset of X from the start of
8630 pool POOL. */
8632 static rtx
8633 s390_pool_offset (struct constant_pool *pool, rtx x)
8635 rtx label;
8637 label = gen_rtx_LABEL_REF (GET_MODE (x), pool->label);
8638 x = gen_rtx_UNSPEC (GET_MODE (x), gen_rtvec (2, x, label),
8639 UNSPEC_POOL_OFFSET);
8640 return gen_rtx_CONST (GET_MODE (x), x);
8643 /* Find constant VAL of mode MODE in the constant pool POOL.
8644 Return an RTX describing the distance from the start of
8645 the pool to the location of the new constant. */
8647 static rtx
8648 s390_find_constant (struct constant_pool *pool, rtx val,
8649 machine_mode mode)
8651 struct constant *c;
8652 int i;
8654 for (i = 0; i < NR_C_MODES; i++)
8655 if (constant_modes[i] == mode)
8656 break;
8657 gcc_assert (i != NR_C_MODES);
8659 for (c = pool->constants[i]; c != NULL; c = c->next)
8660 if (rtx_equal_p (val, c->value))
8661 break;
8663 gcc_assert (c);
8665 return s390_pool_offset (pool, gen_rtx_LABEL_REF (Pmode, c->label));
8668 /* Check whether INSN is an execute. Return the label_ref to its
8669 execute target template if so, NULL_RTX otherwise. */
8671 static rtx
8672 s390_execute_label (rtx insn)
8674 if (NONJUMP_INSN_P (insn)
8675 && GET_CODE (PATTERN (insn)) == PARALLEL
8676 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == UNSPEC
8677 && XINT (XVECEXP (PATTERN (insn), 0, 0), 1) == UNSPEC_EXECUTE)
8678 return XVECEXP (XVECEXP (PATTERN (insn), 0, 0), 0, 2);
8680 return NULL_RTX;
8683 /* Add execute target for INSN to the constant pool POOL. */
8685 static void
8686 s390_add_execute (struct constant_pool *pool, rtx insn)
8688 struct constant *c;
8690 for (c = pool->execute; c != NULL; c = c->next)
8691 if (INSN_UID (insn) == INSN_UID (c->value))
8692 break;
8694 if (c == NULL)
8696 c = (struct constant *) xmalloc (sizeof *c);
8697 c->value = insn;
8698 c->label = gen_label_rtx ();
8699 c->next = pool->execute;
8700 pool->execute = c;
8701 pool->size += 6;
8705 /* Find execute target for INSN in the constant pool POOL.
8706 Return an RTX describing the distance from the start of
8707 the pool to the location of the execute target. */
8709 static rtx
8710 s390_find_execute (struct constant_pool *pool, rtx insn)
8712 struct constant *c;
8714 for (c = pool->execute; c != NULL; c = c->next)
8715 if (INSN_UID (insn) == INSN_UID (c->value))
8716 break;
8718 gcc_assert (c);
8720 return s390_pool_offset (pool, gen_rtx_LABEL_REF (Pmode, c->label));
8723 /* For an execute INSN, extract the execute target template. */
8725 static rtx
8726 s390_execute_target (rtx insn)
8728 rtx pattern = PATTERN (insn);
8729 gcc_assert (s390_execute_label (insn));
8731 if (XVECLEN (pattern, 0) == 2)
8733 pattern = copy_rtx (XVECEXP (pattern, 0, 1));
8735 else
8737 rtvec vec = rtvec_alloc (XVECLEN (pattern, 0) - 1);
8738 int i;
8740 for (i = 0; i < XVECLEN (pattern, 0) - 1; i++)
8741 RTVEC_ELT (vec, i) = copy_rtx (XVECEXP (pattern, 0, i + 1));
8743 pattern = gen_rtx_PARALLEL (VOIDmode, vec);
8746 return pattern;
8749 /* Indicate that INSN cannot be duplicated. This is the case for
8750 execute insns that carry a unique label. */
8752 static bool
8753 s390_cannot_copy_insn_p (rtx_insn *insn)
8755 rtx label = s390_execute_label (insn);
8756 return label && label != const0_rtx;
8759 /* Dump out the constants in POOL. If REMOTE_LABEL is true,
8760 do not emit the pool base label. */
8762 static void
8763 s390_dump_pool (struct constant_pool *pool, bool remote_label)
8765 struct constant *c;
8766 rtx_insn *insn = pool->pool_insn;
8767 int i;
8769 /* Switch to rodata section. */
8770 if (TARGET_CPU_ZARCH)
8772 insn = emit_insn_after (gen_pool_section_start (), insn);
8773 INSN_ADDRESSES_NEW (insn, -1);
8776 /* Ensure minimum pool alignment. */
8777 if (TARGET_CPU_ZARCH)
8778 insn = emit_insn_after (gen_pool_align (GEN_INT (8)), insn);
8779 else
8780 insn = emit_insn_after (gen_pool_align (GEN_INT (4)), insn);
8781 INSN_ADDRESSES_NEW (insn, -1);
8783 /* Emit pool base label. */
8784 if (!remote_label)
8786 insn = emit_label_after (pool->label, insn);
8787 INSN_ADDRESSES_NEW (insn, -1);
8790 /* Dump constants in descending alignment requirement order,
8791 ensuring proper alignment for every constant. */
8792 for (i = 0; i < NR_C_MODES; i++)
8793 for (c = pool->constants[i]; c; c = c->next)
8795 /* Convert UNSPEC_LTREL_OFFSET unspecs to pool-relative references. */
8796 rtx value = copy_rtx (c->value);
8797 if (GET_CODE (value) == CONST
8798 && GET_CODE (XEXP (value, 0)) == UNSPEC
8799 && XINT (XEXP (value, 0), 1) == UNSPEC_LTREL_OFFSET
8800 && XVECLEN (XEXP (value, 0), 0) == 1)
8801 value = s390_pool_offset (pool, XVECEXP (XEXP (value, 0), 0, 0));
8803 insn = emit_label_after (c->label, insn);
8804 INSN_ADDRESSES_NEW (insn, -1);
8806 value = gen_rtx_UNSPEC_VOLATILE (constant_modes[i],
8807 gen_rtvec (1, value),
8808 UNSPECV_POOL_ENTRY);
8809 insn = emit_insn_after (value, insn);
8810 INSN_ADDRESSES_NEW (insn, -1);
8813 /* Ensure minimum alignment for instructions. */
8814 insn = emit_insn_after (gen_pool_align (GEN_INT (2)), insn);
8815 INSN_ADDRESSES_NEW (insn, -1);
8817 /* Output in-pool execute template insns. */
8818 for (c = pool->execute; c; c = c->next)
8820 insn = emit_label_after (c->label, insn);
8821 INSN_ADDRESSES_NEW (insn, -1);
8823 insn = emit_insn_after (s390_execute_target (c->value), insn);
8824 INSN_ADDRESSES_NEW (insn, -1);
8827 /* Switch back to previous section. */
8828 if (TARGET_CPU_ZARCH)
8830 insn = emit_insn_after (gen_pool_section_end (), insn);
8831 INSN_ADDRESSES_NEW (insn, -1);
8834 insn = emit_barrier_after (insn);
8835 INSN_ADDRESSES_NEW (insn, -1);
8837 /* Remove placeholder insn. */
8838 remove_insn (pool->pool_insn);
8841 /* Free all memory used by POOL. */
8843 static void
8844 s390_free_pool (struct constant_pool *pool)
8846 struct constant *c, *next;
8847 int i;
8849 for (i = 0; i < NR_C_MODES; i++)
8850 for (c = pool->constants[i]; c; c = next)
8852 next = c->next;
8853 free (c);
8856 for (c = pool->execute; c; c = next)
8858 next = c->next;
8859 free (c);
8862 BITMAP_FREE (pool->insns);
8863 free (pool);
8867 /* Collect main literal pool. Return NULL on overflow. */
8869 static struct constant_pool *
8870 s390_mainpool_start (void)
8872 struct constant_pool *pool;
8873 rtx_insn *insn;
8875 pool = s390_alloc_pool ();
8877 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
8879 if (NONJUMP_INSN_P (insn)
8880 && GET_CODE (PATTERN (insn)) == SET
8881 && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC_VOLATILE
8882 && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPECV_MAIN_POOL)
8884 /* There might be two main_pool instructions if base_reg
8885 is call-clobbered; one for shrink-wrapped code and one
8886 for the rest. We want to keep the first. */
8887 if (pool->pool_insn)
8889 insn = PREV_INSN (insn);
8890 delete_insn (NEXT_INSN (insn));
8891 continue;
8893 pool->pool_insn = insn;
8896 if (!TARGET_CPU_ZARCH && s390_execute_label (insn))
8898 s390_add_execute (pool, insn);
8900 else if (NONJUMP_INSN_P (insn) || CALL_P (insn))
8902 rtx pool_ref = NULL_RTX;
8903 find_constant_pool_ref (PATTERN (insn), &pool_ref);
8904 if (pool_ref)
8906 rtx constant = get_pool_constant (pool_ref);
8907 machine_mode mode = get_pool_mode (pool_ref);
8908 s390_add_constant (pool, constant, mode);
8912 /* If hot/cold partitioning is enabled we have to make sure that
8913 the literal pool is emitted in the same section where the
8914 initialization of the literal pool base pointer takes place.
8915 emit_pool_after is only used in the non-overflow case on non
8916 Z cpus where we can emit the literal pool at the end of the
8917 function body within the text section. */
8918 if (NOTE_P (insn)
8919 && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS
8920 && !pool->emit_pool_after)
8921 pool->emit_pool_after = PREV_INSN (insn);
8924 gcc_assert (pool->pool_insn || pool->size == 0);
8926 if (pool->size >= 4096)
8928 /* We're going to chunkify the pool, so remove the main
8929 pool placeholder insn. */
8930 remove_insn (pool->pool_insn);
8932 s390_free_pool (pool);
8933 pool = NULL;
8936 /* If the functions ends with the section where the literal pool
8937 should be emitted set the marker to its end. */
8938 if (pool && !pool->emit_pool_after)
8939 pool->emit_pool_after = get_last_insn ();
8941 return pool;
8944 /* POOL holds the main literal pool as collected by s390_mainpool_start.
8945 Modify the current function to output the pool constants as well as
8946 the pool register setup instruction. */
8948 static void
8949 s390_mainpool_finish (struct constant_pool *pool)
8951 rtx base_reg = cfun->machine->base_reg;
8953 /* If the pool is empty, we're done. */
8954 if (pool->size == 0)
8956 /* We don't actually need a base register after all. */
8957 cfun->machine->base_reg = NULL_RTX;
8959 if (pool->pool_insn)
8960 remove_insn (pool->pool_insn);
8961 s390_free_pool (pool);
8962 return;
8965 /* We need correct insn addresses. */
8966 shorten_branches (get_insns ());
8968 /* On zSeries, we use a LARL to load the pool register. The pool is
8969 located in the .rodata section, so we emit it after the function. */
8970 if (TARGET_CPU_ZARCH)
8972 rtx set = gen_main_base_64 (base_reg, pool->label);
8973 rtx_insn *insn = emit_insn_after (set, pool->pool_insn);
8974 INSN_ADDRESSES_NEW (insn, -1);
8975 remove_insn (pool->pool_insn);
8977 insn = get_last_insn ();
8978 pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
8979 INSN_ADDRESSES_NEW (pool->pool_insn, -1);
8981 s390_dump_pool (pool, 0);
8984 /* On S/390, if the total size of the function's code plus literal pool
8985 does not exceed 4096 bytes, we use BASR to set up a function base
8986 pointer, and emit the literal pool at the end of the function. */
8987 else if (INSN_ADDRESSES (INSN_UID (pool->emit_pool_after))
8988 + pool->size + 8 /* alignment slop */ < 4096)
8990 rtx set = gen_main_base_31_small (base_reg, pool->label);
8991 rtx_insn *insn = emit_insn_after (set, pool->pool_insn);
8992 INSN_ADDRESSES_NEW (insn, -1);
8993 remove_insn (pool->pool_insn);
8995 insn = emit_label_after (pool->label, insn);
8996 INSN_ADDRESSES_NEW (insn, -1);
8998 /* emit_pool_after will be set by s390_mainpool_start to the
8999 last insn of the section where the literal pool should be
9000 emitted. */
9001 insn = pool->emit_pool_after;
9003 pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
9004 INSN_ADDRESSES_NEW (pool->pool_insn, -1);
9006 s390_dump_pool (pool, 1);
9009 /* Otherwise, we emit an inline literal pool and use BASR to branch
9010 over it, setting up the pool register at the same time. */
9011 else
9013 rtx_code_label *pool_end = gen_label_rtx ();
9015 rtx pat = gen_main_base_31_large (base_reg, pool->label, pool_end);
9016 rtx_insn *insn = emit_jump_insn_after (pat, pool->pool_insn);
9017 JUMP_LABEL (insn) = pool_end;
9018 INSN_ADDRESSES_NEW (insn, -1);
9019 remove_insn (pool->pool_insn);
9021 insn = emit_label_after (pool->label, insn);
9022 INSN_ADDRESSES_NEW (insn, -1);
9024 pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
9025 INSN_ADDRESSES_NEW (pool->pool_insn, -1);
9027 insn = emit_label_after (pool_end, pool->pool_insn);
9028 INSN_ADDRESSES_NEW (insn, -1);
9030 s390_dump_pool (pool, 1);
9034 /* Replace all literal pool references. */
9036 for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
9038 if (INSN_P (insn))
9039 replace_ltrel_base (&PATTERN (insn));
9041 if (NONJUMP_INSN_P (insn) || CALL_P (insn))
9043 rtx addr, pool_ref = NULL_RTX;
9044 find_constant_pool_ref (PATTERN (insn), &pool_ref);
9045 if (pool_ref)
9047 if (s390_execute_label (insn))
9048 addr = s390_find_execute (pool, insn);
9049 else
9050 addr = s390_find_constant (pool, get_pool_constant (pool_ref),
9051 get_pool_mode (pool_ref));
9053 replace_constant_pool_ref (&PATTERN (insn), pool_ref, addr);
9054 INSN_CODE (insn) = -1;
9060 /* Free the pool. */
9061 s390_free_pool (pool);
9064 /* POOL holds the main literal pool as collected by s390_mainpool_start.
9065 We have decided we cannot use this pool, so revert all changes
9066 to the current function that were done by s390_mainpool_start. */
9067 static void
9068 s390_mainpool_cancel (struct constant_pool *pool)
9070 /* We didn't actually change the instruction stream, so simply
9071 free the pool memory. */
9072 s390_free_pool (pool);
9076 /* Chunkify the literal pool. */
9078 #define S390_POOL_CHUNK_MIN 0xc00
9079 #define S390_POOL_CHUNK_MAX 0xe00
9081 static struct constant_pool *
9082 s390_chunkify_start (void)
9084 struct constant_pool *curr_pool = NULL, *pool_list = NULL;
9085 int extra_size = 0;
9086 bitmap far_labels;
9087 rtx pending_ltrel = NULL_RTX;
9088 rtx_insn *insn;
9090 rtx (*gen_reload_base) (rtx, rtx) =
9091 TARGET_CPU_ZARCH? gen_reload_base_64 : gen_reload_base_31;
9094 /* We need correct insn addresses. */
9096 shorten_branches (get_insns ());
9098 /* Scan all insns and move literals to pool chunks. */
9100 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9102 bool section_switch_p = false;
9104 /* Check for pending LTREL_BASE. */
9105 if (INSN_P (insn))
9107 rtx ltrel_base = find_ltrel_base (PATTERN (insn));
9108 if (ltrel_base)
9110 gcc_assert (ltrel_base == pending_ltrel);
9111 pending_ltrel = NULL_RTX;
9115 if (!TARGET_CPU_ZARCH && s390_execute_label (insn))
9117 if (!curr_pool)
9118 curr_pool = s390_start_pool (&pool_list, insn);
9120 s390_add_execute (curr_pool, insn);
9121 s390_add_pool_insn (curr_pool, insn);
9123 else if (NONJUMP_INSN_P (insn) || CALL_P (insn))
9125 rtx pool_ref = NULL_RTX;
9126 find_constant_pool_ref (PATTERN (insn), &pool_ref);
9127 if (pool_ref)
9129 rtx constant = get_pool_constant (pool_ref);
9130 machine_mode mode = get_pool_mode (pool_ref);
9132 if (!curr_pool)
9133 curr_pool = s390_start_pool (&pool_list, insn);
9135 s390_add_constant (curr_pool, constant, mode);
9136 s390_add_pool_insn (curr_pool, insn);
9138 /* Don't split the pool chunk between a LTREL_OFFSET load
9139 and the corresponding LTREL_BASE. */
9140 if (GET_CODE (constant) == CONST
9141 && GET_CODE (XEXP (constant, 0)) == UNSPEC
9142 && XINT (XEXP (constant, 0), 1) == UNSPEC_LTREL_OFFSET)
9144 gcc_assert (!pending_ltrel);
9145 pending_ltrel = pool_ref;
9150 if (JUMP_P (insn) || JUMP_TABLE_DATA_P (insn) || LABEL_P (insn))
9152 if (curr_pool)
9153 s390_add_pool_insn (curr_pool, insn);
9154 /* An LTREL_BASE must follow within the same basic block. */
9155 gcc_assert (!pending_ltrel);
9158 if (NOTE_P (insn))
9159 switch (NOTE_KIND (insn))
9161 case NOTE_INSN_SWITCH_TEXT_SECTIONS:
9162 section_switch_p = true;
9163 break;
9164 case NOTE_INSN_VAR_LOCATION:
9165 case NOTE_INSN_CALL_ARG_LOCATION:
9166 continue;
9167 default:
9168 break;
9171 if (!curr_pool
9172 || INSN_ADDRESSES_SIZE () <= (size_t) INSN_UID (insn)
9173 || INSN_ADDRESSES (INSN_UID (insn)) == -1)
9174 continue;
9176 if (TARGET_CPU_ZARCH)
9178 if (curr_pool->size < S390_POOL_CHUNK_MAX)
9179 continue;
9181 s390_end_pool (curr_pool, NULL);
9182 curr_pool = NULL;
9184 else
9186 int chunk_size = INSN_ADDRESSES (INSN_UID (insn))
9187 - INSN_ADDRESSES (INSN_UID (curr_pool->first_insn))
9188 + extra_size;
9190 /* We will later have to insert base register reload insns.
9191 Those will have an effect on code size, which we need to
9192 consider here. This calculation makes rather pessimistic
9193 worst-case assumptions. */
9194 if (LABEL_P (insn))
9195 extra_size += 6;
9197 if (chunk_size < S390_POOL_CHUNK_MIN
9198 && curr_pool->size < S390_POOL_CHUNK_MIN
9199 && !section_switch_p)
9200 continue;
9202 /* Pool chunks can only be inserted after BARRIERs ... */
9203 if (BARRIER_P (insn))
9205 s390_end_pool (curr_pool, insn);
9206 curr_pool = NULL;
9207 extra_size = 0;
9210 /* ... so if we don't find one in time, create one. */
9211 else if (chunk_size > S390_POOL_CHUNK_MAX
9212 || curr_pool->size > S390_POOL_CHUNK_MAX
9213 || section_switch_p)
9215 rtx_insn *label, *jump, *barrier, *next, *prev;
9217 if (!section_switch_p)
9219 /* We can insert the barrier only after a 'real' insn. */
9220 if (! NONJUMP_INSN_P (insn) && ! CALL_P (insn))
9221 continue;
9222 if (get_attr_length (insn) == 0)
9223 continue;
9224 /* Don't separate LTREL_BASE from the corresponding
9225 LTREL_OFFSET load. */
9226 if (pending_ltrel)
9227 continue;
9228 next = insn;
9231 insn = next;
9232 next = NEXT_INSN (insn);
9234 while (next
9235 && NOTE_P (next)
9236 && (NOTE_KIND (next) == NOTE_INSN_VAR_LOCATION
9237 || NOTE_KIND (next) == NOTE_INSN_CALL_ARG_LOCATION));
9239 else
9241 gcc_assert (!pending_ltrel);
9243 /* The old pool has to end before the section switch
9244 note in order to make it part of the current
9245 section. */
9246 insn = PREV_INSN (insn);
9249 label = gen_label_rtx ();
9250 prev = insn;
9251 if (prev && NOTE_P (prev))
9252 prev = prev_nonnote_insn (prev);
9253 if (prev)
9254 jump = emit_jump_insn_after_setloc (gen_jump (label), insn,
9255 INSN_LOCATION (prev));
9256 else
9257 jump = emit_jump_insn_after_noloc (gen_jump (label), insn);
9258 barrier = emit_barrier_after (jump);
9259 insn = emit_label_after (label, barrier);
9260 JUMP_LABEL (jump) = label;
9261 LABEL_NUSES (label) = 1;
9263 INSN_ADDRESSES_NEW (jump, -1);
9264 INSN_ADDRESSES_NEW (barrier, -1);
9265 INSN_ADDRESSES_NEW (insn, -1);
9267 s390_end_pool (curr_pool, barrier);
9268 curr_pool = NULL;
9269 extra_size = 0;
9274 if (curr_pool)
9275 s390_end_pool (curr_pool, NULL);
9276 gcc_assert (!pending_ltrel);
9278 /* Find all labels that are branched into
9279 from an insn belonging to a different chunk. */
9281 far_labels = BITMAP_ALLOC (NULL);
9283 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9285 rtx_jump_table_data *table;
9287 /* Labels marked with LABEL_PRESERVE_P can be target
9288 of non-local jumps, so we have to mark them.
9289 The same holds for named labels.
9291 Don't do that, however, if it is the label before
9292 a jump table. */
9294 if (LABEL_P (insn)
9295 && (LABEL_PRESERVE_P (insn) || LABEL_NAME (insn)))
9297 rtx_insn *vec_insn = NEXT_INSN (insn);
9298 if (! vec_insn || ! JUMP_TABLE_DATA_P (vec_insn))
9299 bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (insn));
9301 /* Check potential targets in a table jump (casesi_jump). */
9302 else if (tablejump_p (insn, NULL, &table))
9304 rtx vec_pat = PATTERN (table);
9305 int i, diff_p = GET_CODE (vec_pat) == ADDR_DIFF_VEC;
9307 for (i = 0; i < XVECLEN (vec_pat, diff_p); i++)
9309 rtx label = XEXP (XVECEXP (vec_pat, diff_p, i), 0);
9311 if (s390_find_pool (pool_list, label)
9312 != s390_find_pool (pool_list, insn))
9313 bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (label));
9316 /* If we have a direct jump (conditional or unconditional),
9317 check all potential targets. */
9318 else if (JUMP_P (insn))
9320 rtx pat = PATTERN (insn);
9322 if (GET_CODE (pat) == PARALLEL)
9323 pat = XVECEXP (pat, 0, 0);
9325 if (GET_CODE (pat) == SET)
9327 rtx label = JUMP_LABEL (insn);
9328 if (label && !ANY_RETURN_P (label))
9330 if (s390_find_pool (pool_list, label)
9331 != s390_find_pool (pool_list, insn))
9332 bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (label));
9338 /* Insert base register reload insns before every pool. */
9340 for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
9342 rtx new_insn = gen_reload_base (cfun->machine->base_reg,
9343 curr_pool->label);
9344 rtx_insn *insn = curr_pool->first_insn;
9345 INSN_ADDRESSES_NEW (emit_insn_before (new_insn, insn), -1);
9348 /* Insert base register reload insns at every far label. */
9350 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9351 if (LABEL_P (insn)
9352 && bitmap_bit_p (far_labels, CODE_LABEL_NUMBER (insn)))
9354 struct constant_pool *pool = s390_find_pool (pool_list, insn);
9355 if (pool)
9357 rtx new_insn = gen_reload_base (cfun->machine->base_reg,
9358 pool->label);
9359 INSN_ADDRESSES_NEW (emit_insn_after (new_insn, insn), -1);
9364 BITMAP_FREE (far_labels);
9367 /* Recompute insn addresses. */
9369 init_insn_lengths ();
9370 shorten_branches (get_insns ());
9372 return pool_list;
9375 /* POOL_LIST is a chunk list as prepared by s390_chunkify_start.
9376 After we have decided to use this list, finish implementing
9377 all changes to the current function as required. */
9379 static void
9380 s390_chunkify_finish (struct constant_pool *pool_list)
9382 struct constant_pool *curr_pool = NULL;
9383 rtx_insn *insn;
9386 /* Replace all literal pool references. */
9388 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9390 if (INSN_P (insn))
9391 replace_ltrel_base (&PATTERN (insn));
9393 curr_pool = s390_find_pool (pool_list, insn);
9394 if (!curr_pool)
9395 continue;
9397 if (NONJUMP_INSN_P (insn) || CALL_P (insn))
9399 rtx addr, pool_ref = NULL_RTX;
9400 find_constant_pool_ref (PATTERN (insn), &pool_ref);
9401 if (pool_ref)
9403 if (s390_execute_label (insn))
9404 addr = s390_find_execute (curr_pool, insn);
9405 else
9406 addr = s390_find_constant (curr_pool,
9407 get_pool_constant (pool_ref),
9408 get_pool_mode (pool_ref));
9410 replace_constant_pool_ref (&PATTERN (insn), pool_ref, addr);
9411 INSN_CODE (insn) = -1;
9416 /* Dump out all literal pools. */
9418 for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
9419 s390_dump_pool (curr_pool, 0);
9421 /* Free pool list. */
9423 while (pool_list)
9425 struct constant_pool *next = pool_list->next;
9426 s390_free_pool (pool_list);
9427 pool_list = next;
9431 /* POOL_LIST is a chunk list as prepared by s390_chunkify_start.
9432 We have decided we cannot use this list, so revert all changes
9433 to the current function that were done by s390_chunkify_start. */
9435 static void
9436 s390_chunkify_cancel (struct constant_pool *pool_list)
9438 struct constant_pool *curr_pool = NULL;
9439 rtx_insn *insn;
9441 /* Remove all pool placeholder insns. */
9443 for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
9445 /* Did we insert an extra barrier? Remove it. */
9446 rtx_insn *barrier = PREV_INSN (curr_pool->pool_insn);
9447 rtx_insn *jump = barrier? PREV_INSN (barrier) : NULL;
9448 rtx_insn *label = NEXT_INSN (curr_pool->pool_insn);
9450 if (jump && JUMP_P (jump)
9451 && barrier && BARRIER_P (barrier)
9452 && label && LABEL_P (label)
9453 && GET_CODE (PATTERN (jump)) == SET
9454 && SET_DEST (PATTERN (jump)) == pc_rtx
9455 && GET_CODE (SET_SRC (PATTERN (jump))) == LABEL_REF
9456 && XEXP (SET_SRC (PATTERN (jump)), 0) == label)
9458 remove_insn (jump);
9459 remove_insn (barrier);
9460 remove_insn (label);
9463 remove_insn (curr_pool->pool_insn);
9466 /* Remove all base register reload insns. */
9468 for (insn = get_insns (); insn; )
9470 rtx_insn *next_insn = NEXT_INSN (insn);
9472 if (NONJUMP_INSN_P (insn)
9473 && GET_CODE (PATTERN (insn)) == SET
9474 && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC
9475 && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPEC_RELOAD_BASE)
9476 remove_insn (insn);
9478 insn = next_insn;
9481 /* Free pool list. */
9483 while (pool_list)
9485 struct constant_pool *next = pool_list->next;
9486 s390_free_pool (pool_list);
9487 pool_list = next;
9491 /* Output the constant pool entry EXP in mode MODE with alignment ALIGN. */
9493 void
9494 s390_output_pool_entry (rtx exp, machine_mode mode, unsigned int align)
9496 switch (GET_MODE_CLASS (mode))
9498 case MODE_FLOAT:
9499 case MODE_DECIMAL_FLOAT:
9500 gcc_assert (GET_CODE (exp) == CONST_DOUBLE);
9502 assemble_real (*CONST_DOUBLE_REAL_VALUE (exp),
9503 as_a <scalar_float_mode> (mode), align);
9504 break;
9506 case MODE_INT:
9507 assemble_integer (exp, GET_MODE_SIZE (mode), align, 1);
9508 mark_symbol_refs_as_used (exp);
9509 break;
9511 case MODE_VECTOR_INT:
9512 case MODE_VECTOR_FLOAT:
9514 int i;
9515 machine_mode inner_mode;
9516 gcc_assert (GET_CODE (exp) == CONST_VECTOR);
9518 inner_mode = GET_MODE_INNER (GET_MODE (exp));
9519 for (i = 0; i < XVECLEN (exp, 0); i++)
9520 s390_output_pool_entry (XVECEXP (exp, 0, i),
9521 inner_mode,
9522 i == 0
9523 ? align
9524 : GET_MODE_BITSIZE (inner_mode));
9526 break;
9528 default:
9529 gcc_unreachable ();
9534 /* Return an RTL expression representing the value of the return address
9535 for the frame COUNT steps up from the current frame. FRAME is the
9536 frame pointer of that frame. */
9539 s390_return_addr_rtx (int count, rtx frame ATTRIBUTE_UNUSED)
9541 int offset;
9542 rtx addr;
9544 /* Without backchain, we fail for all but the current frame. */
9546 if (!TARGET_BACKCHAIN && count > 0)
9547 return NULL_RTX;
9549 /* For the current frame, we need to make sure the initial
9550 value of RETURN_REGNUM is actually saved. */
9552 if (count == 0)
9554 /* On non-z architectures branch splitting could overwrite r14. */
9555 if (TARGET_CPU_ZARCH)
9556 return get_hard_reg_initial_val (Pmode, RETURN_REGNUM);
9557 else
9559 cfun_frame_layout.save_return_addr_p = true;
9560 return gen_rtx_MEM (Pmode, return_address_pointer_rtx);
9564 if (TARGET_PACKED_STACK)
9565 offset = -2 * UNITS_PER_LONG;
9566 else
9567 offset = RETURN_REGNUM * UNITS_PER_LONG;
9569 addr = plus_constant (Pmode, frame, offset);
9570 addr = memory_address (Pmode, addr);
9571 return gen_rtx_MEM (Pmode, addr);
9574 /* Return an RTL expression representing the back chain stored in
9575 the current stack frame. */
9578 s390_back_chain_rtx (void)
9580 rtx chain;
9582 gcc_assert (TARGET_BACKCHAIN);
9584 if (TARGET_PACKED_STACK)
9585 chain = plus_constant (Pmode, stack_pointer_rtx,
9586 STACK_POINTER_OFFSET - UNITS_PER_LONG);
9587 else
9588 chain = stack_pointer_rtx;
9590 chain = gen_rtx_MEM (Pmode, chain);
9591 return chain;
9594 /* Find first call clobbered register unused in a function.
9595 This could be used as base register in a leaf function
9596 or for holding the return address before epilogue. */
9598 static int
9599 find_unused_clobbered_reg (void)
9601 int i;
9602 for (i = 0; i < 6; i++)
9603 if (!df_regs_ever_live_p (i))
9604 return i;
9605 return 0;
9609 /* Helper function for s390_regs_ever_clobbered. Sets the fields in DATA for all
9610 clobbered hard regs in SETREG. */
9612 static void
9613 s390_reg_clobbered_rtx (rtx setreg, const_rtx set_insn ATTRIBUTE_UNUSED, void *data)
9615 char *regs_ever_clobbered = (char *)data;
9616 unsigned int i, regno;
9617 machine_mode mode = GET_MODE (setreg);
9619 if (GET_CODE (setreg) == SUBREG)
9621 rtx inner = SUBREG_REG (setreg);
9622 if (!GENERAL_REG_P (inner) && !FP_REG_P (inner))
9623 return;
9624 regno = subreg_regno (setreg);
9626 else if (GENERAL_REG_P (setreg) || FP_REG_P (setreg))
9627 regno = REGNO (setreg);
9628 else
9629 return;
9631 for (i = regno;
9632 i < regno + HARD_REGNO_NREGS (regno, mode);
9633 i++)
9634 regs_ever_clobbered[i] = 1;
9637 /* Walks through all basic blocks of the current function looking
9638 for clobbered hard regs using s390_reg_clobbered_rtx. The fields
9639 of the passed integer array REGS_EVER_CLOBBERED are set to one for
9640 each of those regs. */
9642 static void
9643 s390_regs_ever_clobbered (char regs_ever_clobbered[])
9645 basic_block cur_bb;
9646 rtx_insn *cur_insn;
9647 unsigned int i;
9649 memset (regs_ever_clobbered, 0, 32);
9651 /* For non-leaf functions we have to consider all call clobbered regs to be
9652 clobbered. */
9653 if (!crtl->is_leaf)
9655 for (i = 0; i < 32; i++)
9656 regs_ever_clobbered[i] = call_really_used_regs[i];
9659 /* Make the "magic" eh_return registers live if necessary. For regs_ever_live
9660 this work is done by liveness analysis (mark_regs_live_at_end).
9661 Special care is needed for functions containing landing pads. Landing pads
9662 may use the eh registers, but the code which sets these registers is not
9663 contained in that function. Hence s390_regs_ever_clobbered is not able to
9664 deal with this automatically. */
9665 if (crtl->calls_eh_return || cfun->machine->has_landing_pad_p)
9666 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM ; i++)
9667 if (crtl->calls_eh_return
9668 || (cfun->machine->has_landing_pad_p
9669 && df_regs_ever_live_p (EH_RETURN_DATA_REGNO (i))))
9670 regs_ever_clobbered[EH_RETURN_DATA_REGNO (i)] = 1;
9672 /* For nonlocal gotos all call-saved registers have to be saved.
9673 This flag is also set for the unwinding code in libgcc.
9674 See expand_builtin_unwind_init. For regs_ever_live this is done by
9675 reload. */
9676 if (crtl->saves_all_registers)
9677 for (i = 0; i < 32; i++)
9678 if (!call_really_used_regs[i])
9679 regs_ever_clobbered[i] = 1;
9681 FOR_EACH_BB_FN (cur_bb, cfun)
9683 FOR_BB_INSNS (cur_bb, cur_insn)
9685 rtx pat;
9687 if (!INSN_P (cur_insn))
9688 continue;
9690 pat = PATTERN (cur_insn);
9692 /* Ignore GPR restore insns. */
9693 if (epilogue_completed && RTX_FRAME_RELATED_P (cur_insn))
9695 if (GET_CODE (pat) == SET
9696 && GENERAL_REG_P (SET_DEST (pat)))
9698 /* lgdr */
9699 if (GET_MODE (SET_SRC (pat)) == DImode
9700 && FP_REG_P (SET_SRC (pat)))
9701 continue;
9703 /* l / lg */
9704 if (GET_CODE (SET_SRC (pat)) == MEM)
9705 continue;
9708 /* lm / lmg */
9709 if (GET_CODE (pat) == PARALLEL
9710 && load_multiple_operation (pat, VOIDmode))
9711 continue;
9714 note_stores (pat,
9715 s390_reg_clobbered_rtx,
9716 regs_ever_clobbered);
9721 /* Determine the frame area which actually has to be accessed
9722 in the function epilogue. The values are stored at the
9723 given pointers AREA_BOTTOM (address of the lowest used stack
9724 address) and AREA_TOP (address of the first item which does
9725 not belong to the stack frame). */
9727 static void
9728 s390_frame_area (int *area_bottom, int *area_top)
9730 int b, t;
9732 b = INT_MAX;
9733 t = INT_MIN;
9735 if (cfun_frame_layout.first_restore_gpr != -1)
9737 b = (cfun_frame_layout.gprs_offset
9738 + cfun_frame_layout.first_restore_gpr * UNITS_PER_LONG);
9739 t = b + (cfun_frame_layout.last_restore_gpr
9740 - cfun_frame_layout.first_restore_gpr + 1) * UNITS_PER_LONG;
9743 if (TARGET_64BIT && cfun_save_high_fprs_p)
9745 b = MIN (b, cfun_frame_layout.f8_offset);
9746 t = MAX (t, (cfun_frame_layout.f8_offset
9747 + cfun_frame_layout.high_fprs * 8));
9750 if (!TARGET_64BIT)
9752 if (cfun_fpr_save_p (FPR4_REGNUM))
9754 b = MIN (b, cfun_frame_layout.f4_offset);
9755 t = MAX (t, cfun_frame_layout.f4_offset + 8);
9757 if (cfun_fpr_save_p (FPR6_REGNUM))
9759 b = MIN (b, cfun_frame_layout.f4_offset + 8);
9760 t = MAX (t, cfun_frame_layout.f4_offset + 16);
9763 *area_bottom = b;
9764 *area_top = t;
9766 /* Update gpr_save_slots in the frame layout trying to make use of
9767 FPRs as GPR save slots.
9768 This is a helper routine of s390_register_info. */
9770 static void
9771 s390_register_info_gprtofpr ()
9773 int save_reg_slot = FPR0_REGNUM;
9774 int i, j;
9776 if (!TARGET_Z10 || !TARGET_HARD_FLOAT || !crtl->is_leaf)
9777 return;
9779 /* builtin_eh_return needs to be able to modify the return address
9780 on the stack. It could also adjust the FPR save slot instead but
9781 is it worth the trouble?! */
9782 if (crtl->calls_eh_return)
9783 return;
9785 for (i = 15; i >= 6; i--)
9787 if (cfun_gpr_save_slot (i) == SAVE_SLOT_NONE)
9788 continue;
9790 /* Advance to the next FP register which can be used as a
9791 GPR save slot. */
9792 while ((!call_really_used_regs[save_reg_slot]
9793 || df_regs_ever_live_p (save_reg_slot)
9794 || cfun_fpr_save_p (save_reg_slot))
9795 && FP_REGNO_P (save_reg_slot))
9796 save_reg_slot++;
9797 if (!FP_REGNO_P (save_reg_slot))
9799 /* We only want to use ldgr/lgdr if we can get rid of
9800 stm/lm entirely. So undo the gpr slot allocation in
9801 case we ran out of FPR save slots. */
9802 for (j = 6; j <= 15; j++)
9803 if (FP_REGNO_P (cfun_gpr_save_slot (j)))
9804 cfun_gpr_save_slot (j) = SAVE_SLOT_STACK;
9805 break;
9807 cfun_gpr_save_slot (i) = save_reg_slot++;
9811 /* Set the bits in fpr_bitmap for FPRs which need to be saved due to
9812 stdarg.
9813 This is a helper routine for s390_register_info. */
9815 static void
9816 s390_register_info_stdarg_fpr ()
9818 int i;
9819 int min_fpr;
9820 int max_fpr;
9822 /* Save the FP argument regs for stdarg. f0, f2 for 31 bit and
9823 f0-f4 for 64 bit. */
9824 if (!cfun->stdarg
9825 || !TARGET_HARD_FLOAT
9826 || !cfun->va_list_fpr_size
9827 || crtl->args.info.fprs >= FP_ARG_NUM_REG)
9828 return;
9830 min_fpr = crtl->args.info.fprs;
9831 max_fpr = min_fpr + cfun->va_list_fpr_size - 1;
9832 if (max_fpr >= FP_ARG_NUM_REG)
9833 max_fpr = FP_ARG_NUM_REG - 1;
9835 /* FPR argument regs start at f0. */
9836 min_fpr += FPR0_REGNUM;
9837 max_fpr += FPR0_REGNUM;
9839 for (i = min_fpr; i <= max_fpr; i++)
9840 cfun_set_fpr_save (i);
9843 /* Reserve the GPR save slots for GPRs which need to be saved due to
9844 stdarg.
9845 This is a helper routine for s390_register_info. */
9847 static void
9848 s390_register_info_stdarg_gpr ()
9850 int i;
9851 int min_gpr;
9852 int max_gpr;
9854 if (!cfun->stdarg
9855 || !cfun->va_list_gpr_size
9856 || crtl->args.info.gprs >= GP_ARG_NUM_REG)
9857 return;
9859 min_gpr = crtl->args.info.gprs;
9860 max_gpr = min_gpr + cfun->va_list_gpr_size - 1;
9861 if (max_gpr >= GP_ARG_NUM_REG)
9862 max_gpr = GP_ARG_NUM_REG - 1;
9864 /* GPR argument regs start at r2. */
9865 min_gpr += GPR2_REGNUM;
9866 max_gpr += GPR2_REGNUM;
9868 /* If r6 was supposed to be saved into an FPR and now needs to go to
9869 the stack for vararg we have to adjust the restore range to make
9870 sure that the restore is done from stack as well. */
9871 if (FP_REGNO_P (cfun_gpr_save_slot (GPR6_REGNUM))
9872 && min_gpr <= GPR6_REGNUM
9873 && max_gpr >= GPR6_REGNUM)
9875 if (cfun_frame_layout.first_restore_gpr == -1
9876 || cfun_frame_layout.first_restore_gpr > GPR6_REGNUM)
9877 cfun_frame_layout.first_restore_gpr = GPR6_REGNUM;
9878 if (cfun_frame_layout.last_restore_gpr == -1
9879 || cfun_frame_layout.last_restore_gpr < GPR6_REGNUM)
9880 cfun_frame_layout.last_restore_gpr = GPR6_REGNUM;
9883 if (cfun_frame_layout.first_save_gpr == -1
9884 || cfun_frame_layout.first_save_gpr > min_gpr)
9885 cfun_frame_layout.first_save_gpr = min_gpr;
9887 if (cfun_frame_layout.last_save_gpr == -1
9888 || cfun_frame_layout.last_save_gpr < max_gpr)
9889 cfun_frame_layout.last_save_gpr = max_gpr;
9891 for (i = min_gpr; i <= max_gpr; i++)
9892 cfun_gpr_save_slot (i) = SAVE_SLOT_STACK;
9895 /* Calculate the save and restore ranges for stm(g) and lm(g) in the
9896 prologue and epilogue. */
9898 static void
9899 s390_register_info_set_ranges ()
9901 int i, j;
9903 /* Find the first and the last save slot supposed to use the stack
9904 to set the restore range.
9905 Vararg regs might be marked as save to stack but only the
9906 call-saved regs really need restoring (i.e. r6). This code
9907 assumes that the vararg regs have not yet been recorded in
9908 cfun_gpr_save_slot. */
9909 for (i = 0; i < 16 && cfun_gpr_save_slot (i) != SAVE_SLOT_STACK; i++);
9910 for (j = 15; j > i && cfun_gpr_save_slot (j) != SAVE_SLOT_STACK; j--);
9911 cfun_frame_layout.first_restore_gpr = (i == 16) ? -1 : i;
9912 cfun_frame_layout.last_restore_gpr = (i == 16) ? -1 : j;
9913 cfun_frame_layout.first_save_gpr = (i == 16) ? -1 : i;
9914 cfun_frame_layout.last_save_gpr = (i == 16) ? -1 : j;
9917 /* The GPR and FPR save slots in cfun->machine->frame_layout are set
9918 for registers which need to be saved in function prologue.
9919 This function can be used until the insns emitted for save/restore
9920 of the regs are visible in the RTL stream. */
9922 static void
9923 s390_register_info ()
9925 int i;
9926 char clobbered_regs[32];
9928 gcc_assert (!epilogue_completed);
9930 if (reload_completed)
9931 /* After reload we rely on our own routine to determine which
9932 registers need saving. */
9933 s390_regs_ever_clobbered (clobbered_regs);
9934 else
9935 /* During reload we use regs_ever_live as a base since reload
9936 does changes in there which we otherwise would not be aware
9937 of. */
9938 for (i = 0; i < 32; i++)
9939 clobbered_regs[i] = df_regs_ever_live_p (i);
9941 for (i = 0; i < 32; i++)
9942 clobbered_regs[i] = clobbered_regs[i] && !global_regs[i];
9944 /* Mark the call-saved FPRs which need to be saved.
9945 This needs to be done before checking the special GPRs since the
9946 stack pointer usage depends on whether high FPRs have to be saved
9947 or not. */
9948 cfun_frame_layout.fpr_bitmap = 0;
9949 cfun_frame_layout.high_fprs = 0;
9950 for (i = FPR0_REGNUM; i <= FPR15_REGNUM; i++)
9951 if (clobbered_regs[i] && !call_really_used_regs[i])
9953 cfun_set_fpr_save (i);
9954 if (i >= FPR8_REGNUM)
9955 cfun_frame_layout.high_fprs++;
9958 /* Register 12 is used for GOT address, but also as temp in prologue
9959 for split-stack stdarg functions (unless r14 is available). */
9960 clobbered_regs[12]
9961 |= ((flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
9962 || (flag_split_stack && cfun->stdarg
9963 && (crtl->is_leaf || TARGET_TPF_PROFILING
9964 || has_hard_reg_initial_val (Pmode, RETURN_REGNUM))));
9966 clobbered_regs[BASE_REGNUM]
9967 |= (cfun->machine->base_reg
9968 && REGNO (cfun->machine->base_reg) == BASE_REGNUM);
9970 clobbered_regs[HARD_FRAME_POINTER_REGNUM]
9971 |= !!frame_pointer_needed;
9973 /* On pre z900 machines this might take until machine dependent
9974 reorg to decide.
9975 save_return_addr_p will only be set on non-zarch machines so
9976 there is no risk that r14 goes into an FPR instead of a stack
9977 slot. */
9978 clobbered_regs[RETURN_REGNUM]
9979 |= (!crtl->is_leaf
9980 || TARGET_TPF_PROFILING
9981 || cfun->machine->split_branches_pending_p
9982 || cfun_frame_layout.save_return_addr_p
9983 || crtl->calls_eh_return);
9985 clobbered_regs[STACK_POINTER_REGNUM]
9986 |= (!crtl->is_leaf
9987 || TARGET_TPF_PROFILING
9988 || cfun_save_high_fprs_p
9989 || get_frame_size () > 0
9990 || (reload_completed && cfun_frame_layout.frame_size > 0)
9991 || cfun->calls_alloca);
9993 memset (cfun_frame_layout.gpr_save_slots, SAVE_SLOT_NONE, 16);
9995 for (i = 6; i < 16; i++)
9996 if (clobbered_regs[i])
9997 cfun_gpr_save_slot (i) = SAVE_SLOT_STACK;
9999 s390_register_info_stdarg_fpr ();
10000 s390_register_info_gprtofpr ();
10001 s390_register_info_set_ranges ();
10002 /* stdarg functions might need to save GPRs 2 to 6. This might
10003 override the GPR->FPR save decision made by
10004 s390_register_info_gprtofpr for r6 since vararg regs must go to
10005 the stack. */
10006 s390_register_info_stdarg_gpr ();
10009 /* This function is called by s390_optimize_prologue in order to get
10010 rid of unnecessary GPR save/restore instructions. The register info
10011 for the GPRs is re-computed and the ranges are re-calculated. */
10013 static void
10014 s390_optimize_register_info ()
10016 char clobbered_regs[32];
10017 int i;
10019 gcc_assert (epilogue_completed);
10020 gcc_assert (!cfun->machine->split_branches_pending_p);
10022 s390_regs_ever_clobbered (clobbered_regs);
10024 for (i = 0; i < 32; i++)
10025 clobbered_regs[i] = clobbered_regs[i] && !global_regs[i];
10027 /* There is still special treatment needed for cases invisible to
10028 s390_regs_ever_clobbered. */
10029 clobbered_regs[RETURN_REGNUM]
10030 |= (TARGET_TPF_PROFILING
10031 /* When expanding builtin_return_addr in ESA mode we do not
10032 know whether r14 will later be needed as scratch reg when
10033 doing branch splitting. So the builtin always accesses the
10034 r14 save slot and we need to stick to the save/restore
10035 decision for r14 even if it turns out that it didn't get
10036 clobbered. */
10037 || cfun_frame_layout.save_return_addr_p
10038 || crtl->calls_eh_return);
10040 memset (cfun_frame_layout.gpr_save_slots, SAVE_SLOT_NONE, 6);
10042 for (i = 6; i < 16; i++)
10043 if (!clobbered_regs[i])
10044 cfun_gpr_save_slot (i) = SAVE_SLOT_NONE;
10046 s390_register_info_set_ranges ();
10047 s390_register_info_stdarg_gpr ();
10050 /* Fill cfun->machine with info about frame of current function. */
10052 static void
10053 s390_frame_info (void)
10055 HOST_WIDE_INT lowest_offset;
10057 cfun_frame_layout.first_save_gpr_slot = cfun_frame_layout.first_save_gpr;
10058 cfun_frame_layout.last_save_gpr_slot = cfun_frame_layout.last_save_gpr;
10060 /* The va_arg builtin uses a constant distance of 16 *
10061 UNITS_PER_LONG (r0-r15) to reach the FPRs from the reg_save_area
10062 pointer. So even if we are going to save the stack pointer in an
10063 FPR we need the stack space in order to keep the offsets
10064 correct. */
10065 if (cfun->stdarg && cfun_save_arg_fprs_p)
10067 cfun_frame_layout.last_save_gpr_slot = STACK_POINTER_REGNUM;
10069 if (cfun_frame_layout.first_save_gpr_slot == -1)
10070 cfun_frame_layout.first_save_gpr_slot = STACK_POINTER_REGNUM;
10073 cfun_frame_layout.frame_size = get_frame_size ();
10074 if (!TARGET_64BIT && cfun_frame_layout.frame_size > 0x7fff0000)
10075 fatal_error (input_location,
10076 "total size of local variables exceeds architecture limit");
10078 if (!TARGET_PACKED_STACK)
10080 /* Fixed stack layout. */
10081 cfun_frame_layout.backchain_offset = 0;
10082 cfun_frame_layout.f0_offset = 16 * UNITS_PER_LONG;
10083 cfun_frame_layout.f4_offset = cfun_frame_layout.f0_offset + 2 * 8;
10084 cfun_frame_layout.f8_offset = -cfun_frame_layout.high_fprs * 8;
10085 cfun_frame_layout.gprs_offset = (cfun_frame_layout.first_save_gpr_slot
10086 * UNITS_PER_LONG);
10088 else if (TARGET_BACKCHAIN)
10090 /* Kernel stack layout - packed stack, backchain, no float */
10091 gcc_assert (TARGET_SOFT_FLOAT);
10092 cfun_frame_layout.backchain_offset = (STACK_POINTER_OFFSET
10093 - UNITS_PER_LONG);
10095 /* The distance between the backchain and the return address
10096 save slot must not change. So we always need a slot for the
10097 stack pointer which resides in between. */
10098 cfun_frame_layout.last_save_gpr_slot = STACK_POINTER_REGNUM;
10100 cfun_frame_layout.gprs_offset
10101 = cfun_frame_layout.backchain_offset - cfun_gprs_save_area_size;
10103 /* FPRs will not be saved. Nevertheless pick sane values to
10104 keep area calculations valid. */
10105 cfun_frame_layout.f0_offset =
10106 cfun_frame_layout.f4_offset =
10107 cfun_frame_layout.f8_offset = cfun_frame_layout.gprs_offset;
10109 else
10111 int num_fprs;
10113 /* Packed stack layout without backchain. */
10115 /* With stdarg FPRs need their dedicated slots. */
10116 num_fprs = (TARGET_64BIT && cfun->stdarg ? 2
10117 : (cfun_fpr_save_p (FPR4_REGNUM) +
10118 cfun_fpr_save_p (FPR6_REGNUM)));
10119 cfun_frame_layout.f4_offset = STACK_POINTER_OFFSET - 8 * num_fprs;
10121 num_fprs = (cfun->stdarg ? 2
10122 : (cfun_fpr_save_p (FPR0_REGNUM)
10123 + cfun_fpr_save_p (FPR2_REGNUM)));
10124 cfun_frame_layout.f0_offset = cfun_frame_layout.f4_offset - 8 * num_fprs;
10126 cfun_frame_layout.gprs_offset
10127 = cfun_frame_layout.f0_offset - cfun_gprs_save_area_size;
10129 cfun_frame_layout.f8_offset = (cfun_frame_layout.gprs_offset
10130 - cfun_frame_layout.high_fprs * 8);
10133 if (cfun_save_high_fprs_p)
10134 cfun_frame_layout.frame_size += cfun_frame_layout.high_fprs * 8;
10136 if (!crtl->is_leaf)
10137 cfun_frame_layout.frame_size += crtl->outgoing_args_size;
10139 /* In the following cases we have to allocate a STACK_POINTER_OFFSET
10140 sized area at the bottom of the stack. This is required also for
10141 leaf functions. When GCC generates a local stack reference it
10142 will always add STACK_POINTER_OFFSET to all these references. */
10143 if (crtl->is_leaf
10144 && !TARGET_TPF_PROFILING
10145 && cfun_frame_layout.frame_size == 0
10146 && !cfun->calls_alloca)
10147 return;
10149 /* Calculate the number of bytes we have used in our own register
10150 save area. With the packed stack layout we can re-use the
10151 remaining bytes for normal stack elements. */
10153 if (TARGET_PACKED_STACK)
10154 lowest_offset = MIN (MIN (cfun_frame_layout.f0_offset,
10155 cfun_frame_layout.f4_offset),
10156 cfun_frame_layout.gprs_offset);
10157 else
10158 lowest_offset = 0;
10160 if (TARGET_BACKCHAIN)
10161 lowest_offset = MIN (lowest_offset, cfun_frame_layout.backchain_offset);
10163 cfun_frame_layout.frame_size += STACK_POINTER_OFFSET - lowest_offset;
10165 /* If under 31 bit an odd number of gprs has to be saved we have to
10166 adjust the frame size to sustain 8 byte alignment of stack
10167 frames. */
10168 cfun_frame_layout.frame_size = ((cfun_frame_layout.frame_size +
10169 STACK_BOUNDARY / BITS_PER_UNIT - 1)
10170 & ~(STACK_BOUNDARY / BITS_PER_UNIT - 1));
10173 /* Generate frame layout. Fills in register and frame data for the current
10174 function in cfun->machine. This routine can be called multiple times;
10175 it will re-do the complete frame layout every time. */
10177 static void
10178 s390_init_frame_layout (void)
10180 HOST_WIDE_INT frame_size;
10181 int base_used;
10183 /* After LRA the frame layout is supposed to be read-only and should
10184 not be re-computed. */
10185 if (reload_completed)
10186 return;
10188 /* On S/390 machines, we may need to perform branch splitting, which
10189 will require both base and return address register. We have no
10190 choice but to assume we're going to need them until right at the
10191 end of the machine dependent reorg phase. */
10192 if (!TARGET_CPU_ZARCH)
10193 cfun->machine->split_branches_pending_p = true;
10197 frame_size = cfun_frame_layout.frame_size;
10199 /* Try to predict whether we'll need the base register. */
10200 base_used = cfun->machine->split_branches_pending_p
10201 || crtl->uses_const_pool
10202 || (!DISP_IN_RANGE (frame_size)
10203 && !CONST_OK_FOR_K (frame_size));
10205 /* Decide which register to use as literal pool base. In small
10206 leaf functions, try to use an unused call-clobbered register
10207 as base register to avoid save/restore overhead. */
10208 if (!base_used)
10209 cfun->machine->base_reg = NULL_RTX;
10210 else
10212 int br = 0;
10214 if (crtl->is_leaf)
10215 /* Prefer r5 (most likely to be free). */
10216 for (br = 5; br >= 2 && df_regs_ever_live_p (br); br--)
10218 cfun->machine->base_reg =
10219 gen_rtx_REG (Pmode, (br >= 2) ? br : BASE_REGNUM);
10222 s390_register_info ();
10223 s390_frame_info ();
10225 while (frame_size != cfun_frame_layout.frame_size);
10228 /* Remove the FPR clobbers from a tbegin insn if it can be proven that
10229 the TX is nonescaping. A transaction is considered escaping if
10230 there is at least one path from tbegin returning CC0 to the
10231 function exit block without an tend.
10233 The check so far has some limitations:
10234 - only single tbegin/tend BBs are supported
10235 - the first cond jump after tbegin must separate the CC0 path from ~CC0
10236 - when CC is copied to a GPR and the CC0 check is done with the GPR
10237 this is not supported
10240 static void
10241 s390_optimize_nonescaping_tx (void)
10243 const unsigned int CC0 = 1 << 3;
10244 basic_block tbegin_bb = NULL;
10245 basic_block tend_bb = NULL;
10246 basic_block bb;
10247 rtx_insn *insn;
10248 bool result = true;
10249 int bb_index;
10250 rtx_insn *tbegin_insn = NULL;
10252 if (!cfun->machine->tbegin_p)
10253 return;
10255 for (bb_index = 0; bb_index < n_basic_blocks_for_fn (cfun); bb_index++)
10257 bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
10259 if (!bb)
10260 continue;
10262 FOR_BB_INSNS (bb, insn)
10264 rtx ite, cc, pat, target;
10265 unsigned HOST_WIDE_INT mask;
10267 if (!INSN_P (insn) || INSN_CODE (insn) <= 0)
10268 continue;
10270 pat = PATTERN (insn);
10272 if (GET_CODE (pat) == PARALLEL)
10273 pat = XVECEXP (pat, 0, 0);
10275 if (GET_CODE (pat) != SET
10276 || GET_CODE (SET_SRC (pat)) != UNSPEC_VOLATILE)
10277 continue;
10279 if (XINT (SET_SRC (pat), 1) == UNSPECV_TBEGIN)
10281 rtx_insn *tmp;
10283 tbegin_insn = insn;
10285 /* Just return if the tbegin doesn't have clobbers. */
10286 if (GET_CODE (PATTERN (insn)) != PARALLEL)
10287 return;
10289 if (tbegin_bb != NULL)
10290 return;
10292 /* Find the next conditional jump. */
10293 for (tmp = NEXT_INSN (insn);
10294 tmp != NULL_RTX;
10295 tmp = NEXT_INSN (tmp))
10297 if (reg_set_p (gen_rtx_REG (CCmode, CC_REGNUM), tmp))
10298 return;
10299 if (!JUMP_P (tmp))
10300 continue;
10302 ite = SET_SRC (PATTERN (tmp));
10303 if (GET_CODE (ite) != IF_THEN_ELSE)
10304 continue;
10306 cc = XEXP (XEXP (ite, 0), 0);
10307 if (!REG_P (cc) || !CC_REGNO_P (REGNO (cc))
10308 || GET_MODE (cc) != CCRAWmode
10309 || GET_CODE (XEXP (XEXP (ite, 0), 1)) != CONST_INT)
10310 return;
10312 if (bb->succs->length () != 2)
10313 return;
10315 mask = INTVAL (XEXP (XEXP (ite, 0), 1));
10316 if (GET_CODE (XEXP (ite, 0)) == NE)
10317 mask ^= 0xf;
10319 if (mask == CC0)
10320 target = XEXP (ite, 1);
10321 else if (mask == (CC0 ^ 0xf))
10322 target = XEXP (ite, 2);
10323 else
10324 return;
10327 edge_iterator ei;
10328 edge e1, e2;
10330 ei = ei_start (bb->succs);
10331 e1 = ei_safe_edge (ei);
10332 ei_next (&ei);
10333 e2 = ei_safe_edge (ei);
10335 if (e2->flags & EDGE_FALLTHRU)
10337 e2 = e1;
10338 e1 = ei_safe_edge (ei);
10341 if (!(e1->flags & EDGE_FALLTHRU))
10342 return;
10344 tbegin_bb = (target == pc_rtx) ? e1->dest : e2->dest;
10346 if (tmp == BB_END (bb))
10347 break;
10351 if (XINT (SET_SRC (pat), 1) == UNSPECV_TEND)
10353 if (tend_bb != NULL)
10354 return;
10355 tend_bb = bb;
10360 /* Either we successfully remove the FPR clobbers here or we are not
10361 able to do anything for this TX. Both cases don't qualify for
10362 another look. */
10363 cfun->machine->tbegin_p = false;
10365 if (tbegin_bb == NULL || tend_bb == NULL)
10366 return;
10368 calculate_dominance_info (CDI_POST_DOMINATORS);
10369 result = dominated_by_p (CDI_POST_DOMINATORS, tbegin_bb, tend_bb);
10370 free_dominance_info (CDI_POST_DOMINATORS);
10372 if (!result)
10373 return;
10375 PATTERN (tbegin_insn) = gen_rtx_PARALLEL (VOIDmode,
10376 gen_rtvec (2,
10377 XVECEXP (PATTERN (tbegin_insn), 0, 0),
10378 XVECEXP (PATTERN (tbegin_insn), 0, 1)));
10379 INSN_CODE (tbegin_insn) = -1;
10380 df_insn_rescan (tbegin_insn);
10382 return;
10385 /* Return true if it is legal to put a value with MODE into REGNO. */
10387 bool
10388 s390_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
10390 if (!TARGET_VX && VECTOR_NOFP_REGNO_P (regno))
10391 return false;
10393 switch (REGNO_REG_CLASS (regno))
10395 case VEC_REGS:
10396 return ((GET_MODE_CLASS (mode) == MODE_INT
10397 && s390_class_max_nregs (VEC_REGS, mode) == 1)
10398 || mode == DFmode
10399 || (TARGET_VXE && mode == SFmode)
10400 || s390_vector_mode_supported_p (mode));
10401 break;
10402 case FP_REGS:
10403 if (TARGET_VX
10404 && ((GET_MODE_CLASS (mode) == MODE_INT
10405 && s390_class_max_nregs (FP_REGS, mode) == 1)
10406 || mode == DFmode
10407 || s390_vector_mode_supported_p (mode)))
10408 return true;
10410 if (REGNO_PAIR_OK (regno, mode))
10412 if (mode == SImode || mode == DImode)
10413 return true;
10415 if (FLOAT_MODE_P (mode) && GET_MODE_CLASS (mode) != MODE_VECTOR_FLOAT)
10416 return true;
10418 break;
10419 case ADDR_REGS:
10420 if (FRAME_REGNO_P (regno) && mode == Pmode)
10421 return true;
10423 /* fallthrough */
10424 case GENERAL_REGS:
10425 if (REGNO_PAIR_OK (regno, mode))
10427 if (TARGET_ZARCH
10428 || (mode != TFmode && mode != TCmode && mode != TDmode))
10429 return true;
10431 break;
10432 case CC_REGS:
10433 if (GET_MODE_CLASS (mode) == MODE_CC)
10434 return true;
10435 break;
10436 case ACCESS_REGS:
10437 if (REGNO_PAIR_OK (regno, mode))
10439 if (mode == SImode || mode == Pmode)
10440 return true;
10442 break;
10443 default:
10444 return false;
10447 return false;
10450 /* Return nonzero if register OLD_REG can be renamed to register NEW_REG. */
10452 bool
10453 s390_hard_regno_rename_ok (unsigned int old_reg, unsigned int new_reg)
10455 /* Once we've decided upon a register to use as base register, it must
10456 no longer be used for any other purpose. */
10457 if (cfun->machine->base_reg)
10458 if (REGNO (cfun->machine->base_reg) == old_reg
10459 || REGNO (cfun->machine->base_reg) == new_reg)
10460 return false;
10462 /* Prevent regrename from using call-saved regs which haven't
10463 actually been saved. This is necessary since regrename assumes
10464 the backend save/restore decisions are based on
10465 df_regs_ever_live. Since we have our own routine we have to tell
10466 regrename manually about it. */
10467 if (GENERAL_REGNO_P (new_reg)
10468 && !call_really_used_regs[new_reg]
10469 && cfun_gpr_save_slot (new_reg) == SAVE_SLOT_NONE)
10470 return false;
10472 return true;
10475 /* Return nonzero if register REGNO can be used as a scratch register
10476 in peephole2. */
10478 static bool
10479 s390_hard_regno_scratch_ok (unsigned int regno)
10481 /* See s390_hard_regno_rename_ok. */
10482 if (GENERAL_REGNO_P (regno)
10483 && !call_really_used_regs[regno]
10484 && cfun_gpr_save_slot (regno) == SAVE_SLOT_NONE)
10485 return false;
10487 return true;
10490 /* Maximum number of registers to represent a value of mode MODE
10491 in a register of class RCLASS. */
10494 s390_class_max_nregs (enum reg_class rclass, machine_mode mode)
10496 int reg_size;
10497 bool reg_pair_required_p = false;
10499 switch (rclass)
10501 case FP_REGS:
10502 case VEC_REGS:
10503 reg_size = TARGET_VX ? 16 : 8;
10505 /* TF and TD modes would fit into a VR but we put them into a
10506 register pair since we do not have 128bit FP instructions on
10507 full VRs. */
10508 if (TARGET_VX
10509 && SCALAR_FLOAT_MODE_P (mode)
10510 && GET_MODE_SIZE (mode) >= 16)
10511 reg_pair_required_p = true;
10513 /* Even if complex types would fit into a single FPR/VR we force
10514 them into a register pair to deal with the parts more easily.
10515 (FIXME: What about complex ints?) */
10516 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
10517 reg_pair_required_p = true;
10518 break;
10519 case ACCESS_REGS:
10520 reg_size = 4;
10521 break;
10522 default:
10523 reg_size = UNITS_PER_WORD;
10524 break;
10527 if (reg_pair_required_p)
10528 return 2 * ((GET_MODE_SIZE (mode) / 2 + reg_size - 1) / reg_size);
10530 return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
10533 /* Return TRUE if changing mode from FROM to TO should not be allowed
10534 for register class CLASS. */
10537 s390_cannot_change_mode_class (machine_mode from_mode,
10538 machine_mode to_mode,
10539 enum reg_class rclass)
10541 machine_mode small_mode;
10542 machine_mode big_mode;
10544 /* V1TF and TF have different representations in vector
10545 registers. */
10546 if (reg_classes_intersect_p (VEC_REGS, rclass)
10547 && ((from_mode == V1TFmode && to_mode == TFmode)
10548 || (from_mode == TFmode && to_mode == V1TFmode)))
10549 return 1;
10551 if (GET_MODE_SIZE (from_mode) == GET_MODE_SIZE (to_mode))
10552 return 0;
10554 if (GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
10556 small_mode = from_mode;
10557 big_mode = to_mode;
10559 else
10561 small_mode = to_mode;
10562 big_mode = from_mode;
10565 /* Values residing in VRs are little-endian style. All modes are
10566 placed left-aligned in an VR. This means that we cannot allow
10567 switching between modes with differing sizes. Also if the vector
10568 facility is available we still place TFmode values in VR register
10569 pairs, since the only instructions we have operating on TFmodes
10570 only deal with register pairs. Therefore we have to allow DFmode
10571 subregs of TFmodes to enable the TFmode splitters. */
10572 if (reg_classes_intersect_p (VEC_REGS, rclass)
10573 && (GET_MODE_SIZE (small_mode) < 8
10574 || s390_class_max_nregs (VEC_REGS, big_mode) == 1))
10575 return 1;
10577 /* Likewise for access registers, since they have only half the
10578 word size on 64-bit. */
10579 if (reg_classes_intersect_p (ACCESS_REGS, rclass))
10580 return 1;
10582 return 0;
10585 /* Return true if we use LRA instead of reload pass. */
10586 static bool
10587 s390_lra_p (void)
10589 return s390_lra_flag;
10592 /* Return true if register FROM can be eliminated via register TO. */
10594 static bool
10595 s390_can_eliminate (const int from, const int to)
10597 /* On zSeries machines, we have not marked the base register as fixed.
10598 Instead, we have an elimination rule BASE_REGNUM -> BASE_REGNUM.
10599 If a function requires the base register, we say here that this
10600 elimination cannot be performed. This will cause reload to free
10601 up the base register (as if it were fixed). On the other hand,
10602 if the current function does *not* require the base register, we
10603 say here the elimination succeeds, which in turn allows reload
10604 to allocate the base register for any other purpose. */
10605 if (from == BASE_REGNUM && to == BASE_REGNUM)
10607 if (TARGET_CPU_ZARCH)
10609 s390_init_frame_layout ();
10610 return cfun->machine->base_reg == NULL_RTX;
10613 return false;
10616 /* Everything else must point into the stack frame. */
10617 gcc_assert (to == STACK_POINTER_REGNUM
10618 || to == HARD_FRAME_POINTER_REGNUM);
10620 gcc_assert (from == FRAME_POINTER_REGNUM
10621 || from == ARG_POINTER_REGNUM
10622 || from == RETURN_ADDRESS_POINTER_REGNUM);
10624 /* Make sure we actually saved the return address. */
10625 if (from == RETURN_ADDRESS_POINTER_REGNUM)
10626 if (!crtl->calls_eh_return
10627 && !cfun->stdarg
10628 && !cfun_frame_layout.save_return_addr_p)
10629 return false;
10631 return true;
10634 /* Return offset between register FROM and TO initially after prolog. */
10636 HOST_WIDE_INT
10637 s390_initial_elimination_offset (int from, int to)
10639 HOST_WIDE_INT offset;
10641 /* ??? Why are we called for non-eliminable pairs? */
10642 if (!s390_can_eliminate (from, to))
10643 return 0;
10645 switch (from)
10647 case FRAME_POINTER_REGNUM:
10648 offset = (get_frame_size()
10649 + STACK_POINTER_OFFSET
10650 + crtl->outgoing_args_size);
10651 break;
10653 case ARG_POINTER_REGNUM:
10654 s390_init_frame_layout ();
10655 offset = cfun_frame_layout.frame_size + STACK_POINTER_OFFSET;
10656 break;
10658 case RETURN_ADDRESS_POINTER_REGNUM:
10659 s390_init_frame_layout ();
10661 if (cfun_frame_layout.first_save_gpr_slot == -1)
10663 /* If it turns out that for stdarg nothing went into the reg
10664 save area we also do not need the return address
10665 pointer. */
10666 if (cfun->stdarg && !cfun_save_arg_fprs_p)
10667 return 0;
10669 gcc_unreachable ();
10672 /* In order to make the following work it is not necessary for
10673 r14 to have a save slot. It is sufficient if one other GPR
10674 got one. Since the GPRs are always stored without gaps we
10675 are able to calculate where the r14 save slot would
10676 reside. */
10677 offset = (cfun_frame_layout.frame_size + cfun_frame_layout.gprs_offset +
10678 (RETURN_REGNUM - cfun_frame_layout.first_save_gpr_slot) *
10679 UNITS_PER_LONG);
10680 break;
10682 case BASE_REGNUM:
10683 offset = 0;
10684 break;
10686 default:
10687 gcc_unreachable ();
10690 return offset;
10693 /* Emit insn to save fpr REGNUM at offset OFFSET relative
10694 to register BASE. Return generated insn. */
10696 static rtx
10697 save_fpr (rtx base, int offset, int regnum)
10699 rtx addr;
10700 addr = gen_rtx_MEM (DFmode, plus_constant (Pmode, base, offset));
10702 if (regnum >= 16 && regnum <= (16 + FP_ARG_NUM_REG))
10703 set_mem_alias_set (addr, get_varargs_alias_set ());
10704 else
10705 set_mem_alias_set (addr, get_frame_alias_set ());
10707 return emit_move_insn (addr, gen_rtx_REG (DFmode, regnum));
10710 /* Emit insn to restore fpr REGNUM from offset OFFSET relative
10711 to register BASE. Return generated insn. */
10713 static rtx
10714 restore_fpr (rtx base, int offset, int regnum)
10716 rtx addr;
10717 addr = gen_rtx_MEM (DFmode, plus_constant (Pmode, base, offset));
10718 set_mem_alias_set (addr, get_frame_alias_set ());
10720 return emit_move_insn (gen_rtx_REG (DFmode, regnum), addr);
10723 /* Return true if REGNO is a global register, but not one
10724 of the special ones that need to be saved/restored in anyway. */
10726 static inline bool
10727 global_not_special_regno_p (int regno)
10729 return (global_regs[regno]
10730 /* These registers are special and need to be
10731 restored in any case. */
10732 && !(regno == STACK_POINTER_REGNUM
10733 || regno == RETURN_REGNUM
10734 || regno == BASE_REGNUM
10735 || (flag_pic && regno == (int)PIC_OFFSET_TABLE_REGNUM)));
10738 /* Generate insn to save registers FIRST to LAST into
10739 the register save area located at offset OFFSET
10740 relative to register BASE. */
10742 static rtx
10743 save_gprs (rtx base, int offset, int first, int last)
10745 rtx addr, insn, note;
10746 int i;
10748 addr = plus_constant (Pmode, base, offset);
10749 addr = gen_rtx_MEM (Pmode, addr);
10751 set_mem_alias_set (addr, get_frame_alias_set ());
10753 /* Special-case single register. */
10754 if (first == last)
10756 if (TARGET_64BIT)
10757 insn = gen_movdi (addr, gen_rtx_REG (Pmode, first));
10758 else
10759 insn = gen_movsi (addr, gen_rtx_REG (Pmode, first));
10761 if (!global_not_special_regno_p (first))
10762 RTX_FRAME_RELATED_P (insn) = 1;
10763 return insn;
10767 insn = gen_store_multiple (addr,
10768 gen_rtx_REG (Pmode, first),
10769 GEN_INT (last - first + 1));
10771 if (first <= 6 && cfun->stdarg)
10772 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
10774 rtx mem = XEXP (XVECEXP (PATTERN (insn), 0, i), 0);
10776 if (first + i <= 6)
10777 set_mem_alias_set (mem, get_varargs_alias_set ());
10780 /* We need to set the FRAME_RELATED flag on all SETs
10781 inside the store-multiple pattern.
10783 However, we must not emit DWARF records for registers 2..5
10784 if they are stored for use by variable arguments ...
10786 ??? Unfortunately, it is not enough to simply not the
10787 FRAME_RELATED flags for those SETs, because the first SET
10788 of the PARALLEL is always treated as if it had the flag
10789 set, even if it does not. Therefore we emit a new pattern
10790 without those registers as REG_FRAME_RELATED_EXPR note. */
10792 if (first >= 6 && !global_not_special_regno_p (first))
10794 rtx pat = PATTERN (insn);
10796 for (i = 0; i < XVECLEN (pat, 0); i++)
10797 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
10798 && !global_not_special_regno_p (REGNO (SET_SRC (XVECEXP (pat,
10799 0, i)))))
10800 RTX_FRAME_RELATED_P (XVECEXP (pat, 0, i)) = 1;
10802 RTX_FRAME_RELATED_P (insn) = 1;
10804 else if (last >= 6)
10806 int start;
10808 for (start = first >= 6 ? first : 6; start <= last; start++)
10809 if (!global_not_special_regno_p (start))
10810 break;
10812 if (start > last)
10813 return insn;
10815 addr = plus_constant (Pmode, base,
10816 offset + (start - first) * UNITS_PER_LONG);
10818 if (start == last)
10820 if (TARGET_64BIT)
10821 note = gen_movdi (gen_rtx_MEM (Pmode, addr),
10822 gen_rtx_REG (Pmode, start));
10823 else
10824 note = gen_movsi (gen_rtx_MEM (Pmode, addr),
10825 gen_rtx_REG (Pmode, start));
10826 note = PATTERN (note);
10828 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note);
10829 RTX_FRAME_RELATED_P (insn) = 1;
10831 return insn;
10834 note = gen_store_multiple (gen_rtx_MEM (Pmode, addr),
10835 gen_rtx_REG (Pmode, start),
10836 GEN_INT (last - start + 1));
10837 note = PATTERN (note);
10839 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note);
10841 for (i = 0; i < XVECLEN (note, 0); i++)
10842 if (GET_CODE (XVECEXP (note, 0, i)) == SET
10843 && !global_not_special_regno_p (REGNO (SET_SRC (XVECEXP (note,
10844 0, i)))))
10845 RTX_FRAME_RELATED_P (XVECEXP (note, 0, i)) = 1;
10847 RTX_FRAME_RELATED_P (insn) = 1;
10850 return insn;
10853 /* Generate insn to restore registers FIRST to LAST from
10854 the register save area located at offset OFFSET
10855 relative to register BASE. */
10857 static rtx
10858 restore_gprs (rtx base, int offset, int first, int last)
10860 rtx addr, insn;
10862 addr = plus_constant (Pmode, base, offset);
10863 addr = gen_rtx_MEM (Pmode, addr);
10864 set_mem_alias_set (addr, get_frame_alias_set ());
10866 /* Special-case single register. */
10867 if (first == last)
10869 if (TARGET_64BIT)
10870 insn = gen_movdi (gen_rtx_REG (Pmode, first), addr);
10871 else
10872 insn = gen_movsi (gen_rtx_REG (Pmode, first), addr);
10874 RTX_FRAME_RELATED_P (insn) = 1;
10875 return insn;
10878 insn = gen_load_multiple (gen_rtx_REG (Pmode, first),
10879 addr,
10880 GEN_INT (last - first + 1));
10881 RTX_FRAME_RELATED_P (insn) = 1;
10882 return insn;
10885 /* Return insn sequence to load the GOT register. */
10887 rtx_insn *
10888 s390_load_got (void)
10890 rtx_insn *insns;
10892 /* We cannot use pic_offset_table_rtx here since we use this
10893 function also for non-pic if __tls_get_offset is called and in
10894 that case PIC_OFFSET_TABLE_REGNUM as well as pic_offset_table_rtx
10895 aren't usable. */
10896 rtx got_rtx = gen_rtx_REG (Pmode, 12);
10898 start_sequence ();
10900 if (TARGET_CPU_ZARCH)
10902 emit_move_insn (got_rtx, s390_got_symbol ());
10904 else
10906 rtx offset;
10908 offset = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, s390_got_symbol ()),
10909 UNSPEC_LTREL_OFFSET);
10910 offset = gen_rtx_CONST (Pmode, offset);
10911 offset = force_const_mem (Pmode, offset);
10913 emit_move_insn (got_rtx, offset);
10915 offset = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (offset, 0)),
10916 UNSPEC_LTREL_BASE);
10917 offset = gen_rtx_PLUS (Pmode, got_rtx, offset);
10919 emit_move_insn (got_rtx, offset);
10922 insns = get_insns ();
10923 end_sequence ();
10924 return insns;
10927 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
10928 and the change to the stack pointer. */
10930 static void
10931 s390_emit_stack_tie (void)
10933 rtx mem = gen_frame_mem (BLKmode,
10934 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM));
10936 emit_insn (gen_stack_tie (mem));
10939 /* Copy GPRS into FPR save slots. */
10941 static void
10942 s390_save_gprs_to_fprs (void)
10944 int i;
10946 if (!TARGET_Z10 || !TARGET_HARD_FLOAT || !crtl->is_leaf)
10947 return;
10949 for (i = 6; i < 16; i++)
10951 if (FP_REGNO_P (cfun_gpr_save_slot (i)))
10953 rtx_insn *insn =
10954 emit_move_insn (gen_rtx_REG (DImode, cfun_gpr_save_slot (i)),
10955 gen_rtx_REG (DImode, i));
10956 RTX_FRAME_RELATED_P (insn) = 1;
10957 /* This prevents dwarf2cfi from interpreting the set. Doing
10958 so it might emit def_cfa_register infos setting an FPR as
10959 new CFA. */
10960 add_reg_note (insn, REG_CFA_REGISTER, copy_rtx (PATTERN (insn)));
10965 /* Restore GPRs from FPR save slots. */
10967 static void
10968 s390_restore_gprs_from_fprs (void)
10970 int i;
10972 if (!TARGET_Z10 || !TARGET_HARD_FLOAT || !crtl->is_leaf)
10973 return;
10975 for (i = 6; i < 16; i++)
10977 rtx_insn *insn;
10979 if (!FP_REGNO_P (cfun_gpr_save_slot (i)))
10980 continue;
10982 rtx fpr = gen_rtx_REG (DImode, cfun_gpr_save_slot (i));
10984 if (i == STACK_POINTER_REGNUM)
10985 insn = emit_insn (gen_stack_restore_from_fpr (fpr));
10986 else
10987 insn = emit_move_insn (gen_rtx_REG (DImode, i), fpr);
10989 df_set_regs_ever_live (i, true);
10990 add_reg_note (insn, REG_CFA_RESTORE, gen_rtx_REG (DImode, i));
10991 if (i == STACK_POINTER_REGNUM)
10992 add_reg_note (insn, REG_CFA_DEF_CFA,
10993 plus_constant (Pmode, stack_pointer_rtx,
10994 STACK_POINTER_OFFSET));
10995 RTX_FRAME_RELATED_P (insn) = 1;
11000 /* A pass run immediately before shrink-wrapping and prologue and epilogue
11001 generation. */
11003 namespace {
11005 const pass_data pass_data_s390_early_mach =
11007 RTL_PASS, /* type */
11008 "early_mach", /* name */
11009 OPTGROUP_NONE, /* optinfo_flags */
11010 TV_MACH_DEP, /* tv_id */
11011 0, /* properties_required */
11012 0, /* properties_provided */
11013 0, /* properties_destroyed */
11014 0, /* todo_flags_start */
11015 ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
11018 class pass_s390_early_mach : public rtl_opt_pass
11020 public:
11021 pass_s390_early_mach (gcc::context *ctxt)
11022 : rtl_opt_pass (pass_data_s390_early_mach, ctxt)
11025 /* opt_pass methods: */
11026 virtual unsigned int execute (function *);
11028 }; // class pass_s390_early_mach
11030 unsigned int
11031 pass_s390_early_mach::execute (function *fun)
11033 rtx_insn *insn;
11035 /* Try to get rid of the FPR clobbers. */
11036 s390_optimize_nonescaping_tx ();
11038 /* Re-compute register info. */
11039 s390_register_info ();
11041 /* If we're using a base register, ensure that it is always valid for
11042 the first non-prologue instruction. */
11043 if (fun->machine->base_reg)
11044 emit_insn_at_entry (gen_main_pool (fun->machine->base_reg));
11046 /* Annotate all constant pool references to let the scheduler know
11047 they implicitly use the base register. */
11048 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11049 if (INSN_P (insn))
11051 annotate_constant_pool_refs (&PATTERN (insn));
11052 df_insn_rescan (insn);
11054 return 0;
11057 } // anon namespace
11059 /* Expand the prologue into a bunch of separate insns. */
11061 void
11062 s390_emit_prologue (void)
11064 rtx insn, addr;
11065 rtx temp_reg;
11066 int i;
11067 int offset;
11068 int next_fpr = 0;
11070 /* Choose best register to use for temp use within prologue.
11071 TPF with profiling must avoid the register 14 - the tracing function
11072 needs the original contents of r14 to be preserved. */
11074 if (!has_hard_reg_initial_val (Pmode, RETURN_REGNUM)
11075 && !crtl->is_leaf
11076 && !TARGET_TPF_PROFILING)
11077 temp_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
11078 else if (flag_split_stack && cfun->stdarg)
11079 temp_reg = gen_rtx_REG (Pmode, 12);
11080 else
11081 temp_reg = gen_rtx_REG (Pmode, 1);
11083 s390_save_gprs_to_fprs ();
11085 /* Save call saved gprs. */
11086 if (cfun_frame_layout.first_save_gpr != -1)
11088 insn = save_gprs (stack_pointer_rtx,
11089 cfun_frame_layout.gprs_offset +
11090 UNITS_PER_LONG * (cfun_frame_layout.first_save_gpr
11091 - cfun_frame_layout.first_save_gpr_slot),
11092 cfun_frame_layout.first_save_gpr,
11093 cfun_frame_layout.last_save_gpr);
11094 emit_insn (insn);
11097 /* Dummy insn to mark literal pool slot. */
11099 if (cfun->machine->base_reg)
11100 emit_insn (gen_main_pool (cfun->machine->base_reg));
11102 offset = cfun_frame_layout.f0_offset;
11104 /* Save f0 and f2. */
11105 for (i = FPR0_REGNUM; i <= FPR0_REGNUM + 1; i++)
11107 if (cfun_fpr_save_p (i))
11109 save_fpr (stack_pointer_rtx, offset, i);
11110 offset += 8;
11112 else if (!TARGET_PACKED_STACK || cfun->stdarg)
11113 offset += 8;
11116 /* Save f4 and f6. */
11117 offset = cfun_frame_layout.f4_offset;
11118 for (i = FPR4_REGNUM; i <= FPR4_REGNUM + 1; i++)
11120 if (cfun_fpr_save_p (i))
11122 insn = save_fpr (stack_pointer_rtx, offset, i);
11123 offset += 8;
11125 /* If f4 and f6 are call clobbered they are saved due to
11126 stdargs and therefore are not frame related. */
11127 if (!call_really_used_regs[i])
11128 RTX_FRAME_RELATED_P (insn) = 1;
11130 else if (!TARGET_PACKED_STACK || call_really_used_regs[i])
11131 offset += 8;
11134 if (TARGET_PACKED_STACK
11135 && cfun_save_high_fprs_p
11136 && cfun_frame_layout.f8_offset + cfun_frame_layout.high_fprs * 8 > 0)
11138 offset = (cfun_frame_layout.f8_offset
11139 + (cfun_frame_layout.high_fprs - 1) * 8);
11141 for (i = FPR15_REGNUM; i >= FPR8_REGNUM && offset >= 0; i--)
11142 if (cfun_fpr_save_p (i))
11144 insn = save_fpr (stack_pointer_rtx, offset, i);
11146 RTX_FRAME_RELATED_P (insn) = 1;
11147 offset -= 8;
11149 if (offset >= cfun_frame_layout.f8_offset)
11150 next_fpr = i;
11153 if (!TARGET_PACKED_STACK)
11154 next_fpr = cfun_save_high_fprs_p ? FPR15_REGNUM : 0;
11156 if (flag_stack_usage_info)
11157 current_function_static_stack_size = cfun_frame_layout.frame_size;
11159 /* Decrement stack pointer. */
11161 if (cfun_frame_layout.frame_size > 0)
11163 rtx frame_off = GEN_INT (-cfun_frame_layout.frame_size);
11164 rtx real_frame_off;
11166 if (s390_stack_size)
11168 HOST_WIDE_INT stack_guard;
11170 if (s390_stack_guard)
11171 stack_guard = s390_stack_guard;
11172 else
11174 /* If no value for stack guard is provided the smallest power of 2
11175 larger than the current frame size is chosen. */
11176 stack_guard = 1;
11177 while (stack_guard < cfun_frame_layout.frame_size)
11178 stack_guard <<= 1;
11181 if (cfun_frame_layout.frame_size >= s390_stack_size)
11183 warning (0, "frame size of function %qs is %wd"
11184 " bytes exceeding user provided stack limit of "
11185 "%d bytes. "
11186 "An unconditional trap is added.",
11187 current_function_name(), cfun_frame_layout.frame_size,
11188 s390_stack_size);
11189 emit_insn (gen_trap ());
11190 emit_barrier ();
11192 else
11194 /* stack_guard has to be smaller than s390_stack_size.
11195 Otherwise we would emit an AND with zero which would
11196 not match the test under mask pattern. */
11197 if (stack_guard >= s390_stack_size)
11199 warning (0, "frame size of function %qs is %wd"
11200 " bytes which is more than half the stack size. "
11201 "The dynamic check would not be reliable. "
11202 "No check emitted for this function.",
11203 current_function_name(),
11204 cfun_frame_layout.frame_size);
11206 else
11208 HOST_WIDE_INT stack_check_mask = ((s390_stack_size - 1)
11209 & ~(stack_guard - 1));
11211 rtx t = gen_rtx_AND (Pmode, stack_pointer_rtx,
11212 GEN_INT (stack_check_mask));
11213 if (TARGET_64BIT)
11214 emit_insn (gen_ctrapdi4 (gen_rtx_EQ (VOIDmode,
11215 t, const0_rtx),
11216 t, const0_rtx, const0_rtx));
11217 else
11218 emit_insn (gen_ctrapsi4 (gen_rtx_EQ (VOIDmode,
11219 t, const0_rtx),
11220 t, const0_rtx, const0_rtx));
11225 if (s390_warn_framesize > 0
11226 && cfun_frame_layout.frame_size >= s390_warn_framesize)
11227 warning (0, "frame size of %qs is %wd bytes",
11228 current_function_name (), cfun_frame_layout.frame_size);
11230 if (s390_warn_dynamicstack_p && cfun->calls_alloca)
11231 warning (0, "%qs uses dynamic stack allocation", current_function_name ());
11233 /* Save incoming stack pointer into temp reg. */
11234 if (TARGET_BACKCHAIN || next_fpr)
11235 insn = emit_insn (gen_move_insn (temp_reg, stack_pointer_rtx));
11237 /* Subtract frame size from stack pointer. */
11239 if (DISP_IN_RANGE (INTVAL (frame_off)))
11241 insn = gen_rtx_SET (stack_pointer_rtx,
11242 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
11243 frame_off));
11244 insn = emit_insn (insn);
11246 else
11248 if (!CONST_OK_FOR_K (INTVAL (frame_off)))
11249 frame_off = force_const_mem (Pmode, frame_off);
11251 insn = emit_insn (gen_add2_insn (stack_pointer_rtx, frame_off));
11252 annotate_constant_pool_refs (&PATTERN (insn));
11255 RTX_FRAME_RELATED_P (insn) = 1;
11256 real_frame_off = GEN_INT (-cfun_frame_layout.frame_size);
11257 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
11258 gen_rtx_SET (stack_pointer_rtx,
11259 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
11260 real_frame_off)));
11262 /* Set backchain. */
11264 if (TARGET_BACKCHAIN)
11266 if (cfun_frame_layout.backchain_offset)
11267 addr = gen_rtx_MEM (Pmode,
11268 plus_constant (Pmode, stack_pointer_rtx,
11269 cfun_frame_layout.backchain_offset));
11270 else
11271 addr = gen_rtx_MEM (Pmode, stack_pointer_rtx);
11272 set_mem_alias_set (addr, get_frame_alias_set ());
11273 insn = emit_insn (gen_move_insn (addr, temp_reg));
11276 /* If we support non-call exceptions (e.g. for Java),
11277 we need to make sure the backchain pointer is set up
11278 before any possibly trapping memory access. */
11279 if (TARGET_BACKCHAIN && cfun->can_throw_non_call_exceptions)
11281 addr = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
11282 emit_clobber (addr);
11286 /* Save fprs 8 - 15 (64 bit ABI). */
11288 if (cfun_save_high_fprs_p && next_fpr)
11290 /* If the stack might be accessed through a different register
11291 we have to make sure that the stack pointer decrement is not
11292 moved below the use of the stack slots. */
11293 s390_emit_stack_tie ();
11295 insn = emit_insn (gen_add2_insn (temp_reg,
11296 GEN_INT (cfun_frame_layout.f8_offset)));
11298 offset = 0;
11300 for (i = FPR8_REGNUM; i <= next_fpr; i++)
11301 if (cfun_fpr_save_p (i))
11303 rtx addr = plus_constant (Pmode, stack_pointer_rtx,
11304 cfun_frame_layout.frame_size
11305 + cfun_frame_layout.f8_offset
11306 + offset);
11308 insn = save_fpr (temp_reg, offset, i);
11309 offset += 8;
11310 RTX_FRAME_RELATED_P (insn) = 1;
11311 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
11312 gen_rtx_SET (gen_rtx_MEM (DFmode, addr),
11313 gen_rtx_REG (DFmode, i)));
11317 /* Set frame pointer, if needed. */
11319 if (frame_pointer_needed)
11321 insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
11322 RTX_FRAME_RELATED_P (insn) = 1;
11325 /* Set up got pointer, if needed. */
11327 if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
11329 rtx_insn *insns = s390_load_got ();
11331 for (rtx_insn *insn = insns; insn; insn = NEXT_INSN (insn))
11332 annotate_constant_pool_refs (&PATTERN (insn));
11334 emit_insn (insns);
11337 if (TARGET_TPF_PROFILING)
11339 /* Generate a BAS instruction to serve as a function
11340 entry intercept to facilitate the use of tracing
11341 algorithms located at the branch target. */
11342 emit_insn (gen_prologue_tpf ());
11344 /* Emit a blockage here so that all code
11345 lies between the profiling mechanisms. */
11346 emit_insn (gen_blockage ());
11350 /* Expand the epilogue into a bunch of separate insns. */
11352 void
11353 s390_emit_epilogue (bool sibcall)
11355 rtx frame_pointer, return_reg, cfa_restores = NULL_RTX;
11356 int area_bottom, area_top, offset = 0;
11357 int next_offset;
11358 rtvec p;
11359 int i;
11361 if (TARGET_TPF_PROFILING)
11364 /* Generate a BAS instruction to serve as a function
11365 entry intercept to facilitate the use of tracing
11366 algorithms located at the branch target. */
11368 /* Emit a blockage here so that all code
11369 lies between the profiling mechanisms. */
11370 emit_insn (gen_blockage ());
11372 emit_insn (gen_epilogue_tpf ());
11375 /* Check whether to use frame or stack pointer for restore. */
11377 frame_pointer = (frame_pointer_needed
11378 ? hard_frame_pointer_rtx : stack_pointer_rtx);
11380 s390_frame_area (&area_bottom, &area_top);
11382 /* Check whether we can access the register save area.
11383 If not, increment the frame pointer as required. */
11385 if (area_top <= area_bottom)
11387 /* Nothing to restore. */
11389 else if (DISP_IN_RANGE (cfun_frame_layout.frame_size + area_bottom)
11390 && DISP_IN_RANGE (cfun_frame_layout.frame_size + area_top - 1))
11392 /* Area is in range. */
11393 offset = cfun_frame_layout.frame_size;
11395 else
11397 rtx insn, frame_off, cfa;
11399 offset = area_bottom < 0 ? -area_bottom : 0;
11400 frame_off = GEN_INT (cfun_frame_layout.frame_size - offset);
11402 cfa = gen_rtx_SET (frame_pointer,
11403 gen_rtx_PLUS (Pmode, frame_pointer, frame_off));
11404 if (DISP_IN_RANGE (INTVAL (frame_off)))
11406 insn = gen_rtx_SET (frame_pointer,
11407 gen_rtx_PLUS (Pmode, frame_pointer, frame_off));
11408 insn = emit_insn (insn);
11410 else
11412 if (!CONST_OK_FOR_K (INTVAL (frame_off)))
11413 frame_off = force_const_mem (Pmode, frame_off);
11415 insn = emit_insn (gen_add2_insn (frame_pointer, frame_off));
11416 annotate_constant_pool_refs (&PATTERN (insn));
11418 add_reg_note (insn, REG_CFA_ADJUST_CFA, cfa);
11419 RTX_FRAME_RELATED_P (insn) = 1;
11422 /* Restore call saved fprs. */
11424 if (TARGET_64BIT)
11426 if (cfun_save_high_fprs_p)
11428 next_offset = cfun_frame_layout.f8_offset;
11429 for (i = FPR8_REGNUM; i <= FPR15_REGNUM; i++)
11431 if (cfun_fpr_save_p (i))
11433 restore_fpr (frame_pointer,
11434 offset + next_offset, i);
11435 cfa_restores
11436 = alloc_reg_note (REG_CFA_RESTORE,
11437 gen_rtx_REG (DFmode, i), cfa_restores);
11438 next_offset += 8;
11444 else
11446 next_offset = cfun_frame_layout.f4_offset;
11447 /* f4, f6 */
11448 for (i = FPR4_REGNUM; i <= FPR4_REGNUM + 1; i++)
11450 if (cfun_fpr_save_p (i))
11452 restore_fpr (frame_pointer,
11453 offset + next_offset, i);
11454 cfa_restores
11455 = alloc_reg_note (REG_CFA_RESTORE,
11456 gen_rtx_REG (DFmode, i), cfa_restores);
11457 next_offset += 8;
11459 else if (!TARGET_PACKED_STACK)
11460 next_offset += 8;
11465 /* Return register. */
11467 return_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
11469 /* Restore call saved gprs. */
11471 if (cfun_frame_layout.first_restore_gpr != -1)
11473 rtx insn, addr;
11474 int i;
11476 /* Check for global register and save them
11477 to stack location from where they get restored. */
11479 for (i = cfun_frame_layout.first_restore_gpr;
11480 i <= cfun_frame_layout.last_restore_gpr;
11481 i++)
11483 if (global_not_special_regno_p (i))
11485 addr = plus_constant (Pmode, frame_pointer,
11486 offset + cfun_frame_layout.gprs_offset
11487 + (i - cfun_frame_layout.first_save_gpr_slot)
11488 * UNITS_PER_LONG);
11489 addr = gen_rtx_MEM (Pmode, addr);
11490 set_mem_alias_set (addr, get_frame_alias_set ());
11491 emit_move_insn (addr, gen_rtx_REG (Pmode, i));
11493 else
11494 cfa_restores
11495 = alloc_reg_note (REG_CFA_RESTORE,
11496 gen_rtx_REG (Pmode, i), cfa_restores);
11499 /* Fetch return address from stack before load multiple,
11500 this will do good for scheduling.
11502 Only do this if we already decided that r14 needs to be
11503 saved to a stack slot. (And not just because r14 happens to
11504 be in between two GPRs which need saving.) Otherwise it
11505 would be difficult to take that decision back in
11506 s390_optimize_prologue.
11508 This optimization is only helpful on in-order machines. */
11509 if (! sibcall
11510 && cfun_gpr_save_slot (RETURN_REGNUM) == SAVE_SLOT_STACK
11511 && s390_tune <= PROCESSOR_2097_Z10)
11513 int return_regnum = find_unused_clobbered_reg();
11514 if (!return_regnum)
11515 return_regnum = 4;
11516 return_reg = gen_rtx_REG (Pmode, return_regnum);
11518 addr = plus_constant (Pmode, frame_pointer,
11519 offset + cfun_frame_layout.gprs_offset
11520 + (RETURN_REGNUM
11521 - cfun_frame_layout.first_save_gpr_slot)
11522 * UNITS_PER_LONG);
11523 addr = gen_rtx_MEM (Pmode, addr);
11524 set_mem_alias_set (addr, get_frame_alias_set ());
11525 emit_move_insn (return_reg, addr);
11527 /* Once we did that optimization we have to make sure
11528 s390_optimize_prologue does not try to remove the store
11529 of r14 since we will not be able to find the load issued
11530 here. */
11531 cfun_frame_layout.save_return_addr_p = true;
11534 insn = restore_gprs (frame_pointer,
11535 offset + cfun_frame_layout.gprs_offset
11536 + (cfun_frame_layout.first_restore_gpr
11537 - cfun_frame_layout.first_save_gpr_slot)
11538 * UNITS_PER_LONG,
11539 cfun_frame_layout.first_restore_gpr,
11540 cfun_frame_layout.last_restore_gpr);
11541 insn = emit_insn (insn);
11542 REG_NOTES (insn) = cfa_restores;
11543 add_reg_note (insn, REG_CFA_DEF_CFA,
11544 plus_constant (Pmode, stack_pointer_rtx,
11545 STACK_POINTER_OFFSET));
11546 RTX_FRAME_RELATED_P (insn) = 1;
11549 s390_restore_gprs_from_fprs ();
11551 if (! sibcall)
11554 /* Return to caller. */
11556 p = rtvec_alloc (2);
11558 RTVEC_ELT (p, 0) = ret_rtx;
11559 RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, return_reg);
11560 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
11564 /* Implement TARGET_SET_UP_BY_PROLOGUE. */
11566 static void
11567 s300_set_up_by_prologue (hard_reg_set_container *regs)
11569 if (cfun->machine->base_reg
11570 && !call_really_used_regs[REGNO (cfun->machine->base_reg)])
11571 SET_HARD_REG_BIT (regs->set, REGNO (cfun->machine->base_reg));
11574 /* -fsplit-stack support. */
11576 /* A SYMBOL_REF for __morestack. */
11577 static GTY(()) rtx morestack_ref;
11579 /* When using -fsplit-stack, the allocation routines set a field in
11580 the TCB to the bottom of the stack plus this much space, measured
11581 in bytes. */
11583 #define SPLIT_STACK_AVAILABLE 1024
11585 /* Emit -fsplit-stack prologue, which goes before the regular function
11586 prologue. */
11588 void
11589 s390_expand_split_stack_prologue (void)
11591 rtx r1, guard, cc = NULL;
11592 rtx_insn *insn;
11593 /* Offset from thread pointer to __private_ss. */
11594 int psso = TARGET_64BIT ? 0x38 : 0x20;
11595 /* Pointer size in bytes. */
11596 /* Frame size and argument size - the two parameters to __morestack. */
11597 HOST_WIDE_INT frame_size = cfun_frame_layout.frame_size;
11598 /* Align argument size to 8 bytes - simplifies __morestack code. */
11599 HOST_WIDE_INT args_size = crtl->args.size >= 0
11600 ? ((crtl->args.size + 7) & ~7)
11601 : 0;
11602 /* Label to be called by __morestack. */
11603 rtx_code_label *call_done = NULL;
11604 rtx_code_label *parm_base = NULL;
11605 rtx tmp;
11607 gcc_assert (flag_split_stack && reload_completed);
11608 if (!TARGET_CPU_ZARCH)
11610 sorry ("CPUs older than z900 are not supported for -fsplit-stack");
11611 return;
11614 r1 = gen_rtx_REG (Pmode, 1);
11616 /* If no stack frame will be allocated, don't do anything. */
11617 if (!frame_size)
11619 if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11621 /* If va_start is used, just use r15. */
11622 emit_move_insn (r1,
11623 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
11624 GEN_INT (STACK_POINTER_OFFSET)));
11627 return;
11630 if (morestack_ref == NULL_RTX)
11632 morestack_ref = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
11633 SYMBOL_REF_FLAGS (morestack_ref) |= (SYMBOL_FLAG_LOCAL
11634 | SYMBOL_FLAG_FUNCTION);
11637 if (CONST_OK_FOR_K (frame_size) || CONST_OK_FOR_Op (frame_size))
11639 /* If frame_size will fit in an add instruction, do a stack space
11640 check, and only call __morestack if there's not enough space. */
11642 /* Get thread pointer. r1 is the only register we can always destroy - r0
11643 could contain a static chain (and cannot be used to address memory
11644 anyway), r2-r6 can contain parameters, and r6-r15 are callee-saved. */
11645 emit_move_insn (r1, gen_rtx_REG (Pmode, TP_REGNUM));
11646 /* Aim at __private_ss. */
11647 guard = gen_rtx_MEM (Pmode, plus_constant (Pmode, r1, psso));
11649 /* If less that 1kiB used, skip addition and compare directly with
11650 __private_ss. */
11651 if (frame_size > SPLIT_STACK_AVAILABLE)
11653 emit_move_insn (r1, guard);
11654 if (TARGET_64BIT)
11655 emit_insn (gen_adddi3 (r1, r1, GEN_INT (frame_size)));
11656 else
11657 emit_insn (gen_addsi3 (r1, r1, GEN_INT (frame_size)));
11658 guard = r1;
11661 /* Compare the (maybe adjusted) guard with the stack pointer. */
11662 cc = s390_emit_compare (LT, stack_pointer_rtx, guard);
11665 call_done = gen_label_rtx ();
11666 parm_base = gen_label_rtx ();
11668 /* Emit the parameter block. */
11669 tmp = gen_split_stack_data (parm_base, call_done,
11670 GEN_INT (frame_size),
11671 GEN_INT (args_size));
11672 insn = emit_insn (tmp);
11673 add_reg_note (insn, REG_LABEL_OPERAND, call_done);
11674 LABEL_NUSES (call_done)++;
11675 add_reg_note (insn, REG_LABEL_OPERAND, parm_base);
11676 LABEL_NUSES (parm_base)++;
11678 /* %r1 = litbase. */
11679 insn = emit_move_insn (r1, gen_rtx_LABEL_REF (VOIDmode, parm_base));
11680 add_reg_note (insn, REG_LABEL_OPERAND, parm_base);
11681 LABEL_NUSES (parm_base)++;
11683 /* Now, we need to call __morestack. It has very special calling
11684 conventions: it preserves param/return/static chain registers for
11685 calling main function body, and looks for its own parameters at %r1. */
11687 if (cc != NULL)
11689 tmp = gen_split_stack_cond_call (morestack_ref, cc, call_done);
11691 insn = emit_jump_insn (tmp);
11692 JUMP_LABEL (insn) = call_done;
11693 LABEL_NUSES (call_done)++;
11695 /* Mark the jump as very unlikely to be taken. */
11696 add_reg_br_prob_note (insn,
11697 profile_probability::very_unlikely ());
11699 if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11701 /* If va_start is used, and __morestack was not called, just use
11702 r15. */
11703 emit_move_insn (r1,
11704 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
11705 GEN_INT (STACK_POINTER_OFFSET)));
11708 else
11710 tmp = gen_split_stack_call (morestack_ref, call_done);
11711 insn = emit_jump_insn (tmp);
11712 JUMP_LABEL (insn) = call_done;
11713 LABEL_NUSES (call_done)++;
11714 emit_barrier ();
11717 /* __morestack will call us here. */
11719 emit_label (call_done);
11722 /* We may have to tell the dataflow pass that the split stack prologue
11723 is initializing a register. */
11725 static void
11726 s390_live_on_entry (bitmap regs)
11728 if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11730 gcc_assert (flag_split_stack);
11731 bitmap_set_bit (regs, 1);
11735 /* Return true if the function can use simple_return to return outside
11736 of a shrink-wrapped region. At present shrink-wrapping is supported
11737 in all cases. */
11739 bool
11740 s390_can_use_simple_return_insn (void)
11742 return true;
11745 /* Return true if the epilogue is guaranteed to contain only a return
11746 instruction and if a direct return can therefore be used instead.
11747 One of the main advantages of using direct return instructions
11748 is that we can then use conditional returns. */
11750 bool
11751 s390_can_use_return_insn (void)
11753 int i;
11755 if (!reload_completed)
11756 return false;
11758 if (crtl->profile)
11759 return false;
11761 if (TARGET_TPF_PROFILING)
11762 return false;
11764 for (i = 0; i < 16; i++)
11765 if (cfun_gpr_save_slot (i) != SAVE_SLOT_NONE)
11766 return false;
11768 /* For 31 bit this is not covered by the frame_size check below
11769 since f4, f6 are saved in the register save area without needing
11770 additional stack space. */
11771 if (!TARGET_64BIT
11772 && (cfun_fpr_save_p (FPR4_REGNUM) || cfun_fpr_save_p (FPR6_REGNUM)))
11773 return false;
11775 if (cfun->machine->base_reg
11776 && !call_really_used_regs[REGNO (cfun->machine->base_reg)])
11777 return false;
11779 return cfun_frame_layout.frame_size == 0;
11782 /* The VX ABI differs for vararg functions. Therefore we need the
11783 prototype of the callee to be available when passing vector type
11784 values. */
11785 static const char *
11786 s390_invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
11788 return ((TARGET_VX_ABI
11789 && typelist == 0
11790 && VECTOR_TYPE_P (TREE_TYPE (val))
11791 && (funcdecl == NULL_TREE
11792 || (TREE_CODE (funcdecl) == FUNCTION_DECL
11793 && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
11794 ? N_("vector argument passed to unprototyped function")
11795 : NULL);
11799 /* Return the size in bytes of a function argument of
11800 type TYPE and/or mode MODE. At least one of TYPE or
11801 MODE must be specified. */
11803 static int
11804 s390_function_arg_size (machine_mode mode, const_tree type)
11806 if (type)
11807 return int_size_in_bytes (type);
11809 /* No type info available for some library calls ... */
11810 if (mode != BLKmode)
11811 return GET_MODE_SIZE (mode);
11813 /* If we have neither type nor mode, abort */
11814 gcc_unreachable ();
11817 /* Return true if a function argument of type TYPE and mode MODE
11818 is to be passed in a vector register, if available. */
11820 bool
11821 s390_function_arg_vector (machine_mode mode, const_tree type)
11823 if (!TARGET_VX_ABI)
11824 return false;
11826 if (s390_function_arg_size (mode, type) > 16)
11827 return false;
11829 /* No type info available for some library calls ... */
11830 if (!type)
11831 return VECTOR_MODE_P (mode);
11833 /* The ABI says that record types with a single member are treated
11834 just like that member would be. */
11835 while (TREE_CODE (type) == RECORD_TYPE)
11837 tree field, single = NULL_TREE;
11839 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11841 if (TREE_CODE (field) != FIELD_DECL)
11842 continue;
11844 if (single == NULL_TREE)
11845 single = TREE_TYPE (field);
11846 else
11847 return false;
11850 if (single == NULL_TREE)
11851 return false;
11852 else
11854 /* If the field declaration adds extra byte due to
11855 e.g. padding this is not accepted as vector type. */
11856 if (int_size_in_bytes (single) <= 0
11857 || int_size_in_bytes (single) != int_size_in_bytes (type))
11858 return false;
11859 type = single;
11863 return VECTOR_TYPE_P (type);
11866 /* Return true if a function argument of type TYPE and mode MODE
11867 is to be passed in a floating-point register, if available. */
11869 static bool
11870 s390_function_arg_float (machine_mode mode, const_tree type)
11872 if (s390_function_arg_size (mode, type) > 8)
11873 return false;
11875 /* Soft-float changes the ABI: no floating-point registers are used. */
11876 if (TARGET_SOFT_FLOAT)
11877 return false;
11879 /* No type info available for some library calls ... */
11880 if (!type)
11881 return mode == SFmode || mode == DFmode || mode == SDmode || mode == DDmode;
11883 /* The ABI says that record types with a single member are treated
11884 just like that member would be. */
11885 while (TREE_CODE (type) == RECORD_TYPE)
11887 tree field, single = NULL_TREE;
11889 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11891 if (TREE_CODE (field) != FIELD_DECL)
11892 continue;
11894 if (single == NULL_TREE)
11895 single = TREE_TYPE (field);
11896 else
11897 return false;
11900 if (single == NULL_TREE)
11901 return false;
11902 else
11903 type = single;
11906 return TREE_CODE (type) == REAL_TYPE;
11909 /* Return true if a function argument of type TYPE and mode MODE
11910 is to be passed in an integer register, or a pair of integer
11911 registers, if available. */
11913 static bool
11914 s390_function_arg_integer (machine_mode mode, const_tree type)
11916 int size = s390_function_arg_size (mode, type);
11917 if (size > 8)
11918 return false;
11920 /* No type info available for some library calls ... */
11921 if (!type)
11922 return GET_MODE_CLASS (mode) == MODE_INT
11923 || (TARGET_SOFT_FLOAT && SCALAR_FLOAT_MODE_P (mode));
11925 /* We accept small integral (and similar) types. */
11926 if (INTEGRAL_TYPE_P (type)
11927 || POINTER_TYPE_P (type)
11928 || TREE_CODE (type) == NULLPTR_TYPE
11929 || TREE_CODE (type) == OFFSET_TYPE
11930 || (TARGET_SOFT_FLOAT && TREE_CODE (type) == REAL_TYPE))
11931 return true;
11933 /* We also accept structs of size 1, 2, 4, 8 that are not
11934 passed in floating-point registers. */
11935 if (AGGREGATE_TYPE_P (type)
11936 && exact_log2 (size) >= 0
11937 && !s390_function_arg_float (mode, type))
11938 return true;
11940 return false;
11943 /* Return 1 if a function argument of type TYPE and mode MODE
11944 is to be passed by reference. The ABI specifies that only
11945 structures of size 1, 2, 4, or 8 bytes are passed by value,
11946 all other structures (and complex numbers) are passed by
11947 reference. */
11949 static bool
11950 s390_pass_by_reference (cumulative_args_t ca ATTRIBUTE_UNUSED,
11951 machine_mode mode, const_tree type,
11952 bool named ATTRIBUTE_UNUSED)
11954 int size = s390_function_arg_size (mode, type);
11956 if (s390_function_arg_vector (mode, type))
11957 return false;
11959 if (size > 8)
11960 return true;
11962 if (type)
11964 if (AGGREGATE_TYPE_P (type) && exact_log2 (size) < 0)
11965 return true;
11967 if (TREE_CODE (type) == COMPLEX_TYPE
11968 || TREE_CODE (type) == VECTOR_TYPE)
11969 return true;
11972 return false;
11975 /* Update the data in CUM to advance over an argument of mode MODE and
11976 data type TYPE. (TYPE is null for libcalls where that information
11977 may not be available.). The boolean NAMED specifies whether the
11978 argument is a named argument (as opposed to an unnamed argument
11979 matching an ellipsis). */
11981 static void
11982 s390_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
11983 const_tree type, bool named)
11985 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
11987 if (s390_function_arg_vector (mode, type))
11989 /* We are called for unnamed vector stdarg arguments which are
11990 passed on the stack. In this case this hook does not have to
11991 do anything since stack arguments are tracked by common
11992 code. */
11993 if (!named)
11994 return;
11995 cum->vrs += 1;
11997 else if (s390_function_arg_float (mode, type))
11999 cum->fprs += 1;
12001 else if (s390_function_arg_integer (mode, type))
12003 int size = s390_function_arg_size (mode, type);
12004 cum->gprs += ((size + UNITS_PER_LONG - 1) / UNITS_PER_LONG);
12006 else
12007 gcc_unreachable ();
12010 /* Define where to put the arguments to a function.
12011 Value is zero to push the argument on the stack,
12012 or a hard register in which to store the argument.
12014 MODE is the argument's machine mode.
12015 TYPE is the data type of the argument (as a tree).
12016 This is null for libcalls where that information may
12017 not be available.
12018 CUM is a variable of type CUMULATIVE_ARGS which gives info about
12019 the preceding args and about the function being called.
12020 NAMED is nonzero if this argument is a named parameter
12021 (otherwise it is an extra parameter matching an ellipsis).
12023 On S/390, we use general purpose registers 2 through 6 to
12024 pass integer, pointer, and certain structure arguments, and
12025 floating point registers 0 and 2 (0, 2, 4, and 6 on 64-bit)
12026 to pass floating point arguments. All remaining arguments
12027 are pushed to the stack. */
12029 static rtx
12030 s390_function_arg (cumulative_args_t cum_v, machine_mode mode,
12031 const_tree type, bool named)
12033 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
12035 if (!named)
12036 s390_check_type_for_vector_abi (type, true, false);
12038 if (s390_function_arg_vector (mode, type))
12040 /* Vector arguments being part of the ellipsis are passed on the
12041 stack. */
12042 if (!named || (cum->vrs + 1 > VEC_ARG_NUM_REG))
12043 return NULL_RTX;
12045 return gen_rtx_REG (mode, cum->vrs + FIRST_VEC_ARG_REGNO);
12047 else if (s390_function_arg_float (mode, type))
12049 if (cum->fprs + 1 > FP_ARG_NUM_REG)
12050 return NULL_RTX;
12051 else
12052 return gen_rtx_REG (mode, cum->fprs + 16);
12054 else if (s390_function_arg_integer (mode, type))
12056 int size = s390_function_arg_size (mode, type);
12057 int n_gprs = (size + UNITS_PER_LONG - 1) / UNITS_PER_LONG;
12059 if (cum->gprs + n_gprs > GP_ARG_NUM_REG)
12060 return NULL_RTX;
12061 else if (n_gprs == 1 || UNITS_PER_WORD == UNITS_PER_LONG)
12062 return gen_rtx_REG (mode, cum->gprs + 2);
12063 else if (n_gprs == 2)
12065 rtvec p = rtvec_alloc (2);
12067 RTVEC_ELT (p, 0)
12068 = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, cum->gprs + 2),
12069 const0_rtx);
12070 RTVEC_ELT (p, 1)
12071 = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, cum->gprs + 3),
12072 GEN_INT (4));
12074 return gen_rtx_PARALLEL (mode, p);
12078 /* After the real arguments, expand_call calls us once again
12079 with a void_type_node type. Whatever we return here is
12080 passed as operand 2 to the call expanders.
12082 We don't need this feature ... */
12083 else if (type == void_type_node)
12084 return const0_rtx;
12086 gcc_unreachable ();
12089 /* Return true if return values of type TYPE should be returned
12090 in a memory buffer whose address is passed by the caller as
12091 hidden first argument. */
12093 static bool
12094 s390_return_in_memory (const_tree type, const_tree fundecl ATTRIBUTE_UNUSED)
12096 /* We accept small integral (and similar) types. */
12097 if (INTEGRAL_TYPE_P (type)
12098 || POINTER_TYPE_P (type)
12099 || TREE_CODE (type) == OFFSET_TYPE
12100 || TREE_CODE (type) == REAL_TYPE)
12101 return int_size_in_bytes (type) > 8;
12103 /* vector types which fit into a VR. */
12104 if (TARGET_VX_ABI
12105 && VECTOR_TYPE_P (type)
12106 && int_size_in_bytes (type) <= 16)
12107 return false;
12109 /* Aggregates and similar constructs are always returned
12110 in memory. */
12111 if (AGGREGATE_TYPE_P (type)
12112 || TREE_CODE (type) == COMPLEX_TYPE
12113 || VECTOR_TYPE_P (type))
12114 return true;
12116 /* ??? We get called on all sorts of random stuff from
12117 aggregate_value_p. We can't abort, but it's not clear
12118 what's safe to return. Pretend it's a struct I guess. */
12119 return true;
12122 /* Function arguments and return values are promoted to word size. */
12124 static machine_mode
12125 s390_promote_function_mode (const_tree type, machine_mode mode,
12126 int *punsignedp,
12127 const_tree fntype ATTRIBUTE_UNUSED,
12128 int for_return ATTRIBUTE_UNUSED)
12130 if (INTEGRAL_MODE_P (mode)
12131 && GET_MODE_SIZE (mode) < UNITS_PER_LONG)
12133 if (type != NULL_TREE && POINTER_TYPE_P (type))
12134 *punsignedp = POINTERS_EXTEND_UNSIGNED;
12135 return Pmode;
12138 return mode;
12141 /* Define where to return a (scalar) value of type RET_TYPE.
12142 If RET_TYPE is null, define where to return a (scalar)
12143 value of mode MODE from a libcall. */
12145 static rtx
12146 s390_function_and_libcall_value (machine_mode mode,
12147 const_tree ret_type,
12148 const_tree fntype_or_decl,
12149 bool outgoing ATTRIBUTE_UNUSED)
12151 /* For vector return types it is important to use the RET_TYPE
12152 argument whenever available since the middle-end might have
12153 changed the mode to a scalar mode. */
12154 bool vector_ret_type_p = ((ret_type && VECTOR_TYPE_P (ret_type))
12155 || (!ret_type && VECTOR_MODE_P (mode)));
12157 /* For normal functions perform the promotion as
12158 promote_function_mode would do. */
12159 if (ret_type)
12161 int unsignedp = TYPE_UNSIGNED (ret_type);
12162 mode = promote_function_mode (ret_type, mode, &unsignedp,
12163 fntype_or_decl, 1);
12166 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT
12167 || SCALAR_FLOAT_MODE_P (mode)
12168 || (TARGET_VX_ABI && vector_ret_type_p));
12169 gcc_assert (GET_MODE_SIZE (mode) <= (TARGET_VX_ABI ? 16 : 8));
12171 if (TARGET_VX_ABI && vector_ret_type_p)
12172 return gen_rtx_REG (mode, FIRST_VEC_ARG_REGNO);
12173 else if (TARGET_HARD_FLOAT && SCALAR_FLOAT_MODE_P (mode))
12174 return gen_rtx_REG (mode, 16);
12175 else if (GET_MODE_SIZE (mode) <= UNITS_PER_LONG
12176 || UNITS_PER_LONG == UNITS_PER_WORD)
12177 return gen_rtx_REG (mode, 2);
12178 else if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_LONG)
12180 /* This case is triggered when returning a 64 bit value with
12181 -m31 -mzarch. Although the value would fit into a single
12182 register it has to be forced into a 32 bit register pair in
12183 order to match the ABI. */
12184 rtvec p = rtvec_alloc (2);
12186 RTVEC_ELT (p, 0)
12187 = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, 2), const0_rtx);
12188 RTVEC_ELT (p, 1)
12189 = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, 3), GEN_INT (4));
12191 return gen_rtx_PARALLEL (mode, p);
12194 gcc_unreachable ();
12197 /* Define where to return a scalar return value of type RET_TYPE. */
12199 static rtx
12200 s390_function_value (const_tree ret_type, const_tree fn_decl_or_type,
12201 bool outgoing)
12203 return s390_function_and_libcall_value (TYPE_MODE (ret_type), ret_type,
12204 fn_decl_or_type, outgoing);
12207 /* Define where to return a scalar libcall return value of mode
12208 MODE. */
12210 static rtx
12211 s390_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
12213 return s390_function_and_libcall_value (mode, NULL_TREE,
12214 NULL_TREE, true);
12218 /* Create and return the va_list datatype.
12220 On S/390, va_list is an array type equivalent to
12222 typedef struct __va_list_tag
12224 long __gpr;
12225 long __fpr;
12226 void *__overflow_arg_area;
12227 void *__reg_save_area;
12228 } va_list[1];
12230 where __gpr and __fpr hold the number of general purpose
12231 or floating point arguments used up to now, respectively,
12232 __overflow_arg_area points to the stack location of the
12233 next argument passed on the stack, and __reg_save_area
12234 always points to the start of the register area in the
12235 call frame of the current function. The function prologue
12236 saves all registers used for argument passing into this
12237 area if the function uses variable arguments. */
12239 static tree
12240 s390_build_builtin_va_list (void)
12242 tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
12244 record = lang_hooks.types.make_type (RECORD_TYPE);
12246 type_decl =
12247 build_decl (BUILTINS_LOCATION,
12248 TYPE_DECL, get_identifier ("__va_list_tag"), record);
12250 f_gpr = build_decl (BUILTINS_LOCATION,
12251 FIELD_DECL, get_identifier ("__gpr"),
12252 long_integer_type_node);
12253 f_fpr = build_decl (BUILTINS_LOCATION,
12254 FIELD_DECL, get_identifier ("__fpr"),
12255 long_integer_type_node);
12256 f_ovf = build_decl (BUILTINS_LOCATION,
12257 FIELD_DECL, get_identifier ("__overflow_arg_area"),
12258 ptr_type_node);
12259 f_sav = build_decl (BUILTINS_LOCATION,
12260 FIELD_DECL, get_identifier ("__reg_save_area"),
12261 ptr_type_node);
12263 va_list_gpr_counter_field = f_gpr;
12264 va_list_fpr_counter_field = f_fpr;
12266 DECL_FIELD_CONTEXT (f_gpr) = record;
12267 DECL_FIELD_CONTEXT (f_fpr) = record;
12268 DECL_FIELD_CONTEXT (f_ovf) = record;
12269 DECL_FIELD_CONTEXT (f_sav) = record;
12271 TYPE_STUB_DECL (record) = type_decl;
12272 TYPE_NAME (record) = type_decl;
12273 TYPE_FIELDS (record) = f_gpr;
12274 DECL_CHAIN (f_gpr) = f_fpr;
12275 DECL_CHAIN (f_fpr) = f_ovf;
12276 DECL_CHAIN (f_ovf) = f_sav;
12278 layout_type (record);
12280 /* The correct type is an array type of one element. */
12281 return build_array_type (record, build_index_type (size_zero_node));
12284 /* Implement va_start by filling the va_list structure VALIST.
12285 STDARG_P is always true, and ignored.
12286 NEXTARG points to the first anonymous stack argument.
12288 The following global variables are used to initialize
12289 the va_list structure:
12291 crtl->args.info:
12292 holds number of gprs and fprs used for named arguments.
12293 crtl->args.arg_offset_rtx:
12294 holds the offset of the first anonymous stack argument
12295 (relative to the virtual arg pointer). */
12297 static void
12298 s390_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
12300 HOST_WIDE_INT n_gpr, n_fpr;
12301 int off;
12302 tree f_gpr, f_fpr, f_ovf, f_sav;
12303 tree gpr, fpr, ovf, sav, t;
12305 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
12306 f_fpr = DECL_CHAIN (f_gpr);
12307 f_ovf = DECL_CHAIN (f_fpr);
12308 f_sav = DECL_CHAIN (f_ovf);
12310 valist = build_simple_mem_ref (valist);
12311 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
12312 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
12313 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
12314 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
12316 /* Count number of gp and fp argument registers used. */
12318 n_gpr = crtl->args.info.gprs;
12319 n_fpr = crtl->args.info.fprs;
12321 if (cfun->va_list_gpr_size)
12323 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
12324 build_int_cst (NULL_TREE, n_gpr));
12325 TREE_SIDE_EFFECTS (t) = 1;
12326 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
12329 if (cfun->va_list_fpr_size)
12331 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
12332 build_int_cst (NULL_TREE, n_fpr));
12333 TREE_SIDE_EFFECTS (t) = 1;
12334 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
12337 if (flag_split_stack
12338 && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
12339 == NULL)
12340 && cfun->machine->split_stack_varargs_pointer == NULL_RTX)
12342 rtx reg;
12343 rtx_insn *seq;
12345 reg = gen_reg_rtx (Pmode);
12346 cfun->machine->split_stack_varargs_pointer = reg;
12348 start_sequence ();
12349 emit_move_insn (reg, gen_rtx_REG (Pmode, 1));
12350 seq = get_insns ();
12351 end_sequence ();
12353 push_topmost_sequence ();
12354 emit_insn_after (seq, entry_of_function ());
12355 pop_topmost_sequence ();
12358 /* Find the overflow area.
12359 FIXME: This currently is too pessimistic when the vector ABI is
12360 enabled. In that case we *always* set up the overflow area
12361 pointer. */
12362 if (n_gpr + cfun->va_list_gpr_size > GP_ARG_NUM_REG
12363 || n_fpr + cfun->va_list_fpr_size > FP_ARG_NUM_REG
12364 || TARGET_VX_ABI)
12366 if (cfun->machine->split_stack_varargs_pointer == NULL_RTX)
12367 t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
12368 else
12369 t = make_tree (TREE_TYPE (ovf), cfun->machine->split_stack_varargs_pointer);
12371 off = INTVAL (crtl->args.arg_offset_rtx);
12372 off = off < 0 ? 0 : off;
12373 if (TARGET_DEBUG_ARG)
12374 fprintf (stderr, "va_start: n_gpr = %d, n_fpr = %d off %d\n",
12375 (int)n_gpr, (int)n_fpr, off);
12377 t = fold_build_pointer_plus_hwi (t, off);
12379 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
12380 TREE_SIDE_EFFECTS (t) = 1;
12381 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
12384 /* Find the register save area. */
12385 if ((cfun->va_list_gpr_size && n_gpr < GP_ARG_NUM_REG)
12386 || (cfun->va_list_fpr_size && n_fpr < FP_ARG_NUM_REG))
12388 t = make_tree (TREE_TYPE (sav), return_address_pointer_rtx);
12389 t = fold_build_pointer_plus_hwi (t, -RETURN_REGNUM * UNITS_PER_LONG);
12391 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
12392 TREE_SIDE_EFFECTS (t) = 1;
12393 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
12397 /* Implement va_arg by updating the va_list structure
12398 VALIST as required to retrieve an argument of type
12399 TYPE, and returning that argument.
12401 Generates code equivalent to:
12403 if (integral value) {
12404 if (size <= 4 && args.gpr < 5 ||
12405 size > 4 && args.gpr < 4 )
12406 ret = args.reg_save_area[args.gpr+8]
12407 else
12408 ret = *args.overflow_arg_area++;
12409 } else if (vector value) {
12410 ret = *args.overflow_arg_area;
12411 args.overflow_arg_area += size / 8;
12412 } else if (float value) {
12413 if (args.fgpr < 2)
12414 ret = args.reg_save_area[args.fpr+64]
12415 else
12416 ret = *args.overflow_arg_area++;
12417 } else if (aggregate value) {
12418 if (args.gpr < 5)
12419 ret = *args.reg_save_area[args.gpr]
12420 else
12421 ret = **args.overflow_arg_area++;
12422 } */
12424 static tree
12425 s390_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
12426 gimple_seq *post_p ATTRIBUTE_UNUSED)
12428 tree f_gpr, f_fpr, f_ovf, f_sav;
12429 tree gpr, fpr, ovf, sav, reg, t, u;
12430 int indirect_p, size, n_reg, sav_ofs, sav_scale, max_reg;
12431 tree lab_false, lab_over = NULL_TREE;
12432 tree addr = create_tmp_var (ptr_type_node, "addr");
12433 bool left_align_p; /* How a value < UNITS_PER_LONG is aligned within
12434 a stack slot. */
12436 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
12437 f_fpr = DECL_CHAIN (f_gpr);
12438 f_ovf = DECL_CHAIN (f_fpr);
12439 f_sav = DECL_CHAIN (f_ovf);
12441 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
12442 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
12443 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
12445 /* The tree for args* cannot be shared between gpr/fpr and ovf since
12446 both appear on a lhs. */
12447 valist = unshare_expr (valist);
12448 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
12450 size = int_size_in_bytes (type);
12452 s390_check_type_for_vector_abi (type, true, false);
12454 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
12456 if (TARGET_DEBUG_ARG)
12458 fprintf (stderr, "va_arg: aggregate type");
12459 debug_tree (type);
12462 /* Aggregates are passed by reference. */
12463 indirect_p = 1;
12464 reg = gpr;
12465 n_reg = 1;
12467 /* kernel stack layout on 31 bit: It is assumed here that no padding
12468 will be added by s390_frame_info because for va_args always an even
12469 number of gprs has to be saved r15-r2 = 14 regs. */
12470 sav_ofs = 2 * UNITS_PER_LONG;
12471 sav_scale = UNITS_PER_LONG;
12472 size = UNITS_PER_LONG;
12473 max_reg = GP_ARG_NUM_REG - n_reg;
12474 left_align_p = false;
12476 else if (s390_function_arg_vector (TYPE_MODE (type), type))
12478 if (TARGET_DEBUG_ARG)
12480 fprintf (stderr, "va_arg: vector type");
12481 debug_tree (type);
12484 indirect_p = 0;
12485 reg = NULL_TREE;
12486 n_reg = 0;
12487 sav_ofs = 0;
12488 sav_scale = 8;
12489 max_reg = 0;
12490 left_align_p = true;
12492 else if (s390_function_arg_float (TYPE_MODE (type), type))
12494 if (TARGET_DEBUG_ARG)
12496 fprintf (stderr, "va_arg: float type");
12497 debug_tree (type);
12500 /* FP args go in FP registers, if present. */
12501 indirect_p = 0;
12502 reg = fpr;
12503 n_reg = 1;
12504 sav_ofs = 16 * UNITS_PER_LONG;
12505 sav_scale = 8;
12506 max_reg = FP_ARG_NUM_REG - n_reg;
12507 left_align_p = false;
12509 else
12511 if (TARGET_DEBUG_ARG)
12513 fprintf (stderr, "va_arg: other type");
12514 debug_tree (type);
12517 /* Otherwise into GP registers. */
12518 indirect_p = 0;
12519 reg = gpr;
12520 n_reg = (size + UNITS_PER_LONG - 1) / UNITS_PER_LONG;
12522 /* kernel stack layout on 31 bit: It is assumed here that no padding
12523 will be added by s390_frame_info because for va_args always an even
12524 number of gprs has to be saved r15-r2 = 14 regs. */
12525 sav_ofs = 2 * UNITS_PER_LONG;
12527 if (size < UNITS_PER_LONG)
12528 sav_ofs += UNITS_PER_LONG - size;
12530 sav_scale = UNITS_PER_LONG;
12531 max_reg = GP_ARG_NUM_REG - n_reg;
12532 left_align_p = false;
12535 /* Pull the value out of the saved registers ... */
12537 if (reg != NULL_TREE)
12540 if (reg > ((typeof (reg))max_reg))
12541 goto lab_false;
12543 addr = sav + sav_ofs + reg * save_scale;
12545 goto lab_over;
12547 lab_false:
12550 lab_false = create_artificial_label (UNKNOWN_LOCATION);
12551 lab_over = create_artificial_label (UNKNOWN_LOCATION);
12553 t = fold_convert (TREE_TYPE (reg), size_int (max_reg));
12554 t = build2 (GT_EXPR, boolean_type_node, reg, t);
12555 u = build1 (GOTO_EXPR, void_type_node, lab_false);
12556 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
12557 gimplify_and_add (t, pre_p);
12559 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
12560 u = build2 (MULT_EXPR, TREE_TYPE (reg), reg,
12561 fold_convert (TREE_TYPE (reg), size_int (sav_scale)));
12562 t = fold_build_pointer_plus (t, u);
12564 gimplify_assign (addr, t, pre_p);
12566 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
12568 gimple_seq_add_stmt (pre_p, gimple_build_label (lab_false));
12571 /* ... Otherwise out of the overflow area. */
12573 t = ovf;
12574 if (size < UNITS_PER_LONG && !left_align_p)
12575 t = fold_build_pointer_plus_hwi (t, UNITS_PER_LONG - size);
12577 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
12579 gimplify_assign (addr, t, pre_p);
12581 if (size < UNITS_PER_LONG && left_align_p)
12582 t = fold_build_pointer_plus_hwi (t, UNITS_PER_LONG);
12583 else
12584 t = fold_build_pointer_plus_hwi (t, size);
12586 gimplify_assign (ovf, t, pre_p);
12588 if (reg != NULL_TREE)
12589 gimple_seq_add_stmt (pre_p, gimple_build_label (lab_over));
12592 /* Increment register save count. */
12594 if (n_reg > 0)
12596 u = build2 (PREINCREMENT_EXPR, TREE_TYPE (reg), reg,
12597 fold_convert (TREE_TYPE (reg), size_int (n_reg)));
12598 gimplify_and_add (u, pre_p);
12601 if (indirect_p)
12603 t = build_pointer_type_for_mode (build_pointer_type (type),
12604 ptr_mode, true);
12605 addr = fold_convert (t, addr);
12606 addr = build_va_arg_indirect_ref (addr);
12608 else
12610 t = build_pointer_type_for_mode (type, ptr_mode, true);
12611 addr = fold_convert (t, addr);
12614 return build_va_arg_indirect_ref (addr);
12617 /* Emit rtl for the tbegin or tbegin_retry (RETRY != NULL_RTX)
12618 expanders.
12619 DEST - Register location where CC will be stored.
12620 TDB - Pointer to a 256 byte area where to store the transaction.
12621 diagnostic block. NULL if TDB is not needed.
12622 RETRY - Retry count value. If non-NULL a retry loop for CC2
12623 is emitted
12624 CLOBBER_FPRS_P - If true clobbers for all FPRs are emitted as part
12625 of the tbegin instruction pattern. */
12627 void
12628 s390_expand_tbegin (rtx dest, rtx tdb, rtx retry, bool clobber_fprs_p)
12630 rtx retry_plus_two = gen_reg_rtx (SImode);
12631 rtx retry_reg = gen_reg_rtx (SImode);
12632 rtx_code_label *retry_label = NULL;
12634 if (retry != NULL_RTX)
12636 emit_move_insn (retry_reg, retry);
12637 emit_insn (gen_addsi3 (retry_plus_two, retry_reg, const2_rtx));
12638 emit_insn (gen_addsi3 (retry_reg, retry_reg, const1_rtx));
12639 retry_label = gen_label_rtx ();
12640 emit_label (retry_label);
12643 if (clobber_fprs_p)
12645 if (TARGET_VX)
12646 emit_insn (gen_tbegin_1_z13 (gen_rtx_CONST_INT (VOIDmode, TBEGIN_MASK),
12647 tdb));
12648 else
12649 emit_insn (gen_tbegin_1 (gen_rtx_CONST_INT (VOIDmode, TBEGIN_MASK),
12650 tdb));
12652 else
12653 emit_insn (gen_tbegin_nofloat_1 (gen_rtx_CONST_INT (VOIDmode, TBEGIN_MASK),
12654 tdb));
12656 emit_move_insn (dest, gen_rtx_UNSPEC (SImode,
12657 gen_rtvec (1, gen_rtx_REG (CCRAWmode,
12658 CC_REGNUM)),
12659 UNSPEC_CC_TO_INT));
12660 if (retry != NULL_RTX)
12662 const int CC0 = 1 << 3;
12663 const int CC1 = 1 << 2;
12664 const int CC3 = 1 << 0;
12665 rtx jump;
12666 rtx count = gen_reg_rtx (SImode);
12667 rtx_code_label *leave_label = gen_label_rtx ();
12669 /* Exit for success and permanent failures. */
12670 jump = s390_emit_jump (leave_label,
12671 gen_rtx_EQ (VOIDmode,
12672 gen_rtx_REG (CCRAWmode, CC_REGNUM),
12673 gen_rtx_CONST_INT (VOIDmode, CC0 | CC1 | CC3)));
12674 LABEL_NUSES (leave_label) = 1;
12676 /* CC2 - transient failure. Perform retry with ppa. */
12677 emit_move_insn (count, retry_plus_two);
12678 emit_insn (gen_subsi3 (count, count, retry_reg));
12679 emit_insn (gen_tx_assist (count));
12680 jump = emit_jump_insn (gen_doloop_si64 (retry_label,
12681 retry_reg,
12682 retry_reg));
12683 JUMP_LABEL (jump) = retry_label;
12684 LABEL_NUSES (retry_label) = 1;
12685 emit_label (leave_label);
12690 /* Return the decl for the target specific builtin with the function
12691 code FCODE. */
12693 static tree
12694 s390_builtin_decl (unsigned fcode, bool initialized_p ATTRIBUTE_UNUSED)
12696 if (fcode >= S390_BUILTIN_MAX)
12697 return error_mark_node;
12699 return s390_builtin_decls[fcode];
12702 /* We call mcount before the function prologue. So a profiled leaf
12703 function should stay a leaf function. */
12705 static bool
12706 s390_keep_leaf_when_profiled ()
12708 return true;
12711 /* Output assembly code for the trampoline template to
12712 stdio stream FILE.
12714 On S/390, we use gpr 1 internally in the trampoline code;
12715 gpr 0 is used to hold the static chain. */
12717 static void
12718 s390_asm_trampoline_template (FILE *file)
12720 rtx op[2];
12721 op[0] = gen_rtx_REG (Pmode, 0);
12722 op[1] = gen_rtx_REG (Pmode, 1);
12724 if (TARGET_64BIT)
12726 output_asm_insn ("basr\t%1,0", op); /* 2 byte */
12727 output_asm_insn ("lmg\t%0,%1,14(%1)", op); /* 6 byte */
12728 output_asm_insn ("br\t%1", op); /* 2 byte */
12729 ASM_OUTPUT_SKIP (file, (HOST_WIDE_INT)(TRAMPOLINE_SIZE - 10));
12731 else
12733 output_asm_insn ("basr\t%1,0", op); /* 2 byte */
12734 output_asm_insn ("lm\t%0,%1,6(%1)", op); /* 4 byte */
12735 output_asm_insn ("br\t%1", op); /* 2 byte */
12736 ASM_OUTPUT_SKIP (file, (HOST_WIDE_INT)(TRAMPOLINE_SIZE - 8));
12740 /* Emit RTL insns to initialize the variable parts of a trampoline.
12741 FNADDR is an RTX for the address of the function's pure code.
12742 CXT is an RTX for the static chain value for the function. */
12744 static void
12745 s390_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
12747 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
12748 rtx mem;
12750 emit_block_move (m_tramp, assemble_trampoline_template (),
12751 GEN_INT (2 * UNITS_PER_LONG), BLOCK_OP_NORMAL);
12753 mem = adjust_address (m_tramp, Pmode, 2 * UNITS_PER_LONG);
12754 emit_move_insn (mem, cxt);
12755 mem = adjust_address (m_tramp, Pmode, 3 * UNITS_PER_LONG);
12756 emit_move_insn (mem, fnaddr);
12759 /* Output assembler code to FILE to increment profiler label # LABELNO
12760 for profiling a function entry. */
12762 void
12763 s390_function_profiler (FILE *file, int labelno)
12765 rtx op[7];
12767 char label[128];
12768 ASM_GENERATE_INTERNAL_LABEL (label, "LP", labelno);
12770 fprintf (file, "# function profiler \n");
12772 op[0] = gen_rtx_REG (Pmode, RETURN_REGNUM);
12773 op[1] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
12774 op[1] = gen_rtx_MEM (Pmode, plus_constant (Pmode, op[1], UNITS_PER_LONG));
12776 op[2] = gen_rtx_REG (Pmode, 1);
12777 op[3] = gen_rtx_SYMBOL_REF (Pmode, label);
12778 SYMBOL_REF_FLAGS (op[3]) = SYMBOL_FLAG_LOCAL;
12780 op[4] = gen_rtx_SYMBOL_REF (Pmode, "_mcount");
12781 if (flag_pic)
12783 op[4] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op[4]), UNSPEC_PLT);
12784 op[4] = gen_rtx_CONST (Pmode, op[4]);
12787 if (TARGET_64BIT)
12789 output_asm_insn ("stg\t%0,%1", op);
12790 output_asm_insn ("larl\t%2,%3", op);
12791 output_asm_insn ("brasl\t%0,%4", op);
12792 output_asm_insn ("lg\t%0,%1", op);
12794 else if (TARGET_CPU_ZARCH)
12796 output_asm_insn ("st\t%0,%1", op);
12797 output_asm_insn ("larl\t%2,%3", op);
12798 output_asm_insn ("brasl\t%0,%4", op);
12799 output_asm_insn ("l\t%0,%1", op);
12801 else if (!flag_pic)
12803 op[6] = gen_label_rtx ();
12805 output_asm_insn ("st\t%0,%1", op);
12806 output_asm_insn ("bras\t%2,%l6", op);
12807 output_asm_insn (".long\t%4", op);
12808 output_asm_insn (".long\t%3", op);
12809 targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[6]));
12810 output_asm_insn ("l\t%0,0(%2)", op);
12811 output_asm_insn ("l\t%2,4(%2)", op);
12812 output_asm_insn ("basr\t%0,%0", op);
12813 output_asm_insn ("l\t%0,%1", op);
12815 else
12817 op[5] = gen_label_rtx ();
12818 op[6] = gen_label_rtx ();
12820 output_asm_insn ("st\t%0,%1", op);
12821 output_asm_insn ("bras\t%2,%l6", op);
12822 targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[5]));
12823 output_asm_insn (".long\t%4-%l5", op);
12824 output_asm_insn (".long\t%3-%l5", op);
12825 targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[6]));
12826 output_asm_insn ("lr\t%0,%2", op);
12827 output_asm_insn ("a\t%0,0(%2)", op);
12828 output_asm_insn ("a\t%2,4(%2)", op);
12829 output_asm_insn ("basr\t%0,%0", op);
12830 output_asm_insn ("l\t%0,%1", op);
12834 /* Encode symbol attributes (local vs. global, tls model) of a SYMBOL_REF
12835 into its SYMBOL_REF_FLAGS. */
12837 static void
12838 s390_encode_section_info (tree decl, rtx rtl, int first)
12840 default_encode_section_info (decl, rtl, first);
12842 if (TREE_CODE (decl) == VAR_DECL)
12844 /* Store the alignment to be able to check if we can use
12845 a larl/load-relative instruction. We only handle the cases
12846 that can go wrong (i.e. no FUNC_DECLs). */
12847 if (DECL_ALIGN (decl) == 0 || DECL_ALIGN (decl) % 16)
12848 SYMBOL_FLAG_SET_NOTALIGN2 (XEXP (rtl, 0));
12849 else if (DECL_ALIGN (decl) % 32)
12850 SYMBOL_FLAG_SET_NOTALIGN4 (XEXP (rtl, 0));
12851 else if (DECL_ALIGN (decl) % 64)
12852 SYMBOL_FLAG_SET_NOTALIGN8 (XEXP (rtl, 0));
12855 /* Literal pool references don't have a decl so they are handled
12856 differently here. We rely on the information in the MEM_ALIGN
12857 entry to decide upon the alignment. */
12858 if (MEM_P (rtl)
12859 && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
12860 && TREE_CONSTANT_POOL_ADDRESS_P (XEXP (rtl, 0)))
12862 if (MEM_ALIGN (rtl) == 0 || MEM_ALIGN (rtl) % 16)
12863 SYMBOL_FLAG_SET_NOTALIGN2 (XEXP (rtl, 0));
12864 else if (MEM_ALIGN (rtl) % 32)
12865 SYMBOL_FLAG_SET_NOTALIGN4 (XEXP (rtl, 0));
12866 else if (MEM_ALIGN (rtl) % 64)
12867 SYMBOL_FLAG_SET_NOTALIGN8 (XEXP (rtl, 0));
12871 /* Output thunk to FILE that implements a C++ virtual function call (with
12872 multiple inheritance) to FUNCTION. The thunk adjusts the this pointer
12873 by DELTA, and unless VCALL_OFFSET is zero, applies an additional adjustment
12874 stored at VCALL_OFFSET in the vtable whose address is located at offset 0
12875 relative to the resulting this pointer. */
12877 static void
12878 s390_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
12879 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
12880 tree function)
12882 rtx op[10];
12883 int nonlocal = 0;
12885 /* Make sure unwind info is emitted for the thunk if needed. */
12886 final_start_function (emit_barrier (), file, 1);
12888 /* Operand 0 is the target function. */
12889 op[0] = XEXP (DECL_RTL (function), 0);
12890 if (flag_pic && !SYMBOL_REF_LOCAL_P (op[0]))
12892 nonlocal = 1;
12893 op[0] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op[0]),
12894 TARGET_64BIT ? UNSPEC_PLT : UNSPEC_GOT);
12895 op[0] = gen_rtx_CONST (Pmode, op[0]);
12898 /* Operand 1 is the 'this' pointer. */
12899 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
12900 op[1] = gen_rtx_REG (Pmode, 3);
12901 else
12902 op[1] = gen_rtx_REG (Pmode, 2);
12904 /* Operand 2 is the delta. */
12905 op[2] = GEN_INT (delta);
12907 /* Operand 3 is the vcall_offset. */
12908 op[3] = GEN_INT (vcall_offset);
12910 /* Operand 4 is the temporary register. */
12911 op[4] = gen_rtx_REG (Pmode, 1);
12913 /* Operands 5 to 8 can be used as labels. */
12914 op[5] = NULL_RTX;
12915 op[6] = NULL_RTX;
12916 op[7] = NULL_RTX;
12917 op[8] = NULL_RTX;
12919 /* Operand 9 can be used for temporary register. */
12920 op[9] = NULL_RTX;
12922 /* Generate code. */
12923 if (TARGET_64BIT)
12925 /* Setup literal pool pointer if required. */
12926 if ((!DISP_IN_RANGE (delta)
12927 && !CONST_OK_FOR_K (delta)
12928 && !CONST_OK_FOR_Os (delta))
12929 || (!DISP_IN_RANGE (vcall_offset)
12930 && !CONST_OK_FOR_K (vcall_offset)
12931 && !CONST_OK_FOR_Os (vcall_offset)))
12933 op[5] = gen_label_rtx ();
12934 output_asm_insn ("larl\t%4,%5", op);
12937 /* Add DELTA to this pointer. */
12938 if (delta)
12940 if (CONST_OK_FOR_J (delta))
12941 output_asm_insn ("la\t%1,%2(%1)", op);
12942 else if (DISP_IN_RANGE (delta))
12943 output_asm_insn ("lay\t%1,%2(%1)", op);
12944 else if (CONST_OK_FOR_K (delta))
12945 output_asm_insn ("aghi\t%1,%2", op);
12946 else if (CONST_OK_FOR_Os (delta))
12947 output_asm_insn ("agfi\t%1,%2", op);
12948 else
12950 op[6] = gen_label_rtx ();
12951 output_asm_insn ("agf\t%1,%6-%5(%4)", op);
12955 /* Perform vcall adjustment. */
12956 if (vcall_offset)
12958 if (DISP_IN_RANGE (vcall_offset))
12960 output_asm_insn ("lg\t%4,0(%1)", op);
12961 output_asm_insn ("ag\t%1,%3(%4)", op);
12963 else if (CONST_OK_FOR_K (vcall_offset))
12965 output_asm_insn ("lghi\t%4,%3", op);
12966 output_asm_insn ("ag\t%4,0(%1)", op);
12967 output_asm_insn ("ag\t%1,0(%4)", op);
12969 else if (CONST_OK_FOR_Os (vcall_offset))
12971 output_asm_insn ("lgfi\t%4,%3", op);
12972 output_asm_insn ("ag\t%4,0(%1)", op);
12973 output_asm_insn ("ag\t%1,0(%4)", op);
12975 else
12977 op[7] = gen_label_rtx ();
12978 output_asm_insn ("llgf\t%4,%7-%5(%4)", op);
12979 output_asm_insn ("ag\t%4,0(%1)", op);
12980 output_asm_insn ("ag\t%1,0(%4)", op);
12984 /* Jump to target. */
12985 output_asm_insn ("jg\t%0", op);
12987 /* Output literal pool if required. */
12988 if (op[5])
12990 output_asm_insn (".align\t4", op);
12991 targetm.asm_out.internal_label (file, "L",
12992 CODE_LABEL_NUMBER (op[5]));
12994 if (op[6])
12996 targetm.asm_out.internal_label (file, "L",
12997 CODE_LABEL_NUMBER (op[6]));
12998 output_asm_insn (".long\t%2", op);
13000 if (op[7])
13002 targetm.asm_out.internal_label (file, "L",
13003 CODE_LABEL_NUMBER (op[7]));
13004 output_asm_insn (".long\t%3", op);
13007 else
13009 /* Setup base pointer if required. */
13010 if (!vcall_offset
13011 || (!DISP_IN_RANGE (delta)
13012 && !CONST_OK_FOR_K (delta)
13013 && !CONST_OK_FOR_Os (delta))
13014 || (!DISP_IN_RANGE (delta)
13015 && !CONST_OK_FOR_K (vcall_offset)
13016 && !CONST_OK_FOR_Os (vcall_offset)))
13018 op[5] = gen_label_rtx ();
13019 output_asm_insn ("basr\t%4,0", op);
13020 targetm.asm_out.internal_label (file, "L",
13021 CODE_LABEL_NUMBER (op[5]));
13024 /* Add DELTA to this pointer. */
13025 if (delta)
13027 if (CONST_OK_FOR_J (delta))
13028 output_asm_insn ("la\t%1,%2(%1)", op);
13029 else if (DISP_IN_RANGE (delta))
13030 output_asm_insn ("lay\t%1,%2(%1)", op);
13031 else if (CONST_OK_FOR_K (delta))
13032 output_asm_insn ("ahi\t%1,%2", op);
13033 else if (CONST_OK_FOR_Os (delta))
13034 output_asm_insn ("afi\t%1,%2", op);
13035 else
13037 op[6] = gen_label_rtx ();
13038 output_asm_insn ("a\t%1,%6-%5(%4)", op);
13042 /* Perform vcall adjustment. */
13043 if (vcall_offset)
13045 if (CONST_OK_FOR_J (vcall_offset))
13047 output_asm_insn ("l\t%4,0(%1)", op);
13048 output_asm_insn ("a\t%1,%3(%4)", op);
13050 else if (DISP_IN_RANGE (vcall_offset))
13052 output_asm_insn ("l\t%4,0(%1)", op);
13053 output_asm_insn ("ay\t%1,%3(%4)", op);
13055 else if (CONST_OK_FOR_K (vcall_offset))
13057 output_asm_insn ("lhi\t%4,%3", op);
13058 output_asm_insn ("a\t%4,0(%1)", op);
13059 output_asm_insn ("a\t%1,0(%4)", op);
13061 else if (CONST_OK_FOR_Os (vcall_offset))
13063 output_asm_insn ("iilf\t%4,%3", op);
13064 output_asm_insn ("a\t%4,0(%1)", op);
13065 output_asm_insn ("a\t%1,0(%4)", op);
13067 else
13069 op[7] = gen_label_rtx ();
13070 output_asm_insn ("l\t%4,%7-%5(%4)", op);
13071 output_asm_insn ("a\t%4,0(%1)", op);
13072 output_asm_insn ("a\t%1,0(%4)", op);
13075 /* We had to clobber the base pointer register.
13076 Re-setup the base pointer (with a different base). */
13077 op[5] = gen_label_rtx ();
13078 output_asm_insn ("basr\t%4,0", op);
13079 targetm.asm_out.internal_label (file, "L",
13080 CODE_LABEL_NUMBER (op[5]));
13083 /* Jump to target. */
13084 op[8] = gen_label_rtx ();
13086 if (!flag_pic)
13087 output_asm_insn ("l\t%4,%8-%5(%4)", op);
13088 else if (!nonlocal)
13089 output_asm_insn ("a\t%4,%8-%5(%4)", op);
13090 /* We cannot call through .plt, since .plt requires %r12 loaded. */
13091 else if (flag_pic == 1)
13093 output_asm_insn ("a\t%4,%8-%5(%4)", op);
13094 output_asm_insn ("l\t%4,%0(%4)", op);
13096 else if (flag_pic == 2)
13098 op[9] = gen_rtx_REG (Pmode, 0);
13099 output_asm_insn ("l\t%9,%8-4-%5(%4)", op);
13100 output_asm_insn ("a\t%4,%8-%5(%4)", op);
13101 output_asm_insn ("ar\t%4,%9", op);
13102 output_asm_insn ("l\t%4,0(%4)", op);
13105 output_asm_insn ("br\t%4", op);
13107 /* Output literal pool. */
13108 output_asm_insn (".align\t4", op);
13110 if (nonlocal && flag_pic == 2)
13111 output_asm_insn (".long\t%0", op);
13112 if (nonlocal)
13114 op[0] = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
13115 SYMBOL_REF_FLAGS (op[0]) = SYMBOL_FLAG_LOCAL;
13118 targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[8]));
13119 if (!flag_pic)
13120 output_asm_insn (".long\t%0", op);
13121 else
13122 output_asm_insn (".long\t%0-%5", op);
13124 if (op[6])
13126 targetm.asm_out.internal_label (file, "L",
13127 CODE_LABEL_NUMBER (op[6]));
13128 output_asm_insn (".long\t%2", op);
13130 if (op[7])
13132 targetm.asm_out.internal_label (file, "L",
13133 CODE_LABEL_NUMBER (op[7]));
13134 output_asm_insn (".long\t%3", op);
13137 final_end_function ();
13140 static bool
13141 s390_valid_pointer_mode (machine_mode mode)
13143 return (mode == SImode || (TARGET_64BIT && mode == DImode));
13146 /* Checks whether the given CALL_EXPR would use a caller
13147 saved register. This is used to decide whether sibling call
13148 optimization could be performed on the respective function
13149 call. */
13151 static bool
13152 s390_call_saved_register_used (tree call_expr)
13154 CUMULATIVE_ARGS cum_v;
13155 cumulative_args_t cum;
13156 tree parameter;
13157 machine_mode mode;
13158 tree type;
13159 rtx parm_rtx;
13160 int reg, i;
13162 INIT_CUMULATIVE_ARGS (cum_v, NULL, NULL, 0, 0);
13163 cum = pack_cumulative_args (&cum_v);
13165 for (i = 0; i < call_expr_nargs (call_expr); i++)
13167 parameter = CALL_EXPR_ARG (call_expr, i);
13168 gcc_assert (parameter);
13170 /* For an undeclared variable passed as parameter we will get
13171 an ERROR_MARK node here. */
13172 if (TREE_CODE (parameter) == ERROR_MARK)
13173 return true;
13175 type = TREE_TYPE (parameter);
13176 gcc_assert (type);
13178 mode = TYPE_MODE (type);
13179 gcc_assert (mode);
13181 /* We assume that in the target function all parameters are
13182 named. This only has an impact on vector argument register
13183 usage none of which is call-saved. */
13184 if (pass_by_reference (&cum_v, mode, type, true))
13186 mode = Pmode;
13187 type = build_pointer_type (type);
13190 parm_rtx = s390_function_arg (cum, mode, type, true);
13192 s390_function_arg_advance (cum, mode, type, true);
13194 if (!parm_rtx)
13195 continue;
13197 if (REG_P (parm_rtx))
13199 for (reg = 0;
13200 reg < HARD_REGNO_NREGS (REGNO (parm_rtx), GET_MODE (parm_rtx));
13201 reg++)
13202 if (!call_used_regs[reg + REGNO (parm_rtx)])
13203 return true;
13206 if (GET_CODE (parm_rtx) == PARALLEL)
13208 int i;
13210 for (i = 0; i < XVECLEN (parm_rtx, 0); i++)
13212 rtx r = XEXP (XVECEXP (parm_rtx, 0, i), 0);
13214 gcc_assert (REG_P (r));
13216 for (reg = 0;
13217 reg < HARD_REGNO_NREGS (REGNO (r), GET_MODE (r));
13218 reg++)
13219 if (!call_used_regs[reg + REGNO (r)])
13220 return true;
13225 return false;
13228 /* Return true if the given call expression can be
13229 turned into a sibling call.
13230 DECL holds the declaration of the function to be called whereas
13231 EXP is the call expression itself. */
13233 static bool
13234 s390_function_ok_for_sibcall (tree decl, tree exp)
13236 /* The TPF epilogue uses register 1. */
13237 if (TARGET_TPF_PROFILING)
13238 return false;
13240 /* The 31 bit PLT code uses register 12 (GOT pointer - caller saved)
13241 which would have to be restored before the sibcall. */
13242 if (!TARGET_64BIT && flag_pic && decl && !targetm.binds_local_p (decl))
13243 return false;
13245 /* Register 6 on s390 is available as an argument register but unfortunately
13246 "caller saved". This makes functions needing this register for arguments
13247 not suitable for sibcalls. */
13248 return !s390_call_saved_register_used (exp);
13251 /* Return the fixed registers used for condition codes. */
13253 static bool
13254 s390_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
13256 *p1 = CC_REGNUM;
13257 *p2 = INVALID_REGNUM;
13259 return true;
13262 /* This function is used by the call expanders of the machine description.
13263 It emits the call insn itself together with the necessary operations
13264 to adjust the target address and returns the emitted insn.
13265 ADDR_LOCATION is the target address rtx
13266 TLS_CALL the location of the thread-local symbol
13267 RESULT_REG the register where the result of the call should be stored
13268 RETADDR_REG the register where the return address should be stored
13269 If this parameter is NULL_RTX the call is considered
13270 to be a sibling call. */
13272 rtx_insn *
13273 s390_emit_call (rtx addr_location, rtx tls_call, rtx result_reg,
13274 rtx retaddr_reg)
13276 bool plt_call = false;
13277 rtx_insn *insn;
13278 rtx call;
13279 rtx clobber;
13280 rtvec vec;
13282 /* Direct function calls need special treatment. */
13283 if (GET_CODE (addr_location) == SYMBOL_REF)
13285 /* When calling a global routine in PIC mode, we must
13286 replace the symbol itself with the PLT stub. */
13287 if (flag_pic && !SYMBOL_REF_LOCAL_P (addr_location))
13289 if (TARGET_64BIT || retaddr_reg != NULL_RTX)
13291 addr_location = gen_rtx_UNSPEC (Pmode,
13292 gen_rtvec (1, addr_location),
13293 UNSPEC_PLT);
13294 addr_location = gen_rtx_CONST (Pmode, addr_location);
13295 plt_call = true;
13297 else
13298 /* For -fpic code the PLT entries might use r12 which is
13299 call-saved. Therefore we cannot do a sibcall when
13300 calling directly using a symbol ref. When reaching
13301 this point we decided (in s390_function_ok_for_sibcall)
13302 to do a sibcall for a function pointer but one of the
13303 optimizers was able to get rid of the function pointer
13304 by propagating the symbol ref into the call. This
13305 optimization is illegal for S/390 so we turn the direct
13306 call into a indirect call again. */
13307 addr_location = force_reg (Pmode, addr_location);
13310 /* Unless we can use the bras(l) insn, force the
13311 routine address into a register. */
13312 if (!TARGET_SMALL_EXEC && !TARGET_CPU_ZARCH)
13314 if (flag_pic)
13315 addr_location = legitimize_pic_address (addr_location, 0);
13316 else
13317 addr_location = force_reg (Pmode, addr_location);
13321 /* If it is already an indirect call or the code above moved the
13322 SYMBOL_REF to somewhere else make sure the address can be found in
13323 register 1. */
13324 if (retaddr_reg == NULL_RTX
13325 && GET_CODE (addr_location) != SYMBOL_REF
13326 && !plt_call)
13328 emit_move_insn (gen_rtx_REG (Pmode, SIBCALL_REGNUM), addr_location);
13329 addr_location = gen_rtx_REG (Pmode, SIBCALL_REGNUM);
13332 addr_location = gen_rtx_MEM (QImode, addr_location);
13333 call = gen_rtx_CALL (VOIDmode, addr_location, const0_rtx);
13335 if (result_reg != NULL_RTX)
13336 call = gen_rtx_SET (result_reg, call);
13338 if (retaddr_reg != NULL_RTX)
13340 clobber = gen_rtx_CLOBBER (VOIDmode, retaddr_reg);
13342 if (tls_call != NULL_RTX)
13343 vec = gen_rtvec (3, call, clobber,
13344 gen_rtx_USE (VOIDmode, tls_call));
13345 else
13346 vec = gen_rtvec (2, call, clobber);
13348 call = gen_rtx_PARALLEL (VOIDmode, vec);
13351 insn = emit_call_insn (call);
13353 /* 31-bit PLT stubs and tls calls use the GOT register implicitly. */
13354 if ((!TARGET_64BIT && plt_call) || tls_call != NULL_RTX)
13356 /* s390_function_ok_for_sibcall should
13357 have denied sibcalls in this case. */
13358 gcc_assert (retaddr_reg != NULL_RTX);
13359 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, 12));
13361 return insn;
13364 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE. */
13366 static void
13367 s390_conditional_register_usage (void)
13369 int i;
13371 if (flag_pic)
13373 fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
13374 call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
13376 if (TARGET_CPU_ZARCH)
13378 fixed_regs[BASE_REGNUM] = 0;
13379 call_used_regs[BASE_REGNUM] = 0;
13380 fixed_regs[RETURN_REGNUM] = 0;
13381 call_used_regs[RETURN_REGNUM] = 0;
13383 if (TARGET_64BIT)
13385 for (i = FPR8_REGNUM; i <= FPR15_REGNUM; i++)
13386 call_used_regs[i] = call_really_used_regs[i] = 0;
13388 else
13390 call_used_regs[FPR4_REGNUM] = call_really_used_regs[FPR4_REGNUM] = 0;
13391 call_used_regs[FPR6_REGNUM] = call_really_used_regs[FPR6_REGNUM] = 0;
13394 if (TARGET_SOFT_FLOAT)
13396 for (i = FPR0_REGNUM; i <= FPR15_REGNUM; i++)
13397 call_used_regs[i] = fixed_regs[i] = 1;
13400 /* Disable v16 - v31 for non-vector target. */
13401 if (!TARGET_VX)
13403 for (i = VR16_REGNUM; i <= VR31_REGNUM; i++)
13404 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
13408 /* Corresponding function to eh_return expander. */
13410 static GTY(()) rtx s390_tpf_eh_return_symbol;
13411 void
13412 s390_emit_tpf_eh_return (rtx target)
13414 rtx_insn *insn;
13415 rtx reg, orig_ra;
13417 if (!s390_tpf_eh_return_symbol)
13418 s390_tpf_eh_return_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tpf_eh_return");
13420 reg = gen_rtx_REG (Pmode, 2);
13421 orig_ra = gen_rtx_REG (Pmode, 3);
13423 emit_move_insn (reg, target);
13424 emit_move_insn (orig_ra, get_hard_reg_initial_val (Pmode, RETURN_REGNUM));
13425 insn = s390_emit_call (s390_tpf_eh_return_symbol, NULL_RTX, reg,
13426 gen_rtx_REG (Pmode, RETURN_REGNUM));
13427 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
13428 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), orig_ra);
13430 emit_move_insn (EH_RETURN_HANDLER_RTX, reg);
13433 /* Rework the prologue/epilogue to avoid saving/restoring
13434 registers unnecessarily. */
13436 static void
13437 s390_optimize_prologue (void)
13439 rtx_insn *insn, *new_insn, *next_insn;
13441 /* Do a final recompute of the frame-related data. */
13442 s390_optimize_register_info ();
13444 /* If all special registers are in fact used, there's nothing we
13445 can do, so no point in walking the insn list. */
13447 if (cfun_frame_layout.first_save_gpr <= BASE_REGNUM
13448 && cfun_frame_layout.last_save_gpr >= BASE_REGNUM
13449 && (TARGET_CPU_ZARCH
13450 || (cfun_frame_layout.first_save_gpr <= RETURN_REGNUM
13451 && cfun_frame_layout.last_save_gpr >= RETURN_REGNUM)))
13452 return;
13454 /* Search for prologue/epilogue insns and replace them. */
13456 for (insn = get_insns (); insn; insn = next_insn)
13458 int first, last, off;
13459 rtx set, base, offset;
13460 rtx pat;
13462 next_insn = NEXT_INSN (insn);
13464 if (! NONJUMP_INSN_P (insn) || ! RTX_FRAME_RELATED_P (insn))
13465 continue;
13467 pat = PATTERN (insn);
13469 /* Remove ldgr/lgdr instructions used for saving and restore
13470 GPRs if possible. */
13471 if (TARGET_Z10)
13473 rtx tmp_pat = pat;
13475 if (INSN_CODE (insn) == CODE_FOR_stack_restore_from_fpr)
13476 tmp_pat = XVECEXP (pat, 0, 0);
13478 if (GET_CODE (tmp_pat) == SET
13479 && GET_MODE (SET_SRC (tmp_pat)) == DImode
13480 && REG_P (SET_SRC (tmp_pat))
13481 && REG_P (SET_DEST (tmp_pat)))
13483 int src_regno = REGNO (SET_SRC (tmp_pat));
13484 int dest_regno = REGNO (SET_DEST (tmp_pat));
13485 int gpr_regno;
13486 int fpr_regno;
13488 if (!((GENERAL_REGNO_P (src_regno)
13489 && FP_REGNO_P (dest_regno))
13490 || (FP_REGNO_P (src_regno)
13491 && GENERAL_REGNO_P (dest_regno))))
13492 continue;
13494 gpr_regno = GENERAL_REGNO_P (src_regno) ? src_regno : dest_regno;
13495 fpr_regno = FP_REGNO_P (src_regno) ? src_regno : dest_regno;
13497 /* GPR must be call-saved, FPR must be call-clobbered. */
13498 if (!call_really_used_regs[fpr_regno]
13499 || call_really_used_regs[gpr_regno])
13500 continue;
13502 /* It must not happen that what we once saved in an FPR now
13503 needs a stack slot. */
13504 gcc_assert (cfun_gpr_save_slot (gpr_regno) != SAVE_SLOT_STACK);
13506 if (cfun_gpr_save_slot (gpr_regno) == SAVE_SLOT_NONE)
13508 remove_insn (insn);
13509 continue;
13514 if (GET_CODE (pat) == PARALLEL
13515 && store_multiple_operation (pat, VOIDmode))
13517 set = XVECEXP (pat, 0, 0);
13518 first = REGNO (SET_SRC (set));
13519 last = first + XVECLEN (pat, 0) - 1;
13520 offset = const0_rtx;
13521 base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
13522 off = INTVAL (offset);
13524 if (GET_CODE (base) != REG || off < 0)
13525 continue;
13526 if (cfun_frame_layout.first_save_gpr != -1
13527 && (cfun_frame_layout.first_save_gpr < first
13528 || cfun_frame_layout.last_save_gpr > last))
13529 continue;
13530 if (REGNO (base) != STACK_POINTER_REGNUM
13531 && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
13532 continue;
13533 if (first > BASE_REGNUM || last < BASE_REGNUM)
13534 continue;
13536 if (cfun_frame_layout.first_save_gpr != -1)
13538 rtx s_pat = save_gprs (base,
13539 off + (cfun_frame_layout.first_save_gpr
13540 - first) * UNITS_PER_LONG,
13541 cfun_frame_layout.first_save_gpr,
13542 cfun_frame_layout.last_save_gpr);
13543 new_insn = emit_insn_before (s_pat, insn);
13544 INSN_ADDRESSES_NEW (new_insn, -1);
13547 remove_insn (insn);
13548 continue;
13551 if (cfun_frame_layout.first_save_gpr == -1
13552 && GET_CODE (pat) == SET
13553 && GENERAL_REG_P (SET_SRC (pat))
13554 && GET_CODE (SET_DEST (pat)) == MEM)
13556 set = pat;
13557 first = REGNO (SET_SRC (set));
13558 offset = const0_rtx;
13559 base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
13560 off = INTVAL (offset);
13562 if (GET_CODE (base) != REG || off < 0)
13563 continue;
13564 if (REGNO (base) != STACK_POINTER_REGNUM
13565 && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
13566 continue;
13568 remove_insn (insn);
13569 continue;
13572 if (GET_CODE (pat) == PARALLEL
13573 && load_multiple_operation (pat, VOIDmode))
13575 set = XVECEXP (pat, 0, 0);
13576 first = REGNO (SET_DEST (set));
13577 last = first + XVECLEN (pat, 0) - 1;
13578 offset = const0_rtx;
13579 base = eliminate_constant_term (XEXP (SET_SRC (set), 0), &offset);
13580 off = INTVAL (offset);
13582 if (GET_CODE (base) != REG || off < 0)
13583 continue;
13585 if (cfun_frame_layout.first_restore_gpr != -1
13586 && (cfun_frame_layout.first_restore_gpr < first
13587 || cfun_frame_layout.last_restore_gpr > last))
13588 continue;
13589 if (REGNO (base) != STACK_POINTER_REGNUM
13590 && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
13591 continue;
13592 if (first > BASE_REGNUM || last < BASE_REGNUM)
13593 continue;
13595 if (cfun_frame_layout.first_restore_gpr != -1)
13597 rtx rpat = restore_gprs (base,
13598 off + (cfun_frame_layout.first_restore_gpr
13599 - first) * UNITS_PER_LONG,
13600 cfun_frame_layout.first_restore_gpr,
13601 cfun_frame_layout.last_restore_gpr);
13603 /* Remove REG_CFA_RESTOREs for registers that we no
13604 longer need to save. */
13605 REG_NOTES (rpat) = REG_NOTES (insn);
13606 for (rtx *ptr = &REG_NOTES (rpat); *ptr; )
13607 if (REG_NOTE_KIND (*ptr) == REG_CFA_RESTORE
13608 && ((int) REGNO (XEXP (*ptr, 0))
13609 < cfun_frame_layout.first_restore_gpr))
13610 *ptr = XEXP (*ptr, 1);
13611 else
13612 ptr = &XEXP (*ptr, 1);
13613 new_insn = emit_insn_before (rpat, insn);
13614 RTX_FRAME_RELATED_P (new_insn) = 1;
13615 INSN_ADDRESSES_NEW (new_insn, -1);
13618 remove_insn (insn);
13619 continue;
13622 if (cfun_frame_layout.first_restore_gpr == -1
13623 && GET_CODE (pat) == SET
13624 && GENERAL_REG_P (SET_DEST (pat))
13625 && GET_CODE (SET_SRC (pat)) == MEM)
13627 set = pat;
13628 first = REGNO (SET_DEST (set));
13629 offset = const0_rtx;
13630 base = eliminate_constant_term (XEXP (SET_SRC (set), 0), &offset);
13631 off = INTVAL (offset);
13633 if (GET_CODE (base) != REG || off < 0)
13634 continue;
13636 if (REGNO (base) != STACK_POINTER_REGNUM
13637 && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
13638 continue;
13640 remove_insn (insn);
13641 continue;
13646 /* On z10 and later the dynamic branch prediction must see the
13647 backward jump within a certain windows. If not it falls back to
13648 the static prediction. This function rearranges the loop backward
13649 branch in a way which makes the static prediction always correct.
13650 The function returns true if it added an instruction. */
13651 static bool
13652 s390_fix_long_loop_prediction (rtx_insn *insn)
13654 rtx set = single_set (insn);
13655 rtx code_label, label_ref;
13656 rtx_insn *uncond_jump;
13657 rtx_insn *cur_insn;
13658 rtx tmp;
13659 int distance;
13661 /* This will exclude branch on count and branch on index patterns
13662 since these are correctly statically predicted. */
13663 if (!set
13664 || SET_DEST (set) != pc_rtx
13665 || GET_CODE (SET_SRC(set)) != IF_THEN_ELSE)
13666 return false;
13668 /* Skip conditional returns. */
13669 if (ANY_RETURN_P (XEXP (SET_SRC (set), 1))
13670 && XEXP (SET_SRC (set), 2) == pc_rtx)
13671 return false;
13673 label_ref = (GET_CODE (XEXP (SET_SRC (set), 1)) == LABEL_REF ?
13674 XEXP (SET_SRC (set), 1) : XEXP (SET_SRC (set), 2));
13676 gcc_assert (GET_CODE (label_ref) == LABEL_REF);
13678 code_label = XEXP (label_ref, 0);
13680 if (INSN_ADDRESSES (INSN_UID (code_label)) == -1
13681 || INSN_ADDRESSES (INSN_UID (insn)) == -1
13682 || (INSN_ADDRESSES (INSN_UID (insn))
13683 - INSN_ADDRESSES (INSN_UID (code_label)) < PREDICT_DISTANCE))
13684 return false;
13686 for (distance = 0, cur_insn = PREV_INSN (insn);
13687 distance < PREDICT_DISTANCE - 6;
13688 distance += get_attr_length (cur_insn), cur_insn = PREV_INSN (cur_insn))
13689 if (!cur_insn || JUMP_P (cur_insn) || LABEL_P (cur_insn))
13690 return false;
13692 rtx_code_label *new_label = gen_label_rtx ();
13693 uncond_jump = emit_jump_insn_after (
13694 gen_rtx_SET (pc_rtx,
13695 gen_rtx_LABEL_REF (VOIDmode, code_label)),
13696 insn);
13697 emit_label_after (new_label, uncond_jump);
13699 tmp = XEXP (SET_SRC (set), 1);
13700 XEXP (SET_SRC (set), 1) = XEXP (SET_SRC (set), 2);
13701 XEXP (SET_SRC (set), 2) = tmp;
13702 INSN_CODE (insn) = -1;
13704 XEXP (label_ref, 0) = new_label;
13705 JUMP_LABEL (insn) = new_label;
13706 JUMP_LABEL (uncond_jump) = code_label;
13708 return true;
13711 /* Returns 1 if INSN reads the value of REG for purposes not related
13712 to addressing of memory, and 0 otherwise. */
13713 static int
13714 s390_non_addr_reg_read_p (rtx reg, rtx_insn *insn)
13716 return reg_referenced_p (reg, PATTERN (insn))
13717 && !reg_used_in_mem_p (REGNO (reg), PATTERN (insn));
13720 /* Starting from INSN find_cond_jump looks downwards in the insn
13721 stream for a single jump insn which is the last user of the
13722 condition code set in INSN. */
13723 static rtx_insn *
13724 find_cond_jump (rtx_insn *insn)
13726 for (; insn; insn = NEXT_INSN (insn))
13728 rtx ite, cc;
13730 if (LABEL_P (insn))
13731 break;
13733 if (!JUMP_P (insn))
13735 if (reg_mentioned_p (gen_rtx_REG (CCmode, CC_REGNUM), insn))
13736 break;
13737 continue;
13740 /* This will be triggered by a return. */
13741 if (GET_CODE (PATTERN (insn)) != SET)
13742 break;
13744 gcc_assert (SET_DEST (PATTERN (insn)) == pc_rtx);
13745 ite = SET_SRC (PATTERN (insn));
13747 if (GET_CODE (ite) != IF_THEN_ELSE)
13748 break;
13750 cc = XEXP (XEXP (ite, 0), 0);
13751 if (!REG_P (cc) || !CC_REGNO_P (REGNO (cc)))
13752 break;
13754 if (find_reg_note (insn, REG_DEAD, cc))
13755 return insn;
13756 break;
13759 return NULL;
13762 /* Swap the condition in COND and the operands in OP0 and OP1 so that
13763 the semantics does not change. If NULL_RTX is passed as COND the
13764 function tries to find the conditional jump starting with INSN. */
13765 static void
13766 s390_swap_cmp (rtx cond, rtx *op0, rtx *op1, rtx_insn *insn)
13768 rtx tmp = *op0;
13770 if (cond == NULL_RTX)
13772 rtx_insn *jump = find_cond_jump (NEXT_INSN (insn));
13773 rtx set = jump ? single_set (jump) : NULL_RTX;
13775 if (set == NULL_RTX)
13776 return;
13778 cond = XEXP (SET_SRC (set), 0);
13781 *op0 = *op1;
13782 *op1 = tmp;
13783 PUT_CODE (cond, swap_condition (GET_CODE (cond)));
13786 /* On z10, instructions of the compare-and-branch family have the
13787 property to access the register occurring as second operand with
13788 its bits complemented. If such a compare is grouped with a second
13789 instruction that accesses the same register non-complemented, and
13790 if that register's value is delivered via a bypass, then the
13791 pipeline recycles, thereby causing significant performance decline.
13792 This function locates such situations and exchanges the two
13793 operands of the compare. The function return true whenever it
13794 added an insn. */
13795 static bool
13796 s390_z10_optimize_cmp (rtx_insn *insn)
13798 rtx_insn *prev_insn, *next_insn;
13799 bool insn_added_p = false;
13800 rtx cond, *op0, *op1;
13802 if (GET_CODE (PATTERN (insn)) == PARALLEL)
13804 /* Handle compare and branch and branch on count
13805 instructions. */
13806 rtx pattern = single_set (insn);
13808 if (!pattern
13809 || SET_DEST (pattern) != pc_rtx
13810 || GET_CODE (SET_SRC (pattern)) != IF_THEN_ELSE)
13811 return false;
13813 cond = XEXP (SET_SRC (pattern), 0);
13814 op0 = &XEXP (cond, 0);
13815 op1 = &XEXP (cond, 1);
13817 else if (GET_CODE (PATTERN (insn)) == SET)
13819 rtx src, dest;
13821 /* Handle normal compare instructions. */
13822 src = SET_SRC (PATTERN (insn));
13823 dest = SET_DEST (PATTERN (insn));
13825 if (!REG_P (dest)
13826 || !CC_REGNO_P (REGNO (dest))
13827 || GET_CODE (src) != COMPARE)
13828 return false;
13830 /* s390_swap_cmp will try to find the conditional
13831 jump when passing NULL_RTX as condition. */
13832 cond = NULL_RTX;
13833 op0 = &XEXP (src, 0);
13834 op1 = &XEXP (src, 1);
13836 else
13837 return false;
13839 if (!REG_P (*op0) || !REG_P (*op1))
13840 return false;
13842 if (GET_MODE_CLASS (GET_MODE (*op0)) != MODE_INT)
13843 return false;
13845 /* Swap the COMPARE arguments and its mask if there is a
13846 conflicting access in the previous insn. */
13847 prev_insn = prev_active_insn (insn);
13848 if (prev_insn != NULL_RTX && INSN_P (prev_insn)
13849 && reg_referenced_p (*op1, PATTERN (prev_insn)))
13850 s390_swap_cmp (cond, op0, op1, insn);
13852 /* Check if there is a conflict with the next insn. If there
13853 was no conflict with the previous insn, then swap the
13854 COMPARE arguments and its mask. If we already swapped
13855 the operands, or if swapping them would cause a conflict
13856 with the previous insn, issue a NOP after the COMPARE in
13857 order to separate the two instuctions. */
13858 next_insn = next_active_insn (insn);
13859 if (next_insn != NULL_RTX && INSN_P (next_insn)
13860 && s390_non_addr_reg_read_p (*op1, next_insn))
13862 if (prev_insn != NULL_RTX && INSN_P (prev_insn)
13863 && s390_non_addr_reg_read_p (*op0, prev_insn))
13865 if (REGNO (*op1) == 0)
13866 emit_insn_after (gen_nop1 (), insn);
13867 else
13868 emit_insn_after (gen_nop (), insn);
13869 insn_added_p = true;
13871 else
13872 s390_swap_cmp (cond, op0, op1, insn);
13874 return insn_added_p;
13877 /* Number of INSNs to be scanned backward in the last BB of the loop
13878 and forward in the first BB of the loop. This usually should be a
13879 bit more than the number of INSNs which could go into one
13880 group. */
13881 #define S390_OSC_SCAN_INSN_NUM 5
13883 /* Scan LOOP for static OSC collisions and return true if a osc_break
13884 should be issued for this loop. */
13885 static bool
13886 s390_adjust_loop_scan_osc (struct loop* loop)
13889 HARD_REG_SET modregs, newregs;
13890 rtx_insn *insn, *store_insn = NULL;
13891 rtx set;
13892 struct s390_address addr_store, addr_load;
13893 subrtx_iterator::array_type array;
13894 int insn_count;
13896 CLEAR_HARD_REG_SET (modregs);
13898 insn_count = 0;
13899 FOR_BB_INSNS_REVERSE (loop->latch, insn)
13901 if (!INSN_P (insn) || INSN_CODE (insn) <= 0)
13902 continue;
13904 insn_count++;
13905 if (insn_count > S390_OSC_SCAN_INSN_NUM)
13906 return false;
13908 find_all_hard_reg_sets (insn, &newregs, true);
13909 IOR_HARD_REG_SET (modregs, newregs);
13911 set = single_set (insn);
13912 if (!set)
13913 continue;
13915 if (MEM_P (SET_DEST (set))
13916 && s390_decompose_address (XEXP (SET_DEST (set), 0), &addr_store))
13918 store_insn = insn;
13919 break;
13923 if (store_insn == NULL_RTX)
13924 return false;
13926 insn_count = 0;
13927 FOR_BB_INSNS (loop->header, insn)
13929 if (!INSN_P (insn) || INSN_CODE (insn) <= 0)
13930 continue;
13932 if (insn == store_insn)
13933 return false;
13935 insn_count++;
13936 if (insn_count > S390_OSC_SCAN_INSN_NUM)
13937 return false;
13939 find_all_hard_reg_sets (insn, &newregs, true);
13940 IOR_HARD_REG_SET (modregs, newregs);
13942 set = single_set (insn);
13943 if (!set)
13944 continue;
13946 /* An intermediate store disrupts static OSC checking
13947 anyway. */
13948 if (MEM_P (SET_DEST (set))
13949 && s390_decompose_address (XEXP (SET_DEST (set), 0), NULL))
13950 return false;
13952 FOR_EACH_SUBRTX (iter, array, SET_SRC (set), NONCONST)
13953 if (MEM_P (*iter)
13954 && s390_decompose_address (XEXP (*iter, 0), &addr_load)
13955 && rtx_equal_p (addr_load.base, addr_store.base)
13956 && rtx_equal_p (addr_load.indx, addr_store.indx)
13957 && rtx_equal_p (addr_load.disp, addr_store.disp))
13959 if ((addr_load.base != NULL_RTX
13960 && TEST_HARD_REG_BIT (modregs, REGNO (addr_load.base)))
13961 || (addr_load.indx != NULL_RTX
13962 && TEST_HARD_REG_BIT (modregs, REGNO (addr_load.indx))))
13963 return true;
13966 return false;
13969 /* Look for adjustments which can be done on simple innermost
13970 loops. */
13971 static void
13972 s390_adjust_loops ()
13974 struct loop *loop = NULL;
13976 df_analyze ();
13977 compute_bb_for_insn ();
13979 /* Find the loops. */
13980 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
13982 FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST)
13984 if (dump_file)
13986 flow_loop_dump (loop, dump_file, NULL, 0);
13987 fprintf (dump_file, ";; OSC loop scan Loop: ");
13989 if (loop->latch == NULL
13990 || pc_set (BB_END (loop->latch)) == NULL_RTX
13991 || !s390_adjust_loop_scan_osc (loop))
13993 if (dump_file)
13995 if (loop->latch == NULL)
13996 fprintf (dump_file, " muliple backward jumps\n");
13997 else
13999 fprintf (dump_file, " header insn: %d latch insn: %d ",
14000 INSN_UID (BB_HEAD (loop->header)),
14001 INSN_UID (BB_END (loop->latch)));
14002 if (pc_set (BB_END (loop->latch)) == NULL_RTX)
14003 fprintf (dump_file, " loop does not end with jump\n");
14004 else
14005 fprintf (dump_file, " not instrumented\n");
14009 else
14011 rtx_insn *new_insn;
14013 if (dump_file)
14014 fprintf (dump_file, " adding OSC break insn: ");
14015 new_insn = emit_insn_before (gen_osc_break (),
14016 BB_END (loop->latch));
14017 INSN_ADDRESSES_NEW (new_insn, -1);
14021 loop_optimizer_finalize ();
14023 df_finish_pass (false);
14026 /* Perform machine-dependent processing. */
14028 static void
14029 s390_reorg (void)
14031 bool pool_overflow = false;
14032 int hw_before, hw_after;
14034 if (s390_tune == PROCESSOR_2964_Z13)
14035 s390_adjust_loops ();
14037 /* Make sure all splits have been performed; splits after
14038 machine_dependent_reorg might confuse insn length counts. */
14039 split_all_insns_noflow ();
14041 /* Install the main literal pool and the associated base
14042 register load insns.
14044 In addition, there are two problematic situations we need
14045 to correct:
14047 - the literal pool might be > 4096 bytes in size, so that
14048 some of its elements cannot be directly accessed
14050 - a branch target might be > 64K away from the branch, so that
14051 it is not possible to use a PC-relative instruction.
14053 To fix those, we split the single literal pool into multiple
14054 pool chunks, reloading the pool base register at various
14055 points throughout the function to ensure it always points to
14056 the pool chunk the following code expects, and / or replace
14057 PC-relative branches by absolute branches.
14059 However, the two problems are interdependent: splitting the
14060 literal pool can move a branch further away from its target,
14061 causing the 64K limit to overflow, and on the other hand,
14062 replacing a PC-relative branch by an absolute branch means
14063 we need to put the branch target address into the literal
14064 pool, possibly causing it to overflow.
14066 So, we loop trying to fix up both problems until we manage
14067 to satisfy both conditions at the same time. Note that the
14068 loop is guaranteed to terminate as every pass of the loop
14069 strictly decreases the total number of PC-relative branches
14070 in the function. (This is not completely true as there
14071 might be branch-over-pool insns introduced by chunkify_start.
14072 Those never need to be split however.) */
14074 for (;;)
14076 struct constant_pool *pool = NULL;
14078 /* Collect the literal pool. */
14079 if (!pool_overflow)
14081 pool = s390_mainpool_start ();
14082 if (!pool)
14083 pool_overflow = true;
14086 /* If literal pool overflowed, start to chunkify it. */
14087 if (pool_overflow)
14088 pool = s390_chunkify_start ();
14090 /* Split out-of-range branches. If this has created new
14091 literal pool entries, cancel current chunk list and
14092 recompute it. zSeries machines have large branch
14093 instructions, so we never need to split a branch. */
14094 if (!TARGET_CPU_ZARCH && s390_split_branches ())
14096 if (pool_overflow)
14097 s390_chunkify_cancel (pool);
14098 else
14099 s390_mainpool_cancel (pool);
14101 continue;
14104 /* If we made it up to here, both conditions are satisfied.
14105 Finish up literal pool related changes. */
14106 if (pool_overflow)
14107 s390_chunkify_finish (pool);
14108 else
14109 s390_mainpool_finish (pool);
14111 /* We're done splitting branches. */
14112 cfun->machine->split_branches_pending_p = false;
14113 break;
14116 /* Generate out-of-pool execute target insns. */
14117 if (TARGET_CPU_ZARCH)
14119 rtx_insn *insn, *target;
14120 rtx label;
14122 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14124 label = s390_execute_label (insn);
14125 if (!label)
14126 continue;
14128 gcc_assert (label != const0_rtx);
14130 target = emit_label (XEXP (label, 0));
14131 INSN_ADDRESSES_NEW (target, -1);
14133 target = emit_insn (s390_execute_target (insn));
14134 INSN_ADDRESSES_NEW (target, -1);
14138 /* Try to optimize prologue and epilogue further. */
14139 s390_optimize_prologue ();
14141 /* Walk over the insns and do some >=z10 specific changes. */
14142 if (s390_tune >= PROCESSOR_2097_Z10)
14144 rtx_insn *insn;
14145 bool insn_added_p = false;
14147 /* The insn lengths and addresses have to be up to date for the
14148 following manipulations. */
14149 shorten_branches (get_insns ());
14151 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14153 if (!INSN_P (insn) || INSN_CODE (insn) <= 0)
14154 continue;
14156 if (JUMP_P (insn))
14157 insn_added_p |= s390_fix_long_loop_prediction (insn);
14159 if ((GET_CODE (PATTERN (insn)) == PARALLEL
14160 || GET_CODE (PATTERN (insn)) == SET)
14161 && s390_tune == PROCESSOR_2097_Z10)
14162 insn_added_p |= s390_z10_optimize_cmp (insn);
14165 /* Adjust branches if we added new instructions. */
14166 if (insn_added_p)
14167 shorten_branches (get_insns ());
14170 s390_function_num_hotpatch_hw (current_function_decl, &hw_before, &hw_after);
14171 if (hw_after > 0)
14173 rtx_insn *insn;
14175 /* Insert NOPs for hotpatching. */
14176 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14177 /* Emit NOPs
14178 1. inside the area covered by debug information to allow setting
14179 breakpoints at the NOPs,
14180 2. before any insn which results in an asm instruction,
14181 3. before in-function labels to avoid jumping to the NOPs, for
14182 example as part of a loop,
14183 4. before any barrier in case the function is completely empty
14184 (__builtin_unreachable ()) and has neither internal labels nor
14185 active insns.
14187 if (active_insn_p (insn) || BARRIER_P (insn) || LABEL_P (insn))
14188 break;
14189 /* Output a series of NOPs before the first active insn. */
14190 while (insn && hw_after > 0)
14192 if (hw_after >= 3 && TARGET_CPU_ZARCH)
14194 emit_insn_before (gen_nop_6_byte (), insn);
14195 hw_after -= 3;
14197 else if (hw_after >= 2)
14199 emit_insn_before (gen_nop_4_byte (), insn);
14200 hw_after -= 2;
14202 else
14204 emit_insn_before (gen_nop_2_byte (), insn);
14205 hw_after -= 1;
14211 /* Return true if INSN is a fp load insn writing register REGNO. */
14212 static inline bool
14213 s390_fpload_toreg (rtx_insn *insn, unsigned int regno)
14215 rtx set;
14216 enum attr_type flag = s390_safe_attr_type (insn);
14218 if (flag != TYPE_FLOADSF && flag != TYPE_FLOADDF)
14219 return false;
14221 set = single_set (insn);
14223 if (set == NULL_RTX)
14224 return false;
14226 if (!REG_P (SET_DEST (set)) || !MEM_P (SET_SRC (set)))
14227 return false;
14229 if (REGNO (SET_DEST (set)) != regno)
14230 return false;
14232 return true;
14235 /* This value describes the distance to be avoided between an
14236 arithmetic fp instruction and an fp load writing the same register.
14237 Z10_EARLYLOAD_DISTANCE - 1 as well as Z10_EARLYLOAD_DISTANCE + 1 is
14238 fine but the exact value has to be avoided. Otherwise the FP
14239 pipeline will throw an exception causing a major penalty. */
14240 #define Z10_EARLYLOAD_DISTANCE 7
14242 /* Rearrange the ready list in order to avoid the situation described
14243 for Z10_EARLYLOAD_DISTANCE. A problematic load instruction is
14244 moved to the very end of the ready list. */
14245 static void
14246 s390_z10_prevent_earlyload_conflicts (rtx_insn **ready, int *nready_p)
14248 unsigned int regno;
14249 int nready = *nready_p;
14250 rtx_insn *tmp;
14251 int i;
14252 rtx_insn *insn;
14253 rtx set;
14254 enum attr_type flag;
14255 int distance;
14257 /* Skip DISTANCE - 1 active insns. */
14258 for (insn = last_scheduled_insn, distance = Z10_EARLYLOAD_DISTANCE - 1;
14259 distance > 0 && insn != NULL_RTX;
14260 distance--, insn = prev_active_insn (insn))
14261 if (CALL_P (insn) || JUMP_P (insn))
14262 return;
14264 if (insn == NULL_RTX)
14265 return;
14267 set = single_set (insn);
14269 if (set == NULL_RTX || !REG_P (SET_DEST (set))
14270 || GET_MODE_CLASS (GET_MODE (SET_DEST (set))) != MODE_FLOAT)
14271 return;
14273 flag = s390_safe_attr_type (insn);
14275 if (flag == TYPE_FLOADSF || flag == TYPE_FLOADDF)
14276 return;
14278 regno = REGNO (SET_DEST (set));
14279 i = nready - 1;
14281 while (!s390_fpload_toreg (ready[i], regno) && i > 0)
14282 i--;
14284 if (!i)
14285 return;
14287 tmp = ready[i];
14288 memmove (&ready[1], &ready[0], sizeof (rtx_insn *) * i);
14289 ready[0] = tmp;
14293 /* The s390_sched_state variable tracks the state of the current or
14294 the last instruction group.
14296 0,1,2 number of instructions scheduled in the current group
14297 3 the last group is complete - normal insns
14298 4 the last group was a cracked/expanded insn */
14300 static int s390_sched_state;
14302 #define S390_SCHED_STATE_NORMAL 3
14303 #define S390_SCHED_STATE_CRACKED 4
14305 #define S390_SCHED_ATTR_MASK_CRACKED 0x1
14306 #define S390_SCHED_ATTR_MASK_EXPANDED 0x2
14307 #define S390_SCHED_ATTR_MASK_ENDGROUP 0x4
14308 #define S390_SCHED_ATTR_MASK_GROUPALONE 0x8
14310 static unsigned int
14311 s390_get_sched_attrmask (rtx_insn *insn)
14313 unsigned int mask = 0;
14315 switch (s390_tune)
14317 case PROCESSOR_2827_ZEC12:
14318 if (get_attr_zEC12_cracked (insn))
14319 mask |= S390_SCHED_ATTR_MASK_CRACKED;
14320 if (get_attr_zEC12_expanded (insn))
14321 mask |= S390_SCHED_ATTR_MASK_EXPANDED;
14322 if (get_attr_zEC12_endgroup (insn))
14323 mask |= S390_SCHED_ATTR_MASK_ENDGROUP;
14324 if (get_attr_zEC12_groupalone (insn))
14325 mask |= S390_SCHED_ATTR_MASK_GROUPALONE;
14326 break;
14327 case PROCESSOR_2964_Z13:
14328 case PROCESSOR_3906_Z14:
14329 if (get_attr_z13_cracked (insn))
14330 mask |= S390_SCHED_ATTR_MASK_CRACKED;
14331 if (get_attr_z13_expanded (insn))
14332 mask |= S390_SCHED_ATTR_MASK_EXPANDED;
14333 if (get_attr_z13_endgroup (insn))
14334 mask |= S390_SCHED_ATTR_MASK_ENDGROUP;
14335 if (get_attr_z13_groupalone (insn))
14336 mask |= S390_SCHED_ATTR_MASK_GROUPALONE;
14337 break;
14338 default:
14339 gcc_unreachable ();
14341 return mask;
14344 static unsigned int
14345 s390_get_unit_mask (rtx_insn *insn, int *units)
14347 unsigned int mask = 0;
14349 switch (s390_tune)
14351 case PROCESSOR_2964_Z13:
14352 case PROCESSOR_3906_Z14:
14353 *units = 3;
14354 if (get_attr_z13_unit_lsu (insn))
14355 mask |= 1 << 0;
14356 if (get_attr_z13_unit_fxu (insn))
14357 mask |= 1 << 1;
14358 if (get_attr_z13_unit_vfu (insn))
14359 mask |= 1 << 2;
14360 break;
14361 default:
14362 gcc_unreachable ();
14364 return mask;
14367 /* Return the scheduling score for INSN. The higher the score the
14368 better. The score is calculated from the OOO scheduling attributes
14369 of INSN and the scheduling state s390_sched_state. */
14370 static int
14371 s390_sched_score (rtx_insn *insn)
14373 unsigned int mask = s390_get_sched_attrmask (insn);
14374 int score = 0;
14376 switch (s390_sched_state)
14378 case 0:
14379 /* Try to put insns into the first slot which would otherwise
14380 break a group. */
14381 if ((mask & S390_SCHED_ATTR_MASK_CRACKED) != 0
14382 || (mask & S390_SCHED_ATTR_MASK_EXPANDED) != 0)
14383 score += 5;
14384 if ((mask & S390_SCHED_ATTR_MASK_GROUPALONE) != 0)
14385 score += 10;
14386 /* fallthrough */
14387 case 1:
14388 /* Prefer not cracked insns while trying to put together a
14389 group. */
14390 if ((mask & S390_SCHED_ATTR_MASK_CRACKED) == 0
14391 && (mask & S390_SCHED_ATTR_MASK_EXPANDED) == 0
14392 && (mask & S390_SCHED_ATTR_MASK_GROUPALONE) == 0)
14393 score += 10;
14394 if ((mask & S390_SCHED_ATTR_MASK_ENDGROUP) == 0)
14395 score += 5;
14396 break;
14397 case 2:
14398 /* Prefer not cracked insns while trying to put together a
14399 group. */
14400 if ((mask & S390_SCHED_ATTR_MASK_CRACKED) == 0
14401 && (mask & S390_SCHED_ATTR_MASK_EXPANDED) == 0
14402 && (mask & S390_SCHED_ATTR_MASK_GROUPALONE) == 0)
14403 score += 10;
14404 /* Prefer endgroup insns in the last slot. */
14405 if ((mask & S390_SCHED_ATTR_MASK_ENDGROUP) != 0)
14406 score += 10;
14407 break;
14408 case S390_SCHED_STATE_NORMAL:
14409 /* Prefer not cracked insns if the last was not cracked. */
14410 if ((mask & S390_SCHED_ATTR_MASK_CRACKED) == 0
14411 && (mask & S390_SCHED_ATTR_MASK_EXPANDED) == 0)
14412 score += 5;
14413 if ((mask & S390_SCHED_ATTR_MASK_GROUPALONE) != 0)
14414 score += 10;
14415 break;
14416 case S390_SCHED_STATE_CRACKED:
14417 /* Try to keep cracked insns together to prevent them from
14418 interrupting groups. */
14419 if ((mask & S390_SCHED_ATTR_MASK_CRACKED) != 0
14420 || (mask & S390_SCHED_ATTR_MASK_EXPANDED) != 0)
14421 score += 5;
14422 break;
14425 if (s390_tune >= PROCESSOR_2964_Z13)
14427 int units, i;
14428 unsigned unit_mask, m = 1;
14430 unit_mask = s390_get_unit_mask (insn, &units);
14431 gcc_assert (units <= MAX_SCHED_UNITS);
14433 /* Add a score in range 0..MAX_SCHED_MIX_SCORE depending on how long
14434 ago the last insn of this unit type got scheduled. This is
14435 supposed to help providing a proper instruction mix to the
14436 CPU. */
14437 for (i = 0; i < units; i++, m <<= 1)
14438 if (m & unit_mask)
14439 score += (last_scheduled_unit_distance[i] * MAX_SCHED_MIX_SCORE /
14440 MAX_SCHED_MIX_DISTANCE);
14442 return score;
14445 /* This function is called via hook TARGET_SCHED_REORDER before
14446 issuing one insn from list READY which contains *NREADYP entries.
14447 For target z10 it reorders load instructions to avoid early load
14448 conflicts in the floating point pipeline */
14449 static int
14450 s390_sched_reorder (FILE *file, int verbose,
14451 rtx_insn **ready, int *nreadyp, int clock ATTRIBUTE_UNUSED)
14453 if (s390_tune == PROCESSOR_2097_Z10
14454 && reload_completed
14455 && *nreadyp > 1)
14456 s390_z10_prevent_earlyload_conflicts (ready, nreadyp);
14458 if (s390_tune >= PROCESSOR_2827_ZEC12
14459 && reload_completed
14460 && *nreadyp > 1)
14462 int i;
14463 int last_index = *nreadyp - 1;
14464 int max_index = -1;
14465 int max_score = -1;
14466 rtx_insn *tmp;
14468 /* Just move the insn with the highest score to the top (the
14469 end) of the list. A full sort is not needed since a conflict
14470 in the hazard recognition cannot happen. So the top insn in
14471 the ready list will always be taken. */
14472 for (i = last_index; i >= 0; i--)
14474 int score;
14476 if (recog_memoized (ready[i]) < 0)
14477 continue;
14479 score = s390_sched_score (ready[i]);
14480 if (score > max_score)
14482 max_score = score;
14483 max_index = i;
14487 if (max_index != -1)
14489 if (max_index != last_index)
14491 tmp = ready[max_index];
14492 ready[max_index] = ready[last_index];
14493 ready[last_index] = tmp;
14495 if (verbose > 5)
14496 fprintf (file,
14497 ";;\t\tBACKEND: move insn %d to the top of list\n",
14498 INSN_UID (ready[last_index]));
14500 else if (verbose > 5)
14501 fprintf (file,
14502 ";;\t\tBACKEND: best insn %d already on top\n",
14503 INSN_UID (ready[last_index]));
14506 if (verbose > 5)
14508 fprintf (file, "ready list ooo attributes - sched state: %d\n",
14509 s390_sched_state);
14511 for (i = last_index; i >= 0; i--)
14513 unsigned int sched_mask;
14514 rtx_insn *insn = ready[i];
14516 if (recog_memoized (insn) < 0)
14517 continue;
14519 sched_mask = s390_get_sched_attrmask (insn);
14520 fprintf (file, ";;\t\tBACKEND: insn %d score: %d: ",
14521 INSN_UID (insn),
14522 s390_sched_score (insn));
14523 #define PRINT_SCHED_ATTR(M, ATTR) fprintf (file, "%s ",\
14524 ((M) & sched_mask) ? #ATTR : "");
14525 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_CRACKED, cracked);
14526 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_EXPANDED, expanded);
14527 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_ENDGROUP, endgroup);
14528 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_GROUPALONE, groupalone);
14529 #undef PRINT_SCHED_ATTR
14530 if (s390_tune >= PROCESSOR_2964_Z13)
14532 unsigned int unit_mask, m = 1;
14533 int units, j;
14535 unit_mask = s390_get_unit_mask (insn, &units);
14536 fprintf (file, "(units:");
14537 for (j = 0; j < units; j++, m <<= 1)
14538 if (m & unit_mask)
14539 fprintf (file, " u%d", j);
14540 fprintf (file, ")");
14542 fprintf (file, "\n");
14547 return s390_issue_rate ();
14551 /* This function is called via hook TARGET_SCHED_VARIABLE_ISSUE after
14552 the scheduler has issued INSN. It stores the last issued insn into
14553 last_scheduled_insn in order to make it available for
14554 s390_sched_reorder. */
14555 static int
14556 s390_sched_variable_issue (FILE *file, int verbose, rtx_insn *insn, int more)
14558 last_scheduled_insn = insn;
14560 if (s390_tune >= PROCESSOR_2827_ZEC12
14561 && reload_completed
14562 && recog_memoized (insn) >= 0)
14564 unsigned int mask = s390_get_sched_attrmask (insn);
14566 if ((mask & S390_SCHED_ATTR_MASK_CRACKED) != 0
14567 || (mask & S390_SCHED_ATTR_MASK_EXPANDED) != 0)
14568 s390_sched_state = S390_SCHED_STATE_CRACKED;
14569 else if ((mask & S390_SCHED_ATTR_MASK_ENDGROUP) != 0
14570 || (mask & S390_SCHED_ATTR_MASK_GROUPALONE) != 0)
14571 s390_sched_state = S390_SCHED_STATE_NORMAL;
14572 else
14574 /* Only normal insns are left (mask == 0). */
14575 switch (s390_sched_state)
14577 case 0:
14578 case 1:
14579 case 2:
14580 case S390_SCHED_STATE_NORMAL:
14581 if (s390_sched_state == S390_SCHED_STATE_NORMAL)
14582 s390_sched_state = 1;
14583 else
14584 s390_sched_state++;
14586 break;
14587 case S390_SCHED_STATE_CRACKED:
14588 s390_sched_state = S390_SCHED_STATE_NORMAL;
14589 break;
14593 if (s390_tune >= PROCESSOR_2964_Z13)
14595 int units, i;
14596 unsigned unit_mask, m = 1;
14598 unit_mask = s390_get_unit_mask (insn, &units);
14599 gcc_assert (units <= MAX_SCHED_UNITS);
14601 for (i = 0; i < units; i++, m <<= 1)
14602 if (m & unit_mask)
14603 last_scheduled_unit_distance[i] = 0;
14604 else if (last_scheduled_unit_distance[i] < MAX_SCHED_MIX_DISTANCE)
14605 last_scheduled_unit_distance[i]++;
14608 if (verbose > 5)
14610 unsigned int sched_mask;
14612 sched_mask = s390_get_sched_attrmask (insn);
14614 fprintf (file, ";;\t\tBACKEND: insn %d: ", INSN_UID (insn));
14615 #define PRINT_SCHED_ATTR(M, ATTR) fprintf (file, "%s ", ((M) & sched_mask) ? #ATTR : "");
14616 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_CRACKED, cracked);
14617 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_EXPANDED, expanded);
14618 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_ENDGROUP, endgroup);
14619 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_GROUPALONE, groupalone);
14620 #undef PRINT_SCHED_ATTR
14622 if (s390_tune >= PROCESSOR_2964_Z13)
14624 unsigned int unit_mask, m = 1;
14625 int units, j;
14627 unit_mask = s390_get_unit_mask (insn, &units);
14628 fprintf (file, "(units:");
14629 for (j = 0; j < units; j++, m <<= 1)
14630 if (m & unit_mask)
14631 fprintf (file, " %d", j);
14632 fprintf (file, ")");
14634 fprintf (file, " sched state: %d\n", s390_sched_state);
14636 if (s390_tune >= PROCESSOR_2964_Z13)
14638 int units, j;
14640 s390_get_unit_mask (insn, &units);
14642 fprintf (file, ";;\t\tBACKEND: units unused for: ");
14643 for (j = 0; j < units; j++)
14644 fprintf (file, "%d:%d ", j, last_scheduled_unit_distance[j]);
14645 fprintf (file, "\n");
14650 if (GET_CODE (PATTERN (insn)) != USE
14651 && GET_CODE (PATTERN (insn)) != CLOBBER)
14652 return more - 1;
14653 else
14654 return more;
14657 static void
14658 s390_sched_init (FILE *file ATTRIBUTE_UNUSED,
14659 int verbose ATTRIBUTE_UNUSED,
14660 int max_ready ATTRIBUTE_UNUSED)
14662 last_scheduled_insn = NULL;
14663 memset (last_scheduled_unit_distance, 0, MAX_SCHED_UNITS * sizeof (int));
14664 s390_sched_state = 0;
14667 /* This target hook implementation for TARGET_LOOP_UNROLL_ADJUST calculates
14668 a new number struct loop *loop should be unrolled if tuned for cpus with
14669 a built-in stride prefetcher.
14670 The loop is analyzed for memory accesses by calling check_dpu for
14671 each rtx of the loop. Depending on the loop_depth and the amount of
14672 memory accesses a new number <=nunroll is returned to improve the
14673 behavior of the hardware prefetch unit. */
14674 static unsigned
14675 s390_loop_unroll_adjust (unsigned nunroll, struct loop *loop)
14677 basic_block *bbs;
14678 rtx_insn *insn;
14679 unsigned i;
14680 unsigned mem_count = 0;
14682 if (s390_tune < PROCESSOR_2097_Z10)
14683 return nunroll;
14685 /* Count the number of memory references within the loop body. */
14686 bbs = get_loop_body (loop);
14687 subrtx_iterator::array_type array;
14688 for (i = 0; i < loop->num_nodes; i++)
14689 FOR_BB_INSNS (bbs[i], insn)
14690 if (INSN_P (insn) && INSN_CODE (insn) != -1)
14691 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
14692 if (MEM_P (*iter))
14693 mem_count += 1;
14694 free (bbs);
14696 /* Prevent division by zero, and we do not need to adjust nunroll in this case. */
14697 if (mem_count == 0)
14698 return nunroll;
14700 switch (loop_depth(loop))
14702 case 1:
14703 return MIN (nunroll, 28 / mem_count);
14704 case 2:
14705 return MIN (nunroll, 22 / mem_count);
14706 default:
14707 return MIN (nunroll, 16 / mem_count);
14711 /* Restore the current options. This is a hook function and also called
14712 internally. */
14714 static void
14715 s390_function_specific_restore (struct gcc_options *opts,
14716 struct cl_target_option *ptr ATTRIBUTE_UNUSED)
14718 opts->x_s390_cost_pointer = (long)processor_table[opts->x_s390_tune].cost;
14721 static void
14722 s390_option_override_internal (bool main_args_p,
14723 struct gcc_options *opts,
14724 const struct gcc_options *opts_set)
14726 const char *prefix;
14727 const char *suffix;
14729 /* Set up prefix/suffix so the error messages refer to either the command
14730 line argument, or the attribute(target). */
14731 if (main_args_p)
14733 prefix = "-m";
14734 suffix = "";
14736 else
14738 prefix = "option(\"";
14739 suffix = "\")";
14743 /* Architecture mode defaults according to ABI. */
14744 if (!(opts_set->x_target_flags & MASK_ZARCH))
14746 if (TARGET_64BIT)
14747 opts->x_target_flags |= MASK_ZARCH;
14748 else
14749 opts->x_target_flags &= ~MASK_ZARCH;
14752 /* Set the march default in case it hasn't been specified on cmdline. */
14753 if (!opts_set->x_s390_arch)
14754 opts->x_s390_arch = PROCESSOR_2064_Z900;
14755 else if (opts->x_s390_arch == PROCESSOR_9672_G5
14756 || opts->x_s390_arch == PROCESSOR_9672_G6)
14757 warning (OPT_Wdeprecated, "%sarch=%s%s is deprecated and will be removed "
14758 "in future releases; use at least %sarch=z900%s",
14759 prefix, opts->x_s390_arch == PROCESSOR_9672_G5 ? "g5" : "g6",
14760 suffix, prefix, suffix);
14762 opts->x_s390_arch_flags = processor_flags_table[(int) opts->x_s390_arch];
14764 /* Determine processor to tune for. */
14765 if (!opts_set->x_s390_tune)
14766 opts->x_s390_tune = opts->x_s390_arch;
14767 else if (opts->x_s390_tune == PROCESSOR_9672_G5
14768 || opts->x_s390_tune == PROCESSOR_9672_G6)
14769 warning (OPT_Wdeprecated, "%stune=%s%s is deprecated and will be removed "
14770 "in future releases; use at least %stune=z900%s",
14771 prefix, opts->x_s390_tune == PROCESSOR_9672_G5 ? "g5" : "g6",
14772 suffix, prefix, suffix);
14774 opts->x_s390_tune_flags = processor_flags_table[opts->x_s390_tune];
14776 /* Sanity checks. */
14777 if (opts->x_s390_arch == PROCESSOR_NATIVE
14778 || opts->x_s390_tune == PROCESSOR_NATIVE)
14779 gcc_unreachable ();
14780 if (TARGET_ZARCH_P (opts->x_target_flags) && !TARGET_CPU_ZARCH_P (opts))
14781 error ("z/Architecture mode not supported on %s",
14782 processor_table[(int)opts->x_s390_arch].name);
14783 if (TARGET_64BIT && !TARGET_ZARCH_P (opts->x_target_flags))
14784 error ("64-bit ABI not supported in ESA/390 mode");
14786 /* Enable hardware transactions if available and not explicitly
14787 disabled by user. E.g. with -m31 -march=zEC12 -mzarch */
14788 if (!TARGET_OPT_HTM_P (opts_set->x_target_flags))
14790 if (TARGET_CPU_HTM_P (opts) && TARGET_ZARCH_P (opts->x_target_flags))
14791 opts->x_target_flags |= MASK_OPT_HTM;
14792 else
14793 opts->x_target_flags &= ~MASK_OPT_HTM;
14796 if (TARGET_OPT_VX_P (opts_set->x_target_flags))
14798 if (TARGET_OPT_VX_P (opts->x_target_flags))
14800 if (!TARGET_CPU_VX_P (opts))
14801 error ("hardware vector support not available on %s",
14802 processor_table[(int)opts->x_s390_arch].name);
14803 if (TARGET_SOFT_FLOAT_P (opts->x_target_flags))
14804 error ("hardware vector support not available with -msoft-float");
14807 else
14809 if (TARGET_CPU_VX_P (opts))
14810 /* Enable vector support if available and not explicitly disabled
14811 by user. E.g. with -m31 -march=z13 -mzarch */
14812 opts->x_target_flags |= MASK_OPT_VX;
14813 else
14814 opts->x_target_flags &= ~MASK_OPT_VX;
14817 /* Use hardware DFP if available and not explicitly disabled by
14818 user. E.g. with -m31 -march=z10 -mzarch */
14819 if (!TARGET_HARD_DFP_P (opts_set->x_target_flags))
14821 if (TARGET_DFP_P (opts))
14822 opts->x_target_flags |= MASK_HARD_DFP;
14823 else
14824 opts->x_target_flags &= ~MASK_HARD_DFP;
14827 if (TARGET_HARD_DFP_P (opts->x_target_flags) && !TARGET_DFP_P (opts))
14829 if (TARGET_HARD_DFP_P (opts_set->x_target_flags))
14831 if (!TARGET_CPU_DFP_P (opts))
14832 error ("hardware decimal floating point instructions"
14833 " not available on %s",
14834 processor_table[(int)opts->x_s390_arch].name);
14835 if (!TARGET_ZARCH_P (opts->x_target_flags))
14836 error ("hardware decimal floating point instructions"
14837 " not available in ESA/390 mode");
14839 else
14840 opts->x_target_flags &= ~MASK_HARD_DFP;
14843 if (TARGET_SOFT_FLOAT_P (opts_set->x_target_flags)
14844 && TARGET_SOFT_FLOAT_P (opts->x_target_flags))
14846 if (TARGET_HARD_DFP_P (opts_set->x_target_flags)
14847 && TARGET_HARD_DFP_P (opts->x_target_flags))
14848 error ("-mhard-dfp can%'t be used in conjunction with -msoft-float");
14850 opts->x_target_flags &= ~MASK_HARD_DFP;
14853 if (TARGET_BACKCHAIN_P (opts->x_target_flags)
14854 && TARGET_PACKED_STACK_P (opts->x_target_flags)
14855 && TARGET_HARD_FLOAT_P (opts->x_target_flags))
14856 error ("-mbackchain -mpacked-stack -mhard-float are not supported "
14857 "in combination");
14859 if (opts->x_s390_stack_size)
14861 if (opts->x_s390_stack_guard >= opts->x_s390_stack_size)
14862 error ("stack size must be greater than the stack guard value");
14863 else if (opts->x_s390_stack_size > 1 << 16)
14864 error ("stack size must not be greater than 64k");
14866 else if (opts->x_s390_stack_guard)
14867 error ("-mstack-guard implies use of -mstack-size");
14869 #ifdef TARGET_DEFAULT_LONG_DOUBLE_128
14870 if (!TARGET_LONG_DOUBLE_128_P (opts_set->x_target_flags))
14871 opts->x_target_flags |= MASK_LONG_DOUBLE_128;
14872 #endif
14874 if (opts->x_s390_tune >= PROCESSOR_2097_Z10)
14876 maybe_set_param_value (PARAM_MAX_UNROLLED_INSNS, 100,
14877 opts->x_param_values,
14878 opts_set->x_param_values);
14879 maybe_set_param_value (PARAM_MAX_UNROLL_TIMES, 32,
14880 opts->x_param_values,
14881 opts_set->x_param_values);
14882 maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 2000,
14883 opts->x_param_values,
14884 opts_set->x_param_values);
14885 maybe_set_param_value (PARAM_MAX_COMPLETELY_PEEL_TIMES, 64,
14886 opts->x_param_values,
14887 opts_set->x_param_values);
14890 maybe_set_param_value (PARAM_MAX_PENDING_LIST_LENGTH, 256,
14891 opts->x_param_values,
14892 opts_set->x_param_values);
14893 /* values for loop prefetching */
14894 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE, 256,
14895 opts->x_param_values,
14896 opts_set->x_param_values);
14897 maybe_set_param_value (PARAM_L1_CACHE_SIZE, 128,
14898 opts->x_param_values,
14899 opts_set->x_param_values);
14900 /* s390 has more than 2 levels and the size is much larger. Since
14901 we are always running virtualized assume that we only get a small
14902 part of the caches above l1. */
14903 maybe_set_param_value (PARAM_L2_CACHE_SIZE, 1500,
14904 opts->x_param_values,
14905 opts_set->x_param_values);
14906 maybe_set_param_value (PARAM_PREFETCH_MIN_INSN_TO_MEM_RATIO, 2,
14907 opts->x_param_values,
14908 opts_set->x_param_values);
14909 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES, 6,
14910 opts->x_param_values,
14911 opts_set->x_param_values);
14913 /* Use the alternative scheduling-pressure algorithm by default. */
14914 maybe_set_param_value (PARAM_SCHED_PRESSURE_ALGORITHM, 2,
14915 opts->x_param_values,
14916 opts_set->x_param_values);
14918 maybe_set_param_value (PARAM_MIN_VECT_LOOP_BOUND, 2,
14919 opts->x_param_values,
14920 opts_set->x_param_values);
14922 /* Call target specific restore function to do post-init work. At the moment,
14923 this just sets opts->x_s390_cost_pointer. */
14924 s390_function_specific_restore (opts, NULL);
14927 static void
14928 s390_option_override (void)
14930 unsigned int i;
14931 cl_deferred_option *opt;
14932 vec<cl_deferred_option> *v =
14933 (vec<cl_deferred_option> *) s390_deferred_options;
14935 if (v)
14936 FOR_EACH_VEC_ELT (*v, i, opt)
14938 switch (opt->opt_index)
14940 case OPT_mhotpatch_:
14942 int val1;
14943 int val2;
14944 char s[256];
14945 char *t;
14947 strncpy (s, opt->arg, 256);
14948 s[255] = 0;
14949 t = strchr (s, ',');
14950 if (t != NULL)
14952 *t = 0;
14953 t++;
14954 val1 = integral_argument (s);
14955 val2 = integral_argument (t);
14957 else
14959 val1 = -1;
14960 val2 = -1;
14962 if (val1 == -1 || val2 == -1)
14964 /* argument is not a plain number */
14965 error ("arguments to %qs should be non-negative integers",
14966 "-mhotpatch=n,m");
14967 break;
14969 else if (val1 > s390_hotpatch_hw_max
14970 || val2 > s390_hotpatch_hw_max)
14972 error ("argument to %qs is too large (max. %d)",
14973 "-mhotpatch=n,m", s390_hotpatch_hw_max);
14974 break;
14976 s390_hotpatch_hw_before_label = val1;
14977 s390_hotpatch_hw_after_label = val2;
14978 break;
14980 default:
14981 gcc_unreachable ();
14985 /* Set up function hooks. */
14986 init_machine_status = s390_init_machine_status;
14988 s390_option_override_internal (true, &global_options, &global_options_set);
14990 /* Save the initial options in case the user does function specific
14991 options. */
14992 target_option_default_node = build_target_option_node (&global_options);
14993 target_option_current_node = target_option_default_node;
14995 /* This cannot reside in s390_option_optimization_table since HAVE_prefetch
14996 requires the arch flags to be evaluated already. Since prefetching
14997 is beneficial on s390, we enable it if available. */
14998 if (flag_prefetch_loop_arrays < 0 && HAVE_prefetch && optimize >= 3)
14999 flag_prefetch_loop_arrays = 1;
15001 if (!s390_pic_data_is_text_relative && !flag_pic)
15002 error ("-mno-pic-data-is-text-relative cannot be used without -fpic/-fPIC");
15004 if (TARGET_TPF)
15006 /* Don't emit DWARF3/4 unless specifically selected. The TPF
15007 debuggers do not yet support DWARF 3/4. */
15008 if (!global_options_set.x_dwarf_strict)
15009 dwarf_strict = 1;
15010 if (!global_options_set.x_dwarf_version)
15011 dwarf_version = 2;
15014 /* Register a target-specific optimization-and-lowering pass
15015 to run immediately before prologue and epilogue generation.
15017 Registering the pass must be done at start up. It's
15018 convenient to do it here. */
15019 opt_pass *new_pass = new pass_s390_early_mach (g);
15020 struct register_pass_info insert_pass_s390_early_mach =
15022 new_pass, /* pass */
15023 "pro_and_epilogue", /* reference_pass_name */
15024 1, /* ref_pass_instance_number */
15025 PASS_POS_INSERT_BEFORE /* po_op */
15027 register_pass (&insert_pass_s390_early_mach);
15030 #if S390_USE_TARGET_ATTRIBUTE
15031 /* Inner function to process the attribute((target(...))), take an argument and
15032 set the current options from the argument. If we have a list, recursively go
15033 over the list. */
15035 static bool
15036 s390_valid_target_attribute_inner_p (tree args,
15037 struct gcc_options *opts,
15038 struct gcc_options *new_opts_set,
15039 bool force_pragma)
15041 char *next_optstr;
15042 bool ret = true;
15044 #define S390_ATTRIB(S,O,A) { S, sizeof (S)-1, O, A, 0 }
15045 #define S390_PRAGMA(S,O,A) { S, sizeof (S)-1, O, A, 1 }
15046 static const struct
15048 const char *string;
15049 size_t len;
15050 int opt;
15051 int has_arg;
15052 int only_as_pragma;
15053 } attrs[] = {
15054 /* enum options */
15055 S390_ATTRIB ("arch=", OPT_march_, 1),
15056 S390_ATTRIB ("tune=", OPT_mtune_, 1),
15057 /* uinteger options */
15058 S390_ATTRIB ("stack-guard=", OPT_mstack_guard_, 1),
15059 S390_ATTRIB ("stack-size=", OPT_mstack_size_, 1),
15060 S390_ATTRIB ("branch-cost=", OPT_mbranch_cost_, 1),
15061 S390_ATTRIB ("warn-framesize=", OPT_mwarn_framesize_, 1),
15062 /* flag options */
15063 S390_ATTRIB ("backchain", OPT_mbackchain, 0),
15064 S390_ATTRIB ("hard-dfp", OPT_mhard_dfp, 0),
15065 S390_ATTRIB ("hard-float", OPT_mhard_float, 0),
15066 S390_ATTRIB ("htm", OPT_mhtm, 0),
15067 S390_ATTRIB ("vx", OPT_mvx, 0),
15068 S390_ATTRIB ("packed-stack", OPT_mpacked_stack, 0),
15069 S390_ATTRIB ("small-exec", OPT_msmall_exec, 0),
15070 S390_ATTRIB ("soft-float", OPT_msoft_float, 0),
15071 S390_ATTRIB ("mvcle", OPT_mmvcle, 0),
15072 S390_PRAGMA ("zvector", OPT_mzvector, 0),
15073 /* boolean options */
15074 S390_ATTRIB ("warn-dynamicstack", OPT_mwarn_dynamicstack, 0),
15076 #undef S390_ATTRIB
15077 #undef S390_PRAGMA
15079 /* If this is a list, recurse to get the options. */
15080 if (TREE_CODE (args) == TREE_LIST)
15082 bool ret = true;
15083 int num_pragma_values;
15084 int i;
15086 /* Note: attribs.c:decl_attributes prepends the values from
15087 current_target_pragma to the list of target attributes. To determine
15088 whether we're looking at a value of the attribute or the pragma we
15089 assume that the first [list_length (current_target_pragma)] values in
15090 the list are the values from the pragma. */
15091 num_pragma_values = (!force_pragma && current_target_pragma != NULL)
15092 ? list_length (current_target_pragma) : 0;
15093 for (i = 0; args; args = TREE_CHAIN (args), i++)
15095 bool is_pragma;
15097 is_pragma = (force_pragma || i < num_pragma_values);
15098 if (TREE_VALUE (args)
15099 && !s390_valid_target_attribute_inner_p (TREE_VALUE (args),
15100 opts, new_opts_set,
15101 is_pragma))
15103 ret = false;
15106 return ret;
15109 else if (TREE_CODE (args) != STRING_CST)
15111 error ("attribute %<target%> argument not a string");
15112 return false;
15115 /* Handle multiple arguments separated by commas. */
15116 next_optstr = ASTRDUP (TREE_STRING_POINTER (args));
15118 while (next_optstr && *next_optstr != '\0')
15120 char *p = next_optstr;
15121 char *orig_p = p;
15122 char *comma = strchr (next_optstr, ',');
15123 size_t len, opt_len;
15124 int opt;
15125 bool opt_set_p;
15126 char ch;
15127 unsigned i;
15128 int mask = 0;
15129 enum cl_var_type var_type;
15130 bool found;
15132 if (comma)
15134 *comma = '\0';
15135 len = comma - next_optstr;
15136 next_optstr = comma + 1;
15138 else
15140 len = strlen (p);
15141 next_optstr = NULL;
15144 /* Recognize no-xxx. */
15145 if (len > 3 && p[0] == 'n' && p[1] == 'o' && p[2] == '-')
15147 opt_set_p = false;
15148 p += 3;
15149 len -= 3;
15151 else
15152 opt_set_p = true;
15154 /* Find the option. */
15155 ch = *p;
15156 found = false;
15157 for (i = 0; i < ARRAY_SIZE (attrs); i++)
15159 opt_len = attrs[i].len;
15160 if (ch == attrs[i].string[0]
15161 && ((attrs[i].has_arg) ? len > opt_len : len == opt_len)
15162 && memcmp (p, attrs[i].string, opt_len) == 0)
15164 opt = attrs[i].opt;
15165 if (!opt_set_p && cl_options[opt].cl_reject_negative)
15166 continue;
15167 mask = cl_options[opt].var_value;
15168 var_type = cl_options[opt].var_type;
15169 found = true;
15170 break;
15174 /* Process the option. */
15175 if (!found)
15177 error ("attribute(target(\"%s\")) is unknown", orig_p);
15178 return false;
15180 else if (attrs[i].only_as_pragma && !force_pragma)
15182 /* Value is not allowed for the target attribute. */
15183 error ("value %qs is not supported by attribute %<target%>",
15184 attrs[i].string);
15185 return false;
15188 else if (var_type == CLVC_BIT_SET || var_type == CLVC_BIT_CLEAR)
15190 if (var_type == CLVC_BIT_CLEAR)
15191 opt_set_p = !opt_set_p;
15193 if (opt_set_p)
15194 opts->x_target_flags |= mask;
15195 else
15196 opts->x_target_flags &= ~mask;
15197 new_opts_set->x_target_flags |= mask;
15200 else if (cl_options[opt].var_type == CLVC_BOOLEAN)
15202 int value;
15204 if (cl_options[opt].cl_uinteger)
15206 /* Unsigned integer argument. Code based on the function
15207 decode_cmdline_option () in opts-common.c. */
15208 value = integral_argument (p + opt_len);
15210 else
15211 value = (opt_set_p) ? 1 : 0;
15213 if (value != -1)
15215 struct cl_decoded_option decoded;
15217 /* Value range check; only implemented for numeric and boolean
15218 options at the moment. */
15219 generate_option (opt, NULL, value, CL_TARGET, &decoded);
15220 s390_handle_option (opts, new_opts_set, &decoded, input_location);
15221 set_option (opts, new_opts_set, opt, value,
15222 p + opt_len, DK_UNSPECIFIED, input_location,
15223 global_dc);
15225 else
15227 error ("attribute(target(\"%s\")) is unknown", orig_p);
15228 ret = false;
15232 else if (cl_options[opt].var_type == CLVC_ENUM)
15234 bool arg_ok;
15235 int value;
15237 arg_ok = opt_enum_arg_to_value (opt, p + opt_len, &value, CL_TARGET);
15238 if (arg_ok)
15239 set_option (opts, new_opts_set, opt, value,
15240 p + opt_len, DK_UNSPECIFIED, input_location,
15241 global_dc);
15242 else
15244 error ("attribute(target(\"%s\")) is unknown", orig_p);
15245 ret = false;
15249 else
15250 gcc_unreachable ();
15252 return ret;
15255 /* Return a TARGET_OPTION_NODE tree of the target options listed or NULL. */
15257 tree
15258 s390_valid_target_attribute_tree (tree args,
15259 struct gcc_options *opts,
15260 const struct gcc_options *opts_set,
15261 bool force_pragma)
15263 tree t = NULL_TREE;
15264 struct gcc_options new_opts_set;
15266 memset (&new_opts_set, 0, sizeof (new_opts_set));
15268 /* Process each of the options on the chain. */
15269 if (! s390_valid_target_attribute_inner_p (args, opts, &new_opts_set,
15270 force_pragma))
15271 return error_mark_node;
15273 /* If some option was set (even if it has not changed), rerun
15274 s390_option_override_internal, and then save the options away. */
15275 if (new_opts_set.x_target_flags
15276 || new_opts_set.x_s390_arch
15277 || new_opts_set.x_s390_tune
15278 || new_opts_set.x_s390_stack_guard
15279 || new_opts_set.x_s390_stack_size
15280 || new_opts_set.x_s390_branch_cost
15281 || new_opts_set.x_s390_warn_framesize
15282 || new_opts_set.x_s390_warn_dynamicstack_p)
15284 const unsigned char *src = (const unsigned char *)opts_set;
15285 unsigned char *dest = (unsigned char *)&new_opts_set;
15286 unsigned int i;
15288 /* Merge the original option flags into the new ones. */
15289 for (i = 0; i < sizeof(*opts_set); i++)
15290 dest[i] |= src[i];
15292 /* Do any overrides, such as arch=xxx, or tune=xxx support. */
15293 s390_option_override_internal (false, opts, &new_opts_set);
15294 /* Save the current options unless we are validating options for
15295 #pragma. */
15296 t = build_target_option_node (opts);
15298 return t;
15301 /* Hook to validate attribute((target("string"))). */
15303 static bool
15304 s390_valid_target_attribute_p (tree fndecl,
15305 tree ARG_UNUSED (name),
15306 tree args,
15307 int ARG_UNUSED (flags))
15309 struct gcc_options func_options;
15310 tree new_target, new_optimize;
15311 bool ret = true;
15313 /* attribute((target("default"))) does nothing, beyond
15314 affecting multi-versioning. */
15315 if (TREE_VALUE (args)
15316 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
15317 && TREE_CHAIN (args) == NULL_TREE
15318 && strcmp (TREE_STRING_POINTER (TREE_VALUE (args)), "default") == 0)
15319 return true;
15321 tree old_optimize = build_optimization_node (&global_options);
15323 /* Get the optimization options of the current function. */
15324 tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
15326 if (!func_optimize)
15327 func_optimize = old_optimize;
15329 /* Init func_options. */
15330 memset (&func_options, 0, sizeof (func_options));
15331 init_options_struct (&func_options, NULL);
15332 lang_hooks.init_options_struct (&func_options);
15334 cl_optimization_restore (&func_options, TREE_OPTIMIZATION (func_optimize));
15336 /* Initialize func_options to the default before its target options can
15337 be set. */
15338 cl_target_option_restore (&func_options,
15339 TREE_TARGET_OPTION (target_option_default_node));
15341 new_target = s390_valid_target_attribute_tree (args, &func_options,
15342 &global_options_set,
15343 (args ==
15344 current_target_pragma));
15345 new_optimize = build_optimization_node (&func_options);
15346 if (new_target == error_mark_node)
15347 ret = false;
15348 else if (fndecl && new_target)
15350 DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
15351 if (old_optimize != new_optimize)
15352 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
15354 return ret;
15357 /* Restore targets globals from NEW_TREE and invalidate s390_previous_fndecl
15358 cache. */
15360 void
15361 s390_activate_target_options (tree new_tree)
15363 cl_target_option_restore (&global_options, TREE_TARGET_OPTION (new_tree));
15364 if (TREE_TARGET_GLOBALS (new_tree))
15365 restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
15366 else if (new_tree == target_option_default_node)
15367 restore_target_globals (&default_target_globals);
15368 else
15369 TREE_TARGET_GLOBALS (new_tree) = save_target_globals_default_opts ();
15370 s390_previous_fndecl = NULL_TREE;
15373 /* Establish appropriate back-end context for processing the function
15374 FNDECL. The argument might be NULL to indicate processing at top
15375 level, outside of any function scope. */
15376 static void
15377 s390_set_current_function (tree fndecl)
15379 /* Only change the context if the function changes. This hook is called
15380 several times in the course of compiling a function, and we don't want to
15381 slow things down too much or call target_reinit when it isn't safe. */
15382 if (fndecl == s390_previous_fndecl)
15383 return;
15385 tree old_tree;
15386 if (s390_previous_fndecl == NULL_TREE)
15387 old_tree = target_option_current_node;
15388 else if (DECL_FUNCTION_SPECIFIC_TARGET (s390_previous_fndecl))
15389 old_tree = DECL_FUNCTION_SPECIFIC_TARGET (s390_previous_fndecl);
15390 else
15391 old_tree = target_option_default_node;
15393 if (fndecl == NULL_TREE)
15395 if (old_tree != target_option_current_node)
15396 s390_activate_target_options (target_option_current_node);
15397 return;
15400 tree new_tree = DECL_FUNCTION_SPECIFIC_TARGET (fndecl);
15401 if (new_tree == NULL_TREE)
15402 new_tree = target_option_default_node;
15404 if (old_tree != new_tree)
15405 s390_activate_target_options (new_tree);
15406 s390_previous_fndecl = fndecl;
15408 #endif
15410 /* Implement TARGET_USE_BY_PIECES_INFRASTRUCTURE_P. */
15412 static bool
15413 s390_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size,
15414 unsigned int align ATTRIBUTE_UNUSED,
15415 enum by_pieces_operation op ATTRIBUTE_UNUSED,
15416 bool speed_p ATTRIBUTE_UNUSED)
15418 return (size == 1 || size == 2
15419 || size == 4 || (TARGET_ZARCH && size == 8));
15422 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV hook. */
15424 static void
15425 s390_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
15427 tree sfpc = s390_builtin_decls[S390_BUILTIN_s390_sfpc];
15428 tree efpc = s390_builtin_decls[S390_BUILTIN_s390_efpc];
15429 tree call_efpc = build_call_expr (efpc, 0);
15430 tree fenv_var = create_tmp_var_raw (unsigned_type_node);
15432 #define FPC_EXCEPTION_MASK HOST_WIDE_INT_UC (0xf8000000)
15433 #define FPC_FLAGS_MASK HOST_WIDE_INT_UC (0x00f80000)
15434 #define FPC_DXC_MASK HOST_WIDE_INT_UC (0x0000ff00)
15435 #define FPC_EXCEPTION_MASK_SHIFT HOST_WIDE_INT_UC (24)
15436 #define FPC_FLAGS_SHIFT HOST_WIDE_INT_UC (16)
15437 #define FPC_DXC_SHIFT HOST_WIDE_INT_UC (8)
15439 /* Generates the equivalent of feholdexcept (&fenv_var)
15441 fenv_var = __builtin_s390_efpc ();
15442 __builtin_s390_sfpc (fenv_var & mask) */
15443 tree old_fpc = build2 (MODIFY_EXPR, unsigned_type_node, fenv_var, call_efpc);
15444 tree new_fpc =
15445 build2 (BIT_AND_EXPR, unsigned_type_node, fenv_var,
15446 build_int_cst (unsigned_type_node,
15447 ~(FPC_DXC_MASK | FPC_FLAGS_MASK |
15448 FPC_EXCEPTION_MASK)));
15449 tree set_new_fpc = build_call_expr (sfpc, 1, new_fpc);
15450 *hold = build2 (COMPOUND_EXPR, void_type_node, old_fpc, set_new_fpc);
15452 /* Generates the equivalent of feclearexcept (FE_ALL_EXCEPT)
15454 __builtin_s390_sfpc (__builtin_s390_efpc () & mask) */
15455 new_fpc = build2 (BIT_AND_EXPR, unsigned_type_node, call_efpc,
15456 build_int_cst (unsigned_type_node,
15457 ~(FPC_DXC_MASK | FPC_FLAGS_MASK)));
15458 *clear = build_call_expr (sfpc, 1, new_fpc);
15460 /* Generates the equivalent of feupdateenv (fenv_var)
15462 old_fpc = __builtin_s390_efpc ();
15463 __builtin_s390_sfpc (fenv_var);
15464 __atomic_feraiseexcept ((old_fpc & FPC_FLAGS_MASK) >> FPC_FLAGS_SHIFT); */
15466 old_fpc = create_tmp_var_raw (unsigned_type_node);
15467 tree store_old_fpc = build2 (MODIFY_EXPR, void_type_node,
15468 old_fpc, call_efpc);
15470 set_new_fpc = build_call_expr (sfpc, 1, fenv_var);
15472 tree raise_old_except = build2 (BIT_AND_EXPR, unsigned_type_node, old_fpc,
15473 build_int_cst (unsigned_type_node,
15474 FPC_FLAGS_MASK));
15475 raise_old_except = build2 (RSHIFT_EXPR, unsigned_type_node, raise_old_except,
15476 build_int_cst (unsigned_type_node,
15477 FPC_FLAGS_SHIFT));
15478 tree atomic_feraiseexcept
15479 = builtin_decl_implicit (BUILT_IN_ATOMIC_FERAISEEXCEPT);
15480 raise_old_except = build_call_expr (atomic_feraiseexcept,
15481 1, raise_old_except);
15483 *update = build2 (COMPOUND_EXPR, void_type_node,
15484 build2 (COMPOUND_EXPR, void_type_node,
15485 store_old_fpc, set_new_fpc),
15486 raise_old_except);
15488 #undef FPC_EXCEPTION_MASK
15489 #undef FPC_FLAGS_MASK
15490 #undef FPC_DXC_MASK
15491 #undef FPC_EXCEPTION_MASK_SHIFT
15492 #undef FPC_FLAGS_SHIFT
15493 #undef FPC_DXC_SHIFT
15496 /* Return the vector mode to be used for inner mode MODE when doing
15497 vectorization. */
15498 static machine_mode
15499 s390_preferred_simd_mode (machine_mode mode)
15501 if (TARGET_VX)
15502 switch (mode)
15504 case E_DFmode:
15505 return V2DFmode;
15506 case E_DImode:
15507 return V2DImode;
15508 case E_SImode:
15509 return V4SImode;
15510 case E_HImode:
15511 return V8HImode;
15512 case E_QImode:
15513 return V16QImode;
15514 default:;
15516 return word_mode;
15519 /* Our hardware does not require vectors to be strictly aligned. */
15520 static bool
15521 s390_support_vector_misalignment (machine_mode mode ATTRIBUTE_UNUSED,
15522 const_tree type ATTRIBUTE_UNUSED,
15523 int misalignment ATTRIBUTE_UNUSED,
15524 bool is_packed ATTRIBUTE_UNUSED)
15526 if (TARGET_VX)
15527 return true;
15529 return default_builtin_support_vector_misalignment (mode, type, misalignment,
15530 is_packed);
15533 /* The vector ABI requires vector types to be aligned on an 8 byte
15534 boundary (our stack alignment). However, we allow this to be
15535 overriden by the user, while this definitely breaks the ABI. */
15536 static HOST_WIDE_INT
15537 s390_vector_alignment (const_tree type)
15539 if (!TARGET_VX_ABI)
15540 return default_vector_alignment (type);
15542 if (TYPE_USER_ALIGN (type))
15543 return TYPE_ALIGN (type);
15545 return MIN (64, tree_to_shwi (TYPE_SIZE (type)));
15548 #ifdef HAVE_AS_MACHINE_MACHINEMODE
15549 /* Implement TARGET_ASM_FILE_START. */
15550 static void
15551 s390_asm_file_start (void)
15553 default_file_start ();
15554 s390_asm_output_machine_for_arch (asm_out_file);
15556 #endif
15558 /* Implement TARGET_ASM_FILE_END. */
15559 static void
15560 s390_asm_file_end (void)
15562 #ifdef HAVE_AS_GNU_ATTRIBUTE
15563 varpool_node *vnode;
15564 cgraph_node *cnode;
15566 FOR_EACH_VARIABLE (vnode)
15567 if (TREE_PUBLIC (vnode->decl))
15568 s390_check_type_for_vector_abi (TREE_TYPE (vnode->decl), false, false);
15570 FOR_EACH_FUNCTION (cnode)
15571 if (TREE_PUBLIC (cnode->decl))
15572 s390_check_type_for_vector_abi (TREE_TYPE (cnode->decl), false, false);
15575 if (s390_vector_abi != 0)
15576 fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
15577 s390_vector_abi);
15578 #endif
15579 file_end_indicate_exec_stack ();
15581 if (flag_split_stack)
15582 file_end_indicate_split_stack ();
15585 /* Return true if TYPE is a vector bool type. */
15586 static inline bool
15587 s390_vector_bool_type_p (const_tree type)
15589 return TYPE_VECTOR_OPAQUE (type);
15592 /* Return the diagnostic message string if the binary operation OP is
15593 not permitted on TYPE1 and TYPE2, NULL otherwise. */
15594 static const char*
15595 s390_invalid_binary_op (int op ATTRIBUTE_UNUSED, const_tree type1, const_tree type2)
15597 bool bool1_p, bool2_p;
15598 bool plusminus_p;
15599 bool muldiv_p;
15600 bool compare_p;
15601 machine_mode mode1, mode2;
15603 if (!TARGET_ZVECTOR)
15604 return NULL;
15606 if (!VECTOR_TYPE_P (type1) || !VECTOR_TYPE_P (type2))
15607 return NULL;
15609 bool1_p = s390_vector_bool_type_p (type1);
15610 bool2_p = s390_vector_bool_type_p (type2);
15612 /* Mixing signed and unsigned types is forbidden for all
15613 operators. */
15614 if (!bool1_p && !bool2_p
15615 && TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2))
15616 return N_("types differ in signedness");
15618 plusminus_p = (op == PLUS_EXPR || op == MINUS_EXPR);
15619 muldiv_p = (op == MULT_EXPR || op == RDIV_EXPR || op == TRUNC_DIV_EXPR
15620 || op == CEIL_DIV_EXPR || op == FLOOR_DIV_EXPR
15621 || op == ROUND_DIV_EXPR);
15622 compare_p = (op == LT_EXPR || op == LE_EXPR || op == GT_EXPR || op == GE_EXPR
15623 || op == EQ_EXPR || op == NE_EXPR);
15625 if (bool1_p && bool2_p && (plusminus_p || muldiv_p))
15626 return N_("binary operator does not support two vector bool operands");
15628 if (bool1_p != bool2_p && (muldiv_p || compare_p))
15629 return N_("binary operator does not support vector bool operand");
15631 mode1 = TYPE_MODE (type1);
15632 mode2 = TYPE_MODE (type2);
15634 if (bool1_p != bool2_p && plusminus_p
15635 && (GET_MODE_CLASS (mode1) == MODE_VECTOR_FLOAT
15636 || GET_MODE_CLASS (mode2) == MODE_VECTOR_FLOAT))
15637 return N_("binary operator does not support mixing vector "
15638 "bool with floating point vector operands");
15640 return NULL;
15643 /* Implement TARGET_C_EXCESS_PRECISION.
15645 FIXME: For historical reasons, float_t and double_t are typedef'ed to
15646 double on s390, causing operations on float_t to operate in a higher
15647 precision than is necessary. However, it is not the case that SFmode
15648 operations have implicit excess precision, and we generate more optimal
15649 code if we let the compiler know no implicit extra precision is added.
15651 That means when we are compiling with -fexcess-precision=fast, the value
15652 we set for FLT_EVAL_METHOD will be out of line with the actual precision of
15653 float_t (though they would be correct for -fexcess-precision=standard).
15655 A complete fix would modify glibc to remove the unnecessary typedef
15656 of float_t to double. */
15658 static enum flt_eval_method
15659 s390_excess_precision (enum excess_precision_type type)
15661 switch (type)
15663 case EXCESS_PRECISION_TYPE_IMPLICIT:
15664 case EXCESS_PRECISION_TYPE_FAST:
15665 /* The fastest type to promote to will always be the native type,
15666 whether that occurs with implicit excess precision or
15667 otherwise. */
15668 return FLT_EVAL_METHOD_PROMOTE_TO_FLOAT;
15669 case EXCESS_PRECISION_TYPE_STANDARD:
15670 /* Otherwise, when we are in a standards compliant mode, to
15671 ensure consistency with the implementation in glibc, report that
15672 float is evaluated to the range and precision of double. */
15673 return FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE;
15674 default:
15675 gcc_unreachable ();
15677 return FLT_EVAL_METHOD_UNPREDICTABLE;
15680 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
15682 static unsigned HOST_WIDE_INT
15683 s390_asan_shadow_offset (void)
15685 return TARGET_64BIT ? HOST_WIDE_INT_1U << 52 : HOST_WIDE_INT_UC (0x20000000);
15688 /* Initialize GCC target structure. */
15690 #undef TARGET_ASM_ALIGNED_HI_OP
15691 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
15692 #undef TARGET_ASM_ALIGNED_DI_OP
15693 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
15694 #undef TARGET_ASM_INTEGER
15695 #define TARGET_ASM_INTEGER s390_assemble_integer
15697 #undef TARGET_ASM_OPEN_PAREN
15698 #define TARGET_ASM_OPEN_PAREN ""
15700 #undef TARGET_ASM_CLOSE_PAREN
15701 #define TARGET_ASM_CLOSE_PAREN ""
15703 #undef TARGET_OPTION_OVERRIDE
15704 #define TARGET_OPTION_OVERRIDE s390_option_override
15706 #ifdef TARGET_THREAD_SSP_OFFSET
15707 #undef TARGET_STACK_PROTECT_GUARD
15708 #define TARGET_STACK_PROTECT_GUARD hook_tree_void_null
15709 #endif
15711 #undef TARGET_ENCODE_SECTION_INFO
15712 #define TARGET_ENCODE_SECTION_INFO s390_encode_section_info
15714 #undef TARGET_SCALAR_MODE_SUPPORTED_P
15715 #define TARGET_SCALAR_MODE_SUPPORTED_P s390_scalar_mode_supported_p
15717 #ifdef HAVE_AS_TLS
15718 #undef TARGET_HAVE_TLS
15719 #define TARGET_HAVE_TLS true
15720 #endif
15721 #undef TARGET_CANNOT_FORCE_CONST_MEM
15722 #define TARGET_CANNOT_FORCE_CONST_MEM s390_cannot_force_const_mem
15724 #undef TARGET_DELEGITIMIZE_ADDRESS
15725 #define TARGET_DELEGITIMIZE_ADDRESS s390_delegitimize_address
15727 #undef TARGET_LEGITIMIZE_ADDRESS
15728 #define TARGET_LEGITIMIZE_ADDRESS s390_legitimize_address
15730 #undef TARGET_RETURN_IN_MEMORY
15731 #define TARGET_RETURN_IN_MEMORY s390_return_in_memory
15733 #undef TARGET_INIT_BUILTINS
15734 #define TARGET_INIT_BUILTINS s390_init_builtins
15735 #undef TARGET_EXPAND_BUILTIN
15736 #define TARGET_EXPAND_BUILTIN s390_expand_builtin
15737 #undef TARGET_BUILTIN_DECL
15738 #define TARGET_BUILTIN_DECL s390_builtin_decl
15740 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
15741 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA s390_output_addr_const_extra
15743 #undef TARGET_ASM_OUTPUT_MI_THUNK
15744 #define TARGET_ASM_OUTPUT_MI_THUNK s390_output_mi_thunk
15745 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
15746 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
15748 #undef TARGET_C_EXCESS_PRECISION
15749 #define TARGET_C_EXCESS_PRECISION s390_excess_precision
15751 #undef TARGET_SCHED_ADJUST_PRIORITY
15752 #define TARGET_SCHED_ADJUST_PRIORITY s390_adjust_priority
15753 #undef TARGET_SCHED_ISSUE_RATE
15754 #define TARGET_SCHED_ISSUE_RATE s390_issue_rate
15755 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
15756 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD s390_first_cycle_multipass_dfa_lookahead
15758 #undef TARGET_SCHED_VARIABLE_ISSUE
15759 #define TARGET_SCHED_VARIABLE_ISSUE s390_sched_variable_issue
15760 #undef TARGET_SCHED_REORDER
15761 #define TARGET_SCHED_REORDER s390_sched_reorder
15762 #undef TARGET_SCHED_INIT
15763 #define TARGET_SCHED_INIT s390_sched_init
15765 #undef TARGET_CANNOT_COPY_INSN_P
15766 #define TARGET_CANNOT_COPY_INSN_P s390_cannot_copy_insn_p
15767 #undef TARGET_RTX_COSTS
15768 #define TARGET_RTX_COSTS s390_rtx_costs
15769 #undef TARGET_ADDRESS_COST
15770 #define TARGET_ADDRESS_COST s390_address_cost
15771 #undef TARGET_REGISTER_MOVE_COST
15772 #define TARGET_REGISTER_MOVE_COST s390_register_move_cost
15773 #undef TARGET_MEMORY_MOVE_COST
15774 #define TARGET_MEMORY_MOVE_COST s390_memory_move_cost
15775 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
15776 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
15777 s390_builtin_vectorization_cost
15779 #undef TARGET_MACHINE_DEPENDENT_REORG
15780 #define TARGET_MACHINE_DEPENDENT_REORG s390_reorg
15782 #undef TARGET_VALID_POINTER_MODE
15783 #define TARGET_VALID_POINTER_MODE s390_valid_pointer_mode
15785 #undef TARGET_BUILD_BUILTIN_VA_LIST
15786 #define TARGET_BUILD_BUILTIN_VA_LIST s390_build_builtin_va_list
15787 #undef TARGET_EXPAND_BUILTIN_VA_START
15788 #define TARGET_EXPAND_BUILTIN_VA_START s390_va_start
15789 #undef TARGET_ASAN_SHADOW_OFFSET
15790 #define TARGET_ASAN_SHADOW_OFFSET s390_asan_shadow_offset
15791 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
15792 #define TARGET_GIMPLIFY_VA_ARG_EXPR s390_gimplify_va_arg
15794 #undef TARGET_PROMOTE_FUNCTION_MODE
15795 #define TARGET_PROMOTE_FUNCTION_MODE s390_promote_function_mode
15796 #undef TARGET_PASS_BY_REFERENCE
15797 #define TARGET_PASS_BY_REFERENCE s390_pass_by_reference
15799 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
15800 #define TARGET_FUNCTION_OK_FOR_SIBCALL s390_function_ok_for_sibcall
15801 #undef TARGET_FUNCTION_ARG
15802 #define TARGET_FUNCTION_ARG s390_function_arg
15803 #undef TARGET_FUNCTION_ARG_ADVANCE
15804 #define TARGET_FUNCTION_ARG_ADVANCE s390_function_arg_advance
15805 #undef TARGET_FUNCTION_VALUE
15806 #define TARGET_FUNCTION_VALUE s390_function_value
15807 #undef TARGET_LIBCALL_VALUE
15808 #define TARGET_LIBCALL_VALUE s390_libcall_value
15809 #undef TARGET_STRICT_ARGUMENT_NAMING
15810 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
15812 #undef TARGET_KEEP_LEAF_WHEN_PROFILED
15813 #define TARGET_KEEP_LEAF_WHEN_PROFILED s390_keep_leaf_when_profiled
15815 #undef TARGET_FIXED_CONDITION_CODE_REGS
15816 #define TARGET_FIXED_CONDITION_CODE_REGS s390_fixed_condition_code_regs
15818 #undef TARGET_CC_MODES_COMPATIBLE
15819 #define TARGET_CC_MODES_COMPATIBLE s390_cc_modes_compatible
15821 #undef TARGET_INVALID_WITHIN_DOLOOP
15822 #define TARGET_INVALID_WITHIN_DOLOOP hook_constcharptr_const_rtx_insn_null
15824 #ifdef HAVE_AS_TLS
15825 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
15826 #define TARGET_ASM_OUTPUT_DWARF_DTPREL s390_output_dwarf_dtprel
15827 #endif
15829 #undef TARGET_DWARF_FRAME_REG_MODE
15830 #define TARGET_DWARF_FRAME_REG_MODE s390_dwarf_frame_reg_mode
15832 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
15833 #undef TARGET_MANGLE_TYPE
15834 #define TARGET_MANGLE_TYPE s390_mangle_type
15835 #endif
15837 #undef TARGET_SCALAR_MODE_SUPPORTED_P
15838 #define TARGET_SCALAR_MODE_SUPPORTED_P s390_scalar_mode_supported_p
15840 #undef TARGET_VECTOR_MODE_SUPPORTED_P
15841 #define TARGET_VECTOR_MODE_SUPPORTED_P s390_vector_mode_supported_p
15843 #undef TARGET_PREFERRED_RELOAD_CLASS
15844 #define TARGET_PREFERRED_RELOAD_CLASS s390_preferred_reload_class
15846 #undef TARGET_SECONDARY_RELOAD
15847 #define TARGET_SECONDARY_RELOAD s390_secondary_reload
15849 #undef TARGET_LIBGCC_CMP_RETURN_MODE
15850 #define TARGET_LIBGCC_CMP_RETURN_MODE s390_libgcc_cmp_return_mode
15852 #undef TARGET_LIBGCC_SHIFT_COUNT_MODE
15853 #define TARGET_LIBGCC_SHIFT_COUNT_MODE s390_libgcc_shift_count_mode
15855 #undef TARGET_LEGITIMATE_ADDRESS_P
15856 #define TARGET_LEGITIMATE_ADDRESS_P s390_legitimate_address_p
15858 #undef TARGET_LEGITIMATE_CONSTANT_P
15859 #define TARGET_LEGITIMATE_CONSTANT_P s390_legitimate_constant_p
15861 #undef TARGET_LRA_P
15862 #define TARGET_LRA_P s390_lra_p
15864 #undef TARGET_CAN_ELIMINATE
15865 #define TARGET_CAN_ELIMINATE s390_can_eliminate
15867 #undef TARGET_CONDITIONAL_REGISTER_USAGE
15868 #define TARGET_CONDITIONAL_REGISTER_USAGE s390_conditional_register_usage
15870 #undef TARGET_LOOP_UNROLL_ADJUST
15871 #define TARGET_LOOP_UNROLL_ADJUST s390_loop_unroll_adjust
15873 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
15874 #define TARGET_ASM_TRAMPOLINE_TEMPLATE s390_asm_trampoline_template
15875 #undef TARGET_TRAMPOLINE_INIT
15876 #define TARGET_TRAMPOLINE_INIT s390_trampoline_init
15878 /* PR 79421 */
15879 #undef TARGET_CUSTOM_FUNCTION_DESCRIPTORS
15880 #define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
15882 #undef TARGET_UNWIND_WORD_MODE
15883 #define TARGET_UNWIND_WORD_MODE s390_unwind_word_mode
15885 #undef TARGET_CANONICALIZE_COMPARISON
15886 #define TARGET_CANONICALIZE_COMPARISON s390_canonicalize_comparison
15888 #undef TARGET_HARD_REGNO_SCRATCH_OK
15889 #define TARGET_HARD_REGNO_SCRATCH_OK s390_hard_regno_scratch_ok
15891 #undef TARGET_ATTRIBUTE_TABLE
15892 #define TARGET_ATTRIBUTE_TABLE s390_attribute_table
15894 #undef TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P
15895 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P hook_bool_const_tree_true
15897 #undef TARGET_SET_UP_BY_PROLOGUE
15898 #define TARGET_SET_UP_BY_PROLOGUE s300_set_up_by_prologue
15900 #undef TARGET_EXTRA_LIVE_ON_ENTRY
15901 #define TARGET_EXTRA_LIVE_ON_ENTRY s390_live_on_entry
15903 #undef TARGET_USE_BY_PIECES_INFRASTRUCTURE_P
15904 #define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \
15905 s390_use_by_pieces_infrastructure_p
15907 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
15908 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV s390_atomic_assign_expand_fenv
15910 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
15911 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN s390_invalid_arg_for_unprototyped_fn
15913 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
15914 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE s390_preferred_simd_mode
15916 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
15917 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT s390_support_vector_misalignment
15919 #undef TARGET_VECTOR_ALIGNMENT
15920 #define TARGET_VECTOR_ALIGNMENT s390_vector_alignment
15922 #undef TARGET_INVALID_BINARY_OP
15923 #define TARGET_INVALID_BINARY_OP s390_invalid_binary_op
15925 #ifdef HAVE_AS_MACHINE_MACHINEMODE
15926 #undef TARGET_ASM_FILE_START
15927 #define TARGET_ASM_FILE_START s390_asm_file_start
15928 #endif
15930 #undef TARGET_ASM_FILE_END
15931 #define TARGET_ASM_FILE_END s390_asm_file_end
15933 #if S390_USE_TARGET_ATTRIBUTE
15934 #undef TARGET_SET_CURRENT_FUNCTION
15935 #define TARGET_SET_CURRENT_FUNCTION s390_set_current_function
15937 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
15938 #define TARGET_OPTION_VALID_ATTRIBUTE_P s390_valid_target_attribute_p
15939 #endif
15941 #undef TARGET_OPTION_RESTORE
15942 #define TARGET_OPTION_RESTORE s390_function_specific_restore
15944 struct gcc_target targetm = TARGET_INITIALIZER;
15946 #include "gt-s390.h"