OSDN Git Service

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