OSDN Git Service

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