OSDN Git Service

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