OSDN Git Service

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