OSDN Git Service

* reload.c (find_reloads): When reloading a PLUS with constant
[pf3gnuchains/gcc-fork.git] / gcc / config / s390 / s390.c
1 /* Subroutines used for code generation on IBM S/390 and zSeries
2    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
3    Free Software Foundation, Inc.
4    Contributed by Hartmut Penner (hpenner@de.ibm.com) and
5                   Ulrich Weigand (uweigand@de.ibm.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING.  If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA.  */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "tm_p.h"
31 #include "regs.h"
32 #include "hard-reg-set.h"
33 #include "real.h"
34 #include "insn-config.h"
35 #include "conditions.h"
36 #include "output.h"
37 #include "insn-attr.h"
38 #include "flags.h"
39 #include "except.h"
40 #include "function.h"
41 #include "recog.h"
42 #include "expr.h"
43 #include "reload.h"
44 #include "toplev.h"
45 #include "basic-block.h"
46 #include "integrate.h"
47 #include "ggc.h"
48 #include "target.h"
49 #include "target-def.h"
50 #include "debug.h"
51 #include "langhooks.h"
52 #include "optabs.h"
53 #include "tree-gimple.h"
54
55 /* Machine-specific symbol_ref flags.  */
56 #define SYMBOL_FLAG_ALIGN1      (SYMBOL_FLAG_MACH_DEP << 0)
57
58
59 static bool s390_assemble_integer (rtx, unsigned int, int);
60 static void s390_encode_section_info (tree, rtx, int);
61 static bool s390_cannot_force_const_mem (rtx);
62 static rtx s390_delegitimize_address (rtx);
63 static bool s390_return_in_memory (tree, tree);
64 static void s390_init_builtins (void);
65 static rtx s390_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
66 static void s390_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
67                                   HOST_WIDE_INT, tree);
68 static enum attr_type s390_safe_attr_type (rtx);
69
70 static int s390_adjust_priority (rtx, int);
71 static int s390_issue_rate (void);
72 static int s390_first_cycle_multipass_dfa_lookahead (void);
73 static bool s390_cannot_copy_insn_p (rtx);
74 static bool s390_rtx_costs (rtx, int, int, int *);
75 static int s390_address_cost (rtx);
76 static void s390_reorg (void);
77 static bool s390_valid_pointer_mode (enum machine_mode);
78 static tree s390_build_builtin_va_list (void);
79 static tree s390_gimplify_va_arg (tree, tree, tree *, tree *);
80 static bool s390_function_ok_for_sibcall (tree, tree);
81 static bool s390_call_saved_register_used (tree);
82 static bool s390_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode mode,
83                                     tree, bool);
84
85 #undef  TARGET_ASM_ALIGNED_HI_OP
86 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
87 #undef  TARGET_ASM_ALIGNED_DI_OP
88 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
89 #undef  TARGET_ASM_INTEGER
90 #define TARGET_ASM_INTEGER s390_assemble_integer
91
92 #undef  TARGET_ASM_OPEN_PAREN
93 #define TARGET_ASM_OPEN_PAREN ""
94
95 #undef  TARGET_ASM_CLOSE_PAREN
96 #define TARGET_ASM_CLOSE_PAREN ""
97
98 #undef  TARGET_ENCODE_SECTION_INFO
99 #define TARGET_ENCODE_SECTION_INFO s390_encode_section_info
100
101 #ifdef HAVE_AS_TLS
102 #undef TARGET_HAVE_TLS
103 #define TARGET_HAVE_TLS true
104 #endif
105 #undef TARGET_CANNOT_FORCE_CONST_MEM
106 #define TARGET_CANNOT_FORCE_CONST_MEM s390_cannot_force_const_mem
107
108 #undef TARGET_DELEGITIMIZE_ADDRESS
109 #define TARGET_DELEGITIMIZE_ADDRESS s390_delegitimize_address
110
111 #undef TARGET_RETURN_IN_MEMORY
112 #define TARGET_RETURN_IN_MEMORY s390_return_in_memory
113
114 #undef  TARGET_INIT_BUILTINS
115 #define TARGET_INIT_BUILTINS s390_init_builtins
116 #undef  TARGET_EXPAND_BUILTIN
117 #define TARGET_EXPAND_BUILTIN s390_expand_builtin
118
119 #undef TARGET_ASM_OUTPUT_MI_THUNK
120 #define TARGET_ASM_OUTPUT_MI_THUNK s390_output_mi_thunk
121 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
122 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
123
124 #undef  TARGET_SCHED_ADJUST_PRIORITY
125 #define TARGET_SCHED_ADJUST_PRIORITY s390_adjust_priority
126 #undef TARGET_SCHED_ISSUE_RATE
127 #define TARGET_SCHED_ISSUE_RATE s390_issue_rate
128 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
129 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD s390_first_cycle_multipass_dfa_lookahead
130
131 #undef TARGET_CANNOT_COPY_INSN_P
132 #define TARGET_CANNOT_COPY_INSN_P s390_cannot_copy_insn_p
133 #undef TARGET_RTX_COSTS
134 #define TARGET_RTX_COSTS s390_rtx_costs
135 #undef TARGET_ADDRESS_COST
136 #define TARGET_ADDRESS_COST s390_address_cost
137
138 #undef TARGET_MACHINE_DEPENDENT_REORG
139 #define TARGET_MACHINE_DEPENDENT_REORG s390_reorg
140
141 #undef TARGET_VALID_POINTER_MODE
142 #define TARGET_VALID_POINTER_MODE s390_valid_pointer_mode
143
144 #undef TARGET_BUILD_BUILTIN_VA_LIST
145 #define TARGET_BUILD_BUILTIN_VA_LIST s390_build_builtin_va_list
146 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
147 #define TARGET_GIMPLIFY_VA_ARG_EXPR s390_gimplify_va_arg
148
149 #undef TARGET_PROMOTE_FUNCTION_ARGS
150 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
151 #undef TARGET_PROMOTE_FUNCTION_RETURN
152 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
153 #undef TARGET_PASS_BY_REFERENCE
154 #define TARGET_PASS_BY_REFERENCE s390_pass_by_reference
155
156 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
157 #define TARGET_FUNCTION_OK_FOR_SIBCALL s390_function_ok_for_sibcall
158
159 struct gcc_target targetm = TARGET_INITIALIZER;
160
161 extern int reload_completed;
162
163 /* The alias set for prologue/epilogue register save/restore.  */
164 static int s390_sr_alias_set = 0;
165
166 /* Save information from a "cmpxx" operation until the branch or scc is
167    emitted.  */
168 rtx s390_compare_op0, s390_compare_op1;
169
170 /* Structure used to hold the components of a S/390 memory
171    address.  A legitimate address on S/390 is of the general
172    form
173           base + index + displacement
174    where any of the components is optional.
175
176    base and index are registers of the class ADDR_REGS,
177    displacement is an unsigned 12-bit immediate constant.  */
178
179 struct s390_address
180 {
181   rtx base;
182   rtx indx;
183   rtx disp;
184   int pointer;
185 };
186
187 /* Which cpu are we tuning for.  */
188 enum processor_type s390_tune;
189 enum processor_flags s390_tune_flags;
190 /* Which instruction set architecture to use.  */
191 enum processor_type s390_arch;
192 enum processor_flags s390_arch_flags;
193
194 /* Strings to hold which cpu and instruction set architecture  to use.  */
195 const char *s390_tune_string;           /* for -mtune=<xxx> */
196 const char *s390_arch_string;           /* for -march=<xxx> */
197
198 /* String to specify backchain mode: 
199    "" no-backchain, "1" backchain, "2" kernel-backchain.  */
200 const char *s390_backchain_string = TARGET_DEFAULT_BACKCHAIN;
201
202 const char *s390_warn_framesize_string;
203 const char *s390_warn_dynamicstack_string;
204 const char *s390_stack_size_string;
205 const char *s390_stack_guard_string;
206
207 HOST_WIDE_INT s390_warn_framesize = 0;
208 bool s390_warn_dynamicstack_p = 0;
209 HOST_WIDE_INT s390_stack_size = 0;
210 HOST_WIDE_INT s390_stack_guard = 0;
211
212 /* The following structure is embedded in the machine 
213    specific part of struct function.  */
214
215 struct s390_frame_layout GTY (())
216 {
217   /* Offset within stack frame.  */
218   HOST_WIDE_INT gprs_offset;
219   HOST_WIDE_INT f0_offset;
220   HOST_WIDE_INT f4_offset;
221   HOST_WIDE_INT f8_offset;
222   HOST_WIDE_INT backchain_offset;
223   
224   /* Number of first and last gpr to be saved, restored.  */
225   int first_save_gpr;
226   int first_restore_gpr;
227   int last_save_gpr;
228   int last_restore_gpr;
229
230   /* Bits standing for floating point registers. Set, if the 
231      respective register has to be saved. Starting with reg 16 (f0) 
232      at the rightmost bit.
233      Bit 15 -  8  7  6  5  4  3  2  1  0
234      fpr 15 -  8  7  5  3  1  6  4  2  0
235      reg 31 - 24 23 22 21 20 19 18 17 16  */
236   unsigned int fpr_bitmap;
237
238   /* Number of floating point registers f8-f15 which must be saved.  */
239   int high_fprs;
240
241   /* Set if return address needs to be saved.  */
242   bool save_return_addr_p;
243
244   /* Set if backchain needs to be saved.  */
245   bool save_backchain_p;
246
247   /* Size of stack frame.  */
248   HOST_WIDE_INT frame_size;
249 };
250
251 /* Define the structure for the machine field in struct function.  */
252
253 struct machine_function GTY(())
254 {
255   struct s390_frame_layout frame_layout;
256
257   /* Literal pool base register.  */
258   rtx base_reg;
259
260   /* True if we may need to perform branch splitting.  */
261   bool split_branches_pending_p;
262
263   /* Some local-dynamic TLS symbol name.  */
264   const char *some_ld_name;
265 };
266
267 /* Few accessor macros for struct cfun->machine->s390_frame_layout.  */
268
269 #define cfun_frame_layout (cfun->machine->frame_layout)
270 #define cfun_save_high_fprs_p (!!cfun_frame_layout.high_fprs)
271 #define cfun_gprs_save_area_size ((cfun_frame_layout.last_save_gpr -           \
272   cfun_frame_layout.first_save_gpr + 1) * UNITS_PER_WORD)
273 #define cfun_set_fpr_bit(BITNUM) (cfun->machine->frame_layout.fpr_bitmap |=    \
274   (1 << (BITNUM)))
275 #define cfun_fpr_bit_p(BITNUM) (!!(cfun->machine->frame_layout.fpr_bitmap &    \
276   (1 << (BITNUM))))
277
278 static int s390_match_ccmode_set (rtx, enum machine_mode);
279 static int s390_branch_condition_mask (rtx);
280 static const char *s390_branch_condition_mnemonic (rtx, int);
281 static int check_mode (rtx, enum machine_mode *);
282 static int s390_short_displacement (rtx);
283 static int s390_decompose_address (rtx, struct s390_address *);
284 static rtx get_thread_pointer (void);
285 static rtx legitimize_tls_address (rtx, rtx);
286 static void print_shift_count_operand (FILE *, rtx);
287 static const char *get_some_local_dynamic_name (void);
288 static int get_some_local_dynamic_name_1 (rtx *, void *);
289 static int reg_used_in_mem_p (int, rtx);
290 static int addr_generation_dependency_p (rtx, rtx);
291 static int s390_split_branches (void);
292 static void annotate_constant_pool_refs (rtx *x);
293 static void find_constant_pool_ref (rtx, rtx *);
294 static void replace_constant_pool_ref (rtx *, rtx, rtx);
295 static rtx find_ltrel_base (rtx);
296 static void replace_ltrel_base (rtx *);
297 static void s390_optimize_prologue (void);
298 static int find_unused_clobbered_reg (void);
299 static void s390_frame_area (int *, int *);
300 static void s390_register_info (int []);
301 static void s390_frame_info (void);
302 static void s390_init_frame_layout (void);
303 static void s390_update_frame_layout (void);
304 static rtx save_fpr (rtx, int, int);
305 static rtx restore_fpr (rtx, int, int);
306 static rtx save_gprs (rtx, int, int, int);
307 static rtx restore_gprs (rtx, int, int, int);
308 static int s390_function_arg_size (enum machine_mode, tree);
309 static bool s390_function_arg_float (enum machine_mode, tree);
310 static struct machine_function * s390_init_machine_status (void);
311
312 /* Check whether integer displacement is in range.  */
313 #define DISP_IN_RANGE(d) \
314   (TARGET_LONG_DISPLACEMENT? ((d) >= -524288 && (d) <= 524287) \
315                            : ((d) >= 0 && (d) <= 4095))
316
317 /* Return true if SET either doesn't set the CC register, or else
318    the source and destination have matching CC modes and that
319    CC mode is at least as constrained as REQ_MODE.  */
320
321 static int
322 s390_match_ccmode_set (rtx set, enum machine_mode req_mode)
323 {
324   enum machine_mode set_mode;
325
326   if (GET_CODE (set) != SET)
327     abort ();
328
329   if (GET_CODE (SET_DEST (set)) != REG || !CC_REGNO_P (REGNO (SET_DEST (set))))
330     return 1;
331
332   set_mode = GET_MODE (SET_DEST (set));
333   switch (set_mode)
334     {
335     case CCSmode:
336     case CCSRmode:
337     case CCUmode:
338     case CCURmode:
339     case CCLmode:
340     case CCL1mode:
341     case CCL2mode:
342     case CCL3mode:
343     case CCT1mode:
344     case CCT2mode:
345     case CCT3mode:
346       if (req_mode != set_mode)
347         return 0;
348       break;
349
350     case CCZmode:
351       if (req_mode != CCSmode && req_mode != CCUmode && req_mode != CCTmode
352           && req_mode != CCSRmode && req_mode != CCURmode)
353         return 0;
354       break;
355
356     case CCAPmode:
357     case CCANmode:
358       if (req_mode != CCAmode)
359         return 0;
360       break;
361
362     default:
363       abort ();
364     }
365
366   return (GET_MODE (SET_SRC (set)) == set_mode);
367 }
368
369 /* Return true if every SET in INSN that sets the CC register
370    has source and destination with matching CC modes and that
371    CC mode is at least as constrained as REQ_MODE.
372    If REQ_MODE is VOIDmode, always return false.  */
373
374 int
375 s390_match_ccmode (rtx insn, enum machine_mode req_mode)
376 {
377   int i;
378
379   /* s390_tm_ccmode returns VOIDmode to indicate failure.  */
380   if (req_mode == VOIDmode)
381     return 0;
382
383   if (GET_CODE (PATTERN (insn)) == SET)
384     return s390_match_ccmode_set (PATTERN (insn), req_mode);
385
386   if (GET_CODE (PATTERN (insn)) == PARALLEL)
387       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
388         {
389           rtx set = XVECEXP (PATTERN (insn), 0, i);
390           if (GET_CODE (set) == SET)
391             if (!s390_match_ccmode_set (set, req_mode))
392               return 0;
393         }
394
395   return 1;
396 }
397
398 /* If a test-under-mask instruction can be used to implement
399    (compare (and ... OP1) OP2), return the CC mode required
400    to do that.  Otherwise, return VOIDmode.
401    MIXED is true if the instruction can distinguish between
402    CC1 and CC2 for mixed selected bits (TMxx), it is false
403    if the instruction cannot (TM).  */
404
405 enum machine_mode
406 s390_tm_ccmode (rtx op1, rtx op2, int mixed)
407 {
408   int bit0, bit1;
409
410   /* ??? Fixme: should work on CONST_DOUBLE as well.  */
411   if (GET_CODE (op1) != CONST_INT || GET_CODE (op2) != CONST_INT)
412     return VOIDmode;
413
414   /* Selected bits all zero: CC0.  */
415   if (INTVAL (op2) == 0)
416     return CCTmode;
417
418   /* Selected bits all one: CC3.  */
419   if (INTVAL (op2) == INTVAL (op1))
420     return CCT3mode;
421
422   /* Exactly two bits selected, mixed zeroes and ones: CC1 or CC2.  */
423   if (mixed)
424     {
425       bit1 = exact_log2 (INTVAL (op2));
426       bit0 = exact_log2 (INTVAL (op1) ^ INTVAL (op2));
427       if (bit0 != -1 && bit1 != -1)
428         return bit0 > bit1 ? CCT1mode : CCT2mode;
429     }
430
431   return VOIDmode;
432 }
433
434 /* Given a comparison code OP (EQ, NE, etc.) and the operands
435    OP0 and OP1 of a COMPARE, return the mode to be used for the
436    comparison.  */
437
438 enum machine_mode
439 s390_select_ccmode (enum rtx_code code, rtx op0, rtx op1)
440 {
441   switch (code)
442     {
443       case EQ:
444       case NE:
445         if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
446             && CONST_OK_FOR_CONSTRAINT_P (INTVAL (XEXP (op0, 1)), 'K', "K"))
447           return CCAPmode;
448         if ((GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
449              || GET_CODE (op1) == NEG)
450             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
451           return CCLmode;
452
453         if (GET_CODE (op0) == AND)
454           {
455             /* Check whether we can potentially do it via TM.  */
456             enum machine_mode ccmode;
457             ccmode = s390_tm_ccmode (XEXP (op0, 1), op1, 1);
458             if (ccmode != VOIDmode)
459               {
460                 /* Relax CCTmode to CCZmode to allow fall-back to AND
461                    if that turns out to be beneficial.  */
462                 return ccmode == CCTmode ? CCZmode : ccmode;
463               }
464           }
465
466         if (register_operand (op0, HImode)
467             && GET_CODE (op1) == CONST_INT
468             && (INTVAL (op1) == -1 || INTVAL (op1) == 65535))
469           return CCT3mode;
470         if (register_operand (op0, QImode)
471             && GET_CODE (op1) == CONST_INT
472             && (INTVAL (op1) == -1 || INTVAL (op1) == 255))
473           return CCT3mode;
474
475         return CCZmode;
476
477       case LE:
478       case LT:
479       case GE:
480       case GT:
481           if (GET_CODE (op0) == PLUS && GET_CODE (XEXP (op0, 1)) == CONST_INT
482               && CONST_OK_FOR_CONSTRAINT_P (INTVAL (XEXP (op0, 1)), 'K', "K"))
483             {
484               if (INTVAL (XEXP((op0), 1)) < 0)
485                 return CCANmode;
486               else
487                 return CCAPmode;
488             }
489       case UNORDERED:
490       case ORDERED:
491       case UNEQ:
492       case UNLE:
493       case UNLT:
494       case UNGE:
495       case UNGT:
496       case LTGT:
497         if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
498             && GET_CODE (op1) != CONST_INT)
499           return CCSRmode;
500         return CCSmode;
501
502       case LTU:
503       case GEU:
504         if (GET_CODE (op0) == PLUS
505             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
506           return CCL1mode;
507
508         if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
509             && GET_CODE (op1) != CONST_INT)
510           return CCURmode;
511         return CCUmode;
512
513       case LEU:
514       case GTU:
515         if (GET_CODE (op0) == MINUS
516             && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
517           return CCL2mode;
518
519         if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
520             && GET_CODE (op1) != CONST_INT)
521           return CCURmode;
522         return CCUmode;
523
524       default:
525         abort ();
526     }
527 }
528
529 /* Replace the comparison OP0 CODE OP1 by a semantically equivalent one
530    that we can implement more efficiently.  */
531
532 void
533 s390_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1)
534 {
535   /* Convert ZERO_EXTRACT back to AND to enable TM patterns.  */
536   if ((*code == EQ || *code == NE)
537       && *op1 == const0_rtx
538       && GET_CODE (*op0) == ZERO_EXTRACT
539       && GET_CODE (XEXP (*op0, 1)) == CONST_INT
540       && GET_CODE (XEXP (*op0, 2)) == CONST_INT
541       && SCALAR_INT_MODE_P (GET_MODE (XEXP (*op0, 0))))
542     {
543       rtx inner = XEXP (*op0, 0);
544       HOST_WIDE_INT modesize = GET_MODE_BITSIZE (GET_MODE (inner));
545       HOST_WIDE_INT len = INTVAL (XEXP (*op0, 1));
546       HOST_WIDE_INT pos = INTVAL (XEXP (*op0, 2));
547
548       if (len > 0 && len < modesize
549           && pos >= 0 && pos + len <= modesize
550           && modesize <= HOST_BITS_PER_WIDE_INT)
551         {
552           unsigned HOST_WIDE_INT block;
553           block = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
554           block <<= modesize - pos - len;
555
556           *op0 = gen_rtx_AND (GET_MODE (inner), inner,
557                               gen_int_mode (block, GET_MODE (inner)));
558         }
559     }
560
561   /* Narrow AND of memory against immediate to enable TM.  */
562   if ((*code == EQ || *code == NE)
563       && *op1 == const0_rtx
564       && GET_CODE (*op0) == AND
565       && GET_CODE (XEXP (*op0, 1)) == CONST_INT
566       && SCALAR_INT_MODE_P (GET_MODE (XEXP (*op0, 0))))
567     {
568       rtx inner = XEXP (*op0, 0);
569       rtx mask = XEXP (*op0, 1);
570
571       /* Ignore paradoxical SUBREGs if all extra bits are masked out.  */
572       if (GET_CODE (inner) == SUBREG
573           && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (inner)))
574           && (GET_MODE_SIZE (GET_MODE (inner))
575               >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner))))
576           && ((INTVAL (mask)
577                & GET_MODE_MASK (GET_MODE (inner))
578                & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (inner))))
579               == 0))
580         inner = SUBREG_REG (inner);
581
582       /* Do not change volatile MEMs.  */
583       if (MEM_P (inner) && !MEM_VOLATILE_P (inner))
584         {
585           int part = s390_single_part (XEXP (*op0, 1),
586                                        GET_MODE (inner), QImode, 0);
587           if (part >= 0)
588             {
589               mask = gen_int_mode (s390_extract_part (mask, QImode, 0), QImode);
590               inner = adjust_address_nv (inner, QImode, part);
591               *op0 = gen_rtx_AND (QImode, inner, mask);
592             }
593         }
594     }
595
596   /* Narrow comparisons against 0xffff to HImode if possible.  */
597   if ((*code == EQ || *code == NE)
598       && GET_CODE (*op1) == CONST_INT
599       && INTVAL (*op1) == 0xffff
600       && SCALAR_INT_MODE_P (GET_MODE (*op0))
601       && (nonzero_bits (*op0, GET_MODE (*op0)) 
602           & ~(unsigned HOST_WIDE_INT) 0xffff) == 0)
603     {
604       *op0 = gen_lowpart (HImode, *op0);
605       *op1 = constm1_rtx;
606     }
607
608
609   /* Remove redundant UNSPEC_CMPINT conversions if possible.  */
610   if (GET_CODE (*op0) == UNSPEC
611       && XINT (*op0, 1) == UNSPEC_CMPINT
612       && XVECLEN (*op0, 0) == 1
613       && GET_MODE (XVECEXP (*op0, 0, 0)) == CCUmode
614       && GET_CODE (XVECEXP (*op0, 0, 0)) == REG
615       && REGNO (XVECEXP (*op0, 0, 0)) == CC_REGNUM
616       && *op1 == const0_rtx)
617     {
618       enum rtx_code new_code = UNKNOWN;
619       switch (*code)
620         {
621           case EQ: new_code = EQ;  break;
622           case NE: new_code = NE;  break;
623           case LT: new_code = LTU; break;
624           case GT: new_code = GTU; break;
625           case LE: new_code = LEU; break;
626           case GE: new_code = GEU; break;
627           default: break;
628         }
629
630       if (new_code != UNKNOWN)
631         {
632           *op0 = XVECEXP (*op0, 0, 0);
633           *code = new_code;
634         }
635     }
636 }
637
638 /* Emit a compare instruction suitable to implement the comparison
639    OP0 CODE OP1.  Return the correct condition RTL to be placed in
640    the IF_THEN_ELSE of the conditional branch testing the result.  */
641
642 rtx
643 s390_emit_compare (enum rtx_code code, rtx op0, rtx op1)
644 {
645   enum machine_mode mode = s390_select_ccmode (code, op0, op1);
646   rtx cc = gen_rtx_REG (mode, CC_REGNUM);
647
648   emit_insn (gen_rtx_SET (VOIDmode, cc, gen_rtx_COMPARE (mode, op0, op1)));
649   return gen_rtx_fmt_ee (code, VOIDmode, cc, const0_rtx);
650 }
651
652 /* Emit a jump instruction to TARGET.  If COND is NULL_RTX, emit an
653    unconditional jump, else a conditional jump under condition COND.  */
654
655 void
656 s390_emit_jump (rtx target, rtx cond)
657 {
658   rtx insn;
659
660   target = gen_rtx_LABEL_REF (VOIDmode, target);
661   if (cond)
662     target = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, target, pc_rtx);
663
664   insn = gen_rtx_SET (VOIDmode, pc_rtx, target);
665   emit_jump_insn (insn);
666 }
667
668 /* Return nonzero if OP is a valid comparison operator
669    for a branch condition in mode MODE.  */
670
671 int
672 s390_comparison (rtx op, enum machine_mode mode)
673 {
674   if (mode != VOIDmode && mode != GET_MODE (op))
675     return 0;
676
677   if (!COMPARISON_P (op))
678     return 0;
679
680   if (GET_CODE (XEXP (op, 0)) != REG
681       || REGNO (XEXP (op, 0)) != CC_REGNUM
682       || XEXP (op, 1) != const0_rtx)
683     return 0;
684
685   return s390_branch_condition_mask (op) >= 0;
686 }
687
688 /* Return nonzero if OP is a valid comparison operator
689    for an ALC condition in mode MODE.  */
690
691 int
692 s390_alc_comparison (rtx op, enum machine_mode mode)
693 {
694   if (mode != VOIDmode && mode != GET_MODE (op))
695     return 0;
696
697   while (GET_CODE (op) == ZERO_EXTEND || GET_CODE (op) == SIGN_EXTEND)
698     op = XEXP (op, 0);
699
700   if (!COMPARISON_P (op))
701     return 0;
702
703   if (GET_CODE (XEXP (op, 0)) != REG
704       || REGNO (XEXP (op, 0)) != CC_REGNUM
705       || XEXP (op, 1) != const0_rtx)
706     return 0;
707
708   switch (GET_MODE (XEXP (op, 0)))
709     {
710     case CCL1mode:
711       return GET_CODE (op) == LTU;
712
713     case CCL2mode:
714       return GET_CODE (op) == LEU;
715
716     case CCL3mode:
717       return GET_CODE (op) == GEU;
718
719     case CCUmode:
720       return GET_CODE (op) == GTU;
721
722     case CCURmode:
723       return GET_CODE (op) == LTU;
724
725     case CCSmode:
726       return GET_CODE (op) == UNGT;
727
728     case CCSRmode:
729       return GET_CODE (op) == UNLT;
730
731     default:
732       return 0;
733     }
734 }
735
736 /* Return nonzero if OP is a valid comparison operator
737    for an SLB condition in mode MODE.  */
738
739 int
740 s390_slb_comparison (rtx op, enum machine_mode mode)
741 {
742   if (mode != VOIDmode && mode != GET_MODE (op))
743     return 0;
744
745   while (GET_CODE (op) == ZERO_EXTEND || GET_CODE (op) == SIGN_EXTEND)
746     op = XEXP (op, 0);
747
748   if (!COMPARISON_P (op))
749     return 0;
750
751   if (GET_CODE (XEXP (op, 0)) != REG
752       || REGNO (XEXP (op, 0)) != CC_REGNUM
753       || XEXP (op, 1) != const0_rtx)
754     return 0;
755
756   switch (GET_MODE (XEXP (op, 0)))
757     {
758     case CCL1mode:
759       return GET_CODE (op) == GEU;
760
761     case CCL2mode:
762       return GET_CODE (op) == GTU;
763
764     case CCL3mode:
765       return GET_CODE (op) == LTU;
766
767     case CCUmode:
768       return GET_CODE (op) == LEU;
769
770     case CCURmode:
771       return GET_CODE (op) == GEU;
772
773     case CCSmode:
774       return GET_CODE (op) == LE;
775
776     case CCSRmode:
777       return GET_CODE (op) == GE;
778
779     default:
780       return 0;
781     }
782 }
783
784 /* Return branch condition mask to implement a branch
785    specified by CODE.  Return -1 for invalid comparisons.  */
786
787 static int
788 s390_branch_condition_mask (rtx code)
789 {
790   const int CC0 = 1 << 3;
791   const int CC1 = 1 << 2;
792   const int CC2 = 1 << 1;
793   const int CC3 = 1 << 0;
794
795   if (GET_CODE (XEXP (code, 0)) != REG
796       || REGNO (XEXP (code, 0)) != CC_REGNUM
797       || XEXP (code, 1) != const0_rtx)
798     abort ();
799
800   switch (GET_MODE (XEXP (code, 0)))
801     {
802     case CCZmode:
803       switch (GET_CODE (code))
804         {
805         case EQ:        return CC0;
806         case NE:        return CC1 | CC2 | CC3;
807         default:        return -1;
808         }
809       break;
810
811     case CCT1mode:
812       switch (GET_CODE (code))
813         {
814         case EQ:        return CC1;
815         case NE:        return CC0 | CC2 | CC3;
816         default:        return -1;
817         }
818       break;
819
820     case CCT2mode:
821       switch (GET_CODE (code))
822         {
823         case EQ:        return CC2;
824         case NE:        return CC0 | CC1 | CC3;
825         default:        return -1;
826         }
827       break;
828
829     case CCT3mode:
830       switch (GET_CODE (code))
831         {
832         case EQ:        return CC3;
833         case NE:        return CC0 | CC1 | CC2;
834         default:        return -1;
835         }
836       break;
837
838     case CCLmode:
839       switch (GET_CODE (code))
840         {
841         case EQ:        return CC0 | CC2;
842         case NE:        return CC1 | CC3;
843         default:        return -1;
844         }
845       break;
846
847     case CCL1mode:
848       switch (GET_CODE (code))
849         {
850         case LTU:       return CC2 | CC3;  /* carry */
851         case GEU:       return CC0 | CC1;  /* no carry */
852         default:        return -1;
853         }
854       break;
855
856     case CCL2mode:
857       switch (GET_CODE (code))
858         {
859         case GTU:       return CC0 | CC1;  /* borrow */
860         case LEU:       return CC2 | CC3;  /* no borrow */
861         default:        return -1;
862         }
863       break;
864
865     case CCL3mode:
866       switch (GET_CODE (code))
867         {
868         case EQ:        return CC0 | CC2;
869         case NE:        return CC1 | CC3;
870         case LTU:       return CC1;
871         case GTU:       return CC3;
872         case LEU:       return CC1 | CC2;
873         case GEU:       return CC2 | CC3;
874         default:        return -1;
875         }
876
877     case CCUmode:
878       switch (GET_CODE (code))
879         {
880         case EQ:        return CC0;
881         case NE:        return CC1 | CC2 | CC3;
882         case LTU:       return CC1;
883         case GTU:       return CC2;
884         case LEU:       return CC0 | CC1;
885         case GEU:       return CC0 | CC2;
886         default:        return -1;
887         }
888       break;
889
890     case CCURmode:
891       switch (GET_CODE (code))
892         {
893         case EQ:        return CC0;
894         case NE:        return CC2 | CC1 | CC3;
895         case LTU:       return CC2;
896         case GTU:       return CC1;
897         case LEU:       return CC0 | CC2;
898         case GEU:       return CC0 | CC1;
899         default:        return -1;
900         }
901       break;
902
903     case CCAPmode:
904       switch (GET_CODE (code))
905         {
906         case EQ:        return CC0;
907         case NE:        return CC1 | CC2 | CC3;
908         case LT:        return CC1 | CC3;
909         case GT:        return CC2;
910         case LE:        return CC0 | CC1 | CC3;
911         case GE:        return CC0 | CC2;
912         default:        return -1;
913         }
914       break;
915
916     case CCANmode:
917       switch (GET_CODE (code))
918         {
919         case EQ:        return CC0;
920         case NE:        return CC1 | CC2 | CC3;
921         case LT:        return CC1;
922         case GT:        return CC2 | CC3;
923         case LE:        return CC0 | CC1;
924         case GE:        return CC0 | CC2 | CC3;
925         default:        return -1;
926         }
927       break;
928
929     case CCSmode:
930       switch (GET_CODE (code))
931         {
932         case EQ:        return CC0;
933         case NE:        return CC1 | CC2 | CC3;
934         case LT:        return CC1;
935         case GT:        return CC2;
936         case LE:        return CC0 | CC1;
937         case GE:        return CC0 | CC2;
938         case UNORDERED: return CC3;
939         case ORDERED:   return CC0 | CC1 | CC2;
940         case UNEQ:      return CC0 | CC3;
941         case UNLT:      return CC1 | CC3;
942         case UNGT:      return CC2 | CC3;
943         case UNLE:      return CC0 | CC1 | CC3;
944         case UNGE:      return CC0 | CC2 | CC3;
945         case LTGT:      return CC1 | CC2;
946         default:        return -1;
947         }
948       break;
949
950     case CCSRmode:
951       switch (GET_CODE (code))
952         {
953         case EQ:        return CC0;
954         case NE:        return CC2 | CC1 | CC3;
955         case LT:        return CC2;
956         case GT:        return CC1;
957         case LE:        return CC0 | CC2;
958         case GE:        return CC0 | CC1;
959         case UNORDERED: return CC3;
960         case ORDERED:   return CC0 | CC2 | CC1;
961         case UNEQ:      return CC0 | CC3;
962         case UNLT:      return CC2 | CC3;
963         case UNGT:      return CC1 | CC3;
964         case UNLE:      return CC0 | CC2 | CC3;
965         case UNGE:      return CC0 | CC1 | CC3;
966         case LTGT:      return CC2 | CC1;
967         default:        return -1;
968         }
969       break;
970
971     default:
972       return -1;
973     }
974 }
975
976 /* If INV is false, return assembler mnemonic string to implement
977    a branch specified by CODE.  If INV is true, return mnemonic
978    for the corresponding inverted branch.  */
979
980 static const char *
981 s390_branch_condition_mnemonic (rtx code, int inv)
982 {
983   static const char *const mnemonic[16] =
984     {
985       NULL, "o", "h", "nle",
986       "l", "nhe", "lh", "ne",
987       "e", "nlh", "he", "nl",
988       "le", "nh", "no", NULL
989     };
990
991   int mask = s390_branch_condition_mask (code);
992   gcc_assert (mask >= 0);
993
994   if (inv)
995     mask ^= 15;
996
997   if (mask < 1 || mask > 14)
998     abort ();
999
1000   return mnemonic[mask];
1001 }
1002
1003 /* Return the part of op which has a value different from def.
1004    The size of the part is determined by mode.
1005    Use this function only if you already know that op really
1006    contains such a part.  */
1007
1008 unsigned HOST_WIDE_INT
1009 s390_extract_part (rtx op, enum machine_mode mode, int def)
1010 {
1011   unsigned HOST_WIDE_INT value = 0;
1012   int max_parts = HOST_BITS_PER_WIDE_INT / GET_MODE_BITSIZE (mode);
1013   int part_bits = GET_MODE_BITSIZE (mode);
1014   unsigned HOST_WIDE_INT part_mask = (1 << part_bits) - 1;
1015   int i;
1016
1017   for (i = 0; i < max_parts; i++)
1018     {
1019       if (i == 0)
1020         value = (unsigned HOST_WIDE_INT) INTVAL (op);
1021       else
1022         value >>= part_bits;
1023
1024       if ((value & part_mask) != (def & part_mask))
1025         return value & part_mask;
1026     }
1027
1028   abort ();
1029 }
1030
1031 /* If OP is an integer constant of mode MODE with exactly one
1032    part of mode PART_MODE unequal to DEF, return the number of that
1033    part. Otherwise, return -1.  */
1034
1035 int
1036 s390_single_part (rtx op,
1037                   enum machine_mode mode,
1038                   enum machine_mode part_mode,
1039                   int def)
1040 {
1041   unsigned HOST_WIDE_INT value = 0;
1042   int n_parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (part_mode);
1043   unsigned HOST_WIDE_INT part_mask = (1 << GET_MODE_BITSIZE (part_mode)) - 1;
1044   int i, part = -1;
1045
1046   if (GET_CODE (op) != CONST_INT)
1047     return -1;
1048
1049   for (i = 0; i < n_parts; i++)
1050     {
1051       if (i == 0)
1052         value = (unsigned HOST_WIDE_INT) INTVAL (op);
1053       else
1054         value >>= GET_MODE_BITSIZE (part_mode);
1055
1056       if ((value & part_mask) != (def & part_mask))
1057         {
1058           if (part != -1)
1059             return -1;
1060           else
1061             part = i;
1062         }
1063     }
1064   return part == -1 ? -1 : n_parts - 1 - part;
1065 }
1066
1067 /* Check whether we can (and want to) split a double-word
1068    move in mode MODE from SRC to DST into two single-word
1069    moves, moving the subword FIRST_SUBWORD first.  */
1070
1071 bool
1072 s390_split_ok_p (rtx dst, rtx src, enum machine_mode mode, int first_subword)
1073 {
1074   /* Floating point registers cannot be split.  */
1075   if (FP_REG_P (src) || FP_REG_P (dst))
1076     return false;
1077
1078   /* We don't need to split if operands are directly accessible.  */
1079   if (s_operand (src, mode) || s_operand (dst, mode))
1080     return false;
1081
1082   /* Non-offsettable memory references cannot be split.  */
1083   if ((GET_CODE (src) == MEM && !offsettable_memref_p (src))
1084       || (GET_CODE (dst) == MEM && !offsettable_memref_p (dst)))
1085     return false;
1086
1087   /* Moving the first subword must not clobber a register
1088      needed to move the second subword.  */
1089   if (register_operand (dst, mode))
1090     {
1091       rtx subreg = operand_subword (dst, first_subword, 0, mode);
1092       if (reg_overlap_mentioned_p (subreg, src))
1093         return false;
1094     }
1095
1096   return true;
1097 }
1098
1099 /* Check whether the address of memory reference MEM2 equals exactly
1100    the address of memory reference MEM1 plus DELTA.  Return true if
1101    we can prove this to be the case, false otherwise.  */
1102
1103 bool
1104 s390_offset_p (rtx mem1, rtx mem2, rtx delta)
1105 {
1106   rtx addr1, addr2, addr_delta;
1107
1108   if (GET_CODE (mem1) != MEM || GET_CODE (mem2) != MEM)
1109     return false;
1110
1111   addr1 = XEXP (mem1, 0);
1112   addr2 = XEXP (mem2, 0);
1113
1114   addr_delta = simplify_binary_operation (MINUS, Pmode, addr2, addr1);
1115   if (!addr_delta || !rtx_equal_p (addr_delta, delta))
1116     return false;
1117
1118   return true;
1119 }
1120
1121 /* Expand logical operator CODE in mode MODE with operands OPERANDS.  */
1122
1123 void
1124 s390_expand_logical_operator (enum rtx_code code, enum machine_mode mode,
1125                               rtx *operands)
1126 {
1127   enum machine_mode wmode = mode;
1128   rtx dst = operands[0];
1129   rtx src1 = operands[1];
1130   rtx src2 = operands[2];
1131   rtx op, clob, tem;
1132
1133   /* If we cannot handle the operation directly, use a temp register.  */
1134   if (!s390_logical_operator_ok_p (operands))
1135     dst = gen_reg_rtx (mode);
1136
1137   /* QImode and HImode patterns make sense only if we have a destination
1138      in memory.  Otherwise perform the operation in SImode.  */
1139   if ((mode == QImode || mode == HImode) && GET_CODE (dst) != MEM)
1140     wmode = SImode;
1141
1142   /* Widen operands if required.  */
1143   if (mode != wmode)
1144     {
1145       if (GET_CODE (dst) == SUBREG
1146           && (tem = simplify_subreg (wmode, dst, mode, 0)) != 0)
1147         dst = tem;
1148       else if (REG_P (dst))
1149         dst = gen_rtx_SUBREG (wmode, dst, 0);
1150       else
1151         dst = gen_reg_rtx (wmode);
1152
1153       if (GET_CODE (src1) == SUBREG
1154           && (tem = simplify_subreg (wmode, src1, mode, 0)) != 0)
1155         src1 = tem;
1156       else if (GET_MODE (src1) != VOIDmode)
1157         src1 = gen_rtx_SUBREG (wmode, force_reg (mode, src1), 0);
1158
1159       if (GET_CODE (src2) == SUBREG
1160           && (tem = simplify_subreg (wmode, src2, mode, 0)) != 0)
1161         src2 = tem;
1162       else if (GET_MODE (src2) != VOIDmode)
1163         src2 = gen_rtx_SUBREG (wmode, force_reg (mode, src2), 0);
1164     }
1165
1166   /* Emit the instruction.  */
1167   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, wmode, src1, src2));
1168   clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
1169   emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
1170
1171   /* Fix up the destination if needed.  */
1172   if (dst != operands[0])
1173     emit_move_insn (operands[0], gen_lowpart (mode, dst));
1174 }
1175
1176 /* Check whether OPERANDS are OK for a logical operation (AND, IOR, XOR).  */
1177
1178 bool
1179 s390_logical_operator_ok_p (rtx *operands)
1180 {
1181   /* If the destination operand is in memory, it needs to coincide
1182      with one of the source operands.  After reload, it has to be
1183      the first source operand.  */
1184   if (GET_CODE (operands[0]) == MEM)
1185     return rtx_equal_p (operands[0], operands[1])
1186            || (!reload_completed && rtx_equal_p (operands[0], operands[2]));
1187
1188   return true;
1189 }
1190
1191 /* Narrow logical operation CODE of memory operand MEMOP with immediate
1192    operand IMMOP to switch from SS to SI type instructions.  */
1193
1194 void
1195 s390_narrow_logical_operator (enum rtx_code code, rtx *memop, rtx *immop)
1196 {
1197   int def = code == AND ? -1 : 0;
1198   HOST_WIDE_INT mask;
1199   int part;
1200
1201   gcc_assert (GET_CODE (*memop) == MEM);
1202   gcc_assert (!MEM_VOLATILE_P (*memop));
1203
1204   mask = s390_extract_part (*immop, QImode, def);
1205   part = s390_single_part (*immop, GET_MODE (*memop), QImode, def);
1206   gcc_assert (part >= 0);
1207
1208   *memop = adjust_address (*memop, QImode, part);
1209   *immop = gen_int_mode (mask, QImode);
1210 }
1211
1212
1213 /* Change optimizations to be performed, depending on the
1214    optimization level.
1215
1216    LEVEL is the optimization level specified; 2 if `-O2' is
1217    specified, 1 if `-O' is specified, and 0 if neither is specified.
1218
1219    SIZE is nonzero if `-Os' is specified and zero otherwise.  */
1220
1221 void
1222 optimization_options (int level ATTRIBUTE_UNUSED, int size ATTRIBUTE_UNUSED)
1223 {
1224   /* ??? There are apparently still problems with -fcaller-saves.  */
1225   flag_caller_saves = 0;
1226
1227   /* By default, always emit DWARF-2 unwind info.  This allows debugging
1228      without maintaining a stack frame back-chain.  */
1229   flag_asynchronous_unwind_tables = 1;
1230 }
1231
1232 void
1233 override_options (void)
1234 {
1235   int i;
1236   static struct pta
1237     {
1238       const char *const name;           /* processor name or nickname.  */
1239       const enum processor_type processor;
1240       const enum processor_flags flags;
1241     }
1242   const processor_alias_table[] =
1243     {
1244       {"g5", PROCESSOR_9672_G5, PF_IEEE_FLOAT},
1245       {"g6", PROCESSOR_9672_G6, PF_IEEE_FLOAT},
1246       {"z900", PROCESSOR_2064_Z900, PF_IEEE_FLOAT | PF_ZARCH},
1247       {"z990", PROCESSOR_2084_Z990, PF_IEEE_FLOAT | PF_ZARCH
1248                                     | PF_LONG_DISPLACEMENT},
1249     };
1250
1251   int const pta_size = ARRAY_SIZE (processor_alias_table);
1252
1253   /* Acquire a unique set number for our register saves and restores.  */
1254   s390_sr_alias_set = new_alias_set ();
1255
1256   /* Set up function hooks.  */
1257   init_machine_status = s390_init_machine_status;
1258
1259   /* Architecture mode defaults according to ABI.  */
1260   if (!(target_flags_explicit & MASK_ZARCH))
1261     {
1262       if (TARGET_64BIT)
1263         target_flags |= MASK_ZARCH;
1264       else
1265         target_flags &= ~MASK_ZARCH;
1266     }
1267
1268   /* Determine processor architectural level.  */
1269   if (!s390_arch_string)
1270     s390_arch_string = TARGET_ZARCH? "z900" : "g5";
1271
1272   for (i = 0; i < pta_size; i++)
1273     if (! strcmp (s390_arch_string, processor_alias_table[i].name))
1274       {
1275         s390_arch = processor_alias_table[i].processor;
1276         s390_arch_flags = processor_alias_table[i].flags;
1277         break;
1278       }
1279   if (i == pta_size)
1280     error ("Unknown cpu used in -march=%s.", s390_arch_string);
1281
1282   /* Determine processor to tune for.  */
1283   if (!s390_tune_string)
1284     {
1285       s390_tune = s390_arch;
1286       s390_tune_flags = s390_arch_flags;
1287       s390_tune_string = s390_arch_string;
1288     }
1289   else
1290     {
1291       for (i = 0; i < pta_size; i++)
1292         if (! strcmp (s390_tune_string, processor_alias_table[i].name))
1293           {
1294             s390_tune = processor_alias_table[i].processor;
1295             s390_tune_flags = processor_alias_table[i].flags;
1296             break;
1297           }
1298       if (i == pta_size)
1299         error ("Unknown cpu used in -mtune=%s.", s390_tune_string);
1300     }
1301
1302   /* Sanity checks.  */
1303   if (TARGET_ZARCH && !(s390_arch_flags & PF_ZARCH))
1304     error ("z/Architecture mode not supported on %s.", s390_arch_string);
1305   if (TARGET_64BIT && !TARGET_ZARCH)
1306     error ("64-bit ABI not supported in ESA/390 mode.");
1307
1308   if (s390_warn_framesize_string)
1309     {
1310       if (sscanf (s390_warn_framesize_string, HOST_WIDE_INT_PRINT_DEC,
1311                   &s390_warn_framesize) != 1)
1312         error ("invalid value for -mwarn-framesize");
1313     }
1314
1315   if (s390_warn_dynamicstack_string)
1316     s390_warn_dynamicstack_p = 1;
1317   
1318   if (s390_stack_size_string)
1319     {
1320       if (sscanf (s390_stack_size_string, HOST_WIDE_INT_PRINT_DEC, 
1321                   &s390_stack_size) != 1)
1322         error ("invalid value for -mstack-size");
1323       
1324       if (exact_log2 (s390_stack_size) == -1)
1325         error ("stack size must be an exact power of 2");
1326       
1327       if (s390_stack_guard_string)
1328         {
1329           if (sscanf (s390_stack_guard_string, HOST_WIDE_INT_PRINT_DEC, 
1330                       &s390_stack_guard) != 1)
1331             error ("invalid value for -mstack-guard");
1332           
1333           if (s390_stack_guard >= s390_stack_size)
1334             error ("stack size must be greater than the stack guard value");
1335  
1336           if (exact_log2 (s390_stack_guard) == -1)
1337             error ("stack guard value must be an exact power of 2");
1338         }
1339       else
1340         error ("-mstack-size implies use of -mstack-guard");
1341     }
1342   
1343   if (s390_stack_guard_string && !s390_stack_size_string)
1344     error ("-mstack-guard implies use of -mstack-size"); 
1345 }
1346
1347 /* Map for smallest class containing reg regno.  */
1348
1349 const enum reg_class regclass_map[FIRST_PSEUDO_REGISTER] =
1350 { GENERAL_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
1351   ADDR_REGS,    ADDR_REGS, ADDR_REGS, ADDR_REGS,
1352   ADDR_REGS,    ADDR_REGS, ADDR_REGS, ADDR_REGS,
1353   ADDR_REGS,    ADDR_REGS, ADDR_REGS, ADDR_REGS,
1354   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,
1355   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,
1356   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,
1357   FP_REGS,      FP_REGS,   FP_REGS,   FP_REGS,
1358   ADDR_REGS,    NO_REGS,   ADDR_REGS, ADDR_REGS
1359 };
1360
1361 /* Return attribute type of insn.  */
1362
1363 static enum attr_type
1364 s390_safe_attr_type (rtx insn)
1365 {
1366   if (recog_memoized (insn) >= 0)
1367     return get_attr_type (insn);
1368   else
1369     return TYPE_NONE;
1370 }
1371
1372 /* Return true if OP a (const_int 0) operand.
1373    OP is the current operation.
1374    MODE is the current operation mode.  */
1375
1376 int
1377 const0_operand (register rtx op, enum machine_mode mode)
1378 {
1379   return op == CONST0_RTX (mode);
1380 }
1381
1382 /* Return true if OP is constant.
1383    OP is the current operation.
1384    MODE is the current operation mode.  */
1385
1386 int
1387 consttable_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1388 {
1389   return CONSTANT_P (op);
1390 }
1391
1392 /* Return true if the mode of operand OP matches MODE.
1393    If MODE is set to VOIDmode, set it to the mode of OP.  */
1394
1395 static int
1396 check_mode (register rtx op, enum machine_mode *mode)
1397 {
1398   if (*mode == VOIDmode)
1399       *mode = GET_MODE (op);
1400   else
1401   {
1402     if (GET_MODE (op) != VOIDmode && GET_MODE (op) != *mode)
1403        return 0;
1404   }
1405   return 1;
1406 }
1407
1408 /* Return true if OP a valid operand for the LARL instruction.
1409    OP is the current operation.
1410    MODE is the current operation mode.  */
1411
1412 int
1413 larl_operand (register rtx op, enum machine_mode mode)
1414 {
1415   if (! check_mode (op, &mode))
1416     return 0;
1417
1418   /* Allow labels and local symbols.  */
1419   if (GET_CODE (op) == LABEL_REF)
1420     return 1;
1421   if (GET_CODE (op) == SYMBOL_REF)
1422     return ((SYMBOL_REF_FLAGS (op) & SYMBOL_FLAG_ALIGN1) == 0
1423             && SYMBOL_REF_TLS_MODEL (op) == 0
1424             && (!flag_pic || SYMBOL_REF_LOCAL_P (op)));
1425
1426   /* Everything else must have a CONST, so strip it.  */
1427   if (GET_CODE (op) != CONST)
1428     return 0;
1429   op = XEXP (op, 0);
1430
1431   /* Allow adding *even* in-range constants.  */
1432   if (GET_CODE (op) == PLUS)
1433     {
1434       if (GET_CODE (XEXP (op, 1)) != CONST_INT
1435           || (INTVAL (XEXP (op, 1)) & 1) != 0)
1436         return 0;
1437 #if HOST_BITS_PER_WIDE_INT > 32
1438       if (INTVAL (XEXP (op, 1)) >= (HOST_WIDE_INT)1 << 32
1439           || INTVAL (XEXP (op, 1)) < -((HOST_WIDE_INT)1 << 32))
1440         return 0;
1441 #endif
1442       op = XEXP (op, 0);
1443     }
1444
1445   /* Labels and local symbols allowed here as well.  */
1446   if (GET_CODE (op) == LABEL_REF)
1447     return 1;
1448   if (GET_CODE (op) == SYMBOL_REF)
1449     return ((SYMBOL_REF_FLAGS (op) & SYMBOL_FLAG_ALIGN1) == 0
1450             && SYMBOL_REF_TLS_MODEL (op) == 0
1451             && (!flag_pic || SYMBOL_REF_LOCAL_P (op)));
1452
1453   /* Now we must have a @GOTENT offset or @PLT stub
1454      or an @INDNTPOFF TLS offset.  */
1455   if (GET_CODE (op) == UNSPEC
1456       && XINT (op, 1) == UNSPEC_GOTENT)
1457     return 1;
1458   if (GET_CODE (op) == UNSPEC
1459       && XINT (op, 1) == UNSPEC_PLT)
1460     return 1;
1461   if (GET_CODE (op) == UNSPEC
1462       && XINT (op, 1) == UNSPEC_INDNTPOFF)
1463     return 1;
1464
1465   return 0;
1466 }
1467
1468 /* Return true if OP is a valid S-type operand.
1469    OP is the current operation.
1470    MODE is the current operation mode.  */
1471
1472 int
1473 s_operand (rtx op, enum machine_mode mode)
1474 {
1475   struct s390_address addr;
1476
1477   /* Call general_operand first, so that we don't have to
1478      check for many special cases.  */
1479   if (!general_operand (op, mode))
1480     return 0;
1481
1482   /* Just like memory_operand, allow (subreg (mem ...))
1483      after reload.  */
1484   if (reload_completed
1485       && GET_CODE (op) == SUBREG
1486       && GET_CODE (SUBREG_REG (op)) == MEM)
1487     op = SUBREG_REG (op);
1488
1489   if (GET_CODE (op) != MEM)
1490     return 0;
1491   if (!s390_decompose_address (XEXP (op, 0), &addr))
1492     return 0;
1493   if (addr.indx)
1494     return 0;
1495
1496   return 1;
1497 }
1498
1499 /* Return true if OP is a memory operand pointing to the
1500    literal pool, or an immediate operand.  */
1501
1502 bool
1503 s390_pool_operand (rtx op)
1504 {
1505   struct s390_address addr;
1506
1507   /* Just like memory_operand, allow (subreg (mem ...))
1508      after reload.  */
1509   if (reload_completed
1510       && GET_CODE (op) == SUBREG
1511       && GET_CODE (SUBREG_REG (op)) == MEM)
1512     op = SUBREG_REG (op);
1513
1514   switch (GET_CODE (op))
1515     {
1516     case CONST_INT:
1517     case CONST_DOUBLE:
1518       return true;
1519
1520     case MEM:
1521       if (!s390_decompose_address (XEXP (op, 0), &addr))
1522         return false;
1523       if (addr.base && REG_P (addr.base) && REGNO (addr.base) == BASE_REGNUM)
1524         return true;
1525       if (addr.indx && REG_P (addr.indx) && REGNO (addr.indx) == BASE_REGNUM)
1526         return true;
1527       return false;
1528
1529     default:
1530       return false;
1531     }
1532 }
1533
1534 /* Return true if OP a valid shift count operand.
1535    OP is the current operation.
1536    MODE is the current operation mode.  */
1537
1538 int
1539 shift_count_operand (rtx op, enum machine_mode mode)
1540 {
1541   HOST_WIDE_INT offset = 0;
1542
1543   if (! check_mode (op, &mode))
1544     return 0;
1545
1546   /* We can have an integer constant, an address register,
1547      or a sum of the two.  Note that reload already checks
1548      that any register present is an address register, so
1549      we just check for any register here.  */
1550   if (GET_CODE (op) == CONST_INT)
1551     {
1552       offset = INTVAL (op);
1553       op = NULL_RTX;
1554     }
1555   if (op && GET_CODE (op) == PLUS && GET_CODE (XEXP (op, 1)) == CONST_INT)
1556     {
1557       offset = INTVAL (XEXP (op, 1));
1558       op = XEXP (op, 0);
1559     }
1560   while (op && GET_CODE (op) == SUBREG)
1561     op = SUBREG_REG (op);
1562   if (op && GET_CODE (op) != REG)
1563     return 0;
1564
1565   /* Unfortunately we have to reject constants that are invalid
1566      for an address, or else reload will get confused.  */
1567   if (!DISP_IN_RANGE (offset))
1568     return 0;
1569
1570   return 1;
1571 }
1572
1573 /* Return true if DISP is a valid short displacement.  */
1574
1575 static int
1576 s390_short_displacement (rtx disp)
1577 {
1578   /* No displacement is OK.  */
1579   if (!disp)
1580     return 1;
1581
1582   /* Integer displacement in range.  */
1583   if (GET_CODE (disp) == CONST_INT)
1584     return INTVAL (disp) >= 0 && INTVAL (disp) < 4096;
1585
1586   /* GOT offset is not OK, the GOT can be large.  */
1587   if (GET_CODE (disp) == CONST
1588       && GET_CODE (XEXP (disp, 0)) == UNSPEC
1589       && XINT (XEXP (disp, 0), 1) == UNSPEC_GOT)
1590     return 0;
1591
1592   /* All other symbolic constants are literal pool references,
1593      which are OK as the literal pool must be small.  */
1594   if (GET_CODE (disp) == CONST)
1595     return 1;
1596
1597   return 0;
1598 }
1599
1600 /* Return true if OP is a valid operand for a C constraint.  */
1601
1602 int
1603 s390_extra_constraint_str (rtx op, int c, const char * str)
1604 {
1605   struct s390_address addr;
1606
1607   if (c != str[0])
1608     abort ();
1609
1610   /* Check for offsettable variants of memory constraints.  */
1611   if (c == 'A')
1612     {
1613       /* Only accept non-volatile MEMs.  */
1614       if (!MEM_P (op) || MEM_VOLATILE_P (op))
1615         return 0;
1616
1617       if ((reload_completed || reload_in_progress)
1618           ? !offsettable_memref_p (op)
1619           : !offsettable_nonstrict_memref_p (op))
1620         return 0;
1621
1622       c = str[1];
1623     }
1624
1625   switch (c)
1626     {
1627     case 'Q':
1628       if (GET_CODE (op) != MEM)
1629         return 0;
1630       if (!s390_decompose_address (XEXP (op, 0), &addr))
1631         return 0;
1632       if (addr.indx)
1633         return 0;
1634
1635       if (TARGET_LONG_DISPLACEMENT)
1636         {
1637           if (!s390_short_displacement (addr.disp))
1638             return 0;
1639         }
1640       break;
1641
1642     case 'R':
1643       if (GET_CODE (op) != MEM)
1644         return 0;
1645
1646       if (TARGET_LONG_DISPLACEMENT)
1647         {
1648           if (!s390_decompose_address (XEXP (op, 0), &addr))
1649             return 0;
1650           if (!s390_short_displacement (addr.disp))
1651             return 0;
1652         }
1653       break;
1654
1655     case 'S':
1656       if (!TARGET_LONG_DISPLACEMENT)
1657         return 0;
1658       if (GET_CODE (op) != MEM)
1659         return 0;
1660       if (!s390_decompose_address (XEXP (op, 0), &addr))
1661         return 0;
1662       if (addr.indx)
1663         return 0;
1664       if (s390_short_displacement (addr.disp))
1665         return 0;
1666       break;
1667
1668     case 'T':
1669       if (!TARGET_LONG_DISPLACEMENT)
1670         return 0;
1671       if (GET_CODE (op) != MEM)
1672         return 0;
1673       /* Any invalid address here will be fixed up by reload,
1674          so accept it for the most generic constraint.  */
1675       if (s390_decompose_address (XEXP (op, 0), &addr)
1676           && s390_short_displacement (addr.disp))
1677         return 0;
1678       break;
1679
1680     case 'U':
1681       if (TARGET_LONG_DISPLACEMENT)
1682         {
1683           if (!s390_decompose_address (op, &addr))
1684             return 0;
1685           if (!s390_short_displacement (addr.disp))
1686             return 0;
1687         }
1688       break;
1689
1690     case 'W':
1691       if (!TARGET_LONG_DISPLACEMENT)
1692         return 0;
1693       /* Any invalid address here will be fixed up by reload,
1694          so accept it for the most generic constraint.  */
1695       if (s390_decompose_address (op, &addr)
1696           && s390_short_displacement (addr.disp))
1697         return 0;
1698       break;
1699
1700     case 'Y':
1701       return shift_count_operand (op, VOIDmode);
1702
1703     default:
1704       return 0;
1705     }
1706
1707   return 1;
1708 }
1709
1710 /* Return true if VALUE matches the constraint STR.  */
1711
1712 int
1713 s390_const_ok_for_constraint_p (HOST_WIDE_INT value,
1714                                 int c,
1715                                 const char * str)
1716 {
1717   enum machine_mode mode, part_mode;
1718   int def;
1719   int part, part_goal;
1720
1721   if (c != str[0])
1722     abort ();
1723
1724   switch (str[0])
1725     {
1726     case 'I':
1727       return (unsigned int)value < 256;
1728
1729     case 'J':
1730       return (unsigned int)value < 4096;
1731
1732     case 'K':
1733       return value >= -32768 && value < 32768;
1734
1735     case 'L':
1736       return (TARGET_LONG_DISPLACEMENT ?
1737               (value >= -524288 && value <= 524287)
1738               : (value >= 0 && value <= 4095));
1739     case 'M':
1740       return value == 2147483647;
1741
1742     case 'N':
1743       if (str[1] == 'x')
1744         part_goal = -1;
1745       else
1746         part_goal = str[1] - '0';
1747
1748       switch (str[2])
1749         {
1750         case 'H': part_mode = HImode; break;
1751         case 'Q': part_mode = QImode; break;
1752         default:  return 0;
1753         }
1754
1755       switch (str[3])
1756         {
1757         case 'H': mode = HImode; break;
1758         case 'S': mode = SImode; break;
1759         case 'D': mode = DImode; break;
1760         default: return 0;
1761         }
1762
1763       switch (str[4])
1764         {
1765         case '0': def = 0;  break;
1766         case 'F': def = -1; break;
1767         default: return 0;
1768         }
1769
1770       if (GET_MODE_SIZE (mode) <= GET_MODE_SIZE (part_mode))
1771         return 0;
1772
1773       part = s390_single_part (GEN_INT (value), mode, part_mode, def);
1774       if (part < 0)
1775         return 0;
1776       if (part_goal != -1 && part_goal != part)
1777         return 0;
1778
1779       break;
1780
1781     default:
1782       return 0;
1783     }
1784
1785   return 1;
1786 }
1787
1788 /* Compute a (partial) cost for rtx X.  Return true if the complete
1789    cost has been computed, and false if subexpressions should be
1790    scanned.  In either case, *TOTAL contains the cost result.  */
1791
1792 static bool
1793 s390_rtx_costs (rtx x, int code, int outer_code, int *total)
1794 {
1795   switch (code)
1796     {
1797     case CONST:
1798       if (GET_CODE (XEXP (x, 0)) == MINUS
1799           && GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
1800         *total = 1000;
1801       else
1802         *total = 0;
1803       return true;
1804
1805     case CONST_INT:
1806       /* Force_const_mem does not work out of reload, because the
1807          saveable_obstack is set to reload_obstack, which does not
1808          live long enough.  Because of this we cannot use force_const_mem
1809          in addsi3.  This leads to problems with gen_add2_insn with a
1810          constant greater than a short. Because of that we give an
1811          addition of greater constants a cost of 3 (reload1.c 10096).  */
1812       /* ??? saveable_obstack no longer exists.  */
1813       if (outer_code == PLUS
1814           && (INTVAL (x) > 32767 || INTVAL (x) < -32768))
1815         *total = COSTS_N_INSNS (3);
1816       else
1817         *total = 0;
1818       return true;
1819
1820     case LABEL_REF:
1821     case SYMBOL_REF:
1822     case CONST_DOUBLE:
1823       *total = 0;
1824       return true;
1825
1826     case ASHIFT:
1827     case ASHIFTRT:
1828     case LSHIFTRT:
1829     case PLUS:
1830     case AND:
1831     case IOR:
1832     case XOR:
1833     case MINUS:
1834     case NEG:
1835     case NOT:
1836       *total = COSTS_N_INSNS (1);
1837       return true;
1838
1839     case MULT:
1840       if (GET_MODE (XEXP (x, 0)) == DImode)
1841         *total = COSTS_N_INSNS (40);
1842       else
1843         *total = COSTS_N_INSNS (7);
1844       return true;
1845
1846     case DIV:
1847     case UDIV:
1848     case MOD:
1849     case UMOD:
1850       *total = COSTS_N_INSNS (33);
1851       return true;
1852
1853     default:
1854       return false;
1855     }
1856 }
1857
1858 /* Return the cost of an address rtx ADDR.  */
1859
1860 static int
1861 s390_address_cost (rtx addr)
1862 {
1863   struct s390_address ad;
1864   if (!s390_decompose_address (addr, &ad))
1865     return 1000;
1866
1867   return ad.indx? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (1);
1868 }
1869
1870 /* Return true if OP is a valid operand for the BRAS instruction.
1871    OP is the current operation.
1872    MODE is the current operation mode.  */
1873
1874 int
1875 bras_sym_operand (register rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1876 {
1877   register enum rtx_code code = GET_CODE (op);
1878
1879   /* Allow SYMBOL_REFs.  */
1880   if (code == SYMBOL_REF)
1881     return 1;
1882
1883   /* Allow @PLT stubs.  */
1884   if (code == CONST
1885       && GET_CODE (XEXP (op, 0)) == UNSPEC
1886       && XINT (XEXP (op, 0), 1) == UNSPEC_PLT)
1887     return 1;
1888   return 0;
1889 }
1890
1891 /* If OP is a SYMBOL_REF of a thread-local symbol, return its TLS mode,
1892    otherwise return 0.  */
1893
1894 int
1895 tls_symbolic_operand (register rtx op)
1896 {
1897   if (GET_CODE (op) != SYMBOL_REF)
1898     return 0;
1899   return SYMBOL_REF_TLS_MODEL (op);
1900 }
1901 \f
1902 /* Return true if OP is a load multiple operation.  It is known to be a
1903    PARALLEL and the first section will be tested.
1904    OP is the current operation.
1905    MODE is the current operation mode.  */
1906
1907 int
1908 load_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1909 {
1910   enum machine_mode elt_mode;
1911   int count = XVECLEN (op, 0);
1912   unsigned int dest_regno;
1913   rtx src_addr;
1914   int i, off;
1915
1916
1917   /* Perform a quick check so we don't blow up below.  */
1918   if (count <= 1
1919       || GET_CODE (XVECEXP (op, 0, 0)) != SET
1920       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
1921       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
1922     return 0;
1923
1924   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
1925   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
1926   elt_mode = GET_MODE (SET_DEST (XVECEXP (op, 0, 0)));
1927
1928   /* Check, is base, or base + displacement.  */
1929
1930   if (GET_CODE (src_addr) == REG)
1931     off = 0;
1932   else if (GET_CODE (src_addr) == PLUS
1933            && GET_CODE (XEXP (src_addr, 0)) == REG
1934            && GET_CODE (XEXP (src_addr, 1)) == CONST_INT)
1935     {
1936       off = INTVAL (XEXP (src_addr, 1));
1937       src_addr = XEXP (src_addr, 0);
1938     }
1939   else
1940     return 0;
1941
1942   for (i = 1; i < count; i++)
1943     {
1944       rtx elt = XVECEXP (op, 0, i);
1945
1946       if (GET_CODE (elt) != SET
1947           || GET_CODE (SET_DEST (elt)) != REG
1948           || GET_MODE (SET_DEST (elt)) != elt_mode
1949           || REGNO (SET_DEST (elt)) != dest_regno + i
1950           || GET_CODE (SET_SRC (elt)) != MEM
1951           || GET_MODE (SET_SRC (elt)) != elt_mode
1952           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
1953           || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
1954           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
1955           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1))
1956              != off + i * GET_MODE_SIZE (elt_mode))
1957         return 0;
1958     }
1959
1960   return 1;
1961 }
1962
1963 /* Return true if OP is a store multiple operation.  It is known to be a
1964    PARALLEL and the first section will be tested.
1965    OP is the current operation.
1966    MODE is the current operation mode.  */
1967
1968 int
1969 store_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1970 {
1971   enum machine_mode elt_mode;
1972   int count = XVECLEN (op, 0);
1973   unsigned int src_regno;
1974   rtx dest_addr;
1975   int i, off;
1976
1977   /* Perform a quick check so we don't blow up below.  */
1978   if (count <= 1
1979       || GET_CODE (XVECEXP (op, 0, 0)) != SET
1980       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
1981       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
1982     return 0;
1983
1984   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
1985   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
1986   elt_mode = GET_MODE (SET_SRC (XVECEXP (op, 0, 0)));
1987
1988   /* Check, is base, or base + displacement.  */
1989
1990   if (GET_CODE (dest_addr) == REG)
1991     off = 0;
1992   else if (GET_CODE (dest_addr) == PLUS
1993            && GET_CODE (XEXP (dest_addr, 0)) == REG
1994            && GET_CODE (XEXP (dest_addr, 1)) == CONST_INT)
1995     {
1996       off = INTVAL (XEXP (dest_addr, 1));
1997       dest_addr = XEXP (dest_addr, 0);
1998     }
1999   else
2000     return 0;
2001
2002   for (i = 1; i < count; i++)
2003     {
2004       rtx elt = XVECEXP (op, 0, i);
2005
2006       if (GET_CODE (elt) != SET
2007           || GET_CODE (SET_SRC (elt)) != REG
2008           || GET_MODE (SET_SRC (elt)) != elt_mode
2009           || REGNO (SET_SRC (elt)) != src_regno + i
2010           || GET_CODE (SET_DEST (elt)) != MEM
2011           || GET_MODE (SET_DEST (elt)) != elt_mode
2012           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
2013           || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
2014           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
2015           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1))
2016              != off + i * GET_MODE_SIZE (elt_mode))
2017         return 0;
2018     }
2019   return 1;
2020 }
2021
2022
2023 /* Return true if OP contains a symbol reference */
2024
2025 int
2026 symbolic_reference_mentioned_p (rtx op)
2027 {
2028   register const char *fmt;
2029   register int i;
2030
2031   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
2032     return 1;
2033
2034   fmt = GET_RTX_FORMAT (GET_CODE (op));
2035   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
2036     {
2037       if (fmt[i] == 'E')
2038         {
2039           register int j;
2040
2041           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
2042             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
2043               return 1;
2044         }
2045
2046       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
2047         return 1;
2048     }
2049
2050   return 0;
2051 }
2052
2053 /* Return true if OP contains a reference to a thread-local symbol.  */
2054
2055 int
2056 tls_symbolic_reference_mentioned_p (rtx op)
2057 {
2058   register const char *fmt;
2059   register int i;
2060
2061   if (GET_CODE (op) == SYMBOL_REF)
2062     return tls_symbolic_operand (op);
2063
2064   fmt = GET_RTX_FORMAT (GET_CODE (op));
2065   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
2066     {
2067       if (fmt[i] == 'E')
2068         {
2069           register int j;
2070
2071           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
2072             if (tls_symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
2073               return 1;
2074         }
2075
2076       else if (fmt[i] == 'e' && tls_symbolic_reference_mentioned_p (XEXP (op, i)))
2077         return 1;
2078     }
2079
2080   return 0;
2081 }
2082
2083
2084 /* Return true if OP is a legitimate general operand when
2085    generating PIC code.  It is given that flag_pic is on
2086    and that OP satisfies CONSTANT_P or is a CONST_DOUBLE.  */
2087
2088 int
2089 legitimate_pic_operand_p (register rtx op)
2090 {
2091   /* Accept all non-symbolic constants.  */
2092   if (!SYMBOLIC_CONST (op))
2093     return 1;
2094
2095   /* Reject everything else; must be handled
2096      via emit_symbolic_move.  */
2097   return 0;
2098 }
2099
2100 /* Returns true if the constant value OP is a legitimate general operand.
2101    It is given that OP satisfies CONSTANT_P or is a CONST_DOUBLE.  */
2102
2103 int
2104 legitimate_constant_p (register rtx op)
2105 {
2106   /* Accept all non-symbolic constants.  */
2107   if (!SYMBOLIC_CONST (op))
2108     return 1;
2109
2110   /* Accept immediate LARL operands.  */
2111   if (TARGET_CPU_ZARCH && larl_operand (op, VOIDmode))
2112     return 1;
2113
2114   /* Thread-local symbols are never legal constants.  This is
2115      so that emit_call knows that computing such addresses
2116      might require a function call.  */
2117   if (TLS_SYMBOLIC_CONST (op))
2118     return 0;
2119
2120   /* In the PIC case, symbolic constants must *not* be
2121      forced into the literal pool.  We accept them here,
2122      so that they will be handled by emit_symbolic_move.  */
2123   if (flag_pic)
2124     return 1;
2125
2126   /* All remaining non-PIC symbolic constants are
2127      forced into the literal pool.  */
2128   return 0;
2129 }
2130
2131 /* Determine if it's legal to put X into the constant pool.  This
2132    is not possible if X contains the address of a symbol that is
2133    not constant (TLS) or not known at final link time (PIC).  */
2134
2135 static bool
2136 s390_cannot_force_const_mem (rtx x)
2137 {
2138   switch (GET_CODE (x))
2139     {
2140     case CONST_INT:
2141     case CONST_DOUBLE:
2142       /* Accept all non-symbolic constants.  */
2143       return false;
2144
2145     case LABEL_REF:
2146       /* Labels are OK iff we are non-PIC.  */
2147       return flag_pic != 0;
2148
2149     case SYMBOL_REF:
2150       /* 'Naked' TLS symbol references are never OK,
2151          non-TLS symbols are OK iff we are non-PIC.  */
2152       if (tls_symbolic_operand (x))
2153         return true;
2154       else
2155         return flag_pic != 0;
2156
2157     case CONST:
2158       return s390_cannot_force_const_mem (XEXP (x, 0));
2159     case PLUS:
2160     case MINUS:
2161       return s390_cannot_force_const_mem (XEXP (x, 0))
2162              || s390_cannot_force_const_mem (XEXP (x, 1));
2163
2164     case UNSPEC:
2165       switch (XINT (x, 1))
2166         {
2167         /* Only lt-relative or GOT-relative UNSPECs are OK.  */
2168         case UNSPEC_LTREL_OFFSET:
2169         case UNSPEC_GOT:
2170         case UNSPEC_GOTOFF:
2171         case UNSPEC_PLTOFF:
2172         case UNSPEC_TLSGD:
2173         case UNSPEC_TLSLDM:
2174         case UNSPEC_NTPOFF:
2175         case UNSPEC_DTPOFF:
2176         case UNSPEC_GOTNTPOFF:
2177         case UNSPEC_INDNTPOFF:
2178           return false;
2179
2180         /* If the literal pool shares the code section, be put
2181            execute template placeholders into the pool as well.  */
2182         case UNSPEC_INSN:
2183           return TARGET_CPU_ZARCH;
2184
2185         default:
2186           return true;
2187         }
2188       break;
2189
2190     default:
2191       abort ();
2192     }
2193 }
2194
2195 /* Returns true if the constant value OP is a legitimate general
2196    operand during and after reload.  The difference to
2197    legitimate_constant_p is that this function will not accept
2198    a constant that would need to be forced to the literal pool
2199    before it can be used as operand.  */
2200
2201 int
2202 legitimate_reload_constant_p (register rtx op)
2203 {
2204   /* Accept la(y) operands.  */
2205   if (GET_CODE (op) == CONST_INT
2206       && DISP_IN_RANGE (INTVAL (op)))
2207     return 1;
2208
2209   /* Accept l(g)hi operands.  */
2210   if (GET_CODE (op) == CONST_INT
2211       && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), 'K', "K"))
2212     return 1;
2213
2214   /* Accept lliXX operands.  */
2215   if (TARGET_ZARCH
2216       && s390_single_part (op, DImode, HImode, 0) >= 0)
2217   return 1;
2218
2219   /* Accept larl operands.  */
2220   if (TARGET_CPU_ZARCH
2221       && larl_operand (op, VOIDmode))
2222     return 1;
2223
2224   /* Everything else cannot be handled without reload.  */
2225   return 0;
2226 }
2227
2228 /* Given an rtx OP being reloaded into a reg required to be in class CLASS,
2229    return the class of reg to actually use.  */
2230
2231 enum reg_class
2232 s390_preferred_reload_class (rtx op, enum reg_class class)
2233 {
2234   switch (GET_CODE (op))
2235     {
2236       /* Constants we cannot reload must be forced into the
2237          literal pool.  */
2238
2239       case CONST_DOUBLE:
2240       case CONST_INT:
2241         if (legitimate_reload_constant_p (op))
2242           return class;
2243         else
2244           return NO_REGS;
2245
2246       /* If a symbolic constant or a PLUS is reloaded,
2247          it is most likely being used as an address, so
2248          prefer ADDR_REGS.  If 'class' is not a superset
2249          of ADDR_REGS, e.g. FP_REGS, reject this reload.  */
2250       case PLUS:
2251       case LABEL_REF:
2252       case SYMBOL_REF:
2253       case CONST:
2254         if (reg_class_subset_p (ADDR_REGS, class))
2255           return ADDR_REGS;
2256         else
2257           return NO_REGS;
2258
2259       default:
2260         break;
2261     }
2262
2263   return class;
2264 }
2265
2266 /* Return the register class of a scratch register needed to
2267    load IN into a register of class CLASS in MODE.
2268
2269    We need a temporary when loading a PLUS expression which
2270    is not a legitimate operand of the LOAD ADDRESS instruction.  */
2271
2272 enum reg_class
2273 s390_secondary_input_reload_class (enum reg_class class ATTRIBUTE_UNUSED,
2274                                    enum machine_mode mode, rtx in)
2275 {
2276   if (s390_plus_operand (in, mode))
2277     return ADDR_REGS;
2278
2279   return NO_REGS;
2280 }
2281
2282 /* Return the register class of a scratch register needed to
2283    store a register of class CLASS in MODE into OUT:
2284
2285    We need a temporary when storing a double-word to a
2286    non-offsettable memory address.  */
2287
2288 enum reg_class
2289 s390_secondary_output_reload_class (enum reg_class class,
2290                                     enum machine_mode mode, rtx out)
2291 {
2292   if ((TARGET_64BIT ? mode == TImode
2293                     : (mode == DImode || mode == DFmode))
2294       && reg_classes_intersect_p (GENERAL_REGS, class)
2295       && GET_CODE (out) == MEM
2296       && !offsettable_memref_p (out)
2297       && !s_operand (out, VOIDmode))
2298     return ADDR_REGS;
2299
2300   return NO_REGS;
2301 }
2302
2303 /* Return true if OP is a PLUS that is not a legitimate
2304    operand for the LA instruction.
2305    OP is the current operation.
2306    MODE is the current operation mode.  */
2307
2308 int
2309 s390_plus_operand (register rtx op, enum machine_mode mode)
2310 {
2311   if (!check_mode (op, &mode) || mode != Pmode)
2312     return FALSE;
2313
2314   if (GET_CODE (op) != PLUS)
2315     return FALSE;
2316
2317   if (legitimate_la_operand_p (op))
2318     return FALSE;
2319
2320   return TRUE;
2321 }
2322
2323 /* Generate code to load SRC, which is PLUS that is not a
2324    legitimate operand for the LA instruction, into TARGET.
2325    SCRATCH may be used as scratch register.  */
2326
2327 void
2328 s390_expand_plus_operand (register rtx target, register rtx src,
2329                           register rtx scratch)
2330 {
2331   rtx sum1, sum2;
2332   struct s390_address ad;
2333
2334   /* src must be a PLUS; get its two operands.  */
2335   if (GET_CODE (src) != PLUS || GET_MODE (src) != Pmode)
2336     abort ();
2337
2338   /* Check if any of the two operands is already scheduled
2339      for replacement by reload.  This can happen e.g. when
2340      float registers occur in an address.  */
2341   sum1 = find_replacement (&XEXP (src, 0));
2342   sum2 = find_replacement (&XEXP (src, 1));
2343   src = gen_rtx_PLUS (Pmode, sum1, sum2);
2344
2345   /* If the address is already strictly valid, there's nothing to do.  */
2346   if (!s390_decompose_address (src, &ad)
2347       || (ad.base && !REG_OK_FOR_BASE_STRICT_P (ad.base))
2348       || (ad.indx && !REG_OK_FOR_INDEX_STRICT_P (ad.indx)))
2349     {
2350       /* Otherwise, one of the operands cannot be an address register;
2351          we reload its value into the scratch register.  */
2352       if (true_regnum (sum1) < 1 || true_regnum (sum1) > 15)
2353         {
2354           emit_move_insn (scratch, sum1);
2355           sum1 = scratch;
2356         }
2357       if (true_regnum (sum2) < 1 || true_regnum (sum2) > 15)
2358         {
2359           emit_move_insn (scratch, sum2);
2360           sum2 = scratch;
2361         }
2362
2363       /* According to the way these invalid addresses are generated
2364          in reload.c, it should never happen (at least on s390) that
2365          *neither* of the PLUS components, after find_replacements
2366          was applied, is an address register.  */
2367       if (sum1 == scratch && sum2 == scratch)
2368         {
2369           debug_rtx (src);
2370           abort ();
2371         }
2372
2373       src = gen_rtx_PLUS (Pmode, sum1, sum2);
2374     }
2375
2376   /* Emit the LOAD ADDRESS pattern.  Note that reload of PLUS
2377      is only ever performed on addresses, so we can mark the
2378      sum as legitimate for LA in any case.  */
2379   s390_load_address (target, src);
2380 }
2381
2382
2383 /* Decompose a RTL expression ADDR for a memory address into
2384    its components, returned in OUT.
2385
2386    Returns 0 if ADDR is not a valid memory address, nonzero
2387    otherwise.  If OUT is NULL, don't return the components,
2388    but check for validity only.
2389
2390    Note: Only addresses in canonical form are recognized.
2391    LEGITIMIZE_ADDRESS should convert non-canonical forms to the
2392    canonical form so that they will be recognized.  */
2393
2394 static int
2395 s390_decompose_address (register rtx addr, struct s390_address *out)
2396 {
2397   HOST_WIDE_INT offset = 0;
2398   rtx base = NULL_RTX;
2399   rtx indx = NULL_RTX;
2400   rtx disp = NULL_RTX;
2401   rtx orig_disp;
2402   int pointer = FALSE;
2403   int base_ptr = FALSE;
2404   int indx_ptr = FALSE;
2405
2406   /* Decompose address into base + index + displacement.  */
2407
2408   if (GET_CODE (addr) == REG || GET_CODE (addr) == UNSPEC)
2409     base = addr;
2410
2411   else if (GET_CODE (addr) == PLUS)
2412     {
2413       rtx op0 = XEXP (addr, 0);
2414       rtx op1 = XEXP (addr, 1);
2415       enum rtx_code code0 = GET_CODE (op0);
2416       enum rtx_code code1 = GET_CODE (op1);
2417
2418       if (code0 == REG || code0 == UNSPEC)
2419         {
2420           if (code1 == REG || code1 == UNSPEC)
2421             {
2422               indx = op0;       /* index + base */
2423               base = op1;
2424             }
2425
2426           else
2427             {
2428               base = op0;       /* base + displacement */
2429               disp = op1;
2430             }
2431         }
2432
2433       else if (code0 == PLUS)
2434         {
2435           indx = XEXP (op0, 0); /* index + base + disp */
2436           base = XEXP (op0, 1);
2437           disp = op1;
2438         }
2439
2440       else
2441         {
2442           return FALSE;
2443         }
2444     }
2445
2446   else
2447     disp = addr;                /* displacement */
2448
2449   /* Extract integer part of displacement.  */
2450   orig_disp = disp;
2451   if (disp)
2452     {
2453       if (GET_CODE (disp) == CONST_INT)
2454         {
2455           offset = INTVAL (disp);
2456           disp = NULL_RTX;
2457         }
2458       else if (GET_CODE (disp) == CONST
2459                && GET_CODE (XEXP (disp, 0)) == PLUS
2460                && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
2461         {
2462           offset = INTVAL (XEXP (XEXP (disp, 0), 1));
2463           disp = XEXP (XEXP (disp, 0), 0);
2464         }
2465     }
2466
2467   /* Strip off CONST here to avoid special case tests later.  */
2468   if (disp && GET_CODE (disp) == CONST)
2469     disp = XEXP (disp, 0);
2470
2471   /* We can convert literal pool addresses to
2472      displacements by basing them off the base register.  */
2473   if (disp && GET_CODE (disp) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (disp))
2474     {
2475       /* Either base or index must be free to hold the base register.  */
2476       if (!base)
2477         base = gen_rtx_REG (Pmode, BASE_REGNUM);
2478       else if (!indx)
2479         indx = gen_rtx_REG (Pmode, BASE_REGNUM);
2480       else
2481         return FALSE;
2482
2483       /* Mark up the displacement.  */
2484       disp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, disp),
2485                              UNSPEC_LTREL_OFFSET);
2486     }
2487
2488   /* Validate base register.  */
2489   if (base)
2490     {
2491       if (GET_CODE (base) == UNSPEC)
2492         switch (XINT (base, 1))
2493           {
2494           case UNSPEC_LTREF:
2495             if (!disp)
2496               disp = gen_rtx_UNSPEC (Pmode, 
2497                                      gen_rtvec (1, XVECEXP (base, 0, 0)),
2498                                      UNSPEC_LTREL_OFFSET);
2499             else
2500               return FALSE;
2501
2502             base = gen_rtx_REG (Pmode, BASE_REGNUM);
2503             break;
2504
2505           case UNSPEC_LTREL_BASE:
2506             base = gen_rtx_REG (Pmode, BASE_REGNUM);
2507             break;
2508
2509           default:
2510             return FALSE;
2511           }
2512
2513       if (GET_CODE (base) != REG || GET_MODE (base) != Pmode)
2514         return FALSE;
2515
2516       if (REGNO (base) == BASE_REGNUM
2517           || REGNO (base) == STACK_POINTER_REGNUM
2518           || REGNO (base) == FRAME_POINTER_REGNUM
2519           || ((reload_completed || reload_in_progress)
2520               && frame_pointer_needed
2521               && REGNO (base) == HARD_FRAME_POINTER_REGNUM)
2522           || REGNO (base) == ARG_POINTER_REGNUM
2523           || (flag_pic
2524               && REGNO (base) == PIC_OFFSET_TABLE_REGNUM))
2525         pointer = base_ptr = TRUE;
2526     }
2527
2528   /* Validate index register.  */
2529   if (indx)
2530     {
2531       if (GET_CODE (indx) == UNSPEC)
2532         switch (XINT (indx, 1))
2533           {
2534           case UNSPEC_LTREF:
2535             if (!disp)
2536               disp = gen_rtx_UNSPEC (Pmode, 
2537                                      gen_rtvec (1, XVECEXP (indx, 0, 0)),
2538                                      UNSPEC_LTREL_OFFSET);
2539             else
2540               return FALSE;
2541
2542             indx = gen_rtx_REG (Pmode, BASE_REGNUM);
2543             break;
2544
2545           case UNSPEC_LTREL_BASE:
2546             indx = gen_rtx_REG (Pmode, BASE_REGNUM);
2547             break;
2548
2549           default:
2550             return FALSE;
2551           }
2552
2553       if (GET_CODE (indx) != REG || GET_MODE (indx) != Pmode)
2554         return FALSE;
2555
2556       if (REGNO (indx) == BASE_REGNUM
2557           || REGNO (indx) == STACK_POINTER_REGNUM
2558           || REGNO (indx) == FRAME_POINTER_REGNUM
2559           || ((reload_completed || reload_in_progress)
2560               && frame_pointer_needed
2561               && REGNO (indx) == HARD_FRAME_POINTER_REGNUM)
2562           || REGNO (indx) == ARG_POINTER_REGNUM
2563           || (flag_pic
2564               && REGNO (indx) == PIC_OFFSET_TABLE_REGNUM))
2565         pointer = indx_ptr = TRUE;
2566     }
2567
2568   /* Prefer to use pointer as base, not index.  */
2569   if (base && indx && !base_ptr
2570       && (indx_ptr || (!REG_POINTER (base) && REG_POINTER (indx))))
2571     {
2572       rtx tmp = base;
2573       base = indx;
2574       indx = tmp;
2575     }
2576
2577   /* Validate displacement.  */
2578   if (!disp)
2579     {
2580       /* If the argument pointer or the return address pointer are involved,
2581          the displacement will change later anyway as the virtual registers get
2582          eliminated.  This could make a valid displacement invalid, but it is 
2583          more likely to make an invalid displacement valid, because we sometimes
2584          access the register save area via negative offsets to one of those 
2585          registers.
2586          Thus we don't check the displacement for validity here.  If after
2587          elimination the displacement turns out to be invalid after all,
2588          this is fixed up by reload in any case.  */
2589       if (base != arg_pointer_rtx 
2590           && indx != arg_pointer_rtx 
2591           && base != return_address_pointer_rtx 
2592           && indx != return_address_pointer_rtx)
2593         if (!DISP_IN_RANGE (offset))
2594           return FALSE;
2595     }
2596   else
2597     {
2598       /* All the special cases are pointers.  */
2599       pointer = TRUE;
2600
2601       /* In the small-PIC case, the linker converts @GOT
2602          and @GOTNTPOFF offsets to possible displacements.  */
2603       if (GET_CODE (disp) == UNSPEC
2604           && (XINT (disp, 1) == UNSPEC_GOT
2605               || XINT (disp, 1) == UNSPEC_GOTNTPOFF)
2606           && offset == 0
2607           && flag_pic == 1)
2608         {
2609           ;
2610         }
2611
2612       /* Accept chunkified literal pool symbol references.  */
2613       else if (GET_CODE (disp) == MINUS
2614                && GET_CODE (XEXP (disp, 0)) == LABEL_REF
2615                && GET_CODE (XEXP (disp, 1)) == LABEL_REF)
2616         {
2617           ;
2618         }
2619
2620       /* Accept literal pool references.  */
2621       else if (GET_CODE (disp) == UNSPEC
2622                && XINT (disp, 1) == UNSPEC_LTREL_OFFSET)
2623         {
2624           orig_disp = gen_rtx_CONST (Pmode, disp);
2625           if (offset)
2626             {
2627               /* If we have an offset, make sure it does not
2628                  exceed the size of the constant pool entry.  */
2629               rtx sym = XVECEXP (disp, 0, 0);
2630               if (offset >= GET_MODE_SIZE (get_pool_mode (sym)))
2631                 return FALSE;
2632
2633               orig_disp = plus_constant (orig_disp, offset);
2634             }
2635         }
2636
2637       else
2638         return FALSE;
2639     }
2640
2641   if (!base && !indx)
2642     pointer = TRUE;
2643
2644   if (out)
2645     {
2646       out->base = base;
2647       out->indx = indx;
2648       out->disp = orig_disp;
2649       out->pointer = pointer;
2650     }
2651
2652   return TRUE;
2653 }
2654
2655 /* Return nonzero if ADDR is a valid memory address.
2656    STRICT specifies whether strict register checking applies.  */
2657
2658 int
2659 legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2660                       register rtx addr, int strict)
2661 {
2662   struct s390_address ad;
2663   if (!s390_decompose_address (addr, &ad))
2664     return FALSE;
2665
2666   if (strict)
2667     {
2668       if (ad.base && !REG_OK_FOR_BASE_STRICT_P (ad.base))
2669         return FALSE;
2670       if (ad.indx && !REG_OK_FOR_INDEX_STRICT_P (ad.indx))
2671         return FALSE;
2672     }
2673   else
2674     {
2675       if (ad.base && !REG_OK_FOR_BASE_NONSTRICT_P (ad.base))
2676         return FALSE;
2677       if (ad.indx && !REG_OK_FOR_INDEX_NONSTRICT_P (ad.indx))
2678         return FALSE;
2679     }
2680
2681   return TRUE;
2682 }
2683
2684 /* Return 1 if OP is a valid operand for the LA instruction.
2685    In 31-bit, we need to prove that the result is used as an
2686    address, as LA performs only a 31-bit addition.  */
2687
2688 int
2689 legitimate_la_operand_p (register rtx op)
2690 {
2691   struct s390_address addr;
2692   if (!s390_decompose_address (op, &addr))
2693     return FALSE;
2694
2695   if (TARGET_64BIT || addr.pointer)
2696     return TRUE;
2697
2698   return FALSE;
2699 }
2700
2701 /* Return 1 if it is valid *and* preferable to use LA to
2702    compute the sum of OP1 and OP2.  */
2703
2704 int
2705 preferred_la_operand_p (rtx op1, rtx op2)
2706 {
2707   struct s390_address addr;
2708
2709   if (op2 != const0_rtx)
2710     op1 = gen_rtx_PLUS (Pmode, op1, op2);
2711
2712   if (!s390_decompose_address (op1, &addr))
2713     return FALSE;
2714   if (addr.base && !REG_OK_FOR_BASE_STRICT_P (addr.base))
2715     return FALSE;
2716   if (addr.indx && !REG_OK_FOR_INDEX_STRICT_P (addr.indx))
2717     return FALSE;
2718
2719   if (!TARGET_64BIT && !addr.pointer)
2720     return FALSE;
2721
2722   if (addr.pointer)
2723     return TRUE;
2724
2725   if ((addr.base && REG_P (addr.base) && REG_POINTER (addr.base))
2726       || (addr.indx && REG_P (addr.indx) && REG_POINTER (addr.indx)))
2727     return TRUE;
2728
2729   return FALSE;
2730 }
2731
2732 /* Emit a forced load-address operation to load SRC into DST.
2733    This will use the LOAD ADDRESS instruction even in situations
2734    where legitimate_la_operand_p (SRC) returns false.  */
2735
2736 void
2737 s390_load_address (rtx dst, rtx src)
2738 {
2739   if (TARGET_64BIT)
2740     emit_move_insn (dst, src);
2741   else
2742     emit_insn (gen_force_la_31 (dst, src));
2743 }
2744
2745 /* Return a legitimate reference for ORIG (an address) using the
2746    register REG.  If REG is 0, a new pseudo is generated.
2747
2748    There are two types of references that must be handled:
2749
2750    1. Global data references must load the address from the GOT, via
2751       the PIC reg.  An insn is emitted to do this load, and the reg is
2752       returned.
2753
2754    2. Static data references, constant pool addresses, and code labels
2755       compute the address as an offset from the GOT, whose base is in
2756       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
2757       differentiate them from global data objects.  The returned
2758       address is the PIC reg + an unspec constant.
2759
2760    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
2761    reg also appears in the address.  */
2762
2763 rtx
2764 legitimize_pic_address (rtx orig, rtx reg)
2765 {
2766   rtx addr = orig;
2767   rtx new = orig;
2768   rtx base;
2769
2770   if (GET_CODE (addr) == LABEL_REF
2771       || (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (addr)))
2772     {
2773       /* This is a local symbol.  */
2774       if (TARGET_CPU_ZARCH && larl_operand (addr, VOIDmode))
2775         {
2776           /* Access local symbols PC-relative via LARL.
2777              This is the same as in the non-PIC case, so it is
2778              handled automatically ...  */
2779         }
2780       else
2781         {
2782           /* Access local symbols relative to the GOT.  */
2783
2784           rtx temp = reg? reg : gen_reg_rtx (Pmode);
2785
2786           if (reload_in_progress || reload_completed)
2787             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2788
2789           addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
2790           addr = gen_rtx_CONST (Pmode, addr);
2791           addr = force_const_mem (Pmode, addr);
2792           emit_move_insn (temp, addr);
2793
2794           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
2795           if (reg != 0)
2796             {
2797               emit_move_insn (reg, new);
2798               new = reg;
2799             }
2800         }
2801     }
2802   else if (GET_CODE (addr) == SYMBOL_REF)
2803     {
2804       if (reg == 0)
2805         reg = gen_reg_rtx (Pmode);
2806
2807       if (flag_pic == 1)
2808         {
2809           /* Assume GOT offset < 4k.  This is handled the same way
2810              in both 31- and 64-bit code (@GOT).  */
2811
2812           if (reload_in_progress || reload_completed)
2813             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2814
2815           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
2816           new = gen_rtx_CONST (Pmode, new);
2817           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
2818           new = gen_const_mem (Pmode, new);
2819           emit_move_insn (reg, new);
2820           new = reg;
2821         }
2822       else if (TARGET_CPU_ZARCH)
2823         {
2824           /* If the GOT offset might be >= 4k, we determine the position
2825              of the GOT entry via a PC-relative LARL (@GOTENT).  */
2826
2827           rtx temp = gen_reg_rtx (Pmode);
2828
2829           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTENT);
2830           new = gen_rtx_CONST (Pmode, new);
2831           emit_move_insn (temp, new);
2832
2833           new = gen_const_mem (Pmode, temp);
2834           emit_move_insn (reg, new);
2835           new = reg;
2836         }
2837       else
2838         {
2839           /* If the GOT offset might be >= 4k, we have to load it
2840              from the literal pool (@GOT).  */
2841
2842           rtx temp = gen_reg_rtx (Pmode);
2843
2844           if (reload_in_progress || reload_completed)
2845             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2846
2847           addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
2848           addr = gen_rtx_CONST (Pmode, addr);
2849           addr = force_const_mem (Pmode, addr);
2850           emit_move_insn (temp, addr);
2851
2852           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
2853           new = gen_const_mem (Pmode, new);
2854           emit_move_insn (reg, new);
2855           new = reg;
2856         }
2857     }
2858   else
2859     {
2860       if (GET_CODE (addr) == CONST)
2861         {
2862           addr = XEXP (addr, 0);
2863           if (GET_CODE (addr) == UNSPEC)
2864             {
2865               if (XVECLEN (addr, 0) != 1)
2866                 abort ();
2867               switch (XINT (addr, 1))
2868                 {
2869                   /* If someone moved a GOT-relative UNSPEC
2870                      out of the literal pool, force them back in.  */
2871                   case UNSPEC_GOTOFF:
2872                   case UNSPEC_PLTOFF:
2873                     new = force_const_mem (Pmode, orig);
2874                     break;
2875
2876                   /* @GOT is OK as is if small.  */
2877                   case UNSPEC_GOT:
2878                     if (flag_pic == 2)
2879                       new = force_const_mem (Pmode, orig);
2880                     break;
2881
2882                   /* @GOTENT is OK as is.  */
2883                   case UNSPEC_GOTENT:
2884                     break;
2885
2886                   /* @PLT is OK as is on 64-bit, must be converted to
2887                      GOT-relative @PLTOFF on 31-bit.  */
2888                   case UNSPEC_PLT:
2889                     if (!TARGET_CPU_ZARCH)
2890                       {
2891                         rtx temp = reg? reg : gen_reg_rtx (Pmode);
2892
2893                         if (reload_in_progress || reload_completed)
2894                           regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2895
2896                         addr = XVECEXP (addr, 0, 0);
2897                         addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr),
2898                                                UNSPEC_PLTOFF);
2899                         addr = gen_rtx_CONST (Pmode, addr);
2900                         addr = force_const_mem (Pmode, addr);
2901                         emit_move_insn (temp, addr);
2902
2903                         new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
2904                         if (reg != 0)
2905                           {
2906                             emit_move_insn (reg, new);
2907                             new = reg;
2908                           }
2909                       }
2910                     break;
2911
2912                   /* Everything else cannot happen.  */
2913                   default:
2914                     abort ();
2915                 }
2916             }
2917           else if (GET_CODE (addr) != PLUS)
2918             abort ();
2919         }
2920       if (GET_CODE (addr) == PLUS)
2921         {
2922           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
2923           /* Check first to see if this is a constant offset
2924              from a local symbol reference.  */
2925           if ((GET_CODE (op0) == LABEL_REF
2926                 || (GET_CODE (op0) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (op0)))
2927               && GET_CODE (op1) == CONST_INT)
2928             {
2929               if (TARGET_CPU_ZARCH && larl_operand (op0, VOIDmode))
2930                 {
2931                   if (INTVAL (op1) & 1)
2932                     {
2933                       /* LARL can't handle odd offsets, so emit a
2934                          pair of LARL and LA.  */
2935                       rtx temp = reg? reg : gen_reg_rtx (Pmode);
2936
2937                       if (!DISP_IN_RANGE (INTVAL (op1)))
2938                         {
2939                           int even = INTVAL (op1) - 1;
2940                           op0 = gen_rtx_PLUS (Pmode, op0, GEN_INT (even));
2941                           op0 = gen_rtx_CONST (Pmode, op0);
2942                           op1 = const1_rtx;
2943                         }
2944
2945                       emit_move_insn (temp, op0);
2946                       new = gen_rtx_PLUS (Pmode, temp, op1);
2947
2948                       if (reg != 0)
2949                         {
2950                           emit_move_insn (reg, new);
2951                           new = reg;
2952                         }
2953                     }
2954                   else
2955                     {
2956                       /* If the offset is even, we can just use LARL.
2957                          This will happen automatically.  */
2958                     }
2959                 }
2960               else
2961                 {
2962                   /* Access local symbols relative to the GOT.  */
2963
2964                   rtx temp = reg? reg : gen_reg_rtx (Pmode);
2965
2966                   if (reload_in_progress || reload_completed)
2967                     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
2968
2969                   addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
2970                                          UNSPEC_GOTOFF);
2971                   addr = gen_rtx_PLUS (Pmode, addr, op1);
2972                   addr = gen_rtx_CONST (Pmode, addr);
2973                   addr = force_const_mem (Pmode, addr);
2974                   emit_move_insn (temp, addr);
2975
2976                   new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
2977                   if (reg != 0)
2978                     {
2979                       emit_move_insn (reg, new);
2980                       new = reg;
2981                     }
2982                 }
2983             }
2984
2985           /* Now, check whether it is a GOT relative symbol plus offset
2986              that was pulled out of the literal pool.  Force it back in.  */
2987
2988           else if (GET_CODE (op0) == UNSPEC
2989                    && GET_CODE (op1) == CONST_INT
2990                    && XINT (op0, 1) == UNSPEC_GOTOFF)
2991             {
2992               if (XVECLEN (op0, 0) != 1)
2993                 abort ();
2994
2995               new = force_const_mem (Pmode, orig);
2996             }
2997
2998           /* Otherwise, compute the sum.  */
2999           else
3000             {
3001               base = legitimize_pic_address (XEXP (addr, 0), reg);
3002               new  = legitimize_pic_address (XEXP (addr, 1),
3003                                              base == reg ? NULL_RTX : reg);
3004               if (GET_CODE (new) == CONST_INT)
3005                 new = plus_constant (base, INTVAL (new));
3006               else
3007                 {
3008                   if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
3009                     {
3010                       base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
3011                       new = XEXP (new, 1);
3012                     }
3013                   new = gen_rtx_PLUS (Pmode, base, new);
3014                 }
3015
3016               if (GET_CODE (new) == CONST)
3017                 new = XEXP (new, 0);
3018               new = force_operand (new, 0);
3019             }
3020         }
3021     }
3022   return new;
3023 }
3024
3025 /* Load the thread pointer into a register.  */
3026
3027 static rtx
3028 get_thread_pointer (void)
3029 {
3030   rtx tp;
3031
3032   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
3033   tp = force_reg (Pmode, tp);
3034   mark_reg_pointer (tp, BITS_PER_WORD);
3035
3036   return tp;
3037 }
3038
3039 /* Emit a tls call insn. The call target is the SYMBOL_REF stored
3040    in s390_tls_symbol which always refers to __tls_get_offset.
3041    The returned offset is written to RESULT_REG and an USE rtx is
3042    generated for TLS_CALL.  */
3043
3044 static GTY(()) rtx s390_tls_symbol;
3045
3046 static void
3047 s390_emit_tls_call_insn (rtx result_reg, rtx tls_call)
3048 {
3049   rtx insn;
3050
3051   if (!flag_pic)
3052     abort ();
3053
3054   if (!s390_tls_symbol)
3055     s390_tls_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tls_get_offset");
3056
3057   insn = s390_emit_call (s390_tls_symbol, tls_call, result_reg,
3058                          gen_rtx_REG (Pmode, RETURN_REGNUM));
3059
3060   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), result_reg);
3061   CONST_OR_PURE_CALL_P (insn) = 1;
3062 }
3063
3064 /* ADDR contains a thread-local SYMBOL_REF.  Generate code to compute
3065    this (thread-local) address.  REG may be used as temporary.  */
3066
3067 static rtx
3068 legitimize_tls_address (rtx addr, rtx reg)
3069 {
3070   rtx new, tls_call, temp, base, r2, insn;
3071
3072   if (GET_CODE (addr) == SYMBOL_REF)
3073     switch (tls_symbolic_operand (addr))
3074       {
3075       case TLS_MODEL_GLOBAL_DYNAMIC:
3076         start_sequence ();
3077         r2 = gen_rtx_REG (Pmode, 2);
3078         tls_call = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_TLSGD);
3079         new = gen_rtx_CONST (Pmode, tls_call);
3080         new = force_const_mem (Pmode, new);
3081         emit_move_insn (r2, new);
3082         s390_emit_tls_call_insn (r2, tls_call);
3083         insn = get_insns ();
3084         end_sequence ();
3085
3086         new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_NTPOFF);
3087         temp = gen_reg_rtx (Pmode);
3088         emit_libcall_block (insn, temp, r2, new);
3089
3090         new = gen_rtx_PLUS (Pmode, get_thread_pointer (), temp);
3091         if (reg != 0)
3092           {
3093             s390_load_address (reg, new);
3094             new = reg;
3095           }
3096         break;
3097
3098       case TLS_MODEL_LOCAL_DYNAMIC:
3099         start_sequence ();
3100         r2 = gen_rtx_REG (Pmode, 2);
3101         tls_call = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TLSLDM);
3102         new = gen_rtx_CONST (Pmode, tls_call);
3103         new = force_const_mem (Pmode, new);
3104         emit_move_insn (r2, new);
3105         s390_emit_tls_call_insn (r2, tls_call);
3106         insn = get_insns ();
3107         end_sequence ();
3108
3109         new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TLSLDM_NTPOFF);
3110         temp = gen_reg_rtx (Pmode);
3111         emit_libcall_block (insn, temp, r2, new);
3112
3113         new = gen_rtx_PLUS (Pmode, get_thread_pointer (), temp);
3114         base = gen_reg_rtx (Pmode);
3115         s390_load_address (base, new);
3116
3117         new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_DTPOFF);
3118         new = gen_rtx_CONST (Pmode, new);
3119         new = force_const_mem (Pmode, new);
3120         temp = gen_reg_rtx (Pmode);
3121         emit_move_insn (temp, new);
3122
3123         new = gen_rtx_PLUS (Pmode, base, temp);
3124         if (reg != 0)
3125           {
3126             s390_load_address (reg, new);
3127             new = reg;
3128           }
3129         break;
3130
3131       case TLS_MODEL_INITIAL_EXEC:
3132         if (flag_pic == 1)
3133           {
3134             /* Assume GOT offset < 4k.  This is handled the same way
3135                in both 31- and 64-bit code.  */
3136
3137             if (reload_in_progress || reload_completed)
3138               regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
3139
3140             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTNTPOFF);
3141             new = gen_rtx_CONST (Pmode, new);
3142             new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
3143             new = gen_const_mem (Pmode, new);
3144             temp = gen_reg_rtx (Pmode);
3145             emit_move_insn (temp, new);
3146           }
3147         else if (TARGET_CPU_ZARCH)
3148           {
3149             /* If the GOT offset might be >= 4k, we determine the position
3150                of the GOT entry via a PC-relative LARL.  */
3151
3152             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_INDNTPOFF);
3153             new = gen_rtx_CONST (Pmode, new);
3154             temp = gen_reg_rtx (Pmode);
3155             emit_move_insn (temp, new);
3156
3157             new = gen_const_mem (Pmode, temp);
3158             temp = gen_reg_rtx (Pmode);
3159             emit_move_insn (temp, new);
3160           }
3161         else if (flag_pic)
3162           {
3163             /* If the GOT offset might be >= 4k, we have to load it
3164                from the literal pool.  */
3165
3166             if (reload_in_progress || reload_completed)
3167               regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
3168
3169             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTNTPOFF);
3170             new = gen_rtx_CONST (Pmode, new);
3171             new = force_const_mem (Pmode, new);
3172             temp = gen_reg_rtx (Pmode);
3173             emit_move_insn (temp, new);
3174
3175             new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, temp);
3176             new = gen_const_mem (Pmode, new);
3177
3178             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, new, addr), UNSPEC_TLS_LOAD);
3179             temp = gen_reg_rtx (Pmode);
3180             emit_insn (gen_rtx_SET (Pmode, temp, new));
3181           }
3182         else
3183           {
3184             /* In position-dependent code, load the absolute address of
3185                the GOT entry from the literal pool.  */
3186
3187             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_INDNTPOFF);
3188             new = gen_rtx_CONST (Pmode, new);
3189             new = force_const_mem (Pmode, new);
3190             temp = gen_reg_rtx (Pmode);
3191             emit_move_insn (temp, new);
3192
3193             new = temp;
3194             new = gen_const_mem (Pmode, new);
3195             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, new, addr), UNSPEC_TLS_LOAD);
3196             temp = gen_reg_rtx (Pmode);
3197             emit_insn (gen_rtx_SET (Pmode, temp, new));
3198           }
3199
3200         new = gen_rtx_PLUS (Pmode, get_thread_pointer (), temp);
3201         if (reg != 0)
3202           {
3203             s390_load_address (reg, new);
3204             new = reg;
3205           }
3206         break;
3207
3208       case TLS_MODEL_LOCAL_EXEC:
3209         new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_NTPOFF);
3210         new = gen_rtx_CONST (Pmode, new);
3211         new = force_const_mem (Pmode, new);
3212         temp = gen_reg_rtx (Pmode);
3213         emit_move_insn (temp, new);
3214
3215         new = gen_rtx_PLUS (Pmode, get_thread_pointer (), temp);
3216         if (reg != 0)
3217           {
3218             s390_load_address (reg, new);
3219             new = reg;
3220           }
3221         break;
3222
3223       default:
3224         abort ();
3225       }
3226
3227   else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == UNSPEC)
3228     {
3229       switch (XINT (XEXP (addr, 0), 1))
3230         {
3231         case UNSPEC_INDNTPOFF:
3232           if (TARGET_CPU_ZARCH)
3233             new = addr;
3234           else
3235             abort ();
3236           break;
3237
3238         default:
3239           abort ();
3240         }
3241     }
3242
3243   else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS
3244            && GET_CODE (XEXP (XEXP (addr, 0), 1)) == CONST_INT)
3245     {
3246       new = XEXP (XEXP (addr, 0), 0);
3247       if (GET_CODE (new) != SYMBOL_REF)
3248         new = gen_rtx_CONST (Pmode, new);
3249
3250       new = legitimize_tls_address (new, reg);
3251       new = plus_constant (new, INTVAL (XEXP (XEXP (addr, 0), 1)));
3252       new = force_operand (new, 0);
3253     }
3254
3255   else
3256     abort ();  /* for now ... */
3257
3258   return new;
3259 }
3260
3261 /* Emit insns to move operands[1] into operands[0].  */
3262
3263 void
3264 emit_symbolic_move (rtx *operands)
3265 {
3266   rtx temp = no_new_pseudos ? operands[0] : gen_reg_rtx (Pmode);
3267
3268   if (GET_CODE (operands[0]) == MEM)
3269     operands[1] = force_reg (Pmode, operands[1]);
3270   else if (TLS_SYMBOLIC_CONST (operands[1]))
3271     operands[1] = legitimize_tls_address (operands[1], temp);
3272   else if (flag_pic)
3273     operands[1] = legitimize_pic_address (operands[1], temp);
3274 }
3275
3276 /* Try machine-dependent ways of modifying an illegitimate address X
3277    to be legitimate.  If we find one, return the new, valid address.
3278
3279    OLDX is the address as it was before break_out_memory_refs was called.
3280    In some cases it is useful to look at this to decide what needs to be done.
3281
3282    MODE is the mode of the operand pointed to by X.
3283
3284    When -fpic is used, special handling is needed for symbolic references.
3285    See comments by legitimize_pic_address for details.  */
3286
3287 rtx
3288 legitimize_address (register rtx x, register rtx oldx ATTRIBUTE_UNUSED,
3289                     enum machine_mode mode ATTRIBUTE_UNUSED)
3290 {
3291   rtx constant_term = const0_rtx;
3292
3293   if (TLS_SYMBOLIC_CONST (x))
3294     {
3295       x = legitimize_tls_address (x, 0);
3296
3297       if (legitimate_address_p (mode, x, FALSE))
3298         return x;
3299     }
3300   else if (flag_pic)
3301     {
3302       if (SYMBOLIC_CONST (x)
3303           || (GET_CODE (x) == PLUS
3304               && (SYMBOLIC_CONST (XEXP (x, 0))
3305                   || SYMBOLIC_CONST (XEXP (x, 1)))))
3306           x = legitimize_pic_address (x, 0);
3307
3308       if (legitimate_address_p (mode, x, FALSE))
3309         return x;
3310     }
3311
3312   x = eliminate_constant_term (x, &constant_term);
3313
3314   /* Optimize loading of large displacements by splitting them
3315      into the multiple of 4K and the rest; this allows the
3316      former to be CSE'd if possible.
3317
3318      Don't do this if the displacement is added to a register
3319      pointing into the stack frame, as the offsets will
3320      change later anyway.  */
3321
3322   if (GET_CODE (constant_term) == CONST_INT
3323       && !TARGET_LONG_DISPLACEMENT
3324       && !DISP_IN_RANGE (INTVAL (constant_term))
3325       && !(REG_P (x) && REGNO_PTR_FRAME_P (REGNO (x))))
3326     {
3327       HOST_WIDE_INT lower = INTVAL (constant_term) & 0xfff;
3328       HOST_WIDE_INT upper = INTVAL (constant_term) ^ lower;
3329
3330       rtx temp = gen_reg_rtx (Pmode);
3331       rtx val  = force_operand (GEN_INT (upper), temp);
3332       if (val != temp)
3333         emit_move_insn (temp, val);
3334
3335       x = gen_rtx_PLUS (Pmode, x, temp);
3336       constant_term = GEN_INT (lower);
3337     }
3338
3339   if (GET_CODE (x) == PLUS)
3340     {
3341       if (GET_CODE (XEXP (x, 0)) == REG)
3342         {
3343           register rtx temp = gen_reg_rtx (Pmode);
3344           register rtx val  = force_operand (XEXP (x, 1), temp);
3345           if (val != temp)
3346             emit_move_insn (temp, val);
3347
3348           x = gen_rtx_PLUS (Pmode, XEXP (x, 0), temp);
3349         }
3350
3351       else if (GET_CODE (XEXP (x, 1)) == REG)
3352         {
3353           register rtx temp = gen_reg_rtx (Pmode);
3354           register rtx val  = force_operand (XEXP (x, 0), temp);
3355           if (val != temp)
3356             emit_move_insn (temp, val);
3357
3358           x = gen_rtx_PLUS (Pmode, temp, XEXP (x, 1));
3359         }
3360     }
3361
3362   if (constant_term != const0_rtx)
3363     x = gen_rtx_PLUS (Pmode, x, constant_term);
3364
3365   return x;
3366 }
3367
3368 /* Try a machine-dependent way of reloading an illegitimate address AD
3369    operand.  If we find one, push the reload and and return the new address.
3370
3371    MODE is the mode of the enclosing MEM.  OPNUM is the operand number
3372    and TYPE is the reload type of the current reload.  */
3373
3374 rtx 
3375 legitimize_reload_address (rtx ad, enum machine_mode mode ATTRIBUTE_UNUSED,
3376                            int opnum, int type)
3377 {
3378   if (!optimize || TARGET_LONG_DISPLACEMENT)
3379     return NULL_RTX;
3380
3381   if (GET_CODE (ad) == PLUS)
3382     {
3383       rtx tem = simplify_binary_operation (PLUS, Pmode,
3384                                            XEXP (ad, 0), XEXP (ad, 1));
3385       if (tem)
3386         ad = tem;
3387     }
3388
3389   if (GET_CODE (ad) == PLUS
3390       && GET_CODE (XEXP (ad, 0)) == REG
3391       && GET_CODE (XEXP (ad, 1)) == CONST_INT
3392       && !DISP_IN_RANGE (INTVAL (XEXP (ad, 1))))
3393     {
3394       HOST_WIDE_INT lower = INTVAL (XEXP (ad, 1)) & 0xfff;
3395       HOST_WIDE_INT upper = INTVAL (XEXP (ad, 1)) ^ lower;
3396       rtx cst, tem, new;
3397
3398       cst = GEN_INT (upper);
3399       if (!legitimate_reload_constant_p (cst))
3400         cst = force_const_mem (Pmode, cst);
3401
3402       tem = gen_rtx_PLUS (Pmode, XEXP (ad, 0), cst);
3403       new = gen_rtx_PLUS (Pmode, tem, GEN_INT (lower));
3404
3405       push_reload (XEXP (tem, 1), 0, &XEXP (tem, 1), 0,
3406                    BASE_REG_CLASS, Pmode, VOIDmode, 0, 0, 
3407                    opnum, (enum reload_type) type);
3408       return new;
3409     }
3410
3411   return NULL_RTX;
3412 }
3413
3414 /* Emit code to move LEN bytes from DST to SRC.  */
3415
3416 void
3417 s390_expand_movmem (rtx dst, rtx src, rtx len)
3418 {
3419   if (GET_CODE (len) == CONST_INT && INTVAL (len) >= 0 && INTVAL (len) <= 256)
3420     {
3421       if (INTVAL (len) > 0)
3422         emit_insn (gen_movmem_short (dst, src, GEN_INT (INTVAL (len) - 1)));
3423     }
3424
3425   else if (TARGET_MVCLE)
3426     {
3427       emit_insn (gen_movmem_long (dst, src, convert_to_mode (Pmode, len, 1)));
3428     }
3429
3430   else
3431     {
3432       rtx dst_addr, src_addr, count, blocks, temp;
3433       rtx loop_start_label = gen_label_rtx ();
3434       rtx loop_end_label = gen_label_rtx ();
3435       rtx end_label = gen_label_rtx ();
3436       enum machine_mode mode;
3437
3438       mode = GET_MODE (len);
3439       if (mode == VOIDmode)
3440         mode = Pmode;
3441
3442       dst_addr = gen_reg_rtx (Pmode);
3443       src_addr = gen_reg_rtx (Pmode);
3444       count = gen_reg_rtx (mode);
3445       blocks = gen_reg_rtx (mode);
3446
3447       convert_move (count, len, 1);
3448       emit_cmp_and_jump_insns (count, const0_rtx,
3449                                EQ, NULL_RTX, mode, 1, end_label);
3450
3451       emit_move_insn (dst_addr, force_operand (XEXP (dst, 0), NULL_RTX));
3452       emit_move_insn (src_addr, force_operand (XEXP (src, 0), NULL_RTX));
3453       dst = change_address (dst, VOIDmode, dst_addr);
3454       src = change_address (src, VOIDmode, src_addr);
3455
3456       temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1, 0);
3457       if (temp != count)
3458         emit_move_insn (count, temp);
3459
3460       temp = expand_binop (mode, ashr_optab, count, GEN_INT (8), blocks, 1, 0);
3461       if (temp != blocks)
3462         emit_move_insn (blocks, temp);
3463
3464       emit_cmp_and_jump_insns (blocks, const0_rtx,
3465                                EQ, NULL_RTX, mode, 1, loop_end_label);
3466
3467       emit_label (loop_start_label);
3468
3469       emit_insn (gen_movmem_short (dst, src, GEN_INT (255)));
3470       s390_load_address (dst_addr,
3471                          gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (256)));
3472       s390_load_address (src_addr,
3473                          gen_rtx_PLUS (Pmode, src_addr, GEN_INT (256)));
3474
3475       temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1, 0);
3476       if (temp != blocks)
3477         emit_move_insn (blocks, temp);
3478
3479       emit_cmp_and_jump_insns (blocks, const0_rtx,
3480                                EQ, NULL_RTX, mode, 1, loop_end_label);
3481
3482       emit_jump (loop_start_label);
3483       emit_label (loop_end_label);
3484
3485       emit_insn (gen_movmem_short (dst, src,
3486                                    convert_to_mode (Pmode, count, 1)));
3487       emit_label (end_label);
3488     }
3489 }
3490
3491 /* Emit code to clear LEN bytes at DST.  */
3492
3493 void
3494 s390_expand_clrmem (rtx dst, rtx len)
3495 {
3496   if (GET_CODE (len) == CONST_INT && INTVAL (len) >= 0 && INTVAL (len) <= 256)
3497     {
3498       if (INTVAL (len) > 0)
3499         emit_insn (gen_clrmem_short (dst, GEN_INT (INTVAL (len) - 1)));
3500     }
3501
3502   else if (TARGET_MVCLE)
3503     {
3504       emit_insn (gen_clrmem_long (dst, convert_to_mode (Pmode, len, 1)));
3505     }
3506
3507   else
3508     {
3509       rtx dst_addr, src_addr, count, blocks, temp;
3510       rtx loop_start_label = gen_label_rtx ();
3511       rtx loop_end_label = gen_label_rtx ();
3512       rtx end_label = gen_label_rtx ();
3513       enum machine_mode mode;
3514
3515       mode = GET_MODE (len);
3516       if (mode == VOIDmode)
3517         mode = Pmode;
3518
3519       dst_addr = gen_reg_rtx (Pmode);
3520       src_addr = gen_reg_rtx (Pmode);
3521       count = gen_reg_rtx (mode);
3522       blocks = gen_reg_rtx (mode);
3523
3524       convert_move (count, len, 1);
3525       emit_cmp_and_jump_insns (count, const0_rtx,
3526                                EQ, NULL_RTX, mode, 1, end_label);
3527
3528       emit_move_insn (dst_addr, force_operand (XEXP (dst, 0), NULL_RTX));
3529       dst = change_address (dst, VOIDmode, dst_addr);
3530
3531       temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1, 0);
3532       if (temp != count)
3533         emit_move_insn (count, temp);
3534
3535       temp = expand_binop (mode, ashr_optab, count, GEN_INT (8), blocks, 1, 0);
3536       if (temp != blocks)
3537         emit_move_insn (blocks, temp);
3538
3539       emit_cmp_and_jump_insns (blocks, const0_rtx,
3540                                EQ, NULL_RTX, mode, 1, loop_end_label);
3541
3542       emit_label (loop_start_label);
3543
3544       emit_insn (gen_clrmem_short (dst, GEN_INT (255)));
3545       s390_load_address (dst_addr,
3546                          gen_rtx_PLUS (Pmode, dst_addr, GEN_INT (256)));
3547
3548       temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1, 0);
3549       if (temp != blocks)
3550         emit_move_insn (blocks, temp);
3551
3552       emit_cmp_and_jump_insns (blocks, const0_rtx,
3553                                EQ, NULL_RTX, mode, 1, loop_end_label);
3554
3555       emit_jump (loop_start_label);
3556       emit_label (loop_end_label);
3557
3558       emit_insn (gen_clrmem_short (dst, convert_to_mode (Pmode, count, 1)));
3559       emit_label (end_label);
3560     }
3561 }
3562
3563 /* Emit code to compare LEN bytes at OP0 with those at OP1,
3564    and return the result in TARGET.  */
3565
3566 void
3567 s390_expand_cmpmem (rtx target, rtx op0, rtx op1, rtx len)
3568 {
3569   rtx ccreg = gen_rtx_REG (CCUmode, CC_REGNUM);
3570   rtx result = gen_rtx_UNSPEC (SImode, gen_rtvec (1, ccreg), UNSPEC_CMPINT);
3571
3572   if (GET_CODE (len) == CONST_INT && INTVAL (len) >= 0 && INTVAL (len) <= 256)
3573     {
3574       if (INTVAL (len) > 0)
3575         {
3576           emit_insn (gen_cmpmem_short (op0, op1, GEN_INT (INTVAL (len) - 1)));
3577           emit_move_insn (target, result);
3578         }
3579       else
3580         emit_move_insn (target, const0_rtx);
3581     }
3582
3583   else /* if (TARGET_MVCLE) */
3584     {
3585       emit_insn (gen_cmpmem_long (op0, op1, convert_to_mode (Pmode, len, 1)));
3586       emit_move_insn (target, result);
3587     }
3588
3589 #if 0
3590   /* Deactivate for now as profile code cannot cope with
3591      CC being live across basic block boundaries.  */
3592   else
3593     {
3594       rtx addr0, addr1, count, blocks, temp;
3595       rtx loop_start_label = gen_label_rtx ();
3596       rtx loop_end_label = gen_label_rtx ();
3597       rtx end_label = gen_label_rtx ();
3598       enum machine_mode mode;
3599
3600       mode = GET_MODE (len);
3601       if (mode == VOIDmode)
3602         mode = Pmode;
3603
3604       addr0 = gen_reg_rtx (Pmode);
3605       addr1 = gen_reg_rtx (Pmode);
3606       count = gen_reg_rtx (mode);
3607       blocks = gen_reg_rtx (mode);
3608
3609       convert_move (count, len, 1);
3610       emit_cmp_and_jump_insns (count, const0_rtx,
3611                                EQ, NULL_RTX, mode, 1, end_label);
3612
3613       emit_move_insn (addr0, force_operand (XEXP (op0, 0), NULL_RTX));
3614       emit_move_insn (addr1, force_operand (XEXP (op1, 0), NULL_RTX));
3615       op0 = change_address (op0, VOIDmode, addr0);
3616       op1 = change_address (op1, VOIDmode, addr1);
3617
3618       temp = expand_binop (mode, add_optab, count, constm1_rtx, count, 1, 0);
3619       if (temp != count)
3620         emit_move_insn (count, temp);
3621
3622       temp = expand_binop (mode, ashr_optab, count, GEN_INT (8), blocks, 1, 0);
3623       if (temp != blocks)
3624         emit_move_insn (blocks, temp);
3625
3626       emit_cmp_and_jump_insns (blocks, const0_rtx,
3627                                EQ, NULL_RTX, mode, 1, loop_end_label);
3628
3629       emit_label (loop_start_label);
3630
3631       emit_insn (gen_cmpmem_short (op0, op1, GEN_INT (255)));
3632       temp = gen_rtx_NE (VOIDmode, ccreg, const0_rtx);
3633       temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
3634                         gen_rtx_LABEL_REF (VOIDmode, end_label), pc_rtx);
3635       temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
3636       emit_jump_insn (temp);
3637
3638       s390_load_address (addr0,
3639                          gen_rtx_PLUS (Pmode, addr0, GEN_INT (256)));
3640       s390_load_address (addr1,
3641                          gen_rtx_PLUS (Pmode, addr1, GEN_INT (256)));
3642
3643       temp = expand_binop (mode, add_optab, blocks, constm1_rtx, blocks, 1, 0);
3644       if (temp != blocks)
3645         emit_move_insn (blocks, temp);
3646
3647       emit_cmp_and_jump_insns (blocks, const0_rtx,
3648                                EQ, NULL_RTX, mode, 1, loop_end_label);
3649
3650       emit_jump (loop_start_label);
3651       emit_label (loop_end_label);
3652
3653       emit_insn (gen_cmpmem_short (op0, op1,
3654                                    convert_to_mode (Pmode, count, 1)));
3655       emit_label (end_label);
3656
3657       emit_move_insn (target, result);
3658     }
3659 #endif
3660 }
3661
3662
3663 /* Expand conditional increment or decrement using alc/slb instructions.
3664    Should generate code setting DST to either SRC or SRC + INCREMENT,
3665    depending on the result of the comparison CMP_OP0 CMP_CODE CMP_OP1.
3666    Returns true if successful, false otherwise.  */
3667
3668 bool
3669 s390_expand_addcc (enum rtx_code cmp_code, rtx cmp_op0, rtx cmp_op1,
3670                    rtx dst, rtx src, rtx increment)
3671 {
3672   enum machine_mode cmp_mode;
3673   enum machine_mode cc_mode;
3674   rtx op_res;
3675   rtx insn;
3676   rtvec p;
3677
3678   if ((GET_MODE (cmp_op0) == SImode || GET_MODE (cmp_op0) == VOIDmode)
3679       && (GET_MODE (cmp_op1) == SImode || GET_MODE (cmp_op1) == VOIDmode))
3680     cmp_mode = SImode;
3681   else if ((GET_MODE (cmp_op0) == DImode || GET_MODE (cmp_op0) == VOIDmode)
3682            && (GET_MODE (cmp_op1) == DImode || GET_MODE (cmp_op1) == VOIDmode))
3683     cmp_mode = DImode;
3684   else
3685     return false;
3686
3687   /* Try ADD LOGICAL WITH CARRY.  */
3688   if (increment == const1_rtx)
3689     {
3690       /* Determine CC mode to use.  */
3691       if (cmp_code == EQ || cmp_code == NE)
3692         {
3693           if (cmp_op1 != const0_rtx)
3694             {
3695               cmp_op0 = expand_simple_binop (cmp_mode, XOR, cmp_op0, cmp_op1,
3696                                              NULL_RTX, 0, OPTAB_WIDEN);
3697               cmp_op1 = const0_rtx;
3698             }
3699
3700           cmp_code = cmp_code == EQ ? LEU : GTU;
3701         }
3702
3703       if (cmp_code == LTU || cmp_code == LEU)
3704         {
3705           rtx tem = cmp_op0;
3706           cmp_op0 = cmp_op1;
3707           cmp_op1 = tem;
3708           cmp_code = swap_condition (cmp_code);
3709         }
3710
3711       switch (cmp_code)
3712         {
3713           case GTU:
3714             cc_mode = CCUmode;
3715             break;
3716
3717           case GEU:
3718             cc_mode = CCL3mode;
3719             break;
3720
3721           default:
3722             return false;
3723         }
3724
3725       /* Emit comparison instruction pattern. */
3726       if (!register_operand (cmp_op0, cmp_mode))
3727         cmp_op0 = force_reg (cmp_mode, cmp_op0);
3728
3729       insn = gen_rtx_SET (VOIDmode, gen_rtx_REG (cc_mode, CC_REGNUM),
3730                           gen_rtx_COMPARE (cc_mode, cmp_op0, cmp_op1));
3731       /* We use insn_invalid_p here to add clobbers if required.  */
3732       if (insn_invalid_p (emit_insn (insn)))
3733         abort ();
3734
3735       /* Emit ALC instruction pattern.  */
3736       op_res = gen_rtx_fmt_ee (cmp_code, GET_MODE (dst),
3737                                gen_rtx_REG (cc_mode, CC_REGNUM),
3738                                const0_rtx);
3739
3740       if (src != const0_rtx)
3741         {
3742           if (!register_operand (src, GET_MODE (dst)))
3743             src = force_reg (GET_MODE (dst), src);
3744
3745           src = gen_rtx_PLUS (GET_MODE (dst), src, const0_rtx);
3746           op_res = gen_rtx_PLUS (GET_MODE (dst), src, op_res);
3747         }
3748
3749       p = rtvec_alloc (2);
3750       RTVEC_ELT (p, 0) = 
3751         gen_rtx_SET (VOIDmode, dst, op_res);
3752       RTVEC_ELT (p, 1) = 
3753         gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
3754       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
3755
3756       return true;
3757     }
3758
3759   /* Try SUBTRACT LOGICAL WITH BORROW.  */
3760   if (increment == constm1_rtx)
3761     {
3762       /* Determine CC mode to use.  */
3763       if (cmp_code == EQ || cmp_code == NE)
3764         {
3765           if (cmp_op1 != const0_rtx)
3766             {
3767               cmp_op0 = expand_simple_binop (cmp_mode, XOR, cmp_op0, cmp_op1,
3768                                              NULL_RTX, 0, OPTAB_WIDEN);
3769               cmp_op1 = const0_rtx;
3770             }
3771
3772           cmp_code = cmp_code == EQ ? LEU : GTU;
3773         }
3774
3775       if (cmp_code == GTU || cmp_code == GEU)
3776         {
3777           rtx tem = cmp_op0;
3778           cmp_op0 = cmp_op1;
3779           cmp_op1 = tem;
3780           cmp_code = swap_condition (cmp_code);
3781         }
3782
3783       switch (cmp_code)
3784         {
3785           case LEU:
3786             cc_mode = CCUmode;
3787             break;
3788
3789           case LTU:
3790             cc_mode = CCL3mode;
3791             break;
3792
3793           default:
3794             return false;
3795         }
3796
3797       /* Emit comparison instruction pattern. */
3798       if (!register_operand (cmp_op0, cmp_mode))
3799         cmp_op0 = force_reg (cmp_mode, cmp_op0);
3800
3801       insn = gen_rtx_SET (VOIDmode, gen_rtx_REG (cc_mode, CC_REGNUM),
3802                           gen_rtx_COMPARE (cc_mode, cmp_op0, cmp_op1));
3803       /* We use insn_invalid_p here to add clobbers if required.  */
3804       if (insn_invalid_p (emit_insn (insn)))
3805         abort ();
3806
3807       /* Emit SLB instruction pattern.  */
3808       if (!register_operand (src, GET_MODE (dst)))
3809         src = force_reg (GET_MODE (dst), src);
3810
3811       op_res = gen_rtx_MINUS (GET_MODE (dst), 
3812                               gen_rtx_MINUS (GET_MODE (dst), src, const0_rtx), 
3813                               gen_rtx_fmt_ee (cmp_code, GET_MODE (dst), 
3814                                               gen_rtx_REG (cc_mode, CC_REGNUM), 
3815                                               const0_rtx));
3816       p = rtvec_alloc (2);
3817       RTVEC_ELT (p, 0) = 
3818         gen_rtx_SET (VOIDmode, dst, op_res);
3819       RTVEC_ELT (p, 1) = 
3820         gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, CC_REGNUM));
3821       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
3822
3823       return true;
3824     }
3825
3826   return false;
3827 }
3828
3829
3830 /* This is called from dwarf2out.c via ASM_OUTPUT_DWARF_DTPREL.
3831    We need to emit DTP-relative relocations.  */
3832
3833 void
3834 s390_output_dwarf_dtprel (FILE *file, int size, rtx x)
3835 {
3836   switch (size)
3837     {
3838     case 4:
3839       fputs ("\t.long\t", file);
3840       break;
3841     case 8:
3842       fputs ("\t.quad\t", file);
3843       break;
3844     default:
3845       abort ();
3846     }
3847   output_addr_const (file, x);
3848   fputs ("@DTPOFF", file);
3849 }
3850
3851 /* In the name of slightly smaller debug output, and to cater to
3852    general assembler losage, recognize various UNSPEC sequences
3853    and turn them back into a direct symbol reference.  */
3854
3855 static rtx
3856 s390_delegitimize_address (rtx orig_x)
3857 {
3858   rtx x = orig_x, y;
3859
3860   if (GET_CODE (x) != MEM)
3861     return orig_x;
3862
3863   x = XEXP (x, 0);
3864   if (GET_CODE (x) == PLUS
3865       && GET_CODE (XEXP (x, 1)) == CONST
3866       && GET_CODE (XEXP (x, 0)) == REG
3867       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
3868     {
3869       y = XEXP (XEXP (x, 1), 0);
3870       if (GET_CODE (y) == UNSPEC
3871           && XINT (y, 1) == UNSPEC_GOT)
3872         return XVECEXP (y, 0, 0);
3873       return orig_x;
3874     }
3875
3876   if (GET_CODE (x) == CONST)
3877     {
3878       y = XEXP (x, 0);
3879       if (GET_CODE (y) == UNSPEC
3880           && XINT (y, 1) == UNSPEC_GOTENT)
3881         return XVECEXP (y, 0, 0);
3882       return orig_x;
3883     }
3884
3885   return orig_x;
3886 }
3887
3888 /* Output shift count operand OP to stdio stream FILE.  */
3889
3890 static void
3891 print_shift_count_operand (FILE *file, rtx op)
3892 {
3893   HOST_WIDE_INT offset = 0;
3894
3895   /* We can have an integer constant, an address register,
3896      or a sum of the two.  */
3897   if (GET_CODE (op) == CONST_INT)
3898     {
3899       offset = INTVAL (op);
3900       op = NULL_RTX;
3901     }
3902   if (op && GET_CODE (op) == PLUS && GET_CODE (XEXP (op, 1)) == CONST_INT)
3903     {
3904       offset = INTVAL (XEXP (op, 1));
3905       op = XEXP (op, 0);
3906     }
3907   while (op && GET_CODE (op) == SUBREG)
3908     op = SUBREG_REG (op);
3909
3910   /* Sanity check.  */
3911   if (op && (GET_CODE (op) != REG
3912              || REGNO (op) >= FIRST_PSEUDO_REGISTER
3913              || REGNO_REG_CLASS (REGNO (op)) != ADDR_REGS))
3914     abort ();
3915
3916   /* Shift counts are truncated to the low six bits anyway.  */
3917   fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset & 63);
3918   if (op)
3919     fprintf (file, "(%s)", reg_names[REGNO (op)]);
3920 }
3921
3922 /* Locate some local-dynamic symbol still in use by this function
3923    so that we can print its name in local-dynamic base patterns.  */
3924
3925 static const char *
3926 get_some_local_dynamic_name (void)
3927 {
3928   rtx insn;
3929
3930   if (cfun->machine->some_ld_name)
3931     return cfun->machine->some_ld_name;
3932
3933   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
3934     if (INSN_P (insn)
3935         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
3936       return cfun->machine->some_ld_name;
3937
3938   abort ();
3939 }
3940
3941 static int
3942 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
3943 {
3944   rtx x = *px;
3945
3946   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3947     {
3948       x = get_pool_constant (x);
3949       return for_each_rtx (&x, get_some_local_dynamic_name_1, 0);
3950     }
3951
3952   if (GET_CODE (x) == SYMBOL_REF
3953       && tls_symbolic_operand (x) == TLS_MODEL_LOCAL_DYNAMIC)
3954     {
3955       cfun->machine->some_ld_name = XSTR (x, 0);
3956       return 1;
3957     }
3958
3959   return 0;
3960 }
3961
3962 /* Output machine-dependent UNSPECs occurring in address constant X
3963    in assembler syntax to stdio stream FILE.  Returns true if the
3964    constant X could be recognized, false otherwise.  */
3965
3966 bool
3967 s390_output_addr_const_extra (FILE *file, rtx x)
3968 {
3969   if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 1)
3970     switch (XINT (x, 1))
3971       {
3972       case UNSPEC_GOTENT:
3973         output_addr_const (file, XVECEXP (x, 0, 0));
3974         fprintf (file, "@GOTENT");
3975         return true;
3976       case UNSPEC_GOT:
3977         output_addr_const (file, XVECEXP (x, 0, 0));
3978         fprintf (file, "@GOT");
3979         return true;
3980       case UNSPEC_GOTOFF:
3981         output_addr_const (file, XVECEXP (x, 0, 0));
3982         fprintf (file, "@GOTOFF");
3983         return true;
3984       case UNSPEC_PLT:
3985         output_addr_const (file, XVECEXP (x, 0, 0));
3986         fprintf (file, "@PLT");
3987         return true;
3988       case UNSPEC_PLTOFF:
3989         output_addr_const (file, XVECEXP (x, 0, 0));
3990         fprintf (file, "@PLTOFF");
3991         return true;
3992       case UNSPEC_TLSGD:
3993         output_addr_const (file, XVECEXP (x, 0, 0));
3994         fprintf (file, "@TLSGD");
3995         return true;
3996       case UNSPEC_TLSLDM:
3997         assemble_name (file, get_some_local_dynamic_name ());
3998         fprintf (file, "@TLSLDM");
3999         return true;
4000       case UNSPEC_DTPOFF:
4001         output_addr_const (file, XVECEXP (x, 0, 0));
4002         fprintf (file, "@DTPOFF");
4003         return true;
4004       case UNSPEC_NTPOFF:
4005         output_addr_const (file, XVECEXP (x, 0, 0));
4006         fprintf (file, "@NTPOFF");
4007         return true;
4008       case UNSPEC_GOTNTPOFF:
4009         output_addr_const (file, XVECEXP (x, 0, 0));
4010         fprintf (file, "@GOTNTPOFF");
4011         return true;
4012       case UNSPEC_INDNTPOFF:
4013         output_addr_const (file, XVECEXP (x, 0, 0));
4014         fprintf (file, "@INDNTPOFF");
4015         return true;
4016       }
4017
4018   return false;
4019 }
4020
4021 /* Output address operand ADDR in assembler syntax to
4022    stdio stream FILE.  */
4023
4024 void
4025 print_operand_address (FILE *file, rtx addr)
4026 {
4027   struct s390_address ad;
4028
4029   if (!s390_decompose_address (addr, &ad)
4030       || (ad.base && !REG_OK_FOR_BASE_STRICT_P (ad.base))
4031       || (ad.indx && !REG_OK_FOR_INDEX_STRICT_P (ad.indx)))
4032     output_operand_lossage ("Cannot decompose address.");
4033
4034   if (ad.disp)
4035     output_addr_const (file, ad.disp);
4036   else
4037     fprintf (file, "0");
4038
4039   if (ad.base && ad.indx)
4040     fprintf (file, "(%s,%s)", reg_names[REGNO (ad.indx)],
4041                               reg_names[REGNO (ad.base)]);
4042   else if (ad.base)
4043     fprintf (file, "(%s)", reg_names[REGNO (ad.base)]);
4044 }
4045
4046 /* Output operand X in assembler syntax to stdio stream FILE.
4047    CODE specified the format flag.  The following format flags
4048    are recognized:
4049
4050     'C': print opcode suffix for branch condition.
4051     'D': print opcode suffix for inverse branch condition.
4052     'J': print tls_load/tls_gdcall/tls_ldcall suffix
4053     'O': print only the displacement of a memory reference.
4054     'R': print only the base register of a memory reference.
4055     'S': print S-type memory reference (base+displacement).
4056     'N': print the second word of a DImode operand.
4057     'M': print the second word of a TImode operand.
4058     'Y': print shift count operand.
4059
4060     'b': print integer X as if it's an unsigned byte.
4061     'x': print integer X as if it's an unsigned word.
4062     'h': print integer X as if it's a signed word.
4063     'i': print the first nonzero HImode part of X.
4064     'j': print the first HImode part unequal to 0xffff of X.  */
4065
4066 void
4067 print_operand (FILE *file, rtx x, int code)
4068 {
4069   switch (code)
4070     {
4071     case 'C':
4072       fprintf (file, s390_branch_condition_mnemonic (x, FALSE));
4073       return;
4074
4075     case 'D':
4076       fprintf (file, s390_branch_condition_mnemonic (x, TRUE));
4077       return;
4078
4079     case 'J':
4080       if (GET_CODE (x) == SYMBOL_REF)
4081         {
4082           fprintf (file, "%s", ":tls_load:");
4083           output_addr_const (file, x);
4084         }
4085       else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSGD)
4086         {
4087           fprintf (file, "%s", ":tls_gdcall:");
4088           output_addr_const (file, XVECEXP (x, 0, 0));
4089         }
4090       else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSLDM)
4091         {
4092           fprintf (file, "%s", ":tls_ldcall:");
4093           assemble_name (file, get_some_local_dynamic_name ());
4094         }
4095       else
4096         abort ();
4097       return;
4098
4099     case 'O':
4100       {
4101         struct s390_address ad;
4102
4103         if (GET_CODE (x) != MEM
4104             || !s390_decompose_address (XEXP (x, 0), &ad)
4105             || (ad.base && !REG_OK_FOR_BASE_STRICT_P (ad.base))
4106             || ad.indx)
4107           abort ();
4108
4109         if (ad.disp)
4110           output_addr_const (file, ad.disp);
4111         else
4112           fprintf (file, "0");
4113       }
4114       return;
4115
4116     case 'R':
4117       {
4118         struct s390_address ad;
4119
4120         if (GET_CODE (x) != MEM
4121             || !s390_decompose_address (XEXP (x, 0), &ad)
4122             || (ad.base && !REG_OK_FOR_BASE_STRICT_P (ad.base))
4123             || ad.indx)
4124           abort ();
4125
4126         if (ad.base)
4127           fprintf (file, "%s", reg_names[REGNO (ad.base)]);
4128         else
4129           fprintf (file, "0");
4130       }
4131       return;
4132
4133     case 'S':
4134       {
4135         struct s390_address ad;
4136
4137         if (GET_CODE (x) != MEM
4138             || !s390_decompose_address (XEXP (x, 0), &ad)
4139             || (ad.base && !REG_OK_FOR_BASE_STRICT_P (ad.base))
4140             || ad.indx)
4141           abort ();
4142
4143         if (ad.disp)
4144           output_addr_const (file, ad.disp);
4145         else
4146           fprintf (file, "0");
4147
4148         if (ad.base)
4149           fprintf (file, "(%s)", reg_names[REGNO (ad.base)]);
4150       }
4151       return;
4152
4153     case 'N':
4154       if (GET_CODE (x) == REG)
4155         x = gen_rtx_REG (GET_MODE (x), REGNO (x) + 1);
4156       else if (GET_CODE (x) == MEM)
4157         x = change_address (x, VOIDmode, plus_constant (XEXP (x, 0), 4));
4158       else
4159         abort ();
4160       break;
4161
4162     case 'M':
4163       if (GET_CODE (x) == REG)
4164         x = gen_rtx_REG (GET_MODE (x), REGNO (x) + 1);
4165       else if (GET_CODE (x) == MEM)
4166         x = change_address (x, VOIDmode, plus_constant (XEXP (x, 0), 8));
4167       else
4168         abort ();
4169       break;
4170
4171     case 'Y':
4172       print_shift_count_operand (file, x);
4173       return;
4174     }
4175
4176   switch (GET_CODE (x))
4177     {
4178     case REG:
4179       fprintf (file, "%s", reg_names[REGNO (x)]);
4180       break;
4181
4182     case MEM:
4183       output_address (XEXP (x, 0));
4184       break;
4185
4186     case CONST:
4187     case CODE_LABEL:
4188     case LABEL_REF:
4189     case SYMBOL_REF:
4190       output_addr_const (file, x);
4191       break;
4192
4193     case CONST_INT:
4194       if (code == 'b')
4195         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0xff);
4196       else if (code == 'x')
4197         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0xffff);
4198       else if (code == 'h')
4199         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ((INTVAL (x) & 0xffff) ^ 0x8000) - 0x8000);
4200       else if (code == 'i')
4201         fprintf (file, HOST_WIDE_INT_PRINT_DEC,
4202                  s390_extract_part (x, HImode, 0));
4203       else if (code == 'j')
4204         fprintf (file, HOST_WIDE_INT_PRINT_DEC,
4205                  s390_extract_part (x, HImode, -1));
4206       else
4207         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
4208       break;
4209
4210     case CONST_DOUBLE:
4211       if (GET_MODE (x) != VOIDmode)
4212         abort ();
4213       if (code == 'b')
4214         fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x) & 0xff);
4215       else if (code == 'x')
4216         fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x) & 0xffff);
4217       else if (code == 'h')
4218         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ((CONST_DOUBLE_LOW (x) & 0xffff) ^ 0x8000) - 0x8000);
4219       else
4220         abort ();
4221       break;
4222
4223     default:
4224       fatal_insn ("UNKNOWN in print_operand !?", x);
4225       break;
4226     }
4227 }
4228
4229 /* Target hook for assembling integer objects.  We need to define it
4230    here to work a round a bug in some versions of GAS, which couldn't
4231    handle values smaller than INT_MIN when printed in decimal.  */
4232
4233 static bool
4234 s390_assemble_integer (rtx x, unsigned int size, int aligned_p)
4235 {
4236   if (size == 8 && aligned_p
4237       && GET_CODE (x) == CONST_INT && INTVAL (x) < INT_MIN)
4238     {
4239       fprintf (asm_out_file, "\t.quad\t" HOST_WIDE_INT_PRINT_HEX "\n",
4240                INTVAL (x));
4241       return true;
4242     }
4243   return default_assemble_integer (x, size, aligned_p);
4244 }
4245
4246 /* Returns true if register REGNO is used  for forming
4247    a memory address in expression X.  */
4248
4249 static int
4250 reg_used_in_mem_p (int regno, rtx x)
4251 {
4252   enum rtx_code code = GET_CODE (x);
4253   int i, j;
4254   const char *fmt;
4255
4256   if (code == MEM)
4257     {
4258       if (refers_to_regno_p (regno, regno+1,
4259                              XEXP (x, 0), 0))
4260         return 1;
4261     }
4262   else if (code == SET
4263            && GET_CODE (SET_DEST (x)) == PC)
4264     {
4265       if (refers_to_regno_p (regno, regno+1,
4266                              SET_SRC (x), 0))
4267         return 1;
4268     }
4269
4270   fmt = GET_RTX_FORMAT (code);
4271   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4272     {
4273       if (fmt[i] == 'e'
4274           && reg_used_in_mem_p (regno, XEXP (x, i)))
4275         return 1;
4276
4277       else if (fmt[i] == 'E')
4278         for (j = 0; j < XVECLEN (x, i); j++)
4279           if (reg_used_in_mem_p (regno, XVECEXP (x, i, j)))
4280             return 1;
4281     }
4282   return 0;
4283 }
4284
4285 /* Returns true if expression DEP_RTX sets an address register
4286    used by instruction INSN to address memory.  */
4287
4288 static int
4289 addr_generation_dependency_p (rtx dep_rtx, rtx insn)
4290 {
4291   rtx target, pat;
4292
4293   if (GET_CODE (dep_rtx) == INSN)
4294       dep_rtx = PATTERN (dep_rtx);
4295
4296   if (GET_CODE (dep_rtx) == SET)
4297     {
4298       target = SET_DEST (dep_rtx);
4299       if (GET_CODE (target) == STRICT_LOW_PART)
4300         target = XEXP (target, 0);
4301       while (GET_CODE (target) == SUBREG)
4302         target = SUBREG_REG (target);
4303
4304       if (GET_CODE (target) == REG)
4305         {
4306           int regno = REGNO (target);
4307
4308           if (s390_safe_attr_type (insn) == TYPE_LA)
4309             {
4310               pat = PATTERN (insn);
4311               if (GET_CODE (pat) == PARALLEL)
4312                 {
4313                   if (XVECLEN (pat, 0) != 2)
4314                     abort();
4315                   pat = XVECEXP (pat, 0, 0);
4316                 }
4317               if (GET_CODE (pat) == SET)
4318                 return refers_to_regno_p (regno, regno+1, SET_SRC (pat), 0);
4319               else
4320                 abort();
4321             }
4322           else if (get_attr_atype (insn) == ATYPE_AGEN)
4323             return reg_used_in_mem_p (regno, PATTERN (insn));
4324         }
4325     }
4326   return 0;
4327 }
4328
4329 /* Return 1, if dep_insn sets register used in insn in the agen unit.  */
4330
4331 int
4332 s390_agen_dep_p (rtx dep_insn, rtx insn)
4333 {
4334   rtx dep_rtx = PATTERN (dep_insn);
4335   int i;
4336
4337   if (GET_CODE (dep_rtx) == SET
4338       && addr_generation_dependency_p (dep_rtx, insn))
4339     return 1;
4340   else if (GET_CODE (dep_rtx) == PARALLEL)
4341     {
4342       for (i = 0; i < XVECLEN (dep_rtx, 0); i++)
4343         {
4344           if (addr_generation_dependency_p (XVECEXP (dep_rtx, 0, i), insn))
4345             return 1;
4346         }
4347     }
4348   return 0;
4349 }
4350
4351 /* A C statement (sans semicolon) to update the integer scheduling priority
4352    INSN_PRIORITY (INSN).  Increase the priority to execute the INSN earlier,
4353    reduce the priority to execute INSN later.  Do not define this macro if
4354    you do not need to adjust the scheduling priorities of insns.
4355
4356    A STD instruction should be scheduled earlier,
4357    in order to use the bypass.  */
4358
4359 static int
4360 s390_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
4361 {
4362   if (! INSN_P (insn))
4363     return priority;
4364
4365   if (s390_tune != PROCESSOR_2084_Z990)
4366     return priority;
4367
4368   switch (s390_safe_attr_type (insn))
4369     {
4370       case TYPE_FSTORED:
4371       case TYPE_FSTORES:
4372         priority = priority << 3;
4373         break;
4374       case TYPE_STORE:
4375       case TYPE_STM:
4376         priority = priority << 1;
4377         break;
4378       default:
4379         break;
4380     }
4381   return priority;
4382 }
4383
4384 /* The number of instructions that can be issued per cycle.  */
4385
4386 static int
4387 s390_issue_rate (void)
4388 {
4389   if (s390_tune == PROCESSOR_2084_Z990)
4390     return 3;
4391   return 1;
4392 }
4393
4394 static int
4395 s390_first_cycle_multipass_dfa_lookahead (void)
4396 {
4397   return 4;
4398 }
4399
4400
4401 /* Split all branches that exceed the maximum distance.
4402    Returns true if this created a new literal pool entry.  */
4403
4404 static int
4405 s390_split_branches (void)
4406 {
4407   rtx temp_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
4408   int new_literal = 0;
4409   rtx insn, pat, tmp, target;
4410   rtx *label;
4411
4412   /* We need correct insn addresses.  */
4413
4414   shorten_branches (get_insns ());
4415
4416   /* Find all branches that exceed 64KB, and split them.  */
4417
4418   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4419     {
4420       if (GET_CODE (insn) != JUMP_INSN)
4421         continue;
4422
4423       pat = PATTERN (insn);
4424       if (GET_CODE (pat) == PARALLEL && XVECLEN (pat, 0) > 2)
4425         pat = XVECEXP (pat, 0, 0);
4426       if (GET_CODE (pat) != SET || SET_DEST (pat) != pc_rtx)
4427         continue;
4428
4429       if (GET_CODE (SET_SRC (pat)) == LABEL_REF)
4430         {
4431           label = &SET_SRC (pat);
4432         }
4433       else if (GET_CODE (SET_SRC (pat)) == IF_THEN_ELSE)
4434         {
4435           if (GET_CODE (XEXP (SET_SRC (pat), 1)) == LABEL_REF)
4436             label = &XEXP (SET_SRC (pat), 1);
4437           else if (GET_CODE (XEXP (SET_SRC (pat), 2)) == LABEL_REF)
4438             label = &XEXP (SET_SRC (pat), 2);
4439           else
4440             continue;
4441         }
4442       else
4443         continue;
4444
4445       if (get_attr_length (insn) <= 4)
4446         continue;
4447
4448       /* We are going to use the return register as scratch register,
4449          make sure it will be saved/restored by the prologue/epilogue.  */
4450       cfun_frame_layout.save_return_addr_p = 1;
4451
4452       if (!flag_pic)
4453         {
4454           new_literal = 1;
4455           tmp = force_const_mem (Pmode, *label);
4456           tmp = emit_insn_before (gen_rtx_SET (Pmode, temp_reg, tmp), insn);
4457           INSN_ADDRESSES_NEW (tmp, -1);
4458           annotate_constant_pool_refs (&PATTERN (tmp));
4459
4460           target = temp_reg;
4461         }
4462       else
4463         {
4464           new_literal = 1;
4465           target = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, *label),
4466                                    UNSPEC_LTREL_OFFSET);
4467           target = gen_rtx_CONST (Pmode, target);
4468           target = force_const_mem (Pmode, target);
4469           tmp = emit_insn_before (gen_rtx_SET (Pmode, temp_reg, target), insn);
4470           INSN_ADDRESSES_NEW (tmp, -1);
4471           annotate_constant_pool_refs (&PATTERN (tmp));
4472
4473           target = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, XEXP (target, 0),
4474                                                         cfun->machine->base_reg),
4475                                    UNSPEC_LTREL_BASE);
4476           target = gen_rtx_PLUS (Pmode, temp_reg, target);
4477         }
4478
4479       if (!validate_change (insn, label, target, 0))
4480         abort ();
4481     }
4482
4483   return new_literal;
4484 }
4485
4486 /* Annotate every literal pool reference in X by an UNSPEC_LTREF expression.
4487    Fix up MEMs as required.  */
4488
4489 static void
4490 annotate_constant_pool_refs (rtx *x)
4491 {
4492   int i, j;
4493   const char *fmt;
4494
4495   if (GET_CODE (*x) == SYMBOL_REF
4496       && CONSTANT_POOL_ADDRESS_P (*x))
4497     abort ();
4498
4499   /* Literal pool references can only occur inside a MEM ...  */
4500   if (GET_CODE (*x) == MEM)
4501     {
4502       rtx memref = XEXP (*x, 0);
4503
4504       if (GET_CODE (memref) == SYMBOL_REF
4505           && CONSTANT_POOL_ADDRESS_P (memref))
4506         {
4507           rtx base = cfun->machine->base_reg;
4508           rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, memref, base),
4509                                      UNSPEC_LTREF);
4510
4511           *x = replace_equiv_address (*x, addr);
4512           return;
4513         }
4514
4515       if (GET_CODE (memref) == CONST
4516           && GET_CODE (XEXP (memref, 0)) == PLUS
4517           && GET_CODE (XEXP (XEXP (memref, 0), 1)) == CONST_INT
4518           && GET_CODE (XEXP (XEXP (memref, 0), 0)) == SYMBOL_REF
4519           && CONSTANT_POOL_ADDRESS_P (XEXP (XEXP (memref, 0), 0)))
4520         {
4521           HOST_WIDE_INT off = INTVAL (XEXP (XEXP (memref, 0), 1));
4522           rtx sym = XEXP (XEXP (memref, 0), 0);
4523           rtx base = cfun->machine->base_reg;
4524           rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, sym, base),
4525                                      UNSPEC_LTREF);
4526
4527           *x = replace_equiv_address (*x, plus_constant (addr, off));
4528           return;
4529         }
4530     }
4531
4532   /* ... or a load-address type pattern.  */
4533   if (GET_CODE (*x) == SET)
4534     {
4535       rtx addrref = SET_SRC (*x);
4536
4537       if (GET_CODE (addrref) == SYMBOL_REF
4538           && CONSTANT_POOL_ADDRESS_P (addrref))
4539         {
4540           rtx base = cfun->machine->base_reg;
4541           rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, addrref, base),
4542                                      UNSPEC_LTREF);
4543
4544           SET_SRC (*x) = addr;
4545           return;
4546         }
4547
4548       if (GET_CODE (addrref) == CONST
4549           && GET_CODE (XEXP (addrref, 0)) == PLUS
4550           && GET_CODE (XEXP (XEXP (addrref, 0), 1)) == CONST_INT
4551           && GET_CODE (XEXP (XEXP (addrref, 0), 0)) == SYMBOL_REF
4552           && CONSTANT_POOL_ADDRESS_P (XEXP (XEXP (addrref, 0), 0)))
4553         {
4554           HOST_WIDE_INT off = INTVAL (XEXP (XEXP (addrref, 0), 1));
4555           rtx sym = XEXP (XEXP (addrref, 0), 0);
4556           rtx base = cfun->machine->base_reg;
4557           rtx addr = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, sym, base),
4558                                      UNSPEC_LTREF);
4559
4560           SET_SRC (*x) = plus_constant (addr, off);
4561           return;
4562         }
4563     }
4564
4565   /* Annotate LTREL_BASE as well.  */
4566   if (GET_CODE (*x) == UNSPEC
4567       && XINT (*x, 1) == UNSPEC_LTREL_BASE)
4568     {
4569       rtx base = cfun->machine->base_reg;
4570       *x = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, XVECEXP (*x, 0, 0), base),
4571                                   UNSPEC_LTREL_BASE);
4572       return;
4573     }
4574
4575   fmt = GET_RTX_FORMAT (GET_CODE (*x));
4576   for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
4577     {
4578       if (fmt[i] == 'e')
4579         {
4580           annotate_constant_pool_refs (&XEXP (*x, i));
4581         }
4582       else if (fmt[i] == 'E')
4583         {
4584           for (j = 0; j < XVECLEN (*x, i); j++)
4585             annotate_constant_pool_refs (&XVECEXP (*x, i, j));
4586         }
4587     }
4588 }
4589
4590
4591 /* Find an annotated literal pool symbol referenced in RTX X, 
4592    and store it at REF.  Will abort if X contains references to 
4593    more than one such pool symbol; multiple references to the same
4594    symbol are allowed, however.
4595
4596    The rtx pointed to by REF must be initialized to NULL_RTX
4597    by the caller before calling this routine.  */
4598
4599 static void
4600 find_constant_pool_ref (rtx x, rtx *ref)
4601 {
4602   int i, j;
4603   const char *fmt;
4604
4605   /* Ignore LTREL_BASE references.  */
4606   if (GET_CODE (x) == UNSPEC
4607       && XINT (x, 1) == UNSPEC_LTREL_BASE)
4608     return;
4609   /* Likewise POOL_ENTRY insns.  */
4610   if (GET_CODE (x) == UNSPEC_VOLATILE
4611       && XINT (x, 1) == UNSPECV_POOL_ENTRY)
4612     return;
4613
4614   if (GET_CODE (x) == SYMBOL_REF
4615       && CONSTANT_POOL_ADDRESS_P (x))
4616     abort ();
4617
4618   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_LTREF)
4619     {
4620       rtx sym = XVECEXP (x, 0, 0);
4621       if (GET_CODE (sym) != SYMBOL_REF
4622           || !CONSTANT_POOL_ADDRESS_P (sym))
4623         abort ();
4624
4625       if (*ref == NULL_RTX)
4626         *ref = sym;
4627       else if (*ref != sym)
4628         abort ();
4629
4630       return;
4631     }
4632
4633   fmt = GET_RTX_FORMAT (GET_CODE (x));
4634   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
4635     {
4636       if (fmt[i] == 'e')
4637         {
4638           find_constant_pool_ref (XEXP (x, i), ref);
4639         }
4640       else if (fmt[i] == 'E')
4641         {
4642           for (j = 0; j < XVECLEN (x, i); j++)
4643             find_constant_pool_ref (XVECEXP (x, i, j), ref);
4644         }
4645     }
4646 }
4647
4648 /* Replace every reference to the annotated literal pool 
4649    symbol REF in X by its base plus OFFSET.  */
4650
4651 static void
4652 replace_constant_pool_ref (rtx *x, rtx ref, rtx offset)
4653 {
4654   int i, j;
4655   const char *fmt;
4656
4657   if (*x == ref)
4658     abort ();
4659
4660   if (GET_CODE (*x) == UNSPEC
4661       && XINT (*x, 1) == UNSPEC_LTREF
4662       && XVECEXP (*x, 0, 0) == ref)
4663     {
4664       *x = gen_rtx_PLUS (Pmode, XVECEXP (*x, 0, 1), offset);
4665       return;
4666     }
4667
4668   if (GET_CODE (*x) == PLUS
4669       && GET_CODE (XEXP (*x, 1)) == CONST_INT
4670       && GET_CODE (XEXP (*x, 0)) == UNSPEC
4671       && XINT (XEXP (*x, 0), 1) == UNSPEC_LTREF
4672       && XVECEXP (XEXP (*x, 0), 0, 0) == ref)
4673     {
4674       rtx addr = gen_rtx_PLUS (Pmode, XVECEXP (XEXP (*x, 0), 0, 1), offset);
4675       *x = plus_constant (addr, INTVAL (XEXP (*x, 1)));
4676       return;
4677     }
4678
4679   fmt = GET_RTX_FORMAT (GET_CODE (*x));
4680   for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
4681     {
4682       if (fmt[i] == 'e')
4683         {
4684           replace_constant_pool_ref (&XEXP (*x, i), ref, offset);
4685         }
4686       else if (fmt[i] == 'E')
4687         {
4688           for (j = 0; j < XVECLEN (*x, i); j++)
4689             replace_constant_pool_ref (&XVECEXP (*x, i, j), ref, offset);
4690         }
4691     }
4692 }
4693
4694 /* Check whether X contains an UNSPEC_LTREL_BASE.
4695    Return its constant pool symbol if found, NULL_RTX otherwise.  */
4696
4697 static rtx
4698 find_ltrel_base (rtx x)
4699 {
4700   int i, j;
4701   const char *fmt;
4702
4703   if (GET_CODE (x) == UNSPEC
4704       && XINT (x, 1) == UNSPEC_LTREL_BASE)
4705     return XVECEXP (x, 0, 0);
4706
4707   fmt = GET_RTX_FORMAT (GET_CODE (x));
4708   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
4709     {
4710       if (fmt[i] == 'e')
4711         {
4712           rtx fnd = find_ltrel_base (XEXP (x, i));
4713           if (fnd)
4714             return fnd;
4715         }
4716       else if (fmt[i] == 'E')
4717         {
4718           for (j = 0; j < XVECLEN (x, i); j++)
4719             {
4720               rtx fnd = find_ltrel_base (XVECEXP (x, i, j));
4721               if (fnd)
4722                 return fnd;
4723             }
4724         }
4725     }
4726
4727   return NULL_RTX;
4728 }
4729
4730 /* Replace any occurrence of UNSPEC_LTREL_BASE in X with its base.  */
4731
4732 static void
4733 replace_ltrel_base (rtx *x)
4734 {
4735   int i, j;
4736   const char *fmt;
4737
4738   if (GET_CODE (*x) == UNSPEC
4739       && XINT (*x, 1) == UNSPEC_LTREL_BASE)
4740     {
4741       *x = XVECEXP (*x, 0, 1);
4742       return;
4743     }
4744
4745   fmt = GET_RTX_FORMAT (GET_CODE (*x));
4746   for (i = GET_RTX_LENGTH (GET_CODE (*x)) - 1; i >= 0; i--)
4747     {
4748       if (fmt[i] == 'e')
4749         {
4750           replace_ltrel_base (&XEXP (*x, i));
4751         }
4752       else if (fmt[i] == 'E')
4753         {
4754           for (j = 0; j < XVECLEN (*x, i); j++)
4755             replace_ltrel_base (&XVECEXP (*x, i, j));
4756         }
4757     }
4758 }
4759
4760
4761 /* We keep a list of constants which we have to add to internal
4762    constant tables in the middle of large functions.  */
4763
4764 #define NR_C_MODES 7
4765 enum machine_mode constant_modes[NR_C_MODES] =
4766 {
4767   TImode,
4768   DFmode, DImode,
4769   SFmode, SImode,
4770   HImode,
4771   QImode
4772 };
4773
4774 struct constant
4775 {
4776   struct constant *next;
4777   rtx value;
4778   rtx label;
4779 };
4780
4781 struct constant_pool
4782 {
4783   struct constant_pool *next;
4784   rtx first_insn;
4785   rtx pool_insn;
4786   bitmap insns;
4787
4788   struct constant *constants[NR_C_MODES];
4789   struct constant *execute;
4790   rtx label;
4791   int size;
4792 };
4793
4794 static struct constant_pool * s390_mainpool_start (void);
4795 static void s390_mainpool_finish (struct constant_pool *);
4796 static void s390_mainpool_cancel (struct constant_pool *);
4797
4798 static struct constant_pool * s390_chunkify_start (void);
4799 static void s390_chunkify_finish (struct constant_pool *);
4800 static void s390_chunkify_cancel (struct constant_pool *);
4801
4802 static struct constant_pool *s390_start_pool (struct constant_pool **, rtx);
4803 static void s390_end_pool (struct constant_pool *, rtx);
4804 static void s390_add_pool_insn (struct constant_pool *, rtx);
4805 static struct constant_pool *s390_find_pool (struct constant_pool *, rtx);
4806 static void s390_add_constant (struct constant_pool *, rtx, enum machine_mode);
4807 static rtx s390_find_constant (struct constant_pool *, rtx, enum machine_mode);
4808 static void s390_add_execute (struct constant_pool *, rtx);
4809 static rtx s390_find_execute (struct constant_pool *, rtx);
4810 static rtx s390_execute_label (rtx);
4811 static rtx s390_execute_target (rtx);
4812 static void s390_dump_pool (struct constant_pool *, bool);
4813 static void s390_dump_execute (struct constant_pool *);
4814 static struct constant_pool *s390_alloc_pool (void);
4815 static void s390_free_pool (struct constant_pool *);
4816
4817 /* Create new constant pool covering instructions starting at INSN
4818    and chain it to the end of POOL_LIST.  */
4819
4820 static struct constant_pool *
4821 s390_start_pool (struct constant_pool **pool_list, rtx insn)
4822 {
4823   struct constant_pool *pool, **prev;
4824
4825   pool = s390_alloc_pool ();
4826   pool->first_insn = insn;
4827
4828   for (prev = pool_list; *prev; prev = &(*prev)->next)
4829     ;
4830   *prev = pool;
4831
4832   return pool;
4833 }
4834
4835 /* End range of instructions covered by POOL at INSN and emit
4836    placeholder insn representing the pool.  */
4837
4838 static void
4839 s390_end_pool (struct constant_pool *pool, rtx insn)
4840 {
4841   rtx pool_size = GEN_INT (pool->size + 8 /* alignment slop */);
4842
4843   if (!insn)
4844     insn = get_last_insn ();
4845
4846   pool->pool_insn = emit_insn_after (gen_pool (pool_size), insn);
4847   INSN_ADDRESSES_NEW (pool->pool_insn, -1);
4848 }
4849
4850 /* Add INSN to the list of insns covered by POOL.  */
4851
4852 static void
4853 s390_add_pool_insn (struct constant_pool *pool, rtx insn)
4854 {
4855   bitmap_set_bit (pool->insns, INSN_UID (insn));
4856 }
4857
4858 /* Return pool out of POOL_LIST that covers INSN.  */
4859
4860 static struct constant_pool *
4861 s390_find_pool (struct constant_pool *pool_list, rtx insn)
4862 {
4863   struct constant_pool *pool;
4864
4865   for (pool = pool_list; pool; pool = pool->next)
4866     if (bitmap_bit_p (pool->insns, INSN_UID (insn)))
4867       break;
4868
4869   return pool;
4870 }
4871
4872 /* Add constant VAL of mode MODE to the constant pool POOL.  */
4873
4874 static void
4875 s390_add_constant (struct constant_pool *pool, rtx val, enum machine_mode mode)
4876 {
4877   struct constant *c;
4878   int i;
4879
4880   for (i = 0; i < NR_C_MODES; i++)
4881     if (constant_modes[i] == mode)
4882       break;
4883   if (i == NR_C_MODES)
4884     abort ();
4885
4886   for (c = pool->constants[i]; c != NULL; c = c->next)
4887     if (rtx_equal_p (val, c->value))
4888       break;
4889
4890   if (c == NULL)
4891     {
4892       c = (struct constant *) xmalloc (sizeof *c);
4893       c->value = val;
4894       c->label = gen_label_rtx ();
4895       c->next = pool->constants[i];
4896       pool->constants[i] = c;
4897       pool->size += GET_MODE_SIZE (mode);
4898     }
4899 }
4900
4901 /* Find constant VAL of mode MODE in the constant pool POOL.
4902    Return an RTX describing the distance from the start of
4903    the pool to the location of the new constant.  */
4904
4905 static rtx
4906 s390_find_constant (struct constant_pool *pool, rtx val,
4907                     enum machine_mode mode)
4908 {
4909   struct constant *c;
4910   rtx offset;
4911   int i;
4912
4913   for (i = 0; i < NR_C_MODES; i++)
4914     if (constant_modes[i] == mode)
4915       break;
4916   if (i == NR_C_MODES)
4917     abort ();
4918
4919   for (c = pool->constants[i]; c != NULL; c = c->next)
4920     if (rtx_equal_p (val, c->value))
4921       break;
4922
4923   if (c == NULL)
4924     abort ();
4925
4926   offset = gen_rtx_MINUS (Pmode, gen_rtx_LABEL_REF (Pmode, c->label),
4927                                  gen_rtx_LABEL_REF (Pmode, pool->label));
4928   offset = gen_rtx_CONST (Pmode, offset);
4929   return offset;
4930 }
4931
4932 /* Add execute target for INSN to the constant pool POOL.  */
4933
4934 static void
4935 s390_add_execute (struct constant_pool *pool, rtx insn)
4936 {
4937   struct constant *c;
4938
4939   for (c = pool->execute; c != NULL; c = c->next)
4940     if (INSN_UID (insn) == INSN_UID (c->value))
4941       break;
4942
4943   if (c == NULL)
4944     {
4945       rtx label = s390_execute_label (insn);
4946       gcc_assert (label);
4947
4948       c = (struct constant *) xmalloc (sizeof *c);
4949       c->value = insn;
4950       c->label = label == const0_rtx ? gen_label_rtx () : XEXP (label, 0);
4951       c->next = pool->execute;
4952       pool->execute = c;
4953       pool->size += label == const0_rtx ? 6 : 0;
4954     }
4955 }
4956
4957 /* Find execute target for INSN in the constant pool POOL.
4958    Return an RTX describing the distance from the start of
4959    the pool to the location of the execute target.  */
4960
4961 static rtx
4962 s390_find_execute (struct constant_pool *pool, rtx insn)
4963 {
4964   struct constant *c;
4965   rtx offset;
4966
4967   for (c = pool->execute; c != NULL; c = c->next)
4968     if (INSN_UID (insn) == INSN_UID (c->value))
4969       break;
4970
4971   if (c == NULL)
4972     abort ();
4973
4974   offset = gen_rtx_MINUS (Pmode, gen_rtx_LABEL_REF (Pmode, c->label),
4975                                  gen_rtx_LABEL_REF (Pmode, pool->label));
4976   offset = gen_rtx_CONST (Pmode, offset);
4977   return offset;
4978 }
4979
4980 /* Check whether INSN is an execute.  Return the label_ref to its
4981    execute target template if so, NULL_RTX otherwise.  */
4982
4983 static rtx
4984 s390_execute_label (rtx insn)
4985 {
4986   if (GET_CODE (insn) == INSN
4987       && GET_CODE (PATTERN (insn)) == PARALLEL
4988       && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == UNSPEC
4989       && XINT (XVECEXP (PATTERN (insn), 0, 0), 1) == UNSPEC_EXECUTE)
4990     return XVECEXP (XVECEXP (PATTERN (insn), 0, 0), 0, 2);
4991
4992   return NULL_RTX;
4993 }
4994
4995 /* For an execute INSN, extract the execute target template.  */
4996
4997 static rtx
4998 s390_execute_target (rtx insn)
4999 {
5000   rtx pattern = PATTERN (insn);
5001   gcc_assert (s390_execute_label (insn));
5002
5003   if (XVECLEN (pattern, 0) == 2)
5004     {
5005       pattern = copy_rtx (XVECEXP (pattern, 0, 1));
5006     }
5007   else
5008     {
5009       rtvec vec = rtvec_alloc (XVECLEN (pattern, 0) - 1);
5010       int i;
5011
5012       for (i = 0; i < XVECLEN (pattern, 0) - 1; i++)
5013         RTVEC_ELT (vec, i) = copy_rtx (XVECEXP (pattern, 0, i + 1));
5014
5015       pattern = gen_rtx_PARALLEL (VOIDmode, vec);
5016     }
5017
5018   return pattern;
5019 }
5020
5021 /* Indicate that INSN cannot be duplicated.  This is the case for
5022    execute insns that carry a unique label.  */
5023
5024 static bool
5025 s390_cannot_copy_insn_p (rtx insn)
5026 {
5027   rtx label = s390_execute_label (insn);
5028   return label && label != const0_rtx;
5029 }
5030
5031 /* Dump out the constants in POOL.  If REMOTE_LABEL is true,
5032    do not emit the pool base label.  */
5033
5034 static void
5035 s390_dump_pool (struct constant_pool *pool, bool remote_label)
5036 {
5037   struct constant *c;
5038   rtx insn = pool->pool_insn;
5039   int i;
5040
5041   /* Switch to rodata section.  */
5042   if (TARGET_CPU_ZARCH)
5043     {
5044       insn = emit_insn_after (gen_pool_section_start (), insn);
5045       INSN_ADDRESSES_NEW (insn, -1);
5046     }
5047
5048   /* Ensure minimum pool alignment.  */
5049   if (TARGET_CPU_ZARCH)
5050     insn = emit_insn_after (gen_pool_align (GEN_INT (8)), insn);
5051   else
5052     insn = emit_insn_after (gen_pool_align (GEN_INT (4)), insn);
5053   INSN_ADDRESSES_NEW (insn, -1);
5054
5055   /* Emit pool base label.  */
5056   if (!remote_label)
5057     {
5058       insn = emit_label_after (pool->label, insn);
5059       INSN_ADDRESSES_NEW (insn, -1);
5060     }
5061
5062   /* Dump constants in descending alignment requirement order,
5063      ensuring proper alignment for every constant.  */
5064   for (i = 0; i < NR_C_MODES; i++)
5065     for (c = pool->constants[i]; c; c = c->next)
5066       {
5067         /* Convert UNSPEC_LTREL_OFFSET unspecs to pool-relative references.  */
5068         rtx value = c->value;
5069         if (GET_CODE (value) == CONST
5070             && GET_CODE (XEXP (value, 0)) == UNSPEC
5071             && XINT (XEXP (value, 0), 1) == UNSPEC_LTREL_OFFSET
5072             && XVECLEN (XEXP (value, 0), 0) == 1)
5073           {
5074             value = gen_rtx_MINUS (Pmode, XVECEXP (XEXP (value, 0), 0, 0),
5075                                    gen_rtx_LABEL_REF (VOIDmode, pool->label));
5076             value = gen_rtx_CONST (VOIDmode, value);
5077           }
5078
5079         insn = emit_label_after (c->label, insn);
5080         INSN_ADDRESSES_NEW (insn, -1);
5081
5082         value = gen_rtx_UNSPEC_VOLATILE (constant_modes[i],
5083                                          gen_rtvec (1, value),
5084                                          UNSPECV_POOL_ENTRY);
5085         insn = emit_insn_after (value, insn);
5086         INSN_ADDRESSES_NEW (insn, -1);
5087       }
5088
5089   /* Ensure minimum alignment for instructions.  */
5090   insn = emit_insn_after (gen_pool_align (GEN_INT (2)), insn);
5091   INSN_ADDRESSES_NEW (insn, -1);
5092
5093   /* Output in-pool execute template insns.  */
5094   for (c = pool->execute; c; c = c->next)
5095     {
5096       if (s390_execute_label (c->value) != const0_rtx)
5097         continue;
5098
5099       insn = emit_label_after (c->label, insn);
5100       INSN_ADDRESSES_NEW (insn, -1);
5101
5102       insn = emit_insn_after (s390_execute_target (c->value), insn);
5103       INSN_ADDRESSES_NEW (insn, -1);
5104     }
5105
5106   /* Switch back to previous section.  */
5107   if (TARGET_CPU_ZARCH)
5108     {
5109       insn = emit_insn_after (gen_pool_section_end (), insn);
5110       INSN_ADDRESSES_NEW (insn, -1);
5111     }
5112
5113   insn = emit_barrier_after (insn);
5114   INSN_ADDRESSES_NEW (insn, -1);
5115
5116   /* Remove placeholder insn.  */
5117   remove_insn (pool->pool_insn);
5118
5119   /* Output out-of-pool execute template isns.  */
5120   s390_dump_execute (pool);
5121 }
5122
5123 /* Dump out the out-of-pool execute template insns in POOL
5124    at the end of the instruction stream.  */
5125
5126 static void
5127 s390_dump_execute (struct constant_pool *pool)
5128 {
5129   struct constant *c;
5130   rtx insn;
5131
5132   for (c = pool->execute; c; c = c->next)
5133     {
5134       if (s390_execute_label (c->value) == const0_rtx)
5135         continue;
5136
5137       insn = emit_label (c->label);
5138       INSN_ADDRESSES_NEW (insn, -1);
5139
5140       insn = emit_insn (s390_execute_target (c->value));
5141       INSN_ADDRESSES_NEW (insn, -1);
5142     }
5143 }
5144
5145 /* Allocate new constant_pool structure.  */
5146
5147 static struct constant_pool *
5148 s390_alloc_pool (void)
5149 {
5150   struct constant_pool *pool;
5151   int i;
5152
5153   pool = (struct constant_pool *) xmalloc (sizeof *pool);
5154   pool->next = NULL;
5155   for (i = 0; i < NR_C_MODES; i++)
5156     pool->constants[i] = NULL;
5157
5158   pool->execute = NULL;
5159   pool->label = gen_label_rtx ();
5160   pool->first_insn = NULL_RTX;
5161   pool->pool_insn = NULL_RTX;
5162   pool->insns = BITMAP_XMALLOC ();
5163   pool->size = 0;
5164
5165   return pool;
5166 }
5167
5168 /* Free all memory used by POOL.  */
5169
5170 static void
5171 s390_free_pool (struct constant_pool *pool)
5172 {
5173   struct constant *c, *next;
5174   int i;
5175
5176   for (i = 0; i < NR_C_MODES; i++)
5177     for (c = pool->constants[i]; c; c = next)
5178       {
5179         next = c->next;
5180         free (c);
5181       }
5182
5183   for (c = pool->execute; c; c = next)
5184     {
5185       next = c->next;
5186       free (c);
5187     }
5188
5189   BITMAP_XFREE (pool->insns);
5190   free (pool);
5191 }
5192
5193
5194 /* Collect main literal pool.  Return NULL on overflow.  */
5195
5196 static struct constant_pool *
5197 s390_mainpool_start (void)
5198 {
5199   struct constant_pool *pool;
5200   rtx insn;
5201
5202   pool = s390_alloc_pool ();
5203
5204   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5205     {
5206       if (GET_CODE (insn) == INSN
5207           && GET_CODE (PATTERN (insn)) == SET
5208           && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC_VOLATILE
5209           && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPECV_MAIN_POOL)
5210         {
5211           if (pool->pool_insn)
5212             abort ();
5213           pool->pool_insn = insn;
5214         }
5215
5216       if (s390_execute_label (insn))
5217         {
5218           s390_add_execute (pool, insn);
5219         }
5220       else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
5221         {
5222           rtx pool_ref = NULL_RTX;
5223           find_constant_pool_ref (PATTERN (insn), &pool_ref);
5224           if (pool_ref)
5225             {
5226               rtx constant = get_pool_constant (pool_ref);
5227               enum machine_mode mode = get_pool_mode (pool_ref);
5228               s390_add_constant (pool, constant, mode);
5229             }
5230         }
5231     }
5232
5233   if (!pool->pool_insn && pool->size > 0)
5234     abort ();
5235
5236   if (pool->size >= 4096)
5237     {
5238       /* We're going to chunkify the pool, so remove the main
5239          pool placeholder insn.  */
5240       remove_insn (pool->pool_insn);
5241
5242       s390_free_pool (pool);
5243       pool = NULL;
5244     }
5245
5246   return pool;
5247 }
5248
5249 /* POOL holds the main literal pool as collected by s390_mainpool_start.
5250    Modify the current function to output the pool constants as well as
5251    the pool register setup instruction.  */
5252
5253 static void
5254 s390_mainpool_finish (struct constant_pool *pool)
5255 {
5256   rtx base_reg = cfun->machine->base_reg;
5257   rtx insn;
5258
5259   /* If the pool is empty, we're done.  */
5260   if (pool->size == 0)
5261     {
5262       /* However, we may have out-of-pool execute templates.  */
5263       s390_dump_execute (pool);
5264
5265       /* We don't actually need a base register after all.  */
5266       cfun->machine->base_reg = NULL_RTX;
5267
5268       if (pool->pool_insn)
5269         remove_insn (pool->pool_insn);
5270       s390_free_pool (pool);
5271       return;
5272     }
5273
5274   /* We need correct insn addresses.  */
5275   shorten_branches (get_insns ());
5276
5277   /* On zSeries, we use a LARL to load the pool register.  The pool is
5278      located in the .rodata section, so we emit it after the function.  */
5279   if (TARGET_CPU_ZARCH)
5280     {
5281       insn = gen_main_base_64 (base_reg, pool->label);
5282       insn = emit_insn_after (insn, pool->pool_insn);
5283       INSN_ADDRESSES_NEW (insn, -1);
5284       remove_insn (pool->pool_insn);
5285
5286       insn = get_last_insn ();
5287       pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
5288       INSN_ADDRESSES_NEW (pool->pool_insn, -1);
5289
5290       s390_dump_pool (pool, 0);
5291     }
5292
5293   /* On S/390, if the total size of the function's code plus literal pool
5294      does not exceed 4096 bytes, we use BASR to set up a function base
5295      pointer, and emit the literal pool at the end of the function.  */
5296   else if (INSN_ADDRESSES (INSN_UID (get_last_insn ()))
5297            + pool->size + 8 /* alignment slop */ < 4096)
5298     {
5299       insn = gen_main_base_31_small (base_reg, pool->label);
5300       insn = emit_insn_after (insn, pool->pool_insn);
5301       INSN_ADDRESSES_NEW (insn, -1);
5302       remove_insn (pool->pool_insn);
5303
5304       insn = emit_label_after (pool->label, insn);
5305       INSN_ADDRESSES_NEW (insn, -1);
5306
5307       insn = get_last_insn ();
5308       pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
5309       INSN_ADDRESSES_NEW (pool->pool_insn, -1);
5310
5311       s390_dump_pool (pool, 1);
5312     }
5313
5314   /* Otherwise, we emit an inline literal pool and use BASR to branch
5315      over it, setting up the pool register at the same time.  */
5316   else
5317     {
5318       rtx pool_end = gen_label_rtx ();
5319
5320       insn = gen_main_base_31_large (base_reg, pool->label, pool_end);
5321       insn = emit_insn_after (insn, pool->pool_insn);
5322       INSN_ADDRESSES_NEW (insn, -1);
5323       remove_insn (pool->pool_insn);
5324
5325       insn = emit_label_after (pool->label, insn);
5326       INSN_ADDRESSES_NEW (insn, -1);
5327
5328       pool->pool_insn = emit_insn_after (gen_pool (const0_rtx), insn);
5329       INSN_ADDRESSES_NEW (pool->pool_insn, -1);
5330
5331       insn = emit_label_after (pool_end, pool->pool_insn);
5332       INSN_ADDRESSES_NEW (insn, -1);
5333
5334       s390_dump_pool (pool, 1);
5335     }
5336
5337
5338   /* Replace all literal pool references.  */
5339
5340   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5341     {
5342       if (INSN_P (insn))
5343         replace_ltrel_base (&PATTERN (insn));
5344
5345       if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
5346         {
5347           rtx addr, pool_ref = NULL_RTX;
5348           find_constant_pool_ref (PATTERN (insn), &pool_ref);
5349           if (pool_ref)
5350             {
5351               if (s390_execute_label (insn))
5352                 addr = s390_find_execute (pool, insn);
5353               else
5354                 addr = s390_find_constant (pool, get_pool_constant (pool_ref),
5355                                                  get_pool_mode (pool_ref));
5356
5357               replace_constant_pool_ref (&PATTERN (insn), pool_ref, addr);
5358               INSN_CODE (insn) = -1;
5359             }
5360         }
5361     }
5362
5363
5364   /* Free the pool.  */
5365   s390_free_pool (pool);
5366 }
5367
5368 /* POOL holds the main literal pool as collected by s390_mainpool_start.
5369    We have decided we cannot use this pool, so revert all changes
5370    to the current function that were done by s390_mainpool_start.  */
5371 static void
5372 s390_mainpool_cancel (struct constant_pool *pool)
5373 {
5374   /* We didn't actually change the instruction stream, so simply
5375      free the pool memory.  */
5376   s390_free_pool (pool);
5377 }
5378
5379
5380 /* Chunkify the literal pool.  */
5381
5382 #define S390_POOL_CHUNK_MIN     0xc00
5383 #define S390_POOL_CHUNK_MAX     0xe00
5384
5385 static struct constant_pool *
5386 s390_chunkify_start (void)
5387 {
5388   struct constant_pool *curr_pool = NULL, *pool_list = NULL;
5389   int extra_size = 0;
5390   bitmap far_labels;
5391   rtx pending_ltrel = NULL_RTX;
5392   rtx insn;
5393
5394   rtx (*gen_reload_base) (rtx, rtx) =
5395     TARGET_CPU_ZARCH? gen_reload_base_64 : gen_reload_base_31;
5396
5397
5398   /* We need correct insn addresses.  */
5399
5400   shorten_branches (get_insns ());
5401
5402   /* Scan all insns and move literals to pool chunks.  */
5403
5404   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5405     {
5406       /* Check for pending LTREL_BASE.  */
5407       if (INSN_P (insn))
5408         {
5409           rtx ltrel_base = find_ltrel_base (PATTERN (insn));
5410           if (ltrel_base)
5411             {
5412               if (ltrel_base == pending_ltrel)
5413                 pending_ltrel = NULL_RTX;
5414               else
5415                 abort ();
5416             }
5417         }
5418
5419       if (s390_execute_label (insn))
5420         {
5421           if (!curr_pool)
5422             curr_pool = s390_start_pool (&pool_list, insn);
5423
5424           s390_add_execute (curr_pool, insn);
5425           s390_add_pool_insn (curr_pool, insn);
5426         }
5427       else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
5428         {
5429           rtx pool_ref = NULL_RTX;
5430           find_constant_pool_ref (PATTERN (insn), &pool_ref);
5431           if (pool_ref)
5432             {
5433               rtx constant = get_pool_constant (pool_ref);
5434               enum machine_mode mode = get_pool_mode (pool_ref);
5435
5436               if (!curr_pool)
5437                 curr_pool = s390_start_pool (&pool_list, insn);
5438
5439               s390_add_constant (curr_pool, constant, mode);
5440               s390_add_pool_insn (curr_pool, insn);
5441
5442               /* Don't split the pool chunk between a LTREL_OFFSET load
5443                  and the corresponding LTREL_BASE.  */
5444               if (GET_CODE (constant) == CONST
5445                   && GET_CODE (XEXP (constant, 0)) == UNSPEC
5446                   && XINT (XEXP (constant, 0), 1) == UNSPEC_LTREL_OFFSET)
5447                 {
5448                   if (pending_ltrel)
5449                     abort ();
5450                   pending_ltrel = pool_ref;
5451                 }
5452             }
5453         }
5454
5455       if (GET_CODE (insn) == JUMP_INSN || GET_CODE (insn) == CODE_LABEL)
5456         {
5457           if (curr_pool)
5458             s390_add_pool_insn (curr_pool, insn);
5459           /* An LTREL_BASE must follow within the same basic block.  */
5460           if (pending_ltrel)
5461             abort ();
5462         }
5463
5464       if (!curr_pool
5465           || INSN_ADDRESSES_SIZE () <= (size_t) INSN_UID (insn)
5466           || INSN_ADDRESSES (INSN_UID (insn)) == -1)
5467         continue;
5468
5469       if (TARGET_CPU_ZARCH)
5470         {
5471           if (curr_pool->size < S390_POOL_CHUNK_MAX)
5472             continue;
5473
5474           s390_end_pool (curr_pool, NULL_RTX);
5475           curr_pool = NULL;
5476         }
5477       else
5478         {
5479           int chunk_size = INSN_ADDRESSES (INSN_UID (insn))
5480                            - INSN_ADDRESSES (INSN_UID (curr_pool->first_insn))
5481                          + extra_size;
5482
5483           /* We will later have to insert base register reload insns.
5484              Those will have an effect on code size, which we need to
5485              consider here.  This calculation makes rather pessimistic
5486              worst-case assumptions.  */
5487           if (GET_CODE (insn) == CODE_LABEL)
5488             extra_size += 6;
5489
5490           if (chunk_size < S390_POOL_CHUNK_MIN
5491               && curr_pool->size < S390_POOL_CHUNK_MIN)
5492             continue;
5493
5494           /* Pool chunks can only be inserted after BARRIERs ...  */
5495           if (GET_CODE (insn) == BARRIER)
5496             {
5497               s390_end_pool (curr_pool, insn);
5498               curr_pool = NULL;
5499               extra_size = 0;
5500             }
5501
5502           /* ... so if we don't find one in time, create one.  */
5503           else if ((chunk_size > S390_POOL_CHUNK_MAX
5504                    || curr_pool->size > S390_POOL_CHUNK_MAX))
5505             {
5506               rtx label, jump, barrier;
5507
5508               /* We can insert the barrier only after a 'real' insn.  */
5509               if (GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
5510                 continue;
5511               if (get_attr_length (insn) == 0)
5512                 continue;
5513
5514               /* Don't separate LTREL_BASE from the corresponding
5515                  LTREL_OFFSET load.  */
5516               if (pending_ltrel)
5517                 continue;
5518
5519               label = gen_label_rtx ();
5520               jump = emit_jump_insn_after (gen_jump (label), insn);
5521               barrier = emit_barrier_after (jump);
5522               insn = emit_label_after (label, barrier);
5523               JUMP_LABEL (jump) = label;
5524               LABEL_NUSES (label) = 1;
5525
5526               INSN_ADDRESSES_NEW (jump, -1);
5527               INSN_ADDRESSES_NEW (barrier, -1);
5528               INSN_ADDRESSES_NEW (insn, -1);
5529
5530               s390_end_pool (curr_pool, barrier);
5531               curr_pool = NULL;
5532               extra_size = 0;
5533             }
5534         }
5535     }
5536
5537   if (curr_pool)
5538     s390_end_pool (curr_pool, NULL_RTX);
5539   if (pending_ltrel)
5540     abort ();
5541
5542
5543   /* Find all labels that are branched into
5544      from an insn belonging to a different chunk.  */
5545
5546   far_labels = BITMAP_XMALLOC ();
5547
5548   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5549     {
5550       /* Labels marked with LABEL_PRESERVE_P can be target
5551          of non-local jumps, so we have to mark them.
5552          The same holds for named labels.
5553
5554          Don't do that, however, if it is the label before
5555          a jump table.  */
5556
5557       if (GET_CODE (insn) == CODE_LABEL
5558           && (LABEL_PRESERVE_P (insn) || LABEL_NAME (insn)))
5559         {
5560           rtx vec_insn = next_real_insn (insn);
5561           rtx vec_pat = vec_insn && GET_CODE (vec_insn) == JUMP_INSN ?
5562                         PATTERN (vec_insn) : NULL_RTX;
5563           if (!vec_pat
5564               || !(GET_CODE (vec_pat) == ADDR_VEC
5565                    || GET_CODE (vec_pat) == ADDR_DIFF_VEC))
5566             bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (insn));
5567         }
5568
5569       /* If we have a direct jump (conditional or unconditional)
5570          or a casesi jump, check all potential targets.  */
5571       else if (GET_CODE (insn) == JUMP_INSN)
5572         {
5573           rtx pat = PATTERN (insn);
5574           if (GET_CODE (pat) == PARALLEL && XVECLEN (pat, 0) > 2)
5575             pat = XVECEXP (pat, 0, 0);
5576
5577           if (GET_CODE (pat) == SET)
5578             {
5579               rtx label = JUMP_LABEL (insn);
5580               if (label)
5581                 {
5582                   if (s390_find_pool (pool_list, label)
5583                       != s390_find_pool (pool_list, insn))
5584                     bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (label));
5585                 }
5586             }
5587           else if (GET_CODE (pat) == PARALLEL
5588                    && XVECLEN (pat, 0) == 2
5589                    && GET_CODE (XVECEXP (pat, 0, 0)) == SET
5590                    && GET_CODE (XVECEXP (pat, 0, 1)) == USE
5591                    && GET_CODE (XEXP (XVECEXP (pat, 0, 1), 0)) == LABEL_REF)
5592             {
5593               /* Find the jump table used by this casesi jump.  */
5594               rtx vec_label = XEXP (XEXP (XVECEXP (pat, 0, 1), 0), 0);
5595               rtx vec_insn = next_real_insn (vec_label);
5596               rtx vec_pat = vec_insn && GET_CODE (vec_insn) == JUMP_INSN ?
5597                             PATTERN (vec_insn) : NULL_RTX;
5598               if (vec_pat
5599                   && (GET_CODE (vec_pat) == ADDR_VEC
5600                       || GET_CODE (vec_pat) == ADDR_DIFF_VEC))
5601                 {
5602                   int i, diff_p = GET_CODE (vec_pat) == ADDR_DIFF_VEC;
5603
5604                   for (i = 0; i < XVECLEN (vec_pat, diff_p); i++)
5605                     {
5606                       rtx label = XEXP (XVECEXP (vec_pat, diff_p, i), 0);
5607
5608                       if (s390_find_pool (pool_list, label)
5609                           != s390_find_pool (pool_list, insn))
5610                         bitmap_set_bit (far_labels, CODE_LABEL_NUMBER (label));
5611                     }
5612                 }
5613             }
5614         }
5615     }
5616
5617   /* Insert base register reload insns before every pool.  */
5618
5619   for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
5620     {
5621       rtx new_insn = gen_reload_base (cfun->machine->base_reg, 
5622                                       curr_pool->label);
5623       rtx insn = curr_pool->first_insn;
5624       INSN_ADDRESSES_NEW (emit_insn_before (new_insn, insn), -1);
5625     }
5626
5627   /* Insert base register reload insns at every far label.  */
5628
5629   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5630     if (GET_CODE (insn) == CODE_LABEL
5631         && bitmap_bit_p (far_labels, CODE_LABEL_NUMBER (insn)))
5632       {
5633         struct constant_pool *pool = s390_find_pool (pool_list, insn);
5634         if (pool)
5635           {
5636             rtx new_insn = gen_reload_base (cfun->machine->base_reg, 
5637                                             pool->label);
5638             INSN_ADDRESSES_NEW (emit_insn_after (new_insn, insn), -1);
5639           }
5640       }
5641
5642
5643   BITMAP_XFREE (far_labels);
5644
5645
5646   /* Recompute insn addresses.  */
5647
5648   init_insn_lengths ();
5649   shorten_branches (get_insns ());
5650
5651   return pool_list;
5652 }
5653
5654 /* POOL_LIST is a chunk list as prepared by s390_chunkify_start.
5655    After we have decided to use this list, finish implementing
5656    all changes to the current function as required.  */
5657
5658 static void
5659 s390_chunkify_finish (struct constant_pool *pool_list)
5660 {
5661   struct constant_pool *curr_pool = NULL;
5662   rtx insn;
5663
5664
5665   /* Replace all literal pool references.  */
5666
5667   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5668     {
5669       if (INSN_P (insn))
5670         replace_ltrel_base (&PATTERN (insn));
5671
5672       curr_pool = s390_find_pool (pool_list, insn);
5673       if (!curr_pool)
5674         continue;
5675
5676       if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
5677         {
5678           rtx addr, pool_ref = NULL_RTX;
5679           find_constant_pool_ref (PATTERN (insn), &pool_ref);
5680           if (pool_ref)
5681             {
5682               if (s390_execute_label (insn))
5683                 addr = s390_find_execute (curr_pool, insn);
5684               else
5685                 addr = s390_find_constant (curr_pool,
5686                                            get_pool_constant (pool_ref),
5687                                            get_pool_mode (pool_ref));
5688
5689               replace_constant_pool_ref (&PATTERN (insn), pool_ref, addr);
5690               INSN_CODE (insn) = -1;
5691             }
5692         }
5693     }
5694
5695   /* Dump out all literal pools.  */
5696
5697   for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
5698     s390_dump_pool (curr_pool, 0);
5699
5700   /* Free pool list.  */
5701
5702   while (pool_list)
5703     {
5704       struct constant_pool *next = pool_list->next;
5705       s390_free_pool (pool_list);
5706       pool_list = next;
5707     }
5708 }
5709
5710 /* POOL_LIST is a chunk list as prepared by s390_chunkify_start.
5711    We have decided we cannot use this list, so revert all changes
5712    to the current function that were done by s390_chunkify_start.  */
5713
5714 static void
5715 s390_chunkify_cancel (struct constant_pool *pool_list)
5716 {
5717   struct constant_pool *curr_pool = NULL;
5718   rtx insn;
5719
5720   /* Remove all pool placeholder insns.  */
5721
5722   for (curr_pool = pool_list; curr_pool; curr_pool = curr_pool->next)
5723     {
5724       /* Did we insert an extra barrier?  Remove it.  */
5725       rtx barrier = PREV_INSN (curr_pool->pool_insn);
5726       rtx jump = barrier? PREV_INSN (barrier) : NULL_RTX;
5727       rtx label = NEXT_INSN (curr_pool->pool_insn);
5728
5729       if (jump && GET_CODE (jump) == JUMP_INSN
5730           && barrier && GET_CODE (barrier) == BARRIER
5731           && label && GET_CODE (label) == CODE_LABEL
5732           && GET_CODE (PATTERN (jump)) == SET
5733           && SET_DEST (PATTERN (jump)) == pc_rtx
5734           && GET_CODE (SET_SRC (PATTERN (jump))) == LABEL_REF
5735           && XEXP (SET_SRC (PATTERN (jump)), 0) == label)
5736         {
5737           remove_insn (jump);
5738           remove_insn (barrier);
5739           remove_insn (label);
5740         }
5741
5742       remove_insn (curr_pool->pool_insn);
5743     }
5744
5745   /* Remove all base register reload insns.  */
5746
5747   for (insn = get_insns (); insn; )
5748     {
5749       rtx next_insn = NEXT_INSN (insn);
5750
5751       if (GET_CODE (insn) == INSN
5752           && GET_CODE (PATTERN (insn)) == SET
5753           && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC
5754           && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPEC_RELOAD_BASE)
5755         remove_insn (insn);
5756
5757       insn = next_insn;
5758     }
5759
5760   /* Free pool list.  */
5761
5762   while (pool_list)
5763     {
5764       struct constant_pool *next = pool_list->next;
5765       s390_free_pool (pool_list);
5766       pool_list = next;
5767     }
5768 }
5769
5770
5771 /* Output the constant pool entry EXP in mode MODE with alignment ALIGN.  */
5772
5773 void
5774 s390_output_pool_entry (rtx exp, enum machine_mode mode, unsigned int align)
5775 {
5776   REAL_VALUE_TYPE r;
5777
5778   switch (GET_MODE_CLASS (mode))
5779     {
5780     case MODE_FLOAT:
5781       if (GET_CODE (exp) != CONST_DOUBLE)
5782         abort ();
5783
5784       REAL_VALUE_FROM_CONST_DOUBLE (r, exp);
5785       assemble_real (r, mode, align);
5786       break;
5787
5788     case MODE_INT:
5789       assemble_integer (exp, GET_MODE_SIZE (mode), align, 1);
5790       break;
5791
5792     default:
5793       abort ();
5794     }
5795 }
5796
5797
5798 /* Rework the prologue/epilogue to avoid saving/restoring
5799    registers unnecessarily.  */
5800
5801 static void
5802 s390_optimize_prologue (void)
5803 {
5804   rtx insn, new_insn, next_insn;
5805
5806   /* Do a final recompute of the frame-related data.  */
5807
5808   s390_update_frame_layout ();
5809
5810   /* If all special registers are in fact used, there's nothing we
5811      can do, so no point in walking the insn list.  */
5812
5813   if (cfun_frame_layout.first_save_gpr <= BASE_REGNUM 
5814       && cfun_frame_layout.last_save_gpr >= BASE_REGNUM
5815       && (TARGET_CPU_ZARCH 
5816           || (cfun_frame_layout.first_save_gpr <= RETURN_REGNUM 
5817               && cfun_frame_layout.last_save_gpr >= RETURN_REGNUM)))
5818     return;
5819
5820   /* Search for prologue/epilogue insns and replace them.  */
5821
5822   for (insn = get_insns (); insn; insn = next_insn)
5823     {
5824       int first, last, off;
5825       rtx set, base, offset;
5826
5827       next_insn = NEXT_INSN (insn);
5828
5829       if (GET_CODE (insn) != INSN)
5830         continue;
5831
5832       if (GET_CODE (PATTERN (insn)) == PARALLEL
5833           && store_multiple_operation (PATTERN (insn), VOIDmode))
5834         {
5835           set = XVECEXP (PATTERN (insn), 0, 0);
5836           first = REGNO (SET_SRC (set));
5837           last = first + XVECLEN (PATTERN (insn), 0) - 1;
5838           offset = const0_rtx;
5839           base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
5840           off = INTVAL (offset);
5841
5842           if (GET_CODE (base) != REG || off < 0)
5843             continue;
5844           if (REGNO (base) != STACK_POINTER_REGNUM
5845               && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
5846             continue;
5847           if (first > BASE_REGNUM || last < BASE_REGNUM)
5848             continue;
5849
5850           if (cfun_frame_layout.first_save_gpr != -1)
5851             {
5852               new_insn  = save_gprs (base, 
5853                                      off + (cfun_frame_layout.first_save_gpr
5854                                             - first) * UNITS_PER_WORD, 
5855                                      cfun_frame_layout.first_save_gpr,
5856                                      cfun_frame_layout.last_save_gpr);
5857               new_insn = emit_insn_before (new_insn, insn);
5858               INSN_ADDRESSES_NEW (new_insn, -1);
5859             }
5860
5861           remove_insn (insn);
5862           continue;
5863         }
5864
5865       if (GET_CODE (PATTERN (insn)) == SET
5866           && GET_CODE (SET_SRC (PATTERN (insn))) == REG
5867           && (REGNO (SET_SRC (PATTERN (insn))) == BASE_REGNUM
5868               || (!TARGET_CPU_ZARCH
5869                   && REGNO (SET_SRC (PATTERN (insn))) == RETURN_REGNUM))
5870           && GET_CODE (SET_DEST (PATTERN (insn))) == MEM)
5871         {
5872           set = PATTERN (insn);
5873           first = REGNO (SET_SRC (set));
5874           offset = const0_rtx;
5875           base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
5876           off = INTVAL (offset);
5877
5878           if (GET_CODE (base) != REG || off < 0)
5879             continue;
5880           if (REGNO (base) != STACK_POINTER_REGNUM
5881               && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
5882             continue;
5883           if (cfun_frame_layout.first_save_gpr != -1)
5884             {
5885               new_insn = save_gprs (base, 
5886                                     off + (cfun_frame_layout.first_save_gpr 
5887                                            - first) * UNITS_PER_WORD, 
5888                                     cfun_frame_layout.first_save_gpr,
5889                                     cfun_frame_layout.last_save_gpr);
5890               new_insn = emit_insn_before (new_insn, insn);
5891               INSN_ADDRESSES_NEW (new_insn, -1);
5892             }
5893
5894           remove_insn (insn);
5895           continue;
5896         }
5897
5898       if (GET_CODE (PATTERN (insn)) == PARALLEL
5899           && load_multiple_operation (PATTERN (insn), VOIDmode))
5900         {
5901           set = XVECEXP (PATTERN (insn), 0, 0);
5902           first = REGNO (SET_DEST (set));
5903           last = first + XVECLEN (PATTERN (insn), 0) - 1;
5904           offset = const0_rtx;
5905           base = eliminate_constant_term (XEXP (SET_SRC (set), 0), &offset);
5906           off = INTVAL (offset);
5907
5908           if (GET_CODE (base) != REG || off < 0)
5909             continue;
5910           if (REGNO (base) != STACK_POINTER_REGNUM
5911               && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
5912             continue;
5913           if (first > BASE_REGNUM || last < BASE_REGNUM)
5914             continue;
5915
5916           if (cfun_frame_layout.first_restore_gpr != -1)
5917             {
5918               new_insn = restore_gprs (base, 
5919                                        off + (cfun_frame_layout.first_restore_gpr
5920                                               - first) * UNITS_PER_WORD, 
5921                                        cfun_frame_layout.first_restore_gpr,
5922                                        cfun_frame_layout.last_restore_gpr);
5923               new_insn = emit_insn_before (new_insn, insn);
5924               INSN_ADDRESSES_NEW (new_insn, -1);
5925             }
5926
5927           remove_insn (insn);
5928           continue;
5929         }
5930
5931       if (GET_CODE (PATTERN (insn)) == SET
5932           && GET_CODE (SET_DEST (PATTERN (insn))) == REG
5933           && (REGNO (SET_DEST (PATTERN (insn))) == BASE_REGNUM
5934               || (!TARGET_CPU_ZARCH
5935                   && REGNO (SET_DEST (PATTERN (insn))) == RETURN_REGNUM))
5936           && GET_CODE (SET_SRC (PATTERN (insn))) == MEM)
5937         {
5938           set = PATTERN (insn);
5939           first = REGNO (SET_DEST (set));
5940           offset = const0_rtx;
5941           base = eliminate_constant_term (XEXP (SET_SRC (set), 0), &offset);
5942           off = INTVAL (offset);
5943
5944           if (GET_CODE (base) != REG || off < 0)
5945             continue;
5946           if (REGNO (base) != STACK_POINTER_REGNUM
5947               && REGNO (base) != HARD_FRAME_POINTER_REGNUM)
5948             continue;
5949           if (cfun_frame_layout.first_restore_gpr != -1)
5950             {
5951               new_insn = restore_gprs (base, 
5952                                        off + (cfun_frame_layout.first_restore_gpr 
5953                                               - first) * UNITS_PER_WORD,
5954                                        cfun_frame_layout.first_restore_gpr,
5955                                        cfun_frame_layout.last_restore_gpr);
5956               new_insn = emit_insn_before (new_insn, insn);
5957               INSN_ADDRESSES_NEW (new_insn, -1);
5958             }
5959
5960           remove_insn (insn);
5961           continue;
5962         }
5963     }
5964 }
5965
5966 /* Perform machine-dependent processing.  */
5967
5968 static void
5969 s390_reorg (void)
5970 {
5971   bool pool_overflow = false;
5972
5973   /* Make sure all splits have been performed; splits after
5974      machine_dependent_reorg might confuse insn length counts.  */
5975   split_all_insns_noflow ();
5976
5977
5978   /* Install the main literal pool and the associated base
5979      register load insns.
5980
5981      In addition, there are two problematic situations we need
5982      to correct:
5983
5984      - the literal pool might be > 4096 bytes in size, so that
5985        some of its elements cannot be directly accessed
5986
5987      - a branch target might be > 64K away from the branch, so that
5988        it is not possible to use a PC-relative instruction.
5989
5990      To fix those, we split the single literal pool into multiple
5991      pool chunks, reloading the pool base register at various
5992      points throughout the function to ensure it always points to
5993      the pool chunk the following code expects, and / or replace
5994      PC-relative branches by absolute branches.
5995
5996      However, the two problems are interdependent: splitting the
5997      literal pool can move a branch further away from its target,
5998      causing the 64K limit to overflow, and on the other hand,
5999      replacing a PC-relative branch by an absolute branch means
6000      we need to put the branch target address into the literal
6001      pool, possibly causing it to overflow.
6002
6003      So, we loop trying to fix up both problems until we manage
6004      to satisfy both conditions at the same time.  Note that the
6005      loop is guaranteed to terminate as every pass of the loop
6006      strictly decreases the total number of PC-relative branches
6007      in the function.  (This is not completely true as there
6008      might be branch-over-pool insns introduced by chunkify_start.
6009      Those never need to be split however.)  */
6010
6011   for (;;)
6012     {
6013       struct constant_pool *pool = NULL;
6014
6015       /* Collect the literal pool.  */
6016       if (!pool_overflow)
6017         {
6018           pool = s390_mainpool_start ();
6019           if (!pool)
6020             pool_overflow = true;
6021         }
6022
6023       /* If literal pool overflowed, start to chunkify it.  */
6024       if (pool_overflow)
6025         pool = s390_chunkify_start ();
6026
6027       /* Split out-of-range branches.  If this has created new
6028          literal pool entries, cancel current chunk list and
6029          recompute it.  zSeries machines have large branch
6030          instructions, so we never need to split a branch.  */
6031       if (!TARGET_CPU_ZARCH && s390_split_branches ())
6032         {
6033           if (pool_overflow)
6034             s390_chunkify_cancel (pool);
6035           else
6036             s390_mainpool_cancel (pool);
6037
6038           continue;
6039         }
6040
6041       /* If we made it up to here, both conditions are satisfied.
6042          Finish up literal pool related changes.  */
6043       if (pool_overflow)
6044         s390_chunkify_finish (pool);
6045       else
6046         s390_mainpool_finish (pool);
6047
6048       /* We're done splitting branches.  */
6049       cfun->machine->split_branches_pending_p = false;
6050       break;
6051     }
6052
6053   s390_optimize_prologue ();
6054 }
6055
6056
6057 /* Return an RTL expression representing the value of the return address
6058    for the frame COUNT steps up from the current frame.  FRAME is the
6059    frame pointer of that frame.  */
6060
6061 rtx
6062 s390_return_addr_rtx (int count, rtx frame ATTRIBUTE_UNUSED)
6063 {
6064   int offset;
6065   rtx addr;
6066
6067   /* Without backchain, we fail for all but the current frame.  */
6068
6069   if (!TARGET_BACKCHAIN && !TARGET_KERNEL_BACKCHAIN && count > 0)
6070     return NULL_RTX;
6071
6072   /* For the current frame, we need to make sure the initial
6073      value of RETURN_REGNUM is actually saved.  */
6074
6075   if (count == 0)
6076     {
6077       cfun_frame_layout.save_return_addr_p = true;
6078       return gen_rtx_MEM (Pmode, return_address_pointer_rtx);
6079     }
6080
6081   if (TARGET_BACKCHAIN)
6082     offset = RETURN_REGNUM * UNITS_PER_WORD;
6083   else
6084     offset = -2 * UNITS_PER_WORD;
6085
6086   addr = plus_constant (frame, offset);
6087   addr = memory_address (Pmode, addr);
6088   return gen_rtx_MEM (Pmode, addr);
6089 }
6090
6091 /* Return an RTL expression representing the back chain stored in
6092    the current stack frame.  */
6093
6094 rtx
6095 s390_back_chain_rtx (void)
6096 {
6097   rtx chain;
6098
6099   gcc_assert (TARGET_BACKCHAIN || TARGET_KERNEL_BACKCHAIN);
6100
6101   if (TARGET_BACKCHAIN)
6102     chain = stack_pointer_rtx;
6103   else
6104     chain = plus_constant (stack_pointer_rtx,
6105                            STACK_POINTER_OFFSET - UNITS_PER_WORD);
6106
6107   chain = gen_rtx_MEM (Pmode, chain);
6108   return chain;
6109 }
6110
6111 /* Find first call clobbered register unused in a function.
6112    This could be used as base register in a leaf function
6113    or for holding the return address before epilogue.  */
6114
6115 static int
6116 find_unused_clobbered_reg (void)
6117 {
6118   int i;
6119   for (i = 0; i < 6; i++)
6120     if (!regs_ever_live[i])
6121       return i;
6122   return 0;
6123 }
6124
6125 /* Determine the frame area which actually has to be accessed 
6126    in the function epilogue. The values are stored at the 
6127    given pointers AREA_BOTTOM (address of the lowest used stack
6128    address) and AREA_TOP (address of the first item which does 
6129    not belong to the stack frame).  */
6130
6131 static void
6132 s390_frame_area (int *area_bottom, int *area_top)
6133 {
6134   int b, t;
6135   int i;
6136
6137   b = INT_MAX;
6138   t = INT_MIN;
6139
6140   if (cfun_frame_layout.first_restore_gpr != -1)
6141     {
6142       b = (cfun_frame_layout.gprs_offset
6143            + cfun_frame_layout.first_restore_gpr * UNITS_PER_WORD);
6144       t = b + (cfun_frame_layout.last_restore_gpr
6145                - cfun_frame_layout.first_restore_gpr + 1) * UNITS_PER_WORD;
6146     }
6147
6148   if (TARGET_64BIT && cfun_save_high_fprs_p)
6149     {
6150       b = MIN (b, cfun_frame_layout.f8_offset);
6151       t = MAX (t, (cfun_frame_layout.f8_offset
6152                    + cfun_frame_layout.high_fprs * 8));
6153     }
6154
6155   if (!TARGET_64BIT)
6156     for (i = 2; i < 4; i++)
6157       if (cfun_fpr_bit_p (i))
6158         {
6159           b = MIN (b, cfun_frame_layout.f4_offset + (i - 2) * 8);
6160           t = MAX (t, cfun_frame_layout.f4_offset + (i - 1) * 8);
6161         }
6162   
6163   *area_bottom = b;
6164   *area_top = t;
6165 }
6166
6167 /* Fill cfun->machine with info about register usage of current function.
6168    Return in LIVE_REGS which GPRs are currently considered live.  */
6169
6170 static void
6171 s390_register_info (int live_regs[])
6172 {
6173   int i, j;
6174
6175   /* fprs 8 - 15 are call saved for 64 Bit ABI.  */
6176   cfun_frame_layout.fpr_bitmap = 0;
6177   cfun_frame_layout.high_fprs = 0;
6178   if (TARGET_64BIT)
6179     for (i = 24; i < 32; i++)
6180       if (regs_ever_live[i] && !global_regs[i])
6181         {
6182           cfun_set_fpr_bit (i - 16);
6183           cfun_frame_layout.high_fprs++;
6184         }
6185
6186   /* Find first and last gpr to be saved.  We trust regs_ever_live
6187      data, except that we don't save and restore global registers.
6188
6189      Also, all registers with special meaning to the compiler need
6190      to be handled extra.  */
6191
6192   for (i = 0; i < 16; i++)
6193     live_regs[i] = regs_ever_live[i] && !global_regs[i];
6194
6195   if (flag_pic)
6196     live_regs[PIC_OFFSET_TABLE_REGNUM] 
6197     = regs_ever_live[PIC_OFFSET_TABLE_REGNUM];
6198
6199   live_regs[BASE_REGNUM] 
6200     = cfun->machine->base_reg
6201       && REGNO (cfun->machine->base_reg) == BASE_REGNUM;
6202
6203   live_regs[RETURN_REGNUM]
6204     = cfun->machine->split_branches_pending_p
6205       || cfun_frame_layout.save_return_addr_p;
6206
6207   live_regs[STACK_POINTER_REGNUM]
6208     = !current_function_is_leaf
6209       || TARGET_TPF_PROFILING
6210       || cfun_save_high_fprs_p
6211       || get_frame_size () > 0
6212       || current_function_calls_alloca
6213       || current_function_stdarg;
6214   
6215   for (i = 6; i < 16; i++)
6216     if (live_regs[i])
6217       break;
6218   for (j = 15; j > i; j--)
6219     if (live_regs[j])
6220       break;
6221
6222   if (i == 16)
6223     {
6224       /* Nothing to save/restore.  */
6225       cfun_frame_layout.first_save_gpr = -1;
6226       cfun_frame_layout.first_restore_gpr = -1;
6227       cfun_frame_layout.last_save_gpr = -1;
6228       cfun_frame_layout.last_restore_gpr = -1;
6229     }
6230   else
6231     {
6232       /* Save / Restore from gpr i to j.  */
6233       cfun_frame_layout.first_save_gpr = i;
6234       cfun_frame_layout.first_restore_gpr = i;
6235       cfun_frame_layout.last_save_gpr = j;
6236       cfun_frame_layout.last_restore_gpr = j;
6237     }
6238
6239   if (current_function_stdarg)
6240     {
6241       /* Varargs functions need to save gprs 2 to 6.  */
6242       if (cfun_frame_layout.first_save_gpr == -1
6243           || cfun_frame_layout.first_save_gpr > 2)
6244         cfun_frame_layout.first_save_gpr = 2;
6245
6246       if (cfun_frame_layout.last_save_gpr == -1
6247           || cfun_frame_layout.last_save_gpr < 6)
6248         cfun_frame_layout.last_save_gpr = 6;
6249
6250       /* Mark f0, f2 for 31 bit and f0-f4 for 64 bit to be saved.  */
6251       if (TARGET_HARD_FLOAT)
6252         for (i = 0; i < (TARGET_64BIT ? 4 : 2); i++)
6253           cfun_set_fpr_bit (i);
6254     }
6255
6256   if (!TARGET_64BIT)
6257     for (i = 2; i < 4; i++)
6258       if (regs_ever_live[i + 16] && !global_regs[i + 16])
6259         cfun_set_fpr_bit (i);
6260 }
6261
6262 /* Fill cfun->machine with info about frame of current function.  */
6263
6264 static void
6265 s390_frame_info (void)
6266 {
6267   int i;
6268
6269   cfun_frame_layout.frame_size = get_frame_size ();
6270   if (!TARGET_64BIT && cfun_frame_layout.frame_size > 0x7fff0000)
6271     fatal_error ("Total size of local variables exceeds architecture limit.");
6272   
6273   cfun_frame_layout.save_backchain_p = (TARGET_BACKCHAIN 
6274                                         || TARGET_KERNEL_BACKCHAIN);
6275
6276   if (TARGET_BACKCHAIN)
6277     {
6278       cfun_frame_layout.backchain_offset = 0;
6279       cfun_frame_layout.f0_offset = 16 * UNITS_PER_WORD;
6280       cfun_frame_layout.f4_offset = cfun_frame_layout.f0_offset + 2 * 8;
6281       cfun_frame_layout.f8_offset = -cfun_frame_layout.high_fprs * 8;
6282       cfun_frame_layout.gprs_offset = (cfun_frame_layout.first_save_gpr
6283                                        * UNITS_PER_WORD);
6284     }
6285   else if (TARGET_KERNEL_BACKCHAIN)
6286     {
6287       cfun_frame_layout.backchain_offset = (STACK_POINTER_OFFSET
6288                                             - UNITS_PER_WORD);
6289       cfun_frame_layout.gprs_offset 
6290         = (cfun_frame_layout.backchain_offset 
6291            - (STACK_POINTER_REGNUM - cfun_frame_layout.first_save_gpr + 1)
6292            * UNITS_PER_WORD);
6293           
6294       if (TARGET_64BIT)
6295         {
6296           cfun_frame_layout.f4_offset 
6297             = (cfun_frame_layout.gprs_offset
6298                - 8 * (cfun_fpr_bit_p (2) + cfun_fpr_bit_p (3)));
6299           
6300           cfun_frame_layout.f0_offset 
6301             = (cfun_frame_layout.f4_offset 
6302                - 8 * (cfun_fpr_bit_p (0) + cfun_fpr_bit_p (1)));
6303         }
6304       else
6305         {
6306           /* On 31 bit we have to care about alignment of the
6307              floating point regs to provide fastest access.  */
6308           cfun_frame_layout.f0_offset 
6309             = ((cfun_frame_layout.gprs_offset 
6310                 & ~(STACK_BOUNDARY / BITS_PER_UNIT - 1))
6311                - 8 * (cfun_fpr_bit_p (0) + cfun_fpr_bit_p (1)));
6312           
6313           cfun_frame_layout.f4_offset 
6314             = (cfun_frame_layout.f0_offset
6315                - 8 * (cfun_fpr_bit_p (2) + cfun_fpr_bit_p (3)));
6316         }
6317     }
6318   else /* no backchain */
6319     {
6320       cfun_frame_layout.f4_offset 
6321         = (STACK_POINTER_OFFSET
6322            - 8 * (cfun_fpr_bit_p (2) + cfun_fpr_bit_p (3)));
6323       
6324       cfun_frame_layout.f0_offset 
6325         = (cfun_frame_layout.f4_offset
6326            - 8 * (cfun_fpr_bit_p (0) + cfun_fpr_bit_p (1)));
6327       
6328       cfun_frame_layout.gprs_offset 
6329         = cfun_frame_layout.f0_offset - cfun_gprs_save_area_size;
6330     }
6331
6332   if (current_function_is_leaf
6333       && !TARGET_TPF_PROFILING
6334       && cfun_frame_layout.frame_size == 0
6335       && !cfun_save_high_fprs_p
6336       && !current_function_calls_alloca
6337       && !current_function_stdarg)
6338     return;
6339
6340   if (TARGET_BACKCHAIN)
6341     cfun_frame_layout.frame_size += (STARTING_FRAME_OFFSET
6342                                      + cfun_frame_layout.high_fprs * 8);
6343   else
6344     {
6345       cfun_frame_layout.frame_size += (cfun_frame_layout.save_backchain_p
6346                                        * UNITS_PER_WORD);
6347
6348       /* No alignment trouble here because f8-f15 are only saved under 
6349          64 bit.  */
6350       cfun_frame_layout.f8_offset = (MIN (MIN (cfun_frame_layout.f0_offset,
6351                                                cfun_frame_layout.f4_offset),
6352                                           cfun_frame_layout.gprs_offset)
6353                                      - cfun_frame_layout.high_fprs * 8);
6354
6355       cfun_frame_layout.frame_size += cfun_frame_layout.high_fprs * 8;
6356
6357       for (i = 0; i < 8; i++)
6358         if (cfun_fpr_bit_p (i))
6359           cfun_frame_layout.frame_size += 8;
6360       
6361       cfun_frame_layout.frame_size += cfun_gprs_save_area_size;
6362       
6363       /* If under 31 bit an odd number of gprs has to be saved we have to adjust
6364          the frame size to sustain 8 byte alignment of stack frames.  */
6365       cfun_frame_layout.frame_size = ((cfun_frame_layout.frame_size +
6366                                        STACK_BOUNDARY / BITS_PER_UNIT - 1)
6367                                       & ~(STACK_BOUNDARY / BITS_PER_UNIT - 1));
6368
6369       cfun_frame_layout.frame_size += current_function_outgoing_args_size;
6370     }
6371 }
6372
6373 /* Generate frame layout.  Fills in register and frame data for the current
6374    function in cfun->machine.  This routine can be called multiple times;
6375    it will re-do the complete frame layout every time.  */
6376
6377 static void
6378 s390_init_frame_layout (void)
6379 {
6380   HOST_WIDE_INT frame_size;
6381   int base_used;
6382   int live_regs[16];
6383
6384   /* If return address register is explicitly used, we need to save it.  */
6385   if (regs_ever_live[RETURN_REGNUM]
6386       || !current_function_is_leaf
6387       || TARGET_TPF_PROFILING
6388       || current_function_stdarg
6389       || current_function_calls_eh_return)
6390     cfun_frame_layout.save_return_addr_p = true;
6391
6392   /* On S/390 machines, we may need to perform branch splitting, which
6393      will require both base and return address register.  We have no
6394      choice but to assume we're going to need them until right at the
6395      end of the machine dependent reorg phase.  */
6396   if (!TARGET_CPU_ZARCH)
6397     cfun->machine->split_branches_pending_p = true;
6398
6399   do
6400     {
6401       frame_size = cfun_frame_layout.frame_size;
6402
6403       /* Try to predict whether we'll need the base register.  */
6404       base_used = cfun->machine->split_branches_pending_p
6405                   || current_function_uses_const_pool
6406                   || (!DISP_IN_RANGE (-frame_size)
6407                       && !CONST_OK_FOR_CONSTRAINT_P (-frame_size, 'K', "K"));
6408
6409       /* Decide which register to use as literal pool base.  In small
6410          leaf functions, try to use an unused call-clobbered register
6411          as base register to avoid save/restore overhead.  */
6412       if (!base_used)
6413         cfun->machine->base_reg = NULL_RTX;
6414       else if (current_function_is_leaf && !regs_ever_live[5])
6415         cfun->machine->base_reg = gen_rtx_REG (Pmode, 5);
6416       else
6417         cfun->machine->base_reg = gen_rtx_REG (Pmode, BASE_REGNUM);
6418
6419       s390_register_info (live_regs);
6420       s390_frame_info ();
6421     }
6422   while (frame_size != cfun_frame_layout.frame_size);
6423 }
6424
6425 /* Update frame layout.  Recompute actual register save data based on
6426    current info and update regs_ever_live for the special registers.
6427    May be called multiple times, but may never cause *more* registers
6428    to be saved than s390_init_frame_layout allocated room for.  */
6429
6430 static void
6431 s390_update_frame_layout (void)
6432 {
6433   int live_regs[16];
6434
6435   s390_register_info (live_regs);
6436
6437   regs_ever_live[BASE_REGNUM] = live_regs[BASE_REGNUM];
6438   regs_ever_live[RETURN_REGNUM] = live_regs[RETURN_REGNUM];
6439   regs_ever_live[STACK_POINTER_REGNUM] = live_regs[STACK_POINTER_REGNUM];
6440
6441   if (cfun->machine->base_reg)
6442     regs_ever_live[REGNO (cfun->machine->base_reg)] = 1;
6443 }
6444
6445 /* Return true if register FROM can be eliminated via register TO.  */
6446
6447 bool
6448 s390_can_eliminate (int from, int to)
6449 {
6450   gcc_assert (to == STACK_POINTER_REGNUM
6451               || to == HARD_FRAME_POINTER_REGNUM);
6452
6453   gcc_assert (from == FRAME_POINTER_REGNUM
6454               || from == ARG_POINTER_REGNUM
6455               || from == RETURN_ADDRESS_POINTER_REGNUM);
6456
6457   /* Make sure we actually saved the return address.  */
6458   if (from == RETURN_ADDRESS_POINTER_REGNUM)
6459     if (!current_function_calls_eh_return
6460         && !current_function_stdarg
6461         && !cfun_frame_layout.save_return_addr_p)
6462       return false;
6463
6464   return true;
6465 }
6466
6467 /* Return offset between register FROM and TO initially after prolog.  */
6468
6469 HOST_WIDE_INT
6470 s390_initial_elimination_offset (int from, int to)
6471 {
6472   HOST_WIDE_INT offset;
6473   int index;
6474
6475   /* ??? Why are we called for non-eliminable pairs?  */
6476   if (!s390_can_eliminate (from, to))
6477     return 0;
6478
6479   switch (from)
6480     {
6481     case FRAME_POINTER_REGNUM:
6482       offset = 0;
6483       break;
6484
6485     case ARG_POINTER_REGNUM:
6486       s390_init_frame_layout ();
6487       offset = cfun_frame_layout.frame_size + STACK_POINTER_OFFSET;
6488       break;
6489
6490     case RETURN_ADDRESS_POINTER_REGNUM:
6491       s390_init_frame_layout ();
6492       index = RETURN_REGNUM - cfun_frame_layout.first_save_gpr;
6493       gcc_assert (index >= 0);
6494       offset = cfun_frame_layout.frame_size + cfun_frame_layout.gprs_offset;
6495       offset += index * UNITS_PER_WORD;
6496       break;
6497
6498     default:
6499       gcc_unreachable ();
6500     }
6501
6502   return offset;
6503 }
6504
6505 /* Emit insn to save fpr REGNUM at offset OFFSET relative
6506    to register BASE.  Return generated insn.  */
6507
6508 static rtx
6509 save_fpr (rtx base, int offset, int regnum)
6510 {
6511   rtx addr;
6512   addr = gen_rtx_MEM (DFmode, plus_constant (base, offset));
6513   set_mem_alias_set (addr, s390_sr_alias_set);
6514
6515   return emit_move_insn (addr, gen_rtx_REG (DFmode, regnum));
6516 }
6517
6518 /* Emit insn to restore fpr REGNUM from offset OFFSET relative
6519    to register BASE.  Return generated insn.  */
6520
6521 static rtx
6522 restore_fpr (rtx base, int offset, int regnum)
6523 {
6524   rtx addr;
6525   addr = gen_rtx_MEM (DFmode, plus_constant (base, offset));
6526   set_mem_alias_set (addr, s390_sr_alias_set);
6527
6528   return emit_move_insn (gen_rtx_REG (DFmode, regnum), addr);
6529 }
6530
6531 /* Generate insn to save registers FIRST to LAST into
6532    the register save area located at offset OFFSET
6533    relative to register BASE.  */
6534
6535 static rtx
6536 save_gprs (rtx base, int offset, int first, int last)
6537 {
6538   rtx addr, insn, note;
6539   int i;
6540
6541   addr = plus_constant (base, offset);
6542   addr = gen_rtx_MEM (Pmode, addr);
6543   set_mem_alias_set (addr, s390_sr_alias_set);
6544
6545   /* Special-case single register.  */
6546   if (first == last)
6547     {
6548       if (TARGET_64BIT)
6549         insn = gen_movdi (addr, gen_rtx_REG (Pmode, first));
6550       else
6551         insn = gen_movsi (addr, gen_rtx_REG (Pmode, first));
6552
6553       RTX_FRAME_RELATED_P (insn) = 1;
6554       return insn;
6555     }
6556
6557
6558   insn = gen_store_multiple (addr,
6559                              gen_rtx_REG (Pmode, first),
6560                              GEN_INT (last - first + 1));
6561
6562
6563   /* We need to set the FRAME_RELATED flag on all SETs
6564      inside the store-multiple pattern.
6565
6566      However, we must not emit DWARF records for registers 2..5
6567      if they are stored for use by variable arguments ...
6568
6569      ??? Unfortunately, it is not enough to simply not the the
6570      FRAME_RELATED flags for those SETs, because the first SET
6571      of the PARALLEL is always treated as if it had the flag
6572      set, even if it does not.  Therefore we emit a new pattern
6573      without those registers as REG_FRAME_RELATED_EXPR note.  */
6574
6575   if (first >= 6)
6576     {
6577       rtx pat = PATTERN (insn);
6578
6579       for (i = 0; i < XVECLEN (pat, 0); i++)
6580         if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
6581           RTX_FRAME_RELATED_P (XVECEXP (pat, 0, i)) = 1;
6582
6583       RTX_FRAME_RELATED_P (insn) = 1;
6584     }
6585   else if (last >= 6)
6586     {
6587       addr = plus_constant (base, offset + (6 - first) * UNITS_PER_WORD);
6588       note = gen_store_multiple (gen_rtx_MEM (Pmode, addr),
6589                                  gen_rtx_REG (Pmode, 6),
6590                                  GEN_INT (last - 6 + 1));
6591       note = PATTERN (note);
6592
6593       REG_NOTES (insn) =
6594         gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
6595                            note, REG_NOTES (insn));
6596
6597       for (i = 0; i < XVECLEN (note, 0); i++)
6598         if (GET_CODE (XVECEXP (note, 0, i)) == SET)
6599           RTX_FRAME_RELATED_P (XVECEXP (note, 0, i)) = 1;
6600
6601       RTX_FRAME_RELATED_P (insn) = 1;
6602     }
6603
6604   return insn;
6605 }
6606
6607 /* Generate insn to restore registers FIRST to LAST from
6608    the register save area located at offset OFFSET
6609    relative to register BASE.  */
6610
6611 static rtx
6612 restore_gprs (rtx base, int offset, int first, int last)
6613 {
6614   rtx addr, insn;
6615
6616   addr = plus_constant (base, offset);
6617   addr = gen_rtx_MEM (Pmode, addr);
6618   set_mem_alias_set (addr, s390_sr_alias_set);
6619
6620   /* Special-case single register.  */
6621   if (first == last)
6622     {
6623       if (TARGET_64BIT)
6624         insn = gen_movdi (gen_rtx_REG (Pmode, first), addr);
6625       else
6626         insn = gen_movsi (gen_rtx_REG (Pmode, first), addr);
6627
6628       return insn;
6629     }
6630
6631   insn = gen_load_multiple (gen_rtx_REG (Pmode, first),
6632                             addr,
6633                             GEN_INT (last - first + 1));
6634   return insn;
6635 }
6636
6637 /* Return insn sequence to load the GOT register.  */
6638
6639 static GTY(()) rtx got_symbol;
6640 rtx
6641 s390_load_got (void)
6642 {
6643   rtx insns;
6644
6645   if (!got_symbol)
6646     {
6647       got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
6648       SYMBOL_REF_FLAGS (got_symbol) = SYMBOL_FLAG_LOCAL;
6649     }
6650
6651   start_sequence ();
6652
6653   if (TARGET_CPU_ZARCH)
6654     {
6655       emit_move_insn (pic_offset_table_rtx, got_symbol);
6656     }
6657   else
6658     {
6659       rtx offset;
6660
6661       offset = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, got_symbol),
6662                                UNSPEC_LTREL_OFFSET);
6663       offset = gen_rtx_CONST (Pmode, offset);
6664       offset = force_const_mem (Pmode, offset);
6665
6666       emit_move_insn (pic_offset_table_rtx, offset);
6667
6668       offset = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (offset, 0)),
6669                                UNSPEC_LTREL_BASE);
6670       offset = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, offset);
6671
6672       emit_move_insn (pic_offset_table_rtx, offset);
6673     }
6674
6675   insns = get_insns ();
6676   end_sequence ();
6677   return insns;
6678 }
6679
6680 /* Expand the prologue into a bunch of separate insns.  */
6681
6682 void
6683 s390_emit_prologue (void)
6684 {
6685   rtx insn, addr;
6686   rtx temp_reg;
6687   int i;
6688   int offset;
6689   int next_fpr = 0;
6690
6691   /* Complete frame layout.  */
6692
6693   s390_update_frame_layout ();
6694
6695   /* Annotate all constant pool references to let the scheduler know
6696      they implicitly use the base register.  */
6697
6698   push_topmost_sequence ();
6699
6700   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6701     if (INSN_P (insn))
6702       annotate_constant_pool_refs (&PATTERN (insn));
6703
6704   pop_topmost_sequence ();
6705
6706   /* Choose best register to use for temp use within prologue.
6707      See below for why TPF must use the register 1.  */
6708
6709   if (!current_function_is_leaf && !TARGET_TPF_PROFILING)
6710     temp_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
6711   else
6712     temp_reg = gen_rtx_REG (Pmode, 1);
6713
6714   /* Save call saved gprs.  */
6715   if (cfun_frame_layout.first_save_gpr != -1)
6716     {
6717       insn = save_gprs (stack_pointer_rtx, 
6718                         cfun_frame_layout.gprs_offset,
6719                         cfun_frame_layout.first_save_gpr, 
6720                         cfun_frame_layout.last_save_gpr);
6721       emit_insn (insn);
6722     }
6723
6724   /* Dummy insn to mark literal pool slot.  */
6725
6726   if (cfun->machine->base_reg)
6727     emit_insn (gen_main_pool (cfun->machine->base_reg));
6728
6729   offset = cfun_frame_layout.f0_offset;
6730
6731   /* Save f0 and f2.  */
6732   for (i = 0; i < 2; i++)
6733     {
6734       if (cfun_fpr_bit_p (i))
6735         {
6736           save_fpr (stack_pointer_rtx, offset, i + 16);
6737           offset += 8;
6738         }
6739       else if (TARGET_BACKCHAIN)
6740           offset += 8;
6741     }
6742
6743   /* Save f4 and f6.  */
6744   offset = cfun_frame_layout.f4_offset;
6745   for (i = 2; i < 4; i++)
6746     {
6747       if (cfun_fpr_bit_p (i))
6748         {
6749           insn = save_fpr (stack_pointer_rtx, offset, i + 16);
6750           offset += 8;
6751
6752           /* If f4 and f6 are call clobbered they are saved due to stdargs and
6753              therefore are not frame related.  */
6754           if (!call_really_used_regs[i + 16])
6755             RTX_FRAME_RELATED_P (insn) = 1;
6756         }
6757       else if (TARGET_BACKCHAIN)
6758         offset += 8;
6759     }
6760
6761   if (!TARGET_BACKCHAIN 
6762       && cfun_save_high_fprs_p
6763       && cfun_frame_layout.f8_offset + cfun_frame_layout.high_fprs * 8 > 0)
6764     {
6765       offset = (cfun_frame_layout.f8_offset
6766                 + (cfun_frame_layout.high_fprs - 1) * 8);
6767
6768       for (i = 15; i > 7 && offset >= 0; i--)
6769         if (cfun_fpr_bit_p (i))
6770           {
6771             insn = save_fpr (stack_pointer_rtx, offset, i + 16);
6772                        
6773             RTX_FRAME_RELATED_P (insn) = 1;
6774             offset -= 8;
6775           }
6776       if (offset >= cfun_frame_layout.f8_offset)
6777         next_fpr = i + 16;
6778     }
6779   
6780   if (TARGET_BACKCHAIN)
6781     next_fpr = cfun_save_high_fprs_p ? 31 : 0;
6782
6783   /* Decrement stack pointer.  */
6784
6785   if (cfun_frame_layout.frame_size > 0)
6786     {
6787       rtx frame_off = GEN_INT (-cfun_frame_layout.frame_size);
6788
6789       if (s390_stack_size)
6790         {
6791           HOST_WIDE_INT stack_check_mask = ((s390_stack_size - 1)
6792                                             & ~(s390_stack_guard - 1));
6793           rtx t = gen_rtx_AND (Pmode, stack_pointer_rtx,
6794                                GEN_INT (stack_check_mask));
6795
6796           if (TARGET_64BIT)
6797             gen_cmpdi (t, const0_rtx);
6798           else
6799             gen_cmpsi (t, const0_rtx);
6800
6801           emit_insn (gen_conditional_trap (gen_rtx_EQ (CCmode, 
6802                                                        gen_rtx_REG (CCmode, 
6803                                                                     CC_REGNUM),
6804                                                        const0_rtx),
6805                                            const0_rtx));
6806         }
6807
6808       if (s390_warn_framesize > 0 
6809           && cfun_frame_layout.frame_size >= s390_warn_framesize)
6810         warning ("frame size of `%s' is " HOST_WIDE_INT_PRINT_DEC " bytes", 
6811                  current_function_name (), cfun_frame_layout.frame_size);
6812
6813       if (s390_warn_dynamicstack_p && cfun->calls_alloca)
6814         warning ("`%s' uses dynamic stack allocation", current_function_name ());
6815
6816       /* Save incoming stack pointer into temp reg.  */
6817       if (cfun_frame_layout.save_backchain_p || next_fpr)
6818         insn = emit_insn (gen_move_insn (temp_reg, stack_pointer_rtx));
6819
6820       /* Subtract frame size from stack pointer.  */
6821
6822       if (DISP_IN_RANGE (INTVAL (frame_off)))
6823         {
6824           insn = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
6825                               gen_rtx_PLUS (Pmode, stack_pointer_rtx, 
6826                                             frame_off));
6827           insn = emit_insn (insn);
6828         }
6829       else
6830         {
6831           if (!CONST_OK_FOR_CONSTRAINT_P (INTVAL (frame_off), 'K', "K"))
6832             frame_off = force_const_mem (Pmode, frame_off);
6833
6834           insn = emit_insn (gen_add2_insn (stack_pointer_rtx, frame_off));
6835           annotate_constant_pool_refs (&PATTERN (insn));
6836         }
6837
6838       RTX_FRAME_RELATED_P (insn) = 1;
6839       REG_NOTES (insn) =
6840         gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
6841                            gen_rtx_SET (VOIDmode, stack_pointer_rtx,
6842                              gen_rtx_PLUS (Pmode, stack_pointer_rtx,
6843                                GEN_INT (-cfun_frame_layout.frame_size))),
6844                            REG_NOTES (insn));
6845
6846       /* Set backchain.  */
6847
6848       if (cfun_frame_layout.save_backchain_p)
6849         {
6850           if (cfun_frame_layout.backchain_offset)
6851             addr = gen_rtx_MEM (Pmode, 
6852                                 plus_constant (stack_pointer_rtx, 
6853                                   cfun_frame_layout.backchain_offset));
6854           else
6855             addr = gen_rtx_MEM (Pmode, stack_pointer_rtx);  
6856           set_mem_alias_set (addr, s390_sr_alias_set);
6857           insn = emit_insn (gen_move_insn (addr, temp_reg));
6858         }
6859
6860       /* If we support asynchronous exceptions (e.g. for Java),
6861          we need to make sure the backchain pointer is set up
6862          before any possibly trapping memory access.  */
6863
6864       if (cfun_frame_layout.save_backchain_p && flag_non_call_exceptions)
6865         {
6866           addr = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
6867           emit_insn (gen_rtx_CLOBBER (VOIDmode, addr));
6868         }
6869     }
6870
6871   /* Save fprs 8 - 15 (64 bit ABI).  */
6872
6873   if (cfun_save_high_fprs_p && next_fpr)
6874     {
6875       insn = emit_insn (gen_add2_insn (temp_reg, 
6876                                        GEN_INT (cfun_frame_layout.f8_offset)));
6877
6878       offset = 0;
6879
6880       for (i = 24; i <= next_fpr; i++)
6881         if (cfun_fpr_bit_p (i - 16))
6882           {
6883             rtx addr = plus_constant (stack_pointer_rtx,
6884                                       cfun_frame_layout.frame_size
6885                                       + cfun_frame_layout.f8_offset
6886                                       + offset);
6887             
6888             insn = save_fpr (temp_reg, offset, i);
6889             offset += 8;
6890             RTX_FRAME_RELATED_P (insn) = 1;
6891             REG_NOTES (insn) =
6892               gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
6893                                  gen_rtx_SET (VOIDmode,
6894                                               gen_rtx_MEM (DFmode, addr),
6895                                               gen_rtx_REG (DFmode, i)),
6896                                  REG_NOTES (insn));
6897           }
6898     }
6899
6900   /* Set frame pointer, if needed.  */
6901
6902   if (frame_pointer_needed)
6903     {
6904       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
6905       RTX_FRAME_RELATED_P (insn) = 1;
6906     }
6907
6908   /* Set up got pointer, if needed.  */
6909
6910   if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
6911     {
6912       rtx insns = s390_load_got ();
6913
6914       for (insn = insns; insn; insn = NEXT_INSN (insn))
6915         {
6916           annotate_constant_pool_refs (&PATTERN (insn));
6917
6918           REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, NULL_RTX,
6919                                                 REG_NOTES (insn));
6920         }
6921
6922       emit_insn (insns);
6923     }
6924
6925   if (TARGET_TPF_PROFILING)
6926     {
6927       /* Generate a BAS instruction to serve as a function
6928          entry intercept to facilitate the use of tracing
6929          algorithms located at the branch target.  */
6930       emit_insn (gen_prologue_tpf ());
6931
6932       /* Emit a blockage here so that all code
6933          lies between the profiling mechanisms.  */
6934       emit_insn (gen_blockage ());
6935     }
6936 }
6937
6938 /* Expand the epilogue into a bunch of separate insns.  */
6939
6940 void
6941 s390_emit_epilogue (bool sibcall)
6942 {
6943   rtx frame_pointer, return_reg;
6944   int area_bottom, area_top, offset = 0;
6945   int next_offset;
6946   rtvec p;
6947   int i;
6948
6949   if (TARGET_TPF_PROFILING)
6950     {
6951
6952       /* Generate a BAS instruction to serve as a function
6953          entry intercept to facilitate the use of tracing
6954          algorithms located at the branch target.  */
6955
6956       /* Emit a blockage here so that all code
6957          lies between the profiling mechanisms.  */
6958       emit_insn (gen_blockage ());
6959
6960       emit_insn (gen_epilogue_tpf ());
6961     }
6962
6963   /* Check whether to use frame or stack pointer for restore.  */
6964
6965   frame_pointer = (frame_pointer_needed 
6966                    ? hard_frame_pointer_rtx : stack_pointer_rtx);
6967
6968   s390_frame_area (&area_bottom, &area_top);
6969
6970   /* Check whether we can access the register save area.
6971      If not, increment the frame pointer as required.  */
6972
6973   if (area_top <= area_bottom)
6974     {
6975       /* Nothing to restore.  */
6976     }
6977   else if (DISP_IN_RANGE (cfun_frame_layout.frame_size + area_bottom)
6978            && DISP_IN_RANGE (cfun_frame_layout.frame_size + area_top - 1))
6979     {
6980       /* Area is in range.  */
6981       offset = cfun_frame_layout.frame_size;
6982     }
6983   else
6984     {
6985       rtx insn, frame_off;
6986
6987       offset = area_bottom < 0 ? -area_bottom : 0;
6988       frame_off = GEN_INT (cfun_frame_layout.frame_size - offset);
6989
6990       if (DISP_IN_RANGE (INTVAL (frame_off)))
6991         {
6992           insn = gen_rtx_SET (VOIDmode, frame_pointer,
6993                               gen_rtx_PLUS (Pmode, frame_pointer, frame_off));
6994           insn = emit_insn (insn);
6995         }
6996       else
6997         {
6998           if (!CONST_OK_FOR_CONSTRAINT_P (INTVAL (frame_off), 'K', "K"))
6999             frame_off = force_const_mem (Pmode, frame_off);
7000
7001           insn = emit_insn (gen_add2_insn (frame_pointer, frame_off));
7002           annotate_constant_pool_refs (&PATTERN (insn));
7003         }
7004     }
7005
7006   /* Restore call saved fprs.  */
7007
7008   if (TARGET_64BIT)
7009     {
7010       if (cfun_save_high_fprs_p)
7011         {
7012           next_offset = cfun_frame_layout.f8_offset;
7013           for (i = 24; i < 32; i++)
7014             {
7015               if (cfun_fpr_bit_p (i - 16))
7016                 {
7017                   restore_fpr (frame_pointer,
7018                                offset + next_offset, i);
7019                   next_offset += 8;
7020                 }
7021             }
7022         }
7023               
7024     }
7025   else
7026     {
7027       next_offset = cfun_frame_layout.f4_offset;
7028       for (i = 18; i < 20; i++)
7029         {
7030           if (cfun_fpr_bit_p (i - 16))
7031             {
7032               restore_fpr (frame_pointer,
7033                            offset + next_offset, i);
7034               next_offset += 8;
7035             }
7036           else if (TARGET_BACKCHAIN)
7037             next_offset += 8;
7038         }
7039       
7040     }
7041
7042   /* Return register.  */
7043
7044   return_reg = gen_rtx_REG (Pmode, RETURN_REGNUM);
7045
7046   /* Restore call saved gprs.  */
7047
7048   if (cfun_frame_layout.first_restore_gpr != -1)
7049     {
7050       rtx insn, addr;
7051       int i;
7052
7053       /* Check for global register and save them
7054          to stack location from where they get restored.  */
7055
7056       for (i = cfun_frame_layout.first_restore_gpr;
7057            i <= cfun_frame_layout.last_restore_gpr;
7058            i++)
7059         {
7060           /* These registers are special and need to be
7061              restored in any case.  */
7062           if (i == STACK_POINTER_REGNUM
7063               || i == RETURN_REGNUM
7064               || i == BASE_REGNUM
7065               || (flag_pic && i == (int)PIC_OFFSET_TABLE_REGNUM))
7066             continue;
7067
7068           if (global_regs[i])
7069             {
7070               addr = plus_constant (frame_pointer,
7071                                     offset + cfun_frame_layout.gprs_offset 
7072                                     + (i - cfun_frame_layout.first_save_gpr)
7073                                     * UNITS_PER_WORD);
7074               addr = gen_rtx_MEM (Pmode, addr);
7075               set_mem_alias_set (addr, s390_sr_alias_set);
7076               emit_move_insn (addr, gen_rtx_REG (Pmode, i));
7077             }
7078         }
7079
7080       if (! sibcall)
7081         {
7082           /* Fetch return address from stack before load multiple,
7083              this will do good for scheduling.  */
7084
7085           if (cfun_frame_layout.save_return_addr_p
7086               || (cfun_frame_layout.first_restore_gpr < BASE_REGNUM
7087                   && cfun_frame_layout.last_restore_gpr > RETURN_REGNUM))
7088             {
7089               int return_regnum = find_unused_clobbered_reg();
7090               if (!return_regnum)
7091                 return_regnum = 4;
7092               return_reg = gen_rtx_REG (Pmode, return_regnum);
7093
7094               addr = plus_constant (frame_pointer,
7095                                     offset + cfun_frame_layout.gprs_offset
7096                                     + (RETURN_REGNUM 
7097                                        - cfun_frame_layout.first_save_gpr)
7098                                     * UNITS_PER_WORD);
7099               addr = gen_rtx_MEM (Pmode, addr);
7100               set_mem_alias_set (addr, s390_sr_alias_set);
7101               emit_move_insn (return_reg, addr);
7102             }
7103         }
7104
7105       insn = restore_gprs (frame_pointer,
7106                            offset + cfun_frame_layout.gprs_offset
7107                            + (cfun_frame_layout.first_restore_gpr 
7108                               - cfun_frame_layout.first_save_gpr)
7109                            * UNITS_PER_WORD,
7110                            cfun_frame_layout.first_restore_gpr,
7111                            cfun_frame_layout.last_restore_gpr);
7112       emit_insn (insn);
7113     }
7114
7115   if (! sibcall)
7116     {
7117
7118       /* Return to caller.  */
7119
7120       p = rtvec_alloc (2);
7121
7122       RTVEC_ELT (p, 0) = gen_rtx_RETURN (VOIDmode);
7123       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, return_reg);
7124       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
7125     }
7126 }
7127
7128
7129 /* Return the size in bytes of a function argument of
7130    type TYPE and/or mode MODE.  At least one of TYPE or
7131    MODE must be specified.  */
7132
7133 static int
7134 s390_function_arg_size (enum machine_mode mode, tree type)
7135 {
7136   if (type)
7137     return int_size_in_bytes (type);
7138
7139   /* No type info available for some library calls ...  */
7140   if (mode != BLKmode)
7141     return GET_MODE_SIZE (mode);
7142
7143   /* If we have neither type nor mode, abort */
7144   abort ();
7145 }
7146
7147 /* Return true if a function argument of type TYPE and mode MODE
7148    is to be passed in a floating-point register, if available.  */
7149
7150 static bool
7151 s390_function_arg_float (enum machine_mode mode, tree type)
7152 {
7153   int size = s390_function_arg_size (mode, type);
7154   if (size > 8)
7155     return false;
7156
7157   /* Soft-float changes the ABI: no floating-point registers are used.  */
7158   if (TARGET_SOFT_FLOAT)
7159     return false;
7160
7161   /* No type info available for some library calls ...  */
7162   if (!type)
7163     return mode == SFmode || mode == DFmode;
7164
7165   /* The ABI says that record types with a single member are treated
7166      just like that member would be.  */
7167   while (TREE_CODE (type) == RECORD_TYPE)
7168     {
7169       tree field, single = NULL_TREE;
7170
7171       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
7172         {
7173           if (TREE_CODE (field) != FIELD_DECL)
7174             continue;
7175
7176           if (single == NULL_TREE)
7177             single = TREE_TYPE (field);
7178           else
7179             return false;
7180         }
7181
7182       if (single == NULL_TREE)
7183         return false;
7184       else
7185         type = single;
7186     }
7187
7188   return TREE_CODE (type) == REAL_TYPE;
7189 }
7190
7191 /* Return true if a function argument of type TYPE and mode MODE
7192    is to be passed in an integer register, or a pair of integer
7193    registers, if available.  */
7194
7195 static bool
7196 s390_function_arg_integer (enum machine_mode mode, tree type)
7197 {
7198   int size = s390_function_arg_size (mode, type);
7199   if (size > 8)
7200     return false;
7201
7202   /* No type info available for some library calls ...  */
7203   if (!type)
7204     return GET_MODE_CLASS (mode) == MODE_INT
7205            || (TARGET_SOFT_FLOAT &&  GET_MODE_CLASS (mode) == MODE_FLOAT);
7206
7207   /* We accept small integral (and similar) types.  */
7208   if (INTEGRAL_TYPE_P (type)
7209       || POINTER_TYPE_P (type)
7210       || TREE_CODE (type) == OFFSET_TYPE
7211       || (TARGET_SOFT_FLOAT && TREE_CODE (type) == REAL_TYPE))
7212     return true;
7213
7214   /* We also accept structs of size 1, 2, 4, 8 that are not
7215      passed in floating-point registers.  */
7216   if (AGGREGATE_TYPE_P (type)
7217       && exact_log2 (size) >= 0
7218       && !s390_function_arg_float (mode, type))
7219     return true;
7220
7221   return false;
7222 }
7223
7224 /* Return 1 if a function argument of type TYPE and mode MODE
7225    is to be passed by reference.  The ABI specifies that only
7226    structures of size 1, 2, 4, or 8 bytes are passed by value,
7227    all other structures (and complex numbers) are passed by
7228    reference.  */
7229
7230 static bool
7231 s390_pass_by_reference (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED,
7232                         enum machine_mode mode, tree type,
7233                         bool named ATTRIBUTE_UNUSED)
7234 {
7235   int size = s390_function_arg_size (mode, type);
7236   if (size > 8)
7237     return true;
7238
7239   if (type)
7240     {
7241       if (AGGREGATE_TYPE_P (type) && exact_log2 (size) < 0)
7242         return 1;
7243
7244       if (TREE_CODE (type) == COMPLEX_TYPE
7245           || TREE_CODE (type) == VECTOR_TYPE)
7246         return 1;
7247     }
7248
7249   return 0;
7250 }
7251
7252 /* Update the data in CUM to advance over an argument of mode MODE and
7253    data type TYPE.  (TYPE is null for libcalls where that information
7254    may not be available.).  The boolean NAMED specifies whether the
7255    argument is a named argument (as opposed to an unnamed argument
7256    matching an ellipsis).  */
7257
7258 void
7259 s390_function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
7260                            tree type, int named ATTRIBUTE_UNUSED)
7261 {
7262   if (s390_function_arg_float (mode, type))
7263     {
7264       cum->fprs += 1;
7265     }
7266   else if (s390_function_arg_integer (mode, type))
7267     {
7268       int size = s390_function_arg_size (mode, type);
7269       cum->gprs += ((size + UNITS_PER_WORD-1) / UNITS_PER_WORD);
7270     }
7271   else
7272     abort ();
7273 }
7274
7275 /* Define where to put the arguments to a function.
7276    Value is zero to push the argument on the stack,
7277    or a hard register in which to store the argument.
7278
7279    MODE is the argument's machine mode.
7280    TYPE is the data type of the argument (as a tree).
7281     This is null for libcalls where that information may
7282     not be available.
7283    CUM is a variable of type CUMULATIVE_ARGS which gives info about
7284     the preceding args and about the function being called.
7285    NAMED is nonzero if this argument is a named parameter
7286     (otherwise it is an extra parameter matching an ellipsis).
7287
7288    On S/390, we use general purpose registers 2 through 6 to
7289    pass integer, pointer, and certain structure arguments, and
7290    floating point registers 0 and 2 (0, 2, 4, and 6 on 64-bit)
7291    to pass floating point arguments.  All remaining arguments
7292    are pushed to the stack.  */
7293
7294 rtx
7295 s390_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, tree type,
7296                    int named ATTRIBUTE_UNUSED)
7297 {
7298   if (s390_function_arg_float (mode, type))
7299     {
7300       if (cum->fprs + 1 > (TARGET_64BIT? 4 : 2))
7301         return 0;
7302       else
7303         return gen_rtx_REG (mode, cum->fprs + 16);
7304     }
7305   else if (s390_function_arg_integer (mode, type))
7306     {
7307       int size = s390_function_arg_size (mode, type);
7308       int n_gprs = (size + UNITS_PER_WORD-1) / UNITS_PER_WORD;
7309
7310       if (cum->gprs + n_gprs > 5)
7311         return 0;
7312       else
7313         return gen_rtx_REG (mode, cum->gprs + 2);
7314     }
7315
7316   /* After the real arguments, expand_call calls us once again
7317      with a void_type_node type.  Whatever we return here is
7318      passed as operand 2 to the call expanders.
7319
7320      We don't need this feature ...  */
7321   else if (type == void_type_node)
7322     return const0_rtx;
7323
7324   abort ();
7325 }
7326
7327 /* Return true if return values of type TYPE should be returned
7328    in a memory buffer whose address is passed by the caller as
7329    hidden first argument.  */
7330
7331 static bool
7332 s390_return_in_memory (tree type, tree fundecl ATTRIBUTE_UNUSED)
7333 {
7334   /* We accept small integral (and similar) types.  */
7335   if (INTEGRAL_TYPE_P (type)
7336       || POINTER_TYPE_P (type)
7337       || TREE_CODE (type) == OFFSET_TYPE
7338       || TREE_CODE (type) == REAL_TYPE)
7339     return int_size_in_bytes (type) > 8;
7340
7341   /* Aggregates and similar constructs are always returned
7342      in memory.  */
7343   if (AGGREGATE_TYPE_P (type)
7344       || TREE_CODE (type) == COMPLEX_TYPE
7345       || TREE_CODE (type) == VECTOR_TYPE)
7346     return true;
7347
7348   /* ??? We get called on all sorts of random stuff from
7349      aggregate_value_p.  We can't abort, but it's not clear
7350      what's safe to return.  Pretend it's a struct I guess.  */
7351   return true;
7352 }
7353
7354 /* Define where to return a (scalar) value of type TYPE.
7355    If TYPE is null, define where to return a (scalar)
7356    value of mode MODE from a libcall.  */
7357
7358 rtx
7359 s390_function_value (tree type, enum machine_mode mode)
7360 {
7361   if (type)
7362     {
7363       int unsignedp = TYPE_UNSIGNED (type);
7364       mode = promote_mode (type, TYPE_MODE (type), &unsignedp, 1);
7365     }
7366
7367   if (GET_MODE_CLASS (mode) != MODE_INT
7368       && GET_MODE_CLASS (mode) != MODE_FLOAT)
7369     abort ();
7370   if (GET_MODE_SIZE (mode) > 8)
7371     abort ();
7372
7373   if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
7374     return gen_rtx_REG (mode, 16);
7375   else
7376     return gen_rtx_REG (mode, 2);
7377 }
7378
7379
7380 /* Create and return the va_list datatype.
7381
7382    On S/390, va_list is an array type equivalent to
7383
7384       typedef struct __va_list_tag
7385         {
7386             long __gpr;
7387             long __fpr;
7388             void *__overflow_arg_area;
7389             void *__reg_save_area;
7390         } va_list[1];
7391
7392    where __gpr and __fpr hold the number of general purpose
7393    or floating point arguments used up to now, respectively,
7394    __overflow_arg_area points to the stack location of the
7395    next argument passed on the stack, and __reg_save_area
7396    always points to the start of the register area in the
7397    call frame of the current function.  The function prologue
7398    saves all registers used for argument passing into this
7399    area if the function uses variable arguments.  */
7400
7401 static tree
7402 s390_build_builtin_va_list (void)
7403 {
7404   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
7405
7406   record = lang_hooks.types.make_type (RECORD_TYPE);
7407
7408   type_decl =
7409     build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
7410
7411   f_gpr = build_decl (FIELD_DECL, get_identifier ("__gpr"),
7412                       long_integer_type_node);
7413   f_fpr = build_decl (FIELD_DECL, get_identifier ("__fpr"),
7414                       long_integer_type_node);
7415   f_ovf = build_decl (FIELD_DECL, get_identifier ("__overflow_arg_area"),
7416                       ptr_type_node);
7417   f_sav = build_decl (FIELD_DECL, get_identifier ("__reg_save_area"),
7418                       ptr_type_node);
7419
7420   DECL_FIELD_CONTEXT (f_gpr) = record;
7421   DECL_FIELD_CONTEXT (f_fpr) = record;
7422   DECL_FIELD_CONTEXT (f_ovf) = record;
7423   DECL_FIELD_CONTEXT (f_sav) = record;
7424
7425   TREE_CHAIN (record) = type_decl;
7426   TYPE_NAME (record) = type_decl;
7427   TYPE_FIELDS (record) = f_gpr;
7428   TREE_CHAIN (f_gpr) = f_fpr;
7429   TREE_CHAIN (f_fpr) = f_ovf;
7430   TREE_CHAIN (f_ovf) = f_sav;
7431
7432   layout_type (record);
7433
7434   /* The correct type is an array type of one element.  */
7435   return build_array_type (record, build_index_type (size_zero_node));
7436 }
7437
7438 /* Implement va_start by filling the va_list structure VALIST.
7439    STDARG_P is always true, and ignored.
7440    NEXTARG points to the first anonymous stack argument.
7441
7442    The following global variables are used to initialize
7443    the va_list structure:
7444
7445      current_function_args_info:
7446        holds number of gprs and fprs used for named arguments.
7447      current_function_arg_offset_rtx:
7448        holds the offset of the first anonymous stack argument
7449        (relative to the virtual arg pointer).  */
7450
7451 void
7452 s390_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
7453 {
7454   HOST_WIDE_INT n_gpr, n_fpr;
7455   int off;
7456   tree f_gpr, f_fpr, f_ovf, f_sav;
7457   tree gpr, fpr, ovf, sav, t;
7458
7459   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
7460   f_fpr = TREE_CHAIN (f_gpr);
7461   f_ovf = TREE_CHAIN (f_fpr);
7462   f_sav = TREE_CHAIN (f_ovf);
7463
7464   valist = build_va_arg_indirect_ref (valist);
7465   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
7466   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
7467   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
7468   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
7469
7470   /* Count number of gp and fp argument registers used.  */
7471
7472   n_gpr = current_function_args_info.gprs;
7473   n_fpr = current_function_args_info.fprs;
7474
7475   t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
7476              build_int_cst (NULL_TREE, n_gpr));
7477   TREE_SIDE_EFFECTS (t) = 1;
7478   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7479
7480   t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
7481              build_int_cst (NULL_TREE, n_fpr));
7482   TREE_SIDE_EFFECTS (t) = 1;
7483   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7484
7485   /* Find the overflow area.  */
7486   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
7487
7488   off = INTVAL (current_function_arg_offset_rtx);
7489   off = off < 0 ? 0 : off;
7490   if (TARGET_DEBUG_ARG)
7491     fprintf (stderr, "va_start: n_gpr = %d, n_fpr = %d off %d\n",
7492              (int)n_gpr, (int)n_fpr, off);
7493
7494   t = build (PLUS_EXPR, TREE_TYPE (ovf), t, build_int_cst (NULL_TREE, off));
7495
7496   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
7497   TREE_SIDE_EFFECTS (t) = 1;
7498   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7499
7500   /* Find the register save area.  */
7501   t = make_tree (TREE_TYPE (sav), return_address_pointer_rtx);
7502   if (TARGET_KERNEL_BACKCHAIN)
7503     t = build (PLUS_EXPR, TREE_TYPE (sav), t,
7504                build_int_cst (NULL_TREE,
7505                               -(RETURN_REGNUM - 2) * UNITS_PER_WORD
7506                               - (TARGET_64BIT ? 4 : 2) * 8));
7507   else
7508     t = build (PLUS_EXPR, TREE_TYPE (sav), t,
7509                build_int_cst (NULL_TREE, -RETURN_REGNUM * UNITS_PER_WORD));
7510
7511   t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
7512   TREE_SIDE_EFFECTS (t) = 1;
7513   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7514 }
7515
7516 /* Implement va_arg by updating the va_list structure
7517    VALIST as required to retrieve an argument of type
7518    TYPE, and returning that argument.
7519
7520    Generates code equivalent to:
7521
7522    if (integral value) {
7523      if (size  <= 4 && args.gpr < 5 ||
7524          size  > 4 && args.gpr < 4 )
7525        ret = args.reg_save_area[args.gpr+8]
7526      else
7527        ret = *args.overflow_arg_area++;
7528    } else if (float value) {
7529      if (args.fgpr < 2)
7530        ret = args.reg_save_area[args.fpr+64]
7531      else
7532        ret = *args.overflow_arg_area++;
7533    } else if (aggregate value) {
7534      if (args.gpr < 5)
7535        ret = *args.reg_save_area[args.gpr]
7536      else
7537        ret = **args.overflow_arg_area++;
7538    } */
7539
7540 tree
7541 s390_gimplify_va_arg (tree valist, tree type, tree *pre_p, 
7542                       tree *post_p ATTRIBUTE_UNUSED)
7543 {
7544   tree f_gpr, f_fpr, f_ovf, f_sav;
7545   tree gpr, fpr, ovf, sav, reg, t, u;
7546   int indirect_p, size, n_reg, sav_ofs, sav_scale, max_reg;
7547   tree lab_false, lab_over, addr;
7548
7549   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
7550   f_fpr = TREE_CHAIN (f_gpr);
7551   f_ovf = TREE_CHAIN (f_fpr);
7552   f_sav = TREE_CHAIN (f_ovf);
7553
7554   valist = build_va_arg_indirect_ref (valist);
7555   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
7556   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
7557   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
7558   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
7559
7560   size = int_size_in_bytes (type);
7561
7562   if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
7563     {
7564       if (TARGET_DEBUG_ARG)
7565         {
7566           fprintf (stderr, "va_arg: aggregate type");
7567           debug_tree (type);
7568         }
7569
7570       /* Aggregates are passed by reference.  */
7571       indirect_p = 1;
7572       reg = gpr;
7573       n_reg = 1;
7574
7575       /* TARGET_KERNEL_BACKCHAIN on 31 bit: It is assumed here that no padding
7576          will be added by s390_frame_info because for va_args always an even
7577          number of gprs has to be saved r15-r2 = 14 regs.  */
7578       sav_ofs = (TARGET_KERNEL_BACKCHAIN
7579                  ? (TARGET_64BIT ? 4 : 2) * 8 : 2 * UNITS_PER_WORD);
7580       sav_scale = UNITS_PER_WORD;
7581       size = UNITS_PER_WORD;
7582       max_reg = 4;
7583     }
7584   else if (s390_function_arg_float (TYPE_MODE (type), type))
7585     {
7586       if (TARGET_DEBUG_ARG)
7587         {
7588           fprintf (stderr, "va_arg: float type");
7589           debug_tree (type);
7590         }
7591
7592       /* FP args go in FP registers, if present.  */
7593       indirect_p = 0;
7594       reg = fpr;
7595       n_reg = 1;
7596       sav_ofs = TARGET_KERNEL_BACKCHAIN ? 0 : 16 * UNITS_PER_WORD;
7597       sav_scale = 8;
7598       /* TARGET_64BIT has up to 4 parameter in fprs */
7599       max_reg = TARGET_64BIT ? 3 : 1;
7600     }
7601   else
7602     {
7603       if (TARGET_DEBUG_ARG)
7604         {
7605           fprintf (stderr, "va_arg: other type");
7606           debug_tree (type);
7607         }
7608
7609       /* Otherwise into GP registers.  */
7610       indirect_p = 0;
7611       reg = gpr;
7612       n_reg = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
7613
7614       /* TARGET_KERNEL_BACKCHAIN on 31 bit: It is assumed here that no padding
7615         will be added by s390_frame_info because for va_args always an even
7616         number of gprs has to be saved r15-r2 = 14 regs.  */
7617       sav_ofs = TARGET_KERNEL_BACKCHAIN ? 
7618         (TARGET_64BIT ? 4 : 2) * 8 : 2*UNITS_PER_WORD;
7619
7620       if (size < UNITS_PER_WORD)
7621         sav_ofs += UNITS_PER_WORD - size;
7622
7623       sav_scale = UNITS_PER_WORD;
7624       if (n_reg > 1)
7625         max_reg = 3;
7626       else
7627         max_reg = 4;
7628     }
7629
7630   /* Pull the value out of the saved registers ...  */
7631
7632   lab_false = create_artificial_label ();
7633   lab_over = create_artificial_label ();
7634   addr = create_tmp_var (ptr_type_node, "addr");
7635
7636   t = fold_convert (TREE_TYPE (reg), size_int (max_reg));
7637   t = build2 (GT_EXPR, boolean_type_node, reg, t);
7638   u = build1 (GOTO_EXPR, void_type_node, lab_false);
7639   t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
7640   gimplify_and_add (t, pre_p);
7641
7642   t = build2 (PLUS_EXPR, ptr_type_node, sav, 
7643               fold_convert (ptr_type_node, size_int (sav_ofs)));
7644   u = build2 (MULT_EXPR, TREE_TYPE (reg), reg, 
7645               fold_convert (TREE_TYPE (reg), size_int (sav_scale)));
7646   t = build2 (PLUS_EXPR, ptr_type_node, t, fold_convert (ptr_type_node, u));
7647
7648   t = build2 (MODIFY_EXPR, void_type_node, addr, t);
7649   gimplify_and_add (t, pre_p);
7650
7651   t = build1 (GOTO_EXPR, void_type_node, lab_over);
7652   gimplify_and_add (t, pre_p);
7653
7654   t = build1 (LABEL_EXPR, void_type_node, lab_false);
7655   append_to_statement_list (t, pre_p);
7656
7657
7658   /* ... Otherwise out of the overflow area.  */
7659
7660   t = ovf;
7661   if (size < UNITS_PER_WORD)
7662     t = build2 (PLUS_EXPR, ptr_type_node, t, 
7663                 fold_convert (ptr_type_node, size_int (UNITS_PER_WORD - size)));
7664
7665   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
7666
7667   u = build2 (MODIFY_EXPR, void_type_node, addr, t);
7668   gimplify_and_add (u, pre_p);
7669
7670   t = build2 (PLUS_EXPR, ptr_type_node, t, 
7671               fold_convert (ptr_type_node, size_int (size)));
7672   t = build2 (MODIFY_EXPR, ptr_type_node, ovf, t);
7673   gimplify_and_add (t, pre_p);
7674
7675   t = build1 (LABEL_EXPR, void_type_node, lab_over);
7676   append_to_statement_list (t, pre_p);
7677
7678
7679   /* Increment register save count.  */
7680
7681   u = build2 (PREINCREMENT_EXPR, TREE_TYPE (reg), reg,
7682               fold_convert (TREE_TYPE (reg), size_int (n_reg)));
7683   gimplify_and_add (u, pre_p);
7684
7685   if (indirect_p)
7686     {
7687       t = build_pointer_type (build_pointer_type (type));
7688       addr = fold_convert (t, addr);
7689       addr = build_va_arg_indirect_ref (addr);
7690     }
7691   else
7692     {
7693       t = build_pointer_type (type);
7694       addr = fold_convert (t, addr);
7695     }
7696
7697   return build_va_arg_indirect_ref (addr);
7698 }
7699
7700
7701 /* Builtins.  */
7702
7703 enum s390_builtin
7704 {
7705   S390_BUILTIN_THREAD_POINTER,
7706   S390_BUILTIN_SET_THREAD_POINTER,
7707
7708   S390_BUILTIN_max
7709 };
7710
7711 static unsigned int const code_for_builtin_64[S390_BUILTIN_max] = {
7712   CODE_FOR_get_tp_64,
7713   CODE_FOR_set_tp_64
7714 };
7715
7716 static unsigned int const code_for_builtin_31[S390_BUILTIN_max] = {
7717   CODE_FOR_get_tp_31,
7718   CODE_FOR_set_tp_31
7719 };
7720
7721 static void
7722 s390_init_builtins (void)
7723 {
7724   tree ftype;
7725
7726   ftype = build_function_type (ptr_type_node, void_list_node);
7727   lang_hooks.builtin_function ("__builtin_thread_pointer", ftype,
7728                                S390_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
7729                                NULL, NULL_TREE);
7730
7731   ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
7732   lang_hooks.builtin_function ("__builtin_set_thread_pointer", ftype,
7733                                S390_BUILTIN_SET_THREAD_POINTER, BUILT_IN_MD,
7734                                NULL, NULL_TREE);
7735 }
7736
7737 /* Expand an expression EXP that calls a built-in function,
7738    with result going to TARGET if that's convenient
7739    (and in mode MODE if that's convenient).
7740    SUBTARGET may be used as the target for computing one of EXP's operands.
7741    IGNORE is nonzero if the value is to be ignored.  */
7742
7743 static rtx
7744 s390_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
7745                      enum machine_mode mode ATTRIBUTE_UNUSED,
7746                      int ignore ATTRIBUTE_UNUSED)
7747 {
7748 #define MAX_ARGS 2
7749
7750   unsigned int const *code_for_builtin =
7751     TARGET_64BIT ? code_for_builtin_64 : code_for_builtin_31;
7752
7753   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7754   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
7755   tree arglist = TREE_OPERAND (exp, 1);
7756   enum insn_code icode;
7757   rtx op[MAX_ARGS], pat;
7758   int arity;
7759   bool nonvoid;
7760
7761   if (fcode >= S390_BUILTIN_max)
7762     internal_error ("bad builtin fcode");
7763   icode = code_for_builtin[fcode];
7764   if (icode == 0)
7765     internal_error ("bad builtin fcode");
7766
7767   nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
7768
7769   for (arglist = TREE_OPERAND (exp, 1), arity = 0;
7770        arglist;
7771        arglist = TREE_CHAIN (arglist), arity++)
7772     {
7773       const struct insn_operand_data *insn_op;
7774
7775       tree arg = TREE_VALUE (arglist);
7776       if (arg == error_mark_node)
7777         return NULL_RTX;
7778       if (arity > MAX_ARGS)
7779         return NULL_RTX;
7780
7781       insn_op = &insn_data[icode].operand[arity + nonvoid];
7782
7783       op[arity] = expand_expr (arg, NULL_RTX, insn_op->mode, 0);
7784
7785       if (!(*insn_op->predicate) (op[arity], insn_op->mode))
7786         op[arity] = copy_to_mode_reg (insn_op->mode, op[arity]);
7787     }
7788
7789   if (nonvoid)
7790     {
7791       enum machine_mode tmode = insn_data[icode].operand[0].mode;
7792       if (!target
7793           || GET_MODE (target) != tmode
7794           || !(*insn_data[icode].operand[0].predicate) (target, tmode))
7795         target = gen_reg_rtx (tmode);
7796     }
7797
7798   switch (arity)
7799     {
7800     case 0:
7801       pat = GEN_FCN (icode) (target);
7802       break;
7803     case 1:
7804       if (nonvoid)
7805         pat = GEN_FCN (icode) (target, op[0]);
7806       else
7807         pat = GEN_FCN (icode) (op[0]);
7808       break;
7809     case 2:
7810       pat = GEN_FCN (icode) (target, op[0], op[1]);
7811       break;
7812     default:
7813       abort ();
7814     }
7815   if (!pat)
7816     return NULL_RTX;
7817   emit_insn (pat);
7818
7819   if (nonvoid)
7820     return target;
7821   else
7822     return const0_rtx;
7823 }
7824
7825
7826 /* Output assembly code for the trampoline template to
7827    stdio stream FILE.
7828
7829    On S/390, we use gpr 1 internally in the trampoline code;
7830    gpr 0 is used to hold the static chain.  */
7831
7832 void
7833 s390_trampoline_template (FILE *file)
7834 {
7835   rtx op[2];
7836   op[0] = gen_rtx_REG (Pmode, 0);
7837   op[1] = gen_rtx_REG (Pmode, 1);
7838
7839   if (TARGET_64BIT)
7840     {
7841       output_asm_insn ("basr\t%1,0", op);
7842       output_asm_insn ("lmg\t%0,%1,14(%1)", op);
7843       output_asm_insn ("br\t%1", op);
7844       ASM_OUTPUT_SKIP (file, (HOST_WIDE_INT)(TRAMPOLINE_SIZE - 10));
7845     }
7846   else
7847     {
7848       output_asm_insn ("basr\t%1,0", op);
7849       output_asm_insn ("lm\t%0,%1,6(%1)", op);
7850       output_asm_insn ("br\t%1", op);
7851       ASM_OUTPUT_SKIP (file, (HOST_WIDE_INT)(TRAMPOLINE_SIZE - 8));
7852     }
7853 }
7854
7855 /* Emit RTL insns to initialize the variable parts of a trampoline.
7856    FNADDR is an RTX for the address of the function's pure code.
7857    CXT is an RTX for the static chain value for the function.  */
7858
7859 void
7860 s390_initialize_trampoline (rtx addr, rtx fnaddr, rtx cxt)
7861 {
7862   emit_move_insn (gen_rtx_MEM (Pmode,
7863                    memory_address (Pmode,
7864                    plus_constant (addr, (TARGET_64BIT ? 16 : 8)))), cxt);
7865   emit_move_insn (gen_rtx_MEM (Pmode,
7866                    memory_address (Pmode,
7867                    plus_constant (addr, (TARGET_64BIT ? 24 : 12)))), fnaddr);
7868 }
7869
7870 /* Return rtx for 64-bit constant formed from the 32-bit subwords
7871    LOW and HIGH, independent of the host word size.  */
7872
7873 rtx
7874 s390_gen_rtx_const_DI (int high, int low)
7875 {
7876 #if HOST_BITS_PER_WIDE_INT >= 64
7877   HOST_WIDE_INT val;
7878   val = (HOST_WIDE_INT)high;
7879   val <<= 32;
7880   val |= (HOST_WIDE_INT)low;
7881
7882   return GEN_INT (val);
7883 #else
7884 #if HOST_BITS_PER_WIDE_INT >= 32
7885   return immed_double_const ((HOST_WIDE_INT)low, (HOST_WIDE_INT)high, DImode);
7886 #else
7887   abort ();
7888 #endif
7889 #endif
7890 }
7891
7892 /* Output assembler code to FILE to increment profiler label # LABELNO
7893    for profiling a function entry.  */
7894
7895 void
7896 s390_function_profiler (FILE *file, int labelno)
7897 {
7898   rtx op[7];
7899
7900   char label[128];
7901   ASM_GENERATE_INTERNAL_LABEL (label, "LP", labelno);
7902
7903   fprintf (file, "# function profiler \n");
7904
7905   op[0] = gen_rtx_REG (Pmode, RETURN_REGNUM);
7906   op[1] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
7907   op[1] = gen_rtx_MEM (Pmode, plus_constant (op[1], UNITS_PER_WORD));
7908
7909   op[2] = gen_rtx_REG (Pmode, 1);
7910   op[3] = gen_rtx_SYMBOL_REF (Pmode, label);
7911   SYMBOL_REF_FLAGS (op[3]) = SYMBOL_FLAG_LOCAL;
7912
7913   op[4] = gen_rtx_SYMBOL_REF (Pmode, "_mcount");
7914   if (flag_pic)
7915     {
7916       op[4] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op[4]), UNSPEC_PLT);
7917       op[4] = gen_rtx_CONST (Pmode, op[4]);
7918     }
7919
7920   if (TARGET_64BIT)
7921     {
7922       output_asm_insn ("stg\t%0,%1", op);
7923       output_asm_insn ("larl\t%2,%3", op);
7924       output_asm_insn ("brasl\t%0,%4", op);
7925       output_asm_insn ("lg\t%0,%1", op);
7926     }
7927   else if (!flag_pic)
7928     {
7929       op[6] = gen_label_rtx ();
7930
7931       output_asm_insn ("st\t%0,%1", op);
7932       output_asm_insn ("bras\t%2,%l6", op);
7933       output_asm_insn (".long\t%4", op);
7934       output_asm_insn (".long\t%3", op);
7935       targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[6]));
7936       output_asm_insn ("l\t%0,0(%2)", op);
7937       output_asm_insn ("l\t%2,4(%2)", op);
7938       output_asm_insn ("basr\t%0,%0", op);
7939       output_asm_insn ("l\t%0,%1", op);
7940     }
7941   else
7942     {
7943       op[5] = gen_label_rtx ();
7944       op[6] = gen_label_rtx ();
7945
7946       output_asm_insn ("st\t%0,%1", op);
7947       output_asm_insn ("bras\t%2,%l6", op);
7948       targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[5]));
7949       output_asm_insn (".long\t%4-%l5", op);
7950       output_asm_insn (".long\t%3-%l5", op);
7951       targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[6]));
7952       output_asm_insn ("lr\t%0,%2", op);
7953       output_asm_insn ("a\t%0,0(%2)", op);
7954       output_asm_insn ("a\t%2,4(%2)", op);
7955       output_asm_insn ("basr\t%0,%0", op);
7956       output_asm_insn ("l\t%0,%1", op);
7957     }
7958 }
7959
7960 /* Encode symbol attributes (local vs. global, tls model) of a SYMBOL_REF
7961    into its SYMBOL_REF_FLAGS.  */
7962
7963 static void
7964 s390_encode_section_info (tree decl, rtx rtl, int first)
7965 {
7966   default_encode_section_info (decl, rtl, first);
7967
7968   /* If a variable has a forced alignment to < 2 bytes, mark it with
7969      SYMBOL_FLAG_ALIGN1 to prevent it from being used as LARL operand.  */
7970   if (TREE_CODE (decl) == VAR_DECL
7971       && DECL_USER_ALIGN (decl) && DECL_ALIGN (decl) < 16)
7972     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_ALIGN1;
7973 }
7974
7975 /* Output thunk to FILE that implements a C++ virtual function call (with
7976    multiple inheritance) to FUNCTION.  The thunk adjusts the this pointer
7977    by DELTA, and unless VCALL_OFFSET is zero, applies an additional adjustment
7978    stored at VCALL_OFFSET in the vtable whose address is located at offset 0
7979    relative to the resulting this pointer.  */
7980
7981 static void
7982 s390_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
7983                       HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
7984                       tree function)
7985 {
7986   rtx op[10];
7987   int nonlocal = 0;
7988
7989   /* Operand 0 is the target function.  */
7990   op[0] = XEXP (DECL_RTL (function), 0);
7991   if (flag_pic && !SYMBOL_REF_LOCAL_P (op[0]))
7992     {
7993       nonlocal = 1;
7994       op[0] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op[0]),
7995                               TARGET_64BIT ? UNSPEC_PLT : UNSPEC_GOT);
7996       op[0] = gen_rtx_CONST (Pmode, op[0]);
7997     }
7998
7999   /* Operand 1 is the 'this' pointer.  */
8000   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
8001     op[1] = gen_rtx_REG (Pmode, 3);
8002   else
8003     op[1] = gen_rtx_REG (Pmode, 2);
8004
8005   /* Operand 2 is the delta.  */
8006   op[2] = GEN_INT (delta);
8007
8008   /* Operand 3 is the vcall_offset.  */
8009   op[3] = GEN_INT (vcall_offset);
8010
8011   /* Operand 4 is the temporary register.  */
8012   op[4] = gen_rtx_REG (Pmode, 1);
8013
8014   /* Operands 5 to 8 can be used as labels.  */
8015   op[5] = NULL_RTX;
8016   op[6] = NULL_RTX;
8017   op[7] = NULL_RTX;
8018   op[8] = NULL_RTX;
8019
8020   /* Operand 9 can be used for temporary register.  */
8021   op[9] = NULL_RTX;
8022
8023   /* Generate code.  */
8024   if (TARGET_64BIT)
8025     {
8026       /* Setup literal pool pointer if required.  */
8027       if ((!DISP_IN_RANGE (delta)
8028            && !CONST_OK_FOR_CONSTRAINT_P (delta, 'K', "K"))
8029           || (!DISP_IN_RANGE (vcall_offset)
8030               && !CONST_OK_FOR_CONSTRAINT_P (vcall_offset, 'K', "K")))
8031         {
8032           op[5] = gen_label_rtx ();
8033           output_asm_insn ("larl\t%4,%5", op);
8034         }
8035
8036       /* Add DELTA to this pointer.  */
8037       if (delta)
8038         {
8039           if (CONST_OK_FOR_CONSTRAINT_P (delta, 'J', "J"))
8040             output_asm_insn ("la\t%1,%2(%1)", op);
8041           else if (DISP_IN_RANGE (delta))
8042             output_asm_insn ("lay\t%1,%2(%1)", op);
8043           else if (CONST_OK_FOR_CONSTRAINT_P (delta, 'K', "K"))
8044             output_asm_insn ("aghi\t%1,%2", op);
8045           else
8046             {
8047               op[6] = gen_label_rtx ();
8048               output_asm_insn ("agf\t%1,%6-%5(%4)", op);
8049             }
8050         }
8051
8052       /* Perform vcall adjustment.  */
8053       if (vcall_offset)
8054         {
8055           if (DISP_IN_RANGE (vcall_offset))
8056             {
8057               output_asm_insn ("lg\t%4,0(%1)", op);
8058               output_asm_insn ("ag\t%1,%3(%4)", op);
8059             }
8060           else if (CONST_OK_FOR_CONSTRAINT_P (vcall_offset, 'K', "K"))
8061             {
8062               output_asm_insn ("lghi\t%4,%3", op);
8063               output_asm_insn ("ag\t%4,0(%1)", op);
8064               output_asm_insn ("ag\t%1,0(%4)", op);
8065             }
8066           else
8067             {
8068               op[7] = gen_label_rtx ();
8069               output_asm_insn ("llgf\t%4,%7-%5(%4)", op);
8070               output_asm_insn ("ag\t%4,0(%1)", op);
8071               output_asm_insn ("ag\t%1,0(%4)", op);
8072             }
8073         }
8074
8075       /* Jump to target.  */
8076       output_asm_insn ("jg\t%0", op);
8077
8078       /* Output literal pool if required.  */
8079       if (op[5])
8080         {
8081           output_asm_insn (".align\t4", op);
8082           targetm.asm_out.internal_label (file, "L",
8083                                           CODE_LABEL_NUMBER (op[5]));
8084         }
8085       if (op[6])
8086         {
8087           targetm.asm_out.internal_label (file, "L",
8088                                           CODE_LABEL_NUMBER (op[6]));
8089           output_asm_insn (".long\t%2", op);
8090         }
8091       if (op[7])
8092         {
8093           targetm.asm_out.internal_label (file, "L",
8094                                           CODE_LABEL_NUMBER (op[7]));
8095           output_asm_insn (".long\t%3", op);
8096         }
8097     }
8098   else
8099     {
8100       /* Setup base pointer if required.  */
8101       if (!vcall_offset
8102           || (!DISP_IN_RANGE (delta)
8103               && !CONST_OK_FOR_CONSTRAINT_P (delta, 'K', "K"))
8104           || (!DISP_IN_RANGE (delta)
8105               && !CONST_OK_FOR_CONSTRAINT_P (vcall_offset, 'K', "K")))
8106         {
8107           op[5] = gen_label_rtx ();
8108           output_asm_insn ("basr\t%4,0", op);
8109           targetm.asm_out.internal_label (file, "L",
8110                                           CODE_LABEL_NUMBER (op[5]));
8111         }
8112
8113       /* Add DELTA to this pointer.  */
8114       if (delta)
8115         {
8116           if (CONST_OK_FOR_CONSTRAINT_P (delta, 'J', "J"))
8117             output_asm_insn ("la\t%1,%2(%1)", op);
8118           else if (DISP_IN_RANGE (delta))
8119             output_asm_insn ("lay\t%1,%2(%1)", op);
8120           else if (CONST_OK_FOR_CONSTRAINT_P (delta, 'K', "K"))
8121             output_asm_insn ("ahi\t%1,%2", op);
8122           else
8123             {
8124               op[6] = gen_label_rtx ();
8125               output_asm_insn ("a\t%1,%6-%5(%4)", op);
8126             }
8127         }
8128
8129       /* Perform vcall adjustment.  */
8130       if (vcall_offset)
8131         {
8132           if (CONST_OK_FOR_CONSTRAINT_P (vcall_offset, 'J', "J"))
8133             {
8134               output_asm_insn ("lg\t%4,0(%1)", op);
8135               output_asm_insn ("a\t%1,%3(%4)", op);
8136             }
8137           else if (DISP_IN_RANGE (vcall_offset))
8138             {
8139               output_asm_insn ("lg\t%4,0(%1)", op);
8140               output_asm_insn ("ay\t%1,%3(%4)", op);
8141             }
8142           else if (CONST_OK_FOR_CONSTRAINT_P (vcall_offset, 'K', "K"))
8143             {
8144               output_asm_insn ("lhi\t%4,%3", op);
8145               output_asm_insn ("a\t%4,0(%1)", op);
8146               output_asm_insn ("a\t%1,0(%4)", op);
8147             }
8148           else
8149             {
8150               op[7] = gen_label_rtx ();
8151               output_asm_insn ("l\t%4,%7-%5(%4)", op);
8152               output_asm_insn ("a\t%4,0(%1)", op);
8153               output_asm_insn ("a\t%1,0(%4)", op);
8154             }
8155
8156           /* We had to clobber the base pointer register.
8157              Re-setup the base pointer (with a different base).  */
8158           op[5] = gen_label_rtx ();
8159           output_asm_insn ("basr\t%4,0", op);
8160           targetm.asm_out.internal_label (file, "L",
8161                                           CODE_LABEL_NUMBER (op[5]));
8162         }
8163
8164       /* Jump to target.  */
8165       op[8] = gen_label_rtx ();
8166
8167       if (!flag_pic)
8168         output_asm_insn ("l\t%4,%8-%5(%4)", op);
8169       else if (!nonlocal)
8170         output_asm_insn ("a\t%4,%8-%5(%4)", op);
8171       /* We cannot call through .plt, since .plt requires %r12 loaded.  */
8172       else if (flag_pic == 1)
8173         {
8174           output_asm_insn ("a\t%4,%8-%5(%4)", op);
8175           output_asm_insn ("l\t%4,%0(%4)", op);
8176         }
8177       else if (flag_pic == 2)
8178         {
8179           op[9] = gen_rtx_REG (Pmode, 0);
8180           output_asm_insn ("l\t%9,%8-4-%5(%4)", op);
8181           output_asm_insn ("a\t%4,%8-%5(%4)", op);
8182           output_asm_insn ("ar\t%4,%9", op);
8183           output_asm_insn ("l\t%4,0(%4)", op);
8184         }
8185
8186       output_asm_insn ("br\t%4", op);
8187
8188       /* Output literal pool.  */
8189       output_asm_insn (".align\t4", op);
8190
8191       if (nonlocal && flag_pic == 2)
8192         output_asm_insn (".long\t%0", op);
8193       if (nonlocal)
8194         {
8195           op[0] = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
8196           SYMBOL_REF_FLAGS (op[0]) = SYMBOL_FLAG_LOCAL;
8197         }
8198
8199       targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (op[8]));
8200       if (!flag_pic)
8201         output_asm_insn (".long\t%0", op);
8202       else
8203         output_asm_insn (".long\t%0-%5", op);
8204
8205       if (op[6])
8206         {
8207           targetm.asm_out.internal_label (file, "L",
8208                                           CODE_LABEL_NUMBER (op[6]));
8209           output_asm_insn (".long\t%2", op);
8210         }
8211       if (op[7])
8212         {
8213           targetm.asm_out.internal_label (file, "L",
8214                                           CODE_LABEL_NUMBER (op[7]));
8215           output_asm_insn (".long\t%3", op);
8216         }
8217     }
8218 }
8219
8220 bool
8221 s390_valid_pointer_mode (enum machine_mode mode)
8222 {
8223   return (mode == SImode || (TARGET_64BIT && mode == DImode));
8224 }
8225
8226 /* How to allocate a 'struct machine_function'.  */
8227
8228 static struct machine_function *
8229 s390_init_machine_status (void)
8230 {
8231   return ggc_alloc_cleared (sizeof (struct machine_function));
8232 }
8233
8234 /* Checks whether the given ARGUMENT_LIST would use a caller
8235    saved register.  This is used to decide whether sibling call
8236    optimization could be performed on the respective function
8237    call.  */
8238
8239 static bool
8240 s390_call_saved_register_used (tree argument_list)
8241 {
8242   CUMULATIVE_ARGS cum;
8243   tree parameter;
8244   enum machine_mode mode;
8245   tree type;
8246   rtx parm_rtx;
8247   int reg;
8248
8249   INIT_CUMULATIVE_ARGS (cum, NULL, NULL, 0, 0);
8250
8251   while (argument_list)
8252     {
8253       parameter = TREE_VALUE (argument_list);
8254       argument_list = TREE_CHAIN (argument_list);
8255
8256       if (!parameter)
8257         abort();
8258
8259       /* For an undeclared variable passed as parameter we will get
8260          an ERROR_MARK node here.  */
8261       if (TREE_CODE (parameter) == ERROR_MARK)
8262         return true;
8263
8264       if (! (type = TREE_TYPE (parameter)))
8265         abort();
8266
8267       if (! (mode = TYPE_MODE (TREE_TYPE (parameter))))
8268         abort();
8269
8270       if (pass_by_reference (&cum, mode, type, true))
8271         {
8272           mode = Pmode;
8273           type = build_pointer_type (type);
8274         }
8275
8276        parm_rtx = s390_function_arg (&cum, mode, type, 0);
8277
8278        s390_function_arg_advance (&cum, mode, type, 0);
8279
8280        if (parm_rtx && REG_P (parm_rtx))
8281          {
8282            for (reg = 0;
8283                 reg < HARD_REGNO_NREGS (REGNO (parm_rtx), GET_MODE (parm_rtx));
8284                 reg++)
8285              if (! call_used_regs[reg + REGNO (parm_rtx)])
8286                return true;
8287          }
8288     }
8289   return false;
8290 }
8291
8292 /* Return true if the given call expression can be
8293    turned into a sibling call.
8294    DECL holds the declaration of the function to be called whereas
8295    EXP is the call expression itself.  */
8296
8297 static bool
8298 s390_function_ok_for_sibcall (tree decl, tree exp)
8299 {
8300   /* The TPF epilogue uses register 1.  */
8301   if (TARGET_TPF_PROFILING)
8302     return false;
8303
8304   /* The 31 bit PLT code uses register 12 (GOT pointer - caller saved)
8305      which would have to be restored before the sibcall.  */
8306   if (!TARGET_64BIT && flag_pic && decl && TREE_PUBLIC (decl))
8307     return false;
8308
8309   /* Register 6 on s390 is available as an argument register but unfortunately
8310      "caller saved". This makes functions needing this register for arguments
8311      not suitable for sibcalls.  */
8312   if (TREE_OPERAND (exp, 1)
8313       && s390_call_saved_register_used (TREE_OPERAND (exp, 1)))
8314       return false;
8315
8316   return true;
8317 }
8318
8319 /* This function is used by the call expanders of the machine description.
8320    It emits the call insn itself together with the necessary operations
8321    to adjust the target address and returns the emitted insn.
8322    ADDR_LOCATION is the target address rtx
8323    TLS_CALL the location of the thread-local symbol
8324    RESULT_REG the register where the result of the call should be stored
8325    RETADDR_REG the register where the return address should be stored
8326                If this parameter is NULL_RTX the call is considered
8327                to be a sibling call.  */
8328
8329 rtx
8330 s390_emit_call (rtx addr_location, rtx tls_call, rtx result_reg,
8331                 rtx retaddr_reg)
8332 {
8333   bool plt_call = false;
8334   rtx insn;
8335   rtx call;
8336   rtx clobber;
8337   rtvec vec;
8338
8339   /* Direct function calls need special treatment.  */
8340   if (GET_CODE (addr_location) == SYMBOL_REF)
8341     {
8342       /* When calling a global routine in PIC mode, we must
8343          replace the symbol itself with the PLT stub.  */
8344       if (flag_pic && !SYMBOL_REF_LOCAL_P (addr_location))
8345         {
8346           addr_location = gen_rtx_UNSPEC (Pmode,
8347                                           gen_rtvec (1, addr_location),
8348                                           UNSPEC_PLT);
8349           addr_location = gen_rtx_CONST (Pmode, addr_location);
8350           plt_call = true;
8351         }
8352
8353       /* Unless we can use the bras(l) insn, force the
8354          routine address into a register.  */
8355       if (!TARGET_SMALL_EXEC && !TARGET_CPU_ZARCH)
8356         {
8357           if (flag_pic)
8358             addr_location = legitimize_pic_address (addr_location, 0);
8359           else
8360             addr_location = force_reg (Pmode, addr_location);
8361         }
8362     }
8363
8364   /* If it is already an indirect call or the code above moved the
8365      SYMBOL_REF to somewhere else make sure the address can be found in
8366      register 1.  */
8367   if (retaddr_reg == NULL_RTX
8368       && GET_CODE (addr_location) != SYMBOL_REF
8369       && !plt_call)
8370     {
8371       emit_move_insn (gen_rtx_REG (Pmode, SIBCALL_REGNUM), addr_location);
8372       addr_location = gen_rtx_REG (Pmode, SIBCALL_REGNUM);
8373     }
8374
8375   addr_location = gen_rtx_MEM (QImode, addr_location);
8376   call = gen_rtx_CALL (VOIDmode, addr_location, const0_rtx);
8377
8378   if (result_reg != NULL_RTX)
8379     call = gen_rtx_SET (VOIDmode, result_reg, call);
8380
8381   if (retaddr_reg != NULL_RTX)
8382     {
8383       clobber = gen_rtx_CLOBBER (VOIDmode, retaddr_reg);
8384
8385       if (tls_call != NULL_RTX)
8386         vec = gen_rtvec (3, call, clobber,
8387                          gen_rtx_USE (VOIDmode, tls_call));
8388       else
8389         vec = gen_rtvec (2, call, clobber);
8390
8391       call = gen_rtx_PARALLEL (VOIDmode, vec);
8392     }
8393
8394   insn = emit_call_insn (call);
8395
8396   /* 31-bit PLT stubs and tls calls use the GOT register implicitly.  */
8397   if ((!TARGET_64BIT && plt_call) || tls_call != NULL_RTX)
8398     {
8399       /* s390_function_ok_for_sibcall should
8400          have denied sibcalls in this case.  */
8401       if (retaddr_reg == NULL_RTX)
8402         abort ();
8403
8404       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
8405     }
8406   return insn;
8407 }
8408
8409 /* Implement CONDITIONAL_REGISTER_USAGE.  */
8410
8411 void
8412 s390_conditional_register_usage (void)
8413 {
8414   int i;
8415
8416   if (flag_pic)
8417     {
8418       fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
8419       call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
8420     }
8421   if (TARGET_CPU_ZARCH)
8422     {
8423       fixed_regs[RETURN_REGNUM] = 0;
8424       call_used_regs[RETURN_REGNUM] = 0;
8425     }
8426   if (TARGET_64BIT)
8427     {
8428       for (i = 24; i < 32; i++)
8429         call_used_regs[i] = call_really_used_regs[i] = 0;
8430     }
8431   else
8432     {
8433       for (i = 18; i < 20; i++)
8434         call_used_regs[i] = call_really_used_regs[i] = 0;
8435     }
8436
8437   if (TARGET_SOFT_FLOAT)
8438     {
8439       for (i = 16; i < 32; i++)
8440         call_used_regs[i] = fixed_regs[i] = 1;
8441     }
8442 }
8443
8444 /* Corresponding function to eh_return expander.  */
8445
8446 static GTY(()) rtx s390_tpf_eh_return_symbol;
8447 void
8448 s390_emit_tpf_eh_return (rtx target)
8449 {
8450   rtx insn, reg;
8451
8452   if (!s390_tpf_eh_return_symbol)
8453     s390_tpf_eh_return_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tpf_eh_return");
8454
8455   reg = gen_rtx_REG (Pmode, 2);
8456
8457   emit_move_insn (reg, target);
8458   insn = s390_emit_call (s390_tpf_eh_return_symbol, NULL_RTX, reg,
8459                                      gen_rtx_REG (Pmode, RETURN_REGNUM));
8460   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
8461
8462   emit_move_insn (EH_RETURN_HANDLER_RTX, reg);
8463 }
8464
8465 #include "gt-s390.h"