S/390: Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV.
[official-gcc.git] / gcc / config / s390 / s390.c
blob1924f2acb68f6243e6c8cb6051f1be227babee24
1 /* Subroutines used for code generation on IBM S/390 and zSeries
2 Copyright (C) 1999-2015 Free Software Foundation, Inc.
3 Contributed by Hartmut Penner (hpenner@de.ibm.com) and
4 Ulrich Weigand (uweigand@de.ibm.com) and
5 Andreas Krebbel (Andreas.Krebbel@de.ibm.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "hash-set.h"
29 #include "machmode.h"
30 #include "vec.h"
31 #include "double-int.h"
32 #include "input.h"
33 #include "alias.h"
34 #include "symtab.h"
35 #include "wide-int.h"
36 #include "inchash.h"
37 #include "tree.h"
38 #include "fold-const.h"
39 #include "print-tree.h"
40 #include "stringpool.h"
41 #include "stor-layout.h"
42 #include "varasm.h"
43 #include "calls.h"
44 #include "tm_p.h"
45 #include "regs.h"
46 #include "hard-reg-set.h"
47 #include "insn-config.h"
48 #include "conditions.h"
49 #include "output.h"
50 #include "insn-attr.h"
51 #include "flags.h"
52 #include "except.h"
53 #include "function.h"
54 #include "recog.h"
55 #include "hashtab.h"
56 #include "statistics.h"
57 #include "real.h"
58 #include "fixed-value.h"
59 #include "expmed.h"
60 #include "dojump.h"
61 #include "explow.h"
62 #include "emit-rtl.h"
63 #include "stmt.h"
64 #include "expr.h"
65 #include "reload.h"
66 #include "diagnostic-core.h"
67 #include "predict.h"
68 #include "dominance.h"
69 #include "cfg.h"
70 #include "cfgrtl.h"
71 #include "cfganal.h"
72 #include "lcm.h"
73 #include "cfgbuild.h"
74 #include "cfgcleanup.h"
75 #include "basic-block.h"
76 #include "ggc.h"
77 #include "target.h"
78 #include "target-def.h"
79 #include "debug.h"
80 #include "langhooks.h"
81 #include "insn-codes.h"
82 #include "optabs.h"
83 #include "hash-table.h"
84 #include "tree-ssa-alias.h"
85 #include "internal-fn.h"
86 #include "gimple-fold.h"
87 #include "tree-eh.h"
88 #include "gimple-expr.h"
89 #include "is-a.h"
90 #include "gimple.h"
91 #include "gimplify.h"
92 #include "df.h"
93 #include "params.h"
94 #include "cfgloop.h"
95 #include "opts.h"
96 #include "tree-pass.h"
97 #include "context.h"
98 #include "builtins.h"
99 #include "rtl-iter.h"
101 /* Define the specific costs for a given cpu. */
103 struct processor_costs
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;
138 const struct processor_costs *s390_cost;
140 static const
141 struct processor_costs z900_cost =
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 */
172 static const
173 struct processor_costs z990_cost =
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 */
204 static const
205 struct processor_costs z9_109_cost =
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 */
236 static const
237 struct processor_costs z10_cost =
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 */
268 static const
269 struct processor_costs z196_cost =
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 */
300 static const
301 struct processor_costs zEC12_cost =
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 */
332 extern int reload_completed;
334 /* Kept up to date using the SCHED_VARIABLE_ISSUE hook. */
335 static rtx_insn *last_scheduled_insn;
337 /* Structure used to hold the components of a S/390 memory
338 address. A legitimate address on S/390 is of the general
339 form
340 base + index + displacement
341 where any of the components is optional.
343 base and index are registers of the class ADDR_REGS,
344 displacement is an unsigned 12-bit immediate constant. */
346 struct s390_address
348 rtx base;
349 rtx indx;
350 rtx disp;
351 bool pointer;
352 bool literal_pool;
355 /* The following structure is embedded in the machine
356 specific part of struct function. */
358 struct GTY (()) s390_frame_layout
360 /* Offset within stack frame. */
361 HOST_WIDE_INT gprs_offset;
362 HOST_WIDE_INT f0_offset;
363 HOST_WIDE_INT f4_offset;
364 HOST_WIDE_INT f8_offset;
365 HOST_WIDE_INT backchain_offset;
367 /* Number of first and last gpr where slots in the register
368 save area are reserved for. */
369 int first_save_gpr_slot;
370 int last_save_gpr_slot;
372 /* Location (FP register number) where GPRs (r0-r15) should
373 be saved to.
374 0 - does not need to be saved at all
375 -1 - stack slot */
376 signed char gpr_save_slots[16];
378 /* Number of first and last gpr to be saved, restored. */
379 int first_save_gpr;
380 int first_restore_gpr;
381 int last_save_gpr;
382 int last_restore_gpr;
384 /* Bits standing for floating point registers. Set, if the
385 respective register has to be saved. Starting with reg 16 (f0)
386 at the rightmost bit.
387 Bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
388 fpr 15 13 11 9 14 12 10 8 7 5 3 1 6 4 2 0
389 reg 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 */
390 unsigned int fpr_bitmap;
392 /* Number of floating point registers f8-f15 which must be saved. */
393 int high_fprs;
395 /* Set if return address needs to be saved.
396 This flag is set by s390_return_addr_rtx if it could not use
397 the initial value of r14 and therefore depends on r14 saved
398 to the stack. */
399 bool save_return_addr_p;
401 /* Size of stack frame. */
402 HOST_WIDE_INT frame_size;
405 /* Define the structure for the machine field in struct function. */
407 struct GTY(()) machine_function
409 struct s390_frame_layout frame_layout;
411 /* Literal pool base register. */
412 rtx base_reg;
414 /* True if we may need to perform branch splitting. */
415 bool split_branches_pending_p;
417 bool has_landing_pad_p;
419 /* True if the current function may contain a tbegin clobbering
420 FPRs. */
421 bool tbegin_p;
424 /* Few accessor macros for struct cfun->machine->s390_frame_layout. */
426 #define cfun_frame_layout (cfun->machine->frame_layout)
427 #define cfun_save_high_fprs_p (!!cfun_frame_layout.high_fprs)
428 #define cfun_save_arg_fprs_p (!!(TARGET_64BIT \
429 ? cfun_frame_layout.fpr_bitmap & 0x0f \
430 : cfun_frame_layout.fpr_bitmap & 0x03))
431 #define cfun_gprs_save_area_size ((cfun_frame_layout.last_save_gpr_slot - \
432 cfun_frame_layout.first_save_gpr_slot + 1) * UNITS_PER_LONG)
433 #define cfun_set_fpr_save(REGNO) (cfun->machine->frame_layout.fpr_bitmap |= \
434 (1 << (REGNO - FPR0_REGNUM)))
435 #define cfun_fpr_save_p(REGNO) (!!(cfun->machine->frame_layout.fpr_bitmap & \
436 (1 << (REGNO - FPR0_REGNUM))))
437 #define cfun_gpr_save_slot(REGNO) \
438 cfun->machine->frame_layout.gpr_save_slots[REGNO]
440 /* Number of GPRs and FPRs used for argument passing. */
441 #define GP_ARG_NUM_REG 5
442 #define FP_ARG_NUM_REG (TARGET_64BIT? 4 : 2)
444 /* A couple of shortcuts. */
445 #define CONST_OK_FOR_J(x) \
446 CONST_OK_FOR_CONSTRAINT_P((x), 'J', "J")
447 #define CONST_OK_FOR_K(x) \
448 CONST_OK_FOR_CONSTRAINT_P((x), 'K', "K")
449 #define CONST_OK_FOR_Os(x) \
450 CONST_OK_FOR_CONSTRAINT_P((x), 'O', "Os")
451 #define CONST_OK_FOR_Op(x) \
452 CONST_OK_FOR_CONSTRAINT_P((x), 'O', "Op")
453 #define CONST_OK_FOR_On(x) \
454 CONST_OK_FOR_CONSTRAINT_P((x), 'O', "On")
456 #define REGNO_PAIR_OK(REGNO, MODE) \
457 (HARD_REGNO_NREGS ((REGNO), (MODE)) == 1 || !((REGNO) & 1))
459 /* That's the read ahead of the dynamic branch prediction unit in
460 bytes on a z10 (or higher) CPU. */
461 #define PREDICT_DISTANCE (TARGET_Z10 ? 384 : 2048)
463 static const int s390_hotpatch_hw_max = 1000000;
464 static int s390_hotpatch_hw_before_label = 0;
465 static int s390_hotpatch_hw_after_label = 0;
467 /* Check whether the hotpatch attribute is applied to a function and, if it has
468 an argument, the argument is valid. */
470 static tree
471 s390_handle_hotpatch_attribute (tree *node, tree name, tree args,
472 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
474 tree expr;
475 tree expr2;
476 int err;
478 if (TREE_CODE (*node) != FUNCTION_DECL)
480 warning (OPT_Wattributes, "%qE attribute only applies to functions",
481 name);
482 *no_add_attrs = true;
484 if (args != NULL && TREE_CHAIN (args) != NULL)
486 expr = TREE_VALUE (args);
487 expr2 = TREE_VALUE (TREE_CHAIN (args));
489 if (args == NULL || TREE_CHAIN (args) == NULL)
490 err = 1;
491 else if (TREE_CODE (expr) != INTEGER_CST
492 || !INTEGRAL_TYPE_P (TREE_TYPE (expr))
493 || wi::gtu_p (expr, s390_hotpatch_hw_max))
494 err = 1;
495 else if (TREE_CODE (expr2) != INTEGER_CST
496 || !INTEGRAL_TYPE_P (TREE_TYPE (expr2))
497 || wi::gtu_p (expr2, s390_hotpatch_hw_max))
498 err = 1;
499 else
500 err = 0;
501 if (err)
503 error ("requested %qE attribute is not a comma separated pair of"
504 " non-negative integer constants or too large (max. %d)", name,
505 s390_hotpatch_hw_max);
506 *no_add_attrs = true;
509 return NULL_TREE;
512 static const struct attribute_spec s390_attribute_table[] = {
513 { "hotpatch", 2, 2, true, false, false, s390_handle_hotpatch_attribute, false
515 /* End element. */
516 { NULL, 0, 0, false, false, false, NULL, false }
519 /* Return the alignment for LABEL. We default to the -falign-labels
520 value except for the literal pool base label. */
522 s390_label_align (rtx label)
524 rtx_insn *prev_insn = prev_active_insn (label);
525 rtx set, src;
527 if (prev_insn == NULL_RTX)
528 goto old;
530 set = single_set (prev_insn);
532 if (set == NULL_RTX)
533 goto old;
535 src = SET_SRC (set);
537 /* Don't align literal pool base labels. */
538 if (GET_CODE (src) == UNSPEC
539 && XINT (src, 1) == UNSPEC_MAIN_BASE)
540 return 0;
542 old:
543 return align_labels_log;
546 static machine_mode
547 s390_libgcc_cmp_return_mode (void)
549 return TARGET_64BIT ? DImode : SImode;
552 static machine_mode
553 s390_libgcc_shift_count_mode (void)
555 return TARGET_64BIT ? DImode : SImode;
558 static machine_mode
559 s390_unwind_word_mode (void)
561 return TARGET_64BIT ? DImode : SImode;
564 /* Return true if the back end supports mode MODE. */
565 static bool
566 s390_scalar_mode_supported_p (machine_mode mode)
568 /* In contrast to the default implementation reject TImode constants on 31bit
569 TARGET_ZARCH for ABI compliance. */
570 if (!TARGET_64BIT && TARGET_ZARCH && mode == TImode)
571 return false;
573 if (DECIMAL_FLOAT_MODE_P (mode))
574 return default_decimal_float_supported_p ();
576 return default_scalar_mode_supported_p (mode);
579 /* Set the has_landing_pad_p flag in struct machine_function to VALUE. */
581 void
582 s390_set_has_landing_pad_p (bool value)
584 cfun->machine->has_landing_pad_p = value;
587 /* If two condition code modes are compatible, return a condition code
588 mode which is compatible with both. Otherwise, return
589 VOIDmode. */
591 static machine_mode
592 s390_cc_modes_compatible (machine_mode m1, machine_mode m2)
594 if (m1 == m2)
595 return m1;
597 switch (m1)
599 case CCZmode:
600 if (m2 == CCUmode || m2 == CCTmode || m2 == CCZ1mode
601 || m2 == CCSmode || m2 == CCSRmode || m2 == CCURmode)
602 return m2;
603 return VOIDmode;
605 case CCSmode:
606 case CCUmode:
607 case CCTmode:
608 case CCSRmode:
609 case CCURmode:
610 case CCZ1mode:
611 if (m2 == CCZmode)
612 return m1;
614 return VOIDmode;
616 default:
617 return VOIDmode;
619 return VOIDmode;
622 /* Return true if SET either doesn't set the CC register, or else
623 the source and destination have matching CC modes and that
624 CC mode is at least as constrained as REQ_MODE. */
626 static bool
627 s390_match_ccmode_set (rtx set, machine_mode req_mode)
629 machine_mode set_mode;
631 gcc_assert (GET_CODE (set) == SET);
633 if (GET_CODE (SET_DEST (set)) != REG || !CC_REGNO_P (REGNO (SET_DEST (set))))
634 return 1;
636 set_mode = GET_MODE (SET_DEST (set));
637 switch (set_mode)
639 case CCSmode:
640 case CCSRmode:
641 case CCUmode:
642 case CCURmode:
643 case CCLmode:
644 case CCL1mode:
645 case CCL2mode:
646 case CCL3mode:
647 case CCT1mode:
648 case CCT2mode:
649 case CCT3mode:
650 if (req_mode != set_mode)
651 return 0;
652 break;
654 case CCZmode:
655 if (req_mode != CCSmode && req_mode != CCUmode && req_mode != CCTmode
656 && req_mode != CCSRmode && req_mode != CCURmode)
657 return 0;
658 break;
660 case CCAPmode:
661 case CCANmode:
662 if (req_mode != CCAmode)
663 return 0;
664 break;
666 default:
667 gcc_unreachable ();
670 return (GET_MODE (SET_SRC (set)) == set_mode);
673 /* Return true if every SET in INSN that sets the CC register
674 has source and destination with matching CC modes and that
675 CC mode is at least as constrained as REQ_MODE.
676 If REQ_MODE is VOIDmode, always return false. */
678 bool
679 s390_match_ccmode (rtx_insn *insn, machine_mode req_mode)
681 int i;
683 /* s390_tm_ccmode returns VOIDmode to indicate failure. */
684 if (req_mode == VOIDmode)
685 return false;
687 if (GET_CODE (PATTERN (insn)) == SET)
688 return s390_match_ccmode_set (PATTERN (insn), req_mode);
690 if (GET_CODE (PATTERN (insn)) == PARALLEL)
691 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
693 rtx set = XVECEXP (PATTERN (insn), 0, i);
694 if (GET_CODE (set) == SET)
695 if (!s390_match_ccmode_set (set, req_mode))
696 return false;
699 return true;
702 /* If a test-under-mask instruction can be used to implement
703 (compare (and ... OP1) OP2), return the CC mode required
704 to do that. Otherwise, return VOIDmode.
705 MIXED is true if the instruction can distinguish between
706 CC1 and CC2 for mixed selected bits (TMxx), it is false
707 if the instruction cannot (TM). */
709 machine_mode
710 s390_tm_ccmode (rtx op1, rtx op2, bool mixed)
712 int bit0, bit1;
714 /* ??? Fixme: should work on CONST_DOUBLE as well. */
715 if (GET_CODE (op1) != CONST_INT || GET_CODE (op2) != CONST_INT)
716 return VOIDmode;
718 /* Selected bits all zero: CC0.
719 e.g.: int a; if ((a & (16 + 128)) == 0) */
720 if (INTVAL (op2) == 0)
721 return CCTmode;
723 /* Selected bits all one: CC3.
724 e.g.: int a; if ((a & (16 + 128)) == 16 + 128) */
725 if (INTVAL (op2) == INTVAL (op1))
726 return CCT3mode;
728 /* Exactly two bits selected, mixed zeroes and ones: CC1 or CC2. e.g.:
729 int a;
730 if ((a & (16 + 128)) == 16) -> CCT1
731 if ((a & (16 + 128)) == 128) -> CCT2 */
732 if (mixed)
734 bit1 = exact_log2 (INTVAL (op2));
735 bit0 = exact_log2 (INTVAL (op1) ^ INTVAL (op2));
736 if (bit0 != -1 && bit1 != -1)
737 return bit0 > bit1 ? CCT1mode : CCT2mode;
740 return VOIDmode;
743 /* Given a comparison code OP (EQ, NE, etc.) and the operands
744 OP0 and OP1 of a COMPARE, return the mode to be used for the
745 comparison. */
747 machine_mode
748 s390_select_ccmode (enum rtx_code code, rtx op0, rtx op1)
750 switch (code)
752 case EQ:
753 case NE:
754 if ((GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
755 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
756 return CCAPmode;
757 if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
758 && CONST_OK_FOR_K (INTVAL (XEXP (op0, 1))))
759 return CCAPmode;
760 if ((GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
761 || GET_CODE (op1) == NEG)
762 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
763 return CCLmode;
765 if (GET_CODE (op0) == AND)
767 /* Check whether we can potentially do it via TM. */
768 machine_mode ccmode;
769 ccmode = s390_tm_ccmode (XEXP (op0, 1), op1, 1);
770 if (ccmode != VOIDmode)
772 /* Relax CCTmode to CCZmode to allow fall-back to AND
773 if that turns out to be beneficial. */
774 return ccmode == CCTmode ? CCZmode : ccmode;
778 if (register_operand (op0, HImode)
779 && GET_CODE (op1) == CONST_INT
780 && (INTVAL (op1) == -1 || INTVAL (op1) == 65535))
781 return CCT3mode;
782 if (register_operand (op0, QImode)
783 && GET_CODE (op1) == CONST_INT
784 && (INTVAL (op1) == -1 || INTVAL (op1) == 255))
785 return CCT3mode;
787 return CCZmode;
789 case LE:
790 case LT:
791 case GE:
792 case GT:
793 /* The only overflow condition of NEG and ABS happens when
794 -INT_MAX is used as parameter, which stays negative. So
795 we have an overflow from a positive value to a negative.
796 Using CCAP mode the resulting cc can be used for comparisons. */
797 if ((GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
798 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
799 return CCAPmode;
801 /* If constants are involved in an add instruction it is possible to use
802 the resulting cc for comparisons with zero. Knowing the sign of the
803 constant the overflow behavior gets predictable. e.g.:
804 int a, b; if ((b = a + c) > 0)
805 with c as a constant value: c < 0 -> CCAN and c >= 0 -> CCAP */
806 if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
807 && (CONST_OK_FOR_K (INTVAL (XEXP (op0, 1)))
808 || (CONST_OK_FOR_CONSTRAINT_P (INTVAL (XEXP (op0, 1)), 'O', "Os")
809 /* Avoid INT32_MIN on 32 bit. */
810 && (!TARGET_ZARCH || INTVAL (XEXP (op0, 1)) != -0x7fffffff - 1))))
812 if (INTVAL (XEXP((op0), 1)) < 0)
813 return CCANmode;
814 else
815 return CCAPmode;
817 /* Fall through. */
818 case UNORDERED:
819 case ORDERED:
820 case UNEQ:
821 case UNLE:
822 case UNLT:
823 case UNGE:
824 case UNGT:
825 case LTGT:
826 if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
827 && GET_CODE (op1) != CONST_INT)
828 return CCSRmode;
829 return CCSmode;
831 case LTU:
832 case GEU:
833 if (GET_CODE (op0) == PLUS
834 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
835 return CCL1mode;
837 if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
838 && GET_CODE (op1) != CONST_INT)
839 return CCURmode;
840 return CCUmode;
842 case LEU:
843 case GTU:
844 if (GET_CODE (op0) == MINUS
845 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
846 return CCL2mode;
848 if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
849 && GET_CODE (op1) != CONST_INT)
850 return CCURmode;
851 return CCUmode;
853 default:
854 gcc_unreachable ();
858 /* Replace the comparison OP0 CODE OP1 by a semantically equivalent one
859 that we can implement more efficiently. */
861 static void
862 s390_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
863 bool op0_preserve_value)
865 if (op0_preserve_value)
866 return;
868 /* Convert ZERO_EXTRACT back to AND to enable TM patterns. */
869 if ((*code == EQ || *code == NE)
870 && *op1 == const0_rtx
871 && GET_CODE (*op0) == ZERO_EXTRACT
872 && GET_CODE (XEXP (*op0, 1)) == CONST_INT
873 && GET_CODE (XEXP (*op0, 2)) == CONST_INT
874 && SCALAR_INT_MODE_P (GET_MODE (XEXP (*op0, 0))))
876 rtx inner = XEXP (*op0, 0);
877 HOST_WIDE_INT modesize = GET_MODE_BITSIZE (GET_MODE (inner));
878 HOST_WIDE_INT len = INTVAL (XEXP (*op0, 1));
879 HOST_WIDE_INT pos = INTVAL (XEXP (*op0, 2));
881 if (len > 0 && len < modesize
882 && pos >= 0 && pos + len <= modesize
883 && modesize <= HOST_BITS_PER_WIDE_INT)
885 unsigned HOST_WIDE_INT block;
886 block = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
887 block <<= modesize - pos - len;
889 *op0 = gen_rtx_AND (GET_MODE (inner), inner,
890 gen_int_mode (block, GET_MODE (inner)));
894 /* Narrow AND of memory against immediate to enable TM. */
895 if ((*code == EQ || *code == NE)
896 && *op1 == const0_rtx
897 && GET_CODE (*op0) == AND
898 && GET_CODE (XEXP (*op0, 1)) == CONST_INT
899 && SCALAR_INT_MODE_P (GET_MODE (XEXP (*op0, 0))))
901 rtx inner = XEXP (*op0, 0);
902 rtx mask = XEXP (*op0, 1);
904 /* Ignore paradoxical SUBREGs if all extra bits are masked out. */
905 if (GET_CODE (inner) == SUBREG
906 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (inner)))
907 && (GET_MODE_SIZE (GET_MODE (inner))
908 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner))))
909 && ((INTVAL (mask)
910 & GET_MODE_MASK (GET_MODE (inner))
911 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (inner))))
912 == 0))
913 inner = SUBREG_REG (inner);
915 /* Do not change volatile MEMs. */
916 if (MEM_P (inner) && !MEM_VOLATILE_P (inner))
918 int part = s390_single_part (XEXP (*op0, 1),
919 GET_MODE (inner), QImode, 0);
920 if (part >= 0)
922 mask = gen_int_mode (s390_extract_part (mask, QImode, 0), QImode);
923 inner = adjust_address_nv (inner, QImode, part);
924 *op0 = gen_rtx_AND (QImode, inner, mask);
929 /* Narrow comparisons against 0xffff to HImode if possible. */
930 if ((*code == EQ || *code == NE)
931 && GET_CODE (*op1) == CONST_INT
932 && INTVAL (*op1) == 0xffff
933 && SCALAR_INT_MODE_P (GET_MODE (*op0))
934 && (nonzero_bits (*op0, GET_MODE (*op0))
935 & ~(unsigned HOST_WIDE_INT) 0xffff) == 0)
937 *op0 = gen_lowpart (HImode, *op0);
938 *op1 = constm1_rtx;
941 /* Remove redundant UNSPEC_STRCMPCC_TO_INT conversions if possible. */
942 if (GET_CODE (*op0) == UNSPEC
943 && XINT (*op0, 1) == UNSPEC_STRCMPCC_TO_INT
944 && XVECLEN (*op0, 0) == 1
945 && GET_MODE (XVECEXP (*op0, 0, 0)) == CCUmode
946 && GET_CODE (XVECEXP (*op0, 0, 0)) == REG
947 && REGNO (XVECEXP (*op0, 0, 0)) == CC_REGNUM
948 && *op1 == const0_rtx)
950 enum rtx_code new_code = UNKNOWN;
951 switch (*code)
953 case EQ: new_code = EQ; break;
954 case NE: new_code = NE; break;
955 case LT: new_code = GTU; break;
956 case GT: new_code = LTU; break;
957 case LE: new_code = GEU; break;
958 case GE: new_code = LEU; break;
959 default: break;
962 if (new_code != UNKNOWN)
964 *op0 = XVECEXP (*op0, 0, 0);
965 *code = new_code;
969 /* Remove redundant UNSPEC_CC_TO_INT conversions if possible. */
970 if (GET_CODE (*op0) == UNSPEC
971 && XINT (*op0, 1) == UNSPEC_CC_TO_INT
972 && XVECLEN (*op0, 0) == 1
973 && GET_CODE (XVECEXP (*op0, 0, 0)) == REG
974 && REGNO (XVECEXP (*op0, 0, 0)) == CC_REGNUM
975 && CONST_INT_P (*op1))
977 enum rtx_code new_code = UNKNOWN;
978 switch (GET_MODE (XVECEXP (*op0, 0, 0)))
980 case CCZmode:
981 case CCRAWmode:
982 switch (*code)
984 case EQ: new_code = EQ; break;
985 case NE: new_code = NE; break;
986 default: break;
988 break;
989 default: break;
992 if (new_code != UNKNOWN)
994 /* For CCRAWmode put the required cc mask into the second
995 operand. */
996 if (GET_MODE (XVECEXP (*op0, 0, 0)) == CCRAWmode
997 && INTVAL (*op1) >= 0 && INTVAL (*op1) <= 3)
998 *op1 = gen_rtx_CONST_INT (VOIDmode, 1 << (3 - INTVAL (*op1)));
999 *op0 = XVECEXP (*op0, 0, 0);
1000 *code = new_code;
1004 /* Simplify cascaded EQ, NE with const0_rtx. */
1005 if ((*code == NE || *code == EQ)
1006 && (GET_CODE (*op0) == EQ || GET_CODE (*op0) == NE)
1007 && GET_MODE (*op0) == SImode
1008 && GET_MODE (XEXP (*op0, 0)) == CCZ1mode
1009 && REG_P (XEXP (*op0, 0))
1010 && XEXP (*op0, 1) == const0_rtx
1011 && *op1 == const0_rtx)
1013 if ((*code == EQ && GET_CODE (*op0) == NE)
1014 || (*code == NE && GET_CODE (*op0) == EQ))
1015 *code = EQ;
1016 else
1017 *code = NE;
1018 *op0 = XEXP (*op0, 0);
1021 /* Prefer register over memory as first operand. */
1022 if (MEM_P (*op0) && REG_P (*op1))
1024 rtx tem = *op0; *op0 = *op1; *op1 = tem;
1025 *code = (int)swap_condition ((enum rtx_code)*code);
1029 /* Emit a compare instruction suitable to implement the comparison
1030 OP0 CODE OP1. Return the correct condition RTL to be placed in
1031 the IF_THEN_ELSE of the conditional branch testing the result. */
1034 s390_emit_compare (enum rtx_code code, rtx op0, rtx op1)
1036 machine_mode mode = s390_select_ccmode (code, op0, op1);
1037 rtx cc;
1039 /* Do not output a redundant compare instruction if a compare_and_swap
1040 pattern already computed the result and the machine modes are compatible. */
1041 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
1043 gcc_assert (s390_cc_modes_compatible (GET_MODE (op0), mode)
1044 == GET_MODE (op0));
1045 cc = op0;
1047 else
1049 cc = gen_rtx_REG (mode, CC_REGNUM);
1050 emit_insn (gen_rtx_SET (VOIDmode, cc, gen_rtx_COMPARE (mode, op0, op1)));
1053 return gen_rtx_fmt_ee (code, VOIDmode, cc, const0_rtx);
1056 /* Emit a SImode compare and swap instruction setting MEM to NEW_RTX if OLD
1057 matches CMP.
1058 Return the correct condition RTL to be placed in the IF_THEN_ELSE of the
1059 conditional branch testing the result. */
1061 static rtx
1062 s390_emit_compare_and_swap (enum rtx_code code, rtx old, rtx mem,
1063 rtx cmp, rtx new_rtx)
1065 emit_insn (gen_atomic_compare_and_swapsi_internal (old, mem, cmp, new_rtx));
1066 return s390_emit_compare (code, gen_rtx_REG (CCZ1mode, CC_REGNUM),
1067 const0_rtx);
1070 /* Emit a jump instruction to TARGET and return it. If COND is
1071 NULL_RTX, emit an unconditional jump, else a conditional jump under
1072 condition COND. */
1074 rtx_insn *
1075 s390_emit_jump (rtx target, rtx cond)
1077 rtx insn;
1079 target = gen_rtx_LABEL_REF (VOIDmode, target);
1080 if (cond)
1081 target = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, target, pc_rtx);
1083 insn = gen_rtx_SET (VOIDmode, pc_rtx, target);
1084 return emit_jump_insn (insn);
1087 /* Return branch condition mask to implement a branch
1088 specified by CODE. Return -1 for invalid comparisons. */
1091 s390_branch_condition_mask (rtx code)
1093 const int CC0 = 1 << 3;
1094 const int CC1 = 1 << 2;
1095 const int CC2 = 1 << 1;
1096 const int CC3 = 1 << 0;
1098 gcc_assert (GET_CODE (XEXP (code, 0)) == REG);
1099 gcc_assert (REGNO (XEXP (code, 0)) == CC_REGNUM);
1100 gcc_assert (XEXP (code, 1) == const0_rtx
1101 || (GET_MODE (XEXP (code, 0)) == CCRAWmode
1102 && CONST_INT_P (XEXP (code, 1))));
1105 switch (GET_MODE (XEXP (code, 0)))
1107 case CCZmode:
1108 case CCZ1mode:
1109 switch (GET_CODE (code))
1111 case EQ: return CC0;
1112 case NE: return CC1 | CC2 | CC3;
1113 default: return -1;
1115 break;
1117 case CCT1mode:
1118 switch (GET_CODE (code))
1120 case EQ: return CC1;
1121 case NE: return CC0 | CC2 | CC3;
1122 default: return -1;
1124 break;
1126 case CCT2mode:
1127 switch (GET_CODE (code))
1129 case EQ: return CC2;
1130 case NE: return CC0 | CC1 | CC3;
1131 default: return -1;
1133 break;
1135 case CCT3mode:
1136 switch (GET_CODE (code))
1138 case EQ: return CC3;
1139 case NE: return CC0 | CC1 | CC2;
1140 default: return -1;
1142 break;
1144 case CCLmode:
1145 switch (GET_CODE (code))
1147 case EQ: return CC0 | CC2;
1148 case NE: return CC1 | CC3;
1149 default: return -1;
1151 break;
1153 case CCL1mode:
1154 switch (GET_CODE (code))
1156 case LTU: return CC2 | CC3; /* carry */
1157 case GEU: return CC0 | CC1; /* no carry */
1158 default: return -1;
1160 break;
1162 case CCL2mode:
1163 switch (GET_CODE (code))
1165 case GTU: return CC0 | CC1; /* borrow */
1166 case LEU: return CC2 | CC3; /* no borrow */
1167 default: return -1;
1169 break;
1171 case CCL3mode:
1172 switch (GET_CODE (code))
1174 case EQ: return CC0 | CC2;
1175 case NE: return CC1 | CC3;
1176 case LTU: return CC1;
1177 case GTU: return CC3;
1178 case LEU: return CC1 | CC2;
1179 case GEU: return CC2 | CC3;
1180 default: return -1;
1183 case CCUmode:
1184 switch (GET_CODE (code))
1186 case EQ: return CC0;
1187 case NE: return CC1 | CC2 | CC3;
1188 case LTU: return CC1;
1189 case GTU: return CC2;
1190 case LEU: return CC0 | CC1;
1191 case GEU: return CC0 | CC2;
1192 default: return -1;
1194 break;
1196 case CCURmode:
1197 switch (GET_CODE (code))
1199 case EQ: return CC0;
1200 case NE: return CC2 | CC1 | CC3;
1201 case LTU: return CC2;
1202 case GTU: return CC1;
1203 case LEU: return CC0 | CC2;
1204 case GEU: return CC0 | CC1;
1205 default: return -1;
1207 break;
1209 case CCAPmode:
1210 switch (GET_CODE (code))
1212 case EQ: return CC0;
1213 case NE: return CC1 | CC2 | CC3;
1214 case LT: return CC1 | CC3;
1215 case GT: return CC2;
1216 case LE: return CC0 | CC1 | CC3;
1217 case GE: return CC0 | CC2;
1218 default: return -1;
1220 break;
1222 case CCANmode:
1223 switch (GET_CODE (code))
1225 case EQ: return CC0;
1226 case NE: return CC1 | CC2 | CC3;
1227 case LT: return CC1;
1228 case GT: return CC2 | CC3;
1229 case LE: return CC0 | CC1;
1230 case GE: return CC0 | CC2 | CC3;
1231 default: return -1;
1233 break;
1235 case CCSmode:
1236 switch (GET_CODE (code))
1238 case EQ: return CC0;
1239 case NE: return CC1 | CC2 | CC3;
1240 case LT: return CC1;
1241 case GT: return CC2;
1242 case LE: return CC0 | CC1;
1243 case GE: return CC0 | CC2;
1244 case UNORDERED: return CC3;
1245 case ORDERED: return CC0 | CC1 | CC2;
1246 case UNEQ: return CC0 | CC3;
1247 case UNLT: return CC1 | CC3;
1248 case UNGT: return CC2 | CC3;
1249 case UNLE: return CC0 | CC1 | CC3;
1250 case UNGE: return CC0 | CC2 | CC3;
1251 case LTGT: return CC1 | CC2;
1252 default: return -1;
1254 break;
1256 case CCSRmode:
1257 switch (GET_CODE (code))
1259 case EQ: return CC0;
1260 case NE: return CC2 | CC1 | CC3;
1261 case LT: return CC2;
1262 case GT: return CC1;
1263 case LE: return CC0 | CC2;
1264 case GE: return CC0 | CC1;
1265 case UNORDERED: return CC3;
1266 case ORDERED: return CC0 | CC2 | CC1;
1267 case UNEQ: return CC0 | CC3;
1268 case UNLT: return CC2 | CC3;
1269 case UNGT: return CC1 | CC3;
1270 case UNLE: return CC0 | CC2 | CC3;
1271 case UNGE: return CC0 | CC1 | CC3;
1272 case LTGT: return CC2 | CC1;
1273 default: return -1;
1275 break;
1277 case CCRAWmode:
1278 switch (GET_CODE (code))
1280 case EQ:
1281 return INTVAL (XEXP (code, 1));
1282 case NE:
1283 return (INTVAL (XEXP (code, 1))) ^ 0xf;
1284 default:
1285 gcc_unreachable ();
1288 default:
1289 return -1;
1294 /* Return branch condition mask to implement a compare and branch
1295 specified by CODE. Return -1 for invalid comparisons. */
1298 s390_compare_and_branch_condition_mask (rtx code)
1300 const int CC0 = 1 << 3;
1301 const int CC1 = 1 << 2;
1302 const int CC2 = 1 << 1;
1304 switch (GET_CODE (code))
1306 case EQ:
1307 return CC0;
1308 case NE:
1309 return CC1 | CC2;
1310 case LT:
1311 case LTU:
1312 return CC1;
1313 case GT:
1314 case GTU:
1315 return CC2;
1316 case LE:
1317 case LEU:
1318 return CC0 | CC1;
1319 case GE:
1320 case GEU:
1321 return CC0 | CC2;
1322 default:
1323 gcc_unreachable ();
1325 return -1;
1328 /* If INV is false, return assembler mnemonic string to implement
1329 a branch specified by CODE. If INV is true, return mnemonic
1330 for the corresponding inverted branch. */
1332 static const char *
1333 s390_branch_condition_mnemonic (rtx code, int inv)
1335 int mask;
1337 static const char *const mnemonic[16] =
1339 NULL, "o", "h", "nle",
1340 "l", "nhe", "lh", "ne",
1341 "e", "nlh", "he", "nl",
1342 "le", "nh", "no", NULL
1345 if (GET_CODE (XEXP (code, 0)) == REG
1346 && REGNO (XEXP (code, 0)) == CC_REGNUM
1347 && (XEXP (code, 1) == const0_rtx
1348 || (GET_MODE (XEXP (code, 0)) == CCRAWmode
1349 && CONST_INT_P (XEXP (code, 1)))))
1350 mask = s390_branch_condition_mask (code);
1351 else
1352 mask = s390_compare_and_branch_condition_mask (code);
1354 gcc_assert (mask >= 0);
1356 if (inv)
1357 mask ^= 15;
1359 gcc_assert (mask >= 1 && mask <= 14);
1361 return mnemonic[mask];
1364 /* Return the part of op which has a value different from def.
1365 The size of the part is determined by mode.
1366 Use this function only if you already know that op really
1367 contains such a part. */
1369 unsigned HOST_WIDE_INT
1370 s390_extract_part (rtx op, machine_mode mode, int def)
1372 unsigned HOST_WIDE_INT value = 0;
1373 int max_parts = HOST_BITS_PER_WIDE_INT / GET_MODE_BITSIZE (mode);
1374 int part_bits = GET_MODE_BITSIZE (mode);
1375 unsigned HOST_WIDE_INT part_mask
1376 = ((unsigned HOST_WIDE_INT)1 << part_bits) - 1;
1377 int i;
1379 for (i = 0; i < max_parts; i++)
1381 if (i == 0)
1382 value = (unsigned HOST_WIDE_INT) INTVAL (op);
1383 else
1384 value >>= part_bits;
1386 if ((value & part_mask) != (def & part_mask))
1387 return value & part_mask;
1390 gcc_unreachable ();
1393 /* If OP is an integer constant of mode MODE with exactly one
1394 part of mode PART_MODE unequal to DEF, return the number of that
1395 part. Otherwise, return -1. */
1398 s390_single_part (rtx op,
1399 machine_mode mode,
1400 machine_mode part_mode,
1401 int def)
1403 unsigned HOST_WIDE_INT value = 0;
1404 int n_parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (part_mode);
1405 unsigned HOST_WIDE_INT part_mask
1406 = ((unsigned HOST_WIDE_INT)1 << GET_MODE_BITSIZE (part_mode)) - 1;
1407 int i, part = -1;
1409 if (GET_CODE (op) != CONST_INT)
1410 return -1;
1412 for (i = 0; i < n_parts; i++)
1414 if (i == 0)
1415 value = (unsigned HOST_WIDE_INT) INTVAL (op);
1416 else
1417 value >>= GET_MODE_BITSIZE (part_mode);
1419 if ((value & part_mask) != (def & part_mask))
1421 if (part != -1)
1422 return -1;
1423 else
1424 part = i;
1427 return part == -1 ? -1 : n_parts - 1 - part;
1430 /* Return true if IN contains a contiguous bitfield in the lower SIZE
1431 bits and no other bits are set in IN. POS and LENGTH can be used
1432 to obtain the start position and the length of the bitfield.
1434 POS gives the position of the first bit of the bitfield counting
1435 from the lowest order bit starting with zero. In order to use this
1436 value for S/390 instructions this has to be converted to "bits big
1437 endian" style. */
1439 bool
1440 s390_contiguous_bitmask_p (unsigned HOST_WIDE_INT in, int size,
1441 int *pos, int *length)
1443 int tmp_pos = 0;
1444 int tmp_length = 0;
1445 int i;
1446 unsigned HOST_WIDE_INT mask = 1ULL;
1447 bool contiguous = false;
1449 for (i = 0; i < size; mask <<= 1, i++)
1451 if (contiguous)
1453 if (mask & in)
1454 tmp_length++;
1455 else
1456 break;
1458 else
1460 if (mask & in)
1462 contiguous = true;
1463 tmp_length++;
1465 else
1466 tmp_pos++;
1470 if (!tmp_length)
1471 return false;
1473 /* Calculate a mask for all bits beyond the contiguous bits. */
1474 mask = (-1LL & ~(((1ULL << (tmp_length + tmp_pos - 1)) << 1) - 1));
1476 if (mask & in)
1477 return false;
1479 if (tmp_length + tmp_pos - 1 > size)
1480 return false;
1482 if (length)
1483 *length = tmp_length;
1485 if (pos)
1486 *pos = tmp_pos;
1488 return true;
1491 /* Check whether a rotate of ROTL followed by an AND of CONTIG is
1492 equivalent to a shift followed by the AND. In particular, CONTIG
1493 should not overlap the (rotated) bit 0/bit 63 gap. Negative values
1494 for ROTL indicate a rotate to the right. */
1496 bool
1497 s390_extzv_shift_ok (int bitsize, int rotl, unsigned HOST_WIDE_INT contig)
1499 int pos, len;
1500 bool ok;
1502 ok = s390_contiguous_bitmask_p (contig, bitsize, &pos, &len);
1503 gcc_assert (ok);
1505 return ((rotl >= 0 && rotl <= pos)
1506 || (rotl < 0 && -rotl <= bitsize - len - pos));
1509 /* Check whether we can (and want to) split a double-word
1510 move in mode MODE from SRC to DST into two single-word
1511 moves, moving the subword FIRST_SUBWORD first. */
1513 bool
1514 s390_split_ok_p (rtx dst, rtx src, machine_mode mode, int first_subword)
1516 /* Floating point registers cannot be split. */
1517 if (FP_REG_P (src) || FP_REG_P (dst))
1518 return false;
1520 /* We don't need to split if operands are directly accessible. */
1521 if (s_operand (src, mode) || s_operand (dst, mode))
1522 return false;
1524 /* Non-offsettable memory references cannot be split. */
1525 if ((GET_CODE (src) == MEM && !offsettable_memref_p (src))
1526 || (GET_CODE (dst) == MEM && !offsettable_memref_p (dst)))
1527 return false;
1529 /* Moving the first subword must not clobber a register
1530 needed to move the second subword. */
1531 if (register_operand (dst, mode))
1533 rtx subreg = operand_subword (dst, first_subword, 0, mode);
1534 if (reg_overlap_mentioned_p (subreg, src))
1535 return false;
1538 return true;
1541 /* Return true if it can be proven that [MEM1, MEM1 + SIZE]
1542 and [MEM2, MEM2 + SIZE] do overlap and false
1543 otherwise. */
1545 bool
1546 s390_overlap_p (rtx mem1, rtx mem2, HOST_WIDE_INT size)
1548 rtx addr1, addr2, addr_delta;
1549 HOST_WIDE_INT delta;
1551 if (GET_CODE (mem1) != MEM || GET_CODE (mem2) != MEM)
1552 return true;
1554 if (size == 0)
1555 return false;
1557 addr1 = XEXP (mem1, 0);
1558 addr2 = XEXP (mem2, 0);
1560 addr_delta = simplify_binary_operation (MINUS, Pmode, addr2, addr1);
1562 /* This overlapping check is used by peepholes merging memory block operations.
1563 Overlapping operations would otherwise be recognized by the S/390 hardware
1564 and would fall back to a slower implementation. Allowing overlapping
1565 operations would lead to slow code but not to wrong code. Therefore we are
1566 somewhat optimistic if we cannot prove that the memory blocks are
1567 overlapping.
1568 That's why we return false here although this may accept operations on
1569 overlapping memory areas. */
1570 if (!addr_delta || GET_CODE (addr_delta) != CONST_INT)
1571 return false;
1573 delta = INTVAL (addr_delta);
1575 if (delta == 0
1576 || (delta > 0 && delta < size)
1577 || (delta < 0 && -delta < size))
1578 return true;
1580 return false;
1583 /* Check whether the address of memory reference MEM2 equals exactly
1584 the address of memory reference MEM1 plus DELTA. Return true if
1585 we can prove this to be the case, false otherwise. */
1587 bool
1588 s390_offset_p (rtx mem1, rtx mem2, rtx delta)
1590 rtx addr1, addr2, addr_delta;
1592 if (GET_CODE (mem1) != MEM || GET_CODE (mem2) != MEM)
1593 return false;
1595 addr1 = XEXP (mem1, 0);
1596 addr2 = XEXP (mem2, 0);
1598 addr_delta = simplify_binary_operation (MINUS, Pmode, addr2, addr1);
1599 if (!addr_delta || !rtx_equal_p (addr_delta, delta))
1600 return false;
1602 return true;
1605 /* Expand logical operator CODE in mode MODE with operands OPERANDS. */
1607 void
1608 s390_expand_logical_operator (enum rtx_code code, machine_mode mode,
1609 rtx *operands)
1611 machine_mode wmode = mode;
1612 rtx dst = operands[0];
1613 rtx src1 = operands[1];
1614 rtx src2 = operands[2];
1615 rtx op, clob, tem;
1617 /* If we cannot handle the operation directly, use a temp register. */
1618 if (!s390_logical_operator_ok_p (operands))
1619 dst = gen_reg_rtx (mode);
1621 /* QImode and HImode patterns make sense only if we have a destination
1622 in memory. Otherwise perform the operation in SImode. */
1623 if ((mode == QImode || mode == HImode) && GET_CODE (dst) != MEM)
1624 wmode = SImode;
1626 /* Widen operands if required. */
1627 if (mode != wmode)
1629 if (GET_CODE (dst) == SUBREG
1630 && (tem = simplify_subreg (wmode, dst, mode, 0)) != 0)
1631 dst = tem;
1632 else if (REG_P (dst))
1633 dst = gen_rtx_SUBREG (wmode, dst, 0);
1634 else
1635 dst = gen_reg_rtx (wmode);
1637 if (GET_CODE (src1) == SUBREG
1638 && (tem = simplify_subreg (wmode, src1, mode, 0)) != 0)
1639 src1 = tem;
1640 else if (GET_MODE (src1) != VOIDmode)
1641 src1 = gen_rtx_SUBREG (wmode, force_reg (mode, src1), 0);
1643 if (GET_CODE (src2) == SUBREG
1644 && (tem = simplify_subreg (wmode, src2, mode, 0)) != 0)
1645 src2 = tem;
1646 else if (GET_MODE (src2) != VOIDmode)
1647 src2 = gen_rtx_SUBREG (wmode, force_reg (mode, src2), 0);
1650 /* Emit the instruction. */
1651 op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, wmode, src1, src2));
1652 clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
1653 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
1655 /* Fix up the destination if needed. */
1656 if (dst != operands[0])
1657 emit_move_insn (operands[0], gen_lowpart (mode, dst));
1660 /* Check whether OPERANDS are OK for a logical operation (AND, IOR, XOR). */
1662 bool
1663 s390_logical_operator_ok_p (rtx *operands)
1665 /* If the destination operand is in memory, it needs to coincide
1666 with one of the source operands. After reload, it has to be
1667 the first source operand. */
1668 if (GET_CODE (operands[0]) == MEM)
1669 return rtx_equal_p (operands[0], operands[1])
1670 || (!reload_completed && rtx_equal_p (operands[0], operands[2]));
1672 return true;
1675 /* Narrow logical operation CODE of memory operand MEMOP with immediate
1676 operand IMMOP to switch from SS to SI type instructions. */
1678 void
1679 s390_narrow_logical_operator (enum rtx_code code, rtx *memop, rtx *immop)
1681 int def = code == AND ? -1 : 0;
1682 HOST_WIDE_INT mask;
1683 int part;
1685 gcc_assert (GET_CODE (*memop) == MEM);
1686 gcc_assert (!MEM_VOLATILE_P (*memop));
1688 mask = s390_extract_part (*immop, QImode, def);
1689 part = s390_single_part (*immop, GET_MODE (*memop), QImode, def);
1690 gcc_assert (part >= 0);
1692 *memop = adjust_address (*memop, QImode, part);
1693 *immop = gen_int_mode (mask, QImode);
1697 /* How to allocate a 'struct machine_function'. */
1699 static struct machine_function *
1700 s390_init_machine_status (void)
1702 return ggc_cleared_alloc<machine_function> ();
1705 /* Map for smallest class containing reg regno. */
1707 const enum reg_class regclass_map[FIRST_PSEUDO_REGISTER] =
1708 { GENERAL_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
1709 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
1710 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
1711 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
1712 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
1713 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
1714 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
1715 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
1716 ADDR_REGS, CC_REGS, ADDR_REGS, ADDR_REGS,
1717 ACCESS_REGS, ACCESS_REGS
1720 /* Return attribute type of insn. */
1722 static enum attr_type
1723 s390_safe_attr_type (rtx_insn *insn)
1725 if (recog_memoized (insn) >= 0)
1726 return get_attr_type (insn);
1727 else
1728 return TYPE_NONE;
1731 /* Return true if DISP is a valid short displacement. */
1733 static bool
1734 s390_short_displacement (rtx disp)
1736 /* No displacement is OK. */
1737 if (!disp)
1738 return true;
1740 /* Without the long displacement facility we don't need to
1741 distingiush between long and short displacement. */
1742 if (!TARGET_LONG_DISPLACEMENT)
1743 return true;
1745 /* Integer displacement in range. */
1746 if (GET_CODE (disp) == CONST_INT)
1747 return INTVAL (disp) >= 0 && INTVAL (disp) < 4096;
1749 /* GOT offset is not OK, the GOT can be large. */
1750 if (GET_CODE (disp) == CONST
1751 && GET_CODE (XEXP (disp, 0)) == UNSPEC
1752 && (XINT (XEXP (disp, 0), 1) == UNSPEC_GOT
1753 || XINT (XEXP (disp, 0), 1) == UNSPEC_GOTNTPOFF))
1754 return false;
1756 /* All other symbolic constants are literal pool references,
1757 which are OK as the literal pool must be small. */
1758 if (GET_CODE (disp) == CONST)
1759 return true;
1761 return false;
1764 /* Decompose a RTL expression ADDR for a memory address into
1765 its components, returned in OUT.
1767 Returns false if ADDR is not a valid memory address, true
1768 otherwise. If OUT is NULL, don't return the components,
1769 but check for validity only.
1771 Note: Only addresses in canonical form are recognized.
1772 LEGITIMIZE_ADDRESS should convert non-canonical forms to the
1773 canonical form so that they will be recognized. */
1775 static int
1776 s390_decompose_address (rtx addr, struct s390_address *out)
1778 HOST_WIDE_INT offset = 0;
1779 rtx base = NULL_RTX;
1780 rtx indx = NULL_RTX;
1781 rtx disp = NULL_RTX;
1782 rtx orig_disp;
1783 bool pointer = false;
1784 bool base_ptr = false;
1785 bool indx_ptr = false;
1786 bool literal_pool = false;
1788 /* We may need to substitute the literal pool base register into the address
1789 below. However, at this point we do not know which register is going to
1790 be used as base, so we substitute the arg pointer register. This is going
1791 to be treated as holding a pointer below -- it shouldn't be used for any
1792 other purpose. */
1793 rtx fake_pool_base = gen_rtx_REG (Pmode, ARG_POINTER_REGNUM);
1795 /* Decompose address into base + index + displacement. */
1797 if (GET_CODE (addr) == REG || GET_CODE (addr) == UNSPEC)
1798 base = addr;
1800 else if (GET_CODE (addr) == PLUS)
1802 rtx op0 = XEXP (addr, 0);
1803 rtx op1 = XEXP (addr, 1);
1804 enum rtx_code code0 = GET_CODE (op0);
1805 enum rtx_code code1 = GET_CODE (op1);
1807 if (code0 == REG || code0 == UNSPEC)
1809 if (code1 == REG || code1 == UNSPEC)
1811 indx = op0; /* index + base */
1812 base = op1;
1815 else
1817 base = op0; /* base + displacement */
1818 disp = op1;
1822 else if (code0 == PLUS)
1824 indx = XEXP (op0, 0); /* index + base + disp */
1825 base = XEXP (op0, 1);
1826 disp = op1;
1829 else
1831 return false;
1835 else
1836 disp = addr; /* displacement */
1838 /* Extract integer part of displacement. */
1839 orig_disp = disp;
1840 if (disp)
1842 if (GET_CODE (disp) == CONST_INT)
1844 offset = INTVAL (disp);
1845 disp = NULL_RTX;
1847 else if (GET_CODE (disp) == CONST
1848 && GET_CODE (XEXP (disp, 0)) == PLUS
1849 && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
1851 offset = INTVAL (XEXP (XEXP (disp, 0), 1));
1852 disp = XEXP (XEXP (disp, 0), 0);
1856 /* Strip off CONST here to avoid special case tests later. */
1857 if (disp && GET_CODE (disp) == CONST)
1858 disp = XEXP (disp, 0);
1860 /* We can convert literal pool addresses to
1861 displacements by basing them off the base register. */
1862 if (disp && GET_CODE (disp) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (disp))
1864 /* Either base or index must be free to hold the base register. */
1865 if (!base)
1866 base = fake_pool_base, literal_pool = true;
1867 else if (!indx)
1868 indx = fake_pool_base, literal_pool = true;
1869 else
1870 return false;
1872 /* Mark up the displacement. */
1873 disp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, disp),
1874 UNSPEC_LTREL_OFFSET);
1877 /* Validate base register. */
1878 if (base)
1880 if (GET_CODE (base) == UNSPEC)
1881 switch (XINT (base, 1))
1883 case UNSPEC_LTREF:
1884 if (!disp)
1885 disp = gen_rtx_UNSPEC (Pmode,
1886 gen_rtvec (1, XVECEXP (base, 0, 0)),
1887 UNSPEC_LTREL_OFFSET);
1888 else
1889 return false;
1891 base = XVECEXP (base, 0, 1);
1892 break;
1894 case UNSPEC_LTREL_BASE:
1895 if (XVECLEN (base, 0) == 1)
1896 base = fake_pool_base, literal_pool = true;
1897 else
1898 base = XVECEXP (base, 0, 1);
1899 break;
1901 default:
1902 return false;
1905 if (!REG_P (base)
1906 || (GET_MODE (base) != SImode
1907 && GET_MODE (base) != Pmode))
1908 return false;
1910 if (REGNO (base) == STACK_POINTER_REGNUM
1911 || REGNO (base) == FRAME_POINTER_REGNUM
1912 || ((reload_completed || reload_in_progress)
1913 && frame_pointer_needed
1914 && REGNO (base) == HARD_FRAME_POINTER_REGNUM)
1915 || REGNO (base) == ARG_POINTER_REGNUM
1916 || (flag_pic
1917 && REGNO (base) == PIC_OFFSET_TABLE_REGNUM))
1918 pointer = base_ptr = true;
1920 if ((reload_completed || reload_in_progress)
1921 && base == cfun->machine->base_reg)
1922 pointer = base_ptr = literal_pool = true;
1925 /* Validate index register. */
1926 if (indx)
1928 if (GET_CODE (indx) == UNSPEC)
1929 switch (XINT (indx, 1))
1931 case UNSPEC_LTREF:
1932 if (!disp)
1933 disp = gen_rtx_UNSPEC (Pmode,
1934 gen_rtvec (1, XVECEXP (indx, 0, 0)),
1935 UNSPEC_LTREL_OFFSET);
1936 else
1937 return false;
1939 indx = XVECEXP (indx, 0, 1);
1940 break;
1942 case UNSPEC_LTREL_BASE:
1943 if (XVECLEN (indx, 0) == 1)
1944 indx = fake_pool_base, literal_pool = true;
1945 else
1946 indx = XVECEXP (indx, 0, 1);
1947 break;
1949 default:
1950 return false;
1953 if (!REG_P (indx)
1954 || (GET_MODE (indx) != SImode
1955 && GET_MODE (indx) != Pmode))
1956 return false;
1958 if (REGNO (indx) == STACK_POINTER_REGNUM
1959 || REGNO (indx) == FRAME_POINTER_REGNUM
1960 || ((reload_completed || reload_in_progress)
1961 && frame_pointer_needed
1962 && REGNO (indx) == HARD_FRAME_POINTER_REGNUM)
1963 || REGNO (indx) == ARG_POINTER_REGNUM
1964 || (flag_pic
1965 && REGNO (indx) == PIC_OFFSET_TABLE_REGNUM))
1966 pointer = indx_ptr = true;
1968 if ((reload_completed || reload_in_progress)
1969 && indx == cfun->machine->base_reg)
1970 pointer = indx_ptr = literal_pool = true;
1973 /* Prefer to use pointer as base, not index. */
1974 if (base && indx && !base_ptr
1975 && (indx_ptr || (!REG_POINTER (base) && REG_POINTER (indx))))
1977 rtx tmp = base;
1978 base = indx;
1979 indx = tmp;
1982 /* Validate displacement. */
1983 if (!disp)
1985 /* If virtual registers are involved, the displacement will change later
1986 anyway as the virtual registers get eliminated. This could make a
1987 valid displacement invalid, but it is more likely to make an invalid
1988 displacement valid, because we sometimes access the register save area
1989 via negative offsets to one of those registers.
1990 Thus we don't check the displacement for validity here. If after
1991 elimination the displacement turns out to be invalid after all,
1992 this is fixed up by reload in any case. */
1993 /* LRA maintains always displacements up to date and we need to
1994 know the displacement is right during all LRA not only at the
1995 final elimination. */
1996 if (lra_in_progress
1997 || (base != arg_pointer_rtx
1998 && indx != arg_pointer_rtx
1999 && base != return_address_pointer_rtx
2000 && indx != return_address_pointer_rtx
2001 && base != frame_pointer_rtx
2002 && indx != frame_pointer_rtx
2003 && base != virtual_stack_vars_rtx
2004 && indx != virtual_stack_vars_rtx))
2005 if (!DISP_IN_RANGE (offset))
2006 return false;
2008 else
2010 /* All the special cases are pointers. */
2011 pointer = true;
2013 /* In the small-PIC case, the linker converts @GOT
2014 and @GOTNTPOFF offsets to possible displacements. */
2015 if (GET_CODE (disp) == UNSPEC
2016 && (XINT (disp, 1) == UNSPEC_GOT
2017 || XINT (disp, 1) == UNSPEC_GOTNTPOFF)
2018 && flag_pic == 1)
2023 /* Accept pool label offsets. */
2024 else if (GET_CODE (disp) == UNSPEC
2025 && XINT (disp, 1) == UNSPEC_POOL_OFFSET)
2028 /* Accept literal pool references. */
2029 else if (GET_CODE (disp) == UNSPEC
2030 && XINT (disp, 1) == UNSPEC_LTREL_OFFSET)
2032 /* In case CSE pulled a non literal pool reference out of
2033 the pool we have to reject the address. This is
2034 especially important when loading the GOT pointer on non
2035 zarch CPUs. In this case the literal pool contains an lt
2036 relative offset to the _GLOBAL_OFFSET_TABLE_ label which
2037 will most likely exceed the displacement. */
2038 if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
2039 || !CONSTANT_POOL_ADDRESS_P (XVECEXP (disp, 0, 0)))
2040 return false;
2042 orig_disp = gen_rtx_CONST (Pmode, disp);
2043 if (offset)
2045 /* If we have an offset, make sure it does not
2046 exceed the size of the constant pool entry. */
2047 rtx sym = XVECEXP (disp, 0, 0);
2048 if (offset >= GET_MODE_SIZE (get_pool_mode (sym)))
2049 return false;
2051 orig_disp = plus_constant (Pmode, orig_disp, offset);
2055 else
2056 return false;
2059 if (!base && !indx)
2060 pointer = true;
2062 if (out)
2064 out->base = base;
2065 out->indx = indx;
2066 out->disp = orig_disp;
2067 out->pointer = pointer;
2068 out->literal_pool = literal_pool;
2071 return true;
2074 /* Decompose a RTL expression OP for a shift count into its components,
2075 and return the base register in BASE and the offset in OFFSET.
2077 Return true if OP is a valid shift count, false if not. */
2079 bool
2080 s390_decompose_shift_count (rtx op, rtx *base, HOST_WIDE_INT *offset)
2082 HOST_WIDE_INT off = 0;
2084 /* We can have an integer constant, an address register,
2085 or a sum of the two. */
2086 if (GET_CODE (op) == CONST_INT)
2088 off = INTVAL (op);
2089 op = NULL_RTX;
2091 if (op && GET_CODE (op) == PLUS && GET_CODE (XEXP (op, 1)) == CONST_INT)
2093 off = INTVAL (XEXP (op, 1));
2094 op = XEXP (op, 0);
2096 while (op && GET_CODE (op) == SUBREG)
2097 op = SUBREG_REG (op);
2099 if (op && GET_CODE (op) != REG)
2100 return false;
2102 if (offset)
2103 *offset = off;
2104 if (base)
2105 *base = op;
2107 return true;
2111 /* Return true if CODE is a valid address without index. */
2113 bool
2114 s390_legitimate_address_without_index_p (rtx op)
2116 struct s390_address addr;
2118 if (!s390_decompose_address (XEXP (op, 0), &addr))
2119 return false;
2120 if (addr.indx)
2121 return false;
2123 return true;
2127 /* Return TRUE if ADDR is an operand valid for a load/store relative
2128 instruction. Be aware that the alignment of the operand needs to
2129 be checked separately.
2130 Valid addresses are single references or a sum of a reference and a
2131 constant integer. Return these parts in SYMREF and ADDEND. You can
2132 pass NULL in REF and/or ADDEND if you are not interested in these
2133 values. Literal pool references are *not* considered symbol
2134 references. */
2136 static bool
2137 s390_loadrelative_operand_p (rtx addr, rtx *symref, HOST_WIDE_INT *addend)
2139 HOST_WIDE_INT tmpaddend = 0;
2141 if (GET_CODE (addr) == CONST)
2142 addr = XEXP (addr, 0);
2144 if (GET_CODE (addr) == PLUS)
2146 if (!CONST_INT_P (XEXP (addr, 1)))
2147 return false;
2149 tmpaddend = INTVAL (XEXP (addr, 1));
2150 addr = XEXP (addr, 0);
2153 if ((GET_CODE (addr) == SYMBOL_REF && !CONSTANT_POOL_ADDRESS_P (addr))
2154 || (GET_CODE (addr) == UNSPEC
2155 && (XINT (addr, 1) == UNSPEC_GOTENT
2156 || (TARGET_CPU_ZARCH && XINT (addr, 1) == UNSPEC_PLT))))
2158 if (symref)
2159 *symref = addr;
2160 if (addend)
2161 *addend = tmpaddend;
2163 return true;
2165 return false;
2168 /* Return true if the address in OP is valid for constraint letter C
2169 if wrapped in a MEM rtx. Set LIT_POOL_OK to true if it literal
2170 pool MEMs should be accepted. Only the Q, R, S, T constraint
2171 letters are allowed for C. */
2173 static int
2174 s390_check_qrst_address (char c, rtx op, bool lit_pool_ok)
2176 struct s390_address addr;
2177 bool decomposed = false;
2179 /* This check makes sure that no symbolic address (except literal
2180 pool references) are accepted by the R or T constraints. */
2181 if (s390_loadrelative_operand_p (op, NULL, NULL))
2182 return 0;
2184 /* Ensure literal pool references are only accepted if LIT_POOL_OK. */
2185 if (!lit_pool_ok)
2187 if (!s390_decompose_address (op, &addr))
2188 return 0;
2189 if (addr.literal_pool)
2190 return 0;
2191 decomposed = true;
2194 switch (c)
2196 case 'Q': /* no index short displacement */
2197 if (!decomposed && !s390_decompose_address (op, &addr))
2198 return 0;
2199 if (addr.indx)
2200 return 0;
2201 if (!s390_short_displacement (addr.disp))
2202 return 0;
2203 break;
2205 case 'R': /* with index short displacement */
2206 if (TARGET_LONG_DISPLACEMENT)
2208 if (!decomposed && !s390_decompose_address (op, &addr))
2209 return 0;
2210 if (!s390_short_displacement (addr.disp))
2211 return 0;
2213 /* Any invalid address here will be fixed up by reload,
2214 so accept it for the most generic constraint. */
2215 break;
2217 case 'S': /* no index long displacement */
2218 if (!TARGET_LONG_DISPLACEMENT)
2219 return 0;
2220 if (!decomposed && !s390_decompose_address (op, &addr))
2221 return 0;
2222 if (addr.indx)
2223 return 0;
2224 if (s390_short_displacement (addr.disp))
2225 return 0;
2226 break;
2228 case 'T': /* with index long displacement */
2229 if (!TARGET_LONG_DISPLACEMENT)
2230 return 0;
2231 /* Any invalid address here will be fixed up by reload,
2232 so accept it for the most generic constraint. */
2233 if ((decomposed || s390_decompose_address (op, &addr))
2234 && s390_short_displacement (addr.disp))
2235 return 0;
2236 break;
2237 default:
2238 return 0;
2240 return 1;
2244 /* Evaluates constraint strings described by the regular expression
2245 ([A|B|Z](Q|R|S|T))|U|W|Y and returns 1 if OP is a valid operand for
2246 the constraint given in STR, or 0 else. */
2249 s390_mem_constraint (const char *str, rtx op)
2251 char c = str[0];
2253 switch (c)
2255 case 'A':
2256 /* Check for offsettable variants of memory constraints. */
2257 if (!MEM_P (op) || MEM_VOLATILE_P (op))
2258 return 0;
2259 if ((reload_completed || reload_in_progress)
2260 ? !offsettable_memref_p (op) : !offsettable_nonstrict_memref_p (op))
2261 return 0;
2262 return s390_check_qrst_address (str[1], XEXP (op, 0), true);
2263 case 'B':
2264 /* Check for non-literal-pool variants of memory constraints. */
2265 if (!MEM_P (op))
2266 return 0;
2267 return s390_check_qrst_address (str[1], XEXP (op, 0), false);
2268 case 'Q':
2269 case 'R':
2270 case 'S':
2271 case 'T':
2272 if (GET_CODE (op) != MEM)
2273 return 0;
2274 return s390_check_qrst_address (c, XEXP (op, 0), true);
2275 case 'U':
2276 return (s390_check_qrst_address ('Q', op, true)
2277 || s390_check_qrst_address ('R', op, true));
2278 case 'W':
2279 return (s390_check_qrst_address ('S', op, true)
2280 || s390_check_qrst_address ('T', op, true));
2281 case 'Y':
2282 /* Simply check for the basic form of a shift count. Reload will
2283 take care of making sure we have a proper base register. */
2284 if (!s390_decompose_shift_count (op, NULL, NULL))
2285 return 0;
2286 break;
2287 case 'Z':
2288 return s390_check_qrst_address (str[1], op, true);
2289 default:
2290 return 0;
2292 return 1;
2296 /* Evaluates constraint strings starting with letter O. Input
2297 parameter C is the second letter following the "O" in the constraint
2298 string. Returns 1 if VALUE meets the respective constraint and 0
2299 otherwise. */
2302 s390_O_constraint_str (const char c, HOST_WIDE_INT value)
2304 if (!TARGET_EXTIMM)
2305 return 0;
2307 switch (c)
2309 case 's':
2310 return trunc_int_for_mode (value, SImode) == value;
2312 case 'p':
2313 return value == 0
2314 || s390_single_part (GEN_INT (value), DImode, SImode, 0) == 1;
2316 case 'n':
2317 return s390_single_part (GEN_INT (value - 1), DImode, SImode, -1) == 1;
2319 default:
2320 gcc_unreachable ();
2325 /* Evaluates constraint strings starting with letter N. Parameter STR
2326 contains the letters following letter "N" in the constraint string.
2327 Returns true if VALUE matches the constraint. */
2330 s390_N_constraint_str (const char *str, HOST_WIDE_INT value)
2332 machine_mode mode, part_mode;
2333 int def;
2334 int part, part_goal;
2337 if (str[0] == 'x')
2338 part_goal = -1;
2339 else
2340 part_goal = str[0] - '0';
2342 switch (str[1])
2344 case 'Q':
2345 part_mode = QImode;
2346 break;
2347 case 'H':
2348 part_mode = HImode;
2349 break;
2350 case 'S':
2351 part_mode = SImode;
2352 break;
2353 default:
2354 return 0;
2357 switch (str[2])
2359 case 'H':
2360 mode = HImode;
2361 break;
2362 case 'S':
2363 mode = SImode;
2364 break;
2365 case 'D':
2366 mode = DImode;
2367 break;
2368 default:
2369 return 0;
2372 switch (str[3])
2374 case '0':
2375 def = 0;
2376 break;
2377 case 'F':
2378 def = -1;
2379 break;
2380 default:
2381 return 0;
2384 if (GET_MODE_SIZE (mode) <= GET_MODE_SIZE (part_mode))
2385 return 0;
2387 part = s390_single_part (GEN_INT (value), mode, part_mode, def);
2388 if (part < 0)
2389 return 0;
2390 if (part_goal != -1 && part_goal != part)
2391 return 0;
2393 return 1;
2397 /* Returns true if the input parameter VALUE is a float zero. */
2400 s390_float_const_zero_p (rtx value)
2402 return (GET_MODE_CLASS (GET_MODE (value)) == MODE_FLOAT
2403 && value == CONST0_RTX (GET_MODE (value)));
2406 /* Implement TARGET_REGISTER_MOVE_COST. */
2408 static int
2409 s390_register_move_cost (machine_mode mode,
2410 reg_class_t from, reg_class_t to)
2412 /* On s390, copy between fprs and gprs is expensive. */
2414 /* It becomes somewhat faster having ldgr/lgdr. */
2415 if (TARGET_Z10 && GET_MODE_SIZE (mode) == 8)
2417 /* ldgr is single cycle. */
2418 if (reg_classes_intersect_p (from, GENERAL_REGS)
2419 && reg_classes_intersect_p (to, FP_REGS))
2420 return 1;
2421 /* lgdr needs 3 cycles. */
2422 if (reg_classes_intersect_p (to, GENERAL_REGS)
2423 && reg_classes_intersect_p (from, FP_REGS))
2424 return 3;
2427 /* Otherwise copying is done via memory. */
2428 if ((reg_classes_intersect_p (from, GENERAL_REGS)
2429 && reg_classes_intersect_p (to, FP_REGS))
2430 || (reg_classes_intersect_p (from, FP_REGS)
2431 && reg_classes_intersect_p (to, GENERAL_REGS)))
2432 return 10;
2434 return 1;
2437 /* Implement TARGET_MEMORY_MOVE_COST. */
2439 static int
2440 s390_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
2441 reg_class_t rclass ATTRIBUTE_UNUSED,
2442 bool in ATTRIBUTE_UNUSED)
2444 return 2;
2447 /* Compute a (partial) cost for rtx X. Return true if the complete
2448 cost has been computed, and false if subexpressions should be
2449 scanned. In either case, *TOTAL contains the cost result.
2450 CODE contains GET_CODE (x), OUTER_CODE contains the code
2451 of the superexpression of x. */
2453 static bool
2454 s390_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
2455 int *total, bool speed ATTRIBUTE_UNUSED)
2457 switch (code)
2459 case CONST:
2460 case CONST_INT:
2461 case LABEL_REF:
2462 case SYMBOL_REF:
2463 case CONST_DOUBLE:
2464 case MEM:
2465 *total = 0;
2466 return true;
2468 case ASHIFT:
2469 case ASHIFTRT:
2470 case LSHIFTRT:
2471 case ROTATE:
2472 case ROTATERT:
2473 case AND:
2474 case IOR:
2475 case XOR:
2476 case NEG:
2477 case NOT:
2478 *total = COSTS_N_INSNS (1);
2479 return false;
2481 case PLUS:
2482 case MINUS:
2483 *total = COSTS_N_INSNS (1);
2484 return false;
2486 case MULT:
2487 switch (GET_MODE (x))
2489 case SImode:
2491 rtx left = XEXP (x, 0);
2492 rtx right = XEXP (x, 1);
2493 if (GET_CODE (right) == CONST_INT
2494 && CONST_OK_FOR_K (INTVAL (right)))
2495 *total = s390_cost->mhi;
2496 else if (GET_CODE (left) == SIGN_EXTEND)
2497 *total = s390_cost->mh;
2498 else
2499 *total = s390_cost->ms; /* msr, ms, msy */
2500 break;
2502 case DImode:
2504 rtx left = XEXP (x, 0);
2505 rtx right = XEXP (x, 1);
2506 if (TARGET_ZARCH)
2508 if (GET_CODE (right) == CONST_INT
2509 && CONST_OK_FOR_K (INTVAL (right)))
2510 *total = s390_cost->mghi;
2511 else if (GET_CODE (left) == SIGN_EXTEND)
2512 *total = s390_cost->msgf;
2513 else
2514 *total = s390_cost->msg; /* msgr, msg */
2516 else /* TARGET_31BIT */
2518 if (GET_CODE (left) == SIGN_EXTEND
2519 && GET_CODE (right) == SIGN_EXTEND)
2520 /* mulsidi case: mr, m */
2521 *total = s390_cost->m;
2522 else if (GET_CODE (left) == ZERO_EXTEND
2523 && GET_CODE (right) == ZERO_EXTEND
2524 && TARGET_CPU_ZARCH)
2525 /* umulsidi case: ml, mlr */
2526 *total = s390_cost->ml;
2527 else
2528 /* Complex calculation is required. */
2529 *total = COSTS_N_INSNS (40);
2531 break;
2533 case SFmode:
2534 case DFmode:
2535 *total = s390_cost->mult_df;
2536 break;
2537 case TFmode:
2538 *total = s390_cost->mxbr;
2539 break;
2540 default:
2541 return false;
2543 return false;
2545 case FMA:
2546 switch (GET_MODE (x))
2548 case DFmode:
2549 *total = s390_cost->madbr;
2550 break;
2551 case SFmode:
2552 *total = s390_cost->maebr;
2553 break;
2554 default:
2555 return false;
2557 /* Negate in the third argument is free: FMSUB. */
2558 if (GET_CODE (XEXP (x, 2)) == NEG)
2560 *total += (rtx_cost (XEXP (x, 0), FMA, 0, speed)
2561 + rtx_cost (XEXP (x, 1), FMA, 1, speed)
2562 + rtx_cost (XEXP (XEXP (x, 2), 0), FMA, 2, speed));
2563 return true;
2565 return false;
2567 case UDIV:
2568 case UMOD:
2569 if (GET_MODE (x) == TImode) /* 128 bit division */
2570 *total = s390_cost->dlgr;
2571 else if (GET_MODE (x) == DImode)
2573 rtx right = XEXP (x, 1);
2574 if (GET_CODE (right) == ZERO_EXTEND) /* 64 by 32 bit division */
2575 *total = s390_cost->dlr;
2576 else /* 64 by 64 bit division */
2577 *total = s390_cost->dlgr;
2579 else if (GET_MODE (x) == SImode) /* 32 bit division */
2580 *total = s390_cost->dlr;
2581 return false;
2583 case DIV:
2584 case MOD:
2585 if (GET_MODE (x) == DImode)
2587 rtx right = XEXP (x, 1);
2588 if (GET_CODE (right) == ZERO_EXTEND) /* 64 by 32 bit division */
2589 if (TARGET_ZARCH)
2590 *total = s390_cost->dsgfr;
2591 else
2592 *total = s390_cost->dr;
2593 else /* 64 by 64 bit division */
2594 *total = s390_cost->dsgr;
2596 else if (GET_MODE (x) == SImode) /* 32 bit division */
2597 *total = s390_cost->dlr;
2598 else if (GET_MODE (x) == SFmode)
2600 *total = s390_cost->debr;
2602 else if (GET_MODE (x) == DFmode)
2604 *total = s390_cost->ddbr;
2606 else if (GET_MODE (x) == TFmode)
2608 *total = s390_cost->dxbr;
2610 return false;
2612 case SQRT:
2613 if (GET_MODE (x) == SFmode)
2614 *total = s390_cost->sqebr;
2615 else if (GET_MODE (x) == DFmode)
2616 *total = s390_cost->sqdbr;
2617 else /* TFmode */
2618 *total = s390_cost->sqxbr;
2619 return false;
2621 case SIGN_EXTEND:
2622 case ZERO_EXTEND:
2623 if (outer_code == MULT || outer_code == DIV || outer_code == MOD
2624 || outer_code == PLUS || outer_code == MINUS
2625 || outer_code == COMPARE)
2626 *total = 0;
2627 return false;
2629 case COMPARE:
2630 *total = COSTS_N_INSNS (1);
2631 if (GET_CODE (XEXP (x, 0)) == AND
2632 && GET_CODE (XEXP (x, 1)) == CONST_INT
2633 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
2635 rtx op0 = XEXP (XEXP (x, 0), 0);
2636 rtx op1 = XEXP (XEXP (x, 0), 1);
2637 rtx op2 = XEXP (x, 1);
2639 if (memory_operand (op0, GET_MODE (op0))
2640 && s390_tm_ccmode (op1, op2, 0) != VOIDmode)
2641 return true;
2642 if (register_operand (op0, GET_MODE (op0))
2643 && s390_tm_ccmode (op1, op2, 1) != VOIDmode)
2644 return true;
2646 return false;
2648 default:
2649 return false;
2653 /* Return the cost of an address rtx ADDR. */
2655 static int
2656 s390_address_cost (rtx addr, machine_mode mode ATTRIBUTE_UNUSED,
2657 addr_space_t as ATTRIBUTE_UNUSED,
2658 bool speed ATTRIBUTE_UNUSED)
2660 struct s390_address ad;
2661 if (!s390_decompose_address (addr, &ad))
2662 return 1000;
2664 return ad.indx? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (1);
2667 /* If OP is a SYMBOL_REF of a thread-local symbol, return its TLS mode,
2668 otherwise return 0. */
2671 tls_symbolic_operand (rtx op)
2673 if (GET_CODE (op) != SYMBOL_REF)
2674 return 0;
2675 return SYMBOL_REF_TLS_MODEL (op);
2678 /* Split DImode access register reference REG (on 64-bit) into its constituent
2679 low and high parts, and store them into LO and HI. Note that gen_lowpart/
2680 gen_highpart cannot be used as they assume all registers are word-sized,
2681 while our access registers have only half that size. */
2683 void
2684 s390_split_access_reg (rtx reg, rtx *lo, rtx *hi)
2686 gcc_assert (TARGET_64BIT);
2687 gcc_assert (ACCESS_REG_P (reg));
2688 gcc_assert (GET_MODE (reg) == DImode);
2689 gcc_assert (!(REGNO (reg) & 1));
2691 *lo = gen_rtx_REG (SImode, REGNO (reg) + 1);
2692 *hi = gen_rtx_REG (SImode, REGNO (reg));
2695 /* Return true if OP contains a symbol reference */
2697 bool
2698 symbolic_reference_mentioned_p (rtx op)
2700 const char *fmt;
2701 int i;
2703 if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
2704 return 1;
2706 fmt = GET_RTX_FORMAT (GET_CODE (op));
2707 for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
2709 if (fmt[i] == 'E')
2711 int j;
2713 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
2714 if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
2715 return 1;
2718 else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
2719 return 1;
2722 return 0;
2725 /* Return true if OP contains a reference to a thread-local symbol. */
2727 bool
2728 tls_symbolic_reference_mentioned_p (rtx op)
2730 const char *fmt;
2731 int i;
2733 if (GET_CODE (op) == SYMBOL_REF)
2734 return tls_symbolic_operand (op);
2736 fmt = GET_RTX_FORMAT (GET_CODE (op));
2737 for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
2739 if (fmt[i] == 'E')
2741 int j;
2743 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
2744 if (tls_symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
2745 return true;
2748 else if (fmt[i] == 'e' && tls_symbolic_reference_mentioned_p (XEXP (op, i)))
2749 return true;
2752 return false;
2756 /* Return true if OP is a legitimate general operand when
2757 generating PIC code. It is given that flag_pic is on
2758 and that OP satisfies CONSTANT_P or is a CONST_DOUBLE. */
2761 legitimate_pic_operand_p (rtx op)
2763 /* Accept all non-symbolic constants. */
2764 if (!SYMBOLIC_CONST (op))
2765 return 1;
2767 /* Reject everything else; must be handled
2768 via emit_symbolic_move. */
2769 return 0;
2772 /* Returns true if the constant value OP is a legitimate general operand.
2773 It is given that OP satisfies CONSTANT_P or is a CONST_DOUBLE. */
2775 static bool
2776 s390_legitimate_constant_p (machine_mode mode, rtx op)
2778 /* Accept all non-symbolic constants. */
2779 if (!SYMBOLIC_CONST (op))
2780 return 1;
2782 /* Accept immediate LARL operands. */
2783 if (TARGET_CPU_ZARCH && larl_operand (op, mode))
2784 return 1;
2786 /* Thread-local symbols are never legal constants. This is
2787 so that emit_call knows that computing such addresses
2788 might require a function call. */
2789 if (TLS_SYMBOLIC_CONST (op))
2790 return 0;
2792 /* In the PIC case, symbolic constants must *not* be
2793 forced into the literal pool. We accept them here,
2794 so that they will be handled by emit_symbolic_move. */
2795 if (flag_pic)
2796 return 1;
2798 /* All remaining non-PIC symbolic constants are
2799 forced into the literal pool. */
2800 return 0;
2803 /* Determine if it's legal to put X into the constant pool. This
2804 is not possible if X contains the address of a symbol that is
2805 not constant (TLS) or not known at final link time (PIC). */
2807 static bool
2808 s390_cannot_force_const_mem (machine_mode mode, rtx x)
2810 switch (GET_CODE (x))
2812 case CONST_INT:
2813 case CONST_DOUBLE:
2814 /* Accept all non-symbolic constants. */
2815 return false;
2817 case LABEL_REF:
2818 /* Labels are OK iff we are non-PIC. */
2819 return flag_pic != 0;
2821 case SYMBOL_REF:
2822 /* 'Naked' TLS symbol references are never OK,
2823 non-TLS symbols are OK iff we are non-PIC. */
2824 if (tls_symbolic_operand (x))
2825 return true;
2826 else
2827 return flag_pic != 0;
2829 case CONST:
2830 return s390_cannot_force_const_mem (mode, XEXP (x, 0));
2831 case PLUS:
2832 case MINUS:
2833 return s390_cannot_force_const_mem (mode, XEXP (x, 0))
2834 || s390_cannot_force_const_mem (mode, XEXP (x, 1));
2836 case UNSPEC:
2837 switch (XINT (x, 1))
2839 /* Only lt-relative or GOT-relative UNSPECs are OK. */
2840 case UNSPEC_LTREL_OFFSET:
2841 case UNSPEC_GOT:
2842 case UNSPEC_GOTOFF:
2843 case UNSPEC_PLTOFF:
2844 case UNSPEC_TLSGD:
2845 case UNSPEC_TLSLDM:
2846 case UNSPEC_NTPOFF:
2847 case UNSPEC_DTPOFF:
2848 case UNSPEC_GOTNTPOFF:
2849 case UNSPEC_INDNTPOFF:
2850 return false;
2852 /* If the literal pool shares the code section, be put
2853 execute template placeholders into the pool as well. */
2854 case UNSPEC_INSN:
2855 return TARGET_CPU_ZARCH;
2857 default:
2858 return true;
2860 break;
2862 default:
2863 gcc_unreachable ();
2867 /* Returns true if the constant value OP is a legitimate general
2868 operand during and after reload. The difference to
2869 legitimate_constant_p is that this function will not accept
2870 a constant that would need to be forced to the literal pool
2871 before it can be used as operand.
2872 This function accepts all constants which can be loaded directly
2873 into a GPR. */
2875 bool
2876 legitimate_reload_constant_p (rtx op)
2878 /* Accept la(y) operands. */
2879 if (GET_CODE (op) == CONST_INT
2880 && DISP_IN_RANGE (INTVAL (op)))
2881 return true;
2883 /* Accept l(g)hi/l(g)fi operands. */
2884 if (GET_CODE (op) == CONST_INT
2885 && (CONST_OK_FOR_K (INTVAL (op)) || CONST_OK_FOR_Os (INTVAL (op))))
2886 return true;
2888 /* Accept lliXX operands. */
2889 if (TARGET_ZARCH
2890 && GET_CODE (op) == CONST_INT
2891 && trunc_int_for_mode (INTVAL (op), word_mode) == INTVAL (op)
2892 && s390_single_part (op, word_mode, HImode, 0) >= 0)
2893 return true;
2895 if (TARGET_EXTIMM
2896 && GET_CODE (op) == CONST_INT
2897 && trunc_int_for_mode (INTVAL (op), word_mode) == INTVAL (op)
2898 && s390_single_part (op, word_mode, SImode, 0) >= 0)
2899 return true;
2901 /* Accept larl operands. */
2902 if (TARGET_CPU_ZARCH
2903 && larl_operand (op, VOIDmode))
2904 return true;
2906 /* Accept floating-point zero operands that fit into a single GPR. */
2907 if (GET_CODE (op) == CONST_DOUBLE
2908 && s390_float_const_zero_p (op)
2909 && GET_MODE_SIZE (GET_MODE (op)) <= UNITS_PER_WORD)
2910 return true;
2912 /* Accept double-word operands that can be split. */
2913 if (GET_CODE (op) == CONST_INT
2914 && trunc_int_for_mode (INTVAL (op), word_mode) != INTVAL (op))
2916 machine_mode dword_mode = word_mode == SImode ? DImode : TImode;
2917 rtx hi = operand_subword (op, 0, 0, dword_mode);
2918 rtx lo = operand_subword (op, 1, 0, dword_mode);
2919 return legitimate_reload_constant_p (hi)
2920 && legitimate_reload_constant_p (lo);
2923 /* Everything else cannot be handled without reload. */
2924 return false;
2927 /* Returns true if the constant value OP is a legitimate fp operand
2928 during and after reload.
2929 This function accepts all constants which can be loaded directly
2930 into an FPR. */
2932 static bool
2933 legitimate_reload_fp_constant_p (rtx op)
2935 /* Accept floating-point zero operands if the load zero instruction
2936 can be used. Prior to z196 the load fp zero instruction caused a
2937 performance penalty if the result is used as BFP number. */
2938 if (TARGET_Z196
2939 && GET_CODE (op) == CONST_DOUBLE
2940 && s390_float_const_zero_p (op))
2941 return true;
2943 return false;
2946 /* Given an rtx OP being reloaded into a reg required to be in class RCLASS,
2947 return the class of reg to actually use. */
2949 static reg_class_t
2950 s390_preferred_reload_class (rtx op, reg_class_t rclass)
2952 switch (GET_CODE (op))
2954 /* Constants we cannot reload into general registers
2955 must be forced into the literal pool. */
2956 case CONST_DOUBLE:
2957 case CONST_INT:
2958 if (reg_class_subset_p (GENERAL_REGS, rclass)
2959 && legitimate_reload_constant_p (op))
2960 return GENERAL_REGS;
2961 else if (reg_class_subset_p (ADDR_REGS, rclass)
2962 && legitimate_reload_constant_p (op))
2963 return ADDR_REGS;
2964 else if (reg_class_subset_p (FP_REGS, rclass)
2965 && legitimate_reload_fp_constant_p (op))
2966 return FP_REGS;
2967 return NO_REGS;
2969 /* If a symbolic constant or a PLUS is reloaded,
2970 it is most likely being used as an address, so
2971 prefer ADDR_REGS. If 'class' is not a superset
2972 of ADDR_REGS, e.g. FP_REGS, reject this reload. */
2973 case CONST:
2974 /* Symrefs cannot be pushed into the literal pool with -fPIC
2975 so we *MUST NOT* return NO_REGS for these cases
2976 (s390_cannot_force_const_mem will return true).
2978 On the other hand we MUST return NO_REGS for symrefs with
2979 invalid addend which might have been pushed to the literal
2980 pool (no -fPIC). Usually we would expect them to be
2981 handled via secondary reload but this does not happen if
2982 they are used as literal pool slot replacement in reload
2983 inheritance (see emit_input_reload_insns). */
2984 if (TARGET_CPU_ZARCH
2985 && GET_CODE (XEXP (op, 0)) == PLUS
2986 && GET_CODE (XEXP (XEXP(op, 0), 0)) == SYMBOL_REF
2987 && GET_CODE (XEXP (XEXP(op, 0), 1)) == CONST_INT)
2989 if (flag_pic && reg_class_subset_p (ADDR_REGS, rclass))
2990 return ADDR_REGS;
2991 else
2992 return NO_REGS;
2994 /* fallthrough */
2995 case LABEL_REF:
2996 case SYMBOL_REF:
2997 if (!legitimate_reload_constant_p (op))
2998 return NO_REGS;
2999 /* fallthrough */
3000 case PLUS:
3001 /* load address will be used. */
3002 if (reg_class_subset_p (ADDR_REGS, rclass))
3003 return ADDR_REGS;
3004 else
3005 return NO_REGS;
3007 default:
3008 break;
3011 return rclass;
3014 /* Return true if ADDR is SYMBOL_REF + addend with addend being a
3015 multiple of ALIGNMENT and the SYMBOL_REF being naturally
3016 aligned. */
3018 bool
3019 s390_check_symref_alignment (rtx addr, HOST_WIDE_INT alignment)
3021 HOST_WIDE_INT addend;
3022 rtx symref;
3024 if (!s390_loadrelative_operand_p (addr, &symref, &addend))
3025 return false;
3027 if (addend & (alignment - 1))
3028 return false;
3030 if (GET_CODE (symref) == SYMBOL_REF
3031 && !SYMBOL_REF_NOT_NATURALLY_ALIGNED_P (symref))
3032 return true;
3034 if (GET_CODE (symref) == UNSPEC
3035 && alignment <= UNITS_PER_LONG)
3036 return true;
3038 return false;
3041 /* ADDR is moved into REG using larl. If ADDR isn't a valid larl
3042 operand SCRATCH is used to reload the even part of the address and
3043 adding one. */
3045 void
3046 s390_reload_larl_operand (rtx reg, rtx addr, rtx scratch)
3048 HOST_WIDE_INT addend;
3049 rtx symref;
3051 if (!s390_loadrelative_operand_p (addr, &symref, &addend))
3052 gcc_unreachable ();
3054 if (!(addend & 1))
3055 /* Easy case. The addend is even so larl will do fine. */
3056 emit_move_insn (reg, addr);
3057 else
3059 /* We can leave the scratch register untouched if the target
3060 register is a valid base register. */
3061 if (REGNO (reg) < FIRST_PSEUDO_REGISTER
3062 && REGNO_REG_CLASS (REGNO (reg)) == ADDR_REGS)
3063 scratch = reg;
3065 gcc_assert (REGNO (scratch) < FIRST_PSEUDO_REGISTER);
3066 gcc_assert (REGNO_REG_CLASS (REGNO (scratch)) == ADDR_REGS);
3068 if (addend != 1)
3069 emit_move_insn (scratch,
3070 gen_rtx_CONST (Pmode,
3071 gen_rtx_PLUS (Pmode, symref,
3072 GEN_INT (addend - 1))));
3073 else
3074 emit_move_insn (scratch, symref);
3076 /* Increment the address using la in order to avoid clobbering cc. */
3077 s390_load_address (reg, gen_rtx_PLUS (Pmode, scratch, const1_rtx));
3081 /* Generate what is necessary to move between REG and MEM using
3082 SCRATCH. The direction is given by TOMEM. */
3084 void
3085 s390_reload_symref_address (rtx reg, rtx mem, rtx scratch, bool tomem)
3087 /* Reload might have pulled a constant out of the literal pool.
3088 Force it back in. */
3089 if (CONST_INT_P (mem) || GET_CODE (mem) == CONST_DOUBLE
3090 || GET_CODE (mem) == CONST)
3091 mem = force_const_mem (GET_MODE (reg), mem);
3093 gcc_assert (MEM_P (mem));
3095 /* For a load from memory we can leave the scratch register
3096 untouched if the target register is a valid base register. */
3097 if (!tomem
3098 && REGNO (reg) < FIRST_PSEUDO_REGISTER
3099 && REGNO_REG_CLASS (REGNO (reg)) == ADDR_REGS
3100 && GET_MODE (reg) == GET_MODE (scratch))
3101 scratch = reg;
3103 /* Load address into scratch register. Since we can't have a
3104 secondary reload for a secondary reload we have to cover the case
3105 where larl would need a secondary reload here as well. */
3106 s390_reload_larl_operand (scratch, XEXP (mem, 0), scratch);
3108 /* Now we can use a standard load/store to do the move. */
3109 if (tomem)
3110 emit_move_insn (replace_equiv_address (mem, scratch), reg);
3111 else
3112 emit_move_insn (reg, replace_equiv_address (mem, scratch));
3115 /* Inform reload about cases where moving X with a mode MODE to a register in
3116 RCLASS requires an extra scratch or immediate register. Return the class
3117 needed for the immediate register. */
3119 static reg_class_t
3120 s390_secondary_reload (bool in_p, rtx x, reg_class_t rclass_i,
3121 machine_mode mode, secondary_reload_info *sri)
3123 enum reg_class rclass = (enum reg_class) rclass_i;
3125 /* Intermediate register needed. */
3126 if (reg_classes_intersect_p (CC_REGS, rclass))
3127 return GENERAL_REGS;
3129 if (TARGET_Z10)
3131 HOST_WIDE_INT offset;
3132 rtx symref;
3134 /* On z10 several optimizer steps may generate larl operands with
3135 an odd addend. */
3136 if (in_p
3137 && s390_loadrelative_operand_p (x, &symref, &offset)
3138 && mode == Pmode
3139 && !SYMBOL_REF_ALIGN1_P (symref)
3140 && (offset & 1) == 1)
3141 sri->icode = ((mode == DImode) ? CODE_FOR_reloaddi_larl_odd_addend_z10
3142 : CODE_FOR_reloadsi_larl_odd_addend_z10);
3144 /* On z10 we need a scratch register when moving QI, TI or floating
3145 point mode values from or to a memory location with a SYMBOL_REF
3146 or if the symref addend of a SI or DI move is not aligned to the
3147 width of the access. */
3148 if (MEM_P (x)
3149 && s390_loadrelative_operand_p (XEXP (x, 0), NULL, NULL)
3150 && (mode == QImode || mode == TImode || FLOAT_MODE_P (mode)
3151 || (!TARGET_ZARCH && mode == DImode)
3152 || ((mode == HImode || mode == SImode || mode == DImode)
3153 && (!s390_check_symref_alignment (XEXP (x, 0),
3154 GET_MODE_SIZE (mode))))))
3156 #define __SECONDARY_RELOAD_CASE(M,m) \
3157 case M##mode: \
3158 if (TARGET_64BIT) \
3159 sri->icode = in_p ? CODE_FOR_reload##m##di_toreg_z10 : \
3160 CODE_FOR_reload##m##di_tomem_z10; \
3161 else \
3162 sri->icode = in_p ? CODE_FOR_reload##m##si_toreg_z10 : \
3163 CODE_FOR_reload##m##si_tomem_z10; \
3164 break;
3166 switch (GET_MODE (x))
3168 __SECONDARY_RELOAD_CASE (QI, qi);
3169 __SECONDARY_RELOAD_CASE (HI, hi);
3170 __SECONDARY_RELOAD_CASE (SI, si);
3171 __SECONDARY_RELOAD_CASE (DI, di);
3172 __SECONDARY_RELOAD_CASE (TI, ti);
3173 __SECONDARY_RELOAD_CASE (SF, sf);
3174 __SECONDARY_RELOAD_CASE (DF, df);
3175 __SECONDARY_RELOAD_CASE (TF, tf);
3176 __SECONDARY_RELOAD_CASE (SD, sd);
3177 __SECONDARY_RELOAD_CASE (DD, dd);
3178 __SECONDARY_RELOAD_CASE (TD, td);
3180 default:
3181 gcc_unreachable ();
3183 #undef __SECONDARY_RELOAD_CASE
3187 /* We need a scratch register when loading a PLUS expression which
3188 is not a legitimate operand of the LOAD ADDRESS instruction. */
3189 /* LRA can deal with transformation of plus op very well -- so we
3190 don't need to prompt LRA in this case. */
3191 if (! lra_in_progress && in_p && s390_plus_operand (x, mode))
3192 sri->icode = (TARGET_64BIT ?
3193 CODE_FOR_reloaddi_plus : CODE_FOR_reloadsi_plus);
3195 /* Performing a multiword move from or to memory we have to make sure the
3196 second chunk in memory is addressable without causing a displacement
3197 overflow. If that would be the case we calculate the address in
3198 a scratch register. */
3199 if (MEM_P (x)
3200 && GET_CODE (XEXP (x, 0)) == PLUS
3201 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3202 && !DISP_IN_RANGE (INTVAL (XEXP (XEXP (x, 0), 1))
3203 + GET_MODE_SIZE (mode) - 1))
3205 /* For GENERAL_REGS a displacement overflow is no problem if occurring
3206 in a s_operand address since we may fallback to lm/stm. So we only
3207 have to care about overflows in the b+i+d case. */
3208 if ((reg_classes_intersect_p (GENERAL_REGS, rclass)
3209 && s390_class_max_nregs (GENERAL_REGS, mode) > 1
3210 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS)
3211 /* For FP_REGS no lm/stm is available so this check is triggered
3212 for displacement overflows in b+i+d and b+d like addresses. */
3213 || (reg_classes_intersect_p (FP_REGS, rclass)
3214 && s390_class_max_nregs (FP_REGS, mode) > 1))
3216 if (in_p)
3217 sri->icode = (TARGET_64BIT ?
3218 CODE_FOR_reloaddi_nonoffmem_in :
3219 CODE_FOR_reloadsi_nonoffmem_in);
3220 else
3221 sri->icode = (TARGET_64BIT ?
3222 CODE_FOR_reloaddi_nonoffmem_out :
3223 CODE_FOR_reloadsi_nonoffmem_out);
3227 /* A scratch address register is needed when a symbolic constant is
3228 copied to r0 compiling with -fPIC. In other cases the target
3229 register might be used as temporary (see legitimize_pic_address). */
3230 if (in_p && SYMBOLIC_CONST (x) && flag_pic == 2 && rclass != ADDR_REGS)
3231 sri->icode = (TARGET_64BIT ?
3232 CODE_FOR_reloaddi_PIC_addr :
3233 CODE_FOR_reloadsi_PIC_addr);
3235 /* Either scratch or no register needed. */
3236 return NO_REGS;
3239 /* Generate code to load SRC, which is PLUS that is not a
3240 legitimate operand for the LA instruction, into TARGET.
3241 SCRATCH may be used as scratch register. */
3243 void
3244 s390_expand_plus_operand (rtx target, rtx src,
3245 rtx scratch)
3247 rtx sum1, sum2;
3248 struct s390_address ad;
3250 /* src must be a PLUS; get its two operands. */
3251 gcc_assert (GET_CODE (src) == PLUS);
3252 gcc_assert (GET_MODE (src) == Pmode);
3254 /* Check if any of the two operands is already scheduled
3255 for replacement by reload. This can happen e.g. when
3256 float registers occur in an address. */
3257 sum1 = find_replacement (&XEXP (src, 0));
3258 sum2 = find_replacement (&XEXP (src, 1));
3259 src = gen_rtx_PLUS (Pmode, sum1, sum2);
3261 /* If the address is already strictly valid, there's nothing to do. */
3262 if (!s390_decompose_address (src, &ad)
3263 || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
3264 || (ad.indx && !REGNO_OK_FOR_INDEX_P (REGNO (ad.indx))))
3266 /* Otherwise, one of the operands cannot be an address register;
3267 we reload its value into the scratch register. */
3268 if (true_regnum (sum1) < 1 || true_regnum (sum1) > 15)
3270 emit_move_insn (scratch, sum1);
3271 sum1 = scratch;
3273 if (true_regnum (sum2) < 1 || true_regnum (sum2) > 15)
3275 emit_move_insn (scratch, sum2);
3276 sum2 = scratch;
3279 /* According to the way these invalid addresses are generated
3280 in reload.c, it should never happen (at least on s390) that
3281 *neither* of the PLUS components, after find_replacements
3282 was applied, is an address register. */
3283 if (sum1 == scratch && sum2 == scratch)
3285 debug_rtx (src);
3286 gcc_unreachable ();
3289 src = gen_rtx_PLUS (Pmode, sum1, sum2);
3292 /* Emit the LOAD ADDRESS pattern. Note that reload of PLUS
3293 is only ever performed on addresses, so we can mark the
3294 sum as legitimate for LA in any case. */
3295 s390_load_address (target, src);
3299 /* Return true if ADDR is a valid memory address.
3300 STRICT specifies whether strict register checking applies. */
3302 static bool
3303 s390_legitimate_address_p (machine_mode mode, rtx addr, bool strict)
3305 struct s390_address ad;
3307 if (TARGET_Z10
3308 && larl_operand (addr, VOIDmode)
3309 && (mode == VOIDmode
3310 || s390_check_symref_alignment (addr, GET_MODE_SIZE (mode))))
3311 return true;
3313 if (!s390_decompose_address (addr, &ad))
3314 return false;
3316 if (strict)
3318 if (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
3319 return false;
3321 if (ad.indx && !REGNO_OK_FOR_INDEX_P (REGNO (ad.indx)))
3322 return false;
3324 else
3326 if (ad.base
3327 && !(REGNO (ad.base) >= FIRST_PSEUDO_REGISTER
3328 || REGNO_REG_CLASS (REGNO (ad.base)) == ADDR_REGS))
3329 return false;
3331 if (ad.indx
3332 && !(REGNO (ad.indx) >= FIRST_PSEUDO_REGISTER
3333 || REGNO_REG_CLASS (REGNO (ad.indx)) == ADDR_REGS))
3334 return false;
3336 return true;
3339 /* Return true if OP is a valid operand for the LA instruction.
3340 In 31-bit, we need to prove that the result is used as an
3341 address, as LA performs only a 31-bit addition. */
3343 bool
3344 legitimate_la_operand_p (rtx op)
3346 struct s390_address addr;
3347 if (!s390_decompose_address (op, &addr))
3348 return false;
3350 return (TARGET_64BIT || addr.pointer);
3353 /* Return true if it is valid *and* preferable to use LA to
3354 compute the sum of OP1 and OP2. */
3356 bool
3357 preferred_la_operand_p (rtx op1, rtx op2)
3359 struct s390_address addr;
3361 if (op2 != const0_rtx)
3362 op1 = gen_rtx_PLUS (Pmode, op1, op2);
3364 if (!s390_decompose_address (op1, &addr))
3365 return false;
3366 if (addr.base && !REGNO_OK_FOR_BASE_P (REGNO (addr.base)))
3367 return false;
3368 if (addr.indx && !REGNO_OK_FOR_INDEX_P (REGNO (addr.indx)))
3369 return false;
3371 /* Avoid LA instructions with index register on z196; it is
3372 preferable to use regular add instructions when possible.
3373 Starting with zEC12 the la with index register is "uncracked"
3374 again. */
3375 if (addr.indx && s390_tune == PROCESSOR_2817_Z196)
3376 return false;
3378 if (!TARGET_64BIT && !addr.pointer)
3379 return false;
3381 if (addr.pointer)
3382 return true;
3384 if ((addr.base && REG_P (addr.base) && REG_POINTER (addr.base))
3385 || (addr.indx && REG_P (addr.indx) && REG_POINTER (addr.indx)))
3386 return true;
3388 return false;
3391 /* Emit a forced load-address operation to load SRC into DST.
3392 This will use the LOAD ADDRESS instruction even in situations
3393 where legitimate_la_operand_p (SRC) returns false. */
3395 void
3396 s390_load_address (rtx dst, rtx src)
3398 if (TARGET_64BIT)
3399 emit_move_insn (dst, src);
3400 else
3401 emit_insn (gen_force_la_31 (dst, src));
3404 /* Return a legitimate reference for ORIG (an address) using the
3405 register REG. If REG is 0, a new pseudo is generated.
3407 There are two types of references that must be handled:
3409 1. Global data references must load the address from the GOT, via
3410 the PIC reg. An insn is emitted to do this load, and the reg is
3411 returned.
3413 2. Static data references, constant pool addresses, and code labels
3414 compute the address as an offset from the GOT, whose base is in
3415 the PIC reg. Static data objects have SYMBOL_FLAG_LOCAL set to
3416 differentiate them from global data objects. The returned
3417 address is the PIC reg + an unspec constant.
3419 TARGET_LEGITIMIZE_ADDRESS_P rejects symbolic references unless the PIC
3420 reg also appears in the address. */
3423 legitimize_pic_address (rtx orig, rtx reg)
3425 rtx addr = orig;
3426 rtx addend = const0_rtx;
3427 rtx new_rtx = orig;
3429 gcc_assert (!TLS_SYMBOLIC_CONST (addr));
3431 if (GET_CODE (addr) == CONST)
3432 addr = XEXP (addr, 0);
3434 if (GET_CODE (addr) == PLUS)
3436 addend = XEXP (addr, 1);
3437 addr = XEXP (addr, 0);
3440 if ((GET_CODE (addr) == LABEL_REF
3441 || (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (addr))
3442 || (GET_CODE (addr) == UNSPEC &&
3443 (XINT (addr, 1) == UNSPEC_GOTENT
3444 || (TARGET_CPU_ZARCH && XINT (addr, 1) == UNSPEC_PLT))))
3445 && GET_CODE (addend) == CONST_INT)
3447 /* This can be locally addressed. */
3449 /* larl_operand requires UNSPECs to be wrapped in a const rtx. */
3450 rtx const_addr = (GET_CODE (addr) == UNSPEC ?
3451 gen_rtx_CONST (Pmode, addr) : addr);
3453 if (TARGET_CPU_ZARCH
3454 && larl_operand (const_addr, VOIDmode)
3455 && INTVAL (addend) < (HOST_WIDE_INT)1 << 31
3456 && INTVAL (addend) >= -((HOST_WIDE_INT)1 << 31))
3458 if (INTVAL (addend) & 1)
3460 /* LARL can't handle odd offsets, so emit a pair of LARL
3461 and LA. */
3462 rtx temp = reg? reg : gen_reg_rtx (Pmode);
3464 if (!DISP_IN_RANGE (INTVAL (addend)))
3466 HOST_WIDE_INT even = INTVAL (addend) - 1;
3467 addr = gen_rtx_PLUS (Pmode, addr, GEN_INT (even));
3468 addr = gen_rtx_CONST (Pmode, addr);
3469 addend = const1_rtx;
3472 emit_move_insn (temp, addr);
3473 new_rtx = gen_rtx_PLUS (Pmode, temp, addend);
3475 if (reg != 0)
3477 s390_load_address (reg, new_rtx);
3478 new_rtx = reg;
3481 else
3483 /* If the offset is even, we can just use LARL. This
3484 will happen automatically. */
3487 else
3489 /* No larl - Access local symbols relative to the GOT. */
3491 rtx temp = reg? reg : gen_reg_rtx (Pmode);
3493 if (reload_in_progress || reload_completed)
3494 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
3496 addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
3497 if (addend != const0_rtx)
3498 addr = gen_rtx_PLUS (Pmode, addr, addend);
3499 addr = gen_rtx_CONST (Pmode, addr);
3500 addr = force_const_mem (Pmode, addr);
3501 emit_move_insn (temp, addr);
3503 new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
3504 if (reg != 0)
3506 s390_load_address (reg, new_rtx);
3507 new_rtx = reg;
3511 else if (GET_CODE (addr) == SYMBOL_REF && addend == const0_rtx)
3513 /* A non-local symbol reference without addend.
3515 The symbol ref is wrapped into an UNSPEC to make sure the
3516 proper operand modifier (@GOT or @GOTENT) will be emitted.
3517 This will tell the linker to put the symbol into the GOT.
3519 Additionally the code dereferencing the GOT slot is emitted here.
3521 An addend to the symref needs to be added afterwards.
3522 legitimize_pic_address calls itself recursively to handle
3523 that case. So no need to do it here. */
3525 if (reg == 0)
3526 reg = gen_reg_rtx (Pmode);
3528 if (TARGET_Z10)
3530 /* Use load relative if possible.
3531 lgrl <target>, sym@GOTENT */
3532 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTENT);
3533 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
3534 new_rtx = gen_const_mem (GET_MODE (reg), new_rtx);
3536 emit_move_insn (reg, new_rtx);
3537 new_rtx = reg;
3539 else if (flag_pic == 1)
3541 /* Assume GOT offset is a valid displacement operand (< 4k
3542 or < 512k with z990). This is handled the same way in
3543 both 31- and 64-bit code (@GOT).
3544 lg <target>, sym@GOT(r12) */
3546 if (reload_in_progress || reload_completed)
3547 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
3549 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
3550 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
3551 new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
3552 new_rtx = gen_const_mem (Pmode, new_rtx);
3553 emit_move_insn (reg, new_rtx);
3554 new_rtx = reg;
3556 else if (TARGET_CPU_ZARCH)
3558 /* If the GOT offset might be >= 4k, we determine the position
3559 of the GOT entry via a PC-relative LARL (@GOTENT).
3560 larl temp, sym@GOTENT
3561 lg <target>, 0(temp) */
3563 rtx temp = reg ? reg : gen_reg_rtx (Pmode);
3565 gcc_assert (REGNO (temp) >= FIRST_PSEUDO_REGISTER
3566 || REGNO_REG_CLASS (REGNO (temp)) == ADDR_REGS);
3568 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTENT);
3569 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
3570 emit_move_insn (temp, new_rtx);
3572 new_rtx = gen_const_mem (Pmode, temp);
3573 emit_move_insn (reg, new_rtx);
3575 new_rtx = reg;
3577 else
3579 /* If the GOT offset might be >= 4k, we have to load it
3580 from the literal pool (@GOT).
3582 lg temp, lit-litbase(r13)
3583 lg <target>, 0(temp)
3584 lit: .long sym@GOT */
3586 rtx temp = reg ? reg : gen_reg_rtx (Pmode);
3588 gcc_assert (REGNO (temp) >= FIRST_PSEUDO_REGISTER
3589 || REGNO_REG_CLASS (REGNO (temp)) == ADDR_REGS);
3591 if (reload_in_progress || reload_completed)
3592 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
3594 addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
3595 addr = gen_rtx_CONST (Pmode, addr);
3596 addr = force_const_mem (Pmode, addr);
3597 emit_move_insn (temp, addr);
3599 new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
3600 new_rtx = gen_const_mem (Pmode, new_rtx);
3601 emit_move_insn (reg, new_rtx);
3602 new_rtx = reg;
3605 else if (GET_CODE (addr) == UNSPEC && GET_CODE (addend) == CONST_INT)
3607 gcc_assert (XVECLEN (addr, 0) == 1);
3608 switch (XINT (addr, 1))
3610 /* These address symbols (or PLT slots) relative to the GOT
3611 (not GOT slots!). In general this will exceed the
3612 displacement range so these value belong into the literal
3613 pool. */
3614 case UNSPEC_GOTOFF:
3615 case UNSPEC_PLTOFF:
3616 new_rtx = force_const_mem (Pmode, orig);
3617 break;
3619 /* For -fPIC the GOT size might exceed the displacement
3620 range so make sure the value is in the literal pool. */
3621 case UNSPEC_GOT:
3622 if (flag_pic == 2)
3623 new_rtx = force_const_mem (Pmode, orig);
3624 break;
3626 /* For @GOTENT larl is used. This is handled like local
3627 symbol refs. */
3628 case UNSPEC_GOTENT:
3629 gcc_unreachable ();
3630 break;
3632 /* @PLT is OK as is on 64-bit, must be converted to
3633 GOT-relative @PLTOFF on 31-bit. */
3634 case UNSPEC_PLT:
3635 if (!TARGET_CPU_ZARCH)
3637 rtx temp = reg? reg : gen_reg_rtx (Pmode);
3639 if (reload_in_progress || reload_completed)
3640 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
3642 addr = XVECEXP (addr, 0, 0);
3643 addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr),
3644 UNSPEC_PLTOFF);
3645 if (addend != const0_rtx)
3646 addr = gen_rtx_PLUS (Pmode, addr, addend);
3647 addr = gen_rtx_CONST (Pmode, addr);
3648 addr = force_const_mem (Pmode, addr);
3649 emit_move_insn (temp, addr);
3651 new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
3652 if (reg != 0)
3654 s390_load_address (reg, new_rtx);
3655 new_rtx = reg;
3658 else
3659 /* On 64 bit larl can be used. This case is handled like
3660 local symbol refs. */
3661 gcc_unreachable ();
3662 break;
3664 /* Everything else cannot happen. */
3665 default:
3666 gcc_unreachable ();
3669 else if (addend != const0_rtx)
3671 /* Otherwise, compute the sum. */
3673 rtx base = legitimize_pic_address (addr, reg);
3674 new_rtx = legitimize_pic_address (addend,
3675 base == reg ? NULL_RTX : reg);
3676 if (GET_CODE (new_rtx) == CONST_INT)
3677 new_rtx = plus_constant (Pmode, base, INTVAL (new_rtx));
3678 else
3680 if (GET_CODE (new_rtx) == PLUS && CONSTANT_P (XEXP (new_rtx, 1)))
3682 base = gen_rtx_PLUS (Pmode, base, XEXP (new_rtx, 0));
3683 new_rtx = XEXP (new_rtx, 1);
3685 new_rtx = gen_rtx_PLUS (Pmode, base, new_rtx);
3688 if (GET_CODE (new_rtx) == CONST)
3689 new_rtx = XEXP (new_rtx, 0);
3690 new_rtx = force_operand (new_rtx, 0);
3693 return new_rtx;
3696 /* Load the thread pointer into a register. */
3699 s390_get_thread_pointer (void)
3701 rtx tp = gen_reg_rtx (Pmode);
3703 emit_move_insn (tp, gen_rtx_REG (Pmode, TP_REGNUM));
3704 mark_reg_pointer (tp, BITS_PER_WORD);
3706 return tp;
3709 /* Emit a tls call insn. The call target is the SYMBOL_REF stored
3710 in s390_tls_symbol which always refers to __tls_get_offset.
3711 The returned offset is written to RESULT_REG and an USE rtx is
3712 generated for TLS_CALL. */
3714 static GTY(()) rtx s390_tls_symbol;
3716 static void
3717 s390_emit_tls_call_insn (rtx result_reg, rtx tls_call)
3719 rtx insn;
3721 if (!flag_pic)
3722 emit_insn (s390_load_got ());
3724 if (!s390_tls_symbol)
3725 s390_tls_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tls_get_offset");
3727 insn = s390_emit_call (s390_tls_symbol, tls_call, result_reg,
3728 gen_rtx_REG (Pmode, RETURN_REGNUM));
3730 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), result_reg);
3731 RTL_CONST_CALL_P (insn) = 1;
3734 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
3735 this (thread-local) address. REG may be used as temporary. */
3737 static rtx
3738 legitimize_tls_address (rtx addr, rtx reg)
3740 rtx new_rtx, tls_call, temp, base, r2, insn;
3742 if (GET_CODE (addr) == SYMBOL_REF)
3743 switch (tls_symbolic_operand (addr))
3745 case TLS_MODEL_GLOBAL_DYNAMIC:
3746 start_sequence ();
3747 r2 = gen_rtx_REG (Pmode, 2);
3748 tls_call = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_TLSGD);
3749 new_rtx = gen_rtx_CONST (Pmode, tls_call);
3750 new_rtx = force_const_mem (Pmode, new_rtx);
3751 emit_move_insn (r2, new_rtx);
3752 s390_emit_tls_call_insn (r2, tls_call);
3753 insn = get_insns ();
3754 end_sequence ();
3756 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_NTPOFF);
3757 temp = gen_reg_rtx (Pmode);
3758 emit_libcall_block (insn, temp, r2, new_rtx);
3760 new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
3761 if (reg != 0)
3763 s390_load_address (reg, new_rtx);
3764 new_rtx = reg;
3766 break;
3768 case TLS_MODEL_LOCAL_DYNAMIC:
3769 start_sequence ();
3770 r2 = gen_rtx_REG (Pmode, 2);
3771 tls_call = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TLSLDM);
3772 new_rtx = gen_rtx_CONST (Pmode, tls_call);
3773 new_rtx = force_const_mem (Pmode, new_rtx);
3774 emit_move_insn (r2, new_rtx);
3775 s390_emit_tls_call_insn (r2, tls_call);
3776 insn = get_insns ();
3777 end_sequence ();
3779 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TLSLDM_NTPOFF);
3780 temp = gen_reg_rtx (Pmode);
3781 emit_libcall_block (insn, temp, r2, new_rtx);
3783 new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
3784 base = gen_reg_rtx (Pmode);
3785 s390_load_address (base, new_rtx);
3787 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_DTPOFF);
3788 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
3789 new_rtx = force_const_mem (Pmode, new_rtx);
3790 temp = gen_reg_rtx (Pmode);
3791 emit_move_insn (temp, new_rtx);
3793 new_rtx = gen_rtx_PLUS (Pmode, base, temp);
3794 if (reg != 0)
3796 s390_load_address (reg, new_rtx);
3797 new_rtx = reg;
3799 break;
3801 case TLS_MODEL_INITIAL_EXEC:
3802 if (flag_pic == 1)
3804 /* Assume GOT offset < 4k. This is handled the same way
3805 in both 31- and 64-bit code. */
3807 if (reload_in_progress || reload_completed)
3808 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
3810 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTNTPOFF);
3811 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
3812 new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
3813 new_rtx = gen_const_mem (Pmode, new_rtx);
3814 temp = gen_reg_rtx (Pmode);
3815 emit_move_insn (temp, new_rtx);
3817 else if (TARGET_CPU_ZARCH)
3819 /* If the GOT offset might be >= 4k, we determine the position
3820 of the GOT entry via a PC-relative LARL. */
3822 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_INDNTPOFF);
3823 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
3824 temp = gen_reg_rtx (Pmode);
3825 emit_move_insn (temp, new_rtx);
3827 new_rtx = gen_const_mem (Pmode, temp);
3828 temp = gen_reg_rtx (Pmode);
3829 emit_move_insn (temp, new_rtx);
3831 else if (flag_pic)
3833 /* If the GOT offset might be >= 4k, we have to load it
3834 from the literal pool. */
3836 if (reload_in_progress || reload_completed)
3837 df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
3839 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTNTPOFF);
3840 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
3841 new_rtx = force_const_mem (Pmode, new_rtx);
3842 temp = gen_reg_rtx (Pmode);
3843 emit_move_insn (temp, new_rtx);
3845 new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
3846 new_rtx = gen_const_mem (Pmode, new_rtx);
3848 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, new_rtx, addr), UNSPEC_TLS_LOAD);
3849 temp = gen_reg_rtx (Pmode);
3850 emit_insn (gen_rtx_SET (Pmode, temp, new_rtx));
3852 else
3854 /* In position-dependent code, load the absolute address of
3855 the GOT entry from the literal pool. */
3857 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_INDNTPOFF);
3858 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
3859 new_rtx = force_const_mem (Pmode, new_rtx);
3860 temp = gen_reg_rtx (Pmode);
3861 emit_move_insn (temp, new_rtx);
3863 new_rtx = temp;
3864 new_rtx = gen_const_mem (Pmode, new_rtx);
3865 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, new_rtx, addr), UNSPEC_TLS_LOAD);
3866 temp = gen_reg_rtx (Pmode);
3867 emit_insn (gen_rtx_SET (Pmode, temp, new_rtx));
3870 new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
3871 if (reg != 0)
3873 s390_load_address (reg, new_rtx);
3874 new_rtx = reg;
3876 break;
3878 case TLS_MODEL_LOCAL_EXEC:
3879 new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_NTPOFF);
3880 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
3881 new_rtx = force_const_mem (Pmode, new_rtx);
3882 temp = gen_reg_rtx (Pmode);
3883 emit_move_insn (temp, new_rtx);
3885 new_rtx = gen_rtx_PLUS (Pmode, s390_get_thread_pointer (), temp);
3886 if (reg != 0)
3888 s390_load_address (reg, new_rtx);
3889 new_rtx = reg;
3891 break;
3893 default:
3894 gcc_unreachable ();
3897 else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == UNSPEC)
3899 switch (XINT (XEXP (addr, 0), 1))
3901 case UNSPEC_INDNTPOFF:
3902 gcc_assert (TARGET_CPU_ZARCH);
3903 new_rtx = addr;
3904 break;
3906 default:
3907 gcc_unreachable ();
3911 else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS
3912 && GET_CODE (XEXP (XEXP (addr, 0), 1)) == CONST_INT)
3914 new_rtx = XEXP (XEXP (addr, 0), 0);
3915 if (GET_CODE (new_rtx) != SYMBOL_REF)
3916 new_rtx = gen_rtx_CONST (Pmode, new_rtx);
3918 new_rtx = legitimize_tls_address (new_rtx, reg);
3919 new_rtx = plus_constant (Pmode, new_rtx,
3920 INTVAL (XEXP (XEXP (addr, 0), 1)));
3921 new_rtx = force_operand (new_rtx, 0);
3924 else
3925 gcc_unreachable (); /* for now ... */
3927 return new_rtx;
3930 /* Emit insns making the address in operands[1] valid for a standard
3931 move to operands[0]. operands[1] is replaced by an address which
3932 should be used instead of the former RTX to emit the move
3933 pattern. */
3935 void
3936 emit_symbolic_move (rtx *operands)
3938 rtx temp = !can_create_pseudo_p () ? operands[0] : gen_reg_rtx (Pmode);
3940 if (GET_CODE (operands[0]) == MEM)
3941 operands[1] = force_reg (Pmode, operands[1]);
3942 else if (TLS_SYMBOLIC_CONST (operands[1]))
3943 operands[1] = legitimize_tls_address (operands[1], temp);
3944 else if (flag_pic)
3945 operands[1] = legitimize_pic_address (operands[1], temp);
3948 /* Try machine-dependent ways of modifying an illegitimate address X
3949 to be legitimate. If we find one, return the new, valid address.
3951 OLDX is the address as it was before break_out_memory_refs was called.
3952 In some cases it is useful to look at this to decide what needs to be done.
3954 MODE is the mode of the operand pointed to by X.
3956 When -fpic is used, special handling is needed for symbolic references.
3957 See comments by legitimize_pic_address for details. */
3959 static rtx
3960 s390_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
3961 machine_mode mode ATTRIBUTE_UNUSED)
3963 rtx constant_term = const0_rtx;
3965 if (TLS_SYMBOLIC_CONST (x))
3967 x = legitimize_tls_address (x, 0);
3969 if (s390_legitimate_address_p (mode, x, FALSE))
3970 return x;
3972 else if (GET_CODE (x) == PLUS
3973 && (TLS_SYMBOLIC_CONST (XEXP (x, 0))
3974 || TLS_SYMBOLIC_CONST (XEXP (x, 1))))
3976 return x;
3978 else if (flag_pic)
3980 if (SYMBOLIC_CONST (x)
3981 || (GET_CODE (x) == PLUS
3982 && (SYMBOLIC_CONST (XEXP (x, 0))
3983 || SYMBOLIC_CONST (XEXP (x, 1)))))
3984 x = legitimize_pic_address (x, 0);
3986 if (s390_legitimate_address_p (mode, x, FALSE))
3987 return x;
3990 x = eliminate_constant_term (x, &constant_term);
3992 /* Optimize loading of large displacements by splitting them
3993 into the multiple of 4K and the rest; this allows the
3994 former to be CSE'd if possible.
3996 Don't do this if the displacement is added to a register
3997 pointing into the stack frame, as the offsets will
3998 change later anyway. */
4000 if (GET_CODE (constant_term) == CONST_INT
4001 && !TARGET_LONG_DISPLACEMENT
4002 && !DISP_IN_RANGE (INTVAL (constant_term))
4003 && !(REG_P (x) && REGNO_PTR_FRAME_P (REGNO (x))))
4005 HOST_WIDE_INT lower = INTVAL (constant_term) & 0xfff;
4006 HOST_WIDE_INT upper = INTVAL (constant_term) ^ lower;
4008 rtx temp = gen_reg_rtx (Pmode);
4009 rtx val = force_operand (GEN_INT (upper), temp);
4010 if (val != temp)
4011 emit_move_insn (temp, val);
4013 x = gen_rtx_PLUS (Pmode, x, temp);
4014 constant_term = GEN_INT (lower);
4017 if (GET_CODE (x) == PLUS)
4019 if (GET_CODE (XEXP (x, 0)) == REG)
4021 rtx temp = gen_reg_rtx (Pmode);
4022 rtx val = force_operand (XEXP (x, 1), temp);
4023 if (val != temp)
4024 emit_move_insn (temp, val);
4026 x = gen_rtx_PLUS (Pmode, XEXP (x, 0), temp);
4029 else if (GET_CODE (XEXP (x, 1)) == REG)
4031 rtx temp = gen_reg_rtx (Pmode);
4032 rtx val = force_operand (XEXP (x, 0), temp);
4033 if (val != temp)
4034 emit_move_insn (temp, val);
4036 x = gen_rtx_PLUS (Pmode, temp, XEXP (x, 1));
4040 if (constant_term != const0_rtx)
4041 x = gen_rtx_PLUS (Pmode, x, constant_term);
4043 return x;
4046 /* Try a machine-dependent way of reloading an illegitimate address AD
4047 operand. If we find one, push the reload and return the new address.
4049 MODE is the mode of the enclosing MEM. OPNUM is the operand number
4050 and TYPE is the reload type of the current reload. */
4053 legitimize_reload_address (rtx ad, machine_mode mode ATTRIBUTE_UNUSED,
4054 int opnum, int type)
4056 if (!optimize || TARGET_LONG_DISPLACEMENT)
4057 return NULL_RTX;
4059 if (GET_CODE (ad) == PLUS)
4061 rtx tem = simplify_binary_operation (PLUS, Pmode,
4062 XEXP (ad, 0), XEXP (ad, 1));
4063 if (tem)
4064 ad = tem;
4067 if (GET_CODE (ad) == PLUS
4068 && GET_CODE (XEXP (ad, 0)) == REG
4069 && GET_CODE (XEXP (ad, 1)) == CONST_INT
4070 && !DISP_IN_RANGE (INTVAL (XEXP (ad, 1))))
4072 HOST_WIDE_INT lower = INTVAL (XEXP (ad, 1)) & 0xfff;
4073 HOST_WIDE_INT upper = INTVAL (XEXP (ad, 1)) ^ lower;
4074 rtx cst, tem, new_rtx;
4076 cst = GEN_INT (upper);
4077 if (!legitimate_reload_constant_p (cst))
4078 cst = force_const_mem (Pmode, cst);
4080 tem = gen_rtx_PLUS (Pmode, XEXP (ad, 0), cst);
4081 new_rtx = gen_rtx_PLUS (Pmode, tem, GEN_INT (lower));
4083 push_reload (XEXP (tem, 1), 0, &XEXP (tem, 1), 0,
4084 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
4085 opnum, (enum reload_type) type);
4086 return new_rtx;
4089 return NULL_RTX;
4092 /* Emit code to move LEN bytes from DST to SRC. */
4094 bool
4095 s390_expand_movmem (rtx dst, rtx src, rtx len)
4097 /* When tuning for z10 or higher we rely on the Glibc functions to
4098 do the right thing. Only for constant lengths below 64k we will
4099 generate inline code. */
4100 if (s390_tune >= PROCESSOR_2097_Z10
4101 && (GET_CODE (len) != CONST_INT || INTVAL (len) > (1<<16)))
4102 return false;
4104 if (GET_CODE (len) == CONST_INT && INTVAL (len) >= 0 && INTVAL (len) <= 256)
4106 if (INTVAL (len) > 0)
4107 emit_insn (gen_movmem_short (dst, src, GEN_INT (INTVAL (len) - 1)));
4110 else if (TARGET_MVCLE)
4112 emit_insn (gen_movmem_long (dst, src, convert_to_mode (Pmode, len, 1)));
4115 else
4117 rtx dst_addr, src_addr, count, blocks, temp;
4118 rtx_code_label *loop_start_label = gen_label_rtx ();
4119 rtx_code_label *loop_end_label = gen_label_rtx ();
4120 rtx_code_label *end_label = gen_label_rtx ();
4121 machine_mode mode;
4123 mode = GET_MODE (len);
4124 if (mode == VOIDmode)
4125 mode = Pmode;
4127 dst_addr = gen_reg_rtx (Pmode);
4128 src_addr = gen_reg_rtx (Pmode);
4129 count = gen_reg_rtx (mode);
4130 blocks = gen_reg_rtx (mode);
4132 convert_move (count, len, 1);
4133 emit_cmp_and_jump_insns (count, const0_rtx,
4134 EQ, NULL_RTX, mode, 1, end_label);
4136 emit_move_insn (dst_addr, force_operand (XEXP (dst, 0), NULL_RTX));
4137 emit_move_insn (src_addr, force_operand (XEXP (src, 0), NULL_RTX));
4138 dst = change_address (dst, VOIDmode, dst_addr);
4139 src = change_address (src, VOIDmode, src_addr);
4141 temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1,
4142 OPTAB_DIRECT);
4143 if (temp != count)
4144 emit_move_insn (count, temp);
4146 temp = expand_binop (mode, lshr_optab, count, GEN_INT (8), blocks, 1,
4147 OPTAB_DIRECT);
4148 if (temp != blocks)
4149 emit_move_insn (blocks, temp);
4151 emit_cmp_and_jump_insns (blocks, const0_rtx,
4152 EQ, NULL_RTX, mode, 1, loop_end_label);
4154 emit_label (loop_start_label);
4156 if (TARGET_Z10
4157 && (GET_CODE (len) != CONST_INT || INTVAL (len) > 768))
4159 rtx prefetch;
4161 /* Issue a read prefetch for the +3 cache line. */
4162 prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, src_addr, GEN_INT (768)),
4163 const0_rtx, const0_rtx);
4164 PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
4165 emit_insn (prefetch);
4167 /* Issue a write prefetch for the +3 cache line. */
4168 prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (768)),
4169 const1_rtx, const0_rtx);
4170 PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
4171 emit_insn (prefetch);
4174 emit_insn (gen_movmem_short (dst, src, GEN_INT (255)));
4175 s390_load_address (dst_addr,
4176 gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (256)));
4177 s390_load_address (src_addr,
4178 gen_rtx_PLUS (Pmode, src_addr, GEN_INT (256)));
4180 temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1,
4181 OPTAB_DIRECT);
4182 if (temp != blocks)
4183 emit_move_insn (blocks, temp);
4185 emit_cmp_and_jump_insns (blocks, const0_rtx,
4186 EQ, NULL_RTX, mode, 1, loop_end_label);
4188 emit_jump (loop_start_label);
4189 emit_label (loop_end_label);
4191 emit_insn (gen_movmem_short (dst, src,
4192 convert_to_mode (Pmode, count, 1)));
4193 emit_label (end_label);
4195 return true;
4198 /* Emit code to set LEN bytes at DST to VAL.
4199 Make use of clrmem if VAL is zero. */
4201 void
4202 s390_expand_setmem (rtx dst, rtx len, rtx val)
4204 if (GET_CODE (len) == CONST_INT && INTVAL (len) == 0)
4205 return;
4207 gcc_assert (GET_CODE (val) == CONST_INT || GET_MODE (val) == QImode);
4209 if (GET_CODE (len) == CONST_INT && INTVAL (len) > 0 && INTVAL (len) <= 257)
4211 if (val == const0_rtx && INTVAL (len) <= 256)
4212 emit_insn (gen_clrmem_short (dst, GEN_INT (INTVAL (len) - 1)));
4213 else
4215 /* Initialize memory by storing the first byte. */
4216 emit_move_insn (adjust_address (dst, QImode, 0), val);
4218 if (INTVAL (len) > 1)
4220 /* Initiate 1 byte overlap move.
4221 The first byte of DST is propagated through DSTP1.
4222 Prepare a movmem for: DST+1 = DST (length = LEN - 1).
4223 DST is set to size 1 so the rest of the memory location
4224 does not count as source operand. */
4225 rtx dstp1 = adjust_address (dst, VOIDmode, 1);
4226 set_mem_size (dst, 1);
4228 emit_insn (gen_movmem_short (dstp1, dst,
4229 GEN_INT (INTVAL (len) - 2)));
4234 else if (TARGET_MVCLE)
4236 val = force_not_mem (convert_modes (Pmode, QImode, val, 1));
4237 emit_insn (gen_setmem_long (dst, convert_to_mode (Pmode, len, 1), val));
4240 else
4242 rtx dst_addr, count, blocks, temp, dstp1 = NULL_RTX;
4243 rtx_code_label *loop_start_label = gen_label_rtx ();
4244 rtx_code_label *loop_end_label = gen_label_rtx ();
4245 rtx_code_label *end_label = gen_label_rtx ();
4246 machine_mode mode;
4248 mode = GET_MODE (len);
4249 if (mode == VOIDmode)
4250 mode = Pmode;
4252 dst_addr = gen_reg_rtx (Pmode);
4253 count = gen_reg_rtx (mode);
4254 blocks = gen_reg_rtx (mode);
4256 convert_move (count, len, 1);
4257 emit_cmp_and_jump_insns (count, const0_rtx,
4258 EQ, NULL_RTX, mode, 1, end_label);
4260 emit_move_insn (dst_addr, force_operand (XEXP (dst, 0), NULL_RTX));
4261 dst = change_address (dst, VOIDmode, dst_addr);
4263 if (val == const0_rtx)
4264 temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1,
4265 OPTAB_DIRECT);
4266 else
4268 dstp1 = adjust_address (dst, VOIDmode, 1);
4269 set_mem_size (dst, 1);
4271 /* Initialize memory by storing the first byte. */
4272 emit_move_insn (adjust_address (dst, QImode, 0), val);
4274 /* If count is 1 we are done. */
4275 emit_cmp_and_jump_insns (count, const1_rtx,
4276 EQ, NULL_RTX, mode, 1, end_label);
4278 temp = expand_binop (mode, add_optab, count, GEN_INT (-2), count, 1,
4279 OPTAB_DIRECT);
4281 if (temp != count)
4282 emit_move_insn (count, temp);
4284 temp = expand_binop (mode, lshr_optab, count, GEN_INT (8), blocks, 1,
4285 OPTAB_DIRECT);
4286 if (temp != blocks)
4287 emit_move_insn (blocks, temp);
4289 emit_cmp_and_jump_insns (blocks, const0_rtx,
4290 EQ, NULL_RTX, mode, 1, loop_end_label);
4292 emit_label (loop_start_label);
4294 if (TARGET_Z10
4295 && (GET_CODE (len) != CONST_INT || INTVAL (len) > 1024))
4297 /* Issue a write prefetch for the +4 cache line. */
4298 rtx prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, dst_addr,
4299 GEN_INT (1024)),
4300 const1_rtx, const0_rtx);
4301 emit_insn (prefetch);
4302 PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
4305 if (val == const0_rtx)
4306 emit_insn (gen_clrmem_short (dst, GEN_INT (255)));
4307 else
4308 emit_insn (gen_movmem_short (dstp1, dst, GEN_INT (255)));
4309 s390_load_address (dst_addr,
4310 gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (256)));
4312 temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1,
4313 OPTAB_DIRECT);
4314 if (temp != blocks)
4315 emit_move_insn (blocks, temp);
4317 emit_cmp_and_jump_insns (blocks, const0_rtx,
4318 EQ, NULL_RTX, mode, 1, loop_end_label);
4320 emit_jump (loop_start_label);
4321 emit_label (loop_end_label);
4323 if (val == const0_rtx)
4324 emit_insn (gen_clrmem_short (dst, convert_to_mode (Pmode, count, 1)));
4325 else
4326 emit_insn (gen_movmem_short (dstp1, dst, convert_to_mode (Pmode, count, 1)));
4327 emit_label (end_label);
4331 /* Emit code to compare LEN bytes at OP0 with those at OP1,
4332 and return the result in TARGET. */
4334 bool
4335 s390_expand_cmpmem (rtx target, rtx op0, rtx op1, rtx len)
4337 rtx ccreg = gen_rtx_REG (CCUmode, CC_REGNUM);
4338 rtx tmp;
4340 /* When tuning for z10 or higher we rely on the Glibc functions to
4341 do the right thing. Only for constant lengths below 64k we will
4342 generate inline code. */
4343 if (s390_tune >= PROCESSOR_2097_Z10
4344 && (GET_CODE (len) != CONST_INT || INTVAL (len) > (1<<16)))
4345 return false;
4347 /* As the result of CMPINT is inverted compared to what we need,
4348 we have to swap the operands. */
4349 tmp = op0; op0 = op1; op1 = tmp;
4351 if (GET_CODE (len) == CONST_INT && INTVAL (len) >= 0 && INTVAL (len) <= 256)
4353 if (INTVAL (len) > 0)
4355 emit_insn (gen_cmpmem_short (op0, op1, GEN_INT (INTVAL (len) - 1)));
4356 emit_insn (gen_cmpint (target, ccreg));
4358 else
4359 emit_move_insn (target, const0_rtx);
4361 else if (TARGET_MVCLE)
4363 emit_insn (gen_cmpmem_long (op0, op1, convert_to_mode (Pmode, len, 1)));
4364 emit_insn (gen_cmpint (target, ccreg));
4366 else
4368 rtx addr0, addr1, count, blocks, temp;
4369 rtx_code_label *loop_start_label = gen_label_rtx ();
4370 rtx_code_label *loop_end_label = gen_label_rtx ();
4371 rtx_code_label *end_label = gen_label_rtx ();
4372 machine_mode mode;
4374 mode = GET_MODE (len);
4375 if (mode == VOIDmode)
4376 mode = Pmode;
4378 addr0 = gen_reg_rtx (Pmode);
4379 addr1 = gen_reg_rtx (Pmode);
4380 count = gen_reg_rtx (mode);
4381 blocks = gen_reg_rtx (mode);
4383 convert_move (count, len, 1);
4384 emit_cmp_and_jump_insns (count, const0_rtx,
4385 EQ, NULL_RTX, mode, 1, end_label);
4387 emit_move_insn (addr0, force_operand (XEXP (op0, 0), NULL_RTX));
4388 emit_move_insn (addr1, force_operand (XEXP (op1, 0), NULL_RTX));
4389 op0 = change_address (op0, VOIDmode, addr0);
4390 op1 = change_address (op1, VOIDmode, addr1);
4392 temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1,
4393 OPTAB_DIRECT);
4394 if (temp != count)
4395 emit_move_insn (count, temp);
4397 temp = expand_binop (mode, lshr_optab, count, GEN_INT (8), blocks, 1,
4398 OPTAB_DIRECT);
4399 if (temp != blocks)
4400 emit_move_insn (blocks, temp);
4402 emit_cmp_and_jump_insns (blocks, const0_rtx,
4403 EQ, NULL_RTX, mode, 1, loop_end_label);
4405 emit_label (loop_start_label);
4407 if (TARGET_Z10
4408 && (GET_CODE (len) != CONST_INT || INTVAL (len) > 512))
4410 rtx prefetch;
4412 /* Issue a read prefetch for the +2 cache line of operand 1. */
4413 prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, addr0, GEN_INT (512)),
4414 const0_rtx, const0_rtx);
4415 emit_insn (prefetch);
4416 PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
4418 /* Issue a read prefetch for the +2 cache line of operand 2. */
4419 prefetch = gen_prefetch (gen_rtx_PLUS (Pmode, addr1, GEN_INT (512)),
4420 const0_rtx, const0_rtx);
4421 emit_insn (prefetch);
4422 PREFETCH_SCHEDULE_BARRIER_P (prefetch) = true;
4425 emit_insn (gen_cmpmem_short (op0, op1, GEN_INT (255)));
4426 temp = gen_rtx_NE (VOIDmode, ccreg, const0_rtx);
4427 temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
4428 gen_rtx_LABEL_REF (VOIDmode, end_label), pc_rtx);
4429 temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
4430 emit_jump_insn (temp);
4432 s390_load_address (addr0,
4433 gen_rtx_PLUS (Pmode, addr0, GEN_INT (256)));
4434 s390_load_address (addr1,
4435 gen_rtx_PLUS (Pmode, addr1, GEN_INT (256)));
4437 temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1,
4438 OPTAB_DIRECT);
4439 if (temp != blocks)
4440 emit_move_insn (blocks, temp);
4442 emit_cmp_and_jump_insns (blocks, const0_rtx,
4443 EQ, NULL_RTX, mode, 1, loop_end_label);
4445 emit_jump (loop_start_label);
4446 emit_label (loop_end_label);
4448 emit_insn (gen_cmpmem_short (op0, op1,
4449 convert_to_mode (Pmode, count, 1)));
4450 emit_label (end_label);
4452 emit_insn (gen_cmpint (target, ccreg));
4454 return true;
4458 /* Expand conditional increment or decrement using alc/slb instructions.
4459 Should generate code setting DST to either SRC or SRC + INCREMENT,
4460 depending on the result of the comparison CMP_OP0 CMP_CODE CMP_OP1.
4461 Returns true if successful, false otherwise.
4463 That makes it possible to implement some if-constructs without jumps e.g.:
4464 (borrow = CC0 | CC1 and carry = CC2 | CC3)
4465 unsigned int a, b, c;
4466 if (a < b) c++; -> CCU b > a -> CC2; c += carry;
4467 if (a < b) c--; -> CCL3 a - b -> borrow; c -= borrow;
4468 if (a <= b) c++; -> CCL3 b - a -> borrow; c += carry;
4469 if (a <= b) c--; -> CCU a <= b -> borrow; c -= borrow;
4471 Checks for EQ and NE with a nonzero value need an additional xor e.g.:
4472 if (a == b) c++; -> CCL3 a ^= b; 0 - a -> borrow; c += carry;
4473 if (a == b) c--; -> CCU a ^= b; a <= 0 -> CC0 | CC1; c -= borrow;
4474 if (a != b) c++; -> CCU a ^= b; a > 0 -> CC2; c += carry;
4475 if (a != b) c--; -> CCL3 a ^= b; 0 - a -> borrow; c -= borrow; */
4477 bool
4478 s390_expand_addcc (enum rtx_code cmp_code, rtx cmp_op0, rtx cmp_op1,
4479 rtx dst, rtx src, rtx increment)
4481 machine_mode cmp_mode;
4482 machine_mode cc_mode;
4483 rtx op_res;
4484 rtx insn;
4485 rtvec p;
4486 int ret;
4488 if ((GET_MODE (cmp_op0) == SImode || GET_MODE (cmp_op0) == VOIDmode)
4489 && (GET_MODE (cmp_op1) == SImode || GET_MODE (cmp_op1) == VOIDmode))
4490 cmp_mode = SImode;
4491 else if ((GET_MODE (cmp_op0) == DImode || GET_MODE (cmp_op0) == VOIDmode)
4492 && (GET_MODE (cmp_op1) == DImode || GET_MODE (cmp_op1) == VOIDmode))
4493 cmp_mode = DImode;
4494 else
4495 return false;
4497 /* Try ADD LOGICAL WITH CARRY. */
4498 if (increment == const1_rtx)
4500 /* Determine CC mode to use. */
4501 if (cmp_code == EQ || cmp_code == NE)
4503 if (cmp_op1 != const0_rtx)
4505 cmp_op0 = expand_simple_binop (cmp_mode, XOR, cmp_op0, cmp_op1,
4506 NULL_RTX, 0, OPTAB_WIDEN);
4507 cmp_op1 = const0_rtx;
4510 cmp_code = cmp_code == EQ ? LEU : GTU;
4513 if (cmp_code == LTU || cmp_code == LEU)
4515 rtx tem = cmp_op0;
4516 cmp_op0 = cmp_op1;
4517 cmp_op1 = tem;
4518 cmp_code = swap_condition (cmp_code);
4521 switch (cmp_code)
4523 case GTU:
4524 cc_mode = CCUmode;
4525 break;
4527 case GEU:
4528 cc_mode = CCL3mode;
4529 break;
4531 default:
4532 return false;
4535 /* Emit comparison instruction pattern. */
4536 if (!register_operand (cmp_op0, cmp_mode))
4537 cmp_op0 = force_reg (cmp_mode, cmp_op0);
4539 insn = gen_rtx_SET (VOIDmode, gen_rtx_REG (cc_mode, CC_REGNUM),
4540 gen_rtx_COMPARE (cc_mode, cmp_op0, cmp_op1));
4541 /* We use insn_invalid_p here to add clobbers if required. */
4542 ret = insn_invalid_p (emit_insn (insn), false);
4543 gcc_assert (!ret);
4545 /* Emit ALC instruction pattern. */
4546 op_res = gen_rtx_fmt_ee (cmp_code, GET_MODE (dst),
4547 gen_rtx_REG (cc_mode, CC_REGNUM),
4548 const0_rtx);
4550 if (src != const0_rtx)
4552 if (!register_operand (src, GET_MODE (dst)))
4553 src = force_reg (GET_MODE (dst), src);
4555 op_res = gen_rtx_PLUS (GET_MODE (dst), op_res, src);
4556 op_res = gen_rtx_PLUS (GET_MODE (dst), op_res, const0_rtx);
4559 p = rtvec_alloc (2);
4560 RTVEC_ELT (p, 0) =
4561 gen_rtx_SET (VOIDmode, dst, op_res);
4562 RTVEC_ELT (p, 1) =
4563 gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
4564 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
4566 return true;
4569 /* Try SUBTRACT LOGICAL WITH BORROW. */
4570 if (increment == constm1_rtx)
4572 /* Determine CC mode to use. */
4573 if (cmp_code == EQ || cmp_code == NE)
4575 if (cmp_op1 != const0_rtx)
4577 cmp_op0 = expand_simple_binop (cmp_mode, XOR, cmp_op0, cmp_op1,
4578 NULL_RTX, 0, OPTAB_WIDEN);
4579 cmp_op1 = const0_rtx;
4582 cmp_code = cmp_code == EQ ? LEU : GTU;
4585 if (cmp_code == GTU || cmp_code == GEU)
4587 rtx tem = cmp_op0;
4588 cmp_op0 = cmp_op1;
4589 cmp_op1 = tem;
4590 cmp_code = swap_condition (cmp_code);
4593 switch (cmp_code)
4595 case LEU:
4596 cc_mode = CCUmode;
4597 break;
4599 case LTU:
4600 cc_mode = CCL3mode;
4601 break;
4603 default:
4604 return false;
4607 /* Emit comparison instruction pattern. */
4608 if (!register_operand (cmp_op0, cmp_mode))
4609 cmp_op0 = force_reg (cmp_mode, cmp_op0);
4611 insn = gen_rtx_SET (VOIDmode, gen_rtx_REG (cc_mode, CC_REGNUM),
4612 gen_rtx_COMPARE (cc_mode, cmp_op0, cmp_op1));
4613 /* We use insn_invalid_p here to add clobbers if required. */
4614 ret = insn_invalid_p (emit_insn (insn), false);
4615 gcc_assert (!ret);
4617 /* Emit SLB instruction pattern. */
4618 if (!register_operand (src, GET_MODE (dst)))
4619 src = force_reg (GET_MODE (dst), src);
4621 op_res = gen_rtx_MINUS (GET_MODE (dst),
4622 gen_rtx_MINUS (GET_MODE (dst), src, const0_rtx),
4623 gen_rtx_fmt_ee (cmp_code, GET_MODE (dst),
4624 gen_rtx_REG (cc_mode, CC_REGNUM),
4625 const0_rtx));
4626 p = rtvec_alloc (2);
4627 RTVEC_ELT (p, 0) =
4628 gen_rtx_SET (VOIDmode, dst, op_res);
4629 RTVEC_ELT (p, 1) =
4630 gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
4631 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
4633 return true;
4636 return false;
4639 /* Expand code for the insv template. Return true if successful. */
4641 bool
4642 s390_expand_insv (rtx dest, rtx op1, rtx op2, rtx src)
4644 int bitsize = INTVAL (op1);
4645 int bitpos = INTVAL (op2);
4646 machine_mode mode = GET_MODE (dest);
4647 machine_mode smode;
4648 int smode_bsize, mode_bsize;
4649 rtx op, clobber;
4651 if (bitsize + bitpos > GET_MODE_BITSIZE (mode))
4652 return false;
4654 /* Generate INSERT IMMEDIATE (IILL et al). */
4655 /* (set (ze (reg)) (const_int)). */
4656 if (TARGET_ZARCH
4657 && register_operand (dest, word_mode)
4658 && (bitpos % 16) == 0
4659 && (bitsize % 16) == 0
4660 && const_int_operand (src, VOIDmode))
4662 HOST_WIDE_INT val = INTVAL (src);
4663 int regpos = bitpos + bitsize;
4665 while (regpos > bitpos)
4667 machine_mode putmode;
4668 int putsize;
4670 if (TARGET_EXTIMM && (regpos % 32 == 0) && (regpos >= bitpos + 32))
4671 putmode = SImode;
4672 else
4673 putmode = HImode;
4675 putsize = GET_MODE_BITSIZE (putmode);
4676 regpos -= putsize;
4677 emit_move_insn (gen_rtx_ZERO_EXTRACT (word_mode, dest,
4678 GEN_INT (putsize),
4679 GEN_INT (regpos)),
4680 gen_int_mode (val, putmode));
4681 val >>= putsize;
4683 gcc_assert (regpos == bitpos);
4684 return true;
4687 smode = smallest_mode_for_size (bitsize, MODE_INT);
4688 smode_bsize = GET_MODE_BITSIZE (smode);
4689 mode_bsize = GET_MODE_BITSIZE (mode);
4691 /* Generate STORE CHARACTERS UNDER MASK (STCM et al). */
4692 if (bitpos == 0
4693 && (bitsize % BITS_PER_UNIT) == 0
4694 && MEM_P (dest)
4695 && (register_operand (src, word_mode)
4696 || const_int_operand (src, VOIDmode)))
4698 /* Emit standard pattern if possible. */
4699 if (smode_bsize == bitsize)
4701 emit_move_insn (adjust_address (dest, smode, 0),
4702 gen_lowpart (smode, src));
4703 return true;
4706 /* (set (ze (mem)) (const_int)). */
4707 else if (const_int_operand (src, VOIDmode))
4709 int size = bitsize / BITS_PER_UNIT;
4710 rtx src_mem = adjust_address (force_const_mem (word_mode, src),
4711 BLKmode,
4712 UNITS_PER_WORD - size);
4714 dest = adjust_address (dest, BLKmode, 0);
4715 set_mem_size (dest, size);
4716 s390_expand_movmem (dest, src_mem, GEN_INT (size));
4717 return true;
4720 /* (set (ze (mem)) (reg)). */
4721 else if (register_operand (src, word_mode))
4723 if (bitsize <= 32)
4724 emit_move_insn (gen_rtx_ZERO_EXTRACT (word_mode, dest, op1,
4725 const0_rtx), src);
4726 else
4728 /* Emit st,stcmh sequence. */
4729 int stcmh_width = bitsize - 32;
4730 int size = stcmh_width / BITS_PER_UNIT;
4732 emit_move_insn (adjust_address (dest, SImode, size),
4733 gen_lowpart (SImode, src));
4734 set_mem_size (dest, size);
4735 emit_move_insn (gen_rtx_ZERO_EXTRACT (word_mode, dest,
4736 GEN_INT (stcmh_width),
4737 const0_rtx),
4738 gen_rtx_LSHIFTRT (word_mode, src, GEN_INT (32)));
4740 return true;
4744 /* Generate INSERT CHARACTERS UNDER MASK (IC, ICM et al). */
4745 if ((bitpos % BITS_PER_UNIT) == 0
4746 && (bitsize % BITS_PER_UNIT) == 0
4747 && (bitpos & 32) == ((bitpos + bitsize - 1) & 32)
4748 && MEM_P (src)
4749 && (mode == DImode || mode == SImode)
4750 && register_operand (dest, mode))
4752 /* Emit a strict_low_part pattern if possible. */
4753 if (smode_bsize == bitsize && bitpos == mode_bsize - smode_bsize)
4755 op = gen_rtx_STRICT_LOW_PART (VOIDmode, gen_lowpart (smode, dest));
4756 op = gen_rtx_SET (VOIDmode, op, gen_lowpart (smode, src));
4757 clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
4758 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clobber)));
4759 return true;
4762 /* ??? There are more powerful versions of ICM that are not
4763 completely represented in the md file. */
4766 /* For z10, generate ROTATE THEN INSERT SELECTED BITS (RISBG et al). */
4767 if (TARGET_Z10 && (mode == DImode || mode == SImode))
4769 machine_mode mode_s = GET_MODE (src);
4771 if (mode_s == VOIDmode)
4773 /* Assume const_int etc already in the proper mode. */
4774 src = force_reg (mode, src);
4776 else if (mode_s != mode)
4778 gcc_assert (GET_MODE_BITSIZE (mode_s) >= bitsize);
4779 src = force_reg (mode_s, src);
4780 src = gen_lowpart (mode, src);
4783 op = gen_rtx_ZERO_EXTRACT (mode, dest, op1, op2),
4784 op = gen_rtx_SET (VOIDmode, op, src);
4786 if (!TARGET_ZEC12)
4788 clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
4789 op = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clobber));
4791 emit_insn (op);
4793 return true;
4796 return false;
4799 /* A subroutine of s390_expand_cs_hqi and s390_expand_atomic which returns a
4800 register that holds VAL of mode MODE shifted by COUNT bits. */
4802 static inline rtx
4803 s390_expand_mask_and_shift (rtx val, machine_mode mode, rtx count)
4805 val = expand_simple_binop (SImode, AND, val, GEN_INT (GET_MODE_MASK (mode)),
4806 NULL_RTX, 1, OPTAB_DIRECT);
4807 return expand_simple_binop (SImode, ASHIFT, val, count,
4808 NULL_RTX, 1, OPTAB_DIRECT);
4811 /* Structure to hold the initial parameters for a compare_and_swap operation
4812 in HImode and QImode. */
4814 struct alignment_context
4816 rtx memsi; /* SI aligned memory location. */
4817 rtx shift; /* Bit offset with regard to lsb. */
4818 rtx modemask; /* Mask of the HQImode shifted by SHIFT bits. */
4819 rtx modemaski; /* ~modemask */
4820 bool aligned; /* True if memory is aligned, false else. */
4823 /* A subroutine of s390_expand_cs_hqi and s390_expand_atomic to initialize
4824 structure AC for transparent simplifying, if the memory alignment is known
4825 to be at least 32bit. MEM is the memory location for the actual operation
4826 and MODE its mode. */
4828 static void
4829 init_alignment_context (struct alignment_context *ac, rtx mem,
4830 machine_mode mode)
4832 ac->shift = GEN_INT (GET_MODE_SIZE (SImode) - GET_MODE_SIZE (mode));
4833 ac->aligned = (MEM_ALIGN (mem) >= GET_MODE_BITSIZE (SImode));
4835 if (ac->aligned)
4836 ac->memsi = adjust_address (mem, SImode, 0); /* Memory is aligned. */
4837 else
4839 /* Alignment is unknown. */
4840 rtx byteoffset, addr, align;
4842 /* Force the address into a register. */
4843 addr = force_reg (Pmode, XEXP (mem, 0));
4845 /* Align it to SImode. */
4846 align = expand_simple_binop (Pmode, AND, addr,
4847 GEN_INT (-GET_MODE_SIZE (SImode)),
4848 NULL_RTX, 1, OPTAB_DIRECT);
4849 /* Generate MEM. */
4850 ac->memsi = gen_rtx_MEM (SImode, align);
4851 MEM_VOLATILE_P (ac->memsi) = MEM_VOLATILE_P (mem);
4852 set_mem_alias_set (ac->memsi, ALIAS_SET_MEMORY_BARRIER);
4853 set_mem_align (ac->memsi, GET_MODE_BITSIZE (SImode));
4855 /* Calculate shiftcount. */
4856 byteoffset = expand_simple_binop (Pmode, AND, addr,
4857 GEN_INT (GET_MODE_SIZE (SImode) - 1),
4858 NULL_RTX, 1, OPTAB_DIRECT);
4859 /* As we already have some offset, evaluate the remaining distance. */
4860 ac->shift = expand_simple_binop (SImode, MINUS, ac->shift, byteoffset,
4861 NULL_RTX, 1, OPTAB_DIRECT);
4864 /* Shift is the byte count, but we need the bitcount. */
4865 ac->shift = expand_simple_binop (SImode, ASHIFT, ac->shift, GEN_INT (3),
4866 NULL_RTX, 1, OPTAB_DIRECT);
4868 /* Calculate masks. */
4869 ac->modemask = expand_simple_binop (SImode, ASHIFT,
4870 GEN_INT (GET_MODE_MASK (mode)),
4871 ac->shift, NULL_RTX, 1, OPTAB_DIRECT);
4872 ac->modemaski = expand_simple_unop (SImode, NOT, ac->modemask,
4873 NULL_RTX, 1);
4876 /* A subroutine of s390_expand_cs_hqi. Insert INS into VAL. If possible,
4877 use a single insv insn into SEQ2. Otherwise, put prep insns in SEQ1 and
4878 perform the merge in SEQ2. */
4880 static rtx
4881 s390_two_part_insv (struct alignment_context *ac, rtx *seq1, rtx *seq2,
4882 machine_mode mode, rtx val, rtx ins)
4884 rtx tmp;
4886 if (ac->aligned)
4888 start_sequence ();
4889 tmp = copy_to_mode_reg (SImode, val);
4890 if (s390_expand_insv (tmp, GEN_INT (GET_MODE_BITSIZE (mode)),
4891 const0_rtx, ins))
4893 *seq1 = NULL;
4894 *seq2 = get_insns ();
4895 end_sequence ();
4896 return tmp;
4898 end_sequence ();
4901 /* Failed to use insv. Generate a two part shift and mask. */
4902 start_sequence ();
4903 tmp = s390_expand_mask_and_shift (ins, mode, ac->shift);
4904 *seq1 = get_insns ();
4905 end_sequence ();
4907 start_sequence ();
4908 tmp = expand_simple_binop (SImode, IOR, tmp, val, NULL_RTX, 1, OPTAB_DIRECT);
4909 *seq2 = get_insns ();
4910 end_sequence ();
4912 return tmp;
4915 /* Expand an atomic compare and swap operation for HImode and QImode. MEM is
4916 the memory location, CMP the old value to compare MEM with and NEW_RTX the
4917 value to set if CMP == MEM. */
4919 void
4920 s390_expand_cs_hqi (machine_mode mode, rtx btarget, rtx vtarget, rtx mem,
4921 rtx cmp, rtx new_rtx, bool is_weak)
4923 struct alignment_context ac;
4924 rtx cmpv, newv, val, cc, seq0, seq1, seq2, seq3;
4925 rtx res = gen_reg_rtx (SImode);
4926 rtx_code_label *csloop = NULL, *csend = NULL;
4928 gcc_assert (MEM_P (mem));
4930 init_alignment_context (&ac, mem, mode);
4932 /* Load full word. Subsequent loads are performed by CS. */
4933 val = expand_simple_binop (SImode, AND, ac.memsi, ac.modemaski,
4934 NULL_RTX, 1, OPTAB_DIRECT);
4936 /* Prepare insertions of cmp and new_rtx into the loaded value. When
4937 possible, we try to use insv to make this happen efficiently. If
4938 that fails we'll generate code both inside and outside the loop. */
4939 cmpv = s390_two_part_insv (&ac, &seq0, &seq2, mode, val, cmp);
4940 newv = s390_two_part_insv (&ac, &seq1, &seq3, mode, val, new_rtx);
4942 if (seq0)
4943 emit_insn (seq0);
4944 if (seq1)
4945 emit_insn (seq1);
4947 /* Start CS loop. */
4948 if (!is_weak)
4950 /* Begin assuming success. */
4951 emit_move_insn (btarget, const1_rtx);
4953 csloop = gen_label_rtx ();
4954 csend = gen_label_rtx ();
4955 emit_label (csloop);
4958 /* val = "<mem>00..0<mem>"
4959 * cmp = "00..0<cmp>00..0"
4960 * new = "00..0<new>00..0"
4963 emit_insn (seq2);
4964 emit_insn (seq3);
4966 cc = s390_emit_compare_and_swap (EQ, res, ac.memsi, cmpv, newv);
4967 if (is_weak)
4968 emit_insn (gen_cstorecc4 (btarget, cc, XEXP (cc, 0), XEXP (cc, 1)));
4969 else
4971 rtx tmp;
4973 /* Jump to end if we're done (likely?). */
4974 s390_emit_jump (csend, cc);
4976 /* Check for changes outside mode, and loop internal if so.
4977 Arrange the moves so that the compare is adjacent to the
4978 branch so that we can generate CRJ. */
4979 tmp = copy_to_reg (val);
4980 force_expand_binop (SImode, and_optab, res, ac.modemaski, val,
4981 1, OPTAB_DIRECT);
4982 cc = s390_emit_compare (NE, val, tmp);
4983 s390_emit_jump (csloop, cc);
4985 /* Failed. */
4986 emit_move_insn (btarget, const0_rtx);
4987 emit_label (csend);
4990 /* Return the correct part of the bitfield. */
4991 convert_move (vtarget, expand_simple_binop (SImode, LSHIFTRT, res, ac.shift,
4992 NULL_RTX, 1, OPTAB_DIRECT), 1);
4995 /* Expand an atomic operation CODE of mode MODE. MEM is the memory location
4996 and VAL the value to play with. If AFTER is true then store the value
4997 MEM holds after the operation, if AFTER is false then store the value MEM
4998 holds before the operation. If TARGET is zero then discard that value, else
4999 store it to TARGET. */
5001 void
5002 s390_expand_atomic (machine_mode mode, enum rtx_code code,
5003 rtx target, rtx mem, rtx val, bool after)
5005 struct alignment_context ac;
5006 rtx cmp;
5007 rtx new_rtx = gen_reg_rtx (SImode);
5008 rtx orig = gen_reg_rtx (SImode);
5009 rtx_code_label *csloop = gen_label_rtx ();
5011 gcc_assert (!target || register_operand (target, VOIDmode));
5012 gcc_assert (MEM_P (mem));
5014 init_alignment_context (&ac, mem, mode);
5016 /* Shift val to the correct bit positions.
5017 Preserve "icm", but prevent "ex icm". */
5018 if (!(ac.aligned && code == SET && MEM_P (val)))
5019 val = s390_expand_mask_and_shift (val, mode, ac.shift);
5021 /* Further preparation insns. */
5022 if (code == PLUS || code == MINUS)
5023 emit_move_insn (orig, val);
5024 else if (code == MULT || code == AND) /* val = "11..1<val>11..1" */
5025 val = expand_simple_binop (SImode, XOR, val, ac.modemaski,
5026 NULL_RTX, 1, OPTAB_DIRECT);
5028 /* Load full word. Subsequent loads are performed by CS. */
5029 cmp = force_reg (SImode, ac.memsi);
5031 /* Start CS loop. */
5032 emit_label (csloop);
5033 emit_move_insn (new_rtx, cmp);
5035 /* Patch new with val at correct position. */
5036 switch (code)
5038 case PLUS:
5039 case MINUS:
5040 val = expand_simple_binop (SImode, code, new_rtx, orig,
5041 NULL_RTX, 1, OPTAB_DIRECT);
5042 val = expand_simple_binop (SImode, AND, val, ac.modemask,
5043 NULL_RTX, 1, OPTAB_DIRECT);
5044 /* FALLTHRU */
5045 case SET:
5046 if (ac.aligned && MEM_P (val))
5047 store_bit_field (new_rtx, GET_MODE_BITSIZE (mode), 0,
5048 0, 0, SImode, val);
5049 else
5051 new_rtx = expand_simple_binop (SImode, AND, new_rtx, ac.modemaski,
5052 NULL_RTX, 1, OPTAB_DIRECT);
5053 new_rtx = expand_simple_binop (SImode, IOR, new_rtx, val,
5054 NULL_RTX, 1, OPTAB_DIRECT);
5056 break;
5057 case AND:
5058 case IOR:
5059 case XOR:
5060 new_rtx = expand_simple_binop (SImode, code, new_rtx, val,
5061 NULL_RTX, 1, OPTAB_DIRECT);
5062 break;
5063 case MULT: /* NAND */
5064 new_rtx = expand_simple_binop (SImode, AND, new_rtx, val,
5065 NULL_RTX, 1, OPTAB_DIRECT);
5066 new_rtx = expand_simple_binop (SImode, XOR, new_rtx, ac.modemask,
5067 NULL_RTX, 1, OPTAB_DIRECT);
5068 break;
5069 default:
5070 gcc_unreachable ();
5073 s390_emit_jump (csloop, s390_emit_compare_and_swap (NE, cmp,
5074 ac.memsi, cmp, new_rtx));
5076 /* Return the correct part of the bitfield. */
5077 if (target)
5078 convert_move (target, expand_simple_binop (SImode, LSHIFTRT,
5079 after ? new_rtx : cmp, ac.shift,
5080 NULL_RTX, 1, OPTAB_DIRECT), 1);
5083 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
5084 We need to emit DTP-relative relocations. */
5086 static void s390_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
5088 static void
5089 s390_output_dwarf_dtprel (FILE *file, int size, rtx x)
5091 switch (size)
5093 case 4:
5094 fputs ("\t.long\t", file);
5095 break;
5096 case 8:
5097 fputs ("\t.quad\t", file);
5098 break;
5099 default:
5100 gcc_unreachable ();
5102 output_addr_const (file, x);
5103 fputs ("@DTPOFF", file);
5106 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
5107 /* Implement TARGET_MANGLE_TYPE. */
5109 static const char *
5110 s390_mangle_type (const_tree type)
5112 if (TYPE_MAIN_VARIANT (type) == long_double_type_node
5113 && TARGET_LONG_DOUBLE_128)
5114 return "g";
5116 /* For all other types, use normal C++ mangling. */
5117 return NULL;
5119 #endif
5121 /* In the name of slightly smaller debug output, and to cater to
5122 general assembler lossage, recognize various UNSPEC sequences
5123 and turn them back into a direct symbol reference. */
5125 static rtx
5126 s390_delegitimize_address (rtx orig_x)
5128 rtx x, y;
5130 orig_x = delegitimize_mem_from_attrs (orig_x);
5131 x = orig_x;
5133 /* Extract the symbol ref from:
5134 (plus:SI (reg:SI 12 %r12)
5135 (const:SI (unspec:SI [(symbol_ref/f:SI ("*.LC0"))]
5136 UNSPEC_GOTOFF/PLTOFF)))
5138 (plus:SI (reg:SI 12 %r12)
5139 (const:SI (plus:SI (unspec:SI [(symbol_ref:SI ("L"))]
5140 UNSPEC_GOTOFF/PLTOFF)
5141 (const_int 4 [0x4])))) */
5142 if (GET_CODE (x) == PLUS
5143 && REG_P (XEXP (x, 0))
5144 && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM
5145 && GET_CODE (XEXP (x, 1)) == CONST)
5147 HOST_WIDE_INT offset = 0;
5149 /* The const operand. */
5150 y = XEXP (XEXP (x, 1), 0);
5152 if (GET_CODE (y) == PLUS
5153 && GET_CODE (XEXP (y, 1)) == CONST_INT)
5155 offset = INTVAL (XEXP (y, 1));
5156 y = XEXP (y, 0);
5159 if (GET_CODE (y) == UNSPEC
5160 && (XINT (y, 1) == UNSPEC_GOTOFF
5161 || XINT (y, 1) == UNSPEC_PLTOFF))
5162 return plus_constant (Pmode, XVECEXP (y, 0, 0), offset);
5165 if (GET_CODE (x) != MEM)
5166 return orig_x;
5168 x = XEXP (x, 0);
5169 if (GET_CODE (x) == PLUS
5170 && GET_CODE (XEXP (x, 1)) == CONST
5171 && GET_CODE (XEXP (x, 0)) == REG
5172 && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
5174 y = XEXP (XEXP (x, 1), 0);
5175 if (GET_CODE (y) == UNSPEC
5176 && XINT (y, 1) == UNSPEC_GOT)
5177 y = XVECEXP (y, 0, 0);
5178 else
5179 return orig_x;
5181 else if (GET_CODE (x) == CONST)
5183 /* Extract the symbol ref from:
5184 (mem:QI (const:DI (unspec:DI [(symbol_ref:DI ("foo"))]
5185 UNSPEC_PLT/GOTENT))) */
5187 y = XEXP (x, 0);
5188 if (GET_CODE (y) == UNSPEC
5189 && (XINT (y, 1) == UNSPEC_GOTENT
5190 || XINT (y, 1) == UNSPEC_PLT))
5191 y = XVECEXP (y, 0, 0);
5192 else
5193 return orig_x;
5195 else
5196 return orig_x;
5198 if (GET_MODE (orig_x) != Pmode)
5200 if (GET_MODE (orig_x) == BLKmode)
5201 return orig_x;
5202 y = lowpart_subreg (GET_MODE (orig_x), y, Pmode);
5203 if (y == NULL_RTX)
5204 return orig_x;
5206 return y;
5209 /* Output operand OP to stdio stream FILE.
5210 OP is an address (register + offset) which is not used to address data;
5211 instead the rightmost bits are interpreted as the value. */
5213 static void
5214 print_shift_count_operand (FILE *file, rtx op)
5216 HOST_WIDE_INT offset;
5217 rtx base;
5219 /* Extract base register and offset. */
5220 if (!s390_decompose_shift_count (op, &base, &offset))
5221 gcc_unreachable ();
5223 /* Sanity check. */
5224 if (base)
5226 gcc_assert (GET_CODE (base) == REG);
5227 gcc_assert (REGNO (base) < FIRST_PSEUDO_REGISTER);
5228 gcc_assert (REGNO_REG_CLASS (REGNO (base)) == ADDR_REGS);
5231 /* Offsets are constricted to twelve bits. */
5232 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset & ((1 << 12) - 1));
5233 if (base)
5234 fprintf (file, "(%s)", reg_names[REGNO (base)]);
5237 /* Returns false if the function should not be made hotpatchable.
5238 Otherwise it assigns the number of NOP halfwords to be emitted
5239 before and after the function label to hw_before and hw_after.
5240 Both must not be NULL. */
5242 static bool
5243 s390_function_num_hotpatch_hw (tree decl,
5244 int *hw_before,
5245 int *hw_after)
5247 tree attr;
5249 *hw_before = 0;
5250 *hw_after = 0;
5252 attr = lookup_attribute ("hotpatch", DECL_ATTRIBUTES (decl));
5254 /* Handle the arguments of the hotpatch attribute. The values
5255 specified via attribute might override the cmdline argument
5256 values. */
5257 if (attr)
5259 tree args = TREE_VALUE (attr);
5261 *hw_before = TREE_INT_CST_LOW (TREE_VALUE (args));
5262 *hw_after = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args)));
5264 else
5266 /* Use the values specified by the cmdline arguments. */
5267 *hw_before = s390_hotpatch_hw_before_label;
5268 *hw_after = s390_hotpatch_hw_after_label;
5271 if (*hw_before == 0 && *hw_after == 0)
5272 return false;
5274 if (decl_function_context (decl) != NULL_TREE)
5276 warning_at (DECL_SOURCE_LOCATION (decl), OPT_mhotpatch_,
5277 "hotpatching is not compatible with nested functions");
5278 return false;
5281 return true;
5284 /* Write the extra assembler code needed to declare a function properly. */
5286 void
5287 s390_asm_output_function_label (FILE *asm_out_file, const char *fname,
5288 tree decl)
5290 int hw_before, hw_after;
5291 bool hotpatch_p = (decl
5292 ? s390_function_num_hotpatch_hw (decl,
5293 &hw_before, &hw_after)
5294 : false);
5296 if (hotpatch_p)
5298 int i;
5300 /* Add a trampoline code area before the function label and initialize it
5301 with two-byte nop instructions. This area can be overwritten with code
5302 that jumps to a patched version of the function. */
5303 for (i = 0; i < hw_before; i++)
5304 asm_fprintf (asm_out_file, "\tnopr\t%%r7\n");
5305 /* Note: The function label must be aligned so that (a) the bytes of the
5306 following nop do not cross a cacheline boundary, and (b) a jump address
5307 (eight bytes for 64 bit targets, 4 bytes for 32 bit targets) can be
5308 stored directly before the label without crossing a cacheline
5309 boundary. All this is necessary to make sure the trampoline code can
5310 be changed atomically. */
5313 ASM_OUTPUT_LABEL (asm_out_file, fname);
5315 /* Output a series of NOPs after the function label. */
5316 if (hotpatch_p)
5318 while (hw_after > 0)
5320 if (hw_after >= 3 && TARGET_CPU_ZARCH)
5322 asm_fprintf (asm_out_file, "\tbrcl\t\t0,0\n");
5323 hw_after -= 3;
5325 else if (hw_after >= 2)
5327 gcc_assert (hw_after == 2 || !TARGET_CPU_ZARCH);
5328 asm_fprintf (asm_out_file, "\tnop\t0\n");
5329 hw_after -= 2;
5331 else
5333 gcc_assert (hw_after == 1);
5334 asm_fprintf (asm_out_file, "\tnopr\t%%r7\n");
5335 hw_after -= 1;
5341 /* Output machine-dependent UNSPECs occurring in address constant X
5342 in assembler syntax to stdio stream FILE. Returns true if the
5343 constant X could be recognized, false otherwise. */
5345 static bool
5346 s390_output_addr_const_extra (FILE *file, rtx x)
5348 if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 1)
5349 switch (XINT (x, 1))
5351 case UNSPEC_GOTENT:
5352 output_addr_const (file, XVECEXP (x, 0, 0));
5353 fprintf (file, "@GOTENT");
5354 return true;
5355 case UNSPEC_GOT:
5356 output_addr_const (file, XVECEXP (x, 0, 0));
5357 fprintf (file, "@GOT");
5358 return true;
5359 case UNSPEC_GOTOFF:
5360 output_addr_const (file, XVECEXP (x, 0, 0));
5361 fprintf (file, "@GOTOFF");
5362 return true;
5363 case UNSPEC_PLT:
5364 output_addr_const (file, XVECEXP (x, 0, 0));
5365 fprintf (file, "@PLT");
5366 return true;
5367 case UNSPEC_PLTOFF:
5368 output_addr_const (file, XVECEXP (x, 0, 0));
5369 fprintf (file, "@PLTOFF");
5370 return true;
5371 case UNSPEC_TLSGD:
5372 output_addr_const (file, XVECEXP (x, 0, 0));
5373 fprintf (file, "@TLSGD");
5374 return true;
5375 case UNSPEC_TLSLDM:
5376 assemble_name (file, get_some_local_dynamic_name ());
5377 fprintf (file, "@TLSLDM");
5378 return true;
5379 case UNSPEC_DTPOFF:
5380 output_addr_const (file, XVECEXP (x, 0, 0));
5381 fprintf (file, "@DTPOFF");
5382 return true;
5383 case UNSPEC_NTPOFF:
5384 output_addr_const (file, XVECEXP (x, 0, 0));
5385 fprintf (file, "@NTPOFF");
5386 return true;
5387 case UNSPEC_GOTNTPOFF:
5388 output_addr_const (file, XVECEXP (x, 0, 0));
5389 fprintf (file, "@GOTNTPOFF");
5390 return true;
5391 case UNSPEC_INDNTPOFF:
5392 output_addr_const (file, XVECEXP (x, 0, 0));
5393 fprintf (file, "@INDNTPOFF");
5394 return true;
5397 if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 2)
5398 switch (XINT (x, 1))
5400 case UNSPEC_POOL_OFFSET:
5401 x = gen_rtx_MINUS (GET_MODE (x), XVECEXP (x, 0, 0), XVECEXP (x, 0, 1));
5402 output_addr_const (file, x);
5403 return true;
5405 return false;
5408 /* Output address operand ADDR in assembler syntax to
5409 stdio stream FILE. */
5411 void
5412 print_operand_address (FILE *file, rtx addr)
5414 struct s390_address ad;
5416 if (s390_loadrelative_operand_p (addr, NULL, NULL))
5418 if (!TARGET_Z10)
5420 output_operand_lossage ("symbolic memory references are "
5421 "only supported on z10 or later");
5422 return;
5424 output_addr_const (file, addr);
5425 return;
5428 if (!s390_decompose_address (addr, &ad)
5429 || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
5430 || (ad.indx && !REGNO_OK_FOR_INDEX_P (REGNO (ad.indx))))
5431 output_operand_lossage ("cannot decompose address");
5433 if (ad.disp)
5434 output_addr_const (file, ad.disp);
5435 else
5436 fprintf (file, "0");
5438 if (ad.base && ad.indx)
5439 fprintf (file, "(%s,%s)", reg_names[REGNO (ad.indx)],
5440 reg_names[REGNO (ad.base)]);
5441 else if (ad.base)
5442 fprintf (file, "(%s)", reg_names[REGNO (ad.base)]);
5445 /* Output operand X in assembler syntax to stdio stream FILE.
5446 CODE specified the format flag. The following format flags
5447 are recognized:
5449 'C': print opcode suffix for branch condition.
5450 'D': print opcode suffix for inverse branch condition.
5451 'E': print opcode suffix for branch on index instruction.
5452 'G': print the size of the operand in bytes.
5453 'J': print tls_load/tls_gdcall/tls_ldcall suffix
5454 'M': print the second word of a TImode operand.
5455 'N': print the second word of a DImode operand.
5456 'O': print only the displacement of a memory reference.
5457 'R': print only the base register of a memory reference.
5458 'S': print S-type memory reference (base+displacement).
5459 'Y': print shift count operand.
5461 'b': print integer X as if it's an unsigned byte.
5462 'c': print integer X as if it's an signed byte.
5463 'e': "end" of DImode contiguous bitmask X.
5464 'f': "end" of SImode contiguous bitmask X.
5465 'h': print integer X as if it's a signed halfword.
5466 'i': print the first nonzero HImode part of X.
5467 'j': print the first HImode part unequal to -1 of X.
5468 'k': print the first nonzero SImode part of X.
5469 'm': print the first SImode part unequal to -1 of X.
5470 'o': print integer X as if it's an unsigned 32bit word.
5471 's': "start" of DImode contiguous bitmask X.
5472 't': "start" of SImode contiguous bitmask X.
5473 'x': print integer X as if it's an unsigned halfword.
5476 void
5477 print_operand (FILE *file, rtx x, int code)
5479 HOST_WIDE_INT ival;
5481 switch (code)
5483 case 'C':
5484 fprintf (file, s390_branch_condition_mnemonic (x, FALSE));
5485 return;
5487 case 'D':
5488 fprintf (file, s390_branch_condition_mnemonic (x, TRUE));
5489 return;
5491 case 'E':
5492 if (GET_CODE (x) == LE)
5493 fprintf (file, "l");
5494 else if (GET_CODE (x) == GT)
5495 fprintf (file, "h");
5496 else
5497 output_operand_lossage ("invalid comparison operator "
5498 "for 'E' output modifier");
5499 return;
5501 case 'J':
5502 if (GET_CODE (x) == SYMBOL_REF)
5504 fprintf (file, "%s", ":tls_load:");
5505 output_addr_const (file, x);
5507 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSGD)
5509 fprintf (file, "%s", ":tls_gdcall:");
5510 output_addr_const (file, XVECEXP (x, 0, 0));
5512 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSLDM)
5514 fprintf (file, "%s", ":tls_ldcall:");
5515 const char *name = get_some_local_dynamic_name ();
5516 gcc_assert (name);
5517 assemble_name (file, name);
5519 else
5520 output_operand_lossage ("invalid reference for 'J' output modifier");
5521 return;
5523 case 'G':
5524 fprintf (file, "%u", GET_MODE_SIZE (GET_MODE (x)));
5525 return;
5527 case 'O':
5529 struct s390_address ad;
5530 int ret;
5532 if (!MEM_P (x))
5534 output_operand_lossage ("memory reference expected for "
5535 "'O' output modifier");
5536 return;
5539 ret = s390_decompose_address (XEXP (x, 0), &ad);
5541 if (!ret
5542 || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
5543 || ad.indx)
5545 output_operand_lossage ("invalid address for 'O' output modifier");
5546 return;
5549 if (ad.disp)
5550 output_addr_const (file, ad.disp);
5551 else
5552 fprintf (file, "0");
5554 return;
5556 case 'R':
5558 struct s390_address ad;
5559 int ret;
5561 if (!MEM_P (x))
5563 output_operand_lossage ("memory reference expected for "
5564 "'R' output modifier");
5565 return;
5568 ret = s390_decompose_address (XEXP (x, 0), &ad);
5570 if (!ret
5571 || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
5572 || ad.indx)
5574 output_operand_lossage ("invalid address for 'R' output modifier");
5575 return;
5578 if (ad.base)
5579 fprintf (file, "%s", reg_names[REGNO (ad.base)]);
5580 else
5581 fprintf (file, "0");
5583 return;
5585 case 'S':
5587 struct s390_address ad;
5588 int ret;
5590 if (!MEM_P (x))
5592 output_operand_lossage ("memory reference expected for "
5593 "'S' output modifier");
5594 return;
5596 ret = s390_decompose_address (XEXP (x, 0), &ad);
5598 if (!ret
5599 || (ad.base && !REGNO_OK_FOR_BASE_P (REGNO (ad.base)))
5600 || ad.indx)
5602 output_operand_lossage ("invalid address for 'S' output modifier");
5603 return;
5606 if (ad.disp)
5607 output_addr_const (file, ad.disp);
5608 else
5609 fprintf (file, "0");
5611 if (ad.base)
5612 fprintf (file, "(%s)", reg_names[REGNO (ad.base)]);
5614 return;
5616 case 'N':
5617 if (GET_CODE (x) == REG)
5618 x = gen_rtx_REG (GET_MODE (x), REGNO (x) + 1);
5619 else if (GET_CODE (x) == MEM)
5620 x = change_address (x, VOIDmode,
5621 plus_constant (Pmode, XEXP (x, 0), 4));
5622 else
5623 output_operand_lossage ("register or memory expression expected "
5624 "for 'N' output modifier");
5625 break;
5627 case 'M':
5628 if (GET_CODE (x) == REG)
5629 x = gen_rtx_REG (GET_MODE (x), REGNO (x) + 1);
5630 else if (GET_CODE (x) == MEM)
5631 x = change_address (x, VOIDmode,
5632 plus_constant (Pmode, XEXP (x, 0), 8));
5633 else
5634 output_operand_lossage ("register or memory expression expected "
5635 "for 'M' output modifier");
5636 break;
5638 case 'Y':
5639 print_shift_count_operand (file, x);
5640 return;
5643 switch (GET_CODE (x))
5645 case REG:
5646 fprintf (file, "%s", reg_names[REGNO (x)]);
5647 break;
5649 case MEM:
5650 output_address (XEXP (x, 0));
5651 break;
5653 case CONST:
5654 case CODE_LABEL:
5655 case LABEL_REF:
5656 case SYMBOL_REF:
5657 output_addr_const (file, x);
5658 break;
5660 case CONST_INT:
5661 ival = INTVAL (x);
5662 switch (code)
5664 case 0:
5665 break;
5666 case 'b':
5667 ival &= 0xff;
5668 break;
5669 case 'c':
5670 ival = ((ival & 0xff) ^ 0x80) - 0x80;
5671 break;
5672 case 'x':
5673 ival &= 0xffff;
5674 break;
5675 case 'h':
5676 ival = ((ival & 0xffff) ^ 0x8000) - 0x8000;
5677 break;
5678 case 'i':
5679 ival = s390_extract_part (x, HImode, 0);
5680 break;
5681 case 'j':
5682 ival = s390_extract_part (x, HImode, -1);
5683 break;
5684 case 'k':
5685 ival = s390_extract_part (x, SImode, 0);
5686 break;
5687 case 'm':
5688 ival = s390_extract_part (x, SImode, -1);
5689 break;
5690 case 'o':
5691 ival &= 0xffffffff;
5692 break;
5693 case 'e': case 'f':
5694 case 's': case 't':
5696 int pos, len;
5697 bool ok;
5699 len = (code == 's' || code == 'e' ? 64 : 32);
5700 ok = s390_contiguous_bitmask_p (ival, len, &pos, &len);
5701 gcc_assert (ok);
5702 if (code == 's' || code == 't')
5703 ival = 64 - pos - len;
5704 else
5705 ival = 64 - 1 - pos;
5707 break;
5708 default:
5709 output_operand_lossage ("invalid constant for output modifier '%c'", code);
5711 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ival);
5712 break;
5714 case CONST_DOUBLE:
5715 gcc_assert (GET_MODE (x) == VOIDmode);
5716 if (code == 'b')
5717 fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x) & 0xff);
5718 else if (code == 'x')
5719 fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x) & 0xffff);
5720 else if (code == 'h')
5721 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
5722 ((CONST_DOUBLE_LOW (x) & 0xffff) ^ 0x8000) - 0x8000);
5723 else
5725 if (code == 0)
5726 output_operand_lossage ("invalid constant - try using "
5727 "an output modifier");
5728 else
5729 output_operand_lossage ("invalid constant for output modifier '%c'",
5730 code);
5732 break;
5734 default:
5735 if (code == 0)
5736 output_operand_lossage ("invalid expression - try using "
5737 "an output modifier");
5738 else
5739 output_operand_lossage ("invalid expression for output "
5740 "modifier '%c'", code);
5741 break;
5745 /* Target hook for assembling integer objects. We need to define it
5746 here to work a round a bug in some versions of GAS, which couldn't
5747 handle values smaller than INT_MIN when printed in decimal. */
5749 static bool
5750 s390_assemble_integer (rtx x, unsigned int size, int aligned_p)
5752 if (size == 8 && aligned_p
5753 && GET_CODE (x) == CONST_INT && INTVAL (x) < INT_MIN)
5755 fprintf (asm_out_file, "\t.quad\t" HOST_WIDE_INT_PRINT_HEX "\n",
5756 INTVAL (x));
5757 return true;
5759 return default_assemble_integer (x, size, aligned_p);
5762 /* Returns true if register REGNO is used for forming
5763 a memory address in expression X. */
5765 static bool
5766 reg_used_in_mem_p (int regno, rtx x)
5768 enum rtx_code code = GET_CODE (x);
5769 int i, j;
5770 const char *fmt;
5772 if (code == MEM)
5774 if (refers_to_regno_p (regno, XEXP (x, 0)))
5775 return true;
5777 else if (code == SET
5778 && GET_CODE (SET_DEST (x)) == PC)
5780 if (refers_to_regno_p (regno, SET_SRC (x)))
5781 return true;
5784 fmt = GET_RTX_FORMAT (code);
5785 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5787 if (fmt[i] == 'e'
5788 && reg_used_in_mem_p (regno, XEXP (x, i)))
5789 return true;
5791 else if (fmt[i] == 'E')
5792 for (j = 0; j < XVECLEN (x, i); j++)
5793 if (reg_used_in_mem_p (regno, XVECEXP (x, i, j)))
5794 return true;
5796 return false;
5799 /* Returns true if expression DEP_RTX sets an address register
5800 used by instruction INSN to address memory. */
5802 static bool
5803 addr_generation_dependency_p (rtx dep_rtx, rtx_insn *insn)
5805 rtx target, pat;
5807 if (NONJUMP_INSN_P (dep_rtx))
5808 dep_rtx = PATTERN (dep_rtx);
5810 if (GET_CODE (dep_rtx) == SET)
5812 target = SET_DEST (dep_rtx);
5813 if (GET_CODE (target) == STRICT_LOW_PART)
5814 target = XEXP (target, 0);
5815 while (GET_CODE (target) == SUBREG)
5816 target = SUBREG_REG (target);
5818 if (GET_CODE (target) == REG)
5820 int regno = REGNO (target);
5822 if (s390_safe_attr_type (insn) == TYPE_LA)
5824 pat = PATTERN (insn);
5825 if (GET_CODE (pat) == PARALLEL)
5827 gcc_assert (XVECLEN (pat, 0) == 2);
5828 pat = XVECEXP (pat, 0, 0);
5830 gcc_assert (GET_CODE (pat) == SET);
5831 return refers_to_regno_p (regno, SET_SRC (pat));
5833 else if (get_attr_atype (insn) == ATYPE_AGEN)
5834 return reg_used_in_mem_p (regno, PATTERN (insn));
5837 return false;
5840 /* Return 1, if dep_insn sets register used in insn in the agen unit. */
5843 s390_agen_dep_p (rtx_insn *dep_insn, rtx_insn *insn)
5845 rtx dep_rtx = PATTERN (dep_insn);
5846 int i;
5848 if (GET_CODE (dep_rtx) == SET
5849 && addr_generation_dependency_p (dep_rtx, insn))
5850 return 1;
5851 else if (GET_CODE (dep_rtx) == PARALLEL)
5853 for (i = 0; i < XVECLEN (dep_rtx, 0); i++)
5855 if (addr_generation_dependency_p (XVECEXP (dep_rtx, 0, i), insn))
5856 return 1;
5859 return 0;
5863 /* A C statement (sans semicolon) to update the integer scheduling priority
5864 INSN_PRIORITY (INSN). Increase the priority to execute the INSN earlier,
5865 reduce the priority to execute INSN later. Do not define this macro if
5866 you do not need to adjust the scheduling priorities of insns.
5868 A STD instruction should be scheduled earlier,
5869 in order to use the bypass. */
5870 static int
5871 s390_adjust_priority (rtx_insn *insn, int priority)
5873 if (! INSN_P (insn))
5874 return priority;
5876 if (s390_tune != PROCESSOR_2084_Z990
5877 && s390_tune != PROCESSOR_2094_Z9_109
5878 && s390_tune != PROCESSOR_2097_Z10
5879 && s390_tune != PROCESSOR_2817_Z196
5880 && s390_tune != PROCESSOR_2827_ZEC12)
5881 return priority;
5883 switch (s390_safe_attr_type (insn))
5885 case TYPE_FSTOREDF:
5886 case TYPE_FSTORESF:
5887 priority = priority << 3;
5888 break;
5889 case TYPE_STORE:
5890 case TYPE_STM:
5891 priority = priority << 1;
5892 break;
5893 default:
5894 break;
5896 return priority;
5900 /* The number of instructions that can be issued per cycle. */
5902 static int
5903 s390_issue_rate (void)
5905 switch (s390_tune)
5907 case PROCESSOR_2084_Z990:
5908 case PROCESSOR_2094_Z9_109:
5909 case PROCESSOR_2817_Z196:
5910 return 3;
5911 case PROCESSOR_2097_Z10:
5912 case PROCESSOR_2827_ZEC12:
5913 return 2;
5914 default:
5915 return 1;
5919 static int
5920 s390_first_cycle_multipass_dfa_lookahead (void)
5922 return 4;
5925 /* Annotate every literal pool reference in X by an UNSPEC_LTREF expression.
5926 Fix up MEMs as required. */
5928 static void
5929 annotate_constant_pool_refs (rtx *x)
5931 int i, j;
5932 const char *fmt;
5934 gcc_assert (GET_CODE (*x) != SYMBOL_REF
5935 || !CONSTANT_POOL_ADDRESS_P (*x));
5937 /* Literal pool references can only occur inside a MEM ... */
5938 if (GET_CODE (*x) == MEM)
5940 rtx memref = XEXP (*x, 0);
5942 if (GET_CODE (memref) == SYMBOL_REF
5943 && CONSTANT_POOL_ADDRESS_P (memref))
5945 rtx base = cfun->machine->base_reg;
5946 rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, memref, base),
5947 UNSPEC_LTREF);
5949 *x = replace_equiv_address (*x, addr);
5950 return;
5953 if (GET_CODE (memref) == CONST
5954 && GET_CODE (XEXP (memref, 0)) == PLUS
5955 && GET_CODE (XEXP (XEXP (memref, 0), 1)) == CONST_INT
5956 && GET_CODE (XEXP (XEXP (memref, 0), 0)) == SYMBOL_REF
5957 && CONSTANT_POOL_ADDRESS_P (XEXP (XEXP (memref, 0), 0)))
5959 HOST_WIDE_INT off = INTVAL (XEXP (XEXP (memref, 0), 1));
5960 rtx sym = XEXP (XEXP (memref, 0), 0);
5961 rtx base = cfun->machine->base_reg;
5962 rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, sym, base),
5963 UNSPEC_LTREF);
5965 *x = replace_equiv_address (*x, plus_constant (Pmode, addr, off));
5966 return;
5970 /* ... or a load-address type pattern. */
5971 if (GET_CODE (*x) == SET)
5973 rtx addrref = SET_SRC (*x);
5975 if (GET_CODE (addrref) == SYMBOL_REF
5976 && CONSTANT_POOL_ADDRESS_P (addrref))
5978 rtx base = cfun->machine->base_reg;
5979 rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, addrref, base),
5980 UNSPEC_LTREF);
5982 SET_SRC (*x) = addr;
5983 return;
5986 if (GET_CODE (addrref) == CONST
5987 && GET_CODE (XEXP (addrref, 0)) == PLUS
5988 && GET_CODE (XEXP (XEXP (addrref, 0), 1)) == CONST_INT
5989 && GET_CODE (XEXP (XEXP (addrref, 0), 0)) == SYMBOL_REF
5990 && CONSTANT_POOL_ADDRESS_P (XEXP (XEXP (addrref, 0), 0)))
5992 HOST_WIDE_INT off = INTVAL (XEXP (XEXP (addrref, 0), 1));
5993 rtx sym = XEXP (XEXP (addrref, 0), 0);
5994 rtx base = cfun->machine->base_reg;
5995 rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, sym, base),
5996 UNSPEC_LTREF);
5998 SET_SRC (*x) = plus_constant (Pmode, addr, off);
5999 return;
6003 /* Annotate LTREL_BASE as well. */
6004 if (GET_CODE (*x) == UNSPEC
6005 && XINT (*x, 1) == UNSPEC_LTREL_BASE)
6007 rtx base = cfun->machine->base_reg;
6008 *x = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, XVECEXP (*x, 0, 0), base),
6009 UNSPEC_LTREL_BASE);
6010 return;
6013 fmt = GET_RTX_FORMAT (GET_CODE (*x));
6014 for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
6016 if (fmt[i] == 'e')
6018 annotate_constant_pool_refs (&XEXP (*x, i));
6020 else if (fmt[i] == 'E')
6022 for (j = 0; j < XVECLEN (*x, i); j++)
6023 annotate_constant_pool_refs (&XVECEXP (*x, i, j));
6028 /* Split all branches that exceed the maximum distance.
6029 Returns true if this created a new literal pool entry. */
6031 static int
6032 s390_split_branches (void)
6034 rtx temp_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
6035 int new_literal = 0, ret;
6036 rtx_insn *insn;
6037 rtx pat, target;
6038 rtx *label;
6040 /* We need correct insn addresses. */
6042 shorten_branches (get_insns ());
6044 /* Find all branches that exceed 64KB, and split them. */
6046 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6048 if (! JUMP_P (insn) || tablejump_p (insn, NULL, NULL))
6049 continue;
6051 pat = PATTERN (insn);
6052 if (GET_CODE (pat) == PARALLEL)
6053 pat = XVECEXP (pat, 0, 0);
6054 if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
6055 continue;
6057 if (GET_CODE (SET_SRC (pat)) == LABEL_REF)
6059 label = &SET_SRC (pat);
6061 else if (GET_CODE (SET_SRC (pat)) == IF_THEN_ELSE)
6063 if (GET_CODE (XEXP (SET_SRC (pat), 1)) == LABEL_REF)
6064 label = &XEXP (SET_SRC (pat), 1);
6065 else if (GET_CODE (XEXP (SET_SRC (pat), 2)) == LABEL_REF)
6066 label = &XEXP (SET_SRC (pat), 2);
6067 else
6068 continue;
6070 else
6071 continue;
6073 if (get_attr_length (insn) <= 4)
6074 continue;
6076 /* We are going to use the return register as scratch register,
6077 make sure it will be saved/restored by the prologue/epilogue. */
6078 cfun_frame_layout.save_return_addr_p = 1;
6080 if (!flag_pic)
6082 new_literal = 1;
6083 rtx mem = force_const_mem (Pmode, *label);
6084 rtx_insn *set_insn = emit_insn_before (gen_rtx_SET (Pmode, temp_reg, mem), insn);
6085 INSN_ADDRESSES_NEW (set_insn, -1);
6086 annotate_constant_pool_refs (&PATTERN (set_insn));
6088 target = temp_reg;
6090 else
6092 new_literal = 1;
6093 target = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, *label),
6094 UNSPEC_LTREL_OFFSET);
6095 target = gen_rtx_CONST (Pmode, target);
6096 target = force_const_mem (Pmode, target);
6097 rtx_insn *set_insn = emit_insn_before (gen_rtx_SET (Pmode, temp_reg, target), insn);
6098 INSN_ADDRESSES_NEW (set_insn, -1);
6099 annotate_constant_pool_refs (&PATTERN (set_insn));
6101 target = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, XEXP (target, 0),
6102 cfun->machine->base_reg),
6103 UNSPEC_LTREL_BASE);
6104 target = gen_rtx_PLUS (Pmode, temp_reg, target);
6107 ret = validate_change (insn, label, target, 0);
6108 gcc_assert (ret);
6111 return new_literal;
6115 /* Find an annotated literal pool symbol referenced in RTX X,
6116 and store it at REF. Will abort if X contains references to
6117 more than one such pool symbol; multiple references to the same
6118 symbol are allowed, however.
6120 The rtx pointed to by REF must be initialized to NULL_RTX
6121 by the caller before calling this routine. */
6123 static void
6124 find_constant_pool_ref (rtx x, rtx *ref)
6126 int i, j;
6127 const char *fmt;
6129 /* Ignore LTREL_BASE references. */
6130 if (GET_CODE (x) == UNSPEC
6131 && XINT (x, 1) == UNSPEC_LTREL_BASE)
6132 return;
6133 /* Likewise POOL_ENTRY insns. */
6134 if (GET_CODE (x) == UNSPEC_VOLATILE
6135 && XINT (x, 1) == UNSPECV_POOL_ENTRY)
6136 return;
6138 gcc_assert (GET_CODE (x) != SYMBOL_REF
6139 || !CONSTANT_POOL_ADDRESS_P (x));
6141 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_LTREF)
6143 rtx sym = XVECEXP (x, 0, 0);
6144 gcc_assert (GET_CODE (sym) == SYMBOL_REF
6145 && CONSTANT_POOL_ADDRESS_P (sym));
6147 if (*ref == NULL_RTX)
6148 *ref = sym;
6149 else
6150 gcc_assert (*ref == sym);
6152 return;
6155 fmt = GET_RTX_FORMAT (GET_CODE (x));
6156 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6158 if (fmt[i] == 'e')
6160 find_constant_pool_ref (XEXP (x, i), ref);
6162 else if (fmt[i] == 'E')
6164 for (j = 0; j < XVECLEN (x, i); j++)
6165 find_constant_pool_ref (XVECEXP (x, i, j), ref);
6170 /* Replace every reference to the annotated literal pool
6171 symbol REF in X by its base plus OFFSET. */
6173 static void
6174 replace_constant_pool_ref (rtx *x, rtx ref, rtx offset)
6176 int i, j;
6177 const char *fmt;
6179 gcc_assert (*x != ref);
6181 if (GET_CODE (*x) == UNSPEC
6182 && XINT (*x, 1) == UNSPEC_LTREF
6183 && XVECEXP (*x, 0, 0) == ref)
6185 *x = gen_rtx_PLUS (Pmode, XVECEXP (*x, 0, 1), offset);
6186 return;
6189 if (GET_CODE (*x) == PLUS
6190 && GET_CODE (XEXP (*x, 1)) == CONST_INT
6191 && GET_CODE (XEXP (*x, 0)) == UNSPEC
6192 && XINT (XEXP (*x, 0), 1) == UNSPEC_LTREF
6193 && XVECEXP (XEXP (*x, 0), 0, 0) == ref)
6195 rtx addr = gen_rtx_PLUS (Pmode, XVECEXP (XEXP (*x, 0), 0, 1), offset);
6196 *x = plus_constant (Pmode, addr, INTVAL (XEXP (*x, 1)));
6197 return;
6200 fmt = GET_RTX_FORMAT (GET_CODE (*x));
6201 for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
6203 if (fmt[i] == 'e')
6205 replace_constant_pool_ref (&XEXP (*x, i), ref, offset);
6207 else if (fmt[i] == 'E')
6209 for (j = 0; j < XVECLEN (*x, i); j++)
6210 replace_constant_pool_ref (&XVECEXP (*x, i, j), ref, offset);
6215 /* Check whether X contains an UNSPEC_LTREL_BASE.
6216 Return its constant pool symbol if found, NULL_RTX otherwise. */
6218 static rtx
6219 find_ltrel_base (rtx x)
6221 int i, j;
6222 const char *fmt;
6224 if (GET_CODE (x) == UNSPEC
6225 && XINT (x, 1) == UNSPEC_LTREL_BASE)
6226 return XVECEXP (x, 0, 0);
6228 fmt = GET_RTX_FORMAT (GET_CODE (x));
6229 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6231 if (fmt[i] == 'e')
6233 rtx fnd = find_ltrel_base (XEXP (x, i));
6234 if (fnd)
6235 return fnd;
6237 else if (fmt[i] == 'E')
6239 for (j = 0; j < XVECLEN (x, i); j++)
6241 rtx fnd = find_ltrel_base (XVECEXP (x, i, j));
6242 if (fnd)
6243 return fnd;
6248 return NULL_RTX;
6251 /* Replace any occurrence of UNSPEC_LTREL_BASE in X with its base. */
6253 static void
6254 replace_ltrel_base (rtx *x)
6256 int i, j;
6257 const char *fmt;
6259 if (GET_CODE (*x) == UNSPEC
6260 && XINT (*x, 1) == UNSPEC_LTREL_BASE)
6262 *x = XVECEXP (*x, 0, 1);
6263 return;
6266 fmt = GET_RTX_FORMAT (GET_CODE (*x));
6267 for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
6269 if (fmt[i] == 'e')
6271 replace_ltrel_base (&XEXP (*x, i));
6273 else if (fmt[i] == 'E')
6275 for (j = 0; j < XVECLEN (*x, i); j++)
6276 replace_ltrel_base (&XVECEXP (*x, i, j));
6282 /* We keep a list of constants which we have to add to internal
6283 constant tables in the middle of large functions. */
6285 #define NR_C_MODES 11
6286 machine_mode constant_modes[NR_C_MODES] =
6288 TFmode, TImode, TDmode,
6289 DFmode, DImode, DDmode,
6290 SFmode, SImode, SDmode,
6291 HImode,
6292 QImode
6295 struct constant
6297 struct constant *next;
6298 rtx value;
6299 rtx_code_label *label;
6302 struct constant_pool
6304 struct constant_pool *next;
6305 rtx_insn *first_insn;
6306 rtx_insn *pool_insn;
6307 bitmap insns;
6308 rtx_insn *emit_pool_after;
6310 struct constant *constants[NR_C_MODES];
6311 struct constant *execute;
6312 rtx_code_label *label;
6313 int size;
6316 /* Allocate new constant_pool structure. */
6318 static struct constant_pool *
6319 s390_alloc_pool (void)
6321 struct constant_pool *pool;
6322 int i;
6324 pool = (struct constant_pool *) xmalloc (sizeof *pool);
6325 pool->next = NULL;
6326 for (i = 0; i < NR_C_MODES; i++)
6327 pool->constants[i] = NULL;
6329 pool->execute = NULL;
6330 pool->label = gen_label_rtx ();
6331 pool->first_insn = NULL;
6332 pool->pool_insn = NULL;
6333 pool->insns = BITMAP_ALLOC (NULL);
6334 pool->size = 0;
6335 pool->emit_pool_after = NULL;
6337 return pool;
6340 /* Create new constant pool covering instructions starting at INSN
6341 and chain it to the end of POOL_LIST. */
6343 static struct constant_pool *
6344 s390_start_pool (struct constant_pool **pool_list, rtx_insn *insn)
6346 struct constant_pool *pool, **prev;
6348 pool = s390_alloc_pool ();
6349 pool->first_insn = insn;
6351 for (prev = pool_list; *prev; prev = &(*prev)->next)
6353 *prev = pool;
6355 return pool;
6358 /* End range of instructions covered by POOL at INSN and emit
6359 placeholder insn representing the pool. */
6361 static void
6362 s390_end_pool (struct constant_pool *pool, rtx_insn *insn)
6364 rtx pool_size = GEN_INT (pool->size + 8 /* alignment slop */);
6366 if (!insn)
6367 insn = get_last_insn ();
6369 pool->pool_insn = emit_insn_after (gen_pool (pool_size), insn);
6370 INSN_ADDRESSES_NEW (pool->pool_insn, -1);
6373 /* Add INSN to the list of insns covered by POOL. */
6375 static void
6376 s390_add_pool_insn (struct constant_pool *pool, rtx insn)
6378 bitmap_set_bit (pool->insns, INSN_UID (insn));
6381 /* Return pool out of POOL_LIST that covers INSN. */
6383 static struct constant_pool *
6384 s390_find_pool (struct constant_pool *pool_list, rtx insn)
6386 struct constant_pool *pool;
6388 for (pool = pool_list; pool; pool = pool->next)
6389 if (bitmap_bit_p (pool->insns, INSN_UID (insn)))
6390 break;
6392 return pool;
6395 /* Add constant VAL of mode MODE to the constant pool POOL. */
6397 static void
6398 s390_add_constant (struct constant_pool *pool, rtx val, machine_mode mode)
6400 struct constant *c;
6401 int i;
6403 for (i = 0; i < NR_C_MODES; i++)
6404 if (constant_modes[i] == mode)
6405 break;
6406 gcc_assert (i != NR_C_MODES);
6408 for (c = pool->constants[i]; c != NULL; c = c->next)
6409 if (rtx_equal_p (val, c->value))
6410 break;
6412 if (c == NULL)
6414 c = (struct constant *) xmalloc (sizeof *c);
6415 c->value = val;
6416 c->label = gen_label_rtx ();
6417 c->next = pool->constants[i];
6418 pool->constants[i] = c;
6419 pool->size += GET_MODE_SIZE (mode);
6423 /* Return an rtx that represents the offset of X from the start of
6424 pool POOL. */
6426 static rtx
6427 s390_pool_offset (struct constant_pool *pool, rtx x)
6429 rtx label;
6431 label = gen_rtx_LABEL_REF (GET_MODE (x), pool->label);
6432 x = gen_rtx_UNSPEC (GET_MODE (x), gen_rtvec (2, x, label),
6433 UNSPEC_POOL_OFFSET);
6434 return gen_rtx_CONST (GET_MODE (x), x);
6437 /* Find constant VAL of mode MODE in the constant pool POOL.
6438 Return an RTX describing the distance from the start of
6439 the pool to the location of the new constant. */
6441 static rtx
6442 s390_find_constant (struct constant_pool *pool, rtx val,
6443 machine_mode mode)
6445 struct constant *c;
6446 int i;
6448 for (i = 0; i < NR_C_MODES; i++)
6449 if (constant_modes[i] == mode)
6450 break;
6451 gcc_assert (i != NR_C_MODES);
6453 for (c = pool->constants[i]; c != NULL; c = c->next)
6454 if (rtx_equal_p (val, c->value))
6455 break;
6457 gcc_assert (c);
6459 return s390_pool_offset (pool, gen_rtx_LABEL_REF (Pmode, c->label));
6462 /* Check whether INSN is an execute. Return the label_ref to its
6463 execute target template if so, NULL_RTX otherwise. */
6465 static rtx
6466 s390_execute_label (rtx insn)
6468 if (NONJUMP_INSN_P (insn)
6469 && GET_CODE (PATTERN (insn)) == PARALLEL
6470 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == UNSPEC
6471 && XINT (XVECEXP (PATTERN (insn), 0, 0), 1) == UNSPEC_EXECUTE)
6472 return XVECEXP (XVECEXP (PATTERN (insn), 0, 0), 0, 2);
6474 return NULL_RTX;
6477 /* Add execute target for INSN to the constant pool POOL. */
6479 static void
6480 s390_add_execute (struct constant_pool *pool, rtx insn)
6482 struct constant *c;
6484 for (c = pool->execute; c != NULL; c = c->next)
6485 if (INSN_UID (insn) == INSN_UID (c->value))
6486 break;
6488 if (c == NULL)
6490 c = (struct constant *) xmalloc (sizeof *c);
6491 c->value = insn;
6492 c->label = gen_label_rtx ();
6493 c->next = pool->execute;
6494 pool->execute = c;
6495 pool->size += 6;
6499 /* Find execute target for INSN in the constant pool POOL.
6500 Return an RTX describing the distance from the start of
6501 the pool to the location of the execute target. */
6503 static rtx
6504 s390_find_execute (struct constant_pool *pool, rtx insn)
6506 struct constant *c;
6508 for (c = pool->execute; c != NULL; c = c->next)
6509 if (INSN_UID (insn) == INSN_UID (c->value))
6510 break;
6512 gcc_assert (c);
6514 return s390_pool_offset (pool, gen_rtx_LABEL_REF (Pmode, c->label));
6517 /* For an execute INSN, extract the execute target template. */
6519 static rtx
6520 s390_execute_target (rtx insn)
6522 rtx pattern = PATTERN (insn);
6523 gcc_assert (s390_execute_label (insn));
6525 if (XVECLEN (pattern, 0) == 2)
6527 pattern = copy_rtx (XVECEXP (pattern, 0, 1));
6529 else
6531 rtvec vec = rtvec_alloc (XVECLEN (pattern, 0) - 1);
6532 int i;
6534 for (i = 0; i < XVECLEN (pattern, 0) - 1; i++)
6535 RTVEC_ELT (vec, i) = copy_rtx (XVECEXP (pattern, 0, i + 1));
6537 pattern = gen_rtx_PARALLEL (VOIDmode, vec);
6540 return pattern;
6543 /* Indicate that INSN cannot be duplicated. This is the case for
6544 execute insns that carry a unique label. */
6546 static bool
6547 s390_cannot_copy_insn_p (rtx_insn *insn)
6549 rtx label = s390_execute_label (insn);
6550 return label && label != const0_rtx;
6553 /* Dump out the constants in POOL. If REMOTE_LABEL is true,
6554 do not emit the pool base label. */
6556 static void
6557 s390_dump_pool (struct constant_pool *pool, bool remote_label)
6559 struct constant *c;
6560 rtx_insn *insn = pool->pool_insn;
6561 int i;
6563 /* Switch to rodata section. */
6564 if (TARGET_CPU_ZARCH)
6566 insn = emit_insn_after (gen_pool_section_start (), insn);
6567 INSN_ADDRESSES_NEW (insn, -1);
6570 /* Ensure minimum pool alignment. */
6571 if (TARGET_CPU_ZARCH)
6572 insn = emit_insn_after (gen_pool_align (GEN_INT (8)), insn);
6573 else
6574 insn = emit_insn_after (gen_pool_align (GEN_INT (4)), insn);
6575 INSN_ADDRESSES_NEW (insn, -1);
6577 /* Emit pool base label. */
6578 if (!remote_label)
6580 insn = emit_label_after (pool->label, insn);
6581 INSN_ADDRESSES_NEW (insn, -1);
6584 /* Dump constants in descending alignment requirement order,
6585 ensuring proper alignment for every constant. */
6586 for (i = 0; i < NR_C_MODES; i++)
6587 for (c = pool->constants[i]; c; c = c->next)
6589 /* Convert UNSPEC_LTREL_OFFSET unspecs to pool-relative references. */
6590 rtx value = copy_rtx (c->value);
6591 if (GET_CODE (value) == CONST
6592 && GET_CODE (XEXP (value, 0)) == UNSPEC
6593 && XINT (XEXP (value, 0), 1) == UNSPEC_LTREL_OFFSET
6594 && XVECLEN (XEXP (value, 0), 0) == 1)
6595 value = s390_pool_offset (pool, XVECEXP (XEXP (value, 0), 0, 0));
6597 insn = emit_label_after (c->label, insn);
6598 INSN_ADDRESSES_NEW (insn, -1);
6600 value = gen_rtx_UNSPEC_VOLATILE (constant_modes[i],
6601 gen_rtvec (1, value),
6602 UNSPECV_POOL_ENTRY);
6603 insn = emit_insn_after (value, insn);
6604 INSN_ADDRESSES_NEW (insn, -1);
6607 /* Ensure minimum alignment for instructions. */
6608 insn = emit_insn_after (gen_pool_align (GEN_INT (2)), insn);
6609 INSN_ADDRESSES_NEW (insn, -1);
6611 /* Output in-pool execute template insns. */
6612 for (c = pool->execute; c; c = c->next)
6614 insn = emit_label_after (c->label, insn);
6615 INSN_ADDRESSES_NEW (insn, -1);
6617 insn = emit_insn_after (s390_execute_target (c->value), insn);
6618 INSN_ADDRESSES_NEW (insn, -1);
6621 /* Switch back to previous section. */
6622 if (TARGET_CPU_ZARCH)
6624 insn = emit_insn_after (gen_pool_section_end (), insn);
6625 INSN_ADDRESSES_NEW (insn, -1);
6628 insn = emit_barrier_after (insn);
6629 INSN_ADDRESSES_NEW (insn, -1);
6631 /* Remove placeholder insn. */
6632 remove_insn (pool->pool_insn);
6635 /* Free all memory used by POOL. */
6637 static void
6638 s390_free_pool (struct constant_pool *pool)
6640 struct constant *c, *next;
6641 int i;
6643 for (i = 0; i < NR_C_MODES; i++)
6644 for (c = pool->constants[i]; c; c = next)
6646 next = c->next;
6647 free (c);
6650 for (c = pool->execute; c; c = next)
6652 next = c->next;
6653 free (c);
6656 BITMAP_FREE (pool->insns);
6657 free (pool);
6661 /* Collect main literal pool. Return NULL on overflow. */
6663 static struct constant_pool *
6664 s390_mainpool_start (void)
6666 struct constant_pool *pool;
6667 rtx_insn *insn;
6669 pool = s390_alloc_pool ();
6671 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6673 if (NONJUMP_INSN_P (insn)
6674 && GET_CODE (PATTERN (insn)) == SET
6675 && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC_VOLATILE
6676 && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPECV_MAIN_POOL)
6678 /* There might be two main_pool instructions if base_reg
6679 is call-clobbered; one for shrink-wrapped code and one
6680 for the rest. We want to keep the first. */
6681 if (pool->pool_insn)
6683 insn = PREV_INSN (insn);
6684 delete_insn (NEXT_INSN (insn));
6685 continue;
6687 pool->pool_insn = insn;
6690 if (!TARGET_CPU_ZARCH && s390_execute_label (insn))
6692 s390_add_execute (pool, insn);
6694 else if (NONJUMP_INSN_P (insn) || CALL_P (insn))
6696 rtx pool_ref = NULL_RTX;
6697 find_constant_pool_ref (PATTERN (insn), &pool_ref);
6698 if (pool_ref)
6700 rtx constant = get_pool_constant (pool_ref);
6701 machine_mode mode = get_pool_mode (pool_ref);
6702 s390_add_constant (pool, constant, mode);
6706 /* If hot/cold partitioning is enabled we have to make sure that
6707 the literal pool is emitted in the same section where the
6708 initialization of the literal pool base pointer takes place.
6709 emit_pool_after is only used in the non-overflow case on non
6710 Z cpus where we can emit the literal pool at the end of the
6711 function body within the text section. */
6712 if (NOTE_P (insn)
6713 && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS
6714 && !pool->emit_pool_after)
6715 pool->emit_pool_after = PREV_INSN (insn);
6718 gcc_assert (pool->pool_insn || pool->size == 0);
6720 if (pool->size >= 4096)
6722 /* We're going to chunkify the pool, so remove the main
6723 pool placeholder insn. */
6724 remove_insn (pool->pool_insn);
6726 s390_free_pool (pool);
6727 pool = NULL;
6730 /* If the functions ends with the section where the literal pool
6731 should be emitted set the marker to its end. */
6732 if (pool && !pool->emit_pool_after)
6733 pool->emit_pool_after = get_last_insn ();
6735 return pool;
6738 /* POOL holds the main literal pool as collected by s390_mainpool_start.
6739 Modify the current function to output the pool constants as well as
6740 the pool register setup instruction. */
6742 static void
6743 s390_mainpool_finish (struct constant_pool *pool)
6745 rtx base_reg = cfun->machine->base_reg;
6747 /* If the pool is empty, we're done. */
6748 if (pool->size == 0)
6750 /* We don't actually need a base register after all. */
6751 cfun->machine->base_reg = NULL_RTX;
6753 if (pool->pool_insn)
6754 remove_insn (pool->pool_insn);
6755 s390_free_pool (pool);
6756 return;
6759 /* We need correct insn addresses. */
6760 shorten_branches (get_insns ());
6762 /* On zSeries, we use a LARL to load the pool register. The pool is
6763 located in the .rodata section, so we emit it after the function. */
6764 if (TARGET_CPU_ZARCH)
6766 rtx set = gen_main_base_64 (base_reg, pool->label);
6767 rtx_insn *insn = emit_insn_after (set, pool->pool_insn);
6768 INSN_ADDRESSES_NEW (insn, -1);
6769 remove_insn (pool->pool_insn);
6771 insn = get_last_insn ();
6772 pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
6773 INSN_ADDRESSES_NEW (pool->pool_insn, -1);
6775 s390_dump_pool (pool, 0);
6778 /* On S/390, if the total size of the function's code plus literal pool
6779 does not exceed 4096 bytes, we use BASR to set up a function base
6780 pointer, and emit the literal pool at the end of the function. */
6781 else if (INSN_ADDRESSES (INSN_UID (pool->emit_pool_after))
6782 + pool->size + 8 /* alignment slop */ < 4096)
6784 rtx set = gen_main_base_31_small (base_reg, pool->label);
6785 rtx_insn *insn = emit_insn_after (set, pool->pool_insn);
6786 INSN_ADDRESSES_NEW (insn, -1);
6787 remove_insn (pool->pool_insn);
6789 insn = emit_label_after (pool->label, insn);
6790 INSN_ADDRESSES_NEW (insn, -1);
6792 /* emit_pool_after will be set by s390_mainpool_start to the
6793 last insn of the section where the literal pool should be
6794 emitted. */
6795 insn = pool->emit_pool_after;
6797 pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
6798 INSN_ADDRESSES_NEW (pool->pool_insn, -1);
6800 s390_dump_pool (pool, 1);
6803 /* Otherwise, we emit an inline literal pool and use BASR to branch
6804 over it, setting up the pool register at the same time. */
6805 else
6807 rtx_code_label *pool_end = gen_label_rtx ();
6809 rtx pat = gen_main_base_31_large (base_reg, pool->label, pool_end);
6810 rtx_insn *insn = emit_jump_insn_after (pat, pool->pool_insn);
6811 JUMP_LABEL (insn) = pool_end;
6812 INSN_ADDRESSES_NEW (insn, -1);
6813 remove_insn (pool->pool_insn);
6815 insn = emit_label_after (pool->label, insn);
6816 INSN_ADDRESSES_NEW (insn, -1);
6818 pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
6819 INSN_ADDRESSES_NEW (pool->pool_insn, -1);
6821 insn = emit_label_after (pool_end, pool->pool_insn);
6822 INSN_ADDRESSES_NEW (insn, -1);
6824 s390_dump_pool (pool, 1);
6828 /* Replace all literal pool references. */
6830 for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
6832 if (INSN_P (insn))
6833 replace_ltrel_base (&PATTERN (insn));
6835 if (NONJUMP_INSN_P (insn) || CALL_P (insn))
6837 rtx addr, pool_ref = NULL_RTX;
6838 find_constant_pool_ref (PATTERN (insn), &pool_ref);
6839 if (pool_ref)
6841 if (s390_execute_label (insn))
6842 addr = s390_find_execute (pool, insn);
6843 else
6844 addr = s390_find_constant (pool, get_pool_constant (pool_ref),
6845 get_pool_mode (pool_ref));
6847 replace_constant_pool_ref (&PATTERN (insn), pool_ref, addr);
6848 INSN_CODE (insn) = -1;
6854 /* Free the pool. */
6855 s390_free_pool (pool);
6858 /* POOL holds the main literal pool as collected by s390_mainpool_start.
6859 We have decided we cannot use this pool, so revert all changes
6860 to the current function that were done by s390_mainpool_start. */
6861 static void
6862 s390_mainpool_cancel (struct constant_pool *pool)
6864 /* We didn't actually change the instruction stream, so simply
6865 free the pool memory. */
6866 s390_free_pool (pool);
6870 /* Chunkify the literal pool. */
6872 #define S390_POOL_CHUNK_MIN 0xc00
6873 #define S390_POOL_CHUNK_MAX 0xe00
6875 static struct constant_pool *
6876 s390_chunkify_start (void)
6878 struct constant_pool *curr_pool = NULL, *pool_list = NULL;
6879 int extra_size = 0;
6880 bitmap far_labels;
6881 rtx pending_ltrel = NULL_RTX;
6882 rtx_insn *insn;
6884 rtx (*gen_reload_base) (rtx, rtx) =
6885 TARGET_CPU_ZARCH? gen_reload_base_64 : gen_reload_base_31;
6888 /* We need correct insn addresses. */
6890 shorten_branches (get_insns ());
6892 /* Scan all insns and move literals to pool chunks. */
6894 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6896 bool section_switch_p = false;
6898 /* Check for pending LTREL_BASE. */
6899 if (INSN_P (insn))
6901 rtx ltrel_base = find_ltrel_base (PATTERN (insn));
6902 if (ltrel_base)
6904 gcc_assert (ltrel_base == pending_ltrel);
6905 pending_ltrel = NULL_RTX;
6909 if (!TARGET_CPU_ZARCH && s390_execute_label (insn))
6911 if (!curr_pool)
6912 curr_pool = s390_start_pool (&pool_list, insn);
6914 s390_add_execute (curr_pool, insn);
6915 s390_add_pool_insn (curr_pool, insn);
6917 else if (NONJUMP_INSN_P (insn) || CALL_P (insn))
6919 rtx pool_ref = NULL_RTX;
6920 find_constant_pool_ref (PATTERN (insn), &pool_ref);
6921 if (pool_ref)
6923 rtx constant = get_pool_constant (pool_ref);
6924 machine_mode mode = get_pool_mode (pool_ref);
6926 if (!curr_pool)
6927 curr_pool = s390_start_pool (&pool_list, insn);
6929 s390_add_constant (curr_pool, constant, mode);
6930 s390_add_pool_insn (curr_pool, insn);
6932 /* Don't split the pool chunk between a LTREL_OFFSET load
6933 and the corresponding LTREL_BASE. */
6934 if (GET_CODE (constant) == CONST
6935 && GET_CODE (XEXP (constant, 0)) == UNSPEC
6936 && XINT (XEXP (constant, 0), 1) == UNSPEC_LTREL_OFFSET)
6938 gcc_assert (!pending_ltrel);
6939 pending_ltrel = pool_ref;
6944 if (JUMP_P (insn) || JUMP_TABLE_DATA_P (insn) || LABEL_P (insn))
6946 if (curr_pool)
6947 s390_add_pool_insn (curr_pool, insn);
6948 /* An LTREL_BASE must follow within the same basic block. */
6949 gcc_assert (!pending_ltrel);
6952 if (NOTE_P (insn))
6953 switch (NOTE_KIND (insn))
6955 case NOTE_INSN_SWITCH_TEXT_SECTIONS:
6956 section_switch_p = true;
6957 break;
6958 case NOTE_INSN_VAR_LOCATION:
6959 case NOTE_INSN_CALL_ARG_LOCATION:
6960 continue;
6961 default:
6962 break;
6965 if (!curr_pool
6966 || INSN_ADDRESSES_SIZE () <= (size_t) INSN_UID (insn)
6967 || INSN_ADDRESSES (INSN_UID (insn)) == -1)
6968 continue;
6970 if (TARGET_CPU_ZARCH)
6972 if (curr_pool->size < S390_POOL_CHUNK_MAX)
6973 continue;
6975 s390_end_pool (curr_pool, NULL);
6976 curr_pool = NULL;
6978 else
6980 int chunk_size = INSN_ADDRESSES (INSN_UID (insn))
6981 - INSN_ADDRESSES (INSN_UID (curr_pool->first_insn))
6982 + extra_size;
6984 /* We will later have to insert base register reload insns.
6985 Those will have an effect on code size, which we need to
6986 consider here. This calculation makes rather pessimistic
6987 worst-case assumptions. */
6988 if (LABEL_P (insn))
6989 extra_size += 6;
6991 if (chunk_size < S390_POOL_CHUNK_MIN
6992 && curr_pool->size < S390_POOL_CHUNK_MIN
6993 && !section_switch_p)
6994 continue;
6996 /* Pool chunks can only be inserted after BARRIERs ... */
6997 if (BARRIER_P (insn))
6999 s390_end_pool (curr_pool, insn);
7000 curr_pool = NULL;
7001 extra_size = 0;
7004 /* ... so if we don't find one in time, create one. */
7005 else if (chunk_size > S390_POOL_CHUNK_MAX
7006 || curr_pool->size > S390_POOL_CHUNK_MAX
7007 || section_switch_p)
7009 rtx_insn *label, *jump, *barrier, *next, *prev;
7011 if (!section_switch_p)
7013 /* We can insert the barrier only after a 'real' insn. */
7014 if (! NONJUMP_INSN_P (insn) && ! CALL_P (insn))
7015 continue;
7016 if (get_attr_length (insn) == 0)
7017 continue;
7018 /* Don't separate LTREL_BASE from the corresponding
7019 LTREL_OFFSET load. */
7020 if (pending_ltrel)
7021 continue;
7022 next = insn;
7025 insn = next;
7026 next = NEXT_INSN (insn);
7028 while (next
7029 && NOTE_P (next)
7030 && (NOTE_KIND (next) == NOTE_INSN_VAR_LOCATION
7031 || NOTE_KIND (next) == NOTE_INSN_CALL_ARG_LOCATION));
7033 else
7035 gcc_assert (!pending_ltrel);
7037 /* The old pool has to end before the section switch
7038 note in order to make it part of the current
7039 section. */
7040 insn = PREV_INSN (insn);
7043 label = gen_label_rtx ();
7044 prev = insn;
7045 if (prev && NOTE_P (prev))
7046 prev = prev_nonnote_insn (prev);
7047 if (prev)
7048 jump = emit_jump_insn_after_setloc (gen_jump (label), insn,
7049 INSN_LOCATION (prev));
7050 else
7051 jump = emit_jump_insn_after_noloc (gen_jump (label), insn);
7052 barrier = emit_barrier_after (jump);
7053 insn = emit_label_after (label, barrier);
7054 JUMP_LABEL (jump) = label;
7055 LABEL_NUSES (label) = 1;
7057 INSN_ADDRESSES_NEW (jump, -1);
7058 INSN_ADDRESSES_NEW (barrier, -1);
7059 INSN_ADDRESSES_NEW (insn, -1);
7061 s390_end_pool (curr_pool, barrier);
7062 curr_pool = NULL;
7063 extra_size = 0;
7068 if (curr_pool)
7069 s390_end_pool (curr_pool, NULL);
7070 gcc_assert (!pending_ltrel);
7072 /* Find all labels that are branched into
7073 from an insn belonging to a different chunk. */
7075 far_labels = BITMAP_ALLOC (NULL);
7077 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
7079 rtx_jump_table_data *table;
7081 /* Labels marked with LABEL_PRESERVE_P can be target
7082 of non-local jumps, so we have to mark them.
7083 The same holds for named labels.
7085 Don't do that, however, if it is the label before
7086 a jump table. */
7088 if (LABEL_P (insn)
7089 && (LABEL_PRESERVE_P (insn) || LABEL_NAME (insn)))
7091 rtx_insn *vec_insn = NEXT_INSN (insn);
7092 if (! vec_insn || ! JUMP_TABLE_DATA_P (vec_insn))
7093 bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (insn));
7095 /* Check potential targets in a table jump (casesi_jump). */
7096 else if (tablejump_p (insn, NULL, &table))
7098 rtx vec_pat = PATTERN (table);
7099 int i, diff_p = GET_CODE (vec_pat) == ADDR_DIFF_VEC;
7101 for (i = 0; i < XVECLEN (vec_pat, diff_p); i++)
7103 rtx label = XEXP (XVECEXP (vec_pat, diff_p, i), 0);
7105 if (s390_find_pool (pool_list, label)
7106 != s390_find_pool (pool_list, insn))
7107 bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (label));
7110 /* If we have a direct jump (conditional or unconditional),
7111 check all potential targets. */
7112 else if (JUMP_P (insn))
7114 rtx pat = PATTERN (insn);
7116 if (GET_CODE (pat) == PARALLEL)
7117 pat = XVECEXP (pat, 0, 0);
7119 if (GET_CODE (pat) == SET)
7121 rtx label = JUMP_LABEL (insn);
7122 if (label && !ANY_RETURN_P (label))
7124 if (s390_find_pool (pool_list, label)
7125 != s390_find_pool (pool_list, insn))
7126 bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (label));
7132 /* Insert base register reload insns before every pool. */
7134 for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
7136 rtx new_insn = gen_reload_base (cfun->machine->base_reg,
7137 curr_pool->label);
7138 rtx_insn *insn = curr_pool->first_insn;
7139 INSN_ADDRESSES_NEW (emit_insn_before (new_insn, insn), -1);
7142 /* Insert base register reload insns at every far label. */
7144 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
7145 if (LABEL_P (insn)
7146 && bitmap_bit_p (far_labels, CODE_LABEL_NUMBER (insn)))
7148 struct constant_pool *pool = s390_find_pool (pool_list, insn);
7149 if (pool)
7151 rtx new_insn = gen_reload_base (cfun->machine->base_reg,
7152 pool->label);
7153 INSN_ADDRESSES_NEW (emit_insn_after (new_insn, insn), -1);
7158 BITMAP_FREE (far_labels);
7161 /* Recompute insn addresses. */
7163 init_insn_lengths ();
7164 shorten_branches (get_insns ());
7166 return pool_list;
7169 /* POOL_LIST is a chunk list as prepared by s390_chunkify_start.
7170 After we have decided to use this list, finish implementing
7171 all changes to the current function as required. */
7173 static void
7174 s390_chunkify_finish (struct constant_pool *pool_list)
7176 struct constant_pool *curr_pool = NULL;
7177 rtx_insn *insn;
7180 /* Replace all literal pool references. */
7182 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
7184 if (INSN_P (insn))
7185 replace_ltrel_base (&PATTERN (insn));
7187 curr_pool = s390_find_pool (pool_list, insn);
7188 if (!curr_pool)
7189 continue;
7191 if (NONJUMP_INSN_P (insn) || CALL_P (insn))
7193 rtx addr, pool_ref = NULL_RTX;
7194 find_constant_pool_ref (PATTERN (insn), &pool_ref);
7195 if (pool_ref)
7197 if (s390_execute_label (insn))
7198 addr = s390_find_execute (curr_pool, insn);
7199 else
7200 addr = s390_find_constant (curr_pool,
7201 get_pool_constant (pool_ref),
7202 get_pool_mode (pool_ref));
7204 replace_constant_pool_ref (&PATTERN (insn), pool_ref, addr);
7205 INSN_CODE (insn) = -1;
7210 /* Dump out all literal pools. */
7212 for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
7213 s390_dump_pool (curr_pool, 0);
7215 /* Free pool list. */
7217 while (pool_list)
7219 struct constant_pool *next = pool_list->next;
7220 s390_free_pool (pool_list);
7221 pool_list = next;
7225 /* POOL_LIST is a chunk list as prepared by s390_chunkify_start.
7226 We have decided we cannot use this list, so revert all changes
7227 to the current function that were done by s390_chunkify_start. */
7229 static void
7230 s390_chunkify_cancel (struct constant_pool *pool_list)
7232 struct constant_pool *curr_pool = NULL;
7233 rtx_insn *insn;
7235 /* Remove all pool placeholder insns. */
7237 for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
7239 /* Did we insert an extra barrier? Remove it. */
7240 rtx_insn *barrier = PREV_INSN (curr_pool->pool_insn);
7241 rtx_insn *jump = barrier? PREV_INSN (barrier) : NULL;
7242 rtx_insn *label = NEXT_INSN (curr_pool->pool_insn);
7244 if (jump && JUMP_P (jump)
7245 && barrier && BARRIER_P (barrier)
7246 && label && LABEL_P (label)
7247 && GET_CODE (PATTERN (jump)) == SET
7248 && SET_DEST (PATTERN (jump)) == pc_rtx
7249 && GET_CODE (SET_SRC (PATTERN (jump))) == LABEL_REF
7250 && XEXP (SET_SRC (PATTERN (jump)), 0) == label)
7252 remove_insn (jump);
7253 remove_insn (barrier);
7254 remove_insn (label);
7257 remove_insn (curr_pool->pool_insn);
7260 /* Remove all base register reload insns. */
7262 for (insn = get_insns (); insn; )
7264 rtx_insn *next_insn = NEXT_INSN (insn);
7266 if (NONJUMP_INSN_P (insn)
7267 && GET_CODE (PATTERN (insn)) == SET
7268 && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC
7269 && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPEC_RELOAD_BASE)
7270 remove_insn (insn);
7272 insn = next_insn;
7275 /* Free pool list. */
7277 while (pool_list)
7279 struct constant_pool *next = pool_list->next;
7280 s390_free_pool (pool_list);
7281 pool_list = next;
7285 /* Output the constant pool entry EXP in mode MODE with alignment ALIGN. */
7287 void
7288 s390_output_pool_entry (rtx exp, machine_mode mode, unsigned int align)
7290 REAL_VALUE_TYPE r;
7292 switch (GET_MODE_CLASS (mode))
7294 case MODE_FLOAT:
7295 case MODE_DECIMAL_FLOAT:
7296 gcc_assert (GET_CODE (exp) == CONST_DOUBLE);
7298 REAL_VALUE_FROM_CONST_DOUBLE (r, exp);
7299 assemble_real (r, mode, align);
7300 break;
7302 case MODE_INT:
7303 assemble_integer (exp, GET_MODE_SIZE (mode), align, 1);
7304 mark_symbol_refs_as_used (exp);
7305 break;
7307 default:
7308 gcc_unreachable ();
7313 /* Return an RTL expression representing the value of the return address
7314 for the frame COUNT steps up from the current frame. FRAME is the
7315 frame pointer of that frame. */
7318 s390_return_addr_rtx (int count, rtx frame ATTRIBUTE_UNUSED)
7320 int offset;
7321 rtx addr;
7323 /* Without backchain, we fail for all but the current frame. */
7325 if (!TARGET_BACKCHAIN && count > 0)
7326 return NULL_RTX;
7328 /* For the current frame, we need to make sure the initial
7329 value of RETURN_REGNUM is actually saved. */
7331 if (count == 0)
7333 /* On non-z architectures branch splitting could overwrite r14. */
7334 if (TARGET_CPU_ZARCH)
7335 return get_hard_reg_initial_val (Pmode, RETURN_REGNUM);
7336 else
7338 cfun_frame_layout.save_return_addr_p = true;
7339 return gen_rtx_MEM (Pmode, return_address_pointer_rtx);
7343 if (TARGET_PACKED_STACK)
7344 offset = -2 * UNITS_PER_LONG;
7345 else
7346 offset = RETURN_REGNUM * UNITS_PER_LONG;
7348 addr = plus_constant (Pmode, frame, offset);
7349 addr = memory_address (Pmode, addr);
7350 return gen_rtx_MEM (Pmode, addr);
7353 /* Return an RTL expression representing the back chain stored in
7354 the current stack frame. */
7357 s390_back_chain_rtx (void)
7359 rtx chain;
7361 gcc_assert (TARGET_BACKCHAIN);
7363 if (TARGET_PACKED_STACK)
7364 chain = plus_constant (Pmode, stack_pointer_rtx,
7365 STACK_POINTER_OFFSET - UNITS_PER_LONG);
7366 else
7367 chain = stack_pointer_rtx;
7369 chain = gen_rtx_MEM (Pmode, chain);
7370 return chain;
7373 /* Find first call clobbered register unused in a function.
7374 This could be used as base register in a leaf function
7375 or for holding the return address before epilogue. */
7377 static int
7378 find_unused_clobbered_reg (void)
7380 int i;
7381 for (i = 0; i < 6; i++)
7382 if (!df_regs_ever_live_p (i))
7383 return i;
7384 return 0;
7388 /* Helper function for s390_regs_ever_clobbered. Sets the fields in DATA for all
7389 clobbered hard regs in SETREG. */
7391 static void
7392 s390_reg_clobbered_rtx (rtx setreg, const_rtx set_insn ATTRIBUTE_UNUSED, void *data)
7394 char *regs_ever_clobbered = (char *)data;
7395 unsigned int i, regno;
7396 machine_mode mode = GET_MODE (setreg);
7398 if (GET_CODE (setreg) == SUBREG)
7400 rtx inner = SUBREG_REG (setreg);
7401 if (!GENERAL_REG_P (inner) && !FP_REG_P (inner))
7402 return;
7403 regno = subreg_regno (setreg);
7405 else if (GENERAL_REG_P (setreg) || FP_REG_P (setreg))
7406 regno = REGNO (setreg);
7407 else
7408 return;
7410 for (i = regno;
7411 i < regno + HARD_REGNO_NREGS (regno, mode);
7412 i++)
7413 regs_ever_clobbered[i] = 1;
7416 /* Walks through all basic blocks of the current function looking
7417 for clobbered hard regs using s390_reg_clobbered_rtx. The fields
7418 of the passed integer array REGS_EVER_CLOBBERED are set to one for
7419 each of those regs. */
7421 static void
7422 s390_regs_ever_clobbered (char regs_ever_clobbered[])
7424 basic_block cur_bb;
7425 rtx_insn *cur_insn;
7426 unsigned int i;
7428 memset (regs_ever_clobbered, 0, 32);
7430 /* For non-leaf functions we have to consider all call clobbered regs to be
7431 clobbered. */
7432 if (!crtl->is_leaf)
7434 for (i = 0; i < 32; i++)
7435 regs_ever_clobbered[i] = call_really_used_regs[i];
7438 /* Make the "magic" eh_return registers live if necessary. For regs_ever_live
7439 this work is done by liveness analysis (mark_regs_live_at_end).
7440 Special care is needed for functions containing landing pads. Landing pads
7441 may use the eh registers, but the code which sets these registers is not
7442 contained in that function. Hence s390_regs_ever_clobbered is not able to
7443 deal with this automatically. */
7444 if (crtl->calls_eh_return || cfun->machine->has_landing_pad_p)
7445 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM ; i++)
7446 if (crtl->calls_eh_return
7447 || (cfun->machine->has_landing_pad_p
7448 && df_regs_ever_live_p (EH_RETURN_DATA_REGNO (i))))
7449 regs_ever_clobbered[EH_RETURN_DATA_REGNO (i)] = 1;
7451 /* For nonlocal gotos all call-saved registers have to be saved.
7452 This flag is also set for the unwinding code in libgcc.
7453 See expand_builtin_unwind_init. For regs_ever_live this is done by
7454 reload. */
7455 if (crtl->saves_all_registers)
7456 for (i = 0; i < 32; i++)
7457 if (!call_really_used_regs[i])
7458 regs_ever_clobbered[i] = 1;
7460 FOR_EACH_BB_FN (cur_bb, cfun)
7462 FOR_BB_INSNS (cur_bb, cur_insn)
7464 rtx pat;
7466 if (!INSN_P (cur_insn))
7467 continue;
7469 pat = PATTERN (cur_insn);
7471 /* Ignore GPR restore insns. */
7472 if (epilogue_completed && RTX_FRAME_RELATED_P (cur_insn))
7474 if (GET_CODE (pat) == SET
7475 && GENERAL_REG_P (SET_DEST (pat)))
7477 /* lgdr */
7478 if (GET_MODE (SET_SRC (pat)) == DImode
7479 && FP_REG_P (SET_SRC (pat)))
7480 continue;
7482 /* l / lg */
7483 if (GET_CODE (SET_SRC (pat)) == MEM)
7484 continue;
7487 /* lm / lmg */
7488 if (GET_CODE (pat) == PARALLEL
7489 && load_multiple_operation (pat, VOIDmode))
7490 continue;
7493 note_stores (pat,
7494 s390_reg_clobbered_rtx,
7495 regs_ever_clobbered);
7500 /* Determine the frame area which actually has to be accessed
7501 in the function epilogue. The values are stored at the
7502 given pointers AREA_BOTTOM (address of the lowest used stack
7503 address) and AREA_TOP (address of the first item which does
7504 not belong to the stack frame). */
7506 static void
7507 s390_frame_area (int *area_bottom, int *area_top)
7509 int b, t;
7511 b = INT_MAX;
7512 t = INT_MIN;
7514 if (cfun_frame_layout.first_restore_gpr != -1)
7516 b = (cfun_frame_layout.gprs_offset
7517 + cfun_frame_layout.first_restore_gpr * UNITS_PER_LONG);
7518 t = b + (cfun_frame_layout.last_restore_gpr
7519 - cfun_frame_layout.first_restore_gpr + 1) * UNITS_PER_LONG;
7522 if (TARGET_64BIT && cfun_save_high_fprs_p)
7524 b = MIN (b, cfun_frame_layout.f8_offset);
7525 t = MAX (t, (cfun_frame_layout.f8_offset
7526 + cfun_frame_layout.high_fprs * 8));
7529 if (!TARGET_64BIT)
7531 if (cfun_fpr_save_p (FPR4_REGNUM))
7533 b = MIN (b, cfun_frame_layout.f4_offset);
7534 t = MAX (t, cfun_frame_layout.f4_offset + 8);
7536 if (cfun_fpr_save_p (FPR6_REGNUM))
7538 b = MIN (b, cfun_frame_layout.f4_offset + 8);
7539 t = MAX (t, cfun_frame_layout.f4_offset + 16);
7542 *area_bottom = b;
7543 *area_top = t;
7545 /* Update gpr_save_slots in the frame layout trying to make use of
7546 FPRs as GPR save slots.
7547 This is a helper routine of s390_register_info. */
7549 static void
7550 s390_register_info_gprtofpr ()
7552 int save_reg_slot = FPR0_REGNUM;
7553 int i, j;
7555 if (!TARGET_Z10 || !TARGET_HARD_FLOAT || !crtl->is_leaf)
7556 return;
7558 for (i = 15; i >= 6; i--)
7560 if (cfun_gpr_save_slot (i) == 0)
7561 continue;
7563 /* Advance to the next FP register which can be used as a
7564 GPR save slot. */
7565 while ((!call_really_used_regs[save_reg_slot]
7566 || df_regs_ever_live_p (save_reg_slot)
7567 || cfun_fpr_save_p (save_reg_slot))
7568 && FP_REGNO_P (save_reg_slot))
7569 save_reg_slot++;
7570 if (!FP_REGNO_P (save_reg_slot))
7572 /* We only want to use ldgr/lgdr if we can get rid of
7573 stm/lm entirely. So undo the gpr slot allocation in
7574 case we ran out of FPR save slots. */
7575 for (j = 6; j <= 15; j++)
7576 if (FP_REGNO_P (cfun_gpr_save_slot (j)))
7577 cfun_gpr_save_slot (j) = -1;
7578 break;
7580 cfun_gpr_save_slot (i) = save_reg_slot++;
7584 /* Set the bits in fpr_bitmap for FPRs which need to be saved due to
7585 stdarg.
7586 This is a helper routine for s390_register_info. */
7588 static void
7589 s390_register_info_stdarg_fpr ()
7591 int i;
7592 int min_fpr;
7593 int max_fpr;
7595 /* Save the FP argument regs for stdarg. f0, f2 for 31 bit and
7596 f0-f4 for 64 bit. */
7597 if (!cfun->stdarg
7598 || !TARGET_HARD_FLOAT
7599 || !cfun->va_list_fpr_size
7600 || crtl->args.info.fprs >= FP_ARG_NUM_REG)
7601 return;
7603 min_fpr = crtl->args.info.fprs;
7604 max_fpr = min_fpr + cfun->va_list_fpr_size;
7605 if (max_fpr > FP_ARG_NUM_REG)
7606 max_fpr = FP_ARG_NUM_REG;
7608 for (i = min_fpr; i < max_fpr; i++)
7609 cfun_set_fpr_save (i + FPR0_REGNUM);
7612 /* Reserve the GPR save slots for GPRs which need to be saved due to
7613 stdarg.
7614 This is a helper routine for s390_register_info. */
7616 static void
7617 s390_register_info_stdarg_gpr ()
7619 int i;
7620 int min_gpr;
7621 int max_gpr;
7623 if (!cfun->stdarg
7624 || !cfun->va_list_gpr_size
7625 || crtl->args.info.gprs >= GP_ARG_NUM_REG)
7626 return;
7628 min_gpr = crtl->args.info.gprs;
7629 max_gpr = min_gpr + cfun->va_list_gpr_size;
7630 if (max_gpr > GP_ARG_NUM_REG)
7631 max_gpr = GP_ARG_NUM_REG;
7633 for (i = min_gpr; i < max_gpr; i++)
7634 cfun_gpr_save_slot (2 + i) = -1;
7637 /* The GPR and FPR save slots in cfun->machine->frame_layout are set
7638 for registers which need to be saved in function prologue.
7639 This function can be used until the insns emitted for save/restore
7640 of the regs are visible in the RTL stream. */
7642 static void
7643 s390_register_info ()
7645 int i, j;
7646 char clobbered_regs[32];
7648 gcc_assert (!epilogue_completed);
7650 if (reload_completed)
7651 /* After reload we rely on our own routine to determine which
7652 registers need saving. */
7653 s390_regs_ever_clobbered (clobbered_regs);
7654 else
7655 /* During reload we use regs_ever_live as a base since reload
7656 does changes in there which we otherwise would not be aware
7657 of. */
7658 for (i = 0; i < 32; i++)
7659 clobbered_regs[i] = df_regs_ever_live_p (i);
7661 for (i = 0; i < 32; i++)
7662 clobbered_regs[i] = clobbered_regs[i] && !global_regs[i];
7664 /* Mark the call-saved FPRs which need to be saved.
7665 This needs to be done before checking the special GPRs since the
7666 stack pointer usage depends on whether high FPRs have to be saved
7667 or not. */
7668 cfun_frame_layout.fpr_bitmap = 0;
7669 cfun_frame_layout.high_fprs = 0;
7670 for (i = FPR0_REGNUM; i <= FPR15_REGNUM; i++)
7671 if (clobbered_regs[i] && !call_really_used_regs[i])
7673 cfun_set_fpr_save (i);
7674 if (i >= FPR8_REGNUM)
7675 cfun_frame_layout.high_fprs++;
7678 if (flag_pic)
7679 clobbered_regs[PIC_OFFSET_TABLE_REGNUM]
7680 |= !!df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM);
7682 clobbered_regs[BASE_REGNUM]
7683 |= (cfun->machine->base_reg
7684 && REGNO (cfun->machine->base_reg) == BASE_REGNUM);
7686 clobbered_regs[HARD_FRAME_POINTER_REGNUM]
7687 |= !!frame_pointer_needed;
7689 /* On pre z900 machines this might take until machine dependent
7690 reorg to decide.
7691 save_return_addr_p will only be set on non-zarch machines so
7692 there is no risk that r14 goes into an FPR instead of a stack
7693 slot. */
7694 clobbered_regs[RETURN_REGNUM]
7695 |= (!crtl->is_leaf
7696 || TARGET_TPF_PROFILING
7697 || cfun->machine->split_branches_pending_p
7698 || cfun_frame_layout.save_return_addr_p
7699 || crtl->calls_eh_return);
7701 clobbered_regs[STACK_POINTER_REGNUM]
7702 |= (!crtl->is_leaf
7703 || TARGET_TPF_PROFILING
7704 || cfun_save_high_fprs_p
7705 || get_frame_size () > 0
7706 || (reload_completed && cfun_frame_layout.frame_size > 0)
7707 || cfun->calls_alloca);
7709 memset (cfun_frame_layout.gpr_save_slots, 0, 16);
7711 for (i = 6; i < 16; i++)
7712 if (clobbered_regs[i])
7713 cfun_gpr_save_slot (i) = -1;
7715 s390_register_info_stdarg_fpr ();
7716 s390_register_info_gprtofpr ();
7718 /* First find the range of GPRs to be restored. Vararg regs don't
7719 need to be restored so we do it before assigning slots to the
7720 vararg GPRs. */
7721 for (i = 0; i < 16 && cfun_gpr_save_slot (i) != -1; i++);
7722 for (j = 15; j > i && cfun_gpr_save_slot (j) != -1; j--);
7723 cfun_frame_layout.first_restore_gpr = (i == 16) ? -1 : i;
7724 cfun_frame_layout.last_restore_gpr = (i == 16) ? -1 : j;
7726 /* stdarg functions might need to save GPRs 2 to 6. This might
7727 override the GPR->FPR save decision made above for r6 since
7728 vararg regs must go to the stack. */
7729 s390_register_info_stdarg_gpr ();
7731 /* Now the range of GPRs which need saving. */
7732 for (i = 0; i < 16 && cfun_gpr_save_slot (i) != -1; i++);
7733 for (j = 15; j > i && cfun_gpr_save_slot (j) != -1; j--);
7734 cfun_frame_layout.first_save_gpr = (i == 16) ? -1 : i;
7735 cfun_frame_layout.last_save_gpr = (i == 16) ? -1 : j;
7738 /* This function is called by s390_optimize_prologue in order to get
7739 rid of unnecessary GPR save/restore instructions. The register info
7740 for the GPRs is re-computed and the ranges are re-calculated. */
7742 static void
7743 s390_optimize_register_info ()
7745 char clobbered_regs[32];
7746 int i, j;
7748 gcc_assert (epilogue_completed);
7749 gcc_assert (!cfun->machine->split_branches_pending_p);
7751 s390_regs_ever_clobbered (clobbered_regs);
7753 for (i = 0; i < 32; i++)
7754 clobbered_regs[i] = clobbered_regs[i] && !global_regs[i];
7756 /* There is still special treatment needed for cases invisible to
7757 s390_regs_ever_clobbered. */
7758 clobbered_regs[RETURN_REGNUM]
7759 |= (TARGET_TPF_PROFILING
7760 /* When expanding builtin_return_addr in ESA mode we do not
7761 know whether r14 will later be needed as scratch reg when
7762 doing branch splitting. So the builtin always accesses the
7763 r14 save slot and we need to stick to the save/restore
7764 decision for r14 even if it turns out that it didn't get
7765 clobbered. */
7766 || cfun_frame_layout.save_return_addr_p
7767 || crtl->calls_eh_return);
7769 memset (cfun_frame_layout.gpr_save_slots, 0, 6);
7771 for (i = 6; i < 16; i++)
7772 if (!clobbered_regs[i])
7773 cfun_gpr_save_slot (i) = 0;
7775 for (i = 0; i < 16 && cfun_gpr_save_slot (i) != -1; i++);
7776 for (j = 15; j > i && cfun_gpr_save_slot (j) != -1; j--);
7777 cfun_frame_layout.first_restore_gpr = (i == 16) ? -1 : i;
7778 cfun_frame_layout.last_restore_gpr = (i == 16) ? -1 : j;
7780 s390_register_info_stdarg_gpr ();
7782 for (i = 0; i < 16 && cfun_gpr_save_slot (i) != -1; i++);
7783 for (j = 15; j > i && cfun_gpr_save_slot (j) != -1; j--);
7784 cfun_frame_layout.first_save_gpr = (i == 16) ? -1 : i;
7785 cfun_frame_layout.last_save_gpr = (i == 16) ? -1 : j;
7788 /* Fill cfun->machine with info about frame of current function. */
7790 static void
7791 s390_frame_info (void)
7793 HOST_WIDE_INT lowest_offset;
7795 cfun_frame_layout.first_save_gpr_slot = cfun_frame_layout.first_save_gpr;
7796 cfun_frame_layout.last_save_gpr_slot = cfun_frame_layout.last_save_gpr;
7798 /* The va_arg builtin uses a constant distance of 16 *
7799 UNITS_PER_LONG (r0-r15) to reach the FPRs from the reg_save_area
7800 pointer. So even if we are going to save the stack pointer in an
7801 FPR we need the stack space in order to keep the offsets
7802 correct. */
7803 if (cfun->stdarg && cfun_save_arg_fprs_p)
7805 cfun_frame_layout.last_save_gpr_slot = STACK_POINTER_REGNUM;
7807 if (cfun_frame_layout.first_save_gpr_slot == -1)
7808 cfun_frame_layout.first_save_gpr_slot = STACK_POINTER_REGNUM;
7811 cfun_frame_layout.frame_size = get_frame_size ();
7812 if (!TARGET_64BIT && cfun_frame_layout.frame_size > 0x7fff0000)
7813 fatal_error (input_location,
7814 "total size of local variables exceeds architecture limit");
7816 if (!TARGET_PACKED_STACK)
7818 /* Fixed stack layout. */
7819 cfun_frame_layout.backchain_offset = 0;
7820 cfun_frame_layout.f0_offset = 16 * UNITS_PER_LONG;
7821 cfun_frame_layout.f4_offset = cfun_frame_layout.f0_offset + 2 * 8;
7822 cfun_frame_layout.f8_offset = -cfun_frame_layout.high_fprs * 8;
7823 cfun_frame_layout.gprs_offset = (cfun_frame_layout.first_save_gpr_slot
7824 * UNITS_PER_LONG);
7826 else if (TARGET_BACKCHAIN)
7828 /* Kernel stack layout - packed stack, backchain, no float */
7829 gcc_assert (TARGET_SOFT_FLOAT);
7830 cfun_frame_layout.backchain_offset = (STACK_POINTER_OFFSET
7831 - UNITS_PER_LONG);
7833 /* The distance between the backchain and the return address
7834 save slot must not change. So we always need a slot for the
7835 stack pointer which resides in between. */
7836 cfun_frame_layout.last_save_gpr_slot = STACK_POINTER_REGNUM;
7838 cfun_frame_layout.gprs_offset
7839 = cfun_frame_layout.backchain_offset - cfun_gprs_save_area_size;
7841 /* FPRs will not be saved. Nevertheless pick sane values to
7842 keep area calculations valid. */
7843 cfun_frame_layout.f0_offset =
7844 cfun_frame_layout.f4_offset =
7845 cfun_frame_layout.f8_offset = cfun_frame_layout.gprs_offset;
7847 else
7849 int num_fprs;
7851 /* Packed stack layout without backchain. */
7853 /* With stdarg FPRs need their dedicated slots. */
7854 num_fprs = (TARGET_64BIT && cfun->stdarg ? 2
7855 : (cfun_fpr_save_p (FPR4_REGNUM) +
7856 cfun_fpr_save_p (FPR6_REGNUM)));
7857 cfun_frame_layout.f4_offset = STACK_POINTER_OFFSET - 8 * num_fprs;
7859 num_fprs = (cfun->stdarg ? 2
7860 : (cfun_fpr_save_p (FPR0_REGNUM)
7861 + cfun_fpr_save_p (FPR2_REGNUM)));
7862 cfun_frame_layout.f0_offset = cfun_frame_layout.f4_offset - 8 * num_fprs;
7864 cfun_frame_layout.gprs_offset
7865 = cfun_frame_layout.f0_offset - cfun_gprs_save_area_size;
7867 cfun_frame_layout.f8_offset = (cfun_frame_layout.gprs_offset
7868 - cfun_frame_layout.high_fprs * 8);
7871 if (cfun_save_high_fprs_p)
7872 cfun_frame_layout.frame_size += cfun_frame_layout.high_fprs * 8;
7874 if (!crtl->is_leaf)
7875 cfun_frame_layout.frame_size += crtl->outgoing_args_size;
7877 /* In the following cases we have to allocate a STACK_POINTER_OFFSET
7878 sized area at the bottom of the stack. This is required also for
7879 leaf functions. When GCC generates a local stack reference it
7880 will always add STACK_POINTER_OFFSET to all these references. */
7881 if (crtl->is_leaf
7882 && !TARGET_TPF_PROFILING
7883 && cfun_frame_layout.frame_size == 0
7884 && !cfun->calls_alloca)
7885 return;
7887 /* Calculate the number of bytes we have used in our own register
7888 save area. With the packed stack layout we can re-use the
7889 remaining bytes for normal stack elements. */
7891 if (TARGET_PACKED_STACK)
7892 lowest_offset = MIN (MIN (cfun_frame_layout.f0_offset,
7893 cfun_frame_layout.f4_offset),
7894 cfun_frame_layout.gprs_offset);
7895 else
7896 lowest_offset = 0;
7898 if (TARGET_BACKCHAIN)
7899 lowest_offset = MIN (lowest_offset, cfun_frame_layout.backchain_offset);
7901 cfun_frame_layout.frame_size += STACK_POINTER_OFFSET - lowest_offset;
7903 /* If under 31 bit an odd number of gprs has to be saved we have to
7904 adjust the frame size to sustain 8 byte alignment of stack
7905 frames. */
7906 cfun_frame_layout.frame_size = ((cfun_frame_layout.frame_size +
7907 STACK_BOUNDARY / BITS_PER_UNIT - 1)
7908 & ~(STACK_BOUNDARY / BITS_PER_UNIT - 1));
7911 /* Generate frame layout. Fills in register and frame data for the current
7912 function in cfun->machine. This routine can be called multiple times;
7913 it will re-do the complete frame layout every time. */
7915 static void
7916 s390_init_frame_layout (void)
7918 HOST_WIDE_INT frame_size;
7919 int base_used;
7921 gcc_assert (!reload_completed);
7923 /* On S/390 machines, we may need to perform branch splitting, which
7924 will require both base and return address register. We have no
7925 choice but to assume we're going to need them until right at the
7926 end of the machine dependent reorg phase. */
7927 if (!TARGET_CPU_ZARCH)
7928 cfun->machine->split_branches_pending_p = true;
7932 frame_size = cfun_frame_layout.frame_size;
7934 /* Try to predict whether we'll need the base register. */
7935 base_used = cfun->machine->split_branches_pending_p
7936 || crtl->uses_const_pool
7937 || (!DISP_IN_RANGE (frame_size)
7938 && !CONST_OK_FOR_K (frame_size));
7940 /* Decide which register to use as literal pool base. In small
7941 leaf functions, try to use an unused call-clobbered register
7942 as base register to avoid save/restore overhead. */
7943 if (!base_used)
7944 cfun->machine->base_reg = NULL_RTX;
7945 else if (crtl->is_leaf && !df_regs_ever_live_p (5))
7946 cfun->machine->base_reg = gen_rtx_REG (Pmode, 5);
7947 else
7948 cfun->machine->base_reg = gen_rtx_REG (Pmode, BASE_REGNUM);
7950 s390_register_info ();
7951 s390_frame_info ();
7953 while (frame_size != cfun_frame_layout.frame_size);
7956 /* Remove the FPR clobbers from a tbegin insn if it can be proven that
7957 the TX is nonescaping. A transaction is considered escaping if
7958 there is at least one path from tbegin returning CC0 to the
7959 function exit block without an tend.
7961 The check so far has some limitations:
7962 - only single tbegin/tend BBs are supported
7963 - the first cond jump after tbegin must separate the CC0 path from ~CC0
7964 - when CC is copied to a GPR and the CC0 check is done with the GPR
7965 this is not supported
7968 static void
7969 s390_optimize_nonescaping_tx (void)
7971 const unsigned int CC0 = 1 << 3;
7972 basic_block tbegin_bb = NULL;
7973 basic_block tend_bb = NULL;
7974 basic_block bb;
7975 rtx_insn *insn;
7976 bool result = true;
7977 int bb_index;
7978 rtx_insn *tbegin_insn = NULL;
7980 if (!cfun->machine->tbegin_p)
7981 return;
7983 for (bb_index = 0; bb_index < n_basic_blocks_for_fn (cfun); bb_index++)
7985 bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
7987 if (!bb)
7988 continue;
7990 FOR_BB_INSNS (bb, insn)
7992 rtx ite, cc, pat, target;
7993 unsigned HOST_WIDE_INT mask;
7995 if (!INSN_P (insn) || INSN_CODE (insn) <= 0)
7996 continue;
7998 pat = PATTERN (insn);
8000 if (GET_CODE (pat) == PARALLEL)
8001 pat = XVECEXP (pat, 0, 0);
8003 if (GET_CODE (pat) != SET
8004 || GET_CODE (SET_SRC (pat)) != UNSPEC_VOLATILE)
8005 continue;
8007 if (XINT (SET_SRC (pat), 1) == UNSPECV_TBEGIN)
8009 rtx_insn *tmp;
8011 tbegin_insn = insn;
8013 /* Just return if the tbegin doesn't have clobbers. */
8014 if (GET_CODE (PATTERN (insn)) != PARALLEL)
8015 return;
8017 if (tbegin_bb != NULL)
8018 return;
8020 /* Find the next conditional jump. */
8021 for (tmp = NEXT_INSN (insn);
8022 tmp != NULL_RTX;
8023 tmp = NEXT_INSN (tmp))
8025 if (reg_set_p (gen_rtx_REG (CCmode, CC_REGNUM), tmp))
8026 return;
8027 if (!JUMP_P (tmp))
8028 continue;
8030 ite = SET_SRC (PATTERN (tmp));
8031 if (GET_CODE (ite) != IF_THEN_ELSE)
8032 continue;
8034 cc = XEXP (XEXP (ite, 0), 0);
8035 if (!REG_P (cc) || !CC_REGNO_P (REGNO (cc))
8036 || GET_MODE (cc) != CCRAWmode
8037 || GET_CODE (XEXP (XEXP (ite, 0), 1)) != CONST_INT)
8038 return;
8040 if (bb->succs->length () != 2)
8041 return;
8043 mask = INTVAL (XEXP (XEXP (ite, 0), 1));
8044 if (GET_CODE (XEXP (ite, 0)) == NE)
8045 mask ^= 0xf;
8047 if (mask == CC0)
8048 target = XEXP (ite, 1);
8049 else if (mask == (CC0 ^ 0xf))
8050 target = XEXP (ite, 2);
8051 else
8052 return;
8055 edge_iterator ei;
8056 edge e1, e2;
8058 ei = ei_start (bb->succs);
8059 e1 = ei_safe_edge (ei);
8060 ei_next (&ei);
8061 e2 = ei_safe_edge (ei);
8063 if (e2->flags & EDGE_FALLTHRU)
8065 e2 = e1;
8066 e1 = ei_safe_edge (ei);
8069 if (!(e1->flags & EDGE_FALLTHRU))
8070 return;
8072 tbegin_bb = (target == pc_rtx) ? e1->dest : e2->dest;
8074 if (tmp == BB_END (bb))
8075 break;
8079 if (XINT (SET_SRC (pat), 1) == UNSPECV_TEND)
8081 if (tend_bb != NULL)
8082 return;
8083 tend_bb = bb;
8088 /* Either we successfully remove the FPR clobbers here or we are not
8089 able to do anything for this TX. Both cases don't qualify for
8090 another look. */
8091 cfun->machine->tbegin_p = false;
8093 if (tbegin_bb == NULL || tend_bb == NULL)
8094 return;
8096 calculate_dominance_info (CDI_POST_DOMINATORS);
8097 result = dominated_by_p (CDI_POST_DOMINATORS, tbegin_bb, tend_bb);
8098 free_dominance_info (CDI_POST_DOMINATORS);
8100 if (!result)
8101 return;
8103 PATTERN (tbegin_insn) = gen_rtx_PARALLEL (VOIDmode,
8104 gen_rtvec (2,
8105 XVECEXP (PATTERN (tbegin_insn), 0, 0),
8106 XVECEXP (PATTERN (tbegin_insn), 0, 1)));
8107 INSN_CODE (tbegin_insn) = -1;
8108 df_insn_rescan (tbegin_insn);
8110 return;
8113 /* Return true if it is legal to put a value with MODE into REGNO. */
8115 bool
8116 s390_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
8118 switch (REGNO_REG_CLASS (regno))
8120 case FP_REGS:
8121 if (REGNO_PAIR_OK (regno, mode))
8123 if (mode == SImode || mode == DImode)
8124 return true;
8126 if (FLOAT_MODE_P (mode) && GET_MODE_CLASS (mode) != MODE_VECTOR_FLOAT)
8127 return true;
8129 break;
8130 case ADDR_REGS:
8131 if (FRAME_REGNO_P (regno) && mode == Pmode)
8132 return true;
8134 /* fallthrough */
8135 case GENERAL_REGS:
8136 if (REGNO_PAIR_OK (regno, mode))
8138 if (TARGET_ZARCH
8139 || (mode != TFmode && mode != TCmode && mode != TDmode))
8140 return true;
8142 break;
8143 case CC_REGS:
8144 if (GET_MODE_CLASS (mode) == MODE_CC)
8145 return true;
8146 break;
8147 case ACCESS_REGS:
8148 if (REGNO_PAIR_OK (regno, mode))
8150 if (mode == SImode || mode == Pmode)
8151 return true;
8153 break;
8154 default:
8155 return false;
8158 return false;
8161 /* Return nonzero if register OLD_REG can be renamed to register NEW_REG. */
8163 bool
8164 s390_hard_regno_rename_ok (unsigned int old_reg, unsigned int new_reg)
8166 /* Once we've decided upon a register to use as base register, it must
8167 no longer be used for any other purpose. */
8168 if (cfun->machine->base_reg)
8169 if (REGNO (cfun->machine->base_reg) == old_reg
8170 || REGNO (cfun->machine->base_reg) == new_reg)
8171 return false;
8173 /* Prevent regrename from using call-saved regs which haven't
8174 actually been saved. This is necessary since regrename assumes
8175 the backend save/restore decisions are based on
8176 df_regs_ever_live. Since we have our own routine we have to tell
8177 regrename manually about it. */
8178 if (GENERAL_REGNO_P (new_reg)
8179 && !call_really_used_regs[new_reg]
8180 && cfun_gpr_save_slot (new_reg) == 0)
8181 return false;
8183 return true;
8186 /* Return nonzero if register REGNO can be used as a scratch register
8187 in peephole2. */
8189 static bool
8190 s390_hard_regno_scratch_ok (unsigned int regno)
8192 /* See s390_hard_regno_rename_ok. */
8193 if (GENERAL_REGNO_P (regno)
8194 && !call_really_used_regs[regno]
8195 && cfun_gpr_save_slot (regno) == 0)
8196 return false;
8198 return true;
8201 /* Maximum number of registers to represent a value of mode MODE
8202 in a register of class RCLASS. */
8205 s390_class_max_nregs (enum reg_class rclass, machine_mode mode)
8207 switch (rclass)
8209 case FP_REGS:
8210 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
8211 return 2 * ((GET_MODE_SIZE (mode) / 2 + 8 - 1) / 8);
8212 else
8213 return (GET_MODE_SIZE (mode) + 8 - 1) / 8;
8214 case ACCESS_REGS:
8215 return (GET_MODE_SIZE (mode) + 4 - 1) / 4;
8216 default:
8217 break;
8219 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
8222 /* Return true if we use LRA instead of reload pass. */
8223 static bool
8224 s390_lra_p (void)
8226 return s390_lra_flag;
8229 /* Return true if register FROM can be eliminated via register TO. */
8231 static bool
8232 s390_can_eliminate (const int from, const int to)
8234 /* On zSeries machines, we have not marked the base register as fixed.
8235 Instead, we have an elimination rule BASE_REGNUM -> BASE_REGNUM.
8236 If a function requires the base register, we say here that this
8237 elimination cannot be performed. This will cause reload to free
8238 up the base register (as if it were fixed). On the other hand,
8239 if the current function does *not* require the base register, we
8240 say here the elimination succeeds, which in turn allows reload
8241 to allocate the base register for any other purpose. */
8242 if (from == BASE_REGNUM && to == BASE_REGNUM)
8244 if (TARGET_CPU_ZARCH)
8246 s390_init_frame_layout ();
8247 return cfun->machine->base_reg == NULL_RTX;
8250 return false;
8253 /* Everything else must point into the stack frame. */
8254 gcc_assert (to == STACK_POINTER_REGNUM
8255 || to == HARD_FRAME_POINTER_REGNUM);
8257 gcc_assert (from == FRAME_POINTER_REGNUM
8258 || from == ARG_POINTER_REGNUM
8259 || from == RETURN_ADDRESS_POINTER_REGNUM);
8261 /* Make sure we actually saved the return address. */
8262 if (from == RETURN_ADDRESS_POINTER_REGNUM)
8263 if (!crtl->calls_eh_return
8264 && !cfun->stdarg
8265 && !cfun_frame_layout.save_return_addr_p)
8266 return false;
8268 return true;
8271 /* Return offset between register FROM and TO initially after prolog. */
8273 HOST_WIDE_INT
8274 s390_initial_elimination_offset (int from, int to)
8276 HOST_WIDE_INT offset;
8278 /* ??? Why are we called for non-eliminable pairs? */
8279 if (!s390_can_eliminate (from, to))
8280 return 0;
8282 switch (from)
8284 case FRAME_POINTER_REGNUM:
8285 offset = (get_frame_size()
8286 + STACK_POINTER_OFFSET
8287 + crtl->outgoing_args_size);
8288 break;
8290 case ARG_POINTER_REGNUM:
8291 s390_init_frame_layout ();
8292 offset = cfun_frame_layout.frame_size + STACK_POINTER_OFFSET;
8293 break;
8295 case RETURN_ADDRESS_POINTER_REGNUM:
8296 s390_init_frame_layout ();
8298 if (cfun_frame_layout.first_save_gpr_slot == -1)
8300 /* If it turns out that for stdarg nothing went into the reg
8301 save area we also do not need the return address
8302 pointer. */
8303 if (cfun->stdarg && !cfun_save_arg_fprs_p)
8304 return 0;
8306 gcc_unreachable ();
8309 /* In order to make the following work it is not necessary for
8310 r14 to have a save slot. It is sufficient if one other GPR
8311 got one. Since the GPRs are always stored without gaps we
8312 are able to calculate where the r14 save slot would
8313 reside. */
8314 offset = (cfun_frame_layout.frame_size + cfun_frame_layout.gprs_offset +
8315 (RETURN_REGNUM - cfun_frame_layout.first_save_gpr_slot) *
8316 UNITS_PER_LONG);
8317 break;
8319 case BASE_REGNUM:
8320 offset = 0;
8321 break;
8323 default:
8324 gcc_unreachable ();
8327 return offset;
8330 /* Emit insn to save fpr REGNUM at offset OFFSET relative
8331 to register BASE. Return generated insn. */
8333 static rtx
8334 save_fpr (rtx base, int offset, int regnum)
8336 rtx addr;
8337 addr = gen_rtx_MEM (DFmode, plus_constant (Pmode, base, offset));
8339 if (regnum >= 16 && regnum <= (16 + FP_ARG_NUM_REG))
8340 set_mem_alias_set (addr, get_varargs_alias_set ());
8341 else
8342 set_mem_alias_set (addr, get_frame_alias_set ());
8344 return emit_move_insn (addr, gen_rtx_REG (DFmode, regnum));
8347 /* Emit insn to restore fpr REGNUM from offset OFFSET relative
8348 to register BASE. Return generated insn. */
8350 static rtx
8351 restore_fpr (rtx base, int offset, int regnum)
8353 rtx addr;
8354 addr = gen_rtx_MEM (DFmode, plus_constant (Pmode, base, offset));
8355 set_mem_alias_set (addr, get_frame_alias_set ());
8357 return emit_move_insn (gen_rtx_REG (DFmode, regnum), addr);
8360 /* Return true if REGNO is a global register, but not one
8361 of the special ones that need to be saved/restored in anyway. */
8363 static inline bool
8364 global_not_special_regno_p (int regno)
8366 return (global_regs[regno]
8367 /* These registers are special and need to be
8368 restored in any case. */
8369 && !(regno == STACK_POINTER_REGNUM
8370 || regno == RETURN_REGNUM
8371 || regno == BASE_REGNUM
8372 || (flag_pic && regno == (int)PIC_OFFSET_TABLE_REGNUM)));
8375 /* Generate insn to save registers FIRST to LAST into
8376 the register save area located at offset OFFSET
8377 relative to register BASE. */
8379 static rtx
8380 save_gprs (rtx base, int offset, int first, int last)
8382 rtx addr, insn, note;
8383 int i;
8385 addr = plus_constant (Pmode, base, offset);
8386 addr = gen_rtx_MEM (Pmode, addr);
8388 set_mem_alias_set (addr, get_frame_alias_set ());
8390 /* Special-case single register. */
8391 if (first == last)
8393 if (TARGET_64BIT)
8394 insn = gen_movdi (addr, gen_rtx_REG (Pmode, first));
8395 else
8396 insn = gen_movsi (addr, gen_rtx_REG (Pmode, first));
8398 if (!global_not_special_regno_p (first))
8399 RTX_FRAME_RELATED_P (insn) = 1;
8400 return insn;
8404 insn = gen_store_multiple (addr,
8405 gen_rtx_REG (Pmode, first),
8406 GEN_INT (last - first + 1));
8408 if (first <= 6 && cfun->stdarg)
8409 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
8411 rtx mem = XEXP (XVECEXP (PATTERN (insn), 0, i), 0);
8413 if (first + i <= 6)
8414 set_mem_alias_set (mem, get_varargs_alias_set ());
8417 /* We need to set the FRAME_RELATED flag on all SETs
8418 inside the store-multiple pattern.
8420 However, we must not emit DWARF records for registers 2..5
8421 if they are stored for use by variable arguments ...
8423 ??? Unfortunately, it is not enough to simply not the
8424 FRAME_RELATED flags for those SETs, because the first SET
8425 of the PARALLEL is always treated as if it had the flag
8426 set, even if it does not. Therefore we emit a new pattern
8427 without those registers as REG_FRAME_RELATED_EXPR note. */
8429 if (first >= 6 && !global_not_special_regno_p (first))
8431 rtx pat = PATTERN (insn);
8433 for (i = 0; i < XVECLEN (pat, 0); i++)
8434 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
8435 && !global_not_special_regno_p (REGNO (SET_SRC (XVECEXP (pat,
8436 0, i)))))
8437 RTX_FRAME_RELATED_P (XVECEXP (pat, 0, i)) = 1;
8439 RTX_FRAME_RELATED_P (insn) = 1;
8441 else if (last >= 6)
8443 int start;
8445 for (start = first >= 6 ? first : 6; start <= last; start++)
8446 if (!global_not_special_regno_p (start))
8447 break;
8449 if (start > last)
8450 return insn;
8452 addr = plus_constant (Pmode, base,
8453 offset + (start - first) * UNITS_PER_LONG);
8455 if (start == last)
8457 if (TARGET_64BIT)
8458 note = gen_movdi (gen_rtx_MEM (Pmode, addr),
8459 gen_rtx_REG (Pmode, start));
8460 else
8461 note = gen_movsi (gen_rtx_MEM (Pmode, addr),
8462 gen_rtx_REG (Pmode, start));
8463 note = PATTERN (note);
8465 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note);
8466 RTX_FRAME_RELATED_P (insn) = 1;
8468 return insn;
8471 note = gen_store_multiple (gen_rtx_MEM (Pmode, addr),
8472 gen_rtx_REG (Pmode, start),
8473 GEN_INT (last - start + 1));
8474 note = PATTERN (note);
8476 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note);
8478 for (i = 0; i < XVECLEN (note, 0); i++)
8479 if (GET_CODE (XVECEXP (note, 0, i)) == SET
8480 && !global_not_special_regno_p (REGNO (SET_SRC (XVECEXP (note,
8481 0, i)))))
8482 RTX_FRAME_RELATED_P (XVECEXP (note, 0, i)) = 1;
8484 RTX_FRAME_RELATED_P (insn) = 1;
8487 return insn;
8490 /* Generate insn to restore registers FIRST to LAST from
8491 the register save area located at offset OFFSET
8492 relative to register BASE. */
8494 static rtx
8495 restore_gprs (rtx base, int offset, int first, int last)
8497 rtx addr, insn;
8499 addr = plus_constant (Pmode, base, offset);
8500 addr = gen_rtx_MEM (Pmode, addr);
8501 set_mem_alias_set (addr, get_frame_alias_set ());
8503 /* Special-case single register. */
8504 if (first == last)
8506 if (TARGET_64BIT)
8507 insn = gen_movdi (gen_rtx_REG (Pmode, first), addr);
8508 else
8509 insn = gen_movsi (gen_rtx_REG (Pmode, first), addr);
8511 RTX_FRAME_RELATED_P (insn) = 1;
8512 return insn;
8515 insn = gen_load_multiple (gen_rtx_REG (Pmode, first),
8516 addr,
8517 GEN_INT (last - first + 1));
8518 RTX_FRAME_RELATED_P (insn) = 1;
8519 return insn;
8522 /* Return insn sequence to load the GOT register. */
8524 static GTY(()) rtx got_symbol;
8525 rtx_insn *
8526 s390_load_got (void)
8528 rtx_insn *insns;
8530 /* We cannot use pic_offset_table_rtx here since we use this
8531 function also for non-pic if __tls_get_offset is called and in
8532 that case PIC_OFFSET_TABLE_REGNUM as well as pic_offset_table_rtx
8533 aren't usable. */
8534 rtx got_rtx = gen_rtx_REG (Pmode, 12);
8536 if (!got_symbol)
8538 got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
8539 SYMBOL_REF_FLAGS (got_symbol) = SYMBOL_FLAG_LOCAL;
8542 start_sequence ();
8544 if (TARGET_CPU_ZARCH)
8546 emit_move_insn (got_rtx, got_symbol);
8548 else
8550 rtx offset;
8552 offset = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, got_symbol),
8553 UNSPEC_LTREL_OFFSET);
8554 offset = gen_rtx_CONST (Pmode, offset);
8555 offset = force_const_mem (Pmode, offset);
8557 emit_move_insn (got_rtx, offset);
8559 offset = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (offset, 0)),
8560 UNSPEC_LTREL_BASE);
8561 offset = gen_rtx_PLUS (Pmode, got_rtx, offset);
8563 emit_move_insn (got_rtx, offset);
8566 insns = get_insns ();
8567 end_sequence ();
8568 return insns;
8571 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
8572 and the change to the stack pointer. */
8574 static void
8575 s390_emit_stack_tie (void)
8577 rtx mem = gen_frame_mem (BLKmode,
8578 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM));
8580 emit_insn (gen_stack_tie (mem));
8583 /* Copy GPRS into FPR save slots. */
8585 static void
8586 s390_save_gprs_to_fprs (void)
8588 int i;
8590 if (!TARGET_Z10 || !TARGET_HARD_FLOAT || !crtl->is_leaf)
8591 return;
8593 for (i = 6; i < 16; i++)
8595 if (FP_REGNO_P (cfun_gpr_save_slot (i)))
8597 rtx_insn *insn =
8598 emit_move_insn (gen_rtx_REG (DImode, cfun_gpr_save_slot (i)),
8599 gen_rtx_REG (DImode, i));
8600 RTX_FRAME_RELATED_P (insn) = 1;
8605 /* Restore GPRs from FPR save slots. */
8607 static void
8608 s390_restore_gprs_from_fprs (void)
8610 int i;
8612 if (!TARGET_Z10 || !TARGET_HARD_FLOAT || !crtl->is_leaf)
8613 return;
8615 for (i = 6; i < 16; i++)
8617 if (FP_REGNO_P (cfun_gpr_save_slot (i)))
8619 rtx_insn *insn =
8620 emit_move_insn (gen_rtx_REG (DImode, i),
8621 gen_rtx_REG (DImode, cfun_gpr_save_slot (i)));
8622 df_set_regs_ever_live (i, true);
8623 add_reg_note (insn, REG_CFA_RESTORE, gen_rtx_REG (DImode, i));
8624 if (i == STACK_POINTER_REGNUM)
8625 add_reg_note (insn, REG_CFA_DEF_CFA,
8626 plus_constant (Pmode, stack_pointer_rtx,
8627 STACK_POINTER_OFFSET));
8628 RTX_FRAME_RELATED_P (insn) = 1;
8634 /* A pass run immediately before shrink-wrapping and prologue and epilogue
8635 generation. */
8637 namespace {
8639 const pass_data pass_data_s390_early_mach =
8641 RTL_PASS, /* type */
8642 "early_mach", /* name */
8643 OPTGROUP_NONE, /* optinfo_flags */
8644 TV_MACH_DEP, /* tv_id */
8645 0, /* properties_required */
8646 0, /* properties_provided */
8647 0, /* properties_destroyed */
8648 0, /* todo_flags_start */
8649 ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
8652 class pass_s390_early_mach : public rtl_opt_pass
8654 public:
8655 pass_s390_early_mach (gcc::context *ctxt)
8656 : rtl_opt_pass (pass_data_s390_early_mach, ctxt)
8659 /* opt_pass methods: */
8660 virtual unsigned int execute (function *);
8662 }; // class pass_s390_early_mach
8664 unsigned int
8665 pass_s390_early_mach::execute (function *fun)
8667 rtx_insn *insn;
8669 /* Try to get rid of the FPR clobbers. */
8670 s390_optimize_nonescaping_tx ();
8672 /* Re-compute register info. */
8673 s390_register_info ();
8675 /* If we're using a base register, ensure that it is always valid for
8676 the first non-prologue instruction. */
8677 if (fun->machine->base_reg)
8678 emit_insn_at_entry (gen_main_pool (fun->machine->base_reg));
8680 /* Annotate all constant pool references to let the scheduler know
8681 they implicitly use the base register. */
8682 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
8683 if (INSN_P (insn))
8685 annotate_constant_pool_refs (&PATTERN (insn));
8686 df_insn_rescan (insn);
8688 return 0;
8691 } // anon namespace
8693 /* Expand the prologue into a bunch of separate insns. */
8695 void
8696 s390_emit_prologue (void)
8698 rtx insn, addr;
8699 rtx temp_reg;
8700 int i;
8701 int offset;
8702 int next_fpr = 0;
8704 /* Choose best register to use for temp use within prologue.
8705 See below for why TPF must use the register 1. */
8707 if (!has_hard_reg_initial_val (Pmode, RETURN_REGNUM)
8708 && !crtl->is_leaf
8709 && !TARGET_TPF_PROFILING)
8710 temp_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
8711 else
8712 temp_reg = gen_rtx_REG (Pmode, 1);
8714 s390_save_gprs_to_fprs ();
8716 /* Save call saved gprs. */
8717 if (cfun_frame_layout.first_save_gpr != -1)
8719 insn = save_gprs (stack_pointer_rtx,
8720 cfun_frame_layout.gprs_offset +
8721 UNITS_PER_LONG * (cfun_frame_layout.first_save_gpr
8722 - cfun_frame_layout.first_save_gpr_slot),
8723 cfun_frame_layout.first_save_gpr,
8724 cfun_frame_layout.last_save_gpr);
8725 emit_insn (insn);
8728 /* Dummy insn to mark literal pool slot. */
8730 if (cfun->machine->base_reg)
8731 emit_insn (gen_main_pool (cfun->machine->base_reg));
8733 offset = cfun_frame_layout.f0_offset;
8735 /* Save f0 and f2. */
8736 for (i = FPR0_REGNUM; i <= FPR0_REGNUM + 1; i++)
8738 if (cfun_fpr_save_p (i))
8740 save_fpr (stack_pointer_rtx, offset, i);
8741 offset += 8;
8743 else if (!TARGET_PACKED_STACK || cfun->stdarg)
8744 offset += 8;
8747 /* Save f4 and f6. */
8748 offset = cfun_frame_layout.f4_offset;
8749 for (i = FPR4_REGNUM; i <= FPR4_REGNUM + 1; i++)
8751 if (cfun_fpr_save_p (i))
8753 insn = save_fpr (stack_pointer_rtx, offset, i);
8754 offset += 8;
8756 /* If f4 and f6 are call clobbered they are saved due to
8757 stdargs and therefore are not frame related. */
8758 if (!call_really_used_regs[i])
8759 RTX_FRAME_RELATED_P (insn) = 1;
8761 else if (!TARGET_PACKED_STACK || call_really_used_regs[i])
8762 offset += 8;
8765 if (TARGET_PACKED_STACK
8766 && cfun_save_high_fprs_p
8767 && cfun_frame_layout.f8_offset + cfun_frame_layout.high_fprs * 8 > 0)
8769 offset = (cfun_frame_layout.f8_offset
8770 + (cfun_frame_layout.high_fprs - 1) * 8);
8772 for (i = FPR15_REGNUM; i >= FPR8_REGNUM && offset >= 0; i--)
8773 if (cfun_fpr_save_p (i))
8775 insn = save_fpr (stack_pointer_rtx, offset, i);
8777 RTX_FRAME_RELATED_P (insn) = 1;
8778 offset -= 8;
8780 if (offset >= cfun_frame_layout.f8_offset)
8781 next_fpr = i;
8784 if (!TARGET_PACKED_STACK)
8785 next_fpr = cfun_save_high_fprs_p ? FPR15_REGNUM : 0;
8787 if (flag_stack_usage_info)
8788 current_function_static_stack_size = cfun_frame_layout.frame_size;
8790 /* Decrement stack pointer. */
8792 if (cfun_frame_layout.frame_size > 0)
8794 rtx frame_off = GEN_INT (-cfun_frame_layout.frame_size);
8795 rtx real_frame_off;
8797 if (s390_stack_size)
8799 HOST_WIDE_INT stack_guard;
8801 if (s390_stack_guard)
8802 stack_guard = s390_stack_guard;
8803 else
8805 /* If no value for stack guard is provided the smallest power of 2
8806 larger than the current frame size is chosen. */
8807 stack_guard = 1;
8808 while (stack_guard < cfun_frame_layout.frame_size)
8809 stack_guard <<= 1;
8812 if (cfun_frame_layout.frame_size >= s390_stack_size)
8814 warning (0, "frame size of function %qs is %wd"
8815 " bytes exceeding user provided stack limit of "
8816 "%d bytes. "
8817 "An unconditional trap is added.",
8818 current_function_name(), cfun_frame_layout.frame_size,
8819 s390_stack_size);
8820 emit_insn (gen_trap ());
8822 else
8824 /* stack_guard has to be smaller than s390_stack_size.
8825 Otherwise we would emit an AND with zero which would
8826 not match the test under mask pattern. */
8827 if (stack_guard >= s390_stack_size)
8829 warning (0, "frame size of function %qs is %wd"
8830 " bytes which is more than half the stack size. "
8831 "The dynamic check would not be reliable. "
8832 "No check emitted for this function.",
8833 current_function_name(),
8834 cfun_frame_layout.frame_size);
8836 else
8838 HOST_WIDE_INT stack_check_mask = ((s390_stack_size - 1)
8839 & ~(stack_guard - 1));
8841 rtx t = gen_rtx_AND (Pmode, stack_pointer_rtx,
8842 GEN_INT (stack_check_mask));
8843 if (TARGET_64BIT)
8844 emit_insn (gen_ctrapdi4 (gen_rtx_EQ (VOIDmode,
8845 t, const0_rtx),
8846 t, const0_rtx, const0_rtx));
8847 else
8848 emit_insn (gen_ctrapsi4 (gen_rtx_EQ (VOIDmode,
8849 t, const0_rtx),
8850 t, const0_rtx, const0_rtx));
8855 if (s390_warn_framesize > 0
8856 && cfun_frame_layout.frame_size >= s390_warn_framesize)
8857 warning (0, "frame size of %qs is %wd bytes",
8858 current_function_name (), cfun_frame_layout.frame_size);
8860 if (s390_warn_dynamicstack_p && cfun->calls_alloca)
8861 warning (0, "%qs uses dynamic stack allocation", current_function_name ());
8863 /* Save incoming stack pointer into temp reg. */
8864 if (TARGET_BACKCHAIN || next_fpr)
8865 insn = emit_insn (gen_move_insn (temp_reg, stack_pointer_rtx));
8867 /* Subtract frame size from stack pointer. */
8869 if (DISP_IN_RANGE (INTVAL (frame_off)))
8871 insn = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8872 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
8873 frame_off));
8874 insn = emit_insn (insn);
8876 else
8878 if (!CONST_OK_FOR_K (INTVAL (frame_off)))
8879 frame_off = force_const_mem (Pmode, frame_off);
8881 insn = emit_insn (gen_add2_insn (stack_pointer_rtx, frame_off));
8882 annotate_constant_pool_refs (&PATTERN (insn));
8885 RTX_FRAME_RELATED_P (insn) = 1;
8886 real_frame_off = GEN_INT (-cfun_frame_layout.frame_size);
8887 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
8888 gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8889 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
8890 real_frame_off)));
8892 /* Set backchain. */
8894 if (TARGET_BACKCHAIN)
8896 if (cfun_frame_layout.backchain_offset)
8897 addr = gen_rtx_MEM (Pmode,
8898 plus_constant (Pmode, stack_pointer_rtx,
8899 cfun_frame_layout.backchain_offset));
8900 else
8901 addr = gen_rtx_MEM (Pmode, stack_pointer_rtx);
8902 set_mem_alias_set (addr, get_frame_alias_set ());
8903 insn = emit_insn (gen_move_insn (addr, temp_reg));
8906 /* If we support non-call exceptions (e.g. for Java),
8907 we need to make sure the backchain pointer is set up
8908 before any possibly trapping memory access. */
8909 if (TARGET_BACKCHAIN && cfun->can_throw_non_call_exceptions)
8911 addr = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
8912 emit_clobber (addr);
8916 /* Save fprs 8 - 15 (64 bit ABI). */
8918 if (cfun_save_high_fprs_p && next_fpr)
8920 /* If the stack might be accessed through a different register
8921 we have to make sure that the stack pointer decrement is not
8922 moved below the use of the stack slots. */
8923 s390_emit_stack_tie ();
8925 insn = emit_insn (gen_add2_insn (temp_reg,
8926 GEN_INT (cfun_frame_layout.f8_offset)));
8928 offset = 0;
8930 for (i = FPR8_REGNUM; i <= next_fpr; i++)
8931 if (cfun_fpr_save_p (i))
8933 rtx addr = plus_constant (Pmode, stack_pointer_rtx,
8934 cfun_frame_layout.frame_size
8935 + cfun_frame_layout.f8_offset
8936 + offset);
8938 insn = save_fpr (temp_reg, offset, i);
8939 offset += 8;
8940 RTX_FRAME_RELATED_P (insn) = 1;
8941 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
8942 gen_rtx_SET (VOIDmode,
8943 gen_rtx_MEM (DFmode, addr),
8944 gen_rtx_REG (DFmode, i)));
8948 /* Set frame pointer, if needed. */
8950 if (frame_pointer_needed)
8952 insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
8953 RTX_FRAME_RELATED_P (insn) = 1;
8956 /* Set up got pointer, if needed. */
8958 if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
8960 rtx_insn *insns = s390_load_got ();
8962 for (rtx_insn *insn = insns; insn; insn = NEXT_INSN (insn))
8963 annotate_constant_pool_refs (&PATTERN (insn));
8965 emit_insn (insns);
8968 if (TARGET_TPF_PROFILING)
8970 /* Generate a BAS instruction to serve as a function
8971 entry intercept to facilitate the use of tracing
8972 algorithms located at the branch target. */
8973 emit_insn (gen_prologue_tpf ());
8975 /* Emit a blockage here so that all code
8976 lies between the profiling mechanisms. */
8977 emit_insn (gen_blockage ());
8981 /* Expand the epilogue into a bunch of separate insns. */
8983 void
8984 s390_emit_epilogue (bool sibcall)
8986 rtx frame_pointer, return_reg, cfa_restores = NULL_RTX;
8987 int area_bottom, area_top, offset = 0;
8988 int next_offset;
8989 rtvec p;
8990 int i;
8992 if (TARGET_TPF_PROFILING)
8995 /* Generate a BAS instruction to serve as a function
8996 entry intercept to facilitate the use of tracing
8997 algorithms located at the branch target. */
8999 /* Emit a blockage here so that all code
9000 lies between the profiling mechanisms. */
9001 emit_insn (gen_blockage ());
9003 emit_insn (gen_epilogue_tpf ());
9006 /* Check whether to use frame or stack pointer for restore. */
9008 frame_pointer = (frame_pointer_needed
9009 ? hard_frame_pointer_rtx : stack_pointer_rtx);
9011 s390_frame_area (&area_bottom, &area_top);
9013 /* Check whether we can access the register save area.
9014 If not, increment the frame pointer as required. */
9016 if (area_top <= area_bottom)
9018 /* Nothing to restore. */
9020 else if (DISP_IN_RANGE (cfun_frame_layout.frame_size + area_bottom)
9021 && DISP_IN_RANGE (cfun_frame_layout.frame_size + area_top - 1))
9023 /* Area is in range. */
9024 offset = cfun_frame_layout.frame_size;
9026 else
9028 rtx insn, frame_off, cfa;
9030 offset = area_bottom < 0 ? -area_bottom : 0;
9031 frame_off = GEN_INT (cfun_frame_layout.frame_size - offset);
9033 cfa = gen_rtx_SET (VOIDmode, frame_pointer,
9034 gen_rtx_PLUS (Pmode, frame_pointer, frame_off));
9035 if (DISP_IN_RANGE (INTVAL (frame_off)))
9037 insn = gen_rtx_SET (VOIDmode, frame_pointer,
9038 gen_rtx_PLUS (Pmode, frame_pointer, frame_off));
9039 insn = emit_insn (insn);
9041 else
9043 if (!CONST_OK_FOR_K (INTVAL (frame_off)))
9044 frame_off = force_const_mem (Pmode, frame_off);
9046 insn = emit_insn (gen_add2_insn (frame_pointer, frame_off));
9047 annotate_constant_pool_refs (&PATTERN (insn));
9049 add_reg_note (insn, REG_CFA_ADJUST_CFA, cfa);
9050 RTX_FRAME_RELATED_P (insn) = 1;
9053 /* Restore call saved fprs. */
9055 if (TARGET_64BIT)
9057 if (cfun_save_high_fprs_p)
9059 next_offset = cfun_frame_layout.f8_offset;
9060 for (i = FPR8_REGNUM; i <= FPR15_REGNUM; i++)
9062 if (cfun_fpr_save_p (i))
9064 restore_fpr (frame_pointer,
9065 offset + next_offset, i);
9066 cfa_restores
9067 = alloc_reg_note (REG_CFA_RESTORE,
9068 gen_rtx_REG (DFmode, i), cfa_restores);
9069 next_offset += 8;
9075 else
9077 next_offset = cfun_frame_layout.f4_offset;
9078 /* f4, f6 */
9079 for (i = FPR4_REGNUM; i <= FPR4_REGNUM + 1; i++)
9081 if (cfun_fpr_save_p (i))
9083 restore_fpr (frame_pointer,
9084 offset + next_offset, i);
9085 cfa_restores
9086 = alloc_reg_note (REG_CFA_RESTORE,
9087 gen_rtx_REG (DFmode, i), cfa_restores);
9088 next_offset += 8;
9090 else if (!TARGET_PACKED_STACK)
9091 next_offset += 8;
9096 /* Return register. */
9098 return_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
9100 /* Restore call saved gprs. */
9102 if (cfun_frame_layout.first_restore_gpr != -1)
9104 rtx insn, addr;
9105 int i;
9107 /* Check for global register and save them
9108 to stack location from where they get restored. */
9110 for (i = cfun_frame_layout.first_restore_gpr;
9111 i <= cfun_frame_layout.last_restore_gpr;
9112 i++)
9114 if (global_not_special_regno_p (i))
9116 addr = plus_constant (Pmode, frame_pointer,
9117 offset + cfun_frame_layout.gprs_offset
9118 + (i - cfun_frame_layout.first_save_gpr_slot)
9119 * UNITS_PER_LONG);
9120 addr = gen_rtx_MEM (Pmode, addr);
9121 set_mem_alias_set (addr, get_frame_alias_set ());
9122 emit_move_insn (addr, gen_rtx_REG (Pmode, i));
9124 else
9125 cfa_restores
9126 = alloc_reg_note (REG_CFA_RESTORE,
9127 gen_rtx_REG (Pmode, i), cfa_restores);
9130 if (! sibcall)
9132 /* Fetch return address from stack before load multiple,
9133 this will do good for scheduling.
9135 Only do this if we already decided that r14 needs to be
9136 saved to a stack slot. (And not just because r14 happens to
9137 be in between two GPRs which need saving.) Otherwise it
9138 would be difficult to take that decision back in
9139 s390_optimize_prologue. */
9140 if (cfun_gpr_save_slot (RETURN_REGNUM) == -1)
9142 int return_regnum = find_unused_clobbered_reg();
9143 if (!return_regnum)
9144 return_regnum = 4;
9145 return_reg = gen_rtx_REG (Pmode, return_regnum);
9147 addr = plus_constant (Pmode, frame_pointer,
9148 offset + cfun_frame_layout.gprs_offset
9149 + (RETURN_REGNUM
9150 - cfun_frame_layout.first_save_gpr_slot)
9151 * UNITS_PER_LONG);
9152 addr = gen_rtx_MEM (Pmode, addr);
9153 set_mem_alias_set (addr, get_frame_alias_set ());
9154 emit_move_insn (return_reg, addr);
9156 /* Once we did that optimization we have to make sure
9157 s390_optimize_prologue does not try to remove the
9158 store of r14 since we will not be able to find the
9159 load issued here. */
9160 cfun_frame_layout.save_return_addr_p = true;
9164 insn = restore_gprs (frame_pointer,
9165 offset + cfun_frame_layout.gprs_offset
9166 + (cfun_frame_layout.first_restore_gpr
9167 - cfun_frame_layout.first_save_gpr_slot)
9168 * UNITS_PER_LONG,
9169 cfun_frame_layout.first_restore_gpr,
9170 cfun_frame_layout.last_restore_gpr);
9171 insn = emit_insn (insn);
9172 REG_NOTES (insn) = cfa_restores;
9173 add_reg_note (insn, REG_CFA_DEF_CFA,
9174 plus_constant (Pmode, stack_pointer_rtx,
9175 STACK_POINTER_OFFSET));
9176 RTX_FRAME_RELATED_P (insn) = 1;
9179 s390_restore_gprs_from_fprs ();
9181 if (! sibcall)
9184 /* Return to caller. */
9186 p = rtvec_alloc (2);
9188 RTVEC_ELT (p, 0) = ret_rtx;
9189 RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, return_reg);
9190 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
9194 /* Implement TARGET_SET_UP_BY_PROLOGUE. */
9196 static void
9197 s300_set_up_by_prologue (hard_reg_set_container *regs)
9199 if (cfun->machine->base_reg
9200 && !call_really_used_regs[REGNO (cfun->machine->base_reg)])
9201 SET_HARD_REG_BIT (regs->set, REGNO (cfun->machine->base_reg));
9204 /* Return true if the function can use simple_return to return outside
9205 of a shrink-wrapped region. At present shrink-wrapping is supported
9206 in all cases. */
9208 bool
9209 s390_can_use_simple_return_insn (void)
9211 return true;
9214 /* Return true if the epilogue is guaranteed to contain only a return
9215 instruction and if a direct return can therefore be used instead.
9216 One of the main advantages of using direct return instructions
9217 is that we can then use conditional returns. */
9219 bool
9220 s390_can_use_return_insn (void)
9222 int i;
9224 if (!reload_completed)
9225 return false;
9227 if (crtl->profile)
9228 return false;
9230 if (TARGET_TPF_PROFILING)
9231 return false;
9233 for (i = 0; i < 16; i++)
9234 if (cfun_gpr_save_slot (i))
9235 return false;
9237 /* For 31 bit this is not covered by the frame_size check below
9238 since f4, f6 are saved in the register save area without needing
9239 additional stack space. */
9240 if (!TARGET_64BIT
9241 && (cfun_fpr_save_p (FPR4_REGNUM) || cfun_fpr_save_p (FPR6_REGNUM)))
9242 return false;
9244 if (cfun->machine->base_reg
9245 && !call_really_used_regs[REGNO (cfun->machine->base_reg)])
9246 return false;
9248 return cfun_frame_layout.frame_size == 0;
9251 /* Return the size in bytes of a function argument of
9252 type TYPE and/or mode MODE. At least one of TYPE or
9253 MODE must be specified. */
9255 static int
9256 s390_function_arg_size (machine_mode mode, const_tree type)
9258 if (type)
9259 return int_size_in_bytes (type);
9261 /* No type info available for some library calls ... */
9262 if (mode != BLKmode)
9263 return GET_MODE_SIZE (mode);
9265 /* If we have neither type nor mode, abort */
9266 gcc_unreachable ();
9269 /* Return true if a function argument of type TYPE and mode MODE
9270 is to be passed in a floating-point register, if available. */
9272 static bool
9273 s390_function_arg_float (machine_mode mode, const_tree type)
9275 int size = s390_function_arg_size (mode, type);
9276 if (size > 8)
9277 return false;
9279 /* Soft-float changes the ABI: no floating-point registers are used. */
9280 if (TARGET_SOFT_FLOAT)
9281 return false;
9283 /* No type info available for some library calls ... */
9284 if (!type)
9285 return mode == SFmode || mode == DFmode || mode == SDmode || mode == DDmode;
9287 /* The ABI says that record types with a single member are treated
9288 just like that member would be. */
9289 while (TREE_CODE (type) == RECORD_TYPE)
9291 tree field, single = NULL_TREE;
9293 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9295 if (TREE_CODE (field) != FIELD_DECL)
9296 continue;
9298 if (single == NULL_TREE)
9299 single = TREE_TYPE (field);
9300 else
9301 return false;
9304 if (single == NULL_TREE)
9305 return false;
9306 else
9307 type = single;
9310 return TREE_CODE (type) == REAL_TYPE;
9313 /* Return true if a function argument of type TYPE and mode MODE
9314 is to be passed in an integer register, or a pair of integer
9315 registers, if available. */
9317 static bool
9318 s390_function_arg_integer (machine_mode mode, const_tree type)
9320 int size = s390_function_arg_size (mode, type);
9321 if (size > 8)
9322 return false;
9324 /* No type info available for some library calls ... */
9325 if (!type)
9326 return GET_MODE_CLASS (mode) == MODE_INT
9327 || (TARGET_SOFT_FLOAT && SCALAR_FLOAT_MODE_P (mode));
9329 /* We accept small integral (and similar) types. */
9330 if (INTEGRAL_TYPE_P (type)
9331 || POINTER_TYPE_P (type)
9332 || TREE_CODE (type) == NULLPTR_TYPE
9333 || TREE_CODE (type) == OFFSET_TYPE
9334 || (TARGET_SOFT_FLOAT && TREE_CODE (type) == REAL_TYPE))
9335 return true;
9337 /* We also accept structs of size 1, 2, 4, 8 that are not
9338 passed in floating-point registers. */
9339 if (AGGREGATE_TYPE_P (type)
9340 && exact_log2 (size) >= 0
9341 && !s390_function_arg_float (mode, type))
9342 return true;
9344 return false;
9347 /* Return 1 if a function argument of type TYPE and mode MODE
9348 is to be passed by reference. The ABI specifies that only
9349 structures of size 1, 2, 4, or 8 bytes are passed by value,
9350 all other structures (and complex numbers) are passed by
9351 reference. */
9353 static bool
9354 s390_pass_by_reference (cumulative_args_t ca ATTRIBUTE_UNUSED,
9355 machine_mode mode, const_tree type,
9356 bool named ATTRIBUTE_UNUSED)
9358 int size = s390_function_arg_size (mode, type);
9359 if (size > 8)
9360 return true;
9362 if (type)
9364 if (AGGREGATE_TYPE_P (type) && exact_log2 (size) < 0)
9365 return 1;
9367 if (TREE_CODE (type) == COMPLEX_TYPE
9368 || TREE_CODE (type) == VECTOR_TYPE)
9369 return 1;
9372 return 0;
9375 /* Update the data in CUM to advance over an argument of mode MODE and
9376 data type TYPE. (TYPE is null for libcalls where that information
9377 may not be available.). The boolean NAMED specifies whether the
9378 argument is a named argument (as opposed to an unnamed argument
9379 matching an ellipsis). */
9381 static void
9382 s390_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
9383 const_tree type, bool named ATTRIBUTE_UNUSED)
9385 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
9387 if (s390_function_arg_float (mode, type))
9389 cum->fprs += 1;
9391 else if (s390_function_arg_integer (mode, type))
9393 int size = s390_function_arg_size (mode, type);
9394 cum->gprs += ((size + UNITS_PER_LONG - 1) / UNITS_PER_LONG);
9396 else
9397 gcc_unreachable ();
9400 /* Define where to put the arguments to a function.
9401 Value is zero to push the argument on the stack,
9402 or a hard register in which to store the argument.
9404 MODE is the argument's machine mode.
9405 TYPE is the data type of the argument (as a tree).
9406 This is null for libcalls where that information may
9407 not be available.
9408 CUM is a variable of type CUMULATIVE_ARGS which gives info about
9409 the preceding args and about the function being called.
9410 NAMED is nonzero if this argument is a named parameter
9411 (otherwise it is an extra parameter matching an ellipsis).
9413 On S/390, we use general purpose registers 2 through 6 to
9414 pass integer, pointer, and certain structure arguments, and
9415 floating point registers 0 and 2 (0, 2, 4, and 6 on 64-bit)
9416 to pass floating point arguments. All remaining arguments
9417 are pushed to the stack. */
9419 static rtx
9420 s390_function_arg (cumulative_args_t cum_v, machine_mode mode,
9421 const_tree type, bool named ATTRIBUTE_UNUSED)
9423 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
9425 if (s390_function_arg_float (mode, type))
9427 if (cum->fprs + 1 > FP_ARG_NUM_REG)
9428 return 0;
9429 else
9430 return gen_rtx_REG (mode, cum->fprs + 16);
9432 else if (s390_function_arg_integer (mode, type))
9434 int size = s390_function_arg_size (mode, type);
9435 int n_gprs = (size + UNITS_PER_LONG - 1) / UNITS_PER_LONG;
9437 if (cum->gprs + n_gprs > GP_ARG_NUM_REG)
9438 return 0;
9439 else if (n_gprs == 1 || UNITS_PER_WORD == UNITS_PER_LONG)
9440 return gen_rtx_REG (mode, cum->gprs + 2);
9441 else if (n_gprs == 2)
9443 rtvec p = rtvec_alloc (2);
9445 RTVEC_ELT (p, 0)
9446 = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, cum->gprs + 2),
9447 const0_rtx);
9448 RTVEC_ELT (p, 1)
9449 = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, cum->gprs + 3),
9450 GEN_INT (4));
9452 return gen_rtx_PARALLEL (mode, p);
9456 /* After the real arguments, expand_call calls us once again
9457 with a void_type_node type. Whatever we return here is
9458 passed as operand 2 to the call expanders.
9460 We don't need this feature ... */
9461 else if (type == void_type_node)
9462 return const0_rtx;
9464 gcc_unreachable ();
9467 /* Return true if return values of type TYPE should be returned
9468 in a memory buffer whose address is passed by the caller as
9469 hidden first argument. */
9471 static bool
9472 s390_return_in_memory (const_tree type, const_tree fundecl ATTRIBUTE_UNUSED)
9474 /* We accept small integral (and similar) types. */
9475 if (INTEGRAL_TYPE_P (type)
9476 || POINTER_TYPE_P (type)
9477 || TREE_CODE (type) == OFFSET_TYPE
9478 || TREE_CODE (type) == REAL_TYPE)
9479 return int_size_in_bytes (type) > 8;
9481 /* Aggregates and similar constructs are always returned
9482 in memory. */
9483 if (AGGREGATE_TYPE_P (type)
9484 || TREE_CODE (type) == COMPLEX_TYPE
9485 || TREE_CODE (type) == VECTOR_TYPE)
9486 return true;
9488 /* ??? We get called on all sorts of random stuff from
9489 aggregate_value_p. We can't abort, but it's not clear
9490 what's safe to return. Pretend it's a struct I guess. */
9491 return true;
9494 /* Function arguments and return values are promoted to word size. */
9496 static machine_mode
9497 s390_promote_function_mode (const_tree type, machine_mode mode,
9498 int *punsignedp,
9499 const_tree fntype ATTRIBUTE_UNUSED,
9500 int for_return ATTRIBUTE_UNUSED)
9502 if (INTEGRAL_MODE_P (mode)
9503 && GET_MODE_SIZE (mode) < UNITS_PER_LONG)
9505 if (type != NULL_TREE && POINTER_TYPE_P (type))
9506 *punsignedp = POINTERS_EXTEND_UNSIGNED;
9507 return Pmode;
9510 return mode;
9513 /* Define where to return a (scalar) value of type RET_TYPE.
9514 If RET_TYPE is null, define where to return a (scalar)
9515 value of mode MODE from a libcall. */
9517 static rtx
9518 s390_function_and_libcall_value (machine_mode mode,
9519 const_tree ret_type,
9520 const_tree fntype_or_decl,
9521 bool outgoing ATTRIBUTE_UNUSED)
9523 /* For normal functions perform the promotion as
9524 promote_function_mode would do. */
9525 if (ret_type)
9527 int unsignedp = TYPE_UNSIGNED (ret_type);
9528 mode = promote_function_mode (ret_type, mode, &unsignedp,
9529 fntype_or_decl, 1);
9532 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT || SCALAR_FLOAT_MODE_P (mode));
9533 gcc_assert (GET_MODE_SIZE (mode) <= 8);
9535 if (TARGET_HARD_FLOAT && SCALAR_FLOAT_MODE_P (mode))
9536 return gen_rtx_REG (mode, 16);
9537 else if (GET_MODE_SIZE (mode) <= UNITS_PER_LONG
9538 || UNITS_PER_LONG == UNITS_PER_WORD)
9539 return gen_rtx_REG (mode, 2);
9540 else if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_LONG)
9542 /* This case is triggered when returning a 64 bit value with
9543 -m31 -mzarch. Although the value would fit into a single
9544 register it has to be forced into a 32 bit register pair in
9545 order to match the ABI. */
9546 rtvec p = rtvec_alloc (2);
9548 RTVEC_ELT (p, 0)
9549 = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, 2), const0_rtx);
9550 RTVEC_ELT (p, 1)
9551 = gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, 3), GEN_INT (4));
9553 return gen_rtx_PARALLEL (mode, p);
9556 gcc_unreachable ();
9559 /* Define where to return a scalar return value of type RET_TYPE. */
9561 static rtx
9562 s390_function_value (const_tree ret_type, const_tree fn_decl_or_type,
9563 bool outgoing)
9565 return s390_function_and_libcall_value (TYPE_MODE (ret_type), ret_type,
9566 fn_decl_or_type, outgoing);
9569 /* Define where to return a scalar libcall return value of mode
9570 MODE. */
9572 static rtx
9573 s390_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
9575 return s390_function_and_libcall_value (mode, NULL_TREE,
9576 NULL_TREE, true);
9580 /* Create and return the va_list datatype.
9582 On S/390, va_list is an array type equivalent to
9584 typedef struct __va_list_tag
9586 long __gpr;
9587 long __fpr;
9588 void *__overflow_arg_area;
9589 void *__reg_save_area;
9590 } va_list[1];
9592 where __gpr and __fpr hold the number of general purpose
9593 or floating point arguments used up to now, respectively,
9594 __overflow_arg_area points to the stack location of the
9595 next argument passed on the stack, and __reg_save_area
9596 always points to the start of the register area in the
9597 call frame of the current function. The function prologue
9598 saves all registers used for argument passing into this
9599 area if the function uses variable arguments. */
9601 static tree
9602 s390_build_builtin_va_list (void)
9604 tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
9606 record = lang_hooks.types.make_type (RECORD_TYPE);
9608 type_decl =
9609 build_decl (BUILTINS_LOCATION,
9610 TYPE_DECL, get_identifier ("__va_list_tag"), record);
9612 f_gpr = build_decl (BUILTINS_LOCATION,
9613 FIELD_DECL, get_identifier ("__gpr"),
9614 long_integer_type_node);
9615 f_fpr = build_decl (BUILTINS_LOCATION,
9616 FIELD_DECL, get_identifier ("__fpr"),
9617 long_integer_type_node);
9618 f_ovf = build_decl (BUILTINS_LOCATION,
9619 FIELD_DECL, get_identifier ("__overflow_arg_area"),
9620 ptr_type_node);
9621 f_sav = build_decl (BUILTINS_LOCATION,
9622 FIELD_DECL, get_identifier ("__reg_save_area"),
9623 ptr_type_node);
9625 va_list_gpr_counter_field = f_gpr;
9626 va_list_fpr_counter_field = f_fpr;
9628 DECL_FIELD_CONTEXT (f_gpr) = record;
9629 DECL_FIELD_CONTEXT (f_fpr) = record;
9630 DECL_FIELD_CONTEXT (f_ovf) = record;
9631 DECL_FIELD_CONTEXT (f_sav) = record;
9633 TYPE_STUB_DECL (record) = type_decl;
9634 TYPE_NAME (record) = type_decl;
9635 TYPE_FIELDS (record) = f_gpr;
9636 DECL_CHAIN (f_gpr) = f_fpr;
9637 DECL_CHAIN (f_fpr) = f_ovf;
9638 DECL_CHAIN (f_ovf) = f_sav;
9640 layout_type (record);
9642 /* The correct type is an array type of one element. */
9643 return build_array_type (record, build_index_type (size_zero_node));
9646 /* Implement va_start by filling the va_list structure VALIST.
9647 STDARG_P is always true, and ignored.
9648 NEXTARG points to the first anonymous stack argument.
9650 The following global variables are used to initialize
9651 the va_list structure:
9653 crtl->args.info:
9654 holds number of gprs and fprs used for named arguments.
9655 crtl->args.arg_offset_rtx:
9656 holds the offset of the first anonymous stack argument
9657 (relative to the virtual arg pointer). */
9659 static void
9660 s390_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
9662 HOST_WIDE_INT n_gpr, n_fpr;
9663 int off;
9664 tree f_gpr, f_fpr, f_ovf, f_sav;
9665 tree gpr, fpr, ovf, sav, t;
9667 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
9668 f_fpr = DECL_CHAIN (f_gpr);
9669 f_ovf = DECL_CHAIN (f_fpr);
9670 f_sav = DECL_CHAIN (f_ovf);
9672 valist = build_simple_mem_ref (valist);
9673 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
9674 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
9675 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
9676 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
9678 /* Count number of gp and fp argument registers used. */
9680 n_gpr = crtl->args.info.gprs;
9681 n_fpr = crtl->args.info.fprs;
9683 if (cfun->va_list_gpr_size)
9685 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
9686 build_int_cst (NULL_TREE, n_gpr));
9687 TREE_SIDE_EFFECTS (t) = 1;
9688 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9691 if (cfun->va_list_fpr_size)
9693 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
9694 build_int_cst (NULL_TREE, n_fpr));
9695 TREE_SIDE_EFFECTS (t) = 1;
9696 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9699 /* Find the overflow area. */
9700 if (n_gpr + cfun->va_list_gpr_size > GP_ARG_NUM_REG
9701 || n_fpr + cfun->va_list_fpr_size > FP_ARG_NUM_REG)
9703 t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
9705 off = INTVAL (crtl->args.arg_offset_rtx);
9706 off = off < 0 ? 0 : off;
9707 if (TARGET_DEBUG_ARG)
9708 fprintf (stderr, "va_start: n_gpr = %d, n_fpr = %d off %d\n",
9709 (int)n_gpr, (int)n_fpr, off);
9711 t = fold_build_pointer_plus_hwi (t, off);
9713 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
9714 TREE_SIDE_EFFECTS (t) = 1;
9715 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9718 /* Find the register save area. */
9719 if ((cfun->va_list_gpr_size && n_gpr < GP_ARG_NUM_REG)
9720 || (cfun->va_list_fpr_size && n_fpr < FP_ARG_NUM_REG))
9722 t = make_tree (TREE_TYPE (sav), return_address_pointer_rtx);
9723 t = fold_build_pointer_plus_hwi (t, -RETURN_REGNUM * UNITS_PER_LONG);
9725 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
9726 TREE_SIDE_EFFECTS (t) = 1;
9727 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9731 /* Implement va_arg by updating the va_list structure
9732 VALIST as required to retrieve an argument of type
9733 TYPE, and returning that argument.
9735 Generates code equivalent to:
9737 if (integral value) {
9738 if (size <= 4 && args.gpr < 5 ||
9739 size > 4 && args.gpr < 4 )
9740 ret = args.reg_save_area[args.gpr+8]
9741 else
9742 ret = *args.overflow_arg_area++;
9743 } else if (float value) {
9744 if (args.fgpr < 2)
9745 ret = args.reg_save_area[args.fpr+64]
9746 else
9747 ret = *args.overflow_arg_area++;
9748 } else if (aggregate value) {
9749 if (args.gpr < 5)
9750 ret = *args.reg_save_area[args.gpr]
9751 else
9752 ret = **args.overflow_arg_area++;
9753 } */
9755 static tree
9756 s390_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
9757 gimple_seq *post_p ATTRIBUTE_UNUSED)
9759 tree f_gpr, f_fpr, f_ovf, f_sav;
9760 tree gpr, fpr, ovf, sav, reg, t, u;
9761 int indirect_p, size, n_reg, sav_ofs, sav_scale, max_reg;
9762 tree lab_false, lab_over, addr;
9764 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
9765 f_fpr = DECL_CHAIN (f_gpr);
9766 f_ovf = DECL_CHAIN (f_fpr);
9767 f_sav = DECL_CHAIN (f_ovf);
9769 valist = build_va_arg_indirect_ref (valist);
9770 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
9771 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
9772 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
9774 /* The tree for args* cannot be shared between gpr/fpr and ovf since
9775 both appear on a lhs. */
9776 valist = unshare_expr (valist);
9777 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
9779 size = int_size_in_bytes (type);
9781 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
9783 if (TARGET_DEBUG_ARG)
9785 fprintf (stderr, "va_arg: aggregate type");
9786 debug_tree (type);
9789 /* Aggregates are passed by reference. */
9790 indirect_p = 1;
9791 reg = gpr;
9792 n_reg = 1;
9794 /* kernel stack layout on 31 bit: It is assumed here that no padding
9795 will be added by s390_frame_info because for va_args always an even
9796 number of gprs has to be saved r15-r2 = 14 regs. */
9797 sav_ofs = 2 * UNITS_PER_LONG;
9798 sav_scale = UNITS_PER_LONG;
9799 size = UNITS_PER_LONG;
9800 max_reg = GP_ARG_NUM_REG - n_reg;
9802 else if (s390_function_arg_float (TYPE_MODE (type), type))
9804 if (TARGET_DEBUG_ARG)
9806 fprintf (stderr, "va_arg: float type");
9807 debug_tree (type);
9810 /* FP args go in FP registers, if present. */
9811 indirect_p = 0;
9812 reg = fpr;
9813 n_reg = 1;
9814 sav_ofs = 16 * UNITS_PER_LONG;
9815 sav_scale = 8;
9816 max_reg = FP_ARG_NUM_REG - n_reg;
9818 else
9820 if (TARGET_DEBUG_ARG)
9822 fprintf (stderr, "va_arg: other type");
9823 debug_tree (type);
9826 /* Otherwise into GP registers. */
9827 indirect_p = 0;
9828 reg = gpr;
9829 n_reg = (size + UNITS_PER_LONG - 1) / UNITS_PER_LONG;
9831 /* kernel stack layout on 31 bit: It is assumed here that no padding
9832 will be added by s390_frame_info because for va_args always an even
9833 number of gprs has to be saved r15-r2 = 14 regs. */
9834 sav_ofs = 2 * UNITS_PER_LONG;
9836 if (size < UNITS_PER_LONG)
9837 sav_ofs += UNITS_PER_LONG - size;
9839 sav_scale = UNITS_PER_LONG;
9840 max_reg = GP_ARG_NUM_REG - n_reg;
9843 /* Pull the value out of the saved registers ... */
9845 lab_false = create_artificial_label (UNKNOWN_LOCATION);
9846 lab_over = create_artificial_label (UNKNOWN_LOCATION);
9847 addr = create_tmp_var (ptr_type_node, "addr");
9849 t = fold_convert (TREE_TYPE (reg), size_int (max_reg));
9850 t = build2 (GT_EXPR, boolean_type_node, reg, t);
9851 u = build1 (GOTO_EXPR, void_type_node, lab_false);
9852 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
9853 gimplify_and_add (t, pre_p);
9855 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
9856 u = build2 (MULT_EXPR, TREE_TYPE (reg), reg,
9857 fold_convert (TREE_TYPE (reg), size_int (sav_scale)));
9858 t = fold_build_pointer_plus (t, u);
9860 gimplify_assign (addr, t, pre_p);
9862 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
9864 gimple_seq_add_stmt (pre_p, gimple_build_label (lab_false));
9867 /* ... Otherwise out of the overflow area. */
9869 t = ovf;
9870 if (size < UNITS_PER_LONG)
9871 t = fold_build_pointer_plus_hwi (t, UNITS_PER_LONG - size);
9873 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
9875 gimplify_assign (addr, t, pre_p);
9877 t = fold_build_pointer_plus_hwi (t, size);
9878 gimplify_assign (ovf, t, pre_p);
9880 gimple_seq_add_stmt (pre_p, gimple_build_label (lab_over));
9883 /* Increment register save count. */
9885 u = build2 (PREINCREMENT_EXPR, TREE_TYPE (reg), reg,
9886 fold_convert (TREE_TYPE (reg), size_int (n_reg)));
9887 gimplify_and_add (u, pre_p);
9889 if (indirect_p)
9891 t = build_pointer_type_for_mode (build_pointer_type (type),
9892 ptr_mode, true);
9893 addr = fold_convert (t, addr);
9894 addr = build_va_arg_indirect_ref (addr);
9896 else
9898 t = build_pointer_type_for_mode (type, ptr_mode, true);
9899 addr = fold_convert (t, addr);
9902 return build_va_arg_indirect_ref (addr);
9905 /* Emit rtl for the tbegin or tbegin_retry (RETRY != NULL_RTX)
9906 expanders.
9907 DEST - Register location where CC will be stored.
9908 TDB - Pointer to a 256 byte area where to store the transaction.
9909 diagnostic block. NULL if TDB is not needed.
9910 RETRY - Retry count value. If non-NULL a retry loop for CC2
9911 is emitted
9912 CLOBBER_FPRS_P - If true clobbers for all FPRs are emitted as part
9913 of the tbegin instruction pattern. */
9915 void
9916 s390_expand_tbegin (rtx dest, rtx tdb, rtx retry, bool clobber_fprs_p)
9918 rtx retry_plus_two = gen_reg_rtx (SImode);
9919 rtx retry_reg = gen_reg_rtx (SImode);
9920 rtx_code_label *retry_label = NULL;
9922 if (retry != NULL_RTX)
9924 emit_move_insn (retry_reg, retry);
9925 emit_insn (gen_addsi3 (retry_plus_two, retry_reg, const2_rtx));
9926 emit_insn (gen_addsi3 (retry_reg, retry_reg, const1_rtx));
9927 retry_label = gen_label_rtx ();
9928 emit_label (retry_label);
9931 if (clobber_fprs_p)
9932 emit_insn (gen_tbegin_1 (gen_rtx_CONST_INT (VOIDmode, TBEGIN_MASK), tdb));
9933 else
9934 emit_insn (gen_tbegin_nofloat_1 (gen_rtx_CONST_INT (VOIDmode, TBEGIN_MASK),
9935 tdb));
9937 emit_move_insn (dest, gen_rtx_UNSPEC (SImode,
9938 gen_rtvec (1, gen_rtx_REG (CCRAWmode,
9939 CC_REGNUM)),
9940 UNSPEC_CC_TO_INT));
9941 if (retry != NULL_RTX)
9943 const int CC0 = 1 << 3;
9944 const int CC1 = 1 << 2;
9945 const int CC3 = 1 << 0;
9946 rtx jump;
9947 rtx count = gen_reg_rtx (SImode);
9948 rtx_code_label *leave_label = gen_label_rtx ();
9950 /* Exit for success and permanent failures. */
9951 jump = s390_emit_jump (leave_label,
9952 gen_rtx_EQ (VOIDmode,
9953 gen_rtx_REG (CCRAWmode, CC_REGNUM),
9954 gen_rtx_CONST_INT (VOIDmode, CC0 | CC1 | CC3)));
9955 LABEL_NUSES (leave_label) = 1;
9957 /* CC2 - transient failure. Perform retry with ppa. */
9958 emit_move_insn (count, retry_plus_two);
9959 emit_insn (gen_subsi3 (count, count, retry_reg));
9960 emit_insn (gen_tx_assist (count));
9961 jump = emit_jump_insn (gen_doloop_si64 (retry_label,
9962 retry_reg,
9963 retry_reg));
9964 JUMP_LABEL (jump) = retry_label;
9965 LABEL_NUSES (retry_label) = 1;
9966 emit_label (leave_label);
9970 /* Builtins. */
9972 enum s390_builtin
9974 S390_BUILTIN_TBEGIN,
9975 S390_BUILTIN_TBEGIN_NOFLOAT,
9976 S390_BUILTIN_TBEGIN_RETRY,
9977 S390_BUILTIN_TBEGIN_RETRY_NOFLOAT,
9978 S390_BUILTIN_TBEGINC,
9979 S390_BUILTIN_TEND,
9980 S390_BUILTIN_TABORT,
9981 S390_BUILTIN_NON_TX_STORE,
9982 S390_BUILTIN_TX_NESTING_DEPTH,
9983 S390_BUILTIN_TX_ASSIST,
9985 S390_BUILTIN_S390_SFPC,
9986 S390_BUILTIN_S390_EFPC,
9988 S390_BUILTIN_MAX
9991 tree s390_builtin_decls[S390_BUILTIN_MAX];
9993 static enum insn_code const code_for_builtin[S390_BUILTIN_MAX] = {
9994 CODE_FOR_tbegin,
9995 CODE_FOR_tbegin_nofloat,
9996 CODE_FOR_tbegin_retry,
9997 CODE_FOR_tbegin_retry_nofloat,
9998 CODE_FOR_tbeginc,
9999 CODE_FOR_tend,
10000 CODE_FOR_tabort,
10001 CODE_FOR_ntstg,
10002 CODE_FOR_etnd,
10003 CODE_FOR_tx_assist,
10005 CODE_FOR_s390_sfpc,
10006 CODE_FOR_s390_efpc
10009 static void
10010 s390_init_builtins (void)
10012 tree ftype, uint64_type;
10013 tree returns_twice_attr = tree_cons (get_identifier ("returns_twice"),
10014 NULL, NULL);
10015 tree noreturn_attr = tree_cons (get_identifier ("noreturn"), NULL, NULL);
10017 /* void foo (void) */
10018 ftype = build_function_type_list (void_type_node, NULL_TREE);
10019 s390_builtin_decls[S390_BUILTIN_TBEGINC] =
10020 add_builtin_function ("__builtin_tbeginc", ftype, S390_BUILTIN_TBEGINC,
10021 BUILT_IN_MD, NULL, NULL_TREE);
10023 /* void foo (int) */
10024 ftype = build_function_type_list (void_type_node, integer_type_node,
10025 NULL_TREE);
10026 s390_builtin_decls[S390_BUILTIN_TABORT] =
10027 add_builtin_function ("__builtin_tabort", ftype,
10028 S390_BUILTIN_TABORT, BUILT_IN_MD, NULL,
10029 noreturn_attr);
10030 s390_builtin_decls[S390_BUILTIN_TX_ASSIST] =
10031 add_builtin_function ("__builtin_tx_assist", ftype,
10032 S390_BUILTIN_TX_ASSIST, BUILT_IN_MD, NULL, NULL_TREE);
10034 /* void foo (unsigned) */
10035 ftype = build_function_type_list (void_type_node, unsigned_type_node,
10036 NULL_TREE);
10037 s390_builtin_decls[S390_BUILTIN_S390_SFPC] =
10038 add_builtin_function ("__builtin_s390_sfpc", ftype,
10039 S390_BUILTIN_S390_SFPC, BUILT_IN_MD, NULL, NULL_TREE);
10041 /* int foo (void *) */
10042 ftype = build_function_type_list (integer_type_node, ptr_type_node,
10043 NULL_TREE);
10044 s390_builtin_decls[S390_BUILTIN_TBEGIN] =
10045 add_builtin_function ("__builtin_tbegin", ftype, S390_BUILTIN_TBEGIN,
10046 BUILT_IN_MD, NULL, returns_twice_attr);
10047 s390_builtin_decls[S390_BUILTIN_TBEGIN_NOFLOAT] =
10048 add_builtin_function ("__builtin_tbegin_nofloat", ftype,
10049 S390_BUILTIN_TBEGIN_NOFLOAT,
10050 BUILT_IN_MD, NULL, returns_twice_attr);
10052 /* int foo (void *, int) */
10053 ftype = build_function_type_list (integer_type_node, ptr_type_node,
10054 integer_type_node, NULL_TREE);
10055 s390_builtin_decls[S390_BUILTIN_TBEGIN_RETRY] =
10056 add_builtin_function ("__builtin_tbegin_retry", ftype,
10057 S390_BUILTIN_TBEGIN_RETRY,
10058 BUILT_IN_MD,
10059 NULL, returns_twice_attr);
10060 s390_builtin_decls[S390_BUILTIN_TBEGIN_RETRY_NOFLOAT] =
10061 add_builtin_function ("__builtin_tbegin_retry_nofloat", ftype,
10062 S390_BUILTIN_TBEGIN_RETRY_NOFLOAT,
10063 BUILT_IN_MD,
10064 NULL, returns_twice_attr);
10066 /* int foo (void) */
10067 ftype = build_function_type_list (integer_type_node, NULL_TREE);
10068 s390_builtin_decls[S390_BUILTIN_TX_NESTING_DEPTH] =
10069 add_builtin_function ("__builtin_tx_nesting_depth", ftype,
10070 S390_BUILTIN_TX_NESTING_DEPTH,
10071 BUILT_IN_MD, NULL, NULL_TREE);
10072 s390_builtin_decls[S390_BUILTIN_TEND] =
10073 add_builtin_function ("__builtin_tend", ftype,
10074 S390_BUILTIN_TEND, BUILT_IN_MD, NULL, NULL_TREE);
10076 /* unsigned foo (void) */
10077 ftype = build_function_type_list (unsigned_type_node, NULL_TREE);
10078 s390_builtin_decls[S390_BUILTIN_S390_EFPC] =
10079 add_builtin_function ("__builtin_s390_efpc", ftype,
10080 S390_BUILTIN_S390_EFPC, BUILT_IN_MD, NULL, NULL_TREE);
10082 /* void foo (uint64_t *, uint64_t) */
10083 if (TARGET_64BIT)
10084 uint64_type = long_unsigned_type_node;
10085 else
10086 uint64_type = long_long_unsigned_type_node;
10088 ftype = build_function_type_list (void_type_node,
10089 build_pointer_type (uint64_type),
10090 uint64_type, NULL_TREE);
10091 s390_builtin_decls[S390_BUILTIN_NON_TX_STORE] =
10092 add_builtin_function ("__builtin_non_tx_store", ftype,
10093 S390_BUILTIN_NON_TX_STORE,
10094 BUILT_IN_MD, NULL, NULL_TREE);
10097 /* Expand an expression EXP that calls a built-in function,
10098 with result going to TARGET if that's convenient
10099 (and in mode MODE if that's convenient).
10100 SUBTARGET may be used as the target for computing one of EXP's operands.
10101 IGNORE is nonzero if the value is to be ignored. */
10103 static rtx
10104 s390_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
10105 machine_mode mode ATTRIBUTE_UNUSED,
10106 int ignore ATTRIBUTE_UNUSED)
10108 #define MAX_ARGS 2
10110 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10111 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10112 enum insn_code icode;
10113 rtx op[MAX_ARGS], pat;
10114 int arity;
10115 bool nonvoid;
10116 tree arg;
10117 call_expr_arg_iterator iter;
10119 if (fcode >= S390_BUILTIN_MAX)
10120 internal_error ("bad builtin fcode");
10121 icode = code_for_builtin[fcode];
10122 if (icode == 0)
10123 internal_error ("bad builtin fcode");
10125 if (!TARGET_HTM)
10126 error ("Transactional execution builtins not enabled (-mhtm)\n");
10128 /* Set a flag in the machine specific cfun part in order to support
10129 saving/restoring of FPRs. */
10130 if (fcode == S390_BUILTIN_TBEGIN || fcode == S390_BUILTIN_TBEGIN_RETRY)
10131 cfun->machine->tbegin_p = true;
10133 nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
10135 arity = 0;
10136 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
10138 const struct insn_operand_data *insn_op;
10140 if (arg == error_mark_node)
10141 return NULL_RTX;
10142 if (arity >= MAX_ARGS)
10143 return NULL_RTX;
10145 insn_op = &insn_data[icode].operand[arity + nonvoid];
10147 op[arity] = expand_expr (arg, NULL_RTX, insn_op->mode, EXPAND_NORMAL);
10149 if (!(*insn_op->predicate) (op[arity], insn_op->mode))
10151 if (insn_op->predicate == memory_operand)
10153 /* Don't move a NULL pointer into a register. Otherwise
10154 we have to rely on combine being able to move it back
10155 in order to get an immediate 0 in the instruction. */
10156 if (op[arity] != const0_rtx)
10157 op[arity] = copy_to_mode_reg (Pmode, op[arity]);
10158 op[arity] = gen_rtx_MEM (insn_op->mode, op[arity]);
10160 else
10161 op[arity] = copy_to_mode_reg (insn_op->mode, op[arity]);
10164 arity++;
10167 if (nonvoid)
10169 machine_mode tmode = insn_data[icode].operand[0].mode;
10170 if (!target
10171 || GET_MODE (target) != tmode
10172 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
10173 target = gen_reg_rtx (tmode);
10176 switch (arity)
10178 case 0:
10179 pat = GEN_FCN (icode) (target);
10180 break;
10181 case 1:
10182 if (nonvoid)
10183 pat = GEN_FCN (icode) (target, op[0]);
10184 else
10185 pat = GEN_FCN (icode) (op[0]);
10186 break;
10187 case 2:
10188 if (nonvoid)
10189 pat = GEN_FCN (icode) (target, op[0], op[1]);
10190 else
10191 pat = GEN_FCN (icode) (op[0], op[1]);
10192 break;
10193 default:
10194 gcc_unreachable ();
10196 if (!pat)
10197 return NULL_RTX;
10198 emit_insn (pat);
10200 if (nonvoid)
10201 return target;
10202 else
10203 return const0_rtx;
10206 /* Return the decl for the target specific builtin with the function
10207 code FCODE. */
10209 static tree
10210 s390_builtin_decl (unsigned fcode, bool initialized_p ATTRIBUTE_UNUSED)
10212 if (fcode >= S390_BUILTIN_MAX)
10213 return error_mark_node;
10215 return s390_builtin_decls[fcode];
10218 /* We call mcount before the function prologue. So a profiled leaf
10219 function should stay a leaf function. */
10221 static bool
10222 s390_keep_leaf_when_profiled ()
10224 return true;
10227 /* Output assembly code for the trampoline template to
10228 stdio stream FILE.
10230 On S/390, we use gpr 1 internally in the trampoline code;
10231 gpr 0 is used to hold the static chain. */
10233 static void
10234 s390_asm_trampoline_template (FILE *file)
10236 rtx op[2];
10237 op[0] = gen_rtx_REG (Pmode, 0);
10238 op[1] = gen_rtx_REG (Pmode, 1);
10240 if (TARGET_64BIT)
10242 output_asm_insn ("basr\t%1,0", op); /* 2 byte */
10243 output_asm_insn ("lmg\t%0,%1,14(%1)", op); /* 6 byte */
10244 output_asm_insn ("br\t%1", op); /* 2 byte */
10245 ASM_OUTPUT_SKIP (file, (HOST_WIDE_INT)(TRAMPOLINE_SIZE - 10));
10247 else
10249 output_asm_insn ("basr\t%1,0", op); /* 2 byte */
10250 output_asm_insn ("lm\t%0,%1,6(%1)", op); /* 4 byte */
10251 output_asm_insn ("br\t%1", op); /* 2 byte */
10252 ASM_OUTPUT_SKIP (file, (HOST_WIDE_INT)(TRAMPOLINE_SIZE - 8));
10256 /* Emit RTL insns to initialize the variable parts of a trampoline.
10257 FNADDR is an RTX for the address of the function's pure code.
10258 CXT is an RTX for the static chain value for the function. */
10260 static void
10261 s390_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
10263 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
10264 rtx mem;
10266 emit_block_move (m_tramp, assemble_trampoline_template (),
10267 GEN_INT (2 * UNITS_PER_LONG), BLOCK_OP_NORMAL);
10269 mem = adjust_address (m_tramp, Pmode, 2 * UNITS_PER_LONG);
10270 emit_move_insn (mem, cxt);
10271 mem = adjust_address (m_tramp, Pmode, 3 * UNITS_PER_LONG);
10272 emit_move_insn (mem, fnaddr);
10275 /* Output assembler code to FILE to increment profiler label # LABELNO
10276 for profiling a function entry. */
10278 void
10279 s390_function_profiler (FILE *file, int labelno)
10281 rtx op[7];
10283 char label[128];
10284 ASM_GENERATE_INTERNAL_LABEL (label, "LP", labelno);
10286 fprintf (file, "# function profiler \n");
10288 op[0] = gen_rtx_REG (Pmode, RETURN_REGNUM);
10289 op[1] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
10290 op[1] = gen_rtx_MEM (Pmode, plus_constant (Pmode, op[1], UNITS_PER_LONG));
10292 op[2] = gen_rtx_REG (Pmode, 1);
10293 op[3] = gen_rtx_SYMBOL_REF (Pmode, label);
10294 SYMBOL_REF_FLAGS (op[3]) = SYMBOL_FLAG_LOCAL;
10296 op[4] = gen_rtx_SYMBOL_REF (Pmode, "_mcount");
10297 if (flag_pic)
10299 op[4] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op[4]), UNSPEC_PLT);
10300 op[4] = gen_rtx_CONST (Pmode, op[4]);
10303 if (TARGET_64BIT)
10305 output_asm_insn ("stg\t%0,%1", op);
10306 output_asm_insn ("larl\t%2,%3", op);
10307 output_asm_insn ("brasl\t%0,%4", op);
10308 output_asm_insn ("lg\t%0,%1", op);
10310 else if (!flag_pic)
10312 op[6] = gen_label_rtx ();
10314 output_asm_insn ("st\t%0,%1", op);
10315 output_asm_insn ("bras\t%2,%l6", op);
10316 output_asm_insn (".long\t%4", op);
10317 output_asm_insn (".long\t%3", op);
10318 targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[6]));
10319 output_asm_insn ("l\t%0,0(%2)", op);
10320 output_asm_insn ("l\t%2,4(%2)", op);
10321 output_asm_insn ("basr\t%0,%0", op);
10322 output_asm_insn ("l\t%0,%1", op);
10324 else
10326 op[5] = gen_label_rtx ();
10327 op[6] = gen_label_rtx ();
10329 output_asm_insn ("st\t%0,%1", op);
10330 output_asm_insn ("bras\t%2,%l6", op);
10331 targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[5]));
10332 output_asm_insn (".long\t%4-%l5", op);
10333 output_asm_insn (".long\t%3-%l5", op);
10334 targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[6]));
10335 output_asm_insn ("lr\t%0,%2", op);
10336 output_asm_insn ("a\t%0,0(%2)", op);
10337 output_asm_insn ("a\t%2,4(%2)", op);
10338 output_asm_insn ("basr\t%0,%0", op);
10339 output_asm_insn ("l\t%0,%1", op);
10343 /* Encode symbol attributes (local vs. global, tls model) of a SYMBOL_REF
10344 into its SYMBOL_REF_FLAGS. */
10346 static void
10347 s390_encode_section_info (tree decl, rtx rtl, int first)
10349 default_encode_section_info (decl, rtl, first);
10351 if (TREE_CODE (decl) == VAR_DECL)
10353 /* If a variable has a forced alignment to < 2 bytes, mark it
10354 with SYMBOL_FLAG_ALIGN1 to prevent it from being used as LARL
10355 operand. */
10356 if (DECL_USER_ALIGN (decl) && DECL_ALIGN (decl) < 16)
10357 SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_ALIGN1;
10358 if (!DECL_SIZE (decl)
10359 || !DECL_ALIGN (decl)
10360 || !tree_fits_shwi_p (DECL_SIZE (decl))
10361 || (DECL_ALIGN (decl) <= 64
10362 && DECL_ALIGN (decl) != tree_to_shwi (DECL_SIZE (decl))))
10363 SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_NOT_NATURALLY_ALIGNED;
10366 /* Literal pool references don't have a decl so they are handled
10367 differently here. We rely on the information in the MEM_ALIGN
10368 entry to decide upon natural alignment. */
10369 if (MEM_P (rtl)
10370 && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
10371 && TREE_CONSTANT_POOL_ADDRESS_P (XEXP (rtl, 0))
10372 && (MEM_ALIGN (rtl) == 0
10373 || GET_MODE_BITSIZE (GET_MODE (rtl)) == 0
10374 || MEM_ALIGN (rtl) < GET_MODE_BITSIZE (GET_MODE (rtl))))
10375 SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_NOT_NATURALLY_ALIGNED;
10378 /* Output thunk to FILE that implements a C++ virtual function call (with
10379 multiple inheritance) to FUNCTION. The thunk adjusts the this pointer
10380 by DELTA, and unless VCALL_OFFSET is zero, applies an additional adjustment
10381 stored at VCALL_OFFSET in the vtable whose address is located at offset 0
10382 relative to the resulting this pointer. */
10384 static void
10385 s390_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
10386 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
10387 tree function)
10389 rtx op[10];
10390 int nonlocal = 0;
10392 /* Make sure unwind info is emitted for the thunk if needed. */
10393 final_start_function (emit_barrier (), file, 1);
10395 /* Operand 0 is the target function. */
10396 op[0] = XEXP (DECL_RTL (function), 0);
10397 if (flag_pic && !SYMBOL_REF_LOCAL_P (op[0]))
10399 nonlocal = 1;
10400 op[0] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op[0]),
10401 TARGET_64BIT ? UNSPEC_PLT : UNSPEC_GOT);
10402 op[0] = gen_rtx_CONST (Pmode, op[0]);
10405 /* Operand 1 is the 'this' pointer. */
10406 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
10407 op[1] = gen_rtx_REG (Pmode, 3);
10408 else
10409 op[1] = gen_rtx_REG (Pmode, 2);
10411 /* Operand 2 is the delta. */
10412 op[2] = GEN_INT (delta);
10414 /* Operand 3 is the vcall_offset. */
10415 op[3] = GEN_INT (vcall_offset);
10417 /* Operand 4 is the temporary register. */
10418 op[4] = gen_rtx_REG (Pmode, 1);
10420 /* Operands 5 to 8 can be used as labels. */
10421 op[5] = NULL_RTX;
10422 op[6] = NULL_RTX;
10423 op[7] = NULL_RTX;
10424 op[8] = NULL_RTX;
10426 /* Operand 9 can be used for temporary register. */
10427 op[9] = NULL_RTX;
10429 /* Generate code. */
10430 if (TARGET_64BIT)
10432 /* Setup literal pool pointer if required. */
10433 if ((!DISP_IN_RANGE (delta)
10434 && !CONST_OK_FOR_K (delta)
10435 && !CONST_OK_FOR_Os (delta))
10436 || (!DISP_IN_RANGE (vcall_offset)
10437 && !CONST_OK_FOR_K (vcall_offset)
10438 && !CONST_OK_FOR_Os (vcall_offset)))
10440 op[5] = gen_label_rtx ();
10441 output_asm_insn ("larl\t%4,%5", op);
10444 /* Add DELTA to this pointer. */
10445 if (delta)
10447 if (CONST_OK_FOR_J (delta))
10448 output_asm_insn ("la\t%1,%2(%1)", op);
10449 else if (DISP_IN_RANGE (delta))
10450 output_asm_insn ("lay\t%1,%2(%1)", op);
10451 else if (CONST_OK_FOR_K (delta))
10452 output_asm_insn ("aghi\t%1,%2", op);
10453 else if (CONST_OK_FOR_Os (delta))
10454 output_asm_insn ("agfi\t%1,%2", op);
10455 else
10457 op[6] = gen_label_rtx ();
10458 output_asm_insn ("agf\t%1,%6-%5(%4)", op);
10462 /* Perform vcall adjustment. */
10463 if (vcall_offset)
10465 if (DISP_IN_RANGE (vcall_offset))
10467 output_asm_insn ("lg\t%4,0(%1)", op);
10468 output_asm_insn ("ag\t%1,%3(%4)", op);
10470 else if (CONST_OK_FOR_K (vcall_offset))
10472 output_asm_insn ("lghi\t%4,%3", op);
10473 output_asm_insn ("ag\t%4,0(%1)", op);
10474 output_asm_insn ("ag\t%1,0(%4)", op);
10476 else if (CONST_OK_FOR_Os (vcall_offset))
10478 output_asm_insn ("lgfi\t%4,%3", op);
10479 output_asm_insn ("ag\t%4,0(%1)", op);
10480 output_asm_insn ("ag\t%1,0(%4)", op);
10482 else
10484 op[7] = gen_label_rtx ();
10485 output_asm_insn ("llgf\t%4,%7-%5(%4)", op);
10486 output_asm_insn ("ag\t%4,0(%1)", op);
10487 output_asm_insn ("ag\t%1,0(%4)", op);
10491 /* Jump to target. */
10492 output_asm_insn ("jg\t%0", op);
10494 /* Output literal pool if required. */
10495 if (op[5])
10497 output_asm_insn (".align\t4", op);
10498 targetm.asm_out.internal_label (file, "L",
10499 CODE_LABEL_NUMBER (op[5]));
10501 if (op[6])
10503 targetm.asm_out.internal_label (file, "L",
10504 CODE_LABEL_NUMBER (op[6]));
10505 output_asm_insn (".long\t%2", op);
10507 if (op[7])
10509 targetm.asm_out.internal_label (file, "L",
10510 CODE_LABEL_NUMBER (op[7]));
10511 output_asm_insn (".long\t%3", op);
10514 else
10516 /* Setup base pointer if required. */
10517 if (!vcall_offset
10518 || (!DISP_IN_RANGE (delta)
10519 && !CONST_OK_FOR_K (delta)
10520 && !CONST_OK_FOR_Os (delta))
10521 || (!DISP_IN_RANGE (delta)
10522 && !CONST_OK_FOR_K (vcall_offset)
10523 && !CONST_OK_FOR_Os (vcall_offset)))
10525 op[5] = gen_label_rtx ();
10526 output_asm_insn ("basr\t%4,0", op);
10527 targetm.asm_out.internal_label (file, "L",
10528 CODE_LABEL_NUMBER (op[5]));
10531 /* Add DELTA to this pointer. */
10532 if (delta)
10534 if (CONST_OK_FOR_J (delta))
10535 output_asm_insn ("la\t%1,%2(%1)", op);
10536 else if (DISP_IN_RANGE (delta))
10537 output_asm_insn ("lay\t%1,%2(%1)", op);
10538 else if (CONST_OK_FOR_K (delta))
10539 output_asm_insn ("ahi\t%1,%2", op);
10540 else if (CONST_OK_FOR_Os (delta))
10541 output_asm_insn ("afi\t%1,%2", op);
10542 else
10544 op[6] = gen_label_rtx ();
10545 output_asm_insn ("a\t%1,%6-%5(%4)", op);
10549 /* Perform vcall adjustment. */
10550 if (vcall_offset)
10552 if (CONST_OK_FOR_J (vcall_offset))
10554 output_asm_insn ("l\t%4,0(%1)", op);
10555 output_asm_insn ("a\t%1,%3(%4)", op);
10557 else if (DISP_IN_RANGE (vcall_offset))
10559 output_asm_insn ("l\t%4,0(%1)", op);
10560 output_asm_insn ("ay\t%1,%3(%4)", op);
10562 else if (CONST_OK_FOR_K (vcall_offset))
10564 output_asm_insn ("lhi\t%4,%3", op);
10565 output_asm_insn ("a\t%4,0(%1)", op);
10566 output_asm_insn ("a\t%1,0(%4)", op);
10568 else if (CONST_OK_FOR_Os (vcall_offset))
10570 output_asm_insn ("iilf\t%4,%3", op);
10571 output_asm_insn ("a\t%4,0(%1)", op);
10572 output_asm_insn ("a\t%1,0(%4)", op);
10574 else
10576 op[7] = gen_label_rtx ();
10577 output_asm_insn ("l\t%4,%7-%5(%4)", op);
10578 output_asm_insn ("a\t%4,0(%1)", op);
10579 output_asm_insn ("a\t%1,0(%4)", op);
10582 /* We had to clobber the base pointer register.
10583 Re-setup the base pointer (with a different base). */
10584 op[5] = gen_label_rtx ();
10585 output_asm_insn ("basr\t%4,0", op);
10586 targetm.asm_out.internal_label (file, "L",
10587 CODE_LABEL_NUMBER (op[5]));
10590 /* Jump to target. */
10591 op[8] = gen_label_rtx ();
10593 if (!flag_pic)
10594 output_asm_insn ("l\t%4,%8-%5(%4)", op);
10595 else if (!nonlocal)
10596 output_asm_insn ("a\t%4,%8-%5(%4)", op);
10597 /* We cannot call through .plt, since .plt requires %r12 loaded. */
10598 else if (flag_pic == 1)
10600 output_asm_insn ("a\t%4,%8-%5(%4)", op);
10601 output_asm_insn ("l\t%4,%0(%4)", op);
10603 else if (flag_pic == 2)
10605 op[9] = gen_rtx_REG (Pmode, 0);
10606 output_asm_insn ("l\t%9,%8-4-%5(%4)", op);
10607 output_asm_insn ("a\t%4,%8-%5(%4)", op);
10608 output_asm_insn ("ar\t%4,%9", op);
10609 output_asm_insn ("l\t%4,0(%4)", op);
10612 output_asm_insn ("br\t%4", op);
10614 /* Output literal pool. */
10615 output_asm_insn (".align\t4", op);
10617 if (nonlocal && flag_pic == 2)
10618 output_asm_insn (".long\t%0", op);
10619 if (nonlocal)
10621 op[0] = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
10622 SYMBOL_REF_FLAGS (op[0]) = SYMBOL_FLAG_LOCAL;
10625 targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[8]));
10626 if (!flag_pic)
10627 output_asm_insn (".long\t%0", op);
10628 else
10629 output_asm_insn (".long\t%0-%5", op);
10631 if (op[6])
10633 targetm.asm_out.internal_label (file, "L",
10634 CODE_LABEL_NUMBER (op[6]));
10635 output_asm_insn (".long\t%2", op);
10637 if (op[7])
10639 targetm.asm_out.internal_label (file, "L",
10640 CODE_LABEL_NUMBER (op[7]));
10641 output_asm_insn (".long\t%3", op);
10644 final_end_function ();
10647 static bool
10648 s390_valid_pointer_mode (machine_mode mode)
10650 return (mode == SImode || (TARGET_64BIT && mode == DImode));
10653 /* Checks whether the given CALL_EXPR would use a caller
10654 saved register. This is used to decide whether sibling call
10655 optimization could be performed on the respective function
10656 call. */
10658 static bool
10659 s390_call_saved_register_used (tree call_expr)
10661 CUMULATIVE_ARGS cum_v;
10662 cumulative_args_t cum;
10663 tree parameter;
10664 machine_mode mode;
10665 tree type;
10666 rtx parm_rtx;
10667 int reg, i;
10669 INIT_CUMULATIVE_ARGS (cum_v, NULL, NULL, 0, 0);
10670 cum = pack_cumulative_args (&cum_v);
10672 for (i = 0; i < call_expr_nargs (call_expr); i++)
10674 parameter = CALL_EXPR_ARG (call_expr, i);
10675 gcc_assert (parameter);
10677 /* For an undeclared variable passed as parameter we will get
10678 an ERROR_MARK node here. */
10679 if (TREE_CODE (parameter) == ERROR_MARK)
10680 return true;
10682 type = TREE_TYPE (parameter);
10683 gcc_assert (type);
10685 mode = TYPE_MODE (type);
10686 gcc_assert (mode);
10688 if (pass_by_reference (&cum_v, mode, type, true))
10690 mode = Pmode;
10691 type = build_pointer_type (type);
10694 parm_rtx = s390_function_arg (cum, mode, type, 0);
10696 s390_function_arg_advance (cum, mode, type, 0);
10698 if (!parm_rtx)
10699 continue;
10701 if (REG_P (parm_rtx))
10703 for (reg = 0;
10704 reg < HARD_REGNO_NREGS (REGNO (parm_rtx), GET_MODE (parm_rtx));
10705 reg++)
10706 if (!call_used_regs[reg + REGNO (parm_rtx)])
10707 return true;
10710 if (GET_CODE (parm_rtx) == PARALLEL)
10712 int i;
10714 for (i = 0; i < XVECLEN (parm_rtx, 0); i++)
10716 rtx r = XEXP (XVECEXP (parm_rtx, 0, i), 0);
10718 gcc_assert (REG_P (r));
10720 for (reg = 0;
10721 reg < HARD_REGNO_NREGS (REGNO (r), GET_MODE (r));
10722 reg++)
10723 if (!call_used_regs[reg + REGNO (r)])
10724 return true;
10729 return false;
10732 /* Return true if the given call expression can be
10733 turned into a sibling call.
10734 DECL holds the declaration of the function to be called whereas
10735 EXP is the call expression itself. */
10737 static bool
10738 s390_function_ok_for_sibcall (tree decl, tree exp)
10740 /* The TPF epilogue uses register 1. */
10741 if (TARGET_TPF_PROFILING)
10742 return false;
10744 /* The 31 bit PLT code uses register 12 (GOT pointer - caller saved)
10745 which would have to be restored before the sibcall. */
10746 if (!TARGET_64BIT && flag_pic && decl && !targetm.binds_local_p (decl))
10747 return false;
10749 /* Register 6 on s390 is available as an argument register but unfortunately
10750 "caller saved". This makes functions needing this register for arguments
10751 not suitable for sibcalls. */
10752 return !s390_call_saved_register_used (exp);
10755 /* Return the fixed registers used for condition codes. */
10757 static bool
10758 s390_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
10760 *p1 = CC_REGNUM;
10761 *p2 = INVALID_REGNUM;
10763 return true;
10766 /* This function is used by the call expanders of the machine description.
10767 It emits the call insn itself together with the necessary operations
10768 to adjust the target address and returns the emitted insn.
10769 ADDR_LOCATION is the target address rtx
10770 TLS_CALL the location of the thread-local symbol
10771 RESULT_REG the register where the result of the call should be stored
10772 RETADDR_REG the register where the return address should be stored
10773 If this parameter is NULL_RTX the call is considered
10774 to be a sibling call. */
10776 rtx_insn *
10777 s390_emit_call (rtx addr_location, rtx tls_call, rtx result_reg,
10778 rtx retaddr_reg)
10780 bool plt_call = false;
10781 rtx_insn *insn;
10782 rtx call;
10783 rtx clobber;
10784 rtvec vec;
10786 /* Direct function calls need special treatment. */
10787 if (GET_CODE (addr_location) == SYMBOL_REF)
10789 /* When calling a global routine in PIC mode, we must
10790 replace the symbol itself with the PLT stub. */
10791 if (flag_pic && !SYMBOL_REF_LOCAL_P (addr_location))
10793 if (retaddr_reg != NULL_RTX)
10795 addr_location = gen_rtx_UNSPEC (Pmode,
10796 gen_rtvec (1, addr_location),
10797 UNSPEC_PLT);
10798 addr_location = gen_rtx_CONST (Pmode, addr_location);
10799 plt_call = true;
10801 else
10802 /* For -fpic code the PLT entries might use r12 which is
10803 call-saved. Therefore we cannot do a sibcall when
10804 calling directly using a symbol ref. When reaching
10805 this point we decided (in s390_function_ok_for_sibcall)
10806 to do a sibcall for a function pointer but one of the
10807 optimizers was able to get rid of the function pointer
10808 by propagating the symbol ref into the call. This
10809 optimization is illegal for S/390 so we turn the direct
10810 call into a indirect call again. */
10811 addr_location = force_reg (Pmode, addr_location);
10814 /* Unless we can use the bras(l) insn, force the
10815 routine address into a register. */
10816 if (!TARGET_SMALL_EXEC && !TARGET_CPU_ZARCH)
10818 if (flag_pic)
10819 addr_location = legitimize_pic_address (addr_location, 0);
10820 else
10821 addr_location = force_reg (Pmode, addr_location);
10825 /* If it is already an indirect call or the code above moved the
10826 SYMBOL_REF to somewhere else make sure the address can be found in
10827 register 1. */
10828 if (retaddr_reg == NULL_RTX
10829 && GET_CODE (addr_location) != SYMBOL_REF
10830 && !plt_call)
10832 emit_move_insn (gen_rtx_REG (Pmode, SIBCALL_REGNUM), addr_location);
10833 addr_location = gen_rtx_REG (Pmode, SIBCALL_REGNUM);
10836 addr_location = gen_rtx_MEM (QImode, addr_location);
10837 call = gen_rtx_CALL (VOIDmode, addr_location, const0_rtx);
10839 if (result_reg != NULL_RTX)
10840 call = gen_rtx_SET (VOIDmode, result_reg, call);
10842 if (retaddr_reg != NULL_RTX)
10844 clobber = gen_rtx_CLOBBER (VOIDmode, retaddr_reg);
10846 if (tls_call != NULL_RTX)
10847 vec = gen_rtvec (3, call, clobber,
10848 gen_rtx_USE (VOIDmode, tls_call));
10849 else
10850 vec = gen_rtvec (2, call, clobber);
10852 call = gen_rtx_PARALLEL (VOIDmode, vec);
10855 insn = emit_call_insn (call);
10857 /* 31-bit PLT stubs and tls calls use the GOT register implicitly. */
10858 if ((!TARGET_64BIT && plt_call) || tls_call != NULL_RTX)
10860 /* s390_function_ok_for_sibcall should
10861 have denied sibcalls in this case. */
10862 gcc_assert (retaddr_reg != NULL_RTX);
10863 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, 12));
10865 return insn;
10868 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE. */
10870 static void
10871 s390_conditional_register_usage (void)
10873 int i;
10875 if (flag_pic)
10877 fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
10878 call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
10880 if (TARGET_CPU_ZARCH)
10882 fixed_regs[BASE_REGNUM] = 0;
10883 call_used_regs[BASE_REGNUM] = 0;
10884 fixed_regs[RETURN_REGNUM] = 0;
10885 call_used_regs[RETURN_REGNUM] = 0;
10887 if (TARGET_64BIT)
10889 for (i = FPR8_REGNUM; i <= FPR15_REGNUM; i++)
10890 call_used_regs[i] = call_really_used_regs[i] = 0;
10892 else
10894 call_used_regs[FPR4_REGNUM] = call_really_used_regs[FPR4_REGNUM] = 0;
10895 call_used_regs[FPR6_REGNUM] = call_really_used_regs[FPR6_REGNUM] = 0;
10898 if (TARGET_SOFT_FLOAT)
10900 for (i = FPR0_REGNUM; i <= FPR15_REGNUM; i++)
10901 call_used_regs[i] = fixed_regs[i] = 1;
10905 /* Corresponding function to eh_return expander. */
10907 static GTY(()) rtx s390_tpf_eh_return_symbol;
10908 void
10909 s390_emit_tpf_eh_return (rtx target)
10911 rtx_insn *insn;
10912 rtx reg, orig_ra;
10914 if (!s390_tpf_eh_return_symbol)
10915 s390_tpf_eh_return_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tpf_eh_return");
10917 reg = gen_rtx_REG (Pmode, 2);
10918 orig_ra = gen_rtx_REG (Pmode, 3);
10920 emit_move_insn (reg, target);
10921 emit_move_insn (orig_ra, get_hard_reg_initial_val (Pmode, RETURN_REGNUM));
10922 insn = s390_emit_call (s390_tpf_eh_return_symbol, NULL_RTX, reg,
10923 gen_rtx_REG (Pmode, RETURN_REGNUM));
10924 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
10925 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), orig_ra);
10927 emit_move_insn (EH_RETURN_HANDLER_RTX, reg);
10930 /* Rework the prologue/epilogue to avoid saving/restoring
10931 registers unnecessarily. */
10933 static void
10934 s390_optimize_prologue (void)
10936 rtx_insn *insn, *new_insn, *next_insn;
10938 /* Do a final recompute of the frame-related data. */
10939 s390_optimize_register_info ();
10941 /* If all special registers are in fact used, there's nothing we
10942 can do, so no point in walking the insn list. */
10944 if (cfun_frame_layout.first_save_gpr <= BASE_REGNUM
10945 && cfun_frame_layout.last_save_gpr >= BASE_REGNUM
10946 && (TARGET_CPU_ZARCH
10947 || (cfun_frame_layout.first_save_gpr <= RETURN_REGNUM
10948 && cfun_frame_layout.last_save_gpr >= RETURN_REGNUM)))
10949 return;
10951 /* Search for prologue/epilogue insns and replace them. */
10953 for (insn = get_insns (); insn; insn = next_insn)
10955 int first, last, off;
10956 rtx set, base, offset;
10957 rtx pat;
10959 next_insn = NEXT_INSN (insn);
10961 if (! NONJUMP_INSN_P (insn) || ! RTX_FRAME_RELATED_P (insn))
10962 continue;
10964 pat = PATTERN (insn);
10966 /* Remove ldgr/lgdr instructions used for saving and restore
10967 GPRs if possible. */
10968 if (TARGET_Z10
10969 && GET_CODE (pat) == SET
10970 && GET_MODE (SET_SRC (pat)) == DImode
10971 && REG_P (SET_SRC (pat))
10972 && REG_P (SET_DEST (pat)))
10974 int src_regno = REGNO (SET_SRC (pat));
10975 int dest_regno = REGNO (SET_DEST (pat));
10976 int gpr_regno;
10977 int fpr_regno;
10979 if (!((GENERAL_REGNO_P (src_regno) && FP_REGNO_P (dest_regno))
10980 || (FP_REGNO_P (src_regno) && GENERAL_REGNO_P (dest_regno))))
10981 continue;
10983 gpr_regno = GENERAL_REGNO_P (src_regno) ? src_regno : dest_regno;
10984 fpr_regno = FP_REGNO_P (src_regno) ? src_regno : dest_regno;
10986 /* GPR must be call-saved, FPR must be call-clobbered. */
10987 if (!call_really_used_regs[fpr_regno]
10988 || call_really_used_regs[gpr_regno])
10989 continue;
10991 /* It must not happen that what we once saved in an FPR now
10992 needs a stack slot. */
10993 gcc_assert (cfun_gpr_save_slot (gpr_regno) != -1);
10995 if (cfun_gpr_save_slot (gpr_regno) == 0)
10997 remove_insn (insn);
10998 continue;
11002 if (GET_CODE (pat) == PARALLEL
11003 && store_multiple_operation (pat, VOIDmode))
11005 set = XVECEXP (pat, 0, 0);
11006 first = REGNO (SET_SRC (set));
11007 last = first + XVECLEN (pat, 0) - 1;
11008 offset = const0_rtx;
11009 base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
11010 off = INTVAL (offset);
11012 if (GET_CODE (base) != REG || off < 0)
11013 continue;
11014 if (cfun_frame_layout.first_save_gpr != -1
11015 && (cfun_frame_layout.first_save_gpr < first
11016 || cfun_frame_layout.last_save_gpr > last))
11017 continue;
11018 if (REGNO (base) != STACK_POINTER_REGNUM
11019 && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
11020 continue;
11021 if (first > BASE_REGNUM || last < BASE_REGNUM)
11022 continue;
11024 if (cfun_frame_layout.first_save_gpr != -1)
11026 rtx s_pat = save_gprs (base,
11027 off + (cfun_frame_layout.first_save_gpr
11028 - first) * UNITS_PER_LONG,
11029 cfun_frame_layout.first_save_gpr,
11030 cfun_frame_layout.last_save_gpr);
11031 new_insn = emit_insn_before (s_pat, insn);
11032 INSN_ADDRESSES_NEW (new_insn, -1);
11035 remove_insn (insn);
11036 continue;
11039 if (cfun_frame_layout.first_save_gpr == -1
11040 && GET_CODE (pat) == SET
11041 && GENERAL_REG_P (SET_SRC (pat))
11042 && GET_CODE (SET_DEST (pat)) == MEM)
11044 set = pat;
11045 first = REGNO (SET_SRC (set));
11046 offset = const0_rtx;
11047 base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
11048 off = INTVAL (offset);
11050 if (GET_CODE (base) != REG || off < 0)
11051 continue;
11052 if (REGNO (base) != STACK_POINTER_REGNUM
11053 && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
11054 continue;
11056 remove_insn (insn);
11057 continue;
11060 if (GET_CODE (pat) == PARALLEL
11061 && load_multiple_operation (pat, VOIDmode))
11063 set = XVECEXP (pat, 0, 0);
11064 first = REGNO (SET_DEST (set));
11065 last = first + XVECLEN (pat, 0) - 1;
11066 offset = const0_rtx;
11067 base = eliminate_constant_term (XEXP (SET_SRC (set), 0), &offset);
11068 off = INTVAL (offset);
11070 if (GET_CODE (base) != REG || off < 0)
11071 continue;
11073 if (cfun_frame_layout.first_restore_gpr != -1
11074 && (cfun_frame_layout.first_restore_gpr < first
11075 || cfun_frame_layout.last_restore_gpr > last))
11076 continue;
11077 if (REGNO (base) != STACK_POINTER_REGNUM
11078 && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
11079 continue;
11080 if (first > BASE_REGNUM || last < BASE_REGNUM)
11081 continue;
11083 if (cfun_frame_layout.first_restore_gpr != -1)
11085 rtx rpat = restore_gprs (base,
11086 off + (cfun_frame_layout.first_restore_gpr
11087 - first) * UNITS_PER_LONG,
11088 cfun_frame_layout.first_restore_gpr,
11089 cfun_frame_layout.last_restore_gpr);
11091 /* Remove REG_CFA_RESTOREs for registers that we no
11092 longer need to save. */
11093 REG_NOTES (rpat) = REG_NOTES (insn);
11094 for (rtx *ptr = &REG_NOTES (rpat); *ptr; )
11095 if (REG_NOTE_KIND (*ptr) == REG_CFA_RESTORE
11096 && ((int) REGNO (XEXP (*ptr, 0))
11097 < cfun_frame_layout.first_restore_gpr))
11098 *ptr = XEXP (*ptr, 1);
11099 else
11100 ptr = &XEXP (*ptr, 1);
11101 new_insn = emit_insn_before (rpat, insn);
11102 RTX_FRAME_RELATED_P (new_insn) = 1;
11103 INSN_ADDRESSES_NEW (new_insn, -1);
11106 remove_insn (insn);
11107 continue;
11110 if (cfun_frame_layout.first_restore_gpr == -1
11111 && GET_CODE (pat) == SET
11112 && GENERAL_REG_P (SET_DEST (pat))
11113 && GET_CODE (SET_SRC (pat)) == MEM)
11115 set = pat;
11116 first = REGNO (SET_DEST (set));
11117 offset = const0_rtx;
11118 base = eliminate_constant_term (XEXP (SET_SRC (set), 0), &offset);
11119 off = INTVAL (offset);
11121 if (GET_CODE (base) != REG || off < 0)
11122 continue;
11124 if (REGNO (base) != STACK_POINTER_REGNUM
11125 && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
11126 continue;
11128 remove_insn (insn);
11129 continue;
11134 /* On z10 and later the dynamic branch prediction must see the
11135 backward jump within a certain windows. If not it falls back to
11136 the static prediction. This function rearranges the loop backward
11137 branch in a way which makes the static prediction always correct.
11138 The function returns true if it added an instruction. */
11139 static bool
11140 s390_fix_long_loop_prediction (rtx_insn *insn)
11142 rtx set = single_set (insn);
11143 rtx code_label, label_ref, new_label;
11144 rtx_insn *uncond_jump;
11145 rtx_insn *cur_insn;
11146 rtx tmp;
11147 int distance;
11149 /* This will exclude branch on count and branch on index patterns
11150 since these are correctly statically predicted. */
11151 if (!set
11152 || SET_DEST (set) != pc_rtx
11153 || GET_CODE (SET_SRC(set)) != IF_THEN_ELSE)
11154 return false;
11156 /* Skip conditional returns. */
11157 if (ANY_RETURN_P (XEXP (SET_SRC (set), 1))
11158 && XEXP (SET_SRC (set), 2) == pc_rtx)
11159 return false;
11161 label_ref = (GET_CODE (XEXP (SET_SRC (set), 1)) == LABEL_REF ?
11162 XEXP (SET_SRC (set), 1) : XEXP (SET_SRC (set), 2));
11164 gcc_assert (GET_CODE (label_ref) == LABEL_REF);
11166 code_label = XEXP (label_ref, 0);
11168 if (INSN_ADDRESSES (INSN_UID (code_label)) == -1
11169 || INSN_ADDRESSES (INSN_UID (insn)) == -1
11170 || (INSN_ADDRESSES (INSN_UID (insn))
11171 - INSN_ADDRESSES (INSN_UID (code_label)) < PREDICT_DISTANCE))
11172 return false;
11174 for (distance = 0, cur_insn = PREV_INSN (insn);
11175 distance < PREDICT_DISTANCE - 6;
11176 distance += get_attr_length (cur_insn), cur_insn = PREV_INSN (cur_insn))
11177 if (!cur_insn || JUMP_P (cur_insn) || LABEL_P (cur_insn))
11178 return false;
11180 new_label = gen_label_rtx ();
11181 uncond_jump = emit_jump_insn_after (
11182 gen_rtx_SET (VOIDmode, pc_rtx,
11183 gen_rtx_LABEL_REF (VOIDmode, code_label)),
11184 insn);
11185 emit_label_after (new_label, uncond_jump);
11187 tmp = XEXP (SET_SRC (set), 1);
11188 XEXP (SET_SRC (set), 1) = XEXP (SET_SRC (set), 2);
11189 XEXP (SET_SRC (set), 2) = tmp;
11190 INSN_CODE (insn) = -1;
11192 XEXP (label_ref, 0) = new_label;
11193 JUMP_LABEL (insn) = new_label;
11194 JUMP_LABEL (uncond_jump) = code_label;
11196 return true;
11199 /* Returns 1 if INSN reads the value of REG for purposes not related
11200 to addressing of memory, and 0 otherwise. */
11201 static int
11202 s390_non_addr_reg_read_p (rtx reg, rtx_insn *insn)
11204 return reg_referenced_p (reg, PATTERN (insn))
11205 && !reg_used_in_mem_p (REGNO (reg), PATTERN (insn));
11208 /* Starting from INSN find_cond_jump looks downwards in the insn
11209 stream for a single jump insn which is the last user of the
11210 condition code set in INSN. */
11211 static rtx_insn *
11212 find_cond_jump (rtx_insn *insn)
11214 for (; insn; insn = NEXT_INSN (insn))
11216 rtx ite, cc;
11218 if (LABEL_P (insn))
11219 break;
11221 if (!JUMP_P (insn))
11223 if (reg_mentioned_p (gen_rtx_REG (CCmode, CC_REGNUM), insn))
11224 break;
11225 continue;
11228 /* This will be triggered by a return. */
11229 if (GET_CODE (PATTERN (insn)) != SET)
11230 break;
11232 gcc_assert (SET_DEST (PATTERN (insn)) == pc_rtx);
11233 ite = SET_SRC (PATTERN (insn));
11235 if (GET_CODE (ite) != IF_THEN_ELSE)
11236 break;
11238 cc = XEXP (XEXP (ite, 0), 0);
11239 if (!REG_P (cc) || !CC_REGNO_P (REGNO (cc)))
11240 break;
11242 if (find_reg_note (insn, REG_DEAD, cc))
11243 return insn;
11244 break;
11247 return NULL;
11250 /* Swap the condition in COND and the operands in OP0 and OP1 so that
11251 the semantics does not change. If NULL_RTX is passed as COND the
11252 function tries to find the conditional jump starting with INSN. */
11253 static void
11254 s390_swap_cmp (rtx cond, rtx *op0, rtx *op1, rtx_insn *insn)
11256 rtx tmp = *op0;
11258 if (cond == NULL_RTX)
11260 rtx_insn *jump = find_cond_jump (NEXT_INSN (insn));
11261 rtx set = jump ? single_set (jump) : NULL_RTX;
11263 if (set == NULL_RTX)
11264 return;
11266 cond = XEXP (SET_SRC (set), 0);
11269 *op0 = *op1;
11270 *op1 = tmp;
11271 PUT_CODE (cond, swap_condition (GET_CODE (cond)));
11274 /* On z10, instructions of the compare-and-branch family have the
11275 property to access the register occurring as second operand with
11276 its bits complemented. If such a compare is grouped with a second
11277 instruction that accesses the same register non-complemented, and
11278 if that register's value is delivered via a bypass, then the
11279 pipeline recycles, thereby causing significant performance decline.
11280 This function locates such situations and exchanges the two
11281 operands of the compare. The function return true whenever it
11282 added an insn. */
11283 static bool
11284 s390_z10_optimize_cmp (rtx_insn *insn)
11286 rtx_insn *prev_insn, *next_insn;
11287 bool insn_added_p = false;
11288 rtx cond, *op0, *op1;
11290 if (GET_CODE (PATTERN (insn)) == PARALLEL)
11292 /* Handle compare and branch and branch on count
11293 instructions. */
11294 rtx pattern = single_set (insn);
11296 if (!pattern
11297 || SET_DEST (pattern) != pc_rtx
11298 || GET_CODE (SET_SRC (pattern)) != IF_THEN_ELSE)
11299 return false;
11301 cond = XEXP (SET_SRC (pattern), 0);
11302 op0 = &XEXP (cond, 0);
11303 op1 = &XEXP (cond, 1);
11305 else if (GET_CODE (PATTERN (insn)) == SET)
11307 rtx src, dest;
11309 /* Handle normal compare instructions. */
11310 src = SET_SRC (PATTERN (insn));
11311 dest = SET_DEST (PATTERN (insn));
11313 if (!REG_P (dest)
11314 || !CC_REGNO_P (REGNO (dest))
11315 || GET_CODE (src) != COMPARE)
11316 return false;
11318 /* s390_swap_cmp will try to find the conditional
11319 jump when passing NULL_RTX as condition. */
11320 cond = NULL_RTX;
11321 op0 = &XEXP (src, 0);
11322 op1 = &XEXP (src, 1);
11324 else
11325 return false;
11327 if (!REG_P (*op0) || !REG_P (*op1))
11328 return false;
11330 if (GET_MODE_CLASS (GET_MODE (*op0)) != MODE_INT)
11331 return false;
11333 /* Swap the COMPARE arguments and its mask if there is a
11334 conflicting access in the previous insn. */
11335 prev_insn = prev_active_insn (insn);
11336 if (prev_insn != NULL_RTX && INSN_P (prev_insn)
11337 && reg_referenced_p (*op1, PATTERN (prev_insn)))
11338 s390_swap_cmp (cond, op0, op1, insn);
11340 /* Check if there is a conflict with the next insn. If there
11341 was no conflict with the previous insn, then swap the
11342 COMPARE arguments and its mask. If we already swapped
11343 the operands, or if swapping them would cause a conflict
11344 with the previous insn, issue a NOP after the COMPARE in
11345 order to separate the two instuctions. */
11346 next_insn = next_active_insn (insn);
11347 if (next_insn != NULL_RTX && INSN_P (next_insn)
11348 && s390_non_addr_reg_read_p (*op1, next_insn))
11350 if (prev_insn != NULL_RTX && INSN_P (prev_insn)
11351 && s390_non_addr_reg_read_p (*op0, prev_insn))
11353 if (REGNO (*op1) == 0)
11354 emit_insn_after (gen_nop1 (), insn);
11355 else
11356 emit_insn_after (gen_nop (), insn);
11357 insn_added_p = true;
11359 else
11360 s390_swap_cmp (cond, op0, op1, insn);
11362 return insn_added_p;
11365 /* Perform machine-dependent processing. */
11367 static void
11368 s390_reorg (void)
11370 bool pool_overflow = false;
11372 /* Make sure all splits have been performed; splits after
11373 machine_dependent_reorg might confuse insn length counts. */
11374 split_all_insns_noflow ();
11376 /* Install the main literal pool and the associated base
11377 register load insns.
11379 In addition, there are two problematic situations we need
11380 to correct:
11382 - the literal pool might be > 4096 bytes in size, so that
11383 some of its elements cannot be directly accessed
11385 - a branch target might be > 64K away from the branch, so that
11386 it is not possible to use a PC-relative instruction.
11388 To fix those, we split the single literal pool into multiple
11389 pool chunks, reloading the pool base register at various
11390 points throughout the function to ensure it always points to
11391 the pool chunk the following code expects, and / or replace
11392 PC-relative branches by absolute branches.
11394 However, the two problems are interdependent: splitting the
11395 literal pool can move a branch further away from its target,
11396 causing the 64K limit to overflow, and on the other hand,
11397 replacing a PC-relative branch by an absolute branch means
11398 we need to put the branch target address into the literal
11399 pool, possibly causing it to overflow.
11401 So, we loop trying to fix up both problems until we manage
11402 to satisfy both conditions at the same time. Note that the
11403 loop is guaranteed to terminate as every pass of the loop
11404 strictly decreases the total number of PC-relative branches
11405 in the function. (This is not completely true as there
11406 might be branch-over-pool insns introduced by chunkify_start.
11407 Those never need to be split however.) */
11409 for (;;)
11411 struct constant_pool *pool = NULL;
11413 /* Collect the literal pool. */
11414 if (!pool_overflow)
11416 pool = s390_mainpool_start ();
11417 if (!pool)
11418 pool_overflow = true;
11421 /* If literal pool overflowed, start to chunkify it. */
11422 if (pool_overflow)
11423 pool = s390_chunkify_start ();
11425 /* Split out-of-range branches. If this has created new
11426 literal pool entries, cancel current chunk list and
11427 recompute it. zSeries machines have large branch
11428 instructions, so we never need to split a branch. */
11429 if (!TARGET_CPU_ZARCH && s390_split_branches ())
11431 if (pool_overflow)
11432 s390_chunkify_cancel (pool);
11433 else
11434 s390_mainpool_cancel (pool);
11436 continue;
11439 /* If we made it up to here, both conditions are satisfied.
11440 Finish up literal pool related changes. */
11441 if (pool_overflow)
11442 s390_chunkify_finish (pool);
11443 else
11444 s390_mainpool_finish (pool);
11446 /* We're done splitting branches. */
11447 cfun->machine->split_branches_pending_p = false;
11448 break;
11451 /* Generate out-of-pool execute target insns. */
11452 if (TARGET_CPU_ZARCH)
11454 rtx_insn *insn, *target;
11455 rtx label;
11457 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11459 label = s390_execute_label (insn);
11460 if (!label)
11461 continue;
11463 gcc_assert (label != const0_rtx);
11465 target = emit_label (XEXP (label, 0));
11466 INSN_ADDRESSES_NEW (target, -1);
11468 target = emit_insn (s390_execute_target (insn));
11469 INSN_ADDRESSES_NEW (target, -1);
11473 /* Try to optimize prologue and epilogue further. */
11474 s390_optimize_prologue ();
11476 /* Walk over the insns and do some >=z10 specific changes. */
11477 if (s390_tune == PROCESSOR_2097_Z10
11478 || s390_tune == PROCESSOR_2817_Z196
11479 || s390_tune == PROCESSOR_2827_ZEC12)
11481 rtx_insn *insn;
11482 bool insn_added_p = false;
11484 /* The insn lengths and addresses have to be up to date for the
11485 following manipulations. */
11486 shorten_branches (get_insns ());
11488 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11490 if (!INSN_P (insn) || INSN_CODE (insn) <= 0)
11491 continue;
11493 if (JUMP_P (insn))
11494 insn_added_p |= s390_fix_long_loop_prediction (insn);
11496 if ((GET_CODE (PATTERN (insn)) == PARALLEL
11497 || GET_CODE (PATTERN (insn)) == SET)
11498 && s390_tune == PROCESSOR_2097_Z10)
11499 insn_added_p |= s390_z10_optimize_cmp (insn);
11502 /* Adjust branches if we added new instructions. */
11503 if (insn_added_p)
11504 shorten_branches (get_insns ());
11508 /* Return true if INSN is a fp load insn writing register REGNO. */
11509 static inline bool
11510 s390_fpload_toreg (rtx_insn *insn, unsigned int regno)
11512 rtx set;
11513 enum attr_type flag = s390_safe_attr_type (insn);
11515 if (flag != TYPE_FLOADSF && flag != TYPE_FLOADDF)
11516 return false;
11518 set = single_set (insn);
11520 if (set == NULL_RTX)
11521 return false;
11523 if (!REG_P (SET_DEST (set)) || !MEM_P (SET_SRC (set)))
11524 return false;
11526 if (REGNO (SET_DEST (set)) != regno)
11527 return false;
11529 return true;
11532 /* This value describes the distance to be avoided between an
11533 aritmetic fp instruction and an fp load writing the same register.
11534 Z10_EARLYLOAD_DISTANCE - 1 as well as Z10_EARLYLOAD_DISTANCE + 1 is
11535 fine but the exact value has to be avoided. Otherwise the FP
11536 pipeline will throw an exception causing a major penalty. */
11537 #define Z10_EARLYLOAD_DISTANCE 7
11539 /* Rearrange the ready list in order to avoid the situation described
11540 for Z10_EARLYLOAD_DISTANCE. A problematic load instruction is
11541 moved to the very end of the ready list. */
11542 static void
11543 s390_z10_prevent_earlyload_conflicts (rtx_insn **ready, int *nready_p)
11545 unsigned int regno;
11546 int nready = *nready_p;
11547 rtx_insn *tmp;
11548 int i;
11549 rtx_insn *insn;
11550 rtx set;
11551 enum attr_type flag;
11552 int distance;
11554 /* Skip DISTANCE - 1 active insns. */
11555 for (insn = last_scheduled_insn, distance = Z10_EARLYLOAD_DISTANCE - 1;
11556 distance > 0 && insn != NULL_RTX;
11557 distance--, insn = prev_active_insn (insn))
11558 if (CALL_P (insn) || JUMP_P (insn))
11559 return;
11561 if (insn == NULL_RTX)
11562 return;
11564 set = single_set (insn);
11566 if (set == NULL_RTX || !REG_P (SET_DEST (set))
11567 || GET_MODE_CLASS (GET_MODE (SET_DEST (set))) != MODE_FLOAT)
11568 return;
11570 flag = s390_safe_attr_type (insn);
11572 if (flag == TYPE_FLOADSF || flag == TYPE_FLOADDF)
11573 return;
11575 regno = REGNO (SET_DEST (set));
11576 i = nready - 1;
11578 while (!s390_fpload_toreg (ready[i], regno) && i > 0)
11579 i--;
11581 if (!i)
11582 return;
11584 tmp = ready[i];
11585 memmove (&ready[1], &ready[0], sizeof (rtx_insn *) * i);
11586 ready[0] = tmp;
11590 /* The s390_sched_state variable tracks the state of the current or
11591 the last instruction group.
11593 0,1,2 number of instructions scheduled in the current group
11594 3 the last group is complete - normal insns
11595 4 the last group was a cracked/expanded insn */
11597 static int s390_sched_state;
11599 #define S390_OOO_SCHED_STATE_NORMAL 3
11600 #define S390_OOO_SCHED_STATE_CRACKED 4
11602 #define S390_OOO_SCHED_ATTR_MASK_CRACKED 0x1
11603 #define S390_OOO_SCHED_ATTR_MASK_EXPANDED 0x2
11604 #define S390_OOO_SCHED_ATTR_MASK_ENDGROUP 0x4
11605 #define S390_OOO_SCHED_ATTR_MASK_GROUPALONE 0x8
11607 static unsigned int
11608 s390_get_sched_attrmask (rtx_insn *insn)
11610 unsigned int mask = 0;
11612 if (get_attr_ooo_cracked (insn))
11613 mask |= S390_OOO_SCHED_ATTR_MASK_CRACKED;
11614 if (get_attr_ooo_expanded (insn))
11615 mask |= S390_OOO_SCHED_ATTR_MASK_EXPANDED;
11616 if (get_attr_ooo_endgroup (insn))
11617 mask |= S390_OOO_SCHED_ATTR_MASK_ENDGROUP;
11618 if (get_attr_ooo_groupalone (insn))
11619 mask |= S390_OOO_SCHED_ATTR_MASK_GROUPALONE;
11620 return mask;
11623 /* Return the scheduling score for INSN. The higher the score the
11624 better. The score is calculated from the OOO scheduling attributes
11625 of INSN and the scheduling state s390_sched_state. */
11626 static int
11627 s390_sched_score (rtx_insn *insn)
11629 unsigned int mask = s390_get_sched_attrmask (insn);
11630 int score = 0;
11632 switch (s390_sched_state)
11634 case 0:
11635 /* Try to put insns into the first slot which would otherwise
11636 break a group. */
11637 if ((mask & S390_OOO_SCHED_ATTR_MASK_CRACKED) != 0
11638 || (mask & S390_OOO_SCHED_ATTR_MASK_EXPANDED) != 0)
11639 score += 5;
11640 if ((mask & S390_OOO_SCHED_ATTR_MASK_GROUPALONE) != 0)
11641 score += 10;
11642 case 1:
11643 /* Prefer not cracked insns while trying to put together a
11644 group. */
11645 if ((mask & S390_OOO_SCHED_ATTR_MASK_CRACKED) == 0
11646 && (mask & S390_OOO_SCHED_ATTR_MASK_EXPANDED) == 0
11647 && (mask & S390_OOO_SCHED_ATTR_MASK_GROUPALONE) == 0)
11648 score += 10;
11649 if ((mask & S390_OOO_SCHED_ATTR_MASK_ENDGROUP) == 0)
11650 score += 5;
11651 break;
11652 case 2:
11653 /* Prefer not cracked insns while trying to put together a
11654 group. */
11655 if ((mask & S390_OOO_SCHED_ATTR_MASK_CRACKED) == 0
11656 && (mask & S390_OOO_SCHED_ATTR_MASK_EXPANDED) == 0
11657 && (mask & S390_OOO_SCHED_ATTR_MASK_GROUPALONE) == 0)
11658 score += 10;
11659 /* Prefer endgroup insns in the last slot. */
11660 if ((mask & S390_OOO_SCHED_ATTR_MASK_ENDGROUP) != 0)
11661 score += 10;
11662 break;
11663 case S390_OOO_SCHED_STATE_NORMAL:
11664 /* Prefer not cracked insns if the last was not cracked. */
11665 if ((mask & S390_OOO_SCHED_ATTR_MASK_CRACKED) == 0
11666 && (mask & S390_OOO_SCHED_ATTR_MASK_EXPANDED) == 0)
11667 score += 5;
11668 if ((mask & S390_OOO_SCHED_ATTR_MASK_GROUPALONE) != 0)
11669 score += 10;
11670 break;
11671 case S390_OOO_SCHED_STATE_CRACKED:
11672 /* Try to keep cracked insns together to prevent them from
11673 interrupting groups. */
11674 if ((mask & S390_OOO_SCHED_ATTR_MASK_CRACKED) != 0
11675 || (mask & S390_OOO_SCHED_ATTR_MASK_EXPANDED) != 0)
11676 score += 5;
11677 break;
11679 return score;
11682 /* This function is called via hook TARGET_SCHED_REORDER before
11683 issuing one insn from list READY which contains *NREADYP entries.
11684 For target z10 it reorders load instructions to avoid early load
11685 conflicts in the floating point pipeline */
11686 static int
11687 s390_sched_reorder (FILE *file, int verbose,
11688 rtx_insn **ready, int *nreadyp, int clock ATTRIBUTE_UNUSED)
11690 if (s390_tune == PROCESSOR_2097_Z10)
11691 if (reload_completed && *nreadyp > 1)
11692 s390_z10_prevent_earlyload_conflicts (ready, nreadyp);
11694 if (s390_tune == PROCESSOR_2827_ZEC12
11695 && reload_completed
11696 && *nreadyp > 1)
11698 int i;
11699 int last_index = *nreadyp - 1;
11700 int max_index = -1;
11701 int max_score = -1;
11702 rtx_insn *tmp;
11704 /* Just move the insn with the highest score to the top (the
11705 end) of the list. A full sort is not needed since a conflict
11706 in the hazard recognition cannot happen. So the top insn in
11707 the ready list will always be taken. */
11708 for (i = last_index; i >= 0; i--)
11710 int score;
11712 if (recog_memoized (ready[i]) < 0)
11713 continue;
11715 score = s390_sched_score (ready[i]);
11716 if (score > max_score)
11718 max_score = score;
11719 max_index = i;
11723 if (max_index != -1)
11725 if (max_index != last_index)
11727 tmp = ready[max_index];
11728 ready[max_index] = ready[last_index];
11729 ready[last_index] = tmp;
11731 if (verbose > 5)
11732 fprintf (file,
11733 "move insn %d to the top of list\n",
11734 INSN_UID (ready[last_index]));
11736 else if (verbose > 5)
11737 fprintf (file,
11738 "best insn %d already on top\n",
11739 INSN_UID (ready[last_index]));
11742 if (verbose > 5)
11744 fprintf (file, "ready list ooo attributes - sched state: %d\n",
11745 s390_sched_state);
11747 for (i = last_index; i >= 0; i--)
11749 if (recog_memoized (ready[i]) < 0)
11750 continue;
11751 fprintf (file, "insn %d score: %d: ", INSN_UID (ready[i]),
11752 s390_sched_score (ready[i]));
11753 #define PRINT_OOO_ATTR(ATTR) fprintf (file, "%s ", get_attr_##ATTR (ready[i]) ? #ATTR : "!" #ATTR);
11754 PRINT_OOO_ATTR (ooo_cracked);
11755 PRINT_OOO_ATTR (ooo_expanded);
11756 PRINT_OOO_ATTR (ooo_endgroup);
11757 PRINT_OOO_ATTR (ooo_groupalone);
11758 #undef PRINT_OOO_ATTR
11759 fprintf (file, "\n");
11764 return s390_issue_rate ();
11768 /* This function is called via hook TARGET_SCHED_VARIABLE_ISSUE after
11769 the scheduler has issued INSN. It stores the last issued insn into
11770 last_scheduled_insn in order to make it available for
11771 s390_sched_reorder. */
11772 static int
11773 s390_sched_variable_issue (FILE *file, int verbose, rtx_insn *insn, int more)
11775 last_scheduled_insn = insn;
11777 if (s390_tune == PROCESSOR_2827_ZEC12
11778 && reload_completed
11779 && recog_memoized (insn) >= 0)
11781 unsigned int mask = s390_get_sched_attrmask (insn);
11783 if ((mask & S390_OOO_SCHED_ATTR_MASK_CRACKED) != 0
11784 || (mask & S390_OOO_SCHED_ATTR_MASK_EXPANDED) != 0)
11785 s390_sched_state = S390_OOO_SCHED_STATE_CRACKED;
11786 else if ((mask & S390_OOO_SCHED_ATTR_MASK_ENDGROUP) != 0
11787 || (mask & S390_OOO_SCHED_ATTR_MASK_GROUPALONE) != 0)
11788 s390_sched_state = S390_OOO_SCHED_STATE_NORMAL;
11789 else
11791 /* Only normal insns are left (mask == 0). */
11792 switch (s390_sched_state)
11794 case 0:
11795 case 1:
11796 case 2:
11797 case S390_OOO_SCHED_STATE_NORMAL:
11798 if (s390_sched_state == S390_OOO_SCHED_STATE_NORMAL)
11799 s390_sched_state = 1;
11800 else
11801 s390_sched_state++;
11803 break;
11804 case S390_OOO_SCHED_STATE_CRACKED:
11805 s390_sched_state = S390_OOO_SCHED_STATE_NORMAL;
11806 break;
11809 if (verbose > 5)
11811 fprintf (file, "insn %d: ", INSN_UID (insn));
11812 #define PRINT_OOO_ATTR(ATTR) \
11813 fprintf (file, "%s ", get_attr_##ATTR (insn) ? #ATTR : "");
11814 PRINT_OOO_ATTR (ooo_cracked);
11815 PRINT_OOO_ATTR (ooo_expanded);
11816 PRINT_OOO_ATTR (ooo_endgroup);
11817 PRINT_OOO_ATTR (ooo_groupalone);
11818 #undef PRINT_OOO_ATTR
11819 fprintf (file, "\n");
11820 fprintf (file, "sched state: %d\n", s390_sched_state);
11824 if (GET_CODE (PATTERN (insn)) != USE
11825 && GET_CODE (PATTERN (insn)) != CLOBBER)
11826 return more - 1;
11827 else
11828 return more;
11831 static void
11832 s390_sched_init (FILE *file ATTRIBUTE_UNUSED,
11833 int verbose ATTRIBUTE_UNUSED,
11834 int max_ready ATTRIBUTE_UNUSED)
11836 last_scheduled_insn = NULL;
11837 s390_sched_state = 0;
11840 /* This target hook implementation for TARGET_LOOP_UNROLL_ADJUST calculates
11841 a new number struct loop *loop should be unrolled if tuned for cpus with
11842 a built-in stride prefetcher.
11843 The loop is analyzed for memory accesses by calling check_dpu for
11844 each rtx of the loop. Depending on the loop_depth and the amount of
11845 memory accesses a new number <=nunroll is returned to improve the
11846 behaviour of the hardware prefetch unit. */
11847 static unsigned
11848 s390_loop_unroll_adjust (unsigned nunroll, struct loop *loop)
11850 basic_block *bbs;
11851 rtx_insn *insn;
11852 unsigned i;
11853 unsigned mem_count = 0;
11855 if (s390_tune != PROCESSOR_2097_Z10
11856 && s390_tune != PROCESSOR_2817_Z196
11857 && s390_tune != PROCESSOR_2827_ZEC12)
11858 return nunroll;
11860 /* Count the number of memory references within the loop body. */
11861 bbs = get_loop_body (loop);
11862 subrtx_iterator::array_type array;
11863 for (i = 0; i < loop->num_nodes; i++)
11864 FOR_BB_INSNS (bbs[i], insn)
11865 if (INSN_P (insn) && INSN_CODE (insn) != -1)
11866 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
11867 if (MEM_P (*iter))
11868 mem_count += 1;
11869 free (bbs);
11871 /* Prevent division by zero, and we do not need to adjust nunroll in this case. */
11872 if (mem_count == 0)
11873 return nunroll;
11875 switch (loop_depth(loop))
11877 case 1:
11878 return MIN (nunroll, 28 / mem_count);
11879 case 2:
11880 return MIN (nunroll, 22 / mem_count);
11881 default:
11882 return MIN (nunroll, 16 / mem_count);
11886 static void
11887 s390_option_override (void)
11889 unsigned int i;
11890 cl_deferred_option *opt;
11891 vec<cl_deferred_option> *v =
11892 (vec<cl_deferred_option> *) s390_deferred_options;
11894 if (v)
11895 FOR_EACH_VEC_ELT (*v, i, opt)
11897 switch (opt->opt_index)
11899 case OPT_mhotpatch_:
11901 int val1;
11902 int val2;
11903 char s[256];
11904 char *t;
11906 strncpy (s, opt->arg, 256);
11907 s[255] = 0;
11908 t = strchr (s, ',');
11909 if (t != NULL)
11911 *t = 0;
11912 t++;
11913 val1 = integral_argument (s);
11914 val2 = integral_argument (t);
11916 else
11918 val1 = -1;
11919 val2 = -1;
11921 if (val1 == -1 || val2 == -1)
11923 /* argument is not a plain number */
11924 error ("arguments to %qs should be non-negative integers",
11925 "-mhotpatch=n,m");
11926 break;
11928 else if (val1 > s390_hotpatch_hw_max
11929 || val2 > s390_hotpatch_hw_max)
11931 error ("argument to %qs is too large (max. %d)",
11932 "-mhotpatch=n,m", s390_hotpatch_hw_max);
11933 break;
11935 s390_hotpatch_hw_before_label = val1;
11936 s390_hotpatch_hw_after_label = val2;
11937 break;
11939 default:
11940 gcc_unreachable ();
11944 /* Set up function hooks. */
11945 init_machine_status = s390_init_machine_status;
11947 /* Architecture mode defaults according to ABI. */
11948 if (!(target_flags_explicit & MASK_ZARCH))
11950 if (TARGET_64BIT)
11951 target_flags |= MASK_ZARCH;
11952 else
11953 target_flags &= ~MASK_ZARCH;
11956 /* Set the march default in case it hasn't been specified on
11957 cmdline. */
11958 if (s390_arch == PROCESSOR_max)
11960 s390_arch_string = TARGET_ZARCH? "z900" : "g5";
11961 s390_arch = TARGET_ZARCH ? PROCESSOR_2064_Z900 : PROCESSOR_9672_G5;
11962 s390_arch_flags = processor_flags_table[(int)s390_arch];
11965 /* Determine processor to tune for. */
11966 if (s390_tune == PROCESSOR_max)
11968 s390_tune = s390_arch;
11969 s390_tune_flags = s390_arch_flags;
11972 /* Sanity checks. */
11973 if (TARGET_ZARCH && !TARGET_CPU_ZARCH)
11974 error ("z/Architecture mode not supported on %s", s390_arch_string);
11975 if (TARGET_64BIT && !TARGET_ZARCH)
11976 error ("64-bit ABI not supported in ESA/390 mode");
11978 /* Use hardware DFP if available and not explicitly disabled by
11979 user. E.g. with -m31 -march=z10 -mzarch */
11980 if (!(target_flags_explicit & MASK_HARD_DFP) && TARGET_DFP)
11981 target_flags |= MASK_HARD_DFP;
11983 /* Enable hardware transactions if available and not explicitly
11984 disabled by user. E.g. with -m31 -march=zEC12 -mzarch */
11985 if (!(target_flags_explicit & MASK_OPT_HTM) && TARGET_CPU_HTM && TARGET_ZARCH)
11986 target_flags |= MASK_OPT_HTM;
11988 if (TARGET_HARD_DFP && !TARGET_DFP)
11990 if (target_flags_explicit & MASK_HARD_DFP)
11992 if (!TARGET_CPU_DFP)
11993 error ("hardware decimal floating point instructions"
11994 " not available on %s", s390_arch_string);
11995 if (!TARGET_ZARCH)
11996 error ("hardware decimal floating point instructions"
11997 " not available in ESA/390 mode");
11999 else
12000 target_flags &= ~MASK_HARD_DFP;
12003 if ((target_flags_explicit & MASK_SOFT_FLOAT) && TARGET_SOFT_FLOAT)
12005 if ((target_flags_explicit & MASK_HARD_DFP) && TARGET_HARD_DFP)
12006 error ("-mhard-dfp can%'t be used in conjunction with -msoft-float");
12008 target_flags &= ~MASK_HARD_DFP;
12011 /* Set processor cost function. */
12012 switch (s390_tune)
12014 case PROCESSOR_2084_Z990:
12015 s390_cost = &z990_cost;
12016 break;
12017 case PROCESSOR_2094_Z9_109:
12018 s390_cost = &z9_109_cost;
12019 break;
12020 case PROCESSOR_2097_Z10:
12021 s390_cost = &z10_cost;
12022 break;
12023 case PROCESSOR_2817_Z196:
12024 s390_cost = &z196_cost;
12025 break;
12026 case PROCESSOR_2827_ZEC12:
12027 s390_cost = &zEC12_cost;
12028 break;
12029 default:
12030 s390_cost = &z900_cost;
12033 if (TARGET_BACKCHAIN && TARGET_PACKED_STACK && TARGET_HARD_FLOAT)
12034 error ("-mbackchain -mpacked-stack -mhard-float are not supported "
12035 "in combination");
12037 if (s390_stack_size)
12039 if (s390_stack_guard >= s390_stack_size)
12040 error ("stack size must be greater than the stack guard value");
12041 else if (s390_stack_size > 1 << 16)
12042 error ("stack size must not be greater than 64k");
12044 else if (s390_stack_guard)
12045 error ("-mstack-guard implies use of -mstack-size");
12047 #ifdef TARGET_DEFAULT_LONG_DOUBLE_128
12048 if (!(target_flags_explicit & MASK_LONG_DOUBLE_128))
12049 target_flags |= MASK_LONG_DOUBLE_128;
12050 #endif
12052 if (s390_tune == PROCESSOR_2097_Z10
12053 || s390_tune == PROCESSOR_2817_Z196
12054 || s390_tune == PROCESSOR_2827_ZEC12)
12056 maybe_set_param_value (PARAM_MAX_UNROLLED_INSNS, 100,
12057 global_options.x_param_values,
12058 global_options_set.x_param_values);
12059 maybe_set_param_value (PARAM_MAX_UNROLL_TIMES, 32,
12060 global_options.x_param_values,
12061 global_options_set.x_param_values);
12062 maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 2000,
12063 global_options.x_param_values,
12064 global_options_set.x_param_values);
12065 maybe_set_param_value (PARAM_MAX_COMPLETELY_PEEL_TIMES, 64,
12066 global_options.x_param_values,
12067 global_options_set.x_param_values);
12070 maybe_set_param_value (PARAM_MAX_PENDING_LIST_LENGTH, 256,
12071 global_options.x_param_values,
12072 global_options_set.x_param_values);
12073 /* values for loop prefetching */
12074 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE, 256,
12075 global_options.x_param_values,
12076 global_options_set.x_param_values);
12077 maybe_set_param_value (PARAM_L1_CACHE_SIZE, 128,
12078 global_options.x_param_values,
12079 global_options_set.x_param_values);
12080 /* s390 has more than 2 levels and the size is much larger. Since
12081 we are always running virtualized assume that we only get a small
12082 part of the caches above l1. */
12083 maybe_set_param_value (PARAM_L2_CACHE_SIZE, 1500,
12084 global_options.x_param_values,
12085 global_options_set.x_param_values);
12086 maybe_set_param_value (PARAM_PREFETCH_MIN_INSN_TO_MEM_RATIO, 2,
12087 global_options.x_param_values,
12088 global_options_set.x_param_values);
12089 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES, 6,
12090 global_options.x_param_values,
12091 global_options_set.x_param_values);
12093 /* This cannot reside in s390_option_optimization_table since HAVE_prefetch
12094 requires the arch flags to be evaluated already. Since prefetching
12095 is beneficial on s390, we enable it if available. */
12096 if (flag_prefetch_loop_arrays < 0 && HAVE_prefetch && optimize >= 3)
12097 flag_prefetch_loop_arrays = 1;
12099 /* Use the alternative scheduling-pressure algorithm by default. */
12100 maybe_set_param_value (PARAM_SCHED_PRESSURE_ALGORITHM, 2,
12101 global_options.x_param_values,
12102 global_options_set.x_param_values);
12104 if (TARGET_TPF)
12106 /* Don't emit DWARF3/4 unless specifically selected. The TPF
12107 debuggers do not yet support DWARF 3/4. */
12108 if (!global_options_set.x_dwarf_strict)
12109 dwarf_strict = 1;
12110 if (!global_options_set.x_dwarf_version)
12111 dwarf_version = 2;
12114 /* Register a target-specific optimization-and-lowering pass
12115 to run immediately before prologue and epilogue generation.
12117 Registering the pass must be done at start up. It's
12118 convenient to do it here. */
12119 opt_pass *new_pass = new pass_s390_early_mach (g);
12120 struct register_pass_info insert_pass_s390_early_mach =
12122 new_pass, /* pass */
12123 "pro_and_epilogue", /* reference_pass_name */
12124 1, /* ref_pass_instance_number */
12125 PASS_POS_INSERT_BEFORE /* po_op */
12127 register_pass (&insert_pass_s390_early_mach);
12130 /* Implement TARGET_USE_BY_PIECES_INFRASTRUCTURE_P. */
12132 static bool
12133 s390_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size,
12134 unsigned int align ATTRIBUTE_UNUSED,
12135 enum by_pieces_operation op ATTRIBUTE_UNUSED,
12136 bool speed_p ATTRIBUTE_UNUSED)
12138 return (size == 1 || size == 2
12139 || size == 4 || (TARGET_ZARCH && size == 8));
12142 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV hook. */
12144 static void
12145 s390_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
12147 tree sfpc = s390_builtin_decls[S390_BUILTIN_S390_SFPC];
12148 tree efpc = s390_builtin_decls[S390_BUILTIN_S390_EFPC];
12149 tree call_efpc = build_call_expr (efpc, 0);
12150 tree fenv_var = create_tmp_var (unsigned_type_node);
12152 #define FPC_EXCEPTION_MASK HOST_WIDE_INT_UC (0xf8000000)
12153 #define FPC_FLAGS_MASK HOST_WIDE_INT_UC (0x00f80000)
12154 #define FPC_DXC_MASK HOST_WIDE_INT_UC (0x0000ff00)
12155 #define FPC_EXCEPTION_MASK_SHIFT HOST_WIDE_INT_UC (24)
12156 #define FPC_FLAGS_SHIFT HOST_WIDE_INT_UC (16)
12157 #define FPC_DXC_SHIFT HOST_WIDE_INT_UC (8)
12159 /* Generates the equivalent of feholdexcept (&fenv_var)
12161 fenv_var = __builtin_s390_efpc ();
12162 __builtin_s390_sfpc (fenv_var & mask) */
12163 tree old_fpc = build2 (MODIFY_EXPR, unsigned_type_node, fenv_var, call_efpc);
12164 tree new_fpc =
12165 build2 (BIT_AND_EXPR, unsigned_type_node, fenv_var,
12166 build_int_cst (unsigned_type_node,
12167 ~(FPC_DXC_MASK | FPC_FLAGS_MASK |
12168 FPC_EXCEPTION_MASK)));
12169 tree set_new_fpc = build_call_expr (sfpc, 1, new_fpc);
12170 *hold = build2 (COMPOUND_EXPR, void_type_node, old_fpc, set_new_fpc);
12172 /* Generates the equivalent of feclearexcept (FE_ALL_EXCEPT)
12174 __builtin_s390_sfpc (__builtin_s390_efpc () & mask) */
12175 new_fpc = build2 (BIT_AND_EXPR, unsigned_type_node, call_efpc,
12176 build_int_cst (unsigned_type_node,
12177 ~(FPC_DXC_MASK | FPC_FLAGS_MASK)));
12178 *clear = build_call_expr (sfpc, 1, new_fpc);
12180 /* Generates the equivalent of feupdateenv (fenv_var)
12182 old_fpc = __builtin_s390_efpc ();
12183 __builtin_s390_sfpc (fenv_var);
12184 __atomic_feraiseexcept ((old_fpc & FPC_FLAGS_MASK) >> FPC_FLAGS_SHIFT); */
12186 old_fpc = create_tmp_var (unsigned_type_node);
12187 tree store_old_fpc = build2 (MODIFY_EXPR, void_type_node,
12188 old_fpc, call_efpc);
12190 set_new_fpc = build_call_expr (sfpc, 1, fenv_var);
12192 tree raise_old_except = build2 (BIT_AND_EXPR, unsigned_type_node, old_fpc,
12193 build_int_cst (unsigned_type_node,
12194 FPC_FLAGS_MASK));
12195 raise_old_except = build2 (RSHIFT_EXPR, unsigned_type_node, raise_old_except,
12196 build_int_cst (unsigned_type_node,
12197 FPC_FLAGS_SHIFT));
12198 tree atomic_feraiseexcept
12199 = builtin_decl_implicit (BUILT_IN_ATOMIC_FERAISEEXCEPT);
12200 raise_old_except = build_call_expr (atomic_feraiseexcept,
12201 1, raise_old_except);
12203 *update = build2 (COMPOUND_EXPR, void_type_node,
12204 build2 (COMPOUND_EXPR, void_type_node,
12205 store_old_fpc, set_new_fpc),
12206 raise_old_except);
12208 #undef FPC_EXCEPTION_MASK
12209 #undef FPC_FLAGS_MASK
12210 #undef FPC_DXC_MASK
12211 #undef FPC_EXCEPTION_MASK_SHIFT
12212 #undef FPC_FLAGS_SHIFT
12213 #undef FPC_DXC_SHIFT
12216 /* Initialize GCC target structure. */
12218 #undef TARGET_ASM_ALIGNED_HI_OP
12219 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
12220 #undef TARGET_ASM_ALIGNED_DI_OP
12221 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
12222 #undef TARGET_ASM_INTEGER
12223 #define TARGET_ASM_INTEGER s390_assemble_integer
12225 #undef TARGET_ASM_OPEN_PAREN
12226 #define TARGET_ASM_OPEN_PAREN ""
12228 #undef TARGET_ASM_CLOSE_PAREN
12229 #define TARGET_ASM_CLOSE_PAREN ""
12231 #undef TARGET_OPTION_OVERRIDE
12232 #define TARGET_OPTION_OVERRIDE s390_option_override
12234 #undef TARGET_ENCODE_SECTION_INFO
12235 #define TARGET_ENCODE_SECTION_INFO s390_encode_section_info
12237 #undef TARGET_SCALAR_MODE_SUPPORTED_P
12238 #define TARGET_SCALAR_MODE_SUPPORTED_P s390_scalar_mode_supported_p
12240 #ifdef HAVE_AS_TLS
12241 #undef TARGET_HAVE_TLS
12242 #define TARGET_HAVE_TLS true
12243 #endif
12244 #undef TARGET_CANNOT_FORCE_CONST_MEM
12245 #define TARGET_CANNOT_FORCE_CONST_MEM s390_cannot_force_const_mem
12247 #undef TARGET_DELEGITIMIZE_ADDRESS
12248 #define TARGET_DELEGITIMIZE_ADDRESS s390_delegitimize_address
12250 #undef TARGET_LEGITIMIZE_ADDRESS
12251 #define TARGET_LEGITIMIZE_ADDRESS s390_legitimize_address
12253 #undef TARGET_RETURN_IN_MEMORY
12254 #define TARGET_RETURN_IN_MEMORY s390_return_in_memory
12256 #undef TARGET_INIT_BUILTINS
12257 #define TARGET_INIT_BUILTINS s390_init_builtins
12258 #undef TARGET_EXPAND_BUILTIN
12259 #define TARGET_EXPAND_BUILTIN s390_expand_builtin
12260 #undef TARGET_BUILTIN_DECL
12261 #define TARGET_BUILTIN_DECL s390_builtin_decl
12263 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
12264 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA s390_output_addr_const_extra
12266 #undef TARGET_ASM_OUTPUT_MI_THUNK
12267 #define TARGET_ASM_OUTPUT_MI_THUNK s390_output_mi_thunk
12268 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
12269 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
12271 #undef TARGET_SCHED_ADJUST_PRIORITY
12272 #define TARGET_SCHED_ADJUST_PRIORITY s390_adjust_priority
12273 #undef TARGET_SCHED_ISSUE_RATE
12274 #define TARGET_SCHED_ISSUE_RATE s390_issue_rate
12275 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
12276 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD s390_first_cycle_multipass_dfa_lookahead
12278 #undef TARGET_SCHED_VARIABLE_ISSUE
12279 #define TARGET_SCHED_VARIABLE_ISSUE s390_sched_variable_issue
12280 #undef TARGET_SCHED_REORDER
12281 #define TARGET_SCHED_REORDER s390_sched_reorder
12282 #undef TARGET_SCHED_INIT
12283 #define TARGET_SCHED_INIT s390_sched_init
12285 #undef TARGET_CANNOT_COPY_INSN_P
12286 #define TARGET_CANNOT_COPY_INSN_P s390_cannot_copy_insn_p
12287 #undef TARGET_RTX_COSTS
12288 #define TARGET_RTX_COSTS s390_rtx_costs
12289 #undef TARGET_ADDRESS_COST
12290 #define TARGET_ADDRESS_COST s390_address_cost
12291 #undef TARGET_REGISTER_MOVE_COST
12292 #define TARGET_REGISTER_MOVE_COST s390_register_move_cost
12293 #undef TARGET_MEMORY_MOVE_COST
12294 #define TARGET_MEMORY_MOVE_COST s390_memory_move_cost
12296 #undef TARGET_MACHINE_DEPENDENT_REORG
12297 #define TARGET_MACHINE_DEPENDENT_REORG s390_reorg
12299 #undef TARGET_VALID_POINTER_MODE
12300 #define TARGET_VALID_POINTER_MODE s390_valid_pointer_mode
12302 #undef TARGET_BUILD_BUILTIN_VA_LIST
12303 #define TARGET_BUILD_BUILTIN_VA_LIST s390_build_builtin_va_list
12304 #undef TARGET_EXPAND_BUILTIN_VA_START
12305 #define TARGET_EXPAND_BUILTIN_VA_START s390_va_start
12306 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
12307 #define TARGET_GIMPLIFY_VA_ARG_EXPR s390_gimplify_va_arg
12309 #undef TARGET_PROMOTE_FUNCTION_MODE
12310 #define TARGET_PROMOTE_FUNCTION_MODE s390_promote_function_mode
12311 #undef TARGET_PASS_BY_REFERENCE
12312 #define TARGET_PASS_BY_REFERENCE s390_pass_by_reference
12314 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
12315 #define TARGET_FUNCTION_OK_FOR_SIBCALL s390_function_ok_for_sibcall
12316 #undef TARGET_FUNCTION_ARG
12317 #define TARGET_FUNCTION_ARG s390_function_arg
12318 #undef TARGET_FUNCTION_ARG_ADVANCE
12319 #define TARGET_FUNCTION_ARG_ADVANCE s390_function_arg_advance
12320 #undef TARGET_FUNCTION_VALUE
12321 #define TARGET_FUNCTION_VALUE s390_function_value
12322 #undef TARGET_LIBCALL_VALUE
12323 #define TARGET_LIBCALL_VALUE s390_libcall_value
12325 #undef TARGET_KEEP_LEAF_WHEN_PROFILED
12326 #define TARGET_KEEP_LEAF_WHEN_PROFILED s390_keep_leaf_when_profiled
12328 #undef TARGET_FIXED_CONDITION_CODE_REGS
12329 #define TARGET_FIXED_CONDITION_CODE_REGS s390_fixed_condition_code_regs
12331 #undef TARGET_CC_MODES_COMPATIBLE
12332 #define TARGET_CC_MODES_COMPATIBLE s390_cc_modes_compatible
12334 #undef TARGET_INVALID_WITHIN_DOLOOP
12335 #define TARGET_INVALID_WITHIN_DOLOOP hook_constcharptr_const_rtx_insn_null
12337 #ifdef HAVE_AS_TLS
12338 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
12339 #define TARGET_ASM_OUTPUT_DWARF_DTPREL s390_output_dwarf_dtprel
12340 #endif
12342 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
12343 #undef TARGET_MANGLE_TYPE
12344 #define TARGET_MANGLE_TYPE s390_mangle_type
12345 #endif
12347 #undef TARGET_SCALAR_MODE_SUPPORTED_P
12348 #define TARGET_SCALAR_MODE_SUPPORTED_P s390_scalar_mode_supported_p
12350 #undef TARGET_PREFERRED_RELOAD_CLASS
12351 #define TARGET_PREFERRED_RELOAD_CLASS s390_preferred_reload_class
12353 #undef TARGET_SECONDARY_RELOAD
12354 #define TARGET_SECONDARY_RELOAD s390_secondary_reload
12356 #undef TARGET_LIBGCC_CMP_RETURN_MODE
12357 #define TARGET_LIBGCC_CMP_RETURN_MODE s390_libgcc_cmp_return_mode
12359 #undef TARGET_LIBGCC_SHIFT_COUNT_MODE
12360 #define TARGET_LIBGCC_SHIFT_COUNT_MODE s390_libgcc_shift_count_mode
12362 #undef TARGET_LEGITIMATE_ADDRESS_P
12363 #define TARGET_LEGITIMATE_ADDRESS_P s390_legitimate_address_p
12365 #undef TARGET_LEGITIMATE_CONSTANT_P
12366 #define TARGET_LEGITIMATE_CONSTANT_P s390_legitimate_constant_p
12368 #undef TARGET_LRA_P
12369 #define TARGET_LRA_P s390_lra_p
12371 #undef TARGET_CAN_ELIMINATE
12372 #define TARGET_CAN_ELIMINATE s390_can_eliminate
12374 #undef TARGET_CONDITIONAL_REGISTER_USAGE
12375 #define TARGET_CONDITIONAL_REGISTER_USAGE s390_conditional_register_usage
12377 #undef TARGET_LOOP_UNROLL_ADJUST
12378 #define TARGET_LOOP_UNROLL_ADJUST s390_loop_unroll_adjust
12380 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
12381 #define TARGET_ASM_TRAMPOLINE_TEMPLATE s390_asm_trampoline_template
12382 #undef TARGET_TRAMPOLINE_INIT
12383 #define TARGET_TRAMPOLINE_INIT s390_trampoline_init
12385 #undef TARGET_UNWIND_WORD_MODE
12386 #define TARGET_UNWIND_WORD_MODE s390_unwind_word_mode
12388 #undef TARGET_CANONICALIZE_COMPARISON
12389 #define TARGET_CANONICALIZE_COMPARISON s390_canonicalize_comparison
12391 #undef TARGET_HARD_REGNO_SCRATCH_OK
12392 #define TARGET_HARD_REGNO_SCRATCH_OK s390_hard_regno_scratch_ok
12394 #undef TARGET_ATTRIBUTE_TABLE
12395 #define TARGET_ATTRIBUTE_TABLE s390_attribute_table
12397 #undef TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P
12398 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P hook_bool_const_tree_true
12400 #undef TARGET_SET_UP_BY_PROLOGUE
12401 #define TARGET_SET_UP_BY_PROLOGUE s300_set_up_by_prologue
12403 #undef TARGET_USE_BY_PIECES_INFRASTRUCTURE_P
12404 #define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \
12405 s390_use_by_pieces_infrastructure_p
12407 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
12408 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV s390_atomic_assign_expand_fenv
12410 struct gcc_target targetm = TARGET_INITIALIZER;
12412 #include "gt-s390.h"