OSDN Git Service

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