Debian Codegraph @

gcc/config/s390/s390.cc

raw
1 /* Subroutines used for code generation on IBM S/390 and zSeries
2 Copyright (C) 1999-2025 Free Software Foundation, Inc.
3 Contributed by Hartmut Penner ([email protected]) and
4 Ulrich Weigand ([email protected]) and
5 Andreas Krebbel ([email protected]).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #define IN_TARGET_CODE 1
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "backend.h"
29 #include "target.h"
30 #include "target-globals.h"
31 #include "rtl.h"
32 #include "tree.h"
33 #include "gimple.h"
34 #include "cfghooks.h"
35 #include "cfgloop.h"
36 #include "df.h"
37 #include "memmodel.h"
38 #include "tm_p.h"
39 #include "stringpool.h"
40 #include "attribs.h"
41 #include "expmed.h"
42 #include "optabs.h"
43 #include "regs.h"
44 #include "emit-rtl.h"
45 #include "recog.h"
46 #include "cgraph.h"
47 #include "diagnostic-core.h"
48 #include "diagnostic.h"
49 #include "alias.h"
50 #include "fold-const.h"
51 #include "print-tree.h"
52 #include "stor-layout.h"
53 #include "varasm.h"
54 #include "calls.h"
55 #include "conditions.h"
56 #include "output.h"
57 #include "insn-attr.h"
58 #include "flags.h"
59 #include "except.h"
60 #include "dojump.h"
61 #include "explow.h"
62 #include "stmt.h"
63 #include "expr.h"
64 #include "reload.h"
65 #include "cfgrtl.h"
66 #include "cfganal.h"
67 #include "lcm.h"
68 #include "cfgbuild.h"
69 #include "cfgcleanup.h"
70 #include "debug.h"
71 #include "langhooks.h"
72 #include "internal-fn.h"
73 #include "gimple-iterator.h"
74 #include "gimple-fold.h"
75 #include "tree-eh.h"
76 #include "gimplify.h"
77 #include "opts.h"
78 #include "tree-pass.h"
79 #include "context.h"
80 #include "builtins.h"
81 #include "ifcvt.h"
82 #include "rtl-iter.h"
83 #include "intl.h"
84 #include "tm-constrs.h"
85 #include "tree-vrp.h"
86 #include "symbol-summary.h"
87 #include "sreal.h"
88 #include "ipa-cp.h"
89 #include "ipa-prop.h"
90 #include "ipa-fnsummary.h"
91 #include "sched-int.h"
92
93 /* This file should be included last. */
94 #include "target-def.h"
95
96 static bool s390_hard_regno_mode_ok (unsigned int, machine_mode);
97
98 /* Remember the last target of s390_set_current_function. */
99 static GTY(()) tree s390_previous_fndecl;
100
101 /* Define the specific costs for a given cpu. */
102
103 struct processor_costs
104 {
105 /* multiplication */
106 const int m; /* cost of an M instruction. */
107 const int mghi; /* cost of an MGHI instruction. */
108 const int mh; /* cost of an MH instruction. */
109 const int mhi; /* cost of an MHI instruction. */
110 const int ml; /* cost of an ML instruction. */
111 const int mr; /* cost of an MR instruction. */
112 const int ms; /* cost of an MS instruction. */
113 const int msg; /* cost of an MSG instruction. */
114 const int msgf; /* cost of an MSGF instruction. */
115 const int msgfr; /* cost of an MSGFR instruction. */
116 const int msgr; /* cost of an MSGR instruction. */
117 const int msr; /* cost of an MSR instruction. */
118 const int mult_df; /* cost of multiplication in DFmode. */
119 const int mxbr;
120 /* square root */
121 const int sqxbr; /* cost of square root in TFmode. */
122 const int sqdbr; /* cost of square root in DFmode. */
123 const int sqebr; /* cost of square root in SFmode. */
124 /* multiply and add */
125 const int madbr; /* cost of multiply and add in DFmode. */
126 const int maebr; /* cost of multiply and add in SFmode. */
127 /* division */
128 const int dxbr;
129 const int ddbr;
130 const int debr;
131 const int dlgr;
132 const int dlr;
133 const int dr;
134 const int dsgfr;
135 const int dsgr;
136 };
137
138 #define s390_cost ((const struct processor_costs *)(s390_cost_pointer))
139
140 static const
141 struct processor_costs z900_cost =
142 {
143 COSTS_N_INSNS (5), /* M */
144 COSTS_N_INSNS (10), /* MGHI */
145 COSTS_N_INSNS (5), /* MH */
146 COSTS_N_INSNS (4), /* MHI */
147 COSTS_N_INSNS (5), /* ML */
148 COSTS_N_INSNS (5), /* MR */
149 COSTS_N_INSNS (4), /* MS */
150 COSTS_N_INSNS (15), /* MSG */
151 COSTS_N_INSNS (7), /* MSGF */
152 COSTS_N_INSNS (7), /* MSGFR */
153 COSTS_N_INSNS (10), /* MSGR */
154 COSTS_N_INSNS (4), /* MSR */
155 COSTS_N_INSNS (7), /* multiplication in DFmode */
156 COSTS_N_INSNS (13), /* MXBR */
157 COSTS_N_INSNS (136), /* SQXBR */
158 COSTS_N_INSNS (44), /* SQDBR */
159 COSTS_N_INSNS (35), /* SQEBR */
160 COSTS_N_INSNS (18), /* MADBR */
161 COSTS_N_INSNS (13), /* MAEBR */
162 COSTS_N_INSNS (134), /* DXBR */
163 COSTS_N_INSNS (30), /* DDBR */
164 COSTS_N_INSNS (27), /* DEBR */
165 COSTS_N_INSNS (220), /* DLGR */
166 COSTS_N_INSNS (34), /* DLR */
167 COSTS_N_INSNS (34), /* DR */
168 COSTS_N_INSNS (32), /* DSGFR */
169 COSTS_N_INSNS (32), /* DSGR */
170 };
171
172 static const
173 struct processor_costs z990_cost =
174 {
175 COSTS_N_INSNS (4), /* M */
176 COSTS_N_INSNS (2), /* MGHI */
177 COSTS_N_INSNS (2), /* MH */
178 COSTS_N_INSNS (2), /* MHI */
179 COSTS_N_INSNS (4), /* ML */
180 COSTS_N_INSNS (4), /* MR */
181 COSTS_N_INSNS (5), /* MS */
182 COSTS_N_INSNS (6), /* MSG */
183 COSTS_N_INSNS (4), /* MSGF */
184 COSTS_N_INSNS (4), /* MSGFR */
185 COSTS_N_INSNS (4), /* MSGR */
186 COSTS_N_INSNS (4), /* MSR */
187 COSTS_N_INSNS (1), /* multiplication in DFmode */
188 COSTS_N_INSNS (28), /* MXBR */
189 COSTS_N_INSNS (130), /* SQXBR */
190 COSTS_N_INSNS (66), /* SQDBR */
191 COSTS_N_INSNS (38), /* SQEBR */
192 COSTS_N_INSNS (1), /* MADBR */
193 COSTS_N_INSNS (1), /* MAEBR */
194 COSTS_N_INSNS (60), /* DXBR */
195 COSTS_N_INSNS (40), /* DDBR */
196 COSTS_N_INSNS (26), /* DEBR */
197 COSTS_N_INSNS (176), /* DLGR */
198 COSTS_N_INSNS (31), /* DLR */
199 COSTS_N_INSNS (31), /* DR */
200 COSTS_N_INSNS (31), /* DSGFR */
201 COSTS_N_INSNS (31), /* DSGR */
202 };
203
204 static const
205 struct processor_costs z9_109_cost =
206 {
207 COSTS_N_INSNS (4), /* M */
208 COSTS_N_INSNS (2), /* MGHI */
209 COSTS_N_INSNS (2), /* MH */
210 COSTS_N_INSNS (2), /* MHI */
211 COSTS_N_INSNS (4), /* ML */
212 COSTS_N_INSNS (4), /* MR */
213 COSTS_N_INSNS (5), /* MS */
214 COSTS_N_INSNS (6), /* MSG */
215 COSTS_N_INSNS (4), /* MSGF */
216 COSTS_N_INSNS (4), /* MSGFR */
217 COSTS_N_INSNS (4), /* MSGR */
218 COSTS_N_INSNS (4), /* MSR */
219 COSTS_N_INSNS (1), /* multiplication in DFmode */
220 COSTS_N_INSNS (28), /* MXBR */
221 COSTS_N_INSNS (130), /* SQXBR */
222 COSTS_N_INSNS (66), /* SQDBR */
223 COSTS_N_INSNS (38), /* SQEBR */
224 COSTS_N_INSNS (1), /* MADBR */
225 COSTS_N_INSNS (1), /* MAEBR */
226 COSTS_N_INSNS (60), /* DXBR */
227 COSTS_N_INSNS (40), /* DDBR */
228 COSTS_N_INSNS (26), /* DEBR */
229 COSTS_N_INSNS (30), /* DLGR */
230 COSTS_N_INSNS (23), /* DLR */
231 COSTS_N_INSNS (23), /* DR */
232 COSTS_N_INSNS (24), /* DSGFR */
233 COSTS_N_INSNS (24), /* DSGR */
234 };
235
236 static const
237 struct processor_costs z10_cost =
238 {
239 COSTS_N_INSNS (10), /* M */
240 COSTS_N_INSNS (10), /* MGHI */
241 COSTS_N_INSNS (10), /* MH */
242 COSTS_N_INSNS (10), /* MHI */
243 COSTS_N_INSNS (10), /* ML */
244 COSTS_N_INSNS (10), /* MR */
245 COSTS_N_INSNS (10), /* MS */
246 COSTS_N_INSNS (10), /* MSG */
247 COSTS_N_INSNS (10), /* MSGF */
248 COSTS_N_INSNS (10), /* MSGFR */
249 COSTS_N_INSNS (10), /* MSGR */
250 COSTS_N_INSNS (10), /* MSR */
251 COSTS_N_INSNS (1) , /* multiplication in DFmode */
252 COSTS_N_INSNS (50), /* MXBR */
253 COSTS_N_INSNS (120), /* SQXBR */
254 COSTS_N_INSNS (52), /* SQDBR */
255 COSTS_N_INSNS (38), /* SQEBR */
256 COSTS_N_INSNS (1), /* MADBR */
257 COSTS_N_INSNS (1), /* MAEBR */
258 COSTS_N_INSNS (111), /* DXBR */
259 COSTS_N_INSNS (39), /* DDBR */
260 COSTS_N_INSNS (32), /* DEBR */
261 COSTS_N_INSNS (160), /* DLGR */
262 COSTS_N_INSNS (71), /* DLR */
263 COSTS_N_INSNS (71), /* DR */
264 COSTS_N_INSNS (71), /* DSGFR */
265 COSTS_N_INSNS (71), /* DSGR */
266 };
267
268 static const
269 struct processor_costs z196_cost =
270 {
271 COSTS_N_INSNS (7), /* M */
272 COSTS_N_INSNS (5), /* MGHI */
273 COSTS_N_INSNS (5), /* MH */
274 COSTS_N_INSNS (5), /* MHI */
275 COSTS_N_INSNS (7), /* ML */
276 COSTS_N_INSNS (7), /* MR */
277 COSTS_N_INSNS (6), /* MS */
278 COSTS_N_INSNS (8), /* MSG */
279 COSTS_N_INSNS (6), /* MSGF */
280 COSTS_N_INSNS (6), /* MSGFR */
281 COSTS_N_INSNS (8), /* MSGR */
282 COSTS_N_INSNS (6), /* MSR */
283 COSTS_N_INSNS (1) , /* multiplication in DFmode */
284 COSTS_N_INSNS (40), /* MXBR B+40 */
285 COSTS_N_INSNS (100), /* SQXBR B+100 */
286 COSTS_N_INSNS (42), /* SQDBR B+42 */
287 COSTS_N_INSNS (28), /* SQEBR B+28 */
288 COSTS_N_INSNS (1), /* MADBR B */
289 COSTS_N_INSNS (1), /* MAEBR B */
290 COSTS_N_INSNS (101), /* DXBR B+101 */
291 COSTS_N_INSNS (29), /* DDBR */
292 COSTS_N_INSNS (22), /* DEBR */
293 COSTS_N_INSNS (160), /* DLGR cracked */
294 COSTS_N_INSNS (160), /* DLR cracked */
295 COSTS_N_INSNS (160), /* DR expanded */
296 COSTS_N_INSNS (160), /* DSGFR cracked */
297 COSTS_N_INSNS (160), /* DSGR cracked */
298 };
299
300 static const
301 struct processor_costs zEC12_cost =
302 {
303 COSTS_N_INSNS (7), /* M */
304 COSTS_N_INSNS (5), /* MGHI */
305 COSTS_N_INSNS (5), /* MH */
306 COSTS_N_INSNS (5), /* MHI */
307 COSTS_N_INSNS (7), /* ML */
308 COSTS_N_INSNS (7), /* MR */
309 COSTS_N_INSNS (6), /* MS */
310 COSTS_N_INSNS (8), /* MSG */
311 COSTS_N_INSNS (6), /* MSGF */
312 COSTS_N_INSNS (6), /* MSGFR */
313 COSTS_N_INSNS (8), /* MSGR */
314 COSTS_N_INSNS (6), /* MSR */
315 COSTS_N_INSNS (1) , /* multiplication in DFmode */
316 COSTS_N_INSNS (40), /* MXBR B+40 */
317 COSTS_N_INSNS (100), /* SQXBR B+100 */
318 COSTS_N_INSNS (42), /* SQDBR B+42 */
319 COSTS_N_INSNS (28), /* SQEBR B+28 */
320 COSTS_N_INSNS (1), /* MADBR B */
321 COSTS_N_INSNS (1), /* MAEBR B */
322 COSTS_N_INSNS (131), /* DXBR B+131 */
323 COSTS_N_INSNS (29), /* DDBR */
324 COSTS_N_INSNS (22), /* DEBR */
325 COSTS_N_INSNS (160), /* DLGR cracked */
326 COSTS_N_INSNS (160), /* DLR cracked */
327 COSTS_N_INSNS (160), /* DR expanded */
328 COSTS_N_INSNS (160), /* DSGFR cracked */
329 COSTS_N_INSNS (160), /* DSGR cracked */
330 };
331
332 const struct s390_processor processor_table[] =
333 {
334 { "z900", "z900", PROCESSOR_2064_Z900, &z900_cost, 5 },
335 { "z990", "z990", PROCESSOR_2084_Z990, &z990_cost, 6 },
336 { "z9-109", "z9-109", PROCESSOR_2094_Z9_109, &z9_109_cost, 7 },
337 { "z9-ec", "z9-ec", PROCESSOR_2094_Z9_EC, &z9_109_cost, 7 },
338 { "z10", "z10", PROCESSOR_2097_Z10, &z10_cost, 8 },
339 { "z196", "z196", PROCESSOR_2817_Z196, &z196_cost, 9 },
340 { "zEC12", "zEC12", PROCESSOR_2827_ZEC12, &zEC12_cost, 10 },
341 { "z13", "z13", PROCESSOR_2964_Z13, &zEC12_cost, 11 },
342 { "z14", "arch12", PROCESSOR_3906_Z14, &zEC12_cost, 12 },
343 { "z15", "arch13", PROCESSOR_8561_Z15, &zEC12_cost, 13 },
344 { "z16", "arch14", PROCESSOR_3931_Z16, &zEC12_cost, 14 },
345 { "z17", "arch15", PROCESSOR_9175_Z17, &zEC12_cost, 15 },
346 { "native", "", PROCESSOR_NATIVE, NULL, 0 }
347 };
348
349 extern int reload_completed;
350
351 /* Kept up to date using the SCHED_VARIABLE_ISSUE hook. */
352 static rtx_insn *last_scheduled_insn;
353 #define NUM_SIDES 2
354
355 #define MAX_SCHED_UNITS 4
356 static int last_scheduled_unit_distance[MAX_SCHED_UNITS][NUM_SIDES];
357
358 /* Estimate of number of cycles a long-running insn occupies an
359 execution unit. */
360 static int fxd_longrunning[NUM_SIDES];
361 static int fpd_longrunning[NUM_SIDES];
362
363 /* The maximum score added for an instruction whose unit hasn't been
364 in use for MAX_SCHED_MIX_DISTANCE steps. Increase this value to
365 give instruction mix scheduling more priority over instruction
366 grouping. */
367 #define MAX_SCHED_MIX_SCORE 2
368
369 /* The maximum distance up to which individual scores will be
370 calculated. Everything beyond this gives MAX_SCHED_MIX_SCORE.
371 Increase this with the OOO windows size of the machine. */
372 #define MAX_SCHED_MIX_DISTANCE 70
373
374 /* Structure used to hold the components of a S/390 memory
375 address. A legitimate address on S/390 is of the general
376 form
377 base + index + displacement
378 where any of the components is optional.
379
380 base and index are registers of the class ADDR_REGS,
381 displacement is an unsigned 12-bit immediate constant. */
382
383 /* The max number of insns of backend generated memset/memcpy/memcmp
384 loops. This value is used in the unroll adjust hook to detect such
385 loops. Current max is 9 coming from the memcmp loop. */
386 #define BLOCK_MEM_OPS_LOOP_INSNS 9
387
388 struct s390_address
389 {
390 rtx base;
391 rtx indx;
392 rtx disp;
393 bool pointer;
394 bool literal_pool;
395 };
396
397 /* Few accessor macros for struct cfun->machine->s390_frame_layout. */
398
399 #define cfun_frame_layout (cfun->machine->frame_layout)
400 #define cfun_save_high_fprs_p (!!cfun_frame_layout.high_fprs)
401 #define cfun_save_arg_fprs_p (!!(TARGET_64BIT \
402 ? cfun_frame_layout.fpr_bitmap & 0x0f \
403 : cfun_frame_layout.fpr_bitmap & 0x03))
404 #define cfun_gprs_save_area_size ((cfun_frame_layout.last_save_gpr_slot - \
405 cfun_frame_layout.first_save_gpr_slot + 1) * UNITS_PER_LONG)
406 #define cfun_set_fpr_save(REGNO) (cfun->machine->frame_layout.fpr_bitmap |= \
407 (1 << (REGNO - FPR0_REGNUM)))
408 #define cfun_fpr_save_p(REGNO) (!!(cfun->machine->frame_layout.fpr_bitmap & \
409 (1 << (REGNO - FPR0_REGNUM))))
410 #define cfun_gpr_save_slot(REGNO) \
411 cfun->machine->frame_layout.gpr_save_slots[REGNO]
412
413 /* Number of GPRs and FPRs used for argument passing. */
414 #define GP_ARG_NUM_REG 5
415 #define FP_ARG_NUM_REG (TARGET_64BIT? 4 : 2)
416 #define VEC_ARG_NUM_REG 8
417
418 /* Return TRUE if GPR REGNO is supposed to be restored in the function
419 epilogue. */
420 static inline bool
421 s390_restore_gpr_p (int regno)
422 {
423 return (cfun_frame_layout.first_restore_gpr != -1
424 && regno >= cfun_frame_layout.first_restore_gpr
425 && regno <= cfun_frame_layout.last_restore_gpr);
426 }
427
428 /* Return TRUE if any of the registers in range [FIRST, LAST] is saved
429 because of -mpreserve-args. */
430 static inline bool
431 s390_preserve_gpr_arg_in_range_p (int first, int last)
432 {
433 int num_arg_regs = MIN (crtl->args.info.gprs + cfun->va_list_gpr_size,
434 GP_ARG_NUM_REG);
435 return (num_arg_regs
436 && s390_preserve_args_p
437 && first <= GPR2_REGNUM + num_arg_regs - 1
438 && last >= GPR2_REGNUM);
439 }
440
441 static inline bool
442 s390_preserve_gpr_arg_p (int regno)
443 {
444 return s390_preserve_gpr_arg_in_range_p (regno, regno);
445 }
446
447 static inline bool
448 s390_preserve_fpr_arg_p (int regno)
449 {
450 int num_arg_regs = MIN (crtl->args.info.fprs + cfun->va_list_fpr_size,
451 FP_ARG_NUM_REG);
452 return (s390_preserve_args_p
453 && regno <= FPR0_REGNUM + num_arg_regs - 1
454 && regno >= FPR0_REGNUM);
455 }
456
457 #undef TARGET_ATOMIC_ALIGN_FOR_MODE
458 #define TARGET_ATOMIC_ALIGN_FOR_MODE s390_atomic_align_for_mode
459 static unsigned int
460 s390_atomic_align_for_mode (machine_mode mode)
461 {
462 return GET_MODE_BITSIZE (mode);
463 }
464
465 /* A couple of shortcuts. */
466 #define CONST_OK_FOR_J(x) \
467 CONST_OK_FOR_CONSTRAINT_P((x), 'J', "J")
468 #define CONST_OK_FOR_K(x) \
469 CONST_OK_FOR_CONSTRAINT_P((x), 'K', "K")
470 #define CONST_OK_FOR_Os(x) \
471 CONST_OK_FOR_CONSTRAINT_P((x), 'O', "Os")
472 #define CONST_OK_FOR_Op(x) \
473 CONST_OK_FOR_CONSTRAINT_P((x), 'O', "Op")
474 #define CONST_OK_FOR_On(x) \
475 CONST_OK_FOR_CONSTRAINT_P((x), 'O', "On")
476
477 #define REGNO_PAIR_OK(REGNO, MODE) \
478 (s390_hard_regno_nregs ((REGNO), (MODE)) == 1 || !((REGNO) & 1))
479
480 /* That's the read ahead of the dynamic branch prediction unit in
481 bytes on a z10 (or higher) CPU. */
482 #define PREDICT_DISTANCE (TARGET_Z10 ? 384 : 2048)
483
484 static int
485 s390_address_cost (rtx addr, machine_mode mode ATTRIBUTE_UNUSED,
486 addr_space_t as ATTRIBUTE_UNUSED,
487 bool speed ATTRIBUTE_UNUSED);
488
489 static unsigned int
490 s390_hard_regno_nregs (unsigned int regno, machine_mode mode);
491
492 /* Masks per jump target register indicating which thunk need to be
493 generated. */
494 static GTY(()) int indirect_branch_prez10thunk_mask = 0;
495 static GTY(()) int indirect_branch_z10thunk_mask = 0;
496
497 #define INDIRECT_BRANCH_NUM_OPTIONS 4
498
499 enum s390_indirect_branch_option
500 {
501 s390_opt_indirect_branch_jump = 0,
502 s390_opt_indirect_branch_call,
503 s390_opt_function_return_reg,
504 s390_opt_function_return_mem
505 };
506
507 static GTY(()) int indirect_branch_table_label_no[INDIRECT_BRANCH_NUM_OPTIONS] = { 0 };
508 const char *indirect_branch_table_label[INDIRECT_BRANCH_NUM_OPTIONS] = \
509 { "LJUMP", "LCALL", "LRETREG", "LRETMEM" };
510 const char *indirect_branch_table_name[INDIRECT_BRANCH_NUM_OPTIONS] = \
511 { ".s390_indirect_jump", ".s390_indirect_call",
512 ".s390_return_reg", ".s390_return_mem" };
513
514 bool
515 s390_return_addr_from_memory ()
516 {
517 return cfun_gpr_save_slot(RETURN_REGNUM) == SAVE_SLOT_STACK;
518 }
519
520 /* Return nonzero if it's OK to use fused multiply-add for MODE. */
521 bool
522 s390_fma_allowed_p (machine_mode mode)
523 {
524 if (TARGET_VXE && mode == TFmode)
525 return flag_vx_long_double_fma;
526
527 return true;
528 }
529
530 /* Indicate which ABI has been used for passing vector args.
531 0 - no vector type arguments have been passed where the ABI is relevant
532 1 - the old ABI has been used
533 2 - a vector type argument has been passed either in a vector register
534 or on the stack by value */
535 static int s390_vector_abi = 0;
536
537 /* Set the vector ABI marker if TYPE is subject to the vector ABI
538 switch. The vector ABI affects only vector data types. There are
539 two aspects of the vector ABI relevant here:
540
541 1. vectors >= 16 bytes have an alignment of 8 bytes with the new
542 ABI and natural alignment with the old.
543
544 2. vector <= 16 bytes are passed in VRs or by value on the stack
545 with the new ABI but by reference on the stack with the old.
546
547 If ARG_P is true TYPE is used for a function argument or return
548 value. The ABI marker then is set for all vector data types. If
549 ARG_P is false only type 1 vectors are being checked. */
550
551 static void
552 s390_check_type_for_vector_abi (const_tree type, bool arg_p, bool in_struct_p)
553 {
554 static hash_set<const_tree> visited_types_hash;
555
556 if (s390_vector_abi)
557 return;
558
559 if (type == NULL_TREE || TREE_CODE (type) == ERROR_MARK)
560 return;
561
562 if (visited_types_hash.contains (type))
563 return;
564
565 visited_types_hash.add (type);
566
567 if (VECTOR_TYPE_P (type))
568 {
569 int type_size = int_size_in_bytes (type);
570
571 /* Outside arguments only the alignment is changing and this
572 only happens for vector types >= 16 bytes. */
573 if (!arg_p && type_size < 16)
574 return;
575
576 /* In arguments vector types > 16 are passed as before (GCC
577 never enforced the bigger alignment for arguments which was
578 required by the old vector ABI). However, it might still be
579 ABI relevant due to the changed alignment if it is a struct
580 member. */
581 if (arg_p && type_size > 16 && !in_struct_p)
582 return;
583
584 s390_vector_abi = TARGET_VX_ABI ? 2 : 1;
585 }
586 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
587 {
588 /* ARRAY_TYPE: Since with neither of the ABIs we have more than
589 natural alignment there will never be ABI dependent padding
590 in an array type. That's why we do not set in_struct_p to
591 true here. */
592 s390_check_type_for_vector_abi (TREE_TYPE (type), arg_p, in_struct_p);
593 }
594 else if (FUNC_OR_METHOD_TYPE_P (type))
595 {
596 tree arg_chain;
597
598 /* Check the return type. */
599 s390_check_type_for_vector_abi (TREE_TYPE (type), true, false);
600
601 for (arg_chain = TYPE_ARG_TYPES (type);
602 arg_chain;
603 arg_chain = TREE_CHAIN (arg_chain))
604 s390_check_type_for_vector_abi (TREE_VALUE (arg_chain), true, false);
605 }
606 else if (RECORD_OR_UNION_TYPE_P (type))
607 {
608 tree field;
609
610 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
611 {
612 if (TREE_CODE (field) != FIELD_DECL)
613 continue;
614
615 s390_check_type_for_vector_abi (TREE_TYPE (field), arg_p, true);
616 }
617 }
618 }
619
620
621 /* System z builtins. */
622
623 #include "s390-builtins.h"
624
625 const unsigned int bflags_builtin[S390_BUILTIN_MAX + 1] =
626 {
627 #undef B_DEF
628 #undef OB_DEF
629 #undef OB_DEF_VAR
630 #define B_DEF(NAME, PATTERN, ATTRS, BFLAGS, ...) BFLAGS,
631 #define OB_DEF(...)
632 #define OB_DEF_VAR(...)
633 #include "s390-builtins.def"
634 0
635 };
636
637 const unsigned int opflags_builtin[S390_BUILTIN_MAX + 1] =
638 {
639 #undef B_DEF
640 #undef OB_DEF
641 #undef OB_DEF_VAR
642 #define B_DEF(NAME, PATTERN, ATTRS, BFLAGS, OPFLAGS, ...) OPFLAGS,
643 #define OB_DEF(...)
644 #define OB_DEF_VAR(...)
645 #include "s390-builtins.def"
646 0
647 };
648
649 const unsigned int bflags_overloaded_builtin[S390_OVERLOADED_BUILTIN_MAX + 1] =
650 {
651 #undef B_DEF
652 #undef OB_DEF
653 #undef OB_DEF_VAR
654 #define B_DEF(...)
655 #define OB_DEF(NAME, FIRST_VAR_NAME, LAST_VAR_NAME, BFLAGS, ...) BFLAGS,
656 #define OB_DEF_VAR(...)
657 #include "s390-builtins.def"
658 0
659 };
660
661 const unsigned int
662 bflags_overloaded_builtin_var[S390_OVERLOADED_BUILTIN_VAR_MAX + 1] =
663 {
664 #undef B_DEF
665 #undef OB_DEF
666 #undef OB_DEF_VAR
667 #define B_DEF(...)
668 #define OB_DEF(...)
669 #define OB_DEF_VAR(NAME, PATTERN, FLAGS, OPFLAGS, FNTYPE) FLAGS,
670 #include "s390-builtins.def"
671 0
672 };
673
674 const unsigned int
675 opflags_overloaded_builtin_var[S390_OVERLOADED_BUILTIN_VAR_MAX + 1] =
676 {
677 #undef B_DEF
678 #undef OB_DEF
679 #undef OB_DEF_VAR
680 #define B_DEF(...)
681 #define OB_DEF(...)
682 #define OB_DEF_VAR(NAME, PATTERN, FLAGS, OPFLAGS, FNTYPE) OPFLAGS,
683 #include "s390-builtins.def"
684 0
685 };
686
687 tree s390_builtin_types[BT_MAX];
688 tree s390_builtin_fn_types[BT_FN_MAX];
689 tree s390_builtin_decls[S390_BUILTIN_MAX +
690 S390_OVERLOADED_BUILTIN_MAX +
691 S390_OVERLOADED_BUILTIN_VAR_MAX];
692
693 static enum insn_code const code_for_builtin[S390_BUILTIN_MAX + 1] = {
694 #undef B_DEF
695 #undef OB_DEF
696 #undef OB_DEF_VAR
697 #define B_DEF(NAME, PATTERN, ...) CODE_FOR_##PATTERN,
698 #define OB_DEF(...)
699 #define OB_DEF_VAR(...)
700
701 #include "s390-builtins.def"
702 CODE_FOR_nothing
703 };
704
705 static void
706 s390_init_builtins (void)
707 {
708 /* These definitions are being used in s390-builtins.def. */
709 tree returns_twice_attr = tree_cons (get_identifier ("returns_twice"),
710 NULL, NULL);
711 tree noreturn_attr = tree_cons (get_identifier ("noreturn"), NULL, NULL);
712 tree c_uint64_type_node;
713
714 /* The uint64_type_node from tree.cc is not compatible to the C99
715 uint64_t data type. What we want is c_uint64_type_node from
716 c-common.cc. But since backend code is not supposed to interface
717 with the frontend we recreate it here. */
718 if (TARGET_64BIT)
719 c_uint64_type_node = long_unsigned_type_node;
720 else
721 c_uint64_type_node = long_long_unsigned_type_node;
722
723 #undef DEF_TYPE
724 #define DEF_TYPE(INDEX, NODE, CONST_P) \
725 if (s390_builtin_types[INDEX] == NULL) \
726 s390_builtin_types[INDEX] = (!CONST_P) ? \
727 (NODE) : build_type_variant ((NODE), 1, 0);
728
729 #undef DEF_POINTER_TYPE
730 #define DEF_POINTER_TYPE(INDEX, INDEX_BASE) \
731 if (s390_builtin_types[INDEX] == NULL) \
732 s390_builtin_types[INDEX] = \
733 build_pointer_type (s390_builtin_types[INDEX_BASE]);
734
735 #undef DEF_DISTINCT_TYPE
736 #define DEF_DISTINCT_TYPE(INDEX, INDEX_BASE) \
737 if (s390_builtin_types[INDEX] == NULL) \
738 s390_builtin_types[INDEX] = \
739 build_distinct_type_copy (s390_builtin_types[INDEX_BASE]);
740
741 #undef DEF_VECTOR_TYPE
742 #define DEF_VECTOR_TYPE(INDEX, INDEX_BASE, ELEMENTS) \
743 if (s390_builtin_types[INDEX] == NULL) \
744 s390_builtin_types[INDEX] = \
745 build_vector_type (s390_builtin_types[INDEX_BASE], ELEMENTS);
746
747 #undef DEF_OPAQUE_VECTOR_TYPE
748 #define DEF_OPAQUE_VECTOR_TYPE(INDEX, INDEX_BASE, ELEMENTS) \
749 if (s390_builtin_types[INDEX] == NULL) \
750 s390_builtin_types[INDEX] = \
751 build_opaque_vector_type (s390_builtin_types[INDEX_BASE], ELEMENTS);
752
753 #undef DEF_FN_TYPE
754 #define DEF_FN_TYPE(INDEX, args...) \
755 if (s390_builtin_fn_types[INDEX] == NULL) \
756 s390_builtin_fn_types[INDEX] = \
757 build_function_type_list (args, NULL_TREE);
758 #undef DEF_OV_TYPE
759 #define DEF_OV_TYPE(...)
760 #include "s390-builtin-types.def"
761
762 #undef B_DEF
763 #define B_DEF(NAME, PATTERN, ATTRS, BFLAGS, OPFLAGS, FNTYPE) \
764 if (s390_builtin_decls[S390_BUILTIN_##NAME] == NULL) \
765 s390_builtin_decls[S390_BUILTIN_##NAME] = \
766 add_builtin_function ("__builtin_" #NAME, \
767 s390_builtin_fn_types[FNTYPE], \
768 S390_BUILTIN_##NAME, \
769 BUILT_IN_MD, \
770 NULL, \
771 ATTRS);
772 #undef OB_DEF
773 #define OB_DEF(NAME, FIRST_VAR_NAME, LAST_VAR_NAME, BFLAGS, FNTYPE) \
774 if (s390_builtin_decls[S390_OVERLOADED_BUILTIN_##NAME + S390_BUILTIN_MAX] \
775 == NULL) \
776 s390_builtin_decls[S390_OVERLOADED_BUILTIN_##NAME + S390_BUILTIN_MAX] = \
777 add_builtin_function ("__builtin_" #NAME, \
778 s390_builtin_fn_types[FNTYPE], \
779 S390_OVERLOADED_BUILTIN_##NAME + S390_BUILTIN_MAX, \
780 BUILT_IN_MD, \
781 NULL, \
782 0);
783 #undef OB_DEF_VAR
784 #define OB_DEF_VAR(...)
785 #include "s390-builtins.def"
786
787 }
788
789 /* Return true if ARG is appropriate as argument number ARGNUM of
790 builtin DECL. The operand flags from s390-builtins.def have to
791 passed as OP_FLAGS. */
792 bool
793 s390_const_operand_ok (tree arg, int argnum, int op_flags, tree decl)
794 {
795 if (O_UIMM_P (op_flags))
796 {
797 unsigned HOST_WIDE_INT bitwidths[] = { 1, 2, 3, 4, 5, 8, 12, 16, 32, 4 };
798 unsigned HOST_WIDE_INT bitmasks[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 12 };
799 unsigned HOST_WIDE_INT bitwidth = bitwidths[op_flags - O_U1];
800 unsigned HOST_WIDE_INT bitmask = bitmasks[op_flags - O_U1];
801
802 gcc_assert(ARRAY_SIZE(bitwidths) == (O_M12 - O_U1 + 1));
803 gcc_assert(ARRAY_SIZE(bitmasks) == (O_M12 - O_U1 + 1));
804
805 if (!tree_fits_uhwi_p (arg)
806 || tree_to_uhwi (arg) > (HOST_WIDE_INT_1U << bitwidth) - 1
807 || (bitmask && tree_to_uhwi (arg) & ~bitmask))
808 {
809 if (bitmask)
810 {
811 gcc_assert (bitmask < 16);
812 char values[120] = "";
813
814 for (unsigned HOST_WIDE_INT i = 0; i <= bitmask; i++)
815 {
816 char buf[5];
817 if (i & ~bitmask)
818 continue;
819 int ret = snprintf (buf, 5, HOST_WIDE_INT_PRINT_UNSIGNED, i & bitmask);
820 gcc_assert (ret < 5);
821 strcat (values, buf);
822 if (i < bitmask)
823 strcat (values, ", ");
824 }
825 error ("constant argument %d for builtin %qF is invalid (%s)",
826 argnum, decl, values);
827 }
828 else
829 error ("constant argument %d for builtin %qF is out of range (0-%wu)",
830 argnum, decl, (HOST_WIDE_INT_1U << bitwidth) - 1);
831
832 return false;
833 }
834 }
835
836 if (O_SIMM_P (op_flags))
837 {
838 int bitwidths[] = { 2, 3, 4, 5, 8, 12, 16, 32 };
839 int bitwidth = bitwidths[op_flags - O_S2];
840
841 if (!tree_fits_shwi_p (arg)
842 || tree_to_shwi (arg) < -(HOST_WIDE_INT_1 << (bitwidth - 1))
843 || tree_to_shwi (arg) > ((HOST_WIDE_INT_1 << (bitwidth - 1)) - 1))
844 {
845 error ("constant argument %d for builtin %qF is out of range "
846 "(%wd-%wd)", argnum, decl,
847 -(HOST_WIDE_INT_1 << (bitwidth - 1)),
848 (HOST_WIDE_INT_1 << (bitwidth - 1)) - 1);
849 return false;
850 }
851 }
852 return true;
853 }
854
855 /* Expand an expression EXP that calls a built-in function,
856 with result going to TARGET if that's convenient
857 (and in mode MODE if that's convenient).
858 SUBTARGET may be used as the target for computing one of EXP's operands.
859 IGNORE is nonzero if the value is to be ignored. */
860
861 static rtx
862 s390_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
863 machine_mode mode ATTRIBUTE_UNUSED,
864 int ignore ATTRIBUTE_UNUSED)
865 {
866 #define MAX_ARGS 6
867
868 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
869 unsigned int fcode = DECL_MD_FUNCTION_CODE (fndecl);
870 enum insn_code icode;
871 rtx op[MAX_ARGS], pat;
872 int arity;
873 bool nonvoid;
874 tree arg;
875 call_expr_arg_iterator iter;
876 unsigned int all_op_flags = opflags_for_builtin (fcode);
877 machine_mode last_vec_mode = VOIDmode;
878
879 if (TARGET_DEBUG_ARG)
880 {
881 fprintf (stderr,
882 "s390_expand_builtin, code = %4d, %s, bflags = 0x%x\n",
883 (int)fcode, IDENTIFIER_POINTER (DECL_NAME (fndecl)),
884 bflags_for_builtin (fcode));
885 }
886
887 if (S390_USE_TARGET_ATTRIBUTE)
888 {
889 unsigned int bflags;
890
891 bflags = bflags_for_builtin (fcode);
892 if ((bflags & B_HTM) && !TARGET_HTM)
893 {
894 error ("builtin %qF is not supported without %<-mhtm%> "
895 "(default with %<-march=zEC12%> and higher)", fndecl);
896 return const0_rtx;
897 }
898 if (((bflags & B_VX) || (bflags & B_VXE)) && !TARGET_VX)
899 {
900 error ("builtin %qF requires %<-mvx%> "
901 "(default with %<-march=z13%> and higher)", fndecl);
902 return const0_rtx;
903 }
904
905 if ((bflags & B_VXE) && !TARGET_VXE)
906 {
907 error ("Builtin %qF requires z14 or higher", fndecl);
908 return const0_rtx;
909 }
910
911 if ((bflags & B_VXE2) && !TARGET_VXE2)
912 {
913 error ("Builtin %qF requires z15 or higher", fndecl);
914 return const0_rtx;
915 }
916
917 if ((bflags & B_VXE3) && !TARGET_VXE3)
918 {
919 error ("Builtin %qF requires z17 or higher", fndecl);
920 return const0_rtx;
921 }
922 }
923 if (fcode >= S390_OVERLOADED_BUILTIN_VAR_OFFSET
924 && fcode < S390_ALL_BUILTIN_MAX)
925 {
926 gcc_unreachable ();
927 }
928 else if (fcode < S390_OVERLOADED_BUILTIN_OFFSET)
929 {
930 icode = code_for_builtin[fcode];
931 /* Set a flag in the machine specific cfun part in order to support
932 saving/restoring of FPRs. */
933 if (fcode == S390_BUILTIN_tbegin || fcode == S390_BUILTIN_tbegin_retry)
934 cfun->machine->tbegin_p = true;
935 }
936 else if (fcode < S390_OVERLOADED_BUILTIN_VAR_OFFSET)
937 {
938 error ("unresolved overloaded builtin");
939 return const0_rtx;
940 }
941 else
942 internal_error ("bad builtin fcode");
943
944 if (icode == 0)
945 internal_error ("bad builtin icode");
946
947 nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
948
949 if (nonvoid)
950 {
951 machine_mode tmode = insn_data[icode].operand[0].mode;
952 if (!target
953 || GET_MODE (target) != tmode
954 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
955 target = gen_reg_rtx (tmode);
956
957 /* There are builtins (e.g. vec_promote) with no vector
958 arguments but an element selector. So we have to also look
959 at the vector return type when emitting the modulo
960 operation. */
961 if (VECTOR_MODE_P (insn_data[icode].operand[0].mode))
962 last_vec_mode = insn_data[icode].operand[0].mode;
963 }
964
965 arity = 0;
966 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
967 {
968 rtx tmp_rtx;
969 const struct insn_operand_data *insn_op;
970 unsigned int op_flags = all_op_flags & ((1 << O_SHIFT) - 1);
971
972 all_op_flags = all_op_flags >> O_SHIFT;
973
974 if (arg == error_mark_node)
975 return NULL_RTX;
976 if (arity >= MAX_ARGS)
977 return NULL_RTX;
978
979 if (O_IMM_P (op_flags)
980 && TREE_CODE (arg) != INTEGER_CST)
981 {
982 error ("constant value required for builtin %qF argument %d",
983 fndecl, arity + 1);
984 return const0_rtx;
985 }
986
987 if (!s390_const_operand_ok (arg, arity + 1, op_flags, fndecl))
988 return const0_rtx;
989
990 insn_op = &insn_data[icode].operand[arity + nonvoid];
991 op[arity] = expand_expr (arg, NULL_RTX, insn_op->mode, EXPAND_NORMAL);
992
993 /* expand_expr truncates constants to the target mode only if it
994 is "convenient". However, our checks below rely on this
995 being done. */
996 if (CONST_INT_P (op[arity])
997 && SCALAR_INT_MODE_P (insn_op->mode)
998 && GET_MODE (op[arity]) != insn_op->mode)
999 op[arity] = GEN_INT (trunc_int_for_mode (INTVAL (op[arity]),
1000 insn_op->mode));
1001
1002 /* Wrap the expanded RTX for pointer types into a MEM expr with
1003 the proper mode. This allows us to use e.g. (match_operand
1004 "memory_operand"..) in the insn patterns instead of (mem
1005 (match_operand "address_operand)). This is helpful for
1006 patterns not just accepting MEMs. */
1007 if (POINTER_TYPE_P (TREE_TYPE (arg))
1008 && insn_op->predicate != address_operand)
1009 op[arity] = gen_rtx_MEM (insn_op->mode, op[arity]);
1010
1011 /* Expand the module operation required on element selectors. */
1012 if (op_flags == O_ELEM)
1013 {
1014 gcc_assert (last_vec_mode != VOIDmode);
1015 op[arity] = simplify_expand_binop (SImode, code_to_optab (AND),
1016 op[arity],
1017 GEN_INT (GET_MODE_NUNITS (last_vec_mode) - 1),
1018 NULL_RTX, 1, OPTAB_DIRECT);
1019 }
1020
1021 /* Record the vector mode used for an element selector. This assumes:
1022 1. There is no builtin with two different vector modes and an element selector
1023 2. The element selector comes after the vector type it is referring to.
1024 This currently the true for all the builtins but FIXME we
1025 should better check for that. */
1026 if (VECTOR_MODE_P (insn_op->mode))
1027 last_vec_mode = insn_op->mode;
1028
1029 if (insn_op->predicate (op[arity], insn_op->mode))
1030 {
1031 arity++;
1032 continue;
1033 }
1034
1035 /* A memory operand is rejected by the memory_operand predicate.
1036 Try making the address legal by copying it into a register. */
1037 if (MEM_P (op[arity])
1038 && insn_op->predicate == memory_operand
1039 && (GET_MODE (XEXP (op[arity], 0)) == Pmode
1040 || GET_MODE (XEXP (op[arity], 0)) == VOIDmode))
1041 {
1042 op[arity] = replace_equiv_address (op[arity],
1043 copy_to_mode_reg (Pmode,
1044 XEXP (op[arity], 0)));
1045 }
1046 /* Some of the builtins require different modes/types than the
1047 pattern in order to implement a specific API. Instead of
1048 adding many expanders which do the mode change we do it here.
1049 E.g. s390_vec_add_u128 required to have vector unsigned char
1050 arguments is mapped to addti3. */
1051 else if (insn_op->mode != VOIDmode
1052 && GET_MODE (op[arity]) != VOIDmode
1053 && GET_MODE (op[arity]) != insn_op->mode
1054 && ((tmp_rtx = simplify_gen_subreg (insn_op->mode, op[arity],
1055 GET_MODE (op[arity]), 0))
1056 != NULL_RTX))
1057 {
1058 op[arity] = tmp_rtx;
1059 }
1060
1061 /* The predicate rejects the operand although the mode is fine.
1062 Copy the operand to register. */
1063 if (!insn_op->predicate (op[arity], insn_op->mode)
1064 && (GET_MODE (op[arity]) == insn_op->mode
1065 || GET_MODE (op[arity]) == VOIDmode
1066 || (insn_op->predicate == address_operand
1067 && GET_MODE (op[arity]) == Pmode)))
1068 {
1069 /* An address_operand usually has VOIDmode in the expander
1070 so we cannot use this. */
1071 machine_mode target_mode =
1072 (insn_op->predicate == address_operand
1073 ? (machine_mode) Pmode : insn_op->mode);
1074 op[arity] = copy_to_mode_reg (target_mode, op[arity]);
1075 }
1076
1077 if (!insn_op->predicate (op[arity], insn_op->mode))
1078 {
1079 error ("invalid argument %d for builtin %qF", arity + 1, fndecl);
1080 return const0_rtx;
1081 }
1082 arity++;
1083 }
1084
1085 switch (arity)
1086 {
1087 case 0:
1088 pat = GEN_FCN (icode) (target);
1089 break;
1090 case 1:
1091 if (nonvoid)
1092 pat = GEN_FCN (icode) (target, op[0]);
1093 else
1094 pat = GEN_FCN (icode) (op[0]);
1095 break;
1096 case 2:
1097 if (nonvoid)
1098 pat = GEN_FCN (icode) (target, op[0], op[1]);
1099 else
1100 pat = GEN_FCN (icode) (op[0], op[1]);
1101 break;
1102 case 3:
1103 if (nonvoid)
1104 pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
1105 else
1106 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
1107 break;
1108 case 4:
1109 if (nonvoid)
1110 pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
1111 else
1112 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
1113 break;
1114 case 5:
1115 if (nonvoid)
1116 pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
1117 else
1118 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
1119 break;
1120 case 6:
1121 if (nonvoid)
1122 pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4], op[5]);
1123 else
1124 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4], op[5]);
1125 break;
1126 default:
1127 gcc_unreachable ();
1128 }
1129 if (!pat)
1130 return NULL_RTX;
1131 emit_insn (pat);
1132
1133 if (nonvoid)
1134 return target;
1135 else
1136 return const0_rtx;
1137 }
1138
1139
1140 static const int s390_hotpatch_hw_max = 1000000;
1141 static int s390_hotpatch_hw_before_label = 0;
1142 static int s390_hotpatch_hw_after_label = 0;
1143
1144 /* Check whether the hotpatch attribute is applied to a function and, if it has
1145 an argument, the argument is valid. */
1146
1147 static tree
1148 s390_handle_hotpatch_attribute (tree *node, tree name, tree args,
1149 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1150 {
1151 tree expr;
1152 tree expr2;
1153 int err;
1154
1155 if (TREE_CODE (*node) != FUNCTION_DECL)
1156 {
1157 warning (OPT_Wattributes, "%qE attribute only applies to functions",
1158 name);
1159 *no_add_attrs = true;
1160 }
1161 if (args != NULL && TREE_CHAIN (args) != NULL)
1162 {
1163 expr = TREE_VALUE (args);
1164 expr2 = TREE_VALUE (TREE_CHAIN (args));
1165 }
1166 if (args == NULL || TREE_CHAIN (args) == NULL)
1167 err = 1;
1168 else if (TREE_CODE (expr) != INTEGER_CST
1169 || !INTEGRAL_TYPE_P (TREE_TYPE (expr))
1170 || wi::gtu_p (wi::to_wide (expr), s390_hotpatch_hw_max))
1171 err = 1;
1172 else if (TREE_CODE (expr2) != INTEGER_CST
1173 || !INTEGRAL_TYPE_P (TREE_TYPE (expr2))
1174 || wi::gtu_p (wi::to_wide (expr2), s390_hotpatch_hw_max))
1175 err = 1;
1176 else
1177 err = 0;
1178 if (err)
1179 {
1180 error ("requested %qE attribute is not a comma separated pair of"
1181 " non-negative integer constants or too large (max. %d)", name,
1182 s390_hotpatch_hw_max);
1183 *no_add_attrs = true;
1184 }
1185
1186 return NULL_TREE;
1187 }
1188
1189 /* Expand the s390_vector_bool type attribute. */
1190
1191 static tree
1192 s390_handle_vectorbool_attribute (tree *node, tree name ATTRIBUTE_UNUSED,
1193 tree args ATTRIBUTE_UNUSED,
1194 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1195 {
1196 tree type = *node, result = NULL_TREE;
1197 machine_mode mode;
1198
1199 while (POINTER_TYPE_P (type)
1200 || TREE_CODE (type) == FUNCTION_TYPE
1201 || TREE_CODE (type) == METHOD_TYPE
1202 || TREE_CODE (type) == ARRAY_TYPE)
1203 type = TREE_TYPE (type);
1204
1205 mode = TYPE_MODE (type);
1206 switch (mode)
1207 {
1208 case E_TImode: case E_V1TImode:
1209 result = s390_builtin_types[BT_BV1TI];
1210 break;
1211 case E_DImode: case E_V2DImode:
1212 result = s390_builtin_types[BT_BV2DI];
1213 break;
1214 case E_SImode: case E_V4SImode:
1215 result = s390_builtin_types[BT_BV4SI];
1216 break;
1217 case E_HImode: case E_V8HImode:
1218 result = s390_builtin_types[BT_BV8HI];
1219 break;
1220 case E_QImode: case E_V16QImode:
1221 result = s390_builtin_types[BT_BV16QI];
1222 break;
1223 default:
1224 break;
1225 }
1226
1227 *no_add_attrs = true; /* No need to hang on to the attribute. */
1228
1229 if (result)
1230 *node = lang_hooks.types.reconstruct_complex_type (*node, result);
1231
1232 return NULL_TREE;
1233 }
1234
1235 /* Check syntax of function decl attributes having a string type value. */
1236
1237 static tree
1238 s390_handle_string_attribute (tree *node, tree name ATTRIBUTE_UNUSED,
1239 tree args ATTRIBUTE_UNUSED,
1240 int flags ATTRIBUTE_UNUSED,
1241 bool *no_add_attrs)
1242 {
1243 tree cst;
1244
1245 if (TREE_CODE (*node) != FUNCTION_DECL)
1246 {
1247 warning (OPT_Wattributes, "%qE attribute only applies to functions",
1248 name);
1249 *no_add_attrs = true;
1250 }
1251
1252 cst = TREE_VALUE (args);
1253
1254 if (TREE_CODE (cst) != STRING_CST)
1255 {
1256 warning (OPT_Wattributes,
1257 "%qE attribute requires a string constant argument",
1258 name);
1259 *no_add_attrs = true;
1260 }
1261
1262 if (is_attribute_p ("indirect_branch", name)
1263 || is_attribute_p ("indirect_branch_call", name)
1264 || is_attribute_p ("function_return", name)
1265 || is_attribute_p ("function_return_reg", name)
1266 || is_attribute_p ("function_return_mem", name))
1267 {
1268 if (strcmp (TREE_STRING_POINTER (cst), "keep") != 0
1269 && strcmp (TREE_STRING_POINTER (cst), "thunk") != 0
1270 && strcmp (TREE_STRING_POINTER (cst), "thunk-extern") != 0)
1271 {
1272 warning (OPT_Wattributes,
1273 "argument to %qE attribute is not "
1274 "(keep|thunk|thunk-extern)", name);
1275 *no_add_attrs = true;
1276 }
1277 }
1278
1279 if (is_attribute_p ("indirect_branch_jump", name)
1280 && strcmp (TREE_STRING_POINTER (cst), "keep") != 0
1281 && strcmp (TREE_STRING_POINTER (cst), "thunk") != 0
1282 && strcmp (TREE_STRING_POINTER (cst), "thunk-inline") != 0
1283 && strcmp (TREE_STRING_POINTER (cst), "thunk-extern") != 0)
1284 {
1285 warning (OPT_Wattributes,
1286 "argument to %qE attribute is not "
1287 "(keep|thunk|thunk-inline|thunk-extern)", name);
1288 *no_add_attrs = true;
1289 }
1290
1291 return NULL_TREE;
1292 }
1293
1294 TARGET_GNU_ATTRIBUTES (s390_attribute_table, {
1295 { "hotpatch", 2, 2, true, false, false, false,
1296 s390_handle_hotpatch_attribute, NULL },
1297 { "s390_vector_bool", 0, 0, false, true, false, true,
1298 s390_handle_vectorbool_attribute, NULL },
1299 { "indirect_branch", 1, 1, true, false, false, false,
1300 s390_handle_string_attribute, NULL },
1301 { "indirect_branch_jump", 1, 1, true, false, false, false,
1302 s390_handle_string_attribute, NULL },
1303 { "indirect_branch_call", 1, 1, true, false, false, false,
1304 s390_handle_string_attribute, NULL },
1305 { "function_return", 1, 1, true, false, false, false,
1306 s390_handle_string_attribute, NULL },
1307 { "function_return_reg", 1, 1, true, false, false, false,
1308 s390_handle_string_attribute, NULL },
1309 { "function_return_mem", 1, 1, true, false, false, false,
1310 s390_handle_string_attribute, NULL }
1311 });
1312
1313 /* Return the alignment for LABEL. We default to the -falign-labels
1314 value except for the literal pool base label. */
1315 int
1316 s390_label_align (rtx_insn *label)
1317 {
1318 rtx_insn *prev_insn = prev_active_insn (label);
1319 rtx set, src;
1320
1321 if (prev_insn == NULL_RTX)
1322 goto old;
1323
1324 set = single_set (prev_insn);
1325
1326 if (set == NULL_RTX)
1327 goto old;
1328
1329 src = SET_SRC (set);
1330
1331 /* Don't align literal pool base labels. */
1332 if (GET_CODE (src) == UNSPEC
1333 && XINT (src, 1) == UNSPEC_MAIN_BASE)
1334 return 0;
1335
1336 old:
1337 return align_labels.levels[0].log;
1338 }
1339
1340 static GTY(()) rtx got_symbol;
1341
1342 /* Return the GOT table symbol. The symbol will be created when the
1343 function is invoked for the first time. */
1344
1345 static rtx
1346 s390_got_symbol (void)
1347 {
1348 if (!got_symbol)
1349 {
1350 got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
1351 SYMBOL_REF_FLAGS (got_symbol) = SYMBOL_FLAG_LOCAL;
1352 }
1353
1354 return got_symbol;
1355 }
1356
1357 static scalar_int_mode
1358 s390_libgcc_cmp_return_mode (void)
1359 {
1360 return TARGET_64BIT ? DImode : SImode;
1361 }
1362
1363 static scalar_int_mode
1364 s390_libgcc_shift_count_mode (void)
1365 {
1366 return TARGET_64BIT ? DImode : SImode;
1367 }
1368
1369 static scalar_int_mode
1370 s390_unwind_word_mode (void)
1371 {
1372 return TARGET_64BIT ? DImode : SImode;
1373 }
1374
1375 /* Return true if the back end supports mode MODE. */
1376 static bool
1377 s390_scalar_mode_supported_p (scalar_mode mode)
1378 {
1379 /* In contrast to the default implementation reject TImode constants on 31bit
1380 TARGET_ZARCH for ABI compliance. */
1381 if (!TARGET_64BIT && TARGET_ZARCH && mode == TImode)
1382 return false;
1383
1384 if (DECIMAL_FLOAT_MODE_P (mode))
1385 return default_decimal_float_supported_p ();
1386
1387 return default_scalar_mode_supported_p (mode);
1388 }
1389
1390 /* Return true if the back end supports vector mode MODE. */
1391 static bool
1392 s390_vector_mode_supported_p (machine_mode mode)
1393 {
1394 machine_mode inner;
1395
1396 if (!VECTOR_MODE_P (mode)
1397 || !TARGET_VX
1398 || GET_MODE_SIZE (mode) > 16)
1399 return false;
1400
1401 inner = GET_MODE_INNER (mode);
1402
1403 switch (inner)
1404 {
1405 case E_QImode:
1406 case E_HImode:
1407 case E_SImode:
1408 case E_DImode:
1409 case E_TImode:
1410 case E_SFmode:
1411 case E_DFmode:
1412 case E_TFmode:
1413 return true;
1414 default:
1415 return false;
1416 }
1417 }
1418
1419 /* Set the has_landing_pad_p flag in struct machine_function to VALUE. */
1420
1421 void
1422 s390_set_has_landing_pad_p (bool value)
1423 {
1424 cfun->machine->has_landing_pad_p = value;
1425 }
1426
1427 /* If two condition code modes are compatible, return a condition code
1428 mode which is compatible with both. Otherwise, return
1429 VOIDmode. */
1430
1431 static machine_mode
1432 s390_cc_modes_compatible (machine_mode m1, machine_mode m2)
1433 {
1434 if (m1 == m2)
1435 return m1;
1436
1437 switch (m1)
1438 {
1439 case E_CCZmode:
1440 if (m2 == CCUmode || m2 == CCTmode || m2 == CCZ1mode
1441 || m2 == CCSmode || m2 == CCSRmode || m2 == CCURmode)
1442 return m2;
1443 return VOIDmode;
1444
1445 case E_CCSmode:
1446 case E_CCUmode:
1447 case E_CCTmode:
1448 case E_CCSRmode:
1449 case E_CCURmode:
1450 case E_CCZ1mode:
1451 if (m2 == CCZmode)
1452 return m1;
1453
1454 return VOIDmode;
1455
1456 default:
1457 return VOIDmode;
1458 }
1459 return VOIDmode;
1460 }
1461
1462 /* Return true if SET either doesn't set the CC register, or else
1463 the source and destination have matching CC modes and that
1464 CC mode is at least as constrained as REQ_MODE. */
1465
1466 static bool
1467 s390_match_ccmode_set (rtx set, machine_mode req_mode)
1468 {
1469 machine_mode set_mode;
1470
1471 gcc_assert (GET_CODE (set) == SET);
1472
1473 /* These modes are supposed to be used only in CC consumer
1474 patterns. */
1475 gcc_assert (req_mode != CCVIALLmode && req_mode != CCVIANYmode
1476 && req_mode != CCVFALLmode && req_mode != CCVFANYmode);
1477
1478 if (GET_CODE (SET_DEST (set)) != REG || !CC_REGNO_P (REGNO (SET_DEST (set))))
1479 return 1;
1480
1481 set_mode = GET_MODE (SET_DEST (set));
1482 switch (set_mode)
1483 {
1484 case E_CCZ1mode:
1485 case E_CCSmode:
1486 case E_CCSRmode:
1487 case E_CCSFPSmode:
1488 case E_CCUmode:
1489 case E_CCURmode:
1490 case E_CCOmode:
1491 case E_CCLmode:
1492 case E_CCL1mode:
1493 case E_CCL2mode:
1494 case E_CCL3mode:
1495 case E_CCT1mode:
1496 case E_CCT2mode:
1497 case E_CCT3mode:
1498 case E_CCVEQmode:
1499 case E_CCVIHmode:
1500 case E_CCVIHUmode:
1501 case E_CCVFHmode:
1502 case E_CCVFHEmode:
1503 if (req_mode != set_mode)
1504 return 0;
1505 break;
1506
1507 case E_CCZmode:
1508 if (req_mode != CCSmode && req_mode != CCUmode && req_mode != CCTmode
1509 && req_mode != CCSRmode && req_mode != CCURmode
1510 && req_mode != CCZ1mode)
1511 return 0;
1512 break;
1513
1514 case E_CCAPmode:
1515 case E_CCANmode:
1516 if (req_mode != CCAmode)
1517 return 0;
1518 break;
1519
1520 default:
1521 gcc_unreachable ();
1522 }
1523
1524 return (GET_MODE (SET_SRC (set)) == set_mode);
1525 }
1526
1527 /* Return true if every SET in INSN that sets the CC register
1528 has source and destination with matching CC modes and that
1529 CC mode is at least as constrained as REQ_MODE.
1530 If REQ_MODE is VOIDmode, always return false. */
1531
1532 bool
1533 s390_match_ccmode (rtx_insn *insn, machine_mode req_mode)
1534 {
1535 int i;
1536
1537 /* s390_tm_ccmode returns VOIDmode to indicate failure. */
1538 if (req_mode == VOIDmode)
1539 return false;
1540
1541 if (GET_CODE (PATTERN (insn)) == SET)
1542 return s390_match_ccmode_set (PATTERN (insn), req_mode);
1543
1544 if (GET_CODE (PATTERN (insn)) == PARALLEL)
1545 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1546 {
1547 rtx set = XVECEXP (PATTERN (insn), 0, i);
1548 if (GET_CODE (set) == SET)
1549 if (!s390_match_ccmode_set (set, req_mode))
1550 return false;
1551 }
1552
1553 return true;
1554 }
1555
1556 /* If a test-under-mask instruction can be used to implement
1557 (compare (and ... OP1) OP2), return the CC mode required
1558 to do that. Otherwise, return VOIDmode.
1559 MIXED is true if the instruction can distinguish between
1560 CC1 and CC2 for mixed selected bits (TMxx), it is false
1561 if the instruction cannot (TM). */
1562
1563 machine_mode
1564 s390_tm_ccmode (rtx op1, rtx op2, bool mixed)
1565 {
1566 int bit0, bit1;
1567
1568 /* ??? Fixme: should work on CONST_WIDE_INT as well. */
1569 if (GET_CODE (op1) != CONST_INT || GET_CODE (op2) != CONST_INT)
1570 return VOIDmode;
1571
1572 /* Selected bits all zero: CC0.
1573 e.g.: int a; if ((a & (16 + 128)) == 0) */
1574 if (INTVAL (op2) == 0)
1575 return CCTmode;
1576
1577 /* Selected bits all one: CC3.
1578 e.g.: int a; if ((a & (16 + 128)) == 16 + 128) */
1579 if (INTVAL (op2) == INTVAL (op1))
1580 return CCT3mode;
1581
1582 /* Exactly two bits selected, mixed zeroes and ones: CC1 or CC2. e.g.:
1583 int a;
1584 if ((a & (16 + 128)) == 16) -> CCT1
1585 if ((a & (16 + 128)) == 128) -> CCT2 */
1586 if (mixed)
1587 {
1588 bit1 = exact_log2 (INTVAL (op2));
1589 bit0 = exact_log2 (INTVAL (op1) ^ INTVAL (op2));
1590 if (bit0 != -1 && bit1 != -1)
1591 return bit0 > bit1 ? CCT1mode : CCT2mode;
1592 }
1593
1594 return VOIDmode;
1595 }
1596
1597 /* Given a comparison code OP (EQ, NE, etc.) and the operands
1598 OP0 and OP1 of a COMPARE, return the mode to be used for the
1599 comparison. */
1600
1601 machine_mode
1602 s390_select_ccmode (enum rtx_code code, rtx op0, rtx op1)
1603 {
1604 switch (code)
1605 {
1606 case EQ:
1607 case NE:
1608 if ((GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
1609 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
1610 return CCAPmode;
1611 if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
1612 && CONST_OK_FOR_K (INTVAL (XEXP (op0, 1))))
1613 return CCAPmode;
1614 if ((GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
1615 || GET_CODE (op1) == NEG)
1616 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
1617 return CCLmode;
1618
1619 if (GET_CODE (op0) == AND)
1620 {
1621 /* Check whether we can potentially do it via TM. */
1622 machine_mode ccmode;
1623 ccmode = s390_tm_ccmode (XEXP (op0, 1), op1, 1);
1624 if (ccmode != VOIDmode)
1625 {
1626 /* Relax CCTmode to CCZmode to allow fall-back to AND
1627 if that turns out to be beneficial. */
1628 return ccmode == CCTmode ? CCZmode : ccmode;
1629 }
1630 }
1631
1632 if (register_operand (op0, HImode)
1633 && GET_CODE (op1) == CONST_INT
1634 && (INTVAL (op1) == -1 || INTVAL (op1) == 65535))
1635 return CCT3mode;
1636 if (register_operand (op0, QImode)
1637 && GET_CODE (op1) == CONST_INT
1638 && (INTVAL (op1) == -1 || INTVAL (op1) == 255))
1639 return CCT3mode;
1640
1641 return CCZmode;
1642
1643 case LE:
1644 case LT:
1645 case GE:
1646 case GT:
1647 /* The only overflow condition of NEG and ABS happens when
1648 -INT_MAX is used as parameter, which stays negative. So
1649 we have an overflow from a positive value to a negative.
1650 Using CCAP mode the resulting cc can be used for comparisons. */
1651 if ((GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
1652 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
1653 return CCAPmode;
1654
1655 /* If constants are involved in an add instruction it is possible to use
1656 the resulting cc for comparisons with zero. Knowing the sign of the
1657 constant the overflow behavior gets predictable. e.g.:
1658 int a, b; if ((b = a + c) > 0)
1659 with c as a constant value: c < 0 -> CCAN and c >= 0 -> CCAP */
1660 if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
1661 && (CONST_OK_FOR_K (INTVAL (XEXP (op0, 1)))
1662 || (CONST_OK_FOR_CONSTRAINT_P (INTVAL (XEXP (op0, 1)), 'O', "Os")
1663 /* Avoid INT32_MIN on 32 bit. */
1664 && (!TARGET_ZARCH || INTVAL (XEXP (op0, 1)) != -0x7fffffff - 1))))
1665 {
1666 if (INTVAL (XEXP((op0), 1)) < 0)
1667 return CCANmode;
1668 else
1669 return CCAPmode;
1670 }
1671
1672 /* Fall through. */
1673 case LTGT:
1674 if (HONOR_NANS (op0) || HONOR_NANS (op1))
1675 return CCSFPSmode;
1676
1677 /* Fall through. */
1678 case UNORDERED:
1679 case ORDERED:
1680 case UNEQ:
1681 case UNLE:
1682 case UNLT:
1683 case UNGE:
1684 case UNGT:
1685 if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
1686 && GET_CODE (op1) != CONST_INT)
1687 return CCSRmode;
1688 return CCSmode;
1689
1690 case LTU:
1691 case GEU:
1692 if (GET_CODE (op0) == PLUS
1693 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
1694 return CCL1mode;
1695
1696 if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
1697 && GET_CODE (op1) != CONST_INT)
1698 return CCURmode;
1699 return CCUmode;
1700
1701 case LEU:
1702 case GTU:
1703 if (GET_CODE (op0) == MINUS
1704 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
1705 return CCL2mode;
1706
1707 if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
1708 && GET_CODE (op1) != CONST_INT)
1709 return CCURmode;
1710 return CCUmode;
1711
1712 default:
1713 gcc_unreachable ();
1714 }
1715 }
1716
1717 /* Replace the comparison OP0 CODE OP1 by a semantically equivalent one
1718 that we can implement more efficiently. */
1719
1720 static void
1721 s390_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
1722 bool op0_preserve_value)
1723 {
1724 if (op0_preserve_value)
1725 return;
1726
1727 /* Convert ZERO_EXTRACT back to AND to enable TM patterns. */
1728 if ((*code == EQ || *code == NE)
1729 && *op1 == const0_rtx
1730 && GET_CODE (*op0) == ZERO_EXTRACT
1731 && GET_CODE (XEXP (*op0, 1)) == CONST_INT
1732 && GET_CODE (XEXP (*op0, 2)) == CONST_INT
1733 && SCALAR_INT_MODE_P (GET_MODE (XEXP (*op0, 0))))
1734 {
1735 rtx inner = XEXP (*op0, 0);
1736 HOST_WIDE_INT modesize = GET_MODE_BITSIZE (GET_MODE (inner));
1737 HOST_WIDE_INT len = INTVAL (XEXP (*op0, 1));
1738 HOST_WIDE_INT pos = INTVAL (XEXP (*op0, 2));
1739
1740 if (len > 0 && len < modesize
1741 && pos >= 0 && pos + len <= modesize
1742 && modesize <= HOST_BITS_PER_WIDE_INT)
1743 {
1744 unsigned HOST_WIDE_INT block;
1745 block = (HOST_WIDE_INT_1U << len) - 1;
1746 block <<= modesize - pos - len;
1747
1748 *op0 = gen_rtx_AND (GET_MODE (inner), inner,
1749 gen_int_mode (block, GET_MODE (inner)));
1750 }
1751 }
1752
1753 /* Narrow AND of memory against immediate to enable TM. */
1754 if ((*code == EQ || *code == NE)
1755 && *op1 == const0_rtx
1756 && GET_CODE (*op0) == AND
1757 && GET_CODE (XEXP (*op0, 1)) == CONST_INT
1758 && SCALAR_INT_MODE_P (GET_MODE (XEXP (*op0, 0))))
1759 {
1760 rtx inner = XEXP (*op0, 0);
1761 rtx mask = XEXP (*op0, 1);
1762
1763 /* Ignore paradoxical SUBREGs if all extra bits are masked out. */
1764 if (GET_CODE (inner) == SUBREG
1765 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (inner)))
1766 && (GET_MODE_SIZE (GET_MODE (inner))
1767 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner))))
1768 && ((INTVAL (mask)
1769 & GET_MODE_MASK (GET_MODE (inner))
1770 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (inner))))
1771 == 0))
1772 inner = SUBREG_REG (inner);
1773
1774 /* Do not change volatile MEMs. */
1775 if (MEM_P (inner) && !MEM_VOLATILE_P (inner))
1776 {
1777 int part = s390_single_part (XEXP (*op0, 1),
1778 GET_MODE (inner), QImode, 0);
1779 if (part >= 0)
1780 {
1781 mask = gen_int_mode (s390_extract_part (mask, QImode, 0), QImode);
1782 inner = adjust_address_nv (inner, QImode, part);
1783 *op0 = gen_rtx_AND (QImode, inner, mask);
1784 }
1785 }
1786 }
1787
1788 /* Narrow comparisons against 0xffff to HImode if possible. */
1789 if ((*code == EQ || *code == NE)
1790 && GET_CODE (*op1) == CONST_INT
1791 && INTVAL (*op1) == 0xffff
1792 && SCALAR_INT_MODE_P (GET_MODE (*op0))
1793 && (nonzero_bits (*op0, GET_MODE (*op0))
1794 & ~HOST_WIDE_INT_UC (0xffff)) == 0)
1795 {
1796 *op0 = gen_lowpart (HImode, *op0);
1797 *op1 = constm1_rtx;
1798 }
1799
1800 /* Remove redundant UNSPEC_STRCMPCC_TO_INT conversions if possible. */
1801 if (GET_CODE (*op0) == UNSPEC
1802 && XINT (*op0, 1) == UNSPEC_STRCMPCC_TO_INT
1803 && XVECLEN (*op0, 0) == 1
1804 && GET_MODE (XVECEXP (*op0, 0, 0)) == CCUmode
1805 && GET_CODE (XVECEXP (*op0, 0, 0)) == REG
1806 && REGNO (XVECEXP (*op0, 0, 0)) == CC_REGNUM
1807 && *op1 == const0_rtx)
1808 {
1809 enum rtx_code new_code = UNKNOWN;
1810 switch (*code)
1811 {
1812 case EQ: new_code = EQ; break;
1813 case NE: new_code = NE; break;
1814 case LT: new_code = GTU; break;
1815 case GT: new_code = LTU; break;
1816 case LE: new_code = GEU; break;
1817 case GE: new_code = LEU; break;
1818 default: break;
1819 }
1820
1821 if (new_code != UNKNOWN)
1822 {
1823 *op0 = XVECEXP (*op0, 0, 0);
1824 *code = new_code;
1825 }
1826 }
1827
1828 /* Remove redundant UNSPEC_CC_TO_INT conversions if possible. */
1829 if (GET_CODE (*op0) == UNSPEC
1830 && XINT (*op0, 1) == UNSPEC_CC_TO_INT
1831 && XVECLEN (*op0, 0) == 1
1832 && GET_CODE (XVECEXP (*op0, 0, 0)) == REG
1833 && REGNO (XVECEXP (*op0, 0, 0)) == CC_REGNUM
1834 && CONST_INT_P (*op1))
1835 {
1836 enum rtx_code new_code = UNKNOWN;
1837 switch (GET_MODE (XVECEXP (*op0, 0, 0)))
1838 {
1839 case E_CCZmode:
1840 case E_CCRAWmode:
1841 switch (*code)
1842 {
1843 case EQ: new_code = EQ; break;
1844 case NE: new_code = NE; break;
1845 default: break;
1846 }
1847 break;
1848 default: break;
1849 }
1850
1851 if (new_code != UNKNOWN)
1852 {
1853 /* For CCRAWmode put the required cc mask into the second
1854 operand. */
1855 if (GET_MODE (XVECEXP (*op0, 0, 0)) == CCRAWmode
1856 && INTVAL (*op1) >= 0 && INTVAL (*op1) <= 3)
1857 *op1 = gen_rtx_CONST_INT (VOIDmode, 1 << (3 - INTVAL (*op1)));
1858 *op0 = XVECEXP (*op0, 0, 0);
1859 *code = new_code;
1860 }
1861 }
1862 /* Remove UNSPEC_CC_TO_INT from connectives. This happens for
1863 checks against multiple condition codes. */
1864 if (GET_CODE (*op0) == AND
1865 && GET_CODE (XEXP (*op0, 0)) == UNSPEC
1866 && XINT (XEXP (*op0, 0), 1) == UNSPEC_CC_TO_INT
1867 && XVECLEN (XEXP (*op0, 0), 0) == 1
1868 && REGNO (XVECEXP (XEXP (*op0, 0), 0, 0)) == CC_REGNUM
1869 && CONST_INT_P (XEXP (*op0, 1))
1870 && CONST_INT_P (*op1)
1871 && INTVAL (XEXP (*op0, 1)) == -3
1872 && (*code == EQ || *code == NE))
1873 {
1874 if (INTVAL (*op1) == 0)
1875 {
1876 /* case cc == 0 || cc = 2 => mask = 0xa */
1877 *op0 = XVECEXP (XEXP (*op0, 0), 0, 0);
1878 *op1 = gen_rtx_CONST_INT (VOIDmode, 0xa);
1879 }
1880 else if (INTVAL (*op1) == 1)
1881 {
1882 /* case cc == 1 || cc == 3 => mask = 0x5 */
1883 *op0 = XVECEXP (XEXP (*op0, 0), 0, 0);
1884 *op1 = gen_rtx_CONST_INT (VOIDmode, 0x5);
1885 }
1886 }
1887 if (GET_CODE (*op0) == PLUS
1888 && GET_CODE (XEXP (*op0, 0)) == UNSPEC
1889 && XINT (XEXP (*op0, 0), 1) == UNSPEC_CC_TO_INT
1890 && XVECLEN (XEXP (*op0, 0), 0) == 1
1891 && REGNO (XVECEXP (XEXP (*op0, 0), 0, 0)) == CC_REGNUM
1892 && CONST_INT_P (XEXP (*op0, 1))
1893 && CONST_INT_P (*op1)
1894 && (*code == LEU || *code == GTU))
1895 {
1896 if (INTVAL (*op1) == 1)
1897 {
1898 if (INTVAL (XEXP (*op0, 1)) == -1)
1899 {
1900 /* case cc == 1 || cc == 2 => mask = 0x6 */
1901 *op0 = XVECEXP (XEXP (*op0, 0), 0, 0);
1902 *op1 = gen_rtx_CONST_INT (VOIDmode, 0x6);
1903 *code = *code == GTU ? NE : EQ;
1904 }
1905 else if (INTVAL (XEXP (*op0, 1)) == -2)
1906 {
1907 /* case cc == 2 || cc == 3 => mask = 0x3 */
1908 *op0 = XVECEXP (XEXP (*op0, 0), 0, 0);
1909 *op1 = gen_rtx_CONST_INT (VOIDmode, 0x3);
1910 *code = *code == GTU ? NE : EQ;
1911 }
1912 }
1913 else if (INTVAL (*op1) == 2
1914 && INTVAL (XEXP (*op0, 1)) == -1)
1915 {
1916 /* case cc == 1 || cc == 2 || cc == 3 => mask = 0x7 */
1917 *op0 = XVECEXP (XEXP (*op0, 0), 0, 0);
1918 *op1 = gen_rtx_CONST_INT (VOIDmode, 0x7);
1919 *code = *code == GTU ? NE : EQ;
1920 }
1921 }
1922 else if (*code == LEU || *code == GTU)
1923 {
1924 if (GET_CODE (*op0) == UNSPEC
1925 && XINT (*op0, 1) == UNSPEC_CC_TO_INT
1926 && XVECLEN (*op0, 0) == 1
1927 && REGNO (XVECEXP (*op0, 0, 0)) == CC_REGNUM
1928 && CONST_INT_P (*op1))
1929 {
1930 if (INTVAL (*op1) == 1)
1931 {
1932 /* case cc == 0 || cc == 1 => mask = 0xc */
1933 *op0 = XVECEXP (*op0, 0, 0);
1934 *op1 = gen_rtx_CONST_INT (VOIDmode, 0xc);
1935 *code = *code == GTU ? NE : EQ;
1936 }
1937 else if (INTVAL (*op1) == 2)
1938 {
1939 /* case cc == 0 || cc == 1 || cc == 2 => mask = 0xd */
1940 *op0 = XVECEXP (*op0, 0, 0);
1941 *op1 = gen_rtx_CONST_INT (VOIDmode, 0xd);
1942 *code = *code == GTU ? NE : EQ;
1943 }
1944 else if (INTVAL (*op1) == 3)
1945 {
1946 /* always true */
1947 *op0 = const0_rtx;
1948 *op1 = const0_rtx;
1949 *code = *code == GTU ? NE : EQ;
1950 }
1951 }
1952 }
1953
1954 /* Simplify cascaded EQ, NE with const0_rtx. */
1955 if ((*code == NE || *code == EQ)
1956 && (GET_CODE (*op0) == EQ || GET_CODE (*op0) == NE)
1957 && GET_MODE (*op0) == SImode
1958 && GET_MODE (XEXP (*op0, 0)) == CCZ1mode
1959 && REG_P (XEXP (*op0, 0))
1960 && XEXP (*op0, 1) == const0_rtx
1961 && *op1 == const0_rtx)
1962 {
1963 if ((*code == EQ && GET_CODE (*op0) == NE)
1964 || (*code == NE && GET_CODE (*op0) == EQ))
1965 *code = EQ;
1966 else
1967 *code = NE;
1968 *op0 = XEXP (*op0, 0);
1969 }
1970
1971 /* Prefer register over memory as first operand. */
1972 if (MEM_P (*op0) && REG_P (*op1))
1973 {
1974 rtx tem = *op0; *op0 = *op1; *op1 = tem;
1975 *code = (int)swap_condition ((enum rtx_code)*code);
1976 }
1977
1978 /* A comparison result is compared against zero. Replace it with
1979 the (perhaps inverted) original comparison.
1980 This probably should be done by simplify_relational_operation. */
1981 if ((*code == EQ || *code == NE)
1982 && *op1 == const0_rtx
1983 && COMPARISON_P (*op0)
1984 && CC_REG_P (XEXP (*op0, 0)))
1985 {
1986 enum rtx_code new_code;
1987
1988 if (*code == EQ)
1989 new_code = reversed_comparison_code_parts (GET_CODE (*op0),
1990 XEXP (*op0, 0),
1991 XEXP (*op0, 1), NULL);
1992 else
1993 new_code = GET_CODE (*op0);
1994
1995 if (new_code != UNKNOWN)
1996 {
1997 *code = new_code;
1998 *op1 = XEXP (*op0, 1);
1999 *op0 = XEXP (*op0, 0);
2000 }
2001 }
2002
2003 /* ~a==b -> ~(a^b)==0 ~a!=b -> ~(a^b)!=0 */
2004 if (TARGET_Z15
2005 && (*code == EQ || *code == NE)
2006 && (GET_MODE (*op0) == DImode || GET_MODE (*op0) == SImode)
2007 && GET_CODE (*op0) == NOT)
2008 {
2009 machine_mode mode = GET_MODE (*op0);
2010 *op0 = gen_rtx_XOR (mode, XEXP (*op0, 0), *op1);
2011 *op0 = gen_rtx_NOT (mode, *op0);
2012 *op1 = const0_rtx;
2013 }
2014
2015 /* a&b == -1 -> ~a|~b == 0 a|b == -1 -> ~a&~b == 0 */
2016 if (TARGET_Z15
2017 && (*code == EQ || *code == NE)
2018 && (GET_CODE (*op0) == AND || GET_CODE (*op0) == IOR)
2019 && (GET_MODE (*op0) == DImode || GET_MODE (*op0) == SImode)
2020 && CONST_INT_P (*op1)
2021 && *op1 == constm1_rtx)
2022 {
2023 machine_mode mode = GET_MODE (*op0);
2024 rtx op00 = gen_rtx_NOT (mode, XEXP (*op0, 0));
2025 rtx op01 = gen_rtx_NOT (mode, XEXP (*op0, 1));
2026
2027 if (GET_CODE (*op0) == AND)
2028 *op0 = gen_rtx_IOR (mode, op00, op01);
2029 else
2030 *op0 = gen_rtx_AND (mode, op00, op01);
2031
2032 *op1 = const0_rtx;
2033 }
2034 }
2035
2036
2037 /* Emit a compare instruction suitable to implement the comparison
2038 OP0 CODE OP1. Return the correct condition RTL to be placed in
2039 the IF_THEN_ELSE of the conditional branch testing the result. */
2040
2041 rtx
2042 s390_emit_compare (machine_mode mode, enum rtx_code code, rtx op0, rtx op1)
2043 {
2044 machine_mode cc_mode = s390_select_ccmode (code, op0, op1);
2045 rtx cc;
2046
2047 /* Force OP1 into register in order to satisfy VXE TFmode patterns. */
2048 if (TARGET_VXE && GET_MODE (op1) == TFmode)
2049 op1 = force_reg (TFmode, op1);
2050
2051 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
2052 {
2053 /* Do not output a redundant compare instruction if a
2054 compare_and_swap pattern already computed the result and the
2055 machine modes are compatible. */
2056 gcc_assert (s390_cc_modes_compatible (GET_MODE (op0), cc_mode)
2057 == GET_MODE (op0));
2058 cc = op0;
2059 }
2060 else
2061 {
2062 cc = gen_rtx_REG (cc_mode, CC_REGNUM);
2063 emit_insn (gen_rtx_SET (cc, gen_rtx_COMPARE (cc_mode, op0, op1)));
2064 }
2065
2066 return gen_rtx_fmt_ee (code, mode, cc, const0_rtx);
2067 }
2068
2069 /* If MEM is not a legitimate compare-and-swap memory operand, return a new
2070 MEM, whose address is a pseudo containing the original MEM's address. */
2071
2072 static rtx
2073 s390_legitimize_cs_operand (rtx mem)
2074 {
2075 rtx tmp;
2076
2077 if (!contains_symbol_ref_p (mem))
2078 return mem;
2079 tmp = gen_reg_rtx (Pmode);
2080 emit_move_insn (tmp, copy_rtx (XEXP (mem, 0)));
2081 return change_address (mem, VOIDmode, tmp);
2082 }
2083
2084 /* Emit a SImode compare and swap instruction setting MEM to NEW_RTX if OLD
2085 matches CMP.
2086 Return the correct condition RTL to be placed in the IF_THEN_ELSE of the
2087 conditional branch testing the result. */
2088
2089 static rtx
2090 s390_emit_compare_and_swap (enum rtx_code code, rtx old, rtx mem,
2091 rtx cmp, rtx new_rtx, machine_mode ccmode)
2092 {
2093 rtx cc;
2094
2095 mem = s390_legitimize_cs_operand (mem);
2096 cc = gen_rtx_REG (ccmode, CC_REGNUM);
2097 switch (GET_MODE (mem))
2098 {
2099 case E_SImode:
2100 emit_insn (gen_atomic_compare_and_swapsi_internal (old, mem, cmp,
2101 new_rtx, cc));
2102 break;
2103 case E_DImode:
2104 emit_insn (gen_atomic_compare_and_swapdi_internal (old, mem, cmp,
2105 new_rtx, cc));
2106 break;
2107 case E_TImode:
2108 emit_insn (gen_atomic_compare_and_swapti_internal (old, mem, cmp,
2109 new_rtx, cc));
2110 break;
2111 case E_QImode:
2112 case E_HImode:
2113 default:
2114 gcc_unreachable ();
2115 }
2116 return s390_emit_compare (VOIDmode, code, cc, const0_rtx);
2117 }
2118
2119 /* Emit a jump instruction to TARGET and return it. If COND is
2120 NULL_RTX, emit an unconditional jump, else a conditional jump under
2121 condition COND. */
2122
2123 rtx_insn *
2124 s390_emit_jump (rtx target, rtx cond)
2125 {
2126 rtx insn;
2127
2128 target = gen_rtx_LABEL_REF (VOIDmode, target);
2129 if (cond)
2130 target = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, target, pc_rtx);
2131
2132 insn = gen_rtx_SET (pc_rtx, target);
2133 return emit_jump_insn (insn);
2134 }
2135
2136 /* Return branch condition mask to implement a branch
2137 specified by CODE. Return -1 for invalid comparisons. */
2138
2139 int
2140 s390_branch_condition_mask (rtx code)
2141 {
2142 const int CC0 = 1 << 3;
2143 const int CC1 = 1 << 2;
2144 const int CC2 = 1 << 1;
2145 const int CC3 = 1 << 0;
2146
2147 gcc_assert (GET_CODE (XEXP (code, 0)) == REG);
2148 gcc_assert (REGNO (XEXP (code, 0)) == CC_REGNUM);
2149 gcc_assert (XEXP (code, 1) == const0_rtx
2150 || (GET_MODE (XEXP (code, 0)) == CCRAWmode
2151 && CONST_INT_P (XEXP (code, 1))));
2152
2153
2154 switch (GET_MODE (XEXP (code, 0)))
2155 {
2156 case E_CCZmode:
2157 case E_CCZ1mode:
2158 switch (GET_CODE (code))
2159 {
2160 case EQ: return CC0;
2161 case NE: return CC1 | CC2 | CC3;
2162 default: return -1;
2163 }
2164 break;
2165
2166 case E_CCT1mode:
2167 switch (GET_CODE (code))
2168 {
2169 case EQ: return CC1;
2170 case NE: return CC0 | CC2 | CC3;
2171 default: return -1;
2172 }
2173 break;
2174
2175 case E_CCT2mode:
2176 switch (GET_CODE (code))
2177 {
2178 case EQ: return CC2;
2179 case NE: return CC0 | CC1 | CC3;
2180 default: return -1;
2181 }
2182 break;
2183
2184 case E_CCT3mode:
2185 switch (GET_CODE (code))
2186 {
2187 case EQ: return CC3;
2188 case NE: return CC0 | CC1 | CC2;
2189 default: return -1;
2190 }
2191 break;
2192
2193 case E_CCLmode:
2194 switch (GET_CODE (code))
2195 {
2196 case EQ: return CC0 | CC2;
2197 case NE: return CC1 | CC3;
2198 default: return -1;
2199 }
2200 break;
2201
2202 case E_CCL1mode:
2203 switch (GET_CODE (code))
2204 {
2205 case LTU: return CC2 | CC3; /* carry */
2206 case GEU: return CC0 | CC1; /* no carry */
2207 default: return -1;
2208 }
2209 break;
2210
2211 case E_CCL2mode:
2212 switch (GET_CODE (code))
2213 {
2214 case GTU: return CC0 | CC1; /* borrow */
2215 case LEU: return CC2 | CC3; /* no borrow */
2216 default: return -1;
2217 }
2218 break;
2219
2220 case E_CCL3mode:
2221 switch (GET_CODE (code))
2222 {
2223 case EQ: return CC0 | CC2;
2224 case NE: return CC1 | CC3;
2225 case LTU: return CC1;
2226 case GTU: return CC3;
2227 case LEU: return CC1 | CC2;
2228 case GEU: return CC2 | CC3;
2229 default: return -1;
2230 }
2231
2232 case E_CCUmode:
2233 switch (GET_CODE (code))
2234 {
2235 case EQ: return CC0;
2236 case NE: return CC1 | CC2 | CC3;
2237 case LTU: return CC1;
2238 case GTU: return CC2;
2239 case LEU: return CC0 | CC1;
2240 case GEU: return CC0 | CC2;
2241 default: return -1;
2242 }
2243 break;
2244
2245 case E_CCURmode:
2246 switch (GET_CODE (code))
2247 {
2248 case EQ: return CC0;
2249 case NE: return CC2 | CC1 | CC3;
2250 case LTU: return CC2;
2251 case GTU: return CC1;
2252 case LEU: return CC0 | CC2;
2253 case GEU: return CC0 | CC1;
2254 default: return -1;
2255 }
2256 break;
2257
2258 case E_CCAPmode:
2259 switch (GET_CODE (code))
2260 {
2261 case EQ: return CC0;
2262 case NE: return CC1 | CC2 | CC3;
2263 case LT: return CC1 | CC3;
2264 case GT: return CC2;
2265 case LE: return CC0 | CC1 | CC3;
2266 case GE: return CC0 | CC2;
2267 default: return -1;
2268 }
2269 break;
2270
2271 case E_CCANmode:
2272 switch (GET_CODE (code))
2273 {
2274 case EQ: return CC0;
2275 case NE: return CC1 | CC2 | CC3;
2276 case LT: return CC1;
2277 case GT: return CC2 | CC3;
2278 case LE: return CC0 | CC1;
2279 case GE: return CC0 | CC2 | CC3;
2280 default: return -1;
2281 }
2282 break;
2283
2284 case E_CCOmode:
2285 switch (GET_CODE (code))
2286 {
2287 case EQ: return CC0 | CC1 | CC2;
2288 case NE: return CC3;
2289 default: return -1;
2290 }
2291 break;
2292
2293 case E_CCSmode:
2294 case E_CCSFPSmode:
2295 switch (GET_CODE (code))
2296 {
2297 case EQ: return CC0;
2298 case NE: return CC1 | CC2 | CC3;
2299 case LT: return CC1;
2300 case GT: return CC2;
2301 case LE: return CC0 | CC1;
2302 case GE: return CC0 | CC2;
2303 case UNORDERED: return CC3;
2304 case ORDERED: return CC0 | CC1 | CC2;
2305 case UNEQ: return CC0 | CC3;
2306 case UNLT: return CC1 | CC3;
2307 case UNGT: return CC2 | CC3;
2308 case UNLE: return CC0 | CC1 | CC3;
2309 case UNGE: return CC0 | CC2 | CC3;
2310 case LTGT: return CC1 | CC2;
2311 default: return -1;
2312 }
2313 break;
2314
2315 case E_CCSRmode:
2316 switch (GET_CODE (code))
2317 {
2318 case EQ: return CC0;
2319 case NE: return CC2 | CC1 | CC3;
2320 case LT: return CC2;
2321 case GT: return CC1;
2322 case LE: return CC0 | CC2;
2323 case GE: return CC0 | CC1;
2324 case UNORDERED: return CC3;
2325 case ORDERED: return CC0 | CC2 | CC1;
2326 case UNEQ: return CC0 | CC3;
2327 case UNLT: return CC2 | CC3;
2328 case UNGT: return CC1 | CC3;
2329 case UNLE: return CC0 | CC2 | CC3;
2330 case UNGE: return CC0 | CC1 | CC3;
2331 case LTGT: return CC2 | CC1;
2332 default: return -1;
2333 }
2334 break;
2335
2336 /* Vector comparison modes. */
2337 /* CC2 will never be set. It however is part of the negated
2338 masks. */
2339 case E_CCVIALLmode:
2340 switch (GET_CODE (code))
2341 {
2342 case EQ:
2343 case GTU:
2344 case GT:
2345 case GE: return CC0;
2346 /* The inverted modes are in fact *any* modes. */
2347 case NE:
2348 case LEU:
2349 case LE:
2350 case LT: return CC3 | CC1 | CC2;
2351 default: return -1;
2352 }
2353
2354 case E_CCVIANYmode:
2355 switch (GET_CODE (code))
2356 {
2357 case EQ:
2358 case GTU:
2359 case GT:
2360 case GE: return CC0 | CC1;
2361 /* The inverted modes are in fact *all* modes. */
2362 case NE:
2363 case LEU:
2364 case LE:
2365 case LT: return CC3 | CC2;
2366 default: return -1;
2367 }
2368 case E_CCVFALLmode:
2369 switch (GET_CODE (code))
2370 {
2371 case EQ:
2372 case GT:
2373 case GE: return CC0;
2374 /* The inverted modes are in fact *any* modes. */
2375 case NE:
2376 case UNLE:
2377 case UNLT: return CC3 | CC1 | CC2;
2378 default: return -1;
2379 }
2380
2381 case E_CCVFANYmode:
2382 switch (GET_CODE (code))
2383 {
2384 case EQ:
2385 case GT:
2386 case GE: return CC0 | CC1;
2387 /* The inverted modes are in fact *all* modes. */
2388 case NE:
2389 case UNLE:
2390 case UNLT: return CC3 | CC2;
2391 default: return -1;
2392 }
2393
2394 case E_CCRAWmode:
2395 switch (GET_CODE (code))
2396 {
2397 case EQ:
2398 return INTVAL (XEXP (code, 1));
2399 case NE:
2400 return (INTVAL (XEXP (code, 1))) ^ 0xf;
2401 default:
2402 gcc_unreachable ();
2403 }
2404
2405 default:
2406 return -1;
2407 }
2408 }
2409
2410
2411 /* Return branch condition mask to implement a compare and branch
2412 specified by CODE. Return -1 for invalid comparisons. */
2413
2414 int
2415 s390_compare_and_branch_condition_mask (rtx code)
2416 {
2417 const int CC0 = 1 << 3;
2418 const int CC1 = 1 << 2;
2419 const int CC2 = 1 << 1;
2420
2421 switch (GET_CODE (code))
2422 {
2423 case EQ:
2424 return CC0;
2425 case NE:
2426 return CC1 | CC2;
2427 case LT:
2428 case LTU:
2429 return CC1;
2430 case GT:
2431 case GTU:
2432 return CC2;
2433 case LE:
2434 case LEU:
2435 return CC0 | CC1;
2436 case GE:
2437 case GEU:
2438 return CC0 | CC2;
2439 default:
2440 gcc_unreachable ();
2441 }
2442 return -1;
2443 }
2444
2445 /* If INV is false, return assembler mnemonic string to implement
2446 a branch specified by CODE. If INV is true, return mnemonic
2447 for the corresponding inverted branch. */
2448
2449 static const char *
2450 s390_branch_condition_mnemonic (rtx code, int inv)
2451 {
2452 int mask;
2453
2454 static const char *const mnemonic[16] =
2455 {
2456 NULL, "o", "h", "nle",
2457 "l", "nhe", "lh", "ne",
2458 "e", "nlh", "he", "nl",
2459 "le", "nh", "no", NULL
2460 };
2461
2462 if (GET_CODE (XEXP (code, 0)) == REG
2463 && REGNO (XEXP (code, 0)) == CC_REGNUM
2464 && (XEXP (code, 1) == const0_rtx
2465 || (GET_MODE (XEXP (code, 0)) == CCRAWmode
2466 && CONST_INT_P (XEXP (code, 1)))))
2467 mask = s390_branch_condition_mask (code);
2468 else
2469 mask = s390_compare_and_branch_condition_mask (code);
2470
2471 gcc_assert (mask >= 0);
2472
2473 if (inv)
2474 mask ^= 15;
2475
2476 gcc_assert (mask >= 1 && mask <= 14);
2477
2478 return mnemonic[mask];
2479 }
2480
2481 /* Return the part of op which has a value different from def.
2482 The size of the part is determined by mode.
2483 Use this function only if you already know that op really
2484 contains such a part. */
2485
2486 unsigned HOST_WIDE_INT
2487 s390_extract_part (rtx op, machine_mode mode, int def)
2488 {
2489 unsigned HOST_WIDE_INT value = 0;
2490 int max_parts = HOST_BITS_PER_WIDE_INT / GET_MODE_BITSIZE (mode);
2491 int part_bits = GET_MODE_BITSIZE (mode);
2492 unsigned HOST_WIDE_INT part_mask = (HOST_WIDE_INT_1U << part_bits) - 1;
2493 int i;
2494
2495 for (i = 0; i < max_parts; i++)
2496 {
2497 if (i == 0)
2498 value = UINTVAL (op);
2499 else
2500 value >>= part_bits;
2501
2502 if ((value & part_mask) != (def & part_mask))
2503 return value & part_mask;
2504 }
2505
2506 gcc_unreachable ();
2507 }
2508
2509 /* If OP is an integer constant of mode MODE with exactly one
2510 part of mode PART_MODE unequal to DEF, return the number of that
2511 part. Otherwise, return -1. */
2512
2513 int
2514 s390_single_part (rtx op,
2515 machine_mode mode,
2516 machine_mode part_mode,
2517 int def)
2518 {
2519 unsigned HOST_WIDE_INT value = 0;
2520 int n_parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (part_mode);
2521 unsigned HOST_WIDE_INT part_mask
2522 = (HOST_WIDE_INT_1U << GET_MODE_BITSIZE (part_mode)) - 1;
2523 int i, part = -1;
2524
2525 if (GET_CODE (op) != CONST_INT)
2526 return -1;
2527
2528 for (i = 0; i < n_parts; i++)
2529 {
2530 if (i == 0)
2531 value = UINTVAL (op);
2532 else
2533 value >>= GET_MODE_BITSIZE (part_mode);
2534
2535 if ((value & part_mask) != (def & part_mask))
2536 {
2537 if (part != -1)
2538 return -1;
2539 else
2540 part = i;
2541 }
2542 }
2543 return part == -1 ? -1 : n_parts - 1 - part;
2544 }
2545
2546 /* Return true if IN contains a contiguous bitfield in the lower SIZE
2547 bits and no other bits are set in (the lower SIZE bits of) IN.
2548
2549 PSTART and PEND can be used to obtain the start and end
2550 position (inclusive) of the bitfield relative to 64
2551 bits. *PSTART / *PEND gives the position of the first/last bit
2552 of the bitfield counting from the highest order bit starting
2553 with zero. */
2554
2555 bool
2556 s390_contiguous_bitmask_nowrap_p (unsigned HOST_WIDE_INT in, int size,
2557 int *pstart, int *pend)
2558 {
2559 int start;
2560 int end = -1;
2561 int lowbit = HOST_BITS_PER_WIDE_INT - 1;
2562 int highbit = HOST_BITS_PER_WIDE_INT - size;
2563 unsigned HOST_WIDE_INT bitmask = HOST_WIDE_INT_1U;
2564
2565 gcc_assert (!!pstart == !!pend);
2566 for (start = lowbit; start >= highbit; bitmask <<= 1, start--)
2567 if (end == -1)
2568 {
2569 /* Look for the rightmost bit of a contiguous range of ones. */
2570 if (bitmask & in)
2571 /* Found it. */
2572 end = start;
2573 }
2574 else
2575 {
2576 /* Look for the firt zero bit after the range of ones. */
2577 if (! (bitmask & in))
2578 /* Found it. */
2579 break;
2580 }
2581 /* We're one past the last one-bit. */
2582 start++;
2583
2584 if (end == -1)
2585 /* No one bits found. */
2586 return false;
2587
2588 if (start > highbit)
2589 {
2590 unsigned HOST_WIDE_INT mask;
2591
2592 /* Calculate a mask for all bits beyond the contiguous bits. */
2593 mask = ((~HOST_WIDE_INT_0U >> highbit)
2594 & (~HOST_WIDE_INT_0U << (lowbit - start + 1)));
2595 if (mask & in)
2596 /* There are more bits set beyond the first range of one bits. */
2597 return false;
2598 }
2599
2600 if (pstart)
2601 {
2602 *pstart = start;
2603 *pend = end;
2604 }
2605
2606 return true;
2607 }
2608
2609 /* Same as s390_contiguous_bitmask_nowrap_p but also returns true
2610 if ~IN contains a contiguous bitfield. In that case, *END is <
2611 *START.
2612
2613 If WRAP_P is true, a bitmask that wraps around is also tested.
2614 When a wraparoud occurs *START is greater than *END (in
2615 non-null pointers), and the uppermost (64 - SIZE) bits are thus
2616 part of the range. If WRAP_P is false, no wraparound is
2617 tested. */
2618
2619 bool
2620 s390_contiguous_bitmask_p (unsigned HOST_WIDE_INT in, bool wrap_p,
2621 int size, int *start, int *end)
2622 {
2623 int bs = HOST_BITS_PER_WIDE_INT;
2624 bool b;
2625
2626 gcc_assert (!!start == !!end);
2627 if ((in & ((~HOST_WIDE_INT_0U) >> (bs - size))) == 0)
2628 /* This cannot be expressed as a contiguous bitmask. Exit early because
2629 the second call of s390_contiguous_bitmask_nowrap_p would accept this as
2630 a valid bitmask. */
2631 return false;
2632 b = s390_contiguous_bitmask_nowrap_p (in, size, start, end);
2633 if (b)
2634 return true;
2635 if (! wrap_p)
2636 return false;
2637 b = s390_contiguous_bitmask_nowrap_p (~in, size, start, end);
2638 if (b && start)
2639 {
2640 int s = *start;
2641 int e = *end;
2642
2643 gcc_assert (s >= 1);
2644 *start = ((e + 1) & (bs - 1));
2645 *end = ((s - 1 + bs) & (bs - 1));
2646 }
2647
2648 return b;
2649 }
2650
2651 /* Return true if OP is a constant which fits into a vector register and if it
2652 is a 16-byte constant, then the high and low half must equal. Otherwise
2653 return false. The out parameter *VEC2 equals the high/low half for 16-byte
2654 constants and for smaller constants it equals the concatination of constant
2655 OP until an 8-byte constant is constructed. */
2656
2657 static bool
2658 s390_constant_via_vgm_vrepi_1 (rtx op, unsigned HOST_WIDE_INT *vec2)
2659 {
2660 unsigned HOST_WIDE_INT vec;
2661
2662 if (GET_CODE (op) == CONST_VECTOR)
2663 switch (GET_MODE_SIZE (GET_MODE (op)))
2664 {
2665 case 1:
2666 {
2667 rtx op_v1qi = gen_lowpart (V1QImode, op);
2668 vec = UINTVAL (XVECEXP (op_v1qi, 0, 0));
2669 vec &= GET_MODE_MASK (QImode);
2670 vec |= vec << 8;
2671 vec |= vec << 16;
2672 vec |= vec << 32;
2673 *vec2 = vec;
2674 return true;
2675 }
2676 case 2:
2677 {
2678 rtx op_v1hi = gen_lowpart (V1HImode, op);
2679 vec = UINTVAL (XVECEXP (op_v1hi, 0, 0));
2680 vec &= GET_MODE_MASK (HImode);
2681 vec |= vec << 16;
2682 vec |= vec << 32;
2683 *vec2 = vec;
2684 return true;
2685 }
2686 case 4:
2687 {
2688 rtx op_v1si = gen_lowpart (V1SImode, op);
2689 vec = UINTVAL (XVECEXP (op_v1si, 0, 0));
2690 vec &= GET_MODE_MASK (SImode);
2691 vec |= vec << 32;
2692 *vec2 = vec;
2693 return true;
2694 }
2695 case 8:
2696 {
2697 rtx op_v1di = gen_lowpart (V1DImode, op);
2698 vec = UINTVAL (XVECEXP (op_v1di, 0, 0));
2699 *vec2 = vec;
2700 return true;
2701 }
2702 case 16:
2703 {
2704 rtx op_v2di = gen_lowpart (V2DImode, op);
2705 vec = UINTVAL (XVECEXP (op_v2di, 0, 0));
2706 unsigned HOST_WIDE_INT tmp = UINTVAL (XVECEXP (op_v2di, 0, 1));
2707 if (vec != tmp)
2708 return false;
2709 *vec2 = vec;
2710 return true;
2711 }
2712 default:
2713 return false;
2714 }
2715 else if (CONST_WIDE_INT_P (op) && CONST_WIDE_INT_NUNITS (op) == 2)
2716 {
2717 vec = CONST_WIDE_INT_ELT (op, 0);
2718 unsigned HOST_WIDE_INT tmp = CONST_WIDE_INT_ELT (op, 1);
2719 if (vec != tmp)
2720 return false;
2721 *vec2 = vec;
2722 return true;
2723 }
2724 else if (CONST_DOUBLE_P (op) && GET_MODE (op) == TFmode)
2725 {
2726 long l[4];
2727 const REAL_VALUE_TYPE *rv = CONST_DOUBLE_REAL_VALUE (op);
2728 REAL_VALUE_TO_TARGET_LONG_DOUBLE (*rv, l);
2729 vec = ((unsigned HOST_WIDE_INT) l[0] << 32)
2730 | ((unsigned HOST_WIDE_INT) l[1] & 0xffffffffUL);
2731 unsigned HOST_WIDE_INT tmp
2732 = ((unsigned HOST_WIDE_INT) l[2] << 32)
2733 | ((unsigned HOST_WIDE_INT) l[3] & 0xffffffffUL);
2734 if (vec != tmp)
2735 return false;
2736 *vec2 = vec;
2737 return true;
2738 }
2739 else
2740 return false;
2741 }
2742
2743 /* Return true if constant OP can be loaded via VGM. Otherwise return false.
2744 If MODE, START, and END are not null, then out parameter *MODE is the
2745 element size of VGM, and *START and *END are the starting and ending bit
2746 positions, respectively. */
2747
2748 bool
2749 s390_constant_via_vgm_p (rtx op, machine_mode *mode, int *start, int *end)
2750 {
2751 unsigned HOST_WIDE_INT vec;
2752
2753 if (!s390_constant_via_vgm_vrepi_1 (op, &vec))
2754 return false;
2755
2756 machine_mode iter;
2757 FOR_EACH_MODE_UNTIL (iter, TImode)
2758 {
2759 unsigned HOST_WIDE_INT bits = vec & GET_MODE_MASK (iter);
2760 bool b = s390_contiguous_bitmask_p (bits, true, GET_MODE_BITSIZE (iter),
2761 start, end);
2762 if (!b)
2763 continue;
2764 unsigned HOST_WIDE_INT vec2 = bits;
2765 for (int i = 1; i < 8 / GET_MODE_SIZE (iter); ++i)
2766 vec2 |= bits << (GET_MODE_BITSIZE (iter) * i);
2767 if (vec == vec2)
2768 {
2769 if (mode && start && end)
2770 {
2771 *mode = iter;
2772 *start -= (HOST_BITS_PER_WIDE_INT - GET_MODE_BITSIZE (iter));
2773 *end -= (HOST_BITS_PER_WIDE_INT - GET_MODE_BITSIZE (iter));
2774 }
2775 return true;
2776 }
2777 }
2778
2779 return false;
2780 }
2781
2782 /* Return true if constant OP can be loaded via VREPI. Otherwise return false.
2783 If MODE and IMM are not null, then out parameter *MODE is the element size
2784 of VREPI, and *IMM is the signed integer immediate. */
2785
2786 bool
2787 s390_constant_via_vrepi_p (rtx op, machine_mode *mode, short *imm)
2788 {
2789 unsigned HOST_WIDE_INT vec;
2790
2791 if (!s390_constant_via_vgm_vrepi_1 (op, &vec))
2792 return false;
2793
2794 unsigned HOST_WIDE_INT bits = (short) (vec & 0xffff);
2795
2796 machine_mode iter;
2797 FOR_EACH_MODE_UNTIL (iter, TImode)
2798 {
2799 unsigned HOST_WIDE_INT tmp = bits & GET_MODE_MASK (iter);
2800 unsigned HOST_WIDE_INT vec2 = tmp;
2801 for (int i = 1; i < 8 / GET_MODE_SIZE (iter); ++i)
2802 vec2 |= tmp << (GET_MODE_BITSIZE (iter) * i);
2803 if (vec == vec2)
2804 {
2805 if (mode && imm)
2806 {
2807 *mode = iter;
2808 /* Although, vrepib ignores the high half of the 16-bit mask,
2809 canonicalize to an 8-bit sign-extended mask. */
2810 *imm = iter == QImode ? (signed char) (bits & 0xff)
2811 : (short) (bits & 0xffff);
2812 }
2813 return true;
2814 }
2815 }
2816
2817 return false;
2818 }
2819
2820 /* Return true if OP consists only of byte chunks being either 0 or
2821 0xff. If MASK is !=NULL a byte mask is generated which is
2822 appropriate for the vector generate byte mask instruction. */
2823
2824 bool
2825 s390_constant_via_vgbm_p (rtx op, unsigned *mask)
2826 {
2827 int i;
2828 unsigned tmp_mask = 0;
2829 int nunit, unit_size;
2830
2831 if (GET_CODE (op) == CONST_VECTOR && GET_MODE_SIZE (GET_MODE (op)) <= 16)
2832 {
2833 if (GET_MODE_INNER (GET_MODE (op)) == TImode
2834 || GET_MODE_INNER (GET_MODE (op)) == TFmode)
2835 /* For the sake of simplicity, bitcast 16-byte one-element vector
2836 into two-element vector so we don't have to special case them in
2837 the following. */
2838 op = gen_lowpart (V2DImode, op);
2839 else
2840 /* Handle floats by bitcasting them to ints. */
2841 op = gen_lowpart (related_int_vector_mode (GET_MODE (op)).require (),
2842 op);
2843 }
2844 else if ((CONST_WIDE_INT_P (op) && CONST_WIDE_INT_NUNITS (op) == 2)
2845 || (CONST_DOUBLE_P (op) && GET_MODE (op) == TFmode))
2846 /* For the sake of simplicity, bitcast the 16-byte constants into a vector
2847 so we don't have to special case them in the following. */
2848 op = gen_lowpart (V2DImode, op);
2849 else
2850 return false;
2851
2852 nunit = GET_MODE_NUNITS (GET_MODE (op));
2853 unit_size = GET_MODE_UNIT_SIZE (GET_MODE (op));
2854
2855 for (i = 0; i < nunit; i++)
2856 {
2857 unsigned HOST_WIDE_INT c;
2858 int j;
2859
2860 c = UINTVAL (XVECEXP (op, 0, i));
2861 for (j = 0; j < unit_size; j++)
2862 {
2863 if ((c & 0xff) != 0 && (c & 0xff) != 0xff)
2864 return false;
2865 tmp_mask |= (c & 1) << ((nunit - 1 - i) * unit_size + j);
2866 c = c >> BITS_PER_UNIT;
2867 }
2868 }
2869
2870 if (mask != NULL)
2871 *mask = tmp_mask << (16 - GET_MODE_SIZE (GET_MODE (op)));
2872
2873 return true;
2874 }
2875
2876 /* Check whether a rotate of ROTL followed by an AND of CONTIG is
2877 equivalent to a shift followed by the AND. In particular, CONTIG
2878 should not overlap the (rotated) bit 0/bit 63 gap. Negative values
2879 for ROTL indicate a rotate to the right. */
2880
2881 bool
2882 s390_extzv_shift_ok (int bitsize, int rotl, unsigned HOST_WIDE_INT contig)
2883 {
2884 int start, end;
2885 bool ok;
2886
2887 ok = s390_contiguous_bitmask_nowrap_p (contig, bitsize, &start, &end);
2888 gcc_assert (ok);
2889
2890 if (rotl >= 0)
2891 return (64 - end >= rotl);
2892 else
2893 {
2894 /* Translate "- rotate right" in BITSIZE mode to "rotate left" in
2895 DIMode. */
2896 rotl = -rotl + (64 - bitsize);
2897 return (start >= rotl);
2898 }
2899 }
2900
2901 /* Check whether we can (and want to) split a double-word
2902 move in mode MODE from SRC to DST into two single-word
2903 moves, moving the subword FIRST_SUBWORD first. */
2904
2905 bool
2906 s390_split_ok_p (rtx dst, rtx src, machine_mode mode, int first_subword)
2907 {
2908 /* Floating point and vector registers cannot be split. */
2909 if (FP_REG_P (src) || FP_REG_P (dst) || VECTOR_REG_P (src) || VECTOR_REG_P (dst))
2910 return false;
2911
2912 /* Non-offsettable memory references cannot be split. */
2913 if ((GET_CODE (src) == MEM && !offsettable_memref_p (src))
2914 || (GET_CODE (dst) == MEM && !offsettable_memref_p (dst)))
2915 return false;
2916
2917 /* Moving the first subword must not clobber a register
2918 needed to move the second subword. */
2919 if (register_operand (dst, mode))
2920 {
2921 rtx subreg = operand_subword (dst, first_subword, 0, mode);
2922 if (reg_overlap_mentioned_p (subreg, src))
2923 return false;
2924 }
2925
2926 return true;
2927 }
2928
2929 /* Return true if it can be proven that [MEM1, MEM1 + SIZE]
2930 and [MEM2, MEM2 + SIZE] do overlap and false
2931 otherwise. */
2932
2933 bool
2934 s390_overlap_p (rtx mem1, rtx mem2, HOST_WIDE_INT size)
2935 {
2936 rtx addr1, addr2, addr_delta;
2937 HOST_WIDE_INT delta;
2938
2939 if (GET_CODE (mem1) != MEM || GET_CODE (mem2) != MEM)
2940 return true;
2941
2942 if (size == 0)
2943 return false;
2944
2945 addr1 = XEXP (mem1, 0);
2946 addr2 = XEXP (mem2, 0);
2947
2948 addr_delta = simplify_binary_operation (MINUS, Pmode, addr2, addr1);
2949
2950 /* This overlapping check is used by peepholes merging memory block operations.
2951 Overlapping operations would otherwise be recognized by the S/390 hardware
2952 and would fall back to a slower implementation. Allowing overlapping
2953 operations would lead to slow code but not to wrong code. Therefore we are
2954 somewhat optimistic if we cannot prove that the memory blocks are
2955 overlapping.
2956 That's why we return false here although this may accept operations on
2957 overlapping memory areas. */
2958 if (!addr_delta || GET_CODE (addr_delta) != CONST_INT)
2959 return false;
2960
2961 delta = INTVAL (addr_delta);
2962
2963 if (delta == 0
2964 || (delta > 0 && delta < size)
2965 || (delta < 0 && -delta < size))
2966 return true;
2967
2968 return false;
2969 }
2970
2971 /* Check whether the address of memory reference MEM2 equals exactly
2972 the address of memory reference MEM1 plus DELTA. Return true if
2973 we can prove this to be the case, false otherwise. */
2974
2975 bool
2976 s390_offset_p (rtx mem1, rtx mem2, rtx delta)
2977 {
2978 rtx addr1, addr2, addr_delta;
2979
2980 if (GET_CODE (mem1) != MEM || GET_CODE (mem2) != MEM)
2981 return false;
2982
2983 addr1 = XEXP (mem1, 0);
2984 addr2 = XEXP (mem2, 0);
2985
2986 addr_delta = simplify_binary_operation (MINUS, Pmode, addr2, addr1);
2987 if (!addr_delta || !rtx_equal_p (addr_delta, delta))
2988 return false;
2989
2990 return true;
2991 }
2992
2993 /* Expand logical operator CODE in mode MODE with operands OPERANDS. */
2994
2995 void
2996 s390_expand_logical_operator (enum rtx_code code, machine_mode mode,
2997 rtx *operands)
2998 {
2999 machine_mode wmode = mode;
3000 rtx dst = operands[0];
3001 rtx src1 = operands[1];
3002 rtx src2 = operands[2];
3003 rtx op, clob, tem;
3004
3005 /* If we cannot handle the operation directly, use a temp register. */
3006 if (!s390_logical_operator_ok_p (operands))
3007 dst = gen_reg_rtx (mode);
3008
3009 /* QImode and HImode patterns make sense only if we have a destination
3010 in memory. Otherwise perform the operation in SImode. */
3011 if ((mode == QImode || mode == HImode) && GET_CODE (dst) != MEM)
3012 wmode = SImode;
3013
3014 /* Widen operands if required. */
3015 if (mode != wmode)
3016 {
3017 if (GET_CODE (dst) == SUBREG
3018 && (tem = simplify_subreg (wmode, dst, mode, 0)) != 0)
3019 dst = tem;
3020 else if (REG_P (dst))
3021 dst = gen_rtx_SUBREG (wmode, dst, 0);
3022 else
3023 dst = gen_reg_rtx (wmode);
3024
3025 if (GET_CODE (src1) == SUBREG
3026 && (tem = simplify_subreg (wmode, src1, mode, 0)) != 0)
3027 src1 = tem;
3028 else if (GET_MODE (src1) != VOIDmode)
3029 src1 = gen_rtx_SUBREG (wmode, force_reg (mode, src1), 0);
3030
3031 if (GET_CODE (src2) == SUBREG
3032 && (tem = simplify_subreg (wmode, src2, mode, 0)) != 0)
3033 src2 = tem;
3034 else if (GET_MODE (src2) != VOIDmode)
3035 src2 = gen_rtx_SUBREG (wmode, force_reg (mode, src2), 0);
3036 }
3037
3038 /* Emit the instruction. */
3039 op = gen_rtx_SET (dst, gen_rtx_fmt_ee (code, wmode, src1, src2));
3040 clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
3041 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
3042
3043 /* Fix up the destination if needed. */
3044 if (dst != operands[0])
3045 emit_move_insn (operands[0], gen_lowpart (mode, dst));
3046 }
3047
3048 /* Check whether OPERANDS are OK for a logical operation (AND, IOR, XOR). */
3049
3050 bool
3051 s390_logical_operator_ok_p (rtx *operands)
3052 {
3053 /* If the destination operand is in memory, it needs to coincide
3054 with one of the source operands. After reload, it has to be
3055 the first source operand. */
3056 if (GET_CODE (operands[0]) == MEM)
3057 return rtx_equal_p (operands[0], operands[1])
3058 || (!reload_completed && rtx_equal_p (operands[0], operands[2]));
3059
3060 return true;
3061 }
3062
3063 /* Narrow logical operation CODE of memory operand MEMOP with immediate
3064 operand IMMOP to switch from SS to SI type instructions. */
3065
3066 void
3067 s390_narrow_logical_operator (enum rtx_code code, rtx *memop, rtx *immop)
3068 {
3069 int def = code == AND ? -1 : 0;
3070 HOST_WIDE_INT mask;
3071 int part;
3072
3073 gcc_assert (GET_CODE (*memop) == MEM);
3074 gcc_assert (!MEM_VOLATILE_P (*memop));
3075
3076 mask = s390_extract_part (*immop, QImode, def);
3077 part = s390_single_part (*immop, GET_MODE (*memop), QImode, def);
3078 gcc_assert (part >= 0);
3079
3080 *memop = adjust_address (*memop, QImode, part);
3081 *immop = gen_int_mode (mask, QImode);
3082 }
3083
3084
3085 /* How to allocate a 'struct machine_function'. */
3086
3087 static struct machine_function *
3088 s390_init_machine_status (void)
3089 {
3090 return ggc_cleared_alloc<machine_function> ();
3091 }
3092
3093 /* Map for smallest class containing reg regno. */
3094
3095 const enum reg_class regclass_map[FIRST_PSEUDO_REGISTER] =
3096 { GENERAL_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS, /* 0 */
3097 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS, /* 4 */
3098 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS, /* 8 */
3099 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS, /* 12 */
3100 FP_REGS, FP_REGS, FP_REGS, FP_REGS, /* 16 */
3101 FP_REGS, FP_REGS, FP_REGS, FP_REGS, /* 20 */
3102 FP_REGS, FP_REGS, FP_REGS, FP_REGS, /* 24 */
3103 FP_REGS, FP_REGS, FP_REGS, FP_REGS, /* 28 */
3104 ADDR_REGS, CC_REGS, ADDR_REGS, ADDR_REGS, /* 32 */
3105 ACCESS_REGS, ACCESS_REGS, VEC_REGS, VEC_REGS, /* 36 */
3106 VEC_REGS, VEC_REGS, VEC_REGS, VEC_REGS, /* 40 */
3107 VEC_REGS, VEC_REGS, VEC_REGS, VEC_REGS, /* 44 */
3108 VEC_REGS, VEC_REGS, VEC_REGS, VEC_REGS, /* 48 */
3109 VEC_REGS, VEC_REGS /* 52 */
3110 };
3111
3112 /* Return attribute type of insn. */
3113
3114 static enum attr_type
3115 s390_safe_attr_type (rtx_insn *insn)
3116 {
3117 if (recog_memoized (insn) >= 0)
3118 return get_attr_type (insn);
3119 else
3120 return TYPE_NONE;
3121 }
3122
3123 /* Return attribute relative_long of insn. */
3124
3125 static bool
3126 s390_safe_relative_long_p (rtx_insn *insn)
3127 {
3128 if (recog_memoized (insn) >= 0)
3129 return get_attr_relative_long (insn) == RELATIVE_LONG_YES;
3130 else
3131 return false;
3132 }
3133
3134 /* Return true if DISP is a valid short displacement. */
3135
3136 static bool
3137 s390_short_displacement (rtx disp)
3138 {
3139 /* No displacement is OK. */
3140 if (!disp)
3141 return true;
3142
3143 /* Without the long displacement facility we don't need to
3144 distingiush between long and short displacement. */
3145 if (!TARGET_LONG_DISPLACEMENT)
3146 return true;
3147
3148 /* Integer displacement in range. */
3149 if (GET_CODE (disp) == CONST_INT)
3150 return INTVAL (disp) >= 0 && INTVAL (disp) < 4096;
3151
3152 /* GOT offset is not OK, the GOT can be large. */
3153 if (GET_CODE (disp) == CONST
3154 && GET_CODE (XEXP (disp, 0)) == UNSPEC
3155 && (XINT (XEXP (disp, 0), 1) == UNSPEC_GOT
3156 || XINT (XEXP (disp, 0), 1) == UNSPEC_GOTNTPOFF))
3157 return false;
3158
3159 /* All other symbolic constants are literal pool references,
3160 which are OK as the literal pool must be small. */
3161 if (GET_CODE (disp) == CONST)
3162 return true;
3163
3164 return false;
3165 }
3166
3167 /* Attempts to split `ref', which should be UNSPEC_LTREF, into (base + `disp').
3168 If successful, also determines the
3169 following characteristics of `ref': `is_ptr' - whether it can be an
3170 LA argument, `is_base_ptr' - whether the resulting base is a well-known
3171 base register (stack/frame pointer, etc), `is_pool_ptr` - whether it is
3172 considered a literal pool pointer for purposes of avoiding two different
3173 literal pool pointers per insn during or after reload (`B' constraint). */
3174 static bool
3175 s390_decompose_constant_pool_ref (rtx *ref, rtx *disp, bool *is_ptr,
3176 bool *is_base_ptr, bool *is_pool_ptr)
3177 {
3178 if (!*ref)
3179 return true;
3180
3181 if (GET_CODE (*ref) == UNSPEC)
3182 switch (XINT (*ref, 1))
3183 {
3184 case UNSPEC_LTREF:
3185 if (!*disp)
3186 *disp = gen_rtx_UNSPEC (Pmode,
3187 gen_rtvec (1, XVECEXP (*ref, 0, 0)),
3188 UNSPEC_LTREL_OFFSET);
3189 else
3190 return false;
3191
3192 *ref = XVECEXP (*ref, 0, 1);
3193 break;
3194
3195 default:
3196 return false;
3197 }
3198
3199 if (!REG_P (*ref) || GET_MODE (*ref) != Pmode)
3200 return false;
3201
3202 if (REGNO (*ref) == STACK_POINTER_REGNUM
3203 || REGNO (*ref) == FRAME_POINTER_REGNUM
3204 || ((reload_completed || reload_in_progress)
3205 && frame_pointer_needed
3206 && REGNO (*ref) == HARD_FRAME_POINTER_REGNUM)
3207 || REGNO (*ref) == ARG_POINTER_REGNUM
3208 || (flag_pic
3209 && REGNO (*ref) == PIC_OFFSET_TABLE_REGNUM))
3210 *is_ptr = *is_base_ptr = true;
3211
3212 if ((reload_completed || reload_in_progress)
3213 && *ref == cfun->machine->base_reg)
3214 *is_ptr = *is_base_ptr = *is_pool_ptr = true;
3215
3216 return true;
3217 }
3218
3219 /* Decompose a RTL expression ADDR for a memory address into
3220 its components, returned in OUT.
3221
3222 Returns false if ADDR is not a valid memory address, true
3223 otherwise. If OUT is NULL, don't return the components,
3224 but check for validity only.
3225
3226 Note: Only addresses in canonical form are recognized.
3227 LEGITIMIZE_ADDRESS should convert non-canonical forms to the
3228 canonical form so that they will be recognized. */
3229
3230 static int
3231 s390_decompose_address (rtx addr, struct s390_address *out)
3232 {
3233 HOST_WIDE_INT offset = 0;
3234 rtx base = NULL_RTX;
3235 rtx indx = NULL_RTX;
3236 rtx disp = NULL_RTX;
3237 rtx orig_disp;
3238 bool pointer = false;
3239 bool base_ptr = false;
3240 bool indx_ptr = false;
3241 bool literal_pool = false;
3242
3243 /* We may need to substitute the literal pool base register into the address
3244 below. However, at this point we do not know which register is going to
3245 be used as base, so we substitute the arg pointer register. This is going
3246 to be treated as holding a pointer below -- it shouldn't be used for any
3247 other purpose. */
3248 rtx fake_pool_base = gen_rtx_REG (Pmode, ARG_POINTER_REGNUM);
3249
3250 /* Decompose address into base + index + displacement. */
3251
3252 if (GET_CODE (addr) == REG || GET_CODE (addr) == UNSPEC)
3253 base = addr;
3254
3255 else if (GET_CODE (addr) == PLUS)
3256 {
3257 rtx op0 = XEXP (addr, 0);
3258 rtx op1 = XEXP (addr, 1);
3259 enum rtx_code code0 = GET_CODE (op0);
3260 enum rtx_code code1 = GET_CODE (op1);
3261
3262 if (code0 == REG || code0 == UNSPEC)
3263 {
3264 if (code1 == REG || code1 == UNSPEC)
3265 {
3266 indx = op0; /* index + base */
3267 base = op1;
3268 }
3269
3270 else
3271 {
3272 base = op0; /* base + displacement */
3273 disp = op1;
3274 }
3275 }
3276
3277 else if (code0 == PLUS)
3278 {
3279 indx = XEXP (op0, 0); /* index + base + disp */
3280 base = XEXP (op0, 1);
3281 disp = op1;
3282 }
3283
3284 else
3285 {
3286 return false;
3287 }
3288 }
3289
3290 else
3291 disp = addr; /* displacement */
3292
3293 /* Extract integer part of displacement. */
3294 orig_disp = disp;
3295 if (disp)
3296 {
3297 if (GET_CODE (disp) == CONST_INT)
3298 {
3299 offset = INTVAL (disp);
3300 disp = NULL_RTX;
3301 }
3302 else if (GET_CODE (disp) == CONST
3303 && GET_CODE (XEXP (disp, 0)) == PLUS
3304 && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
3305 {
3306 offset = INTVAL (XEXP (XEXP (disp, 0), 1));
3307 disp = XEXP (XEXP (disp, 0), 0);
3308 }
3309 }
3310
3311 /* Strip off CONST here to avoid special case tests later. */
3312 if (disp && GET_CODE (disp) == CONST)
3313 disp = XEXP (disp, 0);
3314
3315 /* We can convert literal pool addresses to
3316 displacements by basing them off the base register. */
3317 if (disp && GET_CODE (disp) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (disp))
3318 {
3319 if (base || indx)
3320 return false;
3321
3322 base = fake_pool_base, literal_pool = true;
3323
3324 /* Mark up the displacement. */
3325 disp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, disp),
3326 UNSPEC_LTREL_OFFSET);
3327 }
3328
3329 /* Validate base register. */
3330 if (!s390_decompose_constant_pool_ref (&base, &disp, &pointer, &base_ptr,
3331 &literal_pool))
3332 return false;
3333
3334 /* Validate index register. */
3335 if (!s390_decompose_constant_pool_ref (&indx, &disp, &pointer, &indx_ptr,
3336 &literal_pool))
3337 return false;
3338
3339 /* Prefer to use pointer as base, not index. */
3340 if (base && indx && !base_ptr
3341 && (indx_ptr || (!REG_POINTER (base) && REG_POINTER (indx))))
3342 {
3343 rtx tmp = base;
3344 base = indx;
3345 indx = tmp;
3346 }
3347
3348 /* Validate displacement. */
3349 if (!disp)
3350 {
3351 /* If virtual registers are involved, the displacement will change later
3352 anyway as the virtual registers get eliminated. This could make a
3353 valid displacement invalid, but it is more likely to make an invalid
3354 displacement valid, because we sometimes access the register save area
3355 via negative offsets to one of those registers.
3356 Thus we don't check the displacement for validity here. If after
3357 elimination the displacement turns out to be invalid after all,
3358 this is fixed up by reload in any case. */
3359 /* LRA maintains always displacements up to date and we need to
3360 know the displacement is right during all LRA not only at the
3361 final elimination. */
3362 if (lra_in_progress
3363 || (base != arg_pointer_rtx
3364 && indx != arg_pointer_rtx
3365 && base != return_address_pointer_rtx
3366 && indx != return_address_pointer_rtx
3367 && base != frame_pointer_rtx
3368 && indx != frame_pointer_rtx
3369 && base != virtual_stack_vars_rtx
3370 && indx != virtual_stack_vars_rtx))
3371 if (!DISP_IN_RANGE (offset))
3372 return false;
3373 }
3374 else
3375 {
3376 /* All the special cases are pointers. */
3377 pointer = true;
3378
3379 /* In the small-PIC case, the linker converts @GOT
3380 and @GOTNTPOFF offsets to possible displacements. */
3381 if (GET_CODE (disp) == UNSPEC
3382 && (XINT (disp, 1) == UNSPEC_GOT
3383 || XINT (disp, 1) == UNSPEC_GOTNTPOFF)
3384 && flag_pic == 1)
3385 {
3386 ;
3387 }
3388
3389 /* Accept pool label offsets. */
3390 else if (GET_CODE (disp) == UNSPEC
3391 && XINT (disp, 1) == UNSPEC_POOL_OFFSET)
3392 ;
3393
3394 /* Accept literal pool references. */
3395 else if (GET_CODE (disp) == UNSPEC
3396 && XINT (disp, 1) == UNSPEC_LTREL_OFFSET)
3397 {
3398 /* In case CSE pulled a non literal pool reference out of
3399 the pool we have to reject the address. This is
3400 especially important when loading the GOT pointer on non
3401 zarch CPUs. In this case the literal pool contains an lt
3402 relative offset to the _GLOBAL_OFFSET_TABLE_ label which
3403 will most likely exceed the displacement. */
3404 if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
3405 || !CONSTANT_POOL_ADDRESS_P (XVECEXP (disp, 0, 0)))
3406 return false;
3407
3408 orig_disp = gen_rtx_CONST (Pmode, disp);
3409 if (offset)
3410 {
3411 /* If we have an offset, make sure it does not
3412 exceed the size of the constant pool entry.
3413 Otherwise we might generate an out-of-range
3414 displacement for the base register form. */
3415 rtx sym = XVECEXP (disp, 0, 0);
3416 if (offset >= GET_MODE_SIZE (get_pool_mode (sym)))
3417 return false;
3418
3419 orig_disp = plus_constant (Pmode, orig_disp, offset);
3420 }
3421 }
3422
3423 else
3424 return false;
3425 }
3426
3427 if (!base && !indx)
3428 pointer = true;
3429
3430 if (out)
3431 {
3432 out->base = base;
3433 out->indx = indx;
3434 out->disp = orig_disp;
3435 out->pointer = pointer;
3436 out->literal_pool = literal_pool;
3437 }
3438
3439 return true;
3440 }
3441
3442 /* Decompose a RTL expression OP for an address style operand into its
3443 components, and return the base register in BASE and the offset in
3444 OFFSET. While OP looks like an address it is never supposed to be
3445 used as such.
3446
3447 Return true if OP is a valid address operand, false if not. */
3448
3449 bool
3450 s390_decompose_addrstyle_without_index (rtx op, rtx *base,
3451 HOST_WIDE_INT *offset)
3452 {
3453 rtx off = NULL_RTX;
3454
3455 /* We can have an integer constant, an address register,
3456 or a sum of the two. */
3457 if (CONST_SCALAR_INT_P (op))
3458 {
3459 off = op;
3460 op = NULL_RTX;
3461 }
3462 if (op && GET_CODE (op) == PLUS && CONST_SCALAR_INT_P (XEXP (op, 1)))
3463 {
3464 off = XEXP (op, 1);
3465 op = XEXP (op, 0);
3466 }
3467 while (op && GET_CODE (op) == SUBREG)
3468 op = SUBREG_REG (op);
3469
3470 if (op && (!REG_P (op)
3471 || (reload_completed
3472 && !REGNO_OK_FOR_BASE_P (REGNO (op)))))
3473 return false;
3474
3475 if (offset)
3476 {
3477 if (off == NULL_RTX)
3478 *offset = 0;
3479 else if (CONST_INT_P (off))
3480 *offset = INTVAL (off);
3481 else if (CONST_WIDE_INT_P (off))
3482 /* The offset will anyway be cut down to 12 bits so take just
3483 the lowest order chunk of the wide int. */
3484 *offset = CONST_WIDE_INT_ELT (off, 0);
3485 else
3486 gcc_unreachable ();
3487 }
3488 if (base)
3489 *base = op;
3490
3491 return true;
3492 }
3493
3494 /* Check that OP is a valid shift count operand.
3495 It should be of the following structure:
3496 (subreg (and (plus (reg imm_op)) 2^k-1) 7)
3497 where subreg, and and plus are optional.
3498
3499 If IMPLICIT_MASK is > 0 and OP contains and
3500 (AND ... immediate)
3501 it is checked whether IMPLICIT_MASK and the immediate match.
3502 Otherwise, no checking is performed.
3503 */
3504 bool
3505 s390_valid_shift_count (rtx op, HOST_WIDE_INT implicit_mask)
3506 {
3507 /* Strip subreg. */
3508 while (GET_CODE (op) == SUBREG && subreg_lowpart_p (op))
3509 op = XEXP (op, 0);
3510
3511 /* Check for an and with proper constant. */
3512 if (GET_CODE (op) == AND)
3513 {
3514 rtx op1 = XEXP (op, 0);
3515 rtx imm = XEXP (op, 1);
3516
3517 if (GET_CODE (op1) == SUBREG && subreg_lowpart_p (op1))
3518 op1 = XEXP (op1, 0);
3519
3520 if (!(register_operand (op1, GET_MODE (op1)) || GET_CODE (op1) == PLUS))
3521 return false;
3522
3523 /* Accept only CONST_INT as immediates, i.e., reject shift count operands
3524 which do not trivially fit the scheme of address operands. Especially
3525 since strip_address_mutations() expects expressions of the form
3526 (and ... (const_int ...)) and fails for
3527 (and ... (const_wide_int ...)). */
3528 if (!const_int_operand (imm, GET_MODE (imm)))
3529 return false;
3530
3531 HOST_WIDE_INT val = INTVAL (imm);
3532 if (implicit_mask > 0
3533 && (val & implicit_mask) != implicit_mask)
3534 return false;
3535
3536 op = op1;
3537 }
3538
3539 /* Check the rest. */
3540 return s390_decompose_addrstyle_without_index (op, NULL, NULL);
3541 }
3542
3543 /* Return true if CODE is a valid address without index. */
3544
3545 bool
3546 s390_legitimate_address_without_index_p (rtx op)
3547 {
3548 struct s390_address addr;
3549
3550 if (!s390_decompose_address (XEXP (op, 0), &addr))
3551 return false;
3552 if (addr.indx)
3553 return false;
3554
3555 return true;
3556 }
3557
3558
3559 /* Return TRUE if ADDR is an operand valid for a load/store relative
3560 instruction. Be aware that the alignment of the operand needs to
3561 be checked separately.
3562 Valid addresses are single references or a sum of a reference and a
3563 constant integer. Return these parts in SYMREF and ADDEND. You can
3564 pass NULL in REF and/or ADDEND if you are not interested in these
3565 values. */
3566
3567 static bool
3568 s390_loadrelative_operand_p (rtx addr, rtx *symref, HOST_WIDE_INT *addend)
3569 {
3570 HOST_WIDE_INT tmpaddend = 0;
3571
3572 if (GET_CODE (addr) == CONST)
3573 addr = XEXP (addr, 0);
3574
3575 if (GET_CODE (addr) == PLUS)
3576 {
3577 if (!CONST_INT_P (XEXP (addr, 1)))
3578 return false;
3579
3580 tmpaddend = INTVAL (XEXP (addr, 1));
3581 addr = XEXP (addr, 0);
3582 }
3583
3584 if (GET_CODE (addr) == SYMBOL_REF
3585 || (GET_CODE (addr) == UNSPEC
3586 && (XINT (addr, 1) == UNSPEC_GOTENT
3587 || XINT (addr, 1) == UNSPEC_PLT31)))
3588 {
3589 if (symref)
3590 *symref = addr;
3591 if (addend)
3592 *addend = tmpaddend;
3593
3594 return true;
3595 }
3596 return false;
3597 }
3598
3599 /* Return true if the address in OP is valid for constraint letter C
3600 if wrapped in a MEM rtx. Set LIT_POOL_OK to true if it literal
3601 pool MEMs should be accepted. Only the Q, R, S, T constraint
3602 letters are allowed for C. */
3603
3604 static int
3605 s390_check_qrst_address (char c, rtx op, bool lit_pool_ok)
3606 {
3607 rtx symref;
3608 struct s390_address addr;
3609 bool decomposed = false;
3610
3611 if (!address_operand (op, GET_MODE (op)))
3612 return 0;
3613
3614 /* This check makes sure that no symbolic address (except literal
3615 pool references) are accepted by the R or T constraints. */
3616 if (s390_loadrelative_operand_p (op, &symref, NULL)
3617 && (!lit_pool_ok
3618 || !SYMBOL_REF_P (symref)
3619 || !CONSTANT_POOL_ADDRESS_P (symref)))
3620 return 0;
3621
3622 /* Ensure literal pool references are only accepted if LIT_POOL_OK. */
3623 if (!lit_pool_ok)
3624 {
3625 if (!s390_decompose_address (op, &addr))
3626 return 0;
3627 if (addr.literal_pool)
3628 return 0;
3629 decomposed = true;
3630 }
3631
3632 /* With reload, we sometimes get intermediate address forms that are
3633 actually invalid as-is, but we need to accept them in the most
3634 generic cases below ('R' or 'T'), since reload will in fact fix
3635 them up. LRA behaves differently here; we never see such forms,
3636 but on the other hand, we need to strictly reject every invalid
3637 address form. After both reload and LRA invalid address forms
3638 must be rejected, because nothing will fix them up later. Perform
3639 this check right up front. */
3640 if (lra_in_progress || reload_completed)
3641 {
3642 if (!decomposed && !s390_decompose_address (op, &addr))
3643 return 0;
3644 decomposed = true;
3645 }
3646
3647 switch (c)
3648 {
3649 case 'Q': /* no index short displacement */
3650 if (!decomposed && !s390_decompose_address (op, &addr))
3651 return 0;
3652 if (addr.indx)
3653 return 0;
3654 if (!s390_short_displacement (addr.disp))
3655 return 0;
3656 break;
3657
3658 case 'R': /* with index short displacement */
3659 if (TARGET_LONG_DISPLACEMENT)
3660 {
3661 if (!decomposed && !s390_decompose_address (op, &addr))
3662 return 0;
3663 if (!s390_short_displacement (addr.disp))
3664 return 0;
3665 }
3666 /* Any invalid address here will be fixed up by reload,
3667 so accept it for the most generic constraint. */
3668 break;
3669
3670 case 'S': /* no index long displacement */
3671 if (!decomposed && !s390_decompose_address (op, &addr))
3672 return 0;
3673 if (addr.indx)
3674 return 0;
3675 break;
3676
3677 case 'T': /* with index long displacement */
3678 /* Any invalid address here will be fixed up by reload,
3679 so accept it for the most generic constraint. */
3680 break;
3681
3682 default:
3683 return 0;
3684 }
3685 return 1;
3686 }
3687
3688
3689 /* Evaluates constraint strings described by the regular expression
3690 ([A|B|Z](Q|R|S|T))|Y and returns 1 if OP is a valid operand for
3691 the constraint given in STR, or 0 else. */
3692
3693 int
3694 s390_mem_constraint (const char *str, rtx op)
3695 {
3696 char c = str[0];
3697
3698 switch (c)
3699 {
3700 case 'A':
3701 /* Check for offsettable variants of memory constraints. */
3702 if (!MEM_P (op) || MEM_VOLATILE_P (op))
3703 return 0;
3704 if ((reload_completed || reload_in_progress)
3705 ? !offsettable_memref_p (op) : !offsettable_nonstrict_memref_p (op))
3706 return 0;
3707 /* offsettable_memref_p ensures only that any positive offset added to
3708 the address forms a valid general address. For AQ and AR constraints
3709 we also have to verify that the resulting displacement after adding
3710 any positive offset less than the size of the object being referenced
3711 is still valid. */
3712 if (str[1] == 'Q' || str[1] == 'R')
3713 {
3714 int o = GET_MODE_SIZE (GET_MODE (op)) - 1;
3715 rtx tmp = adjust_address (op, QImode, o);
3716 if (!s390_check_qrst_address (str[1], XEXP (tmp, 0), true))
3717 return 0;
3718 }
3719 return s390_check_qrst_address (str[1], XEXP (op, 0), true);
3720 case 'B':
3721 /* Check for non-literal-pool variants of memory constraints. */
3722 if (!MEM_P (op))
3723 return 0;
3724 return s390_check_qrst_address (str[1], XEXP (op, 0), false);
3725 case 'Q':
3726 case 'R':
3727 case 'S':
3728 case 'T':
3729 if (GET_CODE (op) != MEM)
3730 return 0;
3731 return s390_check_qrst_address (c, XEXP (op, 0), true);
3732 case 'Y':
3733 /* Simply check for the basic form of a shift count. Reload will
3734 take care of making sure we have a proper base register. */
3735 if (!s390_decompose_addrstyle_without_index (op, NULL, NULL))
3736 return 0;
3737 break;
3738 case 'Z':
3739 return s390_check_qrst_address (str[1], op, true);
3740 default:
3741 return 0;
3742 }
3743 return 1;
3744 }
3745
3746
3747 /* Evaluates constraint strings starting with letter O. Input
3748 parameter C is the second letter following the "O" in the constraint
3749 string. Returns 1 if VALUE meets the respective constraint and 0
3750 otherwise. */
3751
3752 int
3753 s390_O_constraint_str (const char c, HOST_WIDE_INT value)
3754 {
3755 if (!TARGET_EXTIMM)
3756 return 0;
3757
3758 switch (c)
3759 {
3760 case 's':
3761 return trunc_int_for_mode (value, SImode) == value;
3762
3763 case 'p':
3764 return value == 0
3765 || s390_single_part (GEN_INT (value), DImode, SImode, 0) == 1;
3766
3767 case 'n':
3768 return s390_single_part (GEN_INT (value - 1), DImode, SImode, -1) == 1;
3769
3770 default:
3771 gcc_unreachable ();
3772 }
3773 }
3774
3775
3776 /* Evaluates constraint strings starting with letter N. Parameter STR
3777 contains the letters following letter "N" in the constraint string.
3778 Returns true if VALUE matches the constraint. */
3779
3780 int
3781 s390_N_constraint_str (const char *str, HOST_WIDE_INT value)
3782 {
3783 machine_mode mode, part_mode;
3784 int def;
3785 int part, part_goal;
3786
3787
3788 if (str[0] == 'x')
3789 part_goal = -1;
3790 else
3791 part_goal = str[0] - '0';
3792
3793 switch (str[1])
3794 {
3795 case 'Q':
3796 part_mode = QImode;
3797 break;
3798 case 'H':
3799 part_mode = HImode;
3800 break;
3801 case 'S':
3802 part_mode = SImode;
3803 break;
3804 default:
3805 return 0;
3806 }
3807
3808 switch (str[2])
3809 {
3810 case 'H':
3811 mode = HImode;
3812 break;
3813 case 'S':
3814 mode = SImode;
3815 break;
3816 case 'D':
3817 mode = DImode;
3818 break;
3819 default:
3820 return 0;
3821 }
3822
3823 switch (str[3])
3824 {
3825 case '0':
3826 def = 0;
3827 break;
3828 case 'F':
3829 def = -1;
3830 break;
3831 default:
3832 return 0;
3833 }
3834
3835 if (GET_MODE_SIZE (mode) <= GET_MODE_SIZE (part_mode))
3836 return 0;
3837
3838 part = s390_single_part (GEN_INT (value), mode, part_mode, def);
3839 if (part < 0)
3840 return 0;
3841 if (part_goal != -1 && part_goal != part)
3842 return 0;
3843
3844 return 1;
3845 }
3846
3847
3848 /* Returns true if the input parameter VALUE is a float zero. */
3849
3850 int
3851 s390_float_const_zero_p (rtx value)
3852 {
3853 return (GET_MODE_CLASS (GET_MODE (value)) == MODE_FLOAT
3854 && value == CONST0_RTX (GET_MODE (value)));
3855 }
3856
3857 /* Implement TARGET_REGISTER_MOVE_COST. */
3858
3859 static int
3860 s390_register_move_cost (machine_mode mode,
3861 reg_class_t from, reg_class_t to)
3862 {
3863 /* On s390, copy between fprs and gprs is expensive. */
3864
3865 /* It becomes somewhat faster having ldgr/lgdr. */
3866 if (TARGET_Z10 && GET_MODE_SIZE (mode) == 8)
3867 {
3868 /* ldgr is single cycle. */
3869 if (reg_classes_intersect_p (from, GENERAL_REGS)
3870 && reg_classes_intersect_p (to, FP_REGS))
3871 return 1;
3872 /* lgdr needs 3 cycles. */
3873 if (reg_classes_intersect_p (to, GENERAL_REGS)
3874 && reg_classes_intersect_p (from, FP_REGS))
3875 return 3;
3876 }
3877
3878 /* Otherwise copying is done via memory. */
3879 if ((reg_classes_intersect_p (from, GENERAL_REGS)
3880 && reg_classes_intersect_p (to, FP_REGS))
3881 || (reg_classes_intersect_p (from, FP_REGS)
3882 && reg_classes_intersect_p (to, GENERAL_REGS)))
3883 return 10;
3884
3885 /* We usually do not want to copy via CC. */
3886 if (reg_classes_intersect_p (from, CC_REGS)
3887 || reg_classes_intersect_p (to, CC_REGS))
3888 return 5;
3889
3890 return 1;
3891 }
3892
3893 /* Implement TARGET_MEMORY_MOVE_COST. */
3894
3895 static int
3896 s390_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
3897 reg_class_t rclass ATTRIBUTE_UNUSED,
3898 bool in ATTRIBUTE_UNUSED)
3899 {
3900 return 2;
3901 }
3902
3903 /* Implement TARGET_INSN_COST. */
3904
3905 static int
3906 s390_insn_cost (rtx_insn *insn, bool speed)
3907 {
3908 /* For stores also consider the destination. Penalize if the address
3909 contains a SYMBOL_REF since this has to be fixed up by reload. */
3910 rtx pat = single_set (insn);
3911 if (pat && MEM_P (SET_DEST (pat)))
3912 {
3913 rtx mem = SET_DEST (pat);
3914 rtx addr = XEXP (mem, 0);
3915 int penalty = contains_symbol_ref_p (addr) ? COSTS_N_INSNS (1) : 0;
3916 int src_cost = set_src_cost (SET_SRC (pat), GET_MODE (mem), speed);
3917 src_cost = src_cost > 0 ? src_cost : COSTS_N_INSNS (1);
3918 return penalty + src_cost;
3919 }
3920 return pattern_cost (PATTERN (insn), speed);
3921 }
3922
3923 /* Compute a (partial) cost for rtx X. Return true if the complete
3924 cost has been computed, and false if subexpressions should be
3925 scanned. In either case, *TOTAL contains the cost result. The
3926 initial value of *TOTAL is the default value computed by
3927 rtx_cost. It may be left unmodified. OUTER_CODE contains the
3928 code of the superexpression of x. */
3929
3930 static bool
3931 s390_rtx_costs (rtx x, machine_mode mode, int outer_code,
3932 int opno ATTRIBUTE_UNUSED,
3933 int *total, bool speed ATTRIBUTE_UNUSED)
3934 {
3935 int code = GET_CODE (x);
3936 switch (code)
3937 {
3938 case CONST:
3939 case CONST_INT:
3940 case LABEL_REF:
3941 case SYMBOL_REF:
3942 case CONST_DOUBLE:
3943 case CONST_WIDE_INT:
3944 case MEM:
3945 *total = 0;
3946 return true;
3947 case SET: {
3948 rtx dst = SET_DEST (x);
3949 rtx src = SET_SRC (x);
3950
3951 switch (GET_CODE (src))
3952 {
3953 case IF_THEN_ELSE: {
3954 /* Without this a conditional move instruction would be
3955 accounted as 3 * COSTS_N_INSNS (set, if_then_else,
3956 comparison operator). That's a bit pessimistic. */
3957
3958 if (!TARGET_Z196)
3959 return false;
3960
3961 rtx cond = XEXP (src, 0);
3962 if (!CC_REG_P (XEXP (cond, 0)) || !CONST_INT_P (XEXP (cond, 1)))
3963 return false;
3964
3965 /* It is going to be a load/store on condition. Make it
3966 slightly more expensive than a normal load. */
3967 *total = COSTS_N_INSNS (1) + 2;
3968
3969 rtx then = XEXP (src, 1);
3970 rtx els = XEXP (src, 2);
3971
3972 /* It is a real IF-THEN-ELSE. An additional move will be
3973 needed to implement that. */
3974 if (!TARGET_Z15 && reload_completed && !rtx_equal_p (dst, then)
3975 && !rtx_equal_p (dst, els))
3976 *total += COSTS_N_INSNS (1) / 2;
3977
3978 /* A minor penalty for constants we cannot directly handle. */
3979 if ((CONST_INT_P (then) || CONST_INT_P (els))
3980 && (!TARGET_Z13 || MEM_P (dst)
3981 || (CONST_INT_P (then) && !satisfies_constraint_K (then))
3982 || (CONST_INT_P (els) && !satisfies_constraint_K (els))))
3983 *total += COSTS_N_INSNS (1) / 2;
3984
3985 /* A store on condition can only handle register src operands. */
3986 if (MEM_P (dst) && (!REG_P (then) || !REG_P (els)))
3987 *total += COSTS_N_INSNS (1) / 2;
3988
3989 return true;
3990 }
3991 default:
3992 break;
3993 }
3994
3995 switch (GET_CODE (dst))
3996 {
3997 case SUBREG:
3998 if (!REG_P (SUBREG_REG (dst)))
3999 *total += rtx_cost (SUBREG_REG (src), VOIDmode, SET, 0, speed);
4000 /* fallthrough */
4001 case REG:
4002 /* If this is a VR -> VR copy, count the number of
4003 registers. */
4004 if (VECTOR_MODE_P (GET_MODE (dst)) && REG_P (src))
4005 {
4006 int nregs = s390_hard_regno_nregs (VR0_REGNUM, GET_MODE (dst));
4007 *total = COSTS_N_INSNS (nregs);
4008 }
4009 /* Same for GPRs. */
4010 else if (REG_P (src))
4011 {
4012 int nregs
4013 = s390_hard_regno_nregs (GPR0_REGNUM, GET_MODE (dst));
4014 *total = COSTS_N_INSNS (nregs);
4015 }
4016 else
4017 /* Otherwise just cost the src. */
4018 *total += rtx_cost (src, mode, SET, 1, speed);
4019 return true;
4020 case MEM: {
4021 rtx address = XEXP (dst, 0);
4022 rtx tmp;
4023 HOST_WIDE_INT tmp2;
4024 if (s390_loadrelative_operand_p (address, &tmp, &tmp2))
4025 *total = COSTS_N_INSNS (1);
4026 else
4027 *total = s390_address_cost (address, mode, 0, speed);
4028 return true;
4029 }
4030 default:
4031 /* Not handled for now, assume default costs. */
4032 *total = COSTS_N_INSNS (1);
4033 return false;
4034 }
4035
4036 return false;
4037 }
4038 case IOR:
4039
4040 /* nnrk, nngrk */
4041 if (TARGET_Z15
4042 && (mode == SImode || mode == DImode)
4043 && GET_CODE (XEXP (x, 0)) == NOT
4044 && GET_CODE (XEXP (x, 1)) == NOT)
4045 {
4046 *total = COSTS_N_INSNS (1);
4047 if (!REG_P (XEXP (XEXP (x, 0), 0)))
4048 *total += 1;
4049 if (!REG_P (XEXP (XEXP (x, 1), 0)))
4050 *total += 1;
4051 return true;
4052 }
4053
4054 /* risbg */
4055 if (GET_CODE (XEXP (x, 0)) == AND
4056 && GET_CODE (XEXP (x, 1)) == ASHIFT
4057 && REG_P (XEXP (XEXP (x, 0), 0))
4058 && REG_P (XEXP (XEXP (x, 1), 0))
4059 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4060 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
4061 && (UINTVAL (XEXP (XEXP (x, 0), 1)) ==
4062 (HOST_WIDE_INT_1U << UINTVAL (XEXP (XEXP (x, 1), 1))) - 1))
4063 {
4064 *total = COSTS_N_INSNS (2);
4065 return true;
4066 }
4067
4068 /* ~AND on a 128 bit mode. This can be done using a vector
4069 instruction. */
4070 if (TARGET_VXE
4071 && GET_CODE (XEXP (x, 0)) == NOT
4072 && GET_CODE (XEXP (x, 1)) == NOT
4073 && REG_P (XEXP (XEXP (x, 0), 0))
4074 && REG_P (XEXP (XEXP (x, 1), 0))
4075 && GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 0), 0))) == 16
4076 && s390_hard_regno_mode_ok (VR0_REGNUM,
4077 GET_MODE (XEXP (XEXP (x, 0), 0))))
4078 {
4079 *total = COSTS_N_INSNS (1);
4080 return true;
4081 }
4082
4083 *total = COSTS_N_INSNS (1);
4084 return false;
4085
4086 case AND:
4087 /* nork, nogrk */
4088 if (TARGET_Z15
4089 && (mode == SImode || mode == DImode)
4090 && GET_CODE (XEXP (x, 0)) == NOT
4091 && GET_CODE (XEXP (x, 1)) == NOT)
4092 {
4093 *total = COSTS_N_INSNS (1);
4094 if (!REG_P (XEXP (XEXP (x, 0), 0)))
4095 *total += 1;
4096 if (!REG_P (XEXP (XEXP (x, 1), 0)))
4097 *total += 1;
4098 return true;
4099 }
4100 /* fallthrough */
4101 case ASHIFT:
4102 case ASHIFTRT:
4103 case LSHIFTRT:
4104 case ROTATE:
4105 case ROTATERT:
4106 case XOR:
4107 case NEG:
4108 case NOT:
4109 case PLUS:
4110 case MINUS:
4111 *total = COSTS_N_INSNS (1);
4112 return false;
4113
4114 case MULT:
4115 switch (mode)
4116 {
4117 case E_SImode:
4118 {
4119 rtx left = XEXP (x, 0);
4120 rtx right = XEXP (x, 1);
4121 if (GET_CODE (right) == CONST_INT
4122 && CONST_OK_FOR_K (INTVAL (right)))
4123 *total = s390_cost->mhi;
4124 else if (GET_CODE (left) == SIGN_EXTEND)
4125 *total = s390_cost->mh;
4126 else
4127 *total = s390_cost->ms; /* msr, ms, msy */
4128 break;
4129 }
4130 case E_DImode:
4131 {
4132 rtx left = XEXP (x, 0);
4133 rtx right = XEXP (x, 1);
4134 if (TARGET_ZARCH)
4135 {
4136 if (GET_CODE (right) == CONST_INT
4137 && CONST_OK_FOR_K (INTVAL (right)))
4138 *total = s390_cost->mghi;
4139 else if (GET_CODE (left) == SIGN_EXTEND)
4140 *total = s390_cost->msgf;
4141 else
4142 *total = s390_cost->msg; /* msgr, msg */
4143 }
4144 else /* TARGET_31BIT */
4145 {
4146 if (GET_CODE (left) == SIGN_EXTEND
4147 && GET_CODE (right) == SIGN_EXTEND)
4148 /* mulsidi case: mr, m */
4149 *total = s390_cost->m;
4150 else if (GET_CODE (left) == ZERO_EXTEND
4151 && GET_CODE (right) == ZERO_EXTEND)
4152 /* umulsidi case: ml, mlr */
4153 *total = s390_cost->ml;
4154 else
4155 /* Complex calculation is required. */
4156 *total = COSTS_N_INSNS (40);
4157 }
4158 break;
4159 }
4160 case E_SFmode:
4161 case E_DFmode:
4162 *total = s390_cost->mult_df;
4163 break;
4164 case E_TFmode:
4165 *total = s390_cost->mxbr;
4166 break;
4167 default:
4168 return false;
4169 }
4170 return false;
4171
4172 case FMA:
4173 switch (mode)
4174 {
4175 case E_DFmode:
4176 *total = s390_cost->madbr;
4177 break;
4178 case E_SFmode:
4179 *total = s390_cost->maebr;
4180 break;
4181 default:
4182 return false;
4183 }
4184 /* Negate in the third argument is free: FMSUB. */
4185 if (GET_CODE (XEXP (x, 2)) == NEG)
4186 {
4187 *total += (rtx_cost (XEXP (x, 0), mode, FMA, 0, speed)
4188 + rtx_cost (XEXP (x, 1), mode, FMA, 1, speed)
4189 + rtx_cost (XEXP (XEXP (x, 2), 0), mode, FMA, 2, speed));
4190 return true;
4191 }
4192 return false;
4193
4194 case UDIV:
4195 case UMOD:
4196 if (mode == TImode) /* 128 bit division */
4197 *total = s390_cost->dlgr;
4198 else if (mode == DImode)
4199 {
4200 rtx right = XEXP (x, 1);
4201 if (GET_CODE (right) == ZERO_EXTEND) /* 64 by 32 bit division */
4202 *total = s390_cost->dlr;
4203 else /* 64 by 64 bit division */
4204 *total = s390_cost->dlgr;
4205 }
4206 else if (mode == SImode) /* 32 bit division */
4207 *total = s390_cost->dlr;
4208 return false;
4209
4210 case DIV:
4211 case MOD:
4212 if (mode == DImode)
4213 {
4214 rtx right = XEXP (x, 1);
4215 if (GET_CODE (right) == ZERO_EXTEND) /* 64 by 32 bit division */
4216 if (TARGET_ZARCH)
4217 *total = s390_cost->dsgfr;
4218 else
4219 *total = s390_cost->dr;
4220 else /* 64 by 64 bit division */
4221 *total = s390_cost->dsgr;
4222 }
4223 else if (mode == SImode) /* 32 bit division */
4224 *total = s390_cost->dlr;
4225 else if (mode == SFmode)
4226 {
4227 *total = s390_cost->debr;
4228 }
4229 else if (mode == DFmode)
4230 {
4231 *total = s390_cost->ddbr;
4232 }
4233 else if (mode == TFmode)
4234 {
4235 *total = s390_cost->dxbr;
4236 }
4237 return false;
4238
4239 case SQRT:
4240 if (mode == SFmode)
4241 *total = s390_cost->sqebr;
4242 else if (mode == DFmode)
4243 *total = s390_cost->sqdbr;
4244 else /* TFmode */
4245 *total = s390_cost->sqxbr;
4246 return false;
4247
4248 case SIGN_EXTEND:
4249 case ZERO_EXTEND:
4250 if (outer_code == MULT || outer_code == DIV || outer_code == MOD
4251 || outer_code == PLUS || outer_code == MINUS
4252 || outer_code == COMPARE)
4253 *total = 0;
4254 return false;
4255
4256 case COMPARE:
4257 *total = COSTS_N_INSNS (1);
4258
4259 /* nxrk, nxgrk ~(a^b)==0 */
4260 if (TARGET_Z15
4261 && GET_CODE (XEXP (x, 0)) == NOT
4262 && XEXP (x, 1) == const0_rtx
4263 && GET_CODE (XEXP (XEXP (x, 0), 0)) == XOR
4264 && (GET_MODE (XEXP (x, 0)) == SImode || GET_MODE (XEXP (x, 0)) == DImode)
4265 && mode == CCZmode)
4266 {
4267 if (!REG_P (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4268 *total += 1;
4269 if (!REG_P (XEXP (XEXP (XEXP (x, 0), 0), 1)))
4270 *total += 1;
4271 return true;
4272 }
4273
4274 /* nnrk, nngrk, nork, nogrk */
4275 if (TARGET_Z15
4276 && (GET_CODE (XEXP (x, 0)) == AND || GET_CODE (XEXP (x, 0)) == IOR)
4277 && XEXP (x, 1) == const0_rtx
4278 && (GET_MODE (XEXP (x, 0)) == SImode || GET_MODE (XEXP (x, 0)) == DImode)
4279 && GET_CODE (XEXP (XEXP (x, 0), 0)) == NOT
4280 && GET_CODE (XEXP (XEXP (x, 0), 1)) == NOT
4281 && mode == CCZmode)
4282 {
4283 if (!REG_P (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4284 *total += 1;
4285 if (!REG_P (XEXP (XEXP (XEXP (x, 0), 1), 0)))
4286 *total += 1;
4287 return true;
4288 }
4289
4290 if (GET_CODE (XEXP (x, 0)) == AND
4291 && GET_CODE (XEXP (x, 1)) == CONST_INT
4292 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
4293 {
4294 rtx op0 = XEXP (XEXP (x, 0), 0);
4295 rtx op1 = XEXP (XEXP (x, 0), 1);
4296 rtx op2 = XEXP (x, 1);
4297
4298 if (memory_operand (op0, GET_MODE (op0))
4299 && s390_tm_ccmode (op1, op2, 0) != VOIDmode)
4300 return true;
4301 if (register_operand (op0, GET_MODE (op0))
4302 && s390_tm_ccmode (op1, op2, 1) != VOIDmode)
4303 return true;
4304 }
4305 return false;
4306
4307 default:
4308 return false;
4309 }
4310 }
4311
4312 /* Return the cost of an address rtx ADDR. */
4313
4314 static int
4315 s390_address_cost (rtx addr, machine_mode mode ATTRIBUTE_UNUSED,
4316 addr_space_t as ATTRIBUTE_UNUSED,
4317 bool speed ATTRIBUTE_UNUSED)
4318 {
4319 struct s390_address ad;
4320 if (!s390_decompose_address (addr, &ad))
4321 return 1000;
4322
4323 return ad.indx? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (1);
4324 }
4325
4326 /* Implement targetm.vectorize.builtin_vectorization_cost. */
4327 static int
4328 s390_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
4329 tree vectype,
4330 int misalign ATTRIBUTE_UNUSED)
4331 {
4332 switch (type_of_cost)
4333 {
4334 case scalar_stmt:
4335 case scalar_load:
4336 case scalar_store:
4337 case vector_stmt:
4338 case vector_load:
4339 case vector_store:
4340 case vector_gather_load:
4341 case vector_scatter_store:
4342 case vec_to_scalar:
4343 case scalar_to_vec:
4344 case cond_branch_not_taken:
4345 case vec_perm:
4346 case vec_promote_demote:
4347 case unaligned_load:
4348 case unaligned_store:
4349 return 1;
4350
4351 case cond_branch_taken:
4352 return 3;
4353
4354 case vec_construct:
4355 return TYPE_VECTOR_SUBPARTS (vectype) - 1;
4356
4357 default:
4358 gcc_unreachable ();
4359 }
4360 }
4361
4362 /* If OP is a SYMBOL_REF of a thread-local symbol, return its TLS mode,
4363 otherwise return 0. */
4364
4365 int
4366 tls_symbolic_operand (rtx op)
4367 {
4368 if (GET_CODE (op) != SYMBOL_REF)
4369 return 0;
4370 return SYMBOL_REF_TLS_MODEL (op);
4371 }
4372
4373 /* Split DImode access register reference REG (on 64-bit) into its constituent
4374 low and high parts, and store them into LO and HI. Note that gen_lowpart/
4375 gen_highpart cannot be used as they assume all registers are word-sized,
4376 while our access registers have only half that size. */
4377
4378 void
4379 s390_split_access_reg (rtx reg, rtx *lo, rtx *hi)
4380 {
4381 gcc_assert (TARGET_64BIT);
4382 gcc_assert (ACCESS_REG_P (reg));
4383 gcc_assert (GET_MODE (reg) == DImode);
4384 gcc_assert (!(REGNO (reg) & 1));
4385
4386 *lo = gen_rtx_REG (SImode, REGNO (reg) + 1);
4387 *hi = gen_rtx_REG (SImode, REGNO (reg));
4388 }
4389
4390 /* Return true if OP contains a symbol reference */
4391
4392 bool
4393 symbolic_reference_mentioned_p (rtx op)
4394 {
4395 const char *fmt;
4396 int i;
4397
4398 if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
4399 return 1;
4400
4401 fmt = GET_RTX_FORMAT (GET_CODE (op));
4402 for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
4403 {
4404 if (fmt[i] == 'E')
4405 {
4406 int j;
4407
4408 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
4409 if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
4410 return 1;
4411 }
4412
4413 else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
4414 return 1;
4415 }
4416
4417 return 0;
4418 }
4419
4420 /* Return true if OP contains a reference to a thread-local symbol. */
4421
4422 bool
4423 tls_symbolic_reference_mentioned_p (rtx op)
4424 {
4425 const char *fmt;
4426 int i;
4427
4428 if (GET_CODE (op) == SYMBOL_REF)
4429 return tls_symbolic_operand (op);
4430
4431 fmt = GET_RTX_FORMAT (GET_CODE (op));
4432 for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
4433 {
4434 if (fmt[i] == 'E')
4435 {
4436 int j;
4437
4438 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
4439 if (tls_symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
4440 return true;
4441 }
4442
4443 else if (fmt[i] == 'e' && tls_symbolic_reference_mentioned_p (XEXP (op, i)))
4444 return true;
4445 }
4446
4447 return false;
4448 }
4449
4450
4451 /* Return true if OP is a legitimate general operand when
4452 generating PIC code. It is given that flag_pic is on
4453 and that OP satisfies CONSTANT_P. */
4454
4455 int
4456 legitimate_pic_operand_p (rtx op)
4457 {
4458 /* Accept all non-symbolic constants. */
4459 if (!SYMBOLIC_CONST (op))
4460 return 1;
4461
4462 /* Accept addresses that can be expressed relative to (pc). */
4463 if (larl_operand (op, VOIDmode))
4464 return 1;
4465
4466 /* Reject everything else; must be handled
4467 via emit_symbolic_move. */
4468 return 0;
4469 }
4470
4471 /* Returns true if the constant value OP is a legitimate general operand.
4472 It is given that OP satisfies CONSTANT_P. */
4473
4474 static bool
4475 s390_legitimate_constant_p (machine_mode mode, rtx op)
4476 {
4477 if (TARGET_VX && GET_CODE (op) == CONST_VECTOR)
4478 {
4479 if (!satisfies_constraint_j00 (op)
4480 && !satisfies_constraint_jm1 (op)
4481 && !satisfies_constraint_jzz (op)
4482 && !satisfies_constraint_jxx (op)
4483 && !satisfies_constraint_jyy (op))
4484 return 0;
4485 }
4486
4487 /* Accept all non-symbolic constants. */
4488 if (!SYMBOLIC_CONST (op))
4489 return 1;
4490
4491 /* Accept immediate LARL operands. */
4492 if (larl_operand (op, mode))
4493 return 1;
4494
4495 /* Thread-local symbols are never legal constants. This is
4496 so that emit_call knows that computing such addresses
4497 might require a function call. */
4498 if (TLS_SYMBOLIC_CONST (op))
4499 return 0;
4500
4501 /* In the PIC case, symbolic constants must *not* be
4502 forced into the literal pool. We accept them here,
4503 so that they will be handled by emit_symbolic_move. */
4504 if (flag_pic)
4505 return 1;
4506
4507 /* All remaining non-PIC symbolic constants are
4508 forced into the literal pool. */
4509 return 0;
4510 }
4511
4512 /* Determine if it's legal to put X into the constant pool. This
4513 is not possible if X contains the address of a symbol that is
4514 not constant (TLS) or not known at final link time (PIC). */
4515
4516 static bool
4517 s390_cannot_force_const_mem (machine_mode mode, rtx x)
4518 {
4519 switch (GET_CODE (x))
4520 {
4521 case CONST_INT:
4522 case CONST_DOUBLE:
4523 case CONST_WIDE_INT:
4524 case CONST_VECTOR:
4525 /* Accept all non-symbolic constants. */
4526 return false;
4527
4528 case NEG:
4529 /* Accept an unary '-' only on scalar numeric constants. */
4530 switch (GET_CODE (XEXP (x, 0)))
4531 {
4532 case CONST_INT:
4533 case CONST_DOUBLE:
4534 case CONST_WIDE_INT:
4535 return false;
4536 default:
4537 return true;
4538 }
4539
4540 case LABEL_REF:
4541 /* Labels are OK iff we are non-PIC. */
4542 return flag_pic != 0;
4543
4544 case SYMBOL_REF:
4545 /* 'Naked' TLS symbol references are never OK,
4546 non-TLS symbols are OK iff we are non-PIC. */
4547 if (tls_symbolic_operand (x))
4548 return true;
4549 else
4550 return flag_pic != 0;
4551
4552 case CONST:
4553 return s390_cannot_force_const_mem (mode, XEXP (x, 0));
4554 case PLUS:
4555 case MINUS:
4556 return s390_cannot_force_const_mem (mode, XEXP (x, 0))
4557 || s390_cannot_force_const_mem (mode, XEXP (x, 1));
4558
4559 case UNSPEC:
4560 switch (XINT (x, 1))
4561 {
4562 /* Only lt-relative or GOT-relative UNSPECs are OK. */
4563 case UNSPEC_LTREL_OFFSET:
4564 case UNSPEC_GOT:
4565 case UNSPEC_GOTOFF:
4566 case UNSPEC_PLTOFF:
4567 case UNSPEC_TLSGD:
4568 case UNSPEC_TLSLDM:
4569 case UNSPEC_NTPOFF:
4570 case UNSPEC_DTPOFF:
4571 case UNSPEC_GOTNTPOFF:
4572 case UNSPEC_INDNTPOFF:
4573 return false;
4574
4575 /* If the literal pool shares the code section, be put
4576 execute template placeholders into the pool as well. */
4577 case UNSPEC_INSN:
4578 default:
4579 return true;
4580 }
4581 break;
4582
4583 default:
4584 gcc_unreachable ();
4585 }
4586 }
4587
4588 /* Returns true if the constant value OP is a legitimate general
4589 operand during and after reload. The difference to
4590 legitimate_constant_p is that this function will not accept
4591 a constant that would need to be forced to the literal pool
4592 before it can be used as operand.
4593 This function accepts all constants which can be loaded directly
4594 into a GPR. */
4595
4596 bool
4597 legitimate_reload_constant_p (rtx op)
4598 {
4599 /* Accept la(y) operands. */
4600 if (GET_CODE (op) == CONST_INT
4601 && DISP_IN_RANGE (INTVAL (op)))
4602 return true;
4603
4604 /* Accept l(g)hi/l(g)fi operands. */
4605 if (GET_CODE (op) == CONST_INT
4606 && (CONST_OK_FOR_K (INTVAL (op)) || CONST_OK_FOR_Os (INTVAL (op))))
4607 return true;
4608
4609 /* Accept lliXX operands. */
4610 if (TARGET_ZARCH
4611 && GET_CODE (op) == CONST_INT
4612 && trunc_int_for_mode (INTVAL (op), word_mode) == INTVAL (op)
4613 && s390_single_part (op, word_mode, HImode, 0) >= 0)
4614 return true;
4615
4616 if (TARGET_EXTIMM
4617 && GET_CODE (op) == CONST_INT
4618 && trunc_int_for_mode (INTVAL (op), word_mode) == INTVAL (op)
4619 && s390_single_part (op, word_mode, SImode, 0) >= 0)
4620 return true;
4621
4622 /* Accept larl operands. */
4623 if (larl_operand (op, VOIDmode))
4624 return true;
4625
4626 /* Accept floating-point zero operands that fit into a single GPR. */
4627 if (GET_CODE (op) == CONST_DOUBLE
4628 && s390_float_const_zero_p (op)
4629 && GET_MODE_SIZE (GET_MODE (op)) <= UNITS_PER_WORD)
4630 return true;
4631
4632 /* Accept double-word operands that can be split. */
4633 if (GET_CODE (op) == CONST_WIDE_INT
4634 || (GET_CODE (op) == CONST_INT
4635 && trunc_int_for_mode (INTVAL (op), word_mode) != INTVAL (op)))
4636 {
4637 machine_mode dword_mode = word_mode == SImode ? DImode : TImode;
4638 rtx hi = operand_subword (op, 0, 0, dword_mode);
4639 rtx lo = operand_subword (op, 1, 0, dword_mode);
4640 return legitimate_reload_constant_p (hi)
4641 && legitimate_reload_constant_p (lo);
4642 }
4643
4644 /* Everything else cannot be handled without reload. */
4645 return false;
4646 }
4647
4648 /* Returns true if the constant value OP is a legitimate fp operand
4649 during and after reload.
4650 This function accepts all constants which can be loaded directly
4651 into an FPR. */
4652
4653 static bool
4654 legitimate_reload_fp_constant_p (rtx op)
4655 {
4656 /* Accept floating-point zero operands if the load zero instruction
4657 can be used. Prior to z196 the load fp zero instruction caused a
4658 performance penalty if the result is used as BFP number. */
4659 if (TARGET_Z196
4660 && GET_CODE (op) == CONST_DOUBLE
4661 && s390_float_const_zero_p (op))
4662 return true;
4663
4664 return false;
4665 }
4666
4667 /* Returns true if the constant value OP is a legitimate vector operand
4668 during and after reload.
4669 This function accepts all constants which can be loaded directly
4670 into an VR. */
4671
4672 static bool
4673 legitimate_reload_vector_constant_p (rtx op)
4674 {
4675 return TARGET_VX && (satisfies_constraint_j00 (op)
4676 || satisfies_constraint_jm1 (op)
4677 || satisfies_constraint_jzz (op)
4678 || satisfies_constraint_jxx (op)
4679 || satisfies_constraint_jyy (op));
4680 }
4681
4682 /* Given an rtx OP being reloaded into a reg required to be in class RCLASS,
4683 return the class of reg to actually use. */
4684
4685 static reg_class_t
4686 s390_preferred_reload_class (rtx op, reg_class_t rclass)
4687 {
4688 switch (GET_CODE (op))
4689 {
4690 /* Constants we cannot reload into general registers
4691 must be forced into the literal pool. */
4692 case CONST_VECTOR:
4693 case CONST_DOUBLE:
4694 case CONST_INT:
4695 case CONST_WIDE_INT:
4696 if (reg_class_subset_p (GENERAL_REGS, rclass)
4697 && legitimate_reload_constant_p (op))
4698 return GENERAL_REGS;
4699 else if (reg_class_subset_p (ADDR_REGS, rclass)
4700 && legitimate_reload_constant_p (op))
4701 return ADDR_REGS;
4702 else if (reg_class_subset_p (FP_REGS, rclass)
4703 && legitimate_reload_fp_constant_p (op))
4704 return FP_REGS;
4705 else if (reg_class_subset_p (VEC_REGS, rclass)
4706 && legitimate_reload_vector_constant_p (op))
4707 return VEC_REGS;
4708
4709 return NO_REGS;
4710
4711 /* If a symbolic constant or a PLUS is reloaded,
4712 it is most likely being used as an address, so
4713 prefer ADDR_REGS. If 'class' is not a superset
4714 of ADDR_REGS, e.g. FP_REGS, reject this reload. */
4715 case CONST:
4716 /* Symrefs cannot be pushed into the literal pool with -fPIC
4717 so we *MUST NOT* return NO_REGS for these cases
4718 (s390_cannot_force_const_mem will return true).
4719
4720 On the other hand we MUST return NO_REGS for symrefs with
4721 invalid addend which might have been pushed to the literal
4722 pool (no -fPIC). Usually we would expect them to be
4723 handled via secondary reload but this does not happen if
4724 they are used as literal pool slot replacement in reload
4725 inheritance (see emit_input_reload_insns). */
4726 if (GET_CODE (XEXP (op, 0)) == PLUS
4727 && GET_CODE (XEXP (XEXP(op, 0), 0)) == SYMBOL_REF
4728 && GET_CODE (XEXP (XEXP(op, 0), 1)) == CONST_INT)
4729 {
4730 if (flag_pic && reg_class_subset_p (ADDR_REGS, rclass))
4731 return ADDR_REGS;
4732 else
4733 return NO_REGS;
4734 }
4735 /* fallthrough */
4736 case LABEL_REF:
4737 case SYMBOL_REF:
4738 if (!legitimate_reload_constant_p (op))
4739 return NO_REGS;
4740 /* fallthrough */
4741 case PLUS:
4742 /* load address will be used. */
4743 if (reg_class_subset_p (ADDR_REGS, rclass))
4744 return ADDR_REGS;
4745 else
4746 return NO_REGS;
4747
4748 default:
4749 break;
4750 }
4751
4752 return rclass;
4753 }
4754
4755 /* Return true if ADDR is SYMBOL_REF + addend with addend being a
4756 multiple of ALIGNMENT and the SYMBOL_REF being naturally
4757 aligned. */
4758
4759 bool
4760 s390_check_symref_alignment (rtx addr, HOST_WIDE_INT alignment)
4761 {
4762 HOST_WIDE_INT addend;
4763 rtx symref;
4764
4765 /* The "required alignment" might be 0 (e.g. for certain structs
4766 accessed via BLKmode). Early abort in this case, as well as when
4767 an alignment > 8 is required. */
4768 if (alignment < 2 || alignment > 8)
4769 return false;
4770
4771 if (!s390_loadrelative_operand_p (addr, &symref, &addend))
4772 return false;
4773
4774 if (addend & (alignment - 1))
4775 return false;
4776
4777 if (GET_CODE (symref) == SYMBOL_REF)
4778 {
4779 /* s390_encode_section_info is not called for anchors, since they don't
4780 have corresponding VAR_DECLs. Therefore, we cannot rely on
4781 SYMBOL_FLAG_NOTALIGN{2,4,8}_P returning useful information. */
4782 if (SYMBOL_REF_ANCHOR_P (symref))
4783 {
4784 HOST_WIDE_INT block_offset = SYMBOL_REF_BLOCK_OFFSET (symref);
4785 unsigned int block_alignment = (SYMBOL_REF_BLOCK (symref)->alignment
4786 / BITS_PER_UNIT);
4787
4788 gcc_assert (block_offset >= 0);
4789 return ((block_offset & (alignment - 1)) == 0
4790 && block_alignment >= alignment);
4791 }
4792
4793 /* We have load-relative instructions for 2-byte, 4-byte, and
4794 8-byte alignment so allow only these. */
4795 switch (alignment)
4796 {
4797 case 8: return !SYMBOL_FLAG_NOTALIGN8_P (symref);
4798 case 4: return !SYMBOL_FLAG_NOTALIGN4_P (symref);
4799 case 2: return !SYMBOL_FLAG_NOTALIGN2_P (symref);
4800 default: return false;
4801 }
4802 }
4803
4804 if (GET_CODE (symref) == UNSPEC
4805 && alignment <= UNITS_PER_LONG)
4806 return true;
4807
4808 return false;
4809 }
4810
4811 /* ADDR is moved into REG using larl. If ADDR isn't a valid larl
4812 operand SCRATCH is used to reload the even part of the address and
4813 adding one. */
4814
4815 void
4816 s390_reload_larl_operand (rtx reg, rtx addr, rtx scratch)
4817 {
4818 HOST_WIDE_INT addend;
4819 rtx symref;
4820
4821 if (!s390_loadrelative_operand_p (addr, &symref, &addend))
4822 gcc_unreachable ();
4823
4824 if (!(addend & 1))
4825 /* Easy case. The addend is even so larl will do fine. */
4826 emit_move_insn (reg, addr);
4827 else
4828 {
4829 /* We can leave the scratch register untouched if the target
4830 register is a valid base register. */
4831 if (REGNO (reg) < FIRST_PSEUDO_REGISTER
4832 && REGNO_REG_CLASS (REGNO (reg)) == ADDR_REGS)
4833 scratch = reg;
4834
4835 gcc_assert (REGNO (scratch) < FIRST_PSEUDO_REGISTER);
4836 gcc_assert (REGNO_REG_CLASS (REGNO (scratch)) == ADDR_REGS);
4837
4838 if (addend != 1)
4839 emit_move_insn (scratch,
4840 gen_rtx_CONST (Pmode,
4841 gen_rtx_PLUS (Pmode, symref,
4842 GEN_INT (addend - 1))));
4843 else
4844 emit_move_insn (scratch, symref);
4845
4846 /* Increment the address using la in order to avoid clobbering cc. */
4847 s390_load_address (reg, gen_rtx_PLUS (Pmode, scratch, const1_rtx));
4848 }
4849 }
4850
4851 /* Generate what is necessary to move between REG and MEM using
4852 SCRATCH. The direction is given by TOMEM. */
4853
4854 void
4855 s390_reload_symref_address (rtx reg, rtx mem, rtx scratch, bool tomem)
4856 {
4857 /* Reload might have pulled a constant out of the literal pool.
4858 Force it back in. */
4859 if (CONST_INT_P (mem) || GET_CODE (mem) == CONST_DOUBLE
4860 || GET_CODE (mem) == CONST_WIDE_INT
4861 || GET_CODE (mem) == CONST_VECTOR
4862 || GET_CODE (mem) == CONST)
4863 mem = force_const_mem (GET_MODE (reg), mem);
4864
4865 gcc_assert (MEM_P (mem));
4866
4867 /* For a load from memory we can leave the scratch register
4868 untouched if the target register is a valid base register. */
4869 if (!tomem
4870 && REGNO (reg) < FIRST_PSEUDO_REGISTER
4871 && REGNO_REG_CLASS (REGNO (reg)) == ADDR_REGS
4872 && GET_MODE (reg) == GET_MODE (scratch))
4873 scratch = reg;
4874
4875 /* Load address into scratch register. Since we can't have a
4876 secondary reload for a secondary reload we have to cover the case
4877 where larl would need a secondary reload here as well. */
4878 s390_reload_larl_operand (scratch, XEXP (mem, 0), scratch);
4879
4880 /* Now we can use a standard load/store to do the move. */
4881 if (tomem)
4882 emit_move_insn (replace_equiv_address (mem, scratch), reg);
4883 else
4884 emit_move_insn (reg, replace_equiv_address (mem, scratch));
4885 }
4886
4887 /* Inform reload about cases where moving X with a mode MODE to a register in
4888 RCLASS requires an extra scratch or immediate register. Return the class
4889 needed for the immediate register. */
4890
4891 static reg_class_t
4892 s390_secondary_reload (bool in_p, rtx x, reg_class_t rclass_i,
4893 machine_mode mode, secondary_reload_info *sri)
4894 {
4895 enum reg_class rclass = (enum reg_class) rclass_i;
4896
4897 /* Intermediate register needed. */
4898 if (reg_classes_intersect_p (CC_REGS, rclass))
4899 return GENERAL_REGS;
4900
4901 if (TARGET_VX)
4902 {
4903 /* The vst/vl vector move instructions allow only for short
4904 displacements. */
4905 if (MEM_P (x)
4906 && GET_CODE (XEXP (x, 0)) == PLUS
4907 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4908 && !SHORT_DISP_IN_RANGE(INTVAL (XEXP (XEXP (x, 0), 1)))
4909 && reg_class_subset_p (rclass, VEC_REGS)
4910 && (!reg_class_subset_p (rclass, FP_REGS)
4911 || (GET_MODE_SIZE (mode) > 8
4912 && s390_class_max_nregs (FP_REGS, mode) == 1)))
4913 {
4914 if (in_p)
4915 sri->icode = (TARGET_64BIT ?
4916 CODE_FOR_reloaddi_la_in :
4917 CODE_FOR_reloadsi_la_in);
4918 else
4919 sri->icode = (TARGET_64BIT ?
4920 CODE_FOR_reloaddi_la_out :
4921 CODE_FOR_reloadsi_la_out);
4922 }
4923 }
4924
4925 if (TARGET_Z10)
4926 {
4927 HOST_WIDE_INT offset;
4928 rtx symref;
4929
4930 /* On z10 several optimizer steps may generate larl operands with
4931 an odd addend. */
4932 if (in_p
4933 && s390_loadrelative_operand_p (x, &symref, &offset)
4934 && mode == Pmode
4935 && (!SYMBOL_REF_P (symref) || !SYMBOL_FLAG_NOTALIGN2_P (symref))
4936 && (offset & 1) == 1)
4937 sri->icode = ((mode == DImode) ? CODE_FOR_reloaddi_larl_odd_addend_z10
4938 : CODE_FOR_reloadsi_larl_odd_addend_z10);
4939
4940 /* Handle all the (mem (symref)) accesses we cannot use the z10
4941 instructions for. */
4942 if (MEM_P (x)
4943 && s390_loadrelative_operand_p (XEXP (x, 0), NULL, NULL)
4944 && (mode == QImode
4945 || !reg_class_subset_p (rclass, GENERAL_REGS)
4946 || GET_MODE_SIZE (mode) > UNITS_PER_WORD
4947 || !s390_check_symref_alignment (XEXP (x, 0),
4948 GET_MODE_SIZE (mode))))
4949 {
4950 #define __SECONDARY_RELOAD_CASE(M,m) \
4951 case E_##M##mode: \
4952 if (TARGET_64BIT) \
4953 sri->icode = in_p ? CODE_FOR_reload##m##di_toreg_z10 : \
4954 CODE_FOR_reload##m##di_tomem_z10; \
4955 else \
4956 sri->icode = in_p ? CODE_FOR_reload##m##si_toreg_z10 : \
4957 CODE_FOR_reload##m##si_tomem_z10; \
4958 break;
4959
4960 switch (GET_MODE (x))
4961 {
4962 __SECONDARY_RELOAD_CASE (QI, qi);
4963 __SECONDARY_RELOAD_CASE (HI, hi);
4964 __SECONDARY_RELOAD_CASE (SI, si);
4965 __SECONDARY_RELOAD_CASE (DI, di);
4966 __SECONDARY_RELOAD_CASE (TI, ti);
4967 __SECONDARY_RELOAD_CASE (SF, sf);
4968 __SECONDARY_RELOAD_CASE (DF, df);
4969 __SECONDARY_RELOAD_CASE (TF, tf);
4970 __SECONDARY_RELOAD_CASE (SD, sd);
4971 __SECONDARY_RELOAD_CASE (DD, dd);
4972 __SECONDARY_RELOAD_CASE (TD, td);
4973 __SECONDARY_RELOAD_CASE (V1QI, v1qi);
4974 __SECONDARY_RELOAD_CASE (V2QI, v2qi);
4975 __SECONDARY_RELOAD_CASE (V4QI, v4qi);
4976 __SECONDARY_RELOAD_CASE (V8QI, v8qi);
4977 __SECONDARY_RELOAD_CASE (V16QI, v16qi);
4978 __SECONDARY_RELOAD_CASE (V1HI, v1hi);
4979 __SECONDARY_RELOAD_CASE (V2HI, v2hi);
4980 __SECONDARY_RELOAD_CASE (V4HI, v4hi);
4981 __SECONDARY_RELOAD_CASE (V8HI, v8hi);
4982 __SECONDARY_RELOAD_CASE (V1SI, v1si);
4983 __SECONDARY_RELOAD_CASE (V2SI, v2si);
4984 __SECONDARY_RELOAD_CASE (V4SI, v4si);
4985 __SECONDARY_RELOAD_CASE (V1DI, v1di);
4986 __SECONDARY_RELOAD_CASE (V2DI, v2di);
4987 __SECONDARY_RELOAD_CASE (V1TI, v1ti);
4988 __SECONDARY_RELOAD_CASE (V1SF, v1sf);
4989 __SECONDARY_RELOAD_CASE (V2SF, v2sf);
4990 __SECONDARY_RELOAD_CASE (V4SF, v4sf);
4991 __SECONDARY_RELOAD_CASE (V1DF, v1df);
4992 __SECONDARY_RELOAD_CASE (V2DF, v2df);
4993 __SECONDARY_RELOAD_CASE (V1TF, v1tf);
4994 default:
4995 gcc_unreachable ();
4996 }
4997 #undef __SECONDARY_RELOAD_CASE
4998 }
4999 }
5000
5001 /* We need a scratch register when loading a PLUS expression which
5002 is not a legitimate operand of the LOAD ADDRESS instruction. */
5003 /* LRA can deal with transformation of plus op very well -- so we
5004 don't need to prompt LRA in this case. */
5005 if (! lra_in_progress && in_p && s390_plus_operand (x, mode))
5006 sri->icode = (TARGET_64BIT ?
5007 CODE_FOR_reloaddi_plus : CODE_FOR_reloadsi_plus);
5008
5009 /* Performing a multiword move from or to memory we have to make sure the
5010 second chunk in memory is addressable without causing a displacement
5011 overflow. If that would be the case we calculate the address in
5012 a scratch register. */
5013 if (MEM_P (x)
5014 && GET_CODE (XEXP (x, 0)) == PLUS
5015 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5016 && !DISP_IN_RANGE (INTVAL (XEXP (XEXP (x, 0), 1))
5017 + GET_MODE_SIZE (mode) - 1))
5018 {
5019 /* For GENERAL_REGS a displacement overflow is no problem if occurring
5020 in a s_operand address since we may fallback to lm/stm. So we only
5021 have to care about overflows in the b+i+d case. */
5022 if ((reg_classes_intersect_p (GENERAL_REGS, rclass)
5023 && s390_class_max_nregs (GENERAL_REGS, mode) > 1
5024 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS)
5025 /* For FP_REGS no lm/stm is available so this check is triggered
5026 for displacement overflows in b+i+d and b+d like addresses. */
5027 || (reg_classes_intersect_p (FP_REGS, rclass)
5028 && s390_class_max_nregs (FP_REGS, mode) > 1))
5029 {
5030 if (in_p)
5031 sri->icode = (TARGET_64BIT ?
5032 CODE_FOR_reloaddi_la_in :
5033 CODE_FOR_reloadsi_la_in);
5034 else
5035 sri->icode = (TARGET_64BIT ?
5036 CODE_FOR_reloaddi_la_out :
5037 CODE_FOR_reloadsi_la_out);
5038 }
5039 }
5040
5041 /* A scratch address register is needed when a symbolic constant is
5042 copied to r0 compiling with -fPIC. In other cases the target
5043 register might be used as temporary (see legitimize_pic_address). */
5044 if (in_p && SYMBOLIC_CONST (x) && flag_pic == 2 && rclass != ADDR_REGS)
5045 sri->icode = (TARGET_64BIT ?
5046 CODE_FOR_reloaddi_PIC_addr :
5047 CODE_FOR_reloadsi_PIC_addr);
5048
5049 /* Either scratch or no register needed. */
5050 return NO_REGS;
5051 }
5052
5053 /* Implement TARGET_SECONDARY_MEMORY_NEEDED.
5054
5055 We need secondary memory to move data between GPRs and FPRs.
5056
5057 - With DFP the ldgr lgdr instructions are available. Due to the
5058 different alignment we cannot use them for SFmode. For 31 bit a
5059 64 bit value in GPR would be a register pair so here we still
5060 need to go via memory.
5061
5062 - With z13 we can do the SF/SImode moves with vlgvf. Due to the
5063 overlapping of FPRs and VRs we still disallow TF/TD modes to be
5064 in full VRs so as before also on z13 we do these moves via
5065 memory.
5066
5067 FIXME: Should we try splitting it into two vlgvg's/vlvg's instead? */
5068
5069 static bool
5070 s390_secondary_memory_needed (machine_mode mode,
5071 reg_class_t class1, reg_class_t class2)
5072 {
5073 return (((reg_classes_intersect_p (class1, VEC_REGS)
5074 && reg_classes_intersect_p (class2, GENERAL_REGS))
5075 || (reg_classes_intersect_p (class1, GENERAL_REGS)
5076 && reg_classes_intersect_p (class2, VEC_REGS)))
5077 && (TARGET_TPF || !TARGET_DFP || !TARGET_64BIT
5078 || GET_MODE_SIZE (mode) != 8)
5079 && (!TARGET_VX || (SCALAR_FLOAT_MODE_P (mode)
5080 && GET_MODE_SIZE (mode) > 8)));
5081 }
5082
5083 /* Implement TARGET_SECONDARY_MEMORY_NEEDED_MODE.
5084
5085 get_secondary_mem widens its argument to BITS_PER_WORD which loses on 64bit
5086 because the movsi and movsf patterns don't handle r/f moves. */
5087
5088 static machine_mode
5089 s390_secondary_memory_needed_mode (machine_mode mode)
5090 {
5091 if (GET_MODE_BITSIZE (mode) < 32)
5092 return mode_for_size (32, GET_MODE_CLASS (mode), 0).require ();
5093 return mode;
5094 }
5095
5096 /* Generate code to load SRC, which is PLUS that is not a
5097 legitimate operand for the LA instruction, into TARGET.
5098 SCRATCH may be used as scratch register. */
5099
5100 void
5101 s390_expand_plus_operand (rtx target, rtx src,
5102 rtx scratch)
5103 {
5104 rtx sum1, sum2;
5105 struct s390_address ad;
5106
5107 /* src must be a PLUS; get its two operands. */
5108 gcc_assert (GET_CODE (src) == PLUS);
5109 gcc_assert (GET_MODE (src) == Pmode);
5110
5111 /* Check if any of the two operands is already scheduled
5112 for replacement by reload. This can happen e.g. when
5113 float registers occur in an address. */
5114 sum1 = find_replacement (&XEXP (src, 0));
5115 sum2 = find_replacement (&XEXP (src, 1));
5116 src = gen_rtx_PLUS (Pmode, sum1, sum2);
5117
5118 /* If the address is already strictly valid, there's nothing to do. */
5119 if (!s390_decompose_address (src, &ad)
5120 || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
5121 || (ad.indx && !REGNO_OK_FOR_INDEX_P (REGNO (ad.indx))))
5122 {
5123 /* Otherwise, one of the operands cannot be an address register;
5124 we reload its value into the scratch register. */
5125 if (true_regnum (sum1) < 1 || true_regnum (sum1) > 15)
5126 {
5127 emit_move_insn (scratch, sum1);
5128 sum1 = scratch;
5129 }
5130 if (true_regnum (sum2) < 1 || true_regnum (sum2) > 15)
5131 {
5132 emit_move_insn (scratch, sum2);
5133 sum2 = scratch;
5134 }
5135
5136 /* According to the way these invalid addresses are generated
5137 in reload.cc, it should never happen (at least on s390) that
5138 *neither* of the PLUS components, after find_replacements
5139 was applied, is an address register. */
5140 if (sum1 == scratch && sum2 == scratch)
5141 {
5142 debug_rtx (src);
5143 gcc_unreachable ();
5144 }
5145
5146 src = gen_rtx_PLUS (Pmode, sum1, sum2);
5147 }
5148
5149 /* Emit the LOAD ADDRESS pattern. Note that reload of PLUS
5150 is only ever performed on addresses, so we can mark the
5151 sum as legitimate for LA in any case. */
5152 s390_load_address (target, src);
5153 }
5154
5155
5156 /* Return true if ADDR is a valid memory address.
5157 STRICT specifies whether strict register checking applies. */
5158
5159 static bool
5160 s390_legitimate_address_p (machine_mode mode, rtx addr, bool strict,
5161 code_helper = ERROR_MARK)
5162 {
5163 struct s390_address ad;
5164
5165 if (TARGET_Z10
5166 && larl_operand (addr, VOIDmode)
5167 && (mode == VOIDmode
5168 || s390_check_symref_alignment (addr, GET_MODE_SIZE (mode))))
5169 return true;
5170
5171 if (!s390_decompose_address (addr, &ad))
5172 return false;
5173
5174 /* The vector memory instructions only support short displacements.
5175 Reject invalid displacements early to prevent plenty of lay
5176 instructions to be generated later which then cannot be merged
5177 properly. */
5178 if (TARGET_VX
5179 && VECTOR_MODE_P (mode)
5180 && ad.disp != NULL_RTX
5181 && CONST_INT_P (ad.disp)
5182 && !SHORT_DISP_IN_RANGE (INTVAL (ad.disp)))
5183 return false;
5184
5185 if (strict)
5186 {
5187 if (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
5188 return false;
5189
5190 if (ad.indx && !REGNO_OK_FOR_INDEX_P (REGNO (ad.indx)))
5191 return false;
5192 }
5193 else
5194 {
5195 if (ad.base
5196 && !(REGNO (ad.base) >= FIRST_PSEUDO_REGISTER
5197 || REGNO_REG_CLASS (REGNO (ad.base)) == ADDR_REGS))
5198 return false;
5199
5200 if (ad.indx
5201 && !(REGNO (ad.indx) >= FIRST_PSEUDO_REGISTER
5202 || REGNO_REG_CLASS (REGNO (ad.indx)) == ADDR_REGS))
5203 return false;
5204 }
5205 return true;
5206 }
5207
5208 /* Return true if OP is a valid operand for the LA instruction.
5209 In 31-bit, we need to prove that the result is used as an
5210 address, as LA performs only a 31-bit addition. */
5211
5212 bool
5213 legitimate_la_operand_p (rtx op)
5214 {
5215 struct s390_address addr;
5216 if (!s390_decompose_address (op, &addr))
5217 return false;
5218
5219 return (TARGET_64BIT || addr.pointer);
5220 }
5221
5222 /* Return true if it is valid *and* preferable to use LA to
5223 compute the sum of OP1 and OP2. */
5224
5225 bool
5226 preferred_la_operand_p (rtx op1, rtx op2)
5227 {
5228 struct s390_address addr;
5229
5230 if (op2 != const0_rtx)
5231 op1 = gen_rtx_PLUS (Pmode, op1, op2);
5232
5233 if (!s390_decompose_address (op1, &addr))
5234 return false;
5235 if (addr.base && !REGNO_OK_FOR_BASE_P (REGNO (addr.base)))
5236 return false;
5237 if (addr.indx && !REGNO_OK_FOR_INDEX_P (REGNO (addr.indx)))
5238 return false;
5239
5240 /* Avoid LA instructions with index (and base) register on z196 or
5241 later; it is preferable to use regular add instructions when
5242 possible. Starting with zEC12 the la with index register is
5243 "uncracked" again but still slower than a regular add. */
5244 if (addr.indx && s390_tune >= PROCESSOR_2817_Z196)
5245 return false;
5246
5247 if (!TARGET_64BIT && !addr.pointer)
5248 return false;
5249
5250 if (addr.pointer)
5251 return true;
5252
5253 if ((addr.base && REG_P (addr.base) && REG_POINTER (addr.base))
5254 || (addr.indx && REG_P (addr.indx) && REG_POINTER (addr.indx)))
5255 return true;
5256
5257 return false;
5258 }
5259
5260 /* Emit a forced load-address operation to load SRC into DST.
5261 This will use the LOAD ADDRESS instruction even in situations
5262 where legitimate_la_operand_p (SRC) returns false. */
5263
5264 void
5265 s390_load_address (rtx dst, rtx src)
5266 {
5267 if (TARGET_64BIT)
5268 emit_move_insn (dst, src);
5269 else
5270 emit_insn (gen_force_la_31 (dst, src));
5271 }
5272
5273 /* Return true if it ok to use SYMBOL_REF in a relative address. */
5274
5275 bool
5276 s390_rel_address_ok_p (rtx symbol_ref)
5277 {
5278 tree decl;
5279
5280 if (symbol_ref == s390_got_symbol () || CONSTANT_POOL_ADDRESS_P (symbol_ref))
5281 return true;
5282
5283 decl = SYMBOL_REF_DECL (symbol_ref);
5284
5285 if (!flag_pic || SYMBOL_REF_LOCAL_P (symbol_ref))
5286 return (s390_pic_data_is_text_relative
5287 || (decl
5288 && TREE_CODE (decl) == FUNCTION_DECL));
5289
5290 return false;
5291 }
5292
5293 /* Return a legitimate reference for ORIG (an address) using the
5294 register REG. If REG is 0, a new pseudo is generated.
5295
5296 There are two types of references that must be handled:
5297
5298 1. Global data references must load the address from the GOT, via
5299 the PIC reg. An insn is emitted to do this load, and the reg is
5300 returned.
5301
5302 2. Static data references, constant pool addresses, and code labels
5303 compute the address as an offset from the GOT, whose base is in
5304 the PIC reg. Static data objects have SYMBOL_FLAG_LOCAL set to
5305 differentiate them from global data objects. The returned
5306 address is the PIC reg + an unspec constant.
5307
5308 TARGET_LEGITIMIZE_ADDRESS_P rejects symbolic references unless the PIC
5309 reg also appears in the address. */
5310
5311 rtx
5312 legitimize_pic_address (rtx orig, rtx reg)
5313 {
5314 rtx addr = orig;
5315 rtx addend = const0_rtx;
5316 rtx new_rtx = orig;
5317
5318 gcc_assert (!TLS_SYMBOLIC_CONST (addr));
5319
5320 if (GET_CODE (addr) == CONST)
5321 addr = XEXP (addr, 0);
5322
5323 if (GET_CODE (addr) == PLUS)
5324 {
5325 addend = XEXP (addr, 1);
5326 addr = XEXP (addr, 0);
5327 }
5328
5329 if ((GET_CODE (addr) == LABEL_REF
5330 || (SYMBOL_REF_P (addr) && s390_rel_address_ok_p (addr))
5331 || (GET_CODE (addr) == UNSPEC &&
5332 (XINT (addr, 1) == UNSPEC_GOTENT
5333 || XINT (addr, 1) == UNSPEC_PLT31)))
5334 && GET_CODE (addend) == CONST_INT)
5335 {
5336 /* This can be locally addressed. */
5337
5338 /* larl_operand requires UNSPECs to be wrapped in a const rtx. */
5339 rtx const_addr = (GET_CODE (addr) == UNSPEC ?
5340 gen_rtx_CONST (Pmode, addr) : addr);
5341
5342 if (larl_operand (const_addr, VOIDmode)
5343 && INTVAL (addend) < HOST_WIDE_INT_1 << 31
5344 && INTVAL (addend) >= -(HOST_WIDE_INT_1 << 31))
5345 {
5346 if (INTVAL (addend) & 1)
5347 {
5348 /* LARL can't handle odd offsets, so emit a pair of LARL
5349 and LA. */
5350 rtx temp = reg? reg : gen_reg_rtx (Pmode);
5351
5352 if (!DISP_IN_RANGE (INTVAL (addend)))
5353 {
5354 HOST_WIDE_INT even = INTVAL (addend) - 1;
5355 addr = gen_rtx_PLUS (Pmode, addr, GEN_INT (even));
5356 addr = gen_rtx_CONST (Pmode, addr);
5357 addend = const1_rtx;
5358 }
5359
5360 emit_move_insn (temp, addr);
5361 new_rtx = gen_rtx_PLUS (Pmode, temp, addend);
5362
5363 if (reg != 0)
5364 {
5365 s390_load_address (reg, new_rtx);
5366 new_rtx = reg;
5367 }
5368 }
5369 else
5370 {
5371 /* If the offset is even, we can just use LARL. This
5372 will happen automatically. */
5373 }
5374 }
5375 else
5376 {
5377 /* No larl - Access local symbols relative to the GOT. */
5378
5379 rtx temp = reg? reg : gen_reg_rtx (Pmode);
5380
5381 if (reload_in_progress || reload_completed)
5382 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
5383
5384 addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
5385 if (addend != const0_rtx)
5386 addr = gen_rtx_PLUS (Pmode, addr, addend);
5387 addr = gen_rtx_CONST (Pmode, addr);
5388 addr = force_const_mem (Pmode, addr);
5389 emit_move_insn (temp, addr);
5390
5391 new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
5392 if (reg != 0)
5393 {
5394 s390_load_address (reg, new_rtx);
5395 new_rtx = reg;
5396 }
5397 }
5398 }
5399 else if (GET_CODE (addr) == SYMBOL_REF && addend == const0_rtx)
5400 {
5401 /* A non-local symbol reference without addend.
5402
5403 The symbol ref is wrapped into an UNSPEC to make sure the
5404 proper operand modifier (@GOT or @GOTENT) will be emitted.
5405 This will tell the linker to put the symbol into the GOT.
5406
5407 Additionally the code dereferencing the GOT slot is emitted here.
5408
5409 An addend to the symref needs to be added afterwards.
5410 legitimize_pic_address calls itself recursively to handle
5411 that case. So no need to do it here. */
5412
5413 if (reg == 0)
5414 reg = gen_reg_rtx (Pmode);
5415
5416 if (TARGET_Z10)
5417 {
5418 /* Use load relative if possible.
5419 lgrl <target>, sym@GOTENT */
5420 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTENT);
5421 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5422 new_rtx = gen_const_mem (GET_MODE (reg), new_rtx);
5423
5424 emit_move_insn (reg, new_rtx);
5425 new_rtx = reg;
5426 }
5427 else if (flag_pic == 1)
5428 {
5429 /* Assume GOT offset is a valid displacement operand (< 4k
5430 or < 512k with z990). This is handled the same way in
5431 both 31- and 64-bit code (@GOT).
5432 lg <target>, sym@GOT(r12) */
5433
5434 if (reload_in_progress || reload_completed)
5435 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
5436
5437 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
5438 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5439 new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
5440 new_rtx = gen_const_mem (Pmode, new_rtx);
5441 emit_move_insn (reg, new_rtx);
5442 new_rtx = reg;
5443 }
5444 else
5445 {
5446 /* If the GOT offset might be >= 4k, we determine the position
5447 of the GOT entry via a PC-relative LARL (@GOTENT).
5448 larl temp, sym@GOTENT
5449 lg <target>, 0(temp) */
5450
5451 rtx temp = reg ? reg : gen_reg_rtx (Pmode);
5452
5453 gcc_assert (REGNO (temp) >= FIRST_PSEUDO_REGISTER
5454 || REGNO_REG_CLASS (REGNO (temp)) == ADDR_REGS);
5455
5456 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTENT);
5457 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5458 emit_move_insn (temp, new_rtx);
5459 new_rtx = gen_const_mem (Pmode, temp);
5460 emit_move_insn (reg, new_rtx);
5461
5462 new_rtx = reg;
5463 }
5464 }
5465 else if (GET_CODE (addr) == UNSPEC && GET_CODE (addend) == CONST_INT)
5466 {
5467 gcc_assert (XVECLEN (addr, 0) == 1);
5468 switch (XINT (addr, 1))
5469 {
5470 /* These address symbols (or PLT slots) relative to the GOT
5471 (not GOT slots!). In general this will exceed the
5472 displacement range so these value belong into the literal
5473 pool. */
5474 case UNSPEC_GOTOFF:
5475 case UNSPEC_PLTOFF:
5476 new_rtx = force_const_mem (Pmode, orig);
5477 break;
5478
5479 /* For -fPIC the GOT size might exceed the displacement
5480 range so make sure the value is in the literal pool. */
5481 case UNSPEC_GOT:
5482 if (flag_pic == 2)
5483 new_rtx = force_const_mem (Pmode, orig);
5484 break;
5485
5486 /* For @GOTENT larl is used. This is handled like local
5487 symbol refs. */
5488 case UNSPEC_GOTENT:
5489 gcc_unreachable ();
5490 break;
5491
5492 /* For @PLT larl is used. This is handled like local
5493 symbol refs. */
5494 case UNSPEC_PLT31:
5495 gcc_unreachable ();
5496 break;
5497
5498 /* Everything else cannot happen. */
5499 default:
5500 gcc_unreachable ();
5501 }
5502 }
5503 else if (addend != const0_rtx)
5504 {
5505 /* Otherwise, compute the sum. */
5506
5507 rtx base = legitimize_pic_address (addr, reg);
5508 new_rtx = legitimize_pic_address (addend,
5509 base == reg ? NULL_RTX : reg);
5510 if (GET_CODE (new_rtx) == CONST_INT)
5511 new_rtx = plus_constant (Pmode, base, INTVAL (new_rtx));
5512 else
5513 {
5514 if (GET_CODE (new_rtx) == PLUS && CONSTANT_P (XEXP (new_rtx, 1)))
5515 {
5516 base = gen_rtx_PLUS (Pmode, base, XEXP (new_rtx, 0));
5517 new_rtx = XEXP (new_rtx, 1);
5518 }
5519 new_rtx = gen_rtx_PLUS (Pmode, base, new_rtx);
5520 }
5521
5522 if (GET_CODE (new_rtx) == CONST)
5523 new_rtx = XEXP (new_rtx, 0);
5524 new_rtx = force_operand (new_rtx, 0);
5525 }
5526
5527 return new_rtx;
5528 }
5529
5530 /* Load the thread pointer into a register. */
5531
5532 rtx
5533 s390_get_thread_pointer (void)
5534 {
5535 rtx tp = gen_reg_rtx (Pmode);
5536
5537 emit_insn (gen_get_thread_pointer (Pmode, tp));
5538
5539 mark_reg_pointer (tp, BITS_PER_WORD);
5540
5541 return tp;
5542 }
5543
5544 /* Emit a tls call insn. The call target is the SYMBOL_REF stored
5545 in s390_tls_symbol which always refers to __tls_get_offset.
5546 The returned offset is written to RESULT_REG and an USE rtx is
5547 generated for TLS_CALL. */
5548
5549 static GTY(()) rtx s390_tls_symbol;
5550
5551 static void
5552 s390_emit_tls_call_insn (rtx result_reg, rtx tls_call)
5553 {
5554 rtx insn;
5555
5556 if (!flag_pic)
5557 emit_insn (s390_load_got ());
5558
5559 if (!s390_tls_symbol)
5560 {
5561 s390_tls_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tls_get_offset");
5562 SYMBOL_REF_FLAGS (s390_tls_symbol) |= SYMBOL_FLAG_FUNCTION;
5563 }
5564
5565 insn = s390_emit_call (s390_tls_symbol, tls_call, result_reg,
5566 gen_rtx_REG (Pmode, RETURN_REGNUM));
5567
5568 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), result_reg);
5569 RTL_CONST_CALL_P (insn) = 1;
5570 }
5571
5572 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
5573 this (thread-local) address. REG may be used as temporary. */
5574
5575 static rtx
5576 legitimize_tls_address (rtx addr, rtx reg)
5577 {
5578 rtx new_rtx, tls_call, temp, base, r2;
5579 rtx_insn *insn;
5580
5581 if (GET_CODE (addr) == SYMBOL_REF)
5582 switch (tls_symbolic_operand (addr))
5583 {
5584 case TLS_MODEL_GLOBAL_DYNAMIC:
5585 start_sequence ();
5586 r2 = gen_rtx_REG (Pmode, 2);
5587 tls_call = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_TLSGD);
5588 new_rtx = gen_rtx_CONST (Pmode, tls_call);
5589 new_rtx = force_const_mem (Pmode, new_rtx);
5590 emit_move_insn (r2, new_rtx);
5591 s390_emit_tls_call_insn (r2, tls_call);
5592 insn = get_insns ();
5593 end_sequence ();
5594
5595 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_NTPOFF);
5596 temp = gen_reg_rtx (Pmode);
5597 emit_libcall_block (insn, temp, r2, new_rtx);
5598
5599 new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
5600 if (reg != 0)
5601 {
5602 s390_load_address (reg, new_rtx);
5603 new_rtx = reg;
5604 }
5605 break;
5606
5607 case TLS_MODEL_LOCAL_DYNAMIC:
5608 start_sequence ();
5609 r2 = gen_rtx_REG (Pmode, 2);
5610 tls_call = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TLSLDM);
5611 new_rtx = gen_rtx_CONST (Pmode, tls_call);
5612 new_rtx = force_const_mem (Pmode, new_rtx);
5613 emit_move_insn (r2, new_rtx);
5614 s390_emit_tls_call_insn (r2, tls_call);
5615 insn = get_insns ();
5616 end_sequence ();
5617
5618 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TLSLDM_NTPOFF);
5619 temp = gen_reg_rtx (Pmode);
5620 emit_libcall_block (insn, temp, r2, new_rtx);
5621
5622 new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
5623 base = gen_reg_rtx (Pmode);
5624 s390_load_address (base, new_rtx);
5625
5626 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_DTPOFF);
5627 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5628 new_rtx = force_const_mem (Pmode, new_rtx);
5629 temp = gen_reg_rtx (Pmode);
5630 emit_move_insn (temp, new_rtx);
5631
5632 new_rtx = gen_rtx_PLUS (Pmode, base, temp);
5633 if (reg != 0)
5634 {
5635 s390_load_address (reg, new_rtx);
5636 new_rtx = reg;
5637 }
5638 break;
5639
5640 case TLS_MODEL_INITIAL_EXEC:
5641 if (flag_pic == 1)
5642 {
5643 /* Assume GOT offset < 4k. This is handled the same way
5644 in both 31- and 64-bit code. */
5645
5646 if (reload_in_progress || reload_completed)
5647 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
5648
5649 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTNTPOFF);
5650 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5651 new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
5652 new_rtx = gen_const_mem (Pmode, new_rtx);
5653 temp = gen_reg_rtx (Pmode);
5654 emit_move_insn (temp, new_rtx);
5655 }
5656 else
5657 {
5658 /* If the GOT offset might be >= 4k, we determine the position
5659 of the GOT entry via a PC-relative LARL. */
5660
5661 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_INDNTPOFF);
5662 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5663 temp = gen_reg_rtx (Pmode);
5664 emit_move_insn (temp, new_rtx);
5665
5666 new_rtx = gen_const_mem (Pmode, temp);
5667 temp = gen_reg_rtx (Pmode);
5668 emit_move_insn (temp, new_rtx);
5669 }
5670
5671 new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
5672 if (reg != 0)
5673 {
5674 s390_load_address (reg, new_rtx);
5675 new_rtx = reg;
5676 }
5677 break;
5678
5679 case TLS_MODEL_LOCAL_EXEC:
5680 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_NTPOFF);
5681 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5682 new_rtx = force_const_mem (Pmode, new_rtx);
5683 temp = gen_reg_rtx (Pmode);
5684 emit_move_insn (temp, new_rtx);
5685
5686 new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
5687 if (reg != 0)
5688 {
5689 s390_load_address (reg, new_rtx);
5690 new_rtx = reg;
5691 }
5692 break;
5693
5694 default:
5695 gcc_unreachable ();
5696 }
5697
5698 else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == UNSPEC)
5699 {
5700 switch (XINT (XEXP (addr, 0), 1))
5701 {
5702 case UNSPEC_NTPOFF:
5703 case UNSPEC_INDNTPOFF:
5704 new_rtx = addr;
5705 break;
5706
5707 default:
5708 gcc_unreachable ();
5709 }
5710 }
5711
5712 else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS
5713 && GET_CODE (XEXP (XEXP (addr, 0), 1)) == CONST_INT)
5714 {
5715 new_rtx = XEXP (XEXP (addr, 0), 0);
5716 if (GET_CODE (new_rtx) != SYMBOL_REF)
5717 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5718
5719 new_rtx = legitimize_tls_address (new_rtx, reg);
5720 new_rtx = plus_constant (Pmode, new_rtx,
5721 INTVAL (XEXP (XEXP (addr, 0), 1)));
5722 new_rtx = force_operand (new_rtx, 0);
5723 }
5724
5725 /* (const (neg (unspec (symbol_ref)))) -> (neg (const (unspec (symbol_ref)))) */
5726 else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == NEG)
5727 {
5728 new_rtx = XEXP (XEXP (addr, 0), 0);
5729 if (GET_CODE (new_rtx) != SYMBOL_REF)
5730 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
5731
5732 new_rtx = legitimize_tls_address (new_rtx, reg);
5733 new_rtx = gen_rtx_NEG (Pmode, new_rtx);
5734 new_rtx = force_operand (new_rtx, 0);
5735 }
5736
5737 else
5738 gcc_unreachable (); /* for now ... */
5739
5740 return new_rtx;
5741 }
5742
5743 /* Emit insns making the address in operands[1] valid for a standard
5744 move to operands[0]. operands[1] is replaced by an address which
5745 should be used instead of the former RTX to emit the move
5746 pattern. */
5747
5748 void
5749 emit_symbolic_move (rtx *operands)
5750 {
5751 rtx temp = !can_create_pseudo_p () ? operands[0] : gen_reg_rtx (Pmode);
5752
5753 if (GET_CODE (operands[0]) == MEM)
5754 operands[1] = force_reg (Pmode, operands[1]);
5755 else if (TLS_SYMBOLIC_CONST (operands[1]))
5756 operands[1] = legitimize_tls_address (operands[1], temp);
5757 else if (flag_pic)
5758 operands[1] = legitimize_pic_address (operands[1], temp);
5759 }
5760
5761 /* Try machine-dependent ways of modifying an illegitimate address X
5762 to be legitimate. If we find one, return the new, valid address.
5763
5764 OLDX is the address as it was before break_out_memory_refs was called.
5765 In some cases it is useful to look at this to decide what needs to be done.
5766
5767 MODE is the mode of the operand pointed to by X.
5768
5769 When -fpic is used, special handling is needed for symbolic references.
5770 See comments by legitimize_pic_address for details. */
5771
5772 static rtx
5773 s390_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
5774 machine_mode mode ATTRIBUTE_UNUSED)
5775 {
5776 rtx constant_term = const0_rtx;
5777
5778 if (TLS_SYMBOLIC_CONST (x))
5779 {
5780 x = legitimize_tls_address (x, 0);
5781
5782 if (s390_legitimate_address_p (mode, x, FALSE))
5783 return x;
5784 }
5785 else if (GET_CODE (x) == PLUS
5786 && (TLS_SYMBOLIC_CONST (XEXP (x, 0))
5787 || TLS_SYMBOLIC_CONST (XEXP (x, 1))))
5788 {
5789 return x;
5790 }
5791 else if (flag_pic)
5792 {
5793 if (SYMBOLIC_CONST (x)
5794 || (GET_CODE (x) == PLUS
5795 && (SYMBOLIC_CONST (XEXP (x, 0))
5796 || SYMBOLIC_CONST (XEXP (x, 1)))))
5797 x = legitimize_pic_address (x, 0);
5798
5799 if (s390_legitimate_address_p (mode, x, FALSE))
5800 return x;
5801 }
5802
5803 x = eliminate_constant_term (x, &constant_term);
5804
5805 /* Optimize loading of large displacements by splitting them
5806 into the multiple of 4K and the rest; this allows the
5807 former to be CSE'd if possible.
5808
5809 Don't do this if the displacement is added to a register
5810 pointing into the stack frame, as the offsets will
5811 change later anyway. */
5812
5813 if (GET_CODE (constant_term) == CONST_INT
5814 && !TARGET_LONG_DISPLACEMENT
5815 && !DISP_IN_RANGE (INTVAL (constant_term))
5816 && !(REG_P (x) && REGNO_PTR_FRAME_P (REGNO (x))))
5817 {
5818 HOST_WIDE_INT lower = INTVAL (constant_term) & 0xfff;
5819 HOST_WIDE_INT upper = INTVAL (constant_term) ^ lower;
5820
5821 rtx temp = gen_reg_rtx (Pmode);
5822 rtx val = force_operand (GEN_INT (upper), temp);
5823 if (val != temp)
5824 emit_move_insn (temp, val);
5825
5826 x = gen_rtx_PLUS (Pmode, x, temp);
5827 constant_term = GEN_INT (lower);
5828 }
5829
5830 if (GET_CODE (x) == PLUS)
5831 {
5832 if (GET_CODE (XEXP (x, 0)) == REG)
5833 {
5834 rtx temp = gen_reg_rtx (Pmode);
5835 rtx val = force_operand (XEXP (x, 1), temp);
5836 if (val != temp)
5837 emit_move_insn (temp, val);
5838
5839 x = gen_rtx_PLUS (Pmode, XEXP (x, 0), temp);
5840 }
5841
5842 else if (GET_CODE (XEXP (x, 1)) == REG)
5843 {
5844 rtx temp = gen_reg_rtx (Pmode);
5845 rtx val = force_operand (XEXP (x, 0), temp);
5846 if (val != temp)
5847 emit_move_insn (temp, val);
5848
5849 x = gen_rtx_PLUS (Pmode, temp, XEXP (x, 1));
5850 }
5851 }
5852
5853 if (constant_term != const0_rtx)
5854 x = gen_rtx_PLUS (Pmode, x, constant_term);
5855
5856 return x;
5857 }
5858
5859 /* Try a machine-dependent way of reloading an illegitimate address AD
5860 operand. If we find one, push the reload and return the new address.
5861
5862 MODE is the mode of the enclosing MEM. OPNUM is the operand number
5863 and TYPE is the reload type of the current reload. */
5864
5865 rtx
5866 legitimize_reload_address (rtx ad, machine_mode mode ATTRIBUTE_UNUSED,
5867 int opnum, int type)
5868 {
5869 if (!optimize || TARGET_LONG_DISPLACEMENT)
5870 return NULL_RTX;
5871
5872 if (GET_CODE (ad) == PLUS)
5873 {
5874 rtx tem = simplify_binary_operation (PLUS, Pmode,
5875 XEXP (ad, 0), XEXP (ad, 1));
5876 if (tem)
5877 ad = tem;
5878 }
5879
5880 if (GET_CODE (ad) == PLUS
5881 && GET_CODE (XEXP (ad, 0)) == REG
5882 && GET_CODE (XEXP (ad, 1)) == CONST_INT
5883 && !DISP_IN_RANGE (INTVAL (XEXP (ad, 1))))
5884 {
5885 HOST_WIDE_INT lower = INTVAL (XEXP (ad, 1)) & 0xfff;
5886 HOST_WIDE_INT upper = INTVAL (XEXP (ad, 1)) ^ lower;
5887 rtx cst, tem, new_rtx;
5888
5889 cst = GEN_INT (upper);
5890 if (!legitimate_reload_constant_p (cst))
5891 cst = force_const_mem (Pmode, cst);
5892
5893 tem = gen_rtx_PLUS (Pmode, XEXP (ad, 0), cst);
5894 new_rtx = gen_rtx_PLUS (Pmode, tem, GEN_INT (lower));
5895
5896 push_reload (XEXP (tem, 1), 0, &XEXP (tem, 1), 0,
5897 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
5898 opnum, (enum reload_type) type);
5899 return new_rtx;
5900 }
5901
5902 return NULL_RTX;
5903 }
5904
5905 /* Emit code to move LEN bytes from SRC to DST. */
5906
5907 bool
5908 s390_expand_cpymem (rtx dst, rtx src, rtx len, rtx min_len_rtx, rtx max_len_rtx)
5909 {
5910 /* Exit early in case nothing has to be done. */
5911 if (CONST_INT_P (len) && UINTVAL (len) == 0)
5912 return true;
5913
5914 unsigned HOST_WIDE_INT min_len = UINTVAL (min_len_rtx);
5915 unsigned HOST_WIDE_INT max_len
5916 = max_len_rtx ? UINTVAL (max_len_rtx) : HOST_WIDE_INT_M1U;
5917
5918 /* Expand memcpy for constant length operands without a loop if it
5919 is shorter that way.
5920
5921 With a constant length argument a
5922 memcpy loop (without pfd) is 36 bytes -> 6 * mvc */
5923 if (CONST_INT_P (len)
5924 && UINTVAL (len) <= 6 * 256
5925 && (!TARGET_MVCLE || UINTVAL (len) <= 256))
5926 {
5927 HOST_WIDE_INT o, l;
5928
5929 for (l = INTVAL (len), o = 0; l > 0; l -= 256, o += 256)
5930 {
5931 rtx newdst = adjust_address (dst, BLKmode, o);
5932 rtx newsrc = adjust_address (src, BLKmode, o);
5933 emit_insn (gen_cpymem_short (newdst, newsrc,
5934 GEN_INT (l > 256 ? 255 : l - 1)));
5935 }
5936
5937 return true;
5938 }
5939
5940 else if (TARGET_MVCLE
5941 && (s390_tune < PROCESSOR_2097_Z10
5942 || (CONST_INT_P (len) && UINTVAL (len) <= (1 << 16))))
5943 {
5944 emit_insn (gen_cpymem_long (dst, src, convert_to_mode (Pmode, len, 1)));
5945 return true;
5946 }
5947
5948 /* Non-constant length and no loop required. */
5949 else if (!CONST_INT_P (len) && max_len <= 256)
5950 {
5951 rtx_code_label *end_label;
5952
5953 if (min_len == 0)
5954 {
5955 end_label = gen_label_rtx ();
5956 emit_cmp_and_jump_insns (len, const0_rtx, EQ, NULL_RTX,
5957 GET_MODE (len), 1, end_label,
5958 profile_probability::very_unlikely ());
5959 }
5960
5961 rtx lenm1 = expand_binop (GET_MODE (len), add_optab, len, constm1_rtx,
5962 NULL_RTX, 1, OPTAB_DIRECT);
5963
5964 /* Prefer a vectorized implementation over one which makes use of an
5965 execute instruction since it is faster (although it increases register
5966 pressure). */
5967 if (max_len <= 16 && TARGET_VX)
5968 {
5969 rtx tmp = gen_reg_rtx (V16QImode);
5970 lenm1 = convert_to_mode (SImode, lenm1, 1);
5971 emit_insn (gen_vllv16qi (tmp, lenm1, src));
5972 emit_insn (gen_vstlv16qi (tmp, lenm1, dst));
5973 }
5974 else if (TARGET_Z15)
5975 emit_insn (gen_mvcrl (dst, src, convert_to_mode (SImode, lenm1, 1)));
5976 else
5977 emit_insn (
5978 gen_cpymem_short (dst, src, convert_to_mode (Pmode, lenm1, 1)));
5979
5980 if (min_len == 0)
5981 emit_label (end_label);
5982
5983 return true;
5984 }
5985
5986 else if (s390_tune < PROCESSOR_2097_Z10 || (CONST_INT_P (len) && UINTVAL (len) <= (1 << 16)))
5987 {
5988 rtx dst_addr, src_addr, count, blocks, temp;
5989 rtx_code_label *loop_start_label = gen_label_rtx ();
5990 rtx_code_label *loop_end_label = gen_label_rtx ();
5991 rtx_code_label *end_label = gen_label_rtx ();
5992 machine_mode mode;
5993
5994 mode = GET_MODE (len);
5995 if (mode == VOIDmode)
5996 mode = Pmode;
5997
5998 dst_addr = gen_reg_rtx (Pmode);
5999 src_addr = gen_reg_rtx (Pmode);
6000 count = gen_reg_rtx (mode);
6001 blocks = gen_reg_rtx (mode);
6002
6003 convert_move (count, len, 1);
6004 if (min_len == 0)
6005 emit_cmp_and_jump_insns (count, const0_rtx, EQ, NULL_RTX, mode, 1,
6006 end_label);
6007
6008 emit_move_insn (dst_addr, force_operand (XEXP (dst, 0), NULL_RTX));
6009 emit_move_insn (src_addr, force_operand (XEXP (src, 0), NULL_RTX));
6010 dst = change_address (dst, VOIDmode, dst_addr);
6011 src = change_address (src, VOIDmode, src_addr);
6012
6013 temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1,
6014 OPTAB_DIRECT);
6015 if (temp != count)
6016 emit_move_insn (count, temp);
6017
6018 temp = expand_binop (mode, lshr_optab, count, GEN_INT (8), blocks, 1,
6019 OPTAB_DIRECT);
6020 if (temp != blocks)
6021 emit_move_insn (blocks, temp);
6022
6023 emit_cmp_and_jump_insns (blocks, const0_rtx,
6024 EQ, NULL_RTX, mode, 1, loop_end_label);
6025
6026 emit_label (loop_start_label);
6027
6028 if (TARGET_Z10
6029 && (GET_CODE (len) != CONST_INT || INTVAL (len) > 768))
6030 {
6031 rtx prefetch;
6032
6033 /* Issue a read prefetch for the +3 cache line. */
6034 prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, src_addr, GEN_INT (768)),
6035 const0_rtx, const0_rtx);
6036 PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
6037 emit_insn (prefetch);
6038
6039 /* Issue a write prefetch for the +3 cache line. */
6040 prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (768)),
6041 const1_rtx, const0_rtx);
6042 PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
6043 emit_insn (prefetch);
6044 }
6045
6046 emit_insn (gen_cpymem_short (dst, src, GEN_INT (255)));
6047 s390_load_address (dst_addr,
6048 gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (256)));
6049 s390_load_address (src_addr,
6050 gen_rtx_PLUS (Pmode, src_addr, GEN_INT (256)));
6051
6052 temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1,
6053 OPTAB_DIRECT);
6054 if (temp != blocks)
6055 emit_move_insn (blocks, temp);
6056
6057 emit_cmp_and_jump_insns (blocks, const0_rtx,
6058 EQ, NULL_RTX, mode, 1, loop_end_label);
6059
6060 emit_jump (loop_start_label);
6061 emit_label (loop_end_label);
6062
6063 emit_insn (gen_cpymem_short (dst, src,
6064 convert_to_mode (Pmode, count, 1)));
6065 emit_label (end_label);
6066
6067 return true;
6068 }
6069
6070 return false;
6071 }
6072
6073 bool
6074 s390_expand_movmem (rtx dst, rtx src, rtx len, rtx min_len_rtx, rtx max_len_rtx)
6075 {
6076 /* Exit early in case nothing has to be done. */
6077 if (CONST_INT_P (len) && UINTVAL (len) == 0)
6078 return true;
6079 /* Exit early in case length is not upper bounded. */
6080 else if (max_len_rtx == NULL)
6081 return false;
6082
6083 unsigned HOST_WIDE_INT min_len = UINTVAL (min_len_rtx);
6084 unsigned HOST_WIDE_INT max_len = UINTVAL (max_len_rtx);
6085
6086 /* At most 16 bytes. */
6087 if (max_len <= 16 && TARGET_VX)
6088 {
6089 rtx_code_label *end_label;
6090
6091 if (min_len == 0)
6092 {
6093 end_label = gen_label_rtx ();
6094 emit_cmp_and_jump_insns (len, const0_rtx, EQ, NULL_RTX,
6095 GET_MODE (len), 1, end_label,
6096 profile_probability::very_unlikely ());
6097 }
6098
6099 rtx lenm1;
6100 if (CONST_INT_P (len))
6101 {
6102 lenm1 = gen_reg_rtx (SImode);
6103 emit_move_insn (lenm1, GEN_INT (UINTVAL (len) - 1));
6104 }
6105 else
6106 lenm1
6107 = expand_binop (SImode, add_optab, convert_to_mode (SImode, len, 1),
6108 constm1_rtx, NULL_RTX, 1, OPTAB_DIRECT);
6109
6110 rtx tmp = gen_reg_rtx (V16QImode);
6111 emit_insn (gen_vllv16qi (tmp, lenm1, src));
6112 emit_insn (gen_vstlv16qi (tmp, lenm1, dst));
6113
6114 if (min_len == 0)
6115 emit_label (end_label);
6116
6117 return true;
6118 }
6119
6120 /* At most 256 bytes. */
6121 else if (max_len <= 256 && TARGET_Z15)
6122 {
6123 rtx_code_label *end_label = gen_label_rtx ();
6124
6125 if (min_len == 0)
6126 emit_cmp_and_jump_insns (len, const0_rtx, EQ, NULL_RTX, GET_MODE (len),
6127 1, end_label,
6128 profile_probability::very_unlikely ());
6129
6130 rtx dst_addr = gen_reg_rtx (Pmode);
6131 rtx src_addr = gen_reg_rtx (Pmode);
6132 emit_move_insn (dst_addr, force_operand (XEXP (dst, 0), NULL_RTX));
6133 emit_move_insn (src_addr, force_operand (XEXP (src, 0), NULL_RTX));
6134
6135 rtx lenm1 = CONST_INT_P (len)
6136 ? GEN_INT (UINTVAL (len) - 1)
6137 : expand_binop (GET_MODE (len), add_optab, len, constm1_rtx,
6138 NULL_RTX, 1, OPTAB_DIRECT);
6139
6140 rtx_code_label *right_to_left_label = gen_label_rtx ();
6141 emit_cmp_and_jump_insns (src_addr, dst_addr, LT, NULL_RTX, GET_MODE (len),
6142 1, right_to_left_label);
6143
6144 // MVC
6145 emit_insn (
6146 gen_cpymem_short (dst, src, convert_to_mode (Pmode, lenm1, 1)));
6147 emit_jump (end_label);
6148
6149 // MVCRL
6150 emit_label (right_to_left_label);
6151 emit_insn (gen_mvcrl (dst, src, convert_to_mode (SImode, lenm1, 1)));
6152
6153 emit_label (end_label);
6154
6155 return true;
6156 }
6157
6158 return false;
6159 }
6160
6161 /* Emit code to set LEN bytes at DST to VAL.
6162 Make use of clrmem if VAL is zero. */
6163
6164 void
6165 s390_expand_setmem (rtx dst, rtx len, rtx val, rtx min_len_rtx, rtx max_len_rtx)
6166 {
6167 /* Exit early in case nothing has to be done. */
6168 if (CONST_INT_P (len) && UINTVAL (len) == 0)
6169 return;
6170
6171 gcc_assert (GET_CODE (val) == CONST_INT || GET_MODE (val) == QImode);
6172
6173 unsigned HOST_WIDE_INT min_len = UINTVAL (min_len_rtx);
6174 unsigned HOST_WIDE_INT max_len
6175 = max_len_rtx ? UINTVAL (max_len_rtx) : HOST_WIDE_INT_M1U;
6176
6177 /* Vectorize memset with a constant length
6178 - if 0 < LEN < 16, then emit a vstl based solution;
6179 - if 16 <= LEN <= 64, then emit a vst based solution
6180 where the last two vector stores may overlap in case LEN%16!=0. Paying
6181 the price for an overlap is negligible compared to an extra GPR which is
6182 required for vstl. */
6183 if (CONST_INT_P (len) && UINTVAL (len) <= 64 && val != const0_rtx
6184 && TARGET_VX)
6185 {
6186 rtx val_vec = gen_reg_rtx (V16QImode);
6187 emit_move_insn (val_vec, gen_rtx_VEC_DUPLICATE (V16QImode, val));
6188
6189 if (UINTVAL (len) < 16)
6190 {
6191 rtx len_reg = gen_reg_rtx (SImode);
6192 emit_move_insn (len_reg, GEN_INT (UINTVAL (len) - 1));
6193 emit_insn (gen_vstlv16qi (val_vec, len_reg, dst));
6194 }
6195 else
6196 {
6197 unsigned HOST_WIDE_INT l = UINTVAL (len) / 16;
6198 unsigned HOST_WIDE_INT r = UINTVAL (len) % 16;
6199 unsigned HOST_WIDE_INT o = 0;
6200 for (unsigned HOST_WIDE_INT i = 0; i < l; ++i)
6201 {
6202 rtx newdst = adjust_address (dst, V16QImode, o);
6203 emit_move_insn (newdst, val_vec);
6204 o += 16;
6205 }
6206 if (r != 0)
6207 {
6208 rtx newdst = adjust_address (dst, V16QImode, (o - 16) + r);
6209 emit_move_insn (newdst, val_vec);
6210 }
6211 }
6212 }
6213
6214 /* Expand setmem/clrmem for a constant length operand without a
6215 loop if it will be shorter that way.
6216 clrmem loop (with PFD) is 30 bytes -> 5 * xc
6217 clrmem loop (without PFD) is 24 bytes -> 4 * xc
6218 setmem loop (with PFD) is 38 bytes -> ~4 * (mvi/stc + mvc)
6219 setmem loop (without PFD) is 32 bytes -> ~4 * (mvi/stc + mvc) */
6220 else if (GET_CODE (len) == CONST_INT
6221 && ((val == const0_rtx
6222 && (INTVAL (len) <= 256 * 4
6223 || (INTVAL (len) <= 256 * 5 && TARGET_SETMEM_PFD(val,len))))
6224 || (val != const0_rtx && INTVAL (len) <= 257 * 4))
6225 && (!TARGET_MVCLE || INTVAL (len) <= 256))
6226 {
6227 HOST_WIDE_INT o, l;
6228
6229 if (val == const0_rtx)
6230 /* clrmem: emit 256 byte blockwise XCs. */
6231 for (l = INTVAL (len), o = 0; l > 0; l -= 256, o += 256)
6232 {
6233 rtx newdst = adjust_address (dst, BLKmode, o);
6234 emit_insn (gen_clrmem_short (newdst,
6235 GEN_INT (l > 256 ? 255 : l - 1)));
6236 }
6237 else
6238 /* setmem: emit 1(mvi) + 256(mvc) byte blockwise memsets by
6239 setting first byte to val and using a 256 byte mvc with one
6240 byte overlap to propagate the byte. */
6241 for (l = INTVAL (len), o = 0; l > 0; l -= 257, o += 257)
6242 {
6243 rtx newdst = adjust_address (dst, BLKmode, o);
6244 emit_move_insn (adjust_address (dst, QImode, o), val);
6245 if (l > 1)
6246 {
6247 rtx newdstp1 = adjust_address (dst, BLKmode, o + 1);
6248 emit_insn (gen_cpymem_short (newdstp1, newdst,
6249 GEN_INT (l > 257 ? 255 : l - 2)));
6250 }
6251 }
6252 }
6253
6254 else if (TARGET_MVCLE)
6255 {
6256 val = force_not_mem (convert_modes (Pmode, QImode, val, 1));
6257 if (TARGET_64BIT)
6258 emit_insn (gen_setmem_long_di (dst, convert_to_mode (Pmode, len, 1),
6259 val));
6260 else
6261 emit_insn (gen_setmem_long_si (dst, convert_to_mode (Pmode, len, 1),
6262 val));
6263 }
6264
6265 /* Non-constant length and no loop required. */
6266 else if (!CONST_INT_P (len) && max_len <= 256)
6267 {
6268 rtx_code_label *end_label;
6269
6270 if (min_len == 0)
6271 {
6272 end_label = gen_label_rtx ();
6273 emit_cmp_and_jump_insns (len, const0_rtx, EQ, NULL_RTX,
6274 GET_MODE (len), 1, end_label,
6275 profile_probability::very_unlikely ());
6276 }
6277
6278 rtx lenm1 = expand_binop (GET_MODE (len), add_optab, len, constm1_rtx,
6279 NULL_RTX, 1, OPTAB_DIRECT);
6280
6281 /* Prefer a vectorized implementation over one which makes use of an
6282 execute instruction since it is faster (although it increases register
6283 pressure). */
6284 if (max_len <= 16 && TARGET_VX)
6285 {
6286 rtx val_vec = gen_reg_rtx (V16QImode);
6287 if (val == const0_rtx)
6288 emit_move_insn (val_vec, CONST0_RTX (V16QImode));
6289 else
6290 emit_move_insn (val_vec, gen_rtx_VEC_DUPLICATE (V16QImode, val));
6291
6292 lenm1 = convert_to_mode (SImode, lenm1, 1);
6293 emit_insn (gen_vstlv16qi (val_vec, lenm1, dst));
6294 }
6295 else
6296 {
6297 if (val == const0_rtx)
6298 emit_insn (
6299 gen_clrmem_short (dst, convert_to_mode (Pmode, lenm1, 1)));
6300 else
6301 {
6302 emit_move_insn (adjust_address (dst, QImode, 0), val);
6303
6304 rtx_code_label *onebyte_end_label;
6305 if (min_len <= 1)
6306 {
6307 onebyte_end_label = gen_label_rtx ();
6308 emit_cmp_and_jump_insns (
6309 len, const1_rtx, EQ, NULL_RTX, GET_MODE (len), 1,
6310 onebyte_end_label, profile_probability::very_unlikely ());
6311 }
6312
6313 rtx dstp1 = adjust_address (dst, VOIDmode, 1);
6314 rtx lenm2
6315 = expand_binop (GET_MODE (len), add_optab, len, GEN_INT (-2),
6316 NULL_RTX, 1, OPTAB_DIRECT);
6317 lenm2 = convert_to_mode (Pmode, lenm2, 1);
6318 emit_insn (gen_cpymem_short (dstp1, dst, lenm2));
6319
6320 if (min_len <= 1)
6321 emit_label (onebyte_end_label);
6322 }
6323 }
6324
6325 if (min_len == 0)
6326 emit_label (end_label);
6327 }
6328
6329 else
6330 {
6331 rtx dst_addr, count, blocks, temp, dstp1 = NULL_RTX;
6332 rtx_code_label *loop_start_label = gen_label_rtx ();
6333 rtx_code_label *onebyte_end_label = gen_label_rtx ();
6334 rtx_code_label *zerobyte_end_label = gen_label_rtx ();
6335 rtx_code_label *restbyte_end_label = gen_label_rtx ();
6336 machine_mode mode;
6337
6338 mode = GET_MODE (len);
6339 if (mode == VOIDmode)
6340 mode = Pmode;
6341
6342 dst_addr = gen_reg_rtx (Pmode);
6343 count = gen_reg_rtx (mode);
6344 blocks = gen_reg_rtx (mode);
6345
6346 convert_move (count, len, 1);
6347 if (min_len == 0)
6348 emit_cmp_and_jump_insns (count, const0_rtx, EQ, NULL_RTX, mode, 1,
6349 zerobyte_end_label,
6350 profile_probability::very_unlikely ());
6351
6352 /* We need to make a copy of the target address since memset is
6353 supposed to return it unmodified. We have to make it here
6354 already since the new reg is used at onebyte_end_label. */
6355 emit_move_insn (dst_addr, force_operand (XEXP (dst, 0), NULL_RTX));
6356 dst = change_address (dst, VOIDmode, dst_addr);
6357
6358 if (val != const0_rtx)
6359 {
6360 /* When using the overlapping mvc the original target
6361 address is only accessed as single byte entity (even by
6362 the mvc reading this value). */
6363 set_mem_size (dst, 1);
6364 dstp1 = adjust_address (dst, VOIDmode, 1);
6365 if (min_len <= 1)
6366 emit_cmp_and_jump_insns (count, const1_rtx, EQ, NULL_RTX, mode, 1,
6367 onebyte_end_label,
6368 profile_probability::very_unlikely ());
6369 }
6370
6371 /* There is one unconditional (mvi+mvc)/xc after the loop
6372 dealing with the rest of the bytes, subtracting two (mvi+mvc)
6373 or one (xc) here leaves this number of bytes to be handled by
6374 it. */
6375 temp = expand_binop (mode, add_optab, count,
6376 val == const0_rtx ? constm1_rtx : GEN_INT (-2),
6377 count, 1, OPTAB_DIRECT);
6378 if (temp != count)
6379 emit_move_insn (count, temp);
6380
6381 temp = expand_binop (mode, lshr_optab, count, GEN_INT (8), blocks, 1,
6382 OPTAB_DIRECT);
6383 if (temp != blocks)
6384 emit_move_insn (blocks, temp);
6385
6386 emit_cmp_and_jump_insns (blocks, const0_rtx,
6387 EQ, NULL_RTX, mode, 1, restbyte_end_label);
6388
6389 emit_jump (loop_start_label);
6390
6391 if (val != const0_rtx && min_len <= 1)
6392 {
6393 /* The 1 byte != 0 special case. Not handled efficiently
6394 since we require two jumps for that. However, this
6395 should be very rare. */
6396 emit_label (onebyte_end_label);
6397 emit_move_insn (adjust_address (dst, QImode, 0), val);
6398 emit_jump (zerobyte_end_label);
6399 }
6400
6401 emit_label (loop_start_label);
6402
6403 if (TARGET_SETMEM_PFD (val, len))
6404 {
6405 /* Issue a write prefetch. */
6406 rtx distance = GEN_INT (TARGET_SETMEM_PREFETCH_DISTANCE);
6407 rtx prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, dst_addr, distance),
6408 const1_rtx, const0_rtx);
6409 emit_insn (prefetch);
6410 PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
6411 }
6412
6413 if (val == const0_rtx)
6414 emit_insn (gen_clrmem_short (dst, GEN_INT (255)));
6415 else
6416 {
6417 /* Set the first byte in the block to the value and use an
6418 overlapping mvc for the block. */
6419 emit_move_insn (adjust_address (dst, QImode, 0), val);
6420 emit_insn (gen_cpymem_short (dstp1, dst, GEN_INT (254)));
6421 }
6422 s390_load_address (dst_addr,
6423 gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (256)));
6424
6425 temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1,
6426 OPTAB_DIRECT);
6427 if (temp != blocks)
6428 emit_move_insn (blocks, temp);
6429
6430 emit_cmp_and_jump_insns (blocks, const0_rtx,
6431 NE, NULL_RTX, mode, 1, loop_start_label);
6432
6433 emit_label (restbyte_end_label);
6434
6435 if (val == const0_rtx)
6436 emit_insn (gen_clrmem_short (dst, convert_to_mode (Pmode, count, 1)));
6437 else
6438 {
6439 /* Set the first byte in the block to the value and use an
6440 overlapping mvc for the block. */
6441 emit_move_insn (adjust_address (dst, QImode, 0), val);
6442 /* execute only uses the lowest 8 bits of count that's
6443 exactly what we need here. */
6444 emit_insn (gen_cpymem_short (dstp1, dst,
6445 convert_to_mode (Pmode, count, 1)));
6446 }
6447
6448 emit_label (zerobyte_end_label);
6449 }
6450 }
6451
6452 /* Emit code to compare LEN bytes at OP0 with those at OP1,
6453 and return the result in TARGET. */
6454
6455 bool
6456 s390_expand_cmpmem (rtx target, rtx op0, rtx op1, rtx len)
6457 {
6458 rtx ccreg = gen_rtx_REG (CCUmode, CC_REGNUM);
6459 rtx tmp;
6460
6461 /* When tuning for z10 or higher we rely on the Glibc functions to
6462 do the right thing. Only for constant lengths below 64k we will
6463 generate inline code. */
6464 if (s390_tune >= PROCESSOR_2097_Z10
6465 && (GET_CODE (len) != CONST_INT || INTVAL (len) > (1<<16)))
6466 return false;
6467
6468 /* As the result of CMPINT is inverted compared to what we need,
6469 we have to swap the operands. */
6470 tmp = op0; op0 = op1; op1 = tmp;
6471
6472 if (GET_CODE (len) == CONST_INT && INTVAL (len) >= 0 && INTVAL (len) <= 256)
6473 {
6474 if (INTVAL (len) > 0)
6475 {
6476 emit_insn (gen_cmpmem_short (op0, op1, GEN_INT (INTVAL (len) - 1)));
6477 emit_insn (gen_cmpint (target, ccreg));
6478 }
6479 else
6480 emit_move_insn (target, const0_rtx);
6481 }
6482 else if (TARGET_MVCLE)
6483 {
6484 emit_insn (gen_cmpmem_long (op0, op1, convert_to_mode (Pmode, len, 1)));
6485 emit_insn (gen_cmpint (target, ccreg));
6486 }
6487 else
6488 {
6489 rtx addr0, addr1, count, blocks, temp;
6490 rtx_code_label *loop_start_label = gen_label_rtx ();
6491 rtx_code_label *loop_end_label = gen_label_rtx ();
6492 rtx_code_label *end_label = gen_label_rtx ();
6493 machine_mode mode;
6494
6495 mode = GET_MODE (len);
6496 if (mode == VOIDmode)
6497 mode = Pmode;
6498
6499 addr0 = gen_reg_rtx (Pmode);
6500 addr1 = gen_reg_rtx (Pmode);
6501 count = gen_reg_rtx (mode);
6502 blocks = gen_reg_rtx (mode);
6503
6504 convert_move (count, len, 1);
6505 emit_cmp_and_jump_insns (count, const0_rtx,
6506 EQ, NULL_RTX, mode, 1, end_label);
6507
6508 emit_move_insn (addr0, force_operand (XEXP (op0, 0), NULL_RTX));
6509 emit_move_insn (addr1, force_operand (XEXP (op1, 0), NULL_RTX));
6510 op0 = change_address (op0, VOIDmode, addr0);
6511 op1 = change_address (op1, VOIDmode, addr1);
6512
6513 temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1,
6514 OPTAB_DIRECT);
6515 if (temp != count)
6516 emit_move_insn (count, temp);
6517
6518 temp = expand_binop (mode, lshr_optab, count, GEN_INT (8), blocks, 1,
6519 OPTAB_DIRECT);
6520 if (temp != blocks)
6521 emit_move_insn (blocks, temp);
6522
6523 emit_cmp_and_jump_insns (blocks, const0_rtx,
6524 EQ, NULL_RTX, mode, 1, loop_end_label);
6525
6526 emit_label (loop_start_label);
6527
6528 if (TARGET_Z10
6529 && (GET_CODE (len) != CONST_INT || INTVAL (len) > 512))
6530 {
6531 rtx prefetch;
6532
6533 /* Issue a read prefetch for the +2 cache line of operand 1. */
6534 prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, addr0, GEN_INT (512)),
6535 const0_rtx, const0_rtx);
6536 emit_insn (prefetch);
6537 PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
6538
6539 /* Issue a read prefetch for the +2 cache line of operand 2. */
6540 prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, addr1, GEN_INT (512)),
6541 const0_rtx, const0_rtx);
6542 emit_insn (prefetch);
6543 PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
6544 }
6545
6546 emit_insn (gen_cmpmem_short (op0, op1, GEN_INT (255)));
6547 temp = gen_rtx_NE (VOIDmode, ccreg, const0_rtx);
6548 temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
6549 gen_rtx_LABEL_REF (VOIDmode, end_label), pc_rtx);
6550 temp = gen_rtx_SET (pc_rtx, temp);
6551 emit_jump_insn (temp);
6552
6553 s390_load_address (addr0,
6554 gen_rtx_PLUS (Pmode, addr0, GEN_INT (256)));
6555 s390_load_address (addr1,
6556 gen_rtx_PLUS (Pmode, addr1, GEN_INT (256)));
6557
6558 temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1,
6559 OPTAB_DIRECT);
6560 if (temp != blocks)
6561 emit_move_insn (blocks, temp);
6562
6563 emit_cmp_and_jump_insns (blocks, const0_rtx,
6564 EQ, NULL_RTX, mode, 1, loop_end_label);
6565
6566 emit_jump (loop_start_label);
6567 emit_label (loop_end_label);
6568
6569 emit_insn (gen_cmpmem_short (op0, op1,
6570 convert_to_mode (Pmode, count, 1)));
6571 emit_label (end_label);
6572
6573 emit_insn (gen_cmpint (target, ccreg));
6574 }
6575 return true;
6576 }
6577
6578 /* Emit a conditional jump to LABEL for condition code mask MASK using
6579 comparsion operator COMPARISON. Return the emitted jump insn. */
6580
6581 static rtx_insn *
6582 s390_emit_ccraw_jump (HOST_WIDE_INT mask, enum rtx_code comparison, rtx label)
6583 {
6584 rtx temp;
6585
6586 gcc_assert (comparison == EQ || comparison == NE);
6587 gcc_assert (mask > 0 && mask < 15);
6588
6589 temp = gen_rtx_fmt_ee (comparison, VOIDmode,
6590 gen_rtx_REG (CCRAWmode, CC_REGNUM), GEN_INT (mask));
6591 temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
6592 gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
6593 temp = gen_rtx_SET (pc_rtx, temp);
6594 return emit_jump_insn (temp);
6595 }
6596
6597 /* Emit the instructions to implement strlen of STRING and store the
6598 result in TARGET. The string has the known ALIGNMENT. This
6599 version uses vector instructions and is therefore not appropriate
6600 for targets prior to z13. */
6601
6602 void
6603 s390_expand_vec_strlen (rtx target, rtx string, rtx alignment)
6604 {
6605 rtx highest_index_to_load_reg = gen_reg_rtx (Pmode);
6606 rtx str_reg = gen_reg_rtx (V16QImode);
6607 rtx str_addr_base_reg = gen_reg_rtx (Pmode);
6608 rtx str_idx_reg = gen_reg_rtx (Pmode);
6609 rtx result_reg = gen_reg_rtx (V16QImode);
6610 rtx is_aligned_label = gen_label_rtx ();
6611 rtx into_loop_label = NULL_RTX;
6612 rtx loop_start_label = gen_label_rtx ();
6613 rtx temp;
6614 rtx len = gen_reg_rtx (QImode);
6615 rtx cond;
6616 rtx mem;
6617
6618 s390_load_address (str_addr_base_reg, XEXP (string, 0));
6619 emit_move_insn (str_idx_reg, const0_rtx);
6620
6621 if (INTVAL (alignment) < 16)
6622 {
6623 /* Check whether the address happens to be aligned properly so
6624 jump directly to the aligned loop. */
6625 emit_cmp_and_jump_insns (gen_rtx_AND (Pmode,
6626 str_addr_base_reg, GEN_INT (15)),
6627 const0_rtx, EQ, NULL_RTX,
6628 Pmode, 1, is_aligned_label);
6629
6630 temp = gen_reg_rtx (Pmode);
6631 temp = expand_binop (Pmode, and_optab, str_addr_base_reg,
6632 GEN_INT (15), temp, 1, OPTAB_DIRECT);
6633 gcc_assert (REG_P (temp));
6634 highest_index_to_load_reg =
6635 expand_binop (Pmode, sub_optab, GEN_INT (15), temp,
6636 highest_index_to_load_reg, 1, OPTAB_DIRECT);
6637 gcc_assert (REG_P (highest_index_to_load_reg));
6638 emit_insn (gen_vllv16qi (str_reg,
6639 convert_to_mode (SImode, highest_index_to_load_reg, 1),
6640 gen_rtx_MEM (BLKmode, str_addr_base_reg)));
6641
6642 into_loop_label = gen_label_rtx ();
6643 s390_emit_jump (into_loop_label, NULL_RTX);
6644 emit_barrier ();
6645 }
6646
6647 emit_label (is_aligned_label);
6648 LABEL_NUSES (is_aligned_label) = INTVAL (alignment) < 16 ? 2 : 1;
6649
6650 /* Reaching this point we are only performing 16 bytes aligned
6651 loads. */
6652 emit_move_insn (highest_index_to_load_reg, GEN_INT (15));
6653
6654 emit_label (loop_start_label);
6655 LABEL_NUSES (loop_start_label) = 1;
6656
6657 /* Load 16 bytes of the string into VR. */
6658 mem = gen_rtx_MEM (V16QImode,
6659 gen_rtx_PLUS (Pmode, str_idx_reg, str_addr_base_reg));
6660 set_mem_align (mem, 128);
6661 emit_move_insn (str_reg, mem);
6662 if (into_loop_label != NULL_RTX)
6663 {
6664 emit_label (into_loop_label);
6665 LABEL_NUSES (into_loop_label) = 1;
6666 }
6667
6668 /* Increment string index by 16 bytes. */
6669 expand_binop (Pmode, add_optab, str_idx_reg, GEN_INT (16),
6670 str_idx_reg, 1, OPTAB_DIRECT);
6671
6672 emit_insn (gen_vec_vfenesv16qi (result_reg, str_reg, str_reg,
6673 GEN_INT (VSTRING_FLAG_ZS | VSTRING_FLAG_CS)));
6674
6675 add_int_reg_note (s390_emit_ccraw_jump (8, NE, loop_start_label),
6676 REG_BR_PROB,
6677 profile_probability::very_likely ().to_reg_br_prob_note ());
6678 emit_insn (gen_vec_extractv16qiqi (len, result_reg, GEN_INT (7)));
6679
6680 /* If the string pointer wasn't aligned we have loaded less then 16
6681 bytes and the remaining bytes got filled with zeros (by vll).
6682 Now we have to check whether the resulting index lies within the
6683 bytes actually part of the string. */
6684
6685 cond = s390_emit_compare (VOIDmode, GT, convert_to_mode (Pmode, len, 1),
6686 highest_index_to_load_reg);
6687 s390_load_address (highest_index_to_load_reg,
6688 gen_rtx_PLUS (Pmode, highest_index_to_load_reg,
6689 const1_rtx));
6690 if (TARGET_64BIT)
6691 emit_insn (gen_movdicc (str_idx_reg, cond,
6692 highest_index_to_load_reg, str_idx_reg));
6693 else
6694 emit_insn (gen_movsicc (str_idx_reg, cond,
6695 highest_index_to_load_reg, str_idx_reg));
6696
6697 add_reg_br_prob_note (s390_emit_jump (is_aligned_label, cond),
6698 profile_probability::very_unlikely ());
6699
6700 expand_binop (Pmode, add_optab, str_idx_reg,
6701 GEN_INT (-16), str_idx_reg, 1, OPTAB_DIRECT);
6702 /* FIXME: len is already zero extended - so avoid the llgcr emitted
6703 here. */
6704 temp = expand_binop (Pmode, add_optab, str_idx_reg,
6705 convert_to_mode (Pmode, len, 1),
6706 target, 1, OPTAB_DIRECT);
6707 if (temp != target)
6708 emit_move_insn (target, temp);
6709 }
6710
6711 void
6712 s390_expand_vec_movstr (rtx result, rtx dst, rtx src)
6713 {
6714 rtx temp = gen_reg_rtx (Pmode);
6715 rtx src_addr = XEXP (src, 0);
6716 rtx dst_addr = XEXP (dst, 0);
6717 rtx src_addr_reg = gen_reg_rtx (Pmode);
6718 rtx dst_addr_reg = gen_reg_rtx (Pmode);
6719 rtx offset = gen_reg_rtx (Pmode);
6720 rtx vsrc = gen_reg_rtx (V16QImode);
6721 rtx vpos = gen_reg_rtx (V16QImode);
6722 rtx loadlen = gen_reg_rtx (SImode);
6723 rtx gpos_qi = gen_reg_rtx(QImode);
6724 rtx gpos = gen_reg_rtx (SImode);
6725 rtx done_label = gen_label_rtx ();
6726 rtx loop_label = gen_label_rtx ();
6727 rtx exit_label = gen_label_rtx ();
6728 rtx full_label = gen_label_rtx ();
6729
6730 /* Perform a quick check for string ending on the first up to 16
6731 bytes and exit early if successful. */
6732
6733 emit_insn (gen_vlbb (vsrc, src, GEN_INT (6)));
6734 emit_insn (gen_lcbb (loadlen, src_addr, GEN_INT (6)));
6735 emit_insn (gen_vfenezv16qi (vpos, vsrc, vsrc));
6736 emit_insn (gen_vec_extractv16qiqi (gpos_qi, vpos, GEN_INT (7)));
6737 emit_move_insn (gpos, gen_rtx_SUBREG (SImode, gpos_qi, 0));
6738 /* gpos is the byte index if a zero was found and 16 otherwise.
6739 So if it is lower than the loaded bytes we have a hit. */
6740 emit_cmp_and_jump_insns (gpos, loadlen, GE, NULL_RTX, SImode, 1,
6741 full_label);
6742 emit_insn (gen_vstlv16qi (vsrc, gpos, dst));
6743
6744 force_expand_binop (Pmode, add_optab, dst_addr, gpos, result,
6745 1, OPTAB_DIRECT);
6746 emit_jump (exit_label);
6747 emit_barrier ();
6748
6749 emit_label (full_label);
6750 LABEL_NUSES (full_label) = 1;
6751
6752 /* Calculate `offset' so that src + offset points to the last byte
6753 before 16 byte alignment. */
6754
6755 /* temp = src_addr & 0xf */
6756 force_expand_binop (Pmode, and_optab, src_addr, GEN_INT (15), temp,
6757 1, OPTAB_DIRECT);
6758
6759 /* offset = 0xf - temp */
6760 emit_move_insn (offset, GEN_INT (15));
6761 force_expand_binop (Pmode, sub_optab, offset, temp, offset,
6762 1, OPTAB_DIRECT);
6763
6764 /* Store `offset' bytes in the dstination string. The quick check
6765 has loaded at least `offset' bytes into vsrc. */
6766
6767 emit_insn (gen_vstlv16qi (vsrc, gen_lowpart (SImode, offset), dst));
6768
6769 /* Advance to the next byte to be loaded. */
6770 force_expand_binop (Pmode, add_optab, offset, const1_rtx, offset,
6771 1, OPTAB_DIRECT);
6772
6773 /* Make sure the addresses are single regs which can be used as a
6774 base. */
6775 emit_move_insn (src_addr_reg, src_addr);
6776 emit_move_insn (dst_addr_reg, dst_addr);
6777
6778 /* MAIN LOOP */
6779
6780 emit_label (loop_label);
6781 LABEL_NUSES (loop_label) = 1;
6782
6783 emit_move_insn (vsrc,
6784 gen_rtx_MEM (V16QImode,
6785 gen_rtx_PLUS (Pmode, src_addr_reg, offset)));
6786
6787 emit_insn (gen_vec_vfenesv16qi (vpos, vsrc, vsrc,
6788 GEN_INT (VSTRING_FLAG_ZS | VSTRING_FLAG_CS)));
6789 add_int_reg_note (s390_emit_ccraw_jump (8, EQ, done_label),
6790 REG_BR_PROB, profile_probability::very_unlikely ()
6791 .to_reg_br_prob_note ());
6792
6793 emit_move_insn (gen_rtx_MEM (V16QImode,
6794 gen_rtx_PLUS (Pmode, dst_addr_reg, offset)),
6795 vsrc);
6796 /* offset += 16 */
6797 force_expand_binop (Pmode, add_optab, offset, GEN_INT (16),
6798 offset, 1, OPTAB_DIRECT);
6799
6800 emit_jump (loop_label);
6801 emit_barrier ();
6802
6803 /* REGULAR EXIT */
6804
6805 /* We are done. Add the offset of the zero character to the dst_addr
6806 pointer to get the result. */
6807
6808 emit_label (done_label);
6809 LABEL_NUSES (done_label) = 1;
6810
6811 force_expand_binop (Pmode, add_optab, dst_addr_reg, offset, dst_addr_reg,
6812 1, OPTAB_DIRECT);
6813
6814 emit_insn (gen_vec_extractv16qiqi (gpos_qi, vpos, GEN_INT (7)));
6815 emit_move_insn (gpos, gen_rtx_SUBREG (SImode, gpos_qi, 0));
6816
6817 emit_insn (gen_vstlv16qi (vsrc, gpos, gen_rtx_MEM (BLKmode, dst_addr_reg)));
6818
6819 force_expand_binop (Pmode, add_optab, dst_addr_reg, gpos, result,
6820 1, OPTAB_DIRECT);
6821
6822 /* EARLY EXIT */
6823
6824 emit_label (exit_label);
6825 LABEL_NUSES (exit_label) = 1;
6826 }
6827
6828
6829 /* Expand conditional increment or decrement using alc/slb instructions.
6830 Should generate code setting DST to either SRC or SRC + INCREMENT,
6831 depending on the result of the comparison CMP_OP0 CMP_CODE CMP_OP1.
6832 Returns true if successful, false otherwise.
6833
6834 That makes it possible to implement some if-constructs without jumps e.g.:
6835 (borrow = CC0 | CC1 and carry = CC2 | CC3)
6836 unsigned int a, b, c;
6837 if (a < b) c++; -> CCU b > a -> CC2; c += carry;
6838 if (a < b) c--; -> CCL3 a - b -> borrow; c -= borrow;
6839 if (a <= b) c++; -> CCL3 b - a -> borrow; c += carry;
6840 if (a <= b) c--; -> CCU a <= b -> borrow; c -= borrow;
6841
6842 Checks for EQ and NE with a nonzero value need an additional xor e.g.:
6843 if (a == b) c++; -> CCL3 a ^= b; 0 - a -> borrow; c += carry;
6844 if (a == b) c--; -> CCU a ^= b; a <= 0 -> CC0 | CC1; c -= borrow;
6845 if (a != b) c++; -> CCU a ^= b; a > 0 -> CC2; c += carry;
6846 if (a != b) c--; -> CCL3 a ^= b; 0 - a -> borrow; c -= borrow; */
6847
6848 bool
6849 s390_expand_addcc (enum rtx_code cmp_code, rtx cmp_op0, rtx cmp_op1,
6850 rtx dst, rtx src, rtx increment)
6851 {
6852 machine_mode cmp_mode;
6853 machine_mode cc_mode;
6854 rtx op_res;
6855 rtx insn;
6856 rtvec p;
6857 int ret;
6858
6859 if ((GET_MODE (cmp_op0) == SImode || GET_MODE (cmp_op0) == VOIDmode)
6860 && (GET_MODE (cmp_op1) == SImode || GET_MODE (cmp_op1) == VOIDmode))
6861 cmp_mode = SImode;
6862 else if ((GET_MODE (cmp_op0) == DImode || GET_MODE (cmp_op0) == VOIDmode)
6863 && (GET_MODE (cmp_op1) == DImode || GET_MODE (cmp_op1) == VOIDmode))
6864 cmp_mode = DImode;
6865 else
6866 return false;
6867
6868 /* Try ADD LOGICAL WITH CARRY. */
6869 if (increment == const1_rtx)
6870 {
6871 /* Determine CC mode to use. */
6872 if (cmp_code == EQ || cmp_code == NE)
6873 {
6874 if (cmp_op1 != const0_rtx)
6875 {
6876 cmp_op0 = expand_simple_binop (cmp_mode, XOR, cmp_op0, cmp_op1,
6877 NULL_RTX, 0, OPTAB_WIDEN);
6878 cmp_op1 = const0_rtx;
6879 }
6880
6881 cmp_code = cmp_code == EQ ? LEU : GTU;
6882 }
6883
6884 if (cmp_code == LTU || cmp_code == LEU)
6885 {
6886 rtx tem = cmp_op0;
6887 cmp_op0 = cmp_op1;
6888 cmp_op1 = tem;
6889 cmp_code = swap_condition (cmp_code);
6890 }
6891
6892 switch (cmp_code)
6893 {
6894 case GTU:
6895 cc_mode = CCUmode;
6896 break;
6897
6898 case GEU:
6899 cc_mode = CCL3mode;
6900 break;
6901
6902 default:
6903 return false;
6904 }
6905
6906 /* Emit comparison instruction pattern. */
6907 if (!register_operand (cmp_op0, cmp_mode))
6908 cmp_op0 = force_reg (cmp_mode, cmp_op0);
6909
6910 insn = gen_rtx_SET (gen_rtx_REG (cc_mode, CC_REGNUM),
6911 gen_rtx_COMPARE (cc_mode, cmp_op0, cmp_op1));
6912 /* We use insn_invalid_p here to add clobbers if required. */
6913 ret = insn_invalid_p (emit_insn (insn), false);
6914 gcc_assert (!ret);
6915
6916 /* Emit ALC instruction pattern. */
6917 op_res = gen_rtx_fmt_ee (cmp_code, GET_MODE (dst),
6918 gen_rtx_REG (cc_mode, CC_REGNUM),
6919 const0_rtx);
6920
6921 if (src != const0_rtx)
6922 {
6923 if (!register_operand (src, GET_MODE (dst)))
6924 src = force_reg (GET_MODE (dst), src);
6925
6926 op_res = gen_rtx_PLUS (GET_MODE (dst), op_res, src);
6927 op_res = gen_rtx_PLUS (GET_MODE (dst), op_res, const0_rtx);
6928 }
6929
6930 p = rtvec_alloc (2);
6931 RTVEC_ELT (p, 0) =
6932 gen_rtx_SET (dst, op_res);
6933 RTVEC_ELT (p, 1) =
6934 gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
6935 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
6936
6937 return true;
6938 }
6939
6940 /* Try SUBTRACT LOGICAL WITH BORROW. */
6941 if (increment == constm1_rtx)
6942 {
6943 /* Determine CC mode to use. */
6944 if (cmp_code == EQ || cmp_code == NE)
6945 {
6946 if (cmp_op1 != const0_rtx)
6947 {
6948 cmp_op0 = expand_simple_binop (cmp_mode, XOR, cmp_op0, cmp_op1,
6949 NULL_RTX, 0, OPTAB_WIDEN);
6950 cmp_op1 = const0_rtx;
6951 }
6952
6953 cmp_code = cmp_code == EQ ? LEU : GTU;
6954 }
6955
6956 if (cmp_code == GTU || cmp_code == GEU)
6957 {
6958 rtx tem = cmp_op0;
6959 cmp_op0 = cmp_op1;
6960 cmp_op1 = tem;
6961 cmp_code = swap_condition (cmp_code);
6962 }
6963
6964 switch (cmp_code)
6965 {
6966 case LEU:
6967 cc_mode = CCUmode;
6968 break;
6969
6970 case LTU:
6971 cc_mode = CCL3mode;
6972 break;
6973
6974 default:
6975 return false;
6976 }
6977
6978 /* Emit comparison instruction pattern. */
6979 if (!register_operand (cmp_op0, cmp_mode))
6980 cmp_op0 = force_reg (cmp_mode, cmp_op0);
6981
6982 insn = gen_rtx_SET (gen_rtx_REG (cc_mode, CC_REGNUM),
6983 gen_rtx_COMPARE (cc_mode, cmp_op0, cmp_op1));
6984 /* We use insn_invalid_p here to add clobbers if required. */
6985 ret = insn_invalid_p (emit_insn (insn), false);
6986 gcc_assert (!ret);
6987
6988 /* Emit SLB instruction pattern. */
6989 if (!register_operand (src, GET_MODE (dst)))
6990 src = force_reg (GET_MODE (dst), src);
6991
6992 op_res = gen_rtx_MINUS (GET_MODE (dst),
6993 gen_rtx_MINUS (GET_MODE (dst), src, const0_rtx),
6994 gen_rtx_fmt_ee (cmp_code, GET_MODE (dst),
6995 gen_rtx_REG (cc_mode, CC_REGNUM),
6996 const0_rtx));
6997 p = rtvec_alloc (2);
6998 RTVEC_ELT (p, 0) =
6999 gen_rtx_SET (dst, op_res);
7000 RTVEC_ELT (p, 1) =
7001 gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
7002 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
7003
7004 return true;
7005 }
7006
7007 return false;
7008 }
7009
7010 /* Expand code for the insv template. Return true if successful. */
7011
7012 bool
7013 s390_expand_insv (rtx dest, rtx op1, rtx op2, rtx src)
7014 {
7015 int bitsize = INTVAL (op1);
7016 int bitpos = INTVAL (op2);
7017 machine_mode mode = GET_MODE (dest);
7018 machine_mode smode;
7019 int smode_bsize, mode_bsize;
7020 rtx op, clobber;
7021
7022 if (bitsize + bitpos > GET_MODE_BITSIZE (mode))
7023 return false;
7024
7025 /* Just a move. */
7026 if (bitpos == 0
7027 && bitsize == GET_MODE_BITSIZE (GET_MODE (src))
7028 && mode == GET_MODE (src))
7029 {
7030 emit_move_insn (dest, src);
7031 return true;
7032 }
7033
7034 /* Generate INSERT IMMEDIATE (IILL et al). */
7035 /* (set (ze (reg)) (const_int)). */
7036 if (TARGET_ZARCH
7037 && register_operand (dest, word_mode)
7038 && (bitpos % 16) == 0
7039 && (bitsize % 16) == 0
7040 && const_int_operand (src, VOIDmode))
7041 {
7042 HOST_WIDE_INT val = INTVAL (src);
7043 int regpos = bitpos + bitsize;
7044
7045 while (regpos > bitpos)
7046 {
7047 machine_mode putmode;
7048 int putsize;
7049
7050 if (TARGET_EXTIMM && (regpos % 32 == 0) && (regpos >= bitpos + 32))
7051 putmode = SImode;
7052 else
7053 putmode = HImode;
7054
7055 putsize = GET_MODE_BITSIZE (putmode);
7056 regpos -= putsize;
7057 emit_move_insn (gen_rtx_ZERO_EXTRACT (word_mode, dest,
7058 GEN_INT (putsize),
7059 GEN_INT (regpos)),
7060 gen_int_mode (val, putmode));
7061 val >>= putsize;
7062 }
7063 gcc_assert (regpos == bitpos);
7064 return true;
7065 }
7066
7067 smode = smallest_int_mode_for_size (bitsize).require ();
7068 smode_bsize = GET_MODE_BITSIZE (smode);
7069 mode_bsize = GET_MODE_BITSIZE (mode);
7070
7071 /* Generate STORE CHARACTERS UNDER MASK (STCM et al). */
7072 if (bitpos == 0
7073 && (bitsize % BITS_PER_UNIT) == 0
7074 && MEM_P (dest)
7075 && (register_operand (src, word_mode)
7076 || const_int_operand (src, VOIDmode)))
7077 {
7078 /* Emit standard pattern if possible. */
7079 if (smode_bsize == bitsize)
7080 {
7081 emit_move_insn (adjust_address (dest, smode, 0),
7082 gen_lowpart (smode, src));
7083 return true;
7084 }
7085
7086 /* (set (ze (mem)) (const_int)). */
7087 else if (const_int_operand (src, VOIDmode))
7088 {
7089 int size = bitsize / BITS_PER_UNIT;
7090 rtx src_mem = adjust_address (force_const_mem (word_mode, src),
7091 BLKmode,
7092 UNITS_PER_WORD - size);
7093
7094 dest = adjust_address (dest, BLKmode, 0);
7095 set_mem_size (dest, size);
7096 rtx size_rtx = GEN_INT (size);
7097 s390_expand_cpymem (dest, src_mem, size_rtx, size_rtx, size_rtx);
7098 return true;
7099 }
7100
7101 /* (set (ze (mem)) (reg)). */
7102 else if (register_operand (src, word_mode))
7103 {
7104 if (bitsize <= 32)
7105 emit_move_insn (gen_rtx_ZERO_EXTRACT (word_mode, dest, op1,
7106 const0_rtx), src);
7107 else
7108 {
7109 /* Emit st,stcmh sequence. */
7110 int stcmh_width = bitsize - 32;
7111 int size = stcmh_width / BITS_PER_UNIT;
7112
7113 emit_move_insn (adjust_address (dest, SImode, size),
7114 gen_lowpart (SImode, src));
7115 set_mem_size (dest, size);
7116 emit_move_insn (gen_rtx_ZERO_EXTRACT (word_mode, dest,
7117 GEN_INT (stcmh_width),
7118 const0_rtx),
7119 gen_rtx_LSHIFTRT (word_mode, src, GEN_INT (32)));
7120 }
7121 return true;
7122 }
7123 }
7124
7125 /* Generate INSERT CHARACTERS UNDER MASK (IC, ICM et al). */
7126 if ((bitpos % BITS_PER_UNIT) == 0
7127 && (bitsize % BITS_PER_UNIT) == 0
7128 && (bitpos & 32) == ((bitpos + bitsize - 1) & 32)
7129 && MEM_P (src)
7130 && (mode == DImode || mode == SImode)
7131 && mode != smode
7132 && register_operand (dest, mode))
7133 {
7134 /* Emit a strict_low_part pattern if possible. */
7135 if (smode_bsize == bitsize && bitpos == mode_bsize - smode_bsize)
7136 {
7137 rtx low_dest = gen_lowpart (smode, dest);
7138 if (SUBREG_P (low_dest) && !paradoxical_subreg_p (low_dest))
7139 {
7140 poly_int64 offset = GET_MODE_SIZE (mode) - GET_MODE_SIZE (smode);
7141 rtx low_src = adjust_address (src, smode, offset);
7142 switch (smode)
7143 {
7144 case E_QImode: emit_insn (gen_movstrictqi (low_dest, low_src));
7145 return true;
7146 case E_HImode: emit_insn (gen_movstricthi (low_dest, low_src));
7147 return true;
7148 case E_SImode: emit_insn (gen_movstrictsi (low_dest, low_src));
7149 return true;
7150 default: break;
7151 }
7152 }
7153 }
7154
7155 /* ??? There are more powerful versions of ICM that are not
7156 completely represented in the md file. */
7157 }
7158
7159 /* For z10, generate ROTATE THEN INSERT SELECTED BITS (RISBG et al). */
7160 if (TARGET_Z10 && (mode == DImode || mode == SImode))
7161 {
7162 machine_mode mode_s = GET_MODE (src);
7163
7164 if (CONSTANT_P (src))
7165 {
7166 /* For constant zero values the representation with AND
7167 appears to be folded in more situations than the (set
7168 (zero_extract) ...).
7169 We only do this when the start and end of the bitfield
7170 remain in the same SImode chunk. That way nihf or nilf
7171 can be used.
7172 The AND patterns might still generate a risbg for this. */
7173 if (src == const0_rtx && bitpos / 32 == (bitpos + bitsize - 1) / 32)
7174 return false;
7175 else
7176 src = force_reg (mode, src);
7177 }
7178 else if (mode_s != mode)
7179 {
7180 gcc_assert (GET_MODE_BITSIZE (mode_s) >= bitsize);
7181 src = force_reg (mode_s, src);
7182 src = gen_lowpart (mode, src);
7183 }
7184
7185 op = gen_rtx_ZERO_EXTRACT (mode, dest, op1, op2),
7186 op = gen_rtx_SET (op, src);
7187
7188 if (!TARGET_ZEC12)
7189 {
7190 clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
7191 op = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clobber));
7192 }
7193 emit_insn (op);
7194
7195 return true;
7196 }
7197
7198 return false;
7199 }
7200
7201 /* A subroutine of s390_expand_cs_hqi and s390_expand_atomic which returns a
7202 register that holds VAL of mode MODE shifted by COUNT bits. */
7203
7204 static inline rtx
7205 s390_expand_mask_and_shift (rtx val, machine_mode mode, rtx count)
7206 {
7207 val = expand_simple_binop (SImode, AND, val, GEN_INT (GET_MODE_MASK (mode)),
7208 NULL_RTX, 1, OPTAB_DIRECT);
7209 return expand_simple_binop (SImode, ASHIFT, val, count,
7210 NULL_RTX, 1, OPTAB_DIRECT);
7211 }
7212
7213 /* Generate a vector comparison COND of CMP_OP1 and CMP_OP2 and store
7214 the result in TARGET. */
7215
7216 void
7217 s390_expand_vec_compare (rtx target, enum rtx_code cond,
7218 rtx cmp_op1, rtx cmp_op2)
7219 {
7220 machine_mode mode = GET_MODE (target);
7221 bool neg_p = false, swap_p = false;
7222 rtx tmp;
7223
7224 if (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_VECTOR_FLOAT)
7225 {
7226 cmp_op2 = force_reg (GET_MODE (cmp_op1), cmp_op2);
7227 switch (cond)
7228 {
7229 /* NE a != b -> !(a == b) */
7230 case NE: cond = EQ; neg_p = true; break;
7231 case UNGT:
7232 emit_insn (gen_vec_cmpungt (target, cmp_op1, cmp_op2));
7233 return;
7234 case UNGE:
7235 emit_insn (gen_vec_cmpunge (target, cmp_op1, cmp_op2));
7236 return;
7237 case LE: cond = GE; swap_p = true; break;
7238 /* UNLE: (a u<= b) -> (b u>= a). */
7239 case UNLE:
7240 emit_insn (gen_vec_cmpunge (target, cmp_op2, cmp_op1));
7241 return;
7242 /* LT: a < b -> b > a */
7243 case LT: cond = GT; swap_p = true; break;
7244 /* UNLT: (a u< b) -> (b u> a). */
7245 case UNLT:
7246 emit_insn (gen_vec_cmpungt (target, cmp_op2, cmp_op1));
7247 return;
7248 case UNEQ:
7249 emit_insn (gen_vec_cmpuneq (target, cmp_op1, cmp_op2));
7250 return;
7251 case LTGT:
7252 emit_insn (gen_vec_cmpltgt (target, cmp_op1, cmp_op2));
7253 return;
7254 case ORDERED:
7255 emit_insn (gen_vec_cmpordered (target, cmp_op1, cmp_op2));
7256 return;
7257 case UNORDERED:
7258 emit_insn (gen_vec_cmpunordered (target, cmp_op1, cmp_op2));
7259 return;
7260 default: break;
7261 }
7262 }
7263 else
7264 {
7265 /* Turn x < 0 into x >> (bits per element - 1) */
7266 if (cond == LT && cmp_op2 == CONST0_RTX (mode))
7267 {
7268 int shift = GET_MODE_BITSIZE (GET_MODE_INNER (mode)) - 1;
7269 rtx res = expand_simple_binop (mode, ASHIFTRT, cmp_op1,
7270 GEN_INT (shift), target,
7271 0, OPTAB_DIRECT);
7272 if (res != target)
7273 emit_move_insn (target, res);
7274 return;
7275 }
7276 cmp_op2 = force_reg (GET_MODE (cmp_op1), cmp_op2);
7277
7278 switch (cond)
7279 {
7280 /* NE: a != b -> !(a == b) */
7281 case NE: cond = EQ; neg_p = true; break;
7282 /* GE: a >= b -> !(b > a) */
7283 case GE: cond = GT; neg_p = true; swap_p = true; break;
7284 /* GEU: a >= b -> !(b > a) */
7285 case GEU: cond = GTU; neg_p = true; swap_p = true; break;
7286 /* LE: a <= b -> !(a > b) */
7287 case LE: cond = GT; neg_p = true; break;
7288 /* LEU: a <= b -> !(a > b) */
7289 case LEU: cond = GTU; neg_p = true; break;
7290 /* LT: a < b -> b > a */
7291 case LT: cond = GT; swap_p = true; break;
7292 /* LTU: a < b -> b > a */
7293 case LTU: cond = GTU; swap_p = true; break;
7294 default: break;
7295 }
7296 }
7297
7298 if (swap_p)
7299 {
7300 tmp = cmp_op1; cmp_op1 = cmp_op2; cmp_op2 = tmp;
7301 }
7302
7303 emit_insn (gen_rtx_SET (target, gen_rtx_fmt_ee (cond,
7304 mode,
7305 cmp_op1, cmp_op2)));
7306 if (neg_p)
7307 emit_insn (gen_rtx_SET (target, gen_rtx_NOT (mode, target)));
7308 }
7309
7310 /* Expand the comparison CODE of CMP1 and CMP2 and copy 1 or 0 into
7311 TARGET if either all (ALL_P is true) or any (ALL_P is false) of the
7312 elements in CMP1 and CMP2 fulfill the comparison.
7313 This function is only used to emit patterns for the vx builtins and
7314 therefore only handles comparison codes required by the
7315 builtins. */
7316 void
7317 s390_expand_vec_compare_cc (rtx target, enum rtx_code code,
7318 rtx cmp1, rtx cmp2, bool all_p)
7319 {
7320 machine_mode cc_producer_mode, cc_consumer_mode, scratch_mode;
7321 rtx tmp_reg = gen_reg_rtx (SImode);
7322 bool swap_p = false;
7323
7324 if (GET_MODE_CLASS (GET_MODE (cmp1)) == MODE_VECTOR_INT
7325 || GET_MODE (cmp1) == TImode)
7326 {
7327 switch (code)
7328 {
7329 case EQ:
7330 case NE:
7331 cc_producer_mode = CCVEQmode;
7332 break;
7333 case GE:
7334 case LT:
7335 code = swap_condition (code);
7336 swap_p = true;
7337 /* fallthrough */
7338 case GT:
7339 case LE:
7340 cc_producer_mode = CCVIHmode;
7341 break;
7342 case GEU:
7343 case LTU:
7344 code = swap_condition (code);
7345 swap_p = true;
7346 /* fallthrough */
7347 case GTU:
7348 case LEU:
7349 cc_producer_mode = CCVIHUmode;
7350 break;
7351 default:
7352 gcc_unreachable ();
7353 }
7354
7355 scratch_mode = GET_MODE (cmp1);
7356 /* These codes represent inverted CC interpretations. Inverting
7357 an ALL CC mode results in an ANY CC mode and the other way
7358 around. Invert the all_p flag here to compensate for
7359 that. */
7360 if (code == NE || code == LE || code == LEU)
7361 all_p = !all_p;
7362
7363 cc_consumer_mode = all_p ? CCVIALLmode : CCVIANYmode;
7364 }
7365 else if (GET_MODE_CLASS (GET_MODE (cmp1)) == MODE_VECTOR_FLOAT)
7366 {
7367 bool inv_p = false;
7368
7369 switch (code)
7370 {
7371 case EQ: cc_producer_mode = CCVEQmode; break;
7372 case NE: cc_producer_mode = CCVEQmode; inv_p = true; break;
7373 case GT: cc_producer_mode = CCVFHmode; break;
7374 case GE: cc_producer_mode = CCVFHEmode; break;
7375 case UNLE: cc_producer_mode = CCVFHmode; inv_p = true; break;
7376 case UNLT: cc_producer_mode = CCVFHEmode; inv_p = true; break;
7377 case LT: cc_producer_mode = CCVFHmode; code = GT; swap_p = true; break;
7378 case LE: cc_producer_mode = CCVFHEmode; code = GE; swap_p = true; break;
7379 default: gcc_unreachable ();
7380 }
7381 scratch_mode = related_int_vector_mode (GET_MODE (cmp1)).require ();
7382
7383 if (inv_p)
7384 all_p = !all_p;
7385
7386 cc_consumer_mode = all_p ? CCVFALLmode : CCVFANYmode;
7387 }
7388 else
7389 gcc_unreachable ();
7390
7391 if (swap_p)
7392 {
7393 rtx tmp = cmp2;
7394 cmp2 = cmp1;
7395 cmp1 = tmp;
7396 }
7397
7398 emit_insn (gen_rtx_PARALLEL (VOIDmode,
7399 gen_rtvec (2, gen_rtx_SET (
7400 gen_rtx_REG (cc_producer_mode, CC_REGNUM),
7401 gen_rtx_COMPARE (cc_producer_mode, cmp1, cmp2)),
7402 gen_rtx_CLOBBER (VOIDmode,
7403 gen_rtx_SCRATCH (scratch_mode)))));
7404 emit_move_insn (target, const0_rtx);
7405 emit_move_insn (tmp_reg, const1_rtx);
7406
7407 emit_move_insn (target,
7408 gen_rtx_IF_THEN_ELSE (SImode,
7409 gen_rtx_fmt_ee (code, VOIDmode,
7410 gen_rtx_REG (cc_consumer_mode, CC_REGNUM),
7411 const0_rtx),
7412 tmp_reg, target));
7413 }
7414
7415 /* Invert the comparison CODE applied to a CC mode. This is only safe
7416 if we know whether there result was created by a floating point
7417 compare or not. For the CCV modes this is encoded as part of the
7418 mode. */
7419 enum rtx_code
7420 s390_reverse_condition (machine_mode mode, enum rtx_code code)
7421 {
7422 /* Reversal of FP compares takes care -- an ordered compare
7423 becomes an unordered compare and vice versa. */
7424 if (mode == CCVFALLmode || mode == CCVFANYmode || mode == CCSFPSmode)
7425 return reverse_condition_maybe_unordered (code);
7426 else if (mode == CCVIALLmode || mode == CCVIANYmode)
7427 return reverse_condition (code);
7428 else
7429 gcc_unreachable ();
7430 }
7431
7432 /* Generate a vector comparison expression loading either elements of
7433 THEN or ELS into TARGET depending on the comparison COND of CMP_OP1
7434 and CMP_OP2. */
7435
7436 void
7437 s390_expand_vcond (rtx target, rtx then, rtx els,
7438 enum rtx_code cond, rtx cmp_op1, rtx cmp_op2)
7439 {
7440 rtx tmp;
7441 machine_mode result_mode;
7442 rtx result_target;
7443
7444 machine_mode target_mode = GET_MODE (target);
7445 machine_mode cmp_mode = GET_MODE (cmp_op1);
7446 rtx op = (cond == LT) ? els : then;
7447
7448 /* Try to optimize x < 0 ? -1 : 0 into (signed) x >> 31
7449 and x < 0 ? 1 : 0 into (unsigned) x >> 31. Likewise
7450 for short and byte (x >> 15 and x >> 7 respectively). */
7451 if ((cond == LT || cond == GE)
7452 && target_mode == cmp_mode
7453 && cmp_op2 == CONST0_RTX (cmp_mode)
7454 && op == CONST0_RTX (target_mode)
7455 && s390_vector_mode_supported_p (target_mode)
7456 && GET_MODE_CLASS (target_mode) == MODE_VECTOR_INT)
7457 {
7458 rtx negop = (cond == LT) ? then : els;
7459
7460 int shift = GET_MODE_BITSIZE (GET_MODE_INNER (target_mode)) - 1;
7461
7462 /* if x < 0 ? 1 : 0 or if x >= 0 ? 0 : 1 */
7463 if (negop == CONST1_RTX (target_mode))
7464 {
7465 rtx res = expand_simple_binop (cmp_mode, LSHIFTRT, cmp_op1,
7466 GEN_INT (shift), target,
7467 1, OPTAB_DIRECT);
7468 if (res != target)
7469 emit_move_insn (target, res);
7470 return;
7471 }
7472
7473 /* if x < 0 ? -1 : 0 or if x >= 0 ? 0 : -1 */
7474 else if (all_ones_operand (negop, target_mode))
7475 {
7476 rtx res = expand_simple_binop (cmp_mode, ASHIFTRT, cmp_op1,
7477 GEN_INT (shift), target,
7478 0, OPTAB_DIRECT);
7479 if (res != target)
7480 emit_move_insn (target, res);
7481 return;
7482 }
7483 }
7484
7485 /* We always use an integral type vector to hold the comparison
7486 result. */
7487 result_mode = related_int_vector_mode (cmp_mode).require ();
7488 result_target = gen_reg_rtx (result_mode);
7489
7490 /* We allow vector immediates as comparison operands that
7491 can be handled by the optimization above but not by the
7492 following code. Hence, force them into registers here. */
7493 if (!REG_P (cmp_op1))
7494 cmp_op1 = force_reg (GET_MODE (cmp_op1), cmp_op1);
7495
7496 s390_expand_vec_compare (result_target, cond, cmp_op1, cmp_op2);
7497
7498 /* If the results are supposed to be either -1 or 0 we are done
7499 since this is what our compare instructions generate anyway. */
7500 if (all_ones_operand (then, GET_MODE (then))
7501 && const0_operand (els, GET_MODE (els)))
7502 {
7503 emit_move_insn (target, gen_rtx_SUBREG (target_mode,
7504 result_target, 0));
7505 return;
7506 }
7507
7508 /* Otherwise we will do a vsel afterwards. */
7509 /* This gets triggered e.g.
7510 with gcc.c-torture/compile/pr53410-1.c */
7511 if (!REG_P (then))
7512 then = force_reg (target_mode, then);
7513
7514 if (!REG_P (els))
7515 els = force_reg (target_mode, els);
7516
7517 tmp = gen_rtx_fmt_ee (EQ, VOIDmode,
7518 result_target,
7519 CONST0_RTX (result_mode));
7520
7521 /* We compared the result against zero above so we have to swap then
7522 and els here. */
7523 tmp = gen_rtx_IF_THEN_ELSE (target_mode, tmp, els, then);
7524
7525 gcc_assert (target_mode == GET_MODE (then));
7526 emit_insn (gen_rtx_SET (target, tmp));
7527 }
7528
7529 /* Emit the RTX necessary to initialize the vector TARGET with values
7530 in VALS. */
7531 void
7532 s390_expand_vec_init (rtx target, rtx vals)
7533 {
7534 machine_mode mode = GET_MODE (target);
7535 machine_mode inner_mode = GET_MODE_INNER (mode);
7536 int n_elts = GET_MODE_NUNITS (mode);
7537 bool all_same = true, all_regs = true, all_const_int = true;
7538 rtx x;
7539 int i;
7540
7541 for (i = 0; i < n_elts; ++i)
7542 {
7543 x = XVECEXP (vals, 0, i);
7544
7545 if (!CONST_INT_P (x))
7546 all_const_int = false;
7547
7548 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
7549 all_same = false;
7550
7551 if (!REG_P (x))
7552 all_regs = false;
7553 }
7554
7555 /* Use vector gen mask or vector gen byte mask or vector replicate immediate
7556 if possible. */
7557 if (all_same && all_const_int)
7558 {
7559 rtx vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
7560 if (XVECEXP (vals, 0, 0) == const0_rtx
7561 || s390_constant_via_vgm_p (vec, NULL, NULL, NULL)
7562 || s390_constant_via_vrepi_p (vec, NULL, NULL)
7563 || s390_constant_via_vgbm_p (vec, NULL))
7564 {
7565 emit_insn (gen_rtx_SET (target, vec));
7566 return;
7567 }
7568 }
7569
7570 /* Use vector replicate instructions. vlrep/vrepi/vrep */
7571 if (all_same)
7572 {
7573 rtx elem = XVECEXP (vals, 0, 0);
7574
7575 /* vec_splats accepts general_operand as source. */
7576 if (!general_operand (elem, GET_MODE (elem)))
7577 elem = force_reg (inner_mode, elem);
7578
7579 emit_insn (gen_rtx_SET (target, gen_rtx_VEC_DUPLICATE (mode, elem)));
7580 return;
7581 }
7582
7583 if (all_regs
7584 && REG_P (target)
7585 && n_elts == 2
7586 && GET_MODE_SIZE (inner_mode) == 8)
7587 {
7588 /* Use vector load pair. */
7589 emit_insn (gen_rtx_SET (target,
7590 gen_rtx_VEC_CONCAT (mode,
7591 XVECEXP (vals, 0, 0),
7592 XVECEXP (vals, 0, 1))));
7593 return;
7594 }
7595
7596 /* Use vector load logical element and zero. */
7597 if (TARGET_VXE && (mode == V4SImode || mode == V4SFmode))
7598 {
7599 bool found = true;
7600
7601 x = XVECEXP (vals, 0, 0);
7602 if (memory_operand (x, inner_mode))
7603 {
7604 for (i = 1; i < n_elts; ++i)
7605 found = found && XVECEXP (vals, 0, i) == const0_rtx;
7606
7607 if (found)
7608 {
7609 machine_mode half_mode = (inner_mode == SFmode
7610 ? V2SFmode : V2SImode);
7611 emit_insn (gen_rtx_SET (target,
7612 gen_rtx_VEC_CONCAT (mode,
7613 gen_rtx_VEC_CONCAT (half_mode,
7614 x,
7615 const0_rtx),
7616 gen_rtx_VEC_CONCAT (half_mode,
7617 const0_rtx,
7618 const0_rtx))));
7619 return;
7620 }
7621 }
7622 }
7623
7624 /* We are about to set the vector elements one by one. Zero out the
7625 full register first in order to help the data flow framework to
7626 detect it as full VR set. */
7627 emit_insn (gen_rtx_SET (target, CONST0_RTX (mode)));
7628
7629 /* Unfortunately the vec_init expander is not allowed to fail. So
7630 we have to implement the fallback ourselves. */
7631 for (i = 0; i < n_elts; i++)
7632 {
7633 rtx elem = XVECEXP (vals, 0, i);
7634 if (!general_operand (elem, GET_MODE (elem)))
7635 elem = force_reg (inner_mode, elem);
7636
7637 if (elem != const0_rtx)
7638 emit_insn (gen_rtx_SET (target,
7639 gen_rtx_UNSPEC (mode,
7640 gen_rtvec (3, elem,
7641 GEN_INT (i), target),
7642 UNSPEC_VEC_SET)));
7643 }
7644 }
7645
7646 /* Return a parallel of constant integers to be used as permutation
7647 vector for a vector merge operation in MODE. If HIGH_P is true the
7648 left-most elements of the source vectors are merged otherwise the
7649 right-most elements. */
7650 rtx
7651 s390_expand_merge_perm_const (machine_mode mode, bool high_p)
7652 {
7653 int nelts = GET_MODE_NUNITS (mode);
7654 rtx perm[16];
7655 int addend = high_p ? 0 : nelts;
7656
7657 for (int i = 0; i < nelts; i++)
7658 perm[i] = GEN_INT ((i + addend) / 2 + (i % 2) * nelts);
7659
7660 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, perm));
7661 }
7662
7663 /* Emit RTL to implement a vector merge operation of SRC1 and SRC2
7664 which creates the result in TARGET. HIGH_P determines whether a
7665 merge hi or lo will be generated. */
7666 void
7667 s390_expand_merge (rtx target, rtx src1, rtx src2, bool high_p)
7668 {
7669 machine_mode mode = GET_MODE (target);
7670 opt_machine_mode opt_mode_2x = mode_for_vector (GET_MODE_INNER (mode),
7671 2 * GET_MODE_NUNITS (mode));
7672 gcc_assert (opt_mode_2x.exists ());
7673 machine_mode mode_double_nelts = opt_mode_2x.require ();
7674 rtx constv = s390_expand_merge_perm_const (mode, high_p);
7675 src1 = force_reg (GET_MODE (src1), src1);
7676 src2 = force_reg (GET_MODE (src2), src2);
7677 rtx x = gen_rtx_VEC_CONCAT (mode_double_nelts, src1, src2);
7678 x = gen_rtx_VEC_SELECT (mode, x, constv);
7679 emit_insn (gen_rtx_SET (target, x));
7680 }
7681
7682 /* Emit a vector constant that contains 1s in each element's sign bit position
7683 and 0s in other positions. MODE is the desired constant's mode. */
7684 extern rtx
7685 s390_build_signbit_mask (machine_mode mode)
7686 {
7687 if (mode == TFmode && TARGET_VXE)
7688 {
7689 wide_int mask_val = wi::set_bit_in_zero (127, 128);
7690 rtx mask = immed_wide_int_const (mask_val, TImode);
7691 return gen_lowpart (TFmode, mask);
7692 }
7693
7694 /* Generate the integral element mask value. */
7695 machine_mode inner_mode = GET_MODE_INNER (mode);
7696 int inner_bitsize = GET_MODE_BITSIZE (inner_mode);
7697 wide_int mask_val = wi::set_bit_in_zero (inner_bitsize - 1, inner_bitsize);
7698
7699 /* Emit the element mask rtx. Use gen_lowpart in order to cast the integral
7700 value to the desired mode. */
7701 machine_mode int_mode = related_int_vector_mode (mode).require ();
7702 rtx mask = immed_wide_int_const (mask_val, GET_MODE_INNER (int_mode));
7703 mask = gen_lowpart (inner_mode, mask);
7704
7705 /* Emit the vector mask rtx by mode the element mask rtx. */
7706 int nunits = GET_MODE_NUNITS (mode);
7707 rtvec v = rtvec_alloc (nunits);
7708 for (int i = 0; i < nunits; i++)
7709 RTVEC_ELT (v, i) = mask;
7710 return gen_rtx_CONST_VECTOR (mode, v);
7711 }
7712
7713 /* Structure to hold the initial parameters for a compare_and_swap operation
7714 in HImode and QImode. */
7715
7716 struct alignment_context
7717 {
7718 rtx memsi; /* SI aligned memory location. */
7719 rtx shift; /* Bit offset with regard to lsb. */
7720 rtx modemask; /* Mask of the HQImode shifted by SHIFT bits. */
7721 rtx modemaski; /* ~modemask */
7722 bool aligned; /* True if memory is aligned, false else. */
7723 };
7724
7725 /* A subroutine of s390_expand_cs_hqi and s390_expand_atomic to initialize
7726 structure AC for transparent simplifying, if the memory alignment is known
7727 to be at least 32bit. MEM is the memory location for the actual operation
7728 and MODE its mode. */
7729
7730 static void
7731 init_alignment_context (struct alignment_context *ac, rtx mem,
7732 machine_mode mode)
7733 {
7734 ac->shift = GEN_INT (GET_MODE_SIZE (SImode) - GET_MODE_SIZE (mode));
7735 ac->aligned = (MEM_ALIGN (mem) >= GET_MODE_BITSIZE (SImode));
7736
7737 if (ac->aligned)
7738 ac->memsi = adjust_address (mem, SImode, 0); /* Memory is aligned. */
7739 else
7740 {
7741 /* Alignment is unknown. */
7742 rtx byteoffset, addr, align;
7743
7744 /* Force the address into a register. */
7745 addr = force_reg (Pmode, XEXP (mem, 0));
7746
7747 /* Align it to SImode. */
7748 align = expand_simple_binop (Pmode, AND, addr,
7749 GEN_INT (-GET_MODE_SIZE (SImode)),
7750 NULL_RTX, 1, OPTAB_DIRECT);
7751 /* Generate MEM. */
7752 ac->memsi = gen_rtx_MEM (SImode, align);
7753 MEM_VOLATILE_P (ac->memsi) = MEM_VOLATILE_P (mem);
7754 set_mem_alias_set (ac->memsi, ALIAS_SET_MEMORY_BARRIER);
7755 set_mem_align (ac->memsi, GET_MODE_BITSIZE (SImode));
7756
7757 /* Calculate shiftcount. */
7758 byteoffset = expand_simple_binop (Pmode, AND, addr,
7759 GEN_INT (GET_MODE_SIZE (SImode) - 1),
7760 NULL_RTX, 1, OPTAB_DIRECT);
7761 /* As we already have some offset, evaluate the remaining distance. */
7762 ac->shift = expand_simple_binop (SImode, MINUS, ac->shift, byteoffset,
7763 NULL_RTX, 1, OPTAB_DIRECT);
7764 }
7765
7766 /* Shift is the byte count, but we need the bitcount. */
7767 ac->shift = expand_simple_binop (SImode, ASHIFT, ac->shift, GEN_INT (3),
7768 NULL_RTX, 1, OPTAB_DIRECT);
7769
7770 /* Calculate masks. */
7771 ac->modemask = expand_simple_binop (SImode, ASHIFT,
7772 GEN_INT (GET_MODE_MASK (mode)),
7773 ac->shift, NULL_RTX, 1, OPTAB_DIRECT);
7774 ac->modemaski = expand_simple_unop (SImode, NOT, ac->modemask,
7775 NULL_RTX, 1);
7776 }
7777
7778 /* A subroutine of s390_expand_cs_hqi. Insert INS into VAL. If possible,
7779 use a single insv insn into SEQ2. Otherwise, put prep insns in SEQ1 and
7780 perform the merge in SEQ2. */
7781
7782 static rtx
7783 s390_two_part_insv (struct alignment_context *ac, rtx *seq1, rtx *seq2,
7784 machine_mode mode, rtx val, rtx ins)
7785 {
7786 rtx tmp;
7787
7788 if (ac->aligned)
7789 {
7790 start_sequence ();
7791 tmp = copy_to_mode_reg (SImode, val);
7792 if (s390_expand_insv (tmp, GEN_INT (GET_MODE_BITSIZE (mode)),
7793 const0_rtx, ins))
7794 {
7795 *seq1 = NULL;
7796 *seq2 = get_insns ();
7797 end_sequence ();
7798 return tmp;
7799 }
7800 end_sequence ();
7801 }
7802
7803 /* Failed to use insv. Generate a two part shift and mask. */
7804 start_sequence ();
7805 tmp = s390_expand_mask_and_shift (ins, mode, ac->shift);
7806 *seq1 = get_insns ();
7807 end_sequence ();
7808
7809 start_sequence ();
7810 tmp = expand_simple_binop (SImode, IOR, tmp, val, NULL_RTX, 1, OPTAB_DIRECT);
7811 *seq2 = get_insns ();
7812 end_sequence ();
7813
7814 return tmp;
7815 }
7816
7817 /* Expand an atomic compare and swap operation for HImode and QImode. MEM is
7818 the memory location, CMP the old value to compare MEM with and NEW_RTX the
7819 value to set if CMP == MEM. */
7820
7821 static void
7822 s390_expand_cs_hqi (machine_mode mode, rtx btarget, rtx vtarget, rtx mem,
7823 rtx cmp, rtx new_rtx, bool is_weak)
7824 {
7825 struct alignment_context ac;
7826 rtx cmpv, newv, val, cc, seq0, seq1, seq2, seq3;
7827 rtx res = gen_reg_rtx (SImode);
7828 rtx_code_label *csloop = NULL, *csend = NULL;
7829
7830 gcc_assert (MEM_P (mem));
7831
7832 init_alignment_context (&ac, mem, mode);
7833
7834 /* Load full word. Subsequent loads are performed by CS. */
7835 val = expand_simple_binop (SImode, AND, ac.memsi, ac.modemaski,
7836 NULL_RTX, 1, OPTAB_DIRECT);
7837
7838 /* Prepare insertions of cmp and new_rtx into the loaded value. When
7839 possible, we try to use insv to make this happen efficiently. If
7840 that fails we'll generate code both inside and outside the loop. */
7841 cmpv = s390_two_part_insv (&ac, &seq0, &seq2, mode, val, cmp);
7842 newv = s390_two_part_insv (&ac, &seq1, &seq3, mode, val, new_rtx);
7843
7844 if (seq0)
7845 emit_insn (seq0);
7846 if (seq1)
7847 emit_insn (seq1);
7848
7849 /* Start CS loop. */
7850 if (!is_weak)
7851 {
7852 /* Begin assuming success. */
7853 emit_move_insn (btarget, const1_rtx);
7854
7855 csloop = gen_label_rtx ();
7856 csend = gen_label_rtx ();
7857 emit_label (csloop);
7858 }
7859
7860 /* val = "<mem>00..0<mem>"
7861 * cmp = "00..0<cmp>00..0"
7862 * new = "00..0<new>00..0"
7863 */
7864
7865 emit_insn (seq2);
7866 emit_insn (seq3);
7867
7868 cc = s390_emit_compare_and_swap (EQ, res, ac.memsi, cmpv, newv, CCZ1mode);
7869 if (is_weak)
7870 emit_insn (gen_cstorecc4 (btarget, cc, XEXP (cc, 0), XEXP (cc, 1)));
7871 else
7872 {
7873 rtx tmp;
7874
7875 /* Jump to end if we're done (likely?). */
7876 s390_emit_jump (csend, cc);
7877
7878 /* Check for changes outside mode, and loop internal if so.
7879 Arrange the moves so that the compare is adjacent to the
7880 branch so that we can generate CRJ. */
7881 tmp = copy_to_reg (val);
7882 force_expand_binop (SImode, and_optab, res, ac.modemaski, val,
7883 1, OPTAB_DIRECT);
7884 cc = s390_emit_compare (VOIDmode, NE, val, tmp);
7885 s390_emit_jump (csloop, cc);
7886
7887 /* Failed. */
7888 emit_move_insn (btarget, const0_rtx);
7889 emit_label (csend);
7890 }
7891
7892 /* Return the correct part of the bitfield. */
7893 convert_move (vtarget, expand_simple_binop (SImode, LSHIFTRT, res, ac.shift,
7894 NULL_RTX, 1, OPTAB_DIRECT), 1);
7895 }
7896
7897 /* Variant of s390_expand_cs for SI, DI and TI modes. */
7898 static void
7899 s390_expand_cs_tdsi (machine_mode mode, rtx btarget, rtx vtarget, rtx mem,
7900 rtx cmp, rtx new_rtx, bool is_weak)
7901 {
7902 rtx output = vtarget;
7903 rtx_code_label *skip_cs_label = NULL;
7904 bool do_const_opt = false;
7905
7906 if (!register_operand (output, mode))
7907 output = gen_reg_rtx (mode);
7908
7909 /* If IS_WEAK is true and the INPUT value is a constant, compare the memory
7910 with the constant first and skip the compare_and_swap because its very
7911 expensive and likely to fail anyway.
7912 Note 1: This is done only for IS_WEAK. C11 allows optimizations that may
7913 cause spurious in that case.
7914 Note 2: It may be useful to do this also for non-constant INPUT.
7915 Note 3: Currently only targets with "load on condition" are supported
7916 (z196 and newer). */
7917
7918 if (TARGET_Z196
7919 && (mode == SImode || mode == DImode))
7920 do_const_opt = (is_weak && CONST_INT_P (cmp));
7921
7922 if (do_const_opt)
7923 {
7924 rtx cc = gen_rtx_REG (CCZmode, CC_REGNUM);
7925
7926 skip_cs_label = gen_label_rtx ();
7927 emit_move_insn (btarget, const0_rtx);
7928 if (CONST_INT_P (cmp) && INTVAL (cmp) == 0)
7929 {
7930 rtvec lt = rtvec_alloc (2);
7931
7932 /* Load-and-test + conditional jump. */
7933 RTVEC_ELT (lt, 0)
7934 = gen_rtx_SET (cc, gen_rtx_COMPARE (CCZmode, mem, cmp));
7935 RTVEC_ELT (lt, 1) = gen_rtx_SET (output, mem);
7936 emit_insn (gen_rtx_PARALLEL (VOIDmode, lt));
7937 }
7938 else
7939 {
7940 emit_move_insn (output, mem);
7941 emit_insn (gen_rtx_SET (cc, gen_rtx_COMPARE (CCZmode, output, cmp)));
7942 }
7943 s390_emit_jump (skip_cs_label, gen_rtx_NE (VOIDmode, cc, const0_rtx));
7944 add_reg_br_prob_note (get_last_insn (),
7945 profile_probability::very_unlikely ());
7946 /* If the jump is not taken, OUTPUT is the expected value. */
7947 cmp = output;
7948 /* Reload newval to a register manually, *after* the compare and jump
7949 above. Otherwise Reload might place it before the jump. */
7950 }
7951 else
7952 cmp = force_reg (mode, cmp);
7953 new_rtx = force_reg (mode, new_rtx);
7954 s390_emit_compare_and_swap (EQ, output, mem, cmp, new_rtx,
7955 (do_const_opt) ? CCZmode : CCZ1mode);
7956 if (skip_cs_label != NULL)
7957 emit_label (skip_cs_label);
7958
7959 /* We deliberately accept non-register operands in the predicate
7960 to ensure the write back to the output operand happens *before*
7961 the store-flags code below. This makes it easier for combine
7962 to merge the store-flags code with a potential test-and-branch
7963 pattern following (immediately!) afterwards. */
7964 if (output != vtarget)
7965 emit_move_insn (vtarget, output);
7966
7967 if (do_const_opt)
7968 {
7969 rtx cc, cond, ite;
7970
7971 /* Do not use gen_cstorecc4 here because it writes either 1 or 0, but
7972 btarget has already been initialized with 0 above. */
7973 cc = gen_rtx_REG (CCZmode, CC_REGNUM);
7974 cond = gen_rtx_EQ (VOIDmode, cc, const0_rtx);
7975 ite = gen_rtx_IF_THEN_ELSE (SImode, cond, const1_rtx, btarget);
7976 emit_insn (gen_rtx_SET (btarget, ite));
7977 }
7978 else
7979 {
7980 rtx cc, cond;
7981
7982 cc = gen_rtx_REG (CCZ1mode, CC_REGNUM);
7983 cond = gen_rtx_EQ (SImode, cc, const0_rtx);
7984 emit_insn (gen_cstorecc4 (btarget, cond, cc, const0_rtx));
7985 }
7986 }
7987
7988 /* Expand an atomic compare and swap operation. MEM is the memory location,
7989 CMP the old value to compare MEM with and NEW_RTX the value to set if
7990 CMP == MEM. */
7991
7992 void
7993 s390_expand_cs (machine_mode mode, rtx btarget, rtx vtarget, rtx mem,
7994 rtx cmp, rtx new_rtx, bool is_weak)
7995 {
7996 switch (mode)
7997 {
7998 case E_TImode:
7999 case E_DImode:
8000 case E_SImode:
8001 s390_expand_cs_tdsi (mode, btarget, vtarget, mem, cmp, new_rtx, is_weak);
8002 break;
8003 case E_HImode:
8004 case E_QImode:
8005 s390_expand_cs_hqi (mode, btarget, vtarget, mem, cmp, new_rtx, is_weak);
8006 break;
8007 default:
8008 gcc_unreachable ();
8009 }
8010 }
8011
8012 /* Expand an atomic_exchange operation simulated with a compare-and-swap loop.
8013 The memory location MEM is set to INPUT. OUTPUT is set to the previous value
8014 of MEM. */
8015
8016 void
8017 s390_expand_atomic_exchange_tdsi (rtx output, rtx mem, rtx input)
8018 {
8019 machine_mode mode = GET_MODE (mem);
8020 rtx_code_label *csloop;
8021
8022 if (TARGET_Z196
8023 && (mode == DImode || mode == SImode)
8024 && CONST_INT_P (input) && INTVAL (input) == 0)
8025 {
8026 emit_move_insn (output, const0_rtx);
8027 if (mode == DImode)
8028 emit_insn (gen_atomic_fetch_anddi (output, mem, const0_rtx, input));
8029 else
8030 emit_insn (gen_atomic_fetch_andsi (output, mem, const0_rtx, input));
8031 return;
8032 }
8033
8034 input = force_reg (mode, input);
8035 emit_move_insn (output, mem);
8036 csloop = gen_label_rtx ();
8037 emit_label (csloop);
8038 s390_emit_jump (csloop, s390_emit_compare_and_swap (NE, output, mem, output,
8039 input, CCZ1mode));
8040 }
8041
8042 /* Expand an atomic operation CODE of mode MODE. MEM is the memory location
8043 and VAL the value to play with. If AFTER is true then store the value
8044 MEM holds after the operation, if AFTER is false then store the value MEM
8045 holds before the operation. If TARGET is zero then discard that value, else
8046 store it to TARGET. */
8047
8048 void
8049 s390_expand_atomic (machine_mode mode, enum rtx_code code,
8050 rtx target, rtx mem, rtx val, bool after)
8051 {
8052 struct alignment_context ac;
8053 rtx cmp;
8054 rtx new_rtx = gen_reg_rtx (SImode);
8055 rtx orig = gen_reg_rtx (SImode);
8056 rtx_code_label *csloop = gen_label_rtx ();
8057
8058 gcc_assert (!target || register_operand (target, VOIDmode));
8059 gcc_assert (MEM_P (mem));
8060
8061 init_alignment_context (&ac, mem, mode);
8062
8063 /* Shift val to the correct bit positions.
8064 Preserve "icm", but prevent "ex icm". */
8065 if (!(ac.aligned && code == SET && MEM_P (val)))
8066 val = s390_expand_mask_and_shift (val, mode, ac.shift);
8067
8068 /* Further preparation insns. */
8069 if (code == PLUS || code == MINUS)
8070 emit_move_insn (orig, val);
8071 else if (code == MULT || code == AND) /* val = "11..1<val>11..1" */
8072 val = expand_simple_binop (SImode, XOR, val, ac.modemaski,
8073 NULL_RTX, 1, OPTAB_DIRECT);
8074
8075 /* Load full word. Subsequent loads are performed by CS. */
8076 cmp = force_reg (SImode, ac.memsi);
8077
8078 /* Start CS loop. */
8079 emit_label (csloop);
8080 emit_move_insn (new_rtx, cmp);
8081
8082 /* Patch new with val at correct position. */
8083 switch (code)
8084 {
8085 case PLUS:
8086 case MINUS:
8087 val = expand_simple_binop (SImode, code, new_rtx, orig,
8088 NULL_RTX, 1, OPTAB_DIRECT);
8089 val = expand_simple_binop (SImode, AND, val, ac.modemask,
8090 NULL_RTX, 1, OPTAB_DIRECT);
8091 /* FALLTHRU */
8092 case SET:
8093 if (ac.aligned && MEM_P (val))
8094 store_bit_field (new_rtx, GET_MODE_BITSIZE (mode), 0,
8095 0, 0, SImode, val, false, false);
8096 else
8097 {
8098 new_rtx = expand_simple_binop (SImode, AND, new_rtx, ac.modemaski,
8099 NULL_RTX, 1, OPTAB_DIRECT);
8100 new_rtx = expand_simple_binop (SImode, IOR, new_rtx, val,
8101 NULL_RTX, 1, OPTAB_DIRECT);
8102 }
8103 break;
8104 case AND:
8105 case IOR:
8106 case XOR:
8107 new_rtx = expand_simple_binop (SImode, code, new_rtx, val,
8108 NULL_RTX, 1, OPTAB_DIRECT);
8109 break;
8110 case MULT: /* NAND */
8111 new_rtx = expand_simple_binop (SImode, AND, new_rtx, val,
8112 NULL_RTX, 1, OPTAB_DIRECT);
8113 new_rtx = expand_simple_binop (SImode, XOR, new_rtx, ac.modemask,
8114 NULL_RTX, 1, OPTAB_DIRECT);
8115 break;
8116 default:
8117 gcc_unreachable ();
8118 }
8119
8120 s390_emit_jump (csloop, s390_emit_compare_and_swap (NE, cmp,
8121 ac.memsi, cmp, new_rtx,
8122 CCZ1mode));
8123
8124 /* Return the correct part of the bitfield. */
8125 if (target)
8126 convert_move (target, expand_simple_binop (SImode, LSHIFTRT,
8127 after ? new_rtx : cmp, ac.shift,
8128 NULL_RTX, 1, OPTAB_DIRECT), 1);
8129 }
8130
8131 /* This is called from dwarf2out.cc via TARGET_ASM_OUTPUT_DWARF_DTPREL.
8132 We need to emit DTP-relative relocations. */
8133
8134 static void s390_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
8135
8136 static void
8137 s390_output_dwarf_dtprel (FILE *file, int size, rtx x)
8138 {
8139 switch (size)
8140 {
8141 case 4:
8142 fputs ("\t.long\t", file);
8143 break;
8144 case 8:
8145 fputs ("\t.quad\t", file);
8146 break;
8147 default:
8148 gcc_unreachable ();
8149 }
8150 output_addr_const (file, x);
8151 fputs ("@DTPOFF", file);
8152 }
8153
8154 /* Return the proper mode for REGNO being represented in the dwarf
8155 unwind table. */
8156 machine_mode
8157 s390_dwarf_frame_reg_mode (int regno)
8158 {
8159 machine_mode save_mode = default_dwarf_frame_reg_mode (regno);
8160
8161 /* Make sure not to return DImode for any GPR with -m31 -mzarch. */
8162 if (GENERAL_REGNO_P (regno))
8163 save_mode = Pmode;
8164
8165 /* The rightmost 64 bits of vector registers are call-clobbered. */
8166 if (GET_MODE_SIZE (save_mode) > 8)
8167 save_mode = DImode;
8168
8169 return save_mode;
8170 }
8171
8172 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
8173 /* Implement TARGET_MANGLE_TYPE. */
8174
8175 static const char *
8176 s390_mangle_type (const_tree type)
8177 {
8178 type = TYPE_MAIN_VARIANT (type);
8179
8180 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
8181 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
8182 return NULL;
8183
8184 if (type == s390_builtin_types[BT_BV16QI]) return "U6__boolc";
8185 if (type == s390_builtin_types[BT_BV8HI]) return "U6__bools";
8186 if (type == s390_builtin_types[BT_BV4SI]) return "U6__booli";
8187 if (type == s390_builtin_types[BT_BV2DI]) return "U6__booll";
8188
8189 if (type == long_double_type_node && TARGET_LONG_DOUBLE_128)
8190 return "g";
8191
8192 /* For all other types, use normal C++ mangling. */
8193 return NULL;
8194 }
8195 #endif
8196
8197 /* In the name of slightly smaller debug output, and to cater to
8198 general assembler lossage, recognize various UNSPEC sequences
8199 and turn them back into a direct symbol reference. */
8200
8201 static rtx
8202 s390_delegitimize_address (rtx orig_x)
8203 {
8204 rtx x, y;
8205
8206 orig_x = delegitimize_mem_from_attrs (orig_x);
8207 x = orig_x;
8208
8209 /* Extract the symbol ref from:
8210 (plus:SI (reg:SI 12 %r12)
8211 (const:SI (unspec:SI [(symbol_ref/f:SI ("*.LC0"))]
8212 UNSPEC_GOTOFF/PLTOFF)))
8213 and
8214 (plus:SI (reg:SI 12 %r12)
8215 (const:SI (plus:SI (unspec:SI [(symbol_ref:SI ("L"))]
8216 UNSPEC_GOTOFF/PLTOFF)
8217 (const_int 4 [0x4])))) */
8218 if (GET_CODE (x) == PLUS
8219 && REG_P (XEXP (x, 0))
8220 && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM
8221 && GET_CODE (XEXP (x, 1)) == CONST)
8222 {
8223 HOST_WIDE_INT offset = 0;
8224
8225 /* The const operand. */
8226 y = XEXP (XEXP (x, 1), 0);
8227
8228 if (GET_CODE (y) == PLUS
8229 && GET_CODE (XEXP (y, 1)) == CONST_INT)
8230 {
8231 offset = INTVAL (XEXP (y, 1));
8232 y = XEXP (y, 0);
8233 }
8234
8235 if (GET_CODE (y) == UNSPEC
8236 && (XINT (y, 1) == UNSPEC_GOTOFF
8237 || XINT (y, 1) == UNSPEC_PLTOFF))
8238 return plus_constant (Pmode, XVECEXP (y, 0, 0), offset);
8239 }
8240
8241 if (GET_CODE (x) == CONST)
8242 {
8243 /* Extract the symbol ref from:
8244 (const:DI (unspec:DI [(symbol_ref:DI ("foo"))]
8245 UNSPEC_PLT/GOTENT)) */
8246
8247 y = XEXP (x, 0);
8248 if (GET_CODE (y) == UNSPEC
8249 && (XINT (y, 1) == UNSPEC_GOTENT
8250 || XINT (y, 1) == UNSPEC_PLT31))
8251 return XVECEXP (y, 0, 0);
8252 else
8253 return orig_x;
8254 }
8255
8256 if (GET_CODE (x) != MEM)
8257 return orig_x;
8258
8259 x = XEXP (x, 0);
8260 if (GET_CODE (x) == PLUS
8261 && GET_CODE (XEXP (x, 1)) == CONST
8262 && GET_CODE (XEXP (x, 0)) == REG
8263 && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
8264 {
8265 y = XEXP (XEXP (x, 1), 0);
8266 if (GET_CODE (y) == UNSPEC
8267 && XINT (y, 1) == UNSPEC_GOT)
8268 y = XVECEXP (y, 0, 0);
8269 else
8270 return orig_x;
8271 }
8272 else if (GET_CODE (x) == CONST)
8273 {
8274 /* Extract the symbol ref from:
8275 (mem:QI (const:DI (unspec:DI [(symbol_ref:DI ("foo"))]
8276 UNSPEC_PLT/GOTENT))) */
8277
8278 y = XEXP (x, 0);
8279 if (GET_CODE (y) == UNSPEC
8280 && (XINT (y, 1) == UNSPEC_GOTENT
8281 || XINT (y, 1) == UNSPEC_PLT31))
8282 y = XVECEXP (y, 0, 0);
8283 else
8284 return orig_x;
8285 }
8286 else
8287 return orig_x;
8288
8289 if (GET_MODE (orig_x) != Pmode)
8290 {
8291 if (GET_MODE (orig_x) == BLKmode)
8292 return orig_x;
8293 y = lowpart_subreg (GET_MODE (orig_x), y, Pmode);
8294 if (y == NULL_RTX)
8295 return orig_x;
8296 }
8297 return y;
8298 }
8299
8300 /* Output operand OP to stdio stream FILE.
8301 OP is an address (register + offset) which is not used to address data;
8302 instead the rightmost bits are interpreted as the value. */
8303
8304 static void
8305 print_addrstyle_operand (FILE *file, rtx op)
8306 {
8307 HOST_WIDE_INT offset;
8308 rtx base;
8309
8310 /* Extract base register and offset. */
8311 if (!s390_decompose_addrstyle_without_index (op, &base, &offset))
8312 gcc_unreachable ();
8313
8314 /* Sanity check. */
8315 if (base)
8316 {
8317 gcc_assert (GET_CODE (base) == REG);
8318 gcc_assert (REGNO (base) < FIRST_PSEUDO_REGISTER);
8319 gcc_assert (REGNO_REG_CLASS (REGNO (base)) == ADDR_REGS);
8320 }
8321
8322 /* Offsets are constricted to twelve bits. */
8323 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset & ((1 << 12) - 1));
8324 if (base)
8325 fprintf (file, "(%s)", reg_names[REGNO (base)]);
8326 }
8327
8328 /* Print the shift count operand OP to FILE.
8329 OP is an address-style operand in a form which
8330 s390_valid_shift_count permits. Subregs and no-op
8331 and-masking of the operand are stripped. */
8332
8333 static void
8334 print_shift_count_operand (FILE *file, rtx op)
8335 {
8336 /* No checking of the and mask required here. */
8337 if (!s390_valid_shift_count (op, 0))
8338 gcc_unreachable ();
8339
8340 while (op && GET_CODE (op) == SUBREG)
8341 op = SUBREG_REG (op);
8342
8343 if (GET_CODE (op) == AND)
8344 op = XEXP (op, 0);
8345
8346 print_addrstyle_operand (file, op);
8347 }
8348
8349 /* Assigns the number of NOP halfwords to be emitted before and after the
8350 function label to *HW_BEFORE and *HW_AFTER. Both pointers must not be NULL.
8351 If hotpatching is disabled for the function, the values are set to zero.
8352 */
8353
8354 static void
8355 s390_function_num_hotpatch_hw (tree decl,
8356 int *hw_before,
8357 int *hw_after)
8358 {
8359 tree attr;
8360
8361 attr = lookup_attribute ("hotpatch", DECL_ATTRIBUTES (decl));
8362
8363 /* Handle the arguments of the hotpatch attribute. The values
8364 specified via attribute might override the cmdline argument
8365 values. */
8366 if (attr)
8367 {
8368 tree args = TREE_VALUE (attr);
8369
8370 *hw_before = TREE_INT_CST_LOW (TREE_VALUE (args));
8371 *hw_after = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args)));
8372 }
8373 else
8374 {
8375 /* Use the values specified by the cmdline arguments. */
8376 *hw_before = s390_hotpatch_hw_before_label;
8377 *hw_after = s390_hotpatch_hw_after_label;
8378 }
8379 }
8380
8381 /* Write the current .machine and .machinemode specification to the assembler
8382 file. */
8383
8384 #ifdef HAVE_AS_MACHINE_MACHINEMODE
8385 static void
8386 s390_asm_output_machine_for_arch (FILE *asm_out_file)
8387 {
8388 fprintf (asm_out_file, "\t.machinemode %s\n",
8389 (TARGET_ZARCH) ? "zarch" : "esa");
8390 fprintf (asm_out_file, "\t.machine \"%s",
8391 processor_table[s390_arch].binutils_name);
8392 if (S390_USE_ARCHITECTURE_MODIFIERS)
8393 {
8394 int cpu_flags;
8395
8396 cpu_flags = processor_flags_table[(int) s390_arch];
8397 if (TARGET_HTM && !(cpu_flags & PF_TX))
8398 fprintf (asm_out_file, "+htm");
8399 else if (!TARGET_HTM && (cpu_flags & PF_TX))
8400 fprintf (asm_out_file, "+nohtm");
8401 if (TARGET_VX && !(cpu_flags & PF_VX))
8402 fprintf (asm_out_file, "+vx");
8403 else if (!TARGET_VX && (cpu_flags & PF_VX))
8404 fprintf (asm_out_file, "+novx");
8405 }
8406 fprintf (asm_out_file, "\"\n");
8407 }
8408
8409 /* Write an extra function header before the very start of the function. */
8410
8411 void
8412 s390_asm_output_function_prefix (FILE *asm_out_file,
8413 const char *fnname ATTRIBUTE_UNUSED)
8414 {
8415 if (DECL_FUNCTION_SPECIFIC_TARGET (current_function_decl) == NULL)
8416 return;
8417 /* Since only the function specific options are saved but not the indications
8418 which options are set, it's too much work here to figure out which options
8419 have actually changed. Thus, generate .machine and .machinemode whenever a
8420 function has the target attribute or pragma. */
8421 fprintf (asm_out_file, "\t.machinemode push\n");
8422 fprintf (asm_out_file, "\t.machine push\n");
8423 s390_asm_output_machine_for_arch (asm_out_file);
8424 }
8425
8426 /* Write an extra function footer after the very end of the function. */
8427
8428 void
8429 s390_asm_declare_function_size (FILE *asm_out_file,
8430 const char *fnname, tree decl)
8431 {
8432 if (!flag_inhibit_size_directive)
8433 ASM_OUTPUT_MEASURED_SIZE (asm_out_file, fnname);
8434 if (DECL_FUNCTION_SPECIFIC_TARGET (decl) == NULL)
8435 return;
8436 fprintf (asm_out_file, "\t.machine pop\n");
8437 fprintf (asm_out_file, "\t.machinemode pop\n");
8438 }
8439 #endif
8440
8441 /* Write the extra assembler code needed to declare a function properly. */
8442
8443 void
8444 s390_asm_output_function_label (FILE *out_file, const char *fname,
8445 tree decl)
8446 {
8447 int hw_before, hw_after;
8448
8449 s390_function_num_hotpatch_hw (decl, &hw_before, &hw_after);
8450 if (hw_before > 0)
8451 {
8452 unsigned int function_alignment;
8453 int i;
8454
8455 /* Add a trampoline code area before the function label and initialize it
8456 with two-byte nop instructions. This area can be overwritten with code
8457 that jumps to a patched version of the function. */
8458 asm_fprintf (out_file, "\tnopr\t%%r0"
8459 "\t# pre-label NOPs for hotpatch (%d halfwords)\n",
8460 hw_before);
8461 for (i = 1; i < hw_before; i++)
8462 fputs ("\tnopr\t%r0\n", out_file);
8463
8464 /* Note: The function label must be aligned so that (a) the bytes of the
8465 following nop do not cross a cacheline boundary, and (b) a jump address
8466 (eight bytes for 64 bit targets, 4 bytes for 32 bit targets) can be
8467 stored directly before the label without crossing a cacheline
8468 boundary. All this is necessary to make sure the trampoline code can
8469 be changed atomically.
8470 This alignment is done automatically using the FOUNCTION_BOUNDARY, but
8471 if there are NOPs before the function label, the alignment is placed
8472 before them. So it is necessary to duplicate the alignment after the
8473 NOPs. */
8474 function_alignment = MAX (8, DECL_ALIGN (decl) / BITS_PER_UNIT);
8475 if (! DECL_USER_ALIGN (decl))
8476 function_alignment
8477 = MAX (function_alignment,
8478 (unsigned int) align_functions.levels[0].get_value ());
8479 fputs ("\t# alignment for hotpatch\n", out_file);
8480 ASM_OUTPUT_ALIGN (out_file, align_functions.levels[0].log);
8481 }
8482
8483 if (S390_USE_TARGET_ATTRIBUTE && TARGET_DEBUG_ARG)
8484 {
8485 asm_fprintf (out_file, "\t# fn:%s ar%d\n", fname, s390_arch);
8486 asm_fprintf (out_file, "\t# fn:%s tu%d\n", fname, s390_tune);
8487 asm_fprintf (out_file, "\t# fn:%s sg%d\n", fname, s390_stack_guard);
8488 asm_fprintf (out_file, "\t# fn:%s ss%d\n", fname, s390_stack_size);
8489 asm_fprintf (out_file, "\t# fn:%s bc%d\n", fname, s390_branch_cost);
8490 asm_fprintf (out_file, "\t# fn:%s wf%d\n", fname,
8491 s390_warn_framesize);
8492 asm_fprintf (out_file, "\t# fn:%s ba%d\n", fname, TARGET_BACKCHAIN);
8493 asm_fprintf (out_file, "\t# fn:%s hd%d\n", fname, TARGET_HARD_DFP);
8494 asm_fprintf (out_file, "\t# fn:%s hf%d\n", fname, !TARGET_SOFT_FLOAT);
8495 asm_fprintf (out_file, "\t# fn:%s ht%d\n", fname, TARGET_OPT_HTM);
8496 asm_fprintf (out_file, "\t# fn:%s vx%d\n", fname, TARGET_OPT_VX);
8497 asm_fprintf (out_file, "\t# fn:%s ps%d\n", fname,
8498 TARGET_PACKED_STACK);
8499 asm_fprintf (out_file, "\t# fn:%s se%d\n", fname, TARGET_SMALL_EXEC);
8500 asm_fprintf (out_file, "\t# fn:%s mv%d\n", fname, TARGET_MVCLE);
8501 asm_fprintf (out_file, "\t# fn:%s zv%d\n", fname, TARGET_ZVECTOR);
8502 asm_fprintf (out_file, "\t# fn:%s wd%d\n", fname,
8503 s390_warn_dynamicstack_p);
8504 }
8505 assemble_function_label_raw (out_file, fname);
8506 if (hw_after > 0)
8507 asm_fprintf (out_file,
8508 "\t# post-label NOPs for hotpatch (%d halfwords)\n",
8509 hw_after);
8510 }
8511
8512 /* Output machine-dependent UNSPECs occurring in address constant X
8513 in assembler syntax to stdio stream FILE. Returns true if the
8514 constant X could be recognized, false otherwise. */
8515
8516 static bool
8517 s390_output_addr_const_extra (FILE *file, rtx x)
8518 {
8519 if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 1)
8520 switch (XINT (x, 1))
8521 {
8522 case UNSPEC_GOTENT:
8523 output_addr_const (file, XVECEXP (x, 0, 0));
8524 fprintf (file, "@GOTENT");
8525 return true;
8526 case UNSPEC_GOT:
8527 output_addr_const (file, XVECEXP (x, 0, 0));
8528 fprintf (file, "@GOT");
8529 return true;
8530 case UNSPEC_GOTOFF:
8531 output_addr_const (file, XVECEXP (x, 0, 0));
8532 fprintf (file, "@GOTOFF");
8533 return true;
8534 case UNSPEC_PLT31:
8535 output_addr_const (file, XVECEXP (x, 0, 0));
8536 fprintf (file, "@PLT");
8537 return true;
8538 case UNSPEC_PLTOFF:
8539 output_addr_const (file, XVECEXP (x, 0, 0));
8540 fprintf (file, "@PLTOFF");
8541 return true;
8542 case UNSPEC_TLSGD:
8543 output_addr_const (file, XVECEXP (x, 0, 0));
8544 fprintf (file, "@TLSGD");
8545 return true;
8546 case UNSPEC_TLSLDM:
8547 assemble_name (file, get_some_local_dynamic_name ());
8548 fprintf (file, "@TLSLDM");
8549 return true;
8550 case UNSPEC_DTPOFF:
8551 output_addr_const (file, XVECEXP (x, 0, 0));
8552 fprintf (file, "@DTPOFF");
8553 return true;
8554 case UNSPEC_NTPOFF:
8555 output_addr_const (file, XVECEXP (x, 0, 0));
8556 fprintf (file, "@NTPOFF");
8557 return true;
8558 case UNSPEC_GOTNTPOFF:
8559 output_addr_const (file, XVECEXP (x, 0, 0));
8560 fprintf (file, "@GOTNTPOFF");
8561 return true;
8562 case UNSPEC_INDNTPOFF:
8563 output_addr_const (file, XVECEXP (x, 0, 0));
8564 fprintf (file, "@INDNTPOFF");
8565 return true;
8566 }
8567
8568 if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 2)
8569 switch (XINT (x, 1))
8570 {
8571 case UNSPEC_POOL_OFFSET:
8572 x = gen_rtx_MINUS (GET_MODE (x), XVECEXP (x, 0, 0), XVECEXP (x, 0, 1));
8573 output_addr_const (file, x);
8574 return true;
8575 }
8576 return false;
8577 }
8578
8579 /* Output address operand ADDR in assembler syntax to
8580 stdio stream FILE. */
8581
8582 void
8583 print_operand_address (FILE *file, rtx addr)
8584 {
8585 struct s390_address ad;
8586 memset (&ad, 0, sizeof (s390_address));
8587
8588 if (s390_loadrelative_operand_p (addr, NULL, NULL))
8589 {
8590 if (!TARGET_Z10)
8591 {
8592 output_operand_lossage ("symbolic memory references are "
8593 "only supported on z10 or later");
8594 return;
8595 }
8596 output_addr_const (file, addr);
8597 return;
8598 }
8599
8600 if (!s390_decompose_address (addr, &ad)
8601 || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
8602 || (ad.indx && !REGNO_OK_FOR_INDEX_P (REGNO (ad.indx))))
8603 output_operand_lossage ("cannot decompose address");
8604
8605 if (ad.disp)
8606 output_addr_const (file, ad.disp);
8607 else
8608 fprintf (file, "0");
8609
8610 if (ad.base && ad.indx)
8611 fprintf (file, "(%s,%s)", reg_names[REGNO (ad.indx)],
8612 reg_names[REGNO (ad.base)]);
8613 else if (ad.base)
8614 fprintf (file, "(%s)", reg_names[REGNO (ad.base)]);
8615 }
8616
8617 /* Output operand X in assembler syntax to stdio stream FILE.
8618 CODE specified the format flag. The following format flags
8619 are recognized:
8620
8621 'A': On z14 or higher: If operand is a mem print the alignment
8622 hint usable with vl/vst prefixed by a comma.
8623 'C': print opcode suffix for branch condition.
8624 'D': print opcode suffix for inverse branch condition.
8625 'E': print opcode suffix for branch on index instruction.
8626 'G': print the size of the operand in bytes.
8627 'J': print tls_load/tls_gdcall/tls_ldcall suffix
8628 'K': print @PLT suffix for branch targets; do not use with larl.
8629 'M': print the second word of a TImode operand.
8630 'N': print the second word of a DImode operand.
8631 'O': print only the displacement of a memory reference or address.
8632 'p': print immediate and element size mask for instruction vrepi
8633 'q': print start/end bit positions and element size mask for instruction vgm
8634 'r': print immediate for instruction vgbm
8635 'R': print only the base register of a memory reference or address.
8636 'S': print S-type memory reference (base+displacement).
8637 'Y': print address style operand without index (e.g. shift count or setmem
8638 operand).
8639
8640 'b': print integer X as if it's an unsigned byte.
8641 'c': print integer X as if it's an signed byte.
8642 'e': "end" contiguous bitmask X in either DImode or vector inner mode.
8643 'f': "end" contiguous bitmask X in SImode.
8644 'h': print integer X as if it's a signed halfword.
8645 'i': print the first nonzero HImode part of X.
8646 'j': print the first HImode part unequal to -1 of X.
8647 'k': print the first nonzero SImode part of X.
8648 'm': print the first SImode part unequal to -1 of X.
8649 'o': print integer X as if it's an unsigned 32bit word.
8650 's': "start" of contiguous bitmask X in either DImode or vector inner mode.
8651 't': CONST_INT: "start" of contiguous bitmask X in SImode.
8652 'x': print integer X as if it's an unsigned halfword.
8653 'v': print register number as vector register (v1 instead of f1).
8654 */
8655
8656 void
8657 print_operand (FILE *file, rtx x, int code)
8658 {
8659 HOST_WIDE_INT ival;
8660
8661 switch (code)
8662 {
8663 case 'A':
8664 if (TARGET_VECTOR_LOADSTORE_ALIGNMENT_HINTS && MEM_P (x))
8665 {
8666 if (MEM_ALIGN (x) >= 128)
8667 fprintf (file, ",4");
8668 else if (MEM_ALIGN (x) == 64)
8669 fprintf (file, ",3");
8670 }
8671 return;
8672 case 'C':
8673 fprintf (file, s390_branch_condition_mnemonic (x, FALSE));
8674 return;
8675
8676 case 'D':
8677 fprintf (file, s390_branch_condition_mnemonic (x, TRUE));
8678 return;
8679
8680 case 'E':
8681 if (GET_CODE (x) == LE)
8682 fprintf (file, "l");
8683 else if (GET_CODE (x) == GT)
8684 fprintf (file, "h");
8685 else
8686 output_operand_lossage ("invalid comparison operator "
8687 "for 'E' output modifier");
8688 return;
8689
8690 case 'J':
8691 if (GET_CODE (x) == SYMBOL_REF)
8692 {
8693 fprintf (file, "%s", ":tls_load:");
8694 output_addr_const (file, x);
8695 }
8696 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSGD)
8697 {
8698 fprintf (file, "%s", ":tls_gdcall:");
8699 output_addr_const (file, XVECEXP (x, 0, 0));
8700 }
8701 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSLDM)
8702 {
8703 fprintf (file, "%s", ":tls_ldcall:");
8704 const char *name = get_some_local_dynamic_name ();
8705 gcc_assert (name);
8706 assemble_name (file, name);
8707 }
8708 else
8709 output_operand_lossage ("invalid reference for 'J' output modifier");
8710 return;
8711
8712 case 'G':
8713 fprintf (file, "%u", GET_MODE_SIZE (GET_MODE (x)));
8714 return;
8715
8716 case 'O':
8717 {
8718 struct s390_address ad;
8719 int ret;
8720
8721 ret = s390_decompose_address (MEM_P (x) ? XEXP (x, 0) : x, &ad);
8722
8723 if (!ret
8724 || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
8725 || ad.indx)
8726 {
8727 output_operand_lossage ("invalid address for 'O' output modifier");
8728 return;
8729 }
8730
8731 if (ad.disp)
8732 output_addr_const (file, ad.disp);
8733 else
8734 fprintf (file, "0");
8735 }
8736 return;
8737
8738 case 'p':
8739 {
8740 machine_mode mode;
8741 short imm;
8742 if (!s390_constant_via_vrepi_p (x, &mode, &imm))
8743 {
8744 output_operand_lossage ("invalid constant for output "
8745 "modifier '%c'", code);
8746 return;
8747 }
8748 switch (mode)
8749 {
8750 case QImode:
8751 fprintf (file, "%i,0", imm);
8752 break;
8753 case HImode:
8754 fprintf (file, "%i,1", imm);
8755 break;
8756 case SImode:
8757 fprintf (file, "%i,2", imm);
8758 break;
8759 case DImode:
8760 fprintf (file, "%i,3", imm);
8761 break;
8762 default:
8763 gcc_unreachable ();
8764 }
8765 }
8766 return;
8767
8768 case 'q':
8769 {
8770 machine_mode mode;
8771 int start, end;
8772 if (!s390_constant_via_vgm_p (x, &mode, &start, &end))
8773 {
8774 output_operand_lossage ("invalid constant for output "
8775 "modifier '%c'", code);
8776 return;
8777 }
8778 switch (mode)
8779 {
8780 case QImode:
8781 fprintf (file, "%u,%u,0", start, end);
8782 break;
8783 case HImode:
8784 fprintf (file, "%u,%u,1", start, end);
8785 break;
8786 case SImode:
8787 fprintf (file, "%u,%u,2", start, end);
8788 break;
8789 case DImode:
8790 fprintf (file, "%u,%u,3", start, end);
8791 break;
8792 default:
8793 gcc_unreachable ();
8794 }
8795 }
8796 return;
8797
8798 case 'r':
8799 {
8800 unsigned mask;
8801 if (!s390_constant_via_vgbm_p (x, &mask))
8802 {
8803 output_operand_lossage ("invalid constant for output "
8804 "modifier '%c'", code);
8805 return;
8806 }
8807 fprintf (file, "%u", mask);
8808 }
8809 return;
8810
8811 case 'R':
8812 {
8813 struct s390_address ad;
8814 int ret;
8815
8816 ret = s390_decompose_address (MEM_P (x) ? XEXP (x, 0) : x, &ad);
8817
8818 if (!ret
8819 || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
8820 || ad.indx)
8821 {
8822 output_operand_lossage ("invalid address for 'R' output modifier");
8823 return;
8824 }
8825
8826 if (ad.base)
8827 fprintf (file, "%s", reg_names[REGNO (ad.base)]);
8828 else
8829 fprintf (file, "0");
8830 }
8831 return;
8832
8833 case 'S':
8834 {
8835 struct s390_address ad;
8836 int ret;
8837
8838 if (!MEM_P (x))
8839 {
8840 output_operand_lossage ("memory reference expected for "
8841 "'S' output modifier");
8842 return;
8843 }
8844 ret = s390_decompose_address (XEXP (x, 0), &ad);
8845
8846 if (!ret
8847 || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
8848 || ad.indx)
8849 {
8850 output_operand_lossage ("invalid address for 'S' output modifier");
8851 return;
8852 }
8853
8854 if (ad.disp)
8855 output_addr_const (file, ad.disp);
8856 else
8857 fprintf (file, "0");
8858
8859 if (ad.base)
8860 fprintf (file, "(%s)", reg_names[REGNO (ad.base)]);
8861 }
8862 return;
8863
8864 case 'N':
8865 if (GET_CODE (x) == REG)
8866 x = gen_rtx_REG (GET_MODE (x), REGNO (x) + 1);
8867 else if (GET_CODE (x) == MEM)
8868 x = change_address (x, VOIDmode,
8869 plus_constant (Pmode, XEXP (x, 0), 4));
8870 else
8871 output_operand_lossage ("register or memory expression expected "
8872 "for 'N' output modifier");
8873 break;
8874
8875 case 'M':
8876 if (GET_CODE (x) == REG)
8877 x = gen_rtx_REG (GET_MODE (x), REGNO (x) + 1);
8878 else if (GET_CODE (x) == MEM)
8879 x = change_address (x, VOIDmode,
8880 plus_constant (Pmode, XEXP (x, 0), 8));
8881 else
8882 output_operand_lossage ("register or memory expression expected "
8883 "for 'M' output modifier");
8884 break;
8885
8886 case 'Y':
8887 print_shift_count_operand (file, x);
8888 return;
8889
8890 case 'K':
8891 /* Append @PLT to both local and non-local symbols in order to support
8892 Linux Kernel livepatching: patches contain individual functions and
8893 are loaded further than 2G away from vmlinux, and therefore they must
8894 call even static functions via PLT. ld will optimize @PLT away for
8895 normal code, and keep it for patches.
8896
8897 @PLT only makes sense for functions, data is taken care of by
8898 -mno-pic-data-is-text-relative. */
8899 if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_FUNCTION_P (x))
8900 fprintf (file, "@PLT");
8901 return;
8902 }
8903
8904 switch (GET_CODE (x))
8905 {
8906 case REG:
8907 /* Print FP regs as fx instead of vx when they are accessed
8908 through non-vector mode. */
8909 if (code == 'v'
8910 || VECTOR_NOFP_REG_P (x)
8911 || (FP_REG_P (x) && VECTOR_MODE_P (GET_MODE (x)))
8912 || (VECTOR_REG_P (x)
8913 && (GET_MODE_SIZE (GET_MODE (x)) /
8914 s390_class_max_nregs (FP_REGS, GET_MODE (x))) > 8))
8915 fprintf (file, "%%v%s", reg_names[REGNO (x)] + 2);
8916 else
8917 fprintf (file, "%s", reg_names[REGNO (x)]);
8918 break;
8919
8920 case MEM:
8921 output_address (GET_MODE (x), XEXP (x, 0));
8922 break;
8923
8924 case CONST:
8925 case CODE_LABEL:
8926 case LABEL_REF:
8927 case SYMBOL_REF:
8928 output_addr_const (file, x);
8929 break;
8930
8931 case CONST_INT:
8932 ival = INTVAL (x);
8933 switch (code)
8934 {
8935 case 0:
8936 break;
8937 case 'b':
8938 ival &= 0xff;
8939 break;
8940 case 'c':
8941 ival = ((ival & 0xff) ^ 0x80) - 0x80;
8942 break;
8943 case 'x':
8944 ival &= 0xffff;
8945 break;
8946 case 'h':
8947 ival = ((ival & 0xffff) ^ 0x8000) - 0x8000;
8948 break;
8949 case 'i':
8950 ival = s390_extract_part (x, HImode, 0);
8951 break;
8952 case 'j':
8953 ival = s390_extract_part (x, HImode, -1);
8954 break;
8955 case 'k':
8956 ival = s390_extract_part (x, SImode, 0);
8957 break;
8958 case 'm':
8959 ival = s390_extract_part (x, SImode, -1);
8960 break;
8961 case 'o':
8962 ival &= 0xffffffff;
8963 break;
8964 case 'e': case 'f':
8965 case 's': case 't':
8966 {
8967 int start, end;
8968 int len;
8969 bool ok;
8970
8971 len = (code == 's' || code == 'e' ? 64 : 32);
8972 ok = s390_contiguous_bitmask_p (ival, true, len, &start, &end);
8973 gcc_assert (ok);
8974 if (code == 's' || code == 't')
8975 ival = start;
8976 else
8977 ival = end;
8978 }
8979 break;
8980 default:
8981 output_operand_lossage ("invalid constant for output modifier '%c'", code);
8982 }
8983 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ival);
8984 break;
8985
8986 case CONST_WIDE_INT:
8987 if (code == 'b')
8988 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
8989 CONST_WIDE_INT_ELT (x, 0) & 0xff);
8990 else if (code == 'x')
8991 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
8992 CONST_WIDE_INT_ELT (x, 0) & 0xffff);
8993 else if (code == 'h')
8994 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
8995 ((CONST_WIDE_INT_ELT (x, 0) & 0xffff) ^ 0x8000) - 0x8000);
8996 else
8997 {
8998 if (code == 0)
8999 output_operand_lossage ("invalid constant - try using "
9000 "an output modifier");
9001 else
9002 output_operand_lossage ("invalid constant for output modifier '%c'",
9003 code);
9004 }
9005 break;
9006 case CONST_VECTOR:
9007 switch (code)
9008 {
9009 case 'h':
9010 gcc_assert (const_vec_duplicate_p (x));
9011 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
9012 ((INTVAL (XVECEXP (x, 0, 0)) & 0xffff) ^ 0x8000) - 0x8000);
9013 break;
9014
9015 default:
9016 output_operand_lossage ("invalid constant vector for output "
9017 "modifier '%c'", code);
9018 }
9019 break;
9020
9021 default:
9022 if (code == 0)
9023 output_operand_lossage ("invalid expression - try using "
9024 "an output modifier");
9025 else
9026 output_operand_lossage ("invalid expression for output "
9027 "modifier '%c'", code);
9028 break;
9029 }
9030 }
9031
9032 /* Target hook for assembling integer objects. We need to define it
9033 here to work a round a bug in some versions of GAS, which couldn't
9034 handle values smaller than INT_MIN when printed in decimal. */
9035
9036 static bool
9037 s390_assemble_integer (rtx x, unsigned int size, int aligned_p)
9038 {
9039 if (size == 8 && aligned_p
9040 && GET_CODE (x) == CONST_INT && INTVAL (x) < INT_MIN)
9041 {
9042 fprintf (asm_out_file, "\t.quad\t" HOST_WIDE_INT_PRINT_HEX "\n",
9043 INTVAL (x));
9044 return true;
9045 }
9046 return default_assemble_integer (x, size, aligned_p);
9047 }
9048
9049 /* Returns true if register REGNO is used for forming
9050 a memory address in expression X. */
9051
9052 static bool
9053 reg_used_in_mem_p (int regno, rtx x)
9054 {
9055 enum rtx_code code = GET_CODE (x);
9056 int i, j;
9057 const char *fmt;
9058
9059 if (code == MEM)
9060 {
9061 if (refers_to_regno_p (regno, XEXP (x, 0)))
9062 return true;
9063 }
9064 else if (code == SET
9065 && GET_CODE (SET_DEST (x)) == PC)
9066 {
9067 if (refers_to_regno_p (regno, SET_SRC (x)))
9068 return true;
9069 }
9070
9071 fmt = GET_RTX_FORMAT (code);
9072 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9073 {
9074 if (fmt[i] == 'e'
9075 && reg_used_in_mem_p (regno, XEXP (x, i)))
9076 return true;
9077
9078 else if (fmt[i] == 'E')
9079 for (j = 0; j < XVECLEN (x, i); j++)
9080 if (reg_used_in_mem_p (regno, XVECEXP (x, i, j)))
9081 return true;
9082 }
9083 return false;
9084 }
9085
9086 /* Returns true if expression DEP_RTX sets an address register
9087 used by instruction INSN to address memory. */
9088
9089 static bool
9090 addr_generation_dependency_p (rtx dep_rtx, rtx_insn *insn)
9091 {
9092 rtx target, pat;
9093
9094 if (NONJUMP_INSN_P (dep_rtx))
9095 dep_rtx = PATTERN (dep_rtx);
9096
9097 if (GET_CODE (dep_rtx) == SET)
9098 {
9099 target = SET_DEST (dep_rtx);
9100 if (GET_CODE (target) == STRICT_LOW_PART)
9101 target = XEXP (target, 0);
9102 while (GET_CODE (target) == SUBREG)
9103 target = SUBREG_REG (target);
9104
9105 if (GET_CODE (target) == REG)
9106 {
9107 int regno = REGNO (target);
9108
9109 if (s390_safe_attr_type (insn) == TYPE_LA)
9110 {
9111 pat = PATTERN (insn);
9112 if (GET_CODE (pat) == PARALLEL)
9113 {
9114 gcc_assert (XVECLEN (pat, 0) == 2);
9115 pat = XVECEXP (pat, 0, 0);
9116 }
9117 gcc_assert (GET_CODE (pat) == SET);
9118 return refers_to_regno_p (regno, SET_SRC (pat));
9119 }
9120 else if (get_attr_atype (insn) == ATYPE_AGEN)
9121 return reg_used_in_mem_p (regno, PATTERN (insn));
9122 }
9123 }
9124 return false;
9125 }
9126
9127 /* Return 1, if dep_insn sets register used in insn in the agen unit. */
9128
9129 int
9130 s390_agen_dep_p (rtx_insn *dep_insn, rtx_insn *insn)
9131 {
9132 rtx dep_rtx = PATTERN (dep_insn);
9133 int i;
9134
9135 if (GET_CODE (dep_rtx) == SET
9136 && addr_generation_dependency_p (dep_rtx, insn))
9137 return 1;
9138 else if (GET_CODE (dep_rtx) == PARALLEL)
9139 {
9140 for (i = 0; i < XVECLEN (dep_rtx, 0); i++)
9141 {
9142 if (addr_generation_dependency_p (XVECEXP (dep_rtx, 0, i), insn))
9143 return 1;
9144 }
9145 }
9146 return 0;
9147 }
9148
9149
9150 /* A C statement (sans semicolon) to update the integer scheduling priority
9151 INSN_PRIORITY (INSN). Increase the priority to execute the INSN earlier,
9152 reduce the priority to execute INSN later. Do not define this macro if
9153 you do not need to adjust the scheduling priorities of insns.
9154
9155 A STD instruction should be scheduled earlier,
9156 in order to use the bypass. */
9157 static int
9158 s390_adjust_priority (rtx_insn *insn, int priority)
9159 {
9160 if (! INSN_P (insn))
9161 return priority;
9162
9163 if (s390_tune <= PROCESSOR_2064_Z900)
9164 return priority;
9165
9166 switch (s390_safe_attr_type (insn))
9167 {
9168 case TYPE_FSTOREDF:
9169 case TYPE_FSTORESF:
9170 priority = priority << 3;
9171 break;
9172 case TYPE_STORE:
9173 case TYPE_STM:
9174 priority = priority << 1;
9175 break;
9176 default:
9177 break;
9178 }
9179 return priority;
9180 }
9181
9182
9183 /* The number of instructions that can be issued per cycle. */
9184
9185 static int
9186 s390_issue_rate (void)
9187 {
9188 switch (s390_tune)
9189 {
9190 case PROCESSOR_2084_Z990:
9191 case PROCESSOR_2094_Z9_109:
9192 case PROCESSOR_2094_Z9_EC:
9193 case PROCESSOR_2817_Z196:
9194 return 3;
9195 case PROCESSOR_2097_Z10:
9196 return 2;
9197 case PROCESSOR_2064_Z900:
9198 /* Starting with EC12 we use the sched_reorder hook to take care
9199 of instruction dispatch constraints. The algorithm only
9200 picks the best instruction and assumes only a single
9201 instruction gets issued per cycle. */
9202 case PROCESSOR_2827_ZEC12:
9203 case PROCESSOR_2964_Z13:
9204 case PROCESSOR_3906_Z14:
9205 case PROCESSOR_8561_Z15:
9206 case PROCESSOR_3931_Z16:
9207 case PROCESSOR_9175_Z17:
9208 default:
9209 return 1;
9210 }
9211 }
9212
9213 static int
9214 s390_first_cycle_multipass_dfa_lookahead (void)
9215 {
9216 return 4;
9217 }
9218
9219 static void
9220 annotate_constant_pool_refs_1 (rtx *x)
9221 {
9222 int i, j;
9223 const char *fmt;
9224
9225 gcc_assert (GET_CODE (*x) != SYMBOL_REF
9226 || !CONSTANT_POOL_ADDRESS_P (*x));
9227
9228 /* Literal pool references can only occur inside a MEM ... */
9229 if (GET_CODE (*x) == MEM)
9230 {
9231 rtx memref = XEXP (*x, 0);
9232
9233 if (GET_CODE (memref) == SYMBOL_REF
9234 && CONSTANT_POOL_ADDRESS_P (memref))
9235 {
9236 rtx base = cfun->machine->base_reg;
9237 rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, memref, base),
9238 UNSPEC_LTREF);
9239
9240 *x = replace_equiv_address (*x, addr);
9241 return;
9242 }
9243
9244 if (GET_CODE (memref) == CONST
9245 && GET_CODE (XEXP (memref, 0)) == PLUS
9246 && GET_CODE (XEXP (XEXP (memref, 0), 1)) == CONST_INT
9247 && GET_CODE (XEXP (XEXP (memref, 0), 0)) == SYMBOL_REF
9248 && CONSTANT_POOL_ADDRESS_P (XEXP (XEXP (memref, 0), 0)))
9249 {
9250 HOST_WIDE_INT off = INTVAL (XEXP (XEXP (memref, 0), 1));
9251 rtx sym = XEXP (XEXP (memref, 0), 0);
9252 rtx base = cfun->machine->base_reg;
9253 rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, sym, base),
9254 UNSPEC_LTREF);
9255
9256 *x = replace_equiv_address (*x, plus_constant (Pmode, addr, off));
9257 return;
9258 }
9259 }
9260
9261 /* ... or a load-address type pattern. */
9262 if (GET_CODE (*x) == SET)
9263 {
9264 rtx addrref = SET_SRC (*x);
9265
9266 if (GET_CODE (addrref) == SYMBOL_REF
9267 && CONSTANT_POOL_ADDRESS_P (addrref))
9268 {
9269 rtx base = cfun->machine->base_reg;
9270 rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, addrref, base),
9271 UNSPEC_LTREF);
9272
9273 SET_SRC (*x) = addr;
9274 return;
9275 }
9276
9277 if (GET_CODE (addrref) == CONST
9278 && GET_CODE (XEXP (addrref, 0)) == PLUS
9279 && GET_CODE (XEXP (XEXP (addrref, 0), 1)) == CONST_INT
9280 && GET_CODE (XEXP (XEXP (addrref, 0), 0)) == SYMBOL_REF
9281 && CONSTANT_POOL_ADDRESS_P (XEXP (XEXP (addrref, 0), 0)))
9282 {
9283 HOST_WIDE_INT off = INTVAL (XEXP (XEXP (addrref, 0), 1));
9284 rtx sym = XEXP (XEXP (addrref, 0), 0);
9285 rtx base = cfun->machine->base_reg;
9286 rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, sym, base),
9287 UNSPEC_LTREF);
9288
9289 SET_SRC (*x) = plus_constant (Pmode, addr, off);
9290 return;
9291 }
9292 }
9293
9294 fmt = GET_RTX_FORMAT (GET_CODE (*x));
9295 for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
9296 {
9297 if (fmt[i] == 'e')
9298 {
9299 annotate_constant_pool_refs_1 (&XEXP (*x, i));
9300 }
9301 else if (fmt[i] == 'E')
9302 {
9303 for (j = 0; j < XVECLEN (*x, i); j++)
9304 annotate_constant_pool_refs_1 (&XVECEXP (*x, i, j));
9305 }
9306 }
9307 }
9308
9309 /* Annotate every literal pool reference in INSN by an UNSPEC_LTREF expression.
9310 Fix up MEMs as required.
9311 Skip insns which support relative addressing, because they do not use a base
9312 register. */
9313
9314 static void
9315 annotate_constant_pool_refs (rtx_insn *insn)
9316 {
9317 if (s390_safe_relative_long_p (insn))
9318 return;
9319 annotate_constant_pool_refs_1 (&PATTERN (insn));
9320 }
9321
9322 static void
9323 find_constant_pool_ref_1 (rtx x, rtx *ref)
9324 {
9325 int i, j;
9326 const char *fmt;
9327
9328 /* Likewise POOL_ENTRY insns. */
9329 if (GET_CODE (x) == UNSPEC_VOLATILE
9330 && XINT (x, 1) == UNSPECV_POOL_ENTRY)
9331 return;
9332
9333 gcc_assert (GET_CODE (x) != SYMBOL_REF
9334 || !CONSTANT_POOL_ADDRESS_P (x));
9335
9336 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_LTREF)
9337 {
9338 rtx sym = XVECEXP (x, 0, 0);
9339 gcc_assert (GET_CODE (sym) == SYMBOL_REF
9340 && CONSTANT_POOL_ADDRESS_P (sym));
9341
9342 if (*ref == NULL_RTX)
9343 *ref = sym;
9344 else
9345 gcc_assert (*ref == sym);
9346
9347 return;
9348 }
9349
9350 fmt = GET_RTX_FORMAT (GET_CODE (x));
9351 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9352 {
9353 if (fmt[i] == 'e')
9354 {
9355 find_constant_pool_ref_1 (XEXP (x, i), ref);
9356 }
9357 else if (fmt[i] == 'E')
9358 {
9359 for (j = 0; j < XVECLEN (x, i); j++)
9360 find_constant_pool_ref_1 (XVECEXP (x, i, j), ref);
9361 }
9362 }
9363 }
9364
9365 /* Find an annotated literal pool symbol referenced in INSN,
9366 and store it at REF. Will abort if INSN contains references to
9367 more than one such pool symbol; multiple references to the same
9368 symbol are allowed, however.
9369
9370 The rtx pointed to by REF must be initialized to NULL_RTX
9371 by the caller before calling this routine.
9372
9373 Skip insns which support relative addressing, because they do not use a base
9374 register. */
9375
9376 static void
9377 find_constant_pool_ref (rtx_insn *insn, rtx *ref)
9378 {
9379 if (s390_safe_relative_long_p (insn))
9380 return;
9381 find_constant_pool_ref_1 (PATTERN (insn), ref);
9382 }
9383
9384 static void
9385 replace_constant_pool_ref_1 (rtx *x, rtx ref, rtx offset)
9386 {
9387 int i, j;
9388 const char *fmt;
9389
9390 gcc_assert (*x != ref);
9391
9392 if (GET_CODE (*x) == UNSPEC
9393 && XINT (*x, 1) == UNSPEC_LTREF
9394 && XVECEXP (*x, 0, 0) == ref)
9395 {
9396 *x = gen_rtx_PLUS (Pmode, XVECEXP (*x, 0, 1), offset);
9397 return;
9398 }
9399
9400 if (GET_CODE (*x) == PLUS
9401 && GET_CODE (XEXP (*x, 1)) == CONST_INT
9402 && GET_CODE (XEXP (*x, 0)) == UNSPEC
9403 && XINT (XEXP (*x, 0), 1) == UNSPEC_LTREF
9404 && XVECEXP (XEXP (*x, 0), 0, 0) == ref)
9405 {
9406 rtx addr = gen_rtx_PLUS (Pmode, XVECEXP (XEXP (*x, 0), 0, 1), offset);
9407 *x = plus_constant (Pmode, addr, INTVAL (XEXP (*x, 1)));
9408 return;
9409 }
9410
9411 fmt = GET_RTX_FORMAT (GET_CODE (*x));
9412 for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
9413 {
9414 if (fmt[i] == 'e')
9415 {
9416 replace_constant_pool_ref_1 (&XEXP (*x, i), ref, offset);
9417 }
9418 else if (fmt[i] == 'E')
9419 {
9420 for (j = 0; j < XVECLEN (*x, i); j++)
9421 replace_constant_pool_ref_1 (&XVECEXP (*x, i, j), ref, offset);
9422 }
9423 }
9424 }
9425
9426 /* Replace every reference to the annotated literal pool
9427 symbol REF in INSN by its base plus OFFSET.
9428 Skip insns which support relative addressing, because they do not use a base
9429 register. */
9430
9431 static void
9432 replace_constant_pool_ref (rtx_insn *insn, rtx ref, rtx offset)
9433 {
9434 if (s390_safe_relative_long_p (insn))
9435 return;
9436 replace_constant_pool_ref_1 (&PATTERN (insn), ref, offset);
9437 }
9438
9439 /* We keep a list of constants which we have to add to internal
9440 constant tables in the middle of large functions. */
9441
9442 static machine_mode constant_modes[] =
9443 {
9444 TFmode, FPRX2mode, TImode, TDmode,
9445 V16QImode, V8HImode, V4SImode, V2DImode, V1TImode,
9446 V4SFmode, V2DFmode, V1TFmode,
9447 DFmode, DImode, DDmode,
9448 V8QImode, V4HImode, V2SImode, V1DImode, V2SFmode, V1DFmode,
9449 SFmode, SImode, SDmode,
9450 V4QImode, V2HImode, V1SImode, V1SFmode,
9451 HImode,
9452 V2QImode, V1HImode,
9453 QImode,
9454 V1QImode
9455 };
9456 #define NR_C_MODES (ARRAY_SIZE (constant_modes))
9457
9458 struct constant
9459 {
9460 struct constant *next;
9461 rtx value;
9462 rtx_code_label *label;
9463 };
9464
9465 struct constant_pool
9466 {
9467 struct constant_pool *next;
9468 rtx_insn *first_insn;
9469 rtx_insn *pool_insn;
9470 bitmap insns;
9471 rtx_insn *emit_pool_after;
9472
9473 struct constant *constants[NR_C_MODES];
9474 struct constant *execute;
9475 rtx_code_label *label;
9476 int size;
9477 };
9478
9479 /* Allocate new constant_pool structure. */
9480
9481 static struct constant_pool *
9482 s390_alloc_pool (void)
9483 {
9484 struct constant_pool *pool;
9485 size_t i;
9486
9487 pool = (struct constant_pool *) xmalloc (sizeof *pool);
9488 pool->next = NULL;
9489 for (i = 0; i < NR_C_MODES; i++)
9490 pool->constants[i] = NULL;
9491
9492 pool->execute = NULL;
9493 pool->label = gen_label_rtx ();
9494 pool->first_insn = NULL;
9495 pool->pool_insn = NULL;
9496 pool->insns = BITMAP_ALLOC (NULL);
9497 pool->size = 0;
9498 pool->emit_pool_after = NULL;
9499
9500 return pool;
9501 }
9502
9503 /* Create new constant pool covering instructions starting at INSN
9504 and chain it to the end of POOL_LIST. */
9505
9506 static struct constant_pool *
9507 s390_start_pool (struct constant_pool **pool_list, rtx_insn *insn)
9508 {
9509 struct constant_pool *pool, **prev;
9510
9511 pool = s390_alloc_pool ();
9512 pool->first_insn = insn;
9513
9514 for (prev = pool_list; *prev; prev = &(*prev)->next)
9515 ;
9516 *prev = pool;
9517
9518 return pool;
9519 }
9520
9521 /* End range of instructions covered by POOL at INSN and emit
9522 placeholder insn representing the pool. */
9523
9524 static void
9525 s390_end_pool (struct constant_pool *pool, rtx_insn *insn)
9526 {
9527 rtx pool_size = GEN_INT (pool->size + 8 /* alignment slop */);
9528
9529 if (!insn)
9530 insn = get_last_insn ();
9531
9532 pool->pool_insn = emit_insn_after (gen_pool (pool_size), insn);
9533 INSN_ADDRESSES_NEW (pool->pool_insn, -1);
9534 }
9535
9536 /* Add INSN to the list of insns covered by POOL. */
9537
9538 static void
9539 s390_add_pool_insn (struct constant_pool *pool, rtx insn)
9540 {
9541 bitmap_set_bit (pool->insns, INSN_UID (insn));
9542 }
9543
9544 /* Return pool out of POOL_LIST that covers INSN. */
9545
9546 static struct constant_pool *
9547 s390_find_pool (struct constant_pool *pool_list, rtx insn)
9548 {
9549 struct constant_pool *pool;
9550
9551 for (pool = pool_list; pool; pool = pool->next)
9552 if (bitmap_bit_p (pool->insns, INSN_UID (insn)))
9553 break;
9554
9555 return pool;
9556 }
9557
9558 /* Add constant VAL of mode MODE to the constant pool POOL. */
9559
9560 static void
9561 s390_add_constant (struct constant_pool *pool, rtx val, machine_mode mode)
9562 {
9563 struct constant *c;
9564 size_t i;
9565
9566 for (i = 0; i < NR_C_MODES; i++)
9567 if (constant_modes[i] == mode)
9568 break;
9569 gcc_assert (i != NR_C_MODES);
9570
9571 for (c = pool->constants[i]; c != NULL; c = c->next)
9572 if (rtx_equal_p (val, c->value))
9573 break;
9574
9575 if (c == NULL)
9576 {
9577 c = (struct constant *) xmalloc (sizeof *c);
9578 c->value = val;
9579 c->label = gen_label_rtx ();
9580 c->next = pool->constants[i];
9581 pool->constants[i] = c;
9582 pool->size += GET_MODE_SIZE (mode);
9583 }
9584 }
9585
9586 /* Return an rtx that represents the offset of X from the start of
9587 pool POOL. */
9588
9589 static rtx
9590 s390_pool_offset (struct constant_pool *pool, rtx x)
9591 {
9592 rtx label;
9593
9594 label = gen_rtx_LABEL_REF (GET_MODE (x), pool->label);
9595 x = gen_rtx_UNSPEC (GET_MODE (x), gen_rtvec (2, x, label),
9596 UNSPEC_POOL_OFFSET);
9597 return gen_rtx_CONST (GET_MODE (x), x);
9598 }
9599
9600 /* Find constant VAL of mode MODE in the constant pool POOL.
9601 Return an RTX describing the distance from the start of
9602 the pool to the location of the new constant. */
9603
9604 static rtx
9605 s390_find_constant (struct constant_pool *pool, rtx val,
9606 machine_mode mode)
9607 {
9608 struct constant *c;
9609 size_t i;
9610
9611 for (i = 0; i < NR_C_MODES; i++)
9612 if (constant_modes[i] == mode)
9613 break;
9614 gcc_assert (i != NR_C_MODES);
9615
9616 for (c = pool->constants[i]; c != NULL; c = c->next)
9617 if (rtx_equal_p (val, c->value))
9618 break;
9619
9620 gcc_assert (c);
9621
9622 return s390_pool_offset (pool, gen_rtx_LABEL_REF (Pmode, c->label));
9623 }
9624
9625 /* Check whether INSN is an execute. Return the label_ref to its
9626 execute target template if so, NULL_RTX otherwise. */
9627
9628 static rtx
9629 s390_execute_label (rtx insn)
9630 {
9631 if (INSN_P (insn)
9632 && GET_CODE (PATTERN (insn)) == PARALLEL
9633 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == UNSPEC
9634 && (XINT (XVECEXP (PATTERN (insn), 0, 0), 1) == UNSPEC_EXECUTE
9635 || XINT (XVECEXP (PATTERN (insn), 0, 0), 1) == UNSPEC_EXECUTE_JUMP))
9636 {
9637 if (XINT (XVECEXP (PATTERN (insn), 0, 0), 1) == UNSPEC_EXECUTE)
9638 return XVECEXP (XVECEXP (PATTERN (insn), 0, 0), 0, 2);
9639 else
9640 {
9641 gcc_assert (JUMP_P (insn));
9642 /* For jump insns as execute target:
9643 - There is one operand less in the parallel (the
9644 modification register of the execute is always 0).
9645 - The execute target label is wrapped into an
9646 if_then_else in order to hide it from jump analysis. */
9647 return XEXP (XVECEXP (XVECEXP (PATTERN (insn), 0, 0), 0, 0), 0);
9648 }
9649 }
9650
9651 return NULL_RTX;
9652 }
9653
9654 /* Find execute target for INSN in the constant pool POOL.
9655 Return an RTX describing the distance from the start of
9656 the pool to the location of the execute target. */
9657
9658 static rtx
9659 s390_find_execute (struct constant_pool *pool, rtx insn)
9660 {
9661 struct constant *c;
9662
9663 for (c = pool->execute; c != NULL; c = c->next)
9664 if (INSN_UID (insn) == INSN_UID (c->value))
9665 break;
9666
9667 gcc_assert (c);
9668
9669 return s390_pool_offset (pool, gen_rtx_LABEL_REF (Pmode, c->label));
9670 }
9671
9672 /* For an execute INSN, extract the execute target template. */
9673
9674 static rtx
9675 s390_execute_target (rtx insn)
9676 {
9677 rtx pattern = PATTERN (insn);
9678 gcc_assert (s390_execute_label (insn));
9679
9680 if (XVECLEN (pattern, 0) == 2)
9681 {
9682 pattern = copy_rtx (XVECEXP (pattern, 0, 1));
9683 }
9684 else
9685 {
9686 rtvec vec = rtvec_alloc (XVECLEN (pattern, 0) - 1);
9687 int i;
9688
9689 for (i = 0; i < XVECLEN (pattern, 0) - 1; i++)
9690 RTVEC_ELT (vec, i) = copy_rtx (XVECEXP (pattern, 0, i + 1));
9691
9692 pattern = gen_rtx_PARALLEL (VOIDmode, vec);
9693 }
9694
9695 return pattern;
9696 }
9697
9698 /* Indicate that INSN cannot be duplicated. This is the case for
9699 execute insns that carry a unique label. */
9700
9701 static bool
9702 s390_cannot_copy_insn_p (rtx_insn *insn)
9703 {
9704 rtx label = s390_execute_label (insn);
9705 return label && label != const0_rtx;
9706 }
9707
9708 /* Dump out the constants in POOL. If REMOTE_LABEL is true,
9709 do not emit the pool base label. */
9710
9711 static void
9712 s390_dump_pool (struct constant_pool *pool, bool remote_label)
9713 {
9714 struct constant *c;
9715 rtx_insn *insn = pool->pool_insn;
9716 size_t i;
9717
9718 /* Switch to rodata section. */
9719 insn = emit_insn_after (gen_pool_section_start (), insn);
9720 INSN_ADDRESSES_NEW (insn, -1);
9721
9722 /* Ensure minimum pool alignment. */
9723 insn = emit_insn_after (gen_pool_align (GEN_INT (8)), insn);
9724 INSN_ADDRESSES_NEW (insn, -1);
9725
9726 /* Emit pool base label. */
9727 if (!remote_label)
9728 {
9729 insn = emit_label_after (pool->label, insn);
9730 INSN_ADDRESSES_NEW (insn, -1);
9731 }
9732
9733 /* Dump constants in descending alignment requirement order,
9734 ensuring proper alignment for every constant. */
9735 for (i = 0; i < NR_C_MODES; i++)
9736 for (c = pool->constants[i]; c; c = c->next)
9737 {
9738 /* Convert UNSPEC_LTREL_OFFSET unspecs to pool-relative references. */
9739 rtx value = copy_rtx (c->value);
9740 if (GET_CODE (value) == CONST
9741 && GET_CODE (XEXP (value, 0)) == UNSPEC
9742 && XINT (XEXP (value, 0), 1) == UNSPEC_LTREL_OFFSET
9743 && XVECLEN (XEXP (value, 0), 0) == 1)
9744 value = s390_pool_offset (pool, XVECEXP (XEXP (value, 0), 0, 0));
9745
9746 insn = emit_label_after (c->label, insn);
9747 INSN_ADDRESSES_NEW (insn, -1);
9748
9749 value = gen_rtx_UNSPEC_VOLATILE (constant_modes[i],
9750 gen_rtvec (1, value),
9751 UNSPECV_POOL_ENTRY);
9752 insn = emit_insn_after (value, insn);
9753 INSN_ADDRESSES_NEW (insn, -1);
9754 }
9755
9756 /* Ensure minimum alignment for instructions. */
9757 insn = emit_insn_after (gen_pool_align (GEN_INT (2)), insn);
9758 INSN_ADDRESSES_NEW (insn, -1);
9759
9760 /* Output in-pool execute template insns. */
9761 for (c = pool->execute; c; c = c->next)
9762 {
9763 insn = emit_label_after (c->label, insn);
9764 INSN_ADDRESSES_NEW (insn, -1);
9765
9766 insn = emit_insn_after (s390_execute_target (c->value), insn);
9767 INSN_ADDRESSES_NEW (insn, -1);
9768 }
9769
9770 /* Switch back to previous section. */
9771 insn = emit_insn_after (gen_pool_section_end (), insn);
9772 INSN_ADDRESSES_NEW (insn, -1);
9773
9774 insn = emit_barrier_after (insn);
9775 INSN_ADDRESSES_NEW (insn, -1);
9776
9777 /* Remove placeholder insn. */
9778 remove_insn (pool->pool_insn);
9779 }
9780
9781 /* Free all memory used by POOL. */
9782
9783 static void
9784 s390_free_pool (struct constant_pool *pool)
9785 {
9786 struct constant *c, *next;
9787 size_t i;
9788
9789 for (i = 0; i < NR_C_MODES; i++)
9790 for (c = pool->constants[i]; c; c = next)
9791 {
9792 next = c->next;
9793 free (c);
9794 }
9795
9796 for (c = pool->execute; c; c = next)
9797 {
9798 next = c->next;
9799 free (c);
9800 }
9801
9802 BITMAP_FREE (pool->insns);
9803 free (pool);
9804 }
9805
9806
9807 /* Collect main literal pool. Return NULL on overflow. */
9808
9809 static struct constant_pool *
9810 s390_mainpool_start (void)
9811 {
9812 struct constant_pool *pool;
9813 rtx_insn *insn;
9814
9815 pool = s390_alloc_pool ();
9816
9817 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9818 {
9819 if (NONJUMP_INSN_P (insn)
9820 && GET_CODE (PATTERN (insn)) == SET
9821 && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC_VOLATILE
9822 && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPECV_MAIN_POOL)
9823 {
9824 /* There might be two main_pool instructions if base_reg
9825 is call-clobbered; one for shrink-wrapped code and one
9826 for the rest. We want to keep the first. */
9827 if (pool->pool_insn)
9828 {
9829 insn = PREV_INSN (insn);
9830 delete_insn (NEXT_INSN (insn));
9831 continue;
9832 }
9833 pool->pool_insn = insn;
9834 }
9835
9836 if (NONJUMP_INSN_P (insn) || CALL_P (insn))
9837 {
9838 rtx pool_ref = NULL_RTX;
9839 find_constant_pool_ref (insn, &pool_ref);
9840 if (pool_ref)
9841 {
9842 rtx constant = get_pool_constant (pool_ref);
9843 machine_mode mode = get_pool_mode (pool_ref);
9844 s390_add_constant (pool, constant, mode);
9845 }
9846 }
9847
9848 /* If hot/cold partitioning is enabled we have to make sure that
9849 the literal pool is emitted in the same section where the
9850 initialization of the literal pool base pointer takes place.
9851 emit_pool_after is only used in the non-overflow case on non
9852 Z cpus where we can emit the literal pool at the end of the
9853 function body within the text section. */
9854 if (NOTE_P (insn)
9855 && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS
9856 && !pool->emit_pool_after)
9857 pool->emit_pool_after = PREV_INSN (insn);
9858 }
9859
9860 gcc_assert (pool->pool_insn || pool->size == 0);
9861
9862 if (pool->size >= 4096)
9863 {
9864 /* We're going to chunkify the pool, so remove the main
9865 pool placeholder insn. */
9866 remove_insn (pool->pool_insn);
9867
9868 s390_free_pool (pool);
9869 pool = NULL;
9870 }
9871
9872 /* If the functions ends with the section where the literal pool
9873 should be emitted set the marker to its end. */
9874 if (pool && !pool->emit_pool_after)
9875 pool->emit_pool_after = get_last_insn ();
9876
9877 return pool;
9878 }
9879
9880 /* POOL holds the main literal pool as collected by s390_mainpool_start.
9881 Modify the current function to output the pool constants as well as
9882 the pool register setup instruction. */
9883
9884 static void
9885 s390_mainpool_finish (struct constant_pool *pool)
9886 {
9887 rtx base_reg = cfun->machine->base_reg;
9888 rtx set;
9889 rtx_insn *insn;
9890
9891 /* If the pool is empty, we're done. */
9892 if (pool->size == 0)
9893 {
9894 /* We don't actually need a base register after all. */
9895 cfun->machine->base_reg = NULL_RTX;
9896
9897 if (pool->pool_insn)
9898 remove_insn (pool->pool_insn);
9899 s390_free_pool (pool);
9900 return;
9901 }
9902
9903 /* We need correct insn addresses. */
9904 shorten_branches (get_insns ());
9905
9906 /* Use a LARL to load the pool register. The pool is
9907 located in the .rodata section, so we emit it after the function. */
9908 set = gen_main_base_64 (base_reg, pool->label);
9909 insn = emit_insn_after (set, pool->pool_insn);
9910 INSN_ADDRESSES_NEW (insn, -1);
9911 remove_insn (pool->pool_insn);
9912
9913 insn = get_last_insn ();
9914 pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
9915 INSN_ADDRESSES_NEW (pool->pool_insn, -1);
9916
9917 s390_dump_pool (pool, 0);
9918
9919 /* Replace all literal pool references. */
9920
9921 for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
9922 {
9923 if (NONJUMP_INSN_P (insn) || CALL_P (insn))
9924 {
9925 rtx addr, pool_ref = NULL_RTX;
9926 find_constant_pool_ref (insn, &pool_ref);
9927 if (pool_ref)
9928 {
9929 if (s390_execute_label (insn))
9930 addr = s390_find_execute (pool, insn);
9931 else
9932 addr = s390_find_constant (pool, get_pool_constant (pool_ref),
9933 get_pool_mode (pool_ref));
9934
9935 replace_constant_pool_ref (insn, pool_ref, addr);
9936 INSN_CODE (insn) = -1;
9937 }
9938 }
9939 }
9940
9941
9942 /* Free the pool. */
9943 s390_free_pool (pool);
9944 }
9945
9946 /* Chunkify the literal pool. */
9947
9948 #define S390_POOL_CHUNK_MIN 0xc00
9949 #define S390_POOL_CHUNK_MAX 0xe00
9950
9951 static struct constant_pool *
9952 s390_chunkify_start (void)
9953 {
9954 struct constant_pool *curr_pool = NULL, *pool_list = NULL;
9955 bitmap far_labels;
9956 rtx_insn *insn;
9957
9958 /* We need correct insn addresses. */
9959
9960 shorten_branches (get_insns ());
9961
9962 /* Scan all insns and move literals to pool chunks. */
9963
9964 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9965 {
9966 if (NONJUMP_INSN_P (insn) || CALL_P (insn))
9967 {
9968 rtx pool_ref = NULL_RTX;
9969 find_constant_pool_ref (insn, &pool_ref);
9970 if (pool_ref)
9971 {
9972 rtx constant = get_pool_constant (pool_ref);
9973 machine_mode mode = get_pool_mode (pool_ref);
9974
9975 if (!curr_pool)
9976 curr_pool = s390_start_pool (&pool_list, insn);
9977
9978 s390_add_constant (curr_pool, constant, mode);
9979 s390_add_pool_insn (curr_pool, insn);
9980 }
9981 }
9982
9983 if (JUMP_P (insn) || JUMP_TABLE_DATA_P (insn) || LABEL_P (insn))
9984 {
9985 if (curr_pool)
9986 s390_add_pool_insn (curr_pool, insn);
9987 }
9988
9989 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION)
9990 continue;
9991
9992 if (!curr_pool
9993 || INSN_ADDRESSES_SIZE () <= (size_t) INSN_UID (insn)
9994 || INSN_ADDRESSES (INSN_UID (insn)) == -1)
9995 continue;
9996
9997 if (curr_pool->size < S390_POOL_CHUNK_MAX)
9998 continue;
9999
10000 s390_end_pool (curr_pool, NULL);
10001 curr_pool = NULL;
10002 }
10003
10004 if (curr_pool)
10005 s390_end_pool (curr_pool, NULL);
10006
10007 /* Find all labels that are branched into
10008 from an insn belonging to a different chunk. */
10009
10010 far_labels = BITMAP_ALLOC (NULL);
10011
10012 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
10013 {
10014 rtx_jump_table_data *table;
10015
10016 /* Labels marked with LABEL_PRESERVE_P can be target
10017 of non-local jumps, so we have to mark them.
10018 The same holds for named labels.
10019
10020 Don't do that, however, if it is the label before
10021 a jump table. */
10022
10023 if (LABEL_P (insn)
10024 && (LABEL_PRESERVE_P (insn) || LABEL_NAME (insn)))
10025 {
10026 rtx_insn *vec_insn = NEXT_INSN (insn);
10027 if (! vec_insn || ! JUMP_TABLE_DATA_P (vec_insn))
10028 bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (insn));
10029 }
10030 /* Check potential targets in a table jump (casesi_jump). */
10031 else if (tablejump_p (insn, NULL, &table))
10032 {
10033 rtx vec_pat = PATTERN (table);
10034 int i, diff_p = GET_CODE (vec_pat) == ADDR_DIFF_VEC;
10035
10036 for (i = 0; i < XVECLEN (vec_pat, diff_p); i++)
10037 {
10038 rtx label = XEXP (XVECEXP (vec_pat, diff_p, i), 0);
10039
10040 if (s390_find_pool (pool_list, label)
10041 != s390_find_pool (pool_list, insn))
10042 bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (label));
10043 }
10044 }
10045 /* If we have a direct jump (conditional or unconditional),
10046 check all potential targets. */
10047 else if (JUMP_P (insn))
10048 {
10049 rtx pat = PATTERN (insn);
10050
10051 if (GET_CODE (pat) == PARALLEL)
10052 pat = XVECEXP (pat, 0, 0);
10053
10054 if (GET_CODE (pat) == SET)
10055 {
10056 rtx label = JUMP_LABEL (insn);
10057 if (label && !ANY_RETURN_P (label))
10058 {
10059 if (s390_find_pool (pool_list, label)
10060 != s390_find_pool (pool_list, insn))
10061 bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (label));
10062 }
10063 }
10064 }
10065 }
10066
10067 /* Insert base register reload insns before every pool. */
10068
10069 for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
10070 {
10071 rtx new_insn = gen_reload_base_64 (cfun->machine->base_reg,
10072 curr_pool->label);
10073 rtx_insn *insn = curr_pool->first_insn;
10074 INSN_ADDRESSES_NEW (emit_insn_before (new_insn, insn), -1);
10075 }
10076
10077 /* Insert base register reload insns at every far label. */
10078
10079 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
10080 if (LABEL_P (insn)
10081 && bitmap_bit_p (far_labels, CODE_LABEL_NUMBER (insn)))
10082 {
10083 struct constant_pool *pool = s390_find_pool (pool_list, insn);
10084 if (pool)
10085 {
10086 rtx new_insn = gen_reload_base_64 (cfun->machine->base_reg,
10087 pool->label);
10088 INSN_ADDRESSES_NEW (emit_insn_after (new_insn, insn), -1);
10089 }
10090 }
10091
10092
10093 BITMAP_FREE (far_labels);
10094
10095
10096 /* Recompute insn addresses. */
10097
10098 init_insn_lengths ();
10099 shorten_branches (get_insns ());
10100
10101 return pool_list;
10102 }
10103
10104 /* POOL_LIST is a chunk list as prepared by s390_chunkify_start.
10105 After we have decided to use this list, finish implementing
10106 all changes to the current function as required. */
10107
10108 static void
10109 s390_chunkify_finish (struct constant_pool *pool_list)
10110 {
10111 struct constant_pool *curr_pool = NULL;
10112 rtx_insn *insn;
10113
10114
10115 /* Replace all literal pool references. */
10116
10117 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
10118 {
10119 curr_pool = s390_find_pool (pool_list, insn);
10120 if (!curr_pool)
10121 continue;
10122
10123 if (NONJUMP_INSN_P (insn) || CALL_P (insn))
10124 {
10125 rtx addr, pool_ref = NULL_RTX;
10126 find_constant_pool_ref (insn, &pool_ref);
10127 if (pool_ref)
10128 {
10129 if (s390_execute_label (insn))
10130 addr = s390_find_execute (curr_pool, insn);
10131 else
10132 addr = s390_find_constant (curr_pool,
10133 get_pool_constant (pool_ref),
10134 get_pool_mode (pool_ref));
10135
10136 replace_constant_pool_ref (insn, pool_ref, addr);
10137 INSN_CODE (insn) = -1;
10138 }
10139 }
10140 }
10141
10142 /* Dump out all literal pools. */
10143
10144 for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
10145 s390_dump_pool (curr_pool, 0);
10146
10147 /* Free pool list. */
10148
10149 while (pool_list)
10150 {
10151 struct constant_pool *next = pool_list->next;
10152 s390_free_pool (pool_list);
10153 pool_list = next;
10154 }
10155 }
10156
10157 /* Output the constant pool entry EXP in mode MODE with alignment ALIGN. */
10158
10159 void
10160 s390_output_pool_entry (rtx exp, machine_mode mode, unsigned int align)
10161 {
10162 switch (GET_MODE_CLASS (mode))
10163 {
10164 case MODE_FLOAT:
10165 case MODE_DECIMAL_FLOAT:
10166 gcc_assert (GET_CODE (exp) == CONST_DOUBLE);
10167
10168 assemble_real (*CONST_DOUBLE_REAL_VALUE (exp),
10169 as_a <scalar_float_mode> (mode), align);
10170 break;
10171
10172 case MODE_INT:
10173 assemble_integer (exp, GET_MODE_SIZE (mode), align, 1);
10174 mark_symbol_refs_as_used (exp);
10175 break;
10176
10177 case MODE_VECTOR_INT:
10178 case MODE_VECTOR_FLOAT:
10179 {
10180 int i;
10181 machine_mode inner_mode;
10182 gcc_assert (GET_CODE (exp) == CONST_VECTOR);
10183
10184 inner_mode = GET_MODE_INNER (GET_MODE (exp));
10185 for (i = 0; i < XVECLEN (exp, 0); i++)
10186 s390_output_pool_entry (XVECEXP (exp, 0, i),
10187 inner_mode,
10188 i == 0
10189 ? align
10190 : GET_MODE_BITSIZE (inner_mode));
10191 }
10192 break;
10193
10194 default:
10195 gcc_unreachable ();
10196 }
10197 }
10198
10199 /* Return true if MEM refers to an integer constant in the literal pool. If
10200 VAL is not nullptr, then also fill it with the constant's value. */
10201
10202 bool
10203 s390_const_int_pool_entry_p (rtx mem, HOST_WIDE_INT *val)
10204 {
10205 /* Try to match the following:
10206 - (mem (unspec [(symbol_ref) (reg)] UNSPEC_LTREF)).
10207 - (mem (symbol_ref)). */
10208
10209 if (!MEM_P (mem) || GET_MODE_CLASS (GET_MODE (mem)) != MODE_INT)
10210 return false;
10211
10212 rtx addr = XEXP (mem, 0);
10213 rtx sym;
10214 if (GET_CODE (addr) == UNSPEC && XINT (addr, 1) == UNSPEC_LTREF)
10215 sym = XVECEXP (addr, 0, 0);
10216 else
10217 sym = addr;
10218
10219 if (!SYMBOL_REF_P (sym) || !CONSTANT_POOL_ADDRESS_P (sym))
10220 return false;
10221
10222 rtx val_rtx = get_pool_constant (sym);
10223 machine_mode mode = get_pool_mode (sym);
10224 if (!CONST_INT_P (val_rtx)
10225 || GET_MODE_CLASS (mode) != MODE_INT
10226 || GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (mem)))
10227 return false;
10228
10229 if (mode != GET_MODE (mem))
10230 {
10231 val_rtx = simplify_subreg (GET_MODE (mem), val_rtx, mode, 0);
10232 if (val_rtx == NULL_RTX || !CONST_INT_P (val_rtx))
10233 return false;
10234 }
10235
10236 if (val != nullptr)
10237 *val = INTVAL (val_rtx);
10238 return true;
10239 }
10240
10241 /* Return an RTL expression representing the value of the return address
10242 for the frame COUNT steps up from the current frame. FRAME is the
10243 frame pointer of that frame. */
10244
10245 rtx
10246 s390_return_addr_rtx (int count, rtx frame ATTRIBUTE_UNUSED)
10247 {
10248 int offset;
10249 rtx addr;
10250
10251 /* Without backchain, we fail for all but the current frame. */
10252
10253 if (!TARGET_BACKCHAIN && count > 0)
10254 return NULL_RTX;
10255
10256 /* For the current frame, we need to make sure the initial
10257 value of RETURN_REGNUM is actually saved. */
10258
10259 if (count == 0)
10260 return get_hard_reg_initial_val (Pmode, RETURN_REGNUM);
10261
10262 if (TARGET_PACKED_STACK)
10263 offset = -2 * UNITS_PER_LONG;
10264 else
10265 offset = RETURN_REGNUM * UNITS_PER_LONG;
10266
10267 addr = plus_constant (Pmode, frame, offset);
10268 addr = memory_address (Pmode, addr);
10269 return gen_rtx_MEM (Pmode, addr);
10270 }
10271
10272 /* Return an RTL expression representing the back chain stored in
10273 the current stack frame. */
10274
10275 rtx
10276 s390_back_chain_rtx (void)
10277 {
10278 rtx chain;
10279
10280 gcc_assert (TARGET_BACKCHAIN);
10281
10282 if (TARGET_PACKED_STACK)
10283 chain = plus_constant (Pmode, stack_pointer_rtx,
10284 STACK_POINTER_OFFSET - UNITS_PER_LONG);
10285 else
10286 chain = stack_pointer_rtx;
10287
10288 chain = gen_rtx_MEM (Pmode, chain);
10289 return chain;
10290 }
10291
10292 /* Find first call clobbered register unused in a function.
10293 This could be used as base register in a leaf function
10294 or for holding the return address before epilogue. */
10295
10296 static int
10297 find_unused_clobbered_reg (void)
10298 {
10299 int i;
10300 for (i = 0; i < 6; i++)
10301 if (!df_regs_ever_live_p (i))
10302 return i;
10303 return 0;
10304 }
10305
10306
10307 /* Helper function for s390_regs_ever_clobbered. Sets the fields in DATA for all
10308 clobbered hard regs in SETREG. */
10309
10310 static void
10311 s390_reg_clobbered_rtx (rtx setreg, const_rtx set_insn ATTRIBUTE_UNUSED, void *data)
10312 {
10313 char *regs_ever_clobbered = (char *)data;
10314 unsigned int i, regno;
10315 machine_mode mode = GET_MODE (setreg);
10316
10317 if (GET_CODE (setreg) == SUBREG)
10318 {
10319 rtx inner = SUBREG_REG (setreg);
10320 if (!GENERAL_REG_P (inner) && !FP_REG_P (inner))
10321 return;
10322 regno = subreg_regno (setreg);
10323 }
10324 else if (GENERAL_REG_P (setreg) || FP_REG_P (setreg))
10325 regno = REGNO (setreg);
10326 else
10327 return;
10328
10329 for (i = regno;
10330 i < end_hard_regno (mode, regno);
10331 i++)
10332 regs_ever_clobbered[i] = 1;
10333 }
10334
10335 /* Walks through all basic blocks of the current function looking
10336 for clobbered hard regs using s390_reg_clobbered_rtx. The fields
10337 of the passed integer array REGS_EVER_CLOBBERED are set to one for
10338 each of those regs. */
10339
10340 static void
10341 s390_regs_ever_clobbered (char regs_ever_clobbered[])
10342 {
10343 basic_block cur_bb;
10344 rtx_insn *cur_insn;
10345 unsigned int i;
10346
10347 memset (regs_ever_clobbered, 0, 32);
10348
10349 /* For non-leaf functions we have to consider all call clobbered regs to be
10350 clobbered. */
10351 if (!crtl->is_leaf)
10352 {
10353 for (i = 0; i < 32; i++)
10354 regs_ever_clobbered[i] = call_used_regs[i];
10355 }
10356
10357 /* Make the "magic" eh_return registers live if necessary. For regs_ever_live
10358 this work is done by liveness analysis (mark_regs_live_at_end).
10359 Special care is needed for functions containing landing pads. Landing pads
10360 may use the eh registers, but the code which sets these registers is not
10361 contained in that function. Hence s390_regs_ever_clobbered is not able to
10362 deal with this automatically. */
10363 if (crtl->calls_eh_return || cfun->machine->has_landing_pad_p)
10364 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM ; i++)
10365 if (crtl->calls_eh_return
10366 || (cfun->machine->has_landing_pad_p
10367 && df_regs_ever_live_p (EH_RETURN_DATA_REGNO (i))))
10368 regs_ever_clobbered[EH_RETURN_DATA_REGNO (i)] = 1;
10369
10370 /* For nonlocal gotos all call-saved registers have to be saved.
10371 This flag is also set for the unwinding code in libgcc.
10372 See expand_builtin_unwind_init. For regs_ever_live this is done by
10373 reload. */
10374 if (crtl->saves_all_registers)
10375 for (i = 0; i < 32; i++)
10376 if (!call_used_regs[i])
10377 regs_ever_clobbered[i] = 1;
10378
10379 FOR_EACH_BB_FN (cur_bb, cfun)
10380 {
10381 FOR_BB_INSNS (cur_bb, cur_insn)
10382 {
10383 rtx pat;
10384
10385 if (!INSN_P (cur_insn))
10386 continue;
10387
10388 pat = PATTERN (cur_insn);
10389
10390 /* Ignore GPR restore insns. */
10391 if (epilogue_completed && RTX_FRAME_RELATED_P (cur_insn))
10392 {
10393 if (GET_CODE (pat) == SET
10394 && GENERAL_REG_P (SET_DEST (pat)))
10395 {
10396 /* lgdr */
10397 if (GET_MODE (SET_SRC (pat)) == DImode
10398 && FP_REG_P (SET_SRC (pat)))
10399 continue;
10400
10401 /* l / lg */
10402 if (GET_CODE (SET_SRC (pat)) == MEM)
10403 continue;
10404 }
10405
10406 /* lm / lmg */
10407 if (GET_CODE (pat) == PARALLEL
10408 && load_multiple_operation (pat, VOIDmode))
10409 continue;
10410 }
10411
10412 note_stores (cur_insn,
10413 s390_reg_clobbered_rtx,
10414 regs_ever_clobbered);
10415 }
10416 }
10417 }
10418
10419 /* Determine the frame area which actually has to be accessed
10420 in the function epilogue. The values are stored at the
10421 given pointers AREA_BOTTOM (address of the lowest used stack
10422 address) and AREA_TOP (address of the first item which does
10423 not belong to the stack frame). */
10424
10425 static void
10426 s390_frame_area (int *area_bottom, int *area_top)
10427 {
10428 int b, t;
10429
10430 b = INT_MAX;
10431 t = INT_MIN;
10432
10433 if (cfun_frame_layout.first_restore_gpr != -1)
10434 {
10435 b = (cfun_frame_layout.gprs_offset
10436 + cfun_frame_layout.first_restore_gpr * UNITS_PER_LONG);
10437 t = b + (cfun_frame_layout.last_restore_gpr
10438 - cfun_frame_layout.first_restore_gpr + 1) * UNITS_PER_LONG;
10439 }
10440
10441 if (TARGET_64BIT && cfun_save_high_fprs_p)
10442 {
10443 b = MIN (b, cfun_frame_layout.f8_offset);
10444 t = MAX (t, (cfun_frame_layout.f8_offset
10445 + cfun_frame_layout.high_fprs * 8));
10446 }
10447
10448 if (!TARGET_64BIT)
10449 {
10450 if (cfun_fpr_save_p (FPR4_REGNUM))
10451 {
10452 b = MIN (b, cfun_frame_layout.f4_offset);
10453 t = MAX (t, cfun_frame_layout.f4_offset + 8);
10454 }
10455 if (cfun_fpr_save_p (FPR6_REGNUM))
10456 {
10457 b = MIN (b, cfun_frame_layout.f4_offset + 8);
10458 t = MAX (t, cfun_frame_layout.f4_offset + 16);
10459 }
10460 }
10461 *area_bottom = b;
10462 *area_top = t;
10463 }
10464 /* Update gpr_save_slots in the frame layout trying to make use of
10465 FPRs as GPR save slots.
10466 This is a helper routine of s390_register_info. */
10467
10468 static void
10469 s390_register_info_gprtofpr ()
10470 {
10471 int save_reg_slot = FPR0_REGNUM;
10472 int i, j;
10473
10474 if (TARGET_TPF || !TARGET_Z10 || !TARGET_HARD_FLOAT || !crtl->is_leaf)
10475 return;
10476
10477 /* builtin_eh_return needs to be able to modify the return address
10478 on the stack. It could also adjust the FPR save slot instead but
10479 is it worth the trouble?! */
10480 if (crtl->calls_eh_return)
10481 return;
10482
10483 for (i = 15; i >= 6; i--)
10484 {
10485 if (cfun_gpr_save_slot (i) == SAVE_SLOT_NONE)
10486 continue;
10487
10488 /* Advance to the next FP register which can be used as a
10489 GPR save slot. */
10490 while ((!call_used_regs[save_reg_slot]
10491 || df_regs_ever_live_p (save_reg_slot)
10492 || cfun_fpr_save_p (save_reg_slot))
10493 && FP_REGNO_P (save_reg_slot))
10494 save_reg_slot++;
10495 if (!FP_REGNO_P (save_reg_slot))
10496 {
10497 /* We only want to use ldgr/lgdr if we can get rid of
10498 stm/lm entirely. So undo the gpr slot allocation in
10499 case we ran out of FPR save slots. */
10500 for (j = 6; j <= 15; j++)
10501 if (FP_REGNO_P (cfun_gpr_save_slot (j)))
10502 cfun_gpr_save_slot (j) = SAVE_SLOT_STACK;
10503 break;
10504 }
10505 cfun_gpr_save_slot (i) = save_reg_slot++;
10506 }
10507 }
10508
10509 /* Set the bits in fpr_bitmap for FPRs which need to be saved due to
10510 stdarg or -mpreserve-args.
10511 This is a helper routine for s390_register_info. */
10512 static void
10513 s390_register_info_arg_fpr ()
10514 {
10515 int i;
10516 int min_stdarg_fpr = INT_MAX, max_stdarg_fpr = -1;
10517 int min_preserve_fpr = INT_MAX, max_preserve_fpr = -1;
10518 int min_fpr, max_fpr;
10519
10520 /* Save the FP argument regs for stdarg. f0, f2 for 31 bit and
10521 f0-f4 for 64 bit. */
10522 if (cfun->stdarg
10523 && TARGET_HARD_FLOAT
10524 && cfun->va_list_fpr_size
10525 && crtl->args.info.fprs < FP_ARG_NUM_REG)
10526 {
10527 min_stdarg_fpr = crtl->args.info.fprs;
10528 max_stdarg_fpr = min_stdarg_fpr + cfun->va_list_fpr_size - 1;
10529 if (max_stdarg_fpr >= FP_ARG_NUM_REG)
10530 max_stdarg_fpr = FP_ARG_NUM_REG - 1;
10531
10532 /* FPR argument regs start at f0. */
10533 min_stdarg_fpr += FPR0_REGNUM;
10534 max_stdarg_fpr += FPR0_REGNUM;
10535 }
10536
10537 if (s390_preserve_args_p && crtl->args.info.fprs)
10538 {
10539 min_preserve_fpr = FPR0_REGNUM;
10540 max_preserve_fpr = MIN (FPR0_REGNUM + FP_ARG_NUM_REG - 1,
10541 FPR0_REGNUM + crtl->args.info.fprs - 1);
10542 }
10543
10544 min_fpr = MIN (min_stdarg_fpr, min_preserve_fpr);
10545 max_fpr = MAX (max_stdarg_fpr, max_preserve_fpr);
10546
10547 if (max_fpr == -1)
10548 return;
10549
10550 for (i = min_fpr; i <= max_fpr; i++)
10551 cfun_set_fpr_save (i);
10552 }
10553
10554
10555 /* Reserve the GPR save slots for GPRs which need to be saved due to
10556 stdarg or -mpreserve-args.
10557 This is a helper routine for s390_register_info. */
10558
10559 static void
10560 s390_register_info_arg_gpr ()
10561 {
10562 int i;
10563 int min_stdarg_gpr = INT_MAX, max_stdarg_gpr = -1;
10564 int min_preserve_gpr = INT_MAX, max_preserve_gpr = -1;
10565 int min_gpr, max_gpr;
10566
10567 if (cfun->stdarg
10568 && cfun->va_list_gpr_size
10569 && crtl->args.info.gprs < GP_ARG_NUM_REG)
10570 {
10571 min_stdarg_gpr = crtl->args.info.gprs;
10572 max_stdarg_gpr = min_stdarg_gpr + cfun->va_list_gpr_size - 1;
10573 if (max_stdarg_gpr >= GP_ARG_NUM_REG)
10574 max_stdarg_gpr = GP_ARG_NUM_REG - 1;
10575
10576 /* GPR argument regs start at r2. */
10577 min_stdarg_gpr += GPR2_REGNUM;
10578 max_stdarg_gpr += GPR2_REGNUM;
10579 }
10580
10581 if (s390_preserve_args_p && crtl->args.info.gprs)
10582 {
10583 min_preserve_gpr = GPR2_REGNUM;
10584 max_preserve_gpr = MIN (GPR6_REGNUM,
10585 GPR2_REGNUM + crtl->args.info.gprs - 1);
10586 }
10587
10588 min_gpr = MIN (min_stdarg_gpr, min_preserve_gpr);
10589 max_gpr = MAX (max_stdarg_gpr, max_preserve_gpr);
10590
10591 if (max_gpr == -1)
10592 return;
10593
10594 /* If r6 was supposed to be saved into an FPR and now needs to go to
10595 the stack for vararg we have to adjust the restore range to make
10596 sure that the restore is done from stack as well. */
10597 if (FP_REGNO_P (cfun_gpr_save_slot (GPR6_REGNUM))
10598 && min_gpr <= GPR6_REGNUM
10599 && max_gpr >= GPR6_REGNUM)
10600 {
10601 if (cfun_frame_layout.first_restore_gpr == -1
10602 || cfun_frame_layout.first_restore_gpr > GPR6_REGNUM)
10603 cfun_frame_layout.first_restore_gpr = GPR6_REGNUM;
10604 if (cfun_frame_layout.last_restore_gpr == -1
10605 || cfun_frame_layout.last_restore_gpr < GPR6_REGNUM)
10606 cfun_frame_layout.last_restore_gpr = GPR6_REGNUM;
10607 }
10608
10609 if (cfun_frame_layout.first_save_gpr == -1
10610 || cfun_frame_layout.first_save_gpr > min_gpr)
10611 cfun_frame_layout.first_save_gpr = min_gpr;
10612
10613 if (cfun_frame_layout.last_save_gpr == -1
10614 || cfun_frame_layout.last_save_gpr < max_gpr)
10615 cfun_frame_layout.last_save_gpr = max_gpr;
10616
10617 for (i = min_gpr; i <= max_gpr; i++)
10618 cfun_gpr_save_slot (i) = SAVE_SLOT_STACK;
10619 }
10620
10621 /* Calculate the save and restore ranges for stm(g) and lm(g) in the
10622 prologue and epilogue. */
10623
10624 static void
10625 s390_register_info_set_ranges ()
10626 {
10627 int i, j;
10628
10629 /* Find the first and the last save slot supposed to use the stack
10630 to set the restore range.
10631 Vararg regs might be marked as save to stack but only the
10632 call-saved regs really need restoring (i.e. r6). This code
10633 assumes that the vararg regs have not yet been recorded in
10634 cfun_gpr_save_slot. */
10635 for (i = 0; i < 16 && cfun_gpr_save_slot (i) != SAVE_SLOT_STACK; i++);
10636 for (j = 15; j > i && cfun_gpr_save_slot (j) != SAVE_SLOT_STACK; j--);
10637 cfun_frame_layout.first_restore_gpr = (i == 16) ? -1 : i;
10638 cfun_frame_layout.last_restore_gpr = (i == 16) ? -1 : j;
10639 cfun_frame_layout.first_save_gpr = (i == 16) ? -1 : i;
10640 cfun_frame_layout.last_save_gpr = (i == 16) ? -1 : j;
10641 }
10642
10643 /* The GPR and FPR save slots in cfun->machine->frame_layout are set
10644 for registers which need to be saved in function prologue.
10645 This function can be used until the insns emitted for save/restore
10646 of the regs are visible in the RTL stream. */
10647
10648 static void
10649 s390_register_info ()
10650 {
10651 int i;
10652 char clobbered_regs[32];
10653
10654 gcc_assert (!epilogue_completed);
10655
10656 if (reload_completed)
10657 /* After reload we rely on our own routine to determine which
10658 registers need saving. */
10659 s390_regs_ever_clobbered (clobbered_regs);
10660 else
10661 /* During reload we use regs_ever_live as a base since reload
10662 does changes in there which we otherwise would not be aware
10663 of. */
10664 for (i = 0; i < 32; i++)
10665 clobbered_regs[i] = df_regs_ever_live_p (i);
10666
10667 for (i = 0; i < 32; i++)
10668 clobbered_regs[i] = clobbered_regs[i] && !global_regs[i];
10669
10670 /* Mark the call-saved FPRs which need to be saved.
10671 This needs to be done before checking the special GPRs since the
10672 stack pointer usage depends on whether high FPRs have to be saved
10673 or not. */
10674 cfun_frame_layout.fpr_bitmap = 0;
10675 cfun_frame_layout.high_fprs = 0;
10676 for (i = FPR0_REGNUM; i <= FPR15_REGNUM; i++)
10677 if (clobbered_regs[i] && !call_used_regs[i])
10678 {
10679 cfun_set_fpr_save (i);
10680 if (i >= FPR8_REGNUM)
10681 cfun_frame_layout.high_fprs++;
10682 }
10683
10684 /* Register 12 is used for GOT address, but also as temp in prologue
10685 for split-stack stdarg functions (unless r14 is available). */
10686 clobbered_regs[12]
10687 |= ((flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
10688 || (flag_split_stack && cfun->stdarg
10689 && (crtl->is_leaf || TARGET_TPF_PROFILING
10690 || has_hard_reg_initial_val (Pmode, RETURN_REGNUM))));
10691
10692 clobbered_regs[BASE_REGNUM]
10693 |= (cfun->machine->base_reg
10694 && REGNO (cfun->machine->base_reg) == BASE_REGNUM);
10695
10696 clobbered_regs[HARD_FRAME_POINTER_REGNUM]
10697 |= !!frame_pointer_needed;
10698
10699 /* On pre z900 machines this might take until machine dependent
10700 reorg to decide.
10701 save_return_addr_p will only be set on non-zarch machines so
10702 there is no risk that r14 goes into an FPR instead of a stack
10703 slot. */
10704 clobbered_regs[RETURN_REGNUM]
10705 |= (!crtl->is_leaf
10706 || TARGET_TPF_PROFILING
10707 || cfun_frame_layout.save_return_addr_p
10708 || crtl->calls_eh_return);
10709
10710 clobbered_regs[STACK_POINTER_REGNUM]
10711 |= (!crtl->is_leaf
10712 || TARGET_TPF_PROFILING
10713 || cfun_save_high_fprs_p
10714 || get_frame_size () > 0
10715 || (reload_completed && cfun_frame_layout.frame_size > 0)
10716 || cfun->calls_alloca);
10717
10718 memset (cfun_frame_layout.gpr_save_slots, SAVE_SLOT_NONE, 16);
10719
10720 for (i = 0; i < 16; i++)
10721 if (clobbered_regs[i] && !call_used_regs[i])
10722 cfun_gpr_save_slot (i) = SAVE_SLOT_STACK;
10723
10724 s390_register_info_arg_fpr ();
10725 s390_register_info_gprtofpr ();
10726 s390_register_info_set_ranges ();
10727
10728 /* Forcing argument registers to be saved on the stack might
10729 override the GPR->FPR save decision for r6 so this must come
10730 last. */
10731 s390_register_info_arg_gpr ();
10732 }
10733
10734 /* Return true if REGNO is a global register, but not one
10735 of the special ones that need to be saved/restored in anyway. */
10736
10737 static inline bool
10738 global_not_special_regno_p (int regno)
10739 {
10740 return (global_regs[regno]
10741 /* These registers are special and need to be
10742 restored in any case. */
10743 && !(regno == STACK_POINTER_REGNUM
10744 || regno == RETURN_REGNUM
10745 || regno == BASE_REGNUM
10746 || (flag_pic && regno == (int)PIC_OFFSET_TABLE_REGNUM)));
10747 }
10748
10749 /* This function is called by s390_optimize_prologue in order to get
10750 rid of unnecessary GPR save/restore instructions. The register info
10751 for the GPRs is re-computed and the ranges are re-calculated. */
10752
10753 static void
10754 s390_optimize_register_info ()
10755 {
10756 char clobbered_regs[32];
10757 int i;
10758
10759 gcc_assert (epilogue_completed);
10760
10761 s390_regs_ever_clobbered (clobbered_regs);
10762
10763 /* Global registers do not need to be saved and restored unless it
10764 is one of our special regs. (r12, r13, r14, or r15). */
10765 for (i = 0; i < 32; i++)
10766 clobbered_regs[i] = clobbered_regs[i] && !global_not_special_regno_p (i);
10767
10768 /* There is still special treatment needed for cases invisible to
10769 s390_regs_ever_clobbered. */
10770 clobbered_regs[RETURN_REGNUM]
10771 |= (TARGET_TPF_PROFILING
10772 /* When expanding builtin_return_addr in ESA mode we do not
10773 know whether r14 will later be needed as scratch reg when
10774 doing branch splitting. So the builtin always accesses the
10775 r14 save slot and we need to stick to the save/restore
10776 decision for r14 even if it turns out that it didn't get
10777 clobbered. */
10778 || cfun_frame_layout.save_return_addr_p
10779 || crtl->calls_eh_return);
10780
10781 for (i = 0; i < 16; i++)
10782 if (!clobbered_regs[i] || call_used_regs[i])
10783 cfun_gpr_save_slot (i) = SAVE_SLOT_NONE;
10784
10785 s390_register_info_set_ranges ();
10786 s390_register_info_arg_gpr ();
10787 }
10788
10789 /* Fill cfun->machine with info about frame of current function. */
10790
10791 static void
10792 s390_frame_info (void)
10793 {
10794 HOST_WIDE_INT lowest_offset;
10795
10796 cfun_frame_layout.first_save_gpr_slot = cfun_frame_layout.first_save_gpr;
10797 cfun_frame_layout.last_save_gpr_slot = cfun_frame_layout.last_save_gpr;
10798
10799 /* The va_arg builtin uses a constant distance of 16 *
10800 UNITS_PER_LONG (r0-r15) to reach the FPRs from the reg_save_area
10801 pointer. So even if we are going to save the stack pointer in an
10802 FPR we need the stack space in order to keep the offsets
10803 correct. */
10804 if (cfun->stdarg && cfun_save_arg_fprs_p)
10805 {
10806 cfun_frame_layout.last_save_gpr_slot = STACK_POINTER_REGNUM;
10807
10808 if (cfun_frame_layout.first_save_gpr_slot == -1)
10809 cfun_frame_layout.first_save_gpr_slot = STACK_POINTER_REGNUM;
10810 }
10811
10812 cfun_frame_layout.frame_size = get_frame_size ();
10813 if (!TARGET_64BIT && cfun_frame_layout.frame_size > 0x7fff0000)
10814 fatal_error (input_location,
10815 "total size of local variables exceeds architecture limit");
10816
10817 if (!TARGET_PACKED_STACK)
10818 {
10819 /* Fixed stack layout. */
10820 cfun_frame_layout.backchain_offset = 0;
10821 cfun_frame_layout.f0_offset = 16 * UNITS_PER_LONG;
10822 cfun_frame_layout.f4_offset = cfun_frame_layout.f0_offset + 2 * 8;
10823 cfun_frame_layout.f8_offset = -cfun_frame_layout.high_fprs * 8;
10824 cfun_frame_layout.gprs_offset = (cfun_frame_layout.first_save_gpr_slot
10825 * UNITS_PER_LONG);
10826 }
10827 else if (TARGET_BACKCHAIN)
10828 {
10829 /* Kernel stack layout - packed stack, backchain, no float */
10830 gcc_assert (TARGET_SOFT_FLOAT);
10831 cfun_frame_layout.backchain_offset = (STACK_POINTER_OFFSET
10832 - UNITS_PER_LONG);
10833
10834 /* The distance between the backchain and the return address
10835 save slot must not change. So we always need a slot for the
10836 stack pointer which resides in between. */
10837 cfun_frame_layout.last_save_gpr_slot = STACK_POINTER_REGNUM;
10838
10839 cfun_frame_layout.gprs_offset
10840 = cfun_frame_layout.backchain_offset - cfun_gprs_save_area_size;
10841
10842 /* FPRs will not be saved. Nevertheless pick sane values to
10843 keep area calculations valid. */
10844 cfun_frame_layout.f0_offset =
10845 cfun_frame_layout.f4_offset =
10846 cfun_frame_layout.f8_offset = cfun_frame_layout.gprs_offset;
10847 }
10848 else
10849 {
10850 int num_fprs;
10851
10852 /* Packed stack layout without backchain. */
10853
10854 /* With stdarg FPRs need their dedicated slots. */
10855 num_fprs = (TARGET_64BIT && cfun->stdarg ? 2
10856 : (cfun_fpr_save_p (FPR4_REGNUM) +
10857 cfun_fpr_save_p (FPR6_REGNUM)));
10858 cfun_frame_layout.f4_offset = STACK_POINTER_OFFSET - 8 * num_fprs;
10859
10860 num_fprs = (cfun->stdarg ? 2
10861 : (cfun_fpr_save_p (FPR0_REGNUM)
10862 + cfun_fpr_save_p (FPR2_REGNUM)));
10863 cfun_frame_layout.f0_offset = cfun_frame_layout.f4_offset - 8 * num_fprs;
10864
10865 cfun_frame_layout.gprs_offset
10866 = cfun_frame_layout.f0_offset - cfun_gprs_save_area_size;
10867
10868 cfun_frame_layout.f8_offset = (cfun_frame_layout.gprs_offset
10869 - cfun_frame_layout.high_fprs * 8);
10870 }
10871
10872 if (cfun_save_high_fprs_p)
10873 cfun_frame_layout.frame_size += cfun_frame_layout.high_fprs * 8;
10874
10875 if (!crtl->is_leaf)
10876 cfun_frame_layout.frame_size += crtl->outgoing_args_size;
10877
10878 /* In the following cases we have to allocate a STACK_POINTER_OFFSET
10879 sized area at the bottom of the stack. This is required also for
10880 leaf functions. When GCC generates a local stack reference it
10881 will always add STACK_POINTER_OFFSET to all these references. */
10882 if (crtl->is_leaf
10883 && !TARGET_TPF_PROFILING
10884 && cfun_frame_layout.frame_size == 0
10885 && !cfun->calls_alloca)
10886 return;
10887
10888 /* Calculate the number of bytes we have used in our own register
10889 save area. With the packed stack layout we can re-use the
10890 remaining bytes for normal stack elements. */
10891
10892 if (TARGET_PACKED_STACK)
10893 lowest_offset = MIN (MIN (cfun_frame_layout.f0_offset,
10894 cfun_frame_layout.f4_offset),
10895 cfun_frame_layout.gprs_offset);
10896 else
10897 lowest_offset = 0;
10898
10899 if (TARGET_BACKCHAIN)
10900 lowest_offset = MIN (lowest_offset, cfun_frame_layout.backchain_offset);
10901
10902 cfun_frame_layout.frame_size += STACK_POINTER_OFFSET - lowest_offset;
10903
10904 /* If under 31 bit an odd number of gprs has to be saved we have to
10905 adjust the frame size to sustain 8 byte alignment of stack
10906 frames. */
10907 cfun_frame_layout.frame_size = ((cfun_frame_layout.frame_size +
10908 STACK_BOUNDARY / BITS_PER_UNIT - 1)
10909 & ~(STACK_BOUNDARY / BITS_PER_UNIT - 1));
10910 }
10911
10912 /* Generate frame layout. Fills in register and frame data for the current
10913 function in cfun->machine. This routine can be called multiple times;
10914 it will re-do the complete frame layout every time. */
10915
10916 static void
10917 s390_init_frame_layout (void)
10918 {
10919 HOST_WIDE_INT frame_size;
10920 int base_used;
10921
10922 /* After LRA the frame layout is supposed to be read-only and should
10923 not be re-computed. */
10924 if (reload_completed)
10925 return;
10926
10927 do
10928 {
10929 frame_size = cfun_frame_layout.frame_size;
10930
10931 /* Try to predict whether we'll need the base register. */
10932 base_used = crtl->uses_const_pool
10933 || (!DISP_IN_RANGE (frame_size)
10934 && !CONST_OK_FOR_K (frame_size));
10935
10936 /* Decide which register to use as literal pool base. In small
10937 leaf functions, try to use an unused call-clobbered register
10938 as base register to avoid save/restore overhead. */
10939 if (!base_used)
10940 cfun->machine->base_reg = NULL_RTX;
10941 else
10942 {
10943 int br = 0;
10944
10945 if (crtl->is_leaf)
10946 /* Prefer r5 (most likely to be free). */
10947 for (br = 5; br >= 2 && df_regs_ever_live_p (br); br--)
10948 ;
10949 cfun->machine->base_reg =
10950 gen_rtx_REG (Pmode, (br >= 2) ? br : BASE_REGNUM);
10951 }
10952
10953 s390_register_info ();
10954 s390_frame_info ();
10955 }
10956 while (frame_size != cfun_frame_layout.frame_size);
10957 }
10958
10959 /* Remove the FPR clobbers from a tbegin insn if it can be proven that
10960 the TX is nonescaping. A transaction is considered escaping if
10961 there is at least one path from tbegin returning CC0 to the
10962 function exit block without an tend.
10963
10964 The check so far has some limitations:
10965 - only single tbegin/tend BBs are supported
10966 - the first cond jump after tbegin must separate the CC0 path from ~CC0
10967 - when CC is copied to a GPR and the CC0 check is done with the GPR
10968 this is not supported
10969 */
10970
10971 static void
10972 s390_optimize_nonescaping_tx (void)
10973 {
10974 const unsigned int CC0 = 1 << 3;
10975 basic_block tbegin_bb = NULL;
10976 basic_block tend_bb = NULL;
10977 basic_block bb;
10978 rtx_insn *insn;
10979 bool result = true;
10980 int bb_index;
10981 rtx_insn *tbegin_insn = NULL;
10982
10983 if (!cfun->machine->tbegin_p)
10984 return;
10985
10986 for (bb_index = 0; bb_index < n_basic_blocks_for_fn (cfun); bb_index++)
10987 {
10988 bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
10989
10990 if (!bb)
10991 continue;
10992
10993 FOR_BB_INSNS (bb, insn)
10994 {
10995 rtx ite, cc, pat, target;
10996 unsigned HOST_WIDE_INT mask;
10997
10998 if (!INSN_P (insn) || INSN_CODE (insn) <= 0)
10999 continue;
11000
11001 pat = PATTERN (insn);
11002
11003 if (GET_CODE (pat) == PARALLEL)
11004 pat = XVECEXP (pat, 0, 0);
11005
11006 if (GET_CODE (pat) != SET
11007 || GET_CODE (SET_SRC (pat)) != UNSPEC_VOLATILE)
11008 continue;
11009
11010 if (XINT (SET_SRC (pat), 1) == UNSPECV_TBEGIN)
11011 {
11012 rtx_insn *tmp;
11013
11014 tbegin_insn = insn;
11015
11016 /* Just return if the tbegin doesn't have clobbers. */
11017 if (GET_CODE (PATTERN (insn)) != PARALLEL)
11018 return;
11019
11020 if (tbegin_bb != NULL)
11021 return;
11022
11023 /* Find the next conditional jump. */
11024 for (tmp = NEXT_INSN (insn);
11025 tmp != NULL_RTX;
11026 tmp = NEXT_INSN (tmp))
11027 {
11028 if (reg_set_p (gen_rtx_REG (CCmode, CC_REGNUM), tmp))
11029 return;
11030 if (!JUMP_P (tmp))
11031 continue;
11032
11033 ite = SET_SRC (PATTERN (tmp));
11034 if (GET_CODE (ite) != IF_THEN_ELSE)
11035 continue;
11036
11037 cc = XEXP (XEXP (ite, 0), 0);
11038 if (!REG_P (cc) || !CC_REGNO_P (REGNO (cc))
11039 || GET_MODE (cc) != CCRAWmode
11040 || GET_CODE (XEXP (XEXP (ite, 0), 1)) != CONST_INT)
11041 return;
11042
11043 if (bb->succs->length () != 2)
11044 return;
11045
11046 mask = INTVAL (XEXP (XEXP (ite, 0), 1));
11047 if (GET_CODE (XEXP (ite, 0)) == NE)
11048 mask ^= 0xf;
11049
11050 if (mask == CC0)
11051 target = XEXP (ite, 1);
11052 else if (mask == (CC0 ^ 0xf))
11053 target = XEXP (ite, 2);
11054 else
11055 return;
11056
11057 {
11058 edge_iterator ei;
11059 edge e1, e2;
11060
11061 ei = ei_start (bb->succs);
11062 e1 = ei_safe_edge (ei);
11063 ei_next (&ei);
11064 e2 = ei_safe_edge (ei);
11065
11066 if (e2->flags & EDGE_FALLTHRU)
11067 {
11068 e2 = e1;
11069 e1 = ei_safe_edge (ei);
11070 }
11071
11072 if (!(e1->flags & EDGE_FALLTHRU))
11073 return;
11074
11075 tbegin_bb = (target == pc_rtx) ? e1->dest : e2->dest;
11076 }
11077 if (tmp == BB_END (bb))
11078 break;
11079 }
11080 }
11081
11082 if (XINT (SET_SRC (pat), 1) == UNSPECV_TEND)
11083 {
11084 if (tend_bb != NULL)
11085 return;
11086 tend_bb = bb;
11087 }
11088 }
11089 }
11090
11091 /* Either we successfully remove the FPR clobbers here or we are not
11092 able to do anything for this TX. Both cases don't qualify for
11093 another look. */
11094 cfun->machine->tbegin_p = false;
11095
11096 if (tbegin_bb == NULL || tend_bb == NULL)
11097 return;
11098
11099 calculate_dominance_info (CDI_POST_DOMINATORS);
11100 result = dominated_by_p (CDI_POST_DOMINATORS, tbegin_bb, tend_bb);
11101 free_dominance_info (CDI_POST_DOMINATORS);
11102
11103 if (!result)
11104 return;
11105
11106 PATTERN (tbegin_insn) = gen_rtx_PARALLEL (VOIDmode,
11107 gen_rtvec (2,
11108 XVECEXP (PATTERN (tbegin_insn), 0, 0),
11109 XVECEXP (PATTERN (tbegin_insn), 0, 1)));
11110 INSN_CODE (tbegin_insn) = -1;
11111 df_insn_rescan (tbegin_insn);
11112
11113 return;
11114 }
11115
11116 /* Implement TARGET_HARD_REGNO_NREGS. Because all registers in a class
11117 have the same size, this is equivalent to CLASS_MAX_NREGS. */
11118
11119 static unsigned int
11120 s390_hard_regno_nregs (unsigned int regno, machine_mode mode)
11121 {
11122 return s390_class_max_nregs (REGNO_REG_CLASS (regno), mode);
11123 }
11124
11125 /* Implement TARGET_HARD_REGNO_MODE_OK.
11126
11127 Integer modes <= word size fit into any GPR.
11128 Integer modes > word size fit into successive GPRs, starting with
11129 an even-numbered register.
11130 SImode and DImode fit into FPRs as well.
11131
11132 Floating point modes <= word size fit into any FPR or GPR.
11133 Floating point modes > word size (i.e. DFmode on 32-bit) fit
11134 into any FPR, or an even-odd GPR pair.
11135 TFmode fits only into an even-odd FPR pair.
11136
11137 Complex floating point modes fit either into two FPRs, or into
11138 successive GPRs (again starting with an even number).
11139 TCmode fits only into two successive even-odd FPR pairs.
11140
11141 Condition code modes fit only into the CC register. */
11142
11143 static bool
11144 s390_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
11145 {
11146 if (!TARGET_VX && VECTOR_NOFP_REGNO_P (regno))
11147 return false;
11148
11149 switch (REGNO_REG_CLASS (regno))
11150 {
11151 case VEC_REGS:
11152 return ((GET_MODE_CLASS (mode) == MODE_INT
11153 && s390_class_max_nregs (VEC_REGS, mode) == 1)
11154 || mode == DFmode
11155 || (TARGET_VXE && mode == SFmode)
11156 || s390_vector_mode_supported_p (mode));
11157 break;
11158 case FP_REGS:
11159 if (TARGET_VX
11160 && ((GET_MODE_CLASS (mode) == MODE_INT
11161 && s390_class_max_nregs (FP_REGS, mode) == 1)
11162 || mode == DFmode
11163 || s390_vector_mode_supported_p (mode)))
11164 return true;
11165
11166 if (REGNO_PAIR_OK (regno, mode))
11167 {
11168 if (mode == SImode || mode == DImode)
11169 return true;
11170
11171 if (FLOAT_MODE_P (mode) && GET_MODE_CLASS (mode) != MODE_VECTOR_FLOAT)
11172 return true;
11173 }
11174 break;
11175 case ADDR_REGS:
11176 if (FRAME_REGNO_P (regno))
11177 return mode == Pmode;
11178
11179 /* fallthrough */
11180 case GENERAL_REGS:
11181 if (REGNO_PAIR_OK (regno, mode))
11182 {
11183 if (TARGET_ZARCH
11184 || (mode != TFmode && mode != TCmode && mode != TDmode))
11185 return true;
11186 }
11187 break;
11188 case CC_REGS:
11189 if (GET_MODE_CLASS (mode) == MODE_CC)
11190 return true;
11191 break;
11192 case ACCESS_REGS:
11193 if (REGNO_PAIR_OK (regno, mode))
11194 {
11195 if (mode == SImode || mode == Pmode)
11196 return true;
11197 }
11198 break;
11199 default:
11200 return false;
11201 }
11202
11203 return false;
11204 }
11205
11206 /* Implement TARGET_MODES_TIEABLE_P. */
11207
11208 static bool
11209 s390_modes_tieable_p (machine_mode mode1, machine_mode mode2)
11210 {
11211 return ((mode1 == SFmode || mode1 == DFmode)
11212 == (mode2 == SFmode || mode2 == DFmode));
11213 }
11214
11215 /* Return nonzero if register OLD_REG can be renamed to register NEW_REG. */
11216
11217 bool
11218 s390_hard_regno_rename_ok (unsigned int old_reg, unsigned int new_reg)
11219 {
11220 /* Once we've decided upon a register to use as base register, it must
11221 no longer be used for any other purpose. */
11222 if (cfun->machine->base_reg)
11223 if (REGNO (cfun->machine->base_reg) == old_reg
11224 || REGNO (cfun->machine->base_reg) == new_reg)
11225 return false;
11226
11227 /* Prevent regrename from using call-saved regs which haven't
11228 actually been saved. This is necessary since regrename assumes
11229 the backend save/restore decisions are based on
11230 df_regs_ever_live. Since we have our own routine we have to tell
11231 regrename manually about it. */
11232 if (GENERAL_REGNO_P (new_reg)
11233 && !call_used_regs[new_reg]
11234 && cfun_gpr_save_slot (new_reg) == SAVE_SLOT_NONE)
11235 return false;
11236
11237 return true;
11238 }
11239
11240 /* Return nonzero if register REGNO can be used as a scratch register
11241 in peephole2. */
11242
11243 static bool
11244 s390_hard_regno_scratch_ok (unsigned int regno)
11245 {
11246 /* See s390_hard_regno_rename_ok. */
11247 if (GENERAL_REGNO_P (regno)
11248 && !call_used_regs[regno]
11249 && cfun_gpr_save_slot (regno) == SAVE_SLOT_NONE)
11250 return false;
11251
11252 return true;
11253 }
11254
11255 /* Implement TARGET_HARD_REGNO_CALL_PART_CLOBBERED. When generating
11256 code that runs in z/Architecture mode, but conforms to the 31-bit
11257 ABI, GPRs can hold 8 bytes; the ABI guarantees only that the lower 4
11258 bytes are saved across calls, however. */
11259
11260 static bool
11261 s390_hard_regno_call_part_clobbered (unsigned int, unsigned int regno,
11262 machine_mode mode)
11263 {
11264 /* For r12 we know that the only bits we actually care about are
11265 preserved across function calls. Since r12 is a fixed reg all
11266 accesses to r12 are generated by the backend.
11267
11268 This workaround is necessary until gcse implements proper
11269 tracking of partially clobbered registers. */
11270 if (!TARGET_64BIT
11271 && TARGET_ZARCH
11272 && GET_MODE_SIZE (mode) > 4
11273 && (!flag_pic || regno != PIC_OFFSET_TABLE_REGNUM)
11274 && ((regno >= 6 && regno <= 15) || regno == 32))
11275 return true;
11276
11277 if (TARGET_VX
11278 && GET_MODE_SIZE (mode) > 8
11279 && (((TARGET_64BIT && regno >= 24 && regno <= 31))
11280 || (!TARGET_64BIT && (regno == 18 || regno == 19))))
11281 return true;
11282
11283 return false;
11284 }
11285
11286 /* Maximum number of registers to represent a value of mode MODE
11287 in a register of class RCLASS. */
11288
11289 int
11290 s390_class_max_nregs (enum reg_class rclass, machine_mode mode)
11291 {
11292 int reg_size;
11293 bool reg_pair_required_p = false;
11294
11295 switch (rclass)
11296 {
11297 case FP_REGS:
11298 case VEC_REGS:
11299 reg_size = TARGET_VX ? 16 : 8;
11300
11301 /* TF and TD modes would fit into a VR but we put them into a
11302 register pair since we do not have 128bit FP instructions on
11303 full VRs. */
11304 if (TARGET_VX
11305 && SCALAR_FLOAT_MODE_P (mode)
11306 && GET_MODE_SIZE (mode) >= 16
11307 && !(TARGET_VXE && mode == TFmode))
11308 reg_pair_required_p = true;
11309
11310 /* Even if complex types would fit into a single FPR/VR we force
11311 them into a register pair to deal with the parts more easily.
11312 (FIXME: What about complex ints?) */
11313 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
11314 reg_pair_required_p = true;
11315 break;
11316 case ACCESS_REGS:
11317 reg_size = 4;
11318 break;
11319 default:
11320 reg_size = UNITS_PER_WORD;
11321 break;
11322 }
11323
11324 if (reg_pair_required_p)
11325 return 2 * ((GET_MODE_SIZE (mode) / 2 + reg_size - 1) / reg_size);
11326
11327 return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
11328 }
11329
11330 /* Return nonzero if mode M describes a 128-bit float in a floating point
11331 register pair. */
11332
11333 static bool
11334 s390_is_fpr128 (machine_mode m)
11335 {
11336 return m == FPRX2mode || (!TARGET_VXE && m == TFmode);
11337 }
11338
11339 /* Return nonzero if mode M describes a 128-bit float in a vector
11340 register. */
11341
11342 static bool
11343 s390_is_vr128 (machine_mode m)
11344 {
11345 return m == V1TFmode || (TARGET_VXE && m == TFmode);
11346 }
11347
11348 /* Implement TARGET_CAN_CHANGE_MODE_CLASS. */
11349
11350 static bool
11351 s390_can_change_mode_class (machine_mode from_mode,
11352 machine_mode to_mode,
11353 reg_class_t rclass)
11354 {
11355 machine_mode small_mode;
11356 machine_mode big_mode;
11357
11358 /* 128-bit values have different representations in floating point and
11359 vector registers. */
11360 if (reg_classes_intersect_p (VEC_REGS, rclass)
11361 && ((s390_is_fpr128 (from_mode) && s390_is_vr128 (to_mode))
11362 || (s390_is_vr128 (from_mode) && s390_is_fpr128 (to_mode))))
11363 return false;
11364
11365 if (GET_MODE_SIZE (from_mode) == GET_MODE_SIZE (to_mode))
11366 return true;
11367
11368 if (GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
11369 {
11370 small_mode = from_mode;
11371 big_mode = to_mode;
11372 }
11373 else
11374 {
11375 small_mode = to_mode;
11376 big_mode = from_mode;
11377 }
11378
11379 /* Values residing in VRs are little-endian style. All modes are
11380 placed left-aligned in an VR. This means that we cannot allow
11381 switching between modes with differing sizes. Also if the vector
11382 facility is available we still place TFmode values in VR register
11383 pairs, since the only instructions we have operating on TFmodes
11384 only deal with register pairs. Therefore we have to allow DFmode
11385 subregs of TFmodes to enable the TFmode splitters. */
11386 if (reg_classes_intersect_p (VEC_REGS, rclass)
11387 && (GET_MODE_SIZE (small_mode) < 8
11388 || s390_class_max_nregs (VEC_REGS, big_mode) == 1))
11389 return false;
11390
11391 /* Likewise for access registers, since they have only half the
11392 word size on 64-bit. */
11393 if (reg_classes_intersect_p (ACCESS_REGS, rclass))
11394 return false;
11395
11396 return true;
11397 }
11398
11399 /* Return true if register FROM can be eliminated via register TO. */
11400
11401 static bool
11402 s390_can_eliminate (const int from, const int to)
11403 {
11404 /* We have not marked the base register as fixed.
11405 Instead, we have an elimination rule BASE_REGNUM -> BASE_REGNUM.
11406 If a function requires the base register, we say here that this
11407 elimination cannot be performed. This will cause reload to free
11408 up the base register (as if it were fixed). On the other hand,
11409 if the current function does *not* require the base register, we
11410 say here the elimination succeeds, which in turn allows reload
11411 to allocate the base register for any other purpose. */
11412 if (from == BASE_REGNUM && to == BASE_REGNUM)
11413 {
11414 s390_init_frame_layout ();
11415 return cfun->machine->base_reg == NULL_RTX;
11416 }
11417
11418 /* Everything else must point into the stack frame. */
11419 gcc_assert (to == STACK_POINTER_REGNUM
11420 || to == HARD_FRAME_POINTER_REGNUM);
11421
11422 gcc_assert (from == FRAME_POINTER_REGNUM
11423 || from == ARG_POINTER_REGNUM
11424 || from == RETURN_ADDRESS_POINTER_REGNUM);
11425
11426 /* Make sure we actually saved the return address. */
11427 if (from == RETURN_ADDRESS_POINTER_REGNUM)
11428 if (!crtl->calls_eh_return
11429 && !cfun->stdarg
11430 && !cfun_frame_layout.save_return_addr_p)
11431 return false;
11432
11433 return true;
11434 }
11435
11436 /* Return offset between register FROM and TO initially after prolog. */
11437
11438 HOST_WIDE_INT
11439 s390_initial_elimination_offset (int from, int to)
11440 {
11441 HOST_WIDE_INT offset;
11442
11443 /* ??? Why are we called for non-eliminable pairs? */
11444 if (!s390_can_eliminate (from, to))
11445 return 0;
11446
11447 switch (from)
11448 {
11449 case FRAME_POINTER_REGNUM:
11450 offset = (get_frame_size()
11451 + STACK_POINTER_OFFSET
11452 + crtl->outgoing_args_size);
11453 break;
11454
11455 case ARG_POINTER_REGNUM:
11456 s390_init_frame_layout ();
11457 offset = cfun_frame_layout.frame_size + STACK_POINTER_OFFSET;
11458 break;
11459
11460 case RETURN_ADDRESS_POINTER_REGNUM:
11461 s390_init_frame_layout ();
11462
11463 if (cfun_frame_layout.first_save_gpr_slot == -1)
11464 {
11465 /* If it turns out that for stdarg nothing went into the reg
11466 save area we also do not need the return address
11467 pointer. */
11468 if (cfun->stdarg && !cfun_save_arg_fprs_p)
11469 return 0;
11470
11471 gcc_unreachable ();
11472 }
11473
11474 /* In order to make the following work it is not necessary for
11475 r14 to have a save slot. It is sufficient if one other GPR
11476 got one. Since the GPRs are always stored without gaps we
11477 are able to calculate where the r14 save slot would
11478 reside. */
11479 offset = (cfun_frame_layout.frame_size + cfun_frame_layout.gprs_offset +
11480 (RETURN_REGNUM - cfun_frame_layout.first_save_gpr_slot) *
11481 UNITS_PER_LONG);
11482 break;
11483
11484 case BASE_REGNUM:
11485 offset = 0;
11486 break;
11487
11488 default:
11489 gcc_unreachable ();
11490 }
11491
11492 return offset;
11493 }
11494
11495 /* Emit insn to save fpr REGNUM at offset OFFSET relative
11496 to register BASE. Return generated insn. */
11497
11498 static rtx
11499 save_fpr (rtx base, int offset, int regnum)
11500 {
11501 rtx addr;
11502 rtx insn;
11503
11504 addr = gen_rtx_MEM (DFmode, plus_constant (Pmode, base, offset));
11505
11506 if (regnum >= FPR0_REGNUM && regnum <= (FPR0_REGNUM + FP_ARG_NUM_REG))
11507 set_mem_alias_set (addr, get_varargs_alias_set ());
11508 else
11509 set_mem_alias_set (addr, get_frame_alias_set ());
11510
11511 insn = emit_move_insn (addr, gen_rtx_REG (DFmode, regnum));
11512
11513 if (!call_used_regs[regnum] || s390_preserve_fpr_arg_p (regnum))
11514 RTX_FRAME_RELATED_P (insn) = 1;
11515
11516 if (s390_preserve_fpr_arg_p (regnum) && !cfun_fpr_save_p (regnum))
11517 {
11518 rtx reg = gen_rtx_REG (DFmode, regnum);
11519 add_reg_note (insn, REG_CFA_NO_RESTORE, reg);
11520 add_reg_note (insn, REG_CFA_OFFSET, gen_rtx_SET (addr, reg));
11521 }
11522
11523 return insn;
11524 }
11525
11526 /* Emit insn to restore fpr REGNUM from offset OFFSET relative
11527 to register BASE. Return generated insn. */
11528
11529 static rtx
11530 restore_fpr (rtx base, int offset, int regnum)
11531 {
11532 rtx addr;
11533 addr = gen_rtx_MEM (DFmode, plus_constant (Pmode, base, offset));
11534 set_mem_alias_set (addr, get_frame_alias_set ());
11535
11536 return emit_move_insn (gen_rtx_REG (DFmode, regnum), addr);
11537 }
11538
11539 /* Generate insn to save registers FIRST to LAST into
11540 the register save area located at offset OFFSET
11541 relative to register BASE. */
11542
11543 static void
11544 save_gprs (rtx base, int offset, int first, int last, rtx_insn *before = NULL)
11545 {
11546 rtx addr, insn, note;
11547 rtx_insn *out_insn;
11548 int i;
11549
11550 addr = plus_constant (Pmode, base, offset);
11551 addr = gen_frame_mem (Pmode, addr);
11552
11553 /* Special-case single register. */
11554 if (first == last)
11555 {
11556 if (TARGET_64BIT)
11557 insn = gen_movdi (addr, gen_rtx_REG (Pmode, first));
11558 else
11559 insn = gen_movsi (addr, gen_rtx_REG (Pmode, first));
11560
11561 if (!global_not_special_regno_p (first))
11562 RTX_FRAME_RELATED_P (insn) = 1;
11563
11564 if (s390_preserve_gpr_arg_p (first) && !s390_restore_gpr_p (first))
11565 {
11566 rtx reg = gen_rtx_REG (Pmode, first);
11567 add_reg_note (insn, REG_CFA_NO_RESTORE, reg);
11568 add_reg_note (insn, REG_CFA_OFFSET, gen_rtx_SET (addr, reg));
11569 }
11570
11571 goto emit;
11572 }
11573
11574
11575 insn = gen_store_multiple (addr,
11576 gen_rtx_REG (Pmode, first),
11577 GEN_INT (last - first + 1));
11578
11579 if (first <= 6 && cfun->stdarg)
11580 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
11581 {
11582 rtx mem = XEXP (XVECEXP (PATTERN (insn), 0, i), 0);
11583
11584 if (first + i <= 6)
11585 set_mem_alias_set (mem, get_varargs_alias_set ());
11586 }
11587
11588 /* We need to set the FRAME_RELATED flag on all SETs
11589 inside the store-multiple pattern.
11590
11591 However, we must not emit DWARF records for registers 2..5
11592 if they are stored for use by variable arguments ...
11593
11594 ??? Unfortunately, it is not enough to simply not the
11595 FRAME_RELATED flags for those SETs, because the first SET
11596 of the PARALLEL is always treated as if it had the flag
11597 set, even if it does not. Therefore we emit a new pattern
11598 without those registers as REG_FRAME_RELATED_EXPR note. */
11599
11600 /* In these cases all of the sets are marked as frame related:
11601 1. call-save GPR saved and restored
11602 2. argument GPR saved because of -mpreserve-args */
11603 if ((first >= GPR6_REGNUM && !global_not_special_regno_p (first))
11604 || s390_preserve_gpr_arg_in_range_p (first, last))
11605
11606 {
11607 rtx pat = PATTERN (insn);
11608
11609 for (i = 0; i < XVECLEN (pat, 0); i++)
11610 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
11611 && !global_not_special_regno_p (REGNO (SET_SRC (XVECEXP (pat,
11612 0, i)))))
11613 RTX_FRAME_RELATED_P (XVECEXP (pat, 0, i)) = 1;
11614
11615 RTX_FRAME_RELATED_P (insn) = 1;
11616
11617 /* For the -mpreserve-args register saves no restore operations
11618 will be emitted. CFI checking would complain about this. We
11619 manually generate the REG_CFA notes here to be able to mark
11620 those operations with REG_CFA_NO_RESTORE. */
11621 if (s390_preserve_gpr_arg_in_range_p (first, last))
11622 {
11623 for (int regno = first; regno <= last; regno++)
11624 {
11625 rtx reg = gen_rtx_REG (Pmode, regno);
11626 rtx reg_addr = plus_constant (Pmode, base,
11627 offset + (regno - first) * UNITS_PER_LONG);
11628 if (!s390_restore_gpr_p (regno))
11629 add_reg_note (insn, REG_CFA_NO_RESTORE, reg);
11630 add_reg_note (insn, REG_CFA_OFFSET,
11631 gen_rtx_SET (gen_frame_mem (Pmode, reg_addr), reg));
11632 }
11633 }
11634 }
11635 else if (last >= 6)
11636 {
11637 int start;
11638
11639 for (start = first >= 6 ? first : 6; start <= last; start++)
11640 if (!global_not_special_regno_p (start))
11641 break;
11642
11643 if (start > last)
11644 goto emit;
11645
11646 addr = plus_constant (Pmode, base,
11647 offset + (start - first) * UNITS_PER_LONG);
11648
11649 if (start == last)
11650 {
11651 if (TARGET_64BIT)
11652 note = gen_movdi (gen_rtx_MEM (Pmode, addr),
11653 gen_rtx_REG (Pmode, start));
11654 else
11655 note = gen_movsi (gen_rtx_MEM (Pmode, addr),
11656 gen_rtx_REG (Pmode, start));
11657 note = PATTERN (note);
11658
11659 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note);
11660 RTX_FRAME_RELATED_P (insn) = 1;
11661
11662 goto emit;
11663 }
11664
11665 note = gen_store_multiple (gen_rtx_MEM (Pmode, addr),
11666 gen_rtx_REG (Pmode, start),
11667 GEN_INT (last - start + 1));
11668 note = PATTERN (note);
11669
11670 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note);
11671
11672 for (i = 0; i < XVECLEN (note, 0); i++)
11673 if (GET_CODE (XVECEXP (note, 0, i)) == SET
11674 && !global_not_special_regno_p (REGNO (SET_SRC (XVECEXP (note,
11675 0, i)))))
11676 RTX_FRAME_RELATED_P (XVECEXP (note, 0, i)) = 1;
11677
11678 RTX_FRAME_RELATED_P (insn) = 1;
11679 }
11680
11681 emit:
11682 if (before != NULL_RTX)
11683 out_insn = emit_insn_before (insn, before);
11684 else
11685 out_insn = emit_insn (insn);
11686 INSN_ADDRESSES_NEW (out_insn, -1);
11687 }
11688
11689
11690 /* Generate insn to restore registers FIRST to LAST from
11691 the register save area located at offset OFFSET
11692 relative to register BASE. */
11693
11694 static rtx
11695 restore_gprs (rtx base, int offset, int first, int last)
11696 {
11697 rtx addr, insn;
11698
11699 addr = plus_constant (Pmode, base, offset);
11700 addr = gen_frame_mem (Pmode, addr);
11701
11702 /* Special-case single register. */
11703 if (first == last)
11704 {
11705 if (TARGET_64BIT)
11706 insn = gen_movdi (gen_rtx_REG (Pmode, first), addr);
11707 else
11708 insn = gen_movsi (gen_rtx_REG (Pmode, first), addr);
11709
11710 RTX_FRAME_RELATED_P (insn) = 1;
11711 return insn;
11712 }
11713
11714 insn = gen_load_multiple (gen_rtx_REG (Pmode, first),
11715 addr,
11716 GEN_INT (last - first + 1));
11717 RTX_FRAME_RELATED_P (insn) = 1;
11718 return insn;
11719 }
11720
11721 /* Return insn sequence to load the GOT register. */
11722
11723 rtx_insn *
11724 s390_load_got (void)
11725 {
11726 rtx_insn *insns;
11727
11728 /* We cannot use pic_offset_table_rtx here since we use this
11729 function also for non-pic if __tls_get_offset is called and in
11730 that case PIC_OFFSET_TABLE_REGNUM as well as pic_offset_table_rtx
11731 aren't usable. */
11732 rtx got_rtx = gen_rtx_REG (Pmode, 12);
11733
11734 start_sequence ();
11735
11736 emit_move_insn (got_rtx, s390_got_symbol ());
11737
11738 insns = get_insns ();
11739 end_sequence ();
11740 return insns;
11741 }
11742
11743 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
11744 and the change to the stack pointer. */
11745
11746 static void
11747 s390_emit_stack_tie (void)
11748 {
11749 rtx mem = gen_frame_mem (BLKmode, stack_pointer_rtx);
11750 if (frame_pointer_needed)
11751 emit_insn (gen_stack_tie (Pmode, mem, hard_frame_pointer_rtx));
11752 else
11753 emit_insn (gen_stack_tie (Pmode, mem, stack_pointer_rtx));
11754 }
11755
11756 /* Copy GPRS into FPR save slots. */
11757
11758 static void
11759 s390_save_gprs_to_fprs (void)
11760 {
11761 int i;
11762
11763 if (!TARGET_Z10 || !TARGET_HARD_FLOAT || !crtl->is_leaf)
11764 return;
11765
11766 for (i = 6; i < 16; i++)
11767 {
11768 if (FP_REGNO_P (cfun_gpr_save_slot (i)))
11769 {
11770 rtx_insn *insn =
11771 emit_move_insn (gen_rtx_REG (DImode, cfun_gpr_save_slot (i)),
11772 gen_rtx_REG (DImode, i));
11773 RTX_FRAME_RELATED_P (insn) = 1;
11774 /* This prevents dwarf2cfi from interpreting the set. Doing
11775 so it might emit def_cfa_register infos setting an FPR as
11776 new CFA. */
11777 add_reg_note (insn, REG_CFA_REGISTER, copy_rtx (PATTERN (insn)));
11778 }
11779 }
11780 }
11781
11782 /* Restore GPRs from FPR save slots. */
11783
11784 static void
11785 s390_restore_gprs_from_fprs (void)
11786 {
11787 int i;
11788
11789 if (!TARGET_Z10 || !TARGET_HARD_FLOAT || !crtl->is_leaf)
11790 return;
11791
11792 /* Restore the GPRs starting with the stack pointer. That way the
11793 stack pointer already has its original value when it comes to
11794 restoring the hard frame pointer. So we can set the cfa reg back
11795 to the stack pointer. */
11796 for (i = STACK_POINTER_REGNUM; i >= 6; i--)
11797 {
11798 rtx_insn *insn;
11799
11800 if (!FP_REGNO_P (cfun_gpr_save_slot (i)))
11801 continue;
11802
11803 rtx fpr = gen_rtx_REG (DImode, cfun_gpr_save_slot (i));
11804
11805 if (i == STACK_POINTER_REGNUM)
11806 insn = emit_insn (gen_stack_restore_from_fpr (fpr));
11807 else
11808 insn = emit_move_insn (gen_rtx_REG (DImode, i), fpr);
11809
11810 df_set_regs_ever_live (i, true);
11811 add_reg_note (insn, REG_CFA_RESTORE, gen_rtx_REG (DImode, i));
11812
11813 /* If either the stack pointer or the frame pointer get restored
11814 set the CFA value to its value at function start. Doing this
11815 for the frame pointer results in .cfi_def_cfa_register 15
11816 what is ok since if the stack pointer got modified it has
11817 been restored already. */
11818 if (i == STACK_POINTER_REGNUM || i == HARD_FRAME_POINTER_REGNUM)
11819 add_reg_note (insn, REG_CFA_DEF_CFA,
11820 plus_constant (Pmode, stack_pointer_rtx,
11821 STACK_POINTER_OFFSET));
11822 RTX_FRAME_RELATED_P (insn) = 1;
11823 }
11824 }
11825
11826
11827 /* A pass run immediately before shrink-wrapping and prologue and epilogue
11828 generation. */
11829
11830 namespace {
11831
11832 const pass_data pass_data_s390_early_mach =
11833 {
11834 RTL_PASS, /* type */
11835 "early_mach", /* name */
11836 OPTGROUP_NONE, /* optinfo_flags */
11837 TV_MACH_DEP, /* tv_id */
11838 0, /* properties_required */
11839 0, /* properties_provided */
11840 0, /* properties_destroyed */
11841 0, /* todo_flags_start */
11842 ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
11843 };
11844
11845 class pass_s390_early_mach : public rtl_opt_pass
11846 {
11847 public:
11848 pass_s390_early_mach (gcc::context *ctxt)
11849 : rtl_opt_pass (pass_data_s390_early_mach, ctxt)
11850 {}
11851
11852 /* opt_pass methods: */
11853 virtual unsigned int execute (function *);
11854
11855 }; // class pass_s390_early_mach
11856
11857 unsigned int
11858 pass_s390_early_mach::execute (function *fun)
11859 {
11860 rtx_insn *insn;
11861
11862 /* Try to get rid of the FPR clobbers. */
11863 s390_optimize_nonescaping_tx ();
11864
11865 /* Re-compute register info. */
11866 s390_register_info ();
11867
11868 /* If we're using a base register, ensure that it is always valid for
11869 the first non-prologue instruction. */
11870 if (fun->machine->base_reg)
11871 emit_insn_at_entry (gen_main_pool (fun->machine->base_reg));
11872
11873 /* Annotate all constant pool references to let the scheduler know
11874 they implicitly use the base register. */
11875 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11876 if (INSN_P (insn))
11877 {
11878 annotate_constant_pool_refs (insn);
11879 df_insn_rescan (insn);
11880 }
11881 return 0;
11882 }
11883
11884 } // anon namespace
11885
11886 rtl_opt_pass *
11887 make_pass_s390_early_mach (gcc::context *ctxt)
11888 {
11889 return new pass_s390_early_mach (ctxt);
11890 }
11891
11892 /* Calculate TARGET = REG + OFFSET as s390_emit_prologue would do it.
11893 - push too big immediates to the literal pool and annotate the refs
11894 - emit frame related notes for stack pointer changes. */
11895
11896 static rtx
11897 s390_prologue_plus_offset (rtx target, rtx reg, rtx offset, bool frame_related_p)
11898 {
11899 rtx_insn *insn;
11900 rtx orig_offset = offset;
11901
11902 gcc_assert (REG_P (target));
11903 gcc_assert (REG_P (reg));
11904 gcc_assert (CONST_INT_P (offset));
11905
11906 if (offset == const0_rtx) /* lr/lgr */
11907 {
11908 insn = emit_move_insn (target, reg);
11909 }
11910 else if (DISP_IN_RANGE (INTVAL (offset))) /* la */
11911 {
11912 insn = emit_move_insn (target, gen_rtx_PLUS (Pmode, reg,
11913 offset));
11914 }
11915 else
11916 {
11917 if (!satisfies_constraint_K (offset) /* ahi/aghi */
11918 && (!TARGET_EXTIMM
11919 || (!satisfies_constraint_Op (offset) /* alfi/algfi */
11920 && !satisfies_constraint_On (offset)))) /* slfi/slgfi */
11921 offset = force_const_mem (Pmode, offset);
11922
11923 if (target != reg)
11924 {
11925 insn = emit_move_insn (target, reg);
11926 RTX_FRAME_RELATED_P (insn) = frame_related_p ? 1 : 0;
11927 }
11928
11929 insn = emit_insn (gen_add2_insn (target, offset));
11930
11931 if (!CONST_INT_P (offset))
11932 {
11933 annotate_constant_pool_refs (insn);
11934
11935 if (frame_related_p)
11936 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
11937 gen_rtx_SET (target,
11938 gen_rtx_PLUS (Pmode, target,
11939 orig_offset)));
11940 }
11941 }
11942
11943 RTX_FRAME_RELATED_P (insn) = frame_related_p ? 1 : 0;
11944
11945 /* If this is a stack adjustment and we are generating a stack clash
11946 prologue, then add a REG_STACK_CHECK note to signal that this insn
11947 should be left alone. */
11948 if (flag_stack_clash_protection && target == stack_pointer_rtx)
11949 add_reg_note (insn, REG_STACK_CHECK, const0_rtx);
11950
11951 return insn;
11952 }
11953
11954 /* Emit a compare instruction with a volatile memory access as stack
11955 probe. It does not waste store tags and does not clobber any
11956 registers apart from the condition code. */
11957 static void
11958 s390_emit_stack_probe (rtx addr)
11959 {
11960 rtx mem = gen_rtx_MEM (word_mode, addr);
11961 MEM_VOLATILE_P (mem) = 1;
11962 emit_insn (gen_probe_stack (mem));
11963 }
11964
11965 /* Use a runtime loop if we have to emit more probes than this. */
11966 #define MIN_UNROLL_PROBES 3
11967
11968 /* Allocate SIZE bytes of stack space, using TEMP_REG as a temporary
11969 if necessary. LAST_PROBE_OFFSET contains the offset of the closest
11970 probe relative to the stack pointer.
11971
11972 Note that SIZE is negative.
11973
11974 The return value is true if TEMP_REG has been clobbered. */
11975 static bool
11976 allocate_stack_space (rtx size, HOST_WIDE_INT last_probe_offset,
11977 rtx temp_reg)
11978 {
11979 bool temp_reg_clobbered_p = false;
11980 HOST_WIDE_INT probe_interval
11981 = 1 << param_stack_clash_protection_probe_interval;
11982 HOST_WIDE_INT guard_size
11983 = 1 << param_stack_clash_protection_guard_size;
11984
11985 if (flag_stack_clash_protection)
11986 {
11987 if (last_probe_offset + -INTVAL (size) < guard_size)
11988 dump_stack_clash_frame_info (NO_PROBE_SMALL_FRAME, true);
11989 else
11990 {
11991 rtx offset = GEN_INT (probe_interval - UNITS_PER_LONG);
11992 HOST_WIDE_INT rounded_size = -INTVAL (size) & -probe_interval;
11993 HOST_WIDE_INT num_probes = rounded_size / probe_interval;
11994 HOST_WIDE_INT residual = -INTVAL (size) - rounded_size;
11995
11996 if (num_probes < MIN_UNROLL_PROBES)
11997 {
11998 /* Emit unrolled probe statements. */
11999
12000 for (unsigned int i = 0; i < num_probes; i++)
12001 {
12002 s390_prologue_plus_offset (stack_pointer_rtx,
12003 stack_pointer_rtx,
12004 GEN_INT (-probe_interval), true);
12005 s390_emit_stack_probe (gen_rtx_PLUS (Pmode,
12006 stack_pointer_rtx,
12007 offset));
12008 }
12009 if (num_probes > 0)
12010 last_probe_offset = INTVAL (offset);
12011 dump_stack_clash_frame_info (PROBE_INLINE, residual != 0);
12012 }
12013 else
12014 {
12015 /* Emit a loop probing the pages. */
12016
12017 rtx_code_label *loop_start_label = gen_label_rtx ();
12018
12019 /* From now on temp_reg will be the CFA register. */
12020 s390_prologue_plus_offset (temp_reg, stack_pointer_rtx,
12021 GEN_INT (-rounded_size), true);
12022 emit_label (loop_start_label);
12023
12024 s390_prologue_plus_offset (stack_pointer_rtx,
12025 stack_pointer_rtx,
12026 GEN_INT (-probe_interval), false);
12027 s390_emit_stack_probe (gen_rtx_PLUS (Pmode,
12028 stack_pointer_rtx,
12029 offset));
12030 emit_cmp_and_jump_insns (stack_pointer_rtx, temp_reg,
12031 GT, NULL_RTX,
12032 Pmode, 1, loop_start_label);
12033
12034 /* Without this make_edges ICEes. */
12035 JUMP_LABEL (get_last_insn ()) = loop_start_label;
12036 LABEL_NUSES (loop_start_label) = 1;
12037
12038 /* That's going to be a NOP since stack pointer and
12039 temp_reg are supposed to be the same here. We just
12040 emit it to set the CFA reg back to r15. */
12041 s390_prologue_plus_offset (stack_pointer_rtx, temp_reg,
12042 const0_rtx, true);
12043 temp_reg_clobbered_p = true;
12044 last_probe_offset = INTVAL (offset);
12045 dump_stack_clash_frame_info (PROBE_LOOP, residual != 0);
12046 }
12047
12048 /* Handle any residual allocation request. */
12049 s390_prologue_plus_offset (stack_pointer_rtx,
12050 stack_pointer_rtx,
12051 GEN_INT (-residual), true);
12052 last_probe_offset += residual;
12053 if (last_probe_offset >= probe_interval)
12054 s390_emit_stack_probe (gen_rtx_PLUS (Pmode,
12055 stack_pointer_rtx,
12056 GEN_INT (residual
12057 - UNITS_PER_LONG)));
12058
12059 return temp_reg_clobbered_p;
12060 }
12061 }
12062
12063 /* Subtract frame size from stack pointer. */
12064 s390_prologue_plus_offset (stack_pointer_rtx,
12065 stack_pointer_rtx,
12066 size, true);
12067
12068 return temp_reg_clobbered_p;
12069 }
12070
12071 /* Expand the prologue into a bunch of separate insns. */
12072
12073 void
12074 s390_emit_prologue (void)
12075 {
12076 rtx insn, addr;
12077 rtx temp_reg;
12078 int i;
12079 int offset;
12080 int next_fpr = 0;
12081
12082 /* Choose best register to use for temp use within prologue.
12083 TPF with profiling must avoid the register 14 - the tracing function
12084 needs the original contents of r14 to be preserved. */
12085
12086 if (!has_hard_reg_initial_val (Pmode, RETURN_REGNUM)
12087 && !crtl->is_leaf
12088 && !TARGET_TPF_PROFILING)
12089 temp_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
12090 else if (flag_split_stack && cfun->stdarg)
12091 temp_reg = gen_rtx_REG (Pmode, 12);
12092 else
12093 temp_reg = gen_rtx_REG (Pmode, 1);
12094
12095 /* When probing for stack-clash mitigation, we have to track the distance
12096 between the stack pointer and closest known reference.
12097
12098 Most of the time we have to make a worst case assumption. The
12099 only exception is when TARGET_BACKCHAIN is active, in which case
12100 we know *sp (offset 0) was written. */
12101 HOST_WIDE_INT probe_interval
12102 = 1 << param_stack_clash_protection_probe_interval;
12103 HOST_WIDE_INT last_probe_offset
12104 = (TARGET_BACKCHAIN
12105 ? (TARGET_PACKED_STACK ? STACK_POINTER_OFFSET - UNITS_PER_LONG : 0)
12106 : probe_interval - (STACK_BOUNDARY / UNITS_PER_WORD));
12107
12108 s390_save_gprs_to_fprs ();
12109
12110 /* Save call saved gprs. */
12111 if (cfun_frame_layout.first_save_gpr != -1)
12112 {
12113 save_gprs (stack_pointer_rtx,
12114 cfun_frame_layout.gprs_offset +
12115 UNITS_PER_LONG * (cfun_frame_layout.first_save_gpr
12116 - cfun_frame_layout.first_save_gpr_slot),
12117 cfun_frame_layout.first_save_gpr,
12118 cfun_frame_layout.last_save_gpr);
12119
12120 /* This is not 100% correct. If we have more than one register saved,
12121 then LAST_PROBE_OFFSET can move even closer to sp. */
12122 last_probe_offset
12123 = (cfun_frame_layout.gprs_offset +
12124 UNITS_PER_LONG * (cfun_frame_layout.first_save_gpr
12125 - cfun_frame_layout.first_save_gpr_slot));
12126 }
12127
12128 /* Dummy insn to mark literal pool slot. */
12129
12130 if (cfun->machine->base_reg)
12131 emit_insn (gen_main_pool (cfun->machine->base_reg));
12132
12133 offset = cfun_frame_layout.f0_offset;
12134
12135 /* Save f0 and f2. */
12136 for (i = FPR0_REGNUM; i <= FPR0_REGNUM + 1; i++)
12137 {
12138 if (cfun_fpr_save_p (i))
12139 {
12140 save_fpr (stack_pointer_rtx, offset, i);
12141 if (offset < last_probe_offset)
12142 last_probe_offset = offset;
12143 offset += 8;
12144 }
12145 else if (!TARGET_PACKED_STACK || cfun->stdarg)
12146 offset += 8;
12147 }
12148
12149 /* Save f4 and f6. */
12150 offset = cfun_frame_layout.f4_offset;
12151 for (i = FPR4_REGNUM; i <= FPR4_REGNUM + 1; i++)
12152 {
12153 if (cfun_fpr_save_p (i))
12154 {
12155 save_fpr (stack_pointer_rtx, offset, i);
12156 if (offset < last_probe_offset)
12157 last_probe_offset = offset;
12158 offset += 8;
12159 }
12160 else if (!TARGET_PACKED_STACK || call_used_regs[i])
12161 offset += 8;
12162 }
12163
12164 if (TARGET_PACKED_STACK
12165 && cfun_save_high_fprs_p
12166 && cfun_frame_layout.f8_offset + cfun_frame_layout.high_fprs * 8 > 0)
12167 {
12168 offset = (cfun_frame_layout.f8_offset
12169 + (cfun_frame_layout.high_fprs - 1) * 8);
12170
12171 for (i = FPR15_REGNUM; i >= FPR8_REGNUM && offset >= 0; i--)
12172 if (cfun_fpr_save_p (i))
12173 {
12174 save_fpr (stack_pointer_rtx, offset, i);
12175 if (offset < last_probe_offset)
12176 last_probe_offset = offset;
12177
12178 offset -= 8;
12179 }
12180 if (offset >= cfun_frame_layout.f8_offset)
12181 next_fpr = i;
12182 }
12183
12184 if (!TARGET_PACKED_STACK)
12185 next_fpr = cfun_save_high_fprs_p ? FPR15_REGNUM : 0;
12186
12187 if (flag_stack_usage_info)
12188 current_function_static_stack_size = cfun_frame_layout.frame_size;
12189
12190 /* Decrement stack pointer. */
12191
12192 if (cfun_frame_layout.frame_size > 0)
12193 {
12194 rtx frame_off = GEN_INT (-cfun_frame_layout.frame_size);
12195 rtx_insn *stack_pointer_backup_loc;
12196 bool temp_reg_clobbered_p;
12197
12198 if (s390_stack_size)
12199 {
12200 HOST_WIDE_INT stack_guard;
12201
12202 if (s390_stack_guard)
12203 stack_guard = s390_stack_guard;
12204 else
12205 {
12206 /* If no value for stack guard is provided the smallest power of 2
12207 larger than the current frame size is chosen. */
12208 stack_guard = 1;
12209 while (stack_guard < cfun_frame_layout.frame_size)
12210 stack_guard <<= 1;
12211 }
12212
12213 if (cfun_frame_layout.frame_size >= s390_stack_size)
12214 {
12215 warning (0, "frame size of function %qs is %wd"
12216 " bytes exceeding user provided stack limit of "
12217 "%d bytes; "
12218 "an unconditional trap is added",
12219 current_function_name(), cfun_frame_layout.frame_size,
12220 s390_stack_size);
12221 emit_insn (gen_trap ());
12222 emit_barrier ();
12223 }
12224 else
12225 {
12226 /* stack_guard has to be smaller than s390_stack_size.
12227 Otherwise we would emit an AND with zero which would
12228 not match the test under mask pattern. */
12229 if (stack_guard >= s390_stack_size)
12230 {
12231 warning (0, "frame size of function %qs is %wd"
12232 " bytes which is more than half the stack size; "
12233 "the dynamic check would not be reliable; "
12234 "no check emitted for this function",
12235 current_function_name(),
12236 cfun_frame_layout.frame_size);
12237 }
12238 else
12239 {
12240 HOST_WIDE_INT stack_check_mask = ((s390_stack_size - 1)
12241 & ~(stack_guard - 1));
12242
12243 rtx t = gen_rtx_AND (Pmode, stack_pointer_rtx,
12244 GEN_INT (stack_check_mask));
12245 if (TARGET_64BIT)
12246 emit_insn (gen_ctrapdi4 (gen_rtx_EQ (VOIDmode,
12247 t, const0_rtx),
12248 t, const0_rtx, const0_rtx));
12249 else
12250 emit_insn (gen_ctrapsi4 (gen_rtx_EQ (VOIDmode,
12251 t, const0_rtx),
12252 t, const0_rtx, const0_rtx));
12253 }
12254 }
12255 }
12256
12257 if (s390_warn_framesize > 0
12258 && cfun_frame_layout.frame_size >= s390_warn_framesize)
12259 warning (0, "frame size of %qs is %wd bytes",
12260 current_function_name (), cfun_frame_layout.frame_size);
12261
12262 if (s390_warn_dynamicstack_p && cfun->calls_alloca)
12263 warning (0, "%qs uses dynamic stack allocation", current_function_name ());
12264
12265 /* Save the location where we could backup the incoming stack
12266 pointer. */
12267 stack_pointer_backup_loc = get_last_insn ();
12268
12269 temp_reg_clobbered_p = allocate_stack_space (frame_off, last_probe_offset,
12270 temp_reg);
12271
12272 if (TARGET_BACKCHAIN || next_fpr)
12273 {
12274 if (temp_reg_clobbered_p)
12275 {
12276 /* allocate_stack_space had to make use of temp_reg and
12277 we need it to hold a backup of the incoming stack
12278 pointer. Calculate back that value from the current
12279 stack pointer. */
12280 s390_prologue_plus_offset (temp_reg, stack_pointer_rtx,
12281 GEN_INT (cfun_frame_layout.frame_size),
12282 false);
12283 }
12284 else
12285 {
12286 /* allocate_stack_space didn't actually required
12287 temp_reg. Insert the stack pointer backup insn
12288 before the stack pointer decrement code - knowing now
12289 that the value will survive. */
12290 emit_insn_after (gen_move_insn (temp_reg, stack_pointer_rtx),
12291 stack_pointer_backup_loc);
12292 }
12293 }
12294
12295 /* Set backchain. */
12296
12297 if (TARGET_BACKCHAIN)
12298 {
12299 if (cfun_frame_layout.backchain_offset)
12300 addr = gen_rtx_MEM (Pmode,
12301 plus_constant (Pmode, stack_pointer_rtx,
12302 cfun_frame_layout.backchain_offset));
12303 else
12304 addr = gen_rtx_MEM (Pmode, stack_pointer_rtx);
12305 set_mem_alias_set (addr, get_frame_alias_set ());
12306 insn = emit_insn (gen_move_insn (addr, temp_reg));
12307 }
12308
12309 /* If we support non-call exceptions (e.g. for Java),
12310 we need to make sure the backchain pointer is set up
12311 before any possibly trapping memory access. */
12312 if (TARGET_BACKCHAIN && cfun->can_throw_non_call_exceptions)
12313 {
12314 addr = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
12315 emit_clobber (addr);
12316 }
12317 }
12318 else if (flag_stack_clash_protection)
12319 dump_stack_clash_frame_info (NO_PROBE_NO_FRAME, false);
12320
12321 /* Save fprs 8 - 15 (64 bit ABI). */
12322
12323 if (cfun_save_high_fprs_p && next_fpr)
12324 {
12325 /* If the stack might be accessed through a different register
12326 we have to make sure that the stack pointer decrement is not
12327 moved below the use of the stack slots. */
12328 s390_emit_stack_tie ();
12329
12330 insn = emit_insn (gen_add2_insn (temp_reg,
12331 GEN_INT (cfun_frame_layout.f8_offset)));
12332
12333 offset = 0;
12334
12335 for (i = FPR8_REGNUM; i <= next_fpr; i++)
12336 if (cfun_fpr_save_p (i))
12337 {
12338 rtx addr = plus_constant (Pmode, stack_pointer_rtx,
12339 cfun_frame_layout.frame_size
12340 + cfun_frame_layout.f8_offset
12341 + offset);
12342
12343 insn = save_fpr (temp_reg, offset, i);
12344 offset += 8;
12345 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
12346 gen_rtx_SET (gen_rtx_MEM (DFmode, addr),
12347 gen_rtx_REG (DFmode, i)));
12348 }
12349 }
12350
12351 /* Set frame pointer, if needed. */
12352
12353 if (frame_pointer_needed)
12354 {
12355 s390_emit_stack_tie ();
12356 insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
12357 RTX_FRAME_RELATED_P (insn) = 1;
12358 }
12359
12360 /* Set up got pointer, if needed. */
12361
12362 if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
12363 {
12364 rtx_insn *insns = s390_load_got ();
12365
12366 for (rtx_insn *insn = insns; insn; insn = NEXT_INSN (insn))
12367 annotate_constant_pool_refs (insn);
12368
12369 emit_insn (insns);
12370 }
12371
12372 #if TARGET_TPF != 0
12373 if (TARGET_TPF_PROFILING)
12374 {
12375 /* Generate a BAS instruction to serve as a function entry
12376 intercept to facilitate the use of tracing algorithms located
12377 at the branch target. */
12378 emit_insn (gen_prologue_tpf (
12379 GEN_INT (s390_tpf_trace_hook_prologue_check),
12380 GEN_INT (s390_tpf_trace_hook_prologue_target)));
12381
12382 /* Emit a blockage here so that all code lies between the
12383 profiling mechanisms. */
12384 emit_insn (gen_blockage ());
12385 }
12386 #endif
12387 }
12388
12389 /* Expand the epilogue into a bunch of separate insns. */
12390
12391 void
12392 s390_emit_epilogue (bool sibcall)
12393 {
12394 rtx frame_pointer, return_reg = NULL_RTX, cfa_restores = NULL_RTX;
12395 int area_bottom, area_top, offset = 0;
12396 int next_offset;
12397 int i;
12398
12399 #if TARGET_TPF != 0
12400 if (TARGET_TPF_PROFILING)
12401 {
12402 /* Generate a BAS instruction to serve as a function entry
12403 intercept to facilitate the use of tracing algorithms located
12404 at the branch target. */
12405
12406 /* Emit a blockage here so that all code lies between the
12407 profiling mechanisms. */
12408 emit_insn (gen_blockage ());
12409
12410 emit_insn (gen_epilogue_tpf (
12411 GEN_INT (s390_tpf_trace_hook_epilogue_check),
12412 GEN_INT (s390_tpf_trace_hook_epilogue_target)));
12413 }
12414 #endif
12415
12416 /* Check whether to use frame or stack pointer for restore. */
12417
12418 frame_pointer = (frame_pointer_needed
12419 ? hard_frame_pointer_rtx : stack_pointer_rtx);
12420
12421 s390_frame_area (&area_bottom, &area_top);
12422
12423 /* Check whether we can access the register save area.
12424 If not, increment the frame pointer as required. */
12425
12426 if (area_top <= area_bottom)
12427 {
12428 /* Nothing to restore. */
12429 }
12430 else if (DISP_IN_RANGE (cfun_frame_layout.frame_size + area_bottom)
12431 && DISP_IN_RANGE (cfun_frame_layout.frame_size + area_top - 1))
12432 {
12433 /* Area is in range. */
12434 offset = cfun_frame_layout.frame_size;
12435 }
12436 else
12437 {
12438 rtx_insn *insn;
12439 rtx frame_off, cfa;
12440
12441 offset = area_bottom < 0 ? -area_bottom : 0;
12442 frame_off = GEN_INT (cfun_frame_layout.frame_size - offset);
12443
12444 cfa = gen_rtx_SET (frame_pointer,
12445 gen_rtx_PLUS (Pmode, frame_pointer, frame_off));
12446 if (DISP_IN_RANGE (INTVAL (frame_off)))
12447 {
12448 rtx set;
12449
12450 set = gen_rtx_SET (frame_pointer,
12451 gen_rtx_PLUS (Pmode, frame_pointer, frame_off));
12452 insn = emit_insn (set);
12453 }
12454 else
12455 {
12456 if (!CONST_OK_FOR_K (INTVAL (frame_off)))
12457 frame_off = force_const_mem (Pmode, frame_off);
12458
12459 insn = emit_insn (gen_add2_insn (frame_pointer, frame_off));
12460 annotate_constant_pool_refs (insn);
12461 }
12462 add_reg_note (insn, REG_CFA_ADJUST_CFA, cfa);
12463 RTX_FRAME_RELATED_P (insn) = 1;
12464 }
12465
12466 /* Restore call saved fprs. */
12467
12468 if (TARGET_64BIT)
12469 {
12470 if (cfun_save_high_fprs_p)
12471 {
12472 next_offset = cfun_frame_layout.f8_offset;
12473 for (i = FPR8_REGNUM; i <= FPR15_REGNUM; i++)
12474 {
12475 if (cfun_fpr_save_p (i))
12476 {
12477 restore_fpr (frame_pointer,
12478 offset + next_offset, i);
12479 cfa_restores
12480 = alloc_reg_note (REG_CFA_RESTORE,
12481 gen_rtx_REG (DFmode, i), cfa_restores);
12482 next_offset += 8;
12483 }
12484 }
12485 }
12486
12487 }
12488 else
12489 {
12490 next_offset = cfun_frame_layout.f4_offset;
12491 /* f4, f6 */
12492 for (i = FPR4_REGNUM; i <= FPR4_REGNUM + 1; i++)
12493 {
12494 if (cfun_fpr_save_p (i))
12495 {
12496 restore_fpr (frame_pointer,
12497 offset + next_offset, i);
12498 cfa_restores
12499 = alloc_reg_note (REG_CFA_RESTORE,
12500 gen_rtx_REG (DFmode, i), cfa_restores);
12501 next_offset += 8;
12502 }
12503 else if (!TARGET_PACKED_STACK)
12504 next_offset += 8;
12505 }
12506
12507 }
12508
12509 /* Restore call saved gprs. */
12510
12511 if (cfun_frame_layout.first_restore_gpr != -1)
12512 {
12513 rtx insn, addr;
12514 int i;
12515
12516 /* Check for global register and save them
12517 to stack location from where they get restored. */
12518
12519 for (i = cfun_frame_layout.first_restore_gpr;
12520 i <= cfun_frame_layout.last_restore_gpr;
12521 i++)
12522 {
12523 if (global_not_special_regno_p (i))
12524 {
12525 addr = plus_constant (Pmode, frame_pointer,
12526 offset + cfun_frame_layout.gprs_offset
12527 + (i - cfun_frame_layout.first_save_gpr_slot)
12528 * UNITS_PER_LONG);
12529 addr = gen_rtx_MEM (Pmode, addr);
12530 set_mem_alias_set (addr, get_frame_alias_set ());
12531 emit_move_insn (addr, gen_rtx_REG (Pmode, i));
12532 }
12533 else
12534 cfa_restores
12535 = alloc_reg_note (REG_CFA_RESTORE,
12536 gen_rtx_REG (Pmode, i), cfa_restores);
12537 }
12538
12539 /* Fetch return address from stack before load multiple,
12540 this will do good for scheduling.
12541
12542 Only do this if we already decided that r14 needs to be
12543 saved to a stack slot. (And not just because r14 happens to
12544 be in between two GPRs which need saving.) Otherwise it
12545 would be difficult to take that decision back in
12546 s390_optimize_prologue.
12547
12548 This optimization is only helpful on in-order machines. */
12549 if (! sibcall
12550 && cfun_gpr_save_slot (RETURN_REGNUM) == SAVE_SLOT_STACK
12551 && s390_tune <= PROCESSOR_2097_Z10)
12552 {
12553 int return_regnum = find_unused_clobbered_reg();
12554 if (!return_regnum
12555 || (TARGET_INDIRECT_BRANCH_NOBP_RET_OPTION
12556 && !TARGET_CPU_Z10
12557 && return_regnum == INDIRECT_BRANCH_THUNK_REGNUM))
12558 {
12559 gcc_assert (INDIRECT_BRANCH_THUNK_REGNUM != 4);
12560 return_regnum = 4;
12561 }
12562 return_reg = gen_rtx_REG (Pmode, return_regnum);
12563
12564 addr = plus_constant (Pmode, frame_pointer,
12565 offset + cfun_frame_layout.gprs_offset
12566 + (RETURN_REGNUM
12567 - cfun_frame_layout.first_save_gpr_slot)
12568 * UNITS_PER_LONG);
12569 addr = gen_rtx_MEM (Pmode, addr);
12570 set_mem_alias_set (addr, get_frame_alias_set ());
12571 emit_move_insn (return_reg, addr);
12572
12573 /* Once we did that optimization we have to make sure
12574 s390_optimize_prologue does not try to remove the store
12575 of r14 since we will not be able to find the load issued
12576 here. */
12577 cfun_frame_layout.save_return_addr_p = true;
12578 }
12579
12580 insn = restore_gprs (frame_pointer,
12581 offset + cfun_frame_layout.gprs_offset
12582 + (cfun_frame_layout.first_restore_gpr
12583 - cfun_frame_layout.first_save_gpr_slot)
12584 * UNITS_PER_LONG,
12585 cfun_frame_layout.first_restore_gpr,
12586 cfun_frame_layout.last_restore_gpr);
12587 insn = emit_insn (insn);
12588 REG_NOTES (insn) = cfa_restores;
12589 add_reg_note (insn, REG_CFA_DEF_CFA,
12590 plus_constant (Pmode, stack_pointer_rtx,
12591 STACK_POINTER_OFFSET));
12592 RTX_FRAME_RELATED_P (insn) = 1;
12593 }
12594
12595 s390_restore_gprs_from_fprs ();
12596
12597 if (! sibcall)
12598 {
12599 if (!return_reg && !s390_can_use_return_insn ())
12600 /* We planned to emit (return), be we are not allowed to. */
12601 return_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
12602
12603 if (return_reg)
12604 /* Emit (return) and (use). */
12605 emit_jump_insn (gen_return_use (return_reg));
12606 else
12607 /* The fact that RETURN_REGNUM is used is already reflected by
12608 EPILOGUE_USES. Emit plain (return). */
12609 emit_jump_insn (gen_return ());
12610 }
12611 }
12612
12613 /* Implement TARGET_SET_UP_BY_PROLOGUE. */
12614
12615 static void
12616 s300_set_up_by_prologue (hard_reg_set_container *regs)
12617 {
12618 if (cfun->machine->base_reg
12619 && !call_used_regs[REGNO (cfun->machine->base_reg)])
12620 SET_HARD_REG_BIT (regs->set, REGNO (cfun->machine->base_reg));
12621 }
12622
12623 /* -fsplit-stack support. */
12624
12625 /* A SYMBOL_REF for __morestack. */
12626 static GTY(()) rtx morestack_ref;
12627
12628 /* When using -fsplit-stack, the allocation routines set a field in
12629 the TCB to the bottom of the stack plus this much space, measured
12630 in bytes. */
12631
12632 #define SPLIT_STACK_AVAILABLE 1024
12633
12634 /* Emit the parmblock for __morestack into .rodata section. It
12635 consists of 3 pointer size entries:
12636 - frame size
12637 - size of stack arguments
12638 - offset between parm block and __morestack return label */
12639
12640 void
12641 s390_output_split_stack_data (rtx parm_block, rtx call_done,
12642 rtx frame_size, rtx args_size)
12643 {
12644 rtx ops[] = { parm_block, call_done };
12645
12646 switch_to_section (targetm.asm_out.function_rodata_section
12647 (current_function_decl, false));
12648
12649 if (TARGET_64BIT)
12650 output_asm_insn (".align\t8", NULL);
12651 else
12652 output_asm_insn (".align\t4", NULL);
12653
12654 (*targetm.asm_out.internal_label) (asm_out_file, "L",
12655 CODE_LABEL_NUMBER (parm_block));
12656 if (TARGET_64BIT)
12657 {
12658 output_asm_insn (".quad\t%0", &frame_size);
12659 output_asm_insn (".quad\t%0", &args_size);
12660 output_asm_insn (".quad\t%1-%0", ops);
12661 }
12662 else
12663 {
12664 output_asm_insn (".long\t%0", &frame_size);
12665 output_asm_insn (".long\t%0", &args_size);
12666 output_asm_insn (".long\t%1-%0", ops);
12667 }
12668
12669 switch_to_section (current_function_section ());
12670 }
12671
12672 /* Emit -fsplit-stack prologue, which goes before the regular function
12673 prologue. */
12674
12675 void
12676 s390_expand_split_stack_prologue (void)
12677 {
12678 rtx r1, guard, cc = NULL;
12679 rtx_insn *insn;
12680 /* Offset from thread pointer to __private_ss. */
12681 int psso = TARGET_64BIT ? 0x38 : 0x20;
12682 /* Pointer size in bytes. */
12683 /* Frame size and argument size - the two parameters to __morestack. */
12684 HOST_WIDE_INT frame_size = cfun_frame_layout.frame_size;
12685 /* Align argument size to 8 bytes - simplifies __morestack code. */
12686 HOST_WIDE_INT args_size = crtl->args.size >= 0
12687 ? ((crtl->args.size + 7) & ~7)
12688 : 0;
12689 /* Label to be called by __morestack. */
12690 rtx_code_label *call_done = NULL;
12691 rtx_code_label *parm_base = NULL;
12692 rtx tmp;
12693
12694 gcc_assert (flag_split_stack && reload_completed);
12695
12696 r1 = gen_rtx_REG (Pmode, 1);
12697
12698 /* If no stack frame will be allocated, don't do anything. */
12699 if (!frame_size)
12700 {
12701 if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
12702 {
12703 /* If va_start is used, just use r15. */
12704 emit_move_insn (r1,
12705 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
12706 GEN_INT (STACK_POINTER_OFFSET)));
12707
12708 }
12709 return;
12710 }
12711
12712 if (morestack_ref == NULL_RTX)
12713 {
12714 morestack_ref = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
12715 SYMBOL_REF_FLAGS (morestack_ref) |= (SYMBOL_FLAG_LOCAL
12716 | SYMBOL_FLAG_FUNCTION);
12717 }
12718
12719 if (CONST_OK_FOR_K (frame_size) || CONST_OK_FOR_Op (frame_size))
12720 {
12721 /* If frame_size will fit in an add instruction, do a stack space
12722 check, and only call __morestack if there's not enough space. */
12723
12724 /* Get thread pointer. r1 is the only register we can always destroy - r0
12725 could contain a static chain (and cannot be used to address memory
12726 anyway), r2-r6 can contain parameters, and r6-r15 are callee-saved. */
12727 emit_insn (gen_get_thread_pointer (Pmode, r1));
12728 /* Aim at __private_ss. */
12729 guard = gen_rtx_MEM (Pmode, plus_constant (Pmode, r1, psso));
12730
12731 /* If less that 1kiB used, skip addition and compare directly with
12732 __private_ss. */
12733 if (frame_size > SPLIT_STACK_AVAILABLE)
12734 {
12735 emit_move_insn (r1, guard);
12736 if (TARGET_64BIT)
12737 emit_insn (gen_adddi3 (r1, r1, GEN_INT (frame_size)));
12738 else
12739 emit_insn (gen_addsi3 (r1, r1, GEN_INT (frame_size)));
12740 guard = r1;
12741 }
12742
12743 /* Compare the (maybe adjusted) guard with the stack pointer. */
12744 cc = s390_emit_compare (VOIDmode, LT, stack_pointer_rtx, guard);
12745 }
12746
12747 call_done = gen_label_rtx ();
12748 parm_base = gen_label_rtx ();
12749 LABEL_NUSES (parm_base)++;
12750 LABEL_NUSES (call_done)++;
12751
12752 /* %r1 = litbase. */
12753 insn = emit_move_insn (r1, gen_rtx_LABEL_REF (VOIDmode, parm_base));
12754 add_reg_note (insn, REG_LABEL_OPERAND, parm_base);
12755 LABEL_NUSES (parm_base)++;
12756
12757 /* Now, we need to call __morestack. It has very special calling
12758 conventions: it preserves param/return/static chain registers for
12759 calling main function body, and looks for its own parameters at %r1. */
12760 if (cc != NULL)
12761 tmp = gen_split_stack_cond_call (Pmode,
12762 morestack_ref,
12763 parm_base,
12764 call_done,
12765 GEN_INT (frame_size),
12766 GEN_INT (args_size),
12767 cc);
12768 else
12769 tmp = gen_split_stack_call (Pmode,
12770 morestack_ref,
12771 parm_base,
12772 call_done,
12773 GEN_INT (frame_size),
12774 GEN_INT (args_size));
12775
12776 insn = emit_jump_insn (tmp);
12777 JUMP_LABEL (insn) = call_done;
12778 add_reg_note (insn, REG_LABEL_OPERAND, parm_base);
12779 add_reg_note (insn, REG_LABEL_OPERAND, call_done);
12780
12781 if (cc != NULL)
12782 {
12783 /* Mark the jump as very unlikely to be taken. */
12784 add_reg_br_prob_note (insn,
12785 profile_probability::very_unlikely ());
12786
12787 if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
12788 {
12789 /* If va_start is used, and __morestack was not called, just use
12790 r15. */
12791 emit_move_insn (r1,
12792 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
12793 GEN_INT (STACK_POINTER_OFFSET)));
12794 }
12795 }
12796 else
12797 {
12798 emit_barrier ();
12799 }
12800
12801 /* __morestack will call us here. */
12802
12803 emit_label (call_done);
12804 }
12805
12806 /* We may have to tell the dataflow pass that the split stack prologue
12807 is initializing a register. */
12808
12809 static void
12810 s390_live_on_entry (bitmap regs)
12811 {
12812 if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
12813 {
12814 gcc_assert (flag_split_stack);
12815 bitmap_set_bit (regs, 1);
12816 }
12817 }
12818
12819 /* Return true if the function can use simple_return to return outside
12820 of a shrink-wrapped region. At present shrink-wrapping is supported
12821 in all cases. */
12822
12823 bool
12824 s390_can_use_simple_return_insn (void)
12825 {
12826 return true;
12827 }
12828
12829 /* Return true if the epilogue is guaranteed to contain only a return
12830 instruction and if a direct return can therefore be used instead.
12831 One of the main advantages of using direct return instructions
12832 is that we can then use conditional returns. */
12833
12834 bool
12835 s390_can_use_return_insn (void)
12836 {
12837 int i;
12838
12839 if (!reload_completed)
12840 return false;
12841
12842 if (crtl->profile)
12843 return false;
12844
12845 if (TARGET_TPF_PROFILING)
12846 return false;
12847
12848 for (i = 0; i < 16; i++)
12849 if (cfun_gpr_save_slot (i) != SAVE_SLOT_NONE)
12850 return false;
12851
12852 /* For 31 bit this is not covered by the frame_size check below
12853 since f4, f6 are saved in the register save area without needing
12854 additional stack space. */
12855 if (!TARGET_64BIT
12856 && (cfun_fpr_save_p (FPR4_REGNUM) || cfun_fpr_save_p (FPR6_REGNUM)))
12857 return false;
12858
12859 if (cfun->machine->base_reg
12860 && !call_used_regs[REGNO (cfun->machine->base_reg)])
12861 return false;
12862
12863 return cfun_frame_layout.frame_size == 0;
12864 }
12865
12866 /* The VX ABI differs for vararg functions. Therefore we need the
12867 prototype of the callee to be available when passing vector type
12868 values. */
12869 static const char *
12870 s390_invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
12871 {
12872 return ((TARGET_VX_ABI
12873 && typelist == 0
12874 && VECTOR_TYPE_P (TREE_TYPE (val))
12875 && (funcdecl == NULL_TREE
12876 || (TREE_CODE (funcdecl) == FUNCTION_DECL
12877 && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD
12878 && !fndecl_built_in_p (funcdecl, BUILT_IN_CLASSIFY_TYPE))))
12879 ? N_("vector argument passed to unprototyped function")
12880 : NULL);
12881 }
12882
12883
12884 /* Return the size in bytes of a function argument of
12885 type TYPE and/or mode MODE. At least one of TYPE or
12886 MODE must be specified. */
12887
12888 static int
12889 s390_function_arg_size (machine_mode mode, const_tree type)
12890 {
12891 if (type)
12892 return int_size_in_bytes (type);
12893
12894 /* No type info available for some library calls ... */
12895 if (mode != BLKmode)
12896 return GET_MODE_SIZE (mode);
12897
12898 /* If we have neither type nor mode, abort */
12899 gcc_unreachable ();
12900 }
12901
12902 /* Return true if a variable of TYPE should be passed as single value
12903 with type CODE. If STRICT_SIZE_CHECK_P is true the sizes of the
12904 record type and the field type must match.
12905
12906 The ABI says that record types with a single member are treated
12907 just like that member would be. This function is a helper to
12908 detect such cases. The function also produces the proper
12909 diagnostics for cases where the outcome might be different
12910 depending on the GCC version. */
12911 static bool
12912 s390_single_field_struct_p (enum tree_code code, const_tree type,
12913 bool strict_size_check_p)
12914 {
12915 int empty_base_seen = 0;
12916 bool zero_width_bf_skipped_p = false;
12917 const_tree orig_type = type;
12918
12919 while (TREE_CODE (type) == RECORD_TYPE)
12920 {
12921 tree field, single_type = NULL_TREE;
12922
12923 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
12924 {
12925 if (TREE_CODE (field) != FIELD_DECL)
12926 continue;
12927
12928 if (DECL_FIELD_ABI_IGNORED (field))
12929 {
12930 if (lookup_attribute ("no_unique_address",
12931 DECL_ATTRIBUTES (field)))
12932 empty_base_seen |= 2;
12933 else
12934 empty_base_seen |= 1;
12935 continue;
12936 }
12937
12938 if (DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (field))
12939 {
12940 zero_width_bf_skipped_p = true;
12941 continue;
12942 }
12943
12944 if (single_type == NULL_TREE)
12945 single_type = TREE_TYPE (field);
12946 else
12947 return false;
12948 }
12949
12950 if (single_type == NULL_TREE)
12951 return false;
12952
12953 /* Reaching this point we have a struct with a single member and
12954 zero or more zero-sized bit-fields which have been skipped in the
12955 past. */
12956
12957 /* If ZERO_WIDTH_BF_SKIPPED_P then the struct will not be accepted. In case
12958 we are not supposed to emit a warning exit early. */
12959 if (zero_width_bf_skipped_p && !warn_psabi)
12960 return false;
12961
12962 /* If the field declaration adds extra bytes due to padding this
12963 is not accepted with STRICT_SIZE_CHECK_P. */
12964 if (strict_size_check_p
12965 && (int_size_in_bytes (single_type) <= 0
12966 || int_size_in_bytes (single_type) != int_size_in_bytes (type)))
12967 return false;
12968
12969 type = single_type;
12970 }
12971
12972 if (TREE_CODE (type) != code)
12973 return false;
12974
12975 if (warn_psabi)
12976 {
12977 unsigned uid = TYPE_UID (TYPE_MAIN_VARIANT (orig_type));
12978
12979 if (empty_base_seen)
12980 {
12981 static unsigned last_reported_type_uid_empty_base;
12982 if (uid != last_reported_type_uid_empty_base)
12983 {
12984 last_reported_type_uid_empty_base = uid;
12985 const char *url = CHANGES_ROOT_URL "gcc-10/changes.html#empty_base";
12986 if (empty_base_seen & 1)
12987 inform (input_location,
12988 "parameter passing for argument of type %qT when C++17 "
12989 "is enabled changed to match C++14 %{in GCC 10.1%}",
12990 orig_type, url);
12991 else
12992 inform (input_location,
12993 "parameter passing for argument of type %qT with "
12994 "%<[[no_unique_address]]%> members changed "
12995 "%{in GCC 10.1%}", orig_type, url);
12996 }
12997 }
12998
12999 /* For C++ older GCCs ignored zero width bitfields and therefore
13000 passed structs more often as single values than GCC 12 does.
13001 So diagnostics are only required in cases where we do NOT
13002 accept the struct to be passed as single value. */
13003 if (zero_width_bf_skipped_p)
13004 {
13005 static unsigned last_reported_type_uid_zero_width;
13006 if (uid != last_reported_type_uid_zero_width)
13007 {
13008 last_reported_type_uid_zero_width = uid;
13009 inform (input_location,
13010 "parameter passing for argument of type %qT with "
13011 "zero-width bit fields members changed in GCC 12",
13012 orig_type);
13013 }
13014 }
13015 }
13016
13017 return !zero_width_bf_skipped_p;
13018 }
13019
13020
13021 /* Return true if a function argument of type TYPE and mode MODE
13022 is to be passed in a vector register, if available. */
13023
13024 static bool
13025 s390_function_arg_vector (machine_mode mode, const_tree type)
13026 {
13027 if (!TARGET_VX_ABI)
13028 return false;
13029
13030 if (s390_function_arg_size (mode, type) > 16)
13031 return false;
13032
13033 /* No type info available for some library calls ... */
13034 if (!type)
13035 return VECTOR_MODE_P (mode);
13036
13037 if (!s390_single_field_struct_p (VECTOR_TYPE, type, true))
13038 return false;
13039
13040 return true;
13041 }
13042
13043 /* Return true if a function argument of type TYPE and mode MODE
13044 is to be passed in a floating-point register, if available. */
13045
13046 static bool
13047 s390_function_arg_float (machine_mode mode, const_tree type)
13048 {
13049 if (s390_function_arg_size (mode, type) > 8)
13050 return false;
13051
13052 /* Soft-float changes the ABI: no floating-point registers are used. */
13053 if (TARGET_SOFT_FLOAT)
13054 return false;
13055
13056 /* No type info available for some library calls ... */
13057 if (!type)
13058 return mode == SFmode || mode == DFmode || mode == SDmode || mode == DDmode;
13059
13060 if (!s390_single_field_struct_p (REAL_TYPE, type, false))
13061 return false;
13062
13063 return true;
13064 }
13065
13066 /* Return true if a function argument of type TYPE and mode MODE
13067 is to be passed in an integer register, or a pair of integer
13068 registers, if available. */
13069
13070 static bool
13071 s390_function_arg_integer (machine_mode mode, const_tree type)
13072 {
13073 int size = s390_function_arg_size (mode, type);
13074 if (size > 8)
13075 return false;
13076
13077 /* No type info available for some library calls ... */
13078 if (!type)
13079 return GET_MODE_CLASS (mode) == MODE_INT
13080 || (TARGET_SOFT_FLOAT && SCALAR_FLOAT_MODE_P (mode));
13081
13082 /* We accept small integral (and similar) types. */
13083 if (INTEGRAL_TYPE_P (type)
13084 || POINTER_TYPE_P (type)
13085 || TREE_CODE (type) == NULLPTR_TYPE
13086 || TREE_CODE (type) == OFFSET_TYPE
13087 || (TARGET_SOFT_FLOAT && SCALAR_FLOAT_TYPE_P (type)))
13088 return true;
13089
13090 /* We also accept structs of size 1, 2, 4, 8 that are not
13091 passed in floating-point registers. */
13092 if (AGGREGATE_TYPE_P (type)
13093 && exact_log2 (size) >= 0
13094 && !s390_function_arg_float (mode, type))
13095 return true;
13096
13097 return false;
13098 }
13099
13100 /* Return 1 if a function argument ARG is to be passed by reference.
13101 The ABI specifies that only structures of size 1, 2, 4, or 8 bytes
13102 are passed by value, all other structures (and complex numbers) are
13103 passed by reference. */
13104
13105 static bool
13106 s390_pass_by_reference (cumulative_args_t, const function_arg_info &arg)
13107 {
13108 int size = s390_function_arg_size (arg.mode, arg.type);
13109
13110 if (s390_function_arg_vector (arg.mode, arg.type))
13111 return false;
13112
13113 if (size > 8)
13114 return true;
13115
13116 if (tree type = arg.type)
13117 {
13118 if (AGGREGATE_TYPE_P (type) && exact_log2 (size) < 0)
13119 return true;
13120
13121 if (TREE_CODE (type) == COMPLEX_TYPE
13122 || TREE_CODE (type) == VECTOR_TYPE)
13123 return true;
13124 }
13125
13126 return false;
13127 }
13128
13129 /* Update the data in CUM to advance over argument ARG. */
13130
13131 static void
13132 s390_function_arg_advance (cumulative_args_t cum_v,
13133 const function_arg_info &arg)
13134 {
13135 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
13136
13137 if (s390_function_arg_vector (arg.mode, arg.type))
13138 {
13139 /* We are called for unnamed vector stdarg arguments which are
13140 passed on the stack. In this case this hook does not have to
13141 do anything since stack arguments are tracked by common
13142 code. */
13143 if (!arg.named)
13144 return;
13145 cum->vrs += 1;
13146 }
13147 else if (s390_function_arg_float (arg.mode, arg.type))
13148 {
13149 cum->fprs += 1;
13150 }
13151 else if (s390_function_arg_integer (arg.mode, arg.type))
13152 {
13153 int size = s390_function_arg_size (arg.mode, arg.type);
13154 cum->gprs += ((size + UNITS_PER_LONG - 1) / UNITS_PER_LONG);
13155 }
13156 else
13157 gcc_unreachable ();
13158 }
13159
13160 /* Define where to put the arguments to a function.
13161 Value is zero to push the argument on the stack,
13162 or a hard register in which to store the argument.
13163
13164 CUM is a variable of type CUMULATIVE_ARGS which gives info about
13165 the preceding args and about the function being called.
13166 ARG is a description of the argument.
13167
13168 On S/390, we use general purpose registers 2 through 6 to
13169 pass integer, pointer, and certain structure arguments, and
13170 floating point registers 0 and 2 (0, 2, 4, and 6 on 64-bit)
13171 to pass floating point arguments. All remaining arguments
13172 are pushed to the stack. */
13173
13174 static rtx
13175 s390_function_arg (cumulative_args_t cum_v, const function_arg_info &arg)
13176 {
13177 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
13178
13179 if (!arg.named)
13180 s390_check_type_for_vector_abi (arg.type, true, false);
13181
13182 if (s390_function_arg_vector (arg.mode, arg.type))
13183 {
13184 /* Vector arguments being part of the ellipsis are passed on the
13185 stack. */
13186 if (!arg.named || (cum->vrs + 1 > VEC_ARG_NUM_REG))
13187 return NULL_RTX;
13188
13189 return gen_rtx_REG (arg.mode, cum->vrs + FIRST_VEC_ARG_REGNO);
13190 }
13191 else if (s390_function_arg_float (arg.mode, arg.type))
13192 {
13193 if (cum->fprs + 1 > FP_ARG_NUM_REG)
13194 return NULL_RTX;
13195 else
13196 return gen_rtx_REG (arg.mode, cum->fprs + 16);
13197 }
13198 else if (s390_function_arg_integer (arg.mode, arg.type))
13199 {
13200 int size = s390_function_arg_size (arg.mode, arg.type);
13201 int n_gprs = (size + UNITS_PER_LONG - 1) / UNITS_PER_LONG;
13202
13203 if (cum->gprs + n_gprs > GP_ARG_NUM_REG)
13204 return NULL_RTX;
13205 else if (n_gprs == 1 || UNITS_PER_WORD == UNITS_PER_LONG)
13206 return gen_rtx_REG (arg.mode, cum->gprs + 2);
13207 else if (n_gprs == 2)
13208 {
13209 rtvec p = rtvec_alloc (2);
13210
13211 RTVEC_ELT (p, 0)
13212 = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, cum->gprs + 2),
13213 const0_rtx);
13214 RTVEC_ELT (p, 1)
13215 = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, cum->gprs + 3),
13216 GEN_INT (4));
13217
13218 return gen_rtx_PARALLEL (arg.mode, p);
13219 }
13220 }
13221
13222 /* After the real arguments, expand_call calls us once again with an
13223 end marker. Whatever we return here is passed as operand 2 to the
13224 call expanders.
13225
13226 We don't need this feature ... */
13227 else if (arg.end_marker_p ())
13228 return const0_rtx;
13229
13230 gcc_unreachable ();
13231 }
13232
13233 /* Implement TARGET_FUNCTION_ARG_BOUNDARY. Vector arguments are
13234 left-justified when placed on the stack during parameter passing. */
13235
13236 static pad_direction
13237 s390_function_arg_padding (machine_mode mode, const_tree type)
13238 {
13239 if (s390_function_arg_vector (mode, type))
13240 return PAD_UPWARD;
13241
13242 return default_function_arg_padding (mode, type);
13243 }
13244
13245 /* Return true if return values of type TYPE should be returned
13246 in a memory buffer whose address is passed by the caller as
13247 hidden first argument. */
13248
13249 static bool
13250 s390_return_in_memory (const_tree type, const_tree fundecl ATTRIBUTE_UNUSED)
13251 {
13252 /* We accept small integral (and similar) types. */
13253 if (INTEGRAL_TYPE_P (type)
13254 || POINTER_TYPE_P (type)
13255 || TREE_CODE (type) == OFFSET_TYPE
13256 || SCALAR_FLOAT_TYPE_P (type))
13257 return int_size_in_bytes (type) > 8;
13258
13259 /* vector types which fit into a VR. */
13260 if (TARGET_VX_ABI
13261 && VECTOR_TYPE_P (type)
13262 && int_size_in_bytes (type) <= 16)
13263 return false;
13264
13265 /* Aggregates and similar constructs are always returned
13266 in memory. */
13267 if (AGGREGATE_TYPE_P (type)
13268 || TREE_CODE (type) == COMPLEX_TYPE
13269 || VECTOR_TYPE_P (type))
13270 return true;
13271
13272 /* ??? We get called on all sorts of random stuff from
13273 aggregate_value_p. We can't abort, but it's not clear
13274 what's safe to return. Pretend it's a struct I guess. */
13275 return true;
13276 }
13277
13278 /* Function arguments and return values are promoted to word size. */
13279
13280 static machine_mode
13281 s390_promote_function_mode (const_tree type, machine_mode mode,
13282 int *punsignedp,
13283 const_tree fntype ATTRIBUTE_UNUSED,
13284 int for_return ATTRIBUTE_UNUSED)
13285 {
13286 if (INTEGRAL_MODE_P (mode)
13287 && GET_MODE_SIZE (mode) < UNITS_PER_LONG)
13288 {
13289 if (type != NULL_TREE && POINTER_TYPE_P (type))
13290 *punsignedp = POINTERS_EXTEND_UNSIGNED;
13291 return Pmode;
13292 }
13293
13294 return mode;
13295 }
13296
13297 /* Define where to return a (scalar) value of type RET_TYPE.
13298 If RET_TYPE is null, define where to return a (scalar)
13299 value of mode MODE from a libcall. */
13300
13301 static rtx
13302 s390_function_and_libcall_value (machine_mode mode,
13303 const_tree ret_type,
13304 const_tree fntype_or_decl,
13305 bool outgoing ATTRIBUTE_UNUSED)
13306 {
13307 /* For vector return types it is important to use the RET_TYPE
13308 argument whenever available since the middle-end might have
13309 changed the mode to a scalar mode. */
13310 bool vector_ret_type_p = ((ret_type && VECTOR_TYPE_P (ret_type))
13311 || (!ret_type && VECTOR_MODE_P (mode)));
13312
13313 /* For normal functions perform the promotion as
13314 promote_function_mode would do. */
13315 if (ret_type)
13316 {
13317 int unsignedp = TYPE_UNSIGNED (ret_type);
13318 mode = promote_function_mode (ret_type, mode, &unsignedp,
13319 fntype_or_decl, 1);
13320 }
13321
13322 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT
13323 || SCALAR_FLOAT_MODE_P (mode)
13324 || (TARGET_VX_ABI && vector_ret_type_p));
13325 gcc_assert (GET_MODE_SIZE (mode) <= (TARGET_VX_ABI ? 16 : 8));
13326
13327 if (TARGET_VX_ABI && vector_ret_type_p)
13328 return gen_rtx_REG (mode, FIRST_VEC_ARG_REGNO);
13329 else if (TARGET_HARD_FLOAT && SCALAR_FLOAT_MODE_P (mode))
13330 return gen_rtx_REG (mode, 16);
13331 else if (GET_MODE_SIZE (mode) <= UNITS_PER_LONG
13332 || UNITS_PER_LONG == UNITS_PER_WORD)
13333 return gen_rtx_REG (mode, 2);
13334 else if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_LONG)
13335 {
13336 /* This case is triggered when returning a 64 bit value with
13337 -m31 -mzarch. Although the value would fit into a single
13338 register it has to be forced into a 32 bit register pair in
13339 order to match the ABI. */
13340 rtvec p = rtvec_alloc (2);
13341
13342 RTVEC_ELT (p, 0)
13343 = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, 2), const0_rtx);
13344 RTVEC_ELT (p, 1)
13345 = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, 3), GEN_INT (4));
13346
13347 return gen_rtx_PARALLEL (mode, p);
13348 }
13349
13350 gcc_unreachable ();
13351 }
13352
13353 /* Define where to return a scalar return value of type RET_TYPE. */
13354
13355 static rtx
13356 s390_function_value (const_tree ret_type, const_tree fn_decl_or_type,
13357 bool outgoing)
13358 {
13359 return s390_function_and_libcall_value (TYPE_MODE (ret_type), ret_type,
13360 fn_decl_or_type, outgoing);
13361 }
13362
13363 /* Define where to return a scalar libcall return value of mode
13364 MODE. */
13365
13366 static rtx
13367 s390_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
13368 {
13369 return s390_function_and_libcall_value (mode, NULL_TREE,
13370 NULL_TREE, true);
13371 }
13372
13373
13374 /* Create and return the va_list datatype.
13375
13376 On S/390, va_list is an array type equivalent to
13377
13378 typedef struct __va_list_tag
13379 {
13380 long __gpr;
13381 long __fpr;
13382 void *__overflow_arg_area;
13383 void *__reg_save_area;
13384 } va_list[1];
13385
13386 where __gpr and __fpr hold the number of general purpose
13387 or floating point arguments used up to now, respectively,
13388 __overflow_arg_area points to the stack location of the
13389 next argument passed on the stack, and __reg_save_area
13390 always points to the start of the register area in the
13391 call frame of the current function. The function prologue
13392 saves all registers used for argument passing into this
13393 area if the function uses variable arguments. */
13394
13395 static tree
13396 s390_build_builtin_va_list (void)
13397 {
13398 tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
13399
13400 record = lang_hooks.types.make_type (RECORD_TYPE);
13401
13402 type_decl =
13403 build_decl (BUILTINS_LOCATION,
13404 TYPE_DECL, get_identifier ("__va_list_tag"), record);
13405
13406 f_gpr = build_decl (BUILTINS_LOCATION,
13407 FIELD_DECL, get_identifier ("__gpr"),
13408 long_integer_type_node);
13409 f_fpr = build_decl (BUILTINS_LOCATION,
13410 FIELD_DECL, get_identifier ("__fpr"),
13411 long_integer_type_node);
13412 f_ovf = build_decl (BUILTINS_LOCATION,
13413 FIELD_DECL, get_identifier ("__overflow_arg_area"),
13414 ptr_type_node);
13415 f_sav = build_decl (BUILTINS_LOCATION,
13416 FIELD_DECL, get_identifier ("__reg_save_area"),
13417 ptr_type_node);
13418
13419 va_list_gpr_counter_field = f_gpr;
13420 va_list_fpr_counter_field = f_fpr;
13421
13422 DECL_FIELD_CONTEXT (f_gpr) = record;
13423 DECL_FIELD_CONTEXT (f_fpr) = record;
13424 DECL_FIELD_CONTEXT (f_ovf) = record;
13425 DECL_FIELD_CONTEXT (f_sav) = record;
13426
13427 TYPE_STUB_DECL (record) = type_decl;
13428 TYPE_NAME (record) = type_decl;
13429 TYPE_FIELDS (record) = f_gpr;
13430 DECL_CHAIN (f_gpr) = f_fpr;
13431 DECL_CHAIN (f_fpr) = f_ovf;
13432 DECL_CHAIN (f_ovf) = f_sav;
13433
13434 layout_type (record);
13435
13436 /* The correct type is an array type of one element. */
13437 return build_array_type (record, build_index_type (size_zero_node));
13438 }
13439
13440 /* Implement va_start by filling the va_list structure VALIST.
13441 STDARG_P is always true, and ignored.
13442 NEXTARG points to the first anonymous stack argument.
13443
13444 The following global variables are used to initialize
13445 the va_list structure:
13446
13447 crtl->args.info:
13448 holds number of gprs and fprs used for named arguments.
13449 crtl->args.arg_offset_rtx:
13450 holds the offset of the first anonymous stack argument
13451 (relative to the virtual arg pointer). */
13452
13453 static void
13454 s390_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
13455 {
13456 HOST_WIDE_INT n_gpr, n_fpr;
13457 int off;
13458 tree f_gpr, f_fpr, f_ovf, f_sav;
13459 tree gpr, fpr, ovf, sav, t;
13460
13461 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
13462 f_fpr = DECL_CHAIN (f_gpr);
13463 f_ovf = DECL_CHAIN (f_fpr);
13464 f_sav = DECL_CHAIN (f_ovf);
13465
13466 valist = build_simple_mem_ref (valist);
13467 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
13468 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
13469 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
13470 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
13471
13472 /* Count number of gp and fp argument registers used. */
13473
13474 n_gpr = crtl->args.info.gprs;
13475 n_fpr = crtl->args.info.fprs;
13476
13477 if (cfun->va_list_gpr_size)
13478 {
13479 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
13480 build_int_cst (NULL_TREE, n_gpr));
13481 TREE_SIDE_EFFECTS (t) = 1;
13482 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
13483 }
13484
13485 if (cfun->va_list_fpr_size)
13486 {
13487 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
13488 build_int_cst (NULL_TREE, n_fpr));
13489 TREE_SIDE_EFFECTS (t) = 1;
13490 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
13491 }
13492
13493 if (flag_split_stack
13494 && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
13495 == NULL)
13496 && cfun->machine->split_stack_varargs_pointer == NULL_RTX)
13497 {
13498 rtx reg;
13499 rtx_insn *seq;
13500
13501 reg = gen_reg_rtx (Pmode);
13502 cfun->machine->split_stack_varargs_pointer = reg;
13503
13504 start_sequence ();
13505 emit_move_insn (reg, gen_rtx_REG (Pmode, 1));
13506 seq = get_insns ();
13507 end_sequence ();
13508
13509 push_topmost_sequence ();
13510 emit_insn_after (seq, entry_of_function ());
13511 pop_topmost_sequence ();
13512 }
13513
13514 /* Find the overflow area.
13515 FIXME: This currently is too pessimistic when the vector ABI is
13516 enabled. In that case we *always* set up the overflow area
13517 pointer. */
13518 if (n_gpr + cfun->va_list_gpr_size > GP_ARG_NUM_REG
13519 || n_fpr + cfun->va_list_fpr_size > FP_ARG_NUM_REG
13520 || TARGET_VX_ABI)
13521 {
13522 if (cfun->machine->split_stack_varargs_pointer == NULL_RTX)
13523 t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
13524 else
13525 t = make_tree (TREE_TYPE (ovf), cfun->machine->split_stack_varargs_pointer);
13526
13527 off = INTVAL (crtl->args.arg_offset_rtx);
13528 off = off < 0 ? 0 : off;
13529 if (TARGET_DEBUG_ARG)
13530 fprintf (stderr, "va_start: n_gpr = %d, n_fpr = %d off %d\n",
13531 (int)n_gpr, (int)n_fpr, off);
13532
13533 t = fold_build_pointer_plus_hwi (t, off);
13534
13535 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
13536 TREE_SIDE_EFFECTS (t) = 1;
13537 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
13538 }
13539
13540 /* Find the register save area. */
13541 if ((cfun->va_list_gpr_size && n_gpr < GP_ARG_NUM_REG)
13542 || (cfun->va_list_fpr_size && n_fpr < FP_ARG_NUM_REG))
13543 {
13544 t = make_tree (TREE_TYPE (sav), return_address_pointer_rtx);
13545 t = fold_build_pointer_plus_hwi (t, -RETURN_REGNUM * UNITS_PER_LONG);
13546
13547 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
13548 TREE_SIDE_EFFECTS (t) = 1;
13549 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
13550 }
13551 }
13552
13553 /* Implement va_arg by updating the va_list structure
13554 VALIST as required to retrieve an argument of type
13555 TYPE, and returning that argument.
13556
13557 Generates code equivalent to:
13558
13559 if (integral value) {
13560 if (size <= 4 && args.gpr < 5 ||
13561 size > 4 && args.gpr < 4 )
13562 ret = args.reg_save_area[args.gpr+8]
13563 else
13564 ret = *args.overflow_arg_area++;
13565 } else if (vector value) {
13566 ret = *args.overflow_arg_area;
13567 args.overflow_arg_area += size / 8;
13568 } else if (float value) {
13569 if (args.fgpr < 2)
13570 ret = args.reg_save_area[args.fpr+64]
13571 else
13572 ret = *args.overflow_arg_area++;
13573 } else if (aggregate value) {
13574 if (args.gpr < 5)
13575 ret = *args.reg_save_area[args.gpr]
13576 else
13577 ret = **args.overflow_arg_area++;
13578 } */
13579
13580 static tree
13581 s390_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
13582 gimple_seq *post_p ATTRIBUTE_UNUSED)
13583 {
13584 tree f_gpr, f_fpr, f_ovf, f_sav;
13585 tree gpr, fpr, ovf, sav, reg, t, u;
13586 int indirect_p, size, n_reg, sav_ofs, sav_scale, max_reg;
13587 tree lab_false, lab_over = NULL_TREE;
13588 tree addr = create_tmp_var (ptr_type_node, "addr");
13589 bool left_align_p; /* How a value < UNITS_PER_LONG is aligned within
13590 a stack slot. */
13591
13592 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
13593 f_fpr = DECL_CHAIN (f_gpr);
13594 f_ovf = DECL_CHAIN (f_fpr);
13595 f_sav = DECL_CHAIN (f_ovf);
13596
13597 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
13598 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
13599 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
13600
13601 /* The tree for args* cannot be shared between gpr/fpr and ovf since
13602 both appear on a lhs. */
13603 valist = unshare_expr (valist);
13604 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
13605
13606 size = int_size_in_bytes (type);
13607
13608 s390_check_type_for_vector_abi (type, true, false);
13609
13610 if (pass_va_arg_by_reference (type))
13611 {
13612 if (TARGET_DEBUG_ARG)
13613 {
13614 fprintf (stderr, "va_arg: aggregate type");
13615 debug_tree (type);
13616 }
13617
13618 /* Aggregates are passed by reference. */
13619 indirect_p = 1;
13620 reg = gpr;
13621 n_reg = 1;
13622
13623 /* kernel stack layout on 31 bit: It is assumed here that no padding
13624 will be added by s390_frame_info because for va_args always an even
13625 number of gprs has to be saved r15-r2 = 14 regs. */
13626 sav_ofs = 2 * UNITS_PER_LONG;
13627 sav_scale = UNITS_PER_LONG;
13628 size = UNITS_PER_LONG;
13629 max_reg = GP_ARG_NUM_REG - n_reg;
13630 left_align_p = false;
13631 }
13632 else if (s390_function_arg_vector (TYPE_MODE (type), type))
13633 {
13634 if (TARGET_DEBUG_ARG)
13635 {
13636 fprintf (stderr, "va_arg: vector type");
13637 debug_tree (type);
13638 }
13639
13640 indirect_p = 0;
13641 reg = NULL_TREE;
13642 n_reg = 0;
13643 sav_ofs = 0;
13644 sav_scale = 8;
13645 max_reg = 0;
13646 left_align_p = true;
13647 }
13648 else if (s390_function_arg_float (TYPE_MODE (type), type))
13649 {
13650 if (TARGET_DEBUG_ARG)
13651 {
13652 fprintf (stderr, "va_arg: float type");
13653 debug_tree (type);
13654 }
13655
13656 /* FP args go in FP registers, if present. */
13657 indirect_p = 0;
13658 reg = fpr;
13659 n_reg = 1;
13660 sav_ofs = 16 * UNITS_PER_LONG;
13661 sav_scale = 8;
13662 max_reg = FP_ARG_NUM_REG - n_reg;
13663 left_align_p = false;
13664 }
13665 else
13666 {
13667 if (TARGET_DEBUG_ARG)
13668 {
13669 fprintf (stderr, "va_arg: other type");
13670 debug_tree (type);
13671 }
13672
13673 /* Otherwise into GP registers. */
13674 indirect_p = 0;
13675 reg = gpr;
13676 n_reg = (size + UNITS_PER_LONG - 1) / UNITS_PER_LONG;
13677
13678 /* kernel stack layout on 31 bit: It is assumed here that no padding
13679 will be added by s390_frame_info because for va_args always an even
13680 number of gprs has to be saved r15-r2 = 14 regs. */
13681 sav_ofs = 2 * UNITS_PER_LONG;
13682
13683 if (size < UNITS_PER_LONG)
13684 sav_ofs += UNITS_PER_LONG - size;
13685
13686 sav_scale = UNITS_PER_LONG;
13687 max_reg = GP_ARG_NUM_REG - n_reg;
13688 left_align_p = false;
13689 }
13690
13691 /* Pull the value out of the saved registers ... */
13692
13693 if (reg != NULL_TREE)
13694 {
13695 /*
13696 if (reg > ((typeof (reg))max_reg))
13697 goto lab_false;
13698
13699 addr = sav + sav_ofs + reg * save_scale;
13700
13701 goto lab_over;
13702
13703 lab_false:
13704 */
13705
13706 lab_false = create_artificial_label (UNKNOWN_LOCATION);
13707 lab_over = create_artificial_label (UNKNOWN_LOCATION);
13708
13709 t = fold_convert (TREE_TYPE (reg), size_int (max_reg));
13710 t = build2 (GT_EXPR, boolean_type_node, reg, t);
13711 u = build1 (GOTO_EXPR, void_type_node, lab_false);
13712 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
13713 gimplify_and_add (t, pre_p);
13714
13715 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
13716 u = build2 (MULT_EXPR, TREE_TYPE (reg), reg,
13717 fold_convert (TREE_TYPE (reg), size_int (sav_scale)));
13718 t = fold_build_pointer_plus (t, u);
13719
13720 gimplify_assign (addr, t, pre_p);
13721
13722 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
13723
13724 gimple_seq_add_stmt (pre_p, gimple_build_label (lab_false));
13725 }
13726
13727 /* ... Otherwise out of the overflow area. */
13728
13729 t = ovf;
13730 if (size < UNITS_PER_LONG && !left_align_p)
13731 t = fold_build_pointer_plus_hwi (t, UNITS_PER_LONG - size);
13732
13733 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
13734
13735 gimplify_assign (addr, t, pre_p);
13736
13737 if (size < UNITS_PER_LONG && left_align_p)
13738 t = fold_build_pointer_plus_hwi (t, UNITS_PER_LONG);
13739 else
13740 t = fold_build_pointer_plus_hwi (t, size);
13741
13742 gimplify_assign (ovf, t, pre_p);
13743
13744 if (reg != NULL_TREE)
13745 gimple_seq_add_stmt (pre_p, gimple_build_label (lab_over));
13746
13747
13748 /* Increment register save count. */
13749
13750 if (n_reg > 0)
13751 {
13752 u = build2 (PREINCREMENT_EXPR, TREE_TYPE (reg), reg,
13753 fold_convert (TREE_TYPE (reg), size_int (n_reg)));
13754 gimplify_and_add (u, pre_p);
13755 }
13756
13757 if (indirect_p)
13758 {
13759 t = build_pointer_type_for_mode (build_pointer_type (type),
13760 ptr_mode, true);
13761 addr = fold_convert (t, addr);
13762 addr = build_va_arg_indirect_ref (addr);
13763 }
13764 else
13765 {
13766 t = build_pointer_type_for_mode (type, ptr_mode, true);
13767 addr = fold_convert (t, addr);
13768 }
13769
13770 return build_va_arg_indirect_ref (addr);
13771 }
13772
13773 /* Emit rtl for the tbegin or tbegin_retry (RETRY != NULL_RTX)
13774 expanders.
13775 DEST - Register location where CC will be stored.
13776 TDB - Pointer to a 256 byte area where to store the transaction.
13777 diagnostic block. NULL if TDB is not needed.
13778 RETRY - Retry count value. If non-NULL a retry loop for CC2
13779 is emitted
13780 CLOBBER_FPRS_P - If true clobbers for all FPRs are emitted as part
13781 of the tbegin instruction pattern. */
13782
13783 void
13784 s390_expand_tbegin (rtx dest, rtx tdb, rtx retry, bool clobber_fprs_p)
13785 {
13786 rtx retry_plus_two = gen_reg_rtx (SImode);
13787 rtx retry_reg = gen_reg_rtx (SImode);
13788 rtx_code_label *retry_label = NULL;
13789
13790 if (retry != NULL_RTX)
13791 {
13792 emit_move_insn (retry_reg, retry);
13793 emit_insn (gen_addsi3 (retry_plus_two, retry_reg, const2_rtx));
13794 emit_insn (gen_addsi3 (retry_reg, retry_reg, const1_rtx));
13795 retry_label = gen_label_rtx ();
13796 emit_label (retry_label);
13797 }
13798
13799 if (clobber_fprs_p)
13800 {
13801 if (TARGET_VX)
13802 emit_insn (gen_tbegin_1_z13 (gen_rtx_CONST_INT (VOIDmode, TBEGIN_MASK),
13803 tdb));
13804 else
13805 emit_insn (gen_tbegin_1 (gen_rtx_CONST_INT (VOIDmode, TBEGIN_MASK),
13806 tdb));
13807 }
13808 else
13809 emit_insn (gen_tbegin_nofloat_1 (gen_rtx_CONST_INT (VOIDmode, TBEGIN_MASK),
13810 tdb));
13811
13812 emit_move_insn (dest, gen_rtx_UNSPEC (SImode,
13813 gen_rtvec (1, gen_rtx_REG (CCRAWmode,
13814 CC_REGNUM)),
13815 UNSPEC_CC_TO_INT));
13816 if (retry != NULL_RTX)
13817 {
13818 const int CC0 = 1 << 3;
13819 const int CC1 = 1 << 2;
13820 const int CC3 = 1 << 0;
13821 rtx jump;
13822 rtx count = gen_reg_rtx (SImode);
13823 rtx_code_label *leave_label = gen_label_rtx ();
13824
13825 /* Exit for success and permanent failures. */
13826 jump = s390_emit_jump (leave_label,
13827 gen_rtx_EQ (VOIDmode,
13828 gen_rtx_REG (CCRAWmode, CC_REGNUM),
13829 gen_rtx_CONST_INT (VOIDmode, CC0 | CC1 | CC3)));
13830 LABEL_NUSES (leave_label) = 1;
13831
13832 /* CC2 - transient failure. Perform retry with ppa. */
13833 emit_move_insn (count, retry_plus_two);
13834 emit_insn (gen_subsi3 (count, count, retry_reg));
13835 emit_insn (gen_tx_assist (count));
13836 jump = emit_jump_insn (gen_doloop_si64 (retry_label,
13837 retry_reg,
13838 retry_reg));
13839 JUMP_LABEL (jump) = retry_label;
13840 LABEL_NUSES (retry_label) = 1;
13841 emit_label (leave_label);
13842 }
13843 }
13844
13845
13846 /* Return the decl for the target specific builtin with the function
13847 code FCODE. */
13848
13849 static tree
13850 s390_builtin_decl (unsigned fcode, bool initialized_p ATTRIBUTE_UNUSED)
13851 {
13852 if (fcode >= S390_BUILTIN_MAX)
13853 return error_mark_node;
13854
13855 return s390_builtin_decls[fcode];
13856 }
13857
13858 /* We call mcount before the function prologue. So a profiled leaf
13859 function should stay a leaf function. */
13860
13861 static bool
13862 s390_keep_leaf_when_profiled ()
13863 {
13864 return true;
13865 }
13866
13867 /* Output assembly code for the trampoline template to
13868 stdio stream FILE.
13869
13870 On S/390, we use gpr 1 internally in the trampoline code;
13871 gpr 0 is used to hold the static chain. */
13872
13873 static void
13874 s390_asm_trampoline_template (FILE *file)
13875 {
13876 rtx op[2];
13877 op[0] = gen_rtx_REG (Pmode, 0);
13878 op[1] = gen_rtx_REG (Pmode, 1);
13879
13880 if (TARGET_64BIT)
13881 {
13882 output_asm_insn ("basr\t%1,0", op); /* 2 byte */
13883 output_asm_insn ("lmg\t%0,%1,14(%1)", op); /* 6 byte */
13884 output_asm_insn ("br\t%1", op); /* 2 byte */
13885 ASM_OUTPUT_SKIP (file, (HOST_WIDE_INT)(TRAMPOLINE_SIZE - 10));
13886 }
13887 else
13888 {
13889 output_asm_insn ("basr\t%1,0", op); /* 2 byte */
13890 output_asm_insn ("lm\t%0,%1,6(%1)", op); /* 4 byte */
13891 output_asm_insn ("br\t%1", op); /* 2 byte */
13892 ASM_OUTPUT_SKIP (file, (HOST_WIDE_INT)(TRAMPOLINE_SIZE - 8));
13893 }
13894 }
13895
13896 /* Emit RTL insns to initialize the variable parts of a trampoline.
13897 FNADDR is an RTX for the address of the function's pure code.
13898 CXT is an RTX for the static chain value for the function. */
13899
13900 static void
13901 s390_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
13902 {
13903 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
13904 rtx mem;
13905
13906 emit_block_move (m_tramp, assemble_trampoline_template (),
13907 GEN_INT (2 * UNITS_PER_LONG), BLOCK_OP_NORMAL);
13908
13909 mem = adjust_address (m_tramp, Pmode, 2 * UNITS_PER_LONG);
13910 emit_move_insn (mem, cxt);
13911 mem = adjust_address (m_tramp, Pmode, 3 * UNITS_PER_LONG);
13912 emit_move_insn (mem, fnaddr);
13913 }
13914
13915 static void
13916 output_asm_nops (const char *user, int hw)
13917 {
13918 asm_fprintf (asm_out_file, "\t# NOPs for %s (%d halfwords)\n", user, hw);
13919 while (hw > 0)
13920 {
13921 if (hw >= 3)
13922 {
13923 output_asm_insn ("brcl\t0,0", NULL);
13924 hw -= 3;
13925 }
13926 else if (hw >= 2)
13927 {
13928 output_asm_insn ("bc\t0,0", NULL);
13929 hw -= 2;
13930 }
13931 else
13932 {
13933 output_asm_insn ("bcr\t0,0", NULL);
13934 hw -= 1;
13935 }
13936 }
13937 }
13938
13939 /* Output assembler code to FILE to call a profiler hook. */
13940
13941 void
13942 s390_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
13943 {
13944 rtx op[4];
13945
13946 fprintf (file, "# function profiler \n");
13947
13948 op[0] = gen_rtx_REG (Pmode, RETURN_REGNUM);
13949 op[1] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
13950 op[1] = gen_rtx_MEM (Pmode, plus_constant (Pmode, op[1], UNITS_PER_LONG));
13951 op[3] = GEN_INT (UNITS_PER_LONG);
13952
13953 op[2] = gen_rtx_SYMBOL_REF (Pmode, flag_fentry ? "__fentry__" : "_mcount");
13954 SYMBOL_REF_FLAGS (op[2]) |= SYMBOL_FLAG_FUNCTION;
13955 if (flag_pic && !TARGET_64BIT)
13956 {
13957 op[2] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op[2]), UNSPEC_PLT31);
13958 op[2] = gen_rtx_CONST (Pmode, op[2]);
13959 }
13960
13961 if (flag_record_mcount)
13962 fprintf (file, "1:\n");
13963
13964 if (flag_fentry)
13965 {
13966 if (flag_nop_mcount)
13967 output_asm_nops ("-mnop-mcount", /* brasl */ 3);
13968 else if (cfun->static_chain_decl)
13969 warning (OPT_Wcannot_profile, "nested functions cannot be profiled "
13970 "with %<-mfentry%> on s390");
13971 else
13972 output_asm_insn ("brasl\t0,%2%K2", op);
13973 }
13974 else if (TARGET_64BIT)
13975 {
13976 if (flag_nop_mcount)
13977 output_asm_nops ("-mnop-mcount", /* stg */ 3 + /* brasl */ 3 +
13978 /* lg */ 3);
13979 else
13980 {
13981 output_asm_insn ("stg\t%0,%1", op);
13982 if (flag_dwarf2_cfi_asm)
13983 output_asm_insn (".cfi_rel_offset\t%0,%3", op);
13984 output_asm_insn ("brasl\t%0,%2%K2", op);
13985 output_asm_insn ("lg\t%0,%1", op);
13986 if (flag_dwarf2_cfi_asm)
13987 output_asm_insn (".cfi_restore\t%0", op);
13988 }
13989 }
13990 else
13991 {
13992 if (flag_nop_mcount)
13993 output_asm_nops ("-mnop-mcount", /* st */ 2 + /* brasl */ 3 +
13994 /* l */ 2);
13995 else
13996 {
13997 output_asm_insn ("st\t%0,%1", op);
13998 if (flag_dwarf2_cfi_asm)
13999 output_asm_insn (".cfi_rel_offset\t%0,%3", op);
14000 output_asm_insn ("brasl\t%0,%2%K2", op);
14001 output_asm_insn ("l\t%0,%1", op);
14002 if (flag_dwarf2_cfi_asm)
14003 output_asm_insn (".cfi_restore\t%0", op);
14004 }
14005 }
14006
14007 if (flag_record_mcount)
14008 {
14009 fprintf (file, "\t.section __mcount_loc, \"a\",@progbits\n");
14010 fprintf (file, "\t.%s 1b\n", TARGET_64BIT ? "quad" : "long");
14011 fprintf (file, "\t.previous\n");
14012 }
14013 }
14014
14015 /* Encode symbol attributes (local vs. global, tls model) of a SYMBOL_REF
14016 into its SYMBOL_REF_FLAGS. */
14017
14018 static void
14019 s390_encode_section_info (tree decl, rtx rtl, int first)
14020 {
14021 default_encode_section_info (decl, rtl, first);
14022
14023 if (VAR_P (decl))
14024 {
14025 /* Store the alignment to be able to check if we can use
14026 a larl/load-relative instruction. We only handle the cases
14027 that can go wrong (i.e. no FUNC_DECLs).
14028 All symbols without an explicit alignment are assumed to be 2
14029 byte aligned as mandated by our ABI. This behavior can be
14030 overridden for external and weak symbols with the
14031 -munaligned-symbols switch.
14032 For all external symbols without explicit alignment
14033 DECL_ALIGN is already trimmed down to 8, however for weak
14034 symbols this does not happen. These cases are catched by the
14035 type size check. */
14036 const_tree size = TYPE_SIZE (TREE_TYPE (decl));
14037 unsigned HOST_WIDE_INT size_num = (tree_fits_uhwi_p (size)
14038 ? tree_to_uhwi (size) : 0);
14039 if ((DECL_USER_ALIGN (decl) && DECL_ALIGN (decl) % 16)
14040 || (s390_unaligned_symbols_p
14041 && !decl_binds_to_current_def_p (decl)
14042 && (DECL_USER_ALIGN (decl) ? DECL_ALIGN (decl) % 16 : size_num < 16)))
14043 SYMBOL_FLAG_SET_NOTALIGN2 (XEXP (rtl, 0));
14044 else if (DECL_ALIGN (decl) % 32)
14045 SYMBOL_FLAG_SET_NOTALIGN4 (XEXP (rtl, 0));
14046 else if (DECL_ALIGN (decl) % 64)
14047 SYMBOL_FLAG_SET_NOTALIGN8 (XEXP (rtl, 0));
14048 }
14049
14050 /* Literal pool references don't have a decl so they are handled
14051 differently here. We rely on the information in the MEM_ALIGN
14052 entry to decide upon the alignment. */
14053 if (MEM_P (rtl)
14054 && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
14055 && TREE_CONSTANT_POOL_ADDRESS_P (XEXP (rtl, 0)))
14056 {
14057 if (MEM_ALIGN (rtl) == 0 || MEM_ALIGN (rtl) % 16)
14058 SYMBOL_FLAG_SET_NOTALIGN2 (XEXP (rtl, 0));
14059 else if (MEM_ALIGN (rtl) % 32)
14060 SYMBOL_FLAG_SET_NOTALIGN4 (XEXP (rtl, 0));
14061 else if (MEM_ALIGN (rtl) % 64)
14062 SYMBOL_FLAG_SET_NOTALIGN8 (XEXP (rtl, 0));
14063 }
14064 }
14065
14066 /* Output thunk to FILE that implements a C++ virtual function call (with
14067 multiple inheritance) to FUNCTION. The thunk adjusts the this pointer
14068 by DELTA, and unless VCALL_OFFSET is zero, applies an additional adjustment
14069 stored at VCALL_OFFSET in the vtable whose address is located at offset 0
14070 relative to the resulting this pointer. */
14071
14072 static void
14073 s390_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14074 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
14075 tree function)
14076 {
14077 const char *fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk));
14078 rtx op[10];
14079 int nonlocal = 0;
14080
14081 assemble_start_function (thunk, fnname);
14082 /* Make sure unwind info is emitted for the thunk if needed. */
14083 final_start_function (emit_barrier (), file, 1);
14084
14085 /* Operand 0 is the target function. */
14086 op[0] = XEXP (DECL_RTL (function), 0);
14087 if (flag_pic && !SYMBOL_REF_LOCAL_P (op[0]))
14088 {
14089 nonlocal = 1;
14090 if (!TARGET_64BIT)
14091 {
14092 op[0] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op[0]), UNSPEC_GOT);
14093 op[0] = gen_rtx_CONST (Pmode, op[0]);
14094 }
14095 }
14096
14097 /* Operand 1 is the 'this' pointer. */
14098 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
14099 op[1] = gen_rtx_REG (Pmode, 3);
14100 else
14101 op[1] = gen_rtx_REG (Pmode, 2);
14102
14103 /* Operand 2 is the delta. */
14104 op[2] = GEN_INT (delta);
14105
14106 /* Operand 3 is the vcall_offset. */
14107 op[3] = GEN_INT (vcall_offset);
14108
14109 /* Operand 4 is the temporary register. */
14110 op[4] = gen_rtx_REG (Pmode, 1);
14111
14112 /* Operands 5 to 8 can be used as labels. */
14113 op[5] = NULL_RTX;
14114 op[6] = NULL_RTX;
14115 op[7] = NULL_RTX;
14116 op[8] = NULL_RTX;
14117
14118 /* Operand 9 can be used for temporary register. */
14119 op[9] = NULL_RTX;
14120
14121 /* Generate code. */
14122 if (TARGET_64BIT)
14123 {
14124 /* Setup literal pool pointer if required. */
14125 if ((!DISP_IN_RANGE (delta)
14126 && !CONST_OK_FOR_K (delta)
14127 && !CONST_OK_FOR_Os (delta))
14128 || (!DISP_IN_RANGE (vcall_offset)
14129 && !CONST_OK_FOR_K (vcall_offset)
14130 && !CONST_OK_FOR_Os (vcall_offset)))
14131 {
14132 op[5] = gen_label_rtx ();
14133 output_asm_insn ("larl\t%4,%5", op);
14134 }
14135
14136 /* Add DELTA to this pointer. */
14137 if (delta)
14138 {
14139 if (CONST_OK_FOR_J (delta))
14140 output_asm_insn ("la\t%1,%2(%1)", op);
14141 else if (DISP_IN_RANGE (delta))
14142 output_asm_insn ("lay\t%1,%2(%1)", op);
14143 else if (CONST_OK_FOR_K (delta))
14144 output_asm_insn ("aghi\t%1,%2", op);
14145 else if (CONST_OK_FOR_Os (delta))
14146 output_asm_insn ("agfi\t%1,%2", op);
14147 else
14148 {
14149 op[6] = gen_label_rtx ();
14150 output_asm_insn ("agf\t%1,%6-%5(%4)", op);
14151 }
14152 }
14153
14154 /* Perform vcall adjustment. */
14155 if (vcall_offset)
14156 {
14157 if (DISP_IN_RANGE (vcall_offset))
14158 {
14159 output_asm_insn ("lg\t%4,0(%1)", op);
14160 output_asm_insn ("ag\t%1,%3(%4)", op);
14161 }
14162 else if (CONST_OK_FOR_K (vcall_offset))
14163 {
14164 output_asm_insn ("lghi\t%4,%3", op);
14165 output_asm_insn ("ag\t%4,0(%1)", op);
14166 output_asm_insn ("ag\t%1,0(%4)", op);
14167 }
14168 else if (CONST_OK_FOR_Os (vcall_offset))
14169 {
14170 output_asm_insn ("lgfi\t%4,%3", op);
14171 output_asm_insn ("ag\t%4,0(%1)", op);
14172 output_asm_insn ("ag\t%1,0(%4)", op);
14173 }
14174 else
14175 {
14176 op[7] = gen_label_rtx ();
14177 output_asm_insn ("llgf\t%4,%7-%5(%4)", op);
14178 output_asm_insn ("ag\t%4,0(%1)", op);
14179 output_asm_insn ("ag\t%1,0(%4)", op);
14180 }
14181 }
14182
14183 /* Jump to target. */
14184 output_asm_insn ("jg\t%0%K0", op);
14185
14186 /* Output literal pool if required. */
14187 if (op[5])
14188 {
14189 output_asm_insn (".align\t4", op);
14190 targetm.asm_out.internal_label (file, "L",
14191 CODE_LABEL_NUMBER (op[5]));
14192 }
14193 if (op[6])
14194 {
14195 targetm.asm_out.internal_label (file, "L",
14196 CODE_LABEL_NUMBER (op[6]));
14197 output_asm_insn (".long\t%2", op);
14198 }
14199 if (op[7])
14200 {
14201 targetm.asm_out.internal_label (file, "L",
14202 CODE_LABEL_NUMBER (op[7]));
14203 output_asm_insn (".long\t%3", op);
14204 }
14205 }
14206 else
14207 {
14208 /* Setup base pointer if required. */
14209 if (!vcall_offset
14210 || (!DISP_IN_RANGE (delta)
14211 && !CONST_OK_FOR_K (delta)
14212 && !CONST_OK_FOR_Os (delta))
14213 || (!DISP_IN_RANGE (delta)
14214 && !CONST_OK_FOR_K (vcall_offset)
14215 && !CONST_OK_FOR_Os (vcall_offset)))
14216 {
14217 op[5] = gen_label_rtx ();
14218 output_asm_insn ("basr\t%4,0", op);
14219 targetm.asm_out.internal_label (file, "L",
14220 CODE_LABEL_NUMBER (op[5]));
14221 }
14222
14223 /* Add DELTA to this pointer. */
14224 if (delta)
14225 {
14226 if (CONST_OK_FOR_J (delta))
14227 output_asm_insn ("la\t%1,%2(%1)", op);
14228 else if (DISP_IN_RANGE (delta))
14229 output_asm_insn ("lay\t%1,%2(%1)", op);
14230 else if (CONST_OK_FOR_K (delta))
14231 output_asm_insn ("ahi\t%1,%2", op);
14232 else if (CONST_OK_FOR_Os (delta))
14233 output_asm_insn ("afi\t%1,%2", op);
14234 else
14235 {
14236 op[6] = gen_label_rtx ();
14237 output_asm_insn ("a\t%1,%6-%5(%4)", op);
14238 }
14239 }
14240
14241 /* Perform vcall adjustment. */
14242 if (vcall_offset)
14243 {
14244 if (CONST_OK_FOR_J (vcall_offset))
14245 {
14246 output_asm_insn ("l\t%4,0(%1)", op);
14247 output_asm_insn ("a\t%1,%3(%4)", op);
14248 }
14249 else if (DISP_IN_RANGE (vcall_offset))
14250 {
14251 output_asm_insn ("l\t%4,0(%1)", op);
14252 output_asm_insn ("ay\t%1,%3(%4)", op);
14253 }
14254 else if (CONST_OK_FOR_K (vcall_offset))
14255 {
14256 output_asm_insn ("lhi\t%4,%3", op);
14257 output_asm_insn ("a\t%4,0(%1)", op);
14258 output_asm_insn ("a\t%1,0(%4)", op);
14259 }
14260 else if (CONST_OK_FOR_Os (vcall_offset))
14261 {
14262 output_asm_insn ("iilf\t%4,%3", op);
14263 output_asm_insn ("a\t%4,0(%1)", op);
14264 output_asm_insn ("a\t%1,0(%4)", op);
14265 }
14266 else
14267 {
14268 op[7] = gen_label_rtx ();
14269 output_asm_insn ("l\t%4,%7-%5(%4)", op);
14270 output_asm_insn ("a\t%4,0(%1)", op);
14271 output_asm_insn ("a\t%1,0(%4)", op);
14272 }
14273
14274 /* We had to clobber the base pointer register.
14275 Re-setup the base pointer (with a different base). */
14276 op[5] = gen_label_rtx ();
14277 output_asm_insn ("basr\t%4,0", op);
14278 targetm.asm_out.internal_label (file, "L",
14279 CODE_LABEL_NUMBER (op[5]));
14280 }
14281
14282 /* Jump to target. */
14283 op[8] = gen_label_rtx ();
14284
14285 if (!flag_pic)
14286 output_asm_insn ("l\t%4,%8-%5(%4)", op);
14287 else if (!nonlocal)
14288 output_asm_insn ("a\t%4,%8-%5(%4)", op);
14289 /* We cannot call through .plt, since .plt requires %r12 loaded. */
14290 else if (flag_pic == 1)
14291 {
14292 output_asm_insn ("a\t%4,%8-%5(%4)", op);
14293 output_asm_insn ("l\t%4,%0(%4)", op);
14294 }
14295 else if (flag_pic == 2)
14296 {
14297 op[9] = gen_rtx_REG (Pmode, 0);
14298 output_asm_insn ("l\t%9,%8-4-%5(%4)", op);
14299 output_asm_insn ("a\t%4,%8-%5(%4)", op);
14300 output_asm_insn ("ar\t%4,%9", op);
14301 output_asm_insn ("l\t%4,0(%4)", op);
14302 }
14303
14304 output_asm_insn ("br\t%4", op);
14305
14306 /* Output literal pool. */
14307 output_asm_insn (".align\t4", op);
14308
14309 if (nonlocal && flag_pic == 2)
14310 output_asm_insn (".long\t%0", op);
14311 if (nonlocal)
14312 {
14313 op[0] = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
14314 SYMBOL_REF_FLAGS (op[0]) = SYMBOL_FLAG_LOCAL;
14315 }
14316
14317 targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[8]));
14318 if (!flag_pic)
14319 output_asm_insn (".long\t%0", op);
14320 else
14321 output_asm_insn (".long\t%0-%5", op);
14322
14323 if (op[6])
14324 {
14325 targetm.asm_out.internal_label (file, "L",
14326 CODE_LABEL_NUMBER (op[6]));
14327 output_asm_insn (".long\t%2", op);
14328 }
14329 if (op[7])
14330 {
14331 targetm.asm_out.internal_label (file, "L",
14332 CODE_LABEL_NUMBER (op[7]));
14333 output_asm_insn (".long\t%3", op);
14334 }
14335 }
14336 final_end_function ();
14337 assemble_end_function (thunk, fnname);
14338 }
14339
14340 /* Output either an indirect jump or an indirect call
14341 (RETURN_ADDR_REGNO != INVALID_REGNUM) with target register REGNO
14342 using a branch trampoline disabling branch target prediction. */
14343
14344 void
14345 s390_indirect_branch_via_thunk (unsigned int regno,
14346 unsigned int return_addr_regno,
14347 rtx comparison_operator,
14348 enum s390_indirect_branch_type type)
14349 {
14350 enum s390_indirect_branch_option option;
14351
14352 if (type == s390_indirect_branch_type_return)
14353 {
14354 if (s390_return_addr_from_memory ())
14355 option = s390_opt_function_return_mem;
14356 else
14357 option = s390_opt_function_return_reg;
14358 }
14359 else if (type == s390_indirect_branch_type_jump)
14360 option = s390_opt_indirect_branch_jump;
14361 else if (type == s390_indirect_branch_type_call)
14362 option = s390_opt_indirect_branch_call;
14363 else
14364 gcc_unreachable ();
14365
14366 if (TARGET_INDIRECT_BRANCH_TABLE)
14367 {
14368 char label[32];
14369
14370 ASM_GENERATE_INTERNAL_LABEL (label,
14371 indirect_branch_table_label[option],
14372 indirect_branch_table_label_no[option]++);
14373 ASM_OUTPUT_LABEL (asm_out_file, label);
14374 }
14375
14376 if (return_addr_regno != INVALID_REGNUM)
14377 {
14378 gcc_assert (comparison_operator == NULL_RTX);
14379 fprintf (asm_out_file, " \tbrasl\t%%r%d,", return_addr_regno);
14380 }
14381 else
14382 {
14383 fputs (" \tjg", asm_out_file);
14384 if (comparison_operator != NULL_RTX)
14385 print_operand (asm_out_file, comparison_operator, 'C');
14386
14387 fputs ("\t", asm_out_file);
14388 }
14389
14390 if (TARGET_CPU_Z10)
14391 fprintf (asm_out_file,
14392 TARGET_INDIRECT_BRANCH_THUNK_NAME_EXRL "\n",
14393 regno);
14394 else
14395 fprintf (asm_out_file,
14396 TARGET_INDIRECT_BRANCH_THUNK_NAME_EX "\n",
14397 INDIRECT_BRANCH_THUNK_REGNUM, regno);
14398
14399 if ((option == s390_opt_indirect_branch_jump
14400 && cfun->machine->indirect_branch_jump == indirect_branch_thunk)
14401 || (option == s390_opt_indirect_branch_call
14402 && cfun->machine->indirect_branch_call == indirect_branch_thunk)
14403 || (option == s390_opt_function_return_reg
14404 && cfun->machine->function_return_reg == indirect_branch_thunk)
14405 || (option == s390_opt_function_return_mem
14406 && cfun->machine->function_return_mem == indirect_branch_thunk))
14407 {
14408 if (TARGET_CPU_Z10)
14409 indirect_branch_z10thunk_mask |= (1 << regno);
14410 else
14411 indirect_branch_prez10thunk_mask |= (1 << regno);
14412 }
14413 }
14414
14415 /* Output an inline thunk for indirect jumps. EXECUTE_TARGET can
14416 either be an address register or a label pointing to the location
14417 of the jump instruction. */
14418
14419 void
14420 s390_indirect_branch_via_inline_thunk (rtx execute_target)
14421 {
14422 if (TARGET_INDIRECT_BRANCH_TABLE)
14423 {
14424 char label[32];
14425
14426 ASM_GENERATE_INTERNAL_LABEL (label,
14427 indirect_branch_table_label[s390_opt_indirect_branch_jump],
14428 indirect_branch_table_label_no[s390_opt_indirect_branch_jump]++);
14429 ASM_OUTPUT_LABEL (asm_out_file, label);
14430 }
14431
14432 if (!TARGET_ZARCH)
14433 fputs ("\t.machinemode zarch\n", asm_out_file);
14434
14435 if (REG_P (execute_target))
14436 fprintf (asm_out_file, "\tex\t%%r0,0(%%r%d)\n", REGNO (execute_target));
14437 else
14438 output_asm_insn ("\texrl\t%%r0,%0", &execute_target);
14439
14440 if (!TARGET_ZARCH)
14441 fputs ("\t.machinemode esa\n", asm_out_file);
14442
14443 fputs ("0:\tj\t0b\n", asm_out_file);
14444 }
14445
14446 static bool
14447 s390_valid_pointer_mode (scalar_int_mode mode)
14448 {
14449 return (mode == SImode || (TARGET_64BIT && mode == DImode));
14450 }
14451
14452 /* Checks whether the given CALL_EXPR would use a caller
14453 saved register. This is used to decide whether sibling call
14454 optimization could be performed on the respective function
14455 call. */
14456
14457 static bool
14458 s390_call_saved_register_used (tree call_expr)
14459 {
14460 CUMULATIVE_ARGS cum_v;
14461 cumulative_args_t cum;
14462 tree parameter;
14463 rtx parm_rtx;
14464 int reg, i;
14465
14466 INIT_CUMULATIVE_ARGS (cum_v, NULL, NULL, 0, 0);
14467 cum = pack_cumulative_args (&cum_v);
14468
14469 for (i = 0; i < call_expr_nargs (call_expr); i++)
14470 {
14471 parameter = CALL_EXPR_ARG (call_expr, i);
14472 gcc_assert (parameter);
14473
14474 /* For an undeclared variable passed as parameter we will get
14475 an ERROR_MARK node here. */
14476 if (TREE_CODE (parameter) == ERROR_MARK)
14477 return true;
14478
14479 /* We assume that in the target function all parameters are
14480 named. This only has an impact on vector argument register
14481 usage none of which is call-saved. */
14482 function_arg_info arg (TREE_TYPE (parameter), /*named=*/true);
14483 apply_pass_by_reference_rules (&cum_v, arg);
14484
14485 parm_rtx = s390_function_arg (cum, arg);
14486
14487 s390_function_arg_advance (cum, arg);
14488
14489 if (!parm_rtx)
14490 continue;
14491
14492 if (REG_P (parm_rtx))
14493 {
14494 int size = s390_function_arg_size (arg.mode, arg.type);
14495 int nregs = (size + UNITS_PER_LONG - 1) / UNITS_PER_LONG;
14496
14497 for (reg = 0; reg < nregs; reg++)
14498 if (!call_used_or_fixed_reg_p (reg + REGNO (parm_rtx)))
14499 {
14500 rtx parm;
14501 /* Allow passing through unmodified value from caller,
14502 see PR119873. */
14503 if (CALL_EXPR_MUST_TAIL_CALL (call_expr)
14504 && TREE_CODE (parameter) == SSA_NAME
14505 && SSA_NAME_IS_DEFAULT_DEF (parameter)
14506 && SSA_NAME_VAR (parameter)
14507 && TREE_CODE (SSA_NAME_VAR (parameter)) == PARM_DECL
14508 && (parm = DECL_INCOMING_RTL (SSA_NAME_VAR (parameter)))
14509 && REG_P (parm)
14510 && REGNO (parm) == REGNO (parm_rtx)
14511 && REG_NREGS (parm) == REG_NREGS (parm_rtx))
14512 break;
14513 return true;
14514 }
14515 }
14516 else if (GET_CODE (parm_rtx) == PARALLEL)
14517 {
14518 int i;
14519
14520 for (i = 0; i < XVECLEN (parm_rtx, 0); i++)
14521 {
14522 rtx r = XEXP (XVECEXP (parm_rtx, 0, i), 0);
14523
14524 gcc_assert (REG_P (r));
14525 gcc_assert (REG_NREGS (r) == 1);
14526
14527 if (!call_used_or_fixed_reg_p (REGNO (r)))
14528 {
14529 rtx parm;
14530 if (CALL_EXPR_MUST_TAIL_CALL (call_expr)
14531 && TREE_CODE (parameter) == SSA_NAME
14532 && SSA_NAME_IS_DEFAULT_DEF (parameter)
14533 && SSA_NAME_VAR (parameter)
14534 && TREE_CODE (SSA_NAME_VAR (parameter)) == PARM_DECL
14535 && (parm = DECL_INCOMING_RTL (SSA_NAME_VAR (parameter)))
14536 && rtx_equal_p (parm_rtx, parm))
14537 break;
14538 return true;
14539 }
14540 }
14541 }
14542 }
14543 return false;
14544 }
14545
14546 /* Return true if the given call expression can be
14547 turned into a sibling call.
14548 DECL holds the declaration of the function to be called whereas
14549 EXP is the call expression itself. */
14550
14551 static bool
14552 s390_function_ok_for_sibcall (tree decl, tree exp)
14553 {
14554 /* The TPF epilogue uses register 1. */
14555 if (TARGET_TPF_PROFILING)
14556 return false;
14557
14558 /* The 31 bit PLT code uses register 12 (GOT pointer - caller saved)
14559 which would have to be restored before the sibcall. */
14560 if (!TARGET_64BIT && flag_pic && decl && !targetm.binds_local_p (decl))
14561 return false;
14562
14563 /* The thunks for indirect branches require r1 if no exrl is
14564 available. r1 might not be available when doing a sibling
14565 call. */
14566 if (TARGET_INDIRECT_BRANCH_NOBP_CALL
14567 && !TARGET_CPU_Z10
14568 && !decl)
14569 return false;
14570
14571 /* Register 6 on s390 is available as an argument register but unfortunately
14572 "caller saved". This makes functions needing this register for arguments
14573 not suitable for sibcalls, unless the same value is passed from the
14574 caller. */
14575 return !s390_call_saved_register_used (exp);
14576 }
14577
14578 /* Return the fixed registers used for condition codes. */
14579
14580 static bool
14581 s390_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
14582 {
14583 *p1 = CC_REGNUM;
14584 *p2 = INVALID_REGNUM;
14585
14586 return true;
14587 }
14588
14589 /* This function is used by the call expanders of the machine description.
14590 It emits the call insn itself together with the necessary operations
14591 to adjust the target address and returns the emitted insn.
14592 ADDR_LOCATION is the target address rtx
14593 TLS_CALL the location of the thread-local symbol
14594 RESULT_REG the register where the result of the call should be stored
14595 RETADDR_REG the register where the return address should be stored
14596 If this parameter is NULL_RTX the call is considered
14597 to be a sibling call. */
14598
14599 rtx_insn *
14600 s390_emit_call (rtx addr_location, rtx tls_call, rtx result_reg,
14601 rtx retaddr_reg)
14602 {
14603 bool plt31_call_p = false;
14604 rtx_insn *insn;
14605 rtx vec[4] = { NULL_RTX };
14606 int elts = 0;
14607 rtx *call = &vec[0];
14608 rtx *clobber_ret_reg = &vec[1];
14609 rtx *use = &vec[2];
14610 rtx *clobber_thunk_reg = &vec[3];
14611 int i;
14612
14613 /* Direct function calls need special treatment. */
14614 if (GET_CODE (addr_location) == SYMBOL_REF)
14615 {
14616 /* When calling a global routine in PIC mode, we must
14617 replace the symbol itself with the PLT stub. */
14618 if (flag_pic && !SYMBOL_REF_LOCAL_P (addr_location) && !TARGET_64BIT)
14619 {
14620 if (retaddr_reg != NULL_RTX)
14621 {
14622 addr_location = gen_rtx_UNSPEC (Pmode,
14623 gen_rtvec (1, addr_location),
14624 UNSPEC_PLT31);
14625 addr_location = gen_rtx_CONST (Pmode, addr_location);
14626 plt31_call_p = true;
14627 }
14628 else
14629 /* For -fpic code the PLT entries might use r12 which is
14630 call-saved. Therefore we cannot do a sibcall when
14631 calling directly using a symbol ref. When reaching
14632 this point we decided (in s390_function_ok_for_sibcall)
14633 to do a sibcall for a function pointer but one of the
14634 optimizers was able to get rid of the function pointer
14635 by propagating the symbol ref into the call. This
14636 optimization is illegal for S/390 so we turn the direct
14637 call into a indirect call again. */
14638 addr_location = force_reg (Pmode, addr_location);
14639 }
14640 }
14641
14642 /* If it is already an indirect call or the code above moved the
14643 SYMBOL_REF to somewhere else make sure the address can be found in
14644 register 1. */
14645 if (retaddr_reg == NULL_RTX
14646 && GET_CODE (addr_location) != SYMBOL_REF
14647 && !plt31_call_p)
14648 {
14649 emit_move_insn (gen_rtx_REG (Pmode, SIBCALL_REGNUM), addr_location);
14650 addr_location = gen_rtx_REG (Pmode, SIBCALL_REGNUM);
14651 }
14652
14653 if (TARGET_INDIRECT_BRANCH_NOBP_CALL
14654 && GET_CODE (addr_location) != SYMBOL_REF
14655 && !plt31_call_p)
14656 {
14657 /* Indirect branch thunks require the target to be a single GPR. */
14658 addr_location = force_reg (Pmode, addr_location);
14659
14660 /* Without exrl the indirect branch thunks need an additional
14661 register for larl;ex */
14662 if (!TARGET_CPU_Z10)
14663 {
14664 *clobber_thunk_reg = gen_rtx_REG (Pmode, INDIRECT_BRANCH_THUNK_REGNUM);
14665 *clobber_thunk_reg = gen_rtx_CLOBBER (VOIDmode, *clobber_thunk_reg);
14666 }
14667 }
14668
14669 addr_location = gen_rtx_MEM (QImode, addr_location);
14670 *call = gen_rtx_CALL (VOIDmode, addr_location, const0_rtx);
14671
14672 if (result_reg != NULL_RTX)
14673 *call = gen_rtx_SET (result_reg, *call);
14674
14675 if (retaddr_reg != NULL_RTX)
14676 {
14677 *clobber_ret_reg = gen_rtx_CLOBBER (VOIDmode, retaddr_reg);
14678
14679 if (tls_call != NULL_RTX)
14680 *use = gen_rtx_USE (VOIDmode, tls_call);
14681 }
14682
14683
14684 for (i = 0; i < 4; i++)
14685 if (vec[i] != NULL_RTX)
14686 elts++;
14687
14688 if (elts > 1)
14689 {
14690 rtvec v;
14691 int e = 0;
14692
14693 v = rtvec_alloc (elts);
14694 for (i = 0; i < 4; i++)
14695 if (vec[i] != NULL_RTX)
14696 {
14697 RTVEC_ELT (v, e) = vec[i];
14698 e++;
14699 }
14700
14701 *call = gen_rtx_PARALLEL (VOIDmode, v);
14702 }
14703
14704 insn = emit_call_insn (*call);
14705
14706 /* 31-bit PLT stubs and tls calls use the GOT register implicitly. */
14707 if (plt31_call_p || tls_call != NULL_RTX)
14708 {
14709 /* s390_function_ok_for_sibcall should
14710 have denied sibcalls in this case. */
14711 gcc_assert (retaddr_reg != NULL_RTX);
14712 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, 12));
14713 }
14714 return insn;
14715 }
14716
14717 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE. */
14718
14719 static void
14720 s390_conditional_register_usage (void)
14721 {
14722 int i;
14723
14724 if (flag_pic)
14725 fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
14726 fixed_regs[BASE_REGNUM] = 0;
14727 fixed_regs[RETURN_REGNUM] = 0;
14728 if (TARGET_64BIT)
14729 {
14730 for (i = FPR8_REGNUM; i <= FPR15_REGNUM; i++)
14731 call_used_regs[i] = 0;
14732 }
14733 else
14734 {
14735 call_used_regs[FPR4_REGNUM] = 0;
14736 call_used_regs[FPR6_REGNUM] = 0;
14737 }
14738
14739 if (TARGET_SOFT_FLOAT)
14740 {
14741 for (i = FPR0_REGNUM; i <= FPR15_REGNUM; i++)
14742 fixed_regs[i] = 1;
14743 }
14744
14745 /* Disable v16 - v31 for non-vector target. */
14746 if (!TARGET_VX)
14747 {
14748 for (i = VR16_REGNUM; i <= VR31_REGNUM; i++)
14749 fixed_regs[i] = call_used_regs[i] = 1;
14750 }
14751 }
14752
14753 /* Corresponding function to eh_return expander. */
14754
14755 static GTY(()) rtx s390_tpf_eh_return_symbol;
14756 void
14757 s390_emit_tpf_eh_return (rtx target)
14758 {
14759 rtx_insn *insn;
14760 rtx reg, orig_ra;
14761
14762 if (!s390_tpf_eh_return_symbol)
14763 {
14764 s390_tpf_eh_return_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tpf_eh_return");
14765 SYMBOL_REF_FLAGS (s390_tpf_eh_return_symbol) |= SYMBOL_FLAG_FUNCTION;
14766 }
14767
14768 reg = gen_rtx_REG (Pmode, 2);
14769 orig_ra = gen_rtx_REG (Pmode, 3);
14770
14771 emit_move_insn (reg, target);
14772 emit_move_insn (orig_ra, get_hard_reg_initial_val (Pmode, RETURN_REGNUM));
14773 insn = s390_emit_call (s390_tpf_eh_return_symbol, NULL_RTX, reg,
14774 gen_rtx_REG (Pmode, RETURN_REGNUM));
14775 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
14776 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), orig_ra);
14777
14778 emit_move_insn (EH_RETURN_HANDLER_RTX, reg);
14779 }
14780
14781 /* Rework the prologue/epilogue to avoid saving/restoring
14782 registers unnecessarily. */
14783
14784 static void
14785 s390_optimize_prologue (void)
14786 {
14787 rtx_insn *insn, *new_insn, *next_insn;
14788
14789 /* Do a final recompute of the frame-related data. */
14790 s390_optimize_register_info ();
14791
14792 /* If all special registers are in fact used, there's nothing we
14793 can do, so no point in walking the insn list. */
14794
14795 if (cfun_frame_layout.first_save_gpr <= BASE_REGNUM
14796 && cfun_frame_layout.last_save_gpr >= BASE_REGNUM)
14797 return;
14798
14799 /* Search for prologue/epilogue insns and replace them. */
14800 for (insn = get_insns (); insn; insn = next_insn)
14801 {
14802 int first, last, off;
14803 rtx set, base, offset;
14804 rtx pat;
14805
14806 next_insn = NEXT_INSN (insn);
14807
14808 if (! NONJUMP_INSN_P (insn) || ! RTX_FRAME_RELATED_P (insn))
14809 continue;
14810
14811 pat = PATTERN (insn);
14812
14813 /* Remove ldgr/lgdr instructions used for saving and restore
14814 GPRs if possible. */
14815 if (TARGET_Z10)
14816 {
14817 rtx tmp_pat = pat;
14818
14819 if (INSN_CODE (insn) == CODE_FOR_stack_restore_from_fpr)
14820 tmp_pat = XVECEXP (pat, 0, 0);
14821
14822 if (GET_CODE (tmp_pat) == SET
14823 && GET_MODE (SET_SRC (tmp_pat)) == DImode
14824 && REG_P (SET_SRC (tmp_pat))
14825 && REG_P (SET_DEST (tmp_pat)))
14826 {
14827 int src_regno = REGNO (SET_SRC (tmp_pat));
14828 int dest_regno = REGNO (SET_DEST (tmp_pat));
14829 int gpr_regno;
14830 int fpr_regno;
14831
14832 if (!((GENERAL_REGNO_P (src_regno)
14833 && FP_REGNO_P (dest_regno))
14834 || (FP_REGNO_P (src_regno)
14835 && GENERAL_REGNO_P (dest_regno))))
14836 continue;
14837
14838 gpr_regno = GENERAL_REGNO_P (src_regno) ? src_regno : dest_regno;
14839 fpr_regno = FP_REGNO_P (src_regno) ? src_regno : dest_regno;
14840
14841 /* GPR must be call-saved, FPR must be call-clobbered. */
14842 if (!call_used_regs[fpr_regno]
14843 || call_used_regs[gpr_regno])
14844 continue;
14845
14846 /* It must not happen that what we once saved in an FPR now
14847 needs a stack slot. */
14848 gcc_assert (cfun_gpr_save_slot (gpr_regno) != SAVE_SLOT_STACK);
14849
14850 if (cfun_gpr_save_slot (gpr_regno) == SAVE_SLOT_NONE)
14851 {
14852 remove_insn (insn);
14853 continue;
14854 }
14855 }
14856 }
14857
14858 if (GET_CODE (pat) == PARALLEL
14859 && store_multiple_operation (pat, VOIDmode))
14860 {
14861 set = XVECEXP (pat, 0, 0);
14862 first = REGNO (SET_SRC (set));
14863 last = first + XVECLEN (pat, 0) - 1;
14864 offset = const0_rtx;
14865 base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
14866 off = INTVAL (offset);
14867
14868 if (GET_CODE (base) != REG || off < 0)
14869 continue;
14870 if (cfun_frame_layout.first_save_gpr != -1
14871 && (cfun_frame_layout.first_save_gpr < first
14872 || cfun_frame_layout.last_save_gpr > last))
14873 continue;
14874 if (REGNO (base) != STACK_POINTER_REGNUM
14875 && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
14876 continue;
14877 if (first > BASE_REGNUM || last < BASE_REGNUM)
14878 continue;
14879
14880 if (cfun_frame_layout.first_save_gpr != -1)
14881 save_gprs (base,
14882 off + (cfun_frame_layout.first_save_gpr
14883 - first) * UNITS_PER_LONG,
14884 cfun_frame_layout.first_save_gpr,
14885 cfun_frame_layout.last_save_gpr, insn);
14886
14887 remove_insn (insn);
14888 continue;
14889 }
14890
14891 if (cfun_frame_layout.first_save_gpr == -1
14892 && GET_CODE (pat) == SET
14893 && GENERAL_REG_P (SET_SRC (pat))
14894 && GET_CODE (SET_DEST (pat)) == MEM)
14895 {
14896 set = pat;
14897 first = REGNO (SET_SRC (set));
14898 offset = const0_rtx;
14899 base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
14900 off = INTVAL (offset);
14901
14902 if (GET_CODE (base) != REG || off < 0)
14903 continue;
14904 if (REGNO (base) != STACK_POINTER_REGNUM
14905 && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
14906 continue;
14907
14908 remove_insn (insn);
14909 continue;
14910 }
14911
14912 if (GET_CODE (pat) == PARALLEL
14913 && load_multiple_operation (pat, VOIDmode))
14914 {
14915 set = XVECEXP (pat, 0, 0);
14916 first = REGNO (SET_DEST (set));
14917 last = first + XVECLEN (pat, 0) - 1;
14918 offset = const0_rtx;
14919 base = eliminate_constant_term (XEXP (SET_SRC (set), 0), &offset);
14920 off = INTVAL (offset);
14921
14922 if (GET_CODE (base) != REG || off < 0)
14923 continue;
14924
14925 if (cfun_frame_layout.first_restore_gpr != -1
14926 && (cfun_frame_layout.first_restore_gpr < first
14927 || cfun_frame_layout.last_restore_gpr > last))
14928 continue;
14929 if (REGNO (base) != STACK_POINTER_REGNUM
14930 && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
14931 continue;
14932 if (first > BASE_REGNUM || last < BASE_REGNUM)
14933 continue;
14934
14935 if (cfun_frame_layout.first_restore_gpr != -1)
14936 {
14937 rtx rpat = restore_gprs (base,
14938 off + (cfun_frame_layout.first_restore_gpr
14939 - first) * UNITS_PER_LONG,
14940 cfun_frame_layout.first_restore_gpr,
14941 cfun_frame_layout.last_restore_gpr);
14942
14943 /* Remove REG_CFA_RESTOREs for registers that we no
14944 longer need to save. */
14945 REG_NOTES (rpat) = REG_NOTES (insn);
14946 for (rtx *ptr = &REG_NOTES (rpat); *ptr; )
14947 if (REG_NOTE_KIND (*ptr) == REG_CFA_RESTORE
14948 && ((int) REGNO (XEXP (*ptr, 0))
14949 < cfun_frame_layout.first_restore_gpr))
14950 *ptr = XEXP (*ptr, 1);
14951 else
14952 ptr = &XEXP (*ptr, 1);
14953 new_insn = emit_insn_before (rpat, insn);
14954 RTX_FRAME_RELATED_P (new_insn) = 1;
14955 INSN_ADDRESSES_NEW (new_insn, -1);
14956 }
14957
14958 remove_insn (insn);
14959 continue;
14960 }
14961
14962 if (cfun_frame_layout.first_restore_gpr == -1
14963 && GET_CODE (pat) == SET
14964 && GENERAL_REG_P (SET_DEST (pat))
14965 && GET_CODE (SET_SRC (pat)) == MEM)
14966 {
14967 set = pat;
14968 first = REGNO (SET_DEST (set));
14969 offset = const0_rtx;
14970 base = eliminate_constant_term (XEXP (SET_SRC (set), 0), &offset);
14971 off = INTVAL (offset);
14972
14973 if (GET_CODE (base) != REG || off < 0)
14974 continue;
14975
14976 if (REGNO (base) != STACK_POINTER_REGNUM
14977 && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
14978 continue;
14979
14980 remove_insn (insn);
14981 continue;
14982 }
14983 }
14984 }
14985
14986 /* On z10 and later the dynamic branch prediction must see the
14987 backward jump within a certain windows. If not it falls back to
14988 the static prediction. This function rearranges the loop backward
14989 branch in a way which makes the static prediction always correct.
14990 The function returns true if it added an instruction. */
14991 static bool
14992 s390_fix_long_loop_prediction (rtx_insn *insn)
14993 {
14994 rtx set = single_set (insn);
14995 rtx code_label, label_ref;
14996 rtx_insn *uncond_jump;
14997 rtx_insn *cur_insn;
14998 rtx tmp;
14999 int distance;
15000
15001 /* This will exclude branch on count and branch on index patterns
15002 since these are correctly statically predicted.
15003
15004 The additional check for a PARALLEL is required here since
15005 single_set might be != NULL for PARALLELs where the set of the
15006 iteration variable is dead. */
15007 if (GET_CODE (PATTERN (insn)) == PARALLEL
15008 || !set
15009 || SET_DEST (set) != pc_rtx
15010 || GET_CODE (SET_SRC(set)) != IF_THEN_ELSE)
15011 return false;
15012
15013 /* Skip conditional returns. */
15014 if (ANY_RETURN_P (XEXP (SET_SRC (set), 1))
15015 && XEXP (SET_SRC (set), 2) == pc_rtx)
15016 return false;
15017
15018 label_ref = (GET_CODE (XEXP (SET_SRC (set), 1)) == LABEL_REF ?
15019 XEXP (SET_SRC (set), 1) : XEXP (SET_SRC (set), 2));
15020
15021 gcc_assert (GET_CODE (label_ref) == LABEL_REF);
15022
15023 code_label = XEXP (label_ref, 0);
15024
15025 if (INSN_ADDRESSES (INSN_UID (code_label)) == -1
15026 || INSN_ADDRESSES (INSN_UID (insn)) == -1
15027 || (INSN_ADDRESSES (INSN_UID (insn))
15028 - INSN_ADDRESSES (INSN_UID (code_label)) < PREDICT_DISTANCE))
15029 return false;
15030
15031 for (distance = 0, cur_insn = PREV_INSN (insn);
15032 distance < PREDICT_DISTANCE - 6;
15033 distance += get_attr_length (cur_insn), cur_insn = PREV_INSN (cur_insn))
15034 if (!cur_insn || JUMP_P (cur_insn) || LABEL_P (cur_insn))
15035 return false;
15036
15037 rtx_code_label *new_label = gen_label_rtx ();
15038 uncond_jump = emit_jump_insn_after (
15039 gen_rtx_SET (pc_rtx,
15040 gen_rtx_LABEL_REF (VOIDmode, code_label)),
15041 insn);
15042 emit_label_after (new_label, uncond_jump);
15043
15044 tmp = XEXP (SET_SRC (set), 1);
15045 XEXP (SET_SRC (set), 1) = XEXP (SET_SRC (set), 2);
15046 XEXP (SET_SRC (set), 2) = tmp;
15047 INSN_CODE (insn) = -1;
15048
15049 XEXP (label_ref, 0) = new_label;
15050 JUMP_LABEL (insn) = new_label;
15051 JUMP_LABEL (uncond_jump) = code_label;
15052
15053 return true;
15054 }
15055
15056 /* Returns 1 if INSN reads the value of REG for purposes not related
15057 to addressing of memory, and 0 otherwise. */
15058 static int
15059 s390_non_addr_reg_read_p (rtx reg, rtx_insn *insn)
15060 {
15061 return reg_referenced_p (reg, PATTERN (insn))
15062 && !reg_used_in_mem_p (REGNO (reg), PATTERN (insn));
15063 }
15064
15065 /* Starting from INSN find_cond_jump looks downwards in the insn
15066 stream for a single jump insn which is the last user of the
15067 condition code set in INSN. */
15068 static rtx_insn *
15069 find_cond_jump (rtx_insn *insn)
15070 {
15071 for (; insn; insn = NEXT_INSN (insn))
15072 {
15073 rtx ite, cc;
15074
15075 if (LABEL_P (insn))
15076 break;
15077
15078 if (!JUMP_P (insn))
15079 {
15080 if (reg_mentioned_p (gen_rtx_REG (CCmode, CC_REGNUM), insn))
15081 break;
15082 continue;
15083 }
15084
15085 /* This will be triggered by a return. */
15086 if (GET_CODE (PATTERN (insn)) != SET)
15087 break;
15088
15089 gcc_assert (SET_DEST (PATTERN (insn)) == pc_rtx);
15090 ite = SET_SRC (PATTERN (insn));
15091
15092 if (GET_CODE (ite) != IF_THEN_ELSE)
15093 break;
15094
15095 cc = XEXP (XEXP (ite, 0), 0);
15096 if (!REG_P (cc) || !CC_REGNO_P (REGNO (cc)))
15097 break;
15098
15099 if (find_reg_note (insn, REG_DEAD, cc))
15100 return insn;
15101 break;
15102 }
15103
15104 return NULL;
15105 }
15106
15107 /* Swap the condition in COND and the operands in OP0 and OP1 so that
15108 the semantics does not change. If NULL_RTX is passed as COND the
15109 function tries to find the conditional jump starting with INSN. */
15110 static void
15111 s390_swap_cmp (rtx cond, rtx *op0, rtx *op1, rtx_insn *insn)
15112 {
15113 rtx tmp = *op0;
15114
15115 if (cond == NULL_RTX)
15116 {
15117 rtx_insn *jump = find_cond_jump (NEXT_INSN (insn));
15118 rtx set = jump ? single_set (jump) : NULL_RTX;
15119
15120 if (set == NULL_RTX)
15121 return;
15122
15123 cond = XEXP (SET_SRC (set), 0);
15124 }
15125
15126 *op0 = *op1;
15127 *op1 = tmp;
15128 PUT_CODE (cond, swap_condition (GET_CODE (cond)));
15129 }
15130
15131 /* On z10, instructions of the compare-and-branch family have the
15132 property to access the register occurring as second operand with
15133 its bits complemented. If such a compare is grouped with a second
15134 instruction that accesses the same register non-complemented, and
15135 if that register's value is delivered via a bypass, then the
15136 pipeline recycles, thereby causing significant performance decline.
15137 This function locates such situations and exchanges the two
15138 operands of the compare. The function return true whenever it
15139 added an insn. */
15140 static bool
15141 s390_z10_optimize_cmp (rtx_insn *insn)
15142 {
15143 rtx_insn *prev_insn, *next_insn;
15144 bool insn_added_p = false;
15145 rtx cond, *op0, *op1;
15146
15147 if (GET_CODE (PATTERN (insn)) == PARALLEL)
15148 {
15149 /* Handle compare and branch and branch on count
15150 instructions. */
15151 rtx pattern = single_set (insn);
15152
15153 if (!pattern
15154 || SET_DEST (pattern) != pc_rtx
15155 || GET_CODE (SET_SRC (pattern)) != IF_THEN_ELSE)
15156 return false;
15157
15158 cond = XEXP (SET_SRC (pattern), 0);
15159 op0 = &XEXP (cond, 0);
15160 op1 = &XEXP (cond, 1);
15161 }
15162 else if (GET_CODE (PATTERN (insn)) == SET)
15163 {
15164 rtx src, dest;
15165
15166 /* Handle normal compare instructions. */
15167 src = SET_SRC (PATTERN (insn));
15168 dest = SET_DEST (PATTERN (insn));
15169
15170 if (!REG_P (dest)
15171 || !CC_REGNO_P (REGNO (dest))
15172 || GET_CODE (src) != COMPARE)
15173 return false;
15174
15175 /* s390_swap_cmp will try to find the conditional
15176 jump when passing NULL_RTX as condition. */
15177 cond = NULL_RTX;
15178 op0 = &XEXP (src, 0);
15179 op1 = &XEXP (src, 1);
15180 }
15181 else
15182 return false;
15183
15184 if (!REG_P (*op0) || !REG_P (*op1))
15185 return false;
15186
15187 if (GET_MODE_CLASS (GET_MODE (*op0)) != MODE_INT)
15188 return false;
15189
15190 /* Swap the COMPARE arguments and its mask if there is a
15191 conflicting access in the previous insn. */
15192 prev_insn = prev_active_insn (insn);
15193 if (prev_insn != NULL_RTX && INSN_P (prev_insn)
15194 && reg_referenced_p (*op1, PATTERN (prev_insn)))
15195 s390_swap_cmp (cond, op0, op1, insn);
15196
15197 /* Check if there is a conflict with the next insn. If there
15198 was no conflict with the previous insn, then swap the
15199 COMPARE arguments and its mask. If we already swapped
15200 the operands, or if swapping them would cause a conflict
15201 with the previous insn, issue a NOP after the COMPARE in
15202 order to separate the two instuctions. */
15203 next_insn = next_active_insn (insn);
15204 if (next_insn != NULL_RTX && INSN_P (next_insn)
15205 && s390_non_addr_reg_read_p (*op1, next_insn))
15206 {
15207 if (prev_insn != NULL_RTX && INSN_P (prev_insn)
15208 && s390_non_addr_reg_read_p (*op0, prev_insn))
15209 {
15210 if (REGNO (*op1) == 0)
15211 emit_insn_after (gen_nop_lr1 (), insn);
15212 else
15213 emit_insn_after (gen_nop_lr0 (), insn);
15214 insn_added_p = true;
15215 }
15216 else
15217 s390_swap_cmp (cond, op0, op1, insn);
15218 }
15219 return insn_added_p;
15220 }
15221
15222 /* Number of INSNs to be scanned backward in the last BB of the loop
15223 and forward in the first BB of the loop. This usually should be a
15224 bit more than the number of INSNs which could go into one
15225 group. */
15226 #define S390_OSC_SCAN_INSN_NUM 5
15227
15228 /* Scan LOOP for static OSC collisions and return true if a osc_break
15229 should be issued for this loop. */
15230 static bool
15231 s390_adjust_loop_scan_osc (struct loop* loop)
15232
15233 {
15234 HARD_REG_SET modregs, newregs;
15235 rtx_insn *insn, *store_insn = NULL;
15236 rtx set;
15237 struct s390_address addr_store, addr_load;
15238 subrtx_iterator::array_type array;
15239 int insn_count;
15240
15241 CLEAR_HARD_REG_SET (modregs);
15242
15243 insn_count = 0;
15244 FOR_BB_INSNS_REVERSE (loop->latch, insn)
15245 {
15246 if (!INSN_P (insn) || INSN_CODE (insn) <= 0)
15247 continue;
15248
15249 insn_count++;
15250 if (insn_count > S390_OSC_SCAN_INSN_NUM)
15251 return false;
15252
15253 find_all_hard_reg_sets (insn, &newregs, true);
15254 modregs |= newregs;
15255
15256 set = single_set (insn);
15257 if (!set)
15258 continue;
15259
15260 if (MEM_P (SET_DEST (set))
15261 && s390_decompose_address (XEXP (SET_DEST (set), 0), &addr_store))
15262 {
15263 store_insn = insn;
15264 break;
15265 }
15266 }
15267
15268 if (store_insn == NULL_RTX)
15269 return false;
15270
15271 insn_count = 0;
15272 FOR_BB_INSNS (loop->header, insn)
15273 {
15274 if (!INSN_P (insn) || INSN_CODE (insn) <= 0)
15275 continue;
15276
15277 if (insn == store_insn)
15278 return false;
15279
15280 insn_count++;
15281 if (insn_count > S390_OSC_SCAN_INSN_NUM)
15282 return false;
15283
15284 find_all_hard_reg_sets (insn, &newregs, true);
15285 modregs |= newregs;
15286
15287 set = single_set (insn);
15288 if (!set)
15289 continue;
15290
15291 /* An intermediate store disrupts static OSC checking
15292 anyway. */
15293 if (MEM_P (SET_DEST (set))
15294 && s390_decompose_address (XEXP (SET_DEST (set), 0), NULL))
15295 return false;
15296
15297 FOR_EACH_SUBRTX (iter, array, SET_SRC (set), NONCONST)
15298 if (MEM_P (*iter)
15299 && s390_decompose_address (XEXP (*iter, 0), &addr_load)
15300 && rtx_equal_p (addr_load.base, addr_store.base)
15301 && rtx_equal_p (addr_load.indx, addr_store.indx)
15302 && rtx_equal_p (addr_load.disp, addr_store.disp))
15303 {
15304 if ((addr_load.base != NULL_RTX
15305 && TEST_HARD_REG_BIT (modregs, REGNO (addr_load.base)))
15306 || (addr_load.indx != NULL_RTX
15307 && TEST_HARD_REG_BIT (modregs, REGNO (addr_load.indx))))
15308 return true;
15309 }
15310 }
15311 return false;
15312 }
15313
15314 /* Look for adjustments which can be done on simple innermost
15315 loops. */
15316 static void
15317 s390_adjust_loops ()
15318 {
15319 df_analyze ();
15320 compute_bb_for_insn ();
15321
15322 /* Find the loops. */
15323 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
15324
15325 for (auto loop : loops_list (cfun, LI_ONLY_INNERMOST))
15326 {
15327 if (dump_file)
15328 {
15329 flow_loop_dump (loop, dump_file, NULL, 0);
15330 fprintf (dump_file, ";; OSC loop scan Loop: ");
15331 }
15332 if (loop->latch == NULL
15333 || pc_set (BB_END (loop->latch)) == NULL_RTX
15334 || !s390_adjust_loop_scan_osc (loop))
15335 {
15336 if (dump_file)
15337 {
15338 if (loop->latch == NULL)
15339 fprintf (dump_file, " muliple backward jumps\n");
15340 else
15341 {
15342 fprintf (dump_file, " header insn: %d latch insn: %d ",
15343 INSN_UID (BB_HEAD (loop->header)),
15344 INSN_UID (BB_END (loop->latch)));
15345 if (pc_set (BB_END (loop->latch)) == NULL_RTX)
15346 fprintf (dump_file, " loop does not end with jump\n");
15347 else
15348 fprintf (dump_file, " not instrumented\n");
15349 }
15350 }
15351 }
15352 else
15353 {
15354 rtx_insn *new_insn;
15355
15356 if (dump_file)
15357 fprintf (dump_file, " adding OSC break insn: ");
15358 new_insn = emit_insn_before (gen_osc_break (),
15359 BB_END (loop->latch));
15360 INSN_ADDRESSES_NEW (new_insn, -1);
15361 }
15362 }
15363
15364 loop_optimizer_finalize ();
15365
15366 df_finish_pass (false);
15367 }
15368
15369 /* Perform machine-dependent processing. */
15370
15371 static void
15372 s390_reorg (void)
15373 {
15374 struct constant_pool *pool;
15375 rtx_insn *insn;
15376 int hw_before, hw_after;
15377
15378 if (s390_tune == PROCESSOR_2964_Z13)
15379 s390_adjust_loops ();
15380
15381 /* Make sure all splits have been performed; splits after
15382 machine_dependent_reorg might confuse insn length counts. */
15383 split_all_insns_noflow ();
15384
15385 /* Install the main literal pool and the associated base
15386 register load insns. The literal pool might be > 4096 bytes in
15387 size, so that some of its elements cannot be directly accessed.
15388
15389 To fix this, we split the single literal pool into multiple
15390 pool chunks, reloading the pool base register at various
15391 points throughout the function to ensure it always points to
15392 the pool chunk the following code expects. */
15393
15394 /* Collect the literal pool. */
15395 pool = s390_mainpool_start ();
15396 if (pool)
15397 {
15398 /* Finish up literal pool related changes. */
15399 s390_mainpool_finish (pool);
15400 }
15401 else
15402 {
15403 /* If literal pool overflowed, chunkify it. */
15404 pool = s390_chunkify_start ();
15405 s390_chunkify_finish (pool);
15406 }
15407
15408 /* Generate out-of-pool execute target insns. */
15409 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
15410 {
15411 rtx label;
15412 rtx_insn *target;
15413
15414 label = s390_execute_label (insn);
15415 if (!label)
15416 continue;
15417
15418 gcc_assert (label != const0_rtx);
15419
15420 target = emit_label (XEXP (label, 0));
15421 INSN_ADDRESSES_NEW (target, -1);
15422
15423 if (JUMP_P (insn))
15424 {
15425 target = emit_jump_insn (s390_execute_target (insn));
15426 /* This is important in order to keep a table jump
15427 pointing at the jump table label. Only this makes it
15428 being recognized as table jump. */
15429 JUMP_LABEL (target) = JUMP_LABEL (insn);
15430 }
15431 else
15432 target = emit_insn (s390_execute_target (insn));
15433 INSN_ADDRESSES_NEW (target, -1);
15434 }
15435
15436 /* Try to optimize prologue and epilogue further. */
15437 s390_optimize_prologue ();
15438
15439 /* Walk over the insns and do some >=z10 specific changes. */
15440 if (s390_tune >= PROCESSOR_2097_Z10)
15441 {
15442 rtx_insn *insn;
15443 bool insn_added_p = false;
15444
15445 /* The insn lengths and addresses have to be up to date for the
15446 following manipulations. */
15447 shorten_branches (get_insns ());
15448
15449 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
15450 {
15451 if (!INSN_P (insn) || INSN_CODE (insn) <= 0)
15452 continue;
15453
15454 if (JUMP_P (insn))
15455 insn_added_p |= s390_fix_long_loop_prediction (insn);
15456
15457 if ((GET_CODE (PATTERN (insn)) == PARALLEL
15458 || GET_CODE (PATTERN (insn)) == SET)
15459 && s390_tune == PROCESSOR_2097_Z10)
15460 insn_added_p |= s390_z10_optimize_cmp (insn);
15461 }
15462
15463 /* Adjust branches if we added new instructions. */
15464 if (insn_added_p)
15465 shorten_branches (get_insns ());
15466 }
15467
15468 s390_function_num_hotpatch_hw (current_function_decl, &hw_before, &hw_after);
15469 if (hw_after > 0)
15470 {
15471 rtx_insn *insn;
15472
15473 /* Insert NOPs for hotpatching. */
15474 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
15475 /* Emit NOPs
15476 1. inside the area covered by debug information to allow setting
15477 breakpoints at the NOPs,
15478 2. before any insn which results in an asm instruction,
15479 3. before in-function labels to avoid jumping to the NOPs, for
15480 example as part of a loop,
15481 4. before any barrier in case the function is completely empty
15482 (__builtin_unreachable ()) and has neither internal labels nor
15483 active insns.
15484 */
15485 if (active_insn_p (insn) || BARRIER_P (insn) || LABEL_P (insn))
15486 break;
15487 /* Output a series of NOPs before the first active insn. */
15488 while (insn && hw_after > 0)
15489 {
15490 if (hw_after >= 3)
15491 {
15492 emit_insn_before (gen_nop_6_byte (), insn);
15493 hw_after -= 3;
15494 }
15495 else if (hw_after >= 2)
15496 {
15497 emit_insn_before (gen_nop_4_byte (), insn);
15498 hw_after -= 2;
15499 }
15500 else
15501 {
15502 emit_insn_before (gen_nop_2_byte (), insn);
15503 hw_after -= 1;
15504 }
15505 }
15506 }
15507 }
15508
15509 /* Return true if INSN is a fp load insn writing register REGNO. */
15510 static inline bool
15511 s390_fpload_toreg (rtx_insn *insn, unsigned int regno)
15512 {
15513 rtx set;
15514 enum attr_type flag = s390_safe_attr_type (insn);
15515
15516 if (flag != TYPE_FLOADSF && flag != TYPE_FLOADDF)
15517 return false;
15518
15519 set = single_set (insn);
15520
15521 if (set == NULL_RTX)
15522 return false;
15523
15524 if (!REG_P (SET_DEST (set)) || !MEM_P (SET_SRC (set)))
15525 return false;
15526
15527 if (REGNO (SET_DEST (set)) != regno)
15528 return false;
15529
15530 return true;
15531 }
15532
15533 /* This value describes the distance to be avoided between an
15534 arithmetic fp instruction and an fp load writing the same register.
15535 Z10_EARLYLOAD_DISTANCE - 1 as well as Z10_EARLYLOAD_DISTANCE + 1 is
15536 fine but the exact value has to be avoided. Otherwise the FP
15537 pipeline will throw an exception causing a major penalty. */
15538 #define Z10_EARLYLOAD_DISTANCE 7
15539
15540 /* Rearrange the ready list in order to avoid the situation described
15541 for Z10_EARLYLOAD_DISTANCE. A problematic load instruction is
15542 moved to the very end of the ready list. */
15543 static void
15544 s390_z10_prevent_earlyload_conflicts (rtx_insn **ready, int *nready_p)
15545 {
15546 unsigned int regno;
15547 int nready = *nready_p;
15548 rtx_insn *tmp;
15549 int i;
15550 rtx_insn *insn;
15551 rtx set;
15552 enum attr_type flag;
15553 int distance;
15554
15555 /* Skip DISTANCE - 1 active insns. */
15556 for (insn = last_scheduled_insn, distance = Z10_EARLYLOAD_DISTANCE - 1;
15557 distance > 0 && insn != NULL_RTX;
15558 distance--, insn = prev_active_insn (insn))
15559 if (CALL_P (insn) || JUMP_P (insn))
15560 return;
15561
15562 if (insn == NULL_RTX)
15563 return;
15564
15565 set = single_set (insn);
15566
15567 if (set == NULL_RTX || !REG_P (SET_DEST (set))
15568 || GET_MODE_CLASS (GET_MODE (SET_DEST (set))) != MODE_FLOAT)
15569 return;
15570
15571 flag = s390_safe_attr_type (insn);
15572
15573 if (flag == TYPE_FLOADSF || flag == TYPE_FLOADDF)
15574 return;
15575
15576 regno = REGNO (SET_DEST (set));
15577 i = nready - 1;
15578
15579 while (!s390_fpload_toreg (ready[i], regno) && i > 0)
15580 i--;
15581
15582 if (!i)
15583 return;
15584
15585 tmp = ready[i];
15586 memmove (&ready[1], &ready[0], sizeof (rtx_insn *) * i);
15587 ready[0] = tmp;
15588 }
15589
15590 struct s390_sched_state
15591 {
15592 /* Number of insns in the group. */
15593 int group_state;
15594 /* Execution side of the group. */
15595 int side;
15596 /* Group can only hold two insns. */
15597 bool group_of_two;
15598 } s390_sched_state;
15599
15600 static struct s390_sched_state sched_state;
15601
15602 #define S390_SCHED_ATTR_MASK_CRACKED 0x1
15603 #define S390_SCHED_ATTR_MASK_EXPANDED 0x2
15604 #define S390_SCHED_ATTR_MASK_ENDGROUP 0x4
15605 #define S390_SCHED_ATTR_MASK_GROUPALONE 0x8
15606 #define S390_SCHED_ATTR_MASK_GROUPOFTWO 0x10
15607
15608 static unsigned int
15609 s390_get_sched_attrmask (rtx_insn *insn)
15610 {
15611 unsigned int mask = 0;
15612
15613 switch (s390_tune)
15614 {
15615 case PROCESSOR_2827_ZEC12:
15616 if (get_attr_zEC12_cracked (insn))
15617 mask |= S390_SCHED_ATTR_MASK_CRACKED;
15618 if (get_attr_zEC12_expanded (insn))
15619 mask |= S390_SCHED_ATTR_MASK_EXPANDED;
15620 if (get_attr_zEC12_endgroup (insn))
15621 mask |= S390_SCHED_ATTR_MASK_ENDGROUP;
15622 if (get_attr_zEC12_groupalone (insn))
15623 mask |= S390_SCHED_ATTR_MASK_GROUPALONE;
15624 break;
15625 case PROCESSOR_2964_Z13:
15626 if (get_attr_z13_cracked (insn))
15627 mask |= S390_SCHED_ATTR_MASK_CRACKED;
15628 if (get_attr_z13_expanded (insn))
15629 mask |= S390_SCHED_ATTR_MASK_EXPANDED;
15630 if (get_attr_z13_endgroup (insn))
15631 mask |= S390_SCHED_ATTR_MASK_ENDGROUP;
15632 if (get_attr_z13_groupalone (insn))
15633 mask |= S390_SCHED_ATTR_MASK_GROUPALONE;
15634 if (get_attr_z13_groupoftwo (insn))
15635 mask |= S390_SCHED_ATTR_MASK_GROUPOFTWO;
15636 break;
15637 case PROCESSOR_3906_Z14:
15638 if (get_attr_z14_cracked (insn))
15639 mask |= S390_SCHED_ATTR_MASK_CRACKED;
15640 if (get_attr_z14_expanded (insn))
15641 mask |= S390_SCHED_ATTR_MASK_EXPANDED;
15642 if (get_attr_z14_endgroup (insn))
15643 mask |= S390_SCHED_ATTR_MASK_ENDGROUP;
15644 if (get_attr_z14_groupalone (insn))
15645 mask |= S390_SCHED_ATTR_MASK_GROUPALONE;
15646 if (get_attr_z14_groupoftwo (insn))
15647 mask |= S390_SCHED_ATTR_MASK_GROUPOFTWO;
15648 break;
15649 case PROCESSOR_8561_Z15:
15650 if (get_attr_z15_cracked (insn))
15651 mask |= S390_SCHED_ATTR_MASK_CRACKED;
15652 if (get_attr_z15_expanded (insn))
15653 mask |= S390_SCHED_ATTR_MASK_EXPANDED;
15654 if (get_attr_z15_endgroup (insn))
15655 mask |= S390_SCHED_ATTR_MASK_ENDGROUP;
15656 if (get_attr_z15_groupalone (insn))
15657 mask |= S390_SCHED_ATTR_MASK_GROUPALONE;
15658 if (get_attr_z15_groupoftwo (insn))
15659 mask |= S390_SCHED_ATTR_MASK_GROUPOFTWO;
15660 break;
15661 case PROCESSOR_3931_Z16:
15662 if (get_attr_z16_cracked (insn))
15663 mask |= S390_SCHED_ATTR_MASK_CRACKED;
15664 if (get_attr_z16_expanded (insn))
15665 mask |= S390_SCHED_ATTR_MASK_EXPANDED;
15666 if (get_attr_z16_endgroup (insn))
15667 mask |= S390_SCHED_ATTR_MASK_ENDGROUP;
15668 if (get_attr_z16_groupalone (insn))
15669 mask |= S390_SCHED_ATTR_MASK_GROUPALONE;
15670 if (get_attr_z16_groupoftwo (insn))
15671 mask |= S390_SCHED_ATTR_MASK_GROUPOFTWO;
15672 break;
15673 case PROCESSOR_9175_Z17:
15674 if (get_attr_z17_cracked (insn))
15675 mask |= S390_SCHED_ATTR_MASK_CRACKED;
15676 if (get_attr_z17_expanded (insn))
15677 mask |= S390_SCHED_ATTR_MASK_EXPANDED;
15678 if (get_attr_z17_endgroup (insn))
15679 mask |= S390_SCHED_ATTR_MASK_ENDGROUP;
15680 if (get_attr_z17_groupalone (insn))
15681 mask |= S390_SCHED_ATTR_MASK_GROUPALONE;
15682 if (get_attr_z17_groupoftwo (insn))
15683 mask |= S390_SCHED_ATTR_MASK_GROUPOFTWO;
15684 break;
15685 default:
15686 gcc_unreachable ();
15687 }
15688 return mask;
15689 }
15690
15691 static unsigned int
15692 s390_get_unit_mask (rtx_insn *insn, int *units)
15693 {
15694 unsigned int mask = 0;
15695
15696 switch (s390_tune)
15697 {
15698 case PROCESSOR_2964_Z13:
15699 *units = 4;
15700 if (get_attr_z13_unit_lsu (insn))
15701 mask |= 1 << 0;
15702 if (get_attr_z13_unit_fxa (insn))
15703 mask |= 1 << 1;
15704 if (get_attr_z13_unit_fxb (insn))
15705 mask |= 1 << 2;
15706 if (get_attr_z13_unit_vfu (insn))
15707 mask |= 1 << 3;
15708 break;
15709 case PROCESSOR_3906_Z14:
15710 *units = 4;
15711 if (get_attr_z14_unit_lsu (insn))
15712 mask |= 1 << 0;
15713 if (get_attr_z14_unit_fxa (insn))
15714 mask |= 1 << 1;
15715 if (get_attr_z14_unit_fxb (insn))
15716 mask |= 1 << 2;
15717 if (get_attr_z14_unit_vfu (insn))
15718 mask |= 1 << 3;
15719 break;
15720 case PROCESSOR_8561_Z15:
15721 *units = 4;
15722 if (get_attr_z15_unit_lsu (insn))
15723 mask |= 1 << 0;
15724 if (get_attr_z15_unit_fxa (insn))
15725 mask |= 1 << 1;
15726 if (get_attr_z15_unit_fxb (insn))
15727 mask |= 1 << 2;
15728 if (get_attr_z15_unit_vfu (insn))
15729 mask |= 1 << 3;
15730 break;
15731 case PROCESSOR_3931_Z16:
15732 *units = 4;
15733 if (get_attr_z16_unit_lsu (insn))
15734 mask |= 1 << 0;
15735 if (get_attr_z16_unit_fxa (insn))
15736 mask |= 1 << 1;
15737 if (get_attr_z16_unit_fxb (insn))
15738 mask |= 1 << 2;
15739 if (get_attr_z16_unit_vfu (insn))
15740 mask |= 1 << 3;
15741 break;
15742 case PROCESSOR_9175_Z17:
15743 *units = 4;
15744 if (get_attr_z17_unit_lsu (insn))
15745 mask |= 1 << 0;
15746 if (get_attr_z17_unit_fxa (insn))
15747 mask |= 1 << 1;
15748 if (get_attr_z17_unit_fxb (insn))
15749 mask |= 1 << 2;
15750 if (get_attr_z17_unit_vfu (insn))
15751 mask |= 1 << 3;
15752 break;
15753 default:
15754 gcc_unreachable ();
15755 }
15756 return mask;
15757 }
15758
15759 static bool
15760 s390_is_fpd (rtx_insn *insn)
15761 {
15762 if (insn == NULL_RTX)
15763 return false;
15764
15765 return get_attr_z13_unit_fpd (insn) || get_attr_z14_unit_fpd (insn)
15766 || get_attr_z15_unit_fpd (insn) || get_attr_z16_unit_fpd (insn)
15767 || get_attr_z17_unit_fpd (insn);
15768 }
15769
15770 static bool
15771 s390_is_fxd (rtx_insn *insn)
15772 {
15773 if (insn == NULL_RTX)
15774 return false;
15775
15776 return get_attr_z13_unit_fxd (insn) || get_attr_z14_unit_fxd (insn)
15777 || get_attr_z15_unit_fxd (insn) || get_attr_z16_unit_fxd (insn)
15778 || get_attr_z17_unit_fxd (insn);
15779 }
15780
15781 /* Returns TRUE if INSN is a long-running instruction. */
15782 static bool
15783 s390_is_longrunning (rtx_insn *insn)
15784 {
15785 if (insn == NULL_RTX)
15786 return false;
15787
15788 return s390_is_fxd (insn) || s390_is_fpd (insn);
15789 }
15790
15791
15792 /* Return the scheduling score for INSN. The higher the score the
15793 better. The score is calculated from the OOO scheduling attributes
15794 of INSN and the scheduling state sched_state. */
15795 static int
15796 s390_sched_score (rtx_insn *insn)
15797 {
15798 unsigned int mask = s390_get_sched_attrmask (insn);
15799 int score = 0;
15800
15801 switch (sched_state.group_state)
15802 {
15803 case 0:
15804 /* Try to put insns into the first slot which would otherwise
15805 break a group. */
15806 if ((mask & S390_SCHED_ATTR_MASK_CRACKED) != 0
15807 || (mask & S390_SCHED_ATTR_MASK_EXPANDED) != 0)
15808 score += 5;
15809 if ((mask & S390_SCHED_ATTR_MASK_GROUPALONE) != 0)
15810 score += 10;
15811 break;
15812 case 1:
15813 /* Prefer not cracked insns while trying to put together a
15814 group. */
15815 if ((mask & S390_SCHED_ATTR_MASK_CRACKED) == 0
15816 && (mask & S390_SCHED_ATTR_MASK_EXPANDED) == 0
15817 && (mask & S390_SCHED_ATTR_MASK_GROUPALONE) == 0)
15818 score += 10;
15819 if ((mask & S390_SCHED_ATTR_MASK_ENDGROUP) == 0)
15820 score += 5;
15821 /* If we are in a group of two already, try to schedule another
15822 group-of-two insn to avoid shortening another group. */
15823 if (sched_state.group_of_two
15824 && (mask & S390_SCHED_ATTR_MASK_GROUPOFTWO) != 0)
15825 score += 15;
15826 break;
15827 case 2:
15828 /* Prefer not cracked insns while trying to put together a
15829 group. */
15830 if ((mask & S390_SCHED_ATTR_MASK_CRACKED) == 0
15831 && (mask & S390_SCHED_ATTR_MASK_EXPANDED) == 0
15832 && (mask & S390_SCHED_ATTR_MASK_GROUPALONE) == 0)
15833 score += 10;
15834 /* Prefer endgroup insns in the last slot. */
15835 if ((mask & S390_SCHED_ATTR_MASK_ENDGROUP) != 0)
15836 score += 10;
15837 /* Try to avoid group-of-two insns in the last slot as they will
15838 shorten this group as well as the next one. */
15839 if ((mask & S390_SCHED_ATTR_MASK_GROUPOFTWO) != 0)
15840 score = MAX (0, score - 15);
15841 break;
15842 }
15843
15844 if (s390_tune >= PROCESSOR_2964_Z13)
15845 {
15846 int units, i;
15847 unsigned unit_mask, m = 1;
15848
15849 unit_mask = s390_get_unit_mask (insn, &units);
15850 gcc_assert (units <= MAX_SCHED_UNITS);
15851
15852 /* Add a score in range 0..MAX_SCHED_MIX_SCORE depending on how long
15853 ago the last insn of this unit type got scheduled. This is
15854 supposed to help providing a proper instruction mix to the
15855 CPU. */
15856 for (i = 0; i < units; i++, m <<= 1)
15857 if (m & unit_mask)
15858 score += (last_scheduled_unit_distance[i][sched_state.side]
15859 * MAX_SCHED_MIX_SCORE / MAX_SCHED_MIX_DISTANCE);
15860
15861 int other_side = 1 - sched_state.side;
15862
15863 /* Try to delay long-running insns when side is busy. */
15864 if (s390_is_longrunning (insn))
15865 {
15866 if (s390_is_fxd (insn))
15867 {
15868 if (fxd_longrunning[sched_state.side]
15869 && fxd_longrunning[other_side]
15870 <= fxd_longrunning[sched_state.side])
15871 score = MAX (0, score - 10);
15872
15873 else if (fxd_longrunning[other_side]
15874 >= fxd_longrunning[sched_state.side])
15875 score += 10;
15876 }
15877
15878 if (s390_is_fpd (insn))
15879 {
15880 if (fpd_longrunning[sched_state.side]
15881 && fpd_longrunning[other_side]
15882 <= fpd_longrunning[sched_state.side])
15883 score = MAX (0, score - 10);
15884
15885 else if (fpd_longrunning[other_side]
15886 >= fpd_longrunning[sched_state.side])
15887 score += 10;
15888 }
15889 }
15890 }
15891
15892 return score;
15893 }
15894
15895 /* This function is called via hook TARGET_SCHED_REORDER before
15896 issuing one insn from list READY which contains *NREADYP entries.
15897 For target z10 it reorders load instructions to avoid early load
15898 conflicts in the floating point pipeline */
15899 static int
15900 s390_sched_reorder (FILE *file, int verbose,
15901 rtx_insn **ready, int *nreadyp, int clock ATTRIBUTE_UNUSED)
15902 {
15903 if (s390_tune == PROCESSOR_2097_Z10
15904 && reload_completed
15905 && *nreadyp > 1)
15906 s390_z10_prevent_earlyload_conflicts (ready, nreadyp);
15907
15908 if (s390_tune >= PROCESSOR_2827_ZEC12
15909 && reload_completed
15910 && *nreadyp > 1)
15911 {
15912 int i;
15913 int last_index = *nreadyp - 1;
15914 int max_index = -1;
15915 int max_score = -1;
15916 rtx_insn *tmp;
15917
15918 /* Just move the insn with the highest score to the top (the
15919 end) of the list. A full sort is not needed since a conflict
15920 in the hazard recognition cannot happen. So the top insn in
15921 the ready list will always be taken. */
15922 for (i = last_index; i >= 0; i--)
15923 {
15924 int score;
15925
15926 if (recog_memoized (ready[i]) < 0)
15927 continue;
15928
15929 score = s390_sched_score (ready[i]);
15930 if (score > max_score)
15931 {
15932 max_score = score;
15933 max_index = i;
15934 }
15935 }
15936
15937 if (max_index != -1)
15938 {
15939 if (max_index != last_index)
15940 {
15941 tmp = ready[max_index];
15942 ready[max_index] = ready[last_index];
15943 ready[last_index] = tmp;
15944
15945 if (verbose > 5)
15946 fprintf (file,
15947 ";;\t\tBACKEND: move insn %d to the top of list\n",
15948 INSN_UID (ready[last_index]));
15949 }
15950 else if (verbose > 5)
15951 fprintf (file,
15952 ";;\t\tBACKEND: best insn %d already on top\n",
15953 INSN_UID (ready[last_index]));
15954 }
15955
15956 if (verbose > 5)
15957 {
15958 fprintf (file, "ready list ooo attributes - sched state: %d\n",
15959 sched_state.group_state);
15960
15961 for (i = last_index; i >= 0; i--)
15962 {
15963 unsigned int sched_mask;
15964 rtx_insn *insn = ready[i];
15965
15966 if (recog_memoized (insn) < 0)
15967 continue;
15968
15969 sched_mask = s390_get_sched_attrmask (insn);
15970 fprintf (file, ";;\t\tBACKEND: insn %d score: %d: ",
15971 INSN_UID (insn),
15972 s390_sched_score (insn));
15973 #define PRINT_SCHED_ATTR(M, ATTR) fprintf (file, "%s ",\
15974 ((M) & sched_mask) ? #ATTR : "");
15975 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_CRACKED, cracked);
15976 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_EXPANDED, expanded);
15977 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_ENDGROUP, endgroup);
15978 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_GROUPALONE, groupalone);
15979 #undef PRINT_SCHED_ATTR
15980 if (s390_tune >= PROCESSOR_2964_Z13)
15981 {
15982 unsigned int unit_mask, m = 1;
15983 int units, j;
15984
15985 unit_mask = s390_get_unit_mask (insn, &units);
15986 fprintf (file, "(units:");
15987 for (j = 0; j < units; j++, m <<= 1)
15988 if (m & unit_mask)
15989 fprintf (file, " u%d", j);
15990 fprintf (file, ")");
15991 }
15992 fprintf (file, "\n");
15993 }
15994 }
15995 }
15996
15997 return s390_issue_rate ();
15998 }
15999
16000
16001 /* This function is called via hook TARGET_SCHED_VARIABLE_ISSUE after
16002 the scheduler has issued INSN. It stores the last issued insn into
16003 last_scheduled_insn in order to make it available for
16004 s390_sched_reorder. */
16005 static int
16006 s390_sched_variable_issue (FILE *file, int verbose, rtx_insn *insn, int more)
16007 {
16008 last_scheduled_insn = insn;
16009
16010 bool ends_group = false;
16011
16012 if (s390_tune >= PROCESSOR_2827_ZEC12
16013 && reload_completed
16014 && recog_memoized (insn) >= 0)
16015 {
16016 unsigned int mask = s390_get_sched_attrmask (insn);
16017
16018 if ((mask & S390_SCHED_ATTR_MASK_GROUPOFTWO) != 0)
16019 sched_state.group_of_two = true;
16020
16021 /* If this is a group-of-two insn, we actually ended the last group
16022 and this insn is the first one of the new group. */
16023 if (sched_state.group_state == 2 && sched_state.group_of_two)
16024 {
16025 sched_state.side = sched_state.side ? 0 : 1;
16026 sched_state.group_state = 0;
16027 }
16028
16029 /* Longrunning and side bookkeeping. */
16030 for (int i = 0; i < 2; i++)
16031 {
16032 fxd_longrunning[i] = MAX (0, fxd_longrunning[i] - 1);
16033 fpd_longrunning[i] = MAX (0, fpd_longrunning[i] - 1);
16034 }
16035
16036 unsigned latency = insn_default_latency (insn);
16037 if (s390_is_longrunning (insn))
16038 {
16039 if (s390_is_fxd (insn))
16040 fxd_longrunning[sched_state.side] = latency;
16041 else
16042 fpd_longrunning[sched_state.side] = latency;
16043 }
16044
16045 if (s390_tune >= PROCESSOR_2964_Z13)
16046 {
16047 int units, i;
16048 unsigned unit_mask, m = 1;
16049
16050 unit_mask = s390_get_unit_mask (insn, &units);
16051 gcc_assert (units <= MAX_SCHED_UNITS);
16052
16053 for (i = 0; i < units; i++, m <<= 1)
16054 if (m & unit_mask)
16055 last_scheduled_unit_distance[i][sched_state.side] = 0;
16056 else if (last_scheduled_unit_distance[i][sched_state.side]
16057 < MAX_SCHED_MIX_DISTANCE)
16058 last_scheduled_unit_distance[i][sched_state.side]++;
16059 }
16060
16061 if ((mask & S390_SCHED_ATTR_MASK_CRACKED) != 0
16062 || (mask & S390_SCHED_ATTR_MASK_EXPANDED) != 0
16063 || (mask & S390_SCHED_ATTR_MASK_GROUPALONE) != 0
16064 || (mask & S390_SCHED_ATTR_MASK_ENDGROUP) != 0)
16065 {
16066 sched_state.group_state = 0;
16067 ends_group = true;
16068 }
16069 else
16070 {
16071 switch (sched_state.group_state)
16072 {
16073 case 0:
16074 sched_state.group_state++;
16075 break;
16076 case 1:
16077 sched_state.group_state++;
16078 if (sched_state.group_of_two)
16079 {
16080 sched_state.group_state = 0;
16081 ends_group = true;
16082 }
16083 break;
16084 case 2:
16085 sched_state.group_state++;
16086 ends_group = true;
16087 break;
16088 }
16089 }
16090
16091 if (verbose > 5)
16092 {
16093 unsigned int sched_mask;
16094
16095 sched_mask = s390_get_sched_attrmask (insn);
16096
16097 fprintf (file, ";;\t\tBACKEND: insn %d: ", INSN_UID (insn));
16098 #define PRINT_SCHED_ATTR(M, ATTR) fprintf (file, "%s ", ((M) & sched_mask) ? #ATTR : "");
16099 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_CRACKED, cracked);
16100 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_EXPANDED, expanded);
16101 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_ENDGROUP, endgroup);
16102 PRINT_SCHED_ATTR (S390_SCHED_ATTR_MASK_GROUPALONE, groupalone);
16103 #undef PRINT_SCHED_ATTR
16104
16105 if (s390_tune >= PROCESSOR_2964_Z13)
16106 {
16107 unsigned int unit_mask, m = 1;
16108 int units, j;
16109
16110 unit_mask = s390_get_unit_mask (insn, &units);
16111 fprintf (file, "(units:");
16112 for (j = 0; j < units; j++, m <<= 1)
16113 if (m & unit_mask)
16114 fprintf (file, " %d", j);
16115 fprintf (file, ")");
16116 }
16117 fprintf (file, " sched state: %d\n", sched_state.group_state);
16118
16119 if (s390_tune >= PROCESSOR_2964_Z13)
16120 {
16121 int units, j;
16122
16123 s390_get_unit_mask (insn, &units);
16124
16125 fprintf (file, ";;\t\tBACKEND: units on this side (%d) unused for: ", sched_state.side);
16126 for (j = 0; j < units; j++)
16127 fprintf (file, "%d:%d ", j,
16128 last_scheduled_unit_distance[j][sched_state.side]);
16129 fprintf (file, "\n");
16130 }
16131 }
16132
16133 /* If this insn ended a group, the next will be on the other side. */
16134 if (ends_group)
16135 {
16136 sched_state.group_state = 0;
16137 sched_state.side = sched_state.side ? 0 : 1;
16138 sched_state.group_of_two = false;
16139 }
16140 }
16141
16142 if (GET_CODE (PATTERN (insn)) != USE
16143 && GET_CODE (PATTERN (insn)) != CLOBBER)
16144 return more - 1;
16145 else
16146 return more;
16147 }
16148
16149 static void
16150 s390_sched_init (FILE *file ATTRIBUTE_UNUSED,
16151 int verbose ATTRIBUTE_UNUSED,
16152 int max_ready ATTRIBUTE_UNUSED)
16153 {
16154 /* If the next basic block is most likely entered via a fallthru edge
16155 we keep the last sched state. Otherwise we start a new group.
16156 The scheduler traverses basic blocks in "instruction stream" ordering
16157 so if we see a fallthru edge here, sched_state will be of its
16158 source block.
16159
16160 current_sched_info->prev_head is the insn before the first insn of the
16161 block of insns to be scheduled.
16162 */
16163 last_scheduled_insn = NULL;
16164 memset (last_scheduled_unit_distance, 0,
16165 MAX_SCHED_UNITS * NUM_SIDES * sizeof (int));
16166 memset (fpd_longrunning, 0, NUM_SIDES * sizeof (int));
16167 memset (fxd_longrunning, 0, NUM_SIDES * sizeof (int));
16168 sched_state = {};
16169 }
16170
16171 /* This target hook implementation for TARGET_LOOP_UNROLL_ADJUST calculates
16172 a new number struct loop *loop should be unrolled if tuned for cpus with
16173 a built-in stride prefetcher.
16174 The loop is analyzed for memory accesses by calling check_dpu for
16175 each rtx of the loop. Depending on the loop_depth and the amount of
16176 memory accesses a new number <=nunroll is returned to improve the
16177 behavior of the hardware prefetch unit. */
16178 static unsigned
16179 s390_loop_unroll_adjust (unsigned nunroll, struct loop *loop)
16180 {
16181 basic_block *bbs;
16182 rtx_insn *insn;
16183 unsigned i;
16184 unsigned mem_count = 0;
16185
16186 if (s390_tune < PROCESSOR_2097_Z10)
16187 return nunroll;
16188
16189 if (unroll_only_small_loops)
16190 {
16191 /* Only unroll loops smaller than or equal to 12 insns. */
16192 const unsigned int small_threshold = 12;
16193
16194 if (loop->ninsns > small_threshold)
16195 return 0;
16196
16197 /* ???: Make this dependent on the type of registers in
16198 the loop. Increase the limit for vector registers. */
16199 const unsigned int max_insns = optimize >= 3 ? 36 : 24;
16200
16201 nunroll = MIN (nunroll, max_insns / loop->ninsns);
16202 }
16203
16204 /* Count the number of memory references within the loop body. */
16205 bbs = get_loop_body (loop);
16206 subrtx_iterator::array_type array;
16207 for (i = 0; i < loop->num_nodes; i++)
16208 FOR_BB_INSNS (bbs[i], insn)
16209 if (INSN_P (insn) && INSN_CODE (insn) != -1)
16210 {
16211 rtx set;
16212
16213 /* The runtime of small loops with memory block operations
16214 will be determined by the memory operation. Doing
16215 unrolling doesn't help here. Measurements to confirm
16216 this where only done on recent CPU levels. So better do
16217 not change anything for older CPUs. */
16218 if (s390_tune >= PROCESSOR_2964_Z13
16219 && loop->ninsns <= BLOCK_MEM_OPS_LOOP_INSNS
16220 && ((set = single_set (insn)) != NULL_RTX)
16221 && ((GET_MODE (SET_DEST (set)) == BLKmode
16222 && (GET_MODE (SET_SRC (set)) == BLKmode
16223 || SET_SRC (set) == const0_rtx))
16224 || (GET_CODE (SET_SRC (set)) == COMPARE
16225 && GET_MODE (XEXP (SET_SRC (set), 0)) == BLKmode
16226 && GET_MODE (XEXP (SET_SRC (set), 1)) == BLKmode)))
16227 {
16228 free (bbs);
16229 return 1;
16230 }
16231
16232 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
16233 if (MEM_P (*iter))
16234 mem_count += 1;
16235 }
16236 free (bbs);
16237
16238 /* Prevent division by zero, and we do not need to adjust nunroll in this case. */
16239 if (mem_count == 0)
16240 return nunroll;
16241
16242 switch (loop_depth(loop))
16243 {
16244 case 1:
16245 return MIN (nunroll, 28 / mem_count);
16246 case 2:
16247 return MIN (nunroll, 22 / mem_count);
16248 default:
16249 return MIN (nunroll, 16 / mem_count);
16250 }
16251 }
16252
16253 /* Restore the current options. This is a hook function and also called
16254 internally. */
16255
16256 static void
16257 s390_function_specific_restore (struct gcc_options *opts,
16258 struct gcc_options */* opts_set */,
16259 struct cl_target_option *ptr ATTRIBUTE_UNUSED)
16260 {
16261 opts->x_s390_cost_pointer = (long)processor_table[opts->x_s390_tune].cost;
16262 }
16263
16264 static void
16265 s390_default_align (struct gcc_options *opts)
16266 {
16267 /* Set the default function alignment to 16 in order to get rid of
16268 some unwanted performance effects. */
16269 if (opts->x_flag_align_functions && !opts->x_str_align_functions
16270 && opts->x_s390_tune >= PROCESSOR_2964_Z13)
16271 opts->x_str_align_functions = "16";
16272 }
16273
16274 static void
16275 s390_override_options_after_change (void)
16276 {
16277 s390_default_align (&global_options);
16278
16279 /* Explicit -funroll-loops turns -munroll-only-small-loops off. */
16280 if ((OPTION_SET_P (flag_unroll_loops) && flag_unroll_loops)
16281 || (OPTION_SET_P (flag_unroll_all_loops)
16282 && flag_unroll_all_loops))
16283 {
16284 if (!OPTION_SET_P (unroll_only_small_loops))
16285 unroll_only_small_loops = 0;
16286 if (!OPTION_SET_P (flag_cunroll_grow_size))
16287 flag_cunroll_grow_size = 1;
16288 }
16289 else if (!OPTION_SET_P (flag_cunroll_grow_size))
16290 flag_cunroll_grow_size = flag_peel_loops || optimize >= 3;
16291 }
16292
16293 static void
16294 s390_option_override_internal (struct gcc_options *opts,
16295 struct gcc_options *opts_set)
16296 {
16297 /* Architecture mode defaults according to ABI. */
16298 if (!(opts_set->x_target_flags & MASK_ZARCH))
16299 {
16300 if (TARGET_64BIT)
16301 opts->x_target_flags |= MASK_ZARCH;
16302 else
16303 opts->x_target_flags &= ~MASK_ZARCH;
16304 }
16305
16306 /* Set the march default in case it hasn't been specified on cmdline. */
16307 if (!opts_set->x_s390_arch)
16308 opts->x_s390_arch = PROCESSOR_2064_Z900;
16309
16310 opts->x_s390_arch_flags = processor_flags_table[(int) opts->x_s390_arch];
16311
16312 /* Determine processor to tune for. */
16313 if (!opts_set->x_s390_tune)
16314 opts->x_s390_tune = opts->x_s390_arch;
16315
16316 opts->x_s390_tune_flags = processor_flags_table[opts->x_s390_tune];
16317
16318 /* Sanity checks. */
16319 if (opts->x_s390_arch == PROCESSOR_NATIVE
16320 || opts->x_s390_tune == PROCESSOR_NATIVE)
16321 gcc_unreachable ();
16322 if (TARGET_64BIT && !TARGET_ZARCH_P (opts->x_target_flags))
16323 error ("64-bit ABI not supported in ESA/390 mode");
16324
16325 if (opts->x_s390_indirect_branch == indirect_branch_thunk_inline
16326 || opts->x_s390_indirect_branch_call == indirect_branch_thunk_inline
16327 || opts->x_s390_function_return == indirect_branch_thunk_inline
16328 || opts->x_s390_function_return_reg == indirect_branch_thunk_inline
16329 || opts->x_s390_function_return_mem == indirect_branch_thunk_inline)
16330 error ("thunk-inline is only supported with %<-mindirect-branch-jump%>");
16331
16332 if (opts->x_s390_indirect_branch != indirect_branch_keep)
16333 {
16334 if (!opts_set->x_s390_indirect_branch_call)
16335 opts->x_s390_indirect_branch_call = opts->x_s390_indirect_branch;
16336
16337 if (!opts_set->x_s390_indirect_branch_jump)
16338 opts->x_s390_indirect_branch_jump = opts->x_s390_indirect_branch;
16339 }
16340
16341 if (opts->x_s390_function_return != indirect_branch_keep)
16342 {
16343 if (!opts_set->x_s390_function_return_reg)
16344 opts->x_s390_function_return_reg = opts->x_s390_function_return;
16345
16346 if (!opts_set->x_s390_function_return_mem)
16347 opts->x_s390_function_return_mem = opts->x_s390_function_return;
16348 }
16349
16350 /* Enable hardware transactions if available and not explicitly
16351 disabled by user. E.g. with -m31 -march=zEC12 -mzarch */
16352 if (!TARGET_OPT_HTM_P (opts_set->x_target_flags))
16353 {
16354 if (TARGET_CPU_HTM_P (opts) && TARGET_ZARCH_P (opts->x_target_flags))
16355 opts->x_target_flags |= MASK_OPT_HTM;
16356 else
16357 opts->x_target_flags &= ~MASK_OPT_HTM;
16358 }
16359
16360 if (TARGET_OPT_VX_P (opts_set->x_target_flags))
16361 {
16362 if (TARGET_OPT_VX_P (opts->x_target_flags))
16363 {
16364 if (!TARGET_CPU_VX_P (opts))
16365 error ("hardware vector support not available on %s",
16366 processor_table[(int)opts->x_s390_arch].name);
16367 if (TARGET_SOFT_FLOAT_P (opts->x_target_flags))
16368 error ("hardware vector support not available with "
16369 "%<-msoft-float%>");
16370 }
16371 }
16372 else
16373 {
16374 if (TARGET_CPU_VX_P (opts) && TARGET_ZARCH_P (opts->x_target_flags))
16375 /* Enable vector support if available and not explicitly disabled
16376 by user. E.g. with -m31 -march=z13 -mzarch */
16377 opts->x_target_flags |= MASK_OPT_VX;
16378 else
16379 opts->x_target_flags &= ~MASK_OPT_VX;
16380 }
16381
16382 /* Use hardware DFP if available and not explicitly disabled by
16383 user. E.g. with -m31 -march=z10 -mzarch */
16384 if (!TARGET_HARD_DFP_P (opts_set->x_target_flags))
16385 {
16386 if (TARGET_DFP_P (opts))
16387 opts->x_target_flags |= MASK_HARD_DFP;
16388 else
16389 opts->x_target_flags &= ~MASK_HARD_DFP;
16390 }
16391
16392 if (TARGET_HARD_DFP_P (opts->x_target_flags) && !TARGET_DFP_P (opts))
16393 {
16394 if (TARGET_HARD_DFP_P (opts_set->x_target_flags))
16395 {
16396 if (!TARGET_CPU_DFP_P (opts))
16397 error ("hardware decimal floating-point instructions"
16398 " not available on %s",
16399 processor_table[(int)opts->x_s390_arch].name);
16400 if (!TARGET_ZARCH_P (opts->x_target_flags))
16401 error ("hardware decimal floating-point instructions"
16402 " not available in ESA/390 mode");
16403 }
16404 else
16405 opts->x_target_flags &= ~MASK_HARD_DFP;
16406 }
16407
16408 if (TARGET_SOFT_FLOAT_P (opts_set->x_target_flags)
16409 && TARGET_SOFT_FLOAT_P (opts->x_target_flags))
16410 {
16411 if (TARGET_HARD_DFP_P (opts_set->x_target_flags)
16412 && TARGET_HARD_DFP_P (opts->x_target_flags))
16413 error ("%<-mhard-dfp%> cannot be used in conjunction with "
16414 "%<-msoft-float%>");
16415
16416 opts->x_target_flags &= ~MASK_HARD_DFP;
16417 }
16418
16419 if (TARGET_BACKCHAIN_P (opts->x_target_flags)
16420 && TARGET_PACKED_STACK_P (opts->x_target_flags)
16421 && TARGET_HARD_FLOAT_P (opts->x_target_flags))
16422 error ("%<-mbackchain%> %<-mpacked-stack%> %<-mhard-float%> are not "
16423 "supported in combination");
16424
16425 if (opts->x_s390_stack_size)
16426 {
16427 if (opts->x_s390_stack_guard >= opts->x_s390_stack_size)
16428 error ("stack size must be greater than the stack guard value");
16429 else if (opts->x_s390_stack_size > 1 << 16)
16430 error ("stack size must not be greater than 64k");
16431 }
16432 else if (opts->x_s390_stack_guard)
16433 error ("%<-mstack-guard%> implies use of %<-mstack-size%>");
16434
16435 /* Our implementation of the stack probe requires the probe interval
16436 to be used as displacement in an address operand. The maximum
16437 probe interval currently is 64k. This would exceed short
16438 displacements. Trim that value down to 4k if that happens. This
16439 might result in too many probes being generated only on the
16440 oldest supported machine level z900. */
16441 if (!DISP_IN_RANGE ((1 << param_stack_clash_protection_probe_interval)))
16442 param_stack_clash_protection_probe_interval = 12;
16443
16444 #if TARGET_TPF != 0
16445 if (!CONST_OK_FOR_J (opts->x_s390_tpf_trace_hook_prologue_check))
16446 error ("%<-mtpf-trace-hook-prologue-check%> requires integer in range 0-4095");
16447
16448 if (!CONST_OK_FOR_J (opts->x_s390_tpf_trace_hook_prologue_target))
16449 error ("%<-mtpf-trace-hook-prologue-target%> requires integer in range 0-4095");
16450
16451 if (!CONST_OK_FOR_J (opts->x_s390_tpf_trace_hook_epilogue_check))
16452 error ("%<-mtpf-trace-hook-epilogue-check%> requires integer in range 0-4095");
16453
16454 if (!CONST_OK_FOR_J (opts->x_s390_tpf_trace_hook_epilogue_target))
16455 error ("%<-mtpf-trace-hook-epilogue-target%> requires integer in range 0-4095");
16456
16457 if (s390_tpf_trace_skip)
16458 {
16459 opts->x_s390_tpf_trace_hook_prologue_target = TPF_TRACE_PROLOGUE_SKIP_TARGET;
16460 opts->x_s390_tpf_trace_hook_epilogue_target = TPF_TRACE_EPILOGUE_SKIP_TARGET;
16461 }
16462 #endif
16463
16464 #ifdef TARGET_DEFAULT_LONG_DOUBLE_128
16465 if (!TARGET_LONG_DOUBLE_128_P (opts_set->x_target_flags))
16466 opts->x_target_flags |= MASK_LONG_DOUBLE_128;
16467 #endif
16468
16469 if (opts->x_s390_tune >= PROCESSOR_2097_Z10)
16470 {
16471 SET_OPTION_IF_UNSET (opts, opts_set, param_max_unrolled_insns,
16472 100);
16473 SET_OPTION_IF_UNSET (opts, opts_set, param_max_unroll_times, 32);
16474 SET_OPTION_IF_UNSET (opts, opts_set, param_max_completely_peeled_insns,
16475 2000);
16476 SET_OPTION_IF_UNSET (opts, opts_set, param_max_completely_peel_times,
16477 64);
16478 }
16479
16480 SET_OPTION_IF_UNSET (opts, opts_set, param_max_pending_list_length,
16481 256);
16482 /* values for loop prefetching */
16483 SET_OPTION_IF_UNSET (opts, opts_set, param_l1_cache_line_size, 256);
16484 SET_OPTION_IF_UNSET (opts, opts_set, param_l1_cache_size, 128);
16485 /* s390 has more than 2 levels and the size is much larger. Since
16486 we are always running virtualized assume that we only get a small
16487 part of the caches above l1. */
16488 SET_OPTION_IF_UNSET (opts, opts_set, param_l2_cache_size, 1500);
16489 SET_OPTION_IF_UNSET (opts, opts_set,
16490 param_prefetch_min_insn_to_mem_ratio, 2);
16491 SET_OPTION_IF_UNSET (opts, opts_set, param_simultaneous_prefetches, 6);
16492
16493 /* Use the alternative scheduling-pressure algorithm by default. */
16494 SET_OPTION_IF_UNSET (opts, opts_set, param_sched_pressure_algorithm, 2);
16495
16496 /* Allow simple vector masking using vll/vstl for epilogues. */
16497 if (TARGET_Z13)
16498 SET_OPTION_IF_UNSET (opts, opts_set, param_vect_partial_vector_usage, 1);
16499 else
16500 SET_OPTION_IF_UNSET (opts, opts_set, param_vect_partial_vector_usage, 0);
16501
16502 /* Do not vectorize loops with a low trip count for now. */
16503 SET_OPTION_IF_UNSET (opts, opts_set, param_min_vect_loop_bound, 2);
16504
16505 /* Set the default alignment. */
16506 s390_default_align (opts);
16507
16508 /* Set unroll options. */
16509 s390_override_options_after_change ();
16510
16511 /* Call target specific restore function to do post-init work. At the moment,
16512 this just sets opts->x_s390_cost_pointer. */
16513 s390_function_specific_restore (opts, opts_set, NULL);
16514
16515 /* Check whether -mfentry is supported. It cannot be used in 31-bit mode,
16516 because 31-bit PLT stubs assume that %r12 contains GOT address, which is
16517 not the case when the code runs before the prolog. */
16518 if (opts->x_flag_fentry && !TARGET_64BIT)
16519 error ("%<-mfentry%> is supported only for 64-bit CPUs");
16520 }
16521
16522 static void
16523 s390_option_override (void)
16524 {
16525 unsigned int i;
16526 cl_deferred_option *opt;
16527 vec<cl_deferred_option> *v =
16528 (vec<cl_deferred_option> *) s390_deferred_options;
16529
16530 if (v)
16531 FOR_EACH_VEC_ELT (*v, i, opt)
16532 {
16533 switch (opt->opt_index)
16534 {
16535 case OPT_mhotpatch_:
16536 {
16537 int val1;
16538 int val2;
16539 char *s = strtok (ASTRDUP (opt->arg), ",");
16540 char *t = strtok (NULL, "\0");
16541
16542 if (t != NULL)
16543 {
16544 val1 = integral_argument (s);
16545 val2 = integral_argument (t);
16546 }
16547 else
16548 {
16549 val1 = -1;
16550 val2 = -1;
16551 }
16552 if (val1 == -1 || val2 == -1)
16553 {
16554 /* argument is not a plain number */
16555 error ("arguments to %qs should be non-negative integers",
16556 "-mhotpatch=n,m");
16557 break;
16558 }
16559 else if (val1 > s390_hotpatch_hw_max
16560 || val2 > s390_hotpatch_hw_max)
16561 {
16562 error ("argument to %qs is too large (max. %d)",
16563 "-mhotpatch=n,m", s390_hotpatch_hw_max);
16564 break;
16565 }
16566 s390_hotpatch_hw_before_label = val1;
16567 s390_hotpatch_hw_after_label = val2;
16568 break;
16569 }
16570 default:
16571 gcc_unreachable ();
16572 }
16573 }
16574
16575 /* Set up function hooks. */
16576 init_machine_status = s390_init_machine_status;
16577
16578 s390_option_override_internal (&global_options, &global_options_set);
16579
16580 /* Save the initial options in case the user does function specific
16581 options. */
16582 target_option_default_node
16583 = build_target_option_node (&global_options, &global_options_set);
16584 target_option_current_node = target_option_default_node;
16585
16586 /* This cannot reside in s390_option_optimization_table since HAVE_prefetch
16587 requires the arch flags to be evaluated already. Since prefetching
16588 is beneficial on s390, we enable it if available. */
16589 if (flag_prefetch_loop_arrays < 0 && HAVE_prefetch && optimize >= 3)
16590 flag_prefetch_loop_arrays = 1;
16591
16592 if (!s390_pic_data_is_text_relative && !flag_pic)
16593 error ("%<-mno-pic-data-is-text-relative%> cannot be used without "
16594 "%<-fpic%>/%<-fPIC%>");
16595
16596 if (TARGET_TPF)
16597 {
16598 /* Don't emit DWARF3/4 unless specifically selected. The TPF
16599 debuggers do not yet support DWARF 3/4. */
16600 if (!OPTION_SET_P (dwarf_strict))
16601 dwarf_strict = 1;
16602 if (!OPTION_SET_P (dwarf_version))
16603 dwarf_version = 2;
16604 }
16605 }
16606
16607 #if S390_USE_TARGET_ATTRIBUTE
16608 /* Inner function to process the attribute((target(...))), take an argument and
16609 set the current options from the argument. If we have a list, recursively go
16610 over the list. */
16611
16612 static bool
16613 s390_valid_target_attribute_inner_p (tree args,
16614 struct gcc_options *opts,
16615 struct gcc_options *new_opts_set,
16616 bool force_pragma)
16617 {
16618 char *next_optstr;
16619 bool ret = true;
16620
16621 #define S390_ATTRIB(S,O,A) { S, sizeof (S)-1, O, A, 0 }
16622 #define S390_PRAGMA(S,O,A) { S, sizeof (S)-1, O, A, 1 }
16623 static const struct
16624 {
16625 const char *string;
16626 size_t len;
16627 int opt;
16628 int has_arg;
16629 int only_as_pragma;
16630 } attrs[] = {
16631 /* enum options */
16632 S390_ATTRIB ("arch=", OPT_march_, 1),
16633 S390_ATTRIB ("tune=", OPT_mtune_, 1),
16634 /* uinteger options */
16635 S390_ATTRIB ("stack-guard=", OPT_mstack_guard_, 1),
16636 S390_ATTRIB ("stack-size=", OPT_mstack_size_, 1),
16637 S390_ATTRIB ("branch-cost=", OPT_mbranch_cost_, 1),
16638 S390_ATTRIB ("warn-framesize=", OPT_mwarn_framesize_, 1),
16639 /* flag options */
16640 S390_ATTRIB ("backchain", OPT_mbackchain, 0),
16641 S390_ATTRIB ("hard-dfp", OPT_mhard_dfp, 0),
16642 S390_ATTRIB ("hard-float", OPT_mhard_float, 0),
16643 S390_ATTRIB ("htm", OPT_mhtm, 0),
16644 S390_ATTRIB ("vx", OPT_mvx, 0),
16645 S390_ATTRIB ("packed-stack", OPT_mpacked_stack, 0),
16646 S390_ATTRIB ("small-exec", OPT_msmall_exec, 0),
16647 S390_ATTRIB ("soft-float", OPT_msoft_float, 0),
16648 S390_ATTRIB ("mvcle", OPT_mmvcle, 0),
16649 S390_PRAGMA ("zvector", OPT_mzvector, 0),
16650 /* boolean options */
16651 S390_ATTRIB ("warn-dynamicstack", OPT_mwarn_dynamicstack, 0),
16652 };
16653 #undef S390_ATTRIB
16654 #undef S390_PRAGMA
16655
16656 /* If this is a list, recurse to get the options. */
16657 if (TREE_CODE (args) == TREE_LIST)
16658 {
16659 bool ret = true;
16660 int num_pragma_values;
16661 int i;
16662
16663 /* Note: attribs.cc:decl_attributes prepends the values from
16664 current_target_pragma to the list of target attributes. To determine
16665 whether we're looking at a value of the attribute or the pragma we
16666 assume that the first [list_length (current_target_pragma)] values in
16667 the list are the values from the pragma. */
16668 num_pragma_values = (!force_pragma && current_target_pragma != NULL)
16669 ? list_length (current_target_pragma) : 0;
16670 for (i = 0; args; args = TREE_CHAIN (args), i++)
16671 {
16672 bool is_pragma;
16673
16674 is_pragma = (force_pragma || i < num_pragma_values);
16675 if (TREE_VALUE (args)
16676 && !s390_valid_target_attribute_inner_p (TREE_VALUE (args),
16677 opts, new_opts_set,
16678 is_pragma))
16679 {
16680 ret = false;
16681 }
16682 }
16683 return ret;
16684 }
16685
16686 else if (TREE_CODE (args) != STRING_CST)
16687 {
16688 error ("attribute %<target%> argument not a string");
16689 return false;
16690 }
16691
16692 /* Handle multiple arguments separated by commas. */
16693 next_optstr = ASTRDUP (TREE_STRING_POINTER (args));
16694
16695 while (next_optstr && *next_optstr != '\0')
16696 {
16697 char *p = next_optstr;
16698 char *orig_p = p;
16699 char *comma = strchr (next_optstr, ',');
16700 size_t len, opt_len;
16701 int opt;
16702 bool opt_set_p;
16703 char ch;
16704 unsigned i;
16705 int mask = 0;
16706 enum cl_var_type var_type;
16707 bool found;
16708
16709 if (comma)
16710 {
16711 *comma = '\0';
16712 len = comma - next_optstr;
16713 next_optstr = comma + 1;
16714 }
16715 else
16716 {
16717 len = strlen (p);
16718 next_optstr = NULL;
16719 }
16720
16721 /* Recognize no-xxx. */
16722 if (len > 3 && p[0] == 'n' && p[1] == 'o' && p[2] == '-')
16723 {
16724 opt_set_p = false;
16725 p += 3;
16726 len -= 3;
16727 }
16728 else
16729 opt_set_p = true;
16730
16731 /* Find the option. */
16732 ch = *p;
16733 found = false;
16734 for (i = 0; i < ARRAY_SIZE (attrs); i++)
16735 {
16736 opt_len = attrs[i].len;
16737 if (ch == attrs[i].string[0]
16738 && ((attrs[i].has_arg) ? len > opt_len : len == opt_len)
16739 && memcmp (p, attrs[i].string, opt_len) == 0)
16740 {
16741 opt = attrs[i].opt;
16742 if (!opt_set_p && cl_options[opt].cl_reject_negative)
16743 continue;
16744 mask = cl_options[opt].var_value;
16745 var_type = cl_options[opt].var_type;
16746 found = true;
16747 break;
16748 }
16749 }
16750
16751 /* Process the option. */
16752 if (!found)
16753 {
16754 error ("attribute %<target%> argument %qs is unknown", orig_p);
16755 return false;
16756 }
16757 else if (attrs[i].only_as_pragma && !force_pragma)
16758 {
16759 /* Value is not allowed for the target attribute. */
16760 error ("value %qs is not supported by attribute %<target%>",
16761 attrs[i].string);
16762 return false;
16763 }
16764
16765 else if (var_type == CLVC_BIT_SET || var_type == CLVC_BIT_CLEAR)
16766 {
16767 if (var_type == CLVC_BIT_CLEAR)
16768 opt_set_p = !opt_set_p;
16769
16770 if (opt_set_p)
16771 opts->x_target_flags |= mask;
16772 else
16773 opts->x_target_flags &= ~mask;
16774 new_opts_set->x_target_flags |= mask;
16775 }
16776
16777 else if (cl_options[opt].var_type == CLVC_INTEGER)
16778 {
16779 int value;
16780
16781 if (cl_options[opt].cl_uinteger)
16782 {
16783 /* Unsigned integer argument. Code based on the function
16784 decode_cmdline_option () in opts-common.cc. */
16785 value = integral_argument (p + opt_len);
16786 }
16787 else
16788 value = (opt_set_p) ? 1 : 0;
16789
16790 if (value != -1)
16791 {
16792 struct cl_decoded_option decoded;
16793
16794 /* Value range check; only implemented for numeric and boolean
16795 options at the moment. */
16796 generate_option (opt, NULL, value, CL_TARGET, &decoded);
16797 s390_handle_option (opts, new_opts_set, &decoded, input_location);
16798 set_option (opts, new_opts_set, opt, value,
16799 p + opt_len, DK_UNSPECIFIED, input_location,
16800 global_dc);
16801 }
16802 else
16803 {
16804 error ("attribute %<target%> argument %qs is unknown", orig_p);
16805 ret = false;
16806 }
16807 }
16808
16809 else if (cl_options[opt].var_type == CLVC_ENUM)
16810 {
16811 bool arg_ok;
16812 int value;
16813
16814 arg_ok = opt_enum_arg_to_value (opt, p + opt_len, &value, CL_TARGET);
16815 if (arg_ok)
16816 set_option (opts, new_opts_set, opt, value,
16817 p + opt_len, DK_UNSPECIFIED, input_location,
16818 global_dc);
16819 else
16820 {
16821 error ("attribute %<target%> argument %qs is unknown", orig_p);
16822 ret = false;
16823 }
16824 }
16825
16826 else
16827 gcc_unreachable ();
16828 }
16829 return ret;
16830 }
16831
16832 /* Return a TARGET_OPTION_NODE tree of the target options listed or NULL. */
16833
16834 tree
16835 s390_valid_target_attribute_tree (tree args,
16836 struct gcc_options *opts,
16837 const struct gcc_options *opts_set,
16838 bool force_pragma)
16839 {
16840 tree t = NULL_TREE;
16841 struct gcc_options new_opts_set;
16842
16843 memset (&new_opts_set, 0, sizeof (new_opts_set));
16844
16845 /* Process each of the options on the chain. */
16846 if (! s390_valid_target_attribute_inner_p (args, opts, &new_opts_set,
16847 force_pragma))
16848 return error_mark_node;
16849
16850 /* If some option was set (even if it has not changed), rerun
16851 s390_option_override_internal, and then save the options away. */
16852 if (new_opts_set.x_target_flags
16853 || new_opts_set.x_s390_arch
16854 || new_opts_set.x_s390_tune
16855 || new_opts_set.x_s390_stack_guard
16856 || new_opts_set.x_s390_stack_size
16857 || new_opts_set.x_s390_branch_cost
16858 || new_opts_set.x_s390_warn_framesize
16859 || new_opts_set.x_s390_warn_dynamicstack_p)
16860 {
16861 const unsigned char *src = (const unsigned char *)opts_set;
16862 unsigned char *dest = (unsigned char *)&new_opts_set;
16863 unsigned int i;
16864
16865 /* Merge the original option flags into the new ones. */
16866 for (i = 0; i < sizeof(*opts_set); i++)
16867 dest[i] |= src[i];
16868
16869 /* Do any overrides, such as arch=xxx, or tune=xxx support. */
16870 s390_option_override_internal (opts, &new_opts_set);
16871 /* Save the current options unless we are validating options for
16872 #pragma. */
16873 t = build_target_option_node (opts, &new_opts_set);
16874 }
16875 return t;
16876 }
16877
16878 /* Hook to validate attribute((target("string"))). */
16879
16880 static bool
16881 s390_valid_target_attribute_p (tree fndecl,
16882 tree ARG_UNUSED (name),
16883 tree args,
16884 int ARG_UNUSED (flags))
16885 {
16886 struct gcc_options func_options, func_options_set;
16887 tree new_target, new_optimize;
16888 bool ret = true;
16889
16890 /* attribute((target("default"))) does nothing, beyond
16891 affecting multi-versioning. */
16892 if (TREE_VALUE (args)
16893 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
16894 && TREE_CHAIN (args) == NULL_TREE
16895 && strcmp (TREE_STRING_POINTER (TREE_VALUE (args)), "default") == 0)
16896 return true;
16897
16898 tree old_optimize
16899 = build_optimization_node (&global_options, &global_options_set);
16900
16901 /* Get the optimization options of the current function. */
16902 tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
16903
16904 if (!func_optimize)
16905 func_optimize = old_optimize;
16906
16907 /* Init func_options. */
16908 memset (&func_options, 0, sizeof (func_options));
16909 init_options_struct (&func_options, NULL);
16910 lang_hooks.init_options_struct (&func_options);
16911 memset (&func_options_set, 0, sizeof (func_options_set));
16912
16913 cl_optimization_restore (&func_options, &func_options_set,
16914 TREE_OPTIMIZATION (func_optimize));
16915
16916 /* Initialize func_options to the default before its target options can
16917 be set. */
16918 cl_target_option_restore (&func_options, &func_options_set,
16919 TREE_TARGET_OPTION (target_option_default_node));
16920
16921 new_target = s390_valid_target_attribute_tree (args, &func_options,
16922 &global_options_set,
16923 (args ==
16924 current_target_pragma));
16925 new_optimize = build_optimization_node (&func_options, &func_options_set);
16926 if (new_target == error_mark_node)
16927 ret = false;
16928 else if (fndecl && new_target)
16929 {
16930 DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
16931 if (old_optimize != new_optimize)
16932 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
16933 }
16934 return ret;
16935 }
16936
16937 /* Hook to determine if one function can safely inline another. */
16938
16939 static bool
16940 s390_can_inline_p (tree caller, tree callee)
16941 {
16942 /* Flags which if present in the callee are required in the caller as well. */
16943 const unsigned HOST_WIDE_INT caller_required_masks = MASK_OPT_HTM;
16944
16945 /* Flags which affect the ABI and in general prevent inlining. */
16946 unsigned HOST_WIDE_INT must_match_masks
16947 = (MASK_64BIT | MASK_ZARCH | MASK_HARD_DFP | MASK_SOFT_FLOAT
16948 | MASK_LONG_DOUBLE_128 | MASK_OPT_VX);
16949
16950 /* Flags which we in general want to prevent inlining but accept for
16951 always_inline. */
16952 const unsigned HOST_WIDE_INT always_inline_safe_masks
16953 = MASK_MVCLE | MASK_BACKCHAIN | MASK_SMALL_EXEC;
16954
16955 const HOST_WIDE_INT all_masks
16956 = (caller_required_masks | must_match_masks | always_inline_safe_masks
16957 | MASK_DEBUG_ARG | MASK_PACKED_STACK | MASK_ZVECTOR);
16958
16959 tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
16960 tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
16961
16962 if (!callee_tree)
16963 callee_tree = target_option_default_node;
16964 if (!caller_tree)
16965 caller_tree = target_option_default_node;
16966 if (callee_tree == caller_tree)
16967 return true;
16968
16969 struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
16970 struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
16971
16972 /* If one of these triggers make sure to add proper handling of your
16973 new flag to this hook. */
16974 gcc_assert (!(caller_opts->x_target_flags & ~all_masks));
16975 gcc_assert (!(callee_opts->x_target_flags & ~all_masks));
16976
16977 bool always_inline
16978 = (DECL_DISREGARD_INLINE_LIMITS (callee)
16979 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (callee)));
16980
16981 if (!always_inline)
16982 must_match_masks |= always_inline_safe_masks;
16983
16984 /* Inlining a hard float function into a soft float function is only
16985 allowed if the hard float function doesn't actually make use of
16986 floating point.
16987
16988 We are called from FEs for multi-versioning call optimization, so
16989 beware of ipa_fn_summaries not available. */
16990 if (always_inline && ipa_fn_summaries
16991 && !ipa_fn_summaries->get(cgraph_node::get (callee))->fp_expressions)
16992 must_match_masks &= ~(MASK_HARD_DFP | MASK_SOFT_FLOAT);
16993
16994 if ((caller_opts->x_target_flags & must_match_masks)
16995 != (callee_opts->x_target_flags & must_match_masks))
16996 return false;
16997
16998 if (~(caller_opts->x_target_flags & caller_required_masks)
16999 & (callee_opts->x_target_flags & caller_required_masks))
17000 return false;
17001
17002 /* Don't inline functions to be compiled for a more recent arch into a
17003 function for an older arch. */
17004 if (caller_opts->x_s390_arch < callee_opts->x_s390_arch)
17005 return false;
17006
17007 if (!always_inline && caller_opts->x_s390_tune != callee_opts->x_s390_tune)
17008 return false;
17009
17010 return true;
17011 }
17012 #endif
17013
17014 /* Set VAL to correct enum value according to the indirect-branch or
17015 function-return attribute in ATTR. */
17016
17017 static inline void
17018 s390_indirect_branch_attrvalue (tree attr, enum indirect_branch *val)
17019 {
17020 const char *str = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
17021 if (strcmp (str, "keep") == 0)
17022 *val = indirect_branch_keep;
17023 else if (strcmp (str, "thunk") == 0)
17024 *val = indirect_branch_thunk;
17025 else if (strcmp (str, "thunk-inline") == 0)
17026 *val = indirect_branch_thunk_inline;
17027 else if (strcmp (str, "thunk-extern") == 0)
17028 *val = indirect_branch_thunk_extern;
17029 }
17030
17031 /* Memorize the setting for -mindirect-branch* and -mfunction-return*
17032 from either the cmdline or the function attributes in
17033 cfun->machine. */
17034
17035 static void
17036 s390_indirect_branch_settings (tree fndecl)
17037 {
17038 tree attr;
17039
17040 if (!fndecl)
17041 return;
17042
17043 /* Initialize with the cmdline options and let the attributes
17044 override it. */
17045 cfun->machine->indirect_branch_jump = s390_indirect_branch_jump;
17046 cfun->machine->indirect_branch_call = s390_indirect_branch_call;
17047
17048 cfun->machine->function_return_reg = s390_function_return_reg;
17049 cfun->machine->function_return_mem = s390_function_return_mem;
17050
17051 if ((attr = lookup_attribute ("indirect_branch",
17052 DECL_ATTRIBUTES (fndecl))))
17053 {
17054 s390_indirect_branch_attrvalue (attr,
17055 &cfun->machine->indirect_branch_jump);
17056 s390_indirect_branch_attrvalue (attr,
17057 &cfun->machine->indirect_branch_call);
17058 }
17059
17060 if ((attr = lookup_attribute ("indirect_branch_jump",
17061 DECL_ATTRIBUTES (fndecl))))
17062 s390_indirect_branch_attrvalue (attr, &cfun->machine->indirect_branch_jump);
17063
17064 if ((attr = lookup_attribute ("indirect_branch_call",
17065 DECL_ATTRIBUTES (fndecl))))
17066 s390_indirect_branch_attrvalue (attr, &cfun->machine->indirect_branch_call);
17067
17068 if ((attr = lookup_attribute ("function_return",
17069 DECL_ATTRIBUTES (fndecl))))
17070 {
17071 s390_indirect_branch_attrvalue (attr,
17072 &cfun->machine->function_return_reg);
17073 s390_indirect_branch_attrvalue (attr,
17074 &cfun->machine->function_return_mem);
17075 }
17076
17077 if ((attr = lookup_attribute ("function_return_reg",
17078 DECL_ATTRIBUTES (fndecl))))
17079 s390_indirect_branch_attrvalue (attr, &cfun->machine->function_return_reg);
17080
17081 if ((attr = lookup_attribute ("function_return_mem",
17082 DECL_ATTRIBUTES (fndecl))))
17083 s390_indirect_branch_attrvalue (attr, &cfun->machine->function_return_mem);
17084 }
17085
17086 #if S390_USE_TARGET_ATTRIBUTE
17087 /* Restore targets globals from NEW_TREE and invalidate s390_previous_fndecl
17088 cache. */
17089
17090 void
17091 s390_activate_target_options (tree new_tree)
17092 {
17093 cl_target_option_restore (&global_options, &global_options_set,
17094 TREE_TARGET_OPTION (new_tree));
17095 if (TREE_TARGET_GLOBALS (new_tree))
17096 restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
17097 else if (new_tree == target_option_default_node)
17098 restore_target_globals (&default_target_globals);
17099 else
17100 TREE_TARGET_GLOBALS (new_tree) = save_target_globals_default_opts ();
17101 s390_previous_fndecl = NULL_TREE;
17102 }
17103 #endif
17104
17105 /* Establish appropriate back-end context for processing the function
17106 FNDECL. The argument might be NULL to indicate processing at top
17107 level, outside of any function scope. */
17108 static void
17109 s390_set_current_function (tree fndecl)
17110 {
17111 #if S390_USE_TARGET_ATTRIBUTE
17112 /* Only change the context if the function changes. This hook is called
17113 several times in the course of compiling a function, and we don't want to
17114 slow things down too much or call target_reinit when it isn't safe. */
17115 if (fndecl == s390_previous_fndecl)
17116 {
17117 s390_indirect_branch_settings (fndecl);
17118 return;
17119 }
17120
17121 tree old_tree;
17122 if (s390_previous_fndecl == NULL_TREE)
17123 old_tree = target_option_current_node;
17124 else if (DECL_FUNCTION_SPECIFIC_TARGET (s390_previous_fndecl))
17125 old_tree = DECL_FUNCTION_SPECIFIC_TARGET (s390_previous_fndecl);
17126 else
17127 old_tree = target_option_default_node;
17128
17129 if (fndecl == NULL_TREE)
17130 {
17131 if (old_tree != target_option_current_node)
17132 s390_activate_target_options (target_option_current_node);
17133 return;
17134 }
17135
17136 tree new_tree = DECL_FUNCTION_SPECIFIC_TARGET (fndecl);
17137 if (new_tree == NULL_TREE)
17138 new_tree = target_option_default_node;
17139
17140 if (old_tree != new_tree)
17141 s390_activate_target_options (new_tree);
17142 s390_previous_fndecl = fndecl;
17143 #endif
17144 s390_indirect_branch_settings (fndecl);
17145 }
17146
17147 /* Implement TARGET_USE_BY_PIECES_INFRASTRUCTURE_P. */
17148
17149 static bool
17150 s390_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size,
17151 unsigned int align ATTRIBUTE_UNUSED,
17152 enum by_pieces_operation op ATTRIBUTE_UNUSED,
17153 bool speed_p ATTRIBUTE_UNUSED)
17154 {
17155 return (size == 1 || size == 2
17156 || size == 4 || (TARGET_ZARCH && size == 8));
17157 }
17158
17159 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV hook. */
17160
17161 static void
17162 s390_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
17163 {
17164 tree sfpc = s390_builtin_decls[S390_BUILTIN_s390_sfpc];
17165 tree efpc = s390_builtin_decls[S390_BUILTIN_s390_efpc];
17166 tree call_efpc = build_call_expr (efpc, 0);
17167 tree fenv_var = create_tmp_var_raw (unsigned_type_node);
17168
17169 #define FPC_EXCEPTION_MASK HOST_WIDE_INT_UC (0xf8000000)
17170 #define FPC_FLAGS_MASK HOST_WIDE_INT_UC (0x00f80000)
17171 #define FPC_DXC_MASK HOST_WIDE_INT_UC (0x0000ff00)
17172 #define FPC_EXCEPTION_MASK_SHIFT HOST_WIDE_INT_UC (24)
17173 #define FPC_FLAGS_SHIFT HOST_WIDE_INT_UC (16)
17174 #define FPC_DXC_SHIFT HOST_WIDE_INT_UC (8)
17175
17176 /* Generates the equivalent of feholdexcept (&fenv_var)
17177
17178 fenv_var = __builtin_s390_efpc ();
17179 __builtin_s390_sfpc (fenv_var & mask) */
17180 tree old_fpc = build4 (TARGET_EXPR, unsigned_type_node, fenv_var, call_efpc,
17181 NULL_TREE, NULL_TREE);
17182 tree new_fpc
17183 = build2 (BIT_AND_EXPR, unsigned_type_node, fenv_var,
17184 build_int_cst (unsigned_type_node,
17185 ~(FPC_DXC_MASK | FPC_FLAGS_MASK
17186 | FPC_EXCEPTION_MASK)));
17187 tree set_new_fpc = build_call_expr (sfpc, 1, new_fpc);
17188 *hold = build2 (COMPOUND_EXPR, void_type_node, old_fpc, set_new_fpc);
17189
17190 /* Generates the equivalent of feclearexcept (FE_ALL_EXCEPT)
17191
17192 __builtin_s390_sfpc (__builtin_s390_efpc () & mask) */
17193 new_fpc = build2 (BIT_AND_EXPR, unsigned_type_node, call_efpc,
17194 build_int_cst (unsigned_type_node,
17195 ~(FPC_DXC_MASK | FPC_FLAGS_MASK)));
17196 *clear = build_call_expr (sfpc, 1, new_fpc);
17197
17198 /* Generates the equivalent of feupdateenv (fenv_var)
17199
17200 old_fpc = __builtin_s390_efpc ();
17201 __builtin_s390_sfpc (fenv_var);
17202 __atomic_feraiseexcept ((old_fpc & FPC_FLAGS_MASK) >> FPC_FLAGS_SHIFT); */
17203
17204 old_fpc = create_tmp_var_raw (unsigned_type_node);
17205 tree store_old_fpc = build4 (TARGET_EXPR, void_type_node, old_fpc, call_efpc,
17206 NULL_TREE, NULL_TREE);
17207
17208 set_new_fpc = build_call_expr (sfpc, 1, fenv_var);
17209
17210 tree raise_old_except = build2 (BIT_AND_EXPR, unsigned_type_node, old_fpc,
17211 build_int_cst (unsigned_type_node,
17212 FPC_FLAGS_MASK));
17213 raise_old_except = build2 (RSHIFT_EXPR, unsigned_type_node, raise_old_except,
17214 build_int_cst (unsigned_type_node,
17215 FPC_FLAGS_SHIFT));
17216 tree atomic_feraiseexcept
17217 = builtin_decl_implicit (BUILT_IN_ATOMIC_FERAISEEXCEPT);
17218 raise_old_except = build_call_expr (atomic_feraiseexcept,
17219 1, raise_old_except);
17220
17221 *update = build2 (COMPOUND_EXPR, void_type_node,
17222 build2 (COMPOUND_EXPR, void_type_node,
17223 store_old_fpc, set_new_fpc),
17224 raise_old_except);
17225
17226 #undef FPC_EXCEPTION_MASK
17227 #undef FPC_FLAGS_MASK
17228 #undef FPC_DXC_MASK
17229 #undef FPC_EXCEPTION_MASK_SHIFT
17230 #undef FPC_FLAGS_SHIFT
17231 #undef FPC_DXC_SHIFT
17232 }
17233
17234 /* Return the vector mode to be used for inner mode MODE when doing
17235 vectorization. */
17236 static machine_mode
17237 s390_preferred_simd_mode (scalar_mode mode)
17238 {
17239 if (TARGET_VXE)
17240 switch (mode)
17241 {
17242 case E_SFmode:
17243 return V4SFmode;
17244 default:;
17245 }
17246
17247 if (TARGET_VX)
17248 switch (mode)
17249 {
17250 case E_DFmode:
17251 return V2DFmode;
17252 case E_DImode:
17253 return V2DImode;
17254 case E_SImode:
17255 return V4SImode;
17256 case E_HImode:
17257 return V8HImode;
17258 case E_QImode:
17259 return V16QImode;
17260 default:;
17261 }
17262 return word_mode;
17263 }
17264
17265 /* Our hardware does not require vectors to be strictly aligned. */
17266 static bool
17267 s390_support_vector_misalignment (machine_mode mode ATTRIBUTE_UNUSED,
17268 const_tree type ATTRIBUTE_UNUSED,
17269 int misalignment ATTRIBUTE_UNUSED,
17270 bool is_packed ATTRIBUTE_UNUSED)
17271 {
17272 if (TARGET_VX)
17273 return true;
17274
17275 return default_builtin_support_vector_misalignment (mode, type, misalignment,
17276 is_packed);
17277 }
17278
17279 /* The vector ABI requires vector types to be aligned on an 8 byte
17280 boundary (our stack alignment). However, we allow this to be
17281 overriden by the user, while this definitely breaks the ABI. */
17282 static HOST_WIDE_INT
17283 s390_vector_alignment (const_tree type)
17284 {
17285 tree size = TYPE_SIZE (type);
17286
17287 if (!TARGET_VX_ABI)
17288 return default_vector_alignment (type);
17289
17290 if (TYPE_USER_ALIGN (type))
17291 return TYPE_ALIGN (type);
17292
17293 if (tree_fits_uhwi_p (size)
17294 && tree_to_uhwi (size) < BIGGEST_ALIGNMENT)
17295 return tree_to_uhwi (size);
17296
17297 return BIGGEST_ALIGNMENT;
17298 }
17299
17300 /* Implement TARGET_CONSTANT_ALIGNMENT. Alignment on even addresses for
17301 LARL instruction. */
17302
17303 static HOST_WIDE_INT
17304 s390_constant_alignment (const_tree, HOST_WIDE_INT align)
17305 {
17306 return MAX (align, 16);
17307 }
17308
17309 #ifdef HAVE_AS_MACHINE_MACHINEMODE
17310 /* Implement TARGET_ASM_FILE_START. */
17311 static void
17312 s390_asm_file_start (void)
17313 {
17314 default_file_start ();
17315 s390_asm_output_machine_for_arch (asm_out_file);
17316 }
17317 #endif
17318
17319 /* Implement TARGET_ASM_FILE_END. */
17320 static void
17321 s390_asm_file_end (void)
17322 {
17323 #ifdef HAVE_AS_GNU_ATTRIBUTE
17324 varpool_node *vnode;
17325 cgraph_node *cnode;
17326
17327 FOR_EACH_VARIABLE (vnode)
17328 if (TREE_PUBLIC (vnode->decl))
17329 s390_check_type_for_vector_abi (TREE_TYPE (vnode->decl), false, false);
17330
17331 FOR_EACH_FUNCTION (cnode)
17332 if (TREE_PUBLIC (cnode->decl))
17333 s390_check_type_for_vector_abi (TREE_TYPE (cnode->decl), false, false);
17334
17335
17336 if (s390_vector_abi != 0)
17337 fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
17338 s390_vector_abi);
17339 #endif
17340 file_end_indicate_exec_stack ();
17341
17342 if (flag_split_stack)
17343 file_end_indicate_split_stack ();
17344 }
17345
17346 /* Return true if TYPE is a vector bool type. */
17347 static inline bool
17348 s390_vector_bool_type_p (const_tree type)
17349 {
17350 return TYPE_VECTOR_OPAQUE (type);
17351 }
17352
17353 /* Return the diagnostic message string if the binary operation OP is
17354 not permitted on TYPE1 and TYPE2, NULL otherwise. */
17355 static const char*
17356 s390_invalid_binary_op (int op ATTRIBUTE_UNUSED, const_tree type1, const_tree type2)
17357 {
17358 bool bool1_p, bool2_p;
17359 bool plusminus_p;
17360 bool muldiv_p;
17361 bool compare_p;
17362 machine_mode mode1, mode2;
17363
17364 if (!TARGET_ZVECTOR)
17365 return NULL;
17366
17367 if (!VECTOR_TYPE_P (type1) || !VECTOR_TYPE_P (type2))
17368 return NULL;
17369
17370 bool1_p = s390_vector_bool_type_p (type1);
17371 bool2_p = s390_vector_bool_type_p (type2);
17372
17373 /* Mixing signed and unsigned types is forbidden for all
17374 operators. */
17375 if (!bool1_p && !bool2_p
17376 && TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2))
17377 return N_("types differ in signedness");
17378
17379 plusminus_p = (op == PLUS_EXPR || op == MINUS_EXPR);
17380 muldiv_p = (op == MULT_EXPR || op == RDIV_EXPR || op == TRUNC_DIV_EXPR
17381 || op == CEIL_DIV_EXPR || op == FLOOR_DIV_EXPR
17382 || op == ROUND_DIV_EXPR);
17383 compare_p = (op == LT_EXPR || op == LE_EXPR || op == GT_EXPR || op == GE_EXPR
17384 || op == EQ_EXPR || op == NE_EXPR);
17385
17386 if (bool1_p && bool2_p && (plusminus_p || muldiv_p))
17387 return N_("binary operator does not support two vector bool operands");
17388
17389 if (bool1_p != bool2_p && (muldiv_p || compare_p))
17390 return N_("binary operator does not support vector bool operand");
17391
17392 mode1 = TYPE_MODE (type1);
17393 mode2 = TYPE_MODE (type2);
17394
17395 if (bool1_p != bool2_p && plusminus_p
17396 && (GET_MODE_CLASS (mode1) == MODE_VECTOR_FLOAT
17397 || GET_MODE_CLASS (mode2) == MODE_VECTOR_FLOAT))
17398 return N_("binary operator does not support mixing vector "
17399 "bool with floating point vector operands");
17400
17401 return NULL;
17402 }
17403
17404 #if ENABLE_S390_EXCESS_FLOAT_PRECISION == 1
17405 /* Implement TARGET_C_EXCESS_PRECISION to maintain historic behavior with older
17406 glibc versions
17407
17408 For historical reasons, float_t and double_t had been typedef'ed to
17409 double on s390, causing operations on float_t to operate in a higher
17410 precision than is necessary. However, it is not the case that SFmode
17411 operations have implicit excess precision, and we generate more optimal
17412 code if we let the compiler know no implicit extra precision is added.
17413
17414 With a glibc with that "historic" definition, configure will enable this hook
17415 to set FLT_EVAL_METHOD to 1 for -fexcess-precision=standard (e.g., as implied
17416 by -std=cXY). That means when we are compiling with -fexcess-precision=fast,
17417 the value we set for FLT_EVAL_METHOD will be out of line with the actual
17418 precision of float_t.
17419
17420 Newer versions of glibc will be modified to derive the definition of float_t
17421 from FLT_EVAL_METHOD on s390x, as on many other architectures. There,
17422 configure will disable this hook by default, so that we defer to the default
17423 of FLT_EVAL_METHOD_PROMOTE_TO_FLOAT and a resulting typedef of float_t to
17424 float. Note that in that scenario, float_t and FLT_EVAL_METHOD will be in
17425 line independent of -fexcess-precision. */
17426
17427 static enum flt_eval_method
17428 s390_excess_precision (enum excess_precision_type type)
17429 {
17430 switch (type)
17431 {
17432 case EXCESS_PRECISION_TYPE_IMPLICIT:
17433 case EXCESS_PRECISION_TYPE_FAST:
17434 /* The fastest type to promote to will always be the native type,
17435 whether that occurs with implicit excess precision or
17436 otherwise. */
17437 return FLT_EVAL_METHOD_PROMOTE_TO_FLOAT;
17438 case EXCESS_PRECISION_TYPE_STANDARD:
17439 /* Otherwise, when we are in a standards compliant mode, to
17440 ensure consistency with the implementation in glibc, report that
17441 float is evaluated to the range and precision of double. */
17442 return FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE;
17443 case EXCESS_PRECISION_TYPE_FLOAT16:
17444 error ("%<-fexcess-precision=16%> is not supported on this target");
17445 break;
17446 default:
17447 gcc_unreachable ();
17448 }
17449 return FLT_EVAL_METHOD_UNPREDICTABLE;
17450 }
17451 #endif
17452
17453 void
17454 s390_rawmemchr (machine_mode elt_mode, rtx dst, rtx src, rtx pat)
17455 {
17456 machine_mode vec_mode = mode_for_vector (as_a <scalar_int_mode> (elt_mode),
17457 16 / GET_MODE_SIZE (elt_mode)).require();
17458 rtx lens = gen_reg_rtx (V16QImode);
17459 rtx pattern = gen_reg_rtx (vec_mode);
17460 rtx loop_start = gen_label_rtx ();
17461 rtx loop_end = gen_label_rtx ();
17462 rtx addr = gen_reg_rtx (Pmode);
17463 rtx offset = gen_reg_rtx (Pmode);
17464 rtx loadlen = gen_reg_rtx (SImode);
17465 rtx matchlen = gen_reg_rtx (SImode);
17466 rtx mem;
17467
17468 pat = GEN_INT (trunc_int_for_mode (INTVAL (pat), elt_mode));
17469 emit_insn (gen_rtx_SET (pattern, gen_rtx_VEC_DUPLICATE (vec_mode, pat)));
17470
17471 emit_move_insn (addr, XEXP (src, 0));
17472
17473 // alignment
17474 emit_insn (gen_vlbb (lens, gen_rtx_MEM (BLKmode, addr), GEN_INT (6)));
17475 emit_insn (gen_lcbb (loadlen, addr, GEN_INT (6)));
17476 lens = convert_to_mode (vec_mode, lens, 1);
17477 emit_insn (gen_vec_vfees (vec_mode, lens, lens, pattern, GEN_INT (0)));
17478 lens = convert_to_mode (V4SImode, lens, 1);
17479 emit_insn (gen_vec_extractv4sisi (matchlen, lens, GEN_INT (1)));
17480 lens = convert_to_mode (vec_mode, lens, 1);
17481 emit_cmp_and_jump_insns (matchlen, loadlen, LT, NULL_RTX, SImode, 1, loop_end);
17482 force_expand_binop (Pmode, add_optab, addr, GEN_INT(16), addr, 1, OPTAB_DIRECT);
17483 force_expand_binop (Pmode, and_optab, addr, GEN_INT(~HOST_WIDE_INT_UC(0xf)), addr, 1, OPTAB_DIRECT);
17484 // now, addr is 16-byte aligned
17485
17486 mem = gen_rtx_MEM (vec_mode, addr);
17487 set_mem_align (mem, 128);
17488 emit_move_insn (lens, mem);
17489 emit_insn (gen_vec_vfees (vec_mode, lens, lens, pattern, GEN_INT (VSTRING_FLAG_CS)));
17490 add_int_reg_note (s390_emit_ccraw_jump (4, EQ, loop_end),
17491 REG_BR_PROB,
17492 profile_probability::very_unlikely ().to_reg_br_prob_note ());
17493
17494 emit_label (loop_start);
17495 LABEL_NUSES (loop_start) = 1;
17496
17497 force_expand_binop (Pmode, add_optab, addr, GEN_INT (16), addr, 1, OPTAB_DIRECT);
17498 mem = gen_rtx_MEM (vec_mode, addr);
17499 set_mem_align (mem, 128);
17500 emit_move_insn (lens, mem);
17501 emit_insn (gen_vec_vfees (vec_mode, lens, lens, pattern, GEN_INT (VSTRING_FLAG_CS)));
17502 add_int_reg_note (s390_emit_ccraw_jump (4, NE, loop_start),
17503 REG_BR_PROB,
17504 profile_probability::very_likely ().to_reg_br_prob_note ());
17505
17506 emit_label (loop_end);
17507 LABEL_NUSES (loop_end) = 1;
17508
17509 if (TARGET_64BIT)
17510 {
17511 lens = convert_to_mode (V2DImode, lens, 1);
17512 emit_insn (gen_vec_extractv2didi (offset, lens, GEN_INT (0)));
17513 }
17514 else
17515 {
17516 lens = convert_to_mode (V4SImode, lens, 1);
17517 emit_insn (gen_vec_extractv4sisi (offset, lens, GEN_INT (1)));
17518 }
17519 force_expand_binop (Pmode, add_optab, addr, offset, dst, 1, OPTAB_DIRECT);
17520 }
17521
17522 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
17523
17524 static unsigned HOST_WIDE_INT
17525 s390_asan_shadow_offset (void)
17526 {
17527 return TARGET_64BIT ? HOST_WIDE_INT_1U << 52 : HOST_WIDE_INT_UC (0x20000000);
17528 }
17529
17530 #ifdef HAVE_GAS_HIDDEN
17531 # define USE_HIDDEN_LINKONCE 1
17532 #else
17533 # define USE_HIDDEN_LINKONCE 0
17534 #endif
17535
17536 /* Output an indirect branch trampoline for target register REGNO. */
17537
17538 static void
17539 s390_output_indirect_thunk_function (unsigned int regno, bool z10_p)
17540 {
17541 tree decl;
17542 char thunk_label[32];
17543 int i;
17544
17545 if (z10_p)
17546 sprintf (thunk_label, TARGET_INDIRECT_BRANCH_THUNK_NAME_EXRL, regno);
17547 else
17548 sprintf (thunk_label, TARGET_INDIRECT_BRANCH_THUNK_NAME_EX,
17549 INDIRECT_BRANCH_THUNK_REGNUM, regno);
17550
17551 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17552 get_identifier (thunk_label),
17553 build_function_type_list (void_type_node, NULL_TREE));
17554 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
17555 NULL_TREE, void_type_node);
17556 TREE_PUBLIC (decl) = 1;
17557 TREE_STATIC (decl) = 1;
17558 DECL_IGNORED_P (decl) = 1;
17559
17560 if (USE_HIDDEN_LINKONCE)
17561 {
17562 cgraph_node::create (decl)->set_comdat_group (DECL_ASSEMBLER_NAME (decl));
17563
17564 targetm.asm_out.unique_section (decl, 0);
17565 switch_to_section (get_named_section (decl, NULL, 0));
17566
17567 targetm.asm_out.globalize_label (asm_out_file, thunk_label);
17568 fputs ("\t.hidden\t", asm_out_file);
17569 assemble_name (asm_out_file, thunk_label);
17570 putc ('\n', asm_out_file);
17571 ASM_DECLARE_FUNCTION_NAME (asm_out_file, thunk_label, decl);
17572 }
17573 else
17574 {
17575 switch_to_section (text_section);
17576 ASM_OUTPUT_LABEL (asm_out_file, thunk_label);
17577 }
17578
17579 DECL_INITIAL (decl) = make_node (BLOCK);
17580 current_function_decl = decl;
17581 allocate_struct_function (decl, false);
17582 init_function_start (decl);
17583 cfun->is_thunk = true;
17584 first_function_block_is_cold = false;
17585 final_start_function (emit_barrier (), asm_out_file, 1);
17586
17587 /* This makes CFI at least usable for indirect jumps.
17588
17589 Stopping in the thunk: backtrace will point to the thunk target
17590 is if it was interrupted by a signal. For a call this means that
17591 the call chain will be: caller->callee->thunk */
17592 if (flag_asynchronous_unwind_tables && flag_dwarf2_cfi_asm)
17593 {
17594 fputs ("\t.cfi_signal_frame\n", asm_out_file);
17595 fprintf (asm_out_file, "\t.cfi_return_column %d\n", regno);
17596 for (i = 0; i < FPR15_REGNUM; i++)
17597 fprintf (asm_out_file, "\t.cfi_same_value %s\n", reg_names[i]);
17598 }
17599
17600 if (z10_p)
17601 {
17602 /* exrl 0,1f */
17603
17604 /* We generate a thunk for z10 compiled code although z10 is
17605 currently not enabled. Tell the assembler to accept the
17606 instruction. */
17607 if (!TARGET_CPU_Z10)
17608 {
17609 fputs ("\t.machine push\n", asm_out_file);
17610 fputs ("\t.machine z10\n", asm_out_file);
17611 }
17612 /* We use exrl even if -mzarch hasn't been specified on the
17613 command line so we have to tell the assembler to accept
17614 it. */
17615 if (!TARGET_ZARCH)
17616 fputs ("\t.machinemode zarch\n", asm_out_file);
17617
17618 fputs ("\texrl\t0,1f\n", asm_out_file);
17619
17620 if (!TARGET_ZARCH)
17621 fputs ("\t.machinemode esa\n", asm_out_file);
17622
17623 if (!TARGET_CPU_Z10)
17624 fputs ("\t.machine pop\n", asm_out_file);
17625 }
17626 else
17627 {
17628 /* larl %r1,1f */
17629 fprintf (asm_out_file, "\tlarl\t%%r%d,1f\n",
17630 INDIRECT_BRANCH_THUNK_REGNUM);
17631
17632 /* ex 0,0(%r1) */
17633 fprintf (asm_out_file, "\tex\t0,0(%%r%d)\n",
17634 INDIRECT_BRANCH_THUNK_REGNUM);
17635 }
17636
17637 /* 0: j 0b */
17638 fputs ("0:\tj\t0b\n", asm_out_file);
17639
17640 /* 1: br <regno> */
17641 fprintf (asm_out_file, "1:\tbr\t%%r%d\n", regno);
17642
17643 final_end_function ();
17644 init_insn_lengths ();
17645 free_after_compilation (cfun);
17646 set_cfun (NULL);
17647 current_function_decl = NULL;
17648 }
17649
17650 /* Implement the asm.code_end target hook. */
17651
17652 static void
17653 s390_code_end (void)
17654 {
17655 int i;
17656
17657 for (i = 1; i < 16; i++)
17658 {
17659 if (indirect_branch_z10thunk_mask & (1 << i))
17660 s390_output_indirect_thunk_function (i, true);
17661
17662 if (indirect_branch_prez10thunk_mask & (1 << i))
17663 s390_output_indirect_thunk_function (i, false);
17664 }
17665
17666 if (TARGET_INDIRECT_BRANCH_TABLE)
17667 {
17668 int o;
17669 int i;
17670
17671 for (o = 0; o < INDIRECT_BRANCH_NUM_OPTIONS; o++)
17672 {
17673 if (indirect_branch_table_label_no[o] == 0)
17674 continue;
17675
17676 switch_to_section (get_section (indirect_branch_table_name[o],
17677 0,
17678 NULL_TREE));
17679 for (i = 0; i < indirect_branch_table_label_no[o]; i++)
17680 {
17681 char label_start[32];
17682
17683 ASM_GENERATE_INTERNAL_LABEL (label_start,
17684 indirect_branch_table_label[o], i);
17685
17686 fputs ("\t.long\t", asm_out_file);
17687 assemble_name_raw (asm_out_file, label_start);
17688 fputs ("-.\n", asm_out_file);
17689 }
17690 }
17691 }
17692 }
17693
17694 /* Implement the TARGET_CASE_VALUES_THRESHOLD target hook. */
17695
17696 unsigned int
17697 s390_case_values_threshold (void)
17698 {
17699 /* Disabling branch prediction for indirect jumps makes jump tables
17700 much more expensive. */
17701 if (TARGET_INDIRECT_BRANCH_NOBP_JUMP)
17702 return 20;
17703
17704 return default_case_values_threshold ();
17705 }
17706
17707 /* Evaluate the insns between HEAD and TAIL and do back-end to install
17708 back-end specific dependencies.
17709
17710 Establish an ANTI dependency between r11 and r15 restores from FPRs
17711 to prevent the instructions scheduler from reordering them since
17712 this would break CFI. No further handling in the sched_reorder
17713 hook is required since the r11 and r15 restore will never appear in
17714 the same ready list with that change. */
17715 void
17716 s390_sched_dependencies_evaluation (rtx_insn *head, rtx_insn *tail)
17717 {
17718 if (!frame_pointer_needed || !epilogue_completed)
17719 return;
17720
17721 while (head != tail && DEBUG_INSN_P (head))
17722 head = NEXT_INSN (head);
17723
17724 rtx_insn *r15_restore = NULL, *r11_restore = NULL;
17725
17726 for (rtx_insn *insn = tail; insn != head; insn = PREV_INSN (insn))
17727 {
17728 rtx set = single_set (insn);
17729 if (!INSN_P (insn)
17730 || !RTX_FRAME_RELATED_P (insn)
17731 || set == NULL_RTX
17732 || !REG_P (SET_DEST (set))
17733 || !FP_REG_P (SET_SRC (set)))
17734 continue;
17735
17736 if (REGNO (SET_DEST (set)) == HARD_FRAME_POINTER_REGNUM)
17737 r11_restore = insn;
17738
17739 if (REGNO (SET_DEST (set)) == STACK_POINTER_REGNUM)
17740 r15_restore = insn;
17741 }
17742
17743 if (r11_restore == NULL || r15_restore == NULL)
17744 return;
17745 add_dependence (r11_restore, r15_restore, REG_DEP_ANTI);
17746 }
17747
17748 /* Implement TARGET_SHIFT_TRUNCATION_MASK for integer shifts. */
17749
17750 static unsigned HOST_WIDE_INT
17751 s390_shift_truncation_mask (machine_mode mode)
17752 {
17753 return mode == DImode || mode == SImode ? 63 : 0;
17754 }
17755
17756 /* Return TRUE iff CONSTRAINT is an "f" constraint, possibly with additional
17757 modifiers. */
17758
17759 static bool
17760 f_constraint_p (const char *constraint)
17761 {
17762 bool seen_f_p = false;
17763 bool seen_v_p = false;
17764
17765 for (size_t i = 0, c_len = strlen (constraint); i < c_len;
17766 i += CONSTRAINT_LEN (constraint[i], constraint + i))
17767 {
17768 if (constraint[i] == 'f')
17769 seen_f_p = true;
17770 if (constraint[i] == 'v')
17771 seen_v_p = true;
17772 }
17773
17774 /* Treat "fv" constraints as "v", because LRA will choose the widest register
17775 * class. */
17776 return seen_f_p && !seen_v_p;
17777 }
17778
17779 /* Return TRUE iff X is a hard floating-point (and not a vector) register. */
17780
17781 static bool
17782 s390_hard_fp_reg_p (rtx x)
17783 {
17784 if (!(REG_P (x) && HARD_REGISTER_P (x) && REG_ATTRS (x)))
17785 return false;
17786
17787 tree decl = REG_EXPR (x);
17788 if (!(HAS_DECL_ASSEMBLER_NAME_P (decl) && DECL_ASSEMBLER_NAME_SET_P (decl)))
17789 return false;
17790
17791 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17792
17793 return name[0] == '*' && name[1] == 'f';
17794 }
17795
17796 /* Implement TARGET_MD_ASM_ADJUST hook in order to fix up "f"
17797 constraints when long doubles are stored in vector registers. */
17798
17799 static rtx_insn *
17800 s390_md_asm_adjust (vec<rtx> &outputs, vec<rtx> &inputs,
17801 vec<machine_mode> &input_modes,
17802 vec<const char *> &constraints,
17803 vec<rtx> &/*uses*/, vec<rtx> &/*clobbers*/,
17804 HARD_REG_SET &clobbered_regs, location_t loc)
17805 {
17806
17807 rtx_insn *after_md_seq = NULL, *after_md_end = NULL;
17808 bool saw_cc = false;
17809
17810 unsigned ninputs = inputs.length ();
17811 unsigned noutputs = outputs.length ();
17812 for (unsigned i = 0; i < noutputs; i++)
17813 {
17814 const char *constraint = constraints[i];
17815 if (strncmp (constraint, "=@cc", 4) == 0)
17816 {
17817 if (constraint[4] != 0)
17818 {
17819 error_at (loc, "invalid cc output constraint: %qs", constraint);
17820 continue;
17821 }
17822 if (saw_cc)
17823 {
17824 error_at (loc, "multiple cc output constraints not supported");
17825 continue;
17826 }
17827 if (TEST_HARD_REG_BIT (clobbered_regs, CC_REGNUM))
17828 {
17829 error_at (loc, "%<asm%> specifier for cc output conflicts with %<asm%> clobber list");
17830 continue;
17831 }
17832 rtx dest = outputs[i];
17833 if (GET_MODE (dest) != SImode)
17834 {
17835 error ("invalid type for cc output constraint");
17836 continue;
17837 }
17838 saw_cc = true;
17839 constraints[i] = "=c";
17840 outputs[i] = gen_rtx_REG (CCRAWmode, CC_REGNUM);
17841
17842 push_to_sequence2 (after_md_seq, after_md_end);
17843 emit_insn (gen_rtx_SET (dest,
17844 gen_rtx_UNSPEC (SImode,
17845 gen_rtvec (1, outputs[i]),
17846 UNSPEC_CC_TO_INT)));
17847 after_md_seq = get_insns ();
17848 after_md_end = get_last_insn ();
17849 end_sequence ();
17850 continue;
17851 }
17852 if (!TARGET_VXE)
17853 /* Long doubles are stored in FPR pairs - nothing to do. */
17854 continue;
17855 if (GET_MODE (outputs[i]) != TFmode)
17856 /* Not a long double - nothing to do. */
17857 continue;
17858 bool allows_mem, allows_reg, is_inout;
17859 bool ok = parse_output_constraint (&constraint, i, ninputs, noutputs,
17860 &allows_mem, &allows_reg, &is_inout);
17861 gcc_assert (ok);
17862 if (!f_constraint_p (constraint))
17863 /* Long double with a constraint other than "=f" - nothing to do. */
17864 continue;
17865 gcc_assert (allows_reg);
17866 gcc_assert (!is_inout);
17867 /* Copy output value from a FPR pair into a vector register. */
17868 rtx fprx2;
17869 push_to_sequence2 (after_md_seq, after_md_end);
17870 if (s390_hard_fp_reg_p (outputs[i]))
17871 {
17872 fprx2 = gen_rtx_REG (FPRX2mode, REGNO (outputs[i]));
17873 /* The first half is already at the correct location, copy only the
17874 * second one. Use the UNSPEC pattern instead of the SUBREG one,
17875 * since s390_can_change_mode_class() rejects
17876 * (subreg:DF (reg:TF %fN) 8) and thus subreg validation fails. */
17877 rtx v1 = gen_rtx_REG (V2DFmode, REGNO (outputs[i]));
17878 rtx v3 = gen_rtx_REG (V2DFmode, REGNO (outputs[i]) + 1);
17879 emit_insn (gen_vec_permiv2df (v1, v1, v3, const0_rtx));
17880 }
17881 else
17882 {
17883 fprx2 = gen_reg_rtx (FPRX2mode);
17884 emit_insn (gen_fprx2_to_tf (outputs[i], fprx2));
17885 }
17886 after_md_seq = get_insns ();
17887 after_md_end = get_last_insn ();
17888 end_sequence ();
17889 outputs[i] = fprx2;
17890 }
17891
17892 if (!TARGET_VXE)
17893 /* Long doubles are stored in FPR pairs - nothing left to do. */
17894 return after_md_seq;
17895
17896 for (unsigned i = 0; i < ninputs; i++)
17897 {
17898 if (GET_MODE (inputs[i]) != TFmode)
17899 /* Not a long double - nothing to do. */
17900 continue;
17901 const char *constraint = constraints[noutputs + i];
17902 bool allows_mem, allows_reg;
17903 bool ok = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
17904 constraints.address (), &allows_mem,
17905 &allows_reg);
17906 gcc_assert (ok);
17907 if (!f_constraint_p (constraint))
17908 /* Long double with a constraint other than "f" (or "=f" for inout
17909 operands) - nothing to do. */
17910 continue;
17911 gcc_assert (allows_reg);
17912 /* Copy input value from a vector register into a FPR pair. */
17913 rtx fprx2;
17914 if (s390_hard_fp_reg_p (inputs[i]))
17915 {
17916 fprx2 = gen_rtx_REG (FPRX2mode, REGNO (inputs[i]));
17917 /* Copy only the second half. */
17918 rtx v1 = gen_rtx_REG (V2DFmode, REGNO (inputs[i]) + 1);
17919 rtx v2 = gen_rtx_REG (V2DFmode, REGNO (inputs[i]));
17920 emit_insn (gen_vec_permiv2df (v1, v2, v1, GEN_INT (3)));
17921 }
17922 else
17923 {
17924 fprx2 = gen_reg_rtx (FPRX2mode);
17925 emit_insn (gen_tf_to_fprx2 (fprx2, inputs[i]));
17926 }
17927 inputs[i] = fprx2;
17928 input_modes[i] = FPRX2mode;
17929 }
17930
17931 return after_md_seq;
17932 }
17933
17934 #define MAX_VECT_LEN 16
17935
17936 struct expand_vec_perm_d
17937 {
17938 rtx target, op0, op1;
17939 unsigned char perm[MAX_VECT_LEN];
17940 machine_mode vmode;
17941 unsigned char nelt;
17942 bool testing_p;
17943 bool only_op0;
17944 bool only_op1;
17945 };
17946
17947 /* Try to expand the vector permute operation described by D using the
17948 vector merge instructions vml and vmh. Return true if vector merge
17949 could be used. */
17950 static bool
17951 expand_perm_with_merge (const struct expand_vec_perm_d &d)
17952 {
17953 static const unsigned char hi_perm_di[2] = {0, 2};
17954 static const unsigned char hi_perm_si[4] = {0, 4, 1, 5};
17955 static const unsigned char hi_perm_hi[8] = {0, 8, 1, 9, 2, 10, 3, 11};
17956 static const unsigned char hi_perm_qi[16]
17957 = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23};
17958
17959 static const unsigned char hi_perm_di_swap[2] = {2, 0};
17960 static const unsigned char hi_perm_si_swap[4] = {4, 0, 6, 2};
17961 static const unsigned char hi_perm_hi_swap[8] = {8, 0, 10, 2, 12, 4, 14, 6};
17962 static const unsigned char hi_perm_qi_swap[16]
17963 = {16, 0, 18, 2, 20, 4, 22, 6, 24, 8, 26, 10, 28, 12, 30, 14};
17964
17965 static const unsigned char lo_perm_di[2] = {1, 3};
17966 static const unsigned char lo_perm_si[4] = {2, 6, 3, 7};
17967 static const unsigned char lo_perm_hi[8] = {4, 12, 5, 13, 6, 14, 7, 15};
17968 static const unsigned char lo_perm_qi[16]
17969 = {8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31};
17970
17971 static const unsigned char lo_perm_di_swap[2] = {3, 1};
17972 static const unsigned char lo_perm_si_swap[4] = {5, 1, 7, 3};
17973 static const unsigned char lo_perm_hi_swap[8] = {9, 1, 11, 3, 13, 5, 15, 7};
17974 static const unsigned char lo_perm_qi_swap[16]
17975 = {17, 1, 19, 3, 21, 5, 23, 7, 25, 9, 27, 11, 29, 13, 31, 15};
17976
17977 bool merge_lo_p = false;
17978 bool merge_hi_p = false;
17979 bool swap_operands_p = false;
17980
17981 if ((d.nelt == 2 && memcmp (d.perm, hi_perm_di, 2) == 0)
17982 || (d.nelt == 4 && memcmp (d.perm, hi_perm_si, 4) == 0)
17983 || (d.nelt == 8 && memcmp (d.perm, hi_perm_hi, 8) == 0)
17984 || (d.nelt == 16 && memcmp (d.perm, hi_perm_qi, 16) == 0))
17985 {
17986 merge_hi_p = true;
17987 }
17988 else if ((d.nelt == 2 && memcmp (d.perm, hi_perm_di_swap, 2) == 0)
17989 || (d.nelt == 4 && memcmp (d.perm, hi_perm_si_swap, 4) == 0)
17990 || (d.nelt == 8 && memcmp (d.perm, hi_perm_hi_swap, 8) == 0)
17991 || (d.nelt == 16 && memcmp (d.perm, hi_perm_qi_swap, 16) == 0))
17992 {
17993 merge_hi_p = true;
17994 swap_operands_p = true;
17995 }
17996 else if ((d.nelt == 2 && memcmp (d.perm, lo_perm_di, 2) == 0)
17997 || (d.nelt == 4 && memcmp (d.perm, lo_perm_si, 4) == 0)
17998 || (d.nelt == 8 && memcmp (d.perm, lo_perm_hi, 8) == 0)
17999 || (d.nelt == 16 && memcmp (d.perm, lo_perm_qi, 16) == 0))
18000 {
18001 merge_lo_p = true;
18002 }
18003 else if ((d.nelt == 2 && memcmp (d.perm, lo_perm_di_swap, 2) == 0)
18004 || (d.nelt == 4 && memcmp (d.perm, lo_perm_si_swap, 4) == 0)
18005 || (d.nelt == 8 && memcmp (d.perm, lo_perm_hi_swap, 8) == 0)
18006 || (d.nelt == 16 && memcmp (d.perm, lo_perm_qi_swap, 16) == 0))
18007 {
18008 merge_lo_p = true;
18009 swap_operands_p = true;
18010 }
18011
18012 if (!merge_lo_p && !merge_hi_p)
18013 return false;
18014
18015 if (d.testing_p)
18016 return merge_lo_p || merge_hi_p;
18017
18018 rtx op0, op1;
18019 if (swap_operands_p)
18020 {
18021 op0 = d.op1;
18022 op1 = d.op0;
18023 }
18024 else
18025 {
18026 op0 = d.op0;
18027 op1 = d.op1;
18028 }
18029
18030 s390_expand_merge (d.target, op0, op1, merge_hi_p);
18031
18032 return true;
18033 }
18034
18035 /* Try to expand the vector permute operation described by D using the
18036 vector permute doubleword immediate instruction vpdi. Return true
18037 if vpdi could be used.
18038
18039 VPDI allows 4 different immediate values (0, 1, 4, 5). The 0 and 5
18040 cases are covered by vmrhg and vmrlg already. So we only care
18041 about the 1, 4 cases here.
18042 1 - First element of src1 and second of src2
18043 4 - Second element of src1 and first of src2 */
18044 static bool
18045 expand_perm_with_vpdi (const struct expand_vec_perm_d &d)
18046 {
18047 bool vpdi1_p = false;
18048 bool vpdi4_p = false;
18049 bool swap_operands_p = false;
18050 rtx op0_reg, op1_reg;
18051
18052 // Only V2DI and V2DF are supported here.
18053 if (d.nelt != 2)
18054 return false;
18055
18056 if (d.perm[0] == 0 && d.perm[1] == 3)
18057 vpdi1_p = true;
18058 else if (d.perm[0] == 2 && d.perm[1] == 1)
18059 {
18060 vpdi1_p = true;
18061 swap_operands_p = true;
18062 }
18063 else if ((d.perm[0] == 1 && d.perm[1] == 2)
18064 || (d.perm[0] == 1 && d.perm[1] == 0)
18065 || (d.perm[0] == 3 && d.perm[1] == 2))
18066 vpdi4_p = true;
18067 else if (d.perm[0] == 3 && d.perm[1] == 0)
18068 {
18069 vpdi4_p = true;
18070 swap_operands_p = true;
18071 }
18072
18073 if (!vpdi1_p && !vpdi4_p)
18074 return false;
18075
18076 if (d.testing_p)
18077 return true;
18078
18079 op0_reg = force_reg (GET_MODE (d.op0), d.op0);
18080 op1_reg = force_reg (GET_MODE (d.op1), d.op1);
18081
18082 /* If we only reference either of the operands in
18083 the permute mask, just use one of them. */
18084 if (d.only_op0)
18085 op1_reg = op0_reg;
18086 else if (d.only_op1)
18087 op0_reg = op1_reg;
18088 else if (swap_operands_p)
18089 {
18090 rtx tmp = op0_reg;
18091 op0_reg = op1_reg;
18092 op1_reg = tmp;
18093 }
18094
18095 if (vpdi1_p)
18096 emit_insn (gen_vpdi1 (d.vmode, d.target, op0_reg, op1_reg));
18097 if (vpdi4_p)
18098 emit_insn (gen_vpdi4 (d.vmode, d.target, op0_reg, op1_reg));
18099
18100 return true;
18101 }
18102
18103 /* Helper that checks if a vector permutation mask D
18104 represents a reversal of the vector's elements. */
18105 static inline bool
18106 is_reverse_perm_mask (const struct expand_vec_perm_d &d)
18107 {
18108 for (int i = 0; i < d.nelt; i++)
18109 if (d.perm[i] != d.nelt - i - 1)
18110 return false;
18111 return true;
18112 }
18113
18114 static bool
18115 expand_perm_reverse_elements (const struct expand_vec_perm_d &d)
18116 {
18117 if (d.op0 != d.op1 || !is_reverse_perm_mask (d))
18118 return false;
18119
18120 if (d.testing_p)
18121 return true;
18122
18123 switch (d.vmode)
18124 {
18125 case V1TImode: emit_move_insn (d.target, d.op0); break;
18126 case V2DImode: emit_insn (gen_eltswapv2di (d.target, d.op0)); break;
18127 case V4SImode: emit_insn (gen_eltswapv4si (d.target, d.op0)); break;
18128 case V8HImode: emit_insn (gen_eltswapv8hi (d.target, d.op0)); break;
18129 case V16QImode: emit_insn (gen_eltswapv16qi (d.target, d.op0)); break;
18130 case V2DFmode: emit_insn (gen_eltswapv2df (d.target, d.op0)); break;
18131 case V4SFmode: emit_insn (gen_eltswapv4sf (d.target, d.op0)); break;
18132 default: gcc_unreachable();
18133 }
18134
18135 return true;
18136 }
18137
18138 /* Try to emit vlbr/vstbr. Note, this is only a candidate insn since
18139 TARGET_VECTORIZE_VEC_PERM_CONST operates on vector registers only. Thus,
18140 either fwprop, combine et al. "fixes" one of the input/output operands into
18141 a memory operand or a splitter has to reverse this into a general vperm
18142 operation. */
18143
18144 static bool
18145 expand_perm_as_a_vlbr_vstbr_candidate (const struct expand_vec_perm_d &d)
18146 {
18147 static const char perm[4][MAX_VECT_LEN]
18148 = { { 1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14 },
18149 { 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12 },
18150 { 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8 },
18151 { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 } };
18152
18153 if (!TARGET_VXE2 || d.vmode != V16QImode || d.op0 != d.op1)
18154 return false;
18155
18156 if (memcmp (d.perm, perm[0], MAX_VECT_LEN) == 0)
18157 {
18158 if (!d.testing_p)
18159 {
18160 rtx target = gen_rtx_SUBREG (V8HImode, d.target, 0);
18161 rtx op0 = gen_rtx_SUBREG (V8HImode, d.op0, 0);
18162 emit_insn (gen_bswapv8hi (target, op0));
18163 }
18164 return true;
18165 }
18166
18167 if (memcmp (d.perm, perm[1], MAX_VECT_LEN) == 0)
18168 {
18169 if (!d.testing_p)
18170 {
18171 rtx target = gen_rtx_SUBREG (V4SImode, d.target, 0);
18172 rtx op0 = gen_rtx_SUBREG (V4SImode, d.op0, 0);
18173 emit_insn (gen_bswapv4si (target, op0));
18174 }
18175 return true;
18176 }
18177
18178 if (memcmp (d.perm, perm[2], MAX_VECT_LEN) == 0)
18179 {
18180 if (!d.testing_p)
18181 {
18182 rtx target = gen_rtx_SUBREG (V2DImode, d.target, 0);
18183 rtx op0 = gen_rtx_SUBREG (V2DImode, d.op0, 0);
18184 emit_insn (gen_bswapv2di (target, op0));
18185 }
18186 return true;
18187 }
18188
18189 if (memcmp (d.perm, perm[3], MAX_VECT_LEN) == 0)
18190 {
18191 if (!d.testing_p)
18192 {
18193 rtx target = gen_rtx_SUBREG (V1TImode, d.target, 0);
18194 rtx op0 = gen_rtx_SUBREG (V1TImode, d.op0, 0);
18195 emit_insn (gen_bswapv1ti (target, op0));
18196 }
18197 return true;
18198 }
18199
18200 return false;
18201 }
18202
18203 static bool
18204 expand_perm_as_replicate (const struct expand_vec_perm_d &d)
18205 {
18206 unsigned char i;
18207 unsigned char elem;
18208 rtx base = d.op0;
18209 rtx insn = NULL_RTX;
18210
18211 /* Needed to silence maybe-uninitialized warning. */
18212 gcc_assert (d.nelt > 0);
18213 elem = d.perm[0];
18214 for (i = 1; i < d.nelt; ++i)
18215 if (d.perm[i] != elem)
18216 return false;
18217 if (!d.testing_p)
18218 {
18219 if (elem >= d.nelt)
18220 {
18221 base = d.op1;
18222 elem -= d.nelt;
18223 }
18224 if (memory_operand (base, d.vmode))
18225 {
18226 /* Try to use vector load and replicate. */
18227 rtx new_base = adjust_address (base, GET_MODE_INNER (d.vmode),
18228 elem * GET_MODE_UNIT_SIZE (d.vmode));
18229 insn = maybe_gen_vec_splats (d.vmode, d.target, new_base);
18230 }
18231 if (insn == NULL_RTX)
18232 {
18233 base = force_reg (d.vmode, base);
18234 insn = maybe_gen_vec_splat (d.vmode, d.target, base, GEN_INT (elem));
18235 }
18236
18237 if (insn == NULL_RTX)
18238 return false;
18239 emit_insn (insn);
18240 return true;
18241 }
18242 else
18243 return maybe_code_for_vec_splat (d.vmode) != CODE_FOR_nothing;
18244 }
18245
18246 /* Try to find the best sequence for the vector permute operation
18247 described by D. Return true if the operation could be
18248 expanded. */
18249 static bool
18250 vectorize_vec_perm_const_1 (const struct expand_vec_perm_d &d)
18251 {
18252 if (expand_perm_reverse_elements (d))
18253 return true;
18254
18255 if (expand_perm_with_merge (d))
18256 return true;
18257
18258 if (expand_perm_with_vpdi (d))
18259 return true;
18260
18261 if (expand_perm_as_a_vlbr_vstbr_candidate (d))
18262 return true;
18263
18264 if (expand_perm_as_replicate (d))
18265 return true;
18266
18267 return false;
18268 }
18269
18270 /* Return true if we can emit instructions for the constant
18271 permutation vector in SEL. If OUTPUT, IN0, IN1 are non-null the
18272 hook is supposed to emit the required INSNs. */
18273
18274 bool
18275 s390_vectorize_vec_perm_const (machine_mode vmode, machine_mode op_mode,
18276 rtx target, rtx op0, rtx op1,
18277 const vec_perm_indices &sel)
18278 {
18279 if (vmode != op_mode)
18280 return false;
18281
18282 struct expand_vec_perm_d d;
18283 unsigned int i, nelt;
18284
18285 if (!s390_vector_mode_supported_p (vmode) || GET_MODE_SIZE (vmode) != 16)
18286 return false;
18287
18288 d.target = target;
18289 d.op0 = op0;
18290 d.op1 = op1;
18291
18292 d.vmode = vmode;
18293 gcc_assert (VECTOR_MODE_P (d.vmode));
18294 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
18295 d.testing_p = target == NULL_RTX;
18296 d.only_op0 = false;
18297 d.only_op1 = false;
18298
18299 gcc_assert (target == NULL_RTX || REG_P (target));
18300 gcc_assert (sel.length () == nelt);
18301
18302 unsigned int highest = 0, lowest = 2 * nelt - 1;
18303 for (i = 0; i < nelt; i++)
18304 {
18305 unsigned char e = sel[i];
18306 lowest = MIN (lowest, e);
18307 highest = MAX (highest, e);
18308 gcc_assert (e < 2 * nelt);
18309 d.perm[i] = e;
18310 }
18311
18312 if (lowest < nelt && highest < nelt)
18313 d.only_op0 = true;
18314 else if (lowest >= nelt && highest >= nelt)
18315 d.only_op1 = true;
18316
18317 return vectorize_vec_perm_const_1 (d);
18318 }
18319
18320 /* Consider a NOCE conversion as profitable if there is at least one
18321 conditional move. */
18322
18323 static bool
18324 s390_noce_conversion_profitable_p (rtx_insn *seq, struct noce_if_info *if_info)
18325 {
18326 if (if_info->speed_p)
18327 {
18328 for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
18329 {
18330 rtx set = single_set (insn);
18331 if (set == NULL)
18332 continue;
18333 if (GET_CODE (SET_SRC (set)) != IF_THEN_ELSE)
18334 continue;
18335 rtx src = SET_SRC (set);
18336 machine_mode mode = GET_MODE (src);
18337 if (GET_MODE_CLASS (mode) != MODE_INT
18338 && GET_MODE_CLASS (mode) != MODE_FLOAT)
18339 continue;
18340 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
18341 continue;
18342 return true;
18343 }
18344 }
18345 return default_noce_conversion_profitable_p (seq, if_info);
18346 }
18347
18348 /* Implement TARGET_C_MODE_FOR_FLOATING_TYPE. Return TFmode or DFmode
18349 for TI_LONG_DOUBLE_TYPE which is for long double type, go with the
18350 default one for the others. */
18351
18352 static machine_mode
18353 s390_c_mode_for_floating_type (enum tree_index ti)
18354 {
18355 if (ti == TI_LONG_DOUBLE_TYPE)
18356 return TARGET_LONG_DOUBLE_128 ? TFmode : DFmode;
18357 return default_mode_for_floating_type (ti);
18358 }
18359
18360 /* Initialize GCC target structure. */
18361
18362 #undef TARGET_ASM_ALIGNED_HI_OP
18363 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
18364 #undef TARGET_ASM_ALIGNED_DI_OP
18365 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
18366 #undef TARGET_ASM_INTEGER
18367 #define TARGET_ASM_INTEGER s390_assemble_integer
18368
18369 #undef TARGET_ASM_OPEN_PAREN
18370 #define TARGET_ASM_OPEN_PAREN ""
18371
18372 #undef TARGET_ASM_CLOSE_PAREN
18373 #define TARGET_ASM_CLOSE_PAREN ""
18374
18375 #undef TARGET_OPTION_OVERRIDE
18376 #define TARGET_OPTION_OVERRIDE s390_option_override
18377
18378 #ifdef TARGET_THREAD_SSP_OFFSET
18379 #undef TARGET_STACK_PROTECT_GUARD
18380 #define TARGET_STACK_PROTECT_GUARD hook_tree_void_null
18381 #endif
18382
18383 #undef TARGET_ENCODE_SECTION_INFO
18384 #define TARGET_ENCODE_SECTION_INFO s390_encode_section_info
18385
18386 #undef TARGET_SCALAR_MODE_SUPPORTED_P
18387 #define TARGET_SCALAR_MODE_SUPPORTED_P s390_scalar_mode_supported_p
18388
18389 #ifdef HAVE_AS_TLS
18390 #undef TARGET_HAVE_TLS
18391 #define TARGET_HAVE_TLS true
18392 #endif
18393 #undef TARGET_CANNOT_FORCE_CONST_MEM
18394 #define TARGET_CANNOT_FORCE_CONST_MEM s390_cannot_force_const_mem
18395
18396 #undef TARGET_DELEGITIMIZE_ADDRESS
18397 #define TARGET_DELEGITIMIZE_ADDRESS s390_delegitimize_address
18398
18399 #undef TARGET_LEGITIMIZE_ADDRESS
18400 #define TARGET_LEGITIMIZE_ADDRESS s390_legitimize_address
18401
18402 #undef TARGET_RETURN_IN_MEMORY
18403 #define TARGET_RETURN_IN_MEMORY s390_return_in_memory
18404
18405 #undef TARGET_INIT_BUILTINS
18406 #define TARGET_INIT_BUILTINS s390_init_builtins
18407 #undef TARGET_EXPAND_BUILTIN
18408 #define TARGET_EXPAND_BUILTIN s390_expand_builtin
18409 #undef TARGET_BUILTIN_DECL
18410 #define TARGET_BUILTIN_DECL s390_builtin_decl
18411
18412 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
18413 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA s390_output_addr_const_extra
18414
18415 #undef TARGET_ASM_OUTPUT_MI_THUNK
18416 #define TARGET_ASM_OUTPUT_MI_THUNK s390_output_mi_thunk
18417 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
18418 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
18419
18420 #if ENABLE_S390_EXCESS_FLOAT_PRECISION == 1
18421 /* This hook is only needed to maintain the historic behavior with glibc
18422 versions that typedef float_t to double. */
18423 #undef TARGET_C_EXCESS_PRECISION
18424 #define TARGET_C_EXCESS_PRECISION s390_excess_precision
18425 #endif
18426
18427 #undef TARGET_SCHED_ADJUST_PRIORITY
18428 #define TARGET_SCHED_ADJUST_PRIORITY s390_adjust_priority
18429 #undef TARGET_SCHED_ISSUE_RATE
18430 #define TARGET_SCHED_ISSUE_RATE s390_issue_rate
18431 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
18432 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD s390_first_cycle_multipass_dfa_lookahead
18433
18434 #undef TARGET_SCHED_VARIABLE_ISSUE
18435 #define TARGET_SCHED_VARIABLE_ISSUE s390_sched_variable_issue
18436 #undef TARGET_SCHED_REORDER
18437 #define TARGET_SCHED_REORDER s390_sched_reorder
18438 #undef TARGET_SCHED_INIT
18439 #define TARGET_SCHED_INIT s390_sched_init
18440
18441 #undef TARGET_CANNOT_COPY_INSN_P
18442 #define TARGET_CANNOT_COPY_INSN_P s390_cannot_copy_insn_p
18443 #undef TARGET_INSN_COST
18444 #define TARGET_INSN_COST s390_insn_cost
18445 #undef TARGET_RTX_COSTS
18446 #define TARGET_RTX_COSTS s390_rtx_costs
18447 #undef TARGET_ADDRESS_COST
18448 #define TARGET_ADDRESS_COST s390_address_cost
18449 #undef TARGET_REGISTER_MOVE_COST
18450 #define TARGET_REGISTER_MOVE_COST s390_register_move_cost
18451 #undef TARGET_MEMORY_MOVE_COST
18452 #define TARGET_MEMORY_MOVE_COST s390_memory_move_cost
18453 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
18454 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
18455 s390_builtin_vectorization_cost
18456
18457 #undef TARGET_MACHINE_DEPENDENT_REORG
18458 #define TARGET_MACHINE_DEPENDENT_REORG s390_reorg
18459
18460 #undef TARGET_VALID_POINTER_MODE
18461 #define TARGET_VALID_POINTER_MODE s390_valid_pointer_mode
18462
18463 #undef TARGET_BUILD_BUILTIN_VA_LIST
18464 #define TARGET_BUILD_BUILTIN_VA_LIST s390_build_builtin_va_list
18465 #undef TARGET_EXPAND_BUILTIN_VA_START
18466 #define TARGET_EXPAND_BUILTIN_VA_START s390_va_start
18467 #undef TARGET_ASAN_SHADOW_OFFSET
18468 #define TARGET_ASAN_SHADOW_OFFSET s390_asan_shadow_offset
18469 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
18470 #define TARGET_GIMPLIFY_VA_ARG_EXPR s390_gimplify_va_arg
18471
18472 #undef TARGET_PROMOTE_FUNCTION_MODE
18473 #define TARGET_PROMOTE_FUNCTION_MODE s390_promote_function_mode
18474 #undef TARGET_PASS_BY_REFERENCE
18475 #define TARGET_PASS_BY_REFERENCE s390_pass_by_reference
18476
18477 #undef TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE
18478 #define TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE s390_override_options_after_change
18479
18480 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
18481 #define TARGET_FUNCTION_OK_FOR_SIBCALL s390_function_ok_for_sibcall
18482 #undef TARGET_FUNCTION_ARG
18483 #define TARGET_FUNCTION_ARG s390_function_arg
18484 #undef TARGET_FUNCTION_ARG_ADVANCE
18485 #define TARGET_FUNCTION_ARG_ADVANCE s390_function_arg_advance
18486 #undef TARGET_FUNCTION_ARG_PADDING
18487 #define TARGET_FUNCTION_ARG_PADDING s390_function_arg_padding
18488 #undef TARGET_FUNCTION_VALUE
18489 #define TARGET_FUNCTION_VALUE s390_function_value
18490 #undef TARGET_LIBCALL_VALUE
18491 #define TARGET_LIBCALL_VALUE s390_libcall_value
18492 #undef TARGET_STRICT_ARGUMENT_NAMING
18493 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
18494
18495 #undef TARGET_KEEP_LEAF_WHEN_PROFILED
18496 #define TARGET_KEEP_LEAF_WHEN_PROFILED s390_keep_leaf_when_profiled
18497
18498 #undef TARGET_FIXED_CONDITION_CODE_REGS
18499 #define TARGET_FIXED_CONDITION_CODE_REGS s390_fixed_condition_code_regs
18500
18501 #undef TARGET_CC_MODES_COMPATIBLE
18502 #define TARGET_CC_MODES_COMPATIBLE s390_cc_modes_compatible
18503
18504 #undef TARGET_INVALID_WITHIN_DOLOOP
18505 #define TARGET_INVALID_WITHIN_DOLOOP hook_constcharptr_const_rtx_insn_null
18506
18507 #ifdef HAVE_AS_TLS
18508 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
18509 #define TARGET_ASM_OUTPUT_DWARF_DTPREL s390_output_dwarf_dtprel
18510 #endif
18511
18512 #undef TARGET_DWARF_FRAME_REG_MODE
18513 #define TARGET_DWARF_FRAME_REG_MODE s390_dwarf_frame_reg_mode
18514
18515 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
18516 #undef TARGET_MANGLE_TYPE
18517 #define TARGET_MANGLE_TYPE s390_mangle_type
18518 #endif
18519
18520 #undef TARGET_SCALAR_MODE_SUPPORTED_P
18521 #define TARGET_SCALAR_MODE_SUPPORTED_P s390_scalar_mode_supported_p
18522
18523 #undef TARGET_VECTOR_MODE_SUPPORTED_P
18524 #define TARGET_VECTOR_MODE_SUPPORTED_P s390_vector_mode_supported_p
18525
18526 #undef TARGET_PREFERRED_RELOAD_CLASS
18527 #define TARGET_PREFERRED_RELOAD_CLASS s390_preferred_reload_class
18528
18529 #undef TARGET_SECONDARY_RELOAD
18530 #define TARGET_SECONDARY_RELOAD s390_secondary_reload
18531 #undef TARGET_SECONDARY_MEMORY_NEEDED
18532 #define TARGET_SECONDARY_MEMORY_NEEDED s390_secondary_memory_needed
18533 #undef TARGET_SECONDARY_MEMORY_NEEDED_MODE
18534 #define TARGET_SECONDARY_MEMORY_NEEDED_MODE s390_secondary_memory_needed_mode
18535
18536 #undef TARGET_LIBGCC_CMP_RETURN_MODE
18537 #define TARGET_LIBGCC_CMP_RETURN_MODE s390_libgcc_cmp_return_mode
18538
18539 #undef TARGET_LIBGCC_SHIFT_COUNT_MODE
18540 #define TARGET_LIBGCC_SHIFT_COUNT_MODE s390_libgcc_shift_count_mode
18541
18542 #undef TARGET_LEGITIMATE_ADDRESS_P
18543 #define TARGET_LEGITIMATE_ADDRESS_P s390_legitimate_address_p
18544
18545 #undef TARGET_LEGITIMATE_CONSTANT_P
18546 #define TARGET_LEGITIMATE_CONSTANT_P s390_legitimate_constant_p
18547
18548 #undef TARGET_CAN_ELIMINATE
18549 #define TARGET_CAN_ELIMINATE s390_can_eliminate
18550
18551 #undef TARGET_CONDITIONAL_REGISTER_USAGE
18552 #define TARGET_CONDITIONAL_REGISTER_USAGE s390_conditional_register_usage
18553
18554 #undef TARGET_LOOP_UNROLL_ADJUST
18555 #define TARGET_LOOP_UNROLL_ADJUST s390_loop_unroll_adjust
18556
18557 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
18558 #define TARGET_ASM_TRAMPOLINE_TEMPLATE s390_asm_trampoline_template
18559 #undef TARGET_TRAMPOLINE_INIT
18560 #define TARGET_TRAMPOLINE_INIT s390_trampoline_init
18561
18562 /* PR 79421 */
18563 #undef TARGET_CUSTOM_FUNCTION_DESCRIPTORS
18564 #define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
18565
18566 #undef TARGET_UNWIND_WORD_MODE
18567 #define TARGET_UNWIND_WORD_MODE s390_unwind_word_mode
18568
18569 #undef TARGET_CANONICALIZE_COMPARISON
18570 #define TARGET_CANONICALIZE_COMPARISON s390_canonicalize_comparison
18571
18572 #undef TARGET_HARD_REGNO_SCRATCH_OK
18573 #define TARGET_HARD_REGNO_SCRATCH_OK s390_hard_regno_scratch_ok
18574
18575 #undef TARGET_HARD_REGNO_NREGS
18576 #define TARGET_HARD_REGNO_NREGS s390_hard_regno_nregs
18577 #undef TARGET_HARD_REGNO_MODE_OK
18578 #define TARGET_HARD_REGNO_MODE_OK s390_hard_regno_mode_ok
18579 #undef TARGET_MODES_TIEABLE_P
18580 #define TARGET_MODES_TIEABLE_P s390_modes_tieable_p
18581
18582 #undef TARGET_HARD_REGNO_CALL_PART_CLOBBERED
18583 #define TARGET_HARD_REGNO_CALL_PART_CLOBBERED \
18584 s390_hard_regno_call_part_clobbered
18585
18586 #undef TARGET_ATTRIBUTE_TABLE
18587 #define TARGET_ATTRIBUTE_TABLE s390_attribute_table
18588
18589 #undef TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P
18590 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P hook_bool_const_tree_true
18591
18592 #undef TARGET_SET_UP_BY_PROLOGUE
18593 #define TARGET_SET_UP_BY_PROLOGUE s300_set_up_by_prologue
18594
18595 #undef TARGET_EXTRA_LIVE_ON_ENTRY
18596 #define TARGET_EXTRA_LIVE_ON_ENTRY s390_live_on_entry
18597
18598 #undef TARGET_USE_BY_PIECES_INFRASTRUCTURE_P
18599 #define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \
18600 s390_use_by_pieces_infrastructure_p
18601
18602 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
18603 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV s390_atomic_assign_expand_fenv
18604
18605 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
18606 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN s390_invalid_arg_for_unprototyped_fn
18607
18608 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
18609 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE s390_preferred_simd_mode
18610
18611 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
18612 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT s390_support_vector_misalignment
18613
18614 #undef TARGET_VECTOR_ALIGNMENT
18615 #define TARGET_VECTOR_ALIGNMENT s390_vector_alignment
18616
18617 #undef TARGET_INVALID_BINARY_OP
18618 #define TARGET_INVALID_BINARY_OP s390_invalid_binary_op
18619
18620 #ifdef HAVE_AS_MACHINE_MACHINEMODE
18621 #undef TARGET_ASM_FILE_START
18622 #define TARGET_ASM_FILE_START s390_asm_file_start
18623 #endif
18624
18625 #undef TARGET_ASM_FILE_END
18626 #define TARGET_ASM_FILE_END s390_asm_file_end
18627
18628 #undef TARGET_SET_CURRENT_FUNCTION
18629 #define TARGET_SET_CURRENT_FUNCTION s390_set_current_function
18630
18631 #if S390_USE_TARGET_ATTRIBUTE
18632 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
18633 #define TARGET_OPTION_VALID_ATTRIBUTE_P s390_valid_target_attribute_p
18634
18635 #undef TARGET_CAN_INLINE_P
18636 #define TARGET_CAN_INLINE_P s390_can_inline_p
18637 #endif
18638
18639 #undef TARGET_OPTION_RESTORE
18640 #define TARGET_OPTION_RESTORE s390_function_specific_restore
18641
18642 #undef TARGET_CAN_CHANGE_MODE_CLASS
18643 #define TARGET_CAN_CHANGE_MODE_CLASS s390_can_change_mode_class
18644
18645 #undef TARGET_CONSTANT_ALIGNMENT
18646 #define TARGET_CONSTANT_ALIGNMENT s390_constant_alignment
18647
18648 #undef TARGET_ASM_CODE_END
18649 #define TARGET_ASM_CODE_END s390_code_end
18650
18651 #undef TARGET_CASE_VALUES_THRESHOLD
18652 #define TARGET_CASE_VALUES_THRESHOLD s390_case_values_threshold
18653
18654 #undef TARGET_SCHED_DEPENDENCIES_EVALUATION_HOOK
18655 #define TARGET_SCHED_DEPENDENCIES_EVALUATION_HOOK \
18656 s390_sched_dependencies_evaluation
18657
18658 #undef TARGET_SHIFT_TRUNCATION_MASK
18659 #define TARGET_SHIFT_TRUNCATION_MASK s390_shift_truncation_mask
18660
18661 /* Use only short displacement, since long displacement is not available for
18662 the floating point instructions. */
18663 #undef TARGET_MAX_ANCHOR_OFFSET
18664 #define TARGET_MAX_ANCHOR_OFFSET 0xfff
18665
18666 #undef TARGET_MD_ASM_ADJUST
18667 #define TARGET_MD_ASM_ADJUST s390_md_asm_adjust
18668
18669 #undef TARGET_VECTORIZE_VEC_PERM_CONST
18670 #define TARGET_VECTORIZE_VEC_PERM_CONST s390_vectorize_vec_perm_const
18671
18672 #undef TARGET_NOCE_CONVERSION_PROFITABLE_P
18673 #define TARGET_NOCE_CONVERSION_PROFITABLE_P s390_noce_conversion_profitable_p
18674
18675 #undef TARGET_C_MODE_FOR_FLOATING_TYPE
18676 #define TARGET_C_MODE_FOR_FLOATING_TYPE s390_c_mode_for_floating_type
18677
18678 #undef TARGET_DOCUMENTATION_NAME
18679 #define TARGET_DOCUMENTATION_NAME "S/390"
18680
18681 struct gcc_target targetm = TARGET_INITIALIZER;
18682
18683 #include "gt-s390.h"